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

127 lines
5.4 KiB
Python

"""Measure each chunk loop-claude finished, append the figures to a project's record, and say whether they rise.
usage: reading-cost [--reset] [record]
Run it inside a project checkout or one of its worktrees. record defaults to the
docs/reading-cost.md the project's config names, created when missing. It reads loop-claude's archive of the project,
$LOOP_CLAUDE_TRANSCRIPTS/<key> (default ~/.claude/loop-claude-transcripts), <key> being the
checkout's path relative to ~/code, or to / outside it. For each finished chunk the record does not
yet hold, it measures what the worker and its subagents read before the worker's first commit: their
new input tokens (uncached and cache-written; cache reads excluded), and how often they opened or
searched each file git tracks in the checkout. A chunk whose worker never committed is not measured.
Each figure is appended under a "## Reading cost, chunk <id>, finished <UTC time>" heading, in the
order the chunks finished.
--reset also appends a "## Reading cost reset, <UTC time>" heading: the trend then counts only
figures of chunks that finished after it.
It then compares the median tokens of the record's last 5 figures with the 5 before them, in the
order their chunks finished, and prints "Rising:" or "Not rising:" with both medians. Rising, it
lists the files the last 5 chunks read most. With fewer than 10 figures, it says so.
Exit 1, saying why, on unknown arguments, or when git cannot start or finds no checkout, the
archive or the record cannot be read or written, or a figure in the record cannot be read back.
"""
import dataclasses
import os
import pathlib
import sys
import time
from reading_cost.measure import Checkout, measure, Reading
from failure import Failure
from reading_cost.figures import figure, merged, readings_of, RESET, trend, unheld
from record_store.stores import FileStore, RecordStore, store
from transcript_archive.read import finished_chunks, read_threads
from git_cli.git_repo import git
from project_config.main import project
from transcript_archive.location import project_archive
from project_config import templates
from text_files.utc_stamp import utc
@dataclasses.dataclass(frozen=True)
class Updated:
appended: int
readings: list[Reading]
def main(argv: list[str]) -> int:
reset = argv[:1] == ["--reset"]
given = argv[1:] if reset else argv
if given in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if len(given) > 1 or given[:1] and given[0].startswith("-"):
print(f"unknown arguments {' '.join(given)}; usage: reading-cost [--reset] [record]", file=sys.stderr)
return 1
config = project()
# Given a record, the tool runs without a config, and then knows no worktree names but the ones the default template makes.
checkout = located(templates.BRANCH if isinstance(config, Failure) else config.branch)
if isinstance(checkout, Failure):
return failed(checkout)
home = pathlib.Path.home()
archive = project_archive(checkout.main_root, home, os.environ)
if not given and isinstance(config, Failure):
return failed(config)
record: RecordStore = FileStore(pathlib.Path(given[0]), None) if given or isinstance(config, Failure) else store(config, "reading-cost")
updated = update(record, archive, checkout, str(home), utc(time.time()) if reset else None)
if isinstance(updated, Failure):
return failed(updated)
print(f"Appended {updated.appended} reading-cost figure{'' if updated.appended == 1 else 's'} from {archive}{' and a reset' if reset else ''} to {record.label}.")
print(trend(updated.readings))
return 0
def update(record: RecordStore, archive: pathlib.Path, checkout: Checkout, home: str, reset_at: str | None) -> Updated | Failure:
held = record.held()
if isinstance(held, Failure):
return held
# An empty record starts with its title.
text = held.text or "# Reading cost\n"
figures = new_figures(text, archive, checkout, home)
if isinstance(figures, Failure):
return figures
marks = [*figures, f"## {RESET}{reset_at}\n"] if reset_at else figures
text = merged(text, marks) if marks else text
readings = readings_of(text)
if isinstance(readings, Failure):
return readings
written = record.write(text, held.version) if marks else None
return written if isinstance(written, Failure) else Updated(len(figures), readings)
def new_figures(record: str, archive: pathlib.Path, checkout: Checkout, home: str) -> list[str] | Failure:
chunks = finished_chunks(archive)
if isinstance(chunks, Failure):
return chunks
figures = []
for chunk in unheld(chunks, record):
threads = read_threads(chunk)
if isinstance(threads, Failure):
return threads
if reading := measure(threads, checkout, home):
figures.append(figure(chunk, reading))
return figures
def failed(failure: Failure) -> int:
print(failure.reason, file=sys.stderr)
return 1
def located(branch: str) -> Checkout | Failure:
"""The checkout that holds the current directory, whose project names each item's worktree by the template branch."""
found = git("rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir")
if isinstance(found, Failure):
return found
if len(lines := found.splitlines()) != 2:
return Failure(f"git rev-parse printed {found!r}, not a checkout's top folder and git directory on two lines")
top, common = lines
listed = git("ls-files", "-z", cwd=top)
if isinstance(listed, Failure):
return listed
# The common dir is the main checkout's .git, whichever worktree runs this.
return Checkout(top, str(pathlib.Path(common).parent), frozenset(filter(None, listed.split("\0"))), branch)