code wiki / _hdl_build / nx_ledger_min.nx
nx_ledger_min.nx source
↩ module page · 78 lines · 2587 B
1// nx_ledger_min.nx -- lane gate: require >= min property-verified greens in an autofix ledger.
2// argv: <ledger> <min> (min = lane config DATA, never a baked threshold -- rule 11)
3// Counts "maker=GREEN" rows; prints NX-LEDGER-MIN green=<n> min=<m> verdict=GREEN|RED.
4// exit 0 iff n >= m. Used as the gateelf of the S2 live autofix lane (nx_seat_drive).
5// license_tier: ORIGINAL No hw writes (Rule 26).
6import "nx_deploy_lib.nx"
7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
8import "nx_syscalls.nx"
9
10const LM_CAP: i64 = 1048576
11
12func lm_w(s: *u8) -> i64 {
13 var n: i64 = 0
14 while s[n] != (0 as u8) { n = n + 1 }
15 sys_write(1, s, n)
16 return 0
17}
18
19// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
20// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
21// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
22// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
23func lm_wn(v: i64) -> i64 { nxi_out(v); return 0 }
24
25func lm_atoi(s: *u8) -> i64 {
26 var v: i64 = 0
27 var i: i64 = 0
28 while s[i] != (0 as u8) {
29 let c: i64 = s[i] as i64
30 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } }
31 i = i + 1
32 }
33 return v
34}
35
36// occurrences of NUL-terminated needle in buf[0..n)
37func lm_count(buf: *u8, n: i64, needle: *u8) -> i64 {
38 var cnt: i64 = 0
39 var p: i64 = 0
40 while p < n {
41 var ki: i64 = 0
42 var hit: i64 = 1
43 var scanning: i64 = 1
44 while scanning == 1 {
45 let kc: i64 = needle[ki] as i64
46 if kc == 0 { scanning = 0 } else {
47 var bc: i64 = 0
48 if p + ki < n { bc = buf[p + ki] as i64 }
49 if bc == kc { ki = ki + 1 } else { hit = 0; scanning = 0 }
50 }
51 }
52 if hit == 1 { cnt = cnt + 1 }
53 p = p + 1
54 }
55 return cnt
56}
57
58func main(argc: i64, argv: *i64) -> i64 {
59 if argc < 3 { lm_w("usage: nx_ledger_min <ledger> <min>\n" as *u8); sys_exit(2); return 2 }
60 let lp: *u8 = argv[1] as *u8
61 let ms: *u8 = argv[2] as *u8
62 let minv: i64 = lm_atoi(ms)
63 let buf: *u8 = sys_mmap(LM_CAP)
64 let n: i64 = dp_read(lp, buf, LM_CAP)
65 let g: i64 = lm_count(buf, n, "maker=GREEN" as *u8)
66 lm_w("NX-LEDGER-MIN green=" as *u8)
67 lm_wn(g)
68 lm_w(" min=" as *u8)
69 lm_wn(minv)
70 if g >= minv {
71 lm_w(" verdict=GREEN\n" as *u8)
72 sys_exit(0)
73 return 0
74 }
75 lm_w(" verdict=RED\n" as *u8)
76 sys_exit(1)
77 return 1
78}