Skip to content

Commit 2c18ed0

Browse files
committed
fix(usage): include archived Codex sessions in scans
1 parent 1826fb5 commit 2c18ed0

2 files changed

Lines changed: 85 additions & 40 deletions

File tree

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ describe("UsageService", () => {
468468
}).pipe(Effect.scoped),
469469
);
470470

471-
it.live("reads configured and disabled accounts once across shared and aliased homes", () =>
471+
it.live("reads active and archived account history once across shared and aliased homes", () =>
472472
Effect.gen(function* () {
473473
const { transcript, settings, home } = yield* setup;
474474
const codexHome = NodePath.join(home, "codex-account");
@@ -502,6 +502,28 @@ describe("UsageService", () => {
502502
.map((line) => encodeUnknownJsonString(line))
503503
.join("\n") + "\n",
504504
);
505+
await NodeFSP.mkdir(NodePath.join(codexHome, "archived_sessions"), { recursive: true });
506+
await NodeFSP.copyFile(
507+
NodePath.join(codexHome, "sessions", "rollout.jsonl"),
508+
NodePath.join(codexHome, "archived_sessions", "copy.jsonl"),
509+
);
510+
await NodeFSP.writeFile(
511+
NodePath.join(codexHome, "archived_sessions", "archived.jsonl"),
512+
[
513+
{ type: "session_meta", payload: { id: "codex-archived-session" } },
514+
{ type: "turn_context", payload: { model: "gpt-5.6-sol" } },
515+
{
516+
type: "event_msg",
517+
timestamp: "2026-08-01T10:00:00Z",
518+
payload: {
519+
type: "token_count",
520+
info: { last_token_usage: { input_tokens: 10, output_tokens: 17 } },
521+
},
522+
},
523+
]
524+
.map((line) => encodeUnknownJsonString(line))
525+
.join("\n") + "\n",
526+
);
505527
await NodeFSP.mkdir(NodePath.join(grokHome, "sessions", "session"), { recursive: true });
506528
await NodeFSP.writeFile(
507529
NodePath.join(grokHome, "sessions", "session", "updates.jsonl"),
@@ -524,6 +546,9 @@ describe("UsageService", () => {
524546
serviceLayers({
525547
prefix: "usage-service-accounts-test",
526548
home,
549+
ratesDocument: {
550+
"gpt-5.6-sol": { input_cost_per_token: 0, output_cost_per_token: 1 },
551+
},
527552
settings: {
528553
...settings,
529554
providerInstances: {
@@ -557,7 +582,13 @@ describe("UsageService", () => {
557582
),
558583
);
559584
const summary = yield* service.readSummary(WINDOW);
560-
assert.strictEqual(totalOutputTokens(summary), 59);
585+
assert.strictEqual(totalOutputTokens(summary), 76);
586+
const archivedDir = yield* Effect.promise(() =>
587+
NodeFSP.realpath(NodePath.join(codexHome, "archived_sessions")),
588+
);
589+
const archivedBucket = summary.buckets.find((bucket) => bucket.sourcePath === archivedDir);
590+
assert.strictEqual(archivedBucket?.totals.outputTokens, 17);
591+
assert.strictEqual(archivedBucket?.costUsd, 17);
561592
yield* Effect.promise(() =>
562593
NodeFSP.rename(
563594
NodePath.join(codexHome, "sessions", "rollout.jsonl"),
@@ -566,21 +597,29 @@ describe("UsageService", () => {
566597
);
567598
const moved = yield* service.readSummary(WINDOW);
568599
assert.deepStrictEqual(moved.buckets, summary.buckets);
600+
yield* Effect.promise(() =>
601+
NodeFSP.rename(
602+
NodePath.join(codexHome, "sessions", "moved.jsonl"),
603+
NodePath.join(codexHome, "archived_sessions", "moved.jsonl"),
604+
),
605+
);
606+
const archived = yield* service.readSummary(WINDOW);
607+
assert.deepStrictEqual(archived.buckets, summary.buckets);
569608
yield* Effect.promise(() =>
570609
NodeFSP.rm(NodePath.join(codexHome, "sessions"), { recursive: true }),
571610
);
572611
const removed = yield* service.readSummary(WINDOW);
573612
assert.deepStrictEqual(removed.buckets, summary.buckets);
574613

575614
const sources = summary.sources.filter((source) => source.status === "ok");
576-
assert.strictEqual(sources.length, 4);
615+
assert.strictEqual(sources.length, 5);
577616
assert.strictEqual(
578617
sources.reduce((sum, source) => sum + source.scannedFiles, 0),
579-
4,
618+
6,
580619
);
581620
assert.strictEqual(
582621
sources.filter((source) => source.fingerprint.provider === "codex").length,
583-
1,
622+
2,
584623
);
585624
}).pipe(Effect.scoped),
586625
);

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -347,42 +347,48 @@ export const make = Effect.gen(function* () {
347347
environment.GROK_HOME?.trim() || path.join(NodeOS.homedir(), ".grok"),
348348
);
349349
}
350-
const directory = path.resolve(home, provider === "claude" ? "projects" : "sessions");
351-
const sourceKey = provider + "\0" + directory;
352-
const previous = sourceCache.get(sourceKey);
353-
// Keep canonical paths and source fingerprints stable after root cleanup,
354-
// including aliases and clients merging pre-cleanup environment summaries.
355-
const dir = yield* fileSystem
356-
.realPath(directory)
357-
.pipe(Effect.orElseSucceed(() => previous?.dir ?? directory));
358-
const currentVolumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
359-
const hasRetainedHistory = fileCache
360-
.entries()
361-
.some(
362-
([filePath, entry]) =>
363-
entry.provider === provider &&
364-
entry.mtimeMs >= retentionCutoffMs &&
365-
entry.records.length + entry.tailRecords.length > 0 &&
366-
isWithinDirectory(filePath, dir),
367-
);
368-
// A recreated directory still reports the retained history under its old identity.
369-
const volumeId =
370-
previous?.dir === dir && (hasRetainedHistory || !currentVolumeId)
371-
? previous.volumeId || currentVolumeId
372-
: currentVolumeId;
373-
if (previous?.dir !== dir || previous.volumeId !== volumeId) {
374-
sourceCache.set(sourceKey, { dir, volumeId });
375-
cacheDirty = true;
350+
const directories =
351+
provider === "codex"
352+
? ["sessions", "archived_sessions"]
353+
: [provider === "claude" ? "projects" : "sessions"];
354+
for (const name of directories) {
355+
const directory = path.resolve(home, name);
356+
const sourceKey = provider + "\0" + directory;
357+
const previous = sourceCache.get(sourceKey);
358+
// Keep canonical paths and source fingerprints stable after root cleanup,
359+
// including aliases and clients merging pre-cleanup environment summaries.
360+
const dir = yield* fileSystem
361+
.realPath(directory)
362+
.pipe(Effect.orElseSucceed(() => previous?.dir ?? directory));
363+
const currentVolumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
364+
const hasRetainedHistory = fileCache
365+
.entries()
366+
.some(
367+
([filePath, entry]) =>
368+
entry.provider === provider &&
369+
entry.mtimeMs >= retentionCutoffMs &&
370+
entry.records.length + entry.tailRecords.length > 0 &&
371+
isWithinDirectory(filePath, dir),
372+
);
373+
// A recreated directory still reports the retained history under its old identity.
374+
const volumeId =
375+
previous?.dir === dir && (hasRetainedHistory || !currentVolumeId)
376+
? previous.volumeId || currentVolumeId
377+
: currentVolumeId;
378+
if (previous?.dir !== dir || previous.volumeId !== volumeId) {
379+
sourceCache.set(sourceKey, { dir, volumeId });
380+
cacheDirty = true;
381+
}
382+
const key = `${provider}\0${dir}`;
383+
if (seen.has(key)) continue;
384+
seen.add(key);
385+
dirs.push({
386+
provider,
387+
dir,
388+
volumeId,
389+
...(provider === "grok" ? { fileName: "updates.jsonl" } : {}),
390+
});
376391
}
377-
const key = `${provider}\0${dir}`;
378-
if (seen.has(key)) continue;
379-
seen.add(key);
380-
dirs.push({
381-
provider,
382-
dir,
383-
volumeId,
384-
...(provider === "grok" ? { fileName: "updates.jsonl" } : {}),
385-
});
386392
}
387393
}
388394
return dirs;

0 commit comments

Comments
 (0)