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.

feat(ops-inbox): Mac WhatsApp.app ChatStorage fallback for bridge-miss recovery (#555)

Aurora Capitalb50887399dc5
diff --git a/claude-ops/bin/wa-inbox-fresh.sh b/claude-ops/bin/wa-inbox-fresh.sh
index 849d7155..de444cc3 100755
--- a/claude-ops/bin/wa-inbox-fresh.sh
+++ b/claude-ops/bin/wa-inbox-fresh.sh
@@ -167,6 +167,7 @@ age_min=$(sqlite3 "$DB" "SELECT CAST((julianday('now')-julianday(MAX(timestamp))
log "wa-fresh: newest message = ${after} (${age_min:-?} min old) | new rows this cycle = ${new}"
if [ "${age_min:-9999}" -gt 120 ]; then
log "wa-fresh: WARNING newest message is >2h old — store may be lagging; treat 'last-sender' classification with caution"
+ log "wa-fresh: store may be lagging — cross-check Mac ground truth: bin/wa-mac-latest.sh --recent"
fi
log "wa-fresh: NOTE phone-sent messages may be absent — confirm 'did I reply?' with the human, not this store"
exit 0
diff --git a/claude-ops/bin/wa-mac-latest.sh b/claude-ops/bin/wa-mac-latest.sh
new file mode 100755
index 00000000..8ceeddae
--- /dev/null
+++ b/claude-ops/bin/wa-mac-latest.sh
@@ -0,0 +1,54 @@
+#!/usr/bin/env bash
+# wa-mac-latest.sh — Fallback WhatsApp reader.
+# Reads the Mac WhatsApp.app local store (ChatStorage.sqlite, unencrypted) over
+# Tailscale SSH as ground truth when the whatsmeow bridge's sync lags or misses
+# inbound messages (esp. @lid chats). Read-only.
+#
+# Usage:
+# wa-mac-latest.sh --recent [N] latest N messages across ALL chats (default 15)
+# wa-mac-latest.sh --contact <q> [N] latest N for a contact by name or number (default 12)
+# wa-mac-latest.sh --since "YYYY-MM-DD HH:MM" all messages since a local timestamp
+# add --json for machine-readable output
+# Env: WA_MAC_SSH ssh target (REQUIRED, e.g. user@mac-host)
+set -euo pipefail
+MAC="${WA_MAC_SSH:?set WA_MAC_SSH=user@mac-host (Tailscale ssh target)}"
+DB='$HOME/Library/Group Containers/group.net.whatsapp.WhatsApp.shared/ChatStorage.sqlite'
+AE=978307200
+JSON=0; MODE="recent"; ARG=""; N=""
+while [ $# -gt 0 ]; do
+ case "$1" in
+ --json) JSON=1 ;;
+ --recent) MODE="recent" ;;
+ --contact) MODE="contact"; ARG="${2:-}"; shift ;;
+ --since) MODE="since"; ARG="${2:-}"; shift ;;
+ *) N="$1" ;;
+ esac; shift
+done
+case "$MODE" in recent) LIMIT="${N:-15}";; contact) LIMIT="${N:-12}";; *) LIMIT="" ;; esac
+dt="datetime(ZMESSAGEDATE+$AE,'unixepoch','localtime')"
+case "$MODE" in
+ recent) SQL="SELECT $dt,m.ZISFROMME,s.ZPARTNERNAME,substr(m.ZTEXT,1,160) FROM ZWAMESSAGE m JOIN ZWACHATSESSION s ON m.ZCHATSESSION=s.Z_PK WHERE m.ZTEXT IS NOT NULL ORDER BY m.ZMESSAGEDATE DESC LIMIT $LIMIT;" ;;
+ contact) q=$(printf '%s' "$ARG"|sed "s/'/''/g"); SQL="SELECT $dt,m.ZISFROMME,s.ZPARTNERNAME,substr(m.ZTEXT,1,200) FROM ZWAMESSAGE m JOIN ZWACHATSESSION s ON m.ZCHATSESSION=s.Z_PK WHERE (s.ZPARTNERNAME LIKE '%$q%' OR s.ZCONTACTJID LIKE '%$q%') AND m.ZTEXT IS NOT NULL ORDER BY m.ZMESSAGEDATE DESC LIMIT $LIMIT;" ;;
+ since) ts=$(printf '%s' "$ARG"|sed "s/'/''/g"); SQL="SELECT $dt,m.ZISFROMME,s.ZPARTNERNAME,substr(m.ZTEXT,1,160) FROM ZWAMESSAGE m JOIN ZWACHATSESSION s ON m.ZCHATSESSION=s.Z_PK WHERE m.ZTEXT IS NOT NULL AND $dt >= '$ts' ORDER BY m.ZMESSAGEDATE DESC;" ;;
+esac
+raw=$(timeout 35 ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$MAC" "sqlite3 -separator $'\x1f' \"$DB\" \"$SQL\"" 2>&1) || { echo "ERROR: could not reach Mac WhatsApp DB ($MAC). $raw" >&2; exit 2; }
+if [ "$JSON" = 1 ]; then
+ printf '%s\n' "$raw" | python3 -c 'import sys,json;US="\x1f";o=[]
+for l in sys.stdin:
+ l=l.rstrip("\n")
+ if not l:continue
+ p=l.split(US)
+ if len(p)<4:continue
+ o.append({"date":p[0],"from_me":p[1]=="1","who":p[2],"text":p[3]})
+print(json.dumps(o,ensure_ascii=False,indent=2))'
+else
+ printf '%s\n' "$raw" | python3 -c 'import sys
+US="\x1f"
+for l in sys.stdin:
+ l=l.rstrip("\n")
+ if not l:continue
+ p=l.split(US)
+ if len(p)<4:continue
+ d="→ me " if p[1]=="1" else "← them"
+ print(f"{p[0]} {d} [{p[2]}] {p[3]}")'
+fi
diff --git a/claude-ops/skills/ops-inbox/SKILL.md b/claude-ops/skills/ops-inbox/SKILL.md
index 4373e611..5b9fd7cf 100644
--- a/claude-ops/skills/ops-inbox/SKILL.md
+++ b/claude-ops/skills/ops-inbox/SKILL.md
@@ -83,6 +83,15 @@ Before executing, load available context:
**0a. Freshness gate (run FIRST, blocking, bounded).** Before classifying anything, run `~/bin/wa-inbox-fresh.sh` (shipped by `scripts/install-whatsapp-bridge-linux.sh`). It probes the bridge with a real **curl connection probe** (`curl -s -m4 http://127.0.0.1:8080/`), forces a backfill, triggers voice-note transcription, and waits (bounded ~32s) for the newest message to settle, then prints a FRESHNESS report (`newest message = … (N min old)`). It **only restarts the bridge if the curl probe genuinely fails twice** — do NOT gate liveness on `ss | grep :8080`, because `ss` renders port 8080 as the service name `webcache`, so the grep never matches and you'd needlessly bounce a healthy bridge. Exit 2 means the bridge is down and unrecoverable → the store is STALE, do not trust last-sender classification.
+### Mac WhatsApp.app fallback (bridge-miss recovery)
+
+The whatsmeow bridge can **silently miss inbound messages** when its history/app-state sync lags — most often on `@lid` chats (e.g. 2026-06-11 it missed a reply from a contact that the Mac WhatsApp.app had). The Mac app keeps an **unencrypted** local Core Data store at `~/Library/Group Containers/group.net.whatsapp.WhatsApp.shared/ChatStorage.sqlite`, readable over Tailscale SSH, so it is a reliable ground-truth backstop.
+
+- **When to use:** if `wa-inbox-fresh.sh` reports the store is **STALE** (exit 2, or "newest message" far older than expected), OR a contact's *known* reply is missing from the bridge (common on `@lid` chats), cross-check the Mac app's ground-truth store before classifying that thread as "no reply".
+- **Command:** `bin/wa-mac-latest.sh --contact <name|number> [N]` (also `--recent [N]`, `--since "YYYY-MM-DD HH:MM"`, add `--json` for machine-readable output). It reads `~/Library/Group Containers/group.net.whatsapp.WhatsApp.shared/ChatStorage.sqlite` over Tailscale SSH (target via `WA_MAC_SSH`, e.g. `user@mac-host`). Schema: `ZWAMESSAGE` (`ZTEXT`, `ZISFROMME`, `ZMESSAGEDATE` = seconds since 2001-01-01) joined to `ZWACHATSESSION` (`ZPARTNERNAME`, `ZCONTACTJID`).
+- **READ-ONLY ground truth.** This reader never writes and never sends. Sends still go through the whatsmeow bridge (`mcp__whatsapp__send_message`) under the Rule-6 outbound-approval gate — the Mac store is only consulted to confirm what actually arrived.
+- **Why no Linux-native alternative:** there is no official WhatsApp Linux desktop app; the third-party Flatpak clients (`whatsapp-for-linux`, ZapZap) are Electron WhatsApp-Web wrappers that need a GUI, consume a linked-device slot, and store data in encrypted IndexedDB (not a queryable SQLite) — so the Mac `ChatStorage.sqlite` is the preferred backstop.
+
**The FULL-THREAD AWARENESS GATE (in "Processing each channel") depends on this step having run first.** That gate's "read both directions incl. `[voice]`" only works once `wa-inbox-fresh.sh` (freshness + backfill) and the voice-note transcription pass (step 0c) have completed and the store has settled — otherwise outbound rows and `[voice]` bodies are still missing and the gate reads an incomplete thread.
**0b. Background backfill + contacts-link** (idempotent, safe every time). The backfill pulls recent messages for the 50 most-active chats; the link populates `messages.db.contacts` from the whatsmeow session store so both `<pn>@s.whatsapp.net` and `<lid>@lid` chat JIDs resolve to names (without it the `contacts` table is empty and LID-format chats show raw phone numbers):