Skip to content

Commit 3bce42c

Browse files
fix(usage): a Codex baseline of unknown age never accepts a counter rewind
Scan states cached before v15 resume without lastCumulativeTimestampMs, and the rewind guard treated that as in order, so a stale snapshot appended after upgrade billed an already counted request. Require a known baseline timestamp; the next snapshot records one, so caches stay warm. AI-assisted; verified with the tests and tools listed above.
1 parent 96ad405 commit 3bce42c

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎apps/server/src/provider/Drivers/codexUsage.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,20 @@ describe("parseCodexLine", () => {
740740
).toEqual(processed);
741741
});
742742

743+
it("does not treat a rewind as ordered when a cached state has no baseline timestamp", () => {
744+
const state = initialCodexScanState();
745+
parseCodexLine(turnContext, state);
746+
const at = (line: string, time: string) =>
747+
line.replace("2026-08-01T05:17:49.919Z", `2026-08-01T${time}.000Z`);
748+
const replay = at(counted(counters(20, 4), counters(120, 24)), "10:00:05");
749+
parseCodexLine(at(counted(counters(100, 20), counters(100, 20)), "10:00:00"), state);
750+
parseCodexLine(replay, state);
751+
parseCodexLine(at(counted(counters(30, 6), counters(150, 30)), "10:00:10"), state);
752+
// Scan states cached before v15 resume without the timestamp.
753+
delete state.lastCumulativeTimestampMs;
754+
expect(parseCodexLine(replay, state)).toBeNull();
755+
});
756+
743757
it.each([
744758
{
745759
reset: true,

‎apps/server/src/provider/Drivers/codexUsage.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ function advanceCodexUsage(previous: CodexTokenUsage | null, last: CodexTokenUsa
7777
}
7878

7979
/** A rewind is a new baseline when the timestamp held and the snapshot still contains the request it
80-
* reports; stale and out-of-order records fail that test. A delayed replay of an older snapshot with
81-
* a fresh timestamp is indistinguishable from a rewind in the fields Codex writes. */
80+
* reports; stale and out-of-order records fail that test, as does a baseline of unknown age (scan
81+
* states cached before v15). A delayed replay of an older snapshot with a fresh timestamp is
82+
* indistinguishable from a rewind in the fields Codex writes. */
8283
function isRewoundBaseline(
8384
previous: CodexTokenUsage | null,
8485
previousTimestampMs: number | undefined,
@@ -88,7 +89,8 @@ function isRewoundBaseline(
8889
) {
8990
return (
9091
previous !== null &&
91-
(previousTimestampMs === undefined || timestampMs >= previousTimestampMs) &&
92+
previousTimestampMs !== undefined &&
93+
timestampMs >= previousTimestampMs &&
9294
cumulative.total_tokens < previous.total_tokens &&
9395
CODEX_USAGE_FIELDS.every((key) => cumulative[key] >= last[key])
9496
);

0 commit comments

Comments
 (0)