Skip to content

Commit 58de4b3

Browse files
fix(usage): mark Codex source partial when only archives remain
AI-assisted; verified with the tests and tools listed above.
1 parent 7d1163e commit 58de4b3

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,12 +2716,32 @@ describe("UsageService", () => {
27162716

27172717
it.live("counts archived Codex rollouts once, including one copied to both directories", () =>
27182718
Effect.gen(function* () {
2719-
const { home, settings } = yield* setupArchivedCodex;
2720-
yield* Effect.gen(function* () {
2719+
const { home, settings, codexHome } = yield* setupArchivedCodex;
2720+
const complete = yield* Effect.gen(function* () {
27212721
const service = yield* UsageService.make;
2722-
const summary = yield* service.readSummary(WINDOW);
2723-
assert.strictEqual(totalOutputTokens(summary), 70);
2722+
return yield* service.readSummary(WINDOW);
27242723
}).pipe(Effect.provide(layerService({ prefix: "usage-codex-archived", home, settings })));
2724+
assert.strictEqual(totalOutputTokens(complete), 70);
2725+
2726+
// A newer archive-only scan must not claim completeness over the active history.
2727+
yield* Effect.promise(() =>
2728+
NodeFSP.rm(NodePath.join(codexHome, "sessions"), { recursive: true }),
2729+
);
2730+
const archiveOnly = yield* Effect.gen(function* () {
2731+
return yield* (yield* UsageService.make).readSummary(WINDOW);
2732+
}).pipe(Effect.provide(layerService({ prefix: "usage-codex-archive-only", home, settings })));
2733+
assert.strictEqual(
2734+
archiveOnly.sources.find((source) => source.fingerprint.provider === "codex")?.status,
2735+
"partial",
2736+
);
2737+
const merged = mergeUsage(
2738+
[
2739+
{ environmentId: EnvironmentId.make("complete"), label: "complete", summary: complete },
2740+
{ environmentId: EnvironmentId.make("archive"), label: "archive", summary: archiveOnly },
2741+
],
2742+
USAGE_CONTRACT_VERSION,
2743+
);
2744+
assert.strictEqual(merged.outputTokens, 70);
27252745
}).pipe(Effect.scoped),
27262746
);
27272747

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ export const make = Effect.gen(function* () {
693693
return { path, records, malformedRecords };
694694
});
695695
const unread = failedPaths + read.filter((file) => file.failed).length;
696+
// The first root holds active transcripts; without it, other roots alone are not complete.
697+
const primaryMissing = listings[0] === null;
696698
yield* Effect.annotateCurrentSpan({
697699
"usage.files": files.length,
698700
"usage.files_parsed": read.filter((file) => file.update !== undefined).length,
@@ -708,7 +710,12 @@ export const make = Effect.gen(function* () {
708710
status: "partial",
709711
message: `${unread} transcript path(s) could not be read; usage may be incomplete.`,
710712
}
711-
: {}),
713+
: primaryMissing
714+
? {
715+
status: "partial",
716+
message: "Active transcript directory is missing; usage may be incomplete.",
717+
}
718+
: {}),
712719
} satisfies ScannedDir;
713720
});
714721

‎packages/provider-core/src/server/usage.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export type ProviderUsageReader<Config, R> =
114114
readonly directories: (instance: ProviderUsageInstance<Config>) => Effect.Effect<
115115
ReadonlyArray<{
116116
readonly dir: string;
117-
/** Transcript roots sharing the logical source identity in `dir`. Defaults to `dir`. */
117+
/** Transcript roots sharing the source identity in `dir`; the first is required. Defaults to `dir`. */
118118
readonly transcriptDirs?: readonly string[];
119119
readonly fileName?: string;
120120
}>,

0 commit comments

Comments
 (0)