From f26e3313144a686bfcaf62047d50e34cce0c3c7e Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 14 Oct 2026 23:12:04 +0200 Subject: [PATCH] =?UTF-8?q?Answer=20architecture=20round=203:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1;=20test=20routing's=20plan=20a?= =?UTF-8?q?nd=20archive=20sources,=20read=20the=20newest=20outcomes=20file?= =?UTF-8?q?=20for=20a=20rerun's=20rung,=20and=20note=20the=20archive=20rea?= =?UTF-8?q?ds=20Rung's=20wording?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/loop_claude/test_loop_claude.py | 38 +++++++++++++++++++ bin/model_routing/ladder.py | 1 + bin/transcript_archive/read.py | 14 +++++-- .../test_transcript_archive.py | 10 +++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 5de17c25..635f0537 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -24,6 +24,7 @@ from loop_claude.cycle import interrupt, judge, prompts, retry, run, states, wor from model_routing.ladder import LADDER, Rung from loop_claude.cycle.retry import Limited from loop_claude.loop import main, reload +from transcript_archive import outcomes, write from loop_claude import paths, routing from transcript_archive.location import project_archive from loop_claude.stop import Stop @@ -449,6 +450,43 @@ class Routing(unittest.TestCase): self.assertEqual(main.routed(run_with(below), self.OPUS, never, lambda: None).route, routing.Route(self.OPUS)) +class RouteSources(unittest.TestCase): + def test_routes_on_the_plan_item_and_says_why_it_routes_on_the_projects_rung(self): + with tempfile.TemporaryDirectory() as root: + self.assertIsNone(main.plan_item(root)) + todo = pathlib.Path(root, "todo.md") + todo.write_text(TODO) + self.assertEqual(main.plan_item(root).id, 3) + for text, said in ((TODO.replace("| 5 | 3 |", "| x | 3 |"), "routing on the project's rung: item 3's R 'x' is not a whole number from 1 to 10\n"), (b"\xff", "routing on the project's rung: could not read todo.md: ")): + with self.subTest(said=said): + todo.write_bytes(text if isinstance(text, bytes) else text.encode()) + with contextlib.redirect_stderr(io.StringIO()) as stderr: + self.assertIsNone(main.plan_item(root)) + self.assertTrue(stderr.getvalue().startswith(said), stderr.getvalue()) + + def test_resumes_on_the_archived_route_and_says_why_it_routes_afresh(self): + with tempfile.TemporaryDirectory() as home: + opened = session.session_named("A proj loop-claude", pathlib.Path(home, "projects")) + archive = pathlib.Path(home, "archive") + self.assertIsNone(main.archived_route(archive, opened)) + transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") + self.assertIsNone(main.archived_route(archive, opened)) + route = routing.Route(Rung("claude-opus-5-5", "high"), 3, 11) + with contextlib.redirect_stderr(io.StringIO()): + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(str(route), "working"), NOW)) + self.assertEqual(main.archived_route(archive, opened), route) + (chunk,) = archive.iterdir() + chunk.chmod(0) + try: + with contextlib.redirect_stderr(io.StringIO()) as stderr: + self.assertIsNone(main.archived_route(archive, opened)) + finally: + chunk.chmod(0o700) + self.assertTrue(stderr.getvalue().startswith("routing the chunk afresh: could not read the archive"), stderr.getvalue()) + + class WorkerArgs(unittest.TestCase): def test_opens_a_fresh_session_by_name_and_resumes_an_open_one(self): opened = session.session_named("A proj loop-claude", pathlib.Path("/h/.claude/projects")) diff --git a/bin/model_routing/ladder.py b/bin/model_routing/ladder.py index b6a2d62b..cc6db210 100644 --- a/bin/model_routing/ladder.py +++ b/bin/model_routing/ladder.py @@ -11,6 +11,7 @@ class Rung: def flags(self) -> list[str]: return ["--model", self.model, *(["--effort", self.effort] if self.effort else [])] + # The archive records this wording, and loop_claude.routing.ROUTED reads it back to resume a chunk on its rung. def __str__(self) -> str: return f"{self.model} at {self.effort} effort" if self.effort else self.model diff --git a/bin/transcript_archive/read.py b/bin/transcript_archive/read.py index 3672fc2c..3c6476ce 100644 --- a/bin/transcript_archive/read.py +++ b/bin/transcript_archive/read.py @@ -37,11 +37,19 @@ def read_threads(chunk: FinishedChunk) -> list[bytes] | Failure: def latest_worker(project_archive: pathlib.Path, transcript: str) -> str | Failure | None: - """The worker the latest verdict archived for the transcript's chunk names; None before its first verdict.""" + """The worker the latest outcome archived for the transcript's chunk names; None before its first verdict. + + Stamps run to the second, so the newest outcomes file decides, and in it the last outcome naming a worker. + """ try: with open(transcript, "rb") as handle: folder = chunk_folder(project_archive, handle.read()) - found = sorted(stamped for outcomes in folder.glob("*.outcomes.md") for stamped in workers(outcomes.read_text(encoding="utf-8"))) + if not folder.is_dir(): + return None + files = sorted((path for path in folder.iterdir() if path.name.endswith(".outcomes.md")), key=lambda path: path.stat().st_mtime_ns, reverse=True) + for path in files: + if found := workers(path.read_text(encoding="utf-8")): + return found[-1][1] except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read the archive {project_archive}: {failure}") - return found[-1][1] if found else None + return None diff --git a/bin/transcript_archive/test_transcript_archive.py b/bin/transcript_archive/test_transcript_archive.py index f9c3e00f..1d3385b2 100644 --- a/bin/transcript_archive/test_transcript_archive.py +++ b/bin/transcript_archive/test_transcript_archive.py @@ -56,6 +56,16 @@ class Outcomes(unittest.TestCase): ])) self.assertEqual(outcomes.workers(kept), [(utc(NOW), "w1"), (utc(NOW + 2), "w2"), (utc(NOW + 3), "w3")]) + def test_takes_the_outcome_written_last_within_one_second(self): + with tempfile.TemporaryDirectory() as root: + kept = pathlib.Path(root, "archive") + transcript = pathlib.Path(root, "t.jsonl") + transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n') + with contextlib.redirect_stderr(io.StringIO()): + for worker in ("z-first", "a-last"): + self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished(worker, "working"), NOW)) + self.assertEqual(read.latest_worker(kept, str(transcript)), "a-last") + def test_names_the_worker_the_latest_outcome_of_the_transcripts_chunk_records(self): with tempfile.TemporaryDirectory() as root: kept = pathlib.Path(root, "archive")