e55b8826fb
# Conflicts: # bin/agent_cycle/interrupt.py # bin/agent_cycle/run.py # bin/agent_cycle/states.py # docs/reading-cost.md # todo.md
112 lines
4.1 KiB
Python
112 lines
4.1 KiB
Python
import dataclasses
|
|
|
|
from agent_cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished, Verdict
|
|
from model_routing.ladder import Rung
|
|
from agent_cycle.routing import StepUp
|
|
|
|
# A backstop for what the judge misses, such as a reworded repeat, so the worker cannot loop unattended on answers quoted from the premises or the records.
|
|
QUOTED_ANSWERS_PER_CYCLE = 3
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class TypedAnswer:
|
|
"""The human's answer to an interrupt, typed at loop-claude's terminal."""
|
|
question: str
|
|
answer: str
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Working:
|
|
"""The worker runs, resumed with an answer quoted from the premises or the records, or typed by the human, when there is one."""
|
|
answered: QuotedAnswer | TypedAnswer | None = None
|
|
quoted_answers: int = 0
|
|
# Whether the ruling that brought the answer stepped the worker up a rung, so the worker is told of it.
|
|
stepped: bool = False
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Judging:
|
|
"""The judge rules; quoted_answers counts the questions the premises 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, by typing or in the worker's claude session; a QuotedAnswer reaches here only after the cycle has used up its QUOTED_ANSWERS_PER_CYCLE."""
|
|
transcript_path: str
|
|
ruling: Interrupt | QuotedAnswer
|
|
quoted_answers: int = 0
|
|
# The rung the worker ran on, where the ruling stepped it up from there; None where the rung holds. It holds through the answer the human types, and drops on /talk.
|
|
stepped_from: Rung | None = None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Closing:
|
|
transcript_path: str
|
|
verdict: Finished | Unfinished
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Ended:
|
|
verdict: Finished | Unfinished
|
|
|
|
|
|
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
|
|
# The step-up an interrupt's or a quoted answer's escalation made, whose `after` the cycle runs on from here; None where the route holds, and always for a finished or unfinished verdict.
|
|
stepped: StepUp | None = None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class HumanTyped:
|
|
typed: TypedAnswer
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class HumanTalked:
|
|
"""The human typed in the worker's claude session, which /talk opens, or a release ready to cut opens at once."""
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Archived:
|
|
pass
|
|
|
|
|
|
Event = WorkerRan | Ruled | HumanTyped | HumanTalked | Archived
|
|
|
|
|
|
def to_the_human(ruling: Interrupt | QuotedAnswer, quoted_answers: int) -> bool:
|
|
"""Whether a ruling goes to the human: an interrupt always, a quoted answer once the cycle has used up its QUOTED_ANSWERS_PER_CYCLE."""
|
|
return isinstance(ruling, Interrupt) or quoted_answers >= QUOTED_ANSWERS_PER_CYCLE
|
|
|
|
|
|
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(transcript_path, quoted_answers), Ruled(Interrupt() | QuotedAnswer() as ruling, stepped) if to_the_human(ruling, quoted_answers):
|
|
return Interrupting(transcript_path, ruling, quoted_answers, stepped.before.rung if stepped else None)
|
|
case Judging(_, quoted_answers), Ruled(QuotedAnswer() as answered, stepped):
|
|
return Working(answered, quoted_answers + 1, stepped is not None)
|
|
case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict):
|
|
return Closing(transcript_path, verdict)
|
|
case Interrupting(_, _, quoted_answers, stepped_from), HumanTyped(typed):
|
|
return Working(typed, quoted_answers, stepped_from is not None)
|
|
# The human worked in the worker's claude session, so the judge rules again on the transcript it leaves.
|
|
case Interrupting(transcript_path, _, quoted_answers), HumanTalked():
|
|
return Judging(transcript_path, quoted_answers)
|
|
case Closing(_, verdict), Archived():
|
|
return Ended(verdict)
|
|
raise ValueError(f"no transition from {state} on {event}")
|