Files
claude-config/bin/reading_cost/figures.py
T

74 lines
3.4 KiB
Python

import collections
import re
import statistics
from failure import Failure
from transcript_archive.read import FinishedChunk
from reading_cost.measure import Reading
from text_files.markdown_text import sections
from text_files.utc_stamp import STAMPED
FIGURE = re.compile(rf"^Reading cost, chunk (\w+), finished ({STAMPED})$")
FIGURE_FILE = re.compile(r"^- `(.+)` (\d+)$")
FILES_KEPT = 10
RECORD = "docs/reading-cost.md"
RESET = "Reading cost reset, "
RESET_AT = re.compile(rf"^{RESET}({STAMPED})$")
TOKENS = re.compile(r"^(\d+) new input tokens before the first commit\.$")
WINDOW = 5
def unheld(chunks: list[FinishedChunk], record: str) -> list[FinishedChunk]:
held = {match.group(1) for heading in sections(record) if (match := FIGURE.match(heading))}
return [chunk for chunk in chunks if chunk_id(chunk) not in held]
def chunk_id(chunk: FinishedChunk) -> str:
return chunk.folder[:12]
def figure(chunk: FinishedChunk, reading: Reading) -> str:
files = "".join(f"- `{path}` {times}\n" for path, times in ranked(reading.files))
return f"## Reading cost, chunk {chunk_id(chunk)}, finished {chunk.finished}\n\n{reading.tokens} new input tokens before the first commit.\n" + (f"\nRead most (opens and searches):\n\n{files}" if files else "")
# The figures after the last reset, in the order their chunks finished: each machine appends its own archive's chunks.
def readings_of(record: str) -> list[Reading] | Failure:
dated = []
resets = [""]
for heading, bodies in sections(record).items():
if reset := RESET_AT.match(heading):
resets.append(reset.group(1))
elif named := FIGURE.match(heading):
lines = bodies[0].splitlines() if bodies else []
tokens = [int(match.group(1)) for line in lines if (match := TOKENS.match(line))]
if len(tokens) != 1:
return Failure(f"the figure under \"## {heading}\" holds no single tokens line; restore it from git history")
dated.append((named.group(2), heading, Reading(tokens[0], collections.Counter({match.group(1): int(match.group(2)) for line in lines if (match := FIGURE_FILE.match(line))}))))
elif heading.startswith("Reading cost"):
return Failure(f"\"## {heading}\" names no chunk and UTC time, or no reset time; restore it from git history")
return [reading for finished, _, reading in sorted(dated, key=lambda item: item[:2]) if finished > max(resets)]
def ranked(files: collections.Counter[str]) -> list[tuple[str, int]]:
return sorted(files.items(), key=lambda item: (-item[1], item[0]))[:FILES_KEPT]
def merged(record: str, figures: list[str]) -> str:
return record.rstrip("\n") + "\n\n" + "\n".join(figures)
def trend(readings: list[Reading]) -> str:
if len(readings) < 2 * WINDOW:
return f"{len(readings)} chunks measured since the last reset; the trend needs {2 * WINDOW}."
last, earlier = median_tokens(readings[-WINDOW:]), median_tokens(readings[-2 * WINDOW:-WINDOW])
medians = f"the last {WINDOW} chunks' median is {last} new input tokens before their first commit, the {WINDOW} before them {earlier}."
if last <= earlier:
return f"Not rising: {medians}"
read_most = "".join(f"\n- `{path}` {times}" for path, times in ranked(sum((reading.files for reading in readings[-WINDOW:]), collections.Counter())))
return f"Rising: {medians}\n\nRead most over the last {WINDOW} (opens and searches):\n{read_most}"
def median_tokens(readings: list[Reading]) -> int:
return round(statistics.median(reading.tokens for reading in readings))