Skip to content

Commit 71dbaf1

Browse files
t3dotggclaude
andauthored
perf(usage): cut warm usage scans from seconds to milliseconds on large histories (#15149)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 39efcd8 commit 71dbaf1

9 files changed

Lines changed: 579 additions & 131 deletions

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

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off - the suite seeds and grows real
22
// transcript trees on disk, outside the service's Effect FileSystem.
3+
import * as NodeChildProcess from "node:child_process";
34
import * as NodeFSP from "node:fs/promises";
45
import * as NodeOS from "node:os";
56
import * as NodePath from "node:path";
@@ -117,6 +118,41 @@ const serviceLayers = (input: {
117118
),
118119
);
119120

121+
/** Outside `NARROW_WINDOW`, inside `WINDOW`. Seconds, as `utimes` takes them. */
122+
const BEFORE_NARROW_WINDOW = Date.parse("2026-08-01T10:00:00Z") / 1000;
123+
const NARROW_WINDOW: UsageSummaryInput = {
124+
timeZone: "UTC",
125+
sinceDay: UsageDay.make("2026-09-01"),
126+
untilDay: UsageDay.make("2026-09-02"),
127+
};
128+
129+
/**
130+
* A FIFO named like a transcript. A scan's read of it waits in `open` until
131+
* `openGate`, then fails at once, so a gate holds that scan's directory reads
132+
* in flight. Each waiting gate holds one libuv pool thread; keep at most three.
133+
*/
134+
const makeGate = (path: string, lastWriteSeconds?: number) =>
135+
Effect.promise(async () => {
136+
NodeChildProcess.execFileSync("mkfifo", [path]);
137+
if (lastWriteSeconds !== undefined) {
138+
await NodeFSP.utimes(path, lastWriteSeconds, lastWriteSeconds);
139+
}
140+
});
141+
142+
/**
143+
* Returns once a scan has opened the gate. A scan opens every file of a
144+
* directory at once, so it then holds the directory's transcripts open too.
145+
*/
146+
const openGate = (path: string) =>
147+
Effect.promise(async () => (await NodeFSP.open(path, "w")).close());
148+
149+
/** Replaces a file by rename, so a scan holding the old one keeps reading it. */
150+
const replaceFile = (path: string, content: string) =>
151+
Effect.promise(async () => {
152+
await NodeFSP.writeFile(path + ".next", content);
153+
await NodeFSP.rename(path + ".next", path);
154+
});
155+
120156
function totalOutputTokens(summary: { buckets: readonly { totals: { outputTokens: number } }[] }) {
121157
return summary.buckets.reduce((sum, bucket) => sum + bucket.totals.outputTokens, 0);
122158
}
@@ -972,6 +1008,157 @@ describe("UsageService", () => {
9721008
}).pipe(Effect.scoped),
9731009
);
9741010

1011+
it.live("credits the same copy of a duplicate after its transcripts are deleted", () =>
1012+
Effect.gen(function* () {
1013+
const { transcript, settings, home } = yield* setup;
1014+
const dir = NodePath.dirname(transcript);
1015+
// The walk-first file is the original, padded so it finishes parsing
1016+
// after the small fork copy that repeats its record under a new session.
1017+
const [first = "", second = ""] = yield* Effect.promise(async () => {
1018+
await NodeFSP.writeFile(NodePath.join(dir, "a.jsonl"), "");
1019+
await NodeFSP.writeFile(NodePath.join(dir, "b.jsonl"), "");
1020+
return (await NodeFSP.readdir(dir)).map((name) => NodePath.join(dir, name));
1021+
});
1022+
const forked = (line: string) => line.replace('"session-1"', '"session-2"');
1023+
yield* Effect.promise(async () => {
1024+
await NodeFSP.writeFile(
1025+
first,
1026+
claudeLine(1, 5).replace(
1027+
'"message":',
1028+
'"padding":' + encodeUnknownJsonString("x".repeat(9 * 1024 * 1024)) + ',"message":',
1029+
),
1030+
);
1031+
await NodeFSP.writeFile(second, forked(claudeLine(1, 5)) + forked(claudeLine(2, 7)));
1032+
});
1033+
yield* Effect.gen(function* () {
1034+
const service = yield* UsageService.make;
1035+
const live = yield* service.readSummary(WINDOW);
1036+
assert.strictEqual(live.buckets[0]?.sessions, 2);
1037+
1038+
yield* Effect.promise(() => Promise.all([NodeFSP.rm(first), NodeFSP.rm(second)]));
1039+
const saved = yield* service.readSummary(WINDOW);
1040+
assert.deepStrictEqual(saved.buckets, live.buckets);
1041+
assert.deepStrictEqual(saved.sources, live.sources);
1042+
const restored = yield* (yield* UsageService.make).readSummary(WINDOW);
1043+
assert.deepStrictEqual(restored.buckets, live.buckets);
1044+
}).pipe(
1045+
Effect.provide(serviceLayers({ prefix: "usage-service-copy-order-test", home, settings })),
1046+
);
1047+
}).pipe(Effect.scoped),
1048+
);
1049+
1050+
it.live.skipIf(HostProcessPlatform.defaultValue() === "win32")(
1051+
"keeps a newer cached read when a slower scan of another window finishes later",
1052+
() =>
1053+
Effect.gen(function* () {
1054+
const { transcript, settings, home } = yield* setup;
1055+
const dir = NodePath.dirname(transcript);
1056+
const probe = NodePath.join(dir, "probe.jsonl");
1057+
const hold = NodePath.join(dir, "hold.jsonl");
1058+
yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
1059+
yield* makeGate(probe, BEFORE_NARROW_WINDOW);
1060+
yield* makeGate(hold, BEFORE_NARROW_WINDOW);
1061+
yield* Effect.gen(function* () {
1062+
const service = yield* UsageService.make;
1063+
const wide = yield* service.readSummary(WINDOW).pipe(Effect.forkChild);
1064+
yield* openGate(probe);
1065+
yield* replaceFile(transcript, claudeLine(1, 5) + claudeLine(2, 7));
1066+
// The narrow scan caches the newer read while the wide one waits.
1067+
yield* service.readSummary(NARROW_WINDOW);
1068+
yield* openGate(hold);
1069+
yield* Fiber.join(wide);
1070+
1071+
yield* Effect.promise(() =>
1072+
Promise.all([transcript, probe, hold].map((path) => NodeFSP.rm(path))),
1073+
);
1074+
assert.strictEqual(totalOutputTokens(yield* service.readSummary(WINDOW)), 12);
1075+
}).pipe(
1076+
Effect.provide(
1077+
serviceLayers({ prefix: "usage-service-stale-read-test", home, settings }),
1078+
),
1079+
);
1080+
}).pipe(Effect.scoped),
1081+
);
1082+
1083+
it.live.skipIf(HostProcessPlatform.defaultValue() === "win32")(
1084+
"keeps the later read when a scan that read earlier finishes first",
1085+
() =>
1086+
Effect.gen(function* () {
1087+
const { transcript, settings, home } = yield* setup;
1088+
const dir = NodePath.dirname(transcript);
1089+
const gate = (name: string) => NodePath.join(dir, `${name}.jsonl`);
1090+
const wideProbe = gate("wide-probe");
1091+
const wideHold = gate("wide-hold");
1092+
const narrowProbe = gate("narrow-probe");
1093+
const narrowHold = gate("narrow-hold");
1094+
yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
1095+
yield* makeGate(wideProbe, BEFORE_NARROW_WINDOW);
1096+
yield* makeGate(wideHold, BEFORE_NARROW_WINDOW);
1097+
yield* Effect.gen(function* () {
1098+
const service = yield* UsageService.make;
1099+
const wide = yield* service.readSummary(WINDOW).pipe(Effect.forkChild);
1100+
yield* openGate(wideProbe);
1101+
yield* replaceFile(transcript, claudeLine(1, 5) + claudeLine(2, 7));
1102+
// Made after the wide scan's walk, so only the narrow scan waits on them.
1103+
yield* makeGate(narrowProbe);
1104+
yield* makeGate(narrowHold);
1105+
const narrow = yield* service.readSummary(NARROW_WINDOW).pipe(Effect.forkChild);
1106+
yield* openGate(narrowProbe);
1107+
// Both scans started from an empty cache entry; the earlier read lands first.
1108+
yield* openGate(wideHold);
1109+
yield* Fiber.join(wide);
1110+
yield* openGate(narrowHold);
1111+
yield* Fiber.join(narrow);
1112+
1113+
yield* Effect.promise(() =>
1114+
Promise.all(
1115+
[transcript, wideProbe, wideHold, narrowProbe, narrowHold].map((path) =>
1116+
NodeFSP.rm(path),
1117+
),
1118+
),
1119+
);
1120+
assert.strictEqual(totalOutputTokens(yield* service.readSummary(WINDOW)), 12);
1121+
}).pipe(
1122+
Effect.provide(serviceLayers({ prefix: "usage-service-late-read-test", home, settings })),
1123+
);
1124+
}).pipe(Effect.scoped),
1125+
);
1126+
1127+
it.live("reports saved usage of a removed directory only for windows it reaches", () =>
1128+
Effect.gen(function* () {
1129+
const { transcript, settings, home } = yield* setup;
1130+
yield* Effect.promise(async () => {
1131+
await NodeFSP.writeFile(transcript, claudeLine(1, 5));
1132+
const lastWrite = Date.parse("2026-08-01T10:00:00Z") / 1000;
1133+
await NodeFSP.utimes(transcript, lastWrite, lastWrite);
1134+
});
1135+
const service = yield* UsageService.make.pipe(
1136+
Effect.provide(
1137+
serviceLayers({ prefix: "usage-service-saved-window-test", home, settings }),
1138+
),
1139+
);
1140+
const first = yield* service.readSummary(WINDOW);
1141+
yield* Effect.promise(() =>
1142+
NodeFSP.rm(NodePath.join(home, "claude", "projects"), { recursive: true }),
1143+
);
1144+
1145+
const reached = yield* service.readSummary(WINDOW);
1146+
assert.deepStrictEqual(reached.buckets, first.buckets);
1147+
assert.strictEqual(reached.sources[0]?.status, "ok");
1148+
1149+
// A missing source cannot claim this directory from another environment
1150+
// that still reads it, so it adds nothing to a window after its last write.
1151+
const later = yield* service.readSummary({
1152+
timeZone: "UTC",
1153+
sinceDay: UsageDay.make("2026-08-10"),
1154+
untilDay: UsageDay.make("2026-08-12"),
1155+
});
1156+
assert.deepStrictEqual(later.buckets, []);
1157+
assert.strictEqual(later.sources[0]?.status, "missing");
1158+
assert.strictEqual(later.sources[0]?.scannedFiles, 0);
1159+
}).pipe(Effect.scoped),
1160+
);
1161+
9751162
it.live("does not share an in-flight scan after custom prices change", () =>
9761163
Effect.gen(function* () {
9771164
const { transcript, settings, home } = yield* setup;

0 commit comments

Comments
 (0)