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