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 / core / context.c

232 lines · 8128 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/* context.c -- see context.h. No kernel calls, no libc. */
18#include "context.h"
19#include "../third_party/omega/rx_cortex.h"
20#include "../../argus/sha256.h"
21
22#if defined(CK_CTX_MUTANT_SKIP_BINDING) && !defined(CK_CTX_HOST_TEST)
23#error "CK_CTX_MUTANT_SKIP_BINDING is a host-only TEST mutant (make context-mutant)"
24#endif
25
26static const uint8_t k_magic[6] = { 'A', 'M', 'E', 'M', CK_MEM_VERSION, 0 };
27
28static int printable(uint8_t c) { return c >= 0x20 && c <= 0x7e; }
29
30static int has(const uint8_t *t, size_t n, const char *lit)
31{
32 size_t m = 0, i, j;
33 while (lit[m]) m++;
34 if (m == 0 || m > n) return 0;
35 for (i = 0; i + m <= n; i++) {
36 for (j = 0; j < m && t[i + j] == (uint8_t)lit[j]; j++) {}
37 if (j == m) return 1;
38 }
39 return 0;
40}
41
42int ck_ctx_text_safe(const uint8_t *t, size_t n)
43{
44 return !has(t, n, "<|") && !has(t, n, "|>") && !has(t, n, "End of memory") && !has(t, n, "Owner memory") &&
45 !has(t, n, "Task:");
46}
47
48enum ck_mem_form ck_mem_parse(const uint8_t *b, size_t nb, uint64_t tag, ck_mem_v1 *out)
49{
50 size_t i;
51 if (!b || nb < sizeof k_magic) return CK_MEM_LEGACY;
52 for (i = 0; i < sizeof k_magic; i++)
53 if (b[i] != k_magic[i]) return CK_MEM_LEGACY;
54 if (nb < CK_MEM_HDR_BYTES || tag == 0 || tag > CK_CTX_TEXT_MAX || CK_MEM_HDR_BYTES + tag > nb)
55 return CK_MEM_MALFORMED;
56 if (b[6] != CK_MEM_TYPE_PREF && b[6] != CK_MEM_TYPE_FACT) return CK_MEM_MALFORMED;
57 if (b[7] & ~CK_MEM_PERM_CONTEXT) return CK_MEM_MALFORMED;
58 for (i = 0; i < tag; i++)
59 if (!printable(b[CK_MEM_HDR_BYTES + i])) return CK_MEM_MALFORMED;
60 for (i = CK_MEM_HDR_BYTES + tag; i < nb; i++) /* padding of the last word must be zero */
61 if (b[i]) return CK_MEM_MALFORMED;
62 if (out) {
63 out->type = b[6];
64 out->perm = b[7];
65 for (i = 0; i < 32; i++) out->binding[i] = b[8 + i];
66 out->text = b + CK_MEM_HDR_BYTES;
67 out->text_len = (size_t)tag;
68 }
69 return CK_MEM_V1;
70}
71
72uint32_t ck_mem_encode(uint64_t *w, uint8_t type, uint8_t perm, const uint8_t binding[32], const char *text,
73 size_t n)
74{
75 uint8_t b[CK_MEM_MAX_WORDS * 8];
76 size_t i, nb;
77 if (n == 0 || n > CK_CTX_TEXT_MAX || !binding || !text) return 0;
78 for (i = 0; i < n; i++)
79 if (!printable((uint8_t)text[i])) return 0;
80 for (i = 0; i < sizeof b; i++) b[i] = 0;
81 for (i = 0; i < sizeof k_magic; i++) b[i] = k_magic[i];
82 b[6] = type;
83 b[7] = perm;
84 for (i = 0; i < 32; i++) b[8 + i] = binding[i];
85 for (i = 0; i < n; i++) b[CK_MEM_HDR_BYTES + i] = (uint8_t)text[i];
86 nb = (CK_MEM_HDR_BYTES + n + 7) / 8;
87 for (i = 0; i < nb; i++) {
88 uint64_t v = 0;
89 size_t k;
90 for (k = 0; k < 8; k++) v |= (uint64_t)b[8 * i + k] << (8 * k);
91 w[i] = v;
92 }
93 return (uint32_t)nb;
94}
95
96void ck_ctx_begin(ck_ctx *c, const uint8_t *agent)
97{
98 size_t i;
99 uint8_t *p = (uint8_t *)c;
100 for (i = 0; i < sizeof *c; i++) p[i] = 0;
101 if (agent) {
102 for (i = 0; i < 32; i++) c->agent[i] = agent[i];
103 c->have_agent = 1;
104 }
105}
106
107static int refuse(ck_ctx *c, int rc, uint64_t id)
108{
109 if (!c->refused) {
110 c->refused = rc;
111 c->bad_id = id;
112 }
113 return c->refused;
114}
115
116int ck_ctx_add(ck_ctx *c, const ck_ctx_rec *r)
117{
118 ck_mem_v1 m;
119 size_t i;
120 if (c->refused) return c->refused;
121 if (c->finished || !r || (c->seen && r->id <= c->last_id)) return refuse(c, CK_CTX_E_STATE, r ? r->id : 0);
122 if (!c->have_agent) return refuse(c, CK_CTX_E_SUBJECT, r->id);
123 c->seen++;
124 c->last_id = r->id;
125 if (!r->verified) return refuse(c, CK_CTX_E_UNVERIFIED, r->id);
126 if (r->cls != CX_CLAIM || r->kind != CX_K_CLAIM || r->subject != CK_CTX_OWNER_SUBJECT) {
127 c->other++;
128 return 0;
129 }
130 enum ck_mem_form f = ck_mem_parse(r->bytes, r->nbytes, r->tag, &m);
131 if (f == CK_MEM_LEGACY) {
132 c->legacy++;
133 return 0;
134 }
135 if (f == CK_MEM_MALFORMED) return refuse(c, CK_CTX_E_MALFORMED, r->id);
136#ifndef CK_CTX_MUTANT_SKIP_BINDING
137 for (i = 0; i < 32; i++)
138 if (m.binding[i] != c->agent[i]) return refuse(c, CK_CTX_E_FOREIGN, r->id);
139#endif
140 if (!(m.perm & CK_MEM_PERM_CONTEXT)) {
141 c->unpermitted++;
142 return 0;
143 }
144 if (!ck_ctx_text_safe(m.text, m.text_len)) {
145 c->unsafe++;
146 return 0;
147 }
148 c->eligible++;
149 if (c->kept == CK_CTX_MAX_RECORDS) c->omitted++; /* the oldest kept one is pushed out */
150 else c->kept++;
151 {
152 uint32_t slot = c->head;
153 c->keep[slot].id = r->id;
154 c->keep[slot].type = m.type;
155 c->keep[slot].len = (uint8_t)m.text_len;
156 for (i = 0; i < m.text_len; i++) c->keep[slot].text[i] = (char)m.text[i];
157 c->head = (slot + 1) % CK_CTX_MAX_RECORDS;
158 }
159 return 0;
160}
161
162static int put(ck_ctx *c, const char *s, size_t n)
163{
164 size_t i;
165 if (c->len + n > CK_CTX_PROMPT_MAX) return -1;
166 for (i = 0; i < n; i++) c->prompt[c->len++] = s[i];
167 return 0;
168}
169static int puts_(ck_ctx *c, const char *s)
170{
171 size_t n = 0;
172 while (s[n]) n++;
173 return put(c, s, n);
174}
175
176int ck_ctx_finish(ck_ctx *c, const char *task, size_t tn)
177{
178 size_t i;
179 if (c->refused) return c->refused;
180 if (c->finished) return refuse(c, CK_CTX_E_STATE, 0);
181 if (!c->have_agent) return refuse(c, CK_CTX_E_SUBJECT, 0);
182 if (!task || tn == 0 || tn > CK_CTX_TASK_MAX) return refuse(c, CK_CTX_E_TASK, 0);
183 for (i = 0; i < tn; i++)
184 if (!printable((uint8_t)task[i])) return refuse(c, CK_CTX_E_TASK, 0);
185 if (has((const uint8_t *)task, tn, "<|") || has((const uint8_t *)task, tn, "|>")) return refuse(c, CK_CTX_E_TASK, 0);
186 c->len = 0;
187 if (c->kept == 0) {
188 if (put(c, task, tn)) return refuse(c, CK_CTX_E_LIMIT, 0);
189 } else {
190 /* oldest kept first: the ring starts at head when full, else at 0 */
191 uint32_t k, start = c->kept == CK_CTX_MAX_RECORDS ? c->head : 0;
192 int bad = puts_(c, CK_CTX_HDR);
193 for (k = 0; k < c->kept && !bad; k++) {
194 uint32_t s = (start + k) % CK_CTX_MAX_RECORDS;
195 bad = puts_(c, c->keep[s].type == CK_MEM_TYPE_PREF ? CK_CTX_PREF : CK_CTX_FACT) ||
196 put(c, c->keep[s].text, c->keep[s].len) || put(c, "\n", 1);
197 }
198 if (!bad) bad = puts_(c, CK_CTX_END) || puts_(c, CK_CTX_TASK) || put(c, task, tn);
199 if (bad) {
200 c->len = 0;
201 return refuse(c, CK_CTX_E_LIMIT, 0);
202 }
203 }
204 c->prompt[c->len] = 0;
205 c->selected = c->kept;
206 {
207 static const char dom[] = "aienos.context.v1";
208 sha256_ctx h;
209 sha256_init(&h);
210 sha256_update(&h, (const uint8_t *)dom, sizeof dom); /* includes the 0x00 separator */
211 sha256_update(&h, c->agent, 32);
212 sha256_update(&h, (const uint8_t *)c->prompt, c->len);
213 sha256_final(&h, c->sha);
214 }
215 c->finished = 1;
216 return 0;
217}
218
219const char *ck_ctx_strerror(int rc)
220{
221 switch (rc) {
222 case CK_CTX_OK: return "ok";
223 case CK_CTX_E_SUBJECT: return "no provisioned subject";
224 case CK_CTX_E_UNVERIFIED: return "memory record not verified";
225 case CK_CTX_E_FOREIGN: return "foreign memory: record bound to another agent";
226 case CK_CTX_E_MALFORMED: return "malformed memory record";
227 case CK_CTX_E_TASK: return "task text out of bounds or unsafe";
228 case CK_CTX_E_LIMIT: return "assembled prompt over the context limit";
229 case CK_CTX_E_STATE: return "context assembly misuse";
230 }
231 return "?";
232}