import sys import uuid from loop_claude.effects import Effects from failure import Failure from claude_code.transcript import human_turns, read_transcript from loop_claude.stop import needs_human, needs_terminal, Stop from loop_claude.paths import Paths from model_routing.ladder import Rung from claude_code.trust import trust from loop_claude.project.checkout import worktrees ANSWER_QUESTION = "answer the question above" def interrupt(root: str, session_id: uuid.UUID, worker: Rung, path: str, question: str, paths: Paths, env: dict[str, str], effects: Effects) -> Stop | None: print(f"\n{question}", file=sys.stderr) if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()): return stop entries = read_transcript(path) if isinstance(entries, Failure): return Stop(1, entries.reason) before = human_turns(entries) if unstarted := hand_over(worker, ["--resume", str(session_id)], root, paths, env, effects): return Stop(1, f"could not start claude for the interrupt: {unstarted.reason}") entries = read_transcript(path) if isinstance(entries, Failure): return Stop(1, f"could not tell whether you typed anything: {entries.reason}") return None if human_turns(entries) > before else needs_human(ANSWER_QUESTION, "the interrupt ended with nothing typed") def hand_over(rung: Rung, args: list[str], root: str, paths: Paths, env: dict[str, str], effects: Effects) -> Failure | None: """Marks the checkout and each of its worktrees trusted, then runs claude on the terminal in root, on rung, with env added to its environment; the failure is why claude could not start.""" trust(paths.claude_json, worktrees(root, effects)) try: effects.claude_on_terminal([*rung.flags(), "--permission-mode", "bypassPermissions", *args], root, env) except OSError as failure: return Failure(str(failure.strerror)) return None