From 4da158cdd44f6cec1869d7d8fa15c6da3764c0ab Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 05:32:50 +0000 Subject: [PATCH 1/4] test(server): an unchanged local Codex is not re-probed after a restart Fails on main: every new CodexInstallation re-runs codex --version and codex app-server --help because the fingerprint cache is in memory only (#40142). Co-Authored-By: Claude Opus 5.5 --- .../src/provider/CodexInstallation.test.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/server/src/provider/CodexInstallation.test.ts b/apps/server/src/provider/CodexInstallation.test.ts index a000aacb103d3..8a273f93933c2 100644 --- a/apps/server/src/provider/CodexInstallation.test.ts +++ b/apps/server/src/provider/CodexInstallation.test.ts @@ -51,7 +51,8 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* ( const localDirectory = `${baseDir}/local`; const localBinaryPath = `${localDirectory}/codex${input.platform === "win32" ? ".exe" : ""}`; const probeLog = `${baseDir}/local-probes.txt`; - if (input.local) { + // A restarted harness on the same baseDir keeps the existing binary, and with it its fingerprint. + if (input.local && !(yield* fs.exists(localBinaryPath))) { yield* fs.makeDirectory(localDirectory, { recursive: true }); yield* fs.writeFileString( localBinaryPath, @@ -214,6 +215,21 @@ const compileNative = Effect.fn("test.compileNative")(function* ( expect(yield* compiler.exitCode).toBe(0); }, Effect.scoped); +it.effect("does not re-probe an unchanged local executable after a restart", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" }); + const first = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); + expect((yield* first.installation.resolve()).source).toBe("local"); + const restarted = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); + expect((yield* restarted.installation.resolve()).source).toBe("local"); + expect((yield* fs.readFileString(first.probeLog)).trim().split("\n")).toEqual([ + "--version", + "app-server --help", + ]); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), +); + it.effect("rechecks compatibility after the local executable is replaced", () => Effect.gen(function* () { const h = yield* makeHarness({ local: { version: "0.156.1" } }); From ef83cbb0b3302e3f33d3bedad90d6930487ce2ac Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 05:53:21 +0000 Subject: [PATCH 2/4] fix(server): reuse an unchanged local Codex's version check across restarts The local Codex fingerprint cache lived only in memory, so every server start ran `codex --version` and `codex app-server --help` again. A successful probe now persists its path, fingerprint and version under the caches directory and is reused while the executable is unchanged. Fixes #40142 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/provider/CodexInstallation.ts | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/apps/server/src/provider/CodexInstallation.ts b/apps/server/src/provider/CodexInstallation.ts index ec0514fba967d..0bc6c25245510 100644 --- a/apps/server/src/provider/CodexInstallation.ts +++ b/apps/server/src/provider/CodexInstallation.ts @@ -53,6 +53,11 @@ const PackageManifest = Schema.Struct({ target: Schema.String, entrypoint: Schema.String, }); +const LocalProbe = Schema.fromJsonString( + Schema.Struct({ executablePath: Schema.String, fingerprint: Schema.String, version: Version }), +); +const decodeLocalProbe = Schema.decodeEffect(LocalProbe); +const encodeLocalProbe = Schema.encodeEffect(LocalProbe); const decodeVersion = Schema.decodeUnknownEffect(Version); const decodeNpmLatest = Schema.decodeUnknownEffect(Schema.Struct({ version: Version })); const encodeRecord = Schema.encodeEffect(Schema.fromJsonString(InstalledRelease)); @@ -197,6 +202,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function : options.releaseAsset; const managedDirectory = path.join(options.baseDir, "tools", "codex"); const activePath = path.join(managedDirectory, "active.json"); + const localProbePath = path.join(options.baseDir, "caches", "codex-local-probe.json"); const executableName = platform === "win32" ? "codex.exe" : "codex"; const gate = yield* Semaphore.make(1); const updateCheckGate = yield* Semaphore.make(1); @@ -489,24 +495,42 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function const info = yield* fs.stat(executablePath); const fingerprint = `${realExecutablePath}:${info.size}:${Option.getOrUndefined(info.mtime)?.getTime()}:${Option.getOrUndefined(info.ino)}`; const cached = localCache.get(executablePath); + const local = (version: string) => + ({ + executablePath, + source: "local", + version, + managedVersionDirectory: null, + }) satisfies CodexExecutable; + // Only successful probes persist, so a slow or failed one is retried on the next start. + const persisted = fs.readFileString(localProbePath).pipe( + Effect.flatMap(decodeLocalProbe), + Effect.map((record) => + record.executablePath === executablePath && record.fingerprint === fingerprint + ? local(record.version) + : null, + ), + Effect.orElseSucceed(() => null), + ); + const probe = Effect.gen(function* () { + const output = yield* runCommand(executablePath, ["--version"]); + const version = /^codex-cli (\d+\.\d+\.\d+)$/u.exec(output.trim())?.[1]; + if (!version) return null; + yield* runCommand(executablePath, ["app-server", "--help"]); + yield* fs.makeDirectory(path.dirname(localProbePath), { recursive: true }).pipe( + Effect.andThen(encodeLocalProbe({ executablePath, fingerprint, version })), + Effect.flatMap((record) => fs.writeFileString(localProbePath, record)), + Effect.ignore, + ); + return local(version); + }).pipe( + Effect.timeout("5 seconds"), + Effect.orElseSucceed(() => null), + ); const executable = cached?.fingerprint === fingerprint ? cached.executable - : yield* Effect.gen(function* () { - const output = yield* runCommand(executablePath, ["--version"]); - const version = /^codex-cli (\d+\.\d+\.\d+)$/u.exec(output.trim())?.[1]; - if (!version) return null; - yield* runCommand(executablePath, ["app-server", "--help"]); - return { - executablePath, - source: "local", - version, - managedVersionDirectory: null, - } satisfies CodexExecutable; - }).pipe( - Effect.timeout("5 seconds"), - Effect.orElseSucceed(() => null), - ); + : ((yield* persisted) ?? (yield* probe)); localCache.set(executablePath, { fingerprint, executable }); if (!executable) return null; const advisory = yield* compatibility(executable.version); From ee956040355bb4a9602246039ad4330db93533a5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 06:47:05 +0000 Subject: [PATCH 3/4] fix(server): never persist a version-manager launcher's Codex probe mise, Volta and aqua-proxy switch the Codex they run without changing the launcher binary, so its fingerprint cannot show a version switch. Their probes stay in memory only and every start checks the selected Codex again. Co-Authored-By: Claude Opus 5.5 --- .../src/provider/CodexInstallation.test.ts | 3 +++ apps/server/src/provider/CodexInstallation.ts | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/server/src/provider/CodexInstallation.test.ts b/apps/server/src/provider/CodexInstallation.test.ts index 8a273f93933c2..4aecbc0f1dc48 100644 --- a/apps/server/src/provider/CodexInstallation.test.ts +++ b/apps/server/src/provider/CodexInstallation.test.ts @@ -327,6 +327,9 @@ int main(int argc, char **argv) { expect(yield* h.installation.start).toMatchObject({ source: "local", phase: "succeeded" }); expect((yield* h.installation.resolve()).executablePath).toBe(h.localBinaryPath); expect(h.downloads()).toBe(0); + expect(yield* h.fs.readFileString(`${h.baseDir}/caches/codex-local-probe.json`)).not.toContain( + launcher, + ); }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); diff --git a/apps/server/src/provider/CodexInstallation.ts b/apps/server/src/provider/CodexInstallation.ts index 0bc6c25245510..3ea1aad42d7af 100644 --- a/apps/server/src/provider/CodexInstallation.ts +++ b/apps/server/src/provider/CodexInstallation.ts @@ -432,11 +432,12 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function ); return output; }, Effect.scoped); + const isVersionManagerLauncher = (realExecutablePath: string) => + /^(?:volta-shim|mise|aqua-proxy)(?:\.exe)?$/iu.test(path.basename(realExecutablePath)); // Native Codex needs its adjacent host; launchers resolve their own payloads. const hasCodeModeHost = Effect.fn("CodexInstallation.hasCodeModeHost")( function* (realExecutablePath: string) { - if (/^(?:volta-shim|mise|aqua-proxy)(?:\.exe)?$/iu.test(path.basename(realExecutablePath))) - return true; + if (isVersionManagerLauncher(realExecutablePath)) return true; if (platform === "win32") { if (path.extname(realExecutablePath).toLowerCase() !== ".exe") return true; for (const extension of [".bunx", ".shim"]) { @@ -503,6 +504,8 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function managedVersionDirectory: null, }) satisfies CodexExecutable; // Only successful probes persist, so a slow or failed one is retried on the next start. + // Version managers switch Codex without touching the launcher, so their probes never persist. + const persists = !isVersionManagerLauncher(realExecutablePath); const persisted = fs.readFileString(localProbePath).pipe( Effect.flatMap(decodeLocalProbe), Effect.map((record) => @@ -517,11 +520,12 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function const version = /^codex-cli (\d+\.\d+\.\d+)$/u.exec(output.trim())?.[1]; if (!version) return null; yield* runCommand(executablePath, ["app-server", "--help"]); - yield* fs.makeDirectory(path.dirname(localProbePath), { recursive: true }).pipe( - Effect.andThen(encodeLocalProbe({ executablePath, fingerprint, version })), - Effect.flatMap((record) => fs.writeFileString(localProbePath, record)), - Effect.ignore, - ); + if (persists) + yield* fs.makeDirectory(path.dirname(localProbePath), { recursive: true }).pipe( + Effect.andThen(encodeLocalProbe({ executablePath, fingerprint, version })), + Effect.flatMap((record) => fs.writeFileString(localProbePath, record)), + Effect.ignore, + ); return local(version); }).pipe( Effect.timeout("5 seconds"), From 9b43a27ff8222c8db3cc9cae16017a1e70e3dc6c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 07:10:41 +0000 Subject: [PATCH 4/4] fix(server): persist a local Codex probe only when its payload is identified A script launcher can keep its own file unchanged while the Codex it runs changes, so its fingerprint could reuse a stale version after a restart. Only a native binary, or an npm-installed @openai/codex whose package version joins the fingerprint, now persists its probe. Every other launcher is probed again on each start, as before. Co-Authored-By: Claude Opus 5.5 --- .../src/provider/CodexInstallation.test.ts | 64 ++++++++++++++----- apps/server/src/provider/CodexInstallation.ts | 53 ++++++++++----- 2 files changed, 86 insertions(+), 31 deletions(-) diff --git a/apps/server/src/provider/CodexInstallation.test.ts b/apps/server/src/provider/CodexInstallation.test.ts index 4aecbc0f1dc48..9ff5c95e806b5 100644 --- a/apps/server/src/provider/CodexInstallation.test.ts +++ b/apps/server/src/provider/CodexInstallation.test.ts @@ -215,19 +215,53 @@ const compileNative = Effect.fn("test.compileNative")(function* ( expect(yield* compiler.exitCode).toBe(0); }, Effect.scoped); -it.effect("does not re-probe an unchanged local executable after a restart", () => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" }); - const first = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); - expect((yield* first.installation.resolve()).source).toBe("local"); - const restarted = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); - expect((yield* restarted.installation.resolve()).source).toBe("local"); - expect((yield* fs.readFileString(first.probeLog)).trim().split("\n")).toEqual([ - "--version", - "app-server --help", - ]); - }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), +it.effect.each([ + { layout: "npm bin symlink", persists: true }, + { layout: "npm shim", persists: true }, + { layout: "wrapper script", persists: false }, +])( + "re-probes a $layout Codex after a restart only when its payload can change", + ({ layout, persists }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" }); + const packageDirectory = + layout === "npm shim" + ? `${baseDir}/local/node_modules/@openai/codex` + : `${baseDir}/npm/lib/node_modules/@openai/codex`; + const payload = + layout === "wrapper script" ? `${baseDir}/payload` : `${packageDirectory}/bin/codex.js`; + const install = Effect.fn("test.installPayload")(function* (version: string) { + yield* fs.makeDirectory(`${packageDirectory}/bin`, { recursive: true }); + if (layout !== "wrapper script") + yield* fs.writeFileString( + `${packageDirectory}/package.json`, + JSON.stringify({ name: "@openai/codex", version }), + ); + yield* fs.writeFileString( + payload, + `#!/bin/sh\nprintf '%s\\n' "$*" >> '${baseDir}/local-probes.txt'\ncase "$1" in --version) printf 'codex-cli ${version}\\n';; esac\n`, + { mode: 0o755 }, + ); + }); + yield* install("0.156.1"); + yield* fs.makeDirectory(`${baseDir}/local`, { recursive: true }); + if (layout === "npm bin symlink") yield* fs.symlink(payload, `${baseDir}/local/codex`); + else + yield* fs.writeFileString(`${baseDir}/local/codex`, `#!/bin/sh\nexec '${payload}' "$@"\n`, { + mode: 0o755, + }); + const first = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); + expect((yield* first.installation.resolve()).source).toBe("local"); + const restarted = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); + expect((yield* restarted.installation.resolve()).source).toBe("local"); + expect((yield* fs.readFileString(first.probeLog)).trim().split("\n")).toHaveLength( + persists ? 2 : 4, + ); + yield* install("0.128.9"); + const upgraded = yield* makeHarness({ baseDir, local: { version: "0.156.1" } }); + expect((yield* upgraded.installation.state).installedVersion).toBeNull(); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); it.effect("rechecks compatibility after the local executable is replaced", () => @@ -327,9 +361,7 @@ int main(int argc, char **argv) { expect(yield* h.installation.start).toMatchObject({ source: "local", phase: "succeeded" }); expect((yield* h.installation.resolve()).executablePath).toBe(h.localBinaryPath); expect(h.downloads()).toBe(0); - expect(yield* h.fs.readFileString(`${h.baseDir}/caches/codex-local-probe.json`)).not.toContain( - launcher, - ); + expect(yield* h.fs.exists(`${h.baseDir}/caches/codex-local-probe.json`)).toBe(false); }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); diff --git a/apps/server/src/provider/CodexInstallation.ts b/apps/server/src/provider/CodexInstallation.ts index 3ea1aad42d7af..7985e0d3bc914 100644 --- a/apps/server/src/provider/CodexInstallation.ts +++ b/apps/server/src/provider/CodexInstallation.ts @@ -58,6 +58,11 @@ const LocalProbe = Schema.fromJsonString( ); const decodeLocalProbe = Schema.decodeEffect(LocalProbe); const encodeLocalProbe = Schema.encodeEffect(LocalProbe); +const decodeNpmCodexPackage = Schema.decodeEffect( + Schema.fromJsonString( + Schema.Struct({ name: Schema.Literal("@openai/codex"), version: Schema.String }), + ), +); const decodeVersion = Schema.decodeUnknownEffect(Version); const decodeNpmLatest = Schema.decodeUnknownEffect(Schema.Struct({ version: Version })); const encodeRecord = Schema.encodeEffect(Schema.fromJsonString(InstalledRelease)); @@ -432,14 +437,13 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function ); return output; }, Effect.scoped); - const isVersionManagerLauncher = (realExecutablePath: string) => - /^(?:volta-shim|mise|aqua-proxy)(?:\.exe)?$/iu.test(path.basename(realExecutablePath)); // Native Codex needs its adjacent host; launchers resolve their own payloads. - const hasCodeModeHost = Effect.fn("CodexInstallation.hasCodeModeHost")( + const localKind = Effect.fn("CodexInstallation.localKind")( function* (realExecutablePath: string) { - if (isVersionManagerLauncher(realExecutablePath)) return true; + if (/^(?:volta-shim|mise|aqua-proxy)(?:\.exe)?$/iu.test(path.basename(realExecutablePath))) + return "launcher"; if (platform === "win32") { - if (path.extname(realExecutablePath).toLowerCase() !== ".exe") return true; + if (path.extname(realExecutablePath).toLowerCase() !== ".exe") return "launcher"; for (const extension of [".bunx", ".shim"]) { const marker = yield* fs .stat(realExecutablePath.slice(0, -4) + extension) @@ -449,7 +453,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function marker.value.type === "File" && Number(marker.value.size) > 0 ) - return true; + return "launcher"; } } else { const header = yield* fs.open(realExecutablePath, { flag: "r" }).pipe( @@ -457,7 +461,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function Effect.orElseSucceed(() => Option.none()), ); if (Option.isSome(header) && header.value[0] === 0x23 && header.value[1] === 0x21) - return true; + return "launcher"; } const info = yield* fs.stat( path.join( @@ -465,15 +469,31 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function `codex-code-mode-host${platform === "win32" ? ".exe" : ""}`, ), ); - return ( - info.type === "File" && + return info.type === "File" && Number(info.size) > 0 && (platform === "win32" || (info.mode & 0o111) !== 0) - ); + ? "native" + : null; }, Effect.scoped, - Effect.orElseSucceed(() => false), + Effect.orElseSucceed(() => null), ); + // npm's shim (Windows) or bin symlink (POSIX) stays the same across upgrades; its package does not. + const npmCodexVersion = Effect.fn("CodexInstallation.npmCodexVersion")(function* ( + executablePath: string, + realExecutablePath: string, + ) { + for (const manifest of [ + path.join(path.dirname(executablePath), "node_modules", "@openai", "codex", "package.json"), + path.join(path.dirname(realExecutablePath), "..", "package.json"), + ]) { + const pkg = yield* fs + .readFileString(manifest) + .pipe(Effect.flatMap(decodeNpmCodexPackage), Effect.option); + if (Option.isSome(pkg)) return pkg.value.version; + } + return null; + }); const localCache = new Map(); const resolveLocal = Effect.fn("CodexInstallation.resolveLocal")( function* () { @@ -492,9 +512,14 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function ) ) return null; - if (!(yield* hasCodeModeHost(realExecutablePath))) return null; + const kind = yield* localKind(realExecutablePath); + if (kind === null) return null; + // A launcher's own file cannot show a Codex switch, so only a native binary or npm's package persists. + const npmVersion = + kind === "launcher" ? yield* npmCodexVersion(executablePath, realExecutablePath) : null; + const persists = kind === "native" || npmVersion !== null; const info = yield* fs.stat(executablePath); - const fingerprint = `${realExecutablePath}:${info.size}:${Option.getOrUndefined(info.mtime)?.getTime()}:${Option.getOrUndefined(info.ino)}`; + const fingerprint = `${realExecutablePath}:${info.size}:${Option.getOrUndefined(info.mtime)?.getTime()}:${Option.getOrUndefined(info.ino)}:${npmVersion ?? ""}`; const cached = localCache.get(executablePath); const local = (version: string) => ({ @@ -504,8 +529,6 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function managedVersionDirectory: null, }) satisfies CodexExecutable; // Only successful probes persist, so a slow or failed one is retried on the next start. - // Version managers switch Codex without touching the launcher, so their probes never persist. - const persists = !isVersionManagerLauncher(realExecutablePath); const persisted = fs.readFileString(localProbePath).pipe( Effect.flatMap(decodeLocalProbe), Effect.map((record) =>