Answer product-owner round 1: name the figure's new input tokens, label the places' counts, give a usage error one line, and say where the reading-cost item goes

This commit is contained in:
2026-10-08 16:43:22 +02:00
parent f33704872f
commit da1ef19795
6 changed files with 28 additions and 28 deletions
+19 -18
View File
@@ -3,14 +3,14 @@
usage: reading_cost.py [--reset] [history]
Run it inside a project checkout or one of its worktrees. history defaults to
docs/comprehension-history.md there, 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 chunk the judge ruled
finished, under a UTC time, that history does not hold, it measures what the worker and its subagents read before
the worker's first commit: the tokens they took in, 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
Run it inside a project checkout or one of its worktrees. history defaults to docs/comprehension-
history.md there, 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 history 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
@@ -49,7 +49,7 @@ HEADING = "Reading cost, chunk "
PLACE = re.compile(r"^- `(.+)` (\d+)$")
PLACES_KEPT = 10
RESET = "Reading cost reset, "
TOKENS = re.compile(r"^(\d+) tokens read before the first commit\.$")
TOKENS = re.compile(r"^(\d+) new input tokens before the first commit\.$")
WINDOW = 5
@@ -70,10 +70,12 @@ class Checkout:
def main(argv: list[str]) -> int:
reset = argv[:1] == ["--reset"]
given = argv[1:] if reset else argv
if given[:1] in (["-h"], ["--help"]) or len(given) > 1 or given[:1] and given[0].startswith("-"):
helped = given[:1] in (["-h"], ["--help"]) and len(given) == 1
print(__doc__.strip(), file=sys.stdout if helped else sys.stderr)
return 0 if helped else 1
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.py [--reset] [history]", file=sys.stderr)
return 1
checkout = located()
if isinstance(checkout, Failure):
return failed(checkout)
@@ -85,7 +87,7 @@ def main(argv: list[str]) -> int:
if isinstance(updated, Failure):
return failed(updated)
appended, readings = updated
print(f"Appended {appended} reading-cost figures from {archive}{' and a reset' if reset else ''} to {history}.")
print(f"Appended {appended} reading-cost figure{'' if appended == 1 else 's'} from {archive}{' and a reset' if reset else ''} to {history}.")
print(trend(readings))
return 0
@@ -233,7 +235,7 @@ def relative(path: str, roots: list[str]) -> str | None:
def figure(chunk: FinishedChunk, reading: Reading) -> str:
places = "".join(f"- `{path}` {times}\n" for path, times in ranked(reading.places))
return f"## {HEADING}{chunk_id(chunk)}, finished {chunk.finished}\n\n{reading.tokens} tokens read before the first commit.\n" + (f"\nRead most:\n\n{places}" if places else "")
return f"## {HEADING}{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{places}" if places else "")
# The figures after the last reset, in the order their chunks finished: each machine appends its own archive's chunks.
@@ -260,16 +262,15 @@ def merged(history: str, figures: list[str]) -> str:
return history.rstrip("\n") + "\n\n" + "\n".join(figures)
# Medians, so one chunk that ran a panel of readers or waited out a usage limit does not decide the trend alone.
def trend(readings: list[Reading]) -> str:
if len(readings) < 2 * WINDOW:
return f"{len(readings)} chunks measured; the trend needs {2 * WINDOW}."
last, earlier = middle(readings[-WINDOW:]), middle(readings[-2 * WINDOW:-WINDOW])
medians = f"the last {WINDOW} chunks' median is {last} tokens read before their first commit, the {WINDOW} before them {earlier}."
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.places for reading in readings[-WINDOW:]), collections.Counter())))
return f"Rising: {medians}\n\nRead most over the last {WINDOW}:\n{read_most}"
return f"Rising: {medians}\n\nRead most over the last {WINDOW} (opens and searches):\n{read_most}"
def middle(readings: list[Reading]) -> int:
+5 -6
View File
@@ -73,16 +73,16 @@ class Figures(unittest.TestCase):
def test_rises_only_above_the_five_before_and_needs_ten(self):
flat = [Reading(100, collections.Counter({"a.py": 1}))] * 10
self.assertEqual(reading_cost.trend(flat[:9]), "9 chunks measured; the trend needs 10.")
self.assertTrue(reading_cost.trend(flat).startswith("Not rising: the last 5 chunks' median is 100 tokens"))
self.assertTrue(reading_cost.trend(flat).startswith("Not rising: the last 5 chunks' median is 100 new input tokens"))
self.assertTrue(reading_cost.trend(flat[:9] + [Reading(3000000, collections.Counter())]).startswith("Not rising:"))
self.assertEqual(reading_cost.trend(flat[:5] + [Reading(101, collections.Counter({"b.py": 2}))] * 5), "Rising: the last 5 chunks' median is 101 tokens read before their first commit, the 5 before them 100.\n\nRead most over the last 5:\n\n- `b.py` 10")
self.assertEqual(reading_cost.trend(flat[:5] + [Reading(101, collections.Counter({"b.py": 2}))] * 5), "Rising: the last 5 chunks' median is 101 new input tokens before their first commit, the 5 before them 100.\n\nRead most over the last 5 (opens and searches):\n\n- `b.py` 10")
def test_a_figure_reads_back_as_the_reading_it_was_written_from_and_a_damaged_one_fails(self):
reading = Reading(7, collections.Counter({"b.py": 1, "a.py": 3}))
written = reading_cost.figure(FinishedChunk("abcdef0123456789", "t", pathlib.Path(), []), reading)
self.assertEqual(written, "## Reading cost, chunk abcdef012345, finished t\n\n7 tokens read before the first commit.\n\nRead most:\n\n- `a.py` 3\n- `b.py` 1\n")
self.assertEqual(written, "## Reading cost, chunk abcdef012345, finished t\n\n7 new input tokens before the first commit.\n\nRead most (opens and searches):\n\n- `a.py` 3\n- `b.py` 1\n")
self.assertEqual(reading_cost.readings_of(written), [reading])
self.assertEqual(reading_cost.readings_of(written.replace("7 tokens", "seven tokens")), Failure('the figure under "## Reading cost, chunk abcdef012345, finished t" holds no single tokens line; restore it from git history'))
self.assertEqual(reading_cost.readings_of(written.replace("7 new", "seven new")), Failure('the figure under "## Reading cost, chunk abcdef012345, finished t" holds no single tokens line; restore it from git history'))
def test_counts_only_figures_finished_after_the_last_reset_in_the_order_they_finished(self):
@@ -136,7 +136,7 @@ class ReadingCostTool(unittest.TestCase):
self.assertEqual(ran.returncode, 0, ran.stderr)
history = worktree / "docs" / "comprehension-history.md"
self.assertEqual(ran.stdout, f"Appended 2 reading-cost figures from {self.home / '.claude' / 'loop-claude-transcripts' / 'proj'} to {history}.\n2 chunks measured; the trend needs 10.\n")
figure = "## Reading cost, chunk {}, finished {}\n\n33 tokens read before the first commit.\n\nRead most:\n\n- `a.py` 1\n"
figure = "## Reading cost, chunk {}, finished {}\n\n33 new input tokens before the first commit.\n\nRead most (opens and searches):\n\n- `a.py` 1\n"
self.assertEqual(history.read_text(), "# Comprehension history\n\n" + "\n".join(figure.format(chunk.folder[:12], chunk.finished) for chunk in chunks))
self.assertTrue(self.run_tool(worktree).stdout.startswith(f"Appended 0 reading-cost figures from"))
self.assertEqual(history.read_text().count("## Reading cost"), 2)
@@ -147,7 +147,6 @@ class ReadingCostTool(unittest.TestCase):
def test_refuses_an_option_it_does_not_know(self):
ran = self.run_tool(self.checkout, "--rest")
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
self.assertFalse((self.checkout / "--rest").exists())
def test_exits_1_outside_a_checkout(self):
ran = self.run_tool(self.home)