interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / tests / pi5_agent_test.c

395 lines · 17346 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/* pi5_agent_test.c -- AIENOS PI5-6 final cut host test: the Pi agent stage (svc/pi5_agent.c, hooked by svc/store_boot.c
18 * ck_store_pi5_agent_stage, compiled with -DCK_PI5_UNLOCK -DCK_PI5_UNLOCK_CARD -DCK_PI5_AGENT exactly as "make pi5-agent"
19 * builds it) over the Pi card layout. A file stands in for the card; every "boot" reopens the file and starts from a cold C
20 * state. The inference unit is a STUB defined here (a real 770 MiB model cannot run on the host); the stage's own policy
21 * (order, golden checks, what gets committed, what is recovered) is the real code under test.
22 *
23 * Proves: boot 1 (right secret, model present) generates, closes the session, commits ONE sealed reply object and reads it
24 * back; boot 2 (same secret, no model) recovers the same reply with the same Store generation and the same object digest
25 * without touching the model; boot 2 with a model present still does not generate; the wrong secret recovers nothing,
26 * never reaches the model (zero calls) and leaves the card byte-identical; a reply that is not the golden one (wrong ids,
27 * wrong chat-template head, wrong text, wrong model digest, refused or cancelled prompt) commits nothing; a stored reply that
28 * is not golden is reported FAIL on recovery; no reply and no model is NOMODEL; a lifecycle reply (no trailer) and another
29 * prompt are ignored; the reply object format is the lifecycle's, byte for byte, plus the trailer.
30 * Mutants (Makefile-less, see stage.mk pi5-agent-mutants) must make this test FAIL. Hosted test code only. */
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35#include "ck.h"
36#include "ck_host.h"
37#include "disk_file.h"
38#include "disk_layout.h"
39#include "disk_part.h"
40#include "gpt_write.h"
41#include "../../core/pi5_marker.h"
42#include "../../core/reply_obj.h"
43#include "m5.h"
44#include "store_boot.h"
45#include "../../core/unlock_req.h"
46#include "../../svc/pi5_agent.h"
47
48#if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_AGENT)
49#error "pi5_agent_test.c must be built with -DCK_PI5_UNLOCK -DCK_PI5_AGENT (the image's policy)"
50#endif
51
52static int checks, failures;
53#define CHECK(c) \
54 do { \
55 checks++; \
56 if (!(c)) { \
57 failures++; \
58 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \
59 } \
60 } while (0)
61
62extern int ck_host_entropy_refuse;
63int ck_pi5_unlock_secret(uint8_t out[CK_SB_SECRET_LEN], const char **source)
64{
65 (void)out;
66 (void)source;
67 return -1;
68}
69
70/* ---- the inference unit stub (core/infer.c in the image) ---- */
71static struct {
72 int model; /* MODEL.MAP present and the digest matched: ck_infer_run leaves a resident session */
73 int resident;
74 int prompt_rc; /* ck_infer_prompt result */
75 int cancelled;
76 uint32_t ids[CK_INFER_REPLY_IDS];
77 unsigned nids;
78 char text[64];
79 uint32_t head[4];
80 uint8_t sha[32];
81 int n_run, n_prompt, n_close;
82 char last_prompt[96];
83} G;
84
85static const uint32_t gold_ids[8] = {791, 6864, 315, 9822, 374, 12366, 13, 128009};
86static const uint32_t gold_head[4] = {128000, 128006, 882, 128007};
87
88static void stub_good(int model)
89{
90 memset(&G, 0, sizeof G);
91 G.model = model;
92 memcpy(G.ids, gold_ids, sizeof gold_ids);
93 G.nids = 8;
94 strcpy(G.text, CK_AGENT_GOLDEN_TEXT);
95 memcpy(G.head, gold_head, sizeof gold_head);
96 for (int i = 0; i < 32; i++) {
97 unsigned v;
98 if (sscanf(&CK_AGENT_GOLDEN_MODEL_SHA256[2 * i], "%2x", &v) != 1) {
99 fprintf(stderr, "pi5_agent_test: CK_AGENT_GOLDEN_MODEL_SHA256 is not 64 hex digits (byte %d)\n", i);
100 exit(2);
101 }
102 G.sha[i] = (uint8_t)v;
103 }
104}
105
106void ck_infer_run(void)
107{
108 G.n_run++;
109 G.resident = G.model;
110}
111int ck_infer_resident(void) { return G.resident; }
112const uint8_t *ck_infer_model_sha256(void) { return G.resident ? G.sha : 0; }
113int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), struct ck_infer_reply *r)
114{
115 (void)cancel_poll;
116 G.n_prompt++;
117 snprintf(G.last_prompt, sizeof G.last_prompt, "%.*s", (int)len, text);
118 memset(r, 0, sizeof *r);
119 if (G.prompt_rc) return G.prompt_rc;
120 memcpy(r->ids, G.ids, sizeof G.ids);
121 r->produced = G.nids;
122 r->prompt_ids = 17;
123 r->steps = 17 + G.nids;
124 r->cancelled = G.cancelled;
125 r->us = 1234;
126 r->text_len = (uint32_t)strlen(G.text);
127 memcpy(r->text, G.text, r->text_len);
128 memcpy(r->prompt_head, G.head, sizeof G.head);
129 return 0;
130}
131void ck_infer_resident_close(void)
132{
133 G.n_close++;
134 G.resident = 0;
135}
136
137#define BLOCKS 262144ull
138#define PART_FIRST 8192u
139#define PART_SECS 8192u
140static char g_path[256];
141
142/* Optional forged object committed through the open Store before the agent stage (a tampered or foreign R1 object). */
143static const uint8_t *g_forge;
144static size_t g_forge_len;
145
146/* One boot: open the card file, run the Store stage exactly as ck_stage_store does (unlock policy, then the agent hook),
147 * close the card. Output captured. */
148static int boot(const uint8_t *secret, ck_store_report *r, int *agent_rc, char **cap)
149{
150 disk_file f;
151 disk_dev d;
152 ck_part p, sp;
153 ck_gpt_info gi;
154 if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999;
155 if (ck_gpt_find_aienos(&d, &p, &gi) || ck_pi5_store_view(&p, &sp)) { disk_file_close(&f); return -9998; }
156 ck_host_quiet = 1;
157 ck_host_capture_start();
158 int rc = ck_store_pi5_unlock_run(&sp.dev, secret, "pi5-agent-test", r);
159 if (g_forge && r->verdict == CK_SB_COMMITTED) {
160 ss_object o = {CK_REPLY_OBJ_KIND, 1, g_forge, g_forge_len};
161 CHECK(ss_transact(store_boot_store(), &o, 1, 0, 0, 0) == 0);
162 }
163 *agent_rc = ck_store_pi5_agent_stage(r);
164 *cap = strdup(ck_host_capture_text());
165 ck_host_capture_stop();
166 ck_host_quiet = 0;
167 disk_file_close(&f);
168 return rc;
169}
170
171static uint8_t *slurp(size_t *n)
172{
173 FILE *fp = fopen(g_path, "rb");
174 CHECK(fp != 0);
175 if (!fp) { *n = 0; return 0; }
176 fseek(fp, 0, SEEK_END);
177 *n = (size_t)ftell(fp);
178 rewind(fp);
179 uint8_t *m = malloc(*n);
180 CHECK(m && fread(m, 1, *n, fp) == *n);
181 fclose(fp);
182 return m;
183}
184
185/* The whole "agent: reply ..." line of a capture ("" when absent). */
186static void reply_line(const char *cap, char *out, size_t cap_out)
187{
188 const char *p = strstr(cap, "agent: reply ");
189 size_t n = 0;
190 out[0] = 0;
191 if (!p) return;
192 while (p[n] && p[n] != '\n' && n + 1 < cap_out) { out[n] = p[n]; n++; }
193 out[n] = 0;
194}
195
196static unsigned long long num_after(const char *s, const char *after, const char *key)
197{
198 const char *p = strstr(s, after);
199 if (!p) return 0;
200 p = strstr(p, key);
201 return p ? strtoull(p + strlen(key), 0, 10) : 0;
202}
203
204static void blank_card(void)
205{
206 gw_part gp[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0x5a}, {ck_aienos_part_type, PART_FIRST, PART_FIRST + PART_SECS - 1, "AIENOS", 0}};
207 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0);
208}
209
210int main(void)
211{
212 snprintf(g_path, sizeof g_path, "/tmp/aienos-pi5-agent-test-%d.img", (int)getpid());
213 uint8_t S[CK_SB_SECRET_LEN], W[CK_SB_SECRET_LEN];
214 for (int i = 0; i < 32; i++) S[i] = (uint8_t)(i * 7 + 5);
215 memcpy(W, S, 32);
216 W[31] ^= 0x80;
217 ck_store_report r;
218 char *c1, *c2, *c3, *c4;
219 char l1[1024], l2[1024];
220 size_t n0, n1;
221 uint8_t *b, *a;
222 int arc;
223
224 /* 0. The shared reply object: the lifecycle format byte for byte, plus the trailer only when asked. */
225 {
226 uint8_t o[CK_REPLY_OBJ_TRAILER_MAX + 128];
227 uint32_t ids[3] = {791, 0, 128009};
228 size_t n = ck_reply_obj_build(o, "Q", 1, (const uint8_t *)"A\nB", 3, 0, 0, 0);
229 static const char lit[] = "aienos.reply.v1\nprompt: Q\nreply: A\nB\n";
230 CHECK(n == sizeof lit - 1 && memcmp(o, lit, n) == 0);
231 n = ck_reply_obj_build(o, "Q", 1, (const uint8_t *)"A", 1, CK_AGENT_GOLDEN_MODEL_SHA256, ids, 3);
232 static const char lit2[] = "aienos.reply.v1\nprompt: Q\nreply: A\nmodel: " CK_AGENT_GOLDEN_MODEL_SHA256 "\nids: 791 0 128009\n";
233 CHECK(n == sizeof lit2 - 1 && memcmp(o, lit2, n) == 0);
234 CHECK(CK_REPLY_OBJ_KIND == 0x5231u);
235 }
236
237 /* 1. Boot 1 (G1): blank card, model present: generate, close, commit, read back. */
238 blank_card();
239 stub_good(1);
240 CHECK(boot(S, &r, &arc, &c1) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && arc == 0);
241 CHECK(G.n_run == 1 && G.n_prompt == 1 && G.n_close == 1 && !G.resident);
242 CHECK(strcmp(G.last_prompt, CK_AGENT_PROMPT) == 0);
243 CHECK(strstr(c1, "agent: generated tokens=8 prompt_ids=17"));
244 CHECK(strstr(c1, "prompt_head=128000,128006,882,128007"));
245 CHECK(strstr(c1, "ids=791 6864 315 9822 374 12366 13 128009 text=\"The capital of France is Paris.\""));
246 CHECK(strstr(c1, "model_sha256=" CK_AGENT_GOLDEN_MODEL_SHA256));
247 CHECK(strstr(c1, "agent: committed store_gen="));
248 CHECK(strstr(c1, "AIENOS_PI5_AGENT: GENERATED PASS"));
249 CHECK(strstr(c1, "agent: reply store_gen="));
250 unsigned long long gen1 = num_after(c1, "agent: reply", "store_gen=");
251 CHECK(gen1 > 0);
252 reply_line(c1, l1, sizeof l1);
253 CHECK(strlen(l1) > 100);
254 CHECK(strstr(l1, "object_sha256="));
255
256 /* 2. Boot 2 (G2): same secret, NO model: the same reply, same generation, same digest, model never touched, nothing new. */
257 stub_good(0);
258 b = slurp(&n0);
259 CHECK(boot(S, &r, &arc, &c2) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0 && arc == 0);
260 CHECK(G.n_run == 0 && G.n_prompt == 0 && G.n_close == 0);
261 CHECK(strstr(c2, "AIENOS_PI5_AGENT: RECOVERED PASS"));
262 CHECK(!strstr(c2, "agent: generated") && !strstr(c2, "agent: committed"));
263 reply_line(c2, l2, sizeof l2);
264 CHECK(strcmp(l1, l2) == 0); /* boot 2 prints exactly boot 1's reply line, store generation and object digest included */
265 a = slurp(&n1);
266 CHECK(n0 == n1);
267 free(a);
268 free(b);
269 /* 2b. Same, with the model present on the card: the Store already holds the reply, so nothing is generated. */
270 stub_good(1);
271 CHECK(boot(S, &r, &arc, &c3) == 0 && arc == 0);
272 CHECK(G.n_run == 0 && G.n_prompt == 0);
273 reply_line(c3, l2, sizeof l2);
274 CHECK(strcmp(l1, l2) == 0 && strstr(c3, "RECOVERED PASS"));
275 free(c3);
276
277 /* 3. Negative control (G3): the wrong secret opens nothing, recovers nothing, never reaches the model, writes nothing. */
278 stub_good(1);
279 b = slurp(&n0);
280 CHECK(boot(W, &r, &arc, &c3) == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED && arc == 0);
281 a = slurp(&n1);
282 CHECK(n0 == n1 && memcmp(b, a, n0) == 0);
283 CHECK(G.n_run == 0 && G.n_prompt == 0 && G.n_close == 0);
284 CHECK(strstr(c3, "agent: NOTHING RECOVERED") && strstr(c3, "AIENOS_PI5_AGENT: NOSTORE"));
285 CHECK(!strstr(c3, "agent: reply") && !strstr(c3, "Paris") && !strstr(c3, "RECOVERED PASS"));
286 free(a);
287 free(b);
288 free(c3);
289 /* and the right secret afterwards recovers the same reply again */
290 stub_good(0);
291 CHECK(boot(S, &r, &arc, &c3) == 0 && arc == 0);
292 reply_line(c3, l2, sizeof l2);
293 CHECK(strcmp(l1, l2) == 0);
294 free(c3);
295 free(c1);
296 free(c2);
297
298 /* 4. Replies that are not golden commit nothing (each on a fresh card; then a no-model boot shows nothing is stored). */
299 {
300 enum { WRONG_ID, WRONG_HEAD, WRONG_TEXT, WRONG_MODEL, PROMPT_REFUSED, CANCELLED, MODEL_ABSENT_AFTER_RUN, TOO_MANY, N_CASES };
301 for (int k = 0; k < N_CASES; k++) {
302 blank_card();
303 stub_good(1);
304 switch (k) {
305 case WRONG_ID: G.ids[3] ^= 1; break;
306 case WRONG_HEAD: G.head[2] = 883; break;
307 case WRONG_TEXT: strcpy(G.text, "The capital of France is Lyon."); break;
308 case WRONG_MODEL: G.sha[0] ^= 1; break;
309 case PROMPT_REFUSED: G.prompt_rc = -15; break;
310 case CANCELLED: G.cancelled = 1; break;
311 case MODEL_ABSENT_AFTER_RUN: G.model = 0; break;
312 case TOO_MANY: G.nids = 9; G.ids[8] = 128009; break;
313 }
314 CHECK(boot(S, &r, &arc, &c4) == 0 && r.verdict == CK_SB_COMMITTED && arc != 0);
315 CHECK(strstr(c4, "AIENOS_PI5_AGENT: GENERATED FAIL") || strstr(c4, "AIENOS_PI5_AGENT: NOMODEL"));
316 CHECK(!strstr(c4, "agent: committed") && !strstr(c4, "agent: reply"));
317 CHECK(G.n_close == (k == MODEL_ABSENT_AFTER_RUN ? 0 : 1) && !G.resident);
318 free(c4);
319 stub_good(0);
320 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOTHING GENERATED") && strstr(c4, "AIENOS_PI5_AGENT: NOMODEL"));
321 CHECK(G.n_prompt == 0 && !strstr(c4, "agent: reply"));
322 free(c4);
323 }
324 }
325
326 /* 5. No reply and no model: NOMODEL, nothing written, the model path was tried but not the prompt. */
327 blank_card();
328 stub_good(0);
329 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && G.n_run == 1 && G.n_prompt == 0);
330 CHECK(strstr(c4, "AIENOS_PI5_AGENT: NOMODEL"));
331 free(c4);
332
333 /* 6. Foreign R1 objects are ignored; a stored agent reply that is not golden is reported FAIL on recovery. */
334 {
335 uint8_t o[CK_REPLY_OBJ_TRAILER_MAX + 512];
336 uint32_t bad[8];
337 memcpy(bad, gold_ids, sizeof bad);
338 blank_card();
339 /* 6a. a lifecycle reply for the SAME prompt (no trailer) and an agent-format reply for ANOTHER prompt */
340 g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)"Paris.", 6, 0, 0, 0);
341 g_forge = o;
342 stub_good(0);
343 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOMODEL")); /* ignored: no agent reply yet */
344 free(c4);
345 g_forge_len = ck_reply_obj_build(o, "Say hello.", 10, (const uint8_t *)"Hi", 2, CK_AGENT_GOLDEN_MODEL_SHA256, gold_ids, 8);
346 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOMODEL"));
347 free(c4);
348 g_forge = 0;
349 stub_good(1);
350 CHECK(boot(S, &r, &arc, &c4) == 0 && arc == 0 && strstr(c4, "GENERATED PASS")); /* still generates beside them */
351 reply_line(c4, l1, sizeof l1);
352 free(c4);
353 stub_good(0);
354 CHECK(boot(S, &r, &arc, &c4) == 0 && arc == 0 && strstr(c4, "RECOVERED PASS"));
355 reply_line(c4, l2, sizeof l2);
356 CHECK(strcmp(l1, l2) == 0);
357 free(c4);
358 /* 6b. a well-formed agent object whose token list is not golden (as if written by a broken generator) */
359 blank_card();
360 bad[2] = 316;
361 g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)CK_AGENT_GOLDEN_TEXT,
362 strlen(CK_AGENT_GOLDEN_TEXT), CK_AGENT_GOLDEN_MODEL_SHA256, bad, 8);
363 g_forge = o;
364 stub_good(1);
365 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0);
366 CHECK(strstr(c4, "AIENOS_PI5_AGENT: RECOVERED FAIL") && strstr(c4, "not the golden reply"));
367 CHECK(G.n_run == 0 && G.n_prompt == 0); /* a bad stored reply is reported, never silently regenerated over */
368 free(c4);
369 /* 6c. a stored reply carrying another model digest, and a text with newlines and trailer-looking lines inside */
370 blank_card();
371 g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT),
372 (const uint8_t *)"The capital of France is Paris.\nids: 1 2\nmodel: x", 49, CK_AGENT_GOLDEN_MODEL_SHA256, gold_ids, 8);
373 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "RECOVERED FAIL"));
374 CHECK(strstr(c4, "ids=791 6864 315 9822 374 12366 13 128009")); /* the trailer is read from the END */
375 free(c4);
376 blank_card();
377 {
378 char other[65];
379 memcpy(other, CK_AGENT_GOLDEN_MODEL_SHA256, 65);
380 other[10] = other[10] == '0' ? '1' : '0';
381 g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)CK_AGENT_GOLDEN_TEXT,
382 strlen(CK_AGENT_GOLDEN_TEXT), other, gold_ids, 8);
383 CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "RECOVERED FAIL"));
384 free(c4);
385 }
386 g_forge = 0;
387 }
388
389 memset(S, 0, sizeof S);
390 memset(W, 0, sizeof W);
391 unlink(g_path);
392 printf("pi5_agent_test: %d checks, %d failures\n", checks, failures);
393 printf("CK_PI5_AGENT_HOST: %s\n", failures ? "FAIL" : "PASS");
394 return failures ? 1 : 0;
395}