diff --git a/.github/workflows/cbz-source514-hosted.yml b/.github/workflows/cbz-source514-hosted.yml new file mode 100644 index 000000000..1b4b34ab4 --- /dev/null +++ b/.github/workflows/cbz-source514-hosted.yml @@ -0,0 +1,120 @@ +name: CBZ source514 frozen kernel qualification +'on': + pull_request: + paths: + - '.github/workflows/cbz-source514-hosted.yml' + - 'tools/cbz_source514_hosted.py' + - 'tools/cbz_source514_mutations.py' + - 'tests/test_cbz_source514_hosted.py' + - 'cbz/source514-hosted/**' + workflow_dispatch: +permissions: + contents: read + actions: read +concurrency: + group: cbz-source514-${{ github.sha }} + cancel-in-progress: false +jobs: + source514-frozen-kernel: + runs-on: ubuntu-24.04 + timeout-minutes: 120 + env: + LEAN_NUM_THREADS: '2' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 + with: + python-version: '3.11.16' + - name: Read retained raw RED streams from the original native artifacts + env: + GH_TOKEN: ${{ github.token }} + run: | + gh api repos/d6g8k5htny-coder/Math-/actions/artifacts/12929346355/zip > "$RUNNER_TEMP/source514-original-census-red.zip" + gh api repos/d6g8k5htny-coder/Math-/actions/artifacts/12928801439/zip > "$RUNNER_TEMP/source514-original-recorder-red.zip" + python - "$RUNNER_TEMP/source514-original-census-red.zip" "$RUNNER_TEMP/source514-original-recorder-red.zip" <<'PY' + import hashlib,sys,zipfile + expected=['e4602537b4b8e8929a63fb2ca316f4a5b4c10b9606d1dd54379511b54f81e586','6b655de27b769e1968a36d40e87ee376730efe778cd77068a4fdfd3321231a37'] + for filename,wanted in zip(sys.argv[1:],expected): + data=open(filename,'rb').read() + if hashlib.sha256(data).hexdigest()!=wanted: + raise ValueError('original RED artifact digest differs') + with zipfile.ZipFile(filename) as archive: + for item in archive.infolist(): + if item.filename.endswith(('independent-compiled-census.stdout','independent-compiled-census.stderr','normal-baseline.stderr')): + if item.file_size>1048576: + raise ValueError('unexpected diagnostic stream size') + print('RETAINED_ORIGINAL_RED_STREAM',item.filename) + print(archive.read(item).decode('utf-8')) + PY + - name: Verify recorder contracts and all three reason-bound source mutations + run: | + python -B -S tools/cbz_source514_mutations.py --output "$RUNNER_TEMP/cbz-source514-recorder-tests" + - name: Obtain immutable mathematical source + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + repository: d6g8k5htny-coder/Math- + ref: 86c00d4c08c6c2af0529ec1744693de3ca6ac6e4 + path: candidate + persist-credentials: false + - name: Obtain already-qualified replay tool and its complete self-tests + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + repository: d6g8k5htny-coder/main + ref: 66c54f8c161c3af467e5d1d715066c96b3a0561b + path: replay + sparse-checkout: cbz/evidence/kernel/additional/replay-tool + persist-credentials: false + - name: Obtain immutable collector and established phase recorder + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + repository: d6g8k5htny-coder/Math- + ref: 8151f592918a845bf19eb8eafaf8444540a32734 + path: collector + sparse-checkout: | + formal + frontiers/regular_superlevel_geometry_20261020 + persist-credentials: false + - name: Verify original Python replay reader and its source mutations + run: | + python -B -S replay/cbz/evidence/kernel/additional/replay-tool/run_mutations.py --output "$RUNNER_TEMP/cbz-source514-replay-python-tests" + - name: Install exact Lean and pinned Mathlib dependency cache + uses: leanprover/lean-action@24581d74d674803a25a81b10b059c5c0cdd344c6 + with: + lake-package-directory: candidate/frontiers/source_derivative_gaussian_20261025 + auto-config: 'false' + build: 'false' + test: 'false' + lint: 'false' + use-mathlib-cache: 'true' + use-github-cache: 'false' + - name: Actual Lean adverse fixtures and all six generated-auditor mutants in both modes + run: | + cd candidate/frontiers/source_derivative_gaussian_20261025 + LEAN_BIN="$(lake env which lean)" + cd "$GITHUB_WORKSPACE" + python -B -S replay/cbz/evidence/kernel/additional/replay-tool/run_lean_selftest.py --lean "$LEAN_BIN" --collector collector/formal/gate_helper.py --collector-sha256 ff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4 --output "$RUNNER_TEMP/cbz-source514-lean-selftest-normal" + python -B -S -O replay/cbz/evidence/kernel/additional/replay-tool/run_lean_selftest.py --lean "$LEAN_BIN" --collector collector/formal/gate_helper.py --collector-sha256 ff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4 --output "$RUNNER_TEMP/cbz-source514-lean-selftest-optimized" + - name: Build exact 92 modules and replay complete owned proof closure into an empty kernel + run: | + python -B -S tools/cbz_source514_hosted.py --source candidate --replay replay --collector collector --plan cbz/source514-hosted/domain.json --output "$RUNNER_TEMP/cbz-source514-observation" --carrier . --carrier-head "${{ github.sha }}" + - name: Preserve every raw result and source, including unsuccessful attempts + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + with: + name: cbz-source514-${{ github.run_id }}-${{ github.run_attempt }} + path: | + ${{ runner.temp }}/cbz-source514-* + candidate/frontiers/source_derivative_gaussian_20261025/*.lean + candidate/frontiers/source_derivative_gaussian_20261025/inputs/**/*.lean + candidate/frontiers/source_derivative_gaussian_20261025/lakefile.toml + candidate/frontiers/source_derivative_gaussian_20261025/lake-manifest.json + candidate/frontiers/source_derivative_gaussian_20261025/lean-toolchain + cbz/source514-hosted/domain.json + tools/cbz_source514_hosted.py + tools/cbz_source514_mutations.py + tests/test_cbz_source514_hosted.py + include-hidden-files: true + if-no-files-found: error + retention-days: 90 diff --git a/cbz/source514-hosted/README.md b/cbz/source514-hosted/README.md new file mode 100644 index 000000000..dbe0dafb5 --- /dev/null +++ b/cbz/source514-hosted/README.md @@ -0,0 +1,31 @@ +# Frozen source514 qualification + +This narrow execution carrier serves CBZ criteria3,6,7,9 and11. It does not change mathematical sources or claim a federation baseline. The historical PR514 source is checked out at86c00d4c08c6c2af0529ec1744693de3ca6ac6e4; all67 production and25 consumed support modules are built serially. The seven unconsumed elder source copies remain preserved and outside this proof-closure claim. + +`tools/cbz_source514_hosted.py` reuses the published self-tested bounded Lean replay generator from main66c54f8c161c3af467e5d1d715066c96b3a0561b and the existing C297 raw execution recorder from Math8151f592918a845bf19eb8eafaf8444540a32734. The collector's full SHA256 isff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4. Source HEAD and exact working bytes are compared with immutable Git blobs before and after execution, including files hidden by assume-unchanged flags. Every dependency additionally binds the exact complete index, working modes and bytes to the frozen tree; visible untracked paths and ignored Lean shadows outside .lake are rejected. All actual owned and dependency olean/ilean/dynamic-library objects are bound before and after census and kernel replay. Locked dependencies with no compiled object directory are recorded explicitly as absent; their later appearance also fails the stability check. Loaded recorder/tool bytes are hashed before compilation and executed from those same immutable bytes. The registered67+25 domain must equal the frozen TOML. Loaded launcher bytes are compiled from an immutable Git blob and compared with its declared source identity; the plan is parsed from the same immutable bytes whose identity is recorded. Both carrier inputs are rebound after execution. + +The independent census enumerates compiled module-table constants (with all code-generator ownership metadata explicitly recorded outside the kernel census), records every owned constant's literal type, kind and source-range origin, and is compared with the replay program's separate full environment ownership enumeration. The existing verified program checks every selected module, every owned declaration and its complete transitive type/value closure, rejects unsafe/partial constants and nonstandard axioms, and calls pinned Lean4.34.1's empty trust-level-zero Kernel.Environment.replay. Only Classical.choice, Quot.sound and propext are admitted. Mathematical model adequacy, satisfiability of all hypotheses and prose applications are outside this claim. + +Run the recorder contracts and all three source mutations in both Python modes: + +```sh +python3.11 -B -S tools/cbz_source514_mutations.py --output /new/recorder-selftest +``` + +The45 finite contract cases include dirty bytes under assume-unchanged, valid-but-different Git heads, loaded-code identity, missing/reordered/duplicate domain entries and malformed/incomplete compiled censuses. The mutation threshold is100% of three uniquely anchored source guard removals in both Python modes, requiring the named assertion failure; launch/import/syntax failures do not count as kills. The workflow also runs the original replay reader's31 cases and three source mutants, followed by its actual Lean positive/adverse fixtures and six generated-auditor mutants in both modes before any package admission. + +Prepare clean source/replay/collector checkouts at the above exact commits, install the package's pinned Lean/Mathlib dependencies, then run: + +```sh +python3.11 -B -S tools/cbz_source514_hosted.py --source /path/to/source --replay /path/to/replay --collector /path/to/collector --plan cbz/source514-hosted/domain.json --output /new/actual-observation --carrier /path/to/carrier --carrier-head +``` + +The equivalent hosted command is `.github/workflows/cbz-source514-hosted.yml`. Its artifacts retain actual source identities, program hashes, compiled object hashes, runtime/dependency identities, literal census/types, raw stdout/stderr, every observed exit and timestamps, and failure reports. No console tee is placed inside a log set it hashes. The Python transcript validator states that it does not authenticate executions or verify Lean proofs; the native execution and source/runtime bindings are required evidence. + +Initial state: NOT_RUN. Native hosted results must be observed and all artifact hashes checked before claiming qualification. Maintenance owner:none — bounded self-contained execution carrier. The canonical integrator registers observed results in federation GOVERNANCE.md, TOOLING.md, STATUS.md and DECISIONS.md before adoption. + +Preserved test-first RED: b841fcc18ad7ebbe136ff7d237c8b61455e8e02f, native run40944659971/job123696737145, original recorder baseline exit1, artifact12928801439 (ZIP SHA2566b655de27b769e1968a36d40e87ee376730efe778cd77068a4fdfd3321231a37). The stricter dependency/object controls were added before implementation; this failed attempt remains retained and is not counted as a mutation kill or a mathematical success. + +Additional observed RED: native run40946268822/job123702332184 at source787ad73c42805155a50afca5a30b02e48e39726e ran43 cases and observed one real Git-replacement false acceptance plus one inherited-GIT_DIR redirect error. The repair reuses main701's no-replacement/sanitized-Git environment pattern for every command, including cat-file batch. Existing fixed-scope guard mutation thresholds are unchanged. + +Original all92-module build: run40943603468/job123693053386 compiled every selected module with exit0. Its census then failed because it demanded a kernel declaration for `_private.SourceFourierSeries.0.I4Source.modeCoefficient.match_1.splitter._boxed`. Pinned Lean4.34.1's `ModuleData` explicitly defines extraConstNames as code-generator ownership metadata absent from the ConstantInfo mapping. The repaired census retains and prints every such extra name as NON_KERNEL_CODE_GENERATOR_OWNERSHIP and fails if it unexpectedly has a kernel declaration. Every actual kernel declaration remains checked by the independent full module-table census and the separate complete environment ownership enumeration; no mathematical theorem, bound, failed proof or test is dropped. The original failed diagnostic stream is retained, checksum-verified and printed by subsequent hosted attempts. This is a precise kernel/IR boundary, not a weakened proof obligation. diff --git a/cbz/source514-hosted/domain.json b/cbz/source514-hosted/domain.json new file mode 100644 index 000000000..a03242da4 --- /dev/null +++ b/cbz/source514-hosted/domain.json @@ -0,0 +1,101 @@ +{ + "production": [ + "SourceDerivativeGaussian", + "ContactJetCovariance", + "SourceContactCovariance", + "SourceConfluentPins", + "SourceContactSchur", + "SourceDerivativeContinuity", + "ConfluentIntegralIdentities", + "SourceEndpointRegression", + "GaussianRegressionLimits", + "SourceEndpointParameters", + "SourceContactEndpointParameters", + "SourceConfluentLimits", + "SourcePhysicalEndpointKernel", + "SourceEndpointEstimates", + "SourceSmoothConditionalKernel", + "SourceSmoothPalm", + "GaussianWeightedMajorants", + "SourceConditionalMajorant", + "SourcePinCurvatureBounds", + "ScalarProductCurvatureBound", + "SourceCurvatureDecomposition", + "SourceWeightedCurvatureBound", + "SourcePinPreservingQuartic", + "SourceSmoothTopology", + "SourceRawPinConditioning", + "SourceQuarticCaptureAlgebra", + "SourceMorseIncidence", + "SourceSmoothParameterSpace", + "SourceLifetimePairFiniteness", + "SourceCompactTargetMoments", + "AdaptiveFoldScalarBarrier", + "AdaptiveFoldTransverseRidge", + "AdaptiveFoldBoundaryPath", + "SourceAdaptiveFoldChart", + "SourceAdaptiveFoldEstimates", + "SourceActualFoldDeath", + "SourceActualFoldTorus", + "SourceActualFoldH0", + "SourceFoldWeightedProbability", + "SourceGapPinCurvature", + "SourceAllTargetCurvatureResidual", + "SourceGapWeightedNumerator", + "SourceCompactGapNormalizer", + "SourceCompactFoldWeightedProbability", + "SourceActualFoldCandidate", + "SourceFinitePinKernel", + "SourceFiniteTrig", + "SourceRemoteJetWitness", + "SourceRemoteJetGaussian", + "SourceEndpointHessianWitness", + "SourceH0OlderClearance", + "SourceEndpointHessianGaussian", + "SourceTwoRemoteFirstJetWitness", + "SourceIntrinsicH0Interface", + "SourceTwoRemoteFirstJetGaussian", + "SourceIntrinsicH0WeightedProbability", + "SourceRawPinFreeCoordinates", + "SourceFramedConfluentCovariance", + "SourceGapConditionalMorseDistinct", + "SourceActualMorseLocalModel", + "SourceActualTorusSuperlevelLocalBasis", + "SourceActualTorusRegularSlab", + "SourceActualFiniteSuperlevelComponents", + "SourceActualQuotientSaddleCollar", + "SourceActualSaddleUppercuts", + "SourceActualRegularLocalCollar", + "SourceActualLowerSeparation" + ], + "support": [ + "GaussianSeriesFoundation", + "SpectralWeights", + "SourceFourierSeries", + "SourceC4", + "SourceGaussianLaw", + "SourceLattice", + "SourceKernel", + "SourceCovariance", + "SourceTorus", + "SourceTorusC4", + "SourceSmooth", + "SourceSmoothCarrier", + "ResearchFormalCoreR1.DeterminantMoment", + "ResearchFormalCoreR1.GaussianScalarMoments", + "ResearchFormalCoreR1.GaussianJetMoments", + "ResearchFormalCoreR1.GaussianNormalizer", + "ResearchFormalCoreR1.EndpointGaussianMoments", + "ResearchFormalCoreR1.NormalizerFloor", + "ResearchFormalCoreR1.EndpointNormalizerBridge", + "ResearchFormalCoreR1.GaussianCoupling", + "ResearchFormalCoreR1.GaussianNormalizerLimit", + "ResearchFormalCoreR1.PalmReweighting", + "ResearchFormalCoreR1.GaussianRegressionKernel", + "Lifetime", + "SourceLifetimeGeometry" + ], + "schema": "cbz-source514-exact-domain-v1", + "source_commit": "86c00d4c08c6c2af0529ec1744693de3ca6ac6e4", + "boundary": "67 registered production modules plus exactly25 consumed support modules; inputs/elder/{SuperlevelBar,ElderClearance,ReusedCompactFrontier,ClosedSuperlevel,OrdinaryH0,ElderInterval,ElderBridge}.lean are preserved but not imported by any production module and have no proof-closure admission in this run." +} diff --git a/tests/test_cbz_source514_hosted.py b/tests/test_cbz_source514_hosted.py new file mode 100644 index 000000000..6c037c594 --- /dev/null +++ b/tests/test_cbz_source514_hosted.py @@ -0,0 +1,295 @@ +from __future__ import annotations +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) +import cbz_source514_hosted as subject + +class SourceBindingTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + subprocess.run(["git","init","-q",str(self.root)],check=True) + subprocess.run(["git","-C",str(self.root),"config","user.email","fixture@example.invalid"],check=True) + subprocess.run(["git","-C",str(self.root),"config","user.name","Finite fixture"],check=True) + (self.root/"Proof.lean").write_text("theorem exactFixture : True := True.intro\n") + subprocess.run(["git","-C",str(self.root),"add","Proof.lean"],check=True) + subprocess.run(["git","-C",str(self.root),"commit","-qm","finite fixture"],check=True) + self.previous_head=subject.git(self.root,"rev-parse","HEAD").decode().strip() + (self.root/"Metadata.txt").write_text("second commit; unchanged proof blob\n") + subprocess.run(["git","-C",str(self.root),"add","Metadata.txt"],check=True) + subprocess.run(["git","-C",str(self.root),"commit","-qm","metadata successor"],check=True) + self.head=subject.git(self.root,"rev-parse","HEAD").decode().strip() + + def test_exact_source_is_bound(self): + result=subject.bind(self.root,self.head,["Proof.lean"]) + self.assertEqual(result["commit"],self.head) + self.assertEqual(result["files"]["Proof.lean"]["sha256"],subject.sha((self.root/"Proof.lean").read_bytes())) + + def test_wrong_head_rejected(self): + with self.assertRaisesRegex(ValueError,"source HEAD differs"): + subject.bind(self.root,self.previous_head,["Proof.lean"]) + + def test_dirty_bytes_rejected_even_assume_unchanged(self): + subprocess.run(["git","-C",str(self.root),"update-index","--assume-unchanged","Proof.lean"],check=True) + (self.root/"Proof.lean").write_text("theorem changedFixture : True := True.intro\n") + with self.assertRaisesRegex(ValueError,"source bytes differ"): + subject.bind(self.root,self.head,["Proof.lean"]) + + def test_unsafe_relative_path_rejected(self): + with self.assertRaisesRegex(ValueError,"unsafe bound source path"): + subject.bind(self.root,self.head,["../Proof.lean"]) + + def test_symlink_rejected(self): + (self.root/"Link.lean").symlink_to("Proof.lean") + with self.assertRaisesRegex(ValueError,"symlinked bound source"): + subject.bind(self.root,self.head,["Link.lean"]) + +class DependencyCustodyTests(SourceBindingTests): + def package(self): + temporary=tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + package=Path(temporary.name) + (package/".lake/packages").mkdir(parents=True) + (package/".lake/packages/Fixture").symlink_to(self.root,target_is_directory=True) + lock={"packages":[{"name":"Fixture","type":"git","rev":self.head}]} + return package,lock + + def test_dependency_hidden_dirty_bytes_rejected(self): + subprocess.run(["git","-C",str(self.root),"update-index","--assume-unchanged","Proof.lean"],check=True) + (self.root/"Proof.lean").write_text("theorem changedFixture : True := True.intro\n") + with self.assertRaisesRegex(ValueError,"source bytes differ"): + subject.bind_dependency(self.root,self.head) + + def test_dependency_changed_index_rejected_with_original_working_bytes(self): + original=(self.root/"Proof.lean").read_bytes() + (self.root/"Proof.lean").write_text("theorem changedFixture : True := True.intro\n") + subprocess.run(["git","-C",str(self.root),"add","Proof.lean"],check=True) + (self.root/"Proof.lean").write_bytes(original) + with self.assertRaisesRegex(ValueError,"index differs"): + subject.bind_dependency(self.root,self.head) + + def test_dependency_ignored_lean_shadow_rejected(self): + (self.root/".git/info/exclude").write_text("Shadow.lean\n") + (self.root/"Shadow.lean").write_text("theorem shadowFixture : True := True.intro\n") + with self.assertRaisesRegex(ValueError,"untracked Lean shadow"): + subject.bind_dependency(self.root,self.head) + + def test_dependency_change_during_actual_phase_rejected(self): + package,lock=self.package() + with self.assertRaisesRegex(ValueError,"source bytes differ"): + with subject.frozen_dependencies(package,lock): + subprocess.run(["git","-C",str(self.root),"update-index","--assume-unchanged","Metadata.txt"],check=True) + (self.root/"Metadata.txt").write_text("actual phase mutated a dependency\n") + + def test_git_replacement_cannot_relabel_a_new_tree_as_the_frozen_commit(self): + frozen=self.head + (self.root/"Proof.lean").write_text("theorem replacementFixture : True := True.intro\n") + subprocess.run(["git","-C",str(self.root),"add","Proof.lean"],check=True) + subprocess.run(["git","-C",str(self.root),"commit","-qm","replacement tree"],check=True) + replacement=subject.git(self.root,"rev-parse","HEAD").decode().strip() + subprocess.run(["git","-C",str(self.root),"replace",frozen,replacement],check=True) + subprocess.run(["git","-C",str(self.root),"update-ref","HEAD",frozen],check=True) + with self.assertRaisesRegex(ValueError,"index differs"): + subject.bind_dependency(self.root,frozen) + + def test_inherited_git_directory_cannot_redirect_dependency_identity(self): + with mock.patch.dict(os.environ,{"GIT_DIR":str(self.root/"missing-git"),"GIT_WORK_TREE":str(self.root/"missing-worktree")}): + bound=subject.bind_dependency(self.root,self.head) + self.assertEqual(bound["commit"],self.head) + +class CompiledObjectCustodyTests(unittest.TestCase): + def test_dependency_object_change_rejected_while_owned_object_is_stable(self): + with tempfile.TemporaryDirectory() as temporary: + package=Path(temporary) + owned=package/".lake/build/lib/lean/Owned.olean" + dependency=package/".lake/packages/Fixture/.lake/build/lib/lean/Dependency.olean.private" + owned.parent.mkdir(parents=True) + dependency.parent.mkdir(parents=True) + owned.write_bytes(b"owned fixture object") + dependency.write_bytes(b"dependency fixture object") + with self.assertRaisesRegex(ValueError,"compiled objects changed"): + with subject.frozen_objects(package,["Fixture"]): + dependency.write_bytes(b"changed dependency fixture object") + self.assertEqual(owned.read_bytes(),b"owned fixture object") + + def test_unused_dependency_object_root_absence_is_explicit(self): + with tempfile.TemporaryDirectory() as temporary: + package=Path(temporary) + owned=package/".lake/build/lib/lean/Owned.olean" + owned.parent.mkdir(parents=True) + owned.write_bytes(b"owned fixture object") + snapshot=subject.object_snapshot(package,["Unused"]) + self.assertFalse(snapshot["root_presence"][".lake/packages/Unused/.lake/build"]) + self.assertTrue(snapshot["root_presence"][".lake/build"]) + + def test_dependency_object_root_appearance_is_rejected(self): + with tempfile.TemporaryDirectory() as temporary: + package=Path(temporary) + owned=package/".lake/build/lib/lean/Owned.olean" + owned.parent.mkdir(parents=True) + owned.write_bytes(b"owned fixture object") + with self.assertRaisesRegex(ValueError,"compiled objects changed"): + with subject.frozen_objects(package,["Unused"]): + (package/".lake/packages/Unused/.lake/build").mkdir(parents=True) + +class LoadedCodeTests(unittest.TestCase): + def test_changed_loaded_bytes_rejected(self): + with tempfile.TemporaryDirectory() as temporary: + path=Path(temporary)/"loaded.py" + path.write_text("value = 7\n") + with self.assertRaisesRegex(ValueError,"loaded code differs"): + subject.load("finite_loaded",path,subject.sha(b"value = 8\n")) + def test_immutable_loaded_bytes_are_the_recorded_identity(self): + with tempfile.TemporaryDirectory() as temporary: + path=Path(temporary)/"loaded.py" + data=b"value = 7\n" + path.write_bytes(data) + module=subject.load("finite_loaded",path,subject.sha(data)) + self.assertEqual(module.value,7) + self.assertEqual(module.__source_sha256__,subject.sha(data)) + +class ExecutionInputCustodyTests(SourceBindingTests): + def prepare(self): + tool=self.root/"tools/cbz_source514_hosted.py" + plan=self.root/"cbz/source514-hosted/domain.json" + tool.parent.mkdir() + plan.parent.mkdir(parents=True) + tool.write_bytes(b"value = 7\n") + plan.write_text('{"frozen": 7}\n') + subprocess.run(["git","-C",str(self.root),"add","tools","cbz"],check=True) + subprocess.run(["git","-C",str(self.root),"commit","-qm","execution input fixture"],check=True) + self.head=subject.git(self.root,"rev-parse","HEAD").decode().strip() + return tool,plan + + def test_loaded_launcher_then_restored_file_is_rejected(self): + tool,plan=self.prepare() + tool.write_bytes(b"value = 8\n") + loaded=subject.load("changed_launcher",tool,subject.sha(tool.read_bytes())) + tool.write_bytes(b"value = 7\n") + self.assertEqual(loaded.value,8) + with self.assertRaisesRegex(ValueError,"loaded launcher differs"): + subject.execution_inputs(self.root,self.head,loaded.__source_sha256__,plan) + + def test_plan_mutation_after_parse_is_rejected(self): + tool,plan=self.prepare() + loaded_sha=subject.sha(tool.read_bytes()) + parsed,binding=subject.execution_inputs(self.root,self.head,loaded_sha,plan) + self.assertEqual(parsed,{"frozen":7}) + plan.write_text('{"frozen": 8}\n') + with self.assertRaisesRegex(ValueError,"source bytes differ"): + subject.execution_inputs(self.root,self.head,loaded_sha,plan) + +class RegisteredDomainTests(unittest.TestCase): + def setUp(self): + self.temporary=tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root=Path(self.temporary.name) + self.production=["Production"+str(i) for i in range(67)] + self.support=["Support"+str(i) for i in range(25)] + self.plan={"production":self.production,"support":self.support,"schema":"cbz-source514-exact-domain-v1","source_commit":subject.SOURCE_COMMIT} + (self.root/"inputs").mkdir() + for name in self.production: + (self.root/(name+".lean")).write_text("-- finite source fixture\n") + for name in self.support: + (self.root/"inputs"/(name+".lean")).write_text("-- finite input fixture\n") + for name in ["lake-manifest.json","lean-toolchain"]: + (self.root/name).write_text("finite configuration fixture\n") + self.write_configuration() + + def write_configuration(self): + (self.root/"lakefile.toml").write_text("defaultTargets = "+json.dumps(self.production)+"\n[[lean_lib]]\nname = \"SourceDerivativeInputs\"\nroots = "+json.dumps(self.support)+"\n") + + def test_complete_registered_domain(self): + modules,sources=subject.checked_domain(self.root,self.plan) + self.assertEqual(len(modules),92) + self.assertEqual(len(sources),95) + + def test_reordered_domain_rejected(self): + plan=dict(self.plan,production=self.production[::-1]) + with self.assertRaisesRegex(ValueError,"domain differs"): + subject.checked_domain(self.root,plan) + + def test_duplicate_modules_rejected(self): + self.production[-1]=self.support[0] + self.write_configuration() + with self.assertRaisesRegex(ValueError,"unique support modules"): + subject.checked_domain(self.root,self.plan) + + def test_missing_source_rejected(self): + (self.root/(self.production[0]+".lean")).unlink() + with self.assertRaisesRegex(ValueError,"registered source is absent"): + subject.checked_domain(self.root,self.plan) + +class IndependentCensusTests(unittest.TestCase): + def row(self,module="Fixture",name="Fixture.witness"): + return dict(module=module,name=name,kind="theorem",type="True",user_written=True,user_name=name) + def stream(self,rows): + return "".join("SOURCE514_CENSUS "+json.dumps(r)+"\n" for r in rows) + def test_exact_census(self): + expected,rows=subject.parse_census(self.stream([self.row()]),["Fixture"]) + self.assertEqual(expected,{"module_counts":{"Fixture":1},"module_declarations":{"Fixture":["Fixture.witness"]}}) + self.assertEqual(len(rows),1) + def test_duplicate_names_rejected(self): + with self.assertRaisesRegex(ValueError,"duplicate census"): + subject.parse_census(self.stream([self.row(),self.row()]),["Fixture"]) + def test_omitted_registered_module_rejected(self): + with self.assertRaisesRegex(ValueError,"omitted a registered"): + subject.parse_census(self.stream([self.row()]),["Fixture","Other"]) + def test_unrecognized_output_rejected(self): + with self.assertRaisesRegex(ValueError,"unrecognized"): + subject.parse_census("PASS\n",["Fixture"]) + def test_empty_literal_type_rejected(self): + row=self.row();row["type"]="" + with self.assertRaisesRegex(ValueError,"literal type"): + subject.parse_census(self.stream([row]),["Fixture"]) + def test_non_boolean_origin_rejected(self): + row=self.row();row["user_written"]=1 + with self.assertRaisesRegex(ValueError,"literal type/origin"): + subject.parse_census(self.stream([row]),["Fixture"]) + def test_unknown_fields_rejected(self): + row=self.row();row["ignored"]={} + with self.assertRaisesRegex(ValueError,"record fields"): + subject.parse_census(self.stream([row]),["Fixture"]) + def test_duplicate_json_keys_rejected(self): + line=self.stream([self.row()]).replace('"name": "Fixture.witness"','"name": "other", "name": "Fixture.witness"') + with self.assertRaisesRegex(ValueError,"duplicate census JSON"): + subject.parse_census(line,["Fixture"]) + def test_unsupported_kind_rejected(self): + row=self.row();row["kind"]="ignored" + with self.assertRaisesRegex(ValueError,"unsupported census"): + subject.parse_census(self.stream([row]),["Fixture"]) + def test_empty_user_name_rejected(self): + row=self.row();row["user_name"]="" + with self.assertRaisesRegex(ValueError,"user name"): + subject.parse_census(self.stream([row]),["Fixture"]) + def test_non_kernel_codegen_names_are_explicitly_recorded(self): + metadata={"module":"Fixture","name":"Fixture.witness._boxed","classification":"NON_KERNEL_CODE_GENERATOR_OWNERSHIP"} + stream="SOURCE514_CODEGEN_METADATA "+json.dumps(metadata)+"\n"+self.stream([self.row()]) + expected,rows=subject.parse_census(stream,["Fixture"]) + self.assertEqual(expected["non_kernel_codegen_ownership"],[metadata]) + self.assertEqual(expected["module_declarations"],{"Fixture":["Fixture.witness"]}) + self.assertEqual(len(rows),1) + def test_codegen_metadata_cannot_hide_a_kernel_declaration(self): + metadata={"module":"Fixture","name":"Fixture.witness","classification":"NON_KERNEL_CODE_GENERATOR_OWNERSHIP"} + stream="SOURCE514_CODEGEN_METADATA "+json.dumps(metadata)+"\n"+self.stream([self.row()]) + with self.assertRaisesRegex(ValueError,"overlaps kernel census"): + subject.parse_census(stream,["Fixture"]) + def test_generated_census_uses_exact_table_and_all_modules(self): + program=subject.census_program(["Fixture","Other"]) + self.assertIn("let names := data.constNames.qsort Name.lt",program) + self.assertIn("for extraName in data.extraConstNames",program) + self.assertIn("code-generator metadata unexpectedly has a kernel declaration",program) + self.assertIn("#["+chr(96)+"Fixture, "+chr(96)+"Other]",program) + self.assertIn("missing independent census module",program) + self.assertNotIn("MODLITERAL",program) + +if __name__=="__main__": + unittest.main() diff --git a/tools/cbz_source514_hosted.py b/tools/cbz_source514_hosted.py new file mode 100644 index 000000000..1672fa21e --- /dev/null +++ b/tools/cbz_source514_hosted.py @@ -0,0 +1,412 @@ +#!/usr/bin/env python3 +"""Execute a frozen package with the already self-tested bounded Lean replay. +This recorder does not replace Lean's kernel or authenticate supplied transcripts. +""" +from __future__ import annotations +import argparse +from contextlib import contextmanager +from datetime import datetime, timezone +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import tomllib + +SOURCE_COMMIT = "86c00d4c08c6c2af0529ec1744693de3ca6ac6e4" +REPLAY_COMMIT = "66c54f8c161c3af467e5d1d715066c96b3a0561b" +COLLECTOR_COMMIT = "8151f592918a845bf19eb8eafaf8444540a32734" +COLLECTOR_SHA256 = "ff8a5374597772b2d37054fd722c2243c4b552a63802c69f7c605519832be6c4" +PACKAGE = "frontiers/source_derivative_gaussian_20261025" +STANDARD = {"Classical.choice", "Quot.sound", "propext"} + +def sha(data): + return hashlib.sha256(data).hexdigest() + +def git_environment(): + environment = {key: value for key, value in os.environ.items() if not key.startswith("GIT_")} + environment.update(GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=os.devnull, + GIT_NO_REPLACE_OBJECTS="1", GIT_OPTIONAL_LOCKS="0") + return environment + +def git_command(repo, *args): + return ["git", "--no-replace-objects", "--literal-pathspecs", + "-c", "core.fsmonitor=false", "-c", "core.hooksPath=/dev/null", + "-C", str(repo), *args] + +def git(repo, *args): + return subprocess.check_output(git_command(repo, *args), env=git_environment(), timeout=60) + +def bind(repo, expected_commit, paths): + actual = git(repo, "rev-parse", "HEAD").decode().strip() + if actual != expected_commit: # MUTATION_HEAD + raise ValueError("source HEAD differs from frozen commit") + records = {} + for relative in paths: + path = Path(relative) + if path.is_absolute() or ".." in path.parts: + raise ValueError("unsafe bound source path") + absolute = Path(repo) / path + if any(absolute.parents[i].is_symlink() for i in range(len(path.parts)-1)) or absolute.is_symlink(): + raise ValueError("symlinked bound source") + expected = git(repo, "cat-file", "blob", expected_commit + ":" + path.as_posix()) + actual_bytes = absolute.read_bytes() + if actual_bytes != expected: # MUTATION_BYTES + raise ValueError("source bytes differ from frozen Git blob: " + relative) + records[relative] = {"bytes": len(actual_bytes), "sha256": sha(actual_bytes)} + return {"commit": actual, "files": records} + +def bind_dependency(repo, expected_commit): + repo = Path(repo).resolve() + if git(repo, "rev-parse", "HEAD").decode().strip() != expected_commit: + raise ValueError("dependency HEAD differs from frozen commit") + tree = {} + for record in git(repo, "ls-tree", "-r", "-z", expected_commit).split(b"\0"): + if not record: + continue + metadata, path = record.split(b"\t", 1) + mode, kind, oid = metadata.split() + if kind != b"blob" or mode not in {b"100644", b"100755", b"120000"}: + raise ValueError("unsupported dependency Git entry") + tree[os.fsdecode(path)] = (mode.decode(), oid.decode()) + index = {} + for record in git(repo, "ls-files", "--stage", "-z").split(b"\0"): + if not record: + continue + metadata, path = record.split(b"\t", 1) + mode, oid, stage = metadata.split() + if stage != b"0": + raise ValueError("dependency index has unresolved entries") + index[os.fsdecode(path)] = (mode.decode(), oid.decode()) + if not tree or index != tree: + raise ValueError("dependency index differs from frozen tree") + visible = git(repo, "ls-files", "--others", "--exclude-standard", "-z") + if visible: + raise ValueError("untracked dependency paths outside the cache") + for raw in git(repo, "ls-files", "--others", "--ignored", "--exclude-standard", "-z").split(b"\0"): + if not raw: + continue + path = Path(os.fsdecode(raw)) + if ".lake" not in path.parts and (path.suffix == ".lean" or ".olean" in path.name or ".ilean" in path.name): + raise ValueError("untracked Lean shadow outside the compiled cache") + oids = list(dict.fromkeys(oid for mode, oid in tree.values())) + batch = subprocess.run(git_command(repo, "cat-file", "--batch"), + input=("\n".join(oids) + "\n").encode(), capture_output=True, check=True, + env=git_environment(), timeout=60).stdout + blobs, offset = {}, 0 + for wanted in oids: + end = batch.index(b"\n", offset) + oid, kind, size = batch[offset:end].split() + size = int(size) + if oid.decode() != wanted or kind != b"blob": + raise ValueError("unexpected immutable Git blob response") + start = end + 1 + blobs[wanted] = batch[start:start + size] + if len(blobs[wanted]) != size or batch[start + size:start + size + 1] != b"\n": + raise ValueError("truncated immutable Git blob response") + offset = start + size + 1 + if offset != len(batch): + raise ValueError("trailing immutable Git blob response") + records = {} + for relative, (mode, oid) in tree.items(): + path = repo / relative + if mode == "120000": + if not path.is_symlink(): + raise ValueError("dependency symlink mode differs") + actual = os.fsencode(os.readlink(path)) + else: + if path.is_symlink() or any(parent.is_symlink() for parent in path.parents if parent != repo and parent.is_relative_to(repo)): + raise ValueError("dependency path aliases another source") + actual = path.read_bytes() + if bool(path.stat().st_mode & 0o111) != (mode == "100755"): + raise ValueError("dependency executable mode differs") + if actual != blobs[oid]: + raise ValueError("dependency source bytes differ from frozen Git blob: " + relative) + records[relative] = {"mode": mode, "git_blob": oid, "bytes": len(actual), "sha256": sha(actual)} + return {"commit": expected_commit, "files": records} + +@contextmanager +def frozen_dependencies(package, lock): + def snapshot(): + result = {} + for row in lock["packages"]: + if row["type"] != "git" or row["name"] in result: + raise ValueError("unsupported or duplicate dependency") + result[row["name"]] = bind_dependency(package / ".lake/packages" / row["name"], row["rev"]) + return result + before = snapshot() + try: + yield before + finally: + after = snapshot() + if after != before: + raise ValueError("dependency identities changed during execution") + +def object_snapshot(package, dependencies): + roots = [package / ".lake/build"] + [package / ".lake/packages" / name / ".lake/build" for name in dependencies] + records = {} + root_presence = {} + for index, root in enumerate(roots): + root_presence[str(root.relative_to(package))] = root.is_dir() + if not root.is_dir(): + if index == 0: + raise ValueError("owned compiled object directory absent") + continue + for path in sorted(root.rglob("*")): + if path.is_file() and (".olean" in path.name or ".ilean" in path.name or path.suffix in {".so", ".a"}): + if path.is_symlink(): + raise ValueError("aliased compiled object") + raw = path.read_bytes() + records[str(path.relative_to(package))] = {"bytes": len(raw), "sha256": sha(raw)} + if not records: + raise ValueError("compiled objects absent") + return {"root_presence": root_presence, "objects": records} + +@contextmanager +def frozen_objects(package, dependencies): + before = object_snapshot(package, dependencies) + try: + yield before + finally: + if object_snapshot(package, dependencies) != before: + raise ValueError("compiled objects changed during census/kernel replay") + +def execution_inputs(carrier, carrier_head, loaded_launcher_sha256, plan_path): + carrier = Path(carrier).resolve() + plan_relative = Path(plan_path).resolve().relative_to(carrier).as_posix() + launcher_relative = "tools/cbz_source514_hosted.py" + binding = bind(carrier, carrier_head, [launcher_relative, plan_relative]) + if loaded_launcher_sha256 != binding["files"][launcher_relative]["sha256"]: + raise ValueError("loaded launcher differs from frozen Git source") + plan_bytes = git(carrier, "cat-file", "blob", carrier_head + ":" + plan_relative) + return json.loads(plan_bytes), binding + +def checked_domain(package, plan): + if plan.get("schema") != "cbz-source514-exact-domain-v1" or plan.get("source_commit") != SOURCE_COMMIT: + raise ValueError("plan identity differs from frozen source") + configuration = tomllib.loads((package / "lakefile.toml").read_text()) + production = configuration["defaultTargets"] + support = next(x for x in configuration["lean_lib"] if x["name"] == "SourceDerivativeInputs")["roots"] + if production != plan["production"] or support != plan["support"]: # MUTATION_DOMAIN + raise ValueError("registered production/support domain differs from exact plan") + modules = support + production + if len(production) != 67 or len(support) != 25 or len(modules) != len(set(modules)): + raise ValueError("expected exactly 67 production and 25 unique support modules") + for module in modules: + if not isinstance(module, str) or not all(p.isidentifier() and p.isascii() for p in module.split(".")): + raise ValueError("unsupported module name") + sources = ["inputs/" + m.replace(".", "/") + ".lean" for m in support] + sources += [m.replace(".", "/") + ".lean" for m in production] + sources += ["lakefile.toml", "lake-manifest.json", "lean-toolchain"] + if any(not (package / p).is_file() for p in sources): + raise ValueError("registered source is absent") + return modules, sources + +def census_program(modules): + literal = "#[" + ", ".join(chr(96) + m for m in modules) + "]" + return "\n".join("import " + m for m in modules) + r''' +import Lean +open Lean Elab Command in +run_cmd do + let env := (← getEnv).setExporting false + let modules : Array Name := MODLITERAL + for modName in modules do + let some idx := env.getModuleIdx? modName + | throwError "missing independent census module: {modName}" + let data := env.header.moduleData[idx]! + -- Pinned Lean ModuleData declares extraConstNames to be code-generator + -- ownership metadata absent from its ConstantInfo mapping. Record every + -- such name explicitly; an unexpected kernel declaration fails closed. + for extraName in data.extraConstNames do + if (env.find? extraName).isSome then + throwError "code-generator metadata unexpectedly has a kernel declaration: {extraName}" + let metadata := Json.mkObj [ + ("module", Json.str (toString modName)), ("name", Json.str (toString extraName)), + ("classification", Json.str "NON_KERNEL_CODE_GENERATOR_OWNERSHIP")] + liftIO <| IO.println s!"SOURCE514_CODEGEN_METADATA {metadata.compress}" + let names := data.constNames.qsort Name.lt + if names.isEmpty then throwError "empty independent census module: {modName}" + for name in names do + let some ci := env.find? name | throwError "missing census declaration: {name}" + if (env.checked.get.find? name).isNone then + throwError "census declaration has no kernel declaration: {name}" + let typeText ← liftTermElabM do + return toString (← Lean.Meta.ppExpr ci.type) + let kind := match ci with + | .thmInfo _ => "theorem" | .defnInfo _ => "definition" + | .axiomInfo _ => "axiom" | .opaqueInfo _ => "opaque" + | .inductInfo _ => "inductive" | .ctorInfo _ => "constructor" + | .recInfo _ => "recursor" | .quotInfo _ => "quotient" + let userWritten := (← findDeclarationRanges? name).isSome + let row := Json.mkObj [ + ("module", Json.str (toString modName)), ("name", Json.str (toString name)), + ("kind", Json.str kind), ("type", Json.str typeText), + ("user_written", toJson userWritten), + ("user_name", Json.str (toString ((privateToUserName? name).getD name)))] + liftIO <| IO.println s!"SOURCE514_CENSUS {row.compress}" +'''.replace("MODLITERAL", literal) + +def parse_census(text, modules): + def unique(pairs): + value = {} + for key, item in pairs: + if key in value: + raise ValueError("duplicate census JSON key") + value[key] = item + return value + result = {"module_counts": {}, "module_declarations": {}} + rows, extra_codegen = [], [] + seen, extra_seen = set(), set() + for line in text.splitlines(): + if line.startswith("SOURCE514_CODEGEN_METADATA "): + metadata = json.loads(line.removeprefix("SOURCE514_CODEGEN_METADATA "), object_pairs_hook=unique) + if set(metadata) != {"module", "name", "classification"} or metadata["module"] not in modules or metadata["classification"] != "NON_KERNEL_CODE_GENERATOR_OWNERSHIP": + raise ValueError("invalid non-kernel code-generator metadata") + if not isinstance(metadata["name"], str) or not metadata["name"] or metadata["name"] in extra_seen: + raise ValueError("invalid or duplicate code-generator metadata name") + extra_seen.add(metadata["name"]) + extra_codegen.append(metadata) + continue + if not line.startswith("SOURCE514_CENSUS "): + raise ValueError("unrecognized independent census output") + row = json.loads(line.removeprefix("SOURCE514_CENSUS "), object_pairs_hook=unique, + parse_constant=lambda value: (_ for _ in ()).throw(ValueError("nonfinite census JSON")) ) + if set(row) != {"module", "name", "kind", "type", "user_written", "user_name"}: + raise ValueError("invalid census record fields") + module, name = row["module"], row["name"] + if module not in modules or name in seen or not isinstance(name, str) or not name: + raise ValueError("invalid or duplicate census declaration") + if row["kind"] not in {"theorem", "definition", "axiom", "opaque", "inductive", "constructor", "recursor", "quotient"}: + raise ValueError("unsupported census declaration kind") + if not isinstance(row["user_name"], str) or not row["user_name"]: + raise ValueError("invalid census user name") + if not isinstance(row["type"], str) or not row["type"] or type(row["user_written"]) is not bool: + raise ValueError("invalid literal type/origin") + seen.add(name) + result["module_declarations"].setdefault(module, []).append(name) + rows.append(row) + if set(result["module_declarations"]) != set(modules): + raise ValueError("independent census omitted a registered module") + if seen & extra_seen: + raise ValueError("non-kernel code-generator metadata overlaps kernel census") + result["module_counts"] = {m: len(ns) for m, ns in result["module_declarations"].items()} + if extra_codegen: + result["non_kernel_codegen_ownership"] = extra_codegen + return result, rows + +def load(name, path, expected_sha256): + data = Path(path).read_bytes() + if sha(data) != expected_sha256: + raise ValueError("loaded code differs from frozen source bytes") + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + exec(compile(data, str(path), "exec"), module.__dict__) + module.__source_sha256__ = sha(data) + return module + +def execute(source, replay, collector, plan_path, output, carrier, carrier_head, loaded_launcher_sha256): + source, replay, collector, output = [p.resolve() for p in (source, replay, collector, output)] + output.mkdir(parents=True, exist_ok=False) + recorder_relative = "frontiers/regular_superlevel_geometry_20261020/c297_hosted_verify.py" + recorder = load("c297_recorder", collector / recorder_relative, sha(git(collector, "cat-file", "blob", COLLECTOR_COMMIT + ":" + recorder_relative))) + tool_path = replay / "cbz/evidence/kernel/additional/replay-tool/replay_tool.py" + tool = load("bounded_replay", tool_path, sha(git(replay, "cat-file", "blob", REPLAY_COMMIT + ":cbz/evidence/kernel/additional/replay-tool/replay_tool.py"))) + plan, execution_before = execution_inputs(carrier, carrier_head, loaded_launcher_sha256, plan_path) + package = source / PACKAGE + modules, sources = checked_domain(package, plan) + paths = [PACKAGE + "/" + p for p in sources] + report = {"schema": "cbz-source514-hosted-observation-v1", "status": "RUNNING", + "started_utc": datetime.now(timezone.utc).isoformat(), "phase_results": [], + "repository": os.environ.get("GITHUB_REPOSITORY"), "run_id": os.environ.get("GITHUB_RUN_ID"), + "run_attempt": os.environ.get("GITHUB_RUN_ATTEMPT"), "owned_modules": modules, + "scope": "Exact frozen 92-module registered import closure; seven unconsumed elder copies and mathematical application/nonvacuity claims remain outside this replay.", + "recorder_sha256": recorder.__source_sha256__, "replay_tool_sha256": tool.__source_sha256__, + "launcher_sha256": loaded_launcher_sha256, "execution_inputs_before": execution_before} + environment = dict(os.environ, LEAN_NUM_THREADS="2") + def phase(name, command, cwd=package, timeout=1800): + return recorder.run_phase(name, [str(x) for x in command], cwd, environment, output, report, timeout=timeout) + try: + report["source_before"] = bind(source, SOURCE_COMMIT, paths) + report["replay_source"] = bind(replay, REPLAY_COMMIT, ["cbz/evidence/kernel/additional/replay-tool/replay_tool.py"]) + report["collector_source"] = bind(collector, COLLECTOR_COMMIT, [ + "formal/gate_helper.py", "frontiers/regular_superlevel_geometry_20261020/c297_hosted_verify.py"]) + helper = collector / "formal/gate_helper.py" + if sha(helper.read_bytes()) != COLLECTOR_SHA256: + raise ValueError("collector bytes differ from declared verified helper") + version, _, _ = phase("runtime-version", ["lake", "env", "lean", "--version"]) + version_text = (output / "runtime-version.stdout").read_text() + if "Lean (version 4.34.1," not in version_text or "commit 5045d0056413266e57c625dcd7c365b10e377c52" not in version_text: + raise ValueError("runtime differs from exact pinned Lean release") + lock = json.loads((package / "lake-manifest.json").read_text()) + expected_revisions = {row["name"]: row["rev"] for row in lock["packages"]} + if expected_revisions != recorder.PACKAGE_PINS: + raise ValueError("dependency vector differs from verified standard runtime") + with frozen_dependencies(package, lock) as dependency_records: + report["dependencies"] = dependency_records + for number, module in enumerate(modules): + phase("build-" + str(number).zfill(2) + "-" + module.replace(".", "_"), ["lake", "build", "+" + module]) + with frozen_objects(package, expected_revisions) as object_records: + census_path = output / "IndependentCensus.lean" + census_path.write_text(census_program(modules)) + phase("independent-compiled-census", ["lake", "env", "lean", "-j2", "-M12000", census_path], timeout=2700) + stderr = (output / "independent-compiled-census.stderr").read_bytes() + if stderr: + raise ValueError("independent census emitted stderr") + expected, literals = parse_census((output / "independent-compiled-census.stdout").read_text(), modules) + (output / "compiled-census.json").write_text(json.dumps(expected, indent=2) + "\n") + (output / "compiled-literal-types.json").write_text(json.dumps(literals, indent=2, ensure_ascii=False) + "\n") + program, metadata = tool.generate_program(helper, COLLECTOR_SHA256, modules, modules) + program_path = output / "BoundedKernelReplay.lean" + program_path.write_text(program) + (output / "replay-program.json").write_text(json.dumps(metadata, indent=2) + "\n") + _, _, code = phase("bounded-kernel-replay", ["lake", "env", "lean", "-j2", "-M12000", program_path], timeout=2700) + consistency = tool.validate_transcript((output / "bounded-kernel-replay.stdout").read_text(), expected, + metadata["program_sha256"], exit_code=code, stderr=(output / "bounded-kernel-replay.stderr").read_text()) + report["compiled_objects"] = object_records + report["source_after"] = bind(source, SOURCE_COMMIT, paths) + if report["source_after"] != report["source_before"]: + raise ValueError("bound source changed during execution") + if bind(replay, REPLAY_COMMIT, ["cbz/evidence/kernel/additional/replay-tool/replay_tool.py"]) != report["replay_source"]: + raise ValueError("replay source changed during execution") + if bind(collector, COLLECTOR_COMMIT, ["formal/gate_helper.py", "frontiers/regular_superlevel_geometry_20261020/c297_hosted_verify.py"]) != report["collector_source"]: + raise ValueError("collector/recorder source changed during execution") + _, execution_after = execution_inputs(carrier, carrier_head, loaded_launcher_sha256, plan_path) + if execution_after != execution_before: + raise ValueError("launcher/plan execution inputs changed") + report["execution_inputs_after"] = execution_after + report["replay_observation"] = consistency + report["literal_records"] = len(literals) + report["user_theorems"] = sum(r["kind"] == "theorem" and r["user_written"] for r in literals) + report["status"] = "ACTUAL_BUILD_AND_EMPTY_KERNEL_REPLAY_PASS" + except BaseException as error: + report["status"] = "NOT_PASS" + report["error"] = repr(error) + raise + finally: + report["finished_utc"] = datetime.now(timezone.utc).isoformat() + recorder.save_report(output, report) + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + for name in ["source", "replay", "collector", "plan", "output"]: + parser.add_argument("--" + name, type=Path, required=True) + parser.add_argument("--carrier", type=Path, required=True) + parser.add_argument("--carrier-head", required=True) + args = parser.parse_args() + execute(args.source, args.replay, args.collector, args.plan, args.output, args.carrier, args.carrier_head, globals().get("_CBZ_LOADED_SHA256")) + +if __name__ == "__main__": + if globals().get("_CBZ_LOADED_SHA256") is None: + carrier = Path(__file__).resolve().parents[1] + head = git(carrier, "rev-parse", "HEAD").decode().strip() + relative = "tools/cbz_source514_hosted.py" + immutable = git(carrier, "cat-file", "blob", head + ":" + relative) + if Path(__file__).read_bytes() != immutable: + raise ValueError("launcher working bytes differ from immutable Git source") + namespace = {"__name__": "__main__", "__file__": str(carrier / relative), + "_CBZ_LOADED_SHA256": sha(immutable)} + exec(compile(immutable, str(carrier / relative), "exec"), namespace) + else: + main() diff --git a/tools/cbz_source514_mutations.py b/tools/cbz_source514_mutations.py new file mode 100644 index 000000000..f2d4a57f1 --- /dev/null +++ b/tools/cbz_source514_mutations.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""Three reason-bound source mutations of the source514 recorder's custody/domain guards.""" +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import hashlib +import argparse +parser=argparse.ArgumentParser() +parser.add_argument("--output",type=Path,required=True) +args=parser.parse_args() +args.output.mkdir(parents=True,exist_ok=False) +root=Path(__file__).resolve().parents[1] +source=root/"tools/cbz_source514_hosted.py" +test=root/"tests/test_cbz_source514_hosted.py" +mutations=[ + ("head",'if actual != expected_commit: # MUTATION_HEAD','if False: # MUTATION_HEAD',"SourceBindingTests.test_wrong_head_rejected"), + ("bytes",'if actual_bytes != expected: # MUTATION_BYTES','if False: # MUTATION_BYTES',"SourceBindingTests.test_dirty_bytes_rejected_even_assume_unchanged"), + ("domain",'if production != plan["production"] or support != plan["support"]: # MUTATION_DOMAIN','if False: # MUTATION_DOMAIN',"RegisteredDomainTests.test_reordered_domain_rejected")] +report={"schema":"cbz-source514-recorder-mutations-v1","threshold":"all three source mutants in normal and optimized Python modes","source_sha256":hashlib.sha256(source.read_bytes()).hexdigest(),"executions":[],"status":"NOT_PASS"} +try: + for mode in ["normal","optimized"]: + optimize=["-O"] if mode=="optimized" else [] + command=[sys.executable,"-B","-S",*optimize,"-m","unittest","discover","-s","tests","-p","test_cbz_source514_hosted.py","-v"] + result=subprocess.run(command,cwd=root,capture_output=True) + (args.output/(mode+"-baseline.stdout")).write_bytes(result.stdout) + (args.output/(mode+"-baseline.stderr")).write_bytes(result.stderr) + report["executions"].append({"mode":mode,"label":"baseline","exit":result.returncode}) + if result.returncode: + sys.stdout.buffer.write(result.stdout) + sys.stderr.buffer.write(result.stderr) + raise RuntimeError("unmodified recorder suite must pass before mutation") + for label,anchor,replacement,target in mutations: + text=source.read_text() + if text.count(anchor)!=1: raise RuntimeError("unique mutation anchor absent: "+label) + with tempfile.TemporaryDirectory(prefix="cbz-source514-mutant-") as temporary: + scratch=Path(temporary);(scratch/"tools").mkdir();(scratch/"tests").mkdir() + (scratch/"tools/cbz_source514_hosted.py").write_text(text.replace(anchor,replacement,1)) + shutil.copyfile(test,scratch/"tests/test_cbz_source514_hosted.py") + command=[sys.executable,"-B","-S",*optimize,"-m","unittest","discover","-s","tests","-p","test_cbz_source514_hosted.py","-k",target,"-v"] + result=subprocess.run(command,cwd=scratch,capture_output=True) + stdout=result.stdout.decode();stderr=result.stderr.decode() + prefix=mode+"-"+label + (args.output/(prefix+".stdout")).write_bytes(result.stdout);(args.output/(prefix+".stderr")).write_bytes(result.stderr) + intended=(result.returncode==1 and "AssertionError: ValueError not raised" in stderr and "FAILED (failures=1)" in stderr and "Ran 1 test" in stderr and "ERROR:" not in stderr) + report["executions"].append({"mode":mode,"label":label,"exit":result.returncode,"intended_guard_failure":intended}) + if not intended: raise RuntimeError("mutant not killed for its named guard: "+prefix) + report["status"]="ALL_THREE_REASON_BOUND_MUTANTS_KILLED_IN_BOTH_MODES" +finally: + (args.output/"REPORT.json").write_text(json.dumps(report,indent=2)+"\n") +print(report["status"])