code wiki / _hdl_build / nx_ale_aggregate.nx

nx_ale_aggregate.nx source

↩ module page · 199 lines · 7377 B

1// nx_ale_aggregate.nx -- sovereign ALE AGGREGATE fold organ (rung ALE-R1c). 2// ALE's HEADLINE measure: folds N per-task milli-scores (each a deterministic [0,1000] 3// grade from nx_ale_grade) into ONE clamped mean on [0,1000] -- the single capability 4// number on the leaderboard (frontier ~26% = ~260 milli). PURE function of one file: 5// argv[1] = FIXTURE path (per-task lines "task|<id>|<milli>"; # / blanks ignored) 6// argv[2] = scratch OUT path: the organ writes 7// agg|<agg>\n # the single clamped headline mean 8// task|<id>|<milli>\n x N # re-foldable per-task breakdown 9// so the gate reads back the aggregate + breakdown WITHOUT hitting the 10// 0..255 process-exit-code ceiling. 11// FOLD (milli-units, 1000 == 1.0), data-driven, NO magic numbers: 12// per-task milli is clamped to [0,1000] ON READ (defensive at the boundary, rule 12: 13// a corrupt out-of-range per-task value can never inflate the aggregate) 14// sum = sum over tasks of clamp(milli, 0, 1000) # data-driven, from the fixture 15// N = count of "task|" lines in the fixture # the fixture owns the count 16// mean = sum / N # integer division (floor) 17// agg = clamp(mean, 0, 1000) # BOUNDED into [0,1] inclusive 18// N == 0 -> agg 0 (degenerate empty task set; no divide-by-zero) 19// Deterministic by construction: no clock, no rand -- only the fixture decides the agg. 20// Landmines respected: nested ifs (no &&/||), flat exprs, <=6 args/func, no empty-string 21// literal, strings via Write, openat_wr has no O_TRUNC (scratch is fresh per gate run). 22// license_tier: ORIGINAL 23import "nx_syscalls.nx" 24const K_MAGIC_262144: i64 = 262144 25 26// read whole file at path into buf (cap), return byte count (0 on open-fail) 27func aa_read(path: *u8, buf: *u8, cap: i64) -> i64 { 28 let fd: i64 = sys_openat_rd(path) 29 if fd < 0 { return 0 } 30 var n: i64 = 0 31 var go: i64 = 1 32 while go == 1 { 33 let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n) 34 if r <= 0 { go = 0 } else { n = n + r } 35 if n >= cap - 1 { go = 0 } 36 } 37 sys_close(fd) 38 return n 39} 40 41// length of a C string 42func aa_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 43 44// is pos a line start in buf? (pos==0 or previous byte is newline) 1/0 45func aa_is_bol(buf: *u8, pos: i64) -> i64 { 46 if pos == 0 { return 1 } 47 if buf[pos - 1] == (10 as u8) { return 1 } 48 return 0 49} 50 51// index of the end of the line starting at start (the newline index, or n at EOF) 52func aa_line_end(buf: *u8, n: i64, start: i64) -> i64 { 53 var e: i64 = start 54 while e < n { 55 if buf[e] == (10 as u8) { return e } 56 e = e + 1 57 } 58 return n 59} 60 61// does buf[pos .. pos+plen) byte-equal pfx[0 .. plen)? 1/0 62func aa_pfx_eq(pfx: *u8, plen: i64, buf: *u8, pos: i64) -> i64 { 63 var k: i64 = 0 64 while k < plen { 65 if pfx[k] != buf[pos + k] { return 0 } 66 k = k + 1 67 } 68 return 1 69} 70 71// in a "task|<id>|<milli>" line buf[start..end), find the index just AFTER the 2nd '|' 72// (the start of the <milli> field). returns -1 if fewer than two '|' before end. 73func aa_milli_start(buf: *u8, start: i64, end: i64) -> i64 { 74 var bars: i64 = 0 75 var j: i64 = start 76 while j < end { 77 if buf[j] == (124 as u8) { bars = bars + 1; if bars == 2 { return j + 1 } } 78 j = j + 1 79 } 80 return 0 - 1 81} 82 83// parse the non-negative decimal integer in buf[from..end); -1 if no digit seen. 84func aa_parse_int(buf: *u8, from: i64, end: i64) -> i64 { 85 var seen: i64 = 0 86 var v: i64 = 0 87 var j: i64 = from 88 while j < end { 89 let c: i64 = buf[j] as i64 90 if c >= 48 { if c <= 57 { v = (v * 10) + (c - 48); seen = 1 } else { j = end } } else { if seen == 1 { j = end } } 91 j = j + 1 92 } 93 if seen == 0 { return 0 - 1 } 94 return v 95} 96 97// clamp v into [0,1000] inclusive 98func aa_clamp(v: i64) -> i64 { 99 var c: i64 = v 100 if c < 0 { c = 0 } 101 if c > 1000 { c = 1000 } 102 return c 103} 104 105// write decimal v (>=0) to fd (no newline) 106func aa_wd(fd: i64, v: i64) -> i64 { 107 let bb: *u8 = sys_mmap(28) 108 var m: i64 = v 109 if m < 0 { m = 0 - m } 110 let t: *u8 = sys_mmap(28) 111 var k: i64 = 0 112 if m == 0 { t[0] = 48; k = 1 } 113 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 114 var i: i64 = 0 115 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 116 sys_write(fd, bb, k) 117 return 0 118} 119 120func main(argc: i64, argv: *i64) -> i64 { 121 if argc < 3 { sys_exit(2); return 2 } 122 let fixp: *u8 = argv[1] as *u8 123 let outp: *u8 = argv[2] as *u8 124 125 let fbuf: *u8 = sys_mmap(K_MAGIC_262144) 126 let fn: i64 = aa_read(fixp, fbuf, K_MAGIC_262144) 127 128 let pfx: *u8 = "task|" as *u8 129 let pl: i64 = aa_len(pfx) 130 131 // PASS 1: fold -- count N task| lines and sum clamped per-task milli scores. 132 var total: i64 = 0 133 var sum: i64 = 0 134 var i: i64 = 0 135 while i < fn { 136 if aa_is_bol(fbuf, i) == 1 { 137 var istask: i64 = 0 138 if i + pl <= fn { 139 if aa_pfx_eq(pfx, pl, fbuf, i) == 1 { istask = 1 } 140 } 141 if istask == 1 { 142 let lineend: i64 = aa_line_end(fbuf, fn, i) 143 let ms: i64 = aa_milli_start(fbuf, i, lineend) 144 if ms >= 0 { 145 let raw: i64 = aa_parse_int(fbuf, ms, lineend) 146 if raw >= 0 { 147 total = total + 1 148 // per-task clamp ON READ: corrupt out-of-range value can't inflate 149 sum = sum + aa_clamp(raw) 150 } 151 } 152 i = lineend 153 } 154 } 155 i = i + 1 156 } 157 158 // FOLD: mean = sum / N (integer floor); BOUNDED into [0,1000]; N==0 -> 0 (degenerate) 159 var mean: i64 = 0 160 if total > 0 { mean = sum / total } 161 let agg: i64 = aa_clamp(mean) 162 163 // OUTPUT: agg line + per-task breakdown (re-foldable). Fresh scratch each gate run. 164 let out: i64 = sys_openat_wr(outp, 0x1a4) 165 if out < 0 { sys_exit(3); return 3 } 166 sys_write(out, "agg|" as *u8, 4) 167 aa_wd(out, agg) 168 sys_write(out, "\n" as *u8, 1) 169 170 // PASS 2: re-emit the clamped per-task breakdown, byte-for-byte from the same source, 171 // so the gate can re-fold the emitted task| lines back to the SAME mean. 172 var j: i64 = 0 173 while j < fn { 174 if aa_is_bol(fbuf, j) == 1 { 175 var istask2: i64 = 0 176 if j + pl <= fn { 177 if aa_pfx_eq(pfx, pl, fbuf, j) == 1 { istask2 = 1 } 178 } 179 if istask2 == 1 { 180 let lineend2: i64 = aa_line_end(fbuf, fn, j) 181 let ms2: i64 = aa_milli_start(fbuf, j, lineend2) 182 if ms2 >= 0 { 183 let raw2: i64 = aa_parse_int(fbuf, ms2, lineend2) 184 if raw2 >= 0 { 185 // emit "task|<id>|<clamped-milli>" : copy "task|<id>|" verbatim then clamped value 186 sys_write(out, (fbuf as i64 + j) as *u8, ms2 - j) 187 aa_wd(out, aa_clamp(raw2)) 188 sys_write(out, "\n" as *u8, 1) 189 } 190 } 191 j = lineend2 192 } 193 } 194 j = j + 1 195 } 196 sys_close(out) 197 sys_exit(0) 198 return 0 199}