code wiki / _hdl_build / nx_compose_builder.nx
nx_compose_builder.nx source
↩ module page · 115 lines · 7760 B
1// nx_compose_builder.nx -- X-AUTH-EXT-001 (operator 2026-06-13 deepened anti-cheat): the
2// COMPOSITION-AUTHORING capability. Where nx_auto_builder authors bounded SHAPES (FSM/MATH/TLV),
3// THIS authors PIPELINE/COMPOSITION organs from a DATA spec -- so the team (not Claude) authors
4// the composition/IO organs (deploy, library, publish) too. It is the authoring TOOL (tutor-built,
5// like nx_auto_builder); it AUTHORS the organ from a spec (Claude writes the spec, the tool emits
6// the code). Spec: `name <mod>` + one `step <label>` line per pipeline stage. Emits <mod>.nx --
7// a fail-fast sequencer `<mod>_run(codes,n)` (0 if all stages exit 0, else 1-based index of the
8// first failure = abort/rollback point) + a KAT computed HERE from the step count. nx_sov_build_run
9// then builds+gates the authored organ. license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
12const K_MAGIC_20000: i64 = 20000
13
14func cb_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
15// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
16// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
17// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
18// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
19func cb_putn(v: i64) -> i64 { nxi_out(v); return 0 }
20func cb_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 }
21// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
22// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
23// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
24// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
25func cb_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
26
27func cb_read(path: *u8, buf: *u8, cap: i64) -> i64 {
28 let fd: i64 = sys_openat_rd(path)
29 if fd < 0 { return 0 - 1 }
30 var tot: i64 = 0
31 var r: i64 = 1
32 while r > 0 { let dst: *u8 = ((buf as i64) + tot) as *u8; r = sys_read(fd, dst, cap - tot); if r > 0 { tot = tot + r } }
33 sys_close(fd)
34 return tot
35}
36func cb_scan(buf: *u8, n: i64, start: i64, delim: i64) -> i64 { var i: i64 = start; var s: i64 = 1; while s == 1 { if i >= n { s = 0 } else { if buf[i] == (delim as u8) { s = 0 } else { i = i + 1 } } } return i }
37func cb_starts(buf: *u8, a: i64, e: i64, pre: *u8) -> i64 { var i: i64 = 0; while pre[i] != (0 as u8) { if a + i >= e { return 0 } if buf[a + i] != pre[i] { return 0 } i = i + 1 } return 1 }
38
39func main(argc: i64, argv: *i64) -> i64 {
40 if argc < 2 { cb_puts("COMPOSEBUILD verdict=REFUSED reason=usage:nx_compose_builder <specpath>\n" as *u8); return 2 }
41 let specp: *u8 = argv[1] as *u8
42 let sb: *u8 = sys_mmap(K_MAGIC_20000)
43 let sn: i64 = cb_read(specp, sb, K_MAGIC_20000)
44 if sn <= 0 { cb_puts("COMPOSEBUILD verdict=REFUSED reason=spec-unreadable\n" as *u8); return 2 }
45
46 // parse: name <mod> + count `step ` lines; capture name into nmbuf
47 let nmbuf: *u8 = sys_mmap(128)
48 var nmlen: i64 = 0
49 var nsteps: i64 = 0
50 var p: i64 = 0
51 while p < sn {
52 let a: i64 = p
53 let e: i64 = cb_scan(sb, sn, a, 10)
54 if cb_starts(sb, a, e, "name " as *u8) == 1 {
55 var i: i64 = a + 5
56 nmlen = 0
57 while i < e { nmbuf[nmlen] = sb[i]; nmlen = nmlen + 1; i = i + 1 }
58 nmbuf[nmlen] = 0 as u8
59 }
60 if cb_starts(sb, a, e, "step " as *u8) == 1 { nsteps = nsteps + 1 }
61 p = e + 1
62 }
63 if nmlen == 0 { cb_puts("COMPOSEBUILD verdict=REFUSED reason=no-name\n" as *u8); return 2 }
64 if nsteps < 1 { cb_puts("COMPOSEBUILD verdict=REFUSED reason=no-steps\n" as *u8); return 2 }
65
66 // output path runtime/_hdl_build/<name>.nx
67 let outp: *u8 = sys_mmap(256)
68 var o: i64 = 0
69 let pre: *u8 = "runtime/_hdl_build/"
70 var j: i64 = 0; while pre[j] != (0 as u8) { outp[o] = pre[j]; o = o + 1; j = j + 1 }
71 j = 0; while nmbuf[j] != (0 as u8) { outp[o] = nmbuf[j]; o = o + 1; j = j + 1 }
72 outp[o] = 46 as u8; o = o + 1; outp[o] = 110 as u8; o = o + 1; outp[o] = 120 as u8; o = o + 1; outp[o] = 0 as u8
73
74 let fd: i64 = sys_openat_wr(outp, 420)
75 if fd < 0 { cb_puts("COMPOSEBUILD verdict=REFUSED reason=out-unwritable\n" as *u8); return 2 }
76
77 let kfail: i64 = nsteps / 2 // a middle step to fail in the KAT
78
79 // ---- AUTHOR the composition organ (TEAM-authored from the spec; Claude wrote no organ logic) ----
80 cb_w(fd, "// AUTHORED BY nx_compose_builder (X-AUTH-EXT-001 composition shape) from a DATA spec.\n" as *u8)
81 cb_w(fd, "// Fail-fast pipeline sequencer: run stages in order; 0 if all exit 0, else 1-based index\n" as *u8)
82 cb_w(fd, "// of the first failure (the abort/rollback point). KAT computed from the step count.\n" as *u8)
83 cb_w(fd, "// DRY (2026-07-25, D001 / law L009+L010): verdict emission INHERITS nx_gate_verdict.\n" as *u8)
84 cb_w(fd, "// This generator used to stamp its own _p/_pn print helpers and a hand-rolled verdict\n" as *u8)
85 cb_w(fd, "// line into every organ it authored, so each generated gate landed as a FRESH L009\n" as *u8)
86 cb_w(fd, "// custom-verdict breach AND a fresh L010 helperdup breach -- which is why both laws\n" as *u8)
87 cb_w(fd, "// were RISING while hand-migration ate the backlog. Generated gates are now base-class\n" as *u8)
88 cb_w(fd, "// adopters BY CONSTRUCTION; the generator cannot emit a non-DRY gate.\n" as *u8)
89 cb_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
90 cb_w(fd, "import \"nx_gate_verdict.nx\"\n" as *u8)
91 cb_w(fd, "func " as *u8); cb_w(fd, nmbuf); cb_w(fd, "_run(codes: *i64, n: i64) -> i64 { var i: i64 = 0; while i < n { if codes[i] != 0 { return i + 1 } i = i + 1 } return 0 }\n" as *u8)
92 cb_w(fd, "func main() -> i64 {\n" as *u8)
93 cb_w(fd, " let ctr: *i64 = gv_ctr()\n" as *u8)
94 cb_w(fd, " gv_head(\"" as *u8); cb_w(fd, nmbuf); cb_w(fd, " -- fail-fast pipeline sequencer KAT\" as *u8)\n" as *u8)
95 cb_w(fd, " let a: *i64 = sys_mmap(8 * " as *u8); cb_wn(fd, nsteps); cb_w(fd, ") as *i64\n" as *u8)
96 cb_w(fd, " var i: i64 = 0\n while i < " as *u8); cb_wn(fd, nsteps); cb_w(fd, " { a[i] = 0; i = i + 1 }\n" as *u8)
97 cb_w(fd, " let all_ok: i64 = " as *u8); cb_w(fd, nmbuf); cb_w(fd, "_run(a, " as *u8); cb_wn(fd, nsteps); cb_w(fd, ")\n" as *u8)
98 cb_w(fd, " a[" as *u8); cb_wn(fd, kfail); cb_w(fd, "] = 1\n" as *u8)
99 cb_w(fd, " let fail_at: i64 = " as *u8); cb_w(fd, nmbuf); cb_w(fd, "_run(a, " as *u8); cb_wn(fd, nsteps); cb_w(fd, ")\n" as *u8)
100 cb_w(fd, " var t1: i64 = 0\n" as *u8)
101 cb_w(fd, " if all_ok == 0 { t1 = 1 }\n" as *u8)
102 cb_w(fd, " gv_check(\"T1 all-clean pipeline returns 0\" as *u8, t1, ctr)\n" as *u8)
103 cb_w(fd, " var t2: i64 = 0\n" as *u8)
104 cb_w(fd, " if fail_at == " as *u8); cb_wn(fd, kfail + 1); cb_w(fd, " { t2 = 1 }\n" as *u8)
105 cb_w(fd, " gv_check(\"T2 first failing stage reported 1-based (abort point)\" as *u8, t2, ctr)\n" as *u8)
106 cb_w(fd, " let rc: i64 = gv_verdict(\"" as *u8); cb_w(fd, nmbuf); cb_w(fd, "\" as *u8, ctr, \"fail-fast sequencer, " as *u8); cb_wn(fd, nsteps); cb_w(fd, " steps\" as *u8)\n" as *u8)
107 cb_w(fd, " sys_exit(rc)\n return rc\n}\n" as *u8)
108 sys_close(fd)
109
110 cb_puts("COMPOSEBUILD authored=" as *u8); cb_puts(nmbuf)
111 cb_puts(" steps=" as *u8); cb_putn(nsteps)
112 cb_puts(" -> " as *u8); cb_puts(outp)
113 cb_puts(" verdict=GREEN (spec in, composition organ out -- build via nx_sov_build_run)\n" as *u8)
114 return 0
115}