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 / gpu / adapters / lib.om
| 1 | note gpu/adapters/lib.om: pure Omega hardware accelerator adapter layer & spark-adapters router/verifier/distill. |
| 2 | note Upstream source: spark-adapters/src/{models,router,verifier,distill,lib}.rs plus GB10/multi-vendor hardware table. |
| 3 | note Compose after si/directives/om2.om; no target line, no fn main. |
| 4 | |
| 5 | const AD_OK int 0 |
| 6 | const AD_ERR_NO_DEVICES int 1 |
| 7 | const AD_ERR_BAD_REQ int 2 |
| 8 | const AD_ERR_OFFLINE int 3 |
| 9 | const AD_ERR_VRAM_EXCEEDED int 4 |
| 10 | const AD_ERR_PRECISION int 5 |
| 11 | const AD_ERR_CC_LOW int 6 |
| 12 | |
| 13 | const VENDOR_ANY ulong 0 |
| 14 | const VENDOR_NVIDIA ulong 1 |
| 15 | const VENDOR_AMD ulong 2 |
| 16 | const VENDOR_APPLE ulong 3 |
| 17 | const VENDOR_CPU ulong 4 |
| 18 | |
| 19 | const ARCH_GB10_SM121 ulong 121 |
| 20 | const ARCH_H100_SM90 ulong 90 |
| 21 | const ARCH_RTX4090_SM89 ulong 89 |
| 22 | const ARCH_A100_SM80 ulong 80 |
| 23 | const ARCH_MI300X_GFX942 ulong 942 |
| 24 | const ARCH_M4_MAX ulong 400 |
| 25 | const ARCH_ARM64_CPU ulong 1 |
| 26 | |
| 27 | const CAP_FP16 ulong 0x01 |
| 28 | const CAP_BF16 ulong 0x02 |
| 29 | const CAP_FP8 ulong 0x04 |
| 30 | const CAP_INT8 ulong 0x08 |
| 31 | const CAP_TF32 ulong 0x10 |
| 32 | const CAP_NVFP4 ulong 0x20 |
| 33 | const CAP_UNIFIED ulong 0x40 |
| 34 | const CAP_ALL_MASK ulong 0x7f |
| 35 | |
| 36 | const AD_GIB ulong 1073741824 |
| 37 | const AD_MAX_DEV ulong 16 |
| 38 | |
| 39 | const PROV_OPENAI ulong 1 |
| 40 | const PROV_ANTHROPIC ulong 2 |
| 41 | const PROV_OPENROUTER ulong 3 |
| 42 | const PROV_GEMINI ulong 4 |
| 43 | const PROV_GROQ ulong 5 |
| 44 | const PROV_OLLAMA ulong 6 |
| 45 | const PROV_DEEPSEEK ulong 7 |
| 46 | const PROV_TOGETHER ulong 8 |
| 47 | const PROV_MISTRAL ulong 9 |
| 48 | const PROV_LOCAL_MAX ulong 10 |
| 49 | |
| 50 | const VERIF_COMPILER ulong 1 |
| 51 | const VERIF_JSON ulong 2 |
| 52 | const VERIF_UNSLOP_STRICT ulong 3 |
| 53 | const VERIF_DUAL_CONSENSUS ulong 4 |
| 54 | |
| 55 | const CHOSEN_TEACHER ulong 1 |
| 56 | const CHOSEN_STUDENT ulong 2 |
| 57 | |
| 58 | struct WorkloadReq min_vram ulong req_flags ulong min_cc_maj ulong min_cc_min ulong pref_vendor ulong pref_unified bool end |
| 59 | struct AdapterSel ok bool err int dev_id ulong score ulong checked ulong eligible ulong end |
| 60 | struct RouteRes provider ulong ctx_len ulong requires_key bool is_resident bool matched_catalog bool cat_idx ulong end |
| 61 | struct VerifRes passed bool critical bool score f64 violations ulong delim_ok bool end |
| 62 | struct DistillDec chosen ulong pref_delta f64 commit_cortex bool end |
| 63 | |
| 64 | global ad_obuf uchar 32768 |
| 65 | global ad_olen ulong 1 |
| 66 | |
| 67 | global ad_dev_cnt ulong 1 |
| 68 | global ad_dev_vendor ulong 16 |
| 69 | global ad_dev_arch ulong 16 |
| 70 | global ad_dev_vram ulong 16 |
| 71 | global ad_dev_cc_maj ulong 16 |
| 72 | global ad_dev_cc_min ulong 16 |
| 73 | global ad_dev_flags ulong 16 |
| 74 | global ad_dev_tflops ulong 16 |
| 75 | global ad_dev_bw ulong 16 |
| 76 | global ad_dev_online ulong 16 |
| 77 | |
| 78 | text ta_nl "\n" |
| 79 | text ta_sp " " |
| 80 | text ta_dot "." |
| 81 | text ta_minus "-" |
| 82 | text ta_v_nv "NVIDIA" |
| 83 | text ta_v_amd "AMD" |
| 84 | text ta_v_apl "APPLE" |
| 85 | text ta_v_cpu "CPU" |
| 86 | text ta_a_gb10 "Blackwell-GB10-SM121" |
| 87 | text ta_a_h100 "Hopper-H100-SM90" |
| 88 | text ta_a_a100 "Ampere-A100-SM80" |
| 89 | text ta_a_4090 "Ada-RTX4090-SM89" |
| 90 | text ta_a_mi300 "CDNA3-MI300X-GFX942" |
| 91 | text ta_a_m4 "Apple-M4-Max" |
| 92 | text ta_a_cpu "ARM64-CPU" |
| 93 | text ta_hdr "id vendor arch vram_gib cc flags tflops bw_gbps online\n" |
| 94 | text ta_sep "--------------------------------------------------------------------------\n" |
| 95 | text ta_sel_ok "SELECT ok=true dev_id=" |
| 96 | text ta_sel_sc " score=" |
| 97 | text ta_sel_el " eligible=" |
| 98 | text ta_sel_ch "/" |
| 99 | text ta_sel_no "SELECT ok=false refusal_code=" |
| 100 | text ta_f_fp16 "FP16" |
| 101 | text ta_f_bf16 "BF16" |
| 102 | text ta_f_fp8 "FP8" |
| 103 | text ta_f_int8 "INT8" |
| 104 | text ta_f_tf32 "TF32" |
| 105 | text ta_f_fp4 "NVFP4" |
| 106 | text ta_f_uni "UNIFIED" |
| 107 | text ta_comma "," |
| 108 | text ta_yes "true" |
| 109 | text ta_no "false" |
| 110 | |
| 111 | note upstream router catalog identifiers and prefixes |
| 112 | text tr_cat0 "atlas-lightning-omni" |
| 113 | text tr_cat1 "openai/gpt-4o" |
| 114 | text tr_cat1m "gpt-4o" |
| 115 | text tr_cat2 "openai/o3-mini" |
| 116 | text tr_cat2m "o3-mini" |
| 117 | text tr_cat3 "anthropic/claude-3-7-sonnet" |
| 118 | text tr_cat3m "claude-3-7-sonnet-20250219" |
| 119 | text tr_cat4 "openrouter/auto" |
| 120 | text tr_cat5 "gemini/gemini-2.5-flash" |
| 121 | text tr_cat5m "gemini-2.5-flash" |
| 122 | text tr_cat6 "groq/llama-3.3-70b-versatile" |
| 123 | text tr_cat6m "llama-3.3-70b-versatile" |
| 124 | text tr_cat7 "ollama/qwen2.5-coder" |
| 125 | text tr_cat7m "qwen2.5-coder:7b" |
| 126 | text tr_cat8 "deepseek/deepseek-chat" |
| 127 | text tr_cat8m "deepseek-chat" |
| 128 | |
| 129 | text tr_p_openai "openai/" |
| 130 | text tr_p_anth "anthropic/" |
| 131 | text tr_p_or "openrouter/" |
| 132 | text tr_p_gem "gemini/" |
| 133 | text tr_p_groq "groq/" |
| 134 | text tr_p_oll "ollama/" |
| 135 | text tr_p_ds "deepseek/" |
| 136 | |
| 137 | note upstream verifier banned words, sycophancy phrases, secret signatures |
| 138 | text tv_b0 "delve" |
| 139 | text tv_b1 "tapestry" |
| 140 | text tv_b2 "crucial" |
| 141 | text tv_b3 "beacon" |
| 142 | text tv_b4 "game-changer" |
| 143 | text tv_b5 "unleash" |
| 144 | text tv_b6 "seamlessly" |
| 145 | text tv_b7 "elevate" |
| 146 | text tv_b8 "pivotal" |
| 147 | |
| 148 | text tv_s0 "certainly!" |
| 149 | text tv_s1 "great question!" |
| 150 | text tv_s2 "sure thing!" |
| 151 | text tv_s3 "i would be happy to help" |
| 152 | text tv_s4 "as an ai language model" |
| 153 | |
| 154 | text tv_k0 "-----BEGIN PRIVATE KEY-----" |
| 155 | text tv_k1 "-----BEGIN OPENSSH PRIVATE KEY-----" |
| 156 | text tv_k2 "sk-proj-" |
| 157 | text tv_k3 "ghp_" |
| 158 | |
| 159 | note ---- buffered stdout writer |
| 160 | |
| 161 | fn ad_ob_reset to void |
| 162 | put ad_olen 0 0 |
| 163 | end |
| 164 | |
| 165 | fn ad_ob_byte c uchar to void |
| 166 | n = get ad_olen 0 |
| 167 | if lt n len ad_obuf |
| 168 | put ad_obuf n c |
| 169 | put ad_olen 0 add n 1 |
| 170 | end |
| 171 | end |
| 172 | |
| 173 | fn ad_ob_str s view uchar * to void |
| 174 | for i ulong len s |
| 175 | ad_ob_byte get s i |
| 176 | end |
| 177 | end |
| 178 | |
| 179 | fn ad_ob_u64 v ulong to void |
| 180 | d = new uchar 24 0 |
| 181 | pos = ulong 24 |
| 182 | x = v |
| 183 | for it ulong 24 |
| 184 | pos = sub pos 1 |
| 185 | put d pos uchar add rem x 10 48 |
| 186 | x = div x 10 |
| 187 | if eq x 0 |
| 188 | break |
| 189 | end |
| 190 | end |
| 191 | for k ulong sub 24 pos |
| 192 | ad_ob_byte get d add pos k |
| 193 | end |
| 194 | end |
| 195 | |
| 196 | fn ad_ob_i64 v long to void |
| 197 | if lt v 0 |
| 198 | ad_ob_str ta_minus |
| 199 | ad_ob_u64 ulong neg v |
| 200 | else |
| 201 | ad_ob_u64 ulong v |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | fn ad_ob_flush to void |
| 206 | total = get ad_olen 0 |
| 207 | off = ulong 0 |
| 208 | note each iteration writes at least 1 byte or aborts, bounded by buffer size 32768 |
| 209 | while lt off total bound 32768 |
| 210 | k = write 1 ad_obuf off sub total off |
| 211 | if le k 0 |
| 212 | break |
| 213 | end |
| 214 | off = add off ulong k |
| 215 | end |
| 216 | put ad_olen 0 0 |
| 217 | end |
| 218 | |
| 219 | note ---- hardware device capability table |
| 220 | |
| 221 | fn adapter_reset_devices to void |
| 222 | put ad_dev_cnt 0 0 |
| 223 | end |
| 224 | |
| 225 | fn adapter_add_device vendor ulong arch ulong vram_gib ulong cc_maj ulong cc_min ulong flags ulong to int |
| 226 | cnt = get ad_dev_cnt 0 |
| 227 | if ge cnt AD_MAX_DEV |
| 228 | ret AD_ERR_BAD_REQ |
| 229 | end |
| 230 | put ad_dev_vendor cnt vendor |
| 231 | put ad_dev_arch cnt arch |
| 232 | put ad_dev_vram cnt mul vram_gib AD_GIB |
| 233 | put ad_dev_cc_maj cnt cc_maj |
| 234 | put ad_dev_cc_min cnt cc_min |
| 235 | put ad_dev_flags cnt flags |
| 236 | put ad_dev_tflops cnt 100 |
| 237 | put ad_dev_bw cnt 500 |
| 238 | put ad_dev_online cnt 1 |
| 239 | put ad_dev_cnt 0 add cnt 1 |
| 240 | ret AD_OK |
| 241 | end |
| 242 | |
| 243 | fn adapter_set_perf dev_id ulong tflops ulong bw_gbps ulong online bool to void |
| 244 | if lt dev_id get ad_dev_cnt 0 |
| 245 | put ad_dev_tflops dev_id tflops |
| 246 | put ad_dev_bw dev_id bw_gbps |
| 247 | if online |
| 248 | put ad_dev_online dev_id 1 |
| 249 | else |
| 250 | put ad_dev_online dev_id 0 |
| 251 | end |
| 252 | end |
| 253 | end |
| 254 | |
| 255 | fn adapter_init_default_devices to void |
| 256 | adapter_reset_devices |
| 257 | note 0: NVIDIA DGX Spark GB10 (SM 12.1, 128 GiB unified LPDDR5X, FP16/BF16/FP8/INT8/TF32/NVFP4/UNIFIED) |
| 258 | r0 = adapter_add_device VENDOR_NVIDIA ARCH_GB10_SM121 128 12 1 0x7f |
| 259 | adapter_set_perf 0 250 273 true |
| 260 | note 1: NVIDIA H100 SXM5 (SM 9.0, 80 GiB HBM3, FP16/BF16/FP8/INT8/TF32) |
| 261 | r1 = adapter_add_device VENDOR_NVIDIA ARCH_H100_SM90 80 9 0 0x1f |
| 262 | adapter_set_perf 1 989 3350 true |
| 263 | note 2: NVIDIA A100 SXM4 (SM 8.0, 40 GiB HBM2e, FP16/BF16/INT8/TF32, no FP8) |
| 264 | r2 = adapter_add_device VENDOR_NVIDIA ARCH_A100_SM80 40 8 0 0x1b |
| 265 | adapter_set_perf 2 312 1555 true |
| 266 | note 3: NVIDIA RTX 4090 (SM 8.9, 24 GiB GDDR6X, FP16/BF16/FP8/INT8/TF32) |
| 267 | r3 = adapter_add_device VENDOR_NVIDIA ARCH_RTX4090_SM89 24 8 9 0x1f |
| 268 | adapter_set_perf 3 165 1008 true |
| 269 | note 4: AMD Instinct MI300X (CDNA3 9.4, 192 GiB HBM3, FP16/BF16/FP8/INT8, no TF32) |
| 270 | r4 = adapter_add_device VENDOR_AMD ARCH_MI300X_GFX942 192 9 4 0x0f |
| 271 | adapter_set_perf 4 1307 5300 true |
| 272 | note 5: Apple M4 Max (64 GiB unified, FP16/BF16/INT8/UNIFIED, no FP8/TF32) |
| 273 | r5 = adapter_add_device VENDOR_APPLE ARCH_M4_MAX 64 4 0 0x4b |
| 274 | adapter_set_perf 5 54 546 true |
| 275 | sum_rc = add r0 add r1 add r2 add r3 add r4 r5 |
| 276 | if ne sum_rc 0 |
| 277 | ret |
| 278 | end |
| 279 | end |
| 280 | |
| 281 | note ---- capability checking, strict refusal, and deterministic scoring |
| 282 | |
| 283 | fn adapter_cc_packed maj ulong mn ulong to ulong |
| 284 | ret add mul maj 10 mn |
| 285 | end |
| 286 | |
| 287 | fn adapter_check_device dev_id ulong req view WorkloadReq to int |
| 288 | if ge dev_id get ad_dev_cnt 0 |
| 289 | ret AD_ERR_NO_DEVICES |
| 290 | end |
| 291 | if eq get ad_dev_online dev_id 0 |
| 292 | ret AD_ERR_OFFLINE |
| 293 | end |
| 294 | if lt get ad_dev_vram dev_id get req min_vram |
| 295 | ret AD_ERR_VRAM_EXCEEDED |
| 296 | end |
| 297 | req_f = get req req_flags |
| 298 | dev_f = get ad_dev_flags dev_id |
| 299 | if ne and dev_f req_f req_f |
| 300 | ret AD_ERR_PRECISION |
| 301 | end |
| 302 | dev_cc = adapter_cc_packed get ad_dev_cc_maj dev_id get ad_dev_cc_min dev_id |
| 303 | req_cc = adapter_cc_packed get req min_cc_maj get req min_cc_min |
| 304 | if lt dev_cc req_cc |
| 305 | ret AD_ERR_CC_LOW |
| 306 | end |
| 307 | ret AD_OK |
| 308 | end |
| 309 | |
| 310 | fn adapter_score_device dev_id ulong req view WorkloadReq to ulong |
| 311 | sc = mul get ad_dev_tflops dev_id 10 |
| 312 | sc = add sc mul get ad_dev_bw dev_id 2 |
| 313 | vram_gib = div get ad_dev_vram dev_id AD_GIB |
| 314 | if gt vram_gib 256 |
| 315 | vram_gib = 256 |
| 316 | end |
| 317 | sc = add sc mul vram_gib 5 |
| 318 | pv = get req pref_vendor |
| 319 | if and ne pv VENDOR_ANY eq get ad_dev_vendor dev_id pv |
| 320 | sc = add sc 50000 |
| 321 | end |
| 322 | if get req pref_unified |
| 323 | if ne and get ad_dev_flags dev_id CAP_UNIFIED 0 |
| 324 | sc = add sc 3000 |
| 325 | end |
| 326 | end |
| 327 | ret sc |
| 328 | end |
| 329 | |
| 330 | fn adapter_refusal_priority cur int cand int to int |
| 331 | if eq cur AD_OK |
| 332 | ret cand |
| 333 | end |
| 334 | note Prefer precision or VRAM refusal over generic offline when multiple devices fail |
| 335 | if eq cand AD_ERR_PRECISION |
| 336 | ret cand |
| 337 | end |
| 338 | if and eq cand AD_ERR_VRAM_EXCEEDED ne cur AD_ERR_PRECISION |
| 339 | ret cand |
| 340 | end |
| 341 | if and eq cand AD_ERR_CC_LOW eq cur AD_ERR_OFFLINE |
| 342 | ret cand |
| 343 | end |
| 344 | ret cur |
| 345 | end |
| 346 | |
| 347 | fn adapter_select req view WorkloadReq sel edit AdapterSel to int |
| 348 | put sel ok false |
| 349 | put sel err AD_OK |
| 350 | put sel dev_id 0 |
| 351 | put sel score 0 |
| 352 | put sel checked 0 |
| 353 | put sel eligible 0 |
| 354 | cnt = get ad_dev_cnt 0 |
| 355 | if eq cnt 0 |
| 356 | put sel err AD_ERR_NO_DEVICES |
| 357 | ret AD_ERR_NO_DEVICES |
| 358 | end |
| 359 | if or eq get req min_vram 0 ne and get req req_flags xor CAP_ALL_MASK 0xffffffffffffffff 0 |
| 360 | put sel err AD_ERR_BAD_REQ |
| 361 | ret AD_ERR_BAD_REQ |
| 362 | end |
| 363 | best_ref = AD_OK |
| 364 | for i ulong cnt bound 16 |
| 365 | put sel checked add get sel checked 1 |
| 366 | chk = adapter_check_device i req |
| 367 | if eq chk AD_OK |
| 368 | sc = adapter_score_device i req |
| 369 | put sel eligible add get sel eligible 1 |
| 370 | if or not get sel ok gt sc get sel score |
| 371 | put sel ok true |
| 372 | put sel dev_id i |
| 373 | put sel score sc |
| 374 | end |
| 375 | else |
| 376 | best_ref = adapter_refusal_priority best_ref chk |
| 377 | end |
| 378 | end |
| 379 | if not get sel ok |
| 380 | put sel err best_ref |
| 381 | ret best_ref |
| 382 | end |
| 383 | ret AD_OK |
| 384 | end |
| 385 | |
| 386 | note ---- deterministic hardware probe and selection output |
| 387 | |
| 388 | fn ad_ob_vendor v ulong to void |
| 389 | if eq v VENDOR_NVIDIA |
| 390 | ad_ob_str ta_v_nv |
| 391 | ret |
| 392 | end |
| 393 | if eq v VENDOR_AMD |
| 394 | ad_ob_str ta_v_amd |
| 395 | ret |
| 396 | end |
| 397 | if eq v VENDOR_APPLE |
| 398 | ad_ob_str ta_v_apl |
| 399 | ret |
| 400 | end |
| 401 | ad_ob_str ta_v_cpu |
| 402 | end |
| 403 | |
| 404 | fn ad_ob_arch a ulong to void |
| 405 | if eq a ARCH_GB10_SM121 |
| 406 | ad_ob_str ta_a_gb10 |
| 407 | ret |
| 408 | end |
| 409 | if eq a ARCH_H100_SM90 |
| 410 | ad_ob_str ta_a_h100 |
| 411 | ret |
| 412 | end |
| 413 | if eq a ARCH_A100_SM80 |
| 414 | ad_ob_str ta_a_a100 |
| 415 | ret |
| 416 | end |
| 417 | if eq a ARCH_RTX4090_SM89 |
| 418 | ad_ob_str ta_a_4090 |
| 419 | ret |
| 420 | end |
| 421 | if eq a ARCH_MI300X_GFX942 |
| 422 | ad_ob_str ta_a_mi300 |
| 423 | ret |
| 424 | end |
| 425 | if eq a ARCH_M4_MAX |
| 426 | ad_ob_str ta_a_m4 |
| 427 | ret |
| 428 | end |
| 429 | ad_ob_str ta_a_cpu |
| 430 | end |
| 431 | |
| 432 | fn adapter_print_probe to void |
| 433 | cnt = get ad_dev_cnt 0 |
| 434 | ad_ob_reset |
| 435 | ad_ob_str ta_hdr |
| 436 | ad_ob_str ta_sep |
| 437 | for i ulong cnt bound 16 |
| 438 | ad_ob_u64 i |
| 439 | ad_ob_str ta_sp |
| 440 | ad_ob_vendor get ad_dev_vendor i |
| 441 | ad_ob_str ta_sp |
| 442 | ad_ob_arch get ad_dev_arch i |
| 443 | ad_ob_str ta_sp |
| 444 | ad_ob_u64 div get ad_dev_vram i AD_GIB |
| 445 | ad_ob_str ta_sp |
| 446 | ad_ob_u64 get ad_dev_cc_maj i |
| 447 | ad_ob_str ta_dot |
| 448 | ad_ob_u64 get ad_dev_cc_min i |
| 449 | ad_ob_str ta_sp |
| 450 | ad_ob_u64 get ad_dev_flags i |
| 451 | ad_ob_str ta_sp |
| 452 | ad_ob_u64 get ad_dev_tflops i |
| 453 | ad_ob_str ta_sp |
| 454 | ad_ob_u64 get ad_dev_bw i |
| 455 | ad_ob_str ta_sp |
| 456 | if ne get ad_dev_online i 0 |
| 457 | ad_ob_str ta_yes |
| 458 | else |
| 459 | ad_ob_str ta_no |
| 460 | end |
| 461 | ad_ob_str ta_nl |
| 462 | end |
| 463 | ad_ob_flush |
| 464 | end |
| 465 | |
| 466 | fn adapter_print_selection sel view AdapterSel to void |
| 467 | ad_ob_reset |
| 468 | if get sel ok |
| 469 | ad_ob_str ta_sel_ok |
| 470 | ad_ob_u64 get sel dev_id |
| 471 | ad_ob_str ta_sel_sc |
| 472 | ad_ob_u64 get sel score |
| 473 | ad_ob_str ta_sel_el |
| 474 | ad_ob_u64 get sel eligible |
| 475 | ad_ob_str ta_sel_ch |
| 476 | ad_ob_u64 get sel checked |
| 477 | ad_ob_str ta_nl |
| 478 | else |
| 479 | ad_ob_str ta_sel_no |
| 480 | ad_ob_i64 long get sel err |
| 481 | ad_ob_str ta_nl |
| 482 | end |
| 483 | ad_ob_flush |
| 484 | end |
| 485 | |
| 486 | note ---- upstream spark-adapters model provider router (src/router.rs) |
| 487 | |
| 488 | fn ad_low c uchar to uchar |
| 489 | if and ge c 65 le c 90 |
| 490 | ret uchar add c 32 |
| 491 | end |
| 492 | ret c |
| 493 | end |
| 494 | |
| 495 | fn ad_str_eq a view uchar * b view uchar * to bool |
| 496 | if ne len a len b |
| 497 | ret false |
| 498 | end |
| 499 | for i ulong len a bound 512 |
| 500 | if ne get a i get b i |
| 501 | ret false |
| 502 | end |
| 503 | end |
| 504 | ret true |
| 505 | end |
| 506 | |
| 507 | fn ad_str_starts a view uchar * pre view uchar * to bool |
| 508 | if lt len a len pre |
| 509 | ret false |
| 510 | end |
| 511 | for i ulong len pre bound 128 |
| 512 | if ne get a i get pre i |
| 513 | ret false |
| 514 | end |
| 515 | end |
| 516 | ret true |
| 517 | end |
| 518 | |
| 519 | fn ad_contains_exact a view uchar * pat view uchar * to bool |
| 520 | pl = len pat |
| 521 | al = len a |
| 522 | if or eq pl 0 lt al pl |
| 523 | ret false |
| 524 | end |
| 525 | lim = add sub al pl 1 |
| 526 | for i ulong lim bound 4096 |
| 527 | ok = true |
| 528 | for j ulong pl bound 128 |
| 529 | if ne get a add i j get pat j |
| 530 | ok = false |
| 531 | break |
| 532 | end |
| 533 | end |
| 534 | if ok |
| 535 | ret true |
| 536 | end |
| 537 | end |
| 538 | ret false |
| 539 | end |
| 540 | |
| 541 | fn ad_contains_ci a view uchar * pat view uchar * to bool |
| 542 | pl = len pat |
| 543 | al = len a |
| 544 | if or eq pl 0 lt al pl |
| 545 | ret false |
| 546 | end |
| 547 | lim = add sub al pl 1 |
| 548 | for i ulong lim bound 4096 |
| 549 | ok = true |
| 550 | for j ulong pl bound 128 |
| 551 | if ne ad_low get a add i j ad_low get pat j |
| 552 | ok = false |
| 553 | break |
| 554 | end |
| 555 | end |
| 556 | if ok |
| 557 | ret true |
| 558 | end |
| 559 | end |
| 560 | ret false |
| 561 | end |
| 562 | |
| 563 | fn ad_set_route res edit RouteRes prov ulong ctx ulong rkey bool isres bool cat ulong to void |
| 564 | put res provider prov |
| 565 | put res ctx_len ctx |
| 566 | put res requires_key rkey |
| 567 | put res is_resident isres |
| 568 | put res matched_catalog true |
| 569 | put res cat_idx cat |
| 570 | end |
| 571 | |
| 572 | fn adapter_resolve_catalog q view uchar * res edit RouteRes to bool |
| 573 | if ad_str_eq q tr_cat0 |
| 574 | ad_set_route res PROV_LOCAL_MAX 32768 false true 0 |
| 575 | ret true |
| 576 | end |
| 577 | if or ad_str_eq q tr_cat1 ad_str_eq q tr_cat1m |
| 578 | ad_set_route res PROV_OPENAI 128000 true false 1 |
| 579 | ret true |
| 580 | end |
| 581 | if or ad_str_eq q tr_cat2 ad_str_eq q tr_cat2m |
| 582 | ad_set_route res PROV_OPENAI 200000 true false 2 |
| 583 | ret true |
| 584 | end |
| 585 | if or ad_str_eq q tr_cat3 ad_str_eq q tr_cat3m |
| 586 | ad_set_route res PROV_ANTHROPIC 200000 true false 3 |
| 587 | ret true |
| 588 | end |
| 589 | if ad_str_eq q tr_cat4 |
| 590 | ad_set_route res PROV_OPENROUTER 128000 true false 4 |
| 591 | ret true |
| 592 | end |
| 593 | if or ad_str_eq q tr_cat5 ad_str_eq q tr_cat5m |
| 594 | ad_set_route res PROV_GEMINI 1000000 true false 5 |
| 595 | ret true |
| 596 | end |
| 597 | if or ad_str_eq q tr_cat6 ad_str_eq q tr_cat6m |
| 598 | ad_set_route res PROV_GROQ 128000 true false 6 |
| 599 | ret true |
| 600 | end |
| 601 | if or ad_str_eq q tr_cat7 ad_str_eq q tr_cat7m |
| 602 | ad_set_route res PROV_OLLAMA 32768 false false 7 |
| 603 | ret true |
| 604 | end |
| 605 | if or ad_str_eq q tr_cat8 ad_str_eq q tr_cat8m |
| 606 | ad_set_route res PROV_DEEPSEEK 64000 true false 8 |
| 607 | ret true |
| 608 | end |
| 609 | ret false |
| 610 | end |
| 611 | |
| 612 | fn adapter_resolve_route q view uchar * res edit RouteRes to void |
| 613 | if adapter_resolve_catalog q res |
| 614 | ret |
| 615 | end |
| 616 | put res matched_catalog false |
| 617 | put res cat_idx 0 |
| 618 | if ad_str_starts q tr_p_openai |
| 619 | put res provider PROV_OPENAI |
| 620 | put res ctx_len 128000 |
| 621 | put res requires_key true |
| 622 | put res is_resident false |
| 623 | ret |
| 624 | end |
| 625 | if ad_str_starts q tr_p_anth |
| 626 | put res provider PROV_ANTHROPIC |
| 627 | put res ctx_len 200000 |
| 628 | put res requires_key true |
| 629 | put res is_resident false |
| 630 | ret |
| 631 | end |
| 632 | if ad_str_starts q tr_p_or |
| 633 | put res provider PROV_OPENROUTER |
| 634 | put res ctx_len 128000 |
| 635 | put res requires_key true |
| 636 | put res is_resident false |
| 637 | ret |
| 638 | end |
| 639 | if ad_str_starts q tr_p_gem |
| 640 | put res provider PROV_GEMINI |
| 641 | put res ctx_len 1000000 |
| 642 | put res requires_key true |
| 643 | put res is_resident false |
| 644 | ret |
| 645 | end |
| 646 | if ad_str_starts q tr_p_groq |
| 647 | put res provider PROV_GROQ |
| 648 | put res ctx_len 128000 |
| 649 | put res requires_key true |
| 650 | put res is_resident false |
| 651 | ret |
| 652 | end |
| 653 | if ad_str_starts q tr_p_oll |
| 654 | put res provider PROV_OLLAMA |
| 655 | put res ctx_len 32768 |
| 656 | put res requires_key false |
| 657 | put res is_resident false |
| 658 | ret |
| 659 | end |
| 660 | if ad_str_starts q tr_p_ds |
| 661 | put res provider PROV_DEEPSEEK |
| 662 | put res ctx_len 64000 |
| 663 | put res requires_key true |
| 664 | put res is_resident false |
| 665 | ret |
| 666 | end |
| 667 | note Fallback to local resident GB10 Modular MAX seat |
| 668 | put res provider PROV_LOCAL_MAX |
| 669 | put res ctx_len 32768 |
| 670 | put res requires_key false |
| 671 | put res is_resident true |
| 672 | end |
| 673 | |
| 674 | note ---- upstream sovereign verifier (src/verifier.rs) |
| 675 | |
| 676 | fn ad_has_utf8_dash buf view uchar * b2 uchar to bool |
| 677 | n = len buf |
| 678 | if lt n 3 |
| 679 | ret false |
| 680 | end |
| 681 | lim = sub n 2 |
| 682 | for i ulong lim bound 4096 |
| 683 | if eq get buf i 0xe2 |
| 684 | if eq get buf add i 1 0x80 |
| 685 | if eq get buf add i 2 b2 |
| 686 | ret true |
| 687 | end |
| 688 | end |
| 689 | end |
| 690 | end |
| 691 | ret false |
| 692 | end |
| 693 | |
| 694 | fn ad_check_delimiters buf view uchar * to bool |
| 695 | stk = new uchar 256 0 |
| 696 | sp = ulong 0 |
| 697 | in_str = false |
| 698 | esc = false |
| 699 | for i ulong len buf bound 4096 |
| 700 | c = get buf i |
| 701 | if esc |
| 702 | esc = false |
| 703 | continue |
| 704 | end |
| 705 | if eq c 92 |
| 706 | esc = true |
| 707 | continue |
| 708 | end |
| 709 | if eq c 34 |
| 710 | in_str = not in_str |
| 711 | continue |
| 712 | end |
| 713 | if in_str |
| 714 | continue |
| 715 | end |
| 716 | if or eq c 40 or eq c 91 eq c 123 |
| 717 | if ge sp 256 |
| 718 | ret false |
| 719 | end |
| 720 | put stk sp c |
| 721 | sp = add sp 1 |
| 722 | end |
| 723 | if eq c 41 |
| 724 | if eq sp 0 |
| 725 | ret false |
| 726 | end |
| 727 | sp = sub sp 1 |
| 728 | if ne get stk sp 40 |
| 729 | ret false |
| 730 | end |
| 731 | end |
| 732 | if eq c 93 |
| 733 | if eq sp 0 |
| 734 | ret false |
| 735 | end |
| 736 | sp = sub sp 1 |
| 737 | if ne get stk sp 91 |
| 738 | ret false |
| 739 | end |
| 740 | end |
| 741 | if eq c 125 |
| 742 | if eq sp 0 |
| 743 | ret false |
| 744 | end |
| 745 | sp = sub sp 1 |
| 746 | if ne get stk sp 123 |
| 747 | ret false |
| 748 | end |
| 749 | end |
| 750 | end |
| 751 | ret eq sp 0 |
| 752 | end |
| 753 | |
| 754 | fn ad_check_json_braces buf view uchar * to bool |
| 755 | first_l = long -1 |
| 756 | last_r = long -1 |
| 757 | for i ulong len buf bound 4096 |
| 758 | c = get buf i |
| 759 | if and eq c 123 lt first_l 0 |
| 760 | first_l = long i |
| 761 | end |
| 762 | if eq c 125 |
| 763 | last_r = long i |
| 764 | end |
| 765 | end |
| 766 | if lt first_l 0 |
| 767 | ret true |
| 768 | end |
| 769 | if lt last_r first_l |
| 770 | ret false |
| 771 | end |
| 772 | ret ad_check_delimiters buf |
| 773 | end |
| 774 | |
| 775 | fn ad_count_cliches buf view uchar * to ulong |
| 776 | hits = ulong 0 |
| 777 | if ad_contains_ci buf tv_b0 |
| 778 | hits = add hits 1 |
| 779 | end |
| 780 | if ad_contains_ci buf tv_b1 |
| 781 | hits = add hits 1 |
| 782 | end |
| 783 | if ad_contains_ci buf tv_b2 |
| 784 | hits = add hits 1 |
| 785 | end |
| 786 | if ad_contains_ci buf tv_b3 |
| 787 | hits = add hits 1 |
| 788 | end |
| 789 | if ad_contains_ci buf tv_b4 |
| 790 | hits = add hits 1 |
| 791 | end |
| 792 | if ad_contains_ci buf tv_b5 |
| 793 | hits = add hits 1 |
| 794 | end |
| 795 | if ad_contains_ci buf tv_b6 |
| 796 | hits = add hits 1 |
| 797 | end |
| 798 | if ad_contains_ci buf tv_b7 |
| 799 | hits = add hits 1 |
| 800 | end |
| 801 | if ad_contains_ci buf tv_b8 |
| 802 | hits = add hits 1 |
| 803 | end |
| 804 | ret hits |
| 805 | end |
| 806 | |
| 807 | fn ad_count_sycophancy buf view uchar * to ulong |
| 808 | hits = ulong 0 |
| 809 | if ad_contains_ci buf tv_s0 |
| 810 | hits = add hits 1 |
| 811 | end |
| 812 | if ad_contains_ci buf tv_s1 |
| 813 | hits = add hits 1 |
| 814 | end |
| 815 | if ad_contains_ci buf tv_s2 |
| 816 | hits = add hits 1 |
| 817 | end |
| 818 | if ad_contains_ci buf tv_s3 |
| 819 | hits = add hits 1 |
| 820 | end |
| 821 | if ad_contains_ci buf tv_s4 |
| 822 | hits = add hits 1 |
| 823 | end |
| 824 | ret hits |
| 825 | end |
| 826 | |
| 827 | fn ad_count_secrets buf view uchar * to ulong |
| 828 | hits = ulong 0 |
| 829 | if ad_contains_exact buf tv_k0 |
| 830 | hits = add hits 1 |
| 831 | end |
| 832 | if ad_contains_exact buf tv_k1 |
| 833 | hits = add hits 1 |
| 834 | end |
| 835 | if ad_contains_exact buf tv_k2 |
| 836 | hits = add hits 1 |
| 837 | end |
| 838 | if ad_contains_exact buf tv_k3 |
| 839 | hits = add hits 1 |
| 840 | end |
| 841 | ret hits |
| 842 | end |
| 843 | |
| 844 | fn adapter_verify buf view uchar * strategy ulong res edit VerifRes to void |
| 845 | score = f64 1.0f64 |
| 846 | viol = ulong 0 |
| 847 | crit = false |
| 848 | dok = true |
| 849 | if ad_has_utf8_dash buf 0x94 |
| 850 | viol = add viol 1 |
| 851 | score = sub score 0.25f64 |
| 852 | end |
| 853 | if ad_has_utf8_dash buf 0x93 |
| 854 | viol = add viol 1 |
| 855 | score = sub score 0.15f64 |
| 856 | end |
| 857 | hits = ad_count_cliches buf |
| 858 | if gt hits 0 |
| 859 | viol = add viol hits |
| 860 | score = sub score mul f64 hits 0.15f64 |
| 861 | end |
| 862 | hits = ad_count_sycophancy buf |
| 863 | if gt hits 0 |
| 864 | viol = add viol hits |
| 865 | score = sub score mul f64 hits 0.20f64 |
| 866 | end |
| 867 | hits = ad_count_secrets buf |
| 868 | if gt hits 0 |
| 869 | viol = add viol hits |
| 870 | crit = true |
| 871 | score = 0.0f64 |
| 872 | end |
| 873 | if eq strategy VERIF_COMPILER |
| 874 | dok = ad_check_delimiters buf |
| 875 | if not dok |
| 876 | viol = add viol 1 |
| 877 | score = sub score 0.30f64 |
| 878 | end |
| 879 | end |
| 880 | if eq strategy VERIF_JSON |
| 881 | dok = ad_check_json_braces buf |
| 882 | if not dok |
| 883 | viol = add viol 1 |
| 884 | score = sub score 0.30f64 |
| 885 | end |
| 886 | end |
| 887 | if eq strategy VERIF_UNSLOP_STRICT |
| 888 | if gt viol 0 |
| 889 | score = fmin score 0.50f64 |
| 890 | end |
| 891 | end |
| 892 | score = fmax 0.0f64 fmin 1.0f64 score |
| 893 | passed = eq viol 0 |
| 894 | if not passed |
| 895 | if not crit |
| 896 | if ge score 0.70f64 |
| 897 | passed = true |
| 898 | end |
| 899 | end |
| 900 | end |
| 901 | put res passed passed |
| 902 | put res critical crit |
| 903 | put res score score |
| 904 | put res violations viol |
| 905 | put res delim_ok dok |
| 906 | end |
| 907 | |
| 908 | note ---- upstream distillation preference & Cortex commit selector (src/distill.rs) |
| 909 | |
| 910 | fn adapter_distill_select t_pass bool t_score f64 s_pass bool s_score f64 commit_req bool dst edit DistillDec to void |
| 911 | put dst pref_delta sub t_score s_score |
| 912 | if and t_pass or not s_pass ge t_score s_score |
| 913 | put dst chosen CHOSEN_TEACHER |
| 914 | else |
| 915 | if and s_pass not t_pass |
| 916 | put dst chosen CHOSEN_STUDENT |
| 917 | else |
| 918 | put dst chosen CHOSEN_TEACHER |
| 919 | end |
| 920 | end |
| 921 | put dst commit_cortex and commit_req and t_pass ge t_score 0.85f64 |
| 922 | end |