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
omega-mv / aienux / crates / allen-reflex / reflex_route.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note aienux/crates/allen-reflex/reflex_route.om: ALLEN Tower & Interactive Routing in pure OM-2. |
| 5 | note Library unit (no om2 header line, no fn main). Prefix: arx_. |
| 6 | note Requires reflex.om before it and om/tls/sha2.om in the same concatenated unit. |
| 7 | |
| 8 | note ========================================================================= |
| 9 | note Section 13: ALLEN Tower (Scheduling Orchestrator & Mneme Digest) |
| 10 | note ========================================================================= |
| 11 | |
| 12 | const ARX_TIER_PORTABLE uchar 0 |
| 13 | const ARX_TIER_PI5_HOST uchar 1 |
| 14 | const ARX_TIER_SPARK_HOST uchar 2 |
| 15 | const ARX_TIER_SPARK_QEMU uchar 3 |
| 16 | const ARX_TIER_SPARK_GB10 uchar 4 |
| 17 | const ARX_TIER_SPARK_NVRM uchar 5 |
| 18 | |
| 19 | const ARX_TW_ACT_REUSE uchar 0 |
| 20 | const ARX_TW_ACT_JOIN uchar 1 |
| 21 | const ARX_TW_ACT_PI5 uchar 2 |
| 22 | const ARX_TW_ACT_SPARK_HOST uchar 3 |
| 23 | const ARX_TW_ACT_SPARK_GPU uchar 4 |
| 24 | |
| 25 | const ARX_TW_WHY_CACHED uchar 0 |
| 26 | const ARX_TW_WHY_INFLIGHT uchar 1 |
| 27 | const ARX_TW_WHY_PI5 uchar 2 |
| 28 | const ARX_TW_WHY_SPARK_HOST uchar 3 |
| 29 | const ARX_TW_WHY_SPARK_GPU uchar 4 |
| 30 | const ARX_TW_WHY_UNCACHEABLE uchar 5 |
| 31 | |
| 32 | const ARX_TW_MAX_RCPTS ulong 64 |
| 33 | const ARX_TW_MAX_INF ulong 16 |
| 34 | const ARX_TW_GATE_STRIDE ulong 64 |
| 35 | |
| 36 | global arx_tw_max_rcpts ulong 1 |
| 37 | global arx_tw_max_inf ulong 1 |
| 38 | global arx_tw_rcpt_n ulong 1 |
| 39 | global arx_tw_rcpt_gate uchar 4096 |
| 40 | global arx_tw_rcpt_glen ulong 64 |
| 41 | global arx_tw_rcpt_tier uchar 64 |
| 42 | global arx_tw_rcpt_din uchar 2048 |
| 43 | global arx_tw_rcpt_drcpt uchar 2048 |
| 44 | |
| 45 | global arx_tw_inf_used uchar 16 |
| 46 | global arx_tw_inf_gate uchar 1024 |
| 47 | global arx_tw_inf_glen ulong 16 |
| 48 | global arx_tw_inf_tier uchar 16 |
| 49 | global arx_tw_inf_din uchar 512 |
| 50 | |
| 51 | global arx_tw_dec_act uchar 1 |
| 52 | global arx_tw_dec_why uchar 1 |
| 53 | global arx_tw_dec_slot ulong 1 |
| 54 | global arx_tw_dec_rcpt uchar 32 |
| 55 | global arx_tw_dec_gate uchar 64 |
| 56 | global arx_tw_dec_glen ulong 1 |
| 57 | |
| 58 | text arx_tw_domain "ALLEN-TOWER-INPUT-V1" |
| 59 | text arx_tw_s_act0 "reuse_receipt" |
| 60 | text arx_tw_s_act1 "join_inflight" |
| 61 | text arx_tw_s_act2 "run_pi5_local" |
| 62 | text arx_tw_s_act3 "dispatch_spark_host" |
| 63 | text arx_tw_s_act4 "queue_spark_gb10" |
| 64 | |
| 65 | text arx_tw_s_why0 "cached_pass_receipt" |
| 66 | text arx_tw_s_why1 "inflight_duplicate" |
| 67 | text arx_tw_s_why2 "tier_local_pi5" |
| 68 | text arx_tw_s_why3 "tier_spark_host_pool" |
| 69 | text arx_tw_s_why4 "tier_spark_gpu_serial" |
| 70 | text arx_tw_s_why5 "uncacheable_gate" |
| 71 | |
| 72 | text arx_tw_j1 "{\"tower_action\":\"" |
| 73 | text arx_tw_j2 "\",\"gate\":\"" |
| 74 | text arx_tw_j3 "\",\"why\":\"" |
| 75 | text arx_tw_j4 "\"}" |
| 76 | text arx_hex_digits "0123456789abcdef" |
| 77 | |
| 78 | fn arx_combine_mneme_digest decl view uchar 32 pins view uchar 32 reads view uchar * n_reads ulong dst edit uchar 32 to void |
| 79 | ctx = new ulong 40 0 |
| 80 | nul1 = new uchar 1 0 |
| 81 | cnt_le = new uchar 8 0 |
| 82 | sha256_init ctx |
| 83 | sha256_update ctx arx_tw_domain |
| 84 | sha256_update ctx nul1 |
| 85 | sha256_update ctx decl |
| 86 | sha256_update ctx pins |
| 87 | put cnt_le 0 uchar and n_reads 255 |
| 88 | put cnt_le 1 uchar and shr n_reads 8 255 |
| 89 | put cnt_le 2 uchar and shr n_reads 16 255 |
| 90 | put cnt_le 3 uchar and shr n_reads 24 255 |
| 91 | put cnt_le 4 uchar and shr n_reads 32 255 |
| 92 | put cnt_le 5 uchar and shr n_reads 40 255 |
| 93 | put cnt_le 6 uchar and shr n_reads 48 255 |
| 94 | put cnt_le 7 uchar and shr n_reads 56 255 |
| 95 | sha256_update ctx cnt_le |
| 96 | i = ulong 0 |
| 97 | while lt i n_reads bound 64 |
| 98 | sha256_update ctx part reads mul i 32 32 |
| 99 | i = add i 1 |
| 100 | end |
| 101 | sha256_final ctx dst |
| 102 | end |
| 103 | |
| 104 | fn arx_tower_init max_rcpts ulong max_inf ulong to void |
| 105 | mr = max_rcpts |
| 106 | if gt mr ARX_TW_MAX_RCPTS |
| 107 | mr = ARX_TW_MAX_RCPTS |
| 108 | end |
| 109 | mi = max_inf |
| 110 | if gt mi ARX_TW_MAX_INF |
| 111 | mi = ARX_TW_MAX_INF |
| 112 | end |
| 113 | put arx_tw_max_rcpts 0 mr |
| 114 | put arx_tw_max_inf 0 mi |
| 115 | put arx_tw_rcpt_n 0 0 |
| 116 | i = ulong 0 |
| 117 | while lt i ARX_TW_MAX_INF bound 16 |
| 118 | put arx_tw_inf_used i 0 |
| 119 | i = add i 1 |
| 120 | end |
| 121 | end |
| 122 | |
| 123 | fn arx_tw_match_rcpt idx ulong gate view uchar * tier uchar din view uchar 32 to bool |
| 124 | if ne get arx_tw_rcpt_tier idx tier |
| 125 | ret false |
| 126 | end |
| 127 | gn = len gate |
| 128 | if ne get arx_tw_rcpt_glen idx gn |
| 129 | ret false |
| 130 | end |
| 131 | gb = mul idx ARX_TW_GATE_STRIDE |
| 132 | i = ulong 0 |
| 133 | while lt i gn bound 64 |
| 134 | if ne get arx_tw_rcpt_gate add gb i get gate i |
| 135 | ret false |
| 136 | end |
| 137 | i = add i 1 |
| 138 | end |
| 139 | db = mul idx 32 |
| 140 | i = ulong 0 |
| 141 | while lt i 32 bound 32 |
| 142 | if ne get arx_tw_rcpt_din add db i get din i |
| 143 | ret false |
| 144 | end |
| 145 | i = add i 1 |
| 146 | end |
| 147 | ret true |
| 148 | end |
| 149 | |
| 150 | fn arx_tw_match_inf idx ulong gate view uchar * tier uchar din view uchar 32 to bool |
| 151 | if eq get arx_tw_inf_used idx 0 |
| 152 | ret false |
| 153 | end |
| 154 | if ne get arx_tw_inf_tier idx tier |
| 155 | ret false |
| 156 | end |
| 157 | gn = len gate |
| 158 | if ne get arx_tw_inf_glen idx gn |
| 159 | ret false |
| 160 | end |
| 161 | gb = mul idx ARX_TW_GATE_STRIDE |
| 162 | i = ulong 0 |
| 163 | while lt i gn bound 64 |
| 164 | if ne get arx_tw_inf_gate add gb i get gate i |
| 165 | ret false |
| 166 | end |
| 167 | i = add i 1 |
| 168 | end |
| 169 | db = mul idx 32 |
| 170 | i = ulong 0 |
| 171 | while lt i 32 bound 32 |
| 172 | if ne get arx_tw_inf_din add db i get din i |
| 173 | ret false |
| 174 | end |
| 175 | i = add i 1 |
| 176 | end |
| 177 | ret true |
| 178 | end |
| 179 | |
| 180 | fn arx_tower_complete_inflight gate view uchar * tier uchar din view uchar 32 to bool |
| 181 | mi = get arx_tw_max_inf 0 |
| 182 | i = ulong 0 |
| 183 | while lt i mi bound 16 |
| 184 | if arx_tw_match_inf i gate tier din |
| 185 | put arx_tw_inf_used i 0 |
| 186 | ret true |
| 187 | end |
| 188 | i = add i 1 |
| 189 | end |
| 190 | ret false |
| 191 | end |
| 192 | |
| 193 | fn arx_tw_shift_rcpts to void |
| 194 | rn = get arx_tw_rcpt_n 0 |
| 195 | if le rn 1 |
| 196 | put arx_tw_rcpt_n 0 0 |
| 197 | ret |
| 198 | end |
| 199 | i = ulong 1 |
| 200 | while lt i rn bound 64 |
| 201 | d = sub i 1 |
| 202 | put arx_tw_rcpt_tier d get arx_tw_rcpt_tier i |
| 203 | gl = get arx_tw_rcpt_glen i |
| 204 | put arx_tw_rcpt_glen d gl |
| 205 | sb = mul i ARX_TW_GATE_STRIDE |
| 206 | db = mul d ARX_TW_GATE_STRIDE |
| 207 | k = ulong 0 |
| 208 | while lt k gl bound 64 |
| 209 | put arx_tw_rcpt_gate add db k get arx_tw_rcpt_gate add sb k |
| 210 | k = add k 1 |
| 211 | end |
| 212 | si = mul i 32 |
| 213 | di = mul d 32 |
| 214 | k = ulong 0 |
| 215 | while lt k 32 bound 32 |
| 216 | put arx_tw_rcpt_din add di k get arx_tw_rcpt_din add si k |
| 217 | put arx_tw_rcpt_drcpt add di k get arx_tw_rcpt_drcpt add si k |
| 218 | k = add k 1 |
| 219 | end |
| 220 | i = add i 1 |
| 221 | end |
| 222 | put arx_tw_rcpt_n 0 sub rn 1 |
| 223 | end |
| 224 | |
| 225 | fn arx_tw_store_rcpt_gate rn ulong gate view uchar * to void |
| 226 | gn = len gate |
| 227 | if gt gn ARX_TW_GATE_STRIDE |
| 228 | gn = ARX_TW_GATE_STRIDE |
| 229 | end |
| 230 | put arx_tw_rcpt_glen rn gn |
| 231 | gb = mul rn ARX_TW_GATE_STRIDE |
| 232 | k = ulong 0 |
| 233 | while lt k gn bound 64 |
| 234 | put arx_tw_rcpt_gate add gb k get gate k |
| 235 | k = add k 1 |
| 236 | end |
| 237 | end |
| 238 | |
| 239 | fn arx_tw_store_rcpt_digs rn ulong din view uchar 32 drcpt view uchar 32 to void |
| 240 | db = mul rn 32 |
| 241 | k = ulong 0 |
| 242 | while lt k 32 bound 32 |
| 243 | put arx_tw_rcpt_din add db k get din k |
| 244 | put arx_tw_rcpt_drcpt add db k get drcpt k |
| 245 | k = add k 1 |
| 246 | end |
| 247 | end |
| 248 | |
| 249 | fn arx_tower_record_pass gate view uchar * tier uchar din view uchar 32 drcpt view uchar 32 to void |
| 250 | ign_c = arx_tower_complete_inflight gate tier din |
| 251 | rn = get arx_tw_rcpt_n 0 |
| 252 | i = ulong 0 |
| 253 | while lt i rn bound 64 |
| 254 | if arx_tw_match_rcpt i gate tier din |
| 255 | arx_tw_store_rcpt_digs i din drcpt |
| 256 | ret |
| 257 | end |
| 258 | i = add i 1 |
| 259 | end |
| 260 | mr = get arx_tw_max_rcpts 0 |
| 261 | if eq mr 0 |
| 262 | ret |
| 263 | end |
| 264 | if ge rn mr |
| 265 | arx_tw_shift_rcpts |
| 266 | rn = get arx_tw_rcpt_n 0 |
| 267 | end |
| 268 | put arx_tw_rcpt_tier rn tier |
| 269 | arx_tw_store_rcpt_gate rn gate |
| 270 | arx_tw_store_rcpt_digs rn din drcpt |
| 271 | put arx_tw_rcpt_n 0 add rn 1 |
| 272 | end |
| 273 | |
| 274 | fn arx_tw_insert_inf gate view uchar * tier uchar din view uchar 32 to void |
| 275 | gn = len gate |
| 276 | if gt gn ARX_TW_GATE_STRIDE |
| 277 | gn = ARX_TW_GATE_STRIDE |
| 278 | end |
| 279 | mi = get arx_tw_max_inf 0 |
| 280 | i = ulong 0 |
| 281 | while lt i mi bound 16 |
| 282 | if eq get arx_tw_inf_used i 0 |
| 283 | put arx_tw_inf_used i 1 |
| 284 | put arx_tw_inf_tier i tier |
| 285 | put arx_tw_inf_glen i gn |
| 286 | gb = mul i ARX_TW_GATE_STRIDE |
| 287 | k = ulong 0 |
| 288 | while lt k gn bound 64 |
| 289 | put arx_tw_inf_gate add gb k get gate k |
| 290 | k = add k 1 |
| 291 | end |
| 292 | db = mul i 32 |
| 293 | k = ulong 0 |
| 294 | while lt k 32 bound 32 |
| 295 | put arx_tw_inf_din add db k get din k |
| 296 | k = add k 1 |
| 297 | end |
| 298 | ret |
| 299 | end |
| 300 | i = add i 1 |
| 301 | end |
| 302 | end |
| 303 | |
| 304 | fn arx_tw_copy_dec_gate gate view uchar * to void |
| 305 | gn = len gate |
| 306 | if gt gn ARX_TW_GATE_STRIDE |
| 307 | gn = ARX_TW_GATE_STRIDE |
| 308 | end |
| 309 | put arx_tw_dec_glen 0 gn |
| 310 | k = ulong 0 |
| 311 | while lt k gn bound 64 |
| 312 | put arx_tw_dec_gate k get gate k |
| 313 | k = add k 1 |
| 314 | end |
| 315 | end |
| 316 | |
| 317 | fn arx_tw_copy_dec_rcpt idx ulong to void |
| 318 | db = mul idx 32 |
| 319 | k = ulong 0 |
| 320 | while lt k 32 bound 32 |
| 321 | put arx_tw_dec_rcpt k get arx_tw_rcpt_drcpt add db k |
| 322 | k = add k 1 |
| 323 | end |
| 324 | end |
| 325 | |
| 326 | fn arx_tower_schedule gate view uchar * tier uchar din view uchar 32 cacheable bool to uchar |
| 327 | arx_tw_copy_dec_gate gate |
| 328 | act = ARX_TW_ACT_SPARK_GPU |
| 329 | twhy = ARX_TW_WHY_SPARK_GPU |
| 330 | if or eq tier ARX_TIER_PORTABLE eq tier ARX_TIER_PI5_HOST |
| 331 | act = ARX_TW_ACT_PI5 |
| 332 | twhy = ARX_TW_WHY_PI5 |
| 333 | else |
| 334 | if or eq tier ARX_TIER_SPARK_HOST eq tier ARX_TIER_SPARK_QEMU |
| 335 | act = ARX_TW_ACT_SPARK_HOST |
| 336 | twhy = ARX_TW_WHY_SPARK_HOST |
| 337 | end |
| 338 | end |
| 339 | if not cacheable |
| 340 | put arx_tw_dec_act 0 act |
| 341 | put arx_tw_dec_why 0 ARX_TW_WHY_UNCACHEABLE |
| 342 | ret act |
| 343 | end |
| 344 | rn = get arx_tw_rcpt_n 0 |
| 345 | i = ulong 0 |
| 346 | while lt i rn bound 64 |
| 347 | if arx_tw_match_rcpt i gate tier din |
| 348 | arx_tw_copy_dec_rcpt i |
| 349 | put arx_tw_dec_act 0 ARX_TW_ACT_REUSE |
| 350 | put arx_tw_dec_why 0 ARX_TW_WHY_CACHED |
| 351 | ret ARX_TW_ACT_REUSE |
| 352 | end |
| 353 | i = add i 1 |
| 354 | end |
| 355 | mi = get arx_tw_max_inf 0 |
| 356 | i = ulong 0 |
| 357 | while lt i mi bound 16 |
| 358 | if arx_tw_match_inf i gate tier din |
| 359 | put arx_tw_dec_act 0 ARX_TW_ACT_JOIN |
| 360 | put arx_tw_dec_why 0 ARX_TW_WHY_INFLIGHT |
| 361 | put arx_tw_dec_slot 0 i |
| 362 | ret ARX_TW_ACT_JOIN |
| 363 | end |
| 364 | i = add i 1 |
| 365 | end |
| 366 | arx_tw_insert_inf gate tier din |
| 367 | put arx_tw_dec_act 0 act |
| 368 | put arx_tw_dec_why 0 twhy |
| 369 | ret act |
| 370 | end |
| 371 | |
| 372 | fn arx_tw_push_escaped dst edit uchar * pos ulong src view uchar * to ulong |
| 373 | p = pos |
| 374 | n = len src |
| 375 | i = ulong 0 |
| 376 | while lt i n bound 256 |
| 377 | c = get src i |
| 378 | if eq c 34 |
| 379 | put dst p 92 |
| 380 | put dst add p 1 34 |
| 381 | p = add p 2 |
| 382 | else |
| 383 | if eq c 92 |
| 384 | put dst p 92 |
| 385 | put dst add p 1 92 |
| 386 | p = add p 2 |
| 387 | else |
| 388 | if eq c 10 |
| 389 | put dst p 92 |
| 390 | put dst add p 1 110 |
| 391 | p = add p 2 |
| 392 | else |
| 393 | if eq c 13 |
| 394 | put dst p 92 |
| 395 | put dst add p 1 114 |
| 396 | p = add p 2 |
| 397 | else |
| 398 | if eq c 9 |
| 399 | put dst p 92 |
| 400 | put dst add p 1 116 |
| 401 | p = add p 2 |
| 402 | else |
| 403 | if lt c 0x20 |
| 404 | put dst p 92 |
| 405 | put dst add p 1 117 |
| 406 | put dst add p 2 48 |
| 407 | put dst add p 3 48 |
| 408 | put dst add p 4 get arx_hex_digits ulong shr c 4 |
| 409 | put dst add p 5 get arx_hex_digits ulong and c 15 |
| 410 | p = add p 6 |
| 411 | else |
| 412 | put dst p c |
| 413 | p = add p 1 |
| 414 | end |
| 415 | end |
| 416 | end |
| 417 | end |
| 418 | end |
| 419 | end |
| 420 | i = add i 1 |
| 421 | end |
| 422 | ret p |
| 423 | end |
| 424 | |
| 425 | fn arx_tower_decision_json dst edit uchar * to ulong |
| 426 | gbuf = new uchar 64 0 |
| 427 | p = ulong 0 |
| 428 | p = arx_copy_bytes dst p arx_tw_j1 |
| 429 | act = get arx_tw_dec_act 0 |
| 430 | if eq act ARX_TW_ACT_REUSE |
| 431 | p = arx_copy_bytes dst p arx_tw_s_act0 |
| 432 | else |
| 433 | if eq act ARX_TW_ACT_JOIN |
| 434 | p = arx_copy_bytes dst p arx_tw_s_act1 |
| 435 | else |
| 436 | if eq act ARX_TW_ACT_PI5 |
| 437 | p = arx_copy_bytes dst p arx_tw_s_act2 |
| 438 | else |
| 439 | if eq act ARX_TW_ACT_SPARK_HOST |
| 440 | p = arx_copy_bytes dst p arx_tw_s_act3 |
| 441 | else |
| 442 | p = arx_copy_bytes dst p arx_tw_s_act4 |
| 443 | end |
| 444 | end |
| 445 | end |
| 446 | end |
| 447 | p = arx_copy_bytes dst p arx_tw_j2 |
| 448 | gl = get arx_tw_dec_glen 0 |
| 449 | k = ulong 0 |
| 450 | while lt k gl bound 64 |
| 451 | put gbuf k get arx_tw_dec_gate k |
| 452 | k = add k 1 |
| 453 | end |
| 454 | p = arx_tw_push_escaped dst p part gbuf 0 gl |
| 455 | p = arx_copy_bytes dst p arx_tw_j3 |
| 456 | w = get arx_tw_dec_why 0 |
| 457 | if eq w ARX_TW_WHY_CACHED |
| 458 | p = arx_copy_bytes dst p arx_tw_s_why0 |
| 459 | else |
| 460 | if eq w ARX_TW_WHY_INFLIGHT |
| 461 | p = arx_copy_bytes dst p arx_tw_s_why1 |
| 462 | else |
| 463 | if eq w ARX_TW_WHY_PI5 |
| 464 | p = arx_copy_bytes dst p arx_tw_s_why2 |
| 465 | else |
| 466 | if eq w ARX_TW_WHY_SPARK_HOST |
| 467 | p = arx_copy_bytes dst p arx_tw_s_why3 |
| 468 | else |
| 469 | if eq w ARX_TW_WHY_SPARK_GPU |
| 470 | p = arx_copy_bytes dst p arx_tw_s_why4 |
| 471 | else |
| 472 | p = arx_copy_bytes dst p arx_tw_s_why5 |
| 473 | end |
| 474 | end |
| 475 | end |
| 476 | end |
| 477 | end |
| 478 | p = arx_copy_bytes dst p arx_tw_j4 |
| 479 | ret p |
| 480 | end |
| 481 | |
| 482 | note ========================================================================= |
| 483 | note Section 14: Interactive Routing (route.rs) |
| 484 | note ========================================================================= |
| 485 | |
| 486 | const ARX_INTERACTIVE_MAX ulong 512 |
| 487 | const ARX_PST1_HEADER ulong 224 |
| 488 | const ARX_PST1_PAYLOAD_MAX ulong 800 |
| 489 | |
| 490 | const ARX_RT_LOCAL_DET uchar 0 |
| 491 | const ARX_RT_MEMORY uchar 1 |
| 492 | const ARX_RT_CLARIFY uchar 2 |
| 493 | const ARX_RT_REFUSE uchar 3 |
| 494 | const ARX_RT_LOCAL_MODEL uchar 4 |
| 495 | const ARX_RT_SPARK_INF uchar 5 |
| 496 | const ARX_RT_SPARK_OM uchar 6 |
| 497 | const ARX_RT_APPROVAL uchar 7 |
| 498 | const ARX_RT_NONE uchar 255 |
| 499 | |
| 500 | const ARX_THEN_APPROVE uchar 0 |
| 501 | const ARX_THEN_DENY uchar 1 |
| 502 | const ARX_THEN_CANCEL uchar 2 |
| 503 | const ARX_THEN_REMEMBER uchar 3 |
| 504 | const ARX_THEN_PREFER uchar 4 |
| 505 | const ARX_THEN_NOTE_TASK uchar 5 |
| 506 | const ARX_THEN_SPARK_TASK uchar 6 |
| 507 | const ARX_THEN_NONE uchar 255 |
| 508 | |
| 509 | const ARX_SPK_OWNER uchar 0 |
| 510 | const ARX_SPK_GUEST uchar 1 |
| 511 | |
| 512 | const ARX_NR_STORE uchar 0 |
| 513 | const ARX_NR_NO_CONSENT uchar 1 |
| 514 | const ARX_NR_DESK uchar 2 |
| 515 | const ARX_NR_NO_MODEL uchar 3 |
| 516 | const ARX_NR_SPARK_DOWN uchar 4 |
| 517 | const ARX_NR_NONE uchar 255 |
| 518 | |
| 519 | const ARX_RWHY_SCORED uchar 0 |
| 520 | const ARX_RWHY_EXACT uchar 1 |
| 521 | const ARX_RWHY_POLICY uchar 2 |
| 522 | const ARX_RWHY_NO_USER uchar 3 |
| 523 | const ARX_RWHY_TOO_LONG uchar 4 |
| 524 | const ARX_RWHY_NON_ASCII uchar 5 |
| 525 | const ARX_RWHY_NO_WORDS uchar 6 |
| 526 | const ARX_RWHY_CORE_CLARIFY uchar 7 |
| 527 | const ARX_RWHY_MISSING_SUBJ uchar 8 |
| 528 | const ARX_RWHY_UNFAMILIAR uchar 9 |
| 529 | const ARX_RWHY_UNSURE uchar 10 |
| 530 | const ARX_RWHY_NOTHING_PEND uchar 11 |
| 531 | const ARX_RWHY_MODEL_UNAVAIL uchar 12 |
| 532 | const ARX_RWHY_GUEST_SCOPE uchar 13 |
| 533 | const ARX_RWHY_OUT_OF_CAP uchar 14 |
| 534 | const ARX_RWHY_SPARK_CUE uchar 15 |
| 535 | |
| 536 | note --- RouteCtx globals --- |
| 537 | global arx_rctx_speaker uchar 1 |
| 538 | global arx_rctx_consent uchar 1 |
| 539 | global arx_rctx_pending uchar 1 |
| 540 | global arx_rctx_caps uint 1 |
| 541 | global arx_rctx_has_model uchar 1 |
| 542 | global arx_rctx_model uchar 64 |
| 543 | global arx_rctx_mlen ulong 1 |
| 544 | global arx_rctx_subj uchar 32 |
| 545 | global arx_rctx_min_sim f32 1 |
| 546 | global arx_rctx_min_margin f32 1 |
| 547 | |
| 548 | note --- DecisionProposal output globals --- |
| 549 | global arx_prop_req ulong 1 |
| 550 | global arx_prop_sess ulong 1 |
| 551 | global arx_prop_speaker uchar 1 |
| 552 | global arx_prop_obj uchar 32 |
| 553 | global arx_prop_route uchar 1 |
| 554 | global arx_prop_then uchar 1 |
| 555 | global arx_prop_not_ready uchar 1 |
| 556 | global arx_prop_fallback uchar 1 |
| 557 | global arx_prop_why uchar 1 |
| 558 | global arx_prop_refuse uchar 1 |
| 559 | global arx_prop_label uchar 1 |
| 560 | global arx_prop_has_txt uchar 1 |
| 561 | global arx_prop_txt uchar 64 |
| 562 | global arx_prop_tlen ulong 1 |
| 563 | global arx_prop_has_pst1 uchar 1 |
| 564 | global arx_prop_pst1 uchar 32 |
| 565 | |
| 566 | text arx_rt_obj_dom "AIEN_E2E_OBJECTIVE_V1\n" |
| 567 | text arx_rt_cause_dom "AIENOS_PST1_CAUSE_V1\n" |
| 568 | text arx_rt_task_dom "AIENOS_PST1_TASK_V1\n" |
| 569 | text arx_rt_idem_dom "AIENOS_PST1_IDEMPOTENCE_V1\n" |
| 570 | |
| 571 | text arx_rt_model_words "gpt chatgpt claude gemini llama tinyllama mistral qwen danube phi grok deepseek gemma copilot" |
| 572 | text arx_rt_spark_verbs "run compile build benchmark train test verify execute measure compute" |
| 573 | text arx_rt_w_spark "spark" |
| 574 | text arx_rt_w_gb10 "gb10" |
| 575 | |
| 576 | fn arx_route_runs_without_owner r uchar to bool |
| 577 | if eq r ARX_RT_LOCAL_DET |
| 578 | ret true |
| 579 | end |
| 580 | if eq r ARX_RT_MEMORY |
| 581 | ret true |
| 582 | end |
| 583 | if eq r ARX_RT_LOCAL_MODEL |
| 584 | ret true |
| 585 | end |
| 586 | if eq r ARX_RT_SPARK_INF |
| 587 | ret true |
| 588 | end |
| 589 | ret false |
| 590 | end |
| 591 | |
| 592 | fn arx_cal_frozen mode uchar to void |
| 593 | if eq mode ARX_MODE_REFLEX |
| 594 | put arx_rctx_min_sim 0 0.20f32 |
| 595 | put arx_rctx_min_margin 0 -1.0e38f32 |
| 596 | else |
| 597 | if eq mode ARX_MODE_NEAREST |
| 598 | put arx_rctx_min_sim 0 0.35f32 |
| 599 | put arx_rctx_min_margin 0 -1.0e38f32 |
| 600 | else |
| 601 | put arx_rctx_min_sim 0 -1.0f32 |
| 602 | put arx_rctx_min_margin 0 0.15f32 |
| 603 | end |
| 604 | end |
| 605 | end |
| 606 | |
| 607 | fn arx_cal_off to void |
| 608 | put arx_rctx_min_sim 0 -1.0f32 |
| 609 | put arx_rctx_min_margin 0 -1.0e38f32 |
| 610 | end |
| 611 | |
| 612 | fn arx_rctx_set_model has_m bool name view uchar * to void |
| 613 | if not has_m |
| 614 | put arx_rctx_has_model 0 0 |
| 615 | put arx_rctx_mlen 0 0 |
| 616 | ret |
| 617 | end |
| 618 | n = len name |
| 619 | if gt n 64 |
| 620 | n = 64 |
| 621 | end |
| 622 | i = ulong 0 |
| 623 | while lt i n bound 64 |
| 624 | put arx_rctx_model i get name i |
| 625 | i = add i 1 |
| 626 | end |
| 627 | put arx_rctx_mlen 0 n |
| 628 | put arx_rctx_has_model 0 1 |
| 629 | end |
| 630 | |
| 631 | fn arx_objective_id msg view uchar * dst edit uchar 32 to void |
| 632 | ctx = new ulong 40 0 |
| 633 | sha256_init ctx |
| 634 | sha256_update ctx arx_rt_obj_dom |
| 635 | sha256_update ctx msg |
| 636 | sha256_final ctx dst |
| 637 | end |
| 638 | |
| 639 | fn arx_put_be64 dst edit uchar * off ulong v ulong to void |
| 640 | put dst off uchar and shr v 56 255 |
| 641 | put dst add off 1 uchar and shr v 48 255 |
| 642 | put dst add off 2 uchar and shr v 40 255 |
| 643 | put dst add off 3 uchar and shr v 32 255 |
| 644 | put dst add off 4 uchar and shr v 24 255 |
| 645 | put dst add off 5 uchar and shr v 16 255 |
| 646 | put dst add off 6 uchar and shr v 8 255 |
| 647 | put dst add off 7 uchar and v 255 |
| 648 | end |
| 649 | |
| 650 | fn arx_pst1_fill_hdr subj view uchar 32 dst edit uchar * to void |
| 651 | ctx = new ulong 40 0 |
| 652 | be8 = new uchar 8 0 |
| 653 | cause_id = new uchar 32 0 |
| 654 | tmp_h = new uchar 32 0 |
| 655 | sha256_init ctx |
| 656 | sha256_update ctx arx_rt_cause_dom |
| 657 | arx_put_be64 be8 0 get arx_prop_sess 0 |
| 658 | sha256_update ctx be8 |
| 659 | arx_put_be64 be8 0 get arx_prop_req 0 |
| 660 | sha256_update ctx be8 |
| 661 | i = ulong 0 |
| 662 | while lt i 32 bound 32 |
| 663 | put tmp_h i get arx_prop_obj i |
| 664 | i = add i 1 |
| 665 | end |
| 666 | sha256_update ctx tmp_h |
| 667 | sha256_final ctx cause_id |
| 668 | i = ulong 0 |
| 669 | while lt i ARX_PST1_HEADER bound 224 |
| 670 | put dst i 0 |
| 671 | i = add i 1 |
| 672 | end |
| 673 | put dst 0 1 |
| 674 | put dst 1 1 |
| 675 | i = ulong 0 |
| 676 | while lt i 32 bound 32 |
| 677 | put dst add 4 i get cause_id i |
| 678 | put dst add 68 i get arx_prop_obj i |
| 679 | put dst add 100 i get subj i |
| 680 | i = add i 1 |
| 681 | end |
| 682 | sha256_init ctx |
| 683 | sha256_update ctx arx_rt_task_dom |
| 684 | sha256_update ctx cause_id |
| 685 | sha256_final ctx tmp_h |
| 686 | i = ulong 0 |
| 687 | while lt i 16 bound 16 |
| 688 | put dst add 148 i get tmp_h i |
| 689 | i = add i 1 |
| 690 | end |
| 691 | sha256_init ctx |
| 692 | sha256_update ctx arx_rt_idem_dom |
| 693 | sha256_update ctx cause_id |
| 694 | sha256_final ctx tmp_h |
| 695 | i = ulong 0 |
| 696 | while lt i 16 bound 16 |
| 697 | put dst add 164 i get tmp_h i |
| 698 | i = add i 1 |
| 699 | end |
| 700 | put dst 184 1 |
| 701 | end |
| 702 | |
| 703 | fn arx_pst1_request route uchar payload view uchar * subj view uchar 32 dst edit uchar * to ulong |
| 704 | tmp_h = new uchar 32 0 |
| 705 | if not or eq route ARX_RT_SPARK_INF eq route ARX_RT_SPARK_OM |
| 706 | ret 0 |
| 707 | end |
| 708 | pn = len payload |
| 709 | if gt pn ARX_PST1_PAYLOAD_MAX |
| 710 | ret 0 |
| 711 | end |
| 712 | if lt len dst add ARX_PST1_HEADER pn |
| 713 | ret 0 |
| 714 | end |
| 715 | arx_pst1_fill_hdr subj dst |
| 716 | put dst 188 uchar and shr pn 8 255 |
| 717 | put dst 189 uchar and pn 255 |
| 718 | ign_h = sha2_digest 256 payload tmp_h |
| 719 | i = ulong 0 |
| 720 | while lt i 32 bound 32 |
| 721 | put dst add 192 i get tmp_h i |
| 722 | i = add i 1 |
| 723 | end |
| 724 | i = ulong 0 |
| 725 | while lt i pn bound 800 |
| 726 | put dst add ARX_PST1_HEADER i get payload i |
| 727 | i = add i 1 |
| 728 | end |
| 729 | ret add ARX_PST1_HEADER pn |
| 730 | end |
| 731 | |
| 732 | fn arx_eq_ci a view uchar * b view uchar * to bool |
| 733 | na = len a |
| 734 | if ne na len b |
| 735 | ret false |
| 736 | end |
| 737 | i = ulong 0 |
| 738 | while lt i na bound 256 |
| 739 | if ne arx_to_lower get a i arx_to_lower get b i |
| 740 | ret false |
| 741 | end |
| 742 | i = add i 1 |
| 743 | end |
| 744 | ret true |
| 745 | end |
| 746 | |
| 747 | fn arx_word_in_list_ci w view uchar * lst view uchar * to bool |
| 748 | n = len lst |
| 749 | i = ulong 0 |
| 750 | while lt i n bound 512 |
| 751 | if eq get lst i 32 |
| 752 | i = add i 1 |
| 753 | continue |
| 754 | end |
| 755 | ws = i |
| 756 | while lt i n bound 512 |
| 757 | if eq get lst i 32 |
| 758 | break |
| 759 | end |
| 760 | i = add i 1 |
| 761 | end |
| 762 | if arx_eq_ci w part lst ws sub i ws |
| 763 | ret true |
| 764 | end |
| 765 | end |
| 766 | ret false |
| 767 | end |
| 768 | |
| 769 | fn arx_named_model_unavail msg view uchar * to bool |
| 770 | im = new uchar 64 0 |
| 771 | has_im = ne get arx_rctx_has_model 0 0 |
| 772 | imlen = get arx_rctx_mlen 0 |
| 773 | if has_im |
| 774 | k = ulong 0 |
| 775 | while lt k imlen bound 64 |
| 776 | put im k get arx_rctx_model k |
| 777 | k = add k 1 |
| 778 | end |
| 779 | end |
| 780 | n = len msg |
| 781 | i = ulong 0 |
| 782 | while lt i n bound 512 |
| 783 | if not arx_is_alnum get msg i |
| 784 | i = add i 1 |
| 785 | continue |
| 786 | end |
| 787 | ws = i |
| 788 | while lt i n bound 512 |
| 789 | if not arx_is_alnum get msg i |
| 790 | break |
| 791 | end |
| 792 | i = add i 1 |
| 793 | end |
| 794 | if arx_word_in_list_ci part msg ws sub i ws arx_rt_model_words |
| 795 | if not has_im |
| 796 | ret true |
| 797 | end |
| 798 | if not arx_eq_ci part msg ws sub i ws part im 0 imlen |
| 799 | ret true |
| 800 | end |
| 801 | ret false |
| 802 | end |
| 803 | end |
| 804 | ret false |
| 805 | end |
| 806 | |
| 807 | fn arx_spark_cue msg view uchar * to bool |
| 808 | has_sp = false |
| 809 | has_vb = false |
| 810 | n = len msg |
| 811 | i = ulong 0 |
| 812 | while lt i n bound 512 |
| 813 | if not arx_is_alnum get msg i |
| 814 | i = add i 1 |
| 815 | continue |
| 816 | end |
| 817 | ws = i |
| 818 | while lt i n bound 512 |
| 819 | if not arx_is_alnum get msg i |
| 820 | break |
| 821 | end |
| 822 | i = add i 1 |
| 823 | end |
| 824 | if or arx_eq_ci part msg ws sub i ws arx_rt_w_spark arx_eq_ci part msg ws sub i ws arx_rt_w_gb10 |
| 825 | has_sp = true |
| 826 | end |
| 827 | if arx_word_in_list_ci part msg ws sub i ws arx_rt_spark_verbs |
| 828 | has_vb = true |
| 829 | end |
| 830 | end |
| 831 | ret and has_sp has_vb |
| 832 | end |
| 833 | |
| 834 | fn arx_prop_reset req_id ulong sess_id ulong to void |
| 835 | put arx_prop_req 0 req_id |
| 836 | put arx_prop_sess 0 sess_id |
| 837 | put arx_prop_speaker 0 get arx_rctx_speaker 0 |
| 838 | i = ulong 0 |
| 839 | while lt i 32 bound 32 |
| 840 | put arx_prop_obj i 0 |
| 841 | put arx_prop_pst1 i 0 |
| 842 | i = add i 1 |
| 843 | end |
| 844 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 845 | put arx_prop_then 0 ARX_THEN_NONE |
| 846 | put arx_prop_not_ready 0 ARX_NR_NONE |
| 847 | put arx_prop_fallback 0 ARX_RT_NONE |
| 848 | put arx_prop_why 0 ARX_RWHY_NO_USER |
| 849 | put arx_prop_refuse 0 ARX_REFUSE_NONE |
| 850 | put arx_prop_label 0 ARX_ACT_CLARIFY |
| 851 | put arx_prop_has_txt 0 0 |
| 852 | put arx_prop_tlen 0 0 |
| 853 | put arx_prop_has_pst1 0 0 |
| 854 | end |
| 855 | |
| 856 | fn arx_propose_dispatch act uchar msg view uchar * to uchar |
| 857 | owner = eq get arx_rctx_speaker 0 ARX_SPK_OWNER |
| 858 | caps = get arx_rctx_caps 0 |
| 859 | has_cons = ne get arx_rctx_consent 0 0 |
| 860 | if or eq act ARX_ACT_STATUS or eq act ARX_ACT_HELP eq act ARX_ACT_EVIDENCE |
| 861 | put arx_prop_route 0 ARX_RT_LOCAL_DET |
| 862 | ret ARX_RT_LOCAL_DET |
| 863 | end |
| 864 | if eq act ARX_ACT_NONE |
| 865 | put arx_prop_route 0 ARX_RT_LOCAL_DET |
| 866 | put arx_prop_why 0 ARX_RWHY_OUT_OF_CAP |
| 867 | ret ARX_RT_LOCAL_DET |
| 868 | end |
| 869 | if or eq act ARX_ACT_RECALL eq act ARX_ACT_FACTS |
| 870 | if not owner |
| 871 | put arx_prop_route 0 ARX_RT_REFUSE |
| 872 | put arx_prop_why 0 ARX_RWHY_GUEST_SCOPE |
| 873 | ret ARX_RT_REFUSE |
| 874 | end |
| 875 | put arx_prop_route 0 ARX_RT_MEMORY |
| 876 | if not has_cons |
| 877 | put arx_prop_not_ready 0 ARX_NR_NO_CONSENT |
| 878 | else |
| 879 | if eq and caps 1 0 |
| 880 | put arx_prop_not_ready 0 ARX_NR_STORE |
| 881 | end |
| 882 | end |
| 883 | ret ARX_RT_MEMORY |
| 884 | end |
| 885 | if or eq act ARX_ACT_REMEMBER or eq act ARX_ACT_PREFER eq act ARX_ACT_TASK |
| 886 | if not owner |
| 887 | put arx_prop_route 0 ARX_RT_REFUSE |
| 888 | put arx_prop_why 0 ARX_RWHY_GUEST_SCOPE |
| 889 | ret ARX_RT_REFUSE |
| 890 | end |
| 891 | if and eq act ARX_ACT_TASK arx_spark_cue msg |
| 892 | put arx_prop_route 0 ARX_RT_SPARK_OM |
| 893 | put arx_prop_why 0 ARX_RWHY_SPARK_CUE |
| 894 | put arx_prop_then 0 ARX_THEN_SPARK_TASK |
| 895 | if eq and caps 2 0 |
| 896 | put arx_prop_not_ready 0 ARX_NR_DESK |
| 897 | else |
| 898 | if eq and caps 8 0 |
| 899 | put arx_prop_not_ready 0 ARX_NR_SPARK_DOWN |
| 900 | end |
| 901 | end |
| 902 | ret ARX_RT_SPARK_OM |
| 903 | end |
| 904 | put arx_prop_route 0 ARX_RT_APPROVAL |
| 905 | if eq act ARX_ACT_REMEMBER |
| 906 | put arx_prop_then 0 ARX_THEN_REMEMBER |
| 907 | else |
| 908 | if eq act ARX_ACT_PREFER |
| 909 | put arx_prop_then 0 ARX_THEN_PREFER |
| 910 | else |
| 911 | put arx_prop_then 0 ARX_THEN_NOTE_TASK |
| 912 | end |
| 913 | end |
| 914 | if and ne act ARX_ACT_TASK not has_cons |
| 915 | put arx_prop_not_ready 0 ARX_NR_NO_CONSENT |
| 916 | else |
| 917 | if eq and caps 2 0 |
| 918 | put arx_prop_not_ready 0 ARX_NR_DESK |
| 919 | else |
| 920 | if eq and caps 1 0 |
| 921 | put arx_prop_not_ready 0 ARX_NR_STORE |
| 922 | end |
| 923 | end |
| 924 | end |
| 925 | ret ARX_RT_APPROVAL |
| 926 | end |
| 927 | if eq act ARX_ACT_PROMPT |
| 928 | if and owner arx_spark_cue msg |
| 929 | put arx_prop_route 0 ARX_RT_SPARK_OM |
| 930 | put arx_prop_why 0 ARX_RWHY_SPARK_CUE |
| 931 | put arx_prop_then 0 ARX_THEN_SPARK_TASK |
| 932 | if eq and caps 2 0 |
| 933 | put arx_prop_not_ready 0 ARX_NR_DESK |
| 934 | else |
| 935 | if eq and caps 8 0 |
| 936 | put arx_prop_not_ready 0 ARX_NR_SPARK_DOWN |
| 937 | end |
| 938 | end |
| 939 | ret ARX_RT_SPARK_OM |
| 940 | end |
| 941 | if ne and caps 4 0 |
| 942 | put arx_prop_route 0 ARX_RT_LOCAL_MODEL |
| 943 | ret ARX_RT_LOCAL_MODEL |
| 944 | end |
| 945 | put arx_prop_route 0 ARX_RT_SPARK_INF |
| 946 | if eq and caps 8 0 |
| 947 | put arx_prop_not_ready 0 ARX_NR_SPARK_DOWN |
| 948 | end |
| 949 | ret ARX_RT_SPARK_INF |
| 950 | end |
| 951 | if or eq act ARX_ACT_APPROVE or eq act ARX_ACT_DENY eq act ARX_ACT_CANCEL |
| 952 | if eq get arx_rctx_pending 0 0 |
| 953 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 954 | put arx_prop_why 0 ARX_RWHY_NOTHING_PEND |
| 955 | ret ARX_RT_CLARIFY |
| 956 | end |
| 957 | if not owner |
| 958 | put arx_prop_route 0 ARX_RT_REFUSE |
| 959 | put arx_prop_why 0 ARX_RWHY_GUEST_SCOPE |
| 960 | ret ARX_RT_REFUSE |
| 961 | end |
| 962 | put arx_prop_route 0 ARX_RT_APPROVAL |
| 963 | if eq act ARX_ACT_APPROVE |
| 964 | put arx_prop_then 0 ARX_THEN_APPROVE |
| 965 | else |
| 966 | if eq act ARX_ACT_DENY |
| 967 | put arx_prop_then 0 ARX_THEN_DENY |
| 968 | else |
| 969 | put arx_prop_then 0 ARX_THEN_CANCEL |
| 970 | end |
| 971 | end |
| 972 | if eq and caps 2 0 |
| 973 | put arx_prop_not_ready 0 ARX_NR_DESK |
| 974 | end |
| 975 | ret ARX_RT_APPROVAL |
| 976 | end |
| 977 | if eq act ARX_ACT_CLARIFY |
| 978 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 979 | put arx_prop_why 0 ARX_RWHY_CORE_CLARIFY |
| 980 | ret ARX_RT_CLARIFY |
| 981 | end |
| 982 | put arx_prop_route 0 ARX_RT_REFUSE |
| 983 | ret ARX_RT_REFUSE |
| 984 | end |
| 985 | |
| 986 | fn arx_propose_attach_pst1 rt uchar msg view uchar * to void |
| 987 | subj = new uchar 32 0 |
| 988 | pst = new uchar 1024 0 |
| 989 | dig = new uchar 32 0 |
| 990 | if not or eq rt ARX_RT_SPARK_INF eq rt ARX_RT_SPARK_OM |
| 991 | ret |
| 992 | end |
| 993 | i = ulong 0 |
| 994 | while lt i 32 bound 32 |
| 995 | put subj i get arx_rctx_subj i |
| 996 | i = add i 1 |
| 997 | end |
| 998 | plen = arx_pst1_request rt msg subj pst |
| 999 | if gt plen 0 |
| 1000 | ign_d = sha2_digest 256 part pst 0 plen dig |
| 1001 | i = ulong 0 |
| 1002 | while lt i 32 bound 32 |
| 1003 | put arx_prop_pst1 i get dig i |
| 1004 | i = add i 1 |
| 1005 | end |
| 1006 | put arx_prop_has_pst1 0 1 |
| 1007 | end |
| 1008 | end |
| 1009 | |
| 1010 | fn arx_propose req_id ulong sess_id ulong to uchar |
| 1011 | msg = new uchar 512 0 |
| 1012 | obj = new uchar 32 0 |
| 1013 | arx_prop_reset req_id sess_id |
| 1014 | tn = get arx_turn_n 0 |
| 1015 | has_u = false |
| 1016 | last_u = ulong 0 |
| 1017 | i = ulong 0 |
| 1018 | while lt i tn bound 32 |
| 1019 | if eq get arx_turn_role i ARX_ROLE_USER |
| 1020 | has_u = true |
| 1021 | last_u = i |
| 1022 | end |
| 1023 | i = add i 1 |
| 1024 | end |
| 1025 | if not has_u |
| 1026 | ret ARX_RT_CLARIFY |
| 1027 | end |
| 1028 | raw_n = get arx_turn_raw_len last_u |
| 1029 | mlen = arx_load_turn_buf last_u msg |
| 1030 | arx_objective_id part msg 0 mlen obj |
| 1031 | i = ulong 0 |
| 1032 | while lt i 32 bound 32 |
| 1033 | put arx_prop_obj i get obj i |
| 1034 | i = add i 1 |
| 1035 | end |
| 1036 | if gt raw_n ARX_INTERACTIVE_MAX |
| 1037 | put arx_prop_route 0 ARX_RT_REFUSE |
| 1038 | put arx_prop_why 0 ARX_RWHY_TOO_LONG |
| 1039 | ret ARX_RT_REFUSE |
| 1040 | end |
| 1041 | i = ulong 0 |
| 1042 | while lt i mlen bound 512 |
| 1043 | b = get msg i |
| 1044 | if or lt b 0x20 gt b 0x7e |
| 1045 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 1046 | put arx_prop_why 0 ARX_RWHY_NON_ASCII |
| 1047 | ret ARX_RT_CLARIFY |
| 1048 | end |
| 1049 | i = add i 1 |
| 1050 | end |
| 1051 | act = arx_decide |
| 1052 | put arx_prop_label 0 act |
| 1053 | if ne get arx_dec_has_txt 0 0 |
| 1054 | put arx_prop_has_txt 0 1 |
| 1055 | tl = get arx_dec_txt_len 0 |
| 1056 | put arx_prop_tlen 0 tl |
| 1057 | i = ulong 0 |
| 1058 | while lt i tl bound 64 |
| 1059 | put arx_prop_txt i get arx_dec_txt i |
| 1060 | i = add i 1 |
| 1061 | end |
| 1062 | end |
| 1063 | ew = get arx_exp_why 0 |
| 1064 | if eq ew ARX_WHY_POLICY |
| 1065 | put arx_prop_route 0 ARX_RT_REFUSE |
| 1066 | put arx_prop_why 0 ARX_RWHY_POLICY |
| 1067 | put arx_prop_refuse 0 get arx_exp_refuse 0 |
| 1068 | put arx_prop_has_txt 0 0 |
| 1069 | ret ARX_RT_REFUSE |
| 1070 | end |
| 1071 | if eq ew ARX_WHY_NO_USER_TURN |
| 1072 | put arx_prop_why 0 ARX_RWHY_NO_USER |
| 1073 | ret ARX_RT_CLARIFY |
| 1074 | end |
| 1075 | if eq ew ARX_WHY_INPUT_TOO_LONG |
| 1076 | put arx_prop_route 0 ARX_RT_REFUSE |
| 1077 | put arx_prop_why 0 ARX_RWHY_TOO_LONG |
| 1078 | ret ARX_RT_REFUSE |
| 1079 | end |
| 1080 | if eq ew ARX_WHY_NO_WORDS |
| 1081 | put arx_prop_why 0 ARX_RWHY_NO_WORDS |
| 1082 | ret ARX_RT_CLARIFY |
| 1083 | end |
| 1084 | if eq ew ARX_WHY_SPAN |
| 1085 | put arx_prop_why 0 ARX_RWHY_MISSING_SUBJ |
| 1086 | ret ARX_RT_CLARIFY |
| 1087 | end |
| 1088 | if ne ew ARX_WHY_EXACT_RECALL |
| 1089 | if lt get arx_exp_best_sim 0 get arx_rctx_min_sim 0 |
| 1090 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 1091 | put arx_prop_why 0 ARX_RWHY_UNFAMILIAR |
| 1092 | put arx_prop_has_txt 0 0 |
| 1093 | ret ARX_RT_CLARIFY |
| 1094 | end |
| 1095 | if lt get arx_exp_margin 0 get arx_rctx_min_margin 0 |
| 1096 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 1097 | put arx_prop_why 0 ARX_RWHY_UNSURE |
| 1098 | put arx_prop_has_txt 0 0 |
| 1099 | ret ARX_RT_CLARIFY |
| 1100 | end |
| 1101 | end |
| 1102 | if eq ew ARX_WHY_EXACT_RECALL |
| 1103 | put arx_prop_why 0 ARX_RWHY_EXACT |
| 1104 | else |
| 1105 | put arx_prop_why 0 ARX_RWHY_SCORED |
| 1106 | end |
| 1107 | if arx_named_model_unavail part msg 0 mlen |
| 1108 | put arx_prop_route 0 ARX_RT_CLARIFY |
| 1109 | put arx_prop_why 0 ARX_RWHY_MODEL_UNAVAIL |
| 1110 | put arx_prop_has_txt 0 0 |
| 1111 | ret ARX_RT_CLARIFY |
| 1112 | end |
| 1113 | rt = arx_propose_dispatch act part msg 0 mlen |
| 1114 | if ne get arx_prop_not_ready 0 ARX_NR_NONE |
| 1115 | put arx_prop_fallback 0 ARX_RT_LOCAL_DET |
| 1116 | end |
| 1117 | arx_propose_attach_pst1 rt part msg 0 mlen |
| 1118 | ret rt |
| 1119 | end |
| 1120 | |
| 1121 | text arx_p_s1 "{\"req\":" |
| 1122 | text arx_p_s2 ",\"session\":" |
| 1123 | text arx_p_s3 ",\"speaker\":\"" |
| 1124 | text arx_p_s4 "\",\"objective\":\"" |
| 1125 | text arx_p_s5 "\",\"route\":\"" |
| 1126 | text arx_p_s6 "\",\"then\":\"" |
| 1127 | text arx_p_s7 "\",\"ready\":\"" |
| 1128 | text arx_p_s8 "\",\"fallback\":\"" |
| 1129 | text arx_p_s9 "\",\"why\":\"" |
| 1130 | text arx_p_s10 "\",\"label\":\"" |
| 1131 | text arx_p_s11 "\",\"text\":\"" |
| 1132 | text arx_p_s12 "\",\"pst1\":\"" |
| 1133 | text arx_p_s13 "\",\"grants\":\"none\"}" |
| 1134 | text arx_p_owner "owner" |
| 1135 | text arx_p_guest "guest" |
| 1136 | text arx_p_yes "yes" |
| 1137 | text arx_p_no "no:" |
| 1138 | text arx_p_dash "-" |
| 1139 | |
| 1140 | text arx_rt_n0 "local_deterministic" |
| 1141 | text arx_rt_n1 "memory" |
| 1142 | text arx_rt_n2 "clarify" |
| 1143 | text arx_rt_n3 "refuse" |
| 1144 | text arx_rt_n4 "local_model" |
| 1145 | text arx_rt_n5 "spark_inference" |
| 1146 | text arx_rt_n6 "spark_omegese" |
| 1147 | text arx_rt_n7 "approval_needed" |
| 1148 | |
| 1149 | text arx_th_n0 "approve" |
| 1150 | text arx_th_n1 "deny" |
| 1151 | text arx_th_n2 "cancel" |
| 1152 | text arx_th_n3 "remember" |
| 1153 | text arx_th_n4 "prefer" |
| 1154 | text arx_th_n5 "note_task" |
| 1155 | text arx_th_n6 "spark_task" |
| 1156 | |
| 1157 | text arx_nr_n0 "store_absent" |
| 1158 | text arx_nr_n1 "no_memory_consent" |
| 1159 | text arx_nr_n2 "owner_desk_absent" |
| 1160 | text arx_nr_n3 "no_local_model" |
| 1161 | text arx_nr_n4 "spark_link_down" |
| 1162 | |
| 1163 | text arx_rw_n0 "scored" |
| 1164 | text arx_rw_n1 "exact_recall" |
| 1165 | text arx_rw_n2 "policy" |
| 1166 | text arx_rw_n3 "no_user_turn" |
| 1167 | text arx_rw_n4 "input_too_long" |
| 1168 | text arx_rw_n5 "non_ascii" |
| 1169 | text arx_rw_n6 "no_words" |
| 1170 | text arx_rw_n7 "core_clarify" |
| 1171 | text arx_rw_n8 "missing_subject" |
| 1172 | text arx_rw_n9 "unfamiliar" |
| 1173 | text arx_rw_n10 "unsure" |
| 1174 | text arx_rw_n11 "nothing_pending" |
| 1175 | text arx_rw_n12 "model_unavailable" |
| 1176 | text arx_rw_n13 "guest_scope" |
| 1177 | text arx_rw_n14 "out_of_capability" |
| 1178 | text arx_rw_n15 "spark_cue" |
| 1179 | |
| 1180 | text arx_rr_n1 ":rule-break" |
| 1181 | text arx_rr_n2 ":override" |
| 1182 | text arx_rr_n3 ":forbidden-verb" |
| 1183 | text arx_rr_n4 ":blanket-approval" |
| 1184 | text arx_rr_n5 ":data-order" |
| 1185 | |
| 1186 | fn arx_push_u64 dst edit uchar * pos ulong val ulong to ulong |
| 1187 | buf = new uchar 20 0 |
| 1188 | v = val |
| 1189 | i = ulong 20 |
| 1190 | while gt i 0 bound 20 |
| 1191 | i = sub i 1 |
| 1192 | put buf i add uchar 48 uchar rem v 10 |
| 1193 | v = div v 10 |
| 1194 | if eq v 0 |
| 1195 | break |
| 1196 | end |
| 1197 | end |
| 1198 | p = pos |
| 1199 | while lt i 20 bound 20 |
| 1200 | put dst p get buf i |
| 1201 | p = add p 1 |
| 1202 | i = add i 1 |
| 1203 | end |
| 1204 | ret p |
| 1205 | end |
| 1206 | |
| 1207 | fn arx_push_hex32 dst edit uchar * pos ulong src view uchar 32 to ulong |
| 1208 | p = pos |
| 1209 | i = ulong 0 |
| 1210 | while lt i 32 bound 32 |
| 1211 | b = get src i |
| 1212 | put dst p get arx_hex_digits ulong shr b 4 |
| 1213 | put dst add p 1 get arx_hex_digits ulong and b 15 |
| 1214 | p = add p 2 |
| 1215 | i = add i 1 |
| 1216 | end |
| 1217 | ret p |
| 1218 | end |
| 1219 | |
| 1220 | fn arx_write_route_name r uchar dst edit uchar * pos ulong to ulong |
| 1221 | if eq r ARX_RT_LOCAL_DET |
| 1222 | ret arx_copy_bytes dst pos arx_rt_n0 |
| 1223 | end |
| 1224 | if eq r ARX_RT_MEMORY |
| 1225 | ret arx_copy_bytes dst pos arx_rt_n1 |
| 1226 | end |
| 1227 | if eq r ARX_RT_CLARIFY |
| 1228 | ret arx_copy_bytes dst pos arx_rt_n2 |
| 1229 | end |
| 1230 | if eq r ARX_RT_REFUSE |
| 1231 | ret arx_copy_bytes dst pos arx_rt_n3 |
| 1232 | end |
| 1233 | if eq r ARX_RT_LOCAL_MODEL |
| 1234 | ret arx_copy_bytes dst pos arx_rt_n4 |
| 1235 | end |
| 1236 | if eq r ARX_RT_SPARK_INF |
| 1237 | ret arx_copy_bytes dst pos arx_rt_n5 |
| 1238 | end |
| 1239 | if eq r ARX_RT_SPARK_OM |
| 1240 | ret arx_copy_bytes dst pos arx_rt_n6 |
| 1241 | end |
| 1242 | if eq r ARX_RT_APPROVAL |
| 1243 | ret arx_copy_bytes dst pos arx_rt_n7 |
| 1244 | end |
| 1245 | ret arx_copy_bytes dst pos arx_p_dash |
| 1246 | end |
| 1247 | |
| 1248 | fn arx_write_then_name t uchar dst edit uchar * pos ulong to ulong |
| 1249 | if eq t ARX_THEN_APPROVE |
| 1250 | ret arx_copy_bytes dst pos arx_th_n0 |
| 1251 | end |
| 1252 | if eq t ARX_THEN_DENY |
| 1253 | ret arx_copy_bytes dst pos arx_th_n1 |
| 1254 | end |
| 1255 | if eq t ARX_THEN_CANCEL |
| 1256 | ret arx_copy_bytes dst pos arx_th_n2 |
| 1257 | end |
| 1258 | if eq t ARX_THEN_REMEMBER |
| 1259 | ret arx_copy_bytes dst pos arx_th_n3 |
| 1260 | end |
| 1261 | if eq t ARX_THEN_PREFER |
| 1262 | ret arx_copy_bytes dst pos arx_th_n4 |
| 1263 | end |
| 1264 | if eq t ARX_THEN_NOTE_TASK |
| 1265 | ret arx_copy_bytes dst pos arx_th_n5 |
| 1266 | end |
| 1267 | if eq t ARX_THEN_SPARK_TASK |
| 1268 | ret arx_copy_bytes dst pos arx_th_n6 |
| 1269 | end |
| 1270 | ret arx_copy_bytes dst pos arx_p_dash |
| 1271 | end |
| 1272 | |
| 1273 | fn arx_write_rwhy_name w uchar dst edit uchar * pos ulong to ulong |
| 1274 | if eq w ARX_RWHY_SCORED |
| 1275 | ret arx_copy_bytes dst pos arx_rw_n0 |
| 1276 | end |
| 1277 | if eq w ARX_RWHY_EXACT |
| 1278 | ret arx_copy_bytes dst pos arx_rw_n1 |
| 1279 | end |
| 1280 | if eq w ARX_RWHY_POLICY |
| 1281 | p = arx_copy_bytes dst pos arx_rw_n2 |
| 1282 | rr = get arx_prop_refuse 0 |
| 1283 | if eq rr ARX_REFUSE_RULE_BREAK |
| 1284 | ret arx_copy_bytes dst p arx_rr_n1 |
| 1285 | end |
| 1286 | if eq rr ARX_REFUSE_OVERRIDE |
| 1287 | ret arx_copy_bytes dst p arx_rr_n2 |
| 1288 | end |
| 1289 | if eq rr ARX_REFUSE_FORBIDDEN |
| 1290 | ret arx_copy_bytes dst p arx_rr_n3 |
| 1291 | end |
| 1292 | if eq rr ARX_REFUSE_BLANKET |
| 1293 | ret arx_copy_bytes dst p arx_rr_n4 |
| 1294 | end |
| 1295 | if eq rr ARX_REFUSE_DATA_ORDER |
| 1296 | ret arx_copy_bytes dst p arx_rr_n5 |
| 1297 | end |
| 1298 | ret p |
| 1299 | end |
| 1300 | if eq w ARX_RWHY_NO_USER |
| 1301 | ret arx_copy_bytes dst pos arx_rw_n3 |
| 1302 | end |
| 1303 | if eq w ARX_RWHY_TOO_LONG |
| 1304 | ret arx_copy_bytes dst pos arx_rw_n4 |
| 1305 | end |
| 1306 | if eq w ARX_RWHY_NON_ASCII |
| 1307 | ret arx_copy_bytes dst pos arx_rw_n5 |
| 1308 | end |
| 1309 | if eq w ARX_RWHY_NO_WORDS |
| 1310 | ret arx_copy_bytes dst pos arx_rw_n6 |
| 1311 | end |
| 1312 | if eq w ARX_RWHY_CORE_CLARIFY |
| 1313 | ret arx_copy_bytes dst pos arx_rw_n7 |
| 1314 | end |
| 1315 | if eq w ARX_RWHY_MISSING_SUBJ |
| 1316 | ret arx_copy_bytes dst pos arx_rw_n8 |
| 1317 | end |
| 1318 | if eq w ARX_RWHY_UNFAMILIAR |
| 1319 | ret arx_copy_bytes dst pos arx_rw_n9 |
| 1320 | end |
| 1321 | if eq w ARX_RWHY_UNSURE |
| 1322 | ret arx_copy_bytes dst pos arx_rw_n10 |
| 1323 | end |
| 1324 | if eq w ARX_RWHY_NOTHING_PEND |
| 1325 | ret arx_copy_bytes dst pos arx_rw_n11 |
| 1326 | end |
| 1327 | if eq w ARX_RWHY_MODEL_UNAVAIL |
| 1328 | ret arx_copy_bytes dst pos arx_rw_n12 |
| 1329 | end |
| 1330 | if eq w ARX_RWHY_GUEST_SCOPE |
| 1331 | ret arx_copy_bytes dst pos arx_rw_n13 |
| 1332 | end |
| 1333 | if eq w ARX_RWHY_OUT_OF_CAP |
| 1334 | ret arx_copy_bytes dst pos arx_rw_n14 |
| 1335 | end |
| 1336 | ret arx_copy_bytes dst pos arx_rw_n15 |
| 1337 | end |
| 1338 | |
| 1339 | fn arx_proposal_json dst edit uchar * to ulong |
| 1340 | tmp32 = new uchar 32 0 |
| 1341 | p = ulong 0 |
| 1342 | p = arx_copy_bytes dst p arx_p_s1 |
| 1343 | p = arx_push_u64 dst p get arx_prop_req 0 |
| 1344 | p = arx_copy_bytes dst p arx_p_s2 |
| 1345 | p = arx_push_u64 dst p get arx_prop_sess 0 |
| 1346 | p = arx_copy_bytes dst p arx_p_s3 |
| 1347 | if eq get arx_prop_speaker 0 ARX_SPK_OWNER |
| 1348 | p = arx_copy_bytes dst p arx_p_owner |
| 1349 | else |
| 1350 | p = arx_copy_bytes dst p arx_p_guest |
| 1351 | end |
| 1352 | p = arx_copy_bytes dst p arx_p_s4 |
| 1353 | i = ulong 0 |
| 1354 | while lt i 32 bound 32 |
| 1355 | put tmp32 i get arx_prop_obj i |
| 1356 | i = add i 1 |
| 1357 | end |
| 1358 | p = arx_push_hex32 dst p tmp32 |
| 1359 | p = arx_copy_bytes dst p arx_p_s5 |
| 1360 | p = arx_write_route_name get arx_prop_route 0 dst p |
| 1361 | p = arx_copy_bytes dst p arx_p_s6 |
| 1362 | p = arx_write_then_name get arx_prop_then 0 dst p |
| 1363 | p = arx_copy_bytes dst p arx_p_s7 |
| 1364 | nr = get arx_prop_not_ready 0 |
| 1365 | if eq nr ARX_NR_NONE |
| 1366 | p = arx_copy_bytes dst p arx_p_yes |
| 1367 | else |
| 1368 | p = arx_copy_bytes dst p arx_p_no |
| 1369 | if eq nr ARX_NR_STORE |
| 1370 | p = arx_copy_bytes dst p arx_nr_n0 |
| 1371 | else |
| 1372 | if eq nr ARX_NR_NO_CONSENT |
| 1373 | p = arx_copy_bytes dst p arx_nr_n1 |
| 1374 | else |
| 1375 | if eq nr ARX_NR_DESK |
| 1376 | p = arx_copy_bytes dst p arx_nr_n2 |
| 1377 | else |
| 1378 | if eq nr ARX_NR_NO_MODEL |
| 1379 | p = arx_copy_bytes dst p arx_nr_n3 |
| 1380 | else |
| 1381 | p = arx_copy_bytes dst p arx_nr_n4 |
| 1382 | end |
| 1383 | end |
| 1384 | end |
| 1385 | end |
| 1386 | end |
| 1387 | p = arx_copy_bytes dst p arx_p_s8 |
| 1388 | p = arx_write_route_name get arx_prop_fallback 0 dst p |
| 1389 | p = arx_copy_bytes dst p arx_p_s9 |
| 1390 | p = arx_write_rwhy_name get arx_prop_why 0 dst p |
| 1391 | p = arx_copy_bytes dst p arx_p_s10 |
| 1392 | p = arx_act_write_name get arx_prop_label 0 dst p |
| 1393 | p = arx_copy_bytes dst p arx_p_s11 |
| 1394 | if ne get arx_prop_has_txt 0 0 |
| 1395 | tl = get arx_prop_tlen 0 |
| 1396 | i = ulong 0 |
| 1397 | while lt i tl bound 64 |
| 1398 | put dst add p i get arx_prop_txt i |
| 1399 | i = add i 1 |
| 1400 | end |
| 1401 | p = add p tl |
| 1402 | end |
| 1403 | p = arx_copy_bytes dst p arx_p_s12 |
| 1404 | if ne get arx_prop_has_pst1 0 0 |
| 1405 | i = ulong 0 |
| 1406 | while lt i 32 bound 32 |
| 1407 | put tmp32 i get arx_prop_pst1 i |
| 1408 | i = add i 1 |
| 1409 | end |
| 1410 | p = arx_push_hex32 dst p tmp32 |
| 1411 | else |
| 1412 | p = arx_copy_bytes dst p arx_p_dash |
| 1413 | end |
| 1414 | p = arx_copy_bytes dst p arx_p_s13 |
| 1415 | ret p |
| 1416 | end |
| 1417 | |
| 1418 | fn arx_hex_val c uchar to int |
| 1419 | if ge c 48 |
| 1420 | if le c 57 |
| 1421 | ret int sub c 48 |
| 1422 | end |
| 1423 | end |
| 1424 | if ge c 97 |
| 1425 | if le c 102 |
| 1426 | ret add 10 int sub c 97 |
| 1427 | end |
| 1428 | end |
| 1429 | if ge c 65 |
| 1430 | if le c 70 |
| 1431 | ret add 10 int sub c 65 |
| 1432 | end |
| 1433 | end |
| 1434 | ret -1 |
| 1435 | end |
| 1436 | |
| 1437 | fn arx_pct_decode src view uchar * dst edit uchar * to long |
| 1438 | n = len src |
| 1439 | cap = len dst |
| 1440 | i = ulong 0 |
| 1441 | j = ulong 0 |
| 1442 | while lt i n bound 4096 |
| 1443 | if ge j cap |
| 1444 | ret -1 |
| 1445 | end |
| 1446 | b = get src i |
| 1447 | if eq b 37 |
| 1448 | if ge add i 2 n |
| 1449 | ret -1 |
| 1450 | end |
| 1451 | hi = arx_hex_val get src add i 1 |
| 1452 | lo = arx_hex_val get src add i 2 |
| 1453 | if or lt hi 0 lt lo 0 |
| 1454 | ret -1 |
| 1455 | end |
| 1456 | put dst j uchar or shl hi 4 lo |
| 1457 | j = add j 1 |
| 1458 | i = add i 3 |
| 1459 | else |
| 1460 | put dst j b |
| 1461 | j = add j 1 |
| 1462 | i = add i 1 |
| 1463 | end |
| 1464 | end |
| 1465 | ret long j |
| 1466 | end |