Skip to content

Commit 030ccd4

Browse files
committed
fix(usage): restart clean pre-v12 Codex cache entries instead of resuming them
1 parent e4be285 commit 030ccd4

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎apps/server/src/usage/usageScanCache.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe("scan cache round trip", () => {
249249
},
250250
);
251251

252-
it("re-parses a v11 Codex entry that skipped records but resumes a clean one", () => {
252+
it("re-parses a v11 Codex entry that skipped records and restarts a clean one when it grows", () => {
253253
const codex = (malformedRecords: number): CachedFile => ({
254254
size: 10,
255255
mtimeMs: 100,
@@ -273,7 +273,11 @@ describe("scan cache round trip", () => {
273273
size: -1,
274274
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
275275
});
276-
expect(restored.get("/clean-codex.jsonl")).toEqual(codex(0));
276+
// Served while unchanged, but its state predates the latest snapshot time, so it cannot resume.
277+
expect(restored.get("/clean-codex.jsonl")).toEqual({
278+
...codex(0),
279+
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
280+
});
277281
});
278282

279283
it("re-parses a Claude transcript cached before advisor usage was counted", () => {

‎apps/server/src/usage/usageScanCache.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ export function decodeScanCache(
303303
// A corrupt state disqualifies the entry: resuming with it would attach
304304
// appended usage to the wrong model or replay fork-copied history.
305305
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;
306-
// Before v11 a clean scan could end past an interrupted request whose usage its state dropped,
307-
// so a grown file re-parses whole instead of resuming.
308-
const resumable = !legacy && !(entry.p === "codex" && version < 11);
306+
// Before v11 a clean scan could end past an interrupted request whose usage its state dropped.
307+
// Before v12 the state lacks the latest snapshot time that keeps a re-emitted older snapshot
308+
// from billing twice. Either way a grown file re-parses whole instead of resuming.
309+
const resumable = !legacy && !(entry.p === "codex" && version < 12);
309310

310311
const provider = entry.p;
311312
const records = decodeRecords(entry.r, provider);

0 commit comments

Comments
 (0)