Skip to content

Commit d8f480a

Browse files
fix(server): partitioned Codex models_cache files no longer break shadow homes (#45450)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent f583694 commit d8f480a

2 files changed

Lines changed: 86 additions & 18 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,61 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
165165
}),
166166
);
167167

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+
168223
it.effect.skipIf(!symlinksSupported)(
169224
"replaces a Codex-created local sqlite maintenance lock with the shared lock",
170225
() =>

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const KNOWN_SHARED_DIRECTORIES = [
3131

3232
// auth.lock guards the shadow home's private auth.json.
3333
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "auth.lock", "models_cache.json"]);
34+
// Codex writes originator-partitioned catalogs as models_cache.<originator>.json.
35+
const PARTITIONED_MODELS_CACHE_PATTERN = /^models_cache\.[^.]+\.json$/;
36+
37+
function isPrivateEntryName(entryName: string): boolean {
38+
return PRIVATE_ENTRY_NAMES.has(entryName) || PARTITIONED_MODELS_CACHE_PATTERN.test(entryName);
39+
}
40+
3441
// Finder maintains metadata separately for each directory, including shadow homes.
3542
const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp", ".DS_Store"]);
3643
// Shadow homes may create local runtime locks first; replace them with shared links because they guard shared state.
@@ -404,43 +411,49 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
404411
{ concurrency: "unbounded" },
405412
);
406413

407-
const sharedEntryNames = yield* fileSystem.readDirectory(layout.sharedHomePath).pipe(
408-
Effect.catchTags({
409-
PlatformError: (cause) =>
410-
new CodexShadowHomeFileSystemError({
411-
sharedHomePath: layout.sharedHomePath,
412-
effectiveHomePath,
413-
operation: "readDirectory",
414-
path: layout.sharedHomePath,
415-
cause,
416-
}),
417-
}),
414+
const readDirectoryEntries = (directoryPath: string) =>
415+
fileSystem.readDirectory(directoryPath).pipe(
416+
Effect.catchTags({
417+
PlatformError: (cause) =>
418+
new CodexShadowHomeFileSystemError({
419+
sharedHomePath: layout.sharedHomePath,
420+
effectiveHomePath,
421+
operation: "readDirectory",
422+
path: directoryPath,
423+
cause,
424+
}),
425+
}),
426+
);
427+
428+
const [sharedEntryNames, shadowEntryNames] = yield* Effect.all(
429+
[readDirectoryEntries(layout.sharedHomePath), readDirectoryEntries(effectiveHomePath)],
430+
{ concurrency: "unbounded" },
418431
);
419432
const entries = new Set<string>(KNOWN_SHARED_DIRECTORIES);
420433
for (const entryName of sharedEntryNames) {
421-
if (!PRIVATE_ENTRY_NAMES.has(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName)) {
434+
if (!isPrivateEntryName(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName)) {
422435
entries.add(entryName);
423436
}
424437
}
425438

426439
yield* Effect.forEach(
427-
PRIVATE_ENTRY_NAMES,
440+
shadowEntryNames,
428441
(entryName) =>
429-
entryName === "auth.json"
430-
? Effect.void
431-
: removePrivateSymlink({
442+
entryName !== "auth.json" && isPrivateEntryName(entryName)
443+
? removePrivateSymlink({
432444
fileSystem,
433445
sharedHomePath: layout.sharedHomePath,
434446
effectiveHomePath,
435447
entryName,
436-
}),
448+
})
449+
: Effect.void,
437450
{ discard: true },
438451
);
439452

440453
yield* Effect.forEach(
441454
entries,
442455
(entryName) => {
443-
if (PRIVATE_ENTRY_NAMES.has(entryName)) {
456+
if (isPrivateEntryName(entryName)) {
444457
return Effect.void;
445458
}
446459
return ensureSymlink({

0 commit comments

Comments
 (0)