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.
Shell agent-teamsai-agentai-automationanthropicautonomous-agentawsbusiness-operationsclaudeclaude-codeclaude-code-plugindevopsecommerceinbox-zeroklaviyomarketingmcpmorning-briefingpr-automationshopifyterminal
fix: resolve wacli store-lock race between launchd keepalive and ops daemon
auroracapitalb46465295ca7
diff --git a/claude-ops/scripts/ops-daemon.sh b/claude-ops/scripts/ops-daemon.shindex 0df9ff4d..893e1d85 100755--- a/claude-ops/scripts/ops-daemon.sh+++ b/claude-ops/scripts/ops-daemon.sh@@ -140,9 +140,39 @@ else:echo $(( $(date +%s) + interval_min ))}+# ── Launchd ownership check ──────────────────────────────────────────────+# Returns 0 if the launchd keepalive service for wacli is loaded and has a+# live process. When true, the daemon must NOT start wacli-sync itself —+# launchd is the sole owner and handles restarts via KeepAlive=true.+_wacli_owned_by_launchd() {+ command -v launchctl >/dev/null 2>&1 || return 1+ local pid_str+ pid_str=$(launchctl list 2>/dev/null \+ | awk '$3=="com.claude-ops.wacli-keepalive" {print $1}' || true)+ # Service must be registered AND have a live PID (not "-")+ if [[ -n "$pid_str" ]] && [[ "$pid_str" != "-" ]] && kill -0 "$pid_str" 2>/dev/null; then+ return 0+ fi+ return 1+}+# ── Service lifecycle ─────────────────────────────────────────────────────start_service() {local name="$1"++ # ── Race-condition guard: wacli-sync ownership ──────────────────────────+ # The launchd keepalive service (com.claude-ops.wacli-keepalive) is the+ # sole owner of the wacli sync process. If it is loaded and running, the+ # daemon must not spawn a competing wacli-sync — doing so causes store-lock+ # errors and WhatsApp disconnections. Instead we mark the service as+ # "delegated" and monitor health passively via the health file.+ if [[ "$name" == "wacli-sync" ]] && _wacli_owned_by_launchd; then+ log "START: $name — launchd keepalive is running, deferring ownership (no duplicate spawn)"+ SERVICE_STATUS["$name"]="delegated"+ SERVICE_LAST_HEALTH["$name"]="launchd-owned"+ return 0+ fi+local cmdcmd=$(get_service_field "$name" "command")if [[ -z "$cmd" ]]; then@@ -231,6 +261,14 @@ check_health() {restart_service() {local name="$1"++ # ── Race-condition guard (mirrors start_service) ────────────────────────+ if [[ "$name" == "wacli-sync" ]] && _wacli_owned_by_launchd; then+ log "RESTART: $name — launchd keepalive owns this service, skipping daemon restart"+ SERVICE_STATUS["$name"]="delegated"+ return 0+ fi+local restart_delay max_restartsrestart_delay=$(get_service_field "$name" "restart_delay")restart_delay="${restart_delay:-60}"@@ -1240,7 +1278,19 @@ while true; dofielse# Persistent service: check health + restart if needed- if ! check_health "$svc"; then+ # For wacli-sync delegated to launchd, re-check ownership each loop.+ # If launchd stopped unexpectedly, the daemon can take over.+ if [[ "$svc" == "wacli-sync" ]] && [[ "${SERVICE_STATUS[$svc]:-}" == "delegated" ]]; then+ if _wacli_owned_by_launchd; then+ # Still owned by launchd — just read the health file passively+ check_health "$svc" || true+ SERVICE_STATUS["$svc"]="delegated"+ else+ log "MONITOR: $svc — launchd keepalive no longer running, daemon taking ownership"+ SERVICE_STATUS["$svc"]="dead"+ start_service "$svc"+ fi+ elif ! check_health "$svc"; thenlocal_status="${SERVICE_STATUS[$svc]}"if [[ "$local_status" != "needs_reauth" ]] && [[ "$local_status" != "max_restarts_exceeded" ]]; thenrestart_service "$svc"diff --git a/claude-ops/scripts/wacli-keepalive.sh b/claude-ops/scripts/wacli-keepalive.shindex 9c95257f..de8260e4 100755--- a/claude-ops/scripts/wacli-keepalive.sh+++ b/claude-ops/scripts/wacli-keepalive.sh@@ -77,14 +77,46 @@ filog "START: keepalive pid=$$ starting"-# ── Step 0: Race condition prevention ───────────────────────────────────+# ── Step 0: Race condition prevention with exponential backoff ──────────# Both com.claude-ops.daemon and com.claude-ops.wacli-keepalive start at boot.# If both try wacli sync simultaneously, one crashes on the store lock.-# Wait briefly for any concurrent sync to claim the lock first.-if pgrep -f "wacli sync" >/dev/null 2>&1; then- log "START: another wacli sync is running — waiting 15s for lock release"- sleep 15-fi+# Retry with exponential backoff instead of a single fixed wait.+LOCK_RETRY_MAX=5+LOCK_RETRY_DELAY=5 # seconds — doubles each attempt: 5, 10, 20, 40, 80+_wait_for_store_lock() {+ local attempt=0+ local delay=$LOCK_RETRY_DELAY+ while (( attempt < LOCK_RETRY_MAX )); do+ # Check both running sync processes and the store lock itself+ local has_sync=0 has_lock=0+ pgrep -f "wacli sync" >/dev/null 2>&1 && has_sync=1+ local doctor_json+ doctor_json=$("$WACLI" doctor --json 2>/dev/null || echo '{}')+ echo "$doctor_json" | grep -q '"locked":true' && has_lock=1++ if [[ $has_sync -eq 0 ]] && [[ $has_lock -eq 0 ]]; then+ return 0 # Lock is free+ fi++ log "START: store locked or another sync running (attempt $((attempt+1))/$LOCK_RETRY_MAX) — waiting ${delay}s"+ sleep "$delay"+ delay=$((delay * 2))+ (( attempt++ )) || true+ done++ # Final attempt: force-kill competing processes and clear the lock+ log "START: lock still held after $LOCK_RETRY_MAX retries — force-clearing"+ local stale_pids+ stale_pids=$(pgrep -f "wacli sync" 2>/dev/null || true)+ for pid in $stale_pids; do+ if [[ "$pid" != "$$" ]]; then+ kill -TERM "$pid" 2>/dev/null || true+ fi+ done+ sleep 2+ return 0+}+_wait_for_store_lock# ── Step 1: Kill orphaned wacli sync processes ──────────────────────────cleanup_stale() {