Answer the architect's first round
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import dataclasses
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.session import Session, delete_session, transcript_file
|
||||
from loop_claude.claude_code.transcript import read, render
|
||||
@@ -6,7 +7,7 @@ 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 Answered, Archived, Event, Finishing, Fresh, Interrupting, Judging, Resuming, Ruled, State, WorkerRan, opening, transition
|
||||
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
|
||||
@@ -27,43 +28,59 @@ class Cycle:
|
||||
|
||||
|
||||
def run_cycle(cycle) -> Stop | None:
|
||||
state: State = opening(transcript_file(cycle.session))
|
||||
while True:
|
||||
state: State = Working()
|
||||
while not isinstance(state, Ended):
|
||||
event = step(cycle, state)
|
||||
if isinstance(event, Stop):
|
||||
return event
|
||||
state = transition(state, event)
|
||||
if isinstance(state, Fresh | Resuming):
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def step(cycle, state) -> Event | Stop:
|
||||
"""Carries out state's effects and reports what happened."""
|
||||
def step(cycle, state: Active) -> Event | Stop:
|
||||
match state:
|
||||
case Fresh() | Resuming():
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, resuming(cycle.session)), cycle.effects), cycle.effects):
|
||||
return stop
|
||||
path = transcript_file(cycle.session)
|
||||
return WorkerRan(path) if path else Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
||||
case Judging(path):
|
||||
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, cycle.effects)
|
||||
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
|
||||
case Interrupting(path, question):
|
||||
# The interrupt appends to this transcript, so it is archived at each question and judged anew after.
|
||||
if failure := archive(cycle.project, 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, path, question, cycle.paths, cycle.effects) or Answered()
|
||||
case Finishing(path):
|
||||
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 Archived()
|
||||
raise ValueError(f"no step for {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 resuming(session) -> bool:
|
||||
def work(cycle) -> WorkerRan | Stop:
|
||||
# Asked at each attempt: a failed one may have opened the session.
|
||||
return isinstance(opening(transcript_file(session)), Resuming)
|
||||
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 else Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
||||
|
||||
|
||||
def rule(cycle, transcript_path) -> 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, transcript_path, question) -> Answered | Stop:
|
||||
# The interrupt appends to this transcript, so it is archived at each question and judged anew after.
|
||||
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, transcript_path) -> Archived | Stop:
|
||||
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
|
||||
return Stop(1, failure.reason)
|
||||
delete_session(cycle.session)
|
||||
return Archived()
|
||||
|
||||
|
||||
def session_open(session) -> bool:
|
||||
"""A run resumes the session only while its transcript exists."""
|
||||
return bool(transcript_file(session))
|
||||
|
||||
Reference in New Issue
Block a user