feat(webui): the Triage host counts tokens per turn and holds the daily cap (#1873) #1992

Merged
jason.woltje merged 2 commits from triage-cap into refactor 2026-11-18 20:33:46 +00:00
Owner

What changes

The Triage host counts tokens per turn and holds the owner's daily cap. Nothing passes it a route yet: the Console still sends route: null, so Triage still does not start.

  • Count. Each turn appends one line to <dataRoot>/triage/usage.jsonl: {at, run, turn, status, input, output, cacheRead, cacheWrite}. The file is append only, 0600, one link, owned by the running user and at most 32 MiB. Anything else refuses. A torn line is refused, logged once and counted as one unknown turn.
  • Decide. The cap goes through the daily-quota plugin's mayLaunch and report unchanged, with the organization limits shape. No new counting rule.
  • Before each start and each queued message the host checks the cap. Over it, the answer is triage-over-cap with the next reset time, and nothing starts. The check first waits for any turn still being counted.
  • In flight. A turn that crosses the cap finishes. Messages still queued each get a host line and leave the queue. An idle session over the cap ends at its next receive.
  • Restart. The host sums the file again from the window start. Nothing is kept in memory across starts.
  • Reset. 00:00 in the zone stored with the route, DST days included.
  • Fail closed. A line that cannot be written makes the host refuse every message until it restarts.
  • Read. GET /api/triage adds usageToday, cap and resetsAt, or says "not set" or "not read". No path, key, model or env value. The Triage drawer shows one line from that read.

Jason approved the host count, the reset rule and the three read fields on 2026-11-18 ("route as recommended").

How

  • packages/webui/src/triage-usage.mjs (new), triage-host.mjs, triage-http.mjs, public/display.js, public/shell.css, and tests.
  • No bus schema, vocabulary, policy or new HTTP route. cli.mjs is unchanged.

Tests

  • triage-usage, triage-host, triage-http, triage-drawer-browser. Mutants: 46 of 46 red. The mutant runs found one real gap, a message sent right after a mid-turn kill starting before the killed turn was counted. It is fixed.
  • Lane gate on 57af33630: 216 files, 1936 pass, 0 fail, 4 skipped (org-quick-live).
  • Lead check on 57af33630 in a clean environment: 15 test files (the seven webui triage test files and eight browser files that load display.js), 238 pass, 0 fail.

Review

One fresh claude-opus-5-5 reviewer: changes, one P2 (a repeated usage.begin keeping the first baseline had no test) and five P3s. It found no way past the cap and no lost count. The P2 is fixed in the second commit. No reviewer read that commit.

Left for later

  • The wiring change must close this one. In a long session the transcript reader passes its bounds, every turn then reads unknown, unknown turns add 0, and the cap stops firing. The drawer shows the count of unknown turns. Organizations read usage the same way.
  • A hard kill of the Console mid-turn loses that turn's line.
  • After a failed write and a restart, the lost turn is not counted.
  • The usage file only grows. Near 32 MiB Triage refuses until it is trimmed. A pruning verb is a separate change.
  • The mode check lets setuid, setgid and sticky bits through, as the route store does.
## What changes The Triage host counts tokens per turn and holds the owner's daily cap. Nothing passes it a route yet: the Console still sends `route: null`, so Triage still does not start. - **Count.** Each turn appends one line to `<dataRoot>/triage/usage.jsonl`: `{at, run, turn, status, input, output, cacheRead, cacheWrite}`. The file is append only, 0600, one link, owned by the running user and at most 32 MiB. Anything else refuses. A torn line is refused, logged once and counted as one unknown turn. - **Decide.** The cap goes through the daily-quota plugin's `mayLaunch` and `report` unchanged, with the organization limits shape. No new counting rule. - **Before each start and each queued message** the host checks the cap. Over it, the answer is `triage-over-cap` with the next reset time, and nothing starts. The check first waits for any turn still being counted. - **In flight.** A turn that crosses the cap finishes. Messages still queued each get a host line and leave the queue. An idle session over the cap ends at its next receive. - **Restart.** The host sums the file again from the window start. Nothing is kept in memory across starts. - **Reset.** 00:00 in the zone stored with the route, DST days included. - **Fail closed.** A line that cannot be written makes the host refuse every message until it restarts. - **Read.** GET /api/triage adds `usageToday`, `cap` and `resetsAt`, or says "not set" or "not read". No path, key, model or env value. The Triage drawer shows one line from that read. Jason approved the host count, the reset rule and the three read fields on 2026-11-18 ("route as recommended"). ## How - `packages/webui/src/triage-usage.mjs` (new), `triage-host.mjs`, `triage-http.mjs`, `public/display.js`, `public/shell.css`, and tests. - No bus schema, vocabulary, policy or new HTTP route. `cli.mjs` is unchanged. ## Tests - `triage-usage`, `triage-host`, `triage-http`, `triage-drawer-browser`. Mutants: 46 of 46 red. The mutant runs found one real gap, a message sent right after a mid-turn kill starting before the killed turn was counted. It is fixed. - Lane gate on 57af33630: 216 files, 1936 pass, 0 fail, 4 skipped (`org-quick-live`). - Lead check on 57af33630 in a clean environment: 15 test files (the seven webui triage test files and eight browser files that load display.js), 238 pass, 0 fail. ## Review One fresh claude-opus-5-5 reviewer: changes, one P2 (a repeated usage.begin keeping the first baseline had no test) and five P3s. It found no way past the cap and no lost count. The P2 is fixed in the second commit. No reviewer read that commit. ## Left for later - **The wiring change must close this one.** In a long session the transcript reader passes its bounds, every turn then reads unknown, unknown turns add 0, and the cap stops firing. The drawer shows the count of unknown turns. Organizations read usage the same way. - A hard kill of the Console mid-turn loses that turn's line. - After a failed write and a restart, the lost turn is not counted. - The usage file only grows. Near 32 MiB Triage refuses until it is trimmed. A pruning verb is a separate change. - The mode check lets setuid, setgid and sticky bits through, as the route store does.
jason.woltje added 2 commits 2026-11-18 20:33:31 +00:00
Each turn's usage goes to <dataRoot>/triage/usage.jsonl, one line per
usage.end, 0600 and owned by the running user; a link, wrong mode or
owner, or a non-file refuses. The host holds the stored cap through
daily-quota mayLaunch and report before each start and each queued
message, lets a crossing turn finish, drains the queue with a host
line, sums the file again after a restart (a torn line counts as an
unknown turn), and GET /api/triage adds usageToday, cap and resetsAt.
With no route stored the fields say "not set". cli.mjs keeps route
null. (#1873, lane triage-cap)
jason.woltje merged commit 57af33630d into refactor 2026-11-18 20:33:46 +00:00
Sign in to join this conversation.