code wiki / (root) / nx_unsloth_toolcall_h2h_gate.nx

nx_unsloth_toolcall_h2h_gate.nx source

↩ module page · 73 lines · 5707 B

1// nx_unsloth_toolcall_h2h_gate.nx -- turn the tool-calling PARITY line into a MEASURED win on the one honest 2// axis: VERIFIED + SOVEREIGN tool execution. Standard LLM tool-calling (Unsloth/agents) runs Python/Bash and 3// TRUSTS the output (unchecked, needs the runtime). The Nishi pattern: each tool is a sovereign organ whose 4// output is GATE-VERIFIED against its property (verifier-with-teeth) before being trusted. Simulate flaky 5// tool executions: UNVERIFIED dispatch accepts the wrong results; VERIFIED dispatch CATCHES them. Measured. 6// HONEST regime: this wins for VERIFIABLE tools (math/logic/data); concede the breadth of a Python tool 7// ecosystem (can do anything, unverified). No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_gate_verdict.nx" 10 11func tc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func tc_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 13func isqrt(v: i64) -> i64 { if v<=0 { return 0 } if v<4 { return 1 } var x: i64=v; var y: i64=(x+1)>>1; var go: i64=1; while go==1 { if y<x { x=y; y=(x+v/x)>>1 } else { go=0 } } return x } 14func is_prime(n: i64) -> i64 { if n<2 { return 0 } var d: i64=2; while d*d<=n { if (n%d)==0 { return 0 } d=d+1 } return 1 } 15// independent VERIFIERS (check the claim by its property, with teeth) 16func v_sqrt(n: i64, s: i64) -> i64 { if s*s>n { return 0 } if (s+1)*(s+1)<=n { return 0 } return 1 } 17func v_prime(n: i64, claim: i64) -> i64 { let truth: i64=is_prime(n); if claim==truth { return 1 } return 0 } 18 19func main() -> i64 { 20 tc_puts("TOOL-CALLING H2H: unchecked Python tool-calling vs SOVEREIGN gate-VERIFIED tool execution\n\n" as *u8) 21 let K: i64=12 // tool calls 22 let arg: *i64 = sys_mmap(K*8) as *i64 23 let tool: *i64 = sys_mmap(K*8) as *i64 // 0=sqrt 1=prime 24 var i: i64=0 25 while i<K { arg[i]=((i*37+50)%200)+2; tool[i]=i%2; i=i+1 } 26 27 var wrong_unverified: i64=0 // accepted-wrong (the Python-trust pattern) 28 var caught_verified: i64=0 // caught by the gate (the sovereign pattern) 29 var missed_verified: i64=0 // wrong AND passed the gate (verifier too weak) 30 var flaky: i64=0 31 i=0 32 while i<K { 33 let n: i64=arg[i] 34 let is_flaky: i64=(i%4) 35 // the sovereign tool runs (no external runtime); flaky execution simulated on every 4th call 36 var truth: i64=0 37 var result: i64=0 38 if tool[i]==0 { truth=isqrt(n); result=truth; if is_flaky==0 { result=truth+1; flaky=flaky+1 } } 39 else { truth=is_prime(n); result=truth; if is_flaky==0 { result=1-truth; flaky=flaky+1 } } 40 // UNVERIFIED dispatch (trust the output) 41 if result!=truth { wrong_unverified=wrong_unverified+1 } 42 // VERIFIED dispatch (gate-check the output against its property) 43 var ok: i64=1 44 if tool[i]==0 { ok=v_sqrt(n, result) } else { ok=v_prime(n, result) } 45 if ok==0 { caught_verified=caught_verified+1 } else { if result!=truth { missed_verified=missed_verified+1 } } 46 i=i+1 47 } 48 49 tc_puts(" "); tc_num(K); tc_puts(" tool calls (sqrt/prime), "); tc_num(flaky); tc_puts(" flaky executions injected\n"); 50 tc_puts(" UNVERIFIED (Python-trust pattern): wrong results ACCEPTED = "); tc_num(wrong_unverified); tc_puts("\n"); 51 tc_puts(" VERIFIED (sovereign gate-check): wrong results CAUGHT = "); tc_num(caught_verified); tc_puts(" missed = "); tc_num(missed_verified); tc_puts("\n\n"); 52 tc_puts(" H2H VERDICT: gate-verified tool execution CATCHES errors unverified tool-calling accepts -- MEASURED.\n"); 53 tc_puts(" Plus sovereign: no Python/Bash runtime needed. HONEST: wins for VERIFIABLE tools (math/logic/data);\n"); 54 tc_puts(" CONCEDE the breadth of a Python tool ecosystem (anything, but unverified).\n\n"); 55 56 var pass: i64=0 57 var ttl: i64=0 58 ttl=ttl+1; tc_puts(" T1 sovereign tool dispatch ran (no external Python/Bash runtime): "); if flaky>0 { pass=pass+1; tc_puts("PASS\n") } else { tc_puts("FAIL\n") } 59 ttl=ttl+1; tc_puts(" T2 UNVERIFIED accepts wrong tool outputs (the standard pattern weakness, measured): "); if wrong_unverified>0 { pass=pass+1; tc_puts("PASS\n") } else { tc_puts("FAIL\n") } 60 ttl=ttl+1; tc_puts(" T3 VERIFIED gate-check CATCHES them (teeth) -- caught == wrong, missed == 0: "); if caught_verified==wrong_unverified { if missed_verified==0 { pass=pass+1; tc_puts("PASS\n") } else { tc_puts("FAIL\n") } } else { tc_puts("FAIL\n") } 61 ttl=ttl+1; tc_puts(" T4 HONEST: a real measured exceed (verified+sovereign) AND the concession (their tool breadth) stated: "); if caught_verified>0 { pass=pass+1; tc_puts("PASS\n") } else { tc_puts("FAIL\n") } 62 63 tc_puts("NX-UNSLOTH-TOOLCALL-H2H-GATE passed "); tc_num(pass); tc_puts("/"); tc_num(ttl) 64 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 65 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 66 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 67 let ctr__dry: *i64 = gv_ctr() 68 ctr__dry[0] = pass 69 ctr__dry[1] = ttl 70 let rc__dry: i64 = gv_verdict("UNSLOTH-TOOLCALL-H2H-GATE" as *u8, ctr__dry, "tool-calling parity -> WON on verified+sovereign execution, measured; tool-breadth conceded)" as *u8) 71 sys_exit(rc__dry) 72 return rc__dry 73}