diff --git a/Sources/QuotaCore/MuseCodeSessionReader.swift b/Sources/QuotaCore/MuseCodeSessionReader.swift index 078d9fcc..01cc0cfd 100644 --- a/Sources/QuotaCore/MuseCodeSessionReader.swift +++ b/Sources/QuotaCore/MuseCodeSessionReader.swift @@ -171,6 +171,63 @@ public struct MuseCodeSessionResult: Equatable, Sendable { /// snapshot files directly (the newest snapshot in a directory is the one /// HEAD.json names), which covers both the latest-segment and the /// segment-summed views the fleet's surveys validated. +/// A process-wide parse cache for snapshot segments. The session stores +/// write each snapshot once under a fresh UUID name and rotate by replacement +/// (no reused snapshot names in the live store), and the key also carries the +/// file's resource identity, so even a path reused after replacement cannot +/// serve stale content: an unchanged snapshot never needs a second read and +/// parse across the one-minute refresh cycles. +final class MuseCodeSnapshotSegmentCache: @unchecked Sendable { + private let lock = NSLock() + private var entries: [String: MuseCodeTokenSegment] = [:] + private let cap = 8_192 + private var hits = 0 + private var misses = 0 + + /// `fileId` carries the caller's inode@birthtime identity for the file, + /// collected in the same enumeration pass as its mtime and size so the + /// hot loop stats each snapshot once. It distinguishes a replacement + /// from the file it replaced even when path, size, and mtime collide. + func segment(sessionId: String, url: URL, modified: Date, size: Int, + fileId: String, + parse: () -> MuseCodeTokenSegment?) -> MuseCodeTokenSegment? { + let key = url.path + "|" + fileId + "|" + String(modified.timeIntervalSince1970) + "|" + String(size) + lock.lock() + if let cached = entries[key] { + hits += 1 + lock.unlock() + return cached + } + misses += 1 + lock.unlock() + guard let parsed = parse() else { return nil } + lock.lock() + // Overflow evicts a single entry rather than clearing the map: a + // working set over the cap then degrades to partial misses instead + // of turning the next scan into all misses and full re-parses. + if entries[key] == nil && entries.count >= cap, let victim = entries.keys.first { + entries.removeValue(forKey: victim) + } + entries[key] = parsed + lock.unlock() + return parsed + } + + func reset() { + lock.lock() + entries.removeAll() + hits = 0 + misses = 0 + lock.unlock() + } + + func stats() -> (hits: Int, misses: Int) { + lock.lock() + defer { lock.unlock() } + return (hits, misses) + } +} + public struct MuseCodeSessionReader: Sendable { public static let storeRelativePath = ".local/share/muse/sessions/.msp-view-g6" public static let windowMinutes = [60, 90] @@ -180,6 +237,9 @@ public struct MuseCodeSessionReader: Sendable { private static let maxSnapshotBytes = 33_554_432 private static let futureTolerance: TimeInterval = 120 + /// Shared across readers so the one-minute refresh reuses parses. + static let snapshotSegmentCache = MuseCodeSnapshotSegmentCache() + private let homeDirectory: URL private let now: @Sendable () -> Date @@ -214,10 +274,15 @@ public struct MuseCodeSessionReader: Sendable { let sessionId = directory.lastPathComponent guard let files = try? FileManager.default.contentsOfDirectory( at: directory, - includingPropertiesForKeys: [.contentModificationDateKey, .fileSizeKey]) else { continue } - let dated: [(url: URL, modified: Date, size: Int)] = files.map { url in - let values = try? url.resourceValues(forKeys: [.contentModificationDateKey, .fileSizeKey]) - return (url, values?.contentModificationDate ?? .distantPast, values?.fileSize ?? 0) + includingPropertiesForKeys: [.contentModificationDateKey, .fileSizeKey, + .fileResourceIdentifierKey, .creationDateKey]) else { continue } + let dated: [(url: URL, modified: Date, size: Int, fileId: String)] = files.map { url in + let values = try? url.resourceValues(forKeys: [.contentModificationDateKey, .fileSizeKey, + .fileResourceIdentifierKey, .creationDateKey]) + let fileId = values?.fileResourceIdentifier.map { String(describing: $0) } ?? "" + let birth = values?.creationDate.map { String($0.timeIntervalSince1970) } ?? "" + return (url, values?.contentModificationDate ?? .distantPast, values?.fileSize ?? 0, + fileId + "@" + birth) } // A session counts as active in a window when any of its files — // HEAD.json included — was written inside it, the same activity @@ -243,7 +308,7 @@ public struct MuseCodeSessionReader: Sendable { selected.append(headSnapshot) } let snapshots = selected.sorted { $0.modified < $1.modified } - let currentSnapshot: (url: URL, modified: Date, size: Int)? + let currentSnapshot: (url: URL, modified: Date, size: Int, fileId: String)? if let headId { // A valid HEAD id is authoritative. If its file is absent, the // current model is unknown; never substitute an older model. @@ -263,7 +328,8 @@ public struct MuseCodeSessionReader: Sendable { var parsed: [(snapshotId: String?, segment: MuseCodeTokenSegment)] = [] for snapshot in snapshots { if let segment = Self.segment(sessionId: sessionId, url: snapshot.url, - modified: snapshot.modified, size: snapshot.size) { + modified: snapshot.modified, size: snapshot.size, + fileId: snapshot.fileId) { segments.append(segment) parsed.append((Self.snapshotId(from: snapshot.url), segment)) } @@ -296,7 +362,7 @@ public struct MuseCodeSessionReader: Sendable { return String(name.dropFirst("snapshot-".count).dropLast(".json".count)) } - private static func headSnapshotId(in files: [(url: URL, modified: Date, size: Int)]) -> String? { + private static func headSnapshotId(in files: [(url: URL, modified: Date, size: Int, fileId: String)]) -> String? { guard let head = files.first(where: { $0.url.lastPathComponent == "HEAD.json" }), head.size > 0, head.size <= 1_048_576, let data = try? Data(contentsOf: head.url), @@ -323,7 +389,15 @@ public struct MuseCodeSessionReader: Sendable { ?? .distantPast } - private static func segment(sessionId: String, url: URL, modified: Date, size: Int) -> MuseCodeTokenSegment? { + private static func segment(sessionId: String, url: URL, modified: Date, size: Int, + fileId: String) -> MuseCodeTokenSegment? { + snapshotSegmentCache.segment(sessionId: sessionId, url: url, modified: modified, + size: size, fileId: fileId) { + parseSegment(sessionId: sessionId, url: url, modified: modified, size: size) + } + } + + private static func parseSegment(sessionId: String, url: URL, modified: Date, size: Int) -> MuseCodeTokenSegment? { guard size > 0, size <= maxSnapshotBytes, let data = try? Data(contentsOf: url), let root = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any], diff --git a/Tests/QuotaCoreTests/MuseCodeSessionReaderTests.swift b/Tests/QuotaCoreTests/MuseCodeSessionReaderTests.swift index 78caa4d7..04feb4e9 100644 --- a/Tests/QuotaCoreTests/MuseCodeSessionReaderTests.swift +++ b/Tests/QuotaCoreTests/MuseCodeSessionReaderTests.swift @@ -181,6 +181,146 @@ final class MuseCodeSessionReaderTests: XCTestCase { XCTAssertEqual(observation?.segmentKey, "a3") } + func testReaderServesUnchangedSnapshotsFromParseCache() throws { + let home = try makeHome() + defer { try? FileManager.default.removeItem(at: home) } + try writeSnapshot(home: home, session: "session-a", name: "snapshot-1.json", + usage: usage(model: "muse-spark-1.3-contributor", tokens: 2_000, first: "a1", last: "b1"), + modified: now.addingTimeInterval(-10 * 60)) + let cache = MuseCodeSessionReader.snapshotSegmentCache + cache.reset() + let reader = MuseCodeSessionReader(homeDirectory: home, now: { self.now }) + _ = reader.read() + let missesAfterFirst = cache.stats().misses + XCTAssertGreaterThan(missesAfterFirst, 0) + let again = reader.read() + XCTAssertEqual(cache.stats().misses, missesAfterFirst, + "a second read of an unchanged store parses nothing again") + XCTAssertNil(again.issue) + } + + func testParseCacheOverflowEvictsOneEntryInsteadOfClearingAll() { + let cache = MuseCodeSessionReader.snapshotSegmentCache + cache.reset() + defer { cache.reset() } + let cap = 8_192 + let stamp = Date(timeIntervalSince1970: 1_700_000_000) + let parse: () -> MuseCodeTokenSegment? = { + MuseCodeTokenSegment(sessionId: "s", modelId: "m", totalTokens: 1, observedAt: stamp) + } + for i in 0.. String { + let attrs = try? FileManager.default.attributesOfItem(atPath: url.path) + return attrs.map { + String(describing: $0[.systemFileNumber] ?? 0) + "@" + + String((($0[.creationDate] as? Date) ?? .distantPast).timeIntervalSince1970) + } ?? "" + } + + func testParseCacheReparseesReplacedFileWithIdenticalMetadata() throws { + // Rotation by replacement with a reused path, same size, and the same + // mtime: the creation-date component in the key must still force a + // re-parse. mtime is set to a FUTURE stamp on both files, because + // backdating mtime on macOS also drags birthtime down with it, which + // would make the files genuinely indistinguishable by metadata. + let cache = MuseCodeSessionReader.snapshotSegmentCache + cache.reset() + defer { cache.reset() } + let dir = FileManager.default.temporaryDirectory + .appendingPathComponent("cache-replacement-\(UUID().uuidString)") + try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: dir) } + let url = dir.appendingPathComponent("snapshot-1.json") + let stamp = Date(timeIntervalSince1970: 2_000_000_000) + var parses = 0 + let parse: () -> MuseCodeTokenSegment? = { + parses += 1 + return MuseCodeTokenSegment(sessionId: "s", modelId: "m", + totalTokens: parses, observedAt: stamp) + } + try Data(repeating: 0x61, count: 64).write(to: url) + try FileManager.default.setAttributes([.modificationDate: stamp], ofItemAtPath: url.path) + let first = cache.segment(sessionId: "s", url: url, modified: stamp, size: 64, + fileId: Self.snapshotFileId(at: url), parse: parse) + XCTAssertEqual(first?.totalTokens, 1) + // Replace the file at the same path, then set the same mtime. + try FileManager.default.removeItem(at: url) + try Data(repeating: 0x62, count: 64).write(to: url) + try FileManager.default.setAttributes([.modificationDate: stamp], ofItemAtPath: url.path) + let second = cache.segment(sessionId: "s", url: url, modified: stamp, size: 64, + fileId: Self.snapshotFileId(at: url), parse: parse) + XCTAssertEqual(parses, 2, "a replacement with identical path/size/mtime must not hit the old entry") + XCTAssertEqual(second?.totalTokens, 2, "the replacement's own content is returned, not the stale entry") + } + + func testReaderReparsesASnapshotReplacedWithIdenticalMetadata() throws { + let home = try makeHome() + defer { try? FileManager.default.removeItem(at: home) } + let stamp = now.addingTimeInterval(-10 * 60) + try writeSnapshot(home: home, session: "session-a", name: "snapshot-1.json", + usage: usage(model: "muse-spark-1.3-contributor", tokens: 2_000, first: "a1", last: "b1"), + modified: stamp) + let cache = MuseCodeSessionReader.snapshotSegmentCache + cache.reset() + let reader = MuseCodeSessionReader(homeDirectory: home, now: { self.now }) + let first = try XCTUnwrap(reader.read().report) + XCTAssertEqual(first.window(minutes: 90)?.totalTokens, 2_000) + let url = home.appendingPathComponent( + ".local/share/muse/sessions/.msp-view-g6/session-a/snapshot-1.json") + let sizeBefore = try XCTUnwrap( + (try? FileManager.default.attributesOfItem(atPath: url.path))?[.size] as? Int) + // Rotation by replacement: a new file at the same path with identical + // size and mtime - only the file identity in the cache key + // distinguishes it, and the identity must arrive through the reader's + // own enumeration, not a caller bypass. + try FileManager.default.removeItem(at: url) + try writeSnapshot(home: home, session: "session-a", name: "snapshot-1.json", + usage: usage(model: "muse-spark-1.3-contributor", tokens: 5_000, first: "a1", last: "b1"), + modified: stamp) + let sizeAfter = try XCTUnwrap( + (try? FileManager.default.attributesOfItem(atPath: url.path))?[.size] as? Int) + XCTAssertEqual(sizeBefore, sizeAfter, "test precondition: the replacement has identical size") + let second = try XCTUnwrap(reader.read().report) + XCTAssertEqual(second.window(minutes: 90)?.totalTokens, 5_000, + "a replacement with identical path/size/mtime is re-parsed through the reader's own enumeration") + } + + func testReaderReparsesASnapshotRewrittenInPlace() throws { + let home = try makeHome() + defer { try? FileManager.default.removeItem(at: home) } + try writeSnapshot(home: home, session: "session-a", name: "snapshot-1.json", + usage: usage(model: "muse-spark-1.3-contributor", tokens: 2_000, first: "a1", last: "b1"), + modified: now.addingTimeInterval(-10 * 60)) + let cache = MuseCodeSessionReader.snapshotSegmentCache + cache.reset() + let reader = MuseCodeSessionReader(homeDirectory: home, now: { self.now }) + let first = try XCTUnwrap(reader.read().report) + XCTAssertEqual(first.window(minutes: 90)?.totalTokens, 2_000) + try writeSnapshot(home: home, session: "session-a", name: "snapshot-1.json", + usage: usage(model: "muse-spark-1.3-contributor", tokens: 5_000, first: "a1", last: "b1"), + modified: now.addingTimeInterval(-5 * 60)) + let second = try XCTUnwrap(reader.read().report) + XCTAssertEqual(second.window(minutes: 90)?.totalTokens, 5_000, + "a rewritten snapshot has a new cache key, so the new content is parsed") + } + func testCurrentModelUsesNullHeadWithoutBackfillingAndPreservesHistoricalUsage() throws { let home = try makeHome() defer { try? FileManager.default.removeItem(at: home) }