code wiki / _hdl_build / nx_pattern_emit_rankfuse_demo.nx

nx_pattern_emit_rankfuse_demo.nx source

↩ module page · 68 lines · 5040 B

1// nx_pattern_emit_rankfuse_demo.nx -- DEMO/GATE: the Builder runs the RANK_FUSE emitter on a DATA spec and the 2// emitter AUTHORS a working module + test (no Claude logic). Proves shape 21 is now team-authorable: 3// T1 author succeeds for a valid spec (k=3,n=5) and REFUSES a bad spec (k=1) -- the emit-time rail. 4// T2 the AUTHORED module is real RRF code: it contains exactly k=3 'nx_rrf_add' calls + an 'nx_rrf_argmax' + the _fuse fn. 5// T3 determinism: authoring the same spec twice -> byte-identical module. 6// (The authored module rf_authored.nx + its test are written to runtime/_hdl_build/; building rf_authored_test 7// via the runner then proves it COMPILES + PASSES = the autonomy credit, end to end.) 8// expect_exit: 0 license_tier: ORIGINAL 9import "nx_pattern_emit_rankfuse.nx" 10import "nx_syscalls.nx" 11const K_MAGIC_65536: i64 = 65536 12const K_MAGIC_16384: i64 = 16384 13 14func dw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func dn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 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 } 16 17func read_all(path: *u8, lenbox: *i64) -> i64 { 18 let fd: i64=sys_openat_rd(path); if fd<0 { lenbox[0]=0; return 0 } 19 let buf: *u8=sys_mmap(K_MAGIC_65536); var off: i64=0; var go: i64=1 20 while go==1 { let r: i64=sys_read(fd, ((buf as i64)+off) as *u8, K_MAGIC_16384); if r<=0 { go=0 } else { off=off+r } } 21 sys_close(fd); lenbox[0]=off; return buf as i64 22} 23func count_sub(hay: *u8, hlen: i64, needle: *u8, nlen: i64) -> i64 { 24 var cnt: i64=0; var i: i64=0 25 while i+nlen<=hlen { var j: i64=0; var eq: i64=1; while j<nlen { if hay[i+j]!=needle[j] { eq=0; j=nlen } else { j=j+1 } } if eq==1 { cnt=cnt+1 } i=i+1 } 26 return cnt 27} 28 29func main() -> i64 { 30 dw("=== nx_pattern_emit_rankfuse_demo: the Builder AUTHORS a RANK_FUSE module from a spec (shape 21) ===\n" as *u8) 31 var pass: i64=0; var total: i64=0 32 let mp: *u8="runtime/_hdl_build/rf_authored.nx" as *u8 33 let tp: *u8="runtime/_hdl_build/rf_authored_test.nx" as *u8 34 35 // valid spec: k=3 rankings, n=5 items 36 let spec: *i64=sys_mmap(64) as *i64; spec[0]=3; spec[1]=5 37 let ok: i64=pe_rankfuse_author("rf_authored" as *u8, mp, tp, spec) 38 // bad spec: k=1 (below 2) -> must REFUSE 39 let badspec: *i64=sys_mmap(64) as *i64; badspec[0]=1; badspec[1]=5 40 let bad: i64=pe_rankfuse_author("rf_bad" as *u8, "runtime/_hdl_build/rf_bad.nx" as *u8, "runtime/_hdl_build/rf_bad_test.nx" as *u8, badspec) 41 42 // ---- T1: valid authored, bad refused ---- 43 total=total+1; if ok==1 { if bad==0 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } 44 dw("T1 AUTHOR+RAIL: valid spec authored=" as *u8); dn(ok); dw(", bad spec (k=1) refused=" as *u8); dn(1-bad); dw("\n" as *u8) 45 46 // ---- T2: the authored module is REAL RRF code (k=3 rrf_add + argmax + _fuse) ---- 47 let lb: *i64=sys_mmap(16) as *i64; let buf: *i64=read_all(mp, lb) 48 let b: *u8=buf as *u8; let blen: i64=lb[0] 49 let n_add: i64=count_sub(b, blen, "nx_rrf_add" as *u8, 10) 50 let n_argmax: i64=count_sub(b, blen, "nx_rrf_argmax" as *u8, 13) 51 let n_fuse: i64=count_sub(b, blen, "_fuse(orders" as *u8, 12) 52 total=total+1; if n_add==3 { if n_argmax>=1 { if n_fuse>=1 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } 53 dw("T2 AUTHORED REAL CODE: module=" as *u8); dn(blen); dw("B, nx_rrf_add x" as *u8); dn(n_add); dw(" (=k=3), nx_rrf_argmax x" as *u8); dn(n_argmax); dw(", _fuse fn x" as *u8); dn(n_fuse); dw("\n" as *u8) 54 55 // ---- T3: determinism -- re-author the same spec -> byte-identical ---- 56 pe_rankfuse_author("rf_authored" as *u8, "runtime/_hdl_build/rf_authored2.nx" as *u8, "runtime/_hdl_build/rf_authored2_test.nx" as *u8, spec) 57 let lb2: *i64=sys_mmap(16) as *i64; let buf2: *i64=read_all("runtime/_hdl_build/rf_authored2.nx" as *u8, lb2) 58 let b2: *u8=buf2 as *u8; var d: i64=0; if lb2[0]!=blen { d=1 } 59 var i: i64=0; while i<blen { if b[i]!=b2[i] { d=d+1 } i=i+1 } 60 total=total+1; if d==0 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } 61 dw("T3 DETERMINISM: re-authoring the spec -> byte-identical module (diffs=" as *u8); dn(d); dw(")\n" as *u8) 62 63 dw("\n AUTONOMY CREDIT: shape 21 RANK_FUSE is now TEAM-AUTHORABLE -- the Builder emits the core from a spec, no Claude.\n" as *u8) 64 dw(" (next: build rf_authored_test via the runner to prove the AUTHORED module compiles+passes end-to-end.)\n" as *u8) 65 dw("EMIT-RANKFUSE-DEMO verdict=" as *u8) 66 if pass==total { dw("GREEN passes=" as *u8); dn(pass); dw("/" as *u8); dn(total); dw(" END\n" as *u8); sys_exit(0); return 0 } 67 dw("RED passes=" as *u8); dn(pass); dw("/" as *u8); dn(total); dw(" END\n" as *u8); sys_exit(1); return 1 68}