diff --git a/.gitignore b/.gitignore index 63f77ba8..9bfb5bcf 100644 --- a/.gitignore +++ b/.gitignore @@ -23,5 +23,7 @@ !principles/ !principles/*.md !skills/ -!skills/*/ -!skills/*/** +!skills/comprehension-panel/ +!skills/comprehension-panel/** +!skills/larv-review/ +!skills/larv-review/** diff --git a/bin/claude-instances b/bin/claude-instances index 6dcb13d3..69c009b8 100755 --- a/bin/claude-instances +++ b/bin/claude-instances @@ -48,7 +48,7 @@ def wrappers(found): def wrapper_row(found, pid): 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 diff --git a/bin/loop-claude b/bin/loop-claude index 87799627..02e1db83 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -113,17 +113,24 @@ def session_file(session_id): def end_session(path): - os.remove(path) - shutil.rmtree(path.removesuffix(".jsonl"), ignore_errors=True) + for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"): + 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): for raw in open(path, encoding="utf-8"): - try: - entry = json.loads(raw) - except ValueError: - continue - if isinstance(entry, dict): + if entry := parse(raw): yield entry @@ -147,10 +154,10 @@ def render(entries): def human_turns(entries): spoken = 0 for entry in entries: - if entry.get("type") != "user": + if entry.get("type") != "user" or entry.get("isMeta"): continue content = entry.get("message", {}).get("content") - if isinstance(content, str): + 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)): spoken += 1 @@ -219,17 +226,14 @@ def worker_args(task, name, session_id, resuming): 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 = {} for raw in proc.stdout: - try: - event = json.loads(raw) - except ValueError: - continue + event = parse(raw) or {} if event.get("type") == "result": done = event elif event.get("type") == "assistant": - for part in event["message"]["content"]: + for part in event.get("message", {}).get("content", []): if line := said(part, 160): print(line, flush=True) return done if proc.wait() == 0 else {} @@ -245,12 +249,12 @@ def judge_prompt(task, premises, 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): args = [ - "-p", judge_prompt(task, premises, transcript), + "-p", "--json-schema", json.dumps(JUDGE_SCHEMA), "--model", JUDGE_MODEL, "--no-session-persistence", @@ -259,11 +263,11 @@ def judge(task, premises, transcript): "--tools", "", ] for attempt in range(2): - proc = subprocess.run(["claude", *args], capture_output=True, text=True) - if proc.returncode != 0: - print(proc.stderr.strip() or "the judge failed", file=sys.stderr) + 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: + print(proc.stderr.strip() or "the judge returned no verdict", file=sys.stderr) return {} - verdict = json.loads(proc.stdout)["structured_output"] if grounded(verdict, transcript): return verdict 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): print(f"\n{question}", file=sys.stderr) - if not sys.stdin.isatty(): + if not (sys.stdin.isatty() and sys.stdout.isatty()): return False trust(checkouts()) before = human_turns(entries(path)) @@ -338,4 +342,7 @@ def main(): if __name__ == "__main__": - main() + try: + main() + except KeyboardInterrupt: + sys.exit(130) diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index ed5afbe0..cc99ef04 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -6,6 +6,9 @@ import pathlib import stat import tempfile 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"))) SPEC = importlib.util.spec_from_loader("loop_claude", LOADER) @@ -43,20 +46,22 @@ class HumanTurns(unittest.TestCase): human("typed"), human([{"type": "text", "text": "typed as a block"}]), human([{"type": "tool_result", "content": "not typed"}]), + human("/model"), + {**human("peer message"), "isMeta": True}, assistant({"type": "text", "text": "said"}), ]), 2) 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", "slot-id", resuming=False) + args = loop.worker_args("the task", "A name", SESSION_ID, resuming=False) self.assertIn("--session-id", args) self.assertEqual(args[args.index("--name") + 1], "A name") self.assertTrue(args[1].startswith("the task\n\nContinue any open draft PR")) def test_a_resumed_round_reuses_the_session(self): - args = loop.worker_args("paint the fence", "A name", "slot-id", resuming=True) - self.assertEqual(args[args.index("--resume") + 1], "slot-id") + args = loop.worker_args("paint the fence", "A name", SESSION_ID, resuming=True) + self.assertEqual(args[args.index("--resume") + 1], SESSION_ID) self.assertNotIn("--name", args) self.assertNotIn("paint the fence", args[1]) @@ -93,18 +98,28 @@ class Said(unittest.TestCase): class EndSession(unittest.TestCase): def test_removes_the_transcript_and_its_sibling_folder(self): 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") - 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) 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): def test_an_interrupt_must_quote_the_transcript(self): 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.assertFalse(loop.grounded({"ask": True, "evidence": "invented"}, "the transcript")) + self.assertFalse(loop.grounded({"ask": True, "evidence": ""}, "the transcript")) if __name__ == "__main__":