From 67451b418a5983bdb068292ba5af6141030a9bdf Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 24 Sep 2026 23:27:31 +0200 Subject: [PATCH 1/5] Name the session invariant run_cycle resumes on, and say which outcome ended an interrupt --- CHANGELOG.md | 1 + bin/loop-claude | 14 ++++++++++---- bin/test_loop_claude.py | 41 ++++++++++++++++++++++++++++++++++------- todo.md | 1 - 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a474599c..a97c6b93 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` exits 2 saying which ended the interrupt: no terminal attached, or nothing typed. - `loop-claude --help` names the Claude Code version its transcript, session and trust handling were observed on; check it first when an upgrade breaks a cycle. - `loop-claude` runs the model each project's settings pick; `WORKER_MODEL` and `JUDGE_MODEL` override it. This repo runs on Fable 5.1. - Reviewers share one protocol for verdicts, severities, rounds and declines: `agents/review-protocol.md`. diff --git a/bin/loop-claude b/bin/loop-claude index a414d5d2..75912083 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -133,6 +133,11 @@ def end_session(path): print(f"could not remove {sibling}: {failure}", file=sys.stderr) +def session_open(session_id): + """The transcript outlives its cycle until end_session removes it: present means the chunk is unfinished.""" + return bool(session_file(session_id)) + + def parse(raw): try: entry = json.loads(raw) @@ -306,15 +311,15 @@ def judge(task, premises, transcript): def interrupt(session_id, path, question): print(f"\n{question}", file=sys.stderr) if not (sys.stdin.isatty() and sys.stdout.isatty()): - return False + return "the human is needed and no terminal is attached" trust(checkouts()) before = human_turns(entries(path)) subprocess.run(["claude", "--permission-mode", "bypassPermissions", "--resume", str(session_id)]) - return human_turns(entries(path)) > before + return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything" def run_cycle(task, premises, name, session_id): - worked = run_worker(worker_args(task, name, session_id, resuming=bool(session_file(session_id)))) + worked = run_worker(worker_args(task, name, session_id, resuming=session_open(session_id))) if not worked: return False path = session_file(session_id) @@ -323,7 +328,8 @@ def run_cycle(task, premises, name, session_id): verdict = judge(task, premises, render(entries(path))) while verdict["ask"]: archive(path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}") - if not interrupt(session_id, path, verdict["question"]): + if failure := interrupt(session_id, path, verdict["question"]): + print(failure, file=sys.stderr) sys.exit(2) verdict = judge(task, premises, render(entries(path))) if verdict["finished"]: diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index ae63af3e..d6229fad 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -124,15 +124,37 @@ class Said(unittest.TestCase): class EndSession(unittest.TestCase): - def test_removes_the_transcript_and_its_sibling_folder(self): - with tempfile.TemporaryDirectory() as root: + def test_removes_the_transcript_and_its_siblings_and_closes_the_session(self): + with tempfile.TemporaryDirectory() as home, unittest.mock.patch.dict(os.environ, {"HOME": home}): + root = os.path.join(home, ".claude", "projects", "p") + os.makedirs(root) transcript = os.path.join(root, f"{SESSION_ID}.jsonl") open(transcript, "w").write("{}\n") os.makedirs(os.path.join(root, SESSION_ID, "subagents")) os.makedirs(os.path.join(root, f"{SESSION_ID}.superseded-1", "tool-results")) open(os.path.join(root, f"{SESSION_ID}.orphaned-1-ab.jsonl"), "w").write("{}\n") + self.assertTrue(loop.session_open(SESSION_ID)) loop.end_session(transcript) self.assertEqual(os.listdir(root), []) + self.assertFalse(loop.session_open(SESSION_ID)) + + +class Interrupt(unittest.TestCase): + def test_says_whether_no_terminal_or_nothing_typed_ended_it(self): + with tempfile.TemporaryDirectory() as root: + transcript = os.path.join(root, "session.jsonl") + open(transcript, "w").write("{}\n") + with unittest.mock.patch("sys.stdin.isatty", return_value=False): + self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "the human is needed and no terminal is attached") + with ( + unittest.mock.patch("sys.stdin.isatty", return_value=True), + unittest.mock.patch("sys.stdout.isatty", return_value=True), + unittest.mock.patch.multiple(loop, checkouts=unittest.mock.DEFAULT, trust=unittest.mock.DEFAULT), + unittest.mock.patch("subprocess.run") as run, + ): + self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "the interrupt ended without the human typing anything") + run.side_effect = lambda *args, **kwargs: open(transcript, "a").write(json.dumps(human("typed")) + "\n") + self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "") class Parse(unittest.TestCase): @@ -166,8 +188,8 @@ if "--json-schema" in args: open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:])) if plan[0] == "fail": sys.exit(1) - unquoted = plan[0] == "unquoted" - print(json.dumps({"structured_output": {"ask": unquoted, "evidence": "invented" if unquoted else "", "finished": plan[0] == "finished", "question": "q?" if unquoted else ""}})) + ask = plan[0] in ("ask", "unquoted") + print(json.dumps({"structured_output": {"ask": ask, "evidence": {"ask": "worked", "unquoted": "invented"}.get(plan[0], ""), "finished": plan[0] == "finished", "question": "q?" if ask else ""}})) else: flag = "--session-id" if "--session-id" in args else "--resume" folder = os.path.expanduser("~/.claude/projects/p") @@ -187,9 +209,14 @@ class RunCycle(unittest.TestCase): open(verdicts, "w").write("fail continue finished unquoted continue") loop.ARCHIVE = os.path.join(home, "archive") 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: + 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)) + 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) + self.assertEqual(stopped.exception.code, 2) + self.assertIn("no terminal is attached", "".join(str(call.args[0]) for call in stderr.write.call_args_list)) for plan in ("fail fail fail", "unquoted unquoted unquoted"): open(verdicts, "w").write(plan) with self.assertRaises(SystemExit) as stopped: @@ -199,10 +226,10 @@ class RunCycle(unittest.TestCase): self.assertIn("asking: q?", stopped.exception.code) self.assertEqual(open(verdicts).read(), "") starts = [args[args.index("-p") + 1:][:3] for args in map(json.loads, open(calls)) if "--json-schema" not in args] - self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume"]) + self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume", "--resume"]) self.assertEqual(starts[1][2], SESSION_ID) self.assertEqual(sleep.call_count, 6) - [copy] = [name for _, _, names in os.walk(loop.ARCHIVE) for name in names] + self.assertTrue([name for _, _, names in os.walk(loop.ARCHIVE) for name in names]) class Reload(unittest.TestCase): diff --git a/todo.md b/todo.md index 0ba865d3..cfb60a10 100644 --- a/todo.md +++ b/todo.md @@ -4,7 +4,6 @@ Ordered by how many seats of the 2026-09-24 scoring panel (`docs/decisions.md`) named the place; each is one chunk. -- Decide resume-or-fresh in `run_cycle` from state it owns, and give `interrupt`'s no-tty and nothing-typed outcomes two distinct results. - Give the concept words one definition a reader lands on, cited from where they are used; today three seats found them only in `CHANGELOG.md`'s one-word-per-concept entry. - Keep `###` sub-headings inside their `##` section in `sections`, pinned by a test; today a sub-heading under `## Goals` silently drops the rest of the goals. - Merge `project` and `project_path` into one name and one shape for the project's path. -- 2.52.0 From 14b7e72ae3178b0024da0536e4d7c4b1bb0008d1 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 24 Sep 2026 23:30:53 +0200 Subject: [PATCH 2/5] Pin the archive before the interrupt on stderr --- bin/test_loop_claude.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index d6229fad..ca5d7e5f 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -216,7 +216,9 @@ class RunCycle(unittest.TestCase): with self.assertRaises(SystemExit) as stopped, unittest.mock.patch("sys.stderr") as stderr: loop.run_cycle("task", "premises", "A name", SESSION_ID) self.assertEqual(stopped.exception.code, 2) - self.assertIn("no terminal is attached", "".join(str(call.args[0]) for call in stderr.write.call_args_list)) + said = "".join(str(call.args[0]) for call in stderr.write.call_args_list) + self.assertIn("transcript:", said) + self.assertIn("no terminal is attached", said) for plan in ("fail fail fail", "unquoted unquoted unquoted"): open(verdicts, "w").write(plan) with self.assertRaises(SystemExit) as stopped: @@ -229,7 +231,6 @@ class RunCycle(unittest.TestCase): self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume", "--resume"]) self.assertEqual(starts[1][2], SESSION_ID) self.assertEqual(sleep.call_count, 6) - self.assertTrue([name for _, _, names in os.walk(loop.ARCHIVE) for name in names]) class Reload(unittest.TestCase): -- 2.52.0 From 27dc866e7d16881f7f55c7fc315de202479993c0 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 24 Sep 2026 23:33:24 +0200 Subject: [PATCH 3/5] Name the stream that is not a terminal, and the rerun after exit 2 --- CHANGELOG.md | 2 +- bin/loop-claude | 11 +++++------ bin/test_loop_claude.py | 4 ++-- todo.md | 1 + 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a97c6b93..2560c7c2 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` exits 2 saying which ended the interrupt: no terminal attached, or nothing typed. +- `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 --help` names the Claude Code version its transcript, session and trust handling were observed on; check it first when an upgrade breaks a cycle. - `loop-claude` runs the model each project's settings pick; `WORKER_MODEL` and `JUDGE_MODEL` override it. This repo runs on Fable 5.1. - Reviewers share one protocol for verdicts, severities, rounds and declines: `agents/review-protocol.md`. diff --git a/bin/loop-claude b/bin/loop-claude index 75912083..a6c1a18c 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -10,10 +10,10 @@ unfinished chunk resumes the same session next cycle; a finished one starts a fr continue until that interrupt or Ctrl-C. Exit 1: no task, no premises, claude leaves no transcript, or the worker or the judge fails three -times running, a verdict missing a field or quoting nothing in the transcript being a judge failure; -rerunning resumes the session. Exit 2: the human is needed and no terminal is attached, or the -interrupt ended without them typing anything. Before an interrupt every worktree is marked trusted in -~/.claude.json. +times running, a verdict missing a field or quoting nothing in the transcript being a judge failure. +Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without +them typing anything. Rerunning resumes the session after either. Before an interrupt every worktree +is marked trusted in ~/.claude.json. Each project has one session, "A ~/code/ auto-mode" (the absolute path outside ~/code), deleted when the chunk finishes. Its transcript is archived under $AUTO_MODE_TRANSCRIPTS (default @@ -134,7 +134,6 @@ def end_session(path): def session_open(session_id): - """The transcript outlives its cycle until end_session removes it: present means the chunk is unfinished.""" return bool(session_file(session_id)) @@ -311,7 +310,7 @@ def judge(task, premises, transcript): def interrupt(session_id, path, question): print(f"\n{question}", file=sys.stderr) if not (sys.stdin.isatty() and sys.stdout.isatty()): - return "the human is needed and no terminal is attached" + return "stdin or stdout is not a terminal" trust(checkouts()) before = human_turns(entries(path)) subprocess.run(["claude", "--permission-mode", "bypassPermissions", "--resume", str(session_id)]) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index ca5d7e5f..0893b276 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -145,7 +145,7 @@ class Interrupt(unittest.TestCase): transcript = os.path.join(root, "session.jsonl") open(transcript, "w").write("{}\n") with unittest.mock.patch("sys.stdin.isatty", return_value=False): - self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "the human is needed and no terminal is attached") + self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "stdin or stdout is not a terminal") with ( unittest.mock.patch("sys.stdin.isatty", return_value=True), unittest.mock.patch("sys.stdout.isatty", return_value=True), @@ -218,7 +218,7 @@ class RunCycle(unittest.TestCase): self.assertEqual(stopped.exception.code, 2) said = "".join(str(call.args[0]) for call in stderr.write.call_args_list) self.assertIn("transcript:", said) - self.assertIn("no terminal is attached", said) + self.assertIn("not a terminal", said) for plan in ("fail fail fail", "unquoted unquoted unquoted"): open(verdicts, "w").write(plan) with self.assertRaises(SystemExit) as stopped: diff --git a/todo.md b/todo.md index cfb60a10..20fcea01 100644 --- a/todo.md +++ b/todo.md @@ -11,4 +11,5 @@ Ordered by how many seats of the 2026-09-24 scoring panel (`docs/decisions.md`) - Pin every `CLAUDE.md` heading the agents and skills cite with a test that resolves each citation. - Define chunk, plan and release inside `JUDGE_RULES`; the judge reads nothing else. - Name the `set -u` empty-array idiom and the `@sh` eval in `statusline.sh`, one line each. +- Make `CLAUDE.md`'s comment rules say an invariant earns a line only where the code cannot show it, state the one-line rule once, and scope the docstring exemption to what a tool consumes; today a reviewer finds two sentences saying keep and one saying delete. - Re-run the four-seat scoring panel and record the score in `docs/decisions.md`; the target is every dimension at 7.0 or above. -- 2.52.0 From edff375ff932e01acf36c2e7c1971d12d3d3cbd4 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 24 Sep 2026 23:35:02 +0200 Subject: [PATCH 4/5] Pin the archive on its destination, and bind the rerun to both exits --- bin/loop-claude | 2 +- bin/test_loop_claude.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index a6c1a18c..9966611e 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -12,7 +12,7 @@ continue until that interrupt or Ctrl-C. Exit 1: no task, no premises, claude leaves no transcript, or the worker or the judge fails three times running, a verdict missing a field or quoting nothing in the transcript being a judge failure. Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without -them typing anything. Rerunning resumes the session after either. Before an interrupt every worktree +them typing anything. Rerunning resumes the session after either exit. Before an interrupt every worktree is marked trusted in ~/.claude.json. Each project has one session, "A ~/code/ auto-mode" (the absolute path outside ~/code), diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 0893b276..78210f4e 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -217,7 +217,7 @@ class RunCycle(unittest.TestCase): loop.run_cycle("task", "premises", "A name", SESSION_ID) self.assertEqual(stopped.exception.code, 2) said = "".join(str(call.args[0]) for call in stderr.write.call_args_list) - self.assertIn("transcript:", said) + self.assertIn(f"transcript: {loop.ARCHIVE}", said) self.assertIn("not a terminal", said) for plan in ("fail fail fail", "unquoted unquoted unquoted"): open(verdicts, "w").write(plan) -- 2.52.0 From f58ba33cb0cf20272b71005da46c2ed2d36d6277 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 24 Sep 2026 23:36:21 +0200 Subject: [PATCH 5/5] Rewrap --help --- bin/loop-claude | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index 9966611e..eea96ac8 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -12,8 +12,8 @@ continue until that interrupt or Ctrl-C. Exit 1: no task, no premises, claude leaves no transcript, or the worker or the judge fails three times running, a verdict missing a field or quoting nothing in the transcript being a judge failure. Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without -them typing anything. Rerunning resumes the session after either exit. Before an interrupt every worktree -is marked trusted in ~/.claude.json. +them typing anything. Rerunning resumes the session after either exit. Before an interrupt every +worktree is marked trusted in ~/.claude.json. Each project has one session, "A ~/code/ auto-mode" (the absolute path outside ~/code), deleted when the chunk finishes. Its transcript is archived under $AUTO_MODE_TRANSCRIPTS (default -- 2.52.0