code wiki / _hdl_build / nx_work_predict_gate.nx

nx_work_predict_gate.nx source

↩ module page · 78 lines · 4937 B

1import "nx_gate_gn.nx" 2// nx_work_predict_gate.nx -- hermetic gate for the FUTURE predictor. Proves prediction is grounded 3// and never over-claims readiness: 4// T1 dep DONE -> ADJACENT (probable-next). 5// T2 (LOAD-BEARING) dep NOT done -> BLOCKED (a still-blocked node is NEVER predicted as next). 6// T3 multi-dep all DONE -> ADJACENT; T4 multi-dep one missing -> BLOCKED. 7// T5 deps "-" -> ROOT (foundational, not a grow-from-parent prediction). 8// T6 status DONE -> DONE (already built, not predicted). 9// T7 end-to-end over a fixture ladder: adjacent count + the right ids + correct stats + WHY. 10// T8 (NEG) blocked/root/done ids never appear in the predicted output. 11// Sovereign: imports nx_syscalls + the predict lib only. license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_work_predict_lib.nx" 14 15func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16func gstrlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 17func gfind(buf: *u8, n: i64, pat: *u8) -> i64 { 18 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 } 19 if pl == 0 { return 0 - 1 } 20 var i: i64 = 0 21 while i + pl <= n { 22 var j: i64 = 0; var ok: i64 = 1 23 while j < pl { if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } } 24 if ok == 1 { return i } 25 i = i + 1 26 } 27 return 0 - 1 28} 29 30func main(argc: i64, argv: *i64) -> i64 { 31 gp("=== nx_work_predict_gate (FUTURE: genealogy-grounded probable-next) ===\n" as *u8) 32 33 var pass: i64 = 0 34 var fail: i64 = 0 35 36 // ---- classifier unit tests against a known done-set "\tA\tB\t" ---- 37 let ds: *u8 = "\tA\tB\t" as *u8 38 let dl: i64 = gstrlen(ds) 39 40 if wp_classify("TODO" as *u8, "A" as *u8, ds, dl) == WP_ADJACENT { pass = pass + 1; gp(" T1 dep-DONE->ADJACENT PASS\n" as *u8) } else { fail = fail + 1; gp(" T1 FAIL\n" as *u8) } 41 if wp_classify("TODO" as *u8, "Z" as *u8, ds, dl) == WP_BLOCKED { pass = pass + 1; gp(" T2 dep-missing->BLOCKED PASS\n" as *u8) } else { fail = fail + 1; gp(" T2 FAIL (would predict a blocked node)\n" as *u8) } 42 if wp_classify("NOVEL" as *u8, "A,B" as *u8, ds, dl) == WP_ADJACENT { pass = pass + 1; gp(" T3 multidep-all-DONE->ADJACENT PASS\n" as *u8) } else { fail = fail + 1; gp(" T3 FAIL\n" as *u8) } 43 if wp_classify("TODO" as *u8, "A,Z" as *u8, ds, dl) == WP_BLOCKED { pass = pass + 1; gp(" T4 multidep-one-missing->BLOCKED PASS\n" as *u8) } else { fail = fail + 1; gp(" T4 FAIL\n" as *u8) } 44 if wp_classify("TODO" as *u8, "-" as *u8, ds, dl) == WP_ROOT { pass = pass + 1; gp(" T5 no-dep->ROOT PASS\n" as *u8) } else { fail = fail + 1; gp(" T5 FAIL\n" as *u8) } 45 if wp_classify("DONE" as *u8, "A" as *u8, ds, dl) == WP_DONE { pass = pass + 1; gp(" T6 status-DONE->DONE PASS\n" as *u8) } else { fail = fail + 1; gp(" T6 FAIL\n" as *u8) } 46 47 // ---- end-to-end over a fixture ladder (TAB-separated, # comment skipped) ---- 48 let fix: *u8 = "# layer\tid\tstatus\tgrounding\tdeps\n0\tA\tDONE\tgrounded\t-\n0\tB\tDONE\tgrounded\t-\n1\tC\tTODO\tgrounded\tA\n1\tD\tTODO\tgrounded\tZ\n1\tE\tNOVEL\tgrounded\tA,B\n2\tF\tTODO\tgrounded\tA,Z\n0\tG\tTODO\tgrounded\t-\n1\tH\tDONE\tgrounded\tA\n" as *u8 49 let flen: i64 = gstrlen(fix) 50 let out: *u8 = sys_mmap(65536) 51 let stats: *i64 = sys_mmap(48) as *i64 52 let nadj: i64 = wp_predict(fix, flen, out, 65536, stats) 53 let ol: i64 = gstrlen(out) 54 55 // T7: exactly C + E adjacent; stats rows=8 done=3 adjacent=2 root=1 blocked=2; WHY present. 56 var t7: i64 = 1 57 if nadj != 2 { t7 = 0 } 58 if gfind(out, ol, "C\t(layer 1)\t<- A DONE" as *u8) < 0 { t7 = 0 } 59 if gfind(out, ol, "E\t(layer 1)\t<- A DONE" as *u8) < 0 { t7 = 0 } 60 if stats[0] != 8 { t7 = 0 } 61 if stats[1] != 3 { t7 = 0 } 62 if stats[2] != 2 { t7 = 0 } 63 if stats[3] != 1 { t7 = 0 } 64 if stats[4] != 2 { t7 = 0 } 65 if t7 == 1 { pass = pass + 1; gp(" T7 e2e-adjacent+stats+why PASS\n" as *u8) } else { fail = fail + 1; gp(" T7 FAIL nadj=" as *u8); gn(nadj); gp(" rows=" as *u8); gn(stats[0]); gp(" done=" as *u8); gn(stats[1]); gp(" adj=" as *u8); gn(stats[2]); gp(" root=" as *u8); gn(stats[3]); gp(" blk=" as *u8); gn(stats[4]); gp("\n" as *u8) } 66 67 // T8 (NEG): blocked (D,F), root (G), done (H) never predicted. 68 var t8: i64 = 1 69 if gfind(out, ol, "D\t" as *u8) >= 0 { t8 = 0 } 70 if gfind(out, ol, "F\t" as *u8) >= 0 { t8 = 0 } 71 if gfind(out, ol, "G\t" as *u8) >= 0 { t8 = 0 } 72 if gfind(out, ol, "H\t" as *u8) >= 0 { t8 = 0 } 73 if t8 == 1 { pass = pass + 1; gp(" T8 blocked/root/done-never-predicted PASS\n" as *u8) } else { fail = fail + 1; gp(" T8 FAIL (over-claimed a non-ready node)\n" as *u8) } 74 75 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 76 if fail == 0 { gp(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 77 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 78}