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/patches/de9fdf8e5f0f7b59 # 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 ## GitHub aien-dev/omega-mv#251 (code only) closed derived `de9fdf8e5f0f7b59` #251 Imported from GitHub aien-dev/omega-mv#251 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `8da7f77194a3fae0c3dd561a76d1fe0e23971867` · Head `3c170f10dae463e8ff42ad2085fe7830fe1e6416` ### Apply it ``` git checkout -b patch-de9fdf8e5f0f7b59 8da7f77194a3fae0c3dd561a76d1fe0e23971867 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/de9fdf8e5f0f7b59.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 251 3c170f10dae463e8ff42ad2085fe7830fe1e6416 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":251,"url":"https://github.com/aien-dev/omega-mv/pull/251","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:59Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/evidence/physics0/planner/pd0-planner-eff-dev-8da7f77194a3.json b/evidence/physics0/planner/pd0-planner-eff-dev-8da7f77194a3.json new file mode 100644 index 000000000..31f480651 --- /dev/null +++ b/evidence/physics0/planner/pd0-planner-eff-dev-8da7f77194a3.json @@ -0,0 +1,27 @@ +{ + "receipt": "PD0_PLANNER_EFFICIENCY_DEV", + "claim": "PD-0 development evidence only; NOT an EXP-003 claim", + "commit": "8da7f77194a3", + "eps_ppm": 20000, + "episode_cap": 40, + "residual_sd_micro": 10000, + "recorder": "STAND_IN", + "rows": [ + {"level": 1, "seed": 1, "active_episodes": 1, "active_cost_micro": 24694052, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 1, "seed": 2, "active_episodes": 1, "active_cost_micro": 19850772, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 1, "seed": 3, "active_episodes": 1, "active_cost_micro": 19850772, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 1, "seed": 4, "active_episodes": 1, "active_cost_micro": 21826716, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 1, "seed": 5, "active_episodes": 1, "active_cost_micro": 24694052, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 2, "seed": 1, "active_episodes": 1, "active_cost_micro": 21615428, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 2, "seed": 2, "active_episodes": 1, "active_cost_micro": 18829432, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 2, "seed": 3, "active_episodes": 1, "active_cost_micro": 21615428, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 2, "seed": 4, "active_episodes": 1, "active_cost_micro": 23011364, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 2, "seed": 5, "active_episodes": 1, "active_cost_micro": 21615428, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 4, "seed": 1, "active_episodes": 1, "active_cost_micro": 24694052, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 4, "seed": 2, "active_episodes": 1, "active_cost_micro": 22221484, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 4, "seed": 3, "active_episodes": 1, "active_cost_micro": 23325040, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 4, "seed": 4, "active_episodes": 1, "active_cost_micro": 18594356, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false}, + {"level": 4, "seed": 5, "active_episodes": 1, "active_cost_micro": 24694052, "passive_episodes": 1, "passive_cost_micro": 20107052, "active_refuted_at": [1, 1], "passive_refuted_at": [1, 1], "valid": true, "no_discriminating": false} + ], + "summary": {"active_fewer": 0, "ties": 15, "passive_fewer": 0, "invalid": 0, "total_active_episodes": 15, "total_passive_episodes": 15} +} diff --git a/mk/physics0-planner.mk b/mk/physics0-planner.mk new file mode 100644 index 000000000..07ce2c1f6 --- /dev/null +++ b/mk/physics0-planner.mk @@ -0,0 +1,61 @@ +# PD-0 experiment planner (Direction 5): contract tests, purity check, passive baseline +# and the information-efficiency comparison (development evidence, not EXP-003). +ifndef PHYSICS0_PLANNER_MK +PHYSICS0_PLANNER_MK := 1 +.PHONY: test-physics0-planner p0p-purity physics0-planner-eff + +P0P_COMMIT := $(shell git rev-parse --short=12 HEAD 2>/dev/null || echo unknown) +P0P_CFLAGS = -std=c11 -Wall -Wextra -Werror -pedantic -O2 -D_POSIX_C_SOURCE=200809L -ffp-contract=off -fno-fast-math \ + -Isrc -Isrc/physics0/ladder -Isrc/physics0/planner -Itests/physics0/planner +P0P_ASAN = -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all +# planner objects: verifier formats only, no generator, no world +P0P_SRCS = src/physics0/planner/pd0_planner.c src/physics0/ladder/pd0_fmt.c src/sha256.c +P0P_HDRS = src/physics0/planner/pd0_planner.h src/physics0/ladder/pd0_fmt.h src/physics0/ladder/pd0_rng.h +# bridge object: substrate formats, links the generator on purpose (harness side) +P0P_BRIDGE_CFLAGS = -std=c11 -Wall -Wextra -Werror -pedantic -O2 -D_POSIX_C_SOURCE=200809L -Isrc -Itests/physics0/planner +P0P_BRIDGE_SRCS = tests/physics0/planner/pd0p_bridge.c src/physics0/pd0_gen.c src/physics0/pd0_world.c src/physics0/pd0_guard.c \ + src/physics0/pd0_wire.c src/physics0/pd0_rng.c src/physics0/pd0_relation.c src/physics0/pd0_chan.c +P0P_DIR = $(OUT_DIR)/tests-physics0-planner +P0P_FORBIDDEN = 'pd0_gen|pd0_world|oracle|truth_|forge_|argus_|mmap|fork|exec|socket' + +$(P0P_DIR)/test_pd0_planner: tests/physics0/planner/test_pd0_planner.c $(P0P_SRCS) $(P0P_HDRS) + @mkdir -p $(P0P_DIR) + $(CC) $(P0P_CFLAGS) -o $@ $< $(P0P_SRCS) -lm +$(P0P_DIR)/test_pd0_planner_asan: tests/physics0/planner/test_pd0_planner.c $(P0P_SRCS) $(P0P_HDRS) + @mkdir -p $(P0P_DIR) + $(CC) $(P0P_CFLAGS) $(P0P_ASAN) -o $@ $< $(P0P_SRCS) -lm + +$(P0P_DIR)/bridge/%.o: tests/physics0/planner/%.c tests/physics0/planner/pd0p_bridge.h $(wildcard src/physics0/*.h) + @mkdir -p $(P0P_DIR)/bridge + $(CC) $(P0P_BRIDGE_CFLAGS) -c -o $@ $< +$(P0P_DIR)/bridge/%.o: src/physics0/%.c $(wildcard src/physics0/*.h) + @mkdir -p $(P0P_DIR)/bridge + $(CC) $(P0P_BRIDGE_CFLAGS) -c -o $@ $< +P0P_BRIDGE_OBJS = $(patsubst %.c,$(P0P_DIR)/bridge/%.o,$(notdir $(P0P_BRIDGE_SRCS))) + +# the substrate and the verifier both define the LE helpers and pd0_mul; the bridge side is merged into one +# relocatable object and those seven symbols are renamed there so the two families link side by side +P0P_CLASH = pd0_put_u16 pd0_put_u32 pd0_put_u64 pd0_get_u16 pd0_get_u32 pd0_get_u64 pd0_mul +$(P0P_DIR)/bridge.o: $(P0P_BRIDGE_OBJS) + $(LD) -r -o $@ $(P0P_BRIDGE_OBJS) + objcopy $(foreach s,$(P0P_CLASH),--redefine-sym $(s)=pb_sub_$(s)) $@ +$(P0P_DIR)/pd0_plan_eff: tests/physics0/planner/pd0_plan_eff.c $(P0P_SRCS) $(P0P_HDRS) $(P0P_DIR)/bridge.o + @mkdir -p $(P0P_DIR) + $(CC) $(P0P_CFLAGS) -o $@ $< $(P0P_SRCS) $(P0P_DIR)/bridge.o -lm + +p0p-purity: + @mkdir -p $(P0P_DIR)/purity + @$(CC) $(P0P_CFLAGS) -c -o $(P0P_DIR)/purity/pd0_planner.o src/physics0/planner/pd0_planner.c + @if nm $(P0P_DIR)/purity/pd0_planner.o | grep -E $(P0P_FORBIDDEN); then echo "planner object references generator, world or authority symbols"; exit 1; fi + @echo "p0p-purity: planner object carries no generator, world or authority symbol" + +physics0-planner-eff: $(P0P_DIR)/pd0_plan_eff + @rm -f $(P0P_DIR)/pd0-planner-eff.json + ./$(P0P_DIR)/pd0_plan_eff $(P0P_DIR)/pd0-planner-eff.json $(P0P_COMMIT) | tee $(P0P_DIR)/eff.out | tail -3 + @grep -q '^PD0_PLANNER_EFF: PASS$$' $(P0P_DIR)/eff.out + +test-physics0-planner: p0p-purity $(P0P_DIR)/test_pd0_planner $(P0P_DIR)/test_pd0_planner_asan physics0-planner-eff + ./$(P0P_DIR)/test_pd0_planner + ./$(P0P_DIR)/test_pd0_planner_asan + @echo "test-physics0-planner: PD-0 planner contract (behaviours 1-6), mutants, passive baseline and efficiency comparison pass; receipt in $(P0P_DIR)/pd0-planner-eff.json" +endif diff --git a/src/physics0/planner/pd0_planner.c b/src/physics0/planner/pd0_planner.c new file mode 100644 index 000000000..7842b32bd --- /dev/null +++ b/src/physics0/planner/pd0_planner.c @@ -0,0 +1,268 @@ +/* PD-0 experiment planner, see pd0_planner.h. Integer arithmetic only. */ +#include +#include +#include "pd0_planner.h" +#include "pd0_rng.h" + +__extension__ typedef __int128 p_i128; + +#define P_MAGIC "PD0PLN1" + +typedef struct { + pd0_exp exp; + int64_t d_micro, info_micro, cost_micro; + int eligible; /* 0 if the hash is already in the prior set */ +} p_cand; + +/* ---- helpers ---- */ + +static int64_t p_unit_in(pd0_rng *r, int64_t lo, int64_t hi) { + int64_t u = pd0_rng_unit(r); + p_i128 span = (p_i128)(hi - lo) * u; + return lo + (int64_t)(span / 1000000); +} + +static int p_bounds_ok(const pd0p_bounds *b) { + if (b->n_obs == 0 || b->n_obs > PD0_MAX_OBS) return 0; + if (b->n_channels == 0 || b->n_channels > PD0_MAX_CHAN) return 0; + for (int i = 0; i < b->n_obs; i++) if (b->reset_min[i] > b->reset_max[i]) return 0; + for (int i = 0; i < b->n_channels; i++) if (b->chan_min[i] > b->chan_max[i]) return 0; + return 1; +} + +static int p_sched_in_bounds(const pd0p_bounds *b, const pd0_exp *e) { + if (e->n_obs != b->n_obs || e->n_steps == 0 || e->n_steps > PD0_MAX_STEPS) return 0; + if (b->episode_max_steps && e->n_steps > b->episode_max_steps) return 0; + for (int i = 0; i < e->n_obs; i++) + if (e->reset[i] < b->reset_min[i] || e->reset[i] > b->reset_max[i]) return 0; + for (int s = 0; s < e->n_steps; s++) { + uint8_t c = e->steps[s].channel; + if (c >= b->n_channels) return 0; /* passive step (255) is not emitted by the planner */ + if (e->steps[s].value < b->chan_min[c] || e->steps[s].value > b->chan_max[c]) return 0; + } + return 1; +} + +static uint8_t p_steps_for(const pd0p_bounds *b, uint32_t budget_steps) { + uint32_t n = PD0_MAX_STEPS; + if (b->episode_max_steps && b->episode_max_steps < n) n = b->episode_max_steps; + if (budget_steps < n) n = budget_steps; + return (uint8_t)n; +} + +static void p_random_sched(pd0_rng *r, const pd0p_bounds *b, uint8_t n_steps, pd0_exp *e) { + memset(e, 0, sizeof *e); + e->n_obs = b->n_obs; e->n_steps = n_steps; + for (int i = 0; i < b->n_obs; i++) e->reset[i] = p_unit_in(r, b->reset_min[i], b->reset_max[i]); + for (int s = 0; s < n_steps; s++) { + uint8_t c = (uint8_t)(pd0_rng_next(r) % b->n_channels); + e->steps[s].channel = c; + e->steps[s].value = p_unit_in(r, b->chan_min[c], b->chan_max[c]); + } +} + +/* rollout of every hypothesis; D, info proxy, cost; schedule hash */ +static void p_evaluate(const pd0p_hyps *h, pd0_exp *e, int64_t *d_out, int64_t *info_out, int64_t *cost_out) { + int64_t r = h->residual_sd_micro; + int64_t maxdiff = 0, cost = 0; + p_i128 info = 0; + e->n_hyp = h->n_hyp; + for (int k = 0; k < h->n_hyp; k++) { + const pd0_rel *rel = h->hyp[k]; + int64_t state[PD0_MAX_VARS], next[PD0_MAX_VARS]; + memset(state, 0, sizeof state); + for (int i = 0; i < e->n_obs; i++) state[i] = e->reset[i]; + for (int s = 0; s < e->n_steps; s++) { + pd0_rel_step(rel, state, e->steps[s].channel, e->steps[s].value, next); + for (int i = 0; i < e->n_obs; i++) e->expected[k][s][i] = next[i]; + memcpy(state, next, sizeof state); + } + } + for (int s = 0; s < e->n_steps; s++) { + int64_t stepmax = 0; + for (int a = 0; a < h->n_hyp; a++) + for (int b2 = a + 1; b2 < h->n_hyp; b2++) + for (int i = 0; i < e->n_obs; i++) { + int64_t d = e->expected[a][s][i] - e->expected[b2][s][i]; + if (d < 0) d = -d; + if (d > stepmax) stepmax = d; + } + if (stepmax > maxdiff) maxdiff = stepmax; + p_i128 q = ((p_i128)stepmax * 1000000) / r; /* stepmax / r in micro */ + info += (q * q) / 1000000; + cost += e->steps[s].value < 0 ? -e->steps[s].value : e->steps[s].value; + } + *d_out = (int64_t)(((p_i128)maxdiff * 1000000) / r); + e->divergence_micro = *d_out; + if (info > INT64_MAX) info = INT64_MAX; + *info_out = (int64_t)info; + *cost_out = cost; + pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, e->schedule_hash); +} + +/* spec 8 ordering: higher D, then lower cost, then lexicographic hash */ +static int p_better(const p_cand *a, const p_cand *b) { + if (a->eligible != b->eligible) return a->eligible > b->eligible; + if (a->d_micro != b->d_micro) return a->d_micro > b->d_micro; + if (a->cost_micro != b->cost_micro) return a->cost_micro < b->cost_micro; + return memcmp(a->exp.schedule_hash, b->exp.schedule_hash, PD0_HASH) < 0; +} + +static void p_sort(p_cand *c, uint32_t n) { /* insertion sort: n is small and the order must be total */ + for (uint32_t i = 1; i < n; i++) { + p_cand tmp; memcpy(&tmp, &c[i], sizeof tmp); + uint32_t j = i; + while (j > 0 && p_better(&tmp, &c[j - 1])) { memcpy(&c[j], &c[j - 1], sizeof tmp); j--; } + memcpy(&c[j], &tmp, sizeof tmp); + } +} + +static uint8_t p_outside_middle(const pd0p_domain *dom, const pd0_exp *e) { + for (int i = 0; i < e->n_obs; i++) { + int64_t lo = dom->var_min[i], hi = dom->var_max[i]; + if (hi <= lo) continue; + int64_t q = (hi - lo) / 4, mlo = lo + q, mhi = hi - q; + if (e->reset[i] < mlo || e->reset[i] > mhi) return 1; + for (int s = 0; s < e->n_steps; s++) + if (e->expected[0][s][i] < mlo || e->expected[0][s][i] > mhi) return 1; + } + return 0; +} + +/* ---- public ---- */ + +void pd0p_prior_add(pd0p_prior *p, const uint8_t hash[PD0_HASH]) { + if (p->n < PD0P_MAX_PRIOR) memcpy(p->hash[p->n++], hash, PD0_HASH); +} + +int pd0p_prior_has(const pd0p_prior *p, const uint8_t hash[PD0_HASH]) { + if (!p) return 0; + for (uint32_t i = 0; i < p->n; i++) if (memcmp(p->hash[i], hash, PD0_HASH) == 0) return 1; + return 0; +} + +int pd0p_validate(const pd0p_bounds *b, const pd0p_prior *prior, const pd0_exp *e) { + uint8_t h[PD0_HASH]; + if (!p_bounds_ok(b) || e->n_obs != b->n_obs) return PD0P_ERR_INPUT; + pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, h); + if (memcmp(h, e->schedule_hash, PD0_HASH) != 0) return PD0P_ERR_INPUT; + if (!p_sched_in_bounds(b, e)) return PD0P_ERR_BOUNDS; + if (pd0p_prior_has(prior, e->schedule_hash)) return PD0P_ERR_DUPLICATE; + return PD0P_OK; +} + +int pd0p_propose(const pd0p_bounds *b, const pd0p_domain *dom, const pd0p_hyps *h, + uint32_t budget_steps, const pd0p_prior *prior, uint64_t seed, pd0p_proposal *out) { + memset(out, 0, sizeof *out); + out->seed = seed; + if (!p_bounds_ok(b) || !h || h->n_hyp < 2 || h->n_hyp > PD0_MAX_HYP || h->residual_sd_micro <= 0) + return out->status = PD0P_ERR_INPUT; + for (int k = 0; k < h->n_hyp; k++) + if (!h->hyp[k] || h->hyp[k]->n_vars < b->n_obs || h->hyp[k]->n_channels < b->n_channels) + return out->status = PD0P_ERR_INPUT; + if (budget_steps < 2) return out->status = PD0P_ERR_BUDGET; + uint8_t n_steps = p_steps_for(b, budget_steps); + + p_cand *c = calloc(PD0P_CANDIDATES, sizeof *c); + if (!c) return out->status = PD0P_ERR_INPUT; + pd0_rng rng; pd0_rng_stream(&rng, seed, "plan"); + uint32_t skipped = 0; + for (uint32_t i = 0; i < PD0P_CANDIDATES; i++) { + p_random_sched(&rng, b, n_steps, &c[i].exp); + p_evaluate(h, &c[i].exp, &c[i].d_micro, &c[i].info_micro, &c[i].cost_micro); + c[i].eligible = !pd0p_prior_has(prior, c[i].exp.schedule_hash); + if (!c[i].eligible) skipped++; + } + p_sort(c, PD0P_CANDIDATES); + uint32_t refined = 0; + for (uint32_t t = 0; t < PD0P_REFINE_TOP && t < PD0P_CANDIDATES; t++) { + for (uint32_t round = 0; round < PD0P_REFINE_ROUNDS; round++) { + p_cand m; memcpy(&m, &c[t], sizeof m); + uint32_t pos = (uint32_t)(pd0_rng_next(&rng) % (uint32_t)(b->n_obs + n_steps)); + if (pos < b->n_obs) m.exp.reset[pos] = p_unit_in(&rng, b->reset_min[pos], b->reset_max[pos]); + else { + uint32_t s = pos - b->n_obs; + uint8_t ch = (uint8_t)(pd0_rng_next(&rng) % b->n_channels); + m.exp.steps[s].channel = ch; + m.exp.steps[s].value = p_unit_in(&rng, b->chan_min[ch], b->chan_max[ch]); + } + p_evaluate(h, &m.exp, &m.d_micro, &m.info_micro, &m.cost_micro); + m.eligible = !pd0p_prior_has(prior, m.exp.schedule_hash); + if (!m.eligible) { skipped++; continue; } + if (p_better(&m, &c[t])) { memcpy(&c[t], &m, sizeof m); refined++; } + } + } + p_sort(c, PD0P_REFINE_TOP); + out->n_candidates = PD0P_CANDIDATES; + out->n_refined = refined; + out->n_skipped_duplicate = skipped; + if (!c[0].eligible) { free(c); return out->status = PD0P_ERR_DUPLICATE; } + memcpy(&out->exp, &c[0].exp, sizeof out->exp); + out->divergence_micro = c[0].d_micro; + out->info_proxy_micro = c[0].info_micro; + out->cost_micro = c[0].cost_micro; + out->outside_middle = dom ? p_outside_middle(dom, &out->exp) : 0; + free(c); + if (!p_sched_in_bounds(b, &out->exp)) return out->status = PD0P_ERR_BOUNDS; /* cannot happen; defence */ + if (out->divergence_micro < PD0P_MIN_D_MICRO) return out->status = PD0P_NO_DISCRIMINATING_EXPERIMENT; + return out->status = PD0P_OK; +} + +size_t pd0p_prereg_bytes(const pd0p_proposal *p, uint8_t *out, size_t cap) { + pd0_exp e; + if (p->status != PD0P_OK) return 0; + memcpy(&e, &p->exp, sizeof e); + return pd0_exp_write(&e, out, cap); +} + +static void p_put_i64(uint8_t *o, int64_t v) { pd0_put_u64(o, (uint64_t)v); } + +size_t pd0p_plan_write(const pd0p_proposal *p, uint8_t out[PD0P_PLAN_SIZE]) { + uint8_t *o = out; + memcpy(o, P_MAGIC, 7); o[7] = 0; o += 8; + o[0] = 1; o[1] = 0; o += 2; + *o++ = (uint8_t)(p->status & 0xff); + pd0_put_u32(o, p->n_candidates); o += 4; + pd0_put_u32(o, p->n_refined); o += 4; + pd0_put_u64(o, p->seed); o += 8; + p_put_i64(o, p->divergence_micro); o += 8; + p_put_i64(o, p->info_proxy_micro); o += 8; + p_put_i64(o, p->cost_micro); o += 8; + *o++ = p->outside_middle; + memcpy(o, p->exp.schedule_hash, PD0_HASH); o += PD0_HASH; + return (size_t)(o - out); +} + +int pd0p_plan_parse(const uint8_t *in, size_t len, pd0p_proposal *p) { + if (len != PD0P_PLAN_SIZE || memcmp(in, P_MAGIC "\0", 8) != 0) return -1; + if (in[8] != 1 || in[9] != 0) return -1; /* unknown version refused */ + memset(p, 0, sizeof *p); + p->status = (int8_t)in[10]; + p->n_candidates = pd0_get_u32(in + 11); + p->n_refined = pd0_get_u32(in + 15); + p->seed = pd0_get_u64(in + 19); + p->divergence_micro = (int64_t)pd0_get_u64(in + 27); + p->info_proxy_micro = (int64_t)pd0_get_u64(in + 35); + p->cost_micro = (int64_t)pd0_get_u64(in + 43); + p->outside_middle = in[51]; + memcpy(p->exp.schedule_hash, in + 52, PD0_HASH); + return 0; +} + +int pd0p_passive(const pd0p_bounds *b, uint32_t budget_steps, uint64_t stream_seed, uint32_t index, pd0_exp *out) { + if (!p_bounds_ok(b)) return PD0P_ERR_INPUT; + if (budget_steps < 2) return PD0P_ERR_BUDGET; + pd0_rng r; pd0_rng_stream(&r, stream_seed, "passive"); + r.s ^= (uint64_t)index * 0x9E3779B97F4A7C15ull; /* index-th schedule of the stream, deterministic */ + p_random_sched(&r, b, p_steps_for(b, budget_steps), out); + out->n_hyp = 0; + pd0_schedule_hash(out->n_obs, out->reset, out->n_steps, out->steps, out->schedule_hash); + return PD0P_OK; +} + +uint32_t pd0p_outside_fraction_ppm(const uint8_t *flags, uint32_t n) { + uint64_t k = 0; + if (n == 0) return 0; + for (uint32_t i = 0; i < n; i++) k += flags[i] ? 1u : 0u; + return (uint32_t)((k * 1000000ull) / n); +} diff --git a/src/physics0/planner/pd0_planner.h b/src/physics0/planner/pd0_planner.h new file mode 100644 index 000000000..2c73b17f9 --- /dev/null +++ b/src/physics0/planner/pd0_planner.h @@ -0,0 +1,95 @@ +/* PD-0 experiment planner (spec section 8, revision 5). Direction 5. + * + * The planner answers one question: given two or more plausible relations, + * which bounded experiment makes them disagree most? It PROPOSES only. It has + * no execution path: the proposal is a PD0EXP1 preregistration record (the + * verifier-owned layout in pd0_fmt.h) that the recorder must chain ahead of + * any TRIAL record before the schedule may run (spec behaviour 6). + * + * Inputs are learner-visible only: the hypothesis set as relationship blocks, + * the visited domain, the remaining budget, the describe bounds and a seed. + * No generator, oracle or authority symbol is referenced (purity check in + * mk/physics0-planner.mk). Floating point is used nowhere in this file: the + * divergence and cost are integer micro-unit arithmetic (128-bit intermediate). */ +#ifndef PD0_PLANNER_H +#define PD0_PLANNER_H + +#include +#include +#include "pd0_fmt.h" + +#define PD0P_CANDIDATES 256u /* spec 8: at least 256 seeded random schedules */ +#define PD0P_REFINE_TOP 16u /* spec 8: local refinement of the top 16 */ +#define PD0P_REFINE_ROUNDS 8u /* implementation choice, recorded in the plan record */ +#define PD0P_MAX_PRIOR 4096u +#define PD0P_MIN_D_MICRO 3000000LL /* spec 8 behaviour 4: best D < 3 means no discriminating experiment */ + +/* status of a proposal / result of validation */ +enum { + PD0P_OK = 0, + PD0P_NO_DISCRIMINATING_EXPERIMENT = 1, + PD0P_ERR_BOUNDS = -1, /* schedule outside the describe bounds: a planner defect (behaviour 2) */ + PD0P_ERR_DUPLICATE = -2, /* schedule hash already used (behaviour 3) */ + PD0P_ERR_INPUT = -3, /* malformed input or hash that does not recompute */ + PD0P_ERR_BUDGET = -4 /* fewer than 2 steps of budget left */ +}; + +typedef struct { + uint8_t n_obs, n_channels; + int64_t chan_min[PD0_MAX_CHAN], chan_max[PD0_MAX_CHAN]; + int64_t reset_min[PD0_MAX_OBS], reset_max[PD0_MAX_OBS]; /* PD0DESC2, per variable */ + uint32_t episode_max_steps; +} pd0p_bounds; + +/* visited range per observed variable (from the observed_domain block) */ +typedef struct { int64_t var_min[PD0_MAX_OBS], var_max[PD0_MAX_OBS]; } pd0p_domain; + +typedef struct { + uint8_t n_hyp; /* 2..PD0_MAX_HYP; hyp[0] is the best candidate */ + const pd0_rel *hyp[PD0_MAX_HYP]; + int64_t residual_sd_micro; /* pooled residual sd of the hypotheses on SELECT, micro, > 0 */ +} pd0p_hyps; + +typedef struct { uint32_t n; uint8_t hash[PD0P_MAX_PRIOR][PD0_HASH]; } pd0p_prior; + +typedef struct { + int status; /* PD0P_OK or PD0P_NO_DISCRIMINATING_EXPERIMENT (negative: error) */ + pd0_exp exp; /* the PD0EXP1 preregistration (schedule, expected trajectories, D, hash) */ + int64_t divergence_micro; /* D in micro-units (1_000_000 = one residual sd) */ + int64_t info_proxy_micro; /* sum over steps of (max pair difference / r)^2, micro */ + int64_t cost_micro; /* total |intervention| over the schedule, micro-units */ + uint32_t n_candidates, n_refined, n_skipped_duplicate; + uint8_t outside_middle; /* 1 if the schedule starts or reaches outside the middle 50% of the visited range of some variable */ + uint64_t seed; +} pd0p_proposal; + +/* Behaviours 1 to 6 of spec section 8. Returns the status (also stored in out->status). */ +int pd0p_propose(const pd0p_bounds *b, const pd0p_domain *dom, const pd0p_hyps *h, + uint32_t budget_steps, const pd0p_prior *prior, uint64_t seed, pd0p_proposal *out); + +/* Independent check of any PD0EXP1 against the bounds and the prior hashes: + * 0 ok, PD0P_ERR_BOUNDS, PD0P_ERR_DUPLICATE, PD0P_ERR_INPUT (hash does not recompute). */ +int pd0p_validate(const pd0p_bounds *b, const pd0p_prior *prior, const pd0_exp *e); + +/* PD0EXP1 bytes for the recorder to chain before any TRIAL record. 0 on error. */ +size_t pd0p_prereg_bytes(const pd0p_proposal *p, uint8_t *out, size_t cap); + +/* PD0PLN1: planner side record for receipts (fixed 84 bytes, little-endian): + * magic "PD0PLN1\0", version u16 = 1, status u8, n_candidates u32, n_refined u32, + * seed u64, divergence i64, info_proxy i64, cost i64, outside_middle u8, schedule_hash 32. */ +#define PD0P_PLAN_SIZE 84u +size_t pd0p_plan_write(const pd0p_proposal *p, uint8_t out[PD0P_PLAN_SIZE]); +int pd0p_plan_parse(const uint8_t *in, size_t len, pd0p_proposal *p); /* fills status, counters, hash only */ + +/* Preregistered passive baseline: the index-th uniform random schedule of a + * fixed stream. Deterministic, within bounds, no hypothesis is consulted. */ +int pd0p_passive(const pd0p_bounds *b, uint32_t budget_steps, uint64_t stream_seed, uint32_t index, pd0_exp *out); + +void pd0p_prior_add(pd0p_prior *p, const uint8_t hash[PD0_HASH]); +int pd0p_prior_has(const pd0p_prior *p, const uint8_t hash[PD0_HASH]); + +/* Run-level check of behaviour 5 (provisional 20% rule): fraction of proposals + * flagged outside_middle, in ppm. The caller decides what to do with it. */ +uint32_t pd0p_outside_fraction_ppm(const uint8_t *flags, uint32_t n); + +#endif diff --git a/tests/physics0/planner/pd0_plan_eff.c b/tests/physics0/planner/pd0_plan_eff.c new file mode 100644 index 000000000..57f1818a6 --- /dev/null +++ b/tests/physics0/planner/pd0_plan_eff.c @@ -0,0 +1,199 @@ +/* PD-0 planner information-efficiency comparison (Direction 5), development evidence. + * + * Question: does active experimental choice separate competing explanations + * with fewer observations or less intervention cost than a preregistered + * passive (uniform random) schedule? + * + * For L1, L2 and L4, seeds 1..5: the hypothesis set is the true relation + * (oracle-shaped, from the harness side; this is calibration, NOT a learner + * result) plus two rivals built mechanically from it: the first non-channel + * coefficient of the second equation scaled by 1.1, and the last non-channel + * term of the last equation removed. Each strategy runs episodes of 20 steps + * on the real world and stops when the pooled 20-step rollout NRMSE of EVERY + * rival against the observed trajectories exceeds the declared eps (0.02, the + * L0-L4 in-box bound), or at the episode cap. The truth's own NRMSE must stay + * within eps (sanity; otherwise the row is marked INVALID). + * + * This is PD-0 development evidence. It is NOT an EXP-003 claim and must not + * be cited as one: no blinding, no seal, no preregistered pass criterion. + * + * usage: pd0_plan_eff [commit] (receipt is created with O_EXCL; never overwrites) */ +#include +#include +#include +#include +#include +#include +#include +#include "pd0_planner.h" +#include "pd0p_bridge.h" + +#define EPS_PPM 20000 +#define EP_CAP 40 +#define RESID_SD_MICRO 10000 /* declared r for D: 0.01 units (noise-free worlds) */ +#define PLAN_SEED 2026100401ull +#define PASSIVE_SEED 2026100402ull + +typedef struct { double se[PD0_MAX_OBS], ss[PD0_MAX_OBS], mean[PD0_MAX_OBS]; uint64_t n; } acc; +typedef struct { int episodes[2]; int64_t cost[2]; int refuted_at[2][2]; int valid; int no_disc; } row; + +static void rel_from_pb(const pb_rel *in, pd0_rel *out) { + memset(out, 0, sizeof *out); + out->n_vars = in->n_vars; out->n_latent = in->n_latent; out->n_channels = in->n_channels; out->n_equations = in->n_eq; + for (int e = 0; e < in->n_eq; e++) { + out->eq[e].target = in->eq[e].target; out->eq[e].n_terms = in->eq[e].n_terms; + for (int t = 0; t < in->eq[e].n_terms; t++) { + out->eq[e].coef[t] = in->eq[e].coef[t]; + memcpy(out->eq[e].expo[t], in->eq[e].expo[t], PD0_MAX_VARS + PD0_MAX_CHAN); + } + } + out->description_bits = pd0_rel_bits(out); +} + +static int is_channel_term(const pd0_rel *r, int e, int t) { + for (int c = 0; c < r->n_channels; c++) if (r->eq[e].expo[t][r->n_vars + c]) return 1; + return 0; +} + +/* rival A: first non-channel coefficient of equation 1 (or 0) scaled by 1.1 */ +static void rival_scale(const pd0_rel *truth, pd0_rel *out) { + memcpy(out, truth, sizeof *out); + int e = truth->n_equations > 1 ? 1 : 0; + for (int t = 0; t < out->eq[e].n_terms; t++) + if (!is_channel_term(out, e, t)) { out->eq[e].coef[t] = (out->eq[e].coef[t] * 11) / 10; return; } +} + +/* rival B: last non-channel term of the last equation removed (if it leaves one term) */ +static int rival_missing(const pd0_rel *truth, pd0_rel *out) { + memcpy(out, truth, sizeof *out); + int e = truth->n_equations - 1; + for (int t = out->eq[e].n_terms - 1; t >= 0; t--) + if (!is_channel_term(out, e, t) && out->eq[e].n_terms > 1) { + for (int u = t; u + 1 < out->eq[e].n_terms; u++) { + out->eq[e].coef[u] = out->eq[e].coef[u + 1]; + memcpy(out->eq[e].expo[u], out->eq[e].expo[u + 1], PD0_MAX_VARS + PD0_MAX_CHAN); + } + out->eq[e].n_terms--; + out->description_bits = pd0_rel_bits(out); + return 1; + } + return 0; +} + +static void acc_add(acc *a, int n_obs, const int64_t *pred, const int64_t *obs) { + for (int i = 0; i < n_obs; i++) { + double d = (double)(pred[i] - obs[i]) / 1e6, o = (double)obs[i] / 1e6; + a->se[i] += d * d; a->ss[i] += o * o; a->mean[i] += o; + } + a->n++; +} + +static double acc_nrmse(const acc *a, int n_obs) { + double worst = 0; + if (a->n < 2) return 0; + for (int i = 0; i < n_obs; i++) { + double m = a->mean[i] / (double)a->n, var = a->ss[i] / (double)a->n - m * m; + double sd = var > 0 ? sqrt(var) : 0, rmse = sqrt(a->se[i] / (double)a->n); + double v = sd > 1e-9 ? rmse / sd : (rmse > 1e-9 ? 1e9 : 0); + if (v > worst) worst = v; + } + return worst; +} + +static void rollout(const pd0_rel *r, const pd0_exp *e, int n_ok, int64_t out[PD0_MAX_STEPS][PD0_MAX_OBS]) { + int64_t st[PD0_MAX_VARS], nx[PD0_MAX_VARS]; memset(st, 0, sizeof st); + for (int i = 0; i < e->n_obs; i++) st[i] = e->reset[i]; + for (int s = 0; s < n_ok; s++) { pd0_rel_step(r, st, e->steps[s].channel, e->steps[s].value, nx); for (int i = 0; i < e->n_obs; i++) out[s][i] = nx[i]; memcpy(st, nx, sizeof st); } +} + +/* strategy 0 = active planner, 1 = passive baseline */ +static void run_strategy(int level, uint64_t seed, int strategy, const pd0_rel *truth, const pd0_rel *rv[2], int n_rv, row *out) { + pb_bounds pb; pb_rel pbt; + pb_world *w = pb_open(level, seed, &pb, &pbt); + if (!w) { out->valid = 0; return; } + pd0p_bounds b; memset(&b, 0, sizeof b); + b.n_obs = pb.n_obs; b.n_channels = pb.n_channels; b.episode_max_steps = pb.episode_max_steps; + for (int i = 0; i < pb.n_channels; i++) { b.chan_min[i] = pb.chan_min[i]; b.chan_max[i] = pb.chan_max[i]; } + for (int i = 0; i < pb.n_obs; i++) { b.reset_min[i] = pb.reset_min[i]; b.reset_max[i] = pb.reset_max[i]; } + pd0p_domain dom; for (int i = 0; i < pb.n_obs; i++) { dom.var_min[i] = pb.reset_min[i]; dom.var_max[i] = pb.reset_max[i]; } + pd0p_hyps h; h.n_hyp = (uint8_t)(1 + n_rv); h.hyp[0] = truth; for (int k = 0; k < n_rv; k++) h.hyp[1 + k] = rv[k]; h.residual_sd_micro = RESID_SD_MICRO; + static pd0p_prior prior; memset(&prior, 0, sizeof prior); + acc a_truth, a_rv[2]; memset(&a_truth, 0, sizeof a_truth); memset(a_rv, 0, sizeof a_rv); + int refuted[2] = { 0, 0 }; int64_t cost = 0; int ep; + uint32_t budget = pb.budget_steps; + for (ep = 0; ep < EP_CAP; ep++) { + pd0_exp e; int st; + if (strategy == 0) { + static pd0p_proposal p; + st = pd0p_propose(&b, &dom, &h, budget, &prior, PLAN_SEED + (uint64_t)ep, &p); + if (st == PD0P_NO_DISCRIMINATING_EXPERIMENT) { out->no_disc = 1; break; } + if (st != PD0P_OK) { out->valid = 0; break; } + memcpy(&e, &p.exp, sizeof e); + } else { + if (pd0p_passive(&b, budget, PASSIVE_SEED, (uint32_t)ep, &e) != PD0P_OK) { out->valid = 0; break; } + } + if (pd0p_validate(&b, &prior, &e) != PD0P_OK) { out->valid = 0; break; } + pd0p_prior_add(&prior, e.schedule_hash); + pb_sched s; memset(&s, 0, sizeof s); s.n_obs = e.n_obs; s.n_steps = e.n_steps; + for (int i = 0; i < e.n_obs; i++) s.reset[i] = e.reset[i]; + for (int k = 0; k < e.n_steps; k++) { s.channel[k] = e.steps[k].channel; s.value[k] = e.steps[k].value; cost += e.steps[k].value < 0 ? -e.steps[k].value : e.steps[k].value; } + int64_t obs[PB_STEPS][PB_MAX_OBS]; memset(obs, 0, sizeof obs); + int n_ok = pb_run(w, &s, obs); + if (n_ok < 0) { out->valid = 0; break; } + budget = pb.budget_steps > pb_steps_used(w) ? pb.budget_steps - pb_steps_used(w) : 0; + int64_t pred[PD0_MAX_STEPS][PD0_MAX_OBS]; + rollout(truth, &e, n_ok, pred); for (int k = 0; k < n_ok; k++) acc_add(&a_truth, e.n_obs, pred[k], obs[k]); for (int r = 0; r < n_rv; r++) { rollout(rv[r], &e, n_ok, pred); for (int k = 0; k < n_ok; k++) acc_add(&a_rv[r], e.n_obs, pred[k], obs[k]); } + int all = 1; + for (int r = 0; r < n_rv; r++) { + if (!refuted[r] && acc_nrmse(&a_rv[r], e.n_obs) * 1e6 > EPS_PPM) { refuted[r] = 1; out->refuted_at[strategy][r] = ep + 1; } + if (!refuted[r]) all = 0; + } + if (all) { ep++; break; } + } + if (acc_nrmse(&a_truth, pb.n_obs) * 1e6 > EPS_PPM) out->valid = 0; + out->episodes[strategy] = ep; out->cost[strategy] = cost; + for (int r = 0; r < n_rv; r++) if (!refuted[r]) out->refuted_at[strategy][r] = 0; + pb_close(w); +} + +int main(int argc, char **argv) { + if (argc < 2) { fprintf(stderr, "usage: %s [commit]\n", argv[0]); return 2; } + const char *commit = argc > 2 ? argv[2] : "unknown"; + int fd = open(argv[1], O_WRONLY | O_CREAT | O_EXCL, 0644); + if (fd < 0) { fprintf(stderr, "receipt exists or cannot be created: %s (%s)\n", argv[1], strerror(errno)); return 2; } + FILE *f = fdopen(fd, "w"); + const int levels[3] = { 1, 2, 4 }; + fprintf(f, "{\n \"receipt\": \"PD0_PLANNER_EFFICIENCY_DEV\",\n \"claim\": \"PD-0 development evidence only; NOT an EXP-003 claim\",\n" + " \"commit\": \"%s\",\n \"eps_ppm\": %d,\n \"episode_cap\": %d,\n \"residual_sd_micro\": %d,\n \"recorder\": \"STAND_IN\",\n \"rows\": [\n", commit, EPS_PPM, EP_CAP, RESID_SD_MICRO); + int first = 1, active_wins = 0, ties = 0, passive_wins = 0, invalid = 0; + long long sum_act = 0, sum_pas = 0; + printf("level seed | active episodes (cost units) | passive episodes (cost units) | rival refuted at (active A,B / passive A,B)\n"); + for (int li = 0; li < 3; li++) for (uint64_t seed = 1; seed <= 5; seed++) { + pb_bounds pb; pb_rel pbt; pb_world *w = pb_open(levels[li], seed, &pb, &pbt); + if (!w) { fprintf(stderr, "cannot open L%d seed %llu\n", levels[li], (unsigned long long)seed); return 1; } + pb_close(w); + static pd0_rel truth, ra, rb; rel_from_pb(&pbt, &truth); rival_scale(&truth, &ra); + int n_rv = 1 + rival_missing(&truth, &rb); + const pd0_rel *rv[2] = { &ra, &rb }; + row r; memset(&r, 0, sizeof r); r.valid = 1; + run_strategy(levels[li], seed, 0, &truth, rv, n_rv, &r); + run_strategy(levels[li], seed, 1, &truth, rv, n_rv, &r); + printf("L%d %llu | %d (%.2f) | %d (%.2f) | %d,%d / %d,%d%s%s\n", levels[li], (unsigned long long)seed, + r.episodes[0], (double)r.cost[0] / 1e6, r.episodes[1], (double)r.cost[1] / 1e6, + r.refuted_at[0][0], r.refuted_at[0][1], r.refuted_at[1][0], r.refuted_at[1][1], + r.valid ? "" : " INVALID(truth outside eps)", r.no_disc ? " NO_DISCRIMINATING" : ""); + fprintf(f, "%s {\"level\": %d, \"seed\": %llu, \"active_episodes\": %d, \"active_cost_micro\": %lld, \"passive_episodes\": %d, \"passive_cost_micro\": %lld, " + "\"active_refuted_at\": [%d, %d], \"passive_refuted_at\": [%d, %d], \"valid\": %s, \"no_discriminating\": %s}", + first ? "" : ",\n", levels[li], (unsigned long long)seed, r.episodes[0], (long long)r.cost[0], r.episodes[1], (long long)r.cost[1], + r.refuted_at[0][0], r.refuted_at[0][1], r.refuted_at[1][0], r.refuted_at[1][1], r.valid ? "true" : "false", r.no_disc ? "true" : "false"); + first = 0; + if (!r.valid) invalid++; + else { sum_act += r.episodes[0]; sum_pas += r.episodes[1]; if (r.episodes[0] < r.episodes[1]) active_wins++; else if (r.episodes[0] == r.episodes[1]) ties++; else passive_wins++; } + } + fprintf(f, "\n ],\n \"summary\": {\"active_fewer\": %d, \"ties\": %d, \"passive_fewer\": %d, \"invalid\": %d, \"total_active_episodes\": %lld, \"total_passive_episodes\": %lld}\n}\n", + active_wins, ties, passive_wins, invalid, sum_act, sum_pas); + fclose(f); + printf("summary: active fewer %d, ties %d, passive fewer %d, invalid %d; total episodes active %lld vs passive %lld\n", active_wins, ties, passive_wins, invalid, sum_act, sum_pas); + printf("PD0_PLANNER_EFF: %s\n", invalid == 0 ? "PASS" : "FAIL"); + return invalid == 0 ? 0 : 1; +} diff --git a/tests/physics0/planner/pd0p_bridge.c b/tests/physics0/planner/pd0p_bridge.c new file mode 100644 index 000000000..c48ed73d7 --- /dev/null +++ b/tests/physics0/planner/pd0p_bridge.c @@ -0,0 +1,61 @@ +/* See pd0p_bridge.h. Substrate side: links the world and the generator. */ +#include +#include +#define PD0_WORLD_IMPL +#include "physics0/pd0_world.h" +#include "physics0/pd0_gen.h" +#include "physics0/pd0_relation.h" +#include "pd0p_bridge.h" + +struct pb_world { pd0_world w; }; + +pb_world *pb_open(int level, uint64_t seed, pb_bounds *b, pb_rel *truth) { + pb_world *p = calloc(1, sizeof *p); + if (!p) return NULL; + if (pd0_world_init(&p->w, level, seed) != 0) { free(p); return NULL; } + pd0_desc d; pd0_world_describe(&p->w, &d); + memset(b, 0, sizeof *b); + b->n_obs = d.n_obs; b->n_channels = d.n_channels; + for (int i = 0; i < d.n_channels; i++) { b->chan_min[i] = d.chan_min[i]; b->chan_max[i] = d.chan_max[i]; } + for (int i = 0; i < d.n_obs; i++) { b->reset_min[i] = d.reset_min[i]; b->reset_max[i] = d.reset_max[i]; } + b->episode_max_steps = d.episode_max_steps; b->budget_steps = d.budget_steps; b->budget_episodes = d.budget_episodes; + b->dt_micro = d.dt_micro; + pd0_relation r; + if (pd0_gen_true_relation(&p->w.gen, d.dt_micro, &r) != 0) { free(p); return NULL; } + memset(truth, 0, sizeof *truth); + truth->n_vars = r.n_vars; truth->n_latent = r.n_latent; truth->n_channels = r.n_channels; truth->n_eq = r.n_eq; + for (int e = 0; e < r.n_eq && e < PB_MAX_EQ; e++) { + truth->eq[e].target = r.eq[e].target; + truth->eq[e].n_terms = r.eq[e].n_terms; + for (int t = 0; t < r.eq[e].n_terms && t < PB_MAX_TERMS; t++) { + truth->eq[e].coef[t] = r.eq[e].t[t].coef; + /* substrate exponent layout: PD0_MAX_VARS variables then PD0_MAX_CH channels; + * verifier layout: 12 variables then 4 channels */ + for (int v = 0; v < r.n_vars && v < 12; v++) truth->eq[e].expo[t][v] = r.eq[e].t[t].ex[v]; + for (int c = 0; c < r.n_channels && c < 4; c++) truth->eq[e].expo[t][r.n_vars + c] = r.eq[e].t[t].ex[PD0_MAX_VARS + c]; /* verifier layout: channel c at index n_vars + c */ + } + } + return p; +} + +void pb_close(pb_world *w) { free(w); } + +int pb_run(pb_world *p, const pb_sched *s, int64_t after[PB_STEPS][PB_MAX_OBS]) { + pd0_rec r; + int64_t vals[PD0_MAX_OBS]; + for (int i = 0; i < s->n_obs && i < PD0_MAX_OBS; i++) vals[i] = s->reset[i]; + pd0_world_reset(&p->w, vals, s->n_obs, &r); + if (r.status != PD0_OK) return -1; + int ok = 0; + for (int st = 0; st < s->n_steps; st++) { + pd0_world_step(&p->w, s->channel[st], s->value[st], &r); + if (r.status == PD0_REFUSED_RANGE || r.status == PD0_BUDGET_EXHAUSTED) return ok > 0 ? ok : -1; + if (r.status == PD0_OUT_OF_BOUNDS) return ok; + for (int i = 0; i < s->n_obs && i < PD0_MAX_OBS; i++) after[st][i] = r.after[i]; + ok++; + if (r.status == PD0_EPISODE_END) break; + } + return ok; +} + +uint32_t pb_steps_used(const pb_world *p) { return p->w.steps_used; } diff --git a/tests/physics0/planner/pd0p_bridge.h b/tests/physics0/planner/pd0p_bridge.h new file mode 100644 index 000000000..e9721b346 --- /dev/null +++ b/tests/physics0/planner/pd0p_bridge.h @@ -0,0 +1,42 @@ +/* Test-only bridge between the planner (verifier formats, pd0_fmt.h) and the + * PD-0 world (substrate formats, physics0/pd0_wire.h). The two header families + * define different PD0_MAX_* limits, so they never meet in one translation + * unit; this header carries plain arrays only. The bridge links the generator + * on purpose (it is the HARNESS side of the efficiency comparison) and is never + * linked into the planner objects (purity check in mk/physics0-planner.mk). */ +#ifndef PD0P_BRIDGE_H +#define PD0P_BRIDGE_H +#include + +#define PB_MAX_OBS 8 +#define PB_MAX_CH 4 +#define PB_MAX_EX 16 /* exponent entries: n_vars variables then channels at index n_vars + c (verifier layout) */ +#define PB_MAX_TERMS 64 +#define PB_MAX_EQ 12 +#define PB_STEPS 20 + +typedef struct { + uint8_t n_obs, n_channels; + int64_t chan_min[PB_MAX_CH], chan_max[PB_MAX_CH]; + int64_t reset_min[PB_MAX_OBS], reset_max[PB_MAX_OBS]; + uint32_t episode_max_steps, budget_steps, budget_episodes; + int64_t dt_micro; +} pb_bounds; + +typedef struct { uint8_t target; uint16_t n_terms; int64_t coef[PB_MAX_TERMS]; uint8_t expo[PB_MAX_TERMS][PB_MAX_EX]; } pb_eq; +typedef struct { uint8_t n_vars, n_latent, n_channels, n_eq; pb_eq eq[PB_MAX_EQ]; } pb_rel; + +typedef struct { uint8_t n_obs, n_steps; int64_t reset[PB_MAX_OBS]; uint8_t channel[PB_STEPS]; int64_t value[PB_STEPS]; } pb_sched; + +typedef struct pb_world pb_world; /* opaque: holds the in-process world */ + +/* Open an in-process world for (level 1..6, seed); fills bounds and the true + * relation in the verifier's exponent layout. Returns NULL on error. */ +pb_world *pb_open(int level, uint64_t seed, pb_bounds *b, pb_rel *truth); +void pb_close(pb_world *w); +/* Run one schedule: reset then n_steps steps. after[s][i] is the observed + * state after step s. Returns the number of OK steps (fewer than n_steps when + * the episode ended early), or -1 on a refused reset / budget exhaustion. */ +int pb_run(pb_world *w, const pb_sched *s, int64_t after[PB_STEPS][PB_MAX_OBS]); +uint32_t pb_steps_used(const pb_world *w); +#endif diff --git a/tests/physics0/planner/test_pd0_planner.c b/tests/physics0/planner/test_pd0_planner.c new file mode 100644 index 000000000..11d32d374 --- /dev/null +++ b/tests/physics0/planner/test_pd0_planner.c @@ -0,0 +1,144 @@ +/* PD-0 planner contract tests (spec section 8 behaviours 1 to 6, gate G3). + * Uses only verifier formats and made-up relations; no generator is linked. */ +#include +#include +#include "pd0_planner.h" + +static int checks = 0, failed = 0; +#define CHECK(cond, msg) do { checks++; if (!(cond)) { failed++; printf("FAIL %s:%d %s\n", __FILE__, __LINE__, msg); } } while (0) + +/* a two-variable, one-channel relation in D form: Ds0 = a*s1, Ds1 = b*s0 + c*s1 + d*u */ +static void mk_rel(pd0_rel *r, int64_t a, int64_t b, int64_t c, int64_t d) { + memset(r, 0, sizeof *r); + r->n_vars = 2; r->n_latent = 0; r->n_channels = 1; r->n_equations = 2; + r->eq[0].target = 0; r->eq[0].n_terms = 1; r->eq[0].coef[0] = a; r->eq[0].expo[0][1] = 1; + r->eq[1].target = 1; r->eq[1].n_terms = 0; + if (b) { int t = r->eq[1].n_terms++; r->eq[1].coef[t] = b; r->eq[1].expo[t][0] = 1; } + if (c) { int t = r->eq[1].n_terms++; r->eq[1].coef[t] = c; r->eq[1].expo[t][1] = 1; } + if (d) { int t = r->eq[1].n_terms++; r->eq[1].coef[t] = d; r->eq[1].expo[t][2 + 0] = 1; /* channel 0 at index n_vars + 0 */ } + r->description_bits = pd0_rel_bits(r); +} + +static void mk_bounds(pd0p_bounds *b) { + memset(b, 0, sizeof *b); + b->n_obs = 2; b->n_channels = 1; + b->chan_min[0] = -2000000; b->chan_max[0] = 2000000; + for (int i = 0; i < 2; i++) { b->reset_min[i] = -2000000; b->reset_max[i] = 2000000; } + b->episode_max_steps = 100; +} + +static void mk_dom(pd0p_domain *d) { + memset(d, 0, sizeof *d); + for (int i = 0; i < 2; i++) { d->var_min[i] = -2000000; d->var_max[i] = 2000000; } +} + +int main(void) { + pd0p_bounds b; pd0p_domain dom; mk_bounds(&b); mk_dom(&dom); + static pd0_rel truth, r110, r150, missing, same; + mk_rel(&truth, 50000, -150000, -25000, 50000); /* dt 0.05, k 3, c 0.5 */ + mk_rel(&r110, 50000, -165000, -25000, 50000); /* k x 1.1 */ + mk_rel(&r150, 50000, -225000, -25000, 50000); /* k x 1.5 */ + mk_rel(&missing, 50000, -150000, 0, 50000); /* damping term missing */ + memcpy(&same, &truth, sizeof same); + static pd0p_prior prior; memset(&prior, 0, sizeof prior); + pd0p_hyps h = { 2, { &truth, &r110 }, 10000 }; + static pd0p_proposal p1, p2; + static uint8_t buf1[8192], buf2[8192], plan1[PD0P_PLAN_SIZE], plan2[PD0P_PLAN_SIZE]; + + /* behaviour 1: deterministic given inputs and seed */ + int s1 = pd0p_propose(&b, &dom, &h, 3000, &prior, 7, &p1); + int s2 = pd0p_propose(&b, &dom, &h, 3000, &prior, 7, &p2); + CHECK(s1 == PD0P_OK && s2 == PD0P_OK, "proposal OK"); + size_t n1 = pd0p_prereg_bytes(&p1, buf1, sizeof buf1), n2 = pd0p_prereg_bytes(&p2, buf2, sizeof buf2); + CHECK(n1 > 0 && n1 == n2 && memcmp(buf1, buf2, n1) == 0, "two runs byte-identical PD0EXP1"); + pd0p_plan_write(&p1, plan1); pd0p_plan_write(&p2, plan2); + CHECK(memcmp(plan1, plan2, PD0P_PLAN_SIZE) == 0, "two runs byte-identical PD0PLN1"); + CHECK(p1.n_candidates >= 256, "at least 256 candidates"); + CHECK(p1.exp.n_steps == 20, "20-step schedule"); + CHECK(p1.exp.n_hyp == 2, "expected trajectories for every hypothesis"); + + /* a different seed gives a different (still valid) schedule */ + static pd0p_proposal p3; + CHECK(pd0p_propose(&b, &dom, &h, 3000, &prior, 8, &p3) == PD0P_OK, "seed 8 OK"); + CHECK(memcmp(p3.exp.schedule_hash, p1.exp.schedule_hash, PD0_HASH) != 0, "seed changes the schedule"); + + /* PD0EXP1 round trip and the rev 5 schedule hash (requested values only) */ + pd0_exp back; size_t used = 0; + CHECK(pd0_exp_parse(buf1, n1, &back, &used) == 0 && used == n1, "PD0EXP1 parses"); + uint8_t hh[PD0_HASH]; + pd0_schedule_hash(back.n_obs, back.reset, back.n_steps, back.steps, hh); + CHECK(memcmp(hh, p1.exp.schedule_hash, PD0_HASH) == 0, "schedule hash recomputes from requested values"); + CHECK(pd0p_validate(&b, &prior, &p1.exp) == PD0P_OK, "proposal validates"); + pd0p_proposal pp; CHECK(pd0p_plan_parse(plan1, PD0P_PLAN_SIZE, &pp) == 0 && pp.divergence_micro == p1.divergence_micro, "PD0PLN1 parses"); + plan2[8] = 2; CHECK(pd0p_plan_parse(plan2, PD0P_PLAN_SIZE, &pp) != 0, "PD0PLN1 unknown version refused"); + + /* behaviour 2: out-of-range mutant is refused as a planner defect */ + pd0_exp mut; memcpy(&mut, &p1.exp, sizeof mut); + mut.steps[3].value = b.chan_max[0] + 1; + pd0_schedule_hash(mut.n_obs, mut.reset, mut.n_steps, mut.steps, mut.schedule_hash); + CHECK(pd0p_validate(&b, &prior, &mut) == PD0P_ERR_BOUNDS, "out-of-range step refused (BOUNDS)"); + memcpy(&mut, &p1.exp, sizeof mut); mut.reset[1] = b.reset_min[1] - 1; + pd0_schedule_hash(mut.n_obs, mut.reset, mut.n_steps, mut.steps, mut.schedule_hash); + CHECK(pd0p_validate(&b, &prior, &mut) == PD0P_ERR_BOUNDS, "out-of-box reset refused (BOUNDS)"); + memcpy(&mut, &p1.exp, sizeof mut); mut.steps[0].value += 1; /* stale hash */ + CHECK(pd0p_validate(&b, &prior, &mut) == PD0P_ERR_INPUT, "stale schedule hash refused (INPUT)"); + /* the planner itself never emits out-of-range values over many seeds */ + int in_bounds = 1; uint8_t flags[32]; + for (uint32_t s = 0; s < 32; s++) { + static pd0p_proposal q; + if (pd0p_propose(&b, &dom, &h, 3000, &prior, 100 + s, &q) != PD0P_OK || pd0p_validate(&b, &prior, &q.exp) != PD0P_OK) in_bounds = 0; + flags[s] = q.outside_middle; + } + CHECK(in_bounds, "32 seeds: every proposal within bounds and valid"); + uint32_t frac = pd0p_outside_fraction_ppm(flags, 32); + printf("behaviour 5 (provisional 20%% rule): %u ppm of proposals start or reach outside the middle 50%% of the visited range\n", frac); + CHECK(frac >= 200000, "behaviour 5: at least 20% outside the middle (provisional value)"); + + /* behaviour 3: never repeat a schedule hash */ + pd0p_prior_add(&prior, p1.exp.schedule_hash); + CHECK(pd0p_validate(&b, &prior, &p1.exp) == PD0P_ERR_DUPLICATE, "duplicate schedule refused (DUPLICATE)"); + static pd0p_proposal p4; + CHECK(pd0p_propose(&b, &dom, &h, 3000, &prior, 7, &p4) == PD0P_OK, "same seed with prior: still proposes"); + CHECK(memcmp(p4.exp.schedule_hash, p1.exp.schedule_hash, PD0_HASH) != 0, "same seed with prior: a different schedule"); + CHECK(p4.n_skipped_duplicate >= 1, "duplicate candidate was skipped, not emitted"); + + /* behaviour 4: identical hypotheses have no discriminating experiment */ + pd0p_hyps hs = { 2, { &truth, &same }, 10000 }; + static pd0p_proposal p5; + CHECK(pd0p_propose(&b, &dom, &hs, 3000, NULL, 7, &p5) == PD0P_NO_DISCRIMINATING_EXPERIMENT, "identical hypotheses: NO_DISCRIMINATING_EXPERIMENT"); + CHECK(p5.divergence_micro == 0, "D is zero for identical hypotheses"); + CHECK(pd0p_prereg_bytes(&p5, buf1, sizeof buf1) == 0, "no preregistration bytes without a discriminating experiment"); + + /* D monotone: a larger perturbation diverges more; a missing term is also found */ + pd0p_hyps h150 = { 2, { &truth, &r150 }, 10000 }; + pd0p_hyps hmiss = { 2, { &truth, &missing }, 10000 }; + static pd0p_proposal p6, p7; + CHECK(pd0p_propose(&b, &dom, &h150, 3000, NULL, 7, &p6) == PD0P_OK, "k x1.5 OK"); + CHECK(p6.divergence_micro > p1.divergence_micro, "D(k x1.5) > D(k x1.1)"); + CHECK(pd0p_propose(&b, &dom, &hmiss, 3000, NULL, 7, &p7) == PD0P_OK && p7.divergence_micro >= PD0P_MIN_D_MICRO, "missing-term rival is discriminable"); + /* three hypotheses at once */ + pd0p_hyps h3 = { 3, { &truth, &r110, &missing }, 10000 }; + static pd0p_proposal p8; + CHECK(pd0p_propose(&b, &dom, &h3, 3000, NULL, 7, &p8) == PD0P_OK && p8.exp.n_hyp == 3, "three hypotheses: proposal carries three trajectories"); + CHECK(p8.divergence_micro >= p1.divergence_micro, "D over three hypotheses is at least the pair's D"); + + /* budget and input errors */ + static pd0p_proposal p9; + CHECK(pd0p_propose(&b, &dom, &h, 1, NULL, 7, &p9) == PD0P_ERR_BUDGET, "budget below 2 steps refused"); + CHECK(pd0p_propose(&b, &dom, &h, 10, NULL, 7, &p9) == PD0P_OK && p9.exp.n_steps == 10, "budget shortens the schedule"); + pd0p_hyps h1 = { 1, { &truth }, 10000 }; + CHECK(pd0p_propose(&b, &dom, &h1, 3000, NULL, 7, &p9) == PD0P_ERR_INPUT, "one hypothesis refused (a rival is required)"); + pd0p_hyps h0 = { 2, { &truth, &r110 }, 0 }; + CHECK(pd0p_propose(&b, &dom, &h0, 3000, NULL, 7, &p9) == PD0P_ERR_INPUT, "zero residual sd refused"); + + /* passive baseline: deterministic, in bounds, distinct per index, no hypothesis consulted */ + pd0_exp pa, pb2, pc; + CHECK(pd0p_passive(&b, 3000, 99, 0, &pa) == PD0P_OK && pd0p_passive(&b, 3000, 99, 0, &pb2) == PD0P_OK, "passive OK"); + CHECK(memcmp(&pa, &pb2, sizeof pa) == 0, "passive deterministic"); + CHECK(pd0p_passive(&b, 3000, 99, 1, &pc) == PD0P_OK && memcmp(pc.schedule_hash, pa.schedule_hash, PD0_HASH) != 0, "passive index 1 differs"); + CHECK(pd0p_validate(&b, NULL, &pa) == PD0P_OK, "passive schedule within bounds"); + CHECK(pa.n_hyp == 0, "passive carries no hypothesis trajectory"); + + printf("test_pd0_planner: %d checks, %d failed\n", checks, failed); + return failed ? 1 : 0; +} ```