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 / model / world_model.c

316 lines · 10919 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 * world_model.c -- the lifecycle model (see world_model.h).
19 *
20 * Every AS_BUILT rule cites the rx_world.c function it mirrors, so a change
21 * there that the model does not follow shows up as a differential mismatch.
22 */
23#include "world_model.h"
24
25#include <stdio.h>
26#include <string.h>
27
28const int wm_parent[WM_N] = { -1, WM_P, WM_P, WM_C1 };
29const uint32_t wm_mem[WM_N] = { 4, 4, 2, 2 };
30const uint32_t wm_deadline[WM_N] = { 1, 2, 0, 1 };
31const char *wm_name[WM_N] = { "P", "C1", "C2", "G" };
32
33const WmProfile wm_spec = { "SPEC", CANCEL_TRANSITIVE, 1, 0, 0 };
34const WmProfile wm_as_built = { "AS_BUILT", CANCEL_NONE, 0, 0, 0 };
35const WmProfile wm_mut_leak = { "MUT_LEAK_ON_FAIL", CANCEL_TRANSITIVE, 1, 1, 0 };
36const WmProfile wm_mut_noprop = { "MUT_CANCEL_NO_PROPAGATE", CANCEL_TARGET_ONLY, 1, 0, 0 };
37const WmProfile wm_mut_double = { "MUT_COMMIT_KEEPS_ACTIVATION", CANCEL_TRANSITIVE, 1, 0, 1 };
38const WmProfile wm_mut_deadline = { "MUT_DEADLINE_ADMIT_ONLY", CANCEL_TRANSITIVE, 0, 0, 0 };
39
40int wm_is_descendant(uint32_t d, uint32_t r) {
41 for (int a = wm_parent[d]; a >= 0; a = wm_parent[a])
42 if ((uint32_t)a == r) return 1;
43 return 0;
44}
45
46void wm_init(WmState *s) { memset(s, 0, sizeof *s); }
47
48/* res_fits */
49static int fits(const WmState *s, uint32_t r) {
50 if (s->used_slots >= WM_SLOTS) return 0;
51 if (s->used_mem > WM_MEM) return 0;
52 if (wm_mem[r] > WM_MEM - s->used_mem) return 0;
53 return 1;
54}
55
56/* charge + enqueue; the harness waits until the worker is inside fn, so
57 * admitted == running here. */
58static void admit(WmState *s, uint32_t r) {
59 s->st[r] = WM_RUN;
60 s->used_slots++;
61 s->used_mem += wm_mem[r];
62 s->in_flight++;
63 s->stale[r] = 0;
64 s->act_committed[r] = 0;
65 s->surfaced[r] = 0;
66 if (wm_deadline[r] && s->tick > wm_deadline[r]) { /* charge(): counted */
67 s->overdue++;
68 s->surfaced[r] = 1;
69 }
70}
71
72/* try_admit / pick_admit / best_waiting: one priority class, oldest
73 * fitting waiter first, until nothing fits. */
74static void try_admit(WmState *s) {
75 for (;;) {
76 int best = -1;
77 for (uint32_t r = 0; r < WM_N; r++) {
78 if (s->st[r] != WM_WAITING || !fits(s, r)) continue;
79 if (best < 0 || s->wait_seq[r] < s->wait_seq[best]) best = (int)r;
80 }
81 if (best < 0) return;
82 admit(s, (uint32_t)best);
83 }
84}
85
86/* demand_inner (stability limits off, no fan-out limit). */
87static void demand(WmState *s, uint32_t r, int external) {
88 if (external) s->yield[r] = 0;
89 s->parked[r] = 0;
90 switch (s->st[r]) {
91 case WM_IDLE:
92 s->st[r] = WM_WAITING;
93 s->wait_seq[r] = ++s->seq;
94 try_admit(s);
95 break;
96 case WM_WAITING:
97 break; /* coalesced */
98 default:
99 s->rearm[r] = 1; /* woken while running */
100 break;
101 }
102}
103
104/* others_busy */
105static int others_busy(const WmState *s, uint32_t self) {
106 for (uint32_t i = 0; i < WM_N; i++)
107 if (i != self && (s->st[i] == WM_WAITING || s->st[i] == WM_RUN)) return 1;
108 return 0;
109}
110
111/* release_parked */
112static void release_parked(WmState *s) {
113 for (uint32_t i = 0; i < WM_N; i++) {
114 if (!s->parked[i]) continue;
115 if (s->yield[i] && others_busy(s, i)) continue;
116 s->parked[i] = 0;
117 s->yield[i] = 0;
118 demand(s, i, i == WM_P);
119 }
120}
121
122/* arm_backoff */
123static uint8_t backoff(uint8_t y) {
124 uint32_t n = y ? y * 2u : 1u;
125 if (n > 8u) n = 8u;
126 return (uint8_t)n;
127}
128
129static void uncharge(WmState *s, uint32_t r) {
130 if (s->used_slots) s->used_slots--;
131 s->used_mem = s->used_mem >= wm_mem[r] ? s->used_mem - wm_mem[r] : 0;
132}
133
134/* end_activation_inner */
135static void end_activation(const WmProfile *p, WmState *s, uint32_t r, int failed) {
136 for (uint32_t i = 0; i < WM_N; i++) /* tick_parked */
137 if (s->parked[i] && s->yield[i]) s->yield[i]--;
138 release_parked(s);
139 if (!(failed && p->mut_leak_on_fail)) uncharge(s, r);
140 s->st[r] = WM_IDLE;
141 if (s->in_flight) s->in_flight--;
142 if (s->rearm[r] && s->yield[r] && others_busy(s, r)) {
143 s->yield[r]--;
144 s->parked[r] = 1;
145 s->rearm[r] = 0;
146 try_admit(s);
147 } else if (s->rearm[r]) {
148 s->rearm[r] = 0;
149 demand(s, r, r == WM_P);
150 } else {
151 try_admit(s);
152 }
153 if (s->in_flight == 0) release_parked(s);
154}
155
156/* A write to the object r's subscribers trigger on (finish_writes ->
157 * propagate): running subscribers now hold a stale snapshot. */
158static void write_and_wake(WmState *s, int writer) {
159 uint32_t kids[WM_N], nk = 0;
160 for (uint32_t c = 0; c < WM_N; c++)
161 if (wm_parent[c] == writer) kids[nk++] = c;
162 for (uint32_t i = 0; i < nk; i++)
163 if (s->st[kids[i]] == WM_RUN) s->stale[kids[i]] = 1;
164 for (uint32_t i = 0; i < nk; i++) demand(s, kids[i], writer < 0);
165}
166
167static void cancel(const WmProfile *p, WmState *s, uint32_t r) {
168 if (p->cancel_mode == CANCEL_NONE) return; /* no entry point exists */
169 for (uint32_t x = 0; x < WM_N; x++) {
170 if (x != r && !(p->cancel_mode == CANCEL_TRANSITIVE && wm_is_descendant(x, r)))
171 continue;
172 if (s->st[x] == WM_RUN) {
173 uncharge(s, x);
174 if (s->in_flight) s->in_flight--;
175 }
176 s->st[x] = WM_IDLE;
177 s->rearm[x] = s->parked[x] = s->yield[x] = s->stale[x] = 0;
178 s->cancels++;
179 }
180 try_admit(s);
181 if (s->in_flight == 0) release_parked(s);
182}
183
184int wm_enabled(const WmProfile *p, const WmState *s, WmOp op) {
185 (void)p;
186 switch (op.kind) {
187 case OP_EXT: return s->ext < WM_EXT_MAX;
188 case OP_FIN:
189 case OP_FAIL: return op.r < WM_N && s->st[op.r] == WM_RUN;
190 case OP_TICK: return s->tick < WM_TICK_MAX;
191 case OP_CANCEL: return op.r == WM_P || op.r == WM_C1;
192 default: return 0;
193 }
194}
195
196static uint32_t check(const WmState *s) {
197 uint32_t v = 0, n_run = 0, mem = 0;
198 for (uint32_t r = 0; r < WM_N; r++)
199 if (s->st[r] == WM_RUN) { n_run++; mem += wm_mem[r]; }
200 if (s->used_slots != n_run || s->used_mem != mem ||
201 s->used_slots > WM_SLOTS || s->used_mem > WM_MEM)
202 v |= V_LEAK;
203 for (uint32_t r = 0; r < WM_N; r++)
204 if (s->st[r] == WM_RUN && wm_deadline[r] && s->tick > wm_deadline[r] && !s->surfaced[r])
205 v |= V_DEADLINE;
206 return v;
207}
208
209uint32_t wm_step(const WmProfile *p, WmState *s, WmOp op) {
210 s->viol = 0;
211 uint32_t r = op.r;
212 switch (op.kind) {
213 case OP_EXT:
214 s->ext++;
215 write_and_wake(s, -1);
216 break;
217 case OP_TICK:
218 s->tick++;
219 if (p->deadline_on_tick)
220 for (uint32_t i = 0; i < WM_N; i++)
221 if (s->st[i] == WM_RUN && wm_deadline[i] && s->tick > wm_deadline[i] &&
222 !s->surfaced[i]) {
223 s->surfaced[i] = 1;
224 s->overdue++;
225 }
226 try_admit(s); /* rx_world_set_resources */
227 break;
228 case OP_FAIL: /* fn < 0: FAILED, no back-off */
229 s->failures++;
230 s->st[r] = WM_ENDING;
231 end_activation(p, s, r, 1);
232 break;
233 case OP_FIN:
234 if (wm_deadline[r] && s->tick > wm_deadline[r]) {
235 /* run_one (HD-09 first enforcement cut): fn returned past its
236 * declared deadline -> RUNNING -> CANCELLED, nothing staged or
237 * published, no wake, charge refunded by end_activation. This
238 * is enforcement at return only; I4 (surfaced while running)
239 * is still not met by the as-built World. */
240 s->st[r] = WM_ENDING;
241 end_activation(p, s, r, 0);
242 break;
243 }
244 if (s->stale[r]) { /* PUBLISHING -> INVALIDATED */
245 s->invalidations++;
246 s->yield[r] = backoff(s->yield[r]);
247 s->rearm[r] = 1;
248 s->st[r] = WM_ENDING;
249 end_activation(p, s, r, 0);
250 break;
251 }
252 if (s->act_committed[r]) s->viol |= V_DOUBLE;
253 s->act_committed[r] = 1;
254 s->commits[r]++;
255 s->yield[r] = 0; /* note_value: progress, no flip */
256 s->st[r] = WM_ENDING;
257 write_and_wake(s, (int)r);
258 if (p->mut_commit_keeps_act && !s->rearm[r]) {
259 s->st[r] = WM_RUN; /* activation never retired */
260 break;
261 }
262 end_activation(p, s, r, 0);
263 break;
264 case OP_CANCEL:
265 cancel(p, s, r);
266 if (s->st[r] != WM_IDLE || s->rearm[r] || s->parked[r]) s->viol |= V_CANCEL_SELF;
267 for (uint32_t x = 0; x < WM_N; x++)
268 if (wm_is_descendant(x, r) &&
269 (s->st[x] != WM_IDLE || s->rearm[x] || s->parked[x]))
270 s->viol |= V_CANCEL_CHILD;
271 break;
272 }
273 s->viol |= check(s);
274 return s->viol;
275}
276
277int wm_key(const WmState *s, uint8_t *out) {
278 int n = 0;
279 for (uint32_t r = 0; r < WM_N; r++) {
280 uint32_t rank = 0;
281 if (s->st[r] == WM_WAITING)
282 for (uint32_t q = 0; q < WM_N; q++)
283 if (s->st[q] == WM_WAITING && s->wait_seq[q] <= s->wait_seq[r]) rank++;
284 out[n++] = s->st[r];
285 out[n++] = s->rearm[r];
286 out[n++] = s->stale[r];
287 out[n++] = s->parked[r];
288 out[n++] = s->yield[r];
289 out[n++] = s->surfaced[r];
290 out[n++] = s->act_committed[r];
291 out[n++] = (uint8_t)rank;
292 }
293 out[n++] = s->ext;
294 out[n++] = s->tick;
295 out[n++] = s->used_slots;
296 out[n++] = (uint8_t)s->used_mem;
297 out[n++] = s->in_flight;
298 return n;
299}
300
301void wm_op_str(WmOp op, char *buf, int n) {
302 static const char *k[] = { "EXT", "FIN", "FAIL", "TICK", "CANCEL" };
303 if (op.kind == OP_EXT || op.kind == OP_TICK) snprintf(buf, (size_t)n, "%s", k[op.kind]);
304 else snprintf(buf, (size_t)n, "%s(%s)", k[op.kind], wm_name[op.r]);
305}
306
307int wm_all_ops(WmOp *out) {
308 int n = 0;
309 out[n++] = (WmOp){ OP_EXT, 0 };
310 out[n++] = (WmOp){ OP_TICK, 0 };
311 for (uint8_t r = 0; r < WM_N; r++) out[n++] = (WmOp){ OP_FIN, r };
312 for (uint8_t r = 0; r < WM_N; r++) out[n++] = (WmOp){ OP_FAIL, r };
313 out[n++] = (WmOp){ OP_CANCEL, WM_P };
314 out[n++] = (WmOp){ OP_CANCEL, WM_C1 };
315 return n;
316}