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/omega-mv/blob/main/seed/ng/replay/tok_journal.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / ng / replay / tok_journal.om` 659 lines ยท 12802 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/ng/replay/tok_journal.om: Hash-chained tok-boundary effect journal ("OTKJ" v1). 3 note Pure seeded OM-2. Requires seed/ng/sha256.om linked earlier (for sh_dat, sh_all, sh_dig). 4 note 5 note Wire format ("OTKJ" v1, little-endian, no padding): 6 note Header (16 bytes): 7 note [0..3] magic "OTKJ" (79 84 75 74) 8 note [4] version = 1 9 note [5] flags = 0 (reserved, must be 0) 10 note [6..7] reserved = 0 (must be 0) 11 note [8..15] entry_count u64 12 note Entry (85 + payload_len bytes): 13 note [0..7] seq u64 (strictly monotonic 0, 1, 2, ...) 14 note [8] op_kind u8 (closed set 1..13) 15 note [9..12] block_id u32 16 note [13..16] op_idx u32 17 note [17..20] payload_len u32 18 note [21..] payload_bytes[payload_len] 19 note [+0..31] prev_hash[32] (zeros for seq 0, else previous entry_hash) 20 note [+32..63] entry_hash[32] = SHA-256("OTKJ1\0" || seq || op_kind || block_id || op_idx || payload_len || payload_bytes || prev_hash) 21 note 22 note Growable run-time pools (no arbitrary small hard cap) + direct disk streaming + alarm threshold. 23 24 const TKJ_OK ulong 0 25 const TKJ_ERR_MAGIC ulong 1 26 const TKJ_ERR_VER ulong 2 27 const TKJ_ERR_TRUNC ulong 3 28 const TKJ_ERR_TRAILING ulong 4 29 const TKJ_ERR_SEQ ulong 5 30 const TKJ_ERR_KIND ulong 6 31 const TKJ_ERR_CHAIN ulong 7 32 const TKJ_ERR_HASH ulong 8 33 const TKJ_ERR_IO ulong 9 34 const TKJ_ERR_NOMEM ulong 10 35 36 const TKJ_HDR_LEN ulong 16 37 const TKJ_ENT_FIXED ulong 85 38 39 note Closed set of tok-crossing event kinds (1..13) 40 const TKJ_OP_CHECK ulong 1 41 const TKJ_OP_STORE ulong 2 42 const TKJ_OP_ATOM ulong 3 43 const TKJ_OP_ALLOCA ulong 4 44 const TKJ_OP_CALL ulong 5 45 const TKJ_OP_HOST ulong 6 46 const TKJ_OP_POOL ulong 7 47 const TKJ_OP_TRAP ulong 8 48 const TKJ_OP_EXIT ulong 9 49 const TKJ_OP_POWER ulong 10 50 const TKJ_OP_NET ulong 11 51 const TKJ_OP_SENSOR ulong 12 52 const TKJ_OP_CKPT_MARK ulong 13 53 const TKJ_OP_MAX ulong 13 54 55 text tj_magic "OTKJ" 56 text tj_dom "OTKJ1" 57 58 note Growable run-time pools (unbounded by any small fixed cap) 59 global tj_buf uchar * 60 global tj_eoff ulong * 61 global tj_epoff ulong * 62 global tj_eplen uint * 63 global tj_eseq ulong * 64 global tj_ekind uchar * 65 global tj_eblk uint * 66 global tj_eopi uint * 67 68 note State: 0 buf_used, 1 entry_count, 2 alarm_bytes (0=off), 3 alarm_tripped, 69 note 4 stream_fd_open, 5 stream_fd, 6 stream_bytes, 7 stream_entries, 8 verify_next_pos 70 global tj_st ulong 12 71 global tj_last_h uchar 32 72 global tj_cur_h uchar 32 73 global tj_tmp_p uchar 65536 74 global tj_io uchar 65536 75 global tj_path uchar 4096 76 global tj_plen ulong 1 77 78 fn tkj_ensure_buf need ulong to ulong 79 cur = len tj_buf 80 if le need cur 81 ret TKJ_OK 82 end 83 cap = cur 84 if eq cap 0 85 cap = ulong 4096 86 end 87 while lt cap need bound 64 88 cap = shl cap 1 89 end 90 rc = enlarge tj_buf cap 91 if lt rc 0 92 ret TKJ_ERR_NOMEM 93 end 94 ret TKJ_OK 95 end 96 97 fn tkj_ensure_idx need ulong to ulong 98 cur = len tj_eoff 99 if le need cur 100 ret TKJ_OK 101 end 102 cap = cur 103 if eq cap 0 104 cap = ulong 64 105 end 106 while lt cap need bound 64 107 cap = shl cap 1 108 end 109 r0 = enlarge tj_eoff cap 110 if lt r0 0 111 ret TKJ_ERR_NOMEM 112 end 113 r1 = enlarge tj_epoff cap 114 if lt r1 0 115 ret TKJ_ERR_NOMEM 116 end 117 r2 = enlarge tj_eplen cap 118 if lt r2 0 119 ret TKJ_ERR_NOMEM 120 end 121 r3 = enlarge tj_eseq cap 122 if lt r3 0 123 ret TKJ_ERR_NOMEM 124 end 125 r4 = enlarge tj_ekind cap 126 if lt r4 0 127 ret TKJ_ERR_NOMEM 128 end 129 r5 = enlarge tj_eblk cap 130 if lt r5 0 131 ret TKJ_ERR_NOMEM 132 end 133 r6 = enlarge tj_eopi cap 134 if lt r6 0 135 ret TKJ_ERR_NOMEM 136 end 137 ret TKJ_OK 138 end 139 140 fn tkj_put_u32 off ulong v ulong to void 141 put tj_buf off uchar and v 255 142 put tj_buf add off 1 uchar and shr v 8 255 143 put tj_buf add off 2 uchar and shr v 16 255 144 put tj_buf add off 3 uchar and shr v 24 255 145 end 146 147 fn tkj_put_u64 off ulong v ulong to void 148 for j ulong 8 149 b = uchar and shr v mul j 8 255 150 put tj_buf add off j b 151 end 152 end 153 154 fn tkj_get_u32 off ulong to ulong 155 v = ulong 0 156 for j ulong 4 157 b = ulong get tj_buf add off j 158 v = or v shl b mul j 8 159 end 160 ret v 161 end 162 163 fn tkj_get_u64 off ulong to ulong 164 v = ulong 0 165 for j ulong 8 166 b = ulong get tj_buf add off j 167 v = or v shl b mul j 8 168 end 169 ret v 170 end 171 172 fn tkj_reset to ulong 173 put tj_st 0 0 174 put tj_st 1 0 175 put tj_st 3 0 176 for j ulong 32 177 put tj_last_h j 0 178 put tj_cur_h j 0 179 end 180 rc = tkj_ensure_buf TKJ_HDR_LEN 181 if ne rc TKJ_OK 182 ret rc 183 end 184 for j ulong 4 185 b = get tj_magic j 186 put tj_buf j b 187 end 188 put tj_buf 4 1 189 put tj_buf 5 0 190 put tj_buf 6 0 191 put tj_buf 7 0 192 tkj_put_u64 8 0 193 put tj_st 0 TKJ_HDR_LEN 194 ret TKJ_OK 195 end 196 197 fn tkj_set_alarm_bytes limit ulong to void 198 put tj_st 2 limit 199 put tj_st 3 0 200 end 201 202 fn tkj_alarm_tripped to ulong 203 ret get tj_st 3 204 end 205 206 fn tkj_count to ulong 207 ret get tj_st 1 208 end 209 210 fn tkj_bytes to ulong 211 ret get tj_st 0 212 end 213 214 note Compute entry_hash for serialized entry at buf_off with payload_len plen. 215 fn tkj_hash_entry buf_off ulong plen ulong to ulong 216 body_len = add add 21 plen 32 217 tot = add 6 body_len 218 if gt tot len sh_dat 219 ret TKJ_ERR_NOMEM 220 end 221 for j ulong 5 222 put sh_dat j get tj_dom j 223 end 224 put sh_dat 5 0 225 for j ulong body_len bound `16777216` 226 b = get tj_buf add buf_off j 227 put sh_dat add 6 j b 228 end 229 sh_all tot 230 ret TKJ_OK 231 end 232 233 note Pack 4 u64 words into tj_tmp_p[0..32) (call tkj_append kind blk op_idx 32 next). 234 fn tkj_pack_u64x4 w0 ulong w1 ulong w2 ulong w3 ulong to void 235 for j ulong 8 236 s = mul j 8 237 put tj_tmp_p j uchar and shr w0 s 255 238 put tj_tmp_p add 8 j uchar and shr w1 s 255 239 put tj_tmp_p add 16 j uchar and shr w2 s 255 240 put tj_tmp_p add 24 j uchar and shr w3 s 255 241 end 242 end 243 244 fn tkj_append_tail seq ulong off ulong plen ulong to ulong 245 p_off = add off 21 246 for j ulong plen bound 65536 247 b = get tj_tmp_p j 248 put tj_buf add p_off j b 249 end 250 h_off = add p_off plen 251 for j ulong 32 252 b = get tj_last_h j 253 put tj_buf add h_off j b 254 end 255 rc = tkj_hash_entry off plen 256 if ne rc TKJ_OK 257 ret rc 258 end 259 eh_off = add h_off 32 260 for j ulong 32 261 b = get sh_dig j 262 put tj_buf add eh_off j b 263 put tj_last_h j b 264 end 265 put tj_eoff seq off 266 put tj_epoff seq p_off 267 put tj_eplen seq uint plen 268 put tj_eseq seq seq 269 ret TKJ_OK 270 end 271 272 note Append one tok-crossing event using tj_tmp_p[0..plen) as payload. 273 fn tkj_append kind ulong blk ulong op_idx ulong plen ulong to ulong 274 if or eq kind 0 gt kind TKJ_OP_MAX 275 ret TKJ_ERR_KIND 276 end 277 if gt plen len tj_tmp_p 278 ret TKJ_ERR_NOMEM 279 end 280 seq = get tj_st 1 281 rc = tkj_ensure_idx add seq 1 282 if ne rc TKJ_OK 283 ret rc 284 end 285 off = get tj_st 0 286 rec_len = add TKJ_ENT_FIXED plen 287 rc = tkj_ensure_buf add off rec_len 288 if ne rc TKJ_OK 289 ret rc 290 end 291 tkj_put_u64 off seq 292 put tj_buf add off 8 uchar kind 293 tkj_put_u32 add off 9 blk 294 tkj_put_u32 add off 13 op_idx 295 tkj_put_u32 add off 17 plen 296 rc = tkj_append_tail seq off plen 297 if ne rc TKJ_OK 298 ret rc 299 end 300 put tj_ekind seq uchar kind 301 put tj_eblk seq uint blk 302 put tj_eopi seq uint op_idx 303 new_cnt = add seq 1 304 new_used = add off rec_len 305 put tj_st 1 new_cnt 306 put tj_st 0 new_used 307 tkj_put_u64 8 new_cnt 308 lim = get tj_st 2 309 if and ne lim 0 ge new_used lim 310 put tj_st 3 1 311 end 312 ret TKJ_OK 313 end 314 315 note Read u64 word widx from entry e's payload. 316 fn tkj_entry_u64 e ulong widx ulong to ulong 317 po = get tj_epoff e 318 pl = ulong get tj_eplen e 319 bo = mul widx 8 320 if gt add bo 8 pl 321 ret 0 322 end 323 ret tkj_get_u64 add po bo 324 end 325 326 note Verify one entry at pos; on success sets tj_st[8] to next pos and returns TKJ_OK. 327 fn tkj_verify_hashes pos ulong plen ulong to ulong 328 ph_off = add add pos 21 plen 329 for j ulong 32 330 b = get tj_buf add ph_off j 331 if ne b get tj_cur_h j 332 ret TKJ_ERR_CHAIN 333 end 334 end 335 rc = tkj_hash_entry pos plen 336 if ne rc TKJ_OK 337 ret rc 338 end 339 eh_off = add ph_off 32 340 for j ulong 32 341 b = get tj_buf add eh_off j 342 d = get sh_dig j 343 if ne b d 344 ret TKJ_ERR_HASH 345 end 346 put tj_cur_h j d 347 end 348 put tj_st 8 add eh_off 32 349 ret TKJ_OK 350 end 351 352 fn tkj_verify_one i ulong pos ulong tot_bytes ulong to ulong 353 if gt add pos TKJ_ENT_FIXED tot_bytes 354 ret TKJ_ERR_TRUNC 355 end 356 seq = tkj_get_u64 pos 357 if ne seq i 358 ret TKJ_ERR_SEQ 359 end 360 kind = ulong get tj_buf add pos 8 361 if or eq kind 0 gt kind TKJ_OP_MAX 362 ret TKJ_ERR_KIND 363 end 364 blk = tkj_get_u32 add pos 9 365 opi = tkj_get_u32 add pos 13 366 plen = tkj_get_u32 add pos 17 367 if gt plen sub tot_bytes add pos TKJ_ENT_FIXED 368 ret TKJ_ERR_TRUNC 369 end 370 rc = tkj_verify_hashes pos plen 371 if ne rc TKJ_OK 372 ret rc 373 end 374 rc = tkj_ensure_idx add i 1 375 if ne rc TKJ_OK 376 ret rc 377 end 378 put tj_eoff i pos 379 put tj_epoff i add pos 21 380 put tj_eplen i uint plen 381 put tj_eseq i seq 382 put tj_ekind i uchar kind 383 put tj_eblk i uint blk 384 put tj_eopi i uint opi 385 ret TKJ_OK 386 end 387 388 note Fail-closed verification and indexing of tj_buf[0..tot_bytes). 389 fn tkj_verify_buf tot_bytes ulong to ulong 390 if lt tot_bytes TKJ_HDR_LEN 391 ret TKJ_ERR_TRUNC 392 end 393 for j ulong 4 394 b = get tj_buf j 395 if ne b get tj_magic j 396 ret TKJ_ERR_MAGIC 397 end 398 end 399 v4 = get tj_buf 4 400 if ne v4 1 401 ret TKJ_ERR_VER 402 end 403 v5 = get tj_buf 5 404 v6 = get tj_buf 6 405 v7 = get tj_buf 7 406 if or ne v5 0 or ne v6 0 ne v7 0 407 ret TKJ_ERR_VER 408 end 409 ecnt = tkj_get_u64 8 410 for j ulong 32 411 put tj_cur_h j 0 412 end 413 pos = ulong TKJ_HDR_LEN 414 for i ulong ecnt bound `16777216` 415 rc = tkj_verify_one i pos tot_bytes 416 if ne rc TKJ_OK 417 ret rc 418 end 419 pos = get tj_st 8 420 end 421 if ne pos tot_bytes 422 ret TKJ_ERR_TRAILING 423 end 424 put tj_st 0 tot_bytes 425 put tj_st 1 ecnt 426 for j ulong 32 427 put tj_last_h j get tj_cur_h j 428 end 429 ret TKJ_OK 430 end 431 432 note Serialize tj_buf[0..tj_st[0]) cleanly to disk file at path. 433 fn tkj_write_chunk fd int off ulong n ulong to ulong 434 for j ulong n bound 65536 435 b = get tj_buf add off j 436 put tj_io j b 437 end 438 w = ulong 0 439 while lt w n bound 65536 440 k = write fd tj_io w sub n w 441 if le k 0 442 ret TKJ_ERR_IO 443 end 444 w = add w ulong k 445 end 446 ret TKJ_OK 447 end 448 449 fn tkj_write_fd fd int to ulong 450 tot = get tj_st 0 451 off = ulong 0 452 while lt off tot bound `1048576` 453 n = sub tot off 454 if gt n 65536 455 n = ulong 65536 456 end 457 rc = tkj_write_chunk fd off n 458 if ne rc TKJ_OK 459 r = fclose fd 460 ret rc 461 end 462 off = add off n 463 end 464 r = fclose fd 465 if ne r 0 466 ret TKJ_ERR_IO 467 end 468 ret TKJ_OK 469 end 470 471 fn tkj_write_file path view uchar * to ulong 472 fd = fopen path 0 len path 577 420 473 if lt fd 0 474 ret TKJ_ERR_IO 475 end 476 ret tkj_write_fd int fd 477 end 478 479 fn tkj_write_path to ulong 480 fd = fopen tj_path 0 get tj_plen 0 577 420 481 if lt fd 0 482 ret TKJ_ERR_IO 483 end 484 ret tkj_write_fd int fd 485 end 486 487 note Read disk file from open fd into growable tj_buf and fail-closed verify it. 488 fn tkj_read_fd fd int to ulong 489 off = ulong 0 490 while true bound `1048576` 491 k = read fd tj_io 0 65536 492 if lt k 0 493 r = fclose fd 494 ret TKJ_ERR_IO 495 end 496 if eq k 0 497 break 498 end 499 n = ulong k 500 rc = tkj_ensure_buf add off n 501 if ne rc TKJ_OK 502 r = fclose fd 503 ret rc 504 end 505 for j ulong n bound 65536 506 b = get tj_io j 507 put tj_buf add off j b 508 end 509 off = add off n 510 end 511 r = fclose fd 512 ret tkj_verify_buf off 513 end 514 515 fn tkj_read_file path view uchar * to ulong 516 fd = fopen path 0 len path 0 0 517 if lt fd 0 518 ret TKJ_ERR_IO 519 end 520 ret tkj_read_fd int fd 521 end 522 523 fn tkj_read_path to ulong 524 fd = fopen tj_path 0 get tj_plen 0 0 0 525 if lt fd 0 526 ret TKJ_ERR_IO 527 end 528 ret tkj_read_fd int fd 529 end 530 531 note Direct streaming writer for unbounded disk-backed journals. 532 fn tkj_stream_init_fd fd int to ulong 533 for j ulong 4 534 put tj_io j get tj_magic j 535 end 536 put tj_io 4 1 537 for j ulong 11 538 put tj_io add 5 j 0 539 end 540 k = write fd tj_io 0 TKJ_HDR_LEN 541 if ne k long TKJ_HDR_LEN 542 r = fclose fd 543 ret TKJ_ERR_IO 544 end 545 put tj_st 4 1 546 put tj_st 5 ulong fd 547 put tj_st 6 TKJ_HDR_LEN 548 put tj_st 7 0 549 put tj_st 3 0 550 for j ulong 32 551 put tj_last_h j 0 552 end 553 ret TKJ_OK 554 end 555 556 fn tkj_stream_open path view uchar * to ulong 557 fd = fopen path 0 len path 577 420 558 if lt fd 0 559 ret TKJ_ERR_IO 560 end 561 ret tkj_stream_init_fd int fd 562 end 563 564 fn tkj_stream_open_path to ulong 565 fd = fopen tj_path 0 get tj_plen 0 577 420 566 if lt fd 0 567 ret TKJ_ERR_IO 568 end 569 ret tkj_stream_init_fd int fd 570 end 571 572 fn tkj_stream_flush rec_len ulong to ulong 573 fd = int get tj_st 5 574 w = ulong 0 575 while lt w rec_len bound 65536 576 k = write fd tj_io w sub rec_len w 577 if le k 0 578 ret TKJ_ERR_IO 579 end 580 w = add w ulong k 581 end 582 new_bytes = add get tj_st 6 rec_len 583 put tj_st 6 new_bytes 584 put tj_st 7 add get tj_st 7 1 585 lim = get tj_st 2 586 if and ne lim 0 ge new_bytes lim 587 put tj_st 3 1 588 end 589 ret TKJ_OK 590 end 591 592 fn tkj_stream_append kind ulong blk ulong op_idx ulong plen ulong to ulong 593 if eq get tj_st 4 0 594 ret TKJ_ERR_IO 595 end 596 if or eq kind 0 gt kind TKJ_OP_MAX 597 ret TKJ_ERR_KIND 598 end 599 if gt plen 65000 600 ret TKJ_ERR_NOMEM 601 end 602 seq = get tj_st 7 603 for j ulong 8 604 put tj_io j uchar and shr seq mul j 8 255 605 end 606 put tj_io 8 uchar kind 607 for j ulong 4 608 s = mul j 8 609 put tj_io add 9 j uchar and shr blk s 255 610 put tj_io add 13 j uchar and shr op_idx s 255 611 put tj_io add 17 j uchar and shr plen s 255 612 end 613 for j ulong plen bound 65000 614 put tj_io add 21 j get tj_tmp_p j 615 end 616 ph_off = add 21 plen 617 for j ulong 32 618 put tj_io add ph_off j get tj_last_h j 619 end 620 body_len = add ph_off 32 621 for j ulong 5 622 put sh_dat j get tj_dom j 623 end 624 put sh_dat 5 0 625 for j ulong body_len bound 65536 626 put sh_dat add 6 j get tj_io j 627 end 628 sh_all add 6 body_len 629 for j ulong 32 630 b = get sh_dig j 631 put tj_io add body_len j b 632 put tj_last_h j b 633 end 634 ret tkj_stream_flush add body_len 32 635 end 636 637 fn tkj_stream_close to ulong 638 if eq get tj_st 4 0 639 ret TKJ_ERR_IO 640 end 641 fd = int get tj_st 5 642 cnt = get tj_st 7 643 for j ulong 8 644 put tj_io j uchar and shr cnt mul j 8 255 645 end 646 sk = fseek fd 8 0 647 if lt sk 0 648 r = fclose fd 649 put tj_st 4 0 650 ret TKJ_ERR_IO 651 end 652 k = write fd tj_io 0 8 653 r = fclose fd 654 put tj_st 4 0 655 if ne k 8 656 ret TKJ_ERR_IO 657 end 658 ret TKJ_OK 659 end