Files

94 lines
3.9 KiB
Python

import dataclasses
import pathlib
from transcript_archive.location import chunk_folder, FILE_SUFFIX, transcript_of
from failure import Failure
from transcript_archive.outcomes import FINISHED_AT, spent_stamps, workers
from text_files.compressed import read_stored, stored_glob
@dataclasses.dataclass(frozen=True)
class FinishedChunk:
folder: str
finished: str
transcript: pathlib.Path
subagents: list[pathlib.Path]
# One per chunk folder, its latest finished copy, in the order they finished.
def finished_chunks(project_archive: pathlib.Path) -> list[FinishedChunk] | Failure:
found = []
try:
folders = sorted(path for path in project_archive.glob("*") if path.is_dir())
except OSError as failure:
return Failure(f"could not read the archive {project_archive}: {failure}")
for folder in folders:
copies = []
for outcomes in chunk_outcomes(folder):
try:
stamps = FINISHED_AT.findall(outcomes.read_text(encoding="utf-8"))
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read the archive file {outcomes}: {failure}")
if stamps:
copies.append((stamps[-1], outcomes))
if copies:
finished, outcomes = max(copies)
found.append(FinishedChunk(folder.name, finished, transcript_of(outcomes), subagent_transcripts(folder)))
return sorted(found, key=lambda chunk: (chunk.finished, chunk.folder))
def archived_outcomes(root: pathlib.Path) -> list[tuple[str, pathlib.Path]] | Failure:
"""Each outcomes file in the archive at root, with the key of the project it belongs to, sorted."""
try:
return sorted((str(path.parent.parent.relative_to(root)), path) for path in root.rglob(f"*{FILE_SUFFIX}"))
except OSError as failure:
return Failure(f"could not read the archive {root}: {failure}")
def subagent_transcripts(folder: pathlib.Path) -> list[pathlib.Path]:
"""The plain name of each subagent's transcript archived in a chunk's folder, kept plain or gzipped, sorted."""
return [path for session in sorted(folder.glob("session/*/subagents")) for path in stored_glob(session, "*.jsonl")]
def chunk_outcomes(folder: pathlib.Path) -> list[pathlib.Path]:
"""The outcomes file of each transcript copy in a chunk's folder, sorted."""
return sorted(folder.glob(f"*{FILE_SUFFIX}"))
def read_threads(chunk: FinishedChunk) -> list[bytes] | Failure:
"""The worker's transcript, then each subagent's."""
try:
return [read_stored(path) for path in (chunk.transcript, *chunk.subagents)]
except OSError as failure:
return Failure(f"could not read {failure.filename}: {failure.strerror}")
def latest_worker(project_archive: pathlib.Path, transcript: str) -> str | Failure | None:
"""The worker named by the latest outcome archived for the transcript's chunk; None before any outcome names one.
Stamps run to the second, so the newest outcomes file decides, and in it the last outcome naming a worker.
"""
try:
with open(transcript, "rb") as handle:
folder = chunk_folder(project_archive, handle.read())
if not folder.is_dir():
return None
files = sorted((path for path in folder.iterdir() if path.name.endswith(FILE_SUFFIX)), key=lambda path: path.stat().st_mtime_ns, reverse=True)
for path in files:
if found := workers(path.read_text(encoding="utf-8")):
return found[-1][1]
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read the archive {project_archive}: {failure}")
return None
def last_spent(project_archive: pathlib.Path, transcript: str) -> str | Failure:
"""The UTC stamp of the latest outcome in the transcript's chunk that records a spend; empty before any. Stamps share one format, so the greatest is the latest."""
try:
with open(transcript, "rb") as handle:
folder = chunk_folder(project_archive, handle.read())
stamps = [stamp for path in chunk_outcomes(folder) for stamp in spent_stamps(path.read_text(encoding="utf-8"))]
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read the archive {project_archive}: {failure}")
return max(stamps, default="")