Skip to content

Commit c3050c9

Browse files
committed
fix(server): late PATH rebuilds Codex only when its CLI cannot run
Auth, timeout, and other probe errors keep their instance, so a PATH update no longer closes their sessions.
1 parent ef78097 commit c3050c9

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

‎apps/server/src/provider/CodexProvider.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,15 @@ function accountProbeStatus({ account, rateLimits }: CodexAppServerProviderSnaps
732732
return { status: "ready", auth };
733733
}
734734

735+
const CODEX_PROBE_FAILED_PREFIX = "Codex app-server provider probe failed: ";
736+
737+
/** The CLI spawned but neither the probe nor `codex --version` ran, e.g. its launcher lacks node. */
738+
export const isCodexLaunchFailure = (provider: ServerProvider): boolean =>
739+
provider.installed &&
740+
provider.status === "error" &&
741+
provider.version === null &&
742+
provider.message?.startsWith(CODEX_PROBE_FAILED_PREFIX) === true;
743+
735744
export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(function* (
736745
codexSettings: CodexSettings,
737746
probe: (input: {
@@ -811,7 +820,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
811820
status: "error",
812821
auth: { status: "unknown" },
813822
message: installed
814-
? `Codex app-server provider probe failed: ${error.message}.`
823+
? `${CODEX_PROBE_FAILED_PREFIX}${error.message}.`
815824
: `Could not start Codex CLI (\`${codexSettings.binaryPath}\`). Check Settings → Providers → Codex → Binary path on the server.` +
816825
(codexSettings.binaryPath === "codex"
817826
? " Installing ChatGPT or Codex desktop may not add codex to PATH."

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,13 +1166,16 @@ it.layer(
11661166
"in-flight missing",
11671167
"in-flight installed",
11681168
"launcher",
1169+
"codex timeout",
11691170
"other error",
11701171
] as const)("recovers a copied driver environment after late PATH (%s)", (mode) =>
11711172
Effect.gen(function* () {
11721173
// "launcher": the Codex CLI is found on the early PATH, but its check fails until the
11731174
// late PATH brings the node its npm launcher runs. "other error" is the same failure on a
1174-
// driver whose errors may be auth or network, so it must not be rebuilt.
1175-
const failsEarly = mode === "launcher" || mode === "other error";
1175+
// driver whose errors may be auth or network, so it must not be rebuilt. "codex timeout" is a
1176+
// Codex error other than a launcher failure, which must not be rebuilt either.
1177+
const failsEarly =
1178+
mode === "launcher" || mode === "codex timeout" || mode === "other error";
11761179
const replay = mode === "replay" || failsEarly;
11771180
const host = { PATH: "/early/bin", T3CODE_CODEX_LAUNCH_ARGS: "hidden" };
11781181
const environment = providerHostEnvironment(
@@ -1188,7 +1191,9 @@ it.layer(
11881191
const finished = yield* Deferred.make<void>();
11891192
const paths: Array<string | undefined> = [];
11901193
let first = true;
1191-
const driverKind = ProviderDriverKind.make(mode === "launcher" ? "codex" : "pi");
1194+
const driverKind = ProviderDriverKind.make(
1195+
mode === "launcher" || mode === "codex timeout" ? "codex" : "pi",
1196+
);
11921197
const instanceId = ProviderInstanceId.make("late-path");
11931198
const driver: AnyProviderDriver = {
11941199
driverKind,
@@ -1241,6 +1246,14 @@ it.layer(
12411246
...snapshot,
12421247
installed,
12431248
status: ready ? ("ready" as const) : ("error" as const),
1249+
...(ready
1250+
? {}
1251+
: {
1252+
message:
1253+
mode === "codex timeout"
1254+
? "Timed out while checking Codex app-server provider status."
1255+
: "Codex app-server provider probe failed: transport failed.",
1256+
}),
12441257
};
12451258
yield* Ref.set(current, checked);
12461259
return checked;
@@ -1299,7 +1312,7 @@ it.layer(
12991312
(yield* registry.getProviders).find((row) => row.instanceId === instanceId)?.installed,
13001313
).toBe(true);
13011314
expect(paths).toEqual(
1302-
mode === "in-flight installed" || mode === "other error"
1315+
mode === "in-flight installed" || mode === "codex timeout" || mode === "other error"
13031316
? ["/early/bin"]
13041317
: ["/early/bin", "/early/bin:/late/bin"],
13051318
);

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
latestProviderVersionCacheKeys,
4848
} from "@t3tools/provider-core/server/maintenanceResolver";
4949
import type { ProviderSnapshotSource } from "./builtInProviderCatalog.ts";
50+
import { isCodexLaunchFailure } from "./CodexProvider.ts";
5051

5152
export type ProviderMaintenanceActionKind = "update";
5253

@@ -70,7 +71,7 @@ export class ProviderRegistry extends Context.Service<
7071
) => Effect.Effect<ReadonlyArray<ServerProvider>>;
7172

7273
/**
73-
* Rebuilds and re-checks enabled instances not yet found installed, plus failed Codex checks,
74+
* Rebuilds and re-checks enabled instances not yet found installed, plus Codex CLIs that cannot run,
7475
* after the host environment changed. Drivers copy the environment when built, so only a rebuild
7576
* sees a later PATH.
7677
*/
@@ -699,9 +700,9 @@ export const layer = Layer.effect(
699700
return yield* instanceId === undefined ? refresh() : refreshInstance(instanceId);
700701
});
701702

702-
// Instances whose check passed keep running; restarting them could end live sessions. A failed
703-
// Codex check is rebuilt too: its npm launcher spawns on the bare PATH but cannot find node.
704-
// Other drivers report auth and network failures as "error", and a rebuild would close sessions.
703+
// Instances whose check passed keep running; restarting them could end live sessions. A Codex
704+
// CLI that spawns but cannot run (its npm launcher lacks node) is rebuilt too. Auth, network,
705+
// and timeout errors are not, since a rebuild would close their sessions.
705706
const rebuildMissingAndRefresh = Effect.fn("rebuildMissingAndRefresh")(function* () {
706707
const instances = yield* instanceRegistry.listInstances;
707708
yield* refresh();
@@ -718,7 +719,7 @@ export const layer = Layer.effect(
718719
return (
719720
!snapshot.installed ||
720721
(instance.driverKind === ProviderDriverKind.make("codex") &&
721-
snapshot.status === "error")
722+
isCodexLaunchFailure(snapshot))
722723
);
723724
}),
724725
);

0 commit comments

Comments
 (0)