Skip to content

Commit 1e1ed25

Browse files
committed
fix(usage): report transcript directory listing failures
1 parent ea7f47c commit 1e1ed25

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,27 @@ describe("UsageService", () => {
619619
sources.filter((source) => source.fingerprint.provider === "codex").length,
620620
1,
621621
);
622+
623+
const archives = NodePath.join(codexHome, "archived_sessions");
624+
yield* Effect.promise(async () => {
625+
await NodeFSP.rm(archives, { recursive: true });
626+
await NodeFSP.writeFile(archives, "not a directory");
627+
});
628+
const partial = yield* service.readSummary(WINDOW);
629+
assert.deepStrictEqual(partial.buckets, removed.buckets);
630+
const partialSource = partial.sources.find(
631+
(source) => source.fingerprint.provider === "codex",
632+
);
633+
assert.strictEqual(partialSource?.status, "partial");
634+
assert.include(partialSource?.message ?? "", archives);
635+
636+
yield* Effect.promise(() => NodeFSP.rm(archives));
637+
const missingArchive = yield* service.readSummary(WINDOW);
638+
assert.deepStrictEqual(missingArchive.buckets, removed.buckets);
639+
assert.strictEqual(
640+
missingArchive.sources.find((source) => source.fingerprint.provider === "codex")?.status,
641+
"ok",
642+
);
622643
}).pipe(Effect.scoped),
623644
);
624645

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,18 +582,18 @@ export const make = Effect.gen(function* () {
582582
);
583583
const scanned: ScannedDir[] = [];
584584
for (const { provider, dir, volumeId, transcriptDirs, fileName } of dirs) {
585+
const listingErrors: string[] = [];
585586
const filesByRoot = yield* Effect.forEach(new Set(transcriptDirs ?? [dir]), (root) =>
586587
Effect.gen(function* () {
587588
const exists = yield* fileSystem
588589
.exists(root)
589590
.pipe(Effect.catchCause(() => Effect.succeed(false)));
590591
return exists
591592
? yield* Effect.promise(() =>
592-
listTranscriptFiles(
593-
root,
594-
windowStartMs,
595-
fileName === undefined ? undefined : { fileName },
596-
),
593+
listTranscriptFiles(root, windowStartMs, {
594+
...(fileName === undefined ? {} : { fileName }),
595+
onDirectoryError: (path) => listingErrors.push(path),
596+
}),
597597
)
598598
: null;
599599
}),
@@ -641,6 +641,12 @@ export const make = Effect.gen(function* () {
641641
volumeId,
642642
...(transcriptDirs ? { transcriptDirs } : {}),
643643
files: parsedFiles,
644+
...(listingErrors.length > 0
645+
? {
646+
status: "partial" as const,
647+
message: `Could not list transcript directories: ${listingErrors.join(", ")}`,
648+
}
649+
: {}),
644650
});
645651
}
646652

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ function fnv1a(buffer: Buffer): number {
151151
/**
152152
* Lists `.jsonl` transcripts under `root` last modified at or after `sinceMs`.
153153
*
154-
* Errors on individual entries are swallowed: session files rotate and get
155-
* removed while the walk is in flight, and a partial listing is far better than
156-
* failing the page.
154+
* Removed entries are skipped as session files rotate during the walk.
155+
* Other directory listing failures are reported through `onDirectoryError`
156+
* while the walk continues, so callers can identify partial coverage.
157157
*
158158
* `fileName` restricts the walk to a single basename (Grok's `updates.jsonl`).
159159
* Grok sessions also ship multi-megabyte `chat_history` and `events` logs that
@@ -167,15 +167,21 @@ function fnv1a(buffer: Buffer): number {
167167
export async function listTranscriptFiles(
168168
root: string,
169169
sinceMs: number,
170-
options?: { readonly fileName?: string },
170+
options?: {
171+
readonly fileName?: string;
172+
readonly onDirectoryError?: (path: string) => void;
173+
},
171174
): Promise<readonly TranscriptFile[]> {
172175
const fileName = options?.fileName;
173176
const candidates: string[] = [];
174177
const walk = async (dir: string): Promise<void> => {
175178
let entries;
176179
try {
177180
entries = await NodeFSP.readdir(dir, { withFileTypes: true });
178-
} catch {
181+
} catch (error) {
182+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) {
183+
options?.onDirectoryError?.(dir);
184+
}
179185
return;
180186
}
181187
for (const entry of entries) {

‎packages/shared/src/usageMerge.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ function bucketTokens(bucket: UsageBucket): number {
316316
);
317317
}
318318

319-
export function isCompatibleUsageContractVersion(version: number, expected: number): boolean {
319+
function isCompatibleUsageContractVersion(version: number, expected: number): boolean {
320320
return version >= USAGE_MERGE_COMPATIBLE_SINCE && version <= expected;
321321
}
322322

0 commit comments

Comments
 (0)