Skip to content

Commit bffb4af

Browse files
committed
fix(server): count the first request after a Codex usage counter reset
When Codex restarts its cumulative token counters mid-rollout, the first snapshot of the new sequence equals the request itself while its total drops below the previous snapshot. The delta check subtracted the old sequence and rejected the request as malformed. Accept that exact restart as the new baseline; later deltas and inconsistent counters are validated as before. Fixes #22376
1 parent 72ebd98 commit bffb4af

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22

3+
import { totalTokens } from "@t3tools/provider-core/server/usage";
4+
35
import { codexUsageFormat, initialCodexScanState, parseCodexLine } from "./codexUsage.ts";
46

57
describe("parseCodexLine", () => {
@@ -187,6 +189,35 @@ describe("parseCodexLine", () => {
187189
}
188190
});
189191

192+
it("counts the first request of a new cumulative counter sequence once", () => {
193+
const state = initialCodexScanState();
194+
parseCodexLine(turnContext, state);
195+
const requests = [
196+
[counters(1000, 200), counters(1000, 200)],
197+
[counters(100, 20), counters(100, 20)],
198+
[counters(100, 20), counters(200, 40)],
199+
] as const;
200+
const records = requests.flatMap(([last, total]) => {
201+
const record = parseCodexLine(counted(last, total), state);
202+
return record === null ? [] : [record];
203+
});
204+
205+
expect({
206+
requests: records.length,
207+
processedTokens: records.reduce((sum, record) => sum + totalTokens(record.totals), 0),
208+
skipped: codexUsageFormat.readMalformedRecords?.(state),
209+
}).toEqual({ requests: 3, processedTokens: 1440, skipped: 0 });
210+
});
211+
212+
it("still rejects a cumulative drop that does not match the request", () => {
213+
const state = initialCodexScanState();
214+
parseCodexLine(turnContext, state);
215+
parseCodexLine(counted(counters(1000, 200), counters(1000, 200)), state);
216+
217+
expect(parseCodexLine(counted(counters(100, 20), counters(90, 20)), state)).toBeNull();
218+
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
219+
});
220+
190221
it.each(["request", "cumulative"])("skips malformed %s counters and recovers", (malformed) => {
191222
const state = initialCodexScanState();
192223
parseCodexLine(turnContext, state);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
245245

246246
const signature = JSON.stringify(last);
247247
if (cumulative !== null) {
248+
// A new counter sequence starts over, so its first snapshot is the request itself.
249+
const restarted =
250+
previous !== null &&
251+
cumulative.total_tokens < previous.total_tokens &&
252+
CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
248253
if (
254+
!restarted &&
249255
!CODEX_USAGE_FIELDS.every((key) => cumulative[key] - (previous?.[key] ?? 0) === last[key])
250256
) {
251257
state.malformedRecords += 1;

0 commit comments

Comments
 (0)