Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions apps/server/src/provider/Drivers/CodexHomeLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,72 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
}),
);

it.effect.skipIf(!symlinksSupported)(
"keeps auth-like entries private to each home without touching shared copies",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
const shadowHome = path.join(shadowRoot, "shadow");
const authLikeNames = [
"auth-fixture-a.json",
"auth_fixture_b.json",
"auth.json.bak",
"AUTH.Backup",
"auth.lock",
];

for (const name of authLikeNames) {
yield* writeTextFile(path.join(sharedHome, name), `{"shared":"${name}"}\n`);
}
yield* writeTextFile(path.join(sharedHome, "author.json"), '{"author":true}\n');
yield* writeTextFile(path.join(shadowHome, "auth.json"), '{"shadow":true}\n');
yield* fileSystem.symlink(
path.join(sharedHome, "auth.lock"),
path.join(shadowHome, "auth.lock"),
);

const layout = yield* resolveCodexHomeLayout(
decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
);

yield* materializeCodexShadowHome(layout);

for (const name of authLikeNames) {
const shadowEntry = yield* fileSystem
.stat(path.join(shadowHome, name))
.pipe(Effect.result);
expect({ name, tag: shadowEntry._tag }).toEqual({ name, tag: "Failure" });
expect(yield* fileSystem.readFileString(path.join(sharedHome, name))).toBe(
`{"shared":"${name}"}\n`,
);
}
expect(yield* fileSystem.readLink(path.join(shadowHome, "author.json"))).toBe(
path.join(sharedHome, "author.json"),
);
expect(yield* fileSystem.readFileString(path.join(shadowHome, "auth.json"))).toBe(
'{"shadow":true}\n',
);

// Older versions linked auth-like backups into shadow homes; those links fail closed.
const backupLink = path.join(shadowHome, "auth.json.bak");
yield* fileSystem.symlink(path.join(sharedHome, "auth.json.bak"), backupLink);
const error = yield* materializeCodexShadowHome(layout).pipe(Effect.flip);
expect(error).toMatchObject({
_tag: "CodexShadowHomePrivateEntrySymlinkError",
entryName: "auth.json.bak",
});
expect(yield* fileSystem.readLink(backupLink)).toBe(
path.join(sharedHome, "auth.json.bak"),
);
expect(yield* fileSystem.readFileString(path.join(sharedHome, "auth.json.bak"))).toBe(
'{"shared":"auth.json.bak"}\n',
);
}),
);

it.effect.skipIf(!symlinksSupported)(
"replaces a Codex-created local sqlite maintenance lock with the shared lock",
() =>
Expand Down
56 changes: 40 additions & 16 deletions apps/server/src/provider/Drivers/CodexHomeLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const KNOWN_SHARED_DIRECTORIES = [

// auth.lock guards the shadow home's private auth.json.
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "auth.lock", "models_cache.json"]);
// Credential backups and alternate auth files (auth.json.bak, auth-work.json) stay per-home,
// but unrelated names such as author.json remain shared.
const AUTH_LIKE_ENTRY_NAME = /^auth(?:$|[._-])/i;
// Finder maintains metadata separately for each directory, including shadow homes.
const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp", ".DS_Store"]);
// Shadow homes may create local runtime locks first; replace them with shared links because they guard shared state.
Expand Down Expand Up @@ -345,21 +348,38 @@ const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsP
readonly effectiveHomePath: string;
}): Effect.fn.Return<void, CodexShadowHomeError, Path.Path> {
const path = yield* Path.Path;
const entryName = "auth.json";
const authPath = path.join(input.effectiveHomePath, entryName);
const state = yield* readLinkState({
...input,
entryName,
linkPath: authPath,
});
if (state._tag === "Symlink") {
return yield* new CodexShadowHomePrivateEntrySymlinkError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
entryName,
path: authPath,
});
}
const shadowEntryNames = yield* input.fileSystem.readDirectory(input.effectiveHomePath).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "readDirectory",
path: input.effectiveHomePath,
cause,
}),
}),
);
yield* Effect.forEach(
shadowEntryNames.filter((entryName) => AUTH_LIKE_ENTRY_NAME.test(entryName)),
Effect.fnUntraced(function* (entryName) {
const authPath = path.join(input.effectiveHomePath, entryName);
const state = yield* readLinkState({
...input,
entryName,
linkPath: authPath,
});
if (state._tag === "Symlink") {
return yield* new CodexShadowHomePrivateEntrySymlinkError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
entryName,
path: authPath,
});
}
}),
{ discard: true },
);
},
);

Expand Down Expand Up @@ -418,7 +438,11 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
);
const entries = new Set<string>(KNOWN_SHARED_DIRECTORIES);
for (const entryName of sharedEntryNames) {
if (!PRIVATE_ENTRY_NAMES.has(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName)) {
if (
!PRIVATE_ENTRY_NAMES.has(entryName) &&
!SHADOW_LOCAL_ENTRY_NAMES.has(entryName) &&
!AUTH_LIKE_ENTRY_NAME.test(entryName)
) {
entries.add(entryName);
}
}
Expand Down
Loading