Record with each archived verdict the setup that ran it, what each model spent by kind, and the human's reversals
CI / tests (pull_request) Failing after 2m12s
CI / tests (pull_request) Failing after 2m12s
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
- <sha256>.jsonl is a copy of the session's transcript, named by the hash of its bytes, so each distinct
|
||||
transcript is kept once.
|
||||
- <sha256>.outcomes.md sits beside the copy and holds the task, then each outcome opened by its UTC time (outcomes.py).
|
||||
A verdict, an interrupt or a quoted answer also records the setup that ran it, what was spent since the
|
||||
previous record and any reversal the judge quoted (outcomes.Record).
|
||||
- session/ holds every other file of the session, such as the subagents' transcripts, which Claude
|
||||
Code keeps in a folder named after the session, beside the transcript. A file there is replaced only by a version that
|
||||
grew from it. A file Claude Code rewrote whole lands beside its copy as <name>.<hash of its content>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import re
|
||||
import typing
|
||||
|
||||
from records.markdown_text import quoted
|
||||
from records.utc_stamp import STAMPED
|
||||
@@ -28,6 +29,45 @@ JUDGE_COST = "Judge cost: "
|
||||
JUDGE_COST_AT = re.compile(rf"^{re.escape(JUDGE)}(.+)\n{re.escape(JUDGE_COST)}([0-9.]+)$", re.MULTILINE)
|
||||
|
||||
|
||||
# The claude-config release and commit that ran the cycle, on the line before JUDGE, such as `Config: v0.8.0 (<sha>)`.
|
||||
CONFIG = "Config: "
|
||||
# Opens the block of what the stretch of work since the previous record spent; a record that spent nothing has none.
|
||||
SPENT = "Spent since "
|
||||
# Opens the quote of a typed answer of the human's that reversed a choice the worker took itself.
|
||||
REVERSED = "Reversed:"
|
||||
REVERSED_AT = re.compile(rf"^{re.escape(REVERSED)}\n((?:> .*\n)*> .*)", re.MULTILINE)
|
||||
|
||||
|
||||
class Record(typing.NamedTuple):
|
||||
"""What a verdict, an interrupt or a quoted answer records beside its judge lines, so a change to the setup can be judged before and after: docs/decisions.md → `loop-claude: each verdict records the setup, the spend and the reversals…`. Each part is left out where empty; the cycle always gives config."""
|
||||
config: str = ""
|
||||
# The block spend.spent_block renders.
|
||||
spent: str = ""
|
||||
# The judge's quote of the human's reversing answer, as the judge checked it.
|
||||
reversed: str = ""
|
||||
|
||||
|
||||
def record_lines(record: Record) -> str:
|
||||
reversed_block = f"{REVERSED}\n{quoted(record.reversed)}\n\n" if record.reversed else ""
|
||||
spent_block = f"{record.spent}\n\n" if record.spent else ""
|
||||
config_line = f"{CONFIG}{record.config}\n" if record.config else ""
|
||||
return f"{reversed_block}{spent_block}{config_line}"
|
||||
|
||||
|
||||
def reversals(outcomes: str) -> list[str]:
|
||||
"""Each reversing answer the outcomes quote, unquoted, in the order they were written."""
|
||||
return [unquoted(found[1]) for found in REVERSED_AT.finditer(outcomes)]
|
||||
|
||||
|
||||
def unquoted(block: str) -> str:
|
||||
return "\n".join(line.removeprefix("> ") for line in block.split("\n"))
|
||||
|
||||
|
||||
def spent_stamps(outcomes: str) -> list[str]:
|
||||
"""The UTC stamp of each outcome that records a spend, in the order they were written."""
|
||||
return [stamp for stamp, outcome in each_outcome(outcomes) if f"\n{SPENT}" in outcome]
|
||||
|
||||
|
||||
def judge_lines(judge: str, judge_cost: float) -> str:
|
||||
"""The judge's rung, and what its run cost where that is known."""
|
||||
return f"{JUDGE}{judge}" + (f"\n{JUDGE_COST}{judge_cost:.4f}" if judge_cost else "")
|
||||
@@ -42,26 +82,26 @@ def escalation_block(escalation: str) -> str:
|
||||
return f"Escalation:\n{quoted(escalation)}\n\n" if escalation else ""
|
||||
|
||||
|
||||
def finished(worker: WorkerLine, evidence: str, judge: str, judge_cost: float = 0.0) -> str:
|
||||
return f"{FINISHED}\n\nEvidence:\n{quoted(evidence)}\n\n{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
def finished(worker: WorkerLine, evidence: str, judge: str, judge_cost: float = 0.0, record: Record = Record()) -> str:
|
||||
return f"{FINISHED}\n\nEvidence:\n{quoted(evidence)}\n\n{record_lines(record)}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
|
||||
|
||||
def unfinished(worker: WorkerLine, evidence: str, judge: str, escalation: str, judge_cost: float = 0.0) -> str:
|
||||
def unfinished(worker: WorkerLine, evidence: str, judge: str, escalation: str, judge_cost: float = 0.0, record: Record = Record()) -> str:
|
||||
"""escalation is the judge's quote showing that the worker's cited escalation trigger fired; empty for none."""
|
||||
escalated = escalation_block(escalation)
|
||||
return f"{UNFINISHED}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
return f"{UNFINISHED}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{record_lines(record)}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
|
||||
|
||||
def interrupted(question: str, evidence: str, judge: str, escalation: str, worker: WorkerLine, judge_cost: float = 0.0) -> str:
|
||||
def interrupted(question: str, evidence: str, judge: str, escalation: str, worker: WorkerLine, judge_cost: float = 0.0, record: Record = Record()) -> str:
|
||||
"""escalation is the judge's quote showing that the worker's cited escalation trigger fired; empty for none. worker is the line naming the route the worker ran on until the interrupt."""
|
||||
escalated = escalation_block(escalation)
|
||||
return f"{INTERRUPTED}\n\n{question}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
return f"{INTERRUPTED}\n\n{question}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{record_lines(record)}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
|
||||
|
||||
def answered(*, question: str, sources: str, answer: str, evidence: str, escalation: str, judge: str, worker: WorkerLine, judge_cost: float = 0.0) -> str:
|
||||
def answered(*, question: str, sources: str, answer: str, evidence: str, escalation: str, judge: str, worker: WorkerLine, judge_cost: float = 0.0, record: Record = Record()) -> str:
|
||||
"""escalation and worker are as on interrupted. Every parameter is keyword-only, since a swap of two str ones would still type-check."""
|
||||
escalated = escalation_block(escalation)
|
||||
return f"{ANSWERED}\n\n{question}\n\n{sources} says:\n{quoted(answer)}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
return f"{ANSWERED}\n\n{question}\n\n{sources} says:\n{quoted(answer)}\n\nEvidence:\n{quoted(evidence)}\n\n{escalated}{record_lines(record)}{judge_lines(judge, judge_cost)}\n{WORKER}{worker}"
|
||||
|
||||
|
||||
def rejected(why: str, reply: str, judge: str, judge_cost: float = 0.0) -> str:
|
||||
@@ -80,16 +120,20 @@ def stopped(worker: WorkerLine, why: str) -> str:
|
||||
|
||||
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")
|
||||
for stamp, outcome in each_outcome(outcomes):
|
||||
last = outcome.rsplit("\n", 1)[-1]
|
||||
if outcome.startswith(NAMES_A_WORKER) and last.startswith(WORKER):
|
||||
found.append((start[1], last.removeprefix(WORKER)))
|
||||
found.append((stamp, last.removeprefix(WORKER)))
|
||||
return found
|
||||
|
||||
|
||||
def each_outcome(outcomes: str) -> list[tuple[str, str]]:
|
||||
"""The UTC stamp and the text after it of each outcome, in the order they were written."""
|
||||
starts = list(OUTCOME_AT.finditer(outcomes))
|
||||
return [(start[1], outcomes[start.end():after.start() if after else len(outcomes)].rstrip("\n")) for start, after in zip(starts, [*starts[1:], None])]
|
||||
|
||||
|
||||
def task_of(outcomes: str) -> str | None:
|
||||
"""The task an outcomes file opens with; None when it opens with none."""
|
||||
found = TASK_AT.match(outcomes)
|
||||
|
||||
@@ -3,7 +3,7 @@ import pathlib
|
||||
|
||||
from transcript_archive.location import chunk_folder, FILE_SUFFIX, transcript_of
|
||||
from failure import Failure
|
||||
from transcript_archive.outcomes import FINISHED_AT, workers
|
||||
from transcript_archive.outcomes import FINISHED_AT, spent_stamps, workers
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -66,3 +66,14 @@ def latest_worker(project_archive: pathlib.Path, transcript: str) -> str | Failu
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not read the archive {project_archive}: {failure}")
|
||||
return None
|
||||
|
||||
|
||||
def last_spent(project_archive: pathlib.Path, transcript: str) -> str | Failure:
|
||||
"""The UTC stamp of the latest outcome in the transcript's chunk that records a spend; empty before any. Stamps share one format, so the greatest is the latest."""
|
||||
try:
|
||||
with open(transcript, "rb") as handle:
|
||||
folder = chunk_folder(project_archive, handle.read())
|
||||
stamps = [stamp for path in chunk_outcomes(folder) for stamp in spent_stamps(path.read_text(encoding="utf-8"))]
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(f"could not read the archive {project_archive}: {failure}")
|
||||
return max(stamps, default="")
|
||||
|
||||
@@ -105,6 +105,34 @@ class Outcomes(unittest.TestCase):
|
||||
self.assertEqual(read.latest_worker(kept, str(transcript)), "w2")
|
||||
self.assertIsInstance(read.latest_worker(kept, str(pathlib.Path(root, "gone.jsonl"))), Failure)
|
||||
|
||||
def test_a_record_quotes_the_reversal_then_the_spend_then_the_config_before_the_judge(self):
|
||||
record = outcomes.Record("v0.8.0 (abc)", "Spent since the chunk began, in tokens of x:\n- worker, m: 1", "No, keep it.\nUse X.")
|
||||
self.assertEqual(outcomes.finished("w", "e", "j", record=record), "Verdict: finished\n\nEvidence:\n> e\n\nReversed:\n> No, keep it.\n> Use X.\n\nSpent since the chunk began, in tokens of x:\n- worker, m: 1\n\nConfig: v0.8.0 (abc)\nJudge: j\nWorker: w")
|
||||
self.assertEqual(outcomes.finished("w", "e", "j", record=outcomes.Record("v0.8.0 (abc)")), "Verdict: finished\n\nEvidence:\n> e\n\nConfig: v0.8.0 (abc)\nJudge: j\nWorker: w")
|
||||
|
||||
def test_reads_back_each_reversal_and_the_stamps_of_the_outcomes_that_record_a_spend(self):
|
||||
spent = outcomes.Record("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1", "No, keep it.\nUse X.")
|
||||
kept = "Task: Do it.\n\n" + "\n".join(f"{utc(NOW + offset)} {outcome}\n" for offset, outcome in enumerate([
|
||||
outcomes.unfinished("w", "e", "j", "", record=spent),
|
||||
outcomes.stopped("w", "Ctrl-C"),
|
||||
outcomes.finished("w", "e", "j", record=outcomes.Record("c", "Spent since x, in tokens of x:\n- judge, m: 1")),
|
||||
]))
|
||||
self.assertEqual(outcomes.reversals(kept), ["No, keep it.\nUse X."])
|
||||
self.assertEqual(outcomes.spent_stamps(kept), [utc(NOW), utc(NOW + 2)])
|
||||
self.assertEqual(outcomes.workers(kept), [(utc(NOW), "w"), (utc(NOW + 1), "w"), (utc(NOW + 2), "w")])
|
||||
|
||||
def test_the_last_spend_is_the_latest_outcome_of_the_chunk_that_records_one(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
kept = pathlib.Path(root, "archive")
|
||||
transcript = pathlib.Path(root, "t.jsonl")
|
||||
transcript.write_text('{"timestamp": "2026-10-03T00:00:00Z"}\n')
|
||||
self.assertEqual(read.last_spent(kept, str(transcript)), "")
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.unfinished("w", "e", "j", "", record=outcomes.Record("c", "Spent since the chunk began, in tokens of x:\n- worker, m: 1")), NOW))
|
||||
self.assertIsNone(write.archive(kept, str(transcript), "Do it.", outcomes.stopped("w", "Ctrl-C"), NOW + 1))
|
||||
self.assertEqual(read.last_spent(kept, str(transcript)), utc(NOW))
|
||||
self.assertIsInstance(read.last_spent(kept, str(pathlib.Path(root, "gone.jsonl"))), Failure)
|
||||
|
||||
|
||||
class Location(unittest.TestCase):
|
||||
def test_keys_a_project_by_its_path_under_code_or_else_under_the_root(self):
|
||||
|
||||
Reference in New Issue
Block a user