code wiki / _hdl_build / nx_docx_rich_gate.nx
nx_docx_rich_gate.nx source
↩ module page · 236 lines · 8781 B
1// nx_docx_rich_gate.nx -- gate for RICH .docx (formatting runs + tables), the office-census gap-closer for
2// OF-W3 (formatting) + OF-W4 (tables). The OPC writer uses STORED (uncompressed) ZIP, so the raw .docx bytes
3// contain word/document.xml VERBATIM -- the gate greps them to prove the markup is really EMITTED (not just
4// that a symbol name appears in source), and reads the text back through the real docx reader (which unzips +
5// parses -> proves the OPC package is structurally valid).
6//
7// spec fixture: H heading / B bold / I italic / P plain / a 3x3 bordered TABLE / trailing plain.
8// Rows: 1 rich-write 2 text-roundtrip(heading+plain+table-cell+trailing) 3 formatting-emitted(w:rPr+w:b+w:sz)
9// 4 table-emitted(w:tbl+w:tblPr+w:tr+w:tc) 5 italic-emitted 6 determinism 7 loud-fail-missing-spec[neg]
10// Evidence: DOCX-RICH-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 7/7.
11// license_tier: ORIGINAL
12import "nx_syscalls.nx"
13
14func rg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
15func rg_p(s: *u8) -> i64 { sys_write(1, s, rg_slen(s)); return 0 }
16
17func rg_cat(dst: *u8, off: i64, s: *u8) -> i64 {
18 var i: i64 = 0
19 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
20 return off + i
21}
22
23func rg_catn(dst: *u8, off: i64, v: i64) -> i64 {
24 var o: i64 = off
25 var m: i64 = v
26 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
27 let t: *u8 = sys_mmap(28)
28 var k: i64 = 0
29 if m == 0 { t[0] = 48 as u8; k = 1 }
30 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
31 var i: i64 = 0
32 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
33 return o + k
34}
35
36func rg_write(path: *u8, content: *u8) -> i64 {
37 let fd: i64 = sys_openat_wr(path, 0x1a4)
38 if fd < 0 { return 0 - 1 }
39 sys_write(fd, content, rg_slen(content))
40 sys_close(fd)
41 return 0
42}
43
44func rg_readall(path: *u8, szout: *i64) -> *u8 {
45 let fd: i64 = sys_openat_rd(path)
46 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
47 let sz: i64 = sys_lseek(fd, 0, 2)
48 sys_lseek(fd, 0, 0)
49 let buf: *u8 = sys_mmap(sz + 64)
50 var got: i64 = 0
51 var n: i64 = 1
52 while n > 0 {
53 n = sys_read(fd, (buf as i64 + got) as *u8, 65536)
54 if n > 0 { got = got + n }
55 }
56 sys_close(fd)
57 szout[0] = got
58 return buf
59}
60
61func rg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
62 let pid: i64 = sys_fork()
63 if pid == 0 {
64 if (outpath as i64) != 0 {
65 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
66 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
67 }
68 let argv: *i64 = sys_mmap(128) as *i64
69 argv[0] = elf as i64
70 var i: i64 = 0
71 var go: i64 = 1
72 while go == 1 {
73 if args[i] == 0 { go = 0 } else {
74 argv[i + 1] = args[i]
75 i = i + 1
76 }
77 }
78 argv[i + 1] = 0
79 let envp: *i64 = sys_mmap(16) as *i64
80 envp[0] = 0
81 sys_execve(elf, argv, envp)
82 sys_exit(127)
83 }
84 let st: *i64 = sys_mmap(16) as *i64
85 sys_wait4(pid, st, 0)
86 let sig: i64 = st[0] & 0x7f
87 if sig != 0 { return 128 + sig }
88 return (st[0] >> 8) & 0xff
89}
90
91func rg_has(path: *u8, needle: *u8) -> i64 {
92 let szp: *i64 = sys_mmap(16) as *i64
93 let b: *u8 = rg_readall(path, szp)
94 let sz: i64 = szp[0]
95 let n: i64 = rg_slen(needle)
96 if sz < n { return 0 }
97 var i: i64 = 0
98 while i + n <= sz {
99 var ok: i64 = 1
100 var j: i64 = 0
101 while j < n {
102 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 }
103 }
104 if ok == 1 { return 1 }
105 i = i + 1
106 }
107 return 0
108}
109
110func rg_fileeq(p1: *u8, p2: *u8) -> i64 {
111 let s1: *i64 = sys_mmap(16) as *i64
112 let s2: *i64 = sys_mmap(16) as *i64
113 let b1: *u8 = rg_readall(p1, s1)
114 let b2: *u8 = rg_readall(p2, s2)
115 if s1[0] != s2[0] { return 0 }
116 if s1[0] <= 0 { return 0 }
117 var i: i64 = 0
118 while i < s1[0] {
119 if b1[i] != b2[i] { return 0 }
120 i = i + 1
121 }
122 return 1
123}
124
125func rg_row(name: *u8, pass: i64) -> i64 {
126 rg_p("ROW " as *u8)
127 rg_p(name)
128 if pass == 1 { rg_p(" PASS\n" as *u8) } else { rg_p(" FAIL\n" as *u8) }
129 return pass
130}
131
132func rg_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 {
133 let a: *i64 = sys_mmap(64) as *i64
134 a[0] = a1 as i64
135 a[1] = a2 as i64
136 a[2] = a3 as i64
137 a[3] = a4 as i64
138 a[4] = 0
139 return a
140}
141
142func main(argc: i64, argv: *i64) -> i64 {
143 rg_p("=== DOCX RICH GATE: formatting runs (bold/italic/heading) + TABLES emit + round-trip ===\n" as *u8)
144 let docx: *u8 = "/tmp/nx_docx.sov.elf" as *u8
145 let pr: i64 = sys_openat_rd(docx)
146 if pr >= 0 { sys_close(pr) } else {
147 rg_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
148 rg_runv("_offc/nx_sov_build_run.elf" as *u8, rg_args("nx_docx" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/dxr_rebuild.out" as *u8)
149 }
150
151 rg_write("/tmp/dxr_spec.txt" as *u8, "H Meeting Agenda\nB Attendees\nP Alice, Bob, and Carol\nI noted for the record\nT Item|Owner|Status\nT Budget|Alice|Done\nT Venue|Bob|Open\nP End of agenda\n" as *u8)
152
153 var pass: i64 = 0
154 var r: i64 = 0
155
156 // row 1: write rich
157 let rc1: i64 = rg_runv(docx, rg_args("writerich" as *u8, "/tmp/dxr_rich.docx" as *u8, "/tmp/dxr_spec.txt" as *u8, 0 as *u8), "/tmp/dxr_r1.txt" as *u8)
158 r = 0
159 if rc1 == 0 { if rg_has("/tmp/dxr_r1.txt" as *u8, "DOCX-WRITE-RICH-OK path=/tmp/dxr_rich.docx bytes=" as *u8) == 1 { r = 1 } }
160 pass = pass + rg_row("rich-write" as *u8, r)
161
162 // row 2: text round-trips (heading + plain + a table cell + trailing) through the REAL docx reader
163 let rc2: i64 = rg_runv(docx, rg_args("read" as *u8, "/tmp/dxr_rich.docx" as *u8, 0 as *u8, 0 as *u8), "/tmp/dxr_r2.txt" as *u8)
164 r = 0
165 if rc2 == 0 {
166 if rg_has("/tmp/dxr_r2.txt" as *u8, "Meeting Agenda" as *u8) == 1 {
167 if rg_has("/tmp/dxr_r2.txt" as *u8, "Alice, Bob, and Carol" as *u8) == 1 {
168 if rg_has("/tmp/dxr_r2.txt" as *u8, "Budget" as *u8) == 1 {
169 if rg_has("/tmp/dxr_r2.txt" as *u8, "End of agenda" as *u8) == 1 { r = 1 }
170 }
171 }
172 }
173 }
174 pass = pass + rg_row("text-roundtrip" as *u8, r)
175
176 // row 3: formatting really EMITTED (raw STORED .docx bytes carry the run-properties)
177 r = 0
178 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:rPr><w:b/>" as *u8) == 1 {
179 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:sz w:val=\"32\"/>" as *u8) == 1 { r = 1 }
180 }
181 pass = pass + rg_row("formatting-emitted" as *u8, r)
182
183 // row 4: table really EMITTED (tbl + tblPr + tr + tc)
184 r = 0
185 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tbl>" as *u8) == 1 {
186 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tblPr>" as *u8) == 1 {
187 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tr>" as *u8) == 1 {
188 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tc>" as *u8) == 1 { r = 1 }
189 }
190 }
191 }
192 pass = pass + rg_row("table-emitted" as *u8, r)
193
194 // row 5: italic run
195 r = rg_has("/tmp/dxr_rich.docx" as *u8, "<w:i/>" as *u8)
196 pass = pass + rg_row("italic-emitted" as *u8, r)
197
198 // row 6: determinism
199 rg_runv(docx, rg_args("writerich" as *u8, "/tmp/dxr_rich2.docx" as *u8, "/tmp/dxr_spec.txt" as *u8, 0 as *u8), "/tmp/dxr_r6.txt" as *u8)
200 r = rg_fileeq("/tmp/dxr_rich.docx" as *u8, "/tmp/dxr_rich2.docx" as *u8)
201 pass = pass + rg_row("determinism" as *u8, r)
202
203 // row 7: missing spec (negative control)
204 let rc7: i64 = rg_runv(docx, rg_args("writerich" as *u8, "/tmp/dxr_x.docx" as *u8, "/tmp/dxr_nope.txt" as *u8, 0 as *u8), "/tmp/dxr_r7.txt" as *u8)
205 r = 0
206 if rc7 == 1 { r = 1 }
207 pass = pass + rg_row("loud-fail-missing-spec" as *u8, r)
208
209 let permil: i64 = (pass * 1000) / 7
210 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
211 var fdi: i64 = 0
212 while fdi < 2 {
213 var fd: i64 = 1
214 if fdi == 1 { fd = logfd }
215 if fd > 0 {
216 let line: *u8 = sys_mmap(256)
217 var o: i64 = 0
218 o = rg_cat(line, o, "DOCX-RICH-GATE epoch=" as *u8)
219 o = rg_catn(line, o, sys_now_realtime_sec())
220 o = rg_cat(line, o, " rows=7 pass=" as *u8)
221 o = rg_catn(line, o, pass)
222 o = rg_cat(line, o, " permil=" as *u8)
223 o = rg_catn(line, o, permil)
224 if pass == 7 {
225 o = rg_cat(line, o, " verdict=GREEN\n" as *u8)
226 } else {
227 o = rg_cat(line, o, " verdict=RED\n" as *u8)
228 }
229 sys_write(fd, line, o)
230 }
231 fdi = fdi + 1
232 }
233 if logfd > 0 { sys_close(logfd) }
234 if pass == 7 { return 0 }
235 return 1
236}