Skip to content

Commit f3bc74f

Browse files
fix(server): keep each recovered Codex request's own timestamp and bill a fork's first interrupted turn
Pending `token_usage_record`s are no longer merged by attribution, so a recovered request keeps its own timestamp and lands in the right hour and day. A `token_usage_record` past the fork copy burst now ends suppression the same way a `token_count` does, so a fork whose first genuine turn is interrupted before its first snapshot is still billed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9196450 commit f3bc74f

2 files changed

Lines changed: 93 additions & 60 deletions

File tree

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

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -359,70 +359,91 @@ describe("parseCodexLine", () => {
359359
expect(state.malformedRecords).toBe(1);
360360
});
361361

362-
it("bills each interrupted turn's request with the model and tier it was read under", () => {
362+
it("bills each interrupted turn's request with the model, tier and time it was read under", () => {
363363
const state = initialCodexScanState();
364-
const line = (type: string, second: number, payload: Record<string, unknown>) =>
365-
JSON.stringify({ type, timestamp: `2026-08-01T05:17:0${second}.000Z`, payload });
366-
const context = (second: number, turn_id: string, model: string) =>
367-
line("turn_context", second, { turn_id, model });
368-
const tier = (second: number, service_tier: string) =>
369-
line("event_msg", second, {
364+
const line = (type: string, time: string, payload: Record<string, unknown>) =>
365+
JSON.stringify({ type, timestamp: `2026-11-0${time}Z`, payload });
366+
const context = (time: string, turn_id: string, model: string) =>
367+
line("turn_context", time, { turn_id, model });
368+
const tier = (time: string, service_tier: string) =>
369+
line("event_msg", time, {
370370
type: "thread_settings_applied",
371371
thread_settings: { service_tier },
372372
});
373-
const request = (second: number, usage: unknown) =>
374-
line("token_usage_record", second, { usage });
373+
const request = (time: string, usage: unknown) => line("token_usage_record", time, { usage });
374+
const snapshot = (time: string, last: unknown, total: unknown) =>
375+
line("event_msg", time, {
376+
type: "token_count",
377+
info: { last_token_usage: last, total_token_usage: total },
378+
});
375379
const parsed = [
376-
line("session_meta", 0, { id: "session-a", model_provider: "openai" }),
377-
context(0, "turn-1", "gpt-6-sol"),
378-
request(1, counters(100, 20)),
379-
counted(counters(100, 20), counters(100, 20)),
380-
request(2, counters(150, 10)),
381-
tier(3, "priority"),
382-
context(3, "turn-2", "gpt-6-astra"),
383-
request(4, counters(50, 5)),
384-
request(5, counters(30, 5)),
385-
tier(6, "default"),
386-
context(6, "turn-3", "gpt-6-sol"),
387-
request(7, counters(200, 30)),
388-
counted(counters(200, 30), counters(530, 70)),
380+
line("session_meta", "1T23:59:50", { id: "session-a", model_provider: "openai" }),
381+
context("1T23:59:50", "turn-1", "gpt-6-sol"),
382+
request("1T23:59:51", counters(100, 20)),
383+
snapshot("1T23:59:52", counters(100, 20), counters(100, 20)),
384+
// One interrupted turn whose requests straddle midnight.
385+
request("1T23:59:59", counters(150, 10)),
386+
request("2T00:00:01", counters(50, 5)),
387+
tier("2T00:00:02", "priority"),
388+
context("2T00:00:02", "turn-2", "gpt-6-astra"),
389+
request("2T00:00:03", counters(50, 5)),
390+
request("2T00:00:04", counters(30, 5)),
391+
tier("2T00:00:05", "default"),
392+
context("2T00:00:05", "turn-3", "gpt-6-sol"),
393+
request("2T00:00:06", counters(200, 30)),
394+
snapshot("2T00:00:07", counters(200, 30), counters(580, 75)),
389395
].flatMap((value) => parseCodexRecords(value, state));
390396

397+
const at = (time: string) => Date.parse(`2026-11-0${time}Z`);
391398
expect(
392399
parsed.map((record) => ({
393400
tokens: totalTokens(record.totals),
394401
model: record.model,
395402
speed: record.speed,
396-
second: Math.floor(record.timestampMs / 1000) % 60,
403+
timestampMs: record.timestampMs,
397404
unresolvedServiceTierTurnId: record.unresolvedServiceTierTurnId,
398405
})),
399406
).toEqual([
400407
{
401408
tokens: 120,
402409
model: "gpt-6-sol",
403410
speed: "standard",
404-
second: 49,
411+
timestampMs: at("1T23:59:52"),
405412
unresolvedServiceTierTurnId: "turn-1",
406413
},
407414
{
408415
tokens: 160,
409416
model: "gpt-6-sol",
410417
speed: "standard",
411-
second: 2,
418+
timestampMs: at("1T23:59:59"),
419+
unresolvedServiceTierTurnId: "turn-1",
420+
},
421+
{
422+
tokens: 55,
423+
model: "gpt-6-sol",
424+
speed: "standard",
425+
timestampMs: at("2T00:00:01"),
412426
unresolvedServiceTierTurnId: "turn-1",
413427
},
414428
{
415-
tokens: 90,
429+
tokens: 55,
430+
model: "gpt-6-astra",
431+
speed: "fast",
432+
timestampMs: at("2T00:00:03"),
433+
unresolvedServiceTierTurnId: undefined,
434+
},
435+
{
436+
tokens: 35,
416437
model: "gpt-6-astra",
417438
speed: "fast",
418-
second: 4,
439+
timestampMs: at("2T00:00:04"),
419440
unresolvedServiceTierTurnId: undefined,
420441
},
421442
{
422443
tokens: 230,
423444
model: "gpt-6-sol",
424445
speed: "standard",
425-
second: 49,
446+
timestampMs: at("2T00:00:07"),
426447
unresolvedServiceTierTurnId: undefined,
427448
},
428449
]);
@@ -767,5 +788,26 @@ describe("parseCodexLine", () => {
767788
skipped: codexUsageFormat.readMalformedRecords?.(state),
768789
}).toEqual({ processed: [120, 0, 120, 35], skipped: 0 });
769790
});
791+
792+
it("bills the child's first request when its turn is interrupted before any snapshot", () => {
793+
const state = initialCodexScanState();
794+
const forkInstant = "2026-08-01T05:00:00.000Z";
795+
const request = (timestamp: string, usage: unknown) =>
796+
JSON.stringify({ type: "token_usage_record", timestamp, payload: { usage } });
797+
const billed = [
798+
meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }),
799+
stamped(forkInstant, turnContext),
800+
request("2026-08-01T05:00:00.001Z", counters(100, 20)),
801+
stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 20), counters(100, 20))),
802+
request("2026-08-01T05:00:06.000Z", counters(150, 10)),
803+
request("2026-08-01T05:00:10.000Z", counters(200, 30)),
804+
stamped("2026-08-01T05:00:10.000Z", counted(counters(200, 30), counters(450, 60))),
805+
].flatMap((line) => parseCodexRecords(line, state));
806+
807+
expect({
808+
processed: billed.map((record) => totalTokens(record.totals)),
809+
skipped: codexUsageFormat.readMalformedRecords?.(state),
810+
}).toEqual({ processed: [160, 230], skipped: 0 });
811+
});
770812
});
771813
});

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

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ const CodexUnreportedRequest = Schema.Struct({
8181
type CodexUnreportedRequest = typeof CodexUnreportedRequest.Type;
8282
type CodexRequestAttribution = Omit<CodexUnreportedRequest, "usage">;
8383

84-
const sameAttribution = (a: CodexRequestAttribution, b: CodexRequestAttribution) =>
85-
a.model === b.model &&
86-
a.turnId === b.turnId &&
87-
a.speed === b.speed &&
88-
a.sawThreadSettings === b.sawThreadSettings;
89-
9084
// An interrupted turn can end before a snapshot reports its last request. Bill those requests only
9185
// when the `token_usage_record`s since the previous snapshot explain the whole gap; `last` is the
9286
// final one.
@@ -129,8 +123,7 @@ export const CodexScanState = Schema.Struct({
129123
lastCumulativeUsage: Schema.mutableKey(Schema.NullOr(CodexTokenUsage)),
130124
// Optional so scan states cached before this field existed stay valid.
131125
sequenceStartSignature: Schema.mutableKey(Schema.optionalKey(Schema.String)),
132-
// `token_usage_record` usage since the last cumulative snapshot, one entry per run of records
133-
// with the same attribution, timestamped by its first record.
126+
// `token_usage_record` usage since the last cumulative snapshot, one entry per record.
134127
unreportedRequests: Schema.mutableKey(
135128
Schema.optionalKey(Schema.mutable(Schema.Array(CodexUnreportedRequest))),
136129
),
@@ -175,6 +168,18 @@ function isForkedSessionMeta(payload: Record<string, unknown>): boolean {
175168
return typeof (spawn as Record<string, unknown>)["parent_thread_id"] === "string";
176169
}
177170

171+
// The first usage line past the copied burst ends suppression for the rest of the rollout.
172+
function isForkCopy(state: CodexScanState, timestampMs: number): boolean {
173+
if (!state.suppressingForkCopies) return false;
174+
if (timestampMs - state.forkCopyAnchorMs < FORK_COPY_MAX_GAP_MS) {
175+
state.forkCopyAnchorMs = timestampMs;
176+
return true;
177+
}
178+
state.suppressingForkCopies = false;
179+
state.lastUsageSignature = null;
180+
return false;
181+
}
182+
178183
/**
179184
* Cumulative counters validate request deltas. Older events without cumulative counters use
180185
* normalized consecutive-request deduplication.
@@ -233,17 +238,8 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
233238
const usage = readCodexTokenUsage(payloadRecord["usage"]);
234239
if (timestampMs === null || usage === null || state.model.length === 0) return [];
235240
// Copied history is already billed in the parent.
236-
if (state.suppressingForkCopies) return [];
237-
const attribution = codexAttribution(state, timestampMs);
238-
const pending = (state.unreportedRequests ??= []);
239-
const run = pending.at(-1);
240-
if (run === undefined || !sameAttribution(run, attribution)) {
241-
pending.push({ ...attribution, usage });
242-
return [];
243-
}
244-
const sum = advanceCodexUsage(run.usage, usage);
245-
if (sum === null) delete state.unreportedRequests;
246-
else pending[pending.length - 1] = { ...run, usage: sum };
241+
if (isForkCopy(state, timestampMs)) return [];
242+
(state.unreportedRequests ??= []).push({ ...codexAttribution(state, timestampMs), usage });
247243
return [];
248244
}
249245

@@ -271,20 +267,15 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
271267
const hasCumulative = rawCumulative !== undefined && rawCumulative !== null;
272268
const cumulative = hasCumulative ? readCodexTokenUsage(rawCumulative) : null;
273269

274-
if (state.suppressingForkCopies) {
275-
if (timestampMs - state.forkCopyAnchorMs < FORK_COPY_MAX_GAP_MS) {
276-
state.forkCopyAnchorMs = timestampMs;
277-
// Copied history is already billed in the parent, but establishes the child's baseline.
278-
const signature = last === null ? null : JSON.stringify(last);
279-
if (cumulative !== null) rebaseCodexUsage(state, cumulative);
280-
else if (!hasCumulative && last !== null && signature !== state.lastUsageSignature) {
281-
state.lastCumulativeUsage = advanceCodexUsage(state.lastCumulativeUsage, last);
282-
}
283-
state.lastUsageSignature = signature;
284-
return [];
270+
if (isForkCopy(state, timestampMs)) {
271+
// Copied history is already billed in the parent, but establishes the child's baseline.
272+
const signature = last === null ? null : JSON.stringify(last);
273+
if (cumulative !== null) rebaseCodexUsage(state, cumulative);
274+
else if (!hasCumulative && last !== null && signature !== state.lastUsageSignature) {
275+
state.lastCumulativeUsage = advanceCodexUsage(state.lastCumulativeUsage, last);
285276
}
286-
state.suppressingForkCopies = false;
287-
state.lastUsageSignature = null;
277+
state.lastUsageSignature = signature;
278+
return [];
288279
}
289280

290281
const previous = state.lastCumulativeUsage;

0 commit comments

Comments
 (0)