code wiki / _hdl_build / nx_swebv_tests.nx

nx_swebv_tests.nx source

↩ module page · 244 lines · 9796 B

1// nx_swebv_tests.nx -- F787 rung-4: sovereign per-instance TEST-CONTRACT extractor (oracle-only law). 2// From the 5 fetched pages -> knowledge/store/swebvtest- : instance_id<TAB>f2p_count<TAB>p2p_count<TAB>f2p_list 3// f2p_list = FAIL_TO_PASS pytest node ids (JSON-array-in-string, escape-preserved) = the DECISIVE tests 4// an accepted fix must flip red->green; p2p_count = PASS_TO_PASS regression breadth. pytest RUNS these 5// node ids, Nishi SELECTED them. Escape-aware value end (terminating quote = even backslash run) so 6// patch/test_patch bodies never mis-terminate. Row search BOUNDED to [row,next-row). 7// TOOTH: exactly 500 rows or verdict=RED, store untouched. Aggregate MEASURED stat -> status log. 8// expect_exit: 0 (GREEN) | 1 RED license_tier: ORIGINAL No hw writes (Rule 26). 9import "nx_store_seed_lib.nx" 10import "nx_seg_store.nx" 11import "nx_syscalls.nx" 12 13static g_out: *u8 14static g_o: i64 15static g_rows: i64 16static g_ve: i64 17static g_trunc: i64 18static g_sum_f2p: i64 19static g_sum_p2p: i64 20 21const VT_PREFIX: *u8 = "knowledge/store/swebvtest-" as *u8 22const VT_LOG: *u8 = "knowledge/status/swebv_tests.log" as *u8 23const VT_FCAP: i64 = 4194304 24const VT_OCAP: i64 = 1048576 25const VT_LISTCAP: i64 = 65536 26const VT_SBCAP: i64 = 4096 27const VT_PATHCAP: i64 = 256 28const VT_TAB: i64 = 9 29const VT_NL: i64 = 10 30const VT_Q: i64 = 34 31const VT_BSL: i64 = 92 32const VT_STDERR: i64 = 2 33const VT_MODE: i64 = 0x1a4 34const VT_WANT: i64 = 500 35const VT_D0: i64 = 48 36const VT_B10: i64 = 10 37const VT_NUMB: i64 = 24 38const VT_EXIT_IO: i64 = 1 39const VT_EXIT_RED: i64 = 3 40 41func vt_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 42func vt_werr(s: *u8) -> i64 { sys_write(VT_STDERR, s, vt_slen(s)); return 0 } 43func vt_wn(v: i64) -> i64 { 44 if v == 0 { sys_write(VT_STDERR, "0" as *u8, 1); return 0 } 45 var m: i64 = v 46 if m < 0 { sys_write(VT_STDERR, "-" as *u8, 1); m = 0 - m } 47 let d: *u8 = sys_mmap(VT_NUMB) 48 var k: i64 = 0 49 while m > 0 { d[k] = (VT_D0 + (m % VT_B10)) as u8; m = m / VT_B10; k = k + 1 } 50 let o: *u8 = sys_mmap(VT_NUMB) 51 var i: i64 = 0 52 while i < k { o[i] = d[k - 1 - i]; i = i + 1 } 53 sys_write(VT_STDERR, o, k) 54 return 0 55} 56func vt_read(path: *u8, buf: *u8, cap: i64) -> i64 { 57 let fd: i64 = sys_openat_rd(path) 58 if fd < 0 { return 0 - 1 } 59 var n: i64 = 0 60 var go: i64 = 1 61 while go == 1 { 62 let base: i64 = buf as i64 63 let r: i64 = sys_read(fd, (base + n) as *u8, cap - n) 64 if r <= 0 { go = 0 } else { n = n + r } 65 if n >= cap { go = 0 } 66 } 67 sys_close(fd) 68 return n 69} 70func vt_find(buf: *u8, needle: *u8, from: i64, limit: i64) -> i64 { 71 let m: i64 = vt_slen(needle) 72 if m == 0 { return 0 - 1 } 73 var i: i64 = from 74 while i + m <= limit { 75 var j: i64 = 0 76 var ok: i64 = 1 77 while j < m { if buf[i+j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } 78 if ok == 1 { if i > 0 { if buf[i-1] == (VT_BSL as u8) { ok = 0 } } } 79 if ok == 1 { return i } 80 i = i + 1 81 } 82 return 0 - 1 83} 84func vt_vend(buf: *u8, vs: i64, limit: i64) -> i64 { 85 var i: i64 = vs 86 while i < limit { 87 if buf[i] == (VT_Q as u8) { 88 var b: i64 = 0 89 var j: i64 = i - 1 90 var g: i64 = 1 91 while g == 1 { if j < vs { g = 0 } else { if buf[j] == (VT_BSL as u8) { b = b + 1; j = j - 1 } else { g = 0 } } } 92 if b % 2 == 0 { return i } 93 } 94 i = i + 1 95 } 96 return limit 97} 98func vt_countq(buf: *u8, a: i64, b: i64) -> i64 { 99 var c: i64 = 0 100 var i: i64 = a 101 while i + 1 < b { 102 if buf[i] == (VT_BSL as u8) { if buf[i+1] == (VT_Q as u8) { c = c + 1; i = i + 2 } else { i = i + 1 } } else { i = i + 1 } 103 } 104 return c 105} 106func vt_field(buf: *u8, needle: *u8, from: i64, limit: i64) -> i64 { 107 let p: i64 = vt_find(buf, needle, from, limit) 108 if p < 0 { return 0 - 1 } 109 let vs: i64 = p + vt_slen(needle) 110 g_ve = vt_vend(buf, vs, limit) 111 return vs 112} 113func vt_emitc(c: i64) -> i64 { if g_o < VT_OCAP - 2 { g_out[g_o] = c as u8; g_o = g_o + 1 } return 0 } 114func vt_emit_slice(buf: *u8, a: i64, b: i64) -> i64 { 115 var i: i64 = a 116 while i < b { if g_o < VT_OCAP - 2 { g_out[g_o] = buf[i]; g_o = g_o + 1 } i = i + 1 } 117 return 0 118} 119func vt_emit_slice_cap(buf: *u8, a: i64, b: i64, cap: i64) -> i64 { 120 var e: i64 = b 121 if b - a > cap { e = a + cap; g_trunc = g_trunc + 1 } 122 var i: i64 = a 123 while i < e { if g_o < VT_OCAP - 2 { g_out[g_o] = buf[i]; g_o = g_o + 1 } i = i + 1 } 124 return 0 125} 126func vt_emitn(v: i64) -> i64 { 127 var m: i64 = v 128 if m == 0 { if g_o < VT_OCAP - 2 { g_out[g_o] = VT_D0 as u8; g_o = g_o + 1 } return 0 } 129 if m < 0 { m = 0 - m } 130 let t: *u8 = sys_mmap(VT_NUMB) 131 var k: i64 = 0 132 while m > 0 { t[k] = (VT_D0 + (m % VT_B10)) as u8; m = m / VT_B10; k = k + 1 } 133 var i: i64 = 0 134 while i < k { if g_o < VT_OCAP - 2 { g_out[g_o] = t[k - 1 - i]; g_o = g_o + 1 } i = i + 1 } 135 return 0 136} 137func vt_bcat(d: *u8, off: i64, s: *u8) -> i64 { 138 var o: i64 = off 139 var j: i64 = 0 140 while s[j] != (0 as u8) { d[o] = s[j]; o = o + 1; j = j + 1 } 141 return o 142} 143func vt_bcatn(d: *u8, off: i64, v: i64) -> i64 { 144 var o: i64 = off 145 var m: i64 = v 146 if m == 0 { d[o] = VT_D0 as u8; return o + 1 } 147 if m < 0 { m = 0 - m } 148 let t: *u8 = sys_mmap(VT_NUMB) 149 var k: i64 = 0 150 while m > 0 { t[k] = (VT_D0 + (m % VT_B10)) as u8; m = m / VT_B10; k = k + 1 } 151 var i: i64 = 0 152 while i < k { d[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } 153 return o 154} 155func vt_scan(buf: *u8, n: i64) -> i64 { 156 var p: i64 = 0 157 var go: i64 = 1 158 while go == 1 { 159 let a: i64 = vt_find(buf, "\"row\":{\"repo\":\"" as *u8, p, n) 160 if a < 0 { go = 0 } else { 161 let nx: i64 = vt_find(buf, "\"row\":{\"repo\":\"" as *u8, a + 1, n) 162 var limit: i64 = n 163 if nx >= 0 { limit = nx } 164 let ivs: i64 = vt_field(buf, "\"instance_id\":\"" as *u8, a, limit) 165 let ive: i64 = g_ve 166 let fvs: i64 = vt_field(buf, "\"FAIL_TO_PASS\":\"" as *u8, a, limit) 167 let fve: i64 = g_ve 168 let pvs: i64 = vt_field(buf, "\"PASS_TO_PASS\":\"" as *u8, a, limit) 169 let pve: i64 = g_ve 170 if ivs >= 0 { if fvs >= 0 { if pvs >= 0 { 171 let fcq: i64 = vt_countq(buf, fvs, fve) 172 let fcount: i64 = fcq / 2 173 let pcq: i64 = vt_countq(buf, pvs, pve) 174 let pcount: i64 = pcq / 2 175 vt_emit_slice(buf, ivs, ive) 176 vt_emitc(VT_TAB) 177 vt_emitn(fcount) 178 vt_emitc(VT_TAB) 179 vt_emitn(pcount) 180 vt_emitc(VT_TAB) 181 vt_emit_slice_cap(buf, fvs, fve, VT_LISTCAP) 182 vt_emitc(VT_NL) 183 g_rows = g_rows + 1 184 g_sum_f2p = g_sum_f2p + fcount 185 g_sum_p2p = g_sum_p2p + pcount 186 } } } 187 if nx < 0 { go = 0 } else { p = nx } 188 } 189 } 190 return 0 191} 192func vt_page(path: *u8, fbuf: *u8) -> i64 { 193 let n: i64 = vt_read(path, fbuf, VT_FCAP) 194 if n <= 0 { vt_werr("SWEBV-TESTS page ABSENT: " as *u8); vt_werr(path); vt_werr("\n" as *u8); return 0 } 195 vt_scan(fbuf, n) 196 return 1 197} 198func main() -> i64 { 199 g_out = sys_mmap(VT_OCAP) 200 g_o = 0 201 g_rows = 0 202 g_trunc = 0 203 g_sum_f2p = 0 204 g_sum_p2p = 0 205 let fbuf: *u8 = sys_mmap(VT_FCAP) 206 var pages: i64 = 0 207 pages = pages + vt_page("knowledge/fetched/swebv_rows_000.raw" as *u8, fbuf) 208 pages = pages + vt_page("knowledge/fetched/swebv_rows_100.raw" as *u8, fbuf) 209 pages = pages + vt_page("knowledge/fetched/swebv_rows_200.raw" as *u8, fbuf) 210 pages = pages + vt_page("knowledge/fetched/swebv_rows_300.raw" as *u8, fbuf) 211 pages = pages + vt_page("knowledge/fetched/swebv_rows_400.raw" as *u8, fbuf) 212 if pages < 5 { vt_werr("SWEBV-TESTS FAIL verdict=RED pages=" as *u8); vt_wn(pages); vt_werr("/5\n" as *u8); sys_exit(VT_EXIT_IO); return VT_EXIT_IO } 213 if g_rows != VT_WANT { vt_werr("SWEBV-TESTS FAIL verdict=RED rows=" as *u8); vt_wn(g_rows); vt_werr(" want=500 (store untouched, fail-closed)\n" as *u8); sys_exit(VT_EXIT_RED); return VT_EXIT_RED } 214 let rc: i64 = sts_seed(VT_PREFIX, g_out, g_o) 215 if rc < 0 { vt_werr("SWEBV-TESTS FAIL verdict=RED store-commit\n" as *u8); sys_exit(VT_EXIT_IO); return VT_EXIT_IO } 216 let sb: *u8 = sys_mmap(VT_SBCAP) 217 var o: i64 = vt_bcat(sb, 0, "SWEBV-TESTS OK verdict=GREEN rows=" as *u8) 218 o = vt_bcatn(sb, o, g_rows) 219 o = vt_bcat(sb, o, " f2p_total=" as *u8) 220 o = vt_bcatn(sb, o, g_sum_f2p) 221 o = vt_bcat(sb, o, " p2p_total=" as *u8) 222 o = vt_bcatn(sb, o, g_sum_p2p) 223 o = vt_bcat(sb, o, " f2p_avg=" as *u8) 224 o = vt_bcatn(sb, o, g_sum_f2p / g_rows) 225 o = vt_bcat(sb, o, " p2p_avg=" as *u8) 226 o = vt_bcatn(sb, o, g_sum_p2p / g_rows) 227 o = vt_bcat(sb, o, " f2p_list_truncated=" as *u8) 228 o = vt_bcatn(sb, o, g_trunc) 229 o = vt_bcat(sb, o, " plane_bytes=" as *u8) 230 o = vt_bcatn(sb, o, g_o) 231 o = vt_bcat(sb, o, " (MEASURED from the 500-instance public Verified corpus; FAIL_TO_PASS=decisive, PASS_TO_PASS=regression breadth; oracle-only: pytest runs these, Nishi selected them)\n" as *u8) 232 let tp: *u8 = sys_mmap(VT_PATHCAP) 233 var to: i64 = vt_bcat(tp, 0, VT_LOG) 234 to = vt_bcat(tp, to, ".nxtmp" as *u8) 235 tp[to] = 0 as u8 236 let fd: i64 = sys_openat_wr(tp, VT_MODE) 237 if fd < 0 { vt_werr("SWEBV-TESTS FAIL verdict=RED publish-open\n" as *u8); sys_exit(VT_EXIT_IO); return VT_EXIT_IO } 238 sys_write(fd, sb, o) 239 sys_close(fd) 240 if sys_renameat(tp, VT_LOG) < 0 { vt_werr("SWEBV-TESTS FAIL verdict=RED publish-rename\n" as *u8); sys_exit(VT_EXIT_IO); return VT_EXIT_IO } 241 sys_write(VT_STDERR, sb, o) 242 sys_exit(0) 243 return 0 244}