Skip to content

fix: gateway avoids duplicate macOS launchd services - #97296

Closed
LiLan0125 wants to merge 1 commit into
openclaw:mainfrom
LiLan0125:fix/97178-launchdaemon-conflict
Closed

LiLan0125 wants to merge 1 commit into
openclaw:mainfrom
LiLan0125:fix/97178-launchdaemon-conflict

Conversation

@LiLan0125

Copy link
Copy Markdown
Contributor

Closes #97178

What Problem This Solves

Fixes an issue where macOS users installing or restarting the Gateway as a user LaunchAgent could end up with duplicate OpenClaw services when an operator-managed system LaunchDaemon already uses the same launchd label.

Why This Change Was Made

The LaunchAgent lifecycle now checks the system launchd domain before installing, repairing, bootstrapping, or restarting the user service, including the normal restart kickstart path. It detects both loaded system/<label> services and system LaunchDaemon plist files whose Label key matches the user LaunchAgent label, then surfaces the conflict through install, recovery, and update flows instead of silently proceeding.

User Impact

macOS users get an explicit error that tells them to remove, rename, or choose a different label for the system LaunchDaemon before OpenClaw creates or restarts a user LaunchAgent with the same identity. This prevents duplicate gateway services and restart loops across launchd domains.

Evidence

  • node scripts/run-vitest.mjs run src/daemon/launchd.test.ts passed 1 Vitest shard in 13.62s.
  • node scripts/run-vitest.mjs run src/daemon/launchd.test.ts src/cli/daemon-cli/launchd-recovery.test.ts src/cli/update-cli/update-command.test.ts passed 2 Vitest shards in 23.07s.
  • git diff --check passed.
  • Production-function proof below calls the actual patched installLaunchAgent and restartLaunchAgent implementations from src/daemon/launchd.ts.

Real behavior proof

Behavior addressed: Gateway LaunchAgent install and restart refuse to create or restart a user-domain service when launchd already has system/ai.openclaw.gateway.

Environment tested: Local source checkout on Linux with Node and a temporary launchctl executable on PATH that reports system/ai.openclaw.gateway as loaded.

Steps run after the patch: Called the actual installLaunchAgent, restartLaunchAgent, and resolveLaunchAgentPlistPath exports from src/daemon/launchd.ts via node --import tsx --no-warnings -e, with a temporary HOME and launchctl command log.

Evidence after fix:

install blocked: true
install first line: Refusing to install LaunchAgent ai.openclaw.gateway because launchd already has system/ai.openclaw.gateway.
restart blocked: true
restart first line: Refusing to install LaunchAgent ai.openclaw.gateway because launchd already has system/ai.openclaw.gateway.
launchctl calls: print system/ai.openclaw.gateway | print system/ai.openclaw.gateway
kickstart called: false
user plist written: false

Observed result after the fix: Both install and restart reject the same-label system LaunchDaemon before writing the user plist or running launchctl kickstart.

Not tested: A live macOS LaunchDaemon removal/rename flow and a real GUI LaunchAgent session were not exercised in this Linux checkout.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes size: M labels Jun 27, 2026
@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 27, 2026, 2:10 PM ET / 18:10 UTC.

Summary
Adds same-label system LaunchDaemon conflict checks to macOS LaunchAgent install, restart, bootstrap repair, and update recovery error propagation.

PR surface: Source +123, Tests +196. Total +319 across 6 files.

Reproducibility: yes. The linked issue has concrete live headless macOS evidence, and current main source still writes and bootstraps only the gui-domain LaunchAgent without a same-label system-domain guard; I did not rerun a live macOS LaunchDaemon scenario in this read-only review.

Review metrics: 1 noteworthy metric.

  • Fail-closed lifecycle guards: install, restart, and bootstrap repair guarded. These operator-facing startup and recovery paths now stop on a same-label system LaunchDaemon, which is the intended fix but needs upgrade-path proof.

Stored data model
Persistent data-model change detected: migration/backfill/repair: src/daemon/launchd.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #97178
Summary: This PR is the candidate fix for the linked duplicate system LaunchDaemon plus user LaunchAgent restart-storm bug; the broader system LaunchDaemon lifecycle issue overlaps but should not absorb this narrow guard unless maintainers choose a larger design pass.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add redacted macOS terminal or log proof showing install and restart refuse a same-label system LaunchDaemon before writing the user plist or running kickstart.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR includes focused tests and copied terminal output from production functions on Linux with a fake launchctl, but no after-fix proof against a real macOS LaunchDaemon/LaunchAgent setup; the contributor should add redacted macOS terminal/log proof before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Merging intentionally makes same-label system LaunchDaemon detection a hard stop for user LaunchAgent install/restart/update recovery, so existing hosts with a stale or operator-managed system plist must take manual action before those flows proceed.
  • [P1] The contributor proof is source-level with a fake launchctl on Linux; it does not yet prove the same behavior against real macOS launchd domains and /Library/LaunchDaemons permissions.

Maintainer options:

  1. Require live macOS launchd proof (recommended)
    Ask for redacted macOS terminal or log proof showing install and restart refuse a same-label system LaunchDaemon before writing the user plist or kickstarting the user service.
  2. Accept source-level proof only
    Maintainers can intentionally merge with the current source-level and CI proof, accepting that the real launchd domain and permission behavior was not exercised before merge.
  3. Pause for system-scope design
    If maintainers want install/status/stop/uninstall to share one user/system launchd contract first, pause this PR and fold it into the broader LaunchDaemon lifecycle direction.

Next step before merge

  • [P1] Needs contributor-provided real macOS launchd proof and maintainer acceptance of the intentional fail-closed operator impact; there is no narrow code repair for automation to apply right now.

Security
Cleared: No concrete security or supply-chain regression found; the patch reads launchd state and root-owned plist metadata but does not add privileged writes, sudo execution, dependency changes, or secret handling.

Review details

Best possible solution:

Land the narrow duplicate-manager guard after redacted live macOS launchd proof confirms same-label system LaunchDaemon cases are blocked before user LaunchAgent mutation, while keeping broader system LaunchDaemon lifecycle work in the open companion issue.

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

Yes. The linked issue has concrete live headless macOS evidence, and current main source still writes and bootstraps only the gui-domain LaunchAgent without a same-label system-domain guard; I did not rerun a live macOS LaunchDaemon scenario in this read-only review.

Is this the best way to solve the issue?

Yes, with one proof gap. A pre-write and pre-kickstart conflict guard is the narrowest maintainable fix for this duplicate-manager bug; full system LaunchDaemon lifecycle management should remain with the broader companion issue.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 98ed83f848de.

Label changes

Label justifications:

  • P2: This is a normal-priority macOS gateway service-management bugfix with meaningful availability impact but limited platform and configuration scope.
  • merge-risk: 🚨 compatibility: The PR changes existing macOS install/restart/update recovery behavior to fail closed when a same-label system LaunchDaemon or plist exists, requiring operator cleanup before proceeding.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR includes focused tests and copied terminal output from production functions on Linux with a fake launchctl, but no after-fix proof against a real macOS LaunchDaemon/LaunchAgent setup; the contributor should add redacted macOS terminal/log proof before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +123, Tests +196. Total +319 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 3 127 4 +123
Tests 3 201 5 +196
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 328 9 +319

What I checked:

  • Repository policy applied: Root AGENTS.md was read fully, plus the gateway scoped guide; the startup/setup compatibility and real behavior proof rules affected this review. (AGENTS.md:1, 98ed83f848de)
  • Linked bug context: The linked issue describes a live v2026.6.10 headless macOS system LaunchDaemon that gateway status misses, steering operators toward gateway install and a duplicate gui-domain LaunchAgent.
  • Current main lacks the guard: Current main writes the user LaunchAgent plist and activates it without first checking the system launchd domain or /Library/LaunchDaemons for the same label. (src/daemon/launchd.ts:1016, 98ed83f848de)
  • PR blocks install before mutation: The PR head calls assertNoSystemLaunchDaemonConflict before writeLaunchAgentPlist, so the user plist is not written when a same-label system LaunchDaemon conflict is detected. (src/daemon/launchd.ts:1120, 603ef6d0f5af)
  • PR blocks restart and recovery paths: The PR head checks for a system LaunchDaemon before both normal restart paths and bootstrap repair, and propagates the conflict detail through update recovery instead of collapsing it to a generic repair failure. (src/daemon/launchd.ts:1249, 603ef6d0f5af)
  • Existing broader LaunchDaemon lifecycle remains separate: The open system LaunchDaemon lifecycle issue still tracks stop/uninstall/system-scope product and privilege decisions; this PR stays narrower by refusing duplicate user LaunchAgent creation rather than adding system lifecycle management.

Likely related people:

  • steipete: History shows repeated launchd/service-management refactors and prior ClawSweeper review context around LaunchAgent recovery, service adapters, and extra gateway discovery. (role: gateway lifecycle contributor; confidence: high; commits: 391a3d6eaf2d, 52e3d28ef434, fd968bfb2d16; files: src/daemon/launchd.ts, src/daemon/inspect.ts, src/daemon/service.ts)
  • vincentkoc: Recent history includes launchd-domain error guidance, update/service refresh work, and launchd test performance work near the affected service-management surface. (role: recent adjacent contributor; confidence: medium; commits: be7462af1eca, 65f92fd83915, d5b1329bf358; files: src/daemon/launchd.ts, src/daemon/launchd.test.ts, src/cli/update-cli/update-command.ts)
  • ngutman: History shows focused work on launchd stop persistence, enable-state scoping, stop confirmation, and lifecycle simplification in the same helper/test area. (role: launchd stop lifecycle contributor; confidence: medium; commits: 23d9a100c4aa, c0ddcf663016, affffddf045d; files: src/daemon/launchd.ts, src/daemon/launchd.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 27, 2026
@steipete

steipete commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Closing this in favor of #97285, which now owns the same #97178 duplicate-launchd-manager fix with the stronger proof and current readiness.

Both branches guard LaunchAgent install, restart, bootstrap repair, and update recovery against a same-label system LaunchDaemon. #97285 additionally covers the doctor repair surface, has redacted after-fix proof against a real root-owned macOS LaunchDaemon plus a current-main negative control, and its exact head 41ecff66e1c827954359f3775e63c828f1ebe2cb has green CI and real-behavior-proof checks. This branch still lacks that macOS after-fix proof, so keeping the better-proven canonical implementation avoids parallel repair of the same root cause.

Thank you for the thorough launchd guard and regression coverage, @LiLan0125.

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

Labels

cli CLI command changes gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: openclaw gateway install adds a duplicate gui-domain LaunchAgent over an existing system LaunchDaemon (restart storm)

2 participants