diff --git a/certificates/joint-dual-kappa.json b/certificates/joint-dual-kappa.json index 3373a52446..c45f72c254 100644 --- a/certificates/joint-dual-kappa.json +++ b/certificates/joint-dual-kappa.json @@ -835,14 +835,14 @@ "scripts/experiments/binary_frame_math.py": "1f96e8aafc8ae89f8eeec5d03800131d93d58e01bf8a7fca8feb03702d1740d0", "scripts/experiments/binary_frame_profile_prepare.py": "16dea3d3ef3dc90db928ed3e35d002f6926d2e7388da4766fc2fd725550f739e", "scripts/experiments/binary_frame_profiles.cpp": "8e2e391d3ee57cbb1372c68e4aa031e1b861321536eabd642ada54e12e678669", - "scripts/experiments/binary_frame_replay.py": "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9", + "scripts/experiments/binary_frame_replay.py": "a1cbab2900440baa9b32f482f1531518fd95d55de2fe30e40e7a5249420b33a0", "scripts/experiments/joint_dual_compiler.py": "853d3fc4a740c5b74c23b3a6af5d4dc9b41fb38e9b55678539a7e0ee71947c13", "scripts/experiments/joint_dual_compose.py": "3554e957570e2d3f65c5bd7c945338c9e2fc5f3e79efa2b436a4dfc61899c394", "scripts/experiments/joint_dual_reclaim_compiler.py": "edab206b83cfe3f7a08f1b605b182e354c87cc24e2f3ad81a95f7a083ff342fb", "scripts/experiments/pin_joint_dual_sources.py": "5d41535a73ad62fae7acb5365c5b7f40d7c99e52b6bc4266e44572018080acc5", "scripts/experiments/verify_joint_dual.py": "7a31bba1e9c0ded32ae285df2ca9bc91ef20c330f6789fdba361876f3c7a1985" }, - "source_manifest_sha256": "330fac72bad557ef4ae999298121c7d63ce237e23bb3184222c9bd6b54264687", + "source_manifest_sha256": "6d37a7c0df19a8b16e3b0d6501d0b92f9985704fe988a94517a50f85141af202", "sources": [ { "files": { diff --git a/certificates/skip-frame-kappa.json b/certificates/skip-frame-kappa.json index b7e35f864b..dbfdfb79ac 100644 --- a/certificates/skip-frame-kappa.json +++ b/certificates/skip-frame-kappa.json @@ -825,7 +825,7 @@ "scripts/experiments/binary_frame_math.py": "1f96e8aafc8ae89f8eeec5d03800131d93d58e01bf8a7fca8feb03702d1740d0", "scripts/experiments/binary_frame_profile_prepare.py": "16dea3d3ef3dc90db928ed3e35d002f6926d2e7388da4766fc2fd725550f739e", "scripts/experiments/binary_frame_profiles.cpp": "8e2e391d3ee57cbb1372c68e4aa031e1b861321536eabd642ada54e12e678669", - "scripts/experiments/binary_frame_replay.py": "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9", + "scripts/experiments/binary_frame_replay.py": "a1cbab2900440baa9b32f482f1531518fd95d55de2fe30e40e7a5249420b33a0", "scripts/experiments/skip_frame_compiler.py": "8ffd079fd2e4f055b5befefecc5f33e686de9898219e9252f5d3913df8b195d8", "scripts/experiments/skip_frame_compose.py": "d48e688ebb9a490f4eaf50d9ab0a4d4e3e76b6588b55d213df14c703c27a7f66", "scripts/experiments/verify_skip_frame.py": "fc42dc3d59239ee3908d0e3e95402eb53fd6815a100521911fde1b541070c292" diff --git a/research/joint-dual-replay-production/NOTICE.md b/research/joint-dual-replay-production/NOTICE.md new file mode 100644 index 0000000000..563302513c --- /dev/null +++ b/research/joint-dual-replay-production/NOTICE.md @@ -0,0 +1,19 @@ +# Attribution and license + +Prepared by Wentao Li ([VictorLiwentao](https://github.com/VictorLiwentao), +[@Is__Victor](https://x.com/Is__Victor), wentaol4@illinois.edu) with substantial +AI assistance for orientation, overlap search, profiling, production wiring, +packaging, benchmarking and writing. Not independent human peer review. +Apache-2.0; see the repository root `LICENSE`. + +The exact containment-mask acceleration of the per-output triple scans was +introduced by **Aaron Wang** in the opt-in adapter +`research/joint-dual-replay-fast/` (see that package's README and TASK.json). +This package wires an equivalent check into the production +`scripts/experiments/binary_frame_replay.py:replay` path used by +`make joint-dual-verify`. + +The original joint-dual serialized-word verifier, PR64 scatter multiset control, +compiler receipts and scientific certificate are by **Chafik Boukhalfa** and the +retained frame-compiler lineage. Upstream material retains its own source +notices and license. diff --git a/research/joint-dual-replay-production/README.md b/research/joint-dual-replay-production/README.md new file mode 100644 index 0000000000..3258c2c062 --- /dev/null +++ b/research/joint-dual-replay-production/README.md @@ -0,0 +1,105 @@ +# Production containment masks for joint-dual word replay + +**Tooling improvement; no new kappa.** At base +`0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2`, +`scripts/experiments/binary_frame_replay.py:replay` still scanned every +weight-three triple for each paid output. Aaron Wang's opt-in adapter +`research/joint-dual-replay-fast/` already proved an exact containment-mask +reduction with identical receipts. This package wires that equivalent check +into the **production** source used by `make joint-dual-verify`. + +Compiler replay receipts in `certificates/joint-dual-compiler.json` stay +**byte-identical**. Only the source digests for +`scripts/experiments/binary_frame_replay.py` change inside +`research/joint-dual/SOURCE.json`, `certificates/joint-dual-kappa.json` and +`certificates/skip-frame-kappa.json`. Every assertion, `check_word` control +(including the PR64 scatter multiset), both full-basis orientations and the +histogram/rank-mass fields are unchanged. Selected frontier untouched. + +## Reproduce + +Python **3.12**, standard library only, from the repository root: + +```sh +python3.12 -I -B research/joint-dual-replay-production/verify.py +python3.12 -I -O -B research/joint-dual-replay-production/verify.py +python3.12 -B scripts/research_package.py --package research/joint-dual-replay-production +python3.12 -I -B research/joint-dual-replay-production/benchmark.py --rounds 5 +``` + +Expected: + +```text +PASS_JOINT_DUAL_REPLAY_PRODUCTION words=[23, 25] outputs=12261 receipts=2 no_new_kappa +PASS_JOINT_DUAL_REPLAY_PRODUCTION_O_POLICY refused_by_assert_gate=2 no_new_kappa +PASS_REGISTERED_RESEARCH_PACKAGE research/joint-dual-replay-production outcome=completed +PASS_IDENTICAL_JOINT_DUAL_REPLAY_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 replaying both +pinned words (`h=23` and `h=25`) gave median wall time **5.873 → 3.120 s +(1.882×)**. Retained samples are in [`RESULT.json`](RESULT.json). + +## Why the masks are equivalent + +For `triples = list(combinations(range(h), 3))`, bit `i` of `center[c]` is set +iff `c in triples[i]`, and bit `i` of `pair[c][x]` is set iff +`{c, x} ⊆ triples[i]`. Then + +```text +expected(common, triple) = center[common] + & ~pair[common][x] for each x in set(triple) - {common} +``` + +equals the historical scan + +```text +sum(1 << i for i, t in enumerate(triples) + if common in t and not (set(t) & (set(triple) - {common}))) +``` + +Destination lists for copied centres are the same index lists as +`[i for i, t in enumerate(triples) if common in t]`. Frame lookup preserves +first-match `F.index` semantics. This is the same argument as +`research/joint-dual-replay-fast/README.md`. + +## Overlap and credit + +Closest priors: + +- [Aaron Wang / joint-dual-replay-fast](../joint-dual-replay-fast/) — opt-in + containment-mask 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 production + sparse-fusion orbits, #1861 parity-side) was not reopened. Fresh overlap + refresh over open PRs found no competing production rewrite of + `binary_frame_replay.replay`. + +Chafik Boukhalfa authored the joint-dual verifier and certificates. +Aaron Wang authored the containment-mask algorithm. 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 `research/joint-dual-replay-fast/` note that production + now ships the same containment-mask scan? + **Why it matters:** That package's source-admission pins expect the + historical pairwise enumeration block. + **First step:** Add a one-line status note without editing pinned evidence. + **Blocked by:** Maintainer preference on touching incorporated historical + packages. + +2. **Question:** After mask wiring, is the remaining full-basis word simulation + the next productive production pass? + **Why it matters:** With the per-output scans reduced, dual-orientation XOR + simulation dominates residual replay time. + **First step:** Profile production `replay` with masks merged. + **Blocked by:** No open identical-receipt adapter yet for that stage. diff --git a/research/joint-dual-replay-production/RESULT.json b/research/joint-dual-replay-production/RESULT.json new file mode 100644 index 0000000000..d7b961b8eb --- /dev/null +++ b/research/joint-dual-replay-production/RESULT.json @@ -0,0 +1,86 @@ +{ + "ai_assistance": "Substantial AI assistance for orientation, overlap search, profiling, production wiring, packaging, benchmarking and writing. Not independent human peer review.", + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "both_words_replay": { + "median_baseline_seconds": 5.87312843299992, + "median_optimized_seconds": 3.1202947950000635, + "median_ratio": 1.882235115224041, + "rounds": 5, + "samples": [ + { + "baseline_seconds": 5.888532680000026, + "optimized_seconds": 3.4563439109999763, + "order": [ + "baseline", + "optimized" + ], + "ratio": 1.7036882994367242, + "round": 0 + }, + { + "baseline_seconds": 5.776117523000039, + "optimized_seconds": 3.1633046890001424, + "order": [ + "optimized", + "baseline" + ], + "ratio": 1.8259757092276037, + "round": 1 + }, + { + "baseline_seconds": 5.87312843299992, + "optimized_seconds": 3.08962101599991, + "order": [ + "baseline", + "optimized" + ], + "ratio": 1.9009219585785246, + "round": 2 + }, + { + "baseline_seconds": 5.924514798000018, + "optimized_seconds": 3.085845221999989, + "order": [ + "optimized", + "baseline" + ], + "ratio": 1.9199001802690023, + "round": 3 + }, + { + "baseline_seconds": 5.381213688000116, + "optimized_seconds": 3.1202947950000635, + "order": [ + "baseline", + "optimized" + ], + "ratio": 1.7245850285117073, + "round": 4 + } + ], + "words": [ + "certificates/joint-dual-word-23.json.gz", + "certificates/joint-dual-word-25.json.gz" + ] + }, + "engines": { + "baseline": "research/joint-dual-replay-production/baseline.py", + "optimized": "scripts/experiments/binary_frame_replay.py" + }, + "environment": { + "cpu_affinity_set": false, + "executable": "/usr/bin/python3.12", + "implementation": "CPython", + "os_caches_flushed": false, + "platform": "Linux-6.12.94+-x86_64-with-glibc2.39", + "python": "3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0]" + }, + "no_new_kappa": true, + "predecessor": "research/joint-dual-replay-fast/", + "source_sha256": { + "baseline.py": "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9", + "research/joint-dual-replay-fast/fast_replay.py": "dd96fe7e6f9403a519a06b93fec6420532858b5857762d567793c671dd62f2d0", + "scripts/experiments/binary_frame_replay.py": "a1cbab2900440baa9b32f482f1531518fd95d55de2fe30e40e7a5249420b33a0" + }, + "status": "PASS_IDENTICAL_JOINT_DUAL_REPLAY_PRODUCTION_RECEIPTS" +} diff --git a/research/joint-dual-replay-production/SOURCES.json b/research/joint-dual-replay-production/SOURCES.json new file mode 100644 index 0000000000..474535f6a2 --- /dev/null +++ b/research/joint-dual-replay-production/SOURCES.json @@ -0,0 +1,60 @@ +{ + "schema_version": 1, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "files": [ + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "e8870e5917057d59b730eb14721a778639eca050", + "sha256": "d46eb6d5b3e279428753d770653663fbdf1063e55c255ea073d768f344927ef7" + }, + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "ae6a99f28aa468649eb66a361e764d769363bf11", + "sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32" + }, + { + "path": "certificates/joint-dual-compiler.json", + "usage": "input", + "git_blob": "919fff2e6dac36adecf8ab4503a0d6fd65605e61", + "sha256": "516c4d85158553dabd0a98216a064b2a7397957a9d5dd0df1d6e5aec53a4e805" + }, + { + "path": "certificates/joint-dual-word-23.json.gz", + "usage": "input", + "git_blob": "143f2684b069c9c6bc034b91fa5deb3ddab3abf7", + "sha256": "634da1cb39634be15c6d4070132c6bdf4f5a927331cac3b4f13dc995dd799b6b" + }, + { + "path": "certificates/joint-dual-word-25.json.gz", + "usage": "input", + "git_blob": "fc67bf3a5789da29392ca8f82ff0fbed906af2e3", + "sha256": "dd2223d7ee0b91c4445f5acd0b7cad945f56f4e16456b6818f53ddef64a0cb16" + }, + { + "path": "scripts/experiments/binary_frame_replay.py", + "usage": "provenance", + "git_blob": "a4897e5ecb5c92404257e85c8558e7264f0ef0bb", + "sha256": "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9" + }, + { + "path": "tests/test_joint_reclaim.py", + "usage": "provenance", + "git_blob": "c94302af4c27e6797baec4fa8fe3147dc088f61d", + "sha256": "c4e2817bf6e699f462aa3c589ee45e24ab0dcad6bcdb6152ca2a1e16ac7bffeb" + }, + { + "path": "research/joint-dual-replay-fast/fast_replay.py", + "usage": "input", + "git_blob": "a14c466421138b500eb85798c68bfeafe72d29d0", + "sha256": "dd96fe7e6f9403a519a06b93fec6420532858b5857762d567793c671dd62f2d0" + }, + { + "path": "research/joint-dual-replay-fast/README.md", + "usage": "provenance", + "git_blob": "a3863aa62d35ee03ab867a105782014fffd6448f", + "sha256": "532d668b38e66167f8b6db77ae2d004964db05272d4596364ff1f55d80f4b5de" + } + ] +} diff --git a/research/joint-dual-replay-production/TASK.json b/research/joint-dual-replay-production/TASK.json new file mode 100644 index 0000000000..5b46d0ae68 --- /dev/null +++ b/research/joint-dual-replay-production/TASK.json @@ -0,0 +1,70 @@ +{ + "schema_version": 1, + "task_id": "replay-runtime", + "priority_revision": 4, + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "chooser_seed": "3518c0ee1ad394869a4a6efb529a5503", + "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 joint-dual serialized-word replay (scripts/experiments/binary_frame_replay.py:replay, exercised by make joint-dual-verify on certificates/joint-dual-word-{23,25}.json.gz) adopt the exact containment-mask scan from research/joint-dual-replay-fast/ so the shipped verify path is faster while every assertion, check_word control, full-basis orientation and compiler replay receipt stays identical?", + "scope_change_reason": "The catalogue card asks for one measured replay bottleneck with before/after wall time and retained checks. Expertise override selected verifier-engineering after profiling make verify poles and scanning proved opt-in adapters still pairwise/slow on the shipped path. Leave-alone objects (#1922 joint frames, #2054/#2150 shared/mixed-point, #2100 ternary, #2117 DisjointTensor.frame_data, #2194 production verify_factor, #2210 production leech_minor, #2231 production sparse-fusion orbits, #1861 parity-side, quadratic-affine deferred for #1076) were not reopened. Aaron Wang's incorporated joint-dual-replay-fast adapter already proved an opt-in identical-receipt accelerator; production still used the O(outputs x triples) literal scans.", + "bottleneck_link": "Side result on review throughput: joint-dual-verify is part of make verify-joint. Faster identical replay receipts do 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_JOINT_DUAL_REPLAY_PRODUCTION via research/joint-dual-replay-production/verify.py (also -O policy control); PASS_IDENTICAL_JOINT_DUAL_REPLAY_PRODUCTION_RECEIPTS via benchmark.py; compiler replay receipts unchanged; SOURCE/kappa/skip-frame pins updated only for the production source digest.", + "author": "Wentao Li", + "email": "wentaol4@illinois.edu", + "github": "VictorLiwentao", + "budget_minutes": 60, + "stop_rule": "Ship an equivalence-preserving production binary_frame_replay.replay change with frozen baseline, identical word receipts, rejection/temp-corruption controls, updated source pins only, registered replay and a measured before/after median. Do not invent kappa or reopen leave-alone objects.", + "open_questions": [ + { + "question": "Should research/joint-dual-replay-fast/ note that production now ships the same containment-mask scan?", + "why_it_matters": "That opt-in package's source-admission pins expect the historical pairwise enumeration block in scripts/experiments/binary_frame_replay.py.", + "first_step": "Add a one-line status note to that package README without editing its pinned evidence.", + "blocked_by": "Maintainer preference on touching incorporated historical packages." + }, + { + "question": "After mask wiring, is the remaining full-basis word simulation the next productive production pass?", + "why_it_matters": "With the per-output scans reduced, the dual-orientation XOR word dominates residual replay time.", + "first_step": "Profile production replay with masks merged and name any further exact reuse.", + "blocked_by": "No open identical-receipt adapter yet for the full-basis stage." + } + ] +} diff --git a/research/joint-dual-replay-production/VERIFY.json b/research/joint-dual-replay-production/VERIFY.json new file mode 100644 index 0000000000..82dd58537e --- /dev/null +++ b/research/joint-dual-replay-production/VERIFY.json @@ -0,0 +1,29 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/joint-dual-replay-fast/fast_replay.py", + "certificates/joint-dual-compiler.json", + "certificates/joint-dual-word-23.json.gz", + "certificates/joint-dual-word-25.json.gz", + "certificates/joint-dual-kappa.json", + "certificates/skip-frame-kappa.json", + "research/joint-dual/SOURCE.json", + "scripts/experiments/binary_frame_replay.py", + "tests/test_joint_reclaim.py" + ], + "commands": [ + { + "argv": ["{python}", "-I", "-B", "{package}/verify.py"], + "timeout_seconds": 300, + "checks": "Exact baseline copy of the base-commit binary_frame_replay.py; identical replay receipts on both pinned words versus baseline and certificates/joint-dual-compiler.json; full literal-scan mask identity on all outputs; rejection and temp-copy pair-mask-omission controls; SOURCE/kappa/skip-frame pin consistency; duplicate JSON-key check; -O module refusal control; read-only default." + }, + { + "argv": ["{python}", "-I", "-O", "-B", "{package}/verify.py"], + "timeout_seconds": 60, + "checks": "Deliberate -O control: production and baseline modules refuse to load with Assertions must remain enabled (historical assert policy retained)." + } + ] +} diff --git a/research/joint-dual-replay-production/baseline.py b/research/joint-dual-replay-production/baseline.py new file mode 100644 index 0000000000..a4897e5ecb --- /dev/null +++ b/research/joint-dual-replay-production/baseline.py @@ -0,0 +1,104 @@ +"""Independent replay of serialized XOR word and original-envelope frames.""" +import json,sys,gzip +from pathlib import Path +from itertools import combinations +from collections import Counter +from math import comb + +if sys.flags.optimize: + raise ValueError('Assertions must remain enabled') + + +def check_word(d): + """Bind physical indices and every scatter incidence to the output plan. + + PR #64's separate audit (rfu08) identified the cancelling-scatter gap: + https://github.com/CrocSwap/integer-mult-bounds/pull/64 + Scatter gates commute: each changes an output using an unchanged scratch + source. Compare multisets so reordering remains valid, but extra gates do not. + """ + h,v,R=d['h'],d['v'],d['R'] + assert type(h) is int and h>=3 + assert type(v) is int and v==comb(h,3) + assert type(R) is int and R>=v + frames=d['frames'];universe=(1< dict: + env = os.environ.copy() + env.pop("PYTHONOPTIMIZE", None) + env["PYTHONDONTWRITEBYTECODE"] = "1" + proc = subprocess.run( + [sys.executable, "-I", "-B", "-c", WORKER, str(ROOT), engine, *WORDS], + cwd=ROOT, + env=env, + capture_output=True, + text=True, + ) + if proc.returncode != 0: + raise SystemExit(f"engine={engine} failed\n{proc.stderr}\n{proc.stdout}") + return json.loads(proc.stdout.strip().splitlines()[-1]) + + +def measure(rounds: int) -> dict: + baseline_samples = [] + optimized_samples = [] + reference = None + samples = [] + for round_index in range(rounds): + order = ("baseline", "optimized") if round_index % 2 == 0 else ("optimized", "baseline") + pair = {} + for engine in order: + payload = run_engine(engine) + if reference is None: + reference = payload["receipts"] + elif payload["receipts"] != reference: + raise SystemExit(f"receipt mismatch round={round_index} engine={engine}") + pair[engine] = payload["elapsed"] + baseline_samples.append(pair["baseline"]) + optimized_samples.append(pair["optimized"]) + samples.append( + { + "round": round_index, + "order": list(order), + "baseline_seconds": pair["baseline"], + "optimized_seconds": pair["optimized"], + "ratio": pair["baseline"] / pair["optimized"] if pair["optimized"] else None, + } + ) + print( + f"round={round_index} order={order[0]}->{order[1]} " + f"baseline={pair['baseline']:.6f}s optimized={pair['optimized']:.6f}s", + flush=True, + ) + base_med = statistics.median(baseline_samples) + opt_med = statistics.median(optimized_samples) + return { + "rounds": rounds, + "words": list(WORDS), + "median_baseline_seconds": base_med, + "median_optimized_seconds": opt_med, + "median_ratio": base_med / opt_med if opt_med else None, + "samples": samples, + } + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--rounds", type=int, default=5) + parser.add_argument("--output", type=Path, default=PACKAGE / "RESULT.json") + args = parser.parse_args(argv) + if args.rounds < 1: + raise SystemExit("--rounds must be >= 1") + + replay = measure(args.rounds) + result = { + "status": "PASS_IDENTICAL_JOINT_DUAL_REPLAY_PRODUCTION_RECEIPTS", + "ai_assistance": ( + "Substantial AI assistance for orientation, overlap search, profiling, " + "production wiring, packaging, benchmarking and writing. Not independent " + "human peer review." + ), + "base_commit": "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2", + "predecessor": "research/joint-dual-replay-fast/", + "engines": { + "baseline": "research/joint-dual-replay-production/baseline.py", + "optimized": "scripts/experiments/binary_frame_replay.py", + }, + "source_sha256": { + "baseline.py": sha256((PACKAGE / "baseline.py").read_bytes()).hexdigest(), + "scripts/experiments/binary_frame_replay.py": sha256( + (ROOT / "scripts/experiments/binary_frame_replay.py").read_bytes() + ).hexdigest(), + "research/joint-dual-replay-fast/fast_replay.py": sha256( + (ROOT / "research/joint-dual-replay-fast/fast_replay.py").read_bytes() + ).hexdigest(), + }, + "environment": { + "python": sys.version, + "executable": sys.executable, + "platform": platform.platform(), + "implementation": platform.python_implementation(), + "os_caches_flushed": False, + "cpu_affinity_set": False, + }, + "both_words_replay": replay, + "no_new_kappa": True, + } + args.output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8") + print(result["status"]) + print( + f"replay_median {replay['median_baseline_seconds']:.6f} -> " + f"{replay['median_optimized_seconds']:.6f} ({replay['median_ratio']:.3f}x)" + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/research/joint-dual-replay-production/verify.py b/research/joint-dual-replay-production/verify.py new file mode 100644 index 0000000000..c7374423a6 --- /dev/null +++ b/research/joint-dual-replay-production/verify.py @@ -0,0 +1,240 @@ +#!/usr/bin/env python3 +"""Equivalence, rejection and pin controls for production joint-dual replay. + +Read-only by default. Timing is not a pass gate; see benchmark.py / RESULT.json. +Under -O this script only checks that production and baseline modules refuse to +load (historical assert policy); full equivalence runs with assertions enabled. +""" +from __future__ import annotations + +import argparse +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +EXPERIMENTS = ROOT / "scripts" / "experiments" + +BASE_COMMIT = "0fdd7d82de03e43c9e5a1000dfbcd380d1b687d2" +ORIGINAL_BLOB = "a4897e5ecb5c92404257e85c8558e7264f0ef0bb" +ORIGINAL_SHA256 = "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9" +WORDS = {23: ROOT / "certificates/joint-dual-word-23.json.gz", + 25: ROOT / "certificates/joint-dual-word-25.json.gz"} + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def load_module(path: Path, name: str): + spec = importlib.util.spec_from_file_location(name, path) + require(spec is not None and spec.loader is not None, f"missing module {path}") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +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 load_json_unique(path: Path): + return json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=unique_object) + + +def check_baseline_bytes() -> None: + original = subprocess.check_output( + ["git", "show", f"{BASE_COMMIT}:scripts/experiments/binary_frame_replay.py"], + cwd=ROOT, + ) + require(sha256(original).hexdigest() == ORIGINAL_SHA256, "base binary_frame_replay.py sha256 drift") + require( + subprocess.check_output( + ["git", "rev-parse", f"{BASE_COMMIT}:scripts/experiments/binary_frame_replay.py"], + cwd=ROOT, + ) + .decode() + .strip() + == ORIGINAL_BLOB, + "base binary_frame_replay.py blob drift", + ) + require( + (PACKAGE / "baseline.py").read_bytes() == original, + "baseline.py must be an exact copy of the base-commit source", + ) + require( + digest_file(EXPERIMENTS / "binary_frame_replay.py") != ORIGINAL_SHA256, + "production source unexpectedly unchanged", + ) + + +def literal_expected(triples, common, triple): + return sum( + 1 << i + for i, t in enumerate(triples) + if common in t and not (set(t) & (set(triple) - {common})) + ) + + +def check_mask_identity(production) -> int: + checked = 0 + for h, path in WORDS.items(): + import gzip + word = json.loads(gzip.decompress(path.read_bytes())) + triples, center, pair, destinations, lookup = production._containment_tables(h) + require(len(triples) == word["v"], f"triple count mismatch at h={h}") + for s, g, common, triple in word["outputs"]: + mask = production._expected_mask(center, pair, common, triple) + require(mask == literal_expected(triples, common, triple), + f"mask nonequivalence at h={h} output role {s}") + if len(triple) == 1: + require(destinations[common] == [i for i, t in enumerate(triples) if common in t], + f"destination list drift at h={h} common={common}") + else: + require(lookup[tuple(triple)] == triples.index(tuple(triple)), + f"lookup drift at h={h}") + checked += 1 + return checked + + +def check_replay_equivalence(baseline, production) -> list[dict]: + stored = load_json_unique(ROOT / "certificates/joint-dual-compiler.json") + rows = [] + for h, path in WORDS.items(): + old = baseline.replay(path) + new = production.replay(path) + require(old == new, f"replay receipt differs at h={h}") + require(json.loads(json.dumps(new)) == stored["axes"][str(h)]["replay"], + f"replay differs from compiler certificate at h={h}") + rows.append(new) + return rows + + +def check_rejections(baseline, production) -> None: + import gzip + with tempfile.TemporaryDirectory(prefix="joint-dual-replay-prod-") as tmp: + dest = Path(tmp) / "bad-word.json.gz" + raw = json.loads(gzip.decompress(WORDS[23].read_bytes())) + raw["outputs"] = raw["outputs"][:-1] # drop a paid output + dest.write_bytes(gzip.compress((json.dumps(raw, separators=(",", ":")) + "\n").encode())) + for eng, name in ((baseline, "baseline"), (production, "optimized")): + try: + eng.replay(dest) + raise AssertionError(f"{name} accepted truncated outputs") + except (AssertionError, ValueError): + pass + + +def check_temp_corruption(production) -> None: + with tempfile.TemporaryDirectory(prefix="joint-dual-replay-mutant-") as tmp: + dest = Path(tmp) / "binary_frame_replay.py" + shutil.copy2(EXPERIMENTS / "binary_frame_replay.py", dest) + text = dest.read_text(encoding="utf-8") + # Drop one pair-mask update so expected masks undercount; symbol assert fails. + mutated = text.replace("pair[c][x]|=bit", "pass", 1) + require(mutated != text, "mutation did not edit production source text") + dest.write_text(mutated, encoding="utf-8") + mutant = load_module(dest, "binary_frame_replay_mutant") + try: + mutant.replay(WORDS[23]) + raise AssertionError("pair-mask omission mutant accepted h=23 replay") + except AssertionError as exc: + require("assert" in str(exc).lower() or str(exc) == "", str(exc)) + # Live production path still matches the recorded receipt. + production.replay(WORDS[23]) + + +def check_pin_consistency() -> None: + freeze = load_json_unique(ROOT / "research/joint-dual/SOURCE.json") + require( + freeze["files"]["scripts/experiments/binary_frame_replay.py"] + == digest_file(EXPERIMENTS / "binary_frame_replay.py"), + "SOURCE.json pin does not match working binary_frame_replay.py", + ) + kappa = load_json_unique(ROOT / "certificates/joint-dual-kappa.json") + require( + kappa["local_source_sha256"]["scripts/experiments/binary_frame_replay.py"] + == digest_file(EXPERIMENTS / "binary_frame_replay.py"), + "joint-dual-kappa local pin drift", + ) + require( + kappa["source_manifest_sha256"] == digest_file(ROOT / "research/joint-dual/SOURCE.json"), + "joint-dual-kappa source_manifest_sha256 drift", + ) + skip = load_json_unique(ROOT / "certificates/skip-frame-kappa.json") + require( + skip["local_source_sha256"]["scripts/experiments/binary_frame_replay.py"] + == digest_file(EXPERIMENTS / "binary_frame_replay.py"), + "skip-frame-kappa local pin drift", + ) + # Duplicate-key check already applied via load_json_unique above. + + +def check_optimize_refusal() -> None: + env = dict(**{k: v for k, v in __import__("os").environ.items() if k != "PYTHONOPTIMIZE"}) + env["PYTHONOPTIMIZE"] = "1" + env["PYTHONDONTWRITEBYTECODE"] = "1" + for rel in ( + "scripts/experiments/binary_frame_replay.py", + "research/joint-dual-replay-production/baseline.py", + ): + proc = subprocess.run( + [sys.executable, "-I", "-O", "-B", "-c", + f"import importlib.util; p={str(ROOT / rel)!r}; " + "s=importlib.util.spec_from_file_location('m', p); " + "m=importlib.util.module_from_spec(s); s.loader.exec_module(m)"], + cwd=ROOT, + env=env, + capture_output=True, + text=True, + ) + require(proc.returncode != 0, f"-O unexpectedly loaded {rel}") + require("Assertions must remain enabled" in proc.stderr, + f"-O refusal message missing for {rel}: {proc.stderr}") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.parse_args(argv) + + if sys.flags.optimize: + check_optimize_refusal() + print("PASS_JOINT_DUAL_REPLAY_PRODUCTION_O_POLICY refused_by_assert_gate=2 no_new_kappa") + return 0 + + check_baseline_bytes() + baseline = load_module(PACKAGE / "baseline.py", "binary_frame_replay_baseline") + production = load_module(EXPERIMENTS / "binary_frame_replay.py", "binary_frame_replay_production") + outputs = check_mask_identity(production) + receipts = check_replay_equivalence(baseline, production) + check_rejections(baseline, production) + check_temp_corruption(production) + check_pin_consistency() + check_optimize_refusal() + print( + "PASS_JOINT_DUAL_REPLAY_PRODUCTION " + f"words={[23, 25]} " + f"outputs={outputs} " + f"receipts={len(receipts)} " + "no_new_kappa" + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/research/joint-dual/SOURCE.json b/research/joint-dual/SOURCE.json index f0931acdac..b7d5a2c307 100644 --- a/research/joint-dual/SOURCE.json +++ b/research/joint-dual/SOURCE.json @@ -10,7 +10,7 @@ "certificates/joint-dual-transitions-25.json": "f858c3f71d563c14013c9ae4cc10fce2b9f227da38e468a392c6890613444c8d", "certificates/joint-dual-word-23.json.gz": "634da1cb39634be15c6d4070132c6bdf4f5a927331cac3b4f13dc995dd799b6b", "certificates/joint-dual-word-25.json.gz": "dd2223d7ee0b91c4445f5acd0b7cad945f56f4e16456b6818f53ddef64a0cb16", - "certificates/skip-frame-kappa.json": "9f57831c261dfb6b3bfbe94be75bfa5a3faa05768b7d500281e7398623ad115a", + "certificates/skip-frame-kappa.json": "5d377638225148cdef1fbbdc67d97e500d7242b1702e40f9b89af7f6a315efba", "references/frame-compiler/pr48/SOURCE.json": "6ccc673f88d3650d75ed600b082fe2fc2866dd88a172d182a38c1d0cd27d24b3", "references/frame-compiler/pr48/research/copied-fixed/balanced_assembly.py": "4d27575dcad2b45a281a34054d311409ecd954f373c0c3a62c29bcc843f06a86", "references/frame-compiler/pr48/research/copied-fixed/certificate.json": "33b3373c04e18a7f4cee33ba68da6ed71934acc3e0179cca6197696be4befa22", @@ -44,7 +44,7 @@ "scripts/experiments/binary_frame_math.py": "1f96e8aafc8ae89f8eeec5d03800131d93d58e01bf8a7fca8feb03702d1740d0", "scripts/experiments/binary_frame_profile_prepare.py": "16dea3d3ef3dc90db928ed3e35d002f6926d2e7388da4766fc2fd725550f739e", "scripts/experiments/binary_frame_profiles.cpp": "8e2e391d3ee57cbb1372c68e4aa031e1b861321536eabd642ada54e12e678669", - "scripts/experiments/binary_frame_replay.py": "1a572dbb33faba6695b3293768a7e61392d11cb4c2d5f708eedcd2cb95d0aff9", + "scripts/experiments/binary_frame_replay.py": "a1cbab2900440baa9b32f482f1531518fd95d55de2fe30e40e7a5249420b33a0", "scripts/experiments/joint_dual_compiler.py": "853d3fc4a740c5b74c23b3a6af5d4dc9b41fb38e9b55678539a7e0ee71947c13", "scripts/experiments/joint_dual_compose.py": "3554e957570e2d3f65c5bd7c945338c9e2fc5f3e79efa2b436a4dfc61899c394", "scripts/experiments/joint_dual_reclaim_compiler.py": "edab206b83cfe3f7a08f1b605b182e354c87cc24e2f3ad81a95f7a083ff342fb", diff --git a/scripts/experiments/binary_frame_replay.py b/scripts/experiments/binary_frame_replay.py index a4897e5ecb..6f250e3d98 100644 --- a/scripts/experiments/binary_frame_replay.py +++ b/scripts/experiments/binary_frame_replay.py @@ -1,4 +1,10 @@ -"""Independent replay of serialized XOR word and original-envelope frames.""" +"""Independent replay of serialized XOR word and original-envelope frames. + +Production form of research/joint-dual-replay-fast/: per-output triple scans +use containment masks over the same combinations(range(h), 3) enumeration. +Every assertion, check_word control, full-basis orientation and receipt field +is retained. See research/joint-dual-replay-production/ for equivalence evidence. +""" import json,sys,gzip from pathlib import Path from itertools import combinations @@ -56,12 +62,53 @@ def check_word(d): assert type(source) is int and 2*v<=source<2*v+R assert Counter(map(tuple,d['scatter']))==expected, 'Scatter differs from paid output incidences' + +def _containment_tables(h): + """Center/pair masks and destination lists over combinations(range(h), 3). + + Bit i of center[c] is set iff c is in triples[i]. Bit i of pair[c][x] is set + iff {c, x} is contained in triples[i]. Then + + expected(common, triple) = center[common] + & ~pair[common][x] for each x in set(triple) - {common} + + equals the historical full-triple scan + sum(1<