Skip to content

fix: prevent LaunchAgent reinstall from leaving gateway down - #120699

Merged
steipete merged 4 commits into
mainfrom
codex/fix-launchd-cutover-safety
Aug 8, 2026
Merged

steipete merged 4 commits into
mainfrom
codex/fix-launchd-cutover-safety

Conversation

@steipete

@steipete steipete commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where macOS Gateway service installation could leave the Gateway unsupervised when gateway install --force was invoked from inside that same LaunchAgent, or when external activation failed after the existing job was booted out.

Related operational evidence came from a clawstudio ownership incident, although that incident's direct outage path was the separate built-in mutable-update handoff. Review of the sibling LaunchAgent lifecycle exposed this independently reproducible defect.

Why This Change Was Made

LaunchAgent install now refuses install or uninstall from its own launchd ancestry before any filesystem or launchctl mutation. External install treats the plist, generated environment file, generated wrapper, legacy definitions, and their loaded states as one transaction: it snapshots them before publication, activates the candidate, and restores the exact previous artifacts and supervision if any mutation fails. Staging remains definition-only and does not retire a live legacy job.

The activation owner now rejects ambiguous launchctl print state and checks real bootout/unload failures instead of proceeding with an unprovable prior state.

Production LOC: +276/-27 (net +249) | Tests: +272/-0

The production growth implements the missing lifecycle ownership transaction and removes the service-loss class across canonical and legacy LaunchAgent paths.

Provenance: the inline activation path was introduced by commit 5c9e4cd30a1ee9189f5f7d9d6dfa75b9a3a98ad8 on 2026-03-23 by Peter Steinberger; no associated PR is recorded by GitHub.

User Impact

Operators can no longer accidentally self-terminate the Gateway by reinstalling or uninstalling its LaunchAgent from an in-band agent/tool command. Failed external reinstalls restore the prior runnable service instead of leaving launchd with no job to respawn.

Evidence

  • Exact rebased head: f500a7e7ae1135240ce60df775532d9aa46c21fc
  • Blacksmith Testbox tbx_01kzhhjg7xbpnbgd4v8f9perwy (swift-barnacle), run 31277382157
    • pnpm test src/daemon/launchd.test.ts
    • 121 passed, 24 macOS/platform skips, 0 failed
  • Pre-rebase independent Testbox confirmation: tbx_01kzhg1sy73gdajptc0gyzehtk, run 31276315535, same 121/24 result
  • git diff --check origin/main...HEAD
  • Autoreview after rebase: clean, no accepted/actionable findings, confidence 0.96

Regression coverage includes canonical and configured-old-label in-band refusal, in-band uninstall refusal, stage non-mutation, ambiguous launchd state, loaded and unloaded rollback, fresh-install artifact cleanup, generated env/wrapper restoration, and legacy supervision restoration.

ClawSweeper was explicitly waived for this repair.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Aug 8, 2026
@steipete steipete self-assigned this Aug 8, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Aug 8, 2026
@clawsweeper

clawsweeper Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 8, 2026, 5:37 PM ET / 21:37 UTC.

ClawSweeper review

What this changes

This PR guards against in-band macOS LaunchAgent mutations and adds rollback for failed external Gateway LaunchAgent reinstalls.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⛔ Blocked by patch quality or review findings - 6 items remain

Keep open: the rollback path can still leave a previously loaded cached-label Gateway unsupervised after a failed reinstall. The PR is maintainer-authored and protected by the maintainer label, so it needs the focused repair and review rather than automated cleanup.

Priority: P1
Reviewed head: faa951bb593c7b2d78b0eab39684cc521e0b8ca4

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The patch addresses the correct lifecycle owner and has focused tests, but one P1 rollback state remains uncovered.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored PR is exempt from the external-contributor proof gate; its focused test evidence is supplemental, and a macOS launchd failure trace would still strengthen the repair review.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored PR is exempt from the external-contributor proof gate; its focused test evidence is supplemental, and a macOS launchd failure trace would still strengthen the repair review.
Evidence reviewed 5 items Current-main failure path: Current main removes legacy definitions before replacement activation and then performs bootout/unload/bootstrap without a transaction, so an activation failure can remove existing supervision.
Blocking rollback gap: The proposed snapshot treats an absent plist as unloaded, although the existing runtime reader recognizes a loaded cached label with no plist; activation bootouts that job and rollback will not re-bootstrap it after candidate activation fails.
Missing regression coverage: The new rollback test supplies a prior legacy plist, so it does not cover a loaded cached-label job whose definition file is already absent.
Findings 1 actionable finding [P1] Preserve loaded cached-label jobs during rollback
Security None None.

How this fits together

The macOS daemon manager writes Gateway LaunchAgent artifacts, asks launchd to activate them, and thereby controls whether the Gateway remains supervised. Its rollback path must preserve both service files and launchd’s actual loaded state when activation fails.

flowchart LR
  A[Gateway install command] --> B[LaunchAgent lifecycle manager]
  B --> C[Snapshot prior artifacts and state]
  C --> D[Publish and activate replacement]
  D --> E{Activation succeeds?}
  E -->|Yes| F[Gateway remains supervised]
  E -->|No| G[Restore prior service]
  G --> H[Prior launchd state restored]
Loading

Before merge

  • Preserve loaded cached-label jobs during rollback (P1) - A missing plist is treated as definitely unloaded here, but the existing runtime reader supports a loaded cached label without one. Activation then bootouts that registered job, and rollback skips re-bootstrap because loaded was recorded as false; a failed reinstall can therefore still leave the Gateway unsupervised. Probe launchd in this state and abort or preserve a recoverable definition before mutation.
  • Resolve merge risk (P1) - A failed reinstall can still leave a cached-label Gateway unsupervised when launchd has a loaded job but its plist is absent.
  • Resolve merge risk (P1) - Existing in-band install or uninstall automation will now fail explicitly and require an external shell; that availability-safe behavior change should be confirmed for operators.
  • Complete next step (P2) - The protected maintainer PR should receive its focused lifecycle repair through maintainer handling, not an automated repair lane.
  • Improve patch quality - Handle loaded cached labels with absent plists before any cutover mutation.
  • Improve patch quality - Add a forced activation-failure regression for canonical and legacy cached-label states.

Findings

  • [P1] Preserve loaded cached-label jobs during rollback — src/daemon/launchd.ts:1393-1402
Agent review details

Security

None.

PR surface

Source +249, Tests +270, Docs +1. Total +520 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 276 27 +249
Tests 1 270 0 +270
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 547 27 +520

Review metrics

Metric Value Why it matters
Runtime versus test delta runtime +276/-27; tests +270 The substantial lifecycle transaction adds a real owner boundary, but the uncovered cached-label state is within that new rollback surface.

Merge-risk options

Maintainer options:

  1. Preserve loaded cached-label jobs (recommended)
    Make the snapshot verify launchd state even without a plist and refuse or recover before mutating a loaded job with no restorable definition.

Technical review

Best possible solution:

Probe cached labels before any mutation and abort when a loaded job lacks a restorable definition, then add canonical and legacy failure-path coverage for that state.

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

Yes, source-reproducible: a loaded cached launchd label with no plist followed by candidate activation failure reaches the uncovered snapshot-and-rollback path.

Is this the best way to solve the issue?

No: the lifecycle-owner transaction is the right layer, but its missing-plist snapshot must not equate absence of a file with absence of a loaded launchd job.

Full review comments:

  • [P1] Preserve loaded cached-label jobs during rollback — src/daemon/launchd.ts:1393-1402
    A missing plist is treated as definitely unloaded here, but the existing runtime reader supports a loaded cached label without one. Activation then bootouts that registered job, and rollback skips re-bootstrap because loaded was recorded as false; a failed reinstall can therefore still leave the Gateway unsupervised. Probe launchd in this state and abort or preserve a recoverable definition before mutation.
    Confidence: 0.96

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 909509a5db8d.

Labels

Label changes:

  • add P1: A failed Gateway reinstall can still remove supervision for an active macOS service.
  • add merge-risk: 🚨 compatibility: The new in-band mutation refusal changes behavior for existing automation that invokes install or uninstall from the managed process.
  • add merge-risk: 🚨 availability: The patch changes service cutover and rollback behavior, and its current cached-label gap can leave the Gateway down.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🌊 off-meta tidepool and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored PR is exempt from the external-contributor proof gate; its focused test evidence is supplemental, and a macOS launchd failure trace would still strengthen the repair review.

Label justifications:

  • P1: A failed Gateway reinstall can still remove supervision for an active macOS service.
  • merge-risk: 🚨 availability: The patch changes service cutover and rollback behavior, and its current cached-label gap can leave the Gateway down.
  • merge-risk: 🚨 compatibility: The new in-band mutation refusal changes behavior for existing automation that invokes install or uninstall from the managed process.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🌊 off-meta tidepool and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored PR is exempt from the external-contributor proof gate; its focused test evidence is supplemental, and a macOS launchd failure trace would still strengthen the repair review.

Evidence

What I checked:

  • Current-main failure path: Current main removes legacy definitions before replacement activation and then performs bootout/unload/bootstrap without a transaction, so an activation failure can remove existing supervision. (src/daemon/launchd.ts:1286, 909509a5db8d)
  • Blocking rollback gap: The proposed snapshot treats an absent plist as unloaded, although the existing runtime reader recognizes a loaded cached label with no plist; activation bootouts that job and rollback will not re-bootstrap it after candidate activation fails. (src/daemon/launchd.ts:1393, faa951bb593c)
  • Missing regression coverage: The new rollback test supplies a prior legacy plist, so it does not cover a loaded cached-label job whose definition file is already absent. (src/daemon/launchd.test.ts:1562, faa951bb593c)
  • Feature provenance: History identifies the inline activation path as dating to the cited lifecycle refactor; current-main install routes through this owner. (src/daemon/launchd.ts:1359, 5c9e4cd30a1e)
  • Head validation context: The PR reports a focused 121-pass LaunchAgent test run for f500a7e; the subsequent faa951b commit changes only the test file for formatting, but it does not exercise the cached-label state. (src/daemon/launchd.test.ts:1, faa951bb593c)

Likely related people:

  • steipete: History attributes the original inline activation path to this area owner, and the current repair continues work on the same lifecycle owner. (role: introduced behavior and recent area contributor; confidence: high; commits: 5c9e4cd30a1e, 895b691c55, 6997453098; files: src/daemon/launchd.ts, src/daemon/launchd-current-service.ts)
  • vincentkoc: Recent supervision reconciliation and launchd startup-hardening work overlaps the service-state invariant affected here. (role: recent adjacent contributor; confidence: medium; commits: d7c173b; files: src/daemon/launchd.ts)

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 e70e43f into main Aug 8, 2026
120 checks passed
@steipete
steipete deleted the codex/fix-launchd-cutover-safety branch August 8, 2026 22:23
@steipete

steipete commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant