Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/6421fe7018697b71 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## Refuse inverted buffer lifetimes in offline DAG certificates closed `6421fe7018697b71` #937 · opened 2026-10-30 11:29 UTC · closed 2026-10-30 18:06 UTC Imported from GitHub aien-dev/omega-mv#937, by drakestapleton Base `62d1d0d668f897386a0e311374f8dbcc5c32dde8` · Head `52c9afc989c22655b8d74f0e05ee8ccce5f469bd` drakestapleton wrote on 2026-10-30 11:29 UTC Refs #705 Stacked on the dagsim import (`cursor/import-dagsim-590-44c6`). Merge that first. This diff is the lifetime refusal, its fixtures, the mutant, and the CLI checks, plus the cost-model predict refusal below. An inverted lifetime (resolved end before start) subtracted live bytes, so a model whose valid buffers needed 160 B on a 100 B cap could be certified `OK` with peak 80. That window is now `REFUSED_LIFETIME` before any certificate or Pareto rank. The reason names the buffer, both endpoints, and the resolved times. Interval convention, added to the frozen schema text without a new field: half-open `[start, end)`. `end == start` emits no ledger events, so a zero-duration buffer cannot offset another one. Frees of intervals that started earlier still run before allocations at the same timestamp, so a completion/allocation tie does not double-count. If a sweep would go negative anyway, the certificate is refused. `parse_real` already rejects a missing buffer size, a non-number, NaN, and any negative. `strtod` still accepts `inf`, so the buffer parser also requires `isfinite`. `bytes=inf` is a parse error: `buffer bytes must be a finite number >= 0`. ## Symbol guard Merged `52a82f392` from #936 with a normal merge. `dagsim.o` on this branch uses the full forbidden list from `3bd56731`: `rx_gen_|aienos_cap_|rx_caproot_|rx_capadmin_|rx_world_|mmap|mprotect|fork|exec|dlopen|system`. A planted `mmap` reference fails that recipe and removes the object. ## Cost-model predict `rx_cm_predict` on main is still `void` and `RxCmPrediction` has no `refused` field, so this branch cannot build the new check against main. Merged `cursor/costmodel-refuse-unsupported-op-c2c2` at `a683ffe6d` with a normal merge (no rebase). That is PR #934. #934 still merges to main first; this merge only makes the API visible here. At the call in `tools/dagsim/dagsim.c`, `RX_CM_ERR_UNSUPPORTED` and `p.refused` are the same refusal as an unknown cell (`REFUSED_UNKNOWN`, no makespan). That holds when `known` is set and `n` is not zero. A mutant that puts the predicate back to only `!p.known || p.n == 0` is killed by `predict_unsupported_despite_known` and `predict_refused_despite_known`. ## Tests `make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-dagsim` on Linux x86_64, Ubuntu clang 18.1.3, after merging the #936 guard: - `dagsim_unit: 201 checks, 0 failed` (203 after `52c9afc9`, below) - CLI: the #705 fixtures refuse or fit as specified. Issue fixture reason: `inverted lifetime for buffer inverted: first=C start_ns=20 last=A end_ns=10`. Two valid 80 B buffers: `REFUSED_CAPACITY`, `160 B live at t=10 ns (real1,real2) exceeds mem`. Disjoint pair and completion tie: `OK`, peak 80 - `mutant killed: inverted_lifetime_masks_overflow` - `mutant killed: predict_unsupported_despite_known predict_refused_despite_known` - planted `mmap`: `make` exits 1 at `Makefile:1283`, prints `U mmap`, and deletes `build/dagsim.o` - `crumb verify` with the tool pinned in `crumb.lock` (`d0f0b95d`): OK Head `92992c72121170599cc46f8c198e7703d5220777`. Host regression of this certificate bug: PASS. Chip: NOT_RUN. Measured performance: NOT_RUN. No GPU. Update at `52c9afc989c22655b8d74f0e05ee8ccce5f469bd` (merged main `62d1d0d6`, which contains #936): the `live < -1e-6` "went negative" refusal is now tested. It is reachable through double rounding: a 1e20 B buffer and a 1 B buffer sum to 1e20, so releasing both drives the running total to -1. Check `rounding_drives_live_negative` expects `REFUSED_LIFETIME` with "went negative", peak 0 and makespan 0. `make -j2 PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-dagsim test-costmodel` on the Spark (aarch64): `dagsim_unit: 203 checks, 0 failed`, both mutants above killed, costmodel `checks 127 failures 0`. Negative control: replacing the guard with `if (0)` gives `207 checks, 2 failed` (`rounding_drives_live_negative: got OK`). The predict-refusal branch is reachable only through the test's `--wrap=rx_cm_predict` injector: `rx_cm_predict` callers always pass MATVEC. The import still claims no validated costs. This patch does not change that. Seeded `.om` replacement of the evaluator remains #599. Merge order when the time comes: #934, then this PR (#936 is already on main). — Grokbot
Open in Web Open in Cursor
### Fetch it A git bundle of the pull request's head, 49624 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/6421fe7018697b71.bundle -o 6421fe7018697b71.bundle git fetch 6421fe7018697b71.bundle refs/pull/937/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 937 52c9afc989c22655b8d74f0e05ee8ccce5f469bd ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":937,"url":"https://github.com/aien-dev/omega-mv/pull/937","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:43Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f3e51fe46..fe6ec8e4e 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": "ce3a108d425862f47db0c877b7c6b8a5e17d399205d86c17682c5928178c6b9b", + "last_commit": "9185ed46f", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "a53455738fef88bd837247b7b8e39ab9d08185e50f07c81c3f2acbc84ad54b26", + "last_commit": "da487a095", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "69949441d9dbd568673589e826ce78d0b15cd63237595172a82c01994fbe9385", + "last_commit": "da487a095", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "digest": "7a6ed3e1a9a2d14bbdd5d7fdd7080ef681db1f39a3ef306572c1a929a8913c54", + "last_commit": "b465e2070", "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:5c7a7f090510f4a417d7c6e13f39ec8c2477c84c2d6e82d7249d53d01fbb07bc", + "compiled_at": "2026-10-30T16:31:15Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:0f2081662f9105904c737b2ae9117aae19c35305a0c24a6c08f5d7750056c3cc", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17856, + "generated_at_commit": "7521b2739cd72f42423f92d26879988080765d11", + "source_tree": "sha256:12439b27fe214ebcdda20be5f615359df1b81b50f92bfb32d772e59491d5edea" }, "owner_lane": "", "provenance": { diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dda6d20b5..55735b962 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,6 +5,8 @@ Every model below did real work. A model is listed only for work it did. The orc | Model | What it did | |---|---| +| Grok 4.7 | Treated an unsupported or refused rx_cm_predict result as an unknown cell in the offline DAG evaluator, including when known is set. Merged #934's predict API to build that check. Host test and a mutant that drops it. | +| Grok 4.7 | Refused inverted buffer lifetimes in the imported offline DAG evaluator so a negative live-byte ledger cannot hide a capacity overflow (#705). Host fixtures and a mutant that drops the check. | | 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. | @@ -48,4 +50,5 @@ 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 | Cost-model refusal of unsupported semantic operation IDs before prediction or learning, with a clamp-restoring mutant (Refs #704) | | 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). | diff --git a/Makefile b/Makefile index 6b53cd124..5f2355ea0 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. @@ -1273,7 +1285,7 @@ $(DS_OBJ): tools/dagsim/dagsim.c tools/dagsim/dagsim.h src/runtime/rx_costmodel. rm -f $@; exit 1; fi $(DS_UNIT): tests/dagsim/dagsim_unit.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) | $(OUT_DIR) - $(CC) $(CFLAGS) -Itools/dagsim -o $@ tests/dagsim/dagsim_unit.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) -lm + $(CC) $(CFLAGS) -Itools/dagsim -o $@ tests/dagsim/dagsim_unit.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) -Wl,--wrap=rx_cm_predict -lm $(DS_CLI): tools/dagsim/dagsim_cli.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) | $(OUT_DIR) $(CC) $(CFLAGS) -Itools/dagsim -o $@ tools/dagsim/dagsim_cli.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) -lm @@ -1281,6 +1293,8 @@ $(DS_CLI): tools/dagsim/dagsim_cli.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) | $(OU test-dagsim: $(DS_UNIT) $(DS_CLI) ./$(DS_UNIT) sh tests/dagsim/run_cli.sh $(DS_CLI) $(OUT_DIR)/dagsim-cli-out + sh tests/dagsim/mutant_lifetime.sh + sh tests/dagsim/mutant_predict.sh # AArch64 hosts: the gate. Real realizations, training, promotion through the # R9 barrier on the native authority, held-out workloads. diff --git a/spec/.crumb b/spec/.crumb index a226ce192..eec5c57c0 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-30T11:44:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", + "digest": "sha256:ce3a108d425862f47db0c877b7c6b8a5e17d399205d86c17682c5928178c6b9b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 63, - "generated_at_commit": "a8a6a3d3c47ce797430f045edc5eae11594ba36d", - "source_tree": "sha256:4cb3bb1977c84caacc34366f814d6fe403a2eda7b89b916948d29541675c59bb" + "generated_at_commit": "b465e20709812eef3b0275f5c7f9efcb45a3982d", + "source_tree": "sha256:05711875a735e974b3ca54cc3ffbb476bb5557921581788aef0e41fcada3afe4" }, "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/spec/offline-evidence-v1.md b/spec/offline-evidence-v1.md index e468163e3..4ba1b335e 100644 --- a/spec/offline-evidence-v1.md +++ b/spec/offline-evidence-v1.md @@ -89,9 +89,19 @@ Semantics the evaluator is allowed to rely on: refused. `kind=storage` and `kind=persist` are ops whose `bw` is a storage resource. - `dep a b`: b starts after a ends. Cycles are refused. - `buffer`: live from the start of `first` to the end of `last`. Missing `first` means - live from t=0 (resident state); missing `last` means live until the end. Peak live bytes + live from t=0 (resident state); missing `last` means live until the end. The occupied + interval is half-open `[start, end)`: live at time t when `start <= t < end`. `start` + is the scheduled start of `first` (or 0); `end` is the scheduled end of `last` (or the + makespan). `end == start` is empty (a zero-duration operation or buffer): it contributes + no live bytes and cannot offset another buffer. `end` earlier than `start` is inverted: + the evaluator refuses `REFUSED_LIFETIME` before any capacity certificate or ranking, + naming the buffer, both endpoint operations (`omitted` when the field is absent) and the + resolved times. At one timestamp, frees of intervals that started earlier are applied + before allocations, so a buffer that ends when another starts does not occupy both + (completion/allocation tie). An accepted ledger never goes negative. Peak live bytes per capacity resource must not exceed `cap`, else `REFUSED_CAPACITY` naming the time - and the buffers. + and the buffers. No frozen field was added; this is the interval convention for the + words already in v1. - `sync id=X ops=a,b,...`: declares a barrier op `X` (no cost, no engine) that completes when every listed op has completed. Order later work after it with `dep X y`. A barrier with a measured cost is written as `op id=X kind=barrier t_ns=... prov=...` plus `dep` lines. diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..fc83cd5e4 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:06:38Z", "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": "da487a0950e7c1c9e96a65b51e94c1e1698fbe70", + "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..25b7d92ee 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-30T16:31:15Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:69949441d9dbd568673589e826ce78d0b15cd63237595172a82c01994fbe9385", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "files": 1393, + "generated_at_commit": "7521b2739cd72f42423f92d26879988080765d11", + "source_tree": "sha256:63e1cb439839f7ef9edee542302821a887928d058d6b71a6e5ec6803de47c893" }, "owner_lane": "", "provenance": { diff --git a/tests/dagsim/dagsim_unit.c b/tests/dagsim/dagsim_unit.c index f9c6c089e..71b936399 100644 --- a/tests/dagsim/dagsim_unit.c +++ b/tests/dagsim/dagsim_unit.c @@ -26,6 +26,29 @@ static int g_checks, g_fail; } while (0) #define NEAR(a, b, tol) (fabs((a) - (b)) <= (tol)) +/* The real predict never sets known on a refusal. These cases do, so a caller + * that only looks at p.known will certify a time. 0 calls the real function. + * 1: RX_CM_ERR_UNSUPPORTED with known set and refused clear. + * 2: RX_CM_OK with refused set and known set. */ +static int g_predict_fault; + +extern int __real_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, + uint32_t arm, RxCmPrediction *out); + +int __wrap_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, + uint32_t arm, RxCmPrediction *out) { + if (g_predict_fault == 0) return __real_rx_cm_predict(m, f, arm, out); + memset(out, 0, sizeof *out); + out->known = 1; + out->n = 32; + out->mean_log2_ps = 20.0; + out->sd_log2 = 0.0; + out->dof = 1.0; + if (g_predict_fault == 1) return RX_CM_ERR_UNSUPPORTED; + out->refused = 1; + return RX_CM_OK; +} + static const char *HDR = "oev version=1\n" "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" @@ -405,6 +428,42 @@ static void test_costmodel_reuse(void) { CHECK(rc == DS_REFUSED_UNKNOWN, "unobserved arm refuses, got %s", ds_status_name(rc)); } +/* #934: an unsupported id is refused, and that refusal is not a known cell. + * The two fault cases set known anyway. They must still refuse. */ +static void test_predict_refusal(void) { + RxCostModel cm; rx_cm_init(&cm, 1); + RxCmFeatures f; memset(&f, 0, sizeof f); + f.op = RX_CM_OPS; f.M = 64; f.N = 16; f.core = 0; + RxCmPrediction p; + int st = rx_cm_predict(&cm, &f, 0, &p); + CHECK(st == RX_CM_ERR_UNSUPPORTED && p.refused && !p.known && p.n == 0, + "real predict refuses an unsupported op, st=%d refused=%d known=%d n=%u", + st, p.refused, p.known, p.n); + + ds_model m; ds_result r; char text[4096], err[256]; + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=0 prov=fitted src=rx_costmodel\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + ds_opts o; memset(&o, 0, sizeof o); + o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + o.costmodel = &cm; + + g_predict_fault = 1; + int rc = ds_eval(&m, &o, &r); + g_predict_fault = 0; + CHECK(rc == DS_REFUSED_UNKNOWN && r.makespan_ns == 0.0, + "predict_unsupported_despite_known: got %s makespan %.3f (%s)", + ds_status_name(rc), r.makespan_ns, r.reason); + + g_predict_fault = 2; + rc = ds_eval(&m, &o, &r); + g_predict_fault = 0; + CHECK(rc == DS_REFUSED_UNKNOWN && r.makespan_ns == 0.0, + "predict_refused_despite_known: got %s makespan %.3f (%s)", + ds_status_name(rc), r.makespan_ns, r.reason); +} + static void test_counterfactual(void) { ds_model m; ds_result r; int rc = eval_text( @@ -482,6 +541,116 @@ static void test_minimum_work_diff(void) { CHECK(rc != DS_OK && d.double_counted, "double-counted shared work detected"); } +static int eval_file(const char *path, ds_model *m, ds_result *r) { + char err[256]; + int rc = ds_parse_file(path, m, err, sizeof err); + if (rc != DS_OK) { + memset(r, 0, sizeof *r); + r->status = rc; + snprintf(r->reason, sizeof r->reason, "%s", err); + return rc; + } + ds_opts def; memset(&def, 0, sizeof def); + def.asof_day = ds_day_from_date("2026-10-24"); + def.interval_q = 0.9; + return ds_eval(m, &def, r); +} + +static double peak_of(const ds_model *m, const ds_result *r, const char *cap) { + int i = ds_find_res(m, cap); + return i < 0 ? -1.0 : r->peak_live_bytes[i]; +} + +/* Issue #705. An inverted lifetime must not contribute negative live bytes. */ +static void test_inverted_lifetimes(void) { + ds_model m; ds_result r, r2; + int rc = eval_file("tests/dagsim/fixtures/inverted_masks_overflow.oev", &m, &r); + CHECK(rc == DS_REFUSED_LIFETIME + && strstr(r.reason, "inverted lifetime for buffer inverted:") + && strstr(r.reason, "first=C") && strstr(r.reason, "start_ns=20") + && strstr(r.reason, "last=A") && strstr(r.reason, "end_ns=10") + && !strstr(r.reason, "went negative"), + "inverted_lifetime_masks_overflow: got %s (%s)", ds_status_name(rc), r.reason); + CHECK(r.makespan_ns == 0 && r.peak_live_bytes[0] == 0, "refusal carries no capacity certificate"); + rc = eval_file("tests/dagsim/fixtures/inverted_masks_overflow.oev", &m, &r2); + CHECK(rc == r2.status && strcmp(r.reason, r2.reason) == 0, "rejection text is deterministic"); + + rc = eval_file("tests/dagsim/fixtures/inverted_lone.oev", &m, &r); + CHECK(rc == DS_REFUSED_LIFETIME && strstr(r.reason, "first=C") && strstr(r.reason, "last=A"), + "lone inverted lifetime refused, got %s (%s)", ds_status_name(rc), r.reason); + + rc = eval_file("tests/dagsim/fixtures/capacity_two_80.oev", &m, &r); + CHECK(rc == DS_REFUSED_CAPACITY && strstr(r.reason, "real1") && strstr(r.reason, "real2"), + "two valid 80 B buffers refuse capacity, got %s (%s)", ds_status_name(rc), r.reason); + + rc = eval_file("tests/dagsim/fixtures/valid_disjoint_80.oev", &m, &r); + CHECK(rc == DS_OK, "disjoint 80 B pair fits, got %s (%s)", ds_status_name(rc), r.reason); + CHECK(NEAR(peak_of(&m, &r, "mem"), 80, 1e-9), "disjoint peak 80, got %.1f", peak_of(&m, &r, "mem")); + CHECK(NEAR(r.makespan_ns, 30, 1e-9), "disjoint makespan 30, got %.1f", r.makespan_ns); + + rc = eval_file("tests/dagsim/fixtures/valid_tie_80.oev", &m, &r); + CHECK(rc == DS_OK, "completion/allocation tie fits, got %s (%s)", ds_status_name(rc), r.reason); + CHECK(NEAR(peak_of(&m, &r, "mem"), 80, 1e-9), "tie peak 80 (not 160), got %.1f", peak_of(&m, &r, "mem")); + + /* zero-duration: empty [start, end), peak 0, and it cannot hide an overlap */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=Z kind=compute engine=cpu t_ns=0 prov=measured src=t\n" + "buffer id=z bytes=80 cap=mem first=Z last=Z\n", &m, &r, NULL); + CHECK(rc == DS_OK, "zero-duration buffer status %s (%s)", ds_status_name(rc), r.reason); + CHECK(NEAR(r.peak_live_bytes[0], 0, 1e-9), "zero-duration peak 0, got %.1f", r.peak_live_bytes[0]); + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=Z kind=compute engine=cpu t_ns=0 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "buffer id=z bytes=400 cap=mem first=Z last=Z\n" + "buffer id=real1 bytes=600 cap=mem first=B last=B\n" + "buffer id=real2 bytes=600 cap=mem first=B last=B\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_CAPACITY, "zero-duration must not hide 1200 B, got %s (%s)", ds_status_name(rc), r.reason); + + /* omitted endpoints stay valid: resident from 0, open end, and both omitted */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "buffer id=no_first bytes=100 cap=mem last=A\n" + "buffer id=no_last bytes=50 cap=mem first=A\n" + "buffer id=resident bytes=25 cap=mem\n", &m, &r, NULL); + CHECK(rc == DS_OK, "omitted endpoints status %s (%s)", ds_status_name(rc), r.reason); + CHECK(NEAR(r.peak_live_bytes[0], 175, 1e-9), "omitted endpoints peak 175, got %.1f", r.peak_live_bytes[0]); + + /* The went-negative refusal is reachable without an inverted window: the + * ledger sums doubles, and 1e20 + 1 rounds to 1e20 (ulp 16384). Events + * +1e20@0, +1@10, -1e20@20, -1@30 end the sweep at -1. A certificate + * built on that ledger would be wrong, so it is refused. */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "resource id=hbm kind=capacity cap=1e30 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "dep A B\ndep B C\n" + "buffer id=huge bytes=1e20 cap=hbm first=A last=B\n" + "buffer id=tiny bytes=1 cap=hbm first=B last=C\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_LIFETIME && strstr(r.reason, "went negative"), + "rounding_drives_live_negative: got %s (%s)", ds_status_name(rc), r.reason); + CHECK(peak_of(&m, &r, "hbm") == 0 && r.makespan_ns == 0, + "negative-ledger refusal carries no capacity certificate"); + + char text[2048], err[256]; + snprintf(text, sizeof text, "%s%s", HDR, "buffer id=neg bytes=-5 cap=mem\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "finite number >= 0"), + "negative bytes refused at parse: %s", err); + snprintf(text, sizeof text, "%s%s", HDR, "buffer id=inf bytes=inf cap=mem\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "finite number >= 0"), + "non-finite bytes refused at parse: %s", err); + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "buffer id=ghost bytes=10 cap=mem first=nope last=A\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR && strstr(err, "not an op"), + "unknown endpoint refused at parse: %s", err); +} + int main(void) { test_serial_chain(); test_perfect_overlap(); @@ -495,8 +664,10 @@ int main(void) { test_energy(); test_parse_and_cycles(); test_costmodel_reuse(); + test_predict_refusal(); test_counterfactual(); test_minimum_work_diff(); + test_inverted_lifetimes(); printf("dagsim_unit: %d checks, %d failed\n", g_checks, g_fail); return g_fail ? 1 : 0; } diff --git a/tests/dagsim/fixtures/capacity_two_80.oev b/tests/dagsim/fixtures/capacity_two_80.oev new file mode 100644 index 000000000..c4c18c4a6 --- /dev/null +++ b/tests/dagsim/fixtures/capacity_two_80.oev @@ -0,0 +1,11 @@ +oev version=1 +machine id=toy soc=toy cores=1 mem_bytes=100 prov=synthetic src=analytical-fixture +build repo=omega-mv sha=3bd56731c5517e2a9c8f195955c71e73dc6f9269 +resource id=cpu kind=compute slots=1 prov=synthetic src=analytical-fixture +resource id=mem kind=capacity cap=100 prov=synthetic src=analytical-fixture +op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=B kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=C kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +buffer id=real1 bytes=80 cap=mem first=B last=B +buffer id=real2 bytes=80 cap=mem first=B last=B +energy instrument=none diff --git a/tests/dagsim/fixtures/inverted_lone.oev b/tests/dagsim/fixtures/inverted_lone.oev new file mode 100644 index 000000000..f85cc1f97 --- /dev/null +++ b/tests/dagsim/fixtures/inverted_lone.oev @@ -0,0 +1,10 @@ +oev version=1 +machine id=toy soc=toy cores=1 mem_bytes=100 prov=synthetic src=analytical-fixture +build repo=omega-mv sha=3bd56731c5517e2a9c8f195955c71e73dc6f9269 +resource id=cpu kind=compute slots=1 prov=synthetic src=analytical-fixture +resource id=mem kind=capacity cap=100 prov=synthetic src=analytical-fixture +op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=B kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=C kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +buffer id=inverted bytes=80 cap=mem first=C last=A +energy instrument=none diff --git a/tests/dagsim/fixtures/inverted_masks_overflow.oev b/tests/dagsim/fixtures/inverted_masks_overflow.oev new file mode 100644 index 000000000..724b447cd --- /dev/null +++ b/tests/dagsim/fixtures/inverted_masks_overflow.oev @@ -0,0 +1,12 @@ +oev version=1 +machine id=toy soc=toy cores=1 mem_bytes=100 prov=synthetic src=analytical-fixture +build repo=omega-mv sha=3bd56731c5517e2a9c8f195955c71e73dc6f9269 +resource id=cpu kind=compute slots=1 prov=synthetic src=analytical-fixture +resource id=mem kind=capacity cap=100 prov=synthetic src=analytical-fixture +op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=B kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=C kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +buffer id=inverted bytes=80 cap=mem first=C last=A +buffer id=real1 bytes=80 cap=mem first=B last=B +buffer id=real2 bytes=80 cap=mem first=B last=B +energy instrument=none diff --git a/tests/dagsim/fixtures/valid_disjoint_80.oev b/tests/dagsim/fixtures/valid_disjoint_80.oev new file mode 100644 index 000000000..1bf831d40 --- /dev/null +++ b/tests/dagsim/fixtures/valid_disjoint_80.oev @@ -0,0 +1,11 @@ +oev version=1 +machine id=toy soc=toy cores=1 mem_bytes=100 prov=synthetic src=analytical-fixture +build repo=omega-mv sha=3bd56731c5517e2a9c8f195955c71e73dc6f9269 +resource id=cpu kind=compute slots=1 prov=synthetic src=analytical-fixture +resource id=mem kind=capacity cap=100 prov=synthetic src=analytical-fixture +op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=B kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=C kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +buffer id=real1 bytes=80 cap=mem first=A last=A +buffer id=real2 bytes=80 cap=mem first=C last=C +energy instrument=none diff --git a/tests/dagsim/fixtures/valid_tie_80.oev b/tests/dagsim/fixtures/valid_tie_80.oev new file mode 100644 index 000000000..a39a0a784 --- /dev/null +++ b/tests/dagsim/fixtures/valid_tie_80.oev @@ -0,0 +1,11 @@ +oev version=1 +machine id=toy soc=toy cores=1 mem_bytes=100 prov=synthetic src=analytical-fixture +build repo=omega-mv sha=3bd56731c5517e2a9c8f195955c71e73dc6f9269 +resource id=cpu kind=compute slots=1 prov=synthetic src=analytical-fixture +resource id=mem kind=capacity cap=100 prov=synthetic src=analytical-fixture +op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +op id=B kind=compute engine=cpu t_ns=10 prov=synthetic src=analytical-fixture +dep A B +buffer id=x bytes=80 cap=mem first=A last=A +buffer id=y bytes=80 cap=mem first=B last=B +energy instrument=none diff --git a/tests/dagsim/mutant_lifetime.sh b/tests/dagsim/mutant_lifetime.sh new file mode 100755 index 000000000..b36412254 --- /dev/null +++ b/tests/dagsim/mutant_lifetime.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Drop the inverted-lifetime check and require the named assertion to fail. +# A mutant that still prints a passing inverted_lifetime_masks_overflow is a miss. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) +cd "$ROOT" +src=tools/dagsim/dagsim.c +grep -q 'LIFETIME_ORDER_CHECK' "$src" +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +sed '/LIFETIME_ORDER_CHECK/d' "$src" > "$tmp/dagsim.c" +grep -q 'LIFETIME_ORDER_CHECK' "$tmp/dagsim.c" && { echo "mutant: check still present"; exit 1; } +grep -q 'reject_inverted_lifetimes' "$tmp/dagsim.c" || { echo "mutant: deleted more than the call"; exit 1; } + +CC=${CC:-cc} +# -Wno-unused-function: deleting the call leaves reject_inverted_lifetimes unreferenced. +# The mutant has to run. The kill is the named assertion, not a compile error. +cflags="-std=gnu11 -Wall -Wextra -Werror -Wno-unused-function -D_GNU_SOURCE -O2 -Isrc -Itools/dagsim" +$CC $cflags -c -o "$tmp/dagsim.o" "$tmp/dagsim.c" +$CC $cflags -c -o "$tmp/rx_costmodel.o" src/runtime/rx_costmodel.c +if [ "$(uname -m)" = aarch64 ]; then + sha=src/omega_om_units.c +else + sha=tests/dagsim/sha256_host.c +fi +$CC $cflags -o "$tmp/unit" tests/dagsim/dagsim_unit.c "$tmp/dagsim.o" "$tmp/rx_costmodel.o" "$sha" -Wl,--wrap=rx_cm_predict -lm +set +e +"$tmp/unit" > "$tmp/out" 2>&1 +rc=$? +set -e +if [ "$rc" -eq 0 ]; then + echo "mutant survived: unit suite passed after dropping LIFETIME_ORDER_CHECK" + exit 1 +fi +if ! grep -q 'inverted_lifetime_masks_overflow' "$tmp/out"; then + echo "mutant did not fail named assertion inverted_lifetime_masks_overflow" + cat "$tmp/out" + exit 1 +fi +echo "mutant killed: inverted_lifetime_masks_overflow" diff --git a/tests/dagsim/mutant_predict.sh b/tests/dagsim/mutant_predict.sh new file mode 100644 index 000000000..89df5266d --- /dev/null +++ b/tests/dagsim/mutant_predict.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# Drop the unsupported-return and refused checks. Leave the p.known test in +# place. The named assertions must fail: a prediction can be marked known and +# still be a refusal. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) +cd "$ROOT" +src=tools/dagsim/dagsim.c +grep -q 'PREDICT_REFUSAL_CHECK' "$src" +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +sed '/PREDICT_REFUSAL_CHECK/s/st == RX_CM_ERR_UNSUPPORTED || p->refused || //' "$src" > "$tmp/dagsim.c" +grep -q 'PREDICT_REFUSAL_CHECK' "$tmp/dagsim.c" || { echo "mutant: deleted the check tag"; exit 1; } +grep -q 'RX_CM_ERR_UNSUPPORTED' "$tmp/dagsim.c" && { echo "mutant: unsupported check still present"; exit 1; } +grep -q 'predict_unusable' "$tmp/dagsim.c" || { echo "mutant: deleted the call"; exit 1; } + +CC=${CC:-cc} +# -Wno-unused-parameter: dropping the use of st must not die at compile time. +# The kill is the named assertion, not a warning. +cflags="-std=gnu11 -Wall -Wextra -Werror -Wno-unused-parameter -D_GNU_SOURCE -O2 -Isrc -Itools/dagsim" +$CC $cflags -c -o "$tmp/dagsim.o" "$tmp/dagsim.c" +$CC $cflags -c -o "$tmp/rx_costmodel.o" src/runtime/rx_costmodel.c +if [ "$(uname -m)" = aarch64 ]; then + sha=src/omega_om_units.c +else + sha=tests/dagsim/sha256_host.c +fi +$CC $cflags -o "$tmp/unit" tests/dagsim/dagsim_unit.c "$tmp/dagsim.o" "$tmp/rx_costmodel.o" "$sha" -Wl,--wrap=rx_cm_predict -lm +set +e +"$tmp/unit" > "$tmp/out" 2>&1 +rc=$? +set -e +if [ "$rc" -eq 0 ]; then + echo "mutant survived: unit suite passed after dropping the predict refusal check" + exit 1 +fi +if ! grep -q 'predict_unsupported_despite_known' "$tmp/out"; then + echo "mutant did not fail named assertion predict_unsupported_despite_known" + cat "$tmp/out" + exit 1 +fi +if ! grep -q 'predict_refused_despite_known' "$tmp/out"; then + echo "mutant did not fail named assertion predict_refused_despite_known" + cat "$tmp/out" + exit 1 +fi +echo "mutant killed: predict_unsupported_despite_known predict_refused_despite_known" diff --git a/tests/dagsim/run_cli.sh b/tests/dagsim/run_cli.sh index 5b22297de..0c5d7b364 100644 --- a/tests/dagsim/run_cli.sh +++ b/tests/dagsim/run_cli.sh @@ -57,5 +57,31 @@ grep -q '"status": "REFUSED_REMOVED_REQUIRED"' "$OUT/diff_bad.json"; check $? "u "$BIN" diff $F/mw_baseline.oev $F/mw_baseline.oev --asof $ASOF > "$OUT/diff_same.json" grep -q '"kept": 4, "added": 0, "removed": 0' "$OUT/diff_same.json"; check $? "no-change fixture adds no nodes" +# omega-mv#705: inverted lifetime must not come back as an OK capacity certificate. +"$BIN" eval $F/inverted_masks_overflow.oev --asof 2026-10-24 > "$OUT/inverted.json"; rc=$? +[ $rc -eq 1 ]; check $? "inverted lifetime exits 1" +grep -q '"status": "REFUSED_LIFETIME"' "$OUT/inverted.json"; check $? "inverted lifetime status" +grep -q 'first=C' "$OUT/inverted.json"; check $? "inverted reason names first=C" +grep -q 'last=A' "$OUT/inverted.json"; check $? "inverted reason names last=A" +grep -q 'makespan' "$OUT/inverted.json"; [ $? -ne 0 ]; check $? "no makespan on inverted refusal" +"$BIN" eval $F/inverted_lone.oev --asof 2026-10-24 > "$OUT/inverted_lone.json"; rc=$? +[ $rc -eq 1 ]; check $? "lone inverted lifetime exits 1" +grep -q '"status": "REFUSED_LIFETIME"' "$OUT/inverted_lone.json"; check $? "lone inverted is not peak 0" +"$BIN" eval $F/capacity_two_80.oev --asof 2026-10-24 > "$OUT/cap80.json"; rc=$? +[ $rc -eq 1 ]; check $? "two overlapping 80 B buffers exit 1" +grep -q '"status": "REFUSED_CAPACITY"' "$OUT/cap80.json"; check $? "overlap is REFUSED_CAPACITY" +"$BIN" eval $F/valid_disjoint_80.oev --asof 2026-10-24 > "$OUT/disjoint80.json"; rc=$? +check $rc "disjoint 80 B pair exits 0" +grep -q '"status": "OK"' "$OUT/disjoint80.json"; check $? "disjoint pair OK" +grep -q '"bytes": 80' "$OUT/disjoint80.json"; check $? "disjoint peak 80" +"$BIN" eval $F/valid_tie_80.oev --asof 2026-10-24 > "$OUT/tie80.json"; rc=$? +check $rc "completion/allocation tie exits 0" +grep -q '"status": "OK"' "$OUT/tie80.json"; check $? "tie OK" +grep -q '"bytes": 80' "$OUT/tie80.json"; check $? "tie peak 80" +"$BIN" pareto $F/inverted_masks_overflow.oev $F/valid_disjoint_80.oev --asof 2026-10-24 > "$OUT/pareto_life.txt"; rc=$? +check $rc "pareto with a refused lifetime exits 0" +grep -q 'inverted_masks_overflow.oev *REFUSED_LIFETIME' "$OUT/pareto_life.txt"; check $? "inverted row is refused, not ranked OK" +grep -q 'valid_disjoint_80.oev *OK' "$OUT/pareto_life.txt"; check $? "valid row still ranked" + say "outputs in $OUT" exit $fail 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; } diff --git a/tools/.crumb b/tools/.crumb index e9e4932b5..64cd2aaed 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -62,14 +62,14 @@ } ], "children_root": "sha256:8d3d3b952374b3b72e667521f07bf028c4c02bcefff772ad1a71cfff3e5266f2", - "compiled_at": "2026-10-30T11:28:28Z", + "compiled_at": "2026-10-30T16:31:15Z", "compiler": "crumb-compile/1", - "digest": "sha256:308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", + "digest": "sha256:7a6ed3e1a9a2d14bbdd5d7fdd7080ef681db1f39a3ef306572c1a929a8913c54", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 298, - "generated_at_commit": "a8a6a3d3c47ce797430f045edc5eae11594ba36d", - "source_tree": "sha256:acd1f1b0080968db950791b4eb2dce39bb0289dc0c2e8794bfc7449023446105" + "generated_at_commit": "7521b2739cd72f42423f92d26879988080765d11", + "source_tree": "sha256:b1e02406c13951a779b4e39fa8f697d1bef9c2494216ef7ba51f6acbeb144aff" }, "owner_lane": "", "provenance": { diff --git a/tools/dagsim/dagsim.c b/tools/dagsim/dagsim.c index b7187d445..359a02437 100644 --- a/tools/dagsim/dagsim.c +++ b/tools/dagsim/dagsim.c @@ -31,6 +31,7 @@ const char *ds_status_name(int s) { case DS_REFUSED_CYCLE: return "REFUSED_CYCLE"; case DS_REFUSED_UNSUPPORTED: return "REFUSED_UNSUPPORTED"; case DS_REFUSED_REMOVED_REQUIRED: return "REFUSED_REMOVED_REQUIRED"; + case DS_REFUSED_LIFETIME: return "REFUSED_LIFETIME"; case DS_PARSE_ERROR: return "PARSE_ERROR"; default: return "?"; } @@ -279,7 +280,8 @@ int ds_parse_text(const char *text, ds_model *m, char *err, size_t errcap) { const char *v; if (!(v = get(kvs, n, "id"))) PERR("buffer needs id="); sset(b->id, sizeof b->id, v); - if (!parse_real(get(kvs, n, "bytes"), &b->bytes)) PERR("buffer needs bytes="); + if (!parse_real(get(kvs, n, "bytes"), &b->bytes) || !isfinite(b->bytes)) + PERR("buffer bytes must be a finite number >= 0"); if (!(v = get(kvs, n, "cap")) || (b->cap = ds_find_res(m, v)) < 0 || m->res[b->cap].kind != DS_RES_CAPACITY) PERR("buffer needs cap="); if ((v = get(kvs, n, "first")) && (b->first = ds_find_op(m, v)) < 0) PERR("first=%s not an op", v); if ((v = get(kvs, n, "last")) && (b->last = ds_find_op(m, v)) < 0) PERR("last=%s not an op", v); @@ -387,6 +389,19 @@ static int refuse(ds_result *r, int status, const char *fmt, const char *a, cons return status; } +static int refuse_msg(ds_result *r, int status, const char *msg) { + memset(r, 0, sizeof *r); + r->status = status; + snprintf(r->reason, sizeof r->reason, "%s", msg ? msg : ""); + return status; +} + +/* An unsupported operation and a refused prediction are the same as an + * unknown cell: no certificate. known alone is not enough. */ +static int predict_unusable(int st, const RxCmPrediction *p) { + return st == RX_CM_ERR_UNSUPPORTED || p->refused || !p->known || p->n == 0; /* PREDICT_REFUSAL_CHECK */ +} + static int resolve(const ds_model *m, const ds_opts *o, ds_num *num, ds_result *r) { int any_cal = 0; for (int i = 0; i < m->n_res; i++) { @@ -408,8 +423,9 @@ static int resolve(const ds_model *m, const ds_opts *o, ds_num *num, ds_result * f.op = RX_CM_OP_MATVEC; f.M = op->M; f.N = op->N; f.state_bytes = ((uint64_t)op->M * op->N + op->M + op->N) * 8u; f.cache_bytes = 1u << 20; f.core = op->core; f.eligible = 1u << op->arm; f.current_arm = op->arm; - RxCmPrediction p; rx_cm_predict(cm, &f, op->arm, &p); - if (!p.known || p.n == 0) return refuse(r, DS_REFUSED_UNKNOWN, "op %s: rx_costmodel has no known cell for this (op, core, pressure) and arm; measure it first%s", op->id, ""); + RxCmPrediction p; + int st = rx_cm_predict(cm, &f, op->arm, &p); + if (predict_unusable(st, &p)) return refuse(r, DS_REFUSED_UNKNOWN, "op %s: rx_costmodel has no known cell for this (op, core, pressure) and arm; measure it first%s", op->id, ""); double q = o->interval_q > 0 && o->interval_q < 1 ? o->interval_q : 0.9; double k = rx_cm_t_quantile(q, p.dof); double l2 = p.mean_log2_ps + (o->mode < 0 ? k * p.sd_log2 : o->mode > 0 ? -k * p.sd_log2 : 0); @@ -452,6 +468,33 @@ static int topo(const ds_model *m, int *order) { enum { ST_WAIT = 0, ST_READY, ST_LAUNCH, ST_RUN, ST_DONE }; enum { BLK_NONE = 0, BLK_SLOT, BLK_LAUNCHER }; +/* Half-open [start, end). Missing first -> 0; missing last -> makespan. */ +static void buffer_window(const ds_model *m, const ds_result *r, int b, double *s, double *e) { + *s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0.0; + *e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; +} + +static const char *endpoint_name(const ds_model *m, int op) { + return op >= 0 ? m->ops[op].id : "omitted"; +} + +/* Resolved end earlier than start is not an interval. Refuse before the ledger. */ +static int reject_inverted_lifetimes(const ds_model *m, ds_result *r) { + for (int b = 0; b < m->n_buf; b++) { + double s, e; + buffer_window(m, r, b, &s, &e); + if (e + 1e-9 < s) { + char msg[256]; + snprintf(msg, sizeof msg, + "inverted lifetime for buffer %s: first=%s start_ns=%.9g last=%s end_ns=%.9g", + m->buf[b].id, endpoint_name(m, m->buf[b].first), s, + endpoint_name(m, m->buf[b].last), e); + return refuse_msg(r, DS_REFUSED_LIFETIME, msg); + } + } + return DS_OK; +} + int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { memset(r, 0, sizeof *r); if (m->n_ops == 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "empty DAG%s%s", "", ""); @@ -578,7 +621,14 @@ int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { if (w > r->bound_ns) r->bound_ns = w; } - /* ---- capacity ledger: frees before allocations at the same instant ---- */ + /* Inverted lifetimes never enter the ledger (and never rank). */ + if ((rc = reject_inverted_lifetimes(m, r)) != DS_OK) return rc; /* LIFETIME_ORDER_CHECK */ + + /* ---- capacity ledger: frees before allocations at the same instant ---- + * Positive intervals only. A zero-duration [s, e) is empty and emits no + * event, so a same-time free cannot pull the ledger negative or offset + * another buffer. Frees of earlier intervals still precede allocs, which + * is the completion/allocation tie. */ for (int c = 0; c < m->n_res; c++) { if (m->res[c].kind != DS_RES_CAPACITY) continue; double live = 0, peak = 0, peak_at = 0; @@ -586,8 +636,9 @@ int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { double et[2 * DS_MAX_BUF]; double ev[2 * DS_MAX_BUF]; int en = 0; for (int b = 0; b < m->n_buf; b++) { if (m->buf[b].cap != c) continue; - double s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0; - double e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; + double s, e; + buffer_window(m, r, b, &s, &e); + if (!(s + 1e-9 < e)) continue; et[en] = s; ev[en] = m->buf[b].bytes; en++; et[en] = e; ev[en] = -m->buf[b].bytes; en++; } @@ -597,14 +648,26 @@ int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { while (j >= 0 && (et[j] > kt || (et[j] == kt && ev[j] > 0 && kv_ < 0))) { et[j + 1] = et[j]; ev[j + 1] = ev[j]; j--; } et[j + 1] = kt; ev[j + 1] = kv_; } - for (int i = 0; i < en; i++) { live += ev[i]; if (live > peak) { peak = live; peak_at = et[i]; } } + for (int i = 0; i < en; i++) { + live += ev[i]; + /* Reachable by rounding: 1e20 + 1 sums to 1e20, so the later + * frees overshoot (dagsim_unit rounding_drives_live_negative). */ + if (live < -1e-6) { + char msg[256]; + snprintf(msg, sizeof msg, + "capacity %s live bytes went negative (%.9g at t=%.9g); refusing the certificate", + m->res[c].id, live, et[i]); + return refuse_msg(r, DS_REFUSED_LIFETIME, msg); + } + if (live > peak) { peak = live; peak_at = et[i]; } + } r->peak_live_bytes[c] = peak; r->peak_live_at_ns[c] = peak_at; if (peak > m->res[c].cap + 1e-9) { char names[160] = ""; size_t L = 0; for (int b = 0; b < m->n_buf && L < sizeof names - 8; b++) { if (m->buf[b].cap != c) continue; - double s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0; - double e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; + double s, e; + buffer_window(m, r, b, &s, &e); if (s <= peak_at && e > peak_at) { snprintf(names + L, sizeof names - L, "%s%s", L ? "," : "", m->buf[b].id); L = strlen(names); } } char what[200]; snprintf(what, sizeof what, "%.0f B live at t=%.0f ns (%s) exceeds", peak, peak_at, names); diff --git a/tools/dagsim/dagsim.h b/tools/dagsim/dagsim.h index a409b588b..8ab62e55d 100644 --- a/tools/dagsim/dagsim.h +++ b/tools/dagsim/dagsim.h @@ -7,7 +7,8 @@ * bandwidth (processor sharing), serialized launches, one capacity ledger, * dependencies and barriers. It never executes anything. It refuses rather * than guess: unknown provenance, stale calibration, capacity overflow, - * unsupported constructs and cycles are all refusals with a reason. + * an inverted buffer lifetime (resolved end before start), unsupported + * constructs and cycles are all refusals with a reason. * * Host only. No GPU, no authority, no generation store. */ @@ -39,6 +40,7 @@ enum ds_status { DS_REFUSED_CYCLE, DS_REFUSED_UNSUPPORTED, /* construct the evaluator does not model */ DS_REFUSED_REMOVED_REQUIRED, /* a remaining op depends on removed work without a valid derivation */ + DS_REFUSED_LIFETIME, /* resolved buffer end is earlier than its start */ DS_PARSE_ERROR }; ```