56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
import dataclasses
|
|
|
|
from loop_claude.claude_code.session import Session, delete_session, 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 Verdict, judge
|
|
from loop_claude.cycle.retry import retried
|
|
from loop_claude.cycle.worker import run_worker, worker_args
|
|
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:
|
|
models: Models
|
|
paths: Paths
|
|
premises: str
|
|
project: str
|
|
root: str
|
|
session: Session
|
|
task: str
|
|
|
|
|
|
def run_cycle(cycle) -> Stop | None:
|
|
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, bool(transcript_file(cycle.session))))):
|
|
return stop
|
|
path = transcript_file(cycle.session)
|
|
if not path:
|
|
return Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
|
verdict = judge_through_interrupts(cycle, path)
|
|
if isinstance(verdict, Stop):
|
|
return verdict
|
|
if verdict.finished:
|
|
if failure := archive(cycle.project, path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
|
|
return Stop(1, failure.reason)
|
|
delete_session(cycle.session)
|
|
return None
|
|
|
|
|
|
def judge_through_interrupts(cycle, path) -> Verdict | Stop:
|
|
# The interrupt appends to this transcript, so each pass judges and archives it anew.
|
|
while True:
|
|
entries = read(path)
|
|
if isinstance(entries, Failure):
|
|
return Stop(1, entries.reason)
|
|
verdict = judge(cycle.root, cycle.task, cycle.premises, render(entries), cycle.models.judge)
|
|
if isinstance(verdict, Stop) or not verdict.interrupt:
|
|
return verdict
|
|
if failure := archive(cycle.project, path, f"Task: {cycle.task}\n\nVerdict: interrupt\n\n{verdict.question}", cycle.paths):
|
|
return Stop(1, failure.reason)
|
|
if stop := interrupt(cycle.root, cycle.session.id, cycle.models.worker, path, verdict.question, cycle.paths):
|
|
return stop
|
|
|