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 / lifecycle_fsm.h

141 lines · 7800 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/* lifecycle_fsm.h -- resident production lifecycle: states, verbs, transitions.
18 *
19 * Pure logic (no kernel calls, no libc): the host test tests/test_lifecycle.c
20 * compiles this file as is. core/lifecycle.c binds it to the serial console,
21 * the sealed Store and the device teardown order (CK_SERVICE=1 image only).
22 *
23 * States (native v1 plan P03: initialize, locked/ready, serve, quiesce, stop):
24 * INIT before the lifecycle took over from the boot stages
25 * LOCKED production Store not open (no production key source yet, or it
26 * refused): status/unlock/help/shutdown only; every task refused
27 * READY Store open: a task may be submitted
28 * SERVE one bounded task pending: approve commits it, deny or cancel drops it
29 * QUIESCE stop order running: flush, revoke, halt
30 * STOP nothing more is accepted
31 * Verbs are the v1 operator surface and nothing else: status, unlock, task
32 * <text>, approve, deny, cancel, evidence, help, shutdown, and (P06) prompt
33 * <text>, recall, remember <text>, facts, and (N3, aienos#329) prefer <text>. */
34#ifndef AIENOS_CK_LIFECYCLE_FSM_H
35#define AIENOS_CK_LIFECYCLE_FSM_H
36
37#include <stddef.h>
38#include <stdint.h>
39
40enum ck_lc_state { CK_LC_INIT = 0, CK_LC_LOCKED, CK_LC_READY, CK_LC_SERVE, CK_LC_QUIESCE, CK_LC_STOP };
41
42enum ck_lc_verb {
43 CK_LC_V_INVALID = -1, /* overlong, unprintable or malformed: refused whole */
44 CK_LC_V_UNKNOWN = 0,
45 CK_LC_V_EMPTY,
46 CK_LC_V_HELP,
47 CK_LC_V_STATUS,
48 CK_LC_V_UNLOCK,
49 CK_LC_V_TASK,
50 CK_LC_V_APPROVE,
51 CK_LC_V_DENY,
52 CK_LC_V_CANCEL,
53 CK_LC_V_EVIDENCE,
54 CK_LC_V_SHUTDOWN,
55 CK_LC_V_PROMPT, /* native v1 P06: bounded generation from the resident model */
56 CK_LC_V_RECALL, /* read back the newest approved object from the Store */
57 CK_LC_V_REMEMBER, /* P06 cut 4: record one owner fact in the Cortex journal (sealed Store) */
58 CK_LC_V_FACTS, /* list the remembered facts read back from the journal */
59 CK_LC_V_ROTATE, /* P04 cut 2b: rotate the recovery secret (current + new, 64 hex each) */
60 CK_LC_V_UPDATE, /* P11 cut 3: write the inbox image into the inactive slot and stage it */
61 CK_LC_V_PREFER, /* N3: record one owner preference permitted to condition generation */
62};
63
64/* Bounds: one request line, and the task text that a line may carry. */
65#define CK_LC_LINE_MAX 144 /* "rotate " + 64 hex + " " + 64 hex = 136 bytes fits */
66#define CK_LC_TASK_MAX 48
67#define CK_LC_REPLY_MAX 1536 /* generated reply bytes kept for approval (= CK_INFER_REPLY_TEXT; N3: a few hundred tokens) */
68/* What is pending in SERVE: an operator note (task) or a generated reply. */
69enum ck_lc_pending { CK_LC_PENDING_NONE = 0, CK_LC_PENDING_NOTE, CK_LC_PENDING_REPLY };
70
71/* Outcome of one request against the state machine. */
72enum ck_lc_result {
73 CK_LC_R_OK = 0, /* accepted, state may have changed */
74 CK_LC_R_REFUSED, /* legal verb, refused in this state (reason set) */
75 CK_LC_R_INVALID, /* the line itself was refused (bounds / syntax) */
76 CK_LC_R_COMMIT, /* approve accepted: the binding must commit the task */
77 CK_LC_R_UNLOCK, /* unlock accepted: the binding must try the key source */
78 CK_LC_R_STOP, /* shutdown accepted: the binding must run the stop order */
79 CK_LC_R_GENERATE, /* prompt accepted: the binding must generate (task holds the prompt) */
80 CK_LC_R_RECALL, /* recall accepted: the binding must read the newest approved object */
81 CK_LC_R_REMEMBER, /* remember accepted: the binding must append the fact (task holds the text) */
82 CK_LC_R_FACTS, /* facts accepted: the binding must list the journal */
83 CK_LC_R_ROTATE, /* rotate accepted: the binding must re-wrap K_vol under the new secret */
84 CK_LC_R_UPDATE, /* update accepted: the binding must run the inactive-slot updater */
85 CK_LC_R_PREFER, /* prefer accepted: the binding must append the bound preference (fact holds the text) */
86};
87
88typedef struct {
89 enum ck_lc_state state;
90 char task[CK_LC_TASK_MAX + 1]; /* pending note text, or the prompt of a pending reply, in SERVE; else "" */
91 size_t task_len;
92 char reply[CK_LC_REPLY_MAX + 1]; /* generated reply awaiting approval (pending == REPLY), else "" */
93 size_t reply_len;
94 enum ck_lc_pending pending;
95 /* counters for the evidence verb (bounded, wrap is harmless) */
96 uint32_t requests, refused, invalid, tasks, approved, denied, cancelled;
97 uint32_t prompts, replies, gen_cancelled, gen_failed, recalls;
98 uint32_t remembers, facts; /* P06 cut 4: accepted remember / facts requests */
99 uint32_t updates; /* P11 cut 3: accepted update requests */
100 uint32_t prefers; /* N3: accepted prefer requests */
101 uint32_t bind_refused, replays; /* N3: approvals refused (stale or changed binding) or as a replay */
102 char fact[CK_LC_TASK_MAX + 1]; /* text of the remember request being served, else "" */
103 size_t fact_len;
104 const char *reason; /* last refusal reason, static string */
105} ck_lc;
106
107/* Decode one request line (len bytes, no terminator required). arg and arg_len
108 * receive the text after "task", "prompt" or "remember" (trimmed), else an
109 * empty span. */
110enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, size_t *arg_len);
111const char *ck_lc_verb_name(enum ck_lc_verb v);
112const char *ck_lc_state_name(enum ck_lc_state s);
113
114void ck_lc_init(ck_lc *lc);
115/* Boot stages are over: Store open -> READY, else LOCKED. */
116void ck_lc_start(ck_lc *lc, int store_open);
117/* Apply one decoded request. Pure transition; the binding performs the side
118 * effect that the result names and then calls the *_done function. */
119enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, size_t arg_len);
120/* Binding results: commit outcome (0 ok: SERVE -> READY; else stays SERVE
121 * with reason), unlock outcome (1 opened: LOCKED -> READY). */
122void ck_lc_commit_done(ck_lc *lc, int rc);
123/* N3: approve found the request already committed (idempotence key on the disk):
124 * the pending proposal is dropped, nothing is written (SERVE -> READY). */
125void ck_lc_commit_replay(ck_lc *lc);
126/* N3: approve refused because the proposal no longer binds (stale or changed):
127 * stays SERVE, nothing written; the operator cancels and resubmits. */
128void ck_lc_commit_refused(ck_lc *lc, const char *why);
129void ck_lc_unlock_done(ck_lc *lc, int opened);
130/* Rotation outcome after CK_LC_R_ROTATE: the state never changes; a refusal
131 * by the Store (wrong current secret, no keyslot) counts as refused. */
132void ck_lc_rotate_done(ck_lc *lc, int ok);
133/* Generation outcome after CK_LC_R_GENERATE: a reply of n bytes (1..CK_LC_REPLY_MAX)
134 * becomes the pending proposal (READY -> SERVE, replies++); cancelled (the
135 * operator typed cancel between steps) or failed/refused (n == 0) drops the
136 * prompt and stays READY with the counter named by the outcome. */
137enum ck_lc_gen { CK_LC_GEN_REPLY = 0, CK_LC_GEN_CANCELLED, CK_LC_GEN_FAILED };
138void ck_lc_generate_done(ck_lc *lc, enum ck_lc_gen outcome, const char *reply, size_t n);
139void ck_lc_stopped(ck_lc *lc);
140
141#endif