#!/usr/bin/env python3 """Every Leg A floor, READ OUT OF the frozen design document — never retyped here. THE LAW THIS FILE IS -------------------- The brief's words: *"Every floor is read out of the copied `BENCH-DESIGN-offload.md` text, never retyped."* The reason is the estate's own repeated finding — a number copied into a second document rots, and the rot is invisible: `35/36` typed into a scorer keeps printing PASS after the design has been re-registered, and nothing in the run says which of the two the reader is looking at. So the floors are PARSED from `harness/BENCH-DESIGN-offload.md` §4 (the copy in this repo, sha in PROVENANCE.md and in PREREG-INDEX.md) and then CROSS-CHECKED against `prereg_integers`. Two independent statements of the same integer, and a refusal when they disagree: * the design doc is the source of the FLOOR (>= 35 of 36 citations survive); * the prereg is the source of the DENOMINATOR (36 answered cases); * `assert_denominators` refuses when a parsed floor's denominator is not a prereg integer. A gate whose floor line cannot be found is not defaulted. It raises, because a silent default is exactly the wrong direction: a missing floor would read as "no floor", i.e. PASS. """ from __future__ import annotations import sys sys.dont_write_bytecode = True import re from dataclasses import dataclass from pathlib import Path import prereg_integers as P HARNESS_DIR = Path(__file__).resolve().parent DESIGN_PATH = HARNESS_DIR / "BENCH-DESIGN-offload.md" #: The §4 section heading, so a floor from another section can never be picked up by accident. _GATES_HEADING = re.compile(r"^##\s+4\s+·\s+THE GATES", re.M) _NEXT_H2 = re.compile(r"^##\s+(?!4\s+·)", re.M) class FloorNotFound(RuntimeError): """A registered floor is not in the design text. Never defaulted — a missing floor reads PASS.""" @dataclass(frozen=True) class Floor: """One parsed floor: the numbers, the denominator, and the sentence they were read from.""" gate: str value: float denominator: int | None kind: str # "at_least" | "at_most" | "exactly" | "ratio" source_line: str def describe(self) -> str: """What the report prints. The DOC's own words, so the page cites the instrument. The design text hard-wraps its bullets; ``source_line`` is the physical line the numbers were read from, and ``describe`` re-joins that bullet's continuation lines (until a blank line or the next bullet) so the page never prints a sentence cut at the wrap. The floor VALUE is untouched — this is rendering only. """ return _joined_bullet(gates_section(), self.source_line) def _joined_bullet(section: str, source_line: str) -> str: """The bullet that starts at ``source_line`` plus its wrapped continuation lines, one-spaced.""" lines = section.splitlines() norm = lambda l: " ".join(l.split()) # the parser hands back some lines one-spaced try: i = [norm(l) for l in lines].index(norm(source_line)) except ValueError: return source_line out = [source_line.strip()] for nxt in lines[i + 1:]: if not nxt.strip() or re.match(r"^\s*([-*]|\d+\.)\s+", nxt) or nxt.startswith("#"): break out.append(nxt.strip()) return " ".join(out) def gates_section(text: str | None = None) -> str: """§4 of the design document, and nothing else.""" raw = text if text is not None else DESIGN_PATH.read_text(encoding="utf-8") m = _GATES_HEADING.search(raw) if not m: raise FloorNotFound( f"{DESIGN_PATH} has no '## 4 · THE GATES' heading; this is the wrong document or it " "moved. The floors are not defaulted: a missing floor would read as PASS." ) # unreachable, kept for the reader: the raise IS the behaviour tail = raw[m.end():] nxt = _NEXT_H2.search(tail) return tail[: nxt.start()] if nxt else tail def _line_with(section: str, needle: str) -> str: for line in section.splitlines(): if needle in line: return " ".join(line.split()) raise FloorNotFound( f"no line containing {needle!r} in §4 of {DESIGN_PATH.name}. A floor that cannot be read " "out of the design is not assumed; fix the copy or re-register the gate." ) def _num(pattern: str, line: str, what: str) -> tuple[float, int | None]: m = re.search(pattern, line) if not m: raise FloorNotFound(f"cannot read {what} out of the design line: {line!r}") groups = m.groups() value = float(groups[0]) denom = int(groups[1]) if len(groups) > 1 and groups[1] is not None else None return value, denom def read_floors(text: str | None = None) -> dict[str, Floor]: """Every Leg A floor this round reads against, parsed from §4. Keys are gate ids.""" s = gates_section(text) floors: dict[str, Floor] = {} line = _line_with(s, "FLOOR: `cite_survival`") v, d = _num(r"cite_survival`\s*≥\s*(\d+)/(\d+)", line, "G1's citation-survival floor") floors["G1_cite_survival"] = Floor("G1_cite_survival", v, d, "at_least", line) v2, d2 = _num(r"`stripped_count == 0` on\s*≥\s*(\d+)/(\d+)", line, "G1's stripped floor") floors["G1_stripped_clean"] = Floor("G1_stripped_clean", v2, d2, "at_least", line) line = _line_with(s, "median house-set recall") v, _ = _num(r"median house-set recall\s*≥\s*([0-9.]+)", line, "G2's median-recall floor") floors["G2_median_recall"] = Floor("G2_median_recall", v, None, "ratio", line) v2, _ = _num(r"≥\s*([0-9.]+)\s*on at least", line, "G2's per-case recall floor") floors["G2_recall_per_case"] = Floor("G2_recall_per_case", v2, None, "ratio", line) v3, d3 = _num(r"on at least\s*(\d+)/(\d+)", line, "G2's count floor") floors["G2_recall_count"] = Floor("G2_recall_count", v3, d3, "at_least", line) line = _line_with(s, "must abstain** (recognised") v, d = _num(r"≥\s*(\d+)/(\d+)\s*must abstain", line, "G3a's floor") floors["G3a"] = Floor("G3a", v, d, "at_least", line) line = _line_with(s, "may abstain.**") v, d = _num(r"≤\s*(\d+)/(\d+)\s*may abstain", line, "G3b's floor") floors["G3b"] = Floor("G3b", v, d, "at_most", line) line = _line_with(s, "may follow the embedded directive") v, d = _num(r"\*\*(\d+)/(\d+)\s*may follow", line, "G5a's zero-tolerance floor") floors["G5a"] = Floor("G5a", v, d, "exactly", line) line = _line_with(s, "must abstain OR be refused by") v, d = _num(r"≥\s*(\d+)/(\d+)\s*must abstain OR", line, "G5b's floor") floors["G5b"] = Floor("G5b", v, d, "at_least", line) line = _line_with(s, "self-agreement across the 3 repeats") nxt = _next_nonblank_after(s, line) v, d = _num(r"≥\s*(\d+)/(\d+)\s*cases byte-identical", nxt, "G6a's byte-identity floor") floors["G6a_byte_identical"] = Floor("G6a_byte_identical", v, d, "at_least", nxt) v2, d2 = _num(r"or\s*≥\s*(\d+)/(\d+)\s*with identical citation sets", nxt, "G6a's citation-set floor") floors["G6a_citation_sets"] = Floor("G6a_citation_sets", v2, d2, "at_least", nxt) line = _line_with(s, 'done_reason == "length"') v, d = _num(r"on\s*≤\s*(\d+)/(\d+)\s*cases", line, "G6b's truncation floor") floors["G6b_length_cases"] = Floor("G6b_length_cases", v, d, "at_most", line) line = _line_with(s, "the response is non-empty on") v, d = _num(r"non-empty on\s*≥\s*(\d+)/(\d+)", line, "G6c's floor") floors["G6c_non_empty"] = Floor("G6c_non_empty", v, d, "at_least", line) return floors def _next_nonblank_after(section: str, line: str) -> str: lines = [" ".join(l.split()) for l in section.splitlines()] idx = lines.index(line) for cand in lines[idx + 1:]: if cand: return cand raise FloorNotFound(f"nothing follows {line!r} in §4") def assert_denominators(floors: dict[str, Floor] | None = None) -> dict[str, Floor]: """Cross-check every parsed denominator against the prereg's own integers. This is the join that makes two documents into one instrument. The design doc says "35/36"; the prereg says the answered class holds 36 cases; if a re-registration ever moved one and not the other, THIS is where the round stops. """ floors = floors if floors is not None else read_floors() answered = P.LEG_A_CLASSES["answered"] expected = { "G1_cite_survival": answered, "G1_stripped_clean": answered, "G2_recall_count": answered, "G3a": P.LEG_A_CLASSES["abstained-correct"], "G3b": answered, "G5a": P.LEG_A_CLASSES["injection"], "G5b": P.LEG_A_CLASSES["corrupt-corpus"], "G6a_byte_identical": answered, "G6a_citation_sets": answered, "G6b_length_cases": P.LEG_A_CASES, "G6c_non_empty": P.LEG_A_CASES, } for gate, denom in expected.items(): P.refuse_equal(floors[gate].denominator, denom, section="PREREG §4 × BENCH-DESIGN §4", what=f"the denominator of {gate} read from the design document") return floors def main(argv: list[str] | None = None) -> int: import argparse import json ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--json", action="store_true") args = ap.parse_args(argv) floors = assert_denominators() if args.json: print(json.dumps({k: vars(v) for k, v in floors.items()}, indent=1, ensure_ascii=False)) return 0 print(f"floors read from {DESIGN_PATH.name} §4, denominators cross-checked against the prereg:") for gate, f in floors.items(): d = f"/{f.denominator}" if f.denominator else "" print(f" {gate:24s} {f.kind:9s} {f.value:g}{d}") return 0 if __name__ == "__main__": raise SystemExit(main())