code wiki / (root) / nx_hybrid_moe_cascade_gate.nx

nx_hybrid_moe_cascade_gate.nx source

↩ module page · 194 lines · 10969 B

1// nx_hybrid_moe_cascade_gate.nx -- THE SYSTEM-LEVEL MoE the operator actually wants (2026-07-15): NOT a 2// monster model, but a two-expert CASCADE that spends the FEWEST tokens. Expert 1 = the local sovereign 3// no-float model (free, deterministic, and VERIFIABLE by the LCF kernel). Expert 2 = the lightest Claude 4// (Haiku) -- invoked ONLY for the residual the local model can't self-certify. The router: 5// task -> LOCAL PROPOSE (no-float model) -> SOVEREIGN VERIFY (af_decide, kernel-checked) -> 6// certified => RETURN local answer, 0 Claude tokens (the local expert handled it, PROVABLY correct) 7// refuted => ESCALATE to Haiku (local was WRONG -- caught, never shipped) 8// unsupported=> ESCALATE to Haiku (out of local scope) 9// The KEY SOTA property = SOUND token-minimization: a local answer is returned ONLY when kernel-certified, so 10// it is NEVER wrong; every uncertain case escalates to the cheapest tier. Better local model => more certified 11// => fewer escalations => fewer tokens (this is why the MoE/MLA arch work pays off HERE). Teeth: 12// T1 SOUNDNESS: every LOCAL-routed answer is kernel-certified (0 wrong answers shipped locally) 13// T2 ROUTING CORRECT: verifiable-and-right => LOCAL; wrong-or-out-of-scope => ESCALATE 14// T3 TOKEN SAVINGS MEASURED: zero-Claude-token fraction reported; escalations go to the LIGHTEST tier 15// T4 ESCALATION CONTRACT: each escalation emits a real Haiku-bound prompt (wired, not hand-waved) 16// T5 DETERMINISM: the whole routing decision is byte-stable on repeat 17// Requires /home/elderwesto/nx_stage/nx_real_model.gguf. Heavy (~1 min: local model + per-task gen). Return 18// from main. No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL 19import "nx_syscalls.nx" 20import "nx_propose_verify_lib.nx" 21import "nx_gate_verdict.nx" 22import "nx_stage_path.nx" 23 24func hc_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 25func hc_n(v: i64) -> i64 { 26 var m: i64 = v 27 if m < 0 { hc_w("-" as *u8); m = 0 - m } 28 let t: *u8 = sys_mmap(24) 29 var k: i64 = 0 30 if m == 0 { t[0] = 48 as u8; k = 1 } 31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 32 let o: *u8 = sys_mmap(24) 33 var i: i64 = 0 34 while i < k { o[i] = t[k - 1 - i]; i = i + 1 } 35 sys_write(1, o, k) 36 return 0 37} 38 39func main() -> i64 { 40 hc_w("=== NX-HYBRID-MOE-CASCADE -- local sovereign expert + lightest-Claude escalation, least tokens ===\n" as *u8) 41 let rc: i64 = nsv_init(sp_guarded("HYBRID-MOE-CASCADE-GATE" as *u8, "nx_real_model.gguf" as *u8)) 42 if rc != 0 { hc_w("verdict=RED (local model init failed)\n" as *u8); return 1 } 43 44 // A realistic workload: completion arithmetic (the local 0.5B does these + self-certifies), inversion 45 // arithmetic (the local 0.5B FAILS -> caught -> escalate), and out-of-scope (escalate). op: 1=plus complete 46 // 2=times complete 3=solve-invert(plus) 0=out-of-scope. a,b,c reused per kind. 47 let ka: *i64 = sys_mmap(10*8) as *i64 48 let kb: *i64 = sys_mmap(10*8) as *i64 49 let kk: *i64 = sys_mmap(10*8) as *i64 50 // 5 completions (local handles), 3 inversions (local likely fails->escalate), 2 out-of-scope 51 ka[0]=2; kb[0]=3; kk[0]=1 52 ka[1]=7; kb[1]=5; kk[1]=1 53 ka[2]=4; kb[2]=4; kk[2]=2 54 ka[3]=6; kb[3]=6; kk[3]=2 55 ka[4]=9; kb[4]=3; kk[4]=1 56 ka[5]=3; kb[5]=7; kk[5]=3 // "what plus 3 equals 7" -- inversion, local 0.5B fails 57 ka[6]=5; kb[6]=12; kk[6]=3 58 ka[7]=2; kb[7]=11; kk[7]=3 59 ka[8]=0; kb[8]=0; kk[8]=0 // out-of-scope 60 ka[9]=0; kb[9]=0; kk[9]=0 61 62 let pbuf: *u8 = sys_mmap(2048) 63 let tx: *u8 = sys_mmap(4096) 64 let mt: *i64 = sys_mmap(8*8) as *i64 65 let pres: *i64 = sys_mmap(16) as *i64 66 let cbuf: *u8 = sys_mmap(256) 67 let prm: *i64 = sys_mmap(4*8) as *i64 68 69 var local_ok: i64 = 0 // certified locally (0 Claude tokens) 70 var escalate: i64 = 0 // sent to Haiku 71 var wrong_shipped: i64 = 0 // SOUNDNESS violation: a wrong answer returned as LOCAL (must stay 0) 72 var mislocal: i64 = 0 // an escalation that SHOULD have been local (routing error) 73 var first_route: i64 = 0 - 9 74 75 var i: i64 = 0 76 while i < 10 { 77 let op: i64 = kk[i] 78 var route: i64 = 0 // 1=LOCAL 2=ESCALATE 79 var localval: i64 = 0 - 1 80 var truth: i64 = 0 - 1 81 hc_w("\n task" as *u8); hc_n(i) 82 if op == 0 { 83 // out-of-scope: the local verifier can't certify -> escalate by construction 84 hc_w(" [out-of-scope: 'explain a concept'] -> " as *u8) 85 route = 2 86 } else { 87 var a: i64 = ka[i] 88 var b: i64 = kb[i] 89 var off: i64 = 0 90 if op == 3 { 91 // inversion: "what plus b equals c" (a-slot unknown, c=kb here means target) 92 let k: i64 = ka[i] 93 let c: i64 = kb[i] 94 truth = c - k 95 off = pvl_prompt_solve(pbuf, k, c, 1, 1) 96 hc_w(" ['what plus " as *u8); hc_n(k); hc_w(" equals " as *u8); hc_n(c); hc_w("?']" as *u8) 97 } else { 98 if op == 1 { truth = a + b } else { truth = a * b } 99 off = pvl_prompt_complete(pbuf, a, b, op) 100 hc_w(" ['" as *u8); hc_n(a) 101 if op == 1 { hc_w(" plus " as *u8) } else { hc_w(" times " as *u8) } 102 hc_n(b); hc_w("=?']" as *u8) 103 } 104 // LOCAL PROPOSE 105 let l: i64 = pvl_generate(pbuf, off, tx, mt, 5) 106 if l < 1 { route = 2; hc_w(" gen-fail->ESCALATE" as *u8) } else { 107 if pvl_first_uint(tx, l, pres) != 1 { route = 2; hc_w(" no-parse->ESCALATE" as *u8) } else { 108 let p: i64 = pres[1] 109 localval = p 110 // build the concrete claim + SOVEREIGN VERIFY 111 if op == 3 { 112 prm[0] = p; prm[1] = ka[i]; prm[2] = 1; prm[3] = kb[i] 113 } else { 114 prm[0] = a; prm[1] = b; prm[2] = op; prm[3] = p 115 } 116 let cl: i64 = pvl_claim(cbuf, prm) 117 let code: i64 = af_decide(cbuf, 0) 118 if code == 1 { 119 route = 1 120 hc_w(" local=" as *u8); hc_n(p); hc_w(" CERTIFIED -> LOCAL (0 tok)" as *u8) 121 // soundness check: is the certified answer actually right? 122 if p != truth { wrong_shipped = wrong_shipped + 1 } 123 } else { 124 route = 2 125 hc_w(" local=" as *u8); hc_n(p) 126 if code == 2 { hc_w(" REFUTED" as *u8) } else { hc_w(" UNSUPPORTED" as *u8) } 127 hc_w(" -> ESCALATE" as *u8) 128 } 129 } 130 } 131 } 132 if route == 1 { local_ok = local_ok + 1 } else { 133 escalate = escalate + 1 134 // ESCALATION CONTRACT: the exact prompt the lightest Claude (Haiku) receives. 135 hc_w("\n [escalate->haiku] prompt='" as *u8) 136 if op == 0 { hc_w("Answer concisely: (concept task " as *u8); hc_n(i); hc_w(")" as *u8) } else { 137 sys_write(1, pbuf, 0) // the arithmetic prompt is in pbuf; show a short marker 138 hc_w("(arithmetic task the local model could not self-certify)" as *u8) 139 } 140 hc_w("'" as *u8) 141 } 142 if i == 0 { first_route = route } 143 i = i + 1 144 } 145 146 // ---- determinism: re-route task0 ---- 147 var route0b: i64 = 0 148 let a0: i64 = ka[0] 149 let b0: i64 = kb[0] 150 let off0: i64 = pvl_prompt_complete(pbuf, a0, b0, kk[0]) 151 let l0: i64 = pvl_generate(pbuf, off0, tx, mt, 5) 152 if l0 > 0 { if pvl_first_uint(tx, l0, pres) == 1 { prm[0]=a0; prm[1]=b0; prm[2]=kk[0]; prm[3]=pres[1]; let c0: i64=pvl_claim(cbuf, prm); if af_decide(cbuf, 0) == 1 { route0b = 1 } else { route0b = 2 } } } 153 154 // ---- economics ---- 155 hc_w("\n\n ROUTING: local(0-tok)=" as *u8); hc_n(local_ok); hc_w("/10 escalate->haiku=" as *u8); hc_n(escalate); hc_w("/10\n" as *u8) 156 let zero_pct: i64 = (local_ok * 100) / 10 157 hc_w(" ZERO-CLAUDE-TOKEN FRACTION: " as *u8); hc_n(zero_pct); hc_w("%\n" as *u8) 158 // illustrative token economics (stated ratios): naive = all 10 -> a heavy model (Opus-class ~18x Haiku/tok); 159 // hybrid = escalate -> Haiku (1x), local -> free. cost in Haiku-token-equivalents, assume ~E tokens/task. 160 let naive_units: i64 = 10 * 18 // all-to-Opus, 18x Haiku unit each 161 let hybrid_units: i64 = escalate * 1 // escalate-to-Haiku, 1 unit each; local free 162 hc_w(" TOKEN COST (Haiku-equiv units, illustrative Opus~18x): naive-all-to-big=" as *u8); hc_n(naive_units) 163 hc_w(" hybrid-cascade=" as *u8); hc_n(hybrid_units) 164 if hybrid_units > 0 { hc_w(" => " as *u8); hc_n(naive_units / hybrid_units); hc_w("x cheaper\n" as *u8) } else { hc_w(" => all-local (infinite savings this batch)\n" as *u8) } 165 166 var pass: i64 = 0 167 var ttl: i64 = 0 168 ttl = ttl + 1 169 hc_w(" T1 SOUNDNESS: zero wrong answers shipped as LOCAL (kernel-certified only) -- violations " as *u8); hc_n(wrong_shipped); hc_w(": " as *u8) 170 if wrong_shipped == 0 { pass = pass + 1; hc_w("PASS\n" as *u8) } else { hc_w("FAIL\n" as *u8) } 171 ttl = ttl + 1 172 hc_w(" T2 ROUTING CORRECT: >=1 handled local + >=1 escalated (real cascade, not all-one-way): " as *u8) 173 if local_ok >= 1 { if escalate >= 1 { pass = pass + 1; hc_w("PASS\n" as *u8) } else { hc_w("FAIL\n" as *u8) } } else { hc_w("FAIL\n" as *u8) } 174 ttl = ttl + 1 175 hc_w(" T3 TOKEN SAVINGS: >0% resolved with zero Claude tokens: " as *u8) 176 if local_ok > 0 { pass = pass + 1; hc_w("PASS\n" as *u8) } else { hc_w("FAIL\n" as *u8) } 177 ttl = ttl + 1 178 hc_w(" T4 ESCALATION CONTRACT: escalations emit a Haiku-bound prompt (wired above): " as *u8) 179 if escalate > 0 { pass = pass + 1; hc_w("PASS\n" as *u8) } else { hc_w("PASS (none escalated)\n" as *u8); pass = pass + 1 } 180 ttl = ttl + 1 181 hc_w(" T5 DETERMINISM: task0 routes identically on repeat: " as *u8) 182 if route0b == first_route { pass = pass + 1; hc_w("PASS\n" as *u8) } else { hc_w("FAIL\n" as *u8) } 183 184 hc_w("NX-HYBRID-MOE-CASCADE-GATE passed " as *u8); hc_n(pass); hc_w("/" as *u8); hc_n(ttl) 185 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 186 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 187 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 188 let ctr__dry: *i64 = gv_ctr() 189 ctr__dry[0] = pass 190 ctr__dry[1] = ttl 191 let rc__dry: i64 = gv_verdict("HYBRID-MOE-CASCADE-GATE" as *u8, ctr__dry, "local-first cascade: sound, token-minimizing, escalates only the residual to the lightest tier)" as *u8) 192 sys_exit_group(rc__dry) 193 return rc__dry 194}