Skip to content

Commit 07dfb84

Browse files
committed
fix(service): keep an absolute POSIX sqlite home literal in POSIX service files
A generated systemd unit or launchd plist written from a Windows host carried `CODEX_SQLITE_HOME=D:\tmp\codex-sqlite-home` for an input of `/tmp/codex-sqlite-home`, while `CODEX_HOME` in the same file kept its POSIX value. One file, two variables holding the same kind of value, disagreeing with each other. `currentCodexSqliteHomeAbsolute` ends in `resolve()`, which is host-relative in both directions: on a Windows host it anchors `/tmp/x` to the current drive, and on a POSIX host it would turn `C:\data` into `<cwd>/C:\data`. Neither is a path the target can use. The Windows branch above it already made this argument and preserved an already-absolute drive/UNC path; the POSIX side had no counterpart. Absolute-for-the-target paths are now preserved on both sides. A relative value still resolves, which is why the `resolve()` is there at all — a service unit has no meaningful working directory. Fixes the two long-standing Windows failures in `tests/service.test.ts` (`systemd service unit > preserves custom Codex and OpenCodex homes` and the launchd equivalent), which the reporter had been carrying as tolerated-by-name for several releases: 119 pass / 2 fail -> 123 pass / 0 fail. The added invariant test was driven red against the unfixed resolver. Note it can only fail on a Windows host: on POSIX, `resolve()` of an absolute POSIX path is identity, so this class of bug is invisible there — which is why it survived CI for so long. Closes #1786
1 parent 7612e4c commit 07dfb84

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

‎src/service.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { execFileSync, execSync, spawnSync } from "node:child_process";
99
import { findLiveProxy, proxyIdentityAt, SERVICE_STOP_LIVENESS } from "./server/proxy-liveness";
1010
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
1111
import { homedir, tmpdir } from "node:os";
12-
import { dirname, join, resolve, win32 } from "node:path";
12+
import { dirname, join, posix, resolve, win32 } from "node:path";
1313
import { expandUserPath, getConfigDir, readPid, removePid, removeRuntimePort, verifyPidIdentity } from "./config";
1414
import { loadConfig } from "./config";
1515
import { restoreNativeCodex, restoreNativeCodexAsync } from "./codex/inject";
@@ -113,12 +113,19 @@ function currentCodexSqliteHomeAbsolute(target: "native" | "windows" = "native")
113113
const raw = process.env.CODEX_SQLITE_HOME?.trim();
114114
if (!raw) return undefined;
115115
const expanded = expandUserPath(raw);
116-
// Windows service artifacts can be rendered by cross-platform tests and
117-
// repair tooling. Preserve an already-absolute drive/UNC path instead of
118-
// anchoring it beneath the current POSIX worktree.
119-
return target === "windows" && win32.isAbsolute(expanded)
120-
? win32.normalize(expanded)
121-
: resolve(expanded);
116+
// Service artifacts can be rendered by cross-platform tests and repair tooling, so an
117+
// already-absolute path for the TARGET platform is preserved rather than re-anchored
118+
// against the writing host. `resolve()` is host-relative in both directions: on a POSIX
119+
// host it turns `C:\data` into `<cwd>/C:\data`, and on a Windows host it turns `/tmp/x`
120+
// into `D:\tmp\x` — neither is a path the target can use. A relative value still resolves,
121+
// because a service unit has no meaningful working directory.
122+
//
123+
// CODEX_HOME and OPENCODEX_HOME are carried through literally, so without this the same
124+
// generated file disagreed with itself about two variables holding the same kind of value.
125+
if (target === "windows") {
126+
return win32.isAbsolute(expanded) ? win32.normalize(expanded) : resolve(expanded);
127+
}
128+
return posix.isAbsolute(expanded) ? posix.normalize(expanded) : resolve(expanded);
122129
}
123130

124131
function currentOpenCodexHome(): string {

‎tests/service.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,45 @@ describe("launchd service plist", () => {
663663
else process.env.OPENCODEX_API_AUTH_TOKEN = oldApiAuthToken;
664664
}
665665
});
666+
667+
// A POSIX unit must carry the literal POSIX path no matter which host writes it. The two
668+
// cases above are where this actually bites: on a Windows host `resolve("/tmp/x")` anchors
669+
// to the current drive and the generated file said `D:\tmp\codex-sqlite-home`, while
670+
// CODEX_HOME beside it kept `/tmp/codex-home`. The same file disagreed with itself about two
671+
// variables holding the same kind of value. This states the rule directly so the intent
672+
// survives; on a POSIX host `resolve()` is identity here, so only Windows can catch it.
673+
test("carries an absolute POSIX sqlite home into POSIX units without host anchoring", () => {
674+
const inherited = process.env.CODEX_SQLITE_HOME;
675+
try {
676+
process.env.CODEX_SQLITE_HOME = "/var/lib/opencodex/codex-sqlite";
677+
678+
expect(buildPlist()).toContain(
679+
"<key>CODEX_SQLITE_HOME</key><string>/var/lib/opencodex/codex-sqlite</string>",
680+
);
681+
expect(buildUnit()).toContain(
682+
'Environment="CODEX_SQLITE_HOME=/var/lib/opencodex/codex-sqlite"',
683+
);
684+
} finally {
685+
if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME;
686+
else process.env.CODEX_SQLITE_HOME = inherited;
687+
}
688+
});
689+
690+
// The relative case is why the resolve() is there at all: a service unit has no meaningful
691+
// working directory, so a relative home must still be made absolute.
692+
test("still absolutizes a relative sqlite home", () => {
693+
const inherited = process.env.CODEX_SQLITE_HOME;
694+
try {
695+
process.env.CODEX_SQLITE_HOME = "relative-sqlite-home";
696+
const plist = buildPlist();
697+
698+
expect(plist).toContain("<key>CODEX_SQLITE_HOME</key>");
699+
expect(plist).not.toContain("<string>relative-sqlite-home</string>");
700+
} finally {
701+
if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME;
702+
else process.env.CODEX_SQLITE_HOME = inherited;
703+
}
704+
});
666705
});
667706

668707
describe("service lifecycle cleanup ordering", () => {

0 commit comments

Comments
 (0)