Skip to content

Commit 2597756

Browse files
fix(server): re-probe Codex when its auth.json changes (pingdotgg#34320)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 96a2f0a commit 2597756

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ 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";
24+
import * as TestClock from "effect/testing/TestClock";
2325
import { HttpClient, HttpClientResponse } from "effect/http";
2426
import * as ChildProcess from "effect/process/ChildProcess";
2527
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
@@ -725,4 +727,54 @@ it.layer(layerTest)("CodexDriver", (it) => {
725727
}).pipe(Effect.scoped),
726728
{ skip: windowsHost },
727729
);
730+
731+
it.effect("probes again when auth.json in the Codex home changes", () =>
732+
Effect.gen(function* () {
733+
const fs = yield* FileSystem.FileSystem;
734+
const codexHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-auth-watch-" });
735+
const watchedPath = yield* Deferred.make<string>();
736+
const homeEvents = yield* Queue.unbounded<FileSystem.WatchEvent>();
737+
const probes = yield* Queue.unbounded<void>();
738+
yield* CodexDriver.create({
739+
instanceId: ProviderInstanceId.make("codex-auth-watch"),
740+
displayName: "Codex auth watch",
741+
enabled: true,
742+
environment: [],
743+
config: { ...CodexDriver.defaultConfig(), homePath: codexHome },
744+
}).pipe(
745+
// OS watchers can miss events written right after they start, so the test supplies them.
746+
Effect.provideService(FileSystem.FileSystem, {
747+
...fs,
748+
watch: (path) =>
749+
Stream.unwrap(
750+
Deferred.succeed(watchedPath, path).pipe(Effect.as(Stream.fromQueue(homeEvents))),
751+
),
752+
}),
753+
Effect.provideService(
754+
ChildProcessSpawner.ChildProcessSpawner,
755+
ChildProcessSpawner.make((command) =>
756+
(ChildProcess.isStandardCommand(command) && command.args.includes("app-server")
757+
? Queue.offer(probes, undefined)
758+
: Effect.void
759+
).pipe(
760+
Effect.andThen(
761+
PlatformError.badArgument({
762+
module: "ChildProcessSpawner",
763+
method: "spawn",
764+
description: "The fixture has no Codex executable",
765+
}),
766+
),
767+
),
768+
),
769+
),
770+
);
771+
expect(yield* Deferred.await(watchedPath)).toBe(codexHome);
772+
yield* Queue.take(probes);
773+
774+
yield* Queue.offer(homeEvents, { _tag: "Update", path: "auth.json" });
775+
yield* TestClock.adjust("250 millis");
776+
777+
yield* Queue.take(probes);
778+
}).pipe(Effect.scoped),
779+
);
728780
});

‎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)