code wiki / _hdl_build / nx_vizsla_relate_gate.nx
nx_vizsla_relate_gate.nx source
↩ module page · 334 lines · 14919 B
1// nx_vizsla_relate_gate.nx -- VIZSLA V4 gate: the relationships/PRM engine is
2// gate-proven against a HAND-COMPUTED fixture (the oracle is THIS header, not
3// the module under test). No mocks: it drives the real organ over real
4// seg_store records and hand-counted civil-date arithmetic.
5//
6// contacts (soon_days=14): TIER inner 30, close 90, network 180;
7// mom(inner, bday 06-20), alex(close, bday 03-15), sam(network, bday 12-01),
8// pat(close, bday -)
9// relate_log (11 events):
10// TOUCH 2026-06-10 mom ; TOUCH 2026-02-01 alex
11// OBLIGATION 2026-05-01 OWE alex book-loan 0 borrowed-book (open)
12// OBLIGATION 2026-05-10 DUE sam favor-intro 0 made-intro (open)
13// OBLIGATION 2026-04-01 OWE mom loan-200 20000 cash-loan (SETTLED below)
14// OBLIGATION 2026-05-05 DUE pat loan-50 5000 lent-cash (open)
15// SETTLE 2026-05-20 loan-200 repaid
16// THANKS 2026-06-12 mom birthday-gift (open, 2d)
17// THANKS 2026-05-01 alex referral (open, 44d)
18// THANKS 2026-04-01 sam holiday-card (SENT below -> closed)
19// SENT 2026-04-05 sam holiday-card
20// today=2026-06-14 hand-computed:
21// cadence: mom days=4 FRESH(<=30); alex days=133 DUE(>90); sam/pat NEW
22// => contacts=4 fresh=1 due=1 new=2
23// ledger : open=3 [alex OWE 0, sam DUE 0, pat DUE 5000]; mom SETTLED
24// => open=3 owe=1 due=2 owe_amount=0 due_amount=5000 settled=1
25// thanks : mom 2d PENDING(<=14), alex 44d OVERDUE; sam SENT-closed
26// => open=2 pending=1 overdue=1 sent=1
27// brief : followups=3 (alex DUE, sam/pat NEW), birthdays=1 (mom 06-20 in 6d),
28// thankyous=2, obligations=3
29// DATA-DRIVEN: contacts with soon_days=1 (only change) => thanks
30// open=2 pending=0 overdue=2 sent=1 (mom flips PENDING->OVERDUE),
31// same binary.
32//
33// Rows:
34// 1 loud-fail-missing-contacts cadence on absent file => exit 1
35// 2 load-events scanned=11 new=11 segment=seg-4001
36// 3 idempotent-reload new=0 dup_instore=11 segment=none (law 10)
37// 4 cadence-fresh mom FRESH days=4 cadence=30
38// 5 cadence-due alex DUE days=133 reconnect
39// 6 cadence-new-verdict sam NEW + verdict 4/1/1/2
40// 7 ledger-bidirectional alex OWE + pat DUE 5000 lent-cash + verdict
41// 8 ledger-settle-closes settled loan-200 ABSENT from open ledger
42// 9 thanks-pending mom birthday-gift 2d PENDING
43// 10 thanks-overdue-sent-closes alex 44d OVERDUE + verdict + sam holiday-card ABSENT
44// 11 brief-composition verdict 3/1/2/3 + mom BIRTHDAY in=6d
45// 12 data-driven-soon-days soon_days=1 file => thanks pending=0 overdue=2
46// 13 cadence-determinism byte-identical twice
47// Evidence: VIZSLA-RELATE-GATE line -> stdout + knowledge/status/vizsla_gate.log;
48// exit 0 iff 13/13. Missing-instrument tamper run = pass a bogus instrument
49// path as argv[1] => all rows FAIL => RED (proves the gate discriminates).
50// spec: knowledge/specs/2026-06-10-nishi-vizsla-ladder.md license_tier: ORIGINAL
51import "nx_syscalls.nx"
52
53func rg_slen(s: *u8) -> i64 {
54 var n: i64 = 0
55 while s[n] != (0 as u8) { n = n + 1 }
56 return n
57}
58
59func rg_p(s: *u8) -> i64 {
60 sys_write(1, s, rg_slen(s))
61 return 0
62}
63
64func rg_cat(dst: *u8, off: i64, s: *u8) -> i64 {
65 var i: i64 = 0
66 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
67 return off + i
68}
69
70func rg_catn(dst: *u8, off: i64, v: i64) -> i64 {
71 var o: i64 = off
72 var m: i64 = v
73 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
74 let t: *u8 = sys_mmap(28)
75 var k: i64 = 0
76 if m == 0 { t[0] = 48 as u8; k = 1 }
77 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
78 var i: i64 = 0
79 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
80 return o + k
81}
82
83func rg_write(path: *u8, content: *u8) -> i64 {
84 let fd: i64 = sys_openat_wr(path, 0x1a4)
85 if fd < 0 { return 0 - 1 }
86 sys_write(fd, content, rg_slen(content))
87 sys_close(fd)
88 return 0
89}
90
91func rg_readall(path: *u8, szout: *i64) -> *u8 {
92 let fd: i64 = sys_openat_rd(path)
93 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
94 let sz: i64 = sys_lseek(fd, 0, 2)
95 sys_lseek(fd, 0, 0)
96 let buf: *u8 = sys_mmap(sz + 64)
97 var got: i64 = 0
98 var n: i64 = 1
99 while n > 0 {
100 n = sys_read(fd, (buf as i64 + got) as *u8, 65536)
101 if n > 0 { got = got + n }
102 }
103 sys_close(fd)
104 szout[0] = got
105 return buf
106}
107
108func rg_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, outpath: *u8) -> i64 {
109 let pid: i64 = sys_fork()
110 if pid == 0 {
111 if (outpath as i64) != 0 {
112 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
113 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
114 }
115 let argv: *i64 = sys_mmap(64) as *i64
116 argv[0] = elf as i64
117 var i: i64 = 1
118 if (a1 as i64) != 0 { argv[i] = a1 as i64; i = i + 1 }
119 if (a2 as i64) != 0 { argv[i] = a2 as i64; i = i + 1 }
120 if (a3 as i64) != 0 { argv[i] = a3 as i64; i = i + 1 }
121 if (a4 as i64) != 0 { argv[i] = a4 as i64; i = i + 1 }
122 argv[i] = 0
123 let envp: *i64 = sys_mmap(16) as *i64
124 envp[0] = 0
125 sys_execve(elf, argv, envp)
126 sys_exit(127)
127 }
128 let st: *i64 = sys_mmap(16) as *i64
129 sys_wait4(pid, st, 0)
130 let sig: i64 = st[0] & 0x7f
131 if sig != 0 { return 128 + sig }
132 return (st[0] >> 8) & 0xff
133}
134
135func rg_has(path: *u8, needle: *u8) -> i64 {
136 let szp: *i64 = sys_mmap(16) as *i64
137 let b: *u8 = rg_readall(path, szp)
138 let sz: i64 = szp[0]
139 let n: i64 = rg_slen(needle)
140 if sz < n { return 0 }
141 var i: i64 = 0
142 while i + n <= sz {
143 var ok: i64 = 1
144 var j: i64 = 0
145 while j < n {
146 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 }
147 }
148 if ok == 1 { return 1 }
149 i = i + 1
150 }
151 return 0
152}
153
154func rg_fileeq(p1: *u8, p2: *u8) -> i64 {
155 let s1: *i64 = sys_mmap(16) as *i64
156 let s2: *i64 = sys_mmap(16) as *i64
157 let b1: *u8 = rg_readall(p1, s1)
158 let b2: *u8 = rg_readall(p2, s2)
159 if s1[0] != s2[0] { return 0 }
160 if s1[0] <= 0 { return 0 }
161 var i: i64 = 0
162 while i < s1[0] {
163 if b1[i] != b2[i] { return 0 }
164 i = i + 1
165 }
166 return 1
167}
168
169func rg_row(name: *u8, pass: i64) -> i64 {
170 rg_p("ROW " as *u8)
171 rg_p(name)
172 if pass == 1 { rg_p(" PASS\n" as *u8) } else { rg_p(" FAIL\n" as *u8) }
173 return pass
174}
175
176func main(argc: i64, argv: *i64) -> i64 {
177 rg_p("=== VIZSLA RELATE GATE: hand-computed PRM KATs (bidirectional ledger + thank-yous + brief) ===\n" as *u8)
178 var rb: *u8 = "/tmp/nx_vizsla_relate.sov.elf" as *u8
179 if argc > 1 { rb = argv[1] as *u8 }
180 let pr: i64 = sys_openat_rd(rb)
181 if pr >= 0 { sys_close(pr) }
182 else {
183 rg_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
184 rg_run("_offc/nx_sov_build_run.elf" as *u8, "nx_vizsla_relate" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, "/tmp/vzl_rebuild.out" as *u8)
185 }
186
187 // epoch-unique virgin store prefix
188 let pfx: *u8 = sys_mmap(128)
189 var po: i64 = 0
190 po = rg_cat(pfx, po, "/tmp/vzlG" as *u8)
191 po = rg_catn(pfx, po, sys_now_us())
192 po = rg_cat(pfx, po, "-" as *u8)
193 pfx[po] = 0 as u8
194
195 let contacts: *u8 = "CONF soon_days 14\nTIER inner 30\nTIER close 90\nTIER network 180\nCONTACT mom Mom inner 06-20\nCONTACT alex Alex-Mentor close 03-15\nCONTACT sam Sam-Colleague network 12-01\nCONTACT pat Pat-Friend close -\n" as *u8
196 rg_write("/tmp/vzl_contacts.txt" as *u8, contacts)
197 // same data, soon_days 1 (the ONLY change): the window is data, not code
198 let contacts1: *u8 = "CONF soon_days 1\nTIER inner 30\nTIER close 90\nTIER network 180\nCONTACT mom Mom inner 06-20\nCONTACT alex Alex-Mentor close 03-15\nCONTACT sam Sam-Colleague network 12-01\nCONTACT pat Pat-Friend close -\n" as *u8
199 rg_write("/tmp/vzl_contacts1.txt" as *u8, contacts1)
200 let log: *u8 = "TOUCH 2026-06-10 mom weekly-call\nTOUCH 2026-02-01 alex lunch\nOBLIGATION 2026-05-01 OWE alex book-loan 0 borrowed-book\nOBLIGATION 2026-05-10 DUE sam favor-intro 0 made-intro\nOBLIGATION 2026-04-01 OWE mom loan-200 20000 cash-loan\nOBLIGATION 2026-05-05 DUE pat loan-50 5000 lent-cash\nSETTLE 2026-05-20 loan-200 repaid\nTHANKS 2026-06-12 mom birthday-gift\nTHANKS 2026-05-01 alex referral\nTHANKS 2026-04-01 sam holiday-card\nSENT 2026-04-05 sam holiday-card\n" as *u8
201 rg_write("/tmp/vzl_log.txt" as *u8, log)
202
203 let fco: *u8 = "/tmp/vzl_contacts.txt" as *u8
204 let flog: *u8 = "/tmp/vzl_log.txt" as *u8
205 var pass: i64 = 0
206 var r: i64 = 0
207
208 // row 1: loud-fail on missing contacts
209 let rc1: i64 = rg_run(rb, "cadence" as *u8, pfx, "/tmp/vzl_NOPE.txt" as *u8, "2026-06-14" as *u8, "/tmp/vzl_out1.txt" as *u8)
210 r = 0
211 if rc1 == 1 { r = 1 }
212 pass = pass + rg_row("loud-fail-missing-contacts" as *u8, r)
213
214 // row 2: load 11 events into seg-4001
215 let rc2: i64 = rg_run(rb, "load" as *u8, flog, pfx, "4001" as *u8, "/tmp/vzl_out2.txt" as *u8)
216 r = 0
217 if rc2 == 0 {
218 if rg_has("/tmp/vzl_out2.txt" as *u8, "VIZSLA-RELATE-LOAD scanned=11 new=11 dup_infile=0 dup_instore=0 segment=seg-4001" as *u8) == 1 { r = 1 }
219 }
220 pass = pass + rg_row("load-events" as *u8, r)
221
222 // row 3: idempotent re-load (law 10)
223 let rc3: i64 = rg_run(rb, "load" as *u8, flog, pfx, "4009" as *u8, "/tmp/vzl_out3.txt" as *u8)
224 r = 0
225 if rc3 == 0 {
226 if rg_has("/tmp/vzl_out3.txt" as *u8, "VIZSLA-RELATE-LOAD scanned=11 new=0 dup_infile=0 dup_instore=11 segment=none" as *u8) == 1 { r = 1 }
227 }
228 pass = pass + rg_row("idempotent-reload" as *u8, r)
229
230 // row 4-6: cadence
231 let rc4: i64 = rg_run(rb, "cadence" as *u8, pfx, fco, "2026-06-14" as *u8, "/tmp/vzl_cad.txt" as *u8)
232 r = 0
233 if rc4 == 0 {
234 if rg_has("/tmp/vzl_cad.txt" as *u8, "VIZSLA-RELATE-CADENCE contact=mom tier=inner last=2026-06-10 days=4 cadence=30 state=FRESH action=none" as *u8) == 1 { r = 1 }
235 }
236 pass = pass + rg_row("cadence-fresh" as *u8, r)
237
238 r = rg_has("/tmp/vzl_cad.txt" as *u8, "VIZSLA-RELATE-CADENCE contact=alex tier=close last=2026-02-01 days=133 cadence=90 state=DUE action=reconnect" as *u8)
239 pass = pass + rg_row("cadence-due" as *u8, r)
240
241 r = 0
242 if rg_has("/tmp/vzl_cad.txt" as *u8, "VIZSLA-RELATE-CADENCE contact=sam tier=network last=never days=-1 cadence=180 state=NEW action=introduce" as *u8) == 1 {
243 if rg_has("/tmp/vzl_cad.txt" as *u8, "VIZSLA-RELATE-CADENCE-VERDICT contacts=4 fresh=1 due=1 new=2" as *u8) == 1 { r = 1 }
244 }
245 pass = pass + rg_row("cadence-new-verdict" as *u8, r)
246
247 // row 7-8: ledger (bidirectional + settle closes)
248 let rc7: i64 = rg_run(rb, "ledger" as *u8, pfx, fco, 0 as *u8, "/tmp/vzl_led.txt" as *u8)
249 r = 0
250 if rc7 == 0 {
251 if rg_has("/tmp/vzl_led.txt" as *u8, "VIZSLA-RELATE-OBLIG contact=alex dir=OWE ref=book-loan amount=0 desc=borrowed-book state=OPEN" as *u8) == 1 {
252 if rg_has("/tmp/vzl_led.txt" as *u8, "VIZSLA-RELATE-OBLIG contact=pat dir=DUE ref=loan-50 amount=5000 desc=lent-cash state=OPEN" as *u8) == 1 {
253 if rg_has("/tmp/vzl_led.txt" as *u8, "VIZSLA-RELATE-OBLIG-VERDICT open=3 owe=1 due=2 owe_amount=0 due_amount=5000 settled=1" as *u8) == 1 { r = 1 }
254 }
255 }
256 }
257 pass = pass + rg_row("ledger-bidirectional" as *u8, r)
258
259 // row 8: the settled obligation is ABSENT from the open ledger (additive
260 // close). Guard the negative assertion with a liveness check so it cannot
261 // pass vacuously on empty output (no-cheat: a tamper must read RED here too).
262 r = 0
263 if rg_has("/tmp/vzl_led.txt" as *u8, "VIZSLA-RELATE-OBLIG-VERDICT" as *u8) == 1 {
264 if rg_has("/tmp/vzl_led.txt" as *u8, "ref=loan-200" as *u8) == 0 { r = 1 }
265 }
266 pass = pass + rg_row("ledger-settle-closes" as *u8, r)
267
268 // row 9-10: thanks (pending / overdue + sent closes)
269 let rc9: i64 = rg_run(rb, "thanks" as *u8, pfx, fco, "2026-06-14" as *u8, "/tmp/vzl_thx.txt" as *u8)
270 r = 0
271 if rc9 == 0 {
272 if rg_has("/tmp/vzl_thx.txt" as *u8, "VIZSLA-RELATE-THANKS contact=mom occasion=birthday-gift owed=2026-06-12 days=2 state=PENDING" as *u8) == 1 { r = 1 }
273 }
274 pass = pass + rg_row("thanks-pending" as *u8, r)
275
276 r = 0
277 if rg_has("/tmp/vzl_thx.txt" as *u8, "VIZSLA-RELATE-THANKS contact=alex occasion=referral owed=2026-05-01 days=44 state=OVERDUE" as *u8) == 1 {
278 if rg_has("/tmp/vzl_thx.txt" as *u8, "VIZSLA-RELATE-THANKS-VERDICT open=2 pending=1 overdue=1 sent=1" as *u8) == 1 {
279 if rg_has("/tmp/vzl_thx.txt" as *u8, "occasion=holiday-card" as *u8) == 0 { r = 1 }
280 }
281 }
282 pass = pass + rg_row("thanks-overdue-sent-closes" as *u8, r)
283
284 // row 11: brief composition
285 let rc11: i64 = rg_run(rb, "brief" as *u8, pfx, fco, "2026-06-14" as *u8, "/tmp/vzl_brf.txt" as *u8)
286 r = 0
287 if rc11 == 0 {
288 if rg_has("/tmp/vzl_brf.txt" as *u8, " BIRTHDAY contact=mom on=06-20 in=6d" as *u8) == 1 {
289 if rg_has("/tmp/vzl_brf.txt" as *u8, "VIZSLA-RELATE-BRIEF-VERDICT followups=3 birthdays=1 thankyous=2 obligations=3" as *u8) == 1 { r = 1 }
290 }
291 }
292 pass = pass + rg_row("brief-composition" as *u8, r)
293
294 // row 12: DATA-DRIVEN soon_days=1 file => thanks flips, same binary
295 let rc12: i64 = rg_run(rb, "thanks" as *u8, pfx, "/tmp/vzl_contacts1.txt" as *u8, "2026-06-14" as *u8, "/tmp/vzl_thx1.txt" as *u8)
296 r = 0
297 if rc12 == 0 {
298 if rg_has("/tmp/vzl_thx1.txt" as *u8, "VIZSLA-RELATE-THANKS-VERDICT open=2 pending=0 overdue=2 sent=1" as *u8) == 1 { r = 1 }
299 }
300 pass = pass + rg_row("data-driven-soon-days" as *u8, r)
301
302 // row 13: cadence determinism
303 rg_run(rb, "cadence" as *u8, pfx, fco, "2026-06-14" as *u8, "/tmp/vzl_cad2.txt" as *u8)
304 r = rg_fileeq("/tmp/vzl_cad.txt" as *u8, "/tmp/vzl_cad2.txt" as *u8)
305 pass = pass + rg_row("cadence-determinism" as *u8, r)
306
307 let permil: i64 = (pass * 1000) / 13
308 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
309 var fdi: i64 = 0
310 while fdi < 2 {
311 var fd: i64 = 1
312 if fdi == 1 { fd = logfd }
313 if fd > 0 {
314 let line: *u8 = sys_mmap(256)
315 var o: i64 = 0
316 o = rg_cat(line, o, "VIZSLA-RELATE-GATE epoch=" as *u8)
317 o = rg_catn(line, o, sys_now_realtime_sec())
318 o = rg_cat(line, o, " rows=13 pass=" as *u8)
319 o = rg_catn(line, o, pass)
320 o = rg_cat(line, o, " permil=" as *u8)
321 o = rg_catn(line, o, permil)
322 if pass == 13 {
323 o = rg_cat(line, o, " verdict=GREEN\n" as *u8)
324 } else {
325 o = rg_cat(line, o, " verdict=RED\n" as *u8)
326 }
327 sys_write(fd, line, o)
328 }
329 fdi = fdi + 1
330 }
331 if logfd > 0 { sys_close(logfd) }
332 if pass == 13 { return 0 }
333 return 1
334}