code wiki / (root) / nx_gen_pfx_live_gate.nx

nx_gen_pfx_live_gate.nx source

↩ module page · 78 lines · 3752 B

1// nx_gen_pfx_live_gate.nx -- live gate for the HTTP "pfx" route (prefix-KV-cache over /gen, 2// 2026-07-16 forge-engine speed lever). Runs against a LIVE serve (argv[1]=port, default 8033) 3// loaded with any model; greedy determinism makes byte-equality the bar: 4// T1 baseline: POST /gen promptA (no pfx) -> textA 5// T2 pfx first call (degrade path: full prefill + snapshot) -> MUST == textA byte-exact 6// T3 pfx second call (RESTORE path -- the actual reuse) -> MUST == textA byte-exact 7// T4 boundary-safety: promptB baseline (no pfx) -> textB; then promptB WITH pfx:1 while the 8// snapshot still holds promptA rows -> MUST == textB (divergent tokens re-prefilled fresh; 9// cross-prompt contamination is the failure this tooth catches) 10// ms per call printed (informational; the wall-clock win shows at pack-scale prompts, the gate 11// proves SEMANTICS). license_tier: ORIGINAL expect_exit: 0 12import "nx_forge_loop.nx" 13 14// POST /gen and extract "text"; returns len or negative. pfx spliced when pfxv==1. 15func pg_gen(port: i64, prompt: *u8, pfxv: i64, out: *u8, cap: i64) -> i64 { 16 let body: *u8 = sys_mmap(65536) as *u8 17 let resp: *u8 = sys_mmap(131072) as *u8 18 let plen: i64 = std_slen(prompt) 19 var blen: i64 = fm_build_gen_body(prompt, plen, 8, 0, body) 20 if pfxv == 1 { blen = flp_splice_pfx(body, blen) } 21 let got: i64 = fm_post_gen(127, 0, 0, 1, port, body, blen, resp, 131072) 22 if got <= 0 { return 0 - 30 } 23 let tl: i64 = fm_extract_text(resp, got, out, cap) 24 return tl 25} 26 27func pg_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 28 if an != bn { return 0 } 29 var i: i64 = 0 30 while i < an { 31 if a[i] != b[i] { return 0 } 32 i = i + 1 33 } 34 return 1 35} 36 37func main(argc: i64, argv: *i64) -> i64 { 38 let pa: *u8 = "The capital of France is" as *u8 39 let pb: *u8 = "Hello world says" as *u8 40 var port: i64 = 8033 41 if argc >= 2 { 42 let a1: i64 = argv[1] 43 let p1: *u8 = a1 as *u8 44 port = std_atoi(p1) 45 } 46 std_putln("GEN-PFX-LIVE-GATE: prefix-KV route byte-identity vs plain /gen (greedy)" as *u8) 47 let ta: *u8 = sys_mmap(65536) as *u8 48 let t2: *u8 = sys_mmap(65536) as *u8 49 let t3: *u8 = sys_mmap(65536) as *u8 50 let tb: *u8 = sys_mmap(65536) as *u8 51 let t5: *u8 = sys_mmap(65536) as *u8 52 var pass: i64 = 0 53 let na: i64 = pg_gen(port, pa, 0, ta, 65536) 54 if na > 0 { pass = pass + 1; std_putln("T1 PASS baseline generated" as *u8) } 55 if na <= 0 { std_puts("T1 FAIL rc=" as *u8); std_pdec(na); std_puts(" (serve live?)\n" as *u8) } 56 let n2: i64 = pg_gen(port, pa, 1, t2, 65536) 57 if pg_eq(ta, na, t2, n2) == 1 { pass = pass + 1; std_putln("T2 PASS pfx first-call == baseline (degrade+snapshot)" as *u8) } 58 if pg_eq(ta, na, t2, n2) == 0 { std_putln("T2 FAIL pfx first-call diverged" as *u8) } 59 let n3: i64 = pg_gen(port, pa, 1, t3, 65536) 60 if pg_eq(ta, na, t3, n3) == 1 { pass = pass + 1; std_putln("T3 PASS pfx RESTORE == baseline (the reuse path)" as *u8) } 61 if pg_eq(ta, na, t3, n3) == 0 { std_putln("T3 FAIL pfx restore diverged" as *u8) } 62 let nb: i64 = pg_gen(port, pb, 0, tb, 65536) 63 let n5: i64 = pg_gen(port, pb, 1, t5, 65536) 64 var t4ok: i64 = 0 65 if nb > 0 { if pg_eq(tb, nb, t5, n5) == 1 { t4ok = 1 } } 66 if t4ok == 1 { pass = pass + 1; std_putln("T4 PASS boundary-safe: different prompt with stale snapshot == its own baseline" as *u8) } 67 if t4ok == 0 { std_putln("T4 FAIL cross-prompt contamination or promptB baseline failed" as *u8) } 68 std_puts("GEN-PFX-LIVE-GATE pass=" as *u8) 69 std_pdec(pass) 70 if pass == 4 { 71 std_putln("/4 verdict=GREEN" as *u8) 72 sys_exit(0) 73 return 0 74 } 75 std_putln("/4 verdict=RED" as *u8) 76 sys_exit(1) 77 return 1 78}