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 / tools / allen.c

468 lines · 17917 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/* allen.c -- thin host tool for ALLEN subject state (ARCH-0035 PROPOSED).
18 *
19 * Inspection and qualification only. Not a chat, not an agent runtime, not a
20 * planner, not a daemon, not a Visor, not an operator shell. Every verb runs
21 * to completion and exits; nothing here decides which faculty runs next.
22 *
23 * allen make -o FILE --agent HEX32 --root HEX32 --prov HEX32
24 * [--intent REGIME,TARGET_NS]... [--cortex JOURNAL]
25 * Build a v0 subject object (test fixture: in production the object is
26 * committed into the Aienux Store by continuity_subject.c; this verb
27 * writes the same bytes to a file for host qualification).
28 * allen inspect FILE Decode, print identity, chain, bindings, intents.
29 * allen digest FILE Print the subject id only.
30 * allen lineage JOURNAL Print the Cortex lineage reference of a journal.
31 * allen seed-journal JOURNAL [TAG]
32 * Create a Cortex journal with one fixture record (the organism's genesis
33 * record stand-in; TAG makes two fixtures distinct lineages).
34 * allen publish FILE JOURNAL [--model HEX32]
35 * The host rig: capability root, World, the resident AIEN faculty
36 * (part A of R11: AIEN alone). Checks the memory binding (fail closed),
37 * records the identity binding, publishes every ACTIVE standing intent
38 * through rx_world_publish_external, waits for quiescence and prints
39 * what AIEN did with it. The model digest is printed as provenance and
40 * stored nowhere.
41 * allen probe JOURNAL
42 * The same rig started without ALLEN: how many goals does the World
43 * hold, and how many records does the journal hold? (negative control)
44 *
45 * Exit: 0 ok; 2 usage; 3 refused (decode, binding, publish); 4 rig failure. */
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
50#include "allen/allen_bind.h"
51#include "runtime/aienos_cap.h"
52#include "runtime/rx_aien.h"
53#include "runtime/rx_cortex.h"
54#include "runtime/rx_cortex_record.h"
55#include "runtime/rx_omega.h"
56#include "runtime/rx_world.h"
57
58#define SUBJ_EXTERNAL 7u
59#define ISSUER 1u
60#define RES_DEMAND 0x7711000ull
61#define RES_SELECTION 0x7711001ull
62#define JOURNAL_SUBJECTS 512u
63#define SESSION 0x414c4c454e2d3030ull /* "ALLEN-00" */
64
65static int usage(void)
66{
67 fprintf(stderr,
68 "usage: allen make -o FILE --agent HEX --root HEX --prov HEX [--intent R,T]... [--cortex JOURNAL]\n"
69 " allen inspect FILE | digest FILE | lineage JOURNAL | seed-journal JOURNAL [TAG]\n"
70 " allen publish FILE JOURNAL [--model HEX] | probe JOURNAL\n");
71 return 2;
72}
73
74/* ---- rig: the body ALLEN state is published into ---- */
75typedef struct {
76 AienosCapAdmin *admin;
77 AienosCapView *view;
78 RxWorld w;
79 RxAienFaculty a;
80 RxAienCaps acaps;
81 RxObjRef demand, selection;
82 RxCapRef ext_goal;
83 int up;
84} Rig;
85
86static RxCapRef mint(Rig *r, uint32_t subject, uint64_t resource, uint32_t rights)
87{
88 AienosCapRef office, out = {UINT32_MAX, 0};
89 aienos_cap_office(r->admin, &office);
90 AienosCapMint m = {ISSUER, subject, resource, rights, 0, {UINT32_MAX, 0}, office};
91 if (aienos_cap_mint(r->admin, &m, &out) != 0) out = (AienosCapRef){UINT32_MAX, 0};
92 return (RxCapRef){out.cap_id, out.generation};
93}
94
95static int rig_start(Rig *r)
96{
97 const uint32_t RW = RX_RIGHT_READ | RX_RIGHT_WRITE;
98 uint64_t z[RX_MAX_FIELDS] = {0};
99 RxAienConfig cfg;
100 RxAienInputs in;
101 memset(r, 0, sizeof *r);
102 if (aienos_cap_start(&r->admin, &r->view) != 0) return -1;
103 if (rx_world_init_native(&r->w, r->view, 4, 1u << 20) != RX_OK) {
104 aienos_cap_stop(r->admin, r->view);
105 return -1;
106 }
107 r->up = 1;
108 r->w.external_subject = SUBJ_EXTERNAL;
109 if (rx_world_create(&r->w, RX_OT_DEMAND, RX_PERSIST_RESIDENT, RES_DEMAND, z, &r->demand) != RX_OK ||
110 rx_world_create(&r->w, RX_OT_SELECTION, RX_PERSIST_RESIDENT, RES_SELECTION, z, &r->selection) != RX_OK)
111 return -1;
112 rx_aien_default_config(&cfg);
113 in.demand = r->demand;
114 in.selection = r->selection;
115 if (rx_aien_create_objects(&r->a, &r->w, &cfg, &in) != RX_OK) return -1;
116 for (uint32_t i = 0; i < RX_AIEN_RES_COUNT; i++)
117 r->acaps.own[i] = mint(r, RX_AIEN_SUBJ, RX_AIEN_RES_BASE + i,
118 i == RX_AIEN_RES_PLACEMENT || i == RX_AIEN_RES_GOAL ? RX_RIGHT_READ : RW);
119 r->acaps.demand = mint(r, RX_AIEN_SUBJ, RES_DEMAND, RX_RIGHT_READ);
120 r->acaps.selection = mint(r, RX_AIEN_SUBJ, RES_SELECTION, RX_RIGHT_READ);
121 /* The outside world may write the goal; ALLEN's intents arrive that way. */
122 r->ext_goal = mint(r, SUBJ_EXTERNAL, RX_AIEN_RES_BASE + RX_AIEN_RES_GOAL, RX_RIGHT_WRITE);
123 return rx_aien_register(&r->a, &r->acaps) == RX_OK ? 0 : -1;
124}
125
126static void rig_stop(Rig *r)
127{
128 if (!r->up) return;
129 rx_world_wait_quiescent(&r->w, 20000);
130 rx_world_destroy(&r->w);
131 aienos_cap_stop(r->admin, r->view);
132 r->up = 0;
133}
134
135static uint64_t fld(Rig *r, RxObjRef o, uint32_t i)
136{
137 RxObject x;
138 if (rx_world_read(&r->w, o, &x) != RX_OK) return UINT64_MAX;
139 return x.field[i];
140}
141
142static uint32_t goals_in_world(Rig *r)
143{
144 uint32_t n = 0;
145 for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) {
146 const RxObject *o = &r->w.objects[i];
147 if (o->live && o->type == RX_OT_GOAL && o->field[0] != 0) n++;
148 }
149 return n;
150}
151
152/* ---- verbs ---- */
153static void print_subject(const struct cs_subject *s, const uint8_t id[32])
154{
155 char h[65];
156 allen_hex(id, 32, h);
157 printf("ALLEN subject %s\n", h);
158 allen_hex(s->agent, 32, h);
159 printf("ALLEN agent %s\n", h);
160 allen_hex(s->root, 32, h);
161 printf("ALLEN root %s\n", h);
162 allen_hex(s->previous, 32, h);
163 printf("ALLEN chain sequence %llu previous %s\n", (unsigned long long)s->sequence, h);
164 allen_hex(s->cortex, 32, h);
165 printf("ALLEN cortex %s\n", h);
166 allen_hex(s->provenance, 32, h);
167 printf("ALLEN provenance %s origin %u\n", h, s->origin);
168 printf("ALLEN intents %u knowledge %u\n", s->n_intents, s->n_knowledge);
169 for (uint32_t i = 0; i < s->n_intents; i++) {
170 const struct cs_intent *a = &s->in[i];
171 allen_hex(a->id, 32, h);
172 printf("ALLEN intent %s kind %u state %u since %llu regime %llu target_ns %llu\n", h, a->kind,
173 a->state, (unsigned long long)a->since, (unsigned long long)a->payload[0],
174 (unsigned long long)a->payload[1]);
175 }
176 for (uint32_t i = 0; i < s->n_knowledge; i++) {
177 allen_hex(s->kn[i].digest, 32, h);
178 printf("ALLEN knowledge %s record %llu since %llu\n", h, (unsigned long long)s->kn[i].record,
179 (unsigned long long)s->kn[i].since);
180 }
181}
182
183static int v_make(int argc, char **argv)
184{
185 static struct cs_subject s;
186 static uint8_t buf[CC_MAX_OBJECT_BYTES];
187 struct cr_view v;
188 uint8_t prov[32] = {0}, id[32];
189 const char *out = NULL, *journal = NULL, *why = NULL;
190 size_t len = 0;
191 int have_agent = 0, have_root = 0;
192 memset(&v, 0, sizeof v);
193 for (int i = 2; i < argc; i++) {
194 if (!strcmp(argv[i], "-o") && i + 1 < argc) out = argv[++i];
195 else if (!strcmp(argv[i], "--agent") && i + 1 < argc) have_agent = allen_unhex(argv[++i], v.root.agent_id, 32) == 0;
196 else if (!strcmp(argv[i], "--root") && i + 1 < argc) have_root = allen_unhex(argv[++i], v.root_id, 32) == 0;
197 else if (!strcmp(argv[i], "--prov") && i + 1 < argc) { if (allen_unhex(argv[++i], prov, 32)) return usage(); }
198 else if (!strcmp(argv[i], "--cortex") && i + 1 < argc) journal = argv[++i];
199 else if (!strcmp(argv[i], "--intent") && i + 1 < argc) i++; /* second pass */
200 else return usage();
201 }
202 if (!out || !have_agent || !have_root) return usage();
203 cs_subject_genesis(&s, &v, prov, CS_ORIGIN_OPERATOR);
204 for (int i = 2; i < argc; i++) {
205 if (!strcmp(argv[i], "--intent") && i + 1 < argc) {
206 uint64_t p[2];
207 unsigned long long a, b;
208 if (sscanf(argv[++i], "%llu,%llu", &a, &b) != 2) return usage();
209 p[0] = a;
210 p[1] = b;
211 if (cs_subject_intend(&s, CS_INTENT_GOAL_LATENCY, p, NULL, &why) != CC_OK) {
212 fprintf(stderr, "allen: intent refused: %s\n", why);
213 return 3;
214 }
215 }
216 }
217 if (journal) {
218 CxStore cx;
219 uint8_t ref[32];
220 if (cx_open(&cx, journal, JOURNAL_SUBJECTS, CX_OPEN_READONLY) != CX_OK) {
221 fprintf(stderr, "allen: cannot open journal %s\n", journal);
222 return 3;
223 }
224 if (allen_lineage(&cx, ref) != 0) {
225 fprintf(stderr, "allen: journal has no genesis record; subject left unbound\n");
226 } else {
227 cs_subject_bind_cortex(&s, ref);
228 }
229 cx_close(&cx);
230 }
231 if (cs_subject_encode(&s, buf, sizeof buf, &len, &why) != CC_OK) {
232 fprintf(stderr, "allen: encode refused: %s\n", why);
233 return 3;
234 }
235 {
236 FILE *f = fopen(out, "wb");
237 if (!f || fwrite(buf, 1, len, f) != len) {
238 fprintf(stderr, "allen: cannot write %s\n", out);
239 return 4;
240 }
241 fclose(f);
242 }
243 cs_subject_id(buf, len, id);
244 print_subject(&s, id);
245 return 0;
246}
247
248static int v_inspect(const char *path, int digest_only)
249{
250 static struct cs_subject s;
251 uint8_t id[32];
252 const char *why = NULL;
253 char h[65];
254 if (allen_load(path, &s, id, &why) != 0) {
255 fprintf(stderr, "allen: refused: %s\n", why ? why : "?");
256 return 3;
257 }
258 if (digest_only) {
259 allen_hex(id, 32, h);
260 printf("%s\n", h);
261 return 0;
262 }
263 print_subject(&s, id);
264 return 0;
265}
266
267static int v_lineage(const char *journal)
268{
269 CxStore cx;
270 uint8_t ref[32];
271 char h[65];
272 if (cx_open(&cx, journal, JOURNAL_SUBJECTS, CX_OPEN_READONLY) != CX_OK) {
273 fprintf(stderr, "allen: cannot open journal %s\n", journal);
274 return 3;
275 }
276 if (allen_lineage(&cx, ref) != 0) {
277 printf("ALLEN lineage unbound (journal has no record)\n");
278 cx_close(&cx);
279 return 0;
280 }
281 allen_hex(ref, 32, h);
282 printf("ALLEN lineage %s records %llu\n", h, (unsigned long long)cx.n);
283 cx_close(&cx);
284 return 0;
285}
286
287static int v_seed(const char *journal, uint64_t tag)
288{
289 CxStore cx;
290 CxHeader hd;
291 uint64_t payload[2] = {0x53454544ull, tag}; /* "SEED" + tag: fixture, not a claim */
292 uint64_t id = 0;
293 uint8_t ref[32];
294 char h[65];
295 if (cx_open(&cx, journal, JOURNAL_SUBJECTS, CX_OPEN_SYNC) != CX_OK) {
296 fprintf(stderr, "allen: cannot create journal %s\n", journal);
297 return 3;
298 }
299 if (cx.n != 0) {
300 fprintf(stderr, "allen: journal already has %llu records\n", (unsigned long long)cx.n);
301 cx_close(&cx);
302 return 3;
303 }
304 memset(&hd, 0, sizeof hd);
305 hd.cls = CX_OBSERVATION;
306 hd.kind = CX_K_WORK_ACCEPTED;
307 hd.subject = 1;
308 hd.t = 1;
309 hd.generation = 1;
310 if (cx_append(&cx, &hd, payload, 2, &id) != CX_OK) {
311 fprintf(stderr, "allen: cannot append the genesis record\n");
312 cx_close(&cx);
313 return 3;
314 }
315 allen_lineage(&cx, ref);
316 allen_hex(ref, 32, h);
317 printf("ALLEN seeded journal %s record %llu lineage %s\n", journal, (unsigned long long)id, h);
318 cx_close(&cx);
319 return 0;
320}
321
322static int v_publish(int argc, char **argv)
323{
324 static struct cs_subject s;
325 static Rig r;
326 CxStore cx;
327 uint8_t id[32], model[32];
328 int have_model = 0;
329 const char *why = NULL;
330 const char *path = argv[2], *journal = argv[3];
331 char h[65];
332 AllenBinding b;
333 uint64_t goal_seq = 0, published = 0;
334 int64_t last_cid = 0;
335 for (int i = 4; i < argc; i++) {
336 if (!strcmp(argv[i], "--model") && i + 1 < argc) have_model = allen_unhex(argv[++i], model, 32) == 0;
337 else return usage();
338 }
339 if (allen_load(path, &s, id, &why) != 0) {
340 fprintf(stderr, "allen: refused: %s\n", why ? why : "?");
341 return 3;
342 }
343 if (cx_open(&cx, journal, JOURNAL_SUBJECTS, CX_OPEN_SYNC) != CX_OK) {
344 fprintf(stderr, "allen: cannot open journal %s\n", journal);
345 return 3;
346 }
347 /* Memory binding first: a subject never acts on a journal that is not its own. */
348 if (allen_check_memory(&s, &cx, &why) != 0) {
349 fprintf(stderr, "allen: refused: %s\n", why);
350 cx_close(&cx);
351 return 3;
352 }
353 if (rig_start(&r) != 0) {
354 fprintf(stderr, "allen: rig failed\n");
355 rig_stop(&r);
356 cx_close(&cx);
357 return 4;
358 }
359 /* The organism's recorder: every crumb becomes a Cortex record (as the
360 * living system does). ALLEN does not write Cortex itself. */
361 if (rx_cortex_attach(&r.w, &cx, SESSION) != RX_OK) {
362 fprintf(stderr, "allen: cortex attach failed\n");
363 rig_stop(&r);
364 cx_close(&cx);
365 return 4;
366 }
367 allen_bind_identity(&r.w, &s, id, &b);
368 print_subject(&s, id);
369 allen_hex(b.agent, 32, h);
370 printf("ALLEN binding external_subject %u agent %s sequence %llu\n", b.external_subject, h,
371 (unsigned long long)b.sequence);
372 allen_hex(s.cortex, 32, h);
373 printf("ALLEN memory BOUND lineage %s journal %s\n", h, journal);
374 if (have_model) {
375 allen_hex(model, 32, h);
376 printf("ALLEN model %s (provenance only; not in the subject object)\n", h);
377 } else {
378 printf("ALLEN model none (no model required)\n");
379 }
380 goal_seq = fld(&r, r.a.o.goal, 0);
381 for (uint32_t i = 0; i < s.n_intents; i++) {
382 RxMutation m[4];
383 uint32_t n = allen_goal_mutations(&s.in[i], r.a.o.goal, ++goal_seq, m);
384 if (n == 0) {
385 goal_seq--;
386 continue;
387 }
388 last_cid = rx_world_publish_external(&r.w, r.ext_goal, m, n);
389 allen_hex(s.in[i].id, 32, h);
390 if (last_cid <= 0) {
391 printf("ALLEN publish intent %s REFUSED %lld\n", h, (long long)last_cid);
392 rx_cortex_detach(&r.w);
393 rig_stop(&r);
394 cx_close(&cx);
395 return 3;
396 }
397 printf("ALLEN publish intent %s -> EXTERNAL crumb %lld\n", h, (long long)last_cid);
398 published++;
399 }
400 rx_world_wait_quiescent(&r.w, 20000);
401 /* What AIEN did with it: readiness, not a call. */
402 {
403 uint64_t assess_acts = 0, in_episode = 0;
404 for (uint64_t c = 1; c <= r.w.n_crumbs; c++) {
405 const RxCrumb *k = rx_world_crumb(&r.w, c);
406 if (!k || k->kind == RX_CRUMB_EXTERNAL || k->kind == RX_CRUMB_CREATE) continue;
407 if (k->reaction == r.a.r_assess && k->faculty == RX_FACULTY_AIEN) {
408 assess_acts++;
409 if ((int64_t)k->episode == last_cid) in_episode++;
410 }
411 }
412 printf("ALLEN goal object seq %llu regime %llu target_ns %llu intent_lo %016llx\n",
413 (unsigned long long)fld(&r, r.a.o.goal, 0), (unsigned long long)fld(&r, r.a.o.goal, 1),
414 (unsigned long long)fld(&r, r.a.o.goal, 2), (unsigned long long)fld(&r, r.a.o.goal, 3));
415 printf("AIEN assess activations %llu in_episode_of_last_publish %llu assessment goal_seq %llu status %llu\n",
416 (unsigned long long)assess_acts, (unsigned long long)in_episode,
417 (unsigned long long)fld(&r, r.a.o.assessment, 0), (unsigned long long)fld(&r, r.a.o.assessment, 4));
418 printf("ALLEN published %llu goals_in_world %u crumbs %llu journal_records %llu\n",
419 (unsigned long long)published, goals_in_world(&r), (unsigned long long)r.w.n_crumbs,
420 (unsigned long long)cx.n);
421 }
422 rx_cortex_detach(&r.w);
423 rig_stop(&r);
424 cx_close(&cx);
425 return 0;
426}
427
428static int v_probe(const char *journal)
429{
430 static Rig r;
431 CxStore cx;
432 if (cx_open(&cx, journal, JOURNAL_SUBJECTS, CX_OPEN_READONLY) != CX_OK) {
433 fprintf(stderr, "allen: cannot open journal %s\n", journal);
434 return 3;
435 }
436 if (cx_verify_chain(&cx) != CX_OK) {
437 fprintf(stderr, "allen: journal chain does not verify\n");
438 cx_close(&cx);
439 return 3;
440 }
441 if (rig_start(&r) != 0) {
442 fprintf(stderr, "allen: rig failed\n");
443 rig_stop(&r);
444 cx_close(&cx);
445 return 4;
446 }
447 rx_world_wait_quiescent(&r.w, 20000);
448 printf("ALLEN probe journal_records %llu goals_in_world %u goal_seq %llu assessment_goal_seq %llu\n",
449 (unsigned long long)cx.n, goals_in_world(&r), (unsigned long long)fld(&r, r.a.o.goal, 0),
450 (unsigned long long)fld(&r, r.a.o.assessment, 0));
451 rig_stop(&r);
452 cx_close(&cx);
453 return 0;
454}
455
456int main(int argc, char **argv)
457{
458 if (argc < 2) return usage();
459 if (!strcmp(argv[1], "make")) return v_make(argc, argv);
460 if (!strcmp(argv[1], "inspect") && argc == 3) return v_inspect(argv[2], 0);
461 if (!strcmp(argv[1], "digest") && argc == 3) return v_inspect(argv[2], 1);
462 if (!strcmp(argv[1], "lineage") && argc == 3) return v_lineage(argv[2]);
463 if (!strcmp(argv[1], "seed-journal") && (argc == 3 || argc == 4))
464 return v_seed(argv[2], argc == 4 ? strtoull(argv[3], NULL, 10) : 0);
465 if (!strcmp(argv[1], "publish") && argc >= 4) return v_publish(argc, argv);
466 if (!strcmp(argv[1], "probe") && argc == 3) return v_probe(argv[2]);
467 return usage();
468}