Skip to content

Commit 40adc07

Browse files
t3dotggclaude
andcommitted
fix(usage): keep saved Codex usage across the cache upgrade
Dropping v4 Codex cache entries lost saved usage for rollouts that were deleted from disk. Keep them and force a full re-parse when the rollout still exists. Each cache version now writes its own file, so an older server sharing the state directory never replaces a newer cache, or the reverse. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 372b2fd commit 40adc07

4 files changed

Lines changed: 127 additions & 34 deletions

File tree

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as UsageService from "./UsageService.ts";
3434

3535
const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
3636
const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
37+
const decodeUnknownJsonString = Schema.decodeSync(Schema.fromJsonString(Schema.Unknown));
3738

3839
function claudeLine(id: number, outputTokens: number, model = "claude-fable-5"): string {
3940
return `${JSON.stringify({
@@ -788,6 +789,98 @@ describe("UsageService", () => {
788789
}).pipe(Effect.scoped),
789790
);
790791

792+
it.live(
793+
"upgrades a v4 cache: reprices live Codex tiers, keeps deleted rollouts, leaves v4 intact",
794+
() =>
795+
Effect.gen(function* () {
796+
const { home, settings } = yield* setup;
797+
const sessions = NodePath.join(home, "codex", "sessions");
798+
const rollout = (sessionId: string, outputTokens: number) =>
799+
[
800+
{ type: "session_meta", payload: { id: sessionId } },
801+
{ type: "turn_context", payload: { model: "gpt-6-astra" } },
802+
{
803+
type: "event_msg",
804+
payload: {
805+
type: "thread_settings_applied",
806+
thread_settings: { service_tier: "ultrafast" },
807+
},
808+
},
809+
{
810+
type: "event_msg",
811+
timestamp: "2026-08-01T10:00:00Z",
812+
payload: {
813+
type: "token_count",
814+
info: { last_token_usage: { input_tokens: 0, output_tokens: outputTokens } },
815+
},
816+
},
817+
]
818+
.map((line) => encodeUnknownJsonString(line))
819+
.join("\n") + "\n";
820+
const live = NodePath.join(sessions, "live.jsonl");
821+
const deleted = NodePath.join(sessions, "deleted.jsonl");
822+
yield* Effect.promise(async () => {
823+
await NodeFSP.mkdir(sessions, { recursive: true });
824+
await NodeFSP.writeFile(live, rollout("live", 10));
825+
await NodeFSP.writeFile(deleted, rollout("deleted", 20));
826+
});
827+
828+
yield* Effect.gen(function* () {
829+
const { stateDir } = yield* ServerConfig.ServerConfig;
830+
const cachePath = NodePath.join(stateDir, "usage-scan-cache-v5.json");
831+
const legacyPath = NodePath.join(stateDir, "usage-scan-cache.json");
832+
yield* (yield* UsageService.make).readSummary(WINDOW);
833+
834+
// Rewrite the cache as a v4 server left it: every Codex record at
835+
// speed 0 (standard), and no tier in the reducer state.
836+
const legacy = yield* Effect.promise(async () => {
837+
const document = decodeUnknownJsonString(await NodeFSP.readFile(cachePath, "utf8")) as {
838+
files: Record<string, { r: unknown[][]; cs: { speed?: unknown } }>;
839+
};
840+
for (const file of Object.values(document.files)) {
841+
file.r = file.r.map((row) => [...row.slice(0, 10), 0]);
842+
delete file.cs.speed;
843+
}
844+
const text = encodeUnknownJsonString({ ...document, version: 4 });
845+
await NodeFSP.writeFile(legacyPath, text);
846+
await NodeFSP.rm(cachePath);
847+
await NodeFSP.rm(deleted);
848+
return text;
849+
});
850+
851+
const summary = yield* (yield* UsageService.make).readSummary(WINDOW);
852+
// The live rollout re-parses at the ultrafast rate (10 x 6); the
853+
// deleted one keeps its saved v4 usage at the standard rate (20 x 1).
854+
assert.strictEqual(totalOutputTokens(summary), 30);
855+
assert.strictEqual(
856+
summary.buckets.reduce((sum, bucket) => sum + bucket.costUsd, 0),
857+
80,
858+
);
859+
// A v4 server sharing this state directory still finds its own cache.
860+
assert.strictEqual(
861+
yield* Effect.promise(() => NodeFSP.readFile(legacyPath, "utf8")),
862+
legacy,
863+
);
864+
}).pipe(
865+
Effect.provide(
866+
serviceLayers({
867+
prefix: "usage-service-v4-upgrade-test",
868+
home,
869+
settings,
870+
ratesDocument: {
871+
"gpt-6-astra": {
872+
input_cost_per_token: 0,
873+
output_cost_per_token: 1,
874+
input_cost_per_token_ultrafast: 0,
875+
output_cost_per_token_ultrafast: 6,
876+
},
877+
},
878+
}),
879+
),
880+
);
881+
}).pipe(Effect.scoped),
882+
);
883+
791884
it.live("preserves saved tokens, costs and sessions after transcript cleanup and restart", () =>
792885
Effect.gen(function* () {
793886
const { transcript, settings, home } = yield* setup;

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ import {
6464
decodeScanCache,
6565
dedupeWithinFile,
6666
encodeScanCache,
67+
LEGACY_SCAN_CACHE_FILE_NAME,
6768
pruneScanCache,
69+
SCAN_CACHE_FILE_NAME,
6870
type ScanCache,
6971
} from "./usageScanCache.ts";
7072
import type { UsageRecord } from "./usageTranscripts.ts";
@@ -168,7 +170,8 @@ export const make = Effect.gen(function* () {
168170
};
169171

170172
const ratesCachePath = path.join(config.stateDir, "usage-model-rates.json");
171-
const scanCachePath = path.join(config.stateDir, "usage-scan-cache.json");
173+
const scanCachePath = path.join(config.stateDir, SCAN_CACHE_FILE_NAME);
174+
const legacyScanCachePath = path.join(config.stateDir, LEGACY_SCAN_CACHE_FILE_NAME);
172175
let rates: RateTable = new Map();
173176
let ratesFetchedAtMs: number | null = null;
174177
let ratesStatus: UsagePricing["status"] = "unavailable";
@@ -362,10 +365,17 @@ export const make = Effect.gen(function* () {
362365
*/
363366
const ensureScanCacheLoaded = yield* Effect.cached(
364367
Effect.gen(function* () {
365-
const document = yield* fileSystem.readFileString(scanCachePath).pipe(
366-
Effect.flatMap((raw) => decodeScanCacheFile(raw)),
367-
Effect.catchCause(() => Effect.succeed(null)),
368-
);
368+
const readDocument = (filePath: string) =>
369+
fileSystem.readFileString(filePath).pipe(
370+
Effect.flatMap((raw) => decodeScanCacheFile(raw)),
371+
Effect.catchCause(() => Effect.succeed(null)),
372+
);
373+
let document = yield* readDocument(scanCachePath);
374+
if (document === null) {
375+
document = yield* readDocument(legacyScanCachePath);
376+
// Write the migrated cache to its own file on the next scan.
377+
cacheDirty = document !== null;
378+
}
369379
if (document === null) return;
370380
for (const [path, entry] of decodeScanCache(document)) fileCache.set(path, entry);
371381
const sources = decodeCachedSources(document);

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,6 @@ describe("scan cache round trip", () => {
149149
expect(decodeScanCache(JSON.parse(JSON.stringify(previous))).size).toBe(0);
150150
});
151151

152-
it("keeps v4 Claude entries but re-parses v4 Codex entries, which lack a tier", () => {
153-
const cache = cacheWith([["/claude.jsonl", 100, [record({ speed: "fast" })]]]);
154-
cache.set("/codex.jsonl", {
155-
size: 80,
156-
mtimeMs: 400,
157-
provider: "codex",
158-
records: [record({ provider: "codex", model: "gpt-6-astra", dedupeKey: null })],
159-
tailRecords: [],
160-
position: position(),
161-
});
162-
const v4 = { ...encodeScanCache(cache), version: 4 };
163-
164-
const restored = decodeScanCache(JSON.parse(JSON.stringify(v4)));
165-
166-
expect([...restored.keys()]).toEqual(["/claude.jsonl"]);
167-
expect(restored.get("/claude.jsonl")).toEqual(cache.get("/claude.jsonl"));
168-
});
169-
170152
it("interns repeated model and session strings", () => {
171153
const encoded = encodeScanCache(
172154
cacheWith([["/a.jsonl", 100, [record(), record({ dedupeKey: "msg_2:" }), record()]]]),

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ import type { CodexScanState, UsageRecord, UsageSpeed } from "./usageTranscripts
2525
// re-parses only its appended bytes instead of starting over.
2626
// v4: records carry Claude fast mode, which v3 rows never captured.
2727
// v5: Codex records carry their service tier. v4 rows store speed the same
28-
// way, so v4 Claude and Grok entries still load; v4 Codex entries are dropped
29-
// and re-parsed, since they were all recorded as standard.
28+
// way, so v4 entries still load; see `decodeScanCache` for v4 Codex entries.
3029
const USAGE_SCAN_CACHE_VERSION = 5 as const;
3130
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
3231

32+
/**
33+
* Each cache version writes its own file in the state directory. An older
34+
* server sharing that directory cannot read a newer cache and would replace
35+
* it, dropping saved usage for deleted transcripts. Separate files keep both.
36+
* A v5 server reads the legacy (v4) file once, when its own file is missing.
37+
*/
38+
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v5.json";
39+
export const LEGACY_SCAN_CACHE_FILE_NAME = "usage-scan-cache.json";
40+
3341
/** Serialised as the index into this list. */
3442
const SPEEDS: readonly UsageSpeed[] = ["standard", "fast", "ultrafast"];
3543

@@ -242,7 +250,6 @@ export function decodeScanCache(document: unknown): ScanCache {
242250
const entry = raw as Partial<SerializedFile>;
243251
if (typeof entry.s !== "number" || typeof entry.m !== "number") continue;
244252
if (entry.p !== "claude" && entry.p !== "codex" && entry.p !== "grok") continue;
245-
if (entry.p === "codex" && version < USAGE_SCAN_CACHE_VERSION) continue;
246253
if (!isRecordArray(entry.r) || !isRecordArray(entry.t)) continue;
247254
// Position fields feed byte offsets and a Buffer allocation in the reader,
248255
// so anything outside their real ranges must reject the entry: a bogus
@@ -262,7 +269,11 @@ export function decodeScanCache(document: unknown): ScanCache {
262269
) {
263270
continue;
264271
}
265-
const codexState = decodeCodexState(entry.cs);
272+
// v4 Codex records predate service tiers, so they all priced as standard.
273+
// Keep them, because the rollout may be gone, but make a live rollout
274+
// re-parse whole: no file has size -1, and a zero position cannot resume.
275+
const legacyCodex = entry.p === "codex" && version < USAGE_SCAN_CACHE_VERSION;
276+
const codexState = legacyCodex ? null : decodeCodexState(entry.cs);
266277
if (codexState === undefined) continue;
267278

268279
const provider: UsageProviderKind = entry.p;
@@ -271,17 +282,14 @@ export function decodeScanCache(document: unknown): ScanCache {
271282
if (records === null || tailRecords === null) continue;
272283

273284
cache.set(path, {
274-
size: entry.s,
285+
size: legacyCodex ? -1 : entry.s,
275286
mtimeMs: entry.m,
276287
provider,
277288
records,
278289
tailRecords,
279-
position: {
280-
resumeOffset: entry.o,
281-
guardLength: entry.gl,
282-
guardHash: entry.gh,
283-
codexState,
284-
},
290+
position: legacyCodex
291+
? { resumeOffset: 0, guardLength: 0, guardHash: 0, codexState: null }
292+
: { resumeOffset: entry.o, guardLength: entry.gl, guardHash: entry.gh, codexState },
285293
});
286294
}
287295

0 commit comments

Comments
 (0)