Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/svc/pi5_agent.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / svc / pi5_agent.c` 329 lines ยท 12783 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.c -- see pi5_agent.h. */ 18 #include "pi5_agent.h" 19 #include 20 #include "ck.h" 21 #include "sha256.h" 22 #include "../core/reply_obj.h" 23 24 static const uint32_t golden_ids[CK_AGENT_GOLDEN_NIDS] = {791, 6864, 315, 9822, 374, 12366, 13, 128009}; 25 static const uint32_t golden_head[4] = {128000, 128006, 882, 128007}; 26 27 /* header + prompt + separator + reply (<= CK_INFER_REPLY_TEXT) + terminator + model/ids trailer */ 28 #define CK_AGENT_OBJ_MAX (24 + 48 + 8 + CK_INFER_REPLY_TEXT + 1 + CK_REPLY_OBJ_TRAILER_MAX) 29 static uint8_t g_body[SS_MAX_PLAINTEXT]; 30 31 static void hex(char *out, const uint8_t *b, unsigned n) 32 { 33 static const char d[] = "`0123456789abcdef`"; 34 for (unsigned i = 0; i < n; i++) { 35 out[2 * i] = d[b[i] >> 4]; 36 out[2 * i + 1] = d[b[i] & 15]; 37 } 38 out[2 * n] = 0; 39 } 40 41 static size_t slen(const char *s) 42 { 43 size_t n = 0; 44 while (s[n]) n++; 45 return n; 46 } 47 48 static int same(const uint8_t *a, const char *b, size_t n) 49 { 50 for (size_t i = 0; i < n; i++) 51 if (a[i] != (uint8_t)b[i]) return 0; 52 return 1; 53 } 54 55 static void show(char *out, const uint8_t *b, size_t n) 56 { 57 for (size_t i = 0; i < n; i++) out[i] = (b[i] >= 0x20 && b[i] <= 0x7e && b[i] != '"') ? (char)b[i] : '?'; 58 out[n] = 0; 59 } 60 61 /* A parsed agent object. text points into the body; model is 64 hex chars. */ 62 struct parsed { 63 const uint8_t *text; 64 size_t text_len; 65 char model[65]; 66 uint32_t ids[CK_REPLY_OBJ_MAX_IDS]; 67 unsigned nids; 68 }; 69 70 /* Parse "\nmodel: <64 hex>\nids: \n" from the END (the reply may 71 * itself contain newlines). Returns 0 on a well-formed object for the fixed prompt, else a reason code. */ 72 static int parse(const uint8_t *b, size_t len, struct parsed *p) 73 { 74 const size_t hl = sizeof CK_REPLY_OBJ_HDR - 1, pl = slen(CK_AGENT_PROMPT), sl = sizeof CK_REPLY_OBJ_SEP - 1; 75 const size_t ml = sizeof CK_REPLY_OBJ_MODEL - 1, il = sizeof CK_REPLY_OBJ_IDS - 1; 76 const size_t body0 = hl + pl + sl; /* first byte of the reply text */ 77 size_t q, i, e; 78 unsigned n = 0; 79 if (len < body0 + 1 + ml + 64 + il + 2 || !same(b, CK_REPLY_OBJ_HDR, hl) || !same(b + hl, CK_AGENT_PROMPT, pl) || 80 !same(b + hl + pl, CK_REPLY_OBJ_SEP, sl)) 81 return 1; 82 if (b[len - 1] != '\n') return 2; 83 /* the last "\nids: " (CK_REPLY_OBJ_IDS starts with the newline) */ 84 for (q = len - il;; q--) { 85 if (same(b + q, CK_REPLY_OBJ_IDS, il)) break; 86 if (q == 0) return 3; 87 } 88 /* "\nmodel: " + 64 hex ends exactly at q; that newline is the reply's terminator */ 89 if (q < body0 + 1 + ml + 64) return 4; 90 if (!same(b + q - 64 - ml, CK_REPLY_OBJ_MODEL, ml) || b[q - 64 - ml - 1] != '\n') return 5; 91 for (e = 0; e < 64; e++) { 92 uint8_t c = b[q - 64 + e]; 93 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 6; 94 p->model[e] = (char)c; 95 } 96 p->model[64] = 0; 97 p->text = b + body0; 98 p->text_len = (q - 64 - ml - 1) - body0; 99 /* ids: digits separated by single spaces, ended by the final newline */ 100 for (i = q + il; i < len - 1;) { 101 uint64_t v = 0; 102 size_t d = 0; 103 while (i < len - 1 && b[i] >= '0' && b[i] <= '9') { 104 v = v * 10 + (b[i] - '0'); 105 if (v > 0xffffffffull) return 7; 106 i++, d++; 107 } 108 if (!d || n >= CK_REPLY_OBJ_MAX_IDS) return 8; 109 p->ids[n++] = (uint32_t)v; 110 if (i < len - 1) { 111 if (b[i] != ' ') return 9; 112 i++; 113 if (i >= len - 1) return 10; /* trailing space */ 114 } 115 } 116 p->nids = n; 117 return n ? 0 : 11; 118 } 119 120 /* The golden checks: the exact llama.cpp token list, the golden text, the golden model digest. */ 121 static int golden_ok(const uint32_t *ids, unsigned nids, const uint8_t *text, size_t tlen, const char *model_hex) 122 { 123 unsigned i; 124 if (nids != CK_AGENT_GOLDEN_NIDS || tlen != slen(CK_AGENT_GOLDEN_TEXT) || !same(text, CK_AGENT_GOLDEN_TEXT, tlen)) return 0; 125 for (i = 0; i < nids; i++) 126 if (ids[i] != golden_ids[i]) return 0; 127 for (i = 0; i < 64; i++) 128 if (model_hex[i] != CK_AGENT_GOLDEN_MODEL_SHA256[i]) return 0; 129 return 1; 130 } 131 132 /* Newest R1 object of the Store that starts with the fixed prompt's header. 0: found (body, len, gen filled); 133 * 1: none; <0: an R1 object could not be read (do not guess: refuse). */ 134 static int find_reply(ss_store *s, size_t *len, uint64_t *gen) 135 { 136 const size_t pre = (sizeof CK_REPLY_OBJ_HDR - 1) + slen(CK_AGENT_PROMPT); 137 int best = -1; 138 uint64_t best_gen = 0, best_seq = 0; 139 uint32_t k; 140 for (k = 0; k < s->nclaims; k++) { 141 const ss_claim *c = &s->ws->claims[k]; 142 static uint8_t scratch[SS_MAX_PLAINTEXT]; 143 struct parsed probe; 144 size_t l = 0; 145 uint16_t kind = 0; 146 int rc; 147 if (c->c.obj.object_kind != CK_REPLY_OBJ_KIND) continue; 148 rc = ss_read(s, c->sid, scratch, sizeof scratch, &l, &kind); 149 if (rc) { 150 ck_printf("agent: REFUSED a reply object could not be read rc=%d store_gen=%llu\n", rc, 151 (unsigned long long)c->c.store_generation); 152 return -1; 153 } 154 /* An agent reply is an R1 object for the fixed prompt WITH a well-formed model/ids trailer. A lifecycle reply 155 * (QEMU service image, no trailer), another prompt or a damaged trailer is not ours and is left alone. */ 156 if (kind != CK_REPLY_OBJ_KIND || l < pre || !same(scratch, CK_REPLY_OBJ_HDR, sizeof CK_REPLY_OBJ_HDR - 1) || 157 !same(scratch + sizeof CK_REPLY_OBJ_HDR - 1, CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT)) || parse(scratch, l, &probe)) 158 continue; 159 if (best < 0 || c->c.store_generation > best_gen || (c->c.store_generation == best_gen && c->c.object_sequence > best_seq)) { 160 best = (int)k; 161 best_gen = c->c.store_generation; 162 best_seq = c->c.object_sequence; 163 for (size_t i = 0; i < l; i++) g_body[i] = scratch[i]; 164 *len = l; 165 } 166 } 167 if (best < 0) return 1; 168 *gen = best_gen; 169 return 0; 170 } 171 172 /* Parse, check and print the reply held in g_body (len bytes) at generation gen. */ 173 static int report(const char *how, size_t len, uint64_t gen, struct ck_agent_result *out) 174 { 175 struct parsed p; 176 uint8_t dg[SHA256_DIGEST_SIZE]; 177 char oh[65], shown[CK_INFER_REPLY_TEXT + 1], idl[CK_REPLY_OBJ_MAX_IDS * 11 + 1]; 178 unsigned i; 179 size_t at = 0; 180 int pr = parse(g_body, len, &p); 181 if (pr) { 182 ck_printf("agent: %s FAIL the stored reply object is malformed (reason %d, %u bytes, store_gen=%llu)\n", how, pr, 183 (unsigned)len, (unsigned long long)gen); 184 return -1; 185 } 186 sha256_hash(g_body, len, dg); 187 hex(oh, dg, SHA256_DIGEST_SIZE); 188 show(shown, p.text, p.text_len > CK_INFER_REPLY_TEXT ? CK_INFER_REPLY_TEXT : p.text_len); 189 for (i = 0; i < p.nids; i++) { 190 uint32_t v = p.ids[i]; 191 char d[10]; 192 unsigned nd = 0; 193 if (i) idl[at++] = ' '; 194 do { d[nd++] = (char)('0' + v % 10); v /= 10; } while (v); 195 while (nd) idl[at++] = d[--nd]; 196 } 197 idl[at] = 0; 198 ck_printf("agent: reply store_gen=%llu object_sha256=%s model_sha256=%s tokens=%u ids=%s text=\"%s\"\n", 199 (unsigned long long)gen, oh, p.model, p.nids, idl, shown); 200 out->store_gen = gen; 201 for (i = 0; i < 65; i++) out->object_sha256[i] = oh[i]; 202 #ifdef CK_AGENT_MUTANT_RECOVER_NO_CHECK 203 /* TEST-ONLY mutant: a recovered reply is accepted whatever it says. */ 204 return 0; 205 #else 206 if (!golden_ok(p.ids, p.nids, p.text, p.text_len, p.model)) { 207 ck_printf("agent: %s FAIL the stored reply is not the golden reply (tokens, text or model digest differ)\n", how); 208 return -1; 209 } 210 return 0; 211 #endif 212 } 213 214 static int generate(ss_store *s, struct ck_agent_result *out) 215 { 216 static struct ck_infer_reply r; 217 static uint8_t obj[CK_AGENT_OBJ_MAX]; 218 char mh[65]; 219 const uint8_t *msha; 220 size_t n, len = 0; 221 uint64_t gen = 0; 222 uint8_t sid[1][32]; 223 unsigned i = 0; 224 int rc, ok; 225 (void)i; 226 ck_infer_run(); 227 if (!ck_infer_resident()) { 228 out->mode = CK_AGENT_MODE_NOMODEL; 229 ck_puts("agent: NOTHING GENERATED: this Store holds no agent reply yet and no model is resident (MODEL.MAP absent or " 230 "refused, or the model digest did not match); nothing written\n"); 231 return -1; 232 } 233 msha = ck_infer_model_sha256(); 234 if (!msha) { 235 ck_infer_resident_close(); 236 ck_puts("agent: REFUSED the resident model has no digest; nothing written\n"); 237 return -1; 238 } 239 hex(mh, msha, SHA256_DIGEST_SIZE); 240 rc = ck_infer_prompt(CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT), 0, &r); 241 ck_infer_resident_close(); /* every frame back before the commit */ 242 if (rc < 0 || r.cancelled) { 243 ck_printf("agent: REFUSED the model refused the prompt rc=%d cancelled=%d; nothing written\n", rc, r.cancelled); 244 return -1; 245 } 246 ck_printf("agent: generated tokens=%u prompt_ids=%u steps=%u us=%llu prompt_head=%u,%u,%u,%u\n", r.produced, r.prompt_ids, 247 r.steps, (unsigned long long)r.us, r.prompt_head[0], r.prompt_head[1], r.prompt_head[2], r.prompt_head[3]); 248 ok = r.produced <= CK_AGENT_MAX_NEW && r.produced <= CK_INFER_REPLY_IDS; 249 #ifdef CK_AGENT_MUTANT_NO_PROMPT_CHECK 250 (void)golden_head; /* TEST-ONLY mutant: the chat-template head is not checked */ 251 #else 252 for (i = 0; i < 4; i++) ok = ok && r.prompt_head[i] == golden_head[i]; 253 #endif 254 #ifndef CK_AGENT_MUTANT_NO_GOLDEN_CHECK 255 ok = ok && golden_ok(r.ids, r.produced, r.text, r.text_len, mh); 256 #endif 257 if (!ok) { 258 ck_puts("agent: REFUSED the reply is not the golden reply (prompt head, tokens, text or model digest); nothing written\n"); 259 return -1; 260 } 261 n = ck_reply_obj_build(obj, CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT), r.text, r.text_len, mh, r.ids, r.produced); 262 { 263 ss_object o = {CK_REPLY_OBJ_KIND, 1, obj, n}; 264 #ifdef CK_AGENT_MUTANT_NO_COMMIT 265 /* TEST-ONLY mutant: skip the commit but still report success. */ 266 rc = 0; 267 (void)sid; 268 (void)o; 269 #else 270 rc = ss_transact(s, &o, 1, 0, 0, sid); 271 #endif 272 } 273 if (rc) { 274 ck_printf("agent: REFUSED the Store did not commit the reply rc=%d; nothing written\n", rc); 275 return -1; 276 } 277 /* Read it back from the sealed Store, the way a cold boot will. */ 278 rc = find_reply(s, &len, &gen); 279 if (rc) { 280 ck_puts("agent: FAIL the committed reply is not in the Store\n"); 281 return -1; 282 } 283 if (report("readback", len, gen, out)) return -1; 284 out->ok = 1; 285 ck_printf("agent: committed store_gen=%llu\n", (unsigned long long)gen); 286 return 0; 287 } 288 289 int ck_pi5_agent_run(ss_store *s, struct ck_agent_result *out) 290 { 291 size_t len = 0; 292 uint64_t gen = 0; 293 int rc; 294 *out = (struct ck_agent_result){0}; 295 #ifdef CK_AGENT_MUTANT_MODEL_FIRST 296 ck_infer_run(); /* TEST-ONLY mutant: the model is read and hashed before the Store is checked */ 297 #endif 298 if (!s) { 299 out->mode = CK_AGENT_MODE_NOSTORE; 300 ck_puts("agent: NOTHING RECOVERED: the Store is not open (recovery secret missing or rejected, or the disk was " 301 "refused); the model was not read or hashed, nothing was generated, nothing was written\n"); 302 ck_puts("AIENOS_PI5_AGENT: NOSTORE\n"); 303 return 0; 304 } 305 #ifdef CK_AGENT_MUTANT_RECOVER_BLIND 306 rc = 1; /* TEST-ONLY mutant: recovery never looks at the Store */ 307 #else 308 rc = find_reply(s, &len, &gen); 309 #endif 310 if (rc < 0) { 311 ck_puts("AIENOS_PI5_AGENT: FAIL\n"); 312 return -1; 313 } 314 if (rc == 0) { 315 out->mode = CK_AGENT_MODE_RECOVER; 316 rc = report("recovered", len, gen, out); 317 out->ok = rc == 0; 318 ck_printf("AIENOS_PI5_AGENT: RECOVERED %s\n", rc ? "FAIL" : "PASS"); 319 return rc; 320 } 321 out->mode = CK_AGENT_MODE_GENERATE; 322 rc = generate(s, out); 323 if (out->mode == CK_AGENT_MODE_NOMODEL) { 324 ck_puts("AIENOS_PI5_AGENT: NOMODEL\n"); 325 } else { 326 ck_printf("AIENOS_PI5_AGENT: GENERATED %s\n", rc ? "FAIL" : "PASS"); 327 } 328 return rc; 329 }