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 / core / changeset.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/synapse/core/changeset.om: content-addressed changeset model, canonical serialization, |
| 3 | note SHA-256 identity computation (via om/tls/sha2.om), repository-relative path validation, |
| 4 | note exact & directory-prefix path overlap detector, and shared arena/encoding utilities. |
| 5 | note Composed OM-2 library unit (no om2 directive, no main). |
| 6 | |
| 7 | note ======== status & refusal codes ======== |
| 8 | const SYN_OK ulong 0 |
| 9 | const SYN_ERR_MALFORMED ulong 1 |
| 10 | const SYN_ERR_LEASE_CONFLICT ulong 2 |
| 11 | const SYN_ERR_NOT_FOUND ulong 3 |
| 12 | const SYN_ERR_CYCLE ulong 4 |
| 13 | const SYN_ERR_NOT_HOLDER ulong 5 |
| 14 | const SYN_ERR_LEASE_CLOSED ulong 6 |
| 15 | const SYN_ERR_SELF_REVIEW ulong 7 |
| 16 | const SYN_ERR_STALE_HEAD ulong 8 |
| 17 | const SYN_ERR_REVIEW_BLOCKED ulong 9 |
| 18 | const SYN_ERR_NON_FF ulong 10 |
| 19 | const SYN_ERR_STATE_INVALID ulong 11 |
| 20 | const SYN_ERR_LEDGER_TAMPER ulong 12 |
| 21 | const SYN_ERR_CAPACITY ulong 13 |
| 22 | const SYN_ERR_DUPLICATE ulong 14 |
| 23 | const SYN_ERR_VERDICT_INVALID ulong 15 |
| 24 | |
| 25 | const SYN_NONE ulong 18446744073709551615 |
| 26 | const SYN_STR_CAP ulong 262144 |
| 27 | |
| 28 | note ======== changeset states ======== |
| 29 | const CS_STATE_DRAFT ulong 0 |
| 30 | const CS_STATE_PROPOSED ulong 1 |
| 31 | const CS_STATE_REVIEWED ulong 2 |
| 32 | const CS_STATE_LANDED ulong 3 |
| 33 | const CS_STATE_ABANDONED ulong 4 |
| 34 | |
| 35 | note ======== changeset capacities & meta layout ======== |
| 36 | const CS_MAX ulong 256 |
| 37 | const CS_MAX_PATHS ulong 16 |
| 38 | const CS_PATH_POOL ulong 4096 |
| 39 | const CS_FIELDS ulong 12 |
| 40 | |
| 41 | const CSF_AUTH_OFF ulong 0 |
| 42 | const CSF_AUTH_LEN ulong 1 |
| 43 | const CSF_BR_OFF ulong 2 |
| 44 | const CSF_BR_LEN ulong 3 |
| 45 | const CSF_STATE ulong 4 |
| 46 | const CSF_PSTART ulong 5 |
| 47 | const CSF_PCNT ulong 6 |
| 48 | const CSF_TS ulong 7 |
| 49 | const CSF_PIDX ulong 8 |
| 50 | const CSF_SUM_OFF ulong 9 |
| 51 | const CSF_SUM_LEN ulong 10 |
| 52 | const CSF_USED ulong 11 |
| 53 | |
| 54 | note ======== globals ======== |
| 55 | global g_syn_str uchar 262144 |
| 56 | global g_syn_strn ulong 4 |
| 57 | |
| 58 | global g_cs_id uchar 8192 |
| 59 | global g_cs_parent uchar 8192 |
| 60 | global g_cs_meta ulong 3072 |
| 61 | global g_cs_paths ulong 8192 |
| 62 | global g_cs_hdr ulong 16 |
| 63 | |
| 64 | note staging area for building one changeset cleanly without exceeding 6 parameter slots |
| 65 | global g_cs_stg_par uchar 32 |
| 66 | global g_cs_stg_meta ulong 16 |
| 67 | global g_cs_stg_paths ulong 32 |
| 68 | |
| 69 | text t_cs_dom "SYNAPSE_CHANGESET_V1" |
| 70 | text t_hex "0123456789abcdef" |
| 71 | text t_st_draft "DRAFT" |
| 72 | text t_st_prop "PROPOSED" |
| 73 | text t_st_rev "REVIEWED" |
| 74 | text t_st_land "LANDED" |
| 75 | text t_st_abnd "ABANDONED" |
| 76 | text t_br_main "main" |
| 77 | |
| 78 | note ======== shared byte, hex, arena & I/O helpers ======== |
| 79 | |
| 80 | fn syn_arena_reset to void |
| 81 | put g_syn_strn 0 0 |
| 82 | end |
| 83 | |
| 84 | fn syn_arena_put s view uchar * to ulong |
| 85 | n = len s |
| 86 | cur = get g_syn_strn 0 |
| 87 | if gt add cur n SYN_STR_CAP |
| 88 | ret SYN_NONE |
| 89 | end |
| 90 | for i ulong n |
| 91 | put g_syn_str add cur i get s i |
| 92 | end |
| 93 | put g_syn_strn 0 add cur n |
| 94 | ret cur |
| 95 | end |
| 96 | |
| 97 | fn syn_arena_eq ao ulong al ulong bo ulong bl ulong to bool |
| 98 | if ne al bl |
| 99 | ret false |
| 100 | end |
| 101 | for i ulong al |
| 102 | if ne get g_syn_str add ao i get g_syn_str add bo i |
| 103 | ret false |
| 104 | end |
| 105 | end |
| 106 | ret true |
| 107 | end |
| 108 | |
| 109 | fn syn_arena_eq_view ao ulong al ulong v view uchar * to bool |
| 110 | if ne al len v |
| 111 | ret false |
| 112 | end |
| 113 | for i ulong al |
| 114 | if ne get g_syn_str add ao i get v i |
| 115 | ret false |
| 116 | end |
| 117 | end |
| 118 | ret true |
| 119 | end |
| 120 | |
| 121 | note lexicographic compare of two arena strings: 0 eq, 1 a<b, 2 a>b |
| 122 | fn syn_arena_cmp ao ulong al ulong bo ulong bl ulong to ulong |
| 123 | m = al |
| 124 | if lt bl m |
| 125 | m = bl |
| 126 | end |
| 127 | for i ulong m |
| 128 | ca = get g_syn_str add ao i |
| 129 | cb = get g_syn_str add bo i |
| 130 | if lt ca cb |
| 131 | ret 1 |
| 132 | end |
| 133 | if gt ca cb |
| 134 | ret 2 |
| 135 | end |
| 136 | end |
| 137 | if lt al bl |
| 138 | ret 1 |
| 139 | end |
| 140 | if gt al bl |
| 141 | ret 2 |
| 142 | end |
| 143 | ret 0 |
| 144 | end |
| 145 | |
| 146 | fn syn_arena_copy ao ulong al ulong dst edit uchar * to ulong |
| 147 | lim = al |
| 148 | if gt lim len dst |
| 149 | lim = len dst |
| 150 | end |
| 151 | for i ulong lim |
| 152 | put dst i get g_syn_str add ao i |
| 153 | end |
| 154 | ret lim |
| 155 | end |
| 156 | |
| 157 | fn syn_digest_eq a view uchar * b view uchar * to bool |
| 158 | if ne len a 32 |
| 159 | ret false |
| 160 | end |
| 161 | if ne len b 32 |
| 162 | ret false |
| 163 | end |
| 164 | for i ulong 32 |
| 165 | if ne get a i get b i |
| 166 | ret false |
| 167 | end |
| 168 | end |
| 169 | ret true |
| 170 | end |
| 171 | |
| 172 | fn syn_digest_is_zero d view uchar * to bool |
| 173 | if ne len d 32 |
| 174 | ret false |
| 175 | end |
| 176 | for i ulong 32 |
| 177 | if ne get d i 0 |
| 178 | ret false |
| 179 | end |
| 180 | end |
| 181 | ret true |
| 182 | end |
| 183 | |
| 184 | fn syn_put_u16_le dst edit uchar * off ulong v ulong to void |
| 185 | put dst off uchar and v 0xff |
| 186 | put dst add off 1 uchar and shr v 8 0xff |
| 187 | end |
| 188 | |
| 189 | fn syn_put_u32_le dst edit uchar * off ulong v ulong to void |
| 190 | put dst off uchar and v 0xff |
| 191 | put dst add off 1 uchar and shr v 8 0xff |
| 192 | put dst add off 2 uchar and shr v 16 0xff |
| 193 | put dst add off 3 uchar and shr v 24 0xff |
| 194 | end |
| 195 | |
| 196 | fn syn_put_u64_le dst edit uchar * off ulong v ulong to void |
| 197 | for i ulong 8 |
| 198 | put dst add off i uchar and shr v mul i 8 0xff |
| 199 | end |
| 200 | end |
| 201 | |
| 202 | fn syn_sha_u16 ctx edit ulong * v ulong to void |
| 203 | b = new uchar 2 0 |
| 204 | syn_put_u16_le b 0 v |
| 205 | sha256_update ctx b |
| 206 | end |
| 207 | |
| 208 | fn syn_sha_u32 ctx edit ulong * v ulong to void |
| 209 | b = new uchar 4 0 |
| 210 | syn_put_u32_le b 0 v |
| 211 | sha256_update ctx b |
| 212 | end |
| 213 | |
| 214 | fn syn_sha_u64 ctx edit ulong * v ulong to void |
| 215 | b = new uchar 8 0 |
| 216 | syn_put_u64_le b 0 v |
| 217 | sha256_update ctx b |
| 218 | end |
| 219 | |
| 220 | fn syn_sha_arena ctx edit ulong * off ulong slen ulong to void |
| 221 | blk = new uchar 256 0 |
| 222 | pos = ulong 0 |
| 223 | while lt pos slen bound 4096 |
| 224 | k = sub slen pos |
| 225 | if gt k 256 |
| 226 | k = 256 |
| 227 | end |
| 228 | for i ulong k |
| 229 | put blk i get g_syn_str add add off pos i |
| 230 | end |
| 231 | sha256_update ctx part blk 0 k |
| 232 | pos = add pos k |
| 233 | end |
| 234 | end |
| 235 | |
| 236 | fn syn_sha_arena_str ctx edit ulong * off ulong slen ulong to void |
| 237 | syn_sha_u32 ctx slen |
| 238 | if gt slen 0 |
| 239 | syn_sha_arena ctx off slen |
| 240 | end |
| 241 | end |
| 242 | |
| 243 | fn syn_hex_val c uchar to ulong |
| 244 | if and ge c 48 le c 57 |
| 245 | ret ulong sub c 48 |
| 246 | end |
| 247 | if and ge c 97 le c 102 |
| 248 | ret add ulong sub c 97 10 |
| 249 | end |
| 250 | ret 99 |
| 251 | end |
| 252 | |
| 253 | fn syn_hex_encode src view uchar * dst edit uchar * to void |
| 254 | n = len src |
| 255 | for i ulong n |
| 256 | b = get src i |
| 257 | hi = ulong shr b 4 |
| 258 | lo = ulong and b 15 |
| 259 | put dst mul i 2 get t_hex hi |
| 260 | put dst add mul i 2 1 get t_hex lo |
| 261 | end |
| 262 | end |
| 263 | |
| 264 | fn syn_hex_decode src view uchar * dst edit uchar * to bool |
| 265 | if ne len src 64 |
| 266 | ret false |
| 267 | end |
| 268 | if lt len dst 32 |
| 269 | ret false |
| 270 | end |
| 271 | for i ulong 32 |
| 272 | hi = syn_hex_val get src mul i 2 |
| 273 | lo = syn_hex_val get src add mul i 2 1 |
| 274 | if or gt hi 15 gt lo 15 |
| 275 | ret false |
| 276 | end |
| 277 | put dst i uchar or shl hi 4 lo |
| 278 | end |
| 279 | ret true |
| 280 | end |
| 281 | |
| 282 | fn syn_write_all fd int buf view uchar * n ulong to bool |
| 283 | pos = ulong 0 |
| 284 | while lt pos n bound 65536 |
| 285 | k = write fd buf pos sub n pos |
| 286 | if le k 0 |
| 287 | ret false |
| 288 | end |
| 289 | pos = add pos ulong k |
| 290 | end |
| 291 | ret true |
| 292 | end |
| 293 | |
| 294 | fn syn_write_text fd int t view uchar * to void |
| 295 | ok = syn_write_all fd t len t |
| 296 | if ok |
| 297 | ret |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | fn syn_write_arena fd int off ulong slen ulong to void |
| 302 | blk = new uchar 256 0 |
| 303 | pos = ulong 0 |
| 304 | while lt pos slen bound 4096 |
| 305 | k = sub slen pos |
| 306 | if gt k 256 |
| 307 | k = 256 |
| 308 | end |
| 309 | for i ulong k |
| 310 | put blk i get g_syn_str add add off pos i |
| 311 | end |
| 312 | ok = syn_write_all fd blk k |
| 313 | if eq ok false |
| 314 | ret |
| 315 | end |
| 316 | pos = add pos k |
| 317 | end |
| 318 | end |
| 319 | |
| 320 | fn syn_write_u64 fd int v ulong to void |
| 321 | buf = new uchar 24 0 |
| 322 | if eq v 0 |
| 323 | put buf 0 48 |
| 324 | ok = syn_write_all fd buf 1 |
| 325 | if ok |
| 326 | ret |
| 327 | end |
| 328 | ret |
| 329 | end |
| 330 | tmp = new uchar 24 0 |
| 331 | cnt = ulong 0 |
| 332 | x = v |
| 333 | while gt x 0 bound 32 |
| 334 | d = rem x 10 |
| 335 | put tmp cnt uchar add 48 d |
| 336 | x = div x 10 |
| 337 | cnt = add cnt 1 |
| 338 | end |
| 339 | for i ulong cnt |
| 340 | put buf i get tmp sub sub cnt 1 i |
| 341 | end |
| 342 | ok2 = syn_write_all fd buf cnt |
| 343 | if ok2 |
| 344 | ret |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | fn syn_write_hex32 fd int d view uchar * to void |
| 349 | hx = new uchar 64 0 |
| 350 | syn_hex_encode d hx |
| 351 | ok = syn_write_all fd hx 64 |
| 352 | if ok |
| 353 | ret |
| 354 | end |
| 355 | end |
| 356 | |
| 357 | note ======== path validation & overlap detection ======== |
| 358 | |
| 359 | note Validates repository-relative POSIX path per Synapse validate_repo_relative_path: |
| 360 | note 1..=256 bytes, no leading/trailing '/', no '//', no '\\', no NUL, no '.', '..', '.git' segments. |
| 361 | fn cs_path_valid p view uchar * to bool |
| 362 | n = len p |
| 363 | if or eq n 0 gt n 256 |
| 364 | ret false |
| 365 | end |
| 366 | if or eq get p 0 47 eq get p sub n 1 47 |
| 367 | ret false |
| 368 | end |
| 369 | seg_start = ulong 0 |
| 370 | for i ulong add n 1 |
| 371 | is_sep = false |
| 372 | if eq i n |
| 373 | is_sep = true |
| 374 | else |
| 375 | c = get p i |
| 376 | if or eq c 0 eq c 92 |
| 377 | ret false |
| 378 | end |
| 379 | if eq c 47 |
| 380 | is_sep = true |
| 381 | end |
| 382 | end |
| 383 | if is_sep |
| 384 | seg_len = sub i seg_start |
| 385 | if eq seg_len 0 |
| 386 | ret false |
| 387 | end |
| 388 | if eq seg_len 1 |
| 389 | if eq get p seg_start 46 |
| 390 | ret false |
| 391 | end |
| 392 | end |
| 393 | if eq seg_len 2 |
| 394 | if and eq get p seg_start 46 eq get p add seg_start 1 46 |
| 395 | ret false |
| 396 | end |
| 397 | end |
| 398 | if eq seg_len 4 |
| 399 | if eq get p seg_start 46 |
| 400 | if eq get p add seg_start 1 103 |
| 401 | if eq get p add seg_start 2 105 |
| 402 | if eq get p add seg_start 3 116 |
| 403 | ret false |
| 404 | end |
| 405 | end |
| 406 | end |
| 407 | end |
| 408 | end |
| 409 | seg_start = add i 1 |
| 410 | end |
| 411 | end |
| 412 | ret true |
| 413 | end |
| 414 | |
| 415 | note Path overlap between two arena paths: |
| 416 | note 0 = disjoint, 1 = exact match, 2 = directory prefix overlap (a is dir prefix of b or b of a) |
| 417 | fn cs_paths_overlap_arena ao ulong al ulong bo ulong bl ulong to ulong |
| 418 | if syn_arena_eq ao al bo bl |
| 419 | ret 1 |
| 420 | end |
| 421 | if lt al bl |
| 422 | if eq get g_syn_str add bo al 47 |
| 423 | if syn_arena_eq ao al bo al |
| 424 | ret 2 |
| 425 | end |
| 426 | end |
| 427 | end |
| 428 | if lt bl al |
| 429 | if eq get g_syn_str add ao bl 47 |
| 430 | if syn_arena_eq ao bl bo bl |
| 431 | ret 2 |
| 432 | end |
| 433 | end |
| 434 | end |
| 435 | ret 0 |
| 436 | end |
| 437 | |
| 438 | note Path overlap between two uchar views (0 disjoint, 1 exact, 2 dir prefix) |
| 439 | fn cs_paths_overlap_view a view uchar * b view uchar * to ulong |
| 440 | al = len a |
| 441 | bl = len b |
| 442 | if eq al bl |
| 443 | for i ulong al |
| 444 | if ne get a i get b i |
| 445 | ret 0 |
| 446 | end |
| 447 | end |
| 448 | ret 1 |
| 449 | end |
| 450 | if lt al bl |
| 451 | if eq get b al 47 |
| 452 | for i ulong al |
| 453 | if ne get a i get b i |
| 454 | ret 0 |
| 455 | end |
| 456 | end |
| 457 | ret 2 |
| 458 | end |
| 459 | ret 0 |
| 460 | end |
| 461 | if eq get a bl 47 |
| 462 | for i ulong bl |
| 463 | if ne get a i get b i |
| 464 | ret 0 |
| 465 | end |
| 466 | end |
| 467 | ret 2 |
| 468 | end |
| 469 | ret 0 |
| 470 | end |
| 471 | |
| 472 | note ======== changeset table accessors ======== |
| 473 | |
| 474 | fn cs_reset to void |
| 475 | put g_cs_hdr 0 0 |
| 476 | put g_cs_hdr 1 0 |
| 477 | put g_cs_stg_meta 0 0 |
| 478 | end |
| 479 | |
| 480 | fn cs_count to ulong |
| 481 | ret get g_cs_hdr 0 |
| 482 | end |
| 483 | |
| 484 | fn cs_mget idx ulong f ulong to ulong |
| 485 | ret get g_cs_meta add mul idx CS_FIELDS f |
| 486 | end |
| 487 | |
| 488 | fn cs_mput idx ulong f ulong v ulong to void |
| 489 | put g_cs_meta add mul idx CS_FIELDS f v |
| 490 | end |
| 491 | |
| 492 | fn cs_get_id idx ulong dst edit uchar * to void |
| 493 | base = mul idx 32 |
| 494 | for i ulong 32 |
| 495 | put dst i get g_cs_id add base i |
| 496 | end |
| 497 | end |
| 498 | |
| 499 | fn cs_get_parent idx ulong dst edit uchar * to void |
| 500 | base = mul idx 32 |
| 501 | for i ulong 32 |
| 502 | put dst i get g_cs_parent add base i |
| 503 | end |
| 504 | end |
| 505 | |
| 506 | fn cs_get_state idx ulong to ulong |
| 507 | ret cs_mget idx CSF_STATE |
| 508 | end |
| 509 | |
| 510 | fn cs_get_parent_idx idx ulong to ulong |
| 511 | ret cs_mget idx CSF_PIDX |
| 512 | end |
| 513 | |
| 514 | fn cs_get_author_off idx ulong to ulong |
| 515 | ret cs_mget idx CSF_AUTH_OFF |
| 516 | end |
| 517 | |
| 518 | fn cs_get_author_len idx ulong to ulong |
| 519 | ret cs_mget idx CSF_AUTH_LEN |
| 520 | end |
| 521 | |
| 522 | fn cs_get_branch_off idx ulong to ulong |
| 523 | ret cs_mget idx CSF_BR_OFF |
| 524 | end |
| 525 | |
| 526 | fn cs_get_branch_len idx ulong to ulong |
| 527 | ret cs_mget idx CSF_BR_LEN |
| 528 | end |
| 529 | |
| 530 | fn cs_get_ts idx ulong to ulong |
| 531 | ret cs_mget idx CSF_TS |
| 532 | end |
| 533 | |
| 534 | fn cs_get_path_cnt idx ulong to ulong |
| 535 | ret cs_mget idx CSF_PCNT |
| 536 | end |
| 537 | |
| 538 | fn cs_get_path_off idx ulong pi ulong to ulong |
| 539 | slot = add cs_mget idx CSF_PSTART pi |
| 540 | ret get g_cs_paths mul slot 2 |
| 541 | end |
| 542 | |
| 543 | fn cs_get_path_len idx ulong pi ulong to ulong |
| 544 | slot = add cs_mget idx CSF_PSTART pi |
| 545 | ret get g_cs_paths add mul slot 2 1 |
| 546 | end |
| 547 | |
| 548 | fn cs_find_by_id id view uchar * to ulong |
| 549 | if ne len id 32 |
| 550 | ret SYN_NONE |
| 551 | end |
| 552 | cnt = get g_cs_hdr 0 |
| 553 | for i ulong cnt |
| 554 | base = mul i 32 |
| 555 | eq_ok = true |
| 556 | for j ulong 32 |
| 557 | if ne get g_cs_id add base j get id j |
| 558 | eq_ok = false |
| 559 | break |
| 560 | end |
| 561 | end |
| 562 | if eq_ok |
| 563 | ret i |
| 564 | end |
| 565 | end |
| 566 | ret SYN_NONE |
| 567 | end |
| 568 | |
| 569 | note Pairwise path overlap between changesets idx_a and idx_b: 0 disjoint, 1 exact, 2 prefix |
| 570 | fn cs_changesets_overlap idx_a ulong idx_b ulong to ulong |
| 571 | na = cs_mget idx_a CSF_PCNT |
| 572 | nb = cs_mget idx_b CSF_PCNT |
| 573 | best = ulong 0 |
| 574 | for i ulong na |
| 575 | ao = cs_get_path_off idx_a i |
| 576 | al = cs_get_path_len idx_a i |
| 577 | for j ulong nb |
| 578 | bo = cs_get_path_off idx_b j |
| 579 | bl = cs_get_path_len idx_b j |
| 580 | ov = cs_paths_overlap_arena ao al bo bl |
| 581 | if eq ov 1 |
| 582 | ret 1 |
| 583 | end |
| 584 | if eq ov 2 |
| 585 | best = 2 |
| 586 | end |
| 587 | end |
| 588 | end |
| 589 | ret best |
| 590 | end |
| 591 | |
| 592 | note ======== canonical serialization & SHA-256 identity ======== |
| 593 | |
| 594 | note Computes domain-separated SHA-256 over canonical serialization of changeset idx into out_id[32]. |
| 595 | note Layout: "SYNAPSE_CHANGESET_V1\0" | u16_le(1) | parent_id[32] | str(author) | str(branch) | |
| 596 | note str(summary) | u64_le(created_ts) | u32_le(path_cnt) | str(path_i)* (sorted) |
| 597 | fn cs_compute_id idx ulong dst edit uchar * to void |
| 598 | ctx = new ulong 40 0 |
| 599 | par = new uchar 32 0 |
| 600 | nul = new uchar 1 0 |
| 601 | sha256_init ctx |
| 602 | sha256_update ctx t_cs_dom |
| 603 | sha256_update ctx nul |
| 604 | syn_sha_u16 ctx 1 |
| 605 | cs_get_parent idx par |
| 606 | sha256_update ctx par |
| 607 | syn_sha_arena_str ctx cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN |
| 608 | syn_sha_arena_str ctx cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN |
| 609 | syn_sha_arena_str ctx cs_mget idx CSF_SUM_OFF cs_mget idx CSF_SUM_LEN |
| 610 | syn_sha_u64 ctx cs_mget idx CSF_TS |
| 611 | pcnt = cs_mget idx CSF_PCNT |
| 612 | syn_sha_u32 ctx pcnt |
| 613 | for i ulong pcnt |
| 614 | syn_sha_arena_str ctx cs_get_path_off idx i cs_get_path_len idx i |
| 615 | end |
| 616 | sha256_final ctx dst |
| 617 | end |
| 618 | |
| 619 | note Helper to append bytes to a flat buffer at *pos, returning new pos (or SYN_NONE on overflow). |
| 620 | fn cs_buf_put_bytes dst edit uchar * pos ulong src view uchar * to ulong |
| 621 | n = len src |
| 622 | if gt add pos n len dst |
| 623 | ret SYN_NONE |
| 624 | end |
| 625 | for i ulong n |
| 626 | put dst add pos i get src i |
| 627 | end |
| 628 | ret add pos n |
| 629 | end |
| 630 | |
| 631 | fn cs_buf_put_arena_str dst edit uchar * pos ulong off ulong slen ulong to ulong |
| 632 | if gt add add pos 4 slen len dst |
| 633 | ret SYN_NONE |
| 634 | end |
| 635 | syn_put_u32_le dst pos slen |
| 636 | p = add pos 4 |
| 637 | for i ulong slen |
| 638 | put dst add p i get g_syn_str add off i |
| 639 | end |
| 640 | ret add p slen |
| 641 | end |
| 642 | |
| 643 | note Serializes changeset idx into canonical bytes in dst; returns byte length or SYN_NONE if dst too small. |
| 644 | fn cs_canonical_serialize idx ulong dst edit uchar * to ulong |
| 645 | par = new uchar 32 0 |
| 646 | pos = ulong 0 |
| 647 | pos = cs_buf_put_bytes dst pos t_cs_dom |
| 648 | if eq pos SYN_NONE |
| 649 | ret SYN_NONE |
| 650 | end |
| 651 | if ge add pos 3 len dst |
| 652 | ret SYN_NONE |
| 653 | end |
| 654 | put dst pos 0 |
| 655 | pos = add pos 1 |
| 656 | syn_put_u16_le dst pos 1 |
| 657 | pos = add pos 2 |
| 658 | cs_get_parent idx par |
| 659 | pos = cs_buf_put_bytes dst pos par |
| 660 | if eq pos SYN_NONE |
| 661 | ret SYN_NONE |
| 662 | end |
| 663 | pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN |
| 664 | if eq pos SYN_NONE |
| 665 | ret SYN_NONE |
| 666 | end |
| 667 | pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN |
| 668 | if eq pos SYN_NONE |
| 669 | ret SYN_NONE |
| 670 | end |
| 671 | pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_SUM_OFF cs_mget idx CSF_SUM_LEN |
| 672 | if eq pos SYN_NONE |
| 673 | ret SYN_NONE |
| 674 | end |
| 675 | if gt add pos 12 len dst |
| 676 | ret SYN_NONE |
| 677 | end |
| 678 | syn_put_u64_le dst pos cs_mget idx CSF_TS |
| 679 | pos = add pos 8 |
| 680 | pcnt = cs_mget idx CSF_PCNT |
| 681 | syn_put_u32_le dst pos pcnt |
| 682 | pos = add pos 4 |
| 683 | for i ulong pcnt |
| 684 | pos = cs_buf_put_arena_str dst pos cs_get_path_off idx i cs_get_path_len idx i |
| 685 | if eq pos SYN_NONE |
| 686 | ret SYN_NONE |
| 687 | end |
| 688 | end |
| 689 | ret pos |
| 690 | end |
| 691 | |
| 692 | note ======== changeset staging & creation ======== |
| 693 | |
| 694 | fn cs_stage_begin author view uchar * branch view uchar * ts ulong to ulong |
| 695 | if or eq len author 0 gt len author 128 |
| 696 | ret SYN_ERR_MALFORMED |
| 697 | end |
| 698 | if or eq len branch 0 gt len branch 128 |
| 699 | ret SYN_ERR_MALFORMED |
| 700 | end |
| 701 | ao = syn_arena_put author |
| 702 | if eq ao SYN_NONE |
| 703 | ret SYN_ERR_CAPACITY |
| 704 | end |
| 705 | bo = syn_arena_put branch |
| 706 | if eq bo SYN_NONE |
| 707 | ret SYN_ERR_CAPACITY |
| 708 | end |
| 709 | put g_cs_stg_meta 0 1 |
| 710 | put g_cs_stg_meta 1 ao |
| 711 | put g_cs_stg_meta 2 len author |
| 712 | put g_cs_stg_meta 3 bo |
| 713 | put g_cs_stg_meta 4 len branch |
| 714 | put g_cs_stg_meta 5 ts |
| 715 | put g_cs_stg_meta 6 0 |
| 716 | put g_cs_stg_meta 7 0 |
| 717 | put g_cs_stg_meta 8 0 |
| 718 | for i ulong 32 |
| 719 | put g_cs_stg_par i 0 |
| 720 | end |
| 721 | ret SYN_OK |
| 722 | end |
| 723 | |
| 724 | fn cs_stage_set_parent par view uchar * to ulong |
| 725 | if ne len par 32 |
| 726 | ret SYN_ERR_MALFORMED |
| 727 | end |
| 728 | for i ulong 32 |
| 729 | put g_cs_stg_par i get par i |
| 730 | end |
| 731 | ret SYN_OK |
| 732 | end |
| 733 | |
| 734 | fn cs_stage_set_summary summ view uchar * to ulong |
| 735 | if gt len summ 512 |
| 736 | ret SYN_ERR_MALFORMED |
| 737 | end |
| 738 | so = syn_arena_put summ |
| 739 | if eq so SYN_NONE |
| 740 | ret SYN_ERR_CAPACITY |
| 741 | end |
| 742 | put g_cs_stg_meta 7 so |
| 743 | put g_cs_stg_meta 8 len summ |
| 744 | ret SYN_OK |
| 745 | end |
| 746 | |
| 747 | fn cs_stage_shift_right cnt ulong ins ulong to void |
| 748 | k = cnt |
| 749 | while gt k ins bound 32 |
| 750 | prev = sub k 1 |
| 751 | v0 = get g_cs_stg_paths mul prev 2 |
| 752 | v1 = get g_cs_stg_paths add mul prev 2 1 |
| 753 | put g_cs_stg_paths mul k 2 v0 |
| 754 | put g_cs_stg_paths add mul k 2 1 v1 |
| 755 | k = prev |
| 756 | end |
| 757 | end |
| 758 | |
| 759 | note Adds a validated path into the staged path list, maintaining strict sorted order and rejecting duplicates. |
| 760 | fn cs_stage_add_path p view uchar * to ulong |
| 761 | if eq cs_path_valid p false |
| 762 | ret SYN_ERR_MALFORMED |
| 763 | end |
| 764 | cnt = get g_cs_stg_meta 6 |
| 765 | if ge cnt CS_MAX_PATHS |
| 766 | ret SYN_ERR_CAPACITY |
| 767 | end |
| 768 | po = syn_arena_put p |
| 769 | if eq po SYN_NONE |
| 770 | ret SYN_ERR_CAPACITY |
| 771 | end |
| 772 | pl = len p |
| 773 | note find insertion index while rejecting duplicates |
| 774 | ins = cnt |
| 775 | for i ulong cnt |
| 776 | eo = get g_cs_stg_paths mul i 2 |
| 777 | el = get g_cs_stg_paths add mul i 2 1 |
| 778 | cmp = syn_arena_cmp po pl eo el |
| 779 | if eq cmp 0 |
| 780 | ret SYN_ERR_DUPLICATE |
| 781 | end |
| 782 | if eq cmp 1 |
| 783 | ins = i |
| 784 | break |
| 785 | end |
| 786 | end |
| 787 | cs_stage_shift_right cnt ins |
| 788 | put g_cs_stg_paths mul ins 2 po |
| 789 | put g_cs_stg_paths add mul ins 2 1 pl |
| 790 | put g_cs_stg_meta 6 add cnt 1 |
| 791 | ret SYN_OK |
| 792 | end |
| 793 | |
| 794 | fn cs_stage_write_slot idx ulong pidx ulong pused ulong pcnt ulong to void |
| 795 | cs_mput idx CSF_AUTH_OFF get g_cs_stg_meta 1 |
| 796 | cs_mput idx CSF_AUTH_LEN get g_cs_stg_meta 2 |
| 797 | cs_mput idx CSF_BR_OFF get g_cs_stg_meta 3 |
| 798 | cs_mput idx CSF_BR_LEN get g_cs_stg_meta 4 |
| 799 | cs_mput idx CSF_STATE CS_STATE_DRAFT |
| 800 | cs_mput idx CSF_PSTART pused |
| 801 | cs_mput idx CSF_PCNT pcnt |
| 802 | cs_mput idx CSF_TS get g_cs_stg_meta 5 |
| 803 | cs_mput idx CSF_PIDX pidx |
| 804 | cs_mput idx CSF_SUM_OFF get g_cs_stg_meta 7 |
| 805 | cs_mput idx CSF_SUM_LEN get g_cs_stg_meta 8 |
| 806 | cs_mput idx CSF_USED 1 |
| 807 | base = mul idx 32 |
| 808 | for i ulong 32 |
| 809 | put g_cs_parent add base i get g_cs_stg_par i |
| 810 | end |
| 811 | for i ulong pcnt |
| 812 | slot = add pused i |
| 813 | v0 = get g_cs_stg_paths mul i 2 |
| 814 | v1 = get g_cs_stg_paths add mul i 2 1 |
| 815 | put g_cs_paths mul slot 2 v0 |
| 816 | put g_cs_paths add mul slot 2 1 v1 |
| 817 | end |
| 818 | end |
| 819 | |
| 820 | note Commits staged changeset, computes SHA-256 digest, links into DAG, invalidates prior branch reviews, |
| 821 | note and records CS_CREATE in the ledger. Writes new index to g_cs_hdr[2] and returns SYN_OK or error. |
| 822 | fn cs_stage_commit to ulong |
| 823 | par = new uchar 32 0 |
| 824 | id = new uchar 32 0 |
| 825 | if ne get g_cs_stg_meta 0 1 |
| 826 | ret SYN_ERR_MALFORMED |
| 827 | end |
| 828 | pcnt = get g_cs_stg_meta 6 |
| 829 | if eq pcnt 0 |
| 830 | ret SYN_ERR_MALFORMED |
| 831 | end |
| 832 | idx = get g_cs_hdr 0 |
| 833 | if ge idx CS_MAX |
| 834 | ret SYN_ERR_CAPACITY |
| 835 | end |
| 836 | pused = get g_cs_hdr 1 |
| 837 | if gt add pused pcnt CS_PATH_POOL |
| 838 | ret SYN_ERR_CAPACITY |
| 839 | end |
| 840 | for i ulong 32 |
| 841 | put par i get g_cs_stg_par i |
| 842 | end |
| 843 | pidx = SYN_NONE |
| 844 | if eq syn_digest_is_zero par false |
| 845 | pidx = cs_find_by_id par |
| 846 | if eq pidx SYN_NONE |
| 847 | ret SYN_ERR_NOT_FOUND |
| 848 | end |
| 849 | end |
| 850 | cs_stage_write_slot idx pidx pused pcnt |
| 851 | cs_compute_id idx id |
| 852 | if ne cs_find_by_id id SYN_NONE |
| 853 | cs_mput idx CSF_USED 0 |
| 854 | ret SYN_ERR_DUPLICATE |
| 855 | end |
| 856 | base = mul idx 32 |
| 857 | for i ulong 32 |
| 858 | put g_cs_id add base i get id i |
| 859 | end |
| 860 | put g_cs_hdr 0 add idx 1 |
| 861 | put g_cs_hdr 1 add pused pcnt |
| 862 | put g_cs_hdr 2 idx |
| 863 | put g_cs_stg_meta 0 0 |
| 864 | if ne pidx SYN_NONE |
| 865 | rc = gr_link_parent idx pidx |
| 866 | if ne rc SYN_OK |
| 867 | ret rc |
| 868 | end |
| 869 | end |
| 870 | rv_invalidate_branch cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN idx |
| 871 | if eq syn_arena_eq_view cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN t_br_main false |
| 872 | br_rc = gr_branch_set_arena cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN idx |
| 873 | if ne br_rc SYN_OK |
| 874 | ret br_rc |
| 875 | end |
| 876 | end |
| 877 | lg_set_subject id |
| 878 | seq = lg_append_arena 1 cs_mget idx CSF_TS cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN |
| 879 | if eq seq 0 |
| 880 | ret SYN_ERR_CAPACITY |
| 881 | end |
| 882 | ret SYN_OK |
| 883 | end |
| 884 | |
| 885 | fn cs_last_idx to ulong |
| 886 | ret get g_cs_hdr 2 |
| 887 | end |
| 888 | |
| 889 | note State transition rule: |
| 890 | note Allowed forward edges: |
| 891 | note DRAFT(0) -> PROPOSED(1) | ABANDONED(4) |
| 892 | note PROPOSED(1) -> REVIEWED(2) | ABANDONED(4) |
| 893 | note REVIEWED(2) -> LANDED(3) | ABANDONED(4) |
| 894 | note LANDED and ABANDONED are terminal; backward and skip moves are refused. |
| 895 | fn cs_state_move_allowed cur ulong nxt ulong to bool |
| 896 | if eq cur CS_STATE_DRAFT |
| 897 | ret or eq nxt CS_STATE_PROPOSED eq nxt CS_STATE_ABANDONED |
| 898 | end |
| 899 | if eq cur CS_STATE_PROPOSED |
| 900 | ret or eq nxt CS_STATE_REVIEWED eq nxt CS_STATE_ABANDONED |
| 901 | end |
| 902 | if eq cur CS_STATE_REVIEWED |
| 903 | ret or eq nxt CS_STATE_LANDED eq nxt CS_STATE_ABANDONED |
| 904 | end |
| 905 | ret false |
| 906 | end |
| 907 | |
| 908 | fn cs_write_state_name fd int st ulong to void |
| 909 | if eq st CS_STATE_DRAFT |
| 910 | syn_write_text fd t_st_draft |
| 911 | ret |
| 912 | end |
| 913 | if eq st CS_STATE_PROPOSED |
| 914 | syn_write_text fd t_st_prop |
| 915 | ret |
| 916 | end |
| 917 | if eq st CS_STATE_REVIEWED |
| 918 | syn_write_text fd t_st_rev |
| 919 | ret |
| 920 | end |
| 921 | if eq st CS_STATE_LANDED |
| 922 | syn_write_text fd t_st_land |
| 923 | ret |
| 924 | end |
| 925 | syn_write_text fd t_st_abnd |
| 926 | end |
| 927 | |
| 928 | fn cs_transition idx ulong nxt ulong ts ulong to ulong |
| 929 | id = new uchar 32 0 |
| 930 | if ge idx get g_cs_hdr 0 |
| 931 | ret SYN_ERR_NOT_FOUND |
| 932 | end |
| 933 | cur = cs_mget idx CSF_STATE |
| 934 | if eq cs_state_move_allowed cur nxt false |
| 935 | ret SYN_ERR_STATE_INVALID |
| 936 | end |
| 937 | cs_mput idx CSF_STATE nxt |
| 938 | cs_get_id idx id |
| 939 | lg_set_subject id |
| 940 | if eq nxt CS_STATE_PROPOSED |
| 941 | seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_prop |
| 942 | if eq seq 0 |
| 943 | ret SYN_ERR_CAPACITY |
| 944 | end |
| 945 | ret SYN_OK |
| 946 | end |
| 947 | if eq nxt CS_STATE_REVIEWED |
| 948 | seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_rev |
| 949 | if eq seq 0 |
| 950 | ret SYN_ERR_CAPACITY |
| 951 | end |
| 952 | ret SYN_OK |
| 953 | end |
| 954 | if eq nxt CS_STATE_LANDED |
| 955 | seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_land |
| 956 | if eq seq 0 |
| 957 | ret SYN_ERR_CAPACITY |
| 958 | end |
| 959 | ret SYN_OK |
| 960 | end |
| 961 | seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_abnd |
| 962 | if eq seq 0 |
| 963 | ret SYN_ERR_CAPACITY |
| 964 | end |
| 965 | ret SYN_OK |
| 966 | end |