diff --git a/research/selected-dependency-heads-audit/CLAIM.md b/research/selected-dependency-heads-audit/CLAIM.md new file mode 100644 index 0000000000..29b8d6fe1f --- /dev/null +++ b/research/selected-dependency-heads-audit/CLAIM.md @@ -0,0 +1,32 @@ +# CLAIM (advisory) + +**CLAIM** (supplier-boundary-audit, scope refined) + +Question: do `certificates/selected-result.json` fields +`reviewed_dependency_heads`, `audited_source_head`, `source_prs`, +`previous_kappa`, `status`, `scope` and `reproduce` follow from any +source-bound acceptance check on the selected reproduce path, or can a +well-formed record with false dependency heads still pass +`scripts/verify_selected_result.py` while leaving +certificate/kappa/inventory/supplements unchanged? + +Base / predecessors: main `0b1cccf8`; `research/owner-boundary-audit` +(method pattern); `research/selected-proof-binding` (primary-proof coverage +gap, repaired); `research/ledger-decision-binding` (considered selected +dependency-head consistency but chose a ledger failure mode at an earlier +base); `research/frontier1094-ancestry-binding-seed69` (reuse-mode ancestry +binding; orthogonal). Distinct from discussion #1730 +(certificate/invoice/outer field binding to cold replay). + +Method and domain: (1) mutate only the named metadata fields and re-run +`verify_selected_result.check` against the live checkout; (2) confirm that no +top-level Python file under `scripts/` (`scripts/*.py`) mentions the head +fields; (3) independently recheck the published heads for internal consistency +with `previous_record` and batch1099 integration `reviewed_heads`; (4) ship a +scoped package with positive/negative fixtures and a proposed opt-in guard +(not applied to selected scripts). + +Expected artifact: `research/selected-dependency-heads-audit/`; no κ claim; +selected frontier untouched. + +Claim expires: 2026-10-14T10:11Z UTC. diff --git a/research/selected-dependency-heads-audit/EXPECTED.json b/research/selected-dependency-heads-audit/EXPECTED.json new file mode 100644 index 0000000000..73468b4183 --- /dev/null +++ b/research/selected-dependency-heads-audit/EXPECTED.json @@ -0,0 +1,100 @@ +{ + "status": "PASS_SELECTED_DEPENDENCY_HEADS_AUDIT", + "base_commit": "0b1cccf86e2f5132ed286a26ef0a681a495740ea", + "selected_kappa": "836330098538459/500000000000000000", + "selected_record_sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32", + "checker_sha256": "d7fd929726b326aeeaf0a3996475adec88eee9aac09ae0bebeeaa0bc1fc8f199", + "live_checker_accepts_selected": { + "accepted": true, + "kappa": "836330098538459/500000000000000000", + "error": null, + "label": "live_selected_record" + }, + "live_checker_accepts_metadata_mutants": { + "false_heads": { + "accepted": true, + "kappa": "836330098538459/500000000000000000", + "error": null, + "label": "false_heads" + }, + "false_previous_kappa": { + "accepted": true, + "kappa": "836330098538459/500000000000000000", + "error": null, + "label": "false_previous_kappa" + }, + "false_source_prs": { + "accepted": true, + "kappa": "836330098538459/500000000000000000", + "error": null, + "label": "false_source_prs" + }, + "false_publication": { + "accepted": true, + "kappa": "836330098538459/500000000000000000", + "error": null, + "label": "false_publication" + } + }, + "live_checker_rejects_certificate_digest_mutant": { + "accepted": false, + "error_prefix": "ValueError: Selected certificate bytes changed" + }, + "static_scan": { + "verify_selected_result_record_access_lines": { + "reviewed_dependency_heads": [], + "audited_source_head": [], + "source_prs": [], + "previous_kappa": [], + "status": [], + "scope": [], + "reproduce": [] + }, + "top_level_scripts_py_mentioning_head_fields": [], + "static_scan_scope": "top-level scripts/*.py only; not a recursive repository scan" + }, + "published_consistency": { + "audited_equals_lead_head": true, + "lead_pr": 1094, + "previous_kappa_matches_checkpoint": true, + "heads_subset_of_source_prs": true, + "shared_with_previous_count": 35, + "shared_with_previous_diffs": [], + "shared_with_integration_count": 13, + "shared_with_integration_diffs": [], + "new_heads_since_previous": [ + "987", + "1050", + "1053", + "1055", + "1056", + "1061", + "1063", + "1065", + "1068", + "1074", + "1082", + "1087", + "1094" + ], + "n_reviewed_dependency_heads": 48, + "n_source_prs": 101, + "hex40_heads": true + }, + "proposed_guard": { + "accepts_live": true, + "rejects_mutations": { + "false_heads": true, + "false_previous_kappa": true, + "false_source_prs": true + }, + "mutation_errors": { + "false_heads": "ValueError: audited_source_head differs from reviewed head for source_prs[0]", + "false_previous_kappa": "ValueError: previous_kappa differs from previous_record kappa", + "false_source_prs": "ValueError: audited PR missing from reviewed_dependency_heads: 999999" + } + }, + "severity": "low", + "severity_rationale": "Publication/attribution metadata only; certificate, inventory and supplements remain hash-bound. No kappa change and no counterfeit construction acceptance was demonstrated.", + "scope": "Finite metadata audit of the selected #1094 record and verify_selected_result.py acceptance surface, plus a static scan of top-level scripts/*.py only. Not a full cold replay, Lean proof, repository-wide source scan, or GitHub authenticity theorem." +} diff --git a/research/selected-dependency-heads-audit/README.md b/research/selected-dependency-heads-audit/README.md new file mode 100644 index 0000000000..5d57a464f7 --- /dev/null +++ b/research/selected-dependency-heads-audit/README.md @@ -0,0 +1,130 @@ +# Selected dependency-head metadata is unchecked by the fast selected-record checker + +**Adversarial metadata audit and proposed opt-in guard; no new kappa.** +Task card `supplier-boundary-audit`, scope **refined** to the selected record's +dependency-head and related publication fields. Base +`0b1cccf86e2f5132ed286a26ef0a681a495740ea`. The selected frontier, cold replay +and `scripts/verify_selected_result.py` are **not** modified. + +## Finding + +`scripts/verify_selected_result.py` binds certificate bytes, exact κ, the +selected source inventory and every proof supplement. It does **not** read: + +- `reviewed_dependency_heads` +- `audited_source_head` +- `source_prs` +- `previous_kappa` +- `status` / `scope` / `reproduce` + +Mutating only those fields, while leaving the certificate digest, inventory and +supplements untouched, still yields acceptance. Among top-level Python files +under `scripts/` (`scripts/*.py`), none mention `reviewed_dependency_heads` or +`audited_source_head`. This package does not run `make frontier1094-verify` and +does not claim a repository-wide source scan. + +**Severity: low.** This is a publication/attribution consistency gap, not a +counterfeit construction acceptance. κ, the finite assembly and inventoried +supplier packages remain hash-bound. Reviewers who treat the head table as +machine-checked provenance currently over-read the checker. + +The published #1094 record is nevertheless internally consistent: + +| Check | Result | +| --- | --- | +| `audited_source_head` equals head of `source_prs[0]` (#1094) | yes | +| `previous_kappa` equals `certificates/checkpoints/frontier1042.json` κ | yes | +| All 48 reviewed head keys appear in `source_prs` | yes | +| Shared heads with the previous checkpoint (35) | identical | +| Shared heads with batch1099 integration `reviewed_heads` (13) | identical | + +Independent GitHub PR-head spot checks for #1094, #1087, #1042, #589, #858 and +#987 matched the published digests in the session that authored this package; +those network observations are **not** part of the offline replay. + +## Reproduce + +The audit and guard run with Python 3.10+ (standard library only). The +registered `scripts/research_package.py` runner for this package requires +**Python 3.12**. Prefer the copy/paste recipe below: + +```sh +python3.12 -B research/selected-dependency-heads-audit/verify.py +python3.12 -B research/selected-dependency-heads-audit/guard.py +python3.12 -B scripts/research_package.py --package research/selected-dependency-heads-audit +``` + +Expected final verifier status: `PASS_SELECTED_DEPENDENCY_HEADS_AUDIT`. +The deterministic receipt must equal [EXPECTED.json](EXPECTED.json). +Normal verification writes no files. After an intentional code change, regenerate +with `python3.12 -B research/selected-dependency-heads-audit/verify.py --write`. + +The verifier refuses `-O` (assertions must stay enabled). Positive control: the +live selected record passes `verify_selected_result.check`. Negative control: a +mutated `certificate_sha256` is still rejected. Four metadata mutants are +accepted by the live checker; three head/`previous_kappa`/`source_prs` mutants +are rejected by the proposed [guard.py](guard.py). + +## Proposed guard (not applied) + +[guard.py](guard.py) is an opt-in consistency check. It binds: + +1. `audited_source_head` to the reviewed head of `source_prs[0]`; +2. every reviewed head key to membership in `source_prs` and 40-hex form; +3. `previous_kappa` to the kappa of `previous_record`; +4. shared heads with the previous checkpoint; +5. optionally, shared heads with + `docs/research/community-batch1099-integration.json` `reviewed_heads`. + +It does **not** authenticate GitHub, prove a head is the reviewed mathematical +source, or replace certificate/inventory checks. This package deliberately does +not patch `scripts/verify_selected_result.py`; wiring the guard is a maintainer +decision. + +## Task card, overlap and scope + +Chooser track `adversarial-audit`, nonce `1e43292e32b5524502e2a2fde0d33272`, +card `supplier-boundary-audit`. First working command +`research/owner-boundary-audit/verify.py` passed. Scope refined to one named +current condition: whether selected dependency-head metadata follows from the +actual source-bound selected-record checker. + +Nearest predecessors (credit retained; this package adds a different object): + +- [owner-boundary-audit](../owner-boundary-audit/README.md) — method pattern + (timing-only omission vs full controller); +- [selected-proof-binding](../selected-proof-binding/README.md) — primary-proof + coverage gap in the same checker family (repaired upstream); +- [ledger-decision-binding](../ledger-decision-binding/README.md) — considered + selected dependency-head consistency at an earlier base, then audited a + ledger failure mode instead; +- [frontier1094-ancestry-binding-seed69](../frontier1094-ancestry-binding-seed69/README.md) + — reuse-mode ancestry binding; orthogonal to this metadata surface; +- Discussion/claim #1730 — certificate/invoice/outer binding to the cold replay; + distinct fields and path. + +Advisory claim text: [CLAIM.md](CLAIM.md). + +## Open next questions + +1. **Should `verify_selected_result.py` grow an optional or mandatory dependency-head + consistency clause?** It matters because reviewers cite the head table as + provenance. First step: decide whether previous-checkpoint agreement and + `source_prs[0]` binding are enough, or whether integration `reviewed_heads` + must also be named by the selected record. Blocked by: maintainer product + choice; this package only supplies the mutation evidence and a draft guard. + +2. **Should `status` / `scope` / `reproduce` gain digest pins or schema checks?** + They are also unchecked, but have no local oracle in this package beyond + non-emptiness. First step: inventory which publication fields are machine + cited by downstream tooling. Blocked by: no current consumer identified among + top-level `scripts/*.py` during this session. + +## Credit and limits + +Prepared by Wentao Li ([VictorLiwentao](https://github.com/VictorLiwentao), +[@Is__Victor](https://x.com/Is__Victor)) with substantial AI assistance. +Not independent human peer review. Apache-2.0. Predecessor authors above retain +credit for their packages and reviews. Inherited all-size/compiler, hosting, +precision/tape, analytic and bit H1–H5 obligations are untouched. This is not a +formal multiplication theorem. Selected frontier left unchanged. diff --git a/research/selected-dependency-heads-audit/SOURCES.json b/research/selected-dependency-heads-audit/SOURCES.json new file mode 100644 index 0000000000..e00266a85c --- /dev/null +++ b/research/selected-dependency-heads-audit/SOURCES.json @@ -0,0 +1,36 @@ +{ + "schema_version": 1, + "base_commit": "0b1cccf86e2f5132ed286a26ef0a681a495740ea", + "files": [ + { + "path": "certificates/checkpoints/frontier1042.json", + "usage": "input", + "git_blob": "f451ced6626a1f03aba6915ce23a136b01ebe51d", + "sha256": "207d1f49f113c585024b9d3c17eadbb8b1be17c1321e3d190aabe2f18ce1c5b7" + }, + { + "path": "certificates/selected-result.json", + "usage": "input", + "git_blob": "ae6a99f28aa468649eb66a361e764d769363bf11", + "sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "06c96f5579bb67ed1b86d334e78fbed7da6945f1", + "sha256": "6686ad58f1fbdbc87134a78901b60fd53a20d3984355e91defbbebc9e9fa8075" + }, + { + "path": "docs/research/community-batch1099-integration.json", + "usage": "input", + "git_blob": "9d60e86c0e494d17bec5e28eda41b275c9ede64e", + "sha256": "684c50d1ac69863e1fcc0a34bf76c54bac576f6bbde79b3c67d8432a495c8c1c" + }, + { + "path": "scripts/verify_selected_result.py", + "usage": "input", + "git_blob": "c755b76e0d44ba4917aa94440ebe54daefce5b2a", + "sha256": "d7fd929726b326aeeaf0a3996475adec88eee9aac09ae0bebeeaa0bc1fc8f199" + } + ] +} diff --git a/research/selected-dependency-heads-audit/TASK.json b/research/selected-dependency-heads-audit/TASK.json new file mode 100644 index 0000000000..986bb6ba02 --- /dev/null +++ b/research/selected-dependency-heads-audit/TASK.json @@ -0,0 +1,69 @@ +{ + "schema_version": 1, + "task_id": "supplier-boundary-audit", + "priority_revision": 3, + "base_commit": "0b1cccf86e2f5132ed286a26ef0a681a495740ea", + "chooser_seed": "1e43292e32b5524502e2a2fde0d33272", + "original_card": { + "id": "supplier-boundary-audit", + "question": "Does one selected supplier obligation follow from its actual source-bound checks?", + "starting_points": [ + "certificates/selected-result.json", + "research/owner-boundary-audit/README.md" + ], + "methods": [ + "Trace one hypothesis through the source, checker and complete caller.", + "Construct one semantically invalid but superficially well-formed fixture and test rejection." + ], + "acceptance_gate": "Pin the current sources and one obligation; the historical owner audit is an example, not evidence of a current bug.", + "first_step": { + "command": [ + "python3", + "-B", + "research/owner-boundary-audit/verify.py" + ], + "expected": "PASS_PR626_NEXT_TOUCH_BOUNDARY_AUDIT", + "inspect": "Identify why the timing-only omission passes but the full production controller still rejects it.", + "scope": "Positive control / source-bound orientation only; this is not a new construction or full cold replay." + }, + "first_experiment": "Choose one current supplier obligation and trace whether its acceptance depends on an independent check.", + "bottleneck_link": "Closing a current acceptance gap or proof obligation strengthens reliability even without changing kappa.", + "allowed_variations": [ + "Audit one named current condition using a new semantic counterexample." + ], + "scope_change_triggers": [ + "Reporting the historical timing-only diagnostic as a new current acceptance bug." + ], + "overlap_terms": [ + "owner boundary", + "bridge omission", + "hosting" + ] + }, + "scope_status": "refined", + "actual_scope": "Does acceptance of certificates/selected-result.json by scripts/verify_selected_result.py depend on an independent check of reviewed_dependency_heads, audited_source_head, source_prs and previous_kappa, or can false dependency-head metadata still pass while certificate/inventory/supplements stay intact?", + "scope_change_reason": "The catalogue card asks for one current supplier obligation traced through source-bound checks. The named obligation here is the selected record's dependency-head provenance table (and closely related publication fields), which reviewers treat as checked metadata. The historical owner-boundary timing omission is used only as the method pattern, not re-reported as a current bug.", + "bottleneck_link": "Side result on acceptance reliability: closing a provenance-metadata gap strengthens trust in the selected record without changing the binding complex/bit budgets or kappa.", + "budget_minutes": 60, + "stop_rule": "Finish the mutation audit of verify_selected_result on the named fields, the static scripts/ scan, the published consistency checks against frontier1042 and batch1099 integration reviewed_heads, and an opt-in proposed guard with positive/negative fixtures. Do not patch selected scripts or run the full cold frontier1094 replay.", + "outcome": "completed", + "evidence": "PASS_SELECTED_DEPENDENCY_HEADS_AUDIT via research/selected-dependency-heads-audit/verify.py; EXPECTED.json; proposed guard.py rejects false heads/previous_kappa/source_prs mutants that the live checker accepts.", + "author": "Wentao Li", + "github": "VictorLiwentao", + "email": "wentaol4@illinois.edu", + "ai_assistance": "Substantial AI assistance for orientation, overlap search, task selection, audit design, verifier/guard implementation, package drafting and verification. Not independent human peer review.", + "open_questions": [ + { + "question": "Should verify_selected_result.py adopt an optional or mandatory dependency-head consistency clause along the lines of guard.py?", + "why_it_matters": "Reviewers currently may treat reviewed_dependency_heads as machine-checked provenance even though the fast selected-record path ignores them.", + "first_step": "Decide whether previous-checkpoint agreement plus source_prs[0] binding is enough, or whether integration reviewed_heads must also be named by the selected record.", + "blocked_by": "Maintainer product choice; this package only supplies mutation evidence and a draft guard." + }, + { + "question": "Should status/scope/reproduce gain digest pins or schema checks?", + "why_it_matters": "Those publication fields are likewise unchecked by verify_selected_result.py, but this package found no top-level scripts/*.py consumer that reads them for acceptance.", + "first_step": "Inventory downstream tooling that cites those fields before adding pins.", + "blocked_by": "No current top-level scripts/*.py acceptance consumer identified in this session." + } + ] +} diff --git a/research/selected-dependency-heads-audit/VERIFY.json b/research/selected-dependency-heads-audit/VERIFY.json new file mode 100644 index 0000000000..8094d65475 --- /dev/null +++ b/research/selected-dependency-heads-audit/VERIFY.json @@ -0,0 +1,32 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "certificates/selected-result.json", + "certificates/checkpoints/frontier1042.json", + "docs/research/community-batch1099-integration.json", + "scripts/verify_selected_result.py" + ], + "commands": [ + { + "argv": [ + "{python}", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Pins listed sources; confirms live verify_selected_result accepts the selected record and four metadata mutants while rejecting a certificate_sha256 mutant; statically confirms top-level scripts/*.py do not mention dependency-head fields; checks published consistency against frontier1042 and batch1099 integration reviewed_heads; confirms proposed guard.py accepts the live record and rejects three head/kappa/source_prs mutants; compares the full receipt to EXPECTED.json." + }, + { + "argv": [ + "{python}", + "-B", + "{package}/guard.py" + ], + "timeout_seconds": 60, + "checks": "Opt-in proposed guard accepts the live selected record against previous_record and batch1099 integration reviewed_heads." + } + ] +} diff --git a/research/selected-dependency-heads-audit/guard.py b/research/selected-dependency-heads-audit/guard.py new file mode 100644 index 0000000000..329f14c7d5 --- /dev/null +++ b/research/selected-dependency-heads-audit/guard.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 +"""Proposed opt-in binding for selected-record dependency metadata. + +This is a supplemental consistency check. It is not wired into +``scripts/verify_selected_result.py`` or the selected cold replay. It does not +authenticate GitHub, prove that a head is the reviewed mathematical source, or +replace certificate/inventory/supplement checks. + +Wentao Li (@VictorLiwentao) with substantial AI assistance. Apache-2.0. +""" +from __future__ import annotations + +from fractions import Fraction +from hashlib import sha256 +from pathlib import Path +import json +import re +import sys + +HEX40 = re.compile(r"^[0-9a-f]{40}$") + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def load(path: Path): + return json.loads(path.read_text(encoding="utf-8")) + + +def check_record(record: dict, root: Path, *, integration: dict | None = None): + """Reject obviously unbound or internally inconsistent dependency metadata. + + Checked: + - ``audited_source_head`` is a 40-hex digest and equals the head for + ``source_prs[0]`` in ``reviewed_dependency_heads``; + - every reviewed dependency key is an integer PR present in ``source_prs``; + - every reviewed head is a 40-hex digest; + - ``previous_kappa`` equals the kappa of ``previous_record``; + - shared keys with ``previous_record.reviewed_dependency_heads`` agree; + - when ``integration`` is supplied, shared keys with its ``reviewed_heads`` + agree. + + Not checked: live GitHub state, full Git object presence, mathematical + content of any supplier, or the selected certificate bytes (those remain + the job of ``verify_selected_result``). + """ + root = root.resolve() + heads = record.get("reviewed_dependency_heads") + require(isinstance(heads, dict) and heads, "Missing reviewed_dependency_heads") + source_prs = record.get("source_prs") + require(isinstance(source_prs, list) and source_prs, "Missing source_prs") + require(all(isinstance(n, int) and n > 0 for n in source_prs), "source_prs must be positive ints") + require(len(source_prs) == len(set(source_prs)), "source_prs must be unique") + + audited = record.get("audited_source_head") + require(isinstance(audited, str) and HEX40.fullmatch(audited), "audited_source_head must be 40-hex") + lead = source_prs[0] + lead_key = str(lead) + require(lead_key in heads, "audited PR missing from reviewed_dependency_heads: " + lead_key) + require(heads[lead_key] == audited, "audited_source_head differs from reviewed head for source_prs[0]") + + for key, digest in heads.items(): + require(isinstance(key, str) and key.isdigit(), "reviewed_dependency_heads keys must be decimal strings") + number = int(key) + require(number in source_prs, "reviewed head PR not listed in source_prs: " + key) + require(isinstance(digest, str) and HEX40.fullmatch(digest), "invalid reviewed head: " + key) + + previous_path = root / record["previous_record"] + require(previous_path.is_file(), "previous_record missing: " + record["previous_record"]) + previous = load(previous_path) + require(Fraction(record["previous_kappa"]) == Fraction(previous["kappa"]), + "previous_kappa differs from previous_record kappa") + + previous_heads = previous.get("reviewed_dependency_heads") or {} + for key, digest in previous_heads.items(): + if key in heads: + require(heads[key] == digest, "selected head disagrees with previous_record: " + key) + + for key in ("status", "scope", "reproduce", "review", "validation"): + require(isinstance(record.get(key), str) and record[key].strip(), "missing metadata field: " + key) + + if integration is not None: + reviewed = integration.get("reviewed_heads") or {} + for key, digest in reviewed.items(): + if key in heads: + require(heads[key] == digest, "selected head disagrees with integration reviewed_heads: " + key) + + return { + "lead_pr": lead, + "audited_source_head": audited, + "reviewed_dependency_heads": len(heads), + "source_prs": len(source_prs), + "previous_record": record["previous_record"], + "previous_kappa": record["previous_kappa"], + "shared_with_previous": sum(1 for k in previous_heads if k in heads), + "shared_with_integration": ( + sum(1 for k in (integration.get("reviewed_heads") or {}) if k in heads) + if integration is not None else None + ), + } + + +def main(argv=None): + argv = list(sys.argv[1:] if argv is None else argv) + root = Path(__file__).resolve().parents[2] + record_path = root / "certificates/selected-result.json" + integration_path = root / "docs/research/community-batch1099-integration.json" + if "--root" in argv: + i = argv.index("--root") + root = Path(argv[i + 1]).resolve() + record_path = root / "certificates/selected-result.json" + integration_path = root / "docs/research/community-batch1099-integration.json" + record = load(record_path) + integration = load(integration_path) if integration_path.is_file() else None + summary = check_record(record, root, integration=integration) + print("PASS_SELECTED_DEPENDENCY_HEADS_GUARD " + json.dumps(summary, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/research/selected-dependency-heads-audit/verify.py b/research/selected-dependency-heads-audit/verify.py new file mode 100644 index 0000000000..c787716e9a --- /dev/null +++ b/research/selected-dependency-heads-audit/verify.py @@ -0,0 +1,366 @@ +#!/usr/bin/env python3 +"""Audit: selected dependency-head metadata is unchecked by verify_selected_result. + +Finding (scoped; no new kappa): +``scripts/verify_selected_result.py`` binds certificate bytes, exact kappa, +source inventory and proof supplements. It does **not** read +``reviewed_dependency_heads``, ``audited_source_head``, ``source_prs``, +``previous_kappa``, ``status``, ``scope`` or ``reproduce``. Mutating only those +fields leaves the checker accepting the record. Among top-level Python files +under ``scripts/`` (``scripts/*.py``), none mention the head fields. The +published #1094 record remains internally consistent with its previous +checkpoint and with the batch1099 integration ``reviewed_heads`` on their +shared keys. + +This package does not change the selected frontier or selected scripts, and +does not run a cold frontier replay. ``guard.py`` is a proposed opt-in check +only. + +Wentao Li (@VictorLiwentao) with substantial AI assistance. Apache-2.0. +""" +from __future__ import annotations + +import copy +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import re +import sys + +PACKAGE = Path(__file__).resolve().parent +ROOT = PACKAGE.parents[1] +EXPECTED_PATH = PACKAGE / "EXPECTED.json" + +RECORD_REL = "certificates/selected-result.json" +PREV_REL = "certificates/checkpoints/frontier1042.json" +INTEGRATION_REL = "docs/research/community-batch1099-integration.json" +CHECKER_REL = "scripts/verify_selected_result.py" + +UNCHECKED_FIELDS = ( + "reviewed_dependency_heads", + "audited_source_head", + "source_prs", + "previous_kappa", + "status", + "scope", + "reproduce", +) + + +def require(condition, message): + if not condition: + raise SystemExit("FAIL: " + message) + + +def sha(data: bytes) -> str: + return sha256(data).hexdigest() + + +def load(path: Path): + return json.loads(path.read_text(encoding="utf-8")) + + +def pin_sources(): + sources = load(PACKAGE / "SOURCES.json") + require(sources["base_commit"], "SOURCES.json missing base_commit") + for entry in sources["files"]: + path = ROOT / entry["path"] + require(path.is_file(), "missing pinned source: " + entry["path"]) + digest = sha(path.read_bytes()) + require(digest == entry["sha256"], "pinned source bytes changed: " + entry["path"]) + return sources + + +def load_checker(): + path = ROOT / CHECKER_REL + spec = importlib.util.spec_from_file_location("verify_selected_result_audit", path) + require(spec is not None and spec.loader is not None, "cannot load verify_selected_result") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def accept(module, record, root, label): + try: + kappa = module.check(record, root=root) + return {"accepted": True, "kappa": str(kappa), "error": None, "label": label} + except Exception as exc: # noqa: BLE001 - record exact rejection text + return {"accepted": False, "kappa": None, "error": f"{type(exc).__name__}: {exc}", "label": label} + + +def reject(module, record, root, label): + outcome = accept(module, record, root, label) + require(not outcome["accepted"], "expected rejection for " + label) + return outcome + + +def mutate_heads(record): + mutant = copy.deepcopy(record) + mutant["audited_source_head"] = "0" * 40 + mutant["reviewed_dependency_heads"] = {k: "deadbeef" * 5 for k in mutant["reviewed_dependency_heads"]} + mutant["reviewed_dependency_heads"][str(mutant["source_prs"][0])] = "1" * 40 + return mutant + + +def mutate_previous_kappa(record): + mutant = copy.deepcopy(record) + mutant["previous_kappa"] = "1/2" + return mutant + + +def mutate_source_prs(record): + mutant = copy.deepcopy(record) + mutant["source_prs"] = [999999] + list(mutant["source_prs"][1:]) + return mutant + + +def mutate_publication(record): + mutant = copy.deepcopy(record) + mutant["status"] = "FAKE_STATUS" + mutant["scope"] = "FAKE_SCOPE" + mutant["reproduce"] = "make nonexistent-target" + return mutant + + +def mutate_certificate_digest(record): + """Negative control: the live checker must still reject certificate drift.""" + mutant = copy.deepcopy(record) + mutant["certificate_sha256"] = "0" * 64 + return mutant + + +def static_script_scan(): + """Require that acceptance code does not *read* the metadata fields. + + A docstring may mention the word ``reproduce`` ("reproduce command") without + binding ``record['reproduce']``. Look for subscription / ``.get`` access. + """ + text = (ROOT / CHECKER_REL).read_text(encoding="utf-8") + access_hits = {} + for name in UNCHECKED_FIELDS: + patterns = ( + f"record['{name}']", + f'record["{name}"]', + f"record.get('{name}'", + f'record.get("{name}"', + f"['{name}']", + f'["{name}"]', + ) + # Narrow ``['name']`` / ``["name"]`` to lines that also mention record. + line_hits = [] + for i, line in enumerate(text.splitlines(), 1): + if name not in line: + continue + if any(p in line for p in patterns[:4]) or ( + ("record" in line) and (f"['{name}']" in line or f'["{name}"]' in line) + ): + line_hits.append(i) + access_hits[name] = line_hits + script_hits = [] + for path in sorted((ROOT / "scripts").glob("*.py")): + body = path.read_text(encoding="utf-8", errors="replace") + if "reviewed_dependency_heads" in body or "audited_source_head" in body: + script_hits.append(path.name) + return { + "verify_selected_result_record_access_lines": access_hits, + "top_level_scripts_py_mentioning_head_fields": script_hits, + "static_scan_scope": "top-level scripts/*.py only; not a recursive repository scan", + } + + +def published_consistency(record, previous, integration): + heads = {str(k): v for k, v in record["reviewed_dependency_heads"].items()} + prev_heads = {str(k): v for k, v in previous.get("reviewed_dependency_heads", {}).items()} + integ_heads = {str(k): v for k, v in integration.get("reviewed_heads", {}).items()} + shared_prev = sorted(set(heads) & set(prev_heads), key=int) + shared_integ = sorted(set(heads) & set(integ_heads), key=int) + return { + "audited_equals_lead_head": record["audited_source_head"] == heads[str(record["source_prs"][0])], + "lead_pr": record["source_prs"][0], + "previous_kappa_matches_checkpoint": record["previous_kappa"] == previous["kappa"], + "heads_subset_of_source_prs": sorted( + {int(k) for k in heads} - set(record["source_prs"]) + ) + == [], + "shared_with_previous_count": len(shared_prev), + "shared_with_previous_diffs": [k for k in shared_prev if heads[k] != prev_heads[k]], + "shared_with_integration_count": len(shared_integ), + "shared_with_integration_diffs": [k for k in shared_integ if heads[k] != integ_heads[k]], + "new_heads_since_previous": sorted(set(heads) - set(prev_heads), key=int), + "n_reviewed_dependency_heads": len(heads), + "n_source_prs": len(record["source_prs"]), + "hex40_heads": all(re.fullmatch(r"[0-9a-f]{40}", h) for h in heads.values()), + } + + +def proposed_guard_outcomes(record, root, integration): + sys.path.insert(0, str(PACKAGE)) + import guard # noqa: WPS433 - package-local proposed check + + live = {"accepted": True, "error": None} + try: + guard.check_record(record, root, integration=integration) + except Exception as exc: # noqa: BLE001 + live = {"accepted": False, "error": f"{type(exc).__name__}: {exc}"} + + outcomes = {"live": live, "mutations": {}} + # Publication prose fields (status/scope/reproduce) have no local digest oracle + # in this package; the gap is documented via the live checker mutants only. + for label, builder in ( + ("false_heads", mutate_heads), + ("false_previous_kappa", mutate_previous_kappa), + ("false_source_prs", mutate_source_prs), + ): + try: + guard.check_record(builder(record), root, integration=integration) + outcomes["mutations"][label] = {"accepted": True, "error": None} + except Exception as exc: # noqa: BLE001 + outcomes["mutations"][label] = { + "accepted": False, + "error": f"{type(exc).__name__}: {exc}", + } + return outcomes + + +def build_receipt(write=False): + sources = pin_sources() + record = load(ROOT / RECORD_REL) + previous = load(ROOT / PREV_REL) + integration = load(ROOT / INTEGRATION_REL) + module = load_checker() + + require(not sys.flags.optimize, "Assertions must remain enabled") + + live = accept(module, record, ROOT, "live_selected_record") + require(live["accepted"], "live selected record must pass verify_selected_result") + + mutants = { + "false_heads": accept(module, mutate_heads(record), ROOT, "false_heads"), + "false_previous_kappa": accept(module, mutate_previous_kappa(record), ROOT, "false_previous_kappa"), + "false_source_prs": accept(module, mutate_source_prs(record), ROOT, "false_source_prs"), + "false_publication": accept(module, mutate_publication(record), ROOT, "false_publication"), + } + for label, outcome in mutants.items(): + require(outcome["accepted"], "current checker unexpectedly rejects " + label) + + digest_control = reject(module, mutate_certificate_digest(record), ROOT, "false_certificate_sha256") + + scan = static_script_scan() + require(all(not v for v in scan["verify_selected_result_record_access_lines"].values()), + "verify_selected_result unexpectedly reads an unchecked metadata field") + require(scan["top_level_scripts_py_mentioning_head_fields"] == [], + "top-level scripts/*.py unexpectedly references dependency head fields") + + consistency = published_consistency(record, previous, integration) + require(consistency["audited_equals_lead_head"], "published audited head mismatch") + require(consistency["previous_kappa_matches_checkpoint"], "published previous_kappa mismatch") + require(consistency["heads_subset_of_source_prs"], "published head not in source_prs") + require(consistency["shared_with_previous_diffs"] == [], "published head disagrees with previous_record") + require(consistency["shared_with_integration_diffs"] == [], "published head disagrees with integration") + require(consistency["hex40_heads"], "published head is not 40-hex") + + guard_outcomes = proposed_guard_outcomes(record, ROOT, integration) + require(guard_outcomes["live"]["accepted"], "proposed guard must accept live record") + for label, outcome in guard_outcomes["mutations"].items(): + require(not outcome["accepted"], "proposed guard must reject " + label) + + receipt = { + "status": "PASS_SELECTED_DEPENDENCY_HEADS_AUDIT", + "base_commit": sources["base_commit"], + "selected_kappa": record["kappa"], + "selected_record_sha256": sha((ROOT / RECORD_REL).read_bytes()), + "checker_sha256": sha((ROOT / CHECKER_REL).read_bytes()), + "live_checker_accepts_selected": live, + "live_checker_accepts_metadata_mutants": mutants, + "live_checker_rejects_certificate_digest_mutant": { + "accepted": digest_control["accepted"], + "error_prefix": (digest_control["error"] or "")[:80], + }, + "static_scan": scan, + "published_consistency": consistency, + "proposed_guard": { + "accepts_live": guard_outcomes["live"]["accepted"], + "rejects_mutations": { + label: (not outcome["accepted"]) for label, outcome in guard_outcomes["mutations"].items() + }, + "mutation_errors": { + label: outcome["error"] for label, outcome in guard_outcomes["mutations"].items() + }, + }, + "severity": "low", + "severity_rationale": ( + "Publication/attribution metadata only; certificate, inventory and " + "supplements remain hash-bound. No kappa change and no counterfeit " + "construction acceptance was demonstrated." + ), + "scope": ( + "Finite metadata audit of the selected #1094 record and " + "verify_selected_result.py acceptance surface, plus a static scan " + "of top-level scripts/*.py only. Not a full cold replay, Lean " + "proof, repository-wide source scan, or GitHub authenticity theorem." + ), + } + + if write: + EXPECTED_PATH.write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8") + return receipt + + +def reject_duplicate_json_keys() -> None: + """Fail if any package JSON object repeats a key (stdlib json keeps the last).""" + + def check_obj(raw: str, label: str) -> None: + def pairs_hook(pairs): + seen = set() + out = {} + for k, v in pairs: + require(k not in seen, f"duplicate JSON key {k!r} in {label}") + seen.add(k) + out[k] = v + return out + + json.loads(raw, object_pairs_hook=pairs_hook) + + for path in sorted(PACKAGE.glob("*.json")): + check_obj(path.read_text(encoding="utf-8"), path.name) + + +def main(): + write = "--write" in sys.argv + reject_duplicate_json_keys() + before = EXPECTED_PATH.read_bytes() if EXPECTED_PATH.is_file() else None + receipt = build_receipt(write=write) + expected = load(EXPECTED_PATH) + # Compare without depending on exact exception wording for the digest control + # beyond a stable prefix already stored. + require(receipt == expected, "receipt differs from EXPECTED.json; pass --write after intentional change") + if not write: + after = EXPECTED_PATH.read_bytes() + require(before == after, "read-only verify mutated EXPECTED.json") + forged = dict(expected) + forged = json.loads(json.dumps(forged)) + forged["severity"] = "critical" + require(receipt != forged, "corruption control: severity forge must differ from recomputed receipt") + print(receipt["status"]) + print( + "live_checker_accepts_metadata_mutants=" + + ",".join(sorted(receipt["live_checker_accepts_metadata_mutants"])) + ) + print( + "proposed_guard_rejects=" + + ",".join( + k for k, ok in sorted(receipt["proposed_guard"]["rejects_mutations"].items()) if ok + ) + ) + print( + "published_heads={n_heads}; shared_prev={sp}; shared_integ={si}".format( + n_heads=receipt["published_consistency"]["n_reviewed_dependency_heads"], + sp=receipt["published_consistency"]["shared_with_previous_count"], + si=receipt["published_consistency"]["shared_with_integration_count"], + ) + ) + + +if __name__ == "__main__": + main()