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(gh-quota): PreToolUse guard + orphan-killer daemon for gh REST quota protection (#323)

Aurora Capitalc734612a940a
diff --git a/claude-ops/hooks/gh-watch-guard.sh b/claude-ops/hooks/gh-watch-guard.sh
new file mode 100755
index 00000000..bbee0aad
--- /dev/null
+++ b/claude-ops/hooks/gh-watch-guard.sh
@@ -0,0 +1,78 @@
+#!/bin/bash
+# PreToolUse Bash hook — blocks rate-limit-burning gh patterns.
+#
+# Allow: exit 0 (no stdout JSON). Deny: emit hookSpecificOutput with permissionDecision deny
+# on stdout (same contract as bin/ops-prevent-secret-commit and docs/safety-hooks.md), then exit 0.
+# Pairs with the persistent gh-orphan-killer.sh watchdog.
+#
+# Source of truth: ~/Projects/claude-ops/claude-ops/hooks/gh-watch-guard.sh
+# Plugin cache copy (auto-installed via plugin update) is read-only.
+
+emit_pre_tool_deny() {
+ python3 -c '
+import json, sys
+reason = sys.stdin.read().rstrip("\n")
+print(json.dumps({
+ "hookSpecificOutput": {
+ "hookEventName": "PreToolUse",
+ "permissionDecision": "deny",
+ "permissionDecisionReason": reason
+ }
+}))
+'
+}
+
+raw="${TOOL_INPUT:-}"
+if [ -z "$raw" ]; then
+ raw=$(cat) || true
+fi
+CMD=$(printf '%s' "$raw" | jq -r '(.tool_input.command // .command // empty)' 2>/dev/null || true)
+# Collapse newlines so --watch / tight-loop patterns cannot be evaded by splitting lines.
+CMD_ONELINE=$(printf '%s' "$CMD" | tr '\n\r' ' ')
+
+# Fast path: not a gh command, exit immediately
+case "$CMD" in
+ *gh\ *|*gh-*) ;;
+ *) exit 0 ;;
+esac
+
+# --- Pattern 1: --watch flag on gh pr checks / gh run watch ---
+# `gh pr checks <PR> --watch` and `gh run watch` poll every 2-5s.
+# 5000 REST/hr ÷ 2s = exhausted in ~3 hours of one process. Sam saw this in production.
+if echo "$CMD_ONELINE" | grep -qE 'gh[[:space:]]+pr[[:space:]]+checks[[:space:]]+[^|]*--watch|gh[[:space:]]+run[[:space:]]+watch'; then
+ emit_pre_tool_deny <<'EOF'
+BLOCKED: `gh ... --watch` polls every 2-5s and exhausts the 5000/hr REST quota.
+
+Use the Monitor tool with an `until` poll loop at ≥25s instead:
+
+ prev=""
+ while true; do
+ s=$(gh pr view <PR> --repo <REPO> --json mergeStateStatus,statusCheckRollup)
+ state=$(echo "$s" | jq -r .mergeStateStatus)
+ [ "$state" = "CLEAN" ] || [ "$state" = "UNSTABLE" ] && { echo READY; break; }
+ sleep 30
+ done
+
+For multi-PR watching prefer GraphQL (separate 5000/hr bucket) — single query, multiple PRs.
+
+Source: ~/Projects/claude-ops/claude-ops/hooks/gh-watch-guard.sh
+EOF
+ exit 0
+fi
+
+# --- Pattern 2: tight gh loops (single-digit sleep = under 10s) ---
+# Catches: `while true; do gh ...; sleep 5; done` style polls (not sleep 10+).
+if echo "$CMD_ONELINE" | grep -qE '(^|[^[:alnum:]_])(while|until|for)([^[:alnum:]_]|$).*gh[[:space:]]+(api|pr|run|issue)' && \
+ echo "$CMD_ONELINE" | grep -qE 'sleep[[:space:]]+[0-9]([[:space:];]|$)'; then
+ emit_pre_tool_deny <<'EOF'
+BLOCKED: tight gh polling loop (sleep < 10s) detected.
+
+The 5000/hr REST quota is shared across this session, background daemons, the overnight
+sync cron, and any other gh process. A loop at sleep 5 burns 720 calls/hr — easy to OOM-cache.
+
+Bump to `sleep 30` (or use Monitor tool — handles ≥25s naturally).
+EOF
+ exit 0
+fi
+
+exit 0
diff --git a/claude-ops/scripts/gh-orphan-killer.sh b/claude-ops/scripts/gh-orphan-killer.sh
new file mode 100755
index 00000000..9d0e7ffe
--- /dev/null
+++ b/claude-ops/scripts/gh-orphan-killer.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+# Kills orphan `gh pr checks ... --watch` after 5 min; `gh run watch` only after
+# 2000s so deploy monitors (default watcher timeout 1800s in ops-deploy-monitor.sh)
+# are not SIGKILL'd mid-watch. Runs every 60s while active. Idempotent.
+#
+# Triggered by: SessionStart hook (foreground guarded by pidfile to avoid duplicates).
+#
+# Source of truth: ~/Projects/claude-ops/claude-ops/scripts/gh-orphan-killer.sh
+
+set -euo pipefail
+
+PIDFILE="${TMPDIR:-/tmp}/gh-orphan-killer.pid"
+LOG="${HOME}/.claude/logs/gh-orphan-killer.log"
+mkdir -p "$(dirname "$LOG")"
+
+# Singleton: if another instance is running with a fresh pidfile, exit
+if [ -f "$PIDFILE" ]; then
+ prev_pid=$(cat "$PIDFILE" 2>/dev/null || echo "")
+ if [ -n "$prev_pid" ] && kill -0 "$prev_pid" 2>/dev/null; then
+ exit 0
+ fi
+fi
+echo $$ > "$PIDFILE"
+trap 'rm -f "$PIDFILE"' EXIT
+
+PR_CHECKS_WATCH_MAX_AGE=300
+# Above ops-deploy-monitor default watcher_timeout_seconds (1800) with headroom
+GH_RUN_WATCH_MAX_AGE=2000
+
+while true; do
+ # `ps -eo pid,etimes,command` — etimes = elapsed seconds since process start
+ killed=0
+ while read -r pid etimes max_age command; do
+ [ -z "$pid" ] && continue
+ [[ "$etimes" =~ ^[0-9]+$ ]] || continue
+ [[ "$max_age" =~ ^[0-9]+$ ]] || continue
+ if [ "$etimes" -gt "$max_age" ]; then
+ echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) killing orphan pid=$pid age=${etimes}s max=${max_age}s: $command" >> "$LOG"
+ kill -9 "$pid" 2>/dev/null || true
+ killed=$((killed + 1))
+ fi
+ done < <(ps -eo pid,etimes,command 2>/dev/null | awk -v prmax="$PR_CHECKS_WATCH_MAX_AGE" -v runmax="$GH_RUN_WATCH_MAX_AGE" '
+ /gh pr checks .*--watch/ && !/awk|grep/ { print $1, $2, prmax, substr($0, index($0,$3)) }
+ /gh run watch/ && !/awk|grep/ { print $1, $2, runmax, substr($0, index($0,$3)) }
+ ')
+
+ [ "$killed" -gt 0 ] && echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) killed $killed orphan(s)" >> "$LOG"
+
+ sleep 60
+done