From 500a510f818ce4b973f4862c96e9361b9c450890 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 4 Oct 2026 13:18:01 -0500 Subject: [PATCH 1/3] dual: DUAL-1b pre-registration: replay scenarios, measures and thresholds (ADR 0031 s8, DUAL_PRICE_REFERENCE) Frozen before any replay code is written or run. All inputs UNCALIBRATED. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- .crumb | 20 +++--- evidence/DUAL/1b-replay/receipt.md | 103 +++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 evidence/DUAL/1b-replay/receipt.md diff --git a/.crumb b/.crumb index 182fcb709..bb2301dae 100644 --- a/.crumb +++ b/.crumb @@ -64,7 +64,7 @@ }, { "digest": "566d430880c69205dd89c06d645f5030442c526a97ce3e2c386276c282792e9f", - "last_commit": "1fb209a", + "last_commit": "3a3144d", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -88,13 +88,13 @@ }, { "digest": "8e9dd6f8117c615273c291427db9ad14abaa907bc05741145bb9892706f68efa", - "last_commit": "1fb209a", + "last_commit": "3a3144d", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { "digest": "e96dec62800dc6dae755ebe10eacfedcb0959e29e00a3540284cb585f8f9716f", - "last_commit": "1fb209a", + "last_commit": "3a3144d", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -106,14 +106,14 @@ } ], "children_root": "sha256:0d2e5faff3d6519826a4922d03e6258a03c42792caee91a6a4bc0d6cba17dfa2", - "compiled_at": "2026-10-04T18:45:19Z", + "compiled_at": "2026-10-04T18:54:21Z", "compiler": "crumb-compile/1", - "digest": "sha256:b0b3f7ca6dda9afa4b09fa49f49e397ecfce7737dfd34ea426bcc2b905364232", - "evidence_files": 1288, - "evidence_root": "sha256:5e3386545c3742835ee2e90f3c06e6bfeeb9725fb51664e6c25cb59822356642", - "files": 3043, - "generated_at_commit": "393fa208a1a662de5ccb6c877d8d7c8b22fc1bf7", - "source_tree": "sha256:499774e2016130a9904f3fda0dbe4c7c852fa5b6109b60f44bde780b49cb1eb1" + "digest": "sha256:9632c38a3bb1282a964c283ff983a1b81fb43d4cbee43bb6d05a075c84fbadc6", + "evidence_files": 1289, + "evidence_root": "sha256:f1f7e1d6ad2817edece547fbafecc8cfaaddb735ea956756bc4448789982d2e0", + "files": 3044, + "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", + "source_tree": "sha256:842b576c699d45e987e92e845c640404e34c28a500f272c9015ee7b1b2971dc9" }, "owner_lane": "", "provenance": { diff --git a/evidence/DUAL/1b-replay/receipt.md b/evidence/DUAL/1b-replay/receipt.md new file mode 100644 index 000000000..bd8fdb516 --- /dev/null +++ b/evidence/DUAL/1b-replay/receipt.md @@ -0,0 +1,103 @@ +# DUAL-1b receipt: deterministic replay of the reference controller over digest-bound traces (ADR 0031 s5, s8 DUAL-1) + +Gate: `DUAL_PRICE_REFERENCE`. Host-only, no GPU, no production influence. + +EVERY input and EVERY replayed price in this receipt is UNCALIBRATED: EST-3 has not passed for any signal on main, no +calibration receipt exists, every synthetic trace carries calibrated = 0, and every recorded trace carries calibrated = 0. +No price computed here may influence any production decision. The recorded traces are perf cycle counters, which are +exact counts but are not calibrated estimates of any resource pressure; they are used only as input streams. + +| Field | Value | +|---|---| +| repository | aien-dev/omega | +| base commit | 0b3dcc0efe4210e533fd5cfcc675aada371c9fc9 (branch dual/0b-1a-binding-controller, PR #267, on dual/0a-records PR #266, on main cb7cc216147664549484fcca8e8ceadbd6f6cdbf) | +| pre-registration commit | the commit that adds this file with the sections "Pre-registered" filled and "Results" empty | +| candidate commit | filled in the Results section | +| 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 | + +## Pre-registered: trace format +Kind byte 8, version 1, digest domain `omega.dual.trace.v1`: u32 trace_kind, u64 seed, u32 n_ticks, then per tick +u64 generation, f64 estimate, f64 uncertainty, u32 calibrated, u32 evidence_verified, u32 regime_change. Little-endian, +-0.0 written as +0.0, NaN/Inf refused. Trace digest = SHA-256(domain || 0x00 || encoding). Generators use xorshift64* +from the declared seed. Replay log = the RxDualConstraintState digest of every tick, in order; replay digest = +SHA-256("omega.dual.replay.v1" || 0x00 || controller_id || trace_digest || u32 n || the n state digests). +Recorded traces: trace_kind RECORDED, seed 0, estimate = the perf cycle count of one (CPU, event) series, uncertainty 0, +calibrated 0, evidence_verified 1, regime_change 0, generation = row index + 1; the raw CSV bytes are SHA-256 digested +and listed below. The R15 machine-perf.csv and preflight-perf.csv files contain ONLY perf-stat cycle counters +(armv8_pmuv3_*/cycles/ per CPU and smmuv3_pmcg_*/cycles/); they contain no thermal, power or clock-frequency column, +so the per-CPU cycles-per-second series is the only stream available and is used as a clock-activity proxy. + +## Pre-registered: common parameters +- Resource: unit BYTES (synthetic) or DIMENSIONLESS (recorded; the unit registry has no cycles unit), scale 1000 (synthetic), 1e9 (recorded). +- Budget: 5000 (synthetic, except where stated); 1e9 cycles per second (recorded). +- Class SOFT. budget_contract = fixed stand-in digest 0x22 repeated (0x23 repeated for the second chain of scenario 7). +- Reference controller: eta 0.1, rho 0.05, k_sigma 2.0, max_age 4, cadence 1, lambda_max 10 per resource; n = 1 except + scenario 5/6 (n = 2, ids 1, 2) and scenario 10 (n = 32, ids 1..32). +- Hostile controller (negative control): eta 5.0, rho 0.0, k_sigma 2.0, max_age 4, cadence 1, lambda_max 10, n = 1. +- now_generation = tick generation (no staleness is injected in these scenarios). n_ticks = 200 unless stated. +- K (recovery ticks) = ceil(ln(100) / (-ln(1 - rho))) = ceil(89.78) = 90 for rho 0.05. + +## Pre-registered: scenarios (seeds are part of the trace identity) +| # | Scenario | Trace kind | Seed | Definition | +|---|---|---|---|---| +| 1 | constant low load | 1 | 0x1001 | estimate 4000, sd 100, all ticks | +| 2 | single binding constraint | 2 | 0x1002 | estimate 5500, sd 100, all ticks | +| 3 | abrupt step load | 3 | 0x1003 | estimate 4000 for t < 100, 6000 for t >= 100, sd 100 | +| 4 | square-wave congestion | 4 | 0x1004 | period 50: 25 ticks at 6000 then 25 at 4000, 4 periods, sd 100 | +| 5 | competing: memory | 5 | 0x1005 | resource 1: estimate 6000, sd 50 (one controller n = 2 prices both chains) | +| 6 | competing: latency | 6 | 0x1005 | resource 2: estimate 4900, sd 50 (inside the deadband: pressure -0.1, band 0.1) | +| 7 | sudden capacity reduction | 7 | 0x1007 | estimate 4500, sd 100; budget 5000 for t < 100 (chain A), 3000 for t >= 100 (chain B, new budget_contract, new chain at tick 0). A budget is declared by a contract (budget_contract), so a budget change is a new contract and a new constraint chain; the engine does not mutate a budget inside a chain | +| 8 | workload regime change | 8 | 0x1008 | estimate 5500, sd 100; regime_change = 1 for 80 <= t < 120 | +| 9 | bursty queue arrivals | 9 | 0x1009 | estimate 4200 + burst; each tick with probability 1/8 (xorshift) a burst uniform in [800, 2400]; sd 100 | +| 10 | high branch fan-out | 10 | 0x100A + r | 32 resources (r = 0..31), each a constant estimate 5500 + offset_r, offset_r uniform in [-50, 50] from seed 0x100A + r; sd 100; one controller n = 32 | +| 11 | KV saturation approach | 11 | 0x100B | estimate 3000 + 4000 * t / 199 (crosses budget + band = 5200 near t = 109.5), sd 100 | +| 12 | recovery after pressure | 12 | 0x100C | estimate 6000 for t < 100, 5000 for t >= 100 (inside the band: leak only), sd 100 | +| 13 | recorded R15 | 13 | 0 | machine-perf.csv CPU0 armv8_pmuv3_0/cycles/ and CPU7 armv8_pmuv3_1/cycles/; preflight-perf.csv CPU7 armv8_pmuv3_1/cycles/; both R15 runs (6 traces) | + +## Pre-registered: measures and thresholds (frozen before the first replay run) +Reversal count R(x) of a series x: number of sign changes between consecutive nonzero first differences. +| Id | Measure | Threshold | Applies to | +|---|---|---|---| +| M1 | hard bounds: lambda finite, >= 0, <= lambda_max at every tick | every tick | all scenarios, both controllers | +| M2 | constant-load tail: R(lambda) over the final 50 ticks | = 0 | 1, 2, 3, 5, 6, 7 (chain B), 8, 10 (each chain), 12 | +| M3a | square wave reversals: R(lambda) over the whole trace | <= 2 * periods = 8 | 4 | +| M3b | square wave amplitude: max(lambda) - min(lambda) over the final full period (t 150..199) | <= 0.25 * lambda_max = 2.5 | 4 | +| M4 | step response: lambda(t+1) >= lambda(t) - 1e-12 * (1 + lambda(t)) for every t at or after the step | all post-step ticks | 3 (t >= 100), 7 chain B (all), 11 (t >= first tick with estimate > 5200), 2 (all) | +| M5 | recovery: lambda[t_off - 1 + K] <= 0.01 * lambda[t_off - 1], t_off = 100, K = 90 | holds | 12 | +| M6 | regime: every tick with regime_change = 1 is FROZEN with lambda, estimate and generation equal to the previous tick | every such tick | 8 | +| M7 | uncalibrated: every tick with calibrated = 0 has state UNCALIBRATED (never FRESH) | every tick | all scenarios (all ticks are calibrated = 0), recorded | +| M8 | no input-free oscillation: R(lambda) <= R(estimate) + 1 per chain | holds | all scenarios incl. recorded, both controllers | +| M9 | competing: final lambda(memory) > 0 and final lambda(latency) = 0 | holds | 5, 6 | +| M10 | fan-out spread: max - min of the 32 final lambdas | <= 0.25 (eta * max band-shifted gradient spread / rho = 0.1 * 0.1 / 0.05 = 0.2, plus margin) | 10 | +| M11 | KV: lambda = 0 for every tick with estimate <= 5200 | holds | 11 | +| D1 | determinism: two replays of the same trace with the same controller give identical replay digests | identical | all | +| D2 | golden: trace digest and replay digest of scenarios 2 and 4 equal hex constants committed in the test (cross-process) | identical | 2, 4 | +| D3 | tamper: flipping one bit of the encoded estimate at tick 17 changes the trace digest and the replay digest | both differ | 2 | +| D4 | calibrated variant: scenario 2 with calibrated = 1 (stand-in digest, a FORMAT exercise, not a calibration claim) gives identical lambdas, FRESH states, and a different replay digest | holds | 2 | +| H1 | hard constraint through the replay entry point (class INVARIANT, class UNDECLARED) | refused, no record | 2 | + +Expected analytic values for the reference controller (from the closed form in tests/dual/dual_ref.c): scenario 2 fixed +point eta*g/rho = 0.1*0.3/0.05 = 0.6; scenario 3 post-step fixed point 1.6; scenario 4 peak per period +1.6 * (1 - 0.95^25) = 1.156 (M3b margin 2.5 - 1.16); scenario 7 chain B fixed point 2.6; scenario 12 peak 1.6 * (1 - 0.95^100) = 1.59. + +Note on M3a/M8 (stated before running): for the update lambda' = clip((1 - rho) lambda + eta g) the first difference +d(t) = eta g(t) - rho lambda(t) obeys d(t+1) = (1 - rho) d(t) + eta (g(t+1) - g(t)) while unclipped, so within a run of +same-sign changes of g the sign of d changes at most once, and clipping cannot add a reversal under monotone g. Hence +R(lambda) <= R(g) + 1 <= R(estimate) + 1 for EVERY valid controller, including the hostile one. Reversal counts therefore +cannot distinguish a good controller from a bad one on a noise-free wave; the discriminating oscillation measure is the +amplitude bound M3b, which the hostile controller is expected to fail. + +## Pre-registered: negative control (expected outcomes) +| Controller | Scenario | Expected | +|---|---|---| +| hostile (eta 5.0, rho 0.0) | 4 square wave | M1 PASS, M3a PASS (2 per period), M3b FAIL: lambda slams to lambda_max within 3 ticks of each rising edge and to 0 within 3 ticks of each falling edge, amplitude 10 > 2.5. Gate verdict for this configuration: FAIL | +| reference | 4 square wave | M1, M3a, M3b, M8 PASS | + +Verdict rule: `DUAL_PRICE_REFERENCE = PASS` only if every applicable measure passes for the reference controller on every +scenario AND the negative control fails M3b as declared. Otherwise FAIL with the numbers. A threshold is never changed +after this commit; a surprise is recorded as FAIL. + +## Results +(filled after the run; nothing above this line changes) From cff342b400ec0bc6b2b68e1004212872d490bb07 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 4 Oct 2026 13:30:25 -0500 Subject: [PATCH 2/3] dual: DUAL-1b deterministic replay over digest-bound traces, DUAL_PRICE_REFERENCE = PASS on host (ADR 0031 s5, s8) Trace record (kind byte 8, omega.dual.trace.v1) with canonical encoding and digest; xorshift64* generators for 12 pre-registered scenarios; perf-CSV importer (bytes in, no stdio in the library); replay engine recording every constraint state digest into a replay digest (omega.dual.replay.v1); measures (reversals, peak-to-peak); negative control (eta 5, rho 0 on a square wave) fails the amplitude bound as declared. Golden digests for scenarios 2 and 4 committed. Receipt evidence/DUAL/1b-replay. Every replayed price is UNCALIBRATED (EST-3 has not passed for any signal); no price may influence production. src/runtime untouched. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- .crumb | 22 +-- docs/.crumb | 14 +- docs/dual/.crumb | 8 +- docs/dual/DUAL_RECORDS.md | 3 +- evidence/DUAL/1b-replay/receipt.md | 109 ++++++++++ mk/.crumb | 8 +- mk/dual.mk | 21 ++ src/.crumb | 12 +- src/dual/rx_dual_replay.c | 257 ++++++++++++++++++++++++ src/dual/rx_dual_replay.h | 117 +++++++++++ tests/.crumb | 12 +- tests/dual/dual_traces.c | 76 +++++++ tests/dual/dual_traces.h | 16 ++ tests/dual/test_dual_replay.c | 306 +++++++++++++++++++++++++++++ 14 files changed, 942 insertions(+), 39 deletions(-) create mode 100644 src/dual/rx_dual_replay.c create mode 100644 src/dual/rx_dual_replay.h create mode 100644 tests/dual/dual_traces.c create mode 100644 tests/dual/dual_traces.h create mode 100644 tests/dual/test_dual_replay.c diff --git a/.crumb b/.crumb index bb2301dae..12230228f 100644 --- a/.crumb +++ b/.crumb @@ -51,7 +51,7 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "e555caa4954d4dde3e499f37df8c9af2efc41cf349dc8096273b0e062dd07a01", + "digest": "28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", "last_commit": "00f54b6", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." @@ -63,7 +63,7 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "566d430880c69205dd89c06d645f5030442c526a97ce3e2c386276c282792e9f", + "digest": "25dc918988768c936cdd41410a68c18df3c9acf7cd129812bc030c6a070cc67c", "last_commit": "3a3144d", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." @@ -87,13 +87,13 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "8e9dd6f8117c615273c291427db9ad14abaa907bc05741145bb9892706f68efa", + "digest": "839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", "last_commit": "3a3144d", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "e96dec62800dc6dae755ebe10eacfedcb0959e29e00a3540284cb585f8f9716f", + "digest": "7f8951ba438340bc36223d5722c1055efd8fdd01efe3a43bcf8f2e61ce68dea7", "last_commit": "3a3144d", "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:0d2e5faff3d6519826a4922d03e6258a03c42792caee91a6a4bc0d6cba17dfa2", - "compiled_at": "2026-10-04T18:54:21Z", + "children_root": "sha256:f5cd48815a1e17f58c9a56231edbc7e6d4cb19045afc1f03cba9a5c92b20d6f4", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:9632c38a3bb1282a964c283ff983a1b81fb43d4cbee43bb6d05a075c84fbadc6", + "digest": "sha256:66e284ae0133839cc33508c911c5d9a6f33ab766dff0921e5ef4f9bee8c427f0", "evidence_files": 1289, - "evidence_root": "sha256:f1f7e1d6ad2817edece547fbafecc8cfaaddb735ea956756bc4448789982d2e0", - "files": 3044, - "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", - "source_tree": "sha256:842b576c699d45e987e92e845c640404e34c28a500f272c9015ee7b1b2971dc9" + "evidence_root": "sha256:0a9a6e928903825cb8a22dc4ac22694e6e486e46e31d4cb16a003d27e707014f", + "files": 3049, + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:0f0644ad0efe4e91cecf30c1b2e6dbbe115c8363364b9adfe74879ad492c27a1" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 7d2524369..1b27715f9 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -69,8 +69,8 @@ "purpose": "Directory docs/crumbs. Purpose not yet described by a human." }, { - "digest": "854ab7030882cb50548bdff57507ba8998c474383487f8a0ec7c500388942c39", - "last_commit": "3bebcc6", + "digest": "e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", + "last_commit": "00f54b6", "name": "dual", "purpose": "DUAL (ARCH-0031) documents in omega: applicability experiment method and findings (APPLICABILITY" }, @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:8c5aa863d287effc2ff5df57b7f029cac75a1b0e7b5c71c9fd59c45e308a1e10", - "compiled_at": "2026-10-04T18:38:56Z", + "children_root": "sha256:7010ef7e3e982e3828bfeb2ff1a1fe5c8cfeceb6aef820c34c175fe1d0208734", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:e555caa4954d4dde3e499f37df8c9af2efc41cf349dc8096273b0e062dd07a01", + "digest": "sha256:28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", "evidence_files": 26, "evidence_root": "sha256:f58236184eadc5f8c8f95499127a9f43c2f4dc702ddca267853d8c3dee0690f1", "files": 100, - "generated_at_commit": "3bebcc68321fcc876b88df129f41d6190411a926", - "source_tree": "sha256:cdc21061ff898378059abf06f18c234da5dee85662b05f4c811fd79ac6db7fc0" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:769aaad946a229b703b2def73085c781284fb13666223c34d2fdb16020a3f11b" }, "owner_lane": "", "provenance": { diff --git a/docs/dual/.crumb b/docs/dual/.crumb index dfd9f2a01..42ec6c5ce 100644 --- a/docs/dual/.crumb +++ b/docs/dual/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T18:38:56Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:854ab7030882cb50548bdff57507ba8998c474383487f8a0ec7c500388942c39", + "digest": "sha256:e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 2, - "generated_at_commit": "3bebcc68321fcc876b88df129f41d6190411a926", - "source_tree": "sha256:b4f60491f7fce8800fc6c21c9ed83cc4bc6b98ed72bfd5536325f2d2245f0df0" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:4920e1c6a40a59b4a821be52bcfffd40a7b179228517de9afa28f78126089da7" } }, "name": "dual", diff --git a/docs/dual/DUAL_RECORDS.md b/docs/dual/DUAL_RECORDS.md index 06dd4ad92..7bd813d2b 100644 --- a/docs/dual/DUAL_RECORDS.md +++ b/docs/dual/DUAL_RECORDS.md @@ -13,6 +13,7 @@ nothing under `src/runtime/` includes it). DUAL is scarcity information. It neve | recommendation | 5 | `omega.dual.recommendation.v1` | dig decision_site, dig price_vector, u64 generation, u32 n_candidates, n x dig, u32 actual, u32 recommended, u32 n_consequences, n x (u32 resource_id, u32 unit, f64 predicted, f64 predicted_sd, u32 has_measured, f64 measured, f64 error), u32 authority (= 0) | | outcome | 6 | `omega.dual.outcome.v1` | dig recommendation, dig before_vector, dig after_vector, u32 target_resource_id, u64 generation_before, u64 generation_after, f64 lambda_before, f64 lambda_after | | site | 7 | `omega.dual.site.v1` | u32 site_id, 32 bytes name (printable ASCII, NUL padded), dig owner, u32 max_alternatives | +| trace | 8 | `omega.dual.trace.v1` | u32 trace_kind, u64 seed, u32 n_ticks, n x (u64 generation, f64 estimate, f64 uncertainty, u32 calibrated, u32 evidence_verified, u32 regime_change) (DUAL-1b; replay digest domain `omega.dual.replay.v1` over controller_id, trace digest, u32 n, n state digests) | Every encoding starts with `u8 kind, u8 version`. Doubles are IEEE-754 binary64 bits; -0.0 is written as +0.0; NaN and Inf are refused before encoding. Digest = SHA-256(domain || 0x00 || encoding). Decoders refuse a wrong kind byte, a wrong version, @@ -29,4 +30,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: 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. +Status: DUAL-0a, DUAL-0b (rx_dual_bind.h: binding to est_* records, evidence verification, FRESH/STALE/FROZEN/UNCALIBRATED/REFUSED), DUAL-1a (rx_dual_update.h: reference update) and DUAL-1b (rx_dual_replay.h: digest-bound traces, kind byte 8 `omega.dual.trace.v1`, deterministic replay with replay digest `omega.dual.replay.v1`, perf-CSV import; target `test-dual-replay`) IMPLEMENTED; `DUAL_CONSTRAINT_CONTRACT = PASS` on host (evidence/DUAL/0b-1a-binding-controller); `DUAL_PRICE_REFERENCE = PASS` on host over 12 synthetic scenarios and 6 recorded R15 counter traces with a pre-registered negative control (evidence/DUAL/1b-replay). Every replayed price is UNCALIBRATED (EST-3 has not passed for any signal): no price may influence production. Next: DUAL-2 (Pareto integration) needs EST-5 and EST-7. diff --git a/evidence/DUAL/1b-replay/receipt.md b/evidence/DUAL/1b-replay/receipt.md index bd8fdb516..fc253a844 100644 --- a/evidence/DUAL/1b-replay/receipt.md +++ b/evidence/DUAL/1b-replay/receipt.md @@ -101,3 +101,112 @@ after this commit; a surprise is recorded as FAIL. ## Results (filled after the run; nothing above this line changes) + +| Field | Value | +|---|---| +| pre-registration commit | b0626246f3b6a03326420fafba502c3b5398c4c8 (receipt sections above, no replay code) | +| candidate commit | this commit (branch dual/1b-replay) | +| compiler | cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, aarch64 | +| reference controller_id | bcf4285a4830663a1c9f0548a0bd9fe1c96ca9852a8c90d7e45b13f56d820ec4 (eta 0.1, rho 0.05, k_sigma 2, max_age 4, cadence 1, n 1, lambda_max 10) | +| competing controller_id (n = 2) | e4a8317c4e867e8cb49f28aa8275d2b2da8abf910575d88f5bb89edaffc41b11 | +| fan-out controller_id (n = 32) | b1af2f7e5df739fdb93534ac1ace836ade213f21e03bb20d1b28abad4bcddb27 | +| hostile controller_id | 1d08ce016bbbc47a506145813cd59d5f57bd687929f0a89ad3e1dec4d4d6a1c1 (eta 5.0, rho 0.0, k_sigma 2, max_age 4, cadence 1, n 1, lambda_max 10) | + +### Commands and results (run from the repository root) +| Command | Result | +|---|---| +| make dual-purity | PASS: rx_dual.o rx_dual_bind.o rx_dual_update.o rx_dual_replay.o reference no authority, promotion or process symbol (nm -u of rx_dual_replay.o: memcmp memcpy memset strlen, rx_dual_*, sha256_*, stack protector only; no stdio: the CSV importer takes bytes the test read) | +| make test-dual-replay | plain: 456 checks, 0 failures, PASS; ASan/UBSan: 456 checks, 0 failures, PASS | +| make test-dual | test-dual-types 14107/1667, test-dual-bind 85/85, test-dual-update 355348/35837, test-dual-replay 456/456 checks, 0 failures | +| make all / make test | unchanged: DUAL is not part of them; nothing under src/runtime/ changed (`git diff --quiet HEAD -- src/runtime` clean) | + +### Trace digests and replay digests (reference controller unless marked HOST) +| Chain | n | trace digest (omega.dual.trace.v1) | replay digest (omega.dual.replay.v1) | final lambda | +|---|---|---|---|---| +| 1 constant low | 200 | 0e0c2f9f833a47158fa885af34d2fc5cbea250124f891e56af31c7cf0d7a4ab1 | e3c9fbfc4e73e1054d8739f47c7ad56a5b97ad6efe57b2ef12ece689e67e32d2 | 0 | +| 2 binding (golden) | 200 | f9c3944ab952dd5e2beefebbb2499f98ec7be28157b571a47e406ca40e4d93fb | 46345146d1213fdd2888fd0ace3f7ab31287565fd42adf950ad58edeb11bf5ae | 0.599978968 | +| 3 step | 200 | 966e602e16db9f3a61187f0135c45657dd7b3f22b22d910e3eda9ede19eed175 | 3539d9d349ee9f789a833d95a011351b99807d3f85896996e35e6defa255f489 | 1.59052715 | +| 4 square (golden) | 200 | 0a463faef33ab2a59da6c854eabfa2ec9e189f3e5e6b00f07c773e490e52edd5 | 64a30ce77dc7decfedae15c6c8e8e66fa75693df049916deab7ca2abbfaaa888 | 0 | +| 4 square HOST | 200 | 0a463faef33ab2a59da6c854eabfa2ec9e189f3e5e6b00f07c773e490e52edd5 | 34ac4fde2f4f92a676c619201e0cea590a06522bc1a3d5e40040c4d4bd6b46bb | 0 | +| 5 competing memory | 200 | c988bffd193292402cd853f8453454f128bbc7c5a0b6c89e50fad8df5b0a4c6a | 46afcfa907a0393ccbff84ec62d844fda2af0aa7739f7fbbccdf70b0cde60064 | 1.79993691 | +| 6 competing latency | 200 | 92298884577ea85d0a133da92a2ae73b49f78e189df585af03bd8dbf6016abe4 | 65379174831235554c91a3194dd32a43a3285f5046b4f2096e29a1c22f5a287a | 0 | +| 7A capacity (budget 5000) | 100 | e3f2c5e796ede019b78fcc1b3ff584a2a8028236332d361b356fe0883c3da337 | 714c2438d01a68bf180af03906c50dbac6168757ea25ca41de9c4de85e580325 | 0 | +| 7B capacity (budget 3000, new contract) | 100 | 7f7d9ccdddf47321a545c49340eb48d32d0d595b695e82cdca0995be2a86a28c | 5a041ac1ff7390cd63428b87bcfd0380dec105ecbd48563f94965bf4a81aed62 | 2.58460662 | +| 8 regime change | 200 | 0b17c31469fd478c24ec6acf2318caadab124045431c986af6e985fa50c1245b | 8a4eabfde2d1d51e2457fba6c5f9174c06b320c5c6e22f85aebd0ba2317d0be7 | 0.599836345 | +| 9 bursty (26 bursts) | 200 | 30162f09ee84b7b1bd56a88f9f72426e3c9a476d90201f921c92e63cee1271bd | 723b8d1eb96e1647414cd376f79d87c4f7c035048388a9e41d2d46222caf3b3d | 0.120999564 | +| 10 fan-out r = 0 (of 32; all 32 digests in the test output) | 200 | df3ebf9e65ea2c9bb6e7df3e372edaf43f876f6c2d85916e38d84c21f7ee0d68 | 4746925bb0c1c76b8b2b87ae155644b4b13c009622df3f06f71837bb7ed99e07 | 0.538262214 | +| 11 KV saturation | 200 | f0308e3fa8e549f4a18a3fb57535ade611587f5720582ac057cd60698ae5794a | e49c22a48ec0d7e6dff654017a39e138db6aeff8dd426d2ad3730973ed39510d | 2.84391271 | +| 12 recovery | 200 | dca6d3cb19658bb4b2f9b33386c3c77a70d765e7bc81c994f4488565d1204826 | 356cd1f1f68d3ac3763fec9d9c6efa27e1e90b3ff48a9cab7dfb357d1b7bc5f8 | 0.00941676249 | +| 13 R15 020536Z machine CPU0 | 1976 | 8f5f9b1f92064ba93ce22685a55d6403645a53e9a0397f04db6dc868f5bd9345 | 175bd2cc5950145e7f0b371a8a0994d2fc22bc1c489eca470e942ad15af64bea | 0 | +| 13 R15 020536Z machine CPU7 | 1976 | fe2ca66182ae4f8891165d5b44a635e467efb5d30bd4e77d418dc76cd7869d94 | 908ed8bdef115365fb74392dfe160c7668cf80e31ddc271838fbd16f43333e3a | 0 | +| 13 R15 020536Z preflight CPU7 | 20 | 3a1cf368c4b0f376ecfea21864ce7f39a3cce12ffa96c457212d9e48a4af13c8 | 0e37c660a3e004755517657f85012b11fe5ec8306bb9acd8f6fd1a913cc30373 | 3.70425459 | +| 13 R15 025735Z machine CPU0 | 2033 | 83e92c15ae0cd3a2cc7b747559fbcd3786619180697d1721c910f435b098777b | 2f5387d79988db0bfdbf8272ba28bba19dcad766c79252d7af817e312a168725 | 0 | +| 13 R15 025735Z machine CPU7 | 2033 | 12ee87a420de376a852dbebda58a4ac8a4639e7523545dcc5ddda8c537763b7e | fbbc9e42ee76495260f91495efbcd27620de7a8d1264aeb5bbe744b717ab1e96 | 0 | +| 13 R15 025735Z preflight CPU7 | 20 | 5a262e15b8d85ade33f4d028a052292ed8e26946b837e196cf5e1b30a605c586 | 2b3f0da6e2c063a6594adba2b24b2474e9d3fe2fe04628f82889e4f402c6fa1d | 3.70285023 | + +Recorded raw files (SHA-256 of the bytes as read; `SUM` = equals the entry in that run's own SHA256SUMS): +| File | bytes | sha256 | SUM | +|---|---|---|---| +| evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/machine-perf.csv | 5254065 | 22663a037a7f557634c1786e0427a669925c99d0e6c436309139a362e94a43af | PASS | +| evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/preflight-perf.csv | 1469 | 2078a6412767ca5bbae118fe22486d0846e5e25b83cbd73231468e7736e70943 | not listed in SHA256SUMS; recorded here only | +| evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/machine-perf.csv | 5400978 | 36e0cd936e783a054c82a47444219f1266ddb47a1ae33a14fadaaaac23adb3b2 | PASS | +| evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/preflight-perf.csv | 1469 | ff14887fd894d5902f948942eecfb1f936b598a5f9986f5beb2c544244b8cf51 | not listed in SHA256SUMS; recorded here only | + +Recorded-trace facts: machine CPU0 cycles exceed the 1e9 budget on 53 / 60 of 1976 / 2033 ticks (bursts), CPU7 on 669 / 694 +ticks; the preflight CPU7 series is above budget on all 20 ticks. These are counter streams, not calibrated estimates. + +### Measures (reference controller; thresholds frozen above) +| Chain | M1 | M2 | M3a | M3b | M4 | M5 | M6 | M7 | M8 (R lambda / R est / bound) | other | +|---|---|---|---|---|---|---|---|---|---|---| +| 1 constant low | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | | +| 2 binding | PASS | PASS (0) | | | PASS | | | PASS | PASS 0/0/1 | A PASS final 0.599978968 vs 0.6; D2 PASS; D3 PASS; D4 PASS; H1 PASS | +| 3 step | PASS | PASS (0) | | | PASS (from 100) | | | PASS | PASS 0/0/1 | lambda[99] = 0, lambda[100] > 0 | +| 4 square | PASS | | PASS 7 <= 8 | PASS 1.15617668 <= 2.5 | | | | PASS | PASS 7/6/7 | D2 PASS | +| 5 memory | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | M9 PASS: memory 1.79993691 > 0 | +| 6 latency | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | M9 PASS: latency 0 | +| 7A capacity | PASS | | | | | | | PASS | PASS 0/0/1 | lambda 0 throughout | +| 7B capacity | PASS | PASS (0) | | | PASS (from 0) | | | PASS | PASS 0/0/1 | generation 101..200, tick 0..99 | +| 8 regime | PASS | PASS (0) | | | | | PASS | PASS | PASS 0/0/1 | ticks 80..119 FROZEN, lambda/estimate/generation held; UNCALIBRATED resumes at 120 | +| 9 bursty | PASS | | | | | | | PASS | PASS 32/38/39 | | +| 10 fan-out (32 chains) | PASS x32 | PASS (0) x32 | | | | | | PASS x32 | PASS 0/0/1 x32 | M10 PASS: finals in [0.501392531, 0.697240739], spread 0.195848208 <= 0.25 | +| 11 KV | PASS | | | | PASS (from 110) | | | PASS | PASS 0/0/1 | M11 PASS: lambda 0 while estimate <= 5200; first crossing tick 110 | +| 12 recovery | PASS | PASS (0) | | | | PASS K=90: lambda[189] = 0.0157277126 <= 0.0159052715 | | PASS | PASS 1/0/1 | peak 1.59052715 | +| 13 020536Z machine CPU0 | PASS | | | | | | | PASS | PASS 81/1238/1239 | D1 PASS | +| 13 020536Z machine CPU7 | PASS | | | | | | | PASS | PASS 245/1175/1176 | D1 PASS | +| 13 020536Z preflight CPU7 | PASS | | | | | | | PASS | PASS 0/9/10 | D1 PASS | +| 13 025735Z machine CPU0 | PASS | | | | | | | PASS | PASS 91/1269/1270 | D1 PASS | +| 13 025735Z machine CPU7 | PASS | | | | | | | PASS | PASS 197/1228/1229 | D1 PASS | +| 13 025735Z preflight CPU7 | PASS | | | | | | | PASS | PASS 0/13/14 | D1 PASS | + +D1 (two replays, identical replay digest) PASS on every chain above. D2: golden hex constants for scenarios 2 and 4 are +committed in tests/dual/test_dual_replay.c and matched in both the plain and the ASan process. Trace codec: exact round +trip, kind byte 2 / version 2 / truncation / trailing byte refused, NaN / negative uncertainty / flag > 1 / kind 0 / n 0 / +n > 2048 refused, -0.0 digests as +0.0. Importer: "" refused (RX_DUAL_ERR_ENCODING), no matching row +refused, non-matching event rows skipped, generation = row index + 1. + +### Negative control +| Controller | Scenario | M1 | M3a | M3b | M8 | Verdict for this configuration | +|---|---|---|---|---|---|---| +| hostile eta 5.0, rho 0.0 | 4 square | PASS (max 10 = clip) | PASS 7 <= 8 | FAIL peak-to-peak 10 > 2.5 | PASS 7/6/7 | FAIL, exactly as declared | +| reference | 4 square | PASS | PASS 7 | PASS 1.156 | PASS 7/6/7 | PASS | + +Every declared expectation for the negative control held: the hostile controller is bounded (clip works), reverses no more +often than the reference (the pre-stated theorem: reversal counts cannot exceed R(estimate) + 1 for any valid controller), +and fails the amplitude bound by slamming between 0 and lambda_max. + +### Verdict +`DUAL_PRICE_REFERENCE = PASS` on host: every pre-registered measure passed for the reference controller on every scenario and +on every recorded trace, and the negative control failed M3b as declared. No threshold was changed after the +pre-registration commit. + +Scope and limits (stated, not hidden): +- ALL inputs are UNCALIBRATED. Every replayed state is UNCALIBRATED (or FROZEN on regime ticks); none is FRESH except in the + D4 format exercise, which uses a stand-in digest and makes no calibration claim. No price from this gate, and no price from + this module, may influence any production decision until EST-3 passes for the signal and the later DUAL gates pass. +- The recorded traces are perf cycle counters (the R15 CSVs contain no thermal, power or clock-frequency column); the budget + 1e9 cycles per second is a pre-registered stand-in, not a declared resource budget of any contract. +- The regime-change scenario exercises the FROZEN path with an injected flag; it does not establish that EST-9 innovation is a + usable regime detector (ADR 0031 s5.3 leaves that open). +- Capacity reduction is modelled as a new constraint chain under a new budget_contract because a budget is declared by a + contract; the price history of the old chain is not carried into the new one. +- Host-only. No GPU, no runtime linkage, no src/runtime change. diff --git a/mk/.crumb b/mk/.crumb index c9b3fb4ad..3f9d87990 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T18:45:18Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:566d430880c69205dd89c06d645f5030442c526a97ce3e2c386276c282792e9f", + "digest": "sha256:25dc918988768c936cdd41410a68c18df3c9acf7cd129812bc030c6a070cc67c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 52, - "generated_at_commit": "00f54b6b19e5e64e8947aea2890e688511d897a8", - "source_tree": "sha256:e29b35006332b572af186fdd10bdef7ca6a8531766b44d075485e7ded46be9df" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:33ab9148ac46504fa4ff3e51dd4472de84552d8ab5f7e8b8535fb560b85c5058" }, "owner_lane": "", "provenance": { diff --git a/mk/dual.mk b/mk/dual.mk index 7f73d0c2c..e325e3f82 100644 --- a/mk/dual.mk +++ b/mk/dual.mk @@ -71,3 +71,24 @@ test-dual-update: $(DUAL_DIR)/test_dual_update $(DUAL_DIR)/test_dual_update_asan ./$(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 + +# DUAL-1b replay (appended; same fragment) +ifndef DUAL_MK_3 +DUAL_MK_3 := 1 +.PHONY: test-dual-replay +DUAL_REPLAY_SRCS = $(DUAL_SRCS) src/dual/rx_dual_replay.c tests/dual/dual_traces.c +DUAL_REPLAY_HDRS = $(DUAL_HDRS) src/dual/rx_dual_replay.h tests/dual/dual_traces.h tests/dual/dual_fixtures.h +$(DUAL_DIR)/test_dual_replay: tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) $(DUAL_REPLAY_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) -o $@ tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) -lm +$(DUAL_DIR)/test_dual_replay_asan: tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) $(DUAL_REPLAY_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) $(EST_ASAN) -o $@ tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) -lm +# Run from the repository root: the recorded traces are evidence/R15/raw/*/{machine,preflight}-perf.csv. +test-dual-replay: $(DUAL_DIR)/test_dual_replay $(DUAL_DIR)/test_dual_replay_asan + ./$(DUAL_DIR)/test_dual_replay + ./$(DUAL_DIR)/test_dual_replay_asan > /dev/null + @echo "test-dual-replay: DUAL-1b replay, pre-registered measures and negative control pass in plain and ASan/UBSan builds (all inputs UNCALIBRATED)" +dual-purity: $(DUAL_DIR)/rx_dual_replay.o +test-dual: test-dual-replay +endif diff --git a/src/.crumb b/src/.crumb index 2f84e2beb..541f6f71a 100644 --- a/src/.crumb +++ b/src/.crumb @@ -134,7 +134,7 @@ }, { "digest": "779e416daad95fc928a9217a06a2897d29930df83b61423d2496fb71f7c42355", - "last_commit": "f523edd", + "last_commit": "3a3144d", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -170,14 +170,14 @@ } ], "children_root": "sha256:607467f8ea6e425bf33f7a0df65a411055a569e302b8127ab84822a0b4559109", - "compiled_at": "2026-10-04T18:45:18Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:8e9dd6f8117c615273c291427db9ad14abaa907bc05741145bb9892706f68efa", + "digest": "sha256:839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 401, - "generated_at_commit": "00f54b6b19e5e64e8947aea2890e688511d897a8", - "source_tree": "sha256:5613ea31519d6c5e879b868b3b0beee43e6afa8199bd6e4d9fe48b2ecc18a7ff" + "files": 403, + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:e98bb9c0ddc0aa8281597e44376154c18782109c91224cc49e83aa7a59cc1834" }, "owner_lane": "", "provenance": { diff --git a/src/dual/rx_dual_replay.c b/src/dual/rx_dual_replay.c new file mode 100644 index 000000000..b500906c8 --- /dev/null +++ b/src/dual/rx_dual_replay.c @@ -0,0 +1,257 @@ +/* DUAL-1b: digest-bound traces and deterministic replay. */ +#include "rx_dual_replay.h" +#include "sha256.h" + +#include +#include + +/* ---- generator ---- */ +uint64_t rx_dual_xorshift64s(uint64_t *s) +{ + uint64_t x = *s; + x ^= x >> 12; x ^= x << 25; x ^= x >> 27; + *s = x; + return x * 0x2545F4914F6CDD1Dull; +} +double rx_dual_xorshift_unit(uint64_t *s) { return (double)(rx_dual_xorshift64s(s) >> 11) / 9007199254740992.0; } + +/* ---- little-endian encoding (same conventions as rx_dual.c) ---- */ +static void p_u32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static void p_u64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static void p_f64(uint8_t *p, double d) +{ + uint64_t bits; + if (d == 0.0) d = 0.0; /* -0.0 -> +0.0 */ + memcpy(&bits, &d, 8); + p_u64(p, bits); +} +static uint32_t g_u32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = (v << 8) | p[i]; return v; } +static uint64_t g_u64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; } +static double g_f64(const uint8_t *p) { uint64_t bits = g_u64(p); double d; memcpy(&d, &bits, 8); return d; } + +static RxDualStatus check_tick(const RxDualTraceTick *t) +{ + if (!isfinite(t->estimate) || !isfinite(t->uncertainty)) return RX_DUAL_ERR_NONFINITE; + if (t->uncertainty < 0.0) return RX_DUAL_ERR_RANGE; + if (t->calibrated > 1u || t->evidence_verified > 1u || t->regime_change > 1u) return RX_DUAL_ERR_RANGE; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_check_trace(const RxDualTrace *tr) +{ + RxDualStatus st; + if (!tr) return RX_DUAL_ERR_NULL; + if (tr->trace_kind < 1u || tr->trace_kind >= (uint32_t)RX_DUAL_TRACE_MAX_) return RX_DUAL_ERR_KIND; + if (tr->n_ticks < 1u || tr->n_ticks > RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_RANGE; + for (uint32_t i = 0; i < tr->n_ticks; i++) + if ((st = check_tick(&tr->t[i])) != RX_DUAL_OK) return st; + return RX_DUAL_OK; +} + +static void enc_header(const RxDualTrace *tr, uint8_t out[RX_DUAL_TRACE_HEADER_ENCODED]) +{ + out[0] = (uint8_t)RX_DUAL_KIND_TRACE; out[1] = (uint8_t)RX_DUAL_FORMAT_VERSION; + p_u32(out + 2, tr->trace_kind); p_u64(out + 6, tr->seed); p_u32(out + 14, tr->n_ticks); +} +static void enc_tick(const RxDualTraceTick *t, uint8_t out[RX_DUAL_TRACE_TICK_ENCODED]) +{ + p_u64(out, t->generation); p_f64(out + 8, t->estimate); p_f64(out + 16, t->uncertainty); + p_u32(out + 24, t->calibrated); p_u32(out + 28, t->evidence_verified); p_u32(out + 32, t->regime_change); +} + +RxDualStatus rx_dual_encode_trace(const RxDualTrace *tr, uint8_t *buf, size_t cap, size_t *len) +{ + RxDualStatus st; + size_t need; + if (!buf || !len) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + need = RX_DUAL_TRACE_HEADER_ENCODED + (size_t)tr->n_ticks * RX_DUAL_TRACE_TICK_ENCODED; + if (cap < need) return RX_DUAL_ERR_ENCODING; + enc_header(tr, buf); + for (uint32_t i = 0; i < tr->n_ticks; i++) + enc_tick(&tr->t[i], buf + RX_DUAL_TRACE_HEADER_ENCODED + (size_t)i * RX_DUAL_TRACE_TICK_ENCODED); + *len = need; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_decode_trace(const uint8_t *buf, size_t len, RxDualTrace *out) +{ + uint32_t n; + if (!buf || !out) return RX_DUAL_ERR_NULL; + if (len < RX_DUAL_TRACE_HEADER_ENCODED) return RX_DUAL_ERR_ENCODING; + if (buf[0] != (uint8_t)RX_DUAL_KIND_TRACE) return RX_DUAL_ERR_KIND; + if (buf[1] != (uint8_t)RX_DUAL_FORMAT_VERSION) return RX_DUAL_ERR_ENCODING; + memset(out, 0, sizeof *out); + out->trace_kind = g_u32(buf + 2); out->seed = g_u64(buf + 6); n = g_u32(buf + 14); + if (n < 1u || n > RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_RANGE; + if (len != RX_DUAL_TRACE_HEADER_ENCODED + (size_t)n * RX_DUAL_TRACE_TICK_ENCODED) return RX_DUAL_ERR_ENCODING; + out->n_ticks = n; + for (uint32_t i = 0; i < n; i++) { + const uint8_t *p = buf + RX_DUAL_TRACE_HEADER_ENCODED + (size_t)i * RX_DUAL_TRACE_TICK_ENCODED; + RxDualTraceTick *t = &out->t[i]; + t->generation = g_u64(p); t->estimate = g_f64(p + 8); t->uncertainty = g_f64(p + 16); + t->calibrated = g_u32(p + 24); t->evidence_verified = g_u32(p + 28); t->regime_change = g_u32(p + 32); + } + return rx_dual_check_trace(out); +} + +RxDualStatus rx_dual_digest_trace(const RxDualTrace *tr, RxDualDigest *out) +{ + sha256_ctx ctx; + uint8_t zero = 0, hdr[RX_DUAL_TRACE_HEADER_ENCODED], tick[RX_DUAL_TRACE_TICK_ENCODED]; + RxDualStatus st; + if (!out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)RX_DUAL_DOMAIN_TRACE, strlen(RX_DUAL_DOMAIN_TRACE)); + sha256_update(&ctx, &zero, 1); + enc_header(tr, hdr); + sha256_update(&ctx, hdr, sizeof hdr); + for (uint32_t i = 0; i < tr->n_ticks; i++) { enc_tick(&tr->t[i], tick); sha256_update(&ctx, tick, sizeof tick); } + sha256_final(&ctx, out->b); + return RX_DUAL_OK; +} + +/* ---- perf-stat CSV import (bytes in, no locale, no strtod) ---- */ +static int field_eq(const uint8_t *p, size_t n, const char *s) { return strlen(s) == n && memcmp(p, s, n) == 0; } +static int parse_udec(const uint8_t *p, size_t n, double *out) +{ + double v = 0.0; + if (n == 0 || n > 19) return 0; + for (size_t i = 0; i < n; i++) { + if (p[i] < '0' || p[i] > '9') return 0; + v = v * 10.0 + (double)(p[i] - '0'); + } + *out = v; + return 1; +} + +RxDualStatus rx_dual_trace_from_perf_csv(const uint8_t *bytes, size_t len, const char *cpu, const char *event, + RxDualTrace *out) +{ + size_t i = 0; + uint32_t n = 0; + if (!bytes || !cpu || !event || !out) return RX_DUAL_ERR_NULL; + memset(out, 0, sizeof *out); + while (i < len) { + size_t eol = i, f[8], fn = 0, k; + while (eol < len && bytes[eol] != '\n') eol++; + if (eol > i && bytes[i] != '#') { + /* field starts: f[0..fn), terminated by ',' or end of line; need at least 5 fields */ + f[fn++] = i; + for (k = i; k < eol && fn < 8; k++) if (bytes[k] == ',') f[fn++] = k + 1; + if (fn >= 5) { + size_t c0 = f[1], c1 = f[2] - 1, v0 = f[2], v1 = f[3] - 1, e0 = f[4], e1 = (fn > 5) ? f[5] - 1 : eol; + if (field_eq(bytes + c0, c1 - c0, cpu) && field_eq(bytes + e0, e1 - e0, event)) { + double v; + if (n >= RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_FULL; + if (!parse_udec(bytes + v0, v1 - v0, &v)) return RX_DUAL_ERR_ENCODING; /* "" and friends */ + out->t[n].generation = (uint64_t)n + 1u; + out->t[n].estimate = v; + out->t[n].uncertainty = 0.0; + out->t[n].calibrated = 0; /* a counter is not a calibrated estimate */ + out->t[n].evidence_verified = 1; + out->t[n].regime_change = 0; + n++; + } + } + } + i = eol + 1; + } + if (n == 0) return RX_DUAL_ERR_RANGE; + out->trace_kind = RX_DUAL_TRACE_RECORDED; + out->seed = 0; + out->n_ticks = n; + return rx_dual_check_trace(out); +} + +/* ---- replay ---- */ +static void derive(const RxDualDigest *trace, const char *tag, uint32_t i, RxDualDigest *out) +{ + sha256_ctx ctx; uint8_t ib[4]; + p_u32(ib, i); + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)tag, strlen(tag)); + sha256_update(&ctx, trace->b, RX_DUAL_DIGEST_SIZE); + sha256_update(&ctx, ib, 4); + sha256_final(&ctx, out->b); +} + +RxDualStatus rx_dual_replay_run(const RxDualTrace *tr, const RxDualController *ctl, const RxDualResource *res, + RxDualClass cls, double budget, const RxDualDigest *budget_contract, + const RxDualDigest *calibration_ref, RxDualReplay *out) +{ + RxDualStatus st; + RxDualConstraintState cur, nx; + RxDualInput in; + sha256_ctx ctx; + uint8_t zero = 0, nb[4]; + if (!tr || !ctl || !res || !budget_contract || !out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + memset(out, 0, sizeof *out); + if ((st = rx_dual_digest_trace(tr, &out->trace)) != RX_DUAL_OK) return st; + if ((st = rx_dual_digest_controller(ctl, &out->controller_id)) != RX_DUAL_OK) return st; + for (uint32_t i = 0; i < tr->n_ticks; i++) { + const RxDualTraceTick *t = &tr->t[i]; + memset(&in, 0, sizeof in); + derive(&out->trace, "omega.dual.trace.estimate_ref", i, &in.estimate_ref); + derive(&out->trace, "omega.dual.trace.evidence_root", 0, &in.evidence_root); + in.estimate_kind = RX_DUAL_EST_ESTIMATED; + in.unit = res->unit; + in.estimate = t->estimate; + in.uncertainty = t->uncertainty; + in.generation = t->generation; + in.evidence_verified = t->evidence_verified; + in.regime_change = t->regime_change; + if (t->calibrated) { + if (!calibration_ref || rx_dual_digest_is_zero(calibration_ref)) return RX_DUAL_ERR_CALIBRATION; + in.calibration_ref = *calibration_ref; + } + if (i == 0) st = rx_dual_init_state(res, cls, budget, budget_contract, &in, ctl, t->generation, &nx); + else st = rx_dual_update(&cur, res, &in, ctl, t->generation, &nx); + if (st != RX_DUAL_OK) return st; + if ((st = rx_dual_digest_constraint(&nx, &out->record[i])) != RX_DUAL_OK) return st; + out->lambda[i] = nx.lambda; + out->estimate[i] = nx.estimate; + out->generation[i] = nx.generation; + out->state[i] = nx.lambda_state; + cur = nx; + } + out->n = tr->n_ticks; + out->last = cur; + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)RX_DUAL_DOMAIN_REPLAY, strlen(RX_DUAL_DOMAIN_REPLAY)); + sha256_update(&ctx, &zero, 1); + sha256_update(&ctx, out->controller_id.b, RX_DUAL_DIGEST_SIZE); + sha256_update(&ctx, out->trace.b, RX_DUAL_DIGEST_SIZE); + p_u32(nb, out->n); + sha256_update(&ctx, nb, 4); + for (uint32_t i = 0; i < out->n; i++) sha256_update(&ctx, out->record[i].b, RX_DUAL_DIGEST_SIZE); + sha256_final(&ctx, out->replay.b); + return RX_DUAL_OK; +} + +/* ---- measures ---- */ +uint32_t rx_dual_reversals(const double *x, uint32_t lo, uint32_t hi) +{ + uint32_t r = 0; + int last = 0; + if (!x || hi <= lo) return 0; + for (uint32_t i = lo + 1; i < hi; i++) { + double d = x[i] - x[i - 1]; + int s = (d > 0.0) ? 1 : (d < 0.0) ? -1 : 0; + if (s == 0) continue; + if (last != 0 && s != last) r++; + last = s; + } + return r; +} + +double rx_dual_peak_to_peak(const double *x, uint32_t lo, uint32_t hi) +{ + double mn, mx; + if (!x || hi <= lo) return 0.0; + mn = mx = x[lo]; + for (uint32_t i = lo + 1; i < hi; i++) { if (x[i] < mn) mn = x[i]; if (x[i] > mx) mx = x[i]; } + return mx - mn; +} diff --git a/src/dual/rx_dual_replay.h b/src/dual/rx_dual_replay.h new file mode 100644 index 000000000..659d75f14 --- /dev/null +++ b/src/dual/rx_dual_replay.h @@ -0,0 +1,117 @@ +/* DUAL-1b: deterministic replay of the reference controller over digest-bound + * traces (ADR 0031 section 5 and section 8, gate DUAL_PRICE_REFERENCE). + * + * A trace is a bounded, canonically encoded sequence of controller inputs + * (kind byte 8, domain omega.dual.trace.v1). Its digest binds the inputs a + * replay consumed. The replay engine feeds the trace through + * rx_dual_init_state / rx_dual_update and records the digest of every + * resulting RxDualConstraintState; the digest of that log, bound to the + * controller_id and the trace digest, is the replay digest. Same trace, same + * controller, same resource and budget => identical replay digest. + * + * Bounded, no allocation, no globals, no I/O: the recorded-trace importer + * takes bytes the caller already read. Every price produced by a replay is as + * calibrated as its inputs: with calibrated = 0 on every tick (the only honest + * value while EST-3 has not passed) every state is UNCALIBRATED and may never + * influence production. Replay is measurement of the controller, nothing else. */ +#ifndef OMEGA_RX_DUAL_REPLAY_H +#define OMEGA_RX_DUAL_REPLAY_H + +#include "rx_dual_update.h" + +#define RX_DUAL_KIND_TRACE 8 +#define RX_DUAL_DOMAIN_TRACE "omega.dual.trace.v1" +#define RX_DUAL_DOMAIN_REPLAY "omega.dual.replay.v1" +#define RX_DUAL_TRACE_MAX_TICKS 2048u +#define RX_DUAL_TRACE_TICK_ENCODED 36u /* u64 f64 f64 u32 u32 u32 */ +#define RX_DUAL_TRACE_HEADER_ENCODED 18u /* kind ver u32 u64 u32 */ +#define RX_DUAL_TRACE_ENCODED_MAX (RX_DUAL_TRACE_HEADER_ENCODED + RX_DUAL_TRACE_MAX_TICKS * RX_DUAL_TRACE_TICK_ENCODED) + +typedef enum { + RX_DUAL_TRACE_CONSTANT_LOW = 1, + RX_DUAL_TRACE_BINDING = 2, + RX_DUAL_TRACE_STEP = 3, + RX_DUAL_TRACE_SQUARE = 4, + RX_DUAL_TRACE_COMPETING_MEMORY = 5, + RX_DUAL_TRACE_COMPETING_LATENCY = 6, + RX_DUAL_TRACE_CAPACITY_REDUCTION = 7, + RX_DUAL_TRACE_REGIME_CHANGE = 8, + RX_DUAL_TRACE_BURSTY = 9, + RX_DUAL_TRACE_FANOUT = 10, + RX_DUAL_TRACE_KV_SATURATION = 11, + RX_DUAL_TRACE_RECOVERY = 12, + RX_DUAL_TRACE_RECORDED = 13, + RX_DUAL_TRACE_MAX_ = 14 +} RxDualTraceKind; + +typedef struct { + uint64_t generation; /* World generation the input claims; also used as now_generation */ + double estimate; /* finite */ + double uncertainty; /* finite, >= 0 */ + uint32_t calibrated; /* 0 or 1; 0 = no EST-3 receipt: the state can only be UNCALIBRATED */ + uint32_t evidence_verified; /* 0 or 1 */ + uint32_t regime_change; /* 0 or 1 */ +} RxDualTraceTick; + +typedef struct { + uint32_t trace_kind; /* RxDualTraceKind */ + uint64_t seed; /* generator seed (0 for recorded) */ + uint32_t n_ticks; /* 1..RX_DUAL_TRACE_MAX_TICKS */ + RxDualTraceTick t[RX_DUAL_TRACE_MAX_TICKS]; +} RxDualTrace; + +/* xorshift64*: the one generator every synthetic trace uses. State must be nonzero. */ +uint64_t rx_dual_xorshift64s(uint64_t *state); +/* Uniform double in [0, 1) from one generator step (53 bits). */ +double rx_dual_xorshift_unit(uint64_t *state); + +RxDualStatus rx_dual_check_trace(const RxDualTrace *tr); +RxDualStatus rx_dual_encode_trace(const RxDualTrace *tr, uint8_t *buf, size_t cap, size_t *len); +RxDualStatus rx_dual_decode_trace(const uint8_t *buf, size_t len, RxDualTrace *out); +/* SHA-256(domain || 0x00 || encoding), streamed; no buffer needed. */ +RxDualStatus rx_dual_digest_trace(const RxDualTrace *tr, RxDualDigest *out); + +/* Import one (cpu, event) series from perf-stat CSV bytes: + *