999 lines
67 KiB
Python
999 lines
67 KiB
Python
import ast
|
|
import contextlib
|
|
import dataclasses
|
|
import io
|
|
import json
|
|
import pathlib
|
|
import re
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from chunk_driver.doubles import a_loop
|
|
from routing_figures import archive, state
|
|
from test_env import assert_not_root
|
|
from loop_effects.doubles import assistant, CWD, FakeEffects, NOW, ran, said, STAMP
|
|
from claude_code.output_limits import BASH_CEILING_CHARACTERS
|
|
from routing_figures.calls import Call
|
|
from loop_claude.chunk import chunk_items
|
|
from chunk_driver import chunk_run, prompts, this_loop
|
|
from loop_claude.loop import chunk_start, chunk_windows
|
|
from project_config.test_project_config import configured
|
|
from release_plan.release_standing import DefaultPlan, LastCut, UNSEEN_RELEASES
|
|
from claude_code import entries, reply, session
|
|
from failure import Failure
|
|
from agent_cycle.verdicts import Finished, TakenIn, Unfinished
|
|
from loop_claude.chunk import fresh_prompt
|
|
from agent_cycle import judge, retry, states, worker
|
|
import loop_claude.routed_item
|
|
from loop_claude.loop import main, reload, routing_check, usage
|
|
from release_plan import next_chunk
|
|
from transcript_archive import outcomes
|
|
from loop_state import paths
|
|
from plan_store.todo_md import plan_of
|
|
from model_routing.ladder import ProjectRung, Rung
|
|
from loop_effects.effects import PYTHON_TIMEOUT
|
|
from model_routing.ladder_file import read_ladder, SHIPPED
|
|
from chunk_driver.project import records
|
|
from model_routing.route import Route
|
|
from agent_cycle.stop import Stop
|
|
from agent_cycle import tmux
|
|
from touch_predictions.test_touch_predictions import plan as touch_plan
|
|
from text_files.utc_stamp import utc
|
|
|
|
LADDER = read_ladder(SHIPPED)
|
|
OPUS_PROJECT = ProjectRung(LADDER, Rung("claude-opus-5-5", "medium"))
|
|
|
|
# How git answers in a project's checkout it cannot read, such as one that is gone.
|
|
UNREAD = Failure("not a git repository")
|
|
|
|
|
|
class RoutingCheck(unittest.TestCase):
|
|
def setUp(self):
|
|
self.home = pathlib.Path(tempfile.mkdtemp())
|
|
self.addCleanup(shutil.rmtree, self.home)
|
|
self.root = self.home / "archive"
|
|
self.paths = paths.paths_from(self.home)
|
|
self.session = session.session_named("A log loop-claude", self.paths.claude_code_transcripts)
|
|
|
|
def loop(self, fake):
|
|
"""The loop in /code/log."""
|
|
return a_loop(fake.effects(), root="/code/log", session=self.session, paths=self.paths)
|
|
|
|
def chunks(self, count, verdicts, project="log"):
|
|
"""count finished chunks, each closed by verdicts verdicts, each with one Explore subagent whose report outgrew its searches."""
|
|
for number in range(count):
|
|
folder = self.root / project / f"c{number}"
|
|
subagents = folder / "session" / "s" / "subagents"
|
|
subagents.mkdir(parents=True)
|
|
kept = "".join(f"2026-10-14T10:0{verdict}:00Z {outcomes.unfinished('w', 'e', 'j', '')}\n" for verdict in range(verdicts - 1))
|
|
(folder / "copy.outcomes.md").write_text(f"Task: t\n\n{kept}2026-10-14T11:00:00Z {outcomes.finished('w', 'm', 'j')}\n")
|
|
launch = {"type": "assistant", "timestamp": "2026-10-14T09:00:00Z", "message": {"id": "m", "content": [{"type": "tool_use", "id": "a", "name": "Agent", "input": {"subagent_type": "Explore"}}]}}
|
|
report = {"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": "a", "content": "long report"}]}}
|
|
(folder / "copy.jsonl").write_text(json.dumps(launch) + "\n" + json.dumps(report) + "\n")
|
|
(subagents / "agent-a.jsonl").write_text("")
|
|
(subagents / "agent-a.meta.json").write_text(json.dumps({"agentType": "Explore", "toolUseId": "a"}))
|
|
|
|
def check(self, fake):
|
|
"""What check_routing returns and says for a loop in /code/log."""
|
|
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said:
|
|
stop = routing_check.check_routing(self.root, Rung("m", "high"), self.loop(fake))
|
|
return stop, said.getvalue()
|
|
|
|
def test_measures_once_20_cycles_have_closed_since_the_last_measurement(self):
|
|
self.chunks(5, 3)
|
|
self.assertEqual(self.check(FakeEffects()), (None, ""))
|
|
self.assertFalse((self.root / "routing-figures.md").exists())
|
|
self.chunks(6, 1, "other")
|
|
self.assertEqual(self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 6))[0], None)
|
|
self.assertIn("at cycle 21 across", (self.root / "routing-figures.md").read_text())
|
|
|
|
def test_reads_each_projects_history_through_the_loops_git_and_names_each_it_cannot_read(self):
|
|
self.chunks(5, 4)
|
|
fake = FakeEffects(on_terminal=False, git=[UNREAD] * 3)
|
|
self.check(fake)
|
|
located = ["rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir"]
|
|
self.assertEqual(fake.made("git"), [located, located, ["remote"]], "the config is located for its title templates, then for where it keeps the plan")
|
|
self.assertIn("## Default branches unread\n\n- log: not a git repository\n", (self.root / "routing-figures.md").read_text())
|
|
|
|
def test_hands_the_terminal_over_once_for_each_new_call(self):
|
|
self.chunks(5, 4)
|
|
fake = FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3)
|
|
stop, said = self.check(fake)
|
|
self.assertIsNone(stop)
|
|
call = "Explore subagents' reports took more of the worker's context than their searches would have."
|
|
self.assertIn(f"- {call}", said)
|
|
self.assertIn(f"- {call}", (self.root / "routing-figures.md").read_text())
|
|
((args),) = fake.made("claude_on_terminal")
|
|
self.assertEqual(args[:6], ["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions"])
|
|
self.assertIn(f"don't change anything until they answer:\n- {call}", args[6])
|
|
self.assertEqual(fake.terminal_environments, [{"CLAUDE_CODE_SANDBOXED": "1"}])
|
|
self.assertEqual(fake.terminal_folders, [str(self.home / "code" / "claude-config")], "the development clone, since ~/.claude holds only a release")
|
|
self.assertIn("Record what they decide in this checkout, the development clone", args[6])
|
|
self.assertIsNone(state.mark_measured(self.root, 0))
|
|
self.assertEqual(self.check(FakeEffects(git=[UNREAD] * 3))[0], None)
|
|
self.assertNotIn(call, self.check(FakeEffects())[1])
|
|
|
|
def test_stamps_the_figures_and_the_calls_raised_when_it_measures(self):
|
|
self.chunks(5, 4)
|
|
(self.root / "routing-calls-raised.md").write_text("# Routing calls raised\n\n- 2026-10-14T10:02:00Z Reset\n")
|
|
self.check(FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3))
|
|
figures = (self.root / "routing-figures.md").read_text()
|
|
self.assertIn(f"Measured {utc(NOW)}, at cycle 20 across", figures)
|
|
self.assertIn("that finished after the reset of 2026-10-14T10:02:00Z", figures)
|
|
self.assertIn(f"- {utc(NOW)} Explore subagents'", (self.root / "routing-calls-raised.md").read_text())
|
|
|
|
def test_says_a_figure_it_cannot_read(self):
|
|
self.chunks(5, 4)
|
|
(self.root / "routing-state.json").mkdir()
|
|
self.assertIn("routing figures: could not read", self.check(FakeEffects())[1])
|
|
|
|
def test_goes_on_off_a_terminal_and_raises_the_call_again_next_time(self):
|
|
self.chunks(5, 4)
|
|
stop, said = self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3))
|
|
self.assertIsNone(stop)
|
|
self.assertIn("Not on a terminal: the calls are raised again at the next measurement.", said)
|
|
self.assertIsNone(state.mark_measured(self.root, 0))
|
|
self.assertIn("Not on a terminal", self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3))[1])
|
|
|
|
def test_leaves_the_measuring_to_a_loop_holding_the_lock(self):
|
|
self.chunks(5, 4)
|
|
with archive.locked(self.root):
|
|
self.assertEqual(self.check(FakeEffects()), (None, ""))
|
|
|
|
def test_stops_when_claude_cannot_start_for_the_calls(self):
|
|
self.chunks(5, 4)
|
|
stop, _ = self.check(FakeEffects(on_terminal_claude=[OSError(2, "No such file or directory")], git=[UNREAD] * 3))
|
|
self.assertEqual(stop, Stop(1, f"could not start claude for the routing calls in {self.home / 'code' / 'claude-config'}, the setup's development clone: No such file or directory"))
|
|
|
|
def test_says_a_figure_it_cannot_write_and_goes_on(self):
|
|
assert_not_root(self)
|
|
self.chunks(5, 4)
|
|
(self.root / "routing-figures.lock").touch()
|
|
self.root.chmod(0o555)
|
|
self.addCleanup(self.root.chmod, 0o755)
|
|
stop, said = self.check(FakeEffects(git=[UNREAD] * 3))
|
|
self.assertIsNone(stop)
|
|
self.assertIn("routing figures: could not write", said)
|
|
|
|
def test_puts_every_new_call_to_the_human_and_says_one_it_cannot_keep(self):
|
|
new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))]
|
|
fake = FakeEffects(on_terminal_claude=[0])
|
|
with contextlib.redirect_stderr(io.StringIO()) as said, unittest.mock.patch.object(state, "mark_raised", return_value=Failure("full")):
|
|
self.assertIsNone(routing_check.raise_calls(self.root, new, Rung("m", "high"), self.loop(fake)))
|
|
((args),) = fake.made("claude_on_terminal")
|
|
self.assertIn(f"don't change anything until they answer:\n- {new[0].sentence()}\n- {new[1].sentence()}\nThe figures", args[6])
|
|
self.assertIn(f"call for a routing change:\n- {new[0].sentence()}\n- {new[1].sentence()}\n", said.getvalue())
|
|
self.assertIn("routing figures: full", said.getvalue())
|
|
[sent] = fake.dms
|
|
self.assertTrue(sent.startswith(f"loop-claude in {self.loop(fake).root} asks:\n\nloop-claude's routing figures call for a routing change:\n- {new[0].sentence()}\n"), sent)
|
|
|
|
|
|
# A plan read with no release cut yet.
|
|
NO_CUT = LastCut(None, "", 0, [])
|
|
def routed_item(todo, cut):
|
|
"""loop_claude.routed_item.routed_item for the plan a todo.md's text holds."""
|
|
return loop_claude.routed_item.routed_item(plan_of(todo), cut)
|
|
|
|
|
|
TODO = """# todo
|
|
|
|
## Items
|
|
|
|
| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score |
|
|
|---|---|---|---|---|---|---|---|---|---|---|
|
|
| 7 | 0.2.0 | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |
|
|
| 3 | 0.2.0 | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 |
|
|
| 1 | 0.3.0 | | **Do a later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |
|
|
"""
|
|
|
|
|
|
class RoutedItem(unittest.TestCase):
|
|
def test_routes_on_the_next_releases_top_item_that_waits_on_no_question(self):
|
|
self.assertEqual(routed_item(TODO, NO_CUT).id, 3)
|
|
self.assertIsNone(routed_item(TODO.replace("| | **Do", "| question | **Do"), NO_CUT))
|
|
self.assertIsNone(routed_item("# todo\n", NO_CUT))
|
|
self.assertIsNone(routed_item(TODO[:TODO.index("| 7 |")], NO_CUT))
|
|
old_shape = "## Items\n\n| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 3 | 0.2.0 | | **Do the next thing.** | 5 | 3 | 6 | 8 | 9 | 1 | 25 |\n"
|
|
self.assertIsNone(routed_item(old_shape, NO_CUT))
|
|
self.assertEqual(routed_item(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), NO_CUT), Failure("item 3's L 'x' is not a whole number from 1 to 4"))
|
|
|
|
def test_routes_on_no_item_where_the_release_after_the_last_cut_has_none(self):
|
|
self.assertEqual(routed_item(TODO, LastCut("v0.1.0", "", 3, [TODO])).id, 3)
|
|
# Someone cut 0.2.0 while it held items: they count as 0.3.0's, the next release, and where no later release is known, no item routes.
|
|
self.assertEqual(routed_item(TODO, LastCut("v0.2.0", "", 3, [TODO])).id, 3)
|
|
self.assertIsNone(routed_item(re.sub(r"(?m)^\| \d+ \| 0\.3\.0 \|.*\n", "", TODO), LastCut("v0.2.0", "", 3, [])))
|
|
later_only = TODO[:TODO.index("| 7 |")] + TODO[TODO.index("| 1 | 0.3.0 |"):]
|
|
self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 3, [TODO])))
|
|
self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 0, [TODO])))
|
|
self.assertEqual(routed_item(later_only, LastCut("v0.2.0", "", 3, [later_only])).id, 1)
|
|
|
|
def test_routes_past_an_item_a_waiting_item_blocks(self):
|
|
todo = "## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n| 8 | 0.2.0 | 7 | | **Use the answer.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 9 | 0.2.0 | 8 | | **Build on it.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 3 | 0.2.0 | | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 |\n"
|
|
self.assertEqual(routed_item(todo, NO_CUT).id, 3)
|
|
self.assertEqual(routed_item(todo.replace("| question |", "| |"), NO_CUT).id, 7)
|
|
first = "| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n"
|
|
self.assertEqual(routed_item(todo.replace(first, "") + first, NO_CUT).id, 3)
|
|
|
|
def test_routes_nothing_on_an_item_without_e_or_i(self):
|
|
without_i = TODO.replace("| E | I | R |", "| E | R |").replace("|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|").replace("| 4 | 3 | 5 | 10 |", "| 4 | 3 | 10 |").replace("| 4 | 2 | 3 | 7 |", "| 4 | 2 | 7 |").replace("| 1 | 1 | 1 | 1 |", "| 1 | 1 | 1 |")
|
|
self.assertIsNone(routed_item(without_i, NO_CUT))
|
|
|
|
|
|
class RouteSources(unittest.TestCase):
|
|
def test_routes_on_the_plan_item_and_says_why_it_routes_on_the_projects_rung(self):
|
|
self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(""), NO_CUT), None, FakeEffects().effects()))
|
|
self.assertEqual(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO])), None, FakeEffects().effects()).id, 3)
|
|
self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.3.0", "", 3, [TODO])), None, FakeEffects().effects()))
|
|
|
|
def test_routes_a_window_named_for_an_item_on_that_item_whatever_the_plan_takes_next(self):
|
|
plan = DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO]))
|
|
later = next(item.id for item in plan.plan.items if item.id != 3)
|
|
self.assertEqual(chunk_start.plan_item(plan, later, FakeEffects().effects()).id, later)
|
|
# An item that left the plan, as once it shipped.
|
|
self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects()))
|
|
# A chunk start reads the default branch once, for routing and for scoring the items the worker names.
|
|
with unittest.mock.patch.object(chunk_start, "session_open", return_value=True), unittest.mock.patch.object(chunk_start, "fetched_plan", return_value=plan) as fetched:
|
|
loop = a_loop(FakeEffects().effects())
|
|
self.assertEqual(chunk_start.chunk_start(loop), ("", plan))
|
|
# An open session is no fresh chunk, so the human isn't told again of releases the plan cannot show.
|
|
fetched.assert_called_once_with(loop, False)
|
|
unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8")
|
|
for plan, said in ((DefaultPlan(plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), NO_CUT), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan(unreadable, NO_CUT), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")):
|
|
with self.subTest(said=said):
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
self.assertIsNone(chunk_start.plan_item(plan, None, FakeEffects().effects()))
|
|
self.assertTrue(stderr.getvalue().startswith(f"{STAMP} {said}"), stderr.getvalue())
|
|
|
|
def test_says_why_the_items_the_worker_names_lift_no_rung(self):
|
|
unscored = plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"))
|
|
for default, said in ((DefaultPlan(unscored, NO_CUT), "the items the worker names lift no rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (Failure("no git"), "the items the worker names lift no rung: no git\n"), (DefaultPlan(plan_of(TODO), NO_CUT), "")):
|
|
with self.subTest(said=said):
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
scored = chunk_start.scored_plan(default, FakeEffects().effects())
|
|
self.assertEqual(stderr.getvalue(), f"{STAMP} {said}" if said else "")
|
|
self.assertEqual(scored, default.plan if isinstance(default, DefaultPlan) else default)
|
|
|
|
def test_reads_the_changelog_the_default_branch_holds_through_the_loops_git(self):
|
|
def git(*args, **options):
|
|
answers = {"remote": "origin\n", "symbolic-ref": "origin/main\n", "show": f"what {args[-1]} holds\n"}
|
|
return answers.get(args[0], "1\n")
|
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
folder = configured(self, pathlib.Path(root))
|
|
effects = dataclasses.replace(FakeEffects().effects(), git=git)
|
|
held = chunk_start.fetched_plan(a_loop(effects, root=root, paths=paths.paths_from(folder.parents[1])))
|
|
self.assertEqual(held.cut.changelog, "what origin/main:CHANGELOG.md holds\n")
|
|
|
|
def test_reads_a_plan_outside_the_checkout_as_it_stands_and_stops_without_a_config(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
folder = configured(self, pathlib.Path(root))
|
|
loop = a_loop(FakeEffects(git=["origin\n", "origin/main\n"] * 2).effects(), root=root, paths=paths.paths_from(folder.parents[1]))
|
|
config = folder / "config.toml"
|
|
tracked = a_loop(FakeEffects(git=["origin\n", "origin/main\n", *[Failure("no git")] * 4]).effects(), root=root, paths=paths.paths_from(folder.parents[1]))
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
chunk_start.fetched_plan(tracked)
|
|
self.assertEqual(stderr.getvalue(), "", "a plan git tracks hides no release")
|
|
config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))
|
|
(folder / "todo.md").write_text(TODO)
|
|
unread = Failure("no git")
|
|
standing = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1]))
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
held = chunk_start.fetched_plan(standing)
|
|
self.assertEqual((held.plan, held.cut.todos_since, held.cut.changelog), (plan_of(TODO), [], ""), "the plan as it stands, whatever git answers")
|
|
self.assertEqual(stderr.getvalue(), f"{STAMP} {UNSEEN_RELEASES}\n", "a release review loop-claude cannot call due is never skipped silently")
|
|
quiet = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1]))
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
chunk_start.fetched_plan(quiet, announce=False)
|
|
self.assertEqual(stderr.getvalue(), "", "said once per chunk, as it starts")
|
|
config.unlink()
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
self.assertIn("no config names the checkout", chunk_start.scored_plan(chunk_start.fetched_plan(loop), loop.effects).reason)
|
|
self.assertIn("the items the worker names lift no rung: no config names the checkout", stderr.getvalue())
|
|
self.assertIn("no config names the checkout", chunk_start.fetched_plan(loop).reason)
|
|
|
|
|
|
class ChunkWindows(unittest.TestCase):
|
|
def plan(self, *touched):
|
|
"""A plan of items 1, 2 and on, each predicted to touch the path given for it."""
|
|
items = touch_plan(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1)))
|
|
return next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="")
|
|
|
|
def test_starts_the_next_item_and_those_picked_beside_it_up_to_the_limit(self):
|
|
# Item 2 overlaps item 1, so it waits for it; items 3 and 4 run beside it.
|
|
plan = self.plan("a.py", "a.py", "c.py", "d.py")
|
|
self.assertEqual(chunk_windows.may_start(plan, [], set(), 1), ["item-1"])
|
|
self.assertEqual(chunk_windows.may_start(plan, [], set(), 2), ["item-1", "item-3"])
|
|
self.assertEqual(chunk_windows.may_start(plan, [], set(), 9), ["item-1", "item-3", "item-4"])
|
|
|
|
def test_leaves_the_items_running_chunks_hold_and_starts_only_as_many_as_the_room_left(self):
|
|
plan = self.plan("a.py", "a.py", "c.py", "d.py")
|
|
self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1, 3}, 1), ["item-4"])
|
|
self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 2), ["item-3", "item-4"])
|
|
self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 0), [])
|
|
|
|
def test_counts_the_running_chunks_against_the_limit_and_restarts_stopped_ones_first(self):
|
|
windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True), tmux.Listed("item-4", "", True)]
|
|
self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 3), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 0))
|
|
self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3"], 0))
|
|
self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 5), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 2))
|
|
|
|
def test_a_loop_whose_chunk_finished_leaves_its_room_and_restarts_no_stopped_chunk(self):
|
|
windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True)]
|
|
self.assertEqual(chunk_windows.room_of(windows, "item-1", True, 2), chunk_windows.Room([], ["item-3"], [], 2))
|
|
|
|
def test_leaves_no_room_where_more_chunks_run_than_the_limit(self):
|
|
windows = [tmux.Listed(f"item-{n}", "", False) for n in (1, 2, 3)]
|
|
self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2).left, 0)
|
|
|
|
def test_starts_no_item_that_overlaps_a_running_chunks_items(self):
|
|
# Item 1 merged, so 2 is the plan's next item; it overlaps item 3, which still runs, at c.py.
|
|
items = touch_plan((2, "1.0.0", "", "- `a.py`\n- `c.py`"), (3, "1.0.0", "", "- `c.py`"), (4, "1.0.0", "", "- `d.py`"))
|
|
plan = next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="")
|
|
self.assertEqual(chunk_windows.may_start(plan, ["item-3"], {3}, 2), ["item-4"])
|
|
# A running chunk's item without a prediction leaves no other item free to start beside it.
|
|
unpredicted = touch_plan((2, "1.0.0", "", "- `a.py`"), (3, "1.0.0", "", None))
|
|
self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=unpredicted, first=unpredicted[0], text="", branch_template=""), ["item-3"], {3}, 2), [])
|
|
# A held item that left the plan, as once it merged, overlaps nothing.
|
|
self.assertEqual(chunk_windows.may_start(plan, ["item-9"], {9}, 2), ["item-2", "item-4"])
|
|
|
|
def test_a_plan_without_an_item_starts_one_chunk_and_only_alone(self):
|
|
none = next_chunk.NoItem("release 1.0.0 has no items")
|
|
self.assertEqual(chunk_windows.may_start(none, [], set(), 3), ["no-item"])
|
|
self.assertEqual(chunk_windows.may_start(none, ["item-1"], {1}, 3), [])
|
|
|
|
def test_names_a_window_for_its_item_and_reads_the_item_back(self):
|
|
self.assertEqual(chunk_windows.window_name(372), "item-372")
|
|
self.assertEqual([chunk_windows.item_of(chunk) for chunk in ("item-372", "no-item", "item-", "item-3x", "xitem-3")], [372, None, None, None, None])
|
|
# Only ASCII digits name an item, as window_name writes them.
|
|
self.assertIsNone(chunk_windows.item_of("item-\u0663"))
|
|
|
|
def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self):
|
|
with unittest.mock.patch.object(chunk_windows, "chunks_beside", return_value="item 1's Touches: list cannot be read"):
|
|
self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"])
|
|
|
|
def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
transcripts = pathlib.Path(temp)
|
|
folder = transcripts / "-r"
|
|
folder.mkdir()
|
|
# A folder in place of the transcript file, so reading it fails.
|
|
(folder / f"{chunk_windows.chunk_session('proj', 'item-7', transcripts).id}.jsonl").mkdir()
|
|
self.assertEqual(chunk_windows.held_items(["item-7"], "proj", transcripts), {7})
|
|
|
|
def test_tells_a_chunk_the_items_the_projects_other_live_chunks_hold(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
home = pathlib.Path(temp)
|
|
held = paths.paths_from(home)
|
|
folder = held.claude_code_transcripts / "-r"
|
|
folder.mkdir(parents=True)
|
|
lines = [assistant({"type": "text", "text": "Chunk items: 8 (L 1, E + I 2), 11 (L 2, E + I 3)"})]
|
|
(folder / f"{chunk_windows.chunk_session('r', 'item-8', held.claude_code_transcripts).id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines))
|
|
# Its own window, item-7, holds nothing for it; item-9's stopped chunk holds its item.
|
|
fake = FakeEffects(windows="item-7\t0\t@1\nitem-8 ?\t0\t@2\nitem-9\t1\t@3\nitem-10\t0\t@4\n")
|
|
self.assertEqual(chunk_windows.held_beside("item-7", "/r", held, fake.effects()), [8, 9, 10, 11])
|
|
self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-r", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]])
|
|
|
|
|
|
class StartChunks(unittest.TestCase):
|
|
"""start_chunks: the windows a loop opens for the chunks that may start now."""
|
|
LAUNCHER = pathlib.Path("/b/loop-claude")
|
|
|
|
def setUp(self):
|
|
made = tempfile.TemporaryDirectory()
|
|
self.addCleanup(made.cleanup)
|
|
self.root = pathlib.Path(made.name, "r")
|
|
self.home = configured(self, self.root).parents[1]
|
|
self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-")
|
|
# Items 1 and 2 overlap, so 2 waits; 3 runs beside 1.
|
|
self.plan = ChunkWindows.plan(self, "a.py", "a.py", "c.py")
|
|
|
|
def limit(self, chunks):
|
|
config = self.home / ".claude-projects" / self.root.name / "config.toml"
|
|
config.write_text(config.read_text() + f"parallel-chunks = {chunks}\n")
|
|
|
|
def start(self, fake, chunk=None, leaving=False, plan=None, env=None, owner=None):
|
|
"""start_chunks' answer, for a session whose @loop-root names owner, by default this checkout."""
|
|
fake.owner = str(self.root) if owner is None else owner
|
|
run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk)
|
|
with unittest.mock.patch.object(chunk_windows, "next_plan", return_value=plan or self.plan) as planned, contextlib.redirect_stderr(io.StringIO()) as said:
|
|
effects = fake.effects()
|
|
started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects)
|
|
self.said = said.getvalue()
|
|
self.planned = planned.call_count
|
|
if planned.call_count:
|
|
planned.assert_called_once_with(str(self.root), self.home, effects)
|
|
return started
|
|
|
|
def starts(self, fake):
|
|
"""The window starts of fake's tmux calls, each as (command, window name, what it runs)."""
|
|
return [(call[0], call[call.index("-n") + 1], call[call.index("--") + 1:]) for call in fake.tmux_calls if call[0] in ("new-session", "new-window")]
|
|
|
|
def test_opens_a_window_for_each_chunk_that_may_start_the_first_making_the_session(self):
|
|
self.limit(2)
|
|
fake = FakeEffects()
|
|
self.assertEqual(self.start(fake, env={"LOOP_CLAUDE_WORKER_MODEL": "m", "PATH": "/bin"}), ["item-1", "item-3"])
|
|
self.assertEqual(self.starts(fake), [("new-session", "item-1", [str(self.LAUNCHER), "--chunk", "item-1", "ship it"]), ("new-window", "item-3", [str(self.LAUNCHER), "--chunk", "item-3", "ship it"])])
|
|
first = next(call for call in fake.tmux_calls if call[0] == "new-session")
|
|
self.assertEqual((first[first.index("-s") + 1], first[first.index("-c") + 1]), (self.session, str(self.root)))
|
|
# Only loop-claude's own variables are handed on, since a window gets the tmux server's environment.
|
|
self.assertEqual([word for word in first if "=" in word], ["LOOP_CLAUDE_WORKER_MODEL=m"])
|
|
self.assertEqual(fake.tmux_calls[0], ["list-windows", "-t", f"={self.session}", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"])
|
|
# The session names the checkout whose chunks it runs.
|
|
self.assertIn(["set-option", "-t", f"={self.session}:", "@loop-root", str(self.root)], fake.tmux_calls)
|
|
|
|
def test_refuses_a_session_that_runs_another_checkouts_chunks(self):
|
|
for owner, named in (("/c/acme.api", "/c/acme.api"), ("", "another checkout")):
|
|
with self.subTest(owner=owner):
|
|
fake = FakeEffects(windows="item-5\t0\t@1\n")
|
|
self.assertEqual(self.start(fake, owner=owner), Failure(f"the tmux session {self.session} runs the chunks of {named}, whose key maps to the same session name; rename one of the two checkouts' folders"))
|
|
self.assertEqual(self.starts(fake), [])
|
|
self.assertIn(["show-options", "-qv", "-t", f"={self.session}:", "@loop-root"], fake.tmux_calls)
|
|
|
|
def test_refuses_a_checkout_do_epic_runs_chunks_in(self):
|
|
fake = FakeEffects(windows=f"epic-ABC-12\tABC-34 ?\t0\t@4\t{self.root}\nepic-ABC-12\tABC-35\t0\t@5\t/c/other\nepic-ABC-12\tABC-36\t1\t@6\t{self.root}\nloop-x\titem-1\t0\t@7\t{self.root}\n")
|
|
self.assertEqual(self.start(fake), Failure(f"do-epic runs chunks in {self.root}: ABC-34 of epic-ABC-12; loop-claude doesn't start a chunk there until those chunks finish"))
|
|
self.assertEqual(self.starts(fake), [])
|
|
|
|
def test_leaves_out_the_items_a_running_chunk_names_on_its_chunk_items_line(self):
|
|
self.limit(2)
|
|
transcripts = paths.paths_from(self.home).claude_code_transcripts
|
|
folder = transcripts / "-r"
|
|
folder.mkdir(parents=True)
|
|
opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts)
|
|
(folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n")
|
|
self.assertEqual(self.start(FakeEffects(windows="item-5\t0\t@1\n")), ["item-3"])
|
|
|
|
def test_starts_one_chunk_without_parallel_chunks(self):
|
|
fake = FakeEffects()
|
|
self.assertEqual(self.start(fake), ["item-1"])
|
|
|
|
def test_starts_none_and_reads_no_plan_while_the_running_chunks_fill_the_limit(self):
|
|
self.limit(2)
|
|
fake = FakeEffects(windows="item-5\t0\t@1\nitem-6 ?\t0\t@2\n")
|
|
self.assertEqual((self.start(fake), self.planned, self.starts(fake)), ([], 0, []))
|
|
# More chunks run than the limit allows, as after it was lowered; a stopped chunk still waits.
|
|
fake = FakeEffects(windows="item-5\t0\t@1\nitem-6\t0\t@2\nitem-7\t0\t@3\nitem-8\t1\t@4\nitem-9\t1\t@5\n")
|
|
self.assertEqual((self.start(fake), self.planned), ([], 0))
|
|
|
|
def test_a_finishing_chunks_own_window_counts_as_no_longer_running(self):
|
|
fake = FakeEffects(windows="item-5\t0\t@1\n")
|
|
self.assertEqual(self.start(fake, chunk="item-5"), [])
|
|
fake = FakeEffects(windows="item-5\t0\t@1\n")
|
|
self.assertEqual(self.start(fake, chunk="item-5", leaving=True), ["item-1"])
|
|
# The session exists, since the finishing loop's window is still in it.
|
|
self.assertEqual(self.starts(fake)[0][0], "new-window")
|
|
|
|
def test_the_humans_loop_claude_starts_a_stopped_chunk_again_first_closing_its_dead_window(self):
|
|
fake = FakeEffects(windows="item-5\t1\t@3\n")
|
|
self.assertEqual((self.start(fake), self.planned), (["item-5"], 0))
|
|
self.assertIn(["kill-window", "-t", "@3"], fake.tmux_calls)
|
|
# Its dead window was the session's last, so the session is made again.
|
|
self.assertEqual(self.starts(fake)[0][0], "new-session")
|
|
self.limit(2)
|
|
fake = FakeEffects(windows="item-5\t1\t@3\n")
|
|
self.assertEqual(self.start(fake), ["item-5", "item-1"])
|
|
|
|
def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self):
|
|
self.limit(2)
|
|
fake = FakeEffects(windows="item-5\t1\t@3\n")
|
|
self.assertEqual(self.start(fake, plan=next_chunk.NoItem("release 1.0.0 has no items")), ["item-5"])
|
|
|
|
def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self):
|
|
self.limit(2)
|
|
transcripts = paths.paths_from(self.home).claude_code_transcripts
|
|
folder = transcripts / "-r"
|
|
folder.mkdir(parents=True)
|
|
opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts)
|
|
(folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n")
|
|
fake = FakeEffects(windows="item-7\t0\t@1\nitem-5\t1\t@3\n")
|
|
# Item 1 is the stopped chunk's, so item 3 starts; the stopped one doesn't count against parallel-chunks.
|
|
self.assertEqual(self.start(fake, chunk="item-7", leaving=True), ["item-3"])
|
|
self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], [])
|
|
fake = FakeEffects(windows="item-7\t0\t@1\nno-item\t1\t@3\n")
|
|
self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=next_chunk.NoItem("release 1.0.0 has no items")), [])
|
|
|
|
def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self):
|
|
fake = FakeEffects()
|
|
self.assertEqual(self.start(fake, plan=Failure("no origin/main")), ["no-item"])
|
|
self.assertIn("starting a chunk without a plan item, since the plan cannot be read: no origin/main", self.said)
|
|
|
|
def test_says_why_a_window_could_not_start(self):
|
|
with unittest.mock.patch.object(tmux, "start_window", return_value=Failure("tmux could not start the window")):
|
|
self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window"))
|
|
|
|
def test_says_why_it_cannot_take_the_lock_on_starting_windows(self):
|
|
(self.home / ".local" / "state").mkdir(parents=True)
|
|
(self.home / ".local" / "state" / "loop-claude").write_text("")
|
|
started = self.start(FakeEffects())
|
|
self.assertIsInstance(started, Failure)
|
|
self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason)
|
|
|
|
def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
transcripts = pathlib.Path(temp)
|
|
opened = chunk_windows.chunk_session("proj", "item-7", transcripts)
|
|
self.assertEqual(opened.name, "A proj loop-claude item-7")
|
|
folder = transcripts / "-r"
|
|
folder.mkdir()
|
|
lines = [assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2)"}), assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2), 9 (L 2, E + I 3)"})]
|
|
(folder / f"{opened.id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines))
|
|
self.assertEqual(chunk_windows.held_items(["item-7", "item-8", "no-item"], "proj", transcripts), {7, 8, 9})
|
|
|
|
|
|
class ChunkFacts(unittest.TestCase):
|
|
# The launcher beside which chunk_start finds /b/chunk-preflight.
|
|
LAUNCHER = pathlib.Path("/b/loop-claude")
|
|
|
|
def test_runs_the_preflight_at_the_checkout_root_and_quotes_what_it_printed(self):
|
|
fake = FakeEffects(python=[ran("- A fact.\n")])
|
|
said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER))
|
|
self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")])
|
|
self.assertTrue(said.endswith("\n\n- A fact."), said)
|
|
self.assertNotIn("yourself", said)
|
|
|
|
def test_a_chunk_without_an_item_runs_the_preflight_without_naming_one(self):
|
|
fake = FakeEffects(python=[ran("- A fact.\n")])
|
|
chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER, window=tmux.Window("loop-proj", "no-item", "%4")))
|
|
self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree"], "/r")])
|
|
|
|
def test_the_facts_end_with_the_items_the_projects_other_running_chunks_hold(self):
|
|
# The loop's own window, a stopped one and two that run, one of them waiting for the human.
|
|
fake = FakeEffects(python=[ran("- A fact.\n")], windows="item-7\t0\t@1\nitem-5\t1\t@2\nitem-12 ?\t0\t@3\nitem-9\t0\t@4\n")
|
|
said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER))
|
|
self.assertTrue(said.endswith("- A fact.\n\nOther chunks of this project, running or stopped, hold items 5, 9, 12. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said)
|
|
self.assertEqual(fresh_prompt.held_by_others([9]), "\n\nOther chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it.")
|
|
self.assertTrue(fresh_prompt.held_by_others([4, 9]).startswith("\n\nOther chunks of this project, running or stopped, hold items 4, 9."))
|
|
# A worker whose preflight failed is told them too.
|
|
fake = FakeEffects(python=[ran(code=1, stderr="git could not start\n")], windows="item-9\t0\t@4\n")
|
|
said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER))
|
|
self.assertTrue(said.endswith("Other chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said)
|
|
|
|
def test_has_the_worker_find_the_facts_itself_where_the_preflight_failed(self):
|
|
for failed, why in ((ran(code=1, stderr="git could not start\n"), "git could not start"), (ran(code=1), "exited 1")):
|
|
with self.subTest(why=why):
|
|
said = chunk_start.chunk_facts(a_loop(FakeEffects(python=[failed]).effects(), launcher=self.LAUNCHER))
|
|
self.assertIn("yourself", said)
|
|
self.assertIn(why, said)
|
|
|
|
def test_runs_the_preflight_only_where_the_session_is_not_open(self):
|
|
with tempfile.TemporaryDirectory() as home:
|
|
opened = session.session_named("A proj loop-claude", pathlib.Path(home, "projects"))
|
|
fresh = FakeEffects(python=[ran("- A fact.\n")])
|
|
facts, _ = chunk_start.chunk_start(a_loop(fresh.effects(), session=opened, launcher=self.LAUNCHER))
|
|
self.assertEqual((fresh.made("python"), "- A fact." in facts), ([(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")], True))
|
|
transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl")
|
|
transcript.parent.mkdir(parents=True)
|
|
transcript.write_text("")
|
|
resumed = FakeEffects()
|
|
facts, _ = chunk_start.chunk_start(a_loop(resumed.effects(), session=opened, launcher=self.LAUNCHER))
|
|
self.assertEqual((resumed.made("python"), facts), ([], ""))
|
|
|
|
|
|
class FreshPrompt(unittest.TestCase):
|
|
"""loop-claude's first prompt of a chunk."""
|
|
# What the preflight found, as the fresh prompt quotes it.
|
|
FACTS = "- A preflight fact."
|
|
|
|
def test_names_the_route_in_a_fresh_prompt_only_where_an_item_routed_it(self):
|
|
routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS)
|
|
self.assertIn(f"Continue any open draft PR or worktree for the chunk before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\nloop-claude runs you on m at high effort, routed by item 3's L of 4.", routed)
|
|
unrouted = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS)
|
|
self.assertIn(f"before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\n{self.FACTS}\n\nThe chunk's", unrouted)
|
|
self.assertNotIn("loop-claude runs you on", unrouted)
|
|
|
|
def test_has_the_routed_worker_count_its_items_size_again_and_puts_the_facts_before_the_scope(self):
|
|
routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS)
|
|
self.assertIn("item 3's size again", routed)
|
|
self.assertIn("escalation trigger 1", routed)
|
|
self.assertIn(f"\n\n{self.FACTS}\n\nThe chunk's", routed)
|
|
self.assertNotIn("size again", fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS))
|
|
|
|
def test_gives_each_fresh_worker_the_reading_rule_once(self):
|
|
rule = "never read more than 80 lines of a file at once, nor take a tool output over 3,000 characters into your context"
|
|
for route in (Route(Rung("m", "high"), 3, 4, 5), Route(Rung("m"))):
|
|
with self.subTest(route=route):
|
|
self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", route, None, self.FACTS).count(rule), 1)
|
|
|
|
def test_quotes_what_the_preflight_printed_or_has_the_worker_find_the_facts_itself(self):
|
|
self.assertIn("\n\n- A fact.", fresh_prompt.preflight_facts("- A fact."))
|
|
failed = fresh_prompt.preflight_failed("git could not start")
|
|
self.assertIn("yourself", failed)
|
|
self.assertIn("git could not start", failed)
|
|
|
|
def test_has_each_fresh_worker_continue_an_open_draft_pr_or_worktree_once_whatever_the_preflight_found(self):
|
|
for facts in (fresh_prompt.preflight_facts("- A fact."), fresh_prompt.preflight_failed("git could not start")):
|
|
with self.subTest(facts=facts):
|
|
self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, facts).count("Continue any open draft PR or worktree"), 1)
|
|
|
|
def test_puts_the_rung_sentences_after_the_route_or_else_before_the_facts(self):
|
|
routed, unrouted = Route(OPUS_PROJECT.rung, 3, 2, 4), Route(OPUS_PROJECT.rung)
|
|
self.assertIn(f"routed by item 3's L of 2.{prompts.rungs_held(fresh_prompt.DRIVER, routed, OPUS_PROJECT)} After your first read", fresh_prompt.WORKER.fresh("Do it.", "## Goals", routed, OPUS_PROJECT, self.FACTS))
|
|
self.assertIn(f"{fresh_prompt.NAME_THE_ITEMS}\n\n{prompts.rungs_held(fresh_prompt.DRIVER, unrouted, OPUS_PROJECT).lstrip()}\n\n{self.FACTS}", fresh_prompt.WORKER.fresh("Do it.", "## Goals", unrouted, OPUS_PROJECT, self.FACTS))
|
|
|
|
|
|
# Item 297 scores the higher L, item 298 the higher E + I.
|
|
SCORED = "## Items\n\n| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 297 | 0.8.0 | | **Do it.** | 4 | 2 | 3 | 7 | 2 | 1 by 9 | 25 |\n| 298 | 0.8.0 | | **Do more.** | 3 | 3 | 4 | 8 | 2 | 1 by 9 | 20 |\n"
|
|
|
|
|
|
class ChunkItems(unittest.TestCase):
|
|
def test_names_the_items_on_the_workers_latest_chunk_items_line(self):
|
|
earlier = said("Chunk items: 297 (L 3, E + I 5)")
|
|
latest = said("Starting.\n**Chunk items:** 297 (L 4, E + I 5), item 298 (L 3, E + I 7), #12, 298\nOn to the tests.")
|
|
self.assertEqual(chunk_items.named([earlier, latest]), (297, 298, 12))
|
|
self.assertEqual(chunk_items.named([latest, earlier]), (297,))
|
|
self.assertEqual(chunk_items.named([earlier, said("Chunk items: none")]), ())
|
|
self.assertEqual(chunk_items.named([said("Chunk items: 297 and 298 (L 3, E + I 7)")]), (297, 298))
|
|
|
|
def test_reads_the_example_line_the_prompt_gives_the_worker(self):
|
|
example = re.search(r"`(Chunk items:[^`]*)`", fresh_prompt.NAME_THE_ITEMS)[1]
|
|
self.assertEqual(chunk_items.named([said(example)]), (297, 298))
|
|
|
|
def test_every_prompt_that_starts_or_resumes_a_chunk_asks_for_the_line(self):
|
|
self.assertIn("on one line, as in `Chunk items: ", fresh_prompt.WORKER.resumed("", Route(Rung("m")), None))
|
|
|
|
def test_reads_only_a_line_the_worker_opens_with_the_label_in_its_own_text(self):
|
|
prompt = entries.LoopPrompt(text="Chunk items: 297 (L 3, E + I 5)")
|
|
written = entries.AssistantTurn(parts=(entries.ToolCall("t", "Write", {"content": "Chunk items: 9"}),))
|
|
mentioned = said("Once I know them, I write `Chunk items: 5`.")
|
|
self.assertEqual(chunk_items.named([prompt, written, mentioned]), ())
|
|
|
|
def test_takes_the_highest_l_and_e_plus_i_todo_md_scores_among_the_named_items(self):
|
|
self.assertEqual(chunk_items.taken_in([said("Chunk items: 297 (L 1, E + I 2), 298, 400")], plan_of(SCORED)), TakenIn(4, 7))
|
|
self.assertEqual(chunk_items.taken_in([said("Chunk items: 400")], plan_of(SCORED)), TakenIn())
|
|
self.assertEqual(chunk_items.taken_in([], plan_of(SCORED)), TakenIn())
|
|
|
|
def test_names_the_items_at_no_level_where_todo_md_cannot_be_read(self):
|
|
self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], Failure("no todo.md")), TakenIn())
|
|
self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], plan_of("no items table")), TakenIn())
|
|
|
|
|
|
class LoopRuns(unittest.TestCase):
|
|
def test_stops_naming_the_reload_when_it_hands_over_anything_but_the_state_with_a_task(self):
|
|
for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "root": "/r", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "", "unfinished_evidence": ""})], [json.dumps({"cycle_number": "3", "root": "/r", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "turns": 1, "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it", "unfinished_evidence": ""})]):
|
|
with self.subTest(handed=handed):
|
|
self.assertEqual(main.loop_run_from([main.RELOAD_FLAG, *handed], CWD, FakeEffects().effects()), Stop(1, f"the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude <task>. Handed: {' '.join(handed)}"))
|
|
|
|
def test_decodes_every_field_of_the_state_and_its_route(self):
|
|
state = main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="old", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2)
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state)
|
|
self.assertEqual(set(reload.LOOP_RUN_FIELDS), {field.name for field in dataclasses.fields(main.LoopRun)})
|
|
|
|
def test_rejects_a_field_of_another_type_or_an_unknown_field(self):
|
|
state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2))
|
|
for path, wrong in ((("unfinished_run",), "2"), (("unfinished_run",), True), (("route", "unfinished"), 1.0), (("route", "escalated_at"), "9"), (("route", "item"), True), (("route", "rung"), "claude-opus-5-5"), (("route", "rung", "model"), None), (("route",), "r"), (("route", "extra"), 1), (("route", "model"), "claude-opus-5-5"), (("route", "effort"), "high")):
|
|
with self.subTest(path=path, wrong=wrong):
|
|
handed = json.loads(json.dumps(state))
|
|
target = handed
|
|
for key in path[:-1]:
|
|
target = target[key]
|
|
target[path[-1]] = wrong
|
|
self.assertIsNone(reload.decode_handed(json.dumps(handed)))
|
|
|
|
def test_rejects_a_state_or_rung_missing_a_required_field(self):
|
|
state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"))))
|
|
for path in (("cycle_number",), ("root",), ("task",), ("unfinished_evidence",), ("route", "rung"), ("route", "rung", "model")):
|
|
with self.subTest(path=path):
|
|
handed = json.loads(json.dumps(state))
|
|
target = handed
|
|
for key in path[:-1]:
|
|
target = target[key]
|
|
del target[path[-1]]
|
|
self.assertIsNone(reload.decode_handed(json.dumps(handed)))
|
|
|
|
def test_decodes_a_rung_without_an_effort(self):
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("some-model")))
|
|
self.assertEqual(reload.decode_handed(json.dumps({**dataclasses.asdict(state), "route": {"rung": {"model": "some-model"}}})), state)
|
|
|
|
def test_rejects_text_that_is_not_json(self):
|
|
self.assertIsNone(reload.decode_handed("not json"))
|
|
|
|
def test_decodes_a_route_inside_the_state_or_none(self):
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1))
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state)
|
|
unrouted = dataclasses.replace(state, route=None)
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(unrouted))), unrouted)
|
|
handed = dataclasses.asdict(state)
|
|
handed["route"]["rung"]["effort"] = 1
|
|
self.assertIsNone(reload.decode_handed(json.dumps(handed)))
|
|
del handed["route"]["rung"]
|
|
self.assertIsNone(reload.decode_handed(json.dumps(handed)))
|
|
off_scale = dataclasses.asdict(state)
|
|
off_scale["route"]["likelihood"] = 9
|
|
self.assertIsNone(reload.decode_handed(json.dumps(off_scale)))
|
|
older = dataclasses.asdict(state)
|
|
del older["route"]["escape_and_impact_taken_in"]
|
|
self.assertEqual(reload.decode_handed(json.dumps(older)), state)
|
|
taken = dataclasses.replace(state, route=dataclasses.replace(state.route, escape_and_impact_taken_in=7))
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(taken))), taken)
|
|
for taken_in in (9, 5):
|
|
with self.subTest(taken_in=taken_in):
|
|
handed = dataclasses.asdict(state)
|
|
handed["route"]["escape_and_impact_taken_in"] = taken_in
|
|
self.assertIsNone(reload.decode_handed(json.dumps(handed)))
|
|
|
|
def test_decodes_a_state_from_before_a_field_with_a_default_joined_it(self):
|
|
self.assertEqual(reload.decode_handed(json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "unfinished_evidence": ""})), main.LoopRun(3, "/r", "do it", ""))
|
|
|
|
def test_drops_the_items_taken_in_that_a_loop_started_before_0_11_0_hands_on(self):
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")
|
|
self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state) | {"items_taken_in": [12, 14]})), state)
|
|
|
|
def test_drops_the_slot_that_a_loop_started_before_chunks_ran_in_windows_hands_on(self):
|
|
state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")
|
|
self.assertEqual(reload.decode_handed(json.dumps({key: value for key, value in dataclasses.asdict(state).items() if key != "chunk"} | {"slot": 1})), state)
|
|
|
|
def test_stops_when_the_reload_cannot_start_python(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
launcher = pathlib.Path(root, "loop-claude")
|
|
launcher.write_text("")
|
|
fake = FakeEffects(python=[ran()])
|
|
self.assertEqual(reload.exec_if_edited(a_loop(fake.effects(), launcher=launcher), "stale", main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude <task>"))
|
|
|
|
|
|
class WindowStarts(unittest.TestCase):
|
|
"""The loop-claude the human runs, which starts the windows, and the stop of a window's loop whose chunk finished."""
|
|
HOME = pathlib.Path("/h")
|
|
|
|
def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")):
|
|
"""run_loop's stop and what it said on stdout, with start_chunks answering `started`; self.asked holds what start_chunks was asked."""
|
|
with unittest.mock.patch.object(main, "start_chunks", return_value=list(started) if isinstance(started, tuple) else started) as start, contextlib.redirect_stdout(io.StringIO()) as said:
|
|
stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects())
|
|
self.asked = [call.args[:2] for call in start.call_args_list]
|
|
return stop, said.getvalue()
|
|
|
|
def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self):
|
|
fake = FakeEffects(git=["/r\n"], tmux_version="tmux 3.1c")
|
|
self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c"), ""))
|
|
self.assertEqual((fake.tmux_calls, self.asked), ([["-V"]], []))
|
|
|
|
def test_prints_the_help_without_tmux(self):
|
|
fake = FakeEffects(tmux_version="")
|
|
self.assertEqual(self.run_loop(["--help"], fake)[0], Stop(0, usage.HELP))
|
|
self.assertEqual(fake.tmux_calls, [])
|
|
|
|
def test_starts_the_chunks_then_attaches_to_the_session_or_inside_tmux_switches_to_it(self):
|
|
for env, shown in (({}, ["attach-session", "-t", "=loop-r"]), ({"TMUX": "/tmp/tmux-1000/default,1,0"}, ["switch-client", "-t", "=loop-r"])):
|
|
with self.subTest(shown=shown):
|
|
fake = FakeEffects(git=["/r\n"])
|
|
stop, said = self.run_loop(["do", "it"], fake, env)
|
|
self.assertEqual((stop, said), (Stop(0, ""), f"{STAMP} started item-3, item-4 in the tmux session loop-r\n"))
|
|
self.assertEqual(fake.tmux_calls, [["-V"], shown])
|
|
self.assertEqual(self.asked, [(main.LoopRun(0, "/r", "do it", ""), False)])
|
|
|
|
def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self):
|
|
fake = FakeEffects(git=["/r\n"], on_terminal=False)
|
|
effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude")
|
|
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()):
|
|
main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
|
self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects))
|
|
|
|
def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self):
|
|
fake = FakeEffects(git=["/r\n"])
|
|
effects, env = fake.effects(), {"TMUX_PANE": "%4"}
|
|
launcher = pathlib.Path("/b/loop-claude")
|
|
cycle = unittest.mock.Mock(project=OPUS_PROJECT)
|
|
unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished))
|
|
finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished))
|
|
closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")]
|
|
with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \
|
|
unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \
|
|
unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on:
|
|
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
|
loop = this_loop.Loop(root="/r", session=session.session_named("A r loop-claude item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects)
|
|
self.assertEqual(stop, Stop(0, ""))
|
|
self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)])
|
|
handed_on.assert_called_once_with(closed[1], loop)
|
|
|
|
def test_a_window_loop_given_no_task_prints_the_help(self):
|
|
self.assertEqual(main.loop_run_from(["--chunk", "item-3"], CWD, FakeEffects(git=["/r\n"]).effects()), Stop(1, usage.HELP))
|
|
|
|
def test_a_window_loop_outside_a_checkout_stops(self):
|
|
self.assertIsInstance(main.loop_run_from(["--chunk", "item-3", "do it"], CWD, FakeEffects(git=[Failure("git cannot start")]).effects()), Stop)
|
|
|
|
def test_says_that_nothing_started_where_the_session_already_runs_every_chunk_that_may(self):
|
|
self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=())[1], f"{STAMP} the tmux session loop-r runs every chunk that may run now, so `do it` started in no new window\n")
|
|
|
|
def test_off_a_terminal_it_prints_how_to_attach(self):
|
|
fake = FakeEffects(git=["/r\n"], on_terminal=False)
|
|
stop, said = self.run_loop(["do it"], fake)
|
|
self.assertEqual(stop, Stop(0, ""))
|
|
self.assertTrue(said.endswith(f"{STAMP} not on a terminal; see the chunks with `tmux attach -t =loop-r`\n"), said)
|
|
self.assertEqual(fake.tmux_calls, [["-V"]])
|
|
|
|
def test_stops_where_no_chunk_could_start(self):
|
|
self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=Failure("no plan"))[0], Stop(1, "no plan"))
|
|
|
|
def test_stops_where_tmux_cannot_show_the_session(self):
|
|
fake = FakeEffects(git=["/r\n"])
|
|
effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1)
|
|
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()):
|
|
stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects)
|
|
self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session"))
|
|
|
|
def test_a_window_loop_outside_tmux_stops(self):
|
|
fake = FakeEffects(git=["/r\n"])
|
|
self.assertEqual(self.run_loop(["--chunk", "item-3", "do it"], fake)[0], Stop(1, "--chunk is how loop-claude starts itself in a tmux window; run loop-claude <task>"))
|
|
self.assertEqual(self.asked, [])
|
|
|
|
def test_reads_the_chunk_a_window_loop_is_started_with(self):
|
|
self.assertEqual(main.loop_run_from(["--chunk", "item-3", "do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", "", chunk="item-3"))
|
|
self.assertEqual(main.loop_run_from(["do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", ""))
|
|
|
|
def test_a_finished_chunk_starts_each_chunk_that_may_start_and_stops_without_a_reason(self):
|
|
loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7")
|
|
for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")),
|
|
(Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))):
|
|
with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed:
|
|
loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"})
|
|
self.assertEqual(main.handed_on(loop_run, loop), stop)
|
|
self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "")
|
|
self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects))
|
|
|
|
|
|
class StopDms(unittest.TestCase):
|
|
def run_main(self, argv, fake):
|
|
with tempfile.TemporaryDirectory() as home, contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()):
|
|
code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects())
|
|
return code, said.getvalue()
|
|
|
|
def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self):
|
|
fake = FakeEffects()
|
|
with unittest.mock.patch.object(main, "run_loop", return_value=Stop(2, "the human is needed to look at the chunk, but it stayed unfinished")):
|
|
self.assertEqual(self.run_main(["do it"], fake)[0], 2)
|
|
self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the human is needed to look at the chunk, but it stayed unfinished"])
|
|
|
|
def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self):
|
|
fake = FakeEffects()
|
|
with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")):
|
|
self.assertEqual(self.run_main(["do it"], fake), (0, ""))
|
|
self.assertEqual(fake.dms, [])
|
|
|
|
def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self):
|
|
fake = FakeEffects()
|
|
with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt):
|
|
self.assertEqual(self.run_main(["do it"], fake)[0], 130)
|
|
self.assertEqual(fake.dms, [])
|
|
|
|
def test_dms_a_crash_before_it_reaches_the_terminal(self):
|
|
fake = FakeEffects()
|
|
with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError):
|
|
self.run_main(["do it"], fake)
|
|
self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"])
|
|
|
|
def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self):
|
|
fake = FakeEffects()
|
|
code, said = self.run_main([main.RELOAD_FLAG, "3"], fake)
|
|
self.assertEqual(code, 1)
|
|
self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude <task>. Handed: 3"])
|
|
|
|
def test_says_why_the_dm_of_a_stop_failed_after_the_reason(self):
|
|
fake = FakeEffects(dm_failures=[Failure("cannot reach https://chat.example: timed out")])
|
|
self.assertTrue(self.run_main([main.RELOAD_FLAG, "3"], fake)[1].endswith(f"Handed: 3\n{STAMP} could not DM you: cannot reach https://chat.example: timed out\n"))
|
|
|
|
def test_skips_the_dm_where_it_prints_its_help(self):
|
|
for argv, code in (([], 1), (["--help"], 0)):
|
|
with self.subTest(argv=argv):
|
|
fake = FakeEffects()
|
|
self.assertEqual(self.run_main(argv, fake)[0], code)
|
|
self.assertEqual(fake.dms, [])
|
|
|
|
|
|
class HelpLimits(unittest.TestCase):
|
|
def test_help_wraps_at_100_columns(self):
|
|
self.assertEqual([line for line in usage.HELP.splitlines() if len(line) > 100], [])
|
|
|
|
def test_help_states_the_limits_the_code_holds(self):
|
|
said = " ".join(usage.HELP.split())
|
|
self.assertIn(f"The {['second', 'third', 'fourth', 'fifth'][states.QUOTED_ANSWERS_PER_CYCLE - 1]} question they answer in one cycle", said)
|
|
# The retry messages say these limits in words too.
|
|
self.assertEqual((retry.FAILURES_RUNNING, retry.RETRY_DELAY), (3, 60))
|
|
self.assertIn("fails three times running, retried a minute apart", said)
|
|
self.assertIn(f"retried {retry.PAST_RESET // 60} minutes after the limit resets, or every {retry.UNKNOWN_RESET_POLL // 60} minutes", said)
|
|
self.assertIn(f"still running after {PYTHON_TIMEOUT} s", said)
|
|
self.assertIn(f"Once {state.CYCLES_PER_CHECK} cycles have closed across all projects", said)
|
|
self.assertIn(f"run with a {worker.COMPACT_WINDOW // 1000}k auto-compact window", said)
|
|
self.assertIn(f"{BASH_CEILING_CHARACTERS:,} characters", said)
|
|
self.assertIn(f"unfinished {chunk_run.UNFINISHED_LIMIT} cycles running", said)
|
|
self.assertIn(f"rerun to give it {chunk_run.UNFINISHED_LIMIT} more", said)
|
|
|
|
|
|
class OwnLadder(unittest.TestCase):
|
|
def test_a_cycle_on_a_projects_own_ladder_names_its_file_and_says_where_the_workers_model_is_off_it(self):
|
|
file = pathlib.Path("/h/.claude-projects/acme/routing-ladder.json")
|
|
self.assertEqual(main.own_ladder(file, "claude-fable-5-2", False), f"routing on the project's own ladder, {file}.")
|
|
self.assertEqual(main.own_ladder(file, "claude-opus-5-5", True), f"routing on the project's own ladder, {file}. claude-opus-5-5 is not on it, so the worker runs on claude-opus-5-5 unrouted; LOOP_CLAUDE_WORKER_MODEL and LOOP_CLAUDE_JUDGE_MODEL name other models for a run.")
|
|
|
|
|
|
class ExitContract(unittest.TestCase):
|
|
def test_every_exit_1_stop_has_its_clause_in_help(self):
|
|
read, git, premises_ = "a transcript cannot be read or archived", "git cannot start or finds no checkout", "no config names the checkout, its config cannot be read, or the goals file holds no premises"
|
|
models_ = "the models file cannot be read or written"
|
|
names = (*records.RECORD_FILES, records.HUMAN_RULE_NAMES[0], f"{records.HUMAN_RULE_NAMES[1]}*.md", records.HUMAN_RULE_NAMES[2])
|
|
unreadable = f"the goals file, or {', '.join(names[:-1])} or {names[-1]} where they exist, cannot be read or is not UTF-8"
|
|
# Every Stop whose code is not a literal 0, 2 or 130, in source order: path, function, code, start of reason, its --help clause.
|
|
clauses = [
|
|
("agent_cycle/interrupt.py", "claude_open", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"),
|
|
("agent_cycle/interrupt.py", "human_typed", "1", "entries.reason", read),
|
|
("agent_cycle/interrupt.py", "human_typed", "1", "f'could not tell whether you typed anything:", read),
|
|
("agent_cycle/retry.py", "retried", "1", "f'the {who} failed three times running:", "the worker or the judge fails three times running"),
|
|
("agent_cycle/run.py", "archived_stop", "1", "f'{failure.reason}", read),
|
|
("agent_cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"),
|
|
("agent_cycle/run.py", "rule", "1", "entries.reason", read),
|
|
("agent_cycle/run.py", "rule", "1", "annotation.reason", read),
|
|
("agent_cycle/run.py", "rule", "1", "failure.reason", read),
|
|
("agent_cycle/run.py", "keep_rejection", "1", "unkept.reason", read),
|
|
("agent_cycle/run.py", "close", "1", "annotation.reason", read),
|
|
("agent_cycle/run.py", "close", "1", "failure.reason", read),
|
|
("agent_cycle/run.py", "close", "1", "failure.reason", "a finished chunk's session cannot be deleted"),
|
|
("chunk_driver/chunk_run.py", "closed_cycle", "1", "failure.reason", read),
|
|
("chunk_driver/cycle_start.py", "project_routing", "1", "ladder_file.reason", premises_),
|
|
("chunk_driver/cycle_start.py", "routing_on", "1", "ladder.reason", "the project's routing ladder cannot be read or holds a fault"),
|
|
("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_),
|
|
("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_),
|
|
("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "f'could not save the model in", models_),
|
|
("loop_claude/loop/checkout.py", "checkout_root", "1", "found.reason", git),
|
|
("loop_claude/loop/main.py", "run_loop", "1", "reason", "tmux is missing or older than 3.2"),
|
|
("loop_claude/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how loop-claude starts itself", "--chunk is given outside a tmux window"),
|
|
("loop_claude/loop/main.py", "windows_opened", "1", "started.reason", "a chunk's window cannot be started"),
|
|
("loop_claude/loop/main.py", "windows_opened", "1", "f\"`tmux {' '.join(shown)}` failed", "the tmux session cannot be put on the terminal"),
|
|
("loop_claude/loop/main.py", "handed_on", "1", "f'the chunk finished, but the next could not start", "a chunk's window cannot be started"),
|
|
("loop_claude/loop/main.py", "next_cycle", "1", "premises.reason", premises_),
|
|
("loop_claude/loop/main.py", "next_cycle", "1", "records.reason", unreadable),
|
|
("loop_claude/loop/main.py", "loop_run_from", "0 if task else 1", "HELP", "no task is given"),
|
|
("loop_claude/loop/main.py", "loop_run_from", "1", 'f"the edited loop-claude cannot read the state', "a reload cannot read the state the running script handed it"),
|
|
("loop_claude/loop/reload.py", "exec_if_edited", "1", "f'could not reload loop-claude:", "python cannot start for a reload"),
|
|
("loop_claude/loop/routing_check.py", "raise_calls", "1", "f\"could not start claude for the routing calls in {loop.paths.config_clone}", "claude cannot start for an interrupt"),
|
|
]
|
|
root = pathlib.Path(main.__file__).parents[2]
|
|
found = []
|
|
for path in sorted(path for package in ("agent_cycle", "chunk_driver", "loop_claude") for path in (root / package).rglob("*.py") if not path.name.startswith("test_")):
|
|
functions = [node for node in ast.walk(ast.parse(path.read_text())) if isinstance(node, ast.FunctionDef)]
|
|
stops = [(call.lineno, function.name, ast.unparse(call.args[0]), ast.unparse(call.args[1])) for function in functions for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) == "Stop" and not (isinstance(call.args[0], ast.Constant) and call.args[0].value in (0, 2, 130))]
|
|
found += [(str(path.relative_to(root)), function, code, reason) for _, function, code, reason in sorted(stops)]
|
|
self.assertEqual(len(found), len(clauses), found)
|
|
for (path, function, code, reason), (want_path, want_function, want_code, reason_start, _) in zip(found, clauses):
|
|
with self.subTest(path=path, function=function):
|
|
self.assertEqual((path, function, code), (want_path, want_function, want_code))
|
|
self.assertTrue(reason.startswith(reason_start), reason)
|
|
help_exit_1 = " ".join(usage.HELP[usage.HELP.index("Exit 1"):usage.HELP.index("Exit 2")].split())
|
|
self.assertEqual([clause for *_, clause in clauses if f"- {clause}" not in help_exit_1], [])
|
|
|
|
|
|
class JudgeFailures(unittest.TestCase):
|
|
def test_every_failed_judge_run_has_its_clause_in_help(self):
|
|
started, exited, unparsed = "claude cannot start", "exits non-zero", "prints no json reply"
|
|
field, unasked = "its reply misses or mistypes a field", "interrupts or quotes an answer without a question"
|
|
# Every Failure the judge's run returns, in source order of judge.py, then of the claude run it calls: function, start of reason, its --help clause.
|
|
clauses = [
|
|
("verdict_from", "'the judge returned no reply'", field),
|
|
("verdict_from", 'f"the judge\'s reply has no {name}', field),
|
|
("verdict_from", "'the judge interrupted without a question'", unasked),
|
|
("verdict_from", "'the judge quoted an answer without a question'", unasked),
|
|
("run_judge", "verdict.reason", field),
|
|
("run_judge", "checked.reason", "quotes a passage the transcript does not hold"),
|
|
("misquoted_evidence", "f'the judge quoted {verdict.evidence!r}, which is not in the transcript", "quotes a passage the transcript does not hold"),
|
|
("claude_reply", "f'could not start claude:", started),
|
|
("claude_reply", "proc.stderr.strip() or f'{who} exited", exited),
|
|
("claude_reply", 'f"{who} printed no json reply', unparsed),
|
|
]
|
|
found = []
|
|
for module in (judge, reply):
|
|
functions = [node for node in ast.walk(ast.parse(pathlib.Path(module.__file__).read_text())) if isinstance(node, ast.FunctionDef)]
|
|
found += 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) in ("Failure", "Rejected"))
|
|
self.assertEqual(len(found), len(clauses), found)
|
|
for (_, function, reason), (want_function, reason_start, _) in zip(found, clauses):
|
|
with self.subTest(function=function):
|
|
self.assertEqual(function, want_function)
|
|
self.assertTrue(reason.startswith(reason_start), reason)
|
|
judge_clause = " ".join(usage.HELP[usage.HELP.index("a judge when"):usage.HELP.index("A usage limit")].split())
|
|
self.assertEqual([clause for *_, clause in clauses if clause not in judge_clause], [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|