TypeScript
fix(server): update shared Codex installs from their owning home (#25235)
mariaf4147ddf7948
diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.tsindex 90e37cc6671..0aacca303e6 100644--- a/apps/server/src/provider/Drivers/CodexDriver.test.ts+++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts@@ -377,6 +377,107 @@ it.layer(layerTest)("CodexDriver", (it) => {),);+ it.effect.skipIf(windowsHost)(+ "runs the standalone updater against the home that holds a shared install",+ () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const tempDir = yield* fs+ .makeTempDirectoryScoped({ prefix: "t3-codex-driver-" })+ .pipe(Effect.flatMap((directory) => fs.realPath(directory)));+ // One standalone install, linked onto PATH and used by two account homes.+ // `İ` gets longer when lowercased, so the home must not be sliced from a+ // lowercased copy of the path.+ const installHome = NodePath.join(tempDir, "codex-İnstall");+ const realBinaryPath = NodePath.join(+ installHome,+ "packages",+ "standalone",+ "releases",+ "0.162.1-x86_64-unknown-linux-musl",+ "bin",+ "codex",+ );+ const binaryPath = NodePath.join(tempDir, "bin", "codex");+ yield* fs.makeDirectory(NodePath.dirname(realBinaryPath), { recursive: true });+ yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });+ yield* fs.writeFileString(realBinaryPath, "#!/bin/sh\n");+ yield* fs.chmod(realBinaryPath, 0o755);+ yield* fs.symlink(realBinaryPath, binaryPath);++ const updates = yield* Effect.forEach(["codex-p", "codex-vm"], (account) =>+ Effect.gen(function* () {+ const homePath = NodePath.join(tempDir, `home-${account}`);+ yield* fs.makeDirectory(homePath, { recursive: true });+ const instance = yield* CodexDriver.create({+ instanceId: ProviderInstanceId.make(account),+ displayName: account,+ enabled: false,+ environment: [],+ config: { ...CodexDriver.defaultConfig(), binaryPath, homePath },+ });+ return (yield* instance.snapshot.resolveMaintenance()).update;+ }),+ );++ for (const update of updates) {+ expect(update).toMatchObject({+ command: `${binaryPath} update`,+ args: ["update"],+ lockKey: "codex-native",+ env: { CODEX_HOME: installHome },+ });+ }+ }).pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),+ Effect.scoped,+ ),+ );++ it.effect.skipIf(windowsHost)("derives the standalone update home from either command path", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const tempDir = yield* fs+ .makeTempDirectoryScoped({ prefix: "t3-codex-update-home-" })+ .pipe(Effect.flatMap((directory) => fs.realPath(directory)));+ const linkedInstallHome = NodePath.join(tempDir, "install");+ const fixtures = [+ { binary: "codex", realPath: "/packages/standalone/bin/codex", home: "/" },+ { binary: "codex", realPath: "C:\\Packages\\Standalone\\bin\\codex.exe", home: "C:\\" },+ // The standalone tree itself is a symlink, so only the resolved path names its home.+ {+ binary: NodePath.join("install", "packages", "standalone", "bin", "codex"),+ realPath: "/disk/codex/releases/0.163.0/bin/codex",+ home: linkedInstallHome,+ },+ ];++ for (const fixture of fixtures) {+ const binaryPath = NodePath.join(tempDir, fixture.binary);+ yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });+ yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");+ yield* fs.chmod(binaryPath, 0o755);+ const instance = yield* CodexDriver.create({+ instanceId: ProviderInstanceId.make("codex-update-home"),+ displayName: "Codex update home",+ enabled: false,+ environment: [],+ config: { ...CodexDriver.defaultConfig(), binaryPath, homePath: tempDir },+ }).pipe(+ // Virtualize only the executable target: these trees do not belong to this test.+ Effect.provideService(FileSystem.FileSystem, {+ ...fs,+ realPath: (path) =>+ path === binaryPath ? Effect.succeed(fixture.realPath) : fs.realPath(path),+ }),+ );+ expect((yield* instance.snapshot.resolveMaintenance()).update?.env).toEqual({+ CODEX_HOME: fixture.home,+ });+ }+ }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn), Effect.scoped),+ );+for (const fixture of [{name: "leaves mise npm-backend installations manual-only",diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.tsindex 2d078cf3259..bbd910cf5d4 100644--- a/apps/server/src/provider/Drivers/CodexDriver.ts+++ b/apps/server/src/provider/Drivers/CodexDriver.ts@@ -37,7 +37,6 @@ import {enrichProviderSnapshotWithVersionAdvisory,makeCachedProviderMaintenanceResolution,makePackageManagedProviderMaintenanceResolver,- normalizeCommandPath,resolveProviderMaintenanceCapabilitiesEffect,} from "@t3tools/provider-core/server/maintenanceResolver";import {@@ -60,20 +59,36 @@ import * as HostProcess from "@t3tools/shared/HostProcess";const decodeCodexSettings = Schema.decodeSync(CodexSettings);const DRIVER_KIND = ProviderDriverKind.make("codex");-function isCodexStandaloneCommandPath(commandPath: string): boolean {- return normalizeCommandPath(commandPath).includes("/packages/standalone/");+const CODEX_STANDALONE_HOME_PATTERN = /^(.*)\/packages\/standalone\//is;++function codexStandaloneHome(commandPath: string): string | null {+ // Slash replacement preserves indices; Unicode lowercasing can change the path length.+ const match = CODEX_STANDALONE_HOME_PATTERN.exec(commandPath.replaceAll("\\", "/"));+ if (!match) {+ return null;+ }+ const home = match[1]!;+ // Keep root separators: `C:` is drive-relative, and an empty home is not `/`.+ const isRoot = home === "" || /^[a-z]:$/i.test(home);+ return commandPath.slice(0, home.length + (isRoot ? 1 : 0));}-/** `codex update` owns the shared home's standalone tree; auth-overlay homes do not carry it. */+// Standalone updates locate their install through CODEX_HOME. Account and auth-overlay+// homes can share an executable, so only the updater uses the executable's owning home.function makeCodexMaintenanceResolver(sharedHomePath: string) {return makePackageManagedProviderMaintenanceResolver({provider: DRIVER_KIND,npmPackageName: "@openai/codex",nativeUpdate: {args: ["update"],- isCommandPath: isCodexStandaloneCommandPath,+ isCommandPath: (commandPath) => codexStandaloneHome(commandPath) !== null,requiresCommandPath: true,- env: { CODEX_HOME: sharedHomePath },+ env: (context) => ({+ CODEX_HOME:+ codexStandaloneHome(context.realCommandPath) ??+ codexStandaloneHome(context.resolvedCommandPath) ??+ sharedHomePath,+ }),},});}diff --git a/packages/provider-core/src/server/maintenanceResolver.ts b/packages/provider-core/src/server/maintenanceResolver.tsindex 2e3aad166c5..8a8e11e2ec0 100644--- a/packages/provider-core/src/server/maintenanceResolver.ts+++ b/packages/provider-core/src/server/maintenanceResolver.ts@@ -87,7 +87,9 @@ export interface PackageManagedProviderMaintenanceDefinition {readonly isCommandPath?: (commandPath: string) => boolean;/** The updater cannot detect installs outside `isCommandPath`, so leave those manual-only. */readonly requiresCommandPath?: boolean;- readonly env?: NodeJS.ProcessEnv;+ readonly env?:+ | NodeJS.ProcessEnv+ | ((context: ProviderMaintenanceResolutionContext) => NodeJS.ProcessEnv);} | null;}@@ -446,6 +448,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(}const nativeUpdate = definition.nativeUpdate;+ const nativeEnv = nativeUpdate?.env;const native = nativeUpdate? makeProviderMaintenanceCapabilities({provider: definition.provider,@@ -454,7 +457,9 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(updateArgs: nativeUpdate.args,updateLockKey: `${definition.provider}-native`,platform: context.platform,- ...(nativeUpdate.env ? { env: nativeUpdate.env } : {}),+ ...(nativeEnv+ ? { env: typeof nativeEnv === "function" ? nativeEnv(context) : nativeEnv }+ : {}),}): manual;// A native-looking launcher can still delegate to a mise-owned install.