48 lines
3.6 KiB
Python
48 lines
3.6 KiB
Python
"""How a cycle closes: the loop run the next cycle starts from, the step-up an unfinished verdict makes archived, and the stop once a chunk has had UNFINISHED_LIMIT unfinished verdicts running."""
|
|
import dataclasses
|
|
import pathlib
|
|
|
|
from loop_claude.cycle.run import archived_step, archived_stop, Concluded
|
|
from loop_claude.verdicts import Finished
|
|
from loop_claude.loop.this_loop import Loop
|
|
from loop_claude.loop.reload import LoopRun
|
|
from loop_claude.stop import needs_human, Stop
|
|
from loop_claude.routing import next_route, step_up
|
|
from model_routing.ladder import Rung
|
|
from claude_code.session import transcript_file
|
|
|
|
# How many unfinished verdicts running end the loop, so a worker and a judge that disagree about a chunk cannot cycle unattended without end. A policy number, not derived from the routing steps: well above the two unfinished verdicts running that archived chunks have reached at most, and low enough that a stuck pair is stopped after six cycles. It counts every unfinished verdict, including those of cycles where the human answered an interrupt: a rerun is cheap, and a chunk that needs that many answers is worth a look. The human confirmed both the number and what it counts.
|
|
UNFINISHED_LIMIT = 6
|
|
|
|
|
|
def closed_cycle(loop_run: LoopRun, project: Rung | None, concluded: Concluded, archive_folder: pathlib.Path, loop: Loop) -> LoopRun | Stop:
|
|
"""The loop run after a cycle concluded, with the step-up archived; the stop that needs the human, archived, once the chunk has had UNFINISHED_LIMIT unfinished verdicts running."""
|
|
after = after_verdict(loop_run, project, concluded)
|
|
# A finished chunk drops its route, so it has no step-up, and its run of unfinished verdicts restarts.
|
|
if after.route is None:
|
|
return after
|
|
# The unfinished verdict, archived as the cycle closed, names the route its cycle ended on; the step-up names the one the next cycle runs on.
|
|
path = transcript_file(loop.session)
|
|
if path and (failure := archived_step(archive_folder, path, after.task, step_up(concluded.route, after.route), loop.effects)):
|
|
return Stop(1, failure.reason)
|
|
if limit := reached_limit(after):
|
|
# Archived with the route the next cycle would run on, so a rerun resumes there.
|
|
return archived_stop(archive_folder, loop.session, after.task, after.route, limit, limit.reason, loop.effects)
|
|
return after
|
|
|
|
|
|
def reached_limit(loop_run: LoopRun) -> Stop | None:
|
|
"""The stop that needs the human once the loop run holds UNFINISHED_LIMIT unfinished verdicts running."""
|
|
if loop_run.unfinished_run < UNFINISHED_LIMIT:
|
|
return None
|
|
return needs_human("go on with the chunk", f"the judge ruled it unfinished {loop_run.unfinished_run} cycles running; finish the chunk yourself, or rerun loop-claude to give it {UNFINISHED_LIMIT} more cycles")
|
|
|
|
|
|
def after_verdict(loop_run: LoopRun, project: Rung | None, concluded: Concluded) -> LoopRun:
|
|
"""The loop run the next cycle starts from. A finished chunk drops its route, its run of unfinished verdicts and the items it took in. An unfinished one takes the route routing.next_route makes of the route its cycle ended on, which an interrupt's or a quoted answer's escalation may have raised; the evidence the judge gave for the verdict and the items it found taken in; and counts the verdict."""
|
|
verdict = concluded.verdict
|
|
if isinstance(verdict, Finished):
|
|
return dataclasses.replace(loop_run, route=None, unfinished_evidence="", unfinished_run=0, items_taken_in=())
|
|
route = next_route(project, concluded.route, verdict)
|
|
return dataclasses.replace(loop_run, route=route, unfinished_evidence=verdict.evidence, unfinished_run=loop_run.unfinished_run + 1, items_taken_in=verdict.taken_in.items)
|