57 lines
2.8 KiB
Python
57 lines
2.8 KiB
Python
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"
|
|
# The word and the line were picked on 2026-10-17 by a bench of three readers seated as the README's audience, as ~/.claude/CLAUDE.md → "Working loop" → "Bench a syntax with the people who write it." has it.
|
|
TALK = "/talk"
|
|
ASK_ANSWER = f"Answer ({TALK} opens claude, Enter stops the loop): "
|
|
|
|
|
|
def put_question(question: str, effects: Effects) -> Stop | None:
|
|
"""Prints question for the human; a Stop where loop-claude is off a terminal, so no one can answer it."""
|
|
print(f"\n{question}", file=sys.stderr)
|
|
return needs_terminal(ANSWER_QUESTION, effects.on_terminal())
|
|
|
|
|
|
def read_answer(effects: Effects) -> str | Stop:
|
|
"""The line the human types at loop-claude's terminal, stripped: their answer, or TALK where they ask for claude's session; a Stop where they type nothing or the input ends."""
|
|
try:
|
|
answer = effects.input(ASK_ANSWER).strip()
|
|
except EOFError:
|
|
print(file=sys.stderr)
|
|
return needs_human(ANSWER_QUESTION, "the input ended")
|
|
return answer or needs_human(ANSWER_QUESTION, "nothing was typed")
|
|
|
|
|
|
def talk(root: str, session_id: uuid.UUID, worker: Rung, path: str, paths: Paths, env: dict[str, str], effects: Effects) -> Stop | None:
|
|
"""Resumes the worker's session in claude on the terminal; the human must type something there."""
|
|
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
|