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/lib.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 / lib.om` 936 lines ยท 18597 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note core/hive/lib.om: Pure-Omega Sovereign Agent Hive Engine with 2D Axial Hexagonal Geometry. 3 note Faithful OM-2 library (no om2 line, no fn main) derived from upstream spark-hive 4 note (geometry.rs, store.rs, emission.rs). 5 note Compose with: si/directives/om2.om core/hive/lib.om 6 7 struct HexCoord q int r int end 8 9 struct CubeCoord x int y int z int end 10 11 struct HiveBounds min_q int max_q int min_r int max_r int cnt uint max_dist int end 12 13 note Geometry constants (matching spark-hive/src/geometry.rs) 14 const HIVE_DEFAULT_RADIUS f64 88.0 15 const HIVE_SQRT3 f64 1.`7320508075688773` 16 17 note Fixed lattice & registry bounds (radius <= 8 -> 17x17 = 289 grid slots) 18 const HIVE_GRID_R int 8 19 const HIVE_GRID_SPAN int 17 20 const HIVE_GRID_CAP ulong 289 21 const HIVE_MAX_AGENTS ulong 128 22 const HIVE_MAX_TASKS ulong 128 23 24 note Cell occupancy states 25 const HIVE_CELL_EMPTY uchar 0 26 const HIVE_CELL_OCC uchar 1 27 const HIVE_CELL_BLOCKED uchar 2 28 29 note Agent roles 30 const HIVE_ROLE_COORD uchar 0 31 const HIVE_ROLE_RESEARCH uchar 1 32 const HIVE_ROLE_SOCRATIC uchar 2 33 const HIVE_ROLE_VERIFIER uchar 3 34 const HIVE_ROLE_ADAPTER uchar 4 35 const HIVE_ROLE_WORKER uchar 5 36 37 note Placement intents 38 const HIVE_INTENT_INDEP uchar 0 39 const HIVE_INTENT_JOIN uchar 1 40 const HIVE_INTENT_BRANCH uchar 2 41 const HIVE_INTENT_MEET uchar 3 42 43 note Zoom detail tiers 44 const HIVE_TIER_DOT uint 0 45 const HIVE_TIER_COMPACT uint 1 46 const HIVE_TIER_FULL uint 2 47 48 note Forge task states 49 const HIVE_TASK_NONE uchar 0 50 const HIVE_TASK_OPEN uchar 1 51 const HIVE_TASK_CLAIMED uchar 2 52 const HIVE_TASK_SUBMITTED uchar 3 53 const HIVE_TASK_VERIFIED uchar 4 54 const HIVE_TASK_FAILED uchar 5 55 56 note Fail-closed status / refusal codes 57 const HIVE_OK uint 0 58 const HIVE_ERR_OOB uint 1 59 const HIVE_ERR_COLLISION uint 2 60 const HIVE_ERR_BLOCKED uint 3 61 const HIVE_ERR_DUP_AGENT uint 4 62 const HIVE_ERR_CAPACITY uint 5 63 const HIVE_ERR_NOT_FOUND uint 6 64 const HIVE_ERR_NO_ROUTE uint 7 65 const HIVE_ERR_CLAIMED uint 8 66 const HIVE_ERR_UNAUTH uint 9 67 const HIVE_ERR_BAD_ARG uint 10 68 69 note Global lattice, agent registry, and task lease state (program-mode state) 70 note g_meta: [0]=max_radius [1]=max_agents [2]=agent_cnt [3]=max_tasks [4]=task_cnt [5]=blocked_cnt [6]=revision 71 global g_meta int 8 72 global g_clock ulong 1 73 global g_cell_st uchar 289 74 global g_cell_ag uint 289 75 global g_ag_act uchar 129 76 global g_ag_q int 129 77 global g_ag_r int 129 78 global g_ag_role uchar 129 79 global g_ag_load uint 129 80 global g_ag_cap uint 129 81 global g_tk_st uchar 129 82 global g_tk_q int 129 83 global g_tk_r int 129 84 global g_tk_ring uint 129 85 global g_tk_own uint 129 86 global g_tk_exp ulong 129 87 88 note ---- 2D Axial & Cube Hexagonal Geometry ---- 89 90 fn hive_abs x int to int 91 if lt x 0 92 ret neg x 93 end 94 ret x 95 end 96 97 fn hive_max2 a int b int to int 98 if gt a b 99 ret a 100 end 101 ret b 102 end 103 104 fn hive_axial_to_cube q int r int dst edit CubeCoord to void 105 put dst x q 106 put dst y neg add q r 107 put dst z r 108 end 109 110 fn hive_cube_valid c view CubeCoord to bool 111 s = add add get c x get c y get c z 112 ret eq s 0 113 end 114 115 fn hive_hex_dist q1 int r1 int q2 int r2 int to int 116 dq = sub q1 q2 117 dr = sub r1 r2 118 ds = add dq dr 119 m = hive_max2 hive_abs dq hive_abs dr 120 ret hive_max2 m hive_abs ds 121 end 122 123 fn hive_cube_dist a view CubeCoord b view CubeCoord to int 124 dx = hive_abs sub get a x get b x 125 dy = hive_abs sub get a y get b y 126 dz = hive_abs sub get a z get b z 127 ret div add add dx dy dz 2 128 end 129 130 note 6 canonical axial directions (spark-hive/src/geometry.rs): 131 note 0: (+1, 0) East 1: (+1,-1) Northeast 2: ( 0,-1) Northwest 132 note 3: (-1, 0) West 4: (-1,+1) Southwest 5: ( 0,+1) Southeast 133 134 fn hive_dir_dq d uint to int 135 if eq d 0 136 ret 1 137 end 138 if eq d 1 139 ret 1 140 end 141 if eq d 2 142 ret 0 143 end 144 if eq d 3 145 ret -1 146 end 147 if eq d 4 148 ret -1 149 end 150 if eq d 5 151 ret 0 152 end 153 ret 0 154 end 155 156 fn hive_dir_dr d uint to int 157 if eq d 0 158 ret 0 159 end 160 if eq d 1 161 ret -1 162 end 163 if eq d 2 164 ret -1 165 end 166 if eq d 3 167 ret 0 168 end 169 if eq d 4 170 ret 1 171 end 172 if eq d 5 173 ret 1 174 end 175 ret 0 176 end 177 178 fn hive_neighbor q int r int d uint dst edit HexCoord to bool 179 if ge d 6 180 ret false 181 end 182 put dst q add q hive_dir_dq d 183 put dst r add r hive_dir_dr d 184 ret true 185 end 186 187 fn hive_neighbors q int r int qs edit int 6 rs edit int 6 to void 188 for d ulong 6 189 du = uint d 190 put qs d add q hive_dir_dq du 191 put rs d add r hive_dir_dr du 192 end 193 end 194 195 fn hive_f64_round x f64 to int 196 if ge x 0.0f64 197 ret int add x 0.5f64 198 end 199 ret int sub x 0.5f64 200 end 201 202 fn hive_axial_to_pixel_x q int r int rad f64 to f64 203 qf = f64 q 204 rf = f64 r 205 ret mul mul rad HIVE_SQRT3 add qf div rf 2.0f64 206 end 207 208 fn hive_axial_to_pixel_y r int rad f64 to f64 209 ret mul mul rad 1.5f64 f64 r 210 end 211 212 fn hive_round_axial cx f64 cz f64 dst edit HexCoord to void 213 cy = sub neg cx cz 214 rx = f64 hive_f64_round cx 215 ry = f64 hive_f64_round cy 216 rz = f64 hive_f64_round cz 217 dx = fabs sub rx cx 218 dy = fabs sub ry cy 219 dz = fabs sub rz cz 220 if and gt dx dy gt dx dz 221 rx = sub neg ry rz 222 else 223 if gt dz dy 224 rz = sub neg rx ry 225 end 226 end 227 put dst q int rx 228 put dst r int rz 229 end 230 231 fn hive_pixel_to_axial x f64 y f64 rad f64 dst edit HexCoord to bool 232 if le rad 0.0f64 233 ret false 234 end 235 q = div sub mul div HIVE_SQRT3 3.0f64 x div y 3.0f64 rad 236 r = div mul div 2.0f64 3.0f64 y rad 237 hive_round_axial q r dst 238 ret true 239 end 240 241 fn hive_detail_tier zoom f64 to uint 242 if ge zoom 0.62f64 243 ret HIVE_TIER_FULL 244 end 245 if ge zoom 0.34f64 246 ret HIVE_TIER_COMPACT 247 end 248 ret HIVE_TIER_DOT 249 end 250 251 note ---- Hex Ring & Spiral Enumeration ---- 252 253 fn hive_ring ctr view HexCoord k uint qs edit int * rs edit int * to uint 254 if eq k 0 255 if or lt len qs 1 lt len rs 1 256 ret 0 257 end 258 put qs 0 get ctr q 259 put rs 0 get ctr r 260 ret 1 261 end 262 if gt k 32 263 ret 0 264 end 265 if or lt len qs ulong mul k 6 lt len rs ulong mul k 6 266 ret 0 267 end 268 cur_q = add get ctr q mul hive_dir_dq 4 int k 269 cur_r = add get ctr r mul hive_dir_dr 4 int k 270 idx = ulong 0 271 for d ulong 6 272 step = uint 0 273 while lt step k bound 32 274 put qs idx cur_q 275 put rs idx cur_r 276 idx = add idx 1 277 cur_q = add cur_q hive_dir_dq uint d 278 cur_r = add cur_r hive_dir_dr uint d 279 step = add step 1 280 end 281 end 282 ret uint idx 283 end 284 285 fn hive_spiral ctr view HexCoord max_k uint qs edit int * rs edit int * to uint 286 if gt max_k 16 287 ret 0 288 end 289 tot = ulong 0 290 k = uint 0 291 while le k max_k bound 17 292 need = ulong 1 293 if gt k 0 294 need = ulong mul k 6 295 end 296 if or gt add tot need len qs gt add tot need len rs 297 ret 0 298 end 299 got = hive_ring ctr k part qs tot need part rs tot need 300 if eq got 0 301 ret 0 302 end 303 tot = add tot ulong got 304 k = add k 1 305 end 306 ret uint tot 307 end 308 309 note ---- Lattice State, Bounds, Occupancy & Agent Registry ---- 310 311 fn hive_init max_rad int max_ag uint max_tk uint to uint 312 if or lt max_rad 1 gt max_rad HIVE_GRID_R 313 ret HIVE_ERR_BAD_ARG 314 end 315 if or eq max_ag 0 gt ulong max_ag HIVE_MAX_AGENTS 316 ret HIVE_ERR_BAD_ARG 317 end 318 if or eq max_tk 0 gt ulong max_tk HIVE_MAX_TASKS 319 ret HIVE_ERR_BAD_ARG 320 end 321 put g_meta 0 max_rad 322 put g_meta 1 int max_ag 323 put g_meta 2 0 324 put g_meta 3 int max_tk 325 put g_meta 4 0 326 put g_meta 5 0 327 put g_meta 6 0 328 put g_clock 0 0 329 for i ulong 289 330 put g_cell_st i HIVE_CELL_EMPTY 331 put g_cell_ag i 0 332 end 333 for i ulong 129 334 put g_ag_act i 0 335 put g_ag_q i 0 336 put g_ag_r i 0 337 put g_ag_role i 0 338 put g_ag_load i 0 339 put g_ag_cap i 0 340 put g_tk_st i HIVE_TASK_NONE 341 put g_tk_q i 0 342 put g_tk_r i 0 343 put g_tk_ring i 0 344 put g_tk_own i 0 345 put g_tk_exp i 0 346 end 347 ret HIVE_OK 348 end 349 350 fn hive_in_bounds q int r int to bool 351 if or lt q neg HIVE_GRID_R gt q HIVE_GRID_R 352 ret false 353 end 354 if or lt r neg HIVE_GRID_R gt r HIVE_GRID_R 355 ret false 356 end 357 mrad = get g_meta 0 358 ret le hive_hex_dist 0 0 q r mrad 359 end 360 361 fn hive_slot q int r int to ulong 362 qq = add q HIVE_GRID_R 363 rr = add r HIVE_GRID_R 364 ret ulong add mul qq HIVE_GRID_SPAN rr 365 end 366 367 fn hive_slot_q sl ulong to int 368 ret sub int div sl ulong HIVE_GRID_SPAN HIVE_GRID_R 369 end 370 371 fn hive_slot_r sl ulong to int 372 ret sub int rem sl ulong HIVE_GRID_SPAN HIVE_GRID_R 373 end 374 375 fn hive_cell_state q int r int to uchar 376 if not hive_in_bounds q r 377 ret 255 378 end 379 ret get g_cell_st hive_slot q r 380 end 381 382 fn hive_cell_agent q int r int to uint 383 if not hive_in_bounds q r 384 ret 0 385 end 386 ret get g_cell_ag hive_slot q r 387 end 388 389 fn hive_block_cell q int r int to uint 390 if not hive_in_bounds q r 391 ret HIVE_ERR_OOB 392 end 393 sl = hive_slot q r 394 st = get g_cell_st sl 395 if eq st HIVE_CELL_OCC 396 ret HIVE_ERR_COLLISION 397 end 398 if eq st HIVE_CELL_BLOCKED 399 ret HIVE_ERR_BLOCKED 400 end 401 put g_cell_st sl HIVE_CELL_BLOCKED 402 put g_cell_ag sl 0 403 put g_meta 5 add get g_meta 5 1 404 put g_meta 6 add get g_meta 6 1 405 ret HIVE_OK 406 end 407 408 fn hive_unblock_cell q int r int to uint 409 if not hive_in_bounds q r 410 ret HIVE_ERR_OOB 411 end 412 sl = hive_slot q r 413 if ne get g_cell_st sl HIVE_CELL_BLOCKED 414 ret HIVE_ERR_BAD_ARG 415 end 416 put g_cell_st sl HIVE_CELL_EMPTY 417 put g_meta 5 sub get g_meta 5 1 418 put g_meta 6 add get g_meta 6 1 419 ret HIVE_OK 420 end 421 422 fn hive_place_agent aid uint q int r int role uchar cap uint to uint 423 if or eq aid 0 gt ulong aid HIVE_MAX_AGENTS 424 ret HIVE_ERR_BAD_ARG 425 end 426 if eq cap 0 427 ret HIVE_ERR_BAD_ARG 428 end 429 if not hive_in_bounds q r 430 ret HIVE_ERR_OOB 431 end 432 aidx = ulong aid 433 if ne get g_ag_act aidx 0 434 ret HIVE_ERR_DUP_AGENT 435 end 436 if ge get g_meta 2 get g_meta 1 437 ret HIVE_ERR_CAPACITY 438 end 439 sl = hive_slot q r 440 st = get g_cell_st sl 441 if eq st HIVE_CELL_BLOCKED 442 ret HIVE_ERR_BLOCKED 443 end 444 if eq st HIVE_CELL_OCC 445 ret HIVE_ERR_COLLISION 446 end 447 put g_cell_st sl HIVE_CELL_OCC 448 put g_cell_ag sl aid 449 put g_ag_act aidx 1 450 put g_ag_q aidx q 451 put g_ag_r aidx r 452 put g_ag_role aidx role 453 put g_ag_load aidx 0 454 put g_ag_cap aidx cap 455 put g_meta 2 add get g_meta 2 1 456 put g_meta 6 add get g_meta 6 1 457 ret HIVE_OK 458 end 459 460 fn hive_remove_agent aid uint to uint 461 if or eq aid 0 gt ulong aid HIVE_MAX_AGENTS 462 ret HIVE_ERR_BAD_ARG 463 end 464 aidx = ulong aid 465 if eq get g_ag_act aidx 0 466 ret HIVE_ERR_NOT_FOUND 467 end 468 if gt get g_ag_load aidx 0 469 ret HIVE_ERR_CLAIMED 470 end 471 sl = hive_slot get g_ag_q aidx get g_ag_r aidx 472 put g_cell_st sl HIVE_CELL_EMPTY 473 put g_cell_ag sl 0 474 put g_ag_act aidx 0 475 put g_meta 2 sub get g_meta 2 1 476 put g_meta 6 add get g_meta 6 1 477 ret HIVE_OK 478 end 479 480 fn hive_find_free parent_id uint dst edit HexCoord to uint 481 rqs = new int 96 0 482 rrs = new int 96 0 483 ctr = new HexCoord 0 0 484 if ne parent_id 0 485 if gt ulong parent_id HIVE_MAX_AGENTS 486 ret HIVE_ERR_NOT_FOUND 487 end 488 pidx = ulong parent_id 489 if eq get g_ag_act pidx 0 490 ret HIVE_ERR_NOT_FOUND 491 end 492 put ctr q get g_ag_q pidx 493 put ctr r get g_ag_r pidx 494 end 495 if hive_in_bounds get ctr q get ctr r 496 if eq get g_cell_st hive_slot get ctr q get ctr r HIVE_CELL_EMPTY 497 put dst q get ctr q 498 put dst r get ctr r 499 ret HIVE_OK 500 end 501 end 502 max_k = uint mul get g_meta 0 2 503 k = uint 1 504 while le k max_k bound 16 505 cnt = hive_ring ctr k rqs rrs 506 i = uint 0 507 while lt i cnt bound 96 508 cand_q = get rqs ulong i 509 cand_r = get rrs ulong i 510 if hive_in_bounds cand_q cand_r 511 if eq get g_cell_st hive_slot cand_q cand_r HIVE_CELL_EMPTY 512 put dst q cand_q 513 put dst r cand_r 514 ret HIVE_OK 515 end 516 end 517 i = add i 1 518 end 519 k = add k 1 520 end 521 ret HIVE_ERR_CAPACITY 522 end 523 524 fn hive_place_auto aid uint parent_id uint role uchar cap uint dst edit HexCoord to uint 525 rc = hive_find_free parent_id dst 526 if ne rc HIVE_OK 527 ret rc 528 end 529 ret hive_place_agent aid get dst q get dst r role cap 530 end 531 532 fn hive_compute_bounds dst edit HiveBounds to void 533 first = true 534 mn_q = int 0 535 mx_q = int 0 536 mn_r = int 0 537 mx_r = int 0 538 md = int 0 539 cnt = uint 0 540 i = ulong 1 541 while le i HIVE_MAX_AGENTS bound 128 542 if ne get g_ag_act i 0 543 aq = get g_ag_q i 544 ar = get g_ag_r i 545 if first 546 mn_q = aq 547 mx_q = aq 548 mn_r = ar 549 mx_r = ar 550 first = false 551 else 552 if lt aq mn_q 553 mn_q = aq 554 end 555 if gt aq mx_q 556 mx_q = aq 557 end 558 if lt ar mn_r 559 mn_r = ar 560 end 561 if gt ar mx_r 562 mx_r = ar 563 end 564 end 565 d = hive_hex_dist 0 0 aq ar 566 if gt d md 567 md = d 568 end 569 cnt = add cnt 1 570 end 571 i = add i 1 572 end 573 put dst min_q mn_q 574 put dst max_q mx_q 575 put dst min_r mn_r 576 put dst max_r mx_r 577 put dst cnt cnt 578 put dst max_dist md 579 end 580 581 note ---- Shortest-Hop Peer Routing Across Hex Cells (BFS) ---- 582 583 fn hive_bfs_expand cur ulong vis edit uchar 289 par edit ushort 289 que edit ushort 289 tail edit ulong 1 to void 584 cq = hive_slot_q cur 585 cr = hive_slot_r cur 586 for d ulong 6 587 nq = add cq hive_dir_dq uint d 588 nr = add cr hive_dir_dr uint d 589 if hive_in_bounds nq nr 590 nsl = hive_slot nq nr 591 if ne get g_cell_st nsl HIVE_CELL_BLOCKED 592 if eq get vis nsl 0 593 put vis nsl 1 594 put par nsl ushort cur 595 t = get tail 0 596 put que t ushort nsl 597 put tail 0 add t 1 598 end 599 end 600 end 601 end 602 end 603 604 fn hive_walk_back ends view ulong 2 par view ushort 289 rev edit ushort 289 to ulong 605 src_sl = get ends 0 606 cur = get ends 1 607 n = ulong 0 608 while true bound 289 609 put rev n ushort cur 610 n = add n 1 611 if eq cur src_sl 612 break 613 end 614 cur = ulong get par cur 615 end 616 ret n 617 end 618 619 fn hive_reconstruct_path ends view ulong 2 par view ushort 289 path_q edit int * path_r edit int * to int 620 rev = new ushort 289 0 621 n = hive_walk_back ends par rev 622 if or lt len path_q n lt len path_r n 623 ret neg int HIVE_ERR_CAPACITY 624 end 625 i = ulong 0 626 while lt i n bound 289 627 sl = ulong get rev sub sub n 1 i 628 put path_q i hive_slot_q sl 629 put path_r i hive_slot_r sl 630 i = add i 1 631 end 632 ret int sub n 1 633 end 634 635 fn hive_bfs_run ends view ulong 2 par edit ushort 289 to bool 636 vis = new uchar 289 0 637 que = new ushort 289 0 638 tail = new ulong 1 0 639 src_sl = get ends 0 640 dst_sl = get ends 1 641 put vis src_sl 1 642 put par src_sl ushort src_sl 643 put que 0 ushort src_sl 644 put tail 0 1 645 head = ulong 0 646 while lt head get tail 0 bound 289 647 cur = ulong get que head 648 head = add head 1 649 if eq cur dst_sl 650 ret true 651 end 652 hive_bfs_expand cur vis par que tail 653 end 654 ret false 655 end 656 657 fn hive_route src view HexCoord dst view HexCoord path_q edit int * path_r edit int * to int 658 par = new ushort 289 0 659 ends = new ulong 2 0 660 if not hive_in_bounds get src q get src r 661 ret neg int HIVE_ERR_OOB 662 end 663 if not hive_in_bounds get dst q get dst r 664 ret neg int HIVE_ERR_OOB 665 end 666 src_sl = hive_slot get src q get src r 667 dst_sl = hive_slot get dst q get dst r 668 if eq get g_cell_st src_sl HIVE_CELL_BLOCKED 669 ret neg int HIVE_ERR_BLOCKED 670 end 671 if eq get g_cell_st dst_sl HIVE_CELL_BLOCKED 672 ret neg int HIVE_ERR_BLOCKED 673 end 674 if eq src_sl dst_sl 675 if or lt len path_q 1 lt len path_r 1 676 ret neg int HIVE_ERR_CAPACITY 677 end 678 put path_q 0 get src q 679 put path_r 0 get src r 680 ret 0 681 end 682 put ends 0 src_sl 683 put ends 1 dst_sl 684 if not hive_bfs_run ends par 685 ret neg int HIVE_ERR_NO_ROUTE 686 end 687 ret hive_reconstruct_path ends par path_q path_r 688 end 689 690 note ---- Task Assignment, Lease Lifecycle & Capacity Bounds ---- 691 692 fn hive_task_create tid uint q int r int ring uint to uint 693 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 694 ret HIVE_ERR_BAD_ARG 695 end 696 if not hive_in_bounds q r 697 ret HIVE_ERR_OOB 698 end 699 if eq get g_cell_st hive_slot q r HIVE_CELL_BLOCKED 700 ret HIVE_ERR_BLOCKED 701 end 702 tidx = ulong tid 703 if ne get g_tk_st tidx HIVE_TASK_NONE 704 ret HIVE_ERR_COLLISION 705 end 706 if ge get g_meta 4 get g_meta 3 707 ret HIVE_ERR_CAPACITY 708 end 709 put g_tk_st tidx HIVE_TASK_OPEN 710 put g_tk_q tidx q 711 put g_tk_r tidx r 712 put g_tk_ring tidx ring 713 put g_tk_own tidx 0 714 put g_tk_exp tidx 0 715 put g_meta 4 add get g_meta 4 1 716 ret HIVE_OK 717 end 718 719 fn hive_task_claim tid uint aid uint ttl ulong to uint 720 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 721 ret HIVE_ERR_BAD_ARG 722 end 723 if or eq aid 0 gt ulong aid HIVE_MAX_AGENTS 724 ret HIVE_ERR_BAD_ARG 725 end 726 if eq ttl 0 727 ret HIVE_ERR_BAD_ARG 728 end 729 tidx = ulong tid 730 aidx = ulong aid 731 tst = get g_tk_st tidx 732 if eq tst HIVE_TASK_NONE 733 ret HIVE_ERR_NOT_FOUND 734 end 735 if eq get g_ag_act aidx 0 736 ret HIVE_ERR_NOT_FOUND 737 end 738 now = get g_clock 0 739 if eq tst HIVE_TASK_CLAIMED 740 old_own = get g_tk_own tidx 741 if gt get g_tk_exp tidx now 742 if ne old_own aid 743 ret HIVE_ERR_CLAIMED 744 end 745 put g_tk_exp tidx add now ttl 746 ret HIVE_OK 747 end 748 if ne old_own 0 749 oidx = ulong old_own 750 if gt get g_ag_load oidx 0 751 put g_ag_load oidx sub get g_ag_load oidx 1 752 end 753 end 754 else 755 if ne tst HIVE_TASK_OPEN 756 ret HIVE_ERR_CLAIMED 757 end 758 end 759 if ge get g_ag_load aidx get g_ag_cap aidx 760 ret HIVE_ERR_CAPACITY 761 end 762 put g_tk_st tidx HIVE_TASK_CLAIMED 763 put g_tk_own tidx aid 764 put g_tk_exp tidx add now ttl 765 put g_ag_load aidx add get g_ag_load aidx 1 766 ret HIVE_OK 767 end 768 769 fn hive_task_heartbeat tid uint aid uint ttl ulong to uint 770 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 771 ret HIVE_ERR_BAD_ARG 772 end 773 if eq ttl 0 774 ret HIVE_ERR_BAD_ARG 775 end 776 tidx = ulong tid 777 if ne get g_tk_st tidx HIVE_TASK_CLAIMED 778 ret HIVE_ERR_NOT_FOUND 779 end 780 if ne get g_tk_own tidx aid 781 ret HIVE_ERR_UNAUTH 782 end 783 put g_tk_exp tidx add get g_clock 0 ttl 784 ret HIVE_OK 785 end 786 787 fn hive_tick delta ulong to uint 788 now = add get g_clock 0 delta 789 put g_clock 0 now 790 reclaimed = uint 0 791 i = ulong 1 792 while le i HIVE_MAX_TASKS bound 128 793 if eq get g_tk_st i HIVE_TASK_CLAIMED 794 if le get g_tk_exp i now 795 owner = get g_tk_own i 796 if ne owner 0 797 oidx = ulong owner 798 if gt get g_ag_load oidx 0 799 put g_ag_load oidx sub get g_ag_load oidx 1 800 end 801 end 802 put g_tk_st i HIVE_TASK_OPEN 803 put g_tk_own i 0 804 put g_tk_exp i 0 805 reclaimed = add reclaimed 1 806 end 807 end 808 i = add i 1 809 end 810 ret reclaimed 811 end 812 813 fn hive_task_submit tid uint aid uint to uint 814 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 815 ret HIVE_ERR_BAD_ARG 816 end 817 tidx = ulong tid 818 if ne get g_tk_st tidx HIVE_TASK_CLAIMED 819 ret HIVE_ERR_NOT_FOUND 820 end 821 if ne get g_tk_own tidx aid 822 ret HIVE_ERR_UNAUTH 823 end 824 put g_tk_st tidx HIVE_TASK_SUBMITTED 825 ret HIVE_OK 826 end 827 828 fn hive_task_verify tid uint passed bool to uint 829 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 830 ret HIVE_ERR_BAD_ARG 831 end 832 tidx = ulong tid 833 if ne get g_tk_st tidx HIVE_TASK_SUBMITTED 834 ret HIVE_ERR_NOT_FOUND 835 end 836 owner = get g_tk_own tidx 837 if ne owner 0 838 oidx = ulong owner 839 if gt get g_ag_load oidx 0 840 put g_ag_load oidx sub get g_ag_load oidx 1 841 end 842 end 843 if passed 844 put g_tk_st tidx HIVE_TASK_VERIFIED 845 else 846 put g_tk_st tidx HIVE_TASK_FAILED 847 end 848 ret HIVE_OK 849 end 850 851 fn hive_assign_nearest tid uint ttl ulong assigned edit uint 1 to uint 852 pq = new int 289 0 853 pr = new int 289 0 854 src = new HexCoord 0 0 855 dst = new HexCoord 0 0 856 put assigned 0 0 857 if or eq tid 0 gt ulong tid HIVE_MAX_TASKS 858 ret HIVE_ERR_BAD_ARG 859 end 860 tidx = ulong tid 861 if ne get g_tk_st tidx HIVE_TASK_OPEN 862 ret HIVE_ERR_CLAIMED 863 end 864 put dst q get g_tk_q tidx 865 put dst r get g_tk_r tidx 866 best_aid = uint 0 867 best_hops = int `1000000` 868 i = ulong 1 869 while le i HIVE_MAX_AGENTS bound 128 870 if ne get g_ag_act i 0 871 if lt get g_ag_load i get g_ag_cap i 872 put src q get g_ag_q i 873 put src r get g_ag_r i 874 hops = hive_route src dst pq pr 875 if ge hops 0 876 if lt hops best_hops 877 best_hops = hops 878 best_aid = uint i 879 end 880 end 881 end 882 end 883 i = add i 1 884 end 885 if eq best_aid 0 886 ret HIVE_ERR_NO_ROUTE 887 end 888 rc = hive_task_claim tid best_aid ttl 889 if eq rc HIVE_OK 890 put assigned 0 best_aid 891 end 892 ret rc 893 end 894 895 note ---- I/O and integer formatting helpers ---- 896 897 fn hive_write_all fd int buf view uchar * to bool 898 n = len buf 899 off = ulong 0 900 while lt off n bound 65536 901 k = write fd buf off sub n off 902 if le k 0 903 ret false 904 end 905 off = add off ulong k 906 end 907 ret true 908 end 909 910 fn hive_write_u64 fd int v ulong to bool 911 buf = new uchar 24 0 912 if eq v 0 913 put buf 0 uchar 48 914 ret hive_write_all fd part buf 0 1 915 end 916 cur = v 917 pos = ulong 24 918 while gt cur 0 bound 24 919 pos = sub pos 1 920 put buf pos add uchar 48 uchar rem cur 10 921 cur = div cur 10 922 end 923 ret hive_write_all fd part buf pos sub 24 pos 924 end 925 926 fn hive_write_i32 fd int v int to bool 927 neg_ch = new uchar 1 45 928 if lt v 0 929 if not hive_write_all fd neg_ch 930 ret false 931 end 932 mag = ulong sub long 0 long v 933 ret hive_write_u64 fd mag 934 end 935 ret hive_write_u64 fd ulong v 936 end