53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
import re
|
|
|
|
from markdown_text import quoted
|
|
from utc_stamp import STAMPED
|
|
|
|
FINISHED = "Verdict: finished"
|
|
UNFINISHED = "Verdict: unfinished"
|
|
FINISHED_AT = re.compile(rf"^({STAMPED}) {re.escape(FINISHED)}$", re.MULTILINE)
|
|
UNFINISHED_AT = re.compile(rf"^{STAMPED} {re.escape(UNFINISHED)}$", re.MULTILINE)
|
|
# A verdict closes each cycle the judge ruled on; an interrupt or a stop closes none.
|
|
VERDICT_AT = re.compile(rf"^{STAMPED} (?:{re.escape(FINISHED)}|{re.escape(UNFINISHED)})$", re.MULTILINE)
|
|
# An outcome opens with its UTC stamp; a verdict that closes a cycle, a stop, or a step up ends with the worker line.
|
|
OUTCOME_AT = re.compile(rf"^({STAMPED}) ", re.MULTILINE)
|
|
NAMES_A_WORKER = ("Verdict: finished\n", "Verdict: unfinished\n", "Stopped: ", "Stepped up\n")
|
|
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"{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 stepped_up(worker: str) -> str:
|
|
"""The rung the next cycle steps up to, archived as it steps, so a rerun after any exit resumes on it."""
|
|
return f"Stepped up\n\n{WORKER}{worker}"
|
|
|
|
|
|
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 names one, 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(NAMES_A_WORKER) and last.startswith(WORKER):
|
|
found.append((start[1], last.removeprefix(WORKER)))
|
|
return found
|