Skip to content

Commit 8e7a53e

Browse files
fix(server): managed Codex accounts connect on Windows without Developer Mode
1 parent 21da306 commit 8e7a53e

2 files changed

Lines changed: 124 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", () =>
@@ -308,7 +335,10 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
308335
}),
309336
);
310337

311-
const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
338+
const error = yield* materializeCodexShadowHome(layout).pipe(
339+
Effect.provideService(HostProcess.Platform, "linux"),
340+
Effect.flip,
341+
);
312342

313343
expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
314344
expect(error).toMatchObject({
@@ -323,5 +353,63 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
323353
);
324354
}),
325355
);
356+
357+
it.effect("links shared directories as junctions when Windows refuses symlinks", () =>
358+
Effect.gen(function* () {
359+
const fileSystem = yield* FileSystem.FileSystem;
360+
const path = yield* Path.Path;
361+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
362+
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
363+
const shadowHome = path.join(shadowRoot, "shadow");
364+
yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions"));
365+
366+
const layout = yield* resolveCodexHomeLayout(
367+
decodeCodexSettings({
368+
homePath: sharedHome,
369+
shadowHomePath: shadowHome,
370+
}),
371+
);
372+
373+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
374+
// A second pass finds the junctions already in place.
375+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
376+
yield* writeTextFile(path.join(shadowHome, "sessions", "rollout.jsonl"), "{}\n");
377+
378+
expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe(
379+
path.join(sharedHome, "sessions"),
380+
);
381+
expect(yield* fileSystem.exists(path.join(sharedHome, "sessions", "rollout.jsonl"))).toBe(
382+
true,
383+
);
384+
}),
385+
);
386+
387+
it.effect("keeps shared files local when Windows refuses symlinks", () =>
388+
Effect.gen(function* () {
389+
const fileSystem = yield* FileSystem.FileSystem;
390+
const path = yield* Path.Path;
391+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
392+
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
393+
const shadowHome = path.join(shadowRoot, "shadow");
394+
yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
395+
yield* writeTextFile(path.join(sharedHome, "installation_id"), "shared\n");
396+
// Codex created its own copy after an earlier pass could not link the shared one.
397+
yield* writeTextFile(path.join(shadowHome, "installation_id"), "shadow\n");
398+
399+
const layout = yield* resolveCodexHomeLayout(
400+
decodeCodexSettings({
401+
homePath: sharedHome,
402+
shadowHomePath: shadowHome,
403+
}),
404+
);
405+
406+
yield* onWindowsWithoutSymlinks(materializeCodexShadowHome(layout));
407+
408+
expect(yield* fileSystem.exists(path.join(shadowHome, "config.toml"))).toBe(false);
409+
expect(yield* fileSystem.readFileString(path.join(shadowHome, "installation_id"))).toBe(
410+
"shadow\n",
411+
);
412+
}),
413+
);
326414
});
327415
});

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

Lines changed: 35 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";
@@ -27,6 +30,7 @@ const KNOWN_SHARED_DIRECTORIES = [
2730
"logs",
2831
"mcp-oauth-locks",
2932
] as const;
33+
const KNOWN_SHARED_DIRECTORY_NAMES: ReadonlySet<string> = new Set(KNOWN_SHARED_DIRECTORIES);
3034

3135
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "models_cache.json"]);
3236
// Finder maintains metadata separately for each directory, including shadow homes.
@@ -241,6 +245,23 @@ const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (inpu
241245
cause,
242246
}),
243247
}),
248+
// Windows refuses symlinks without Developer Mode, but a junction needs no privilege.
249+
// Junctions only link directories on local volumes, so they are only a fallback.
250+
Effect.catch((error) =>
251+
Effect.gen(function* () {
252+
const isDirectory =
253+
(yield* HostProcess.isWindows) &&
254+
(yield* input.fileSystem.stat(target).pipe(
255+
Effect.map((info) => info.type === "Directory"),
256+
Effect.orElseSucceed(() => false),
257+
));
258+
if (!isDirectory) return yield* error;
259+
return yield* Effect.tryPromise({
260+
try: () => NodeFSP.symlink(target, link, "junction"),
261+
catch: () => error,
262+
});
263+
}),
264+
),
244265
);
245266

246267
if (state._tag === "NotSymlink") {
@@ -392,18 +413,31 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
392413
{ discard: true },
393414
);
394415

416+
const isWindows = yield* HostProcess.isWindows;
395417
yield* Effect.forEach(
396418
entries,
397419
(entryName) => {
398420
if (PRIVATE_ENTRY_NAMES.has(entryName)) {
399421
return Effect.void;
400422
}
401-
return ensureSymlink({
423+
const linked = ensureSymlink({
402424
fileSystem,
403425
sharedHomePath: layout.sharedHomePath,
404426
effectiveHomePath,
405427
entryName,
406428
});
429+
// Without Developer Mode, Windows refuses to symlink a file, and Codex then creates its own
430+
// copy in the shadow home. Either way the entry stays local instead of disabling the provider.
431+
return isWindows && !KNOWN_SHARED_DIRECTORY_NAMES.has(entryName)
432+
? linked.pipe(
433+
Effect.catch((error) =>
434+
Effect.logDebug("Codex shadow home entry is not shared.", {
435+
entryName,
436+
detail: error.message,
437+
}),
438+
),
439+
)
440+
: linked;
407441
},
408442
{ discard: true },
409443
);

0 commit comments

Comments
 (0)