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(wacli-keepalive): close periodic_backfill race conditions

auroracapital6227eaca4ddb
diff --git a/claude-ops/scripts/wacli-keepalive.sh b/claude-ops/scripts/wacli-keepalive.sh
index 85a454db..ec2ed986 100755
--- a/claude-ops/scripts/wacli-keepalive.sh
+++ b/claude-ops/scripts/wacli-keepalive.sh
@@ -45,6 +45,11 @@ mkdir -p "$LOG_DIR" "$STORE" "$MEMORY_DIR" "$WACLI_CACHE_DIR"
# supervisor loop watches $BATCH_MARKER and always restarts sync when it's set,
# distinguishing a deliberate batch pause from an unexpected crash.
acquire_wacli_batch() {
+ # Reentrant: if an outer caller already holds the batch, skip the real work.
+ # _WACLI_BATCH_HELD is set by periodic_backfill's subshell to prevent inner
+ # functions (detect_missed_messages, write_backfill_memory) from releasing the
+ # marker while the subshell is still running.
+ if [[ "${_WACLI_BATCH_HELD:-0}" == "1" ]]; then return 0; fi
touch "$BATCH_MARKER"
local sync_pid=""
[[ -f "$SYNC_PID_FILE" ]] && sync_pid=$(cat "$SYNC_PID_FILE" 2>/dev/null || true)
@@ -65,6 +70,8 @@ acquire_wacli_batch() {
}
release_wacli_batch() {
+ # Reentrant: skip if an outer caller holds the batch (see acquire_wacli_batch).
+ if [[ "${_WACLI_BATCH_HELD:-0}" == "1" ]]; then return 0; fi
rm -f "$BATCH_MARKER"
}
@@ -350,8 +357,8 @@ if missed:
# queued chats this races the DNS lookup and saturates WhatsApp's new-session
# rate limit, yielding `failed to dial whatsapp web websocket` or 30s
# on-demand history sync timeouts. Instead, we let periodic_backfill below
-# pick up the work AFTER --follow is connected — it pauses --follow,
-# reuses the warm session, and resumes. See LAST_BACKFILL_TIME=0 below.
+# pick up the work AFTER --follow has stabilised (INITIAL_BACKFILL_DELAY
+# seconds) — it pauses --follow, reuses the warm session, and resumes.
# ── Wacli data cache for daemon consumption ─────────────────────────────
# Write chat/message data to JSON cache so the daemon never calls wacli directly.
@@ -415,13 +422,13 @@ check_pause_signal() {
# ── Periodic backfill in background ─────────────────────────────────────
# Runs every BACKFILL_INTERVAL during persistent sync. Non-blocking.
-# LAST_BACKFILL_TIME=0 forces the first check to fire immediately once
-# --follow is running (see supervisor loop below), replacing the pre-follow
-# backfill that used to live at line 347. The first pass does the same work
-# (auto_detect_empty_chats + detect_missed_messages + run_backfill) but runs
-# it through the warm --follow session via acquire_wacli_batch instead of
-# spinning up N parallel wacli processes that race for a fresh WebSocket.
-LAST_BACKFILL_TIME=0
+# The initial backfill that used to live before --follow was removed because
+# each `wacli history backfill` opens its own WebSocket, racing WhatsApp's
+# new-session rate limit. Instead, the first backfill fires after a short
+# stabilisation delay (INITIAL_BACKFILL_DELAY) once --follow is connected,
+# reusing the warm session via acquire_wacli_batch.
+INITIAL_BACKFILL_DELAY="${INITIAL_BACKFILL_DELAY:-30}"
+LAST_BACKFILL_TIME=$(( $(date +%s) - BACKFILL_INTERVAL + INITIAL_BACKFILL_DELAY ))
periodic_backfill() {
local now
@@ -431,12 +438,22 @@ periodic_backfill() {
log "PERIODIC-BACKFILL: checking for chats needing backfill"
(
+ # Hold the batch lock for the entire subshell so the supervisor loop
+ # always sees BATCH_MARKER while we are working. _WACLI_BATCH_HELD
+ # makes the inner acquire/release calls in detect_missed_messages and
+ # write_backfill_memory no-ops, preventing premature marker removal.
+ acquire_wacli_batch
+ _WACLI_BATCH_HELD=1
+
auto_detect_empty_chats
detect_missed_messages
if [[ -f "$STORE/.backfill_jids" ]] && [[ -s "$STORE/.backfill_jids" ]]; then
run_backfill
write_backfill_memory
fi
+
+ _WACLI_BATCH_HELD=0
+ release_wacli_batch
) &
}