code wiki / _hdl_build / nx_build_nobypass_gate.nx
nx_build_nobypass_gate.nx source
↩ module page · 123 lines · 5725 B
1// nx_build_nobypass_gate.nx -- R-LIVE-2: the BINDING NO-BYPASS RATCHET (build-time twin of
2// nx_pub_nobypass_gate). It measures the REAL bypass count (organs in runtime/_hdl_build NOT in the
3// persistent build-registry) against a BASELINE on disk: the build FAILS (RED) if the count GREW (a new
4// unregistered organ was added), and RATCHETS the baseline DOWN as organs get registered -- so the bypass
5// surface can only ever shrink, never grow. First run initializes the baseline. The gate self-registers so
6// it is never its own bypass.
7// HERMETIC NEGATIVE CONTROL: the ratchet predicate is tested on synthetic numbers (must FIRE on baseline+1,
8// PASS on baseline and baseline-1) so GREEN proves the binding is load-bearing, not decorative.
9// Sovereign: imports nx_build_registry_lib (+ nx_syscalls). license_tier: ORIGINAL expect_exit: 0
10import "nx_build_registry_lib.nx"
11import "nx_syscalls.nx"
12
13const REG: *u8 = "knowledge/registry/build_registry.log"
14const BL: *u8 = "knowledge/registry/build_bypass_baseline.txt"
15const ODIR: *u8 = "runtime/_hdl_build"
16
17func nb_p(s: *u8) -> i64 { let n: i64 = br_len(s); sys_write(1, s, n); return 0 }
18func nb_pn(v: i64) -> i64 {
19 let bb: *u8 = sys_mmap(28); var m: i64 = v
20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
21 let t: *u8 = sys_mmap(28); var k: i64 = 0
22 if m == 0 { t[0] = 48 as u8; k = 1 }
23 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
24 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
25 sys_write(1, bb, k); return 0
26}
27func nb_getdents(fd: i64, buf: *u8, count: i64) -> i64 { return __syscall(217, fd, buf, count, 0, 0, 0) }
28func nb_isorgan(name: *u8) -> i64 {
29 if name[0] != (110 as u8) { return 0 }
30 if name[1] != (120 as u8) { return 0 }
31 if name[2] != (95 as u8) { return 0 }
32 let n: i64 = br_len(name)
33 if n < 4 { return 0 }
34 if name[n - 3] != (46 as u8) { return 0 }
35 if name[n - 2] != (110 as u8) { return 0 }
36 if name[n - 1] != (120 as u8) { return 0 }
37 return 1
38}
39// REAL bypass count = organs on disk NOT in the registry.
40func nb_census() -> i64 {
41 let fd: i64 = sys_openat_rd(ODIR)
42 if fd < 0 { return 0 }
43 let gbuf: *u8 = sys_mmap(65536)
44 var total: i64 = 0; var reg: i64 = 0
45 var nread: i64 = nb_getdents(fd, gbuf, 65536)
46 while nread > 0 {
47 var off: i64 = 0
48 while off < nread {
49 let reclen: i64 = (gbuf[off + 16] as i64) | ((gbuf[off + 17] as i64) << 8)
50 if reclen <= 0 { off = nread } else {
51 let name: *u8 = ((gbuf as i64) + off + 19) as *u8
52 if nb_isorgan(name) == 1 { total = total + 1; if br_registered(REG, name) == 1 { reg = reg + 1 } }
53 off = off + reclen
54 }
55 }
56 nread = nb_getdents(fd, gbuf, 65536)
57 }
58 sys_close(fd)
59 return total - reg
60}
61func nb_read_int(path: *u8) -> i64 {
62 let buf: *u8 = sys_mmap(64)
63 let fd: i64 = sys_openat_rd(path)
64 if fd < 0 { return 0 - 1 }
65 let n: i64 = sys_read(fd, buf, 63)
66 sys_close(fd)
67 if n <= 0 { return 0 - 1 }
68 var v: i64 = 0; var i: i64 = 0
69 while i < n { let c: i64 = buf[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 }
70 return v
71}
72func nb_write_int(path: *u8, v: i64) -> i64 {
73 let fd: i64 = sys_openat_wr(path, 420) // O_TRUNC
74 if fd < 0 { return 0 - 1 }
75 let buf: *u8 = sys_mmap(64); var o: i64 = 0; var m: i64 = v
76 if m == 0 { buf[0] = 48 as u8; o = 1 } else {
77 let t: *u8 = sys_mmap(28); var k: i64 = 0
78 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
79 while o < k { buf[o] = t[k - 1 - o]; o = o + 1 }
80 }
81 buf[o] = 10 as u8; o = o + 1
82 sys_write(fd, buf, o); sys_close(fd)
83 return 0
84}
85// the ratchet predicate: ok iff current does NOT exceed baseline.
86func nb_ok(current: i64, baseline: i64) -> i64 { if current > baseline { return 0 } return 1 }
87
88func main() -> i64 {
89 nb_p("=== nx_build_nobypass_gate: R-LIVE-2 binding no-bypass RATCHET ===\n" as *u8)
90 // self-register so this gate is never its own bypass.
91 br_submit(REG, "nx_build_nobypass_gate.nx" as *u8, "GOV-PM" as *u8, "BUILT" as *u8)
92
93 // hermetic neg-control: the ratchet must FIRE on +1, PASS on equal and -1.
94 var ncok: i64 = 1
95 if nb_ok(100, 99) != 0 { ncok = 0 } // current 100 > baseline 99 -> must be 0 (fires)
96 if nb_ok(99, 99) != 1 { ncok = 0 }
97 if nb_ok(98, 99) != 1 { ncok = 0 }
98 nb_p(" neg-control (fires on +1, passes on =/-1): " as *u8)
99 if ncok == 1 { nb_p("OK\n" as *u8) } else { nb_p("BROKEN\n" as *u8) }
100
101 let current: i64 = nb_census()
102 var baseline: i64 = nb_read_int(BL)
103 var init: i64 = 0
104 if baseline < 0 { baseline = current; init = 1 } // first run establishes the line
105
106 let ok: i64 = nb_ok(current, baseline)
107 nb_p(" bypass current=" as *u8); nb_pn(current); nb_p(" baseline=" as *u8); nb_pn(baseline)
108 if init == 1 { nb_p(" (initialized)" as *u8) }
109 nb_p("\n" as *u8)
110
111 if ok == 1 {
112 // ratchet DOWN: baseline can only shrink toward 0.
113 if current < baseline { nb_write_int(BL, current); nb_p(" ratcheted baseline DOWN to " as *u8); nb_pn(current); nb_p("\n" as *u8) }
114 else { nb_write_int(BL, baseline); nb_p(" baseline held (no new bypass)\n" as *u8) }
115 } else {
116 nb_p(" !! NEW BYPASS: a backend organ was built without registering (call br_submit). baseline NOT raised.\n" as *u8)
117 }
118
119 nb_p(" verdict=" as *u8)
120 if ncok == 1 { if ok == 1 { nb_p("GREEN (no new bypass; surface can only shrink)\n" as *u8); sys_exit(0); return 0 } }
121 nb_p("RED (a new unregistered organ appeared, or the ratchet is disarmed)\n" as *u8)
122 sys_exit(1); return 1
123}