Run every claude the loop starts in a Claude home generated outside the clone
CI / tests (pull_request) Successful in 4m45s

This commit is contained in:
2026-12-04 01:07:03 +01:00
parent 2460b9e132
commit 0203a8e0b3
48 changed files with 703 additions and 369 deletions
+3 -3
View File
@@ -71,9 +71,9 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
loop_run = loop_run_from(argv, cwd, effects)
if isinstance(loop_run, Stop):
return loop_run
# Before anything starts: a worktree, a claude session or a tmux window.
if reason := tmux.unusable(effects.tmux, fresh_prompt.DRIVER):
return Stop(1, reason)
# Before anything starts: a worktree, a claude session or a tmux window. One stop names everything that is the person's to do.
if needs := [reason for reason in (tmux.unusable(effects.tmux, fresh_prompt.DRIVER), effects.claude_home()) if reason]:
return Stop(1, "\n".join(needs))
if loop_run.chunk is None:
return windows_opened(loop_run, env, paths, launcher, effects, steps.start_chunks)
pane = env.get("TMUX_PANE")
+11 -2
View File
@@ -137,6 +137,15 @@ class WindowStarts(unittest.TestCase):
self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "build-release 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_refuses_to_start_anything_until_a_claude_can_run_in_the_loops_claude_home_and_names_all_that_is_the_persons_at_once(self):
fake = FakeEffects(git=["/r\n"], tmux_version="tmux 3.1c")
fake.home_unready = "nobody is signed in"
self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "build-release runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c\nnobody is signed in"), ""))
fake = FakeEffects(git=["/r\n"])
fake.home_unready = "nobody is signed in"
self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "nobody is signed in"), ""))
self.assertEqual(self.asked, [])
def test_prints_the_help_without_tmux(self):
fake = FakeEffects(tmux_version="")
self.assertEqual(self.run_loop(["--help"], fake)[0], Stop(0, usage.HELP))
@@ -172,7 +181,7 @@ class WindowStarts(unittest.TestCase):
run_cycle=lambda cycle: concluded.pop(0), closed_cycle=lambda *args: closed.pop(0), check_routing=lambda *args: None)
with contextlib.redirect_stdout(io.StringIO()):
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME, {}), launcher, CWD, effects, taken)
loop = this_loop.Loop(root="/r", session=session.session_named("A r build-release 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)
loop = this_loop.Loop(root="/r", session=session.session_named("A r build-release item-3", self.HOME / ".local" / "share" / "build-release" / "claude-home" / "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(cycles, [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)])
self.assertEqual(started, [(main.LoopRun(2, "/r", "do it", "", chunk="item-3"), True, loop.paths, env, launcher, effects)])
@@ -189,7 +198,7 @@ class WindowStarts(unittest.TestCase):
return lambda *args: calls.append((name, *args)) or answers[name](*args)
stop = main.run_loop(["--chunk", "item-3", "do it"], env, home, launcher, CWD, effects, steps(lambda *args: self.fail(args), **{name: step(name) for name in answers}))
self.loop = this_loop.Loop(root="/r", session=session.session_named("A r build-release item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=home, env=env, launcher=launcher, effects=effects)
self.loop = this_loop.Loop(root="/r", session=session.session_named("A r build-release item-3", self.HOME / ".local" / "share" / "build-release" / "claude-home" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=home, env=env, launcher=launcher, effects=effects)
return stop, calls
def test_a_window_loop_hands_each_step_of_a_cycle_what_the_steps_before_it_made(self):
+1 -1
View File
@@ -56,7 +56,7 @@ class ExitContract(unittest.TestCase):
("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"),
("build_release/loop/checkout.py", "checkout_root", "1", "found.reason", git),
("build_release/loop/main.py", "run_loop", "1", "reason", "tmux is missing or older than 3.2"),
("build_release/loop/main.py", "run_loop", "1", "'\\n'.join(needs)", "tmux is missing or older than 3.2, the loop's Claude home cannot be built, or nobody is signed in there"),
("build_release/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how build-release starts itself", "--chunk is given outside a tmux window"),
("build_release/loop/main.py", "windows_opened", "1", "started.reason", "a chunk's window cannot be started"),
("build_release/loop/main.py", "windows_opened", "1", "f\"`tmux {' '.join(shown)}` failed", "the tmux session cannot be put on the terminal"),
+7 -2
View File
@@ -27,7 +27,11 @@ chunk starts a fresh session. Cycles continue until an exit below or Ctrl-C, eit
that one chunk.
Words: the setup's CLAUDE.md → "Vocabulary".
build-release needs tmux 3.2 or later, and checks it before it starts anything. Each chunk runs in
build-release runs every claude in the loop's Claude home, ~/.local/share/build-release/claude-home,
which it builds from the clone it ships in before it starts anything. Until someone has signed in
there, it stops and prints the line that does so. README.md → Install says what the home holds.
build-release needs tmux 3.2 or later, and checks that first too. Each chunk runs in
its own window of one tmux session per project, named loop-<key>, such as loop-claude-config. <key>
is the checkout's path relative to ~/code, or to / outside it, with a dash for each character other
than a letter, a digit, _ or -. The tmux session's @loop-root option names its checkout, and a
@@ -169,7 +173,8 @@ window closes, so the next build-release <task> reads the plan again.
Exit 1, saying why, when:
- no task is given
- git cannot start or finds no checkout
- tmux is missing or older than 3.2
- tmux is missing or older than 3.2, the loop's Claude home cannot be built, or nobody is signed
in there
- --chunk is given outside a tmux window
- a chunk's window cannot be started, as when the project's config cannot be read, or the tmux
session runs another checkout's chunks
+35 -8
View File
@@ -54,6 +54,14 @@ FAKE_CLAUDE = """#!{python}
import json, os, pathlib, signal, sys, time
argv = sys.argv[1:]
# What claude_home.home asks while it builds the loop's Claude home: someone is signed in, and each plugin installs.
if argv[:1] == ["plugin"]:
sys.exit(0)
if argv == ["auth", "status"]:
print(json.dumps({{"loggedIn": os.environ.get("FAKE_CLAUDE_SIGNED_OUT") is None}}))
if os.environ.get("FAKE_CLAUDE_VANISHES"):
os.unlink(sys.argv[0])
sys.exit(0)
role = "judge" if "--json-schema" in argv else "worker" if "-p" in argv else "interrupt"
scenario = pathlib.Path(os.environ["FAKE_CLAUDE_SCENARIO"])
steps = json.loads(scenario.read_text())
@@ -78,7 +86,7 @@ def session():
def transcript():
return pathlib.Path.home() / ".claude" / "projects" / "-fake" / f"{{session()}}.jsonl"
return pathlib.Path(os.environ["CLAUDE_CONFIG_DIR"]) / "projects" / "-fake" / f"{{session()}}.jsonl"
def append(*entries):
@@ -328,7 +336,7 @@ class Checkout:
return self.home / ".config/build-release" / "models.json"
def transcripts(self):
return sorted((self.home / ".claude" / "projects").glob("*/*.jsonl"))
return sorted((self.home / ".local/share/build-release/claude-home" / "projects").glob("*/*.jsonl"))
def archive(self):
return pathlib.Path(self.env.get("BUILD_RELEASE_TRANSCRIPTS", self.home / ".local/state/build-release" / "transcripts"))
@@ -442,8 +450,6 @@ class Cycles(unittest.TestCase):
(checkout.root / "sub").mkdir()
(checkout.root / "docs").mkdir()
(checkout.root / "docs" / "decisions.md").write_text("# Decisions\n")
(checkout.home / ".claude").mkdir()
(checkout.home / ".claude" / "CLAUDE.md").write_text("# Global instructions\n")
run = checkout.run([
worker(say="started"), judge(evidence="started"),
worker(say="landed-it", siblings=True), judge(finished=True, evidence="landed-it"),
@@ -767,7 +773,8 @@ class Cycles(unittest.TestCase):
def test_retries_a_worker_claude_cannot_start(self):
checkout = Checkout(self)
(checkout.temp / "fake-bin" / "claude").unlink()
# The claude that answers for the loop's Claude home is gone once the worker starts.
checkout.env["FAKE_CLAUDE_VANISHES"] = "1"
run = checkout.run([])
self.assertEqual((run.code, run.calls), (1, []))
self.assertEqual(run.sleeps, [60, 60])
@@ -907,6 +914,21 @@ class Windows(unittest.TestCase):
run = checkout.run([], argv=[TASK])
self.assertEqual((run.code, run.stderr), (1, "build-release runs each chunk in a tmux window, and needs tmux 3.2 or later; it found no tmux\n"))
def test_builds_the_loops_claude_home_from_the_clone_then_refuses_to_start_until_someone_signs_in_there(self):
checkout = self.checkout()
checkout.env["FAKE_CLAUDE_SIGNED_OUT"] = "1"
checkout.env["FAKE_TMUX_VERSION"] = "tmux 3.1c"
home = checkout.home / ".local/share/build-release/claude-home"
run = checkout.run([], argv=[TASK])
self.assertEqual((run.code, run.calls), (1, []))
self.assertEqual(run.stderr.splitlines(), [
"build-release runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c",
f"nobody is signed in to the loop's Claude home; run `CLAUDE_CONFIG_DIR={home} claude` and type /login there, once",
])
self.assertEqual((home / "CLAUDE.md").resolve(), BIN.parent / "CLAUDE.md")
self.assertIn(f"{BIN}/rule-hook", (home / "settings.json").read_text())
self.assertFalse((checkout.home / ".claude").exists())
def test_marks_the_window_while_an_interrupt_waits_and_rings_its_bell(self):
checkout = self.checkout()
run = checkout.run([worker(say="A or B?"), judge(interrupt=True, question="A or B?")], chunk="item-4")
@@ -1016,7 +1038,7 @@ class Interrupt(unittest.TestCase):
class Models(unittest.TestCase):
def catalog(self, checkout):
folder = checkout.home / ".claude" / "cache" / "model-catalog"
folder = checkout.home / ".local/share/build-release/claude-home" / "cache" / "model-catalog"
folder.mkdir(parents=True)
(folder / "old.json").write_text(json.dumps({"catalog": {"config": {"models": [{"id": "old-model"}]}}, "fetchedAt": 1}))
(folder / "new.json").write_text(json.dumps({"catalog": {"config": {"models": [{"id": "model-a", "name": "A"}, {"id": "model-b", "name": "B"}]}}, "fetchedAt": 2}))
@@ -1079,7 +1101,7 @@ class Models(unittest.TestCase):
checkout = Checkout(self)
del checkout.env["BUILD_RELEASE_JUDGE_MODEL"], checkout.env["BUILD_RELEASE_WORKER_MODEL"]
self.catalog(checkout)
catalog = checkout.home / ".claude" / "cache" / "model-catalog" / "new.json"
catalog = checkout.home / ".local/share/build-release/claude-home" / "cache" / "model-catalog" / "new.json"
for shape in ({"catalog": {"config": {"models": []}}}, {"catalog": None}, {"catalog": {"config": []}}, {"catalog": {"config": {"models": "x"}}}):
with self.subTest(shape=shape):
catalog.write_text(json.dumps({**shape, "fetchedAt": 3}))
@@ -1103,8 +1125,13 @@ class Models(unittest.TestCase):
class Reload(unittest.TestCase):
def test_an_edit_takes_effect_at_the_next_cycle_boundary_once_it_answers_help(self):
checkout = Checkout(self)
copy = checkout.temp / "bin"
# A clone of its own, since the loop's Claude home is built from the commit and the settings.json of the clone the script ships in.
clone = checkout.temp / "clone"
copy = clone / "bin"
shutil.copytree(BIN, copy, ignore=shutil.ignore_patterns("__pycache__", "test_*"))
(clone / "settings.json").write_text("{}\n")
for command in (["init", "-q"], ["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--allow-empty", "-m", "A release"]):
subprocess.run(["git", *command], check=True, cwd=clone, env=checkout.env)
checkout.script = copy / "build-release"
checkout.env["FAKE_CLAUDE_EDIT"] = str(copy)
run = checkout.run([