Skip to content

Commit 5366382

Browse files
committed
fix(usage): a Codex context marker that zeroes the counters forgets the old sequence start
A post-compaction marker with zeroed cumulative counters rebases the baseline to zero, but the remembered first request of the sequence survived it. The first genuine request after the marker can have the same counters as that old start, so the replay guard dropped it and kept the zero baseline, and the next request was rejected as malformed. Clear the remembered start when the marker zeroes the cumulative counters and keep it for markers that leave them alone.
1 parent 190760e commit 5366382

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,46 @@ describe("parseCodexLine", () => {
277277
}).toEqual({ processed: [120, 120, 0, 120], skipped: 0 });
278278
});
279279

280+
it.each([
281+
{
282+
reset: true,
283+
markerTotal: counters(0, 0),
284+
nextTotal: counters(130, 25),
285+
processed: [120, 120, 0, 120, 35],
286+
},
287+
{
288+
reset: false,
289+
markerTotal: counters(200, 40),
290+
nextTotal: counters(230, 45),
291+
processed: [120, 120, 0, 0, 35],
292+
},
293+
])(
294+
"bills a request equal to the old sequence start only after a context marker reset the counters (reset: $reset)",
295+
({ markerTotal, nextTotal, processed }) => {
296+
const state = initialCodexScanState();
297+
parseCodexLine(turnContext, state);
298+
const request = counters(100, 20);
299+
const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, markerTotal, {
300+
model_context_window: 475000,
301+
});
302+
const billed = [
303+
counted(request, request),
304+
counted(request, counters(200, 40)),
305+
marker,
306+
counted(request, request),
307+
counted(counters(30, 5), nextTotal),
308+
].map((line) => {
309+
const record = parseCodexLine(line, state);
310+
return record === null ? 0 : totalTokens(record.totals);
311+
});
312+
313+
expect({
314+
processed: billed,
315+
skipped: codexUsageFormat.readMalformedRecords?.(state),
316+
}).toEqual({ processed, skipped: 0 });
317+
},
318+
);
319+
280320
it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => {
281321
const meta = (id: string, model_provider?: string) =>
282322
JSON.stringify({ type: "session_meta", payload: { id, model_provider } });

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
225225
// post-compaction marker whose total is the new context size (its cumulative may reset to zero).
226226
if (last.input_tokens + last.output_tokens === 0) {
227227
state.lastUsageSignature = null;
228+
// Zeroed counters rebase the sequence, so its remembered start can no longer be replayed.
229+
if (cumulative !== null && cumulative.input_tokens + cumulative.output_tokens === 0) {
230+
delete state.sequenceStartUsage;
231+
}
228232
return null;
229233
}
230234

0 commit comments

Comments
 (0)