156 lines
7.9 KiB
Python
156 lines
7.9 KiB
Python
import contextlib
|
|
import io
|
|
import json
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
|
|
|
from chunk_driver.doubles import a_loop
|
|
from routing_figures import archive, state
|
|
from test_env import assert_not_root
|
|
from routing_figures.calls import Call
|
|
from failure import Failure
|
|
from loop_effects.doubles import FakeEffects, NOW
|
|
from transcript_archive import outcomes
|
|
from loop_state import paths
|
|
from loop_claude.loop import routing_check
|
|
from model_routing.ladder import Rung
|
|
from claude_code import session
|
|
from agent_cycle.stop import Stop
|
|
from text_files.utc_stamp import utc
|
|
|
|
|
|
# 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])
|
|
self.root.mkdir()
|
|
fake.replace_file = lambda path, data: Failure("full")
|
|
with contextlib.redirect_stderr(io.StringIO()) as said:
|
|
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(f"routing figures: could not write {self.root / state.RAISED}: 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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|