The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 19:30 UTC
fix(usage): count initial Codex requests with inherited totals (#23729)
Alex committed
Oct 22, 2026 at 10:56 UTC
5e8c5eb75aa43f4ef673255abe7cc4ed9b1d19b9
6 files changed
+222
-14
apps/server/src/provider/Drivers/codexUsage.test.ts
+117
@@ -182,6 +182,123 @@ describe("parseCodexLine", () => {
182
}
183
});
184
185
+ it.each(["continuation", "fork", "subagent"])(
186
+ "bills only the latest request when a %s has no initial counter baseline",
187
+ (source) => {
188
+ const state = initialCodexScanState();
189
+ parseCodexLine(
190
+ JSON.stringify({
191
+ type: "session_meta",
192
+ timestamp: "2026-08-01T05:17:41.000Z",
193
+ payload: {
194
+ id: "child",
195
+ model_provider: "openai",
196
+ ...(source === "fork" ? { forked_from_id: "parent" } : {}),
197
+ ...(source === "subagent"
198
+ ? { source: { subagent: { thread_spawn: { parent_thread_id: "parent" } } } }
199
+ : { source: "vscode" }),
200
+ },
201
+ }),
202
+ state,
203
+ );
204
+ parseCodexLine(turnContext, state);
205
+ const request = {
206
+ ...counters(100, 20),
207
+ cached_input_tokens: 30,
208
+ cache_write_input_tokens: 10,
209
+ reasoning_output_tokens: 5,
210
+ };
211
+ const cumulative = {
212
+ ...counters(500, 60),
213
+ cached_input_tokens: 200,
214
+ cache_write_input_tokens: 20,
215
+ reasoning_output_tokens: 15,
216
+ };
217
+ const first = counted(request, cumulative);
218
+ expect(parseCodexLine(first, state)).toMatchObject({
219
+ sessionId: "child",
220
+ totals: {
221
+ uncachedInputTokens: 60,
222
+ cachedInputTokens: 30,
223
+ cacheCreationTokens: 10,
224
+ outputTokens: 20,
225
+ reasoningTokens: 5,
226
+ },
227
+ });
228
+ expect(parseCodexLine(first, state)).toBeNull();
229
+ expect(parseCodexLine(counted(counters(10, 2), cumulative), state)).toBeNull();
230
+ expect(
231
+ parseCodexLine(
232
+ counted(counters(10, 2), {
233
+ ...cumulative,
234
+ input_tokens: 510,
235
+ output_tokens: 62,
236
+ total_tokens: 572,
237
+ }),
238
+ state,
239
+ )?.totals.outputTokens,
240
+ ).toBe(2);
241
+ expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
242
+ },
243
+ );
244
+
245
+ it.each([
246
+ {
247
+ ...counters(90, 40),
248
+ cached_input_tokens: 60,
249
+ cache_write_input_tokens: 20,
250
+ reasoning_output_tokens: 10,
251
+ },
252
+ {
253
+ ...counters(200, 10),
254
+ cached_input_tokens: 60,
255
+ cache_write_input_tokens: 20,
256
+ reasoning_output_tokens: 5,
257
+ },
258
+ {
259
+ ...counters(200, 40),
260
+ cached_input_tokens: 20,
261
+ cache_write_input_tokens: 20,
262
+ reasoning_output_tokens: 10,
263
+ },
264
+ {
265
+ ...counters(200, 40),
266
+ cached_input_tokens: 60,
267
+ cache_write_input_tokens: 5,
268
+ reasoning_output_tokens: 10,
269
+ },
270
+ {
271
+ ...counters(200, 40),
272
+ cached_input_tokens: 60,
273
+ cache_write_input_tokens: 20,
274
+ reasoning_output_tokens: 3,
275
+ },
276
+ {
277
+ ...counters(110, 40),
278
+ cached_input_tokens: 60,
279
+ cache_write_input_tokens: 20,
280
+ reasoning_output_tokens: 10,
281
+ },
282
+ {
283
+ ...counters(200, 30),
284
+ cached_input_tokens: 60,
285
+ cache_write_input_tokens: 20,
286
+ reasoning_output_tokens: 20,
287
+ },
288
+ ])("rejects an initial snapshot with an impossible inherited baseline: %j", (cumulative) => {
289
+ const state = initialCodexScanState();
290
+ parseCodexLine(turnContext, state);
291
+ const request = {
292
+ ...counters(100, 20),
293
+ cached_input_tokens: 30,
294
+ cache_write_input_tokens: 10,
295
+ reasoning_output_tokens: 5,
296
+ };
297
+ expect(parseCodexLine(counted(request, cumulative), state)).toBeNull();
298
+ expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
299
+ expect(state.lastCumulativeUsage).toEqual(cumulative);
300
+ });
301
+
302
it.each(["request", "cumulative"])("skips malformed %s counters and recovers", (malformed) => {
303
const state = initialCodexScanState();
304
parseCodexLine(turnContext, state);
apps/server/src/provider/Drivers/codexUsage.ts
+9
-4
@@ -243,10 +243,15 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
243
if (cumulative !== null) {
244
// A session resumed in a new Codex process restarts its cumulative counters at this request.
245
const restarted = CODEX_USAGE_FIELDS.every((key) => cumulative[key] === last[key]);
246
- if (
247
- !restarted &&
248
- !CODEX_USAGE_FIELDS.every((key) => cumulative[key] - (previous?.[key] ?? 0) === last[key])
249
- ) {
246
+ // Forks and paginated continuations can inherit totals without copying a counter snapshot.
247
+ // Only bill the latest request, and require the unseen prefix to be valid usage too.
248
+ const reconciled =
249
+ previous === null
250
+ ? readCodexTokenUsage(
251
+ Object.fromEntries(CODEX_USAGE_FIELDS.map((key) => [key, cumulative[key] - last[key]])),
252
+ ) !== null
253
+ : CODEX_USAGE_FIELDS.every((key) => cumulative[key] - previous[key] === last[key]);
254
+ if (!restarted && !reconciled) {
255
state.malformedRecords += 1;
256
return null;
257
}
apps/server/src/usage/UsageService.test.ts
+14
-5
@@ -1881,7 +1881,7 @@ describe("UsageService", () => {
1881
}).pipe(Effect.scoped),
1882
);
1883
1884
- it.live.each([4, 5, 6, 7])(
1884
+ it.live.each([4, 5, 6, 7, 8])(
1885
"upgrades a v%s cache: reparses live Codex usage, keeps deleted rollouts and the old file",
1886
(version) =>
1887
Effect.gen(function* () {
@@ -1903,7 +1903,10 @@ describe("UsageService", () => {
1903
timestamp: "2026-08-01T10:00:00Z",
1904
payload: {
1905
type: "token_count",
1906
- info: { last_token_usage: { input_tokens: 0, output_tokens: outputTokens } },
1906
+ info: {
1907
+ last_token_usage: { input_tokens: 0, output_tokens: outputTokens },
1908
+ total_token_usage: { input_tokens: 100, output_tokens: outputTokens + 30 },
1909
+ },
1910
},
1911
},
1912
]
@@ -1929,13 +1932,14 @@ describe("UsageService", () => {
1932
yield* first.awaitPersisted;
1933
1934
const legacy = yield* Effect.promise(async () => {
1935
+ const livePath = await NodeFSP.realpath(live);
1936
const document = decodeUnknownJsonString(await NodeFSP.readFile(cachePath, "utf8")) as {
1933
- files: Record<string, { r: unknown[][]; cs: Record<string, unknown> }>;
1937
+ files: Record<string, { r: unknown[][]; mr: number; cs: Record<string, unknown> }>;
1938
};
1939
for (const [path, file] of Object.entries(document.files)) {
1940
file.r = file.r.map((row) => [
1941
...row.slice(0, 6),
1938
- path === live ? 99 : row[6],
1942
+ path === livePath ? 99 : row[6],
1943
...row.slice(7, 10),
1944
version === 4 ? 0 : 1,
1945
]);
@@ -1944,6 +1948,11 @@ describe("UsageService", () => {
1948
delete file.cs.lastCumulativeUsage;
1949
delete file.cs.malformedRecords;
1950
}
1951
+ if (version === 8 && path === livePath) {
1952
+ file.r = [];
1953
+ file.mr = 1;
1954
+ file.cs.malformedRecords = 1;
1955
+ }
1956
}
1957
const text = encodeUnknownJsonString({ ...document, version });
1958
await NodeFSP.rm(cachePath);
@@ -1971,7 +1980,7 @@ describe("UsageService", () => {
1980
60 + 20 * (version === 4 ? 1 : 2),
1981
);
1982
yield* upgraded.awaitPersisted;
1974
- assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v8.json");
1983
+ assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v9.json");
1984
assert.strictEqual(
1985
yield* Effect.promise(() => NodeFSP.readFile(legacyPath, "utf8")),
1986
legacy,
apps/server/src/usage/usageScanCache.test.ts
+16
-1
@@ -162,13 +162,14 @@ describe("scan cache round trip", () => {
162
expect(restored).toEqual(original);
163
});
164
165
- it.each([5, 6, 7])(
165
+ it.each([5, 6, 7, 8])(
166
"retains v%s history and compatible positions but invalidates obsolete state",
167
(version) => {
168
const original = cacheWith([["/gone.jsonl", 100, [record({ speed: "fast" })]]]);
169
original.set("/gone-codex.jsonl", {
170
...original.get("/gone.jsonl")!,
171
provider: "codex",
172
+ malformedRecords: 1,
173
records: [record({ provider: "codex", speed: "fast", dedupeKey: null })],
174
position: position({
175
state: {
@@ -191,6 +192,11 @@ describe("scan cache round trip", () => {
192
malformedRecords: 3,
193
position: position({ state: { sessionId: "session-a" } }),
194
});
195
+ const cleanCodex = {
196
+ ...original.get("/gone-codex.jsonl")!,
197
+ malformedRecords: 0,
198
+ };
199
+ original.set("/clean-codex.jsonl", cleanCodex);
200
const encoded = encodeScanCache(original);
201
const entry = encoded.files["/gone.jsonl"]!;
202
const legacy = {
@@ -219,6 +225,15 @@ describe("scan cache round trip", () => {
225
size: -1,
226
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
227
});
228
+ expect(restored.get("/clean-codex.jsonl")).toEqual(
229
+ version >= 8
230
+ ? cleanCodex
231
+ : {
232
+ ...cleanCodex,
233
+ size: -1,
234
+ position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
235
+ },
236
+ );
237
expect(restored.get("/gone-pi.jsonl")).toEqual(
238
version >= 6
239
? original.get("/gone-pi.jsonl")
apps/server/src/usage/usageScanCache.ts
+7
-4
@@ -9,7 +9,7 @@ import * as Schema from "effect/Schema";
9
import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
10
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
11
12
-const USAGE_SCAN_CACHE_VERSION = 8 as const;
12
+const USAGE_SCAN_CACHE_VERSION = 9 as const;
13
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
14
15
/**
@@ -17,8 +17,9 @@ const SPEED_COMPATIBLE_SINCE_VERSION = 4;
17
* replace it, dropping saved usage for deleted transcripts. Separate files keep
18
* both.
19
*/
20
-export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v8.json";
20
+export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v9.json";
21
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
22
+ "usage-scan-cache-v8.json",
23
"usage-scan-cache-v7.json",
24
"usage-scan-cache-v6.json",
25
"usage-scan-cache-v5.json",
@@ -311,9 +312,11 @@ export function decodeScanCache(
312
continue;
313
}
314
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
314
- // Older Codex entries lack provenance state (v7) or misread compaction and resume counters (v8).
315
+ // v9 repairs skipped initial Codex requests; clean v8 entries can still resume.
316
const legacy =
316
- format.state !== undefined && (version < 6 || (entry.p === "codex" && version < 8));
317
+ format.state !== undefined &&
318
+ (version < 6 ||
319
+ (entry.p === "codex" && (version < 8 || (version < 9 && malformedRecords > 0))));
320
// A corrupt state disqualifies the entry: resuming with it would attach
321
// appended usage to the wrong model or replay fork-copied history.
322
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;
apps/server/src/usage/usageTranscriptReader.test.ts
+59
@@ -221,6 +221,65 @@ describe("readTranscriptRecords resume", () => {
221
assert.strictEqual(second.tailRecords.length, 0);
222
});
223
224
+ it.each([
225
+ { mode: "buffered", streamingThresholdBytes: Infinity },
226
+ { mode: "streaming", streamingThresholdBytes: 0 },
227
+ ])(
228
+ "resumes an initial Codex carried-total tail without rebilling it ($mode)",
229
+ async (options) => {
230
+ const path = NodePath.join(dir, "continuation.jsonl");
231
+ const usage = (input: number, output: number) => ({
232
+ input_tokens: input,
233
+ output_tokens: output,
234
+ });
235
+ const event = (last: unknown, cumulative: unknown) =>
236
+ JSON.stringify({
237
+ type: "event_msg",
238
+ timestamp: "2026-08-01T10:00:05Z",
239
+ payload: {
240
+ type: "token_count",
241
+ info: { last_token_usage: last, total_token_usage: cumulative },
242
+ },
243
+ });
244
+ const firstLine = event(usage(100, 20), usage(500, 60));
245
+ await NodeFSP.writeFile(
246
+ path,
247
+ codexMetaLine() + codexModelLine("gpt-6-astra") + codexTierLine("ultrafast") + firstLine,
248
+ );
249
+ const first = await readTranscriptRecords(path, TEST_FORMATS.codex, undefined, options);
250
+ assert.isNotNull(first);
251
+ assert.strictEqual(first.records.length, 0);
252
+ assert.deepStrictEqual(
253
+ first.tailRecords.map((r) => [r.totals.outputTokens, r.speed]),
254
+ [[20, "ultrafast"]],
255
+ );
256
+ assert.strictEqual(first.malformedRecords, 0);
257
+
258
+ await NodeFSP.appendFile(path, `\n${firstLine}\n${event(usage(10, 2), usage(510, 62))}\n`);
259
+ const resumed = await readTranscriptRecords(
260
+ path,
261
+ TEST_FORMATS.codex,
262
+ JSON.parse(JSON.stringify(first.position)),
263
+ options,
264
+ );
265
+ const full = await readTranscriptRecords(path, TEST_FORMATS.codex, undefined, options);
266
+ assert.isNotNull(resumed);
267
+ assert.isNotNull(full);
268
+ assert.isTrue(resumed.resumed);
269
+ assert.deepStrictEqual(resumed.records, full.records);
270
+ assert.deepStrictEqual(
271
+ resumed.records.map((r) => [r.totals.outputTokens, r.speed]),
272
+ [
273
+ [20, "ultrafast"],
274
+ [2, "ultrafast"],
275
+ ],
276
+ );
277
+ assert.deepStrictEqual(resumed.tailRecords, []);
278
+ assert.strictEqual(resumed.malformedRecords, 0);
279
+ assert.deepStrictEqual(resumed.position, full.position);
280
+ },
281
+ );
282
+
283
it("re-parses from the start when the guard bytes no longer match", async () => {
284
const path = NodePath.join(dir, "claude.jsonl");
285
await NodeFSP.writeFile(path, claudeLine(1, 5));