From 6170f5c8c74504eb6e2c100cfaac2a52feb18f10 Mon Sep 17 00:00:00 2001 From: Bryan Font Date: Mon, 13 Jul 2026 11:43:01 -0400 Subject: [PATCH 1/2] Add shadow Codex lineage ledger --- .../Providers/Codex/CodexLineageLedger.swift | 163 +++++++++++++++ .../CodexLineageLedgerTests.swift | 195 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 Sources/CodexBarCore/Providers/Codex/CodexLineageLedger.swift create mode 100644 Tests/CodexBarTests/CodexLineageLedgerTests.swift diff --git a/Sources/CodexBarCore/Providers/Codex/CodexLineageLedger.swift b/Sources/CodexBarCore/Providers/Codex/CodexLineageLedger.swift new file mode 100644 index 0000000000..ad798a76a3 --- /dev/null +++ b/Sources/CodexBarCore/Providers/Codex/CodexLineageLedger.swift @@ -0,0 +1,163 @@ +import Foundation + +/// Experimental accounting model for Codex rollout families. +/// +/// Rollout files are overlapping physical views of a logical lineage. The ledger builds the +/// transitive lineage first, then admits each complete token observation once per lineage. +/// It intentionally does not participate in production cost totals yet. +enum CodexLineageLedger { + struct Totals: Equatable, Hashable, Sendable { + var input: Int + var cached: Int + var output: Int + + static let zero = Self(input: 0, cached: 0, output: 0) + + mutating func add(_ other: Self) { + self.input += other.input + self.cached += other.cached + self.output += other.output + } + } + + struct Observation: Equatable, Sendable { + let timestamp: String + let last: Totals + let total: Totals + } + + struct Document: Equatable, Sendable { + /// Canonical owner from the rollout filename when available. + let ownerID: String + /// Session identity persisted in metadata. Fork copies may retain an ancestor identity. + let metadataSessionID: String? + let parentSessionID: String? + let observations: [Observation] + } + + struct Report: Equatable, Sendable { + let utcDays: [String: Totals] + let localDays: [String: Totals] + let componentCount: Int + let acceptedObservationCount: Int + let duplicateObservationCount: Int + } + + enum LedgerError: Error, Equatable { + case emptyOwnerID + case invalidTimestamp(String) + } + + static func reconcile(documents: [Document], localTimeZone: TimeZone) throws -> Report { + var graph = DisjointSet() + for document in documents { + guard !document.ownerID.isEmpty else { throw LedgerError.emptyOwnerID } + graph.insert(document.ownerID) + if let metadataSessionID = Self.nonEmpty(document.metadataSessionID) { + graph.union(document.ownerID, metadataSessionID) + } + if let parentSessionID = Self.nonEmpty(document.parentSessionID) { + graph.union(document.ownerID, parentSessionID) + } + } + + var acceptedByComponent: [String: [Fingerprint: AcceptedObservation]] = [:] + var physicalObservationCount = 0 + for document in documents { + let componentID = graph.find(document.ownerID) + var accepted = acceptedByComponent[componentID] ?? [:] + for observation in document.observations { + physicalObservationCount += 1 + let date = try Self.date(from: observation.timestamp) + let fingerprint = Fingerprint(last: observation.last, total: observation.total) + if let existing = accepted[fingerprint], existing.date <= date { + continue + } + accepted[fingerprint] = AcceptedObservation(date: date, last: observation.last) + } + acceptedByComponent[componentID] = accepted + } + + var utcDays: [String: Totals] = [:] + var localDays: [String: Totals] = [:] + var acceptedObservationCount = 0 + for accepted in acceptedByComponent.values { + acceptedObservationCount += accepted.count + for observation in accepted.values { + Self.add(observation.last, on: observation.date, timeZone: .gmt, to: &utcDays) + Self.add(observation.last, on: observation.date, timeZone: localTimeZone, to: &localDays) + } + } + + return Report( + utcDays: utcDays, + localDays: localDays, + componentCount: Set(documents.map { graph.find($0.ownerID) }).count, + acceptedObservationCount: acceptedObservationCount, + duplicateObservationCount: physicalObservationCount - acceptedObservationCount) + } + + private struct Fingerprint: Equatable, Hashable { + let last: Totals + let total: Totals + } + + private struct AcceptedObservation { + let date: Date + let last: Totals + } + + private static func nonEmpty(_ value: String?) -> String? { + guard let value, !value.isEmpty else { return nil } + return value + } + + private static func date(from timestamp: String) throws -> Date { + guard let date = CostUsageScanner.dateFromTimestamp(timestamp) else { + throw LedgerError.invalidTimestamp(timestamp) + } + return date + } + + private static func add( + _ totals: Totals, + on date: Date, + timeZone: TimeZone, + to days: inout [String: Totals]) + { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = timeZone + let components = calendar.dateComponents([.year, .month, .day], from: date) + guard let year = components.year, let month = components.month, let day = components.day else { return } + let key = String(format: "%04d-%02d-%02d", year, month, day) + var dayTotals = days[key] ?? .zero + dayTotals.add(totals) + days[key] = dayTotals + } + + private struct DisjointSet { + private var parents: [String: String] = [:] + + mutating func insert(_ item: String) { + if self.parents[item] == nil { + self.parents[item] = item + } + } + + mutating func find(_ item: String) -> String { + self.insert(item) + guard let parent = self.parents[item], parent != item else { return item } + let root = self.find(parent) + self.parents[item] = root + return root + } + + mutating func union(_ first: String, _ second: String) { + let firstRoot = self.find(first) + let secondRoot = self.find(second) + if firstRoot != secondRoot { + self.parents[secondRoot] = firstRoot + } + } + } +} diff --git a/Tests/CodexBarTests/CodexLineageLedgerTests.swift b/Tests/CodexBarTests/CodexLineageLedgerTests.swift new file mode 100644 index 0000000000..434e1e7572 --- /dev/null +++ b/Tests/CodexBarTests/CodexLineageLedgerTests.swift @@ -0,0 +1,195 @@ +import Foundation +import Testing +@testable import CodexBarCore + +struct CodexLineageLedgerTests { + @Test + func `transitive lineage counts copied observations once`() throws { + let first = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) + let second = Self.observation(timestamp: "2026-07-09T12:01:00Z", input: 50, totalInput: 150) + let third = Self.observation(timestamp: "2026-07-09T12:02:00Z", input: 25, totalInput: 175) + let documents = [ + Self.document(owner: "root", observations: [first]), + Self.document(owner: "child", metadata: "root", parent: "root", observations: [first, second]), + Self.document(owner: "grandchild", metadata: "child", parent: "child", observations: [ + first, + second, + third, + ]), + ] + + let report = try CodexLineageLedger.reconcile( + documents: documents, + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + + #expect(report.utcDays["2026-07-09"]?.input == 175) + #expect(report.localDays["2026-07-09"]?.input == 175) + #expect(report.componentCount == 1) + #expect(report.acceptedObservationCount == 3) + #expect(report.duplicateObservationCount == 3) + } + + @Test + func `equal observations in disconnected lineages remain additive`() throws { + let observation = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) + let report = try CodexLineageLedger.reconcile( + documents: [ + Self.document(owner: "first", observations: [observation]), + Self.document(owner: "second", observations: [observation]), + ], + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + + #expect(report.utcDays["2026-07-09"]?.input == 200) + #expect(report.componentCount == 2) + #expect(report.acceptedObservationCount == 2) + #expect(report.duplicateObservationCount == 0) + } + + @Test + func `unchanged state reemissions within a lineage count once`() throws { + let observation = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) + let report = try CodexLineageLedger.reconcile( + documents: [Self.document(owner: "root", observations: [observation, observation, observation])], + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + + #expect(report.utcDays["2026-07-09"]?.input == 100) + #expect(report.acceptedObservationCount == 1) + #expect(report.duplicateObservationCount == 2) + } + + @Test + func `complete token state distinguishes observations within a lineage`() throws { + let first = Self.observation( + timestamp: "2026-07-09T12:00:00Z", + input: 100, + cached: 40, + output: 10, + totalInput: 100) + let changedTotal = Self.observation( + timestamp: "2026-07-09T12:01:00Z", + input: 100, + cached: 40, + output: 10, + totalInput: 200) + let changedLast = Self.observation( + timestamp: "2026-07-09T12:02:00Z", + input: 125, + cached: 50, + output: 15, + totalInput: 200) + let report = try CodexLineageLedger.reconcile( + documents: [ + Self.document(owner: "root", observations: [first, changedTotal, changedLast]), + ], + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + + #expect(report.utcDays["2026-07-09"] == .init(input: 325, cached: 130, output: 35)) + #expect(report.acceptedObservationCount == 3) + #expect(report.duplicateObservationCount == 0) + } + + @Test + func `UTC and local projections preserve their distinct day boundaries`() throws { + let observation = Self.observation(timestamp: "2026-07-10T02:00:00Z", input: 100, totalInput: 100) + let report = try CodexLineageLedger.reconcile( + documents: [Self.document(owner: "root", observations: [observation])], + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + + #expect(report.utcDays["2026-07-10"]?.input == 100) + #expect(report.localDays["2026-07-09"]?.input == 100) + } + + @Test(arguments: [ + ("archived-fork-33ce-3869", 15_309_178), + ("live-fork-4d90-52bf", 26_801_911), + ]) + func `sanitized fork fixtures collapse copied prefixes and unchanged reemissions`( + fixtureName: String, + expectedTokens: Int) throws + { + let fixture = try SanitizedForkFamilyFixture.load(named: fixtureName) + let parentMetadata = try fixture.sessionMetadata(named: "parent") + let childMetadata = try fixture.sessionMetadata(named: "child") + let parent = try fixture.events(named: "parent") + let child = try fixture.events(named: "child") + let documents = [ + CodexLineageLedger.Document( + ownerID: "parent-owner", + metadataSessionID: parentMetadata.id, + parentSessionID: parentMetadata.forkedFromID, + observations: parent.map(Self.observation)), + CodexLineageLedger.Document( + ownerID: "child-owner", + metadataSessionID: childMetadata.id, + parentSessionID: childMetadata.forkedFromID, + observations: child.map(Self.observation)), + ] + + let report = try CodexLineageLedger.reconcile( + documents: documents, + localTimeZone: #require(TimeZone(identifier: "America/New_York"))) + let total = report.utcDays.values.reduce(0) { partial, totals in + partial + totals.input + totals.output + } + + #expect(total == expectedTokens) + #expect(total < fixture.manifest.oracle.dedupedLastTokens) + } + + @Test + func `document order does not change lineage totals or attribution`() throws { + let copiedLater = Self.observation(timestamp: "2026-07-10T00:05:00Z", input: 100, totalInput: 100) + let original = Self.observation(timestamp: "2026-07-09T23:55:00Z", input: 100, totalInput: 100) + let root = Self.document(owner: "root", observations: [original]) + let child = Self.document(owner: "child", parent: "root", observations: [copiedLater]) + let timeZone = try #require(TimeZone(identifier: "America/New_York")) + + let forward = try CodexLineageLedger.reconcile(documents: [root, child], localTimeZone: timeZone) + let reversed = try CodexLineageLedger.reconcile(documents: [child, root], localTimeZone: timeZone) + + #expect(forward == reversed) + #expect(forward.utcDays["2026-07-09"]?.input == 100) + #expect(forward.utcDays["2026-07-10"] == nil) + } + + private static func document( + owner: String, + metadata: String? = nil, + parent: String? = nil, + observations: [CodexLineageLedger.Observation]) -> CodexLineageLedger.Document + { + .init( + ownerID: owner, + metadataSessionID: metadata, + parentSessionID: parent, + observations: observations) + } + + private static func observation( + timestamp: String, + input: Int, + cached: Int = 0, + output: Int = 0, + totalInput: Int) -> CodexLineageLedger.Observation + { + .init( + timestamp: timestamp, + last: .init(input: input, cached: cached, output: output), + total: .init(input: totalInput, cached: cached, output: output)) + } + + private static func observation( + _ event: SanitizedForkFamilyFixture.TokenEvent) -> CodexLineageLedger.Observation + { + .init( + timestamp: event.timestamp, + last: .init( + input: event.last.inputTokens, + cached: event.last.cachedInputTokens, + output: event.last.outputTokens), + total: .init( + input: event.total.inputTokens, + cached: event.total.cachedInputTokens, + output: event.total.outputTokens)) + } +} From 547567d5168679c0b59aad1450dbfbf4ec3e8d76 Mon Sep 17 00:00:00 2001 From: Bryan Font Date: Mon, 13 Jul 2026 12:52:24 -0400 Subject: [PATCH 2/2] Adapt rollout snapshots for lineage accounting --- .../Generated/CodexParserHash.generated.swift | 2 +- .../Vendored/CostUsage/CostUsageScanner.swift | 56 +++++++++++++++-- .../CodexLineageLedgerTests.swift | 61 +++++++++++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift b/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift index 085fda188b..2a1047a65a 100644 --- a/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift +++ b/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift @@ -1,5 +1,5 @@ // Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand. enum CodexParserHash { - static let value = "cdef6eb9658a43e2" + static let value = "865e101428b49ab7" } diff --git a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift index fef88de04f..9bda419158 100644 --- a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift +++ b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift @@ -98,6 +98,13 @@ enum CostUsageScanner { let totals: CostUsageCodexTotals } + private struct CodexParsedTokenEvidence { + let sessionId: String? + let forkedFromId: String? + let snapshots: [CodexTimestampedTotals] + let observations: [CodexLineageLedger.Observation] + } + enum CodexForkBaseline { case resolved(CostUsageCodexTotals?) case unresolved @@ -1680,13 +1687,13 @@ enum CostUsageScanner { private static func parseCodexTokenSnapshots( fileURL: URL, - checkCancellation: CancellationCheck? = nil) throws -> ( - sessionId: String?, - snapshots: [CodexTimestampedTotals]) + checkCancellation: CancellationCheck? = nil) throws -> CodexParsedTokenEvidence { var sessionId: String? + var forkedFromId: String? var accumulator = CodexSnapshotAccumulator() var snapshots: [CodexTimestampedTotals] = [] + var observations: [CodexLineageLedger.Observation] = [] var warnedAboutUnparsedTimestamp = false func parsedSnapshotDate(timestamp: String) -> Date? { @@ -1708,6 +1715,12 @@ enum CostUsageScanner { timestamp: timestamp, date: parsedSnapshotDate(timestamp: timestamp), totals: counted)) + if let last, let total { + observations.append(CodexLineageLedger.Observation( + timestamp: timestamp, + last: Self.lineageTotals(last), + total: Self.lineageTotals(total))) + } } do { @@ -1724,6 +1737,9 @@ enum CostUsageScanner { if sessionId == nil { sessionId = metadata.sessionId } + if forkedFromId == nil { + forkedFromId = metadata.forkedFromId + } case let .tokenCount(record): appendSnapshot(timestamp: record.timestamp, last: record.last, total: record.total) case .turnContext, .taskStarted: @@ -1746,6 +1762,9 @@ enum CostUsageScanner { ?? obj["sessionId"] as? String ?? obj["id"] as? String } + if forkedFromId == nil { + forkedFromId = Self.codexForkParentId(from: payload) + } return } @@ -1785,7 +1804,36 @@ enum CostUsageScanner { metadata: ["path": fileURL.path, "error": error.localizedDescription]) } - return (sessionId, snapshots) + return CodexParsedTokenEvidence( + sessionId: sessionId, + forkedFromId: forkedFromId, + snapshots: snapshots, + observations: observations) + } + + static func parseCodexLineageDocument( + fileURL: URL, + checkCancellation: CancellationCheck? = nil) throws -> CodexLineageLedger.Document + { + let parsed = try Self.parseCodexTokenSnapshots( + fileURL: fileURL, + checkCancellation: checkCancellation) + return CodexLineageLedger.Document( + ownerID: Self.codexRolloutOwnerID(fileURL: fileURL) ?? parsed.sessionId ?? fileURL.standardizedFileURL.path, + metadataSessionID: parsed.sessionId, + parentSessionID: parsed.forkedFromId, + observations: parsed.observations) + } + + private static func lineageTotals(_ totals: CostUsageCodexTotals) -> CodexLineageLedger.Totals { + CodexLineageLedger.Totals(input: totals.input, cached: totals.cached, output: totals.output) + } + + private static func codexRolloutOwnerID(fileURL: URL) -> String? { + let stem = fileURL.deletingPathExtension().lastPathComponent + guard stem.count >= 36 else { return nil } + let candidate = String(stem.suffix(36)) + return UUID(uuidString: candidate) == nil ? nil : candidate.lowercased() } static func parseCodexFile( diff --git a/Tests/CodexBarTests/CodexLineageLedgerTests.swift b/Tests/CodexBarTests/CodexLineageLedgerTests.swift index 434e1e7572..9a6c46c808 100644 --- a/Tests/CodexBarTests/CodexLineageLedgerTests.swift +++ b/Tests/CodexBarTests/CodexLineageLedgerTests.swift @@ -3,6 +3,56 @@ import Testing @testable import CodexBarCore struct CodexLineageLedgerTests { + @Test + func `fast rollout parser adapts complete token states into a ledger document`() throws { + let environment = try CostUsageTestEnvironment() + defer { environment.cleanup() } + let ownerID = "019f55a1-7f6e-70c0-8e4f-f5bbefa9b7ac" + let fileURL = environment.root.appendingPathComponent( + "rollout-2026-07-09T12-00-00-\(ownerID).jsonl") + let contents = [ + #"{"type":"session_meta","payload":{"id":"metadata-id","forked_from_id":"parent-id"}}"#, + Self.tokenCountLine( + timestamp: "2026-07-09T12:00:00Z", + last: (input: 100, cached: 40, output: 10), + total: (input: 100, cached: 40, output: 10)), + Self.tokenCountLine( + timestamp: "2026-07-09T12:01:00Z", + last: (input: 50, cached: 20, output: 5), + total: (input: 150, cached: 60, output: 15)), + #"{"type":"event_msg","timestamp":"2026-07-09T12:02:00Z","payload":{"type":"token_count","info":{"# + + #""total_token_usage":{"input_tokens":200,"cached_input_tokens":80,"output_tokens":20}}}}"#, + ].joined(separator: "\n") + try contents.write(to: fileURL, atomically: true, encoding: .utf8) + + let document = try CostUsageScanner.parseCodexLineageDocument(fileURL: fileURL) + + #expect(document.ownerID == ownerID) + #expect(document.metadataSessionID == "metadata-id") + #expect(document.parentSessionID == "parent-id") + #expect(document.observations.count == 2) + #expect(document.observations[0].last == .init(input: 100, cached: 40, output: 10)) + #expect(document.observations[1].total == .init(input: 150, cached: 60, output: 15)) + } + + @Test + func `ledger document parsing propagates cancellation`() throws { + let environment = try CostUsageTestEnvironment() + defer { environment.cleanup() } + let fileURL = environment.root.appendingPathComponent("rollout-without-owner.jsonl") + try Self.tokenCountLine( + timestamp: "2026-07-09T12:00:00Z", + last: (input: 100, cached: 0, output: 10), + total: (input: 100, cached: 0, output: 10)) + .write(to: fileURL, atomically: true, encoding: .utf8) + + #expect(throws: CancellationError.self) { + _ = try CostUsageScanner.parseCodexLineageDocument( + fileURL: fileURL, + checkCancellation: { throw CancellationError() }) + } + } + @Test func `transitive lineage counts copied observations once`() throws { let first = Self.observation(timestamp: "2026-07-09T12:00:00Z", input: 100, totalInput: 100) @@ -192,4 +242,15 @@ struct CodexLineageLedgerTests { cached: event.total.cachedInputTokens, output: event.total.outputTokens)) } + + private static func tokenCountLine( + timestamp: String, + last: (input: Int, cached: Int, output: Int), + total: (input: Int, cached: Int, output: Int)) -> String + { + #"{"type":"event_msg","timestamp":"\#(timestamp)","payload":{"type":"token_count","info":{"# + + #""last_token_usage":{"input_tokens":\#(last.input),"cached_input_tokens":\#(last.cached),"# + + #""output_tokens":\#(last.output)},"total_token_usage":{"input_tokens":\#(total.input),"# + + #""cached_input_tokens":\#(total.cached),"output_tokens":\#(total.output)}}}}"# + } }