Skip to content

Commit 1b9ba76

Browse files
committed
fix(windows): stop service loop on missing install
1 parent 9830ab1 commit 1b9ba76

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ Run opencodex as a login-managed background service (macOS **launchd**, Linux **
204204
Windows **Task Scheduler**) that auto-starts on login and auto-restarts on crash. Service runs set
205205
`OCX_SERVICE=1` so a restart does not churn the Codex config.
206206

207+
The Windows wrapper verifies its baked Bun runtime and CLI entry before every start attempt. If an
208+
interrupted package update removed either file, it logs one `installation is incomplete` message and
209+
stops instead of retrying the same missing executable every five seconds. Reinstall opencodex, then
210+
run `ocx service repair` to refresh the task with the restored package paths.
211+
207212
| Subcommand | Action |
208213
| --- | --- |
209214
| none | Create/update and start the service. |

‎src/service.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,14 @@ export function buildWindowsServiceScript(entry = cliEntry(), port = resolveServ
15451545
'>>"%OCX_SERVICE_LOG%" echo opencodex_home="%OPENCODEX_HOME%"',
15461546
'>>"%OCX_SERVICE_LOG%" echo codex_home="%CODEX_HOME%"',
15471547
'>>"%OCX_SERVICE_LOG%" echo token_file="%OCX_API_TOKEN_FILE%"',
1548+
'if not exist "%OCX_BUN%" (',
1549+
' >>"%OCX_SERVICE_LOG%" echo [%DATE% %TIME%] installation is incomplete: bundled Bun is missing; reinstall opencodex, then run ocx service repair',
1550+
" exit /b 3",
1551+
")",
1552+
'if not exist "%OCX_CLI%" (',
1553+
' >>"%OCX_SERVICE_LOG%" echo [%DATE% %TIME%] installation is incomplete: CLI entry is missing; reinstall opencodex, then run ocx service repair',
1554+
" exit /b 3",
1555+
")",
15481556
`"%OCX_BUN%" "%OCX_CLI%" start --port ${port} >>"%OCX_SERVICE_LOG%" 2>&1`,
15491557
"if %ERRORLEVEL% NEQ 0 (",
15501558
' >>"%OCX_SERVICE_LOG%" echo [%DATE% %TIME%] child exited with code %ERRORLEVEL%; restarting in 5s',

‎structure/06_docs-and-release.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ bun install --frozen-lockfile
3535
bun run build
3636
```
3737

38+
## Windows service wrapper and incomplete updates
39+
40+
[Decision Log]
41+
- 목적과 의도: Prevent a failed npm replacement from making the Task Scheduler wrapper retry missing package files forever.
42+
- 기존 구현 및 제약 조건: The wrapper deliberately restarts a proxy after runtime crashes, but an absent baked Bun or CLI path cannot recover inside that process. Current updater preflight and stop-first behavior reduce replacement risk but do not provide a transactional restore of npm's package tree and global launchers.
43+
- 검토한 주요 대안: Keep unconditional five-second retries, add a generic crash ceiling, restore npm directories in-place, or classify only proven missing executable paths as terminal.
44+
- 선택한 방식: Check the baked Bun and CLI paths before every spawn; log one actionable incomplete-install message and exit with code 3 when either is absent. Preserve the existing retry loop for a child that actually launched and then failed.
45+
- 다른 대안 대신 이 방식을 선택한 이유: A generic retry ceiling can stop a service after unrelated intermittent crashes, while copying a package directory without matching npm shims, ownership, and lock guarantees is not a safe rollback.
46+
- 장점, 단점 및 영향: File-less package skeletons no longer produce unbounded service logs or restart churn. The wrapper still recovers ordinary proxy crashes, but repairing an incomplete npm install remains an explicit reinstall plus `ocx service repair` operation until a verified staged-update design exists.
47+
3848
## GitHub workflow map
3949

4050
| Workflow | Trigger | Purpose |

‎tests/service.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,30 @@ describe("Windows service task", () => {
523523
expect(script).not.toContain("timeout /t");
524524
});
525525

526+
test("stops instead of restart-looping when an update removed the baked runtime or CLI (#1849)", () => {
527+
const script = buildWindowsServiceScript({
528+
bun: "C:\\OpenCodex\\bun.exe",
529+
bunRuntimeSource: "bundled",
530+
cli: "C:\\OpenCodex\\cli.ts",
531+
});
532+
const loopAt = script.indexOf(":loop");
533+
const bunCheckAt = script.indexOf('if not exist "%OCX_BUN%"');
534+
const cliCheckAt = script.indexOf('if not exist "%OCX_CLI%"');
535+
const launchAt = script.indexOf('"%OCX_BUN%" "%OCX_CLI%" start --port');
536+
const retryAt = script.indexOf("goto loop");
537+
538+
expect(loopAt).toBeGreaterThanOrEqual(0);
539+
expect(bunCheckAt).toBeGreaterThan(loopAt);
540+
expect(cliCheckAt).toBeGreaterThan(bunCheckAt);
541+
expect(launchAt).toBeGreaterThan(cliCheckAt);
542+
expect(retryAt).toBeGreaterThan(launchAt);
543+
expect(script).toContain("installation is incomplete: bundled Bun is missing");
544+
expect(script).toContain("installation is incomplete: CLI entry is missing");
545+
expect(script.match(/exit \/b 3/g)).toHaveLength(2);
546+
// `goto loop` re-enters both checks before another child spawn.
547+
expect(script.slice(loopAt, launchAt).match(/if not exist/g)).toHaveLength(2);
548+
});
549+
526550
test("rewrites profile-relative paths to env indirection so non-ASCII usernames survive OEM-codepage batch parsing", () => {
527551
const oldUserProfile = process.env.USERPROFILE;
528552
const oldAppData = process.env.APPDATA;

0 commit comments

Comments
 (0)