Skip to content

Commit 159d4f7

Browse files
committed
test(codex): cover quoted legacy shim chmod recovery
1 parent 9e90ddf commit 159d4f7

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎docs-site/src/content/docs/reference/cli/lifecycle.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,9 @@ launcher stays restored and the operation can be retried. Missing or unusable na
881881
package-manager repair; OpenCodex does not guess another installation or rewrap the manager's path.
882882
Migration also refuses when the recorded `codex-shim.json` is not a regular file owned by you or is
883883
group- or world-writable. Older releases wrote that file with the process umask, so a umask of `002`
884-
left it at mode `0664`. The refusal names the file and the `chmod 600` that clears it; OpenCodex does
885-
not change the permissions itself.
884+
left it at mode `0664`. The refusal names the file and the `chmod 600` that clears only the
885+
group- or world-writable refusal. Symlink and foreign-owner refusals remain; OpenCodex does not
886+
change the permissions itself.
886887

887888
Launcher installation alone does not prove that Codex requests will use OpenCodex. After a runnable
888889
install, the command checks the current Codex routing and reports a warning instead of a green result

‎structure/runtime.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ does not perform OAuth, and runtime credential resolution rereads the owned sour
143143
| `src/types.ts` | Shared config, parsed request, adapter, and event types. |
144144
| `src/reasoning-effort.ts` | Codex reasoning-level definitions (`low`/`medium`/`high`/`xhigh`), per-model effort mapping, and catalog effort sanitization. |
145145
| `src/cli/tray-proxy.ts` | Proxy restart coordination. Replacement re-observation captures one clock value for both its remaining budget and absolute end, so the observation window never extends the caller deadline. CLI proxy update restart: `src/cli/system-restart-client.ts` preserves a private newer-CLI candidate with its original runtime record and physical home snapshot. `src/cli/update-restart.ts` accepts only known versions and positively classified standalone POSIX targets: PID-1 parent, no installed/active manager, no ownership record, no client/sibling role. It holds the existing ownership mutation lease through stop and spawn and revalidates the captured target before stop. `src/cli/update-restart-transport.ts` proves identity and sends stop on one direct TCP connection, refuses reconnects and requires complete successful teardown acknowledgement. It never signals a PID or reclaims a port. Confirmed predecessor exit, runtime-record removal, port availability and definitive endpoint absence precede the one launch. The executable that launch spawns must pass the shared `REAL_BUN_MIN_BYTES` gate: an incomplete runtime refuses before any stop byte, and after a confirmed stop the transaction waits for it within the deadline, then rechecks deadline and physical home synchronously before spawning. `src/cli/update-restart-child.ts` strips inherited lease delegation, acquires its own lease before CLI preflight and checks the frozen physical homes, ownership revision, CLI version, endpoint and deadline before startup, each bind and publication. It keeps custody through rollback; its deadline ends an unfinished child. `src/cli/update-restart-home.ts` owns physical-home and unclaimed-state checks. Success requires that launched child PID, fresh attestation and the exact known version; all incomplete outcomes terminate without generic restart recovery. Supervised, Windows and uncertain cases refuse this update path. |
146-
| `src/codex/shim.ts` | Codex autostart facade: dispatches Unix private PATH overlays to `src/codex/shim-overlay.ts` and retains Windows in-place script wrapping. `src/codex/shim-migration.ts` owns explicit legacy Unix restoration; `src/codex/shim-diagnostics.ts` owns diagnosis/status, distinguishes runnable from active overlays and states the unhealthy verdict for a damaged in-place (Windows) wrapper. `src/codex/shim-templates.ts` uses `src/lib/self-launch-argv.ts` to invoke standalone executables with `ensure` directly in Unix, CMD and PowerShell wrappers, without a virtual source entrypoint; management subcommands bypass startup even after value-taking global flags. Discovery resolves fnm’s temporary `fnm_multishells` directory to the durable installation directory through `src/codex/shim-path-resolution.ts`, taking the directory realpath while retaining the command basename; unresolved temporary entries refuse installation. The injected path flavour governs join and separator. The shared PATH scanner follows npm/fnm symlinks and POSIX target execute access, skips non-executable and special-file entries, and reads only the first 16 KiB of regular files through a descriptor with nonblocking POSIX open and a post-open type check. An unreadable executable still shadows later commands. Overlay activation requires the selected executable to identify the recorded private wrapper or the same file through an alias; another marker-bearing shim does not prove activation. Shell functions/aliases and empty PATH components are outside this scanner’s guarantee. `src/cli/codex-shim-readiness.ts` retains connect’s advisory exit policy. `tests/codex-integration/codex-shim-path-readiness.test.ts` covers POSIX shell lookup parity, FIFO replacement, symlinks and bounded reads. Windows retains PATHEXT lookup without POSIX execute-access checks. |
146+
| `src/codex/shim.ts` | Codex autostart facade: dispatches Unix private PATH overlays to `src/codex/shim-overlay.ts` and retains Windows in-place script wrapping. `src/codex/shim-migration.ts` owns explicit legacy Unix restoration. Legacy Unix shim migration rejects symlink, foreign-owned, and group/world-writable state files. The writable-mode refusal prints a shell-quoted `chmod 600` hint; it clears only that mode refusal, leaving file-type and ownership checks intact. `tests/codex-integration/codex-shim-migration.test.ts` executes the hint through `/bin/sh` with a space and apostrophe in the state path, then verifies mode `0600` and schema-2 migration. `src/codex/shim-diagnostics.ts` owns diagnosis/status, distinguishes runnable from active overlays and states the unhealthy verdict for a damaged in-place (Windows) wrapper. `src/codex/shim-templates.ts` uses `src/lib/self-launch-argv.ts` to invoke standalone executables with `ensure` directly in Unix, CMD and PowerShell wrappers, without a virtual source entrypoint; management subcommands bypass startup even after value-taking global flags. Discovery resolves fnm’s temporary `fnm_multishells` directory to the durable installation directory through `src/codex/shim-path-resolution.ts`, taking the directory realpath while retaining the command basename; unresolved temporary entries refuse installation. The injected path flavour governs join and separator. The shared PATH scanner follows npm/fnm symlinks and POSIX target execute access, skips non-executable and special-file entries, and reads only the first 16 KiB of regular files through a descriptor with nonblocking POSIX open and a post-open type check. An unreadable executable still shadows later commands. Overlay activation requires the selected executable to identify the recorded private wrapper or the same file through an alias; another marker-bearing shim does not prove activation. Shell functions/aliases and empty PATH components are outside this scanner’s guarantee. `src/cli/codex-shim-readiness.ts` retains connect’s advisory exit policy. `tests/codex-integration/codex-shim-path-readiness.test.ts` covers POSIX shell lookup parity, FIFO replacement, symlinks and bounded reads. Windows retains PATHEXT lookup without POSIX execute-access checks. |
147147
| `src/service.ts` | OS service manager (macOS launchd, Linux systemd, Windows schtasks and native WinSW): always-on proxy with crash restart. The scheduler wrapper logs transactional-backup recovery without interpolating backup names. Facade over the `src/service/` leaves — `src/service/launchd.ts`, `src/service/systemd.ts`, `src/service/windows-ops.ts`, `src/service/windows-scheduler.ts`, `src/service/windows-taskxml.ts`, `src/service/state.ts`, `src/service/guards.ts`, `src/service/health.ts`, `src/service/repair.ts`, `src/service/orchestration.ts`, `src/service/diagnostics.ts`, `src/service/cli.ts`. Service definitions filter shell-local multishell PATH entries, including WinSW XML generated during install and repair, and recorded temporary launchers are replaced during repair; changed launchd definitions are reloaded. Codex-home ownership accepts either the recorded path or the same existing physical directory so path aliases remain compatible across upgrades. Elevated Task Scheduler repair stages bounded payloads; the unelevated launcher pins every namespace ancestor and payload with non-reparse handles that deny write/delete sharing on the payload and delete sharing on each ancestor until UAC processing exits. On Windows, `src/service/windows-process-priority.ts` raises the process that binds the proxy to ABOVE_NORMAL just before `startServer` (best-effort; `OCX_DISABLE_PRIORITY_BOOST=1` opts out), because the NORMAL task priority (`<Priority>4</Priority>`) still let a saturated host hold `/healthz` past the 750 ms CLI liveness ceiling; spawned children fall back to NORMAL since Windows does not inherit ABOVE_NORMAL. The Task Scheduler batch wrapper (`src/service/windows-taskxml.ts`) checks bundled Bun against `REAL_BUN_MIN_BYTES` before each launch: npm's placeholder, or a file that vanished before its size was read, is logged and re-checked every five seconds instead of executed, while a Bun path missing at the exist check keeps the backup-restore then exit-3 path ([wrapper contract](ops/docs-and-release.md#windows-service-wrapper-and-incomplete-updates)). |
148148

149149
`src/cli/provider.ts` accepts the Google-only `--google-tool-schema-policy` creation flag and rejects

‎tests/codex-integration/codex-shim-migration.test.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { join } from "node:path";
55
import { autoRestoreCodexShim, buildUnixCodexShim, codexShimStatus, diagnoseCodexShim, installCodexShim, setCodexShimProbeHookForTests, setCodexShimProbeObservationMsForTests, uninstallCodexShim } from "../../src/codex/shim";
66
import { readState, type ShimState } from "../../src/codex/shim-state-file";
77

8-
function fixture(run: (f: { root: string; home: string; native: string; backup: string; quarantine: string; wrapper: string; statePath: string; state: ShimState; legacyBytes: string; version: (name: string) => string }) => void): void {
8+
function fixture(run: (f: { root: string; home: string; native: string; backup: string; quarantine: string; wrapper: string; statePath: string; state: ShimState; legacyBytes: string; version: (name: string) => string }) => void, homeName = "home"): void {
99
const root = fs.mkdtempSync(join(tmpdir(), "ocx-overlay-migration-"));
10-
const home = join(root, "home");
10+
const home = join(root, homeName);
1111
const bin = join(root, "manager");
1212
fs.mkdirSync(home, { mode: 0o700 });
1313
fs.mkdirSync(bin);
@@ -96,6 +96,25 @@ describe.skipIf(process.platform === "win32")("legacy Unix shim migration", () =
9696
expect(readState()).toMatchObject({ schema: 2, launcherPath: f.native });
9797
}));
9898

99+
test("printed chmod hint handles spaces and apostrophes and permits legacy migration", () => fixture(f => {
100+
fs.chmodSync(f.statePath, 0o664);
101+
const raw = fs.readFileSync(f.statePath, "utf8");
102+
const refused = installCodexShim();
103+
expect(refused).toMatchObject({ installed: false, refused: true });
104+
expect(refused.message).toContain(`run chmod 600 '${f.root}/owner'\\''s home/codex-shim.json' and retry`);
105+
expect(fs.readFileSync(f.statePath, "utf8")).toBe(raw);
106+
expect(fs.lstatSync(f.statePath).mode & 0o777).toBe(0o664);
107+
const hint = refused.message.match(/run (chmod 600 .+) and retry/)?.[1];
108+
expect(hint).toBeDefined();
109+
const result = Bun.spawnSync(["/bin/sh", "-c", hint!]);
110+
expect(result.exitCode, result.stderr.toString()).toBe(0);
111+
expect(fs.lstatSync(f.statePath).mode & 0o777).toBe(0o600);
112+
const installed = installCodexShim();
113+
expect(installed.installed, installed.message).toBe(true);
114+
expect(readState()).toMatchObject({ schema: 2, launcherPath: f.native });
115+
expect(fs.lstatSync(f.statePath).mode & 0o777).toBe(0o600);
116+
}, "owner's home"));
117+
99118
test("regular backup restoration retains its inode", () => fixture(f => {
100119
fs.unlinkSync(f.backup);
101120
fs.writeFileSync(f.backup, "#!/bin/sh\nexit 0\n", { mode: 0o755 });

0 commit comments

Comments
 (0)