83 lines
3.1 KiB
Python
83 lines
3.1 KiB
Python
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, 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, Answered, Archived, Ended, Event, Finishing, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
|
|
from loop_claude.cycle.worker import run_worker, worker_args
|
|
from loop_claude.effects import Effects
|
|
from loop_claude.outcome import Failure, Stop
|
|
from loop_claude.paths import Paths
|
|
from loop_claude.project.models import Models
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Cycle:
|
|
effects: Effects
|
|
models: Models
|
|
paths: Paths
|
|
premises: str
|
|
project: str
|
|
root: str
|
|
session: Session
|
|
task: str
|
|
|
|
|
|
def run_cycle(cycle: Cycle) -> Stop | None:
|
|
state: State = Working()
|
|
while not isinstance(state, Ended):
|
|
event = step(cycle, state)
|
|
if isinstance(event, Stop):
|
|
return event
|
|
state = transition(state, event)
|
|
return None
|
|
|
|
|
|
def step(cycle: Cycle, state: Active) -> Event | Stop:
|
|
match state:
|
|
case Working():
|
|
return work(cycle)
|
|
case Judging(transcript_path):
|
|
return rule(cycle, transcript_path)
|
|
case Interrupting(transcript_path, question):
|
|
return ask(cycle, transcript_path, question)
|
|
case Finishing(transcript_path):
|
|
return close(cycle, transcript_path)
|
|
case _:
|
|
typing.assert_never(state)
|
|
|
|
|
|
def work(cycle: Cycle) -> 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)), 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_path)
|
|
if isinstance(entries, Failure):
|
|
return Stop(1, entries.reason)
|
|
verdict = judge(cycle.root, cycle.task, cycle.premises, 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) -> Answered | Stop:
|
|
# The interrupt appends to this transcript, so it is archived at each question.
|
|
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: 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 Answered()
|
|
|
|
|
|
def close(cycle: Cycle, transcript_path: str) -> Archived | Stop:
|
|
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
|
|
return Stop(1, failure.reason)
|
|
if failure := delete_session(cycle.session):
|
|
return Stop(1, failure.reason)
|
|
return Archived()
|