Archive every cycle's transcript and its session folder, however the cycle ends
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import hashlib
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from loop_claude.failure import Failure
|
||||
@@ -14,11 +15,34 @@ def archive(project_key: str, path: str, note: str, paths: Paths) -> Failure | N
|
||||
make_private(folder)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}")
|
||||
# <session id>/ holds the subagents' transcripts and the tool results too long for the transcript.
|
||||
session_folder = pathlib.Path(path.removesuffix(".jsonl"))
|
||||
if unkept := mirror(session_folder, folder / session_folder.name):
|
||||
return unkept
|
||||
copy = folder / f"{hashlib.sha256(raw).hexdigest()}.jsonl"
|
||||
# The copy lands last, so a copy on disk always has its verdict beside it.
|
||||
if not copy.exists():
|
||||
for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\n".encode()), (copy, raw)):
|
||||
if unwritten := replace_file(dest, data):
|
||||
return Failure(f"could not archive the transcript: {dest}: {unwritten.reason}")
|
||||
# The copy lands last, so a copy on disk always has its verdict and its session folder beside it.
|
||||
if copy.exists():
|
||||
return None
|
||||
for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\n".encode()), (copy, raw)):
|
||||
if unwritten := replace_file(dest, data):
|
||||
return Failure(f"could not archive the transcript: {dest}: {unwritten.reason}")
|
||||
print(f"transcript: {copy}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
|
||||
# Claude Code only appends to a session folder's files, so overwriting a copy loses nothing.
|
||||
def mirror(source: pathlib.Path, dest: pathlib.Path) -> Failure | None:
|
||||
for file in sorted(source.rglob("*")):
|
||||
copy = dest / file.relative_to(source)
|
||||
try:
|
||||
if not file.is_file():
|
||||
continue
|
||||
data = file.read_bytes()
|
||||
if copy.is_file() and copy.read_bytes() == data:
|
||||
continue
|
||||
make_private(copy.parent)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not archive the transcript: {failure.filename}: {failure.strerror}")
|
||||
if unwritten := replace_file(copy, data):
|
||||
return Failure(f"could not archive the transcript: {copy}: {unwritten.reason}")
|
||||
return None
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -22,6 +22,7 @@ class Interrupting:
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Finishing:
|
||||
transcript_path: str
|
||||
verdict: Finished | Unfinished
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -62,10 +63,8 @@ def transition(state: State, event: Event) -> State:
|
||||
return Judging(transcript_path)
|
||||
case Judging(transcript_path), Ruled(Interrupt(question=question)):
|
||||
return Interrupting(transcript_path, question)
|
||||
case Judging(transcript_path), Ruled(Finished()):
|
||||
return Finishing(transcript_path)
|
||||
case Judging(), Ruled(Unfinished()):
|
||||
return Ended()
|
||||
case Judging(transcript_path), Ruled(Finished() | Unfinished() as verdict):
|
||||
return Finishing(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), Answered():
|
||||
return Judging(transcript_path)
|
||||
|
||||
Reference in New Issue
Block a user