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: serialize post-update migrations (#696)

Aurora Capital2e4cd38bdc04
diff --git a/claude-ops/bin/ops-post-update-migrate b/claude-ops/bin/ops-post-update-migrate
index 9e524f66..93d323ca 100755
--- a/claude-ops/bin/ops-post-update-migrate
+++ b/claude-ops/bin/ops-post-update-migrate
@@ -26,48 +26,45 @@ mkdir -p "$DATA_DIR" 2>/dev/null || true
log() { printf "[%s] %s\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*" >> "$LOG"; }
+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
+ rmdir "$LOCK_DIR" 2>/dev/null && mkdir "$LOCK_DIR" 2>/dev/null || exit 0
+ else
+ exit 0
+ fi
+fi
+trap 'rmdir "$LOCK_DIR" 2>/dev/null' EXIT
+
# Always refresh current/ + installPath — must run on every cache materialization,
# including same-version force-reinstalls where the per-version sentinel already exists.
refresh_current_directory() {
- local cache_parent current_dir installed_json
+ local cache_parent current_dir installed_json root_real current_real
cache_parent="$(dirname "$PLUGIN_ROOT")"
current_dir="$cache_parent/current"
installed_json="$PLUGINS_DIR/installed_plugins.json"
-
- # Guard: when the plugin already runs FROM current/, PLUGIN_ROOT == current_dir.
- # rsync/cp of a directory onto itself is pointless and, with --delete under
- # concurrent SessionStart hooks, storms the disk (fseventsd) and spikes load.
- local root_real cur_real
root_real="$(cd "$PLUGIN_ROOT" 2>/dev/null && pwd -P || echo "$PLUGIN_ROOT")"
- cur_real="$(cd "$current_dir" 2>/dev/null && pwd -P || echo "$current_dir")"
- if [[ "$root_real" == "$cur_real" ]]; then
- log " skip: PLUGIN_ROOT already == current/ ($cur_real) — no self-rsync"
+ current_real="$(cd "$current_dir" 2>/dev/null && pwd -P || echo "$current_dir")"
+
+ if [[ "$root_real" == "$current_real" ]]; then
+ log " skip: PLUGIN_ROOT already == current/ ($current_real) — no self-rsync"
+ elif command -v rsync >/dev/null 2>&1; then
+ if rsync -a --delete "$PLUGIN_ROOT/" "$current_dir/" 2>/dev/null; then
+ log " ok: refreshed $current_dir via rsync"
+ else
+ log " warn: rsync to current/ failed (non-fatal)"
+ fi
else
- # Single-flight lock so concurrent sessions can't stack rsyncs on current/.
- local lock="$DATA_DIR/.refresh-current.lock"
- [[ -d "$lock" ]] && [[ -n "$(find "$lock" -maxdepth 0 -mmin +2 2>/dev/null)" ]] \
- && rmdir "$lock" 2>/dev/null || true # clear stale lock from a killed run
- if mkdir "$lock" 2>/dev/null; then
- trap 'rmdir "'"$lock"'" 2>/dev/null || true' RETURN
- if command -v rsync >/dev/null 2>&1; then
- rsync -a --delete "$PLUGIN_ROOT/" "$current_dir/" 2>/dev/null \
- && log " ok: refreshed $current_dir via rsync" \
- || log " warn: rsync to current/ failed (non-fatal)"
- else
- mkdir -p "$current_dir"
- cp -rp "$PLUGIN_ROOT/." "$current_dir/" 2>/dev/null \
- && log " ok: refreshed $current_dir via cp" \
- || log " warn: cp to current/ failed (non-fatal)"
- fi
+ mkdir -p "$current_dir"
+ if cp -rp "$PLUGIN_ROOT/." "$current_dir/" 2>/dev/null; then
+ log " ok: refreshed $current_dir via cp"
else
- log " skip: refresh-current lock held by another process"
+ log " warn: cp to current/ failed (non-fatal)"
fi
fi
if [[ -f "$installed_json" ]]; then
- python3 - "$installed_json" "$current_dir" <<'PYEOF' 2>/dev/null \
- && log " ok: patched installed_plugins.json → current" \
- || log " warn: installed_plugins.json patch failed (non-fatal)"
+ if python3 - "$installed_json" "$current_dir" <<'PYEOF' 2>/dev/null
import json, sys
path, current = sys.argv[1], sys.argv[2]
with open(path) as f:
@@ -81,6 +78,11 @@ if changed:
with open(path, "w") as f:
json.dump(d, f, indent=2)
PYEOF
+ then
+ log " ok: patched installed_plugins.json → current"
+ else
+ log " warn: installed_plugins.json patch failed (non-fatal)"
+ fi
fi
}
@@ -143,51 +145,7 @@ run_migrations() {
fi
fi
- # 4. Stable 'current/' directory — prevents "Plugin directory does not exist" errors
- # when Claude Code updates the plugin mid-session and deletes the old versioned dir.
- {
- local cache_parent current_dir
- cache_parent="$(dirname "$PLUGIN_ROOT")"
- current_dir="$cache_parent/current"
-
- local b_root_real b_cur_real
- b_root_real="$(cd "$PLUGIN_ROOT" 2>/dev/null && pwd -P || echo "$PLUGIN_ROOT")"
- b_cur_real="$(cd "$current_dir" 2>/dev/null && pwd -P || echo "$current_dir")"
- if [[ "$b_root_real" == "$b_cur_real" ]]; then
- log " skip: PLUGIN_ROOT already == current/ ($b_cur_real) — no self-rsync"
- elif command -v rsync >/dev/null 2>&1; then
- rsync -a --delete "$PLUGIN_ROOT/" "$current_dir/" 2>/dev/null \
- && log " ok: refreshed $current_dir via rsync" \
- || log " warn: rsync to current/ failed (non-fatal)"
- else
- mkdir -p "$current_dir"
- cp -rp "$PLUGIN_ROOT/." "$current_dir/" 2>/dev/null \
- && log " ok: refreshed $current_dir via cp" \
- || log " warn: cp to current/ failed (non-fatal)"
- fi
-
- local installed_json="$HOME/.claude/plugins/installed_plugins.json"
- if [[ -f "$installed_json" ]]; then
- python3 - "$installed_json" "$current_dir" <<'PYEOF' 2>/dev/null \
- && log " ok: patched installed_plugins.json → current" \
- || log " warn: installed_plugins.json patch failed (non-fatal)"
-import json, sys
-path, current = sys.argv[1], sys.argv[2]
-with open(path) as f:
- d = json.load(f)
-changed = False
-for e in d.get("plugins", {}).get("ops@ops-marketplace", []):
- if e.get("scope") == "user" and e.get("installPath", "") != current:
- e["installPath"] = current
- changed = True
-if changed:
- with open(path, "w") as f:
- json.dump(d, f, indent=2)
-PYEOF
- fi
- } || true
-
- # 5. Add future migrations here. Pattern:
+ # 4. Add future migrations here. Pattern:
# if needs_migration; then
# run_idempotent_migration && log " ok: <name>"
# fi
@@ -198,8 +156,11 @@ PYEOF
# Run with a 60s wall-clock budget so a SessionStart hook never blocks Claude.
( run_migrations ) &
MIGRATE_PID=$!
-( sleep 60; kill -0 "$MIGRATE_PID" 2>/dev/null && kill "$MIGRATE_PID" 2>/dev/null ) &
-
+for ((waited = 0; waited < 60; waited++)); do
+ kill -0 "$MIGRATE_PID" 2>/dev/null || break
+ sleep 1
+done
+kill -0 "$MIGRATE_PID" 2>/dev/null && kill "$MIGRATE_PID" 2>/dev/null || true
wait "$MIGRATE_PID" 2>/dev/null || true
# Stamp sentinel even on partial failure — individual migrations log their own state.
diff --git a/claude-ops/tests/test-post-update-migrate-lock.sh b/claude-ops/tests/test-post-update-migrate-lock.sh
new file mode 100644
index 00000000..3619e4ce
--- /dev/null
+++ b/claude-ops/tests/test-post-update-migrate-lock.sh
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+TMP="$(mktemp -d)"
+trap 'rm -rf "$TMP"' EXIT
+
+PLUGIN_ROOT="$TMP/cache/2.38.4"
+CONFIG_DIR="$TMP/config"
+DATA_DIR="$TMP/data"
+BIN_DIR="$TMP/bin"
+RSYNC_LOG="$TMP/rsync.log"
+SCRIPT="$PLUGIN_ROOT/bin/ops-post-update-migrate"
+
+mkdir -p "$TMP/home" "$PLUGIN_ROOT/.claude-plugin" "$PLUGIN_ROOT/bin" "$CONFIG_DIR/plugins" "$DATA_DIR/.migrated" "$BIN_DIR"
+printf '{"version":"test"}\n' > "$PLUGIN_ROOT/.claude-plugin/plugin.json"
+touch "$DATA_DIR/.migrated/vtest"
+cp "$ROOT/bin/ops-post-update-migrate" "$SCRIPT"
+
+cat > "$BIN_DIR/rsync" <<'EOF'
+#!/usr/bin/env bash
+printf '%s\n' "$$" >> "$RSYNC_LOG"
+sleep 2
+EOF
+chmod +x "$BIN_DIR/rsync"
+
+run_migrate() {
+ HOME="$TMP/home" \
+ CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT" \
+ CLAUDE_CONFIG_DIR="$CONFIG_DIR" \
+ CLAUDE_PLUGIN_DATA_DIR="$DATA_DIR" \
+ RSYNC_LOG="$RSYNC_LOG" \
+ PATH="$BIN_DIR:$PATH" \
+ bash "$SCRIPT"
+}
+
+run_migrate &
+first=$!
+run_migrate &
+second=$!
+wait "$first" "$second"
+
+count="$(wc -l < "$RSYNC_LOG" | tr -d ' ')"
+if [[ "$count" != "1" ]]; then
+ printf 'FAIL: expected one cache refresh, got %s\n' "$count" >&2
+ exit 1
+fi
+
+printf 'PASS: concurrent migrations share one refresh\n'
+
+: > "$RSYNC_LOG"
+rm "$DATA_DIR/.migrated/vtest"
+run_migrate
+
+count="$(wc -l < "$RSYNC_LOG" | tr -d ' ')"
+if [[ "$count" != "1" ]]; then
+ printf 'FAIL: expected one first-run refresh, got %s\n' "$count" >&2
+ exit 1
+fi
+
+printf 'PASS: first-time migration refreshes once\n'
+
+if pgrep -f "^bash $SCRIPT$" >/dev/null; then
+ printf 'FAIL: migration left a watchdog process running\n' >&2
+ exit 1
+fi
+
+printf 'PASS: migration leaves no watchdog process\n'