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/blob/main/tests/runtime/rx_dual_jspace_tap.c # 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 branch `main` Branches Tags History `omega-mv / tests / runtime / rx_dual_jspace_tap.c` 688 lines ยท 32822 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * rx_dual_jspace_tap.c -- DUAL-3a parity tests for the read-only observation 19 * tap at decision site "jspace.residency" (js_forge_enforce, rx_jspace.c). 20 * 21 * Every scenario builds the same deterministic J-Space workload three times: 22 * once with no observer (today's behaviour), twice with a recording observer 23 * (legs A and B). Fixed JsCosts replace calibration so the chooser's inputs are 24 * identical run to run. Checked, per scenario: 25 * 26 * - every js_forge_enforce return code and every JsPolicyReport identical 27 * with and without the observer; 28 * - JsStats (actions, residency, peak, spilled, live, derives, copies) and 29 * the final space digest (every branch, unit, placement, extent, content 30 * check, then full content digests) identical with and without; 31 * - the record stream of leg A is byte-identical to leg B (determinism); 32 * - one record per considered candidate (count == JsPolicyReport.considered), 33 * seq strictly increasing from 1, canonical length JS_DUAL_OBS_BYTES, digest 34 * recomputed and equal; 35 * - each record's chosen action equals the decision the chooser rule gives on 36 * the record's own alternatives (first cheapest priced alternative, keep when 37 * its score is at or above pressure * retain); 38 * - each record's chosen action equals the placement transition that very 39 * realization underwent in the NO-OBSERVER run (slot + generation keyed), so 40 * the observer run took the same decision per realization as the silent run; 41 * - pressure value and case agree with the budget/residency bytes recorded. 42 * 43 * Pure-extraction check: choose() was split into decide() (working set exposed) 44 * plus a wrapper. reference_choose_pre_tap() below is the verbatim pre-change 45 * body (a class D reference oracle, test only). On every live realization of a 46 * fan-out workload, for both pressure values and both allow_move values, the 47 * two must agree. 48 * 49 * Negative control: built with -DDUAL_TAP_HOSTILE_TEST the leg-A observer casts 50 * its ctx to the space and raises retain_ns_per_byte on its third record. The 51 * decisions that follow differ from the silent run and this program MUST exit 1. 52 * The production source never defines or tests that macro (checked by grep in 53 * mk/dual_jspace_tap.mk). 54 * 55 * This file includes rx_jspace.c (not linked with it) so the reference oracle 56 * can call the static cost helpers and the digest can walk the index. 57 */ 58 #include "runtime/rx_jspace.c" 59 60 #include 61 #include 62 #include 63 #include 64 #include 65 #include 66 67 #define UNIT 4096u 68 #define U(x) ((unsigned long long)(x)) 69 70 static int g_fail; 71 #define CHECK(c, ...) do { if (!(c)) { g_fail++; printf(" FAIL: " __VA_ARGS__); printf("\n"); } } while (0) 72 73 /* ---- deterministic realizer -------------------------------------------------- */ 74 75 static uint64_t splitmix(uint64_t *x) { 76 uint64_t z = (*x += 0x9e3779b97f4a7c15ull); 77 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 78 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 79 return z ^ (z >> 31); 80 } 81 82 /* Root: pseudo-random fill. Derive: one word in eight changes, so a delta 83 * against the parent is compressible (COMPRESS becomes a real alternative). */ 84 static void derive_sparse(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { 85 uint64_t x = token; 86 if (!prev) { 87 for (size_t i = 0; i + 8 <= n; i += 8) { uint64_t v = splitmix(&x); memcpy(out + i, &v, 8); } 88 return; 89 } 90 memcpy(out, prev, n); 91 for (size_t i = (size_t)(token % 8) * 8; i + 8 <= n; i += 64) { 92 uint64_t v = splitmix(&x); 93 memcpy(out + i, &v, 8); 94 } 95 } 96 97 static const JsRealizer RZ = { JS_REAL_LATENT_CHECKPOINT, "sparse", UNIT, derive_sparse }; 98 99 /* Fixed costs. With n = UNIT, e = 1, parent HOT: 100 * MOVE 0.25 COMPRESS (0.5 + 0.5) / 0.75 = 1.333 SPILL 2 + 4 = 6 EVICT 1.0 101 * Soft threshold 1e3 * retain = 0.`9765625`: MOVE is taken, nothing else. 102 * Capacity threshold 1e9 * retain: EVICT (1.0) beats COMPRESS (1.333). */ 103 static void costs_plain(JsSpace *s) { 104 JsCosts c = { 0 }; 105 c.derive_ns = (double)UNIT; 106 c.copy_ns_per_byte = 0.125; 107 c.move_ns_per_byte = 0.125; 108 c.compress_ns_per_byte = 0.5; 109 c.decompress_ns_per_byte = 0.5; 110 c.compress_ratio = 0.25; 111 c.spill_write_ns_per_byte = 2.0; 112 c.spill_read_ns_per_byte = 4.0; 113 c.retain_ns_per_byte = 1.0 / 1024.0; 114 s->costs = c; 115 } 116 117 /* Tie costs: COMPRESS = (0.25 + 0.25) / 0.5 = 1.0 and EVICT = 1.0 exactly 118 * (parent HOT, e = 1). The chooser's strict "<" keeps the first built. */ 119 static void costs_tie(JsSpace *s) { 120 costs_plain(s); 121 s->costs.compress_ns_per_byte = 0.25; 122 s->costs.decompress_ns_per_byte = 0.25; 123 s->costs.compress_ratio = 0.5; 124 } 125 126 /* Spill-cheap costs: SPILL (0.125 + 0.125) = 0.25 beats EVICT 1.0 and COMPRESS. */ 127 static void costs_spill(JsSpace *s) { 128 costs_plain(s); 129 s->costs.spill_write_ns_per_byte = 0.125; 130 s->costs.spill_read_ns_per_byte = 0.125; 131 } 132 133 /* ---- record store ------------------------------------------------------------ */ 134 135 typedef struct { 136 uint8_t *bytes; /* JS_DUAL_OBS_BYTES + 32 digest per record */ 137 size_t n, cap; 138 JsDualObservation last; 139 int bad_len, bad_digest, bad_seq, bad_rule; 140 uint64_t next_seq; 141 JsSpace *space; /* hostile build only: the observer's way in */ 142 int tamper_record; /* normal build: write into the const record */ 143 } Rec; 144 145 #define REC_STRIDE (JS_DUAL_OBS_BYTES + 32u) 146 147 /* The chooser rule restated over a record's alternatives. */ 148 static uint8_t rule_of(const JsDualObservation *o) { 149 uint8_t act = JS_ACT_COUNT; 150 double best = 0; 151 for (unsigned i = 0; i < JS_DUAL_OBS_N_ALT; i++) { 152 if (o->alt[i].status != JS_DUAL_ALT_PRICED) continue; 153 if (act == JS_ACT_COUNT || o->alt[i].score < best) { best = o->alt[i].score; act = o->alt[i].action; } 154 } 155 if (act != JS_ACT_COUNT && best >= o->keep_threshold) return JS_ACT_COUNT; 156 return act; 157 } 158 159 static void record(const JsDualObservation *o, const void *ctx) { 160 Rec *r = (Rec *)ctx; 161 if (r->n == r->cap) { 162 r->cap = r->cap ? r->cap * 2 : 64; 163 r->bytes = realloc(r->bytes, r->cap * REC_STRIDE); 164 } 165 uint8_t *dst = r->bytes + r->n * REC_STRIDE; 166 size_t len = js_dual_observation_encode(o, dst); 167 if (len != JS_DUAL_OBS_BYTES) r->bad_len++; 168 sha256_ctx h; 169 uint8_t d[32]; 170 sha256_init(&h); 171 sha256_update(&h, (const uint8_t *)JS_DUAL_OBS_DOMAIN, sizeof JS_DUAL_OBS_DOMAIN); 172 sha256_update(&h, dst, len); 173 sha256_final(&h, d); 174 if (memcmp(d, o->digest, 32)) r->bad_digest++; 175 memcpy(dst + JS_DUAL_OBS_BYTES, o->digest, 32); 176 if (o->seq != r->next_seq) r->bad_seq++; 177 r->next_seq = o->seq + 1; 178 if (rule_of(o) != o->chosen) r->bad_rule++; 179 r->last = *o; 180 r->n++; 181 if (r->tamper_record) { 182 /* A hostile observer writes into the record it was handed. The chooser's 183 * result is a separate local: the production decision must not move. */ 184 JsDualObservation *w = (JsDualObservation *)o; 185 w->chosen = JS_ACT_EVICT; 186 w->pressure = 0; 187 } 188 #ifdef DUAL_TAP_HOSTILE_TEST 189 /* retain price 0 makes every priced alternative "at or above" the keep 190 * threshold: from here on the chooser keeps everything. */ 191 if (r->n == 3 && r->space) r->space->costs.retain_ns_per_byte = 0.0; 192 #endif 193 } 194 195 static void rec_free(Rec *r) { free(r->bytes); memset(r, 0, sizeof *r); } 196 197 /* ---- workload ---------------------------------------------------------------- */ 198 199 typedef struct { 200 const char *name; 201 void (*costs)(JsSpace *); 202 unsigned fanout, depth; 203 uint64_t max_spill; /* 0 = unlimited */ 204 int rounds; 205 /* budget per round: resident_bytes * num / den + add, computed when the round starts */ 206 struct { unsigned num, den; int64_t add; } budget[4]; 207 int rep_null; /* pass rep = NULL to enforce */ 208 int tamper; /* observer writes into the record */ 209 } Scenario; 210 211 static unsigned g_spill_seq; 212 213 static void space_open(JsSpace *s, const Scenario *sc) { 214 char path[128]; 215 snprintf(path, sizeof path, "/tmp/jstap_%ld_%u", (long)getpid(), g_spill_seq++); 216 JsLimits l; 217 js_limits_default(&l); 218 l.max_spill_bytes = sc->max_spill; 219 CHECK(js_space_init_limits(s, path, &l) == JS_OK, "%s: init", sc->name); 220 sc->costs(s); 221 } 222 223 /* One root with `depth` derived units, then `fanout` forks each deriving one 224 * more unit: fanout + depth + 1 realizations, all HOT. */ 225 static void program(JsSpace *s, const Scenario *sc, uint32_t *root_out) { 226 uint32_t root; 227 CHECK(js_branch_root(s, &RZ, 7, &root) == JS_OK, "%s: root", sc->name); 228 for (unsigned i = 0; i < sc->depth; i++) CHECK(js_branch_derive(s, root, 100 + i) == JS_OK, "%s: derive", sc->name); 229 for (unsigned k = 0; k < sc->fanout; k++) { 230 uint32_t c; 231 CHECK(js_branch_fork(s, root, &c) == JS_OK, "%s: fork %u", sc->name, k); 232 CHECK(js_branch_derive(s, c, 1000 + k) == JS_OK, "%s: derive %u", sc->name, k); 233 } 234 *root_out = root; 235 } 236 237 /* ---- state view -------------------------------------------------------------- */ 238 239 typedef struct { uint32_t slot; uint64_t gen; uint8_t placement; } Place; 240 241 static int place_cmp(const void *a, const void *b) { 242 const Place *x = a, *y = b; 243 return (x->slot > y->slot) - (x->slot < y->slot); 244 } 245 246 static uint32_t placements(JsSpace *s, Place **out) { 247 Cand *cs; 248 uint32_t n = collect(s, &cs); 249 Place *p = calloc(n ? n : 1, sizeof *p); 250 for (uint32_t i = 0; i < n; i++) { 251 p[i].slot = cs[i].r->slot; p[i].gen = cs[i].r->gen; p[i].placement = (uint8_t)cs[i].r->placement; 252 } 253 free(cs); 254 qsort(p, n, sizeof *p, place_cmp); 255 *out = p; 256 return n; 257 } 258 259 static void space_digest(JsSpace *s, uint8_t out[32]) { 260 sha256_ctx h; 261 sha256_init(&h); 262 for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) { 263 JsBranch *br = s->branches[b]; 264 if (!br) continue; 265 sha256_update(&h, (const uint8_t *)&b, 4); 266 sha256_update(&h, (const uint8_t *)&br->n_units, 4); 267 for (uint32_t i = 0; i < br->n_units; i++) { 268 const JsReal *r = br->units[i]; 269 uint64_t v[5] = { r->slot, r->gen, r->placement, r->packed_len, 270 r->placement == JS_PLACE_SPILLED ? r->spill_off : 0 }; 271 sha256_update(&h, (const uint8_t *)v, sizeof v); 272 sha256_update(&h, r->content, 32); 273 sha256_update(&h, r->semantic_state_id.b, 32); 274 } 275 } 276 sha256_update(&h, (const uint8_t *)&s->stats, sizeof s->stats); 277 sha256_update(&h, (const uint8_t *)&s->spill_end, sizeof s->spill_end); 278 /* Full content digests last: they restore bytes, identically in every leg. */ 279 for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) { 280 if (!s->branches[b]) continue; 281 uint8_t d[32] = { 0 }; 282 int rc = js_branch_content_digest(s, b, d); 283 sha256_update(&h, (const uint8_t *)&rc, sizeof rc); 284 sha256_update(&h, d, 32); 285 } 286 sha256_final(&h, out); 287 } 288 289 /* ---- one leg ----------------------------------------------------------------- */ 290 291 typedef struct { 292 int rc[4]; 293 JsPolicyReport rep[4]; 294 JsStats stats; 295 uint8_t digest[32]; 296 /* per round, the silent run's placement before and after enforce */ 297 Place *before[4], *after[4]; 298 uint32_t n_place[4]; 299 uint64_t resident_before[4], budget[4]; 300 } Leg; 301 302 static uint64_t budget_of(const Scenario *sc, int round, uint64_t resident) { 303 int64_t b = (int64_t)(resident * sc->budget[round].num / sc->budget[round].den) + sc->budget[round].add; 304 return b < 0 ? 0 : (uint64_t)b; 305 } 306 307 static void run_leg(const Scenario *sc, Rec *rec, Leg *leg, int keep_places) { 308 JsSpace s; 309 uint32_t root; 310 memset(leg, 0, sizeof *leg); 311 space_open(&s, sc); 312 program(&s, sc, &root); 313 if (rec) { rec->space = &s; js_dual_set_observer(&s, record, rec); } 314 for (int k = 0; k < sc->rounds; k++) { 315 /* Make everything resident again between rounds (COLD stays COLD) so 316 * each round decides again. */ 317 if (k) for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) { 318 JsBranch *br = s.branches[b]; 319 if (!br) continue; 320 for (uint32_t i = 0; i < br->n_units; i++) js_real_restore(&s, br->units[i]); 321 } 322 leg->resident_before[k] = s.stats.resident_bytes; 323 leg->budget[k] = budget_of(sc, k, s.stats.resident_bytes); 324 if (keep_places) leg->n_place[k] = placements(&s, &leg->before[k]); 325 memset(&leg->rep[k], 0, sizeof leg->rep[k]); 326 leg->rc[k] = js_forge_enforce(&s, leg->budget[k], sc->rep_null ? NULL : &leg->rep[k]); 327 if (keep_places) placements(&s, &leg->after[k]); 328 } 329 leg->stats = s.stats; 330 space_digest(&s, leg->digest); 331 js_space_destroy(&s); 332 } 333 334 static void leg_free(Leg *l) { 335 for (int k = 0; k < 4; k++) { free(l->before[k]); free(l->after[k]); } 336 } 337 338 static uint8_t transition(uint8_t from, uint8_t to) { 339 if (from == to) return JS_ACT_COUNT; 340 if (from == JS_PLACE_HOT && to == JS_PLACE_COLD) return JS_ACT_MOVE; 341 if (to == JS_PLACE_COMPRESSED) return JS_ACT_COMPRESS; 342 if (to == JS_PLACE_SPILLED) return JS_ACT_SPILL; 343 if (to == JS_PLACE_EVICTED) return JS_ACT_EVICT; 344 return 0xff; 345 } 346 347 /* ---- scenario ---------------------------------------------------------------- */ 348 349 static int same_rep(const JsPolicyReport *a, const JsPolicyReport *b) { return !memcmp(a, b, sizeof *a); } 350 351 static void run_scenario(const Scenario *sc) { 352 Leg silent, la, lb; 353 Rec ra = { 0 }, rb = { 0 }; 354 ra.next_seq = rb.next_seq = 1; 355 ra.tamper_record = rb.tamper_record = sc->tamper; 356 run_leg(sc, NULL, &silent, 1); 357 run_leg(sc, &ra, &la, 0); 358 run_leg(sc, &rb, &lb, 0); 359 360 uint64_t considered = 0; 361 for (int k = 0; k < sc->rounds; k++) { 362 CHECK(silent.rc[k] == la.rc[k] && silent.rc[k] == lb.rc[k], 363 "%s round %d: rc silent %d A %d B %d", sc->name, k, silent.rc[k], la.rc[k], lb.rc[k]); 364 CHECK(same_rep(&silent.rep[k], &la.rep[k]) && same_rep(&silent.rep[k], &lb.rep[k]), 365 "%s round %d: JsPolicyReport differs with observer", sc->name, k); 366 CHECK(silent.budget[k] == la.budget[k] && silent.resident_before[k] == la.resident_before[k], 367 "%s round %d: budget/residency differ before enforce", sc->name, k); 368 considered += silent.rep[k].considered; 369 } 370 CHECK(!memcmp(&silent.stats, &la.stats, sizeof silent.stats) && !memcmp(&silent.stats, &lb.stats, sizeof silent.stats), 371 "%s: JsStats differ with observer (resident silent %llu A %llu B %llu)", sc->name, 372 U(silent.stats.resident_bytes), U(la.stats.resident_bytes), U(lb.stats.resident_bytes)); 373 CHECK(!memcmp(silent.digest, la.digest, 32) && !memcmp(silent.digest, lb.digest, 32), 374 "%s: final space digest differs with observer", sc->name); 375 CHECK(ra.n == rb.n && (ra.n == 0 || !memcmp(ra.bytes, rb.bytes, ra.n * REC_STRIDE)), 376 "%s: record streams A (%zu) and B (%zu) differ", sc->name, ra.n, rb.n); 377 if (!sc->rep_null) 378 CHECK(ra.n == considered, "%s: %zu records for %llu considered candidates", sc->name, ra.n, U(considered)); 379 CHECK(!ra.bad_len && !ra.bad_digest && !ra.bad_seq, "%s: records malformed (len %d digest %d seq %d)", 380 sc->name, ra.bad_len, ra.bad_digest, ra.bad_seq); 381 CHECK(!ra.bad_rule, "%s: %d records whose chosen action is not the chooser rule on their alternatives", 382 sc->name, ra.bad_rule); 383 384 /* Per-realization parity with the silent run: walk leg A's records round by 385 * round (records are in enforce order; a round emits rep.considered of them) 386 * and compare chosen with the placement transition the silent run shows. */ 387 size_t at = 0; 388 for (int k = 0; k < sc->rounds && !sc->rep_null; k++) { 389 uint64_t n = silent.rep[k].considered; 390 for (uint64_t j = 0; j < n && at < ra.n; j++, at++) { 391 const uint8_t *enc = ra.bytes + at * REC_STRIDE; 392 /* fixed offsets of the canonical layout */ 393 uint8_t chosen = enc[37]; 394 uint8_t pcase = enc[35]; 395 double pressure; memcpy(&pressure, enc + 27, 8); 396 uint32_t slot; memcpy(&slot, enc + 199, 4); 397 uint64_t gen; memcpy(&gen, enc + 203, 8); 398 uint64_t budget, resident; memcpy(&budget, enc + 278, 8); memcpy(&resident, enc + 286, 8); 399 CHECK(budget == silent.budget[k], "%s round %d rec %llu: budget %llu != %llu", sc->name, k, U(j), U(budget), U(silent.budget[k])); 400 CHECK((pcase == JS_DUAL_PRESSURE_CAPACITY) == (resident > budget), 401 "%s round %d rec %llu: pressure case %u with resident %llu budget %llu", sc->name, k, U(j), pcase, U(resident), U(budget)); 402 CHECK(pressure == (pcase == JS_DUAL_PRESSURE_CAPACITY ? 1e9 : 1e3), 403 "%s round %d rec %llu: pressure %g for case %u", sc->name, k, U(j), pressure, pcase); 404 Place key = { slot, 0, 0 }, *pb, *pa; 405 pb = bsearch(&key, silent.before[k], silent.n_place[k], sizeof key, place_cmp); 406 pa = bsearch(&key, silent.after[k], silent.n_place[k], sizeof key, place_cmp); 407 CHECK(pb && pa && pb->gen == gen, "%s round %d rec %llu: realization slot %u gen %llu unknown to the silent run", 408 sc->name, k, U(j), slot, U(gen)); 409 if (!pb || !pa) continue; 410 uint8_t tr = transition(pb->placement, pa->placement); 411 /* The last record of a round that ended in an error names an action 412 * that was chosen but failed; its placement did not change. */ 413 int failed_last = silent.rc[k] != JS_OK && j == n - 1; 414 if (!failed_last) 415 CHECK(tr == chosen, "%s round %d rec %llu: observer saw %s, silent run did %s on slot %u", 416 sc->name, k, U(j), js_action_name(chosen), js_action_name(tr), slot); 417 else 418 CHECK(tr == JS_ACT_COUNT && chosen != JS_ACT_COUNT, 419 "%s round %d: failed last decision not visible as expected", sc->name, k); 420 } 421 } 422 423 printf(" %-22s rounds %d records %zu considered %llu rc[0] %d resident %llu digest %02x%02x%02x%02x %s\n", 424 sc->name, sc->rounds, ra.n, U(considered), silent.rc[0], U(silent.stats.resident_bytes), 425 silent.digest[0], silent.digest[1], silent.digest[2], silent.digest[3], 426 g_fail ? "(failures above)" : "ok"); 427 rec_free(&ra); rec_free(&rb); 428 leg_free(&silent); leg_free(&la); leg_free(&lb); 429 } 430 431 /* ---- scenario-specific assertions on record content -------------------------- */ 432 433 static void check_cases(void) { 434 /* Soft case: all HOT, total under budget, hot over half: every record is 435 * SOFT with pressure 1e3, MOVE allowed and chosen (0.25 < 0.`9765625`). */ 436 Scenario soft = { "soft-case", costs_plain, 16, 4, 0, 1, { { 3, 2, 0 } }, 0, 0 }; 437 Rec r = { 0 }; r.next_seq = 1; 438 Leg l; 439 run_leg(&soft, &r, &l, 0); 440 CHECK(r.n > 0, "soft: no records"); 441 int all_soft = 1, all_move = 1; 442 for (size_t i = 0; i < r.n; i++) { 443 const uint8_t *e = r.bytes + i * REC_STRIDE; 444 if (e[35] != JS_DUAL_PRESSURE_SOFT || e[36] != 1) all_soft = 0; 445 if (e[37] != JS_ACT_MOVE) all_move = 0; 446 } 447 CHECK(all_soft, "soft: a record was not the SOFT case with MOVE allowed"); 448 CHECK(all_move, "soft: a record chose something other than MOVE"); 449 CHECK(r.last.alt[0].status == JS_DUAL_ALT_PRICED && r.last.alt[0].score == 0.25, 450 "soft: MOVE score %g", r.last.alt[0].score); 451 CHECK(r.last.alt[3].status == JS_DUAL_ALT_PRICED && r.last.alt[3].score == 1.0, 452 "soft: EVICT score %g", r.last.alt[3].score); 453 CHECK(r.last.keep_threshold == 1e3 / 1024.0, "soft: keep threshold %g", r.last.keep_threshold); 454 CHECK(r.last.kind == JS_DUAL_OBS_KIND && r.last.version == JS_DUAL_OBS_VERSION, "soft: kind/version"); 455 CHECK(r.last.unit_bytes == UNIT && r.last.held_bytes == UNIT, "soft: unit/held bytes"); 456 rec_free(&r); leg_free(&l); 457 458 /* Capacity case: total over budget: MOVE inadmissible, EVICT chosen. */ 459 Scenario cap = { "capacity-case", costs_plain, 16, 4, 0, 1, { { 1, 2, 0 } }, 0, 0 }; 460 memset(&r, 0, sizeof r); r.next_seq = 1; 461 run_leg(&cap, &r, &l, 0); 462 /* Once total residency is back under budget the same enforce call goes on 463 * with the hot-arena SOFT case, so a run holds both cases: every CAPACITY 464 * record must have MOVE inadmissible and allow_move 0; the first record is 465 * CAPACITY with EVICT chosen under the 1e9 threshold. */ 466 CHECK(r.n > 0, "capacity: no records"); 467 int n_cap = 0, n_soft = 0, bad = 0; 468 for (size_t i = 0; i < r.n; i++) { 469 const uint8_t *e = r.bytes + i * REC_STRIDE; 470 if (e[35] == JS_DUAL_PRESSURE_CAPACITY) { 471 n_cap++; 472 if (e[36] != 0 || e[39] != JS_ACT_MOVE || e[40] != JS_DUAL_ALT_INADMISSIBLE) bad++; 473 } else n_soft++; 474 } 475 CHECK(n_cap > 0 && !bad, "capacity: %d CAPACITY records, %d with MOVE admissible", n_cap, bad); 476 { 477 const uint8_t *e0 = r.bytes; 478 double thr; memcpy(&thr, e0 + 183, 8); 479 CHECK(e0[35] == JS_DUAL_PRESSURE_CAPACITY && e0[37] == JS_ACT_EVICT && thr == 1e9 / 1024.0, 480 "capacity: first record case %u chose %s threshold %g", e0[35], js_action_name(e0[37]), thr); 481 } 482 printf(" capacity: %d CAPACITY then %d SOFT records in one enforce call\n", n_cap, n_soft); 483 rec_free(&r); leg_free(&l); 484 485 /* Ties: COMPRESS and EVICT price to exactly 1.0; the first built wins. */ 486 Scenario tie = { "tie-case", costs_tie, 16, 4, 0, 1, { { 1, 2, 0 } }, 0, 0 }; 487 memset(&r, 0, sizeof r); r.next_seq = 1; 488 run_leg(&tie, &r, &l, 0); 489 int ties = 0, tie_ok = 1; 490 for (size_t i = 0; i < r.n; i++) { 491 const uint8_t *e = r.bytes + i * REC_STRIDE; 492 double sc_c, sc_e, best; memcpy(&sc_c, e + 73 + 26, 8); memcpy(&sc_e, e + 141 + 26, 8); 493 memcpy(&best, e + 175, 8); 494 /* a tie between COMPRESS and EVICT that is also the cheapest price */ 495 if (e[74] == JS_DUAL_ALT_PRICED && e[142] == JS_DUAL_ALT_PRICED && sc_c == sc_e && best == sc_c) { 496 ties++; 497 if (e[37] != JS_ACT_COMPRESS) tie_ok = 0; 498 } 499 } 500 CHECK(ties > 0, "tie: no record where COMPRESS == EVICT is the cheapest price"); 501 printf(" ties: %d records with COMPRESS and EVICT tied at the cheapest price\n", ties); 502 CHECK(tie_ok, "tie: a tied record did not choose the first-built alternative"); 503 rec_free(&r); leg_free(&l); 504 } 505 506 /* ---- pure extraction: decide()/choose() against the pre-tap body ------------- */ 507 508 /* Verbatim copy of choose() as it was before DUAL-3a (reference oracle). */ 509 static JsAction reference_choose_pre_tap(const JsSpace *s, const JsReal *r, double pressure, bool allow_move) { 510 const JsCosts *c = &s->costs; 511 double n = (double)r->realizer->unit_bytes; 512 double e = expected_reads(r); 513 double best = 0; 514 JsAction act = JS_ACT_COUNT; /* keep */ 515 if (r->placement != JS_PLACE_HOT && r->placement != JS_PLACE_COLD && 516 r->placement != JS_PLACE_COMPRESSED) return JS_ACT_COUNT; 517 double held = r->placement == JS_PLACE_COMPRESSED ? (double)r->packed_len : n; 518 struct { JsAction a; double now, later, freed; } opt[4]; 519 int k = 0; 520 if (r->placement == JS_PLACE_HOT && allow_move) { 521 /* MOVE frees hot-arena bytes, not total residency. */ 522 opt[k++] = (typeof(opt[0])){ JS_ACT_MOVE, n * c->move_ns_per_byte, 0, n * 0.5 }; 523 } 524 if (r->placement != JS_PLACE_COMPRESSED) { 525 double p = predicted_packed(s, r); 526 if (p < n) 527 /* A delta is decoded against its parent's bytes, so restoring it 528 * also pays for producing those. */ 529 opt[k++] = (typeof(opt[0])){ JS_ACT_COMPRESS, n * c->compress_ns_per_byte, 530 n * c->decompress_ns_per_byte + 531 (r->parent_realization ? restore_cost(s, r->parent_realization) : 0), 532 n - p }; 533 } 534 opt[k++] = (typeof(opt[0])){ JS_ACT_SPILL, n * c->spill_write_ns_per_byte, 535 n * c->spill_read_ns_per_byte, held }; 536 opt[k++] = (typeof(opt[0])){ JS_ACT_EVICT, 0, recompute_cost(s, r), held }; 537 for (int i = 0; i < k; i++) { 538 if (opt[i].freed <= 0) continue; 539 /* nanoseconds of expected work per byte of residency given back */ 540 double cost = (opt[i].now + e * opt[i].later) / opt[i].freed; 541 if (act == JS_ACT_COUNT || cost < best) { best = cost; act = opt[i].a; } 542 } 543 /* Keep when holding the bytes is cheaper than the best way to give them up. */ 544 if (act != JS_ACT_COUNT && best >= pressure * c->retain_ns_per_byte) return JS_ACT_COUNT; 545 return act; 546 } 547 548 static void check_extraction(void) { 549 static void (*const cost_sets[3])(JsSpace *) = { costs_plain, costs_tie, costs_spill }; 550 static const double pressures[4] = { 1e3, 1e9, 0.0, 1e-3 }; 551 unsigned compared = 0, mismatches = 0; 552 for (int cs = 0; cs < 3; cs++) { 553 Scenario sc = { "extraction", cost_sets[cs], 64, 8, 0, 1, { { 1, 1, 0 } }, 0, 0 }; 554 JsSpace s; 555 uint32_t root; 556 space_open(&s, &sc); 557 program(&s, &sc, &root); 558 /* Mixed placements: some COLD, COMPRESSED, SPILLED, EVICTED. */ 559 Cand *cands; 560 uint32_t n = collect(&s, &cands); 561 for (uint32_t i = 0; i < n; i++) { 562 JsReal *r = cands[i].r; 563 switch (i % 5) { 564 case 1: js_real_move(&s, r); break; 565 case 2: js_real_compress(&s, r); break; 566 case 3: js_real_spill(&s, r); break; 567 case 4: js_real_evict(&s, r); break; 568 default: break; 569 } 570 } 571 for (uint32_t i = 0; i < n; i++) 572 for (int p = 0; p < 4; p++) 573 for (int am = 0; am < 2; am++) { 574 JsAction a = choose(&s, cands[i].r, pressures[p], am); 575 JsAction b = reference_choose_pre_tap(&s, cands[i].r, pressures[p], am); 576 compared++; 577 if (a != b) mismatches++; 578 /* and the public pure entry point stays the allow_move = true decision */ 579 if (am && js_forge_choose(&s, cands[i].r, pressures[p]) != b) mismatches++; 580 } 581 free(cands); 582 js_space_destroy(&s); 583 } 584 CHECK(mismatches == 0, "extraction: %u of %u decisions differ from the pre-tap chooser", mismatches, compared); 585 printf(" extraction: %u decisions compared with the pre-tap chooser, %u mismatches\n", compared, mismatches); 586 } 587 588 /* ---- encoding ----------------------------------------------------------------- */ 589 590 static void check_encoding(void) { 591 JsDualObservation o; 592 memset(&o, 0, sizeof o); 593 o.kind = JS_DUAL_OBS_KIND; o.version = JS_DUAL_OBS_VERSION; o.seq = 0x0102030405060708ull; 594 o.pressure = 1e3; o.pressure_case = JS_DUAL_PRESSURE_SOFT; o.allow_move = 1; o.chosen = JS_ACT_MOVE; 595 o.real_slot = 0xAABBCCDDu; o.budget_bytes = 0x1122334455667788ull; 596 uint8_t enc[JS_DUAL_OBS_BYTES]; 597 size_t len = js_dual_observation_encode(&o, enc); 598 CHECK(len == JS_DUAL_OBS_BYTES, "encode: %zu bytes, declared %u", len, JS_DUAL_OBS_BYTES); 599 CHECK(enc[0] == JS_DUAL_OBS_KIND && enc[1] == JS_DUAL_OBS_VERSION && enc[2] == JS_DUAL_SITE_ID_LEN, 600 "encode: kind/version/site length header"); 601 CHECK(!memcmp(enc + 3, JS_DUAL_SITE_ID, JS_DUAL_SITE_ID_LEN), "encode: site id"); 602 CHECK(enc[19] == 0x08 && enc[26] == 0x01, "encode: seq little-endian"); 603 CHECK(enc[199] == 0xDD && enc[202] == 0xAA, "encode: slot little-endian"); 604 CHECK(enc[278] == 0x88 && enc[285] == 0x11, "encode: budget little-endian at offset 278"); 605 uint8_t enc2[JS_DUAL_OBS_BYTES]; 606 js_dual_observation_encode(&o, enc2); 607 CHECK(!memcmp(enc, enc2, len), "encode: not deterministic"); 608 o.digest[0] = 1; 609 js_dual_observation_encode(&o, enc2); 610 CHECK(!memcmp(enc, enc2, len), "encode: digest field leaked into the canonical bytes"); 611 printf(" encoding: %zu canonical bytes, kind 0x%02x version %u, domain \"%s\"\n", 612 len, JS_DUAL_OBS_KIND, JS_DUAL_OBS_VERSION, JS_DUAL_OBS_DOMAIN); 613 } 614 615 /* ---- observer lifecycle ------------------------------------------------------- */ 616 617 static void check_lifecycle(void) { 618 Scenario sc = { "lifecycle", costs_plain, 8, 2, 0, 1, { { 1, 2, 0 } }, 0, 0 }; 619 JsSpace s; 620 uint32_t root; 621 space_open(&s, &sc); 622 program(&s, &sc, &root); 623 CHECK(s.dual_observer == NULL && s.dual_ctx == NULL && s.dual_seq == 0, "lifecycle: fresh space has an observer"); 624 JsPolicyReport rep = { 0 }; 625 CHECK(js_forge_enforce(&s, s.stats.resident_bytes / 2, &rep) == JS_OK, "lifecycle: enforce"); 626 CHECK(s.dual_seq == 0, "lifecycle: seq advanced without an observer"); 627 Rec r = { 0 }; r.next_seq = 1; 628 js_dual_set_observer(&s, record, &r); 629 for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) { 630 JsBranch *br = s.branches[b]; 631 if (!br) continue; 632 for (uint32_t i = 0; i < br->n_units; i++) js_real_restore(&s, br->units[i]); 633 } 634 memset(&rep, 0, sizeof rep); 635 CHECK(js_forge_enforce(&s, s.stats.resident_bytes / 2, &rep) == JS_OK, "lifecycle: enforce observed"); 636 CHECK(r.n == rep.considered && s.dual_seq == r.n, "lifecycle: %zu records, %llu considered, seq %llu", 637 r.n, U(rep.considered), U(s.dual_seq)); 638 js_dual_set_observer(&s, NULL, &r); 639 CHECK(s.dual_observer == NULL && s.dual_ctx == NULL, "lifecycle: clearing the observer left ctx behind"); 640 size_t before = r.n; 641 for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) { 642 JsBranch *br = s.branches[b]; 643 if (!br) continue; 644 for (uint32_t i = 0; i < br->n_units; i++) js_real_restore(&s, br->units[i]); 645 } 646 CHECK(js_forge_enforce(&s, s.stats.resident_bytes / 2, NULL) == JS_OK, "lifecycle: enforce after clear"); 647 CHECK(r.n == before, "lifecycle: records emitted after the observer was cleared"); 648 uint64_t seq = s.dual_seq; 649 rec_free(&r); 650 js_space_destroy(&s); 651 printf(" lifecycle: observer set, counted, cleared; seq %llu\n", U(seq)); 652 } 653 654 /* ---- main ---------------------------------------------------------------------- */ 655 656 int main(void) { 657 printf("DUAL-3a jspace.residency tap: parity%s\n", 658 #ifdef DUAL_TAP_HOSTILE_TEST 659 " [HOSTILE NEGATIVE CONTROL: the observer mutates the space; this run must FAIL]" 660 #else 661 "" 662 #endif 663 ); 664 check_encoding(); 665 check_lifecycle(); 666 check_extraction(); 667 check_cases(); 668 669 const Scenario scenarios[] = { 670 /* name, costs, fanout, depth, max_spill, rounds, budgets {num, den, add}, rep NULL, tamper */ 671 { "under-budget", costs_plain, 16, 4, 0, 1, { { 1000, 1, 0 } }, 0, 0 }, 672 { "soft-hot-arena", costs_plain, 32, 4, 0, 2, { { 3, 2, 0 }, { 3, 2, 0 } }, 0, 0 }, 673 { "over-total-capacity", costs_plain, 32, 4, 0, 2, { { 1, 2, 0 }, { 1, 4, 0 } }, 0, 0 }, 674 { "ties", costs_tie, 32, 4, 0, 2, { { 1, 2, 0 }, { 3, 2, 0 } }, 0, 0 }, 675 { "exactly-at-budget", costs_plain, 32, 4, 0, 1, { { 1, 1, 0 } }, 0, 0 }, 676 { "one-byte-below", costs_plain, 32, 4, 0, 1, { { 1, 1, +1 } }, 0, 0 }, 677 { "one-byte-above", costs_plain, 32, 4, 0, 1, { { 1, 1, -1 } }, 0, 0 }, 678 { "spill-exhaustion", costs_spill, 32, 4, 2 * UNIT, 2, { { 1, 2, 0 }, { 1, 2, 0 } }, 0, 0 }, 679 { "evict-drain", costs_plain, 32, 4, 0, 3, { { 0, 1, 0 }, { 0, 1, 0 }, { 1, 2, 0 } }, 0, 0 }, 680 { "fanout-500", costs_plain, 500, 4, 0, 3, { { 3, 2, 0 }, { 1, 2, 0 }, { 1, 8, 0 } }, 0, 0 }, 681 { "rep-null", costs_plain, 32, 4, 0, 2, { { 1, 2, 0 }, { 3, 2, 0 } }, 1, 0 }, 682 { "record-tamper", costs_plain, 32, 4, 0, 2, { { 1, 2, 0 }, { 3, 2, 0 } }, 0, 1 }, 683 }; 684 for (size_t i = 0; i < sizeof scenarios / sizeof scenarios[0]; i++) run_scenario(&scenarios[i]); 685 686 printf("DUAL_3A_JSPACE_TAP_PARITY=%s (failures=%d)\n", g_fail ? "FAIL" : "PASS", g_fail); 687 return g_fail ? 1 : 0; 688 }