Skip to content

Commit ee93d23

Browse files
committed
fix(server): ignore replayed Codex usage snapshots after a counter reset
A re-emitted first request of the current counter sequence has cumulative counters equal to its own request and a total below the live baseline, so it passed as a restart: the request was billed again and the baseline moved back, which then rejected the next genuine delta. Remember where the current sequence began and treat a restart candidate equal to it as a stale repeat, leaving the baseline untouched and not counting it as malformed.
1 parent bffb4af commit ee93d23

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ describe("parseCodexLine", () => {
218218
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
219219
});
220220

221+
it("ignores a replay of the current sequence's first request without moving the baseline back", () => {
222+
const state = initialCodexScanState();
223+
parseCodexLine(turnContext, state);
224+
const request = counters(100, 20);
225+
const processed = [
226+
counted(request, request),
227+
counted(request, counters(200, 40)),
228+
counted(request, request),
229+
counted(request, counters(300, 60)),
230+
].map((line) => {
231+
const record = parseCodexLine(line, state);
232+
return record === null ? 0 : totalTokens(record.totals);
233+
});
234+
235+
expect({
236+
processed,
237+
skipped: codexUsageFormat.readMalformedRecords?.(state),
238+
}).toEqual({ processed: [120, 120, 0, 120], skipped: 0 });
239+
});
240+
221241
it.each(["request", "cumulative"])("skips malformed %s counters and recovers", (malformed) => {
222242
const state = initialCodexScanState();
223243
parseCodexLine(turnContext, state);

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export const CodexScanState = Schema.Struct({
7171
sessionId: Schema.mutableKey(Schema.String),
7272
lastUsageSignature: Schema.mutableKey(Schema.NullOr(Schema.String)),
7373
lastCumulativeUsage: Schema.mutableKey(Schema.NullOr(CodexTokenUsage)),
74+
// Optional so scan states cached before this field existed stay valid.
75+
sequenceStartUsage: Schema.mutableKey(Schema.optionalKey(CodexTokenUsage)),
7476
malformedRecords: Schema.mutableKey(TokenCount),
7577
sawSessionMeta: Schema.mutableKey(Schema.Boolean),
7678
modelProvider: Schema.mutableKey(Schema.String),
@@ -246,17 +248,27 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
246248
const signature = JSON.stringify(last);
247249
if (cumulative !== null) {
248250
// A new counter sequence starts over, so its first snapshot is the request itself.
251+
const startsSequence = CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
249252
const restarted =
250-
previous !== null &&
251-
cumulative.total_tokens < previous.total_tokens &&
252-
CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
253+
startsSequence && previous !== null && cumulative.total_tokens < previous.total_tokens;
254+
const sequenceStart = state.sequenceStartUsage;
255+
// Re-emitting the current sequence's first request looks like a restart, but is already billed.
256+
if (
257+
restarted &&
258+
sequenceStart !== undefined &&
259+
CODEX_USAGE_FIELDS.every((key) => cumulative[key] === sequenceStart[key])
260+
) {
261+
state.lastCumulativeUsage = previous;
262+
return null;
263+
}
253264
if (
254265
!restarted &&
255266
!CODEX_USAGE_FIELDS.every((key) => cumulative[key] - (previous?.[key] ?? 0) === last[key])
256267
) {
257268
state.malformedRecords += 1;
258269
return null;
259270
}
271+
if (startsSequence) state.sequenceStartUsage = cumulative;
260272
} else {
261273
if (signature === state.lastUsageSignature) return null;
262274
// Advance expected totals for billed legacy requests so a cumulative re-emission is stale.

0 commit comments

Comments
 (0)