Name the loop's states with one transition function, and inject its side effects

This commit is contained in:
2026-10-01 23:12:37 +02:00
parent d0ce3b2690
commit f385b563f0
16 changed files with 469 additions and 126 deletions
+10 -8
View File
@@ -1,31 +1,33 @@
import subprocess
import sys
from loop_claude import human_needed
from loop_claude.claude_code.transcript import human_turns, read
from loop_claude.claude_code.trust import trust
from loop_claude.effects import Effects
from loop_claude.outcome import Failure, Stop
from loop_claude.project.checkout import checkouts
TO = "answer the question above"
def interrupt(root, session_id, model, path, question, paths) -> Stop | None:
def interrupt(root, session_id, model, path, question, paths, effects: Effects) -> Stop | None:
print(f"\n{question}", file=sys.stderr)
if stop := human_needed.off_terminal(TO):
if stop := human_needed.off_terminal(TO, effects.on_terminal()):
return stop
trust(paths.claude_json, checkouts(root))
trust(paths.claude_json, checkouts(root, effects))
entries = read(path)
if isinstance(entries, Failure):
return Stop(1, entries.reason)
before = human_turns(entries)
try:
subprocess.run(["claude", "--model", model, "--permission-mode", "bypassPermissions", "--resume", str(session_id)], cwd=root)
effects.run(["claude", "--model", model, "--permission-mode", "bypassPermissions", "--resume", str(session_id)], cwd=root)
except OSError as failure:
return Stop(1, f"could not start claude for the interrupt: {failure.strerror}")
entries = read(path)
if isinstance(entries, Failure):
return Stop(1, f"could not tell whether you typed anything: {entries.reason}")
if human_turns(entries) <= before:
return human_needed.stop(TO, "the interrupt ended with nothing typed")
return None
return None if typed_since(before, entries) else human_needed.stop(TO, "the interrupt ended with nothing typed")
def typed_since(turns_before, entries) -> bool:
return human_turns(entries) > turns_before