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.
+13 -1
View File
@@ -152,7 +152,7 @@ class Cycles(unittest.TestCase):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
self.assertIsNone(steps.run_cycle(self.cycle(fake)))
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"])
self.assertEqual(session.transcript_file(self.session), "")
self.assertIsNone(session.transcript_file(self.session))
def test_an_unfinished_chunk_keeps_its_session_and_the_next_cycle_resumes_it(self):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 2, claude=[judged("unfinished")] * 2)
@@ -178,6 +178,18 @@ class Cycles(unittest.TestCase):
self.assertNotIn("the second", prompts[0])
self.assertIn("[human] the second", prompts[1])
@unittest.skipIf(os.geteuid() == 0, "root writes into a read-only directory")
def test_stops_keeping_the_transcript_when_the_finished_chunks_session_cannot_be_deleted(self):
folder = self.paths.claude_code_transcripts / "-fake" / str(self.session.id)
(folder / "subagents").mkdir(parents=True)
folder.chmod(0o500)
self.addCleanup(folder.chmod, 0o700)
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
stop = steps.run_cycle(self.cycle(fake))
self.assertEqual(stop, Stop(1, f"could not delete {folder}: Permission denied; delete {folder}* and rerun loop-claude, or the next chunk resumes the finished chunk's session"))
self.assertTrue(session.session_open(self.session))
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"])
def test_stops_when_the_worker_leaves_no_transcript(self):
fake = FakeEffects(streams=[Streamed(0)])
self.assertEqual(steps.run_cycle(self.cycle(fake)), Stop(1, f"claude left no transcript for session {self.session.id}"))