Files
claude-config/bin/loop_claude/cycle/states.py
T

86 lines
2.5 KiB
Python

import dataclasses
from loop_claude.cycle.verdicts import AnsweredFromRecords, Finished, Interrupt, Unfinished, Verdict
# A backstop, so a worker the records' answers do not satisfy cannot loop unattended; the judge sends a repeated question to the human.
RECORDS_ANSWERS_PER_CYCLE = 3
@dataclasses.dataclass(frozen=True)
class Working:
"""The worker runs, resumed with the records' answer when there is one."""
answer: str | None = None
records_answers: int = 0
@dataclasses.dataclass(frozen=True)
class Judging:
"""The judge rules; records_answers counts the questions the records answered this cycle."""
transcript_path: str
records_answers: int = 0
@dataclasses.dataclass(frozen=True)
class Interrupting:
transcript_path: str
question: str
records_answers: int = 0
@dataclasses.dataclass(frozen=True)
class Closing:
transcript_path: str
verdict: Finished | Unfinished
@dataclasses.dataclass(frozen=True)
class Ended:
"""The cycle is over."""
Active = Working | Judging | Interrupting | Closing
State = Active | Ended
@dataclasses.dataclass(frozen=True)
class WorkerRan:
transcript_path: str
@dataclasses.dataclass(frozen=True)
class Ruled:
verdict: Verdict
@dataclasses.dataclass(frozen=True)
class HumanAnswered:
pass
@dataclasses.dataclass(frozen=True)
class Archived:
pass
Event = WorkerRan | Ruled | HumanAnswered | Archived
def transition(state: State, event: Event) -> State:
match state, event:
case Working(_, records_answers), WorkerRan(transcript_path):
return Judging(transcript_path, records_answers)
case Judging(_, records_answers), Ruled(AnsweredFromRecords(answer=answer)) if records_answers < RECORDS_ANSWERS_PER_CYCLE:
return Working(answer, records_answers + 1)
case Judging(transcript_path, records_answers), Ruled(AnsweredFromRecords(question=question, answer=answer)):
return Interrupting(transcript_path, f"{question}\n\nREADME.md or the project's records say:\n{answer}", records_answers)
case Judging(transcript_path, records_answers), Ruled(Interrupt(question=question)):
return Interrupting(transcript_path, question, records_answers)
case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict):
return Closing(transcript_path, verdict)
# The human works in the interrupt's claude session, so the judge rules again on the transcript it leaves.
case Interrupting(transcript_path, _, records_answers), HumanAnswered():
return Judging(transcript_path, records_answers)
case Closing(), Archived():
return Ended()
raise ValueError(f"no transition from {state} on {event}")