Skip to content

Commit 16fb914

Browse files
rihanawscursoragent
andcommitted
fix(usage): an ignored Codex restart replay restores its baseline timestamp
The restart-replay branch restored the cumulative counters but kept the timestamp the replay had just installed. For a warm v14 cache with no baseline timestamp, replaying the first snapshot gave the newer counters an older time, so replaying the second snapshot passed the rewind guard and billed 24 tokens again. The replay now restores the timestamp too, removing the field when the baseline had none. An accepted counter rewind also discarded pending anonymous requests without a warning. Requests the rewound snapshot does not report now count as malformed records and stay unbilled. Co-authored-by: Cursor <[email protected]>
1 parent 3bce42c commit 16fb914

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,50 @@ describe("parseCodexLine", () => {
745745
parseCodexLine(turnContext, state);
746746
const at = (line: string, time: string) =>
747747
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);
748+
const billed = (line: string) => {
749+
const record = parseCodexLine(line, state);
750+
return record === null ? 0 : totalTokens(record.totals);
751+
};
752+
const start = at(counted(counters(100, 20), counters(100, 20)), "10:00:00");
753+
const second = at(counted(counters(20, 4), counters(120, 24)), "10:00:05");
754+
const third = at(counted(counters(30, 6), counters(150, 30)), "10:00:10");
755+
expect([start, second, third].map(billed)).toEqual([120, 24, 36]);
752756
// Scan states cached before v15 resume without the timestamp.
753757
delete state.lastCumulativeTimestampMs;
754-
expect(parseCodexLine(replay, state)).toBeNull();
758+
759+
const replayedStart = billed(start);
760+
const hasTimestampAfterStart = "lastCumulativeTimestampMs" in state;
761+
const replayedSecond = billed(second);
762+
expect({ replayed: [replayedStart, replayedSecond], hasTimestampAfterStart }).toEqual({
763+
replayed: [0, 0],
764+
hasTimestampAfterStart: false,
765+
});
766+
});
767+
768+
it("warns about pending anonymous requests a counter rewind discards", () => {
769+
const state = initialCodexScanState();
770+
parseCodexLine(turnContext, state);
771+
const at = (line: string, time: string) =>
772+
line.replace("2026-08-01T05:17:49.919Z", `2026-08-01T${time}.000Z`);
773+
const request = (time: string, usage: unknown) =>
774+
JSON.stringify({
775+
type: "token_usage_record",
776+
timestamp: `2026-08-01T${time}.000Z`,
777+
payload: { usage },
778+
});
779+
const billed = [
780+
at(counted(counters(100, 20), counters(100, 20)), "10:00:00"),
781+
request("10:00:05", counters(40, 8)),
782+
request("10:00:06", counters(20, 4)),
783+
// Rewinds below the 120-token baseline and reports only the 10:00:06 request.
784+
at(counted(counters(20, 4), counters(60, 12)), "10:00:07"),
785+
at(counted(counters(10, 2), counters(70, 14)), "10:00:08"),
786+
].flatMap((line) => parseCodexRecords(line, state));
787+
788+
expect({
789+
processed: billed.map((record) => totalTokens(record.totals)),
790+
skipped: codexUsageFormat.readMalformedRecords?.(state),
791+
}).toEqual({ processed: [120, 24, 12], skipped: 1 });
755792
});
756793

757794
it.each([

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ function unreportedRequests(
140140
);
141141
}
142142

143+
// A rewound snapshot accounts only for the request it reports. Earlier pending requests cannot be
144+
// reconciled against the rewound counters, so they stay unbilled.
145+
function discardedRequestCount(
146+
pending: readonly CodexUnreportedRequest[] | undefined,
147+
last: CodexTokenUsage,
148+
) {
149+
const final = pending?.at(-1);
150+
if (pending === undefined || final === undefined) return 0;
151+
const reported = CODEX_USAGE_FIELDS.every((key) => final.usage[key] === last[key]);
152+
return pending.length - (reported ? 1 : 0);
153+
}
154+
143155
/**
144156
* Codex `token_count` events carry no model or service tier, so both are carried forward: the model
145157
* from the most recent `turn_context`, the tier from the most recent `thread_settings_applied`.
@@ -373,6 +385,9 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
373385
// Only the same event is a replay; a later request can restart with identical counters.
374386
if (restarted && sequenceStart === state.sequenceStartSignature) {
375387
state.lastCumulativeUsage = previous;
388+
// The replay's older timestamp would let a later stale snapshot pass as a rewind.
389+
if (previousTimestampMs === undefined) delete state.lastCumulativeTimestampMs;
390+
else state.lastCumulativeTimestampMs = previousTimestampMs;
376391
return [];
377392
}
378393
// Forks and paginated continuations can inherit totals without copying a counter snapshot.
@@ -384,7 +399,9 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
384399
) !== null
385400
: CODEX_USAGE_FIELDS.every((key) => cumulative[key] - previous[key] === last[key]);
386401
if (!restarted && !reconciled) {
387-
if (!isRewoundBaseline(previous, previousTimestampMs, cumulative, last, timestampMs)) {
402+
if (isRewoundBaseline(previous, previousTimestampMs, cumulative, last, timestampMs)) {
403+
state.malformedRecords += discardedRequestCount(pending, last);
404+
} else {
388405
const bridged = unreportedRequests(previous, cumulative, last, pending);
389406
if (bridged === null) {
390407
state.malformedRecords += 1;

0 commit comments

Comments
 (0)