The best way to control Claude Code, Codex and more from any computer or phone
fix(usage): Codex compaction and resumed sessions no longer show skipped-usage warnings (#22558)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 20, 2026 at 21:39 UTC
733a303abf313d16e9e32c3436488cb286a4478b
5 files changed
+47
-17
apps/server/src/provider/Drivers/codexUsage.test.ts
+30
@@ -226,6 +226,36 @@ describe("parseCodexLine", () => {
226
expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
227
});
228
229
+ it.each([false, true])(
230
+ "accepts the post-compaction context marker without a malformed record (prior request: %s)",
231
+ (priorRequest) => {
232
+ const state = initialCodexScanState();
233
+ parseCodexLine(turnContext, state);
234
+ if (priorRequest) parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
235
+ const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0), {
236
+ model_context_window: 475000,
237
+ });
238
+ expect(parseCodexLine(marker, state)).toBeNull();
239
+ expect(
240
+ parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
241
+ ).toBe(5);
242
+ expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
243
+ },
244
+ );
245
+
246
+ it("bills the first request after a resume restarts the cumulative counters", () => {
247
+ const state = initialCodexScanState();
248
+ parseCodexLine(turnContext, state);
249
+ parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
250
+ expect(
251
+ parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
252
+ ).toBe(5);
253
+ expect(
254
+ parseCodexLine(counted(counters(10, 2), counters(40, 7)), state)?.totals.outputTokens,
255
+ ).toBe(2);
256
+ expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
257
+ });
258
+
259
it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => {
260
const meta = (id: string, model_provider?: string) =>
261
JSON.stringify({ type: "session_meta", payload: { id, model_provider } });
apps/server/src/provider/Drivers/codexUsage.ts
+7
-8
@@ -219,13 +219,9 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
219
return null;
220
}
221
222
- // fill_to_context_window uses zero components and synthetic totals, then offsets future totals.
223
- if (
224
- cumulative !== null &&
225
- last.input_tokens + last.output_tokens === 0 &&
226
- cumulative.input_tokens + cumulative.output_tokens === 0 &&
227
- cumulative.total_tokens === infoRecord["model_context_window"]
228
- ) {
222
+ // Zero-component counters report context size, not a request: fill_to_context_window, or the
223
+ // post-compaction marker whose total is the new context size (its cumulative may reset to zero).
224
+ if (last.input_tokens + last.output_tokens === 0) {
225
state.lastUsageSignature = null;
226
return null;
227
}
@@ -245,7 +241,10 @@ function parseCodexRecord(parsed: unknown, state: CodexScanState): UsageRecord |
241
242
const signature = JSON.stringify(last);
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
) {
250
state.malformedRecords += 1;
@@ -325,7 +324,7 @@ export const codexUsageFormat: TranscriptUsageFormat<CodexScanState> = {
324
thread_settings: { service_tier: true },
325
forked_from_id: true,
326
source: { subagent: { thread_spawn: { parent_thread_id: true } } },
328
- info: { last_token_usage: true, total_token_usage: true, model_context_window: true },
327
+ info: { last_token_usage: true, total_token_usage: true },
328
},
329
},
330
// `turn_context`, `thread_settings_applied` and `session_meta` lines hold no
apps/server/src/usage/UsageService.test.ts
+2
-2
@@ -1881,7 +1881,7 @@ describe("UsageService", () => {
1881
}).pipe(Effect.scoped),
1882
);
1883
1884
- it.live.each([4, 5, 6])(
1884
+ it.live.each([4, 5, 6, 7])(
1885
"upgrades a v%s cache: reparses live Codex usage, keeps deleted rollouts and the old file",
1886
(version) =>
1887
Effect.gen(function* () {
@@ -1971,7 +1971,7 @@ describe("UsageService", () => {
1971
60 + 20 * (version === 4 ? 1 : 2),
1972
);
1973
yield* upgraded.awaitPersisted;
1974
- assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v7.json");
1974
+ assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v8.json");
1975
assert.strictEqual(
1976
yield* Effect.promise(() => NodeFSP.readFile(legacyPath, "utf8")),
1977
legacy,
apps/server/src/usage/usageScanCache.test.ts
+3
-3
@@ -162,7 +162,7 @@ describe("scan cache round trip", () => {
162
expect(restored).toEqual(original);
163
});
164
165
- it.each([5, 6])(
165
+ it.each([5, 6, 7])(
166
"retains v%s history and compatible positions but invalidates obsolete state",
167
(version) => {
168
const original = cacheWith([["/gone.jsonl", 100, [record({ speed: "fast" })]]]);
@@ -172,7 +172,7 @@ describe("scan cache round trip", () => {
172
records: [record({ provider: "codex", speed: "fast", dedupeKey: null })],
173
position: position({
174
state: {
175
- ...(version === 6 ? initialCodexScanState() : {}),
175
+ ...(version >= 6 ? initialCodexScanState() : {}),
176
model: "gpt-6-astra",
177
speed: "fast",
178
sessionId: "session-a",
@@ -220,7 +220,7 @@ describe("scan cache round trip", () => {
220
position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
221
});
222
expect(restored.get("/gone-pi.jsonl")).toEqual(
223
- version === 6
223
+ version >= 6
224
? original.get("/gone-pi.jsonl")
225
: {
226
...original.get("/gone-pi.jsonl"),
apps/server/src/usage/usageScanCache.ts
+5
-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 = 7 as const;
12
+const USAGE_SCAN_CACHE_VERSION = 8 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-v7.json";
20
+export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v8.json";
21
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
22
+ "usage-scan-cache-v7.json",
23
"usage-scan-cache-v6.json",
24
"usage-scan-cache-v5.json",
25
"usage-scan-cache.json",
@@ -310,9 +311,9 @@ export function decodeScanCache(
311
continue;
312
}
313
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
313
- // Only Codex requires new provenance state in v7.
314
+ // Older Codex entries lack provenance state (v7) or misread compaction and resume counters (v8).
315
const legacy =
315
- format.state !== undefined && (version < 6 || (entry.p === "codex" && version < 7));
316
+ format.state !== undefined && (version < 6 || (entry.p === "codex" && version < 8));
317
// A corrupt state disqualifies the entry: resuming with it would attach
318
// appended usage to the wrong model or replay fork-copied history.
319
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;