Skip to content

Commit 489522f

Browse files
committed
fix(server): keep existing Codex WAL data accessible
1 parent 211cbb6 commit 489522f

6 files changed

Lines changed: 112 additions & 21 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,12 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter
15681568
const serverConfig = yield* ServerConfig;
15691569
const homeLayout = yield* resolveCodexHomeLayout(config);
15701570

1571-
yield* materializeCodexShadowHome(homeLayout).pipe(
1571+
const environmentOverrides = yield* mergeProviderInstanceEnvironment(
1572+
environment,
1573+
hostEnvironment,
1574+
);
1575+
const processEnv = yield* materializeCodexShadowHome(homeLayout).pipe(
1576+
Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)),
15721577
Effect.mapError(
15731578
(cause) =>
15741579
new ProviderAdapterDriverCreateError({
@@ -1590,10 +1595,7 @@ export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapter
15901595
return yield* makeCodexAdapterV2({
15911596
instanceId,
15921597
settings,
1593-
environment: codexHomeEnvironment(
1594-
homeLayout,
1595-
yield* mergeProviderInstanceEnvironment(environment, hostEnvironment),
1596-
),
1598+
environment: processEnv,
15971599
clientFactory,
15981600
crypto,
15991601
fileSystem,

‎apps/server/src/provider/CodexManagedRuntime.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
9191
environmentId,
9292
profileKey,
9393
]);
94-
yield* materializeCodexShadowHome(homeLayout).pipe(
94+
const homeEnvironment = yield* materializeCodexShadowHome(homeLayout).pipe(
95+
Effect.andThen(() => codexHomeEnvironment(homeLayout, options.environment)),
9596
Effect.provideService(FileSystem.FileSystem, fs),
9697
Effect.provideService(Path.Path, path),
9798
Effect.mapError(
@@ -115,7 +116,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
115116
);
116117
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
117118
const environment: NodeJS.ProcessEnv = {
118-
...codexHomeEnvironment(homeLayout, options.environment),
119+
...homeEnvironment,
119120
CODEX_HOME: homePath,
120121
};
121122
delete environment.ACCESS_TOKEN;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ it.layer(layerTest)("CodexDriver", (it) => {
317317
expect(launch.settings.launchArgs).not.toContain("dummy-owned-access");
318318
expect(launch.environment.OPENAI_API_KEY).toBeUndefined();
319319
expect(launch.environment.CODEX_HOME).toBe(launch.settings.homePath);
320-
expect(launch.environment.CODEX_SQLITE_HOME).toBe(sharedHome);
320+
expect(launch.environment.CODEX_SQLITE_HOME).toBe(shadowHome);
321321
const before = yield* instance.auth!.subscribe("test-owner").pipe(Stream.runHead);
322322
expect(Option.getOrThrow(before).phase).toBe("idle");
323323
yield* instance.auth!.logout(Effect.void);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ export const CodexDriver: ProviderDriver<
136136
const modelCatalog = yield* ModelCatalog.ModelCatalog;
137137
const currentCatalog = modelCatalog.current(DRIVER_KIND);
138138
const homeLayout = yield* resolveCodexHomeLayout(config);
139-
const processEnv = codexHomeEnvironment(
140-
homeLayout,
141-
yield* mergeProviderInstanceEnvironment(environment),
142-
);
139+
const environmentOverrides = yield* mergeProviderInstanceEnvironment(environment);
143140
const continuationIdentity = codexContinuationIdentity(homeLayout);
144141
const stampIdentity = withInstanceIdentity({
145142
instanceId,
@@ -148,7 +145,8 @@ export const CodexDriver: ProviderDriver<
148145
accentColor,
149146
continuationGroupKey: continuationIdentity.continuationKey,
150147
});
151-
yield* materializeCodexShadowHome(homeLayout).pipe(
148+
const processEnv = yield* materializeCodexShadowHome(homeLayout).pipe(
149+
Effect.andThen(() => codexHomeEnvironment(homeLayout, environmentOverrides)),
152150
Effect.mapError(
153151
(cause) =>
154152
new ProviderDriverError({

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeSqlite from "node:sqlite";
3+
14
import * as NodeServices from "@effect/platform-node/NodeServices";
25
import { describe, expect, it } from "@effect/vitest";
36
import * as Effect from "effect/Effect";
@@ -6,9 +9,11 @@ import * as Path from "effect/Path";
69
import * as Schema from "effect/Schema";
710

811
import { CodexSettings } from "@t3tools/contracts";
12+
import * as HostProcess from "@t3tools/shared/HostProcess";
913
import {
1014
CodexShadowHomeEntryConflictError,
1115
CodexShadowHomePathConflictError,
16+
codexHomeEnvironment,
1217
materializeCodexShadowHome,
1318
resolveCodexHomeLayout,
1419
} from "./CodexHomeLayout.ts";
@@ -64,6 +69,65 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
6469
);
6570
});
6671

72+
describe("codexHomeEnvironment", () => {
73+
it.effect.each(["live", "interrupted"] as const)(
74+
"reads committed shadow WAL data with a %s connection",
75+
(connection) =>
76+
Effect.gen(function* () {
77+
const fileSystem = yield* FileSystem.FileSystem;
78+
const path = yield* Path.Path;
79+
const sharedHome = yield* makeTempDir("t3code-codex-shared-");
80+
const shadowHome = yield* makeTempDir("t3code-codex-shadow-");
81+
const sharedDatabasePath = path.join(sharedHome, "state_5.sqlite");
82+
const shadowDatabasePath = path.join(shadowHome, "state_5.sqlite");
83+
{
84+
using seed = new NodeSqlite.DatabaseSync(sharedDatabasePath);
85+
seed.exec("PRAGMA journal_mode = WAL; CREATE TABLE entries (value TEXT);");
86+
}
87+
// Windows places sidecars beside database links; Unix resolves the target first.
88+
if ((yield* HostProcess.isWindows) && symlinksSupported) {
89+
yield* fileSystem.symlink(sharedDatabasePath, shadowDatabasePath);
90+
} else {
91+
yield* fileSystem.copyFile(sharedDatabasePath, shadowDatabasePath);
92+
}
93+
using writer = new NodeSqlite.DatabaseSync(shadowDatabasePath);
94+
writer.exec("PRAGMA wal_autocheckpoint = 0; INSERT INTO entries VALUES ('committed');");
95+
const database = yield* fileSystem.readFile(shadowDatabasePath);
96+
const walPath = `${shadowDatabasePath}-wal`;
97+
const wal = yield* fileSystem.readFile(walPath);
98+
expect(wal.byteLength).toBeGreaterThan(0);
99+
if (connection === "interrupted") {
100+
// Restore the uncheckpointed disk image only after SQLite closes our fixture.
101+
writer.close();
102+
yield* fileSystem.writeFile(shadowDatabasePath, database);
103+
yield* fileSystem.writeFile(walPath, wal);
104+
}
105+
{
106+
using sharedReader = new NodeSqlite.DatabaseSync(sharedDatabasePath);
107+
expect(sharedReader.prepare("SELECT value FROM entries").all()).toEqual([]);
108+
}
109+
110+
const layout = yield* resolveCodexHomeLayout(
111+
decodeCodexSettings({ homePath: sharedHome, shadowHomePath: shadowHome }),
112+
);
113+
const environment = yield* codexHomeEnvironment(layout, {});
114+
expect(yield* fileSystem.readFile(walPath)).toEqual(wal);
115+
using reader = new NodeSqlite.DatabaseSync(
116+
path.join(environment.CODEX_SQLITE_HOME!, "state_5.sqlite"),
117+
);
118+
expect(reader.prepare("SELECT value FROM entries").all()).toEqual([
119+
{ value: "committed" },
120+
]);
121+
if (connection === "live") {
122+
writer.exec("INSERT INTO entries VALUES ('still writable');");
123+
expect(reader.prepare("SELECT count(*) AS count FROM entries").get()).toEqual({
124+
count: 2,
125+
});
126+
}
127+
}),
128+
);
129+
});
130+
67131
describe("materializeCodexShadowHome", () => {
68132
it.effect.skipIf(!symlinksSupported)(
69133
"preserves SQLite files when shared sidecars appear after the shadow home starts",

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ export const resolveCodexHomeLayout = Effect.fn("resolveCodexHomeLayout")(functi
6868
};
6969
});
7070

71-
export const codexHomeEnvironment = (layout: CodexHomeLayout, environment: NodeJS.ProcessEnv) =>
72-
layout.mode === "authOverlay"
73-
? {
74-
...environment,
75-
CODEX_SQLITE_HOME: environment.CODEX_SQLITE_HOME?.trim() || layout.sharedHomePath,
76-
}
77-
: environment;
78-
7971
const CodexShadowHomeContext = {
8072
sharedHomePath: Schema.String,
8173
effectiveHomePath: Schema.String,
@@ -142,6 +134,40 @@ export const CodexShadowHomeError = Schema.Union([
142134
]);
143135
export type CodexShadowHomeError = typeof CodexShadowHomeError.Type;
144136

137+
export const codexHomeEnvironment = Effect.fn("codexHomeEnvironment")(function* (
138+
layout: CodexHomeLayout,
139+
environment: NodeJS.ProcessEnv,
140+
) {
141+
const effectiveHomePath = layout.effectiveHomePath;
142+
if (layout.mode !== "authOverlay" || !effectiveHomePath) return environment;
143+
const override = environment.CODEX_SQLITE_HOME?.trim();
144+
if (override) return { ...environment, CODEX_SQLITE_HOME: override };
145+
146+
const fileSystem = yield* FileSystem.FileSystem;
147+
const entries = yield* fileSystem.readDirectory(effectiveHomePath).pipe(
148+
Effect.catchTags({
149+
PlatformError: (cause) =>
150+
cause.reason._tag === "NotFound"
151+
? Effect.succeed([])
152+
: new CodexShadowHomeFileSystemError({
153+
sharedHomePath: layout.sharedHomePath,
154+
effectiveHomePath,
155+
operation: "readDirectory",
156+
path: effectiveHomePath,
157+
cause,
158+
}),
159+
}),
160+
);
161+
// Keep existing databases on their original path so SQLite can recover WAL in place,
162+
// including when another Codex process still owns a connection there.
163+
return {
164+
...environment,
165+
CODEX_SQLITE_HOME: entries.some((entry) => SQLITE_ENTRY_NAME.test(entry))
166+
? effectiveHomePath
167+
: layout.sharedHomePath,
168+
};
169+
});
170+
145171
type LinkState =
146172
| {
147173
readonly _tag: "Missing";

0 commit comments

Comments
 (0)