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(dispatch): strip ANTHROPIC_API_KEY from all claude agent spawns (#497)

Aurora Capitalbdfb472439dc
diff --git a/claude-ops/bin/ops-bg b/claude-ops/bin/ops-bg
index 7582a9ce..61d34f74 100755
--- a/claude-ops/bin/ops-bg
+++ b/claude-ops/bin/ops-bg
@@ -21,6 +21,8 @@
# --model opus
# --effort high
# --add-dir $PWD
+# --name (optional) name shown in `claude agents` / fleet TUI for the bg session
+# --agent (optional) specialized agent persona
#
# Examples:
# ops-bg dispatch "audit My-Project repos for cost-leak gaps"
@@ -39,7 +41,7 @@ CLAUDE_BIN="${CLAUDE_BIN:-}"
if [ -z "$CLAUDE_BIN" ]; then
for candidate in \
"$HOME/.local/bin/claude" \
- "/opt/homebrew/bin/claude" \
+ "$HOME/.local/bin/claude" \
"/usr/local/bin/claude"; do
if [ -x "$candidate" ]; then
CLAUDE_BIN="$candidate"
@@ -63,8 +65,9 @@ SUB="$1"; shift
cmd_dispatch() {
local model="opus"
local effort="high"
- local add_dir="${OPSBG_DIR:-$HOME/Projects}"
+ local add_dir="$PWD"
local agent=""
+ local name=""
local extra=()
while [ $# -gt 0 ]; do
case "$1" in
@@ -72,29 +75,28 @@ cmd_dispatch() {
--effort) effort="$2"; shift 2 ;;
--cwd|--add-dir) add_dir="$2"; shift 2 ;;
--agent) agent="$2"; shift 2 ;;
- --) shift; extra+=("$@"); set --; break ;;
+ --name) name="$2"; shift 2 ;;
+ --) shift; extra+=("$@"); break ;;
--help|-h) USAGE 0 ;;
*) break ;;
esac
done
- if [ $# -lt 1 ] && [ ${#extra[@]} -eq 0 ]; then
+ if [ $# -lt 1 ]; then
echo "ops-bg dispatch: missing <prompt>" >&2
exit 2
fi
- local prompt=""
- [ $# -ge 1 ] && prompt="$*"
- if [ ! -d "$add_dir" ]; then
- echo "ops-bg dispatch: workdir does not exist: $add_dir" >&2
- exit 2
- fi
- add_dir="$(cd "$add_dir" && pwd)"
+ local prompt="$*"
local -a args=(--bg --model "$model" --effort "$effort" --add-dir "$add_dir")
+ if [ -n "$name" ]; then
+ args=(--bg --name "$name" --model "$model" --effort "$effort" --add-dir "$add_dir")
+ fi
if [ -n "$agent" ]; then
args+=(--agent "$agent")
fi
- [ -n "$prompt" ] && args+=("$prompt")
- [ "${#extra[@]}" -gt 0 ] && args+=("${extra[@]}")
- cd "$add_dir" # start fleet agents in default workdir
+ args+=("$prompt")
+ args+=("${extra[@]}")
+ # Never let agents inherit the API key — force OAuth (subscription) auth.
+ unset ANTHROPIC_API_KEY
exec "$CLAUDE_BIN" "${args[@]}"
}
diff --git a/claude-ops/scripts/ops-cron-pocket-executor.py b/claude-ops/scripts/ops-cron-pocket-executor.py
index ed8d0632..9b70153f 100755
--- a/claude-ops/scripts/ops-cron-pocket-executor.py
+++ b/claude-ops/scripts/ops-cron-pocket-executor.py
@@ -43,6 +43,7 @@ Env:
POCKET_WORKER_TIMEOUT seconds, default 1800 (30 min)
POCKET_EXEC_DRY_RUN=1 inspect only, no spawns / no writes.
"""
+
from __future__ import annotations
import json
@@ -94,7 +95,13 @@ SUPERVISOR_HEALTH = STATE_DIR / ".supervisor-health"
EXECUTOR_HEALTH = STATE_DIR / ".executor-health"
LOG_FILE = STATE_DIR / "executor.log"
-OUTBOUND_KINDS = {"send_message", "draft_email", "send_file", "send_sms", "send_whatsapp"}
+OUTBOUND_KINDS = {
+ "send_message",
+ "draft_email",
+ "send_file",
+ "send_sms",
+ "send_whatsapp",
+}
def now_iso() -> str:
@@ -112,7 +119,9 @@ def log(msg: str) -> None:
pass
-def write_health(target: Path, status: str, msg: str = "", extra: dict | None = None) -> None:
+def write_health(
+ target: Path, status: str, msg: str = "", extra: dict | None = None
+) -> None:
payload = {"status": status, "message": msg, "last_run": now_iso()}
if extra:
payload.update(extra)
@@ -230,7 +239,14 @@ def process_replies() -> int:
processed = 0
for r in new_replies:
qid = r["id"]
- q = next((qq for qq in questions if qq.get("id") == qid and qq.get("status") == "open"), None)
+ q = next(
+ (
+ qq
+ for qq in questions
+ if qq.get("id") == qid and qq.get("status") == "open"
+ ),
+ None,
+ )
if not q:
log(f"reply for unknown/closed qid {qid} — archiving")
append_jsonl(REPLIES_ARCHIVE, r)
@@ -239,16 +255,25 @@ def process_replies() -> int:
worker = q.get("from_worker", "")
if worker:
worker_inbox = STATE_DIR / "worker-inboxes" / f"{worker}.jsonl"
- append_jsonl(worker_inbox, {
- "ts": now_iso(),
- "from": "supervisor",
- "qid": qid,
+ append_jsonl(
+ worker_inbox,
+ {
+ "ts": now_iso(),
+ "from": "supervisor",
+ "qid": qid,
+ "answer": r.get("answer", ""),
+ "via": r.get("via", ""),
+ },
+ )
+ append_jsonl(
+ ANSWERED,
+ {
+ **q,
+ "status": "answered",
"answer": r.get("answer", ""),
- "via": r.get("via", ""),
- })
- append_jsonl(ANSWERED, {**q, "status": "answered",
- "answer": r.get("answer", ""),
- "answered_at": now_iso()})
+ "answered_at": now_iso(),
+ },
+ )
append_jsonl(REPLIES_ARCHIVE, r)
answered_ids.add(qid)
processed += 1
@@ -258,7 +283,9 @@ def process_replies() -> int:
try:
remaining = [q for q in questions if q.get("id") not in answered_ids]
QUESTIONS.write_text(
- ("\n".join(json.dumps(q) for q in remaining) + "\n") if remaining else ""
+ ("\n".join(json.dumps(q) for q in remaining) + "\n")
+ if remaining
+ else ""
)
REPLIES.write_text("")
except OSError as e:
@@ -333,7 +360,10 @@ def _pocket_notify(event: str, message: str, severity: str = "medium") -> None:
try:
subprocess.run(
["ops-pocket-notify", event, message, "--severity", severity],
- stdin=subprocess.DEVNULL, capture_output=True, timeout=20, check=False,
+ stdin=subprocess.DEVNULL,
+ capture_output=True,
+ timeout=20,
+ check=False,
)
except (OSError, subprocess.SubprocessError):
pass
@@ -371,20 +401,38 @@ def spawn_worker(task: dict) -> dict | None:
# 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]
+ cmd = [
+ CLAUDE_BIN,
+ "--dangerously-skip-permissions",
+ "--bg",
+ "--name",
+ display_name,
+ "--effort",
+ "high",
+ "--model",
+ WORKER_MODEL,
+ "--add-dir",
+ str(EXEC_CWD),
+ "-p",
+ prompt,
+ ]
if WORKER_USER and WORKER_USER != (os.environ.get("USER") or ""):
# Drop privileges: run the worker as the restricted POCKET_WORKER_USER.
# `sudo -n` is non-interactive (fails loudly if NOPASSWD sudoers is missing
# rather than hanging); `-H` sets HOME to the worker user's home so Claude
# writes session state there; --preserve-env carries only the vars the
# worker needs, dropping the executor user's secrets from the environment.
- _preserve = "PATH,CLAUDE_CONFIG_DIR,CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS,ANTHROPIC_API_KEY,POCKET_WORKER_MODEL,POCKET_ENV_BROKER_SOCK,POCKET_STATE_DIR,POCKET_TASK_ID,POCKET_WORKER_ID"
- cmd = ["sudo", "-n", "-H", "-u", WORKER_USER, f"--preserve-env={_preserve}", "--", *cmd]
+ _preserve = "PATH,CLAUDE_CONFIG_DIR,CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS,POCKET_WORKER_MODEL,POCKET_ENV_BROKER_SOCK,POCKET_STATE_DIR,POCKET_TASK_ID,POCKET_WORKER_ID"
+ cmd = [
+ "sudo",
+ "-n",
+ "-H",
+ "-u",
+ WORKER_USER,
+ f"--preserve-env={_preserve}",
+ "--",
+ *cmd,
+ ]
try:
out_f = stdout_path.open("w")
@@ -409,6 +457,7 @@ def spawn_worker(task: dict) -> dict | 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)
@@ -431,22 +480,29 @@ def spawn_worker(task: dict) -> dict | None:
"bg_session_id": bg_session_id, # for SendMessage / claude attach
"spawn_mode": "claude-bg",
}
- append_jsonl(SPAWN_LEDGER, {
- "ts": record["started_at"],
- "worker": worker_id,
- "pid": proc.pid,
- "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} bg_session={bg_session_id or 'unknown'}")
- _pocket_notify("worker.spawned", f"worker started for {task_id}: {(task.get('title') or '')[:80]}")
+ append_jsonl(
+ SPAWN_LEDGER,
+ {
+ "ts": record["started_at"],
+ "worker": worker_id,
+ "pid": proc.pid,
+ "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} bg_session={bg_session_id or 'unknown'}"
+ )
+ _pocket_notify(
+ "worker.spawned",
+ f"worker started for {task_id}: {(task.get('title') or '')[:80]}",
+ )
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)."""
@@ -454,18 +510,26 @@ def _claude_agents_status_map() -> dict[str, str] | None:
out = subprocess.run(
[CLAUDE_BIN, "agents", "--json"],
stdin=subprocess.DEVNULL,
- capture_output=True, text=True, timeout=10,
+ 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")}
+ 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:
+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
@@ -477,6 +541,7 @@ def _bg_session_done(bg_session_id: str | None, agents_map: dict[str, str] | Non
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 receipt
and remove from registry. If past deadline, SIGTERM. Returns
@@ -502,7 +567,11 @@ def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:
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")
- _pocket_notify("worker.failed", f"worker {worker_id} timed out (no session id)", "high")
+ _pocket_notify(
+ "worker.failed",
+ f"worker {worker_id} timed out (no session id)",
+ "high",
+ )
killed += 1
del in_flight[worker_id]
continue
@@ -513,11 +582,21 @@ def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:
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")
- _pocket_notify("worker.failed", f"worker {worker_id} timed out and was stopped", "high")
+ log(
+ f"worker {worker_id} bg={bg_session_id} timed out — stopping session"
+ )
+ _pocket_notify(
+ "worker.failed",
+ f"worker {worker_id} timed out and was stopped",
+ "high",
+ )
try:
- subprocess.run([CLAUDE_BIN, "stop", bg_session_id],
- stdin=subprocess.DEVNULL, capture_output=True, timeout=10)
+ subprocess.run(
+ [CLAUDE_BIN, "stop", bg_session_id],
+ stdin=subprocess.DEVNULL,
+ capture_output=True,
+ timeout=10,
+ )
except Exception:
pass
killed += 1
@@ -552,13 +631,17 @@ def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:
logs = subprocess.run(
[CLAUDE_BIN, "logs", bg_session_id],
stdin=subprocess.DEVNULL,
- capture_output=True, text=True, timeout=15,
+ 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}")
+ 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:
@@ -578,7 +661,9 @@ def reap_workers(in_flight: dict[str, dict]) -> tuple[int, int]:
}
try:
RESULTS_DIR.mkdir(parents=True, exist_ok=True)
- (RESULTS_DIR / f"{task_id}.done.json").write_text(json.dumps(receipt, indent=2))
+ (RESULTS_DIR / f"{task_id}.done.json").write_text(
+ json.dumps(receipt, indent=2)
+ )
except OSError as e:
log(f"failed to write receipt for {task_id}: {e}")
log(f"reaped {worker_id} task={task_id}")