interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / runtime / rx_regulate_test.c

433 lines · 22275 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/* ADAPTIVE-M4 host gate: bounded resource regulation closes a loop on the
18 * World's own admission budget, inside a fixed envelope, without weakening
19 * fairness or the operator emergency stop.
20 *
21 * A workload of 14 reactions (12 FOREGROUND, 2 BACKGROUND), each needing one
22 * MiB, is woken by one outside publication ("a burst"). The World admits as
23 * many as the budget allows; the rest wait in BLOCKED_RESOURCE and the
24 * World gauges how many waited at once (peak_blocked, as the heartbeat test
25 * reads it). That blocked-wait peak per burst is the declared objective:
26 * fewer is better. The test publishes the measured peak as the condition,
27 * lets the regulator decide, applies the policy, and measures the next burst.
28 *
29 * Phases: A nominal; B pressure -> EXPAND improves the objective; C calm ->
30 * back to NOMINAL; D hysteresis: chatter and dead band bound the flips, the
31 * World's own oscillation guard never trips; E envelope: a huge signal
32 * cannot exceed the maximum, a forged policy is refused at apply, PROTECT
33 * applies the minimum; F an operator stop halts the loop and resume restores
34 * it; G fairness: the bound is unchanged in every applied budget and the
35 * BACKGROUND reactions finish every burst. */
36#include "runtime/aienos_cap.h"
37#include "runtime/rx_regulate.h"
38#include "runtime/rx_world.h"
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <time.h>
44
45enum { SUBJ_EXTERNAL = 100, SUBJ_OPERATOR = 70, SUBJ_WORK = 41, ISSUER = 3 };
46enum { N_FG = 12, N_BG = 2, N_WORK = N_FG + N_BG };
47#define RES_TRIGGER 0x7741000ull
48#define RES_WORK_BASE 0x7741100ull
49#define OT_TRIGGER 0x7741u
50#define OT_WORK 0x7742u
51#define MIB (1ull << 20)
52#define BASE_SB 4u
53
54static int g_checks, g_fail;
55#define CHECK(cond, ...) do { \
56 g_checks++; \
57 if (!(cond)) { \
58 g_fail++; \
59 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
60 fprintf(stderr, __VA_ARGS__); \
61 fputc('\n', stderr); \
62 } \
63 } while (0)
64
65typedef struct {
66 AienosCapAdmin *admin;
67 AienosCapView *view;
68 RxWorld w;
69 RxRegulator reg;
70 RxRegulateEnvelope env;
71 RxCallerCred cr_reg, cr_work, cr_op;
72 RxCapRef c_op, ext_condition, ext_trigger;
73 RxObjRef trigger, work[N_WORK];
74 RxCapRef work_trigger_read, work_own[N_WORK];
75 uint64_t trigger_seq, cond_seq;
76 uint64_t bg_done;
77} Env;
78
79static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
80 AienosCapRef office;
81 aienos_cap_office(e->admin, &office);
82 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
83 AienosCapRef r = { UINT32_MAX, 0 };
84 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 };
85 return (RxCapRef){ r.cap_id, r.generation };
86}
87
88static uint64_t fld(Env *e, RxObjRef r, uint32_t i) {
89 RxObject o;
90 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX;
91 return o.field[i];
92}
93
94static int publish(Env *e, RxCapRef cap, RxObjRef o, const uint32_t *fields,
95 const uint64_t *vals, uint32_t n) {
96 RxMutation m[RX_MAX_FIELDS];
97 for (uint32_t i = 0; i < n; i++) m[i] = (RxMutation){ o, fields[i], vals[i] };
98 int64_t id = rx_world_publish_external(&e->w, cap, m, n);
99 return id > 0 ? 0 : -1;
100}
101
102static void sleep_us(int us) {
103 struct timespec ts = { 0, (long)us * 1000L };
104 nanosleep(&ts, NULL);
105}
106
107/* ---- workload: one reaction per work object, woken by the trigger ---- */
108typedef struct { Env *e; uint32_t i; } Work;
109static Work g_work[N_WORK];
110
111static int fn_work(RxCtx *c) {
112 Work *wk = c->user;
113 uint64_t seq = 0, done = 0;
114 for (uint32_t i = 0; i < c->n_in; i++) {
115 if (c->in[i].obj.id == wk->e->trigger.id) seq = c->in[i].field[0];
116 else if (c->in[i].obj.id == wk->e->work[wk->i].id) done = c->in[i].field[1];
117 }
118 c->out[c->n_out++] = (RxMutation){ wk->e->work[wk->i], 0, seq };
119 c->out[c->n_out++] = (RxMutation){ wk->e->work[wk->i], 1, done + 1 };
120 return 0;
121}
122
123static int register_work(Env *e) {
124 for (uint32_t i = 0; i < N_WORK; i++) {
125 g_work[i] = (Work){ e, i };
126 RxReactionDesc d;
127 memset(&d, 0, sizeof d);
128 d.name = i < N_FG ? "work.fg" : "work.bg";
129 d.faculty = RX_FACULTY_OMEGA;
130 d.subject = SUBJ_WORK;
131 d.caller = e->cr_work;
132 d.priority = i < N_FG ? RX_PRIO_FOREGROUND : RX_PRIO_BACKGROUND;
133 d.need.memory_bytes = MIB;
134 d.fn = fn_work;
135 d.user = &g_work[i];
136 d.n_triggers = 1;
137 d.triggers[0] = (RxDep){ e->trigger, RX_FIELD(0) };
138 d.n_reads = 2;
139 d.reads[0] = (RxDep){ e->trigger, RX_FIELD(0) };
140 d.reads[1] = (RxDep){ e->work[i], RX_ALL_FIELDS };
141 d.n_writes = 1;
142 d.writes[0] = (RxDep){ e->work[i], RX_ALL_FIELDS };
143 d.n_caps = 2;
144 d.caps[0] = (RxCapNeed){ e->work_trigger_read, RES_TRIGGER, RX_RIGHT_READ };
145 d.caps[1] = (RxCapNeed){ e->work_own[i], RES_WORK_BASE + i, RX_RIGHT_READ | RX_RIGHT_WRITE };
146 uint32_t rid;
147 if (rx_world_add_reaction(&e->w, &d, &rid) != RX_OK) return -1;
148 }
149 return 0;
150}
151
152static int env_start(Env *e) {
153 memset(e, 0, sizeof(*e));
154 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1;
155 if (rx_world_init_native(&e->w, e->view, 4, 1u << 20) != RX_OK) return -1;
156 e->w.external_subject = SUBJ_EXTERNAL;
157 RxStabilityBudget st = { 0, 0, 3, 0, 0 }; /* the World's own A-B-A guard */
158 rx_world_set_stability(&e->w, &st);
159
160 uint64_t z[RX_MAX_FIELDS] = { 0 };
161 if (rx_world_create(&e->w, OT_TRIGGER, RX_PERSIST_RESIDENT, RES_TRIGGER, z, &e->trigger) != RX_OK)
162 return -1;
163 for (uint32_t i = 0; i < N_WORK; i++)
164 if (rx_world_create(&e->w, OT_WORK, RX_PERSIST_RESIDENT, RES_WORK_BASE + i, z,
165 &e->work[i]) != RX_OK) return -1;
166
167 RxRegulateEnvelope *v = &e->env;
168 memset(v, 0, sizeof(*v));
169 v->slots_min = 1; v->slots_max = 8;
170 v->memory_min = 2 * MIB; v->memory_max = 16 * MIB;
171 v->energy_min = 1; v->energy_max = UINT64_MAX;
172 v->base.slots = 2; v->base.memory_bytes = 4 * MIB; v->base.energy_budget = UINT64_MAX;
173 v->base.starvation_bound = BASE_SB;
174 v->base.offered_locality = UINT32_MAX; v->base.offered_accel = UINT32_MAX;
175 v->base.compute_mask = UINT32_MAX;
176 v->contention_hi = 6; v->contention_lo = 2;
177 v->pressure_hi = 90; v->pressure_lo = 50;
178 v->min_dwell = 3;
179 if (rx_regulate_create_objects(&e->reg, &e->w, v) != RX_OK) return -1;
180
181 if (rx_world_enroll_caller(&e->w, SUBJ_OPERATOR, &e->cr_op) != RX_CALLER_OK ||
182 rx_world_enroll_caller(&e->w, RX_REG_SUBJ, &e->cr_reg) != RX_CALLER_OK ||
183 rx_world_enroll_caller(&e->w, SUBJ_WORK, &e->cr_work) != RX_CALLER_OK) return -1;
184 e->c_op = mint(e, SUBJ_OPERATOR, RX_WORLD_RES_CONTROL, RX_WORLD_RIGHT_HALT);
185 e->ext_condition = mint(e, SUBJ_EXTERNAL, RX_REG_RES_BASE + RX_REG_RES_CONDITION, RX_RIGHT_WRITE);
186 e->ext_trigger = mint(e, SUBJ_EXTERNAL, RES_TRIGGER, RX_RIGHT_WRITE);
187 e->work_trigger_read = mint(e, SUBJ_WORK, RES_TRIGGER, RX_RIGHT_READ);
188 for (uint32_t i = 0; i < N_WORK; i++)
189 e->work_own[i] = mint(e, SUBJ_WORK, RES_WORK_BASE + i, RX_RIGHT_READ | RX_RIGHT_WRITE);
190 RxRegulateCaps rc = {
191 mint(e, RX_REG_SUBJ, RX_REG_RES_BASE + RX_REG_RES_CONDITION, RX_RIGHT_READ),
192 mint(e, RX_REG_SUBJ, RX_REG_RES_BASE + RX_REG_RES_POLICY, RX_RIGHT_READ | RX_RIGHT_WRITE),
193 };
194 if (rx_regulate_register(&e->reg, &rc, &e->cr_reg) != RX_OK) return -1;
195 if (register_work(e) != 0) return -1;
196 return rx_world_bind_callers(&e->w) == RX_OK ? 0 : -1;
197}
198
199static void env_stop(Env *e) {
200 rx_world_wait_quiescent(&e->w, 20000);
201 rx_world_destroy(&e->w);
202 aienos_cap_stop(e->admin, e->view);
203}
204
205/* One burst: wake the whole workload, wait for every reaction to finish,
206 * return how many waited for a slot at once. Negative when the publication
207 * was refused. The gauge is the World's own; the rig resets it per burst. */
208static int64_t burst(Env *e) {
209 e->w.peak_blocked = 0;
210 uint64_t seq = ++e->trigger_seq;
211 uint32_t f = 0;
212 if (publish(e, e->ext_trigger, e->trigger, &f, &seq, 1) != 0) { e->trigger_seq--; return -1; }
213 for (int spin = 0; spin < 5000; spin++) {
214 uint32_t done = 0;
215 for (uint32_t i = 0; i < N_WORK; i++) done += fld(e, e->work[i], 0) == seq;
216 if (done == N_WORK) break;
217 sleep_us(1000);
218 }
219 rx_world_wait_quiescent(&e->w, 20000);
220 uint32_t done = 0, bg = 0;
221 for (uint32_t i = 0; i < N_WORK; i++) {
222 done += fld(e, e->work[i], 0) == seq;
223 if (i >= N_FG) bg += fld(e, e->work[i], 0) == seq;
224 }
225 CHECK(done == N_WORK, "burst %llu: %u of %u reactions finished",
226 (unsigned long long)seq, done, N_WORK);
227 e->bg_done += bg;
228 return (int64_t)e->w.peak_blocked;
229}
230
231/* Publish one condition sample and let the regulator settle on it. */
232static int sample(Env *e, uint64_t contention, uint64_t pressure) {
233 uint64_t seq = e->cond_seq + 1;
234 uint32_t f[4] = { 0, 1, 3, 5 };
235 uint64_t v[4] = { seq, contention, pressure, e->trigger_seq };
236 if (publish(e, e->ext_condition, e->reg.condition, f, v, 4) != 0) return -1;
237 e->cond_seq = seq;
238 rx_world_wait_quiescent(&e->w, 20000);
239 CHECK(fld(e, e->reg.policy, 5) == seq, "regulator did not decide on sample %llu",
240 (unsigned long long)seq);
241 return 0;
242}
243
244static uint64_t regime(Env *e) { return fld(e, e->reg.policy, 4); }
245static uint64_t flips(Env *e) { return fld(e, e->reg.policy, 7); }
246
247/* Every applied budget keeps what the regulator does not own. */
248static void check_untouched(Env *e, const char *when) {
249 const RxResourceBudget *a = &e->reg.applied, *b = &e->env.base;
250 CHECK(a->starvation_bound == BASE_SB && a->offered_locality == b->offered_locality &&
251 a->offered_accel == b->offered_accel && a->compute_mask == b->compute_mask &&
252 a->logical_tick == b->logical_tick, "%s: a field outside the policy changed", when);
253 CHECK(e->w.budget.starvation_bound == BASE_SB, "%s: world fairness bound is %u",
254 when, e->w.budget.starvation_bound);
255}
256
257static void row(const char *phase, Env *e, int64_t deferred) {
258 printf("M4 %-10s regime %llu slots %u memory %llu MiB blocked-wait/burst %lld flips %llu refused %llu\n",
259 phase, (unsigned long long)regime(e), e->w.budget.slots,
260 (unsigned long long)(e->w.budget.memory_bytes / MIB), (long long)deferred,
261 (unsigned long long)flips(e), (unsigned long long)e->reg.refused);
262}
263
264/* ---- pure decision checks, no world ---- */
265static void decision_checks(const RxRegulateEnvelope *v) {
266 CHECK(rx_regulate_decide(v, RX_REG_NOMINAL, 0, v->contention_hi, 0) == RX_REG_EXPAND,
267 "nominal must expand at contention_hi");
268 CHECK(rx_regulate_decide(v, RX_REG_NOMINAL, 0, v->contention_hi - 1, 0) == RX_REG_NOMINAL,
269 "nominal must hold below contention_hi");
270 CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell, v->contention_lo + 1, 0) == RX_REG_EXPAND,
271 "expand must hold above contention_lo (dead band)");
272 CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell - 1, 0, 0) == RX_REG_EXPAND,
273 "expand must hold before min_dwell");
274 CHECK(rx_regulate_decide(v, RX_REG_EXPAND, v->min_dwell, v->contention_lo, 0) == RX_REG_NOMINAL,
275 "expand must return to nominal once calm and settled");
276 CHECK(rx_regulate_decide(v, RX_REG_EXPAND, 0, UINT64_MAX, v->pressure_hi) == RX_REG_PROTECT,
277 "protect wins over expand");
278 CHECK(rx_regulate_decide(v, RX_REG_PROTECT, v->min_dwell, 0, v->pressure_lo + 1) == RX_REG_PROTECT,
279 "protect must hold above pressure_lo");
280 CHECK(rx_regulate_decide(v, RX_REG_PROTECT, v->min_dwell, v->contention_hi, v->pressure_lo) == RX_REG_EXPAND,
281 "protect leaves to expand when contention is high");
282 RxRegulateEnvelope bad = *v;
283 bad.base.slots = v->slots_max + 1;
284 CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "base outside the bounds must be refused");
285 bad = *v; bad.contention_lo = bad.contention_hi;
286 CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "empty dead band must be refused");
287 bad = *v; bad.min_dwell = 0;
288 CHECK(rx_regulate_envelope_ok(&bad) == RX_ERR_ARG, "zero dwell must be refused");
289 RxResourceBudget b;
290 rx_regulate_policy_for(v, RX_REG_EXPAND, &b);
291 CHECK(b.slots == v->slots_max && b.memory_bytes == v->memory_max && b.starvation_bound == BASE_SB,
292 "expand policy must be the envelope maximum with the base fairness bound");
293 rx_regulate_policy_for(v, RX_REG_PROTECT, &b);
294 CHECK(b.slots == v->slots_min && b.memory_bytes == v->memory_min, "protect policy must be the minimum");
295}
296
297int main(void) {
298 Env e;
299 if (env_start(&e) != 0) { fprintf(stderr, "env_start failed\n"); return 2; }
300 decision_checks(&e.env);
301
302 /* A: nominal. The first apply installs the base budget. */
303 CHECK(rx_regulate_apply(&e.reg) == RX_OK, "first apply must install the base budget");
304 CHECK(rx_regulate_apply(&e.reg) == 1, "second apply must find nothing new");
305 CHECK(e.w.budget.slots == e.env.base.slots, "base slots not in force: %u", e.w.budget.slots);
306 check_untouched(&e, "A");
307 int64_t d_nom = burst(&e);
308 int64_t d_nom2 = burst(&e);
309 CHECK(d_nom > 0 && d_nom2 > 0, "nominal bursts must make work wait (%lld, %lld)", (long long)d_nom, (long long)d_nom2);
310 CHECK(regime(&e) == RX_REG_NOMINAL && flips(&e) == 0, "A: regime %llu flips %llu",
311 (unsigned long long)regime(&e), (unsigned long long)flips(&e));
312 row("A nominal", &e, d_nom2);
313
314 /* B: pressure. Measured contention goes in, a bounded policy comes out,
315 * and the next burst is cheaper. */
316 CHECK(sample(&e, (uint64_t)d_nom2, 10) == 0, "B sample refused");
317 CHECK(regime(&e) == RX_REG_EXPAND && flips(&e) == 1, "B: regime %llu flips %llu",
318 (unsigned long long)regime(&e), (unsigned long long)flips(&e));
319 CHECK(rx_regulate_apply(&e.reg) == RX_OK, "B: apply must install the expand policy");
320 CHECK(e.w.budget.slots == e.env.slots_max && e.w.budget.memory_bytes == e.env.memory_max,
321 "B: expand budget not in force (slots %u)", e.w.budget.slots);
322 check_untouched(&e, "B");
323 int64_t d_exp = burst(&e);
324 CHECK(d_exp >= 0 && d_exp < d_nom2, "B: expand must improve the objective: %lld vs nominal %lld",
325 (long long)d_exp, (long long)d_nom2);
326 row("B expand", &e, d_exp);
327
328 /* C: calm. Below the dead band for min_dwell samples returns to nominal. */
329 for (uint64_t i = 0; i < e.env.min_dwell; i++) {
330 CHECK(sample(&e, (uint64_t)d_exp < e.env.contention_lo ? (uint64_t)d_exp : 0, 10) == 0, "C sample refused");
331 CHECK(regime(&e) == RX_REG_EXPAND, "C: left expand before min_dwell at sample %llu", (unsigned long long)i);
332 }
333 CHECK(sample(&e, 0, 10) == 0, "C sample refused");
334 CHECK(regime(&e) == RX_REG_NOMINAL && flips(&e) == 2, "C: regime %llu flips %llu",
335 (unsigned long long)regime(&e), (unsigned long long)flips(&e));
336 CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots,
337 "C: base budget not restored (slots %u)", e.w.budget.slots);
338 check_untouched(&e, "C");
339 int64_t d_rec = burst(&e);
340 CHECK(d_rec > d_exp, "C: recovery must make work wait again: %lld vs %lld",
341 (long long)d_rec, (long long)d_exp);
342 row("C recover", &e, d_rec);
343
344 /* D: hysteresis. Chatter across both thresholds flips at most once per
345 * min_dwell + 1 samples; chatter inside the dead band never flips. The
346 * World's own oscillation guard (limit 3) must not trip either. */
347 uint64_t f0 = flips(&e);
348 const int N_CHATTER = 24;
349 for (int i = 0; i < N_CHATTER; i++)
350 CHECK(sample(&e, i % 2 ? 0 : e.env.contention_hi, 10) == 0, "D sample refused");
351 uint64_t chatter_flips = flips(&e) - f0;
352 uint64_t bound = (uint64_t)N_CHATTER / (e.env.min_dwell + 1) + 2;
353 CHECK(chatter_flips <= bound, "D: chatter caused %llu flips, bound %llu",
354 (unsigned long long)chatter_flips, (unsigned long long)bound);
355 CHECK(chatter_flips >= 2, "D: chatter must still be followed (flips %llu)", (unsigned long long)chatter_flips);
356 /* settle to nominal, then drive the dead band */
357 for (uint64_t i = 0; i <= e.env.min_dwell; i++) CHECK(sample(&e, 0, 10) == 0, "D settle refused");
358 CHECK(regime(&e) == RX_REG_NOMINAL, "D: not nominal after settling");
359 f0 = flips(&e);
360 for (int i = 0; i < 10; i++)
361 CHECK(sample(&e, i % 2 ? e.env.contention_lo + 1 : e.env.contention_hi - 1, 10) == 0, "D band refused");
362 CHECK(flips(&e) == f0, "D: dead-band chatter flipped the policy %llu times",
363 (unsigned long long)(flips(&e) - f0));
364 CHECK(e.w.stats.oscillation_trips == 0 && e.w.stats.quarantines == 0 &&
365 !e.w.reactions[e.reg.rid].quarantined,
366 "D: World guard tripped (osc %llu quarantines %llu)",
367 (unsigned long long)e.w.stats.oscillation_trips, (unsigned long long)e.w.stats.quarantines);
368 CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots,
369 "D: apply after chatter must land on the base budget");
370 row("D chatter", &e, -1);
371
372 /* E: envelope. A huge signal gives exactly the maximum; a forged policy
373 * (the test, as issuer, mints a write it should never have) is refused
374 * at apply and the budget in force stays; PROTECT gives the minimum. */
375 CHECK(sample(&e, UINT64_MAX, 10) == 0, "E sample refused");
376 CHECK(regime(&e) == RX_REG_EXPAND && fld(&e, e.reg.policy, 1) == e.env.slots_max &&
377 fld(&e, e.reg.policy, 2) == e.env.memory_max,
378 "E: policy escaped the maximum (slots %llu)", (unsigned long long)fld(&e, e.reg.policy, 1));
379 CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.slots_max, "E: expand apply");
380 CHECK(publish(&e, e.ext_condition, e.reg.policy, (uint32_t[]){ 1 }, (uint64_t[]){ 1000 }, 1) != 0,
381 "E: the condition writer must not be able to write the policy");
382 RxCapRef forged = mint(&e, SUBJ_EXTERNAL, RX_REG_RES_BASE + RX_REG_RES_POLICY, RX_RIGHT_WRITE);
383 uint64_t ver = fld(&e, e.reg.policy, 0);
384 uint32_t ff[2] = { 0, 1 };
385 uint64_t fv[2] = { ver + 100, (uint64_t)e.env.slots_max * 4 };
386 CHECK(publish(&e, forged, e.reg.policy, ff, fv, 2) == 0, "E: forged write did not land (test rig)");
387 rx_world_wait_quiescent(&e.w, 20000);
388 uint64_t refused0 = e.reg.refused;
389 CHECK(rx_regulate_apply(&e.reg) == RX_ERR_BOUNDS && e.reg.refused == refused0 + 1,
390 "E: forged policy must be refused at apply");
391 CHECK(e.w.budget.slots == e.env.slots_max, "E: forged policy changed the budget (slots %u)", e.w.budget.slots);
392 check_untouched(&e, "E");
393 CHECK(sample(&e, 0, e.env.pressure_hi) == 0, "E protect sample refused");
394 CHECK(regime(&e) == RX_REG_PROTECT, "E: pressure did not protect");
395 CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.slots_min &&
396 e.w.budget.memory_bytes == e.env.memory_min, "E: protect budget not in force (slots %u)", e.w.budget.slots);
397 check_untouched(&e, "E protect");
398 int64_t d_prot = burst(&e);
399 CHECK(d_prot >= d_nom2, "E: protect must not wait less than nominal: %lld vs %lld",
400 (long long)d_prot, (long long)d_nom2);
401 row("E protect", &e, d_prot);
402
403 /* F: operator stop in the middle of regulation. The World refuses the
404 * condition and the burst, apply refuses, nothing moves; resume restores. */
405 RxHaltStatus h;
406 CHECK(rx_world_emergency_stop(&e.w, SUBJ_OPERATOR, &e.cr_op, e.c_op, 4, &h) == RX_OK && h.halted,
407 "F: operator stop refused");
408 uint32_t slots_at_stop = e.w.budget.slots;
409 CHECK(sample(&e, UINT64_MAX, 10) != 0, "F: condition publication must be refused while stopped");
410 CHECK(burst(&e) < 0, "F: burst must be refused while stopped");
411 CHECK(rx_regulate_apply(&e.reg) == RX_ERR_HALTED, "F: apply must refuse while stopped");
412 CHECK(e.w.budget.slots == slots_at_stop, "F: budget moved during the stop");
413 CHECK(rx_world_emergency_resume(&e.w, SUBJ_OPERATOR, &e.cr_op, e.c_op, &h) == RX_OK && !h.halted,
414 "F: resume refused");
415 for (uint64_t i = 0; i <= e.env.min_dwell; i++) CHECK(sample(&e, 0, 10) == 0, "F sample refused");
416 CHECK(regime(&e) == RX_REG_NOMINAL, "F: not nominal after resume");
417 CHECK(rx_regulate_apply(&e.reg) == RX_OK && e.w.budget.slots == e.env.base.slots, "F: apply after resume");
418 int64_t d_after = burst(&e);
419 CHECK(d_after > 0, "F: burst after resume");
420 row("F resumed", &e, d_after);
421
422 /* G: fairness. The BACKGROUND reactions finished every burst that ran. */
423 uint64_t bursts = e.trigger_seq;
424 CHECK(e.bg_done == bursts * N_BG, "G: background reactions finished %llu of %llu",
425 (unsigned long long)e.bg_done, (unsigned long long)(bursts * N_BG));
426 check_untouched(&e, "G");
427 CHECK(e.w.stats.quarantines == 0 && e.w.stats.oscillation_trips == 0, "G: World guard tripped");
428 row("G fairness", &e, -1);
429
430 env_stop(&e);
431 printf("RX_REGULATE %s: pass %d fail %d\n", g_fail ? "FAIL" : "PASS", g_checks - g_fail, g_fail);
432 return g_fail ? 1 : 0;
433}