From c7acf62791af77a227524ab3183e27b40a168aa8 Mon Sep 17 00:00:00 2001 From: harjoth Date: Wed, 26 Aug 2026 20:58:52 -0700 Subject: [PATCH] fix(onboard): reap a stale Hermes forward watcher before starting its forward MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `scripts/install.sh` plants a managed forward watcher after an install-driven Hermes onboard. The watcher probes the sandbox's API port every 10 seconds and restarts the port forward when the probe fails. It reaps the previous watcher only inside its own post-onboard restore step, which runs after `run_onboard`, so a watcher from an earlier cycle stays live for the whole of the next onboard. It then races onboarding's own forward stop and start, and each side can tear down the other's still-settling ssh listener, which leaves the API port refused across retries. Onboarding now stops the watcher recorded for a sandbox and port before it starts that port's forward, and `nemoclaw destroy` stops every watcher recorded for the destroyed sandbox so one cannot outlive a name the next onboard reuses. The destroy-side reap runs as soon as the delete is confirmed succeeded or the sandbox was already gone — before any later, independently-retryable cleanup step (currently: the managed Hermes state-volume removal) can exit the command early and skip it. A failed delete itself still leaves the reap untouched: the sandbox is registered and still running, and the watcher a premature reap would stop is that live sandbox's own forward self-healing, not an orphan's. Both reuse the identity contract `nemoclaw uninstall` already applies: the process is signaled only when it runs as the current user and its command line is exactly `node .js `. That is stricter than the installer's own argv-substring check (below). A process that fails the check is left alone, and an inconclusive inspection never signals. The state directory backing this reap is always an explicit, caller-supplied path (defaulting to `resolveNemoclawStateDir()`, which redirects to an isolated directory under `npm test`) rather than resolved from `$HOME` inside the watcher-lookup code itself, so a caller that forgets to wire it up reads an empty directory instead of the invoking user's real `~/.nemoclaw/state`. When a watcher survives the reap and the forward still fails to open its listener, the existing failure warning now names the surviving pid and script path, so the next occurrence is diagnosable without git archaeology. Trade-off: a plain `nemoclaw onboard` on an already-healthy sandbox now stops that sandbox's own watcher before restarting its forward, and nothing replants it afterward — only the installer creates a watcher, and onboarding/destroy never do, including a `destroy` followed by a fresh `onboard`. This is deliberate; it's the same reap that closes the race. The sandbox keeps working, but its forward stops auto-recovering from a dropped listener until the next full install. This does not extend NemoClaw's forward-start retry budget. The reported "within 10000ms" window belongs to the OpenShell CLI, not NemoClaw, and #9290 showed that repeated manual retries do not clear the condition. It also does not weaken the host-side API forward verification added by #9299; onboarding still fails when the forward is unreachable. Confidence: the lifecycle gap is verified in the source and is worth closing on its own. Whether it is the whole cause of #10385 is not established. The mechanism described here needs a prior watcher to race against, so it cannot by itself explain a first-ever install's failure — `restore_onboard_forward_after_post_checks` only runs once, after `run_onboard` completes, so there is no earlier watcher on that path. The reported failure is specific to Ubuntu 24.04 with an NVIDIA GPU, and this change does not explain that platform specificity. Confirming either needs a run on the affected host. Pre-existing, unrelated hazards noted for the record, not fixed here (all in `scripts/install.sh`, all weaker than the identity contract this change reuses): - Its own post-onboard reap (~line 564) matches an old watcher by substring (`old_args == *"$expected_watcher_script"*`) rather than the exact command line the TypeScript side now requires. - That same reap unconditionally removes the pid file (~line 568) even when the identity check fails, regardless of whether the process it points at was actually this watcher. - Its retry loop signals the watcher it just spawned (~line 716) with no identity check at all before falling back to a fresh attempt. Known limitations disclosed, not addressed here (each is a design or scope decision beyond this bug fix, not a defect introduced by it): - The identity check and the signal it authorizes are not atomic: the PID could theoretically exit and be reused by an unrelated process in the interval between the check and `host.kill(pid)`. This is the same check-then-kill pattern `nemoclaw uninstall` already ships; closing it for real would mean binding identity to the signal itself (e.g. a Linux pidfd), which is a larger, cross-cutting change to that existing contract, not something this PR's watcher-lifecycle fix should take on unreviewed. - Nothing replants a watcher after a successful onboarding forward start, so a sandbox that recovers from this race loses its self-healing until the next full install (see the Trade-off paragraph above). Adding a replant is new behavior, not a bug fix, and needs its own design decision. - `HermesForwardWatcherState`, `HermesForwardWatcherCommandLine`, and `isManagedHermesForwardWatcherProcess` still live under `domain/uninstall/` even though onboarding and destroy are now consumers too. Moving them to a lifecycle-neutral owner is a clean, low-risk follow-up, but it's a pure organizational change with no behavior difference, so it's left for a separate PR rather than bundled into this fix. `src/lib/core/shell-quote.ts` fan-in moves from 28 to 29 and `src/lib/state/paths.ts` from 20 to 22 for the new consumers added here. `src/lib/state/state-root.ts`'s budget is lowered from the stale 22 to its current measured 21: this change no longer imports it directly (the watcher adapter takes its state directory as a parameter instead), so that edge is gone rather than added to. Refs #10385 Co-Authored-By: Claude Sonnet 5 Signed-off-by: harjoth --- ci/source-architecture-budget.json | 3 +- docs/reference/troubleshooting.mdx | 36 +++++ src/lib/actions/sandbox/destroy-flow.test.ts | 24 +++ .../destroy-hermes-forward-watcher.test.ts | 97 ++++++++++++ src/lib/actions/sandbox/destroy-preflight.ts | 31 ++++ src/lib/actions/sandbox/destroy.ts | 40 ++++- .../hermes-forward-watcher-cleanup.ts | 7 +- .../openshell/hermes-forward-watcher.ts | 122 ++++++++++++++- src/lib/onboard/dashboard.ts | 55 +++++++ src/lib/state/hermes-forward-watcher.ts | 7 +- test/helpers/destroy-flow-test-harness.ts | 5 + .../hermes-forward-watcher-test-fixture.ts | 90 +++++++++++ ...d-dashboard-hermes-forward-watcher.test.ts | 144 ++++++++++++++++++ 13 files changed, 651 insertions(+), 10 deletions(-) create mode 100644 src/lib/actions/sandbox/destroy-hermes-forward-watcher.test.ts create mode 100644 test/helpers/hermes-forward-watcher-test-fixture.ts create mode 100644 test/onboarding/onboard-dashboard-hermes-forward-watcher.test.ts diff --git a/ci/source-architecture-budget.json b/ci/source-architecture-budget.json index 4932787703a..b6f9cfb1b26 100644 --- a/ci/source-architecture-budget.json +++ b/ci/source-architecture-budget.json @@ -16,7 +16,7 @@ "src/lib/cli/terminal-style.ts": 43, "src/lib/core/json-types.ts": 37, "src/lib/core/ports.ts": 89, - "src/lib/core/shell-quote.ts": 28, + "src/lib/core/shell-quote.ts": 29, "src/lib/core/url-utils.ts": 30, "src/lib/core/wait.ts": 37, "src/lib/credentials/store.ts": 46, @@ -27,6 +27,7 @@ "src/lib/runner.ts": 86, "src/lib/security/redact.ts": 54, "src/lib/state/onboard-session.ts": 36, + "src/lib/state/paths.ts": 22, "src/lib/state/registry.ts": 101, "src/lib/state/state-root.ts": 21, "src/lib/subprocess-env.ts": 24, diff --git a/docs/reference/troubleshooting.mdx b/docs/reference/troubleshooting.mdx index 5c7d9b1652a..6ed85f26752 100644 --- a/docs/reference/troubleshooting.mdx +++ b/docs/reference/troubleshooting.mdx @@ -5313,6 +5313,42 @@ An HTTP status of `200` or `401` means the host port forward is reachable. Any other status, `000`, or connection failure requires fresh diagnostics. Rerun `$$nemoclaw onboard` and follow the reported `gateway` or `api` failure. +### Onboarding Reports That a Hermes Forward Watcher Is Still Running + +The installer starts a background forward watcher after it onboards a Hermes sandbox. +The watcher probes the sandbox's API port every 10 seconds and restarts the port forward when the probe fails. + +Onboarding stops the watcher recorded for a sandbox and port before it starts that port's forward. +Sandbox destroy stops every watcher recorded for a sandbox once its delete succeeds or it was already gone; a failed delete leaves the watcher running, because the sandbox is still live and needs its self-healing forward. +Both stop a process only after confirming that it runs as the current user and that its command line is exactly the watcher NemoClaw recorded. + +Running `$$nemoclaw onboard` directly on a sandbox that already has a watcher also stops that watcher. +Only the installer plants a replacement; onboarding and destroy never do, including a `destroy` followed by a fresh `onboard`. +The sandbox keeps working, but its forward stops auto-recovering from a dropped listener until you reinstall. + +When a watcher survives that check and the forward still fails to open its listener, onboarding names the surviving process: + +```text +! Port 8642 forward did not start: local forward listener did not open on 127.0.0.1:8642 within 10000ms + A managed Hermes forward watcher is still running for 'hermes' on port 8642: pid 60642 (/home/user/.nemoclaw/state/hermes-hermes-8642.forward.pid.js). Stop it before retrying. +``` + +A surviving watcher restarts the same forward while onboarding is establishing it, and each side can stop the other's listener. +Confirm the reported process before you stop it: + +```bash +ps -p -o user=,args= +``` + +That command reporting your own user and a command line that *contains* the watcher script path from the diagnostic is not enough — NemoClaw only ever names a watcher here after confirming its command line matches exactly (`node