"""Reads the chunks of every project in loop-claude's archive, and keeps the routing figures at the archive's root for the human to read.""" import contextlib import dataclasses import fcntl import functools import json import pathlib import typing from transcript_archive.read import archived_outcomes, chunk_outcomes, finished_chunks from branch_chunks.read import branch_chunks from branch_chunks.cut import BranchChunk from transcript_archive.project_key import checkout_of from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Subagent from git_cli.git_repo import default_ref, Git from failure import Failure from claude_code.entries import parse_entry from records.file_writes import replace_file from transcript_archive.outcomes import VERDICT_AT FIGURES = "routing-figures.md" # Seconds each git call reading a project's history may take. GIT_TIMEOUT = 60 LOCK = "routing-figures.lock" @contextlib.contextmanager def locked(root: pathlib.Path) -> typing.Iterator[bool]: """Whether this loop holds the archive's routing lock; while another loop holds it, measuring is that loop's.""" try: handle = open(root / LOCK, "a") except OSError: yield False return with handle: try: fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError: yield False return yield True def cycles(root: pathlib.Path) -> int | Failure: """The cycles every project's loop has closed with a verdict.""" listed = archived_outcomes(root) if isinstance(listed, Failure): return listed try: return sum(len(VERDICT_AT.findall(path.read_text(encoding="utf-8"))) for _, path in listed) except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not count the cycles in {root}: {failure}") @dataclasses.dataclass(frozen=True) class ArchiveChunks: """skipped names each chunk folder that could not be read whole, such as one another loop is writing; unread names each project whose default branch could not be read, with why.""" chunks: list[ChunkFigures] skipped: tuple[str, ...] unread: tuple[str, ...] = () def measure_archive(root: pathlib.Path, home: str, since: str, git: Git) -> ArchiveChunks | Failure: """The figures of each chunk that finished after since, measured one chunk at a time because the archive outgrows memory. Only an archive that cannot be listed fails.""" listed = archived_outcomes(root) if isinstance(listed, Failure): return listed projects = [measure_project(root, project, home, since, git) for project in sorted({root / key for key, _ in listed})] return ArchiveChunks( [chunk for project in projects for chunk in project.chunks], tuple(skipped for project in projects for skipped in project.skipped), tuple(unread for project in projects for unread in project.unread), ) def measure_project(root: pathlib.Path, project: pathlib.Path, home: str, since: str, git: Git) -> ArchiveChunks: """The figures of each of one project's chunks that finished after since, as measure_archive measures them.""" key = str(project.relative_to(root)) listed = finished_chunks(project) if isinstance(listed, Failure): return ArchiveChunks([], (key,)) chunks = [chunk for chunk in listed if chunk.finished > since] if not chunks: return ArchiveChunks([], ()) history = branch_history(checkout_of(key, pathlib.Path(home)), git) found = [] skipped = [] for chunk in chunks: folder = project / chunk.folder try: kept = [path.read_text(encoding="utf-8") for path in chunk_outcomes(folder)] subagents = [subagent(path) for path in chunk.subagents] worker = entries(chunk.transcript) except (OSError, UnicodeDecodeError): skipped.append(str(folder.relative_to(root))) continue found.append(chunk_figures(Chunk(key, kept, worker, subagents), home, None if isinstance(history, Failure) else history)) return ArchiveChunks(found, tuple(skipped), (f"{key}: {history.reason}",) if isinstance(history, Failure) else ()) def branch_history(checkout: str, git: Git) -> list[BranchChunk] | Failure: """The chunks on the checkout's default branch as last fetched, read guarded, since the checkout's config may name a program.""" ref = default_ref(checkout, GIT_TIMEOUT, guarded=True, run=git) return ref if isinstance(ref, Failure) else branch_chunks(ref, functools.partial(git, cwd=checkout, timeout=GIT_TIMEOUT, guarded=True)) def entries(path: pathlib.Path) -> list[dict]: with open(path, "rb") as handle: return [entry for line in handle if (entry := parse_entry(line))] def subagent(transcript: pathlib.Path) -> Subagent: """The subagent, named by the .meta.json Claude Code writes beside its transcript: {"agentType": "Explore", "toolUseId": "toolu_01…", ...}. A meta missing or unreadable names nothing.""" path = transcript.with_name(transcript.name.removesuffix(".jsonl") + ".meta.json") try: meta = json.loads(path.read_text(encoding="utf-8")) except (FileNotFoundError, ValueError): meta = {} meta = meta if isinstance(meta, dict) else {} return Subagent(str(meta.get("agentType") or ""), str(meta.get("toolUseId") or ""), entries(transcript)) def write_figures(root: pathlib.Path, text: str) -> Failure | None: unwritten = replace_file(root / FIGURES, text.encode()) return Failure(f"could not write {root / FIGURES}: {unwritten.reason}") if unwritten else None