Repository navigation
fix(update): recover npm 12 self-updates #2383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -227,11 +227,90 @@ function runNpmSelfUpdate() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { /* keep default */ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const launcher = fileURLToPath(import.meta.url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function startProxyDirectly() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!existsSync(launcher)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error("opencodex: cannot restart the proxy because the launcher is missing; reinstall opencodex manually."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const env = { ...process.env }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete env.OCX_SERVICE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(`Attempting to restart the proxy on port ${bakePort}.`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const child = spawn(process.execPath, [launcher, "start", "--port", String(bakePort)], { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detached: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdio: "ignore", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| windowsHide: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| child.on("error", error => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(`opencodex: direct proxy restart failed: ${error.message}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| child.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function refreshBackgroundServiceOrStartDirect() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const prevBake = process.env.OCX_BAKE_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.OCX_BAKE_PORT = String(bakePort); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let svc = spawnSync(process.execPath, serviceRefreshArgs(), { stdio: "inherit", windowsHide: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `serviceWasInstalled` is inferred from service-state.json alone, which can be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // STALE — present while the registration is gone. Repair refuses that case by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // design, and its thrown Error is indistinguishable from any other failure at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // this layer (plain Error, inherited stdio, generic exit status). So ask for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // structured state instead of parsing the failure: install only when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // diagnostic says the service is genuinely absent. Installing after ANY repair | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // failure would resurrect the elevation prompt this change exists to avoid, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // could re-register a service the user just uninstalled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (svc.status !== 0 && readServiceInstalledFromStatus(launcher) === false) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("No registered service found — installing it instead."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svc = spawnSync(process.execPath, serviceInstallArgs(), { stdio: "inherit", windowsHide: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+256
to
+267
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Add a timeout to the Lines 256 and 267 call The failure mode is specific to the recovery path. 🐛 Proposed fix: bound both service lifecycle spawns- let svc = spawnSync(process.execPath, serviceRefreshArgs(), { stdio: "inherit", windowsHide: true });
+ let svc = spawnSync(process.execPath, serviceRefreshArgs(), {
+ stdio: "inherit",
+ timeout: 120_000,
+ windowsHide: true,
+ });- svc = spawnSync(process.execPath, serviceInstallArgs(), { stdio: "inherit", windowsHide: true });
+ svc = spawnSync(process.execPath, serviceInstallArgs(), {
+ stdio: "inherit",
+ timeout: 120_000,
+ windowsHide: true,
+ });Note that a timed-out 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let needDirectStart = svc.status !== 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!needDirectStart) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Exit 0 can still leave stale/missing assets that never bring the proxy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // back — match the GUI/CLI fallthrough so /healthz is not left dead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const st = spawnSync(process.execPath, [launcher, "status", "--json"], { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf8", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 20_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| windowsHide: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (st.status === 0 && typeof st.stdout === "string" && st.stdout.trim()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsed = JSON.parse(st.stdout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proxyUp = parsed?.proxy?.running === true || parsed?.proxy?.health?.ok === true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const viable = parsed?.startup?.serviceViable === true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!proxyUp && !viable) needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // status failed or empty — fail closed to direct start (match CLI). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (needDirectStart) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A repair needs no elevation, but it can still fail — or exit 0 while leaving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // a non-viable manager. Fall back to a direct detached proxy start so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // update never leaves the user without a running proxy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svc.status === 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? "opencodex: service refresh left a non-viable manager — starting the proxy directly instead." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : "opencodex: service refresh failed — starting the proxy directly instead.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn(" Run 'ocx service repair' to see why the background service could not restart."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| startProxyDirectly(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (prevBake === undefined) delete process.env.OCX_BAKE_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else process.env.OCX_BAKE_PORT = prevBake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
n3wr1ch marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Never replace package files under a live proxy — stop it first (full `ocx stop` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // semantics: graceful drain, service stop, native Codex restore). Gate on the service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and the runtime-port record too: a service-managed or orphaned proxy can be live | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // while ocx.pid is stale/missing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const launcher = fileURLToPath(import.meta.url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (trayBeforeUpdate.stopBeforeReplacement) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("⏹ Handing off the Windows tray before updating..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -249,6 +328,17 @@ function runNpmSelfUpdate() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasRuntimeState = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| existsSync(join(configDir(), "ocx.pid")) || existsSync(join(configDir(), "runtime-port.json")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function recoverStoppedRuntimeAfterFailure() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (serviceWasInstalled) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn("opencodex: update failed after stopping the proxy — restoring the previous background service."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| refreshBackgroundServiceOrStartDirect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (hasRuntimeState) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn("opencodex: update failed after stopping the proxy — restarting the previous version directly."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| startProxyDirectly(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (serviceWasInstalled || hasRuntimeState) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("⏹ Stopping the running proxy before updating..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stopRes = spawnSync(process.execPath, [launcher, "stop"], { stdio: "inherit", windowsHide: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -309,7 +399,8 @@ function runNpmSelfUpdate() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // it recreates the #1849 destruction path. Report and stop; the boot probe and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // recovery marker cover the swap-window states. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(`opencodex: transactional update failed unexpectedly (${error?.message ?? error}). ` + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "The live install was not knowingly modified; run 'ocx update' again or reinstall with npm install -g."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `The live install was not knowingly modified; run 'ocx update' again or reinstall with ` + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `npm install -g --allow-scripts=bun ${PKG}@${tag}.`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res = { status: 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (res.status === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -329,77 +420,15 @@ function runNpmSelfUpdate() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // launcher so the new files write the baked paths and the service restarts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (serviceWasInstalled) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Refreshing the background service with the updated files..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const prevBake = process.env.OCX_BAKE_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.env.OCX_BAKE_PORT = String(bakePort); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let svc = spawnSync(process.execPath, serviceRefreshArgs(), { stdio: "inherit", windowsHide: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `serviceWasInstalled` is inferred from service-state.json alone, which can be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // STALE — present while the registration is gone. Repair refuses that case by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // design, and its thrown Error is indistinguishable from any other failure at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // this layer (plain Error, inherited stdio, generic exit status). So ask for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // structured state instead of parsing the failure: install only when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // diagnostic says the service is genuinely absent. Installing after ANY repair | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // failure would resurrect the elevation prompt this change exists to avoid, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // could re-register a service the user just uninstalled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (svc.status !== 0 && readServiceInstalledFromStatus(launcher) === false) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("No registered service found — installing it instead."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svc = spawnSync(process.execPath, serviceInstallArgs(), { stdio: "inherit", windowsHide: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let needDirectStart = svc.status !== 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!needDirectStart) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Exit 0 can still leave stale/missing assets that never bring the proxy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // back — match the GUI/CLI fallthrough so /healthz is not left dead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const st = spawnSync(process.execPath, [launcher, "status", "--json"], { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf8", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 20_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| windowsHide: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (st.status === 0 && typeof st.stdout === "string" && st.stdout.trim()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsed = JSON.parse(st.stdout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proxyUp = parsed?.proxy?.running === true || parsed?.proxy?.health?.ok === true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const viable = parsed?.startup?.serviceViable === true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!proxyUp && !viable) needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // status failed or empty — fail closed to direct start (match CLI). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| needDirectStart = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (needDirectStart) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A repair needs no elevation, but it can still fail — or exit 0 while leaving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // a non-viable manager. Fall back to a direct detached proxy start so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // update never leaves the user without a running proxy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svc.status === 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? "opencodex: service refresh left a non-viable manager — starting the proxy directly instead." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : "opencodex: service refresh failed — starting the proxy directly instead.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.warn(" Run 'ocx service repair' to see why the background service could not restart."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const env = { ...process.env }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete env.OCX_SERVICE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const child = spawn(process.execPath, [launcher, "start", "--port", String(bakePort)], { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detached: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdio: "ignore", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| windowsHide: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| child.unref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(`Proxy starting on port ${bakePort}.`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (prevBake === undefined) delete process.env.OCX_BAKE_PORT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else process.env.OCX_BAKE_PORT = prevBake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| refreshBackgroundServiceOrStartDirect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log("Restart the proxy: ocx start"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (trayBeforeUpdate.restoreOnFailure) runTrayLifecycle(launcher, "start"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(`\nUpdate failed (npm exit ${res.status ?? "?"}). Try manually: npm install -g ${PKG}@${tag}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| recoverStoppedRuntimeAfterFailure(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(`\nUpdate failed (npm exit ${res.status ?? "?"}). Try manually: npm install -g --allow-scripts=bun ${PKG}@${tag}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
n3wr1ch marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,39 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { describe, expect, test } from "bun:test"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { readFileSync } from "node:fs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { join } from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createServer } from "node:net"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { tmpdir } from "node:os"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { dirname, join } from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { runNpmCachePreflight } from "../src/update/npm-cache-preflight.mjs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { killProxy } from "../src/lib/process-control"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const repoRoot = join(import.meta.dir, ".."); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| function freePort(): Promise<number> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const { promise, resolve, reject } = Promise.withResolvers<number>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const server = createServer(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| server.on("error", reject); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| server.listen(0, "127.0.0.1", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const address = server.address(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const port = typeof address === "object" && address ? address.port : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| server.close(() => port ? resolve(port) : reject(new Error("no free port"))); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| async function waitForProxy(port: number): Promise<boolean> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const deadline = Date.now() + 15_000; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (Date.now() < deadline) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await fetch(`http://127.0.0.1:${port}/healthz`, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| signal: AbortSignal.timeout(500), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (response.ok) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { /* detached proxy is still starting */ } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // The detached process exposes readiness only over HTTP; fake timers cannot advance it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| await Bun.sleep(100); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const updateSource = readFileSync(join(import.meta.dir, "..", "src", "update", "index.ts"), "utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const launcherSource = readFileSync(join(import.meta.dir, "..", "bin", "ocx.mjs"), "utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const serverSource = readFileSync(join(import.meta.dir, "..", "src", "server", "index.ts"), "utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -105,6 +136,97 @@ describe("update stops the running proxy before replacing files", () => { | |||||||||||||||||||||||||||||||||||||||||||||||||
| expect(updateSource).toContain("runtimeTrusted"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| test.skipIf(process.platform === "win32")( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "npm launcher restarts the stopped runtime after a staged update failure", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const root = mkdtempSync(join(tmpdir(), "ocx-update-recovery-")); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const packageRoot = join(root, "node_modules", "@bitkyc08", "opencodex"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const launcher = join(packageRoot, "bin", "ocx.mjs"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const opencodexHome = join(root, "opencodex-home"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const fakeBin = join(root, "fake-bin"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const fakeNpm = join(fakeBin, "npm"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const cache = join(root, "npm-cache"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const bundledBun = join(repoRoot, "node_modules", "bun"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const env = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ...process.env, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| HOME: root, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| USERPROFILE: root, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| OPENCODEX_HOME: opencodexHome, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| OCX_FAKE_NPM_CACHE: cache, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| PATH: `${fakeBin}:${process.env.PATH ?? ""}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| let recoveredPid: number | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const port = await freePort(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(existsSync(bundledBun)).toBe(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdirSync(dirname(launcher), { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdirSync(join(packageRoot, "node_modules"), { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdirSync(opencodexHome, { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdirSync(fakeBin, { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdirSync(cache, { recursive: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| copyFileSync(join(repoRoot, "bin", "ocx.mjs"), launcher); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| chmodSync(launcher, 0o755); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| symlinkSync(join(repoRoot, "src"), join(packageRoot, "src"), "dir"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| symlinkSync(bundledBun, join(packageRoot, "node_modules", "bun"), "dir"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(join(packageRoot, "package.json"), JSON.stringify({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "@bitkyc08/opencodex", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| version: "1.0.0", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| type: "module", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(join(opencodexHome, "config.json"), JSON.stringify({ port })); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(join(opencodexHome, "runtime-port.json"), JSON.stringify({ port, pid: 999_999_999 })); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(fakeNpm, `#!/bin/sh | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case "$1" in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| view) printf '2.0.0\\n' ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| config) printf '%s\\n' "$OCX_FAKE_NPM_CACHE" ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| install) exit 1 ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| *) exit 1 ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| esac | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| chmodSync(fakeNpm, 0o755); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = Bun.spawnSync(["node", launcher, "update"], { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cwd: root, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| env, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout: "pipe", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr: "pipe", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 30_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const output = result.stdout.toString() + result.stderr.toString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result.exitCode).toBe(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(output).toContain("Stopping the running proxy before updating"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(output).toContain("restarting the previous version directly"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(output).toContain(`Attempting to restart the proxy on port ${port}.`); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(await waitForProxy(port)).toBe(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const runtime = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8")); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(runtime.pid).toBeGreaterThan(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recoveredPid = runtime.pid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+203
to
+205
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Assert that the recovered pid differs from the seeded placeholder before using it for cleanup. Line 178 seeds Today the assertion is safe, because 💚 Proposed fix: pin the assertion to a freshly written pid const runtime = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8"));
expect(runtime.pid).toBeGreaterThan(0);
+ expect(runtime.pid).not.toBe(999_999_999);
+ expect(runtime.port).toBe(port);
recoveredPid = runtime.pid;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const stopped = existsSync(launcher) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ? Bun.spawnSync(["node", launcher, "stop"], { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cwd: root, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| env, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout: "ignore", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr: "ignore", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 30_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (stopped?.exitCode !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!recoveredPid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recoveredPid = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8")).pid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { /* the proxy never wrote runtime state */ } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Number.isSafeInteger(recoveredPid) && recoveredPid! > 0) killProxy(recoveredPid!); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rmSync(root, { recursive: true, force: true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+216
to
+224
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Wrap the
Line 222 calls it unguarded inside
Also note the fallback read at line 219 can recover the seeded placeholder 🧹 Proposed fix: isolate cleanup failures if (stopped?.exitCode !== 0) {
if (!recoveredPid) {
try {
recoveredPid = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8")).pid;
} catch { /* the proxy never wrote runtime state */ }
}
- if (Number.isSafeInteger(recoveredPid) && recoveredPid! > 0) killProxy(recoveredPid!);
+ if (Number.isSafeInteger(recoveredPid) && recoveredPid! > 0) {
+ try {
+ killProxy(recoveredPid!);
+ } catch (error) {
+ console.warn(`cleanup: could not kill recovered proxy ${recoveredPid}: ${error}`);
+ }
+ }
}
rmSync(root, { recursive: true, force: true });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
n3wr1ch marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| test("both update paths surface a skipped history restore after the stop", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // A codex-history-backup-*.json surviving `ocx stop` means the native-history restore | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // was skipped (locked state DB) — users must be told or their threads silently stay | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.