code wiki / _hdl_build / nx_vizsla_action_gate.nx
nx_vizsla_action_gate.nx source
↩ module page · 211 lines · 9274 B
1// nx_vizsla_action_gate.nx -- VIZSLA action gate: a first-party browser FORM POST (URL-encoded body) becomes a
2// real relationship mutation. Proves the controller decodes + dispatches to the gated tenant engine = you
3// manage IN the page, not by dropping files. 100% sovereign: provisions a tenant, feeds urlencoded bodies to
4// nx_vizsla_action, greps the tenant files + the resulting brief.
5//
6// Rows:
7// 1 add-contact-full "id=newp&name=New+Person&tier=inner&bday=07-04" -> CONTACT newp New-Person inner 07-04
8// 2 add-contact-derive-id "name=Jane+Doe&tier=close" -> CONTACT jane-doe Jane-Doe close -
9// 3 urldecode-pct "id=pct&name=Pct%20Test&tier=network" -> CONTACT pct Pct-Test network - (%20 decode)
10// 4 touch-appends "date=2026-06-22&id=newp¬e=called+them+today"-> TOUCH 2026-06-22 newp called-them-today
11// 5 touch-makes-fresh brief: jane-doe IS a FOLLOWUP, newp is NOT (touched today = FRESH)
12// 6 neg-unknown-action submit action=frobnicate -> exit 1
13// 7 neg-empty-name submit add-contact with empty body -> exit 1
14// Evidence: VIZSLA-ACTION-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 7/7.
15// license_tier: ORIGINAL
16import "nx_syscalls.nx"
17import "nx_gate_verdict.nx"
18
19func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
20func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 }
21
22func og_cat(dst: *u8, off: i64, s: *u8) -> i64 {
23 var i: i64 = 0
24 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
25 return off + i
26}
27
28func og_catn(dst: *u8, off: i64, v: i64) -> i64 {
29 var o: i64 = off
30 var m: i64 = v
31 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
32 let t: *u8 = sys_mmap(28)
33 var k: i64 = 0
34 if m == 0 { t[0] = 48 as u8; k = 1 }
35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
36 var i: i64 = 0
37 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
38 return o + k
39}
40
41func og_readall(path: *u8, szout: *i64) -> *u8 {
42 let fd: i64 = sys_openat_rd(path)
43 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
44 let sz: i64 = sys_lseek(fd, 0, 2)
45 sys_lseek(fd, 0, 0)
46 let buf: *u8 = sys_mmap(sz + 64)
47 var got: i64 = 0
48 var n: i64 = 1
49 while n > 0 { n = sys_read(fd, (buf as i64 + got) as *u8, 65536); if n > 0 { got = got + n } }
50 sys_close(fd)
51 szout[0] = got
52 return buf
53}
54
55func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
56 let pid: i64 = sys_fork()
57 if pid == 0 {
58 if (outpath as i64) != 0 {
59 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
60 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
61 }
62 let argv: *i64 = sys_mmap(128) as *i64
63 argv[0] = elf as i64
64 var i: i64 = 0
65 var go: i64 = 1
66 while go == 1 { if args[i] == 0 { go = 0 } else { argv[i + 1] = args[i]; i = i + 1 } }
67 argv[i + 1] = 0
68 let envp: *i64 = sys_mmap(16) as *i64
69 envp[0] = 0
70 sys_execve(elf, argv, envp)
71 sys_exit(127)
72 }
73 let st: *i64 = sys_mmap(16) as *i64
74 sys_wait4(pid, st, 0)
75 let sig: i64 = st[0] & 0x7f
76 if sig != 0 { return 128 + sig }
77 return (st[0] >> 8) & 0xff
78}
79
80func og_has(path: *u8, needle: *u8) -> i64 {
81 let szp: *i64 = sys_mmap(16) as *i64
82 let b: *u8 = og_readall(path, szp)
83 let sz: i64 = szp[0]
84 let n: i64 = og_slen(needle)
85 if sz < n { return 0 }
86 var i: i64 = 0
87 while i + n <= sz {
88 var ok: i64 = 1
89 var j: i64 = 0
90 while j < n { if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } }
91 if ok == 1 { return 1 }
92 i = i + 1
93 }
94 return 0
95}
96
97func og_row(name: *u8, pass: i64) -> i64 {
98 og_p("ROW " as *u8); og_p(name)
99 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) }
100 return pass
101}
102
103func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 {
104 let a: *i64 = sys_mmap(64) as *i64
105 a[0] = a1 as i64; a[1] = a2 as i64; a[2] = a3 as i64; a[3] = a4 as i64; a[4] = a5 as i64; a[5] = 0
106 return a
107}
108
109func og_join(base: *u8, name: *u8) -> *u8 {
110 let p: *u8 = sys_mmap(512)
111 var o: i64 = 0
112 o = og_cat(p, o, base); o = og_cat(p, o, name); p[o] = 0 as u8
113 return p
114}
115
116func main(argc: i64, argv: *i64) -> i64 {
117 og_p("=== VIZSLA ACTION GATE: first-party form POST -> relationship mutation ===\n" as *u8)
118 var ob: *u8 = "buildroot/_build/nx_vizsla_action.sov.elf" as *u8
119 if argc > 1 { ob = argv[1] as *u8 }
120 let pr: i64 = sys_openat_rd(ob)
121 if pr >= 0 { sys_close(pr) }
122 else {
123 og_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
124 og_runv("_offc/nx_sov_build_run.elf" as *u8, og_args("nx_vizsla_action" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vaa_rebuild.out" as *u8)
125 }
126
127 let base: *u8 = sys_mmap(256)
128 var bo: i64 = 0
129 bo = og_cat(base, bo, "/tmp/vaG" as *u8); bo = og_catn(base, bo, sys_now_us()); bo = og_cat(base, bo, "_" as *u8)
130 base[bo] = 0 as u8
131
132 // provision a fresh tenant "act" (creates default tiers inner/close/network)
133 og_runv("_offc/nx_vizsla_tenant.elf" as *u8, og_args("provision" as *u8, base, "act" as *u8, 0 as *u8, 0 as *u8), "/tmp/vaa_prov.txt" as *u8)
134
135 let actc: *u8 = og_join(base, "act.contacts.txt" as *u8)
136 let actl: *u8 = og_join(base, "act.relate_log.txt" as *u8)
137 let actp: *u8 = og_join(base, "act.relate-" as *u8)
138 var pass: i64 = 0
139 var r: i64 = 0
140
141 // row 1: add-contact full form
142 og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "add-contact" as *u8, "id=newp&name=New+Person&tier=inner&bday=07-04" as *u8), "/tmp/vaa_1.txt" as *u8)
143 r = og_has(actc, "CONTACT newp New-Person inner 07-04" as *u8)
144 pass = pass + og_row("add-contact-full" as *u8, r)
145
146 // row 2: name-only -> id derived
147 og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "add-contact" as *u8, "name=Jane+Doe&tier=close" as *u8), "/tmp/vaa_2.txt" as *u8)
148 r = og_has(actc, "CONTACT jane-doe Jane-Doe close -" as *u8)
149 pass = pass + og_row("add-contact-derive-id" as *u8, r)
150
151 // row 3: %XX urldecode
152 og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "add-contact" as *u8, "id=pct&name=Pct%20Test&tier=network" as *u8), "/tmp/vaa_3.txt" as *u8)
153 r = og_has(actc, "CONTACT pct Pct-Test network -" as *u8)
154 pass = pass + og_row("urldecode-pct" as *u8, r)
155
156 // row 4: touch appends + note sanitized
157 og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "touch" as *u8, "date=2026-06-22&id=newp¬e=called+them+today" as *u8), "/tmp/vaa_4.txt" as *u8)
158 r = og_has(actl, "TOUCH 2026-06-22 newp called-them-today" as *u8)
159 pass = pass + og_row("touch-appends" as *u8, r)
160
161 // row 5: the touch made newp FRESH (jane-doe still a followup)
162 og_runv("_offc/nx_vizsla_relate.elf" as *u8, og_args("brief" as *u8, actp, actc, "2026-06-22" as *u8, 0 as *u8), "/tmp/vaa_brief.txt" as *u8)
163 var jfollow: i64 = og_has("/tmp/vaa_brief.txt" as *u8, "FOLLOWUP contact=jane-doe" as *u8)
164 var nfollow: i64 = og_has("/tmp/vaa_brief.txt" as *u8, "FOLLOWUP contact=newp" as *u8)
165 r = 0
166 if jfollow == 1 { if nfollow == 0 { r = 1 } }
167 pass = pass + og_row("touch-makes-fresh" as *u8, r)
168
169 // row 6: neg-control unknown action
170 let rc6: i64 = og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "frobnicate" as *u8, "x=1" as *u8), "/tmp/vaa_6.txt" as *u8)
171 r = 0
172 if rc6 == 1 { r = 1 }
173 pass = pass + og_row("neg-unknown-action" as *u8, r)
174
175 // row 7: neg-control empty body (no name/id)
176 let rc7: i64 = og_runv(ob, og_args("submit" as *u8, base, "act" as *u8, "add-contact" as *u8, "tier=inner" as *u8), "/tmp/vaa_7.txt" as *u8)
177 r = 0
178 if rc7 == 1 { r = 1 }
179 pass = pass + og_row("neg-empty-name" as *u8, r)
180
181 let permil: i64 = (pass * 1000) / 7
182 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
183 var fdi: i64 = 0
184 while fdi < 2 {
185 var fd: i64 = 1
186 if fdi == 1 { fd = logfd }
187 if fd > 0 {
188 let line: *u8 = sys_mmap(256)
189 var o: i64 = 0
190 o = og_cat(line, o, "VIZSLA-ACTION-GATE epoch=" as *u8)
191 o = og_catn(line, o, sys_now_realtime_sec())
192 o = og_cat(line, o, " rows=7 pass=" as *u8)
193 o = og_catn(line, o, pass)
194 o = og_cat(line, o, " permil=" as *u8)
195 o = og_catn(line, o, permil)
196 if pass == 7 { o = og_cat(line, o, " verdict=GREEN\n" as *u8) } else { o = og_cat(line, o, " verdict=RED\n" as *u8) }
197 sys_write(fd, line, o)
198 }
199 fdi = fdi + 1
200 }
201 if logfd > 0 { sys_close(logfd) }
202 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
203 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
204 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
205 let ctr__dry: *i64 = gv_ctr()
206 ctr__dry[0] = pass
207 ctr__dry[1] = 7
208 let rc__dry: i64 = gv_verdict("VIZSLA-ACTION-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
209 sys_exit(rc__dry)
210 return rc__dry
211}