feat(ai_review): render humanized token counts in usage + inline

This commit is contained in:
claude
2026-08-21 23:54:10 +00:00
parent 3bcf825104
commit 2cf4bdbfe8
2 changed files with 31 additions and 5 deletions
+4 -2
View File
@@ -1033,7 +1033,7 @@ def inline_comment_body(f: dict) -> str:
tok = f.get("_tok_attrib") tok = f.get("_tok_attrib")
if tok is not None: if tok is not None:
pct = (f.get("_tok_pct", 0.0) or 0.0) * 100 pct = (f.get("_tok_pct", 0.0) or 0.0) * 100
body += f"\n\n🪙 ~{tok} tok ({pct:.0f}% · attributed output)" body += f"\n\n🪙 ~{fmt_tokens(tok)} tok ({pct:.0f}% · attributed output)"
return body return body
@@ -1122,7 +1122,9 @@ def _render_collapsible_usage(usage: dict | None, model: str, config: dict | Non
"<summary>🔋 AI Usage & Run Details</summary>", "<summary>🔋 AI Usage & Run Details</summary>",
"", "",
f"- **Model / Engine**: `{model}` · opencode · {usage.get('steps', 0)} steps · {dur_s}", f"- **Model / Engine**: `{model}` · opencode · {usage.get('steps', 0)} steps · {dur_s}",
f"- **Total Tokens**: {in_tok} in / {out_tok} out ({reason_tok} reasoning, cache {cache_r} read / {cache_w} write, {total} total)", f"- **Total Tokens**: {fmt_tokens(in_tok)} in / {fmt_tokens(out_tok)} out "
f"({fmt_tokens(reason_tok)} reasoning, cache {fmt_tokens(cache_r)} read / "
f"{fmt_tokens(cache_w)} write, {fmt_tokens(total)} total)",
f"- **Est. cost on {eq_label}**: {eq_s}{eq_note}", f"- **Est. cost on {eq_label}**: {eq_s}{eq_note}",
f"- **Actual**: {actual_s}{actual_note}", f"- **Actual**: {actual_s}{actual_note}",
f"- **Scope**: {scope}", f"- **Scope**: {scope}",
+27 -3
View File
@@ -335,7 +335,7 @@ def test_inline_comment_body_with_token_attribution():
"fix": "f", "suggestion": "", "reference": "", "fix": "f", "suggestion": "", "reference": "",
"_tok_attrib": 1234, "_tok_pct": 0.30} "_tok_attrib": 1234, "_tok_pct": 0.30}
body = inline_comment_body(f) body = inline_comment_body(f)
assert "🪙 ~1234 tok" in body assert "🪙 ~1,234 (1.2K) tok" in body
assert "30%" in body assert "30%" in body
assert "attributed output" in body assert "attributed output" in body
@@ -757,7 +757,7 @@ def test_render_collapsible_usage_renders_totals():
assert "`glm-5.2:cloud`" in sec assert "`glm-5.2:cloud`" in sec
assert "7 steps" in sec assert "7 steps" in sec
assert "142.0s" in sec assert "142.0s" in sec
assert "18420 in / 612 out" in sec and "19032 total" in sec assert "18,420 (18.4K) in / 612 out" in sec and "19,032 (19.0K) total" in sec
assert "$0.00" in sec assert "$0.00" in sec
assert "Whole-repo checkout" in sec assert "Whole-repo checkout" in sec
assert "attributed" in sec assert "attributed" in sec
@@ -824,7 +824,7 @@ def test_render_collapsible_usage_contains_details():
assert "<summary>🔋 AI Usage & Run Details</summary>" in block assert "<summary>🔋 AI Usage & Run Details</summary>" in block
assert "</details>" in block assert "</details>" in block
assert "glm-5.2:cloud" in block assert "glm-5.2:cloud" in block
assert "1000 in / 200 out" in block assert "1,000 (1.0K) in / 200 out" in block
def test_render_collapsible_usage_empty_when_no_usage(): def test_render_collapsible_usage_empty_when_no_usage():
@@ -1668,3 +1668,27 @@ def test_fmt_tokens_none():
def test_fmt_tokens_negative(): def test_fmt_tokens_negative():
assert fmt_tokens(-1) == "?" assert fmt_tokens(-1) == "?"
# ---------------------------------------------------------------------------
# fmt_tokens — applied in usage + inline comment bodies (Task 3)
# ---------------------------------------------------------------------------
def test_collapsible_usage_renders_humanized_tokens():
usage = {"input": 2_071_025, "output": 17303, "reasoning": 0,
"cache_read": 0, "cache_write": 0, "total": 2_088_328,
"cost": 0.0, "steps": 1, "duration_s": 10.0}
block = _render_collapsible_usage(usage, "glm-5.2:cloud", config={})
assert "2,071,025 (2.1M) in" in block
assert "17,303 (17.3K) out" in block
def test_inline_comment_body_humanized_tokens():
# Value chosen > 1000 so fmt_tokens actually adds the comma + short suffix;
# the plan's 362 would render identically with or without fmt_tokens.
f = {"severity": "medium", "path": "x.py", "line": 1,
"problem": "p", "fix": "", "suggestion": "", "reference": "",
"_tok_attrib": 17303, "_tok_pct": 0.11}
body = inline_comment_body(f)
assert "🪙 ~17,303 (17.3K) tok" in body