From 1eb715354c742663f226a3fd94696a79507d4f1c Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 26 Sep 2026 14:57:42 +0200 Subject: [PATCH] Two blank lines throughout, callers above callees in each group, model with the loop's own --- bin/loop-claude | 76 ++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index 803829ea..4afdffda 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -111,7 +111,6 @@ def main(): time.sleep(60) - def run_cycle(root, task, premises, name, session_id): if failure := run_worker(root, worker_args(task, name, session_id, resuming=session_open(session_id))): return failure @@ -131,7 +130,6 @@ def run_cycle(root, task, premises, name, session_id): return "" - def reload_if_edited(root, task, cycle_number, failures): try: source = open(__file__, encoding="utf-8").read() @@ -148,7 +146,6 @@ def reload_if_edited(root, task, cycle_number, failures): os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, json.dumps(state)]) - def reloaded_state(handed): state = parse(handed[0]) if len(handed) == 1 else None if not state or {"cycle_number", "failures", "root"} - state.keys() or not isinstance(state.get("task"), str) or not state["task"]: @@ -156,7 +153,6 @@ def reloaded_state(handed): return state - def checkout_root(): found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True) if found.returncode != 0: @@ -164,7 +160,6 @@ def checkout_root(): return found.stdout.strip() - def read_premises(root): try: found = sections(open(os.path.join(root, "README.md"), encoding="utf-8").read()) @@ -181,7 +176,6 @@ def read_premises(root): return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}" - def sections(text): found, current, fenced = {}, None, False for line in text.splitlines(): @@ -196,18 +190,11 @@ def sections(text): return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()} - def session_name(root): # "A " is the human's cue in the session picker that the session is the loop's. return f"A {project(root)} loop-claude" - -def project(root): - return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/") - - - def archive(root, path, note): folder = os.path.join(ARCHIVE, project(root)) try: @@ -222,7 +209,6 @@ def archive(root, path, note): print(f"could not archive the transcript: {failure}", file=sys.stderr) - def make_private(folder): mask = os.umask(0o077) try: @@ -231,19 +217,25 @@ def make_private(folder): os.umask(mask) +def model(variable): + return ["--model", os.environ[variable]] if os.environ.get(variable) else [] + + +def project(root): + return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/") + + # The transcript file -def session_file(session_id): - hit = glob.glob(os.path.expanduser(f"~/.claude/projects/*/{session_id}.jsonl")) - return hit[0] if hit else "" - - - def session_open(session_id): return bool(session_file(session_id)) +def session_file(session_id): + hit = glob.glob(os.path.expanduser(f"~/.claude/projects/*/{session_id}.jsonl")) + return hit[0] if hit else "" + def delete_transcript_files(path): for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"): @@ -253,14 +245,12 @@ def delete_transcript_files(path): print(f"could not remove {sibling}: {failure}", file=sys.stderr) - def entries(path): for raw in open(path, encoding="utf-8"): if entry := parse(raw): yield entry - def parse(raw): try: entry = json.loads(raw) @@ -272,6 +262,10 @@ def parse(raw): # "user" entries +def human_turns(entries): + return sum(1 for entry in entries if typed(entry)) + + def typed(entry): """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": @@ -283,11 +277,6 @@ def typed(entry): 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): - return sum(1 for entry in entries if typed(entry)) - - # "assistant" entries and stream-json @@ -310,7 +299,6 @@ def run_worker(root, args): return "" if done else "claude exited 0 without a result event" - def worker_args(task, name, session_id, resuming): if resuming: start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session_id)] @@ -325,22 +313,6 @@ def worker_args(task, name, session_id, resuming): ] - -def model(variable): - return ["--model", os.environ[variable]] if os.environ.get(variable) else [] - - - -def said(part, limit=400): - kind = part.get("type") if isinstance(part, dict) else None - if kind == "text" and part.get("text", "").strip(): - return part["text"] - if kind == "tool_use": - return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}" - return "" - - - def render(entries): lines = [] for entry in entries: @@ -352,6 +324,15 @@ def render(entries): return "\n\n".join(lines) +def said(part, limit=400): + kind = part.get("type") if isinstance(part, dict) else None + if kind == "text" and part.get("text", "").strip(): + return part["text"] + if kind == "tool_use": + return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}" + return "" + + # --json-schema verdicts @@ -381,7 +362,6 @@ def judge(root, task, premises, transcript): time.sleep(60) - def judge_prompt(task, premises, transcript): return ( f"From README.md:\n{premises}\n\n" @@ -392,12 +372,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): if not verdict["ask"]: return True @@ -418,7 +396,6 @@ def interrupt(root, session_id, path, question): return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything" - def checkouts(root): found = [root] listing = subprocess.run(["git", "worktree", "list", "--porcelain"], capture_output=True, cwd=root, text=True) @@ -427,7 +404,6 @@ def checkouts(root): return sorted(set(found)) - def trust(paths): config = os.path.expanduser("~/.claude.json") try: @@ -453,8 +429,6 @@ def trust(paths): print("trusted for the interrupt: " + ", ".join(granted), file=sys.stderr) - - if __name__ == "__main__": try: main()