Answer architecture round 4: anchor the record's headings to a UTC stamp, import the archive's names from their owner, keep docs/reading-cost.md out of the panel's reading, and order the decision's reasons

This commit is contained in:
2026-10-08 18:02:03 +02:00
parent 946daf5e3f
commit 8c9bf090f1
9 changed files with 38 additions and 25 deletions
+2 -1
View File
@@ -15,7 +15,8 @@ from loop_claude.claude_code.session import session_files
FINISHED = "Verdict: finished"
UNFINISHED = "Verdict: unfinished"
STAMP = "%Y-%m-%dT%H:%M:%SZ"
FINISHED_AT = re.compile(rf"^(\d{{4}}-\d\d-\d\dT\d\d:\d\d:\d\dZ) {re.escape(FINISHED)}$", re.MULTILINE)
STAMPED = r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ"
FINISHED_AT = re.compile(rf"^({STAMPED}) {re.escape(FINISHED)}$", re.MULTILINE)
@dataclasses.dataclass(frozen=True)
+1 -1
View File
@@ -2,7 +2,7 @@ import pathlib
import typing
from failure import Failure
from loop_claude.transcript_archive import finished_chunks, FinishedChunk, utc
from loop_claude.transcript_archive import FinishedChunk
from loop_claude.paths import paths_from
from loop_claude.project.checkout import project_key
+12 -9
View File
@@ -3,20 +3,21 @@ import re
import statistics
from failure import Failure
from reading_cost.archive import FinishedChunk
from loop_claude.transcript_archive import FinishedChunk, STAMPED
from reading_cost.measure import Reading
from markdown_sections import sections
HEADING = "Reading cost, chunk "
FIGURE = re.compile(rf"^Reading cost, chunk (\w+), finished ({STAMPED})$")
PLACE = re.compile(r"^- `(.+)` (\d+)$")
PLACES_KEPT = 10
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 = {heading.removeprefix(HEADING).split(",")[0] for heading in sections(record) if heading.startswith(HEADING)}
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]
@@ -26,7 +27,7 @@ def chunk_id(chunk: FinishedChunk) -> str:
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} new input tokens before the first commit.\n" + (f"\nRead most (opens and searches):\n\n{places}" if places else "")
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{places}" if places else "")
# The figures after the last reset, in the order their chunks finished: each machine appends its own archive's chunks.
@@ -34,15 +35,17 @@ def readings_of(record: str) -> list[Reading] | Failure:
dated = []
resets = [""]
for heading, bodies in sections(record).items():
if heading.startswith(RESET):
resets.append(heading.removeprefix(RESET))
elif heading.startswith(HEADING):
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((heading.rsplit(", finished ", 1)[-1], Reading(tokens[0], collections.Counter({match.group(1): int(match.group(2)) for line in lines if (match := PLACE.match(line))}))))
return [reading for finished, reading in sorted(dated, key=lambda item: item[0]) if finished > max(resets)]
dated.append((named.group(2), heading, Reading(tokens[0], collections.Counter({match.group(1): int(match.group(2)) for line in lines if (match := PLACE.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(places: collections.Counter[str]) -> list[tuple[str, int]]:
+13 -7
View File
@@ -22,6 +22,7 @@ lists the files the last 5 chunks read most. With fewer than 10 figures, it says
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 subprocess
@@ -31,9 +32,16 @@ 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 reading_cost.archive import finished_chunks, project_archive, read_threads, utc
from loop_claude.transcript_archive import finished_chunks, utc
from reading_cost.archive import project_archive, read_threads
from file_writes import replace_file
@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
@@ -52,14 +60,12 @@ def main(argv: list[str]) -> int:
updated = update(record, archive, checkout, str(home), utc(time.time()) if reset else None)
if isinstance(updated, Failure):
return failed(updated)
appended, readings = updated
print(f"Appended {appended} reading-cost figure{'' if appended == 1 else 's'} from {archive}{' and a reset' if reset else ''} to {record}.")
print(trend(readings))
print(f"Appended {updated.appended} reading-cost figure{'' if updated.appended == 1 else 's'} from {archive}{' and a reset' if reset else ''} to {record}.")
print(trend(updated.readings))
return 0
def update(record: pathlib.Path, archive: pathlib.Path, checkout: Checkout, home: str, reset_at: str | None) -> tuple[int, list[Reading]] | Failure:
"""How many figures it appended, and the readings the trend counts."""
def update(record: pathlib.Path, archive: pathlib.Path, checkout: Checkout, home: str, reset_at: str | None) -> Updated | Failure:
text = read_record(record)
if isinstance(text, Failure):
return text
@@ -73,7 +79,7 @@ def update(record: pathlib.Path, archive: pathlib.Path, checkout: Checkout, home
return readings
if marks and (unwritten := write_record(record, text)):
return unwritten
return len(figures), readings
return Updated(len(figures), readings)
def new_figures(record: str, archive: pathlib.Path, checkout: Checkout, home: str) -> list[str] | Failure:
+6 -3
View File
@@ -79,10 +79,13 @@ class Figures(unittest.TestCase):
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 = figures.figure(FinishedChunk("abcdef0123456789", "t", pathlib.Path(), []), reading)
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")
written = figures.figure(FinishedChunk("abcdef0123456789", "2026-10-08T00:00:00Z", pathlib.Path(), []), reading)
self.assertEqual(written, "## Reading cost, chunk abcdef012345, finished 2026-10-08T00:00:00Z\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(figures.readings_of(written), [reading])
self.assertEqual(figures.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'))
self.assertEqual(figures.readings_of(written.replace("7 new", "seven new")), Failure('the figure under "## Reading cost, chunk abcdef012345, finished 2026-10-08T00:00:00Z" holds no single tokens line; restore it from git history'))
for damaged in (written.replace(", finished", ""), "## Reading cost reset, x\n"):
with self.subTest(damaged=damaged):
self.assertIsInstance(figures.readings_of(damaged), Failure)
def test_counts_only_figures_finished_after_the_last_reset_in_the_order_they_finished(self):