99 * src/cli/index.ts — only the published npm `bin` routes through here.)
1010 */
1111import { spawn , spawnSync } from "node:child_process" ;
12+ import { STOP_HISTORY_INCOMPLETE_EXIT_CODE } from "../src/update/stop-contract.mjs" ;
13+ import { probeProxyLiveness } from "../src/update/proxy-liveness-probe.mjs" ;
14+ import { decidePostStopUpdate } from "../src/update/stop-decision.mjs" ;
1215import { randomBytes } from "node:crypto" ;
1316import { createRequire } from "node:module" ;
1417import { existsSync , readFileSync , readdirSync } from "node:fs" ;
@@ -17,6 +20,7 @@ import { dirname, join, resolve } from "node:path";
1720import { fileURLToPath } from "node:url" ;
1821import { isRealBunBinary } from "../src/lib/bun-binary-validator.mjs" ;
1922import { npmInvocation } from "../src/update/npm-invocation.mjs" ;
23+ import { hasPendingTeardownIn } from "../src/config/pending-teardown-names.mjs" ;
2024import {
2125 npmCachePreflightFailureMessage ,
2226 runNpmCachePreflight ,
@@ -202,7 +206,12 @@ function runNpmSelfUpdate() {
202206 // Capture listen target before stop clears runtime-port.json (mirrors GUI/CLI update worker).
203207 // Do not treat a live runtime port of 10100 as "missing" — track whether the read succeeded.
204208 let bakePort = 10100 ;
209+ // The hostname travels with the port: a proxy bound to ::1 or a specific interface is
210+ // invisible to a probe that assumes 127.0.0.1, and "no answer" would then read as
211+ // "stopped" for exactly the proxy the probe exists to find.
212+ let bakeHostname = "127.0.0.1" ;
205213 let sawRuntimePort = false ;
214+ let sawRuntimeHostname = false ;
206215 try {
207216 const rt = JSON . parse ( readFileSync ( join ( configDir ( ) , "runtime-port.json" ) , "utf8" ) ) ;
208217 if ( Number . isFinite ( rt ?. port ) && rt . port > 0 && rt . port <= 65535 ) {
@@ -219,16 +228,29 @@ function runNpmSelfUpdate() {
219228 }
220229 if ( runtimeLive ) {
221230 bakePort = Math . trunc ( rt . port ) ;
231+ if ( typeof rt ?. hostname === "string" && rt . hostname . trim ( ) !== "" ) {
232+ bakeHostname = rt . hostname . trim ( ) ;
233+ sawRuntimeHostname = true ;
234+ }
222235 sawRuntimePort = true ;
223236 }
224237 }
225238 } catch { /* fall through to config */ }
226- if ( ! sawRuntimePort ) {
239+ // Port and hostname resolve INDEPENDENTLY: a legacy runtime record carries a port and no
240+ // hostname, and skipping config in that case probed 127.0.0.1 for a proxy bound to ::1.
241+ if ( ! sawRuntimePort || bakeHostname === "127.0.0.1" ) {
227242 try {
228243 const cfg = JSON . parse ( readFileSync ( join ( configDir ( ) , "config.json" ) , "utf8" ) ) ;
229- if ( Number . isFinite ( cfg ?. port ) && cfg . port > 0 && cfg . port <= 65535 ) bakePort = Math . trunc ( cfg . port ) ;
244+ if ( ! sawRuntimePort && Number . isFinite ( cfg ?. port ) && cfg . port > 0 && cfg . port <= 65535 ) {
245+ bakePort = Math . trunc ( cfg . port ) ;
246+ }
247+ if ( ! sawRuntimeHostname && typeof cfg ?. hostname === "string" && cfg . hostname . trim ( ) !== "" ) {
248+ bakeHostname = cfg . hostname . trim ( ) ;
249+ }
230250 } catch { /* keep default */ }
231251 }
252+ // Wildcard and bracketed-IPv6 normalization lives in probeProxyLiveness, so both lanes
253+ // get it from one place.
232254
233255 const launcher = fileURLToPath ( import . meta. url ) ;
234256
@@ -343,17 +365,47 @@ function runNpmSelfUpdate() {
343365 }
344366 }
345367
346- if ( serviceWasInstalled || hasRuntimeState ) {
368+ // An outstanding pending-teardown receipt is a fourth reason to run the stop. After a
369+ // parent crashed mid-deferral the service, pid and runtime records can all be absent
370+ // while the shared client config still points at a proxy that is gone; installing over
371+ // that silently skips the recovery the receipt was written to trigger (#3008). Presence
372+ // is the whole test here — the launcher cannot parse it, and `ocx stop` is what decides
373+ // whether the obligation is safe to finish.
374+ const hasPendingTeardown = hasPendingTeardownIn ( readdirSync , configDir ( ) ) ;
375+ if ( serviceWasInstalled || hasRuntimeState || hasPendingTeardown ) {
347376 console . log ( "⏹ Stopping the running proxy before updating..." ) ;
348377 const stopRes = spawnSync ( process . execPath , [ launcher , "stop" ] , { stdio : "inherit" , windowsHide : true } ) ;
349378 const stillHasRuntimeState =
350379 existsSync ( join ( configDir ( ) , "ocx.pid" ) ) || existsSync ( join ( configDir ( ) , "runtime-port.json" ) ) ;
351- if ( stopRes . status !== 0 || stillHasRuntimeState ) {
380+ // A history-only failure means teardown succeeded and a backup manifest is waiting for
381+ // review: the proxy is down and replacing package files is safe. Every other nonzero
382+ // status is a stop that did not finish, and a signal kill (status null) says nothing
383+ // about whether it did - both abort, because replacing files under a live server
384+ // leaves it running mixed old and new modules (#3008).
385+ // The same decision the Bun updater makes, from the same module (#3008). Absent PID and
386+ // runtime files are weak evidence, so the captured endpoint is asked; "unknown" aborts
387+ // because a silent listener is exactly the state where replacing files is dangerous.
388+ const decision = decidePostStopUpdate ( {
389+ status : stopRes . status ,
390+ hasRuntimeState : stillHasRuntimeState ,
391+ // Re-checked AFTER the stop: a quarantined receipt lets the stop itself succeed
392+ // (there is nothing left to stop), so a pre-stop check alone let the retry install
393+ // over a teardown that never ran.
394+ teardownOutstanding : hasPendingTeardownIn ( readdirSync , configDir ( ) ) ,
395+ liveness : probeProxyLiveness ( bakePort , bakeHostname ) ,
396+ } ) ;
397+ const historyOnlyStop = decision . reason === "history-only" ;
398+ if ( ! decision . proceed ) {
352399 if ( trayBeforeUpdate . restoreOnFailure ) runTrayLifecycle ( launcher , "start" ) ;
353- console . error ( "opencodex: could not stop the running proxy; aborting the update. Run 'ocx stop' and retry." ) ;
400+ if ( decision . reason === "teardown-outstanding" ) {
401+ console . error ( "opencodex: a shared teardown from an earlier stop is still outstanding and needs manual review; aborting the update." ) ;
402+ console . error ( "opencodex: confirm no proxy is running, run 'ocx restore', then remove the pending-teardown file in the opencodex home." ) ;
403+ } else console . error ( decision . reason === "proxy-unknown"
404+ ? `opencodex: could not confirm the proxy on ${ bakeHostname } :${ bakePort } is stopped; aborting the update. Run 'ocx stop' and retry.`
405+ : "opencodex: could not stop the running proxy; aborting the update. Run 'ocx stop' and retry." ) ;
354406 process . exit ( 1 ) ;
355407 }
356- if ( historyRestoreIncomplete ( ) ) {
408+ if ( historyOnlyStop || historyRestoreIncomplete ( ) ) {
357409 console . warn (
358410 "opencodex: WARNING — Codex resume-history metadata restore is incomplete (a backup manifest remains).\n" +
359411 " The DB may be busy or the manifest/target may need review; untracked routed history is intentionally unchanged.\n" +
0 commit comments