Require every verdict field, treat an interrupted handoff as unanswered, and tighten the --help contract
This commit is contained in:
+14
-10
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run a task as headless claude rounds; a judge stops the loop only when a decision is the human's.
|
||||
"""Run a task as headless claude rounds; a judge interrupts only when a decision is the human's.
|
||||
|
||||
usage: loop-claude <task>
|
||||
|
||||
@@ -8,14 +8,15 @@ the task; a judge then reads the transcript against the README. A question only
|
||||
opens `claude --resume` on the terminal. An unfinished chunk resumes the same session next round; a
|
||||
finished one starts a fresh session. Rounds continue until Ctrl-C.
|
||||
|
||||
Exit 1: the README gate blocks, 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.
|
||||
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.
|
||||
|
||||
Each project has one session, "A ~/code/<path> auto-mode", at an id derived from that name; it is
|
||||
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 models. A change to this
|
||||
file takes effect at the next round boundary.
|
||||
and when the chunk finishes. JUDGE_MODEL and WORKER_MODEL override the models (default
|
||||
claude-opus-5-5). A change to this file takes effect at the next round boundary.
|
||||
"""
|
||||
import glob
|
||||
import json
|
||||
@@ -159,7 +160,7 @@ def human_turns(entries):
|
||||
content = entry.get("message", {}).get("content")
|
||||
if isinstance(content, str) and not content.startswith("<"):
|
||||
spoken += 1
|
||||
elif isinstance(content, list) and not any(part.get("type") == "tool_result" for part in content if isinstance(part, dict)):
|
||||
elif isinstance(content, list) and not any(said(part) == "" or said(part).startswith("[Request interrupted") for part in content):
|
||||
spoken += 1
|
||||
return spoken
|
||||
|
||||
@@ -188,7 +189,6 @@ def checkouts():
|
||||
|
||||
|
||||
def trust(paths):
|
||||
# No CLI flag skips the trust dialog, and the interactive resume would stall on it in every worktree the round created.
|
||||
config = os.path.expanduser("~/.claude.json")
|
||||
try:
|
||||
data = json.load(open(config, encoding="utf-8"))
|
||||
@@ -221,7 +221,7 @@ def worker_args(task, name, session_id, resuming):
|
||||
"--output-format", "stream-json",
|
||||
"--permission-mode", "bypassPermissions",
|
||||
"--verbose",
|
||||
*(["--model", WORKER_MODEL] if WORKER_MODEL else []),
|
||||
"--model", WORKER_MODEL,
|
||||
]
|
||||
|
||||
|
||||
@@ -248,6 +248,10 @@ def judge_prompt(task, premises, transcript):
|
||||
)
|
||||
|
||||
|
||||
def complete(verdict):
|
||||
return isinstance(verdict, dict) and set(JUDGE_SCHEMA["required"]) <= verdict.keys()
|
||||
|
||||
|
||||
def grounded(verdict, transcript):
|
||||
return not verdict["ask"] or bool(verdict["evidence"]) and verdict["evidence"] in transcript
|
||||
|
||||
@@ -265,7 +269,7 @@ def judge(task, premises, transcript):
|
||||
for attempt in range(2):
|
||||
proc = subprocess.run(["claude", *args], capture_output=True, input=judge_prompt(task, premises, transcript), text=True)
|
||||
verdict = (parse(proc.stdout) or {}).get("structured_output") if proc.returncode == 0 else None
|
||||
if not isinstance(verdict, dict) or "ask" not in verdict:
|
||||
if not complete(verdict):
|
||||
print(proc.stderr.strip() or "the judge returned no verdict", file=sys.stderr)
|
||||
return {}
|
||||
if grounded(verdict, transcript):
|
||||
|
||||
@@ -48,6 +48,7 @@ class HumanTurns(unittest.TestCase):
|
||||
human([{"type": "tool_result", "content": "not typed"}]),
|
||||
human("<command-name>/model</command-name>"),
|
||||
{**human("peer message"), "isMeta": True},
|
||||
human([{"type": "text", "text": "[Request interrupted by user]"}]),
|
||||
assistant({"type": "text", "text": "said"}),
|
||||
]), 2)
|
||||
|
||||
@@ -122,5 +123,12 @@ class Grounded(unittest.TestCase):
|
||||
self.assertFalse(loop.grounded({"ask": True, "evidence": ""}, "the transcript"))
|
||||
|
||||
|
||||
class Verdict(unittest.TestCase):
|
||||
def test_needs_every_field_the_schema_requires(self):
|
||||
self.assertTrue(loop.complete({"ask": False, "evidence": "", "finished": True, "question": ""}))
|
||||
self.assertFalse(loop.complete({"ask": False}))
|
||||
self.assertFalse(loop.complete(None))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user