code wiki / _hdl_build / nx_vizsla_bills_gate.nx
nx_vizsla_bills_gate.nx source
↩ module page · 274 lines · 11359 B
1// nx_vizsla_bills_gate.nx -- VIZSLA bills gate: the recurring-payables tracker
2// proven against a HAND-COMPUTED fixture (oracle = this header). No mocks:
3// drives the real organ over real seg_store PAID records + civil-date math.
4//
5// bills (soon_days=7): rent $1800 due-1 MANUAL; electric $120 due-15 MANUAL;
6// netflix $15.99 due-20 AUTO; water $45 due-10 MANUAL; phone $60 due-25 MANUAL.
7// paid: PAID rent 2026-06-02 (June); PAID netflix 2026-05-20 (MAY, not June).
8// today=2026-06-14:
9// rent paid this month -> PAID, next due 2026-07-01
10// electric due-15 unpaid, 1d off -> DUE (<=soon) owed 12000
11// netflix due-20 unpaid (May!=Jun)-> DUE (6d) owed 1599
12// water due-10 unpaid, -4d -> OVERDUE owed 4500
13// phone due-25 unpaid, 11d>7 -> UPCOMING owed 6000
14// => bills=5 paid=1 upcoming=1 due=2 overdue=1 owed_cents=24099
15// DATA-DRIVEN soon_days=14: phone 11d<=14 -> DUE => due=3 upcoming=0 (owed same).
16//
17// Rows:
18// 1 loud-fail-missing-bills status on absent file => exit 1
19// 2 loud-fail-bad-cadence a non-MONTHLY bill => malformed => exit 1
20// 3 load-paid scanned=2 new=2 segment=seg-5001
21// 4 idempotent-reload new=0 dup_instore=2 segment=none (law 10)
22// 5 bill-paid rent PAID, next due 2026-07-01
23// 6 bill-due electric DUE due=2026-06-15
24// 7 bill-overdue water OVERDUE
25// 8 bill-upcoming phone UPCOMING
26// 9 paid-this-month-scoped netflix DUE (its PAID was MAY, not June)
27// 10 status-verdict 5/1/1/2/1 owed_cents=24099
28// 11 data-driven-soon-days soon_days=14 => due=3 upcoming=0, same binary
29// 12 status-determinism byte-identical twice
30// Evidence: VIZSLA-BILL-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 12/12.
31// Tamper: bogus instrument argv[1] => all rows FAIL => RED.
32// spec: knowledge/specs/2026-06-10-nishi-vizsla-ladder.md license_tier: ORIGINAL
33import "nx_syscalls.nx"
34
35func rg_slen(s: *u8) -> i64 {
36 var n: i64 = 0
37 while s[n] != (0 as u8) { n = n + 1 }
38 return n
39}
40
41func rg_p(s: *u8) -> i64 {
42 sys_write(1, s, rg_slen(s))
43 return 0
44}
45
46func rg_cat(dst: *u8, off: i64, s: *u8) -> i64 {
47 var i: i64 = 0
48 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
49 return off + i
50}
51
52func rg_catn(dst: *u8, off: i64, v: i64) -> i64 {
53 var o: i64 = off
54 var m: i64 = v
55 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
56 let t: *u8 = sys_mmap(28)
57 var k: i64 = 0
58 if m == 0 { t[0] = 48 as u8; k = 1 }
59 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
60 var i: i64 = 0
61 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
62 return o + k
63}
64
65func rg_write(path: *u8, content: *u8) -> i64 {
66 let fd: i64 = sys_openat_wr(path, 0x1a4)
67 if fd < 0 { return 0 - 1 }
68 sys_write(fd, content, rg_slen(content))
69 sys_close(fd)
70 return 0
71}
72
73func rg_readall(path: *u8, szout: *i64) -> *u8 {
74 let fd: i64 = sys_openat_rd(path)
75 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
76 let sz: i64 = sys_lseek(fd, 0, 2)
77 sys_lseek(fd, 0, 0)
78 let buf: *u8 = sys_mmap(sz + 64)
79 var got: i64 = 0
80 var n: i64 = 1
81 while n > 0 {
82 n = sys_read(fd, (buf as i64 + got) as *u8, 65536)
83 if n > 0 { got = got + n }
84 }
85 sys_close(fd)
86 szout[0] = got
87 return buf
88}
89
90func rg_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, outpath: *u8) -> i64 {
91 let pid: i64 = sys_fork()
92 if pid == 0 {
93 if (outpath as i64) != 0 {
94 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
95 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
96 }
97 let argv: *i64 = sys_mmap(64) as *i64
98 argv[0] = elf as i64
99 var i: i64 = 1
100 if (a1 as i64) != 0 { argv[i] = a1 as i64; i = i + 1 }
101 if (a2 as i64) != 0 { argv[i] = a2 as i64; i = i + 1 }
102 if (a3 as i64) != 0 { argv[i] = a3 as i64; i = i + 1 }
103 if (a4 as i64) != 0 { argv[i] = a4 as i64; i = i + 1 }
104 argv[i] = 0
105 let envp: *i64 = sys_mmap(16) as *i64
106 envp[0] = 0
107 sys_execve(elf, argv, envp)
108 sys_exit(127)
109 }
110 let st: *i64 = sys_mmap(16) as *i64
111 sys_wait4(pid, st, 0)
112 let sig: i64 = st[0] & 0x7f
113 if sig != 0 { return 128 + sig }
114 return (st[0] >> 8) & 0xff
115}
116
117func rg_has(path: *u8, needle: *u8) -> i64 {
118 let szp: *i64 = sys_mmap(16) as *i64
119 let b: *u8 = rg_readall(path, szp)
120 let sz: i64 = szp[0]
121 let n: i64 = rg_slen(needle)
122 if sz < n { return 0 }
123 var i: i64 = 0
124 while i + n <= sz {
125 var ok: i64 = 1
126 var j: i64 = 0
127 while j < n {
128 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 }
129 }
130 if ok == 1 { return 1 }
131 i = i + 1
132 }
133 return 0
134}
135
136func rg_fileeq(p1: *u8, p2: *u8) -> i64 {
137 let s1: *i64 = sys_mmap(16) as *i64
138 let s2: *i64 = sys_mmap(16) as *i64
139 let b1: *u8 = rg_readall(p1, s1)
140 let b2: *u8 = rg_readall(p2, s2)
141 if s1[0] != s2[0] { return 0 }
142 if s1[0] <= 0 { return 0 }
143 var i: i64 = 0
144 while i < s1[0] {
145 if b1[i] != b2[i] { return 0 }
146 i = i + 1
147 }
148 return 1
149}
150
151func rg_row(name: *u8, pass: i64) -> i64 {
152 rg_p("ROW " as *u8)
153 rg_p(name)
154 if pass == 1 { rg_p(" PASS\n" as *u8) } else { rg_p(" FAIL\n" as *u8) }
155 return pass
156}
157
158func main(argc: i64, argv: *i64) -> i64 {
159 rg_p("=== VIZSLA BILLS GATE: hand-computed recurring-payables KATs ===\n" as *u8)
160 var rb: *u8 = "/tmp/nx_vizsla_bills.sov.elf" as *u8
161 if argc > 1 { rb = argv[1] as *u8 }
162 let pr: i64 = sys_openat_rd(rb)
163 if pr >= 0 { sys_close(pr) }
164 else {
165 rg_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
166 rg_run("_offc/nx_sov_build_run.elf" as *u8, "nx_vizsla_bills" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, "/tmp/bl_rebuild.out" as *u8)
167 }
168
169 let pfx: *u8 = sys_mmap(128)
170 var po: i64 = 0
171 po = rg_cat(pfx, po, "/tmp/blG" as *u8); po = rg_catn(pfx, po, sys_now_us()); po = rg_cat(pfx, po, "-" as *u8); pfx[po] = 0 as u8
172
173 rg_write("/tmp/bl_bills.txt" as *u8, "CONF soon_days 7\nBILL rent Landlord 180000 1 MONTHLY MANUAL\nBILL electric PowerCo 12000 15 MONTHLY MANUAL\nBILL netflix Netflix 1599 20 MONTHLY AUTO\nBILL water CityWater 4500 10 MONTHLY MANUAL\nBILL phone TelCo 6000 25 MONTHLY MANUAL\n" as *u8)
174 rg_write("/tmp/bl_bills14.txt" as *u8, "CONF soon_days 14\nBILL rent Landlord 180000 1 MONTHLY MANUAL\nBILL electric PowerCo 12000 15 MONTHLY MANUAL\nBILL netflix Netflix 1599 20 MONTHLY AUTO\nBILL water CityWater 4500 10 MONTHLY MANUAL\nBILL phone TelCo 6000 25 MONTHLY MANUAL\n" as *u8)
175 rg_write("/tmp/bl_badcad.txt" as *u8, "CONF soon_days 7\nBILL gym Gym 3000 5 WEEKLY MANUAL\n" as *u8)
176 rg_write("/tmp/bl_log.txt" as *u8, "PAID 2026-06-02 rent 180000 june-rent\nPAID 2026-05-20 netflix 1599 may-auto\n" as *u8)
177
178 let fb: *u8 = "/tmp/bl_bills.txt" as *u8
179 let flog: *u8 = "/tmp/bl_log.txt" as *u8
180 var pass: i64 = 0
181 var r: i64 = 0
182
183 // row 1: loud-fail missing bills file
184 let rc1: i64 = rg_run(rb, "status" as *u8, pfx, "/tmp/bl_NOPE.txt" as *u8, "2026-06-14" as *u8, "/tmp/bl_o1.txt" as *u8)
185 r = 0
186 if rc1 == 1 { r = 1 }
187 pass = pass + rg_row("loud-fail-missing-bills" as *u8, r)
188
189 // row 2: a non-MONTHLY cadence is malformed => loud-fail
190 let rc2: i64 = rg_run(rb, "status" as *u8, pfx, "/tmp/bl_badcad.txt" as *u8, "2026-06-14" as *u8, "/tmp/bl_o2.txt" as *u8)
191 r = 0
192 if rc2 == 1 { r = 1 }
193 pass = pass + rg_row("loud-fail-bad-cadence" as *u8, r)
194
195 // row 3: load 2 PAID into seg-5001
196 let rc3: i64 = rg_run(rb, "load" as *u8, flog, pfx, "5001" as *u8, "/tmp/bl_o3.txt" as *u8)
197 r = 0
198 if rc3 == 0 {
199 if rg_has("/tmp/bl_o3.txt" as *u8, "VIZSLA-BILL-LOAD scanned=2 new=2 dup_infile=0 dup_instore=0 segment=seg-5001" as *u8) == 1 { r = 1 }
200 }
201 pass = pass + rg_row("load-paid" as *u8, r)
202
203 // row 4: idempotent reload
204 let rc4: i64 = rg_run(rb, "load" as *u8, flog, pfx, "5009" as *u8, "/tmp/bl_o4.txt" as *u8)
205 r = 0
206 if rc4 == 0 {
207 if rg_has("/tmp/bl_o4.txt" as *u8, "VIZSLA-BILL-LOAD scanned=2 new=0 dup_infile=0 dup_instore=2 segment=none" as *u8) == 1 { r = 1 }
208 }
209 pass = pass + rg_row("idempotent-reload" as *u8, r)
210
211 // rows 5-10: status
212 let rc5: i64 = rg_run(rb, "status" as *u8, pfx, fb, "2026-06-14" as *u8, "/tmp/bl_st.txt" as *u8)
213 r = 0
214 if rc5 == 0 {
215 if rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL id=rent payee=Landlord amount=180000 due=2026-07-01 autopay=MANUAL state=PAID" as *u8) == 1 { r = 1 }
216 }
217 pass = pass + rg_row("bill-paid" as *u8, r)
218
219 r = rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL id=electric payee=PowerCo amount=12000 due=2026-06-15 autopay=MANUAL state=DUE" as *u8)
220 pass = pass + rg_row("bill-due" as *u8, r)
221
222 r = rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL id=water payee=CityWater amount=4500 due=2026-06-10 autopay=MANUAL state=OVERDUE" as *u8)
223 pass = pass + rg_row("bill-overdue" as *u8, r)
224
225 r = rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL id=phone payee=TelCo amount=6000 due=2026-06-25 autopay=MANUAL state=UPCOMING" as *u8)
226 pass = pass + rg_row("bill-upcoming" as *u8, r)
227
228 r = rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL id=netflix payee=Netflix amount=1599 due=2026-06-20 autopay=AUTO state=DUE" as *u8)
229 pass = pass + rg_row("paid-this-month-scoped" as *u8, r)
230
231 r = rg_has("/tmp/bl_st.txt" as *u8, "VIZSLA-BILL-VERDICT bills=5 paid=1 upcoming=1 due=2 overdue=1 owed_cents=24099" as *u8)
232 pass = pass + rg_row("status-verdict" as *u8, r)
233
234 // row 11: DATA-DRIVEN soon_days=14 -> phone DUE
235 let rc11: i64 = rg_run(rb, "status" as *u8, pfx, "/tmp/bl_bills14.txt" as *u8, "2026-06-14" as *u8, "/tmp/bl_st14.txt" as *u8)
236 r = 0
237 if rc11 == 0 {
238 if rg_has("/tmp/bl_st14.txt" as *u8, "VIZSLA-BILL-VERDICT bills=5 paid=1 upcoming=0 due=3 overdue=1 owed_cents=24099" as *u8) == 1 { r = 1 }
239 }
240 pass = pass + rg_row("data-driven-soon-days" as *u8, r)
241
242 // row 12: status determinism
243 rg_run(rb, "status" as *u8, pfx, fb, "2026-06-14" as *u8, "/tmp/bl_st2.txt" as *u8)
244 r = rg_fileeq("/tmp/bl_st.txt" as *u8, "/tmp/bl_st2.txt" as *u8)
245 pass = pass + rg_row("status-determinism" as *u8, r)
246
247 let permil: i64 = (pass * 1000) / 12
248 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
249 var fdi: i64 = 0
250 while fdi < 2 {
251 var fd: i64 = 1
252 if fdi == 1 { fd = logfd }
253 if fd > 0 {
254 let line: *u8 = sys_mmap(256)
255 var o: i64 = 0
256 o = rg_cat(line, o, "VIZSLA-BILL-GATE epoch=" as *u8)
257 o = rg_catn(line, o, sys_now_realtime_sec())
258 o = rg_cat(line, o, " rows=12 pass=" as *u8)
259 o = rg_catn(line, o, pass)
260 o = rg_cat(line, o, " permil=" as *u8)
261 o = rg_catn(line, o, permil)
262 if pass == 12 {
263 o = rg_cat(line, o, " verdict=GREEN\n" as *u8)
264 } else {
265 o = rg_cat(line, o, " verdict=RED\n" as *u8)
266 }
267 sys_write(fd, line, o)
268 }
269 fdi = fdi + 1
270 }
271 if logfd > 0 { sys_close(logfd) }
272 if pass == 12 { return 0 }
273 return 1
274}