Skip to content

Commit 5d3a4c9

Browse files
committed
fix(spend): apply report window to OpenCodex cache misses
1 parent 73bc2a1 commit 5d3a4c9

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

Sources/CodexBarCore/Vendored/OpenCodexUsage/OpenCodexUsageStore.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public struct OpenCodexUsageStore: Sendable {
6363
return $0.requestID < $1.requestID
6464
}
6565
self.replaceCachedEntries(deduped, identity: identity)
66-
return deduped
66+
// Keep the full lifetime log in the cache, but mirror the cache-hit query when
67+
// returning parsed entries so report misses never materialize unbounded history.
68+
guard let since else { return deduped }
69+
return deduped.filter { $0.timestamp >= since }
6770
}
6871

6972
private func readCachedEntries(identity: String, since: Date?) -> [OpenCodexUsageEntry]? {

Tests/CodexBarTests/OpenCodexUsageStoreWindowTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ struct OpenCodexUsageStoreWindowTests {
3131

3232
#expect(cached.isEmpty)
3333
}
34+
35+
@Test
36+
func `cache miss applies report window while preserving cached history`() throws {
37+
let fixture = StoreCacheFixture()
38+
try fixture.prepare()
39+
defer { fixture.cleanup() }
40+
let store = OpenCodexUsageStore(cacheRoot: fixture.cacheRoot)
41+
let old = Date(timeIntervalSince1970: 1_784_179_200)
42+
let now = old.addingTimeInterval(86400)
43+
let oldMillis = Int(old.timeIntervalSince1970 * 1000)
44+
let nowMillis = Int(now.timeIntervalSince1970 * 1000)
45+
try """
46+
{"requestId":"old","timestamp":\(oldMillis),"provider":"openai","model":"gpt-5.4",\
47+
"usageStatus":"reported"}
48+
{"requestId":"new","timestamp":\(nowMillis),"provider":"openai","model":"gpt-5.4",\
49+
"usageStatus":"reported"}
50+
""".write(to: fixture.logURL, atomically: true, encoding: .utf8)
51+
52+
let windowed = try store.loadEntries(logURL: fixture.logURL, since: now)
53+
54+
#expect(windowed.map(\.requestID) == ["new"])
55+
56+
let fullCache = try store.loadEntries(logURL: fixture.logURL)
57+
#expect(fullCache.map(\.requestID) == ["old", "new"])
58+
}
3459
}
3560

3661
private struct StoreCacheFixture {

0 commit comments

Comments
 (0)