From 56fffe6162150a0f68fbcbbe2afb843d71482113 Mon Sep 17 00:00:00 2001 From: BarryYangi Date: Sun, 17 May 2026 23:47:12 +0800 Subject: [PATCH] Fix Codex model attribution for large turn contexts --- .../Vendored/CostUsage/CostUsageScanner.swift | 2 +- .../CostUsageScannerPriorityTests.swift | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift index a8b75f766..fca3d4290 100644 --- a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift +++ b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift @@ -769,7 +769,7 @@ enum CostUsageScanner { } let maxLineBytes = 256 * 1024 - let prefixBytes = 32 * 1024 + let prefixBytes = maxLineBytes if startOffset == 0, let metadata = Self.parseCodexSessionMetadata(fileURL: fileURL) diff --git a/Tests/CodexBarTests/CostUsageScannerPriorityTests.swift b/Tests/CodexBarTests/CostUsageScannerPriorityTests.swift index e52a3dd47..9dd24a418 100644 --- a/Tests/CodexBarTests/CostUsageScannerPriorityTests.swift +++ b/Tests/CodexBarTests/CostUsageScannerPriorityTests.swift @@ -120,6 +120,49 @@ struct CostUsageScannerPriorityTests { #expect(report.summary?.totalCostUSD == expected) } + @Test + func `codex daily report keeps model from large turn context`() throws { + let env = try CostUsageTestEnvironment() + defer { env.cleanup() } + + let day = try env.makeLocalNoon(year: 2026, month: 5, day: 15) + let iso0 = env.isoString(for: day) + let iso1 = env.isoString(for: day.addingTimeInterval(1)) + let largeInstructions = String(repeating: "x", count: 33 * 1024) + let entries: [[String: Any]] = [ + [ + "type": "turn_context", + "timestamp": iso0, + "payload": [ + "model": "gpt-5.5", + "developer_instructions": largeInstructions, + ], + ], + ["type": "event_msg", "timestamp": iso1, "payload": ["type": "task_started", "turn_id": "subagent-turn"]], + self.tokenCount(timestamp: iso1, input: 100, cached: 20, output: 10), + ] + _ = try env.writeCodexSessionFile( + day: day, + filename: "subagent-large-turn-context.jsonl", + contents: env.jsonl(entries)) + + var options = CostUsageScanner.Options( + codexSessionsRoot: env.codexSessionsRoot, + cacheRoot: env.cacheRoot) + options.refreshMinIntervalSeconds = 0 + + let report = CostUsageScanner.loadDailyReport( + provider: .codex, + since: day, + until: day, + now: day, + options: options) + + #expect(report.data.count == 1) + #expect(report.data[0].modelsUsed == ["gpt-5.5"]) + #expect(report.data[0].modelBreakdowns?.map(\.modelName) == ["gpt-5.5"]) + } + @Test func `codex pricing skips priority surcharge for long context rows`() throws { let env = try CostUsageTestEnvironment()