44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import dataclasses
|
|
import glob
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import uuid
|
|
|
|
from loop_claude.paths import Paths
|
|
|
|
NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Session:
|
|
"""Exists while its transcript does."""
|
|
id: uuid.UUID
|
|
claude_code_transcripts: pathlib.Path
|
|
name: str
|
|
|
|
|
|
def session_for(project: str, paths: Paths) -> Session:
|
|
# "A " is the human's cue in the session picker that the session is the loop's.
|
|
name = f"A {project} loop-claude"
|
|
return Session(id=uuid.uuid5(NAMESPACE, name), claude_code_transcripts=paths.claude_code_transcripts, name=name)
|
|
|
|
|
|
# <claude_code_transcripts>/-home-me-code-log/<session id>.jsonl
|
|
def transcript_file(session: Session) -> str:
|
|
hit = glob.glob(os.path.join(glob.escape(str(session.claude_code_transcripts)), "*", f"{session.id}.jsonl"))
|
|
return hit[0] if hit else ""
|
|
|
|
|
|
def delete_session(session: Session) -> None:
|
|
path = transcript_file(session)
|
|
if not path:
|
|
return
|
|
# <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")) + "*"):
|
|
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)
|