diff --git a/.crumb b/.crumb index b554f014a..492355b59 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "fc91a572765dd6ead13c4f19163002d68e6d5ab05c3189acb129b8dc9ee3ab01", - "last_commit": "9da2f29", + "digest": "1e31dff42b3f696c641f95733d670af27b72f77bd0e1708d42e3a879e9e45ffc", + "last_commit": "403ef98", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "687df5bb63472b726bad6b98195cf7c91351a0b6f3302eed7fd5bb4e32aea461", - "last_commit": "9da2f29", + "digest": "4bd9775399bb986340328ed1b843506e06ba0cd3126269518e85669c3b79417f", + "last_commit": "403ef98", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "83e09945d541d8d548d39994ac79e3b56b7206929010e0f6a305fe3482f698f0", - "last_commit": "9da2f29", + "digest": "4507760d5412654a2c14457a0a900a76a54dd99729acb54d764925bc6dad4d2b", + "last_commit": "403ef98", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "fd40504451c18f359577acd98497bf1176124097c8942f40177262344f7fc8b1", - "last_commit": "9da2f29", + "digest": "3cf64120d367b0ec3b8cb61415ddc3ba7f8b42e04c79d3a0e958b7eb8eef1622", + "last_commit": "403ef98", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:1ebdb3c971fdb24ecbc11bcb2b132e2e15e5200ec42de2548e05cc8d180b134f", - "compiled_at": "2026-10-04T18:24:05Z", + "children_root": "sha256:53c927616046363118eba9033e24e3cf283edad7f5369da568c4bbe306afbfbd", + "compiled_at": "2026-10-04T18:24:30Z", "compiler": "crumb-compile/1", - "digest": "sha256:80473bbaf025d5ee335196e9087fb77de1810638b97b1c46859ae0557eb2647d", - "evidence_files": 1282, - "evidence_root": "sha256:21a5bd5d54cd2816f3786602a347714cbfbde532cc58b2c56d5b1d20bae74cfa", - "files": 3016, - "generated_at_commit": "9da2f29c55cc3e7e632b0096a0eff2ec4211e2a8", - "source_tree": "sha256:30543f2a9c89f1a5b7ec1e0c87daeebd25f6e26e0d117cc8fea02b4c418450b4" + "digest": "sha256:70665ffd4eae02082919ea7e35a2a9d98f763259a987b743e70c4b4fe20b75aa", + "evidence_files": 1283, + "evidence_root": "sha256:faaf0962528d6ab359bd4702bafe3a263a8c79ab6fc8802c0ea45ed0390c8e05", + "files": 3026, + "generated_at_commit": "403ef98a510f61d77f77c2033ecc31de82d2fc85", + "source_tree": "sha256:ef319b183d3499cb4129cf5a2a05c4c60abfe1dcf0e7d3c16a5c287775f38d90" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 054d03e7c..b676081e1 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -112,14 +112,14 @@ } ], "children_root": "sha256:c250a1e7a18e576884d3bf007849954a7adbe0b0ab8d6e261303c62e67d8c34c", - "compiled_at": "2026-10-04T18:16:20Z", + "compiled_at": "2026-10-04T18:16:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:fc91a572765dd6ead13c4f19163002d68e6d5ab05c3189acb129b8dc9ee3ab01", + "digest": "sha256:1e31dff42b3f696c641f95733d670af27b72f77bd0e1708d42e3a879e9e45ffc", "evidence_files": 26, "evidence_root": "sha256:f58236184eadc5f8c8f95499127a9f43c2f4dc702ddca267853d8c3dee0690f1", "files": 99, - "generated_at_commit": "f056b77f64ee94a80f4ed2899b5f9495aa3ef840", - "source_tree": "sha256:495b6176cf7f80d521c0298dc621218b839479564719312f2de4c89f2fc9373b" + "generated_at_commit": "41dcb768a955c1027d60d8b86d4ff6629118b9e6", + "source_tree": "sha256:4ed995ad9f9645169f2fbf1bbf513db9e0352b200eebfd674ad520d412805289" }, "owner_lane": "", "provenance": { diff --git a/docs/dual/DUAL_RECORDS.md b/docs/dual/DUAL_RECORDS.md index d6d8e9732..06dd4ad92 100644 --- a/docs/dual/DUAL_RECORDS.md +++ b/docs/dual/DUAL_RECORDS.md @@ -29,5 +29,4 @@ truncation and trailing bytes, then run the same validation as the constructor. - Recommendation: `authority` must be `NONE`; indices in range; consequences in ascending resource order; `error` must equal the recomputed `(measured - predicted) / predicted_sd`; an unmeasured row carries zero. - Controller: eta >= 0, rho in [0,1], k_sigma >= 0, max_age >= 1, cadence >= 1, per-resource lambda_max > 0, ascending ids. Any parameter change changes `controller_id`. -Status of this slice: DUAL-0a IMPLEMENTED (records, registry, encoding, digests). `DUAL_CONSTRAINT_CONTRACT` is not yet claimed: -it needs DUAL-0b (binding to ESTIMATION records, staleness). +Status: DUAL-0a, DUAL-0b (rx_dual_bind.h: binding to est_* records, evidence verification, FRESH/STALE/FROZEN/UNCALIBRATED/REFUSED) and DUAL-1a (rx_dual_update.h: reference update) IMPLEMENTED; `DUAL_CONSTRAINT_CONTRACT = PASS` on host (evidence/DUAL/0b-1a-binding-controller); `DUAL_PRICE_REFERENCE` waits for DUAL-1b replay. diff --git a/evidence/DUAL/0b-1a-binding-controller/receipt.md b/evidence/DUAL/0b-1a-binding-controller/receipt.md new file mode 100644 index 000000000..5e8db9540 --- /dev/null +++ b/evidence/DUAL/0b-1a-binding-controller/receipt.md @@ -0,0 +1,40 @@ +# DUAL-0b + DUAL-1a receipt: ESTIMATION binding, staleness, reference controller (ADR 0031 s4.2, s5) + +| Field | Value | +|---|---| +| repository | aien-dev/omega | +| base commit | f056b77f64ee94a80f4ed2899b5f9495aa3ef840 (branch dual/0a-records, PR #266, on top of main cb7cc216147664549484fcca8e8ceadbd6f6cdbf) | +| candidate commit | this commit | +| compiler | cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 | +| flags | EST_CFLAGS (-std=c11 -Wall -Wextra -Werror -pedantic -O2 -ffp-contract=off -fno-fast-math) and EST_ASAN | +| host | Spark aarch64 Linux, host-only, no GPU | +| controller schema | RxDualController (eta, rho, k_sigma, max_age, cadence, per-resource lambda_max), digest domain omega.dual.controller.v1 = controller_id | + +## Commands and results +| Command | Result | +|---|---| +| make dual-purity | PASS (rx_dual.o, rx_dual_bind.o, rx_dual_update.o: no authority/promotion/process symbol) | +| make test-dual-types | 14107 / 1667 (ASan) checks, 0 failures | +| make test-dual-bind | 85 / 85 checks, 0 failures | +| make test-dual-update | 355348 / 35837 (ASan) checks, 0 failures | + +## What is proven +- Binding: estimate and uncertainty are copied from est_belief / est_prediction / est_observation and the record digest is cited; a prediction must cite the exact model (unit source) and prior belief (evidence source) by digest; a declared prior (zero evidence root) is refused as evidence; est units map by one explicit table, NONE/unknown refused. +- Evidence: a belief's root must equal est_evidence_root_extend(parent_root, digest(obs)); broken root, wrong observation, wrong parent root and an input not bound from the belief are all refused. The DUAL state root chains estimate_ref digests with the same primitive. +- Classification: REFUSED (NaN/Inf/negative uncertainty/missing digests/future generation), STALE (age > max_age, older than prev, unverified evidence), FROZEN (regime signal), UNCALIBRATED (no EST-3 receipt), FRESH; unit mismatch is a structural refusal (no conversion). +- Controller: analytic cases match the independent closed form (tests/dual/dual_ref.c, derived on paper from s5.1) to 1e-9: single binding constraint, slack constraint decaying to 0, two competing constraints, inside-deadband no movement, clip saturation, zero eta no movement, rho>0 geometric forgetting; byte-identical records for identical inputs. Property suite (3000 rounds x 39 ticks): lambda finite, 0 <= lambda <= lambda_max, held under STALE, equals clipped recursion otherwise. Held states keep lambda, estimate, generation and evidence root; parent/tick advance. Parameter mutation (controller_id mismatch), unknown resource, unit/id mismatch, zero/negative scale, INVARIANT class, lambda above max, malformed parent, overflow to Inf are refused with no record. + +## Negative controls (mutants, same suites) +| Mutant | Result | +|---|---| +| remove lambda_max clipping | test_dual_update 35634 failures: FAIL (killed) | +| remove deadband (g always = pressure) | test_dual_update 1052 failures: FAIL (killed) | +| remove controller_id check | test_dual_update 1 failure: FAIL (killed) | +| unverified evidence no longer STALE | test_dual_bind 1 failure: FAIL (killed) | +| evidence root comparison removed | test_dual_bind 4 failures: FAIL (killed) | + +## Verdict +DUAL-0b IMPLEMENTED. DUAL-1a IMPLEMENTED. DUAL_CONSTRAINT_CONTRACT: PASS on host for the record/binding contract (typed records, +canonical round trip, kind bytes and digest domains, unit registry, class rules, staleness detection, property tests, zero runtime +linkage). DUAL_PRICE_REFERENCE: NOT YET CLAIMED (needs DUAL-1b replay on digest-bound traces with the oscillation bound). +No production influence. EST-3 remains FAILED on main, so no price may control anything. diff --git a/mk/.crumb b/mk/.crumb index 9b7e664bc..a659d4fd8 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T18:16:20Z", + "compiled_at": "2026-10-04T18:16:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:687df5bb63472b726bad6b98195cf7c91351a0b6f3302eed7fd5bb4e32aea461", + "digest": "sha256:4bd9775399bb986340328ed1b843506e06ba0cd3126269518e85669c3b79417f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 50, - "generated_at_commit": "f056b77f64ee94a80f4ed2899b5f9495aa3ef840", - "source_tree": "sha256:03e843f9bcbdc5712b6cc737db5c7e4c91152e5036653073ed72a5390648e379" + "generated_at_commit": "41dcb768a955c1027d60d8b86d4ff6629118b9e6", + "source_tree": "sha256:078bf208413c507fba44fb779fa801998ee5f961aae7cb0575d3957c1eaf5ca8" }, "owner_lane": "", "provenance": { diff --git a/mk/dual.mk b/mk/dual.mk index 961e5d4fd..7f73d0c2c 100644 --- a/mk/dual.mk +++ b/mk/dual.mk @@ -10,11 +10,12 @@ ifndef DUAL_MK DUAL_MK := 1 include mk/estimation.mk -.PHONY: test-dual-types dual-purity test-dual +.PHONY: test-dual-types test-dual-bind test-dual-update dual-purity test-dual DUAL_CFLAGS = $(EST_CFLAGS) -Isrc/dual -Itests/dual DUAL_DIR = $(OUT_DIR)/tests-dual DUAL_CORE_SRCS = src/dual/rx_dual.c src/sha256.c -DUAL_HDRS = src/dual/rx_dual.h +DUAL_SRCS = src/dual/rx_dual.c src/dual/rx_dual_bind.c src/dual/rx_dual_update.c $(EST_TYPES_SRCS) +DUAL_HDRS = src/dual/rx_dual.h src/dual/rx_dual_bind.h src/dual/rx_dual_update.h $(EST_HDRS) # A price is information, not authority: no capability, promotion, generation # administration, World, ARGUS, AEGIS, memory-mapping or process symbol. DUAL_FORBIDDEN = 'aienos_|rx_gen_|rx_cap|rx_world|rx_js|argus_|forge_|aegis|mmap|mprotect|fork|exec|dlopen|system|socket|fopen|open$$' @@ -23,7 +24,7 @@ $(DUAL_DIR)/%.o: src/dual/%.c $(DUAL_HDRS) @mkdir -p $(DUAL_DIR) $(CC) $(DUAL_CFLAGS) -c -o $@ $< -dual-purity: $(DUAL_DIR)/rx_dual.o +dual-purity: $(DUAL_DIR)/rx_dual.o $(DUAL_DIR)/rx_dual_bind.o $(DUAL_DIR)/rx_dual_update.o @if nm -u $^ | grep -E $(DUAL_FORBIDDEN) ; then \ echo "dual object references an operation a pricing module must not have"; exit 1; fi @echo "dual-purity: $(notdir $^) reference no authority, promotion or process symbol" @@ -40,6 +41,33 @@ test-dual-types: $(DUAL_DIR)/test_dual_types $(DUAL_DIR)/test_dual_types_asan ./$(DUAL_DIR)/test_dual_types_asan @echo "test-dual-types: DUAL-0a contract checks pass in plain and ASan/UBSan builds" -test-dual: dual-purity test-dual-types +test-dual: dual-purity test-dual-types test-dual-bind test-dual-update @echo "test-dual: DUAL standalone module passes" endif + +# DUAL-0b binding and DUAL-1a controller (appended; same fragment) +ifndef DUAL_MK_2 +DUAL_MK_2 := 1 +$(DUAL_DIR)/test_dual_bind: tests/dual/test_dual_bind.c tests/dual/dual_fixtures.h $(DUAL_SRCS) $(DUAL_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) -o $@ tests/dual/test_dual_bind.c $(DUAL_SRCS) -lm +$(DUAL_DIR)/test_dual_bind_asan: tests/dual/test_dual_bind.c tests/dual/dual_fixtures.h $(DUAL_SRCS) $(DUAL_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) $(EST_ASAN) -o $@ tests/dual/test_dual_bind.c $(DUAL_SRCS) -lm +test-dual-bind: $(DUAL_DIR)/test_dual_bind $(DUAL_DIR)/test_dual_bind_asan + ./$(DUAL_DIR)/test_dual_bind + ./$(DUAL_DIR)/test_dual_bind_asan + @echo "test-dual-bind: DUAL-0b binding and staleness checks pass in plain and ASan/UBSan builds" + +DUAL_REF_SRCS = tests/dual/dual_ref.c +$(DUAL_DIR)/test_dual_update: tests/dual/test_dual_update.c tests/dual/dual_fixtures.h tests/dual/dual_ref.h $(DUAL_REF_SRCS) $(DUAL_SRCS) $(DUAL_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) -o $@ tests/dual/test_dual_update.c $(DUAL_REF_SRCS) $(DUAL_SRCS) -lm +$(DUAL_DIR)/test_dual_update_asan: tests/dual/test_dual_update.c tests/dual/dual_fixtures.h tests/dual/dual_ref.h $(DUAL_REF_SRCS) $(DUAL_SRCS) $(DUAL_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) $(EST_ASAN) -DPROP_ROUNDS=300 -o $@ tests/dual/test_dual_update.c $(DUAL_REF_SRCS) $(DUAL_SRCS) -lm +test-dual-update: $(DUAL_DIR)/test_dual_update $(DUAL_DIR)/test_dual_update_asan + ./$(DUAL_DIR)/test_dual_update + ./$(DUAL_DIR)/test_dual_update_asan + @echo "test-dual-update: DUAL-1a reference update passes analytic, property and refusal checks in plain and ASan/UBSan builds" +endif diff --git a/src/.crumb b/src/.crumb index de0a878ad..56d340d21 100644 --- a/src/.crumb +++ b/src/.crumb @@ -164,14 +164,14 @@ } ], "children_root": "sha256:342b19861dd2ee471546469278de0fa357eed3720995ee57366bbaaf75cf4f49", - "compiled_at": "2026-10-04T18:16:20Z", + "compiled_at": "2026-10-04T18:16:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:83e09945d541d8d548d39994ac79e3b56b7206929010e0f6a305fe3482f698f0", + "digest": "sha256:4507760d5412654a2c14457a0a900a76a54dd99729acb54d764925bc6dad4d2b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 392, - "generated_at_commit": "f056b77f64ee94a80f4ed2899b5f9495aa3ef840", - "source_tree": "sha256:fa083004eed5ae74d8132434eccc310aad69747aa4b79398fa0dd549ca38966f" + "files": 396, + "generated_at_commit": "41dcb768a955c1027d60d8b86d4ff6629118b9e6", + "source_tree": "sha256:ae9262b9e9d06668b482e95027148f7c5df9a94c6a276adaf19e02588f09c29e" }, "owner_lane": "", "provenance": { diff --git a/src/dual/rx_dual_bind.c b/src/dual/rx_dual_bind.c new file mode 100644 index 000000000..b3a5fd810 --- /dev/null +++ b/src/dual/rx_dual_bind.c @@ -0,0 +1,168 @@ +/* DUAL-0b: binding to ESTIMATION records and freshness classification. */ +#include "rx_dual_bind.h" + +#include +#include + +static void cp(RxDualDigest *d, const est_digest *e) { memcpy(d->b, e->b, RX_DUAL_DIGEST_SIZE); } + +RxDualStatus rx_dual_unit_from_est(est_unit u, RxDualUnit *out) +{ + if (!out) return RX_DUAL_ERR_NULL; + switch (u) { + case EST_UNIT_MILLI_CELSIUS: *out = RX_DUAL_UNIT_MILLI_CELSIUS; return RX_DUAL_OK; + case EST_UNIT_MILLI_CELSIUS_PER_S: *out = RX_DUAL_UNIT_MILLI_CELSIUS_PER_S; return RX_DUAL_OK; + case EST_UNIT_WATT: *out = RX_DUAL_UNIT_WATT; return RX_DUAL_OK; + case EST_UNIT_WATT_PER_S: *out = RX_DUAL_UNIT_WATT_PER_S; return RX_DUAL_OK; + case EST_UNIT_NANOSECOND: *out = RX_DUAL_UNIT_NS; return RX_DUAL_OK; + case EST_UNIT_BYTE: *out = RX_DUAL_UNIT_BYTES; return RX_DUAL_OK; + case EST_UNIT_DIMENSIONLESS: *out = RX_DUAL_UNIT_DIMENSIONLESS; return RX_DUAL_OK; + case EST_UNIT_LOG2_PICOSECOND: *out = RX_DUAL_UNIT_LOG2_PS; return RX_DUAL_OK; + case EST_UNIT_NONE: + case EST_UNIT_MAX_: + default: + *out = RX_DUAL_UNIT_NONE; + return RX_DUAL_ERR_UNIT; + } +} + +static RxDualStatus sd_from_var(double var, double *sd) +{ + if (!isfinite(var)) return RX_DUAL_ERR_NONFINITE; + if (var < 0.0) return RX_DUAL_ERR_RANGE; + *sd = sqrt(var); + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_bind_belief(const est_belief *b, uint32_t k, const RxDualDigest *calibration_ref, + RxDualInput *out) +{ + est_digest d; + RxDualStatus st; + if (!b || !out) return RX_DUAL_ERR_NULL; + memset(out, 0, sizeof *out); + if (est_check_belief(b) != EST_OK) return RX_DUAL_ERR_KIND; + if (k >= b->n) return RX_DUAL_ERR_RANGE; + if (est_digest_is_zero(&b->evidence_root)) return RX_DUAL_ERR_EVIDENCE; /* a prior is not evidence */ + if ((st = rx_dual_unit_from_est(b->unit[k], &out->unit)) != RX_DUAL_OK) return st; + if (est_digest_belief(b, &d) != EST_OK) return RX_DUAL_ERR_KIND; + cp(&out->estimate_ref, &d); + out->estimate_kind = RX_DUAL_EST_ESTIMATED; + out->estimate = b->x[k]; + if ((st = sd_from_var(b->P[k * b->n + k], &out->uncertainty)) != RX_DUAL_OK) return st; + out->generation = b->generation; + cp(&out->evidence_root, &b->evidence_root); + if (calibration_ref) out->calibration_ref = *calibration_ref; + return rx_dual_check_input(out); +} + +RxDualStatus rx_dual_bind_prediction(const est_prediction *p, const est_model *model, const est_belief *prior, + uint32_t k, const RxDualDigest *calibration_ref, RxDualInput *out) +{ + est_digest d; + RxDualStatus st; + if (!p || !model || !prior || !out) return RX_DUAL_ERR_NULL; + memset(out, 0, sizeof *out); + if (est_check_prediction(p) != EST_OK || est_check_model(model) != EST_OK || est_check_belief(prior) != EST_OK) + return RX_DUAL_ERR_KIND; + if (k >= p->n || k >= model->n) return RX_DUAL_ERR_RANGE; + if (est_digest_model(model, &d) != EST_OK || memcmp(d.b, p->model.b, EST_DIGEST_SIZE) != 0) return RX_DUAL_ERR_DIGEST; + if (est_digest_belief(prior, &d) != EST_OK || memcmp(d.b, p->prior.b, EST_DIGEST_SIZE) != 0) return RX_DUAL_ERR_DIGEST; + if (est_digest_is_zero(&prior->evidence_root)) return RX_DUAL_ERR_EVIDENCE; + if ((st = rx_dual_unit_from_est(model->state_unit[k], &out->unit)) != RX_DUAL_OK) return st; + if (est_digest_prediction(p, &d) != EST_OK) return RX_DUAL_ERR_KIND; + cp(&out->estimate_ref, &d); + out->estimate_kind = RX_DUAL_EST_PREDICTED; + out->estimate = p->x[k]; + if ((st = sd_from_var(p->P[k * p->n + k], &out->uncertainty)) != RX_DUAL_OK) return st; + out->generation = p->generation; + cp(&out->evidence_root, &prior->evidence_root); + if (calibration_ref) out->calibration_ref = *calibration_ref; + return rx_dual_check_input(out); +} + +RxDualStatus rx_dual_bind_observation(const est_observation *o, uint32_t k, uint64_t generation, + const RxDualDigest *calibration_ref, RxDualInput *out) +{ + est_digest d; + RxDualStatus st; + if (!o || !out) return RX_DUAL_ERR_NULL; + memset(out, 0, sizeof *out); + if (est_check_observation(o) != EST_OK) return RX_DUAL_ERR_KIND; + if (k >= o->m) return RX_DUAL_ERR_RANGE; + if ((st = rx_dual_unit_from_est(o->unit[k], &out->unit)) != RX_DUAL_OK) return st; + if (est_digest_observation(o, &d) != EST_OK) return RX_DUAL_ERR_KIND; + cp(&out->estimate_ref, &d); + cp(&out->observation_ref, &d); + out->estimate_kind = RX_DUAL_EST_MEASURED; + out->estimate = o->z[k]; + if ((st = sd_from_var(o->R[k * o->m + k], &out->uncertainty)) != RX_DUAL_OK) return st; + out->generation = generation; + cp(&out->evidence_root, &o->evidence); /* the raw-bytes digest the sensor path declared */ + out->evidence_verified = 1; /* an observation is its own evidence (ARCH-0020 caller rule) */ + if (calibration_ref) out->calibration_ref = *calibration_ref; + return rx_dual_check_input(out); +} + +RxDualStatus rx_dual_verify_belief_evidence(const est_belief *b, const est_digest *parent_root, + const est_observation *obs, RxDualInput *in) +{ + est_digest obs_d, want, have; + if (!b || !parent_root || !obs || !in) return RX_DUAL_ERR_NULL; + if (est_check_belief(b) != EST_OK || est_check_observation(obs) != EST_OK) return RX_DUAL_ERR_KIND; + if (est_digest_belief(b, &have) != EST_OK) return RX_DUAL_ERR_KIND; + if (memcmp(have.b, in->estimate_ref.b, EST_DIGEST_SIZE) != 0) return RX_DUAL_ERR_DIGEST; /* input not bound from b */ + if (est_digest_observation(obs, &obs_d) != EST_OK) return RX_DUAL_ERR_KIND; + if (est_evidence_root_extend(parent_root, &obs_d, &want) != EST_OK) return RX_DUAL_ERR_EVIDENCE; + if (memcmp(want.b, b->evidence_root.b, EST_DIGEST_SIZE) != 0) { in->evidence_verified = 0; return RX_DUAL_ERR_EVIDENCE; } + in->evidence_verified = 1; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_check_input(const RxDualInput *in) +{ + if (!in) return RX_DUAL_ERR_NULL; + if (!rx_dual_unit_valid(in->unit)) return RX_DUAL_ERR_UNIT; + if ((uint32_t)in->estimate_kind < 1u || (uint32_t)in->estimate_kind >= (uint32_t)RX_DUAL_EST_MAX_) return RX_DUAL_ERR_KIND; + if (!isfinite(in->estimate) || !isfinite(in->uncertainty)) return RX_DUAL_ERR_NONFINITE; + if (in->uncertainty < 0.0) return RX_DUAL_ERR_RANGE; + if (rx_dual_digest_is_zero(&in->estimate_ref) || rx_dual_digest_is_zero(&in->evidence_root)) return RX_DUAL_ERR_DIGEST; + if (in->estimate_kind == RX_DUAL_EST_MEASURED && rx_dual_digest_is_zero(&in->observation_ref)) return RX_DUAL_ERR_DIGEST; + if (in->evidence_verified > 1u || in->regime_change > 1u) return RX_DUAL_ERR_RANGE; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_classify_input(const RxDualInput *in, const RxDualConstraintState *prev, + const RxDualController *ctl, uint64_t now_generation, + RxDualLambdaState *out) +{ + RxDualStatus st; + if (!in || !ctl || !out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_controller(ctl)) != RX_DUAL_OK) return st; + if (prev) { + if ((st = rx_dual_check_constraint(prev)) != RX_DUAL_OK) return st; + if (in->unit != prev->unit) return RX_DUAL_ERR_UNIT; /* structural: never converted */ + } + /* value problems: the input is refused for this tick */ + if (rx_dual_check_input(in) != RX_DUAL_OK) { *out = RX_DUAL_LAMBDA_REFUSED; return RX_DUAL_OK; } + if (in->generation > now_generation) { *out = RX_DUAL_LAMBDA_REFUSED; return RX_DUAL_OK; } /* future generation */ + /* staleness */ + if (now_generation - in->generation > ctl->max_age) { *out = RX_DUAL_LAMBDA_STALE; return RX_DUAL_OK; } + if (prev && in->generation < prev->generation) { *out = RX_DUAL_LAMBDA_STALE; return RX_DUAL_OK; } + if (!in->evidence_verified) { *out = RX_DUAL_LAMBDA_STALE; return RX_DUAL_OK; } + if (in->regime_change) { *out = RX_DUAL_LAMBDA_FROZEN; return RX_DUAL_OK; } + if (rx_dual_digest_is_zero(&in->calibration_ref)) { *out = RX_DUAL_LAMBDA_UNCALIBRATED; return RX_DUAL_OK; } + *out = RX_DUAL_LAMBDA_FRESH; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_evidence_extend(const RxDualDigest *root, const RxDualDigest *estimate_ref, RxDualDigest *out) +{ + est_digest r, e, o; + if (!root || !estimate_ref || !out) return RX_DUAL_ERR_NULL; + if (rx_dual_digest_is_zero(estimate_ref)) return RX_DUAL_ERR_DIGEST; + memcpy(r.b, root->b, 32); memcpy(e.b, estimate_ref->b, 32); + if (est_evidence_root_extend(&r, &e, &o) != EST_OK) return RX_DUAL_ERR_EVIDENCE; + memcpy(out->b, o.b, 32); + return RX_DUAL_OK; +} diff --git a/src/dual/rx_dual_bind.h b/src/dual/rx_dual_bind.h new file mode 100644 index 000000000..4cf19df31 --- /dev/null +++ b/src/dual/rx_dual_bind.h @@ -0,0 +1,81 @@ +/* DUAL-0b: binding DUAL inputs to ESTIMATION records (ARCH-0020) and + * classifying their freshness (ADR 0031 sections 4.2, 5.2, 8.1). + * + * DUAL never reads a raw counter and never computes an estimate. Every + * estimate and uncertainty in a DUAL record is COPIED from an est_* record + * whose digest is cited (estimate_ref), together with that record's World + * generation and evidence root. The dependency direction is fixed: + * + * exact counters -> est_observation -> ESTIMATION -> est_belief / est_prediction + * -> RxDualInput (this file) -> controller (rx_dual_update.h) + * + * Units are converted from est_unit to RxDualUnit by one explicit table + * (rx_dual_unit_from_est); an unknown or NONE unit is refused. + * + * Evidence verification reuses est_evidence_root_extend: a belief's root must + * equal extend(parent_root, digest(observation)); only then is the input + * marked evidence_verified. An unverified input can only yield STALE. */ +#ifndef OMEGA_RX_DUAL_BIND_H +#define OMEGA_RX_DUAL_BIND_H + +#include "rx_dual.h" +#include "est_types.h" + +/* What the controller may read from ESTIMATION for one resource at one tick. */ +typedef struct { + RxDualDigest estimate_ref; /* digest of the est record the values come from */ + RxDualDigest observation_ref; /* digest of the observation (MEASURED), else zero */ + RxDualEstimateKind estimate_kind; + RxDualUnit unit; /* converted from the record's est_unit */ + double estimate; /* copied component value */ + double uncertainty; /* sqrt of the component variance, copied */ + uint64_t generation; /* World generation of the record */ + RxDualDigest evidence_root; /* the record's evidence root (non-zero) */ + uint32_t evidence_verified; /* 1 only after rx_dual_verify_belief_evidence passed */ + RxDualDigest calibration_ref; /* EST-3 receipt digest for this signal; zero = UNCALIBRATED */ + uint32_t regime_change; /* 1 = out-of-calibration (EST-9 style) signal: freeze */ +} RxDualInput; + +RxDualStatus rx_dual_unit_from_est(est_unit u, RxDualUnit *out); + +/* Bind component `k` of a belief. calibration_ref may be NULL (UNCALIBRATED). + * Refuses an invalid belief, k >= n, a zero evidence root (a declared prior + * is not evidence), or a negative variance. evidence_verified starts at 0. */ +RxDualStatus rx_dual_bind_belief(const est_belief *b, uint32_t k, const RxDualDigest *calibration_ref, + RxDualInput *out); +/* Bind component `k` of a prediction. The model (for the unit) and the prior + * belief (for the evidence root) must be the records the prediction cites: + * their digests must equal p->model and p->prior, else RX_DUAL_ERR_DIGEST. */ +RxDualStatus rx_dual_bind_prediction(const est_prediction *p, const est_model *model, const est_belief *prior, + uint32_t k, const RxDualDigest *calibration_ref, RxDualInput *out); +/* Bind component `k` of an observation as a MEASURED input. An observation + * carries no World generation, so the caller states it. */ +RxDualStatus rx_dual_bind_observation(const est_observation *o, uint32_t k, uint64_t generation, + const RxDualDigest *calibration_ref, RxDualInput *out); + +/* Verify a belief's evidence root against the parent root and the observation + * that produced it (root' = extend(parent_root, digest(obs))). On success sets + * in->evidence_verified = 1; on mismatch returns RX_DUAL_ERR_EVIDENCE and + * leaves it 0. `in` must have been bound from `b` (estimate_ref must match). */ +RxDualStatus rx_dual_verify_belief_evidence(const est_belief *b, const est_digest *parent_root, + const est_observation *obs, RxDualInput *in); + +/* Structural validity of an input (finite, unit known, kind known, digests present). */ +RxDualStatus rx_dual_check_input(const RxDualInput *in); + +/* Classify an input for the next tick (ADR 0031 section 5.2): + * REFUSED nonfinite or negative values, missing digests, generation in the future + * STALE now - generation > max_age, generation older than prev, or evidence not verified + * FROZEN regime_change set + * UNCALIBRATED no calibration_ref + * FRESH otherwise + * Unit mismatch against prev is a structural refusal (RX_DUAL_ERR_UNIT), not a state. + * prev may be NULL for an initial state. */ +RxDualStatus rx_dual_classify_input(const RxDualInput *in, const RxDualConstraintState *prev, + const RxDualController *ctl, uint64_t now_generation, + RxDualLambdaState *out); + +/* DUAL state evidence chain: root' = est_evidence_root_extend(root, estimate_ref). */ +RxDualStatus rx_dual_evidence_extend(const RxDualDigest *root, const RxDualDigest *estimate_ref, RxDualDigest *out); + +#endif /* OMEGA_RX_DUAL_BIND_H */ diff --git a/src/dual/rx_dual_update.c b/src/dual/rx_dual_update.c new file mode 100644 index 000000000..5624dc67a --- /dev/null +++ b/src/dual/rx_dual_update.c @@ -0,0 +1,123 @@ +/* DUAL-1a: reference price update. */ +#include "rx_dual_update.h" + +#include +#include + +RxDualStatus rx_dual_step_lambda(double lambda, double estimate, double uncertainty, double budget, + double scale, const RxDualController *ctl, double lambda_max, double *out) +{ + double pressure, deadband, g, next; + RxDualStatus st; + if (!ctl || !out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_controller(ctl)) != RX_DUAL_OK) return st; + if (!isfinite(lambda) || !isfinite(estimate) || !isfinite(uncertainty) || !isfinite(budget) || + !isfinite(scale) || !isfinite(lambda_max)) + return RX_DUAL_ERR_NONFINITE; + if (!(scale > 0.0)) return RX_DUAL_ERR_SCALE; + if (lambda < 0.0 || uncertainty < 0.0 || !(lambda_max > 0.0) || lambda > lambda_max) return RX_DUAL_ERR_RANGE; + pressure = (estimate - budget) / scale; + deadband = ctl->k_sigma * uncertainty / scale; + if (fabs(pressure) <= deadband) g = 0.0; + else g = pressure - (pressure > 0.0 ? deadband : -deadband); + next = (1.0 - ctl->rho) * lambda + ctl->eta * g; + if (!isfinite(next)) return RX_DUAL_ERR_NONFINITE; + if (next < 0.0) next = 0.0; + if (next > lambda_max) next = lambda_max; + *out = next; + return RX_DUAL_OK; +} + +/* Shared structural checks for init and update. */ +static RxDualStatus common(const RxDualResource *res, const RxDualInput *in, const RxDualController *ctl, + double *lambda_max, RxDualDigest *ctl_id) +{ + RxDualStatus st; + if (!res || !in || !ctl) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_resource(res)) != RX_DUAL_OK) return st; + if ((st = rx_dual_check_controller(ctl)) != RX_DUAL_OK) return st; + if ((st = rx_dual_controller_lambda_max(ctl, res->resource_id, lambda_max)) != RX_DUAL_OK) return st; + if ((st = rx_dual_digest_controller(ctl, ctl_id)) != RX_DUAL_OK) return st; + return RX_DUAL_OK; +} + +static void take_input(RxDualConstraintState *s, const RxDualInput *in) +{ + s->observation_ref = in->observation_ref; + s->estimate_ref = in->estimate_ref; + s->estimate_kind = in->estimate_kind; + s->estimate = in->estimate; + s->uncertainty = in->uncertainty; + s->calibration_ref = in->calibration_ref; + s->generation = in->generation; +} + +RxDualStatus rx_dual_init_state(const RxDualResource *res, RxDualClass cls, double budget, + const RxDualDigest *budget_contract, const RxDualInput *in, + const RxDualController *ctl, uint64_t now_generation, + RxDualConstraintState *out) +{ + double lmax = 0.0, next = 0.0; + RxDualDigest ctl_id, zero; + RxDualLambdaState cls_state; + RxDualStatus st; + if (!budget_contract || !out) return RX_DUAL_ERR_NULL; + if ((st = common(res, in, ctl, &lmax, &ctl_id)) != RX_DUAL_OK) return st; + if (cls != RX_DUAL_CLASS_CAPACITY && cls != RX_DUAL_CLASS_SOFT) return RX_DUAL_ERR_CLASS; + if (!isfinite(budget)) return RX_DUAL_ERR_NONFINITE; + if (rx_dual_digest_is_zero(budget_contract)) return RX_DUAL_ERR_DIGEST; + /* The very first input must be structurally valid and in the resource's unit. */ + if ((st = rx_dual_check_input(in)) != RX_DUAL_OK) return st; + if (in->unit != res->unit) return RX_DUAL_ERR_UNIT; + if ((st = rx_dual_classify_input(in, NULL, ctl, now_generation, &cls_state)) != RX_DUAL_OK) return st; + memset(out, 0, sizeof *out); + out->resource_id = res->resource_id; + out->unit = res->unit; + out->cls = cls; + out->budget = budget; + out->budget_contract = *budget_contract; + take_input(out, in); + out->controller_id = ctl_id; + out->tick = 0; + memset(&zero, 0, sizeof zero); + if ((st = rx_dual_evidence_extend(&zero, &in->estimate_ref, &out->evidence_root)) != RX_DUAL_OK) return st; + out->lambda = 0.0; + out->lambda_state = cls_state; + if (cls_state == RX_DUAL_LAMBDA_FRESH || cls_state == RX_DUAL_LAMBDA_UNCALIBRATED) { + if ((st = rx_dual_step_lambda(0.0, in->estimate, in->uncertainty, budget, res->scale, ctl, lmax, &next)) != RX_DUAL_OK) + return st; + out->lambda = next; + } + return rx_dual_check_constraint(out); +} + +RxDualStatus rx_dual_update(const RxDualConstraintState *prev, const RxDualResource *res, + const RxDualInput *in, const RxDualController *ctl, + uint64_t now_generation, RxDualConstraintState *out) +{ + double lmax = 0.0, next = 0.0; + RxDualDigest ctl_id, parent; + RxDualLambdaState state; + RxDualStatus st; + if (!prev || !out) return RX_DUAL_ERR_NULL; + if ((st = common(res, in, ctl, &lmax, &ctl_id)) != RX_DUAL_OK) return st; + if ((st = rx_dual_check_constraint_against(prev, res)) != RX_DUAL_OK) return st; /* malformed parent, id, unit */ + if (!rx_dual_digest_eq(&prev->controller_id, &ctl_id)) return RX_DUAL_ERR_CONTROLLER; /* parameter mutation */ + if (prev->lambda > lmax) return RX_DUAL_ERR_RANGE; + if ((st = rx_dual_digest_constraint(prev, &parent)) != RX_DUAL_OK) return st; + if ((st = rx_dual_classify_input(in, prev, ctl, now_generation, &state)) != RX_DUAL_OK) return st; + *out = *prev; + out->parent = parent; + out->tick = prev->tick + 1; + out->lambda_state = state; + if (state == RX_DUAL_LAMBDA_FRESH || state == RX_DUAL_LAMBDA_UNCALIBRATED) { + if (in->unit != res->unit) return RX_DUAL_ERR_UNIT; + if ((st = rx_dual_step_lambda(prev->lambda, in->estimate, in->uncertainty, prev->budget, res->scale, ctl, lmax, &next)) != RX_DUAL_OK) + return st; + take_input(out, in); + out->lambda = next; + if ((st = rx_dual_evidence_extend(&prev->evidence_root, &in->estimate_ref, &out->evidence_root)) != RX_DUAL_OK) return st; + } + /* STALE / FROZEN / REFUSED: lambda, estimate fields, generation and evidence root are held. */ + return rx_dual_check_constraint(out); +} diff --git a/src/dual/rx_dual_update.h b/src/dual/rx_dual_update.h new file mode 100644 index 000000000..ae81d1898 --- /dev/null +++ b/src/dual/rx_dual_update.h @@ -0,0 +1,46 @@ +/* DUAL-1a: the reference price update (ADR 0031 section 5.1) as a pure, + * deterministic function of (previous state, bound input, controller). + * + * pressure = (estimate - budget) / scale + * deadband = k_sigma * uncertainty / scale + * g = 0 if |pressure| <= deadband else pressure - sign(pressure) * deadband + * lambda' = clip((1 - rho) * lambda + eta * g, 0, lambda_max) + * + * Properties (checked by tests/dual): lambda' is finite, 0 <= lambda' <= + * lambda_max; inside the deadband there is no pressure-driven movement; + * persistent positive pressure raises lambda until clipped; slack decays it + * toward zero; eta = 0 never moves on gradient; rho > 0 forgets old scarcity; + * the same inputs give byte-identical records. + * + * Refusals (negative status, no record): NULL, malformed prev/resource/ + * controller, controller digest != prev.controller_id, resource not priced by + * the controller, resource id or unit mismatch, prev.lambda above lambda_max. + * Held states (record produced, lambda unchanged): REFUSED (bad values, future + * generation), STALE (too old, going backwards, evidence not verified), + * FROZEN (regime change). UNCALIBRATED updates lambda but may never influence + * production. The cadence parameter is identity only: the caller invokes the + * update once per cadence tick. */ +#ifndef OMEGA_RX_DUAL_UPDATE_H +#define OMEGA_RX_DUAL_UPDATE_H + +#include "rx_dual_bind.h" + +/* Initial state (tick 0, parent zero) from a first input. The class and budget + * come from the owning contract (budget_contract cites it). INVARIANT and + * undeclared classes are refused here as everywhere. lambda starts at 0. */ +RxDualStatus rx_dual_init_state(const RxDualResource *res, RxDualClass cls, double budget, + const RxDualDigest *budget_contract, const RxDualInput *in, + const RxDualController *ctl, uint64_t now_generation, + RxDualConstraintState *out); + +/* One tick. out->parent = digest(prev), out->tick = prev->tick + 1. */ +RxDualStatus rx_dual_update(const RxDualConstraintState *prev, const RxDualResource *res, + const RxDualInput *in, const RxDualController *ctl, + uint64_t now_generation, RxDualConstraintState *out); + +/* The arithmetic alone, for the independent reference test. Returns the new + * lambda or refuses on nonfinite / nonpositive scale. */ +RxDualStatus rx_dual_step_lambda(double lambda, double estimate, double uncertainty, double budget, + double scale, const RxDualController *ctl, double lambda_max, double *out); + +#endif /* OMEGA_RX_DUAL_UPDATE_H */ diff --git a/tests/.crumb b/tests/.crumb index d1bd1eeb3..83424cbce 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -278,14 +278,14 @@ } ], "children_root": "sha256:4401e9b1951727dd948e583dc596ca7330946f64fe8aba7466569aa7e09e8672", - "compiled_at": "2026-10-04T18:16:20Z", + "compiled_at": "2026-10-04T18:16:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:fd40504451c18f359577acd98497bf1176124097c8942f40177262344f7fc8b1", + "digest": "sha256:3cf64120d367b0ec3b8cb61415ddc3ba7f8b42e04c79d3a0e958b7eb8eef1622", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 779, - "generated_at_commit": "f056b77f64ee94a80f4ed2899b5f9495aa3ef840", - "source_tree": "sha256:43a2acbc664afaf715384d67327a06b10f249db2d0d21a749f91b896d4224219" + "files": 784, + "generated_at_commit": "41dcb768a955c1027d60d8b86d4ff6629118b9e6", + "source_tree": "sha256:4fd150be03a6ea339696b23754f5048eec519a311613508eeca32069d87453c6" }, "owner_lane": "", "provenance": { diff --git a/tests/dual/dual_fixtures.h b/tests/dual/dual_fixtures.h new file mode 100644 index 000000000..e043ae404 --- /dev/null +++ b/tests/dual/dual_fixtures.h @@ -0,0 +1,60 @@ +/* Shared fixtures for DUAL-0b / DUAL-1a tests: minimal valid ESTIMATION + * records (n = m = 1) and a controller. */ +#ifndef DUAL_FIXTURES_H +#define DUAL_FIXTURES_H +#include "rx_dual_update.h" +#include + +static inline RxDualDigest fx_dig(uint8_t v) { RxDualDigest d; memset(d.b, v, 32); return d; } + +static inline est_model fx_model(est_unit unit) +{ + est_model m; memset(&m, 0, sizeof m); + m.n = 1; m.m = 1; m.estimator = EST_ESTIMATOR_LINEAR_KALMAN; m.meaning = EST_UNCERTAINTY_GAUSSIAN_COVARIANCE; + m.state_unit[0] = unit; m.obs_unit[0] = unit; + m.F[0] = 1.0; m.Q[0] = 0.01; m.H[0] = 1.0; m.R[0] = 0.04; m.step_ns = 1000; + return m; +} +static inline est_observation fx_obs(est_unit unit, double z, double var, uint64_t seq) +{ + est_observation o; memset(&o, 0, sizeof o); + o.m = 1; o.unit[0] = unit; o.z[0] = z; o.R[0] = var; o.t_ns = (int64_t)seq * 1000; o.seq = seq; + memset(o.source.b, 0x5A, 32); memset(o.evidence.b, 0xE1 + (uint8_t)seq, 32); + return o; +} +/* Belief at `generation` with x, variance; evidence_root = extend(parent_root, digest(obs)). */ +static inline est_belief fx_belief(const est_model *m, const est_observation *obs, const est_digest *parent_root, + est_unit unit, double x, double var, uint64_t generation) +{ + est_belief b; est_digest od; memset(&b, 0, sizeof b); + b.n = 1; b.unit[0] = unit; b.x[0] = x; b.P[0] = var; b.generation = generation; b.t_ns = (int64_t)generation * 1000; + est_digest_model(m, &b.model); + memset(b.parent.b, 0x77, 32); + est_digest_observation(obs, &od); + est_evidence_root_extend(parent_root, &od, &b.evidence_root); + return b; +} +static inline RxDualController fx_ctl(uint32_t id, double eta, double rho, double k_sigma, double lmax, uint64_t max_age) +{ + RxDualController c; memset(&c, 0, sizeof c); + c.eta = eta; c.rho = rho; c.k_sigma = k_sigma; c.max_age = max_age; c.cadence = 1; + c.n = 1; c.resource_id[0] = id; c.lambda_max[0] = lmax; + return c; +} +static inline RxDualResource fx_res(uint32_t id, RxDualUnit unit, double scale) +{ + RxDualResource r; memset(&r, 0, sizeof r); + r.resource_id = id; r.unit = unit; r.scale = scale; r.contract = fx_dig(0x11); + return r; +} +/* A verified, calibrated input: estimate x, sd, at generation g, unit bytes. */ +static inline RxDualInput fx_input(double x, double sd, uint64_t g, int calibrated) +{ + RxDualInput in; memset(&in, 0, sizeof in); + in.estimate_ref = fx_dig(0x30 + (uint8_t)(g & 0x3F)); in.estimate_kind = RX_DUAL_EST_ESTIMATED; + in.unit = RX_DUAL_UNIT_BYTES; in.estimate = x; in.uncertainty = sd; in.generation = g; + in.evidence_root = fx_dig(0x66); in.evidence_verified = 1; + if (calibrated) in.calibration_ref = fx_dig(0x44); + return in; +} +#endif diff --git a/tests/dual/dual_ref.c b/tests/dual/dual_ref.c new file mode 100644 index 000000000..26775f9e2 --- /dev/null +++ b/tests/dual/dual_ref.c @@ -0,0 +1,17 @@ +#include "dual_ref.h" +#include +double dual_ref_gradient(double estimate, double budget, double scale, double uncertainty, double k_sigma) +{ + double p = (estimate - budget) / scale, band = k_sigma * uncertainty / scale; + if (p > band) return p - band; + if (p < -band) return p + band; + return 0.0; +} +double dual_ref_closed_form(double l0, double g, double eta, double rho, unsigned long t) +{ + double a; + if (rho == 0.0) return l0 + eta * g * (double)t; + a = pow(1.0 - rho, (double)t); + return a * l0 + eta * g * (1.0 - a) / rho; +} +double dual_ref_fixed_point(double g, double eta, double rho) { return eta * g / rho; } diff --git a/tests/dual/dual_ref.h b/tests/dual/dual_ref.h new file mode 100644 index 000000000..fd7ac94b0 --- /dev/null +++ b/tests/dual/dual_ref.h @@ -0,0 +1,16 @@ +/* Independent analytic reference for the DUAL-1a update, derived from ADR 0031 + * section 5.1 on paper, not from rx_dual_update.c. Closed forms for a constant + * input: with g constant and no clipping, the recursion + * l(t+1) = (1 - rho) l(t) + eta g + * has the solution + * l(t) = (1 - rho)^t l(0) + eta g (1 - (1 - rho)^t) / rho (rho > 0) + * l(t) = l(0) + eta g t (rho = 0) + * and the fixed point l* = eta g / rho. Clipping to [0, lambda_max] is applied + * per step, so the closed form holds exactly while the trajectory stays inside + * the bounds, and the implementation must saturate at the bound afterwards. */ +#ifndef DUAL_REF_H +#define DUAL_REF_H +double dual_ref_gradient(double estimate, double budget, double scale, double uncertainty, double k_sigma); +double dual_ref_closed_form(double l0, double g, double eta, double rho, unsigned long t); +double dual_ref_fixed_point(double g, double eta, double rho); +#endif diff --git a/tests/dual/test_dual_bind.c b/tests/dual/test_dual_bind.c new file mode 100644 index 000000000..d8eb784d0 --- /dev/null +++ b/tests/dual/test_dual_bind.c @@ -0,0 +1,141 @@ +/* DUAL-0b tests: binding to est_* records, unit table, evidence verification, + * freshness classification. */ +#include "dual_fixtures.h" +#include +#include + +static int g_checks, g_fail; +#define CHECK(c) do { g_checks++; if (!(c)) { g_fail++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) +#define CHECK_ST(expr, want) do { RxDualStatus s_ = (expr); g_checks++; if (s_ != (want)) { g_fail++; \ + fprintf(stderr, "FAIL %s:%d: %s -> %d, want %d\n", __FILE__, __LINE__, #expr, (int)s_, (int)(want)); } } while (0) + +static void test_units(void) +{ + RxDualUnit u; + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_BYTE, &u), RX_DUAL_OK); CHECK(u == RX_DUAL_UNIT_BYTES); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_NANOSECOND, &u), RX_DUAL_OK); CHECK(u == RX_DUAL_UNIT_NS); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_WATT, &u), RX_DUAL_OK); CHECK(u == RX_DUAL_UNIT_WATT); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_LOG2_PICOSECOND, &u), RX_DUAL_OK); CHECK(u == RX_DUAL_UNIT_LOG2_PS); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_NONE, &u), RX_DUAL_ERR_UNIT); CHECK(u == RX_DUAL_UNIT_NONE); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_MAX_, &u), RX_DUAL_ERR_UNIT); + CHECK_ST(rx_dual_unit_from_est((est_unit)77, &u), RX_DUAL_ERR_UNIT); + CHECK_ST(rx_dual_unit_from_est(EST_UNIT_BYTE, NULL), RX_DUAL_ERR_NULL); +} + +static void test_bind_belief_and_evidence(void) +{ + est_model m = fx_model(EST_UNIT_BYTE); + est_observation o = fx_obs(EST_UNIT_BYTE, 5200.0, 100.0, 1); + est_digest zero; memset(&zero, 0, sizeof zero); + est_belief b = fx_belief(&m, &o, &zero, EST_UNIT_BYTE, 5150.0, 64.0, 7); + RxDualInput in; RxDualDigest cal = fx_dig(0x44); est_digest bd; + CHECK(est_check_belief(&b) == EST_OK); + CHECK_ST(rx_dual_bind_belief(&b, 0, &cal, &in), RX_DUAL_OK); + CHECK(in.estimate == 5150.0 && in.uncertainty == 8.0 && in.unit == RX_DUAL_UNIT_BYTES && in.generation == 7); + CHECK(in.estimate_kind == RX_DUAL_EST_ESTIMATED && in.evidence_verified == 0 && rx_dual_digest_is_zero(&in.observation_ref)); + est_digest_belief(&b, &bd); CHECK(memcmp(bd.b, in.estimate_ref.b, 32) == 0); /* cites the record, copies nothing else */ + CHECK(memcmp(in.evidence_root.b, b.evidence_root.b, 32) == 0); + /* evidence verification: pass, then a broken root, then a wrong observation */ + CHECK_ST(rx_dual_verify_belief_evidence(&b, &zero, &o, &in), RX_DUAL_OK); CHECK(in.evidence_verified == 1); + { est_belief bad = b; bad.evidence_root.b[3] ^= 1; RxDualInput in2; + CHECK_ST(rx_dual_bind_belief(&bad, 0, &cal, &in2), RX_DUAL_OK); + CHECK_ST(rx_dual_verify_belief_evidence(&bad, &zero, &o, &in2), RX_DUAL_ERR_EVIDENCE); CHECK(in2.evidence_verified == 0); } + { est_observation o2 = fx_obs(EST_UNIT_BYTE, 5200.0, 100.0, 2); RxDualInput in3; + CHECK_ST(rx_dual_bind_belief(&b, 0, &cal, &in3), RX_DUAL_OK); + CHECK_ST(rx_dual_verify_belief_evidence(&b, &zero, &o2, &in3), RX_DUAL_ERR_EVIDENCE); } + { est_digest other; memset(other.b, 0x99, 32); RxDualInput in4; + CHECK_ST(rx_dual_bind_belief(&b, 0, &cal, &in4), RX_DUAL_OK); + CHECK_ST(rx_dual_verify_belief_evidence(&b, &other, &o, &in4), RX_DUAL_ERR_EVIDENCE); } + /* input not bound from this belief */ + { RxDualInput in5 = fx_input(1.0, 1.0, 7, 1); CHECK_ST(rx_dual_verify_belief_evidence(&b, &zero, &o, &in5), RX_DUAL_ERR_DIGEST); } + /* refusals */ + CHECK_ST(rx_dual_bind_belief(&b, 1, &cal, &in), RX_DUAL_ERR_RANGE); /* component out of range */ + { est_belief prior = b; prior.generation = 0; memset(prior.parent.b, 0, 32); memset(prior.evidence_root.b, 0, 32); + CHECK(est_check_belief(&prior) == EST_OK); + CHECK_ST(rx_dual_bind_belief(&prior, 0, &cal, &in), RX_DUAL_ERR_EVIDENCE); } /* a prior is not evidence */ + { est_belief nf = b; nf.x[0] = NAN; CHECK_ST(rx_dual_bind_belief(&nf, 0, &cal, &in), RX_DUAL_ERR_KIND); } /* est refuses it first */ + { est_belief nu = b; nu.unit[0] = EST_UNIT_NONE; CHECK(rx_dual_bind_belief(&nu, 0, &cal, &in) != RX_DUAL_OK); } + CHECK_ST(rx_dual_bind_belief(NULL, 0, &cal, &in), RX_DUAL_ERR_NULL); + /* uncalibrated binding is allowed and recorded as such */ + CHECK_ST(rx_dual_bind_belief(&b, 0, NULL, &in), RX_DUAL_OK); CHECK(rx_dual_digest_is_zero(&in.calibration_ref)); +} + +static void test_bind_prediction_and_observation(void) +{ + est_model m = fx_model(EST_UNIT_NANOSECOND); + est_observation o = fx_obs(EST_UNIT_NANOSECOND, 10.0, 1.0, 1); + est_digest zero; memset(&zero, 0, sizeof zero); + est_belief prior = fx_belief(&m, &o, &zero, EST_UNIT_NANOSECOND, 10.0, 1.0, 3); + est_prediction p; RxDualInput in; est_digest pd; + memset(&p, 0, sizeof p); + est_digest_belief(&prior, &p.prior); est_digest_model(&m, &p.model); + p.horizon = 2; p.generation = 5; p.t_ns = 5000; p.n = 1; p.m = 1; p.x[0] = 12.0; p.P[0] = 2.25; p.y_mean[0] = 12.0; p.S[0] = 2.29; + CHECK(est_check_prediction(&p) == EST_OK); + CHECK_ST(rx_dual_bind_prediction(&p, &m, &prior, 0, NULL, &in), RX_DUAL_OK); + CHECK(in.estimate == 12.0 && in.uncertainty == 1.5 && in.unit == RX_DUAL_UNIT_NS && in.generation == 5 && in.estimate_kind == RX_DUAL_EST_PREDICTED); + est_digest_prediction(&p, &pd); CHECK(memcmp(pd.b, in.estimate_ref.b, 32) == 0); + CHECK(memcmp(in.evidence_root.b, prior.evidence_root.b, 32) == 0); + { est_model m2 = m; m2.Q[0] = 0.02; CHECK_ST(rx_dual_bind_prediction(&p, &m2, &prior, 0, NULL, &in), RX_DUAL_ERR_DIGEST); } /* wrong model */ + { est_belief b2 = prior; b2.x[0] = 11.0; CHECK_ST(rx_dual_bind_prediction(&p, &m, &b2, 0, NULL, &in), RX_DUAL_ERR_DIGEST); } /* wrong prior */ + CHECK_ST(rx_dual_bind_prediction(&p, &m, &prior, 1, NULL, &in), RX_DUAL_ERR_RANGE); + /* observation: MEASURED, its own evidence */ + CHECK_ST(rx_dual_bind_observation(&o, 0, 9, NULL, &in), RX_DUAL_OK); + CHECK(in.estimate_kind == RX_DUAL_EST_MEASURED && in.estimate == 10.0 && in.uncertainty == 1.0 && in.generation == 9); + CHECK(rx_dual_digest_eq(&in.estimate_ref, &in.observation_ref) && in.evidence_verified == 1); + CHECK(memcmp(in.evidence_root.b, o.evidence.b, 32) == 0); + { est_observation bad = o; memset(bad.evidence.b, 0, 32); CHECK_ST(rx_dual_bind_observation(&bad, 0, 9, NULL, &in), RX_DUAL_ERR_KIND); } + CHECK_ST(rx_dual_bind_observation(&o, 3, 9, NULL, &in), RX_DUAL_ERR_RANGE); +} + +static void test_classify(void) +{ + RxDualController ctl = fx_ctl(1, 0.1, 0.01, 2.0, 10.0, 4); + RxDualLambdaState st; + RxDualInput in = fx_input(5200.0, 100.0, 10, 1); + RxDualConstraintState prev; memset(&prev, 0, sizeof prev); + prev.resource_id = 1; prev.unit = RX_DUAL_UNIT_BYTES; prev.cls = RX_DUAL_CLASS_SOFT; prev.budget = 5000.0; + prev.budget_contract = fx_dig(0x22); prev.estimate_ref = fx_dig(0x33); prev.estimate_kind = RX_DUAL_EST_ESTIMATED; + prev.estimate = 5000.0; prev.uncertainty = 100.0; prev.calibration_ref = fx_dig(0x44); prev.lambda = 0.0; + prev.lambda_state = RX_DUAL_LAMBDA_FRESH; prev.controller_id = fx_dig(0x55); prev.generation = 9; prev.tick = 0; + prev.evidence_root = fx_dig(0x66); + CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_FRESH); + CHECK_ST(rx_dual_classify_input(&in, NULL, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_FRESH); + in.calibration_ref = fx_dig(0); CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_UNCALIBRATED); + in = fx_input(5200.0, 100.0, 10, 1); + in.regime_change = 1; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_FROZEN); + in.regime_change = 0; + in.evidence_verified = 0; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_STALE); + in.evidence_verified = 1; + CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 14, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_FRESH); /* age 4 == max_age ok */ + CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 15, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_STALE); /* age 5 > max_age */ + in.generation = 8; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_STALE); /* older than prev */ + in.generation = 11; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_REFUSED); /* future */ + in.generation = 10; + in.estimate = NAN; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_REFUSED); + in.estimate = INFINITY; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_REFUSED); + in.estimate = 5200.0; in.uncertainty = -1.0; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_REFUSED); + in.uncertainty = 100.0; memset(&in.evidence_root, 0, 32); CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_OK); CHECK(st == RX_DUAL_LAMBDA_REFUSED); + in = fx_input(5200.0, 100.0, 10, 1); + in.unit = RX_DUAL_UNIT_NS; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_ERR_UNIT); /* structural */ + in.unit = RX_DUAL_UNIT_BYTES; + prev.cls = RX_DUAL_CLASS_INVARIANT; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_ERR_CLASS); + prev.cls = RX_DUAL_CLASS_SOFT; + ctl.max_age = 0; CHECK_ST(rx_dual_classify_input(&in, &prev, &ctl, 10, &st), RX_DUAL_ERR_RANGE); + /* evidence chain */ + { RxDualDigest zero = fx_dig(0), r1, r2, r3, e = fx_dig(0x33); + CHECK_ST(rx_dual_evidence_extend(&zero, &e, &r1), RX_DUAL_OK); + CHECK_ST(rx_dual_evidence_extend(&r1, &e, &r2), RX_DUAL_OK); + CHECK_ST(rx_dual_evidence_extend(&zero, &e, &r3), RX_DUAL_OK); + CHECK(!rx_dual_digest_is_zero(&r1) && !rx_dual_digest_eq(&r1, &r2) && rx_dual_digest_eq(&r1, &r3)); + CHECK_ST(rx_dual_evidence_extend(&zero, &zero, &r1), RX_DUAL_ERR_DIGEST); } +} + +int main(void) +{ + test_units(); + test_bind_belief_and_evidence(); + test_bind_prediction_and_observation(); + test_classify(); + printf("test_dual_bind: %d checks, %d failures: %s\n", g_checks, g_fail, g_fail ? "FAIL" : "PASS"); + return g_fail ? 1 : 0; +} diff --git a/tests/dual/test_dual_update.c b/tests/dual/test_dual_update.c new file mode 100644 index 000000000..eb0a8790b --- /dev/null +++ b/tests/dual/test_dual_update.c @@ -0,0 +1,207 @@ +/* DUAL-1a tests: the reference update as a pure function; analytic cases + * against an independent closed form (dual_ref.c); property suite; hostile + * structural cases. */ +#include "dual_fixtures.h" +#include "dual_ref.h" +#include +#include + +#ifndef PROP_ROUNDS +#define PROP_ROUNDS 3000 +#endif +static int g_checks, g_fail; +#define CHECK(c) do { g_checks++; if (!(c)) { g_fail++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) +#define CHECK_ST(expr, want) do { RxDualStatus s_ = (expr); g_checks++; if (s_ != (want)) { g_fail++; \ + fprintf(stderr, "FAIL %s:%d: %s -> %d, want %d\n", __FILE__, __LINE__, #expr, (int)s_, (int)(want)); } } while (0) +static uint64_t g_rng = 0xD1B54A32D192ED03ull; +static uint64_t rnd(void) { g_rng ^= g_rng >> 12; g_rng ^= g_rng << 25; g_rng ^= g_rng >> 27; return g_rng * 0x2545F4914F6CDD1Dull; } +static double rdbl(double lo, double hi) { return lo + (hi - lo) * ((double)(rnd() >> 11) / 9007199254740992.0); } + +static const double BUDGET = 5000.0, SCALE = 1000.0; +static RxDualDigest BC; + +static RxDualStatus run(const RxDualController *ctl, const RxDualResource *res, const double *est, const double *sd, + uint32_t n, RxDualConstraintState *out, double *trace) +{ + RxDualConstraintState s, nx; RxDualStatus st; + RxDualInput in = fx_input(est[0], sd[0], 1, 1); + if ((st = rx_dual_init_state(res, RX_DUAL_CLASS_SOFT, BUDGET, &BC, &in, ctl, 1, &s)) != RX_DUAL_OK) return st; + if (trace) trace[0] = s.lambda; + for (uint32_t t = 1; t < n; t++) { + in = fx_input(est[t], sd[t], t + 1, 1); + if ((st = rx_dual_update(&s, res, &in, ctl, t + 1, &nx)) != RX_DUAL_OK) return st; + if (trace) trace[t] = nx.lambda; + s = nx; + } + *out = s; + return RX_DUAL_OK; +} + +/* ---- analytic cases vs the independent closed form ---- */ +static void test_analytic(void) +{ + enum { N = 60 }; + double est[N], sd[N], tr[N]; + RxDualResource res = fx_res(1, RX_DUAL_UNIT_BYTES, SCALE); + RxDualConstraintState fin; + /* 1. single binding constraint: estimate 5500 (pressure 0.5), sd 100 (band 0.2) -> g = 0.3 */ + { RxDualController ctl = fx_ctl(1, 0.1, 0.05, 2.0, 10.0, 4); double g = dual_ref_gradient(5500, BUDGET, SCALE, 100, 2.0); + for (int i = 0; i < N; i++) { est[i] = 5500; sd[i] = 100; } + CHECK(fabs(g - 0.3) < 1e-12); + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + for (unsigned long t = 0; t < N; t++) CHECK(fabs(tr[t] - dual_ref_closed_form(0.0, g, 0.1, 0.05, t + 1)) < 1e-9); + CHECK(tr[N - 1] > tr[0] && tr[N - 1] <= dual_ref_fixed_point(g, 0.1, 0.05) + 1e-9 && fin.lambda_state == RX_DUAL_LAMBDA_FRESH); } + /* 2. slack constraint from a nonzero price: estimate 4000 (pressure -1), band 0.2 -> g = -0.8: decays to 0 and stays */ + { RxDualController ctl = fx_ctl(1, 0.1, 0.05, 2.0, 10.0, 4); + for (int i = 0; i < N; i++) { est[i] = (i < 20) ? 5500 : 4000; sd[i] = 100; } + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + CHECK(tr[19] > 0.3 && fin.lambda == 0.0); + for (int i = 20; i < N; i++) CHECK(tr[i] <= tr[i - 1] && tr[i] >= 0.0); } + /* 3. two competing constraints: memory binding, latency slack; each priced independently */ + { RxDualController ctl; RxDualResource rm = fx_res(1, RX_DUAL_UNIT_BYTES, SCALE), rl = fx_res(2, RX_DUAL_UNIT_BYTES, SCALE); + RxDualConstraintState fm, fl; double gm, gl; + memset(&ctl, 0, sizeof ctl); ctl.eta = 0.2; ctl.rho = 0.1; ctl.k_sigma = 1.0; ctl.max_age = 4; ctl.cadence = 1; ctl.n = 2; + ctl.resource_id[0] = 1; ctl.resource_id[1] = 2; ctl.lambda_max[0] = 5.0; ctl.lambda_max[1] = 5.0; + for (int i = 0; i < N; i++) { est[i] = 6000; sd[i] = 50; } + CHECK_ST(run(&ctl, &rm, est, sd, N, &fm, tr), RX_DUAL_OK); + gm = dual_ref_gradient(6000, BUDGET, SCALE, 50, 1.0); + CHECK(fabs(tr[N - 1] - dual_ref_closed_form(0.0, gm, 0.2, 0.1, N)) < 1e-9); + for (int i = 0; i < N; i++) { est[i] = 4900; sd[i] = 50; } + CHECK_ST(run(&ctl, &rl, est, sd, N, &fl, tr), RX_DUAL_OK); + gl = dual_ref_gradient(4900, BUDGET, SCALE, 50, 1.0); + CHECK(gl < 0.0 && fl.lambda == 0.0 && fm.lambda > 1.0); } + /* 4. inside the deadband: estimate 5100, sd 100, k 2 -> band 0.2 >= pressure 0.1: no movement, ever */ + { RxDualController ctl = fx_ctl(1, 0.5, 0.0, 2.0, 10.0, 4); + for (int i = 0; i < N; i++) { est[i] = 5100; sd[i] = 100; } + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + for (int i = 0; i < N; i++) CHECK(tr[i] == 0.0); } + /* 5. clip: eta 1, rho 0, pressure 2 -> hits lambda_max 3 at step 2 and stays */ + { RxDualController ctl = fx_ctl(1, 1.0, 0.0, 0.0, 3.0, 4); + for (int i = 0; i < N; i++) { est[i] = 7000; sd[i] = 0; } + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + CHECK(tr[0] == 2.0 && tr[1] == 3.0 && tr[N - 1] == 3.0); } + /* 6. zero eta: never moves; 7. rho > 0 forgets scarcity after pressure disappears (inside band) */ + { RxDualController ctl = fx_ctl(1, 0.0, 0.1, 2.0, 10.0, 4); + for (int i = 0; i < N; i++) { est[i] = 9000; sd[i] = 0; } + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + for (int i = 0; i < N; i++) CHECK(tr[i] == 0.0); } + { RxDualController ctl = fx_ctl(1, 0.5, 0.1, 2.0, 10.0, 4); + for (int i = 0; i < N; i++) { est[i] = (i < 10) ? 7000 : 5000; sd[i] = 100; } + CHECK_ST(run(&ctl, &res, est, sd, N, &fin, tr), RX_DUAL_OK); + CHECK(tr[9] > 1.0); + for (int i = 10; i < N; i++) CHECK(fabs(tr[i] - tr[9] * pow(0.9, (double)(i - 9))) < 1e-9); + CHECK(fin.lambda < 0.01 * tr[9]); } + /* 8. determinism: same inputs -> byte-identical records */ + { RxDualController ctl = fx_ctl(1, 0.1, 0.05, 2.0, 10.0, 4); RxDualConstraintState a, b; uint8_t ea[4096], eb[4096]; size_t la, lb; + for (int i = 0; i < N; i++) { est[i] = 5300 + 200 * sin(i * 0.3); sd[i] = 80; } + CHECK_ST(run(&ctl, &res, est, sd, N, &a, NULL), RX_DUAL_OK); + CHECK_ST(run(&ctl, &res, est, sd, N, &b, NULL), RX_DUAL_OK); + CHECK_ST(rx_dual_encode_constraint(&a, ea, sizeof ea, &la), RX_DUAL_OK); + CHECK_ST(rx_dual_encode_constraint(&b, eb, sizeof eb, &lb), RX_DUAL_OK); + CHECK(la == lb && memcmp(ea, eb, la) == 0 && a.tick == N - 1); } +} + +/* ---- held states and structural refusals ---- */ +static void test_states_and_refusals(void) +{ + RxDualController ctl = fx_ctl(1, 0.1, 0.05, 2.0, 10.0, 4); + RxDualResource res = fx_res(1, RX_DUAL_UNIT_BYTES, SCALE); + RxDualInput in = fx_input(5500, 100, 1, 1); + RxDualConstraintState s0, s1, s2; RxDualDigest d0; + CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_SOFT, BUDGET, &BC, &in, &ctl, 1, &s0), RX_DUAL_OK); + CHECK(s0.tick == 0 && rx_dual_digest_is_zero(&s0.parent) && s0.lambda > 0.0 && s0.lambda_state == RX_DUAL_LAMBDA_FRESH); + CHECK_ST(rx_dual_digest_constraint(&s0, &d0), RX_DUAL_OK); + /* stale input: lambda, estimate, generation, evidence root held; parent/tick advance */ + in = fx_input(9000, 100, 2, 1); in.evidence_verified = 0; + CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); + CHECK(s1.lambda_state == RX_DUAL_LAMBDA_STALE && s1.lambda == s0.lambda && s1.estimate == s0.estimate && s1.generation == s0.generation); + CHECK(rx_dual_digest_eq(&s1.parent, &d0) && s1.tick == 1 && rx_dual_digest_eq(&s1.evidence_root, &s0.evidence_root)); + /* too old */ + in = fx_input(9000, 100, 2, 1); CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 7, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_STALE && s1.lambda == s0.lambda); + /* refused values: held, REFUSED */ + in = fx_input(NAN, 100, 2, 1); CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_REFUSED && s1.lambda == s0.lambda); + in = fx_input(9000, -1, 2, 1); CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_REFUSED); + in = fx_input(9000, 100, 5, 1); CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_REFUSED); /* future generation */ + /* frozen: regime change holds the price */ + in = fx_input(9000, 100, 2, 1); in.regime_change = 1; + CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_FROZEN && s1.lambda == s0.lambda); + /* uncalibrated: lambda moves but is marked; a later calibrated input is FRESH again */ + in = fx_input(9000, 100, 2, 0); + CHECK_ST(rx_dual_update(&s0, &res, &in, &ctl, 2, &s1), RX_DUAL_OK); CHECK(s1.lambda_state == RX_DUAL_LAMBDA_UNCALIBRATED && s1.lambda > s0.lambda); + in = fx_input(9000, 100, 3, 1); + CHECK_ST(rx_dual_update(&s1, &res, &in, &ctl, 3, &s2), RX_DUAL_OK); CHECK(s2.lambda_state == RX_DUAL_LAMBDA_FRESH && s2.tick == 2); + /* structural refusals: no record */ + in = fx_input(9000, 100, 2, 1); + { RxDualController m = ctl; m.eta = 0.2; CHECK_ST(rx_dual_update(&s0, &res, &in, &m, 2, &s1), RX_DUAL_ERR_CONTROLLER); } /* mutated parameters */ + { RxDualController m = ctl; m.resource_id[0] = 2; CHECK_ST(rx_dual_update(&s0, &res, &in, &m, 2, &s1), RX_DUAL_ERR_RESOURCE); } /* resource not priced */ + { RxDualResource r2 = res; r2.unit = RX_DUAL_UNIT_NS; CHECK_ST(rx_dual_update(&s0, &r2, &in, &ctl, 2, &s1), RX_DUAL_ERR_UNIT); } + { RxDualResource r2 = res; r2.resource_id = 3; CHECK_ST(rx_dual_update(&s0, &r2, &in, &ctl, 2, &s1), RX_DUAL_ERR_RESOURCE); } + { RxDualResource r2 = res; r2.scale = 0.0; CHECK_ST(rx_dual_update(&s0, &r2, &in, &ctl, 2, &s1), RX_DUAL_ERR_SCALE); } + { RxDualResource r2 = res; r2.scale = -1.0; CHECK_ST(rx_dual_update(&s0, &r2, &in, &ctl, 2, &s1), RX_DUAL_ERR_SCALE); } + { RxDualInput u = in; u.unit = RX_DUAL_UNIT_NS; CHECK_ST(rx_dual_update(&s0, &res, &u, &ctl, 2, &s1), RX_DUAL_ERR_UNIT); } /* unit mismatch */ + { RxDualConstraintState bad = s0; bad.cls = RX_DUAL_CLASS_INVARIANT; CHECK_ST(rx_dual_update(&bad, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_CLASS); } + { RxDualConstraintState bad = s0; bad.lambda = 11.0; CHECK_ST(rx_dual_update(&bad, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_RANGE); } /* above max */ + { RxDualConstraintState bad = s0; bad.lambda = -1.0; CHECK_ST(rx_dual_update(&bad, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_RANGE); } + { RxDualConstraintState bad = s0; bad.parent = fx_dig(0x01); CHECK_ST(rx_dual_update(&bad, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_DIGEST); } /* malformed parent */ + { RxDualConstraintState bad = s0; memset(&bad.controller_id, 0, 32); CHECK_ST(rx_dual_update(&bad, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_DIGEST); } + CHECK_ST(rx_dual_update(NULL, &res, &in, &ctl, 2, &s1), RX_DUAL_ERR_NULL); + CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_INVARIANT, BUDGET, &BC, &in, &ctl, 1, &s1), RX_DUAL_ERR_CLASS); + CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_UNDECLARED, BUDGET, &BC, &in, &ctl, 1, &s1), RX_DUAL_ERR_CLASS); + CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_SOFT, INFINITY, &BC, &in, &ctl, 1, &s1), RX_DUAL_ERR_NONFINITE); + { RxDualDigest z = fx_dig(0); CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_SOFT, BUDGET, &z, &in, &ctl, 1, &s1), RX_DUAL_ERR_DIGEST); } + { RxDualInput u = in; u.estimate = NAN; CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_SOFT, BUDGET, &BC, &u, &ctl, 1, &s1), RX_DUAL_ERR_NONFINITE); } + /* CAPACITY: diagnostic price computed; class preserved */ + CHECK_ST(rx_dual_init_state(&res, RX_DUAL_CLASS_CAPACITY, BUDGET, &BC, &in, &ctl, 2, &s1), RX_DUAL_OK); + CHECK(s1.cls == RX_DUAL_CLASS_CAPACITY && s1.lambda > 0.0); + /* step function edge cases */ + { double o; CHECK_ST(rx_dual_step_lambda(0.5, INFINITY, 1, BUDGET, SCALE, &ctl, 10, &o), RX_DUAL_ERR_NONFINITE); + CHECK_ST(rx_dual_step_lambda(0.5, -INFINITY, 1, BUDGET, SCALE, &ctl, 10, &o), RX_DUAL_ERR_NONFINITE); + CHECK_ST(rx_dual_step_lambda(0.5, 1, 1, BUDGET, 0.0, &ctl, 10, &o), RX_DUAL_ERR_SCALE); + CHECK_ST(rx_dual_step_lambda(0.5, 1, 1, BUDGET, -1.0, &ctl, 10, &o), RX_DUAL_ERR_SCALE); + CHECK_ST(rx_dual_step_lambda(-0.5, 1, 1, BUDGET, SCALE, &ctl, 10, &o), RX_DUAL_ERR_RANGE); + CHECK_ST(rx_dual_step_lambda(0.5, 1, -1, BUDGET, SCALE, &ctl, 10, &o), RX_DUAL_ERR_RANGE); + CHECK_ST(rx_dual_step_lambda(11, 1, 1, BUDGET, SCALE, &ctl, 10, &o), RX_DUAL_ERR_RANGE); + CHECK_ST(rx_dual_step_lambda(0.5, 1e308, 1, -1e308, SCALE, &ctl, 10, &o), RX_DUAL_ERR_NONFINITE); } /* overflow refused, never clipped into range */ +} + +/* ---- property suite ---- */ +static void test_properties(void) +{ + for (int round = 0; round < PROP_ROUNDS; round++) { + RxDualController ctl = fx_ctl(1, rdbl(0, 2), rdbl(0, 1), rdbl(0, 4), rdbl(0.01, 50), 1 + rnd() % 8); + RxDualResource res = fx_res(1, RX_DUAL_UNIT_BYTES, rdbl(1e-3, 1e6)); + RxDualInput in = fx_input(rdbl(-1e6, 1e6), rdbl(0, 1e4), 1, 1); + RxDualConstraintState s, nx; RxDualStatus st; + double budget = rdbl(-1e6, 1e6), lmax = ctl.lambda_max[0]; + st = rx_dual_init_state(&res, RX_DUAL_CLASS_SOFT, budget, &BC, &in, &ctl, 1, &s); + CHECK_ST(st, RX_DUAL_OK); + for (uint64_t t = 2; t <= 40; t++) { + double g, want, prev = s.lambda; + in = fx_input(rdbl(-1e6, 1e6), rdbl(0, 1e4), t, (rnd() % 5) != 0); + if (rnd() % 7 == 0) in.evidence_verified = 0; + CHECK_ST(rx_dual_update(&s, &res, &in, &ctl, t, &nx), RX_DUAL_OK); + CHECK(isfinite(nx.lambda) && nx.lambda >= 0.0 && nx.lambda <= lmax && nx.tick == t - 1); + if (nx.lambda_state == RX_DUAL_LAMBDA_STALE) CHECK(nx.lambda == prev); + else { + g = dual_ref_gradient(in.estimate, budget, res.scale, in.uncertainty, ctl.k_sigma); + want = (1.0 - ctl.rho) * prev + ctl.eta * g; + if (want < 0) want = 0; + if (want > lmax) want = lmax; + CHECK(fabs(nx.lambda - want) <= 1e-9 * (1.0 + fabs(want))); + if (g == 0.0) CHECK(nx.lambda <= prev); /* inside deadband: only the leak can act */ + if (ctl.eta == 0.0) CHECK(nx.lambda <= prev); + } + s = nx; + } + } +} + +int main(void) +{ + BC = fx_dig(0x22); + test_analytic(); + test_states_and_refusals(); + test_properties(); + printf("test_dual_update: %d checks, %d failures: %s\n", g_checks, g_fail, g_fail ? "FAIL" : "PASS"); + return g_fail ? 1 : 0; +}