diff --git a/bin/loop-claude b/bin/loop-claude index ca1fa494..e088fbf9 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -26,7 +26,8 @@ boundary, once the new file answers --help. Claude Code internals this rests on, observed on 2.1.282: - The transcript is ~/.claude/projects/*/.jsonl, one JSON object per line. A / folder and .superseded-*/ and .orphaned-*.jsonl siblings belong to the same session. -- Claude Code writes "user" entries of its own into the transcript; typed() tells a human's from them. +- Claude Code writes "user" entries of its own into the transcript, and a `claude -p` prompt lands as one + with promptSource "sdk"; typed() tells a human's from them. - An "assistant" entry, in the transcript and in --output-format stream-json, carries message.content parts of type "text" (text) and "tool_use" (name, input); stream-json ends with a "result" event. - `claude -p --output-format json --json-schema` returns the verdict under "structured_output". @@ -59,8 +60,8 @@ JUDGE_RULES = ( "rule the rejected one out - and the choice materially changes the deliverable\n" "ask=false otherwise. A sensible preference is not a derivation. Bookkeeping the agent settled " "silently - what to document, wording, where a note goes - is not on its own a reason to " - "interrupt. The human may have replied inside the transcript: judge only what is still open, " - "never a question they already answered there.\n" + "interrupt. The human may have replied inside the transcript, on a [human] line: judge only what " + "is still open, never a question they already answered there.\n" "The question must be one the agent itself raised. Never construct a question from facts the agent " "stated. When ask=true, quote the agent's own wording where it already framed the question.\n" "finished=true when the chunk is complete: merged, or nothing remains that the agent could do on " @@ -171,12 +172,15 @@ def render(entries): for entry in entries: if entry.get("type") == "assistant": lines += [line for part in entry.get("message", {}).get("content", []) if (line := said(part))] + elif typed(entry): + content = entry["message"]["content"] + lines.append("[human] " + (content if isinstance(content, str) else "".join(part.get("text", "") for part in content))) return "\n\n".join(lines) def typed(entry): - """A human turn: a "user" entry, not isMeta, whose message.content is a string not starting with "<" ( and its kind) or one or more text parts, none starting with "[Request interrupted".""" - if entry.get("type") != "user" or entry.get("isMeta"): + """A human turn: a "user" entry, not isMeta, not promptSource "sdk" (the loop's own prompts), whose message.content is a string not starting with "<" ( and its kind) or one or more text parts, none starting with "[Request interrupted".""" + if entry.get("type") != "user" or entry.get("isMeta") or entry.get("promptSource") == "sdk": return False content = entry.get("message", {}).get("content") if isinstance(content, str): @@ -286,7 +290,7 @@ def judge_prompt(task, premises, transcript): f"From README.md:\n{premises}\n\n" f"The task an agent was given:\n{task}\n\n" # The tail, where an ask or a finish lands; the session's transcript grows cycle over cycle. - f"What the agent then did and said, latest last:\n{transcript[-40000:]}\n\n" + f"What the agent did and said, and what the human typed on [human] lines, latest last:\n{transcript[-40000:]}\n\n" f"{JUDGE_RULES}" ) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index c8276ae3..2224c7f7 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -66,13 +66,17 @@ class ReadPremises(unittest.TestCase): class Render(unittest.TestCase): - def test_joins_assistant_text_and_tool_calls_and_skips_the_rest(self): + def test_joins_assistant_text_and_tool_calls_and_human_turns_and_skips_the_rest(self): said = loop.render([ human("do it"), assistant({"type": "text", "text": "Starting."}, {"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}), assistant({"type": "text", "text": " "}), + human([{"type": "tool_result", "content": "not typed"}]), + human("/exit"), + human([{"type": "text", "text": "yes, "}, {"type": "text", "text": "merge it"}]), + {**human("Continue the task; the previous run stopped before it was finished."), "promptSource": "sdk"}, ]) - self.assertEqual(said, 'Starting.\n\n[tool Bash] {"command": "ls"}') + self.assertEqual(said, '[human] do it\n\nStarting.\n\n[tool Bash] {"command": "ls"}\n\n[human] yes, merge it') class HumanTurns(unittest.TestCase): @@ -83,6 +87,7 @@ class HumanTurns(unittest.TestCase): human([{"type": "tool_result", "content": "not typed"}]), human("/model"), {**human("peer message"), "isMeta": True}, + {**human("the loop's own prompt"), "promptSource": "sdk"}, human([{"type": "text", "text": "[Request interrupted by user]"}]), human([]), assistant({"type": "text", "text": "said"}),