diff --git a/.crumb b/.crumb index f3e51fe46..e137753b9 100644 --- a/.crumb +++ b/.crumb @@ -71,39 +71,39 @@ "purpose": "Directory research. Purpose not yet described by a human." }, { - "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "digest": "c35cac90c8386d8ef0a0ea92522a79ac2db07623b1cb57591699a2bd0d2ddcb7", + "last_commit": "050da8309", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "a53455738fef88bd837247b7b8e39ab9d08185e50f07c81c3f2acbc84ad54b26", + "last_commit": "050da8309", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "8ca963d00e040dc184d0303007d901f2124f49ef78dd286410eedb51f9363837", + "last_commit": "050da8309", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:38e6d24b5a8f944ef49c1b4a739c2f83ed6da3f45f397415b98c6789254d9c08", + "compiled_at": "2026-10-30T13:09:42Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:3721fec6e22e3492f19a02273bf0e65033dc52c2bb2c3cbff01d779e5be9dc39", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "generated_at_commit": "050da8309bbe326f8e662e6e012a7895026bb2b6", + "source_tree": "sha256:2cce73b804b0990a1f818d7ede952db2b0c822fbdc21059f7f89fe3187d03a9e" }, "owner_lane": "", "provenance": { diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dda6d20b5..3b783e173 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,6 +5,7 @@ Every model below did real work. A model is listed only for work it did. The orc | Model | What it did | |---|---| +| Grok 4.7 | Merged current main (`62d1d0d66`) into #934. Conflicts were generated crumbs (`.crumb`, `spec/.crumb`, `src/.crumb`, `tests/.crumb`) and CONTRIBUTORS.md. The Makefile kept the test-costmodel mutant block and the test-dagsim block. Regenerated crumbs with the crumb.lock tool (`d0f0b95d`). | | Grok 4.7 | Merged current main (`089e5b053`) into #936. Conflicts were generated crumbs (`.crumb`, `tests/.crumb`); regenerated them with the crumb.lock tool (`d0f0b95d`). | | Grok 4.7 | Restored the offline DAG evaluator's forbidden-symbol guard from #590 (`3bd56731`): generation store, authority, world, and calls that map or run code. | | Grok 4.7 | Imported the offline DAG evaluator `tools/dagsim` verbatim from closed PR #590 (`3bd56731`) and wired a host `test-dagsim` target. Analytical fixture checks only. No validated cost claim. | @@ -49,3 +50,4 @@ Update this table in the same commit as the work it credits. | Codex | Integrated FR-9 with compiler batch 3 and the latest quantity/diagnostic audit; preserved empty text and integer-bitcast behavior on x86 and kept seed bootstrap within its function budget by removing unused helpers and inlining one single-use helper. | | Codex | Fixed x86 quantity unary refusals and empty-text emission; added explicit per-target acceptance contracts, preserved historical failed experiments, and exercised missing diagnostic callers and stale-binding controls. | | Grok 4.7 | Merged current main (b3dca0f1) into #514. The only conflict was the generated root `.crumb`; regenerated crumbs with the crumb.lock tool (d0f0b95d). | +| Grok 4.7 | Cost-model refusal of unsupported semantic operation IDs before prediction or learning, with a clamp-restoring mutant (Refs #704) | diff --git a/Makefile b/Makefile index 6b53cd124..b73f0b794 100644 --- a/Makefile +++ b/Makefile @@ -1248,6 +1248,18 @@ $(RX_CM_UNIT): tests/runtime/rx_costmodel_unit.c $(RX_CM_OBJ) src/omega_om_units test-costmodel: $(RX_CM_UNIT) ./$(RX_CM_UNIT) + @set -eu; dir=$(OUT_DIR)/costmodel-mutant; rm -rf $$dir; mkdir -p $$dir; \ + sed "/CM704:refuse-unsupported-op/c\\ uint32_t op = f && f->op < RX_CM_OPS ? f->op : RX_CM_OPS - 1u; /* CM704:refuse-unsupported-op */" \ + src/runtime/rx_costmodel.c > $$dir/rx_costmodel.c; \ + if cmp -s src/runtime/rx_costmodel.c $$dir/rx_costmodel.c; then \ + echo "mutant restore-op-clamp: sed changed nothing"; exit 1; fi; \ + $(CC) $(CFLAGS) -c -o $$dir/rx_costmodel.o $$dir/rx_costmodel.c; \ + $(CC) $(CFLAGS) -o $$dir/unit tests/runtime/rx_costmodel_unit.c $$dir/rx_costmodel.o src/omega_om_units.c -lm; \ + rc=0; $$dir/unit refuse > $$dir/out.txt 2> $$dir/err.txt || rc=$$?; \ + if [ $$rc -eq 0 ] || ! grep -q unsupported-op-not-aliased-to-matvec $$dir/err.txt; then \ + echo "mutant restore-op-clamp was not killed by unsupported-op-not-aliased-to-matvec (exit $$rc)"; \ + cat $$dir/err.txt; exit 1; fi; \ + echo "mutant restore-op-clamp: killed by unsupported-op-not-aliased-to-matvec (exit $$rc)" # Offline resource-DAG evaluator, imported verbatim from closed PR #590 # (commit 3bd56731). Host arithmetic on the numbers written in the fixtures. diff --git a/spec/.crumb b/spec/.crumb index a226ce192..18b524e26 100644 --- a/spec/.crumb +++ b/spec/.crumb @@ -20,14 +20,14 @@ } ], "children_root": "sha256:6834a3241647d3a098283d4eafedbdb1428dfa4d834f9ed5f22025b6d2303093", - "compiled_at": "2026-10-30T11:28:28Z", + "compiled_at": "2026-10-30T13:07:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", + "digest": "sha256:c35cac90c8386d8ef0a0ea92522a79ac2db07623b1cb57591699a2bd0d2ddcb7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 63, - "generated_at_commit": "a8a6a3d3c47ce797430f045edc5eae11594ba36d", - "source_tree": "sha256:4cb3bb1977c84caacc34366f814d6fe403a2eda7b89b916948d29541675c59bb" + "generated_at_commit": "a683ffe6dcb72632a4f56ae82f0511786921bfa0", + "source_tree": "sha256:10c3b9d33e4a7a671675928ed8a92b8d5e00a304f89728becf92d79bcd0cacc0" }, "owner_lane": "", "provenance": { diff --git a/spec/empirical-optimizer.md b/spec/empirical-optimizer.md index 5b23f32cb..798ed1d98 100644 --- a/spec/empirical-optimizer.md +++ b/spec/empirical-optimizer.md @@ -83,6 +83,15 @@ model is a fixed 28,112-byte blob, well under the 64 KiB generation limit. √(σ²(1 + φ'Λ⁻¹φ)) × `sd_scale`. The leverage term widens the spread away from the training data. σ² has a floor of 0.03² (about 2%). - **A cell** needs 12 observations (`RX_CM_MIN_OBS`) before it predicts. +- **An unknown semantic operation** is not a cell. `rx_cm_cell` returns + `RX_CM_CELL_NONE`, which is never used as an index. Prediction, decision + and observation refuse it (`RX_CM_ERR_UNSUPPORTED`, decision `RX_CM_REFUSE`) + and do not read or write another family's statistics. An unknown core class + is still the OTHER bucket, and pressure past the last bucket still saturates. + Those are declared buckets, not a catch-all operation. Adding a family + widens `RX_CM_OPS`, changes `RX_CM_CELLS`, and bumps the blob version. A + blob whose version, basis size, cell count, arm cap or core count disagrees + is refused. - **Pressure cells** do not refit the whole curve. They hold a two-number correction over the quiet cell of the same core class: log2 cost under pressure = quiet prediction + a + b × (doublings past cache). Four diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..32f0170d2 100644 --- a/src/.crumb +++ b/src/.crumb @@ -145,8 +145,8 @@ "purpose": "Ionic-droplet reservoir simulator (omega#440): binary32 fixed-step RK4 of the authors' droplet m" }, { - "digest": "1e400d9b84494347892448fd576be7bf596ef810ed6e88bcd70f7e7d9a6b5746", - "last_commit": "97a960f5c", + "digest": "43126cdfbfc4a67b3ab8785a1358a861960954e0374b7118781d06f117f9de5c", + "last_commit": "b99ffb73f", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -187,15 +187,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:2f729945214be989a74d7c2064ae4ba3c2deb52ebea3f334b459198a7bd032ed", - "compiled_at": "2026-10-30T02:49:13Z", + "children_root": "sha256:0af477389a15f0e4f2ad9101e55195d4d07c9650af7d8e617bd67b0dc96e0b37", + "compiled_at": "2026-10-30T13:07:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", + "digest": "sha256:a53455738fef88bd837247b7b8e39ab9d08185e50f07c81c3f2acbc84ad54b26", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 568, - "generated_at_commit": "2cb1ccb5d3c1a1ce1cb94a7a3dd3c221395c4e15", - "source_tree": "sha256:7f1fef46a27e8baf5cf567bb590d417ede3767c5f07e714d0f0812af0645bb4b" + "generated_at_commit": "a683ffe6dcb72632a4f56ae82f0511786921bfa0", + "source_tree": "sha256:11e4a7fa2937947bc41072570791d300d75f1fb5673f0befcf90862f0d6f428b" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index a82872991..bcd420c68 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-25T19:38:33Z", + "compiled_at": "2026-10-30T11:21:25Z", "compiler": "crumb-compile/1", - "digest": "sha256:1e400d9b84494347892448fd576be7bf596ef810ed6e88bcd70f7e7d9a6b5746", + "digest": "sha256:43126cdfbfc4a67b3ab8785a1358a861960954e0374b7118781d06f117f9de5c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 65, - "generated_at_commit": "97a960f5c18fc09b506168c55c10031b3f0cffc3", - "source_tree": "sha256:fc3f521b52b9fddaf86ad8b95b05c8060b03b84dc5117e2655ec37ae03c5323b" + "generated_at_commit": "dd39bf71fb8eb14c45bf0abd20c74d5da69c807c", + "source_tree": "sha256:701918e639effadd136bf5e99dd06480103741211efce5e5232f7521aab82c87" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_costmodel.c b/src/runtime/rx_costmodel.c index d8ddf956d..2a5df36d9 100644 --- a/src/runtime/rx_costmodel.c +++ b/src/runtime/rx_costmodel.c @@ -27,6 +27,9 @@ #include #include +_Static_assert(RX_CM_CELL_NONE >= RX_CM_CELLS, + "unsupported-op sentinel must not be a cell index"); + #define PRIOR_A 1.0 #define PRIOR_B 0.01 /* prior noise variance ~0.01 log2^2 (about 7%) */ #define NOISE_FLOOR 0.0009 /* never claim better than ~2% sd per call */ @@ -53,9 +56,12 @@ void rx_cm_init(RxCostModel *m, uint32_t n_arms) { } uint32_t rx_cm_cell(const RxCmFeatures *f) { - uint32_t op = f->op < RX_CM_OPS ? f->op : RX_CM_OPS - 1; + /* An unknown semantic operation is not the last supported family. + * The restore-op-clamp mutant rewrites the next line. */ + uint32_t op = f && f->op < RX_CM_OPS ? f->op : RX_CM_CELL_NONE; /* CM704:refuse-unsupported-op */ + if (op >= RX_CM_OPS) return RX_CM_CELL_NONE; uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; - uint32_t pr = f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1; + uint32_t pr = f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1u; return (op * RX_CM_CORES + core) * RX_CM_PRESSURE + pr; } @@ -168,7 +174,9 @@ static int quiet_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t a double *sd, double *dof, uint64_t *n) { RxCmFeatures g = *f; g.pressure = 0; - const RxCmArmStats *b = &m->cell[rx_cm_cell(&g)][arm]; + uint32_t cell = rx_cm_cell(&g); + if (cell >= RX_CM_CELLS) return 0; + const RxCmArmStats *b = &m->cell[cell][arm]; double phi[RX_CM_D]; rx_cm_basis(f, phi); if (b->n < RX_CM_MIN_OBS || !fit_predict(b, phi, mean, sd, dof)) return 0; @@ -180,18 +188,24 @@ static uint32_t bucket(const RxCmFeatures *f) { return f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1; } -void rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, - RxCmPrediction *out) { +int rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, + RxCmPrediction *out) { + if (!out) return RX_CM_ERR_ARG; memset(out, 0, sizeof *out); - if (arm >= m->n_arms) return; + if (!m || !f || arm >= m->n_arms) return RX_CM_ERR_ARG; + uint32_t cell = rx_cm_cell(f); + if (cell >= RX_CM_CELLS) { + out->refused = 1; + return RX_CM_ERR_UNSUPPORTED; + } double mean = 0, sd = 0, dof = 0; uint64_t n = 0; out->fail_p = fail_p(m, arm); - if (!quiet_predict(m, f, arm, &mean, &sd, &dof, &n)) return; + if (!quiet_predict(m, f, arm, &mean, &sd, &dof, &n)) return RX_CM_OK; if (bucket(f) == 0) { out->known = 1; } else { - const RxCmArmStats *r = &m->cell[rx_cm_cell(f)][arm]; + const RxCmArmStats *r = &m->cell[cell][arm]; double phi[RX_CM_D], rm = 0, rsd = 0, rdof = 0; residual_basis(f, phi); if (r->n >= RX_CM_RES_MIN_OBS && fit_predict(r, phi, &rm, &rsd, &rdof)) { @@ -214,11 +228,14 @@ void rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; double mw = m->power_mw[core][arm]; if (mw > 0.0) out->energy_pj = exp2(mean) * mw * 1e-3; /* ps * mW = 1e-3 pJ */ + return RX_CM_OK; } int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o) { if (!m || !f || !o || arm >= m->n_arms) return RX_CM_ERR_ARG; - RxCmArmStats *s = &m->cell[rx_cm_cell(f)][arm]; + uint32_t cell = rx_cm_cell(f); + if (cell >= RX_CM_CELLS) return RX_CM_ERR_UNSUPPORTED; + RxCmArmStats *s = &m->cell[cell][arm]; s->trials++; if (o->failed) { s->failures++; @@ -302,6 +319,16 @@ static double expected_improvement(double d, double s) { void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, int allow_measure, RxCmDecision *out) { memset(out, 0, sizeof *out); + if (m && f && rx_cm_cell(f) >= RX_CM_CELLS) { + uint32_t na = m->n_arms; + out->action = RX_CM_REFUSE; + out->why = RX_CM_WHY_UNSUPPORTED_OP; + out->arm = na; + out->measure[0] = na; + out->measure[1] = na; + for (uint32_t a = 0; a < RX_CM_ARMS; a++) out->pred[a].refused = 1; + return; + } uint32_t fb = p->fallback_arm < m->n_arms ? p->fallback_arm : 0; uint32_t elig = (f->eligible | (1u << fb)) & ((1u << m->n_arms) - 1u); int have[RX_CM_ARMS] = { 0 }; @@ -436,6 +463,10 @@ void rx_cm_charge(RxCostModel *m, uint64_t work_ps, uint64_t explore_ps) { /* ---- canonical bytes ---- */ #define MAGIC 0x4d43474fu /* "OGCM" little-endian */ +/* VERSION binds the feature basis and the cell layout. RX_CM_CELLS already + * includes RX_CM_OPS, so a new operation family changes the blob length and + * this header's cells field. Bump VERSION in that same change. Deserialize + * refuses a different version, basis size, cell count, arm cap or core count. */ #define VERSION 1u typedef struct { diff --git a/src/runtime/rx_costmodel.h b/src/runtime/rx_costmodel.h index 6e2c0db52..162b5f053 100644 --- a/src/runtime/rx_costmodel.h +++ b/src/runtime/rx_costmodel.h @@ -50,7 +50,10 @@ #include #define RX_CM_ARMS 8u -#define RX_CM_OPS 1u /* matvec only today; widen with a second operation */ +#define RX_CM_OPS 1u /* matvec only. A new family widens this, changes + * RX_CM_CELLS, and must bump the blob version. + * An id outside 0 .. RX_CM_OPS-1 is refused; + * it is not aliased onto the last family. */ #define RX_CM_CORES 3u #define RX_CM_PRESSURE 3u #define RX_CM_CELLS (RX_CM_OPS * RX_CM_CORES * RX_CM_PRESSURE) @@ -64,14 +67,16 @@ #define RX_CM_ERR_ARG -1 #define RX_CM_ERR_FORMAT -2 #define RX_CM_ERR_DIGEST -3 +#define RX_CM_ERR_UNSUPPORTED -4 /* semantic operation id is not a modeled family */ #define RX_CM_SKIPPED 1 /* observe: pressure data with no quiet model to correct */ +#define RX_CM_CELL_NONE UINT32_MAX /* not an index into RxCostModel.cell */ enum { RX_CM_OP_MATVEC = 0 }; enum { RX_CM_CORE_X925 = 0, RX_CM_CORE_A725 = 1, RX_CM_CORE_OTHER = 2 }; /* What the caller knows about one call. Fields the matvec workload cannot * vary are carried and recorded, never used to fit (see spec). */ typedef struct { - uint32_t op; /* semantic operation class */ + uint32_t op; /* semantic operation class; only ids below RX_CM_OPS */ uint32_t M, N; /* input shape; branch count is 0 for matvec */ uint64_t state_bytes; /* working set: (M*N + N + M) * 8 */ uint64_t cache_bytes; /* last private cache of this core class */ @@ -101,18 +106,20 @@ typedef struct { int borrowed; /* unseen pressure: the quiet prediction, widened */ double energy_pj; /* predicted energy per call (0 if no power figure) */ double fail_p; /* posterior mean failure probability */ + int refused; /* 1: this operation id is not modeled */ } RxCmPrediction; -enum { RX_CM_SELECT = 1, RX_CM_MEASURE = 2, RX_CM_FALLBACK = 3 }; +enum { RX_CM_SELECT = 1, RX_CM_MEASURE = 2, RX_CM_FALLBACK = 3, RX_CM_REFUSE = 4 }; -/* Why a MEASURE or FALLBACK was returned. */ +/* Why a MEASURE, FALLBACK or REFUSE was returned. */ enum { RX_CM_WHY_CONFIDENT = 0, RX_CM_WHY_UNKNOWN_ARM, /* an eligible arm has too few observations here */ RX_CM_WHY_WORTH_MEASURING, /* expected improvement from measuring exceeds ei_min */ RX_CM_WHY_BUDGET_SPENT, /* would measure, but the exploration budget is spent */ RX_CM_WHY_NO_ELIGIBLE, /* nothing but the fallback may run */ - RX_CM_WHY_OVER_BUDGET /* no arm is predicted to meet the latency/energy budget */ + RX_CM_WHY_OVER_BUDGET, /* no arm is predicted to meet the latency/energy budget */ + RX_CM_WHY_UNSUPPORTED_OP /* semantic operation id is not a modeled family */ }; typedef struct { @@ -165,19 +172,31 @@ typedef struct { void rx_cm_default_policy(RxCmPolicy *p); void rx_cm_init(RxCostModel *m, uint32_t n_arms); -/* Fold one measurement in. 0 on success; RX_CM_SKIPPED for a pressure - * measurement when there is no quiet model yet to correct. */ +/* Fold one measurement in. RX_CM_OK on success; RX_CM_SKIPPED for a pressure + * measurement when there is no quiet model yet to correct. + * RX_CM_ERR_UNSUPPORTED when `f->op` is not a modeled family: no sufficient + * statistic, trial, failure, exploration counter or other model field changes. + * RX_CM_ERR_ARG for a null argument, an arm this model does not have, or a + * successful observation with zero latency. */ int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o); -/* Predict one arm. Needs RX_CM_MIN_OBS quiet observations for the core class. - * Under pressure, adds the learned correction once it has RX_CM_RES_MIN_OBS - * observations; before that, widens by RX_CM_UNSEEN_SHIFT and marks it - * borrowed. */ -void rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *out); - -/* Decide. `allow_measure` 0 freezes the model's behaviour: it never asks to - * measure and picks the best prediction. The fallback arm is chosen when no - * other eligible arm has any prediction at all. */ +/* Predict one arm. Returns RX_CM_ERR_UNSUPPORTED and sets out->refused when + * the semantic operation is not a modeled family. That output is zeros aside + * from `refused`: no mean, spread, failure rate or ranking is borrowed from + * another operation. Returns RX_CM_ERR_ARG for a null argument or an arm this + * model does not have. Otherwise returns RX_CM_OK. + * A supported operation needs RX_CM_MIN_OBS quiet observations for the core + * class before out->known is set. Under pressure, adds the learned correction + * once it has RX_CM_RES_MIN_OBS observations; before that, widens by + * RX_CM_UNSEEN_SHIFT and marks it borrowed. */ +int rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *out); + +/* Decide. An unsupported semantic operation sets RX_CM_REFUSE and + * RX_CM_WHY_UNSUPPORTED_OP, leaves every prediction refused, and does not + * rank arms (`arm` and `measure[]` are set to n_arms, not a selection). + * `allow_measure` 0 freezes the model's behaviour for a supported operation: + * it never asks to measure and picks the best prediction. The fallback arm is + * chosen when no other eligible arm has any prediction at all. */ void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, int allow_measure, RxCmDecision *out); @@ -199,6 +218,13 @@ void rx_cm_digest(const RxCostModel *m, uint8_t out[32]); /* Basis for these features, exposed so tests can see exactly what is fit. */ void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]); + +/* Cell index for these features. RX_CM_CELL_NONE if `f` is null or `f->op` + * is not one of the RX_CM_OPS modeled families. That sentinel is never a + * legal index of RxCostModel.cell. An unknown core class maps to + * RX_CM_CORE_OTHER. Pressure above the last bucket saturates into that + * bucket. Those two mappings are hardware and pressure buckets, not a + * catch-all operation family. */ uint32_t rx_cm_cell(const RxCmFeatures *f); /* Two-sided central interval half-width multiplier for coverage q with diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..1c82edd25 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -265,8 +265,8 @@ "purpose": "Reservoir tests (omega#440): Gate 1 simulator and readout checks against an independent binary64" }, { - "digest": "10d19641e4bb5b435ec6363d4ea7d3c1596af47d97b86f4de35d6ae57770241e", - "last_commit": "45f7d7691", + "digest": "6653865f3df7dcf70eb25bb3db3ca4a5ac5fd52c89bbf211c9f64d83b5c4c285", + "last_commit": "b99ffb73f", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -301,15 +301,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:545723dcb437ee41cde61be01da0fcd83d5e76363b747bef2a2f53e92cf3979c", + "compiled_at": "2026-10-30T13:07:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:8ca963d00e040dc184d0303007d901f2124f49ef78dd286410eedb51f9363837", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "generated_at_commit": "a683ffe6dcb72632a4f56ae82f0511786921bfa0", + "source_tree": "sha256:08ce18f020845672fb87bab3883c683adcadc5182d06fcdadbafe8d447b1c337" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 9dce17c38..1af500bb4 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-26T08:13:25Z", + "compiled_at": "2026-10-30T11:23:02Z", "compiler": "crumb-compile/1", - "digest": "sha256:10d19641e4bb5b435ec6363d4ea7d3c1596af47d97b86f4de35d6ae57770241e", + "digest": "sha256:6653865f3df7dcf70eb25bb3db3ca4a5ac5fd52c89bbf211c9f64d83b5c4c285", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 70, - "generated_at_commit": "45f7d76919042ef821cca9ccc75c2bbf7183f712", - "source_tree": "sha256:6d63521e1c1f8eea1bf172d18bde2e6f4fc7c084e9506bc1cc088f8b9ff339d6" + "generated_at_commit": "dd39bf71fb8eb14c45bf0abd20c74d5da69c807c", + "source_tree": "sha256:4869fe8767f35e77b054054425264fe343b2b5ed679fbef28087c527e64a7bd2" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_costmodel_unit.c b/tests/runtime/rx_costmodel_unit.c index 36907aae2..aa7bbf361 100644 --- a/tests/runtime/rx_costmodel_unit.c +++ b/tests/runtime/rx_costmodel_unit.c @@ -20,7 +20,10 @@ * Any host. Checks fit, calibration of the predictive intervals on held-out * points, the decision rules (select, measure when unsure, frozen, budget, * eligibility, failures, fallback, latency/energy budgets) and the canonical - * blob (round trip, tamper, truncation, version). + * blob (round trip, tamper, truncation, version, cell count). An unsupported + * semantic operation is refused. `make test-costmodel` restores the old + * matvec clamp in a mutant; that binary must fail + * unsupported-op-not-aliased-to-matvec. */ #include "runtime/rx_costmodel.h" #include "sha256.h" @@ -108,8 +111,19 @@ static void t_basis(void) { CHECK(phi[5] == 1.0 && phi[6] == 1.0 && fabs(phi[7] - 2.0) < 1e-12, "basis indicators"); CHECK(rx_cm_cell(&f) == 1u * RX_CM_PRESSURE + 1u, "cell index"); f.core = 99; + f.pressure = 0; + CHECK(rx_cm_cell(&f) == (RX_CM_OP_MATVEC * RX_CM_CORES + RX_CM_CORE_OTHER) * RX_CM_PRESSURE, + "out-of-range core is the OTHER class"); + f.core = RX_CM_CORE_A725; f.pressure = 99; - CHECK(rx_cm_cell(&f) < RX_CM_CELLS, "out-of-range features clamp into a cell"); + CHECK(rx_cm_cell(&f) == (RX_CM_OP_MATVEC * RX_CM_CORES + RX_CM_CORE_A725) * RX_CM_PRESSURE + + (RX_CM_PRESSURE - 1u), + "out-of-range pressure saturates to the last bucket"); + f.op = RX_CM_OPS; + CHECK(rx_cm_cell(&f) == RX_CM_CELL_NONE && RX_CM_CELL_NONE >= RX_CM_CELLS, + "first unsupported op is not a cell index"); + f.op = UINT32_MAX; + CHECK(rx_cm_cell(&f) == RX_CM_CELL_NONE, "UINT32_MAX op is not a cell index"); } static void t_fit_and_calibration(int exact, double *cov80, double *cov95, double *rmse) { @@ -312,7 +326,8 @@ static void t_eligibility_property(void) { for (int i = 0; i < 400; i++) rx_cm_observe(&m, &f, 0, &bad); f.eligible = 0x1; rx_cm_decide(&m, &pol, &f, 1, &d); - CHECK(d.action == RX_CM_FALLBACK && d.arm == 0, "the fallback stays available"); + CHECK(d.action == RX_CM_FALLBACK && d.arm == 0 && d.why == RX_CM_WHY_NO_ELIGIBLE, + "the fallback stays available"); } static void t_budgets(void) { @@ -433,20 +448,262 @@ static void t_blob(void) { b[4] = 9; /* version */ sha256_hash(b, n - 32, b + n - 32); CHECK(rx_cm_deserialize(&r, b, n) == RX_CM_ERR_FORMAT, "unknown version refused"); + memcpy(b, a, n); + /* Header.cells is the fifth uint32. A wider operation family changes it. */ + uint32_t cells = 0; + memcpy(&cells, b + 16, sizeof cells); + cells += 1u; + memcpy(b + 16, &cells, sizeof cells); + sha256_hash(b, n - 32, b + n - 32); + CHECK(rx_cm_deserialize(&r, b, n) == RX_CM_ERR_FORMAT, "cell-count mismatch refused"); free(a); free(b); } -int main(void) { - double c80, c95, rmse; - t_basis(); - t_fit_and_calibration(1, &c80, &c95, &rmse); - t_fit_and_calibration(0, &c80, &c95, &rmse); - t_decide(); - t_eligibility_property(); - t_budgets(); - t_calibrate(); - t_blob(); +/* Issue 704: an unsupported semantic operation used to clamp onto matvec, so a + * learned matvec prediction was reported for it and its observations trained + * that cell. `make test-costmodel` rebuilds rx_costmodel.c with that clamp + * restored; the named assertion unsupported-op-not-aliased-to-matvec must fail. + * Decision modes for a supported operation are asserted here without the shared + * rng. t_decide, t_budgets and t_eligibility cover the same modes on the + * synthetic fit. */ +static RxCmFeatures repro_features(uint32_t op) { + RxCmFeatures f; + memset(&f, 0, sizeof f); + f.op = op; + f.M = 64; + f.N = 64; + f.state_bytes = (64ull * 64ull + 64ull + 64ull) * 8ull; + f.cache_bytes = 1048576ull; + f.core = RX_CM_CORE_X925; + f.eligible = 1; + f.need_evidence = 1; + return f; +} + +static void t_refuse_unsupported_op(void) { + RxCostModel m; + rx_cm_init(&m, 1); + RxCmFeatures good = repro_features(RX_CM_OP_MATVEC); + RxCmObservation seen = { 1000000ull, 0 }; + for (int i = 0; i < 20; i++) + CHECK(rx_cm_observe(&m, &good, 0, &seen) == RX_CM_OK, "matvec observation accepted"); + m.explore_ps = 42; + m.work_ps = 99; + + RxCmPrediction learned; + int lr = rx_cm_predict(&m, &good, 0, &learned); + CHECK(lr == RX_CM_OK && learned.known && !learned.refused && learned.n == 20 && + learned.mean_log2_ps > 19.0 && learned.mean_log2_ps < 21.0, + "valid matvec prediction is learned (rc %d known %d n %u mean %.6f)", lr, learned.known, + learned.n, learned.mean_log2_ps); + + RxCmFeatures bad = good; + bad.op = UINT32_MAX; + RxCmPrediction aliased; + int ur = rx_cm_predict(&m, &bad, 0, &aliased); + RxCostModel snap = m; + RxCmObservation failed = { 1000000ull, 1 }; + RxCmObservation zero_fail = { 0, 1 }; + int orc = rx_cm_observe(&m, &bad, 0, &seen); + int frc = rx_cm_observe(&m, &bad, 0, &failed); + int zrc = rx_cm_observe(&m, &bad, 0, &zero_fail); + CHECK(ur == RX_CM_ERR_UNSUPPORTED && aliased.refused && !aliased.known && !aliased.borrowed && + aliased.mean_log2_ps == 0.0 && aliased.n == 0 && aliased.fail_p == 0.0 && + rx_cm_cell(&bad) == RX_CM_CELL_NONE && + orc == RX_CM_ERR_UNSUPPORTED && frc == RX_CM_ERR_UNSUPPORTED && + zrc == RX_CM_ERR_UNSUPPORTED && memcmp(&m, &snap, sizeof m) == 0 && + m.cell[0][0].n == 20 && m.cell[0][0].trials == 20 && m.cell[0][0].failures == 0, + "unsupported-op-not-aliased-to-matvec"); + + size_t blob_n = rx_cm_blob_size(), got = 0; + uint8_t *blob0 = malloc(blob_n), *blob1 = malloc(blob_n); + CHECK(blob0 && blob1 && rx_cm_serialize(&snap, blob0, blob_n, &got) == RX_CM_OK && got == blob_n, + "serialize before rejected observations"); + CHECK(blob0 && blob1 && rx_cm_serialize(&m, blob1, blob_n, &got) == RX_CM_OK && + memcmp(blob0, blob1, blob_n) == 0, + "frozen model serialization identical after rejected observations"); + free(blob0); + free(blob1); + + bad.op = RX_CM_OPS; + RxCmPrediction first; + int fr = rx_cm_predict(&m, &bad, 0, &first); + snap = m; + orc = rx_cm_observe(&m, &bad, 0, &seen); + frc = rx_cm_observe(&m, &bad, 0, &failed); + CHECK(fr == RX_CM_ERR_UNSUPPORTED && first.refused && !first.known && + rx_cm_cell(&bad) == RX_CM_CELL_NONE && orc == RX_CM_ERR_UNSUPPORTED && + frc == RX_CM_ERR_UNSUPPORTED && memcmp(&m, &snap, sizeof m) == 0, + "first unsupported id RX_CM_OPS is refused"); + + /* A supported observation still trains matvec after the refusals. */ + CHECK(rx_cm_observe(&m, &good, 0, &seen) == RX_CM_OK && m.cell[0][0].n == 21, + "supported matvec observation still trains"); + + RxCmPolicy pol; + rx_cm_default_policy(&pol); + RxCmDecision d; + bad.op = UINT32_MAX; + rx_cm_decide(&m, &pol, &bad, 1, &d); + CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.arm == m.n_arms && + d.measure[0] == m.n_arms && d.measure[1] == m.n_arms && d.expected_improvement == 0.0 && + d.pred[0].refused && !d.pred[0].known && !d.pred[0].borrowed && + d.pred[0].mean_log2_ps == 0.0, + "decide refuses UINT32_MAX without ranking"); + rx_cm_decide(&m, &pol, &bad, 0, &d); + CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP, + "frozen decide refuses UINT32_MAX"); + bad.op = RX_CM_OPS; + rx_cm_decide(&m, &pol, &bad, 1, &d); + CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.pred[0].refused, + "decide refuses RX_CM_OPS"); + + /* Pressure on a real matvec is still a residual. Unsupported pressure is not. */ + RxCostModel quiet; + rx_cm_init(&quiet, 1); + RxCmFeatures qf = repro_features(RX_CM_OP_MATVEC); + RxCmObservation exact = { (uint64_t)exp2(20.0), 0 }; + for (int i = 0; i < 16; i++) rx_cm_observe(&quiet, &qf, 0, &exact); + RxCmFeatures unseen = qf; + unseen.pressure = 2; + RxCmPrediction pq, p0; + rx_cm_predict(&quiet, &qf, 0, &p0); + rx_cm_predict(&quiet, &unseen, 0, &pq); + CHECK(pq.borrowed && !pq.refused && pq.mean_log2_ps == p0.mean_log2_ps, + "unseen matvec pressure borrows the quiet prediction"); + RxCmFeatures pressed = qf; + pressed.pressure = 1; + RxCostModel bare; + rx_cm_init(&bare, 1); + CHECK(rx_cm_observe(&bare, &pressed, 0, &exact) == RX_CM_SKIPPED && + bare.cell[rx_cm_cell(&pressed)][0].trials == 1 && + bare.cell[rx_cm_cell(&pressed)][0].n == 0, + "valid matvec pressure without a quiet model is skipped"); + pressed.op = RX_CM_OPS; + RxCostModel bare_snap = bare; + CHECK(rx_cm_observe(&bare, &pressed, 0, &exact) == RX_CM_ERR_UNSUPPORTED && + memcmp(&bare, &bare_snap, sizeof bare) == 0, + "unsupported op under pressure is refused, not skipped into a bucket"); + pressed = qf; + pressed.pressure = 1; + RxCmObservation slow = { (uint64_t)exp2(21.0), 0 }; + for (int i = 0; i < 6; i++) + CHECK(rx_cm_observe(&quiet, &pressed, 0, &slow) == RX_CM_OK, "pressure residual accepted"); + uint64_t residual_n = quiet.cell[rx_cm_cell(&pressed)][0].n; + CHECK(residual_n == 6, "pressure residual has its own samples"); + rx_cm_predict(&quiet, &pressed, 0, &pq); + CHECK(pq.known && !pq.borrowed && !pq.refused && fabs(pq.mean_log2_ps - (p0.mean_log2_ps + 1.0)) < 0.25, + "pressure correction learned (%.3f vs %.3f)", pq.mean_log2_ps, p0.mean_log2_ps + 1.0); + pressed.op = UINT32_MAX; + snap = quiet; + CHECK(rx_cm_observe(&quiet, &pressed, 0, &slow) == RX_CM_ERR_UNSUPPORTED && + memcmp(&quiet, &snap, sizeof quiet) == 0, + "unsupported op does not train the pressure residual"); + + /* OTHER core and a saturated pressure bucket stay real cells. */ + RxCostModel other_m; + rx_cm_init(&other_m, 1); + RxCmFeatures other = qf; + other.core = 99; + other.pressure = 0; + uint32_t oc = rx_cm_cell(&other); + CHECK(oc < RX_CM_CELLS && oc != rx_cm_cell(&qf), "OTHER core is a real cell"); + CHECK(rx_cm_observe(&other_m, &other, 0, &exact) == RX_CM_OK && other_m.cell[oc][0].n == 1 && + other_m.cell[0][0].n == 0, + "OTHER core observation stays out of the matvec X925 cell"); + other.pressure = 99; + uint32_t pc = rx_cm_cell(&other); + CHECK(pc < RX_CM_CELLS && pc != oc && pc != RX_CM_CELL_NONE, + "saturated pressure on OTHER is its own bucket"); + + /* Each decision mode on a supported matvec, then refusal beside them. */ + RxCostModel empty; + rx_cm_init(&empty, 2); + RxCmFeatures g = feat(16, 16, RX_CM_CORE_X925, 0); + g.eligible = 0x3; + rx_cm_decide(&empty, &pol, &g, 1, &d); + CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_UNKNOWN_ARM, "empty model measures"); + rx_cm_decide(&empty, &pol, &g, 0, &d); + CHECK(d.action == RX_CM_FALLBACK && d.arm == 0 && d.why == RX_CM_WHY_UNKNOWN_ARM, + "empty frozen model falls back"); + RxCostModel one; + rx_cm_init(&one, 1); + rx_cm_decide(&one, &pol, &g, 1, &d); + CHECK(d.action == RX_CM_FALLBACK && d.why == RX_CM_WHY_NO_ELIGIBLE, "only the fallback is eligible"); + + RxCostModel tight; + rx_cm_init(&tight, 2); + RxCmObservation hi = { (uint64_t)exp2(22.0), 0 }; + for (int i = 0; i < 40; i++) { + rx_cm_observe(&tight, &g, 0, &exact); + rx_cm_observe(&tight, &g, 1, &hi); + } + rx_cm_decide(&tight, &pol, &g, 1, &d); + CHECK(d.action == RX_CM_SELECT && d.arm == 0 && d.why == RX_CM_WHY_CONFIDENT, + "separated arms select confidently (action %d arm %u why %u)", d.action, d.arm, d.why); + g.latency_budget_ps = 1000; + rx_cm_decide(&tight, &pol, &g, 0, &d); + CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_OVER_BUDGET, "no arm fits the latency budget"); + g.latency_budget_ps = 0; + + RxCostModel equal; + rx_cm_init(&equal, 2); + for (int i = 0; i < 40; i++) { + rx_cm_observe(&equal, &g, 0, &exact); + rx_cm_observe(&equal, &g, 1, &exact); + } + rx_cm_decide(&equal, &pol, &g, 1, &d); + CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_CONFIDENT, + "equal and tight: not worth a run (action %d ei %.3f)", d.action, d.expected_improvement); + RxCmFeatures gp = g; + gp.pressure = 1; + rx_cm_decide(&equal, &pol, &gp, 1, &d); + CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, + "unseen pressure is worth measuring (action %d why %u ei %.3f)", d.action, d.why, + d.expected_improvement); + rx_cm_charge(&equal, 0, pol.explore_allow_ps + 1); + rx_cm_decide(&equal, &pol, &gp, 1, &d); + CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_BUDGET_SPENT, + "spent exploration budget selects (action %d why %u)", d.action, d.why); + rx_cm_charge(&equal, (uint64_t)((double)(pol.explore_allow_ps + 1) / pol.explore_frac) * 2, 0); + rx_cm_decide(&equal, &pol, &gp, 1, &d); + CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, + "budget refills as chosen work accrues"); + gp.op = RX_CM_OPS; + rx_cm_decide(&equal, &pol, &gp, 1, &d); + CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.pred[0].refused && + d.pred[1].refused && !d.pred[0].known && !d.pred[1].borrowed, + "unsupported op is not ranked in any decision mode"); + + RxCmFeatures cal[16]; + uint32_t carm[16]; + uint64_t cps[16]; + for (int i = 0; i < 16; i++) { + cal[i] = repro_features(i & 1 ? UINT32_MAX : RX_CM_OPS); + carm[i] = 0; + cps[i] = (uint64_t)exp2(30.0); + } + double scale_before = equal.sd_scale; + double scale = rx_cm_calibrate(&equal, cal, carm, cps, 16, 0.80); + CHECK(scale == 0.0 && equal.sd_scale == scale_before, + "unsupported calibration points do not change the scale"); +} + +int main(int argc, char **argv) { + int only_refuse = argc > 1 && strcmp(argv[1], "refuse") == 0; + if (!only_refuse) { + double c80, c95, rmse; + t_basis(); + t_fit_and_calibration(1, &c80, &c95, &rmse); + t_fit_and_calibration(0, &c80, &c95, &rmse); + t_decide(); + t_eligibility_property(); + t_budgets(); + t_calibrate(); + t_blob(); + } + t_refuse_unsupported_op(); printf("checks %d failures %d\n", g_checks, g_fail); return g_fail ? 1 : 0; }