|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""P-022 — the merge gate: does the world a freeze enters satisfy its references? |
| 3 | +
|
| 4 | +The frozen T0 names things that live outside itself: an accepted harness |
| 5 | +identity, the step-7 machinery that enforces the campaign join, the T0-0 |
| 6 | +amendment that revoked automatic collection authority. On the branch where T0 |
| 7 | +was frozen, some of those referents arrive through *other* pull requests. That |
| 8 | +is fine while the merge order holds — and merge order held by agreement is one |
| 9 | +stray click away from becoming an archaeological artifact, with a document |
| 10 | +saying FROZEN in a tree where its referent does not exist yet. |
| 11 | +
|
| 12 | +So the order is checked rather than promised, and it is checked against the |
| 13 | +**target tree**, never against pull-request numbers. A PR number proves that |
| 14 | +someone pressed a button; it proves nothing about what the merged tree contains. |
| 15 | +
|
| 16 | +Run it on the commit a merge would produce: |
| 17 | +
|
| 18 | + python scripts/step7/mergegate.py --repo . --commit <sha> |
| 19 | +
|
| 20 | +Exit 0 only when every predicate holds. Any failure means the frozen contract |
| 21 | +would become reachable from a tree that cannot satisfy it, and the merge is |
| 22 | +refused however mergeable the forge believes it to be. |
| 23 | +
|
| 24 | +The digest is not hard-coded here: it is read out of the frozen T0 and then |
| 25 | +RECOMPUTED from the instrument sources in the target tree, by the frozen |
| 26 | +formula, without importing the instrument. A gate that trusted a constant in |
| 27 | +its own source would be checking itself. |
| 28 | +""" |
| 29 | + |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +import argparse |
| 33 | + |
| 34 | +import json |
| 35 | +import re |
| 36 | +import subprocess |
| 37 | +import sys |
| 38 | +from pathlib import Path |
| 39 | + |
| 40 | +import execbinding as eb # the single implementation of the harness-digest formula |
| 41 | + |
| 42 | +T0_PATH = "docs/notes/p022-263-t0-protocol-freeze.md" |
| 43 | +STEP7_NOTE = "docs/notes/p022-263a-step7-environment-capture.md" |
| 44 | +BINDING_ARTIFACTS = ( |
| 45 | + "docs/evidence/calibration/p022-263a-policy-freeze.json", |
| 46 | + "docs/evidence/calibration/p022-263a-design-constants.json", |
| 47 | + "docs/evidence/calibration/p022-263a-training-preregistration.json", |
| 48 | +) |
| 49 | +STEP7_TOOLS = ("scripts/step7/envcapture.py", "scripts/step7/hostqual.py", |
| 50 | + "scripts/step7/execbinding.py") |
| 51 | +DIGEST_RE = re.compile(r"measurement_harness_digest\s*\n?\s*([0-9a-f]{64})") |
| 52 | + |
| 53 | + |
| 54 | +def blob(repo: Path, commit: str, path: str) -> bytes | None: |
| 55 | + proc = subprocess.run(["git", "-C", str(repo), "cat-file", "blob", f"{commit}:{path}"], |
| 56 | + capture_output=True, check=False) |
| 57 | + return proc.stdout if proc.returncode == 0 else None |
| 58 | + |
| 59 | + |
| 60 | +def check(name: str, ok: bool, detail: str) -> dict[str, object]: |
| 61 | + return {"check": name, "result": "pass" if ok else "fail", "detail": detail} |
| 62 | + |
| 63 | + |
| 64 | +def gate(repo: Path, commit: str) -> list[dict[str, object]]: |
| 65 | + results: list[dict[str, object]] = [] |
| 66 | + |
| 67 | + raw_t0 = blob(repo, commit, T0_PATH) |
| 68 | + if raw_t0 is None: |
| 69 | + return [check("t0_present", False, f"{T0_PATH} does not exist at {commit}")] |
| 70 | + text = raw_t0.decode("utf-8", "replace") |
| 71 | + frozen = re.search(r"^\s*(NOT_FROZEN|FROZEN)\.?\s*$", text, re.MULTILINE) |
| 72 | + flag = re.search(r"^\s*collection_authorized:\s*(true|false)\s*$", text, re.MULTILINE) |
| 73 | + declared = frozen.group(1) if frozen else "<no status line>" |
| 74 | + authorized = (flag.group(1) == "true") if flag else None |
| 75 | + results.append(check( |
| 76 | + "t0_frozen_and_authorized", declared == "FROZEN" and authorized is True, |
| 77 | + f"T0 declares {declared}, collection_authorized={json.dumps(authorized)}")) |
| 78 | + |
| 79 | + named = DIGEST_RE.search(text) |
| 80 | + results.append(check("t0_names_a_digest", named is not None, |
| 81 | + f"T0 names {named.group(1)[:12] if named else '<none>'} as the accepted " |
| 82 | + "harness identity")) |
| 83 | + if named is None: |
| 84 | + return results |
| 85 | + expected = named.group(1) |
| 86 | + |
| 87 | + # Recomputed from the target tree's own sources, by the frozen formula. |
| 88 | + live = eb.harness_digest_at(repo, commit) |
| 89 | + results.append(check( |
| 90 | + "instrument_matches_t0", live == expected, |
| 91 | + f"the instrument at {commit[:12]} hashes to {live[:12] if live else '<unreadable>'}; " |
| 92 | + f"T0 names {expected[:12]}. The frozen contract would otherwise point at an identity " |
| 93 | + "this tree does not contain")) |
| 94 | + |
| 95 | + rebound = [] |
| 96 | + for path in BINDING_ARTIFACTS: |
| 97 | + raw = blob(repo, commit, path) |
| 98 | + if raw is None: |
| 99 | + rebound.append(f"{Path(path).name}: absent") |
| 100 | + elif expected not in raw.decode("utf-8", "replace"): |
| 101 | + rebound.append(f"{Path(path).name}: still bound to an older digest") |
| 102 | + results.append(check( |
| 103 | + "steps_4_5_6_rebound", not rebound, |
| 104 | + "; ".join(rebound) if rebound else |
| 105 | + f"the policy freeze, the design constants and the training preregistration all bind " |
| 106 | + f"{expected[:12]}")) |
| 107 | + |
| 108 | + missing_tools = [t for t in STEP7_TOOLS if blob(repo, commit, t) is None] |
| 109 | + hostqual = (blob(repo, commit, "scripts/step7/hostqual.py") or b"").decode("utf-8", "replace") |
| 110 | + enforces_link = "CAMPAIGN_LINK_SCHEMA" in hostqual and "check_campaign_link" in hostqual |
| 111 | + enforces_authority = "collection_authorized is" in hostqual or ( |
| 112 | + "authorized is not True" in hostqual) |
| 113 | + results.append(check( |
| 114 | + "step7_machinery_present", not missing_tools and enforces_link and enforces_authority, |
| 115 | + "; ".join(filter(None, [ |
| 116 | + f"missing: {missing_tools}" if missing_tools else "", |
| 117 | + "" if enforces_link else "hostqual does not enforce the campaign link", |
| 118 | + "" if enforces_authority else "hostqual does not enforce the authority state", |
| 119 | + ])) or "capture, qualification and binding tools are present, and the campaign link and " |
| 120 | + "authority state are enforced")) |
| 121 | + |
| 122 | + note = (blob(repo, commit, STEP7_NOTE) or b"").decode("utf-8", "replace") |
| 123 | + revoked = "AUTOMATIC AUTHORISATION OF THE FIRST STEP-7 COLLECTION IS REVOKED" in note |
| 124 | + lingering = "SINGLE STEP-7 COLLECTION AUTHORISED automatically" in note |
| 125 | + results.append(check( |
| 126 | + "t0_zero_prerequisite", revoked and not lingering, |
| 127 | + "the step-7 status block revokes the automatic collection authority" |
| 128 | + if revoked and not lingering else |
| 129 | + "the step-7 note still authorises the first collection automatically, so hosts plus a " |
| 130 | + "binding would again be enough to start a clock")) |
| 131 | + return results |
| 132 | + |
| 133 | + |
| 134 | +def main(argv: list[str] | None = None) -> int: |
| 135 | + parser = argparse.ArgumentParser(description=__doc__) |
| 136 | + parser.add_argument("--repo", type=Path, default=Path(__file__).resolve().parents[2]) |
| 137 | + parser.add_argument("--commit", default="HEAD") |
| 138 | + parser.add_argument("--json", action="store_true") |
| 139 | + args = parser.parse_args(argv) |
| 140 | + |
| 141 | + results = gate(args.repo, args.commit) |
| 142 | + failed = [r for r in results if r["result"] != "pass"] |
| 143 | + if args.json: |
| 144 | + print(json.dumps({"commit": args.commit, "checks": results, |
| 145 | + "merge_allowed": not failed}, indent=2)) |
| 146 | + else: |
| 147 | + for r in results: |
| 148 | + print(f"{'ok ' if r['result'] == 'pass' else 'FAIL'} [{r['check']}] {r['detail']}") |
| 149 | + print() |
| 150 | + print("merge gate: allowed" if not failed else |
| 151 | + f"merge gate: REFUSED, {len(failed)} predicate(s) unsatisfied — the frozen " |
| 152 | + "contract must not become reachable from this tree") |
| 153 | + return 1 if failed else 0 |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + sys.exit(main()) |
0 commit comments