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_aien.c

557 lines · 24636 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_aien.c -- AIEN as a resident cognitive faculty (ADR 0016 ยง44, R11).
19 *
20 * See rx_aien.h for the object and reaction map. Every fn_* is a reaction
21 * body: it reads its versioned snapshot and proposes mutations. AIEN keeps
22 * no state outside the world, so a reaction that is invalidated and runs
23 * again proposes the same thing from the same inputs.
24 */
25#include "rx_aien.h"
26
27#include <string.h>
28
29static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) {
30 for (uint32_t i = 0; i < c->n_in; i++)
31 if (c->in[i].obj.id == r.id) return &c->in[i];
32 return NULL;
33}
34
35static void put(RxCtx *c, RxObjRef o, uint32_t field, uint64_t v) {
36 c->out[c->n_out++] = (RxMutation){ o, field, v };
37}
38
39static int within(uint64_t x, uint64_t mean, uint32_t tol_pct) {
40 return x * 100u <= mean * (100u + tol_pct) && x * 100u >= mean * (100u - tol_pct);
41}
42
43/* A failed prediction is settled when explain has dealt with it and nothing
44 * is left to try: its own hypothesis was exhausted, or it refuted the
45 * hypothesis it was testing. An open or testing hypothesis is not settled:
46 * its test is the next record's prediction. */
47static int failure_settled(const RxSnapshotDep *p, const RxSnapshotDep *h) {
48 if (p->field[6] != RX_AIEN_PRED_FAILED) return 0;
49 if (h->field[7] == RX_AIEN_HYP_EXHAUSTED) return h->field[2] == p->field[0];
50 if (h->field[7] == RX_AIEN_HYP_UNSUPPORTED) return h->field[2] < p->field[0];
51 return 0;
52}
53
54/* A failed prediction is relearned from fresh intervals when its failure is
55 * settled, and also while a drift hypothesis for it is open: nothing that
56 * AIEN knows of changed, so the first test of "this record's cost moved" is
57 * to learn the same record again where it runs. aien.plan asks for research
58 * only if the relearned cost really moved (omega #527: a burst under load
59 * failed the prediction, the cost came back, but the drift plan waited for
60 * a new record that could not come, so the cost was never confirmed). */
61static int relearns(const RxSnapshotDep *p, const RxSnapshotDep *h) {
62 if (failure_settled(p, h)) return 1;
63 return p->field[6] == RX_AIEN_PRED_FAILED && h->field[2] == p->field[0] &&
64 h->field[1] == RX_AIEN_HYP_DRIFT && h->field[7] == RX_AIEN_HYP_OPEN;
65}
66
67static int explored(const RxSnapshotDep *mem, uint64_t key) {
68 uint64_t n = mem->field[0] < RX_AIEN_MEMORY_SLOTS ? mem->field[0] : RX_AIEN_MEMORY_SLOTS;
69 for (uint64_t i = 0; i < n; i++)
70 if (mem->field[1 + i] == key) return 1;
71 return 0;
72}
73
74/* ---- aien.observe: what production actually pays under the current record ----
75 *
76 * Wakes on each of Omega's demand windows and on a new selection. The cost
77 * of an interval is the difference of two demand snapshots, so it is exact
78 * however many calls landed between wakes. The first interval after a new
79 * record is discarded: it may straddle the switch. */
80static int fn_observe(RxCtx *c) {
81 RxAienFaculty *f = c->user;
82 const RxSnapshotDep *dem = in_of(c, f->in.demand);
83 const RxSnapshotDep *sel = in_of(c, f->in.selection);
84 const RxSnapshotDep *b = in_of(c, f->o.belief);
85 if (!dem || !sel || !b) return -1;
86 uint64_t epoch = sel->field[0], real = sel->field[1];
87 if (epoch == 0) return 0; /* no record yet: nothing to believe about */
88 if (rx_aien_regime(dem->field[4], dem->field[5]) != sel->field[6]) return 0;
89 uint64_t calls = dem->field[0], spent = dem->field[1];
90
91 if (b->field[0] != epoch || b->field[1] != real || calls < b->field[2]) {
92 put(c, f->o.belief, 0, epoch);
93 put(c, f->o.belief, 1, real);
94 put(c, f->o.belief, 2, calls);
95 put(c, f->o.belief, 3, spent);
96 put(c, f->o.belief, 4, 0);
97 put(c, f->o.belief, 5, 0);
98 put(c, f->o.belief, 6, 0);
99 put(c, f->o.belief, 7, 0);
100 return 0;
101 }
102 /* A prediction about this record failed on this belief and the failure
103 * is settled (explained, nothing left to try) or is open drift (see
104 * relearns). The belief has served its
105 * purpose: relearn what this record costs from fresh intervals. Without
106 * this a failed belief stays failed until a new record exists, and a
107 * later, real change (a core move) could never be noticed. */
108 const RxSnapshotDep *p = in_of(c, f->o.prediction);
109 const RxSnapshotDep *h = in_of(c, f->o.hypothesis);
110 if (!p || !h) return -1;
111 if (p->field[1] == epoch && b->field[7] >= f->cfg.misses_to_fail && relearns(p, h)) {
112 put(c, f->o.belief, 2, calls);
113 put(c, f->o.belief, 3, spent);
114 put(c, f->o.belief, 4, 0);
115 put(c, f->o.belief, 5, 0);
116 put(c, f->o.belief, 6, 0);
117 put(c, f->o.belief, 7, 0);
118 return 0;
119 }
120 uint64_t dc = calls - b->field[2];
121 if (dc < f->cfg.min_calls) return 0; /* too little evidence: wait */
122 if (dem->field[6] != real) { /* production has not switched yet */
123 put(c, f->o.belief, 2, calls);
124 put(c, f->o.belief, 3, spent);
125 return 0;
126 }
127 uint64_t x = (spent - b->field[3]) / dc;
128 uint64_t seen = b->field[5], mean = b->field[4], miss = b->field[7];
129 if (seen == 0) {
130 /* warm-up interval, discarded */
131 } else if (seen <= f->cfg.baseline_intervals) {
132 /* A baseline is a claim that the record costs `mean` within the
133 * tolerance, so its own intervals must agree within it. One that
134 * disagrees with the intervals before it starts the baseline again
135 * from itself: averaging a burst into the mean gives a cost nothing
136 * pays, and every steady interval after it then misses (R13 under
137 * load: 16361, 26203, 8370, 7708 ns learned as 14660 ns, then 391
138 * intervals at 7.0 to 8.8 us all failed against it). */
139 if (seen > 1 && !within(x, mean, f->cfg.tolerance_pct)) seen = 1;
140 mean = (mean * (seen - 1) + x) / seen;
141 } else {
142 miss = within(x, mean, f->cfg.tolerance_pct) ? 0 : miss + 1;
143 }
144 put(c, f->o.belief, 2, calls);
145 put(c, f->o.belief, 3, spent);
146 put(c, f->o.belief, 4, mean);
147 put(c, f->o.belief, 5, seen + 1);
148 put(c, f->o.belief, 6, x);
149 put(c, f->o.belief, 7, miss);
150 return 0;
151}
152
153/* ---- aien.predict: turn the belief into an explicit, checkable prediction ---- */
154static int fn_predict(RxCtx *c) {
155 RxAienFaculty *f = c->user;
156 const RxSnapshotDep *b = in_of(c, f->o.belief);
157 const RxSnapshotDep *p = in_of(c, f->o.prediction);
158 const RxSnapshotDep *pl = in_of(c, f->o.placement);
159 const RxSnapshotDep *sel = in_of(c, f->in.selection);
160 if (!b || !p || !pl || !sel) return -1;
161 if (b->field[0] == 0 || b->field[5] <= f->cfg.baseline_intervals) return 0;
162 if (b->field[0] != sel->field[0]) return 0; /* belief is about an older record */
163
164 if (p->field[0] == 0 || p->field[1] != b->field[0]) {
165 put(c, f->o.prediction, 0, p->field[0] + 1);
166 put(c, f->o.prediction, 1, b->field[0]);
167 put(c, f->o.prediction, 2, sel->field[6]);
168 put(c, f->o.prediction, 3, 0);
169 put(c, f->o.prediction, 4, pl->field[1]);
170 put(c, f->o.prediction, 5, b->field[4]);
171 put(c, f->o.prediction, 6, RX_AIEN_PRED_HOLDING);
172 put(c, f->o.prediction, 7, 0);
173 return 0;
174 }
175 if (p->field[6] == RX_AIEN_PRED_FAILED) {
176 /* Settled, and observe has relearned the same record from fresh
177 * intervals (its misses were cleared): that belief becomes a fresh
178 * prediction, so the next real change is noticed. */
179 const RxSnapshotDep *h = in_of(c, f->o.hypothesis);
180 if (!h) return -1;
181 if (relearns(p, h) && b->field[0] == p->field[1] &&
182 b->field[7] < f->cfg.misses_to_fail) {
183 put(c, f->o.prediction, 0, p->field[0] + 1);
184 put(c, f->o.prediction, 1, b->field[0]);
185 put(c, f->o.prediction, 2, sel->field[6]);
186 put(c, f->o.prediction, 3, 0);
187 put(c, f->o.prediction, 4, pl->field[1]);
188 put(c, f->o.prediction, 5, b->field[4]);
189 put(c, f->o.prediction, 6, RX_AIEN_PRED_HOLDING);
190 put(c, f->o.prediction, 7, 0);
191 }
192 return 0;
193 }
194 if (b->field[7] >= f->cfg.misses_to_fail) {
195 put(c, f->o.prediction, 6, RX_AIEN_PRED_FAILED);
196 put(c, f->o.prediction, 7, b->field[6]);
197 return 0;
198 }
199 if (b->field[5] == f->cfg.baseline_intervals + 1) return 0; /* nothing tested yet */
200 if (b->field[7] != 0) { /* one miss: not yet a failure */
201 put(c, f->o.prediction, 3, 0);
202 return 0;
203 }
204 uint64_t hits = p->field[3] + 1;
205 put(c, f->o.prediction, 3, hits);
206 if (p->field[6] == RX_AIEN_PRED_HOLDING && hits >= f->cfg.confirm_intervals)
207 put(c, f->o.prediction, 6, RX_AIEN_PRED_CONFIRMED);
208 return 0;
209}
210
211/* ---- aien.explain: form a hypothesis for a failed prediction, test it later ---- */
212static int fn_explain(RxCtx *c) {
213 RxAienFaculty *f = c->user;
214 const RxSnapshotDep *p = in_of(c, f->o.prediction);
215 const RxSnapshotDep *h = in_of(c, f->o.hypothesis);
216 const RxSnapshotDep *pl = in_of(c, f->o.placement);
217 if (!p || !h || !pl) return -1;
218 uint64_t pseq = p->field[0], state = p->field[6];
219 if (pseq == 0) return 0;
220
221 /* A later prediction under the hypothesized condition tests it. A drift
222 * plan is made on the relearned prediction (aien.plan), so only a later
223 * one tests a drift hypothesis. */
224 uint64_t tested_from = h->field[2] + (h->field[1] == RX_AIEN_HYP_DRIFT);
225 if (h->field[7] == RX_AIEN_HYP_TESTING && pseq > tested_from && p->field[4] == h->field[4]) {
226 if (state == RX_AIEN_PRED_CONFIRMED) put(c, f->o.hypothesis, 7, RX_AIEN_HYP_SUPPORTED);
227 else if (state == RX_AIEN_PRED_FAILED) put(c, f->o.hypothesis, 7, RX_AIEN_HYP_UNSUPPORTED);
228 return 0;
229 }
230 /* An open drift hypothesis is tested by relearning the same record where
231 * it runs (relearns, aien.observe). When the body moved to another core
232 * class before that relearn, the relearned prediction is about the new
233 * class and "nothing changed" is no longer the question: the record was
234 * made on another core class. Left open, the drift hypothesis would wait
235 * for a same-class prediction that cannot come, and the body would never
236 * research the class it runs on now (OMEGA-R14-HOST, 2026-10-20). */
237 if (h->field[7] == RX_AIEN_HYP_OPEN && h->field[1] == RX_AIEN_HYP_DRIFT &&
238 pseq == h->field[2] + 1 && p->field[4] != h->field[4]) {
239 put(c, f->o.hypothesis, 0, h->field[0] + 1);
240 put(c, f->o.hypothesis, 1, RX_AIEN_HYP_CORE_CLASS);
241 put(c, f->o.hypothesis, 2, pseq);
242 put(c, f->o.hypothesis, 3, h->field[4]);
243 put(c, f->o.hypothesis, 4, p->field[4]);
244 put(c, f->o.hypothesis, 5, h->field[5]);
245 put(c, f->o.hypothesis, 6, h->field[6]);
246 put(c, f->o.hypothesis, 7, RX_AIEN_HYP_OPEN);
247 return 0;
248 }
249 if (state != RX_AIEN_PRED_FAILED || h->field[2] == pseq) return 0;
250 uint64_t now = pl->field[1], then = p->field[4];
251 put(c, f->o.hypothesis, 0, h->field[0] + 1);
252 put(c, f->o.hypothesis, 1, now != then ? RX_AIEN_HYP_CORE_CLASS : RX_AIEN_HYP_DRIFT);
253 put(c, f->o.hypothesis, 2, pseq);
254 put(c, f->o.hypothesis, 3, then);
255 put(c, f->o.hypothesis, 4, now);
256 put(c, f->o.hypothesis, 5, p->field[5]);
257 put(c, f->o.hypothesis, 6, p->field[7]);
258 put(c, f->o.hypothesis, 7, RX_AIEN_HYP_OPEN);
259 return 0;
260}
261
262/* ---- aien.assess: is a human goal met by what AIEN now expects? ---- */
263static int fn_assess(RxCtx *c) {
264 RxAienFaculty *f = c->user;
265 const RxSnapshotDep *g = in_of(c, f->o.goal);
266 const RxSnapshotDep *p = in_of(c, f->o.prediction);
267 const RxSnapshotDep *m = in_of(c, f->o.memory);
268 const RxSnapshotDep *pl = in_of(c, f->o.placement);
269 if (!g || !p || !m || !pl) return -1;
270 if (g->field[0] == 0) return 0;
271 uint64_t status = RX_AIEN_GOAL_UNKNOWN, expected = 0;
272 if (p->field[0] != 0 && p->field[2] == g->field[1] && p->field[6] == RX_AIEN_PRED_CONFIRMED &&
273 p->field[4] == pl->field[1]) {
274 expected = p->field[5];
275 if (expected <= g->field[2]) status = RX_AIEN_GOAL_MET;
276 else if (explored(m, rx_aien_key(pl->field[1], g->field[1]))) status = RX_AIEN_GOAL_UNMET_EXPLORED;
277 else status = RX_AIEN_GOAL_UNMET;
278 } else if (p->field[0] != 0 && p->field[2] == g->field[1] &&
279 p->field[6] == RX_AIEN_PRED_CONFIRMED) {
280 /* A known cost on another core is evidence that the old regime did
281 * not meet the goal. The new core remains unknown, which itself can
282 * justify one bounded research plan. */
283 expected = p->field[5];
284 }
285 put(c, f->o.assessment, 0, g->field[0]);
286 put(c, f->o.assessment, 1, g->field[1]);
287 put(c, f->o.assessment, 2, g->field[2]);
288 put(c, f->o.assessment, 3, expected);
289 put(c, f->o.assessment, 4, status);
290 put(c, f->o.assessment, 5, pl->field[1]);
291 put(c, f->o.assessment, 6, p->field[0]);
292 return 0;
293}
294
295/* ---- aien.plan: one experiment per unexplored condition ---- */
296static void remember(RxCtx *c, RxAienFaculty *f, const RxSnapshotDep *m, uint64_t key) {
297 uint64_t n = m->field[0];
298 /* Oldest entry is overwritten once full; the count keeps growing. */
299 put(c, f->o.memory, 1 + (uint32_t)(n % RX_AIEN_MEMORY_SLOTS), key);
300 put(c, f->o.memory, 0, n + 1);
301}
302
303static void publish_plan(RxCtx *c, RxAienFaculty *f, const RxSnapshotDep *pn, uint64_t regime,
304 uint64_t cls, uint64_t hseq, uint64_t why, uint64_t gseq) {
305 put(c, f->o.plan, 0, pn->field[0] + 1);
306 put(c, f->o.plan, 1, RX_AIEN_ACT_RESEARCH);
307 put(c, f->o.plan, 2, regime);
308 put(c, f->o.plan, 3, cls);
309 put(c, f->o.plan, 4, hseq);
310 put(c, f->o.plan, 5, why);
311 put(c, f->o.plan, 6, gseq);
312}
313
314static int fn_plan(RxCtx *c) {
315 RxAienFaculty *f = c->user;
316 const RxSnapshotDep *h = in_of(c, f->o.hypothesis);
317 const RxSnapshotDep *a = in_of(c, f->o.assessment);
318 const RxSnapshotDep *m = in_of(c, f->o.memory);
319 const RxSnapshotDep *p = in_of(c, f->o.prediction);
320 const RxSnapshotDep *pn = in_of(c, f->o.plan);
321 if (!h || !a || !m || !p || !pn) return -1;
322
323 if (a->field[0] != 0 &&
324 (a->field[4] == RX_AIEN_GOAL_UNMET ||
325 (a->field[4] == RX_AIEN_GOAL_UNKNOWN &&
326 a->field[3] > a->field[2])) &&
327 pn->field[6] != a->field[0]) {
328 uint64_t key = rx_aien_key(a->field[5], a->field[1]);
329 if (!explored(m, key)) {
330 publish_plan(c, f, pn, a->field[1], a->field[5], 0,
331 RX_AIEN_WHY_GOAL, a->field[0]);
332 remember(c, f, m, key);
333 return 0;
334 }
335 }
336
337 if (h->field[7] == RX_AIEN_HYP_OPEN) {
338 if (h->field[1] == RX_AIEN_HYP_DRIFT) {
339 /* Drift is first tested by relearning the same record (see
340 * relearns). Research only if the relearned cost really moved;
341 * if it is back within the tolerance of what the failed
342 * prediction expected, the failure was a burst, not drift. */
343 if (p->field[0] != h->field[2] + 1 || p->field[6] != RX_AIEN_PRED_CONFIRMED ||
344 p->field[4] != h->field[4]) return 0;
345 if (within(p->field[5], h->field[5], f->cfg.tolerance_pct)) {
346 put(c, f->o.hypothesis, 7, RX_AIEN_HYP_UNSUPPORTED);
347 return 0;
348 }
349 } else if (p->field[0] != h->field[2]) {
350 return 0; /* prediction moved on; wait for it */
351 }
352 uint64_t regime = p->field[2], cls = h->field[4];
353 uint64_t key = rx_aien_key(cls, regime);
354 if (explored(m, key)) {
355 put(c, f->o.hypothesis, 7, RX_AIEN_HYP_EXHAUSTED);
356 return 0;
357 }
358 uint64_t why = h->field[1] == RX_AIEN_HYP_CORE_CLASS ? RX_AIEN_WHY_CORE_CLASS
359 : RX_AIEN_WHY_DRIFT;
360 publish_plan(c, f, pn, regime, cls, h->field[0], why, 0);
361 remember(c, f, m, key);
362 put(c, f->o.hypothesis, 7, RX_AIEN_HYP_TESTING);
363 return 0;
364 }
365 return 0;
366}
367
368/* ---- setup ---- */
369
370void rx_aien_default_config(RxAienConfig *cfg) {
371 memset(cfg, 0, sizeof(*cfg));
372 cfg->baseline_intervals = 4;
373 cfg->tolerance_pct = 20;
374 cfg->misses_to_fail = 2;
375 cfg->confirm_intervals = 4;
376 cfg->min_calls = 8;
377}
378
379int rx_aien_create_objects(RxAienFaculty *f, RxWorld *w, const RxAienConfig *cfg,
380 const RxAienInputs *in) {
381 memset(f, 0, sizeof(*f));
382 f->w = w;
383 f->cfg = *cfg;
384 f->in = *in;
385 if (cfg->baseline_intervals == 0 || cfg->misses_to_fail == 0 || cfg->tolerance_pct >= 100)
386 return RX_ERR_ARG;
387 uint64_t z[RX_MAX_FIELDS] = { 0 };
388#define MK(ref, type, res) do { \
389 int rc_ = rx_world_create(w, (type), RX_PERSIST_RESIDENT, \
390 RX_AIEN_RES_BASE + (res), z, &(ref)); \
391 if (rc_ != RX_OK) return rc_; \
392 } while (0)
393 MK(f->o.placement, RX_OT_PLACEMENT, RX_AIEN_RES_PLACEMENT);
394 MK(f->o.goal, RX_OT_GOAL, RX_AIEN_RES_GOAL);
395 MK(f->o.belief, RX_OT_BELIEF, RX_AIEN_RES_BELIEF);
396 MK(f->o.prediction, RX_OT_PREDICTION, RX_AIEN_RES_PREDICTION);
397 MK(f->o.hypothesis, RX_OT_HYPOTHESIS, RX_AIEN_RES_HYPOTHESIS);
398 MK(f->o.plan, RX_OT_PLAN, RX_AIEN_RES_PLAN);
399 MK(f->o.assessment, RX_OT_ASSESSMENT, RX_AIEN_RES_ASSESSMENT);
400 MK(f->o.memory, RX_OT_MEMORY, RX_AIEN_RES_MEMORY);
401 MK(f->o.experiment_belief, RX_OT_EXPERIMENT_BELIEF, RX_AIEN_RES_EXPERIMENT_BELIEF);
402#undef MK
403 return RX_OK;
404}
405
406typedef struct {
407 RxReactionDesc d;
408 const RxAienFaculty *f;
409 const RxAienCaps *caps;
410} Builder;
411
412static RxCapRef cap_for(const Builder *b, RxObjRef o) {
413 if (o.id == b->f->in.demand.id) return b->caps->demand;
414 if (o.id == b->f->in.selection.id) return b->caps->selection;
415 uint64_t res = b->f->w->objects[o.id].resource;
416 return b->caps->own[res - RX_AIEN_RES_BASE];
417}
418
419/* This optional reaction is deliberately separate from the cost prediction:
420 * a GB10 add witnesses an experiment but does not measure matvec cost. */
421static int fn_experiment(RxCtx *c) {
422 RxAienFaculty *f = c->user;
423 const RxSnapshotDep *e = in_of(c, f->experiment_evidence);
424 const RxSnapshotDep *b = in_of(c, f->o.experiment_belief);
425 if (!e || !b) return -1;
426 if (e->field[0] == 0 || e->field[0] <= b->field[0]) return 0;
427 /* Supported only when every replicate matched; one mismatch refutes. */
428 int held = e->field[3] == 1 && e->field[1] == e->field[2] && e->field[4] != 0;
429 put(c, f->o.experiment_belief, 0, e->field[0]);
430 put(c, f->o.experiment_belief, 1, e->field[2]);
431 put(c, f->o.experiment_belief, 2, held ? RX_AIEN_EXP_SUPPORTED : RX_AIEN_EXP_REFUTED);
432 put(c, f->o.experiment_belief, 3, e->field[4]);
433 return 0;
434}
435
436int rx_aien_register_experiment(RxAienFaculty *f, RxObjRef evidence,
437 RxCapRef evidence_read, RxCapRef belief_write) {
438 if (!f || !f->w || evidence.id >= RX_MAX_OBJECTS ||
439 !f->w->objects[evidence.id].live ||
440 f->w->objects[evidence.id].generation != evidence.generation) return RX_ERR_ARG;
441 f->experiment_evidence = evidence;
442 RxReactionDesc d;
443 memset(&d, 0, sizeof d);
444 d.name = "aien.experiment.observe";
445 d.faculty = RX_FACULTY_AIEN;
446 d.subject = RX_AIEN_SUBJ;
447 d.priority = RX_PRIO_LEARNING;
448 d.fn = fn_experiment;
449 d.user = f;
450 d.n_triggers = 1;
451 d.triggers[0] = (RxDep){ evidence, RX_FIELD(0) };
452 d.n_reads = 1;
453 d.reads[0] = (RxDep){ f->o.experiment_belief, RX_ALL_FIELDS };
454 d.n_writes = 1;
455 d.writes[0] = (RxDep){ f->o.experiment_belief, RX_ALL_FIELDS };
456 d.n_caps = 2;
457 d.caps[0] = (RxCapNeed){ evidence_read, f->w->objects[evidence.id].resource, RX_RIGHT_READ };
458 d.caps[1] = (RxCapNeed){ belief_write,
459 RX_AIEN_RES_BASE + RX_AIEN_RES_EXPERIMENT_BELIEF, RX_RIGHT_READ | RX_RIGHT_WRITE };
460 return rx_world_add_reaction_keyed(f->w, f->keys, &d, &f->r_experiment);
461}
462
463static void need(Builder *b, RxObjRef o, uint32_t rights) {
464 uint64_t res = b->f->w->objects[o.id].resource;
465 for (uint32_t i = 0; i < b->d.n_caps; i++)
466 if (b->d.caps[i].resource == res) { b->d.caps[i].rights |= rights; return; }
467 b->d.caps[b->d.n_caps++] = (RxCapNeed){ cap_for(b, o), res, rights };
468}
469
470static void begin(Builder *b, const char *name, uint32_t prio, RxFn fn, RxAienFaculty *f,
471 const RxAienCaps *caps) {
472 memset(b, 0, sizeof(*b));
473 b->d.name = name;
474 b->d.faculty = RX_FACULTY_AIEN;
475 b->d.subject = RX_AIEN_SUBJ;
476 b->d.priority = prio;
477 b->d.fn = fn;
478 b->d.user = f;
479 b->f = f;
480 b->caps = caps;
481}
482
483static void trig(Builder *b, RxObjRef o, uint64_t mask) {
484 b->d.triggers[b->d.n_triggers++] = (RxDep){ o, mask };
485 need(b, o, RX_RIGHT_READ);
486}
487
488static void rd(Builder *b, RxObjRef o) {
489 b->d.reads[b->d.n_reads++] = (RxDep){ o, RX_ALL_FIELDS };
490 need(b, o, RX_RIGHT_READ);
491}
492
493static void wr(Builder *b, RxObjRef o) {
494 b->d.writes[b->d.n_writes++] = (RxDep){ o, RX_ALL_FIELDS };
495 need(b, o, RX_RIGHT_READ | RX_RIGHT_WRITE);
496}
497
498int rx_aien_register(RxAienFaculty *f, const RxAienCaps *caps) {
499 RxWorld *w = f->w;
500 Builder b;
501 int rc;
502
503 begin(&b, "aien.observe", RX_PRIO_LEARNING, fn_observe, f, caps);
504 trig(&b, f->in.demand, RX_FIELD(2));
505 trig(&b, f->in.selection, RX_FIELD(0));
506 rd(&b, f->o.belief);
507 rd(&b, f->o.prediction);
508 rd(&b, f->o.hypothesis);
509 wr(&b, f->o.belief);
510 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_observe)) != RX_OK) return rc;
511
512 begin(&b, "aien.predict", RX_PRIO_LEARNING, fn_predict, f, caps);
513 trig(&b, f->o.belief, RX_FIELD(5));
514 rd(&b, f->o.prediction);
515 rd(&b, f->o.placement);
516 rd(&b, f->in.selection);
517 rd(&b, f->o.hypothesis);
518 wr(&b, f->o.prediction);
519 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_predict)) != RX_OK) return rc;
520
521 begin(&b, "aien.explain", RX_PRIO_LEARNING, fn_explain, f, caps);
522 trig(&b, f->o.prediction, RX_FIELD(0) | RX_FIELD(6));
523 rd(&b, f->o.hypothesis);
524 rd(&b, f->o.placement);
525 wr(&b, f->o.hypothesis);
526 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_explain)) != RX_OK) return rc;
527
528 begin(&b, "aien.assess", RX_PRIO_LEARNING, fn_assess, f, caps);
529 trig(&b, f->o.goal, RX_ALL_FIELDS);
530 trig(&b, f->o.prediction, RX_FIELD(0) | RX_FIELD(6));
531 rd(&b, f->o.memory);
532 rd(&b, f->o.placement);
533 rd(&b, f->o.assessment);
534 wr(&b, f->o.assessment);
535 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_assess)) != RX_OK) return rc;
536
537 begin(&b, "aien.plan", RX_PRIO_LEARNING, fn_plan, f, caps);
538 trig(&b, f->o.hypothesis, RX_FIELD(0) | RX_FIELD(7)); /* a new hypothesis, or a state */
539 trig(&b, f->o.assessment, RX_FIELD(4) | RX_FIELD(0));
540 rd(&b, f->o.memory);
541 trig(&b, f->o.prediction, RX_FIELD(0) | RX_FIELD(6)); /* a relearned drift prediction */
542 rd(&b, f->o.plan);
543 wr(&b, f->o.plan);
544 wr(&b, f->o.memory);
545 wr(&b, f->o.hypothesis);
546 if ((rc = rx_world_add_reaction_keyed(w, f->keys, &b.d, &f->r_plan)) != RX_OK) return rc;
547 return RX_OK;
548}
549
550/* ---- restart: explored-condition memory from outside ---- */
551int rx_aien_seed_explored(RxAienFaculty *f, RxCapRef memory_write, const uint64_t *keys, uint32_t n) {
552 if (!f || !f->w || !keys || n == 0 || n > RX_AIEN_MEMORY_SLOTS) return RX_ERR_ARG;
553 RxMutation m[1 + RX_AIEN_MEMORY_SLOTS];
554 m[0] = (RxMutation){f->o.memory, 0, n};
555 for (uint32_t i = 0; i < n; i++) m[1 + i] = (RxMutation){f->o.memory, 1 + i, keys[i]};
556 return rx_world_publish_external(f->w, memory_write, m, n + 1) > 0 ? RX_OK : RX_ERR_AUTHORITY;
557}