diff --git a/CLAUDE.md b/CLAUDE.md index 59d60e6..7fd20fa 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,7 +27,9 @@ Run one Deno test file or test: `deno test -A tests/integration/pg-store-read.te CI (`.github/workflows/ci.yml`, Supabase CLI pinned at 3.0.0) runs check, unit, `supabase db start`, pgTAP and integration. E2E is local only. After editing function code, restart `supabase functions serve`: a running server can -boot a half-written file and fail with BOOT_ERROR or "Unexpected eof". +boot a half-written file and fail with BOOT_ERROR or "Unexpected eof". The edge runtime's parser is older than +`deno check`'s: a regex literal holding a quote in `_shared/web-api.ts` passed `deno check` but would not boot, so serve +the function once after an edit, before a deploy. ## Architecture @@ -101,17 +103,23 @@ past the snippet), so the MCP tool's output stays as it was. boards on these tools: graph, claim_next, claims, links and search browse. It has no server code of its own. The design is in `docs/superpowers/specs/2026-11-23-blackboard-skill-design.md`. Test a change to it the way the plan `docs/superpowers/plans/2026-11-23-blackboard-skill.md` did: subagents run single cycles on a throwaway `board:toy-*` -board in `agent-memory-tool`, and the board is deleted afterwards. This repo holds the maintained copy; projects keep -only their board files. +board in `agent-memory-tool`, and the board is deleted afterwards (with `board-runner forget-usage ` for its +token usage rows). This repo holds the maintained copy; projects keep only their board files. **Board runner.** `runner/` is Python 3.11+, standard library only, with `unittest` tests (CI job `runner`). It drives a board from a TOML board file. Design: `docs/superpowers/specs/2026-11-24-board-runner-design.md`. - **Processes:** it keeps the lead as an interactive `claude` session in tmux, and starts one fresh `claude -p` worker per intent, per configured slot. -- **Board-blind:** it never touches shared memory. Sessions run the skill in `runner` mode and end with a `BOARD-WORK`, - `BOARD-LEAD` or `BOARD-START` line, which `protocol.py` parses. A Stop hook (`hook.py`) appends each lead turn to - `events.jsonl`. +- **Board-blind:** it never reads shared memory. Sessions run the skill in `runner` mode and end with a `BOARD-WORK`, + `BOARD-LEAD` or `BOARD-START` line, which `protocol.py` parses. A Stop hook (`hook.py`) appends each lead turn, and + its transcript path, to `events.jsonl`. +- **Token usage:** its one write. `usage.py` reads raw token counts (never dollars) from each worker's, one-shot's and + the lead's transcripts, and `usage_sender.py` posts them to `web-api`'s `POST /usage` with the person's + `AGENT_MEMORY_TOKEN` (from the environment or the board file's `usage_env_file`). Rows go through + `usage-pending.jsonl` and are retried until accepted; only a 400, 413 or 422 drops one, so `/usage` answers scope + errors with 404 or 403. They land in the `board_usage` table (`record_board_usage` upserts on scope, board and run + label; agents never see it). Design: `docs/superpowers/specs/2026-11-27-token-usage-design.md`. - **Scheduler:** `scheduler.py` is pure, with events in and actions out, so every scheduling rule is unit-tested with a fake clock. `cli.py` is the I/O loop around it. - **Tests:** change the scheduler test-first. Test runner-mode skill changes with subagents on toy boards, as above. @@ -123,10 +131,10 @@ controls: `docs/superpowers/specs/2026-11-26-web-ui-hints-intents-design.md`. - **Data:** the server talks only to the `web-api` Edge Function (`supabase/functions/web-api/index.ts`, routes in `_shared/web-api.ts`), never to Postgres. It adds the token from `.env` and forwards only an allow-list: GET routes - (`/api/scopes`, `boards`, `graph`, `memories`, `memories/:id`) and four POST routes (`hints`, `intents`, `amendments`, - `boards/control`), so the token never reaches the browser. It binds to `127.0.0.1` and checks `Host`. A POST must be - JSON, at most 16 KiB, and same-origin (`Sec-Fetch-Site`, or `Origin` when that is absent), else 403, and gets a 30 s - timeout (`web/lib/proxy.ts`). + (`/api/scopes`, `boards`, `graph`, `usage`, `memories`, `memories/:id`) and four POST routes (`hints`, `intents`, + `amendments`, `boards/control`; never `usage`, which only the runner writes), so the token never reaches the browser. + It binds to `127.0.0.1` and checks `Host`. A POST must be JSON, at most 16 KiB, and same-origin (`Sec-Fetch-Site`, or + `Origin` when that is absent), else 403, and gets a 30 s timeout (`web/lib/proxy.ts`). - **web-api:** read routes on `PgMemoryStore` as the caller, with the embedder built lazily so only text search loads it. Browse and ranked search from here do not count as an access. There is no history route. - **Writes:** `POST /hints`, `/intents`, `/amendments` and `/boards/control` run as the caller through `store`, `relate` @@ -141,8 +149,9 @@ controls: `docs/superpowers/specs/2026-11-26-web-ui-hints-intents-design.md`. `$BOARD_RUNNER_HOME`). The label is untrusted text from a `By:` line, so it must match the shape `new_label` makes, and the real path must stay inside that board's `workers/`. The server picks the byte offsets, so the browser never splits a UTF-8 sequence. -- **Live:** the browser polls `/graph` every `POLL_MS` (3 s) while the tab is visible and diffs each result. Hosted - `/graph` takes about 0.3 s whatever the board's size, mostly network and auth. +- **Live:** the browser polls `/graph` every `POLL_MS` (3 s) while the tab is visible and diffs each result, and + `/usage` at most every 30 s for the strip's token total and the drawer's per-run counts. Hosted `/graph` takes about + 0.3 s whatever the board's size, mostly network and auth. - **Tests:** pure helpers are split from DOM modules so they run under `deno task test:unit`. `deno task check` type-checks `web/lib` and `web/server.ts`. The browser modules are checked by hand against a toy board. The skill's paused branch, amendment fallback and `round:` wording have not been run on a real board. diff --git a/README.md b/README.md index f5060d7..7c11499 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,12 @@ separate bot member for agents, with a database rule that bot members cannot wri | `/intents` | `scope`, `board`, `text`, `produce?`, `giveUp?`, `from?` | 201 `{ memory }`; 409 `Board is not running` unless running or paused | | `/amendments` | `scope`, `board`, `text` | 201 `{ memory }`; 409 when the board is done | | `/boards/control` | `scope`, `board`, `action?` (`pause`, `resume`, `stop`, `restart`), `maxIntents?` (number or `"none"`) | 200 `{ goal }`; 409 `Board is ` when the action is not allowed from there | +| `/usage` | one run's raw token counts (sent by the board runner, never the browser) | 201 `{ id }`; 404 or 403 for a scope the token cannot write | +| `/usage/delete` | `scope`, `board` | 200 `{ deleted }` | + +`GET /usage?scope=&board=` returns a board's runs, newest first, and their summed `total`. The strip shows that total +("Tokens 12M in · 310k out", input counting cache reads and writes), and an intent's, fact's or goal's drawer shows the +runs that worked on it. These are raw token counts; no costs are stored or shown. `text` is 1 to 2,000 characters; `produce` and `giveUp` at most 500. A board or memory the caller cannot read gives a 404 that reads the same whether it is absent or unreadable. If linking fails after a memory is stored, the route diff --git a/docs/superpowers/plans/2026-11-27-token-usage.md b/docs/superpowers/plans/2026-11-27-token-usage.md new file mode 100644 index 0000000..c9715a6 --- /dev/null +++ b/docs/superpowers/plans/2026-11-27-token-usage.md @@ -0,0 +1,286 @@ +# Token usage per board run — Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** The board runner records every run's raw token counts in a new `board_usage` table through `web-api`, and the +web UI shows them per run and summed per board. + +**Architecture:** A migration adds the table, a select policy and two SECURITY DEFINER functions (record, delete). +`web-api` gains `POST /usage`, `GET /usage` and `POST /usage/delete` on a new `PgUsageStore`. The Python runner parses +token counts from transcripts (pure `usage.py`), queues rows durably and sends them with retry (`usage_sender.py`), +and wires that into worker exit, orphans, one-shots and lead turns. The browser fetches `/api/usage` and shows it. + +**Tech Stack:** Postgres migrations + pgTAP; Deno/TypeScript Edge Function; Python 3.11 stdlib + `unittest`; plain ES +modules. + +**Spec:** `docs/superpowers/specs/2026-11-27-token-usage-design.md` + +## Global Constraints + +- Raw token counts only: never store, compute or show dollars (`total_cost_usd`, `costUSD` are ignored). +- Schema change is a new migration (`supabase/migrations/20261127120000_board_usage.sql`); it must build from empty with + `supabase db reset`. SECURITY DEFINER functions use `set search_path = ''`, fully qualified names, execute revoked + from public and granted to `agent_memory_member`. +- Bind timestamps as text and cast (`${x}::text::timestamptz`). +- Errors reaching a client are user-safe: `Scope '' not found` for an absent or unreadable scope, `No write access + to scope ''` for a readable one the caller cannot write (the existing store wording); other database errors + become `Database error`. +- `role` is one of `worker`, `lead`, `start`, `release`. Unique key `(scope_id, board, run_label)`; every write is an + upsert. +- Limits: board and run label 1–100 chars, outcome ≤ 40, runner model ≤ 100, at most 20 models, model names 1–100 + chars, every count a non-negative integer ≤ 2^53 − 1. +- The runner never reads shared memory. Its only network call is posting or deleting its own usage rows. +- The runner must never crash, block the loop for more than the send timeout (5 s), or slow scheduling because usage + could not be parsed or sent. `scheduler.py` stays pure and unchanged. +- Sender: drop (with one log line) only on 400, 413, 422; keep and retry everything else, including 401, 403, 404 and + network errors; backoff doubling from 5 s to at most 300 s; queue cap 10,000 rows, oldest dropped with a warning; + each distinct failure logged once until a send succeeds. +- Credentials: `AGENT_MEMORY_TOKEN`, and `WEB_API_URL` or `AGENT_MEMORY_URL` with the path segment `agent-memory` + replaced by `web-api`; from the environment, else from the file named by the optional board-file key + `usage_env_file` (`KEY=value` lines, `#` comments, optional quotes; `~` expanded). Never log the token. +- Agents never see usage: no MCP tool changes. +- Repo is public: no project ref, company or personal names in committed files. + +## Review Focus + +1. A transcript cut off mid-line, or with no `result` line, still yields counts from its `assistant` events, each + message id counted once even when Claude Code writes several lines for one message. +2. A runner started before the hosted migration and redeploy (404 on `/usage`) keeps every row queued and sends it once + the route exists; a restart in between loses nothing (the queue file survives, rows with the same key coalesce). +3. The lead row is replaced, not added to, after each turn: many turns produce one row per lead label with growing + totals, and the pending queue holds at most one row per key. +4. A `BOARD-WORK` intent id that is not a uuid, or names a memory that is not an intent of that board, is stored as null + rather than failing the row. +5. The local web UI and the browser never post usage: `/api/usage` is GET-only through the proxy. + +--- + +### Task 1: The `board_usage` table and its functions + +**Files:** +- Create: `supabase/migrations/20261127120000_board_usage.sql` +- Test: `supabase/tests/database/18_board_usage.test.sql` + +**Interfaces:** +- Produces (SQL): + - table `agent_memory.board_usage` with columns as in the spec ("The table"), `check (role in ('worker','lead','start','release'))`, + count columns `bigint not null default 0 check (>= 0)`, `models jsonb not null default '{}'`, + unique `(scope_id, board, run_label)`, index on `(scope_id, board)`. + - policy `board_usage_select` for select to `agent_memory_member` using `agent_memory.can_read(scope_id)`; grant + select only (no insert/update/delete grants). + - `agent_memory.record_board_usage(p_scope text, p_board text, p_label text, p_role text, p_intent text, + p_outcome text, p_runner_model text, p_input bigint, p_output bigint, p_cache_read bigint, p_cache_write bigint, + p_turns int, p_duration_ms bigint, p_models jsonb, p_started text, p_ended text) returns uuid` — resolves the scope + by name (absent or unreadable → `Scope '%' not found`; not writable → `No write access to scope '%'`), validates + every limit, keeps `p_intent` only when it is a uuid naming an undeleted memory in that scope tagged + `board:` and `kind:intent`, upserts on the unique key (updating every value and `updated_at`), and + returns the row id. `recorded_by` is `agent_memory.current_member_id()`. + - `agent_memory.delete_board_usage(p_scope text, p_board text) returns int` — same scope checks, deletes the board's + rows, returns the count. + +- [ ] **Step 1: Write the pgTAP test** `18_board_usage.test.sql`, using the fixtures style of `14_memory_claims.test.sql`. + Assertions: the table, policy and both functions exist with `search_path=''`; a writer records a row and reads it + back; recording the same `(scope, board, label)` again leaves one row with the new totals; a reader without write + gets `No write access to scope '…'`; a member with no grant gets `Scope '…' not found` and selects zero rows; a + negative count, a role `boss`, 21 models and a 101-character label each raise; `p_intent` set to `'not-a-uuid'`, to + the id of a `kind:fact` memory, and to an intent of another board each store null, while a real intent of the board + is kept; `delete_board_usage` returns the count and leaves other boards' rows; a member cannot insert into the table + directly; deleting the scope cascades. +- [ ] **Step 2: Run** `supabase db reset && supabase test db` — expect `18_board_usage` to fail (no table). +- [ ] **Step 3: Write the migration.** +- [ ] **Step 4: Run** `supabase db reset && supabase test db` — all pass. +- [ ] **Step 5: Commit** `feat(db): board_usage table with record and delete functions`. + +### Task 2: `web-api` usage routes, store and backup + +**Files:** +- Create: `supabase/functions/_shared/usage-store.ts` +- Modify: `supabase/functions/_shared/web-api.ts`, `supabase/functions/web-api/index.ts`, `scripts/backup.ts` +- Test: `tests/unit/web-api.test.ts`, `tests/integration/usage-store.test.ts` (new), `tests/integration/web-api.test.ts`, + `tests/integration/backup.test.ts` + +**Interfaces:** +- Consumes: Task 1's SQL functions. +- Produces: + - `type UsageRole = 'worker' | 'lead' | 'start' | 'release'` + - `interface ModelTokens { input: number; output: number; cacheRead: number; cacheWrite: number }` + - `interface UsageRow { scope: string; board: string; runLabel: string; role: UsageRole; intentId: string | null; + outcome: string | null; runnerModel: string; tokens: ModelTokens; turns: number | null; durationMs: number | null; + models: Record; startedAt: string | null; endedAt: string | null }` + - `interface UsageStore { record(r: UsageRow): Promise; forBoard(scope: string, board: string): + Promise<{ runs: (UsageRow & { id: string; recordedBy: string; updatedAt: string })[]; total: ModelTokens; + more: boolean }>; deleteBoard(scope: string, board: string): Promise }`, implemented by + `PgUsageStore(sql, memberId)` with the `run`/`withMember` error handling of `PgThreadStore`. `forBoard` returns at + most 1000 runs, newest `updated_at` first, and `total` summed in SQL over all the board's rows. + - `routeWebApi(req, store, usage: UsageStore)`: `POST /usage` (JSON body = `UsageRow`, validated in TS with the + Global Constraints limits → 400 on any violation, `{ id }` on success), `GET /usage?scope=&board=` (both required), + `POST /usage/delete` (`{ scope, board }` → `{ deleted }`). Same JSON-body and size rules as the other write routes. + +- [ ] **Step 1: Unit tests** in `tests/unit/web-api.test.ts` with a fake `UsageStore`: `POST /usage` passes a valid row + through unchanged; rejects a negative count, `role: 'boss'`, a 21-model map, a missing `runLabel` and a fractional + count with 400; `GET /usage` without `board` is 400 and with both passes them through; `POST /usage/delete` returns + `{ deleted }`; a `GET` to `/usage/delete` is 405. Update the existing helpers to pass a fake `UsageStore`. +- [ ] **Step 2: Integration tests** in `tests/integration/usage-store.test.ts`: record, upsert (one row, new totals), + `forBoard` totals across three rows, `deleteBoard` count, reader sees, `none` member gets `Scope '…' not found`, + `ro` gets `No write access…`. Add one `web-api` integration test that posts a row and reads it back. Add `board_usage` + to `backup.test.ts`'s round-trip. +- [ ] **Step 3: Run** `deno task test:unit` and the three integration files — expect failures. +- [ ] **Step 4: Implement** the store, the routes, `index.ts` wiring, and backup/restore of `board_usage` (rows by scope + name and `recorded_by` member name, timestamps as text, following `memory_claims`). +- [ ] **Step 5: Run** `deno task check && deno task test:unit && deno task test:int` — pass. +- [ ] **Step 6: Commit** `feat(web-api): record, read and delete board usage`. + +### Task 3: Runner — token counts from transcripts + +**Files:** +- Create: `runner/board_runner/usage.py` +- Test: `runner/tests/test_usage.py`, fixtures under `runner/tests/fixtures/usage/` + +**Interfaces:** +- Produces: + - `@dataclass(frozen=True) class Tokens: input: int = 0; output: int = 0; cache_read: int = 0; cache_write: int = 0` + with `__add__`. + - `@dataclass(frozen=True) class RunUsage: tokens: Tokens; models: dict[str, Tokens]; turns: int | None; + duration_ms: int | None` + - `def run_usage(stdout: str) -> RunUsage | None` — from `claude -p` output (`json` or `stream-json`): the last + `result` object's `usage` (`input_tokens`, `output_tokens`, `cache_read_input_tokens`, + `cache_creation_input_tokens`), `modelUsage` (`inputTokens`, `outputTokens`, `cacheReadInputTokens`, + `cacheCreationInputTokens`), `num_turns`, `duration_ms`. Without a result object, the sum of `assistant` events' + `message.usage`, once per `message.id`, models keyed by `message.model`, `turns`/`duration_ms` None. None when + nothing is found. + - `def session_usage(transcript: Path) -> RunUsage` — an interactive session's transcript plus every + `/subagents/*.jsonl`: assistant `message.usage` once per `message.id` (per file), + models by `message.model`, `turns` = number of distinct assistant message ids in the main file, `duration_ms` None. + Missing or unreadable files count as zero. + - `def usage_row(*, scope, board, run_label, role, intent_id, outcome, runner_model, usage: RunUsage, started_at, + ended_at) -> dict` — the JSON body of `POST /usage` (camelCase names of Task 2's `UsageRow`; timestamps ISO-8601 UTC + strings or None). + +- [ ] **Step 1: Tests** with fixtures cut from real transcripts (no names or secrets): a stream-json worker with a result + line (counts equal its `usage`, models from `modelUsage`, turns and duration set); the same file with the result + line removed and the last line cut mid-JSON (counts are the per-message sum, a message id repeated over three lines + counted once); an old `.json` single-object output; a non-Claude model (`glm-5.3:cloud`) whose `costUSD` is ignored; + an empty file → None; a session transcript with one subagent file → sums both; `usage_row` round-trips through + `json.dumps` with exactly Task 2's keys. +- [ ] **Step 2: Run** `cd runner && python3 -m unittest tests.test_usage` — fail. +- [ ] **Step 3: Implement** `usage.py`. +- [ ] **Step 4: Run** `cd runner && python3 -m unittest` — pass. +- [ ] **Step 5: Commit** `feat(runner): read token counts from transcripts`. + +### Task 4: Runner — durable queue and sender + +**Files:** +- Create: `runner/board_runner/usage_sender.py` +- Modify: `runner/board_runner/board_file.py` (optional top-level `usage_env_file`, a path string) +- Test: `runner/tests/test_usage_sender.py`, `runner/tests/test_board_file.py` + +**Interfaces:** +- Produces: + - `def usage_config(env: Mapping[str, str], env_file: Path | None) -> tuple[str, str] | None` — `(usage_url, + token)`, where `usage_url` ends in `/usage`; None when no token or URL. + - `class UsageQueue(path: Path, post: Callable[[str, dict], int], clock: Callable[[], float], log: Callable[[str, + bool], None])` with `add(row: dict) -> None` (appends to the file, replacing a pending row with the same + `(scope, board, runLabel)`), `flush(now: float) -> None` (sends due rows in order, stops at the first failure), + `pending() -> int`. `post(path, body)` returns the HTTP status, or 0 for a network error or timeout; `path` is + `"/usage"` or `"/usage/delete"`. + - `def http_post(base_url: str, token: str, timeout: float = 5.0) -> Callable[[str, dict], int]` — urllib, JSON, + `Authorization: Bearer`. + - `BoardFile.usage_env_file: Path | None`. + +- [ ] **Step 1: Tests** with a fake `post` and clock: a 200 removes the row from the file; 400, 413, 422 drop it with one + log line; 401, 403, 404, 500 and 0 keep it, and the next attempt waits 5, 10, 20 … s capped at 300; a later 200 + resets the backoff and sends the rest in order; the same failure logged once across many retries; adding a row with + an existing key leaves one pending row with the new totals; 10,001 adds keep 10,000 with a warning; a new + `UsageQueue` on the same file sees the rows a previous one left; a corrupt line in the file is skipped. + `usage_config`: env wins over the file; `AGENT_MEMORY_URL` `…/functions/v1/agent-memory` becomes `…/functions/v1/web-api/usage`; + quotes and comments in the env file; no token → None. Board file: `usage_env_file = "~/x.env"` loads expanded; absent + → None; a non-string → `BoardFileError`. +- [ ] **Step 2: Run** `cd runner && python3 -m unittest tests.test_usage_sender tests.test_board_file` — fail. +- [ ] **Step 3: Implement.** +- [ ] **Step 4: Run** `cd runner && python3 -m unittest` — pass. +- [ ] **Step 5: Commit** `feat(runner): queue and send usage rows with retry`. + +### Task 5: Runner — record every run + +**Files:** +- Modify: `runner/board_runner/cli.py`, `runner/board_runner/hook.py`, `runner/board_runner/launch.py` (labels for + one-shots), `runner/README.md` +- Test: `runner/tests/test_cli.py`, `runner/tests/test_hook.py` + +**Interfaces:** +- Consumes: Task 3's `run_usage`, `session_usage`, `usage_row`; Task 4's `UsageQueue`, `usage_config`, `http_post`. +- Produces: + - `Runner(..., usage_post: Callable[[str, dict], int] | None = None, env: Mapping[str, str] = os.environ)`: when + `usage_post` is None it is built from `usage_config`; with no config, one warning at start ("token usage is queued + but not sent: set AGENT_MEMORY_TOKEN and WEB_API_URL, or usage_env_file") and rows are still queued. The queue file + is `//usage-pending.jsonl`. `flush` runs once per loop tick and once at shutdown. + - Worker exit (`_poll_workers`): a `worker` row from `run_usage` of the transcript, `intent_id` = the parsed + `BOARD-WORK` intent, `outcome` = `fact ` / `partial` / `none` / `stopped` / `lost`, `runner_model` = the + slot's model, `started_at` from the state's `started_at`, `ended_at` now. No row when `run_usage` is None. + - `state.json` workers entries gain `path` and `model`; `_kill_orphans` queues a row for each orphan from its + transcript (`outcome` from `parse_work(claude_result_text(...))`, else `lost`). + - `start` one-shot: a `start` row from its stdout, label `new_label(board, "start")`. Release one-shots write stdout to + `//oneshots/