Archive every cycle's transcript and its session folder, however the cycle ends

This commit is contained in:
2026-10-02 16:00:14 +02:00
parent 9fb811c10a
commit 990963d586
8 changed files with 98 additions and 36 deletions
+23 -11
View File
@@ -8,6 +8,7 @@ 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 Active, Answered, Archived, Ended, Event, Finishing, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
from loop_claude.cycle.verdicts import Finished, Unfinished
from loop_claude.cycle.worker import run_worker, worker_args
from loop_claude.effects import Effects
from loop_claude.failure import Failure
@@ -30,14 +31,24 @@ class Cycle:
def run_cycle(cycle: Cycle) -> Stop | None:
state: State = Working()
while not isinstance(state, Ended):
event = step(cycle, state)
if isinstance(event, Stop):
return event
state = transition(state, event)
try:
while not isinstance(state, Ended):
event = step(cycle, state)
if isinstance(event, Stop):
return stopped(cycle, event)
state = transition(state, event)
except KeyboardInterrupt:
return stopped(cycle, Stop(130, ""))
return None
def stopped(cycle: Cycle, stop: Stop) -> Stop:
path = transcript_file(cycle.session)
failure = archive(cycle.project_key, path, f"Task: {cycle.task}\n\nStopped: {'Ctrl-C' if stop.code == 130 else stop.reason}", cycle.paths) if path else None
# A stop for a failed archive has just failed the same way again.
return Stop(1, f"{failure.reason}\n{stop.reason}".rstrip()) if failure and failure.reason != stop.reason else stop
def step(cycle: Cycle, state: Active) -> Event | Stop:
match state:
case Working():
@@ -46,8 +57,8 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
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 Finishing(transcript_path, verdict):
return close(cycle, transcript_path, verdict)
case _:
typing.assert_never(state)
@@ -69,15 +80,16 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
# The interrupt appends to this transcript, so it is archived at each question.
# Archived before the interrupt appends to the transcript, so the copy carries the question beside it.
if failure := archive(cycle.project_key, 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: Cycle, transcript_path: str) -> Archived | Stop:
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
def close(cycle: Cycle, transcript_path: str, verdict: Finished | Unfinished) -> Archived | Stop:
finished = isinstance(verdict, Finished)
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: {'finished' if finished else 'unfinished'}", cycle.paths):
return Stop(1, failure.reason)
if failure := delete_session(cycle.session):
if finished and (failure := delete_session(cycle.session)):
return Stop(1, failure.reason)
return Archived()