import dataclasses import hashlib import json import pathlib from failure import Failure # The folder of a chunk's judge runs, beside its transcript copies. FOLDER = "judge" @dataclasses.dataclass(frozen=True) class JudgeRun: """The prompt a judge run read and claude's json reply to it. The judge has no tools and one turn, so these two are its whole transcript.""" prompt: str reply: dict def content(self) -> bytes: return json.dumps({"prompt": self.prompt, "reply": self.reply}, ensure_ascii=False, indent=1).encode() @property def file(self) -> str: """Where the archive keeps the run, relative to its chunk's folder. The name is the hash of the content, so a run archived twice is kept once.""" return f"{FOLDER}/{hashlib.sha256(self.content()).hexdigest()}.json" @staticmethod def read(path: pathlib.Path) -> "JudgeRun | Failure": try: kept = json.loads(path.read_text(encoding="utf-8")) return JudgeRun(kept["prompt"], kept["reply"]) except (OSError, ValueError, KeyError, TypeError) as failure: return Failure(f"could not read the archived judge run {path}: {failure}")