code wiki / (root) / nx_forge_engine_gate.nx

nx_forge_engine_gate.nx source

↩ module page · 181 lines · 8479 B

1// nx_forge_engine_gate.nx -- gate for the assembled autonomous-coding engine. Deterministic, 2// NO model: the raceless bind-pre-fork stub maker serves scripted /gen responses + captures 3// request bodies. Proves the two-stage engine: 4// T1 GREEN at loop stage (good candidate -> rc=1; sampling never engaged) 5// T2 GREEN at sample stage: loop exhausts (broken, maxc=1) -> sample1 broken -> sample2 good 6// -> rc=102 (100+k). The free-verifier best-of-N lever, mechanized. 7// T2b sample requests carry "temp" + DETERMINISTIC per-sample seeds (11001, 11002) 8// T2c loop-stage request has NO "temp" key (greedy preserved) AND sample requests carry NO 9// repair header (independence between samples) 10// T3 NEG: everything broken -> rc=-40 (exhausted, never a fake GREEN) 11// Fixture JSONs built at gate time from the SAME flp_* fixture programs (never drift). 12// license_tier: ORIGINAL expect_exit: 0 13import "nx_forge_engine.nx" 14import "nx_gate_verdict.nx" 15 16func feg_bind(port: i64) -> i64 { 17 let fd: i64 = sys_socket(2, 1, 0) 18 if fd < 0 { return 0 - 1 } 19 let opt: *i64 = sys_mmap(8) as *i64 20 opt[0] = 1 21 __syscall(54, fd, 1, 2, opt as i64, 4, 0) 22 let addr: *u8 = sys_mmap(16) as *u8 23 addr[0] = 2 as u8 24 addr[1] = 0 as u8 25 addr[2] = ((port >> 8) & 255) as u8 26 addr[3] = (port & 255) as u8 27 var z: i64 = 4 28 while z < 16 { addr[z] = 0 as u8; z = z + 1 } 29 if sys_bind(fd, addr, 16) < 0 { sys_close(fd); return 0 - 2 } 30 if sys_listen(fd, 16) < 0 { sys_close(fd); return 0 - 3 } 31 return fd 32} 33 34func feg_serve(lfd: i64, n: i64, resps: *i64, caps: *i64) -> i64 { 35 let h1: *u8 = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " as *u8 36 let h2: *u8 = "\r\nConnection: close\r\n\r\n" as *u8 37 let rq: *u8 = sys_mmap(65536) as *u8 38 let bodyb: *u8 = sys_mmap(65536) as *u8 39 let hdr: *u8 = sys_mmap(256) as *u8 40 var i: i64 = 0 41 while i < n { 42 let cfd: i64 = sys_accept(lfd) 43 if cfd < 0 { i = n } 44 if cfd >= 0 { 45 let r: i64 = sys_read(cfd, rq, 65536) 46 let cp0: i64 = caps[i] 47 let cpath: *u8 = cp0 as *u8 48 let kfd: i64 = sys_openat_wr(cpath, 420) 49 if kfd >= 0 { fc_wb(kfd, rq, r); sys_close(kfd) } 50 let rp0: i64 = resps[i] 51 let rpath: *u8 = rp0 as *u8 52 let bn: i64 = fc_read(rpath, bodyb, 65536) 53 var o: i64 = 0 54 var j: i64 = 0 55 while h1[j] != (0 as u8) { hdr[o] = h1[j]; o = o + 1; j = j + 1 } 56 let dl: i64 = std_itoa(bn, hdr + o) 57 o = o + dl 58 j = 0 59 while h2[j] != (0 as u8) { hdr[o] = h2[j]; o = o + 1; j = j + 1 } 60 sys_write(cfd, hdr, o) 61 sys_write(cfd, bodyb, bn) 62 sys_close(cfd) 63 i = i + 1 64 } 65 } 66 sys_close(lfd) 67 return 0 68} 69 70func feg_mkjson(progpath: *u8, outpath: *u8) -> i64 { 71 let pre: *u8 = "{\"ok\":1,\"text\":\"" as *u8 72 let pb: *u8 = sys_mmap(65536) as *u8 73 let jb: *u8 = sys_mmap(131072) as *u8 74 let n: i64 = fc_read(progpath, pb, 65536) 75 if n <= 0 { return 0 - 1 } 76 var o: i64 = 0 77 var i: i64 = 0 78 while pre[i] != (0 as u8) { jb[o] = pre[i]; o = o + 1; i = i + 1 } 79 o = fc_json_esc_bytes(jb, o, pb, n) 80 jb[o] = 34 as u8 81 o = o + 1 82 jb[o] = 125 as u8 83 o = o + 1 84 let fd: i64 = sys_openat_wr(outpath, 420) 85 if fd < 0 { return 0 - 2 } 86 let w: i64 = fc_wb(fd, jb, o) 87 sys_close(fd) 88 if w != 0 { return 0 - 3 } 89 return 0 90} 91 92func feg_case(organ: *u8, n: i64, resps: *i64, caps: *i64, port: i64, maxc: i64, nsamples: i64, temp_pm: i64) -> i64 { 93 let taskp: *u8 = "knowledge/forge/fix/flp_task.txt" as *u8 94 let expp: *u8 = "knowledge/forge/fix/flp_expect.bin" as *u8 95 let packp: *u8 = "knowledge/forge/nishilang_pack_mini_v1.txt" as *u8 96 let lfd: i64 = feg_bind(port) 97 if lfd < 0 { return 0 - 90 } 98 let pid: i64 = sys_fork() 99 if pid == 0 { 100 feg_serve(lfd, n, resps, caps) 101 sys_exit(0) 102 } 103 sys_close(lfd) 104 let rc: i64 = fe_run(organ, taskp, expp, packp, port, maxc, 400, 0, nsamples, temp_pm) 105 let st: *i64 = sys_mmap(64) as *i64 106 sys_wait4(pid, st, 0) 107 return rc 108} 109 110func feg_cap_has(cappath: *u8, needle: *u8) -> i64 { 111 let b: *u8 = sys_mmap(65536) as *u8 112 let n: i64 = fc_read(cappath, b, 65536) 113 if n <= 0 { return 0 - 1 } 114 let nl: i64 = std_slen(needle) 115 let h: i64 = fc_contains(b, n, needle, nl) 116 return h 117} 118 119func main(argc: i64, argv: *i64) -> i64 { 120 std_putln("FORGE-ENGINE-GATE: two-stage engine (greedy+repair, then sampled best-of-N)" as *u8) 121 let mg: i64 = feg_mkjson("knowledge/forge/fix/flp_good.nx" as *u8, "/tmp/feg_resp_good.json" as *u8) 122 let mb: i64 = feg_mkjson("knowledge/forge/fix/flp_broken.nx" as *u8, "/tmp/feg_resp_broken.json" as *u8) 123 if mg != 0 || mb != 0 { 124 std_putln("GATE-FAIL fixture-json build" as *u8) 125 sys_exit(1) 126 return 1 127 } 128 let resps: *i64 = sys_mmap(64) as *i64 129 let caps: *i64 = sys_mmap(64) as *i64 130 var pass: i64 = 0 131 // T1 green at loop stage 132 resps[0] = "/tmp/feg_resp_good.json" as *u8 as i64 133 caps[0] = "/tmp/feg_cap_t1_c1.txt" as *u8 as i64 134 let r1: i64 = feg_case("nx_feg_t1" as *u8, 1, resps, caps, 17851, 1, 2, 800) 135 if r1 == 1 { pass = pass + 1; std_putln("T1 PASS green@loop" as *u8) } 136 if r1 != 1 { std_puts("T1 FAIL rc=" as *u8); std_pdec(r1); std_puts("\n" as *u8) } 137 // T2 green at sample 2 138 resps[0] = "/tmp/feg_resp_broken.json" as *u8 as i64 139 resps[1] = "/tmp/feg_resp_broken.json" as *u8 as i64 140 resps[2] = "/tmp/feg_resp_good.json" as *u8 as i64 141 caps[0] = "/tmp/feg_cap_t2_c1.txt" as *u8 as i64 142 caps[1] = "/tmp/feg_cap_t2_c2.txt" as *u8 as i64 143 caps[2] = "/tmp/feg_cap_t2_c3.txt" as *u8 as i64 144 let r2: i64 = feg_case("nx_feg_t2" as *u8, 3, resps, caps, 17852, 1, 3, 800) 145 if r2 == 102 { pass = pass + 1; std_putln("T2 PASS green@sample2 (rc=102)" as *u8) } 146 if r2 != 102 { std_puts("T2 FAIL rc=" as *u8); std_pdec(r2); std_puts("\n" as *u8) } 147 let s1t: i64 = feg_cap_has("/tmp/feg_cap_t2_c2.txt" as *u8, "\"temp\":800" as *u8) 148 let s1s: i64 = feg_cap_has("/tmp/feg_cap_t2_c2.txt" as *u8, "\"seed\":11001" as *u8) 149 if s1t == 1 && s1s == 1 { pass = pass + 1; std_putln("T2b PASS sample1 carries temp + deterministic seed 11001" as *u8) } 150 if s1t != 1 || s1s != 1 { std_putln("T2b FAIL sampling knobs missing from sample1 body" as *u8) } 151 let s2s: i64 = feg_cap_has("/tmp/feg_cap_t2_c3.txt" as *u8, "\"seed\":11002" as *u8) 152 if s2s == 1 { pass = pass + 1; std_putln("T2c PASS sample2 seed 11002 (distinct, deterministic)" as *u8) } 153 if s2s != 1 { std_putln("T2c FAIL sample2 seed missing" as *u8) } 154 let g0: i64 = feg_cap_has("/tmp/feg_cap_t2_c1.txt" as *u8, "\"temp\":" as *u8) 155 let rp: i64 = feg_cap_has("/tmp/feg_cap_t2_c2.txt" as *u8, "PRIOR ATTEMPT FAILED" as *u8) 156 if g0 == 0 && rp == 0 { pass = pass + 1; std_putln("T2d PASS loop stayed greedy + samples independent (no repair header)" as *u8) } 157 if g0 != 0 || rp != 0 { std_putln("T2d FAIL greedy/independence violated" as *u8) } 158 // T3 NEG all broken -> exhausted 159 resps[0] = "/tmp/feg_resp_broken.json" as *u8 as i64 160 resps[1] = "/tmp/feg_resp_broken.json" as *u8 as i64 161 resps[2] = "/tmp/feg_resp_broken.json" as *u8 as i64 162 caps[0] = "/tmp/feg_cap_t3_c1.txt" as *u8 as i64 163 caps[1] = "/tmp/feg_cap_t3_c2.txt" as *u8 as i64 164 caps[2] = "/tmp/feg_cap_t3_c3.txt" as *u8 as i64 165 let r3: i64 = feg_case("nx_feg_t3" as *u8, 3, resps, caps, 17853, 1, 2, 800) 166 var t3ok: i64 = 0 167 if r3 + 40 == 0 { t3ok = 1 } 168 if t3ok == 1 { pass = pass + 1; std_putln("T3 PASS NEG all-broken exhausts rc=-40 (no fake GREEN)" as *u8) } 169 if t3ok == 0 { std_puts("T3 FAIL rc=" as *u8); std_pdec(r3); std_puts("\n" as *u8) } 170 std_puts("FORGE-ENGINE-GATE pass=" as *u8) 171 std_pdec(pass) 172 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 173 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 174 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 175 let ctr__dry: *i64 = gv_ctr() 176 ctr__dry[0] = pass 177 ctr__dry[1] = 6 178 let rc__dry: i64 = gv_verdict("FORGE-ENGINE-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 179 sys_exit(rc__dry) 180 return rc__dry 181}