interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / core / hive / lib.om

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