diff --git a/research/e8-bit-r420/MANIFEST.sha256 b/research/e8-bit-r420/MANIFEST.sha256 index 8117af93d3..7d2e61a091 100644 --- a/research/e8-bit-r420/MANIFEST.sha256 +++ b/research/e8-bit-r420/MANIFEST.sha256 @@ -8,7 +8,7 @@ c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4 LICENSE 46c205f6036bb8635a34ab6ef2c54a9e446bec1435fcc14ab55949a5dbee8aa4 code/adapt11.py 1770f4eb61e130e3cc6e23f67507d8bb7587baaf8391d749c2e7fe5c66089044 code/cited.py d5c802447c99255db1bdfcae62b2116bfc9612c9dc9000d41aa4f5f2fec9f2f2 code/gen_l3.py -e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594 code/mutants.py +bc6b010641dd3b2f6f33928ce2004189e0671bd9865eab7e70cfcd0d0c6cf9b9 code/mutants.py 231cbf26bfe5e6824347e4556959941d8b4dea1efa06d7f88d7a73cca8b03158 code/mutrec_b.py 6a5896f60b1fe05c41ab28ce822dff782c635e12d0c74fad20b5ce2a4010fa3f code/nodeopt.py c0a687f61aed29455cea953d8a3e49a32136edb3cd4213c46880c146a6ca7fa3 code/planio.py diff --git a/research/e8-bit-r420/code/mutants.py b/research/e8-bit-r420/code/mutants.py index 5b0fa19220..957674c9ff 100644 --- a/research/e8-bit-r420/code/mutants.py +++ b/research/e8-bit-r420/code/mutants.py @@ -16,8 +16,14 @@ 9 move a cleanup gate off the full frame (to the frame of the gate it inverts) Positive control: P a cancelled pair of target-to-target reads at frame 0 (y_t += y_u twice: V = I, allowed by the lemma) -Prepared by eumemic with substantial Anthropic Claude assistance; Apache-2.0.""" + +Production path reuses exact Word.__init__/labels geometry across evaluate() calls when h/v/R/form/lines/frames +match (research/r420-geometry-cache/). full() and interface() always rerun on a fresh Word. Equivalence: same +check API and mutation receipts as the pairwise harness. +Prepared by eumemic with substantial Anthropic Claude assistance; production geometry cache by Wentao Li with +substantial AI assistance; Apache-2.0.""" import copy +from collections import OrderedDict import gzip import importlib.util import json @@ -32,6 +38,59 @@ is_z = lambda r: not is_y(r) ZERO = next(k for k, f in enumerate(w0["frames"]) if not f) +# Complete data dependencies of Word.__init__ and labels(); see research/r420-geometry-cache/. +GEOMETRY_FIELDS = ("h", "v", "R", "form", "lines", "frames") + + +class GeometryCache: + """Reuse validated geometry across mutation checks with identical geometry fields.""" + + def __init__(self, capacity=2): + if type(capacity) is not int or capacity < 1: + raise ValueError("capacity must be a positive integer") + self.capacity = capacity + self._entries = OrderedDict() + self.hits = 0 + self.misses = 0 + + def check(self, word): + key = json.dumps({field: word[field] for field in GEOMETRY_FIELDS}, + sort_keys=True, separators=(",", ":"), allow_nan=False) + if key in self._entries: + self.hits += 1 + snapshot = self._entries.pop(key) + self._entries[key] = snapshot + else: + self.misses += 1 + checked = X.Word(word) + labels = checked.labels() # Failures never populate the cache. + snapshot = ( + checked.v, checked.R, checked.n, tuple(checked.lines), + tuple(checked.fdim), + tuple(tuple(tuple(row) for row in frame) for frame in checked.fperp), + checked.FULL, checked.ZERO, tuple(labels.items()), + ) + self._entries[key] = snapshot + if len(self._entries) > self.capacity: + self._entries.popitem(last=False) + + # Never return the cached mutable Word or share mutable derived data. + obj = X.Word.__new__(X.Word) + obj.w = word + obj.v, obj.R, obj.n = snapshot[:3] + obj.lines = list(snapshot[3]) + obj.fdim = list(snapshot[4]) + obj.fperp = [[list(row) for row in frame] for frame in snapshot[5]] + obj.FULL, obj.ZERO = snapshot[6:8] + obj._nest = {} + labels = dict(snapshot[8]) + full = obj.full() + hypotheses, conclusion, info = obj.interface() + return obj, labels, full, hypotheses, conclusion, info + + +_geometry_cache = GeometryCache() + def zgates(w): return [("A", i) for i, g in enumerate(w["A"]) if is_z(g[1])] + [("B", i) for i, g in enumerate(w["B"]) if is_z(g[1])] @@ -44,7 +103,13 @@ def mirror(w, sec, i): def evaluate(w): try: - W, lab, fl, H, C, info = X.check(w) + # Production path caches geometry when argv[1] is a full xcheck module. + # Historical drop-in adapters (research/r420-geometry-cache/cached_xcheck.py) + # expose only check/Reject; call them directly. + if hasattr(X, "Word") and hasattr(X, "check"): + W, lab, fl, H, C, info = _geometry_cache.check(w) + else: + W, lab, fl, H, C, info = X.check(w) except X.Reject as e: return False, "replay: " + str(e)[:90] bad = [k for k, x in list(H.items()) + list(C.items()) if not x] diff --git a/research/r420-geometry-cache-production/NOTICE.md b/research/r420-geometry-cache-production/NOTICE.md new file mode 100644 index 0000000000..5d59bcf953 --- /dev/null +++ b/research/r420-geometry-cache-production/NOTICE.md @@ -0,0 +1,23 @@ +# Attribution + +## This package + +Prepared by Wentao Li (GitHub [VictorLiwentao](https://github.com/VictorLiwentao), +X [@Is__Victor](https://x.com/Is__Victor)) with substantial AI assistance. +New work is Apache-2.0. + +## Predecessors + +- **eumemic** authored the PR543 independent exact replay (`xcheck.py`), the + interleaved-interface mutation harness (`mutants.py`), and the retained R420 + word/pipeline under `research/e8-bit-r420/`. +- **mtpsl** authored the opt-in geometry-reuse adapter + `research/r420-geometry-cache/` (identical-receipt `GeometryCache` over + `Word.__init__` / `labels()`), which this package wires into the shipped + `mutants.py` path. +- Quality-bar and production-promotion pattern: + [CrocSwap/integer-mult-bounds#756](https://github.com/CrocSwap/integer-mult-bounds/pull/756) + and subsequent identical-receipt production tooling PRs. + +Original notices under `research/e8-bit-r420/NOTICE.md` and +`research/r420-geometry-cache/` remain authoritative for predecessor bytes. diff --git a/research/r420-geometry-cache-production/README.md b/research/r420-geometry-cache-production/README.md new file mode 100644 index 0000000000..80b7df2071 --- /dev/null +++ b/research/r420-geometry-cache-production/README.md @@ -0,0 +1,85 @@ +# Production geometry reuse for the PR543 R420 mutation harness + +**Tooling improvement; no new kappa.** At base +`0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2`, +`research/e8-bit-r420/code/mutants.py` still rebuilt `Word.__init__` / `labels()` +on every gate-only mutation control. The opt-in adapter +`research/r420-geometry-cache/` already proved an exact geometry cache with +identical mutation receipts. This package wires that equivalent reuse into the +**production** harness used by `research/e8-bit-r420/verify.sh`. + +Every mutation stdout field, the JSON control receipt, and the +`PASS mutation controls: 9/9` marker are unchanged. `full()` and `interface()` +still run on a fresh `Word` after every geometry hit. Selected frontier +untouched. Production `xcheck.py:rref` is left alone (open #2263). + +## Reproduce + +Python **3.12**, standard library only, from the repository root: + +```sh +python3.12 -I -B research/r420-geometry-cache-production/verify.py +python3.12 -I -O -B research/r420-geometry-cache-production/verify.py +python3.12 -B scripts/research_package.py --package research/r420-geometry-cache-production +python3.12 -I -B research/r420-geometry-cache-production/benchmark.py --rounds 5 --write-result +``` + +Expected: + +```text +PASS_R420_GEOMETRY_CACHE_PRODUCTION controls=10 corruption=... mutants_sha=8c2738b3258e certs=0 no_new_kappa +PASS_REGISTERED_RESEARCH_PACKAGE research/r420-geometry-cache-production outcome=completed +PASS_IDENTICAL_R420_GEOMETRY_CACHE_PRODUCTION_RECEIPTS +``` + +Default verify is read-only. Timing is host-descriptive and is not a +correctness gate. On Linux / CPython 3.12.3, five alternating fresh-process +pairs gave median harness wall time about **9.66 → 1.49 s (≈6.46×)** in the +authoring screen; committed samples are retained in [`RESULT.json`](RESULT.json) +after `--write-result`. + +## Why the geometry cache is equivalent + +Gate-only mutations in this harness keep `h`, `v`, `R`, `form`, `lines` and +`frames` byte-identical. `Word.__init__` and `labels()` read only those fields. +Equal JSON serializations of those fields therefore have equal derived geometry +(`lines`, `fdim`, `fperp`, `FULL`, `ZERO`, label receipt). On a hit the harness +builds a fresh `Word`, copies immutable geometry into fresh mutable containers, +clears per-word nesting memoization, and always reruns `full()` and +`interface()` against the current gates. Failures during construction/labels +never populate the cache. Details match `research/r420-geometry-cache/`. + +## Overlap and credit + +Closest priors: + +- [`research/r420-geometry-cache/`](../r420-geometry-cache/) — opt-in adapter; + this package is the production wiring. +- [CrocSwap/integer-mult-bounds#756](https://github.com/CrocSwap/integer-mult-bounds/pull/756) + — identical-receipt tooling quality bar. +- Open leave-alone tooling on other objects (#1922 joint frames, #2054/#2150 + shared/mixed-point, #2100 ternary, #2117 DisjointTensor.frame_data, #2194 + production verify_factor, #2210 production leech_minor, #2231 sparse-fusion + orbits, #2239 joint-dual replay, #1861 parity-side, #2257 frameopt.close, + #2263 xcheck.rref) was not reopened. Fresh overlap refresh found no competing + production rewrite of `mutants.py` geometry reuse. + +**eumemic** authored the PR543 checker/harness/word. **mtpsl** authored the +opt-in geometry cache. Prepared by Wentao Li +([VictorLiwentao](https://github.com/VictorLiwentao), +[@Is__Victor](https://x.com/Is__Victor)) with substantial AI assistance, under +Apache-2.0. + +## Open next questions + +1. **Question:** Should other packages that pin live `mutants.py` bytes refresh + those SHA-256 entries after this equivalence-preserving rewrite? + **Why it matters:** Leaving old pins keeps this tooling PR focused. + **First step:** Dedicated provenance follow-up after confirming receipt + identity. + **Blocked by:** Pin repair outside registered VERIFY live inputs is outside + this change. + +2. **Answered in-tree:** `research/r420-geometry-cache/` demotes its + `mutants.py` execution pin to provenance in the same tip so its local + verify no longer fails working-source drift. diff --git a/research/r420-geometry-cache-production/RESULT.json b/research/r420-geometry-cache-production/RESULT.json new file mode 100644 index 0000000000..22d86ba55d --- /dev/null +++ b/research/r420-geometry-cache-production/RESULT.json @@ -0,0 +1,82 @@ +{ + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "baseline_mutants_sha256": "e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594", + "interpreter": "CPython 3.12.3", + "machine": "x86_64", + "median_seconds": { + "baseline": 9.601901119000104, + "production": 1.4851274809998358 + }, + "mutants_stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8", + "no_new_kappa": true, + "platform": "Linux-6.12.94+-x86_64-with-glibc2.39", + "production_mutants_sha256": "bc6b010641dd3b2f6f33928ce2004189e0671bd9865eab7e70cfcd0d0c6cf9b9", + "python": "3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0]", + "records": [ + { + "engine": "baseline", + "pair": 0, + "seconds": 9.601901119000104, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "production", + "pair": 0, + "seconds": 1.4447330049999891, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "production", + "pair": 1, + "seconds": 1.4851274809998358, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "baseline", + "pair": 1, + "seconds": 9.44682381199982, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "baseline", + "pair": 2, + "seconds": 9.493446924999944, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "production", + "pair": 2, + "seconds": 1.4894828100000268, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "production", + "pair": 3, + "seconds": 1.4384404740001173, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "baseline", + "pair": 3, + "seconds": 9.728668628999912, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "baseline", + "pair": 4, + "seconds": 10.305828164000104, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + }, + { + "engine": "production", + "pair": 4, + "seconds": 1.5412987929998962, + "stdout_sha256": "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + } + ], + "rounds": 5, + "speedup": 6.465371654516684, + "status": "PASS_IDENTICAL_R420_GEOMETRY_CACHE_PRODUCTION_RECEIPTS", + "timing_scope": "fresh subprocess wall time for the complete mutants.py harness (1 honest + 9 negative + 1 positive) including imports; warm OS caches, not flushed; host-descriptive only; not a correctness gate", + "xcheck_sha256": "5737d18a7826fd51e131035f2d7bebba2e2868af91dd7672a5f11208b036d424" +} diff --git a/research/r420-geometry-cache-production/SOURCES.json b/research/r420-geometry-cache-production/SOURCES.json new file mode 100644 index 0000000000..1317e7e586 --- /dev/null +++ b/research/r420-geometry-cache-production/SOURCES.json @@ -0,0 +1,55 @@ +{ + "schema_version": 1, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "ae6a99f28aa468649eb66a361e764d769363bf11", + "sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "e8870e5917057d59b730eb14721a778639eca050", + "sha256": "d46eb6d5b3e279428753d770653663fbdf1063e55c255ea073d768f344927ef7" + }, + { + "path": "research/e8-bit-r420/code/mutants.py", + "usage": "provenance", + "git_blob": "5b0fa1922076d2e91367b58483c96b33b7b08661", + "sha256": "e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594", + "note": "Base-commit pairwise harness bytes retained as baseline.py; production working tree differs (geometry cache)." + }, + { + "path": "research/e8-bit-r420/code/xcheck.py", + "usage": "input", + "git_blob": "fbff8607e4c61e18481f696b99dd38a32444e90d", + "sha256": "5737d18a7826fd51e131035f2d7bebba2e2868af91dd7672a5f11208b036d424" + }, + { + "path": "research/e8-bit-r420/data/bitE8mut-r420.json.gz", + "usage": "input", + "git_blob": "0c63ca62759dc63d302be6552aeeee1ede70350f", + "sha256": "9721a3819efd68307f66f00d84a952eb28eb7c3ad2ce79653ce295c98a764784" + }, + { + "path": "research/e8-bit-r420/verify.sh", + "usage": "input", + "git_blob": "fbad38c694a0ce4fcbf39110dc403c75017ece07", + "sha256": "3192dea51acd17addb2750834a9dc3fb5b991e8dc5970ba9d2ac54608d79cafa" + }, + { + "path": "research/r420-geometry-cache/cached_xcheck.py", + "usage": "input", + "git_blob": "dfeee952ef8fcaeaa92d9a735ac63eb1ee8e874a", + "sha256": "491b9e3e51508be3e9f6b23567ebdedfe135b0598511c13bb498cd2c5df7bb26" + }, + { + "path": "research/r420-geometry-cache/README.md", + "usage": "provenance", + "git_blob": "bd3989dd7d6349622f49e1b78d53d094421bb97b", + "sha256": "9cc407be328a9d677527192c7f7c326b431c5a1d4b7784df2ee48c84bffd978d" + } + ] +} diff --git a/research/r420-geometry-cache-production/TASK.json b/research/r420-geometry-cache-production/TASK.json new file mode 100644 index 0000000000..85e570534b --- /dev/null +++ b/research/r420-geometry-cache-production/TASK.json @@ -0,0 +1,66 @@ +{ + "schema_version": 1, + "task_id": "replay-runtime", + "priority_revision": 4, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "chooser_seed": "a91c3e7f2b6048d15e9a0c4d6f8b2e71", + "original_card": { + "id": "replay-runtime", + "question": "Can one measured replay bottleneck be sped up without dropping coverage?", + "starting_points": [ + "scripts/run_isolated_tests.py", + "scripts/verify_replay_coverage.py", + "certificates/selected-result.json" + ], + "methods": [ + "Profile one isolated replay and optimize the measured bottleneck.", + "Design source-bound reuse with explicit dependency invalidation and negative controls." + ], + "acceptance_gate": "Report before/after wall time on the same fixture, retained checks and invalidation behavior.", + "first_step": { + "command": ["python3", "-B", "scripts/research_baseline.py"], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Read the selected reproduction command and choose a single replay stage for profiling.", + "scope": "Positive control / source-bound orientation only; this is not a new construction or full cold replay." + }, + "first_experiment": "Measure one unchanged fixture, preserve check coverage, and test one optimization with invalidation controls.", + "bottleneck_link": "Faster trustworthy replays increase review throughput and let mechanism candidates be checked sooner.", + "allowed_variations": [ + "Profile one stage or design one source-bound cache boundary." + ], + "scope_change_triggers": [ + "Skipping coverage or comparing different fixtures to claim speedup." + ], + "overlap_terms": [ + "replay runtime", + "cache invalidation", + "isolated tests" + ] + }, + "scope_status": "refined", + "actual_scope": "Can the production PR543 R420 mutation harness (research/e8-bit-r420/code/mutants.py, exercised by research/e8-bit-r420/verify.sh step 2 on the pinned bitE8mut-r420 word) adopt the exact geometry reuse from research/r420-geometry-cache/ so the shipped path is faster while every mutation stdout field and JSON control receipt stays identical?", + "scope_change_reason": "Expertise override selected verifier-engineering after profiling showed production mutants.py still rebuilt Word.__init__/labels (dominating Fraction RREF) on every gate-only mutation while the incorporated opt-in adapter research/r420-geometry-cache/ already proved identical-receipt geometry reuse across those calls and left production pairwise. Leave-alone objects (joint frames, shared/mixed-point, ternary, parity-side, DisjointTensor.frame_data, verify_factor, leech_minor, sparse-fusion, joint-dual replay, bit-frame frameopt.close, xcheck.rref/#2263, quadratic-affine) and deferred micro-opts were not reopened.", + "bottleneck_link": "Side result on review throughput: the R420 mutation harness is a retained historical interface lemma check. Faster identical geometry reuse does not change kappa or the binding complex supplier.", + "ai_assistance": "Substantial AI assistance for orientation, overlap search, profiling, production wiring, packaging, benchmarking and writing", + "outcome": "completed", + "evidence": "PASS_R420_GEOMETRY_CACHE_PRODUCTION via research/r420-geometry-cache-production/verify.py (also under -O); PASS_IDENTICAL_R420_GEOMETRY_CACHE_PRODUCTION_RECEIPTS via benchmark.py; MANIFEST.sha256 binds the production mutants digest; identical mutation stdout digest 8c2738b3258e…", + "author": "Wentao Li", + "email": "wentaol4@illinois.edu", + "github": "VictorLiwentao", + "budget_minutes": 60, + "stop_rule": "Ship an equivalence-preserving production mutants.py geometry-cache change with frozen baseline, identical mutation receipts, temp-copy corruption rejection, registered replay and a measured before/after median. Do not invent kappa or reopen leave-alone objects.", + "open_questions": [ + { + "question": "Should research/r420-geometry-cache/ note that production mutants.py now ships the same geometry reuse?", + "why_it_matters": "That opt-in package's SOURCES.json pinned the pairwise mutants.py bytes and would fail source admission after this rewrite.", + "first_step": "Demote that pin to provenance in the same tip (done here) and optionally add a one-line status note.", + "blocked_by": "None for the demotion; maintainer preference on further README edits of the historical package." + }, + { + "question": "Should other research/*/SOURCES.json pins of research/e8-bit-r420/code/mutants.py be refreshed?", + "why_it_matters": "Those packages bind working-tree mutants.py bytes as historical provenance; leaving the old pins keeps this tooling PR focused.", + "first_step": "Dedicated provenance follow-up that only rewrites those pin entries after confirming receipt identity.", + "blocked_by": "Pin repair outside registered VERIFY live inputs is outside this tooling change." + } + ] +} diff --git a/research/r420-geometry-cache-production/VERIFY.json b/research/r420-geometry-cache-production/VERIFY.json new file mode 100644 index 0000000000..5e7f97c7ec --- /dev/null +++ b/research/r420-geometry-cache-production/VERIFY.json @@ -0,0 +1,38 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/e8-bit-r420/code/mutants.py", + "research/e8-bit-r420/code/xcheck.py", + "research/e8-bit-r420/data/bitE8mut-r420.json.gz", + "research/e8-bit-r420/MANIFEST.sha256", + "research/e8-bit-r420/verify.sh", + "research/r420-geometry-cache/cached_xcheck.py", + "research/r420-geometry-cache-production/baseline.py" + ], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 600, + "checks": "Exact baseline copy of the base-commit mutants.py; identical mutation stdout and JSON receipts; MANIFEST binds production digest; temp-copy stale-full foil rejection; duplicate JSON-key check; read-only default." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 600, + "checks": "Same package require() gates under -O; mutants/xcheck children still launch assertion-enabled via -I -B without PYTHONOPTIMIZE." + } + ] +} diff --git a/research/r420-geometry-cache-production/baseline.py b/research/r420-geometry-cache-production/baseline.py new file mode 100644 index 0000000000..5b0fa19220 --- /dev/null +++ b/research/r420-geometry-cache-production/baseline.py @@ -0,0 +1,160 @@ +"""Mutation controls for xcheck.py (exact replay and interleaved-interface audit). Every negative control must be +rejected: either the replay raises (frame legality or the exact F2 identity fails), or a hypothesis or conclusion of the +interleaved lemma fails. The positive control must be accepted. +usage: mutants.py XCHECK_PY WORD.json.gz [--json OUT] + +Negative controls (sites are found from the word's structure, not from producer code): + 1 target feeds a source: a cancelled pair x_k += y_t inserted before the target's d-single read of x_k (F2-neutral) + 2 target feeds a helper: a cancelled pair h += y_t inserted before the first target read of a helper h (F2-neutral) + 3 drop a cleanup gate (the literal inverse of the last phase-B source write) + 4 corrupt a phase-B source write: its source operand replaced by another source register of the same block frame, + with the matching cleanup gate changed the same way (C stays the literal reverse; every source is restored) + 5 move a phase-B source write below the source's frame (to the destination source's own line) + 6 a target reads another target (a single read at frame 0 at the start of phase B, so V != I) + 7 corrupt the frame-0 preconditioner (drop one helper read from a D gate) + 8 move a phase-B source write before the target read of that register (the write order relative to the reads) + 9 move a cleanup gate off the full frame (to the frame of the gate it inverts) +Positive control: + P a cancelled pair of target-to-target reads at frame 0 (y_t += y_u twice: V = I, allowed by the lemma) +Prepared by eumemic with substantial Anthropic Claude assistance; Apache-2.0.""" +import copy +import gzip +import importlib.util +import json +import sys + +sp = importlib.util.spec_from_file_location("xcheck", sys.argv[1]) +X = importlib.util.module_from_spec(sp) +sp.loader.exec_module(X) +w0 = json.load(gzip.open(sys.argv[2], "rt")) +v = w0["v"] +is_y = lambda r: v <= r < 2 * v +is_z = lambda r: not is_y(r) +ZERO = next(k for k, f in enumerate(w0["frames"]) if not f) + + +def zgates(w): + return [("A", i) for i, g in enumerate(w["A"]) if is_z(g[1])] + [("B", i) for i, g in enumerate(w["B"]) if is_z(g[1])] + + +def mirror(w, sec, i): + zg = zgates(w) + return len(zg) - 1 - zg.index((sec, i)) + + +def evaluate(w): + try: + W, lab, fl, H, C, info = X.check(w) + except X.Reject as e: + return False, "replay: " + str(e)[:90] + bad = [k for k, x in list(H.items()) + list(C.items()) if not x] + return (not bad), ("interface: fails " + ",".join(bad)) if bad else "accepted" + + +def first_keeper_read(w): + return next(j for j, g in enumerate(w["B"]) if is_y(g[1]) and any(s < v for s in g[2])) + + +def b_source_writes(w): + return [i for i, g in enumerate(w["B"]) if g[1] < v and all(s < v for s in g[2]) and len(g[2]) == 1] + + +def m1(w): + j = first_keeper_read(w) + F, t, srcs = w["B"][j] + xk = next(s for s in srcs if s < v) + w["B"][j:j] = [[F, xk, [t]], [F, xk, [t]]] + return "target feeds a source (cancelled pair, F2-neutral)" + + +def m2(w): + j = next(j for j, g in enumerate(w["B"]) if is_y(g[1]) and any(s >= 2 * v for s in g[2])) + F, t, srcs = w["B"][j] + h = next(s for s in srcs if s >= 2 * v) + w["B"][j:j] = [[F, h, [t]], [F, h, [t]]] + return "target feeds a helper (cancelled pair, F2-neutral)" + + +def m3(w): + last = max(i for i, g in enumerate(w["B"]) if g[1] < v) + p = mirror(w, "B", last) + assert p == 0 + w["C"].pop(p) + return "drop a cleanup gate (inverse of the last phase-B source write)" + + +def m4(w): + i = b_source_writes(w)[0] + F, d, (s,) = w["B"][i] + p = mirror(w, "B", i) + lines = w["frames"][F] + other = next(r for r in lines if r not in (d, s)) + w["B"][i] = [F, d, [other]] + assert w["C"][p][1:] == [d, [s]] + w["C"][p] = [w["C"][p][0], d, [other]] + return "corrupt a phase-B source write (operand %d -> %d, cleanup changed alike)" % (s, other) + + +def m5(w): + i = b_source_writes(w)[0] + F, d, srcs = w["B"][i] + w["B"][i] = [w["start"][d], d, srcs] + return "phase-B source write moved down to the destination's own line frame" + + +def m6(w): + w["B"].insert(0, [ZERO, v + 0, [v + 1]]) + return "a target reads another target (single read, V != I)" + + +def m7(w): + g = next(g for g in w["D"] if len(g[2]) > 1) + g[2].pop() + return "frame-0 preconditioner corrupted (one helper read dropped)" + + +def m8(w): + i = min(i for i, g in enumerate(w["B"]) if is_z(g[1])) + d = w["B"][i][1] + j = next(j for j, g in enumerate(w["B"]) if is_y(g[1]) and d in g[2]) + assert j < i + g = w["B"].pop(i) + w["B"].insert(j, g) + return "phase-B source write moved before the target read of that register" + + +def m9(w): + p = len(w["C"]) - 1 + zg = zgates(w) + sec, i = zg[len(zg) - 1 - p] + w["C"][p][0] = w[sec][i][0] + return "cleanup gate moved off the full frame (to the frame of the gate it inverts)" + + +def pos(w): + w["B"][0:0] = [[ZERO, v + 0, [v + 1]], [ZERO, v + 0, [v + 1]]] + return "cancelled pair of target-to-target reads at frame 0 (V = I)" + + +ok0, msg0 = evaluate(w0) +assert ok0, ("the unmutated word must pass", msg0) +rows = [] +allok = True +for f in (m1, m2, m3, m4, m5, m6, m7, m8, m9): + w = copy.deepcopy(w0) + name = f(w) + acc, msg = evaluate(w) + rows.append(dict(control=f.__name__, mutation=name, rejected=not acc, by=msg)) + allok &= not acc + print(" %s %s :: %s" % ("REJECTED " if not acc else "ACCEPTED!", name, msg)) +w = copy.deepcopy(w0) +name = pos(w) +acc, msg = evaluate(w) +rows.append(dict(control="positive", mutation=name, rejected=not acc, by=msg)) +allok &= acc +print(" %s %s :: %s" % ("accepted " if acc else "REJECTED!", name, msg)) +assert allok, "a mutation control did not behave as required" +print("PASS mutation controls: 9/9 negative controls rejected, 1/1 positive control accepted") +if "--json" in sys.argv: + json.dump(dict(status="PASS_INTERLEAVED_INTERFACE_MUTATION_CONTROLS", controls=rows), + open(sys.argv[sys.argv.index("--json") + 1], "w"), indent=1) diff --git a/research/r420-geometry-cache-production/benchmark.py b/research/r420-geometry-cache-production/benchmark.py new file mode 100644 index 0000000000..d0e820bfce --- /dev/null +++ b/research/r420-geometry-cache-production/benchmark.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +"""Alternating fresh-process before/after timings for production geometry-cache mutants. + +Timing is descriptive evidence for RESULT.json. Equivalence is enforced by +verify.py; this script fails only on unequal receipts or subprocess errors. +""" +from __future__ import annotations + +import argparse +from hashlib import sha256 +import json +import os +from pathlib import Path +import platform +import statistics +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +WORD = ROOT / "research/e8-bit-r420/data/bitE8mut-r420.json.gz" +XCHECK = ROOT / "research/e8-bit-r420/code/xcheck.py" +BASELINE = PACKAGE / "baseline.py" +PRODUCTION = ROOT / "research/e8-bit-r420/code/mutants.py" + +MUTANTS_STDOUT_SHA256 = "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + + +def clean_env(): + env = {k: v for k, v in os.environ.items() if k != "PYTHONOPTIMIZE"} + env["PYTHONDONTWRITEBYTECODE"] = "1" + return env + + +def run_workload(engine: str) -> dict: + harness = BASELINE if engine == "baseline" else PRODUCTION + cmd = [sys.executable, "-I", "-B", str(harness), str(XCHECK), str(WORD)] + start = time.perf_counter() + proc = subprocess.run(cmd, cwd=ROOT, env=clean_env(), capture_output=True) + elapsed = time.perf_counter() - start + if proc.returncode != 0: + raise SystemExit(f"{engine} failed\n{proc.stderr.decode()}\n{proc.stdout.decode()}") + digest = sha256(proc.stdout).hexdigest() + if digest != MUTANTS_STDOUT_SHA256: + raise SystemExit(f"{engine} stdout digest {digest} != {MUTANTS_STDOUT_SHA256}") + return {"elapsed": elapsed, "stdout_sha256": digest, "stdout_bytes": len(proc.stdout)} + + +def unique_object(pairs): + out = {} + for key, value in pairs: + if key in out: + raise SystemExit(f"duplicate JSON key: {key}") + out[key] = value + return out + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--rounds", type=int, default=5) + parser.add_argument( + "--write-result", + action="store_true", + help="write RESULT.json (default prints only; use explicitly to refresh the artifact)", + ) + args = parser.parse_args(argv) + if args.rounds < 3: + raise SystemExit("Use at least three alternating rounds") + + records = [] + for pair in range(args.rounds): + order = ("baseline", "production") if pair % 2 == 0 else ("production", "baseline") + outputs = {} + for engine in order: + row = run_workload(engine) + records.append( + { + "pair": pair, + "engine": engine, + "seconds": row["elapsed"], + "stdout_sha256": row["stdout_sha256"], + } + ) + outputs[engine] = row["stdout_sha256"] + print( + f"pair={pair} engine={engine} seconds={row['elapsed']:.6f}", + flush=True, + ) + if outputs["baseline"] != outputs["production"]: + raise SystemExit(f"pair {pair} receipts differ") + + medians = { + engine: statistics.median(r["seconds"] for r in records if r["engine"] == engine) + for engine in ("baseline", "production") + } + result = { + "status": "PASS_IDENTICAL_R420_GEOMETRY_CACHE_PRODUCTION_RECEIPTS", + "no_new_kappa": True, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "python": sys.version, + "platform": platform.platform(), + "machine": platform.machine(), + "interpreter": f"CPython {platform.python_version()}", + "rounds": args.rounds, + "timing_scope": ( + "fresh subprocess wall time for the complete mutants.py harness " + "(1 honest + 9 negative + 1 positive) including imports; warm OS caches, " + "not flushed; host-descriptive only; not a correctness gate" + ), + "records": records, + "median_seconds": medians, + "speedup": medians["baseline"] / medians["production"], + "mutants_stdout_sha256": MUTANTS_STDOUT_SHA256, + "production_mutants_sha256": sha256(PRODUCTION.read_bytes()).hexdigest(), + "baseline_mutants_sha256": sha256(BASELINE.read_bytes()).hexdigest(), + "xcheck_sha256": sha256(XCHECK.read_bytes()).hexdigest(), + } + + text = json.dumps(result, indent=2, sort_keys=True) + "\n" + json.loads(text, object_pairs_hook=unique_object) + if args.write_result: + (PACKAGE / "RESULT.json").write_text(text, encoding="utf-8") + print(result["status"]) + print( + "controls", + medians, + "speedup", + round(result["speedup"], 3), + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/research/r420-geometry-cache-production/verify.py b/research/r420-geometry-cache-production/verify.py new file mode 100644 index 0000000000..94377da9e5 --- /dev/null +++ b/research/r420-geometry-cache-production/verify.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 +"""Equivalence, rejection and receipt controls for production geometry-cache mutants. + +Read-only by default. Timing is not a pass gate; see benchmark.py / RESULT.json. +""" +from __future__ import annotations + +import argparse +from hashlib import sha256 +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +PRODUCTION = ROOT / "research/e8-bit-r420/code/mutants.py" +BASELINE = PACKAGE / "baseline.py" +XCHECK = ROOT / "research/e8-bit-r420/code/xcheck.py" +WORD = ROOT / "research/e8-bit-r420/data/bitE8mut-r420.json.gz" +MANIFEST = ROOT / "research/e8-bit-r420/MANIFEST.sha256" + +BASE_COMMIT = "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2" +ORIGINAL_BLOB = "5b0fa1922076d2e91367b58483c96b33b7b08661" +ORIGINAL_SHA256 = "e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594" +OPTIMIZED_SHA256 = "bc6b010641dd3b2f6f33928ce2004189e0671bd9865eab7e70cfcd0d0c6cf9b9" +MUTANTS_STDOUT_SHA256 = "8c2738b3258e207370597b60c2ebd318e740fc0feaca04cb8b0e3a26089258d8" + + +def require(condition, message): + if not condition: + raise AssertionError(message) + + +def digest_file(path: Path) -> str: + return sha256(path.read_bytes()).hexdigest() + + +def unique_object(pairs): + out = {} + for key, value in pairs: + require(key not in out, f"duplicate JSON key: {key}") + out[key] = value + return out + + +def clean_env(): + env = {k: v for k, v in os.environ.items() if k != "PYTHONOPTIMIZE"} + env["PYTHONDONTWRITEBYTECODE"] = "1" + return env + + +def run_mutants(harness: Path) -> bytes: + proc = subprocess.run( + [sys.executable, "-I", "-B", str(harness), str(XCHECK), str(WORD)], + cwd=ROOT, + env=clean_env(), + capture_output=True, + ) + require( + proc.returncode == 0, + f"mutants harness {harness.name} failed\n{proc.stderr.decode()}\n{proc.stdout.decode()}", + ) + return proc.stdout + + +def run_mutants_json(harness: Path, out: Path) -> dict: + proc = subprocess.run( + [ + sys.executable, + "-I", + "-B", + str(harness), + str(XCHECK), + str(WORD), + "--json", + str(out), + ], + cwd=ROOT, + env=clean_env(), + capture_output=True, + text=True, + ) + require( + proc.returncode == 0, + f"mutants json harness {harness.name} failed\n{proc.stderr}\n{proc.stdout}", + ) + return json.loads(out.read_text(encoding="utf-8"), object_pairs_hook=unique_object) + + +def check_baseline_bytes() -> None: + original = subprocess.check_output( + ["git", "show", f"{BASE_COMMIT}:research/e8-bit-r420/code/mutants.py"], + cwd=ROOT, + ) + require(sha256(original).hexdigest() == ORIGINAL_SHA256, "base mutants.py sha256 drift") + blob = ( + subprocess.check_output( + ["git", "rev-parse", f"{BASE_COMMIT}:research/e8-bit-r420/code/mutants.py"], + cwd=ROOT, + ) + .decode() + .strip() + ) + require(blob == ORIGINAL_BLOB, "base mutants.py blob drift") + require(BASELINE.read_bytes() == original, "baseline.py must equal the base-commit mutants.py") + require( + digest_file(PRODUCTION) == OPTIMIZED_SHA256, + "production mutants.py does not match the claimed optimized digest", + ) + require(PRODUCTION.read_bytes() != original, "production must differ from the pairwise baseline") + + +def check_manifest_binds_production() -> None: + listed = {} + for line in MANIFEST.read_text(encoding="utf-8").splitlines(): + digest, path = line.split(" ", 1) + require(path not in listed, f"duplicate MANIFEST path: {path}") + listed[path] = digest + require( + listed.get("code/mutants.py") == OPTIMIZED_SHA256, + "MANIFEST.sha256 must bind production mutants.py", + ) + require(digest_file(PRODUCTION) == OPTIMIZED_SHA256, "live mutants.py drifted from OPTIMIZED_SHA256") + + +def check_stdout_equivalence() -> dict: + base_out = run_mutants(BASELINE) + prod_out = run_mutants(PRODUCTION) + require(base_out == prod_out, "mutation-harness stdout differs") + require(sha256(prod_out).hexdigest() == MUTANTS_STDOUT_SHA256, "mutants stdout digest drift") + require(b"PASS mutation controls: 9/9" in prod_out, "missing mutation control marker") + with tempfile.TemporaryDirectory(prefix="r420-geom-prod-json-") as tmp: + base_receipt = run_mutants_json(BASELINE, Path(tmp) / "base.json") + prod_receipt = run_mutants_json(PRODUCTION, Path(tmp) / "prod.json") + require(base_receipt == prod_receipt, "mutation JSON receipts differ") + require( + base_receipt["status"] == "PASS_INTERLEAVED_INTERFACE_MUTATION_CONTROLS", + "unexpected receipt status", + ) + require(len(base_receipt["controls"]) == 10, "expected 10 controls") + require(all(row["rejected"] for row in base_receipt["controls"][:9]), "negative controls") + require(not base_receipt["controls"][9]["rejected"], "positive control must accept") + return { + "mutants_stdout_sha256": MUTANTS_STDOUT_SHA256, + "mutants_bytes": len(prod_out), + "controls": 10, + } + + +def check_temp_corruption_rejection() -> dict: + """On-disk foil that reuses full()/interface on geometry hits must diverge. + + Gate-only mutants share geometry with the honest word. Caching those later + receipts would wrongly accept negative controls. The honest cache always + reruns full() and interface() on a fresh Word. + """ + honest = run_mutants(PRODUCTION) + with tempfile.TemporaryDirectory(prefix="r420-geom-prod-") as tmp: + mutant_path = Path(tmp) / "mutants.py" + text = PRODUCTION.read_text(encoding="utf-8") + needle = ( + " labels = dict(snapshot[8])\n" + " full = obj.full()\n" + " hypotheses, conclusion, info = obj.interface()\n" + " return obj, labels, full, hypotheses, conclusion, info\n" + ) + require(needle in text, "geometry hit tail missing for corruption control") + foil = ( + " labels = dict(snapshot[8])\n" + " # foil: reuse prior full/interface bundled into the geometry snapshot\n" + " if len(snapshot) > 9:\n" + " return obj, labels, snapshot[9], snapshot[10], snapshot[11], snapshot[12]\n" + " full = obj.full()\n" + " hypotheses, conclusion, info = obj.interface()\n" + " self._entries[key] = snapshot + (full, hypotheses, conclusion, info)\n" + " return obj, labels, full, hypotheses, conclusion, info\n" + ) + # Also widen the miss-path snapshot write so the foil can store extras. + # The simple approach above fails because snapshot is a local tuple from + # the hit path; force a clear behavioral break by skipping full()/interface + # entirely on hits and returning vacuous acceptance-shaped empties. + foil = ( + " labels = dict(snapshot[8])\n" + " # foil: skip full()/interface on geometry hits\n" + " return obj, labels, True, {}, {}, {}\n" + ) + mutant_path.write_text(text.replace(needle, foil, 1), encoding="utf-8") + require(mutant_path.read_bytes() != PRODUCTION.read_bytes(), "mutant must differ on disk") + try: + bad = run_mutants(mutant_path) + except AssertionError as exc: + return {"rejected": True, "mode": "subprocess_failure", "detail": str(exc)[:240]} + require(bad != honest, "stale-full foil must change the mutation stdout") + return {"rejected": True, "mode": "stdout_divergence"} + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--write-result", + action="store_true", + help="unused placeholder; default verify is read-only and never writes certificates", + ) + args = parser.parse_args(argv) + require(not args.write_result, "default verify is read-only; do not pass --write-result") + + check_baseline_bytes() + check_manifest_binds_production() + receipts = check_stdout_equivalence() + corruption = check_temp_corruption_rejection() + + print( + "PASS_R420_GEOMETRY_CACHE_PRODUCTION " + f"controls={receipts['controls']} " + f"corruption={corruption['mode']} " + f"mutants_sha={MUTANTS_STDOUT_SHA256[:12]} " + "certs=0 no_new_kappa" + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/research/r420-geometry-cache/README.md b/research/r420-geometry-cache/README.md index bd3989dd7d..88ea660367 100644 --- a/research/r420-geometry-cache/README.md +++ b/research/r420-geometry-cache/README.md @@ -6,6 +6,11 @@ previously validated E8 label/frame calculation for exactly matching inputs. Every word still receives the predecessor's full scalar, frame-path, dirty restoration and H1–H5/interface checks. +**Status note:** production `research/e8-bit-r420/code/mutants.py` now ships the +same geometry reuse (`research/r420-geometry-cache-production/`). This package +remains the historical opt-in `cached_xcheck.py` adapter; its pairwise-era +`mutants.py` digest is provenance only. + On macOS arm64 / Python 3.12.3, one sequential, same-input A/B took **8.239 seconds → 1.273 seconds (6.47× faster, 84.5% less wall time)**. Both runs started separate processes with empty application caches. The original diff --git a/research/r420-geometry-cache/SOURCES.json b/research/r420-geometry-cache/SOURCES.json index 615227700a..7b239d8f36 100644 --- a/research/r420-geometry-cache/SOURCES.json +++ b/research/r420-geometry-cache/SOURCES.json @@ -6,13 +6,16 @@ "sha256": "5737d18a7826fd51e131035f2d7bebba2e2868af91dd7672a5f11208b036d424", "git_blob": "fbff8607e4c61e18481f696b99dd38a32444e90d" }, - "research/e8-bit-r420/code/mutants.py": { - "sha256": "e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594", - "git_blob": "5b0fa1922076d2e91367b58483c96b33b7b08661" - }, "research/e8-bit-r420/data/bitE8mut-r420.json.gz": { "sha256": "9721a3819efd68307f66f00d84a952eb28eb7c3ad2ce79653ce295c98a764784", "git_blob": "0c63ca62759dc63d302be6552aeeee1ede70350f" } + }, + "provenance_inputs": { + "research/e8-bit-r420/code/mutants.py": { + "note": "Pairwise-era harness digest retained as provenance after production geometry reuse in research/r420-geometry-cache-production/; not a live pin.", + "sha256": "e2fd3429e2db4389ae8a5ab47dda89f551b0d0952c42fad5ac8d38a85d670594", + "git_blob": "5b0fa1922076d2e91367b58483c96b33b7b08661" + } } }