Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/physics0/verify/test_pd0_ladder.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / physics0 / verify / test_pd0_ladder.c` 92 lines ยท 10415 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* The ladder checker: an honest oracle-shaped bundle reaches PROVISIONAL_LAW; every dishonest mutant stops for the recorded reason. */ 18 #include "bundle.h" 19 #include "pd0_receipt.h" 20 #include "sha256.h" 21 #include 22 static int g_checks, g_fail; 23 #define CHECK(c) do { g_checks++; if (!(c)) { g_fail++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) 24 #define CHECK_CODE(rc, R, want) do { g_checks++; if ((rc) != (want) && (R).code != (want) && (R).stall_code != (want)) { g_fail++; fprintf(stderr, "FAIL %s:%d: rc=%d (%s) R.code=%d (%s) state=%s want %s\n", __FILE__, __LINE__, rc, pd0v_name(rc), (R).code, pd0v_name((R).code), pd0_ladder_state_name((R).state), #want); } } while (0) 25 static const char *g_dir = "build/tests-physics0-verify/receipts"; 26 static void receipt(const char *kind, const bundle *b, int code) { uint8_t h[32]; sha256_hash(b->buf, b->len, h); char path[1024]; if (pd0_receipt_write(g_dir, kind, h, code, NULL, path, sizeof path)) fprintf(stderr, "receipt write failed for %s\n", kind); } 27 static int run(int level, uint64_t seed, const bundle_opts *o, pd0_ladder_report *R, uint8_t *law, size_t *law_len, const char *kind) 28 { 29 bundle b; b_init(&b, level, seed); int rc = b_build(&b, o, R); if (law) *law_len = pd0_ladder_emit_law(R, &b.P, law, 8192); receipt(kind, &b, rc ? rc : R->code); b_free(&b); return rc; 30 } 31 int main(void) 32 { 33 static uint8_t law[8192], law2[8192]; size_t ln = 0; pd0_ladder_report R; bundle_opts o; pd0_ladder_params P; pd0_ladder_params_default(&P, 2, 1); 34 /* honest, three levels and several seeds */ 35 int levels[] = { 1, 2, 4 }; 36 for (int li = 0; li < 3; li++) for (uint64_t seed = 1; seed <= 3; seed++) { 37 memset(&o, 0, sizeof o); int rc = run(levels[li], seed, &o, &R, law, &ln, "ladder-honest"); 38 CHECK(rc == 0 && R.state == LS_REPLICATED); CHECK(R.n_refutations == 0 && R.p == 35 && R.f == 0 && R.confidence_ppm == pd0_confidence_ppm(35, 0)); 39 CHECK(ln > 0 && pd0_ladder_verify_law(&R, &P, law, ln) == PD0V_OK); 40 pd0_law *l = calloc(1, sizeof *l); CHECK(pd0_law_parse(law, ln, l) == 0 && l->state == PDLAW_PROVISIONAL_LAW && l->n_experiments == 8 && l->rel.n_refutations == 0); 41 if (li == 0 && seed == 1) printf("claim: %.*s\n", (int)l->claim_len, l->claim); 42 /* T8 tampering on the honest law */ 43 pd0_law m = *l; m.confidence_ppm += 1; size_t n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_CONFIDENCE_MISMATCH); 44 m = *l; memcpy(m.claim, "This is always true. ", 21); n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_CLAIM_FORBIDDEN_WORD); 45 m = *l; m.claim[0] = 'B'; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_CLAIM_TEXT_MISMATCH); 46 m = *l; m.n_experiments--; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_EXPERIMENT_MISSING); 47 m = *l; m.chain_root[0] ^= 1; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_CHAIN_ROOT_MISMATCH); 48 m = *l; m.rel.eq[1].coef[0] += 1; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_RELATION_MISMATCH); 49 m = *l; m.dom.n_observations += 1; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_DOMAIN_MISMATCH); 50 m = *l; m.n_exceptions = 1; n = pd0_law_write(&m, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_EXCEPTIONS_MISMATCH); 51 memcpy(law2, law, ln); law2[ln - 1] ^= 1; CHECK(pd0_ladder_verify_law(&R, &P, law2, ln) == PD0V_LAW_ID_MISMATCH); 52 free(l); } 53 /* mutants: each stops for the recorded reason */ 54 memset(&o, 0, sizeof o); o.skip_corr = 1; int rc = run(1, 1, &o, &R, law, &ln, "ladder-skip-state"); CHECK_CODE(rc, R, PD0V_T8_STATE_SKIPPED); CHECK(R.state == LS_OBSERVATION); 55 memset(&o, 0, sizeof o); o.fake_corr = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-fake-corr"); CHECK_CODE(rc, R, PD0V_T2_CORR_MISMATCH); CHECK(R.state == LS_OBSERVATION); 56 memset(&o, 0, sizeof o); o.no_rival = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-no-rival"); CHECK_CODE(rc, R, PD0V_T4_NO_RIVAL); CHECK(R.state == LS_CANDIDATE); 57 memset(&o, 0, sizeof o); o.eps_above_bound = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-eps-above-bound"); CHECK_CODE(rc, R, PD0V_T4_EPS_ABOVE_BOUND); CHECK(R.state == LS_CANDIDATE); 58 memset(&o, 0, sizeof o); o.reuse_schedule = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-reused-schedule"); CHECK_CODE(rc, R, PD0V_T5_SCHEDULE_REUSED); 59 memset(&o, 0, sizeof o); o.trial_without_prereg = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-trial-not-prereg"); CHECK_CODE(rc, R, PD0V_T6_TRIAL_NOT_PREREGISTERED); 60 memset(&o, 0, sizeof o); o.rep_before_candidate = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-rep-in-fit"); CHECK_CODE(rc, R, PD0V_REP_INFLUENCED_FIT); 61 memset(&o, 0, sizeof o); o.batch_seed_reuse = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-stream-reuse"); CHECK_CODE(rc, R, PD0V_T7_STREAM_REUSED); 62 memset(&o, 0, sizeof o); o.all_planner = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-origin-imbalance"); CHECK_CODE(rc, R, PD0V_T7_ORIGIN_IMBALANCE); 63 /* wrong constant: the committed predictions miss; the hypothesis is refuted, not promoted */ 64 memset(&o, 0, sizeof o); o.wrong_k = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-wrong-k"); CHECK((rc == 0 || rc == PD0V_T8_STATE_SKIPPED) && R.n_refutations == 1 && R.state == LS_CANDIDATE && R.n_exceptions == 1 && R.f >= 1 && R.last_nrmse_micro > 20000); /* refuted on trial 1; later preregs without a new T4 are a skipped state */ 65 CHECK(pd0_ladder_verify_law(&R, &P, law, ln) == 0); { pd0_law *l = calloc(1, sizeof *l); CHECK(pd0_law_parse(law, ln, l) == 0 && l->state != PDLAW_PROVISIONAL_LAW); free(l); } 66 /* partial ladders: a PROVISIONAL_LAW record is refused as a skipped state */ 67 memset(&o, 0, sizeof o); o.stop_after_prereg = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-stop-prereg"); CHECK(rc == 0 && R.state == LS_PREDICTED && R.code == PD0V_T6_TRIAL_MISSING); 68 { pd0_law *l = calloc(1, sizeof *l); pd0_law_parse(law, ln, l); l->state = PDLAW_PROVISIONAL_LAW; size_t n = pd0_law_write(l, law2, sizeof law2); CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_T8_STATE_SKIPPED); free(l); } 69 memset(&o, 0, sizeof o); o.stop_after_trials = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-stop-trials"); CHECK(rc == 0 && R.state == LS_INTERVENED && R.code == PD0V_T7_TOO_FEW_BATCHES); 70 /* fabricated law with no evidence at all (M4) */ 71 { static uint8_t empty[64]; pd0_ladder_report R0; CHECK(pd0_ladder_check(empty, 0, &P, &R0) == 0 && R0.state == LS_START); 72 pd0_law *l = calloc(1, sizeof *l); l->state = PDLAW_PROVISIONAL_LAW; l->rel.n_vars = 2; l->rel.n_channels = 1; l->dom.n_obs = 2; l->dom.n_channels = 1; size_t n = pd0_law_write(l, law2, sizeof law2); 73 CHECK(pd0_ladder_verify_law(&R0, &P, law2, n) == PD0V_T8_STATE_SKIPPED); free(l); } 74 /* demotion: a later contradicting trial pulls the law back to HYPOTHESIS */ 75 /* spec rev 6 (b): EPISODE_END records carry an outcome; an honest ladder whose every episode ends with one still replicates */ 76 memset(&o, 0, sizeof o); o.end_last = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-episode-end"); CHECK(rc == 0 && R.state == LS_REPLICATED && R.n_refutations == 0); 77 /* spec rev 6 (a): trial error is normalised by the pooled FIT sd, not the within-trial spread: quiet trials with 78 * observation noise (sd 0.02, eps 0.02 x 3 batches eps) must not refute the true relation */ 79 memset(&o, 0, sizeof o); o.quiet_trials = 1; o.obs_noise = 20000; rc = run(1, 1, &o, &R, law, &ln, "ladder-quiet-noisy-trials"); CHECK(rc == 0 && R.state >= LS_INTERVENED && R.n_refutations == 0); 80 { int64_t pred[6] = { 1000, 0, 2000, 0, 3000, 0 }, obs[6] = { 11000, 0, 12000, 0, 13000, 0 }, sd[2] = { `1000000`, `1000000` }; 81 CHECK(pd0_nrmse_micro(pred, obs, 3, 2) > `10000000`); /* within-trial spread 0.0008: error reads as 12 sd */ 82 CHECK(pd0_nrmse_pooled_micro(pred, obs, 3, 2, sd) == 10000); /* pooled sd 1.0: the same error is 0.01 */ 83 int64_t sd0[2] = { 0, 0 }; CHECK(pd0_nrmse_pooled_micro(pred, obs, 3, 2, sd0) == 10000); } /* sd 0 falls back to the RMSE */ 84 /* spec rev 7: void TRIAL/REP episodes (OUT_OF_BOUNDS before the horizon) are replaced and never count toward p or f */ 85 memset(&o, 0, sizeof o); o.void_rep = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-void-rep"); CHECK(rc == 0 && R.state == LS_REPLICATED && R.n_void_episodes == 1 && R.n_void_batches == 0 && R.p == 35 && R.f == 0); 86 memset(&o, 0, sizeof o); o.void_rep_four = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-void-batch"); CHECK(rc == 0 && R.state == LS_REPLICATED && R.n_void_batches == 1 && R.n_void_episodes == 4 && R.p == 35 && R.f == 0); 87 memset(&o, 0, sizeof o); o.void_trial = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-void-trial"); CHECK(rc == 0 && R.state == LS_REPLICATED && R.n_void_episodes == 1 && R.p == 35 && R.f == 0 && R.n_experiments == 8); 88 memset(&o, 0, sizeof o); o.demote_after_law = 1; rc = run(1, 1, &o, &R, law, &ln, "ladder-demotion"); CHECK(rc == 0 && R.demoted && R.state == LS_HYPOTHESIS && R.n_refutations == 1); 89 { bundle b; b_init(&b, 1, 1); bundle_opts h; memset(&h, 0, sizeof h); pd0_ladder_report Rh; b_build(&b, &h, &Rh); size_t n = pd0_ladder_emit_law(&Rh, &b.P, law2, sizeof law2); b_free(&b); 90 CHECK(pd0_ladder_verify_law(&R, &P, law2, n) == PD0V_LAW_DEMOTED); } 91 printf("test_pd0_ladder: %d checks, %d failed\n", g_checks, g_fail); return g_fail ? 1 : 0; 92 }