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/core/hive/main.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 / core / hive / main.om` 645 lines ยท 11677 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note core/hive/main.om: Sovereign Agent Hive CLI. 3 note Reads hex hive commands line-by-line on stdin and emits state, geometry, 4 note routing, and task-lease results on stdout. Exits nonzero on any refused 5 note or malformed command. 6 note Compose with: si/directives/om2.om core/hive/lib.om core/hive/main.om 7 8 text h_cmd_init "INIT" 9 text h_cmd_place "PLACE" 10 text h_cmd_auto "AUTO" 11 text h_cmd_block "BLOCK" 12 text h_cmd_unblk "UNBLOCK" 13 text h_cmd_dist "DIST" 14 text h_cmd_nbrs "NEIGHBORS" 15 text h_cmd_ring "RING" 16 text h_cmd_route "ROUTE" 17 text h_cmd_task "TASK" 18 text h_cmd_claim "CLAIM" 19 text h_cmd_asgn "ASSIGN" 20 text h_cmd_tick "TICK" 21 text h_cmd_stat "STAT" 22 23 text h_ok_init "OK INIT\n" 24 text h_ok_place "OK PLACE aid=" 25 text h_ok_auto "OK AUTO aid=" 26 text h_ok_blk "OK BLOCK " 27 text h_ok_unblk "OK UNBLOCK " 28 text h_ok_dist "OK DIST d=" 29 text h_ok_nbrs "OK NEIGHBORS" 30 text h_ok_ring "OK RING k=" 31 text h_ok_route "OK ROUTE hops=" 32 text h_ok_task "OK TASK tid=" 33 text h_ok_claim "OK CLAIM tid=" 34 text h_ok_asgn "OK ASSIGN tid=" 35 text h_ok_tick "OK TICK reclaimed=" 36 text h_ok_stat "OK STAT agents=" 37 text h_err_pfx "ERR code=" 38 text h_sp " " 39 text h_lp "(" 40 text h_cm "," 41 text h_rp ")" 42 text h_nl "\n" 43 text h_k_aid " aid=" 44 text h_k_q " q=" 45 text h_k_r " r=" 46 text h_k_cnt " cells=" 47 text h_k_blk " blocked=" 48 text h_k_tk " tasks=" 49 text h_k_md " max_dist=" 50 51 fn h_eq_slice a view uchar * b view uchar * to bool 52 if ne len a len b 53 ret false 54 end 55 n = len a 56 i = ulong 0 57 while lt i n bound 65536 58 if ne get a i get b i 59 ret false 60 end 61 i = add i 1 62 end 63 ret true 64 end 65 66 fn h_is_space c uchar to bool 67 if eq c 32 68 ret true 69 end 70 if eq c 9 71 ret true 72 end 73 ret eq c 13 74 end 75 76 fn h_next_tok line view uchar * pos edit ulong 1 dst edit ulong 2 to bool 77 note dst[0]=start, dst[1]=len 78 n = len line 79 p = get pos 0 80 while lt p n bound 65536 81 if not h_is_space get line p 82 break 83 end 84 p = add p 1 85 end 86 if ge p n 87 put pos 0 p 88 ret false 89 end 90 st = p 91 while lt p n bound 65536 92 if h_is_space get line p 93 break 94 end 95 p = add p 1 96 end 97 put dst 0 st 98 put dst 1 sub p st 99 put pos 0 p 100 ret true 101 end 102 103 fn h_parse_i32 tok view uchar * dst edit int 1 to bool 104 n = len tok 105 if eq n 0 106 ret false 107 end 108 if gt n 11 109 ret false 110 end 111 idx = ulong 0 112 is_neg = false 113 if eq get tok 0 45 114 is_neg = true 115 idx = ulong 1 116 if eq n 1 117 ret false 118 end 119 end 120 acc = long 0 121 while lt idx n bound 12 122 c = get tok idx 123 if or lt c 48 gt c 57 124 ret false 125 end 126 acc = add mul acc 10 long sub c 48 127 if gt acc `2147483648` 128 ret false 129 end 130 idx = add idx 1 131 end 132 if is_neg 133 acc = sub 0 acc 134 else 135 if gt acc `2147483647` 136 ret false 137 end 138 end 139 put dst 0 int acc 140 ret true 141 end 142 143 fn h_read_ints line view uchar * pos edit ulong 1 vals edit int 8 want uint to bool 144 tok = new ulong 2 0 145 v = new int 1 0 146 i = uint 0 147 while lt i want bound 8 148 if not h_next_tok line pos tok 149 ret false 150 end 151 if not h_parse_i32 part line get tok 0 get tok 1 v 152 ret false 153 end 154 put vals ulong i get v 0 155 i = add i 1 156 end 157 ret true 158 end 159 160 fn h_emit_err code uint to void 161 w1 = hive_write_all 1 h_err_pfx 162 w2 = hive_write_u64 1 ulong code 163 w3 = hive_write_all 1 h_nl 164 if and w1 and w2 w3 165 ret 166 end 167 exit 2 168 end 169 170 fn h_emit_pair q int r int to void 171 w1 = hive_write_all 1 h_lp 172 w2 = hive_write_i32 1 q 173 w3 = hive_write_all 1 h_cm 174 w4 = hive_write_i32 1 r 175 w5 = hive_write_all 1 h_rp 176 if and w1 and w2 and w3 and w4 w5 177 ret 178 end 179 exit 2 180 end 181 182 fn h_cmd_do_route args view int 8 to bool 183 src = new HexCoord get args 0 get args 1 184 dst = new HexCoord get args 2 get args 3 185 pq = new int 289 0 186 pr = new int 289 0 187 hops = hive_route src dst pq pr 188 if lt hops 0 189 h_emit_err uint neg hops 190 ret false 191 end 192 w1 = hive_write_all 1 h_ok_route 193 w2 = hive_write_i32 1 hops 194 if not and w1 w2 195 exit 2 196 end 197 n = add ulong hops 1 198 i = ulong 0 199 while lt i n bound 289 200 w = hive_write_all 1 h_sp 201 if not w 202 exit 2 203 end 204 h_emit_pair get pq i get pr i 205 i = add i 1 206 end 207 wn = hive_write_all 1 h_nl 208 if not wn 209 exit 2 210 end 211 ret true 212 end 213 214 fn h_cmd_do_ring args view int 8 to bool 215 ctr = new HexCoord get args 0 get args 1 216 rqs = new int 192 0 217 rrs = new int 192 0 218 if lt get args 2 0 219 h_emit_err HIVE_ERR_BAD_ARG 220 ret false 221 end 222 k = uint get args 2 223 cnt = hive_ring ctr k rqs rrs 224 if eq cnt 0 225 h_emit_err HIVE_ERR_BAD_ARG 226 ret false 227 end 228 w1 = hive_write_all 1 h_ok_ring 229 w2 = hive_write_u64 1 ulong k 230 w3 = hive_write_all 1 h_k_cnt 231 w4 = hive_write_u64 1 ulong cnt 232 if not and w1 and w2 and w3 w4 233 exit 2 234 end 235 i = ulong 0 236 while lt i ulong cnt bound 192 237 w = hive_write_all 1 h_sp 238 if not w 239 exit 2 240 end 241 h_emit_pair get rqs i get rrs i 242 i = add i 1 243 end 244 wn = hive_write_all 1 h_nl 245 if not wn 246 exit 2 247 end 248 ret true 249 end 250 251 fn h_cmd_do_nbrs args view int 8 to bool 252 qs = new int 6 0 253 rs = new int 6 0 254 hive_neighbors get args 0 get args 1 qs rs 255 w0 = hive_write_all 1 h_ok_nbrs 256 if not w0 257 exit 2 258 end 259 for i ulong 6 260 ws = hive_write_all 1 h_sp 261 if not ws 262 exit 2 263 end 264 h_emit_pair get qs i get rs i 265 end 266 wn = hive_write_all 1 h_nl 267 if not wn 268 exit 2 269 end 270 ret true 271 end 272 273 fn h_must ok bool to void 274 if not ok 275 exit 2 276 end 277 end 278 279 fn h_cmd_do_stat to bool 280 bd = new HiveBounds 0 0 0 0 0 0 281 hive_compute_bounds bd 282 h_must hive_write_all 1 h_ok_stat 283 h_must hive_write_u64 1 ulong get bd cnt 284 h_must hive_write_all 1 h_k_blk 285 h_must hive_write_i32 1 get g_meta 5 286 h_must hive_write_all 1 h_k_tk 287 h_must hive_write_i32 1 get g_meta 4 288 h_must hive_write_all 1 h_k_md 289 h_must hive_write_i32 1 get bd max_dist 290 h_must hive_write_all 1 h_nl 291 ret true 292 end 293 294 fn h_exec_geom cmd view uchar * line view uchar * pos edit ulong 1 to int 295 note returns 0=not handled, 1=ok, -1=err 296 args = new int 8 0 297 if h_eq_slice cmd h_cmd_dist 298 if not h_read_ints line pos args 4 299 h_emit_err HIVE_ERR_BAD_ARG 300 ret -1 301 end 302 d = hive_hex_dist get args 0 get args 1 get args 2 get args 3 303 w1 = hive_write_all 1 h_ok_dist 304 w2 = hive_write_i32 1 d 305 w3 = hive_write_all 1 h_nl 306 if and w1 and w2 w3 307 ret 1 308 end 309 exit 2 310 end 311 if h_eq_slice cmd h_cmd_nbrs 312 if not h_read_ints line pos args 2 313 h_emit_err HIVE_ERR_BAD_ARG 314 ret -1 315 end 316 if h_cmd_do_nbrs args 317 ret 1 318 end 319 ret -1 320 end 321 if h_eq_slice cmd h_cmd_ring 322 if not h_read_ints line pos args 3 323 h_emit_err HIVE_ERR_BAD_ARG 324 ret -1 325 end 326 if h_cmd_do_ring args 327 ret 1 328 end 329 ret -1 330 end 331 if h_eq_slice cmd h_cmd_route 332 if not h_read_ints line pos args 4 333 h_emit_err HIVE_ERR_BAD_ARG 334 ret -1 335 end 336 if h_cmd_do_route args 337 ret 1 338 end 339 ret -1 340 end 341 ret 0 342 end 343 344 fn h_exec_grid cmd view uchar * line view uchar * pos edit ulong 1 to int 345 args = new int 8 0 346 pt = new HexCoord 0 0 347 if h_eq_slice cmd h_cmd_init 348 if not h_read_ints line pos args 3 349 h_emit_err HIVE_ERR_BAD_ARG 350 ret -1 351 end 352 if or lt get args 1 1 lt get args 2 1 353 h_emit_err HIVE_ERR_BAD_ARG 354 ret -1 355 end 356 rc = hive_init get args 0 uint get args 1 uint get args 2 357 if ne rc HIVE_OK 358 h_emit_err rc 359 ret -1 360 end 361 if hive_write_all 1 h_ok_init 362 ret 1 363 end 364 exit 2 365 end 366 if h_eq_slice cmd h_cmd_place 367 if not h_read_ints line pos args 5 368 h_emit_err HIVE_ERR_BAD_ARG 369 ret -1 370 end 371 if lt get args 0 1 372 h_emit_err HIVE_ERR_BAD_ARG 373 ret -1 374 end 375 if or lt get args 3 0 gt get args 3 255 376 h_emit_err HIVE_ERR_BAD_ARG 377 ret -1 378 end 379 if lt get args 4 1 380 h_emit_err HIVE_ERR_BAD_ARG 381 ret -1 382 end 383 rc = hive_place_agent uint get args 0 get args 1 get args 2 uchar get args 3 uint get args 4 384 if ne rc HIVE_OK 385 h_emit_err rc 386 ret -1 387 end 388 h_must hive_write_all 1 h_ok_place 389 h_must hive_write_i32 1 get args 0 390 h_must hive_write_all 1 h_k_q 391 h_must hive_write_i32 1 get args 1 392 h_must hive_write_all 1 h_k_r 393 h_must hive_write_i32 1 get args 2 394 h_must hive_write_all 1 h_nl 395 ret 1 396 end 397 if h_eq_slice cmd h_cmd_auto 398 if not h_read_ints line pos args 4 399 h_emit_err HIVE_ERR_BAD_ARG 400 ret -1 401 end 402 if or lt get args 0 1 lt get args 1 0 403 h_emit_err HIVE_ERR_BAD_ARG 404 ret -1 405 end 406 if or lt get args 2 0 gt get args 2 255 407 h_emit_err HIVE_ERR_BAD_ARG 408 ret -1 409 end 410 if lt get args 3 1 411 h_emit_err HIVE_ERR_BAD_ARG 412 ret -1 413 end 414 rc = hive_place_auto uint get args 0 uint get args 1 uchar get args 2 uint get args 3 pt 415 if ne rc HIVE_OK 416 h_emit_err rc 417 ret -1 418 end 419 h_must hive_write_all 1 h_ok_auto 420 h_must hive_write_i32 1 get args 0 421 h_must hive_write_all 1 h_k_q 422 h_must hive_write_i32 1 get pt q 423 h_must hive_write_all 1 h_k_r 424 h_must hive_write_i32 1 get pt r 425 h_must hive_write_all 1 h_nl 426 ret 1 427 end 428 if h_eq_slice cmd h_cmd_block 429 if not h_read_ints line pos args 2 430 h_emit_err HIVE_ERR_BAD_ARG 431 ret -1 432 end 433 rc = hive_block_cell get args 0 get args 1 434 if ne rc HIVE_OK 435 h_emit_err rc 436 ret -1 437 end 438 w1 = hive_write_all 1 h_ok_blk 439 if not w1 440 exit 2 441 end 442 h_emit_pair get args 0 get args 1 443 if hive_write_all 1 h_nl 444 ret 1 445 end 446 exit 2 447 end 448 if h_eq_slice cmd h_cmd_unblk 449 if not h_read_ints line pos args 2 450 h_emit_err HIVE_ERR_BAD_ARG 451 ret -1 452 end 453 rc = hive_unblock_cell get args 0 get args 1 454 if ne rc HIVE_OK 455 h_emit_err rc 456 ret -1 457 end 458 w1 = hive_write_all 1 h_ok_unblk 459 if not w1 460 exit 2 461 end 462 h_emit_pair get args 0 get args 1 463 if hive_write_all 1 h_nl 464 ret 1 465 end 466 exit 2 467 end 468 ret 0 469 end 470 471 fn h_exec_task cmd view uchar * line view uchar * pos edit ulong 1 to int 472 args = new int 8 0 473 asg = new uint 1 0 474 if h_eq_slice cmd h_cmd_task 475 if not h_read_ints line pos args 4 476 h_emit_err HIVE_ERR_BAD_ARG 477 ret -1 478 end 479 if or lt get args 0 1 lt get args 3 0 480 h_emit_err HIVE_ERR_BAD_ARG 481 ret -1 482 end 483 rc = hive_task_create uint get args 0 get args 1 get args 2 uint get args 3 484 if ne rc HIVE_OK 485 h_emit_err rc 486 ret -1 487 end 488 w1 = hive_write_all 1 h_ok_task 489 w2 = hive_write_i32 1 get args 0 490 w3 = hive_write_all 1 h_nl 491 if and w1 and w2 w3 492 ret 1 493 end 494 exit 2 495 end 496 if h_eq_slice cmd h_cmd_claim 497 if not h_read_ints line pos args 3 498 h_emit_err HIVE_ERR_BAD_ARG 499 ret -1 500 end 501 if or lt get args 0 1 lt get args 1 1 502 h_emit_err HIVE_ERR_BAD_ARG 503 ret -1 504 end 505 if lt get args 2 1 506 h_emit_err HIVE_ERR_BAD_ARG 507 ret -1 508 end 509 rc = hive_task_claim uint get args 0 uint get args 1 ulong get args 2 510 if ne rc HIVE_OK 511 h_emit_err rc 512 ret -1 513 end 514 h_must hive_write_all 1 h_ok_claim 515 h_must hive_write_i32 1 get args 0 516 h_must hive_write_all 1 h_k_aid 517 h_must hive_write_i32 1 get args 1 518 h_must hive_write_all 1 h_nl 519 ret 1 520 end 521 if h_eq_slice cmd h_cmd_asgn 522 if not h_read_ints line pos args 2 523 h_emit_err HIVE_ERR_BAD_ARG 524 ret -1 525 end 526 if or lt get args 0 1 lt get args 1 1 527 h_emit_err HIVE_ERR_BAD_ARG 528 ret -1 529 end 530 rc = hive_assign_nearest uint get args 0 ulong get args 1 asg 531 if ne rc HIVE_OK 532 h_emit_err rc 533 ret -1 534 end 535 h_must hive_write_all 1 h_ok_asgn 536 h_must hive_write_i32 1 get args 0 537 h_must hive_write_all 1 h_k_aid 538 h_must hive_write_u64 1 ulong get asg 0 539 h_must hive_write_all 1 h_nl 540 ret 1 541 end 542 if h_eq_slice cmd h_cmd_tick 543 if not h_read_ints line pos args 1 544 h_emit_err HIVE_ERR_BAD_ARG 545 ret -1 546 end 547 if lt get args 0 0 548 h_emit_err HIVE_ERR_BAD_ARG 549 ret -1 550 end 551 rec = hive_tick ulong get args 0 552 h_must hive_write_all 1 h_ok_tick 553 h_must hive_write_u64 1 ulong rec 554 h_must hive_write_all 1 h_nl 555 ret 1 556 end 557 if h_eq_slice cmd h_cmd_stat 558 if h_cmd_do_stat 559 ret 1 560 end 561 ret -1 562 end 563 ret 0 564 end 565 566 fn h_dispatch_line line view uchar * to bool 567 pos = new ulong 1 0 568 tok = new ulong 2 0 569 cbuf = new uchar 16 0 570 if not h_next_tok line pos tok 571 ret true 572 end 573 cs = get tok 0 574 cl = get tok 1 575 if eq get line cs 35 576 ret true 577 end 578 if gt cl 16 579 h_emit_err HIVE_ERR_BAD_ARG 580 ret false 581 end 582 i = ulong 0 583 while lt i cl bound 16 584 put cbuf i get line add cs i 585 i = add i 1 586 end 587 r1 = h_exec_geom part cbuf 0 cl line pos 588 if ne r1 0 589 ret gt r1 0 590 end 591 r2 = h_exec_grid part cbuf 0 cl line pos 592 if ne r2 0 593 ret gt r2 0 594 end 595 r3 = h_exec_task part cbuf 0 cl line pos 596 if ne r3 0 597 ret gt r3 0 598 end 599 h_emit_err HIVE_ERR_BAD_ARG 600 ret false 601 end 602 603 fn h_find_nl buf view uchar * start ulong to ulong 604 n = len buf 605 p = start 606 while lt p n bound 65536 607 if eq get buf p 10 608 ret p 609 end 610 p = add p 1 611 end 612 ret n 613 end 614 615 fn main to int 616 raw = new uchar 65536 0 617 rc0 = hive_init 6 64 64 618 if ne rc0 HIVE_OK 619 ret 2 620 end 621 tot = ulong 0 622 while lt tot 65536 bound 65536 623 k = read 0 raw tot sub 65536 tot 624 if lt k 0 625 ret 2 626 end 627 if eq k 0 628 break 629 end 630 tot = add tot ulong k 631 end 632 had_err = false 633 pos = ulong 0 634 while lt pos tot bound 65536 635 lend = h_find_nl part raw 0 tot pos 636 if not h_dispatch_line part raw pos sub lend pos 637 had_err = true 638 end 639 pos = add lend 1 640 end 641 if had_err 642 ret 1 643 end 644 ret 0 645 end