Measure routing figures every 20th cycle and raise each call for a routing change once
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"""Reads the chunks of every project in loop-claude's archive, and keeps the routing figures and the calls already raised at the archive's root."""
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
from routing_figures.measure import Chunk, ChunkFigures, measured, Subagent
|
||||
from failure import Failure
|
||||
from transcript_archive.read import finished_chunks
|
||||
from claude_code.entries import parse_entry
|
||||
from file_writes import replace_file
|
||||
from utc_stamp import STAMPED
|
||||
|
||||
FIGURES = "routing-figures.md"
|
||||
RAISED = "routing-calls-raised.md"
|
||||
RAISED_HEADING = "# Routing calls raised\n"
|
||||
RAISED_CALL = re.compile(rf"^- {STAMPED} (.+)$", re.MULTILINE)
|
||||
VERDICT = re.compile(rf"^{STAMPED} Verdict: (?:finished|unfinished)$", re.MULTILINE)
|
||||
|
||||
|
||||
def cycles(root: pathlib.Path) -> int | Failure:
|
||||
"""The cycles every project's loop has closed with a verdict."""
|
||||
try:
|
||||
return sum(len(VERDICT.findall(path.read_text(encoding="utf-8"))) for path in root.rglob("*.outcomes.md"))
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not count the cycles in {root}: {failure}")
|
||||
|
||||
|
||||
def measure_archive(root: pathlib.Path, home: str) -> list[ChunkFigures] | Failure:
|
||||
"""Each finished chunk's figures, measured one chunk at a time, since the archive outgrows memory."""
|
||||
try:
|
||||
projects = sorted({path.parent.parent for path in root.rglob("*.outcomes.md")})
|
||||
except OSError as failure:
|
||||
return Failure(f"could not read the archive {root}: {failure}")
|
||||
found = []
|
||||
for project in projects:
|
||||
chunks = finished_chunks(project)
|
||||
if isinstance(chunks, Failure):
|
||||
return chunks
|
||||
for chunk in chunks:
|
||||
folder = project / chunk.folder
|
||||
try:
|
||||
kept = "\n".join(path.read_text(encoding="utf-8") for path in sorted(folder.glob("*.outcomes.md")))
|
||||
subagents = [Subagent(meta(path), entries(path)) for path in chunk.subagents]
|
||||
found.append(measured(Chunk(str(project.relative_to(root)), kept, entries(chunk.transcript), subagents), home))
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not read the chunk {folder}: {failure}")
|
||||
return found
|
||||
|
||||
|
||||
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 meta(transcript: pathlib.Path) -> dict:
|
||||
"""The .meta.json Claude Code writes beside a subagent's transcript; empty where it wrote none or one this cannot read."""
|
||||
path = transcript.with_name(transcript.name.removesuffix(".jsonl") + ".meta.json")
|
||||
try:
|
||||
found = json.loads(path.read_text(encoding="utf-8"))
|
||||
except (FileNotFoundError, ValueError):
|
||||
return {}
|
||||
return found if isinstance(found, dict) else {}
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def unraised(root: pathlib.Path, found: list[str]) -> list[str] | Failure:
|
||||
"""The calls among found that were never raised."""
|
||||
raised = read_raised(root)
|
||||
return raised if isinstance(raised, Failure) else [call for call in found if call not in set(RAISED_CALL.findall(raised))]
|
||||
|
||||
|
||||
def mark_raised(root: pathlib.Path, raised: list[str], at: str) -> Failure | None:
|
||||
kept = read_raised(root)
|
||||
if isinstance(kept, Failure):
|
||||
return kept
|
||||
# The first call opens a list below the heading; each later one joins it.
|
||||
gap = "\n" if RAISED_CALL.search(kept) else "\n\n"
|
||||
unwritten = replace_file(root / RAISED, (kept.rstrip("\n") + gap + "".join(f"- {at} {call}\n" for call in raised)).encode())
|
||||
return Failure(f"could not write {root / RAISED}: {unwritten.reason}") if unwritten else None
|
||||
|
||||
|
||||
def read_raised(root: pathlib.Path) -> str | Failure:
|
||||
try:
|
||||
return (root / RAISED).read_text(encoding="utf-8")
|
||||
except FileNotFoundError:
|
||||
return RAISED_HEADING
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not read {root / RAISED}: {failure}")
|
||||
Reference in New Issue
Block a user