code wiki / _hdl_build / nx_build_nobypass_gate.nx
nx_build_nobypass_gate.nx source
↩ module page · 130 lines · 6213 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"
12import "nx_gate_verdict.nx"
13
14const REG: *u8 = "knowledge/registry/build_registry.log"
15const BL: *u8 = "knowledge/registry/build_bypass_baseline.txt"
16const ODIR: *u8 = "runtime/_hdl_build"
17
18func nb_p(s: *u8) -> i64 { let n: i64 = br_len(s); sys_write(1, s, n); return 0 }
19func nb_pn(v: i64) -> i64 {
20 let bb: *u8 = sys_mmap(28); var m: i64 = v
21 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
22 let t: *u8 = sys_mmap(28); var k: i64 = 0
23 if m == 0 { t[0] = 48 as u8; k = 1 }
24 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
25 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
26 sys_write(1, bb, k); return 0
27}
28func nb_getdents(fd: i64, buf: *u8, count: i64) -> i64 { return __syscall(217, fd, buf, count, 0, 0, 0) }
29func nb_isorgan(name: *u8) -> i64 {
30 if name[0] != (110 as u8) { return 0 }
31 if name[1] != (120 as u8) { return 0 }
32 if name[2] != (95 as u8) { return 0 }
33 let n: i64 = br_len(name)
34 if n < 4 { return 0 }
35 if name[n - 3] != (46 as u8) { return 0 }
36 if name[n - 2] != (110 as u8) { return 0 }
37 if name[n - 1] != (120 as u8) { return 0 }
38 return 1
39}
40// REAL bypass count = organs on disk NOT in the registry.
41func nb_census() -> i64 {
42 let fd: i64 = sys_openat_rd(ODIR)
43 if fd < 0 { return 0 }
44 let gbuf: *u8 = sys_mmap(65536)
45 var total: i64 = 0; var reg: i64 = 0
46 var nread: i64 = nb_getdents(fd, gbuf, 65536)
47 while nread > 0 {
48 var off: i64 = 0
49 while off < nread {
50 let reclen: i64 = (gbuf[off + 16] as i64) | ((gbuf[off + 17] as i64) << 8)
51 if reclen <= 0 { off = nread } else {
52 let name: *u8 = ((gbuf as i64) + off + 19) as *u8
53 if nb_isorgan(name) == 1 { total = total + 1; if br_registered(REG, name) == 1 { reg = reg + 1 } }
54 off = off + reclen
55 }
56 }
57 nread = nb_getdents(fd, gbuf, 65536)
58 }
59 sys_close(fd)
60 return total - reg
61}
62func nb_read_int(path: *u8) -> i64 {
63 let buf: *u8 = sys_mmap(64)
64 let fd: i64 = sys_openat_rd(path)
65 if fd < 0 { return 0 - 1 }
66 let n: i64 = sys_read(fd, buf, 63)
67 sys_close(fd)
68 if n <= 0 { return 0 - 1 }
69 var v: i64 = 0; var i: i64 = 0
70 while i < n { let c: i64 = buf[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 }
71 return v
72}
73func nb_write_int(path: *u8, v: i64) -> i64 {
74 let fd: i64 = sys_openat_wr(path, 420) // O_TRUNC
75 if fd < 0 { return 0 - 1 }
76 let buf: *u8 = sys_mmap(64); var o: i64 = 0; var m: i64 = v
77 if m == 0 { buf[0] = 48 as u8; o = 1 } else {
78 let t: *u8 = sys_mmap(28); var k: i64 = 0
79 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
80 while o < k { buf[o] = t[k - 1 - o]; o = o + 1 }
81 }
82 buf[o] = 10 as u8; o = o + 1
83 sys_write(fd, buf, o); sys_close(fd)
84 return 0
85}
86// the ratchet predicate: ok iff current does NOT exceed baseline.
87func nb_ok(current: i64, baseline: i64) -> i64 { if current > baseline { return 0 } return 1 }
88
89func main() -> i64 {
90 nb_p("=== nx_build_nobypass_gate: R-LIVE-2 binding no-bypass RATCHET ===\n" as *u8)
91 // self-register so this gate is never its own bypass.
92 br_submit(REG, "nx_build_nobypass_gate.nx" as *u8, "GOV-PM" as *u8, "BUILT" as *u8)
93
94 // hermetic neg-control: the ratchet must FIRE on +1, PASS on equal and -1.
95 var ncok: i64 = 1
96 if nb_ok(100, 99) != 0 { ncok = 0 } // current 100 > baseline 99 -> must be 0 (fires)
97 if nb_ok(99, 99) != 1 { ncok = 0 }
98 if nb_ok(98, 99) != 1 { ncok = 0 }
99 nb_p(" neg-control (fires on +1, passes on =/-1): " as *u8)
100 if ncok == 1 { nb_p("OK\n" as *u8) } else { nb_p("BROKEN\n" as *u8) }
101
102 let current: i64 = nb_census()
103 var baseline: i64 = nb_read_int(BL)
104 var init: i64 = 0
105 if baseline < 0 { baseline = current; init = 1 } // first run establishes the line
106
107 let ok: i64 = nb_ok(current, baseline)
108 nb_p(" bypass current=" as *u8); nb_pn(current); nb_p(" baseline=" as *u8); nb_pn(baseline)
109 if init == 1 { nb_p(" (initialized)" as *u8) }
110 nb_p("\n" as *u8)
111
112 if ok == 1 {
113 // ratchet DOWN: baseline can only shrink toward 0.
114 if current < baseline { nb_write_int(BL, current); nb_p(" ratcheted baseline DOWN to " as *u8); nb_pn(current); nb_p("\n" as *u8) }
115 else { nb_write_int(BL, baseline); nb_p(" baseline held (no new bypass)\n" as *u8) }
116 } else {
117 nb_p(" !! NEW BYPASS: a backend organ was built without registering (call br_submit). baseline NOT raised.\n" as *u8)
118 }
119
120 // MIGRATED onto nx_gate_verdict (D001). BOTH conjuncts are gv_check rows: the dry_apply
121 // candidate set ctr[0]=ncok/ctr[1]=1 and DROPPED the ratchet result `ok` from the counter,
122 // so a NEW BYPASS would print its warning and still exit GREEN. One-state judge-equivalence
123 // (today ok=1) cannot see a dropped conjunct; the failing direction is load-bearing.
124 let ctr__gv: *i64 = gv_ctr()
125 gv_check("neg-control: ratchet fires on +1, passes on =/-1" as *u8, ncok, ctr__gv)
126 gv_check("no new bypass (current <= baseline)" as *u8, ok, ctr__gv)
127 let rc__gv: i64 = gv_verdict("BUILD-NOBYPASS-GATE" as *u8, ctr__gv, "no new bypass; surface can only shrink" as *u8)
128 sys_exit(rc__gv)
129 return rc__gv
130}