Skip to content

Commit af11b4e

Browse files
IngwannuWibias
authored andcommitted
fix(service): align WSL Codex home ownership
1 parent bdd89e6 commit af11b4e

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎src/service.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { expandUserPath, getConfigDir, readPid, removePid, removeRuntimePort, ve
1414
import { loadConfig } from "./config";
1515
import { restoreNativeCodex, restoreNativeCodexAsync } from "./codex/inject";
1616
import { stripGrokConfig } from "./grok/inject";
17-
import { isWslRuntime } from "./codex/home";
17+
import { isWslRuntime, resolveCodexHomeDir, type CodexHomeDeps } from "./codex/home";
1818
import { BUN_RUNTIME_PATH_ENV, BUN_RUNTIME_SOURCE_ENV, durableBunRuntime } from "./lib/bun-runtime";
1919
import type { BunRuntimeSource } from "./lib/bun-runtime";
2020
import { isProcessAlive, stopProxy } from "./lib/process-control";
@@ -94,9 +94,11 @@ function serviceStatePaths(): string[] {
9494
return paths;
9595
}
9696

97-
function currentCodexHome(): string {
98-
const raw = process.env.CODEX_HOME?.trim();
99-
return raw ? resolve(expandUserPath(raw)) : join(homedir(), ".codex");
97+
function currentCodexHome(deps: CodexHomeDeps = {}): string {
98+
// Service ownership must identify the same home as the runtime. In WSL an
99+
// unset CODEX_HOME can resolve to the single Windows Desktop home rather than
100+
// Linux ~/.codex; recording the fallback here creates a false foreign owner.
101+
return resolveCodexHomeDir(deps);
100102
}
101103

102104
function currentOpenCodexHome(): string {
@@ -218,8 +220,8 @@ export function inspectServiceStateEvidence(
218220
}
219221

220222
/** The homes this process is actually using, for comparison against a claim. */
221-
export function currentServiceHomes(): { codexHome: string; opencodexHome: string } {
222-
return { codexHome: currentCodexHome(), opencodexHome: currentOpenCodexHome() };
223+
export function currentServiceHomes(deps: CodexHomeDeps = {}): { codexHome: string; opencodexHome: string } {
224+
return { codexHome: currentCodexHome(deps), opencodexHome: currentOpenCodexHome() };
223225
}
224226

225227
export function serviceHomeMatches(a: string, b: string): boolean {

‎structure/02_config-and-codex-home.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ $CODEX_HOME/.opencodex-native-main-profiles/
2020
Never assume macOS-only paths. Windows, service installs, and app-launched Codex can all depend on
2121
the resolved `CODEX_HOME`.
2222

23+
Service install-state ownership uses this same resolver. In WSL, an unset `CODEX_HOME` may resolve
24+
to the single discoverable Windows Desktop home; recording Linux `~/.codex` instead would make a
25+
later repair or uninstall look foreign even though the service and runtime were started from the
26+
same environment. An explicit `CODEX_HOME` remains authoritative, and existing foreign ownership
27+
records are never migrated implicitly.
28+
29+
[Decision Log]
30+
- 목적과 의도: Keep service ownership metadata aligned with the Codex home the proxy actually uses.
31+
- 기존 구현 및 제약 조건: The runtime performed narrow WSL Windows-home discovery, while service state used `CODEX_HOME || ~/.codex`.
32+
- 검토한 주요 대안: Bake `CODEX_HOME` into every service, migrate old state automatically, or reuse the runtime resolver.
33+
- 선택한 방식: Resolve service install and comparison state through the existing runtime Codex-home resolver.
34+
- 다른 대안 대신 이 방식을 선택한 이유: It preserves explicit overrides and the existing WSL ambiguity rules without rewriting user environment or foreign state.
35+
- 장점, 단점 및 영향: New installs and same-environment repairs agree with runtime targeting; genuinely foreign or ambiguous state remains fail-closed.
36+
2337
Native-main profile ownership is bound to the real `CODEX_HOME`, not to an OpenCodex instance.
2438
Its encrypted vault, transaction journal, recovery marker, and referenced quarantine files live in
2539
the owner-only `.opencodex-native-main-profiles` directory. The unchanged

‎tests/codex-home-wsl.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from "bun:test";
22
import { wslAutomountRoot, listWslWindowsCodexHomes } from "../src/codex/home";
33
import { isWindowsInteropDir } from "../src/codex/shim";
4+
import { currentServiceHomes } from "../src/service";
45

56
describe("wsl.conf automount root", () => {
67
test("defaults to /mnt when wsl.conf is absent or silent", () => {
@@ -43,4 +44,23 @@ describe("wsl.conf automount root", () => {
4344
expect(seen[0]).toBe("/win/c/Users");
4445
expect(homes).toEqual(["/win/c/Users/example/.codex"]);
4546
});
47+
48+
test("service ownership uses the same discovered Windows Codex home as the runtime", () => {
49+
const usersRoot = ["/mnt/c", "Users"].join("/");
50+
const windowsCodexHome = [usersRoot, "windows-user", ".codex"].join("/");
51+
const homes = currentServiceHomes({
52+
env: { WSL_DISTRO_NAME: "Ubuntu" },
53+
platform: "linux",
54+
homedir: () => "/home/example",
55+
usersRoot,
56+
existsSync: (path: string) => path === usersRoot
57+
|| path === `${windowsCodexHome}/config.toml`,
58+
readdirSync: () => ["windows-user"],
59+
statSync: (() => ({ isDirectory: () => true })) as never,
60+
realpathSync: (path: string) => path,
61+
});
62+
63+
expect(homes.codexHome).toBe(windowsCodexHome);
64+
expect(homes.codexHome).not.toBe("/home/example/.codex");
65+
});
4666
});

0 commit comments

Comments
 (0)