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 / protocol / synapse / claims / src / 40_state.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Claim table, id hash table, target canonical form, overlap rules, ledger loader/checker and the |
| 3 | note list / holder JSON writers. |
| 4 | |
| 5 | note claim cells (nf per claim in gcl) |
| 6 | const cid ulong 0 |
| 7 | const cag ulong 1 |
| 8 | const cagl ulong 2 |
| 9 | const cse ulong 3 |
| 10 | const csel ulong 4 |
| 11 | const ckind ulong 5 |
| 12 | const ctg ulong 6 |
| 13 | const ctgl ulong 7 |
| 14 | const cmode ulong 8 |
| 15 | const cst ulong 9 |
| 16 | const cin ulong 11 |
| 17 | const cinl ulong 12 |
| 18 | const cexp ulong 13 |
| 19 | const csince ulong 14 |
| 20 | const cseq ulong 15 |
| 21 | const cor ulong 16 |
| 22 | const corl ulong 17 |
| 23 | const cev ulong 18 |
| 24 | const cevl ulong 19 |
| 25 | const cclo ulong 20 |
| 26 | |
| 27 | text mnf "not found" |
| 28 | text mnh "not holder" |
| 29 | text mna "not active" |
| 30 | text mchain "ledger integrity failure; run verify" |
| 31 | text mtab "claim table full" |
| 32 | text mio "ledger read error" |
| 33 | |
| 34 | fn c_get i ulong f ulong to ulong |
| 35 | ret get gcl add mul i nf f |
| 36 | end |
| 37 | |
| 38 | fn c_put i ulong f ulong v ulong to void |
| 39 | put gcl add mul i nf f v |
| 40 | end |
| 41 | |
| 42 | note ======== id hash table: key = 16-hex id as ulong, value = claim index + 1 ======== |
| 43 | |
| 44 | fn ht_slot k ulong to ulong |
| 45 | p = and k htmask |
| 46 | while true bound 270000 |
| 47 | v = get ght p |
| 48 | if eq v 0 |
| 49 | ret p |
| 50 | end |
| 51 | if eq get ghk p k |
| 52 | ret p |
| 53 | end |
| 54 | p = and add p 1 htmask |
| 55 | end |
| 56 | ret 0 |
| 57 | end |
| 58 | |
| 59 | fn ht_find k ulong to ulong |
| 60 | p = ht_slot k |
| 61 | v = get ght p |
| 62 | if eq v 0 |
| 63 | ret ncl |
| 64 | end |
| 65 | ret sub v 1 |
| 66 | end |
| 67 | |
| 68 | note ht_put: false when the key is already present |
| 69 | fn ht_put k ulong i ulong to bool |
| 70 | p = ht_slot k |
| 71 | if ne get ght p 0 |
| 72 | ret false |
| 73 | end |
| 74 | put ght p add i 1 |
| 75 | put ghk p k |
| 76 | ret true |
| 77 | end |
| 78 | |
| 79 | note hex16 of s[o..o+16) -> ulong (input already checked lowercase hex) |
| 80 | fn hx_u s edit uchar * o ulong to ulong |
| 81 | v = ulong 0 |
| 82 | for i ulong 16 |
| 83 | v = or shl v 4 hexv get s add o i |
| 84 | end |
| 85 | ret v |
| 86 | end |
| 87 | |
| 88 | fn o_id v ulong to void |
| 89 | for i ulong 16 |
| 90 | o_b hexd and shr v sub 60 mul i 4 15 |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | note ======== targets ======== |
| 95 | |
| 96 | note kind code (0 repo 1 branch 2 path 3 pr 4 ext) of the kind word before the first ':', or tbad |
| 97 | fn tg_kind s edit uchar * o ulong n ulong to ulong |
| 98 | k = ulong 0 |
| 99 | while lt k n bound 300 |
| 100 | if eq get s add o k 58 |
| 101 | break |
| 102 | end |
| 103 | k = add k 1 |
| 104 | end |
| 105 | if ge k n |
| 106 | ret tbad |
| 107 | end |
| 108 | w = tw_find part s o k wall 13 5 |
| 109 | if eq w tbad |
| 110 | ret tbad |
| 111 | end |
| 112 | ret sub w 13 |
| 113 | end |
| 114 | |
| 115 | fn rp_ch c uchar to bool |
| 116 | if and ge c 97 le c 122 |
| 117 | ret true |
| 118 | end |
| 119 | if and ge c 48 le c 57 |
| 120 | ret true |
| 121 | end |
| 122 | ret or or eq c 46 eq c 95 eq c 45 |
| 123 | end |
| 124 | |
| 125 | note tg_ext: appends the lowercased ext scheme of s[o+i..o+n); returns the index of its ':' or tbad |
| 126 | fn tg_ext s edit uchar * o ulong n ulong i0 ulong to ulong |
| 127 | i = i0 |
| 128 | scl = ulong 0 |
| 129 | while lt i n bound 300 |
| 130 | c = lower get s add o i |
| 131 | if eq c 58 |
| 132 | break |
| 133 | end |
| 134 | if eq or rp_ch c eq c 43 false |
| 135 | ret tbad |
| 136 | end |
| 137 | ar_b c |
| 138 | scl = add scl 1 |
| 139 | i = add i 1 |
| 140 | end |
| 141 | if or or eq scl 0 ge i n eq add i 1 n |
| 142 | ret tbad |
| 143 | end |
| 144 | ret i |
| 145 | end |
| 146 | |
| 147 | note tg_own: appends the lowercased OWNER/REPO of s[o+i..o+n); returns the index after it or tbad |
| 148 | fn tg_own s edit uchar * o ulong n ulong i0 ulong to ulong |
| 149 | i = i0 |
| 150 | sl = ulong 0 |
| 151 | rl = ulong 0 |
| 152 | while lt i n bound 300 |
| 153 | c = lower get s add o i |
| 154 | if or eq c 35 eq c 58 |
| 155 | break |
| 156 | end |
| 157 | if eq c 47 |
| 158 | sl = add sl 1 |
| 159 | if eq rl 0 |
| 160 | ret tbad |
| 161 | end |
| 162 | if eq get s add o sub i 1 47 |
| 163 | ret tbad |
| 164 | end |
| 165 | else |
| 166 | if eq rp_ch c false |
| 167 | ret tbad |
| 168 | end |
| 169 | end |
| 170 | ar_b c |
| 171 | rl = add rl 1 |
| 172 | i = add i 1 |
| 173 | end |
| 174 | if ne sl 1 |
| 175 | ret tbad |
| 176 | end |
| 177 | if eq get s add o sub i 1 47 |
| 178 | ret tbad |
| 179 | end |
| 180 | ret i |
| 181 | end |
| 182 | |
| 183 | note tg_sfx: s[o+i..o+n) is a valid branch/pr/path suffix for kind kd (separator plus non-empty rest) |
| 184 | fn tg_sfx s edit uchar * o ulong n ulong i ulong kd ulong to bool |
| 185 | if ge add i 1 n |
| 186 | ret false |
| 187 | end |
| 188 | c = get s add o i |
| 189 | if and eq kd 2 ne c 58 |
| 190 | ret false |
| 191 | end |
| 192 | if and ne kd 2 ne c 35 |
| 193 | ret false |
| 194 | end |
| 195 | if eq kd 3 |
| 196 | for j ulong sub sub n i 1 |
| 197 | d = get s add add o i add j 1 |
| 198 | if or lt d 48 gt d 57 |
| 199 | ret false |
| 200 | end |
| 201 | end |
| 202 | end |
| 203 | ret true |
| 204 | end |
| 205 | |
| 206 | note tg_canon: canonical copy of target s[o..o+n) appended to the arena; returns its offset (length |
| 207 | note in gx[xpos]) and the kind in gx[xkind]; tbad (arena unchanged) when the target is invalid. |
| 208 | fn tg_canon s edit uchar * o ulong n ulong to ulong |
| 209 | if eq sf_ok part s o n false |
| 210 | ret tbad |
| 211 | end |
| 212 | kd = tg_kind s o n |
| 213 | if eq kd tbad |
| 214 | ret tbad |
| 215 | end |
| 216 | put gx xkind kd |
| 217 | st = get gx xarn |
| 218 | i = add tw_len wall add kd 13 1 |
| 219 | ar_lnv s o i |
| 220 | if eq kd 4 |
| 221 | i = tg_ext s o n i |
| 222 | else |
| 223 | i = tg_own s o n i |
| 224 | end |
| 225 | if eq i tbad |
| 226 | put gx xarn st |
| 227 | ret tbad |
| 228 | end |
| 229 | if eq kd 0 |
| 230 | if lt i n |
| 231 | put gx xarn st |
| 232 | ret tbad |
| 233 | end |
| 234 | else |
| 235 | if ne kd 4 |
| 236 | if eq tg_sfx s o n i kd false |
| 237 | put gx xarn st |
| 238 | ret tbad |
| 239 | end |
| 240 | end |
| 241 | end |
| 242 | ar_lnv s add o i sub n i |
| 243 | put gx xpos sub get gx xarn st |
| 244 | ret st |
| 245 | end |
| 246 | |
| 247 | note end of the owner/repo part of arena target t (kind code k, kind word skipped) |
| 248 | fn tg_rend k ulong t ulong n ulong to ulong |
| 249 | i = add tw_len wall add k 13 1 |
| 250 | while lt i n bound 300 |
| 251 | c = ar_g add t i |
| 252 | if or eq c 35 eq c 58 |
| 253 | ret i |
| 254 | end |
| 255 | i = add i 1 |
| 256 | end |
| 257 | ret n |
| 258 | end |
| 259 | |
| 260 | note literal prefix (bytes before the first '*') length of the glob part from offset g |
| 261 | fn gl_pre t ulong g ulong n ulong to ulong |
| 262 | i = g |
| 263 | while lt i n bound 300 |
| 264 | if eq ar_g add t i 42 |
| 265 | ret sub i g |
| 266 | end |
| 267 | i = add i 1 |
| 268 | end |
| 269 | ret sub n g |
| 270 | end |
| 271 | |
| 272 | note ov_path: 3 when the literal glob prefixes of two path targets (repo part ending at ea/eb) agree, else 0 |
| 273 | fn ov_path ta ulong ea ulong la ulong tb ulong eb ulong lb ulong to ulong |
| 274 | pa = gl_pre ta add ea 1 la |
| 275 | pb = gl_pre tb add eb 1 lb |
| 276 | m = pa |
| 277 | if lt pb m |
| 278 | m = pb |
| 279 | end |
| 280 | ga = add ta add ea 1 |
| 281 | gb = add tb add eb 1 |
| 282 | if ar_eq ga m gb m |
| 283 | ret 3 |
| 284 | end |
| 285 | ret 0 |
| 286 | end |
| 287 | |
| 288 | note ov: overlap reason of two canonical arena targets: 0 none 1 same_target 2 repo_covers |
| 289 | note 3 path_overlap 4 ext_casefold |
| 290 | fn ov ka ulong ta ulong la ulong kb ulong tb ulong lb ulong to ulong |
| 291 | if or eq ka 4 eq kb 4 |
| 292 | if ne ka kb |
| 293 | ret 0 |
| 294 | end |
| 295 | if ar_eq ta la tb lb |
| 296 | ret 1 |
| 297 | end |
| 298 | if ar_eqci ta la tb lb |
| 299 | ret 4 |
| 300 | end |
| 301 | ret 0 |
| 302 | end |
| 303 | if ar_eq ta la tb lb |
| 304 | ret 1 |
| 305 | end |
| 306 | ea = tg_rend ka ta la |
| 307 | eb = tg_rend kb tb lb |
| 308 | sa = add tw_len wall add ka 13 1 |
| 309 | sb = add tw_len wall add kb 13 1 |
| 310 | if eq ar_eq add ta sa sub ea sa add tb sb sub eb sb false |
| 311 | ret 0 |
| 312 | end |
| 313 | if or eq ka 0 eq kb 0 |
| 314 | ret 2 |
| 315 | end |
| 316 | if or ne ka 2 ne kb 2 |
| 317 | ret 0 |
| 318 | end |
| 319 | ret ov_path ta ea la tb eb lb |
| 320 | end |
| 321 | |
| 322 | fn c_act i ulong to bool |
| 323 | if ne c_get i cclo 0 |
| 324 | ret false |
| 325 | end |
| 326 | ret gt c_get i cexp get gx xnow |
| 327 | end |
| 328 | |
| 329 | note ======== ledger loading ======== |
| 330 | |
| 331 | note paths (filled by main): gpth holds home-relative strings; gx[xpled]/gx[xpledl] etc. index it |
| 332 | global gpth uchar 16384 |
| 333 | |
| 334 | fn ar_eqs a ulong an ulong s edit uchar * o ulong n ulong to bool |
| 335 | if ne an n |
| 336 | ret false |
| 337 | end |
| 338 | for i ulong n |
| 339 | if ne ar_g add a i get s add o i |
| 340 | ret false |
| 341 | end |
| 342 | end |
| 343 | ret true |
| 344 | end |
| 345 | |
| 346 | note append the line's evidence (gp qev) to claim i's evidence, joined by ',', capped at evcap |
| 347 | fn ld_ev i ulong s edit uchar * to void |
| 348 | n = get gp qevl |
| 349 | if eq n 0 |
| 350 | ret |
| 351 | end |
| 352 | ol = c_get i cevl |
| 353 | add1 = ulong 0 |
| 354 | if gt ol 0 |
| 355 | add1 = 1 |
| 356 | end |
| 357 | if gt add add ol add1 n evcap |
| 358 | ret |
| 359 | end |
| 360 | st = get gx xarn |
| 361 | ar_cpv c_get i cev ol |
| 362 | if gt ol 0 |
| 363 | ar_b 44 |
| 364 | end |
| 365 | ar_lnv s get gp qev n |
| 366 | c_put i cev st |
| 367 | c_put i cevl add add ol add1 n |
| 368 | end |
| 369 | |
| 370 | note ld_apply: the scanned line s (gp cells) becomes claim state. 0 ok, 4 id error, 6 malformed. |
| 371 | fn ld_apply s edit uchar * to ulong |
| 372 | op = get gp qop |
| 373 | id = hx_u s get gp qid |
| 374 | kd = sub get gp qkind 13 |
| 375 | if eq op 0 |
| 376 | if ge get gx xnc ncl |
| 377 | die 70 mtab |
| 378 | end |
| 379 | i = get gx xnc |
| 380 | st = get gx xarn |
| 381 | t = tg_canon s get gp qtg get gp qtgl |
| 382 | if eq t tbad |
| 383 | ret 6 |
| 384 | end |
| 385 | if eq ar_eqs t get gx xpos s get gp qtg get gp qtgl false |
| 386 | put gx xarn st |
| 387 | ret 6 |
| 388 | end |
| 389 | if ne get gx xkind kd |
| 390 | put gx xarn st |
| 391 | ret 6 |
| 392 | end |
| 393 | if eq ht_put id i false |
| 394 | put gx xarn st |
| 395 | ret 4 |
| 396 | end |
| 397 | put gx xnc add i 1 |
| 398 | c_put i cid id |
| 399 | c_put i ckind kd |
| 400 | c_put i ctg t |
| 401 | c_put i ctgl get gp qtgl |
| 402 | c_put i cag ar_ln s get gp qag get gp qagl |
| 403 | c_put i cagl get gp qagl |
| 404 | c_put i cse ar_ln s get gp qse get gp qsel |
| 405 | c_put i csel get gp qsel |
| 406 | c_put i cmode sub get gp qmode 18 |
| 407 | c_put i cst sub get gp qst 5 |
| 408 | c_put i cin ar_ln s get gp qin get gp qinl |
| 409 | c_put i cinl get gp qinl |
| 410 | c_put i cexp get gp qexp |
| 411 | c_put i csince get gp qts |
| 412 | c_put i cseq get gp qseq |
| 413 | c_put i cor ar_ln s get gp qor get gp qorl |
| 414 | c_put i corl get gp qorl |
| 415 | c_put i cev ar_ln s get gp qev get gp qevl |
| 416 | c_put i cevl get gp qevl |
| 417 | c_put i cclo 0 |
| 418 | ret 0 |
| 419 | end |
| 420 | i = ht_find id |
| 421 | if eq i ncl |
| 422 | ret 4 |
| 423 | end |
| 424 | if ne c_get i cclo 0 |
| 425 | ret 6 |
| 426 | end |
| 427 | if ne op 4 |
| 428 | if eq ar_eqs c_get i cag c_get i cagl s get gp qag get gp qagl false |
| 429 | ret 6 |
| 430 | end |
| 431 | end |
| 432 | c_put i cseq get gp qseq |
| 433 | c_put i cexp get gp qexp |
| 434 | c_put i cst sub get gp qst 5 |
| 435 | if eq op 2 |
| 436 | c_put i cclo 1 |
| 437 | end |
| 438 | if eq op 4 |
| 439 | c_put i cclo 2 |
| 440 | end |
| 441 | ld_ev i s |
| 442 | ret 0 |
| 443 | end |
| 444 | |
| 445 | note ld_line: chain checks against xseq / glh, then apply. 0 ok, else wwhy index + 1 |
| 446 | note (1 malformed, 2 seq, 3 prev, 5 id). |
| 447 | fn ld_line s edit uchar * n ulong to ulong |
| 448 | if eq pl_line s n false |
| 449 | ret 1 |
| 450 | end |
| 451 | if ne get gp qseq add get gx xseq 1 |
| 452 | ret 2 |
| 453 | end |
| 454 | p = get gp qprev |
| 455 | for i ulong 64 |
| 456 | if ne get s add p i get glh i |
| 457 | ret 3 |
| 458 | end |
| 459 | end |
| 460 | r = ld_apply s |
| 461 | if eq r 4 |
| 462 | ret 5 |
| 463 | end |
| 464 | if eq r 6 |
| 465 | ret 1 |
| 466 | end |
| 467 | h = get gp qhash |
| 468 | for j ulong 64 |
| 469 | put glh j get s add h j |
| 470 | end |
| 471 | put gx xseq get gp qseq |
| 472 | put gx xevn add get gx xevn 1 |
| 473 | ret 0 |
| 474 | end |
| 475 | |
| 476 | fn glh_zero to void |
| 477 | for i ulong 64 |
| 478 | put glh i 48 |
| 479 | end |
| 480 | end |
| 481 | |
| 482 | note ld_open: open the ledger read-only; absent -> -1 |
| 483 | fn ld_open to long |
| 484 | fd = fopen gpth get gx xpled get gx xpledl 524288 0 |
| 485 | if eq fd -2 |
| 486 | ret -1 |
| 487 | end |
| 488 | if lt fd 0 |
| 489 | die 70 mio |
| 490 | end |
| 491 | ret fd |
| 492 | end |
| 493 | |
| 494 | fn ld_reset to void |
| 495 | put gx xnc 0 |
| 496 | put gx xseq 0 |
| 497 | put gx xevn 0 |
| 498 | glh_zero |
| 499 | for i ulong 262144 |
| 500 | put ght i 0 |
| 501 | end |
| 502 | end |
| 503 | |
| 504 | note ld_all: load the whole ledger into the claim table (any failure: die 3) |
| 505 | fn ld_all s edit uchar * to void |
| 506 | ld_reset |
| 507 | fd = ld_open |
| 508 | if lt fd 0 |
| 509 | ret |
| 510 | end |
| 511 | r_open fd |
| 512 | while true bound 999999 |
| 513 | n = r_line s |
| 514 | if eq n -1 |
| 515 | break |
| 516 | end |
| 517 | if lt n 0 |
| 518 | die 3 mchain |
| 519 | end |
| 520 | if ne ld_line s ulong n 0 |
| 521 | die 3 mchain |
| 522 | end |
| 523 | end |
| 524 | c = fclose int fd |
| 525 | if ne get gx xreof 1 |
| 526 | die 70 mio |
| 527 | end |
| 528 | end |
| 529 | |
| 530 | note ======== JSON writers ======== |
| 531 | |
| 532 | fn o_kq t view uchar * first bool to void |
| 533 | o_key t first |
| 534 | o_q |
| 535 | end |
| 536 | |
| 537 | text jid "id" |
| 538 | text jag "agent" |
| 539 | text jse "session" |
| 540 | text jkind "kind" |
| 541 | text jtg "target" |
| 542 | text jmode "mode" |
| 543 | text jst "state" |
| 544 | text jin "intent" |
| 545 | text jexp "expires" |
| 546 | text jact "active" |
| 547 | text jsin "since" |
| 548 | text jlseq "last_seq" |
| 549 | text jor "origin" |
| 550 | text jev "evidence" |
| 551 | |
| 552 | fn o_tsq s ulong to void |
| 553 | o_q |
| 554 | o_ts s |
| 555 | o_q |
| 556 | end |
| 557 | |
| 558 | fn o_arq off ulong n ulong to void |
| 559 | o_q |
| 560 | o_ar off n |
| 561 | o_q |
| 562 | end |
| 563 | |
| 564 | note the `list` object of claim i |
| 565 | fn o_claim i ulong to void |
| 566 | o_kq jid true |
| 567 | o_id c_get i cid |
| 568 | o_q |
| 569 | o_key jag false |
| 570 | o_arq c_get i cag c_get i cagl |
| 571 | o_key jse false |
| 572 | o_arq c_get i cse c_get i csel |
| 573 | o_key jkind false |
| 574 | o_q |
| 575 | o_wd wall add c_get i ckind 13 |
| 576 | o_q |
| 577 | o_key jtg false |
| 578 | o_arq c_get i ctg c_get i ctgl |
| 579 | o_key jmode false |
| 580 | o_q |
| 581 | o_wd wall add c_get i cmode 18 |
| 582 | o_q |
| 583 | o_key jst false |
| 584 | o_q |
| 585 | o_wd wall add c_get i cst 5 |
| 586 | o_q |
| 587 | o_key jin false |
| 588 | o_arq c_get i cin c_get i cinl |
| 589 | o_key jexp false |
| 590 | o_tsq c_get i cexp |
| 591 | o_key jact false |
| 592 | if c_act i |
| 593 | o_t ttrue |
| 594 | else |
| 595 | o_t tfalse |
| 596 | end |
| 597 | o_key jsin false |
| 598 | o_tsq c_get i csince |
| 599 | o_key jlseq false |
| 600 | o_u c_get i cseq |
| 601 | o_key jor false |
| 602 | o_arq c_get i cor c_get i corl |
| 603 | o_key jev false |
| 604 | o_b 91 |
| 605 | o_ar c_get i cev c_get i cevl |
| 606 | o_b 93 |
| 607 | o_b 125 |
| 608 | end |
| 609 | |
| 610 | note the conflict `holder` object of claim i |
| 611 | fn o_hold i ulong to void |
| 612 | o_kq jid true |
| 613 | o_id c_get i cid |
| 614 | o_q |
| 615 | o_key jag false |
| 616 | o_arq c_get i cag c_get i cagl |
| 617 | o_key jse false |
| 618 | o_arq c_get i cse c_get i csel |
| 619 | o_key jtg false |
| 620 | o_arq c_get i ctg c_get i ctgl |
| 621 | o_key jmode false |
| 622 | o_q |
| 623 | o_wd wall add c_get i cmode 18 |
| 624 | o_q |
| 625 | o_key jexp false |
| 626 | o_tsq c_get i cexp |
| 627 | o_key jin false |
| 628 | o_arq c_get i cin c_get i cinl |
| 629 | o_b 125 |
| 630 | end |