Fix the round loop's judge retry, reload and archive modes in loop-claude
This commit is contained in:
+55
-41
@@ -9,15 +9,16 @@ or a release ready to cut, hands the session over: `claude --resume` on the term
|
||||
chunk resumes the same session next round; a finished one starts a fresh session. Rounds continue
|
||||
until Ctrl-C or that handoff.
|
||||
|
||||
Exit 1: no task, the README gate blocks, claude leaves no transcript, or claude fails three rounds
|
||||
running. Exit 2: the human is needed and no terminal is attached, or the handoff ended without them
|
||||
typing anything. Before a handoff every worktree is marked trusted in ~/.claude.json.
|
||||
Exit 1: no task, the README gate blocks, claude leaves no transcript, or the worker or the judge fails
|
||||
three times running. Exit 2: the human is needed and no terminal is attached, or the handoff ended
|
||||
without them typing anything. Before a handoff every worktree is marked trusted in ~/.claude.json.
|
||||
|
||||
Each project has one session, "A ~/code/<path> auto-mode" (the absolute path outside ~/code),
|
||||
resumed while present and deleted when the chunk finishes. Its transcript is archived under
|
||||
$AUTO_MODE_TRANSCRIPTS (default ~/.claude/auto-mode-transcripts/<path>/) whenever the human is asked
|
||||
and when the chunk finishes. JUDGE_MODEL and WORKER_MODEL override the model the project's
|
||||
Claude Code settings pick. A change to this file takes effect at the next round boundary.
|
||||
Claude Code settings pick. A change to this file takes effect at the next round boundary, once the
|
||||
new file answers --help.
|
||||
|
||||
Claude Code internals this rests on, observed on 2.1.282:
|
||||
- The transcript is ~/.claude/projects/*/<session-id>.jsonl, one JSON object per line. A <session-id>/
|
||||
@@ -39,6 +40,7 @@ import time
|
||||
import uuid
|
||||
|
||||
ARCHIVE = os.path.expanduser(os.environ.get("AUTO_MODE_TRANSCRIPTS", "~/.claude/auto-mode-transcripts"))
|
||||
FAILURES = "LOOP_CLAUDE_FAILURES"
|
||||
ROUND = "LOOP_CLAUDE_ROUND"
|
||||
SESSION_NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
|
||||
SOURCE = open(__file__, encoding="utf-8").read()
|
||||
@@ -82,6 +84,8 @@ def sections(text):
|
||||
if line.startswith("## "):
|
||||
current = line.strip()[3:]
|
||||
found[current] = []
|
||||
elif line.startswith("#"):
|
||||
current = None
|
||||
elif current and line.strip():
|
||||
found[current].append(line)
|
||||
return {name: "\n".join(body) for name, body in found.items() if body}
|
||||
@@ -97,24 +101,20 @@ def gate():
|
||||
return f"## Goals\n{found['Goals']}\n\n## Audience\n{found['Audience']}"
|
||||
|
||||
|
||||
def project_path():
|
||||
def project():
|
||||
root = os.path.expanduser("~/code")
|
||||
cwd = os.getcwd()
|
||||
if cwd == root:
|
||||
return "code"
|
||||
if cwd.startswith(f"{root}/"):
|
||||
return os.path.relpath(cwd, root)
|
||||
return cwd.lstrip("/")
|
||||
if cwd == root or cwd.startswith(f"{root}/"):
|
||||
return "~/" + os.path.relpath(cwd, os.path.dirname(root))
|
||||
return cwd
|
||||
|
||||
|
||||
def project_path():
|
||||
return project().removeprefix("~").lstrip("/")
|
||||
|
||||
|
||||
def session_name():
|
||||
root = os.path.expanduser("~/code")
|
||||
cwd = os.getcwd()
|
||||
if cwd == root:
|
||||
return "A ~/code auto-mode"
|
||||
if cwd.startswith(f"{root}/"):
|
||||
return f"A ~/code/{os.path.relpath(cwd, root)} auto-mode"
|
||||
return f"A {cwd} auto-mode"
|
||||
return f"A {project()} auto-mode"
|
||||
|
||||
|
||||
def session_file(session_id):
|
||||
@@ -161,25 +161,28 @@ def render(entries):
|
||||
return "\n\n".join(lines)
|
||||
|
||||
|
||||
def typed(content):
|
||||
if isinstance(content, str):
|
||||
return not content.startswith("<")
|
||||
parts = content if isinstance(content, list) else []
|
||||
return bool(parts) and all(isinstance(part, dict) and part.get("type") == "text" and not part.get("text", "").startswith("[Request interrupted") for part in parts)
|
||||
|
||||
|
||||
def human_turns(entries):
|
||||
spoken = 0
|
||||
for entry in entries:
|
||||
if entry.get("type") != "user" or entry.get("isMeta"):
|
||||
continue
|
||||
content = entry.get("message", {}).get("content")
|
||||
if isinstance(content, str) and not content.startswith("<"):
|
||||
spoken += 1
|
||||
elif isinstance(content, list) and not any(said(part) == "" or said(part).startswith("[Request interrupted") for part in content):
|
||||
spoken += 1
|
||||
return spoken
|
||||
return sum(1 for entry in entries if entry.get("type") == "user" and not entry.get("isMeta") and typed(entry.get("message", {}).get("content")))
|
||||
|
||||
|
||||
def make_private(folder):
|
||||
if folder and not os.path.isdir(folder):
|
||||
make_private(os.path.dirname(folder))
|
||||
os.mkdir(folder, 0o700)
|
||||
|
||||
|
||||
def archive(path, note):
|
||||
folder = os.path.join(ARCHIVE, project_path())
|
||||
try:
|
||||
stamp = time.strftime("%Y-%m-%dT%H-%M-%SZ", time.gmtime(os.path.getmtime(path)))
|
||||
os.makedirs(folder, mode=0o700, exist_ok=True)
|
||||
os.chmod(ARCHIVE, 0o700)
|
||||
make_private(folder)
|
||||
dest = os.path.join(folder, f"{stamp}.md")
|
||||
if not os.path.exists(dest):
|
||||
with open(os.open(dest, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as copy:
|
||||
@@ -291,6 +294,15 @@ def judge(task, premises, transcript):
|
||||
return verdict
|
||||
|
||||
|
||||
def verdict_on(task, premises, path):
|
||||
for attempt in range(3):
|
||||
if verdict := judge(task, premises, render(entries(path))):
|
||||
return verdict
|
||||
print("the judge failed - retrying in a minute", file=sys.stderr)
|
||||
time.sleep(60)
|
||||
sys.exit("the judge failed three times running")
|
||||
|
||||
|
||||
def handoff(session_id, path, question):
|
||||
print(f"\n{question}", file=sys.stderr)
|
||||
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
||||
@@ -308,29 +320,31 @@ def run_round(task, premises, name, session_id):
|
||||
path = session_file(session_id)
|
||||
if not path:
|
||||
sys.exit(f"claude left no transcript for session {session_id}")
|
||||
verdict = judge(task, premises, render(entries(path)))
|
||||
while verdict and verdict["ask"]:
|
||||
verdict = verdict_on(task, premises, path)
|
||||
while verdict["ask"]:
|
||||
archive(path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}")
|
||||
if not handoff(session_id, path, verdict["question"]):
|
||||
sys.exit(2)
|
||||
verdict = judge(task, premises, render(entries(path)))
|
||||
if not verdict:
|
||||
return False
|
||||
verdict = verdict_on(task, premises, path)
|
||||
if verdict["finished"]:
|
||||
archive(path, f"Task: {task}\n\nVerdict: finished")
|
||||
end_session(path)
|
||||
return True
|
||||
|
||||
|
||||
def reload_if_edited(round_number):
|
||||
def reload_if_edited(round_number, failures):
|
||||
try:
|
||||
source = open(__file__, encoding="utf-8").read()
|
||||
compile(source, __file__, "exec")
|
||||
except (OSError, SyntaxError) as failure:
|
||||
except OSError as failure:
|
||||
print(f"keeping the running script: {failure}", file=sys.stderr)
|
||||
return
|
||||
if source == SOURCE:
|
||||
return
|
||||
trial = subprocess.run([sys.executable, __file__, "--help"], capture_output=True, text=True)
|
||||
if trial.returncode != 0:
|
||||
print(f"keeping the running script: {trial.stderr.strip()}", file=sys.stderr)
|
||||
return
|
||||
os.environ[FAILURES] = str(failures)
|
||||
os.environ[ROUND] = str(round_number)
|
||||
os.execv(sys.executable, [sys.executable, __file__, *sys.argv[1:]])
|
||||
|
||||
@@ -340,10 +354,10 @@ def main():
|
||||
if task in ("", "-h", "--help"):
|
||||
print(__doc__.strip())
|
||||
sys.exit(0 if task else 1)
|
||||
failures = int(os.environ.pop(FAILURES, 0))
|
||||
round_number = int(os.environ.pop(ROUND, 0))
|
||||
failures = 0
|
||||
while True:
|
||||
reload_if_edited(round_number)
|
||||
reload_if_edited(round_number, failures)
|
||||
round_number += 1
|
||||
premises = gate()
|
||||
name = session_name()
|
||||
@@ -353,8 +367,8 @@ def main():
|
||||
continue
|
||||
failures += 1
|
||||
if failures == 3:
|
||||
sys.exit("claude failed three rounds running")
|
||||
print("claude failed - retrying in a minute", file=sys.stderr)
|
||||
sys.exit("the worker failed three times running")
|
||||
print("the worker failed - retrying in a minute", file=sys.stderr)
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
|
||||
+78
-5
@@ -27,7 +27,7 @@ def human(content):
|
||||
|
||||
class Sections(unittest.TestCase):
|
||||
def test_keeps_only_headed_sections_with_a_body(self):
|
||||
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n"
|
||||
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n# Appendix\nnot audience\n"
|
||||
self.assertEqual(loop.sections(text), {"Goals": "1. one\n2. two", "Audience": "me"})
|
||||
|
||||
|
||||
@@ -50,10 +50,25 @@ class HumanTurns(unittest.TestCase):
|
||||
human("<command-name>/model</command-name>"),
|
||||
{**human("peer message"), "isMeta": True},
|
||||
human([{"type": "text", "text": "[Request interrupted by user]"}]),
|
||||
human([]),
|
||||
assistant({"type": "text", "text": "said"}),
|
||||
]), 2)
|
||||
|
||||
|
||||
class Project(unittest.TestCase):
|
||||
def test_names_a_checkout_under_code_by_its_relative_path_and_any_other_by_its_absolute_one(self):
|
||||
with unittest.mock.patch.dict(os.environ, {"HOME": "/h"}):
|
||||
with unittest.mock.patch("os.getcwd", return_value="/h/code/a/b"):
|
||||
self.assertEqual(loop.session_name(), "A ~/code/a/b auto-mode")
|
||||
self.assertEqual(loop.project_path(), "code/a/b")
|
||||
with unittest.mock.patch("os.getcwd", return_value="/h/code"):
|
||||
self.assertEqual(loop.session_name(), "A ~/code auto-mode")
|
||||
self.assertEqual(loop.project_path(), "code")
|
||||
with unittest.mock.patch("os.getcwd", return_value="/srv/x"):
|
||||
self.assertEqual(loop.session_name(), "A /srv/x auto-mode")
|
||||
self.assertEqual(loop.project_path(), "srv/x")
|
||||
|
||||
|
||||
class WorkerArgs(unittest.TestCase):
|
||||
def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self):
|
||||
args = loop.worker_args("the task", "A name", SESSION_ID, resuming=False)
|
||||
@@ -85,15 +100,18 @@ class JudgePrompt(unittest.TestCase):
|
||||
|
||||
|
||||
class Archive(unittest.TestCase):
|
||||
def test_keeps_the_copy_as_private_as_the_transcript(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
def test_keeps_the_copy_and_every_folder_it_creates_as_private_as_the_transcript(self):
|
||||
with tempfile.TemporaryDirectory() as root, unittest.mock.patch("os.getcwd", return_value="/srv/a/b"):
|
||||
transcript = os.path.join(root, "session.jsonl")
|
||||
open(transcript, "w").write(json.dumps(assistant({"type": "text", "text": "said"})) + "\n")
|
||||
loop.ARCHIVE = os.path.join(root, "archive")
|
||||
loop.ARCHIVE = os.path.join(root, "deep", "archive")
|
||||
loop.archive(transcript, "note")
|
||||
[copy] = [os.path.join(folder, name) for folder, _, names in os.walk(loop.ARCHIVE) for name in names]
|
||||
self.assertEqual(stat.S_IMODE(os.stat(copy).st_mode), 0o600)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(os.path.dirname(copy)).st_mode), 0o700)
|
||||
folder = os.path.dirname(copy)
|
||||
while folder != root:
|
||||
self.assertEqual(stat.S_IMODE(os.stat(folder).st_mode), 0o700, folder)
|
||||
folder = os.path.dirname(folder)
|
||||
self.assertIn("said", open(copy).read())
|
||||
|
||||
|
||||
@@ -139,5 +157,60 @@ class Verdict(unittest.TestCase):
|
||||
self.assertFalse(loop.complete(None))
|
||||
|
||||
|
||||
STUB_CLAUDE = """#!/usr/bin/env python3
|
||||
import json, os, sys
|
||||
args = sys.argv[1:]
|
||||
open(os.environ["STUB_CALLS"], "a").write(json.dumps(args) + "\\n")
|
||||
if "--json-schema" in args:
|
||||
plan = open(os.environ["STUB_VERDICTS"]).read().split()
|
||||
open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:]))
|
||||
if plan[0] == "fail":
|
||||
sys.exit(1)
|
||||
print(json.dumps({"structured_output": {"ask": False, "evidence": "", "finished": plan[0] == "finished", "question": ""}}))
|
||||
else:
|
||||
flag = "--session-id" if "--session-id" in args else "--resume"
|
||||
folder = os.path.expanduser("~/.claude/projects/p")
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
open(os.path.join(folder, args[args.index(flag) + 1] + ".jsonl"), "a").write(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}) + "\\n")
|
||||
print(json.dumps({"type": "result"}))
|
||||
"""
|
||||
|
||||
|
||||
class RunRound(unittest.TestCase):
|
||||
def test_resumes_until_the_judge_calls_the_chunk_finished_and_rejudges_a_judge_failure(self):
|
||||
with tempfile.TemporaryDirectory() as home:
|
||||
stub = os.path.join(home, "claude")
|
||||
open(stub, "w").write(STUB_CLAUDE)
|
||||
os.chmod(stub, 0o700)
|
||||
calls, verdicts = os.path.join(home, "calls"), os.path.join(home, "verdicts")
|
||||
open(verdicts, "w").write("fail continue finished 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:
|
||||
for _ in range(3):
|
||||
self.assertTrue(loop.run_round("task", "premises", "A name", SESSION_ID))
|
||||
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"])
|
||||
self.assertEqual(starts[1][2], SESSION_ID)
|
||||
self.assertEqual(sleep.call_count, 1)
|
||||
self.assertEqual(open(verdicts).read(), "")
|
||||
self.assertEqual(len(os.listdir(loop.ARCHIVE)), 1)
|
||||
|
||||
|
||||
class Reload(unittest.TestCase):
|
||||
def test_runs_the_edited_file_before_trusting_it(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
script = os.path.join(root, "loop-claude")
|
||||
open(script, "w").write(loop.SOURCE.replace("import glob\n", "import glob\nimport no_such_module\n"))
|
||||
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
||||
loop.reload_if_edited(round_number=1, failures=0)
|
||||
execv.assert_not_called()
|
||||
open(script, "w").write(loop.SOURCE.replace("ROUND = ", "ROUND = \"x\" or "))
|
||||
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
||||
loop.reload_if_edited(round_number=1, failures=2)
|
||||
execv.assert_called_once()
|
||||
self.assertEqual((os.environ.pop(loop.ROUND), os.environ.pop(loop.FAILURES)), ("1", "2"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
Nine-seat comprehension panel, 2026-09-24, depth 1: Navigation 7.6, Locality 5.0, Shape 6.2, Self-sufficiency 5.3, overall 5.9. Every seat was capped by Locality or Self-sufficiency, and seven of nine named `bin/loop-claude`'s round loop as the unit they would least want to modify. The items below are ordered by how many seats named the place and how much of the cap it carries; each is one chunk.
|
||||
|
||||
- Fix in `bin/loop-claude`: a judge failure re-judges after a minute instead of re-running the worker with "stopped before it was finished"; the failure counter survives a reload; `reload_if_edited` runs the new file with `--help` before exec so an import-time error keeps the old one; every directory `archive` creates is 0700, not only the leaf; `human_turns` states the list case positively; `project_path` and `session_name` share one branch; `sections` stops at any heading. Add a test for `run_round`'s resume decision against a stub `claude`.
|
||||
- Replace `/home/lilleman` in `settings.json` (lines 9, 16, 59) and the three agents' principles pointers with a form that holds on another machine, and read the status line's time zone from the environment instead of `statusline.sh:65`.
|
||||
- Give the four things called "gate" (README gate, architecture gate, green gate, stability reviewer) one word each, and stop "round", "session", "handoff" and "memory" from each meaning two things across `CLAUDE.md`, `bin/loop-claude` and the skills.
|
||||
- Make `README.md`'s first line name every top-level entry (`AGENTS.md`, `statusline.sh`, `principles/`), make `principles/README.md` say who the principles are for instead of "Org-wide", and give the decision title "Merged under the comprehension floor" a subject.
|
||||
|
||||
Reference in New Issue
Block a user