import dataclasses import glob import os import pathlib import shutil import sys import uuid from loop_claude.checkout import project_name NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec") @dataclasses.dataclass(frozen=True) class Session: id: uuid.UUID name: str projects: pathlib.Path def session_for(root, paths): """The project's one session, at an id fixed by its name. It exists while its transcript does: a chunk's first cycle starts it, each later cycle of the chunk resumes it, and the finished chunk deletes it.""" # "A " is the human's cue in the session picker that the session is the loop's. name = f"A {project_name(root, paths.code)} loop-claude" return Session(uuid.uuid5(NAMESPACE, name), name, paths.projects) def transcript_file(session): # Where Claude Code 2.1.282 writes a session's transcript. hit = glob.glob(os.path.join(glob.escape(str(session.projects)), "*", f"{session.id}.jsonl")) return hit[0] if hit else "" def delete_session(session): path = transcript_file(session) if not path: return # Claude Code 2.1.282 also keeps a / folder and .superseded-*/ and .orphaned-*.jsonl siblings. for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"): 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)