From 3c16e5de5ba1c21c45dc2ad1aff26e2d76cf40cb Mon Sep 17 00:00:00 2001 From: ahrazzle <38987266+ahrazzle@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:26:29 -0600 Subject: [PATCH] Add independent replay of the P104 words behind PRs 7000 and 6030 Forward replay, invoice rebuild, unique-root crossing check, a known-answer test on 6030 and seven mutation controls. Audit tooling only. Co-Authored-By: Claude Opus 5.5 --- .../independent-global-rank-replay/README.md | 126 +++ .../RESULT.json | 263 ++++++ .../SOURCES.json | 60 ++ .../independent-global-rank-replay/TASK.json | 104 +++ .../VERIFY.json | 38 + .../independent-global-rank-replay/replay.py | 756 ++++++++++++++++++ tests/test_independent_global_rank_replay.py | 119 +++ 7 files changed, 1466 insertions(+) create mode 100644 research/independent-global-rank-replay/README.md create mode 100644 research/independent-global-rank-replay/RESULT.json create mode 100644 research/independent-global-rank-replay/SOURCES.json create mode 100644 research/independent-global-rank-replay/TASK.json create mode 100644 research/independent-global-rank-replay/VERIFY.json create mode 100644 research/independent-global-rank-replay/replay.py create mode 100644 tests/test_independent_global_rank_replay.py diff --git a/research/independent-global-rank-replay/README.md b/research/independent-global-rank-replay/README.md new file mode 100644 index 0000000000..048479fddf --- /dev/null +++ b/research/independent-global-rank-replay/README.md @@ -0,0 +1,126 @@ +# Independent replay of two P104 native words + +This package replays two existing native words, rebuilds their invoices, and checks them with one script. +The words are the one in #7000 (`research/shared-wire-split-composition`) and the one in #6030 +(`research/p104-helper470-twin-rung-removal`). It adds no new construction, no new kappa and no supplier. +Python standard library only, 3.11 to 3.14. Base commit `e54523517d0f03e58352e647c403a5af3e525a57`. + +```sh +python3 -I -B research/independent-global-rank-replay/replay.py +python3 -I -B tests/test_independent_global_rank_replay.py +``` + +Each run ends with `PASS` and takes about 40 seconds. The script compares its output with the committed +`RESULT.json`. Rerun with `--write` to regenerate it. + +## What the script checks + +1. Every input file matches its sha256 in `SOURCES.json`. +2. A forward replay of each word over exact fractions, with every public input a free symbol. It checks + frame continuity, that every scalar is nonzero, that every public MOVE goes strictly up in frame, that + all owned rows are cleared, and that every helper has a positive credit with nested frames. +3. The main rows equal the block the word declares: a unit shear on ordinary ports, the declared mix on the + other live ports, and the declared hosted pairs. +4. The double-credit test. Every helper row is the identity, no input appears in two helper rows, and no + helper row reads a main input. Nothing is inferred from a rank count. +5. The paid and target profiles (T and P) rebuilt from the replay and the caller data. +6. One crossing solver for both words. It proves the root is unique on [0, 0.01] by a derivative bound, and + it refuses empty profiles and rank 0. +7. For #7000: the root is at least the tested value in `FULL-RESULT.json` (checked at the root and by the sign + of the moment at the tested value). +8. For #6030: a known-answer test. T and P are rebuilt from the replay, including the query term + (15 query shears times 104 ports on P[1]), and must equal the record exactly. The solver sees only the + rebuilt profiles. The root must lie in the recorded interval. +9. Seven mutation controls. + +## Results + +| Item | Value | +|---|---| +| #7000 credit mass per call / paid mass per call | 7650 / 10962 | +| #7000 rank surplus (18 calls) | 1520 | +| #7000 crossing | 0.00363005817468836708593357676489... | +| #7000 tested value / margin | 0.00363005817007, root is 4.6e-12 above | +| #6030 productive / paid / surplus | 13444 / 66834 / 760 | +| #6030 root | 0.00312581155791189724625134438... | +| #6030 recorded interval | (0.00312581155791, 0.00312581155792) | +| Derivative bound on [0, 0.01] | -387072 (#7000), -214612 (#6030), both below 0 | +| Owned erasures of a nonzero field | 53 (#7000), 30 (#6030). Allowed, reported as information. | + +Both words pass every check. Both moments change sign many times when ordered by rank (15 and 19 changes), +so Descartes' rule of signs would only bound the number of roots by 15 and 19. Uniqueness comes from the +derivative bound instead: the moment falls strictly on the whole interval, so it has at most one root there. + +## Mutation controls + +Each control edits the word, or the rebuild, and must be rejected by its target check. The unmutated input +passes that same check in the same run. A control passes only if the target check fails and nothing outside +its listed side effects fails. + +| Control | Target check | Also fails | +|---|---|---| +| Insert `ADD 1478 += 759` at event 40637 (frame 660) | double credit | none | +| Flip the scalar of one main-row ADD | main rows | none | +| Insert an ADD with scalar 0 | nonzero scalars | none | +| A public MOVE goes down and back up | public moves up | record profiles | +| Delete a public MOVE | frame chain | record profiles | +| Rebuild #6030 without the query term | P equals record | paid calls, surplus, interval | +| Remove one rank-8 credit per call from #7000 | root at least tested | none | + +The first control is the leak that a symbol-renaming alias test missed: with it the per-field credit and the +paid mass do not change, but a helper row stops being the identity and one input is shared. + +## What is independent and what is not + +Independent of the audited packages: the forward replay, the check list above, the profile rebuild from the +replay, the #6030 known-answer test and the mutation controls. No code from the audited packages is imported. +Their JSON files are read as data. + +Not independent: + +- The crossing uses the repository's own invoice formula (the sum of (T - P)(r) r^(1-k)). Matching the + #7000 record is a reproduction of that formula, not a second derivation of it. +- The caller tables are read from the records: `caller.json` for #7000, and for #6030 the main targets, + idle jobs, port count and the 15 query shears in `RESULT.json`. +- The main-row block is read from the word's own declared parameters (alpha, beta, alpha2, beta2, Dports). + It catches edits to the word, not a wrong declaration. +- The #7000 record is also used for comparison: its T, P, surplus, bracket and tested value. These are read + after the solve and never enter it. + +## What is not covered + +- No claim about kappa, the supplier, the caller identity or the outer gate. Those checks are in the audited + packages. +- Split tails are not identified from the word. The two proof files name the pivot helper and the cut + frame. The tail rank is derived from the frames. For helper 450 two helpers (496 and 1479) have the same + frames and rank, so the unsplit sensitivity in `RESULT.json` uses ranks only. +- The derivative bound uses 70-digit decimal arithmetic with a margin above 2e5, not interval arithmetic. +- Nothing here checks the public/owned MOVE costs against the physical model, only the replayed counts + against the records. + +## Sensitivities (information) + +`RESULT.json` lists the #7000 crossing if one field of rank r lost its credit in every call, for each r +from 1 to 9 (rank 2: 0.0035437, rank 8: 0.0032862), and if the two named splits were valued as unsplit +rank-9 helpers (0.0036288). They are derived from the replay output, not from a fixed rank or fixed tuples. +The rank that an aliased tail would lose depends on field order, so all ranks are listed. + +## Files + +- `replay.py`: the script. +- `RESULT.json`: its output, regenerated with `--write`. +- `SOURCES.json`, `VERIFY.json`, `TASK.json`: inputs and their hashes, replay commands, task record. +- `../../tests/test_independent_global_rank_replay.py`: unit tests plus the full replay with the controls. + +## Open next questions + +- Can the main-row block be checked against the symbolic caller identity instead of the word's own + declaration? It would catch a wrong declaration. First step: compare the expected main matrix with + `data/caller.json` in #7000. No blocker. +- Can split tails be identified from the word alone? The 450 split has two tail candidates. First step: + trace which tail reads the cut vectors in `data/split759_450.proof.json`. No blocker. + +## Credit + +Audited work: the #7000 package and the #6030 package, with their predecessors credited there. An independent reviewer found the weaknesses in the first version of these checks, and this version fixes them. This package was +written and run by Claude models (Anthropic) under human oversight. Apache-2.0. diff --git a/research/independent-global-rank-replay/RESULT.json b/research/independent-global-rank-replay/RESULT.json new file mode 100644 index 0000000000..19fba4eff7 --- /dev/null +++ b/research/independent-global-rank-replay/RESULT.json @@ -0,0 +1,263 @@ +{ + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "controls": { + "6030_without_query_term": { + "crossing": "0.009676180745500276582762388438236732630932236421317766647159", + "edit": "leave the query shears (15 x 104 on P[1]) out of the rebuild", + "failed_checks": [ + "P_equals_record", + "paid_calls_equal_expect", + "root_in_recorded_interval", + "surplus_equals_expect" + ], + "rejected": true, + "target_check": "P_equals_record" + }, + "7000_one_rank8_credit_lost_per_call": { + "crossing": "0.003286164009629317532936715259", + "edit": "remove one rank-8 credit in each of the 18 calls from T", + "failed_checks": [ + "root_at_least_tested" + ], + "rejected": true, + "target_check": "root_at_least_tested" + }, + "main_row_sign_flip": { + "edit": "flip the scalar of the ADD at event 3030 (main row 152)", + "failed_checks": [ + "main_rows" + ], + "rejected": true, + "target_check": "main_rows" + }, + "public_move_down_and_up": { + "edit": "after the public MOVE at event 24472 insert MOVE 415 99->3 and back", + "failed_checks": [ + "public_moves_up", + "record_profiles" + ], + "rejected": true, + "target_check": "public_moves_up" + }, + "public_move_removed": { + "edit": "delete the public MOVE at event 24472", + "failed_checks": [ + "frame_chain", + "record_profiles" + ], + "rejected": true, + "target_check": "frame_chain" + }, + "shared_input_between_split_tails": { + "edit": "insert ADD 1478 += 759 at event 40637 (frame 660)", + "failed_checks": [ + "double_credit" + ], + "rejected": true, + "target_check": "double_credit" + }, + "zero_scalar_add": { + "edit": "insert a copy of the ADD at event 0 with scalar 0", + "failed_checks": [ + "nonzero_scalars" + ], + "rejected": true, + "target_check": "nonzero_scalars" + } + }, + "failures": [], + "input_sha256": { + "research/p104-helper470-twin-rung-removal/EXPECT.json": "1300b39981c7e1a6edafe677f8d606b5be51a658c3516bd70467800674991338", + "research/p104-helper470-twin-rung-removal/RESULT.json": "84fcbe2a0b64307b5703c363408042bdd91555b0419509aa50daa43b3b1924ad", + "research/p104-helper470-twin-rung-removal/WORD.json.gz": "7834725d9e34ee846a9ccc615221ea81e0201613938afa185d2eda0c9a6c232f", + "research/shared-wire-split-composition/FULL-RESULT.json": "d9ba709fa5dcefffe3f3abdcf34600ce188224703cfd743e6db8b58ba4816cb7", + "research/shared-wire-split-composition/data/caller.json": "3828d53e80ea97e8b866320f081af20b6b93bfebd58b95b56b85a7afe151af5e", + "research/shared-wire-split-composition/data/final.json.gz": "2b94953dc14a122e3ef089633f05b58ec689ec5156feb7c784ed3e137dc01883", + "research/shared-wire-split-composition/data/split759.proof.json": "35f8b64110957b6f51fd8dd0e53326c466e2ffeecf9254012f0e55a5275bea81", + "research/shared-wire-split-composition/data/split759_450.proof.json": "78b25749911131b50bb4565b17a06d08ccf4b511ec05e598e7f146c96aed4174" + }, + "invoice_7000": { + "calls": 18, + "checks": { + "P_equals_record": true, + "T_equals_record": true, + "claim_at_most_tested": true, + "declared_splits_chain_ok": true, + "moment_positive_at_tested": true, + "root_at_least_tested": true, + "root_in_recorded_bracket": true, + "root_matches_record_digits": true, + "root_unique_on_interval": true, + "surplus_equals_record": true + }, + "claim": "363005817/100000000000", + "credit_hist_per_call": { + "1": 13, + "2": 18, + "3": 44, + "4": 215, + "5": 68, + "6": 42, + "7": 18, + "8": 67, + "9": 595 + }, + "credit_mass_per_call": 7650, + "crossing": "0.003630058174688367085933576764896899672679313338856263091621", + "declared_splits": [ + { + "chain_ok": true, + "cut_frame": 660, + "head_rank": 7, + "pivot": 759, + "source": "data/split759.proof.json", + "tail_candidates": [ + 1478 + ], + "tail_rank": 2, + "unsplit_rank": 9 + }, + { + "chain_ok": true, + "cut_frame": 595, + "head_rank": 1, + "pivot": 450, + "source": "data/split759_450.proof.json", + "tail_candidates": [ + 496, + 1479 + ], + "tail_rank": 8, + "unsplit_rank": 9 + } + ], + "derivative_upper_bound_on_interval": "-387072.479", + "paid_hist_per_call": { + "1": 4075, + "2": 828, + "3": 811, + "4": 241, + "5": 320, + "6": 17, + "7": 12, + "8": 6 + }, + "paid_mass_per_call": 10962, + "rank_surplus": 1520, + "recorded_crossing": "0.0036300581746883670859335767648968996726793133388562630916210931832", + "sensitivities": { + "one_field_of_rank_loses_credit_each_call": { + "1": "0.003586749696251990608144038528", + "2": "0.003543661422881519511374557908", + "3": "0.003500654532686239312573542229", + "4": "0.003457698728768232317378358026", + "5": "0.003414779304180724795925519611", + "6": "0.003371887413163687878871135603", + "7": "0.003329017090158370710761658702", + "8": "0.003286164009629317532936715259", + "9": "0.003243324873369088487634938483" + }, + "splits_valued_as_unsplit": { + "crossing": "0.003628822497358640906859281697", + "crossing_minus_base": "-0.000001235677", + "rank_mass_change": 0 + } + }, + "sign_changes_by_rank": 15, + "tested": "363005817007/100000000000000" + }, + "known_answer_6030": { + "calls": 18, + "checks": { + "P_equals_record": true, + "T_equals_record": true, + "paid_calls_equal_expect": true, + "ports_equal_record": true, + "productive_equals_expect": true, + "root_in_recorded_interval": true, + "root_unique_on_interval": true, + "surplus_equals_expect": true + }, + "credit_mass_per_call": 4232, + "crossing": "0.003125811557911897246251344381357396646615240473377728538430", + "derivative_upper_bound_on_interval": "-214612.957", + "interval": [ + "312581155791/100000000000000", + "19536322237/6250000000000" + ], + "paid": 66834, + "paid_mass_per_call": 5952, + "ports": 104, + "productive": 13444, + "query_term": 1560, + "rank_surplus": 760, + "sign_changes_by_rank": 19 + }, + "status": "PASS", + "word_6030": { + "checks": { + "double_credit": true, + "frame_chain": true, + "helper_credit_positive_nested": true, + "main_rows": true, + "nonzero_scalars": true, + "owned_rows_cleared": true, + "public_moves_up": true, + "record_profiles": true + }, + "double_credit": { + "first_non_identity": [], + "first_shared": [], + "helper_rows_reading_main": 0, + "non_identity_helper_rows": 0, + "shared_inputs": 0 + }, + "events": 22803, + "faults": {}, + "helpers": 562, + "information": { + "owned_erasures_nonzero": 30, + "owned_moves_down": 20 + }, + "main": { + "first_wrong": [], + "main_rows": 208, + "main_rows_wrong": 0 + }, + "mains": 208, + "public": 770 + }, + "word_7000": { + "checks": { + "double_credit": true, + "frame_chain": true, + "helper_credit_positive_nested": true, + "main_rows": true, + "nonzero_scalars": true, + "owned_rows_cleared": true, + "public_moves_up": true, + "record_profiles": true + }, + "double_credit": { + "first_non_identity": [], + "first_shared": [], + "helper_rows_reading_main": 0, + "non_identity_helper_rows": 0, + "shared_inputs": 0 + }, + "events": 71261, + "faults": {}, + "helpers": 1080, + "information": { + "owned_erasures_nonzero": 53, + "owned_moves_down": 24 + }, + "main": { + "first_wrong": [], + "main_rows": 400, + "main_rows_wrong": 0 + }, + "mains": 400, + "public": 1480 + } +} diff --git a/research/independent-global-rank-replay/SOURCES.json b/research/independent-global-rank-replay/SOURCES.json new file mode 100644 index 0000000000..de4e02d2f8 --- /dev/null +++ b/research/independent-global-rank-replay/SOURCES.json @@ -0,0 +1,60 @@ +{ + "schema_version": 1, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "files": [ + { + "path": "research/shared-wire-split-composition/data/final.json.gz", + "usage": "input", + "git_blob": "74563a7867d9d762b6588f65362369668841bb3e", + "sha256": "2b94953dc14a122e3ef089633f05b58ec689ec5156feb7c784ed3e137dc01883" + }, + { + "path": "research/shared-wire-split-composition/data/caller.json", + "usage": "input", + "git_blob": "cf55f4271009a06ad192a070d330a4466b6d44fa", + "sha256": "3828d53e80ea97e8b866320f081af20b6b93bfebd58b95b56b85a7afe151af5e" + }, + { + "path": "research/shared-wire-split-composition/FULL-RESULT.json", + "usage": "input", + "git_blob": "ff3256f0d3ffbfc56b062f9ec0fdbbc7b30e76d5", + "sha256": "d9ba709fa5dcefffe3f3abdcf34600ce188224703cfd743e6db8b58ba4816cb7" + }, + { + "path": "research/shared-wire-split-composition/data/split759.proof.json", + "usage": "input", + "git_blob": "16fa0581235ecbc6657aeb5be4f4509319d2e6c5", + "sha256": "35f8b64110957b6f51fd8dd0e53326c466e2ffeecf9254012f0e55a5275bea81" + }, + { + "path": "research/shared-wire-split-composition/data/split759_450.proof.json", + "usage": "input", + "git_blob": "2fb9b2e8b1b8915ff7e81d03057eeeba351139f2", + "sha256": "78b25749911131b50bb4565b17a06d08ccf4b511ec05e598e7f146c96aed4174" + }, + { + "path": "research/p104-helper470-twin-rung-removal/WORD.json.gz", + "usage": "input", + "git_blob": "22f9ab02d1b73ea0aa4b07ce445a14cc2602c760", + "sha256": "7834725d9e34ee846a9ccc615221ea81e0201613938afa185d2eda0c9a6c232f" + }, + { + "path": "research/p104-helper470-twin-rung-removal/RESULT.json", + "usage": "input", + "git_blob": "e4a03118d585fb80c9ac932281b528ac394ccc5f", + "sha256": "84fcbe2a0b64307b5703c363408042bdd91555b0419509aa50daa43b3b1924ad" + }, + { + "path": "research/p104-helper470-twin-rung-removal/EXPECT.json", + "usage": "input", + "git_blob": "0e7201cef95465bb92ede7da07c46438e2357226", + "sha256": "1300b39981c7e1a6edafe677f8d606b5be51a658c3516bd70467800674991338" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec", + "sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81" + } + ] +} diff --git a/research/independent-global-rank-replay/TASK.json b/research/independent-global-rank-replay/TASK.json new file mode 100644 index 0000000000..4b1ba2e920 --- /dev/null +++ b/research/independent-global-rank-replay/TASK.json @@ -0,0 +1,104 @@ +{ + "schema_version": 1, + "task_id": "independent-moment", + "priority_revision": 8, + "base_commit": "e54523517d0f03e58352e647c403a5af3e525a57", + "original_card": { + "id": "independent-moment", + "question": "Does a changed construction's serialized target/paid invoice independently support its claimed improvement?", + "starting_points": [ + "docs/research/current-priorities.md", + "docs/research/research-family-status.md", + "certificates/selected-result.json", + "docs/research/pr-intake-evidence/pr5599-completion/PROOF-REVIEW.md", + "docs/research/recursive-invoice-preflight.md" + ], + "methods": [ + "Construct and independently check one source-bound instance of the stated first experiment.", + "Seek an exact counterexample or target-specific complete-budget obstruction to that same proposed change." + ], + "acceptance_gate": "Demonstrate independence, source binding, positive cases and semantic failure controls. Name the existing checks and the missing coverage. More digits or another equivalent evaluation of an unchanged root is not by itself new verification coverage.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_selected_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Read revision 8, the selected review and live exact-source handoffs. Then follow this card's bounded first_experiment.", + "scope": "Source-bound orientation only, not a supplier replay or theorem proof." + }, + "first_experiment": "Independently recount one changed emitted word and its complete caller, importing no constructor or pricing engine. Include target losses, owned lifetimes, native orientation and extra charges. Check existing #5633/#5708/#5717/#5719 coverage to avoid duplicate audits and preserve its exact head. Stop after the chosen missing implication; a correct root alone is not supplier admission.", + "bottleneck_link": "Judge the actual current supplier contract and complete signed target/paid bill; frozen-profile ceilings require an applicability proof.", + "allowed_variations": [ + "One exact source head, helper/cut region or quantified proposition and changed hypothesis; check live handoffs first. Different seeds are not different scopes." + ], + "scope_change_triggers": [ + "Changing source head, helper/cut domain, operation alphabet or caller boundary without recording the new scope.", + "Treating an archived proposal or local numerical pass as complete supplier admission." + ], + "overlap_terms": [ + "invoice", + "signed moment", + "productive target", + "independent recount" + ], + "existing_checks": [ + { + "scope": "PR #4088: mixed local/owner/phase ledger and moment reported.", + "evidence": "research/staged544-mixed-fourth-entry/README.md", + "remaining": "Independent binding of changed construction to paid histogram, distinct from full admission.", + "status": "author-checked", + "source_commit": "72d7efc181edb21806a878f174847c96dd410ab0", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "PR #4091: sharp pooled ceiling proposed for fixed THREE/FOUR words and inventory.", + "evidence": "research/staged-fourth-mixture-ceiling/README.md", + "remaining": "Validate dual, exhaustive pattern scope and exact source equivalence before applying to newer fills.", + "status": "author-checked", + "source_commit": "a29bdd8f56cd18b805917775cdec09ff935eddc4", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "PR #4067: paid (2,2) to (1,3) exchange and fixed OQ4 host reported.", + "evidence": "research/r437-two-window-carrier/README.md", + "remaining": "Independent full cost delta; remaining caller/precision/stock obligations are separate.", + "status": "author-checked", + "source_commit": "a95f11c52e3ca1e4c5171b3c20d8f4b0815aee2e", + "selected_record_sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "scope": "Selected PR5599: complete helper-subspace construction and independent caller replay; five framework assumptions remain.", + "evidence": "docs/research/pr-intake-evidence/pr5599-completion/PROOF-REVIEW.md", + "remaining": "The next experiment must identify uncovered source-specific work. Successor archive intake does not transfer this admission.", + "status": "reviewed", + "source_commit": "09dd0965711ca7eeb169057a420abab1c9052137", + "selected_record_sha256": "2a0220e866417774606e0f991e41b9c1d26b1f43b97857238cf478dbc7d330b5" + } + ] + }, + "scope_status": "refined", + "scope_change_reason": "The card asks for an independent check of a serialized invoice. This package checks two named words (#7000 and #6030) at one base commit, and says which parts are independent and which reuse the repository invoice formula.", + "actual_scope": "Independent forward replay of the native words of #7000 (research/shared-wire-split-composition) and #6030 (research/p104-helper470-twin-rung-removal): rebuild of the paid and target profiles, a unique-root crossing check, a known-answer test on #6030, and mutation controls. No new kappa, construction or supplier.", + "bottleneck_link": "Checking tool only. It tests whether the recorded profiles and crossings follow from the words and callers, and rejects shared helper inputs. It does not change any bound.", + "stop_rule": "Stop when the replay, the rebuild, the known-answer test and the controls all run from pinned inputs with one command, and report what the checks do not cover.", + "outcome": "completed", + "evidence": "replay.py prints PASS. The #7000 profiles and the #6030 profiles rebuilt from replay equal their records exactly. Both crossings are unique on [0, 0.01]; the #7000 root is above the tested value and the #6030 root is inside its recorded interval. Seven controls are each rejected by their target check.", + "author": "Agentic team with human oversight", + "ai_assistance": "Written and run by Claude models (Anthropic) under human oversight. No code from the audited packages is imported; their JSON files are read as data.", + "open_questions": [ + { + "question": "Can the main-row convention (unit shear, declared mix, hosted pairs) be checked against the caller data instead of read from the word?", + "why_it_matters": "The main-row check currently uses the word own declared parameters, so it catches changes to the word but not a wrong declaration.", + "first_step": "Compare the expected main matrix with the symbolic caller identity in research/shared-wire-split-composition/data/caller.json.", + "blocked_by": "none" + }, + { + "question": "Can split tails be identified from the word alone?", + "why_it_matters": "The 450 split has two tail candidates (496 and 1479) with the same frames and rank, so the unsplit sensitivity uses ranks, not field identities.", + "first_step": "Trace which tail reads the cut vectors recorded in data/split759_450.proof.json.", + "blocked_by": "none" + } + ] +} diff --git a/research/independent-global-rank-replay/VERIFY.json b/research/independent-global-rank-replay/VERIFY.json new file mode 100644 index 0000000000..717f5ffe82 --- /dev/null +++ b/research/independent-global-rank-replay/VERIFY.json @@ -0,0 +1,38 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/shared-wire-split-composition/data/final.json.gz", + "research/shared-wire-split-composition/data/caller.json", + "research/shared-wire-split-composition/FULL-RESULT.json", + "research/shared-wire-split-composition/data/split759.proof.json", + "research/shared-wire-split-composition/data/split759_450.proof.json", + "research/p104-helper470-twin-rung-removal/WORD.json.gz", + "research/p104-helper470-twin-rung-removal/RESULT.json", + "research/p104-helper470-twin-rung-removal/EXPECT.json" + ], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-B", + "{package}/replay.py" + ], + "timeout_seconds": 300, + "checks": "Forward replay of the #7000 and #6030 words with frame, scalar, MOVE direction, main-row, identity-helper and no-shared-input checks; rebuild of both invoices; unique crossing on [0, 0.01] by a derivative bound; #7000 root at least the tested value; #6030 known-answer test against the recorded interval; seven mutation controls; output equal to RESULT.json." + }, + { + "argv": [ + "{python}", + "-I", + "-B", + "{package}/../../tests/test_independent_global_rank_replay.py" + ], + "timeout_seconds": 300, + "checks": "Unit tests for empty and rank-zero profiles, the derivative bound, three-part split chains and input pins, plus the full replay once with every control required to fail at its target check." + } + ] +} diff --git a/research/independent-global-rank-replay/replay.py b/research/independent-global-rank-replay/replay.py new file mode 100644 index 0000000000..b171d32c7a --- /dev/null +++ b/research/independent-global-rank-replay/replay.py @@ -0,0 +1,756 @@ +#!/usr/bin/env python3 +"""Independent forward replay of two P104 native words, with invoice rebuild and mutation controls. + +Standard library only (Python 3.11 to 3.14). Run without arguments: + + python3 -I -B research/independent-global-rank-replay/replay.py + +It prints one JSON document and ends with PASS, or lists the failed checks and +exits 1. `--write` regenerates RESULT.json; the default run compares its output +with the committed RESULT.json, so script and output stay in step. + +What it does, in order: + +1. Verifies the sha256 of every input against SOURCES.json. +2. Forward-replays each native word over exact rationals, with every public + input a free symbol, and checks frames, scalars, MOVE direction, the main + rows, and that every helper row is the identity with no input shared + between helper rows. +3. Rebuilds the paid and target profiles (T and P) from the replay and the + caller data, finds the crossing with one solver, and proves the root is + unique on [0, 0.01] by a derivative bound. +4. #7000: compares with its record and requires the root to be at least the + tested value in FULL-RESULT.json. +5. #6030: known-answer test. Rebuilds T and P from the replay, requires them to + equal the record exactly, solves, and requires the root to lie in the + recorded interval. The solver never reads the recorded T, P or answer. +6. Mutation controls: real edits of the word (and of the rebuild) that each + target check must reject, while the unmutated input passes it. + +Everything the crossing uses (the invoice formula, the caller tables) comes +from the repository's own records, so matching the record for #7000 is a +reproduction. The replay, the profile rebuild, the #6030 known-answer test and +the controls are the independent parts. See README.md. +""" +import gzip +import hashlib +import json +import sys +from collections import Counter, defaultdict +from decimal import Decimal, localcontext +from fractions import Fraction as F +from pathlib import Path + +PKG = Path(__file__).resolve().parent +ROOT = PKG.parents[1] +P7 = 'research/shared-wire-split-composition/' +P6 = 'research/p104-helper470-twin-rung-removal/' +K_MAX = Decimal('0.01') # search interval for the crossing: [0, K_MAX] +PRECISION = 70 # decimal digits for the solver +BISECTIONS = 230 # width 0.01 / 2**230 is below 1e-71 +EVENT_LENGTH = {'ADD': 5, 'COPY': 5, 'SCALE': 4, 'MOVE': 5, 'INIT': 3, 'CLEAR': 3} +# Faults that are about scalars or direction, not about the frame chain. +NON_FRAME_FAULTS = {'public_move_not_up', 'zero_scalar'} + + +class CheckFailure(Exception): + pass + + +# ---------------------------------------------------------------- inputs + +def load(path): + data = Path(path).read_bytes() + return json.loads(gzip.decompress(data) if str(path).endswith('.gz') else data) + + +def input_pins(root=ROOT): + sources = json.loads((PKG / 'SOURCES.json').read_text()) + return {row['path']: row['sha256'] for row in sources['files'] if row['usage'] == 'input'} + + +def verify_pins(root=ROOT, pins=None): + """Return the list of inputs whose bytes differ from their pin.""" + bad = [] + for path, want in (pins or input_pins(root)).items(): + got = hashlib.sha256((Path(root) / path).read_bytes()).hexdigest() + if got != want: + bad.append(path) + return bad + + +# ------------------------------------------------------------- GF(2) frames + +def reduce_basis(vectors): + basis = {} + for v in vectors: + while v: + h = v.bit_length() - 1 + if h in basis: + v ^= basis[h] + else: + basis[h] = v + break + return basis + + +class Frames: + """Frame table: dimension and containment of GF(2) subspaces, cached.""" + + def __init__(self, bases): + self.bases = [list(b) for b in bases] + self.dims = [len(reduce_basis(b)) for b in self.bases] + self._contains = {} + + def contains(self, big, small): + key = (big, small) + if key not in self._contains: + both = self.bases[big] + self.bases[small] + self._contains[key] = len(reduce_basis(both)) == self.dims[big] + return self._contains[key] + + def strictly_up(self, a, b): + return self.contains(b, a) and self.dims[b] > self.dims[a] + + +def chain_check(frames, parts): + """Check a split chain of two or more consecutive parts. + + parts is a list of (initial_frame, final_frame) for the fields in order. + Requires: each part nested and strictly up, each part ends where the next + begins, and the part ranks add up to the rank of the whole chain. Returns + (ok, ranks). + """ + ranks = [] + ok = len(parts) >= 2 + for k, (a, b) in enumerate(parts): + ok &= frames.strictly_up(a, b) + ranks.append(frames.dims[b] - frames.dims[a]) + if k + 1 < len(parts): + ok &= b == parts[k + 1][0] + ok &= sum(ranks) == frames.dims[parts[-1][1]] - frames.dims[parts[0][0]] + return bool(ok), ranks + + +# ------------------------------------------------------------------ replay + +def main_ids(word): + live = set(word['live']) + mains = live | {x + 112 for x in live} + hosted = word.get('hosted') + if hosted: + mains |= set(hosted['X2']) | set(hosted['Y2']) + return mains + + +def expected_main_rows(word): + """The main block the word declares: a unit shear on ordinary ports, the + declared mix on the hosted D ports, and the declared hosted X2/Y2 pairs. + + Convention (read off the words, and consistent with their declared alpha, + beta, alpha2, beta2). A shear port x with partner y = x + 112 maps x -> x, + y -> x + y. When the word declares hosted ports, the shear ports are the + live ports listed in Dports; every other live port is a mix port and maps + x -> x + alpha y, y -> beta x + (1 - beta) y. A hosted pair maps + X2 -> X2 + alpha2 Y2, Y2 -> Y2 + beta2 X2. Without hosted ports every + live port is a shear port. + """ + live = word['live'] + hosted = word.get('hosted') + shear_ports = set(live) + if hosted: + if hosted['mode'] != 'mix-t0': + raise ValueError('unknown hosted mode ' + str(hosted['mode'])) + shear_ports = {live[p] for p in hosted['Dports']} + rows = {} + + def put(j, row): + if j in rows: + raise ValueError('main row listed twice: ' + str(j)) + rows[j] = {k: v for k, v in row.items() if v} + + for x in live: + y = x + 112 + if x in shear_ports: + put(x, {x: F(1)}) + put(y, {x: F(1), y: F(1)}) + else: + alpha, beta = F(hosted['alpha']), F(hosted['beta']) + put(x, {x: F(1), y: alpha}) + put(y, {x: beta, y: 1 - beta}) + if hosted: + alpha2, beta2 = F(hosted['alpha2']), F(hosted['beta2']) + for xi, yi in zip(hosted['X2'], hosted['Y2']): + put(xi, {xi: F(1), yi: alpha2}) + put(yi, {yi: F(1), xi: beta2}) + return rows + + +class Replay: + pass + + +def addto(dst, src, q): + for k, v in src.items(): + z = dst.get(k, 0) + q * v + if z: + dst[k] = z + else: + dst.pop(k, None) + + +def replay(word, frames): + """Forward replay over exact rationals. Every public input is a symbol.""" + n = word['public'] + total = n + word['owned_fields'] + mains = main_ids(word) + helpers = [r for r in range(n) if r not in mains] + val = [{r: F(1)} if r < n else None for r in range(total)] + cur = [word['initial'][r] if r < n else None for r in range(total)] + paid, faults, info = Counter(), Counter(), Counter() + dims = frames.dims + + def live(*rows): + for r in rows: + if val[r] is None: + faults['dead_row_used'] += 1 + return False + return True + + for e in word['program']: + op = e[0] + if op not in EVENT_LENGTH: + faults['unknown_op'] += 1 + continue + if len(e) != EVENT_LENGTH[op]: + faults['malformed_event'] += 1 + continue + r = e[1] + if op == 'ADD': + s, q, fr = e[2], F(e[3]), e[4] + if q == 0: + faults['zero_scalar'] += 1 + if cur[r] != fr or cur[s] != fr: + faults['add_frame_mismatch'] += 1 + if live(r, s): + addto(val[r], val[s], q) + elif op == 'COPY': + s, q, fr = e[2], F(e[3]), e[4] + if q == 0: + faults['zero_scalar'] += 1 + if cur[r] != fr or cur[s] != fr: + faults['copy_frame_mismatch'] += 1 + if live(r, s): + if val[r]: + faults['copy_onto_nonzero'] += 1 + val[r] = {k: q * v for k, v in val[s].items()} + elif op == 'SCALE': + q, fr = F(e[2]), e[3] + if q == 0: + faults['zero_scalar'] += 1 + if cur[r] != fr: + faults['scale_frame_mismatch'] += 1 + if live(r): + val[r] = {k: q * v for k, v in val[r].items()} + elif op == 'MOVE': + a, b, owned = e[2], e[3], e[4] + if cur[r] != a: + faults['move_from_wrong_frame'] += 1 + if owned != (r >= n): + faults['move_owned_flag'] += 1 + if not (frames.contains(b, a) or frames.contains(a, b)): + faults['move_not_nested'] += 1 + if r < n and not frames.strictly_up(a, b): + faults['public_move_not_up'] += 1 + if r >= n and dims[b] < dims[a]: + info['owned_moves_down'] += 1 + rank = abs(dims[b] - dims[a]) + if rank == 0: + faults['move_rank_zero'] += 1 # never added to the paid profile + else: + paid[rank] += 1 + cur[r] = b + elif op == 'INIT': + if r < n: + faults['init_public'] += 1 + elif val[r] is not None: + faults['init_live'] += 1 + else: + val[r] = {} + cur[r] = e[2] + elif op == 'CLEAR': + if r < n: + faults['clear_public'] += 1 + elif val[r] is None: + faults['clear_dead'] += 1 + else: + if cur[r] != e[2]: + faults['clear_frame_mismatch'] += 1 + if val[r]: + # Allowed: the owned-erasure rule permits clearing a nonzero + # owned field. Reported as information, not a fault. + info['owned_erasures_nonzero'] += 1 + val[r] = None + cur[r] = None + for r in range(n): + if cur[r] != word['final'][r]: + faults['final_frame_mismatch'] += 1 + if val[r] is None: + faults['public_row_dead'] += 1 + rp = Replay() + rp.word, rp.frames, rp.n, rp.mains, rp.helpers = word, frames, n, mains, helpers + rp.val, rp.cur, rp.paid, rp.faults, rp.info = val, cur, paid, faults, info + rp.owned_left_live = sum(1 for r in range(n, total) if val[r] is not None) + # Per-field credit as the invoice uses it: rank of the final frame over the initial one. + rp.credit = Counter() + rp.credit_bad = 0 + for j in helpers: + a, b = word['initial'][j], word['final'][j] + rank = dims[b] - dims[a] + if rank <= 0 or not frames.contains(b, a): + rp.credit_bad += 1 + else: + rp.credit[rank] += 1 + return rp + + +def double_credit_findings(rp): + """The double-credit test: every helper row is the identity, no input is + shared between helper rows, and no helper row reads a main input.""" + val, helpers, mains = rp.val, rp.helpers, rp.mains + not_identity = [j for j in helpers if val[j] != {j: F(1)}] + owners = defaultdict(list) + for j in helpers: + for i in val[j]: + owners[i].append(j) + shared = sorted((i, js) for i, js in owners.items() if len(js) > 1) + reads_main = [j for j in helpers if any(i in mains for i in val[j])] + return dict(non_identity_helper_rows=len(not_identity), shared_inputs=len(shared), + helper_rows_reading_main=len(reads_main), + first_non_identity=not_identity[:5], first_shared=[[i, js] for i, js in shared[:5]]) + + +def main_row_findings(rp): + expected = expected_main_rows(rp.word) + wrong = [] + if set(expected) != rp.mains: + wrong.append('main set differs from declared mains') + wrong += [j for j, row in expected.items() if rp.val[j] != row] + return dict(main_rows=len(expected), main_rows_wrong=len(wrong), first_wrong=wrong[:5]) + + +def evaluate(rp, record_paid, record_credit): + """Named checks for one replayed word. Each is a pass/fail fact.""" + f = rp.faults + dc = double_credit_findings(rp) + mr = main_row_findings(rp) + checks = { + 'frame_chain': not any(k not in NON_FRAME_FAULTS for k in f), + 'nonzero_scalars': f['zero_scalar'] == 0, + 'public_moves_up': f['public_move_not_up'] == 0, + 'owned_rows_cleared': rp.owned_left_live == 0, + 'helper_credit_positive_nested': rp.credit_bad == 0, + 'main_rows': mr['main_rows_wrong'] == 0, + 'double_credit': (dc['non_identity_helper_rows'] == 0 and dc['shared_inputs'] == 0 + and dc['helper_rows_reading_main'] == 0), + 'record_profiles': dict(rp.paid) == record_paid and dict(rp.credit) == record_credit, + } + return checks, dict(faults=dict(f), information=dict(rp.info), double_credit=dc, main=mr) + + +# ---------------------------------------------------------- crossing solver + +class Moment: + """m(k) = sum_r (T[r] - P[r]) r^(1-k), the signed concave moment.""" + + def __init__(self, T, P): + T = {r: c for r, c in T.items() if c} + P = {r: c for r, c in P.items() if c} + if not T and not P: + raise ValueError('empty profile: no target and no paid rank') + bad = sorted(r for r in set(T) | set(P) if not isinstance(r, int) or r < 1) + if bad: + raise ValueError('rank must be a positive integer; found ' + repr(bad)) + self.coef = {r: T.get(r, 0) - P.get(r, 0) for r in sorted(set(T) | set(P))} + self.T, self.P = T, P + with localcontext() as cx: + cx.prec = PRECISION + self.logs = {r: Decimal(r).ln() for r in self.coef} + + def value(self, k): + with localcontext() as cx: + cx.prec = PRECISION + k = Decimal(k) + return sum(Decimal(c) * Decimal(r) * (-k * self.logs[r]).exp() + for r, c in self.coef.items() if c) + + def derivative_upper(self, k_max): + """An upper bound for m'(k) on [0, k_max]. + + m'(k) = - sum c_r r ln(r) r^(-k). A positive c_r term is largest in + absolute value at k = 0 only for negative c, and smallest in size at + k = k_max for positive c, so bound each term on its worst side. + """ + with localcontext() as cx: + cx.prec = PRECISION + k_max = Decimal(k_max) + total = Decimal(0) + for r, c in self.coef.items(): + if c == 0 or r == 1: + continue + w = Decimal(c) * Decimal(r) * self.logs[r] + total -= w * (-k_max * self.logs[r]).exp() if c > 0 else w + return total + + def sign_changes(self): + seq = [c for c in self.coef.values() if c] + return sum(1 for a, b in zip(seq, seq[1:]) if (a > 0) != (b > 0)) + + +def solve(T, P, k_max=K_MAX): + """The unique crossing of m on [0, k_max], with its proof obligations. + + Raises ValueError if m(0) <= 0, m(k_max) >= 0, or the derivative bound does + not show m is strictly decreasing on the whole interval (interior check). + """ + m = Moment(T, P) + u = m.derivative_upper(k_max) + with localcontext() as cx: + cx.prec = PRECISION + lo, hi = Decimal(0), Decimal(k_max) + m0, mk = m.value(lo), m.value(hi) + if not (m0 > 0 and mk < 0): + raise ValueError('no sign change on [0, %s]' % k_max) + if not u < 0: + raise ValueError('derivative bound %s does not show a monotone moment' % u) + for _ in range(BISECTIONS): + mid = (lo + hi) / 2 + if m.value(mid) > 0: + lo = mid + else: + hi = mid + root = (lo + hi) / 2 + return dict(root=root, moment=m, derivative_upper=u, m_at_0=m0, m_at_kmax=mk) + + +def digits(d, places=60): + return format(d, '.%df' % places) + + +# ------------------------------------------------------------- profiles + +def rank_mass(hist): + return sum(r * c for r, c in hist.items()) + + +def surplus(T, P): + return sum(r * (T.get(r, 0) - P.get(r, 0)) for r in set(T) | set(P)) + + +def build_7000(rp, caller): + """T and P for #7000: per-call native credit and paid ranks from the replay, + plus the caller's QUERY, IDLE_ONE and diagonal terms (crossing formula of + the repository's invoice).""" + T, P, calls = Counter(), Counter(), 0 + for e in caller['events']: + if e[0] == 'J': + calls += 1 + T.update(rp.credit) + P.update(rp.paid) + else: + ports = len(caller['types'][e[0]]['ports']) + if e[1] == 'QUERY': + P[1] += ports + elif e[1] == 'IDLE_ONE': + P[8 * len(set(e[3]))] += ports + for d in caller['types'].values(): + for dg in d['diagonal']: + if not all(v in (0, 1) for v in dg[2:]): + raise ValueError('repeated stage power in caller diagonal') + T[dg[0] % 2 + 8 * sum(dg[2:])] += len(d['ports']) + return T, P, calls + + +def clean(hist): + return {int(r): int(c) for r, c in hist.items() if c} + + +def declared_splits(root, word, frames, helpers): + """Splits named by the package's two proof files (pivot helper and cut frame). + + The head is the pivot. Its unsplit exit frame is the modal final frame of the + helpers with the same initial frame. Tail candidates are helpers that start + at the cut frame and end at the exit frame; every candidate is chain-checked. + The tail's rank is derived from the frames, not written down. + """ + out = [] + for name in ('data/split759.proof.json', 'data/split759_450.proof.json'): + proof = load(Path(root) / P7 / name) + pivot, cut = proof['pivot'], proof['frame'] + init, fin = word['initial'][pivot], word['final'][pivot] + exit_frame = Counter(word['final'][j] for j in helpers + if word['initial'][j] == init).most_common(1)[0][0] + tails = [t for t in helpers if word['initial'][t] == cut and word['final'][t] == exit_frame] + chains = [chain_check(frames, [(init, fin), (cut, exit_frame)]) for _ in tails] + head_rank = frames.dims[fin] - frames.dims[init] + whole = frames.dims[exit_frame] - frames.dims[init] + out.append(dict(source=name, pivot=pivot, cut_frame=cut, head_rank=head_rank, + tail_rank=whole - head_rank, unsplit_rank=whole, tail_candidates=tails, + chain_ok=bool(tails) and fin == cut and all(ok for ok, _ in chains))) + return out + + +def sensitivities_7000(T, P, calls, splits): + """Information only: how far the crossing moves under named changes.""" + out = {} + base = solve(T, P)['root'] + by_rank = {} + for r in sorted(r for r in T if r < 10): + T2 = Counter(T) + T2[r] -= calls # one credited field of this rank loses its credit in every call + by_rank[str(r)] = digits(solve(T2, P)['root'], 30) + out['one_field_of_rank_loses_credit_each_call'] = by_rank + T1 = Counter(T) + for s in splits: + T1[s['head_rank']] -= calls + T1[s['tail_rank']] -= calls + T1[s['unsplit_rank']] += calls + out['splits_valued_as_unsplit'] = dict( + rank_mass_change=surplus(T1, P) - surplus(T, P), crossing=digits(solve(T1, P)['root'], 30), + crossing_minus_base=digits(solve(T1, P)['root'] - base, 12)) + return out + + +# --------------------------------------------------------------- the runs + +def run_7000(root, frames, word, rp, record): + caller = load(Path(root) / P7 / 'data/caller.json') + T, P, calls = build_7000(rp, caller) + inv = record['invoice'] + rec_T, rec_P = clean(inv['T']), clean(inv['P']) + sol = solve(T, P) + root_v = sol['root'] + tested = F(record['gate']['tested']) + claim = F(record['gate']['claim']) + lo, hi = (F(x) for x in record['gate']['raw_bracket']) + rec_cross = Decimal(inv['raw_crossing']) + checks = { + 'T_equals_record': clean(T) == rec_T, + 'P_equals_record': clean(P) == rec_P, + 'surplus_equals_record': surplus(T, P) == int(inv['S']), + 'root_unique_on_interval': sol['derivative_upper'] < 0, + 'moment_positive_at_tested': sol['moment'].value(Decimal(tested.numerator) / Decimal(tested.denominator)) > 0, + 'root_at_least_tested': F(root_v) >= tested, + 'claim_at_most_tested': claim <= tested, + 'root_in_recorded_bracket': lo < F(root_v) < hi, + 'root_matches_record_digits': abs(root_v - rec_cross) < Decimal('1e-55'), + } + splits = declared_splits(root, word, frames, rp.helpers) + checks['declared_splits_chain_ok'] = all(s['chain_ok'] for s in splits) + res = dict( + calls=calls, credit_hist_per_call=dict(sorted(rp.credit.items())), credit_mass_per_call=rank_mass(rp.credit), + paid_hist_per_call=dict(sorted(rp.paid.items())), paid_mass_per_call=rank_mass(rp.paid), + rank_surplus=surplus(T, P), crossing=digits(root_v), recorded_crossing=inv['raw_crossing'], + tested=str(tested), claim=str(claim), derivative_upper_bound_on_interval=digits(sol['derivative_upper'], 3), + sign_changes_by_rank=sol['moment'].sign_changes(), + declared_splits=splits, sensitivities=sensitivities_7000(T, P, calls, splits)) + return checks, res, (T, P, calls, sol) + + +def known_6030(root, frames6, word6, rp6, include_query=True): + """Known-answer test. Solves from the rebuilt T and P; reads the record only + afterwards, to compare.""" + result = load(Path(root) / P6 / 'RESULT.json') + expect = load(Path(root) / P6 / 'EXPECT.json') + pc, cc = result['physical_caller'], result['complete_cost'] + calls = pc['full_calls'] + ports = len(word6['live']) + T, P = Counter(), Counter() + for r, c in rp6.credit.items(): + T[r] += calls * c + for r, c in rp6.paid.items(): + P[r] += calls * c + for r, c in pc['main_targets'].items(): + T[int(r)] += c + for r, c in pc['whole_idle_jobs'].items(): + P[int(r)] += c + if include_query: + P[1] += cc['query_shears'] * ports + sol = solve(T, P) # the solver has seen only the rebuilt profiles + root_v = sol['root'] + lo, hi = (F(x) for x in expect['interval']) + checks = { + 'ports_equal_record': ports == pc['ports'], + 'T_equals_record': clean(T) == clean(cc['targets']), + 'P_equals_record': clean(P) == clean(cc['paid']), + 'productive_equals_expect': sum(T.values()) == expect['productive_fields'], + 'paid_calls_equal_expect': sum(P.values()) == expect['paid_calls'], + 'surplus_equals_expect': surplus(T, P) == expect['rank_mass_surplus'], + 'root_unique_on_interval': sol['derivative_upper'] < 0, + 'root_in_recorded_interval': lo < F(root_v) < hi, + } + res = dict(calls=calls, ports=ports, query_term=cc['query_shears'] * ports if include_query else 0, + credit_mass_per_call=rank_mass(rp6.credit), paid_mass_per_call=rank_mass(rp6.paid), + productive=sum(T.values()), paid=sum(P.values()), rank_surplus=surplus(T, P), + crossing=digits(root_v), interval=[str(lo), str(hi)], + derivative_upper_bound_on_interval=digits(sol['derivative_upper'], 3), + sign_changes_by_rank=sol['moment'].sign_changes()) + return checks, res + + +# ---------------------------------------------------------------- controls + +def with_program(word, program): + mutated = dict(word) + mutated['program'] = program + return mutated + + +def first_index(program, pred): + for i, e in enumerate(program): + if pred(e): + return i + raise ValueError('no event to mutate') + + +def mutations(word, mains): + """Real edits of the word. Each returns (mutated word, description).""" + n = word['public'] + prog = word['program'] + out = {} + # The reviewer's leak: helper 1478 reads helper 759 while both sit in frame 660. + p = list(prog) + p.insert(40637, ['ADD', 1478, 759, '1', 660]) + out['shared_input_between_split_tails'] = ( + with_program(word, p), 'insert ADD 1478 += 759 at event 40637 (frame 660)', 'double_credit', set()) + # A different kind: flip the sign of one main-row ADD. + i = first_index(prog, lambda e: e[0] == 'ADD' and e[1] in mains and e[2] in mains) + p = list(prog) + e = list(p[i]) + e[3] = str(-F(e[3])) + p[i] = e + out['main_row_sign_flip'] = ( + with_program(word, p), 'flip the scalar of the ADD at event %d (main row %d)' % (i, e[1]), 'main_rows', set()) + # A scalar fault with no effect on any value: an ADD by zero. + i = first_index(prog, lambda e: e[0] == 'ADD') + p = list(prog) + p.insert(i + 1, p[i][:3] + ['0'] + p[i][4:]) + out['zero_scalar_add'] = ( + with_program(word, p), 'insert a copy of the ADD at event %d with scalar 0' % i, 'nonzero_scalars', set()) + # A public MOVE that goes down and back up: legal frames, wrong direction, extra paid rank. + i = first_index(prog, lambda e: e[0] == 'MOVE' and e[1] < n) + r, a, b = prog[i][1], prog[i][2], prog[i][3] + p = list(prog) + p[i + 1:i + 1] = [['MOVE', r, b, a, False], ['MOVE', r, a, b, False]] + out['public_move_down_and_up'] = ( + with_program(word, p), 'after the public MOVE at event %d insert MOVE %d %d->%d and back' % (i, r, b, a), + 'public_moves_up', {'record_profiles'}) + # A public MOVE removed: the frame chain breaks. + p = list(prog) + del p[i] + out['public_move_removed'] = ( + with_program(word, p), 'delete the public MOVE at event %d' % i, 'frame_chain', {'record_profiles'}) + return out + + +def run_controls(root, frames, word, mains, native_record): + rec_paid, rec_credit = native_record + results = {} + for name, (mutated, text, target, allowed) in mutations(word, mains).items(): + rp = replay(mutated, frames) + checks, _ = evaluate(rp, rec_paid, rec_credit) + failed = {k for k, v in checks.items() if not v} + results[name] = dict(edit=text, target_check=target, failed_checks=sorted(failed), + rejected=target in failed and failed <= {target} | allowed) + return results + + +def run_profile_controls(T, P, calls, sol7, tested, root, frames6, word6, rp6): + """Controls on the rebuild and the crossing check.""" + out = {} + # 1. Rebuild #6030 without the query term (the reviewer's 15 x 104 on P[1]). + checks, res = known_6030(root, frames6, word6, rp6, include_query=False) + failed = sorted(k for k, v in checks.items() if not v) + out['6030_without_query_term'] = dict( + edit='leave the query shears (15 x 104 on P[1]) out of the rebuild', target_check='P_equals_record', + failed_checks=failed, crossing=res['crossing'], + rejected='P_equals_record' in failed and 'paid_calls_equal_expect' in failed) + # 2. Remove one rank-8 credit per call (one tail loses its credit): root must drop below tested. + T2 = Counter(T) + T2[8] -= calls + root2 = solve(T2, P)['root'] + out['7000_one_rank8_credit_lost_per_call'] = dict( + edit='remove one rank-8 credit in each of the %d calls from T' % calls, + target_check='root_at_least_tested', crossing=digits(root2, 30), + failed_checks=['root_at_least_tested'] if F(root2) < tested else [], + rejected=F(root2) < tested and F(sol7['root']) >= tested) + return out + + +def run_all(root=ROOT): + bad = verify_pins(root) + if bad: + raise CheckFailure('input bytes differ from their pins: ' + ', '.join(bad)) + word7 = load(Path(root) / P7 / 'data/final.json.gz') + record7 = load(Path(root) / P7 / 'FULL-RESULT.json') + frames7 = Frames(word7['frames']) + rp7 = replay(word7, frames7) + rec_paid7, rec_credit7 = clean(record7['native']['paid']), clean(record7['native']['helpers']) + checks7, info7 = evaluate(rp7, rec_paid7, rec_credit7) + inv_checks, inv_res, (T, P, calls, sol7) = run_7000(root, frames7, word7, rp7, record7) + + word6 = load(Path(root) / P6 / 'WORD.json.gz') + record6 = load(Path(root) / P6 / 'RESULT.json') + frames6 = Frames(word6['frames']) + rp6 = replay(word6, frames6) + rec_paid6, rec_credit6 = clean(record6['local']['paid']), clean(record6['local']['helpers']) + checks6, info6 = evaluate(rp6, rec_paid6, rec_credit6) + kn_checks, kn_res = known_6030(root, frames6, word6, rp6) + + controls = run_controls(root, frames7, word7, rp7.mains, (rec_paid7, rec_credit7)) + controls.update(run_profile_controls(T, P, calls, sol7, F(record7['gate']['tested']), + root, frames6, word6, rp6)) + result = { + 'status': None, + 'base_commit': json.loads((PKG / 'SOURCES.json').read_text())['base_commit'], + 'input_sha256': input_pins(root), + 'word_7000': dict(events=len(word7['program']), public=word7['public'], mains=len(rp7.mains), + helpers=len(rp7.helpers), checks=checks7, **info7), + 'invoice_7000': dict(checks=inv_checks, **inv_res), + 'word_6030': dict(events=len(word6['program']), public=word6['public'], mains=len(rp6.mains), + helpers=len(rp6.helpers), checks=checks6, **info6), + 'known_answer_6030': dict(checks=kn_checks, **kn_res), + 'controls': controls, + } + failures = [] + for section in ('word_7000', 'invoice_7000', 'word_6030', 'known_answer_6030'): + failures += [section + ':' + k for k, v in result[section]['checks'].items() if not v] + failures += ['control:' + k for k, v in controls.items() if not v['rejected']] + result['status'] = 'PASS' if not failures else 'FAIL' + result['failures'] = failures + return json.loads(json.dumps(result, default=str, sort_keys=True)) + + +def main(argv): + try: + result = run_all() + except (CheckFailure, ValueError) as error: + print('FAIL', error) + return 1 + text = json.dumps(result, indent=1, sort_keys=True) + '\n' + result_path = PKG / 'RESULT.json' + if '--write' in argv: + result_path.write_text(text) + print(text, end='') + if result['failures']: + print('FAIL', ', '.join(result['failures'])) + return 1 + if '--write' not in argv and (not result_path.exists() or result_path.read_text() != text): + print('FAIL RESULT.json differs from this run; rerun with --write and review the change') + return 1 + print('PASS') + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/tests/test_independent_global_rank_replay.py b/tests/test_independent_global_rank_replay.py new file mode 100644 index 0000000000..aef08480cd --- /dev/null +++ b/tests/test_independent_global_rank_replay.py @@ -0,0 +1,119 @@ +"""Controls for research/independent-global-rank-replay. + +The full replay takes about 40 seconds. It runs once for the whole module. +""" +import importlib.util +import json +from pathlib import Path +import shutil +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +PKG = ROOT / 'research/independent-global-rank-replay' +spec = importlib.util.spec_from_file_location('independent_replay', PKG / 'replay.py') +replay = importlib.util.module_from_spec(spec) +spec.loader.exec_module(replay) + + +class FastTests(unittest.TestCase): + def test_empty_profile_is_an_error(self): + with self.assertRaises(ValueError): + replay.solve({}, {}) + + def test_rank_zero_is_an_error(self): + with self.assertRaises(ValueError): + replay.solve({0: 1, 1: 5}, {2: 1}) + + def test_no_sign_change_is_an_error(self): + with self.assertRaises(ValueError): + replay.solve({1: 5}, {2: 1}) + + def test_derivative_bound_sees_a_rising_term(self): + # A rank-9 deficit makes the moment rise with k, so the upper bound is positive. + self.assertGreater(replay.Moment({1: 5}, {9: 1}).derivative_upper('0.01'), 0) + # A rank-2 surplus makes it fall. + self.assertLess(replay.Moment({2: 5}, {1: 9}).derivative_upper('0.01'), 0) + + def test_simple_root_is_found(self): + # m(k) = 400 * 2^(-k) - 399, so the root is log2(400/399). + import math + sol = replay.solve({2: 200}, {1: 399}) + self.assertAlmostEqual(float(sol['root']), math.log2(400 / 399), places=12) + + def test_chain_of_three_parts(self): + frames = replay.Frames([[], [1], [1, 2], [1, 2, 4], [2]]) + self.assertEqual(replay.chain_check(frames, [(0, 1), (1, 2), (2, 3)]), (True, [1, 1, 1])) + # Broken link: the second part does not start where the first ends. + self.assertFalse(replay.chain_check(frames, [(0, 1), (0, 2), (2, 3)])[0]) + # A part that is not nested. + self.assertFalse(replay.chain_check(frames, [(0, 1), (1, 4), (4, 3)])[0]) + # A single part is not a split. + self.assertFalse(replay.chain_check(frames, [(0, 1)])[0]) + + def test_input_pins_hold_and_detect_change(self): + self.assertEqual(replay.verify_pins(), []) + pins = replay.input_pins() + with tempfile.TemporaryDirectory() as tmp: + for path in pins: + target = Path(tmp) / path + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy(ROOT / path, target) + victim = Path(tmp) / sorted(pins)[0] + victim.write_bytes(victim.read_bytes() + b' ') + self.assertEqual(replay.verify_pins(Path(tmp)), [sorted(pins)[0]]) + + +class FullReplayTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.result = replay.run_all() + + def test_everything_passes(self): + self.assertEqual(self.result['failures'], []) + self.assertEqual(self.result['status'], 'PASS') + + def test_committed_result_matches(self): + committed = json.loads((PKG / 'RESULT.json').read_text()) + self.assertEqual(committed, self.result) + + def test_headline_numbers_7000(self): + inv = self.result['invoice_7000'] + self.assertEqual(inv['credit_mass_per_call'], 7650) + self.assertEqual(inv['paid_mass_per_call'], 10962) + self.assertEqual(inv['rank_surplus'], 1520) + self.assertTrue(inv['crossing'].startswith('0.0036300581746883670859')) + + def test_known_answer_6030(self): + kn = self.result['known_answer_6030'] + self.assertEqual((kn['productive'], kn['paid'], kn['rank_surplus']), (13444, 66834, 760)) + self.assertEqual(kn['query_term'], 1560) + self.assertTrue(kn['crossing'].startswith('0.0031258115579118972')) + + def test_every_control_is_rejected_by_its_target(self): + controls = self.result['controls'] + self.assertEqual(len(controls), 7) + for name, control in controls.items(): + with self.subTest(name): + self.assertIn(control['target_check'], control['failed_checks']) + self.assertTrue(control['rejected']) + + def test_unmutated_inputs_pass_every_target_check(self): + for section in ('word_7000', 'invoice_7000', 'word_6030', 'known_answer_6030'): + for name, ok in self.result[section]['checks'].items(): + with self.subTest(section + ':' + name): + self.assertTrue(ok) + targets = {c['target_check'] for c in self.result['controls'].values()} + # Each target is a check that the unmutated runs above report as passing. + reported = set() + for section in ('word_7000', 'invoice_7000', 'word_6030', 'known_answer_6030'): + reported |= set(self.result[section]['checks']) + self.assertLessEqual(targets, reported) + + def test_clear_count_is_information_not_a_fault(self): + self.assertEqual(self.result['word_7000']['information']['owned_erasures_nonzero'], 53) + self.assertEqual(self.result['word_7000']['faults'], {}) + + +if __name__ == '__main__': + unittest.main()