Files
claude-config/bin/transcript_archive/outcomes.py
T

44 lines
1.6 KiB
Python

import re
from markdown_text import quoted
from utc_stamp import STAMPED
FINISHED = "Verdict: finished"
FINISHED_AT = re.compile(rf"^({STAMPED}) {re.escape(FINISHED)}$", re.MULTILINE)
# An outcome opens with its UTC stamp; a verdict that closes a cycle, or a stop, ends with the worker line.
OUTCOME_AT = re.compile(rf"^({STAMPED}) ", re.MULTILINE)
CLOSES_A_CYCLE = ("Verdict: finished\n", "Verdict: unfinished\n", "Stopped: ")
WORKER = "Worker: "
def finished(worker: str, evidence: str) -> str:
return f"{FINISHED}\n\nEvidence:\n{quoted(evidence)}\n\n{WORKER}{worker}"
def unfinished(worker: str, evidence: str) -> str:
return f"Verdict: unfinished\n\nEvidence:\n{quoted(evidence)}\n\n{WORKER}{worker}"
def interrupted(question: str, evidence: str) -> str:
return f"Verdict: interrupt\n\n{question}\n\nEvidence:\n{quoted(evidence)}"
def answered(question: str, sources: str, answer: str, evidence: str) -> str:
return f"Verdict: quoted answer\n\n{question}\n\n{sources} says:\n{quoted(answer)}\n\nEvidence:\n{quoted(evidence)}"
def stopped(worker: str, why: str) -> str:
return f"Stopped: {why}\n\n{WORKER}{worker}"
def workers(outcomes: str) -> list[tuple[str, str]]:
"""The UTC stamp and the worker of each outcome that closes a cycle, in the order they were written."""
starts = list(OUTCOME_AT.finditer(outcomes))
found = []
for start, after in zip(starts, [*starts[1:], None]):
outcome = outcomes[start.end():after.start() if after else len(outcomes)].rstrip("\n")
last = outcome.rsplit("\n", 1)[-1]
if outcome.startswith(CLOSES_A_CYCLE) and last.startswith(WORKER):
found.append((start[1], last.removeprefix(WORKER)))
return found