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 / aienux / crates / allen-reflex-kernel / reflex_kernel.om

1751 lines · 37439 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/allen-reflex-kernel/reflex_kernel.om: Pure-Omega port of allen-reflex-kernel.
3note Ported from aienux crates/allen-reflex-kernel/src/{lib,heap,pool,card,runner,kernel}.rs.
4note Implements bounded bump heap with mark/reset and live/peak accounting, PSV training pool
5note parser and 15-action mapper, exam & route card parser/validator with percent-decoding,
6note deterministic multi-mode runner (reflex/nearest/perceptron) with ALLEN_D and ALLEN_P
7note formatting, and kernel lifecycle entry points (heap_init/stats, card_open/kind, train,
8note decide, route, close).
9note Compose with: si/directives/om2.om aienux/crates/allen-reflex-kernel/reflex_kernel.om ...
10
11note Card and budget limits
12const ARK_FILE_MAX ulong 131072
13const ARK_CASES_MAX ulong 512
14const ARK_ID_MAX ulong 16
15const ARK_TURNS_MAX ulong 4096
16const ARK_SESSION_MAX ulong 999999999
17const ARK_LINE_CAP ulong 8192
18const ARK_ALLEN_HEAP_BYTES ulong 33554432
19const ARK_POOL_MAX ulong 512
20const ARK_POOL_ARENA_MAX ulong 65536
21const ARK_N_LABELS ulong 15
22const ARK_N_ROUTES ulong 8
23
24note Card kinds
25const ARK_KIND_NONE int -1
26const ARK_KIND_EXAM int 0
27const ARK_KIND_ROUTE int 1
28
29note Card parse error codes (1..16)
30const ARK_ERR_NONE int 0
31const ARK_ERR_FILE_SIZE int 1
32const ARK_ERR_ASCII int 2
33const ARK_ERR_NO_LF int 3
34const ARK_ERR_BAD_HEADER int 4
35const ARK_ERR_EMPTY_LINE int 5
36const ARK_ERR_TOO_MANY_CASES int 6
37const ARK_ERR_NO_BAR int 7
38const ARK_ERR_ID_LEN int 8
39const ARK_ERR_ID_CHARS int 9
40const ARK_ERR_TURNS_LEN int 10
41const ARK_ERR_DUP_ID int 11
42const ARK_ERR_NO_CASE int 12
43const ARK_ERR_ROUTE_FIELDS int 13
44const ARK_ERR_SPEAKER int 14
45const ARK_ERR_SESSION int 15
46const ARK_ERR_FLAGS int 16
47
48note Pool parse error codes
49const ARK_POOL_ERR_FIELDS int -1
50const ARK_POOL_ERR_ACTION int -2
51const ARK_POOL_ERR_CAP int -3
52
53note Scorer modes
54const ARK_MODE_REFLEX uint 0
55const ARK_MODE_NEAREST uint 1
56const ARK_MODE_PERCEPTRON uint 2
57
58note Action indices (0..14)
59const ARK_ACT_STATUS uint 0
60const ARK_ACT_HELP uint 1
61const ARK_ACT_EVIDENCE uint 2
62const ARK_ACT_RECALL uint 3
63const ARK_ACT_FACTS uint 4
64const ARK_ACT_REMEMBER uint 5
65const ARK_ACT_PREFER uint 6
66const ARK_ACT_TASK uint 7
67const ARK_ACT_PROMPT uint 8
68const ARK_ACT_APPROVE uint 9
69const ARK_ACT_DENY uint 10
70const ARK_ACT_CANCEL uint 11
71const ARK_ACT_NONE uint 12
72const ARK_ACT_CLARIFY uint 13
73const ARK_ACT_REFUSE uint 14
74
75note Route indices (0..7)
76const ARK_ROUTE_LOCAL_DET uint 0
77const ARK_ROUTE_MEMORY uint 1
78const ARK_ROUTE_CLARIFY uint 2
79const ARK_ROUTE_REFUSE uint 3
80const ARK_ROUTE_LOCAL_MODEL uint 4
81const ARK_ROUTE_SPARK_INFER uint 5
82const ARK_ROUTE_SPARK_OMEGESE uint 6
83const ARK_ROUTE_APPROVAL uint 7
84
85note Image capability bits (card cannot claim any)
86const ARK_CAP_STORE uint 1
87const ARK_CAP_DESK uint 2
88const ARK_CAP_LOCAL_MODEL uint 4
89const ARK_CAP_SPARK uint 8
90
91note Header, speaker, mode, action, route and JSON text constants
92text ark_exam_hdr "AIENALX1 QUALIF-ALLEN-EXAM"
93text ark_route_hdr "AIENALR1 QUALIF-ALLEN-ROUTE"
94text ark_owner_pfx "owner:"
95text ark_guest_pfx "guest:"
96text ark_tag_decide "ALLEN_D"
97text ark_tag_route "ALLEN_P"
98
99text ark_m_reflex "reflex"
100text ark_m_nearest "nearest"
101text ark_m_perceptron "perceptron"
102
103text ark_a_status "status"
104text ark_a_help "help"
105text ark_a_evidence "evidence"
106text ark_a_recall "recall"
107text ark_a_facts "facts"
108text ark_a_remember "remember"
109text ark_a_prefer "prefer"
110text ark_a_task "task"
111text ark_a_prompt "prompt"
112text ark_a_approve "approve"
113text ark_a_deny "deny"
114text ark_a_cancel "cancel"
115text ark_a_none "none"
116text ark_a_clarify "clarify"
117text ark_a_refuse "refuse"
118
119text ark_r_local_det "local_deterministic"
120text ark_r_memory "memory"
121text ark_r_clarify "clarify"
122text ark_r_refuse "refuse"
123text ark_r_local_model "local_model"
124text ark_r_spark_infer "spark_inference"
125text ark_r_spark_om "spark_omegese"
126text ark_r_approval "approval_needed"
127
128text ark_j_act_pfx "{\"action\":\""
129text ark_j_act_sfx "\",\"args\":{}}"
130text ark_j_p_req "{\"request_id\":"
131text ark_j_p_sess ",\"session_id\":"
132text ark_j_p_route ",\"route\":\""
133text ark_j_p_lbl "\",\"label\":\""
134text ark_j_p_grants "\",\"grants\":\"none\",\"ready\":"
135text ark_j_true "true}"
136text ark_j_false "false}"
137
138note Data structures (declared before first fn)
139struct ArkBump base ulong limit ulong cur ulong last ulong peak ulong live ulong live_peak ulong allocs ulong fails ulong end
140struct ArkMark cur ulong live ulong end
141struct ArkStats cap ulong used ulong peak ulong live ulong live_peak ulong allocs ulong fails ulong end
142struct ArkTrainInfo ticks ulong examples ulong exemplars ulong memory_bytes ulong end
143struct ArkDecideInfo nbytes ulong json_off ulong ticks ulong heap_reset ulong end
144struct ArkRouteInfo nbytes ulong json_off ulong ticks ulong heap_reset ulong route uint label uint owner uint ready uint end
145struct ArkProposalFacts route uint label uint owner uint ready uint end
146
147note Global parsed exam card state (up to 512 cases)
148global ark_case_id_off ulong 512
149global ark_case_id_end ulong 512
150global ark_case_turns_off ulong 512
151global ark_case_turns_end ulong 512
152global ark_case_count ulong 1
153
154note Global parsed route card state (up to 512 requests)
155global ark_route_id_off ulong 512
156global ark_route_id_end ulong 512
157global ark_route_owner uchar 512
158global ark_route_session ulong 512
159global ark_route_consent uchar 512
160global ark_route_pending uchar 512
161global ark_route_turns_off ulong 512
162global ark_route_turns_end ulong 512
163global ark_route_count ulong 1
164global ark_last_err int 1
165
166note Global training pool state (PSV examples stored in arena)
167global ark_pool_act uchar 512
168global ark_pool_tok_hash ulong 512
169global ark_pool_turns_len ulong 512
170global ark_pool_count ulong 1
171global ark_pool_arena uchar 65536
172global ark_pool_arena_len ulong 1
173
174note Global kernel heap state: 0=base 1=limit 2=cur 3=last 4=peak 5=live 6=live_peak 7=allocs 8=fails
175global ark_kheap ulong 9
176
177note Global kernel session and card buffers
178note ark_kstate: 0=card_kind(-1/0/1 stored as ulong+1: 0=none,1=exam,2=route), 1=card_len,
179note 2=has_after_cases, 3=after_cur, 4=after_live, 5=has_session, 6=sess_mode,
180note 7=sess_examples, 8=sess_exemplars, 9=sess_mem_bytes, 10=sess_heap_ptr, 11=line_heap_ptr
181global ark_kstate ulong 12
182global ark_kcard_buf uchar 131072
183global ark_kline_buf uchar 8192
184global ark_kjson_buf uchar 2048
185global ark_kdec_buf uchar 4096
186global ark_kact_counts ulong 15
187global ark_kmode_weights ulong 15
188
189note ============================================================
190note Section 1: Bounded Bump Heap (heap.rs)
191note ============================================================
192
193fn ark_is_pow2 a ulong to bool
194if eq a 0
195ret false
196end
197ret eq and a sub a 1 0
198end
199
200fn ark_bump_init h edit ArkBump base ulong nbytes ulong to void
201ok = false
202if ne base 0
203if ne nbytes 0
204if le nbytes sub 0xffffffffffffffff base
205ok = true
206end
207end
208end
209if ok
210put h base base
211put h limit add base nbytes
212put h cur base
213else
214put h base ulong 0
215put h limit ulong 0
216put h cur ulong 0
217end
218put h last ulong 0
219put h peak ulong 0
220put h live ulong 0
221put h live_peak ulong 0
222put h allocs ulong 0
223put h fails ulong 0
224end
225
226fn ark_bump_fail h edit ArkBump to ulong
227put h fails add get h fails 1
228ret ulong 0
229end
230
231fn ark_bump_grew h edit ArkBump e ulong add_live ulong to void
232used = sub e get h base
233if gt used get h peak
234put h peak used
235end
236nlive = add get h live add_live
237put h live nlive
238if gt nlive get h live_peak
239put h live_peak nlive
240end
241end
242
243fn ark_bump_alloc h edit ArkBump size ulong align ulong to ulong
244if eq get h base 0
245ret ark_bump_fail h
246end
247if not ark_is_pow2 align
248ret ark_bump_fail h
249end
250mask = sub align 1
251c = get h cur
252if gt mask sub 0xffffffffffffffff c
253ret ark_bump_fail h
254end
255start = and add c mask xor mask 0xffffffffffffffff
256if gt size sub 0xffffffffffffffff start
257ret ark_bump_fail h
258end
259e = add start size
260if gt e get h limit
261ret ark_bump_fail h
262end
263put h cur e
264put h last start
265put h allocs add get h allocs 1
266ark_bump_grew h e size
267ret start
268end
269
270fn ark_bump_dealloc h edit ArkBump p ulong size ulong to void
271lv = get h live
272if ge lv size
273put h live sub lv size
274else
275put h live ulong 0
276end
277if eq p get h last
278if le size sub 0xffffffffffffffff p
279if eq add p size get h cur
280put h cur p
281put h last ulong 0
282end
283end
284end
285end
286
287fn ark_bump_resize_in_place h edit ArkBump p ulong size ulong new_size ulong to ulong
288if eq p 0
289ret ulong 0
290end
291if ne p get h last
292ret ulong 0
293end
294if gt size sub 0xffffffffffffffff p
295ret ulong 0
296end
297if ne add p size get h cur
298ret ulong 0
299end
300if gt new_size sub 0xffffffffffffffff p
301ret ulong 0
302end
303e = add p new_size
304if gt e get h limit
305ret ulong 0
306end
307put h cur e
308lv = get h live
309if ge lv size
310put h live sub lv size
311else
312put h live ulong 0
313end
314ark_bump_grew h e new_size
315ret p
316end
317
318fn ark_bump_mark h view ArkBump m edit ArkMark to void
319put m cur get h cur
320put m live get h live
321end
322
323fn ark_bump_reset_to h edit ArkBump m view ArkMark to bool
324if eq get h base 0
325ret false
326end
327if ne get h live get m live
328ret false
329end
330if gt get m cur get h cur
331ret false
332end
333put h cur get m cur
334put h last ulong 0
335ret true
336end
337
338fn ark_bump_reset_peaks h edit ArkBump to void
339b = get h base
340if ne b 0
341put h peak sub get h cur b
342end
343put h live_peak get h live
344end
345
346fn ark_bump_stats h view ArkBump st edit ArkStats to void
347b = get h base
348put st cap sub get h limit b
349put st used sub get h cur b
350put st peak get h peak
351put st live get h live
352put st live_peak get h live_peak
353put st allocs get h allocs
354put st fails get h fails
355end
356
357note ============================================================
358note Section 2: Byte and String Helpers
359note ============================================================
360
361fn ark_is_id_byte b uchar to bool
362if ge b 65
363if le b 90
364ret true
365end
366end
367if ge b 97
368if le b 122
369ret true
370end
371end
372if ge b 48
373if le b 57
374ret true
375end
376end
377if eq b 95
378ret true
379end
380if eq b 45
381ret true
382end
383ret false
384end
385
386fn ark_is_ascii_printable b uchar to bool
387if ge b 0x20
388if le b 0x7e
389ret true
390end
391end
392ret false
393end
394
395fn ark_hex_val b uchar to int
396if ge b 48
397if le b 57
398ret int sub b 48
399end
400end
401if ge b 97
402if le b 102
403ret add 10 int sub b 97
404end
405end
406if ge b 65
407if le b 70
408ret add 10 int sub b 65
409end
410end
411ret -1
412end
413
414fn ark_slice_eq_text buf view uchar * off ulong nbytes ulong lit view uchar * to bool
415if ne nbytes len lit
416ret false
417end
418i = ulong 0
419while lt i nbytes bound 64
420if ne get buf add off i get lit i
421ret false
422end
423i = add i 1
424end
425ret true
426end
427
428fn ark_slice_eq_slice buf view uchar * a_off ulong a_len ulong b_off ulong b_len ulong to bool
429if ne a_len b_len
430ret false
431end
432i = ulong 0
433while lt i a_len bound 4096
434if ne get buf add a_off i get buf add b_off i
435ret false
436end
437i = add i 1
438end
439ret true
440end
441
442fn ark_copy_text dst edit uchar * off ulong src view uchar * to ulong
443n = len src
444i = ulong 0
445while lt i n bound 256
446put dst add off i get src i
447i = add i 1
448end
449ret add off n
450end
451
452fn ark_write_u64_dec dst edit uchar * off ulong val ulong to ulong
453if eq val 0
454put dst off uchar 48
455ret add off 1
456end
457tmp = new uchar 24 0
458v = val
459d = ulong 0
460while gt v 0 bound 24
461q = div v 10
462r = rem v 10
463put tmp d uchar add 48 r
464v = q
465d = add d 1
466end
467i = ulong 0
468while lt i d bound 24
469put dst add off i get tmp sub sub d 1 i
470i = add i 1
471end
472ret add off d
473end
474
475note ============================================================
476note Section 3: Exam and Route Card Parser (card.rs)
477note ============================================================
478
479fn ark_card_kind buf view uchar * to int
480n = len buf
481el = len ark_exam_hdr
482if gt n el
483if ark_slice_eq_text buf 0 el ark_exam_hdr
484if eq get buf el 10
485ret ARK_KIND_EXAM
486end
487end
488end
489rl = len ark_route_hdr
490if gt n rl
491if ark_slice_eq_text buf 0 rl ark_route_hdr
492if eq get buf rl 10
493ret ARK_KIND_ROUTE
494end
495end
496end
497ret ARK_KIND_NONE
498end
499
500fn ark_card_check_common buf view uchar * to int
501n = len buf
502if eq n 0
503ret ARK_ERR_FILE_SIZE
504end
505if gt n ARK_FILE_MAX
506ret ARK_ERR_FILE_SIZE
507end
508i = ulong 0
509while lt i n bound 131072
510b = get buf i
511if ne b 10
512if not ark_is_ascii_printable b
513ret ARK_ERR_ASCII
514end
515end
516i = add i 1
517end
518if ne get buf sub n 1 10
519ret ARK_ERR_NO_LF
520end
521ret ARK_ERR_NONE
522end
523
524fn ark_check_id buf view uchar * s ulong e ulong to int
525if le e s
526ret ARK_ERR_ID_LEN
527end
528idlen = sub e s
529if gt idlen ARK_ID_MAX
530ret ARK_ERR_ID_LEN
531end
532i = s
533while lt i e bound 32
534if not ark_is_id_byte get buf i
535ret ARK_ERR_ID_CHARS
536end
537i = add i 1
538end
539ret ARK_ERR_NONE
540end
541
542fn ark_exam_dup_id buf view uchar * s ulong e ulong cnt ulong to bool
543idlen = sub e s
544j = ulong 0
545while lt j cnt bound 512
546co = get ark_case_id_off j
547ce = get ark_case_id_end j
548if ark_slice_eq_slice buf s idlen co sub ce co
549ret true
550end
551j = add j 1
552end
553ret false
554end
555
556fn ark_route_dup_id buf view uchar * s ulong e ulong cnt ulong to bool
557idlen = sub e s
558j = ulong 0
559while lt j cnt bound 512
560co = get ark_route_id_off j
561ce = get ark_route_id_end j
562if ark_slice_eq_slice buf s idlen co sub ce co
563ret true
564end
565j = add j 1
566end
567ret false
568end
569
570fn ark_find_lf buf view uchar * pos ulong n ulong to ulong
571e = pos
572while lt e n bound 131072
573if eq get buf e 10
574ret e
575end
576e = add e 1
577end
578ret n
579end
580
581fn ark_find_bar buf view uchar * pos ulong e ulong to ulong
582p = pos
583while lt p e bound 8192
584if eq get buf p 124
585ret p
586end
587p = add p 1
588end
589ret e
590end
591
592fn ark_card_parse_exam buf view uchar * to int
593put ark_case_count 0 ulong 0
594put ark_last_err 0 ARK_ERR_NONE
595cerr = ark_card_check_common buf
596if ne cerr ARK_ERR_NONE
597put ark_last_err 0 cerr
598ret neg cerr
599end
600if ne ark_card_kind buf ARK_KIND_EXAM
601put ark_last_err 0 ARK_ERR_BAD_HEADER
602ret neg ARK_ERR_BAD_HEADER
603end
604n = len buf
605pos = add len ark_exam_hdr 1
606cnt = ulong 0
607while lt pos n bound 1024
608e = ark_find_lf buf pos n
609if eq e pos
610put ark_last_err 0 ARK_ERR_EMPTY_LINE
611ret neg ARK_ERR_EMPTY_LINE
612end
613if ge cnt ARK_CASES_MAX
614put ark_last_err 0 ARK_ERR_TOO_MANY_CASES
615ret neg ARK_ERR_TOO_MANY_CASES
616end
617bar = ark_find_bar buf pos e
618if eq bar e
619put ark_last_err 0 ARK_ERR_NO_BAR
620ret neg ARK_ERR_NO_BAR
621end
622iderr = ark_check_id buf pos bar
623if ne iderr ARK_ERR_NONE
624put ark_last_err 0 iderr
625ret neg iderr
626end
627tstart = add bar 1
628if le e tstart
629put ark_last_err 0 ARK_ERR_TURNS_LEN
630ret neg ARK_ERR_TURNS_LEN
631end
632tl = sub e tstart
633if gt tl ARK_TURNS_MAX
634put ark_last_err 0 ARK_ERR_TURNS_LEN
635ret neg ARK_ERR_TURNS_LEN
636end
637if ark_exam_dup_id buf pos bar cnt
638put ark_last_err 0 ARK_ERR_DUP_ID
639ret neg ARK_ERR_DUP_ID
640end
641put ark_case_id_off cnt pos
642put ark_case_id_end cnt bar
643put ark_case_turns_off cnt tstart
644put ark_case_turns_end cnt e
645cnt = add cnt 1
646pos = add e 1
647end
648if eq cnt 0
649put ark_last_err 0 ARK_ERR_NO_CASE
650ret neg ARK_ERR_NO_CASE
651end
652put ark_case_count 0 cnt
653ret int cnt
654end
655
656fn ark_parse_session buf view uchar * s ulong e ulong to long
657if le e s
658ret -1
659end
660n = sub e s
661if gt n 9
662ret -1
663end
664acc = ulong 0
665i = s
666while lt i e bound 16
667b = get buf i
668if lt b 48
669ret -1
670end
671if gt b 57
672ret -1
673end
674acc = add mul acc 10 ulong sub b 48
675i = add i 1
676end
677if eq acc 0
678ret -1
679end
680if gt acc ARK_SESSION_MAX
681ret -1
682end
683ret long acc
684end
685
686fn ark_parse_flags buf view uchar * s ulong e ulong to int
687note returns bitmask: bit0=consent(C), bit1=pending(P), or -1 on error
688if le e s
689ret -1
690end
691n = sub e s
692if eq n 1
693b0 = get buf s
694if eq b0 45
695ret 0
696end
697if eq b0 67
698ret 1
699end
700if eq b0 80
701ret 2
702end
703ret -1
704end
705if eq n 2
706b0 = get buf s
707b1 = get buf add s 1
708if eq b0 b1
709ret -1
710end
711mask = int 0
712if eq b0 67
713mask = or mask 1
714else
715if eq b0 80
716mask = or mask 2
717else
718ret -1
719end
720end
721if eq b1 67
722mask = or mask 1
723else
724if eq b1 80
725mask = or mask 2
726else
727ret -1
728end
729end
730ret mask
731end
732ret -1
733end
734
735fn ark_parse_speaker_line buf view uchar * s ulong e ulong to long
736note returns (session << 1) | owner_bit, or -1 on error (speaker), -2 on error (session)
737if le e s
738ret -1
739end
740n = sub e s
741if lt n 7
742ret -1
743end
744is_owner = ulong 0
745if ark_slice_eq_text buf s 6 ark_owner_pfx
746is_owner = ulong 1
747else
748if ark_slice_eq_text buf s 6 ark_guest_pfx
749is_owner = ulong 0
750else
751ret -1
752end
753end
754sess = ark_parse_session buf add s 6 e
755if lt sess 0
756ret -2
757end
758ret long or shl ulong sess 1 is_owner
759end
760
761fn ark_parse_route_line buf view uchar * pos ulong e ulong cnt ulong to int
762b0 = ark_find_bar buf pos e
763if eq b0 e
764ret ARK_ERR_ROUTE_FIELDS
765end
766b1 = ark_find_bar buf add b0 1 e
767if eq b1 e
768ret ARK_ERR_ROUTE_FIELDS
769end
770b2 = ark_find_bar buf add b1 1 e
771if eq b2 e
772ret ARK_ERR_ROUTE_FIELDS
773end
774iderr = ark_check_id buf pos b0
775if ne iderr ARK_ERR_NONE
776ret iderr
777end
778sp = ark_parse_speaker_line buf add b0 1 b1
779if eq sp -1
780ret ARK_ERR_SPEAKER
781end
782if eq sp -2
783ret ARK_ERR_SESSION
784end
785fl = ark_parse_flags buf add b1 1 b2
786if lt fl 0
787ret ARK_ERR_FLAGS
788end
789if le e add b2 1
790ret ARK_ERR_TURNS_LEN
791end
792if gt sub e add b2 1 ARK_TURNS_MAX
793ret ARK_ERR_TURNS_LEN
794end
795if ark_route_dup_id buf pos b0 cnt
796ret ARK_ERR_DUP_ID
797end
798put ark_route_id_off cnt pos
799put ark_route_id_end cnt b0
800put ark_route_owner cnt uchar and ulong sp 1
801put ark_route_session cnt shr ulong sp 1
802put ark_route_consent cnt uchar and fl 1
803put ark_route_pending cnt uchar and shr fl 1 1
804put ark_route_turns_off cnt add b2 1
805put ark_route_turns_end cnt e
806ret ARK_ERR_NONE
807end
808
809fn ark_card_parse_route buf view uchar * to int
810put ark_route_count 0 ulong 0
811put ark_last_err 0 ARK_ERR_NONE
812cerr = ark_card_check_common buf
813if ne cerr ARK_ERR_NONE
814put ark_last_err 0 cerr
815ret neg cerr
816end
817if ne ark_card_kind buf ARK_KIND_ROUTE
818put ark_last_err 0 ARK_ERR_BAD_HEADER
819ret neg ARK_ERR_BAD_HEADER
820end
821n = len buf
822pos = add len ark_route_hdr 1
823cnt = ulong 0
824while lt pos n bound 1024
825e = ark_find_lf buf pos n
826if eq e pos
827put ark_last_err 0 ARK_ERR_EMPTY_LINE
828ret neg ARK_ERR_EMPTY_LINE
829end
830if ge cnt ARK_CASES_MAX
831put ark_last_err 0 ARK_ERR_TOO_MANY_CASES
832ret neg ARK_ERR_TOO_MANY_CASES
833end
834lerr = ark_parse_route_line buf pos e cnt
835if ne lerr ARK_ERR_NONE
836put ark_last_err 0 lerr
837ret neg lerr
838end
839cnt = add cnt 1
840pos = add e 1
841end
842if eq cnt 0
843put ark_last_err 0 ARK_ERR_NO_CASE
844ret neg ARK_ERR_NO_CASE
845end
846put ark_route_count 0 cnt
847ret int cnt
848end
849
850fn ark_hex_pair b1 uchar b2 uchar to int
851h1 = ark_hex_val b1
852if lt h1 0
853ret -1
854end
855h2 = ark_hex_val b2
856if lt h2 0
857ret -1
858end
859ret or shl h1 4 h2
860end
861
862fn ark_pct_decode src view uchar * dst edit uchar * to long
863note Decodes %HH escapes from src into dst. Returns decoded len or -1 on invalid escape/overflow.
864nbytes = len src
865dcap = len dst
866i = ulong 0
867w = ulong 0
868while lt i nbytes bound 4096
869b = get src i
870if ge w dcap
871ret -1
872end
873if eq b 37
874if gt add i 3 nbytes
875ret -1
876end
877v = ark_hex_pair get src add i 1 get src add i 2
878if lt v 0
879ret -1
880end
881put dst w uchar v
882w = add w 1
883i = add i 3
884else
885put dst w b
886w = add w 1
887i = add i 1
888end
889end
890ret long w
891end
892
893note ============================================================
894note Section 4: Actions, Modes, Routes and Training Pool (pool.rs)
895note ============================================================
896
897fn ark_mode_of n uint to int
898if le n 2
899ret int n
900end
901ret -1
902end
903
904fn ark_action_from_name buf view uchar * off ulong nbytes ulong to int
905if ark_slice_eq_text buf off nbytes ark_a_status
906ret int ARK_ACT_STATUS
907end
908if ark_slice_eq_text buf off nbytes ark_a_help
909ret int ARK_ACT_HELP
910end
911if ark_slice_eq_text buf off nbytes ark_a_evidence
912ret int ARK_ACT_EVIDENCE
913end
914if ark_slice_eq_text buf off nbytes ark_a_recall
915ret int ARK_ACT_RECALL
916end
917if ark_slice_eq_text buf off nbytes ark_a_facts
918ret int ARK_ACT_FACTS
919end
920if ark_slice_eq_text buf off nbytes ark_a_remember
921ret int ARK_ACT_REMEMBER
922end
923if ark_slice_eq_text buf off nbytes ark_a_prefer
924ret int ARK_ACT_PREFER
925end
926if ark_slice_eq_text buf off nbytes ark_a_task
927ret int ARK_ACT_TASK
928end
929if ark_slice_eq_text buf off nbytes ark_a_prompt
930ret int ARK_ACT_PROMPT
931end
932if ark_slice_eq_text buf off nbytes ark_a_approve
933ret int ARK_ACT_APPROVE
934end
935if ark_slice_eq_text buf off nbytes ark_a_deny
936ret int ARK_ACT_DENY
937end
938if ark_slice_eq_text buf off nbytes ark_a_cancel
939ret int ARK_ACT_CANCEL
940end
941if ark_slice_eq_text buf off nbytes ark_a_none
942ret int ARK_ACT_NONE
943end
944if ark_slice_eq_text buf off nbytes ark_a_clarify
945ret int ARK_ACT_CLARIFY
946end
947if ark_slice_eq_text buf off nbytes ark_a_refuse
948ret int ARK_ACT_REFUSE
949end
950ret -1
951end
952
953fn ark_write_mode_name mode uint dst edit uchar * off ulong to ulong
954if eq mode ARK_MODE_REFLEX
955ret ark_copy_text dst off ark_m_reflex
956end
957if eq mode ARK_MODE_NEAREST
958ret ark_copy_text dst off ark_m_nearest
959end
960ret ark_copy_text dst off ark_m_perceptron
961end
962
963fn ark_write_action_name act uint dst edit uchar * off ulong to ulong
964if eq act ARK_ACT_STATUS
965ret ark_copy_text dst off ark_a_status
966end
967if eq act ARK_ACT_HELP
968ret ark_copy_text dst off ark_a_help
969end
970if eq act ARK_ACT_EVIDENCE
971ret ark_copy_text dst off ark_a_evidence
972end
973if eq act ARK_ACT_RECALL
974ret ark_copy_text dst off ark_a_recall
975end
976if eq act ARK_ACT_FACTS
977ret ark_copy_text dst off ark_a_facts
978end
979if eq act ARK_ACT_REMEMBER
980ret ark_copy_text dst off ark_a_remember
981end
982if eq act ARK_ACT_PREFER
983ret ark_copy_text dst off ark_a_prefer
984end
985if eq act ARK_ACT_TASK
986ret ark_copy_text dst off ark_a_task
987end
988if eq act ARK_ACT_PROMPT
989ret ark_copy_text dst off ark_a_prompt
990end
991if eq act ARK_ACT_APPROVE
992ret ark_copy_text dst off ark_a_approve
993end
994if eq act ARK_ACT_DENY
995ret ark_copy_text dst off ark_a_deny
996end
997if eq act ARK_ACT_CANCEL
998ret ark_copy_text dst off ark_a_cancel
999end
1000if eq act ARK_ACT_NONE
1001ret ark_copy_text dst off ark_a_none
1002end
1003if eq act ARK_ACT_CLARIFY
1004ret ark_copy_text dst off ark_a_clarify
1005end
1006ret ark_copy_text dst off ark_a_refuse
1007end
1008
1009fn ark_write_route_name route uint dst edit uchar * off ulong to ulong
1010if eq route ARK_ROUTE_LOCAL_DET
1011ret ark_copy_text dst off ark_r_local_det
1012end
1013if eq route ARK_ROUTE_MEMORY
1014ret ark_copy_text dst off ark_r_memory
1015end
1016if eq route ARK_ROUTE_CLARIFY
1017ret ark_copy_text dst off ark_r_clarify
1018end
1019if eq route ARK_ROUTE_REFUSE
1020ret ark_copy_text dst off ark_r_refuse
1021end
1022if eq route ARK_ROUTE_LOCAL_MODEL
1023ret ark_copy_text dst off ark_r_local_model
1024end
1025if eq route ARK_ROUTE_SPARK_INFER
1026ret ark_copy_text dst off ark_r_spark_infer
1027end
1028if eq route ARK_ROUTE_SPARK_OMEGESE
1029ret ark_copy_text dst off ark_r_spark_om
1030end
1031ret ark_copy_text dst off ark_r_approval
1032end
1033
1034fn ark_hash_turns buf view uchar * off ulong nbytes ulong to ulong
1035note FNV-1a 64-bit hash over turns bytes for deterministic exemplar matching
1036h = ulong 14695981039346656037
1037i = ulong 0
1038while lt i nbytes bound 4096
1039b = ulong get buf add off i
1040h = mul xor h b ulong 1099511628211
1041i = add i 1
1042end
1043ret h
1044end
1045
1046fn ark_pool_reset to void
1047put ark_pool_count 0 ulong 0
1048put ark_pool_arena_len 0 ulong 0
1049end
1050
1051fn ark_is_blank_line buf view uchar * s ulong e ulong to bool
1052i = s
1053while lt i e bound 8192
1054b = get buf i
1055if ne b 32
1056if ne b 9
1057if ne b 13
1058ret false
1059end
1060end
1061end
1062i = add i 1
1063end
1064ret true
1065end
1066
1067fn ark_pool_find_bars buf view uchar * pos ulong line_end ulong need ulong dst edit ulong 8 to bool
1068nb = ulong 0
1069p = pos
1070while lt p line_end bound 8192
1071if eq get buf p 124
1072if lt nb need
1073put dst nb p
1074nb = add nb 1
1075end
1076end
1077p = add p 1
1078end
1079ret eq nb need
1080end
1081
1082fn ark_pool_parse_line buf view uchar * pos ulong line_end ulong fields ulong to int
1083bars = new ulong 8 0
1084if not ark_pool_find_bars buf pos line_end sub fields 1 bars
1085ret ARK_POOL_ERR_FIELDS
1086end
1087act = int -1
1088trn_s = ulong 0
1089trn_e = ulong 0
1090if eq fields 5
1091act = ark_action_from_name buf add get bars 1 1 sub get bars 2 add get bars 1 1
1092trn_s = add get bars 3 1
1093trn_e = line_end
1094else
1095act = ark_action_from_name buf add get bars 2 1 sub get bars 3 add get bars 2 1
1096trn_s = add get bars 4 1
1097trn_e = get bars 5
1098end
1099if lt act 0
1100ret ARK_POOL_ERR_ACTION
1101end
1102idx = get ark_pool_count 0
1103if ge idx ARK_POOL_MAX
1104ret ARK_POOL_ERR_CAP
1105end
1106put ark_pool_act idx uchar act
1107put ark_pool_tok_hash idx ark_hash_turns buf trn_s sub trn_e trn_s
1108put ark_pool_turns_len idx sub trn_e trn_s
1109put ark_pool_count 0 add idx 1
1110ret 0
1111end
1112
1113fn ark_pool_parse_psv buf view uchar * fields ulong to int
1114note Parses PSV lines (fields=5 for train, fields=7 for extra) into the global pool.
1115note Returns number of examples added, or negative ARK_POOL_ERR_*.
1116if ne fields 5
1117if ne fields 7
1118ret ARK_POOL_ERR_FIELDS
1119end
1120end
1121n = len buf
1122pos = ulong 0
1123added = int 0
1124while lt pos n bound 2048
1125e = ark_find_lf buf pos n
1126line_end = e
1127if gt line_end pos
1128if eq get buf sub line_end 1 13
1129line_end = sub line_end 1
1130end
1131end
1132skip = false
1133if eq line_end pos
1134skip = true
1135else
1136if eq get buf pos 35
1137skip = true
1138else
1139if ark_is_blank_line buf pos line_end
1140skip = true
1141end
1142end
1143end
1144if not skip
1145lrc = ark_pool_parse_line buf pos line_end fields
1146if lt lrc 0
1147ret lrc
1148end
1149added = add added 1
1150end
1151if lt e n
1152pos = add e 1
1153else
1154pos = n
1155end
1156end
1157ret added
1158end
1159
1160note ============================================================
1161note Section 5: Runner Formatting & Routing Policy (runner.rs)
1162note ============================================================
1163
1164fn ark_runner_line_prefix tag_kind uint mode uint id view uchar * dst edit uchar * to long
1165note Writes "ALLEN_D <mode> <id> " (tag_kind=0) or "ALLEN_P <mode> <id> " (tag_kind=1) into dst.
1166note Returns offset where JSON begins, or -1 if dst is too small.
1167id_n = len id
1168need = add 22 id_n
1169if lt len dst need
1170ret -1
1171end
1172pos = ulong 0
1173if eq tag_kind 0
1174pos = ark_copy_text dst pos ark_tag_decide
1175else
1176pos = ark_copy_text dst pos ark_tag_route
1177end
1178put dst pos uchar 32
1179pos = add pos 1
1180pos = ark_write_mode_name mode dst pos
1181put dst pos uchar 32
1182pos = add pos 1
1183i = ulong 0
1184while lt i id_n bound 32
1185put dst add pos i get id i
1186i = add i 1
1187end
1188pos = add pos id_n
1189put dst pos uchar 32
1190pos = add pos 1
1191ret long pos
1192end
1193
1194fn ark_runner_line_append dst edit uchar * off ulong json view uchar * to long
1195jn = len json
1196if gt add off jn len dst
1197ret -1
1198end
1199i = ulong 0
1200while lt i jn bound 4096
1201put dst add off i get json i
1202i = add i 1
1203end
1204ret long add off jn
1205end
1206
1207fn ark_score_fallback mode uint h ulong to uint
1208best_act = ARK_ACT_CLARIFY
1209best_score = ulong 0
1210a = ulong 0
1211while lt a ARK_N_LABELS bound 15
1212c = get ark_kact_counts a
1213if gt c 0
1214mix = and xor h add a ulong mode 15
1215sc = add add mul c 16 mix get ark_kmode_weights a
1216if gt sc best_score
1217best_score = sc
1218best_act = uint a
1219end
1220end
1221a = add a 1
1222end
1223ret best_act
1224end
1225
1226fn ark_score_turns mode uint turns view uchar * to uint
1227note Deterministic multi-mode classifier over the trained pool.
1228note Learning is off; purely reads ark_pool_* and ark_kact_counts.
1229cnt = get ark_pool_count 0
1230h = ark_hash_turns turns 0 len turns
1231i = ulong 0
1232while lt i cnt bound 512
1233if eq get ark_pool_tok_hash i h
1234ret uint get ark_pool_act i
1235end
1236i = add i 1
1237end
1238if eq cnt 0
1239ret ARK_ACT_CLARIFY
1240end
1241ret ark_score_fallback mode h
1242end
1243
1244fn ark_decide_json_buf mode uint turns view uchar * dst edit uchar * to ulong
1245act = ark_score_turns mode turns
1246pos = ark_copy_text dst 0 ark_j_act_pfx
1247pos = ark_write_action_name act dst pos
1248pos = ark_copy_text dst pos ark_j_act_sfx
1249ret pos
1250end
1251
1252fn ark_route_for_action act uint owner uint consent uint pending uint to uint
1253note Maps (action, speaker, consent, pending) to Route index per allen_reflex::route.
1254if eq act ARK_ACT_REFUSE
1255ret ARK_ROUTE_REFUSE
1256end
1257if eq act ARK_ACT_CLARIFY
1258ret ARK_ROUTE_CLARIFY
1259end
1260if eq act ARK_ACT_RECALL
1261if eq owner 1
1262if eq consent 1
1263ret ARK_ROUTE_MEMORY
1264end
1265end
1266ret ARK_ROUTE_REFUSE
1267end
1268if eq act ARK_ACT_FACTS
1269if eq owner 1
1270if eq consent 1
1271ret ARK_ROUTE_MEMORY
1272end
1273end
1274ret ARK_ROUTE_REFUSE
1275end
1276if eq act ARK_ACT_APPROVE
1277if eq owner 1
1278if eq pending 1
1279ret ARK_ROUTE_APPROVAL
1280end
1281end
1282ret ARK_ROUTE_CLARIFY
1283end
1284if eq act ARK_ACT_DENY
1285if eq owner 1
1286if eq pending 1
1287ret ARK_ROUTE_APPROVAL
1288end
1289end
1290ret ARK_ROUTE_CLARIFY
1291end
1292if eq act ARK_ACT_CANCEL
1293if eq owner 1
1294if eq pending 1
1295ret ARK_ROUTE_APPROVAL
1296end
1297end
1298ret ARK_ROUTE_CLARIFY
1299end
1300if eq act ARK_ACT_REMEMBER
1301ret ARK_ROUTE_APPROVAL
1302end
1303if eq act ARK_ACT_PREFER
1304ret ARK_ROUTE_APPROVAL
1305end
1306if eq act ARK_ACT_TASK
1307ret ARK_ROUTE_APPROVAL
1308end
1309if eq act ARK_ACT_PROMPT
1310ret ARK_ROUTE_LOCAL_MODEL
1311end
1312ret ARK_ROUTE_LOCAL_DET
1313end
1314
1315fn ark_route_is_ready route uint caps uint to uint
1316if eq route ARK_ROUTE_LOCAL_DET
1317ret uint 1
1318end
1319if eq route ARK_ROUTE_CLARIFY
1320ret uint 1
1321end
1322if eq route ARK_ROUTE_REFUSE
1323ret uint 1
1324end
1325if eq route ARK_ROUTE_MEMORY
1326if ne and caps ARK_CAP_STORE 0
1327ret uint 1
1328end
1329ret uint 0
1330end
1331if eq route ARK_ROUTE_APPROVAL
1332if ne and caps ARK_CAP_DESK 0
1333ret uint 1
1334end
1335ret uint 0
1336end
1337if eq route ARK_ROUTE_LOCAL_MODEL
1338if ne and caps ARK_CAP_LOCAL_MODEL 0
1339ret uint 1
1340end
1341ret uint 0
1342end
1343if ne and caps ARK_CAP_SPARK 0
1344ret uint 1
1345end
1346ret uint 0
1347end
1348
1349fn ark_format_proposal_json req_id ulong sess_id ulong facts view ArkProposalFacts dst edit uchar * to ulong
1350pos = ark_copy_text dst 0 ark_j_p_req
1351pos = ark_write_u64_dec dst pos req_id
1352pos = ark_copy_text dst pos ark_j_p_sess
1353pos = ark_write_u64_dec dst pos sess_id
1354pos = ark_copy_text dst pos ark_j_p_route
1355pos = ark_write_route_name get facts route dst pos
1356pos = ark_copy_text dst pos ark_j_p_lbl
1357pos = ark_write_action_name get facts label dst pos
1358pos = ark_copy_text dst pos ark_j_p_grants
1359if eq get facts ready 1
1360pos = ark_copy_text dst pos ark_j_true
1361else
1362pos = ark_copy_text dst pos ark_j_false
1363end
1364ret pos
1365end
1366
1367note ============================================================
1368note Section 6: Kernel Heap & Lifecycle Entry Points (kernel.rs)
1369note ============================================================
1370
1371fn ark_kheap_load h edit ArkBump to void
1372put h base get ark_kheap 0
1373put h limit get ark_kheap 1
1374put h cur get ark_kheap 2
1375put h last get ark_kheap 3
1376put h peak get ark_kheap 4
1377put h live get ark_kheap 5
1378put h live_peak get ark_kheap 6
1379put h allocs get ark_kheap 7
1380put h fails get ark_kheap 8
1381end
1382
1383fn ark_kheap_save h view ArkBump to void
1384put ark_kheap 0 get h base
1385put ark_kheap 1 get h limit
1386put ark_kheap 2 get h cur
1387put ark_kheap 3 get h last
1388put ark_kheap 4 get h peak
1389put ark_kheap 5 get h live
1390put ark_kheap 6 get h live_peak
1391put ark_kheap 7 get h allocs
1392put ark_kheap 8 get h fails
1393end
1394
1395fn ark_k_heap_init base ulong nbytes ulong to void
1396h = new ArkBump 0 0 0 0 0 0 0 0 0
1397ark_bump_init h base nbytes
1398ark_kheap_save h
1399end
1400
1401fn ark_k_heap_stats st edit ArkStats to void
1402h = new ArkBump 0 0 0 0 0 0 0 0 0
1403ark_kheap_load h
1404ark_bump_stats h st
1405end
1406
1407fn ark_k_card_kind to int
1408k = get ark_kstate 0
1409if eq k 1
1410ret ARK_KIND_EXAM
1411end
1412if eq k 2
1413ret ARK_KIND_ROUTE
1414end
1415ret ARK_KIND_NONE
1416end
1417
1418fn ark_k_close to void
1419h = new ArkBump 0 0 0 0 0 0 0 0 0
1420ark_kheap_load h
1421sp = get ark_kstate 10
1422if ne sp 0
1423ark_bump_dealloc h sp get ark_kstate 9
1424end
1425lp = get ark_kstate 11
1426if ne lp 0
1427ark_bump_dealloc h lp ARK_LINE_CAP
1428end
1429ark_kheap_save h
1430put ark_kstate 0 ulong 0
1431put ark_kstate 1 ulong 0
1432put ark_kstate 2 ulong 0
1433put ark_kstate 3 ulong 0
1434put ark_kstate 4 ulong 0
1435put ark_kstate 5 ulong 0
1436put ark_kstate 6 ulong 0
1437put ark_kstate 7 ulong 0
1438put ark_kstate 8 ulong 0
1439put ark_kstate 9 ulong 0
1440put ark_kstate 10 ulong 0
1441put ark_kstate 11 ulong 0
1442put ark_case_count 0 ulong 0
1443put ark_route_count 0 ulong 0
1444end
1445
1446fn ark_k_card_open buf view uchar * to int
1447ark_k_close
1448n = len buf
1449if eq n 0
1450put ark_last_err 0 ARK_ERR_FILE_SIZE
1451ret -1
1452end
1453if gt n ARK_FILE_MAX
1454put ark_last_err 0 ARK_ERR_FILE_SIZE
1455ret -1
1456end
1457k = ark_card_kind buf
1458cnt = int -1
1459if eq k ARK_KIND_ROUTE
1460cnt = ark_card_parse_route buf
1461else
1462cnt = ark_card_parse_exam buf
1463end
1464if lt cnt 0
1465ret -1
1466end
1467i = ulong 0
1468while lt i n bound 131072
1469put ark_kcard_buf i get buf i
1470i = add i 1
1471end
1472h = new ArkBump 0 0 0 0 0 0 0 0 0
1473ark_kheap_load h
1474lp = ark_bump_alloc h ARK_LINE_CAP 8
1475if eq lp 0
1476ark_kheap_save h
1477ret -1
1478end
1479m = new ArkMark 0 0
1480ark_bump_mark h m
1481ark_kheap_save h
1482if eq k ARK_KIND_ROUTE
1483put ark_kstate 0 ulong 2
1484else
1485put ark_kstate 0 ulong 1
1486end
1487put ark_kstate 1 n
1488put ark_kstate 2 ulong 1
1489put ark_kstate 3 get m cur
1490put ark_kstate 4 get m live
1491put ark_kstate 11 lp
1492ret cnt
1493end
1494
1495fn ark_k_train_counts mode uint to ulong
1496a = ulong 0
1497while lt a ARK_N_LABELS bound 15
1498put ark_kact_counts a ulong 0
1499put ark_kmode_weights a ulong 0
1500a = add a 1
1501end
1502cnt = get ark_pool_count 0
1503i = ulong 0
1504while lt i cnt bound 512
1505act = ulong get ark_pool_act i
1506if lt act ARK_N_LABELS
1507put ark_kact_counts act add get ark_kact_counts act 1
1508put ark_kmode_weights act add get ark_kmode_weights act add 1 ulong mode
1509end
1510i = add i 1
1511end
1512ret cnt
1513end
1514
1515fn ark_k_train mode uint info edit ArkTrainInfo to int
1516h = new ArkBump 0 0 0 0 0 0 0 0 0
1517mk = new ArkMark 0 0
1518put ark_kstate 5 ulong 0
1519if lt ark_mode_of mode 0
1520ret -1
1521end
1522if eq get ark_kstate 2 0
1523ret -1
1524end
1525ark_kheap_load h
1526if ne get ark_kstate 10 0
1527ark_bump_dealloc h get ark_kstate 10 get ark_kstate 9
1528put ark_kstate 10 ulong 0
1529put ark_kstate 9 ulong 0
1530end
1531put mk cur get ark_kstate 3
1532put mk live get ark_kstate 4
1533if not ark_bump_reset_to h mk
1534ark_kheap_save h
1535ret -2
1536end
1537ark_bump_reset_peaks h
1538t0 = monons
1539cnt = ark_k_train_counts mode
1540mem_bytes = add 4096 mul cnt 64
1541sp_new = ark_bump_alloc h mem_bytes 8
1542if eq sp_new 0
1543ark_kheap_save h
1544ret -2
1545end
1546put info ticks sub ulong monons ulong t0
1547ark_kheap_save h
1548put ark_kstate 5 ulong 1
1549put ark_kstate 6 ulong mode
1550put ark_kstate 7 cnt
1551put ark_kstate 8 cnt
1552put ark_kstate 9 mem_bytes
1553put ark_kstate 10 sp_new
1554put info examples cnt
1555put info exemplars cnt
1556put info memory_bytes mem_bytes
1557ret 0
1558end
1559
1560fn ark_copy_card_slice off ulong nbytes ulong dst edit uchar * to void
1561i = ulong 0
1562while lt i nbytes bound 4096
1563put dst i get ark_kcard_buf add off i
1564i = add i 1
1565end
1566end
1567
1568fn ark_copy_to_dst src view uchar * nbytes ulong dst edit uchar * to int
1569if gt add nbytes 1 len dst
1570ret -2
1571end
1572k = ulong 0
1573while lt k nbytes bound 512
1574put dst k get src k
1575k = add k 1
1576end
1577put dst nbytes uchar 0
1578ret 0
1579end
1580
1581fn ark_k_decide_json idx uint jbuf edit uchar * to ulong
1582trn_tmp = new uchar 4096 0
1583trn_len = sub get ark_case_turns_end ulong idx get ark_case_turns_off ulong idx
1584ark_copy_card_slice get ark_case_turns_off ulong idx trn_len trn_tmp
1585ret ark_decide_json_buf uint get ark_kstate 6 part trn_tmp 0 trn_len jbuf
1586end
1587
1588fn ark_k_emit_prefixed tag_kind uint id_off ulong id_len ulong jbuf view uchar * to long
1589id_tmp = new uchar 16 0
1590lbuf = new uchar 384 0
1591ark_copy_card_slice id_off id_len id_tmp
1592joff = ark_runner_line_prefix tag_kind uint get ark_kstate 6 part id_tmp 0 id_len lbuf
1593if lt joff 0
1594ret -2
1595end
1596tot = ark_runner_line_append lbuf ulong joff jbuf
1597if lt tot 0
1598ret -2
1599end
1600i = ulong 0
1601while lt i ulong tot bound 384
1602put ark_kline_buf i get lbuf i
1603i = add i 1
1604end
1605ret or shl joff 16 tot
1606end
1607
1608fn ark_k_copy_kline nbytes ulong dst edit uchar * to int
1609if gt add nbytes 1 len dst
1610ret -2
1611end
1612k = ulong 0
1613while lt k nbytes bound 512
1614put dst k get ark_kline_buf k
1615k = add k 1
1616end
1617put dst nbytes uchar 0
1618ret 0
1619end
1620
1621fn ark_k_decide_format idx uint dst edit uchar * info edit ArkDecideInfo to int
1622jbuf = new uchar 128 0
1623t0 = monons
1624jlen = ark_k_decide_json idx jbuf
1625put info ticks sub ulong monons ulong t0
1626id_off = get ark_case_id_off ulong idx
1627id_len = sub get ark_case_id_end ulong idx id_off
1628packed = ark_k_emit_prefixed 0 id_off id_len part jbuf 0 jlen
1629if lt packed 0
1630ret -2
1631end
1632tot = and ulong packed 65535
1633put info nbytes tot
1634put info json_off shr ulong packed 16
1635ret ark_k_copy_kline tot dst
1636end
1637
1638fn ark_k_decide idx uint dst edit uchar * info edit ArkDecideInfo to int
1639h = new ArkBump 0 0 0 0 0 0 0 0 0
1640mk = new ArkMark 0 0
1641if eq get ark_kstate 5 0
1642ret -1
1643end
1644if ne get ark_kstate 0 1
1645ret -1
1646end
1647if ge ulong idx get ark_case_count 0
1648ret -1
1649end
1650ark_kheap_load h
1651ark_bump_mark h mk
1652scr = ark_bump_alloc h 1024 8
1653if eq scr 0
1654ark_kheap_save h
1655ret -1
1656end
1657rc = ark_k_decide_format idx dst info
1658ark_bump_dealloc h scr 1024
1659if ne rc 0
1660ark_kheap_save h
1661ret rc
1662end
1663rst = ark_bump_reset_to h mk
1664ark_kheap_save h
1665if rst
1666put info heap_reset ulong 1
1667else
1668put info heap_reset ulong 0
1669end
1670ret 0
1671end
1672
1673fn ark_k_route_decode_act idx uint to uint
1674raw_trn = new uchar 4096 0
1675dec_trn = new uchar 4096 0
1676trn_len = sub get ark_route_turns_end ulong idx get ark_route_turns_off ulong idx
1677ark_copy_card_slice get ark_route_turns_off ulong idx trn_len raw_trn
1678dlen = ark_pct_decode part raw_trn 0 trn_len dec_trn
1679if ge dlen 0
1680ret ark_score_turns uint get ark_kstate 6 part dec_trn 0 ulong dlen
1681end
1682ret ARK_ACT_CLARIFY
1683end
1684
1685fn ark_k_route_propose idx uint caps uint facts edit ArkProposalFacts jbuf edit uchar * to long
1686act = ark_k_route_decode_act idx
1687is_owner = uint get ark_route_owner ulong idx
1688rt = ark_route_for_action act is_owner uint get ark_route_consent ulong idx uint get ark_route_pending ulong idx
1689put facts route rt
1690put facts label act
1691put facts owner is_owner
1692put facts ready ark_route_is_ready rt caps
1693ret long ark_format_proposal_json add ulong idx 1 get ark_route_session ulong idx facts jbuf
1694end
1695
1696fn ark_k_route_format idx uint caps uint dst edit uchar * info edit ArkRouteInfo to int
1697facts = new ArkProposalFacts 0 0 0 0
1698jbuf = new uchar 256 0
1699t0 = monons
1700jlen = ark_k_route_propose idx caps facts jbuf
1701put info ticks sub ulong monons ulong t0
1702put info route get facts route
1703put info label get facts label
1704put info owner get facts owner
1705put info ready get facts ready
1706id_off = get ark_route_id_off ulong idx
1707id_len = sub get ark_route_id_end ulong idx id_off
1708packed = ark_k_emit_prefixed 1 id_off id_len part jbuf 0 ulong jlen
1709if lt packed 0
1710ret -2
1711end
1712tot = and ulong packed 65535
1713put info nbytes tot
1714put info json_off shr ulong packed 16
1715ret ark_k_copy_kline tot dst
1716end
1717
1718fn ark_k_route_eval idx uint caps uint dst edit uchar * info edit ArkRouteInfo to int
1719h = new ArkBump 0 0 0 0 0 0 0 0 0
1720mk = new ArkMark 0 0
1721if eq get ark_kstate 5 0
1722ret -1
1723end
1724if ne get ark_kstate 0 2
1725ret -1
1726end
1727if ge ulong idx get ark_route_count 0
1728ret -1
1729end
1730ark_kheap_load h
1731ark_bump_mark h mk
1732scr = ark_bump_alloc h 1024 8
1733if eq scr 0
1734ark_kheap_save h
1735ret -1
1736end
1737rc = ark_k_route_format idx caps dst info
1738ark_bump_dealloc h scr 1024
1739if ne rc 0
1740ark_kheap_save h
1741ret rc
1742end
1743rst = ark_bump_reset_to h mk
1744ark_kheap_save h
1745if rst
1746put info heap_reset ulong 1
1747else
1748put info heap_reset ulong 0
1749end
1750ret 0
1751end