Skip to content

Commit 979c487

Browse files
committed
fix(server): keep new Codex SQLite state shadow-local
1 parent 4c1d99d commit 979c487

2 files changed

Lines changed: 113 additions & 6 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,57 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
210210
}),
211211
);
212212

213+
it.effect("keeps a SQLite family local when Codex creates it between passes", () =>
214+
Effect.gen(function* () {
215+
const fileSystem = yield* FileSystem.FileSystem;
216+
const path = yield* Path.Path;
217+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
218+
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
219+
const shadowHome = path.join(shadowRoot, "shadow");
220+
const layout = yield* resolveCodexHomeLayout(
221+
decodeCodexSettings({
222+
homePath: sharedHome,
223+
shadowHomePath: shadowHome,
224+
}),
225+
);
226+
227+
yield* materializeCodexShadowHome(layout);
228+
229+
for (const suffix of ["", "-wal", "-shm"]) {
230+
yield* writeTextFile(path.join(sharedHome, `queue_1.sqlite${suffix}`), `shared${suffix}`);
231+
yield* writeTextFile(path.join(shadowHome, `queue_1.sqlite${suffix}`), `shadow${suffix}`);
232+
yield* writeTextFile(
233+
path.join(sharedHome, `future_runtime.sqlite${suffix}`),
234+
`future-shared${suffix}`,
235+
);
236+
}
237+
yield* writeTextFile(path.join(shadowHome, "future_runtime.sqlite"), "future-shadow");
238+
239+
yield* materializeCodexShadowHome(layout);
240+
yield* materializeCodexShadowHome(layout);
241+
242+
for (const suffix of ["", "-wal", "-shm"]) {
243+
const entryName = `queue_1.sqlite${suffix}`;
244+
const sharedPath = path.join(sharedHome, entryName);
245+
const shadowPath = path.join(shadowHome, entryName);
246+
247+
expect(yield* fileSystem.readFileString(sharedPath)).toBe(`shared${suffix}`);
248+
expect(yield* fileSystem.readFileString(shadowPath)).toBe(`shadow${suffix}`);
249+
expect((yield* fileSystem.readLink(shadowPath).pipe(Effect.result))._tag).toBe("Failure");
250+
}
251+
252+
expect(
253+
yield* fileSystem.readFileString(path.join(shadowHome, "future_runtime.sqlite")),
254+
).toBe("future-shadow");
255+
expect(yield* fileSystem.exists(path.join(shadowHome, "future_runtime.sqlite-wal"))).toBe(
256+
false,
257+
);
258+
expect(yield* fileSystem.exists(path.join(shadowHome, "future_runtime.sqlite-shm"))).toBe(
259+
false,
260+
);
261+
}),
262+
);
263+
213264
it.effect("rejects shadow homes that point at the shared home", () =>
214265
Effect.gen(function* () {
215266
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
@@ -235,6 +286,7 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
235286

236287
it.effect("rejects shared entries that already exist in the shadow home as real files", () =>
237288
Effect.gen(function* () {
289+
const fileSystem = yield* FileSystem.FileSystem;
238290
const path = yield* Path.Path;
239291
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
240292
const shadowRoot = yield* makeTempDir("t3code-codex-shadow-root-");
@@ -262,6 +314,19 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
262314
expect(error.message).toBe(
263315
`Cannot create Codex shadow home entry 'config.toml' because '${path.join(shadowHome, "config.toml")}' already exists and is not a symlink.`,
264316
);
317+
expect(yield* fileSystem.readFileString(path.join(sharedHome, "config.toml"))).toBe(
318+
'model = "gpt-5-codex"\n',
319+
);
320+
expect(yield* fileSystem.readFileString(path.join(shadowHome, "config.toml"))).toBe(
321+
'model = "local"\n',
322+
);
323+
324+
yield* fileSystem.remove(path.join(shadowHome, "config.toml"));
325+
yield* materializeCodexShadowHome(layout);
326+
327+
expect(yield* fileSystem.readLink(path.join(shadowHome, "config.toml"))).toBe(
328+
path.join(sharedHome, "config.toml"),
329+
);
265330
}),
266331
);
267332

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ const KNOWN_SHARED_DIRECTORIES = [
3232
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "models_cache.json"]);
3333
const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp"]);
3434
const REPLACEABLE_SHARED_RUNTIME_DIRECTORIES = new Set(["mcp-oauth-locks"]);
35+
const SQLITE_FAMILY_SUFFIXES = ["", "-wal", "-shm"] as const;
36+
37+
function sqliteFamilyBaseName(entryName: string): string | undefined {
38+
const baseName = entryName.replace(/-(?:wal|shm)$/, "");
39+
return baseName.endsWith(".sqlite") ? baseName : undefined;
40+
}
3541

3642
function resolveHomePath(path: Path.Path, value: string | undefined): string {
3743
const expanded =
@@ -184,27 +190,27 @@ const readLinkState = Effect.fn("CodexHomeLayout.readLinkState")(function* (inpu
184190
);
185191
});
186192

187-
const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: {
193+
const removeSymlink = Effect.fn("CodexHomeLayout.removeSymlink")(function* (input: {
188194
readonly fileSystem: FileSystem.FileSystem;
189195
readonly sharedHomePath: string;
190196
readonly effectiveHomePath: string;
191197
readonly entryName: string;
192198
}): Effect.fn.Return<void, CodexShadowHomeError, Path.Path> {
193199
const path = yield* Path.Path;
194-
const privatePath = path.join(input.effectiveHomePath, input.entryName);
200+
const entryPath = path.join(input.effectiveHomePath, input.entryName);
195201
const state = yield* readLinkState({
196202
...input,
197-
linkPath: privatePath,
203+
linkPath: entryPath,
198204
});
199205
if (state._tag === "Symlink") {
200-
yield* input.fileSystem.remove(privatePath).pipe(
206+
yield* input.fileSystem.remove(entryPath).pipe(
201207
Effect.catchTags({
202208
PlatformError: (cause) =>
203209
new CodexShadowHomeFileSystemError({
204210
sharedHomePath: input.sharedHomePath,
205211
effectiveHomePath: input.effectiveHomePath,
206212
operation: "remove",
207-
path: privatePath,
213+
path: entryPath,
208214
entryName: input.entryName,
209215
cause,
210216
}),
@@ -377,12 +383,44 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
377383
}
378384
}
379385

386+
const shadowLocalSqliteFamilies = new Set<string>();
387+
for (const entryName of entries) {
388+
const familyBaseName = sqliteFamilyBaseName(entryName);
389+
if (familyBaseName === undefined || shadowLocalSqliteFamilies.has(familyBaseName)) {
390+
continue;
391+
}
392+
393+
const familyBaseState = yield* readLinkState({
394+
fileSystem,
395+
sharedHomePath: layout.sharedHomePath,
396+
effectiveHomePath,
397+
entryName: familyBaseName,
398+
linkPath: path.join(effectiveHomePath, familyBaseName),
399+
});
400+
if (familyBaseState._tag !== "NotSymlink") {
401+
continue;
402+
}
403+
404+
shadowLocalSqliteFamilies.add(familyBaseName);
405+
yield* Effect.forEach(
406+
SQLITE_FAMILY_SUFFIXES,
407+
(suffix) =>
408+
removeSymlink({
409+
fileSystem,
410+
sharedHomePath: layout.sharedHomePath,
411+
effectiveHomePath,
412+
entryName: `${familyBaseName}${suffix}`,
413+
}),
414+
{ discard: true },
415+
);
416+
}
417+
380418
yield* Effect.forEach(
381419
PRIVATE_ENTRY_NAMES,
382420
(entryName) =>
383421
entryName === "auth.json"
384422
? Effect.void
385-
: removePrivateSymlink({
423+
: removeSymlink({
386424
fileSystem,
387425
sharedHomePath: layout.sharedHomePath,
388426
effectiveHomePath,
@@ -397,6 +435,10 @@ export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome"
397435
if (PRIVATE_ENTRY_NAMES.has(entryName)) {
398436
return Effect.void;
399437
}
438+
const familyBaseName = sqliteFamilyBaseName(entryName);
439+
if (familyBaseName !== undefined && shadowLocalSqliteFamilies.has(familyBaseName)) {
440+
return Effect.void;
441+
}
400442
return ensureSymlink({
401443
fileSystem,
402444
sharedHomePath: layout.sharedHomePath,

0 commit comments

Comments
 (0)