From 82f4de13f0e41253740d880f26fdc63fda772323 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:35:55 -0700 Subject: [PATCH 01/38] test(formal): expose declaration-admission blind spots --- formal/tests/test_declaration_admission.py | 150 +++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 formal/tests/test_declaration_admission.py diff --git a/formal/tests/test_declaration_admission.py b/formal/tests/test_declaration_admission.py new file mode 100644 index 000000000..0b86d6bb8 --- /dev/null +++ b/formal/tests/test_declaration_admission.py @@ -0,0 +1,150 @@ +"""Regression tests for RF-GATE-01 declaration admission. + +These tests exercise the real source-only gate in a copied formal tree. They do +not run Lean; hosted execute-time controls remain separate. +""" +import hashlib +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +FORMAL = Path(__file__).resolve().parents[1] +MODULE = "ResearchFormalCoreR1/AlgebraV2.lean" +PREFIX = "ResearchFormalCoreR1." + + +def sha256(path: Path) -> str: + return hashlib.sha256(path.read_bytes()).hexdigest() + + +class DeclarationAdmissionRegressionTests(unittest.TestCase): + def fixture(self): + td = tempfile.TemporaryDirectory() + root = Path(td.name) / "formal" + shutil.copytree( + FORMAL, + root, + ignore=shutil.ignore_patterns(".lake", "__pycache__", "*.pyc"), + ) + return td, root + + def write_module(self, root: Path, extra: str): + p = root / MODULE + text = p.read_text() + marker = "\nend ResearchFormalCoreR1\n" + self.assertIn(marker, text) + p.write_text(text.replace(marker, "\n" + extra + "\n" + marker, 1)) + mpath = root / "manifest.json" + manifest = json.loads(mpath.read_text()) + manifest["files"][MODULE] = sha256(p) + mpath.write_text(json.dumps(manifest, indent=2) + "\n") + + def run_gate(self, root: Path): + return subprocess.run( + [sys.executable, "gate.py"], + cwd=root, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + timeout=60, + ) + + def assert_rejected(self, extra: str): + td, root = self.fixture() + try: + self.write_module(root, extra) + result = self.run_gate(root) + self.assertNotEqual( + result.returncode, + 0, + "source gate admitted an unregistered Lean declaration:\n" + + extra + + "\nstdout:\n" + + result.stdout + + "\nstderr:\n" + + result.stderr, + ) + finally: + td.cleanup() + + def test_hidden_declaration_forms_are_rejected(self): + cases = ( + " theorem rf_gate_indented : False := by sorry", + "private theorem rf_gate_private : False := by sorry", + "@[simp] theorem rf_gate_attributed : False := by sorry", + "def rf_gate_unused : False := by sorry", + "opaque rf_gate_opaque : False", + "axiom rf_gate_axiom : False", + ) + for source in cases: + with self.subTest(source=source): + self.assert_rejected(source) + + def test_comment_text_is_not_inventoried_as_a_declaration(self): + td, root = self.fixture() + try: + self.write_module( + root, + "/-\ntheorem rf_gate_comment_ghost : False := by sorry\n-/", + ) + result = self.run_gate(root) + self.assertEqual( + result.returncode, + 0, + "comment text was treated as a package declaration:\n" + + result.stdout + + result.stderr, + ) + finally: + td.cleanup() + + def test_duplicate_target_list_is_rejected_source_side(self): + td, root = self.fixture() + try: + extra = "theorem ec005_fold_gap (s : ℝ) : True := by trivial" + self.write_module(root, extra) + mpath = root / "manifest.json" + manifest = json.loads(mpath.read_text()) + name = PREFIX + "ec005_fold_gap" + insert_at = manifest["targets"].index(PREFIX + "ec014_contact_power") + 1 + manifest["targets"].insert(insert_at, name) + mpath.write_text(json.dumps(manifest, indent=2) + "\n") + result = self.run_gate(root) + self.assertNotEqual( + result.returncode, + 0, + "duplicate source/manifest target escaped source-side validation", + ) + finally: + td.cleanup() + + def test_nested_namespace_cannot_be_misprefixed(self): + td, root = self.fixture() + try: + extra = ( + "namespace Nested\n" + "theorem rf_gate_nested : True := by trivial\n" + "end Nested" + ) + self.write_module(root, extra) + mpath = root / "manifest.json" + manifest = json.loads(mpath.read_text()) + insert_at = manifest["targets"].index(PREFIX + "ec014_contact_power") + 1 + manifest["targets"].insert(insert_at, PREFIX + "rf_gate_nested") + mpath.write_text(json.dumps(manifest, indent=2) + "\n") + result = self.run_gate(root) + self.assertNotEqual( + result.returncode, + 0, + "nested declaration escaped with a fabricated flat namespace name", + ) + finally: + td.cleanup() + + +if __name__ == "__main__": + unittest.main() From 73c30c082e4883e100e25923ce7027595c96a6d2 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:44:58 -0700 Subject: [PATCH 02/38] formal: harden source declaration admission --- formal/gate.py | 254 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) diff --git a/formal/gate.py b/formal/gate.py index f946c38aa..a1159501b 100644 --- a/formal/gate.py +++ b/formal/gate.py @@ -15,6 +15,8 @@ AXIOM = re.compile(r"'(" + NAME + r")' (?:depends on axioms: \[([^\]]*)\]|does not depend on any axioms)") SHA256 = re.compile(r'[0-9a-f]{64}') COMMIT = re.compile(r'[0-9a-f]{40}') +DECL_LINE = re.compile(r'^(?:noncomputable\\s+)?(def|theorem|lemma)\\s+(' + NAME + r')\\b') +UNSUPPORTED_COMMANDS = ('private ', 'protected ', '@[', 'opaque ', 'axiom ', 'abbrev ', 'structure ', 'class ', 'instance ', 'local instance ', 'inductive ', 'mutual', 'syntax ', 'macro ', 'elab ', 'initialize ', 'example ') ROOT = Path(__file__).resolve().parent def sha(data): @@ -24,6 +26,258 @@ def require(condition, message): if not condition: raise ValueError(message) +def strip_lean_noncode(text): + out=[]; i=0; block=0; string=False + while i < len(text): + if block: + if text.startswith('/-',i): block+=1; out.extend(' '); i+=2 + elif text.startswith('-/',i): block-=1; out.extend(' '); i+=2 + elif text[i]=='\\n': out.append('\\n'); i+=1 + else: out.append(' '); i+=1 + elif string: + if text[i]=='\\\\' and i+1 Date: Mon, 5 Oct 2026 17:45:38 -0700 Subject: [PATCH 03/38] test(formal): repin hardened gate --- formal/tests/test_d2_schur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_d2_schur.py b/formal/tests/test_d2_schur.py index 2801b961b..b756eecb3 100644 --- a/formal/tests/test_d2_schur.py +++ b/formal/tests/test_d2_schur.py @@ -52,7 +52,7 @@ def test_manifest_exact_extension(self): for path in (MODULE, 'D2_SCHUR.md', 'tests/test_d2_schur.py'): self.assertEqual(hashlib.sha256((ROOT/path).read_bytes()).hexdigest(), m['files'][path]) self.assertEqual(m['files']['gate.py'], - '958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68') + '1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92') self.assertEqual(m['files']['ResearchFormalCoreR1/WeightPerturbation.lean'], '50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f') From 369c81d0056b800587fdeee2665395f412919b86 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:45:40 -0700 Subject: [PATCH 04/38] test(formal): repin hardened gate --- formal/tests/test_weight_perturbation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weight_perturbation.py b/formal/tests/test_weight_perturbation.py index d3b6c1cc5..a887346ac 100644 --- a/formal/tests/test_weight_perturbation.py +++ b/formal/tests/test_weight_perturbation.py @@ -65,7 +65,7 @@ def test_predecessor_and_gate_identity_contract(self): pins = { 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', 'ResearchFormalCoreR1/MomentGenerality.lean': 'fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509', - 'gate.py': '958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68', + 'gate.py': '1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From a316fe0f957bfdbcbc4c8444b975ded4a858ed4f Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:45:56 -0700 Subject: [PATCH 05/38] docs(formal): document declaration admission --- formal/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/formal/README.md b/formal/README.md index 47d3ebba4..0fd419f32 100644 --- a/formal/README.md +++ b/formal/README.md @@ -51,3 +51,8 @@ Blueprint declaration links are checked against the target inventory and compile ## D2 Schur and endpoint positivity The additive [D2Schur](D2_SCHUR.md) supplies sixteen real-algebra declarations: exact Schur cancellation, determinant endpoint interpolation, positive conditional-expression bounds, cubic-variance cumulant conversion, and reference/singular controls. The earlier47 Lean proof bytes and the gate remain unchanged. The total inventory is63. Moment realizability, Gaussian/Palm identification and coefficient quadrature are not formalized by these conditional expressions; independent alignment remains pending. + + +## Declaration admission + +[DECLARATION_ADMISSION.md](DECLARATION_ADMISSION.md) records RF-GATE-01 hardening. The theorem target list remains the proof inventory; definitions are now also transitive-axiom audited. From 8c1231b892b7dcc35e300194bceedb1da63d9b9a Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:45:59 -0700 Subject: [PATCH 06/38] docs(formal): remove stale suite count --- formal/LINEAGE_VALIDATION.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/LINEAGE_VALIDATION.md b/formal/LINEAGE_VALIDATION.md index 5a0c5d20e..e96ec4d30 100644 --- a/formal/LINEAGE_VALIDATION.md +++ b/formal/LINEAGE_VALIDATION.md @@ -72,5 +72,5 @@ python3 formal/gate.py --execute Only the focused suite was run in the implementer's partial staging checkout at publication. Full-suite, exact-source and Lean execution claims require their -actual complete-checkout results; the expected main-based suite is 97 tests. +actual complete-checkout results; the complete suite size is not a protocol constant; use the actual current-run count. Local focused tests are not a Lean kernel replay or independent review. From f5c9dff42a3270b4876c77845510777a0c1a7af0 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:46:01 -0700 Subject: [PATCH 07/38] docs(formal): record RF-GATE-01 hardening --- formal/DECLARATION_ADMISSION.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 formal/DECLARATION_ADMISSION.md diff --git a/formal/DECLARATION_ADMISSION.md b/formal/DECLARATION_ADMISSION.md new file mode 100644 index 000000000..453a5c28a --- /dev/null +++ b/formal/DECLARATION_ADMISSION.md @@ -0,0 +1,9 @@ +# Package declaration admission hardening + +Scientific effect: **NONE**. Engineering successor for RF-GATE-01 (Math- issue #310), implemented by OpenAI / GPT-5.6 Sol in `round11-gate-hardening-20261005`. The #309 mathematical declarations are preserved. + +The predecessor used a column-zero theorem/lemma regex. Lean accepts declarations outside it, so declarations could compile outside manifest and axiom coverage. This successor intentionally enforces a conservative grammar rather than claiming to parse arbitrary Lean: one `namespace ResearchFormalCoreR1`; column-zero `theorem`, `lemma`, `def`, or `noncomputable def`; declaration-bearing alternatives and nested namespaces fail closed after comments and strings are removed. + +The manifest records all package declarations, not just theorem targets. Execute-time transitive-axiom auditing covers definitions as well as theorems; the historical target-only `axioms` receipt field is retained and `declaration_axioms` is additive. Lean4.34.1, dependency pins, leanchecker and all five existing negative controls are unchanged. + +Test-first predecessor `82f4de13f0e41253740d880f26fdc63fda772323` / run37394891831 failed with nine intended regression failures before Lean installation. A successor must pass those regressions, complete Python suites in both modes, source identity and the actual pinned Lean/leanchecker/axiom/type workflow. This is engineering evidence, not independent mathematical alignment or reviewer authentication. From 81078fa2641fa69be2b467a49a01dcfb57f2bef8 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:46:44 -0700 Subject: [PATCH 08/38] formal: bind declaration admission controls --- formal/manifest.json | 89 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/formal/manifest.json b/formal/manifest.json index 5aa2c31af..7f2738c66 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -98,14 +98,14 @@ ".gitignore": "b34af236d1df9e39ed4f98fca51702530ce76bdee4e1a69512f6caed933c3374", "COMPATIBILITY.md": "33721aad32f82131a6956a06b58c5bc41f5d28f26efe840a99a4d51ad60cf247", "GLOSSARY.md": "6bdda2ce054915efc26e8db462dbe8fd9d0c1db30e02328857bebc6568e05ce9", - "README.md": "52c4829c23c5caa8142b4c8d7152766b92cb966b1cb8213ff5210e6fa4ef080e", + "README.md": "937b240b9453261cbfea11dbaceb7c59faa7083d807e0146c0fb247474df0e05", "ResearchFormalCoreR1/AlgebraV2.lean": "4480708263c40a2f7f03f12ef7e15f0eb4f8193c2dccbb253921a7ec251863c3", "ResearchFormalCoreR1/ProbabilityCompanionsV2.lean": "338452a0c2ba24d016059354d543cc995b8cb29c9d4a41d98bb93499f5f8cb8f", "ResearchFormalCoreR1.lean": "c55cc116ff3ffbffdb692226e215450a5d2e85b60d27b1b04d529b87bdef38f8", "SCOPE.md": "8fa7e879e23742aba77df8687659ca9dcfd13a19e0add6192229c2f80629b3cb", "blueprint/src/content.tex": "0c976e8e4a5e69a443e93796c8f6cbf91bf73c224448c4256de72a041e04161f", "evidence/initial-build-failure.log": "43393d2e2b3148a2453e3092a1ff04e257dbd4a6dc97eb2853a8bc0451002591", - "gate.py": "958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68", + "gate.py": "1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92", "lake-manifest.json": "d63753befccc21923783a5085148e3ff028d97134ea52f5a08bd87ecf1b0b043", "lakefile.toml": "d629891716c169404d1f0d76492441c8dd66f1e790b1b8b0819e51c8d5059255", "lean-toolchain": "d5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b", @@ -126,13 +126,90 @@ "MOMENT_GENERALITY.md": "5386c763f95ecd0946e0f23a22d1ba90e7956ff1a1bfcada0d454d1b5ace0cf2", "tests/test_moment_generality.py": "cba72a9747670e269bbec414f89fd2e8291dcf331d01a8e1e88ad6c7f2841383", "tests/test_alignment_lineage.py": "c510c6566d7689bebde76843582e9936dfa9e62e783b816b5e4f63d83434b893", - "LINEAGE_VALIDATION.md": "bba9d0a859f1ba45670fe122e4e72ae1094a2226de98aa814d838b0baaf61815", + "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "ResearchFormalCoreR1/WeightPerturbation.lean": "50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f", "WEIGHT_PERTURBATION.md": "0ac969b86f5b2cc4e84b4bb61790c7497164b923dc5f3fc5be99195b75b1b212", - "tests/test_weight_perturbation.py": "e5400fec9d7a0cfeb7669689375184a04e8748fed6c28f58d51cfe1529fbc75b", + "tests/test_weight_perturbation.py": "8a9a90921be35b009ffb188c1be725c409bbd792b442215dbbc125e7f4e2ebe0", "evidence/weight-perturbation-initial-build-failure.log": "2abe4ec4bcfa4de282e7983ffebb225af21308c60b84083a1696bcced31b9c7c", "D2_SCHUR.md": "c25f4add4c92479ae0f6dfaac43fc9be7a71766f050a79acbc2c72acf47ef4d3", "ResearchFormalCoreR1/D2Schur.lean": "d3fb3ed7b4043227f1d5a06e208de9d94eb413c067e9d0f8802388407c9e15b9", - "tests/test_d2_schur.py": "3463a1865809735ee59d0042d2a9049eb4c2cad7beb4f01ca173475c33cb1589" - } + "tests/test_d2_schur.py": "17492a222ca8005465d934710d654c7171d03d8febf0af775bcfdbe35f15f9da", + "DECLARATION_ADMISSION.md": "d0ea2f3875e6f0ad8ff38e57c9e366953f06dcb6bbe275ec0bbb690fb03d7769", + "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7" + }, + "declarations": [ + "ResearchFormalCoreR1.foldPotential", + "ResearchFormalCoreR1.ec005_fold_gap", + "ResearchFormalCoreR1.ec008_factor_expansion", + "ResearchFormalCoreR1.ec010_generic", + "ResearchFormalCoreR1.ec010_transverse", + "ResearchFormalCoreR1.ec011_scalar_cancellation", + "ResearchFormalCoreR1.ec014_contact_power", + "ResearchFormalCoreR1.p02_lm008_r2_cancel", + "ResearchFormalCoreR1.p02_lm008_cross_multiplied", + "ResearchFormalCoreR1.p02_lm008_quotient_bound", + "ResearchFormalCoreR1.p02_lm009_bad_event_threshold", + "ResearchFormalCoreR1.p02_lm009_power40_identity", + "ResearchFormalCoreR1.p02_lm009_r4_le_r3", + "ResearchFormalCoreR1.p02_lm009_palm_r4_to_r3", + "ResearchFormalCoreR1.p02_lm008_integral_cs", + "ResearchFormalCoreR1.p02_lm008_event_cs", + "ResearchFormalCoreR1.p02_lm008_event_numerator", + "ResearchFormalCoreR1.p02_lm008_measure_transfer", + "ResearchFormalCoreR1.p02_lm009_measure_transfer_r8_to_r3", + "ResearchFormalCoreR1.p02_lm008_sqrt_counterexample", + "ResearchFormalCoreR1.p02_lm008_upper_normalizer_counterexample", + "ResearchFormalCoreR1.weightedLaw", + "ResearchFormalCoreR1.weightedLaw_apply", + "ResearchFormalCoreR1.weightedLaw_isProbabilityMeasure", + "ResearchFormalCoreR1.weightedLaw_event_real", + "ResearchFormalCoreR1.weightedLaw_absolutelyContinuous", + "ResearchFormalCoreR1.weightedLaw_congr_ae", + "ResearchFormalCoreR1.weightedLaw_zero", + "ResearchFormalCoreR1.weightedLaw_one", + "ResearchFormalCoreR1.p02_lm008_probability_transfer", + "ResearchFormalCoreR1.p02_lm009_probability_transfer_r8_to_r3", + "ResearchFormalCoreR1.badJetEvent", + "ResearchFormalCoreR1.p02_lm009_markov_event", + "ResearchFormalCoreR1.p02_lm009_badJet_measurable", + "ResearchFormalCoreR1.p02_lm009_badJet_subset", + "ResearchFormalCoreR1.p02_lm009_moment40_tail", + "ResearchFormalCoreR1.p02_lm009_moment40_weighted_r4", + "ResearchFormalCoreR1.p02_lm009_moment40_weighted_r3", + "ResearchFormalCoreR1.p02_lm009_moment40_family_r3", + "ResearchFormalCoreR1.p02_lm009_moment40_tail_finite", + "ResearchFormalCoreR1.p02_lm009_moment40_tail_of_finite", + "ResearchFormalCoreR1.p02_lm009_moment40_family_r3_interval", + "ResearchFormalCoreR1.p02_lm009_moment40_family_r3_of_interval", + "ResearchFormalCoreR1.weightPerturbation_integral", + "ResearchFormalCoreR1.weightPerturbation_setIntegral", + "ResearchFormalCoreR1.weightPerturbation_normalizer_lower", + "ResearchFormalCoreR1.weightPerturbation_event_integral_bounds", + "ResearchFormalCoreR1.weightPerturbation_quotient_bound", + "ResearchFormalCoreR1.weightedLaw_event_perturbation", + "ResearchFormalCoreR1.weightedLaw_event_perturbation_r2", + "ResearchFormalCoreR1.d2Alpha", + "ResearchFormalCoreR1.d2Gamma", + "ResearchFormalCoreR1.d2DetV", + "ResearchFormalCoreR1.d2Numerator", + "ResearchFormalCoreR1.d2Schur", + "ResearchFormalCoreR1.d2Delta", + "ResearchFormalCoreR1.d2Tau", + "ResearchFormalCoreR1.d2_schur_numerator", + "ResearchFormalCoreR1.d2_det_axis", + "ResearchFormalCoreR1.d2_det_diagonal", + "ResearchFormalCoreR1.d2_det_interpolation", + "ResearchFormalCoreR1.d2_schur_ratio", + "ResearchFormalCoreR1.d2_numerator_pos", + "ResearchFormalCoreR1.d2_det_pos", + "ResearchFormalCoreR1.d2_schur_pos", + "ResearchFormalCoreR1.d2_det_le_endpoint_max", + "ResearchFormalCoreR1.d2_schur_endpoint_lower", + "ResearchFormalCoreR1.d2_tau_cumulant_eq", + "ResearchFormalCoreR1.d2_tau_axis", + "ResearchFormalCoreR1.d2_tau_diagonal", + "ResearchFormalCoreR1.d2_tau_pos", + "ResearchFormalCoreR1.d2_gaussian_control", + "ResearchFormalCoreR1.d2_degenerate_control" + ] } From 9f71025bd1faad460e168bab084f62a961d7f105 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:49:53 -0700 Subject: [PATCH 09/38] fix(formal): repair declaration scanner helper --- formal/gate.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/formal/gate.py b/formal/gate.py index a1159501b..98b0b2f83 100644 --- a/formal/gate.py +++ b/formal/gate.py @@ -48,9 +48,41 @@ def strip_lean_noncode(text): require(block==0 and not string,'unterminated Lean comment or string') return ''.join(out) -def scan_module_declarations(text,path): - clean=strip_lean_noncode(text) - namespaces=re.findall(r'^\\s*namespace\\s+(' + NAME + r')\\s* require(isinstance(targets, list) and targets and len(set(targets)) == len(targets), 'empty or duplicate target list') +def scan_module_declarations(text, path): + """Fail-closed grammar for primary source modules; not a general Lean parser.""" + clean = strip_lean_noncode(text) + namespaces = re.findall(r'^\\s*namespace\\s+(' + NAME + r')\\s*$', clean, re.M) + require(namespaces == ['ResearchFormalCoreR1'], + 'unsupported namespace structure: ' + path) + declarations = [] + targets = [] + for lineno, line in enumerate(clean.splitlines(), 1): + if not line.strip(): + continue + match = DECL_LINE.match(line) + if match: + kind, name = match.groups() + full = 'ResearchFormalCoreR1.' + name + declarations.append(full) + if kind in ('theorem', 'lemma'): + targets.append(full) + continue + stripped = line.lstrip() + if line != stripped and re.match( + r'(?:noncomputable\\s+)?(?:def|theorem|lemma)\\b', stripped): + raise ValueError( + 'indented declaration outside supported grammar: ' + + path + ':' + str(lineno)) + if any(stripped.startswith(prefix) for prefix in UNSUPPORTED_COMMANDS): + raise ValueError( + 'unsupported declaration-bearing command: ' + + path + ':' + str(lineno)) + require(len(declarations) == len(set(declarations)), + 'duplicate source declaration: ' + path) + return declarations, targets + +def audit_axioms(text, targets): + require(isinstance(targets, list) and targets and len(set(targets)) == len(targets), 'empty or duplicate target list') records = {} for match in AXIOM.finditer(text): name, raw = match.groups() @@ -62,7 +94,6 @@ def scan_module_declarations(text,path): require(not AXIOM.sub('', text).strip(), 'unrecognized audit output') require(set(records) == set(targets), 'missing target axiom report') return records - def check_files(root, files): require(isinstance(files, dict) and files, 'empty file manifest') root = root.resolve() From 8bd9153d238154bdfc500084fbcd75097febca83 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:50:59 -0700 Subject: [PATCH 10/38] test(formal): repin final hardened gate --- formal/tests/test_d2_schur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_d2_schur.py b/formal/tests/test_d2_schur.py index b756eecb3..f2f72a6d4 100644 --- a/formal/tests/test_d2_schur.py +++ b/formal/tests/test_d2_schur.py @@ -52,7 +52,7 @@ def test_manifest_exact_extension(self): for path in (MODULE, 'D2_SCHUR.md', 'tests/test_d2_schur.py'): self.assertEqual(hashlib.sha256((ROOT/path).read_bytes()).hexdigest(), m['files'][path]) self.assertEqual(m['files']['gate.py'], - '1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92') + 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a') self.assertEqual(m['files']['ResearchFormalCoreR1/WeightPerturbation.lean'], '50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f') From 40d1716c510c4d9c5392e24f31b03a09dcc61cbb Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:51:02 -0700 Subject: [PATCH 11/38] test(formal): repin final hardened gate --- formal/tests/test_moment_generality.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_generality.py b/formal/tests/test_moment_generality.py index b43e8e74b..be9288ba9 100644 --- a/formal/tests/test_moment_generality.py +++ b/formal/tests/test_moment_generality.py @@ -55,7 +55,7 @@ def test_predecessor_and_gate_pins_preserved(self): pins = { 'ResearchFormalCoreR1/MomentTail.lean': '87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': '958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68', + 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 419d3695590374a2f77c5c1243acfb5f6070ed14 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:51:05 -0700 Subject: [PATCH 12/38] test(formal): repin final hardened gate --- formal/tests/test_moment_tail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_tail.py b/formal/tests/test_moment_tail.py index e33e5eddc..d7c9d0c90 100644 --- a/formal/tests/test_moment_tail.py +++ b/formal/tests/test_moment_tail.py @@ -57,7 +57,7 @@ def test_retained_control_and_proof_pins(self): pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': '958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68', + 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', } for name, digest in pins.items(): self.assertEqual(m['files'][name], digest) From 943466c9000d61aea37f9584f0d11cb091570b01 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:51:10 -0700 Subject: [PATCH 13/38] test(formal): repin final hardened gate --- formal/tests/test_weight_perturbation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weight_perturbation.py b/formal/tests/test_weight_perturbation.py index a887346ac..46ff1eaa4 100644 --- a/formal/tests/test_weight_perturbation.py +++ b/formal/tests/test_weight_perturbation.py @@ -65,7 +65,7 @@ def test_predecessor_and_gate_identity_contract(self): pins = { 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', 'ResearchFormalCoreR1/MomentGenerality.lean': 'fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509', - 'gate.py': '1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92', + 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 4fe469d973758af227dd69bcf8aecfd7bb3d613e Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:51:12 -0700 Subject: [PATCH 14/38] test(formal): repin final hardened gate --- formal/tests/test_weighted_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weighted_law.py b/formal/tests/test_weighted_law.py index 804ff608b..d093e9bea 100644 --- a/formal/tests/test_weighted_law.py +++ b/formal/tests/test_weighted_law.py @@ -57,7 +57,7 @@ def test_parent_proofs_and_execution_controls_retained(self): m = json.loads((ROOT/'manifest.json').read_text()) pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', - 'gate.py': '958b149ea3a4ce735644a2079f817b5deddf5dc0d5ca347904ad09db6ad47d68', + 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', 'lean-toolchain': 'd5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b', } for path, digest in pins.items(): From c7555fee7a344971cb4906c41e4d6fca228ba13a Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:51:36 -0700 Subject: [PATCH 15/38] formal: bind final declaration-admission bytes --- formal/manifest.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/formal/manifest.json b/formal/manifest.json index 7f2738c66..16709672b 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -105,7 +105,7 @@ "SCOPE.md": "8fa7e879e23742aba77df8687659ca9dcfd13a19e0add6192229c2f80629b3cb", "blueprint/src/content.tex": "0c976e8e4a5e69a443e93796c8f6cbf91bf73c224448c4256de72a041e04161f", "evidence/initial-build-failure.log": "43393d2e2b3148a2453e3092a1ff04e257dbd4a6dc97eb2853a8bc0451002591", - "gate.py": "1a3c3a3d3e3f6c59ff5e9d27476dd301658f232c6370bbf5666108230853bd92", + "gate.py": "e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a", "lake-manifest.json": "d63753befccc21923783a5085148e3ff028d97134ea52f5a08bd87ecf1b0b043", "lakefile.toml": "d629891716c169404d1f0d76492441c8dd66f1e790b1b8b0819e51c8d5059255", "lean-toolchain": "d5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b", @@ -117,23 +117,23 @@ "tests/test_measure_bridge.py": "0e54b2ed7c2e197967180993b4c0c1ece1e63576ad34fd74ac232a4d507628a2", "ResearchFormalCoreR1/WeightedLaw.lean": "f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736", "WEIGHTED_LAW.md": "a530dc957923c4c9111b734d8fcc0f4cd3cdba45b08480cfc2e241ecefc5fba4", - "tests/test_weighted_law.py": "e4cb5f2cdc58ac1e3af09b548c6dbb24ff3f060a101636589d2db994c0d66b94", + "tests/test_weighted_law.py": "72f087e8cab5fa63381f1ad578a8679fa738c36c482cbb3c7454979b3c94413d", "ResearchFormalCoreR1/MomentTail.lean": "87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa", "MOMENT_TAIL.md": "20ecddcd830c41935dc4b366882db4d0432240932af6632d025b981f90e044bd", - "tests/test_moment_tail.py": "4019207cb4df25f5776856e348ca1eba2d96ade955aef9df60ccd99204ed82b0", + "tests/test_moment_tail.py": "e393c0def197005c8fc5b21616e3122e6c5bc7f9270cdbc65fa65c6712f71a33", "evidence/moment-tail-initial-build-failure.log": "61bee2d55d0dd548b0bccc4d2e40970f654fca8ab1385af7f371592d45c0ab08", "ResearchFormalCoreR1/MomentGenerality.lean": "fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509", "MOMENT_GENERALITY.md": "5386c763f95ecd0946e0f23a22d1ba90e7956ff1a1bfcada0d454d1b5ace0cf2", - "tests/test_moment_generality.py": "cba72a9747670e269bbec414f89fd2e8291dcf331d01a8e1e88ad6c7f2841383", + "tests/test_moment_generality.py": "e9af2b7bf689851e47c63298c9fb8ce0c47f5b0db2bde5ff98ee705db8e1f2b2", "tests/test_alignment_lineage.py": "c510c6566d7689bebde76843582e9936dfa9e62e783b816b5e4f63d83434b893", "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "ResearchFormalCoreR1/WeightPerturbation.lean": "50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f", "WEIGHT_PERTURBATION.md": "0ac969b86f5b2cc4e84b4bb61790c7497164b923dc5f3fc5be99195b75b1b212", - "tests/test_weight_perturbation.py": "8a9a90921be35b009ffb188c1be725c409bbd792b442215dbbc125e7f4e2ebe0", + "tests/test_weight_perturbation.py": "4b3526e98554ac873d3b3cf2aeb412bd98fb4971647c0391fcd2f58f93458273", "evidence/weight-perturbation-initial-build-failure.log": "2abe4ec4bcfa4de282e7983ffebb225af21308c60b84083a1696bcced31b9c7c", "D2_SCHUR.md": "c25f4add4c92479ae0f6dfaac43fc9be7a71766f050a79acbc2c72acf47ef4d3", "ResearchFormalCoreR1/D2Schur.lean": "d3fb3ed7b4043227f1d5a06e208de9d94eb413c067e9d0f8802388407c9e15b9", - "tests/test_d2_schur.py": "17492a222ca8005465d934710d654c7171d03d8febf0af775bcfdbe35f15f9da", + "tests/test_d2_schur.py": "76e00000883dadc2facda608df02d2898a2d298e449b400643177d78e0337f62", "DECLARATION_ADMISSION.md": "d0ea2f3875e6f0ad8ff38e57c9e366953f06dcb6bbe275ec0bbb690fb03d7769", "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7" }, From b07e063e7a875086f271d465fce59caa0b23c149 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:52:39 -0700 Subject: [PATCH 16/38] fix(formal): remove orphaned predecessor tail --- formal/gate.py | 224 ------------------------------------------------- 1 file changed, 224 deletions(-) diff --git a/formal/gate.py b/formal/gate.py index 98b0b2f83..f3d425db7 100644 --- a/formal/gate.py +++ b/formal/gate.py @@ -290,227 +290,3 @@ def main(): if __name__ == '__main__': sys.exit(main()) -,clean,re.M) - require(namespaces==['ResearchFormalCoreR1'],'unsupported namespace structure: '+path) - declarations=[]; targets=[] - for lineno,line in enumerate(clean.splitlines(),1): - if not line.strip(): continue - match=DECL_LINE.match(line) - if match: - kind,name=match.groups(); full='ResearchFormalCoreR1.'+name - declarations.append(full) - if kind in ('theorem','lemma'): targets.append(full) - continue - stripped=line.lstrip() - if line!=stripped and re.match(r'(?:noncomputable\\s+)?(?:def|theorem|lemma)\\b',stripped): - raise ValueError('indented declaration outside supported grammar: '+path+':'+str(lineno)) - if any(stripped.startswith(prefix) for prefix in UNSUPPORTED_COMMANDS): - raise ValueError('unsupported declaration-bearing command: '+path+':'+str(lineno)) - require(len(declarations)==len(set(declarations)),'duplicate source declaration: '+path) - return declarations,targets - -def audit_axioms(text, targets): - require(isinstance(targets, list) and targets and len(set(targets)) == len(targets), 'empty or duplicate target list') - records = {} - for match in AXIOM.finditer(text): - name, raw = match.groups() - require(name in targets and name not in records, 'unexpected or duplicate axiom target: ' + name) - axioms = [] if raw is None or not raw.strip() else [a.strip() for a in raw.split(',')] - require(all(re.fullmatch(NAME, a) for a in axioms), 'malformed axiom name') - require(set(axioms) <= ALLOWED, 'forbidden transitive axiom: ' + repr(set(axioms) - ALLOWED)) - records[name] = sorted(set(axioms)) - require(not AXIOM.sub('', text).strip(), 'unrecognized audit output') - require(set(records) == set(targets), 'missing target axiom report') - return records - -def check_files(root, files): - require(isinstance(files, dict) and files, 'empty file manifest') - root = root.resolve() - for name, digest in files.items(): - p = PurePosixPath(name) - require(not p.is_absolute() and '..' not in p.parts and str(p) == name and '\\' not in name, 'unsafe manifest path') - require(isinstance(digest, str) and SHA256.fullmatch(digest), 'invalid source hash') - target = root / p - require(all(not (root.joinpath(*p.parts[:i])).is_symlink() for i in range(1, len(p.parts) + 1)), 'symlink source') - require(target.is_file() and target.resolve().is_relative_to(root), 'missing or escaping source: ' + name) - require(sha(target.read_bytes()) == digest, 'source hash mismatch: ' + name) - -def check_blueprint(text, targets): - links = re.findall(r'\\lean\{([^}]+)\}', text) - require(links and all(n in targets for n in links), 'missing or unresolved Blueprint declaration') - require('\\leanok' not in text, 'unreviewed Blueprint completion label') - return links - -def lineage_identity(party, role): - """Validate declared identity text, not the truth or completeness of authorship.""" - require(isinstance(party, dict), 'malformed review lineage: ' + role) - placeholders = {'unknown', 'unverified', 'unspecified', 'tbd', 'n/a', - 'none', 'null', '?', 'not known', 'not available'} - result = {} - for key in ('provider', 'family', 'agent'): - value = party.get(key) - require(isinstance(value, str) and value.strip(), 'missing review lineage: ' + role + '.' + key) - normalized = ' '.join(value.split()).casefold() - require(normalized not in placeholders, 'ambiguous review lineage: ' + role + '.' + key) - result[key] = normalized - return result - -def check_alignment(review, manifest_digest, targets, scope_digest): - require(isinstance(review, dict), 'review must be object') - require(review.get('disposition') == 'ACCEPTED', 'alignment not accepted') - require(review.get('manifest_sha256') == manifest_digest, 'stale alignment manifest') - require(review.get('scope_sha256') == scope_digest, 'stale alignment scope') - covered = review.get('targets', []) - require(isinstance(covered, list) and len(covered) == len(set(covered)) and set(covered) == set(targets), 'partial or ambiguous review') - r = lineage_identity(review.get('reviewer'), 'reviewer') - authors = [('author', lineage_identity(review.get('author'), 'author'))] - # Absence preserves the historical single-author schema. Declared proposers - # are additional authors, never ignored metadata or a substitute for author. - proposers = review.get('proposal_authors', []) - require(isinstance(proposers, list), 'malformed review lineage: proposal_authors') - for index, proposer in enumerate(proposers): - role = 'proposal_authors[' + str(index) + ']' - identity = lineage_identity(proposer, role) - if 'targets' in proposer: - scope = proposer['targets'] - require(isinstance(scope, list) and scope and - all(isinstance(name, str) and name in targets for name in scope) and - len(scope) == len(set(scope)), 'invalid proposal target scope: ' + role) - authors.append((role, identity)) - for role, author in authors: - for key in ('provider', 'family', 'agent'): - require(author[key] != r[key], 'lineage not independent: ' + role + '.' + key) - e = review.get('evidence', {}) - require(isinstance(e.get('repository'), str) and re.fullmatch(r'[^/\s]+/[^/\s]+', e['repository']), 'missing evidence repo') - require(isinstance(e.get('commit'), str) and COMMIT.fullmatch(e['commit']), 'mutable review ref') - require(isinstance(e.get('sha256'), str) and SHA256.fullmatch(e['sha256']), 'missing review hash') - require(isinstance(e.get('path'), str) and e['path'] and not PurePosixPath(e['path']).is_absolute() and '..' not in PurePosixPath(e['path']).parts, 'missing or unsafe review path') - # This checks structure, identity and scope. A controller must retrieve and - # authenticate the review evidence; strings do not prove a review happened. - -def load_json(text): - def unique(pairs): - obj = {} - for key, value in pairs: - require(key not in obj, 'duplicate JSON key: ' + key) - obj[key] = value - return obj - return json.loads(text, object_pairs_hook=unique) - -def source_check(): - raw = (ROOT / 'manifest.json').read_bytes() - m = load_json(raw) - require(m.get('schema_version') == 1 and m.get('scientific_effect') == 'NONE', 'invalid evidence schema') - require(m.get('formalization_status') == 'proved' and m.get('alignment_status') == 'PENDING_INDEPENDENT_REVIEW', 'source metadata cannot self-award execution or review') - required = {'gate.py', 'tests/test_gate.py', 'lean-toolchain', 'lakefile.toml', 'lake-manifest.json', 'ResearchFormalCoreR1.lean', 'SCOPE.md', 'GLOSSARY.md', 'README.md', 'blueprint/src/content.tex', 'tests/test_alignment_lineage.py', 'LINEAGE_VALIDATION.md'} - require(required <= set(m['files']), 'unbound control or scope file') - originals = {'originals/Algebra.lean.txt': '4c196820c4db8fafc288dd35642828ba577e3544d60aba7d1d6d24f14ad1e8ae', 'originals/ProbabilityCompanions.lean.txt': '4ace6600a476859982c8851ae9097c89b082d3c96291b3c8330bd4cc00bae65d'} - require(all(m['files'].get(path) == value for path, value in originals.items()), 'original source identity changed or omitted') - require('COMPATIBILITY.md' in m['files'], 'unbound successor rationale') - check_files(ROOT, m['files']) - require((ROOT / 'lean-toolchain').read_text().strip() == 'leanprover/lean4:v4.34.1', 'wrong Lean toolchain') - lock = load_json((ROOT / 'lake-manifest.json').read_text()) - packages = lock['packages'] - require(len(packages) == len({p['name'] for p in packages}), 'duplicate dependency') - actual = {p['name']: p['rev'] for p in packages} - require(actual == m['dependency_revisions'], 'dependency lock mismatch') - require(all(COMMIT.fullmatch(v) for v in actual.values()), 'unpinned dependency') - expected_root = ''.join('import ' + path[:-5].replace('/', '.') + '\n' for path in m['source_modules']) - require((ROOT / 'ResearchFormalCoreR1.lean').read_text() == expected_root, 'root must only import registered modules') - names = [] - for path in m['source_modules']: - require(path in m['files'], 'unbound source module') - text = (ROOT / path).read_text() - names.extend('ResearchFormalCoreR1.' + n for n in re.findall(r'^(?:theorem|lemma)\s+(' + NAME + ')', text, re.M)) - require(names == m['targets'], 'target inventory differs from source declarations') - check_blueprint((ROOT / 'blueprint/src/content.tex').read_text(), names) - lean_files = {p.relative_to(ROOT).as_posix() for p in ROOT.rglob('*.lean') if '.lake' not in p.relative_to(ROOT).parts} - require(lean_files == set(m['source_modules']) | {'ResearchFormalCoreR1.lean'}, 'unregistered Lean module') - return m, sha(raw) - -def run(command, label, out, expect_success=True): - result = subprocess.run(command, cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=900) - (out / (label + '.log')).write_text(result.stdout) - print(label + ': exit ' + str(result.returncode), flush=True) - require((result.returncode == 0) == expect_success, 'unexpected process outcome: ' + label + '\n' + result.stdout[-6000:]) - return result.stdout - -def execute(m, digest): - out = ROOT / '.lake' / 'formal-evidence' - out.mkdir(parents=True, exist_ok=True) - require(not (ROOT / '.lake').is_symlink() and not (ROOT / '.lake/build').is_symlink(), 'symlink build directory') - if (ROOT / '.lake/build').exists(): - shutil.rmtree(ROOT / '.lake/build') # fresh local-package build; dependency cache is untouched - run(['lake', 'build'], 'build', out) - run(['lake', 'env', 'leanchecker', 'ResearchFormalCoreR1'], 'leanchecker', out) - audit = out / 'Audit.lean' - audit.write_text('import ResearchFormalCoreR1\n' + '\n'.join('#print axioms ' + n for n in m['targets']) + '\n') - text = run(['lake', 'env', 'lean', str(audit)], 'axioms', out) - axioms = audit_axioms(text, m['targets']) - types = out / 'Types.lean' - types.write_text('import ResearchFormalCoreR1\nset_option pp.explicit true\n' + '\n'.join('#check ' + n for n in m['targets']) + '\n') - run(['lake', 'env', 'lean', str(types)], 'elaborated-types', out) - # Real counterexample/mutation controls, not comparisons of fixed labels. - cases = { - 'false_fold': ('import ResearchFormalCoreR1\nexample : ResearchFormalCoreR1.foldPotential 1 1 - ResearchFormalCoreR1.foldPotential 1 (-1) = (2 : Real)^3 / 7 := by\n norm_num [ResearchFormalCoreR1.foldPotential]\n', False, None), - 'false_power': ('import ResearchFormalCoreR1\nexample : (2 : Real)^4 <= (2 : Real)^3 := by\n norm_num\n', False, None), - 'sorry': ('import ResearchFormalCoreR1\ntheorem injected : False := by sorry\n#print axioms injected\n', True, 'injected'), - 'custom_imported': ('import ResearchFormalCoreR1\naxiom hiddenPremise : False\ntheorem middle : False := hiddenPremise\ntheorem injected : False := middle\n#print axioms injected\n', True, 'injected'), - 'native': ('import ResearchFormalCoreR1\ntheorem injected : (2 : Nat) + 2 = 4 := by native_decide\n#print axioms injected\n', True, 'injected')} - outcomes = {} - for label, (source, succeeds, target) in cases.items(): - path = out / (label + '.lean'); path.write_text(source) - log = run(['lake', 'env', 'lean', str(path)], label, out, succeeds) - if target: - # Remove compiler diagnostics only by selecting the axiom line onward; - # absence remains an error, never an empty allowlist. - matches = list(AXIOM.finditer(log)) - require(matches, 'negative control missing axiom report: ' + label) - payload = '\n'.join(x.group() for x in matches) - try: - audit_axioms(payload, [target]) - except ValueError: - outcomes[label] = 'REJECTED_BY_AXIOM_GATE' - else: - raise ValueError('negative control escaped: ' + label) - else: - require('unsolved goals' in log or 'norm_num' in log and 'failed' in log, 'negative control did not fail on its proof goal') - outcomes[label] = 'REJECTED_BY_LEAN' - # Recheck after compilation: a build hook must not rewrite bound sources/lock. - _, after = source_check(); require(after == digest, 'manifest changed during execution') - version = run(['lake', 'env', 'lean', '--version'], 'version', out).strip() - require('version 4.34.1' in version, 'unexpected running Lean version') - actual_dependencies = {name: subprocess.check_output(['git', '-C', str(ROOT / '.lake/packages' / name), 'rev-parse', 'HEAD'], text=True).strip() for name in m['dependency_revisions']} - require(actual_dependencies == m['dependency_revisions'], 'installed dependency HEAD differs from lock') - head = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip() - require(COMMIT.fullmatch(head), 'missing exact checked commit') - receipt = dict(schema_version=1, scientific_effect='NONE', formalization_status='kernel-checked', - alignment_status='PENDING_INDEPENDENT_REVIEW', manifest_sha256=digest, - checked_commit=head, repository=os.environ.get('GITHUB_REPOSITORY'), - workflow_run_id=os.environ.get('GITHUB_RUN_ID'), lean_version=version, - dependency_revisions=m['dependency_revisions'], axioms=axioms, negative_controls=outcomes) - receipt['logs'] = {p.name: sha(p.read_bytes()) for p in sorted(out.glob('*.log'))} - (out / 'receipt.json').write_text(json.dumps(receipt, indent=2, sort_keys=True) + '\n') - print(json.dumps(receipt, indent=2, sort_keys=True)) - return receipt - -def main(): - p = argparse.ArgumentParser(description=__doc__) - p.add_argument('--execute', action='store_true', help='run build, transitive axiom audit and negative controls') - p.add_argument('--alignment', type=Path, help='validate a separately authenticated alignment record; never promotes science') - args = p.parse_args() - try: - m, digest = source_check() - if args.alignment: - check_alignment(load_json(args.alignment.read_text()), digest, m['targets'], m['files']['SCOPE.md']) - if args.execute: - execute(m, digest) - else: - print('SOURCE_IDENTITY_PASS (not a Lean build or scientific acceptance): ' + digest) - except (ValueError, KeyError, TypeError, OSError, subprocess.SubprocessError) as e: - print('FORMAL_GATE_FAIL: ' + str(e), file=sys.stderr) - return 1 - return 0 - -if __name__ == '__main__': - sys.exit(main()) From 42241429f3fa748b93d6397346f705304416d5d3 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:53:27 -0700 Subject: [PATCH 17/38] test(formal): repin repaired admission gate --- formal/tests/test_d2_schur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_d2_schur.py b/formal/tests/test_d2_schur.py index f2f72a6d4..3951bf09a 100644 --- a/formal/tests/test_d2_schur.py +++ b/formal/tests/test_d2_schur.py @@ -52,7 +52,7 @@ def test_manifest_exact_extension(self): for path in (MODULE, 'D2_SCHUR.md', 'tests/test_d2_schur.py'): self.assertEqual(hashlib.sha256((ROOT/path).read_bytes()).hexdigest(), m['files'][path]) self.assertEqual(m['files']['gate.py'], - 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a') + '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612') self.assertEqual(m['files']['ResearchFormalCoreR1/WeightPerturbation.lean'], '50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f') From 6a43eed63e207a018fa7379dd14a765d15786271 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:53:30 -0700 Subject: [PATCH 18/38] test(formal): repin repaired admission gate --- formal/tests/test_moment_generality.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_generality.py b/formal/tests/test_moment_generality.py index be9288ba9..2f6be0dde 100644 --- a/formal/tests/test_moment_generality.py +++ b/formal/tests/test_moment_generality.py @@ -55,7 +55,7 @@ def test_predecessor_and_gate_pins_preserved(self): pins = { 'ResearchFormalCoreR1/MomentTail.lean': '87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', + 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 0a131f739a54b23e87ea172fc4c84b57ed449e84 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:53:33 -0700 Subject: [PATCH 19/38] test(formal): repin repaired admission gate --- formal/tests/test_moment_tail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_tail.py b/formal/tests/test_moment_tail.py index d7c9d0c90..d9124626e 100644 --- a/formal/tests/test_moment_tail.py +++ b/formal/tests/test_moment_tail.py @@ -57,7 +57,7 @@ def test_retained_control_and_proof_pins(self): pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', + 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', } for name, digest in pins.items(): self.assertEqual(m['files'][name], digest) From 1c7dad36f3abff6cda56c46b0f6b2065511d222c Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:53:35 -0700 Subject: [PATCH 20/38] test(formal): repin repaired admission gate --- formal/tests/test_weight_perturbation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weight_perturbation.py b/formal/tests/test_weight_perturbation.py index 46ff1eaa4..faea7f7d6 100644 --- a/formal/tests/test_weight_perturbation.py +++ b/formal/tests/test_weight_perturbation.py @@ -65,7 +65,7 @@ def test_predecessor_and_gate_identity_contract(self): pins = { 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', 'ResearchFormalCoreR1/MomentGenerality.lean': 'fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509', - 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', + 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 8a9e3ba7b7c39f10204e73ec792e4cca823728d4 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:53:38 -0700 Subject: [PATCH 21/38] test(formal): repin repaired admission gate --- formal/tests/test_weighted_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weighted_law.py b/formal/tests/test_weighted_law.py index d093e9bea..96c902e80 100644 --- a/formal/tests/test_weighted_law.py +++ b/formal/tests/test_weighted_law.py @@ -57,7 +57,7 @@ def test_parent_proofs_and_execution_controls_retained(self): m = json.loads((ROOT/'manifest.json').read_text()) pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', - 'gate.py': 'e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a', + 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', 'lean-toolchain': 'd5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b', } for path, digest in pins.items(): From 3eaf30b9bce38aa1741bd40ad6330483a72eeea3 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:54:04 -0700 Subject: [PATCH 22/38] formal: bind repaired admission gate --- formal/manifest.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/formal/manifest.json b/formal/manifest.json index 16709672b..a7e743c53 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -105,7 +105,7 @@ "SCOPE.md": "8fa7e879e23742aba77df8687659ca9dcfd13a19e0add6192229c2f80629b3cb", "blueprint/src/content.tex": "0c976e8e4a5e69a443e93796c8f6cbf91bf73c224448c4256de72a041e04161f", "evidence/initial-build-failure.log": "43393d2e2b3148a2453e3092a1ff04e257dbd4a6dc97eb2853a8bc0451002591", - "gate.py": "e5803daa85459e2d3d3ea6ca708d6ac5fd4f241cdba7648fcffb1b3fc6f4608a", + "gate.py": "04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612", "lake-manifest.json": "d63753befccc21923783a5085148e3ff028d97134ea52f5a08bd87ecf1b0b043", "lakefile.toml": "d629891716c169404d1f0d76492441c8dd66f1e790b1b8b0819e51c8d5059255", "lean-toolchain": "d5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b", @@ -117,23 +117,23 @@ "tests/test_measure_bridge.py": "0e54b2ed7c2e197967180993b4c0c1ece1e63576ad34fd74ac232a4d507628a2", "ResearchFormalCoreR1/WeightedLaw.lean": "f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736", "WEIGHTED_LAW.md": "a530dc957923c4c9111b734d8fcc0f4cd3cdba45b08480cfc2e241ecefc5fba4", - "tests/test_weighted_law.py": "72f087e8cab5fa63381f1ad578a8679fa738c36c482cbb3c7454979b3c94413d", + "tests/test_weighted_law.py": "a4f6b0a8041afe625ee8754c7b707822c94756cf03bb3134025df0d25338ffe3", "ResearchFormalCoreR1/MomentTail.lean": "87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa", "MOMENT_TAIL.md": "20ecddcd830c41935dc4b366882db4d0432240932af6632d025b981f90e044bd", - "tests/test_moment_tail.py": "e393c0def197005c8fc5b21616e3122e6c5bc7f9270cdbc65fa65c6712f71a33", + "tests/test_moment_tail.py": "bd95419f59b768b23d0c41ca65b0f8fc3df81816efce34ac03eb364fcebaade5", "evidence/moment-tail-initial-build-failure.log": "61bee2d55d0dd548b0bccc4d2e40970f654fca8ab1385af7f371592d45c0ab08", "ResearchFormalCoreR1/MomentGenerality.lean": "fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509", "MOMENT_GENERALITY.md": "5386c763f95ecd0946e0f23a22d1ba90e7956ff1a1bfcada0d454d1b5ace0cf2", - "tests/test_moment_generality.py": "e9af2b7bf689851e47c63298c9fb8ce0c47f5b0db2bde5ff98ee705db8e1f2b2", + "tests/test_moment_generality.py": "e74858c7c275d83bf59fbd5ca6c4c8c8890481d2865521ee001c21329e2e76e7", "tests/test_alignment_lineage.py": "c510c6566d7689bebde76843582e9936dfa9e62e783b816b5e4f63d83434b893", "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "ResearchFormalCoreR1/WeightPerturbation.lean": "50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f", "WEIGHT_PERTURBATION.md": "0ac969b86f5b2cc4e84b4bb61790c7497164b923dc5f3fc5be99195b75b1b212", - "tests/test_weight_perturbation.py": "4b3526e98554ac873d3b3cf2aeb412bd98fb4971647c0391fcd2f58f93458273", + "tests/test_weight_perturbation.py": "9c24e9a76024e00b1026a733187b512088a192b899e4a7a3ca8ace7a75520403", "evidence/weight-perturbation-initial-build-failure.log": "2abe4ec4bcfa4de282e7983ffebb225af21308c60b84083a1696bcced31b9c7c", "D2_SCHUR.md": "c25f4add4c92479ae0f6dfaac43fc9be7a71766f050a79acbc2c72acf47ef4d3", "ResearchFormalCoreR1/D2Schur.lean": "d3fb3ed7b4043227f1d5a06e208de9d94eb413c067e9d0f8802388407c9e15b9", - "tests/test_d2_schur.py": "76e00000883dadc2facda608df02d2898a2d298e449b400643177d78e0337f62", + "tests/test_d2_schur.py": "84bd76408b820f2fa2f9a54a61b224234d8d0654e20cd64a1cfa4eb10bfc45c6", "DECLARATION_ADMISSION.md": "d0ea2f3875e6f0ad8ff38e57c9e366953f06dcb6bbe275ec0bbb690fb03d7769", "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7" }, From b3e44d6773e3322f2d9d31425340cf1abcbe6875 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:55:22 -0700 Subject: [PATCH 23/38] fix(formal): correct raw-regex escapes From 40484e31e888427f91aa970e7adfe10a54e97cef Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:55:41 -0700 Subject: [PATCH 24/38] fix(formal): use intended regex escapes --- formal/gate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/formal/gate.py b/formal/gate.py index f3d425db7..72f664f84 100644 --- a/formal/gate.py +++ b/formal/gate.py @@ -15,7 +15,7 @@ AXIOM = re.compile(r"'(" + NAME + r")' (?:depends on axioms: \[([^\]]*)\]|does not depend on any axioms)") SHA256 = re.compile(r'[0-9a-f]{64}') COMMIT = re.compile(r'[0-9a-f]{40}') -DECL_LINE = re.compile(r'^(?:noncomputable\\s+)?(def|theorem|lemma)\\s+(' + NAME + r')\\b') +DECL_LINE = re.compile(r'^(?:noncomputable\s+)?(def|theorem|lemma)\s+(' + NAME + r')\b') UNSUPPORTED_COMMANDS = ('private ', 'protected ', '@[', 'opaque ', 'axiom ', 'abbrev ', 'structure ', 'class ', 'instance ', 'local instance ', 'inductive ', 'mutual', 'syntax ', 'macro ', 'elab ', 'initialize ', 'example ') ROOT = Path(__file__).resolve().parent @@ -51,7 +51,7 @@ def strip_lean_noncode(text): def scan_module_declarations(text, path): """Fail-closed grammar for primary source modules; not a general Lean parser.""" clean = strip_lean_noncode(text) - namespaces = re.findall(r'^\\s*namespace\\s+(' + NAME + r')\\s*$', clean, re.M) + namespaces = re.findall(r'^\s*namespace\s+(' + NAME + r')\s*$', clean, re.M) require(namespaces == ['ResearchFormalCoreR1'], 'unsupported namespace structure: ' + path) declarations = [] @@ -69,7 +69,7 @@ def scan_module_declarations(text, path): continue stripped = line.lstrip() if line != stripped and re.match( - r'(?:noncomputable\\s+)?(?:def|theorem|lemma)\\b', stripped): + r'(?:noncomputable\s+)?(?:def|theorem|lemma)\b', stripped): raise ValueError( 'indented declaration outside supported grammar: ' + path + ':' + str(lineno)) From d279f1ee9dcb4b021952b42748c24441309e7b60 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:09 -0700 Subject: [PATCH 25/38] test(formal): repin corrected admission gate --- formal/tests/test_d2_schur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_d2_schur.py b/formal/tests/test_d2_schur.py index 3951bf09a..6d853a57b 100644 --- a/formal/tests/test_d2_schur.py +++ b/formal/tests/test_d2_schur.py @@ -52,7 +52,7 @@ def test_manifest_exact_extension(self): for path in (MODULE, 'D2_SCHUR.md', 'tests/test_d2_schur.py'): self.assertEqual(hashlib.sha256((ROOT/path).read_bytes()).hexdigest(), m['files'][path]) self.assertEqual(m['files']['gate.py'], - '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612') + 'f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c') self.assertEqual(m['files']['ResearchFormalCoreR1/WeightPerturbation.lean'], '50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f') From b5c9712d5e12ba488bb6edb14461a67c1468a233 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:12 -0700 Subject: [PATCH 26/38] test(formal): repin corrected admission gate --- formal/tests/test_moment_generality.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_generality.py b/formal/tests/test_moment_generality.py index 2f6be0dde..4730769e1 100644 --- a/formal/tests/test_moment_generality.py +++ b/formal/tests/test_moment_generality.py @@ -55,7 +55,7 @@ def test_predecessor_and_gate_pins_preserved(self): pins = { 'ResearchFormalCoreR1/MomentTail.lean': '87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', + 'gate.py': 'f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 6f457c87e200ac6278d85adcfcc05ca9f8595715 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:14 -0700 Subject: [PATCH 27/38] test(formal): repin corrected admission gate --- formal/tests/test_moment_tail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_moment_tail.py b/formal/tests/test_moment_tail.py index d9124626e..9b17b1ca0 100644 --- a/formal/tests/test_moment_tail.py +++ b/formal/tests/test_moment_tail.py @@ -57,7 +57,7 @@ def test_retained_control_and_proof_pins(self): pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', - 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', + 'gate.py': 'f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c', } for name, digest in pins.items(): self.assertEqual(m['files'][name], digest) From 3a71f734d0d104fa6fb79f6bc3f29f4b031f2cff Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:17 -0700 Subject: [PATCH 28/38] test(formal): repin corrected admission gate --- formal/tests/test_weight_perturbation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weight_perturbation.py b/formal/tests/test_weight_perturbation.py index faea7f7d6..254323d0c 100644 --- a/formal/tests/test_weight_perturbation.py +++ b/formal/tests/test_weight_perturbation.py @@ -65,7 +65,7 @@ def test_predecessor_and_gate_identity_contract(self): pins = { 'ResearchFormalCoreR1/WeightedLaw.lean': 'f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736', 'ResearchFormalCoreR1/MomentGenerality.lean': 'fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509', - 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', + 'gate.py': 'f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c', } for path, digest in pins.items(): self.assertEqual(m['files'][path], digest) From 380b1ba39224a9bad02250dc39f000dae36dbc3e Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:20 -0700 Subject: [PATCH 29/38] test(formal): repin corrected admission gate --- formal/tests/test_weighted_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formal/tests/test_weighted_law.py b/formal/tests/test_weighted_law.py index 96c902e80..4fe15608e 100644 --- a/formal/tests/test_weighted_law.py +++ b/formal/tests/test_weighted_law.py @@ -57,7 +57,7 @@ def test_parent_proofs_and_execution_controls_retained(self): m = json.loads((ROOT/'manifest.json').read_text()) pins = { 'ResearchFormalCoreR1/MeasureBridge.lean': '28ac130db490c3c3e2fbfabf3ac5be43c854ab1468bf573bf5a8c0dd2c50955f', - 'gate.py': '04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612', + 'gate.py': 'f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c', 'lean-toolchain': 'd5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b', } for path, digest in pins.items(): From 38ec7fcf0765dd85657c226f3bd21dc0a8bd877b Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:57:19 -0700 Subject: [PATCH 30/38] formal: bind final RF-GATE-01 repair --- formal/manifest.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/formal/manifest.json b/formal/manifest.json index a7e743c53..14d4a80dc 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -105,7 +105,7 @@ "SCOPE.md": "8fa7e879e23742aba77df8687659ca9dcfd13a19e0add6192229c2f80629b3cb", "blueprint/src/content.tex": "0c976e8e4a5e69a443e93796c8f6cbf91bf73c224448c4256de72a041e04161f", "evidence/initial-build-failure.log": "43393d2e2b3148a2453e3092a1ff04e257dbd4a6dc97eb2853a8bc0451002591", - "gate.py": "04cf97b4832dcbba74cb23bc89836805a9060524501e2e08fdd505cc93021612", + "gate.py": "f739a5758ad02774eaabfb6e533fa68e1484e86bd0bb58793cd02a6adaee4b2c", "lake-manifest.json": "d63753befccc21923783a5085148e3ff028d97134ea52f5a08bd87ecf1b0b043", "lakefile.toml": "d629891716c169404d1f0d76492441c8dd66f1e790b1b8b0819e51c8d5059255", "lean-toolchain": "d5edba4e4b8faad9c1baeadb265716d20d03be4d1a2647dc5e35b0c0325bea7b", @@ -117,23 +117,23 @@ "tests/test_measure_bridge.py": "0e54b2ed7c2e197967180993b4c0c1ece1e63576ad34fd74ac232a4d507628a2", "ResearchFormalCoreR1/WeightedLaw.lean": "f31dfd2cd048cc0c0446c54c46d89c9cdd5d7bb9d76e9d8d16b7c034f351d736", "WEIGHTED_LAW.md": "a530dc957923c4c9111b734d8fcc0f4cd3cdba45b08480cfc2e241ecefc5fba4", - "tests/test_weighted_law.py": "a4f6b0a8041afe625ee8754c7b707822c94756cf03bb3134025df0d25338ffe3", + "tests/test_weighted_law.py": "480894aeeda8a15ec047e7771bccba38a88507c209c1ed79c5c72143dfd5b5b7", "ResearchFormalCoreR1/MomentTail.lean": "87fe3ee90afc52d8c2652881f1ae195d1b0b15a2ffc18c84d6166d1a4d0de2aa", "MOMENT_TAIL.md": "20ecddcd830c41935dc4b366882db4d0432240932af6632d025b981f90e044bd", - "tests/test_moment_tail.py": "bd95419f59b768b23d0c41ca65b0f8fc3df81816efce34ac03eb364fcebaade5", + "tests/test_moment_tail.py": "b6c9b9d02a614d54d70ac6319cd4080ae2c3510efb4635a80f53adfdedfbaae2", "evidence/moment-tail-initial-build-failure.log": "61bee2d55d0dd548b0bccc4d2e40970f654fca8ab1385af7f371592d45c0ab08", "ResearchFormalCoreR1/MomentGenerality.lean": "fdec55f1602938cd8fdb5b71535962e76a9671bf230dea53868b14147fb3b509", "MOMENT_GENERALITY.md": "5386c763f95ecd0946e0f23a22d1ba90e7956ff1a1bfcada0d454d1b5ace0cf2", - "tests/test_moment_generality.py": "e74858c7c275d83bf59fbd5ca6c4c8c8890481d2865521ee001c21329e2e76e7", + "tests/test_moment_generality.py": "a1273a38c4c2593099cebb86e3d95ee18150d2d1486cb8e61c1dee7296cfb708", "tests/test_alignment_lineage.py": "c510c6566d7689bebde76843582e9936dfa9e62e783b816b5e4f63d83434b893", "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "ResearchFormalCoreR1/WeightPerturbation.lean": "50ce15709a133da12a0b4333cc653d26c5fd8a412976c975a7c2f3b25ed5c00f", "WEIGHT_PERTURBATION.md": "0ac969b86f5b2cc4e84b4bb61790c7497164b923dc5f3fc5be99195b75b1b212", - "tests/test_weight_perturbation.py": "9c24e9a76024e00b1026a733187b512088a192b899e4a7a3ca8ace7a75520403", + "tests/test_weight_perturbation.py": "dc100f6adedbd4db05de102ef6b3155c6084dffe348ccc19c0d9ab61d4344930", "evidence/weight-perturbation-initial-build-failure.log": "2abe4ec4bcfa4de282e7983ffebb225af21308c60b84083a1696bcced31b9c7c", "D2_SCHUR.md": "c25f4add4c92479ae0f6dfaac43fc9be7a71766f050a79acbc2c72acf47ef4d3", "ResearchFormalCoreR1/D2Schur.lean": "d3fb3ed7b4043227f1d5a06e208de9d94eb413c067e9d0f8802388407c9e15b9", - "tests/test_d2_schur.py": "84bd76408b820f2fa2f9a54a61b224234d8d0654e20cd64a1cfa4eb10bfc45c6", + "tests/test_d2_schur.py": "7a84fed88d0c2d34872c305e027754720cc31ed34ac91214d6e73b0c77c0a3d7", "DECLARATION_ADMISSION.md": "d0ea2f3875e6f0ad8ff38e57c9e366953f06dcb6bbe275ec0bbb690fb03d7769", "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7" }, From e8f4387f5e7940f71d0b4cfeb3e38dac0f95834f Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:50:56 -0500 Subject: [PATCH 31/38] RF312: fix helper loading and bind actual compatibility bytes Load the byte-preserved helper by absolute path when gate.py is imported through importlib, correct the compatibility test's formal-root resolution, and rotate only the files-only overlay hashes to the actual source bytes. This addresses the exact pre-Lean failures from run 40241804730 without changing Lean/runtime semantics. --- formal/gate.py | 11 ++++++++++- formal/manifest.overlay.json | 4 ++-- formal/tests/test_gate_compatibility.py | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/formal/gate.py b/formal/gate.py index fe12db07c..29a2a9e60 100755 --- a/formal/gate.py +++ b/formal/gate.py @@ -8,6 +8,7 @@ """ import argparse import hashlib +import importlib.util import json import os from pathlib import Path @@ -15,7 +16,15 @@ import subprocess import sys -import gate_helper as _core +if 'gate_helper' in sys.modules: + _core = sys.modules['gate_helper'] +else: + _helper_path = Path(__file__).resolve().with_name('gate_helper.py') + _helper_spec = importlib.util.spec_from_file_location('rf312_gate_helper', _helper_path) + if _helper_spec is None or _helper_spec.loader is None: + raise ImportError('cannot load reviewed RF312 helper: ' + str(_helper_path)) + _core = importlib.util.module_from_spec(_helper_spec) + _helper_spec.loader.exec_module(_core) # Re-export the reviewed helper API. Tests and callers continue importing `gate`. for _name in dir(_core): diff --git a/formal/manifest.overlay.json b/formal/manifest.overlay.json index 3e2050461..943eb04a5 100644 --- a/formal/manifest.overlay.json +++ b/formal/manifest.overlay.json @@ -3,11 +3,11 @@ "DECLARATION_ADMISSION.md": "88bda3e378128ef3b8f4cf4e8842ba4a6f89ee5bcecf810a5a3b583079a0cedb", "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "README.md": "52c4829c23c5caa8142b4c8d7152766b92cb966b1cb8213ff5210e6fa4ef080e", - "gate.py": "c422cb1186957a83851e9a36b4f216442d0c835cae3fdaf89b32c73ffddb855d", + "gate.py": "4d40e8d9b604b8ae7d8fccc73f869a0bd8642773e015a55f31594b895a81fb33", "gate_helper.py": "ff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4", "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7", "tests/test_gate.py": "e279e5b1e8c14f913728a18fa345dd1060d856799369fc4c9dce4a5f6dfb7574", - "tests/test_gate_compatibility.py": "62720c319d42876e6f35050a558ac48daa0abb66d59e8afe14015b222732646b" + "tests/test_gate_compatibility.py": "920811ebad0bf7f5c9d057d4396669ad340aae353e224d2e5dbc32ae94587b57" }, "schema_version": 1 } diff --git a/formal/tests/test_gate_compatibility.py b/formal/tests/test_gate_compatibility.py index 57b5aeb86..50ead82d2 100644 --- a/formal/tests/test_gate_compatibility.py +++ b/formal/tests/test_gate_compatibility.py @@ -7,7 +7,7 @@ import unittest from unittest import mock -HERE = pathlib.Path(__file__).resolve().parent +HERE = pathlib.Path(__file__).resolve().parents[1] class CompatibilityTests(unittest.TestCase): def load(self): From 2e49c1d471b0c023dfe9008c71cfc45feda5d1aa Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 20:19:08 +0000 Subject: [PATCH 32/38] RF312: make the layered gate observe the effective ROOT; synthetic fixtures emit the inventory Repairs the red formal job at e8f4387f along dot's failure map (Math-#312 6405047589), preserving the reviewed helper bytes (gate_helper.py unchanged): - gate.py: _sync_root() propagates the wrapper's effective ROOT to the helper module and derives the overlay path from it at every call boundary (source_check, execute, dependency_state, _overlay_bytes), so a caller that rebinds gate.ROOT is observed by every source, overlay and execution read. The overlay is read after the helper's own contract has passed, so a manifest that fails its required-file inventory is refused for that reason. A run whose final observation is refused now leaves no receipt.json behind: the helper writes its own receipt before the wrapper's final checks, and the tests' "refuses receipt" contract requires none to persist. - tests/test_gate.py: the copied-tree fixtures write a neutral overlay before hashing, so the effective manifest is exactly the one a test edits; the synthetic Lean responder emits a protocol-complete compiled-environment inventory derived from the manifest (user theorems placed by the source pre-check, bound compiled_inventory records, the injected constants of the four admission experiments and of the compiled-admission copy, read from the copy's first module), and creates the build directory a real build leaves; the layered gate's three Lean-version observations are recorded; a new regression proves that rebinding gate.ROOT redirects source, overlay and evidence reads. - manifest.overlay.json: rebinds gate.py and tests/test_gate.py to their bytes. Local: 243 tests OK in normal and -O mode; `python3 formal/gate.py` passes source identity. The hosted formal-evidence run (Lean build, recheck, axioms, inventory, controls) is the qualifying execution. Scientific effect NONE. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01NMeKEismAyeqgdB4sy2NJU --- formal/gate.py | 52 +++++++++---- formal/manifest.overlay.json | 4 +- formal/tests/test_gate.py | 139 +++++++++++++++++++++++++++++++++-- 3 files changed, 172 insertions(+), 23 deletions(-) diff --git a/formal/gate.py b/formal/gate.py index 29a2a9e60..7b226a50a 100755 --- a/formal/gate.py +++ b/formal/gate.py @@ -39,6 +39,21 @@ _ALLOWED_OVERLAY_KEYS = frozenset({'schema_version', 'files'}) +def _sync_root(): + """Propagate this module's effective ``ROOT`` to the reviewed helper at every call boundary. + + The helper reads its own module global ``ROOT`` (and this module derives the overlay path + from ``ROOT``) at call time, so a caller that rebinds ``gate.ROOT`` -- the fixtures of the + runtime tests do -- must be observed by every source, overlay and execution read, not only + by this wrapper. Import-time constants would silently keep reading the original tree. + """ + global OVERLAY + root = Path(ROOT) + _core.ROOT = root + OVERLAY = root / 'manifest.overlay.json' + return root + + def sha(data): return hashlib.sha256(data).hexdigest() @@ -62,9 +77,10 @@ def check_worktree_clean(name, status): def dependency_state(m): + root = _sync_root() actual = {} for name in m['dependency_revisions']: - repo = str(ROOT / '.lake' / 'packages' / name) + repo = str(root / '.lake' / 'packages' / name) actual[name] = subprocess.check_output( ['git', '-C', repo, 'rev-parse', 'HEAD'], text=True).strip() check_worktree_clean(name, subprocess.check_output( @@ -79,8 +95,9 @@ def observation(version, dependencies, digest): def _overlay_bytes(): - require(OVERLAY.is_file() and not OVERLAY.is_symlink(), 'missing or symlink manifest overlay') - return OVERLAY.read_bytes() + overlay = _sync_root() / 'manifest.overlay.json' + require(overlay.is_file() and not overlay.is_symlink(), 'missing or symlink manifest overlay') + return overlay.read_bytes() def _effective_manifest(text): @@ -102,21 +119,24 @@ def _effective_manifest(text): def source_check(): - raw = (ROOT / 'manifest.json').read_bytes() - overlay = _overlay_bytes() + root = _sync_root() + raw = (root / 'manifest.json').read_bytes() original = _core.load_json try: _core.load_json = _effective_manifest m, _ = _HELPER_SOURCE_CHECK() finally: _core.load_json = original - return m, sha(raw + b'\0RF312-OVERLAY\0' + overlay) + # The overlay is read after the helper's own contract has passed: a manifest that fails its + # required-file inventory is refused for that reason, whether or not an overlay exists. + return m, sha(raw + b'\0RF312-OVERLAY\0' + _overlay_bytes()) def execute(m, digest): - out = ROOT / '.lake' / 'formal-evidence' + root = _sync_root() + out = root / '.lake' / 'formal-evidence' out.mkdir(parents=True, exist_ok=True) - require(not (ROOT / '.lake').is_symlink() and not (ROOT / '.lake/build').is_symlink(), + require(not (root / '.lake').is_symlink() and not (root / '.lake/build').is_symlink(), 'symlink build directory') # Keep test monkeypatches visible to the reviewed helper execution. _core.run = globals()['run'] @@ -125,11 +145,17 @@ def execute(m, digest): ['lake', 'env', 'lean', '--version'], 'version-preflight', out)) preflight = observation(preflight_version, dependency_state(m), digest) receipt = _HELPER_EXECUTE(m, digest) - _, after = source_check() - require(after == digest, 'manifest or overlay changed during execution') - version = check_lean_version(run(['lake', 'env', 'lean', '--version'], 'version-final', out)) - final = observation(version, dependency_state(m), after) - require(final == preflight, 'runtime, dependencies or sources changed during execution') + try: + _, after = source_check() + require(after == digest, 'manifest or overlay changed during execution') + version = check_lean_version(run(['lake', 'env', 'lean', '--version'], 'version-final', out)) + final = observation(version, dependency_state(m), after) + require(final == preflight, 'runtime, dependencies or sources changed during execution') + except BaseException: + # The reviewed helper has already written its own receipt. A run whose final observation + # is refused must leave no receipt behind (the logs stay as evidence of the refusal). + (out / 'receipt.json').unlink(missing_ok=True) + raise receipt = dict(receipt) receipt.update( lean_version=version, diff --git a/formal/manifest.overlay.json b/formal/manifest.overlay.json index 943eb04a5..274930a1b 100644 --- a/formal/manifest.overlay.json +++ b/formal/manifest.overlay.json @@ -3,10 +3,10 @@ "DECLARATION_ADMISSION.md": "88bda3e378128ef3b8f4cf4e8842ba4a6f89ee5bcecf810a5a3b583079a0cedb", "LINEAGE_VALIDATION.md": "593f8cbdb0a9261a57eded9c559963a9247a1036d5e501c9d6bbd77efe9b0c3a", "README.md": "52c4829c23c5caa8142b4c8d7152766b92cb966b1cb8213ff5210e6fa4ef080e", - "gate.py": "4d40e8d9b604b8ae7d8fccc73f869a0bd8642773e015a55f31594b895a81fb33", + "gate.py": "509b6eb3b5ee2dc7e42810a40b07192b0feaf3924c9dbb398db43cb44e16e731", "gate_helper.py": "ff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4", "tests/test_declaration_admission.py": "6a869f853e886772a825794f06a8399fb62a541fa45e6d881029bb57e4dd7af7", - "tests/test_gate.py": "e279e5b1e8c14f913728a18fa345dd1060d856799369fc4c9dce4a5f6dfb7574", + "tests/test_gate.py": "62bd0f6b3c149b30aa4ad2817aba59667b87ed5c80445f3fd64b005814968a9b", "tests/test_gate_compatibility.py": "920811ebad0bf7f5c9d057d4396669ad340aae353e224d2e5dbc32ae94587b57" }, "schema_version": 1 diff --git a/formal/tests/test_gate.py b/formal/tests/test_gate.py index e031f0b9b..e36177a1f 100644 --- a/formal/tests/test_gate.py +++ b/formal/tests/test_gate.py @@ -17,6 +17,57 @@ if spec and spec.loader and (ROOT / 'gate.py').exists(): spec.loader.exec_module(gate) +STD_AXIOMS = ('Classical.choice', 'Quot.sound', 'propext') + + +def neutral_overlay(root): + """Write an empty RF312 manifest overlay into a copied formal tree. + + The production overlay rebinds the compatibility files; these fixtures recompute every + binding from the copied bytes instead, so the effective manifest is exactly the one the + test edits. Call before hashing the tree, so the overlay file's own hash is bound too. + """ + (root / 'manifest.overlay.json').write_text(json.dumps({'files': {}, 'schema_version': 1})) + + +def encode_name(value): + return gate.encode(tuple(value) if isinstance(value, tuple) else tuple(value.split('.'))) + + +def inventory_transcript(root, manifest, injected=()): + """A clean compiled-environment inventory for the copied package, as the generated Lean + program would print it (RF-GATE-01 protocol), with optional injected constants. + + User theorems are placed in the module whose source declares them, exactly as the + source pre-check does; every other constant comes from the manifest's bound + `compiled_inventory`. `injected` entries are (kind, origin, module, name, extra_axioms) + with module/name as component tuples; a registered module's count grows with them. + """ + package = gate.PACKAGE + modules = [package + '.' + path[len(package) + 1:-5] for path in manifest['source_modules']] + constants, counts, definitions = [], {m: 0 for m in modules}, [] + for path, module in zip(manifest['source_modules'], modules): + for theorem in gate.grammar_check((root / path).read_text(), path, definitions): + constants.append(('theorem', 'user', encode_name(module), encode_name(package + '.' + theorem))) + counts[module] += 1 + for kind, origin, module, name in manifest['compiled_inventory']: + constants.append((kind, origin, encode_name(module), encode_name(name))) + counts[module] += 1 + axioms = set(STD_AXIOMS) + for kind, origin, module, name, extra in injected: + constants.append((kind, origin, gate.encode(module), gate.encode(name))) + rendered = '.'.join(str(c) for c in module) + if rendered in counts: + counts[rendered] += 1 + axioms.update(extra) + lines = ['MODULE|' + encode_name(m) + '|false|' + str(counts[m]) + '|0' for m in modules] + lines.append('MODULE|' + encode_name(package) + '|false|0|0') + lines.extend('INV|' + '|'.join(c) for c in constants) + lines.append('AXIOMS|' + ','.join(sorted(encode_name(a) for a in axioms))) + lines.append('CLOSURE|' + str(len(constants) + 1000)) + return '\n'.join(lines) + '\n' + + class GateTests(unittest.TestCase): def setUp(self): self.assertTrue(hasattr(gate, 'audit_axioms'), 'formal evidence gate not implemented') @@ -109,6 +160,7 @@ def fixture(self): self.addCleanup(self.tmp.cleanup) p = Path(self.tmp.name) / 'formal' shutil.copytree(ROOT, p, ignore=shutil.ignore_patterns('.lake', '__pycache__')) + neutral_overlay(p) self.previous = gate.ROOT; gate.ROOT = p self.addCleanup(lambda: setattr(gate, 'ROOT', self.previous)) self.m = json.loads((p/'manifest.json').read_text()) @@ -130,6 +182,34 @@ def test_root_declaration_cannot_evade_audit(self): root.write_text(root.read_text()+'theorem hidden : False := by sorry\n') self.m['files']['ResearchFormalCoreR1.lean']=hashlib.sha256(root.read_bytes()).hexdigest(); self.save() with self.assertRaises(ValueError): gate.source_check() + def test_rebound_root_redirects_source_overlay_and_evidence_reads(self): + # RF312 regression: rebinding `gate.ROOT` must redirect every read of the layered gate -- + # the helper's manifest and source checks, the overlay, and the execution evidence + # directory -- not only this wrapper's own reads (dot's failure map, Math-#312). + p = self.fixture(); self.save() + + def overlay(files): + path = p / 'manifest.overlay.json' + path.write_text(json.dumps({'schema_version': 1, 'files': files})) + self.m['files']['manifest.overlay.json'] = hashlib.sha256(path.read_bytes()).hexdigest() + self.save() + + _, digest = gate.source_check() + self.assertEqual(Path(gate._core.ROOT), p, 'helper still reads the original tree') + self.assertEqual(Path(gate.OVERLAY), p / 'manifest.overlay.json') + # A wrong binding written only into the copy's overlay is seen, so the overlay read moved. + overlay({'README.md': '0' * 64}) + with self.assertRaisesRegex(ValueError, 'source hash mismatch: README.md'): + gate.source_check() + # The digest binds the overlay bytes that were actually read. + overlay({'README.md': hashlib.sha256((p / 'README.md').read_bytes()).hexdigest()}) + _, other = gate.source_check() + self.assertNotEqual(digest, other) + # A source edit made only in the copy is seen by the helper's own file check. + (p / 'README.md').write_bytes((p / 'README.md').read_bytes() + b'\n') + with self.assertRaisesRegex(ValueError, 'source hash mismatch: README.md'): + gate.source_check() + def test_duplicate_json_rejected(self): with self.assertRaises(ValueError): gate.load_json('{"a":1,"a":2}') def test_json_object(self): @@ -145,6 +225,7 @@ def fixture(self): self.addCleanup(tmp.cleanup) self.root = Path(tmp.name) / 'formal' shutil.copytree(ROOT, self.root, ignore=shutil.ignore_patterns('.lake', '__pycache__')) + neutral_overlay(self.root) previous = gate.ROOT gate.ROOT = self.root self.addCleanup(lambda: setattr(gate, 'ROOT', previous)) @@ -162,15 +243,51 @@ def fixture(self): def execute_synthetic(self, versions, version_exit=0, mutate_source=False, wrong_dependency=False, dirty_dependency=None, drift_dependency=False, env=None): + # The layered gate observes the running Lean three times: the wrapper's preflight, the + # reviewed helper's own check after its controls, and the wrapper's final observation. + # `versions` keeps its two-entry meaning: [preflight, final]; the helper's read sees the + # preflight release (nothing changes it during the helper's own execution). + schedule = [versions[0], versions[0], versions[-1]] + package = gate.PACKAGE + first_module = self.manifest['source_modules'][0] + local = gate.LOCAL_MODULE + admissions = { + 'hidden_sorry': [('theorem', 'user', local, ('_private', 'fixture', 0, package, 'hiddenAdmission'), ('sorryAx',))], + 'hidden_theorem': [('theorem', 'user', local, (package, 'indentedAdmission'), ())], + 'hidden_def': [('def', 'user', local, (package, 'unusedAdmission'), ('sorryAx',))], + 'outside_namespace': [('theorem', 'user', local, ('outsideNamespaceAdmission',), ())], + } + + def effective_root(kwargs): + cwd = kwargs.get('cwd') + if cwd is not None and Path(cwd).resolve().is_relative_to(self.root.resolve()): + return Path(cwd) + return self.root + + def compiled_injections(package_root): + # The compiled-admission copy carries the injected declaration in its first module. + text = (package_root / first_module).read_text() + module = tuple((package + '.' + first_module[len(package) + 1:-5]).split('.')) + found = [] + for label, declaration, (kind, origin, name, private), expected in gate.COMPILED_INJECTIONS: + if declaration in text: + full = ('_private', 'fixture', 0) + name if private else name + extra = ('sorryAx',) if 'sorry' in declaration else () + found.append((kind, origin, module, full, extra)) + return found + def external_run(command, **kwargs): self.calls.append(command) if command == ['lake', 'env', 'lean', '--version']: - text = versions[min(self.version_index, len(versions) - 1)] + '\n' + text = schedule[min(self.version_index, len(schedule) - 1)] + '\n' self.version_index += 1 return subprocess.CompletedProcess(command, version_exit, text) - if command == ['lake', 'build'] and mutate_source: - path = self.root / 'lake-manifest.json' - path.write_bytes(path.read_bytes() + b'\n') + if command == ['lake', 'build']: + # A synthetic build leaves a build directory, as the real one does. + (effective_root(kwargs) / '.lake' / 'build').mkdir(parents=True, exist_ok=True) + if mutate_source: + path = self.root / 'lake-manifest.json' + path.write_bytes(path.read_bytes() + b'\n') label = Path(command[-1]).stem if label == 'Audit': text = '\n'.join("'" + n + "' does not depend on any axioms" for n in self.manifest['targets']) @@ -179,6 +296,12 @@ def external_run(command, **kwargs): elif label in ('sorry', 'custom_imported', 'native'): forbidden = {'sorry': 'sorryAx', 'custom_imported': 'hiddenPremise', 'native': 'injected._nativeDecide_1'}[label] text = "'injected' depends on axioms: [" + forbidden + ']' + elif label == 'Inventory': + # User theorems come from the fixture's own (uninjected) sources; the compiled- + # admission copy differs only by the declaration it injected, read from `cwd`. + text = inventory_transcript(self.root, self.manifest, compiled_injections(effective_root(kwargs))) + elif label in admissions: + text = inventory_transcript(self.root, self.manifest, admissions[label]) else: text = '' return subprocess.CompletedProcess(command, 0, text) @@ -239,7 +362,7 @@ def test_other_commit_after_execution_refuses_receipt(self): wrong = 'Lean (version 4.34.1, x86_64-unknown-linux-gnu, commit ' + 'f' * 40 + ', Release)' with self.assertRaisesRegex(ValueError, 'unexpected running Lean version'): self.execute_synthetic([self.RELEASE, wrong]) - self.assertEqual(self.version_index, 2) + self.assertEqual(self.version_index, 3) # preflight, the helper's own check, final self.assertFalse(self.sentinel.exists()) self.assert_no_receipt() @@ -265,7 +388,7 @@ def test_success_checks_before_build_and_after_controls_and_binds_both_logs(self self.assertEqual(self.calls[0], ['lake', 'env', 'lean', '--version']) self.assertEqual(self.calls[1], ['lake', 'build']) self.assertEqual(self.calls[-1], ['lake', 'env', 'lean', '--version']) - self.assertEqual(self.version_index, 2) + self.assertEqual(self.version_index, 3) # preflight, the helper's own check, final self.assertFalse(self.sentinel.exists()) self.assertEqual(receipt['lean_version'], self.RELEASE) self.assertEqual(receipt['manifest_sha256'], self.digest) @@ -280,7 +403,7 @@ def test_version_change_after_execution_refuses_receipt(self): self.fixture() with self.assertRaisesRegex(ValueError, 'unexpected running Lean version'): self.execute_synthetic([self.RELEASE, self.RELEASE.replace('4.34.1', '4.34.10')]) - self.assertEqual(self.version_index, 2) + self.assertEqual(self.version_index, 3) # preflight, the helper's own check, final self.assert_no_receipt() def test_source_change_during_build_still_refuses_receipt(self): @@ -326,7 +449,7 @@ def test_dependency_dirtied_during_execution_refuses_receipt(self): self.fixture() with self.assertRaisesRegex(ValueError, 'dependency worktree is not clean'): self.execute_synthetic([self.RELEASE, self.RELEASE], dirty_dependency='final') - self.assertEqual(self.version_index, 2) + self.assertEqual(self.version_index, 3) # the wrapper's final observation sees the dirty tree self.assert_no_receipt() def test_dependency_moved_during_execution_refuses_receipt(self): From f0c34c57a0d1ec6a55ea6467d5df019a3af4e851 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:06:57 -0500 Subject: [PATCH 33/38] fix(formal): bind declared RF312 manifest overlay receipts --- .../plans/2026-10-28-rf312-receipt-binding.md | 44 +++++ formal/manifest.json | 4 + tests/test_required_formal_check.py | 158 ++++++++++++++++++ tools/required_formal_check.py | 31 +++- 4 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-28-rf312-receipt-binding.md diff --git a/docs/superpowers/plans/2026-10-28-rf312-receipt-binding.md b/docs/superpowers/plans/2026-10-28-rf312-receipt-binding.md new file mode 100644 index 000000000..bf0113c80 --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-rf312-receipt-binding.md @@ -0,0 +1,44 @@ +# RF312 receipt binding implementation plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans for this single task, followed by one fresh whole-branch engineering review. + +**Goal:** Make the current required-check consumer bind the producer's exact base-manifest plus overlay identity while retaining explicitly non-overlay legacy behavior. + +**Architecture:** Add a fixed opt-in declaration to the base manifest. The consumer derives one identity from that declaration and the original bytes; it never accepts whichever of raw/composite happens to match. Preserve the producer, helper, overlay and every mathematical source byte. + +**Tech Stack:** Python standard library; existing unittest suites; no new dependency. + +**Spec:** [DCL-RF312-BIND-001](https://github.com/d6g8k5htny-coder/Math-/pull/312#pullrequestreview-5658582326), [current source reconciliation](https://github.com/d6g8k5htny-coder/Math-/pull/312#issuecomment-6467595927), and [actual new author pickup](https://github.com/d6g8k5htny-coder/Math-/pull/312#issuecomment-6468228926). + +## Global constraints + +- Base source head: `66d66e43e3f3f58835bc938717dacf8532aaffc3`; current Math Main `4a0d51bbd06f0d9ac731a1d1df38f25999797135` is a separate integration cut. +- Four paths only: this plan, `formal/manifest.json`, `tools/required_formal_check.py`, `tests/test_required_formal_check.py`. +- Exact declaration: `"source_identity": {"schema": "rf312-overlay-v1", "overlay": "manifest.overlay.json"}`. +- Declared mode hashes `manifest_bytes + b'\0RF312-OVERLAY\0' + overlay_bytes` exactly as the unchanged producer does. +- Legacy mode requires both declaration absence and reserved overlay absence; explicit null, unknown contracts and undeclared overlays refuse. +- Overlay must be the fixed regular non-symlink sibling and strict JSON with exactly `schema_version` (integer1) and `files` (string-to-lowercase-64hex bindings). +- Preserve checked commit/run/attempt, clean dependency locks, both runtime observations, receipt/log bytes and all existing acceptance predicates. +- No proof/Lean/source-target/producer/helper/overlay/workflow/status changes, no real Lean or kernel execution, no CI request, Ready or integration. Existing unit fixtures may invoke only copied-tree metadata CLI validation and synthetic runtime responders. +- Three D2 companion pin/raw-receipt holds remain separate and unchanged. Old reviews/receipts keep their original identities; predecessor closeouts remain UNKNOWN. + +## Review focus + +- Legacy raw receipts must remain valid only without any declaration or reserved overlay. +- Unknown/null/extra/path-changing identity declarations and duplicate/invalid JSON must refuse. +- Missing/dangling/symlink/directory overlays must refuse before reading any target. +- Raw/composite source drift and a raw-receipt downgrade must fail without rewriting the original receipt. +- The bridge regression must call the actual producer source-check and actual consumer; its runtime receipt remains an explicitly synthetic fixture, not kernel evidence. + +## Task1: Reconcile source identity at the receipt boundary + +**Files:** the four exact paths above. + +**Interfaces:** Keep `bind_receipt(receipt: Path, manifest: Path, env: dict, git_head: str) -> dict` unchanged. Add internal `manifest_identity(manifest: Path, raw: bytes) -> str`; reuse existing strict JSON validation, context checks and dependency-lock validation with the same captured base bytes. + +- [ ] Run clean existing consumer67 and formal244 unittest suites in normal and optimized modes. Expected: all pass. +- [ ] Add an actual-producer→fixture-receipt→consumer success test and direct malformed/downgrade/refusal controls. Run before production edits. Expected: the bridge fails with the original manifest/receipt mismatch and weak old-consumer cases are exposed. +- [ ] Add only the fixed declaration to the base manifest and implement strict identity selection in the consumer. No raw fallback in the overlay regime. +- [ ] Run the new consumer scope and both complete affected suites in normal and optimized modes. Expected: all existing/new methods pass; no real Lean, dependency/network or mathematical execution. +- [ ] Verify all old manifest fields and every non-owned source/overlay/helper/Lean/workflow byte remain exact; run whitespace validation and record the new raw/composite identity. +- [ ] Commit, obtain separate exact-tree engineering review, then publish at most one expected-old-head guarded fast-forward amendment to existing#312. Keep Draft and release only this source role. Fresh current-base/Ready/hosted qualification and integration remain separate. diff --git a/formal/manifest.json b/formal/manifest.json index adc977043..894b030e3 100644 --- a/formal/manifest.json +++ b/formal/manifest.json @@ -1,4 +1,8 @@ { + "source_identity": { + "schema": "rf312-overlay-v1", + "overlay": "manifest.overlay.json" + }, "schema_version": 1, "scientific_effect": "NONE", "formalization_status": "proved", diff --git a/tests/test_required_formal_check.py b/tests/test_required_formal_check.py index e9d2e6f39..80961d07a 100644 --- a/tests/test_required_formal_check.py +++ b/tests/test_required_formal_check.py @@ -5,6 +5,7 @@ import json import os from pathlib import Path +import shutil import subprocess import sys import tempfile @@ -586,3 +587,160 @@ def test_manifest_without_exact_lock_refused(self): for name in ('preflight', 'final'): self.record['observations'][name]['manifest_sha256'] = self.record['manifest_sha256'] self.refused('manifest has no exact dependency lock') + +class LayeredManifestBindingTests(unittest.TestCase): + """Real producer/consumer boundary and fail-closed overlay identity regimes.""" + setUp = RequiredCheckTests.setUp + write_receipt = RequiredCheckTests.write_receipt + binding = RequiredCheckTests.binding + CONTRACT = {'schema': 'rf312-overlay-v1', 'overlay': 'manifest.overlay.json'} + + def identity(self, digest): + self.record['manifest_sha256'] = digest + for observed in self.record['observations'].values(): + observed['manifest_sha256'] = digest + self.write_receipt() + + def layered(self): + document = json.loads(self.manifest.read_text()) + document['source_identity'] = dict(self.CONTRACT) + self.manifest.write_text(json.dumps(document) + '\n') + self.overlay = self.manifest.with_name('manifest.overlay.json') + self.overlay.write_text('{"schema_version":1,"files":{}}\n') + self.composite_receipt() + + def composite_receipt(self): + # Hand-derived contract bytes, independent of the consumer implementation. + self.identity(hashlib.sha256( + self.manifest.read_bytes() + b'\0RF312-OVERLAY\0' + + self.overlay.read_bytes()).hexdigest()) + + def test_actual_producer_source_check_receipt_binds(self): + # Exercise the actual source validator, without calling execute or any Lean process. + formal = self.root / 'formal' + shutil.copytree(ROOT / 'formal', formal, + ignore=shutil.ignore_patterns('.lake', '__pycache__', '*.pyc')) + self.manifest = formal / 'manifest.json' + document = json.loads(self.manifest.read_text()) + document['source_identity'] = dict(self.CONTRACT) + self.manifest.write_text(json.dumps(document) + '\n') + prior_helper = sys.modules.pop('gate_helper', None) + try: + spec = importlib.util.spec_from_file_location('receipt_binding_producer', ROOT / 'formal/gate.py') + producer = importlib.util.module_from_spec(spec) + spec.loader.exec_module(producer) + producer.ROOT = formal + checked, digest = producer.source_check() + finally: + if prior_helper is not None: + sys.modules['gate_helper'] = prior_helper + self.record['repository'] = self.env['GITHUB_REPOSITORY'] = 'd6g8k5htny-coder/Math-' + self.record['dependency_revisions'] = dict(checked['dependency_revisions']) + for observed in self.record['observations'].values(): + observed['dependency_revisions'] = dict(checked['dependency_revisions']) + self.identity(digest) + before = self.receipt.read_bytes() + result = self.binding() + self.assertEqual(result['checked_commit'], 'a' * 40) + self.assertEqual(result['receipt_sha256'], hashlib.sha256(before).hexdigest()) + self.assertEqual(self.receipt.read_bytes(), before) + + def test_empty_files_overlay_receipt_binds(self): + self.layered() + self.assertEqual(self.binding()['run_attempt'], '2') + + def test_raw_or_overlay_byte_drift_refused(self): + self.layered() + for path in (self.manifest, self.overlay): + with self.subTest(path=path.name): + original = path.read_bytes() + path.write_bytes(original + b' ') + try: + with self.assertRaisesRegex(ValueError, 'manifest/receipt mismatch'): + self.binding() + finally: + path.write_bytes(original) + + def test_missing_symlink_dangling_and_nonregular_overlay_refused(self): + self.layered() + original = self.overlay.read_bytes() + target = self.root / 'overlay-target.json' + target.write_bytes(original) + for kind in ('missing', 'symlink', 'dangling', 'directory'): + with self.subTest(kind=kind): + self.overlay.unlink() + if kind == 'symlink': + self.overlay.symlink_to(target) + elif kind == 'dangling': + self.overlay.symlink_to(self.root / 'absent-overlay-target.json') + elif kind == 'directory': + self.overlay.mkdir() + try: + with self.assertRaises((ValueError, OSError)): + self.binding() + finally: + if self.overlay.is_dir() and not self.overlay.is_symlink(): + self.overlay.rmdir() + elif self.overlay.is_symlink(): + self.overlay.unlink() + self.overlay.write_bytes(original) + + def test_overlay_strict_json_and_binding_shape_refused(self): + self.layered() + for raw in ( + b'{', b'[]', b'null', b'{"schema_version":1,"files":{},"files":{}}', + b'{"schema_version":NaN,"files":{}}', + b'{"schema_version":true,"files":{}}', + b'{"schema_version":2,"files":{}}', + b'{"schema_version":1,"files":[]}', + b'{"schema_version":1,"files":{"gate.py":"invalid"}}', + b'{"schema_version":1,"files":{},"targets":[]}', b'\xff', + ): + with self.subTest(raw=raw): + self.overlay.write_bytes(raw) + self.composite_receipt() + with self.assertRaises(ValueError): + self.binding() + + def test_unknown_identity_contract_never_becomes_legacy(self): + self.layered() + document = json.loads(self.manifest.read_text()) + overlay_bytes = self.overlay.read_bytes() + for value in ( + None, {}, [], 'rf312-overlay-v1', + {'schema': 'rf312-overlay-v2', 'overlay': 'manifest.overlay.json'}, + {'schema': 'rf312-overlay-v1', 'overlay': '../manifest.overlay.json'}, + {'schema': 'rf312-overlay-v1', 'overlay': '/tmp/manifest.overlay.json'}, + {'schema': 'rf312-overlay-v1', 'overlay': 'manifest.overlay.json', 'extra': True}, + ): + for present in (False, True): + with self.subTest(contract=value, overlay_present=present): + document['source_identity'] = value + self.manifest.write_text(json.dumps(document) + '\n') + if present: + self.overlay.write_bytes(overlay_bytes) + else: + self.overlay.unlink(missing_ok=True) + # A raw receipt must not make an invalid regime acceptable. + self.identity(hashlib.sha256(self.manifest.read_bytes()).hexdigest()) + with self.assertRaises(ValueError): + self.binding() + + def test_undeclared_overlay_refused_with_otherwise_valid_raw_receipt(self): + self.manifest.with_name('manifest.overlay.json').write_text( + '{"schema_version":1,"files":{}}\n') + with self.assertRaisesRegex(ValueError, 'undeclared manifest overlay'): + self.binding() + + def test_raw_receipt_cannot_downgrade_declared_overlay(self): + self.layered() + self.identity(hashlib.sha256(self.manifest.read_bytes()).hexdigest()) + with self.assertRaisesRegex(ValueError, 'manifest/receipt mismatch'): + self.binding() + + def test_missing_declared_overlay_cannot_downgrade_to_raw(self): + self.layered() + self.overlay.unlink() + self.identity(hashlib.sha256(self.manifest.read_bytes()).hexdigest()) + with self.assertRaisesRegex(ValueError, 'manifest overlay'): + self.binding() diff --git a/tools/required_formal_check.py b/tools/required_formal_check.py index ea5af2c89..0b9d694bd 100644 --- a/tools/required_formal_check.py +++ b/tools/required_formal_check.py @@ -68,6 +68,32 @@ def runtime_observation(value, lock: dict, name: str) -> dict: return value +def manifest_identity(manifest: Path, raw: bytes) -> str: + """Select one declared source identity; never downgrade an overlay receipt.""" + document = strict_json(raw.decode('utf-8')) + require(isinstance(document, dict), 'manifest must be an object') + overlay = manifest.with_name('manifest.overlay.json') + if 'source_identity' not in document: + require(not overlay.exists() and not overlay.is_symlink(), 'undeclared manifest overlay') + return hashlib.sha256(raw).hexdigest() + require(document['source_identity'] == { + 'schema': 'rf312-overlay-v1', 'overlay': 'manifest.overlay.json'}, + 'unsupported source identity contract') + require(not overlay.is_symlink() and overlay.is_file(), + 'missing or nonregular manifest overlay') + overlay_raw = overlay.read_bytes() + value = strict_json(overlay_raw.decode('utf-8')) + require(isinstance(value, dict) and set(value) == {'schema_version', 'files'}, + 'invalid manifest overlay keys') + require(type(value['schema_version']) is int and value['schema_version'] == 1 + and isinstance(value['files'], dict), 'invalid manifest overlay schema') + require(all(isinstance(name, str) and isinstance(digest, str) + and re.fullmatch(r'[0-9a-f]{64}', digest) is not None + for name, digest in value['files'].items()), + 'invalid manifest overlay file binding') + return hashlib.sha256(raw + b'\0RF312-OVERLAY\0' + overlay_raw).hexdigest() + + def bind_receipt(receipt: Path, manifest: Path, env: dict, git_head: str) -> dict: ctx = context(env) require(git_head == ctx['checked_commit'], 'checkout/tested commit mismatch') @@ -81,12 +107,13 @@ def bind_receipt(receipt: Path, manifest: Path, env: dict, git_head: str) -> dic ('formalization_status', 'kernel-checked'), ('scientific_effect', 'NONE')]: require(record.get(key) == expected, 'receipt mismatch: '+key) - require(record.get('manifest_sha256') == hashlib.sha256(manifest.read_bytes()).hexdigest(), + manifest_raw = manifest.read_bytes() + require(record.get('manifest_sha256') == manifest_identity(manifest, manifest_raw), 'manifest/receipt mismatch') # The producer's runtime fields are validated, not only hashed: the exact release, the # locked and clean dependencies, this attempt, and equal observations before the first # and after the last Lean execution. A missing or older-schema field refuses. - lock = dependency_lock(manifest.read_bytes()) + lock = dependency_lock(manifest_raw) require(record.get('workflow_run_attempt') == ctx['run_attempt'], 'receipt mismatch: workflow_run_attempt') require(isinstance(record.get('lean_version'), str) and LEAN_RELEASE.fullmatch(record['lean_version']) is not None, 'receipt runtime is not the pinned Lean release') From 80946e1cf311b3cbbb051bb3662357b11f8b3d75 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:20:28 -0500 Subject: [PATCH 34/38] test(formal): use the actual production identity declaration --- tests/test_required_formal_check.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_required_formal_check.py b/tests/test_required_formal_check.py index 80961d07a..6163fb182 100644 --- a/tests/test_required_formal_check.py +++ b/tests/test_required_formal_check.py @@ -621,9 +621,6 @@ def test_actual_producer_source_check_receipt_binds(self): shutil.copytree(ROOT / 'formal', formal, ignore=shutil.ignore_patterns('.lake', '__pycache__', '*.pyc')) self.manifest = formal / 'manifest.json' - document = json.loads(self.manifest.read_text()) - document['source_identity'] = dict(self.CONTRACT) - self.manifest.write_text(json.dumps(document) + '\n') prior_helper = sys.modules.pop('gate_helper', None) try: spec = importlib.util.spec_from_file_location('receipt_binding_producer', ROOT / 'formal/gate.py') From b0ac8988c346e986a4294e5b9bff7e86650211c9 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:50:19 -0500 Subject: [PATCH 35/38] fix(d2): bind composite core receipts to qualified source cut Qualify fixed formal source and byte-bound receipt helper in all three D2 consumers; preserve mathematical payloads and historical evidence. Source engineering only; no scientific or kernel qualification. --- companions/d2_atom_integral_20261006/SCOPE.md | 24 +++ .../SOURCE_FILES.json | 20 +-- companions/d2_atom_integral_20261006/check.py | 52 ++++++- .../d2_atom_integral_20261006/test_check.py | 137 +++++++++++++++++ .../d2_moment_bridge_20261006/README.md | 24 +++ .../SOURCE_FILES.json | 20 +-- companions/d2_moment_bridge_20261006/check.py | 54 ++++++- .../d2_moment_bridge_20261006/test_bridge.py | 139 +++++++++++++++++- companions/d2_square_schur_20261006/SCOPE.md | 24 +++ .../SOURCE_FILES.json | 20 +-- companions/d2_square_schur_20261006/check.py | 52 ++++++- .../d2_square_schur_20261006/test_check.py | 137 +++++++++++++++++ ...26-10-28-d2-composite-receipt-consumers.md | 43 ++++++ 13 files changed, 696 insertions(+), 50 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-28-d2-composite-receipt-consumers.md diff --git a/companions/d2_atom_integral_20261006/SCOPE.md b/companions/d2_atom_integral_20261006/SCOPE.md index 71008894a..a5b523d5c 100644 --- a/companions/d2_atom_integral_20261006/SCOPE.md +++ b/companions/d2_atom_integral_20261006/SCOPE.md @@ -174,3 +174,27 @@ bytes. The new tests are synthetic and do not execute Lean. New current-commit kernel receipts and hosted checks are still required after publication. Alignment remains PENDING_INDEPENDENT_REVIEW; scientific effect NONE. + +## RF312 source/receipt engineering proposal, 28 October 2026 + +This dated private proposal preserves the mathematical text and historical evidence above. +Its source basis is RF312 `80946e1cf311b3cbbb051bb3662357b11f8b3d75`, formal tree +`8922fd8c833df0c52d4a8c7b5c3ad802b93098d4`, with the unchanged nine formal Lean/root +blobs, 63 target entries, source modules, dependencies, toolchain and companion Lean/contract bytes. +These are source identities and declared dependencies, not a fresh proof or applicability review. + +The raw manifest SHA256 is `ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062`; +the core receipt instead binds composite source `481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066`. +The fixed strict RF312 overlay contract must match; no raw-receipt fallback is admitted. +The reused identity validator lies outside the formal tree, at `tools/required_formal_check.py` +(blob `0b9d694bdf888c75826ca07ae176c5a5c43b8880`, SHA256 +`6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc`). Its source bytes are +checked before those verified bytes are evaluated; drift, symlink, missing overlay and wrong regimes refuse. +Its identity is also retained in the source snapshot across execution. Atom's two imported companion +Lean pins and all existing runtime re-observation checks are preserved. + +Protocol unit fixtures are synthetic and execute no Lean or finite-law diagnostic. They do not +supply a core or companion kernel/runtime receipt. Existing receipts/reviews remain historical; +fresh actual current-composition core and all three D2 hosted/negative-control/runtime checks, +qualified review and separate integration remain required. Scientific effect NONE; alignment and +scientific acceptance are unchanged. No existing312 source/ref or integration custody follows. diff --git a/companions/d2_atom_integral_20261006/SOURCE_FILES.json b/companions/d2_atom_integral_20261006/SOURCE_FILES.json index 82953007d..4f562d4b1 100644 --- a/companions/d2_atom_integral_20261006/SOURCE_FILES.json +++ b/companions/d2_atom_integral_20261006/SOURCE_FILES.json @@ -1,5 +1,5 @@ { - "core_tree": "fc0b11706d064b9de42331ee0a2464865107b87b", + "core_tree": "8922fd8c833df0c52d4a8c7b5c3ad802b93098d4", "files": { ".github/workflows/d2-atom-integral.yml": { "bytes": 1942, @@ -17,14 +17,14 @@ "sha256": "247507c4b7aeaccc033cd4d648442172d61227bb27239a7dacccd0f2fa1ba6c3" }, "companions/d2_atom_integral_20261006/SCOPE.md": { - "bytes": 10928, - "git_blob": "71008894a5a3aa01da18163afbce1903dabe51df", - "sha256": "90f12484ead012929ce0664ed0d4ef90f40c2d75d39d2d8580115f19b2db40ec" + "bytes": 12754, + "git_blob": "a5b523d5c7a704f368fb51c58b15e0e3fbc926ac", + "sha256": "aa3e0d19cda6a032384a00aa994950a1cf14e94143d5bc0400a4210acd7bddb2" }, "companions/d2_atom_integral_20261006/check.py": { - "bytes": 10044, - "git_blob": "eb918b7ee775c0bb61a9ffff2cadba0b76e40673", - "sha256": "df3f8aec73478b74d0d0f2f06d2655097f80b9d9ebbb4c720d6a63076493315d" + "bytes": 11895, + "git_blob": "791ce1ddd1cb2df075a118f061922d323e42ac3d", + "sha256": "14c432a63a2cc9c74d3698be87927fa5e518e64b62a670ac0a3b579095e77749" }, "companions/d2_atom_integral_20261006/replay.sh": { "bytes": 3471, @@ -32,9 +32,9 @@ "sha256": "89d9a0c64fd88aa8cf8c6614df84b3f543c3d4ecc87ad2e529feffda90091c75" }, "companions/d2_atom_integral_20261006/test_check.py": { - "bytes": 8946, - "git_blob": "efa3793f8ffeecc3e504b559601d52c6e68008f0", - "sha256": "83750832498ac74f96297174c494262537069c9c82eacb7aa2315f39a77ad454" + "bytes": 15691, + "git_blob": "a73d12ecd2a4205a6e0876068a35a2c5f6a71a39", + "sha256": "8a35474c02fb88b249816935370fdb979eaeaf67183e9dcbd70aac261705837b" } }, "scientific_effect": "NONE", diff --git a/companions/d2_atom_integral_20261006/check.py b/companions/d2_atom_integral_20261006/check.py index eb918b7ee..791ce1ddd 100644 --- a/companions/d2_atom_integral_20261006/check.py +++ b/companions/d2_atom_integral_20261006/check.py @@ -18,9 +18,14 @@ PACKAGE='companions/d2_atom_integral_20261006' WORKFLOW='.github/workflows/d2-atom-integral.yml' REPO='d6g8k5htny-coder/Math-' -CORE_TREE='fc0b11706d064b9de42331ee0a2464865107b87b' -CORE_MANIFEST='7149915669414e22f593ce2c6540c5e10d922eb758c7464794fc64764e679778' -CORE_GATE='25e08d9054b929dd657668e4ee4a3524688dc476e5af958f950de3b919f04e9a' +CORE_TREE='8922fd8c833df0c52d4a8c7b5c3ad802b93098d4' +CORE_MANIFEST='ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062' +CORE_GATE='509b6eb3b5ee2dc7e42810a40b07192b0feaf3924c9dbb398db43cb44e16e731' + +CORE_SOURCE_IDENTITY='481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066' +CORE_IDENTITY_PATH='tools/required_formal_check.py' +CORE_IDENTITY_SHA256='6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc' +CORE_IDENTITY_BLOB='0b9d694bdf888c75826ca07ae176c5a5c43b8880' CONSUMED={ 'companions/d2_moment_bridge_20261006/D2MomentBridge.lean':'b3184107e290189b91e3e18fd2aa1cb631bb9bde', 'companions/d2_square_schur_20261006/D2SquareSchur.lean':'4855c92d55254969e1de7f8cf8dec2bdc027cf69'} @@ -54,6 +59,38 @@ def reject(_):raise ValueError('noninteger JSON number') def git(*args): return subprocess.run(['git',*args],cwd=ROOT,check=True,capture_output=True,text=True,timeout=30).stdout.strip() +def core_identity(): + """Bind the outside-tree validator before evaluating its verified bytes.""" + path = ROOT / CORE_IDENTITY_PATH + require(not any(p.is_symlink() for p in (path, *path.parents)) and path.is_file(), + 'core identity validator missing or symlink') + raw = path.read_bytes() + framed = b'blob ' + str(len(raw)).encode() + b'\0' + raw + require(sha(raw) == CORE_IDENTITY_SHA256 + and hashlib.sha1(framed).hexdigest() == CORE_IDENTITY_BLOB, + 'core identity validator changed') + namespace = {'__name__': 'bound_d2_core_identity', '__file__': str(path)} + exec(compile(raw, str(path), 'exec'), namespace) + manifest = ROOT / 'formal/manifest.json' + manifest_raw = manifest.read_bytes() + require(sha(manifest_raw) == CORE_MANIFEST, 'core manifest changed') + digest = namespace['manifest_identity'](manifest, manifest_raw) + require(digest == CORE_SOURCE_IDENTITY, 'core source identity changed') + return digest + + +def validate_core_receipt(core, current): + """The current core receipt binds the composite source, not the raw manifest.""" + require(isinstance(core, dict), 'invalid core receipt') + require(core.get('checked_commit') == current['checked_commit'], + 'core executed different source') + require(core.get('manifest_sha256') == core_identity(), 'core receipt manifest') + require(core.get('formalization_status') == 'kernel-checked', 'no core kernel receipt') + + + + + def source(): meta=strict((SIDE/'SOURCE_FILES.json').read_bytes());head=git('rev-parse','HEAD') require(set(meta)=={'version','scientific_effect','core_tree','targets','files'},'manifest schema') @@ -64,6 +101,7 @@ def source(): require(git('rev-parse',head+':formal')==CORE_TREE,'core tree changed') require(sha((ROOT/'formal/manifest.json').read_bytes())==CORE_MANIFEST,'core manifest changed') require(sha((ROOT/'formal/gate.py').read_bytes())==CORE_GATE,'core verifier changed') + core_identity() if os.environ.get('GITHUB_ACTIONS')=='true': require(os.environ.get('GITHUB_SHA')==head and os.environ.get('GITHUB_REPOSITORY')==REPO,'host/source mismatch') require(os.environ.get('GITHUB_RUN_ID') and os.environ.get('GITHUB_RUN_ATTEMPT'),'missing run identity') @@ -78,7 +116,10 @@ def source(): require(git('rev-parse',head+':'+name)==ids[name]['git_blob'],'dirty source: '+name) if name in PAYLOADS:require(meta['files'][name]==ids[name],'source identity: '+name) if name in CONSUMED:require(CONSUMED[name]==ids[name]['git_blob'],'consumed source changed: '+name) - git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW,*CONSUMED) + helper_id=identity((ROOT/CORE_IDENTITY_PATH).read_bytes()) + require(git('rev-parse',head+':'+CORE_IDENTITY_PATH)==helper_id['git_blob'],'dirty core identity validator') + ids[CORE_IDENTITY_PATH]=helper_id + git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW,CORE_IDENTITY_PATH,*CONSUMED) return {'checked_commit':head,'repository':REPO,'run_id':os.environ.get('GITHUB_RUN_ID','local'), 'run_attempt':os.environ.get('GITHUB_RUN_ATTEMPT','local'),'core_tree':CORE_TREE, 'sources':ids,'declarations':list(TARGETS),'scientific_effect':'NONE'} @@ -149,8 +190,7 @@ def finish(out): require((out/(label+'.status')).read_bytes()==b'1\n','negative status: '+label) negative((out/(label+'.log')).read_text(),label,1) raw=(ROOT/'formal/.lake/formal-evidence/receipt.json').read_bytes();core=strict(raw) - require(core['checked_commit']==current['checked_commit'] and core['manifest_sha256']==CORE_MANIFEST,'core identity') - require(core['formalization_status']=='kernel-checked','no core kernel receipt') + validate_core_receipt(core,current) final=final_runtime(out,core,strict((ROOT/'formal/manifest.json').read_bytes())['dependency_revisions']) result={**current,'axioms':ax,'theorem_count':6,'definition_count':0,'core_receipt_sha256':sha(raw),'final_observation':final, 'formalization_status':'kernel-checked','alignment_status':'PENDING_INDEPENDENT_REVIEW', diff --git a/companions/d2_atom_integral_20261006/test_check.py b/companions/d2_atom_integral_20261006/test_check.py index efa3793f8..a73d12ecd 100644 --- a/companions/d2_atom_integral_20261006/test_check.py +++ b/companions/d2_atom_integral_20261006/test_check.py @@ -150,4 +150,141 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +class CoreIdentityProtocolTests(unittest.TestCase): + """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" + def setUp(self): + import shutil + import subprocess + from unittest import mock + self._subprocess = subprocess + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = Path(self.tmp.name).resolve() + original = SIDE.parents[1] + shutil.copytree(original / 'formal', self.root / 'formal') + for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): + relative = Path('companions') / (package + '_20261006') + shutil.copytree(original / relative, self.root / relative) + workflow = Path('.github/workflows') / (package.replace('_', '-') + '.yml') + (self.root / workflow).parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(original / workflow, self.root / workflow) + (self.root / 'tools').mkdir() + shutil.copy2(original / 'tools/required_formal_check.py', + self.root / 'tools/required_formal_check.py') + for args in (('init', '-q'), ('add', '.'), + ('-c', 'user.name=protocol-fixture', + '-c', 'user.email=protocol@example.invalid', + 'commit', '-qm', 'copied exact source protocol fixture')): + subprocess.run(['git', '-C', str(self.root), *args], + check=True, capture_output=True) + self.head = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD'], text=True).strip() + actual_tree = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD:formal'], + text=True).strip() + self.assertEqual(actual_tree, '8922fd8c833df0c52d4a8c7b5c3ad802b93098d4') + self.context = {'checked_commit': self.head} + patch = mock.patch.dict('os.environ', {'GITHUB_ACTIONS': 'false'}) + patch.start() + self.addCleanup(patch.stop) + copied = self.root / 'companions' / SIDE.name / 'check.py' + spec = importlib.util.spec_from_file_location('copied_d2_protocol', copied) + self.c = importlib.util.module_from_spec(spec) + spec.loader.exec_module(self.c) + + def validator(self): + function = getattr(self.c, 'validate_core_receipt', None) + self.assertTrue(callable(function), 'production core receipt validator missing') + return function + + def good_receipt(self): + return {'checked_commit': self.head, + 'manifest_sha256': '481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066', + 'formalization_status': 'kernel-checked'} + + def test_source_accepts_exact_reviewed_core_without_lean(self): + try: + current = self.c.source() + except (ValueError, self._subprocess.CalledProcessError) as error: + self.fail('reviewed frozen source cut must bind: ' + str(error)) + self.assertEqual(current['checked_commit'], self.head) + self.assertIn('tools/required_formal_check.py', current['sources']) + + def test_actual_producer_to_receipt_validator(self): + producer_path = self.root / 'formal/gate.py' + spec = importlib.util.spec_from_file_location('copied_rf312_producer', producer_path) + producer = importlib.util.module_from_spec(spec) + spec.loader.exec_module(producer) + _, digest = producer.source_check() + self.assertEqual(digest, self.good_receipt()['manifest_sha256']) + self.validator()( + {'checked_commit': self.head, 'manifest_sha256': digest, + 'formalization_status': 'kernel-checked'}, self.context) + + def test_raw_digest_wrong_commit_and_non_kernel_receipts_refuse(self): + validate = self.validator() + raw = self.c.sha((self.root / 'formal/manifest.json').read_bytes()) + for key, value in (('manifest_sha256', raw), + ('checked_commit', '0' * 40), + ('formalization_status', 'source')): + with self.subTest(key=key): + bad = dict(self.good_receipt(), **{key: value}) + with self.assertRaises(ValueError): + validate(bad, self.context) + + def test_helper_drift_refuses_before_evaluating_changed_code(self): + helper = self.root / 'tools/required_formal_check.py' + helper.write_bytes(helper.read_bytes() + + b"\nraise RuntimeError('untrusted helper evaluated')\n") + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_helper_symlink_and_absence_refuse(self): + helper = self.root / 'tools/required_formal_check.py' + raw = helper.read_bytes() + helper.unlink() + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + outside = self.root / 'outside-validator.py' + outside.write_bytes(raw) + helper.symlink_to(outside) + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_manifest_overlay_and_core_file_drift_refuse(self): + changes = (('formal/manifest.json', b'\n'), + ('formal/manifest.overlay.json', b'\n'), + ('formal/ResearchFormalCoreR1/AlgebraV2.lean', b'\n')) + for relative, suffix in changes: + with self.subTest(path=relative): + path = self.root / relative + raw = path.read_bytes() + path.write_bytes(raw + suffix) + try: + with self.assertRaises((ValueError, self._subprocess.CalledProcessError)): + self.c.source() + finally: + path.write_bytes(raw) + + def test_missing_or_unknown_overlay_regime_never_downgrades(self): + validate = self.validator() + overlay = self.root / 'formal/manifest.overlay.json' + original = overlay.read_bytes() + overlay.unlink() + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(b'{"schema_version":2,"files":{}}') + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(original) + manifest = self.root / 'formal/manifest.json' + raw = manifest.read_bytes() + document = self.c.strict(raw) + document.pop('source_identity') + manifest.write_text(__import__('json').dumps(document)) + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + + + if __name__=='__main__':unittest.main(verbosity=2) diff --git a/companions/d2_moment_bridge_20261006/README.md b/companions/d2_moment_bridge_20261006/README.md index 005796b55..b60408210 100644 --- a/companions/d2_moment_bridge_20261006/README.md +++ b/companions/d2_moment_bridge_20261006/README.md @@ -200,3 +200,27 @@ bytes. The new tests are synthetic and do not execute Lean. New current-commit kernel receipts and hosted checks are still required after publication. Alignment remains PENDING_INDEPENDENT_REVIEW; scientific effect NONE. + +## RF312 source/receipt engineering proposal, 28 October 2026 + +This dated private proposal preserves the mathematical text and historical evidence above. +Its source basis is RF312 `80946e1cf311b3cbbb051bb3662357b11f8b3d75`, formal tree +`8922fd8c833df0c52d4a8c7b5c3ad802b93098d4`, with the unchanged nine formal Lean/root +blobs, 63 target entries, source modules, dependencies, toolchain and companion Lean/contract bytes. +These are source identities and declared dependencies, not a fresh proof or applicability review. + +The raw manifest SHA256 is `ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062`; +the core receipt instead binds composite source `481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066`. +The fixed strict RF312 overlay contract must match; no raw-receipt fallback is admitted. +The reused identity validator lies outside the formal tree, at `tools/required_formal_check.py` +(blob `0b9d694bdf888c75826ca07ae176c5a5c43b8880`, SHA256 +`6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc`). Its source bytes are +checked before those verified bytes are evaluated; drift, symlink, missing overlay and wrong regimes refuse. +Its identity is also retained in the source snapshot across execution. Atom's two imported companion +Lean pins and all existing runtime re-observation checks are preserved. + +Protocol unit fixtures are synthetic and execute no Lean or finite-law diagnostic. They do not +supply a core or companion kernel/runtime receipt. Existing receipts/reviews remain historical; +fresh actual current-composition core and all three D2 hosted/negative-control/runtime checks, +qualified review and separate integration remain required. Scientific effect NONE; alignment and +scientific acceptance are unchanged. No existing312 source/ref or integration custody follows. diff --git a/companions/d2_moment_bridge_20261006/SOURCE_FILES.json b/companions/d2_moment_bridge_20261006/SOURCE_FILES.json index b72d7de79..9bb2e275f 100644 --- a/companions/d2_moment_bridge_20261006/SOURCE_FILES.json +++ b/companions/d2_moment_bridge_20261006/SOURCE_FILES.json @@ -1,5 +1,5 @@ { - "core_tree": "fc0b11706d064b9de42331ee0a2464865107b87b", + "core_tree": "8922fd8c833df0c52d4a8c7b5c3ad802b93098d4", "definitions": [ "moment", "cubicResidual" @@ -16,14 +16,14 @@ "sha256": "cde3bbd96eb29eb9ab7ff1448ffab1f794fb2b80355ac618800155eea6ccc42f" }, "companions/d2_moment_bridge_20261006/README.md": { - "bytes": 12277, - "git_blob": "005796b5527601c1156d2deaa9c950adf253faf3", - "sha256": "16b5b7e07eaa2a047160c2a295333eea370644d8343b635d6d92a4556a33eff6" + "bytes": 14103, + "git_blob": "b6040821087c473dd7b65d84b83b3fb4b0572fc4", + "sha256": "49640108a670a37fd4322c16c544c0cbf868a4eee824b0eca41139f150c771a7" }, "companions/d2_moment_bridge_20261006/check.py": { - "bytes": 10232, - "git_blob": "867324d3829e35d503df993f1a69a159c4a664c0", - "sha256": "04310a141a4c042eb8d255bba2c073b9ec348460e6dc522b6d7189285acd2730" + "bytes": 12105, + "git_blob": "9ef3412bcd3ff4bde17e0cdbe48c8194a62004a4", + "sha256": "fa71312e4ffe26a5dae1d036bf7339802cfe57fb3d12d8ec405ed90a3fec0954" }, "companions/d2_moment_bridge_20261006/replay.sh": { "bytes": 2793, @@ -31,9 +31,9 @@ "sha256": "1e50e7ffc5c68b3771e32f2574d709d055a9b5b43ef1fa74bcaccfc14e912a68" }, "companions/d2_moment_bridge_20261006/test_bridge.py": { - "bytes": 11134, - "git_blob": "5bcc4628bf61f035f96f7a4faf6c796a38e05fe9", - "sha256": "cf257c0dd750c5cc108d5af5d61129531d7f57e2ed03c6a5ab8261964fb25469" + "bytes": 17879, + "git_blob": "a15a8cb9ba4c0cb57855aa3aaaf89582e1742be3", + "sha256": "3af2d8c332407cc5b632354dfe89064086d12f72234e426e68136a98a168aa82" } }, "schema": 1, diff --git a/companions/d2_moment_bridge_20261006/check.py b/companions/d2_moment_bridge_20261006/check.py index 867324d38..9ef3412bc 100644 --- a/companions/d2_moment_bridge_20261006/check.py +++ b/companions/d2_moment_bridge_20261006/check.py @@ -13,9 +13,14 @@ ROOT=SIDE.parents[1] PACKAGE='companions/d2_moment_bridge_20261006' WORKFLOW='.github/workflows/d2-moment-bridge.yml' -CORE_TREE='fc0b11706d064b9de42331ee0a2464865107b87b' -CORE_MANIFEST='7149915669414e22f593ce2c6540c5e10d922eb758c7464794fc64764e679778' -CORE_GATE='25e08d9054b929dd657668e4ee4a3524688dc476e5af958f950de3b919f04e9a' +CORE_TREE='8922fd8c833df0c52d4a8c7b5c3ad802b93098d4' +CORE_MANIFEST='ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062' +CORE_GATE='509b6eb3b5ee2dc7e42810a40b07192b0feaf3924c9dbb398db43cb44e16e731' + +CORE_SOURCE_IDENTITY='481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066' +CORE_IDENTITY_PATH='tools/required_formal_check.py' +CORE_IDENTITY_SHA256='6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc' +CORE_IDENTITY_BLOB='0b9d694bdf888c75826ca07ae176c5a5c43b8880' REPO='d6g8k5htny-coder/Math-' CONTROLS=('RejectZeroAtom','RejectSameRadius') @@ -52,6 +57,38 @@ def names(meta): return result +def core_identity(): + """Bind the outside-tree validator before evaluating its verified bytes.""" + path = ROOT / CORE_IDENTITY_PATH + require(not any(p.is_symlink() for p in (path, *path.parents)) and path.is_file(), + 'core identity validator missing or symlink') + raw = path.read_bytes() + framed = b'blob ' + str(len(raw)).encode() + b'\0' + raw + require(sha(raw) == CORE_IDENTITY_SHA256 + and hashlib.sha1(framed).hexdigest() == CORE_IDENTITY_BLOB, + 'core identity validator changed') + namespace = {'__name__': 'bound_d2_core_identity', '__file__': str(path)} + exec(compile(raw, str(path), 'exec'), namespace) + manifest = ROOT / 'formal/manifest.json' + manifest_raw = manifest.read_bytes() + require(sha(manifest_raw) == CORE_MANIFEST, 'core manifest changed') + digest = namespace['manifest_identity'](manifest, manifest_raw) + require(digest == CORE_SOURCE_IDENTITY, 'core source identity changed') + return digest + + +def validate_core_receipt(core, current): + """The current core receipt binds the composite source, not the raw manifest.""" + require(isinstance(core, dict), 'invalid core receipt') + require(core.get('checked_commit') == current['checked_commit'], + 'core executed different source') + require(core.get('manifest_sha256') == core_identity(), 'core receipt manifest') + require(core.get('formalization_status') == 'kernel-checked', 'no core kernel receipt') + + + + + def source(): meta=metadata();head=git('rev-parse','HEAD') require(re.fullmatch(r'[0-9a-f]{40}',head),'invalid HEAD') @@ -59,6 +96,7 @@ def source(): require(git('rev-parse',head+':formal')==CORE_TREE,'core tree changed; new source review required') require(sha((ROOT/'formal/manifest.json').read_bytes())==CORE_MANIFEST,'core manifest changed') require(sha((ROOT/'formal/gate.py').read_bytes())==CORE_GATE,'core verifier changed') + core_identity() if os.environ.get('GITHUB_ACTIONS')=='true': require(os.environ.get('GITHUB_SHA')==head and os.environ.get('GITHUB_REPOSITORY')==REPO,'host/source mismatch') require(os.environ.get('GITHUB_RUN_ID') and os.environ.get('GITHUB_RUN_ATTEMPT'),'run identity absent') @@ -75,7 +113,11 @@ def source(): if path in meta['files']: require(meta['files'][path]=={'bytes':len(raw),'sha256':sha(raw),'git_blob':bid},'manifest source mismatch: '+path) ids[path]={'sha256':sha(raw),'git_blob':bid,'bytes':len(raw)} - git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW) + helper_raw=(ROOT/CORE_IDENTITY_PATH).read_bytes() + helper_blob=blob(helper_raw) + require(git('rev-parse',head+':'+CORE_IDENTITY_PATH)==helper_blob,'dirty core identity validator') + ids[CORE_IDENTITY_PATH]={'bytes':len(helper_raw),'sha256':sha(helper_raw),'git_blob':helper_blob} + git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW,CORE_IDENTITY_PATH) return {'checked_commit':head,'repository':REPO,'run_id':os.environ.get('GITHUB_RUN_ID','local'), 'run_attempt':os.environ.get('GITHUB_RUN_ATTEMPT','local'),'core_tree':CORE_TREE, 'sources':ids,'declarations':names(meta),'scientific_effect':'NONE'} @@ -166,9 +208,7 @@ def finish(out): require((out/(label+'.status')).read_text()=='0\n','missing successful process: '+label) core=ROOT/'formal/.lake/formal-evidence/receipt.json' raw=core.read_bytes();receipt=strict(raw) - require(receipt['checked_commit']==current['checked_commit'],'core executed different source') - require(receipt['manifest_sha256']==CORE_MANIFEST,'core receipt manifest') - require(receipt['formalization_status']=='kernel-checked','no core kernel receipt') + validate_core_receipt(receipt,current) final=final_runtime(out,receipt,strict((ROOT/'formal/manifest.json').read_bytes())['dependency_revisions']) result={**current,'axioms':ax,'theorem_count':len(metadata()['theorems']), 'definition_count':len(metadata()['definitions']),'core_receipt_sha256':sha(raw),'final_observation':final, diff --git a/companions/d2_moment_bridge_20261006/test_bridge.py b/companions/d2_moment_bridge_20261006/test_bridge.py index 5bcc4628b..a15a8cb9b 100644 --- a/companions/d2_moment_bridge_20261006/test_bridge.py +++ b/companions/d2_moment_bridge_20261006/test_bridge.py @@ -49,7 +49,7 @@ def test_exact_declared_inventory(self): self.assertEqual(meta['theorems'],THEOREMS) self.assertEqual(meta['definitions'],DEFINITIONS) self.assertEqual(meta['scientific_effect'],'NONE') - self.assertEqual(meta['core_tree'],'fc0b11706d064b9de42331ee0a2464865107b87b') + self.assertEqual(meta['core_tree'],'8922fd8c833df0c52d4a8c7b5c3ad802b93098d4') def test_residual_identity_and_exact_equality_class(self): count=0 @@ -198,5 +198,142 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +class CoreIdentityProtocolTests(unittest.TestCase): + """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" + def setUp(self): + import shutil + import subprocess + from unittest import mock + self._subprocess = subprocess + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = Path(self.tmp.name).resolve() + original = SIDE.parents[1] + shutil.copytree(original / 'formal', self.root / 'formal') + for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): + relative = Path('companions') / (package + '_20261006') + shutil.copytree(original / relative, self.root / relative) + workflow = Path('.github/workflows') / (package.replace('_', '-') + '.yml') + (self.root / workflow).parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(original / workflow, self.root / workflow) + (self.root / 'tools').mkdir() + shutil.copy2(original / 'tools/required_formal_check.py', + self.root / 'tools/required_formal_check.py') + for args in (('init', '-q'), ('add', '.'), + ('-c', 'user.name=protocol-fixture', + '-c', 'user.email=protocol@example.invalid', + 'commit', '-qm', 'copied exact source protocol fixture')): + subprocess.run(['git', '-C', str(self.root), *args], + check=True, capture_output=True) + self.head = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD'], text=True).strip() + actual_tree = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD:formal'], + text=True).strip() + self.assertEqual(actual_tree, '8922fd8c833df0c52d4a8c7b5c3ad802b93098d4') + self.context = {'checked_commit': self.head} + patch = mock.patch.dict('os.environ', {'GITHUB_ACTIONS': 'false'}) + patch.start() + self.addCleanup(patch.stop) + copied = self.root / 'companions' / SIDE.name / 'check.py' + spec = importlib.util.spec_from_file_location('copied_d2_protocol', copied) + self.c = importlib.util.module_from_spec(spec) + spec.loader.exec_module(self.c) + + def validator(self): + function = getattr(self.c, 'validate_core_receipt', None) + self.assertTrue(callable(function), 'production core receipt validator missing') + return function + + def good_receipt(self): + return {'checked_commit': self.head, + 'manifest_sha256': '481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066', + 'formalization_status': 'kernel-checked'} + + def test_source_accepts_exact_reviewed_core_without_lean(self): + try: + current = self.c.source() + except (ValueError, self._subprocess.CalledProcessError) as error: + self.fail('reviewed frozen source cut must bind: ' + str(error)) + self.assertEqual(current['checked_commit'], self.head) + self.assertIn('tools/required_formal_check.py', current['sources']) + + def test_actual_producer_to_receipt_validator(self): + producer_path = self.root / 'formal/gate.py' + spec = importlib.util.spec_from_file_location('copied_rf312_producer', producer_path) + producer = importlib.util.module_from_spec(spec) + spec.loader.exec_module(producer) + _, digest = producer.source_check() + self.assertEqual(digest, self.good_receipt()['manifest_sha256']) + self.validator()( + {'checked_commit': self.head, 'manifest_sha256': digest, + 'formalization_status': 'kernel-checked'}, self.context) + + def test_raw_digest_wrong_commit_and_non_kernel_receipts_refuse(self): + validate = self.validator() + raw = self.c.sha((self.root / 'formal/manifest.json').read_bytes()) + for key, value in (('manifest_sha256', raw), + ('checked_commit', '0' * 40), + ('formalization_status', 'source')): + with self.subTest(key=key): + bad = dict(self.good_receipt(), **{key: value}) + with self.assertRaises(ValueError): + validate(bad, self.context) + + def test_helper_drift_refuses_before_evaluating_changed_code(self): + helper = self.root / 'tools/required_formal_check.py' + helper.write_bytes(helper.read_bytes() + + b"\nraise RuntimeError('untrusted helper evaluated')\n") + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_helper_symlink_and_absence_refuse(self): + helper = self.root / 'tools/required_formal_check.py' + raw = helper.read_bytes() + helper.unlink() + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + outside = self.root / 'outside-validator.py' + outside.write_bytes(raw) + helper.symlink_to(outside) + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_manifest_overlay_and_core_file_drift_refuse(self): + changes = (('formal/manifest.json', b'\n'), + ('formal/manifest.overlay.json', b'\n'), + ('formal/ResearchFormalCoreR1/AlgebraV2.lean', b'\n')) + for relative, suffix in changes: + with self.subTest(path=relative): + path = self.root / relative + raw = path.read_bytes() + path.write_bytes(raw + suffix) + try: + with self.assertRaises((ValueError, self._subprocess.CalledProcessError)): + self.c.source() + finally: + path.write_bytes(raw) + + def test_missing_or_unknown_overlay_regime_never_downgrades(self): + validate = self.validator() + overlay = self.root / 'formal/manifest.overlay.json' + original = overlay.read_bytes() + overlay.unlink() + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(b'{"schema_version":2,"files":{}}') + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(original) + manifest = self.root / 'formal/manifest.json' + raw = manifest.read_bytes() + document = self.c.strict(raw) + document.pop('source_identity') + manifest.write_text(__import__('json').dumps(document)) + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + + + if __name__=='__main__': unittest.main(verbosity=2) diff --git a/companions/d2_square_schur_20261006/SCOPE.md b/companions/d2_square_schur_20261006/SCOPE.md index 284ff4057..b718ddc31 100644 --- a/companions/d2_square_schur_20261006/SCOPE.md +++ b/companions/d2_square_schur_20261006/SCOPE.md @@ -151,3 +151,27 @@ bytes. The new tests are synthetic and do not execute Lean. New current-commit kernel receipts and hosted checks are still required after publication. Alignment remains PENDING_INDEPENDENT_REVIEW; scientific effect NONE. + +## RF312 source/receipt engineering proposal, 28 October 2026 + +This dated private proposal preserves the mathematical text and historical evidence above. +Its source basis is RF312 `80946e1cf311b3cbbb051bb3662357b11f8b3d75`, formal tree +`8922fd8c833df0c52d4a8c7b5c3ad802b93098d4`, with the unchanged nine formal Lean/root +blobs, 63 target entries, source modules, dependencies, toolchain and companion Lean/contract bytes. +These are source identities and declared dependencies, not a fresh proof or applicability review. + +The raw manifest SHA256 is `ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062`; +the core receipt instead binds composite source `481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066`. +The fixed strict RF312 overlay contract must match; no raw-receipt fallback is admitted. +The reused identity validator lies outside the formal tree, at `tools/required_formal_check.py` +(blob `0b9d694bdf888c75826ca07ae176c5a5c43b8880`, SHA256 +`6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc`). Its source bytes are +checked before those verified bytes are evaluated; drift, symlink, missing overlay and wrong regimes refuse. +Its identity is also retained in the source snapshot across execution. Atom's two imported companion +Lean pins and all existing runtime re-observation checks are preserved. + +Protocol unit fixtures are synthetic and execute no Lean or finite-law diagnostic. They do not +supply a core or companion kernel/runtime receipt. Existing receipts/reviews remain historical; +fresh actual current-composition core and all three D2 hosted/negative-control/runtime checks, +qualified review and separate integration remain required. Scientific effect NONE; alignment and +scientific acceptance are unchanged. No existing312 source/ref or integration custody follows. diff --git a/companions/d2_square_schur_20261006/SOURCE_FILES.json b/companions/d2_square_schur_20261006/SOURCE_FILES.json index 265c35176..bd531b41c 100644 --- a/companions/d2_square_schur_20261006/SOURCE_FILES.json +++ b/companions/d2_square_schur_20261006/SOURCE_FILES.json @@ -1,5 +1,5 @@ { - "core_tree": "fc0b11706d064b9de42331ee0a2464865107b87b", + "core_tree": "8922fd8c833df0c52d4a8c7b5c3ad802b93098d4", "files": { ".github/workflows/d2-square-schur.yml": { "bytes": 1650, @@ -17,14 +17,14 @@ "sha256": "e0f9560e6f129216cd989cea3cdf97254972ca9b1cae7bab57df664329ae78fa" }, "companions/d2_square_schur_20261006/SCOPE.md": { - "bytes": 9354, - "git_blob": "284ff4057933a7106d92784b4328bde5adbae1d4", - "sha256": "d0ed0e8665e601407afe00b7293c2fdbae0ac22bc22ed8fc06a3ee7e2a1a98dd" + "bytes": 11180, + "git_blob": "b718ddc31d7725c3ac576e1cb1cdc2430247baa1", + "sha256": "794d2fe362e8c28551af066820ae7ea1461658d7072052c7739add0284de9aec" }, "companions/d2_square_schur_20261006/check.py": { - "bytes": 9803, - "git_blob": "3aea440c6cb1b2ce3f193d36b2058590da816a4c", - "sha256": "73726f63407ee4b612caa7c101d35cf0e285ebc55cc11872a1bc24c75799daf3" + "bytes": 11654, + "git_blob": "5f57381b27c995e98c0d34147a465358b6e5bcc4", + "sha256": "1865267548687f0e1c86f11d9a5311dc47ccff9e3db2aa0e205aa17411c1a131" }, "companions/d2_square_schur_20261006/replay.sh": { "bytes": 3071, @@ -32,9 +32,9 @@ "sha256": "bad86f8be8df812ceb0a13cb7d0dba7e0f3ade11a4525ec5aee9660bbf3e5067" }, "companions/d2_square_schur_20261006/test_check.py": { - "bytes": 7582, - "git_blob": "e058c4548aca3d434d795f012e971342eb0f00ca", - "sha256": "57dfd8f8b8b51a74a9f3ea036bc6d6fb536a920052c399281945319285bb83c4" + "bytes": 14327, + "git_blob": "71d80e20848e7220bc3ff1607543ac0ba70fa6e1", + "sha256": "b2d57423d7af99f47541e97c94c58169816a782a1036e1de45e5e324b2bb3329" } }, "scientific_effect": "NONE", diff --git a/companions/d2_square_schur_20261006/check.py b/companions/d2_square_schur_20261006/check.py index 3aea440c6..5f57381b2 100644 --- a/companions/d2_square_schur_20261006/check.py +++ b/companions/d2_square_schur_20261006/check.py @@ -18,9 +18,14 @@ PACKAGE='companions/d2_square_schur_20261006' WORKFLOW='.github/workflows/d2-square-schur.yml' REPO='d6g8k5htny-coder/Math-' -CORE_TREE='fc0b11706d064b9de42331ee0a2464865107b87b' -CORE_MANIFEST='7149915669414e22f593ce2c6540c5e10d922eb758c7464794fc64764e679778' -CORE_GATE='25e08d9054b929dd657668e4ee4a3524688dc476e5af958f950de3b919f04e9a' +CORE_TREE='8922fd8c833df0c52d4a8c7b5c3ad802b93098d4' +CORE_MANIFEST='ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062' +CORE_GATE='509b6eb3b5ee2dc7e42810a40b07192b0feaf3924c9dbb398db43cb44e16e731' + +CORE_SOURCE_IDENTITY='481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066' +CORE_IDENTITY_PATH='tools/required_formal_check.py' +CORE_IDENTITY_SHA256='6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc' +CORE_IDENTITY_BLOB='0b9d694bdf888c75826ca07ae176c5a5c43b8880' TARGETS=tuple('D2SquareSchur.'+n for n in ('weighted_square_identity','weighted_square_lower', 'schur_no_upper','schur_from_lower_inputs')) CONTROLS=('RejectSignedWeights','RejectEndpointMaximum','RejectOutsideDirection') @@ -49,6 +54,38 @@ def git(*args): def metadata():return strict((SIDE/'SOURCE_FILES.json').read_bytes()) +def core_identity(): + """Bind the outside-tree validator before evaluating its verified bytes.""" + path = ROOT / CORE_IDENTITY_PATH + require(not any(p.is_symlink() for p in (path, *path.parents)) and path.is_file(), + 'core identity validator missing or symlink') + raw = path.read_bytes() + framed = b'blob ' + str(len(raw)).encode() + b'\0' + raw + require(sha(raw) == CORE_IDENTITY_SHA256 + and hashlib.sha1(framed).hexdigest() == CORE_IDENTITY_BLOB, + 'core identity validator changed') + namespace = {'__name__': 'bound_d2_core_identity', '__file__': str(path)} + exec(compile(raw, str(path), 'exec'), namespace) + manifest = ROOT / 'formal/manifest.json' + manifest_raw = manifest.read_bytes() + require(sha(manifest_raw) == CORE_MANIFEST, 'core manifest changed') + digest = namespace['manifest_identity'](manifest, manifest_raw) + require(digest == CORE_SOURCE_IDENTITY, 'core source identity changed') + return digest + + +def validate_core_receipt(core, current): + """The current core receipt binds the composite source, not the raw manifest.""" + require(isinstance(core, dict), 'invalid core receipt') + require(core.get('checked_commit') == current['checked_commit'], + 'core executed different source') + require(core.get('manifest_sha256') == core_identity(), 'core receipt manifest') + require(core.get('formalization_status') == 'kernel-checked', 'no core kernel receipt') + + + + + def source(): meta=metadata();head=git('rev-parse','HEAD') require(set(meta)=={'version','scientific_effect','core_tree','targets','files'},'manifest schema') @@ -59,6 +96,7 @@ def source(): require(git('rev-parse',head+':formal')==CORE_TREE,'core tree changed') require(sha((ROOT/'formal/manifest.json').read_bytes())==CORE_MANIFEST,'core manifest changed') require(sha((ROOT/'formal/gate.py').read_bytes())==CORE_GATE,'core verifier changed') + core_identity() if os.environ.get('GITHUB_ACTIONS')=='true': require(os.environ.get('GITHUB_SHA')==head and os.environ.get('GITHUB_REPOSITORY')==REPO,'host/source mismatch') require(os.environ.get('GITHUB_RUN_ID') and os.environ.get('GITHUB_RUN_ATTEMPT'),'missing run identity') @@ -72,7 +110,10 @@ def source(): ids[name]=identity(path.read_bytes()) require(git('rev-parse',head+':'+name)==ids[name]['git_blob'],'dirty source: '+name) if name in PAYLOADS:require(meta['files'][name]==ids[name],'source identity: '+name) - git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW) + helper_id=identity((ROOT/CORE_IDENTITY_PATH).read_bytes()) + require(git('rev-parse',head+':'+CORE_IDENTITY_PATH)==helper_id['git_blob'],'dirty core identity validator') + ids[CORE_IDENTITY_PATH]=helper_id + git('diff','--exit-code','HEAD','--','formal',PACKAGE,WORKFLOW,CORE_IDENTITY_PATH) return {'checked_commit':head,'repository':REPO,'run_id':os.environ.get('GITHUB_RUN_ID','local'), 'run_attempt':os.environ.get('GITHUB_RUN_ATTEMPT','local'),'core_tree':CORE_TREE, 'sources':ids,'declarations':list(TARGETS),'scientific_effect':'NONE'} @@ -147,8 +188,7 @@ def finish(out): require((out/(label+'.status')).read_bytes()==b'1\n','negative status: '+label) negative((out/(label+'.log')).read_text(),label,1) raw=(ROOT/'formal/.lake/formal-evidence/receipt.json').read_bytes();core=strict(raw) - require(core['checked_commit']==current['checked_commit'] and core['manifest_sha256']==CORE_MANIFEST,'core identity') - require(core['formalization_status']=='kernel-checked','no core kernel receipt') + validate_core_receipt(core,current) final=final_runtime(out,core,strict((ROOT/'formal/manifest.json').read_bytes())['dependency_revisions']) result={**current,'axioms':ax,'theorem_count':4,'definition_count':0,'core_receipt_sha256':sha(raw),'final_observation':final, 'formalization_status':'kernel-checked','alignment_status':'PENDING_INDEPENDENT_REVIEW', diff --git a/companions/d2_square_schur_20261006/test_check.py b/companions/d2_square_schur_20261006/test_check.py index e058c4548..71d80e208 100644 --- a/companions/d2_square_schur_20261006/test_check.py +++ b/companions/d2_square_schur_20261006/test_check.py @@ -136,4 +136,141 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +class CoreIdentityProtocolTests(unittest.TestCase): + """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" + def setUp(self): + import shutil + import subprocess + from unittest import mock + self._subprocess = subprocess + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = Path(self.tmp.name).resolve() + original = SIDE.parents[1] + shutil.copytree(original / 'formal', self.root / 'formal') + for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): + relative = Path('companions') / (package + '_20261006') + shutil.copytree(original / relative, self.root / relative) + workflow = Path('.github/workflows') / (package.replace('_', '-') + '.yml') + (self.root / workflow).parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(original / workflow, self.root / workflow) + (self.root / 'tools').mkdir() + shutil.copy2(original / 'tools/required_formal_check.py', + self.root / 'tools/required_formal_check.py') + for args in (('init', '-q'), ('add', '.'), + ('-c', 'user.name=protocol-fixture', + '-c', 'user.email=protocol@example.invalid', + 'commit', '-qm', 'copied exact source protocol fixture')): + subprocess.run(['git', '-C', str(self.root), *args], + check=True, capture_output=True) + self.head = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD'], text=True).strip() + actual_tree = subprocess.check_output( + ['git', '-C', str(self.root), 'rev-parse', 'HEAD:formal'], + text=True).strip() + self.assertEqual(actual_tree, '8922fd8c833df0c52d4a8c7b5c3ad802b93098d4') + self.context = {'checked_commit': self.head} + patch = mock.patch.dict('os.environ', {'GITHUB_ACTIONS': 'false'}) + patch.start() + self.addCleanup(patch.stop) + copied = self.root / 'companions' / SIDE.name / 'check.py' + spec = importlib.util.spec_from_file_location('copied_d2_protocol', copied) + self.c = importlib.util.module_from_spec(spec) + spec.loader.exec_module(self.c) + + def validator(self): + function = getattr(self.c, 'validate_core_receipt', None) + self.assertTrue(callable(function), 'production core receipt validator missing') + return function + + def good_receipt(self): + return {'checked_commit': self.head, + 'manifest_sha256': '481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066', + 'formalization_status': 'kernel-checked'} + + def test_source_accepts_exact_reviewed_core_without_lean(self): + try: + current = self.c.source() + except (ValueError, self._subprocess.CalledProcessError) as error: + self.fail('reviewed frozen source cut must bind: ' + str(error)) + self.assertEqual(current['checked_commit'], self.head) + self.assertIn('tools/required_formal_check.py', current['sources']) + + def test_actual_producer_to_receipt_validator(self): + producer_path = self.root / 'formal/gate.py' + spec = importlib.util.spec_from_file_location('copied_rf312_producer', producer_path) + producer = importlib.util.module_from_spec(spec) + spec.loader.exec_module(producer) + _, digest = producer.source_check() + self.assertEqual(digest, self.good_receipt()['manifest_sha256']) + self.validator()( + {'checked_commit': self.head, 'manifest_sha256': digest, + 'formalization_status': 'kernel-checked'}, self.context) + + def test_raw_digest_wrong_commit_and_non_kernel_receipts_refuse(self): + validate = self.validator() + raw = self.c.sha((self.root / 'formal/manifest.json').read_bytes()) + for key, value in (('manifest_sha256', raw), + ('checked_commit', '0' * 40), + ('formalization_status', 'source')): + with self.subTest(key=key): + bad = dict(self.good_receipt(), **{key: value}) + with self.assertRaises(ValueError): + validate(bad, self.context) + + def test_helper_drift_refuses_before_evaluating_changed_code(self): + helper = self.root / 'tools/required_formal_check.py' + helper.write_bytes(helper.read_bytes() + + b"\nraise RuntimeError('untrusted helper evaluated')\n") + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_helper_symlink_and_absence_refuse(self): + helper = self.root / 'tools/required_formal_check.py' + raw = helper.read_bytes() + helper.unlink() + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + outside = self.root / 'outside-validator.py' + outside.write_bytes(raw) + helper.symlink_to(outside) + with self.assertRaisesRegex(ValueError, 'identity validator'): + self.c.source() + + def test_manifest_overlay_and_core_file_drift_refuse(self): + changes = (('formal/manifest.json', b'\n'), + ('formal/manifest.overlay.json', b'\n'), + ('formal/ResearchFormalCoreR1/AlgebraV2.lean', b'\n')) + for relative, suffix in changes: + with self.subTest(path=relative): + path = self.root / relative + raw = path.read_bytes() + path.write_bytes(raw + suffix) + try: + with self.assertRaises((ValueError, self._subprocess.CalledProcessError)): + self.c.source() + finally: + path.write_bytes(raw) + + def test_missing_or_unknown_overlay_regime_never_downgrades(self): + validate = self.validator() + overlay = self.root / 'formal/manifest.overlay.json' + original = overlay.read_bytes() + overlay.unlink() + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(b'{"schema_version":2,"files":{}}') + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + overlay.write_bytes(original) + manifest = self.root / 'formal/manifest.json' + raw = manifest.read_bytes() + document = self.c.strict(raw) + document.pop('source_identity') + manifest.write_text(__import__('json').dumps(document)) + with self.assertRaises(ValueError): + validate(self.good_receipt(), self.context) + + + if __name__=='__main__':unittest.main(verbosity=2) diff --git a/docs/superpowers/plans/2026-10-28-d2-composite-receipt-consumers.md b/docs/superpowers/plans/2026-10-28-d2-composite-receipt-consumers.md new file mode 100644 index 000000000..152075dda --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-d2-composite-receipt-consumers.md @@ -0,0 +1,43 @@ +# D2 composite receipt consumers implementation plan + +> **For agentic workers:** Use superpowers:executing-plans for this single coordinated source task and one fresh whole-branch engineering/security review. + +**Goal:** Make the three existing D2 source/receipt consumers accept the explicitly qualified RF312 source cut and its composite receipt identity while refusing drift and downgrade. + +**Architecture:** Keep raw manifest, formal tree and gate pins distinct from the composite receipt digest. Each checker validates the exact outside-tree `manifest_identity` helper bytes before evaluating those verified bytes, then calls that actual helper. A cohesive `validate_core_receipt(core,current)` used by `finish` checks current source, the composite identity and kernel-status label. Existing source snapshots additionally bind the helper. + +**Tech Stack:** Python standard library, Git metadata and temporary protocol fixtures only. + +**Spec:** [Private proposal pickup](https://github.com/d6g8k5htny-coder/Math-/pull/312#issuecomment-6468894513). Source basis `80946e1cf311b3cbbb051bb3662357b11f8b3d75`; parent four-path source owner retains custody. This private proposal grants no existing312 ref/body/Ready/integration seat. + +## Global constraints + +- Formal tree `8922fd8c833df0c52d4a8c7b5c3ad802b93098d4`; raw manifest SHA256 `ba7d11e43574e39da3c759e0501399a6dcd1b7da32b157a4a6b01f5c133b3062`; gate SHA256 `509b6eb3b5ee2dc7e42810a40b07192b0feaf3924c9dbb398db43cb44e16e731`. +- Composite receipt identity `481653d73d00f64bf06fefbf24be994f60ebe5c54f826abf899461f6e6937066`. +- Outside-tree validator: `tools/required_formal_check.py`, blob `0b9d694bdf888c75826ca07ae176c5a5c43b8880`, SHA256 `6c27355237b45d3999298522c141984ff5b7c114f18b3c555cc45989976fc6dc`. +- Preserve all formal and companion Lean/contract/import/target/toolchain/dependency/workflow bytes, source scientific dispositions and historical evidence. No original receipt is rebound. +- No finite-law diagnostics, actual mathematics, Lean/kernel, full gate/replay or CI run. Only targeted protocol classes in normal and optimized modes. +- Initial implementation remains private. Existing312 branch publication requires a later actual source-owner handoff and fresh state reconciliation. + +## Review focus + +- Loading an unbound helper outside the formal tree must refuse before evaluating its bytes; symlinks or drift cannot become trusted code. +- A valid composite receipt passes; the raw digest, wrong commit or wrong kernel-status label refuses. +- Manifest, overlay, core tree and helper mutations refuse without a legacy fallback. +- Atom's two unchanged imported companion Lean pins and the existing runtime re-observation controls remain intact. +- New SOURCE_FILES hashes describe actual new code/tests/documentation while all mathematical payload bytes and earlier evidence remain exact. + +## Task1: Update the three consumers + +**Files:** Each companion's `check.py`, existing test file, `SOURCE_FILES.json` and README/SCOPE, plus this plan (13 paths). Moment test file is `test_bridge.py`; square and atom use `test_check.py`. + +**Interfaces:** Reuse `manifest_identity(manifest: Path, raw: bytes) -> str`. Add each checker's `core_identity() -> str` and `validate_core_receipt(core: dict, current: dict) -> None`; `source` and `finish` must use them. + +- [ ] Run the unchanged three `FinalRuntimeTests` classes in normal and optimized modes. Expected21 methods per mode PASS. +- [ ] Add targeted `CoreIdentityProtocolTests`: actual `source()` on a temporary Git copy, actual producer `source_check` plus actual receipt validator, raw-digest/wrong-commit/non-kernel refusal, and helper/manifest/overlay/symlink drift refusal. Run those classes before production edits; expect missing source-cut/receipt support failures. +- [ ] Add the fixed source pins and bound-helper path. Validate helper symlink/hash/blob before evaluating the verified byte string, call actual `manifest_identity`, and require the exact composite digest. Have `source` include the helper's current Git/blob identity and have `finish` call the production receipt validator. +- [ ] Append dated engineering source-cut qualifications without rewriting old mathematical/historical text; regenerate only existing SOURCE_FILES core_tree and changed payload identities. +- [ ] Run the new targeted protocol classes and existing FinalRuntimeTests in both modes; require all PASS. Check whitespace and exact source preservation, including all9 formal Lean blobs and5 companion Lean/contract blobs. +- [ ] Commit one private candidate, obtain fresh distinct whole-branch engineering/security review, and deliver exact parent/head/tree/paths/logs. No publication to the existing312 ref or integration is implied. + +Protocol fixtures may use synthetic receipts and Git commits; they are never hosted or kernel evidence. No same-family scientific review or status promotion is claimed. From 1c22fa4d21bcceb4f6ac5d24cd7c1bf3f8695f77 Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:32:08 -0500 Subject: [PATCH 36/38] test(d2): exclude runtime caches from source fixtures Preserve tracked formal bytes and modes while excluding ignored runtime caches from the actual protocol setup. Test-only correction; original hosted failure cause remains unknown. --- .../SOURCE_FILES.json | 6 +- .../d2_atom_integral_20261006/test_check.py | 66 ++++++++++++++++++- .../SOURCE_FILES.json | 6 +- .../d2_moment_bridge_20261006/test_bridge.py | 66 ++++++++++++++++++- .../SOURCE_FILES.json | 6 +- .../d2_square_schur_20261006/test_check.py | 66 ++++++++++++++++++- .../2026-10-28-d2-hosted-source-fixtures.md | 43 ++++++++++++ 7 files changed, 247 insertions(+), 12 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-28-d2-hosted-source-fixtures.md diff --git a/companions/d2_atom_integral_20261006/SOURCE_FILES.json b/companions/d2_atom_integral_20261006/SOURCE_FILES.json index 4f562d4b1..c9ee85db3 100644 --- a/companions/d2_atom_integral_20261006/SOURCE_FILES.json +++ b/companions/d2_atom_integral_20261006/SOURCE_FILES.json @@ -32,9 +32,9 @@ "sha256": "89d9a0c64fd88aa8cf8c6614df84b3f543c3d4ecc87ad2e529feffda90091c75" }, "companions/d2_atom_integral_20261006/test_check.py": { - "bytes": 15691, - "git_blob": "a73d12ecd2a4205a6e0876068a35a2c5f6a71a39", - "sha256": "8a35474c02fb88b249816935370fdb979eaeaf67183e9dcbd70aac261705837b" + "bytes": 19049, + "git_blob": "59ef016d445913dcb9d6e43ae5cf6774bbbd1ecd", + "sha256": "31658f7cc26a65102710cc7f29e477412403871b724a4b9a861f7c1172a6b99c" } }, "scientific_effect": "NONE", diff --git a/companions/d2_atom_integral_20261006/test_check.py b/companions/d2_atom_integral_20261006/test_check.py index a73d12ecd..59ef016d4 100644 --- a/companions/d2_atom_integral_20261006/test_check.py +++ b/companions/d2_atom_integral_20261006/test_check.py @@ -150,6 +150,70 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +def copy_formal_fixture(source, destination): + """Copy formal inputs for the actual protocol fixture; never execute Lean.""" + import shutil + return shutil.copytree(source, destination, + ignore=shutil.ignore_patterns('.lake', '__pycache__')) + + +class SourceFixtureIsolationTests(unittest.TestCase): + """Ignored runtime state must not enter a source-only protocol fixture.""" + def test_ignored_runtime_and_python_cache_are_not_copied(self): + import subprocess + with tempfile.TemporaryDirectory() as td: + base = Path(td) + source = base / 'input' / 'formal' + source.mkdir(parents=True) + (source / '.gitignore').write_text('.lake/\n__pycache__/\n') + tracked = source / 'tracked-source.txt' + tracked.write_bytes(b'exact tracked source\n') + tracked.chmod(0o755) + ignored = ('.lake/packages/runtime-cache', '__pycache__/runtime.pyc') + for relative in ignored: + path = source / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b'ignored runtime sentinel\n') + subprocess.run(['git', '-C', str(source.parent), 'init', '-q'], + check=True, capture_output=True) + subprocess.run(['git', '-C', str(source.parent), 'add', 'formal'], + check=True, capture_output=True) + for relative in ignored: + subprocess.run(['git', '-C', str(source.parent), 'check-ignore', + 'formal/' + relative], check=True, capture_output=True) + copied = base / 'copied-formal' + copy_formal_fixture(source, copied) + for relative in ignored: + with self.subTest(relative=relative): + self.assertFalse((copied / relative).exists(), + 'ignored runtime cache entered the source fixture') + self.assertEqual((copied / tracked.name).read_bytes(), tracked.read_bytes()) + self.assertEqual((copied / tracked.name).stat().st_mode & 0o777, + tracked.stat().st_mode & 0o777) + + def test_actual_tracked_formal_tree_bytes_and_modes_preserved(self): + import subprocess + original = SIDE.parents[1] + def git(root, *args): + return subprocess.check_output(['git', '-C', str(root), *args], text=True).strip() + with tempfile.TemporaryDirectory() as td: + base = Path(td) + copied = base / 'formal' + copy_formal_fixture(original / 'formal', copied) + git(base, 'init', '-q') + git(base, 'add', 'formal') + tree = git(base, 'write-tree') + self.assertEqual(git(base, 'rev-parse', tree + ':formal'), + git(original, 'rev-parse', 'HEAD:formal')) + for relative in git(original, 'ls-files', '--', 'formal').splitlines(): + source = original / relative + target = base / relative + with self.subTest(path=relative): + self.assertEqual(target.read_bytes(), source.read_bytes()) + self.assertEqual(target.stat().st_mode & 0o777, + source.stat().st_mode & 0o777) + + class CoreIdentityProtocolTests(unittest.TestCase): """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" def setUp(self): @@ -161,7 +225,7 @@ def setUp(self): self.addCleanup(self.tmp.cleanup) self.root = Path(self.tmp.name).resolve() original = SIDE.parents[1] - shutil.copytree(original / 'formal', self.root / 'formal') + copy_formal_fixture(original / 'formal', self.root / 'formal') for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): relative = Path('companions') / (package + '_20261006') shutil.copytree(original / relative, self.root / relative) diff --git a/companions/d2_moment_bridge_20261006/SOURCE_FILES.json b/companions/d2_moment_bridge_20261006/SOURCE_FILES.json index 9bb2e275f..363ff561c 100644 --- a/companions/d2_moment_bridge_20261006/SOURCE_FILES.json +++ b/companions/d2_moment_bridge_20261006/SOURCE_FILES.json @@ -31,9 +31,9 @@ "sha256": "1e50e7ffc5c68b3771e32f2574d709d055a9b5b43ef1fa74bcaccfc14e912a68" }, "companions/d2_moment_bridge_20261006/test_bridge.py": { - "bytes": 17879, - "git_blob": "a15a8cb9ba4c0cb57855aa3aaaf89582e1742be3", - "sha256": "3af2d8c332407cc5b632354dfe89064086d12f72234e426e68136a98a168aa82" + "bytes": 21237, + "git_blob": "0e083c427ce775b2b2d34f7b8d04445afda7c5c4", + "sha256": "4c0930e6e6f73ead7bf1c562786257c458f51a8083e321568095492bf972b3d9" } }, "schema": 1, diff --git a/companions/d2_moment_bridge_20261006/test_bridge.py b/companions/d2_moment_bridge_20261006/test_bridge.py index a15a8cb9b..0e083c427 100644 --- a/companions/d2_moment_bridge_20261006/test_bridge.py +++ b/companions/d2_moment_bridge_20261006/test_bridge.py @@ -198,6 +198,70 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +def copy_formal_fixture(source, destination): + """Copy formal inputs for the actual protocol fixture; never execute Lean.""" + import shutil + return shutil.copytree(source, destination, + ignore=shutil.ignore_patterns('.lake', '__pycache__')) + + +class SourceFixtureIsolationTests(unittest.TestCase): + """Ignored runtime state must not enter a source-only protocol fixture.""" + def test_ignored_runtime_and_python_cache_are_not_copied(self): + import subprocess + with tempfile.TemporaryDirectory() as td: + base = Path(td) + source = base / 'input' / 'formal' + source.mkdir(parents=True) + (source / '.gitignore').write_text('.lake/\n__pycache__/\n') + tracked = source / 'tracked-source.txt' + tracked.write_bytes(b'exact tracked source\n') + tracked.chmod(0o755) + ignored = ('.lake/packages/runtime-cache', '__pycache__/runtime.pyc') + for relative in ignored: + path = source / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b'ignored runtime sentinel\n') + subprocess.run(['git', '-C', str(source.parent), 'init', '-q'], + check=True, capture_output=True) + subprocess.run(['git', '-C', str(source.parent), 'add', 'formal'], + check=True, capture_output=True) + for relative in ignored: + subprocess.run(['git', '-C', str(source.parent), 'check-ignore', + 'formal/' + relative], check=True, capture_output=True) + copied = base / 'copied-formal' + copy_formal_fixture(source, copied) + for relative in ignored: + with self.subTest(relative=relative): + self.assertFalse((copied / relative).exists(), + 'ignored runtime cache entered the source fixture') + self.assertEqual((copied / tracked.name).read_bytes(), tracked.read_bytes()) + self.assertEqual((copied / tracked.name).stat().st_mode & 0o777, + tracked.stat().st_mode & 0o777) + + def test_actual_tracked_formal_tree_bytes_and_modes_preserved(self): + import subprocess + original = SIDE.parents[1] + def git(root, *args): + return subprocess.check_output(['git', '-C', str(root), *args], text=True).strip() + with tempfile.TemporaryDirectory() as td: + base = Path(td) + copied = base / 'formal' + copy_formal_fixture(original / 'formal', copied) + git(base, 'init', '-q') + git(base, 'add', 'formal') + tree = git(base, 'write-tree') + self.assertEqual(git(base, 'rev-parse', tree + ':formal'), + git(original, 'rev-parse', 'HEAD:formal')) + for relative in git(original, 'ls-files', '--', 'formal').splitlines(): + source = original / relative + target = base / relative + with self.subTest(path=relative): + self.assertEqual(target.read_bytes(), source.read_bytes()) + self.assertEqual(target.stat().st_mode & 0o777, + source.stat().st_mode & 0o777) + + class CoreIdentityProtocolTests(unittest.TestCase): """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" def setUp(self): @@ -209,7 +273,7 @@ def setUp(self): self.addCleanup(self.tmp.cleanup) self.root = Path(self.tmp.name).resolve() original = SIDE.parents[1] - shutil.copytree(original / 'formal', self.root / 'formal') + copy_formal_fixture(original / 'formal', self.root / 'formal') for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): relative = Path('companions') / (package + '_20261006') shutil.copytree(original / relative, self.root / relative) diff --git a/companions/d2_square_schur_20261006/SOURCE_FILES.json b/companions/d2_square_schur_20261006/SOURCE_FILES.json index bd531b41c..c9e626968 100644 --- a/companions/d2_square_schur_20261006/SOURCE_FILES.json +++ b/companions/d2_square_schur_20261006/SOURCE_FILES.json @@ -32,9 +32,9 @@ "sha256": "bad86f8be8df812ceb0a13cb7d0dba7e0f3ade11a4525ec5aee9660bbf3e5067" }, "companions/d2_square_schur_20261006/test_check.py": { - "bytes": 14327, - "git_blob": "71d80e20848e7220bc3ff1607543ac0ba70fa6e1", - "sha256": "b2d57423d7af99f47541e97c94c58169816a782a1036e1de45e5e324b2bb3329" + "bytes": 17685, + "git_blob": "d1f8b60d86783bed80a20c8d553e36d168d7972c", + "sha256": "7508a7032f5b63a51bd24d2ea1eb85c026e750aeef70900f45f7a0874e7b75be" } }, "scientific_effect": "NONE", diff --git a/companions/d2_square_schur_20261006/test_check.py b/companions/d2_square_schur_20261006/test_check.py index 71d80e208..d1f8b60d8 100644 --- a/companions/d2_square_schur_20261006/test_check.py +++ b/companions/d2_square_schur_20261006/test_check.py @@ -136,6 +136,70 @@ def test_replay_observes_after_the_last_lean_run_and_finish_requires_it(self): source=(SIDE/'check.py').read_text();body=source[source.index('def finish(out):'):source.index('def main(')] self.assertIn('final_runtime(out,',body) +def copy_formal_fixture(source, destination): + """Copy formal inputs for the actual protocol fixture; never execute Lean.""" + import shutil + return shutil.copytree(source, destination, + ignore=shutil.ignore_patterns('.lake', '__pycache__')) + + +class SourceFixtureIsolationTests(unittest.TestCase): + """Ignored runtime state must not enter a source-only protocol fixture.""" + def test_ignored_runtime_and_python_cache_are_not_copied(self): + import subprocess + with tempfile.TemporaryDirectory() as td: + base = Path(td) + source = base / 'input' / 'formal' + source.mkdir(parents=True) + (source / '.gitignore').write_text('.lake/\n__pycache__/\n') + tracked = source / 'tracked-source.txt' + tracked.write_bytes(b'exact tracked source\n') + tracked.chmod(0o755) + ignored = ('.lake/packages/runtime-cache', '__pycache__/runtime.pyc') + for relative in ignored: + path = source / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b'ignored runtime sentinel\n') + subprocess.run(['git', '-C', str(source.parent), 'init', '-q'], + check=True, capture_output=True) + subprocess.run(['git', '-C', str(source.parent), 'add', 'formal'], + check=True, capture_output=True) + for relative in ignored: + subprocess.run(['git', '-C', str(source.parent), 'check-ignore', + 'formal/' + relative], check=True, capture_output=True) + copied = base / 'copied-formal' + copy_formal_fixture(source, copied) + for relative in ignored: + with self.subTest(relative=relative): + self.assertFalse((copied / relative).exists(), + 'ignored runtime cache entered the source fixture') + self.assertEqual((copied / tracked.name).read_bytes(), tracked.read_bytes()) + self.assertEqual((copied / tracked.name).stat().st_mode & 0o777, + tracked.stat().st_mode & 0o777) + + def test_actual_tracked_formal_tree_bytes_and_modes_preserved(self): + import subprocess + original = SIDE.parents[1] + def git(root, *args): + return subprocess.check_output(['git', '-C', str(root), *args], text=True).strip() + with tempfile.TemporaryDirectory() as td: + base = Path(td) + copied = base / 'formal' + copy_formal_fixture(original / 'formal', copied) + git(base, 'init', '-q') + git(base, 'add', 'formal') + tree = git(base, 'write-tree') + self.assertEqual(git(base, 'rev-parse', tree + ':formal'), + git(original, 'rev-parse', 'HEAD:formal')) + for relative in git(original, 'ls-files', '--', 'formal').splitlines(): + source = original / relative + target = base / relative + with self.subTest(path=relative): + self.assertEqual(target.read_bytes(), source.read_bytes()) + self.assertEqual(target.stat().st_mode & 0o777, + source.stat().st_mode & 0o777) + + class CoreIdentityProtocolTests(unittest.TestCase): """Source/receipt protocol fixtures only; no Lean or finite-law execution.""" def setUp(self): @@ -147,7 +211,7 @@ def setUp(self): self.addCleanup(self.tmp.cleanup) self.root = Path(self.tmp.name).resolve() original = SIDE.parents[1] - shutil.copytree(original / 'formal', self.root / 'formal') + copy_formal_fixture(original / 'formal', self.root / 'formal') for package in ('d2_moment_bridge', 'd2_square_schur', 'd2_atom_integral'): relative = Path('companions') / (package + '_20261006') shutil.copytree(original / relative, self.root / relative) diff --git a/docs/superpowers/plans/2026-10-28-d2-hosted-source-fixtures.md b/docs/superpowers/plans/2026-10-28-d2-hosted-source-fixtures.md new file mode 100644 index 000000000..a2b66daa3 --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-d2-hosted-source-fixtures.md @@ -0,0 +1,43 @@ +# D2 source-only protocol fixtures + +Actual private proposal author: OpenAI/Codex `/root/d2_dependency_preflight`. +Claim Math-#312/6469891555, narrowed before edits by #312/6470070201. +Input local949d/tree776f is byte-identical to nativeb0ac. Existing integrator +retains source/ref/Ready/base/Main custody; this plan grants no publication role. + +## Defect and evidence boundary + +All three protocol test setups copy the entire formal directory after hosted +workflows install the dependency cache. Seven methods in each of two modes make +fourteen full-directory copies per replay. The source-only author checkout did +not contain that cache. Existing formal fixtures exclude `.lake` and +`__pycache__`; the companion fixtures must use the same bounded source boundary. + +This is an independently demonstrated fixture defect. The original fresh moment +and square hosted error remains UNKNOWN: approved job-log reads returned +Transport closed without trace bytes. No causal attribution or CI rerun follows +from this proposal. The actual bound API exports the required methods; no +speculative API or production change is justified. + +## Bounded implementation + +1. Extract the current unfiltered formal copy into the actual helper called by + each real protocol setup. Add an isolated ignored-cache sentinel regression; + run it before filtering to retain meaningful assertion RED, not missing API. +2. Give that helper the canonical `.lake`/`__pycache__` exclusions. Test that its + copied tracked formal Git tree, physical bytes and file modes remain exact. +3. Update only the three test identities in SOURCE_FILES. Run the targeted + protocol/runtime and new fixture-isolation classes normally and optimized; + no finite-law class, Lean/kernel/math/full gate/replay/hosted rerun. +4. Audit the exact seven-path scope, unchanged production and all mathematical, + dependency, workflow, source identity and historical records. Obtain a fresh + separate whole-candidate engineering review. Native publication, if later + selected, requires actual custody handoff and fresh reconciliation. + +Writable paths: three companion test files, their three SOURCE_FILES documents, +and this plan only. No checker, formal/helper, identity/pin, proof/contract, +target/import/lock/toolchain, README/SCOPE or workflow changes. No old receipt or +review is rebound. Source-exposed/same-family/org0/scienceNONE. + +Checkpoint13:30; hard14:00UTC28October2026 or invocation end. Preserve all scoped +access denials and actual integrator custody; release only the author's own stage. From d88e4820fcec94b91f6b92ffaccf28c0b0815aff Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:29:03 -0500 Subject: [PATCH 37/38] fix(legacy): model captured stdout in diagnostic reference Construct and parse the unchanged diagnostic grammar under the captured-child stdout context. Preserve exact byte equality and inherited color controls; retain original failed evaluations at their original source identities. --- .../2026-10-28-legacy-json-pipe-context.md | 33 +++++ tests/test_legacy_json_capture_context.py | 122 ++++++++++++++++++ tools/legacy_json_replay.py | 24 ++-- 3 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-28-legacy-json-pipe-context.md create mode 100644 tests/test_legacy_json_capture_context.py diff --git a/docs/superpowers/plans/2026-10-28-legacy-json-pipe-context.md b/docs/superpowers/plans/2026-10-28-legacy-json-pipe-context.md new file mode 100644 index 000000000..6b2f8a866 --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-legacy-json-pipe-context.md @@ -0,0 +1,33 @@ +# Legacy diagnostic reference pipe context + +Actual author: OpenAI/Codex `/root/d2_dependency_preflight`, new source pickup +Math-#312/6471431957 after Root6471374057 and actual integrator release6471237285. +Input825905cd5cb63fb288aa79ac4baefb14ff0cd9b9/treea0be18b5; CP14:35/hard15:00UTC +28October2026 or invocation end. This is a new stage, not an old lease extension. + +Current square/atom original logs contain38 plain-vs-ANSI diagnostic differences +each. Explicit repository-wide discovery intentionally reaches these tests; +recorded standard-library paths identify Python3.14, not an exact patch/binary. +The reference already models child pipe width, but leaves parent stdout attached +to a TTY while constructing/parsing argparse. Modern argparse may select color +from stdout; the actual captured child's stdout is non-TTY. Old unavailable +b0/3bab failure causes stay UNKNOWN, and all old failures keep their source/run. + +1. Add isolated stdlib-only tests. Observe the actual real ArgumentParser and + HelpFormatter under a synthetic parent stdout whose isatty is true; original + helper must fail the captured-child non-TTY boundary before production edits. +2. Capture reference stdout during BOTH parser construction and parsing, + alongside existing stderr capture. Retain choices, widths, exact bytes and + inherited color environment; no ANSI stripping, forced-color or Python policy. +3. Run only the new harmless methods normally/optimized. Verify real standard + captured-parser bytes, stream restoration and unchanged environment controls. + No authentic C6/PTY/spawn/_contract/replay/math/Lean/CI/held-target execution. +4. Audit three-path scope and unchanged helper code outside that reference, + contracts, original tests, proofs, workflows and source identities. Fresh + separate whole-candidate engineering review precedes one expected-old8259, + forcefalse source amendment with native readbacks and own source release. + +Owned files: tools/legacy_json_replay.py, the new isolated test file and this plan. +This is an explicit reference-instrument correction with a new source/evaluation +identity, not a retroactive pass. Fresh current Core/all-D2 and applicable hosted, +Ready/protected integration remain separate. Scientific effect NONE; org0. diff --git a/tests/test_legacy_json_capture_context.py b/tests/test_legacy_json_capture_context.py new file mode 100644 index 000000000..ce21bea89 --- /dev/null +++ b/tests/test_legacy_json_capture_context.py @@ -0,0 +1,122 @@ +"""In-memory reference-context controls; no checker, PTY, spawn or replay.""" +import argparse +import contextlib +import importlib.util +import io +import os +from pathlib import Path +import sys +import unittest +from unittest import mock + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location( + 'legacy_capture_context_subject', ROOT / 'tools/legacy_json_replay.py') +HELPER = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(HELPER) + + +class SyntheticParentTTY(io.StringIO): + def isatty(self): + return True + + +def captured_standard_diagnostic(width): + """Independent stdlib parser in the actual captured-stream context.""" + stdout, stderr = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): + parser = argparse.ArgumentParser( + prog='reference-child.py', + formatter_class=lambda prog: argparse.HelpFormatter(prog, width=width)) + parser.add_argument('--mutant', choices=HELPER.C6_CHOICES) + try: + parser.parse_args(['--mutant', 'unknown']) + except SystemExit as error: + if error.code != 2: + raise + else: + raise AssertionError('standard grammar did not reject') + if stdout.getvalue(): + raise AssertionError('standard invalid argument emitted stdout') + return stderr.getvalue().encode('utf-8') + + +class LegacyCaptureContextTests(unittest.TestCase): + def test_constructor_parse_and_real_formatter_observe_captured_stdout(self): + seen = {'constructor': [], 'parse': [], 'formatter': []} + real_init = argparse.ArgumentParser.__init__ + real_parse = argparse.ArgumentParser.parse_args + real_formatter_init = argparse.HelpFormatter.__init__ + + def formatter_init(actual, *args, **kwargs): + seen['formatter'].append(sys.stdout.isatty()) + return real_formatter_init(actual, *args, **kwargs) + + def parser_init(actual, *args, **kwargs): + seen['constructor'].append(sys.stdout.isatty()) + return real_init(actual, *args, **kwargs) + + def parse(actual, *args, **kwargs): + seen['parse'].append(sys.stdout.isatty()) + return real_parse(actual, *args, **kwargs) + + parent_out, parent_err = SyntheticParentTTY(), io.StringIO() + with contextlib.redirect_stdout(parent_out), contextlib.redirect_stderr(parent_err): + with mock.patch.object(argparse.ArgumentParser, '__init__', parser_init), \ + mock.patch.object(argparse.ArgumentParser, 'parse_args', parse), \ + mock.patch.object(argparse.HelpFormatter, '__init__', formatter_init): + result = HELPER._unknown_stderr('c6-cluster', 'reference-child.py') + self.assertIs(sys.stdout, parent_out) + self.assertIs(sys.stderr, parent_err) + self.assertTrue(result) + self.assertEqual(parent_out.getvalue(), '') + self.assertEqual(parent_err.getvalue(), '') + for phase, observations in seen.items(): + with self.subTest(phase=phase): + self.assertTrue(observations, 'real stdlib boundary was not observed') + self.assertEqual(observations, [False] * len(observations), + 'reference inherited the parent TTY instead of child capture') + + def test_exact_bytes_match_captured_standard_parser_for_width_inputs(self): + for columns, width in ((None, 78), ('35', 33), ('80', 78), ('220', 218), + (' 110 ', 108), ('', 78), ('0', 78), + ('-3', 78), ('not-a-number', 78)): + with self.subTest(columns=columns), mock.patch.dict(os.environ): + os.environ.pop('COLUMNS', None) + if columns is not None: + os.environ['COLUMNS'] = columns + expected = captured_standard_diagnostic(width) + with contextlib.redirect_stdout(SyntheticParentTTY()): + actual = HELPER._unknown_stderr('c6-cluster', 'reference-child.py') + self.assertEqual(actual, expected) + + def test_color_environment_controls_remain_unchanged(self): + controls = ('PYTHON_COLORS', 'NO_COLOR', 'FORCE_COLOR', 'TERM') + for values in ({'PYTHON_COLORS': '0'}, {'PYTHON_COLORS': '1'}, + {'NO_COLOR': '1'}, {'FORCE_COLOR': '1'}, {'TERM': 'dumb'}): + with self.subTest(values=values), mock.patch.dict(os.environ): + for key in (*controls, 'COLUMNS'): + os.environ.pop(key, None) + os.environ.update(values) + before = dict(os.environ) + expected = captured_standard_diagnostic(78) + with contextlib.redirect_stdout(SyntheticParentTTY()): + actual = HELPER._unknown_stderr('c6-cluster', 'reference-child.py') + self.assertEqual(actual, expected) + self.assertEqual(dict(os.environ), before) + + def test_parent_streams_restored_after_real_formatter_boundary_error(self): + parent_out, parent_err = SyntheticParentTTY(), io.StringIO() + with contextlib.redirect_stdout(parent_out), contextlib.redirect_stderr(parent_err): + with mock.patch.object(argparse, 'HelpFormatter', + side_effect=RuntimeError('inert fixture refusal')): + with self.assertRaisesRegex(RuntimeError, 'inert fixture refusal'): + HELPER._unknown_stderr('c6-cluster', 'reference-child.py') + self.assertIs(sys.stdout, parent_out) + self.assertIs(sys.stderr, parent_err) + self.assertEqual(parent_out.getvalue(), '') + self.assertEqual(parent_err.getvalue(), '') + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/tools/legacy_json_replay.py b/tools/legacy_json_replay.py index 854db466a..1aa66bb31 100755 --- a/tools/legacy_json_replay.py +++ b/tools/legacy_json_replay.py @@ -118,19 +118,21 @@ def _unknown_stderr(family: str, script_name: str) -> bytes: if columns <= 0: columns = 80 # CPython 3.11--3.13 HelpFormatter subtracts two from the terminal columns. - parser = argparse.ArgumentParser( - prog=script_name, - formatter_class=lambda prog: argparse.HelpFormatter(prog, width=columns - 2)) - parser.add_argument('--mutant', choices=C6_CHOICES) stream = io.StringIO() - try: - with contextlib.redirect_stderr(stream): + # Construct and format under the child's captured stdout context as well: + # newer argparse can select colors from stdout. Keep color environment intact. + with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(stream): + parser = argparse.ArgumentParser( + prog=script_name, + formatter_class=lambda prog: argparse.HelpFormatter(prog, width=columns - 2)) + parser.add_argument('--mutant', choices=C6_CHOICES) + try: parser.parse_args(['--mutant', 'unknown']) - except SystemExit as exc: - if exc.code != 2: - raise ProtocolError('reference argument grammar did not reject') from exc - else: - raise ProtocolError('reference argument grammar unexpectedly accepted') + except SystemExit as exc: + if exc.code != 2: + raise ProtocolError('reference argument grammar did not reject') from exc + else: + raise ProtocolError('reference argument grammar unexpectedly accepted') return stream.getvalue().encode('utf-8') From d916c8b39897e42a0642a1a2d296e2ba8816592a Mon Sep 17 00:00:00 2001 From: Dylan Roy <249851642+d6g8k5htny-coder@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:26:35 -0500 Subject: [PATCH 38/38] ci(d2): allocate bounded time for complete Atom replay Match the existing Square 60-minute budget without removing any mandatory stage. Bind the reviewed Atom workflow identity; preserve the original cancellation's unknown actor and reason. --- .github/workflows/d2-atom-integral.yml | 2 +- .../SOURCE_FILES.json | 4 +-- .../plans/2026-10-28-d2-atom-ci-budget.md | 35 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-28-d2-atom-ci-budget.md diff --git a/.github/workflows/d2-atom-integral.yml b/.github/workflows/d2-atom-integral.yml index 70100d320..19467302e 100644 --- a/.github/workflows/d2-atom-integral.yml +++ b/.github/workflows/d2-atom-integral.yml @@ -21,7 +21,7 @@ permissions: jobs: d2-atom-integral: runs-on: ubuntu-latest - timeout-minutes: 30 + timeout-minutes: 60 env: LEAN_NUM_THREADS: 2 steps: diff --git a/companions/d2_atom_integral_20261006/SOURCE_FILES.json b/companions/d2_atom_integral_20261006/SOURCE_FILES.json index c9ee85db3..1e27e280b 100644 --- a/companions/d2_atom_integral_20261006/SOURCE_FILES.json +++ b/companions/d2_atom_integral_20261006/SOURCE_FILES.json @@ -3,8 +3,8 @@ "files": { ".github/workflows/d2-atom-integral.yml": { "bytes": 1942, - "git_blob": "70100d3205a492bdf3583153d4f82f7e6f2eac7a", - "sha256": "c9b7f107af2dff4cfb71496616a6514a41af39291799b6ec9287977791053a99" + "git_blob": "19467302e94fb03e4afa8e05e7be545a15195917", + "sha256": "790bca4b320dd9cdf1f309cd126ed1334c54680a72b4e3693964386a9ea5d694" }, "companions/d2_atom_integral_20261006/Contract.lean": { "bytes": 3433, diff --git a/docs/superpowers/plans/2026-10-28-d2-atom-ci-budget.md b/docs/superpowers/plans/2026-10-28-d2-atom-ci-budget.md new file mode 100644 index 000000000..57651c5ff --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-d2-atom-ci-budget.md @@ -0,0 +1,35 @@ +# Bounded Atom CI allocation + +Actual source author `/root/d2_dependency_preflight`, pickup Math-#312/6472861682 +after Root6472823629 and actual R3 integration release6472677571. Immutable input +d88e4820fcec94b91f6b92ffaccf28c0b0815aff. NEW CP15:35/hard16:00UTC28October2026 +or invocation end; previous source/private assignments remain closed. + +The current Atom repository suites took815.619+844.204seconds (27m39.823). +Observed setup/cache and other unit stages leave roughly23seconds of its30min +job for fresh Core and mandatory Atom dependencies/proofs/contracts/axioms/types, +fresh rechecker, negatives, final runtime/dependencies and receipt. A Core child +already permits900seconds. Forty-five minutes leaves about15min for all of those +remaining stages and jitter; no completed Atom duration establishes adequacy. +The reviewed bounded allocation is60min, matching existing Square60 while Atom +also builds two dependencies. This is no completion guarantee. Old cancellation +actor/reason remain UNKNOWN; its original result is not relabelled timeout/PASS. + +1. Change only Atom job timeout30→60. Keep Square and every action, trigger, + permission, command, test, kernel/control and acceptance requirement exact. +2. In a committed temporary Git/source fixture, actual production source() + must reject new workflow bytes with the OLD manifest workflow identity. +3. Update only that workflow's exact byte/SHA256/Gitblob record in Atom's + SOURCE_FILES. Actual source() and inspected metadata-only protocol checks + must pass normally/optimized. No mirrored timeout test is needed. +4. Audit the complete three-path delta, unchanged fields/proofs/Corepins and + consumed sources. Obtain fresh nonauthor engineering/workflow review under + existing SectionC authority, then at most one guarded expected-oldd88 source + fast-forward, native readbacks, final disposition and only-own release. + +Owned paths: Atom workflow, Atom SOURCE_FILES, this plan. No authentic C6/PTY, +mathematics/Lean/kernel/full replay/full suite/new environment/executor/CIrequest, +dispatch/rerun/cancel/settings or denied-route retry. Any actual operation or +owner/capability stop still controls. Old cancelled receipts keep their source; +new-source hosted/Ready/protected integration is a separate successor stage. +Scientific effect NONE; source-exposed/same-family/org0, no future seat.