Skip to content

Commit 5024f43

Browse files
committed
fix(usage): retain source identity across cleanup
1 parent 4fdeb43 commit 5024f43

2 files changed

Lines changed: 74 additions & 30 deletions

File tree

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ describe("UsageService", () => {
133133
[
134134
{ type: "session_meta", payload: { id: "codex-account-session" } },
135135
{ type: "turn_context", payload: { model: "gpt-5.6-sol" } },
136-
{
136+
// A-B-A at one timestamp must preserve both equal A events.
137+
...[11, 12, 11].map((outputTokens) => ({
137138
type: "event_msg",
138139
timestamp: "2026-08-01T10:00:00Z",
139140
payload: {
140141
type: "token_count",
141-
info: { last_token_usage: { input_tokens: 10, output_tokens: 11 } },
142+
info: { last_token_usage: { input_tokens: 10, output_tokens: outputTokens } },
142143
},
143-
},
144+
})),
144145
]
145146
.map((line) => encodeUnknownJsonString(line))
146147
.join("\n") + "\n",
@@ -200,7 +201,7 @@ describe("UsageService", () => {
200201
),
201202
);
202203
const summary = yield* service.readSummary(WINDOW);
203-
assert.strictEqual(totalOutputTokens(summary), 36);
204+
assert.strictEqual(totalOutputTokens(summary), 59);
204205
yield* Effect.promise(() =>
205206
NodeFSP.rename(
206207
NodePath.join(codexHome, "sessions", "rollout.jsonl"),
@@ -412,6 +413,10 @@ describe("UsageService", () => {
412413
it.live("preserves saved tokens, costs and sessions after transcript cleanup and restart", () =>
413414
Effect.gen(function* () {
414415
const { transcript, settings, home } = yield* setup;
416+
const alias = NodePath.join(home, "claude-alias");
417+
yield* Effect.promise(() =>
418+
NodeFSP.symlink(NodePath.join(home, "claude"), alias, "junction"),
419+
);
415420
const content = claudeLine(1, 5);
416421
yield* Effect.promise(() => NodeFSP.writeFile(transcript, content));
417422
yield* Effect.gen(function* () {
@@ -439,17 +444,24 @@ describe("UsageService", () => {
439444
yield* Effect.promise(() =>
440445
NodeFSP.rm(NodePath.join(home, "claude", "projects"), { recursive: true }),
441446
);
442-
const missingRoot = yield* restarted.readSummary(WINDOW);
447+
const afterRootCleanup = yield* UsageService.make;
448+
const missingRoot = yield* afterRootCleanup.readSummary(WINDOW);
443449
assert.deepStrictEqual(missingRoot.buckets, first.buckets);
444450
assert.strictEqual(missingRoot.sources[0]?.distinctSessions, 1);
445451
assert.strictEqual(missingRoot.sources[0]?.status, "ok");
452+
assert.deepStrictEqual(missingRoot.sources[0]?.fingerprint, first.sources[0]?.fingerprint);
446453
const merged = mergeUsage(
447454
[
448455
{
449456
environmentId: EnvironmentId.make("cleanup-test"),
450457
label: "test",
451458
summary: missingRoot,
452459
},
460+
{
461+
environmentId: EnvironmentId.make("other-environment"),
462+
label: "before cleanup",
463+
summary: first,
464+
},
453465
],
454466
missingRoot.contractVersion,
455467
);
@@ -468,7 +480,7 @@ describe("UsageService", () => {
468480
serviceLayers({
469481
prefix: "usage-service-cleanup-test",
470482
home,
471-
settings,
483+
settings: { providers: { ...settings.providers, claudeAgent: { homePath: alias } } },
472484
ratesDocument: {
473485
"claude-fable-5": { input_cost_per_token: 1e-5, output_cost_per_token: 5e-5 },
474486
},

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

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ const ScanCacheJson = Schema.fromJsonString(Schema.Unknown as unknown as Schema.
102102
const decodeScanCacheFile = Schema.decodeUnknownEffect(ScanCacheJson);
103103
const encodeScanCacheFile = Schema.encodeEffect(ScanCacheJson);
104104
const encodeUsageRecordKey = Schema.encodeSync(ScanCacheJson);
105+
const CachedSource = Schema.Struct({ dir: Schema.String, volumeId: Schema.String });
106+
const decodeCachedSources = Schema.decodeUnknownOption(
107+
Schema.Struct({ sources: Schema.Record(Schema.String, CachedSource) }),
108+
);
105109

106110
export class UsageService extends Context.Service<
107111
UsageService,
@@ -148,6 +152,7 @@ export const make = Effect.gen(function* () {
148152
const hostEnvironment = yield* HostProcessEnvironment;
149153

150154
const fileCache: ScanCache = new Map();
155+
const sourceCache = new Map<string, typeof CachedSource.Type>();
151156
let cacheDirty = false;
152157

153158
const ratesCachePath = path.join(config.stateDir, "usage-model-rates.json");
@@ -243,7 +248,12 @@ export const make = Effect.gen(function* () {
243248
const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* (
244249
settings: ServerSettingsValue,
245250
) {
246-
const dirs: Array<{ provider: UsageProviderKind; dir: string; fileName?: string }> = [];
251+
const dirs: Array<{
252+
provider: UsageProviderKind;
253+
dir: string;
254+
volumeId: string;
255+
fileName?: string;
256+
}> = [];
247257
const seen = new Set<string>();
248258
for (const driver of ["claudeAgent", "codex", "grok"] as const) {
249259
// Disabled accounts still have history. Explicit default slots replace
@@ -281,14 +291,28 @@ export const make = Effect.gen(function* () {
281291
);
282292
}
283293
const directory = path.resolve(home, provider === "claude" ? "projects" : "sessions");
284-
// Account aliases and Codex auth overlays can share the same history.
294+
const sourceKey = provider + "\0" + directory;
295+
const previous = sourceCache.get(sourceKey);
296+
// Keep canonical paths and source fingerprints stable after root cleanup,
297+
// including aliases and clients merging pre-cleanup environment summaries.
285298
const dir = yield* fileSystem
286299
.realPath(directory)
287-
.pipe(Effect.orElseSucceed(() => directory));
300+
.pipe(Effect.orElseSucceed(() => previous?.dir ?? directory));
301+
const currentVolumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
302+
const volumeId = currentVolumeId || (previous?.dir === dir ? previous.volumeId : "");
303+
if (previous?.dir !== dir || previous.volumeId !== volumeId) {
304+
sourceCache.set(sourceKey, { dir, volumeId });
305+
cacheDirty = true;
306+
}
288307
const key = `${provider}\0${dir}`;
289308
if (seen.has(key)) continue;
290309
seen.add(key);
291-
dirs.push({ provider, dir, ...(provider === "grok" ? { fileName: "updates.jsonl" } : {}) });
310+
dirs.push({
311+
provider,
312+
dir,
313+
volumeId,
314+
...(provider === "grok" ? { fileName: "updates.jsonl" } : {}),
315+
});
292316
}
293317
}
294318
return dirs;
@@ -309,14 +333,22 @@ export const make = Effect.gen(function* () {
309333
);
310334
if (document === null) return;
311335
for (const [path, entry] of decodeScanCache(document)) fileCache.set(path, entry);
336+
const sources = decodeCachedSources(document);
337+
if (Option.isSome(sources)) {
338+
for (const [key, source] of Object.entries(sources.value.sources))
339+
sourceCache.set(key, source);
340+
}
312341
}),
313342
);
314343

315344
const persistScanCache = Effect.fn("UsageService.persistScanCache")(function* () {
316345
if (!cacheDirty) return;
317346
// Cleared only after the write lands, so a failed persist is retried on
318347
// the next scan instead of leaving disk permanently stale.
319-
yield* encodeScanCacheFile(encodeScanCache(fileCache)).pipe(
348+
yield* encodeScanCacheFile({
349+
...encodeScanCache(fileCache),
350+
sources: Object.fromEntries(sourceCache),
351+
}).pipe(
320352
Effect.flatMap((serialized) => fileSystem.writeFileString(scanCachePath, serialized)),
321353
Effect.map(() => {
322354
cacheDirty = false;
@@ -412,8 +444,7 @@ export const make = Effect.gen(function* () {
412444
Effect.provideService(Path.Path, path),
413445
);
414446
const scanned: ScannedDir[] = [];
415-
for (const { provider, dir, fileName } of dirs) {
416-
const volumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
447+
for (const { provider, dir, volumeId, fileName } of dirs) {
417448
const exists = yield* fileSystem
418449
.exists(dir)
419450
.pipe(Effect.catchCause(() => Effect.succeed(false)));
@@ -534,24 +565,25 @@ export const make = Effect.gen(function* () {
534565
continue;
535566
}
536567
scannedFiles += 1;
568+
const codexEventOccurrences = new Map<string, number>();
537569
for (const record of file.records) {
538-
// Only sessions that contributed in-window count: the mtime slack
539-
// admits boundary files whose records fall outside the range.
540-
// A moved Codex rollout can coexist with its saved copy. Codex records
541-
// have no provider event id, so identify the same session event here.
542-
const usageRecord =
543-
record.provider === "codex" && record.sessionId.length > 0
544-
? {
545-
...record,
546-
dedupeKey: encodeUsageRecordKey([
547-
record.provider,
548-
record.sessionId,
549-
record.timestampMs,
550-
record.model,
551-
record.totals,
552-
]),
553-
}
554-
: record;
570+
let usageRecord = record;
571+
if (record.provider === "codex" && record.sessionId.length > 0) {
572+
// Match moved rollout copies without collapsing repeated equal events
573+
// within one rollout (timestamps can have only second precision).
574+
const key = encodeUsageRecordKey([
575+
record.provider,
576+
record.sessionId,
577+
record.timestampMs,
578+
record.model,
579+
record.totals,
580+
]);
581+
const occurrence = (codexEventOccurrences.get(key) ?? 0) + 1;
582+
codexEventOccurrences.set(key, occurrence);
583+
usageRecord = { ...record, dedupeKey: key + ":" + occurrence };
584+
}
585+
// Only sessions contributing in-window count; the mtime slack can
586+
// admit boundary files whose records fall outside the range.
555587
if (aggregator.add(usageRecord) && record.sessionId.length > 0) {
556588
sessionIds.add(record.sessionId);
557589
}

0 commit comments

Comments
 (0)