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_service.c

239 lines · 8674 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_service.c -- production continuity on the resident lifecycle
18 * (native v1 plan P05, cut 1). Contract and limits: continuity_service.h. */
19#include "continuity_service.h"
20#include <string.h>
21#include "ck.h"
22#include "continuity_resolve_sealed.h"
23#include "continuity_subject_provision.h"
24#include "entropy.h"
25#include "fmt.h"
26#include "sha256.h"
27
28#if !defined(CK_SERVICE) && !defined(CK_CONT_SERVICE_HOST_TEST)
29#error "continuity_service.c is only part of a CK_SERVICE=1 image (or the host stage test)"
30#endif
31#if defined(CK_TEST_CONTINUITY)
32#error "continuity_service.c is never combined with the TEST-ONLY continuity mode selection"
33#endif
34
35/* ---- the entropy source: the kernel's RNDR service or a refusal, no fallback ---- */
36static int ent_present(void *ctx)
37{
38 (void)ctx;
39 return ck_entropy_status() == CK_RNG_OK;
40}
41static int ent_read(void *ctx, uint64_t *out)
42{
43 (void)ctx;
44 uint64_t w = 0;
45 if (ck_entropy_fill(&w, sizeof w) != 0) return 0;
46 *out = w;
47 return 1;
48}
49static const struct ck_rng_ops k_ent_ops = {ent_present, ent_read, 0};
50
51/* ---- workspace (heap, once; the view is kept for the service lifetime) ---- */
52static struct {
53 struct cr_work *w;
54 struct cr_txwork *tw;
55 struct cr_view *v;
56 struct cs_subject *subj;
57 uint8_t sid[32];
58 int state;
59 int allocated;
60} g;
61
62static int alloc_all(void)
63{
64 if (g.allocated) return 0;
65 g.w = ck_alloc(sizeof *g.w);
66 g.tw = ck_alloc(sizeof *g.tw);
67 g.v = ck_alloc(sizeof *g.v);
68 g.subj = ck_alloc(sizeof *g.subj);
69 g.allocated = g.w && g.tw && g.v && g.subj;
70 return g.allocated ? 0 : -1;
71}
72
73static void hex_into(char *o, const uint8_t *b, size_t n)
74{
75 static const char hx[] = "0123456789abcdef";
76 for (size_t i = 0; i < n; i++) {
77 o[2 * i] = hx[b[i] >> 4];
78 o[2 * i + 1] = hx[b[i] & 15];
79 }
80 o[2 * n] = 0;
81}
82
83static void view_line(const char *word, const struct cr_view *v)
84{
85 char ag[65];
86 hex_into(ag, v->root.agent_id, 32);
87 ck_printf("continuity: %s agent=%s incarnation=%llu sequence=%llu cortex=%llu branches=%u\n", word, ag,
88 (unsigned long long)v->manifest.incarnation, (unsigned long long)v->manifest.sequence,
89 (unsigned long long)v->cortex_count, (unsigned)v->state.n);
90}
91
92static void subject_line(const char *word, const struct cs_subject *s, const uint8_t id[32])
93{
94 char sub[65], ln[65];
95 uint32_t active = 0;
96 for (uint32_t i = 0; i < s->n_intents; i++) active += s->in[i].state == CS_ACTIVE;
97 hex_into(sub, id, 32);
98 hex_into(ln, s->cortex, 32);
99 ck_printf("allen: %s subject=%s sequence=%llu lineage=%s intents=%u active=%u knowledge=%u\n", word, sub,
100 (unsigned long long)s->sequence, ln, (unsigned)s->n_intents, (unsigned)active, (unsigned)s->n_knowledge);
101}
102
103static int refused(const char *step, int outcome, const char *why, int store_rc)
104{
105 ck_printf("continuity: REFUSED step=\"%s\" outcome=%s%s%s store_rc=%d (nothing written; lifecycle stays locked)\n",
106 step, cr_outcome_name(outcome), why ? " why=" : "", why ? why : "", store_rc);
107 g.state = CK_CONT_REFUSED;
108 return g.state;
109}
110
111/* provenance = SHA-256(label || owner pk || store uuid); lineage = SHA-256(label || store uuid). */
112static void genesis_inputs(struct cs_genesis *gen, const uint8_t store_uuid[16], const uint8_t *owner_pk,
113 size_t owner_pk_len)
114{
115 static const char lp[] = "AIENOS/ALLEN/PROVENANCE-V1";
116 static const char ll[] = "AIENOS/ALLEN/CORTEX-LINEAGE-V1";
117 uint8_t buf[sizeof lp + 64 + 16];
118 size_t n = 0;
119 memset(gen, 0, sizeof *gen);
120 memcpy(buf, lp, sizeof lp - 1);
121 n = sizeof lp - 1;
122 if (owner_pk && owner_pk_len && owner_pk_len <= 64) {
123 memcpy(buf + n, owner_pk, owner_pk_len);
124 n += owner_pk_len;
125 }
126 memcpy(buf + n, store_uuid, 16);
127 n += 16;
128 sha256_hash(buf, n, gen->provenance);
129 memcpy(buf, ll, sizeof ll - 1);
130 memcpy(buf + sizeof ll - 1, store_uuid, 16);
131 sha256_hash(buf, sizeof ll - 1 + 16, gen->cortex);
132 gen->origin = CS_ORIGIN_OPERATOR;
133}
134
135int ck_cont_service_start(ss_store *s, const uint8_t store_uuid[16], const uint8_t *owner_pk, size_t owner_pk_len,
136 uint8_t source)
137{
138 struct cr_source src;
139 struct cr_sink snk;
140 struct cr_sealed_sink sk = {s, 0, 0};
141 const char *why = 0;
142 int store_rc = 0;
143 g.state = CK_CONT_NONE;
144 if (!s || !store_uuid) return refused("arguments", CR_E_ARG, "null", 0);
145 if (alloc_all()) return refused("workspace", CR_LIMIT, "heap", 0);
146 cr_bind_sealed(&src, s);
147 cr_bind_sealed_sink(&snk, &sk);
148
149 int o = cr_resolve(&src, g.w, g.v, &why, &store_rc);
150 if (o == CR_UNPROVISIONED) {
151 struct ck_rng rng;
152 struct cs_genesis gen;
153 memset(&rng, 0, sizeof rng);
154 if (ck_rng_probe(&rng, &k_ent_ops) != CK_RNG_OK)
155 return refused("genesis entropy", CR_NO_ENTROPY, ck_rng_reason(rng.state), 0);
156 genesis_inputs(&gen, store_uuid, owner_pk, owner_pk_len);
157 o = cs_provision(&src, &snk, g.w, g.tw, &rng, store_uuid, source, &gen, g.v, g.subj, g.sid, &why, &store_rc);
158 if (o != CR_RESOLVED) return refused("provision", o, why, store_rc);
159 ck_printf("continuity: genesis source=%s provenance=%s (identity and ALLEN subject in one transaction)\n",
160 source == CC_SOURCE_OPERATOR ? "operator" : "qualification",
161 owner_pk && owner_pk_len ? "owner-root-pk+store-uuid" : "store-uuid-only");
162 view_line("PROVISIONED", g.v);
163 subject_line("GENESIS", g.subj, g.sid);
164 g.state = CK_CONT_PROVISIONED;
165 return g.state;
166 }
167 if (o != CR_RESOLVED) return refused("resolve", o, why, store_rc);
168
169 /* Restore the subject read-only before anything is written. */
170 int cs = cs_resolve(&src, g.w, g.v, g.subj, g.sid, &why, &store_rc);
171 if (cs == CS_ABSENT) return refused("subject", CR_CORRUPT, "provisioned without a subject (never minted here)", store_rc);
172 if (cs != CS_RESOLVED) return refused("subject", cs, why, store_rc);
173 subject_line("RESTORED", g.subj, g.sid);
174
175 int committed = 0;
176 o = cr_resume(&src, &snk, g.w, g.tw, g.v, &committed, &why, &store_rc);
177 if (o != CR_RESOLVED) return refused("resume", o, why, store_rc);
178 if (!committed) {
179 view_line("RESUMED_READONLY", g.v);
180 ck_puts("continuity: degraded mount: restored without writing; commits are refused until a valid mount\n");
181 g.state = CK_CONT_RESUMED_READONLY;
182 return g.state;
183 }
184 view_line("RESUMED", g.v);
185 g.state = CK_CONT_RESUMED;
186 return g.state;
187}
188
189int ck_cont_service_state(void)
190{
191 return g.state;
192}
193
194int ck_cont_service_writable(void)
195{
196 return g.state == CK_CONT_PROVISIONED || g.state == CK_CONT_RESUMED;
197}
198
199static const char *state_word(int st)
200{
201 switch (st) {
202 case CK_CONT_PROVISIONED: return "provisioned";
203 case CK_CONT_RESUMED: return "resumed";
204 case CK_CONT_RESUMED_READONLY: return "resumed-readonly";
205 case CK_CONT_REFUSED: return "refused";
206 default: return "none";
207 }
208}
209
210void ck_cont_service_status(char *out, size_t cap)
211{
212 if (!out || !cap) return;
213 if (g.state > 0 && g.v && g.subj) {
214 char ag[17];
215 hex_into(ag, g.v->root.agent_id, 8);
216 ck_snprintf(out, cap, "continuity=%s agent=%s incarnation=%llu subject_seq=%llu", state_word(g.state), ag,
217 (unsigned long long)g.v->manifest.incarnation, (unsigned long long)g.subj->sequence);
218 } else {
219 ck_snprintf(out, cap, "continuity=%s", state_word(g.state));
220 }
221}
222
223const uint8_t *ck_cont_service_agent(void)
224{
225 return g.state > 0 && g.v ? g.v->root.agent_id : 0;
226}
227
228uint64_t ck_cont_service_incarnation(void)
229{
230 return g.state > 0 && g.v ? g.v->manifest.incarnation : 0;
231}
232
233void ck_cont_service_stop(void)
234{
235 g.state = CK_CONT_NONE;
236 if (g.v) memset(g.v, 0, sizeof *g.v);
237 if (g.subj) memset(g.subj, 0, sizeof *g.subj);
238 memset(g.sid, 0, sizeof g.sid);
239}