testsAndMisc/python_pkg/token_audit/checkup.py
Krzysztof kuhy Rudnicki 11aea86920 Land the token_audit standing-cost checkup (116 tests, 100% branch coverage)
Answers "what is billed on every turn regardless of use" by measurement
instead of recall, which is the part earlier audits kept getting wrong.

A surface is reported dead only when unused in BOTH windows. One earlier
audit's seven-day dead list included six servers that had been used within
thirty days, so a single window is not evidence. Shares are weighted with
cache-read weight 0.1, so a cached surface is not billed as though it were
re-read in full each turn.

Verified by running it, not just by tests: the report renders over 44,311
turns, putting rules/ at 4,907 tokens (1.77%), MEMORY.md at 4,312 (1.55%),
memories/ at 3,758 (1.35%), skill descriptions at 3,279 (1.18%) and CLAUDE.md
at 2,057 (0.74%), with per-MCP 7d/30d call counts behind each verdict.

The files were found untracked in the tree from an earlier session; every
module is already under the 250-line cap, so nothing needed splitting, and
the package was already at 100% branch coverage, so nothing needed new tests.
This lands the instrument only -- no surface is unregistered on its say-so.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 14:33:03 +02:00

72 lines
2.4 KiB
Python

"""The weekly standing-cost checkup.
Complements the spend report. That report answers "where did the tokens go";
this answers "what is still being paid for every turn regardless of use", which
is the question earlier audits kept answering by memory and kept getting wrong.
Run as ``python3 -m python_pkg.token_audit.checkup``.
"""
from __future__ import annotations
import sys
from python_pkg.token_audit import surfaces, unused
from python_pkg.token_audit.probe import weighted_pct
def _fmt_usage(rows: list[unused.Usage]) -> list[str]:
"""Render call counts, marking only rows dead in BOTH windows."""
header = (
f"| name | {unused.SHORT_WINDOW_DAYS}d | {unused.LONG_WINDOW_DAYS}d | verdict |"
)
lines = [header, "|---|---|---|---|"]
for row in rows:
verdict = "**park**" if row.dead else "keep"
lines.append(
f"| {row.name} | {row.short_calls} | {row.long_calls} | {verdict} |"
)
return lines
def build(turns: int, weighted_total: int) -> str:
"""Render the checkup as markdown."""
out = ["# Standing-cost checkup", ""]
out.append(
"Every row below is billed on each of "
f"{turns:,} turns. Shares are weighted (cache-read weight 0.1)."
)
out.append("")
out.append("## Surfaces")
out.append("| surface | est tokens | share | detail |")
out.append("|---|---|---|---|")
for surface in surfaces.collect():
share = weighted_pct(surface.est_tokens, turns, weighted_total)
est = f"{surface.est_tokens:,}" if surface.est_tokens else "A/B only"
out.append(f"| {surface.name} | {est} | {share:.2f}% | {surface.detail} |")
out += ["", "## MCP servers", ""]
out += _fmt_usage(unused.mcp_usage())
out += ["", "## Skills", ""]
out += _fmt_usage(unused.skill_usage())
out += [
"",
"_A surface marked `A/B only` cannot be sized from disk; measure it",
"with `probe.measure()`, which runs non-strict so project `.mcp.json`",
"files are included._",
"",
]
return "\n".join(out)
def main(argv: list[str] | None = None) -> int:
"""Print the checkup, taking turns and weighted total from the last report."""
argv = sys.argv[1:] if argv is None else argv
turns = int(argv[0]) if argv else 44311
total = int(argv[1]) if len(argv) > 1 else 1230346533
sys.stdout.write(build(turns, total))
return 0
if __name__ == "__main__":
sys.exit(main())