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 / src / runtime / rx_plan.c

581 lines · 21179 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_plan.c -- Omega plan IR and verified plan cache. See rx_plan.h and
19 * spec/plan-reuse.md.
20 */
21#include "rx_plan.h"
22
23#include "omega_native.h"
24#include "sha256.h"
25
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29
30static uint64_t now_ns(void) {
31 struct timespec ts;
32 clock_gettime(CLOCK_MONOTONIC, &ts);
33 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
34}
35
36uint64_t pl_word(const uint8_t id[32], uint32_t k) {
37 uint64_t res = 0;
38 (void)omega2_rx_pl_word(&res, id, 32, (uint64_t)k);
39 return res;
40}
41
42/* ---- canonical bytes ---- */
43
44typedef struct { uint8_t *b; size_t n, cap; int over; } Buf;
45
46static void put(Buf *o, const void *p, size_t n) {
47 if (o->n + n > o->cap) { o->over = 1; return; }
48 memcpy(o->b + o->n, p, n);
49 o->n += n;
50}
51static void p32(Buf *o, uint32_t v) {
52 uint8_t x[4] = { (uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24) };
53 put(o, x, 4);
54}
55static void p64(Buf *o, uint64_t v) { p32(o, (uint32_t)v); p32(o, (uint32_t)(v >> 32)); }
56
57static void sort_list(PlPredList *l) {
58 if (l->n <= 1) return;
59 uint32_t n = l->n <= PL_MAX_PREDS ? l->n : PL_MAX_PREDS;
60 uint64_t buf[PL_MAX_PREDS * 6];
61 memset(buf, 0, sizeof buf);
62 for (uint32_t i = 0; i < n; i++) {
63 buf[i * 6 + 0] = l->p[i].kind;
64 buf[i * 6 + 1] = l->p[i].slot;
65 buf[i * 6 + 2] = l->p[i].field;
66 buf[i * 6 + 3] = l->p[i].slot2;
67 buf[i * 6 + 4] = l->p[i].type;
68 buf[i * 6 + 5] = l->p[i].value;
69 }
70 uint64_t res = 0;
71 if (omega2_rx_pl_sort_preds(&res, buf, PL_MAX_PREDS * 6, (uint64_t)n) == OMEGA_OK) {
72 for (uint32_t i = 0; i < n; i++) {
73 l->p[i].kind = (uint32_t)buf[i * 6 + 0];
74 l->p[i].slot = (uint32_t)buf[i * 6 + 1];
75 l->p[i].field = (uint32_t)buf[i * 6 + 2];
76 l->p[i].slot2 = (uint32_t)buf[i * 6 + 3];
77 l->p[i].type = (uint32_t)buf[i * 6 + 4];
78 l->p[i].value = buf[i * 6 + 5];
79 }
80 }
81}
82
83void rx_plan_sort(PlanTemplate *t) {
84 sort_list(&t->pre);
85 sort_list(&t->state);
86 sort_list(&t->env);
87 sort_list(&t->invariants);
88 sort_list(&t->success);
89}
90
91static void put_list(Buf *o, const PlPredList *l) {
92 p32(o, l->n);
93 for (uint32_t i = 0; i < l->n; i++) {
94 const PlPred *p = &l->p[i];
95 p32(o, p->kind); p32(o, p->slot); p32(o, p->field); p32(o, p->slot2); p32(o, p->type);
96 p64(o, p->value);
97 }
98}
99
100#define PL_MAGIC 0x4C504D4Fu /* "OMPL" */
101
102size_t rx_plan_encode(const PlanTemplate *t, uint8_t *buf, size_t cap) {
103 Buf o = { buf, 0, cap, 0 };
104 p32(&o, PL_MAGIC);
105 p32(&o, 1);
106 p32(&o, t->goal_kind);
107 put(&o, t->goal_shape, 32);
108 p32(&o, t->n_goal_slots);
109 p32(&o, t->n_slots);
110 for (uint32_t i = 0; i < t->n_slots; i++) {
111 const PlSlot *s = &t->slots[i];
112 p32(&o, s->type); p32(&o, s->role); p32(&o, s->rights);
113 p32(&o, s->bind_rule); p32(&o, s->bind_from); p32(&o, s->bind_field);
114 }
115 put_list(&o, &t->pre);
116 put_list(&o, &t->state);
117 put_list(&o, &t->env);
118 put_list(&o, &t->invariants);
119 put_list(&o, &t->success);
120 p32(&o, t->fail_flags);
121 const RxResourceNeed *r = &t->step_need;
122 p32(&o, r->compute_class); p32(&o, r->locality); p32(&o, r->accelerator_features);
123 p32(&o, r->latency_class); p64(&o, r->memory_bytes); p64(&o, r->deadline);
124 p64(&o, r->energy_cost);
125 p32(&o, t->n_steps);
126 p64(&o, t->energy_total);
127 p32(&o, t->n_evidence);
128 p32(&o, t->graph_kind);
129 put(&o, t->graph_digest, 32);
130 return o.over ? 0 : o.n;
131}
132
133typedef struct { const uint8_t *b; size_t n, at; int bad; } In;
134
135static void get(In *i, void *p, size_t n) {
136 if (i->at + n > i->n) { i->bad = 1; memset(p, 0, n); return; }
137 memcpy(p, i->b + i->at, n);
138 i->at += n;
139}
140static uint32_t g32(In *i) {
141 uint8_t x[4];
142 get(i, x, 4);
143 return (uint32_t)x[0] | (uint32_t)x[1] << 8 | (uint32_t)x[2] << 16 | (uint32_t)x[3] << 24;
144}
145static uint64_t g64(In *i) { uint64_t lo = g32(i); return lo | (uint64_t)g32(i) << 32; }
146
147static void get_list(In *i, PlPredList *l) {
148 l->n = g32(i);
149 if (l->n > PL_MAX_PREDS) { i->bad = 1; l->n = 0; return; }
150 for (uint32_t k = 0; k < l->n; k++) {
151 PlPred *p = &l->p[k];
152 p->kind = g32(i); p->slot = g32(i); p->field = g32(i); p->slot2 = g32(i);
153 p->type = g32(i); p->value = g64(i);
154 }
155}
156
157int rx_plan_decode(const uint8_t *buf, size_t n, PlanTemplate *t) {
158 In i = { buf, n, 0, 0 };
159 memset(t, 0, sizeof *t);
160 if (g32(&i) != PL_MAGIC || g32(&i) != 1) return -1;
161 t->goal_kind = g32(&i);
162 get(&i, t->goal_shape, 32);
163 t->n_goal_slots = g32(&i);
164 t->n_slots = g32(&i);
165 if (t->n_slots > PL_MAX_SLOTS || t->n_goal_slots > t->n_slots) return -1;
166 for (uint32_t k = 0; k < t->n_slots; k++) {
167 PlSlot *s = &t->slots[k];
168 s->type = g32(&i); s->role = g32(&i); s->rights = g32(&i);
169 s->bind_rule = g32(&i); s->bind_from = g32(&i); s->bind_field = g32(&i);
170 }
171 get_list(&i, &t->pre);
172 get_list(&i, &t->state);
173 get_list(&i, &t->env);
174 get_list(&i, &t->invariants);
175 get_list(&i, &t->success);
176 t->fail_flags = g32(&i);
177 RxResourceNeed *r = &t->step_need;
178 r->compute_class = g32(&i); r->locality = g32(&i); r->accelerator_features = g32(&i);
179 r->latency_class = g32(&i); r->memory_bytes = g64(&i); r->deadline = g64(&i);
180 r->energy_cost = g64(&i);
181 t->n_steps = g32(&i);
182 t->energy_total = g64(&i);
183 t->n_evidence = g32(&i);
184 t->graph_kind = g32(&i);
185 get(&i, t->graph_digest, 32);
186 if (i.bad || i.at != n) return -1;
187 return 0;
188}
189
190void rx_plan_identify(PlanTemplate *t) {
191 rx_plan_sort(t);
192 rx_graph_identify(&t->graph);
193 memcpy(t->graph_digest, t->graph.digest, 32);
194 static uint8_t buf[1u << 16];
195 size_t n = rx_plan_encode(t, buf, sizeof buf);
196 sha256_ctx c;
197 sha256_init(&c);
198 sha256_update(&c, (const uint8_t *)"OMEGA_PLAN_TEMPLATE_V1", 22);
199 sha256_update(&c, buf, n);
200 sha256_final(&c, t->semantic_id);
201}
202
203/* ---- views and predicates ---- */
204
205void rx_plan_view(RxWorld *w, PlView *v) {
206 pthread_mutex_lock(&w->mu);
207 v->n = RX_MAX_OBJECTS;
208 for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) {
209 const RxObject *o = &w->objects[i];
210 v->o[i].type = o->type;
211 v->o[i].generation = o->generation;
212 v->o[i].live = o->live;
213 v->o[i].resource = o->resource;
214 memcpy(v->o[i].field, o->field, sizeof o->field);
215 }
216 pthread_mutex_unlock(&w->mu);
217}
218
219static const uint64_t *fields_of(const PlView *v, RxObjRef r) {
220 if (r.id >= v->n || !v->o[r.id].live || v->o[r.id].generation != r.generation) return NULL;
221 return v->o[r.id].field;
222}
223
224static int is_slot(RxObjRef r, const RxObjRef *slots, uint32_t n) {
225 for (uint32_t s = 0; s < n; s++)
226 if (slots[s].id == r.id && slots[s].generation == r.generation) return 1;
227 return 0;
228}
229
230int rx_plan_pred(const PlPred *p, const PlView *v, const PlEnv *env, const RxObjRef *slots,
231 uint32_t n_slots) {
232 uint32_t f = p->field & (RX_MAX_FIELDS - 1);
233 if (p->kind == PL_P_ENV_EQ || p->kind == PL_P_ENV_GE) {
234 if (!env || p->slot >= PL_ENV_COUNT) return 0;
235 const uint64_t *x = fields_of(v, env->obj[p->slot]);
236 if (!x) return 0;
237 return p->kind == PL_P_ENV_EQ ? x[f] == p->value : x[f] >= p->value;
238 }
239 if (!slots || p->slot >= n_slots) return 0;
240 RxObjRef r = slots[p->slot];
241 const uint64_t *x = fields_of(v, r);
242 if (!x) return 0;
243 switch (p->kind) {
244 case PL_P_EQ_CONST:
245 return x[f] == p->value;
246 case PL_P_EQ_SLOT:
247 return p->slot2 < n_slots && x[f] == pl_ref_pack(slots[p->slot2]);
248 case PL_P_NOT_SLOT:
249 if (x[f] == 0) return 0;
250 for (uint32_t s = 0; s < n_slots; s++)
251 if (x[f] == pl_ref_pack(slots[s])) return 0;
252 return 1;
253 case PL_P_SELF_REF:
254 return x[f] == pl_ref_pack(r);
255 case PL_P_LIVE:
256 return v->o[r.id].type == p->type;
257 case PL_P_UNREFERENCED: {
258 uint64_t me = pl_ref_pack(r);
259 for (uint32_t i = 0; i < v->n; i++) {
260 if (!v->o[i].live || v->o[i].type != p->type) continue;
261 if (v->o[i].field[f] != me) continue;
262 if (!is_slot((RxObjRef){ i, v->o[i].generation }, slots, n_slots)) return 0;
263 }
264 return 1;
265 }
266 default:
267 return 0;
268 }
269}
270
271int rx_plan_preds(const PlPredList *l, const PlView *v, const PlEnv *env, const RxObjRef *slots,
272 uint32_t n_slots, PlCost *cost) {
273 for (uint32_t i = 0; i < l->n; i++) {
274 if (cost) cost->preds++;
275 if (!rx_plan_pred(&l->p[i], v, env, slots, n_slots)) return (int)i;
276 }
277 return -1;
278}
279
280static int cap_find(RxWorld *w, const AgCapTable *caps, uint64_t resource, uint32_t rights,
281 RxCapRef *out) {
282 for (uint32_t i = 0; caps && i < caps->n; i++) {
283 if (caps->cap[i].resource != resource || (caps->cap[i].rights & rights) != rights) continue;
284 if (rx_world_validate_cap(w, caps->cap[i].ref, caps->subject, resource, rights, NULL) !=
285 RX_CAP_OK)
286 continue;
287 if (out) *out = caps->cap[i].ref;
288 return 1;
289 }
290 return 0;
291}
292
293static int need_fits(const RxResourceNeed *n, const RxResourceBudget *b) {
294 if (n->memory_bytes > b->memory_bytes) return 0;
295 if (n->accelerator_features & ~b->offered_accel) return 0;
296 if (n->locality & ~b->offered_locality) return 0;
297 if (n->compute_class && !(b->compute_mask & (1u << (n->compute_class & 31u)))) return 0;
298 if (n->energy_cost > b->energy_budget) return 0;
299 return 1;
300}
301
302PlAxis rx_plan_applicable(const PlanTemplate *t, RxWorld *w, const PlView *v, const PlEnv *env,
303 const RxObjRef *slots, const AgCapTable *caps, PlCost *cost,
304 int *pred_out) {
305 int dummy;
306 if (!pred_out) pred_out = &dummy;
307 *pred_out = -1;
308 if (t->status == PL_STATUS_STALE) return PL_AX_STALE;
309 /* Binding: every slot live, at its generation, of its type. */
310 for (uint32_t s = 0; s < t->n_slots; s++) {
311 PlPred p = { PL_P_LIVE, s, 0, 0, t->slots[s].type, 0 };
312 if (cost) cost->preds++;
313 if (!rx_plan_pred(&p, v, env, slots, t->n_slots)) { *pred_out = (int)s; return PL_AX_BINDING; }
314 }
315 int k;
316 if ((k = rx_plan_preds(&t->pre, v, env, slots, t->n_slots, cost)) >= 0) {
317 *pred_out = k;
318 return PL_AX_GOAL;
319 }
320 PlPred wg = { PL_P_ENV_EQ, PL_ENV_WORLDGEN, 0, 0, 0, t->verified_world_gen };
321 PlPred cg = { PL_P_ENV_EQ, PL_ENV_COGNITION, 0, 0, 0, t->verified_cog_gen };
322 if (cost) cost->preds += 2;
323 if (!rx_plan_pred(&wg, v, env, NULL, 0)) return PL_AX_WORLD_GEN;
324 if (!rx_plan_pred(&cg, v, env, NULL, 0)) return PL_AX_COG_GEN;
325 if ((k = rx_plan_preds(&t->env, v, env, slots, t->n_slots, cost)) >= 0) {
326 *pred_out = k;
327 return PL_AX_ENVIRONMENT;
328 }
329 if ((k = rx_plan_preds(&t->state, v, env, slots, t->n_slots, cost)) >= 0) {
330 *pred_out = k;
331 return PL_AX_STATE;
332 }
333 if ((k = rx_plan_preds(&t->invariants, v, env, slots, t->n_slots, cost)) >= 0) {
334 *pred_out = 1000 + k;
335 return PL_AX_STATE;
336 }
337 for (uint32_t s = 0; s < t->n_slots; s++) {
338 if (!t->slots[s].rights) continue;
339 if (cost) cost->preds++;
340 if (!cap_find(w, caps, v->o[slots[s].id].resource, t->slots[s].rights, NULL)) {
341 *pred_out = (int)s;
342 return PL_AX_AUTHORITY;
343 }
344 }
345 RxResourceBudget b;
346 pthread_mutex_lock(&w->mu);
347 b = w->budget;
348 pthread_mutex_unlock(&w->mu);
349 if (cost) cost->preds++;
350 if (!need_fits(&t->step_need, &b)) return PL_AX_RESOURCES;
351 return PL_AX_OK;
352}
353
354/* ---- store ---- */
355
356void rx_plan_cache_init(PlCache *c) { memset(c, 0, sizeof *c); }
357
358void rx_plan_cache_free(PlCache *c) {
359 for (uint32_t i = 0; i < c->n; i++) free(c->t[i]);
360 memset(c, 0, sizeof *c);
361}
362
363PlanTemplate *rx_plan_cache_find(PlCache *c, const uint8_t semantic_id[32]) {
364 for (uint32_t i = 0; i < c->n; i++)
365 if (memcmp(c->t[i]->semantic_id, semantic_id, 32) == 0) return c->t[i];
366 return NULL;
367}
368
369PlanTemplate *rx_plan_cache_store(PlCache *c, const PlanTemplate *t, uint64_t world_gen,
370 uint64_t cog_gen) {
371 PlanTemplate *have = rx_plan_cache_find(c, t->semantic_id);
372 if (have) {
373 /* Same meaning found again: renew where it was verified. It has one
374 * verified execution under these generations, as a new candidate has. */
375 have->verified_world_gen = world_gen;
376 have->verified_cog_gen = cog_gen;
377 have->status = PL_STATUS_CANDIDATE;
378 have->rederivations++;
379 c->deduplicated++;
380 return have;
381 }
382 if (c->n >= PL_MAX_TEMPLATES) return NULL;
383 PlanTemplate *n = malloc(sizeof *n);
384 if (!n) return NULL;
385 *n = *t;
386 n->status = PL_STATUS_CANDIDATE;
387 n->verified_world_gen = world_gen;
388 n->verified_cog_gen = cog_gen;
389 n->uses = n->successes = n->failures = n->promotions = n->rederivations = 0;
390 memset(n->refusals, 0, sizeof n->refusals);
391 c->t[c->n++] = n;
392 c->stored++;
393 return n;
394}
395
396uint32_t rx_plan_cache_retrieve(PlCache *c, uint32_t goal_kind, const uint8_t shape[32],
397 PlanTemplate **out, uint32_t max, PlCost *cost) {
398 uint32_t k = 0;
399 for (uint32_t i = 0; i < c->n && k < max; i++) {
400 if (c->t[i]->goal_kind != goal_kind || memcmp(c->t[i]->goal_shape, shape, 32) != 0) continue;
401 out[k++] = c->t[i];
402 if (cost) cost->candidates++;
403 }
404 return k;
405}
406
407void rx_plan_record_use(PlanTemplate *t, int success) {
408 t->uses++;
409 if (success) {
410 t->successes++;
411 if (t->status == PL_STATUS_CANDIDATE) {
412 t->status = PL_STATUS_VERIFIED;
413 t->promotions++;
414 }
415 } else {
416 t->failures++;
417 t->status = PL_STATUS_STALE;
418 }
419}
420
421/* ---- execution ---- */
422
423int rx_plan_execute(const PlanTemplate *t, RxWorld *w, const PlEnv *env, const RxObjRef *slots,
424 const AgCapTable *caps, const PlExecCtx *x, uint64_t run, PlRunStore *rs,
425 PlLegalFn legal, PlExecResult *out) {
426 memset(out, 0, sizeof *out);
427 if (t->graph_kind != PL_GRAPH_RX_GRAPH || t->n_slots > 8) return -1;
428 uint64_t t0 = now_ns();
429 AgLibrary lib;
430 memset(&lib, 0, sizeof lib);
431 lib.n = 1;
432 lib.proc[0] = (AgProcedure){ t->goal_kind, &t->graph };
433 AgGoal goal;
434 memset(&goal, 0, sizeof goal);
435 goal.kind = t->goal_kind;
436 goal.n_args = t->n_slots;
437 for (uint32_t s = 0; s < t->n_slots; s++) goal.args[s] = slots[s];
438 AgConstraints cons;
439 memset(&cons, 0, sizeof cons);
440 cons.max_effects = UINT32_MAX;
441 pthread_mutex_lock(&w->mu);
442 cons.budget = w->budget;
443 pthread_mutex_unlock(&w->mu);
444 AgReport rep;
445 out->compile_verdict = rx_graph_compile(&goal, w, caps, &cons, &lib, 1, &rs->g, &rep);
446 out->n_missing = rep.n_missing;
447 out->n_resource_blocked = rep.n_resource_blocked;
448 out->compile_ns = now_ns() - t0;
449 if (out->compile_verdict != AG_OK_READY) {
450 out->fail = PL_FAIL_GRAPH;
451 return 0;
452 }
453 memcpy(out->compiled_digest, rs->g.digest, 32);
454 out->nodes = rs->g.n_nodes;
455
456 /* Realization: this binding, the capabilities it uses, the compiled graph,
457 * the World generation and the core class. */
458 PlView *v = malloc(sizeof *v);
459 if (!v) return -1;
460 rx_plan_view(w, v);
461 sha256_ctx c;
462 sha256_init(&c);
463 sha256_update(&c, (const uint8_t *)"OMEGA_PLAN_REALIZATION_V1", 25);
464 sha256_update(&c, t->semantic_id, 32);
465 uint8_t b8[8];
466 for (uint32_t s = 0; s < t->n_slots; s++) {
467 RxCapRef cr = { 0, 0 };
468 if (t->slots[s].rights)
469 cap_find(w, caps, v->o[slots[s].id].resource, t->slots[s].rights, &cr);
470 uint64_t words[2] = { pl_ref_pack(slots[s]), ((uint64_t)cr.generation << 32) | cr.cap_id };
471 for (int q = 0; q < 2; q++) {
472 for (int i = 0; i < 8; i++) b8[i] = (uint8_t)(words[q] >> (8 * i));
473 sha256_update(&c, b8, 8);
474 }
475 }
476 sha256_update(&c, rs->g.digest, 32);
477 const uint64_t *wg = fields_of(v, env->obj[PL_ENV_WORLDGEN]);
478 uint64_t tail[2] = { wg ? wg[0] : 0, x->core_class };
479 for (int q = 0; q < 2; q++) {
480 for (int i = 0; i < 8; i++) b8[i] = (uint8_t)(tail[q] >> (8 * i));
481 sha256_update(&c, b8, 8);
482 }
483 sha256_final(&c, out->realization_id);
484
485 if (x->reference_first) {
486 uint64_t r0 = now_ns();
487 AgReference *ref = malloc(sizeof *ref);
488 if (!ref) { free(v); return -1; }
489 rx_graph_reference(&rs->g, w, x->skills, caps, run, ref);
490 out->reference_outcome = ref->r.outcome;
491 free(ref);
492 out->reference_ns = now_ns() - r0;
493 if (out->reference_outcome != AG_RUN_SUCCESS) {
494 out->fail = PL_FAIL_GRAPH;
495 free(v);
496 return 0;
497 }
498 }
499
500 uint64_t t1 = now_ns();
501 uint64_t checked = 0;
502 pthread_mutex_lock(&w->mu);
503 out->crumbs_before = w->n_crumbs;
504 pthread_mutex_unlock(&w->mu);
505 if (rx_graph_lower(&rs->L, w, &rs->g, x->skills, caps, x->cell_res, x->cell_cap, x->run_res,
506 x->run_cap, NULL) != 0) {
507 out->fail = PL_FAIL_GRAPH;
508 free(v);
509 return 0;
510 }
511 if (rx_graph_start(&rs->L, x->ext_run_cap, run) < 0) {
512 out->fail = PL_FAIL_GRAPH;
513 free(v);
514 return 0;
515 }
516 rx_world_wait_quiescent(w, 30000);
517 AgResult res;
518 rx_graph_collect(&rs->L, run, &res);
519 out->outcome = res.outcome;
520 out->run_ns = now_ns() - t1;
521
522 uint64_t t2 = now_ns();
523 if (res.outcome != AG_RUN_SUCCESS) out->fail |= PL_FAIL_GRAPH;
524 out->evidence_required = rs->g.n_evidence;
525 for (uint32_t i = 0; i < rs->g.n_evidence; i++)
526 out->evidence_present += res.evidence[rs->g.evidence[i]] != 0 &&
527 res.status[rs->g.evidence[i]] == AG_OK;
528 if (out->evidence_present != out->evidence_required) out->fail |= PL_FAIL_EVIDENCE;
529 rx_plan_view(w, v);
530 if (rx_plan_preds(&t->invariants, v, env, slots, t->n_slots, NULL) >= 0 || (legal && !legal(v)))
531 out->fail |= PL_FAIL_INVARIANT;
532 int ok = rx_plan_preds(&t->success, v, env, slots, t->n_slots, NULL) < 0;
533 if (rx_world_verify_crumbs(w, &checked) != 0) out->fail |= PL_FAIL_CRUMBS;
534 out->crumbs_checked = checked;
535 pthread_mutex_lock(&w->mu);
536 out->crumbs_after = w->n_crumbs;
537 pthread_mutex_unlock(&w->mu);
538 out->success = ok && out->fail == 0;
539 out->verify_ns = now_ns() - t2;
540 free(v);
541 return 0;
542}
543
544/* ---- World records ---- */
545
546int64_t rx_plan_decide(RxWorld *w, RxCapRef cap, RxObjRef decision, uint64_t goal_seq,
547 PlAxis axis, const PlanTemplate *t, int pred, uint64_t seq,
548 const uint8_t *realization_id) {
549 uint64_t f[RX_MAX_FIELDS] = {
550 goal_seq, (uint64_t)axis, t ? pl_word(t->semantic_id, 0) : 0,
551 t ? pl_word(t->semantic_id, 1) : 0, pred >= 0 ? (uint64_t)pred + 1 : 0, seq,
552 realization_id ? pl_word(realization_id, 0) : 0, t ? t->status : 0
553 };
554 RxMutation m[RX_MAX_FIELDS];
555 for (uint32_t i = 0; i < RX_MAX_FIELDS; i++) m[i] = (RxMutation){ decision, i, f[i] };
556 return rx_world_publish_external(w, cap, m, RX_MAX_FIELDS);
557}
558
559int64_t rx_plan_publish_index(RxWorld *w, RxCapRef cap, RxObjRef index, const PlCache *c,
560 const PlanTemplate *t) {
561 uint64_t f[RX_MAX_FIELDS] = {
562 c->n, pl_word(t->semantic_id, 0), pl_word(t->semantic_id, 1), t->status,
563 t->verified_world_gen, t->uses, t->successes, t->failures
564 };
565 RxMutation m[RX_MAX_FIELDS];
566 for (uint32_t i = 0; i < RX_MAX_FIELDS; i++) m[i] = (RxMutation){ index, i, f[i] };
567 return rx_world_publish_external(w, cap, m, RX_MAX_FIELDS);
568}
569
570const char *rx_plan_axis_name(PlAxis a) {
571 static const char *n[PL_AX_COUNT] = { "accepted", "shape", "binding", "goal_constraints",
572 "world_generation", "cognitive_generation",
573 "environment", "required_state", "authority",
574 "resources", "stale" };
575 return a < PL_AX_COUNT ? n[a] : "?";
576}
577
578const char *rx_plan_status_name(uint32_t s) {
579 return s == PL_STATUS_CANDIDATE ? "CANDIDATE" : s == PL_STATUS_VERIFIED ? "VERIFIED"
580 : s == PL_STATUS_STALE ? "STALE" : "?";
581}