code wiki / _hdl_build / nx_auto_builder_s20.nx

nx_auto_builder_s20.nx source

↩ module page · 419 lines · 17615 B

1// nx_auto_builder.nx -- the BUILDER's SELF-BUILD organ (the S-class-exceed rung of build-code 2// synthesis): a NEW CAPABILITY IS A SPEC FILE. Reads a build-spec grammar, routes the shape via 3// the CLASSIFIER (no human/Claude shape decision), dispatches the right pattern emitter, then 4// builds AND RUNS the authored KAT sovereignly -- spec in, gated capability out, hands-off. 5// name <module> the authored module name (paths derived) 6// feature <fname> classifier feature vector rows (in_table, in_state, ...) 7// param <ints...> shape-specific parameter vector, meaning per routed shape: 8// shape 12 WIRE_TLV: type_w len_w type_first len_le pad2 trail_w (pe7 v3) 9// shape 13 STRUCT_WALK: nfields k0 w0 k1 w1 ... (pe8 v2) 10// shape 18 STATE_MACHINE nstates nevents s0 t[0] t[1] ... (pe13) 11// VERDICTS (honest, exit-coded): GREEN(0) authored + KAT green; RED(1) KAT failed; 12// REFUSED(2) malformed spec or emitter rail refused; NEEDS-TUTOR(3) classifier routed a shape 13// this organ has no dispatch row for (NOVEL stays measured, never silently absorbed). 14// Durable: AUTOBUILD rows w/ measured ms -> knowledge/status/auto_builder.log. 15// EXCEEDS manual authoring: zero marginal Claude lines per capability, ms-scale latency, 16// byte-REPRODUCIBLE output (same spec -> identical module), classifier-routed dispatch. 17// Usage: nx_auto_builder <specpath> 18// LAWS: struct-free, integer-only, flat ifs (per-row parse helpers), no &&/||, <=6 args. 19// license_tier: ORIGINAL 20import "nx_pattern_classify.nx" 21import "nx_pattern_emit7.nx" 22import "nx_pattern_emit8.nx" 23import "nx_pattern_emit10.nx" 24import "nx_pattern_emit13.nx" 25import "nx_pattern_emit14.nx" 26import "nx_pattern_emit6.nx" 27import "nx_pattern_emit6_erf.nx" 28import "nx_syscalls.nx" 29const K_MAGIC_1000000: i64 = 1000000 30const K_MAGIC_2048: i64 = 2048 31 32func ab_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd, s, n); return 0 } 33func ab_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 34 35func ab_ms() -> i64 { 36 let ts: *i64 = sys_mmap(16) as *i64 37 sys_clock_gettime_mono(ts) 38 return ts[0] * 1000 + ts[1] / K_MAGIC_1000000 39} 40 41func ab_cat(dst: *u8, o: i64, s: *u8) -> i64 { 42 var i: i64 = 0 43 while s[i] != (0 as u8) { dst[o + i] = s[i]; i = i + 1 } 44 dst[o + i] = 0 as u8 45 return o + i 46} 47 48// ---- spec tokenizer (the proven flag pattern) ---- 49func ab_tok(buf: *u8, ls: i64, le: i64, idx: i64, se: *i64) -> i64 { 50 var i: i64 = ls 51 var cur: i64 = 0 52 var scanning: i64 = 1 53 var ret: i64 = 0 54 while scanning == 1 { 55 var skipping: i64 = 1 56 while skipping == 1 { 57 if i >= le { skipping = 0 } 58 if skipping == 1 { if buf[i] != (32 as u8) { skipping = 0 } } 59 if skipping == 1 { i = i + 1 } 60 } 61 if i >= le { scanning = 0 } 62 if scanning == 1 { 63 var j: i64 = i 64 var walking: i64 = 1 65 while walking == 1 { 66 if j >= le { walking = 0 } 67 if walking == 1 { if buf[j] == (32 as u8) { walking = 0 } } 68 if walking == 1 { j = j + 1 } 69 } 70 if cur == idx { se[0] = i; se[1] = j; ret = 1; scanning = 0 } 71 if scanning == 1 { cur = cur + 1; i = j } 72 } 73 } 74 return ret 75} 76 77func ab_teq(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 { 78 var n: i64 = 0 79 while lit[n] != (0 as u8) { n = n + 1 } 80 if e - s != n { return 0 } 81 var i: i64 = 0 82 while i < n { 83 if buf[s + i] != lit[i] { return 0 } 84 i = i + 1 85 } 86 return 1 87} 88 89// signed decimal token (build specs carry -1 table entries) 90func ab_tint(buf: *u8, s: i64, e: i64) -> i64 { 91 var v: i64 = 0 92 var neg: i64 = 0 93 var i: i64 = s 94 if buf[i] == (45 as u8) { neg = 1; i = i + 1 } 95 while i < e { 96 let c: i64 = buf[i] as i64 97 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } 98 i = i + 1 99 } 100 if neg == 1 { return 0 - v } 101 return v 102} 103 104func ab_tcopy(buf: *u8, s: i64, e: i64, pool: *u8, pp: *i64) -> i64 { 105 let base: i64 = (pool as i64) + pp[0] 106 var i: i64 = 0 107 while s + i < e { pool[pp[0] + i] = buf[s + i]; i = i + 1 } 108 pool[pp[0] + i] = 0 as u8 109 pp[0] = pp[0] + i + 1 110 return base 111} 112 113// feature name -> classifier index (the PLC_ contract as data); -1 unknown 114func ab_feat_idx(buf: *u8, s: i64, e: i64) -> i64 { 115 if ab_teq(buf, s, e, "in_bytes" as *u8) == 1 { return 0 } 116 if ab_teq(buf, s, e, "in_array" as *u8) == 1 { return 1 } 117 if ab_teq(buf, s, e, "in_pair" as *u8) == 1 { return 2 } 118 if ab_teq(buf, s, e, "in_state" as *u8) == 1 { return 3 } 119 if ab_teq(buf, s, e, "in_table" as *u8) == 1 { return 4 } 120 if ab_teq(buf, s, e, "in_coords" as *u8) == 1 { return 5 } 121 if ab_teq(buf, s, e, "has_time" as *u8) == 1 { return 6 } 122 if ab_teq(buf, s, e, "has_plant" as *u8) == 1 { return 7 } 123 if ab_teq(buf, s, e, "out_verdict" as *u8) == 1 { return 8 } 124 if ab_teq(buf, s, e, "out_value" as *u8) == 1 { return 9 } 125 if ab_teq(buf, s, e, "is_meta" as *u8) == 1 { return 10 } 126 if ab_teq(buf, s, e, "in_coeffs" as *u8) == 1 { return 11 } 127 if ab_teq(buf, s, e, "in_framed" as *u8) == 1 { return 12 } 128 if ab_teq(buf, s, e, "in_marked" as *u8) == 1 { return 13 } 129 if ab_teq(buf, s, e, "has_inverse" as *u8) == 1 { return 14 } 130 return 0 - 1 131} 132 133// parse one spec line into (namep, feats, params); returns 0 ok, -1 malformed 134func ab_parse_line(spec: *u8, ls: i64, le: i64, st: *i64) -> i64 { 135 // st[0]=name ptr st[1]=nparams st[2]=feats ptr st[3]=params ptr st[4]=pool ptr st[5]=pp ptr 136 if le <= ls { return 0 } 137 if spec[ls] == (35 as u8) { return 0 } 138 let se: *i64 = sys_mmap(32) as *i64 139 if ab_tok(spec, ls, le, 0, se) != 1 { return 0 } 140 let k0s: i64 = se[0] 141 let k0e: i64 = se[1] 142 if ab_teq(spec, k0s, k0e, "name" as *u8) == 1 { 143 if ab_tok(spec, ls, le, 1, se) != 1 { return 0 - 1 } 144 st[0] = ab_tcopy(spec, se[0], se[1], st[4] as *u8, st[5] as *i64) 145 return 0 146 } 147 if ab_teq(spec, k0s, k0e, "feature" as *u8) == 1 { 148 if ab_tok(spec, ls, le, 1, se) != 1 { return 0 - 1 } 149 let fi: i64 = ab_feat_idx(spec, se[0], se[1]) 150 if fi < 0 { return 0 - 1 } 151 let f: *i64 = st[2] as *i64 152 f[fi] = 1 153 return 0 154 } 155 if ab_teq(spec, k0s, k0e, "specfile" as *u8) == 1 { 156 if ab_tok(spec, ls, le, 1, se) != 1 { return 0 - 1 } 157 st[6] = ab_tcopy(spec, se[0], se[1], st[4] as *u8, st[5] as *i64) 158 return 0 159 } 160 if ab_teq(spec, k0s, k0e, "param" as *u8) == 1 { 161 let p: *i64 = st[3] as *i64 162 var ti: i64 = 1 163 var going: i64 = 1 164 while going == 1 { 165 if ab_tok(spec, ls, le, ti, se) != 1 { going = 0 } 166 if going == 1 { 167 if st[1] >= 300 { return 0 - 1 } 168 p[st[1]] = ab_tint(spec, se[0], se[1]) 169 st[1] = st[1] + 1 170 ti = ti + 1 171 } 172 } 173 return 0 174 } 175 return 0 176} 177 178// dispatch rows: route a classified shape to its emitter author. Adding emitter 179// support = adding a row function (rule 11 in code form: one row, one shape). 180func ab_author_12(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64) -> i64 { 181 if np < 6 { return 0 } 182 return pe7_author_wire_tlv3(name, mp, tp, p) 183} 184func ab_author_13(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64) -> i64 { 185 if np < 1 { return 0 } 186 let nf: i64 = p[0] 187 if np < 1 + nf * 2 { return 0 } 188 let kinds: *i64 = sys_mmap(8 * 32) as *i64 189 let widths: *i64 = sys_mmap(8 * 32) as *i64 190 var i: i64 = 0 191 while i < nf { kinds[i] = p[1 + i * 2]; widths[i] = p[2 + i * 2]; i = i + 1 } 192 return pe8_author_struct_walk2(name, mp, tp, kinds, widths, nf) 193} 194func ab_author_18(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64) -> i64 { 195 if np < 3 { return 0 } 196 if np < 3 + p[0] * p[1] { return 0 } 197 return pe13_author_state_machine(name, mp, tp, p) 198} 199// THRESHOLD_TABLE (shape 4) -- emitter-of-emitters bootstrap: pe14 PERMIL_VERDICT bakes 200// permil=count*1000/total + an ordered verdict-band ladder from a DATA table. Fills the 201// classifier PL_THRESHOLD_TABLE route (in_table+out_value) that had no emitter. 202// p[0]=total p[1]=nbands p[2+2i]=threshold_permil p[3+2i]=verdict_code. 203func ab_author_4(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64) -> i64 { 204 if np < 4 { return 0 } 205 if np < 2 + p[1] * 2 { return 0 } 206 return pe14_author_permil_verdict(name, mp, tp, p) 207} 208// WIRE_MARKER (shape 15, X-GALX-001 wiring): route a PLC_IN_MARKED spec to the existing 209// pe10 prefix-marker emitter. Param vector: p[0]=prefix p[1]=count p[2..2+count]=marker table 210// p[2+count]=terminator. sa[] is repacked as [count, m0, m1, ...] for pe10's rail. 211// NOTE: pe10 bakes a JPEG-class frame (1-byte prefix + 1-byte marker + 2-byte BE inclusive 212// length). It does NOT cover the PNG chunk frame (4-byte BE exclusive len + 4-byte ASCII type 213// + crc32); a PNG tEXt/iTXt reader needs a distinct emitter shape (still NEEDS-TUTOR). 214func ab_author_15(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64) -> i64 { 215 if np < 3 { return 0 } 216 let count: i64 = p[1] 217 if count < 1 { return 0 } 218 if np < 3 + count { return 0 } 219 let sa: *i64 = sys_mmap(8 * 16) as *i64 220 sa[0] = count 221 var i: i64 = 0 222 while i < count { sa[1 + i] = p[2 + i]; i = i + 1 } 223 let term: i64 = p[2 + count] 224 return pe10_author_wire_marker(name, mp, tp, p[0], sa, term) 225} 226// MATH_KERNEL (shape 11, X-AUT-004): the Builder is GIVEN what to emit -- a template 227// id + the oracle CONSTANT TABLE from a GENERATED spec file (the nx_mathspec_* 228// emission format: "c[<i>] = <int>" rows, negatives as "0 - <int>"). No grammar 229// strings beyond the path; every number stays oracle-derived. 230func ab_load_spec(path: *u8, c: *i64) -> i64 { 231 let lenp: *i64 = sys_mmap(16) as *i64 232 let fb: *u8 = sys_read_file(path, lenp) 233 let n: i64 = lenp[0] 234 if n <= 0 { return 0 } 235 var cnt: i64 = 0 236 var ls: i64 = 0 237 while ls < n { 238 var le: i64 = ls 239 var s: i64 = 1 240 while s == 1 { if le >= n { s = 0 } else { if fb[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 241 var i: i64 = ls 242 var pos: i64 = 0 - 1 243 while i + 1 < le { 244 if fb[i] == (99 as u8) { if fb[i+1] == (91 as u8) { pos = i + 2; i = le } } 245 i = i + 1 246 } 247 if pos >= 0 { 248 var idx: i64 = 0 249 var j: i64 = pos 250 var dig: i64 = 1 251 while dig == 1 { 252 if j >= le { dig = 0 } 253 else { if fb[j] < (48 as u8) { dig = 0 } else { if fb[j] > (57 as u8) { dig = 0 } else { idx = idx * 10 + ((fb[j] as i64) - 48); j = j + 1 } } } 254 } 255 var eq: i64 = 0 - 1 256 while j < le { if fb[j] == (61 as u8) { eq = j; j = le } else { j = j + 1 } } 257 if eq >= 0 { 258 var k2: i64 = eq + 1 259 var sk: i64 = 1 260 while sk == 1 { if k2 >= le { sk = 0 } else { if fb[k2] == (32 as u8) { k2 = k2 + 1 } else { sk = 0 } } } 261 var negv: i64 = 0 262 if k2 + 2 < le { if fb[k2] == (48 as u8) { if fb[k2+1] == (32 as u8) { if fb[k2+2] == (45 as u8) { negv = 1; k2 = k2 + 4 } } } } 263 var v: i64 = 0 264 var dg: i64 = 1 265 while dg == 1 { 266 if k2 >= le { dg = 0 } 267 else { if fb[k2] < (48 as u8) { dg = 0 } else { if fb[k2] > (57 as u8) { dg = 0 } else { v = v * 10 + ((fb[k2] as i64) - 48); k2 = k2 + 1 } } } 268 } 269 if negv == 1 { v = 0 - v } 270 if idx < 64 { c[idx] = v; if idx + 1 > cnt { cnt = idx + 1 } } 271 } 272 } 273 ls = le + 1 274 } 275 return cnt 276} 277func ab_author_11(name: *u8, mp: *u8, tp: *u8, p: *i64, np: i64, sf: *u8) -> i64 { 278 if np < 1 { return 0 } 279 if (sf as i64) == 0 { return 0 } 280 let c: *i64 = sys_mmap(8 * 64) as *i64 281 let cnt: i64 = ab_load_spec(sf, c) 282 if cnt < 4 { return 0 } 283 let mfn: *u8 = sys_mmap(256) 284 let o2: i64 = ab_cat(mfn, 0, name) 285 ab_cat(mfn, o2, ".nx" as *u8) 286 if p[0] == 1 { 287 if np < 2 { return 0 } 288 return pe6_author_logf64(name, mfn, mp, tp, c, p[1]) 289 } 290 if p[0] == 2 { 291 if cnt < 38 { return 0 } 292 return pe6_author_erf(mfn, mp, tp, c) 293 } 294 return 0 295} 296 297// build + RUN the authored KAT through the sovereign runner; returns wait status 298func ab_kat(testname: *u8) -> i64 { 299 let pid: i64 = sys_fork() 300 if pid == 0 { 301 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 302 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 303 let argv: *i64 = sys_mmap(32) as *i64 304 argv[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64 305 argv[1] = testname as i64 306 argv[2] = 0 307 let envp: *i64 = sys_mmap(16) as *i64 308 envp[0] = 0 309 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp) 310 sys_exit(127) 311 } 312 let st: *i64 = sys_mmap(16) as *i64 313 sys_wait4(pid, st, 0) 314 return st[0] 315} 316 317func ab_log(name: *u8, shape: i64, ms: i64, verdict: *u8) -> i64 { 318 let lfd: i64 = sys_openat_append("knowledge/status/auto_builder.log" as *u8, 0x1a4) 319 if lfd < 0 { return 0 - 1 } 320 ab_w(lfd, "AUTOBUILD name=" as *u8); ab_w(lfd, name) 321 ab_w(lfd, " shape=" as *u8); ab_wn(lfd, shape) 322 ab_w(lfd, " ms=" as *u8); ab_wn(lfd, ms) 323 ab_w(lfd, " verdict=" as *u8); ab_w(lfd, verdict); ab_w(lfd, "\n" as *u8) 324 sys_close(lfd) 325 return 0 326} 327 328func main(argc: i64, argv: *i64) -> i64 { 329 if argc < 2 { ab_w(1, "usage: nx_auto_builder <specpath>\n" as *u8); sys_exit(2); return 2 } 330 let t0: i64 = ab_ms() 331 let sp: *u8 = argv[1] as *u8 332 let lenp: *i64 = sys_mmap(16) as *i64 333 let spec: *u8 = sys_read_file(sp, lenp) 334 let sn: i64 = lenp[0] 335 if sn <= 0 { 336 ab_w(1, "AUTO-BUILD REFUSED: spec missing " as *u8); ab_w(1, sp); ab_w(1, "\n" as *u8) 337 ab_log("(missing)" as *u8, 0, 0, "REFUSED" as *u8) 338 sys_exit(2); return 2 339 } 340 let st: *i64 = sys_mmap(8 * 8) as *i64 341 let feats: *i64 = plc_new_features() 342 let params: *i64 = sys_mmap(8 * 320) as *i64 343 let pool: *u8 = sys_mmap(K_MAGIC_2048) 344 let pp: *i64 = sys_mmap(16) as *i64 345 st[0] = 0; st[1] = 0; st[6] = 0 346 st[2] = feats as i64; st[3] = params as i64; st[4] = pool as i64; st[5] = pp as i64 347 var ls: i64 = 0 348 var bad: i64 = 0 349 while ls < sn { 350 var le: i64 = ls 351 var seeking: i64 = 1 352 while seeking == 1 { 353 if le >= sn { seeking = 0 } 354 if seeking == 1 { if spec[le] == (10 as u8) { seeking = 0 } } 355 if seeking == 1 { le = le + 1 } 356 } 357 if ab_parse_line(spec, ls, le, st) < 0 { bad = 1 } 358 ls = le + 1 359 } 360 if st[0] == 0 { bad = 1 } 361 if bad == 1 { 362 ab_w(1, "AUTO-BUILD REFUSED: malformed spec (name row + known features required)\n" as *u8) 363 ab_log("(malformed)" as *u8, 0, ab_ms() - t0, "REFUSED" as *u8) 364 sys_exit(2); return 2 365 } 366 let name: *u8 = st[0] as *u8 367 368 // the classifier routes the shape -- no human/Claude shape decision 369 let shape: i64 = plc_match_spec(feats) 370 ab_w(1, "AUTO-BUILD: " as *u8); ab_w(1, name) 371 ab_w(1, " classified shape=" as *u8); ab_wn(1, shape); ab_w(1, "\n" as *u8) 372 373 // derive authored paths 374 let mp: *u8 = sys_mmap(256) 375 var o: i64 = ab_cat(mp, 0, "runtime/_hdl_build/" as *u8) 376 o = ab_cat(mp, o, name) 377 ab_cat(mp, o, ".nx" as *u8) 378 let tp: *u8 = sys_mmap(256) 379 o = ab_cat(tp, 0, "runtime/_hdl_build/" as *u8) 380 o = ab_cat(tp, o, name) 381 ab_cat(tp, o, "_test.nx" as *u8) 382 let tn: *u8 = sys_mmap(256) 383 o = ab_cat(tn, 0, name) 384 ab_cat(tn, o, "_test" as *u8) 385 386 var authored: i64 = 0 - 1 387 if shape == 11 { authored = ab_author_11(name, mp, tp, params, st[1], st[6] as *u8) } 388 if shape == 12 { authored = ab_author_12(name, mp, tp, params, st[1]) } 389 if shape == 13 { authored = ab_author_13(name, mp, tp, params, st[1]) } 390 if shape == 15 { authored = ab_author_15(name, mp, tp, params, st[1]) } 391 if shape == 18 { authored = ab_author_18(name, mp, tp, params, st[1]) } 392 if shape == 4 { authored = ab_author_4(name, mp, tp, params, st[1]) } 393 if authored == (0 - 1) { 394 ab_w(1, "AUTO-BUILD NEEDS-TUTOR: shape " as *u8); ab_wn(1, shape) 395 ab_w(1, " has no dispatch row yet (honest, never silently absorbed)\n" as *u8) 396 ab_log(name, shape, ab_ms() - t0, "NEEDS-TUTOR" as *u8) 397 sys_exit(3); return 3 398 } 399 if authored == 0 { 400 ab_w(1, "AUTO-BUILD REFUSED: emitter rail rejected the param vector\n" as *u8) 401 ab_log(name, shape, ab_ms() - t0, "REFUSED" as *u8) 402 sys_exit(2); return 2 403 } 404 405 // the authored KAT must build and RUN green, sovereignly 406 let kst: i64 = ab_kat(tn) 407 let ms: i64 = ab_ms() - t0 408 if kst == 0 { 409 ab_w(1, "AUTO-BUILD GREEN: " as *u8); ab_w(1, name) 410 ab_w(1, " shape=" as *u8); ab_wn(1, shape) 411 ab_w(1, " KAT green, ms=" as *u8); ab_wn(1, ms); ab_w(1, " (spec in, gated capability out)\n" as *u8) 412 ab_log(name, shape, ms, "GREEN" as *u8) 413 sys_exit(0); return 0 414 } 415 ab_w(1, "AUTO-BUILD RED: authored KAT failed (fix the emitter or the spec, never the gate)\n" as *u8) 416 ab_log(name, shape, ms, "RED" as *u8) 417 sys_exit(1) 418 return 1 419}