diff --git a/.crumb b/.crumb index c824442e1..2bf37ec1c 100644 --- a/.crumb +++ b/.crumb @@ -64,7 +64,7 @@ }, { "digest": "ac43a69e03617abcc3c94f3fc1bc10c7593d9d57d49059c20bc4ce3abfaa8489", - "last_commit": "0710968", + "last_commit": "e59633e", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "078ee785ec85885891d21c67272be609d37d36eb4b516256a74e299d2dbc713d", - "last_commit": "70998dd", + "digest": "5a7af4c7ecebc57aae07e6eec5f187b21427ca5f9890364927fb21c344d927df", + "last_commit": "e39d0c5", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "b6e45fd97c85a40daeb3c943be669531e380092e0cd6e48e92e70a32b018af75", - "last_commit": "49475ff", + "digest": "ddb73f8dcc17dc0921d17e0f3f9a1216b32d4145f21e23a8845e4bf81d3ec2f5", + "last_commit": "e39d0c5", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:bdc9f7440c4a3ce29ce9b6063c5b643168371c593d406ef16396495e17fd3449", - "compiled_at": "2026-10-11T15:28:03Z", + "children_root": "sha256:861dd7ec42b6147528e8179462f3de9fc86b4df793770f43da7f7f493315f2c8", + "compiled_at": "2026-10-11T15:34:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:7a1f8730414063c3b8043efaa1ab1c12d424c734093d7de7cfc8d9227d4e4d71", + "digest": "sha256:a267c4c7730f70c4d3608f96694d3235f8db7ee81691c4383a5e1a72447d941c", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", - "files": 9663, - "generated_at_commit": "e03ab2063194f0ebde8a29f1835c16fa27a86bfc", - "source_tree": "sha256:f00f8a8233c957612d70a8539fa49c84546ea6ec8eaf7b7deac5318277b3c2de" + "files": 9666, + "generated_at_commit": "e39d0c58fa08564c61c92d43f1523f06d3a67d66", + "source_tree": "sha256:ae770ca9483afe52e3b2421751dee8d2b318da5166570924850da4875eae23ca" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/rx-host.yml b/.github/workflows/rx-host.yml index 4db1e9439..7db2422fd 100644 --- a/.github/workflows/rx-host.yml +++ b/.github/workflows/rx-host.yml @@ -280,6 +280,12 @@ jobs: grep -q '"tree_dirty": false,' "$receipt" sha256sum "$receipt" cat "$receipt" + - name: Bounded resource regulation (ADAPTIVE-M4) + env: + PHYSICS_DIR: ${{ github.workspace }}/physics-src + AIENOS_R7_DIR: ${{ github.workspace }}/aienos-src + AIENOS_COMMIT: ${{ steps.aienos.outputs.sha }} + run: make test-regulate - name: AIEN as a resident cognitive faculty (R11) env: OMEGA_CANDIDATE_COMMIT: ${{ github.sha }} diff --git a/Makefile b/Makefile index 7d640f676..75ab440e4 100644 --- a/Makefile +++ b/Makefile @@ -1318,6 +1318,20 @@ $(RX_R13_CORTEX): $(RX_R13_CORTEX_SRCS) tests/runtime/rx_r13_living.c src/runtim test-r13-cortex-episode: $(RX_R13_CORTEX) ./$(RX_R13_CORTEX) +# ADAPTIVE-M4: bounded resource regulation (src/runtime/rx_regulate.c) on the +# R11 host recipe. The regulator closes a loop on the World's admission budget +# inside a fixed envelope; the test measures deferrals per burst as the objective. +RX_REGULATE_TEST = $(OUT_DIR)/rx_regulate_test +RX_REGULATE_SRCS = $(filter-out tests/runtime/rx_r11_aien.c,$(RX_R11_SRCS)) \ + src/runtime/rx_regulate.c tests/runtime/rx_regulate_test.c +$(RX_REGULATE_TEST): $(RX_REGULATE_SRCS) src/runtime/rx_regulate.h src/runtime/rx_world.h \ + $(AIENOS_CAP_LIB) | $(OUT_DIR) + $(CC) $(CFLAGS) -pthread -o $@ $(RX_REGULATE_SRCS) $(AIENOS_CAP_LIB) -lm + +.PHONY: test-regulate +test-regulate: $(RX_REGULATE_TEST) + ./$(RX_REGULATE_TEST) + $(RX_R13_SILICON): $(RX_R13_SRCS) $(RX_R13_SILICON_EXTRA) src/runtime/rx_living.h src/runtime/rx_argus.h \ $(RX_COMPOSE_LIVING_CHECKS) $(ARGUS_STAMP) $(AIENOS_CAP_LIB) | $(OUT_DIR) $(RX_PROD_REFUSE_TEST) diff --git a/src/.crumb b/src/.crumb index d6a2034ca..ff25f0684 100644 --- a/src/.crumb +++ b/src/.crumb @@ -128,7 +128,7 @@ }, { "digest": "8c5cf96997407cff3f8a4c78150ae5294d2ed09a8d9076ae933e426a60792037", - "last_commit": "70998dd", + "last_commit": "e59633e", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, @@ -139,8 +139,8 @@ "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { - "digest": "9ca48dec58f57f6a8b9595fcf03318b766b5f0cf6fe2b7e286428cf2a4a5b98e", - "last_commit": "773bc36", + "digest": "72229d85f6c1384838c8509aa852daed769ea93190af5f7170aad438fbd16583", + "last_commit": "e39d0c5", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -175,15 +175,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:862233bb9e134a3a8b42e8aba7489af2b2a71b68a5675a0f03dbb054cf2044f0", - "compiled_at": "2026-10-11T15:28:03Z", + "children_root": "sha256:e1091a54c66a4e6f3356780dfc0939dc88980cb4e1143d70993ecfd653ef761d", + "compiled_at": "2026-10-11T15:34:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:078ee785ec85885891d21c67272be609d37d36eb4b516256a74e299d2dbc713d", + "digest": "sha256:5a7af4c7ecebc57aae07e6eec5f187b21427ca5f9890364927fb21c344d927df", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 449, - "generated_at_commit": "e03ab2063194f0ebde8a29f1835c16fa27a86bfc", - "source_tree": "sha256:38d40b0e65f0e13fd152c392130d4fccab4ec93a1f8f6926f67160edb432fc82" + "files": 451, + "generated_at_commit": "e39d0c58fa08564c61c92d43f1523f06d3a67d66", + "source_tree": "sha256:155b4110a0a583d1f3ce1a2e8a32be3525578cc0c0cf0b8723adcfb8d5b4efbf" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index 140282476..7445bcd4d 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T15:14:08Z", + "compiled_at": "2026-10-11T15:34:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:9ca48dec58f57f6a8b9595fcf03318b766b5f0cf6fe2b7e286428cf2a4a5b98e", + "digest": "sha256:72229d85f6c1384838c8509aa852daed769ea93190af5f7170aad438fbd16583", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 63, - "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", - "source_tree": "sha256:04e809d33fdbefe0a5c25559db70c691f68927578eefb6077feb78932891cb59" + "files": 65, + "generated_at_commit": "e39d0c58fa08564c61c92d43f1523f06d3a67d66", + "source_tree": "sha256:a5e4c83df3346dc48d5cd7a8ff1e5b768604bd640996247f79ade0427b428dfa" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_regulate.c b/src/runtime/rx_regulate.c new file mode 100644 index 000000000..3b7fc7006 --- /dev/null +++ b/src/runtime/rx_regulate.c @@ -0,0 +1,158 @@ +/* ADAPTIVE-M4: bounded resource regulation. Contract in rx_regulate.h. */ +#include "runtime/rx_regulate.h" + +#include + +static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) { + for (uint32_t i = 0; i < c->n_in; i++) + if (c->in[i].obj.id == r.id) return &c->in[i]; + return NULL; +} + +static void put(RxCtx *c, RxObjRef o, uint32_t field, uint64_t v) { + c->out[c->n_out++] = (RxMutation){ o, field, v }; +} + +int rx_regulate_envelope_ok(const RxRegulateEnvelope *e) { + if (!e) return RX_ERR_ARG; + if (e->slots_min == 0 || e->slots_min > e->slots_max) return RX_ERR_ARG; + if (e->memory_min > e->memory_max || e->energy_min > e->energy_max) return RX_ERR_ARG; + if (e->base.slots < e->slots_min || e->base.slots > e->slots_max) return RX_ERR_ARG; + if (e->base.memory_bytes < e->memory_min || e->base.memory_bytes > e->memory_max) + return RX_ERR_ARG; + if (e->base.energy_budget < e->energy_min || e->base.energy_budget > e->energy_max) + return RX_ERR_ARG; + if (e->contention_lo >= e->contention_hi) return RX_ERR_ARG; + if (e->pressure_lo >= e->pressure_hi || e->pressure_hi > 100) return RX_ERR_ARG; + if (e->min_dwell == 0) return RX_ERR_ARG; + return RX_OK; +} + +void rx_regulate_policy_for(const RxRegulateEnvelope *e, uint32_t regime, RxResourceBudget *out) { + *out = e->base; + if (regime == RX_REG_EXPAND) { + out->slots = e->slots_max; + out->memory_bytes = e->memory_max; + out->energy_budget = e->energy_max; + } else if (regime == RX_REG_PROTECT) { + out->slots = e->slots_min; + out->memory_bytes = e->memory_min; + out->energy_budget = e->energy_min; + } +} + +uint32_t rx_regulate_decide(const RxRegulateEnvelope *e, uint32_t regime, uint64_t dwell, + uint64_t contention, uint64_t pressure) { + int settled = dwell >= e->min_dwell; + /* Protection enters at once and wins over expansion. */ + if (pressure >= e->pressure_hi) return RX_REG_PROTECT; + switch (regime) { + case RX_REG_PROTECT: + if (!settled || pressure > e->pressure_lo) return RX_REG_PROTECT; + return contention >= e->contention_hi ? RX_REG_EXPAND : RX_REG_NOMINAL; + case RX_REG_EXPAND: + if (!settled || contention > e->contention_lo) return RX_REG_EXPAND; + return RX_REG_NOMINAL; + default: + return contention >= e->contention_hi ? RX_REG_EXPAND : RX_REG_NOMINAL; + } +} + +/* Policy written only here. A condition sample already decided on (same seq) + * publishes nothing, so a re-run of the same activation is a no-op. */ +static int fn_regulate(RxCtx *c) { + RxRegulator *r = c->user; + const RxSnapshotDep *cond = in_of(c, r->condition); + const RxSnapshotDep *pol = in_of(c, r->policy); + if (!cond || !pol) return RX_ERR_ARG; + uint64_t cseq = cond->field[0]; + if (cseq == pol->field[5]) return 0; + uint32_t regime = (uint32_t)pol->field[4]; + uint64_t dwell = pol->field[6]; + uint32_t next = rx_regulate_decide(&r->env, regime, dwell, cond->field[1], cond->field[3]); + put(c, r->policy, 5, cseq); + if (next == regime) { + put(c, r->policy, 6, dwell + 1); + return 0; + } + RxResourceBudget b; + rx_regulate_policy_for(&r->env, next, &b); + put(c, r->policy, 0, pol->field[0] + 1); + put(c, r->policy, 1, b.slots); + put(c, r->policy, 2, b.memory_bytes); + put(c, r->policy, 3, b.energy_budget); + put(c, r->policy, 4, next); + put(c, r->policy, 6, 0); + put(c, r->policy, 7, pol->field[7] + 1); + return 0; +} + +int rx_regulate_create_objects(RxRegulator *r, RxWorld *w, const RxRegulateEnvelope *e) { + if (!r || !w) return RX_ERR_ARG; + int rc = rx_regulate_envelope_ok(e); + if (rc != RX_OK) return rc; + memset(r, 0, sizeof(*r)); + r->w = w; + r->env = *e; + uint64_t z[RX_MAX_FIELDS] = { 0 }; + rc = rx_world_create(w, RX_OT_REG_CONDITION, RX_PERSIST_RESIDENT, + RX_REG_RES_BASE + RX_REG_RES_CONDITION, z, &r->condition); + if (rc != RX_OK) return rc; + uint64_t p[RX_MAX_FIELDS] = { 1, e->base.slots, e->base.memory_bytes, e->base.energy_budget, + RX_REG_NOMINAL, 0, 0, 0 }; + return rx_world_create(w, RX_OT_REG_POLICY, RX_PERSIST_RESIDENT, + RX_REG_RES_BASE + RX_REG_RES_POLICY, p, &r->policy); +} + +int rx_regulate_register(RxRegulator *r, const RxRegulateCaps *caps, const RxCallerCred *cred) { + if (!r || !r->w || !caps) return RX_ERR_ARG; + RxReactionDesc d; + memset(&d, 0, sizeof d); + d.name = "regulate.budget"; + d.faculty = RX_FACULTY_AIEN; + d.subject = RX_REG_SUBJ; + d.priority = RX_PRIO_CRITICAL; + d.fn = fn_regulate; + d.user = r; + if (cred) d.caller = *cred; + d.n_triggers = 1; + d.triggers[0] = (RxDep){ r->condition, RX_FIELD(0) }; + d.n_reads = 2; + d.reads[0] = (RxDep){ r->condition, RX_ALL_FIELDS }; + d.reads[1] = (RxDep){ r->policy, RX_ALL_FIELDS }; + d.n_writes = 1; + d.writes[0] = (RxDep){ r->policy, RX_ALL_FIELDS }; + d.n_caps = 2; + d.caps[0] = (RxCapNeed){ caps->condition_read, RX_REG_RES_BASE + RX_REG_RES_CONDITION, + RX_RIGHT_READ }; + d.caps[1] = (RxCapNeed){ caps->policy_rw, RX_REG_RES_BASE + RX_REG_RES_POLICY, + RX_RIGHT_READ | RX_RIGHT_WRITE }; + return rx_world_add_reaction(r->w, &d, &r->rid); +} + +int rx_regulate_apply(RxRegulator *r) { + if (!r || !r->w) return RX_ERR_ARG; + RxHaltStatus h; + rx_world_halt_status(r->w, &h); + if (h.halted) { r->refused++; return RX_ERR_HALTED; } + RxObject p; + int rc = rx_world_read(r->w, r->policy, &p); + if (rc != RX_OK) return rc; + if (p.field[0] == r->applied_version) return 1; + const RxRegulateEnvelope *e = &r->env; + RxResourceBudget b = e->base; /* fairness and offers: never regulated */ + b.slots = (uint32_t)p.field[1]; + b.memory_bytes = p.field[2]; + b.energy_budget = p.field[3]; + if (p.field[1] > UINT32_MAX || b.slots < e->slots_min || b.slots > e->slots_max || + b.memory_bytes < e->memory_min || b.memory_bytes > e->memory_max || + b.energy_budget < e->energy_min || b.energy_budget > e->energy_max || + p.field[4] > RX_REG_PROTECT) { + r->refused++; + return RX_ERR_BOUNDS; + } + rx_world_set_resources(r->w, &b); + r->applied = b; + r->applied_version = p.field[0]; + return RX_OK; +} diff --git a/src/runtime/rx_regulate.h b/src/runtime/rx_regulate.h new file mode 100644 index 000000000..7dda2546f --- /dev/null +++ b/src/runtime/rx_regulate.h @@ -0,0 +1,110 @@ +/* ADAPTIVE-M4: bounded resource regulation as a resident reaction. + * + * The World already prices admission against an RxResourceBudget + * (rx_world_set_resources: slots, memory, energy, fairness bound) and keeps + * reactions stable (RxStabilityBudget). Nothing in the runtime adapts that + * budget to what the machine is going through. This faculty closes that loop + * without touching the scheduler: + * + * condition object (written from outside: a sensor, a supervisor, a test) + * | trigger + * regulate.budget (one reaction: reads the condition and the current + * | policy, decides a regime with hysteresis, publishes + * v a bounded policy) + * policy object (written only by the regulator) + * | rx_regulate_apply (host step, never inside a reaction) + * v + * rx_world_set_resources + * + * Authority independence. The envelope (slots, memory, energy minimum and + * maximum, the nominal base budget, the fairness bound) is fixed when the + * objects are created and copied into the regulator. No capability, caller + * or object write can change it: a policy outside the envelope, however it + * got into the object, is refused at apply and the budget in force stays. + * The fields the regulator does not own (starvation_bound, locality, + * accelerator and compute offers, logical tick) are taken from the base + * budget on every apply, so fairness is never regulated away. + * + * Regimes. NOMINAL applies the base budget. EXPAND grants the envelope + * maximum when contention (deferred admissions in the window) reaches + * contention_hi and holds it until contention has fallen to contention_lo + * and the regime has been held for min_dwell samples. PROTECT applies the + * envelope minimum as soon as memory pressure reaches pressure_hi, takes + * precedence over EXPAND, and is left the same way (pressure_lo, dwell). + * Entering a regime is immediate; leaving needs the dead band and the dwell, + * so a signal that chatters around a threshold cannot flip the budget more + * than once per min_dwell + 1 samples and a signal inside the dead band + * never flips it. + * + * Emergency stop. The regulator runs under its own subject, so it can never + * stop or resume the world. While an operator stop is in force, outside + * condition publications are refused by the World and rx_regulate_apply + * refuses (RX_ERR_HALTED) without touching the budget. + * + * Objects. condition: 0 seq, 1 contention, 2 latency ns, 3 memory pressure + * (0..100), 4 deadline overdue, 5 window tick. policy: 0 version (changes + * only when the budget changes), 1 slots, 2 memory bytes, 3 energy budget, + * 4 regime, 5 condition seq decided on, 6 dwell (samples in this regime), + * 7 flips (regime changes so far). */ +#ifndef RX_REGULATE_H +#define RX_REGULATE_H + +#include "runtime/rx_world.h" + +#define RX_REG_SUBJ 37u +#define RX_REG_RES_BASE 0x5340000ull +enum { RX_REG_RES_CONDITION = 0, RX_REG_RES_POLICY, RX_REG_RES_COUNT }; +enum { RX_OT_REG_CONDITION = 0x5340u, RX_OT_REG_POLICY }; +enum { RX_REG_NOMINAL = 0, RX_REG_EXPAND = 1, RX_REG_PROTECT = 2 }; + +typedef struct { + uint32_t slots_min, slots_max; + uint64_t memory_min, memory_max; + uint64_t energy_min, energy_max; + RxResourceBudget base; /* NOMINAL; must lie inside the bounds above */ + uint64_t contention_hi, contention_lo; /* lo < hi */ + uint64_t pressure_hi, pressure_lo; /* lo < hi <= 100 */ + uint64_t min_dwell; /* >= 1 */ +} RxRegulateEnvelope; + +typedef struct { + RxCapRef condition_read; /* RX_REG_SUBJ on condition, READ */ + RxCapRef policy_rw; /* RX_REG_SUBJ on policy, READ|WRITE */ +} RxRegulateCaps; + +typedef struct { + RxWorld *w; + RxRegulateEnvelope env; + RxObjRef condition, policy; + uint32_t rid; + uint64_t applied_version; /* policy version in force (0: none applied yet) */ + RxResourceBudget applied; /* the budget rx_regulate_apply last installed */ + uint64_t refused; /* applies refused: outside the envelope or halted */ +} RxRegulator; + +/* RX_OK when every bound, the base budget and the thresholds are consistent. */ +int rx_regulate_envelope_ok(const RxRegulateEnvelope *e); + +/* Create the condition and policy objects (resources RX_REG_RES_BASE + i). + * The policy starts as NOMINAL, version 1. Copies the envelope. */ +int rx_regulate_create_objects(RxRegulator *r, RxWorld *w, const RxRegulateEnvelope *e); + +/* Register regulate.budget under RX_REG_SUBJ. `cred` is the subject's + * caller credential in a world that binds callers; null otherwise. */ +int rx_regulate_register(RxRegulator *r, const RxRegulateCaps *caps, const RxCallerCred *cred); + +/* Host step. Reads the policy and installs it with rx_world_set_resources + * when its version is new and it lies inside the envelope. RX_OK applied; + * 1 nothing new; RX_ERR_HALTED an operator stop is in force (budget + * untouched); RX_ERR_BOUNDS the policy is outside the envelope (budget + * untouched, counted in `refused`). Never call from a reaction or with the + * world lock held. */ +int rx_regulate_apply(RxRegulator *r); + +/* The pure decision, exposed so a test can drive it without a world. */ +uint32_t rx_regulate_decide(const RxRegulateEnvelope *e, uint32_t regime, uint64_t dwell, + uint64_t contention, uint64_t pressure); +/* The budget a regime stands for inside `e`. */ +void rx_regulate_policy_for(const RxRegulateEnvelope *e, uint32_t regime, RxResourceBudget *out); + +#endif diff --git a/tests/.crumb b/tests/.crumb index 661d1abbf..c83a9b7e3 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -212,7 +212,7 @@ }, { "digest": "d5153d318a11642a5e13824353222fc392ead92108540bc8ffd747fcfd40b813", - "last_commit": "49475ff", + "last_commit": "e59633e", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, @@ -259,8 +259,8 @@ "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { - "digest": "aa03a550299d9f3effb477f19a4c177b84b0679850d019430ba362e185b4317a", - "last_commit": "773bc36", + "digest": "6ce8e4d40746ec42f2f06437583fb91a613f25997fa0b427bbd640c6b4d2c649", + "last_commit": "e39d0c5", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -289,15 +289,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:69aaf7d04e800f1d8fb081bfdfedab3dcd5b607d257efcc39183f7d6dd4a2dce", - "compiled_at": "2026-10-11T15:28:03Z", + "children_root": "sha256:2187e90416056b45aa2201a569aaef1933d170b2d851ed0ceb0cf688b368675f", + "compiled_at": "2026-10-11T15:34:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:b6e45fd97c85a40daeb3c943be669531e380092e0cd6e48e92e70a32b018af75", + "digest": "sha256:ddb73f8dcc17dc0921d17e0f3f9a1216b32d4145f21e23a8845e4bf81d3ec2f5", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 959, - "generated_at_commit": "e03ab2063194f0ebde8a29f1835c16fa27a86bfc", - "source_tree": "sha256:c6b6cbd6ae8eb3d7b760b8e3f4a3ae74ba61cb05fc410973c178038300a4a8be" + "files": 960, + "generated_at_commit": "e39d0c58fa08564c61c92d43f1523f06d3a67d66", + "source_tree": "sha256:8ed8c7e50daade08217b085625db89ec345ab328a293331a4fe00f1e27737bfb" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 4fac95dad..1824de73e 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T15:14:08Z", + "compiled_at": "2026-10-11T15:34:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:aa03a550299d9f3effb477f19a4c177b84b0679850d019430ba362e185b4317a", + "digest": "sha256:6ce8e4d40746ec42f2f06437583fb91a613f25997fa0b427bbd640c6b4d2c649", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 65, - "generated_at_commit": "84107aed4de7fd16edc4fd3d19ac893a6d9c98e7", - "source_tree": "sha256:2df5ab291dd24ca44332c45a19729e5f2141155aeea05bfb6243e305be99d6eb" + "files": 66, + "generated_at_commit": "e39d0c58fa08564c61c92d43f1523f06d3a67d66", + "source_tree": "sha256:5d3a26ec62cda4ef4d99bbeef0897c2aecf8587f13cb5b6a01c78c60130cec15" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_regulate_test.c b/tests/runtime/rx_regulate_test.c new file mode 100644 index 000000000..67de727c8 --- /dev/null +++ b/tests/runtime/rx_regulate_test.c @@ -0,0 +1,417 @@ +/* ADAPTIVE-M4 host gate: bounded resource regulation closes a loop on the + * World's own admission budget, inside a fixed envelope, without weakening + * fairness or the operator emergency stop. + * + * A workload of 14 reactions (12 FOREGROUND, 2 BACKGROUND), each needing one + * MiB, is woken by one outside publication ("a burst"). The World admits as + * many as the budget allows; the rest wait in BLOCKED_RESOURCE and the + * World gauges how many waited at once (peak_blocked, as the heartbeat test + * reads it). That blocked-wait peak per burst is the declared objective: + * fewer is better. The test publishes the measured peak as the condition, + * lets the regulator decide, applies the policy, and measures the next burst. + * + * Phases: A nominal; B pressure -> EXPAND improves the objective; C calm -> + * back to NOMINAL; D hysteresis: chatter and dead band bound the flips, the + * World's own oscillation guard never trips; E envelope: a huge signal + * cannot exceed the maximum, a forged policy is refused at apply, PROTECT + * applies the minimum; F an operator stop halts the loop and resume restores + * it; G fairness: the bound is unchanged in every applied budget and the + * BACKGROUND reactions finish every burst. */ +#include "runtime/aienos_cap.h" +#include "runtime/rx_regulate.h" +#include "runtime/rx_world.h" + +#include +#include +#include +#include + +enum { SUBJ_EXTERNAL = 100, SUBJ_OPERATOR = 70, SUBJ_WORK = 41, ISSUER = 3 }; +enum { N_FG = 12, N_BG = 2, N_WORK = N_FG + N_BG }; +#define RES_TRIGGER 0x7741000ull +#define RES_WORK_BASE 0x7741100ull +#define OT_TRIGGER 0x7741u +#define OT_WORK 0x7742u +#define MIB (1ull << 20) +#define BASE_SB 4u + +static int g_checks, g_fail; +#define CHECK(cond, ...) do { \ + g_checks++; \ + if (!(cond)) { \ + g_fail++; \ + fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fputc('\n', stderr); \ + } \ + } while (0) + +typedef struct { + AienosCapAdmin *admin; + AienosCapView *view; + RxWorld w; + RxRegulator reg; + RxRegulateEnvelope env; + RxCallerCred cr_reg, cr_work, cr_op; + RxCapRef c_op, ext_condition, ext_trigger; + RxObjRef trigger, work[N_WORK]; + RxCapRef work_trigger_read, work_own[N_WORK]; + uint64_t trigger_seq, cond_seq; + uint64_t bg_done; +} Env; + +static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { + AienosCapRef office; + aienos_cap_office(e->admin, &office); + AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; + AienosCapRef r = { UINT32_MAX, 0 }; + if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; + return (RxCapRef){ r.cap_id, r.generation }; +} + +static uint64_t fld(Env *e, RxObjRef r, uint32_t i) { + RxObject o; + if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; + return o.field[i]; +} + +static int publish(Env *e, RxCapRef cap, RxObjRef o, const uint32_t *fields, + const uint64_t *vals, uint32_t n) { + RxMutation m[RX_MAX_FIELDS]; + for (uint32_t i = 0; i < n; i++) m[i] = (RxMutation){ o, fields[i], vals[i] }; + int64_t id = rx_world_publish_external(&e->w, cap, m, n); + return id > 0 ? 0 : -1; +} + +static void sleep_us(int us) { + struct timespec ts = { 0, (long)us * 1000L }; + nanosleep(&ts, NULL); +} + +/* ---- workload: one reaction per work object, woken by the trigger ---- */ +typedef struct { Env *e; uint32_t i; } Work; +static Work g_work[N_WORK]; + +static int fn_work(RxCtx *c) { + Work *wk = c->user; + uint64_t seq = 0, done = 0; + for (uint32_t i = 0; i < c->n_in; i++) { + if (c->in[i].obj.id == wk->e->trigger.id) seq = c->in[i].field[0]; + else if (c->in[i].obj.id == wk->e->work[wk->i].id) done = c->in[i].field[1]; + } + c->out[c->n_out++] = (RxMutation){ wk->e->work[wk->i], 0, seq }; + c->out[c->n_out++] = (RxMutation){ wk->e->work[wk->i], 1, done + 1 }; + return 0; +} + +static int register_work(Env *e) { + for (uint32_t i = 0; i < N_WORK; i++) { + g_work[i] = (Work){ e, i }; + RxReactionDesc d; + memset(&d, 0, sizeof d); + d.name = i < N_FG ? "work.fg" : "work.bg"; + d.faculty = RX_FACULTY_OMEGA; + d.subject = SUBJ_WORK; + d.caller = e->cr_work; + d.priority = i < N_FG ? RX_PRIO_FOREGROUND : RX_PRIO_BACKGROUND; + d.need.memory_bytes = MIB; + d.fn = fn_work; + d.user = &g_work[i]; + d.n_triggers = 1; + d.triggers[0] = (RxDep){ e->trigger, RX_FIELD(0) }; + d.n_reads = 2; + d.reads[0] = (RxDep){ e->trigger, RX_FIELD(0) }; + d.reads[1] = (RxDep){ e->work[i], RX_ALL_FIELDS }; + d.n_writes = 1; + d.writes[0] = (RxDep){ e->work[i], RX_ALL_FIELDS }; + d.n_caps = 2; + d.caps[0] = (RxCapNeed){ e->work_trigger_read, RES_TRIGGER, RX_RIGHT_READ }; + d.caps[1] = (RxCapNeed){ e->work_own[i], RES_WORK_BASE + i, RX_RIGHT_READ | RX_RIGHT_WRITE }; + uint32_t rid; + if (rx_world_add_reaction(&e->w, &d, &rid) != RX_OK) return -1; + } + return 0; +} + +static int env_start(Env *e) { + memset(e, 0, sizeof(*e)); + if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; + if (rx_world_init_native(&e->w, e->view, 4, 1u << 20) != RX_OK) return -1; + e->w.external_subject = SUBJ_EXTERNAL; + RxStabilityBudget st = { 0, 0, 3, 0, 0 }; /* the World's own A-B-A guard */ + rx_world_set_stability(&e->w, &st); + + uint64_t z[RX_MAX_FIELDS] = { 0 }; + if (rx_world_create(&e->w, OT_TRIGGER, RX_PERSIST_RESIDENT, RES_TRIGGER, z, &e->trigger) != RX_OK) + return -1; + for (uint32_t i = 0; i < N_WORK; i++) + if (rx_world_create(&e->w, OT_WORK, RX_PERSIST_RESIDENT, RES_WORK_BASE + i, z, + &e->work[i]) != RX_OK) return -1; + + RxRegulateEnvelope *v = &e->env; + memset(v, 0, sizeof(*v)); + v->slots_min = 1; v->slots_max = 8; + v->memory_min = 2 * MIB; v->memory_max = 16 * MIB; + v->energy_min = 1; v->energy_max = UINT64_MAX; + v->base.slots = 2; v->base.memory_bytes = 4 * MIB; v->base.energy_budget = UINT64_MAX; + v->base.starvation_bound = BASE_SB; + v->base.offered_locality = UINT32_MAX; v->base.offered_accel = UINT32_MAX; + v->base.compute_mask = UINT32_MAX; + v->contention_hi = 6; v->contention_lo = 2; + v->pressure_hi = 90; v->pressure_lo = 50; + v->min_dwell = 3; + if (rx_regulate_create_objects(&e->reg, &e->w, v) != RX_OK) return -1; + + if (rx_world_enroll_caller(&e->w, SUBJ_OPERATOR, &e->cr_op) != RX_CALLER_OK || + rx_world_enroll_caller(&e->w, RX_REG_SUBJ, &e->cr_reg) != RX_CALLER_OK || + rx_world_enroll_caller(&e->w, SUBJ_WORK, &e->cr_work) != RX_CALLER_OK) return -1; + e->c_op = mint(e, SUBJ_OPERATOR, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT); + e->ext_condition = mint(e, SUBJ_EXTERNAL, RX_REG_RES_BASE + RX_REG_RES_CONDITION, RX_RIGHT_WRITE); + e->ext_trigger = mint(e, SUBJ_EXTERNAL, RES_TRIGGER, RX_RIGHT_WRITE); + e->work_trigger_read = mint(e, SUBJ_WORK, RES_TRIGGER, RX_RIGHT_READ); + for (uint32_t i = 0; i < N_WORK; i++) + e->work_own[i] = mint(e, SUBJ_WORK, RES_WORK_BASE + i, RX_RIGHT_READ | RX_RIGHT_WRITE); + RxRegulateCaps rc = { + mint(e, RX_REG_SUBJ, RX_REG_RES_BASE + RX_REG_RES_CONDITION, RX_RIGHT_READ), + mint(e, RX_REG_SUBJ, RX_REG_RES_BASE + RX_REG_RES_POLICY, RX_RIGHT_READ | RX_RIGHT_WRITE), + }; + if (rx_regulate_register(&e->reg, &rc, &e->cr_reg) != RX_OK) return -1; + if (register_work(e) != 0) return -1; + return rx_world_bind_callers(&e->w) == RX_OK ? 0 : -1; +} + +static void env_stop(Env *e) { + rx_world_wait_quiescent(&e->w, 20000); + rx_world_destroy(&e->w); + aienos_cap_stop(e->admin, e->view); +} + +/* One burst: wake the whole workload, wait for every reaction to finish, + * return how many waited for a slot at once. Negative when the publication + * was refused. The gauge is the World's own; the rig resets it per burst. */ +static int64_t burst(Env *e) { + e->w.peak_blocked = 0; + uint64_t seq = ++e->trigger_seq; + uint32_t f = 0; + if (publish(e, e->ext_trigger, e->trigger, &f, &seq, 1) != 0) { e->trigger_seq--; return -1; } + for (int spin = 0; spin < 5000; spin++) { + uint32_t done = 0; + for (uint32_t i = 0; i < N_WORK; i++) done += fld(e, e->work[i], 0) == seq; + if (done == N_WORK) break; + sleep_us(1000); + } + rx_world_wait_quiescent(&e->w, 20000); + uint32_t done = 0, bg = 0; + for (uint32_t i = 0; i < N_WORK; i++) { + done += fld(e, e->work[i], 0) == seq; + if (i >= N_FG) bg += fld(e, e->work[i], 0) == seq; + } + CHECK(done == N_WORK, "burst %llu: %u of %u reactions finished", + (unsigned long long)seq, done, N_WORK); + e->bg_done += bg; + return (int64_t)e->w.peak_blocked; +} + +/* Publish one condition sample and let the regulator settle on it. */ +static int sample(Env *e, uint64_t contention, uint64_t pressure) { + uint64_t seq = e->cond_seq + 1; + uint32_t f[4] = { 0, 1, 3, 5 }; + uint64_t v[4] = { seq, contention, pressure, e->trigger_seq }; + if (publish(e, e->ext_condition, e->reg.condition, f, v, 4) != 0) return -1; + e->cond_seq = seq; + rx_world_wait_quiescent(&e->w, 20000); + CHECK(fld(e, e->reg.policy, 5) == seq, "regulator did not decide on sample %llu", + (unsigned long long)seq); + return 0; +} + +static uint64_t regime(Env *e) { return fld(e, e->reg.policy, 4); } +static uint64_t flips(Env *e) { return fld(e, e->reg.policy, 7); } + +/* Every applied budget keeps what the regulator does not own. */ +static void check_untouched(Env *e, const char *when) { + const RxResourceBudget *a = &e->reg.applied, *b = &e->env.base; + CHECK(a->starvation_bound == BASE_SB && a->offered_locality == b->offered_locality && + a->offered_accel == b->offered_accel && a->compute_mask == b->compute_mask && + a->logical_tick == b->logical_tick, "%s: a field outside the policy changed", when); + CHECK(e->w.budget.starvation_bound == BASE_SB, "%s: world fairness bound is %u", + when, e->w.budget.starvation_bound); +} + +static void row(const char *phase, Env *e, int64_t deferred) { + printf("M4 %-10s regime %llu slots %u memory %llu MiB blocked-wait/burst %lld flips %llu refused %llu\n", + phase, (unsigned long long)regime(e), e->w.budget.slots, + (unsigned long long)(e->w.budget.memory_bytes / MIB), (long long)deferred, + (unsigned long long)flips(e), (unsigned long long)e->reg.refused); +} + +/* ---- pure decision checks, no world ---- */ +static void decision_checks(const RxRegulateEnvelope *v) { + CHECK(rx_regulate_decide(v, RX_REG_NOMINAL, 0, v->contention_hi, 0) == RX_REG_EXPAND, + "nominal must expand at contention_hi"); + CHECK(rx_regulate_decide(v, RX_REG_NOMINAL, 0, v->contention_hi - 1, 0) == RX_REG_NOMINAL, + "nominal must hold below contention_hi"); + CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell, v->contention_lo + 1, 0) == RX_REG_EXPAND, + "expand must hold above contention_lo (dead band)"); + CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell - 1, 0, 0) == RX_REG_EXPAND, + "expand must hold before min_dwell"); + CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell, v->contention_lo, 0) == RX_REG_NOMINAL, + "expand must return to nominal once calm and settled"); + CHECK(rx_regulate_decide(v, RX_REG_EXPAND, 0, UINT64_MAX, v->pressure_hi) == RX_REG_PROTECT, + "protect wins over expand"); + CHECK(rx_regulate_decide(v, RX_REG_PROTECT, v->min_dwell, 0, v->pressure_lo + 1) == RX_REG_PROTECT, + "protect must hold above pressure_lo"); + CHECK(rx_regulate_decide(v, RX_REG_PROTECT, v->min_dwell, v->contention_hi, v->pressure_lo) == RX_REG_EXPAND, + "protect leaves to expand when contention is high"); + RxRegulateEnvelope bad = *v; + bad.base.slots = v->slots_max + 1; + CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "base outside the bounds must be refused"); + bad = *v; bad.contention_lo = bad.contention_hi; + CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "empty dead band must be refused"); + bad = *v; bad.min_dwell = 0; + CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "zero dwell must be refused"); + RxResourceBudget b; + rx_regulate_policy_for(v, RX_REG_EXPAND, &b); + CHECK(b.slots == v->slots_max && b.memory_bytes == v->memory_max && b.starvation_bound == BASE_SB, + "expand policy must be the envelope maximum with the base fairness bound"); + rx_regulate_policy_for(v, RX_REG_PROTECT, &b); + CHECK(b.slots == v->slots_min && b.memory_bytes == v->memory_min, "protect policy must be the minimum"); +} + +int main(void) { + Env e; + if (env_start(&e) != 0) { fprintf(stderr, "env_start failed\n"); return 2; } + decision_checks(&e.env); + + /* A: nominal. The first apply installs the base budget. */ + CHECK(rx_regulate_apply(&e.reg) == RX_OK, "first apply must install the base budget"); + CHECK(rx_regulate_apply(&e.reg) == 1, "second apply must find nothing new"); + CHECK(e.w.budget.slots == e.env.base.slots, "base slots not in force: %u", e.w.budget.slots); + check_untouched(&e, "A"); + int64_t d_nom = burst(&e); + int64_t d_nom2 = burst(&e); + CHECK(d_nom > 0 && d_nom2 > 0, "nominal bursts must make work wait (%lld, %lld)", (long long)d_nom, (long long)d_nom2); + CHECK(regime(&e) == RX_REG_NOMINAL && flips(&e) == 0, "A: regime %llu flips %llu", + (unsigned long long)regime(&e), (unsigned long long)flips(&e)); + row("A nominal", &e, d_nom2); + + /* B: pressure. Measured contention goes in, a bounded policy comes out, + * and the next burst is cheaper. */ + CHECK(sample(&e, (uint64_t)d_nom2, 10) == 0, "B sample refused"); + CHECK(regime(&e) == RX_REG_EXPAND && flips(&e) == 1, "B: regime %llu flips %llu", + (unsigned long long)regime(&e), (unsigned long long)flips(&e)); + CHECK(rx_regulate_apply(&e.reg) == RX_OK, "B: apply must install the expand policy"); + CHECK(e.w.budget.slots == e.env.slots_max && e.w.budget.memory_bytes == e.env.memory_max, + "B: expand budget not in force (slots %u)", e.w.budget.slots); + check_untouched(&e, "B"); + int64_t d_exp = burst(&e); + CHECK(d_exp >= 0 && d_exp < d_nom2, "B: expand must improve the objective: %lld vs nominal %lld", + (long long)d_exp, (long long)d_nom2); + row("B expand", &e, d_exp); + + /* C: calm. Below the dead band for min_dwell samples returns to nominal. */ + for (uint64_t i = 0; i < e.env.min_dwell; i++) { + CHECK(sample(&e, (uint64_t)d_exp < e.env.contention_lo ? (uint64_t)d_exp : 0, 10) == 0, "C sample refused"); + CHECK(regime(&e) == RX_REG_EXPAND, "C: left expand before min_dwell at sample %llu", (unsigned long long)i); + } + CHECK(sample(&e, 0, 10) == 0, "C sample refused"); + CHECK(regime(&e) == RX_REG_NOMINAL && flips(&e) == 2, "C: regime %llu flips %llu", + (unsigned long long)regime(&e), (unsigned long long)flips(&e)); + CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots, + "C: base budget not restored (slots %u)", e.w.budget.slots); + check_untouched(&e, "C"); + int64_t d_rec = burst(&e); + CHECK(d_rec > d_exp, "C: recovery must make work wait again: %lld vs %lld", + (long long)d_rec, (long long)d_exp); + row("C recover", &e, d_rec); + + /* D: hysteresis. Chatter across both thresholds flips at most once per + * min_dwell + 1 samples; chatter inside the dead band never flips. The + * World's own oscillation guard (limit 3) must not trip either. */ + uint64_t f0 = flips(&e); + const int N_CHATTER = 24; + for (int i = 0; i < N_CHATTER; i++) + CHECK(sample(&e, i % 2 ? 0 : e.env.contention_hi, 10) == 0, "D sample refused"); + uint64_t chatter_flips = flips(&e) - f0; + uint64_t bound = (uint64_t)N_CHATTER / (e.env.min_dwell + 1) + 2; + CHECK(chatter_flips <= bound, "D: chatter caused %llu flips, bound %llu", + (unsigned long long)chatter_flips, (unsigned long long)bound); + CHECK(chatter_flips >= 2, "D: chatter must still be followed (flips %llu)", (unsigned long long)chatter_flips); + /* settle to nominal, then drive the dead band */ + for (uint64_t i = 0; i <= e.env.min_dwell; i++) CHECK(sample(&e, 0, 10) == 0, "D settle refused"); + CHECK(regime(&e) == RX_REG_NOMINAL, "D: not nominal after settling"); + f0 = flips(&e); + for (int i = 0; i < 10; i++) + CHECK(sample(&e, i % 2 ? e.env.contention_lo + 1 : e.env.contention_hi - 1, 10) == 0, "D band refused"); + CHECK(flips(&e) == f0, "D: dead-band chatter flipped the policy %llu times", + (unsigned long long)(flips(&e) - f0)); + CHECK(e.w.stats.oscillation_trips == 0 && e.w.stats.quarantines == 0 && + !e.w.reactions[e.reg.rid].quarantined, + "D: World guard tripped (osc %llu quarantines %llu)", + (unsigned long long)e.w.stats.oscillation_trips, (unsigned long long)e.w.stats.quarantines); + CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots, + "D: apply after chatter must land on the base budget"); + row("D chatter", &e, -1); + + /* E: envelope. A huge signal gives exactly the maximum; a forged policy + * (the test, as issuer, mints a write it should never have) is refused + * at apply and the budget in force stays; PROTECT gives the minimum. */ + CHECK(sample(&e, UINT64_MAX, 10) == 0, "E sample refused"); + CHECK(regime(&e) == RX_REG_EXPAND && fld(&e, e.reg.policy, 1) == e.env.slots_max && + fld(&e, e.reg.policy, 2) == e.env.memory_max, + "E: policy escaped the maximum (slots %llu)", (unsigned long long)fld(&e, e.reg.policy, 1)); + CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.slots_max, "E: expand apply"); + CHECK(publish(&e, e.ext_condition, e.reg.policy, (uint32_t[]){ 1 }, (uint64_t[]){ 1000 }, 1) != 0, + "E: the condition writer must not be able to write the policy"); + RxCapRef forged = mint(&e, SUBJ_EXTERNAL, RX_REG_RES_BASE + RX_REG_RES_POLICY, RX_RIGHT_WRITE); + uint64_t ver = fld(&e, e.reg.policy, 0); + uint32_t ff[2] = { 0, 1 }; + uint64_t fv[2] = { ver + 100, (uint64_t)e.env.slots_max * 4 }; + CHECK(publish(&e, forged, e.reg.policy, ff, fv, 2) == 0, "E: forged write did not land (test rig)"); + rx_world_wait_quiescent(&e.w, 20000); + uint64_t refused0 = e.reg.refused; + CHECK(rx_regulate_apply(&e.reg) == RX_ERR_BOUNDS && e.reg.refused == refused0 + 1, + "E: forged policy must be refused at apply"); + CHECK(e.w.budget.slots == e.env.slots_max, "E: forged policy changed the budget (slots %u)", e.w.budget.slots); + check_untouched(&e, "E"); + CHECK(sample(&e, 0, e.env.pressure_hi) == 0, "E protect sample refused"); + CHECK(regime(&e) == RX_REG_PROTECT, "E: pressure did not protect"); + CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.slots_min && + e.w.budget.memory_bytes == e.env.memory_min, "E: protect budget not in force (slots %u)", e.w.budget.slots); + check_untouched(&e, "E protect"); + int64_t d_prot = burst(&e); + CHECK(d_prot >= d_nom2, "E: protect must not wait less than nominal: %lld vs %lld", + (long long)d_prot, (long long)d_nom2); + row("E protect", &e, d_prot); + + /* F: operator stop in the middle of regulation. The World refuses the + * condition and the burst, apply refuses, nothing moves; resume restores. */ + RxHaltStatus h; + CHECK(rx_world_emergency_stop(&e.w, SUBJ_OPERATOR, &e.cr_op, e.c_op, 4, &h) == RX_OK && h.halted, + "F: operator stop refused"); + uint32_t slots_at_stop = e.w.budget.slots; + CHECK(sample(&e, UINT64_MAX, 10) != 0, "F: condition publication must be refused while stopped"); + CHECK(burst(&e) < 0, "F: burst must be refused while stopped"); + CHECK(rx_regulate_apply(&e.reg) == RX_ERR_HALTED, "F: apply must refuse while stopped"); + CHECK(e.w.budget.slots == slots_at_stop, "F: budget moved during the stop"); + CHECK(rx_world_emergency_resume(&e.w, SUBJ_OPERATOR, &e.cr_op, e.c_op, &h) == RX_OK && !h.halted, + "F: resume refused"); + for (uint64_t i = 0; i <= e.env.min_dwell; i++) CHECK(sample(&e, 0, 10) == 0, "F sample refused"); + CHECK(regime(&e) == RX_REG_NOMINAL, "F: not nominal after resume"); + CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots, "F: apply after resume"); + int64_t d_after = burst(&e); + CHECK(d_after > 0, "F: burst after resume"); + row("F resumed", &e, d_after); + + /* G: fairness. The BACKGROUND reactions finished every burst that ran. */ + uint64_t bursts = e.trigger_seq; + CHECK(e.bg_done == bursts * N_BG, "G: background reactions finished %llu of %llu", + (unsigned long long)e.bg_done, (unsigned long long)(bursts * N_BG)); + check_untouched(&e, "G"); + CHECK(e.w.stats.quarantines == 0 && e.w.stats.oscillation_trips == 0, "G: World guard tripped"); + row("G fairness", &e, -1); + + env_stop(&e); + printf("RX_REGULATE %s: pass %d fail %d\n", g_fail ? "FAIL" : "PASS", g_checks - g_fail, g_fail); + return g_fail ? 1 : 0; +}