Skip to content

Commit b428418

Browse files
authored
fix(usage): count Codex request ledgers and replace archived cache entries (#35497)
1 parent fc2b5de commit b428418

7 files changed

Lines changed: 159 additions & 21 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,61 @@ describe("parseCodexLine", () => {
450450
expect(state.malformedRecords).toBe(0);
451451
});
452452

453+
it("bills request-ledger usage through resumed context counters and compaction", () => {
454+
const state = initialCodexScanState();
455+
const request = (response_id: string, usage: unknown) =>
456+
JSON.stringify({
457+
type: "token_usage_record",
458+
timestamp: "2026-08-01T05:17:45.000Z",
459+
payload: { response_id, usage },
460+
});
461+
parseCodexLine(sessionMeta, state);
462+
parseCodexLine(turnContext, state);
463+
parseCodexLine(counted(counters(100, 20), counters(1000, 200)), state);
464+
// Resume restores older context counters, while the request ledger continues.
465+
expect(
466+
parseCodexLine(request("response-1", counters(80, 10)), state)?.totals.outputTokens,
467+
).toBe(10);
468+
expect(parseCodexLine(counted(counters(80, 10), counters(580, 110)), state)).toBeNull();
469+
// Rate-limit refreshes repeat the snapshot without another request.
470+
expect(parseCodexLine(counted(counters(80, 10), counters(580, 110)), state)).toBeNull();
471+
expect(
472+
parseCodexLine(request("compaction", counters(90, 15)), state)?.totals.outputTokens,
473+
).toBe(15);
474+
parseCodexLine(turnContext, state);
475+
expect(
476+
parseCodexLine(counted({ ...counters(0, 0), total_tokens: 40 }, counters(580, 110)), state),
477+
).toBeNull();
478+
expect(parseCodexLine(request("response-2", counters(30, 5)), state)?.totals.outputTokens).toBe(
479+
5,
480+
);
481+
expect(parseCodexLine(counted(counters(30, 5), counters(610, 115)), state)).toBeNull();
482+
expect(state.malformedRecords).toBe(0);
483+
// A later turn from an older CLI can still report usage through snapshots alone.
484+
parseCodexLine(turnContext, state);
485+
expect(
486+
parseCodexLine(counted(counters(20, 2), counters(630, 117)), state)?.totals.outputTokens,
487+
).toBe(2);
488+
expect(
489+
parseCodexLine(request("interrupted", counters(40, 4)), state)?.totals.outputTokens,
490+
).toBe(4);
491+
parseCodexLine(turnContext, state);
492+
expect(
493+
parseCodexLine(counted(counters(10, 1), counters(680, 122)), state)?.totals.outputTokens,
494+
).toBe(1);
495+
expect(parseCodexLine(request("", counters(15, 3)), state)).toBeNull();
496+
parseCodexLine(turnContext, state);
497+
const transition = parseCodexRecords(request("upgraded", counters(25, 5)), state);
498+
expect(transition.map((record) => record.totals.outputTokens)).toEqual([3, 5]);
499+
expect(parseCodexLine(counted(counters(25, 5), counters(720, 130)), state)).toBeNull();
500+
expect(parseCodexLine(request("", counters(10, 1)), state)).toBeNull();
501+
expect(
502+
parseCodexLine(counted(counters(10, 1), counters(730, 131)), state)?.totals.outputTokens,
503+
).toBe(1);
504+
expect(parseCodexLine(counted(counters(10, 1), counters(730, 131)), state)).toBeNull();
505+
expect(state.malformedRecords).toBe(0);
506+
});
507+
453508
it("does not bill a context-window estimate or lose the following real request", () => {
454509
const state = initialCodexScanState();
455510
parseCodexLine(turnContext, state);

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export const CodexScanState = Schema.Struct({
127127
unreportedRequests: Schema.mutableKey(
128128
Schema.optionalKey(Schema.mutable(Schema.Array(CodexUnreportedRequest))),
129129
),
130+
requestLedgerActive: Schema.mutableKey(Schema.optionalKey(Schema.Boolean)),
130131
malformedRecords: Schema.mutableKey(TokenCount),
131132
sawSessionMeta: Schema.mutableKey(Schema.Boolean),
132133
modelProvider: Schema.mutableKey(Schema.String),
@@ -181,8 +182,8 @@ function isForkCopy(state: CodexScanState, timestampMs: number): boolean {
181182
}
182183

183184
/**
184-
* Cumulative counters validate request deltas. Older events without cumulative counters use
185-
* normalized consecutive-request deduplication.
185+
* Identified request-ledger entries report spending independently of context snapshots.
186+
* Legacy snapshots validate request deltas against cumulative counters.
186187
*/
187188
export function parseCodexLine(line: string, state: CodexScanState): readonly UsageRecord[] {
188189
let parsed: unknown;
@@ -221,6 +222,7 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
221222
}
222223

223224
if (record["type"] === "turn_context") {
225+
state.requestLedgerActive = false;
224226
if (typeof payloadRecord["model"] === "string") state.model = payloadRecord["model"];
225227
// Model carry is useful for usage, but history recovery needs this context's own identity.
226228
state.turnId =
@@ -232,13 +234,31 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
232234
return [];
233235
}
234236

235-
// Several of these may sit between two snapshots, so they only explain a gap in the counters.
237+
// Request ledgers include spending that context snapshots can omit.
236238
if (record["type"] === "token_usage_record") {
237239
const timestampMs = parseTimestampMs(record["timestamp"]);
238240
const usage = readCodexTokenUsage(payloadRecord["usage"]);
239241
if (timestampMs === null || usage === null || state.model.length === 0) return [];
240242
// Copied history is already billed in the parent.
241243
if (isForkCopy(state, timestampMs)) return [];
244+
const responseId = payloadRecord["response_id"];
245+
if (typeof responseId === "string" && responseId.length > 0) {
246+
const pending = state.unreportedRequests ?? [];
247+
state.requestLedgerActive = true;
248+
for (const request of pending) {
249+
state.lastCumulativeUsage = advanceCodexUsage(state.lastCumulativeUsage, request.usage);
250+
}
251+
state.lastCumulativeUsage = advanceCodexUsage(state.lastCumulativeUsage, usage);
252+
delete state.unreportedRequests;
253+
return [
254+
...pending.map((request) => codexRequestRecord(state, request.usage, request)),
255+
{
256+
...codexRequestRecord(state, usage, codexAttribution(state, timestampMs)),
257+
dedupeKey: JSON.stringify(["codex", state.sessionId, responseId]),
258+
},
259+
];
260+
}
261+
state.requestLedgerActive = false;
242262
(state.unreportedRequests ??= []).push({ ...codexAttribution(state, timestampMs), usage });
243263
return [];
244264
}
@@ -278,6 +298,12 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): readonly Usag
278298
return [];
279299
}
280300

301+
// Context snapshots can rewind on resume and omit compaction spending; identified requests are authoritative.
302+
if (state.requestLedgerActive) {
303+
if (cumulative !== null) rebaseCodexUsage(state, cumulative);
304+
return [];
305+
}
306+
281307
const previous = state.lastCumulativeUsage;
282308
const pending = state.unreportedRequests;
283309
// A valid snapshot remains the next baseline even when its paired request is malformed.
@@ -418,6 +444,7 @@ export const codexUsageFormat: TranscriptUsageFormat<CodexScanState> = {
418444
session_id: true,
419445
model_provider: true,
420446
turn_id: true,
447+
response_id: true,
421448
model: true,
422449
thread_settings: { service_tier: true },
423450
forked_from_id: true,

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,55 @@ describe("UsageService", () => {
577577
assert.strictEqual(totalOutputTokens(rewritten), 71);
578578
assert.deepStrictEqual((yield* service.readSummary(WINDOW)).buckets, rewritten.buckets);
579579
yield* service.awaitPersisted;
580+
const { stateDir } = yield* ServerConfig.ServerConfig;
581+
const cachePath = NodePath.join(stateDir, SCAN_CACHE_FILE_NAME);
582+
const originalPath = (yield* Effect.promise(() => NodeFSP.realpath(compressedPath))).slice(
583+
0,
584+
-4,
585+
);
586+
yield* Effect.promise(async () => {
587+
const document = decodeUnknownJsonString(await NodeFSP.readFile(cachePath, "utf8")) as {
588+
files: Record<
589+
string,
590+
{ cs: Record<string, unknown>; r: unknown[]; t: unknown[]; mr: number }
591+
>;
592+
};
593+
const archived = document.files[await NodeFSP.realpath(compressedPath)];
594+
assert.ok(archived);
595+
document.files[originalPath] = {
596+
...archived,
597+
r: [],
598+
t: [],
599+
mr: 1,
600+
cs: { ...archived.cs, malformedRecords: 1 },
601+
};
602+
await NodeFSP.writeFile(cachePath, encodeUnknownJsonString(document));
603+
});
580604
const restarted = yield* UsageService.make;
581-
assert.deepStrictEqual((yield* restarted.readSummary(WINDOW)).buckets, rewritten.buckets);
605+
const restored = yield* restarted.readSummary(WINDOW);
606+
assert.deepStrictEqual(restored.buckets, rewritten.buckets);
607+
assert.strictEqual(
608+
restored.sources.find((source) => source.fingerprint.provider === "codex")
609+
?.malformedRecords,
610+
0,
611+
);
582612
yield* restarted.awaitPersisted;
613+
yield* Effect.promise(async () => {
614+
const document = decodeUnknownJsonString(await NodeFSP.readFile(cachePath, "utf8")) as {
615+
files: Record<string, unknown>;
616+
};
617+
assert.strictEqual(document.files[originalPath], undefined);
618+
await NodeFSP.rm(compressedPath);
619+
});
620+
const removed = yield* UsageService.make;
621+
const retained = yield* removed.readSummary(WINDOW);
622+
assert.deepStrictEqual(retained.buckets, rewritten.buckets);
623+
assert.strictEqual(
624+
retained.sources.find((source) => source.fingerprint.provider === "codex")
625+
?.malformedRecords,
626+
0,
627+
);
628+
yield* removed.awaitPersisted;
583629
}).pipe(
584630
Effect.scoped,
585631
Effect.provide(layerService({ prefix: "usage-compressed-codex", home, settings })),
@@ -2789,7 +2835,7 @@ describe("UsageService", () => {
27892835
}).pipe(Effect.scoped),
27902836
);
27912837

2792-
it.live.each([4, 5, 6, 7, 8, 10])(
2838+
it.live.each([4, 5, 6, 7, 8, 10, 11, 12, 13])(
27932839
"upgrades a v%s cache: reparses live Codex usage, keeps deleted rollouts, removes the old file",
27942840
(version) =>
27952841
Effect.gen(function* () {
@@ -2890,7 +2936,7 @@ describe("UsageService", () => {
28902936
60 + 20 * (version === 4 ? 1 : 2),
28912937
);
28922938
yield* upgraded.awaitPersisted;
2893-
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v13.json");
2939+
assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v14.json");
28942940
assert.deepStrictEqual(
28952941
(yield* Effect.promise(() => NodeFSP.readdir(stateDir)))
28962942
.filter((name) => name.startsWith("usage-scan-cache"))

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,13 @@ export const make = Effect.gen(function* () {
638638
}
639639
return { path, records, malformedRecords };
640640
});
641+
const livePaths = new Set(files.map((file) => file.path));
642+
for (const file of read) {
643+
if (file.failed || !file.path.endsWith(".jsonl.zst")) continue;
644+
const originalPath = file.path.slice(0, -4);
645+
// A successfully read archive replaces the original's saved records and diagnostics.
646+
if (!livePaths.has(originalPath) && fileCache.delete(originalPath)) cacheDirty = true;
647+
}
641648
const unread = failedPaths + read.filter((file) => file.failed).length;
642649
yield* Effect.annotateCurrentSpan({
643650
"usage.files": files.length,

‎apps/server/src/usage/usageScanCache.test.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe("scan cache round trip", () => {
168168
expect(restored).toEqual(original);
169169
});
170170

171-
it.each([5, 6, 7, 8, 10])(
171+
it.each([5, 6, 7, 8, 10, 11, 12, 13])(
172172
"retains v%s history and compatible positions but invalidates obsolete state",
173173
(version) => {
174174
const original = cacheWith([["/gone.jsonl", 100, [record({ speed: "fast" })]]]);
@@ -225,19 +225,23 @@ describe("scan cache round trip", () => {
225225
};
226226

227227
const restored = decodeScanCache(JSON.parse(JSON.stringify(legacy)), TEST_FORMAT_MAP);
228-
expect(restored.get("/gone.jsonl")).toEqual({
229-
...original.get("/gone.jsonl"),
230-
size: -1,
231-
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
232-
});
228+
expect(restored.get("/gone.jsonl")).toEqual(
229+
version >= 13
230+
? original.get("/gone.jsonl")
231+
: {
232+
...original.get("/gone.jsonl"),
233+
size: -1,
234+
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
235+
},
236+
);
233237
expect(restored.get("/gone-codex.jsonl")).toEqual({
234238
...original.get("/gone-codex.jsonl"),
235239
size: -1,
236240
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
237241
});
238242
expect(restored.get("/clean-codex.jsonl")).toEqual({
239243
...cleanCodex,
240-
size: version >= 8 ? cleanCodex.size : -1,
244+
size: -1,
241245
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
242246
});
243247
expect(restored.get("/gone-pi.jsonl")).toEqual(

‎apps/server/src/usage/usageScanCache.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
1010
import { mergeUsageCopies } from "./usageAggregation.ts";
1111
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
1212

13-
const USAGE_SCAN_CACHE_VERSION = 13 as const;
13+
const USAGE_SCAN_CACHE_VERSION = 14 as const;
1414
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
1515

1616
/**
1717
* An older server sharing that directory cannot read a newer cache and would
1818
* replace it, dropping saved usage for deleted transcripts. Separate files keep
1919
* the newer one; older files are removed once it is saved.
2020
*/
21-
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v13.json";
21+
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v14.json";
2222
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
23+
"usage-scan-cache-v13.json",
2324
"usage-scan-cache-v12.json",
2425
"usage-scan-cache-v11.json",
2526
"usage-scan-cache-v10.json",
@@ -296,14 +297,12 @@ export function decodeScanCache(
296297
continue;
297298
}
298299
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
299-
// v9 and v11 repair skipped Codex requests; clean v8+ entries stay served while unchanged.
300+
// v14 uses Codex request ledgers, including compaction spending omitted by context snapshots.
300301
// v10 adds Claude advisor calls; before v12 a repeated Claude key kept its first, possibly partial, copy.
301302
// v13 adds Claude 1-hour cache writes.
302303
const legacy =
303304
(entry.p === "claude" && version < 13) ||
304-
(format.state !== undefined &&
305-
(version < 6 ||
306-
(entry.p === "codex" && (version < 8 || (version < 11 && malformedRecords > 0)))));
305+
(format.state !== undefined && (version < 6 || (entry.p === "codex" && version < 14)));
307306
// A corrupt state disqualifies the entry: resuming with it would attach
308307
// appended usage to the wrong model or replay fork-copied history.
309308
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,13 +1555,13 @@ describe("VcsStatusBroadcaster", () => {
15551555
yield* Effect.gen(function* () {
15561556
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
15571557
const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
1558-
assert.deepEqual(yield* branches, [{ refName: "main" }]);
1558+
assert.deepEqual(yield* branches, [{ refName: "main", isRepo: true }]);
15591559

15601560
// Every checkout removes HEAD.lock, which leaves the watch running.
15611561
state.currentLocalStatus = { ...baseLocalStatus, refName: "other" };
15621562
yield* Queue.offer(events, { _tag: "Remove", path: "HEAD.lock" });
15631563
yield* Queue.offer(events, { _tag: "Update", path: "HEAD" });
1564-
assert.deepEqual(yield* branches, [{ refName: "other" }]);
1564+
assert.deepEqual(yield* branches, [{ refName: "other", isRepo: true }]);
15651565

15661566
// `git worktree remove` deletes the watched directory; Windows keeps reporting it.
15671567
gitDirExists = false;

0 commit comments

Comments
 (0)