161 lines
10 KiB
Python
161 lines
10 KiB
Python
import contextlib
|
|
import dataclasses
|
|
import io
|
|
import pathlib
|
|
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 loop_claude.loop import chunk_start
|
|
from project_config.doubles import configured
|
|
from release_plan.release_standing import DefaultPlan, LastCut, UNSEEN_RELEASES
|
|
from failure import Failure
|
|
from loop_effects.doubles import FakeEffects, ran, STAMP
|
|
from loop_claude.chunk import fresh_prompt
|
|
from loop_claude.doubles import NO_CUT, TODO
|
|
from loop_state import paths
|
|
from plan_store.todo_md import plan_of
|
|
from claude_code import session
|
|
from agent_cycle import tmux
|
|
|
|
|
|
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 tempfile.TemporaryDirectory() as root:
|
|
folder = configured(self, pathlib.Path(root))
|
|
config = folder / "config.toml"
|
|
config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))
|
|
(folder / "todo.md").write_text(TODO)
|
|
opened = session.session_named("A proj loop-claude", pathlib.Path(root, "projects"))
|
|
transcript = pathlib.Path(root, "projects", "-fake", f"{opened.id}.jsonl")
|
|
transcript.parent.mkdir(parents=True)
|
|
transcript.write_text("")
|
|
unread = Failure("no git")
|
|
fake = FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread])
|
|
with contextlib.redirect_stderr(io.StringIO()) as stderr:
|
|
facts, held = chunk_start.chunk_start(a_loop(fake.effects(), root=root, session=opened, paths=paths.paths_from(folder.parents[1])))
|
|
self.assertEqual((facts, held.plan), ("", plan_of(TODO)))
|
|
self.assertEqual([args for args in fake.made("git") if args[0] == "remote"], [["remote"]])
|
|
# An open session is no fresh chunk, so the human isn't told again of releases the plan cannot show.
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
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 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), ([], ""))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|