import dataclasses import typing from loop_claude.claude_code.session import Session, delete_session, session_open, transcript_file from loop_claude.claude_code.transcript import read_transcript, render from loop_claude.cycle.archive import archive from loop_claude.cycle.interrupt import interrupt from loop_claude.cycle.judge import judge from loop_claude.cycle.retry import retried from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, HumanAnswered, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition from loop_claude.cycle.verdicts import Finished, Unfinished from loop_claude.cycle.worker import run_worker, worker_args from loop_claude.effects import Effects from loop_claude.failure import Failure from loop_claude.paths import Paths from loop_claude.project.model_choice import Models from loop_claude.stop import Stop @dataclasses.dataclass(frozen=True) class Cycle: effects: Effects models: Models paths: Paths premises: str project_key: str records: str root: str session: Session task: str def run_cycle(cycle: Cycle) -> Stop | None: state: State = Working() try: while not isinstance(state, Ended): event = step(cycle, state) if isinstance(event, Stop): return stopped(cycle, event, event.reason) state = transition(state, event) except KeyboardInterrupt: return stopped(cycle, Stop(130, ""), "Ctrl-C") return None def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop: path = transcript_file(cycle.session) failure = archive(cycle.project_key, path, cycle.task, f"Stopped: {why}", cycle.paths) if path else None # A failed archive exits 1 whatever the stop; when the stop was this archive failing, its reason is said once. return Stop(1, f"{failure.reason}\n{stop.reason}".rstrip()) if failure and failure.reason != stop.reason else stop def step(cycle: Cycle, state: Active) -> Event | Stop: match state: case Working(answer, _): return work(cycle, answer) case Judging(transcript_path, _): return rule(cycle, transcript_path) case Interrupting(transcript_path, question, _): return ask(cycle, transcript_path, question) case Closing(transcript_path, verdict): return close(cycle, transcript_path, verdict) case _: typing.assert_never(state) def work(cycle: Cycle, answer: str | None) -> WorkerRan | Stop: # session_open runs inside the lambda: a failed attempt may have opened the session. if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, session_open(cycle.session), answer), cycle.effects), cycle.effects): return stop path = transcript_file(cycle.session) return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}") def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop: entries = read_transcript(transcript_path) if isinstance(entries, Failure): return Stop(1, entries.reason) verdict = judge(cycle.root, cycle.task, cycle.premises, cycle.records, render(entries), cycle.models.judge, cycle.effects) return verdict if isinstance(verdict, Stop) else Ruled(verdict) def ask(cycle: Cycle, transcript_path: str, question: str) -> HumanAnswered | Stop: # Archived now, so the transcript the question was asked on is kept with it; the human's answer then grows the transcript. if failure := archive(cycle.project_key, transcript_path, cycle.task, f"Verdict: interrupt\n\n{question}", cycle.paths): return Stop(1, failure.reason) return interrupt(cycle.root, cycle.session.id, cycle.models.worker, transcript_path, question, cycle.paths, cycle.effects) or HumanAnswered() def close(cycle: Cycle, transcript_path: str, verdict: Finished | Unfinished) -> Archived | Stop: finished = isinstance(verdict, Finished) if failure := archive(cycle.project_key, transcript_path, cycle.task, f"Verdict: {'finished' if finished else 'unfinished'}", cycle.paths): return Stop(1, failure.reason) if finished and (failure := delete_session(cycle.session)): return Stop(1, failure.reason) return Archived()