Skip to content

Commit d1812dd

Browse files
committed
fix(server): keep Codex SQLite state in one home
1 parent 00cdceb commit d1812dd

15 files changed

Lines changed: 943 additions & 43 deletions

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import * as Orchestrator from "../Orchestrator.ts";
6363
import * as ProviderReplayHarness from "../testkit/ProviderReplayHarness.ts";
6464
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
6565
import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
66+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
6667
import * as CodexAdapterV2 from "./CodexAdapterV2.ts";
6768
import { makeReplayServerConfig, withCodexReplayChildMetadata } from "./CodexAdapterV2.testkit.ts";
6869
import * as CodexAdapterV2Testkit from "./CodexAdapterV2.testkit.ts";
@@ -784,6 +785,68 @@ describe("CodexAdapterV2 process spawning", () => {
784785
}).pipe(Effect.provideService(HostProcess.Platform, "linux")),
785786
);
786787

788+
it.effect.each([undefined, "/explicit/sqlite"])(
789+
"keeps shadow app-server SQLite together, honoring an explicit home: %s",
790+
(sqliteHome) =>
791+
Effect.gen(function* () {
792+
const fs = yield* FileSystem.FileSystem;
793+
const path = yield* Path.Path;
794+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-sqlite-launch-" });
795+
const sharedHome = path.join(root, "shared");
796+
const shadowHome = path.join(root, "shadow");
797+
const environments: Array<NodeJS.ProcessEnv> = [];
798+
const spawner = ChildProcessSpawner.make((command) => {
799+
if (ChildProcess.isStandardCommand(command)) environments.push(command.options.env ?? {});
800+
return Effect.fail(
801+
PlatformError.systemError({
802+
_tag: "NotFound",
803+
module: "ChildProcess",
804+
method: "spawn",
805+
}),
806+
);
807+
});
808+
const adapter = yield* CodexAdapterV2.createCodexAdapterV2({
809+
instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID,
810+
displayName: undefined,
811+
environment:
812+
sqliteHome === undefined
813+
? []
814+
: [{ name: "CODEX_SQLITE_HOME", value: sqliteHome, sensitive: false }],
815+
enabled: true,
816+
config: { ...DEFAULT_CODEX_SETTINGS, homePath: sharedHome, shadowHomePath: shadowHome },
817+
}).pipe(
818+
Effect.provide(
819+
Layer.mergeAll(
820+
CodexAdapterV2.layerAppServerClientFactory,
821+
ServerConfig.layerTest(process.cwd(), { prefix: "t3-codex-sqlite-" }),
822+
),
823+
),
824+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
825+
Effect.provideService(
826+
ProviderEventLoggers.ProviderEventLoggers,
827+
ProviderEventLoggers.NoOpProviderEventLoggers,
828+
),
829+
);
830+
yield* adapter
831+
.openSession({
832+
threadId: ThreadId.make("thread-sqlite"),
833+
providerSessionId: ProviderSessionId.make("provider-session-sqlite"),
834+
modelSelection: CODEX_TEST_MODEL_SELECTION,
835+
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
836+
})
837+
.pipe(Effect.scoped, Effect.exit);
838+
assert.lengthOf(environments, 1);
839+
assert.equal(environments[0]?.CODEX_HOME, shadowHome);
840+
assert.equal(environments[0]?.CODEX_SQLITE_HOME, sqliteHome ?? sharedHome);
841+
}).pipe(
842+
Effect.provide(
843+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
844+
),
845+
Effect.provideService(HostProcess.Environment, {}),
846+
),
847+
{ skip: !symlinksSupported },
848+
);
849+
787850
it.effect("expands ~ in the configured binary path before spawning", () =>
788851
Effect.gen(function* () {
789852
const spawnedCommands: Array<string> = [];

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,16 @@ import {
106106
} from "../../provider/CodexMcpElicitation.ts";
107107
import {
108108
materializeCodexShadowHome,
109+
mergeCodexEnvironment,
109110
resolveCodexHomeLayout,
111+
withCodexSqliteHome,
110112
} from "../../provider/Drivers/CodexHomeLayout.ts";
111113
import {
112114
boundProviderEventForLogging,
113115
shouldPersistProviderEvent,
114116
} from "../../provider/EventNdjsonLogger.ts";
115117
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
116118
import { codexAppServerArgs, resolveCodexLaunchArgs } from "../../provider/codexLaunchArgs.ts";
117-
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
118119
import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession";
119120
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
120121
import {
@@ -1616,7 +1617,7 @@ export const createCodexAdapterV2 = (
16161617
new ProviderAdapterDriverCreateError({
16171618
driver: CODEX_DRIVER_KIND,
16181619
instanceId,
1619-
detail: "Failed to materialize the Codex shadow home.",
1620+
detail: cause.message,
16201621
cause,
16211622
}),
16221623
),
@@ -1632,7 +1633,11 @@ export const createCodexAdapterV2 = (
16321633
return yield* makeCodexAdapterV2({
16331634
instanceId,
16341635
settings,
1635-
environment: yield* mergeProviderInstanceEnvironment(environment, hostEnvironment),
1636+
environment: withCodexSqliteHome(
1637+
homeLayout,
1638+
yield* mergeCodexEnvironment(environment, hostEnvironment),
1639+
yield* HostProcess.Platform,
1640+
),
16361641
clientFactory,
16371642
crypto,
16381643
fileSystem,

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ it.effect("managed home defaults to the global Codex home and honors configured
4444
ProviderInstanceId.make("codex-work"),
4545
decodeSettings({ homePath: "/custom/shared", shadowHomePath: "/custom/shadow" }),
4646
);
47-
assert.equal(configured.sharedHomePath, "/custom/shared");
48-
assert.equal(configured.effectiveHomePath, "/custom/shadow");
47+
assert.equal(configured.sharedHomePath, path.resolve("/custom/shared"));
48+
assert.equal(configured.effectiveHomePath, path.resolve("/custom/shadow"));
4949
}).pipe(Effect.provide(NodeServices.layer)),
5050
);
5151
it.effect.each(
@@ -135,10 +135,12 @@ it.effect.each(
135135
if (account === "primary") {
136136
assert.equal(effective.config.homePath, sharedHome);
137137
assert.equal(runtime.homeLayout.mode, "direct");
138+
assert.isUndefined(effective.environment.CODEX_SQLITE_HOME);
138139
} else {
139140
assert.include(effective.config.homePath, instanceId);
140-
assert.include(effective.config.homePath, "userdata/providers/codex");
141+
assert.include(effective.config.homePath, path.join("userdata", "providers", "codex"));
141142
assert.equal(runtime.homeLayout.mode, "authOverlay");
143+
assert.equal(effective.environment.CODEX_SQLITE_HOME, sharedHome);
142144
assert.equal(
143145
yield* fs.readLink(path.join(effective.config.homePath, "sessions")),
144146
path.join(sharedHome, "sessions"),
@@ -173,6 +175,18 @@ it.effect.each(
173175
assert.strictEqual(ambient.CODEX_HOME, "/user/.codex");
174176
}).pipe(Effect.scoped);
175177
assert.strictEqual(leases, 0);
178+
if (account === "additional") {
179+
yield* fs.writeFileString(path.join(sharedHome, "state_5.sqlite"), "");
180+
yield* fs.symlink(
181+
path.join(sharedHome, "state_5.sqlite"),
182+
path.join(runtime.homePath, "state_5.sqlite"),
183+
);
184+
const error = yield* runtime.resolve.pipe(Effect.scoped, Effect.flip);
185+
assert.equal(error._tag, "ProviderSetupError");
186+
assert.include(error.detail, "Codex SQLite recovery is required");
187+
assert.include(error.detail, "Stop all Codex processes");
188+
assert.strictEqual(leases, 0);
189+
}
176190
yield* runtime.auth.controller.logout(Effect.void);
177191
assert.equal(
178192
yield* fs.readFileString(path.join(sharedHome, "auth.json")),

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import * as Schema from "effect/Schema";
77
import * as ServerConfig from "../config.ts";
88
import * as CodexInstallation from "./CodexInstallation.ts";
99
import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
10-
import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts";
10+
import { materializeCodexShadowHome, withCodexSqliteHome } from "./Drivers/CodexHomeLayout.ts";
11+
import * as HostProcess from "@t3tools/shared/HostProcess";
1112

1213
export interface CodexEffectiveRuntime {
1314
readonly config: CodexSettings;
@@ -90,11 +91,15 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
9091
),
9192
);
9293
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
93-
const environment: NodeJS.ProcessEnv = {
94-
...options.environment,
95-
ACCESS_TOKEN: credentials.accessToken,
96-
CODEX_HOME: homePath,
97-
};
94+
const environment = withCodexSqliteHome(
95+
homeLayout,
96+
{
97+
...options.environment,
98+
ACCESS_TOKEN: credentials.accessToken,
99+
CODEX_HOME: homePath,
100+
},
101+
yield* HostProcess.Platform,
102+
);
98103
delete environment.T3CODE_CODEX_LAUNCH_ARGS;
99104
delete environment.OPENAI_API_KEY;
100105
delete environment.OPENAI_BASE_URL;

‎apps/server/src/provider/Drivers/CodexDriver.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ it.layer(layerTest)("CodexDriver", (it) => {
207207
expect(restored.auth.email).toBe("[email protected]");
208208
expect(restored.runtimePaths?.homePath).toBe(sharedHome);
209209
expect(restored.runtimePaths?.shadowHomePath).toContain(
210-
`providers/codex/${instanceId}/shadow`,
210+
NodePath.join("providers", "codex", instanceId, "shadow"),
211211
);
212212
yield* Deferred.await(observedAccount);
213213
// Sessions launch the T3-installed Codex with the account's token, not ambient credentials.
@@ -231,8 +231,37 @@ it.layer(layerTest)("CodexDriver", (it) => {
231231
expect(launch.environment.ACCESS_TOKEN).toBe("dummy-owned-access");
232232
expect(launch.environment.OPENAI_API_KEY).toBeUndefined();
233233
expect(launch.environment.CODEX_HOME).toBe(launch.settings.homePath);
234+
expect(launch.environment.CODEX_SQLITE_HOME).toBe(sharedHome);
234235
const before = yield* instance.auth!.subscribe("test-owner").pipe(Stream.runHead);
235236
expect(Option.getOrThrow(before).phase).toBe("idle");
237+
const fs = yield* FileSystem.FileSystem;
238+
yield* fs.writeFileString(NodePath.join(sharedHome, "state_5.sqlite"), "");
239+
yield* fs.symlink(
240+
NodePath.join(sharedHome, "state_5.sqlite"),
241+
NodePath.join(launch.settings.homePath, "state_5.sqlite"),
242+
);
243+
const blocked = yield* instance.snapshot.refresh;
244+
expect(blocked.message).toContain("Codex SQLite recovery is required");
245+
expect(blocked.message).toContain("Stop all Codex processes");
246+
// An explicitly configured shadow is checked during construction, not only
247+
// later runtime probes. The registry must receive the same recovery guidance.
248+
const explicit = yield* CodexDriver.create({
249+
instanceId,
250+
displayName: "Explicit shadow account",
251+
enabled: true,
252+
environment: [],
253+
config: {
254+
...CodexDriver.defaultConfig(),
255+
setupMode: "managed",
256+
homePath: sharedHome,
257+
shadowHomePath: launch.settings.homePath,
258+
},
259+
}).pipe(Effect.scoped, Effect.flip);
260+
expect(explicit.detail).toContain("Codex SQLite recovery is required");
261+
expect(explicit.detail).toContain("Stop all Codex processes");
262+
expect(yield* fs.readLink(NodePath.join(launch.settings.homePath, "state_5.sqlite"))).toBe(
263+
NodePath.join(sharedHome, "state_5.sqlite"),
264+
);
236265
yield* instance.auth!.logout(Effect.void);
237266
const after = Option.getOrThrow(yield* Fiber.join(disconnected));
238267
expect(after.auth.status).toBe("unauthenticated");

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import * as ModelCatalog from "@t3tools/provider-core/server/ModelCatalog";
5454
import { applyCodexModelCatalog } from "../codexModelCatalog.ts";
5555
import type { ProviderDriver, ProviderInstance } from "@t3tools/provider-core/server/driver";
5656
import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
57-
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
5857
import {
5958
enrichProviderSnapshotWithVersionAdvisory,
6059
makeCachedProviderMaintenanceResolution,
@@ -70,7 +69,9 @@ import {
7069
import {
7170
codexContinuationIdentity,
7271
materializeCodexShadowHome,
72+
mergeCodexEnvironment,
7373
resolveCodexHomeLayout,
74+
withCodexSqliteHome,
7475
} from "./CodexHomeLayout.ts";
7576
import { makeManagedCodexProvider } from "./CodexManagedProvider.ts";
7677
import { codexUsageReader } from "./codexUsage.ts";
@@ -155,8 +156,12 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv, Path.Pat
155156
const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
156157
const modelCatalog = yield* ModelCatalog.ModelCatalog;
157158
const currentCatalog = modelCatalog.current(DRIVER_KIND);
158-
const processEnv = yield* mergeProviderInstanceEnvironment(environment);
159159
const homeLayout = yield* resolveCodexHomeLayout(config);
160+
const processEnv = withCodexSqliteHome(
161+
homeLayout,
162+
yield* mergeCodexEnvironment(environment),
163+
yield* HostProcess.Platform,
164+
);
160165
const continuationIdentity = codexContinuationIdentity(homeLayout);
161166
const stampIdentity = withInstanceIdentity({
162167
instanceId,
@@ -212,7 +217,7 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv, Path.Pat
212217
new ProviderDriverError({
213218
driver: DRIVER_KIND,
214219
instanceId,
215-
detail: "Failed to build Codex orchestration adapter.",
220+
detail: cause.message,
216221
cause,
217222
}),
218223
),

0 commit comments

Comments
 (0)