testsAndMisc/python_pkg/token_audit/unused.py
Krzysztof kuhy Rudnicki adf2e402df Grade against 3.14 too, not just run on it
`requires-python` and the CI pin already said 3.14, but ruff, mypy and pylint
were still grading `meta/` against 3.10 -- three tools judging an interpreter
nothing runs any more, and ruff withholding the pyupgrade fixes that only
become safe at the newer target. `pin_ci_python.py` rewrites these three knobs.

What the stale target was hiding: `datetime.timezone.utc` where `datetime.UTC`
is now correct (UP017), and ruff's stable formatter applying PEP 758 --
`except (A, B):` becomes `except A, B:`. That makes the source 3.14-only at the
syntax level, which is what `requires-python = ">=3.14"` already promised; kuhy
chose it deliberately over holding ruff a release back (2026-08-29).

Also drops a comment in `wsg_grabber/models.py` that the bump makes false: it
explained why the file used ``str, Enum`` instead of ``StrEnum`` under mypy at
3.10, sitting directly above a ``class Verdict(StrEnum)`` that had already
stopped obeying it.

ruff, ruff-format, mypy --strict and pylint all clean at the new target.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SfnJmxxsZ534AKk16rC2Gr
2026-08-29 11:50:39 +02:00

79 lines
2.5 KiB
Python

"""Find registered surfaces that nothing has called, using two time windows.
A single seven-day window is not enough evidence to unregister anything: one
audit's seven-day "dead" list included six servers that had been used within
thirty days. A surface therefore qualifies as dead only when it is unused in
the short window *and* the long one.
"""
from __future__ import annotations
from collections import Counter
from dataclasses import dataclass
import json
from pathlib import Path
import re
import time
TRANSCRIPT_ROOT = Path.home() / ".claude" / "projects"
SECONDS_PER_DAY = 86400
SHORT_WINDOW_DAYS = 7
LONG_WINDOW_DAYS = 30
_MCP_CALL = re.compile(r'"name":"mcp__([a-zA-Z0-9_-]+)__')
_SKILL_CALL = re.compile(r'"skill":"([^"]+)"')
@dataclass(frozen=True)
class Usage:
"""Call counts for one surface across both windows."""
name: str
short_calls: int
long_calls: int
@property
def dead(self) -> bool:
"""True when unused in both windows, so it is safe to park."""
return self.short_calls == 0 and self.long_calls == 0
def _scan(pattern: re.Pattern[str], since: float) -> Counter[str]:
"""Count regex matches across transcripts modified after *since*."""
found: Counter[str] = Counter()
for path in TRANSCRIPT_ROOT.glob("*/*.jsonl"):
try:
if path.stat().st_mtime < since:
continue
text = path.read_text(errors="replace")
except OSError:
continue
found.update(pattern.findall(text))
return found
def usage_for(
names: list[str], pattern: re.Pattern[str], now: float | None = None
) -> list[Usage]:
"""Report short- and long-window call counts for each of *names*."""
now = time.time() if now is None else now
short = _scan(pattern, now - SHORT_WINDOW_DAYS * SECONDS_PER_DAY)
long_ = _scan(pattern, now - LONG_WINDOW_DAYS * SECONDS_PER_DAY)
return [Usage(n, short.get(n, 0), long_.get(n, 0)) for n in sorted(names)]
def mcp_usage(config: Path | None = None) -> list[Usage]:
"""Call counts for every globally registered MCP server."""
config = config or Path.home() / ".claude.json"
try:
names = list(json.loads(config.read_text()).get("mcpServers", {}))
except json.JSONDecodeError, OSError:
return []
return usage_for(names, _MCP_CALL)
def skill_usage() -> list[Usage]:
"""Invocation counts for every personal skill."""
root = Path.home() / ".claude" / "skills"
names = [p.parent.name for p in root.glob("*/SKILL.md")]
return usage_for(names, _SKILL_CALL)