Close stdin on the worker, pipe the judge its prompt, ground empty evidence, and skip meta turns in the handoff count
This commit is contained in:
+4
-2
@@ -23,5 +23,7 @@
|
|||||||
!principles/
|
!principles/
|
||||||
!principles/*.md
|
!principles/*.md
|
||||||
!skills/
|
!skills/
|
||||||
!skills/*/
|
!skills/comprehension-panel/
|
||||||
!skills/*/**
|
!skills/comprehension-panel/**
|
||||||
|
!skills/larv-review/
|
||||||
|
!skills/larv-review/**
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ def wrappers(found):
|
|||||||
|
|
||||||
def wrapper_row(found, pid):
|
def wrapper_row(found, pid):
|
||||||
children = {child for child, (argv, ppid, _) in found.items() if ppid == pid and is_claude(argv)}
|
children = {child for child, (argv, ppid, _) in found.items() if ppid == pid and is_claude(argv)}
|
||||||
status = "Inter" if any("--resume" in found[child][0] for child in children) else "Auto"
|
status = "Inter" if any("-p" not in found[child][0] for child in children) else "Auto"
|
||||||
return (status, shown(found[pid][2])), children
|
return (status, shown(found[pid][2])), children
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+30
-23
@@ -113,17 +113,24 @@ def session_file(session_id):
|
|||||||
|
|
||||||
|
|
||||||
def end_session(path):
|
def end_session(path):
|
||||||
os.remove(path)
|
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
|
||||||
shutil.rmtree(path.removesuffix(".jsonl"), ignore_errors=True)
|
try:
|
||||||
|
shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling)
|
||||||
|
except OSError as failure:
|
||||||
|
print(f"could not remove {sibling}: {failure}", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
def parse(raw):
|
||||||
|
try:
|
||||||
|
entry = json.loads(raw)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
return entry if isinstance(entry, dict) else None
|
||||||
|
|
||||||
|
|
||||||
def entries(path):
|
def entries(path):
|
||||||
for raw in open(path, encoding="utf-8"):
|
for raw in open(path, encoding="utf-8"):
|
||||||
try:
|
if entry := parse(raw):
|
||||||
entry = json.loads(raw)
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
if isinstance(entry, dict):
|
|
||||||
yield entry
|
yield entry
|
||||||
|
|
||||||
|
|
||||||
@@ -147,10 +154,10 @@ def render(entries):
|
|||||||
def human_turns(entries):
|
def human_turns(entries):
|
||||||
spoken = 0
|
spoken = 0
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry.get("type") != "user":
|
if entry.get("type") != "user" or entry.get("isMeta"):
|
||||||
continue
|
continue
|
||||||
content = entry.get("message", {}).get("content")
|
content = entry.get("message", {}).get("content")
|
||||||
if isinstance(content, str):
|
if isinstance(content, str) and not content.startswith("<"):
|
||||||
spoken += 1
|
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(part.get("type") == "tool_result" for part in content if isinstance(part, dict)):
|
||||||
spoken += 1
|
spoken += 1
|
||||||
@@ -219,17 +226,14 @@ def worker_args(task, name, session_id, resuming):
|
|||||||
|
|
||||||
|
|
||||||
def run_worker(args):
|
def run_worker(args):
|
||||||
proc = subprocess.Popen(["claude", *args], stdout=subprocess.PIPE, text=True)
|
proc = subprocess.Popen(["claude", *args], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
|
||||||
done = {}
|
done = {}
|
||||||
for raw in proc.stdout:
|
for raw in proc.stdout:
|
||||||
try:
|
event = parse(raw) or {}
|
||||||
event = json.loads(raw)
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
if event.get("type") == "result":
|
if event.get("type") == "result":
|
||||||
done = event
|
done = event
|
||||||
elif event.get("type") == "assistant":
|
elif event.get("type") == "assistant":
|
||||||
for part in event["message"]["content"]:
|
for part in event.get("message", {}).get("content", []):
|
||||||
if line := said(part, 160):
|
if line := said(part, 160):
|
||||||
print(line, flush=True)
|
print(line, flush=True)
|
||||||
return done if proc.wait() == 0 else {}
|
return done if proc.wait() == 0 else {}
|
||||||
@@ -245,12 +249,12 @@ def judge_prompt(task, premises, transcript):
|
|||||||
|
|
||||||
|
|
||||||
def grounded(verdict, transcript):
|
def grounded(verdict, transcript):
|
||||||
return not verdict["ask"] or verdict["evidence"] in transcript
|
return not verdict["ask"] or bool(verdict["evidence"]) and verdict["evidence"] in transcript
|
||||||
|
|
||||||
|
|
||||||
def judge(task, premises, transcript):
|
def judge(task, premises, transcript):
|
||||||
args = [
|
args = [
|
||||||
"-p", judge_prompt(task, premises, transcript),
|
"-p",
|
||||||
"--json-schema", json.dumps(JUDGE_SCHEMA),
|
"--json-schema", json.dumps(JUDGE_SCHEMA),
|
||||||
"--model", JUDGE_MODEL,
|
"--model", JUDGE_MODEL,
|
||||||
"--no-session-persistence",
|
"--no-session-persistence",
|
||||||
@@ -259,11 +263,11 @@ def judge(task, premises, transcript):
|
|||||||
"--tools", "",
|
"--tools", "",
|
||||||
]
|
]
|
||||||
for attempt in range(2):
|
for attempt in range(2):
|
||||||
proc = subprocess.run(["claude", *args], capture_output=True, text=True)
|
proc = subprocess.run(["claude", *args], capture_output=True, input=judge_prompt(task, premises, transcript), text=True)
|
||||||
if proc.returncode != 0:
|
verdict = (parse(proc.stdout) or {}).get("structured_output") if proc.returncode == 0 else None
|
||||||
print(proc.stderr.strip() or "the judge failed", file=sys.stderr)
|
if not isinstance(verdict, dict) or "ask" not in verdict:
|
||||||
|
print(proc.stderr.strip() or "the judge returned no verdict", file=sys.stderr)
|
||||||
return {}
|
return {}
|
||||||
verdict = json.loads(proc.stdout)["structured_output"]
|
|
||||||
if grounded(verdict, transcript):
|
if grounded(verdict, transcript):
|
||||||
return verdict
|
return verdict
|
||||||
print("the judge quoted nothing in the transcript twice - interrupting anyway", file=sys.stderr)
|
print("the judge quoted nothing in the transcript twice - interrupting anyway", file=sys.stderr)
|
||||||
@@ -272,7 +276,7 @@ def judge(task, premises, transcript):
|
|||||||
|
|
||||||
def handoff(session_id, path, question):
|
def handoff(session_id, path, question):
|
||||||
print(f"\n{question}", file=sys.stderr)
|
print(f"\n{question}", file=sys.stderr)
|
||||||
if not sys.stdin.isatty():
|
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
||||||
return False
|
return False
|
||||||
trust(checkouts())
|
trust(checkouts())
|
||||||
before = human_turns(entries(path))
|
before = human_turns(entries(path))
|
||||||
@@ -338,4 +342,7 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit(130)
|
||||||
|
|||||||
+20
-5
@@ -6,6 +6,9 @@ import pathlib
|
|||||||
import stat
|
import stat
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
SESSION_ID = str(getattr(uuid, "uuid7", uuid.uuid4)())
|
||||||
|
|
||||||
LOADER = importlib.machinery.SourceFileLoader("loop_claude", str(pathlib.Path(__file__).with_name("loop-claude")))
|
LOADER = importlib.machinery.SourceFileLoader("loop_claude", str(pathlib.Path(__file__).with_name("loop-claude")))
|
||||||
SPEC = importlib.util.spec_from_loader("loop_claude", LOADER)
|
SPEC = importlib.util.spec_from_loader("loop_claude", LOADER)
|
||||||
@@ -43,20 +46,22 @@ class HumanTurns(unittest.TestCase):
|
|||||||
human("typed"),
|
human("typed"),
|
||||||
human([{"type": "text", "text": "typed as a block"}]),
|
human([{"type": "text", "text": "typed as a block"}]),
|
||||||
human([{"type": "tool_result", "content": "not typed"}]),
|
human([{"type": "tool_result", "content": "not typed"}]),
|
||||||
|
human("<command-name>/model</command-name>"),
|
||||||
|
{**human("peer message"), "isMeta": True},
|
||||||
assistant({"type": "text", "text": "said"}),
|
assistant({"type": "text", "text": "said"}),
|
||||||
]), 2)
|
]), 2)
|
||||||
|
|
||||||
|
|
||||||
class WorkerArgs(unittest.TestCase):
|
class WorkerArgs(unittest.TestCase):
|
||||||
def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self):
|
def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self):
|
||||||
args = loop.worker_args("the task", "A name", "slot-id", resuming=False)
|
args = loop.worker_args("the task", "A name", SESSION_ID, resuming=False)
|
||||||
self.assertIn("--session-id", args)
|
self.assertIn("--session-id", args)
|
||||||
self.assertEqual(args[args.index("--name") + 1], "A name")
|
self.assertEqual(args[args.index("--name") + 1], "A name")
|
||||||
self.assertTrue(args[1].startswith("the task\n\nContinue any open draft PR"))
|
self.assertTrue(args[1].startswith("the task\n\nContinue any open draft PR"))
|
||||||
|
|
||||||
def test_a_resumed_round_reuses_the_session(self):
|
def test_a_resumed_round_reuses_the_session(self):
|
||||||
args = loop.worker_args("paint the fence", "A name", "slot-id", resuming=True)
|
args = loop.worker_args("paint the fence", "A name", SESSION_ID, resuming=True)
|
||||||
self.assertEqual(args[args.index("--resume") + 1], "slot-id")
|
self.assertEqual(args[args.index("--resume") + 1], SESSION_ID)
|
||||||
self.assertNotIn("--name", args)
|
self.assertNotIn("--name", args)
|
||||||
self.assertNotIn("paint the fence", args[1])
|
self.assertNotIn("paint the fence", args[1])
|
||||||
|
|
||||||
@@ -93,18 +98,28 @@ class Said(unittest.TestCase):
|
|||||||
class EndSession(unittest.TestCase):
|
class EndSession(unittest.TestCase):
|
||||||
def test_removes_the_transcript_and_its_sibling_folder(self):
|
def test_removes_the_transcript_and_its_sibling_folder(self):
|
||||||
with tempfile.TemporaryDirectory() as root:
|
with tempfile.TemporaryDirectory() as root:
|
||||||
transcript = os.path.join(root, "abc.jsonl")
|
transcript = os.path.join(root, f"{SESSION_ID}.jsonl")
|
||||||
open(transcript, "w").write("{}\n")
|
open(transcript, "w").write("{}\n")
|
||||||
os.makedirs(os.path.join(root, "abc", "subagents"))
|
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")
|
||||||
loop.end_session(transcript)
|
loop.end_session(transcript)
|
||||||
self.assertEqual(os.listdir(root), [])
|
self.assertEqual(os.listdir(root), [])
|
||||||
|
|
||||||
|
|
||||||
|
class Parse(unittest.TestCase):
|
||||||
|
def test_yields_only_json_objects(self):
|
||||||
|
self.assertEqual(loop.parse('{"type": "user"}'), {"type": "user"})
|
||||||
|
self.assertIsNone(loop.parse("not json"))
|
||||||
|
self.assertIsNone(loop.parse("[1, 2]"))
|
||||||
|
|
||||||
|
|
||||||
class Grounded(unittest.TestCase):
|
class Grounded(unittest.TestCase):
|
||||||
def test_an_interrupt_must_quote_the_transcript(self):
|
def test_an_interrupt_must_quote_the_transcript(self):
|
||||||
self.assertTrue(loop.grounded({"ask": False, "evidence": ""}, "anything"))
|
self.assertTrue(loop.grounded({"ask": False, "evidence": ""}, "anything"))
|
||||||
self.assertTrue(loop.grounded({"ask": True, "evidence": "Needs you: x"}, "so.\n\nNeeds you: x\n"))
|
self.assertTrue(loop.grounded({"ask": True, "evidence": "Needs you: x"}, "so.\n\nNeeds you: x\n"))
|
||||||
self.assertFalse(loop.grounded({"ask": True, "evidence": "invented"}, "the transcript"))
|
self.assertFalse(loop.grounded({"ask": True, "evidence": "invented"}, "the transcript"))
|
||||||
|
self.assertFalse(loop.grounded({"ask": True, "evidence": ""}, "the transcript"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user