Skip to content

Commit ee494b7

Browse files
only21milclaude
andcommitted
fix(server): Codex switches respect each instance's CODEX_HOME variable
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d53873b commit ee494b7

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,47 @@ it.layer(layerTest)("CodexDriver", (it) => {
383383
),
384384
);
385385

386+
it.effect.skipIf(windowsHost)(
387+
"uses an environment CODEX_HOME as the instance's shared home",
388+
() =>
389+
Effect.gen(function* () {
390+
const fs = yield* FileSystem.FileSystem;
391+
const tempDir = yield* fs
392+
.makeTempDirectoryScoped({ prefix: "t3-codex-driver-" })
393+
.pipe(Effect.flatMap((directory) => fs.realPath(directory)));
394+
395+
const instances = yield* Effect.forEach(["work", "personal"], (account) =>
396+
Effect.gen(function* () {
397+
const codexHome = NodePath.join(tempDir, `codex-${account}`);
398+
const binaryPath = NodePath.join(codexHome, "packages", "standalone", "bin", "codex");
399+
yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
400+
yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
401+
yield* fs.chmod(binaryPath, 0o755);
402+
const instance = yield* CodexDriver.create({
403+
instanceId: ProviderInstanceId.make(`codex-${account}`),
404+
displayName: account,
405+
enabled: false,
406+
environment: [{ name: "CODEX_HOME", value: codexHome, sensitive: false }],
407+
config: { ...CodexDriver.defaultConfig(), binaryPath },
408+
});
409+
return {
410+
codexHome,
411+
continuationKey: instance.continuationIdentity.continuationKey,
412+
update: (yield* instance.snapshot.resolveMaintenance()).update,
413+
};
414+
}),
415+
);
416+
417+
for (const { codexHome, continuationKey, update } of instances) {
418+
expect(continuationKey).toBe(`codex:home:${codexHome}`);
419+
expect(update?.env).toEqual({ CODEX_HOME: codexHome });
420+
}
421+
}).pipe(
422+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
423+
Effect.scoped,
424+
),
425+
);
426+
386427
for (const fixture of [
387428
{
388429
name: "leaves mise npm-backend installations manual-only",

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ export const CodexDriver: ProviderDriver<
140140
const modelCatalog = yield* ModelCatalog.ModelCatalog;
141141
const currentCatalog = modelCatalog.current(DRIVER_KIND);
142142
const processEnv = yield* mergeProviderInstanceEnvironment(environment);
143-
const homeLayout = yield* resolveCodexHomeLayout(config);
143+
// Codex reads CODEX_HOME from the environment when homePath is empty, so
144+
// that home owns this instance's sessions and must key continuation.
145+
const environmentHome = processEnv.CODEX_HOME?.trim();
146+
const homeLayout = yield* resolveCodexHomeLayout(
147+
!config.homePath.trim() && !config.shadowHomePath.trim() && environmentHome
148+
? { ...config, homePath: environmentHome }
149+
: config,
150+
);
144151
const continuationIdentity = codexContinuationIdentity(homeLayout);
145152
const stampIdentity = withInstanceIdentity({
146153
instanceId,

0 commit comments

Comments
 (0)