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/tests/test_lifecycle.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 / tests / test_lifecycle.c` 238 lines ยท 15679 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 /* test_lifecycle.c -- the resident lifecycle state machine and request 18 * decoder (core/lifecycle_fsm.c) on the host: bounds, verbs, every state 19 * transition, refusals in the wrong state, the stop order flag. */ 20 #include 21 #include "ck_test.h" 22 #include "lifecycle_fsm.h" 23 24 static enum ck_lc_verb dec(const char *s, const char **a, size_t *al) { return ck_lc_decode(s, strlen(s), a, al); } 25 static enum ck_lc_result req(ck_lc *lc, const char *s) 26 { 27 const char *a; size_t al; 28 enum ck_lc_verb v = dec(s, &a, &al); 29 return ck_lc_apply(lc, v, a, al); 30 } 31 32 int main(void) 33 { 34 const char *a; size_t al; 35 /* decoder */ 36 CHECK(dec("status", &a, &al) == CK_LC_V_STATUS && al == 0); 37 CHECK(dec(" help ", &a, &al) == CK_LC_V_HELP); 38 CHECK(dec("", &a, &al) == CK_LC_V_EMPTY && dec(" ", &a, &al) == CK_LC_V_EMPTY); 39 CHECK(dec("task write the note", &a, &al) == CK_LC_V_TASK && al == 14 && memcmp(a, "write the note", 14) == 0); 40 CHECK(dec("task padded ", &a, &al) == CK_LC_V_TASK && al == 6 && memcmp(a, "padded", 6) == 0); 41 CHECK(dec("task", &a, &al) == CK_LC_V_INVALID); /* task needs text */ 42 CHECK(dec("status now", &a, &al) == CK_LC_V_INVALID); /* no argument allowed */ 43 CHECK(dec("unlock", &a, &al) == CK_LC_V_UNLOCK && al == 0); 44 CHECK(dec("unlock deadbeef ", &a, &al) == CK_LC_V_UNLOCK && al == 8 && memcmp(a, "deadbeef", 8) == 0); 45 CHECK(CK_LC_LINE_MAX >= 7 + 64); /* "unlock " + a 64-hex recovery secret fits in one line */ 46 CHECK(CK_LC_LINE_MAX >= 7 + 64 + 1 + 64); /* "rotate " + current + " " + new */ 47 CHECK(dec("rotate aa bb", &a, &al) == CK_LC_V_ROTATE && al == 5 && memcmp(a, "aa bb", 5) == 0); 48 CHECK(dec("rotate", &a, &al) == CK_LC_V_ROTATE && al == 0); 49 CHECK(dec("update", &a, &al) == CK_LC_V_UPDATE && al == 0); /* P11 cut 3 */ 50 CHECK(dec("update halt-after=2", &a, &al) == CK_LC_V_UPDATE && al == 12 && memcmp(a, "halt-after=2", 12) == 0); 51 CHECK(strcmp(ck_lc_verb_name(CK_LC_V_UPDATE), "update") == 0); 52 CHECK(dec("frobnicate", &a, &al) == CK_LC_V_UNKNOWN); 53 CHECK(dec("prompt hi", &a, &al) == CK_LC_V_PROMPT && al == 2 && memcmp(a, "hi", 2) == 0); 54 CHECK(dec("prompt", &a, &al) == CK_LC_V_INVALID); /* prompt needs text */ 55 CHECK(dec("recall", &a, &al) == CK_LC_V_RECALL && al == 0); 56 CHECK(dec("recall x", &a, &al) == CK_LC_V_INVALID); /* no argument allowed */ 57 CHECK(dec("remember prefer short notes", &a, &al) == CK_LC_V_REMEMBER && al == 18 && memcmp(a, "prefer short notes", 18) == 0); 58 CHECK(dec("remember", &a, &al) == CK_LC_V_INVALID); /* remember needs text */ 59 CHECK(dec("facts", &a, &al) == CK_LC_V_FACTS && al == 0); 60 CHECK(dec("facts all", &a, &al) == CK_LC_V_INVALID); /* no argument allowed */ 61 CHECK(dec("sta\x01tus", &a, &al) == CK_LC_V_INVALID); /* unprintable */ 62 { 63 char big[CK_LC_LINE_MAX + 2]; 64 memset(big, 'x', sizeof big - 1); big[sizeof big - 1] = 0; 65 CHECK(dec(big, &a, &al) == CK_LC_V_INVALID); /* CK_LC_LINE_MAX + 1 bytes refused whole */ 66 char t[CK_LC_LINE_MAX + 1]; 67 memset(t, 'y', CK_LC_LINE_MAX); t[CK_LC_LINE_MAX] = 0; 68 memcpy(t, "task ", 5); 69 CHECK(dec(t, &a, &al) == CK_LC_V_INVALID && al == CK_LC_LINE_MAX - 5); /* text 91 > 48 */ 70 } 71 CHECK(strcmp(ck_lc_verb_name(CK_LC_V_SHUTDOWN), "shutdown") == 0 && strcmp(ck_lc_state_name(CK_LC_SERVE), "serve") == 0); 72 73 /* locked: nothing but status/help/evidence/unlock/shutdown */ 74 ck_lc lc; 75 ck_lc_init(&lc); 76 CHECK(lc.state == CK_LC_INIT && req(&lc, "status") == CK_LC_R_REFUSED); 77 ck_lc_start(&lc, 0); 78 CHECK(lc.state == CK_LC_LOCKED); 79 CHECK(req(&lc, "status") == CK_LC_R_OK && req(&lc, "help") == CK_LC_R_OK && req(&lc, "evidence") == CK_LC_R_OK); 80 CHECK(req(&lc, "task hello") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.tasks == 0); 81 CHECK(req(&lc, "rotate aa bb") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED); /* unlock first */ 82 CHECK(req(&lc, "update") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.updates == 0 && strstr(lc.reason, "unlock before updating")); 83 CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "deny") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); 84 CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); 85 ck_lc_unlock_done(&lc, 0); 86 CHECK(lc.state == CK_LC_LOCKED && strstr(lc.reason, "still locked")); 87 CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); 88 ck_lc_unlock_done(&lc, 1); 89 CHECK(lc.state == CK_LC_READY); 90 CHECK(req(&lc, "unlock") == CK_LC_R_REFUSED); /* already unlocked */ 91 CHECK(req(&lc, "rotate") == CK_LC_R_REFUSED && lc.state == CK_LC_READY); /* needs both secrets */ 92 CHECK(req(&lc, "rotate aa bb") == CK_LC_R_ROTATE); 93 ck_lc_rotate_done(&lc, 0); 94 CHECK(lc.state == CK_LC_READY && strstr(lc.reason, "rotation refused")); 95 CHECK(req(&lc, "rotate aa bb") == CK_LC_R_ROTATE); 96 ck_lc_rotate_done(&lc, 1); 97 CHECK(lc.state == CK_LC_READY); 98 CHECK(req(&lc, "update") == CK_LC_R_UPDATE && lc.state == CK_LC_READY && lc.updates == 1); 99 CHECK(req(&lc, "shutdown") == CK_LC_R_STOP && lc.state == CK_LC_QUIESCE); 100 CHECK(req(&lc, "status") == CK_LC_R_REFUSED && strcmp(lc.reason, "stopping") == 0); 101 ck_lc_stopped(&lc); 102 CHECK(lc.state == CK_LC_STOP && req(&lc, "task x") == CK_LC_R_REFUSED); 103 104 /* ready / serve: one pending task, approve commits, deny and cancel drop */ 105 ck_lc_init(&lc); 106 ck_lc_start(&lc, 1); 107 CHECK(lc.state == CK_LC_READY); 108 CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); 109 CHECK(req(&lc, "task draft the plan") == CK_LC_R_OK && lc.state == CK_LC_SERVE && lc.tasks == 1); 110 CHECK(req(&lc, "update") == CK_LC_R_REFUSED && lc.state == CK_LC_SERVE && strstr(lc.reason, "pending")); /* P11 cut 3 */ 111 CHECK(strcmp(lc.task, "draft the plan") == 0 && lc.task_len == 14); 112 CHECK(req(&lc, "task another") == CK_LC_R_REFUSED && strcmp(lc.task, "draft the plan") == 0); /* one at a time */ 113 CHECK(req(&lc, "approve") == CK_LC_R_COMMIT && lc.state == CK_LC_SERVE); 114 ck_lc_commit_done(&lc, -5); /* commit failed: still pending */ 115 CHECK(lc.state == CK_LC_SERVE && lc.approved == 0 && strcmp(lc.task, "draft the plan") == 0); 116 CHECK(req(&lc, "approve") == CK_LC_R_COMMIT); 117 ck_lc_commit_done(&lc, 0); 118 CHECK(lc.state == CK_LC_READY && lc.approved == 1 && lc.task_len == 0 && lc.task[0] == 0); 119 CHECK(req(&lc, "task b") == CK_LC_R_OK && req(&lc, "deny") == CK_LC_R_OK && lc.state == CK_LC_READY && lc.denied == 1); 120 CHECK(req(&lc, "task c") == CK_LC_R_OK && req(&lc, "cancel") == CK_LC_R_OK && lc.state == CK_LC_READY && lc.cancelled == 1); 121 CHECK(req(&lc, "nonsense") == CK_LC_R_REFUSED && req(&lc, "task") == CK_LC_R_INVALID && lc.invalid == 1); 122 /* a pending unapproved task is dropped by shutdown, never committed */ 123 CHECK(req(&lc, "task d") == CK_LC_R_OK && req(&lc, "shutdown") == CK_LC_R_STOP); 124 CHECK(lc.state == CK_LC_QUIESCE && lc.task_len == 0 && lc.approved == 1); 125 ck_lc_commit_done(&lc, 0); /* ignored outside SERVE */ 126 CHECK(lc.approved == 1); 127 CHECK(lc.requests == 15); /* 14 + the P11 cut 3 update refused in SERVE */ 128 129 /* P06: prompt and recall. Locked: both refused, nothing counted. */ 130 ck_lc_init(&lc); 131 ck_lc_start(&lc, 0); 132 CHECK(req(&lc, "prompt hi") == CK_LC_R_REFUSED && lc.prompts == 0 && strstr(lc.reason, "locked")); 133 CHECK(req(&lc, "recall") == CK_LC_R_REFUSED && lc.recalls == 0 && lc.refused == 2); 134 /* ready: prompt asks the kernel to generate (state stays READY, the 135 * prompt is the task text); the reply becomes the pending proposal */ 136 ck_lc_init(&lc); 137 ck_lc_start(&lc, 1); 138 CHECK(req(&lc, "prompt What is the capital of France?") == CK_LC_R_GENERATE && lc.state == CK_LC_READY && lc.prompts == 1); 139 CHECK(strcmp(lc.task, "What is the capital of France?") == 0 && lc.task_len == 30 && lc.pending == CK_LC_PENDING_NONE); 140 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, "Paris.", 6); 141 CHECK(lc.state == CK_LC_SERVE && lc.pending == CK_LC_PENDING_REPLY && lc.replies == 1); 142 CHECK(lc.reply_len == 6 && strcmp(lc.reply, "Paris.") == 0 && strcmp(lc.task, "What is the capital of France?") == 0); 143 CHECK(req(&lc, "prompt again") == CK_LC_R_REFUSED && lc.state == CK_LC_SERVE && lc.prompts == 1); /* one proposal at a time */ 144 CHECK(req(&lc, "task note") == CK_LC_R_REFUSED && lc.tasks == 0); 145 CHECK(req(&lc, "approve") == CK_LC_R_COMMIT && lc.pending == CK_LC_PENDING_REPLY); 146 ck_lc_commit_done(&lc, 0); 147 CHECK(lc.state == CK_LC_READY && lc.approved == 1 && lc.pending == CK_LC_PENDING_NONE); 148 CHECK(lc.reply_len == 0 && lc.reply[0] == 0 && lc.task_len == 0 && lc.task[0] == 0); 149 /* cancelled between steps: nothing pending, counted apart from `cancel` of a proposal */ 150 CHECK(req(&lc, "prompt Say hello.") == CK_LC_R_GENERATE); 151 ck_lc_generate_done(&lc, CK_LC_GEN_CANCELLED, 0, 0); 152 CHECK(lc.state == CK_LC_READY && lc.gen_cancelled == 1 && lc.cancelled == 0 && lc.task_len == 0 && lc.pending == CK_LC_PENDING_NONE); 153 /* failed, empty and overlong replies: nothing pending, gen_failed counts */ 154 CHECK(req(&lc, "prompt Say hello.") == CK_LC_R_GENERATE); 155 ck_lc_generate_done(&lc, CK_LC_GEN_FAILED, 0, 0); 156 CHECK(lc.state == CK_LC_READY && lc.gen_failed == 1 && lc.task_len == 0); 157 CHECK(req(&lc, "prompt x") == CK_LC_R_GENERATE); 158 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, "", 0); 159 CHECK(lc.state == CK_LC_READY && lc.gen_failed == 2); 160 { 161 char big[CK_LC_REPLY_MAX + 2]; 162 memset(big, 'r', sizeof big - 1); big[sizeof big - 1] = 0; 163 CHECK(req(&lc, "prompt x") == CK_LC_R_GENERATE); 164 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, big, CK_LC_REPLY_MAX + 1); 165 CHECK(lc.state == CK_LC_READY && lc.gen_failed == 3 && lc.reply_len == 0); 166 CHECK(req(&lc, "prompt x") == CK_LC_R_GENERATE); 167 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, big, CK_LC_REPLY_MAX); /* exactly the bound fits */ 168 CHECK(lc.state == CK_LC_SERVE && lc.reply_len == CK_LC_REPLY_MAX && lc.replies == 2); 169 } 170 /* a reply arriving with no prompt pending is ignored (kernel-internal misuse) */ 171 CHECK(req(&lc, "deny") == CK_LC_R_OK && lc.state == CK_LC_READY && lc.denied == 1 && lc.reply_len == 0); 172 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, "stray", 5); 173 CHECK(lc.state == CK_LC_READY && lc.replies == 2 && lc.pending == CK_LC_PENDING_NONE); 174 /* recall is answered in READY and in SERVE and never changes the state */ 175 CHECK(req(&lc, "recall") == CK_LC_R_RECALL && lc.recalls == 1 && lc.state == CK_LC_READY); 176 CHECK(req(&lc, "task n") == CK_LC_R_OK && req(&lc, "recall") == CK_LC_R_RECALL && lc.recalls == 2); 177 CHECK(lc.state == CK_LC_SERVE && lc.pending == CK_LC_PENDING_NOTE); 178 /* shutdown drops a pending reply, never commits it; then everything is refused */ 179 CHECK(req(&lc, "cancel") == CK_LC_R_OK && req(&lc, "prompt z") == CK_LC_R_GENERATE); 180 ck_lc_generate_done(&lc, CK_LC_GEN_REPLY, "zz", 2); 181 CHECK(lc.state == CK_LC_SERVE && req(&lc, "shutdown") == CK_LC_R_STOP); 182 CHECK(lc.state == CK_LC_QUIESCE && lc.reply_len == 0 && lc.task_len == 0 && lc.pending == CK_LC_PENDING_NONE); 183 CHECK(req(&lc, "prompt q") == CK_LC_R_REFUSED && req(&lc, "recall") == CK_LC_R_REFUSED); 184 CHECK(lc.requests == 18 && lc.prompts == 7 && lc.refused == 4 && lc.approved == 1); 185 186 /* P06 cut 4: remember and facts. Locked: both refused, nothing counted. */ 187 ck_lc_init(&lc); 188 ck_lc_start(&lc, 0); 189 CHECK(req(&lc, "remember prefer short notes") == CK_LC_R_REFUSED && lc.remembers == 0 && strstr(lc.reason, "locked")); 190 CHECK(req(&lc, "facts") == CK_LC_R_REFUSED && lc.facts == 0 && lc.refused == 2); 191 /* ready: remember hands the text to the binding and never changes the 192 * state or the pending task; facts likewise */ 193 ck_lc_init(&lc); 194 ck_lc_start(&lc, 1); 195 CHECK(req(&lc, "remember prefer short notes") == CK_LC_R_REMEMBER && lc.state == CK_LC_READY && lc.remembers == 1); 196 CHECK(lc.fact_len == 18 && strcmp(lc.fact, "prefer short notes") == 0 && lc.task_len == 0 && lc.pending == CK_LC_PENDING_NONE); 197 CHECK(req(&lc, "facts") == CK_LC_R_FACTS && lc.facts == 1 && lc.state == CK_LC_READY); 198 CHECK(req(&lc, "task n") == CK_LC_R_OK && lc.state == CK_LC_SERVE); 199 CHECK(req(&lc, "remember second") == CK_LC_R_REMEMBER && lc.remembers == 2 && lc.state == CK_LC_SERVE); 200 CHECK(strcmp(lc.fact, "second") == 0 && lc.fact_len == 6 && strcmp(lc.task, "n") == 0 && lc.pending == CK_LC_PENDING_NOTE); 201 CHECK(req(&lc, "facts") == CK_LC_R_FACTS && lc.facts == 2 && lc.state == CK_LC_SERVE); 202 { 203 char big[CK_LC_LINE_MAX + 1]; 204 memset(big, 'f', CK_LC_LINE_MAX); big[CK_LC_LINE_MAX] = 0; 205 memcpy(big, "remember ", 9); 206 CHECK(req(&lc, big) == CK_LC_R_INVALID && lc.invalid == 1 && lc.remembers == 2); /* text 87 > 48 refused whole */ 207 } 208 CHECK(req(&lc, "shutdown") == CK_LC_R_STOP && req(&lc, "remember z") == CK_LC_R_REFUSED && req(&lc, "facts") == CK_LC_R_REFUSED); 209 CHECK(lc.requests == 9 && lc.remembers == 2 && lc.facts == 2 && lc.refused == 2 && lc.invalid == 1); 210 211 /* N3 (aienos#329): prefer hands the text to the binding like remember; 212 * an approval the binding refuses (stale / changed) keeps SERVE; a replay 213 * drops the proposal to READY with nothing written. */ 214 CHECK(dec("prefer Address me as Captain Drake.", &a, &al) == CK_LC_V_PREFER && al == 28); 215 CHECK(dec("prefer", &a, &al) == CK_LC_V_INVALID); 216 CHECK(strcmp(ck_lc_verb_name(CK_LC_V_PREFER), "prefer") == 0); 217 ck_lc_init(&lc); 218 ck_lc_start(&lc, 0); 219 CHECK(req(&lc, "prefer x") == CK_LC_R_REFUSED && lc.prefers == 0 && strstr(lc.reason, "locked")); 220 ck_lc_init(&lc); 221 ck_lc_start(&lc, 1); 222 CHECK(req(&lc, "prefer approve") == CK_LC_R_PREFER && lc.prefers == 1 && lc.state == CK_LC_READY); 223 CHECK(strcmp(lc.fact, "approve") == 0 && lc.fact_len == 7 && lc.approved == 0 && lc.pending == CK_LC_PENDING_NONE); 224 CHECK(req(&lc, "task n") == CK_LC_R_OK && lc.state == CK_LC_SERVE); 225 CHECK(req(&lc, "prefer y") == CK_LC_R_PREFER && lc.state == CK_LC_SERVE && strcmp(lc.task, "n") == 0); 226 CHECK(req(&lc, "approve") == CK_LC_R_COMMIT && lc.state == CK_LC_SERVE); 227 ck_lc_commit_refused(&lc, "stale"); 228 CHECK(lc.state == CK_LC_SERVE && lc.bind_refused == 1 && lc.refused == 1 && strcmp(lc.reason, "stale") == 0); 229 CHECK(lc.pending == CK_LC_PENDING_NOTE && lc.approved == 0); 230 CHECK(req(&lc, "approve") == CK_LC_R_COMMIT); 231 ck_lc_commit_replay(&lc); 232 CHECK(lc.state == CK_LC_READY && lc.replays == 1 && lc.refused == 2 && lc.approved == 0 && lc.task_len == 0 && 233 lc.pending == CK_LC_PENDING_NONE && strstr(lc.reason, "replay")); 234 ck_lc_commit_replay(&lc); /* ignored outside SERVE */ 235 ck_lc_commit_refused(&lc, "ignored"); /* likewise */ 236 CHECK(lc.replays == 1 && lc.bind_refused == 1 && lc.refused == 2); 237 return ck_t_verdict("CK_LIFECYCLE"); 238 }