Skip to content

Commit ba84ca8

Browse files
committed
fix(server): re-probe Codex when its auth.json changes
Signing in again with `codex login` (or out with `codex logout`) rewrites the instance's auth.json outside T3, but nothing noticed, so the provider card kept the previous auth error until the next health interval (5 minutes by default) or a manual Retry. Watch the instance's auth home (the shadow home when one is set, otherwise the shared home) and run the same forced refresh Retry uses, debounced, when auth.json changes. The watcher lives in the instance scope. Fixes #34295
1 parent 52d621a commit ba84ca8

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎apps/server/src/provider/Drivers/CodexDriver.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as FileSystem from "effect/FileSystem";
1717
import * as Layer from "effect/Layer";
1818
import * as Option from "effect/Option";
1919
import * as PlatformError from "effect/PlatformError";
20+
import * as Queue from "effect/Queue";
2021
import * as Schema from "effect/Schema";
2122
import * as Sink from "effect/Sink";
2223
import * as Stream from "effect/Stream";
@@ -726,3 +727,53 @@ it.layer(layerTest)("CodexDriver", (it) => {
726727
{ skip: windowsHost },
727728
);
728729
});
730+
731+
// Live: the auth watcher debounces on the real clock.
732+
it.live("probes again when auth.json in the Codex home changes", () =>
733+
Effect.gen(function* () {
734+
const fs = yield* FileSystem.FileSystem;
735+
const codexHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-auth-watch-" });
736+
const watchedPath = yield* Deferred.make<string>();
737+
const homeEvents = yield* Queue.unbounded<FileSystem.WatchEvent>();
738+
const probes = yield* Queue.unbounded<void>();
739+
yield* CodexDriver.create({
740+
instanceId: ProviderInstanceId.make("codex-auth-watch"),
741+
displayName: "Codex auth watch",
742+
enabled: true,
743+
environment: [],
744+
config: { ...CodexDriver.defaultConfig(), homePath: codexHome },
745+
}).pipe(
746+
// OS watchers can miss events written right after they start, so the test supplies them.
747+
Effect.provideService(FileSystem.FileSystem, {
748+
...fs,
749+
watch: (path) =>
750+
Stream.unwrap(
751+
Deferred.succeed(watchedPath, path).pipe(Effect.as(Stream.fromQueue(homeEvents))),
752+
),
753+
}),
754+
Effect.provideService(
755+
ChildProcessSpawner.ChildProcessSpawner,
756+
ChildProcessSpawner.make((command) =>
757+
(ChildProcess.isStandardCommand(command) && command.args.includes("app-server")
758+
? Queue.offer(probes, undefined)
759+
: Effect.void
760+
).pipe(
761+
Effect.andThen(
762+
PlatformError.badArgument({
763+
module: "ChildProcessSpawner",
764+
method: "spawn",
765+
description: "The fixture has no Codex executable",
766+
}),
767+
),
768+
),
769+
),
770+
),
771+
);
772+
expect(yield* Deferred.await(watchedPath)).toBe(codexHome);
773+
yield* Queue.take(probes);
774+
775+
yield* Queue.offer(homeEvents, { _tag: "Update", path: "auth.json" });
776+
777+
yield* Queue.take(probes);
778+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
779+
);

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { CodexSettings, ProviderDriverKind } from "@t3tools/contracts";
22
import * as Digest from "effect/crypto/Digest";
33
import * as SecureRandom from "effect/crypto/SecureRandom";
44
import type * as Signature from "effect/crypto/Signature";
5+
import * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
67
import * as FileSystem from "effect/FileSystem";
78
import * as Path from "effect/Path";
89
import * as Schema from "effect/Schema";
10+
import * as Stream from "effect/Stream";
911
import { HttpClient } from "effect/http";
1012
import { ChildProcessSpawner } from "effect/process";
1113

@@ -371,6 +373,17 @@ export const CodexDriver: ProviderDriver<
371373
),
372374
);
373375

376+
// `codex login` and `codex logout` rewrite auth.json outside T3.
377+
if (effectiveConfig.enabled) {
378+
yield* fileSystem.watch(accountKey).pipe(
379+
Stream.filter((event) => pathService.basename(event.path) === "auth.json"),
380+
Stream.debounce(Duration.millis(250)),
381+
Stream.runForEach(() => snapshot.refresh.pipe(Effect.ignoreCause({ log: true }))),
382+
Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
383+
Effect.ignoreCause({ log: true }),
384+
Effect.forkScoped,
385+
);
386+
}
374387
return {
375388
instanceId,
376389
driverKind: DRIVER_KIND,

0 commit comments

Comments
 (0)