Files
claude-config/bin/score_outcomes/reversals.py
T

43 lines
2.3 KiB
Python

"""The human's reversals of choices workers took on their own recommendation, per finished chunk in loop-claude's archive. docs/decisions.md → `loop-claude: a judge decides each interrupt and each quoted answer…`, in its entry of 2026-11-08, is valid while they stay few."""
import json
import pathlib
from transcript_archive.read import chunk_outcomes, finished_chunks
from transcript_archive.outcomes import CONFIG, reversals, workers
from failure import Failure
from score_outcomes.results import plural
def reversal_lines(project_archive: pathlib.Path) -> list[str] | Failure:
"""Newest first, each finished chunk the judge looked for reversals in, with how many distinct answers it quoted as reversing, then how many had none. The judge looks for them from the release that records a Config line beside each verdict, so a chunk without one is left out."""
chunks = finished_chunks(project_archive)
if isinstance(chunks, Failure):
return chunks
counted = []
for chunk in reversed(chunks):
texts = []
for path in chunk_outcomes(project_archive / chunk.folder):
try:
texts.append(path.read_text(encoding="utf-8"))
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"could not read the archive file {path}: {failure}")
if any(f"\n{CONFIG}" in text for text in texts):
# The judge quotes the latest reversal at each ruling, so one answer may be quoted many times.
found = dict.fromkeys(quote for text in texts for quote in reversals(text))
counted.append((f"{chunk.finished}{item_of(texts)}", len(found)))
if not counted:
return ["- None: loop-claude's archive holds no finished chunk whose verdicts record a Config line."]
lines = [f"- {chunk}: {count} reversed" for chunk, count in counted if count]
others = len(counted) - len(lines)
return lines + ([f"- {'The other' if lines else 'All'} {plural(others, 'finished chunk')}: none."] if others else [])
def item_of(texts: list[str]) -> str:
"""`, item <ID>` for the item the chunk's latest worker line routes it by; empty where none does."""
named = sorted(stamped for text in texts for stamped in workers(text))
try:
item = json.loads(named[-1][1]).get("item") if named else None
except (ValueError, AttributeError):
item = None
return f", item {item}" if isinstance(item, int) and not isinstance(item, bool) and item else ""