code wiki / _hdl_build / nx_warden_build.nx
nx_warden_build.nx source
↩ module page · 118 lines · 7230 B
1// nx_warden_build.nx -- the WARDEN's ONE-COMMAND BUILD: spec in -> gated capability out, zero Claude in
2// the loop for covered shapes. The pieces existed separately (classifier routes, emitters author,
3// engineer_build_gate compiles+runs, build_pipeline decides) but a HAND still drove them between steps.
4// This organ EXECUTES the whole route: FEATURE VECTOR -> plc_classify -> emitter dispatch (the Builder
5// AUTHORS module+test to disk) -> eng_build_gate on the authored test (the ENGINEER's compile+link+run
6// with recompile-retry -- composed, not reimplemented: rule 15) -> bp_run (the RACI verdict) -> durable
7// line in knowledge/status/team_build.log. A NOVEL spec routes to NEEDS_TUTOR honestly -- measured,
8// never guessed. RACI: WARDEN routes, Builder authors, Engineer tests, nothing self-grades.
9// LAWS: struct-free, integer-only, flat ifs, <=6 args per func. license_tier: ORIGINAL
10import "nx_pattern_classify.nx"
11import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
12import "nx_pattern_emit.nx"
13import "nx_pattern_emit3.nx"
14import "nx_pattern_emit4.nx"
15import "nx_pattern_emit5.nx"
16import "nx_pattern_emit7.nx"
17import "nx_pattern_emit8.nx"
18import "nx_pattern_emit6.nx"
19import "nx_pattern_emit10.nx"
20import "nx_pattern_emit11.nx"
21import "nx_pattern_emit12.nx"
22import "nx_engineer_build_gate.nx"
23import "nx_build_pipeline.nx"
24
25func wb_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 }
26// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
27// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
28// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
29// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
30func wb_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
31func wb_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ dst[off+i]=s[i]; i=i+1 } dst[off+i]=0 as u8; return off+i }
32
33// the BUILDER's emitter dispatch: author module+test for the classified pattern from the spec's
34// integer params p (meaning per pattern -- the assignment's real numbers, no magic defaults).
35func wb_author(pattern: i64, name: *u8, p: *i64) -> i64 {
36 let mp: *u8 = sys_mmap(512); var o: i64 = wb_cat(mp, 0, "runtime/_hdl_build/" as *u8); o = wb_cat(mp, o, name); o = wb_cat(mp, o, ".nx" as *u8)
37 let tp: *u8 = sys_mmap(512); o = wb_cat(tp, 0, "runtime/_hdl_build/" as *u8); o = wb_cat(tp, o, name); o = wb_cat(tp, o, "_test.nx" as *u8)
38 if pattern == PL_VERDICT_GATE { return pe_author(1, name, mp, tp, p[0], p[1]) }
39 if pattern == PL_COMPARATOR { return pe_author(3, name, mp, tp, 0, 0) }
40 if pattern == PL_ACCUMULATOR { return pe_author(5, name, mp, tp, 0, 0) }
41 if pattern == PL_THRESHOLD_TABLE { return pe_author_threshold(name, mp, tp, p[0], p[1], p[2], p[3], p[4], p[5]) }
42 if pattern == PL_STATE_FLOW { return pe3_author_state_flow(name, mp, tp, p[0] as *i64, p[1]) }
43 if pattern == PL_PARSER_KAT { return pe4_author_parser(name, mp, tp) }
44 if pattern == PL_GOVERNOR { return pe4_author_governor(name, mp, tp, p[0], p[1], p[2]) }
45 if pattern == PL_GEOMETRY { return pe5_author_geometry(name, mp, tp, p[0], p[1]) }
46 if pattern == PL_PID_LOOP { return pe5_author_pid(name, mp, tp, p[0], p[1], p[2]) }
47 if pattern == PL_WIRE_TLV { return pe7_author_wire_tlv(name, mp, tp, p[0], p[1], p[2]) }
48 if pattern == PL_STRUCT_WALK { return pe8_author_struct_walk(name, mp, tp, p[0] as *i64, p[1] as *i64, p[2]) }
49 if pattern == PL_WIRE_MARKER { return pe10_author_wire_marker(name, mp, tp, p[0], p[1] as *i64, p[2]) }
50 if pattern == PL_PREDICTOR_CODER { return pe11_author_predictor_coder(name, mp, tp, p[0], p[1]) }
51 if pattern == PL_TEMPLATE_TABLE { return pe12_author_template_table(name, mp, tp, p[0] as *i64, p[1]) }
52 if pattern == PL_MATH_KERNEL {
53 // pe6's "bespoke modfile arg" is just the authored module's own file
54 // name (the test's import line) -- derivable from name, so the
55 // one-command contract holds: p[0]=oracle spec ptr, p[1]=ncoef.
56 let mfn: *u8 = sys_mmap(256)
57 var o2: i64 = wb_cat(mfn, 0, name)
58 o2 = wb_cat(mfn, o2, ".nx" as *u8)
59 return pe6_author_logf64(name, mfn, mp, tp, p[0] as *i64, p[1])
60 }
61 return 0
62}
63
64// can the dispatch AUTHOR this pattern through the one-command path?
65// MATH_KERNEL's "bespoke" arg turned out derivable (wired 2026-06-10, debt
66// paid). IO_CONTRACT (pe9) emits a lane PROGRAM with no module+test pair
67// (wb_verify has nothing to run) and SCAFFOLD's product is the boilerplate
68// other emitters already subsume -- both stay NAMED DISPATCH DEBT; routing
69// them as NEEDS_TUTOR would be a lie.
70func wb_dispatchable(pattern: i64) -> i64 {
71 if pattern == PL_IO_CONTRACT { return 0 }
72 if pattern == PL_SCAFFOLD { return 0 }
73 return 1
74}
75
76// the ENGINEER verifies the authored test via the composed build gate (compile+link+run+retry).
77// 1 iff the test built AND exited 0.
78func wb_verify(name: *u8) -> i64 {
79 let src: *u8 = sys_mmap(512); var o: i64 = wb_cat(src, 0, "runtime/_hdl_build/" as *u8); o = wb_cat(src, o, name); o = wb_cat(src, o, "_test.nx" as *u8)
80 let out: *i64 = sys_mmap(32) as *i64
81 let v: i64 = eng_build_gate("_offc/nx_cc_known_good.elf" as *u8, src,
82 "/tmp/_wb.s" as *u8, "/tmp/_wb_h.s" as *u8, "/tmp/_wb.elf" as *u8, "/tmp/_wb_out.txt" as *u8, out)
83 if v == EBG_PASS { return 1 }
84 return 0
85}
86
87// covered by the library + an emitter exists, but the one-command dispatch can't author it yet
88const WB_DISPATCH_DEBT: i64 = 8
89
90// THE ONE COMMAND: spec (features f + module name + params p) -> final pipeline stage.
91// out[0]=classified pattern, out[1]=authored, out[2]=test pass.
92func wb_build(f: *i64, name: *u8, p: *i64, out: *i64) -> i64 {
93 let pat: i64 = plc_classify(f)
94 out[0] = pat; out[1] = 0; out[2] = 0
95 if pat == 0 { return BP_NEEDS_TUTOR }
96 if wb_dispatchable(pat) == 0 { return WB_DISPATCH_DEBT }
97 if wb_author(pat, name, p) != 1 { return BP_REJECTED }
98 out[1] = 1
99 out[2] = wb_verify(name)
100 return bp_run(1, pat, BR_BUILDER, BR_ENGINEER, BR_DOCTOR, 1, out[2], 0, 1)
101}
102
103// durable verdict line (append-only; the SITEQ/SECSTATUS discipline)
104func wb_report(name: *u8, pat: i64, stage: i64, testpass: i64) -> i64 {
105 let fd: i64 = sys_openat_append("knowledge/status/team_build.log" as *u8, 0x1a4)
106 if fd < 0 { return 0 }
107 wb_w(fd, "TEAMBUILD name=" as *u8); wb_w(fd, name)
108 wb_w(fd, " pattern=" as *u8); wb_w(fd, pl_name(pat))
109 wb_w(fd, " test_pass=" as *u8); wb_wn(fd, testpass)
110 wb_w(fd, " stage=" as *u8); wb_wn(fd, stage)
111 if stage == BP_ADMITTED { wb_w(fd, " verdict=ADMITTED-HANDS-OFF" as *u8) }
112 if stage == BP_NEEDS_TUTOR { wb_w(fd, " verdict=NEEDS-TUTOR(honest)" as *u8) }
113 if stage == BP_REJECTED { wb_w(fd, " verdict=REJECTED" as *u8) }
114 if stage == WB_DISPATCH_DEBT { wb_w(fd, " verdict=DISPATCH-DEBT(emitter-exists-wire-it)" as *u8) }
115 wb_w(fd, " epoch=" as *u8); wb_wn(fd, sys_now_realtime_sec()); wb_w(fd, "\n" as *u8)
116 sys_close(fd)
117 return 1
118}