Skip to content

Commit ea7f47c

Browse files
committed
fix(usage): unify Codex archive source ownership
1 parent 2c18ed0 commit ea7f47c

9 files changed

Lines changed: 450 additions & 112 deletions

File tree

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { EnvironmentId, USAGE_CONTRACT_VERSION } from "@t3tools/contracts";
44
import { type RouteProp, useIsFocused, useNavigation, useRoute } from "@react-navigation/native";
55
import { cursorKeychainAccessEnvironments } from "@t3tools/client-runtime/state/usage";
66
import {
7-
isCompatibleUsageContractVersion,
7+
getUsageContractMismatch,
88
isModelCostUnknown,
99
type DailyTotals,
1010
type MergedUsage,
@@ -846,16 +846,11 @@ function isUsageLoading(environment: EnvironmentUsageStatus) {
846846
}
847847

848848
function usageEnvironmentStatus(environment: EnvironmentUsageStatus): string {
849-
if (
850-
environment.summary &&
851-
!isCompatibleUsageContractVersion(environment.summary.contractVersion, USAGE_CONTRACT_VERSION)
852-
) {
853-
return formatUsageContractMismatch(environment.label, {
854-
direction:
855-
environment.summary.contractVersion < USAGE_CONTRACT_VERSION
856-
? "serverBehind"
857-
: "clientBehind",
858-
});
849+
const mismatch = environment.summary
850+
? getUsageContractMismatch(environment.summary, USAGE_CONTRACT_VERSION)
851+
: null;
852+
if (mismatch !== null) {
853+
return formatUsageContractMismatch(environment.label, mismatch);
859854
}
860855
if (!environment.isConnected)
861856
return environment.summary ? "Disconnected · showing saved usage" : "Waiting for connection…";

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

Lines changed: 175 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,10 @@ describe("UsageService", () => {
583583
);
584584
const summary = yield* service.readSummary(WINDOW);
585585
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);
586+
const canonicalHome = yield* Effect.promise(() => NodeFSP.realpath(codexHome));
587+
const codexBucket = summary.buckets.find((bucket) => bucket.sourcePath === canonicalHome);
588+
assert.strictEqual(codexBucket?.totals.outputTokens, 51);
589+
assert.strictEqual(codexBucket?.costUsd, 51);
592590
yield* Effect.promise(() =>
593591
NodeFSP.rename(
594592
NodePath.join(codexHome, "sessions", "rollout.jsonl"),
@@ -612,18 +610,186 @@ describe("UsageService", () => {
612610
assert.deepStrictEqual(removed.buckets, summary.buckets);
613611

614612
const sources = summary.sources.filter((source) => source.status === "ok");
615-
assert.strictEqual(sources.length, 5);
613+
assert.strictEqual(sources.length, 4);
616614
assert.strictEqual(
617615
sources.reduce((sum, source) => sum + source.scannedFiles, 0),
618616
6,
619617
);
620618
assert.strictEqual(
621619
sources.filter((source) => source.fingerprint.provider === "codex").length,
622-
2,
620+
1,
623621
);
624622
}).pipe(Effect.scoped),
625623
);
626624

625+
it.live.each([
626+
{ removeSessionsRoot: false, label: "a rollout move" },
627+
{ removeSessionsRoot: true, label: "sessions root cleanup" },
628+
])(
629+
"merges archived usage once across independent caches after $label",
630+
({ removeSessionsRoot }) =>
631+
Effect.gen(function* () {
632+
const { home, settings } = yield* setup;
633+
const codexHome = NodePath.join(home, "codex");
634+
const alias = NodePath.join(home, "codex-alias");
635+
const sessions = NodePath.join(codexHome, "sessions");
636+
const archives = NodePath.join(codexHome, "archived_sessions");
637+
yield* Effect.promise(async () => {
638+
await NodeFSP.mkdir(sessions, { recursive: true });
639+
await NodeFSP.symlink(codexHome, alias, "junction");
640+
await NodeFSP.writeFile(
641+
NodePath.join(sessions, "rollout.jsonl"),
642+
[
643+
{ type: "session_meta", payload: { id: "shared-archived-session" } },
644+
{ type: "turn_context", payload: { model: "gpt-5.6-sol" } },
645+
{
646+
type: "event_msg",
647+
timestamp: "2026-08-01T10:00:00Z",
648+
payload: {
649+
type: "token_count",
650+
info: { last_token_usage: { input_tokens: 10, output_tokens: 11 } },
651+
},
652+
},
653+
]
654+
.map((line) => encodeUnknownJsonString(line))
655+
.join("\n") + "\n",
656+
);
657+
});
658+
const warm = yield* UsageService.make.pipe(
659+
Effect.provide(
660+
serviceLayers({
661+
prefix: "usage-service-archive-warm-test",
662+
home,
663+
settings,
664+
ratesDocument: {
665+
"gpt-5.6-sol": { input_cost_per_token: 0, output_cost_per_token: 1 },
666+
},
667+
}),
668+
),
669+
);
670+
const active = yield* warm.readSummary(WINDOW);
671+
assert.strictEqual(totalOutputTokens(active), 11);
672+
const legacyVolume = yield* Effect.promise(() => NodeFSP.stat(sessions));
673+
yield* Effect.promise(async () => {
674+
await NodeFSP.mkdir(archives);
675+
await NodeFSP.rename(
676+
NodePath.join(sessions, "rollout.jsonl"),
677+
NodePath.join(archives, "rollout.jsonl"),
678+
);
679+
if (removeSessionsRoot) await NodeFSP.rm(sessions, { recursive: true });
680+
});
681+
const fresh = yield* UsageService.make.pipe(
682+
Effect.provide(
683+
serviceLayers({
684+
prefix: "usage-service-archive-fresh-test",
685+
home,
686+
settings: { providers: { ...settings.providers, codex: { homePath: alias } } },
687+
ratesDocument: {
688+
"gpt-5.6-sol": { input_cost_per_token: 0, output_cost_per_token: 1 },
689+
},
690+
}),
691+
),
692+
);
693+
const archived = yield* fresh.readSummary(WINDOW);
694+
const retained = yield* warm.readSummary(WINDOW);
695+
assert.strictEqual(totalOutputTokens(archived), 11);
696+
assert.strictEqual(totalOutputTokens(retained), 11);
697+
const activeSource = active.sources.find(
698+
(source) => source.fingerprint.provider === "codex",
699+
);
700+
assert.deepStrictEqual(
701+
archived.sources.find((source) => source.fingerprint.provider === "codex")?.fingerprint,
702+
activeSource?.fingerprint,
703+
);
704+
assert.deepStrictEqual(
705+
retained.sources.find((source) => source.fingerprint.provider === "codex")?.fingerprint,
706+
activeSource?.fingerprint,
707+
);
708+
// A v6 environment keeps the active-root attribution in its cache even
709+
// after archival and root cleanup. There is no old inode left to stat.
710+
const legacy = {
711+
...active,
712+
contractVersion: 6,
713+
buckets: active.buckets.map((bucket) =>
714+
bucket.provider === "codex" ? { ...bucket, sourcePath: sessions } : bucket,
715+
),
716+
sources: active.sources.map((source) =>
717+
source.fingerprint.provider === "codex"
718+
? {
719+
...source,
720+
fingerprint: {
721+
...source.fingerprint,
722+
resolvedHomePath: sessions,
723+
volumeId: `${legacyVolume.dev}:${legacyVolume.ino}`,
724+
},
725+
}
726+
: source,
727+
),
728+
};
729+
for (const freshIsNewer of [false, true]) {
730+
const merged = mergeUsage(
731+
[
732+
{
733+
environmentId: EnvironmentId.make("warm"),
734+
label: "warm",
735+
summary: {
736+
...retained,
737+
readAt: freshIsNewer ? "2026-08-01T11:00:00Z" : "2026-08-01T12:00:00Z",
738+
},
739+
},
740+
{
741+
environmentId: EnvironmentId.make("fresh"),
742+
label: "fresh",
743+
summary: {
744+
...archived,
745+
readAt: freshIsNewer ? "2026-08-01T12:00:00Z" : "2026-08-01T11:00:00Z",
746+
},
747+
},
748+
],
749+
archived.contractVersion,
750+
);
751+
assert.strictEqual(merged.outputTokens, 11);
752+
assert.strictEqual(merged.totalTokens, 21);
753+
assert.strictEqual(merged.costUsd, 11);
754+
assert.strictEqual(merged.records, 1);
755+
assert.strictEqual(merged.sessions, 1);
756+
const mixed = mergeUsage(
757+
[
758+
{
759+
environmentId: EnvironmentId.make("legacy"),
760+
label: "legacy",
761+
summary: {
762+
...legacy,
763+
readAt: freshIsNewer ? "2026-08-01T11:00:00Z" : "2026-08-01T12:00:00Z",
764+
},
765+
},
766+
{
767+
environmentId: EnvironmentId.make("fresh"),
768+
label: "fresh",
769+
summary: {
770+
...archived,
771+
readAt: freshIsNewer ? "2026-08-01T12:00:00Z" : "2026-08-01T11:00:00Z",
772+
},
773+
},
774+
],
775+
archived.contractVersion,
776+
);
777+
assert.strictEqual(mixed.outputTokens, 11);
778+
assert.strictEqual(mixed.costUsd, 11);
779+
assert.strictEqual(mixed.records, 1);
780+
assert.strictEqual(mixed.sessions, 1);
781+
assert.deepStrictEqual(mixed.contractMismatches, [
782+
{
783+
environmentId: EnvironmentId.make("legacy"),
784+
direction: "serverBehind",
785+
contractVersion: 6,
786+
provider: "codex",
787+
},
788+
]);
789+
}
790+
}).pipe(Effect.scoped),
791+
);
792+
627793
it.live(
628794
"uses explicit account settings before environment and legacy homes, then refreshes them",
629795
() =>
@@ -740,7 +906,7 @@ describe("UsageService", () => {
740906
assert.strictEqual(
741907
summary.sources.find((source) => source.fingerprint.provider === "codex")?.fingerprint
742908
.resolvedHomePath,
743-
NodePath.join(home, "inherited-codex", "sessions"),
909+
NodePath.join(home, "inherited-codex"),
744910
);
745911
assert.strictEqual(
746912
summary.sources.find((source) => source.fingerprint.provider === "grok")?.fingerprint

0 commit comments

Comments
 (0)