Skip to content

Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.

fix(migrate): never mutate the real launchd domain from a sandboxed HOME (#847)

Aurora Capital3c173c3ddce8
diff --git a/claude-ops/bin/ops-post-update-migrate b/claude-ops/bin/ops-post-update-migrate
index 90358cc2..de2d81ac 100755
--- a/claude-ops/bin/ops-post-update-migrate
+++ b/claude-ops/bin/ops-post-update-migrate
@@ -31,6 +31,33 @@ mkdir -p "$DATA_DIR" 2>/dev/null || true
log() { printf "[%s] %s\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >> "$LOG"; }
+# launchd is a per-USER global namespace. It does NOT honour $HOME, so a test that
+# sandboxes HOME (mktemp -d) still writes plists into a fake home but then loads
+# and unloads them in the REAL gui/<uid> domain, under the real ${USER} label.
+#
+# That is not theoretical. On 2026-08-20 tests/test-post-update-migrate-sentinel.sh
+# ran against a temp HOME, and its `launchctl unload/load` re-pointed the live
+# com.<user>.whatsapp-bridge job at a 12-byte `#!/bin/bash` stub in /var/folders.
+# The production WhatsApp bridge was SIGTERMed and stayed dead, with launchd
+# reporting a temp-dir Program, until it was manually re-bootstrapped.
+#
+# Guard the whole class: any launchctl mutation is skipped unless $HOME is the
+# invoking user's real home. A sandboxed run still exercises every path that
+# writes files, which is what the tests actually assert on.
+REAL_HOME="$(python3 -c 'import os,pwd; print(pwd.getpwuid(os.getuid()).pw_dir)' 2>/dev/null || echo "$HOME")"
+
+launchctl_guarded() {
+ if [[ "${OPS_MIGRATE_NO_LAUNCHCTL:-0}" == "1" ]]; then
+ log " skip: launchctl $* (OPS_MIGRATE_NO_LAUNCHCTL=1)"
+ return 0
+ fi
+ if [[ "$HOME" != "$REAL_HOME" ]]; then
+ log " skip: launchctl $* (sandboxed HOME=$HOME, real=$REAL_HOME)"
+ return 0
+ fi
+ launchctl "$@" 2>/dev/null || true
+}
+
LOCK_DIR="$DATA_DIR/.post-update-migrate.lock"
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
if [[ -n "$(find "$LOCK_DIR" -maxdepth 0 -mmin +10 -print 2>/dev/null)" ]]; then
@@ -170,12 +197,12 @@ run_migrations() {
local legacy_wa_plist="$HOME/Library/LaunchAgents/com.claude-ops.whatsapp-bridge.plist"
local legacy_user_plist="$HOME/Library/LaunchAgents/com.<user>.whatsapp-bridge.plist"
if [[ -f "$legacy_wa_plist" && "$legacy_wa_plist" != "$plist_dst" ]]; then
- launchctl unload "$legacy_wa_plist" 2>/dev/null || true
+ launchctl_guarded unload "$legacy_wa_plist"
rm -f "$legacy_wa_plist" 2>/dev/null || true
log " ok: removed legacy com.claude-ops whatsapp-bridge LaunchAgent"
fi
if [[ -f "$legacy_user_plist" && "$legacy_user_plist" != "$plist_dst" ]]; then
- launchctl unload "$legacy_user_plist" 2>/dev/null || true
+ launchctl_guarded unload "$legacy_user_plist"
rm -f "$legacy_user_plist" 2>/dev/null || true
log " ok: removed legacy com.<user> whatsapp-bridge LaunchAgent"
fi
@@ -203,8 +230,8 @@ run_migrations() {
-e "s#__HOME__#$HOME#g" \
-e "s#__USER__#$USER#g" \
"$plist_src" > "$plist_dst" 2>/dev/null && {
- launchctl unload "$plist_dst" 2>/dev/null || true
- launchctl load "$plist_dst" 2>/dev/null || true
+ launchctl_guarded unload "$plist_dst"
+ launchctl_guarded load "$plist_dst"
log " ok: whatsapp-bridge LaunchAgent refreshed"
}
fi
@@ -215,7 +242,7 @@ run_migrations() {
if [[ "$(uname -s)" == "Darwin" ]]; then
local stale_plist="$HOME/Library/LaunchAgents/com.claude-ops.wacli-keepalive.plist"
if [[ -f "$stale_plist" ]]; then
- launchctl unload "$stale_plist" 2>/dev/null || true
+ launchctl_guarded unload "$stale_plist"
rm -f "$stale_plist" 2>/dev/null || true
log " ok: removed deprecated wacli-keepalive LaunchAgent"
fi
diff --git a/claude-ops/tests/run-all.sh b/claude-ops/tests/run-all.sh
index 3166be39..57fe976f 100755
--- a/claude-ops/tests/run-all.sh
+++ b/claude-ops/tests/run-all.sh
@@ -49,6 +49,7 @@ run_suite "$TESTS_DIR/test-claude-invoke.sh"
run_suite "$TESTS_DIR/test-staged-enrollment.sh"
run_suite "$TESTS_DIR/test-coordination-provisioning.sh"
run_suite "$TESTS_DIR/test-post-update-migrate-sentinel.sh"
+run_suite "$TESTS_DIR/test-migrate-launchctl-domain-isolation.sh"
run_suite "$TESTS_DIR/test-template.sh"
run_suite "$TESTS_DIR/test-claude-md.sh"
run_suite "$TESTS_DIR/test-no-secrets.sh"
diff --git a/claude-ops/tests/test-migrate-launchctl-domain-isolation.sh b/claude-ops/tests/test-migrate-launchctl-domain-isolation.sh
new file mode 100755
index 00000000..fd4e4743
--- /dev/null
+++ b/claude-ops/tests/test-migrate-launchctl-domain-isolation.sh
@@ -0,0 +1,160 @@
+#!/usr/bin/env bash
+# Regression guard: ops-post-update-migrate must never mutate the REAL launchd
+# domain when it runs against a sandboxed $HOME.
+#
+# THE BUG THIS PREVENTS (2026-08-20, production incident)
+# ------------------------------------------------------
+# launchd is a per-USER global namespace and does NOT honour $HOME. The sentinel
+# test sandboxes HOME with mktemp -d, but the migration's plist label is built
+# from the REAL ${USER}, and `launchctl unload/load` therefore acted on the real
+# gui/<uid> domain. Running the test suite re-pointed the live
+# com.<user>.whatsapp-bridge job at a 12-byte `#!/bin/bash` stub inside
+# /var/folders, SIGTERMed the production WhatsApp bridge, and left it dead:
+#
+# launchd: booting out service: caller = launchctl<-bash<-bash...
+# launchctl list com.<user>.whatsapp-bridge
+# "Program" = "/var/folders/.../T/tmp.XXXX/bridge/run-bridge.sh"
+#
+# HOW THIS TEST WORKS
+# -------------------
+# It does NOT call launchctl for real. It shims `launchctl` onto $PATH so any
+# invocation is recorded to a file, then asserts the recording is empty for a
+# sandboxed HOME and non-empty for a real-HOME run. That keeps the guard honest
+# (it observes the actual syscall surface) without touching the machine.
+set -uo pipefail
+
+TESTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+SCRIPT="${1:-$TESTS_DIR/../bin/ops-post-update-migrate}"
+
+PASS=0
+FAIL=0
+
+ck() {
+ if [[ "$2" == "$3" ]]; then
+ echo " PASS: $1"
+ PASS=$((PASS + 1))
+ else
+ echo " FAIL: $1 (want '$3', got '$2')"
+ FAIL=$((FAIL + 1))
+ fi
+}
+
+if [[ "$(uname -s)" != "Darwin" ]]; then
+ echo "SKIP: launchd domain isolation is macOS-only"
+ exit 0
+fi
+
+# --- shim launchctl so nothing real is ever touched -------------------------
+SHIM_DIR=$(mktemp -d)
+CALLS="$SHIM_DIR/launchctl-calls.txt"
+: > "$CALLS"
+cat > "$SHIM_DIR/launchctl" <<SHIM
+#!/bin/bash
+printf '%s\n' "\$*" >> "$CALLS"
+exit 0
+SHIM
+chmod +x "$SHIM_DIR/launchctl"
+export PATH="$SHIM_DIR:$PATH"
+
+setup_sandbox() {
+ ROOT=$(mktemp -d)
+ export HOME="$ROOT/home"
+ mkdir -p "$HOME/Library/LaunchAgents"
+ PLUGIN_ROOT="$ROOT/plugin"
+ mkdir -p "$PLUGIN_ROOT/.claude-plugin" "$PLUGIN_ROOT/assets/launchagents"
+ echo '{"version":"9.9.9-test"}' >"$PLUGIN_ROOT/.claude-plugin/plugin.json"
+ cat >"$PLUGIN_ROOT/assets/launchagents/com.claude-ops.whatsapp-bridge.plist" <<'PL'
+<?xml version="1.0" encoding="UTF-8"?>
+<plist version="1.0"><dict>
+<key>Label</key><string>com.__USER__.whatsapp-bridge</string>
+<key>ProgramArguments</key><array><string>__BRIDGE_WORKING_DIR__/run-bridge.sh</string></array>
+</dict></plist>
+PL
+ export CLAUDE_PLUGIN_DATA_DIR="$ROOT/data"
+ mkdir -p "$CLAUDE_PLUGIN_DATA_DIR"
+ export WHATSAPP_BRIDGE_HOME="$ROOT/bridge"
+ mkdir -p "$WHATSAPP_BRIDGE_HOME/logs"
+ # wrapper present, so the migration takes the branch that loads a LaunchAgent
+ printf '#!/bin/bash\n' >"$WHATSAPP_BRIDGE_HOME/run-bridge.sh"
+ chmod +x "$WHATSAPP_BRIDGE_HOME/run-bridge.sh"
+ PLIST="$HOME/Library/LaunchAgents/com.${USER}.whatsapp-bridge.plist"
+ # a legacy plist too, to exercise the unload-and-remove branches
+ printf '<plist/>' >"$HOME/Library/LaunchAgents/com.claude-ops.wacli-keepalive.plist"
+}
+
+run() { CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT" bash "$SCRIPT" >/dev/null 2>&1 || true; }
+
+REAL_HOME_VALUE="$(python3 -c 'import os,pwd; print(pwd.getpwuid(os.getuid()).pw_dir)')"
+
+echo "=== sandboxed HOME must not touch the real launchd domain ==="
+setup_sandbox
+: > "$CALLS"
+run
+n_calls=$(wc -l < "$CALLS" | tr -d ' ')
+ck "zero launchctl invocations" "$n_calls" "0"
+ck "plist still written (file work not skipped)" "$([[ -f $PLIST ]] && echo yes || echo no)" "yes"
+if [[ -s "$CALLS" ]]; then
+ echo " offending calls:"
+ sed 's/^/ /' "$CALLS"
+fi
+
+echo
+echo "=== the guard must not be dead code: real HOME still loads ==="
+# The guard keys on $HOME, so proving it is live requires the REAL home. That
+# makes the migration's destination path real too:
+# $HOME/Library/LaunchAgents/com.${USER}.whatsapp-bridge.plist
+# With the real $USER that is the LIVE production plist, and this test would
+# overwrite it with sandbox paths — reintroducing, from the guard itself, the
+# exact corruption the guard exists to prevent.
+#
+# Override USER instead. The destination becomes a bogus label that launchd has
+# never heard of, so the file is inert, while $HOME stays real and the guard's
+# condition is genuinely exercised. The shim still intercepts the launchctl call.
+FAKE_USER="ops-migrate-guard-test-$$"
+REAL_PLIST="$REAL_HOME_VALUE/Library/LaunchAgents/com.${USER}.whatsapp-bridge.plist"
+REAL_PLIST_SUM_BEFORE="$( [[ -f "$REAL_PLIST" ]] && shasum -a256 "$REAL_PLIST" | awk '{print $1}' || echo absent)"
+TEST_PLIST="$REAL_HOME_VALUE/Library/LaunchAgents/com.${FAKE_USER}.whatsapp-bridge.plist"
+
+setup_sandbox
+: > "$CALLS"
+CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT" \
+ HOME="$REAL_HOME_VALUE" \
+ USER="$FAKE_USER" \
+ CLAUDE_PLUGIN_DATA_DIR="$CLAUDE_PLUGIN_DATA_DIR" \
+ WHATSAPP_BRIDGE_HOME="$WHATSAPP_BRIDGE_HOME" \
+ bash "$SCRIPT" >/dev/null 2>&1 || true
+rm -f "$TEST_PLIST"
+n_real=$(wc -l < "$CALLS" | tr -d ' ')
+if [[ "$n_real" -gt 0 ]]; then
+ echo " PASS: real HOME still invokes launchctl ($n_real call(s)) — guard is live, not dead code"
+ PASS=$((PASS + 1))
+else
+ echo " FAIL: real HOME made zero launchctl calls — the guard is unconditionally off"
+ FAIL=$((FAIL + 1))
+fi
+
+# The whole point of this suite is that running it never damages the machine.
+# Assert that, do not assume it.
+REAL_PLIST_SUM_AFTER="$( [[ -f "$REAL_PLIST" ]] && shasum -a256 "$REAL_PLIST" | awk '{print $1}' || echo absent)"
+ck "live production plist untouched by this test" "$REAL_PLIST_SUM_AFTER" "$REAL_PLIST_SUM_BEFORE"
+ck "no stray test plist left behind" "$([[ -f $TEST_PLIST ]] && echo yes || echo no)" "no"
+
+echo
+echo "=== explicit opt-out env var also suppresses ==="
+setup_sandbox
+: > "$CALLS"
+CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT" \
+ HOME="$REAL_HOME_VALUE" \
+ USER="$FAKE_USER" \
+ OPS_MIGRATE_NO_LAUNCHCTL=1 \
+ CLAUDE_PLUGIN_DATA_DIR="$CLAUDE_PLUGIN_DATA_DIR" \
+ WHATSAPP_BRIDGE_HOME="$WHATSAPP_BRIDGE_HOME" \
+ bash "$SCRIPT" >/dev/null 2>&1 || true
+rm -f "$TEST_PLIST"
+ck "zero launchctl invocations with OPS_MIGRATE_NO_LAUNCHCTL=1" "$(wc -l < "$CALLS" | tr -d ' ')" "0"
+
+rm -rf "$SHIM_DIR"
+echo
+echo " Passed: $PASS"
+echo " Failed: $FAIL"
+[[ $FAIL -eq 0 ]]
diff --git a/claude-ops/tests/test-post-update-migrate-sentinel.sh b/claude-ops/tests/test-post-update-migrate-sentinel.sh
index ef16aea6..383e1a99 100755
--- a/claude-ops/tests/test-post-update-migrate-sentinel.sh
+++ b/claude-ops/tests/test-post-update-migrate-sentinel.sh
@@ -33,6 +33,12 @@ ck() {
setup() {
ROOT=$(mktemp -d)
export HOME="$ROOT/home"
+ # $USER feeds the plist label AND the launchd label. Leaving the real value
+ # here means a sandboxed run still names the operator's live service, which is
+ # how tests/test-migrate-launchctl-domain-isolation.sh's incident happened.
+ # Keep the label fake so nothing this test writes can ever collide with a real
+ # one, even if a future migration resolves paths differently.
+ export USER="ops-migrate-sentinel-test-$$"
mkdir -p "$HOME/Library/LaunchAgents"
PLUGIN_ROOT="$ROOT/plugin"
mkdir -p "$PLUGIN_ROOT/.claude-plugin" "$PLUGIN_ROOT/assets/launchagents"