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 / svc / continuity_subject.c

638 lines · 22585 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/* continuity_subject.c -- ALLEN subject state (kind 24): codec, editing,
18 * resolve and commit. See continuity_subject.h for the format and rules. */
19#include "continuity_subject.h"
20
21#include "sha256.h"
22#include "store_v1.h"
23
24static const uint8_t MAGIC_SUBJECT[8] = {'A', 'I', 'E', 'N', 'S', 'U', 'B', 'J'};
25static const char INTENT_DOMAIN[] = "AIENOS_INTENT_v0:";
26
27static int fail(const char **why, int cls, const char *w)
28{
29 if (why)
30 *why = w;
31 return cls;
32}
33
34/* ---- intent identity ---- */
35void cs_intent_id(const uint8_t agent[32], uint32_t kind, uint64_t since, const uint64_t payload[2],
36 uint8_t out[32])
37{
38 sha256_ctx c;
39 uint8_t b[8];
40 sha256_init(&c);
41 sha256_update(&c, (const uint8_t *)INTENT_DOMAIN, sizeof INTENT_DOMAIN - 1);
42 sha256_update(&c, agent, 32);
43 sv1_put32(b, kind);
44 sha256_update(&c, b, 4);
45 sv1_put64(b, since);
46 sha256_update(&c, b, 8);
47 sv1_put64(b, payload[0]);
48 sha256_update(&c, b, 8);
49 sv1_put64(b, payload[1]);
50 sha256_update(&c, b, 8);
51 sha256_final(&c, out);
52}
53
54/* ---- validation (every rule the decoder enforces, usable before encode) ---- */
55static int state_ok(uint8_t s)
56{
57 return s == CS_ACTIVE || s == CS_INACTIVE || s == CS_SUPERSEDED;
58}
59
60int cs_subject_validate(const struct cs_subject *s, const char **why)
61{
62 if (!s)
63 return fail(why, CC_E_ARG, "null argument");
64 if (sv1_all_zero(s->root, 32))
65 return fail(why, CC_E_CORRUPT, "zero root id");
66 if (sv1_all_zero(s->agent, 32))
67 return fail(why, CC_E_CORRUPT, "zero agent id");
68 if (s->sequence == 0)
69 return fail(why, CC_E_CORRUPT, "zero sequence");
70 if (s->sequence == 1 && !sv1_all_zero(s->previous, 32))
71 return fail(why, CC_E_CORRUPT, "genesis subject names a previous object");
72 if (s->sequence > 1 && sv1_all_zero(s->previous, 32))
73 return fail(why, CC_E_CORRUPT, "subject object after the first has no previous");
74 if (sv1_all_zero(s->provenance, 32))
75 return fail(why, CC_E_CORRUPT, "zero provenance");
76 if (s->origin != CS_ORIGIN_OPERATOR && s->origin != CS_ORIGIN_PROMOTED)
77 return fail(why, CC_E_CORRUPT, "unknown subject origin");
78 if (s->n_intents > CS_MAX_INTENTS)
79 return fail(why, CC_E_LIMIT, "too many standing intents");
80 if (s->n_knowledge > CS_MAX_KNOWLEDGE)
81 return fail(why, CC_E_LIMIT, "too many knowledge references");
82 for (uint32_t i = 0; i < s->n_intents; i++) {
83 const struct cs_intent *a = &s->in[i];
84 uint8_t id[32];
85 if (i > 0 && sv1_cmp_id(s->in[i - 1].id, a->id) >= 0)
86 return fail(why, CC_E_CORRUPT, "intents not strictly ascending by id");
87 if (!state_ok(a->state))
88 return fail(why, CC_E_CORRUPT, "unknown intent state");
89 if (a->kind != CS_INTENT_GOAL_LATENCY)
90 return fail(why, CC_E_CORRUPT, "unknown intent kind");
91 if (a->since == 0 || a->since > s->sequence)
92 return fail(why, CC_E_CORRUPT, "intent created outside the subject chain");
93 cs_intent_id(s->agent, a->kind, a->since, a->payload, id);
94#ifndef CS_MUTANT_SKIP_INTENT_ID
95 if (!sv1_equal(id, a->id, 32))
96 return fail(why, CC_E_CORRUPT, "intent id does not derive from its content");
97#endif
98 if (!sv1_all_zero(a->supersedes, 32)) {
99 uint32_t j;
100 if (sv1_equal(a->supersedes, a->id, 32))
101 return fail(why, CC_E_CORRUPT, "intent supersedes itself");
102 for (j = 0; j < s->n_intents; j++)
103 if (sv1_equal(s->in[j].id, a->supersedes, 32))
104 break;
105 if (j == s->n_intents)
106 return fail(why, CC_E_CORRUPT, "superseded intent is absent");
107 if (s->in[j].state != CS_SUPERSEDED)
108 return fail(why, CC_E_CORRUPT, "superseded intent is not marked superseded");
109 if (s->in[j].since > a->since)
110 return fail(why, CC_E_CORRUPT, "intent supersedes a newer intent");
111 }
112#ifndef CS_MUTANT_ACCEPT_TWO_ACTIVE
113 if (a->state == CS_ACTIVE)
114 for (uint32_t j = 0; j < i; j++)
115 if (s->in[j].state == CS_ACTIVE && s->in[j].kind == a->kind &&
116 s->in[j].payload[0] == a->payload[0])
117 return fail(why, CC_E_CORRUPT, "two active intents in one slot");
118#endif
119 }
120 /* Every SUPERSEDED intent is named by exactly one other intent. */
121 for (uint32_t i = 0; i < s->n_intents; i++) {
122 if (s->in[i].state != CS_SUPERSEDED)
123 continue;
124 uint32_t named = 0;
125 for (uint32_t j = 0; j < s->n_intents; j++)
126 if (sv1_equal(s->in[j].supersedes, s->in[i].id, 32))
127 named++;
128 if (named != 1)
129 return fail(why, CC_E_CORRUPT, "superseded intent not named by exactly one successor");
130 }
131 for (uint32_t i = 0; i < s->n_knowledge; i++) {
132 const struct cs_knowledge *k = &s->kn[i];
133 if (sv1_all_zero(k->digest, 32))
134 return fail(why, CC_E_CORRUPT, "zero knowledge digest");
135 if (i > 0 && sv1_cmp_id(s->kn[i - 1].digest, k->digest) >= 0)
136 return fail(why, CC_E_CORRUPT, "knowledge not strictly ascending by digest");
137 if (k->since == 0 || k->since > s->sequence)
138 return fail(why, CC_E_CORRUPT, "knowledge held outside the subject chain");
139 }
140 return CC_OK;
141}
142
143/* ---- encode ---- */
144static size_t body_len(const struct cs_subject *s)
145{
146 return CS_FIXED_BODY + (size_t)s->n_intents * CS_INTENT_BYTES +
147 (size_t)s->n_knowledge * CS_KNOWLEDGE_BYTES;
148}
149
150int cs_subject_encode(const struct cs_subject *s, uint8_t *out, size_t cap, size_t *len,
151 const char **why)
152{
153 int e;
154 size_t body, total, at = 0;
155 if (!s || !len)
156 return fail(why, CC_E_ARG, "null argument");
157 *len = 0;
158 if ((e = cs_subject_validate(s, why)))
159 return e;
160 body = body_len(s);
161 total = CC_HEADER_BYTES + body;
162 if (total > CC_MAX_OBJECT_BYTES)
163 return fail(why, CC_E_LIMIT, "continuity object exceeds the 16384-byte sealed Store cap");
164 if (!out || cap < total)
165 return fail(why, CC_E_ARG, "output buffer too small");
166 sv1_copy(out + at, MAGIC_SUBJECT, 8);
167 at += 8;
168 sv1_put16(out + at, CC_FORMAT_VERSION);
169 at += 2;
170 sv1_put16(out + at, 0);
171 at += 2;
172 sv1_put32(out + at, (uint32_t)body);
173 at += 4;
174 sv1_copy(out + at, s->root, 32);
175 at += 32;
176 sv1_copy(out + at, s->agent, 32);
177 at += 32;
178 sv1_copy(out + at, s->previous, 32);
179 at += 32;
180 sv1_put64(out + at, s->sequence);
181 at += 8;
182 sv1_copy(out + at, s->cortex, 32);
183 at += 32;
184 sv1_copy(out + at, s->provenance, 32);
185 at += 32;
186 out[at++] = s->origin;
187 sv1_zero(out + at, 7);
188 at += 7;
189 sv1_put16(out + at, (uint16_t)s->n_intents);
190 at += 2;
191 sv1_put16(out + at, (uint16_t)s->n_knowledge);
192 at += 2;
193 sv1_put32(out + at, 0);
194 at += 4;
195 for (uint32_t i = 0; i < s->n_intents; i++) {
196 const struct cs_intent *a = &s->in[i];
197 sv1_copy(out + at, a->id, 32);
198 at += 32;
199 sv1_copy(out + at, a->supersedes, 32);
200 at += 32;
201 sv1_put32(out + at, a->kind);
202 at += 4;
203 out[at++] = a->state;
204 sv1_zero(out + at, 3);
205 at += 3;
206 sv1_put64(out + at, a->since);
207 at += 8;
208 sv1_put64(out + at, a->payload[0]);
209 at += 8;
210 sv1_put64(out + at, a->payload[1]);
211 at += 8;
212 }
213 for (uint32_t i = 0; i < s->n_knowledge; i++) {
214 const struct cs_knowledge *k = &s->kn[i];
215 sv1_copy(out + at, k->digest, 32);
216 at += 32;
217 sv1_put64(out + at, k->record);
218 at += 8;
219 sv1_put64(out + at, k->since);
220 at += 8;
221 }
222 *len = at;
223 return CC_OK;
224}
225
226/* ---- decode: one bounds check, every rule, no trailing bytes ---- */
227struct rd {
228 const uint8_t *b;
229 size_t len, at;
230};
231
232static int take(struct rd *r, size_t n, const uint8_t **p, const char **why)
233{
234 if (n > r->len - r->at)
235 return fail(why, CC_E_CORRUPT, "truncated object");
236 *p = r->b + r->at;
237 r->at += n;
238 return CC_OK;
239}
240
241static int rd_zeros(struct rd *r, size_t n, const char **why)
242{
243 const uint8_t *p;
244 int e;
245 if ((e = take(r, n, &p, why)))
246 return e;
247 if (!sv1_all_zero(p, n))
248 return fail(why, CC_E_CORRUPT, "nonzero reserved bytes");
249 return CC_OK;
250}
251
252int cs_subject_decode(const uint8_t *in, size_t len, struct cs_subject *s, const char **why)
253{
254 struct rd r;
255 const uint8_t *p;
256 int e;
257 uint32_t v;
258 if (!in || !s)
259 return fail(why, CC_E_ARG, "null argument");
260 sv1_zero(s, sizeof *s);
261 r.b = in;
262 r.len = len;
263 r.at = 0;
264 if (len > CC_MAX_OBJECT_BYTES)
265 return fail(why, CC_E_LIMIT, "continuity object exceeds the 16384-byte sealed Store cap");
266 if ((e = take(&r, 8, &p, why)))
267 return e;
268 if (!sv1_equal(p, MAGIC_SUBJECT, 8))
269 return fail(why, CC_E_CORRUPT, "bad magic");
270 if ((e = take(&r, 2, &p, why)))
271 return e;
272 if (sv1_get16(p) != CC_FORMAT_VERSION)
273 return fail(why, CC_E_CORRUPT, "unsupported continuity format version");
274 if ((e = rd_zeros(&r, 2, why)))
275 return fail(why, e, "nonzero header reserved");
276 if ((e = take(&r, 4, &p, why)))
277 return e;
278 if (sv1_get32(p) != (uint32_t)(len - CC_HEADER_BYTES))
279 return fail(why, CC_E_CORRUPT, "body length mismatch");
280 if ((e = take(&r, 32, &p, why)))
281 return e;
282 sv1_copy(s->root, p, 32);
283 if ((e = take(&r, 32, &p, why)))
284 return e;
285 sv1_copy(s->agent, p, 32);
286 if ((e = take(&r, 32, &p, why)))
287 return e;
288 sv1_copy(s->previous, p, 32);
289 if ((e = take(&r, 8, &p, why)))
290 return e;
291 s->sequence = sv1_get64(p);
292 if ((e = take(&r, 32, &p, why)))
293 return e;
294 sv1_copy(s->cortex, p, 32);
295 if ((e = take(&r, 32, &p, why)))
296 return e;
297 sv1_copy(s->provenance, p, 32);
298 if ((e = take(&r, 1, &p, why)))
299 return e;
300 s->origin = p[0];
301 if ((e = rd_zeros(&r, 7, why)))
302 return e;
303 if ((e = take(&r, 2, &p, why)))
304 return e;
305 v = sv1_get16(p);
306 if (v > CS_MAX_INTENTS)
307 return fail(why, CC_E_LIMIT, "too many standing intents");
308 s->n_intents = v;
309 if ((e = take(&r, 2, &p, why)))
310 return e;
311 v = sv1_get16(p);
312 if (v > CS_MAX_KNOWLEDGE)
313 return fail(why, CC_E_LIMIT, "too many knowledge references");
314 s->n_knowledge = v;
315 if ((e = rd_zeros(&r, 4, why)))
316 return e;
317 for (uint32_t i = 0; i < s->n_intents; i++) {
318 struct cs_intent *a = &s->in[i];
319 if ((e = take(&r, 32, &p, why)))
320 return e;
321 sv1_copy(a->id, p, 32);
322 if ((e = take(&r, 32, &p, why)))
323 return e;
324 sv1_copy(a->supersedes, p, 32);
325 if ((e = take(&r, 4, &p, why)))
326 return e;
327 a->kind = sv1_get32(p);
328 if ((e = take(&r, 1, &p, why)))
329 return e;
330 a->state = p[0];
331 if ((e = rd_zeros(&r, 3, why)))
332 return e;
333 if ((e = take(&r, 8, &p, why)))
334 return e;
335 a->since = sv1_get64(p);
336 if ((e = take(&r, 8, &p, why)))
337 return e;
338 a->payload[0] = sv1_get64(p);
339 if ((e = take(&r, 8, &p, why)))
340 return e;
341 a->payload[1] = sv1_get64(p);
342 }
343 for (uint32_t i = 0; i < s->n_knowledge; i++) {
344 struct cs_knowledge *k = &s->kn[i];
345 if ((e = take(&r, 32, &p, why)))
346 return e;
347 sv1_copy(k->digest, p, 32);
348 if ((e = take(&r, 8, &p, why)))
349 return e;
350 k->record = sv1_get64(p);
351 if ((e = take(&r, 8, &p, why)))
352 return e;
353 k->since = sv1_get64(p);
354 }
355 if (r.at != r.len)
356 return fail(why, CC_E_CORRUPT, "trailing bytes");
357 return cs_subject_validate(s, why);
358}
359
360int cs_subject_id(const uint8_t *bytes, size_t len, uint8_t out[32])
361{
362 return cc_object_id(CC_KIND_SUBJECT, bytes, len, out);
363}
364
365/* ---- editing ---- */
366void cs_subject_genesis(struct cs_subject *s, const struct cr_view *v, const uint8_t provenance[32],
367 uint8_t origin)
368{
369 sv1_zero(s, sizeof *s);
370 sv1_copy(s->root, v->root_id, 32);
371 sv1_copy(s->agent, v->root.agent_id, 32);
372 s->sequence = 1;
373 sv1_copy(s->provenance, provenance, 32);
374 s->origin = origin;
375}
376
377void cs_subject_advance(struct cs_subject *s, const uint8_t prev_id[32],
378 const uint8_t provenance[32], uint8_t origin)
379{
380 sv1_copy(s->previous, prev_id, 32);
381 s->sequence += 1;
382 sv1_copy(s->provenance, provenance, 32);
383 s->origin = origin;
384}
385
386static long find_intent(const struct cs_subject *s, const uint8_t id[32], uint32_t *ins)
387{
388 uint32_t i;
389 for (i = 0; i < s->n_intents; i++) {
390 int c = sv1_cmp_id(s->in[i].id, id);
391 if (c == 0)
392 return (long)i;
393 if (c > 0)
394 break;
395 }
396 if (ins)
397 *ins = i;
398 return -1;
399}
400
401const struct cs_intent *cs_subject_active(const struct cs_subject *s, uint32_t kind, uint64_t slot)
402{
403 for (uint32_t i = 0; i < s->n_intents; i++)
404 if (s->in[i].state == CS_ACTIVE && s->in[i].kind == kind && s->in[i].payload[0] == slot)
405 return &s->in[i];
406 return 0;
407}
408
409int cs_subject_intend(struct cs_subject *s, uint32_t kind, const uint64_t payload[2],
410 uint8_t id_out[32], const char **why)
411{
412 struct cs_intent a;
413 uint32_t ins = 0;
414 if (!s || !payload)
415 return fail(why, CC_E_ARG, "null argument");
416 if (kind != CS_INTENT_GOAL_LATENCY)
417 return fail(why, CC_E_ARG, "unknown intent kind");
418 if (s->n_intents >= CS_MAX_INTENTS)
419 return fail(why, CC_E_LIMIT, "too many standing intents");
420 sv1_zero(&a, sizeof a);
421 a.kind = kind;
422 a.state = CS_ACTIVE;
423 a.since = s->sequence;
424 a.payload[0] = payload[0];
425 a.payload[1] = payload[1];
426 cs_intent_id(s->agent, kind, a.since, a.payload, a.id);
427 if (find_intent(s, a.id, &ins) >= 0)
428 return fail(why, CC_E_ARG, "intent already present");
429 /* One active intent per slot: the old one is superseded by this one. */
430 for (uint32_t i = 0; i < s->n_intents; i++) {
431 struct cs_intent *o = &s->in[i];
432 if (o->state == CS_ACTIVE && o->kind == kind && o->payload[0] == payload[0]) {
433 o->state = CS_SUPERSEDED;
434 sv1_copy(a.supersedes, o->id, 32);
435 break;
436 }
437 }
438 for (uint32_t i = s->n_intents; i > ins; i--)
439 s->in[i] = s->in[i - 1];
440 s->in[ins] = a;
441 s->n_intents += 1;
442 if (id_out)
443 sv1_copy(id_out, a.id, 32);
444 return CC_OK;
445}
446
447int cs_subject_retire(struct cs_subject *s, const uint8_t id[32], const char **why)
448{
449 long i;
450 if (!s || !id)
451 return fail(why, CC_E_ARG, "null argument");
452 i = find_intent(s, id, 0);
453 if (i < 0)
454 return fail(why, CC_E_ARG, "intent absent");
455 if (s->in[i].state != CS_ACTIVE)
456 return fail(why, CC_E_ARG, "intent not active");
457 s->in[i].state = CS_INACTIVE;
458 return CC_OK;
459}
460
461int cs_subject_hold(struct cs_subject *s, const uint8_t digest[32], uint64_t record,
462 const char **why)
463{
464 uint32_t ins;
465 if (!s || !digest)
466 return fail(why, CC_E_ARG, "null argument");
467 if (sv1_all_zero(digest, 32))
468 return fail(why, CC_E_ARG, "zero knowledge digest");
469 if (s->n_knowledge >= CS_MAX_KNOWLEDGE)
470 return fail(why, CC_E_LIMIT, "too many knowledge references");
471 for (ins = 0; ins < s->n_knowledge; ins++) {
472 int c = sv1_cmp_id(s->kn[ins].digest, digest);
473 if (c == 0)
474 return fail(why, CC_E_ARG, "knowledge already held");
475 if (c > 0)
476 break;
477 }
478 for (uint32_t i = s->n_knowledge; i > ins; i--)
479 s->kn[i] = s->kn[i - 1];
480 sv1_copy(s->kn[ins].digest, digest, 32);
481 s->kn[ins].record = record;
482 s->kn[ins].since = s->sequence;
483 s->n_knowledge += 1;
484 return CC_OK;
485}
486
487void cs_subject_bind_cortex(struct cs_subject *s, const uint8_t lineage[32])
488{
489 sv1_copy(s->cortex, lineage, 32);
490}
491
492/* ---- resolve ---- */
493static int map_cc(int rc)
494{
495 return rc == CC_E_LIMIT ? CR_LIMIT : rc == CC_E_ARG ? CR_E_ARG : CR_CORRUPT;
496}
497
498/* Pass 1: every kind-24 object of this Store into w->t (id, seq, previous),
499 * each one decoded and checked against the resolved root. Pass 2: the chain
500 * 1..n must be present exactly once each and linked by previous. The head is
501 * re-read into out. */
502int cs_resolve(const struct cr_source *src, struct cr_work *w, const struct cr_view *v,
503 struct cs_subject *out, uint8_t out_id[32], const char **why, int *store_rc)
504{
505 uint32_t n, i, cnt = 0;
506 uint64_t head_seq = 0;
507 uint32_t head = 0;
508 if (!src || !w || !v || !out || !out_id)
509 return fail(why, CR_E_ARG, "null argument");
510 n = src->count(src->ctx);
511 for (i = 0; i < n; i++) {
512 uint16_t kind = 0, version = 0;
513 size_t len = 0;
514 int rc;
515 if ((rc = src->entry(src->ctx, i, &kind, &version))) {
516 if (store_rc)
517 *store_rc = rc;
518 return fail(why, CR_STORE, "store entry failed");
519 }
520 if (kind != CC_KIND_SUBJECT)
521 continue;
522 if (version != CC_STORE_OBJECT_VERSION)
523 return fail(why, CR_CORRUPT, "subject object with an unknown claim version");
524 if ((rc = src->read(src->ctx, i, w->buf, sizeof w->buf, &len))) {
525 if (store_rc)
526 *store_rc = rc;
527 return fail(why, CR_STORE, "store read failed");
528 }
529 if (len > sizeof w->buf)
530 return fail(why, CR_LIMIT, "subject object exceeds the 16384-byte sealed Store cap");
531 if ((rc = cs_subject_decode(w->buf, len, out, why)))
532 return map_cc(rc);
533#ifndef CS_MUTANT_ACCEPT_FOREIGN
534 if (!sv1_equal(out->root, v->root_id, 32) || !sv1_equal(out->agent, v->root.agent_id, 32))
535 return fail(why, CR_CORRUPT, "subject object belongs to another agent");
536#endif
537 if (cnt >= CR_TABLE_CAP)
538 return fail(why, CR_LIMIT, "too many subject objects");
539 if (cc_object_id(CC_KIND_SUBJECT, w->buf, len, w->t[cnt].id) != CC_OK)
540 return fail(why, CR_E_ARG, "object id");
541 w->t[cnt].seq = out->sequence;
542 sv1_copy(w->t[cnt].previous, out->previous, 32);
543 w->t[cnt].src = i;
544 w->t[cnt].kind = kind;
545 w->t[cnt].version = version;
546 cnt++;
547 }
548 if (cnt == 0) {
549 sv1_zero(out, sizeof *out);
550 sv1_zero(out_id, 32);
551 return CS_ABSENT;
552 }
553 /* Pass 2: exactly one object per sequence 1..cnt, each naming its predecessor. */
554 for (uint64_t s = 1; s <= cnt; s++) {
555 uint32_t at = cnt, found = 0;
556 for (i = 0; i < cnt; i++)
557 if (w->t[i].seq == s) {
558 at = i;
559 found++;
560 }
561#ifndef CS_MUTANT_ACCEPT_FORK
562 if (found > 1)
563 return fail(why, CR_CORRUPT, "subject chain fork");
564#endif
565 if (found == 0)
566 return fail(why, CR_CORRUPT, "subject chain gap");
567 if (s == 1) {
568 if (!sv1_all_zero(w->t[at].previous, 32))
569 return fail(why, CR_CORRUPT, "genesis subject names a previous object");
570 } else {
571 uint32_t prev = cnt;
572 for (i = 0; i < cnt; i++)
573 if (w->t[i].seq == s - 1)
574 prev = i;
575 if (prev == cnt || !sv1_equal(w->t[at].previous, w->t[prev].id, 32))
576 return fail(why, CR_CORRUPT, "subject chain link broken");
577 }
578 if (s > head_seq) {
579 head_seq = s;
580 head = at;
581 }
582 }
583 {
584 size_t len = 0;
585 int rc;
586 if ((rc = src->read(src->ctx, w->t[head].src, w->buf, sizeof w->buf, &len))) {
587 if (store_rc)
588 *store_rc = rc;
589 return fail(why, CR_STORE, "store read failed");
590 }
591 if ((rc = cs_subject_decode(w->buf, len, out, why)))
592 return map_cc(rc);
593 }
594 sv1_copy(out_id, w->t[head].id, 32);
595 return CS_RESOLVED;
596}
597
598/* ---- commit ---- */
599int cs_commit(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w,
600 const struct cr_view *v, const struct cs_subject *s, uint8_t out_id[32],
601 const char **why, int *store_rc)
602{
603 static struct cs_subject cur; /* scratch; the kernel style keeps big state static */
604 uint8_t cur_id[32];
605 size_t len = 0;
606 int rc;
607 struct cr_wobj obj;
608 if (!src || !snk || !w || !v || !s || !out_id)
609 return fail(why, CR_E_ARG, "null argument");
610 if ((rc = cs_subject_validate(s, why)))
611 return map_cc(rc);
612 if (!sv1_equal(s->root, v->root_id, 32) || !sv1_equal(s->agent, v->root.agent_id, 32))
613 return fail(why, CR_CORRUPT, "subject object belongs to another agent");
614 rc = cs_resolve(src, w, v, &cur, cur_id, why, store_rc);
615 if (rc == CS_ABSENT) {
616 if (s->sequence != 1)
617 return fail(why, CR_CORRUPT, "first subject object must have sequence 1");
618 } else if (rc == CS_RESOLVED) {
619 if (s->sequence != cur.sequence + 1 || !sv1_equal(s->previous, cur_id, 32))
620 return fail(why, CR_CORRUPT, "subject object does not continue the resolved chain");
621 } else {
622 return rc;
623 }
624 if ((rc = cs_subject_encode(s, w->buf, sizeof w->buf, &len, why)))
625 return map_cc(rc);
626 obj.kind = CC_KIND_SUBJECT;
627 obj.version = CC_STORE_OBJECT_VERSION;
628 obj.bytes = w->buf;
629 obj.len = len;
630 if ((rc = snk->transact(snk->ctx, &obj, 1))) {
631 if (store_rc)
632 *store_rc = rc;
633 return fail(why, CR_STORE, "store transaction failed");
634 }
635 if (cc_object_id(CC_KIND_SUBJECT, w->buf, len, out_id) != CC_OK)
636 return fail(why, CR_E_ARG, "object id");
637 return CS_RESOLVED;
638}