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