@@ -142,6 +142,7 @@ const layerService = (input: {
142142 ) => Effect.Effect<Response, HttpClientError.HttpClientError>;
143143 readonly environment?: NodeJS.ProcessEnv;
144144 readonly platform?: NodeJS.Platform;
145+ readonly hostname?: string;
145146}) =>
146147 layerCursorUsageAccounts(
147148 CursorAccountReader.layer.pipe(Layer.provide(CursorKeychain.layer)),
@@ -157,6 +158,7 @@ const layerService = (input: {
157158 ),
158159 Layer.provideMerge(NodeServices.layer),
159160 Layer.provideMerge(Layer.succeed(HostProcess.Platform, input.platform ?? "linux")),
161+ Layer.provideMerge(Layer.succeed(HostProcess.Hostname, input.hostname ?? "usage-test-host")),
160162 Layer.provideMerge(ServerSettings.layerTest(input.settings)),
161163 Layer.provideMerge(
162164 Layer.succeed(
@@ -465,6 +467,93 @@ describe("Codex usage history", () => {
465467});
466468
467469describe("UsageService", () => {
470+ it.live("keeps the first host identity across recreation and explicit override removal", () =>
471+ Effect.gen(function* () {
472+ const { transcript, settings, home } = yield* setup;
473+ yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
474+ for (const [hostname, override, expected] of [
475+ ["container-a", undefined, "container-a"],
476+ ["container-b", undefined, "container-a"],
477+ ["container-c", "physical-host", "physical-host"],
478+ ["container-d", undefined, "container-a"],
479+ ] as const) {
480+ const summary = yield* Effect.gen(function* () {
481+ const service = yield* UsageService.make;
482+ return yield* service.readSummary(WINDOW);
483+ }).pipe(
484+ Effect.scoped,
485+ Effect.provide(
486+ layerService({
487+ prefix: "usage-host-id",
488+ home,
489+ settings,
490+ hostname,
491+ environment: override === undefined ? {} : { T3CODE_HOST_ID: override },
492+ }),
493+ ),
494+ );
495+ const source = summary.sources.find((source) => source.fingerprint.provider === "claude");
496+ assert.strictEqual(source?.fingerprint.hostId, expected);
497+ }
498+ }).pipe(Effect.scoped),
499+ );
500+
501+ it.live("concurrent initialization keeps one host identity across service recreation", () =>
502+ Effect.gen(function* () {
503+ const { transcript, settings, home } = yield* setup;
504+ yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
505+ const prefix = "usage-host-id-race";
506+ const hostIdPath = NodePath.join(home, prefix, "usage-host-id");
507+ const bothRead = yield* Deferred.make<void>();
508+ let reads = 0;
509+ const summaries = yield* Effect.forEach(
510+ ["container-a", "container-b"],
511+ (hostname) =>
512+ Effect.gen(function* () {
513+ const fileSystem = yield* FileSystem.FileSystem;
514+ let firstRead = true;
515+ const service = yield* UsageService.make.pipe(
516+ Effect.provideService(FileSystem.FileSystem, {
517+ ...fileSystem,
518+ readFileString: (path, encoding) =>
519+ Effect.gen(function* () {
520+ if (path !== hostIdPath || !firstRead) {
521+ return yield* fileSystem.readFileString(path, encoding);
522+ }
523+ firstRead = false;
524+ // Both instances observe the missing file before either can create it.
525+ const result = yield* Effect.exit(fileSystem.readFileString(path, encoding));
526+ reads += 1;
527+ if (reads === 2) yield* Deferred.succeed(bothRead, undefined);
528+ yield* Deferred.await(bothRead);
529+ return yield* result;
530+ }),
531+ }),
532+ );
533+ return yield* service.readSummary(WINDOW);
534+ }).pipe(
535+ Effect.scoped,
536+ Effect.provide(layerService({ prefix, home, settings, hostname })),
537+ ),
538+ { concurrency: "unbounded" },
539+ );
540+ const persisted = yield* Effect.promise(() => NodeFSP.readFile(hostIdPath, "utf8"));
541+ const hostId = persisted.trim();
542+ assert.include(["container-a", "container-b"], hostId);
543+ const restarted = yield* Effect.gen(function* () {
544+ const service = yield* UsageService.make;
545+ return yield* service.readSummary(WINDOW);
546+ }).pipe(
547+ Effect.scoped,
548+ Effect.provide(layerService({ prefix, home, settings, hostname: "container-c" })),
549+ );
550+ for (const summary of [...summaries, restarted]) {
551+ const source = summary.sources.find((source) => source.fingerprint.provider === "claude");
552+ assert.strictEqual(source?.fingerprint.hostId, hostId);
553+ }
554+ }).pipe(Effect.scoped),
555+ );
556+
468557 it.live(
469558 "repairs initial Codex prices after unchanged-file retries, persists evidence and corrections, and honors later native tiers",
470559 () =>
0 commit comments