code wiki / _hdl_build / nx_swebv_grade.nx

nx_swebv_grade.nx source

↩ module page · 158 lines · 6777 B

1// nx_swebv_grade.nx -- F787 rung-3 (sovereign half): THE SWE-bench Verified GRADER, Nishi first 2// byte up (operator law 2026-07-20: "we only use python or other stuff as oracles, nishi from the 3// first byte up"). Python/pytest NEVER computes the score -- it is an ORACLE that writes one verdict 4// row per instance into the results plane; THIS organ owns the contract: denominator = the swebvinst- 5// index (500 public instances), numerator = resolved rows in swebvres-, UNGRADED COUNTS AS UNRESOLVED 6// (the leaderboard contract), score = resolved*100/denominator, published atomically to the evidence 7// layer. HONESTY BY CONSTRUCTION: zero oracle rows -> verdict=UNMEASURED (never a fabricated 0 or 8// omission); missing instance index -> verdict=RED fail-loud. 9// nx_swebv_grade [instprefix] [resprefix] [outpath] 10// defaults: knowledge/store/swebvinst- knowledge/store/swebvres- knowledge/status/swebv_grade.log 11// results row contract: instance_id<TAB>verdict(resolved|unresolved|error)<TAB>epoch<TAB>oracle-id 12// (maintained via nx_store_put with id=instance_id -> id-replace = dedup at the store layer) 13// expect_exit: 0 (GREEN|UNMEASURED) | 1 RED license_tier: ORIGINAL No hw writes (Rule 26). 14import "nx_store_seed_lib.nx" 15import "nx_seg_store.nx" 16import "nx_syscalls.nx" 17 18const SG_CAP: i64 = 1048576 19const SG_OCAP: i64 = 8192 20const SG_PATHCAP: i64 = 256 21const SG_TAB: i64 = 9 22const SG_NL: i64 = 10 23const SG_STDERR: i64 = 2 24const SG_MODE: i64 = 0x1a4 25const SG_D0: i64 = 48 26const SG_B10: i64 = 10 27const SG_NUMB: i64 = 24 28const SG_EXIT_RED: i64 = 1 29const SG_R1: i64 = 114 // 'r' of resolved 30const SG_E1: i64 = 101 // 'e' of error 31 32func sg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 33func sg_werr(s: *u8) -> i64 { sys_write(SG_STDERR, s, sg_slen(s)); return 0 } 34func sg_cat(d: *u8, off: i64, s: *u8) -> i64 { 35 var o: i64 = off 36 var j: i64 = 0 37 while s[j] != (0 as u8) { d[o] = s[j]; o = o + 1; j = j + 1 } 38 return o 39} 40func sg_catn(d: *u8, off: i64, v: i64) -> i64 { 41 var o: i64 = off 42 var m: i64 = v 43 if m == 0 { d[o] = SG_D0 as u8; return o + 1 } 44 if m < 0 { m = 0 - m } 45 let t: *u8 = sys_mmap(SG_NUMB) 46 var k: i64 = 0 47 while m > 0 { t[k] = (SG_D0 + (m % SG_B10)) as u8; m = m / SG_B10; k = k + 1 } 48 var i: i64 = 0 49 while i < k { d[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } 50 return o 51} 52// count non-empty lines in a loaded plane buffer 53func sg_lines(buf: *u8, n: i64) -> i64 { 54 var cnt: i64 = 0 55 var i: i64 = 0 56 while i < n { 57 var le: i64 = i 58 var s: i64 = 1 59 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (SG_NL as u8) { s = 0 } else { le = le + 1 } } } 60 if le > i { cnt = cnt + 1 } 61 i = le + 1 62 } 63 return cnt 64} 65// second column (verdict) first byte of each line: r=resolved e=error else unresolved 66static g_resolved: i64 67static g_error: i64 68static g_other: i64 69func sg_verdicts(buf: *u8, n: i64) -> i64 { 70 g_resolved = 0 71 g_error = 0 72 g_other = 0 73 var i: i64 = 0 74 while i < n { 75 var le: i64 = i 76 var s: i64 = 1 77 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (SG_NL as u8) { s = 0 } else { le = le + 1 } } } 78 if le > i { 79 // find first tab in [i,le) 80 var t: i64 = i 81 var s2: i64 = 1 82 while s2 == 1 { if t >= le { s2 = 0 } else { if buf[t] == (SG_TAB as u8) { s2 = 0 } else { t = t + 1 } } } 83 if t < le { 84 let c: i64 = buf[t+1] 85 if c == SG_R1 { g_resolved = g_resolved + 1 } 86 else { if c == SG_E1 { g_error = g_error + 1 } else { g_other = g_other + 1 } } 87 } 88 } 89 i = le + 1 90 } 91 return 0 92} 93 94func main(argc: i64, argv: *i64) -> i64 { 95 var ip: *u8 = "knowledge/store/swebvinst-" as *u8 96 var rp: *u8 = "knowledge/store/swebvres-" as *u8 97 var op: *u8 = "knowledge/status/swebv_grade.log" as *u8 98 if argc > 1 { ip = argv[1] as *u8 } 99 if argc > 2 { rp = argv[2] as *u8 } 100 if argc > 3 { op = argv[3] as *u8 } 101 102 let ib: *u8 = sys_mmap(SG_CAP) 103 let in_n: i64 = sts_load(ip, ib, SG_CAP - 16) 104 let denom: i64 = sg_lines(ib, in_n) 105 if denom <= 0 { sg_werr("SWEBV-GRADE FAIL verdict=RED instance index EMPTY (run nx_swebv_ingest first)\n" as *u8); sys_exit(SG_EXIT_RED); return SG_EXIT_RED } 106 107 let rb: *u8 = sys_mmap(SG_CAP) 108 let rn: i64 = sts_load(rp, rb, SG_CAP - 16) 109 var graded: i64 = 0 110 if rn > 0 { graded = sg_lines(rb, rn); sg_verdicts(rb, rn) } else { g_resolved = 0; g_error = 0; g_other = 0 } 111 112 let out: *u8 = sys_mmap(SG_OCAP) 113 var o: i64 = 0 114 if graded == 0 { 115 o = sg_cat(out, o, "SWEBV-GRADE verdict=UNMEASURED denom=" as *u8) 116 o = sg_catn(out, o, denom) 117 o = sg_cat(out, o, " graded=0 (no oracle verdict rows in swebvres- yet; oracle leg = F806; NO number is published to the card until fresh oracle runs exist -- honest-numbers law)\n" as *u8) 118 } else { 119 let pct: i64 = (g_resolved * 100) / denom 120 let cov: i64 = (graded * 100) / denom 121 o = sg_cat(out, o, "SWEBV-GRADE verdict=GREEN resolved=" as *u8) 122 o = sg_catn(out, o, g_resolved) 123 o = sg_cat(out, o, " unresolved=" as *u8) 124 o = sg_catn(out, o, g_other) 125 o = sg_cat(out, o, " error=" as *u8) 126 o = sg_catn(out, o, g_error) 127 o = sg_cat(out, o, " graded=" as *u8) 128 o = sg_catn(out, o, graded) 129 o = sg_cat(out, o, " denom=" as *u8) 130 o = sg_catn(out, o, denom) 131 o = sg_cat(out, o, " resolve_pct=" as *u8) 132 o = sg_catn(out, o, pct) 133 o = sg_cat(out, o, " coverage_pct=" as *u8) 134 o = sg_catn(out, o, cov) 135 o = sg_cat(out, o, " (ungraded counts as unresolved per the SWE-bench contract; score = resolved*100/denom; oracle-only law: pytest wrote verdicts, Nishi computed this)\n" as *u8) 136 } 137 o = sg_cat(out, o, "epoch=" as *u8) 138 o = sg_catn(out, o, sys_now_realtime_sec()) 139 o = sg_cat(out, o, " src_inst=" as *u8) 140 o = sg_cat(out, o, ip) 141 o = sg_cat(out, o, " src_res=" as *u8) 142 o = sg_cat(out, o, rp) 143 o = sg_cat(out, o, "\n" as *u8) 144 145 // atomic publish to the evidence layer 146 let tp: *u8 = sys_mmap(SG_PATHCAP) 147 var to: i64 = sg_cat(tp, 0, op) 148 to = sg_cat(tp, to, ".nxtmp" as *u8) 149 tp[to] = 0 as u8 150 let fd: i64 = sys_openat_wr(tp, SG_MODE) 151 if fd < 0 { sg_werr("SWEBV-GRADE FAIL verdict=RED publish-open\n" as *u8); sys_exit(SG_EXIT_RED); return SG_EXIT_RED } 152 sys_write(fd, out, o) 153 sys_close(fd) 154 if sys_renameat(tp, op) < 0 { sg_werr("SWEBV-GRADE FAIL verdict=RED publish-rename\n" as *u8); sys_exit(SG_EXIT_RED); return SG_EXIT_RED } 155 sys_write(SG_STDERR, out, o) 156 sys_exit(0) 157 return 0 158}