From 50ea343b7596ac92438414ff9da93b0f72f43d77 Mon Sep 17 00:00:00 2001 From: Navneet Chaudhary Date: Tue, 3 Nov 2026 00:55:49 +0530 Subject: [PATCH 01/17] fix(server): keep Codex shadow SQLite state together --- .../Adapters/CodexAdapterV2.ts | 31 ++- .../src/provider/CodexManagedRuntime.test.ts | 117 +++++++- .../src/provider/CodexManagedRuntime.ts | 8 +- .../src/provider/Drivers/CodexDriver.test.ts | 107 ++++++++ .../src/provider/Drivers/CodexDriver.ts | 8 +- .../provider/Drivers/CodexHomeLayout.test.ts | 254 ++++++++++++++++++ .../src/provider/Drivers/CodexHomeLayout.ts | 124 ++++++++- .../provider/Drivers/CodexManagedProvider.ts | 25 +- 8 files changed, 649 insertions(+), 25 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index f81a3809b35fe..f4e6bad74934f 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -111,6 +111,7 @@ import { toMcpElicitationResponse, } from "../../provider/CodexMcpElicitation.ts"; import { + codexHomeEnvironment, materializeCodexShadowHome, resolveCodexHomeLayout, } from "../../provider/Drivers/CodexHomeLayout.ts"; @@ -1594,17 +1595,25 @@ 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).pipe( + Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), + Effect.mapError( + (cause) => + new ProviderAdapterDriverCreateError({ + driver: CODEX_DRIVER_KIND, + instanceId, + detail: "Failed to prepare the Codex shadow home.", + cause, + }), + ), + ) + : environmentOverrides; const settings = { ...config, @@ -1616,7 +1625,7 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter return yield* makeCodexAdapterV2({ instanceId, settings, - environment: yield* mergeProviderInstanceEnvironment(environment, hostEnvironment), + environment: processEnv, clientFactory, crypto, fileSystem, diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 8f8408bdaee1f..893ad88061c7e 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -6,7 +6,12 @@ import { ChildProcess, ChildProcessSpawner } from "effect/process"; import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; -import { CodexSettings, EnvironmentId, ProviderInstanceId } from "@t3tools/contracts"; +import { + CodexSettings, + EnvironmentId, + ProviderInstanceId, + ProviderSetupError, +} from "@t3tools/contracts"; import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -22,6 +27,7 @@ import * as CodexInstallation from "./CodexInstallation.ts"; import { makeCodexManagedRuntime } from "./CodexManagedRuntime.ts"; import * as ProviderCredentialStore from "./ProviderCredentialStore.ts"; import { codexAppServerArgs } from "./codexLaunchArgs.ts"; +import { CodexShadowHomeSqliteConflictError } from "./Drivers/CodexHomeLayout.ts"; const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const decodeSettings = Schema.decodeSync(CodexSettings); @@ -119,7 +125,9 @@ it.live.each( if (account === "primary") { assert.equal(effective.config.homePath, sharedHome); assert.equal(runtime.homeLayout.mode, "direct"); + assert.isUndefined(effective.environment.CODEX_SQLITE_HOME); } else { + assert.equal(effective.environment.CODEX_SQLITE_HOME, sharedHome); assert.include(effective.config.homePath, instanceId); assert.include(effective.config.homePath, "userdata/providers/codex"); assert.equal(runtime.homeLayout.mode, "authOverlay"); @@ -275,3 +283,110 @@ it.live.each( ), ), ); + +it.live( + "managed additional account preserves dirty legacy SQLite state and returns a structured setup error", + () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("codex-personal"); + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* fs.makeTempDirectoryScoped({ prefix: "codex-shared-home-" }); + yield* fs.writeFileString(path.join(sharedHome, "config.toml"), "# shared config\n"); + + const data = new Map(); + const secrets = ServerSecretStore.ServerSecretStore.of({ + get: (key) => Effect.sync(() => Option.fromUndefinedOr(data.get(key))), + set: (key, value) => + Effect.sync(() => { + data.set(key, value); + }), + remove: (key) => + Effect.sync(() => { + data.delete(key); + }), + create: () => Effect.die("unused"), + getOrCreateRandom: () => Effect.die("unused"), + }); + const executable = { + executablePath: "/user/bin/codex", + managedVersionDirectory: null, + source: "local" as const, + version: "0.166.0", + }; + const layerInstaller = Layer.mock(CodexInstallation.CodexInstallation)({ + managedDirectory: "/isolated/tools/codex", + resolve: () => Effect.succeed(executable), + acquire: () => Effect.succeed(executable), + }); + + yield* Effect.gen(function* () { + const store = yield* ProviderCredentialStore.make("codex-chatgpt", instanceId); + const json = yield* encodeJson({ + clientId: "oaiapp_test", + accessToken: "dummy-owned-access", + refreshToken: "dummy-refresh", + expiresAt: (yield* Clock.currentTimeMillis) + 3_600_000, + earliestRefreshAt: null, + scopes: ["chatgpt.tokens.use.direct"], + subject: "test-user", + email: null, + }); + yield* store.set(new TextEncoder().encode(json)); + + const runtime = yield* makeCodexManagedRuntime({ + instanceId, + enabled: true, + config: decodeSettings({ setupMode: "managed", homePath: sharedHome }), + environment: { + PATH: process.env.PATH, + SystemRoot: process.env.SystemRoot, + }, + }); + assert.equal(runtime.homeLayout.mode, "authOverlay"); + const shadowHome = runtime.homePath; + yield* fs.makeDirectory(shadowHome, { recursive: true }); + + const databaseName = "thread_history_1.sqlite"; + const database = path.join(sharedHome, databaseName); + const linkedDatabase = path.join(shadowHome, databaseName); + const sidecar = path.join(shadowHome, `${databaseName}-shm`); + yield* fs.writeFileString(database, "shared-database"); + yield* fs.symlink(database, linkedDatabase); + yield* fs.writeFileString(sidecar, "shadow-sidecar"); + + const error = yield* runtime.resolve.pipe(Effect.flip); + + assert.instanceOf(error, ProviderSetupError); + assert.equal(error.operation, "runtime"); + assert.instanceOf(error.cause, CodexShadowHomeSqliteConflictError); + assert.include(error.detail, "Codex SQLite recovery is required"); + assert.equal(yield* fs.readLink(linkedDatabase), database); + assert.equal(yield* fs.readFileString(sidecar), "shadow-sidecar"); + }).pipe( + Effect.provide(CodexManagedTokenBroker.layer), + Effect.provideService(HostProcess.Arguments, [ + process.execPath, + NodeURL.fileURLToPath(new URL("../bin.ts", import.meta.url)), + ]), + Effect.provideService(ServerSecretStore.ServerSecretStore, secrets), + Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, { + getEnvironmentId: Effect.succeed( + EnvironmentId.make("00000000-0000-4000-8000-000000000001"), + ), + }), + Effect.provide(layerInstaller), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("fresh credentials must not require HTTP")), + ), + ); + }).pipe( + Effect.scoped, + Effect.provide( + ServerConfig.layerTest(process.cwd(), { + prefix: "t3-managed-runtime-sqlite-conflict-", + }).pipe(Layer.provideMerge(NodeServices.layer)), + ), + ), +); diff --git a/apps/server/src/provider/CodexManagedRuntime.ts b/apps/server/src/provider/CodexManagedRuntime.ts index 3704c919c2791..9b49996e60534 100644 --- a/apps/server/src/provider/CodexManagedRuntime.ts +++ b/apps/server/src/provider/CodexManagedRuntime.ts @@ -11,7 +11,7 @@ import { resolveSelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeR import * as CodexInstallation from "./CodexInstallation.ts"; import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts"; import { COMMAND_TIMEOUT_MS } from "./CodexManagedTokenProtocol.ts"; -import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts"; +import { codexHomeEnvironment, materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts"; export interface CodexEffectiveRuntime { readonly config: CodexSettings; @@ -91,7 +91,8 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func environmentId, profileKey, ]); - yield* materializeCodexShadowHome(homeLayout).pipe( + const homeEnvironment = yield* materializeCodexShadowHome(homeLayout).pipe( + Effect.andThen(() => codexHomeEnvironment(homeLayout, options.environment)), Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), Effect.mapError( @@ -100,6 +101,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func instanceId: options.instanceId, operation: "runtime", detail: cause.message, + cause, }), ), ); @@ -115,7 +117,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/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.ts index c047336ef5dfc..a1178e7eb25c7 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.test.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts @@ -40,6 +40,7 @@ import * as CodexAdapterV2 from "../../orchestration-v2/Adapters/CodexAdapterV2. import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as ProviderCredentialStore from "../ProviderCredentialStore.ts"; +import { resolveManagedCodexHomeLayout } from "../CodexManagedHome.ts"; import * as ProviderHostLive from "../ProviderHostLive.ts"; const layerDeps = ServerConfig.layerTest(process.cwd(), { @@ -103,6 +104,112 @@ const noSpawn = ChildProcessSpawner.make(() => const encodeCredentials = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); it.layer(layerTest)("CodexDriver", (it) => { + it.effect.skipIf(!windowsHost)( + "surfaces managed SQLite recovery while preserving authenticated provider state", + () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("sqlite-conflict-managed-account"); + const credentials = new Map(); + const secrets = ServerSecretStore.ServerSecretStore.of({ + get: (key) => Effect.sync(() => Option.fromUndefinedOr(credentials.get(key))), + set: (key, value) => + Effect.sync(() => { + credentials.set(key, value); + }), + remove: (key) => + Effect.sync(() => { + credentials.delete(key); + }), + create: () => Effect.die("unused"), + getOrCreateRandom: () => Effect.die("unused"), + }); + + yield* Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const serverConfig = yield* ServerConfig.ServerConfig; + const installation = yield* CodexInstallation.CodexInstallation; + const sharedHome = NodePath.join(serverConfig.stateDir, "sqlite-conflict-shared-home"); + yield* fs.makeDirectory(sharedHome, { recursive: true }); + yield* fs.writeFileString(NodePath.join(sharedHome, "config.toml"), "# shared config\n"); + const config = { + ...CodexDriver.defaultConfig(), + setupMode: "managed" as const, + homePath: sharedHome, + }; + const layout = yield* resolveManagedCodexHomeLayout( + serverConfig.stateDir, + instanceId, + config, + ); + const shadowHome = layout.effectiveHomePath!; + yield* fs.makeDirectory(shadowHome, { recursive: true }); + const databaseName = "thread_history_1.sqlite"; + const database = NodePath.join(sharedHome, databaseName); + const linkedDatabase = NodePath.join(shadowHome, databaseName); + const sidecar = NodePath.join(shadowHome, `${databaseName}-shm`); + yield* fs.writeFileString(database, "shared-database"); + yield* fs.symlink(database, linkedDatabase); + yield* fs.writeFileString(sidecar, "shadow-sidecar"); + + const store = yield* ProviderCredentialStore.make("codex-chatgpt", instanceId); + const json = yield* encodeCredentials({ + clientId: "oaiapp_test", + accessToken: "dummy-owned-access", + refreshToken: "dummy-refresh", + expiresAt: Number.MAX_SAFE_INTEGER, + earliestRefreshAt: null, + scopes: ["chatgpt.tokens.use.direct"], + subject: "test-user", + email: "account@example.test", + }); + yield* store.set(new TextEncoder().encode(json)); + + const executable = { + executablePath: "/user/bin/codex", + managedVersionDirectory: null, + source: "local" as const, + version: "0.166.0", + }; + const instance = yield* CodexDriver.create({ + instanceId, + displayName: "SQLite conflict account", + enabled: true, + environment: [], + config, + }).pipe( + Effect.provideService( + CodexInstallation.CodexInstallation, + CodexInstallation.CodexInstallation.of({ + ...installation, + managedDirectory: "unused-managed-installation", + resolve: () => Effect.succeed(executable), + acquire: () => Effect.succeed(executable), + }), + ), + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make(() => Effect.die("SQLite conflict must fail before spawn")), + ), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("SQLite conflict must fail before HTTP")), + ), + ); + + const snapshot = yield* instance.snapshot.refresh; + + expect(snapshot.status).toBe("error"); + expect(snapshot.installed).toBe(true); + expect(snapshot.auth.status).toBe("authenticated"); + expect(snapshot.auth.email).toBe("account@example.test"); + expect(snapshot.models).toEqual([]); + expect(snapshot.message).toContain("Codex SQLite recovery is required"); + expect(yield* fs.readLink(linkedDatabase)).toBe(database); + expect(yield* fs.readFileString(sidecar)).toBe("shadow-sidecar"); + }).pipe(Effect.provideService(ServerSecretStore.ServerSecretStore, secrets)); + }), + ); + const checkManagedAccount = (replacementTiming: "unchanged" | "during-probe") => Effect.gen(function* () { const instanceId = ProviderInstanceId.make("restored-managed-account"); diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index 61f26c3f8cc7d..ed4e064e00a54 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -46,6 +46,7 @@ import { } from "@t3tools/provider-core/server/snapshotSettings"; import { codexContinuationIdentity, + codexHomeEnvironment, materializeCodexShadowHome, resolveCodexHomeLayout, } from "./CodexHomeLayout.ts"; @@ -150,11 +151,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 homeLayout = yield* resolveCodexHomeLayout( !config.homePath.trim() && !config.shadowHomePath.trim() && environmentHome?.trim() ? { ...config, homePath: environmentHome } @@ -168,7 +169,8 @@ export const CodexDriver: ProviderDriver< accentColor, continuationGroupKey: continuationIdentity.continuationKey, }); - yield* materializeCodexShadowHome(homeLayout).pipe( + const processEnv = yield* materializeCodexShadowHome(homeLayout).pipe( + Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), 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..9466dc80cf747 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -1,3 +1,5 @@ +// @effect-diagnostics nodeBuiltinImport:off -- SQLite is used only in disposable test homes. +import * as NodeSqlite from "node:sqlite"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; @@ -9,10 +11,14 @@ 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 windowsHost = HostProcess.Platform.defaultValue() === "win32"; const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings); const decodeCodexSettings = (input: { @@ -363,5 +369,253 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { ); }), ); + + it.effect.skipIf(!symlinksSupported || !windowsHost)( + "refuses a dirty legacy SQLite overlay before materialization mutates other entries", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const database = path.join(sharedHome, "thread_history_1.sqlite"); + const linkedDatabase = path.join(shadowHome, "thread_history_1.sqlite"); + const sidecar = path.join(shadowHome, "thread_history_1.sqlite-shm"); + const modelsCache = path.join(shadowHome, "models_cache.json"); + const sharedModelsCache = path.join(sharedHome, "models_cache.json"); + yield* writeTextFile(database, "shared-database"); + yield* writeTextFile(sharedModelsCache, "shared-cache"); + yield* fileSystem.symlink(database, linkedDatabase); + yield* fileSystem.symlink(sharedModelsCache, modelsCache); + yield* writeTextFile(sidecar, "shadow-sidecar"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + expect(yield* fileSystem.readLink(modelsCache)).toBe(sharedModelsCache); + expect(yield* fileSystem.readFileString(sidecar)).toBe("shadow-sidecar"); + expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); + }), + ); + }); + + describe("codexHomeEnvironment", () => { + it.effect.skipIf(!symlinksSupported)( + "routes clean shadow homes to the shared SQLite directory without mirroring databases", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + yield* writeTextFile(path.join(sharedHome, "thread_history_1.sqlite"), "database"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + yield* materializeCodexShadowHome(layout); + const environment = yield* codexHomeEnvironment(layout, {}); + + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + expect(yield* fileSystem.exists(path.join(shadowHome, "thread_history_1.sqlite"))).toBe( + false, + ); + }), + ); + + it.effect.skipIf(!symlinksSupported || !windowsHost)( + "routes a clean legacy SQLite link to the shared home without deleting the link", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const database = path.join(sharedHome, "state_5.sqlite"); + const linkedDatabase = path.join(shadowHome, "state_5.sqlite"); + yield* writeTextFile(database, "shared-database"); + yield* fileSystem.symlink(database, linkedDatabase); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout); + const environment = yield* codexHomeEnvironment(layout, {}); + + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + }), + ); + + it.effect.skipIf(!symlinksSupported || !windowsHost)( + "refuses a legacy SQLite link that points outside the shared home", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const otherHome = yield* makeTempDir("t3code-codex-other-"); + const entryName = "state_5.sqlite"; + const wrongTarget = path.join(otherHome, entryName); + const linkedDatabase = path.join(shadowHome, entryName); + yield* writeTextFile(wrongTarget, "other-database"); + yield* fileSystem.symlink(wrongTarget, linkedDatabase); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName }); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(wrongTarget); + }), + ); + + it.effect("defaults a blank SQLite override to the canonical shared home", () => + Effect.gen(function* () { + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const environment = yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: " " }); + + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + }), + ); + + it.effect("preserves an explicit nonblank SQLite override", () => + Effect.gen(function* () { + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const environment = yield* codexHomeEnvironment(layout, { + CODEX_SQLITE_HOME: ` ${customHome} `, + }); + + expect(environment.CODEX_SQLITE_HOME).toBe(customHome); + }), + ); + + it.effect.skipIf(!symlinksSupported || !windowsHost)( + "refuses a legacy linked database with a shadow-local SHM without changing either file", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const databaseName = "thread_history_1.sqlite"; + const database = path.join(sharedHome, databaseName); + const linkedDatabase = path.join(shadowHome, databaseName); + const sidecar = path.join(shadowHome, `${databaseName}-shm`); + yield* writeTextFile(database, "shared-database"); + yield* fileSystem.symlink(database, linkedDatabase); + yield* writeTextFile(sidecar, "shadow-sidecar"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: databaseName }); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + expect(yield* fileSystem.readFileString(sidecar)).toBe("shadow-sidecar"); + expect(yield* fileSystem.readFileString(database)).toBe("shared-database"); + }), + ); + + it.effect.skipIf(!symlinksSupported || !windowsHost)( + "preserves a real uncheckpointed shadow WAL when rejecting a legacy linked database", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const database = path.join(sharedHome, "thread_history_1.sqlite"); + const linkedDatabase = path.join(shadowHome, "thread_history_1.sqlite"); + yield* Effect.sync(() => new NodeSqlite.DatabaseSync(database).close()); + yield* fileSystem.symlink(database, linkedDatabase); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + yield* Effect.gen(function* () { + const writer = yield* Effect.acquireRelease( + Effect.sync(() => new NodeSqlite.DatabaseSync(linkedDatabase)), + (db) => Effect.sync(() => db.close()), + ); + writer.exec( + "PRAGMA journal_mode=WAL; PRAGMA wal_autocheckpoint=0; CREATE TABLE writes (value TEXT); INSERT INTO writes VALUES ('legacy');", + ); + const wal = path.join(shadowHome, "thread_history_1.sqlite-wal"); + const shm = path.join(shadowHome, "thread_history_1.sqlite-shm"); + const walBefore = yield* fileSystem.readFile(wal); + const shmBefore = yield* fileSystem.readFile(shm); + const databaseBefore = yield* fileSystem.readFile(database); + + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(yield* fileSystem.readFile(wal)).toEqual(walBefore); + expect(yield* fileSystem.readFile(shm)).toEqual(shmBefore); + expect(yield* fileSystem.readFile(database)).toEqual(databaseBefore); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + expect(writer.prepare("SELECT value FROM writes").all()).toEqual([{ value: "legacy" }]); + }).pipe(Effect.scoped); + }), + ); + + it.effect( + "keeps an existing private SQLite family in the shadow home instead of discarding its WAL", + () => + Effect.gen(function* () { + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private-database"); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), "private-wal"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const environment = yield* codexHomeEnvironment(layout, {}); + + expect(environment.CODEX_SQLITE_HOME).toBe(shadowHome); + }), + ); + + it.effect.skipIf(!windowsHost)( + "refuses an orphan shadow SQLite sidecar rather than silently abandoning it", + () => + Effect.gen(function* () { + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + yield* writeTextFile(path.join(shadowHome, "goals_1.SQLITE-WAL"), "orphan-wal"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: "goals_1.SQLITE-WAL" }); + }), + ); }); }); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index f6d2ba9d44cf5..ca2bcbaf32853 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -33,6 +33,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_SIDECAR_ENTRY_NAME = /^(.*\.sqlite)-(?:shm|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", @@ -116,6 +119,18 @@ export class CodexShadowHomeEntryConflictError extends Schema.TaggedError()( + "CodexShadowHomeSqliteConflictError", + { + ...CodexShadowHomeContext, + entryName: Schema.String, + }, +) { + override get message(): string { + return `Codex SQLite recovery is required for shadow-home entry '${this.entryName}'. Stop all Codex processes using this provider, back up the shadow home including SQLite sidecars, and recover the legacy SQLite state before retrying. No SQLite files were changed.`; + } +} + export class CodexShadowHomePrivateEntrySymlinkError extends Schema.TaggedError()( "CodexShadowHomePrivateEntrySymlinkError", { @@ -133,6 +148,7 @@ export const CodexShadowHomeError = Schema.Union([ CodexShadowHomeFileSystemError, CodexShadowHomePathConflictError, CodexShadowHomeEntryConflictError, + CodexShadowHomeSqliteConflictError, CodexShadowHomePrivateEntrySymlinkError, ]); export type CodexShadowHomeError = typeof CodexShadowHomeError.Type; @@ -190,6 +206,105 @@ const readLinkState = Effect.fn("CodexHomeLayout.readLinkState")(function* (inpu ); }); +export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironment")(function* ( + layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv, +) { + const effectiveHomePath = layout.effectiveHomePath; + if (layout.mode !== "authOverlay" || !effectiveHomePath) return environment; + + const override = environment.CODEX_SQLITE_HOME?.trim(); + if (override) return { ...environment, CODEX_SQLITE_HOME: override }; + + const fileSystem = yield* FileSystem.FileSystem; + const entries = yield* fileSystem.readDirectory(effectiveHomePath).pipe( + Effect.catchTags({ + PlatformError: (cause) => + cause.reason._tag === "NotFound" + ? Effect.succeed>([]) + : new CodexShadowHomeFileSystemError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + operation: "readDirectory", + path: effectiveHomePath, + cause, + }), + }), + ); + const sqliteEntries = entries.filter((entry) => SQLITE_ENTRY_NAME.test(entry)); + + // Unix resolves database symlinks before journal placement, so preserve existing overlays. + if (!(yield* HostProcess.isWindows)) { + return { + ...environment, + CODEX_SQLITE_HOME: sqliteEntries.length > 0 ? effectiveHomePath : layout.sharedHomePath, + }; + } + + const path = yield* Path.Path; + const regularDatabases = new Set(); + const regularSidecars: Array<{ readonly entryName: string; readonly databaseName: string }> = []; + let linkedEntry: string | undefined; + let hasLocalSqlite = false; + for (const entryName of sqliteEntries) { + const linkPath = path.join(effectiveHomePath, entryName); + const state = yield* readLinkState({ + fileSystem, + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + linkPath, + }); + if (state._tag === "Symlink") { + linkedEntry ??= entryName; + if ( + path.resolve(effectiveHomePath, state.target).toLowerCase() !== + path.join(layout.sharedHomePath, entryName).toLowerCase() + ) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + }); + } + continue; + } + if (state._tag !== "NotSymlink") continue; + hasLocalSqlite = true; + if (SQLITE_DATABASE_ENTRY_NAME.test(entryName)) { + regularDatabases.add(entryName.toLowerCase()); + continue; + } + const sidecar = SQLITE_SIDECAR_ENTRY_NAME.exec(entryName); + if (sidecar?.[1]) { + regularSidecars.push({ entryName, databaseName: sidecar[1].toLowerCase() }); + } + } + + if (linkedEntry && hasLocalSqlite) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: linkedEntry, + }); + } + + for (const sidecar of regularSidecars) { + if (!regularDatabases.has(sidecar.databaseName)) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: sidecar.entryName, + }); + } + } + + return { + ...environment, + CODEX_SQLITE_HOME: hasLocalSqlite ? effectiveHomePath : layout.sharedHomePath, + }; +}); + const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { readonly fileSystem: FileSystem.FileSystem; readonly sharedHomePath: string; @@ -363,6 +478,9 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + // Refuse dangerous legacy SQLite layouts before materialization mutates any shadow-home entry. + yield* codexHomeEnvironment(layout, {}).pipe(Effect.asVoid); + const makeDirectory = (directoryPath: string) => fileSystem.makeDirectory(directoryPath, { recursive: true }).pipe( Effect.catchTags({ @@ -402,7 +520,11 @@ 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) + ) { entries.add(entryName); } } diff --git a/apps/server/src/provider/Drivers/CodexManagedProvider.ts b/apps/server/src/provider/Drivers/CodexManagedProvider.ts index f9728f9a24973..99ae777bfbdd2 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,15 @@ import { type ProviderDriverCreateInput, type ProviderInstance, } from "@t3tools/provider-core/server/driver"; -import { codexContinuationIdentity } from "./CodexHomeLayout.ts"; +import { + CodexShadowHomeSqliteConflictError, + codexContinuationIdentity, +} 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); +const isCodexShadowHomeSqliteConflictError = Schema.is(CodexShadowHomeSqliteConflictError); export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(function* ( input: ProviderDriverCreateInput, @@ -179,11 +185,16 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu : {}), }), ), - Effect.catch(() => - runtime.auth.read.pipe( + Effect.catch((cause) => { + const sqliteConflict = + isProviderSetupError(cause) && isCodexShadowHomeSqliteConflictError(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 +215,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( From f3d14d2e7e6ca4e1ae3c0e5f9d99994091a06b1f Mon Sep 17 00:00:00 2001 From: Navneet Chaudhary Date: Tue, 3 Nov 2026 02:38:21 +0530 Subject: [PATCH 02/17] fix(server): reject legacy Codex SQLite links --- .../Adapters/CodexAdapterV2.ts | 2 +- .../src/provider/CodexManagedRuntime.test.ts | 3 +- .../src/provider/CodexManagedRuntime.ts | 2 +- .../src/provider/Drivers/CodexDriver.ts | 2 +- .../provider/Drivers/CodexHomeLayout.test.ts | 69 ++++++++++++++++--- .../src/provider/Drivers/CodexHomeLayout.ts | 50 +++++--------- 6 files changed, 83 insertions(+), 45 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index f4e6bad74934f..4393e67207592 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1601,7 +1601,7 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter ); const processEnv = hooks.resolveRuntime === undefined - ? yield* materializeCodexShadowHome(homeLayout).pipe( + ? yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe( Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), Effect.mapError( (cause) => diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 893ad88061c7e..949bff7616058 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -1,6 +1,7 @@ // @effect-diagnostics nodeBuiltinImport:off - checks the host's default Codex directory without writing to it. import * as NodeURL from "node:url"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Stream from "effect/Stream"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts"; @@ -284,7 +285,7 @@ it.live.each( ), ); -it.live( +it.live.skipIf(!symlinksSupported)( "managed additional account preserves dirty legacy SQLite state and returns a structured setup error", () => Effect.gen(function* () { diff --git a/apps/server/src/provider/CodexManagedRuntime.ts b/apps/server/src/provider/CodexManagedRuntime.ts index 9b49996e60534..96ea6e59a94fb 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, ]); - const homeEnvironment = yield* materializeCodexShadowHome(homeLayout).pipe( + const homeEnvironment = yield* materializeCodexShadowHome(homeLayout, options.environment).pipe( Effect.andThen(() => codexHomeEnvironment(homeLayout, options.environment)), Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index ed4e064e00a54..bb78470122b94 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -169,7 +169,7 @@ export const CodexDriver: ProviderDriver< accentColor, continuationGroupKey: continuationIdentity.continuationKey, }); - const processEnv = yield* materializeCodexShadowHome(homeLayout).pipe( + const processEnv = yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe( Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), Effect.mapError( (cause) => diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 9466dc80cf747..000555785694e 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -370,7 +370,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect.skipIf(!symlinksSupported || !windowsHost)( + it.effect.skipIf(!symlinksSupported)( "refuses a dirty legacy SQLite overlay before materialization mutates other entries", () => Effect.gen(function* () { @@ -427,8 +427,8 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect.skipIf(!symlinksSupported || !windowsHost)( - "routes a clean legacy SQLite link to the shared home without deleting the link", + it.effect.skipIf(!symlinksSupported)( + "refuses a clean legacy SQLite link instead of silently redirecting it", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -443,16 +443,16 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), ); - yield* materializeCodexShadowHome(layout); - yield* materializeCodexShadowHome(layout); - const environment = yield* codexHomeEnvironment(layout, {}); + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: "state_5.sqlite" }); expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + expect(yield* fileSystem.readFileString(database)).toBe("shared-database"); }), ); - it.effect.skipIf(!symlinksSupported || !windowsHost)( + it.effect.skipIf(!symlinksSupported)( "refuses a legacy SQLite link that points outside the shared home", () => Effect.gen(function* () { @@ -509,7 +509,58 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect.skipIf(!symlinksSupported || !windowsHost)( + it.effect( + "materialization honors an explicit SQLite override without deleting dormant sidecars", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); + const sidecar = path.join(shadowHome, "state_5.sqlite-wal"); + yield* writeTextFile(sidecar, "legacy-wal"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + const launchEnvironment = { CODEX_SQLITE_HOME: customHome }; + + yield* materializeCodexShadowHome(layout, launchEnvironment); + const environment = yield* codexHomeEnvironment(layout, launchEnvironment); + + expect(environment.CODEX_SQLITE_HOME).toBe(customHome); + expect(yield* fileSystem.readFileString(sidecar)).toBe("legacy-wal"); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "explicit SQLite overrides still refuse legacy database symlinks", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); + const database = path.join(sharedHome, "state_5.sqlite"); + const linkedDatabase = path.join(shadowHome, "state_5.sqlite"); + yield* writeTextFile(database, "shared-database"); + yield* fileSystem.symlink(database, linkedDatabase); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* materializeCodexShadowHome(layout, { + CODEX_SQLITE_HOME: customHome, + }).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: "state_5.sqlite" }); + expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); + }), + ); + + it.effect.skipIf(!symlinksSupported)( "refuses a legacy linked database with a shadow-local SHM without changing either file", () => Effect.gen(function* () { diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index ca2bcbaf32853..4068495d051f1 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -213,9 +213,6 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm const effectiveHomePath = layout.effectiveHomePath; if (layout.mode !== "authOverlay" || !effectiveHomePath) return environment; - const override = environment.CODEX_SQLITE_HOME?.trim(); - if (override) return { ...environment, CODEX_SQLITE_HOME: override }; - const fileSystem = yield* FileSystem.FileSystem; const entries = yield* fileSystem.readDirectory(effectiveHomePath).pipe( Effect.catchTags({ @@ -232,20 +229,11 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm }), ); const sqliteEntries = entries.filter((entry) => SQLITE_ENTRY_NAME.test(entry)); - - // Unix resolves database symlinks before journal placement, so preserve existing overlays. - if (!(yield* HostProcess.isWindows)) { - return { - ...environment, - CODEX_SQLITE_HOME: sqliteEntries.length > 0 ? effectiveHomePath : layout.sharedHomePath, - }; - } - const path = yield* Path.Path; const regularDatabases = new Set(); const regularSidecars: Array<{ readonly entryName: string; readonly databaseName: string }> = []; - let linkedEntry: string | undefined; let hasLocalSqlite = false; + for (const entryName of sqliteEntries) { const linkPath = path.join(effectiveHomePath, entryName); const state = yield* readLinkState({ @@ -256,18 +244,11 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm linkPath, }); if (state._tag === "Symlink") { - linkedEntry ??= entryName; - if ( - path.resolve(effectiveHomePath, state.target).toLowerCase() !== - path.join(layout.sharedHomePath, entryName).toLowerCase() - ) { - return yield* new CodexShadowHomeSqliteConflictError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - entryName, - }); - } - continue; + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName, + }); } if (state._tag !== "NotSymlink") continue; hasLocalSqlite = true; @@ -281,12 +262,16 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm } } - if (linkedEntry && hasLocalSqlite) { - return yield* new CodexShadowHomeSqliteConflictError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - entryName: linkedEntry, - }); + const override = environment.CODEX_SQLITE_HOME?.trim(); + if (override) return { ...environment, CODEX_SQLITE_HOME: override }; + + // Unix resolves regular database paths before journal placement. Existing private SQLite + // families can therefore remain private, but legacy database symlinks are rejected above. + if (!(yield* HostProcess.isWindows)) { + return { + ...environment, + CODEX_SQLITE_HOME: hasLocalSqlite ? effectiveHomePath : layout.sharedHomePath, + }; } for (const sidecar of regularSidecars) { @@ -464,6 +449,7 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv = {}, ) { if (layout.mode !== "authOverlay") return; const effectiveHomePath = layout.effectiveHomePath; @@ -479,7 +465,7 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" const path = yield* Path.Path; // Refuse dangerous legacy SQLite layouts before materialization mutates any shadow-home entry. - yield* codexHomeEnvironment(layout, {}).pipe(Effect.asVoid); + yield* codexHomeEnvironment(layout, environment).pipe(Effect.asVoid); const makeDirectory = (directoryPath: string) => fileSystem.makeDirectory(directoryPath, { recursive: true }).pipe( From a09bf0e0a794ca8e1ea9ded2d9fbc78a2bfbfcc4 Mon Sep 17 00:00:00 2001 From: Navneet Chaudhary Date: Tue, 3 Nov 2026 12:11:47 +0530 Subject: [PATCH 03/17] test(server): fix Codex SQLite regression setup --- apps/server/src/provider/CodexManagedRuntime.test.ts | 2 +- apps/server/src/provider/Drivers/CodexDriver.test.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 21c2e5f309e0f..49b2ab496d1e0 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -388,7 +388,7 @@ it.live.skipIf(!symlinksSupported)( Effect.provide( ServerConfig.layerTest(process.cwd(), { prefix: "t3-managed-runtime-sqlite-conflict-", - }).pipe(Layer.provideMerge(NodeServices.layer)), + }).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ), ), ); diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.ts index 1dbad2f376318..bb27e34fc7c68 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.test.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts @@ -9,6 +9,7 @@ import * as NodePath from "node:path"; import { expect, it } from "@effect/vitest"; import { EnvironmentId, ProviderInstanceId, ProviderSessionId, ThreadId } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Deferred from "effect/Deferred"; @@ -105,7 +106,7 @@ const noSpawn = ChildProcessSpawner.make(() => const encodeCredentials = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); it.layer(layerTest)("CodexDriver", (it) => { - it.effect.skipIf(!windowsHost)( + it.effect.skipIf(!windowsHost || !symlinksSupported)( "surfaces managed SQLite recovery while preserving authenticated provider state", () => Effect.gen(function* () { From 88f5927444de8d989676cc5d591021470a00c7a9 Mon Sep 17 00:00:00 2001 From: Navneet Chaudhary Date: Tue, 3 Nov 2026 12:20:34 +0530 Subject: [PATCH 04/17] test(server): cover Codex SQLite recovery across hosts --- apps/server/src/provider/Drivers/CodexDriver.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.ts index bb27e34fc7c68..52402d4869537 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.test.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts @@ -106,7 +106,7 @@ const noSpawn = ChildProcessSpawner.make(() => const encodeCredentials = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); it.layer(layerTest)("CodexDriver", (it) => { - it.effect.skipIf(!windowsHost || !symlinksSupported)( + it.effect.skipIf(!symlinksSupported)( "surfaces managed SQLite recovery while preserving authenticated provider state", () => Effect.gen(function* () { From 0dc5a542bba7954680bbc27e32a30e074f2da8d6 Mon Sep 17 00:00:00 2001 From: Navneet <155615649+nkcbuilds@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:35:16 +0530 Subject: [PATCH 05/17] fix(server): read host env via HostProcess in Codex SQLite test The dirty-legacy SQLite regression test was reading process.env inside Effect.gen, which effect-tsgo rejects (TS377077). Match the other managed-runtime tests and take PATH/SystemRoot from HostProcess.Environment. --- apps/server/src/provider/CodexManagedRuntime.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 8003f5456da20..225b00cfbd62d 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -337,13 +337,14 @@ it.live.skipIf(!symlinksSupported)( }); yield* store.set(new TextEncoder().encode(json)); + const hostEnv = yield* HostProcess.Environment; const runtime = yield* makeCodexManagedRuntime({ instanceId, enabled: true, config: decodeSettings({ setupMode: "managed", homePath: sharedHome }), environment: { - PATH: process.env.PATH, - SystemRoot: process.env.SystemRoot, + PATH: hostEnv.PATH, + SystemRoot: hostEnv.SystemRoot, }, }); assert.equal(runtime.homeLayout.mode, "authOverlay"); From efc11f702c0e81f0aedea02b51630b60d3f78c87 Mon Sep 17 00:00:00 2001 From: Navneet Chaudhary Date: Sun, 8 Nov 2026 12:34:04 +0530 Subject: [PATCH 06/17] fix(server): keep Codex SQLite state safe in provider terminals Route provider-bound terminals through canonical SQLite storage, reject mixed or overlapping homes, and preserve actionable recovery guidance. Serialize concurrent shadow setup and cover Windows path aliases, overrides, and legacy state with focused regressions. Prepared with GPT-6 through ChatGPT using the T3/MoonDesk harness. --- .../src/provider/CodexManagedRuntime.test.ts | 2 +- .../provider/Drivers/CodexHomeLayout.test.ts | 181 +++++++++- .../src/provider/Drivers/CodexHomeLayout.ts | 170 ++++++++-- apps/server/src/serverSettings.test.ts | 1 + apps/server/src/terminal/Manager.test.ts | 315 +++++++++++++++++- apps/server/src/terminal/Manager.ts | 50 ++- packages/contracts/src/terminal.test.ts | 18 + packages/contracts/src/terminal.ts | 6 +- 8 files changed, 692 insertions(+), 51 deletions(-) diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index 225b00cfbd62d..a3e1f31ac203c 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -132,7 +132,7 @@ it.live.each( } else { assert.equal(effective.environment.CODEX_SQLITE_HOME, sharedHome); assert.include(effective.config.homePath, instanceId); - assert.include(effective.config.homePath, "userdata/providers/codex"); + assert.include(effective.config.homePath, path.join("userdata", "providers", "codex")); assert.equal(runtime.homeLayout.mode, "authOverlay"); assert.equal( yield* fs.readLink(path.join(effective.config.homePath, "sessions")), diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 000555785694e..02bceab391715 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -71,6 +71,103 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }); describe("materializeCodexShadowHome", () => { + it.effect.each(["shadow-within-shared", "shared-within-shadow"] as const)( + "refuses overlapping home directories before making links (%s)", + (overlap) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* makeTempDir("t3code-codex-overlap-"); + const sharedHome = + overlap === "shadow-within-shared" + ? path.join(root, "shared") + : path.join(root, "shadow", "shared"); + const shadowHome = + overlap === "shadow-within-shared" + ? path.join(root, "shared", "shadow") + : path.join(root, "shadow"); + const config = decodeCodexSettings({ + homePath: sharedHome, + shadowHomePath: shadowHome, + }); + const layout = yield* resolveCodexHomeLayout(config); + + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); + expect(yield* fileSystem.exists(path.join(shadowHome, "sessions"))).toBe(false); + expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "refuses homes that point to the same directory through a symlink alias", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* makeTempDir("t3code-codex-home-alias-"); + const sharedHome = path.join(root, "shared"); + const shadowHome = path.join(root, "alias-to-shared"); + yield* fileSystem.makeDirectory(sharedHome); + yield* fileSystem.symlink(sharedHome, shadowHome); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); + expect(yield* fileSystem.readDirectory(sharedHome)).toEqual([]); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "rejects a missing shadow child beneath an alias of the shared home", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* makeTempDir("t3code-codex-missing-alias-"); + const sharedHome = path.join(root, "shared"); + const aliasedParent = path.join(root, "shared-alias"); + yield* fileSystem.makeDirectory(sharedHome); + yield* fileSystem.symlink(sharedHome, aliasedParent); + const shadowHome = path.join(aliasedParent, "fresh-shadow"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); + expect(yield* fileSystem.exists(path.join(sharedHome, "fresh-shadow"))).toBe(false); + }), + ); + + it.effect.skipIf(!symlinksSupported)( + "materializes the same fresh shadow safely under concurrent setup", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* makeTempDir("t3code-codex-materialize-race-"); + const sharedHome = path.join(root, "shared"); + const shadowHome = path.join(root, "shadow"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const outcomes = yield* Effect.all( + Array.from({ length: 8 }, () => materializeCodexShadowHome(layout).pipe(Effect.exit)), + { concurrency: "unbounded" }, + ); + expect(outcomes.map((outcome) => outcome._tag)).toEqual(Array(8).fill("Success")); + expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe( + path.join(sharedHome, "sessions"), + ); + }), + ); + it.effect.skipIf(!symlinksSupported)( "keeps Finder metadata local across repeated shadow home setup", () => @@ -331,7 +428,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { effectiveHomePath: sharedHome, }); expect(error.message).toBe( - `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`, + `Codex shadow home path '${sharedHome}' and shared home path '${sharedHome}' must be separate, non-overlapping directories (including filesystem aliases).`, ); }), ); @@ -650,14 +747,18 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect.skipIf(!windowsHost)( - "refuses an orphan shadow SQLite sidecar rather than silently abandoning it", + it.effect( + "refuses mixed shared and private SQLite state instead of silently choosing the shadow", () => Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const sharedHome = yield* makeTempDir("t3code-codex-shared-"); const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - yield* writeTextFile(path.join(shadowHome, "goals_1.SQLITE-WAL"), "orphan-wal"); + const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); + const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); + yield* writeTextFile(sharedDatabase, "shared-state"); + yield* writeTextFile(privateDatabase, "private-logs"); const layout = yield* resolveCodexHomeLayout( decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), ); @@ -665,8 +766,78 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName: "goals_1.SQLITE-WAL" }); + expect(error).toMatchObject({ entryName: "logs_2.sqlite" }); + expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); + expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); }), ); + + it.effect("rejects mixed SQLite state before materialization modifies either home", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); + const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); + yield* writeTextFile(sharedDatabase, "shared-state"); + yield* writeTextFile(privateDatabase, "private-logs"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); + expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); + expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); + expect(yield* fileSystem.exists(path.join(shadowHome, "sessions"))).toBe(false); + }), + ); + + it.effect( + "lets an explicit SQLite override resolve otherwise mixed regular state without mutation", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); + const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); + const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); + yield* writeTextFile(sharedDatabase, "shared-state"); + yield* writeTextFile(privateDatabase, "private-logs"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const environment = yield* codexHomeEnvironment(layout, { + CODEX_SQLITE_HOME: customHome, + }); + + expect(environment.CODEX_SQLITE_HOME).toBe(customHome); + expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); + expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); + }), + ); + + it.effect("refuses an orphan shadow SQLite sidecar rather than silently abandoning it", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + yield* writeTextFile(path.join(shadowHome, "goals_1.SQLITE-WAL"), "orphan-wal"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ entryName: "goals_1.SQLITE-WAL" }); + }), + ); }); }); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 4068495d051f1..7189f21b2cb4d 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -4,6 +4,7 @@ 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 Semaphore from "effect/Semaphore"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -83,7 +84,14 @@ export class CodexShadowHomeFileSystemError extends Schema.TaggedError(); const regularSidecars: Array<{ readonly entryName: string; readonly databaseName: string }> = []; - let hasLocalSqlite = false; + let firstLocalSqliteEntry: string | undefined; for (const entryName of sqliteEntries) { const linkPath = path.join(effectiveHomePath, entryName); @@ -251,7 +259,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm }); } if (state._tag !== "NotSymlink") continue; - hasLocalSqlite = true; + firstLocalSqliteEntry ??= entryName; if (SQLITE_DATABASE_ENTRY_NAME.test(entryName)) { regularDatabases.add(entryName.toLowerCase()); continue; @@ -265,15 +273,6 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm const override = environment.CODEX_SQLITE_HOME?.trim(); if (override) return { ...environment, CODEX_SQLITE_HOME: override }; - // Unix resolves regular database paths before journal placement. Existing private SQLite - // families can therefore remain private, but legacy database symlinks are rejected above. - if (!(yield* HostProcess.isWindows)) { - return { - ...environment, - CODEX_SQLITE_HOME: hasLocalSqlite ? effectiveHomePath : layout.sharedHomePath, - }; - } - for (const sidecar of regularSidecars) { if (!regularDatabases.has(sidecar.databaseName)) { return yield* new CodexShadowHomeSqliteConflictError({ @@ -284,10 +283,33 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm } } - return { - ...environment, - CODEX_SQLITE_HOME: hasLocalSqlite ? effectiveHomePath : layout.sharedHomePath, - }; + if (firstLocalSqliteEntry === undefined) { + return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; + } + + const sharedEntries = yield* fileSystem.readDirectory(layout.sharedHomePath).pipe( + Effect.catchTags({ + PlatformError: (cause) => + cause.reason._tag === "NotFound" + ? Effect.succeed>([]) + : new CodexShadowHomeFileSystemError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + operation: "readDirectory", + path: layout.sharedHomePath, + cause, + }), + }), + ); + if (sharedEntries.some((entry) => SQLITE_ENTRY_NAME.test(entry))) { + return yield* new CodexShadowHomeSqliteConflictError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + entryName: firstLocalSqliteEntry, + }); + } + + return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; }); const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { @@ -335,8 +357,28 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu const createLink = input.fileSystem.symlink(target, link).pipe( Effect.catchTags({ - PlatformError: (cause) => - new CodexShadowHomeFileSystemError({ + PlatformError: Effect.fn("CodexHomeLayout.ensureSymlink.onExists")(function* ( + cause: PlatformError.PlatformError, + ) { + if (cause.reason._tag === "AlreadyExists") { + const concurrentState = yield* readLinkState({ ...input, linkPath: link }); + if ( + concurrentState._tag === "Symlink" && + path.resolve(path.dirname(link), concurrentState.target) === target + ) { + return; + } + if (concurrentState._tag === "NotSymlink") { + return yield* new CodexShadowHomeEntryConflictError({ + sharedHomePath: input.sharedHomePath, + effectiveHomePath: input.effectiveHomePath, + entryName: input.entryName, + linkPath: link, + targetPath: target, + }); + } + } + return yield* new CodexShadowHomeFileSystemError({ sharedHomePath: input.sharedHomePath, effectiveHomePath: input.effectiveHomePath, operation: "symlink", @@ -344,7 +386,8 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu targetPath: target, entryName: input.entryName, cause, - }), + }); + }), }), ); @@ -447,23 +490,88 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP }, ); -export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( +const shadowHomeMaterializationLeases = new Map< + string, + { readonly semaphore: Semaphore.Semaphore; users: number } +>(); + +const withShadowHomeMaterializationLease = ( + shadowHomePath: string, + effect: Effect.Effect, +): Effect.Effect => + Effect.suspend(() => { + const lease = shadowHomeMaterializationLeases.get(shadowHomePath) ?? { + semaphore: Semaphore.makeUnsafe(1), + users: 0, + }; + shadowHomeMaterializationLeases.set(shadowHomePath, lease); + lease.users++; + return lease.semaphore.withPermit(effect).pipe( + Effect.ensuring( + Effect.sync(() => { + lease.users--; + if (lease.users === 0) shadowHomeMaterializationLeases.delete(shadowHomePath); + }), + ), + ); + }); + +const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* ( layout: CodexHomeLayout, environment: NodeJS.ProcessEnv = {}, ) { if (layout.mode !== "authOverlay") return; const effectiveHomePath = layout.effectiveHomePath; if (!effectiveHomePath) return; - if (layout.sharedHomePath === effectiveHomePath) { + const path = yield* Path.Path; + const fileSystem = yield* FileSystem.FileSystem; + const canonicalHome = Effect.fn("CodexHomeLayout.canonicalHome")(function* (directory: string) { + let ancestor = directory; + const missingSegments: string[] = []; + while (true) { + const existing = yield* fileSystem.realPath(ancestor).pipe( + Effect.catchTags({ + PlatformError: (cause) => + cause.reason._tag === "NotFound" + ? Effect.succeed(null) + : new CodexShadowHomeFileSystemError({ + sharedHomePath: layout.sharedHomePath, + effectiveHomePath, + operation: "realPath", + path: ancestor, + cause, + }), + }), + ); + if (existing !== null) return path.resolve(existing, ...missingSegments.reverse()); + const parent = path.dirname(ancestor); + if (parent === ancestor) return directory; + missingSegments.push(path.basename(ancestor)); + ancestor = parent; + } + }); + const isSameOrNestedHome = (parent: string, child: string) => { + const relative = path.relative(parent, child); + return ( + relative === "" || + (relative !== ".." && + !relative.startsWith("../") && + !relative.startsWith("..\\") && + !path.isAbsolute(relative)) + ); + }; + const actualSharedHome = yield* canonicalHome(layout.sharedHomePath); + const actualShadowHome = yield* canonicalHome(effectiveHomePath); + if ( + isSameOrNestedHome(actualSharedHome, actualShadowHome) || + isSameOrNestedHome(actualShadowHome, actualSharedHome) + ) { return yield* new CodexShadowHomePathConflictError({ sharedHomePath: layout.sharedHomePath, effectiveHomePath, }); } - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - // Refuse dangerous legacy SQLite layouts before materialization mutates any shadow-home entry. yield* codexHomeEnvironment(layout, environment).pipe(Effect.asVoid); @@ -552,6 +660,16 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" }); }); +export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( + layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv = {}, +) { + return yield* withShadowHomeMaterializationLease( + layout.effectiveHomePath ?? layout.sharedHomePath, + materializeShadowHome(layout, environment), + ); +}); + export function codexContinuationIdentity(layout: CodexHomeLayout) { return { driverKind: ProviderDriverKind.make("codex"), diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index caba93a788b7f..4fca9f20c811f 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -1924,6 +1924,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" const environment = yield* resolveProviderInstanceTerminalEnvironment({ 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 d89316cc74951..373b05e994730 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -12,6 +12,7 @@ import { TerminalProviderInstanceNotFoundError, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; @@ -1990,29 +1991,309 @@ it.layer( }, ])("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) => Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const serverSettings = yield* ServerSettings.ServerSettingsService; - const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - path, - rawProviderInstanceId: "configured_home", - env: undefined, + const tempRoot = + "shadowHomePath" in config + ? yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-terminal-home-pref-" }) + : undefined; + const effectiveConfig = tempRoot + ? { homePath: path.join(tempRoot, "shared"), shadowHomePath: path.join(tempRoot, "shadow") } + : config; + const environment = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "configured_home", + env: undefined, + }); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("configured_home")]: { + driver: ProviderDriverKind.make(driver), + environment: [ + { name: variable, value: "~/.environment-account", sensitive: false }, + ], + config: effectiveConfig, + }, + }, + }), + ), + ); + + expect(environment[variable]).toBe( + tempRoot ? effectiveConfig.shadowHomePath : path.resolve(expectedHome), + ); + }), + ); + + it.effect( + "materializes fresh Codex shadow terminals before selecting the shared SQLite home", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-fresh-", + }); + const sharedHome = path.join(root, "shared"); + const shadowHome = path.join(root, "shadow"); + const environment = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: undefined, + }); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + config: { homePath: sharedHome, shadowHomePath: shadowHome }, + }, + }, + }), + ), + ); + + expect(environment.CODEX_HOME).toBe(shadowHome); + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + expect(yield* fileSystem.exists(shadowHome)).toBe(true); + expect(yield* fileSystem.exists(sharedHome)).toBe(true); + expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe( + path.join(sharedHome, "sessions"), + ); + }).pipe(Effect.provideService(HostProcess.Environment, {})), + ); + + it.effect.each([ + { + name: "host", + instanceEnvironment: undefined, + clientEnvironment: undefined, + hostEnvironment: { codex_sqlite_home: "/sqlite/host" }, + expected: "/sqlite/host", + }, + { + name: "client", + instanceEnvironment: undefined, + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/client", + }, + { + name: "instance", + instanceEnvironment: [ + { name: "CODEX_SQLITE_HOME", value: "/sqlite/instance", sensitive: false }, + ], + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/instance", + }, + { + name: "lowercase instance on Windows", + instanceEnvironment: [ + { name: "codex_sqlite_home", value: "/sqlite/lowercase-instance", sensitive: false }, + ], + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/lowercase-instance", + }, + ])( + "uses the $name CODEX_SQLITE_HOME override for Codex shadow terminals", + ({ instanceEnvironment, clientEnvironment, hostEnvironment, expected }) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-override-", + }); + const environment = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: clientEnvironment, + }); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + ...(instanceEnvironment ? { environment: instanceEnvironment } : {}), + config: { + homePath: path.join(root, "shared"), + shadowHomePath: path.join(root, "shadow"), + }, + }, + }, + }), + ), + ); + + expect(environment.CODEX_SQLITE_HOME).toBe(expected); + }).pipe( + Effect.provideService(HostProcess.Environment, hostEnvironment), + Effect.provideService(HostProcess.Platform, "win32"), + ), + ); + + it.effect("honors a Windows lowercase CODEX_SQLITE_HOME client override", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-case-", }); + const customHome = path.join(root, "custom-sqlite"); + const environment = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: { codex_sqlite_home: customHome }, + }); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + config: { + homePath: path.join(root, "shared"), + shadowHomePath: path.join(root, "shadow"), + }, + }, + }, + }), + ), + ); - expect(environment[variable]).toBe(path.resolve(expectedHome)); + expect(environment.CODEX_SQLITE_HOME).toBe(customHome); + expect( + Object.keys(environment).filter((key) => key.toLowerCase() === "codex_sqlite_home"), + ).toEqual(["CODEX_SQLITE_HOME"]); }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("configured_home")]: { - driver: ProviderDriverKind.make(driver), - environment: [{ name: variable, value: "~/.environment-account", sensitive: false }], - config, + Effect.provideService(HostProcess.Platform, "win32"), + Effect.provideService(HostProcess.Environment, { CODEX_SQLITE_HOME: "/sqlite/host" }), + ), + ); + + it.effect("refuses a provider terminal with mixed shared and private SQLite state", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-mixed-", + }); + const sharedHome = path.join(root, "shared"); + const shadowHome = path.join(root, "shadow"); + yield* fileSystem.makeDirectory(sharedHome, { recursive: true }); + yield* fileSystem.makeDirectory(shadowHome, { recursive: true }); + const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); + const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); + yield* fileSystem.writeFileString(sharedDatabase, "shared-state"); + yield* fileSystem.writeFileString(privateDatabase, "private-logs"); + + const error = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: undefined, + }).pipe(Effect.flip); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + config: { homePath: sharedHome, shadowHomePath: shadowHome }, + }, }, + }), + ), + ); + + expect(error).toMatchObject({ + _tag: "TerminalProviderEnvironmentError", + cause: { + _tag: "CodexShadowHomeSqliteConflictError", + entryName: "logs_2.sqlite", + }, + }); + expect(error.message).toContain("Codex SQLite recovery is required"); + expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); + expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); + }).pipe(Effect.provideService(HostProcess.Environment, {})), + ); + + it.effect.skipIf(!symlinksSupported)( + "refuses a provider-bound terminal for a legacy Codex SQLite symlink", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-legacy-", + }); + const sharedHome = path.join(root, "shared"); + const shadowHome = path.join(root, "shadow"); + const database = path.join(sharedHome, "state_5.sqlite"); + yield* fileSystem.makeDirectory(sharedHome, { recursive: true }); + yield* fileSystem.makeDirectory(shadowHome, { recursive: true }); + yield* fileSystem.writeFileString(database, "shared-state"); + yield* fileSystem.symlink(database, path.join(shadowHome, "state_5.sqlite")); + + const error = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: undefined, + }).pipe(Effect.flip); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + config: { homePath: sharedHome, shadowHomePath: shadowHome }, + }, + }, + }), + ), + ); + + expect(error).toMatchObject({ + _tag: "TerminalProviderEnvironmentError", + cause: { + _tag: "CodexShadowHomeSqliteConflictError", + entryName: "state_5.sqlite", }, - }), - ), - ), + }); + expect(error.message).toContain("Codex SQLite recovery is required"); + expect(error.message).toContain(sharedHome); + expect(error.message).toContain(shadowHome); + expect(error.message).toContain("reconnecting alone cannot clear this refusal"); + }).pipe(Effect.provideService(HostProcess.Environment, {})), ); it.effect("restarts a running terminal when the resolved provider environment changes", () => diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 3ee43635f45c5..3933dfd500f25 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -64,7 +64,11 @@ import * as SynchronizedRef from "effect/SynchronizedRef"; import * as ServerConfig from "../config.ts"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; -import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts"; +import { + codexHomeEnvironment, + materializeCodexShadowHome, + resolveCodexHomeLayout, +} from "../provider/Drivers/CodexHomeLayout.ts"; import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts"; import { deriveProviderInstanceConfigMap } from "../provider/ProviderInstanceRegistryHydration.ts"; import * as ServerSettings from "../serverSettings.ts"; @@ -1436,6 +1440,7 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( "terminal.resolveProviderInstanceTerminalEnvironment", )(function* (input: { readonly serverSettings: ServerSettings.ServerSettingsService["Service"]; + readonly fileSystem: FileSystem.FileSystem; readonly path: Path.Path; readonly rawProviderInstanceId: string; readonly env: Record | undefined; @@ -1458,6 +1463,47 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( ); if (layout.effectiveHomePath) resolved = { ...resolved, CODEX_HOME: layout.effectiveHomePath }; + if (layout.mode === "authOverlay") { + const hostEnv = yield* HostProcess.Environment; + const clientEnv = input.env ?? {}; + const isWindows = (yield* HostProcess.Platform) === "win32"; + const sqliteOverride = (environment: NodeJS.ProcessEnv) => + Object.entries(environment).find(([name]) => + isWindows ? name.toUpperCase() === "CODEX_SQLITE_HOME" : name === "CODEX_SQLITE_HOME", + )?.[1]; + // Preserve client-over-host precedence even when Windows supplies a different casing. + const selectedOverride = sqliteOverride(clientEnv) ?? sqliteOverride(hostEnv); + const inherited = yield* mergeProviderInstanceEnvironment(instance.environment, { + ...hostEnv, + ...clientEnv, + ...(selectedOverride !== undefined ? { CODEX_SQLITE_HOME: selectedOverride } : {}), + }); + const sqliteEnvironment = yield* Effect.gen(function* () { + yield* materializeCodexShadowHome(layout, inherited); + return yield* codexHomeEnvironment(layout, inherited); + }).pipe( + Effect.mapError( + (cause) => + new TerminalProviderEnvironmentError({ + providerInstanceId, + ...(cause._tag === "CodexShadowHomeSqliteConflictError" + ? { detail: cause.message } + : {}), + cause, + }), + ), + Effect.provideService(FileSystem.FileSystem, input.fileSystem), + Effect.provideService(Path.Path, input.path), + ); + resolved = { + ...Object.fromEntries( + Object.entries(resolved).filter( + ([key]) => !isWindows || key.toUpperCase() !== "CODEX_SQLITE_HOME", + ), + ), + CODEX_SQLITE_HOME: sqliteEnvironment.CODEX_SQLITE_HOME, + }; + } } } else if (instance.driver === "claudeAgent") { const config = decodeClaudeSettings(instance.config ?? {}); @@ -1480,12 +1526,14 @@ 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", )((rawProviderInstanceId: string, env: Record | undefined) => resolveProviderInstanceTerminalEnvironment({ serverSettings, + fileSystem, path, rawProviderInstanceId, env, diff --git a/packages/contracts/src/terminal.test.ts b/packages/contracts/src/terminal.test.ts index dda8060195fcf..621b7f18c464f 100644 --- a/packages/contracts/src/terminal.test.ts +++ b/packages/contracts/src/terminal.test.ts @@ -36,6 +36,24 @@ describe("TerminalProviderEnvironmentError", () => { ); expect(decoded.message).not.toContain("secret backend unavailable"); }); + + it("round-trips an explicit safe detail as the public message", () => { + const detail = "Codex SQLite recovery is required before this terminal can start."; + const error = new TerminalProviderEnvironmentError({ + providerInstanceId: ProviderInstanceId.make("codex_work"), + detail, + cause: { _tag: "CodexShadowHomeSqliteConflictError" }, + }); + + const decoded = decodeTerminalError(encodeTerminalError(error)); + + expect(decoded).toMatchObject({ + _tag: "TerminalProviderEnvironmentError", + providerInstanceId: "codex_work", + detail, + }); + expect(decoded.message).toBe(detail); + }); }); describe("TerminalOpenInput", () => { diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.ts index 8731d6ec25c8f..83c50bb8f8313 100644 --- a/packages/contracts/src/terminal.ts +++ b/packages/contracts/src/terminal.ts @@ -314,11 +314,15 @@ export class TerminalProviderEnvironmentError extends Schema.TaggedError Date: Sun, 8 Nov 2026 14:02:19 +0530 Subject: [PATCH 07/17] test(server): guard Codex shadow tests when symlinks are unavailable Only run SQLite override and shadow-terminal materialization cases on hosts with symlink support. Keep safe preflight and non-overlay tests active on all platforms. Prepared with GPT-6 through ChatGPT using the T3/MoonDesk harness. --- .../provider/Drivers/CodexHomeLayout.test.ts | 2 +- apps/server/src/terminal/Manager.test.ts | 196 +++++++++--------- 2 files changed, 102 insertions(+), 96 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 02bceab391715..344348ebfa77f 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -606,7 +606,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect( + it.effect.skipIf(!symlinksSupported)( "materialization honors an explicit SQLite override without deleting dormant sidecars", () => Effect.gen(function* () { diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 373b05e994730..0ba6984e439b9 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -1967,29 +1967,31 @@ it.layer( }), ); - it.effect.each([ - { - name: "Codex home", - driver: "codex", - variable: "CODEX_HOME", - config: { homePath: "/configured/codex" }, - expectedHome: "/configured/codex", - }, - { - name: "Codex shadow home", - driver: "codex", - variable: "CODEX_HOME", - config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" }, - expectedHome: "/configured/codex-shadow", - }, - { - name: "Claude home", - driver: "claudeAgent", - variable: "CLAUDE_CONFIG_DIR", - config: { homePath: "/configured/claude" }, - expectedHome: "/configured/claude", - }, - ])("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) => + it.effect.each( + [ + { + name: "Codex home", + driver: "codex", + variable: "CODEX_HOME", + config: { homePath: "/configured/codex" }, + expectedHome: "/configured/codex", + }, + { + name: "Codex shadow home", + driver: "codex", + variable: "CODEX_HOME", + config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" }, + expectedHome: "/configured/codex-shadow", + }, + { + name: "Claude home", + driver: "claudeAgent", + variable: "CLAUDE_CONFIG_DIR", + config: { homePath: "/configured/claude" }, + expectedHome: "/configured/claude", + }, + ].filter(({ config }) => symlinksSupported || !("shadowHomePath" in config)), + )("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -2031,7 +2033,7 @@ it.layer( }), ); - it.effect( + it.effect.skipIf(!symlinksSupported)( "materializes fresh Codex shadow terminals before selecting the shared SQLite home", () => Effect.gen(function* () { @@ -2074,40 +2076,42 @@ it.layer( }).pipe(Effect.provideService(HostProcess.Environment, {})), ); - it.effect.each([ - { - name: "host", - instanceEnvironment: undefined, - clientEnvironment: undefined, - hostEnvironment: { codex_sqlite_home: "/sqlite/host" }, - expected: "/sqlite/host", - }, - { - name: "client", - instanceEnvironment: undefined, - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/client", - }, - { - name: "instance", - instanceEnvironment: [ - { name: "CODEX_SQLITE_HOME", value: "/sqlite/instance", sensitive: false }, - ], - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/instance", - }, - { - name: "lowercase instance on Windows", - instanceEnvironment: [ - { name: "codex_sqlite_home", value: "/sqlite/lowercase-instance", sensitive: false }, - ], - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/lowercase-instance", - }, - ])( + it.effect.each( + [ + { + name: "host", + instanceEnvironment: undefined, + clientEnvironment: undefined, + hostEnvironment: { codex_sqlite_home: "/sqlite/host" }, + expected: "/sqlite/host", + }, + { + name: "client", + instanceEnvironment: undefined, + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/client", + }, + { + name: "instance", + instanceEnvironment: [ + { name: "CODEX_SQLITE_HOME", value: "/sqlite/instance", sensitive: false }, + ], + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/instance", + }, + { + name: "lowercase instance on Windows", + instanceEnvironment: [ + { name: "codex_sqlite_home", value: "/sqlite/lowercase-instance", sensitive: false }, + ], + clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, + hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, + expected: "/sqlite/lowercase-instance", + }, + ].filter(() => symlinksSupported), + )( "uses the $name CODEX_SQLITE_HOME override for Codex shadow terminals", ({ instanceEnvironment, clientEnvironment, hostEnvironment, expected }) => Effect.gen(function* () { @@ -2149,47 +2153,49 @@ it.layer( ), ); - it.effect("honors a Windows lowercase CODEX_SQLITE_HOME client override", () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-case-", - }); - const customHome = path.join(root, "custom-sqlite"); - const environment = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: { codex_sqlite_home: customHome }, + it.effect.skipIf(!symlinksSupported)( + "honors a Windows lowercase CODEX_SQLITE_HOME client override", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-terminal-codex-case-", }); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - config: { - homePath: path.join(root, "shared"), - shadowHomePath: path.join(root, "shadow"), + const customHome = path.join(root, "custom-sqlite"); + const environment = yield* Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + serverSettings, + fileSystem, + path, + rawProviderInstanceId: "codex_shadow", + env: { codex_sqlite_home: customHome }, + }); + }).pipe( + Effect.provide( + ServerSettings.layerTest({ + providerInstances: { + [ProviderInstanceId.make("codex_shadow")]: { + driver: ProviderDriverKind.make("codex"), + config: { + homePath: path.join(root, "shared"), + shadowHomePath: path.join(root, "shadow"), + }, }, }, - }, - }), - ), - ); + }), + ), + ); - expect(environment.CODEX_SQLITE_HOME).toBe(customHome); - expect( - Object.keys(environment).filter((key) => key.toLowerCase() === "codex_sqlite_home"), - ).toEqual(["CODEX_SQLITE_HOME"]); - }).pipe( - Effect.provideService(HostProcess.Platform, "win32"), - Effect.provideService(HostProcess.Environment, { CODEX_SQLITE_HOME: "/sqlite/host" }), - ), + expect(environment.CODEX_SQLITE_HOME).toBe(customHome); + expect( + Object.keys(environment).filter((key) => key.toLowerCase() === "codex_sqlite_home"), + ).toEqual(["CODEX_SQLITE_HOME"]); + }).pipe( + Effect.provideService(HostProcess.Platform, "win32"), + Effect.provideService(HostProcess.Environment, { CODEX_SQLITE_HOME: "/sqlite/host" }), + ), ); it.effect("refuses a provider terminal with mixed shared and private SQLite state", () => From 9c28bf4bfa1b366d7c6e0dc1f832c84ea023aa07 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 20:26:55 +0000 Subject: [PATCH 08/17] fix(server): accept inert legacy Codex SQLite links and trim scope Legacy SQLite links in a shadow home are inert once Codex opens databases from CODEX_SQLITE_HOME, and on macOS and Linux SQLite already kept their sidecars beside the shared file. Refusing them would stop every existing shadow-home instance after upgrade. Refuse only a shadow WAL or journal that still holds data for a database it does not own. Drop the realpath alias check, in-process materialization lock and terminal shadow materialization, which the SQLite fix does not need, and cut tests to the routing cases. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/CodexAdapterV2.ts | 2 - .../src/provider/CodexManagedRuntime.test.ts | 121 +---- .../src/provider/CodexManagedRuntime.ts | 3 +- .../src/provider/Drivers/CodexDriver.test.ts | 108 ---- .../src/provider/Drivers/CodexDriver.ts | 2 - .../provider/Drivers/CodexHomeLayout.test.ts | 507 +++--------------- .../src/provider/Drivers/CodexHomeLayout.ts | 240 +++------ apps/server/src/terminal/Manager.test.ts | 350 ++---------- apps/server/src/terminal/Manager.ts | 33 +- packages/contracts/src/terminal.test.ts | 18 - 10 files changed, 182 insertions(+), 1202 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 00c4306e22b44..ef0d1efff487d 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -113,7 +113,6 @@ import { toMcpElicitationResponse, } from "../../provider/CodexMcpElicitation.ts"; import { - codexHomeEnvironment, materializeCodexShadowHome, resolveCodexHomeLayout, } from "../../provider/Drivers/CodexHomeLayout.ts"; @@ -1702,7 +1701,6 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter const processEnv = hooks.resolveRuntime === undefined ? yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe( - Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), Effect.mapError( (cause) => new ProviderAdapterDriverCreateError({ diff --git a/apps/server/src/provider/CodexManagedRuntime.test.ts b/apps/server/src/provider/CodexManagedRuntime.test.ts index a3e1f31ac203c..2c697f8fa8531 100644 --- a/apps/server/src/provider/CodexManagedRuntime.test.ts +++ b/apps/server/src/provider/CodexManagedRuntime.test.ts @@ -1,19 +1,13 @@ // @effect-diagnostics nodeBuiltinImport:off - checks the host's default Codex directory without writing to it. import * as NodeURL from "node:url"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Stream from "effect/Stream"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; -import { - CodexSettings, - EnvironmentId, - ProviderInstanceId, - ProviderSetupError, -} from "@t3tools/contracts"; +import { CodexSettings, EnvironmentId, ProviderInstanceId } from "@t3tools/contracts"; import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -29,7 +23,6 @@ import * as CodexInstallation from "./CodexInstallation.ts"; import { makeCodexManagedRuntime } from "./CodexManagedRuntime.ts"; import * as ProviderCredentialStore from "./ProviderCredentialStore.ts"; import { codexAppServerArgs } from "./codexLaunchArgs.ts"; -import { CodexShadowHomeSqliteConflictError } from "./Drivers/CodexHomeLayout.ts"; const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const decodeSettings = Schema.decodeSync(CodexSettings); @@ -128,11 +121,9 @@ it.live.each( if (account === "primary") { assert.equal(effective.config.homePath, sharedHome); assert.equal(runtime.homeLayout.mode, "direct"); - assert.isUndefined(effective.environment.CODEX_SQLITE_HOME); } else { - assert.equal(effective.environment.CODEX_SQLITE_HOME, sharedHome); assert.include(effective.config.homePath, instanceId); - assert.include(effective.config.homePath, path.join("userdata", "providers", "codex")); + assert.include(effective.config.homePath, "userdata/providers/codex"); assert.equal(runtime.homeLayout.mode, "authOverlay"); assert.equal( yield* fs.readLink(path.join(effective.config.homePath, "sessions")), @@ -286,111 +277,3 @@ it.live.each( ), ), ); - -it.live.skipIf(!symlinksSupported)( - "managed additional account preserves dirty legacy SQLite state and returns a structured setup error", - () => - Effect.gen(function* () { - const instanceId = ProviderInstanceId.make("codex-personal"); - const fs = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* fs.makeTempDirectoryScoped({ prefix: "codex-shared-home-" }); - yield* fs.writeFileString(path.join(sharedHome, "config.toml"), "# shared config\n"); - - const data = new Map(); - const secrets = ServerSecretStore.ServerSecretStore.of({ - get: (key) => Effect.sync(() => Option.fromUndefinedOr(data.get(key))), - set: (key, value) => - Effect.sync(() => { - data.set(key, value); - }), - remove: (key) => - Effect.sync(() => { - data.delete(key); - }), - create: () => Effect.die("unused"), - getOrCreateRandom: () => Effect.die("unused"), - }); - const executable = { - executablePath: "/user/bin/codex", - managedVersionDirectory: null, - source: "local" as const, - version: "0.166.0", - }; - const layerInstaller = Layer.mock(CodexInstallation.CodexInstallation)({ - managedDirectory: "/isolated/tools/codex", - resolve: () => Effect.succeed(executable), - acquire: () => Effect.succeed(executable), - }); - - yield* Effect.gen(function* () { - const store = yield* ProviderCredentialStore.make("codex-chatgpt", instanceId); - const json = yield* encodeJson({ - clientId: "oaiapp_test", - accessToken: "dummy-owned-access", - refreshToken: "dummy-refresh", - expiresAt: (yield* Clock.currentTimeMillis) + 3_600_000, - earliestRefreshAt: null, - scopes: ["chatgpt.tokens.use.direct"], - subject: "test-user", - email: null, - }); - yield* store.set(new TextEncoder().encode(json)); - - const hostEnv = yield* HostProcess.Environment; - const runtime = yield* makeCodexManagedRuntime({ - instanceId, - enabled: true, - config: decodeSettings({ setupMode: "managed", homePath: sharedHome }), - environment: { - PATH: hostEnv.PATH, - SystemRoot: hostEnv.SystemRoot, - }, - }); - assert.equal(runtime.homeLayout.mode, "authOverlay"); - const shadowHome = runtime.homePath; - yield* fs.makeDirectory(shadowHome, { recursive: true }); - - const databaseName = "thread_history_1.sqlite"; - const database = path.join(sharedHome, databaseName); - const linkedDatabase = path.join(shadowHome, databaseName); - const sidecar = path.join(shadowHome, `${databaseName}-shm`); - yield* fs.writeFileString(database, "shared-database"); - yield* fs.symlink(database, linkedDatabase); - yield* fs.writeFileString(sidecar, "shadow-sidecar"); - - const error = yield* runtime.resolve.pipe(Effect.flip); - - assert.instanceOf(error, ProviderSetupError); - assert.equal(error.operation, "runtime"); - assert.instanceOf(error.cause, CodexShadowHomeSqliteConflictError); - assert.include(error.detail, "Codex SQLite recovery is required"); - assert.equal(yield* fs.readLink(linkedDatabase), database); - assert.equal(yield* fs.readFileString(sidecar), "shadow-sidecar"); - }).pipe( - Effect.provide(CodexManagedTokenBroker.layer), - Effect.provideService(HostProcess.Arguments, [ - process.execPath, - NodeURL.fileURLToPath(new URL("../bin.ts", import.meta.url)), - ]), - Effect.provideService(ServerSecretStore.ServerSecretStore, secrets), - Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, { - getEnvironmentId: Effect.succeed( - EnvironmentId.make("00000000-0000-4000-8000-000000000001"), - ), - }), - Effect.provide(layerInstaller), - Effect.provideService( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("fresh credentials must not require HTTP")), - ), - ); - }).pipe( - Effect.scoped, - Effect.provide( - ServerConfig.layerTest(process.cwd(), { - prefix: "t3-managed-runtime-sqlite-conflict-", - }).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), - ), - ), -); diff --git a/apps/server/src/provider/CodexManagedRuntime.ts b/apps/server/src/provider/CodexManagedRuntime.ts index 96ea6e59a94fb..2f9292409ee6a 100644 --- a/apps/server/src/provider/CodexManagedRuntime.ts +++ b/apps/server/src/provider/CodexManagedRuntime.ts @@ -11,7 +11,7 @@ import { resolveSelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeR import * as CodexInstallation from "./CodexInstallation.ts"; import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts"; import { COMMAND_TIMEOUT_MS } from "./CodexManagedTokenProtocol.ts"; -import { codexHomeEnvironment, materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts"; +import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts"; export interface CodexEffectiveRuntime { readonly config: CodexSettings; @@ -92,7 +92,6 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func profileKey, ]); const homeEnvironment = yield* materializeCodexShadowHome(homeLayout, options.environment).pipe( - Effect.andThen(() => codexHomeEnvironment(homeLayout, options.environment)), Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), Effect.mapError( diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.ts index b61181979b586..597be85623115 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.test.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts @@ -9,7 +9,6 @@ import * as NodePath from "node:path"; import { expect, it } from "@effect/vitest"; import { EnvironmentId, ProviderInstanceId, ProviderSessionId, ThreadId } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Deferred from "effect/Deferred"; @@ -44,7 +43,6 @@ import * as CodexAdapterV2 from "../../orchestration-v2/Adapters/CodexAdapterV2. import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as ProviderCredentialStore from "../ProviderCredentialStore.ts"; -import { resolveManagedCodexHomeLayout } from "../CodexManagedHome.ts"; import * as ProviderHostLive from "../ProviderHostLive.ts"; const layerDeps = ServerConfig.layerTest(process.cwd(), { @@ -108,112 +106,6 @@ const noSpawn = ChildProcessSpawner.make(() => const encodeCredentials = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); it.layer(layerTest)("CodexDriver", (it) => { - it.effect.skipIf(!symlinksSupported)( - "surfaces managed SQLite recovery while preserving authenticated provider state", - () => - Effect.gen(function* () { - const instanceId = ProviderInstanceId.make("sqlite-conflict-managed-account"); - const credentials = new Map(); - const secrets = ServerSecretStore.ServerSecretStore.of({ - get: (key) => Effect.sync(() => Option.fromUndefinedOr(credentials.get(key))), - set: (key, value) => - Effect.sync(() => { - credentials.set(key, value); - }), - remove: (key) => - Effect.sync(() => { - credentials.delete(key); - }), - create: () => Effect.die("unused"), - getOrCreateRandom: () => Effect.die("unused"), - }); - - yield* Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const serverConfig = yield* ServerConfig.ServerConfig; - const installation = yield* CodexInstallation.CodexInstallation; - const sharedHome = NodePath.join(serverConfig.stateDir, "sqlite-conflict-shared-home"); - yield* fs.makeDirectory(sharedHome, { recursive: true }); - yield* fs.writeFileString(NodePath.join(sharedHome, "config.toml"), "# shared config\n"); - const config = { - ...CodexDriver.defaultConfig(), - setupMode: "managed" as const, - homePath: sharedHome, - }; - const layout = yield* resolveManagedCodexHomeLayout( - serverConfig.stateDir, - instanceId, - config, - ); - const shadowHome = layout.effectiveHomePath!; - yield* fs.makeDirectory(shadowHome, { recursive: true }); - const databaseName = "thread_history_1.sqlite"; - const database = NodePath.join(sharedHome, databaseName); - const linkedDatabase = NodePath.join(shadowHome, databaseName); - const sidecar = NodePath.join(shadowHome, `${databaseName}-shm`); - yield* fs.writeFileString(database, "shared-database"); - yield* fs.symlink(database, linkedDatabase); - yield* fs.writeFileString(sidecar, "shadow-sidecar"); - - const store = yield* ProviderCredentialStore.make("codex-chatgpt", instanceId); - const json = yield* encodeCredentials({ - clientId: "oaiapp_test", - accessToken: "dummy-owned-access", - refreshToken: "dummy-refresh", - expiresAt: Number.MAX_SAFE_INTEGER, - earliestRefreshAt: null, - scopes: ["chatgpt.tokens.use.direct"], - subject: "test-user", - email: "account@example.test", - }); - yield* store.set(new TextEncoder().encode(json)); - - const executable = { - executablePath: "/user/bin/codex", - managedVersionDirectory: null, - source: "local" as const, - version: "0.166.0", - }; - const instance = yield* CodexDriver.create({ - instanceId, - displayName: "SQLite conflict account", - enabled: true, - environment: [], - config, - }).pipe( - Effect.provideService( - CodexInstallation.CodexInstallation, - CodexInstallation.CodexInstallation.of({ - ...installation, - managedDirectory: "unused-managed-installation", - resolve: () => Effect.succeed(executable), - acquire: () => Effect.succeed(executable), - }), - ), - Effect.provideService( - ChildProcessSpawner.ChildProcessSpawner, - ChildProcessSpawner.make(() => Effect.die("SQLite conflict must fail before spawn")), - ), - Effect.provideService( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("SQLite conflict must fail before HTTP")), - ), - ); - - const snapshot = yield* instance.snapshot.refresh; - - expect(snapshot.status).toBe("error"); - expect(snapshot.installed).toBe(true); - expect(snapshot.auth.status).toBe("authenticated"); - expect(snapshot.auth.email).toBe("account@example.test"); - expect(snapshot.models).toEqual([]); - expect(snapshot.message).toContain("Codex SQLite recovery is required"); - expect(yield* fs.readLink(linkedDatabase)).toBe(database); - expect(yield* fs.readFileString(sidecar)).toBe("shadow-sidecar"); - }).pipe(Effect.provideService(ServerSecretStore.ServerSecretStore, secrets)); - }), - ); - const checkManagedAccount = (replacementTiming: "unchanged" | "during-probe") => Effect.gen(function* () { const instanceId = ProviderInstanceId.make("restored-managed-account"); diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index 7d6a2fe2e3657..9a9ca7c0cb67a 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -52,7 +52,6 @@ import { } from "@t3tools/provider-core/server/snapshotSettings"; import { codexContinuationIdentity, - codexHomeEnvironment, materializeCodexShadowHome, resolveCodexHomeLayout, } from "./CodexHomeLayout.ts"; @@ -204,7 +203,6 @@ export const CodexDriver: ProviderDriver< continuationGroupKey: continuationIdentity.continuationKey, }); const processEnv = yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe( - Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)), 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 344348ebfa77f..95605729dfb60 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -1,5 +1,3 @@ -// @effect-diagnostics nodeBuiltinImport:off -- SQLite is used only in disposable test homes. -import * as NodeSqlite from "node:sqlite"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; @@ -16,9 +14,7 @@ import { materializeCodexShadowHome, resolveCodexHomeLayout, } from "./CodexHomeLayout.ts"; -import * as HostProcess from "@t3tools/shared/HostProcess"; import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; -const windowsHost = HostProcess.Platform.defaultValue() === "win32"; const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings); const decodeCodexSettings = (input: { @@ -44,6 +40,15 @@ const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* yield* fileSystem.writeFileString(filePath, contents); }); +const makeShadowLayout = Effect.fn("CodexHomeLayout.test.makeShadowLayout")(function* () { + const sharedHome = yield* makeTempDir("t3code-codex-shared-"); + const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + const layout = yield* resolveCodexHomeLayout( + decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + ); + return { sharedHome, shadowHome, layout }; +}); + it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { describe("resolveCodexHomeLayout", () => { it.effect("uses the shared home for continuation and the shadow home for runtime", () => @@ -71,103 +76,6 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }); describe("materializeCodexShadowHome", () => { - it.effect.each(["shadow-within-shared", "shared-within-shadow"] as const)( - "refuses overlapping home directories before making links (%s)", - (overlap) => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* makeTempDir("t3code-codex-overlap-"); - const sharedHome = - overlap === "shadow-within-shared" - ? path.join(root, "shared") - : path.join(root, "shadow", "shared"); - const shadowHome = - overlap === "shadow-within-shared" - ? path.join(root, "shared", "shadow") - : path.join(root, "shadow"); - const config = decodeCodexSettings({ - homePath: sharedHome, - shadowHomePath: shadowHome, - }); - const layout = yield* resolveCodexHomeLayout(config); - - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); - expect(yield* fileSystem.exists(path.join(shadowHome, "sessions"))).toBe(false); - expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); - }), - ); - - it.effect.skipIf(!symlinksSupported)( - "refuses homes that point to the same directory through a symlink alias", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* makeTempDir("t3code-codex-home-alias-"); - const sharedHome = path.join(root, "shared"); - const shadowHome = path.join(root, "alias-to-shared"); - yield* fileSystem.makeDirectory(sharedHome); - yield* fileSystem.symlink(sharedHome, shadowHome); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); - expect(yield* fileSystem.readDirectory(sharedHome)).toEqual([]); - }), - ); - - it.effect.skipIf(!symlinksSupported)( - "rejects a missing shadow child beneath an alias of the shared home", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* makeTempDir("t3code-codex-missing-alias-"); - const sharedHome = path.join(root, "shared"); - const aliasedParent = path.join(root, "shared-alias"); - yield* fileSystem.makeDirectory(sharedHome); - yield* fileSystem.symlink(sharedHome, aliasedParent); - const shadowHome = path.join(aliasedParent, "fresh-shadow"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomePathConflictError); - expect(yield* fileSystem.exists(path.join(sharedHome, "fresh-shadow"))).toBe(false); - }), - ); - - it.effect.skipIf(!symlinksSupported)( - "materializes the same fresh shadow safely under concurrent setup", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* makeTempDir("t3code-codex-materialize-race-"); - const sharedHome = path.join(root, "shared"); - const shadowHome = path.join(root, "shadow"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const outcomes = yield* Effect.all( - Array.from({ length: 8 }, () => materializeCodexShadowHome(layout).pipe(Effect.exit)), - { concurrency: "unbounded" }, - ); - expect(outcomes.map((outcome) => outcome._tag)).toEqual(Array(8).fill("Success")); - expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe( - path.join(sharedHome, "sessions"), - ); - }), - ); - it.effect.skipIf(!symlinksSupported)( "keeps Finder metadata local across repeated shadow home setup", () => @@ -186,15 +94,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"); @@ -240,7 +148,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")); @@ -290,7 +198,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); expect(yield* fileSystem.readLink(shadowLock)).toBe(sharedLock); }), @@ -318,7 +226,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - yield* materializeCodexShadowHome(layout); + yield* materializeCodexShadowHome(layout, {}); const locksTarget = yield* fileSystem.readLink(shadowLocks); const sharedLockExists = yield* fileSystem.exists( @@ -353,11 +261,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"), ); @@ -390,7 +298,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 @@ -420,7 +328,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({ @@ -428,7 +336,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { effectiveHomePath: sharedHome, }); expect(error.message).toBe( - `Codex shadow home path '${sharedHome}' and shared home path '${sharedHome}' must be separate, non-overlapping directories (including filesystem aliases).`, + `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`, ); }), ); @@ -451,7 +359,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({ @@ -466,377 +374,90 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { ); }), ); - - it.effect.skipIf(!symlinksSupported)( - "refuses a dirty legacy SQLite overlay before materialization mutates other entries", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const database = path.join(sharedHome, "thread_history_1.sqlite"); - const linkedDatabase = path.join(shadowHome, "thread_history_1.sqlite"); - const sidecar = path.join(shadowHome, "thread_history_1.sqlite-shm"); - const modelsCache = path.join(shadowHome, "models_cache.json"); - const sharedModelsCache = path.join(sharedHome, "models_cache.json"); - yield* writeTextFile(database, "shared-database"); - yield* writeTextFile(sharedModelsCache, "shared-cache"); - yield* fileSystem.symlink(database, linkedDatabase); - yield* fileSystem.symlink(sharedModelsCache, modelsCache); - yield* writeTextFile(sidecar, "shadow-sidecar"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); - expect(yield* fileSystem.readLink(modelsCache)).toBe(sharedModelsCache); - expect(yield* fileSystem.readFileString(sidecar)).toBe("shadow-sidecar"); - expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); - }), - ); }); - describe("codexHomeEnvironment", () => { - it.effect.skipIf(!symlinksSupported)( - "routes clean shadow homes to the shared SQLite directory without mirroring databases", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - yield* writeTextFile(path.join(sharedHome, "thread_history_1.sqlite"), "database"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - yield* materializeCodexShadowHome(layout); - const environment = yield* codexHomeEnvironment(layout, {}); - - expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); - expect(yield* fileSystem.exists(path.join(shadowHome, "thread_history_1.sqlite"))).toBe( - false, - ); - }), - ); - - it.effect.skipIf(!symlinksSupported)( - "refuses a clean legacy SQLite link instead of silently redirecting it", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const database = path.join(sharedHome, "state_5.sqlite"); - const linkedDatabase = path.join(shadowHome, "state_5.sqlite"); - yield* writeTextFile(database, "shared-database"); - yield* fileSystem.symlink(database, linkedDatabase); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName: "state_5.sqlite" }); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); - expect(yield* fileSystem.readFileString(database)).toBe("shared-database"); - }), - ); - + describe("SQLite routing", () => { it.effect.skipIf(!symlinksSupported)( - "refuses a legacy SQLite link that points outside the shared home", + "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 = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const otherHome = yield* makeTempDir("t3code-codex-other-"); - const entryName = "state_5.sqlite"; - const wrongTarget = path.join(otherHome, entryName); - const linkedDatabase = path.join(shadowHome, entryName); - yield* writeTextFile(wrongTarget, "other-database"); - yield* fileSystem.symlink(wrongTarget, linkedDatabase); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); + 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 error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + const environment = yield* materializeCodexShadowHome(layout, { KEEP: "1" }); - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName }); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(wrongTarget); + expect(environment).toEqual({ KEEP: "1", CODEX_SQLITE_HOME: sharedHome }); + const shadowEntries = yield* fileSystem.readDirectory(shadowHome); + expect(shadowEntries.filter((entry) => entry.includes(".sqlite"))).toEqual([]); }), ); - it.effect("defaults a blank SQLite override to the canonical shared home", () => - Effect.gen(function* () { - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const environment = yield* codexHomeEnvironment(layout, { CODEX_SQLITE_HOME: " " }); - - expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); - }), - ); - - it.effect("preserves an explicit nonblank SQLite override", () => - Effect.gen(function* () { - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const environment = yield* codexHomeEnvironment(layout, { - CODEX_SQLITE_HOME: ` ${customHome} `, - }); - - expect(environment.CODEX_SQLITE_HOME).toBe(customHome); - }), - ); - it.effect.skipIf(!symlinksSupported)( - "materialization honors an explicit SQLite override without deleting dormant sidecars", + "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 = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); - const sidecar = path.join(shadowHome, "state_5.sqlite-wal"); - yield* writeTextFile(sidecar, "legacy-wal"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "db"); + yield* fileSystem.symlink( + path.join(sharedHome, "state_5.sqlite"), + path.join(shadowHome, "state_5.sqlite"), ); - const launchEnvironment = { CODEX_SQLITE_HOME: customHome }; - - yield* materializeCodexShadowHome(layout, launchEnvironment); - const environment = yield* codexHomeEnvironment(layout, launchEnvironment); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), ""); + yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-shm"), "index"); - expect(environment.CODEX_SQLITE_HOME).toBe(customHome); - expect(yield* fileSystem.readFileString(sidecar)).toBe("legacy-wal"); - }), - ); + const environment = yield* materializeCodexShadowHome(layout, {}); - it.effect.skipIf(!symlinksSupported)( - "explicit SQLite overrides still refuse legacy database symlinks", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); - const database = path.join(sharedHome, "state_5.sqlite"); - const linkedDatabase = path.join(shadowHome, "state_5.sqlite"); - yield* writeTextFile(database, "shared-database"); - yield* fileSystem.symlink(database, linkedDatabase); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); + expect(yield* fileSystem.readLink(path.join(shadowHome, "state_5.sqlite"))).toBe( + path.join(sharedHome, "state_5.sqlite"), ); - - const error = yield* materializeCodexShadowHome(layout, { - CODEX_SQLITE_HOME: customHome, - }).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName: "state_5.sqlite" }); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); }), ); it.effect.skipIf(!symlinksSupported)( - "refuses a legacy linked database with a shadow-local SHM without changing either file", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const databaseName = "thread_history_1.sqlite"; - const database = path.join(sharedHome, databaseName); - const linkedDatabase = path.join(shadowHome, databaseName); - const sidecar = path.join(shadowHome, `${databaseName}-shm`); - yield* writeTextFile(database, "shared-database"); - yield* fileSystem.symlink(database, linkedDatabase); - yield* writeTextFile(sidecar, "shadow-sidecar"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName: databaseName }); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); - expect(yield* fileSystem.readFileString(sidecar)).toBe("shadow-sidecar"); - expect(yield* fileSystem.readFileString(database)).toBe("shared-database"); - }), - ); - - it.effect.skipIf(!symlinksSupported || !windowsHost)( - "preserves a real uncheckpointed shadow WAL when rejecting a legacy linked database", + "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 = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const database = path.join(sharedHome, "thread_history_1.sqlite"); - const linkedDatabase = path.join(shadowHome, "thread_history_1.sqlite"); - yield* Effect.sync(() => new NodeSqlite.DatabaseSync(database).close()); - yield* fileSystem.symlink(database, linkedDatabase); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - yield* Effect.gen(function* () { - const writer = yield* Effect.acquireRelease( - Effect.sync(() => new NodeSqlite.DatabaseSync(linkedDatabase)), - (db) => Effect.sync(() => db.close()), - ); - writer.exec( - "PRAGMA journal_mode=WAL; PRAGMA wal_autocheckpoint=0; CREATE TABLE writes (value TEXT); INSERT INTO writes VALUES ('legacy');", - ); - const wal = path.join(shadowHome, "thread_history_1.sqlite-wal"); - const shm = path.join(shadowHome, "thread_history_1.sqlite-shm"); - const walBefore = yield* fileSystem.readFile(wal); - const shmBefore = yield* fileSystem.readFile(shm); - const databaseBefore = yield* fileSystem.readFile(database); - - const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(yield* fileSystem.readFile(wal)).toEqual(walBefore); - expect(yield* fileSystem.readFile(shm)).toEqual(shmBefore); - expect(yield* fileSystem.readFile(database)).toEqual(databaseBefore); - expect(yield* fileSystem.readLink(linkedDatabase)).toBe(database); - expect(writer.prepare("SELECT value FROM writes").all()).toEqual([{ value: "legacy" }]); - }).pipe(Effect.scoped); - }), - ); - - it.effect( - "keeps an existing private SQLite family in the shadow home instead of discarding its WAL", - () => - Effect.gen(function* () { - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - yield* writeTextFile(path.join(shadowHome, "state_5.sqlite"), "private-database"); - yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), "private-wal"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const environment = yield* codexHomeEnvironment(layout, {}); - - expect(environment.CODEX_SQLITE_HOME).toBe(shadowHome); - }), - ); - - it.effect( - "refuses mixed shared and private SQLite state instead of silently choosing the shadow", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); - const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); - yield* writeTextFile(sharedDatabase, "shared-state"); - yield* writeTextFile(privateDatabase, "private-logs"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), + 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* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ entryName: "logs_2.sqlite" }); - expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); - expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); + 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); + expect( + yield* materializeCodexShadowHome(layout, { CODEX_SQLITE_HOME: " /elsewhere " }), + ).toEqual({ CODEX_SQLITE_HOME: "/elsewhere" }); }), ); - it.effect("rejects mixed SQLite state before materialization modifies either home", () => + it.effect("keeps a shadow's own databases until the shared home has some too", () => Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); - const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); - yield* writeTextFile(sharedDatabase, "shared-state"); - yield* writeTextFile(privateDatabase, "private-logs"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip); - - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); - expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); - expect(yield* fileSystem.exists(path.join(sharedHome, "sessions"))).toBe(false); - expect(yield* fileSystem.exists(path.join(shadowHome, "sessions"))).toBe(false); - }), - ); - - it.effect( - "lets an explicit SQLite override resolve otherwise mixed regular state without mutation", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - const customHome = yield* makeTempDir("t3code-codex-custom-sqlite-"); - const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); - const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); - yield* writeTextFile(sharedDatabase, "shared-state"); - yield* writeTextFile(privateDatabase, "private-logs"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); - - const environment = yield* codexHomeEnvironment(layout, { - CODEX_SQLITE_HOME: customHome, - }); - - expect(environment.CODEX_SQLITE_HOME).toBe(customHome); - expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); - expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); - }), - ); + 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"); - it.effect("refuses an orphan shadow SQLite sidecar rather than silently abandoning it", () => - Effect.gen(function* () { - const path = yield* Path.Path; - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); - yield* writeTextFile(path.join(shadowHome, "goals_1.SQLITE-WAL"), "orphan-wal"); - const layout = yield* resolveCodexHomeLayout( - decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }), - ); + expect((yield* codexHomeEnvironment(layout, {})).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); - expect(error).toMatchObject({ entryName: "goals_1.SQLITE-WAL" }); }), ); }); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 7189f21b2cb4d..146c020c864fa 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -4,7 +4,7 @@ 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 Semaphore from "effect/Semaphore"; +import * as Option from "effect/Option"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -36,7 +36,7 @@ const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "auth.lock", "models_cache.jso 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_SIDECAR_ENTRY_NAME = /^(.*\.sqlite)-(?:shm|wal|journal)$/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", @@ -84,14 +84,7 @@ export class CodexShadowHomeFileSystemError extends Schema.TaggedError + fileSystem.readDirectory(directory).pipe( Effect.catchTags({ PlatformError: (cause) => cause.reason._tag === "NotFound" @@ -231,18 +222,37 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm sharedHomePath: layout.sharedHomePath, effectiveHomePath, operation: "readDirectory", - path: effectiveHomePath, + path: directory, cause, }), }), ); - const sqliteEntries = entries.filter((entry) => SQLITE_ENTRY_NAME.test(entry)); + +/** + * 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. + */ +export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironment")(function* ( + layout: CodexHomeLayout, + environment: NodeJS.ProcessEnv, +) { + const effectiveHomePath = layout.effectiveHomePath; + if (layout.mode !== "authOverlay" || !effectiveHomePath) return environment; + const override = environment.CODEX_SQLITE_HOME?.trim(); + if (override) return { ...environment, CODEX_SQLITE_HOME: override }; + + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const regularDatabases = new Set(); - const regularSidecars: Array<{ readonly entryName: string; readonly databaseName: string }> = []; - let firstLocalSqliteEntry: string | undefined; - - for (const entryName of sqliteEntries) { + 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, @@ -251,64 +261,47 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm entryName, linkPath, }); - if (state._tag === "Symlink") { - return yield* new CodexShadowHomeSqliteConflictError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - entryName, - }); - } if (state._tag !== "NotSymlink") continue; - firstLocalSqliteEntry ??= entryName; if (SQLITE_DATABASE_ENTRY_NAME.test(entryName)) { regularDatabases.add(entryName.toLowerCase()); continue; } - const sidecar = SQLITE_SIDECAR_ENTRY_NAME.exec(entryName); - if (sidecar?.[1]) { - regularSidecars.push({ entryName, databaseName: sidecar[1].toLowerCase() }); + // `-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() }); } } - const override = environment.CODEX_SQLITE_HOME?.trim(); - if (override) return { ...environment, CODEX_SQLITE_HOME: override }; - - for (const sidecar of regularSidecars) { - if (!regularDatabases.has(sidecar.databaseName)) { - return yield* new CodexShadowHomeSqliteConflictError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - entryName: sidecar.entryName, - }); - } + // 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 (firstLocalSqliteEntry === undefined) { + const [privateDatabase] = regularDatabases; + if (privateDatabase === undefined) { return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; } - - const sharedEntries = yield* fileSystem.readDirectory(layout.sharedHomePath).pipe( - Effect.catchTags({ - PlatformError: (cause) => - cause.reason._tag === "NotFound" - ? Effect.succeed>([]) - : new CodexShadowHomeFileSystemError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - operation: "readDirectory", - path: layout.sharedHomePath, - cause, - }), - }), + // A shadow that only ever had its own databases keeps them, until the shared home has some too. + const sharedEntries = yield* readHomeEntries( + fileSystem, + layout, + effectiveHomePath, + layout.sharedHomePath, ); - if (sharedEntries.some((entry) => SQLITE_ENTRY_NAME.test(entry))) { + if (sharedEntries.some((entry) => SQLITE_DATABASE_ENTRY_NAME.test(entry))) { return yield* new CodexShadowHomeSqliteConflictError({ sharedHomePath: layout.sharedHomePath, effectiveHomePath, - entryName: firstLocalSqliteEntry, + entryName: privateDatabase, }); } - return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; }); @@ -357,28 +350,8 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu const createLink = input.fileSystem.symlink(target, link).pipe( Effect.catchTags({ - PlatformError: Effect.fn("CodexHomeLayout.ensureSymlink.onExists")(function* ( - cause: PlatformError.PlatformError, - ) { - if (cause.reason._tag === "AlreadyExists") { - const concurrentState = yield* readLinkState({ ...input, linkPath: link }); - if ( - concurrentState._tag === "Symlink" && - path.resolve(path.dirname(link), concurrentState.target) === target - ) { - return; - } - if (concurrentState._tag === "NotSymlink") { - return yield* new CodexShadowHomeEntryConflictError({ - sharedHomePath: input.sharedHomePath, - effectiveHomePath: input.effectiveHomePath, - entryName: input.entryName, - linkPath: link, - targetPath: target, - }); - } - } - return yield* new CodexShadowHomeFileSystemError({ + PlatformError: (cause) => + new CodexShadowHomeFileSystemError({ sharedHomePath: input.sharedHomePath, effectiveHomePath: input.effectiveHomePath, operation: "symlink", @@ -386,8 +359,7 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu targetPath: target, entryName: input.entryName, cause, - }); - }), + }), }), ); @@ -490,90 +462,21 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP }, ); -const shadowHomeMaterializationLeases = new Map< - string, - { readonly semaphore: Semaphore.Semaphore; users: number } ->(); - -const withShadowHomeMaterializationLease = ( - shadowHomePath: string, - effect: Effect.Effect, -): Effect.Effect => - Effect.suspend(() => { - const lease = shadowHomeMaterializationLeases.get(shadowHomePath) ?? { - semaphore: Semaphore.makeUnsafe(1), - users: 0, - }; - shadowHomeMaterializationLeases.set(shadowHomePath, lease); - lease.users++; - return lease.semaphore.withPermit(effect).pipe( - Effect.ensuring( - Effect.sync(() => { - lease.users--; - if (lease.users === 0) shadowHomeMaterializationLeases.delete(shadowHomePath); - }), - ), - ); - }); - const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* ( layout: CodexHomeLayout, - environment: NodeJS.ProcessEnv = {}, ) { if (layout.mode !== "authOverlay") return; const effectiveHomePath = layout.effectiveHomePath; if (!effectiveHomePath) return; - const path = yield* Path.Path; - const fileSystem = yield* FileSystem.FileSystem; - const canonicalHome = Effect.fn("CodexHomeLayout.canonicalHome")(function* (directory: string) { - let ancestor = directory; - const missingSegments: string[] = []; - while (true) { - const existing = yield* fileSystem.realPath(ancestor).pipe( - Effect.catchTags({ - PlatformError: (cause) => - cause.reason._tag === "NotFound" - ? Effect.succeed(null) - : new CodexShadowHomeFileSystemError({ - sharedHomePath: layout.sharedHomePath, - effectiveHomePath, - operation: "realPath", - path: ancestor, - cause, - }), - }), - ); - if (existing !== null) return path.resolve(existing, ...missingSegments.reverse()); - const parent = path.dirname(ancestor); - if (parent === ancestor) return directory; - missingSegments.push(path.basename(ancestor)); - ancestor = parent; - } - }); - const isSameOrNestedHome = (parent: string, child: string) => { - const relative = path.relative(parent, child); - return ( - relative === "" || - (relative !== ".." && - !relative.startsWith("../") && - !relative.startsWith("..\\") && - !path.isAbsolute(relative)) - ); - }; - const actualSharedHome = yield* canonicalHome(layout.sharedHomePath); - const actualShadowHome = yield* canonicalHome(effectiveHomePath); - if ( - isSameOrNestedHome(actualSharedHome, actualShadowHome) || - isSameOrNestedHome(actualShadowHome, actualSharedHome) - ) { + if (layout.sharedHomePath === effectiveHomePath) { return yield* new CodexShadowHomePathConflictError({ sharedHomePath: layout.sharedHomePath, effectiveHomePath, }); } - // Refuse dangerous legacy SQLite layouts before materialization mutates any shadow-home entry. - yield* codexHomeEnvironment(layout, environment).pipe(Effect.asVoid); + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; const makeDirectory = (directoryPath: string) => fileSystem.makeDirectory(directoryPath, { recursive: true }).pipe( @@ -660,14 +563,15 @@ const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* }); }); +/** Prepares the shadow home and returns the environment Codex must launch with. */ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* ( layout: CodexHomeLayout, - environment: NodeJS.ProcessEnv = {}, + environment: NodeJS.ProcessEnv, ) { - return yield* withShadowHomeMaterializationLease( - layout.effectiveHomePath ?? layout.sharedHomePath, - materializeShadowHome(layout, environment), - ); + // Refuse a stranded SQLite layout before touching the shadow home. + const launchEnvironment = yield* codexHomeEnvironment(layout, environment); + yield* materializeShadowHome(layout); + return launchEnvironment; }); export function codexContinuationIdentity(layout: CodexHomeLayout) { diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index cfd6059fe18bf..cec0efda9e7fe 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -12,7 +12,6 @@ import { TerminalProviderInstanceNotFoundError, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; @@ -2028,50 +2027,47 @@ it.layer( }), ); - it.effect.each( - [ - { - name: "Codex home", - driver: "codex", - variable: "CODEX_HOME", - config: { homePath: "/configured/codex" }, - expectedHome: "/configured/codex", - }, - { - name: "Codex shadow home", - driver: "codex", - variable: "CODEX_HOME", - config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" }, - expectedHome: "/configured/codex-shadow", - }, - { - name: "Claude home", - driver: "claudeAgent", - variable: "CLAUDE_CONFIG_DIR", - config: { homePath: "/configured/claude" }, - expectedHome: "/configured/claude", - }, - ].filter(({ config }) => symlinksSupported || !("shadowHomePath" in config)), - )("prefers $name over the instance environment", ({ driver, variable, config, expectedHome }) => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const tempRoot = - "shadowHomePath" in config - ? yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-terminal-home-pref-" }) - : undefined; - const effectiveConfig = tempRoot - ? { homePath: path.join(tempRoot, "shared"), shadowHomePath: path.join(tempRoot, "shadow") } - : config; - const environment = yield* Effect.gen(function* () { + it.effect.each([ + { + name: "Codex home", + driver: "codex", + variable: "CODEX_HOME", + config: { homePath: "/configured/codex" }, + expectedHome: "/configured/codex", + }, + { + name: "Codex shadow home", + driver: "codex", + variable: "CODEX_HOME", + config: { homePath: "/configured/codex", shadowHomePath: "/configured/codex-shadow" }, + expectedHome: "/configured/codex-shadow", + expectedSqliteHome: "/configured/codex", + }, + { + name: "Claude home", + driver: "claudeAgent", + variable: "CLAUDE_CONFIG_DIR", + config: { homePath: "/configured/claude" }, + expectedHome: "/configured/claude", + }, + ])( + "prefers $name over the instance environment", + ({ driver, variable, config, expectedHome, expectedSqliteHome }) => + Effect.gen(function* () { + const path = yield* Path.Path; const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ + const environment = yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ serverSettings, - fileSystem, + 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({ @@ -2081,288 +2077,14 @@ it.layer( environment: [ { name: variable, value: "~/.environment-account", sensitive: false }, ], - config: effectiveConfig, + config, }, }, }), ), - ); - - expect(environment[variable]).toBe( - tempRoot ? effectiveConfig.shadowHomePath : path.resolve(expectedHome), - ); - }), - ); - - it.effect.skipIf(!symlinksSupported)( - "materializes fresh Codex shadow terminals before selecting the shared SQLite home", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-fresh-", - }); - const sharedHome = path.join(root, "shared"); - const shadowHome = path.join(root, "shadow"); - const environment = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: undefined, - }); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - config: { homePath: sharedHome, shadowHomePath: shadowHome }, - }, - }, - }), - ), - ); - - expect(environment.CODEX_HOME).toBe(shadowHome); - expect(environment.CODEX_SQLITE_HOME).toBe(sharedHome); - expect(yield* fileSystem.exists(shadowHome)).toBe(true); - expect(yield* fileSystem.exists(sharedHome)).toBe(true); - expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe( - path.join(sharedHome, "sessions"), - ); - }).pipe(Effect.provideService(HostProcess.Environment, {})), - ); - - it.effect.each( - [ - { - name: "host", - instanceEnvironment: undefined, - clientEnvironment: undefined, - hostEnvironment: { codex_sqlite_home: "/sqlite/host" }, - expected: "/sqlite/host", - }, - { - name: "client", - instanceEnvironment: undefined, - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/client", - }, - { - name: "instance", - instanceEnvironment: [ - { name: "CODEX_SQLITE_HOME", value: "/sqlite/instance", sensitive: false }, - ], - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/instance", - }, - { - name: "lowercase instance on Windows", - instanceEnvironment: [ - { name: "codex_sqlite_home", value: "/sqlite/lowercase-instance", sensitive: false }, - ], - clientEnvironment: { CODEX_SQLITE_HOME: "/sqlite/client" }, - hostEnvironment: { CODEX_SQLITE_HOME: "/sqlite/host" }, - expected: "/sqlite/lowercase-instance", - }, - ].filter(() => symlinksSupported), - )( - "uses the $name CODEX_SQLITE_HOME override for Codex shadow terminals", - ({ instanceEnvironment, clientEnvironment, hostEnvironment, expected }) => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-override-", - }); - const environment = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: clientEnvironment, - }); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - ...(instanceEnvironment ? { environment: instanceEnvironment } : {}), - config: { - homePath: path.join(root, "shared"), - shadowHomePath: path.join(root, "shadow"), - }, - }, - }, - }), - ), - ); - - expect(environment.CODEX_SQLITE_HOME).toBe(expected); - }).pipe( - Effect.provideService(HostProcess.Environment, hostEnvironment), - Effect.provideService(HostProcess.Platform, "win32"), - ), - ); - - it.effect.skipIf(!symlinksSupported)( - "honors a Windows lowercase CODEX_SQLITE_HOME client override", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-case-", - }); - const customHome = path.join(root, "custom-sqlite"); - const environment = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: { codex_sqlite_home: customHome }, - }); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - config: { - homePath: path.join(root, "shared"), - shadowHomePath: path.join(root, "shadow"), - }, - }, - }, - }), - ), - ); - - expect(environment.CODEX_SQLITE_HOME).toBe(customHome); - expect( - Object.keys(environment).filter((key) => key.toLowerCase() === "codex_sqlite_home"), - ).toEqual(["CODEX_SQLITE_HOME"]); - }).pipe( - Effect.provideService(HostProcess.Platform, "win32"), - Effect.provideService(HostProcess.Environment, { CODEX_SQLITE_HOME: "/sqlite/host" }), ), ); - it.effect("refuses a provider terminal with mixed shared and private SQLite state", () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-mixed-", - }); - const sharedHome = path.join(root, "shared"); - const shadowHome = path.join(root, "shadow"); - yield* fileSystem.makeDirectory(sharedHome, { recursive: true }); - yield* fileSystem.makeDirectory(shadowHome, { recursive: true }); - const sharedDatabase = path.join(sharedHome, "state_5.sqlite"); - const privateDatabase = path.join(shadowHome, "logs_2.sqlite"); - yield* fileSystem.writeFileString(sharedDatabase, "shared-state"); - yield* fileSystem.writeFileString(privateDatabase, "private-logs"); - - const error = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: undefined, - }).pipe(Effect.flip); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - config: { homePath: sharedHome, shadowHomePath: shadowHome }, - }, - }, - }), - ), - ); - - expect(error).toMatchObject({ - _tag: "TerminalProviderEnvironmentError", - cause: { - _tag: "CodexShadowHomeSqliteConflictError", - entryName: "logs_2.sqlite", - }, - }); - expect(error.message).toContain("Codex SQLite recovery is required"); - expect(yield* fileSystem.readFileString(sharedDatabase)).toBe("shared-state"); - expect(yield* fileSystem.readFileString(privateDatabase)).toBe("private-logs"); - }).pipe(Effect.provideService(HostProcess.Environment, {})), - ); - - it.effect.skipIf(!symlinksSupported)( - "refuses a provider-bound terminal for a legacy Codex SQLite symlink", - () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ - prefix: "t3-terminal-codex-legacy-", - }); - const sharedHome = path.join(root, "shared"); - const shadowHome = path.join(root, "shadow"); - const database = path.join(sharedHome, "state_5.sqlite"); - yield* fileSystem.makeDirectory(sharedHome, { recursive: true }); - yield* fileSystem.makeDirectory(shadowHome, { recursive: true }); - yield* fileSystem.writeFileString(database, "shared-state"); - yield* fileSystem.symlink(database, path.join(shadowHome, "state_5.sqlite")); - - const error = yield* Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return yield* TerminalManager.resolveProviderInstanceTerminalEnvironment({ - serverSettings, - fileSystem, - path, - rawProviderInstanceId: "codex_shadow", - env: undefined, - }).pipe(Effect.flip); - }).pipe( - Effect.provide( - ServerSettings.layerTest({ - providerInstances: { - [ProviderInstanceId.make("codex_shadow")]: { - driver: ProviderDriverKind.make("codex"), - config: { homePath: sharedHome, shadowHomePath: shadowHome }, - }, - }, - }), - ), - ); - - expect(error).toMatchObject({ - _tag: "TerminalProviderEnvironmentError", - cause: { - _tag: "CodexShadowHomeSqliteConflictError", - entryName: "state_5.sqlite", - }, - }); - expect(error.message).toContain("Codex SQLite recovery is required"); - expect(error.message).toContain(sharedHome); - expect(error.message).toContain(shadowHome); - expect(error.message).toContain("reconnecting alone cannot clear this refusal"); - }).pipe(Effect.provideService(HostProcess.Environment, {})), - ); - it.effect("restarts a running terminal when the resolved provider environment changes", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index e21ddfc60fa3d..dd8f17432feb7 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -67,7 +67,6 @@ import * as ServerConfig from "../config.ts"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; import { codexHomeEnvironment, - materializeCodexShadowHome, resolveCodexHomeLayout, } from "../provider/Drivers/CodexHomeLayout.ts"; import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts"; @@ -1480,23 +1479,10 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( if (layout.effectiveHomePath) resolved = { ...resolved, CODEX_HOME: layout.effectiveHomePath }; if (layout.mode === "authOverlay") { - const hostEnv = yield* HostProcess.Environment; - const clientEnv = input.env ?? {}; - const isWindows = (yield* HostProcess.Platform) === "win32"; - const sqliteOverride = (environment: NodeJS.ProcessEnv) => - Object.entries(environment).find(([name]) => - isWindows ? name.toUpperCase() === "CODEX_SQLITE_HOME" : name === "CODEX_SQLITE_HOME", - )?.[1]; - // Preserve client-over-host precedence even when Windows supplies a different casing. - const selectedOverride = sqliteOverride(clientEnv) ?? sqliteOverride(hostEnv); - const inherited = yield* mergeProviderInstanceEnvironment(instance.environment, { - ...hostEnv, - ...clientEnv, - ...(selectedOverride !== undefined ? { CODEX_SQLITE_HOME: selectedOverride } : {}), - }); - const sqliteEnvironment = yield* Effect.gen(function* () { - yield* materializeCodexShadowHome(layout, inherited); - return yield* codexHomeEnvironment(layout, inherited); + const hostEnvironment = yield* HostProcess.Environment; + const sqliteEnvironment = yield* codexHomeEnvironment(layout, { + ...hostEnvironment, + ...resolved, }).pipe( Effect.mapError( (cause) => @@ -1511,14 +1497,9 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( Effect.provideService(FileSystem.FileSystem, input.fileSystem), Effect.provideService(Path.Path, input.path), ); - resolved = { - ...Object.fromEntries( - Object.entries(resolved).filter( - ([key]) => !isWindows || key.toUpperCase() !== "CODEX_SQLITE_HOME", - ), - ), - CODEX_SQLITE_HOME: sqliteEnvironment.CODEX_SQLITE_HOME, - }; + if (sqliteEnvironment.CODEX_SQLITE_HOME) { + resolved = { ...resolved, CODEX_SQLITE_HOME: sqliteEnvironment.CODEX_SQLITE_HOME }; + } } } } else if (instance.driver === "claudeAgent") { diff --git a/packages/contracts/src/terminal.test.ts b/packages/contracts/src/terminal.test.ts index 621b7f18c464f..dda8060195fcf 100644 --- a/packages/contracts/src/terminal.test.ts +++ b/packages/contracts/src/terminal.test.ts @@ -36,24 +36,6 @@ describe("TerminalProviderEnvironmentError", () => { ); expect(decoded.message).not.toContain("secret backend unavailable"); }); - - it("round-trips an explicit safe detail as the public message", () => { - const detail = "Codex SQLite recovery is required before this terminal can start."; - const error = new TerminalProviderEnvironmentError({ - providerInstanceId: ProviderInstanceId.make("codex_work"), - detail, - cause: { _tag: "CodexShadowHomeSqliteConflictError" }, - }); - - const decoded = decodeTerminalError(encodeTerminalError(error)); - - expect(decoded).toMatchObject({ - _tag: "TerminalProviderEnvironmentError", - providerInstanceId: "codex_work", - detail, - }); - expect(decoded.message).toBe(detail); - }); }); describe("TerminalOpenInput", () => { From ac7db46b4aedb8f03ce425c2df11cc952d12055d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 22:54:03 +0000 Subject: [PATCH 09/17] fix(server): refuse only a Codex database present in both homes AI-assisted; verified with the tests and tools listed above. --- .../provider/Drivers/CodexHomeLayout.test.ts | 5 ++++- .../src/provider/Drivers/CodexHomeLayout.ts | 22 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 95605729dfb60..f9e285f4820da 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -446,7 +446,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect("keeps a shadow's own databases until the shared home has some too", () => + it.effect("keeps a shadow's own databases and refuses only one the shared home also has", () => Effect.gen(function* () { const path = yield* Path.Path; const { sharedHome, shadowHome, layout } = yield* makeShadowLayout(); @@ -455,6 +455,9 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { expect((yield* codexHomeEnvironment(layout, {})).CODEX_SQLITE_HOME).toBe(shadowHome); + yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "shared db"); + expect((yield* codexHomeEnvironment(layout, {})).CODEX_SQLITE_HOME).toBe(sharedHome); + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "shared db"); const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 146c020c864fa..10e81cb9153ec 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -284,25 +284,31 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm entryName: stranded.entryName, }); } - const [privateDatabase] = regularDatabases; - if (privateDatabase === undefined) { + if (regularDatabases.size === 0) { return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; } - // A shadow that only ever had its own databases keeps them, until the shared home has some too. - const sharedEntries = yield* readHomeEntries( + const sharedDatabases = (yield* readHomeEntries( fileSystem, layout, effectiveHomePath, layout.sharedHomePath, - ); - if (sharedEntries.some((entry) => SQLITE_DATABASE_ENTRY_NAME.test(entry))) { + )) + .filter((entry) => SQLITE_DATABASE_ENTRY_NAME.test(entry)) + .map((entry) => entry.toLowerCase()); + // A shadow that only ever had its own databases keeps them, until the shared home has some too. + if (sharedDatabases.length === 0) { + return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; + } + // 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: privateDatabase, + entryName: conflict, }); } - return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; + return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; }); const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { From 50cfd724adcc9d5a6894605b9a23b3cf43b5cbb9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 23:05:52 +0000 Subject: [PATCH 10/17] fix(server): keep private Codex databases when the shared home has others AI-assisted; verified with the tests and tools listed above. --- .../provider/Drivers/CodexHomeLayout.test.ts | 38 ++++++++++++------- .../src/provider/Drivers/CodexHomeLayout.ts | 17 +++++---- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index f9e285f4820da..3c641ad4c2e49 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -446,22 +446,34 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }), ); - it.effect("keeps a shadow's own databases and refuses only one the shared home also has", () => - Effect.gen(function* () { - 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"); + it.effect.skipIf(!symlinksSupported)( + "keeps a shadow's own databases 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); + expect((yield* codexHomeEnvironment(layout, {})).CODEX_SQLITE_HOME).toBe(shadowHome); - yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "shared db"); - expect((yield* codexHomeEnvironment(layout, {})).CODEX_SQLITE_HOME).toBe(sharedHome); + 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", + ); - yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "shared db"); - const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - }), + yield* writeTextFile(path.join(sharedHome, "state_5.sqlite"), "shared db"); + const error = yield* codexHomeEnvironment(layout, {}).pipe(Effect.flip); + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + }), ); }); }); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 10e81cb9153ec..25290fbb3eee4 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -295,10 +295,6 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm )) .filter((entry) => SQLITE_DATABASE_ENTRY_NAME.test(entry)) .map((entry) => entry.toLowerCase()); - // A shadow that only ever had its own databases keeps them, until the shared home has some too. - if (sharedDatabases.length === 0) { - return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; - } // 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) { @@ -308,7 +304,8 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm entryName: conflict, }); } - return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; + // Neither home holds every database, so the shadow keeps its own and links the shared ones. + return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; }); const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { @@ -470,6 +467,7 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* ( layout: CodexHomeLayout, + linkSharedDatabases: boolean, ) { if (layout.mode !== "authOverlay") return; const effectiveHomePath = layout.effectiveHomePath; @@ -526,7 +524,8 @@ const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* if ( !PRIVATE_ENTRY_NAMES.has(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName) && - !SQLITE_ENTRY_NAME.test(entryName) + (!SQLITE_ENTRY_NAME.test(entryName) || + (linkSharedDatabases && SQLITE_DATABASE_ENTRY_NAME.test(entryName))) ) { entries.add(entryName); } @@ -576,7 +575,11 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" ) { // Refuse a stranded SQLite layout before touching the shadow home. const launchEnvironment = yield* codexHomeEnvironment(layout, environment); - yield* materializeShadowHome(layout); + yield* materializeShadowHome( + layout, + !environment.CODEX_SQLITE_HOME?.trim() && + launchEnvironment.CODEX_SQLITE_HOME === layout.effectiveHomePath, + ); return launchEnvironment; }); From 4442692b1f04a13e171a6bf4895f2c060e4a1f84 Mon Sep 17 00:00:00 2001 From: nkcbuilds <155615649+nkcbuilds@users.noreply.github.com> Date: Fri, 13 Nov 2026 05:51:45 +0530 Subject: [PATCH 11/17] fix(server): refuse mixed Codex SQLite homes on Windows instead of linking When a shadow home keeps its own SQLite databases and the shared home has others, the shadow was routed to its own SQLite home with the shared databases symlinked in. POSIX SQLite opens a symlinked database by its target's path, so its WAL stays beside the shared file. Windows SQLite derives the WAL from the link's path, so a shadow writer and a shared-home writer would each keep their own WAL for one database, which is the split reported in #24486. On Windows, refuse that layout before touching either home, including a shadow whose legacy link points at a shared database that is gone, and never link a database during setup even if the shared home gained one after the check. Name both homes and both databases in the recovery message. macOS and Linux keep the current routing. Tests cover the reviewer's case, the dangling legacy link and the check-to-setup race on Windows, keep the existing mixed-home test on POSIX, and use node:sqlite to show a POSIX shadow and the shared home share one WAL through the link. --- .../provider/Drivers/CodexHomeLayout.test.ts | 139 +++++++++++++++++- .../src/provider/Drivers/CodexHomeLayout.ts | 26 +++- 2 files changed, 162 insertions(+), 3 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 3c641ad4c2e49..57de34b03e2db 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -4,6 +4,7 @@ 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 { @@ -14,6 +15,7 @@ import { materializeCodexShadowHome, resolveCodexHomeLayout, } from "./CodexHomeLayout.ts"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings); @@ -447,7 +449,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { ); it.effect.skipIf(!symlinksSupported)( - "keeps a shadow's own databases and refuses only one the shared home also has", + "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; @@ -473,7 +475,140 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { 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")), + ); + + it.effect.skipIf(!symlinksSupported)( + "refuses on Windows to link shared databases into a shadow that has its own", + () => + 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"); + + // With no shared databases there is nothing to link, so the shadow keeps its own. + expect((yield* materializeCodexShadowHome(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 error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); + + expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); + expect(error).toMatchObject({ + sharedHomePath: sharedHome, + effectiveHomePath: shadowHome, + entryName: "state_5.sqlite", + sharedEntryName: "goals_1.sqlite", + }); + expect(error.message).toContain(shadowHome); + expect(error.message).toContain(sharedHome); + expect(yield* fileSystem.exists(path.join(shadowHome, "goals_1.sqlite"))).toBe(false); + 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"); + expect( + yield* fileSystem.readFileString(path.join(sharedHome, "goals_1.sqlite-wal")), + ).toBe("shared wal"); + expect( + yield* materializeCodexShadowHome(layout, { CODEX_SQLITE_HOME: sharedHome }), + ).toEqual({ CODEX_SQLITE_HOME: sharedHome }); + }).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")), + ); + + 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 25290fbb3eee4..f5fd8a7630c95 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -125,9 +125,14 @@ export class CodexShadowHomeSqliteConflictError extends Schema.TaggedError(); + const linkedDatabases: Array = []; const sidecarsWithData: Array<{ readonly entryName: string; readonly databaseName: string }> = []; for (const entryName of yield* readHomeEntries( fileSystem, @@ -261,6 +268,9 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm 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()); @@ -304,6 +314,18 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm 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 && (yield* HostProcess.isWindows)) { + 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 { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; }); @@ -575,10 +597,12 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome" ) { // Refuse a stranded SQLite layout before touching the shadow home. const launchEnvironment = yield* codexHomeEnvironment(layout, environment); + // 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, + launchEnvironment.CODEX_SQLITE_HOME === layout.effectiveHomePath && + !(yield* HostProcess.isWindows), ); return launchEnvironment; }); From ddf3674303d9b07e0772264e00addea0a5e54864 Mon Sep 17 00:00:00 2001 From: nkcbuilds <155615649+nkcbuilds@users.noreply.github.com> Date: Fri, 13 Nov 2026 06:16:20 +0000 Subject: [PATCH 12/17] fix(server): give shadow terminals the instance environment's CODEX_HOME Since #35912 a Codex shadow home shares the instance environment's CODEX_HOME when no home path is set. Provider terminals still resolved the shared home from settings alone, so they routed CODEX_SQLITE_HOME to ~/.codex while the provider used the environment's home. Resolve it the way the provider does. --- apps/server/src/terminal/Manager.test.ts | 32 ++++++++++++++++++++++++ apps/server/src/terminal/Manager.ts | 17 ++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index ba6b4d7e3c24d..3c46d8a1e9c8b 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -2352,6 +2352,38 @@ it.layer( ), ); + 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: "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" }, + }, + }, + }), + ), + ), + ); + it.effect("restarts a running terminal when the resolved provider environment changes", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 886ba24358dc3..ff1233c54d1db 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1550,13 +1550,22 @@ 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 hostEnvironment = yield* HostProcess.Environment; const sqliteEnvironment = yield* codexHomeEnvironment(layout, { ...hostEnvironment, ...resolved, From cfd6b1bdfb5194b572f51451c12b7aaa3d85abcc Mon Sep 17 00:00:00 2001 From: Gib Smith <17834086+GibSmith@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:28:43 -0600 Subject: [PATCH 13/17] fix(server): refuse a Windows Codex SQLite home that reaches a database through a link An explicit CODEX_SQLITE_HOME skipped every SQLite check. On Windows, naming a directory that holds a linked database, such as a shadow home with legacy links or the goals_1.sqlite link the old mixed-home fallback made, launched Codex against the link. Windows SQLite names the WAL after the path it is given, so that Codex kept its own WAL apart from the shared home's writers. This is the split reported in #24486, reached through the override instead of the fallback. On Windows, refuse an explicit SQLite home that holds a symlinked database, unless it is the shared home. The directory itself is read, so an alias of the shadow is caught too. The provider, the orchestration adapter, the managed runtime and provider terminals all resolve their environment here. macOS and Linux are unchanged, since SQLite follows the link itself there. A relative home is not inspected, because Codex resolves it per session. The Windows regression test now covers the review's fixture end to end: a real shadow state_5.sqlite beside a shared goals_1.sqlite that a shared-home writer holds open. It checks the provider and terminal paths, with and without the override, and that two writers routed to the shared home share one WAL. The legacy-link test uses the reported layout of four linked databases with index and empty WAL sidecars. Written by Claude Fable 5.2 in the T3 Code Claude Code harness. Co-Authored-By: Claude Fable 5.2 --- .../provider/Drivers/CodexHomeLayout.test.ts | 156 ++++++++++++++---- .../src/provider/Drivers/CodexHomeLayout.ts | 48 +++++- 2 files changed, 171 insertions(+), 33 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 57de34b03e2db..46db99b0baaaa 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -404,21 +404,41 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { 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* fileSystem.symlink( - path.join(sharedHome, "state_5.sqlite"), - path.join(shadowHome, "state_5.sqlite"), - ); - yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-wal"), ""); - yield* writeTextFile(path.join(shadowHome, "state_5.sqlite-shm"), "index"); + // 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); - expect(yield* fileSystem.readLink(path.join(shadowHome, "state_5.sqlite"))).toBe( - path.join(sharedHome, "state_5.sqlite"), - ); - }), + 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)( @@ -471,6 +491,11 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { 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); @@ -478,13 +503,36 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }).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)( - "refuses on Windows to link shared databases into a shadow that has its own", + "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(), + ), + ); + // The provider prepares the shadow; a provider terminal only resolves its environment. + 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"); @@ -493,32 +541,80 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { shadowHome, ); - yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite"), "shared db"); - yield* writeTextFile(path.join(sharedHome, "goals_1.sqlite-wal"), "shared wal"); - const error = yield* materializeCodexShadowHome(layout, {}).pipe(Effect.flip); + // 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);", + ); - expect(error).toBeInstanceOf(CodexShadowHomeSqliteConflictError); - expect(error).toMatchObject({ - sharedHomePath: sharedHome, - effectiveHomePath: shadowHome, - entryName: "state_5.sqlite", - sharedEntryName: "goals_1.sqlite", + 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(error.message).toContain(shadowHome); - expect(error.message).toContain(sharedHome); - expect(yield* fileSystem.exists(path.join(shadowHome, "goals_1.sqlite"))).toBe(false); + expect(routed).toEqual({ 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"); - expect( - yield* fileSystem.readFileString(path.join(sharedHome, "goals_1.sqlite-wal")), - ).toBe("shared wal"); - expect( - yield* materializeCodexShadowHome(layout, { CODEX_SQLITE_HOME: sharedHome }), - ).toEqual({ CODEX_SQLITE_HOME: sharedHome }); }).pipe(Effect.provideService(HostProcess.Platform, "win32")), ); diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index f5fd8a7630c95..a918b47478821 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -127,9 +127,14 @@ export class CodexShadowHomeSqliteConflictError extends Schema.TaggedError(); const linkedDatabases: Array = []; const sidecarsWithData: Array<{ readonly entryName: string; readonly databaseName: string }> = []; From 1e110ac3ca2a873121b6a95716c107ef203793af Mon Sep 17 00:00:00 2001 From: Gib Smith <17834086+GibSmith@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:28:44 -0600 Subject: [PATCH 14/17] fix(server): keep a Windows Codex shadow on the one SQLite directory T3 checked Review of cc72e96d54 found three ways a Windows shadow home could still open a shared database through a link, so that it kept a WAL apart from the shared home's writers (#24486). Codex ranks `sqlite_home`, from any config.toml or a `-c` launch arg, above CODEX_SQLITE_HOME. A shadow whose config named itself opened its legacy links while T3 reported the shared home. T3 now passes the directory it checked as a final `-c sqlite_home=...` on every Codex command it builds: app-server sessions and probes, exec and mcp list. Codex 0.170.0 takes the last `-c` over every config layer, including a trusted project's .codex/config.toml, which T3 cannot read before launch. A `sqlite_home` in the launch args or in the shadow's or shared home's config.toml is refused with guidance, because Codex typed into a provider terminal gets no argument from T3, and silently outranking the setting would move a user's history. The config file is matched by line rather than parsed, and a line that only looks like the key is refused too. A relative CODEX_SQLITE_HOME skipped the check, and probes, sessions and text generation each resolve it against a different directory. It is refused. The shared home was recognised by its exact spelling only. An alias of it was inspected for links, and another letter case or a trailing separator reached Codex as written. The override is now compared by real path, ignoring case, and Codex gets the shared home's own spelling or the directory's real path. macOS and Linux are unchanged: SQLite opens a link by its target there. Written by Claude Fable 5.2 in the T3 Code Claude Code harness. Co-Authored-By: Claude Fable 5.2 --- .../Adapters/CodexAdapterV2.ts | 7 +- .../src/provider/CodexManagedRuntime.test.ts | 2 +- apps/server/src/provider/CodexProvider.ts | 2 +- .../src/provider/Drivers/CodexDriver.ts | 6 +- .../provider/Drivers/CodexHomeLayout.test.ts | 154 +++++++++++++++++- .../src/provider/Drivers/CodexHomeLayout.ts | 147 ++++++++++++++--- .../provider/Drivers/CodexManagedProvider.ts | 8 +- .../src/provider/codexLaunchArgs.test.ts | 49 +++++- apps/server/src/provider/codexLaunchArgs.ts | 49 +++++- apps/server/src/terminal/Manager.ts | 5 +- .../src/textGeneration/CodexTextGeneration.ts | 4 +- 11 files changed, 378 insertions(+), 55 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 94878d0a8f531..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, @@ -1852,7 +1853,11 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter ); const processEnv = hooks.resolveRuntime === undefined - ? yield* materializeCodexShadowHome(homeLayout, environmentOverrides).pipe( + ? yield* materializeCodexShadowHome( + homeLayout, + environmentOverrides, + resolveCodexLaunchArgs(config.launchArgs, environmentOverrides), + ).pipe( Effect.mapError( (cause) => new ProviderAdapterDriverCreateError({ 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/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 b65c0c1358282..7ed0cebabb8ee 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -203,7 +203,11 @@ export const CodexDriver: ProviderDriver< accentColor, continuationGroupKey: continuationIdentity.continuationKey, }); - const processEnv = yield* materializeCodexShadowHome(homeLayout, environmentOverrides).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 46db99b0baaaa..8c0e8932db876 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -43,8 +43,10 @@ const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* }); const makeShadowLayout = Effect.fn("CodexHomeLayout.test.makeShadowLayout")(function* () { - const sharedHome = yield* makeTempDir("t3code-codex-shared-"); - const shadowHome = yield* makeTempDir("t3code-codex-shadow-"); + 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 }), ); @@ -527,7 +529,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { entries.filter((entry) => entry.startsWith("goals_1.sqlite")).toSorted(), ), ); - // The provider prepares the shadow; a provider terminal only resolves its environment. + // 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), @@ -592,7 +594,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { const routed = yield* materializeCodexShadowHome(layout, { CODEX_SQLITE_HOME: sharedHome, }); - expect(routed).toEqual({ 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, ); @@ -675,6 +680,147 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }).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, + `[features]\nx = true\n "sqlite_home"='${shadowHome}'\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 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 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 relativeShadow = path.relative(path.resolve("."), shadowHome); + + 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", () => diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index a918b47478821..2b95e23a1d662 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -9,6 +9,11 @@ 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"; + export interface CodexHomeLayout { readonly mode: "direct" | "authOverlay"; readonly sharedHomePath: string; @@ -37,6 +42,8 @@ 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; +// Any line that could assign the key counts, table or not: refusing one too many is the safe side. +const SQLITE_HOME_CONFIG_KEY = /^[ \t]*["']?sqlite_home["']?[ \t]*=/mu; // 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", @@ -84,7 +91,14 @@ export class CodexShadowHomeFileSystemError 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", { @@ -160,10 +198,16 @@ export const CodexShadowHomeError = Schema.Union([ 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"; @@ -247,23 +291,87 @@ const readHomeEntries = ( 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) return environment; + 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 override = environment.CODEX_SQLITE_HOME?.trim(); + const windows = yield* HostProcess.isWindows; + const context = { sharedHomePath: layout.sharedHomePath, effectiveHomePath }; + const base = { ...environment }; + delete base[CODEX_PINNED_SQLITE_HOME_ENV]; + const override = base.CODEX_SQLITE_HOME?.trim(); + // 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 (SQLITE_HOME_CONFIG_KEY.test(config)) { + return yield* new CodexShadowHomeSqliteSettingError({ + ...context, + setting: "configFile", + value: configPath, + }); + } + } + } + if (override) { - const launchEnvironment = { ...environment, CODEX_SQLITE_HOME: override }; - // Codex expands `~` itself. A relative home resolves against each session's directory, which - // is unknown here, and the shared home is where every other Codex opens these databases. - const sqliteHomePath = expandHomePath(override, yield* HostProcess.HomeDirectory); - if ( - !(yield* HostProcess.isWindows) || - !path.isAbsolute(sqliteHomePath) || - path.resolve(sqliteHomePath) === layout.sharedHomePath - ) { - return launchEnvironment; + // 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( @@ -289,7 +397,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm }); } } - return launchEnvironment; + return route(sqliteHomePath); } const regularDatabases = new Set(); @@ -336,9 +444,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm entryName: stranded.entryName, }); } - if (regularDatabases.size === 0) { - return { ...environment, CODEX_SQLITE_HOME: layout.sharedHomePath }; - } + if (regularDatabases.size === 0) return route(layout.sharedHomePath); const sharedDatabases = (yield* readHomeEntries( fileSystem, layout, @@ -360,7 +466,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm 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 && (yield* HostProcess.isWindows)) { + if (sharedDatabase && privateDatabase && windows) { return yield* new CodexShadowHomeSqliteConflictError({ sharedHomePath: layout.sharedHomePath, effectiveHomePath, @@ -369,7 +475,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm }); } // Neither home holds every database, so the shadow keeps its own and links the shared ones. - return { ...environment, CODEX_SQLITE_HOME: effectiveHomePath }; + return route(effectiveHomePath); }); const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: { @@ -636,9 +742,10 @@ const materializeShadowHome = Effect.fn("CodexHomeLayout.materialize")(function* 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); + 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, diff --git a/apps/server/src/provider/Drivers/CodexManagedProvider.ts b/apps/server/src/provider/Drivers/CodexManagedProvider.ts index aacc28fbbfa33..dca371187056b 100644 --- a/apps/server/src/provider/Drivers/CodexManagedProvider.ts +++ b/apps/server/src/provider/Drivers/CodexManagedProvider.ts @@ -28,15 +28,11 @@ import { type ProviderDriverCreateInput, type ProviderInstance, } from "@t3tools/provider-core/server/driver"; -import { - CodexShadowHomeSqliteConflictError, - 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); -const isCodexShadowHomeSqliteConflictError = Schema.is(CodexShadowHomeSqliteConflictError); export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(function* ( input: ProviderDriverCreateInput, @@ -187,7 +183,7 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu ), Effect.catch((cause) => { const sqliteConflict = - isProviderSetupError(cause) && isCodexShadowHomeSqliteConflictError(cause.cause) + isProviderSetupError(cause) && isCodexShadowHomeSqliteError(cause.cause) ? cause.cause : undefined; return runtime.auth.read.pipe( diff --git a/apps/server/src/provider/codexLaunchArgs.test.ts b/apps/server/src/provider/codexLaunchArgs.test.ts index e74b29d44818c..efb78d340a687 100644 --- a/apps/server/src/provider/codexLaunchArgs.test.ts +++ b/apps/server/src/provider/codexLaunchArgs.test.ts @@ -21,7 +21,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 +33,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 +45,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 +64,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 +78,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 +87,33 @@ describe("codex exec and mcp list launch args", () => { ]); }); }); + +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..76980e8699bf3 100644 --- a/apps/server/src/provider/codexLaunchArgs.ts +++ b/apps/server/src/provider/codexLaunchArgs.ts @@ -9,12 +9,35 @@ 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 SQLITE_HOME_LAUNCH_ARG = /sqlite_home["']?\s*=/u; + +/** The launch arg that sets `sqlite_home`, in any form Codex accepts for a config override. */ +export const codexLaunchArgSettingSqliteHome = (launchArgs?: string) => + codexLaunchArgv(launchArgs).find((arg) => SQLITE_HOME_LAUNCH_ARG.test(arg)); + // 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 +88,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/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 70a15b081f0ec..ea9e14883cf21 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -71,6 +71,7 @@ import * as ServerConfig from "../config.ts"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; import { codexHomeEnvironment, + isCodexShadowHomeSqliteError, resolveCodexHomeLayout, } from "../provider/Drivers/CodexHomeLayout.ts"; import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts"; @@ -1748,9 +1749,7 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( (cause) => new TerminalProviderEnvironmentError({ providerInstanceId, - ...(cause._tag === "CodexShadowHomeSqliteConflictError" - ? { detail: cause.message } - : {}), + ...(isCodexShadowHomeSqliteError(cause) ? { detail: cause.message } : {}), cause, }), ), 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( From 846e6ddb8398b7d05e287625a4804fd84d9c84dc Mon Sep 17 00:00:00 2001 From: Gib Smith <17834086+GibSmith@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:28:45 -0600 Subject: [PATCH 15/17] fix(server): a Windows terminal's SQLite home outranks the host's whatever its casing Windows reads environment names without regard to case, but a provider terminal merged the host, client and instance environments as plain objects. A client's `codex_sqlite_home` sat beside the host's CODEX_SQLITE_HOME, the host's spelling was the one checked, and its value was written back over the client's choice (#24486 review). The terminal now merges the three by name ignoring case on Windows, keeping instance over client over host. The SQLite check reads the variable the same way and returns a single CODEX_SQLITE_HOME, so the provider and the terminal agree on which value Codex gets. Written by Claude Fable 5.2 in the T3 Code Claude Code harness. Co-Authored-By: Claude Fable 5.2 --- .../src/provider/Drivers/CodexHomeLayout.ts | 10 ++- apps/server/src/terminal/Manager.test.ts | 61 +++++++++++++++++++ apps/server/src/terminal/Manager.ts | 30 +++++++-- 3 files changed, 94 insertions(+), 7 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 2b95e23a1d662..05a93593ab509 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -308,7 +308,15 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm const context = { sharedHomePath: layout.sharedHomePath, effectiveHomePath }; const base = { ...environment }; delete base[CODEX_PINNED_SQLITE_HOME_ENV]; - const override = base.CODEX_SQLITE_HOME?.trim(); + 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 => ({ diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 05827e5239b87..a5170c300e48a 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -2793,6 +2793,67 @@ it.layer( ), ); + // 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(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("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", + }, + }, + }, + }), + ), + ), + ); + it.effect("restarts a running terminal when the resolved provider environment changes", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index ea9e14883cf21..3cfbf3347c807 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1741,10 +1741,20 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( if (layout.effectiveHomePath) resolved = { ...resolved, CODEX_HOME: layout.effectiveHomePath }; if (layout.mode === "authOverlay") { - const sqliteEnvironment = yield* codexHomeEnvironment(layout, { - ...hostEnvironment, - ...resolved, - }).pipe( + 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({ @@ -1756,8 +1766,16 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( Effect.provideService(FileSystem.FileSystem, input.fileSystem), Effect.provideService(Path.Path, input.path), ); - if (sqliteEnvironment.CODEX_SQLITE_HOME) { - resolved = { ...resolved, CODEX_SQLITE_HOME: sqliteEnvironment.CODEX_SQLITE_HOME }; + 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, + }; } } } From 766ced0bf8623f36f1b9373ebe29276fc51d2767 Mon Sep 17 00:00:00 2001 From: Gib Smith <17834086+GibSmith@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:28:45 -0600 Subject: [PATCH 16/17] fix(server): a Windows Codex shadow launches when sqlite_home is not Codex's own setting The Windows check for a `sqlite_home` that would outrank CODEX_SQLITE_HOME matched the text `sqlite_home =` anywhere. It refused a shadow whose MCP server had an environment variable of that name, whose instructions mentioned the setting inside a multi-line string, or whose launch args set `mcp_servers..env.sqlite_home`. Codex 0.170.0 accepts all three and keeps its databases where CODEX_SQLITE_HOME points (#24486 review). Codex reads `sqlite_home` from the root table only. config.toml is now scanned for that key alone: before the first table header, outside strings, comments, arrays and inline tables, bare or quoted, with a quoted key's escapes decoded. A launch arg counts only when the key before its first `=` is `sqlite_home`. `sqlite_home.x` and `[sqlite_home]` are tables, which Codex does not read as a path, so they are not refused. The scanner is 85 lines and apps/server has no TOML parser to reuse. Where a string or array is never closed, the old line match still decides, so a file Codex cannot parse is refused if any line could assign the key. A quoted key in a launch arg is refused too, although Codex 0.170.0 ignores it. The old match also missed a root key written with an escape, such as "sqlite_home", or behind a byte order mark. Codex honours both, and both are now refused. Written by Claude Fable 5.2 in the T3 Code Claude Code harness. Co-Authored-By: Claude Fable 5.2 --- .../provider/Drivers/CodexHomeLayout.test.ts | 42 ++++++++- .../src/provider/Drivers/CodexHomeLayout.ts | 5 +- .../src/provider/codexLaunchArgs.test.ts | 30 +++++++ apps/server/src/provider/codexLaunchArgs.ts | 17 +++- .../provider/codexSqliteHomeSetting.test.ts | 76 +++++++++++++++++ .../src/provider/codexSqliteHomeSetting.ts | 85 +++++++++++++++++++ 6 files changed, 248 insertions(+), 7 deletions(-) create mode 100644 apps/server/src/provider/codexSqliteHomeSetting.test.ts create mode 100644 apps/server/src/provider/codexSqliteHomeSetting.ts diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 8c0e8932db876..445ec45e0c793 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -742,7 +742,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { yield* fileSystem.remove(shadowConfig); yield* writeTextFile( sharedConfig, - `[features]\nx = true\n "sqlite_home"='${shadowHome}'\n`, + `x = true\n "sqlite_home"='${shadowHome}'\n[features]\ny = true\n`, ); for (const refused of yield* launch()) { expect(refused).toMatchObject({ @@ -759,6 +759,46 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { }).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", () => diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.ts index 05a93593ab509..26b7d4f35353b 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.ts @@ -13,6 +13,7 @@ import { CODEX_PINNED_SQLITE_HOME_ENV, codexLaunchArgSettingSqliteHome, } from "../codexLaunchArgs.ts"; +import { codexConfigSetsSqliteHome } from "../codexSqliteHomeSetting.ts"; export interface CodexHomeLayout { readonly mode: "direct" | "authOverlay"; @@ -42,8 +43,6 @@ 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; -// Any line that could assign the key counts, table or not: refusing one too many is the safe side. -const SQLITE_HOME_CONFIG_KEY = /^[ \t]*["']?sqlite_home["']?[ \t]*=/mu; // 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", @@ -352,7 +351,7 @@ export const codexHomeEnvironment = Effect.fn("CodexHomeLayout.codexHomeEnvironm }), }), ); - if (SQLITE_HOME_CONFIG_KEY.test(config)) { + if (codexConfigSetsSqliteHome(config)) { return yield* new CodexShadowHomeSqliteSettingError({ ...context, setting: "configFile", diff --git a/apps/server/src/provider/codexLaunchArgs.test.ts b/apps/server/src/provider/codexLaunchArgs.test.ts index efb78d340a687..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"; @@ -88,6 +89,35 @@ 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", () => { diff --git a/apps/server/src/provider/codexLaunchArgs.ts b/apps/server/src/provider/codexLaunchArgs.ts index 76980e8699bf3..f22096c3751c6 100644 --- a/apps/server/src/provider/codexLaunchArgs.ts +++ b/apps/server/src/provider/codexLaunchArgs.ts @@ -22,11 +22,22 @@ const pinnedSqliteHomeArgs = (environment: NodeJS.ProcessEnv | undefined) => { return ["-c", `sqlite_home=${value}`]; }; -const SQLITE_HOME_LAUNCH_ARG = /sqlite_home["']?\s*=/u; +const CONFIG_OVERRIDE_FLAG = /^(?:--config=|-c=?)/u; +const QUOTED_OVERRIDE_KEY = /^(["'])(.*)\1$/u; -/** The launch arg that sets `sqlite_home`, in any form Codex accepts for a config override. */ +/** + * 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) => SQLITE_HOME_LAUNCH_ARG.test(arg)); + 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 = ( 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); +}; From f7b860b87f3ccb09a819b4fc41ca720f848e9767 Mon Sep 17 00:00:00 2001 From: Gib Smith <17834086+GibSmith@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:28:46 -0600 Subject: [PATCH 17/17] test(server): CodexHomeLayout expectations hold on native Windows Three CodexHomeLayout tests failed on native Windows. CI runs on Linux and macOS only, so it did not see them. Two expected only CODEX_SQLITE_HOME. On Windows T3 also passes the checked directory as T3CODE_CODEX_SQLITE_HOME, for the final -c sqlite_home pin, and resolves a rooted path such as /elsewhere onto the working directory's drive. The expectations now say so. The relative CODEX_SQLITE_HOME test took path.relative from the working directory to a temp shadow. When the temp directory is on another drive there is no relative path, so the test handed the guard an absolute one. It now writes the relative path out. Written by Claude Opus 5.5 in the T3 Code Claude Code harness. Co-Authored-By: Claude Opus 5.5 --- .../provider/Drivers/CodexHomeLayout.test.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts index 445ec45e0c793..784c62be9356a 100644 --- a/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts +++ b/apps/server/src/provider/Drivers/CodexHomeLayout.test.ts @@ -53,6 +53,13 @@ const makeShadowLayout = Effect.fn("CodexHomeLayout.test.makeShadowLayout")(func 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", () => @@ -393,7 +400,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { const environment = yield* materializeCodexShadowHome(layout, { KEEP: "1" }); - expect(environment).toEqual({ KEEP: "1", CODEX_SQLITE_HOME: sharedHome }); + expect(environment).toEqual({ KEEP: "1", ...(yield* routedTo(sharedHome)) }); const shadowEntries = yield* fileSystem.readDirectory(shadowHome); expect(shadowEntries.filter((entry) => entry.includes(".sqlite"))).toEqual([]); }), @@ -464,9 +471,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { 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({ CODEX_SQLITE_HOME: "/elsewhere" }); + ).toEqual(yield* routedTo(path.resolve("/elsewhere"))); }), ); @@ -803,15 +811,11 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => { // directory resolved it differently. it.effect.skipIf(!symlinksSupported)("refuses a relative Windows CODEX_SQLITE_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, "goals_1.sqlite"), "db"); - yield* fileSystem.symlink( - path.join(sharedHome, "goals_1.sqlite"), - path.join(shadowHome, "goals_1.sqlite"), - ); - const relativeShadow = path.relative(path.resolve("."), shadowHome); + 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(