perf(server): scan only appended transcript bytes for usage summaries (#9024)

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-09-01 02:59:16 -07:00
1 parent 0bfb6df34b
commit 8f1ef8b9eb
6 files changed
+964 -91

No files matched your search

+73 -2
View File
@@ -5,6 +5,7 @@ import {
dedupeWithinFile,
encodeScanCache,
pruneScanCache,
type CachedFile,
type ScanCache,
} from "./usageScanCache.ts";
import type { UsageRecord } from "./usageTranscripts.ts";
@@ -28,10 +29,27 @@ function record(overrides: Partial<UsageRecord> = {}): UsageRecord {
};
}
function position(overrides: Partial<CachedFile["position"]> = {}): CachedFile["position"] {
return {
resumeOffset: 120,
guardLength: 64,
guardHash: 0xdeadbeef,
codexState: null,
...overrides,
};
}
function cacheWith(entries: readonly [string, number, readonly UsageRecord[]][]): ScanCache {
const cache: ScanCache = new Map();
for (const [path, mtimeMs, records] of entries) {
cache.set(path, { size: records.length * 10, mtimeMs, provider: "claude", records });
cache.set(path, {
size: records.length * 10,
mtimeMs,
provider: "claude",
records,
tailRecords: [],
position: position(),
});
}
return cache;
}
@@ -49,14 +67,67 @@ describe("scan cache round trip", () => {
records: [
record({ provider: "grok", model: "grok-4.5-build", dedupeKey: "s:p:grok-4.5-build" }),
],
tailRecords: [record({ provider: "grok", model: "grok-4.5-build", dedupeKey: null })],
position: position({ resumeOffset: 30, guardLength: 30, guardHash: 123 }),
});
original.set("/codex.jsonl", {
size: 80,
mtimeMs: 400,
provider: "codex",
records: [record({ provider: "codex", model: "gpt-5.2-codex", dedupeKey: null })],
tailRecords: [],
position: position({
codexState: {
model: "gpt-5.2-codex",
sessionId: "session-c",
lastUsageSignature: '{"input_tokens":1}',
sawSessionMeta: true,
suppressingForkCopies: false,
forkCopyAnchorMs: 0,
},
}),
});
const restored = decodeScanCache(JSON.parse(JSON.stringify(encodeScanCache(original))));
expect(restored.size).toBe(3);
expect(restored.size).toBe(4);
expect(restored.get("/a.jsonl")).toEqual(original.get("/a.jsonl"));
expect(restored.get("/b.jsonl")).toEqual(original.get("/b.jsonl"));
expect(restored.get("/grok.jsonl")).toEqual(original.get("/grok.jsonl"));
expect(restored.get("/codex.jsonl")).toEqual(original.get("/codex.jsonl"));
});
it("drops an entry whose persisted parse state is corrupt", () => {
// Resuming with a bad reducer state would attach appended usage to the
// wrong model or replay fork-copied history; that entry must cold parse.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = {
...encoded,
files: {
"/a.jsonl": { ...encoded.files["/a.jsonl"]!, cs: { model: 42 } },
},
};
expect(decodeScanCache(JSON.parse(JSON.stringify(poisoned))).has("/a.jsonl")).toBe(false);
});
it("drops an entry whose guard length is outside the supported range", () => {
// The guard length sizes a Buffer in the reader; a bogus value would make
// every parse of that file fail and silently drop its usage.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = {
...encoded,
files: { "/a.jsonl": { ...encoded.files["/a.jsonl"]!, gl: 1e20 } },
};
expect(decodeScanCache(JSON.parse(JSON.stringify(poisoned))).has("/a.jsonl")).toBe(false);
});
it("rejects a document from the previous cache version", () => {
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const previous = { ...encoded, version: 2 };
expect(decodeScanCache(JSON.parse(JSON.stringify(previous))).size).toBe(0);
});
it("interns repeated model and session strings", () => {