omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / kernel / core / lifecycle.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/core/lifecycle.om: Pure-Omega Resident Production Lifecycle FSM. |
| 3 | note Converted from upstream aienux native/kernel/core/lifecycle_fsm.h and lifecycle_fsm.c. |
| 4 | |
| 5 | note States |
| 6 | const LC_INIT uint 0 |
| 7 | const LC_LOCKED uint 1 |
| 8 | const LC_READY uint 2 |
| 9 | const LC_SERVE uint 3 |
| 10 | const LC_QUIESCE uint 4 |
| 11 | const LC_STOP uint 5 |
| 12 | |
| 13 | note Verbs |
| 14 | const LC_V_INVALID int -1 |
| 15 | const LC_V_UNKNOWN int 0 |
| 16 | const LC_V_EMPTY int 1 |
| 17 | const LC_V_HELP int 2 |
| 18 | const LC_V_STATUS int 3 |
| 19 | const LC_V_UNLOCK int 4 |
| 20 | const LC_V_TASK int 5 |
| 21 | const LC_V_APPROVE int 6 |
| 22 | const LC_V_DENY int 7 |
| 23 | const LC_V_CANCEL int 8 |
| 24 | const LC_V_EVIDENCE int 9 |
| 25 | const LC_V_SHUTDOWN int 10 |
| 26 | const LC_V_PROMPT int 11 |
| 27 | const LC_V_RECALL int 12 |
| 28 | const LC_V_REMEMBER int 13 |
| 29 | const LC_V_FACTS int 14 |
| 30 | const LC_V_ROTATE int 15 |
| 31 | const LC_V_UPDATE int 16 |
| 32 | const LC_V_PREFER int 17 |
| 33 | |
| 34 | note Pending kinds |
| 35 | const LC_PENDING_NONE uint 0 |
| 36 | const LC_PENDING_NOTE uint 1 |
| 37 | const LC_PENDING_REPLY uint 2 |
| 38 | |
| 39 | note Transition outcomes |
| 40 | const LC_R_OK uint 0 |
| 41 | const LC_R_REFUSED uint 1 |
| 42 | const LC_R_INVALID uint 2 |
| 43 | const LC_R_COMMIT uint 3 |
| 44 | const LC_R_UNLOCK uint 4 |
| 45 | const LC_R_STOP uint 5 |
| 46 | const LC_R_GENERATE uint 6 |
| 47 | const LC_R_RECALL uint 7 |
| 48 | const LC_R_REMEMBER uint 8 |
| 49 | const LC_R_FACTS uint 9 |
| 50 | const LC_R_ROTATE uint 10 |
| 51 | const LC_R_UPDATE uint 11 |
| 52 | const LC_R_PREFER uint 12 |
| 53 | |
| 54 | note Generation completion outcomes |
| 55 | const LC_GEN_REPLY uint 0 |
| 56 | const LC_GEN_CANCELLED uint 1 |
| 57 | const LC_GEN_FAILED uint 2 |
| 58 | |
| 59 | note Bounds |
| 60 | const LC_LINE_MAX ulong 144 |
| 61 | const LC_TASK_MAX ulong 48 |
| 62 | const LC_REPLY_MAX ulong 1536 |
| 63 | |
| 64 | note Counters indices in lc_cnt (18 u32s) |
| 65 | const LC_C_STATE ulong 0 |
| 66 | const LC_C_PENDING ulong 1 |
| 67 | const LC_C_TASK_LEN ulong 2 |
| 68 | const LC_C_REPLY_LEN ulong 3 |
| 69 | const LC_C_FACT_LEN ulong 4 |
| 70 | const LC_C_REQUESTS ulong 5 |
| 71 | const LC_C_REFUSED ulong 6 |
| 72 | const LC_C_INVALID ulong 7 |
| 73 | const LC_C_TASKS ulong 8 |
| 74 | const LC_C_APPROVED ulong 9 |
| 75 | const LC_C_DENIED ulong 10 |
| 76 | const LC_C_CANCELLED ulong 11 |
| 77 | const LC_C_PROMPTS ulong 12 |
| 78 | const LC_C_REPLIES ulong 13 |
| 79 | const LC_C_GEN_CANCEL ulong 14 |
| 80 | const LC_C_GEN_FAIL ulong 15 |
| 81 | const LC_C_RECALLS ulong 16 |
| 82 | const LC_C_REMEMBERS ulong 17 |
| 83 | const LC_C_FACTS ulong 18 |
| 84 | const LC_C_UPDATES ulong 19 |
| 85 | const LC_C_PREFERS ulong 20 |
| 86 | const LC_C_BIND_REF ulong 21 |
| 87 | const LC_C_REPLAYS ulong 22 |
| 88 | |
| 89 | global lc_cnt uint 24 |
| 90 | global lc_task uchar 48 |
| 91 | global lc_fact uchar 48 |
| 92 | global lc_reply uchar 1536 |
| 93 | |
| 94 | text lcw_task "task" |
| 95 | text lcw_prompt "prompt" |
| 96 | text lcw_remember "remember" |
| 97 | text lcw_prefer "prefer" |
| 98 | text lcw_unlock "unlock" |
| 99 | text lcw_rotate "rotate" |
| 100 | text lcw_update "update" |
| 101 | text lcw_help "help" |
| 102 | text lcw_status "status" |
| 103 | text lcw_approve "approve" |
| 104 | text lcw_deny "deny" |
| 105 | text lcw_cancel "cancel" |
| 106 | text lcw_recall "recall" |
| 107 | text lcw_facts "facts" |
| 108 | text lcw_evidence "evidence" |
| 109 | text lcw_shutdown "shutdown" |
| 110 | |
| 111 | fn lc_word_is line view uchar * ws ulong wlen ulong lit view uchar * to bool |
| 112 | if ne wlen len lit |
| 113 | ret false |
| 114 | end |
| 115 | i = ulong 0 |
| 116 | while lt i wlen bound 32 |
| 117 | if ne get line add ws i get lit i |
| 118 | ret false |
| 119 | end |
| 120 | i = add i 1 |
| 121 | end |
| 122 | ret true |
| 123 | end |
| 124 | |
| 125 | fn lc_decode_noarg line view uchar * ws ulong wn ulong to int |
| 126 | if lc_word_is line ws wn lcw_help |
| 127 | ret LC_V_HELP |
| 128 | end |
| 129 | if lc_word_is line ws wn lcw_status |
| 130 | ret LC_V_STATUS |
| 131 | end |
| 132 | if lc_word_is line ws wn lcw_approve |
| 133 | ret LC_V_APPROVE |
| 134 | end |
| 135 | if lc_word_is line ws wn lcw_deny |
| 136 | ret LC_V_DENY |
| 137 | end |
| 138 | if lc_word_is line ws wn lcw_cancel |
| 139 | ret LC_V_CANCEL |
| 140 | end |
| 141 | if lc_word_is line ws wn lcw_recall |
| 142 | ret LC_V_RECALL |
| 143 | end |
| 144 | if lc_word_is line ws wn lcw_facts |
| 145 | ret LC_V_FACTS |
| 146 | end |
| 147 | if lc_word_is line ws wn lcw_evidence |
| 148 | ret LC_V_EVIDENCE |
| 149 | end |
| 150 | if lc_word_is line ws wn lcw_shutdown |
| 151 | ret LC_V_SHUTDOWN |
| 152 | end |
| 153 | ret LC_V_UNKNOWN |
| 154 | end |
| 155 | |
| 156 | fn lc_decode line view uchar * arg_out edit LcArg to int |
| 157 | put arg_out off 0 |
| 158 | put arg_out alen 0 |
| 159 | n = len line |
| 160 | if gt n LC_LINE_MAX |
| 161 | ret LC_V_INVALID |
| 162 | end |
| 163 | i = ulong 0 |
| 164 | while lt i n bound 144 |
| 165 | ch = get line i |
| 166 | if or lt ch 0x20 gt ch 0x7e |
| 167 | ret LC_V_INVALID |
| 168 | end |
| 169 | i = add i 1 |
| 170 | end |
| 171 | i = ulong 0 |
| 172 | while lt i n bound 144 |
| 173 | if ne get line i 32 |
| 174 | break |
| 175 | end |
| 176 | i = add i 1 |
| 177 | end |
| 178 | if eq i n |
| 179 | ret LC_V_EMPTY |
| 180 | end |
| 181 | ws = i |
| 182 | while lt i n bound 144 |
| 183 | if eq get line i 32 |
| 184 | break |
| 185 | end |
| 186 | i = add i 1 |
| 187 | end |
| 188 | we = i |
| 189 | while lt i n bound 144 |
| 190 | if ne get line i 32 |
| 191 | break |
| 192 | end |
| 193 | i = add i 1 |
| 194 | end |
| 195 | aend = n |
| 196 | while gt aend i bound 144 |
| 197 | if ne get line sub aend 1 32 |
| 198 | break |
| 199 | end |
| 200 | aend = sub aend 1 |
| 201 | end |
| 202 | alen = sub aend i |
| 203 | put arg_out off i |
| 204 | put arg_out alen alen |
| 205 | wn = sub we ws |
| 206 | if lc_word_is line ws wn lcw_task |
| 207 | if or eq alen 0 gt alen LC_TASK_MAX |
| 208 | ret LC_V_INVALID |
| 209 | end |
| 210 | ret LC_V_TASK |
| 211 | end |
| 212 | if lc_word_is line ws wn lcw_prompt |
| 213 | if or eq alen 0 gt alen LC_TASK_MAX |
| 214 | ret LC_V_INVALID |
| 215 | end |
| 216 | ret LC_V_PROMPT |
| 217 | end |
| 218 | if lc_word_is line ws wn lcw_remember |
| 219 | if or eq alen 0 gt alen LC_TASK_MAX |
| 220 | ret LC_V_INVALID |
| 221 | end |
| 222 | ret LC_V_REMEMBER |
| 223 | end |
| 224 | if lc_word_is line ws wn lcw_prefer |
| 225 | if or eq alen 0 gt alen LC_TASK_MAX |
| 226 | ret LC_V_INVALID |
| 227 | end |
| 228 | ret LC_V_PREFER |
| 229 | end |
| 230 | if lc_word_is line ws wn lcw_unlock |
| 231 | ret LC_V_UNLOCK |
| 232 | end |
| 233 | if lc_word_is line ws wn lcw_rotate |
| 234 | ret LC_V_ROTATE |
| 235 | end |
| 236 | if lc_word_is line ws wn lcw_update |
| 237 | ret LC_V_UPDATE |
| 238 | end |
| 239 | if ne alen 0 |
| 240 | ret LC_V_INVALID |
| 241 | end |
| 242 | ret lc_decode_noarg line ws wn |
| 243 | end |
| 244 | |
| 245 | fn lc_init to void |
| 246 | for i ulong 24 |
| 247 | put lc_cnt i 0 |
| 248 | end |
| 249 | for i ulong 48 |
| 250 | put lc_task i 0 |
| 251 | put lc_fact i 0 |
| 252 | end |
| 253 | for i ulong 1536 |
| 254 | put lc_reply i 0 |
| 255 | end |
| 256 | put lc_cnt LC_C_STATE LC_INIT |
| 257 | end |
| 258 | |
| 259 | fn lc_start store_open bool to void |
| 260 | if store_open |
| 261 | put lc_cnt LC_C_STATE LC_READY |
| 262 | else |
| 263 | put lc_cnt LC_C_STATE LC_LOCKED |
| 264 | end |
| 265 | end |
| 266 | |
| 267 | fn lc_drop_task to void |
| 268 | for i ulong 48 |
| 269 | put lc_task i 0 |
| 270 | end |
| 271 | for i ulong 1536 |
| 272 | put lc_reply i 0 |
| 273 | end |
| 274 | put lc_cnt LC_C_TASK_LEN 0 |
| 275 | put lc_cnt LC_C_REPLY_LEN 0 |
| 276 | put lc_cnt LC_C_PENDING LC_PENDING_NONE |
| 277 | end |
| 278 | |
| 279 | fn lc_set_task arg view uchar * to void |
| 280 | n = len arg |
| 281 | i = ulong 0 |
| 282 | while lt i n bound 48 |
| 283 | put lc_task i get arg i |
| 284 | i = add i 1 |
| 285 | end |
| 286 | put lc_cnt LC_C_TASK_LEN uint n |
| 287 | end |
| 288 | |
| 289 | fn lc_set_fact arg view uchar * to void |
| 290 | n = len arg |
| 291 | i = ulong 0 |
| 292 | while lt i n bound 48 |
| 293 | put lc_fact i get arg i |
| 294 | i = add i 1 |
| 295 | end |
| 296 | put lc_cnt LC_C_FACT_LEN uint n |
| 297 | end |
| 298 | |
| 299 | fn lc_refuse to uint |
| 300 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 301 | ret LC_R_REFUSED |
| 302 | end |
| 303 | |
| 304 | fn lc_apply_ready_verbs verb int arg view uchar * st uint to uint |
| 305 | alen = len arg |
| 306 | if eq verb LC_V_TASK |
| 307 | if or eq st LC_LOCKED or eq st LC_SERVE or eq alen 0 gt alen LC_TASK_MAX |
| 308 | ret lc_refuse |
| 309 | end |
| 310 | lc_set_task arg |
| 311 | put lc_cnt LC_C_PENDING LC_PENDING_NOTE |
| 312 | put lc_cnt LC_C_TASKS add get lc_cnt LC_C_TASKS 1 |
| 313 | put lc_cnt LC_C_STATE LC_SERVE |
| 314 | ret LC_R_OK |
| 315 | end |
| 316 | if eq verb LC_V_PROMPT |
| 317 | if or eq st LC_LOCKED or eq st LC_SERVE or eq alen 0 gt alen LC_TASK_MAX |
| 318 | ret lc_refuse |
| 319 | end |
| 320 | lc_set_task arg |
| 321 | put lc_cnt LC_C_PROMPTS add get lc_cnt LC_C_PROMPTS 1 |
| 322 | ret LC_R_GENERATE |
| 323 | end |
| 324 | if eq verb LC_V_RECALL |
| 325 | if eq st LC_LOCKED |
| 326 | ret lc_refuse |
| 327 | end |
| 328 | put lc_cnt LC_C_RECALLS add get lc_cnt LC_C_RECALLS 1 |
| 329 | ret LC_R_RECALL |
| 330 | end |
| 331 | if eq verb LC_V_REMEMBER |
| 332 | if or eq st LC_LOCKED or eq alen 0 gt alen LC_TASK_MAX |
| 333 | ret lc_refuse |
| 334 | end |
| 335 | lc_set_fact arg |
| 336 | put lc_cnt LC_C_REMEMBERS add get lc_cnt LC_C_REMEMBERS 1 |
| 337 | ret LC_R_REMEMBER |
| 338 | end |
| 339 | if eq verb LC_V_PREFER |
| 340 | if or eq st LC_LOCKED or eq alen 0 gt alen LC_TASK_MAX |
| 341 | ret lc_refuse |
| 342 | end |
| 343 | lc_set_fact arg |
| 344 | put lc_cnt LC_C_PREFERS add get lc_cnt LC_C_PREFERS 1 |
| 345 | ret LC_R_PREFER |
| 346 | end |
| 347 | if eq verb LC_V_FACTS |
| 348 | if eq st LC_LOCKED |
| 349 | ret lc_refuse |
| 350 | end |
| 351 | put lc_cnt LC_C_FACTS add get lc_cnt LC_C_FACTS 1 |
| 352 | ret LC_R_FACTS |
| 353 | end |
| 354 | ret lc_refuse |
| 355 | end |
| 356 | |
| 357 | fn lc_apply verb int arg view uchar * to uint |
| 358 | put lc_cnt LC_C_REQUESTS add get lc_cnt LC_C_REQUESTS 1 |
| 359 | st = get lc_cnt LC_C_STATE |
| 360 | if or eq st LC_QUIESCE or eq st LC_STOP eq st LC_INIT |
| 361 | ret lc_refuse |
| 362 | end |
| 363 | if eq verb LC_V_INVALID |
| 364 | put lc_cnt LC_C_INVALID add get lc_cnt LC_C_INVALID 1 |
| 365 | ret LC_R_INVALID |
| 366 | end |
| 367 | if eq verb LC_V_UNKNOWN |
| 368 | ret lc_refuse |
| 369 | end |
| 370 | if or eq verb LC_V_EMPTY or eq verb LC_V_HELP or eq verb LC_V_STATUS eq verb LC_V_EVIDENCE |
| 371 | ret LC_R_OK |
| 372 | end |
| 373 | if eq verb LC_V_UNLOCK |
| 374 | if ne st LC_LOCKED |
| 375 | ret lc_refuse |
| 376 | end |
| 377 | ret LC_R_UNLOCK |
| 378 | end |
| 379 | if eq verb LC_V_ROTATE |
| 380 | if or eq st LC_LOCKED or eq st LC_SERVE eq len arg 0 |
| 381 | ret lc_refuse |
| 382 | end |
| 383 | ret LC_R_ROTATE |
| 384 | end |
| 385 | if eq verb LC_V_UPDATE |
| 386 | if or eq st LC_LOCKED eq st LC_SERVE |
| 387 | ret lc_refuse |
| 388 | end |
| 389 | put lc_cnt LC_C_UPDATES add get lc_cnt LC_C_UPDATES 1 |
| 390 | ret LC_R_UPDATE |
| 391 | end |
| 392 | if eq verb LC_V_APPROVE |
| 393 | if ne st LC_SERVE |
| 394 | ret lc_refuse |
| 395 | end |
| 396 | ret LC_R_COMMIT |
| 397 | end |
| 398 | if eq verb LC_V_DENY |
| 399 | if ne st LC_SERVE |
| 400 | ret lc_refuse |
| 401 | end |
| 402 | put lc_cnt LC_C_DENIED add get lc_cnt LC_C_DENIED 1 |
| 403 | lc_drop_task |
| 404 | put lc_cnt LC_C_STATE LC_READY |
| 405 | ret LC_R_OK |
| 406 | end |
| 407 | if eq verb LC_V_CANCEL |
| 408 | if ne st LC_SERVE |
| 409 | ret lc_refuse |
| 410 | end |
| 411 | put lc_cnt LC_C_CANCELLED add get lc_cnt LC_C_CANCELLED 1 |
| 412 | lc_drop_task |
| 413 | put lc_cnt LC_C_STATE LC_READY |
| 414 | ret LC_R_OK |
| 415 | end |
| 416 | if eq verb LC_V_SHUTDOWN |
| 417 | if eq st LC_SERVE |
| 418 | lc_drop_task |
| 419 | end |
| 420 | put lc_cnt LC_C_STATE LC_QUIESCE |
| 421 | ret LC_R_STOP |
| 422 | end |
| 423 | ret lc_apply_ready_verbs verb arg st |
| 424 | end |
| 425 | |
| 426 | fn lc_commit_done rc int to void |
| 427 | if ne get lc_cnt LC_C_STATE LC_SERVE |
| 428 | ret |
| 429 | end |
| 430 | if eq rc 0 |
| 431 | put lc_cnt LC_C_APPROVED add get lc_cnt LC_C_APPROVED 1 |
| 432 | lc_drop_task |
| 433 | put lc_cnt LC_C_STATE LC_READY |
| 434 | else |
| 435 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 436 | end |
| 437 | end |
| 438 | |
| 439 | fn lc_commit_replay to void |
| 440 | if ne get lc_cnt LC_C_STATE LC_SERVE |
| 441 | ret |
| 442 | end |
| 443 | put lc_cnt LC_C_REPLAYS add get lc_cnt LC_C_REPLAYS 1 |
| 444 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 445 | lc_drop_task |
| 446 | put lc_cnt LC_C_STATE LC_READY |
| 447 | end |
| 448 | |
| 449 | fn lc_commit_refused to void |
| 450 | if ne get lc_cnt LC_C_STATE LC_SERVE |
| 451 | ret |
| 452 | end |
| 453 | put lc_cnt LC_C_BIND_REF add get lc_cnt LC_C_BIND_REF 1 |
| 454 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 455 | end |
| 456 | |
| 457 | fn lc_unlock_done opened bool to void |
| 458 | if ne get lc_cnt LC_C_STATE LC_LOCKED |
| 459 | ret |
| 460 | end |
| 461 | if opened |
| 462 | put lc_cnt LC_C_STATE LC_READY |
| 463 | else |
| 464 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 465 | end |
| 466 | end |
| 467 | |
| 468 | fn lc_rotate_done ok bool to void |
| 469 | if ne get lc_cnt LC_C_STATE LC_READY |
| 470 | ret |
| 471 | end |
| 472 | if not ok |
| 473 | put lc_cnt LC_C_REFUSED add get lc_cnt LC_C_REFUSED 1 |
| 474 | end |
| 475 | end |
| 476 | |
| 477 | fn lc_generate_done outcome uint reply view uchar * to void |
| 478 | if or ne get lc_cnt LC_C_STATE LC_READY eq get lc_cnt LC_C_TASK_LEN 0 |
| 479 | ret |
| 480 | end |
| 481 | rlen = len reply |
| 482 | if and eq outcome LC_GEN_REPLY and gt rlen 0 le rlen LC_REPLY_MAX |
| 483 | i = ulong 0 |
| 484 | while lt i rlen bound 1536 |
| 485 | put lc_reply i get reply i |
| 486 | i = add i 1 |
| 487 | end |
| 488 | put lc_cnt LC_C_REPLY_LEN uint rlen |
| 489 | put lc_cnt LC_C_PENDING LC_PENDING_REPLY |
| 490 | put lc_cnt LC_C_REPLIES add get lc_cnt LC_C_REPLIES 1 |
| 491 | put lc_cnt LC_C_STATE LC_SERVE |
| 492 | ret |
| 493 | end |
| 494 | if eq outcome LC_GEN_CANCELLED |
| 495 | put lc_cnt LC_C_GEN_CANCEL add get lc_cnt LC_C_GEN_CANCEL 1 |
| 496 | else |
| 497 | put lc_cnt LC_C_GEN_FAIL add get lc_cnt LC_C_GEN_FAIL 1 |
| 498 | end |
| 499 | lc_drop_task |
| 500 | end |
| 501 | |
| 502 | fn lc_stopped to void |
| 503 | put lc_cnt LC_C_STATE LC_STOP |
| 504 | end |
| 505 | |
| 506 | fn lc_get_cnt idx ulong to uint |
| 507 | if ge idx 24 |
| 508 | ret 0 |
| 509 | end |
| 510 | ret get lc_cnt idx |
| 511 | end |