refactor(review): always render usage; drop report_usage flag

This commit is contained in:
claude
2026-08-22 01:18:34 +00:00
parent e1b74d982d
commit 86352a3771
3 changed files with 53 additions and 37 deletions
+12 -13
View File
@@ -1998,7 +1998,6 @@ def review_pr(
model: str,
max_tokens: int = 8000,
max_chars: int = 150000,
report_usage: bool = False,
base_ref: str = "",
) -> bool:
"""Run one review and post it as `pragent-bot`.
@@ -2013,10 +2012,11 @@ def review_pr(
from the PR head) so a PR cannot ship its own reviewer instructions; empty
means "the repo's default branch".
`report_usage`: when True (PR carries the `AI-USAGE` label), the opencode
engine's measured token/cost usage is rendered as a `## 🔋 AI usage` section
on the review body and an attributed `🪙 ~N tok` line on each inline
comment. No-op on the ollama fallback (no usage available).
The opencode engine's measured token/cost usage is always rendered as a
`## 🔋 AI usage` section on the review body and an attributed `🪙 ~N tok`
line on each inline comment when usage data is available (i.e. when the
opencode subprocess returned a `usage` dict). No-op on the ollama fallback
(no usage available — `usage` is None).
Returns True on success (including a deliberate skip), False on failure
(failure note posted when possible). Never raises — fail-open by design.
@@ -2100,16 +2100,16 @@ def review_pr(
review_summary, findings, summary_changes, risks, _walkthrough, _risk_verdict, _test_coverage = parse_review_output(stdout)
if not findings and not review_summary:
# The findings JSON was missing or malformed. Don't discard the
# run: salvage the prose, keep the usage report (the label asked
# for it, and the tokens were spent either way), and log enough
# of the raw output to diagnose why the agent went off-format.
# run: salvage the prose, keep the usage report (the tokens were
# spent either way), and log enough of the raw output to
# diagnose why the agent went off-format.
print(
f"pragent: {repo}#{index} sha={sha[:8]} unparseable output "
f"({len(stdout)} chars); tail: {stdout[-600:]!r}",
file=sys.stderr, flush=True,
)
salvaged = salvage_summary(stdout)
usage_section = _render_collapsible_usage(usage, model, config=config) if report_usage else ""
usage_section = _render_collapsible_usage(usage, model, config=config) if usage else ""
post_review(api, repo, index, token, format_review_body(
salvaged or "AI review produced no parseable output.",
model, sha, usage_section=usage_section))
@@ -2154,11 +2154,10 @@ def review_pr(
)
# Compute attribution so inline comments + the table can show per-comment
# estimates. Only meaningful when we have measured usage AND the PR asked
# for it.
if report_usage and usage and usage.get("output"):
# estimates. Only meaningful when we have measured usage.
if usage and usage.get("output"):
compute_attribution(findings, usage["output"])
usage_section = _render_collapsible_usage(usage, model, config=config) if report_usage else ""
usage_section = _render_collapsible_usage(usage, model, config=config) if usage else ""
# Anchor against the RAW diff, never the compressed one. Compression
# drops context lines, so a finding on a line that survived in the file