From 5d9cb8091227c385befbea2231bee38a66b6db43 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 25 Sep 2026 17:58:04 +0200 Subject: [PATCH 1/4] Say why the worker failed: the exit code, or exit 0 without a result event --- CHANGELOG.md | 1 + bin/loop-claude | 22 ++++++++++++---------- bin/test_loop_claude.py | 23 ++++++++++++++++++++++- todo.md | 1 - 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d27047c3..a0bc1cd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Track the authored configuration: `CLAUDE.md`, `AGENTS.md`, `settings.json`, `statusline.sh`, agents, skills and principles. Credentials, transcripts and memory files stay untracked. - `loop-claude ` runs a project's next chunk cycle after cycle, interrupting only for a decision that is yours or a release ready to cut. - `loop-claude` exits 1 when the judge fails three times running; a verdict missing a field or quoting nothing in the transcript is a failure. +- `loop-claude` says why the worker failed: the exit code, or an exit 0 without a result event. - `loop-claude` exits 2 when an interrupt gets no answer, saying why: stdin or stdout is not a terminal, or nothing was typed. Rerunning resumes the session. - `loop-claude` archives each session's transcript under `~/.claude/loop-claude-transcripts/`, one folder per project; `LOOP_CLAUDE_TRANSCRIPTS` overrides the root. - `loop-claude` runs at the checkout root wherever inside the checkout it is started. diff --git a/bin/loop-claude b/bin/loop-claude index 0e9b54d4..163e9d7e 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -262,16 +262,18 @@ def worker_args(task, name, session_id, resuming): def run_worker(args): proc = subprocess.Popen(["claude", *args], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) - done = {} + done = False for raw in proc.stdout: event = parse(raw) or {} if event.get("type") == "result": - done = event + done = True elif event.get("type") == "assistant": for part in event.get("message", {}).get("content", []): if line := said(part, 160): print(line, flush=True) - return done if proc.wait() == 0 else {} + if code := proc.wait(): + return f"claude exited {code}" + return "" if done else "claude exited 0 without a result event" def judge_prompt(task, premises, transcript): @@ -328,9 +330,8 @@ def interrupt(session_id, path, question): def run_cycle(task, premises, name, session_id): - worked = run_worker(worker_args(task, name, session_id, resuming=session_open(session_id))) - if not worked: - return False + if failure := run_worker(worker_args(task, name, session_id, resuming=session_open(session_id))): + return failure path = session_file(session_id) if not path: sys.exit(f"claude left no transcript for session {session_id}") @@ -344,7 +345,7 @@ def run_cycle(task, premises, name, session_id): if verdict["finished"]: archive(path, f"Task: {task}\n\nVerdict: finished") end_session(path) - return True + return "" def reload_if_edited(task, cycle_number, failures): @@ -378,13 +379,14 @@ def main(): premises = read_premises() name = session_name() print(f"\n=== cycle {cycle_number} ===", flush=True) - if run_cycle(task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name)): + failure = run_cycle(task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name)) + if not failure: failures = 0 continue failures += 1 if failures == 3: - sys.exit("the worker failed three times running") - print("the worker failed - retrying in a minute", file=sys.stderr) + sys.exit(f"the worker failed three times running: {failure}") + print(f"the worker failed: {failure} - retrying in a minute", file=sys.stderr) time.sleep(60) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 05f1a469..2a36670e 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -275,6 +275,22 @@ else: """ +class RunWorker(unittest.TestCase): + def test_says_whether_a_non_zero_exit_or_a_missing_result_event_failed_it(self): + with tempfile.TemporaryDirectory() as home: + stub = os.path.join(home, "claude") + scripts = { + "": 'print(json.dumps({"type": "result"}))', + "claude exited 0 without a result event": 'print(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}))', + "claude exited 3": "sys.exit(3)", + } + with unittest.mock.patch.dict(os.environ, {"PATH": f"{home}:{os.environ['PATH']}"}), unittest.mock.patch.object(sys, "stdout", io.StringIO()): + for failure, script in scripts.items(): + open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport sys\n{script}\n") + os.chmod(stub, 0o700) + self.assertEqual(loop.run_worker([]), failure) + + class RunCycle(unittest.TestCase): def test_resumes_until_the_judge_calls_the_chunk_finished_and_rejudges_a_failed_or_unquoted_verdict(self): with tempfile.TemporaryDirectory() as home: @@ -287,7 +303,7 @@ class RunCycle(unittest.TestCase): environment = {"HOME": home, "PATH": f"{home}:{os.environ['PATH']}", "STUB_CALLS": calls, "STUB_VERDICTS": verdicts} with unittest.mock.patch.dict(os.environ, environment), unittest.mock.patch("time.sleep") as sleep, unittest.mock.patch("sys.stdin.isatty", return_value=False): for _ in range(3): - self.assertTrue(loop.run_cycle("task", "premises", "A name", SESSION_ID)) + self.assertEqual(loop.run_cycle("task", "premises", "A name", SESSION_ID), "") open(verdicts, "w").write("ask") with self.assertRaises(SystemExit) as stopped, unittest.mock.patch("sys.stderr") as stderr: loop.run_cycle("task", "premises", "A name", SESSION_ID) @@ -338,6 +354,11 @@ class Main(unittest.TestCase): loop.main() mocks[2].assert_called_once_with("do it", 3, 2) self.assertIn("=== cycle 4 ===", sys.stdout.getvalue()) + mocks[3].side_effect = ["claude exited 3"] * 3 + stack.enter_context(unittest.mock.patch("time.sleep")) + with self.assertRaises(SystemExit) as stopped: + loop.main() + self.assertEqual(stopped.exception.code, "the worker failed three times running: claude exited 3") if __name__ == "__main__": diff --git a/todo.md b/todo.md index 115dda3d..ee50a303 100644 --- a/todo.md +++ b/todo.md @@ -4,7 +4,6 @@ Ordered by how many seats of the 2026-09-25 scoring panel (`docs/decisions.md`) named the place; each is one chunk. -- Say why the worker failed when `claude` exits 0 without a result event, today `run_worker` returns `{}` for that and for a non-zero exit and prints nothing. - Pass the checkout root to `project`, `read_premises` and `checkouts`, today each reads the cwd `main` set once. - Reject empty evidence in `grounded` by name, today `bool(evidence) and evidence in transcript`, where a tidy-up drops the guard and `"" in transcript` is always true. - Split `CLAUDE.md`'s release bullet under "Working loop" so the plan-it and stop predicates each stand alone, today six rules in one paragraph differing by one clause. -- 2.52.0 From 5d59d54b518f3ba5118f8d233b3ffa3476736b7a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 25 Sep 2026 18:00:51 +0200 Subject: [PATCH 2/4] Drive the retry line in the main test from zero failures --- bin/test_loop_claude.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 2a36670e..fa7cc65e 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -277,14 +277,14 @@ else: class RunWorker(unittest.TestCase): def test_says_whether_a_non_zero_exit_or_a_missing_result_event_failed_it(self): - with tempfile.TemporaryDirectory() as home: - stub = os.path.join(home, "claude") + with tempfile.TemporaryDirectory() as bin: + stub = os.path.join(bin, "claude") scripts = { "": 'print(json.dumps({"type": "result"}))', "claude exited 0 without a result event": 'print(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}))', "claude exited 3": "sys.exit(3)", } - with unittest.mock.patch.dict(os.environ, {"PATH": f"{home}:{os.environ['PATH']}"}), unittest.mock.patch.object(sys, "stdout", io.StringIO()): + with unittest.mock.patch.dict(os.environ, {"PATH": f"{bin}:{os.environ['PATH']}"}), unittest.mock.patch.object(sys, "stdout", io.StringIO()): for failure, script in scripts.items(): open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport sys\n{script}\n") os.chmod(stub, 0o700) @@ -355,9 +355,13 @@ class Main(unittest.TestCase): mocks[2].assert_called_once_with("do it", 3, 2) self.assertIn("=== cycle 4 ===", sys.stdout.getvalue()) mocks[3].side_effect = ["claude exited 3"] * 3 - stack.enter_context(unittest.mock.patch("time.sleep")) + stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "0", "do it"])) + stack.enter_context(unittest.mock.patch.object(sys, "stderr", io.StringIO())) + sleep = stack.enter_context(unittest.mock.patch("time.sleep")) with self.assertRaises(SystemExit) as stopped: loop.main() + self.assertIn("the worker failed: claude exited 3 - retrying in a minute", sys.stderr.getvalue()) + self.assertEqual(sleep.call_count, 2) self.assertEqual(stopped.exception.code, "the worker failed three times running: claude exited 3") -- 2.52.0 From b633b98e707e99d77926420593f48cbafbee88fa Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 25 Sep 2026 18:02:43 +0200 Subject: [PATCH 3/4] Name a signal kill, match the judge's retry line, state worker failure in --help --- bin/loop-claude | 14 +++++++++----- bin/test_loop_claude.py | 5 +++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index 163e9d7e..1dac205b 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -11,9 +11,10 @@ An unfinished chunk resumes the same session next cycle; a finished one starts a Cycles continue until that interrupt or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary". Exit 1: no task, not inside a checkout, no premises, claude leaves no transcript, or the worker or -the judge fails three times running. A judge fails when its verdict misses a field or quotes nothing -in the transcript. Exit 2: the human is needed and stdin or stdout is not a terminal, or the -interrupt ended without them typing anything. Rerunning resumes any open session. +the judge fails three times running. A worker fails when claude exits non-zero, is killed, or ends +without a result event; a judge when its verdict misses a field or quotes nothing in the transcript. +Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without +them typing anything. Rerunning resumes any open session. Each project has one session, "A loop-claude", being the checkout relative to ~/code, or to / outside it, deleted when the chunk finishes. Its transcript is archived under @@ -271,7 +272,10 @@ def run_worker(args): for part in event.get("message", {}).get("content", []): if line := said(part, 160): print(line, flush=True) - if code := proc.wait(): + code = proc.wait() + if code < 0: + return f"claude killed by signal {-code}" + if code: return f"claude exited {code}" return "" if done else "claude exited 0 without a result event" @@ -386,7 +390,7 @@ def main(): failures += 1 if failures == 3: sys.exit(f"the worker failed three times running: {failure}") - print(f"the worker failed: {failure} - retrying in a minute", file=sys.stderr) + print(f"retrying the worker in a minute: {failure}", file=sys.stderr) time.sleep(60) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index fa7cc65e..a0017d47 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -283,10 +283,11 @@ class RunWorker(unittest.TestCase): "": 'print(json.dumps({"type": "result"}))', "claude exited 0 without a result event": 'print(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}))', "claude exited 3": "sys.exit(3)", + "claude killed by signal 9": "os.kill(os.getpid(), 9)", } with unittest.mock.patch.dict(os.environ, {"PATH": f"{bin}:{os.environ['PATH']}"}), unittest.mock.patch.object(sys, "stdout", io.StringIO()): for failure, script in scripts.items(): - open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport sys\n{script}\n") + open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport os\nimport sys\n{script}\n") os.chmod(stub, 0o700) self.assertEqual(loop.run_worker([]), failure) @@ -360,7 +361,7 @@ class Main(unittest.TestCase): sleep = stack.enter_context(unittest.mock.patch("time.sleep")) with self.assertRaises(SystemExit) as stopped: loop.main() - self.assertIn("the worker failed: claude exited 3 - retrying in a minute", sys.stderr.getvalue()) + self.assertIn("retrying the worker in a minute: claude exited 3", sys.stderr.getvalue()) self.assertEqual(sleep.call_count, 2) self.assertEqual(stopped.exception.code, "the worker failed three times running: claude exited 3") -- 2.52.0 From e38c8168f2bab6decfbf1760549c90ca8ca763ac Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 25 Sep 2026 18:03:12 +0200 Subject: [PATCH 4/4] Name the signal kill in the changelog line --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bc1cd5..bd10dfd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - Track the authored configuration: `CLAUDE.md`, `AGENTS.md`, `settings.json`, `statusline.sh`, agents, skills and principles. Credentials, transcripts and memory files stay untracked. - `loop-claude ` runs a project's next chunk cycle after cycle, interrupting only for a decision that is yours or a release ready to cut. - `loop-claude` exits 1 when the judge fails three times running; a verdict missing a field or quoting nothing in the transcript is a failure. -- `loop-claude` says why the worker failed: the exit code, or an exit 0 without a result event. +- `loop-claude` says why the worker failed: the exit code, the signal that killed it, or an exit 0 without a result event. - `loop-claude` exits 2 when an interrupt gets no answer, saying why: stdin or stdout is not a terminal, or nothing was typed. Rerunning resumes the session. - `loop-claude` archives each session's transcript under `~/.claude/loop-claude-transcripts/`, one folder per project; `LOOP_CLAUDE_TRANSCRIPTS` overrides the root. - `loop-claude` runs at the checkout root wherever inside the checkout it is started. -- 2.52.0