Skip to content

fix(macos): fence Gateway recovery installs with the observed runtime pin - #165044

Merged
steipete merged 1 commit into
mainfrom
steipete/mac-recovery-expected-runtime-pin
Oct 4, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/mac-recovery-expected-runtime-pin

Conversation

@steipete

@steipete steipete commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes: the macOS app's Gateway recovery can overwrite an operator's runtime pin or service change when it lands between the app's final custody check and the recovery installer's own snapshot. Affected paths are the same-version Node rollback after a failed Bun migration, and restoring a hosting change onto the service's previous bundled build.

User Impact

Every app-owned recovery install is now fenced the way #164616 fenced forward installs. If an operator repins or changes the service first, recovery stops, preserves the newer selection, and reports it without retrying or starting anything. Recovery still restores exactly what it restored before: the retained npm Node package with its own Node, or the previous build's Bun, entrypoint and SQLite library. It also keeps working when the failed replacement service is not running.

Why This Change Was Made

#164616 left these two recovery paths unfenced (its "Residual Recovery Window" section). Both spawn the retained, older CLI, and a published CLI cannot accept --expected-runtime-pin. The app also cannot just run its bundled CLI instead. The installer builds the service command from its own process:

  • process.argv[1] selects the entrypoint (src/daemon/program-args.ts:169), the openclaw shim PATH dirs, and dev-mode detection.
  • A Bun service's OPENCLAW_SQLITE_LIBRARY comes from the installer's own environment.

Substituting the bundled CLI therefore installs the app's own package instead of the retained one. A live run during this work did exactly that: an unfenced bundled install moved the service onto the installer's Node and package.

Core: a hidden gateway install --restore-service-cli '{"executable","entrypoint","sqliteLibrary"}' option tells the installer to plan the service as that retained CLI would have. All argv- and execPath-derived inputs use the retained values, and the SQLite library is set only in the service-target environment, never the installer's. It is accepted only together with --expected-runtime-pin and an explicit --runtime. It refuses wrappers, update-owned reconciliation, a --runtime-path that names a different executable, and anything that is not an OpenClaw entrypoint. Pinning stays with --runtime-path: Node rollback stays unpinned, and prior-build restore keeps its Bun pin. A restore drops requireRunning, because recovery replaces the app's own failed service and a crash-looping replacement often isn't "running". The definition and pin-revision binding still fences operator changes.

App: one owner, runDaemonCommand(restoring:). Recovery runs the fence-capable bundled installer with the retained service environment, keeps the retained CLI's path and authority checks before spawn, and sends the restore option plus the CLI-observed expectation. Swift never computes the revision. Node rollback uses this app build's validated bundled runtime (BundledRuntime.resolve(bundle:)), never seeding, because seeding can replace the legacy CLI wrapper. Prior-build restoration uses the hosting change's prepared current build. The retained CLI is still used, but only for read-only --version probes. A pin refusal during Node migration now clears activation intent, matching the hosting-change refusal, so nothing reattaches or retries.

There is deliberately no unfenced fallback. If the app's bundled runtime is missing or incompatible, recovery stops with "reinstall OpenClaw.app". That only matters when Resume restores an absent legacy Node service; a present original service is only verified, not reinstalled. The core updater step (step 1 of the migration) still runs the installed CLI and remains out of scope, as documented.

Release-note context: macOS app Gateway recovery (Node rollback after a failed Bun switch, and restoring the previous hosting mode) now preserves an operator's concurrent runtime or service change instead of overwriting it.

Update behavior

Capability comes from choosing the bundled installer, not from a version check. Retained or published CLIs never receive the new option. They now run only read-only --version and absence probes during recovery; the core updater step is unchanged.

Measured live: a runtime-pin write by a newer CLI forward-migrates the state database. Status reads and no-op pin assertions do not. Recovery pin writes now use the bundled writer, but in both real flows the same writer already ran earlier in the same operation:

  • Same-version Node rollback: clears the failed Bun pin. The forward Bun install committed that pin with the bundled CLI, and the retained Node package is the app's exact version.
  • Prior-build restoration: commits the previous build's Bun pin. The hosting change's own uninstall already ran through the current bundled CLI and committed the pin deletion.

So recovery adds no new schema transition. A prior build that cannot open a newer schema was already unrecoverable by the old path too, because the retained CLI refuses that database.

Published-driver × candidate: published [email protected] restarts, health-checks and reports status for a service restored by this branch's installer (table below).

Evidence

Live launchd, real CLIs (macOS, throwaway --profile, own LaunchAgent label, random port). The published 2026.9.8 npm package plays the retained CLI; this branch's CLI plays the bundled installer. All jobs, plists, state, and logs were removed afterwards.

Cell Result
Published CLI self-installs a Node service; candidate restores it with --restore-service-cli (Cellar-realpath Node, as the app sends it) + observed pin installed; ProgramArguments, WorkingDirectory and generated env byte-identical to the published self-install (Cellar path stabilized to /opt/homebrew/opt/node@24/bin/node)
Operator repins with the published CLI after the observation; candidate recovery dispatches with the stale expectation Refused with "Gateway service or runtime pin changed before installation. The newer selection was preserved…"; plist unchanged; pin still stored
Same race on the original path: retained CLI's unfenced install --force --runtime node installed; operator pin silently removed (the defect)
Failed service stopped and unloaded (loaded=false); candidate fenced restore installed; published Gateway running, RPC OK
Published CLI drives the candidate-restored service gateway restart restarted; health ok
Bun prior-build shape: --runtime bun --runtime-path <bun> --restore-service-cli {bun, openclaw.mjs, sqlite} Pinned; [bun, --no-install, <retained>/dist/index.js, gateway, --port …]; generated env OPENCLAW_SQLITE_LIBRARY = retained library

Replay at the committed head d015175d606b repeated the parity, race, stopped-service and Bun cells with the same results. The full generated env file, token included, matched byte for byte.

Harness caveats:

  • main is at state schema 20 while still reporting version 2026.9.8. When a candidate pin write migrated a throwaway state database, the published 2026.9.8 Gateway correctly refused to start on it. That refusal is how the pin-write rule above was found and isolated.
  • A published health call without the profile's port in config targets the profile default port; RPC status on the service port is the liveness check used here.

Regressions that fail on the original defect

  • Swift: operator repin before Node rollback dispatch is preserved, not overwritten and operator repin before prior-build restoration dispatch is preserved, not overwritten. The fake installer models core: the operator's pin lands as the CLI spawns; an install without --expected-runtime-pin overwrites it, and one with it is refused. Against the original production Swift both fail, with the pin deleted and health verification proceeding. With the fix both pass: the pin survives, health is not checked, and the exact restore and expectation arguments go through the bundled installer prefix.
  • Core: 17 restore cases in install.test.ts, covering refusal without a fence, a malformed spec, missing --runtime, a wrapper, a mismatched --runtime-path, update-owned reconciliation, a stale expectation, Node unpinned with no requireRunning, Bun pinned, and the SQLite library override or removal. Swapping in the original install.ts gives 16 failures. A real-builder test in daemon-install-path.test.ts proves the planned command and shim PATH dirs come from the retained package, never the installer's.

Tests and gates

Command (--maxWorkers=1) Result Wall time
pnpm test src/cli/daemon-cli/install.test.ts 47 passed 21 s
pnpm test src/commands/daemon-install-path.test.ts 13 passed 8 s
pnpm test src/cli/daemon-cli/register-service-commands.test.ts 48 passed 8 s
pnpm test src/commands/daemon-install-helpers.test.ts (moved block) 87 passed 9 s
pnpm test src/daemon/program-args.test.ts 36 passed 8 s
pnpm test src/cli/daemon-cli/install-environment.test.ts 7 passed 8 s
pnpm test src/commands/node-daemon-install-helpers.test.ts 5 passed 2 s
Swift filter (private HOME/state/config/TMPDIR, native build) Tests Wall time
ManagedNodeGatewayMigrationTests 29 5.5 s
GatewayLaunchAgentManagerTests 21 4.6 s
GatewayHostingChangeTests 7 4.9 s
GatewayHostingTests 4 4.8 s
GatewayProcessManagerTests hosting/restoration selection 10 4.4 s

No new timers, sleeps, process boots, or worker pins. CI seconds will be added once the run exists. The Swift filters ran in an isolated checkout verified byte-identical to this branch, because this worktree lacks generated Mermaid resources for the native build.

Other gates:

  • node scripts/check-changed.mjs: exit 0 across the core, coreTests, apps, docs, and tooling lanes, including core and core-test typecheck, the line-cap and max-lines ratchets, and format.
  • scripts/lint-swift.sh macos, scripts/format-swift.sh macos, pnpm native:i18n:verify, git diff --check: all pass.

Size. Production is +173 lines: core +123, of which about +8 is the pure-move extraction's imports and exports, and Swift +50. Tests are +519. The growth is a new installer capability plus its single app owner. A round of compressing unrelated secret and host-env service code to reach LOC-neutral was deliberately reverted: that code is security-relevant, and implicit ...params spreads into the secret collectors would be harder to audit than the lines they save. The extraction is the sanctioned fix for the line-cap ratchet; it lets daemon-install-helpers.ts drop its grandfathered max-lines suppression and baseline entry.

Codex review of the final tree against merge base aad828fe5bb: scoped-clean, no actionable P0–P2 findings ("patch is correct").

… pin

Same-version Node rollback and prior-build hosting restoration still spawned
the retained CLI, which cannot accept --expected-runtime-pin, so an operator
repin between the app's final custody check and that CLI's snapshot was
overwritten. The bundled CLI could not be substituted because the installer
derives the service entrypoint, shim PATH, dev mode and Bun SQLite library
from its own process.

Core adds a hidden `gateway install --restore-service-cli` option that plans
the service for an explicitly supplied retained executable, entrypoint and
SQLite library. It requires --expected-runtime-pin and an explicit --runtime,
keeps pinning owned by --runtime-path, and does not require the failed
replacement service to be running. The app runs both recovery paths through
its bundled installer with the CLI-observed expectation; Swift never derives
the revision. A pin refusal during Node migration now clears activation
intent like the hosting-change refusal.

Move the auth-profile service-env collectors into a sibling module so
daemon-install-helpers.ts drops below the max-lines cap; its suppression and
baseline entry are removed.
@clawsweeper

clawsweeper Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: macos App: macos gateway Gateway runtime cli CLI command changes commands Command implementations size: L labels Oct 4, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 4, 2026
@clawsweeper

clawsweeper Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 4, 2026, 2:34 PM ET / 18:34 UTC.

ClawSweeper review

What this changes

The macOS app uses its bundled installer to restore retained Gateway runtimes while preserving concurrent operator service or runtime changes.

Merge readiness

✅ Ready for maintainer review

This remains necessary: current main leaves both recovery installs unfenced, and the merged predecessor explicitly reserved this follow-up. No blocking introduced defect was found.

Likely related people: steipete (high confidence, recent merged work on these recovery owners).

Priority: P2
Reviewed head: d015175d606b042458759bac33a604992d271c7f

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with relevant real launchd observations, regression coverage, and no blocking introduced finding.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.
Evidence reviewed 9 items Pinned patch and merge ownership: The reviewed introduction is aad828f..d015175. The test merge records the exact fetched-main parent followed by the exact PR head, and its result retains the same introduced surface.
Current main still needs the recovery fix: Current main's Node restoration invokes the retained CLI directly. Prior-build restoration likewise passes installedCLI without selecting the fence-capable bundled installer.
Merged predecessor explicitly excludes recovery: #164616 is verified merged. Its supplied Residual Recovery Window section expressly excludes same-version Node rollback and prior-build restoration, so it does not supersede this PR.
Findings None None.
Security None None.

How this fits together

The macOS app manages Gateway hosting and recovery through the OpenClaw CLI. The installer translates the retained runtime, package, environment, and observed operator selection into a launchd service.

flowchart TD
  A[Gateway recovery request] --> B[Retained runtime and package]
  B --> C[Bundled installer]
  D[Observed operator selection] --> C
  C --> E[Check current service and pin]
  E --> F[Restore launchd service]
  E --> G[Preserve newer selection and report refusal]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +123, Tests +273, Docs +1, Other +295. Total +692 across 22 files.

View PR surface stats
Area Files Added Removed Net
Source 8 200 77 +123
Tests 3 309 36 +273
Docs 1 10 9 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 10 329 34 +295
Total 22 848 156 +692

Review metrics

Metric Value Why it matters
Recovery installer option 1 hidden CLI option added The retained-service specification requires an observed pin fence and explicit runtime; published retained CLIs never receive it.
Production and regression growth production +173 lines, tests +519 lines The PR explains growth as the retained-service installer capability and shared native recovery owner, with regressions for the original overwrite.

Technical review

Best possible solution:

Keep recovery under the existing installer and native service owners, preserving the retained package while refusing stale operator selections.

Do we have a high-confidence way to reproduce the issue?

Yes: current-main recovery dispatches through retained CLIs without the observed pin expectation, leaving the reported pre-snapshot overwrite window. This review inspected source; the PR separately records a real launchd reproduction and after-fix replay.

Is this the best way to solve the issue?

Yes: selecting the retained service through the existing fence-capable installer preserves command and environment ownership without sending unsupported flags to published CLIs.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 0816aa1b57dc.

Labels

Label changes:

  • add P2: This fixes a bounded macOS recovery race that can overwrite a concurrent operator runtime selection.
  • add merge-risk: 🚨 compatibility: Absent legacy-service recovery now requires a healthy bundled installer; the verified repository admin explicitly accepted stopping rather than using an unfenced fallback.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.

Label justifications:

  • P2: This fixes a bounded macOS recovery race that can overwrite a concurrent operator runtime selection.
  • merge-risk: 🚨 compatibility: Absent legacy-service recovery now requires a healthy bundled installer; the verified repository admin explicitly accepted stopping rather than using an unfenced fallback.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured head-specific macOS launchd observations exercise the real restore installer with retained Node and Bun targets, demonstrate stale operator-pin refusal before service replacement, and show stopped-service recovery plus published-driver restart compatibility. Existing pin formats remain unchanged, and the body explains and exercises compatibility with already-established writer transitions.

Evidence

What I checked:

  • Pinned patch and merge ownership: The reviewed introduction is aad828f..d015175. The test merge records the exact fetched-main parent followed by the exact PR head, and its result retains the same introduced surface. (101c003eaf47)
  • Current main still needs the recovery fix: Current main's Node restoration invokes the retained CLI directly. Prior-build restoration likewise passes installedCLI without selecting the fence-capable bundled installer. (apps/macos/Sources/OpenClaw/ManagedNodeGatewayMigration.swift:545, 0816aa1b57dc)
  • Merged predecessor explicitly excludes recovery: fix(macos): fence app-owned service installs with the observed runtime pin #164616 is verified merged. Its supplied Residual Recovery Window section expressly excludes same-version Node rollback and prior-build restoration, so it does not supersede this PR. (3560bc0daf9b)
  • Single recovery invocation owner: Recovery selects the bundled installer, carries the retained environment and explicit restore specification, observes runtime intent through that same invocation, and retains final service and retained-path custody checks before spawning. (apps/macos/Sources/OpenClaw/GatewayLaunchAgentManager.swift:684, d015175d606b)
  • Restore admission and final mutation guards: The restore parser requires an expectation and explicit runtime, rejects wrappers and update-owned reconciliation, and validates retained files and runtime. Installation retains definition matching; the existing service owner rechecks pin revision and definition under native mutation custody before the platform write. (src/cli/daemon-cli/install-restore-cli.ts:41, d015175d606b)
  • Recorded real launchd and upgrade evidence: The captured PR body reports real macOS launchd runs using published [email protected] and the candidate installer, replayed at d015175. Observations include byte-identical retained Node service arguments, working directory and generated environment; stale-expectation refusal preserving both plist and operator pin; stopped-service restoration with RPC recovery; published-driver restart and health; and retained Bun entrypoint/library selection. These exercise the changed CLI production boundary; Swift interception regressions are supplemental. (d015175d606b)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 327dc38 into main Oct 4, 2026
149 of 153 checks passed
@steipete
steipete deleted the steipete/mac-recovery-expected-runtime-pin branch October 4, 2026 19:31
@steipete

steipete commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 5, 2026
… pin (openclaw#165044)

Same-version Node rollback and prior-build hosting restoration still spawned
the retained CLI, which cannot accept --expected-runtime-pin, so an operator
repin between the app's final custody check and that CLI's snapshot was
overwritten. The bundled CLI could not be substituted because the installer
derives the service entrypoint, shim PATH, dev mode and Bun SQLite library
from its own process.

Core adds a hidden `gateway install --restore-service-cli` option that plans
the service for an explicitly supplied retained executable, entrypoint and
SQLite library. It requires --expected-runtime-pin and an explicit --runtime,
keeps pinning owned by --runtime-path, and does not require the failed
replacement service to be running. The app runs both recovery paths through
its bundled installer with the CLI-observed expectation; Swift never derives
the revision. A pin refusal during Node migration now clears activation
intent like the hosting-change refusal.

Move the auth-profile service-env collectors into a sibling module so
daemon-install-helpers.ts drops below the max-lines cap; its suppression and
baseline entry are removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: macos App: macos cli CLI command changes commands Command implementations docs Improvements or additions to documentation gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant