code wiki / _hdl_build / nx_vizsla_capture_gate.nx
nx_vizsla_capture_gate.nx source
↩ module page · 191 lines · 8812 B
1// nx_vizsla_capture_gate.nx -- VIZSLA capture gate: the opt-in page->person extractor pulls a contact from a
2// loaded page's structured data (JSON-LD Person > og:title > <title>), strips the site suffix, and emits an
3// action-ready suggestion -- NEVER auto-adding. 100% sovereign: writes synthetic page fixtures, runs the
4// extractor, greps the suggestion. (The realm choice + confirm happen downstream; this is just the extractor.)
5//
6// Rows:
7// 1 jsonld-person JSON-LD {"@type":"Person","name":"Ada Lovelace"} -> id=ada-lovelace name=Ada-Lovelace source=jsonld
8// 2 og-title <meta og:title "Grace Hopper - Rear Admiral | LinkedIn"> -> id=grace-hopper source=og (suffix stripped)
9// 3 title-fallback <title>Alan Turing | LinkedIn</title> -> id=alan-turing source=title (suffix stripped)
10// 4 priority-jsonld JSON-LD + og both present -> JSON-LD wins (source=jsonld, NOT the og "Wrong Name")
11// 5 form-output emits VIZSLA-CAPTURE-FORM id=ada-lovelace&name=Ada-Lovelace (action-ready body)
12// 6 neg-no-person a page with no Person/og/title -> exit 1
13// Evidence: VIZSLA-CAPTURE-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 6/6.
14// license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_gate_verdict.nx"
17
18func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
19func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 }
20
21func og_cat(dst: *u8, off: i64, s: *u8) -> i64 {
22 var i: i64 = 0
23 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
24 return off + i
25}
26
27func og_catn(dst: *u8, off: i64, v: i64) -> i64 {
28 var o: i64 = off
29 var m: i64 = v
30 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
31 let t: *u8 = sys_mmap(28)
32 var k: i64 = 0
33 if m == 0 { t[0] = 48 as u8; k = 1 }
34 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
35 var i: i64 = 0
36 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
37 return o + k
38}
39
40func og_write(path: *u8, content: *u8) -> i64 {
41 let fd: i64 = sys_openat_wr(path, 0x1a4)
42 if fd < 0 { return 0 - 1 }
43 sys_write(fd, content, og_slen(content))
44 sys_close(fd)
45 return 0
46}
47
48func og_readall(path: *u8, szout: *i64) -> *u8 {
49 let fd: i64 = sys_openat_rd(path)
50 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
51 let sz: i64 = sys_lseek(fd, 0, 2)
52 sys_lseek(fd, 0, 0)
53 let buf: *u8 = sys_mmap(sz + 64)
54 var got: i64 = 0
55 var n: i64 = 1
56 while n > 0 { n = sys_read(fd, (buf as i64 + got) as *u8, 65536); if n > 0 { got = got + n } }
57 sys_close(fd)
58 szout[0] = got
59 return buf
60}
61
62func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
63 let pid: i64 = sys_fork()
64 if pid == 0 {
65 if (outpath as i64) != 0 {
66 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
67 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
68 }
69 let argv: *i64 = sys_mmap(128) as *i64
70 argv[0] = elf as i64
71 var i: i64 = 0
72 var go: i64 = 1
73 while go == 1 { if args[i] == 0 { go = 0 } else { argv[i + 1] = args[i]; i = i + 1 } }
74 argv[i + 1] = 0
75 let envp: *i64 = sys_mmap(16) as *i64
76 envp[0] = 0
77 sys_execve(elf, argv, envp)
78 sys_exit(127)
79 }
80 let st: *i64 = sys_mmap(16) as *i64
81 sys_wait4(pid, st, 0)
82 let sig: i64 = st[0] & 0x7f
83 if sig != 0 { return 128 + sig }
84 return (st[0] >> 8) & 0xff
85}
86
87func og_has(path: *u8, needle: *u8) -> i64 {
88 let szp: *i64 = sys_mmap(16) as *i64
89 let b: *u8 = og_readall(path, szp)
90 let sz: i64 = szp[0]
91 let n: i64 = og_slen(needle)
92 if sz < n { return 0 }
93 var i: i64 = 0
94 while i + n <= sz {
95 var ok: i64 = 1
96 var j: i64 = 0
97 while j < n { if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } }
98 if ok == 1 { return 1 }
99 i = i + 1
100 }
101 return 0
102}
103
104func og_row(name: *u8, pass: i64) -> i64 {
105 og_p("ROW " as *u8); og_p(name)
106 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) }
107 return pass
108}
109
110func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 {
111 let a: *i64 = sys_mmap(64) as *i64
112 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
113 return a
114}
115
116func main(argc: i64, argv: *i64) -> i64 {
117 og_p("=== VIZSLA CAPTURE GATE: opt-in page->person extractor (JSON-LD/og/title) ===\n" as *u8)
118 var ob: *u8 = "buildroot/_build/nx_vizsla_capture.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_capture" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vzcap_rebuild.out" as *u8)
125 }
126
127 og_write("/tmp/cap_jsonld.html" as *u8, "<html><head><script type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"Person\",\"name\":\"Ada Lovelace\",\"jobTitle\":\"Mathematician\"}</script></head><body>x</body></html>" as *u8)
128 og_write("/tmp/cap_og.html" as *u8, "<html><head><meta property=\"og:title\" content=\"Grace Hopper - Rear Admiral | LinkedIn\"></head><body></body></html>" as *u8)
129 og_write("/tmp/cap_title.html" as *u8, "<html><head><title>Alan Turing | LinkedIn</title></head><body></body></html>" as *u8)
130 og_write("/tmp/cap_pri.html" as *u8, "<html><head><script type=\"application/ld+json\">{\"@type\":\"Person\",\"name\":\"Ada Lovelace\"}</script><meta property=\"og:title\" content=\"Wrong Name\"></head></html>" as *u8)
131 og_write("/tmp/cap_none.html" as *u8, "<html><head></head><body>just some text, nobody here</body></html>" as *u8)
132
133 var pass: i64 = 0
134 var r: i64 = 0
135
136 og_runv(ob, og_args("extract" as *u8, "/tmp/cap_jsonld.html" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/cap_r1.txt" as *u8)
137 r = og_has("/tmp/cap_r1.txt" as *u8, "VIZSLA-CAPTURE id=ada-lovelace name=Ada-Lovelace source=jsonld" as *u8)
138 pass = pass + og_row("jsonld-person" as *u8, r)
139
140 og_runv(ob, og_args("extract" as *u8, "/tmp/cap_og.html" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/cap_r2.txt" as *u8)
141 r = og_has("/tmp/cap_r2.txt" as *u8, "id=grace-hopper name=Grace-Hopper source=og" as *u8)
142 pass = pass + og_row("og-title" as *u8, r)
143
144 og_runv(ob, og_args("extract" as *u8, "/tmp/cap_title.html" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/cap_r3.txt" as *u8)
145 r = og_has("/tmp/cap_r3.txt" as *u8, "id=alan-turing name=Alan-Turing source=title" as *u8)
146 pass = pass + og_row("title-fallback" as *u8, r)
147
148 og_runv(ob, og_args("extract" as *u8, "/tmp/cap_pri.html" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/cap_r4.txt" as *u8)
149 r = 0
150 if og_has("/tmp/cap_r4.txt" as *u8, "source=jsonld" as *u8) == 1 { if og_has("/tmp/cap_r4.txt" as *u8, "Wrong" as *u8) == 0 { r = 1 } }
151 pass = pass + og_row("priority-jsonld-wins" as *u8, r)
152
153 r = og_has("/tmp/cap_r1.txt" as *u8, "VIZSLA-CAPTURE-FORM id=ada-lovelace&name=Ada-Lovelace" as *u8)
154 pass = pass + og_row("form-output" as *u8, r)
155
156 let rc6: i64 = og_runv(ob, og_args("extract" as *u8, "/tmp/cap_none.html" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/cap_r6.txt" as *u8)
157 r = 0
158 if rc6 == 1 { r = 1 }
159 pass = pass + og_row("neg-no-person" as *u8, r)
160
161 let permil: i64 = (pass * 1000) / 6
162 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
163 var fdi: i64 = 0
164 while fdi < 2 {
165 var fd: i64 = 1
166 if fdi == 1 { fd = logfd }
167 if fd > 0 {
168 let line: *u8 = sys_mmap(256)
169 var o: i64 = 0
170 o = og_cat(line, o, "VIZSLA-CAPTURE-GATE epoch=" as *u8)
171 o = og_catn(line, o, sys_now_realtime_sec())
172 o = og_cat(line, o, " rows=6 pass=" as *u8)
173 o = og_catn(line, o, pass)
174 o = og_cat(line, o, " permil=" as *u8)
175 o = og_catn(line, o, permil)
176 if pass == 6 { o = og_cat(line, o, " verdict=GREEN\n" as *u8) } else { o = og_cat(line, o, " verdict=RED\n" as *u8) }
177 sys_write(fd, line, o)
178 }
179 fdi = fdi + 1
180 }
181 if logfd > 0 { sys_close(logfd) }
182 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
183 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
184 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
185 let ctr__dry: *i64 = gv_ctr()
186 ctr__dry[0] = pass
187 ctr__dry[1] = 6
188 let rc__dry: i64 = gv_verdict("VIZSLA-CAPTURE-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
189 sys_exit(rc__dry)
190 return rc__dry
191}