|
| 1 | +# Architecture |
| 2 | + |
| 3 | +approval-hub is a local HTTP broker daemon plus a Bubble Tea TUI client, |
| 4 | +both packaged in a single Go binary (`approval-hub`). A thin Claude Code |
| 5 | +plugin distributed separately wires each Claude Code session's `PreToolUse` |
| 6 | +hook to the broker via `type: "http"`. |
| 7 | + |
| 8 | +## Process layout |
| 9 | + |
| 10 | +```text |
| 11 | ++------------------+ +------------------+ |
| 12 | +| claude session A | | claude session B | |
| 13 | +| PreToolUse:http | | PreToolUse:http | |
| 14 | ++--------+---------+ +--------+---------+ |
| 15 | + | | |
| 16 | + | POST /decide | POST /decide |
| 17 | + | Bearer <token> | Bearer <token> |
| 18 | + +-------------+-------------+ |
| 19 | + v |
| 20 | + +---------------------+ |
| 21 | + | broker daemon | |
| 22 | + | 127.0.0.1:17456 | |
| 23 | + | | |
| 24 | + | - decision engine | |
| 25 | + | - matcher store | |
| 26 | + | - pending queue | |
| 27 | + | - SSE /events | |
| 28 | + +---------+-----------+ |
| 29 | + | SSE /events |
| 30 | + v |
| 31 | + +---------------------+ |
| 32 | + | TUI client | |
| 33 | + | approval-hub attach| |
| 34 | + +---------------------+ |
| 35 | +``` |
| 36 | + |
| 37 | +Three processes are loosely coupled over HTTP and SSE. If the TUI or the |
| 38 | +daemon dies, Claude sessions keep working: the hook receives a non-2xx / |
| 39 | +connection error and Claude Code falls back to its built-in interactive |
| 40 | +prompt. This is guaranteed by Claude Code's hook semantics. |
| 41 | + |
| 42 | +## Broker daemon |
| 43 | + |
| 44 | +Single goroutine-safe HTTP server on `127.0.0.1`. |
| 45 | + |
| 46 | +- `POST /decide` — PreToolUse entry. Generates a permission-rule-syntax |
| 47 | + matcher (`Bash(npm test:*)`, `Edit(/path/to/file)`, ...) from the tool |
| 48 | + input, looks it up in the matcher store. Cache hit returns immediately; |
| 49 | + cache miss enqueues a pending request and waits up to |
| 50 | + `ui_timeout_seconds` for a TUI decision, after which it returns `defer`. |
| 51 | +- `GET /events` — Server-Sent Events stream. Publishes |
| 52 | + `pending_added` / `pending_resolved` / `pending_expired` / `rule_added` |
| 53 | + / `rule_revoked`. Slow subscribers drop events when their buffer fills. |
| 54 | +- `GET /pending`, `POST /pending/{id}/resolve` — list and resolve. |
| 55 | +- `GET /rules`, `DELETE /rules/{id}` — inspect and tombstone matchers. |
| 56 | +- `POST /rotate-token` — generate a new bearer token and persist it. |
| 57 | + |
| 58 | +Bearer-token middleware in front of every route. Rate limiter uses a |
| 59 | +per-second token bucket sized by `rate_limit_per_second`. |
| 60 | + |
| 61 | +## TUI client |
| 62 | + |
| 63 | +Bubble Tea Model with an SSE consumer goroutine. Reconnects with |
| 64 | +exponential backoff up to 30 s. Decisions go back to the broker as |
| 65 | +`POST /pending/{id}/resolve` with `scope` of `once` (no learning), |
| 66 | +`persist` (matcher appended to the store), or `ttl` (matcher with an |
| 67 | +`expires_at`). |
| 68 | + |
| 69 | +Session IDs are SHA-1 hashed to one of 8 ANSI colors so requests from |
| 70 | +the same session group visually. |
| 71 | + |
| 72 | +## Matcher store |
| 73 | + |
| 74 | +Append-only JSONL log at `${data_dir}/learned-rules.jsonl`. Each line is |
| 75 | +either a `rule_added` record (with optional `expires_at`) or a |
| 76 | +`rule_revoked` tombstone referencing a previous rule's ID. The broker |
| 77 | +reconstructs an in-memory map on startup. |
| 78 | + |
| 79 | +Matcher strings follow Claude Code's `if` field syntax so the rule set |
| 80 | +can be exported to `settings.json`'s `permissions.allow` list when the |
| 81 | +broker is uninstalled. |
| 82 | + |
| 83 | +## Failure modes |
| 84 | + |
| 85 | +- **broker not running** — hook receives connection refused → Claude |
| 86 | + Code falls back to manual prompt |
| 87 | +- **broker crashes mid-pending** — hook hits its own `timeout` → |
| 88 | + Claude Code falls back |
| 89 | +- **TUI client crashes** — broker keeps queueing pending; next attach |
| 90 | + replays via `GET /pending` and SSE |
| 91 | +- **pending queue full** — broker returns 503; Claude Code falls back |
| 92 | +- **disk full (store)** — `AddRule` errors; resolve still returns the |
| 93 | + decision in-memory |
| 94 | + |
| 95 | +## Configuration |
| 96 | + |
| 97 | +`config.json` in the OS-standard data directory |
| 98 | +(`~/Library/Application Support/approval-hub` on macOS, |
| 99 | +`~/.config/approval-hub` on Linux, or `$APPROVAL_HUB_DATA`). |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "port": 17456, |
| 104 | + "token": "ahub_<64hex>", |
| 105 | + "ui_timeout_seconds": 60, |
| 106 | + "max_pending_requests": 100, |
| 107 | + "rate_limit_per_second": 50 |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +File permissions are `0600`. The token is embedded into the Claude Code |
| 112 | +plugin's `hooks/hooks.json` by the `install` skill. |
0 commit comments