Stop the loop when a finished chunk's session cannot be deleted, and give the session one owner

This commit is contained in:
2026-10-02 07:38:18 +02:00
parent 2c39de2b48
commit ce9c96c57c
6 changed files with 39 additions and 23 deletions
+15 -8
View File
@@ -3,9 +3,9 @@ import glob
import os
import pathlib
import shutil
import sys
import uuid
from loop_claude.outcome import Failure
from loop_claude.paths import Paths
NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
@@ -26,18 +26,25 @@ def session_for(project: str, paths: Paths) -> Session:
# <claude_code_transcripts>/-home-me-code-log/<session id>.jsonl
def transcript_file(session: Session) -> str:
def transcript_file(session: Session) -> str | None:
hit = glob.glob(os.path.join(glob.escape(str(session.claude_code_transcripts)), "*", f"{session.id}.jsonl"))
return hit[0] if hit else ""
return hit[0] if hit else None
def delete_session(session: Session) -> None:
def session_open(session: Session) -> bool:
return transcript_file(session) is not None
def delete_session(session: Session) -> Failure | None:
path = transcript_file(session)
if not path:
return
if path is None:
return None
stem = path.removesuffix(".jsonl")
# <session id>.jsonl, <session id>/subagents/, and from older versions <session id>.superseded-1789290815002/ and <session id>.orphaned-*.jsonl
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
# The transcript goes last, so a failed delete leaves a session that session_open still sees.
for sibling in sorted(glob.glob(glob.escape(stem) + "*"), key=lambda sibling: sibling == path):
try:
shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling)
except OSError as failure:
print(f"could not remove {sibling}: {failure}", file=sys.stderr)
return Failure(f"could not delete {sibling}: {failure.strerror}; delete {stem}* and rerun loop-claude, or the next chunk resumes the finished chunk's session")
return None
+4 -7
View File
@@ -1,7 +1,7 @@
import dataclasses
import typing
from loop_claude.claude_code.session import Session, delete_session, transcript_file
from loop_claude.claude_code.session import Session, delete_session, session_open, transcript_file
from loop_claude.claude_code.transcript import read, render
from loop_claude.cycle.archive import archive
from loop_claude.cycle.interrupt import interrupt
@@ -56,7 +56,7 @@ def work(cycle: Cycle) -> WorkerRan | Stop:
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, session_open(cycle.session)), cycle.effects), cycle.effects):
return stop
path = transcript_file(cycle.session)
return WorkerRan(path) if path else Stop(1, f"claude left no transcript for session {cycle.session.id}")
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
@@ -77,9 +77,6 @@ def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
def close(cycle: Cycle, transcript_path: str) -> Archived | Stop:
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
return Stop(1, failure.reason)
delete_session(cycle.session)
if failure := delete_session(cycle.session):
return Stop(1, failure.reason)
return Archived()
def session_open(session: Session) -> bool:
return bool(transcript_file(session))
+6 -6
View File
@@ -11,12 +11,12 @@ its session next cycle; the next chunk starts a fresh session.
Cycles continue until an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary".
Exit 1: no task, git cannot start or finds no checkout, no premises, a models file it cannot read or
write, claude leaves no transcript or one it cannot read or archive, claude cannot start for an
interrupt, python cannot start for a reload, a reload whose loop run it cannot read, or the worker or the
judge fails three times running, retried a minute apart within the cycle. A worker fails when claude
cannot start, exits non-zero, is killed, or ends without a result event; a judge when claude cannot
start or exits non-zero, or its verdict misses or mistypes a field, interrupts without a question,
or quotes nothing in the transcript.
write, claude leaves no transcript or one it cannot read or archive, a finished chunk's session it
cannot delete, claude cannot start for an interrupt, python cannot start for a reload, a reload whose
loop run it cannot read, or the worker or the judge fails three times running, retried a minute apart
within the cycle. A worker fails when claude cannot start, exits non-zero, is killed, or ends without
a result event; a judge when claude cannot start or exits non-zero, or its verdict misses or mistypes
a field, interrupts without a question, or quotes nothing in the transcript.
Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin or stdout is not a
terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any
open session.