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

85 lines
2.5 KiB
Python

import dataclasses
from loop_claude.cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
# A backstop for what the judge misses, such as a reworded repeat, so the worker cannot loop unattended on answers quoted from README.md or the records.
QUOTED_ANSWERS_PER_CYCLE = 3
@dataclasses.dataclass(frozen=True)
class Working:
"""The worker runs, resumed with an answer quoted from README.md or the records when there is one."""
answered: QuotedAnswer | None = None
quoted_answers: int = 0
@dataclasses.dataclass(frozen=True)
class Judging:
"""The judge rules; quoted_answers counts the questions README.md or the records answered this cycle, and an interrupt does not reset it."""
transcript_path: str
quoted_answers: int = 0
@dataclasses.dataclass(frozen=True)
class Interrupting:
"""The human answers; a QuotedAnswer here is one past the cycle's QUOTED_ANSWERS_PER_CYCLE."""
transcript_path: str
ruling: Interrupt | QuotedAnswer
quoted_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(_, quoted_answers), WorkerRan(transcript_path):
return Judging(transcript_path, quoted_answers)
case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered) if quoted_answers < QUOTED_ANSWERS_PER_CYCLE:
return Working(answered, quoted_answers + 1)
case Judging(transcript_path, quoted_answers), Ruled(Interrupt() | QuotedAnswer() as ruling):
return Interrupting(transcript_path, ruling, quoted_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, _, quoted_answers), HumanAnswered():
return Judging(transcript_path, quoted_answers)
case Closing(), Archived():
return Ended()
raise ValueError(f"no transition from {state} on {event}")