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
feat(pocket+telegram): executor bg-mode spawns + telegram orchestrator dispatch (#363)
Aurora Capitale6df91185108
diff --git a/claude-ops/scripts/ops-cron-pocket-executor.py b/claude-ops/scripts/ops-cron-pocket-executor.pyindex 293e9081..b87a92c8 100755--- a/claude-ops/scripts/ops-cron-pocket-executor.py+++ b/claude-ops/scripts/ops-cron-pocket-executor.py@@ -330,14 +330,27 @@ def spawn_worker(task: dict) -> dict | None:stderr_path = WORKER_LOGS / f"{worker_id}.err.log"prompt = build_worker_prompt(task)- cmd = [CLAUDE_BIN, "--dangerously-skip-permissions",- "--model", WORKER_MODEL, "-p", prompt]+ # 2026-05-25: switched from `claude -p` (one-shot headless) to+ # `claude --bg ... -p PROMPT` so workers appear in `claude agents`, are+ # steerable via SendMessage, survive terminal disconnects, and Sam can+ # attach/inspect live. Prompt MUST be passed as -p argument; stdin is+ # ignored by --bg (would leave the session idle with "(send a prompt)").env = os.environ.copy()env["CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS"] = "0"+ # Display name shown in `claude agents` list — short, categorical, NOT the+ # full prompt. Sam corrected 2026-05-25: name field ≠ prompt field.+ display_name = f"pocket: {(task.get('title') or task_id)[:60]}"+ cmd = [CLAUDE_BIN, "--dangerously-skip-permissions", "--bg",+ "--name", display_name,+ "--effort", "high",+ "--model", WORKER_MODEL,+ "--add-dir", str(EXEC_CWD),+ "-p", prompt]try:out_f = stdout_path.open("w")err_f = stderr_path.open("w")+ # claude --bg returns immediately with session id on stdout, then detaches.proc = subprocess.Popen(cmd,cwd=str(EXEC_CWD),@@ -347,6 +360,21 @@ def spawn_worker(task: dict) -> dict | None:stderr=err_f,start_new_session=True,)+ # Wait briefly for it to detach + write its session id, then keep going.+ try:+ proc.wait(timeout=10)+ except subprocess.TimeoutExpired:+ pass+ # Parse session id from stdout (claude --bg prints `backgrounded · <hex8>`).+ bg_session_id = None+ try:+ head = stdout_path.read_text()[:512]+ import re as _re+ m = _re.search(r"backgrounded[^a-f0-9]+([a-f0-9]{8,})", head)+ if m:+ bg_session_id = m.group(1)+ except Exception:+ passexcept OSError as e:log(f"spawn failed for {task_id}: {e}")return None@@ -361,6 +389,8 @@ def spawn_worker(task: dict) -> dict | None:"stdout": str(stdout_path),"stderr": str(stderr_path),"model": WORKER_MODEL,+ "bg_session_id": bg_session_id, # for SendMessage / claude attach+ "spawn_mode": "claude-bg",}append_jsonl(SPAWN_LEDGER, {"ts": record["started_at"],@@ -369,15 +399,50 @@ def spawn_worker(task: dict) -> dict | None:"pocket_task_id": task_id,"title": record["title"],"model": WORKER_MODEL,+ "bg_session_id": bg_session_id,+ "spawn_mode": "claude-bg",})- log(f"spawned {worker_id} pid={proc.pid} task={task_id}")+ log(f"spawned {worker_id} pid={proc.pid} task={task_id} bg_session={bg_session_id or 'unknown'}")return record++def _claude_agents_status_map() -> dict[str, str] | None:+ """Returns {sessionId: status} for all claude --bg sessions visible to the+ daemon. None if the query failed (never raises — reap must keep working)."""+ try:+ out = subprocess.run(+ [CLAUDE_BIN, "agents", "--json"],+ stdin=subprocess.DEVNULL,+ capture_output=True, text=True, timeout=10,+ )+ if out.returncode != 0:+ return None+ d = json.loads(out.stdout or "[]")+ sessions = d if isinstance(d, list) else d.get("sessions", []) or []+ return {s.get("sessionId", ""): s.get("status", "?") for s in sessions if s.get("sessionId")}+ except Exception:+ return None+++def _bg_session_done(bg_session_id: str | None, agents_map: dict[str, str] | None) -> bool:+ """A claude --bg session is 'done' when its session id no longer appears in+ `claude agents --json` (daemon evicts completed sessions) OR status is+ 'completed'/'stopped'. Returns False if we have no session id or the daemon+ query failed (can't tell)."""+ if not bg_session_id or agents_map is None:+ return False+ full_key = next((k for k in agents_map if k.startswith(bg_session_id)), None)+ if full_key is None:+ return True+ return agents_map.get(full_key, "?") in ("completed", "stopped", "done")+def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:"""Check each in-flight worker. If exited, write done.json receiptand remove from registry. If past deadline, SIGTERM. Returns(completed_count, killed_count)."""+ # Invalidate cached agents map at start of each reap pass.+ reap_workers.__dict__.pop("_agents_map", None)completed = 0killed = 0now = int(time.time())@@ -387,21 +452,73 @@ def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:if not pid:del in_flight[worker_id]continue- alive = pid_alive(pid)- if alive and now > rec.get("deadline_epoch", now + 1):- log(f"worker {worker_id} pid={pid} timed out — SIGTERM")- try:- os.killpg(os.getpgid(pid), signal.SIGTERM)- except (ProcessLookupError, PermissionError, OSError):- pass- killed += 1- continue- if alive:- continue+ # For claude --bg workers, the spawned PID is just the shim that+ # handed off to claude-daemon — it exits ~5s after spawn. PID-alive+ # is meaningless. Use `claude agents --json` to query real session state.+ bg_session_id = rec.get("bg_session_id")+ spawn_mode = rec.get("spawn_mode", "")+ if spawn_mode == "claude-bg":+ # PID-alive is meaningless for bg workers; never fall through to legacy.+ if not bg_session_id:+ if now > rec.get("deadline_epoch", now + 1):+ log(f"worker {worker_id} claude-bg missing session id — timed out")+ killed += 1+ del in_flight[worker_id]+ continue+ # Lazy-init the agents map per reap pass.+ if "_agents_map" not in reap_workers.__dict__:+ reap_workers._agents_map = _claude_agents_status_map()+ agents_map = reap_workers._agents_map+ if not _bg_session_done(bg_session_id, agents_map):+ # Still in-flight or daemon unreachable. Apply deadline timeout.+ if now > rec.get("deadline_epoch", now + 1):+ log(f"worker {worker_id} bg={bg_session_id} timed out — stopping session")+ try:+ subprocess.run([CLAUDE_BIN, "stop", bg_session_id],+ stdin=subprocess.DEVNULL, capture_output=True, timeout=10)+ except Exception:+ pass+ killed += 1+ continue+ continue+ # session disappeared from agents list → completed. Fall through to receipt.+ else:+ # Legacy claude -p path — PID check.+ alive = pid_alive(pid)+ if alive and now > rec.get("deadline_epoch", now + 1):+ log(f"worker {worker_id} pid={pid} timed out — SIGTERM")+ try:+ os.killpg(os.getpgid(pid), signal.SIGTERM)+ except (ProcessLookupError, PermissionError, OSError):+ pass+ killed += 1+ continue+ if alive:+ continuetask_id = rec.get("pocket_task_id", "unknown")stdout_path = Path(rec.get("stdout", ""))summary = ""- if stdout_path.exists():+ # For claude --bg workers, the shim stdout is uninteresting (just session+ # banner). Pull the real conversation output from `claude logs <session>`.+ bg_session_id = rec.get("bg_session_id")+ spawn_mode = rec.get("spawn_mode", "")+ if spawn_mode == "claude-bg" and bg_session_id:+ # Daemon takes a few seconds to flush session output after stop.+ # Retry up to 3× with backoff before giving up.+ for attempt in range(3):+ try:+ logs = subprocess.run(+ [CLAUDE_BIN, "logs", bg_session_id],+ stdin=subprocess.DEVNULL,+ capture_output=True, text=True, timeout=15,+ )+ if logs.returncode == 0 and len(logs.stdout.strip()) > 200:+ summary = logs.stdout[-4000:]+ break+ except Exception as e:+ log(f"claude logs fetch failed for {bg_session_id} (attempt {attempt+1}): {e}")+ time.sleep(2 * (attempt + 1)) # 2s, 4s, 6s+ if not summary and stdout_path.exists():try:txt = stdout_path.read_text()summary = txt[-4000:]diff --git a/claude-ops/scripts/ops-message-listener.sh b/claude-ops/scripts/ops-message-listener.shindex 6f89a49a..f64989c7 100755--- a/claude-ops/scripts/ops-message-listener.sh+++ b/claude-ops/scripts/ops-message-listener.sh@@ -10,9 +10,10 @@ LOG_DIR="$DATA_DIR/logs"LOG="$LOG_DIR/message-listener.log"QUEUE_FILE="$DATA_DIR/inbox-queue.json"HEALTH_FILE="$DATA_DIR/listener-health.txt"-STATE_FILE="$DATA_DIR/listener-state.json"+STATE_FILE="$DATA_DIR/tg-orchestrator-state.json"BRIDGE_DB="${WHATSAPP_BRIDGE_DB:-$HOME/.local/share/whatsapp-mcp/whatsapp-bridge/store/messages.db}"POLL_INTERVAL="${OPS_LISTENER_POLL_INTERVAL:-60}"+TG_TARGET_SESSION="${TELEGRAM_TARGET_SESSION:-ebfd4ba2}"mkdir -p "$LOG_DIR" "$DATA_DIR"@@ -35,19 +36,22 @@ load_state() {import json, osf = '$STATE_FILE'if os.path.exists(f):- data = json.load(open(f))+ try:+ data = json.load(open(f))+ offset = data.get('offset', 0)+ print(str(offset))+ except:+ print('0')else:- data = {'wa_last_seen': None, 'tg_last_seen': None}-print(data.get('wa_last_seen') or '')-print(data.get('tg_last_seen') or '')-" 2>/dev/null || printf '\n\n'+ print('0')+" 2>/dev/null || echo '0'}save_state() {- local wa_ts="$1" tg_ts="$2"+ local tg_offset="$1"python3 -c "import json-data = {'wa_last_seen': '$wa_ts', 'tg_last_seen': '$tg_ts'}+data = {'offset': int($tg_offset)}json.dump(data, open('$STATE_FILE', 'w'))" 2>/dev/null || true}@@ -55,7 +59,8 @@ json.dump(data, open('$STATE_FILE', 'w'))# ── Append messages to queue ──────────────────────────────────────────────append_to_queue() {local new_msgs_json="$1"- python3 -c "+ # Pass JSON via stdin to avoid triple-quote shell injection (Bugbot issue #14328751/1).+ printf '%s' "$new_msgs_json" | python3 -c "import json, sysfrom datetime import datetime, timezone@@ -65,7 +70,7 @@ try:except:queue = {'messages': [], 'last_updated': None}-new_msgs = json.loads('''$new_msgs_json''')+new_msgs = json.load(sys.stdin)# Dedup by message idexisting_ids = {m.get('id') for m in queue['messages']}added = 0@@ -84,6 +89,23 @@ print(added)" 2>/dev/null || echo 0}+# ── Dispatch Telegram messages to target session ───────────────────────────+dispatch_telegram() {+ local new_msgs_json="$1"+ printf '%s' "$new_msgs_json" | python3 -c "+import json, sys+msgs = json.load(sys.stdin)+for msg in msgs:+ from_user = msg.get('from', 'unknown')+ text = msg.get('text', '').replace('\n', '\\\\n')+ print(f'telegram:{from_user}:{text}')+" 2>/dev/null | while IFS= read -r dispatch_line; do+ if [[ -n "$dispatch_line" ]]; then+ ops-bg send "$TG_TARGET_SESSION" "$dispatch_line" 2>/dev/null || log "dispatch failed: $dispatch_line"+ fi+ done || true+}+# ── Poll WhatsApp ─────────────────────────────────────────────────────────poll_whatsapp() {local since="$1"@@ -118,23 +140,8 @@ print(json.dumps(filtered))}# ── Poll Telegram ─────────────────────────────────────────────────────────-# DEPRECATED: Telegram polling is now handled by the telegram-channel MCP server-# (telegram-server/channel-mcp.mjs). Two simultaneous consumers of getUpdates-# on the same bot token steal each other's updates (Telegram delivers each-# update to exactly ONE long-poller — the 409 / offset-contention bug).-#-# This branch exits immediately when OPS_DISABLE_TG_POLLER=1 (default going-# forward). Set that flag in Doppler or your shell env when the channel MCP-# is registered. WhatsApp polling in this listener is unaffected.poll_telegram() {local since_update_id="${1:-0}"-- # Gate: skip if the channel MCP is taking over Telegram polling.- if [[ "${OPS_DISABLE_TG_POLLER:-0}" == "1" ]]; then- echo "[]"- return- fi-local tg_token="${TELEGRAM_BOT_TOKEN:-}"if [[ -z "$tg_token" ]]; then@@ -187,14 +194,13 @@ log "START: ops-message-listener polling every ${POLL_INTERVAL}s"write_health "polling" "starting"# Load initial state-read -r WA_LAST_SEEN TG_LAST_UPDATE_ID <<< "$(load_state | tr '\n' ' ')"-WA_LAST_SEEN="${WA_LAST_SEEN:-}"+TG_LAST_UPDATE_ID=$(load_state)TG_LAST_UPDATE_ID="${TG_LAST_UPDATE_ID:-0}"+log "Loaded Telegram offset: $TG_LAST_UPDATE_ID"+# Bootstrap WA since: use 2 minutes ago on first run-if [[ -z "$WA_LAST_SEEN" ]]; then- WA_LAST_SEEN=$(date -u -v-2M "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -d "2 minutes ago" "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u "+%Y-%m-%dT%H:%M:%SZ")-fi+WA_LAST_SEEN=$(date -u -d "2 minutes ago" "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u "+%Y-%m-%dT%H:%M:%SZ")POLL_COUNT=0while true; do@@ -223,7 +229,8 @@ print(max(ts) if ts else '$WA_LAST_SEEN')if [[ "$TG_COUNT" -gt 0 ]]; thenADDED=$(append_to_queue "$TG_MSGS")- log "Telegram: $TG_COUNT new messages (queued $ADDED)"+ log "Telegram: $TG_COUNT new messages (queued $ADDED, dispatching to $TG_TARGET_SESSION)"+ dispatch_telegram "$TG_MSGS" || trueTG_LAST_UPDATE_ID=$(echo "$TG_MSGS" | python3 -c "import json, sysmsgs = json.load(sys.stdin)@@ -233,13 +240,13 @@ print(max(ids) if ids else $TG_LAST_UPDATE_ID)fi# ── Save state ──────────────────────────────────────────────────────────- save_state "$WA_LAST_SEEN" "$TG_LAST_UPDATE_ID"+ save_state "$TG_LAST_UPDATE_ID"# ── Write health ──────────────────────────────────────────────────────write_health "polling" "poll=$POLL_COUNT wa_new=$WA_COUNT tg_new=$TG_COUNT ts=$POLL_TS"if [[ $(( POLL_COUNT % 10 )) -eq 0 ]]; then- log "Heartbeat: poll #$POLL_COUNT — wa_seen=$WA_LAST_SEEN tg_offset=$TG_LAST_UPDATE_ID"+ log "Heartbeat: poll #$POLL_COUNT — tg_offset=$TG_LAST_UPDATE_ID"fisleep "$POLL_INTERVAL"