diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index fdd7c22e6ecca..327b5a0547aea 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1779,6 +1779,7 @@ export const layerAppServerClientFactory: Layer.Layer< command: input.settings.binaryPath || "codex", args: codexAppServerArgs( resolveCodexLaunchArgs(input.settings.launchArgs, input.environment), + input.environment, ), env: environment, threadId: input.threadId, @@ -1846,17 +1847,28 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter const serverConfig = yield* ServerConfig; const homeLayout = yield* resolveCodexHomeLayout(config); - yield* materializeCodexShadowHome(homeLayout).pipe( - Effect.mapError( - (cause) => - new ProviderAdapterDriverCreateError({ - driver: CODEX_DRIVER_KIND, - instanceId, - detail: "Failed to materialize the Codex shadow home.", - cause, - }), - ), + const environmentOverrides = yield* mergeProviderInstanceEnvironment( + environment, + hostEnvironment, ); + const processEnv = + hooks.resolveRuntime === undefined + ? yield* materializeCodexShadowHome( + homeLayout, + environmentOverrides, + resolveCodexLaunchArgs(config.launchArgs, environmentOverrides), + ).pipe( + Effect.mapError( + (cause) => + new ProviderAdapterDriverCreateError({ + driver: CODEX_DRIVER_KIND, + instanceId, + detail: "Failed to prepare the Codex shadow home.", + cause, + }), + ), + ) + : environmentOverrides; const settings = { ...config, @@ -1868,7 +1880,7 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter return yield* makeCodexAdapterV2({ instanceId, settings, - environment: yield* mergeProviderInstanceEnvironment(environment, hostEnvironment), + environment: processEnv, clientFactory, digest, fileSystem, diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 2c697f8fa8531..6d8fbed6cdf18 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -139,7 +139,7 @@ it.live.each( assert.isUndefined(effective.environment.OPENAI_API_KEY); assert.isUndefined(effective.environment.OPENAI_BASE_URL); assert.isUndefined(effective.environment.T3CODE_CODEX_LAUNCH_ARGS); - const args = codexAppServerArgs(effective.config.launchArgs); + const args = codexAppServerArgs(effective.config.launchArgs, effective.environment); assert.include(args, 'model_provider="openai_token_sharing"'); const provider = args.find((arg) => arg.startsWith("model_providers.openai_token_sharing="), diff --git a/apps/server/src/provider/CodexManagedRuntime.ts b/apps/server/src/provider/CodexManagedRuntime.ts index 3704c919c2791..2f9292409ee6a 100644 --- a/apps/server/src/provider/CodexManagedRuntime.ts +++ b/apps/server/src/provider/CodexManagedRuntime.ts @@ -91,7 +91,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func environmentId, profileKey, ]); - yield* materializeCodexShadowHome(homeLayout).pipe( + const homeEnvironment = yield* materializeCodexShadowHome(homeLayout, options.environment).pipe( Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), Effect.mapError( @@ -100,6 +100,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func instanceId: options.instanceId, operation: "runtime", detail: cause.message, + cause, }), ), ); @@ -115,7 +116,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func ); // Ambient CLI overrides cannot redirect a T3-owned token to a different provider. const environment: NodeJS.ProcessEnv = { - ...options.environment, + ...homeEnvironment, CODEX_HOME: homePath, }; delete environment.ACCESS_TOKEN; diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 63154f4e99499..80c0bff5e51c1 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -649,7 +649,7 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu const environment = yield* codexProcessEnvironment(input); const spawnCommand = yield* resolveSpawnCommand( input.binaryPath, - codexAppServerArgs(input.launchArgs), + codexAppServerArgs(input.launchArgs, input.environment), { env: environment, extendEnv: true }, ); const child = yield* spawner diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index 03245ba46d6c1..7ed0cebabb8ee 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -185,11 +185,11 @@ export const CodexDriver: ProviderDriver< const installation = yield* CodexInstallation.CodexInstallation; const modelCatalog = yield* ModelCatalog.ModelCatalog; const currentCatalog = modelCatalog.current(DRIVER_KIND); - const processEnv = yield* mergeProviderInstanceEnvironment( + const environmentOverrides = yield* mergeProviderInstanceEnvironment( environment, yield* HostProcess.Environment, ); - const environmentHome = processEnv.CODEX_HOME; + const environmentHome = environmentOverrides.CODEX_HOME; const homeConfig = !config.homePath.trim() && environmentHome?.trim() ? { ...config, homePath: environmentHome } @@ -203,7 +203,11 @@ export const CodexDriver: ProviderDriver< accentColor, continuationGroupKey: continuationIdentity.continuationKey, }); - yield* materializeCodexShadowHome(homeLayout).pipe( + const processEnv = yield* materializeCodexShadowHome( + homeLayout, + environmentOverrides, + resolveCodexLaunchArgs(config.launchArgs, environmentOverrides), + ).pipe( Effect.mapError( (cause) => new ProviderDriverError({ diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 2bbcea20faa91..784c62be9356a 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -4,14 +4,18 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; +import * as NodeSqlite from "node:sqlite"; import { CodexSettings } from "@t3tools/contracts"; import { CodexShadowHomeEntryConflictError, CodexShadowHomePathConflictError, + CodexShadowHomeSqliteConflictError, + codexHomeEnvironment, materializeCodexShadowHome, resolveCodexHomeLayout, } from "./CodexHomeLayout.ts"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings); @@ -38,6 +42,24 @@ const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* yield* fileSystem.writeFileString(filePath, contents); }); +const makeShadowLayout = Effect.fn("CodexHomeLayout.test.makeShadowLayout")(function* () { + const fileSystem = yield* FileSystem.FileSystem; + // macOS hands out temp directories through a link, and Windows routing names real paths. + const sharedHome = yield* fileSystem.realPath(yield* makeTempDir("t3code-codex-shared-")); + const shadowHome = yield* fileSystem.realPath(yield* makeTempDir("t3code-codex-shadow-")); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + return { sharedHome, shadowHome, layout }; +}); + +/** What a launch gets for a SQLite home: on Windows, T3 also pins it as a Codex argument. */ +const routedTo = Effect.fn("CodexHomeLayout.test.routedTo")(function* (sqliteHome: string) { + return (yield* HostProcess.isWindows) + ? { CODEX_SQLITE_HOME: sqliteHome, T3CODE_CODEX_SQLITE_HOME: sqliteHome } + : { CODEX_SQLITE_HOME: sqliteHome }; +}); + it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { describe("resolveCodexHomeLayout", () => { it.effect("uses the shared home for continuation and the shadow home for runtime", () => @@ -83,15 +105,15 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata"); yield* fileSystem.remove(shadowMetadata); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.exists(shadowMetadata)).toBe(false); yield* writeTextFile(shadowMetadata, "shadow Finder metadata"); - yield* materializeCodexShadowHome(layout); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.readFileString(sharedMetadata)).toBe("shared Finder metadata"); expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata"); @@ -137,7 +159,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions")); const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml")); @@ -187,7 +209,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.readLink(shadowLock)).toBe(sharedLock); }), @@ -215,7 +237,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); const locksTarget = yield* fileSystem.readLink(shadowLocks); const sharedLockExists = yield* fileSystem.exists( @@ -250,11 +272,11 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.exists(threadLock)).toBe(true); yield* fileSystem.remove(threadLock); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.readLink(shadowLocks)).toBe( path.join(sharedHome, "thread-writer-locks"), ); @@ -287,7 +309,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml")); const logLinkResult = yield* fileSystem @@ -317,7 +339,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); expect(error).toMatchObject({ @@ -348,7 +370,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError); expect(error).toMatchObject({ @@ -364,4 +386,515 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); }); + + describe("SQLite routing", () => { + it.effect.skipIf(!symlinksSupported)( + "keeps SQLite out of a fresh shadow and routes it to the shared home", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "db"); + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite-wal"), "wal"); + + const environment = yield* materializeCodexShadowHome(layout, { KEEP: "1" }); + + expect(environment).toEqual({ KEEP: "1", ...(yield* routedTo(sharedHome)) }); + const shadowEntries = yield* fileSystem.readDirectory(shadowHome); + expect(shadowEntries.filter((entry) => entry.includes(".sqlite"))).toEqual([]); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "accepts legacy SQLite links with empty or index-only sidecars", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + // What Windows leaves behind: the shared Codex has sidecars of its own, and SQLite put an + // index and an empty WAL beside each link. + const databases = [ + "goals_1.sqlite", + "memories_1.sqlite", + "memories_v2_1.sqlite", + "state_5.sqlite", + ]; + for (const database of databases) { + yield* writeTextFile(path.join(sharedHome, database), "db"); + yield* writeTextFile(path.join(sharedHome, `${database}-wal`), "shared wal"); + yield* writeTextFile(path.join(sharedHome, `${database}-shm`), "shared index"); + yield* fileSystem.symlink( + path.join(sharedHome, database), + path.join(shadowHome, database), + ); + yield* writeTextFile(path.join(shadowHome, `${database}-wal`), ""); + yield* writeTextFile(path.join(shadowHome, `${database}-shm`), "index"); + } + + const environment = yield* materializeCodexShadowHome(layout, {}); + + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + for (const database of databases) { + expect(yield* fileSystem.readLink(path.join(shadowHome, database))).toBe( + path.join(sharedHome, database), + ); + expect(yield* fileSystem.readFileString(path.join(shadowHome, `${database}-wal`))).toBe( + "", + ); + expect(yield* fileSystem.readFileString(path.join(shadowHome, `${database}-shm`))).toBe( + "index", + ); + } + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + it.effect.skipIf(!symlinksSupported)( + "refuses a shadow WAL holding data for a linked database without touching it", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + const shadowWal = path.join(shadowHome, "state_5.sqlite-wal"); + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "db"); + yield* fileSystem.symlink( + path.join(sharedHome, "state_5.sqlite"), + path.join(shadowHome, "state_5.sqlite"), + ); + yield* writeTextFile(shadowWal, "uncheckpointed frames"); + + const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: "state_5.sqlite-wal" }); + expect(yield* fileSystem.readFileString(shadowWal)).toBe("uncheckpointed frames"); + expect(yield* fileSystem.exists(path.join(shadowHome, "sessions"))).toBe(false); + // Windows resolves a rooted path onto the working directory's drive. + expect( + yield* materializeCodexShadowHome(layout, { CODEX_SQLITE_HOME: " /elsewhere " }), + ).toEqual(yield* routedTo(path.resolve("/elsewhere"))); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "keeps a shadow's own databases on POSIX and refuses only one the shared home also has", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private db"); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), "private wal"); + + expect((yield* codexHomeEnvironment(layout, {})).CODEX_SQLITE_HOME).toBe(shadowHome); + + yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "shared db"); + yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite-wal"), "shared wal"); + const environment = yield* materializeCodexShadowHome(layout, {}); + expect(environment.CODEX_SQLITE_HOME).toBe(shadowHome); + expect(yield* fileSystem.readLink(path.join(shadowHome, "goals_1.sqlite"))).toBe( + path.join(sharedHome, "goals_1.sqlite"), + ); + expect(yield* fileSystem.exists(path.join(shadowHome, "goals_1.sqlite-wal"))).toBe(false); + expect(yield* fileSystem.readFileString(path.join(shadowHome, "state_5.sqlite"))).toBe( + "private db", + ); + // POSIX SQLite follows the link itself, so naming this shadow outright stays allowed. + expect( + (yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: shadowHome })) + .CODEX_SQLITE_HOME, + ).toBe(shadowHome); + + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "shared db"); + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + }).pipe(Effect.provideService(HostProcess.Platform, "linux")), + ); + + // regression: #24486 a Windows shadow with its own state_5.sqlite was routed to itself with the + // shared goals_1.sqlite linked in, so it and a shared-home Codex each kept a WAL for goals. + it.effect.skipIf(!symlinksSupported)( + "never lets a Windows shadow reach a shared database through a link", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + const sharedGoals = path.join(sharedHome, "goals_1.sqlite"); + const shadowGoals = path.join(shadowHome, "goals_1.sqlite"); + const openDatabase = (databasePath: string) => + Effect.acquireRelease( + Effect.sync(() => new NodeSqlite.DatabaseSync(databasePath)), + (database) => Effect.sync(() => database.close()), + ); + const goalEntries = (home: string) => + fileSystem + .readDirectory(home) + .pipe( + Effect.map((entries) => + entries.filter((entry) => entry.startsWith("goals_1.sqlite")).toSorted(), + ), + ); + // One helper prepares the shadow and the other only resolves; they must agree. + const launch = (environment: NodeJS.ProcessEnv) => + Effect.all([ + materializeCodexShadowHome(layout, environment).pipe(Effect.result), + codexHomeEnvironment(layout, environment).pipe(Effect.result), + ]); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private db"); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), "private wal"); + + // With no shared databases there is nothing to link, so the shadow keeps its own. + expect((yield* materializeCodexShadowHome(layout, {})).CODEX_SQLITE_HOME).toBe( + shadowHome, + ); + + // A Codex running in the shared home holds goals open with a commit still in its WAL. + const sharedWriter = yield* openDatabase(sharedGoals); + sharedWriter.exec( + "PRAGMA journal_mode=WAL; PRAGMA wal_autocheckpoint=0; CREATE TABLE goals(id); INSERT INTO goals VALUES (1);", + ); + + for (const refused of yield* launch({})) { + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { + _tag: "CodexShadowHomeSqliteConflictError", + sharedHomePath: sharedHome, + effectiveHomePath: shadowHome, + entryName: "state_5.sqlite", + sharedEntryName: "goals_1.sqlite", + }, + }); + } + expect(yield* goalEntries(shadowHome)).toEqual([]); + // The shadow may be named outright while it holds only databases of its own. + for (const kept of yield* launch({ CODEX_SQLITE_HOME: shadowHome })) { + expect(kept).toMatchObject({ success: { CODEX_SQLITE_HOME: shadowHome } }); + } + expect(yield* goalEntries(shadowHome)).toEqual([]); + + // A build with the old fallback may already have left the link it made. + yield* fileSystem.symlink(sharedGoals, shadowGoals); + const shadowAlias = path.join(yield* makeTempDir("t3code-codex-alias-"), "shadow"); + yield* fileSystem.symlink(shadowHome, shadowAlias); + for (const environment of [ + {}, + { CODEX_SQLITE_HOME: shadowHome }, + { CODEX_SQLITE_HOME: shadowAlias }, + ]) { + for (const refused of yield* launch(environment)) { + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { _tag: "CodexShadowHomeSqliteConflictError" }, + }); + } + } + expect( + (yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: shadowHome }).pipe( + Effect.flip, + )).message, + ).toContain(`'${shadowHome}', where 'goals_1.sqlite' is a link`); + + // Naming the shared home is the way through: both writers then open one path, so one WAL. + const routed = yield* materializeCodexShadowHome(layout, { + CODEX_SQLITE_HOME: sharedHome, + }); + expect(routed).toEqual({ + CODEX_SQLITE_HOME: sharedHome, + T3CODE_CODEX_SQLITE_HOME: sharedHome, + }); + expect(yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: sharedHome })).toEqual( + routed, + ); + const routedGoals = path.join(routed.CODEX_SQLITE_HOME ?? "", "goals_1.sqlite"); + expect(routedGoals).toBe(sharedGoals); + const shadowWriter = yield* openDatabase(routedGoals); + shadowWriter.exec("INSERT INTO goals VALUES (2);"); + const count = "SELECT count(*) AS count FROM goals"; + expect(shadowWriter.prepare(count).get()?.count).toBe(2); + expect(sharedWriter.prepare(count).get()?.count).toBe(2); + expect(yield* goalEntries(sharedHome)).toEqual([ + "goals_1.sqlite", + "goals_1.sqlite-shm", + "goals_1.sqlite-wal", + ]); + expect(yield* goalEntries(shadowHome)).toEqual(["goals_1.sqlite"]); + expect(yield* fileSystem.readFileString(path.join(shadowHome, "state_5.sqlite"))).toBe( + "private db", + ); + expect( + yield* fileSystem.readFileString(path.join(shadowHome, "state_5.sqlite-wal")), + ).toBe("private wal"); + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + it.effect.skipIf(!symlinksSupported)( + "refuses on Windows a shadow that keeps its own database beside a dangling legacy link", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private db"); + yield* fileSystem.symlink( + path.join(sharedHome, "goals_1.sqlite"), + path.join(shadowHome, "goals_1.sqlite"), + ); + + const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); + + expect(error).toMatchObject({ + _tag: "CodexShadowHomeSqliteConflictError", + entryName: "state_5.sqlite", + sharedEntryName: "goals_1.sqlite", + }); + expect(yield* fileSystem.exists(path.join(sharedHome, "goals_1.sqlite"))).toBe(false); + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + it.effect.skipIf(!symlinksSupported)( + "does not link a database the shared home gains after the Windows check", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private db"); + let checked = false; + const environment = yield* materializeCodexShadowHome(layout, {}).pipe( + Effect.provideService(FileSystem.FileSystem, { + ...fileSystem, + readDirectory: (directory, options) => + directory === sharedHome && !checked + ? fileSystem.readDirectory(directory, options).pipe( + Effect.tap(() => { + checked = true; + return fileSystem.writeFileString( + path.join(sharedHome, "goals_1.sqlite"), + "shared db", + ); + }), + ) + : fileSystem.readDirectory(directory, options), + }), + ); + + expect(checked).toBe(true); + expect(environment.CODEX_SQLITE_HOME).toBe(shadowHome); + expect(yield* fileSystem.exists(path.join(shadowHome, "goals_1.sqlite"))).toBe(false); + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + // regression: #24486 Codex ranks sqlite_home above CODEX_SQLITE_HOME, so a shadow whose config or + // launch args named it still opened the linked database while T3 reported the shared home. + it.effect.skipIf(!symlinksSupported)( + "pins the Windows SQLite home and refuses a sqlite_home setting that would outrank it", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + const sharedConfig = path.join(sharedHome, "config.toml"); + const shadowConfig = path.join(shadowHome, "config.toml"); + yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "db"); + yield* fileSystem.symlink( + path.join(sharedHome, "goals_1.sqlite"), + path.join(shadowHome, "goals_1.sqlite"), + ); + const launch = (launchArgs?: string) => + Effect.all([ + materializeCodexShadowHome(layout, {}, launchArgs).pipe(Effect.result), + codexHomeEnvironment(layout, {}, launchArgs).pipe(Effect.result), + ]); + + for (const launched of yield* launch("--strict-config")) { + expect(launched).toMatchObject({ + success: { CODEX_SQLITE_HOME: sharedHome, T3CODE_CODEX_SQLITE_HOME: sharedHome }, + }); + } + for (const launchArgs of [ + `-c sqlite_home="${shadowHome}"`, + `--config=sqlite_home='${shadowHome}'`, + `-csqlite_home=${shadowHome}`, + ]) { + for (const refused of yield* launch(launchArgs)) { + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { _tag: "CodexShadowHomeSqliteSettingError", setting: "launchArgs" }, + }); + } + } + + yield* writeTextFile(shadowConfig, `model = "gpt"\nsqlite_home = "${shadowHome}"\n`); + for (const refused of yield* launch()) { + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { + _tag: "CodexShadowHomeSqliteSettingError", + setting: "configFile", + value: shadowConfig, + }, + }); + } + // POSIX SQLite opens a link by its target, so Codex's own precedence is safe there. + expect( + yield* codexHomeEnvironment(layout, {}).pipe( + Effect.provideService(HostProcess.Platform, "linux"), + ), + ).toEqual({ CODEX_SQLITE_HOME: sharedHome }); + + // A fresh shadow links the shared config in, so that file counts before the link exists. + yield* fileSystem.remove(shadowConfig); + yield* writeTextFile( + sharedConfig, + `x = true\n "sqlite_home"='${shadowHome}'\n[features]\ny = true\n`, + ); + for (const refused of yield* launch()) { + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { setting: "configFile", value: sharedConfig }, + }); + } + expect(yield* fileSystem.exists(shadowConfig)).toBe(false); + + yield* writeTextFile(sharedConfig, `# sqlite_home = "${shadowHome}"\nmodel = "gpt"\n`); + for (const launched of yield* launch()) { + expect(launched).toMatchObject({ success: { CODEX_SQLITE_HOME: sharedHome } }); + } + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + // regression: #24486 the check matched any `sqlite_home =` text, so Windows refused a shadow whose + // MCP server had a variable of that name, or whose instructions mentioned the setting. + it.effect.skipIf(!symlinksSupported)( + "launches a Windows shadow whose sqlite_home is not Codex's own setting", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "db"); + yield* fileSystem.symlink( + path.join(sharedHome, "goals_1.sqlite"), + path.join(shadowHome, "goals_1.sqlite"), + ); + const expectLaunch = Effect.fn(function* (launchArgs?: string) { + for (const run of [materializeCodexShadowHome, codexHomeEnvironment]) { + expect(yield* run(layout, {}, launchArgs).pipe(Effect.result)).toMatchObject({ + success: { CODEX_SQLITE_HOME: sharedHome, T3CODE_CODEX_SQLITE_HOME: sharedHome }, + }); + } + }); + + const shadowConfig = path.join(shadowHome, "config.toml"); + yield* writeTextFile( + shadowConfig, + 'developer_instructions = """\nKeep this text.\nsqlite_home = "somewhere"\n"""\nmodel = "gpt"\n', + ); + yield* expectLaunch(); + yield* expectLaunch(`-c 'mcp_servers.example.env.sqlite_home="MCP-only"'`); + yield* expectLaunch(`--config 'developer_instructions="Leave sqlite_home=unset"'`); + + yield* fileSystem.remove(shadowConfig); + yield* writeTextFile( + path.join(sharedHome, "config.toml"), + '[mcp_servers.example]\ncommand = "echo"\n[mcp_servers.example.env]\nsqlite_home = "an MCP-only value"\n', + ); + yield* expectLaunch(); + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + // regression: #24486 a relative CODEX_SQLITE_HOME skipped the Windows check, and each launch + // directory resolved it differently. + it.effect.skipIf(!symlinksSupported)("refuses a relative Windows CODEX_SQLITE_HOME", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const { layout } = yield* makeShadowLayout(); + // Written out, not derived from the temp shadow: on Windows a temp directory on another + // drive has no path relative to the working directory. + const relativeShadow = path.join("codex", "shadow"); + + for (const run of [materializeCodexShadowHome, codexHomeEnvironment]) { + expect( + yield* run(layout, { CODEX_SQLITE_HOME: relativeShadow }).pipe(Effect.flip), + ).toMatchObject({ + _tag: "CodexShadowHomeSqliteSettingError", + setting: "relativeEnvironment", + value: relativeShadow, + }); + } + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + // regression: #24486 only the exact spelling of the shared home skipped the link check, so an + // alias of it was inspected, and every spelling was passed to Codex as written. + it.effect.skipIf(!symlinksSupported)( + "treats every spelling of the shared home as the shared home on Windows", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + // Left unset, T3 routes to the shared home without inspecting what it holds. + const elsewhere = path.join( + yield* makeTempDir("t3code-codex-elsewhere-"), + "goals.sqlite", + ); + yield* writeTextFile(elsewhere, "db"); + yield* fileSystem.symlink(elsewhere, path.join(sharedHome, "goals_1.sqlite")); + const alias = path.join(yield* makeTempDir("t3code-codex-alias-"), "shared"); + yield* fileSystem.symlink(sharedHome, alias); + + for (const spelling of [alias, `${sharedHome}${path.sep}`, sharedHome.toUpperCase()]) { + for (const run of [materializeCodexShadowHome, codexHomeEnvironment]) { + expect((yield* run(layout, { CODEX_SQLITE_HOME: spelling })).CODEX_SQLITE_HOME).toBe( + sharedHome, + ); + } + } + // Any other directory is named by its real path, however the override reached it. + const shadowAlias = path.join(yield* makeTempDir("t3code-codex-alias-"), "shadow"); + yield* fileSystem.symlink(shadowHome, shadowAlias); + expect( + (yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: shadowAlias })) + .CODEX_SQLITE_HOME, + ).toBe(shadowHome); + }).pipe(Effect.provideService(HostProcess.Platform, "win32")), + ); + + it.effect.skipIf(!symlinksSupported || HostProcess.Platform.defaultValue() === "win32")( + "keeps one WAL for a shared database a POSIX shadow opens through its link", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), ""); + const sharedDb = new NodeSqlite.DatabaseSync(path.join(sharedHome, "goals_1.sqlite")); + sharedDb.exec( + "PRAGMA journal_mode=WAL; PRAGMA wal_autocheckpoint=0; CREATE TABLE goals(id); INSERT INTO goals VALUES (1);", + ); + + const environment = yield* materializeCodexShadowHome(layout, {}); + const sqliteHome = environment.CODEX_SQLITE_HOME ?? ""; + expect(sqliteHome).toBe(shadowHome); + const shadowDb = new NodeSqlite.DatabaseSync(path.join(sqliteHome, "goals_1.sqlite")); + shadowDb.exec("INSERT INTO goals VALUES (2);"); + const shadowCount = shadowDb.prepare("SELECT count(*) AS count FROM goals").get(); + const sharedCount = sharedDb.prepare("SELECT count(*) AS count FROM goals").get(); + const shadowEntries = yield* fileSystem.readDirectory(shadowHome); + shadowDb.close(); + sharedDb.close(); + + expect(shadowCount?.count).toBe(2); + expect(sharedCount?.count).toBe(2); + expect(shadowEntries.filter((entry) => entry.startsWith("goals_1.sqlite"))).toEqual([ + "goals_1.sqlite", + ]); + }).pipe(Effect.provideService(HostProcess.Platform, "linux")), + ); + }); }); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index f6d2ba9d44cf5..26b7d4f35353b 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -4,10 +4,17 @@ import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as PlatformError from "effect/PlatformError"; +import * as Option from "effect/Option"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { + CODEX_PINNED_SQLITE_HOME_ENV, + codexLaunchArgSettingSqliteHome, +} from "../codexLaunchArgs.ts"; +import { codexConfigSetsSqliteHome } from "../codexSqliteHomeSetting.ts"; + export interface CodexHomeLayout { readonly mode: "direct" | "authOverlay"; readonly sharedHomePath: string; @@ -33,6 +40,9 @@ const KNOWN_SHARED_DIRECTORIES = [ const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "auth.lock", "models_cache.json"]); // Finder maintains metadata separately for each directory, including shadow homes. const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp", ".DS_Store"]); +const SQLITE_ENTRY_NAME = /\.sqlite(?:-(?:shm|wal|journal))?$/iu; +const SQLITE_DATABASE_ENTRY_NAME = /\.sqlite$/iu; +const SQLITE_DATA_SIDECAR_ENTRY_NAME = /^(.*\.sqlite)-(?:wal|journal)$/iu; // Shadow homes may create local runtime locks first; replace them with shared links because they guard shared state. const REPLACEABLE_SHARED_RUNTIME_ENTRIES = new Set([ "mcp-oauth-locks", @@ -80,7 +90,14 @@ export class CodexShadowHomeFileSystemError extends Schema.TaggedError()( + "CodexShadowHomeSqliteConflictError", + { + ...CodexShadowHomeContext, + entryName: Schema.String, + /** Set when the shadow's own databases cannot be combined with this shared one on Windows. */ + sharedEntryName: Schema.optional(Schema.String), + /** Set when an explicit `CODEX_SQLITE_HOME` holds the database as a link on Windows. */ + sqliteHomePath: Schema.optional(Schema.String), + }, +) { + override get message(): string { + if (this.sqliteHomePath !== undefined) { + return `CODEX_SQLITE_HOME points at '${this.sqliteHomePath}', where '${this.entryName}' is a link to a database kept elsewhere. On Windows Codex would give it a WAL of its own, apart from the one other Codex processes use for that database. Stop every Codex process using that directory. Then point CODEX_SQLITE_HOME at the directory that holds the database, or unset it to use the shared home '${this.sharedHomePath}', and retry. T3 did not change any SQLite files.`; + } + if (this.sharedEntryName !== undefined) { + return `Codex shadow home '${this.effectiveHomePath}' has its own SQLite database '${this.entryName}', while '${this.sharedEntryName}' belongs to the shared home '${this.sharedHomePath}'. On Windows Codex cannot use both without splitting a database from its WAL. Stop every Codex process using either home and back up both. Then either move the shadow's .sqlite files, with their -wal and -shm files, into the shared home to keep them, or move them out of the shadow home, and retry. T3 did not change any SQLite files.`; + } + return `Codex shadow home '${this.effectiveHomePath}' holds SQLite data in '${this.entryName}' that the shared home '${this.sharedHomePath}' may not have. Stop every Codex process using either home, back up both, then move that file out of the shadow home and retry. T3 did not change any SQLite files.`; + } +} + +/** A setting T3 cannot honour while keeping a Windows shadow's databases in one directory. */ +class CodexShadowHomeSqliteSettingError extends Schema.TaggedError()( + "CodexShadowHomeSqliteSettingError", + { + ...CodexShadowHomeContext, + setting: Schema.Literals(["relativeEnvironment", "configFile", "launchArgs"]), + /** The relative path, the config file, or the launch arg. */ + value: Schema.String, + }, +) { + override get message(): string { + const why = + "On Windows every Codex process sharing a database must open it through one directory"; + if (this.setting === "relativeEnvironment") { + return `CODEX_SQLITE_HOME is the relative path '${this.value}', which Codex resolves against each session's folder. ${why}. Set CODEX_SQLITE_HOME to an absolute path, or unset it to use the shared home '${this.sharedHomePath}', and retry. T3 did not change any SQLite files.`; + } + const where = + this.setting === "configFile" + ? `'${this.value}' sets sqlite_home` + : `The Codex launch arguments set sqlite_home ('${this.value}')`; + return `${where}, which Codex ranks above CODEX_SQLITE_HOME. ${why}, and T3 checks that directory only when CODEX_SQLITE_HOME names it. Remove the sqlite_home setting used by the shadow home '${this.effectiveHomePath}' and retry. To keep its directory, set CODEX_SQLITE_HOME to that path instead. T3 did not change any SQLite files.`; + } +} + export class CodexShadowHomePrivateEntrySymlinkError extends Schema.TaggedError()( "CodexShadowHomePrivateEntrySymlinkError", { @@ -133,10 +196,17 @@ export const CodexShadowHomeError = Schema.Union([ CodexShadowHomeFileSystemError, CodexShadowHomePathConflictError, CodexShadowHomeEntryConflictError, + CodexShadowHomeSqliteConflictError, + CodexShadowHomeSqliteSettingError, CodexShadowHomePrivateEntrySymlinkError, ]); export type CodexShadowHomeError = typeof CodexShadowHomeError.Type; +/** The errors whose message tells the user how to fix their SQLite layout. */ +export const isCodexShadowHomeSqliteError = Schema.is( + Schema.Union([CodexShadowHomeSqliteConflictError, CodexShadowHomeSqliteSettingError]), +); + type LinkState = | { readonly _tag: "Missing"; @@ -190,6 +260,231 @@ const readLinkState = Effect.fn("CodexHomeLayout.readLinkState")(function* (inpu ); }); +const readHomeEntries = ( + fileSystem: FileSystem.FileSystem, + layout: CodexHomeLayout, + effectiveHomePath: string, + directory: string, +) => + fileSystem.readDirectory(directory).pipe( + Effect.catchTags({ + PlatformError: (cause) => + cause.reason._tag === "NotFound" + ? Effect.succeed>([]) + : new CodexShadowHomeFileSystemError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + operation: "readDirectory", + path: directory, + cause, + }), + }), + ); + +/** + * Codex opens its SQLite databases from `CODEX_SQLITE_HOME`. Windows SQLite derives `-wal`/`-shm` + * from the path it was given, so a database reached through a shadow-home link gets its own WAL. + * Point every shadow at the shared home instead; legacy links left in the shadow become inert. + * POSIX SQLite opens a link by its target's path, so only there may a shadow link shared ones. + */ +export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironment")(function* ( + layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv, + /** The launch args Codex will get, so a `sqlite_home` among them is not silently outranked. */ + launchArgs?: string, +) { + const effectiveHomePath = layout.effectiveHomePath; + if (layout.mode !== "authOverlay" || !effectiveHomePath) { + // A T3 started from a Codex shell inherits that Codex's pin; only a shadow home sets one. + if (environment[CODEX_PINNED_SQLITE_HOME_ENV] === undefined) return environment; + const unpinned = { ...environment }; + delete unpinned[CODEX_PINNED_SQLITE_HOME_ENV]; + return unpinned; + } + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const windows = yield* HostProcess.isWindows; + const context = { sharedHomePath: layout.sharedHomePath, effectiveHomePath }; + const base = { ...environment }; + delete base[CODEX_PINNED_SQLITE_HOME_ENV]; + let override = base.CODEX_SQLITE_HOME?.trim(); + if (windows) { + // Windows reads names without regard to case, so Codex must get only the spelling T3 checked. + for (const name of Object.keys(base)) { + if (name.toUpperCase() !== "CODEX_SQLITE_HOME") continue; + override = base[name]?.trim(); + delete base[name]; + } + } + // Every Codex config layer outranks the variable, so Windows launches also pass the directory + // as an argument. + const route = (sqliteHomePath: string): NodeJS.ProcessEnv => ({ + ...base, + CODEX_SQLITE_HOME: sqliteHomePath, + ...(windows ? { [CODEX_PINNED_SQLITE_HOME_ENV]: sqliteHomePath } : {}), + }); + + if (windows) { + // Codex run by hand in a provider terminal gets no argument from T3, and a setting T3 outranked + // without saying so would look like lost history. + const launchArg = codexLaunchArgSettingSqliteHome(launchArgs); + if (launchArg !== undefined) { + return yield* new CodexShadowHomeSqliteSettingError({ + ...context, + setting: "launchArgs", + value: launchArg, + }); + } + // A fresh shadow links the shared config.toml in, so that file counts before the link exists. + for (const homePath of [effectiveHomePath, layout.sharedHomePath]) { + const configPath = path.join(homePath, "config.toml"); + const config = yield* fileSystem.readFileString(configPath).pipe( + Effect.catchTags({ + PlatformError: (cause) => + cause.reason._tag === "NotFound" + ? Effect.succeed("") + : new CodexShadowHomeFileSystemError({ + ...context, + operation: "readFile", + path: configPath, + cause, + }), + }), + ); + if (codexConfigSetsSqliteHome(config)) { + return yield* new CodexShadowHomeSqliteSettingError({ + ...context, + setting: "configFile", + value: configPath, + }); + } + } + } + + if (override) { + // Codex expands `~` itself, and a relative home is safe where a link opens by its target. + if (!windows) return route(override); + const expanded = expandHomePath(override, yield* HostProcess.HomeDirectory); + // Probes, sessions and text generation each start Codex in a different directory. + if (!path.isAbsolute(expanded)) { + return yield* new CodexShadowHomeSqliteSettingError({ + ...context, + setting: "relativeEnvironment", + value: override, + }); + } + // One directory has many spellings: an alias, another letter case, a trailing separator. + const canonical = (directory: string) => + fileSystem.realPath(directory).pipe(Effect.orElseSucceed(() => path.resolve(directory))); + const sqliteHomePath = yield* canonical(expanded); + if (sqliteHomePath.toLowerCase() === (yield* canonical(layout.sharedHomePath)).toLowerCase()) { + return route(layout.sharedHomePath); + } + // Naming a directory outright, the shadow included, must not reach a database through a link. + for (const entryName of yield* readHomeEntries( + fileSystem, + layout, + effectiveHomePath, + sqliteHomePath, + )) { + if (!SQLITE_DATABASE_ENTRY_NAME.test(entryName)) continue; + const state = yield* readLinkState({ + fileSystem, + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + linkPath: path.join(sqliteHomePath, entryName), + }); + if (state._tag === "Symlink") { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + sqliteHomePath, + }); + } + } + return route(sqliteHomePath); + } + + const regularDatabases = new Set(); + const linkedDatabases: Array = []; + const sidecarsWithData: Array<{ readonly entryName: string; readonly databaseName: string }> = []; + for (const entryName of yield* readHomeEntries( + fileSystem, + layout, + effectiveHomePath, + effectiveHomePath, + )) { + if (!SQLITE_ENTRY_NAME.test(entryName)) continue; + const linkPath = path.join(effectiveHomePath, entryName); + const state = yield* readLinkState({ + fileSystem, + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + linkPath, + }); + if (state._tag === "Symlink" && SQLITE_DATABASE_ENTRY_NAME.test(entryName)) { + linkedDatabases.push(entryName.toLowerCase()); + } + if (state._tag !== "NotSymlink") continue; + if (SQLITE_DATABASE_ENTRY_NAME.test(entryName)) { + regularDatabases.add(entryName.toLowerCase()); + continue; + } + // `-shm` is a rebuildable index and an empty WAL or journal holds nothing to lose. + const sidecar = SQLITE_DATA_SIDECAR_ENTRY_NAME.exec(entryName); + if (!sidecar?.[1]) continue; + const info = yield* fileSystem.stat(linkPath).pipe(Effect.option); + if (Option.isSome(info) && Number(info.value.size) > 0) { + sidecarsWithData.push({ entryName, databaseName: sidecar[1].toLowerCase() }); + } + } + + // A WAL or journal beside a linked or missing database may hold writes the shared copy lacks. + const stranded = sidecarsWithData.find((sidecar) => !regularDatabases.has(sidecar.databaseName)); + if (stranded) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: stranded.entryName, + }); + } + if (regularDatabases.size === 0) return route(layout.sharedHomePath); + const sharedDatabases = (yield* readHomeEntries( + fileSystem, + layout, + effectiveHomePath, + layout.sharedHomePath, + )) + .filter((entry) => SQLITE_DATABASE_ENTRY_NAME.test(entry)) + .map((entry) => entry.toLowerCase()); + // Only the same database diverging in both homes is ambiguous, as for other shadow entries. + const conflict = sharedDatabases.find((entry) => regularDatabases.has(entry)); + if (conflict) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: conflict, + }); + } + // A legacy link counts even when its shared database is gone, since Codex would recreate it. + const [sharedDatabase] = [...sharedDatabases, ...linkedDatabases].toSorted(); + const [privateDatabase] = [...regularDatabases].toSorted(); + // Linking the shared databases in would give each Windows connection its own WAL. + if (sharedDatabase && privateDatabase && windows) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: privateDatabase, + sharedEntryName: sharedDatabase, + }); + } + // Neither home holds every database, so the shadow keeps its own and links the shared ones. + return route(effectiveHomePath); +}); + const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { readonly fileSystem: FileSystem.FileSystem; readonly sharedHomePath: string; @@ -347,8 +642,9 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP }, ); -export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( +const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* ( layout: CodexHomeLayout, + linkSharedDatabases: boolean, ) { if (layout.mode !== "authOverlay") return; const effectiveHomePath = layout.effectiveHomePath; @@ -402,7 +698,12 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" ); const entries = new Set(KNOWN_SHARED_DIRECTORIES); for (const entryName of sharedEntryNames) { - if (!PRIVATE_ENTRY_NAMES.has(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName)) { + if ( + !PRIVATE_ENTRY_NAMES.has(entryName) && + !SHADOW_LOCAL_ENTRY_NAMES.has(entryName) && + (!SQLITE_ENTRY_NAME.test(entryName) || + (linkSharedDatabases && SQLITE_DATABASE_ENTRY_NAME.test(entryName))) + ) { entries.add(entryName); } } @@ -444,6 +745,24 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" }); }); +/** Prepares the shadow home and returns the environment Codex must launch with. */ +export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( + layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv, + launchArgs?: string, +) { + // Refuse a stranded SQLite layout before touching the shadow home. + const launchEnvironment = yield* codexHomeEnvironment(layout, environment, launchArgs); + // Windows never links a database, even one the shared home gained after the check above. + yield* materializeShadowHome( + layout, + !environment.CODEX_SQLITE_HOME?.trim() && + launchEnvironment.CODEX_SQLITE_HOME === layout.effectiveHomePath && + !(yield* HostProcess.isWindows), + ); + return launchEnvironment; +}); + export function codexContinuationIdentity(layout: CodexHomeLayout) { return { driverKind: ProviderDriverKind.make("codex"), diff --git a/apps/server/src/provider/Drivers/CodexManagedProvider.ts b/apps/server/src/provider/Drivers/CodexManagedProvider.ts index f182245f8840d..dca371187056b 100644 --- a/apps/server/src/provider/Drivers/CodexManagedProvider.ts +++ b/apps/server/src/provider/Drivers/CodexManagedProvider.ts @@ -7,6 +7,7 @@ import { import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; +import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as Option from "effect/Option"; import { ChildProcessSpawner } from "effect/process"; @@ -27,10 +28,11 @@ import { type ProviderDriverCreateInput, type ProviderInstance, } from "@t3tools/provider-core/server/driver"; -import { codexContinuationIdentity } from "./CodexHomeLayout.ts"; +import { codexContinuationIdentity, isCodexShadowHomeSqliteError } from "./CodexHomeLayout.ts"; import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity"; import { HttpClient } from "effect/http"; const DRIVER = ProviderDriverKind.make("codex"); +const isProviderSetupError = Schema.is(ProviderSetupError); export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(function* ( input: ProviderDriverCreateInput, @@ -179,11 +181,16 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu : {}), }), ), - Effect.catch(() => - runtime.auth.read.pipe( + Effect.catch((cause) => { + const sqliteConflict = + isProviderSetupError(cause) && isCodexShadowHomeSqliteError(cause.cause) + ? cause.cause + : undefined; + return runtime.auth.read.pipe( Effect.orElseSucceed(() => Option.none()), Effect.map((current) => ({ ...base, + ...(sqliteConflict ? { status: "error" as const } : {}), installed: true, version: executable.value.version, models: [], @@ -204,10 +211,12 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu ...(Option.isSome(current) && current.value.scopes.includes("chatgpt.tokens.use.direct") ? { usageLimits } : {}), - message: "Could not check Codex right now. Retry, or reconnect in provider settings.", + message: + sqliteConflict?.message ?? + "Could not check Codex right now. Retry, or reconnect in provider settings.", })), - ), - ), + ); + }), Effect.scoped, ); }).pipe( diff --git a/apps/server/src/provider/codexLaunchArgs.test.ts b/apps/server/src/provider/codexLaunchArgs.test.ts index e74b29d44818c..beff8ccc5c2bd 100644 --- a/apps/server/src/provider/codexLaunchArgs.test.ts +++ b/apps/server/src/provider/codexLaunchArgs.test.ts @@ -5,6 +5,7 @@ import { describe, it } from "vite-plus/test"; import { codexAppServerArgs, codexExecLaunchArgs, + codexLaunchArgSettingSqliteHome, codexMcpListLaunchArgs, } from "./codexLaunchArgs.ts"; @@ -21,7 +22,7 @@ describe("codexAppServerArgs", () => { expected: ["-c", "thread_unload_delay_secs=1800"], }, ])("asks Codex to unload unsubscribed threads immediately and $name", (row) => { - NodeAssert.deepStrictEqual(codexAppServerArgs(row.launchArgs), [ + NodeAssert.deepStrictEqual(codexAppServerArgs(row.launchArgs, undefined), [ "app-server", "-c", "thread_unload_delay_secs=0", @@ -33,7 +34,10 @@ describe("codexAppServerArgs", () => { describe("codexExecLaunchArgs", () => { it("keeps shared codex flags and omits app-server-only flags", () => { NodeAssert.deepStrictEqual( - codexExecLaunchArgs('--strict-config --enable foo --listen off --config model="gpt 5"'), + codexExecLaunchArgs( + '--strict-config --enable foo --listen off --config model="gpt 5"', + undefined, + ), ["--strict-config", "--enable", "foo", "--config", "model=gpt 5"], ); }); @@ -42,15 +46,17 @@ describe("codexExecLaunchArgs", () => { NodeAssert.deepStrictEqual( codexExecLaunchArgs( "--enable hooks --enable=hooks --enable codex_hooks --enable=codex_hooks --enable foo --disable hooks", + undefined, ), ["--enable", "foo", "--disable", "hooks"], ); }); it("does not pair value-taking flags with adjacent flags", () => { - NodeAssert.deepStrictEqual(codexExecLaunchArgs("--config --strict-config --enable --disable"), [ - "--strict-config", - ]); + NodeAssert.deepStrictEqual( + codexExecLaunchArgs("--config --strict-config --enable --disable", undefined), + ["--strict-config"], + ); }); }); @@ -59,7 +65,7 @@ describe("codex exec and mcp list launch args", () => { "--strict-config --oss --local-provider ollama --profile local -p=work --listen off --enable foo"; it("keeps the local model provider flags for codex exec", () => { - NodeAssert.deepStrictEqual(codexExecLaunchArgs(launchArgs), [ + NodeAssert.deepStrictEqual(codexExecLaunchArgs(launchArgs, undefined), [ "--strict-config", "--oss", "--local-provider", @@ -73,7 +79,7 @@ describe("codex exec and mcp list launch args", () => { }); it("keeps the selected profile and drops exec-only flags for codex mcp list", () => { - NodeAssert.deepStrictEqual(codexMcpListLaunchArgs(launchArgs), [ + NodeAssert.deepStrictEqual(codexMcpListLaunchArgs(launchArgs, undefined), [ "--profile", "local", "-p=work", @@ -82,3 +88,62 @@ describe("codex exec and mcp list launch args", () => { ]); }); }); + +describe("codexLaunchArgSettingSqliteHome", () => { + it.each([ + ['-c sqlite_home="/my shadow"', "sqlite_home=/my shadow"], + ["--config sqlite_home=/shadow", "sqlite_home=/shadow"], + ["--config=sqlite_home='/shadow'", "--config=sqlite_home=/shadow"], + ["-csqlite_home=/shadow", "-csqlite_home=/shadow"], + ["-c=sqlite_home=/shadow", "-c=sqlite_home=/shadow"], + ["--strict-config -c ' sqlite_home = \"/shadow\"'", ' sqlite_home = "/shadow"'], + // Codex 0.170.0 ignores a quoted key, but a later release may not. + [`-c '"sqlite_home"="/shadow"'`, '"sqlite_home"="/shadow"'], + ])("finds the override in %s", (launchArgs, expected) => { + NodeAssert.equal(codexLaunchArgSettingSqliteHome(launchArgs), expected); + }); + + // regression: #24486 any argument holding `sqlite_home=` counted, so Windows refused an MCP + // server's own variable and text that only mentioned the setting. + it.each([ + `-c 'mcp_servers.example.env.sqlite_home="MCP-only"'`, + `--config=mcp_servers.example.env.sqlite_home=MCP-only`, + `-c 'developer_instructions="Leave sqlite_home=unset"'`, + "-c sqlite_home.nested=1", + "-c model=sqlite_home", + "--strict-config --enable sqlite_home", + "", + ])("leaves %s alone", (launchArgs) => { + NodeAssert.equal(codexLaunchArgSettingSqliteHome(launchArgs), undefined); + }); +}); + +describe("pinned SQLite home", () => { + // regression: #24486 a sqlite_home in any Codex config layer outranked CODEX_SQLITE_HOME. + it("puts the SQLite home T3 validated after the launch args of every Codex command", () => { + const environment = { T3CODE_CODEX_SQLITE_HOME: String.raw`C:\Users\me\.codex` }; + const pin = ["-c", String.raw`sqlite_home='C:\Users\me\.codex'`]; + + NodeAssert.deepStrictEqual(codexAppServerArgs("--strict-config", environment), [ + "app-server", + "-c", + "thread_unload_delay_secs=0", + "--strict-config", + ...pin, + ]); + NodeAssert.deepStrictEqual(codexExecLaunchArgs("--strict-config", environment), [ + "--strict-config", + ...pin, + ]); + NodeAssert.deepStrictEqual(codexMcpListLaunchArgs("--enable foo", environment), [ + "--enable", + "foo", + ...pin, + ]); + // A TOML literal string cannot hold an apostrophe. + NodeAssert.deepStrictEqual( + codexMcpListLaunchArgs("", { T3CODE_CODEX_SQLITE_HOME: String.raw`C:\O'Neil\.codex` }), + ["-c", String.raw`sqlite_home="C:\\O'Neil\\.codex"`], + ); + }); +}); diff --git a/apps/server/src/provider/codexLaunchArgs.ts b/apps/server/src/provider/codexLaunchArgs.ts index e49314ee83e0d..f22096c3751c6 100644 --- a/apps/server/src/provider/codexLaunchArgs.ts +++ b/apps/server/src/provider/codexLaunchArgs.ts @@ -9,12 +9,46 @@ export const resolveCodexLaunchArgs = ( const codexLaunchArgv = (launchArgs?: string): ReadonlyArray => tokenizeCliArgs(launchArgs); +/** Set by `codexHomeEnvironment` for a Windows shadow home: the SQLite directory T3 validated. */ +export const CODEX_PINNED_SQLITE_HOME_ENV = "T3CODE_CODEX_SQLITE_HOME"; + +// Codex ranks `sqlite_home` from any config.toml above CODEX_SQLITE_HOME, and a later `-c` above +// both, so this goes after the launch args. +const pinnedSqliteHomeArgs = (environment: NodeJS.ProcessEnv | undefined) => { + const sqliteHome = environment?.[CODEX_PINNED_SQLITE_HOME_ENV]; + if (!sqliteHome) return []; + // A TOML literal string takes a Windows path as written, but cannot hold an apostrophe. + const value = sqliteHome.includes("'") ? JSON.stringify(sqliteHome) : `'${sqliteHome}'`; + return ["-c", `sqlite_home=${value}`]; +}; + +const CONFIG_OVERRIDE_FLAG = /^(?:--config=|-c=?)/u; +const QUOTED_OVERRIDE_KEY = /^(["'])(.*)\1$/u; + +/** + * The launch arg that sets `sqlite_home`. Codex splits an override at its first `=` and reads only + * the root key, so `mcp_servers.x.env.sqlite_home` and a value that mentions the key do not count. + */ +export const codexLaunchArgSettingSqliteHome = (launchArgs?: string) => + codexLaunchArgv(launchArgs).find((arg) => { + // Either the flag with its override attached, or the override that follows a bare flag. + const override = arg.replace(CONFIG_OVERRIDE_FLAG, ""); + const equals = override.indexOf("="); + if (equals < 0) return false; + const key = override.slice(0, equals).trim(); + return key.replace(QUOTED_OVERRIDE_KEY, "$2").trim() === "sqlite_home"; + }); + // T3 unsubscribes only when it unloads a thread; Codex's own unload delay would stack on that. -export const codexAppServerArgs = (launchArgs?: string) => [ +export const codexAppServerArgs = ( + launchArgs: string | undefined, + environment: NodeJS.ProcessEnv | undefined, +) => [ "app-server", "-c", "thread_unload_delay_secs=0", ...codexLaunchArgv(launchArgs), + ...pinnedSqliteHomeArgs(environment), ]; interface LaunchArgFilter { @@ -65,14 +99,22 @@ const filterLaunchArgs = (launchArgs: string | undefined, allowed: LaunchArgFilt const HOOK_FEATURES = new Set(["hooks", "codex_hooks"]); // Feature toggles beat config overrides, so a launch arg's hook enable would undo ours. -export const codexExecLaunchArgs = (launchArgs?: string) => { +export const codexExecLaunchArgs = ( + launchArgs: string | undefined, + environment: NodeJS.ProcessEnv | undefined, +) => { const args = filterLaunchArgs(launchArgs, EXEC_FILTER); - return args.filter((arg, index) => { - if (arg.startsWith("--enable=")) return !HOOK_FEATURES.has(arg.slice("--enable=".length)); - if (arg === "--enable") return !HOOK_FEATURES.has(args[index + 1] ?? ""); - return !(args[index - 1] === "--enable" && HOOK_FEATURES.has(arg)); - }); + return [ + ...args.filter((arg, index) => { + if (arg.startsWith("--enable=")) return !HOOK_FEATURES.has(arg.slice("--enable=".length)); + if (arg === "--enable") return !HOOK_FEATURES.has(args[index + 1] ?? ""); + return !(args[index - 1] === "--enable" && HOOK_FEATURES.has(arg)); + }), + ...pinnedSqliteHomeArgs(environment), + ]; }; -export const codexMcpListLaunchArgs = (launchArgs?: string) => - filterLaunchArgs(launchArgs, MCP_LIST_FILTER); +export const codexMcpListLaunchArgs = ( + launchArgs: string | undefined, + environment: NodeJS.ProcessEnv | undefined, +) => [...filterLaunchArgs(launchArgs, MCP_LIST_FILTER), ...pinnedSqliteHomeArgs(environment)]; diff --git a/apps/server/src/provider/codexSqliteHomeSetting.test.ts b/apps/server/src/provider/codexSqliteHomeSetting.test.ts new file mode 100644 index 0000000000000..9c57e4c8e0eb7 --- /dev/null +++ b/apps/server/src/provider/codexSqliteHomeSetting.test.ts @@ -0,0 +1,76 @@ +import * as NodeAssert from "node:assert/strict"; + +import { describe, it } from "vite-plus/test"; + +import { codexConfigSetsSqliteHome } from "./codexSqliteHomeSetting.ts"; + +// Each row was run through Codex 0.170.0, which opened its databases in the named directory for +// the first table and in CODEX_HOME for the second. +describe("codexConfigSetsSqliteHome", () => { + it.each([ + ["a root key", 'model = "gpt"\nsqlite_home = "C:\\\\shadow"\n'], + ["a root key without a final newline", "sqlite_home='/shadow'"], + ["a quoted key", '"sqlite_home" = "/shadow"\n'], + ["a literal-quoted key", "'sqlite_home' = '/shadow'\n"], + ["an escaped key", '"sqlite\\u005fhome" = "/shadow"\n'], + ["a key with a long escape", '"sqlite\\U0000005Fhome" = "/shadow"\n'], + ["a key with a hex escape", '"sqlite\\x5fhome" = "/shadow"\n'], + ["a byte order mark, tabs and CRLF", '\uFEFF\t sqlite_home\t =\t"/shadow"\r\n'], + [ + "a key after arrays whose lines open with a bracket", + 'notify = [\n "a", # ]\n]\nmatrix = [\n[1, 2],\n[3, 4],\n]\nsqlite_home = "/shadow"\n', + ], + ["a key after a string that ends in a quote", 'note = """a""""\nsqlite_home = "/shadow"\n'], + [ + "a key after a literal string holding a table header", + "note = '''\n[fake]\n'''\nsqlite_home = '/shadow'\n", + ], + [ + "a key after a comment and a value that hold string and table syntax", + '# """\nnote = "a # [b]" # [not.a.table]\nsqlite_home = "/shadow"\n', + ], + ["a key before the first table", 'sqlite_home = "/shadow"\n[features]\nx = true\n'], + ])("finds %s", (_name, config) => { + NodeAssert.equal(codexConfigSetsSqliteHome(config), true); + }); + + // regression: #24486 every line that looked like the key counted, whatever table or string + // held it. + it.each([ + ["no config", ""], + ["a comment", '# sqlite_home = "/shadow"\nmodel = "gpt"\n'], + [ + "an MCP server's variable", + '[mcp_servers.example]\ncommand = "echo"\n[mcp_servers.example.env]\nsqlite_home = "an MCP-only value"\n', + ], + ["a table's key", "[features]\nx = true\n \"sqlite_home\"='/shadow'\n"], + ["an array table's key", '[[skills.config]]\npath = "x"\nsqlite_home = "/shadow"\n'], + [ + "text in a multi-line string", + 'developer_instructions = """\nKeep this text.\nsqlite_home = "somewhere"\n"""\nmodel = "gpt"\n', + ], + [ + "text in a multi-line literal string", + "developer_instructions = '''\nsqlite_home = 'somewhere'\n'''\n", + ], + ["a key of a multi-line inline table", 'unused = {\n sqlite_home = "/shadow"\n}\n'], + ["a key of another root table", 'tools.sqlite_home = "/shadow"\n'], + // Codex reads neither of these as a path and falls back to its defaults. + ["a table named sqlite_home", "sqlite_home.x = 1\n"], + ["a table header named sqlite_home", "[sqlite_home]\nx = 1\n"], + ["a quoted key that spells out the escape", '"sqlite\\\\u005fhome" = "/shadow"\n'], + ["a longer key", 'codex_sqlite_home = "/shadow"\nsqlite_home_dir = "/shadow"\n'], + ])("ignores %s", (_name, config) => { + NodeAssert.equal(codexConfigSetsSqliteHome(config), false); + }); + + // Codex 0.170.0 drops a config it cannot parse, but where the string or array was meant to + // end is a guess. + it.each([ + ["an unclosed multi-line string", 'note = """\nsqlite_home = "/shadow"\n'], + ["an unclosed array", 'notify = [\n"a",\nsqlite_home = "/shadow"\n'], + ["an unclosed string", 'note = "a\nsqlite_home = "/shadow"\n'], + ])("finds the key after %s", (_name, config) => { + NodeAssert.equal(codexConfigSetsSqliteHome(config), true); + }); +}); diff --git a/apps/server/src/provider/codexSqliteHomeSetting.ts b/apps/server/src/provider/codexSqliteHomeSetting.ts new file mode 100644 index 0000000000000..f5510b9949a32 --- /dev/null +++ b/apps/server/src/provider/codexSqliteHomeSetting.ts @@ -0,0 +1,85 @@ +const BARE_KEY = /[A-Za-z0-9_-]+/uy; +const KEY_ESCAPE = /\\(?:u([0-9A-Fa-f]{4})|U([0-9A-Fa-f]{8})|x([0-9A-Fa-f]{2})|[^])/gu; +const ASSIGNMENT = /[ \t]*=/uy; +// Where an unclosed string or array was meant to end is a guess, so any line that could assign +// the key counts. +const SQLITE_HOME_LINE = /^[ \t]*["']?sqlite_home["']?[ \t]*=/mu; + +const unescapeKey = (key: string) => + key.replace(KEY_ESCAPE, (escape, ...hex: Array) => { + const code = Number.parseInt(hex[0] ?? hex[1] ?? hex[2] ?? "", 16); + // Only an ASCII escape can spell part of the key. + return code < 0x80 ? String.fromCharCode(code) : escape; + }); + +/** + * Whether a Codex config.toml sets `sqlite_home`. Codex reads it from the root table only, so a + * table's key of that name, such as an MCP server's environment variable, and the text of a + * string or a comment are not the setting. + */ +export const codexConfigSetsSqliteHome = (config: string): boolean => { + let unclosed = false; + + /** The index after the string that opens at `start`. */ + const stringEnd = (start: number) => { + const quote = config[start] ?? ""; + const closing = config.startsWith(quote.repeat(3), start) ? quote.repeat(3) : quote; + for (let index = start + closing.length; index < config.length; index++) { + const char = config[index]; + if (char === "\n" && closing.length === 1) break; + if (char === "\\" && quote === '"' && config[index + 1] !== "\n") index++; + else if (config.startsWith(closing, index)) { + index += closing.length; + // A multi-line string may end in up to two quotes of its own. + for (let extra = 0; closing.length === 3 && extra < 2 && config[index] === quote; extra++) { + index++; + } + return index; + } + } + unclosed = true; + return config.length; + }; + + let index = 0; + while (index < config.length) { + const char = config[index] ?? ""; + // `\s` covers the byte order mark a Windows editor may write. + if (/\s/u.test(char)) { + index++; + continue; + } + // Nothing after the first table header belongs to the root table. + if (char === "[") break; + if (char !== "#") { + let key: string | undefined; + if (char === '"' || char === "'") { + const end = stringEnd(index); + key = config.slice(index + 1, end - 1); + if (char === '"') key = unescapeKey(key); + index = end; + } else { + BARE_KEY.lastIndex = index; + key = BARE_KEY.exec(config)?.[0]; + index += key?.length ?? 0; + } + ASSIGNMENT.lastIndex = index; + // `sqlite_home.x` is a table, which Codex does not read as a path. + if (key === "sqlite_home" && ASSIGNMENT.test(config)) return true; + } + // A value ends at the first newline outside its strings, arrays and inline tables. + let depth = 0; + for (; index < config.length; index++) { + const valueChar = config[index]; + if (valueChar === "\n" && depth <= 0) break; + if (valueChar === "#") { + const lineEnd = config.indexOf("\n", index); + index = (lineEnd < 0 ? config.length : lineEnd) - 1; + } else if (valueChar === '"' || valueChar === "'") index = stringEnd(index) - 1; + else if (valueChar === "[" || valueChar === "{") depth++; + else if (valueChar === "]" || valueChar === "}") depth--; + } + if (depth > 0) unclosed = true; + } + return unclosed && SQLITE_HOME_LINE.test(config); +}; diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 10d0b3fe6c1e6..bb72631e8d82f 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -2096,6 +2096,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" const environment = yield* resolveProviderInstanceTerminalEnvironment({ deriveProviderInstanceConfigMap, serverSettings, + fileSystem, path, rawProviderInstanceId: instanceId, env: undefined, diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 74814a0880542..a5170c300e48a 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -2716,6 +2716,7 @@ it.layer( variable: "CODEX_HOME", config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" }, expectedHome: "/configured/codex-shadow", + expectedSqliteHome: "/configured/codex", }, { name: "Claude home", @@ -2724,27 +2725,128 @@ it.layer( config: { homePath: "/configured/claude" }, expectedHome: "/configured/claude", }, - ])("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) => + ])( + "prefers $name over the instance environment", + ({ driver, variable, config, expectedHome, expectedSqliteHome }) => + Effect.gen(function* () { + const path = yield* Path.Path; + const serverSettings = yield* ServerSettings.ServerSettingsService; + const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + deriveProviderInstanceConfigMap, + serverSettings, + fileSystem: yield* FileSystem.FileSystem, + path, + rawProviderInstanceId: "configured_home", + env: undefined, + }); + + expect(environment[variable]).toBe(path.resolve(expectedHome)); + expect(environment.CODEX_SQLITE_HOME).toBe( + expectedSqliteHome && path.resolve(expectedSqliteHome), + ); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("configured_home")]: { + driver: ProviderDriverKind.make(driver), + environment: [ + { name: variable, value: "~/.environment-account", sensitive: false }, + ], + config, + }, + }, + }), + ), + ), + ); + + it.effect("routes a shadow terminal's SQLite to the instance environment CODEX_HOME", () => Effect.gen(function* () { const path = yield* Path.Path; const serverSettings = yield* ServerSettings.ServerSettingsService; const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ deriveProviderInstanceConfigMap, serverSettings, + fileSystem: yield* FileSystem.FileSystem, path, - rawProviderInstanceId: "configured_home", + rawProviderInstanceId: "environment_home", env: undefined, - }); + }).pipe(Effect.provideService(HostProcess.Environment, {})); + + expect(environment.CODEX_HOME).toBe(path.resolve("/configured/environment-shadow")); + expect(environment.CODEX_SQLITE_HOME).toBe(path.resolve("/configured/environment-codex")); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("environment_home")]: { + driver: ProviderDriverKind.make("codex"), + environment: [ + { name: "CODEX_HOME", value: "/configured/environment-codex", sensitive: false }, + ], + config: { shadowHomePath: "/configured/environment-shadow" }, + }, + }, + }), + ), + ), + ); + + // regression: #24486 Windows kept a client's `codex_sqlite_home` beside the host's + // CODEX_SQLITE_HOME, and the host's spelling was the one validated and written back. + it.effect("ranks a Windows terminal's SQLite home by its source, whatever its casing", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const serverSettings = yield* ServerSettings.ServerSettingsService; + const fileSystem = yield* FileSystem.FileSystem; + const resolve = (rawProviderInstanceId: string) => + TerminalManager.resolveProviderInstanceTerminalEnvironment({ + deriveProviderInstanceConfigMap, + serverSettings, + fileSystem, + path, + rawProviderInstanceId, + env: { codex_sqlite_home: "/configured/client-sqlite" }, + }).pipe( + Effect.provideService(HostProcess.Environment, { + CODEX_SQLITE_HOME: "/configured/host-sqlite", + }), + Effect.provideService(HostProcess.Platform, "win32"), + ); - expect(environment[variable]).toBe(path.resolve(expectedHome)); + expect(yield* resolve("client_sqlite")).toMatchObject({ + CODEX_HOME: path.resolve("/configured/client-shadow"), + CODEX_SQLITE_HOME: path.resolve("/configured/client-sqlite"), + }); + expect(yield* resolve("instance_sqlite")).toMatchObject({ + CODEX_SQLITE_HOME: path.resolve("/configured/instance-sqlite"), + }); + expect((yield* resolve("client_sqlite")).codex_sqlite_home).toBeUndefined(); }).pipe( Effect.provide( ServerSettings.layerTest({ providerInstances: { - [ProviderInstanceId.make("configured_home")]: { - driver: ProviderDriverKind.make(driver), - environment: [{ name: variable, value: "~/.environment-account", sensitive: false }], - config, + [ProviderInstanceId.make("client_sqlite")]: { + driver: ProviderDriverKind.make("codex"), + config: { + homePath: "/configured/codex", + shadowHomePath: "/configured/client-shadow", + }, + }, + [ProviderInstanceId.make("instance_sqlite")]: { + driver: ProviderDriverKind.make("codex"), + environment: [ + { + name: "Codex_Sqlite_Home", + value: "/configured/instance-sqlite", + sensitive: false, + }, + ], + config: { + homePath: "/configured/codex", + shadowHomePath: "/configured/instance-shadow", + }, }, }, }), diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index dc7e192119903..3cfbf3347c807 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -69,7 +69,11 @@ import * as SynchronizedRef from "effect/SynchronizedRef"; import packageJson from "../../package.json" with { type: "json" }; import * as ServerConfig from "../config.ts"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; -import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts"; +import { + codexHomeEnvironment, + isCodexShadowHomeSqliteError, + resolveCodexHomeLayout, +} from "../provider/Drivers/CodexHomeLayout.ts"; import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts"; import type { deriveProviderInstanceConfigMap } from "../provider/ProviderInstanceRegistryHydration.ts"; import * as ServerSettings from "../serverSettings.ts"; @@ -1703,6 +1707,7 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( )(function* (input: { readonly deriveProviderInstanceConfigMap: typeof deriveProviderInstanceConfigMap; readonly serverSettings: ServerSettings.ServerSettingsService["Service"]; + readonly fileSystem: FileSystem.FileSystem; readonly path: Path.Path; readonly rawProviderInstanceId: string; readonly env: Record | undefined; @@ -1720,11 +1725,59 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( if (instance.driver === "codex") { const config = decodeCodexSettings(instance.config ?? {}); if (Option.isSome(config)) { - const layout = yield* resolveCodexHomeLayout(config.value).pipe( - Effect.provideService(Path.Path, input.path), - ); + const hostEnvironment = yield* HostProcess.Environment; + // A shadow shares the instance environment's CODEX_HOME, as the provider resolves it. + const environmentHome = (yield* mergeProviderInstanceEnvironment( + instance.environment, + hostEnvironment, + )).CODEX_HOME; + const layout = yield* resolveCodexHomeLayout( + config.value.shadowHomePath.trim() && + !config.value.homePath.trim() && + environmentHome?.trim() + ? { ...config.value, homePath: environmentHome } + : config.value, + ).pipe(Effect.provideService(Path.Path, input.path)); if (layout.effectiveHomePath) resolved = { ...resolved, CODEX_HOME: layout.effectiveHomePath }; + if (layout.mode === "authOverlay") { + const windows = yield* HostProcess.isWindows; + const launchEnvironment = { ...hostEnvironment }; + for (const [name, value] of Object.entries(resolved)) { + // Windows reads names without regard to case, so a client's or instance's spelling must + // replace the host's rather than sit beside it. + if (windows) { + for (const inherited of Object.keys(launchEnvironment)) { + if (inherited.toUpperCase() === name.toUpperCase()) + delete launchEnvironment[inherited]; + } + } + launchEnvironment[name] = value; + } + const sqliteEnvironment = yield* codexHomeEnvironment(layout, launchEnvironment).pipe( + Effect.mapError( + (cause) => + new TerminalProviderEnvironmentError({ + providerInstanceId, + ...(isCodexShadowHomeSqliteError(cause) ? { detail: cause.message } : {}), + cause, + }), + ), + Effect.provideService(FileSystem.FileSystem, input.fileSystem), + Effect.provideService(Path.Path, input.path), + ); + const sqliteHome = sqliteEnvironment.CODEX_SQLITE_HOME; + if (sqliteHome) { + resolved = { + ...Object.fromEntries( + Object.entries(resolved).filter( + ([name]) => !windows || name.toUpperCase() !== "CODEX_SQLITE_HOME", + ), + ), + CODEX_SQLITE_HOME: sqliteHome, + }; + } + } } } else if (instance.driver === "claudeAgent") { const config = decodeClaudeSettings(instance.config ?? {}); @@ -1749,6 +1802,7 @@ export const make = Effect.fn("TerminalManager.make")(function* ( const portDiscovery = yield* PortScanner.PortDiscovery; const nativeTelemetry = yield* NativeTelemetryClient.NativeTelemetryClient; const serverSettings = yield* ServerSettings.ServerSettingsService; + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const resolveProviderInstanceEnvironment = Effect.fn( "terminal.resolveProviderInstanceEnvironment", @@ -1756,6 +1810,7 @@ export const make = Effect.fn("TerminalManager.make")(function* ( resolveProviderInstanceTerminalEnvironment({ deriveProviderInstanceConfigMap: deriveConfigMap, serverSettings, + fileSystem, path, rawProviderInstanceId, env, diff --git a/apps/server/src/textGeneration/CodexTextGeneration.ts b/apps/server/src/textGeneration/CodexTextGeneration.ts index c9810e3cfe360..7d194d211fe1e 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.ts @@ -301,10 +301,10 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func binary, cwd, env, - launchArgs: codexMcpListLaunchArgs(launchArgs), + launchArgs: codexMcpListLaunchArgs(launchArgs, effectiveEnvironment), }); const mcpServersOff = codexMcpServersOff(enabledMcpServers); - const execLaunchArgs = codexExecLaunchArgs(launchArgs); + const execLaunchArgs = codexExecLaunchArgs(launchArgs, effectiveEnvironment); // A launch-arg mcp_servers={...} table replaces ours and can re-enable dotted names. const launchArgsSetMcpTable = execLaunchArgs.some((arg) => /\bmcp_servers\s*=/.test(arg)); const spawnCommand = yield* resolveSpawnCommand( diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.ts index fea5f216f80bd..fec8739682559 100644 --- a/packages/contracts/src/terminal.ts +++ b/packages/contracts/src/terminal.ts @@ -469,11 +469,15 @@ export class TerminalProviderEnvironmentError extends Schema.TaggedError