Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexHomeLayout.test.ts

422 lines18.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7​
8import { CodexSettings } from "@t3tools/contracts";
9import {
10 CodexShadowHomeEntryConflictError,
11 CodexShadowHomePathConflictError,
12 materializeCodexShadowHome,
13 resolveCodexHomeLayout,
14} from "./CodexHomeLayout.ts";
15import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
16const decodeCodexSettingsValue = Schema.decodeSync(CodexSettings);
17​
18const decodeCodexSettings = (input: {
19 readonly enabled?: boolean;
20 readonly homePath?: string;
21 readonly shadowHomePath?: string;
22 readonly customModels?: readonly string[];
23 readonly binaryPath?: string;
24}): CodexSettings => decodeCodexSettingsValue(input);
25​
26const makeTempDir = Effect.fn("CodexHomeLayout.test.makeTempDir")(function* (prefix: string) {
27 const fileSystem = yield* FileSystem.FileSystem;
28 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
29});
30​
31const writeTextFile = Effect.fn("CodexHomeLayout.test.writeTextFile")(function* (
32 filePath: string,
33 contents: string,
34) {
35 const fileSystem = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true });
38 yield* fileSystem.writeFileString(filePath, contents);
39});
40​
41it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
42 describe("resolveCodexHomeLayout", () => {
43 it.effect("uses the shared home for continuation and the shadow home for runtime", () =>
44 Effect.gen(function* () {
45 const path = yield* Path.Path;
46 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
47 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
48 const shadowHome = path.join(shadowRoot, "shadow");
49​
50 const layout = yield* resolveCodexHomeLayout(
51 decodeCodexSettings({
52 homePath: sharedHome,
53 shadowHomePath: shadowHome,
54 }),
55 );
56​
57 expect(layout).toMatchObject({
58 mode: "authOverlay",
59 sharedHomePath: sharedHome,
60 effectiveHomePath: shadowHome,
61 continuationKey: `codex:home:${sharedHome}`,
62 });
63 }),
64 );
65 });
66​
67 describe("materializeCodexShadowHome", () => {
68 it.effect.skipIf(!symlinksSupported)(
69 "keeps Finder metadata local across repeated shadow home setup",
70 () =>
71 Effect.gen(function* () {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
75 const shadowHome = yield* makeTempDir("t3code-codex-shadow-");
76 const sharedMetadata = path.join(sharedHome, ".DS_Store");
77 const shadowMetadata = path.join(shadowHome, ".DS_Store");
78​
79 yield* writeTextFile(sharedMetadata, "shared Finder metadata");
80 yield* writeTextFile(shadowMetadata, "shadow Finder metadata");
81 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
82 const layout = yield* resolveCodexHomeLayout(
83 decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
84 );
85​
86 yield* materializeCodexShadowHome(layout);
87 expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata");
88 yield* fileSystem.remove(shadowMetadata);
89 yield* materializeCodexShadowHome(layout);
90 expect(yield* fileSystem.exists(shadowMetadata)).toBe(false);
91​
92 yield* writeTextFile(shadowMetadata, "shadow Finder metadata");
93 yield* materializeCodexShadowHome(layout);
94 yield* materializeCodexShadowHome(layout);
95​
96 expect(yield* fileSystem.readFileString(sharedMetadata)).toBe("shared Finder metadata");
97 expect(yield* fileSystem.readFileString(shadowMetadata)).toBe("shadow Finder metadata");
98 expect(yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"))).toBe(
99 '{"shadow":true}\n',
100 );
101 expect(yield* fileSystem.readLink(path.join(shadowHome, "sessions"))).toBe(
102 path.join(sharedHome, "sessions"),
103 );
104 }),
105 );
106​
107 it.effect.skipIf(!symlinksSupported)(
108 "materializes a shadow home with shared state links and private auth",
109 () =>
110 Effect.gen(function* () {
111 const fileSystem = yield* FileSystem.FileSystem;
112 const path = yield* Path.Path;
113 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
114 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
115 const shadowHome = path.join(shadowRoot, "shadow");
116​
117 yield* fileSystem.makeDirectory(path.join(sharedHome, "sessions"));
118 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
119 yield* writeTextFile(
120 path.join(sharedHome, "models_cache.json"),
121 '{"models":["shared"]}\n',
122 );
123 yield* writeTextFile(path.join(sharedHome, "auth.json"), '{"shared":true}\n');
124 yield* writeTextFile(path.join(sharedHome, "auth.lock"), "");
125 yield* fileSystem.makeDirectory(shadowHome, { recursive: true });
126 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
127 yield* writeTextFile(path.join(shadowHome, "auth.lock"), "");
128 yield* fileSystem.symlink(
129 path.join(sharedHome, "models_cache.json"),
130 path.join(shadowHome, "models_cache.json"),
131 );
132​
133 const layout = yield* resolveCodexHomeLayout(
134 decodeCodexSettings({
135 homePath: sharedHome,
136 shadowHomePath: shadowHome,
137 }),
138 );
139​
140 yield* materializeCodexShadowHome(layout);
141​
142 const sessionsTarget = yield* fileSystem.readLink(path.join(shadowHome, "sessions"));
143 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
144 const mcpOauthLocksTarget = yield* fileSystem.readLink(
145 path.join(shadowHome, "mcp-oauth-locks"),
146 );
147 const modelsCacheExists = yield* fileSystem.exists(
148 path.join(shadowHome, "models_cache.json"),
149 );
150 const authLinkResult = yield* fileSystem
151 .readLink(path.join(shadowHome, "auth.json"))
152 .pipe(Effect.result);
153 const authContents = yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"));
154 const authLockLinkResult = yield* fileSystem
155 .readLink(path.join(shadowHome, "auth.lock"))
156 .pipe(Effect.result);
157​
158 expect(sessionsTarget).toBe(path.join(sharedHome, "sessions"));
159 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
160 expect(mcpOauthLocksTarget).toBe(path.join(sharedHome, "mcp-oauth-locks"));
161 expect(modelsCacheExists).toBe(false);
162 expect(authLinkResult._tag).toBe("Failure");
163 expect(authContents).toContain("shadow");
164 expect(authLockLinkResult._tag).toBe("Failure");
165 }),
166 );
167​
168 it.effect.skipIf(!symlinksSupported)(
169 "keeps originator-partitioned models cache files private to each home",
170 () =>
171 Effect.gen(function* () {
172 const fileSystem = yield* FileSystem.FileSystem;
173 const path = yield* Path.Path;
174 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
175 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
176 const shadowHome = path.join(shadowRoot, "shadow");
177 const sharedDesktopCache = path.join(sharedHome, "models_cache.codex_work_desktop.json");
178 const shadowDesktopCache = path.join(shadowHome, "models_cache.codex_work_desktop.json");
179 const sharedWorkCache = path.join(sharedHome, "models_cache.codex_work_cca.json");
180 const shadowWorkCache = path.join(shadowHome, "models_cache.codex_work_cca.json");
181 const sharedOrbitCache = path.join(sharedHome, "models_cache.orbit_cca_desktop.json");
182 const shadowOrbitCache = path.join(shadowHome, "models_cache.orbit_cca_desktop.json");
183​
184 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
185 yield* writeTextFile(sharedDesktopCache, '{"owner":"shared-desktop"}\n');
186 yield* writeTextFile(shadowDesktopCache, '{"owner":"shadow-desktop"}\n');
187 yield* writeTextFile(sharedWorkCache, '{"owner":"shared-work"}\n');
188 yield* writeTextFile(sharedOrbitCache, '{"owner":"shared-orbit"}\n');
189 yield* fileSystem.symlink(sharedWorkCache, shadowWorkCache);
190​
191 const layout = yield* resolveCodexHomeLayout(
192 decodeCodexSettings({
193 homePath: sharedHome,
194 shadowHomePath: shadowHome,
195 }),
196 );
197​
198 yield* materializeCodexShadowHome(layout);
199 yield* materializeCodexShadowHome(layout);
200​
201 const desktopCacheLinkResult = yield* fileSystem
202 .readLink(shadowDesktopCache)
203 .pipe(Effect.result);
204​
205 expect(yield* fileSystem.readFileString(sharedDesktopCache)).toBe(
206 '{"owner":"shared-desktop"}\n',
207 );
208 expect(yield* fileSystem.readFileString(shadowDesktopCache)).toBe(
209 '{"owner":"shadow-desktop"}\n',
210 );
211 expect(desktopCacheLinkResult._tag).toBe("Failure");
212 expect(yield* fileSystem.exists(shadowWorkCache)).toBe(false);
213 expect(yield* fileSystem.readFileString(sharedWorkCache)).toBe(
214 '{"owner":"shared-work"}\n',
215 );
216 expect(yield* fileSystem.exists(shadowOrbitCache)).toBe(false);
217 expect(yield* fileSystem.readLink(path.join(shadowHome, "config.toml"))).toBe(
218 path.join(sharedHome, "config.toml"),
219 );
220 }),
221 );
222​
223 it.effect.skipIf(!symlinksSupported)(
224 "replaces a Codex-created local sqlite maintenance lock with the shared lock",
225 () =>
226 Effect.gen(function* () {
227 const fileSystem = yield* FileSystem.FileSystem;
228 const path = yield* Path.Path;
229 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
230 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
231 const shadowHome = path.join(shadowRoot, "shadow");
232 const sharedLock = path.join(sharedHome, ".sqlite-maintenance.lock");
233 const shadowLock = path.join(shadowHome, ".sqlite-maintenance.lock");
234​
235 yield* writeTextFile(sharedLock, "");
236 yield* writeTextFile(shadowLock, "");
237​
238 const layout = yield* resolveCodexHomeLayout(
239 decodeCodexSettings({
240 homePath: sharedHome,
241 shadowHomePath: shadowHome,
242 }),
243 );
244​
245 yield* materializeCodexShadowHome(layout);
246​
247 expect(yield* fileSystem.readLink(shadowLock)).toBe(sharedLock);
248 }),
249 );
250​
251 it.effect.skipIf(!symlinksSupported)(
252 "replaces Codex-created local MCP OAuth locks with the shared lock directory",
253 () =>
254 Effect.gen(function* () {
255 const fileSystem = yield* FileSystem.FileSystem;
256 const path = yield* Path.Path;
257 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
258 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
259 const shadowHome = path.join(shadowRoot, "shadow");
260 const sharedLocks = path.join(sharedHome, "mcp-oauth-locks");
261 const shadowLocks = path.join(shadowHome, "mcp-oauth-locks");
262​
263 yield* writeTextFile(path.join(sharedLocks, "file-store.lock"), "");
264 yield* writeTextFile(path.join(shadowLocks, "file-store.lock"), "");
265​
266 const layout = yield* resolveCodexHomeLayout(
267 decodeCodexSettings({
268 homePath: sharedHome,
269 shadowHomePath: shadowHome,
270 }),
271 );
272​
273 yield* materializeCodexShadowHome(layout);
274​
275 const locksTarget = yield* fileSystem.readLink(shadowLocks);
276 const sharedLockExists = yield* fileSystem.exists(
277 path.join(sharedLocks, "file-store.lock"),
278 );
279​
280 expect(locksTarget).toBe(sharedLocks);
281 expect(sharedLockExists).toBe(true);
282 }),
283 );
284​
285 it.effect.skipIf(!symlinksSupported)(
286 "shares local thread writer locks once no writer still holds one",
287 () =>
288 Effect.gen(function* () {
289 const fileSystem = yield* FileSystem.FileSystem;
290 const path = yield* Path.Path;
291 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
292 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
293 const shadowHome = path.join(shadowRoot, "shadow");
294 const shadowLocks = path.join(shadowHome, "thread-writer-locks");
295 const threadLock = path.join(shadowLocks, "019a0000-0000-7000-8000-000000000000.lock");
296​
297 yield* writeTextFile(path.join(shadowLocks, ".coordination.lock"), "");
298 yield* writeTextFile(path.join(shadowLocks, ".DS_Store"), "");
299 yield* writeTextFile(threadLock, "");
300​
301 const layout = yield* resolveCodexHomeLayout(
302 decodeCodexSettings({
303 homePath: sharedHome,
304 shadowHomePath: shadowHome,
305 }),
306 );
307​
308 yield* materializeCodexShadowHome(layout);
309 expect(yield* fileSystem.exists(threadLock)).toBe(true);
310​
311 yield* fileSystem.remove(threadLock);
312 yield* materializeCodexShadowHome(layout);
313 expect(yield* fileSystem.readLink(shadowLocks)).toBe(
314 path.join(sharedHome, "thread-writer-locks"),
315 );
316 }),
317 );
318​
319 it.effect.skipIf(!symlinksSupported)(
320 "accepts Codex-created shadow-local runtime directories",
321 () =>
322 Effect.gen(function* () {
323 const fileSystem = yield* FileSystem.FileSystem;
324 const path = yield* Path.Path;
325 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
326 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
327 const shadowHome = path.join(shadowRoot, "shadow");
328​
329 yield* fileSystem.makeDirectory(path.join(sharedHome, "log"));
330 yield* fileSystem.makeDirectory(path.join(sharedHome, "memories"));
331 yield* fileSystem.makeDirectory(path.join(sharedHome, "tmp"));
332 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
333 yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
334 yield* fileSystem.makeDirectory(path.join(shadowHome, "log"), { recursive: true });
335 yield* fileSystem.makeDirectory(path.join(shadowHome, "memories"), { recursive: true });
336 yield* fileSystem.makeDirectory(path.join(shadowHome, "tmp"), { recursive: true });
337​
338 const layout = yield* resolveCodexHomeLayout(
339 decodeCodexSettings({
340 homePath: sharedHome,
341 shadowHomePath: shadowHome,
342 }),
343 );
344​
345 yield* materializeCodexShadowHome(layout);
346​
347 const configTarget = yield* fileSystem.readLink(path.join(shadowHome, "config.toml"));
348 const logLinkResult = yield* fileSystem
349 .readLink(path.join(shadowHome, "log"))
350 .pipe(Effect.result);
351 const memoriesLinkResult = yield* fileSystem
352 .readLink(path.join(shadowHome, "memories"))
353 .pipe(Effect.result);
354 const tmpLinkResult = yield* fileSystem
355 .readLink(path.join(shadowHome, "tmp"))
356 .pipe(Effect.result);
357​
358 expect(configTarget).toBe(path.join(sharedHome, "config.toml"));
359 expect(logLinkResult._tag).toBe("Failure");
360 expect(memoriesLinkResult._tag).toBe("Failure");
361 expect(tmpLinkResult._tag).toBe("Failure");
362 }),
363 );
364​
365 it.effect("rejects shadow homes that point at the shared home", () =>
366 Effect.gen(function* () {
367 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
368 const layout = yield* resolveCodexHomeLayout(
369 decodeCodexSettings({
370 homePath: sharedHome,
371 shadowHomePath: sharedHome,
372 }),
373 );
374​
375 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
376​
377 expect(error).toBeInstanceOf(CodexShadowHomePathConflictError);
378 expect(error).toMatchObject({
379 sharedHomePath: sharedHome,
380 effectiveHomePath: sharedHome,
381 });
382 expect(error.message).toBe(
383 `Codex shadow home path '${sharedHome}' must be different from the shared home path '${sharedHome}'.`,
384 );
385 }),
386 );
387​
388 it.effect.skipIf(!symlinksSupported)(
389 "rejects shared entries that already exist in the shadow home as real files",
390 () =>
391 Effect.gen(function* () {
392 const path = yield* Path.Path;
393 const sharedHome = yield* makeTempDir("t3code-codex-shared-");
394 const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
395 const shadowHome = path.join(shadowRoot, "shadow");
396 yield* writeTextFile(path.join(sharedHome, "config.toml"), 'model = "gpt-5-codex"\n');
397 yield* writeTextFile(path.join(shadowHome, "config.toml"), 'model = "local"\n');
398​
399 const layout = yield* resolveCodexHomeLayout(
400 decodeCodexSettings({
401 homePath: sharedHome,
402 shadowHomePath: shadowHome,
403 }),
404 );
405​
406 const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
407​
408 expect(error).toBeInstanceOf(CodexShadowHomeEntryConflictError);
409 expect(error).toMatchObject({
410 sharedHomePath: sharedHome,
411 effectiveHomePath: shadowHome,
412 entryName: "config.toml",
413 linkPath: path.join(shadowHome, "config.toml"),
414 targetPath: path.join(sharedHome, "config.toml"),
415 });
416 expect(error.message).toBe(
417 `Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`,
418 );
419 }),
420 );
421 });
422});