Answer architecture round 3: 0 🔴, 1 🟡 (0 misread); every suite sits beside the module it tests, and test_loop_claude.py holds only loop-claude's own
CI / tests (pull_request) Successful in 5m17s

This commit is contained in:
2026-11-19 11:43:38 +01:00
parent df210aaeca
commit 9bc2887ee8
17 changed files with 503 additions and 380 deletions
+15
View File
@@ -0,0 +1,15 @@
"""Test doubles for the suites that run a cycle: the tmux window it runs in, and the prompts of an entry point that exists only in tests."""
from agent_cycle import tmux
from agent_cycle.worker_prompts import WorkerPrompts
WINDOW = tmux.Window("loop-proj", "item-7", "%4")
# The prompts of an entry point that exists only in tests, so a cycle's tests read no real entry point's wording.
PROMPTS = WorkerPrompts(
driver="a-driver",
fresh=lambda task, premises, route, project, facts: f"Fresh: {task}\n\n{premises}\n\n{facts}",
resumed=lambda evidence, route, project: f"Resumed: {evidence}",
rungs_held=lambda route, project: f" You hold {route.rung}.",
quote_sources="the project's records",
answer_destinations="the plan or a comment",
records_named="the project's decisions",
)
+1 -1
View File
@@ -21,7 +21,7 @@ from model_routing.ladder import Rung
from agent_cycle.stop import Stop
from agent_cycle import tmux
from text_files.utc_stamp import utc
from agent_cycle.test_tmux import WINDOW
from agent_cycle.doubles import WINDOW
class Interrupts(unittest.TestCase):
+50
View File
@@ -1,4 +1,5 @@
import dataclasses
import json
import pathlib
import sys
import unittest
@@ -10,6 +11,8 @@ from loop_effects.doubles import assistant, FakeEffects, human, judged, kinds
from failure import Failure
from agent_cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished
from agent_cycle import judge
from agent_cycle.doubles import PROMPTS
from model_routing.route import Route
from model_routing.ladder import Rung
from agent_cycle.judge import verdict_from
@@ -123,5 +126,52 @@ class Verdict(unittest.TestCase):
self.assertEqual(verdict, Unfinished("round 2: 🔴", entries_read=2))
class JudgePrompt(unittest.TestCase):
def brief(self, entries, escalated_at, answer_destinations="the plan or a comment"):
"""What the judge of a-driver reads of a transcript holding entries."""
return judge.Brief("Do it.", "## Goals", "", "the project's decisions", answer_destinations, kinds(*entries), escalated_at, "a-driver")
def test_shows_the_transcripts_tail_and_returns_what_it_showed(self):
entries = [assistant({"type": "text", "text": "old " + "x" * judge.JUDGE_PROMPT_CHARS}), assistant({"type": "text", "text": "new"})]
prompt, grounds = judge.judge_prompt(self.brief(entries, 0))
self.assertTrue(grounds.shown.endswith("\n\nnew"))
self.assertNotIn("old", grounds.shown)
self.assertIn(grounds.shown, prompt)
self.assertLessEqual(len(prompt), judge.JUDGE_PROMPT_CHARS)
self.assertEqual(grounds.after_mark, "old " + "x" * judge.JUDGE_PROMPT_CHARS + "\n\nnew")
self.assertEqual(grounds.quotable, "## Goals\n\n")
def test_the_judge_prompt_and_schema_call_the_worker_the_worker(self):
prompt, _ = judge.judge_prompt(self.brief([assistant({"type": "text", "text": "done"})], 1))
self.assertNotRegex(prompt + json.dumps(judge.judge_schema("a-driver")), r"\b[Aa]gent")
def test_the_judge_prompt_names_the_entry_points_answer_destinations_in_both_rules_on_a_recorded_answer(self):
prompt, _ = judge.judge_prompt(self.brief([assistant({"type": "text", "text": "done"})], 0, "the ledger"))
self.assertIn("is recorded in the ledger at the one place it governs", prompt)
self.assertIn("the worker writing it into the ledger, but not into one of the human's rules", prompt)
def test_shows_the_judge_where_the_worker_last_stepped_up_and_checks_an_escalation_only_after_it(self):
entries = [assistant({"type": "text", "text": "round 2: 🔴"}), assistant({"type": "text", "text": "round 3: 🔴"})]
prompt, grounds = judge.judge_prompt(self.brief(entries, 1))
self.assertRegex(prompt, "round 2: 🔴\n\n\\[step\\] [^\n]+\n\nround 3: 🔴\n\n")
self.assertEqual(grounds.after_mark, "round 3: 🔴")
prompt, grounds = judge.judge_prompt(self.brief(entries, 0))
self.assertNotIn("stepped the worker up", prompt)
self.assertEqual(grounds.shown, "round 2: 🔴\n\nround 3: 🔴")
self.assertEqual(grounds.after_mark, "round 2: 🔴\n\nround 3: 🔴")
def test_drops_an_escalation_quote_from_after_the_step_up_that_the_tail_it_shows_cuts_off(self):
entries = [assistant({"type": "text", "text": "round 2: 🔴"}), assistant({"type": "text", "text": "old " + "x" * judge.JUDGE_PROMPT_CHARS}), assistant({"type": "text", "text": "new"})]
_, grounds = judge.judge_prompt(self.brief(entries, 1))
self.assertEqual(judge.quotes_checked(Unfinished("e", "old x", entries_read=3), grounds), Unfinished("e", entries_read=3))
self.assertEqual(judge.quotes_checked(Unfinished("e", "new", entries_read=3), grounds), Unfinished("e", "new", entries_read=3))
def test_shows_a_typed_answer_before_the_step_up_as_the_humans(self):
typed = PROMPTS.typed("Which one?", "Use B.", Route(Rung("worker-model")), None, False)
entries = [{**human(typed), "promptSource": "sdk"}, assistant({"type": "text", "text": "round 3: 🔴"})]
_, grounds = judge.judge_prompt(self.brief(entries, 1))
self.assertIn("\n\n[human] Use B.\n\n[step] ", grounds.shown)
if __name__ == "__main__":
unittest.main()
+94 -4
View File
@@ -1,19 +1,109 @@
import dataclasses
import pathlib
import sys
import types
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from agent_cycle.verdicts import Interrupt, TakenIn, Unfinished
from model_routing.ladder import ProjectRung, Rung
from model_routing.ladder_file import read_ladder, SHIPPED
from model_routing.route import Route
from agent_cycle import routing
from model_routing.ladder import Rung
LADDER = read_ladder(SHIPPED)
OPUS_PROJECT = ProjectRung(LADDER, Rung("claude-opus-5-5", "medium"))
# The ladder's rungs around the project's: the one below it, its own, and those one to four steps up.
SONNET = Rung("claude-sonnet-5-5", "high")
OPUS = Rung("claude-opus-5-5", "medium")
ONE_UP = Rung("claude-fable-5-2", "low")
TWO_UP = Rung("claude-fable-5-2", "medium")
THREE_UP = Rung("claude-opus-5-5", "xhigh")
FOUR_UP = Rung("claude-fable-5-2", "high")
def scored(likelihood, escape_and_impact):
"""Item 3 with that L and E + I, as routing.Scored reads an item."""
return types.SimpleNamespace(id=3, likelihood=likelihood, escape_and_impact=escape_and_impact)
def unfinished(likelihood=0, escalated_at=None):
"""An unfinished verdict: the worker took in an item of that L, 0 for none, and the judge found an escalation after reading escalated_at transcript entries, None for none."""
return Unfinished("e", escalation="round 2: 🔴" if escalated_at is not None else "", taken_in=TakenIn(highest_likelihood=likelihood), entries_read=escalated_at)
def interrupted(likelihood=0, escalated_at=None):
"""An interrupt, its arguments as unfinished's."""
return Interrupt("e", "q", escalation="round 2: 🔴" if escalated_at is not None else "", taken_in=TakenIn(highest_likelihood=likelihood), entries_read=escalated_at)
class FirstRoute(unittest.TestCase):
def test_starts_a_chunk_on_the_rung_its_items_l_picks_below_the_projects_too(self):
self.assertEqual(routing.first_route(OPUS_PROJECT, scored(4, 5)), Route(TWO_UP, 3, 4, 5))
self.assertEqual(routing.first_route(OPUS_PROJECT, scored(1, 5)), Route(SONNET, 3, 1, 5))
self.assertEqual(routing.first_route(OPUS_PROJECT, None), Route(OPUS))
class NextRoute(unittest.TestCase):
def test_steps_up_a_rung_after_every_second_unfinished_verdict(self):
once = routing.next_route(OPUS_PROJECT, Route(ONE_UP, 3, 3, 4), unfinished())
self.assertEqual(once, Route(ONE_UP, 3, 3, 4, unfinished=1))
self.assertEqual(routing.next_route(OPUS_PROJECT, once, unfinished()), Route(TWO_UP, 3, 3, 4))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(LADDER.rungs[-1], unfinished=1), unfinished()).rung, LADDER.rungs[-1])
def test_steps_up_to_the_rung_a_taken_in_items_l_picks_and_never_down(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(OPUS), unfinished(4)).rung, TWO_UP)
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(THREE_UP), unfinished(4)).rung, THREE_UP)
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(OPUS, unfinished=1), unfinished(4)).rung, THREE_UP)
def test_steps_up_from_a_rung_below_the_projects_one_rung_at_a_time(self):
low = Route(SONNET, 3, 1, 3, unfinished=1)
self.assertEqual(routing.next_route(OPUS_PROJECT, low, unfinished()).rung, OPUS)
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(low.rung, 3, 1, 3), unfinished()).rung, low.rung)
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(low.rung, 3, 1, 3), unfinished(3)).rung, ONE_UP)
def test_steps_up_a_rung_on_an_escalation_the_judge_found_up_to_two_above_the_items_rung_marking_the_transcript_entries_it_read(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP, 3, 4, 5), unfinished(escalated_at=7)), Route(THREE_UP, 3, 4, 5, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(THREE_UP, 3, 4, 5, escalated_at=7), unfinished(escalated_at=9)), Route(FOUR_UP, 3, 4, 5, escalated_at=9))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(FOUR_UP, 3, 4, 5, escalated_at=9), unfinished(escalated_at=12)), Route(FOUR_UP, 3, 4, 5, unfinished=1, escalated_at=9))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP, 3, 4, 5, unfinished=1), unfinished(escalated_at=7)), Route(THREE_UP, 3, 4, 5, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP), unfinished(escalated_at=7)), Route(TWO_UP, unfinished=1))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(SONNET, 3, 2, 4), unfinished(4, escalated_at=7)), Route(TWO_UP, 3, 2, 4, unfinished=1))
def test_keeps_the_mark_through_a_step_no_escalation_took(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(ONE_UP, 3, 3, 4, unfinished=1, escalated_at=5), unfinished()), Route(TWO_UP, 3, 3, 4, escalated_at=5))
def test_steps_up_a_rung_as_the_judge_interrupts_up_to_two_above_the_items_rung_without_counting_an_unfinished_verdict(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP, 3, 4, 5, unfinished=1), interrupted(escalated_at=7)), Route(THREE_UP, 3, 4, 5, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(THREE_UP, 3, 4, 5, escalated_at=7), interrupted(escalated_at=9)), Route(FOUR_UP, 3, 4, 5, escalated_at=9))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(FOUR_UP, 3, 4, 5, unfinished=1, escalated_at=9), interrupted(escalated_at=12)), Route(FOUR_UP, 3, 4, 5, unfinished=1, escalated_at=9))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(SONNET, 3, 2, 4), interrupted(escalated_at=7)), Route(OPUS, 3, 2, 4, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP, 3, 4, 5, unfinished=1, escalated_at=2), interrupted(4)), Route(TWO_UP, 3, 4, 5, unfinished=1, escalated_at=2))
def test_raises_the_worker_to_a_taken_in_items_rung_above_the_escalation_cap_without_a_step_as_the_judge_interrupts(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(OPUS, 3, 1, 3, escalated_at=2), interrupted(4, escalated_at=7)), Route(TWO_UP, 3, 1, 3, escalated_at=2))
def test_steps_up_from_the_rung_a_taken_in_item_picks_as_the_judge_interrupts(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(ONE_UP, 3, 3, 4), interrupted(4, escalated_at=7)), Route(THREE_UP, 3, 3, 4, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(TWO_UP, 3, 4, 5), interrupted(4, escalated_at=7)), Route(THREE_UP, 3, 4, 5, escalated_at=7))
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(FOUR_UP, 3, 4, 5, unfinished=1), interrupted(4, escalated_at=7)), Route(FOUR_UP, 3, 4, 5, unfinished=1))
def test_a_rung_an_edit_took_off_the_ladder_counts_as_the_projects(self):
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(Rung("claude-opus-5-5", "low")), unfinished()).rung, OPUS)
self.assertEqual(routing.next_route(OPUS_PROJECT, Route(Rung("claude-opus-5-5", "low")), unfinished(2)).rung, OPUS)
class StepUp(unittest.TestCase):
OPUS = Rung("claude-opus-5-5", "medium")
def test_names_no_step_up_where_only_the_count_of_unfinished_verdicts_changed(self):
route = Route(self.OPUS, 3, 3, 4)
route = Route(OPUS, 3, 3, 4)
self.assertIsNone(routing.step_up(route, dataclasses.replace(route, unfinished=1)))
self.assertEqual(routing.step_up(route, route.taking_in(6)), routing.StepUp(route, route.taking_in(6)))
class JudgeRung(unittest.TestCase):
def test_runs_the_judge_of_a_replayed_model_on_its_models_highest_rung(self):
self.assertEqual(routing.judge_rung(LADDER, "claude-opus-5-5"), THREE_UP)
# A model off the ladder, or not replayed, gets no effort flag.
self.assertEqual(routing.judge_rung(LADDER, "judge-model"), Rung("judge-model"))
self.assertEqual(routing.judge_rung(LADDER, "claude-sonnet-5-5"), Rung("claude-sonnet-5-5"))
+1 -12
View File
@@ -23,6 +23,7 @@ from transcript_archive import outcomes, write
from loop_state import paths
from transcript_archive.location import project_archive
from model_routing.ladder import ProjectRung, Rung
from agent_cycle.doubles import PROMPTS, WINDOW
from model_routing.ladder_file import read_ladder, SHIPPED
from model_routing.route import Route
from agent_cycle import routing
@@ -31,8 +32,6 @@ from claude_code import session
from loop_effects.child_shells import Shell
from agent_cycle.stop import Stop
from text_files.utc_stamp import utc
from agent_cycle.test_tmux import WINDOW
from agent_cycle.worker_prompts import WorkerPrompts
LADDER = read_ladder(SHIPPED)
@@ -51,16 +50,6 @@ SONNET_179 = '{"model": "claude-sonnet-5-5", "effort": "high", "item": 179, "lik
OPUS_179_AT_3 = '{"model": "claude-opus-5-5", "effort": "medium", "item": 179, "likelihood": 2, "escape_and_impact": 5, "escalated_at": 3}'
OPUS_179_AT_6 = '{"model": "claude-opus-5-5", "effort": "medium", "item": 179, "likelihood": 2, "escape_and_impact": 5, "escalated_at": 6}'
JUDGE_RUN_LINE = re.compile(r"^Judge run: judge/[0-9a-f]{64}\.json\n", re.MULTILINE)
# The prompts of an entry point that exists only here, so a cycle's tests read no real entry point's wording.
PROMPTS = WorkerPrompts(
driver="a-driver",
fresh=lambda task, premises, route, project, facts: f"Fresh: {task}\n\n{premises}\n\n{facts}",
resumed=lambda evidence, route, project: f"Resumed: {evidence}",
rungs_held=lambda route, project: f" You hold {route.rung}.",
quote_sources="the project's records",
answer_destinations="the plan or a comment",
records_named="the project's decisions",
)
class QuestionFor(unittest.TestCase):
+1 -3
View File
@@ -9,9 +9,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from loop_effects.doubles import FakeEffects
from agent_cycle import tmux
WINDOW = tmux.Window("loop-proj", "item-7", "%4")
from agent_cycle.doubles import WINDOW
class TmuxCommands(unittest.TestCase):
+20 -1
View File
@@ -10,7 +10,7 @@ import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from claude_code import entries
from claude_code import entries, session
from failure import Failure
from loop_effects.doubles import FakeEffects, NOW, ran, Streamed
from agent_cycle import judge, lingering_workers, worker
@@ -131,5 +131,24 @@ class MemoryWait(unittest.TestCase):
self.assertEqual([seam for seam, _ in fake.calls], ["memory", "stream_claude"])
class WorkerArgs(unittest.TestCase):
OPENED = session.session_named("A proj a-driver", pathlib.Path("/h/.claude/projects"))
def test_opens_a_fresh_session_by_name_and_resumes_an_open_one(self):
fresh = worker.worker_args(self.OPENED, Rung("m"), "Do it.", resume=False)
self.assertEqual(fresh[fresh.index("--session-id") + 1], str(self.OPENED.id))
self.assertEqual(fresh[fresh.index("--name") + 1], "A proj a-driver")
resumed = worker.worker_args(self.OPENED, Rung("m"), "Continue the task.", resume=True)
self.assertEqual(resumed[resumed.index("--resume") + 1], str(self.OPENED.id))
self.assertNotIn("--session-id", resumed)
self.assertEqual(resumed[resumed.index("-p") + 1], "Continue the task.")
def test_disallows_a_wakeup_so_a_worker_that_ends_its_turn_exits(self):
for resume in (False, True):
with self.subTest(resume=resume):
args = worker.worker_args(self.OPENED, Rung("m"), "Do it.", resume=resume)
self.assertEqual(args[args.index("--disallowedTools") + 1], "ScheduleWakeup")
if __name__ == "__main__":
unittest.main()
+33
View File
@@ -0,0 +1,33 @@
import dataclasses
import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from agent_cycle.doubles import PROMPTS
from model_routing.route import Route
from model_routing.ladder import Rung
from agent_cycle import worker_prompts
class TypedAnswers(unittest.TestCase):
def test_splits_the_prompt_handing_over_the_answer_into_the_answer_on_the_line_after_its_marker_and_the_rest_and_no_other_prompt(self):
route = Route(Rung("worker-model"))
marked = "The human answered your question at a-driver's terminal:\nB"
typed = worker_prompts.typed_answer(PROMPTS.typed("Which one?", "Use B.", route, None, False))
self.assertEqual(typed.answer, "Use B.")
self.assertTrue(typed.loop_words.startswith("Your question was: Which one?\n\nContinue the task."))
self.assertEqual(worker_prompts.typed_answer(PROMPTS.typed(f"Which one?\n{marked}", "Use B.", route, None, False)).answer, "Use B.")
# A ruling that stepped the worker up adds the sentences naming its new rung.
self.assertEqual(worker_prompts.typed_answer(PROMPTS.typed("Which one?", "Use B.", Route(Rung("claude-sonnet-5-5", "high"), 179, 2, 5), None, True)).answer, "Use B.")
self.assertIsNone(worker_prompts.typed_answer(PROMPTS.answered("Which one?", marked, route, None, False)))
self.assertIsNone(worker_prompts.typed_answer(PROMPTS.resumed(marked, route, None)))
def test_the_typed_answers_prompt_names_the_entry_points_answer_destinations(self):
ledger = dataclasses.replace(PROMPTS, answer_destinations="the ledger")
self.assertIn("is recorded in the ledger at the one place it governs.", ledger.typed("Which one?", "Use B.", Route(Rung("worker-model")), None, False))
if __name__ == "__main__":
unittest.main()