Skip to content

Commit a86fe3a

Browse files
fix(server): managed Codex accounts connect on Windows without Developer Mode
1 parent c7a3f02 commit a86fe3a

2 files changed

Lines changed: 128 additions & 2 deletions

File tree

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { describe, expect, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as FileSystem from "effect/FileSystem";
55
import * as Path from "effect/Path";
6+
import * as PlatformError from "effect/PlatformError";
67
import * as Schema from "effect/Schema";
78

89
import { CodexSettings } from "@t3tools/contracts";
10+
import * as HostProcess from "@t3tools/shared/HostProcess";
911
import {
1012
CodexShadowHomeEntryConflictError,
1113
CodexShadowHomePathConflictError,
@@ -38,6 +40,31 @@ const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function*
3840
yield* fileSystem.writeFileString(filePath, contents);
3941
});
4042

43+
// Windows without Developer Mode: every symlink fails with EPERM, as Effect reports it there.
44+
const onWindowsWithoutSymlinks = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
45+
Effect.gen(function* () {
46+
const fileSystem = yield* FileSystem.FileSystem;
47+
const refusingFileSystem = FileSystem.FileSystem.of({
48+
...fileSystem,
49+
symlink: (_target, link) =>
50+
Effect.fail(
51+
PlatformError.systemError({
52+
_tag: "Unknown",
53+
module: "FileSystem",
54+
method: "symlink",
55+
pathOrDescriptor: link,
56+
cause: Object.assign(new Error("EPERM: operation not permitted, symlink"), {
57+
code: "EPERM",
58+
}),
59+
}),
60+
),
61+
});
62+
return yield* effect.pipe(
63+
Effect.provideService(FileSystem.FileSystem, refusingFileSystem),
64+
Effect.provideService(HostProcess.Platform, "win32"),
65+
);
66+
});
67+
4168
it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
4269
describe("resolveCodexHomeLayout", () => {
4370
it.effect("uses the shared home for continuation and the shadow home for runtime", () =>
@@ -348,7 +375,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
348375
}),
349376
);
350377

351-
const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
378+
const error = yield* materializeCodexShadowHome(layout).pipe(
379+
Effect.provideService(HostProcess.Platform, "linux"),
380+
Effect.flip,
381+
);
352382

353383
expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
354384
expect(error).toMatchObject({
@@ -363,5 +393,63 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
363393
);
364394
}),
365395
);
396+
397+
it.effect("links shared directories as junctions when Windows refuses symlinks", () =>
398+
Effect.gen(function* () {
399+
const fileSystem = yield* FileSystem.FileSystem;
400+
const path = yield* Path.Path;
401+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
402+
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
403+
const shadowHome = path.join(shadowRoot, "shadow");
404+
yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions"));
405+
406+
const layout = yield* resolveCodexHomeLayout(
407+
decodeCodexSettings({
408+
homePath: sharedHome,
409+
shadowHomePath: shadowHome,
410+
}),
411+
);
412+
413+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
414+
// A second pass finds the junctions already in place.
415+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
416+
yield* writeTextFile(path.join(shadowHome, "sessions", "rollout.jsonl"), "{}\n");
417+
418+
expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe(
419+
path.join(sharedHome, "sessions"),
420+
);
421+
expect(yield* fileSystem.exists(path.join(sharedHome, "sessions", "rollout.jsonl"))).toBe(
422+
true,
423+
);
424+
}),
425+
);
426+
427+
it.effect("keeps shared files local when Windows refuses symlinks", () =>
428+
Effect.gen(function* () {
429+
const fileSystem = yield* FileSystem.FileSystem;
430+
const path = yield* Path.Path;
431+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
432+
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
433+
const shadowHome = path.join(shadowRoot, "shadow");
434+
yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
435+
yield* writeTextFile(path.join(sharedHome, "installation_id"), "shared\n");
436+
// Codex created its own copy after an earlier pass could not link the shared one.
437+
yield* writeTextFile(path.join(shadowHome, "installation_id"), "shadow\n");
438+
439+
const layout = yield* resolveCodexHomeLayout(
440+
decodeCodexSettings({
441+
homePath: sharedHome,
442+
shadowHomePath: shadowHome,
443+
}),
444+
);
445+
446+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
447+
448+
expect(yield* fileSystem.exists(path.join(shadowHome, "config.toml"))).toBe(false);
449+
expect(yield* fileSystem.readFileString(path.join(shadowHome, "installation_id"))).toBe(
450+
"shadow\n",
451+
);
452+
}),
453+
);
366454
});
367455
});

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off - Effect's symlink has no type argument, and Windows needs a junction to link without elevation.
2+
import * as NodeFSP from "node:fs/promises";
3+
14
import { ProviderDriverKind, type CodexSettings } from "@t3tools/contracts";
25
import * as Effect from "effect/Effect";
36
import * as FileSystem from "effect/FileSystem";
@@ -28,6 +31,7 @@ const KNOWN_SHARED_DIRECTORIES = [
2831
"mcp-oauth-locks",
2932
"thread-writer-locks",
3033
] as const;
34+
const KNOWN_SHARED_DIRECTORY_NAMES: ReadonlySet<string> = new Set(KNOWN_SHARED_DIRECTORIES);
3135

3236
// auth.lock guards the shadow home's private auth.json.
3337
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "auth.lock", "models_cache.json"]);
@@ -246,6 +250,23 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu
246250
cause,
247251
}),
248252
}),
253+
// Windows refuses symlinks without Developer Mode, but a junction needs no privilege.
254+
// Junctions only link directories on local volumes, so they are only a fallback.
255+
Effect.catch((error) =>
256+
Effect.gen(function* () {
257+
const isDirectory =
258+
(yield* HostProcess.isWindows) &&
259+
(yield* input.fileSystem.stat(target).pipe(
260+
Effect.map((info) => info.type === "Directory"),
261+
Effect.orElseSucceed(() => false),
262+
));
263+
if (!isDirectory) return yield* error;
264+
return yield* Effect.tryPromise({
265+
try: () => NodeFSP.symlink(target, link, "junction"),
266+
catch: () => error,
267+
});
268+
}),
269+
),
249270
);
250271

251272
if (state._tag === "NotSymlink") {
@@ -421,18 +442,35 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
421442
{ discard: true },
422443
);
423444

445+
const isWindows = yield* HostProcess.isWindows;
424446
yield* Effect.forEach(
425447
entries,
426448
(entryName) => {
427449
if (PRIVATE_ENTRY_NAMES.has(entryName)) {
428450
return Effect.void;
429451
}
430-
return ensureSymlink({
452+
const linked = ensureSymlink({
431453
fileSystem,
432454
sharedHomePath: layout.sharedHomePath,
433455
effectiveHomePath,
434456
entryName,
435457
});
458+
// Without Developer Mode, Windows refuses to symlink a file, and Codex then creates its own
459+
// copy in the shadow home. Either way the entry stays local instead of disabling the provider.
460+
return isWindows && !KNOWN_SHARED_DIRECTORY_NAMES.has(entryName)
461+
? linked.pipe(
462+
Effect.catchIf(
463+
(error) =>
464+
error._tag === "CodexShadowHomeEntryConflictError" ||
465+
(error._tag === "CodexShadowHomeFileSystemError" && error.operation === "symlink"),
466+
(error) =>
467+
Effect.logDebug("Codex shadow home entry is not shared.", {
468+
entryName,
469+
detail: error.message,
470+
}),
471+
),
472+
)
473+
: linked;
436474
},
437475
{ discard: true },
438476
);

0 commit comments

Comments
 (0)