Answer architecture round 2: the shared modules and the judge's reply contract are named for what they hold

This commit is contained in:
2026-10-03 18:12:38 +02:00
parent d3613f45d4
commit 38c2e24f96
20 changed files with 70 additions and 59 deletions
View File
+1 -1
View File
@@ -3,7 +3,7 @@ import pathlib
import sys
from failure import Failure
from files.writes import replace_json
from file_writes import replace_json
# A failure only warns: the interrupt still runs, and the human answers Claude Code's trust dialog.
+1 -1
View File
@@ -5,7 +5,7 @@ import sys
from failure import Failure
from loop_claude.claude_code.transcript import first_timestamped_line
from files.writes import make_private, replace_file
from file_writes import make_private, replace_file
from loop_claude.paths import Paths
from loop_claude.claude_code.session import session_files
+1 -1
View File
@@ -4,7 +4,7 @@ import re
from loop_claude.effects import Effects
from failure import Failure
from loop_claude.claude_code.entries import hit_limit, limit_said, parse_entry, structured_output
from loop_claude.cycle.verdicts import Interrupt, JUDGE_RULES, JUDGE_SCHEMA, QuotedAnswer, Verdict, verdict_from
from loop_claude.cycle.judge_reply import Interrupt, JUDGE_RULES, JUDGE_SCHEMA, QuotedAnswer, Verdict, verdict_from
from loop_claude.cycle.retry import Limited, retried
from loop_claude.stop import Stop
+1 -1
View File
@@ -6,7 +6,7 @@ from loop_claude.cycle.archive import archive
from loop_claude.claude_code.session import delete_session, Session, session_open, transcript_file
from loop_claude.effects import Effects
from failure import Failure
from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished
from loop_claude.cycle.judge_reply import Finished, Interrupt, QuotedAnswer, Unfinished
from loop_claude.cycle.interrupt import interrupt
from loop_claude.cycle.judge import judge
from loop_claude.project.model_choice import Models
+1 -1
View File
@@ -1,6 +1,6 @@
import dataclasses
from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
from loop_claude.cycle.judge_reply import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
# A backstop for what the judge misses, such as a reworded repeat, so the worker cannot loop unattended on answers quoted from README.md or the records.
QUOTED_ANSWERS_PER_CYCLE = 3
+1 -1
View File
@@ -4,7 +4,7 @@ from failure import Failure
from loop_claude.cycle.retry import Limited
from loop_claude.claude_code.transcript import part_line
from loop_claude.project.records import QUOTE_SOURCES
from loop_claude.cycle.verdicts import QuotedAnswer
from loop_claude.cycle.judge_reply import QuotedAnswer
from loop_claude.claude_code.session import Session
PRINTED_TOOL_INPUT = 160
+1 -1
View File
@@ -9,7 +9,7 @@ from loop_claude.effects import Effects
from failure import Failure
from loop_claude.stop import needs_human, needs_terminal, Stop
from loop_claude.paths import Paths
from files.writes import replace_json
from file_writes import replace_json
@dataclasses.dataclass(frozen=True)
+1 -1
View File
@@ -1,5 +1,5 @@
from loop_claude.project.checkout import read_checkout_file
from files.markdown import sections
from markdown_sections import sections
from loop_claude.stop import Stop
+2 -2
View File
@@ -58,8 +58,8 @@ import re
import sys
from failure import Failure
from files.markdown import FENCE, sections
from files.writes import replace_file
from markdown_sections import FENCE, sections
from file_writes import replace_file
EXEMPTIONS = ("", "decision", "defect", "principle", "question", "security")
ITEMS_HEADER = "| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |"
+22
View File
@@ -0,0 +1,22 @@
import os
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from failure import Failure
import file_writes
class ReplaceFile(unittest.TestCase):
def test_removes_what_it_staged_when_the_replace_fails(self):
with tempfile.TemporaryDirectory() as root:
os.mkdir(os.path.join(root, "in-the-way"))
self.assertEqual(file_writes.replace_file(pathlib.Path(root, "in-the-way"), b"data"), Failure("Is a directory"))
self.assertEqual(os.listdir(root), ["in-the-way"])
if __name__ == "__main__":
unittest.main()
-33
View File
@@ -1,33 +0,0 @@
import os
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from failure import Failure
from files import markdown, writes
class Sections(unittest.TestCase):
def test_keeps_every_headed_section_with_its_bodies(self):
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n## Audience\nagain\n# Appendix\nnot audience\n"
self.assertEqual(markdown.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]})
def test_closes_a_fence_only_on_its_own_marker(self):
text = "## Goals\n````md\n~~~\n## Quoted\n```\n````\n ~~~\n## Also quoted\n ~~~\n1. one\n## Audience\nme\n"
self.assertEqual(list(markdown.sections(text)), ["Goals", "Audience"])
self.assertEqual(list(markdown.sections("```\nquoted:\n```python\n## Inside\n```\n## Goals\n1. one\n")), ["Goals"])
class ReplaceFile(unittest.TestCase):
def test_removes_what_it_staged_when_the_replace_fails(self):
with tempfile.TemporaryDirectory() as root:
os.mkdir(os.path.join(root, "in-the-way"))
self.assertEqual(writes.replace_file(pathlib.Path(root, "in-the-way"), b"data"), Failure("Is a directory"))
self.assertEqual(os.listdir(root), ["in-the-way"])
if __name__ == "__main__":
unittest.main()
+12 -12
View File
@@ -21,7 +21,7 @@ from loop_claude.project import checkout, model_choice, records
from loop_claude.effects import Effects
from loop_claude.claude_code import entries, session, transcript, trust
from failure import Failure
from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, verdict_from
from loop_claude.cycle.judge_reply import Finished, Interrupt, QuotedAnswer, Unfinished, verdict_from
from loop_claude.cycle.retry import Limited
from loop_claude.loop import main, reload
from loop_claude import paths
@@ -173,13 +173,13 @@ class Cycles(unittest.TestCase):
def cycle(self, fake, records=""):
return run.Cycle(effects=fake.effects(), models=model_choice.Models("judge-model", "worker-model"), paths=self.paths, premises="## Goals", project_key="proj", records=records, root=str(self.home), session=self.session, task="Do it.")
def archived_verdicts(self):
def archived_outcomes(self):
return sorted(path.read_text() for path in (self.paths.transcript_archive / "proj").glob("*/*.outcomes.md"))
def test_a_finished_chunk_is_archived_and_its_session_deleted(self):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
self.assertIsNone(run.run_cycle(self.cycle(fake)))
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"])
self.assertEqual(self.archived_outcomes(), ["Task: Do it.\n\nVerdict: finished\n"])
self.assertIsNone(session.transcript_file(self.session))
def test_an_unfinished_chunks_transcript_is_archived_and_the_next_cycle_resumes_its_session(self):
@@ -190,14 +190,14 @@ class Cycles(unittest.TestCase):
workers = fake.made("stream_claude")
self.assertIn("--session-id", workers[0])
self.assertIn("--resume", workers[1])
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: unfinished\n"] * 2)
self.assertEqual(self.archived_outcomes(), ["Task: Do it.\n\nVerdict: unfinished\n"] * 2)
def test_a_cycle_stopped_by_a_failure_or_ctrl_c_archives_what_its_session_holds(self):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[ran(code=1)] * 3)
self.assertEqual(run.run_cycle(self.cycle(fake)).code, 1)
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[KeyboardInterrupt()])
self.assertEqual(run.run_cycle(self.cycle(fake)), Stop(130, ""))
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nStopped: Ctrl-C\n", "Task: Do it.\n\nStopped: the judge failed three times running: the judge exited 1\n"])
self.assertEqual(self.archived_outcomes(), ["Task: Do it.\n\nStopped: Ctrl-C\n", "Task: Do it.\n\nStopped: the judge failed three times running: the judge exited 1\n"])
def test_archives_each_chunks_session_folder_replacing_a_copy_only_with_what_grew_from_it(self):
folder = self.paths.claude_code_transcripts / "-fake" / str(self.session.id)
@@ -243,7 +243,7 @@ class Cycles(unittest.TestCase):
return 0
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], on_terminal_claude=[answer], git=[ran()])
self.assertIsNone(run.run_cycle(self.cycle(fake)))
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: interrupt\n\nWhich one?\n", "Task: Do it.\n\nVerdict: unfinished\n"])
self.assertEqual(self.archived_outcomes(), ["Task: Do it.\n\nVerdict: interrupt\n\nWhich one?\n", "Task: Do it.\n\nVerdict: unfinished\n"])
self.assertIn(str(self.session.id), fake.made("claude_on_terminal")[0])
prompts = [stdin for args, stdin in fake.made("claude")]
self.assertEqual(len(prompts), 2)
@@ -279,7 +279,7 @@ class Cycles(unittest.TestCase):
stop = run.run_cycle(self.cycle(fake))
self.assertEqual(stop, Stop(1, f"could not delete {folder}: Permission denied; delete every file and folder matching {folder}*, then rerun loop-claude; a rerun before that resumes the finished chunk's session"))
self.assertTrue(session.session_open(self.session))
self.assertEqual(self.archived_verdicts(), [f"Task: Do it.\n\nVerdict: finished\n\nStopped: {stop.reason}\n"])
self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\nVerdict: finished\n\nStopped: {stop.reason}\n"])
def test_stops_when_the_worker_leaves_no_transcript(self):
fake = FakeEffects(streams=[Streamed(0)])
@@ -628,14 +628,14 @@ class JudgeFailures(unittest.TestCase):
("cycle/judge.py", "run_judge", "proc.stderr.strip() or f'the judge exited", started),
("cycle/judge.py", "run_judge", 'f"{verdict.reason}; it replied:', field),
("cycle/judge.py", "run_judge", "f'the judge quoted {verdict.evidence!r}, which is not in the transcript", "quotes a passage the transcript does not hold"),
("cycle/verdicts.py", "verdict_from", "'the judge returned no reply'", field),
("cycle/verdicts.py", "verdict_from", 'f"the judge\'s reply has no {name}', field),
("cycle/verdicts.py", "verdict_from", "'the judge interrupted without a question'", unasked),
("cycle/verdicts.py", "verdict_from", "'the judge quoted an answer without a question'", unasked),
("cycle/judge_reply.py", "verdict_from", "'the judge returned no reply'", field),
("cycle/judge_reply.py", "verdict_from", 'f"the judge\'s reply has no {name}', field),
("cycle/judge_reply.py", "verdict_from", "'the judge interrupted without a question'", unasked),
("cycle/judge_reply.py", "verdict_from", "'the judge quoted an answer without a question'", unasked),
]
root = pathlib.Path(main.__file__).parents[1]
found = []
for path in ("cycle/judge.py", "cycle/verdicts.py"):
for path in ("cycle/judge.py", "cycle/judge_reply.py"):
functions = [node for node in ast.walk(ast.parse((root / path).read_text())) if isinstance(node, ast.FunctionDef)]
failures = sorted((call.lineno, function.name, ast.unparse(call.args[0])) for function in functions for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) == "Failure")
found += [(path, function, reason) for _, function, reason in failures]
+1 -1
View File
@@ -670,7 +670,7 @@ class Reload(unittest.TestCase):
worker(append=["loop-claude", "\ndef\n"]), judge(),
worker(replace=["loop-claude", "\ndef\n", "\n# edited\n"]), judge(),
worker(append=["loop_claude/nested/edited.py", "# edited\n"]), judge(),
worker(append=["files/writes.py", "# edited\n"]), judge(),
worker(append=["file_writes.py", "# edited\n"]), judge(),
worker(),
])
self.assertEqual(run.code, 130)
+22
View File
@@ -0,0 +1,22 @@
import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import markdown_sections
class Sections(unittest.TestCase):
def test_keeps_every_headed_section_with_its_bodies(self):
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n## Audience\nagain\n# Appendix\nnot audience\n"
self.assertEqual(markdown_sections.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]})
def test_closes_a_fence_only_on_its_own_marker(self):
text = "## Goals\n````md\n~~~\n## Quoted\n```\n````\n ~~~\n## Also quoted\n ~~~\n1. one\n## Audience\nme\n"
self.assertEqual(list(markdown_sections.sections(text)), ["Goals", "Audience"])
self.assertEqual(list(markdown_sections.sections("```\nquoted:\n```python\n## Inside\n```\n## Goals\n1. one\n")), ["Goals"])
if __name__ == "__main__":
unittest.main()