aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / om / journal.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/aegis/journal.om: Append-only SHA-256 hash-chained event/task journal (pure OM-2 library unit). |
| 3 | note Composes with om/tls/sha2.om (FIPS 180-4 SHA-256) and core/aegis/dag.om for replay. |
| 4 | note Guarantees: strict sequence monotonicity (1..N), cryptographic hash chaining from genesis, |
| 5 | note replay reconstruction into DAG state (refusing tampered/torn logs), and binary wire |
| 6 | note encoding/decoding with torn-record and magic/commit sentinel detection. |
| 7 | |
| 8 | const JRN_MAX_RECORDS ulong 128 |
| 9 | const JRN_PAYLOAD_MAX ulong 64 |
| 10 | const JRN_WIRE_REC_SIZE ulong 192 |
| 11 | |
| 12 | const JRN_MAGIC ulong 0x4145474a |
| 13 | const JRN_COMMIT_END ulong 0x454e4431 |
| 14 | |
| 15 | note Journal event kinds |
| 16 | const EVT_TASK_SUBMITTED ulong 1 |
| 17 | const EVT_TASK_DEP ulong 2 |
| 18 | const EVT_TASK_STARTED ulong 3 |
| 19 | const EVT_TASK_DONE ulong 4 |
| 20 | const EVT_TASK_FAILED ulong 5 |
| 21 | const EVT_TASK_BLOCKED ulong 6 |
| 22 | const EVT_TASK_CANCELLED ulong 7 |
| 23 | const EVT_LEASE_ACQUIRED ulong 8 |
| 24 | const EVT_LEASE_RECLAIMED ulong 9 |
| 25 | const EVT_HEARTBEAT_TICK ulong 10 |
| 26 | const EVT_POLICY_DENIED ulong 11 |
| 27 | |
| 28 | note Journal status and integrity error codes |
| 29 | const JRN_OK ulong 0 |
| 30 | const JRN_ERR_FULL ulong 1 |
| 31 | const JRN_ERR_PAYLOAD_TOO_LONG ulong 2 |
| 32 | const JRN_ERR_BAD_MAGIC ulong 3 |
| 33 | const JRN_ERR_SEQ_GAP ulong 4 |
| 34 | const JRN_ERR_PREV_HASH_MISMATCH ulong 5 |
| 35 | const JRN_ERR_HASH_MISMATCH ulong 6 |
| 36 | const JRN_ERR_TORN_RECORD ulong 7 |
| 37 | const JRN_ERR_NOT_FOUND ulong 8 |
| 38 | |
| 39 | note jrn_meta: [0]=record_cnt, [1]=last_seq |
| 40 | global jrn_meta ulong 8 |
| 41 | global jrn_magic ulong 128 |
| 42 | global jrn_seq ulong 128 |
| 43 | global jrn_tick ulong 128 |
| 44 | global jrn_kind ulong 128 |
| 45 | global jrn_task_id ulong 128 |
| 46 | global jrn_aux ulong 128 |
| 47 | global jrn_plen ulong 128 |
| 48 | global jrn_commit ulong 128 |
| 49 | global jrn_payload uchar 8192 |
| 50 | global jrn_prev_hash uchar 4096 |
| 51 | global jrn_hash uchar 4096 |
| 52 | |
| 53 | text jrn_domain "AEGIS-JRN-V1" |
| 54 | |
| 55 | fn jrn_reset to void |
| 56 | for i ulong 8 |
| 57 | put jrn_meta i 0 |
| 58 | end |
| 59 | for i ulong 128 |
| 60 | put jrn_magic i 0 |
| 61 | put jrn_seq i 0 |
| 62 | put jrn_tick i 0 |
| 63 | put jrn_kind i 0 |
| 64 | put jrn_task_id i 0 |
| 65 | put jrn_aux i 0 |
| 66 | put jrn_plen i 0 |
| 67 | put jrn_commit i 0 |
| 68 | end |
| 69 | for i ulong 8192 |
| 70 | put jrn_payload i 0 |
| 71 | end |
| 72 | for i ulong 4096 |
| 73 | put jrn_prev_hash i 0 |
| 74 | put jrn_hash i 0 |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | fn jrn_count to ulong |
| 79 | ret get jrn_meta 0 |
| 80 | end |
| 81 | |
| 82 | fn jrn_last_seq to ulong |
| 83 | ret get jrn_meta 1 |
| 84 | end |
| 85 | |
| 86 | fn jrn_rec_seq idx ulong to ulong |
| 87 | if ge idx get jrn_meta 0 |
| 88 | ret 0 |
| 89 | end |
| 90 | ret get jrn_seq idx |
| 91 | end |
| 92 | |
| 93 | fn jrn_rec_tick idx ulong to ulong |
| 94 | if ge idx get jrn_meta 0 |
| 95 | ret 0 |
| 96 | end |
| 97 | ret get jrn_tick idx |
| 98 | end |
| 99 | |
| 100 | fn jrn_rec_kind idx ulong to ulong |
| 101 | if ge idx get jrn_meta 0 |
| 102 | ret 0 |
| 103 | end |
| 104 | ret get jrn_kind idx |
| 105 | end |
| 106 | |
| 107 | fn jrn_rec_task_id idx ulong to ulong |
| 108 | if ge idx get jrn_meta 0 |
| 109 | ret 0 |
| 110 | end |
| 111 | ret get jrn_task_id idx |
| 112 | end |
| 113 | |
| 114 | fn jrn_rec_aux idx ulong to ulong |
| 115 | if ge idx get jrn_meta 0 |
| 116 | ret 0 |
| 117 | end |
| 118 | ret get jrn_aux idx |
| 119 | end |
| 120 | |
| 121 | fn jrn_copy_hash idx ulong dst edit uchar 32 to ulong |
| 122 | if ge idx get jrn_meta 0 |
| 123 | ret JRN_ERR_NOT_FOUND |
| 124 | end |
| 125 | base = mul idx 32 |
| 126 | for i ulong 32 |
| 127 | put dst i get jrn_hash add base i |
| 128 | end |
| 129 | ret JRN_OK |
| 130 | end |
| 131 | |
| 132 | fn jrn_put_be32 buf edit uchar * off ulong v ulong to void |
| 133 | put buf add off 0 uchar and shr v 24 0xff |
| 134 | put buf add off 1 uchar and shr v 16 0xff |
| 135 | put buf add off 2 uchar and shr v 8 0xff |
| 136 | put buf add off 3 uchar and v 0xff |
| 137 | end |
| 138 | |
| 139 | fn jrn_put_be64 buf edit uchar * off ulong v ulong to void |
| 140 | put buf add off 0 uchar and shr v 56 0xff |
| 141 | put buf add off 1 uchar and shr v 48 0xff |
| 142 | put buf add off 2 uchar and shr v 40 0xff |
| 143 | put buf add off 3 uchar and shr v 32 0xff |
| 144 | put buf add off 4 uchar and shr v 24 0xff |
| 145 | put buf add off 5 uchar and shr v 16 0xff |
| 146 | put buf add off 6 uchar and shr v 8 0xff |
| 147 | put buf add off 7 uchar and v 0xff |
| 148 | end |
| 149 | |
| 150 | fn jrn_get_be32 buf view uchar * off ulong to ulong |
| 151 | b0 = ulong get buf add off 0 |
| 152 | b1 = ulong get buf add off 1 |
| 153 | b2 = ulong get buf add off 2 |
| 154 | b3 = ulong get buf add off 3 |
| 155 | ret or shl b0 24 or shl b1 16 or shl b2 8 b3 |
| 156 | end |
| 157 | |
| 158 | fn jrn_get_be64 buf view uchar * off ulong to ulong |
| 159 | hi = jrn_get_be32 buf off |
| 160 | lo = jrn_get_be32 buf add off 4 |
| 161 | ret or shl hi 32 lo |
| 162 | end |
| 163 | |
| 164 | note Compute canonical SHA-256 digest of record idx into dst_hash[32]. |
| 165 | note Covers: domain(12) | magic(4) | seq(8) | tick(8) | kind(8) | task_id(8) | aux(8) | |
| 166 | note plen(4) | prev_hash(32) | commit(4) | payload(plen). |
| 167 | fn jrn_compute_record_hash idx ulong dst_hash edit uchar 32 to void |
| 168 | ctx = new ulong 40 0 |
| 169 | hdr = new uchar 96 0 |
| 170 | pl = new uchar 64 0 |
| 171 | for i ulong 12 |
| 172 | put hdr i get jrn_domain i |
| 173 | end |
| 174 | jrn_put_be32 hdr 12 get jrn_magic idx |
| 175 | jrn_put_be64 hdr 16 get jrn_seq idx |
| 176 | jrn_put_be64 hdr 24 get jrn_tick idx |
| 177 | jrn_put_be64 hdr 32 get jrn_kind idx |
| 178 | jrn_put_be64 hdr 40 get jrn_task_id idx |
| 179 | jrn_put_be64 hdr 48 get jrn_aux idx |
| 180 | plen = get jrn_plen idx |
| 181 | jrn_put_be32 hdr 56 plen |
| 182 | hbase = mul idx 32 |
| 183 | for i ulong 32 |
| 184 | put hdr add 60 i get jrn_prev_hash add hbase i |
| 185 | end |
| 186 | jrn_put_be32 hdr 92 get jrn_commit idx |
| 187 | sha256_init ctx |
| 188 | sha256_update ctx part hdr 0 96 |
| 189 | if and gt plen 0 le plen JRN_PAYLOAD_MAX |
| 190 | pbase = mul idx JRN_PAYLOAD_MAX |
| 191 | for i ulong plen bound 64 |
| 192 | put pl i get jrn_payload add pbase i |
| 193 | end |
| 194 | sha256_update ctx part pl 0 plen |
| 195 | end |
| 196 | sha256_final ctx dst_hash |
| 197 | for i ulong 40 |
| 198 | put ctx i 0 |
| 199 | end |
| 200 | end |
| 201 | |
| 202 | fn jrn_store_payload idx ulong payload view uchar * to void |
| 203 | pbase = mul idx JRN_PAYLOAD_MAX |
| 204 | for i ulong JRN_PAYLOAD_MAX |
| 205 | put jrn_payload add pbase i 0 |
| 206 | end |
| 207 | plen = len payload |
| 208 | for i ulong plen bound 64 |
| 209 | put jrn_payload add pbase i get payload i |
| 210 | end |
| 211 | end |
| 212 | |
| 213 | fn jrn_link_and_seal idx ulong to void |
| 214 | dig = new uchar 32 0 |
| 215 | hbase = mul idx 32 |
| 216 | if eq idx 0 |
| 217 | for i ulong 32 |
| 218 | put jrn_prev_hash add hbase i 0 |
| 219 | end |
| 220 | else |
| 221 | prev_base = mul sub idx 1 32 |
| 222 | for i ulong 32 |
| 223 | put jrn_prev_hash add hbase i get jrn_hash add prev_base i |
| 224 | end |
| 225 | end |
| 226 | jrn_compute_record_hash idx dig |
| 227 | for i ulong 32 |
| 228 | put jrn_hash add hbase i get dig i |
| 229 | end |
| 230 | end |
| 231 | |
| 232 | note Append a new hash-chained event record. Returns JRN_OK or error code. |
| 233 | fn jrn_append kind ulong task_id ulong aux ulong tick ulong payload view uchar * to ulong |
| 234 | idx = get jrn_meta 0 |
| 235 | if ge idx JRN_MAX_RECORDS |
| 236 | ret JRN_ERR_FULL |
| 237 | end |
| 238 | plen = len payload |
| 239 | if gt plen JRN_PAYLOAD_MAX |
| 240 | ret JRN_ERR_PAYLOAD_TOO_LONG |
| 241 | end |
| 242 | seq = add get jrn_meta 1 1 |
| 243 | put jrn_magic idx JRN_MAGIC |
| 244 | put jrn_seq idx seq |
| 245 | put jrn_tick idx tick |
| 246 | put jrn_kind idx kind |
| 247 | put jrn_task_id idx task_id |
| 248 | put jrn_aux idx aux |
| 249 | put jrn_plen idx plen |
| 250 | put jrn_commit idx JRN_COMMIT_END |
| 251 | jrn_store_payload idx payload |
| 252 | jrn_link_and_seal idx |
| 253 | put jrn_meta 0 add idx 1 |
| 254 | put jrn_meta 1 seq |
| 255 | ret JRN_OK |
| 256 | end |
| 257 | |
| 258 | note Verify a single record's prev_hash linkage against record idx-1 (or all-zero genesis). |
| 259 | fn jrn_check_prev_link idx ulong to bool |
| 260 | hbase = mul idx 32 |
| 261 | if eq idx 0 |
| 262 | for i ulong 32 |
| 263 | if ne get jrn_prev_hash add hbase i 0 |
| 264 | ret false |
| 265 | end |
| 266 | end |
| 267 | ret true |
| 268 | end |
| 269 | prev_base = mul sub idx 1 32 |
| 270 | for i ulong 32 |
| 271 | if ne get jrn_prev_hash add hbase i get jrn_hash add prev_base i |
| 272 | ret false |
| 273 | end |
| 274 | end |
| 275 | ret true |
| 276 | end |
| 277 | |
| 278 | note Full cryptographic chain audit: checks magic, commit sentinel, strict sequence |
| 279 | note monotonicity (1..N), payload length bounds, prev_hash continuity, and SHA-256 integrity. |
| 280 | fn jrn_verify_chain to ulong |
| 281 | n = get jrn_meta 0 |
| 282 | dig = new uchar 32 0 |
| 283 | for i ulong n bound 128 |
| 284 | if ne get jrn_magic i JRN_MAGIC |
| 285 | ret JRN_ERR_BAD_MAGIC |
| 286 | end |
| 287 | if ne get jrn_commit i JRN_COMMIT_END |
| 288 | ret JRN_ERR_TORN_RECORD |
| 289 | end |
| 290 | if ne get jrn_seq i add i 1 |
| 291 | ret JRN_ERR_SEQ_GAP |
| 292 | end |
| 293 | if gt get jrn_plen i JRN_PAYLOAD_MAX |
| 294 | ret JRN_ERR_TORN_RECORD |
| 295 | end |
| 296 | if not jrn_check_prev_link i |
| 297 | ret JRN_ERR_PREV_HASH_MISMATCH |
| 298 | end |
| 299 | jrn_compute_record_hash i dig |
| 300 | hbase = mul i 32 |
| 301 | for b ulong 32 |
| 302 | if ne get dig b get jrn_hash add hbase b |
| 303 | ret JRN_ERR_HASH_MISMATCH |
| 304 | end |
| 305 | end |
| 306 | end |
| 307 | ret JRN_OK |
| 308 | end |
| 309 | |
| 310 | note Replay verified journal into DAG state. Refuses to replay if chain verification fails. |
| 311 | fn jrn_replay_to_dag to ulong |
| 312 | chk = jrn_verify_chain |
| 313 | if ne chk JRN_OK |
| 314 | ret chk |
| 315 | end |
| 316 | dag_reset |
| 317 | n = get jrn_meta 0 |
| 318 | for i ulong n bound 128 |
| 319 | k = get jrn_kind i |
| 320 | tid = get jrn_task_id i |
| 321 | ax = get jrn_aux i |
| 322 | if eq k EVT_TASK_SUBMITTED |
| 323 | prio = and ax 0xffffffff |
| 324 | tkind = shr ax 32 |
| 325 | rc = dag_add_task tid prio tkind |
| 326 | if ne rc DAG_OK |
| 327 | ret JRN_ERR_TORN_RECORD |
| 328 | end |
| 329 | end |
| 330 | if eq k EVT_TASK_DEP |
| 331 | rc = dag_add_dep tid ax |
| 332 | if ne rc DAG_OK |
| 333 | ret JRN_ERR_TORN_RECORD |
| 334 | end |
| 335 | end |
| 336 | if eq k EVT_TASK_STARTED |
| 337 | rdy = dag_solve_ready |
| 338 | if gt rdy DAG_MAX_TASKS |
| 339 | ret JRN_ERR_TORN_RECORD |
| 340 | end |
| 341 | rc = dag_start_task tid |
| 342 | if ne rc DAG_OK |
| 343 | ret JRN_ERR_TORN_RECORD |
| 344 | end |
| 345 | end |
| 346 | if eq k EVT_TASK_DONE |
| 347 | rc = dag_complete_task tid |
| 348 | if ne rc DAG_OK |
| 349 | ret JRN_ERR_TORN_RECORD |
| 350 | end |
| 351 | end |
| 352 | if eq k EVT_TASK_FAILED |
| 353 | rdy = dag_solve_ready |
| 354 | if gt rdy DAG_MAX_TASKS |
| 355 | ret JRN_ERR_TORN_RECORD |
| 356 | end |
| 357 | rc = dag_fail_task tid ax |
| 358 | if ne rc DAG_OK |
| 359 | ret JRN_ERR_TORN_RECORD |
| 360 | end |
| 361 | end |
| 362 | if eq k EVT_TASK_CANCELLED |
| 363 | rc = dag_cancel_task tid |
| 364 | if ne rc DAG_OK |
| 365 | ret JRN_ERR_TORN_RECORD |
| 366 | end |
| 367 | end |
| 368 | if eq k EVT_LEASE_RECLAIMED |
| 369 | if eq dag_get_state tid TASK_RUNNING |
| 370 | rc = dag_requeue_task tid |
| 371 | if ne rc DAG_OK |
| 372 | ret JRN_ERR_TORN_RECORD |
| 373 | end |
| 374 | end |
| 375 | end |
| 376 | end |
| 377 | rdy2 = dag_solve_ready |
| 378 | if gt rdy2 DAG_MAX_TASKS |
| 379 | ret JRN_ERR_TORN_RECORD |
| 380 | end |
| 381 | ret JRN_OK |
| 382 | end |
| 383 | |
| 384 | note Encode record idx into 192-byte wire format at dst[off..off+192]. |
| 385 | note Layout: magic(4) | seq(8) | tick(8) | kind(8) | task_id(8) | aux(8) | plen(4) | |
| 386 | note payload(64) | prev_hash(32) | hash(32) | commit(4) | reserved(12) = 192 bytes. |
| 387 | fn jrn_encode_record idx ulong dst edit uchar * off ulong to ulong |
| 388 | if ge idx get jrn_meta 0 |
| 389 | ret JRN_ERR_NOT_FOUND |
| 390 | end |
| 391 | if gt add off JRN_WIRE_REC_SIZE len dst |
| 392 | ret JRN_ERR_TORN_RECORD |
| 393 | end |
| 394 | jrn_put_be32 dst add off 0 get jrn_magic idx |
| 395 | jrn_put_be64 dst add off 4 get jrn_seq idx |
| 396 | jrn_put_be64 dst add off 12 get jrn_tick idx |
| 397 | jrn_put_be64 dst add off 20 get jrn_kind idx |
| 398 | jrn_put_be64 dst add off 28 get jrn_task_id idx |
| 399 | jrn_put_be64 dst add off 36 get jrn_aux idx |
| 400 | jrn_put_be32 dst add off 44 get jrn_plen idx |
| 401 | pbase = mul idx JRN_PAYLOAD_MAX |
| 402 | for i ulong JRN_PAYLOAD_MAX |
| 403 | put dst add off add 48 i get jrn_payload add pbase i |
| 404 | end |
| 405 | hbase = mul idx 32 |
| 406 | for i ulong 32 |
| 407 | put dst add off add 112 i get jrn_prev_hash add hbase i |
| 408 | put dst add off add 144 i get jrn_hash add hbase i |
| 409 | end |
| 410 | jrn_put_be32 dst add off 176 get jrn_commit idx |
| 411 | for i ulong 12 |
| 412 | put dst add off add 180 i 0 |
| 413 | end |
| 414 | ret JRN_OK |
| 415 | end |
| 416 | |
| 417 | fn jrn_decode_one idx ulong src view uchar * off ulong to void |
| 418 | put jrn_magic idx jrn_get_be32 src add off 0 |
| 419 | put jrn_seq idx jrn_get_be64 src add off 4 |
| 420 | put jrn_tick idx jrn_get_be64 src add off 12 |
| 421 | put jrn_kind idx jrn_get_be64 src add off 20 |
| 422 | put jrn_task_id idx jrn_get_be64 src add off 28 |
| 423 | put jrn_aux idx jrn_get_be64 src add off 36 |
| 424 | put jrn_plen idx jrn_get_be32 src add off 44 |
| 425 | pbase = mul idx JRN_PAYLOAD_MAX |
| 426 | for i ulong JRN_PAYLOAD_MAX |
| 427 | put jrn_payload add pbase i get src add off add 48 i |
| 428 | end |
| 429 | hbase = mul idx 32 |
| 430 | for i ulong 32 |
| 431 | put jrn_prev_hash add hbase i get src add off add 112 i |
| 432 | put jrn_hash add hbase i get src add off add 144 i |
| 433 | end |
| 434 | put jrn_commit idx jrn_get_be32 src add off 176 |
| 435 | end |
| 436 | |
| 437 | note Decode a binary journal stream of nbytes from src into the journal and verify its chain. |
| 438 | note Refuses truncated/torn trailing bytes (nbytes % 192 != 0), bad magic, missing commit, or hash mismatch. |
| 439 | fn jrn_decode_stream src view uchar * nbytes ulong to ulong |
| 440 | if gt nbytes len src |
| 441 | ret JRN_ERR_TORN_RECORD |
| 442 | end |
| 443 | if ne rem nbytes JRN_WIRE_REC_SIZE 0 |
| 444 | ret JRN_ERR_TORN_RECORD |
| 445 | end |
| 446 | nrec = div nbytes JRN_WIRE_REC_SIZE |
| 447 | if gt nrec JRN_MAX_RECORDS |
| 448 | ret JRN_ERR_FULL |
| 449 | end |
| 450 | jrn_reset |
| 451 | for idx ulong nrec bound 128 |
| 452 | jrn_decode_one idx src mul idx JRN_WIRE_REC_SIZE |
| 453 | end |
| 454 | put jrn_meta 0 nrec |
| 455 | if gt nrec 0 |
| 456 | put jrn_meta 1 get jrn_seq sub nrec 1 |
| 457 | end |
| 458 | ret jrn_verify_chain |
| 459 | end |