feat: token-usage reporting (AI-USAGE label) #6

Merged
gitea_admin merged 3 commits from feat/token-usage into main 2026-08-18 04:32:19 +00:00

3 Commits

Author SHA1 Message Date
Marcos d3dcd9c8e5 chore: trigger regression review (AI-USAGE label removed) 2026-08-18 04:23:01 +00:00
Marcos 513de0e3a8 docs(pilot): document AI-USAGE label, token attribution, webhook denylist
- new "AI-USAGE label" section: what gets reported, the attributed-vs-measured
  distinction, PRAGENT_USAGE_ALWAYS, regression-clean without the label.
- new "Webhook fires on any PR update (except closed)" section: denylist
  semantics + why the AI-REVIEW gate + sha dedupe keep it safe.
- arch diagram gate updated (allowlist → denylist; AI-USAGE side-gate).
- opencode engine step 4: --format json + parse_opencode_events → (text, usage).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-18 04:17:32 +00:00
Marcos 087834565d feat(pilot): token-usage reporting gated by AI-USAGE label
Add per-review + per-comment token accounting, surfaced only when a PR carries
the new AI-USAGE label (on top of the existing AI-REVIEW trigger).

opencode_review:
- run_opencode now uses `--format json`; parse_opencode_events reconstructs the
  assistant text from `text` events and sums tokens/cost/steps from every
  `step_finish` event (tolerant of noise / missing fields).
- run() measures duration_s around the opencode call and returns (text, usage).
- changed_files(diff) extracts the `+++ b/` paths; the brief now lists them
  under a "Changed files" focus block so the agent grounds findings in the
  diff's neighbourhood instead of unbounded whole-repo walks.

ai_review:
- format_usage_section renders a `## AI usage` block: measured totals
  (in/out/reasoning/cache/cost/steps/duration), the whole-repo scope note, and
  an attributed per-finding table. Per-comment counts are output tokens split by
  each finding's body weight — labelled "attributed" since one model pass
  produces all findings.
- inline_comment_body appends `🪙 ~N tok (X% · attributed output)` when
  attribution is present.
- review_pr gains report_usage; compute_attribution stashes _tok_attrib/_tok_pct.
- format_review_body inserts the usage section between summary and findings.

webhook_server:
- Fire on every pull_request action except `closed` (denylist, was an allowlist)
  — the AI-REVIEW gate + sha dedupe keep this safe.
- AI-USAGE label detection + PRAGENT_USAGE_ALWAYS env drive report_usage.

.opencode factory + review-methodology skill: new "Ground findings in context"
step — read callers/imports/sibling functions per changed file (1-3 files per
finding), no unbounded walks.

Tests: parse_opencode_events (text+usage sum, malformed tolerance, none-usage),
changed_files, compute_attribution math, inline 🪙 line, format_usage_section
totals/table/cost, format_review_body ordering. 68 passing.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-18 04:15:11 +00:00