code wiki / _hdl_build / nx_docx_rich_gate.nx

nx_docx_rich_gate.nx source

↩ module page · 244 lines · 9328 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" 13import "nx_gate_verdict.nx" 14 15func rg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 16func rg_p(s: *u8) -> i64 { sys_write(1, s, rg_slen(s)); return 0 } 17 18func rg_cat(dst: *u8, off: i64, s: *u8) -> i64 { 19 var i: i64 = 0 20 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 21 return off + i 22} 23 24func rg_catn(dst: *u8, off: i64, v: i64) -> i64 { 25 var o: i64 = off 26 var m: i64 = v 27 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 28 let t: *u8 = sys_mmap(28) 29 var k: i64 = 0 30 if m == 0 { t[0] = 48 as u8; k = 1 } 31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 32 var i: i64 = 0 33 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 34 return o + k 35} 36 37func rg_write(path: *u8, content: *u8) -> i64 { 38 let fd: i64 = sys_openat_wr(path, 0x1a4) 39 if fd < 0 { return 0 - 1 } 40 sys_write(fd, content, rg_slen(content)) 41 sys_close(fd) 42 return 0 43} 44 45func rg_readall(path: *u8, szout: *i64) -> *u8 { 46 let fd: i64 = sys_openat_rd(path) 47 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 } 48 let sz: i64 = sys_lseek(fd, 0, 2) 49 sys_lseek(fd, 0, 0) 50 let buf: *u8 = sys_mmap(sz + 64) 51 var got: i64 = 0 52 var n: i64 = 1 53 while n > 0 { 54 n = sys_read(fd, (buf as i64 + got) as *u8, 65536) 55 if n > 0 { got = got + n } 56 } 57 sys_close(fd) 58 szout[0] = got 59 return buf 60} 61 62func rg_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 { 74 if args[i] == 0 { go = 0 } else { 75 argv[i + 1] = args[i] 76 i = i + 1 77 } 78 } 79 argv[i + 1] = 0 80 let envp: *i64 = sys_mmap(16) as *i64 81 envp[0] = 0 82 sys_execve(elf, argv, envp) 83 sys_exit(127) 84 } 85 let st: *i64 = sys_mmap(16) as *i64 86 sys_wait4(pid, st, 0) 87 let sig: i64 = st[0] & 0x7f 88 if sig != 0 { return 128 + sig } 89 return (st[0] >> 8) & 0xff 90} 91 92func rg_has(path: *u8, needle: *u8) -> i64 { 93 let szp: *i64 = sys_mmap(16) as *i64 94 let b: *u8 = rg_readall(path, szp) 95 let sz: i64 = szp[0] 96 let n: i64 = rg_slen(needle) 97 if sz < n { return 0 } 98 var i: i64 = 0 99 while i + n <= sz { 100 var ok: i64 = 1 101 var j: i64 = 0 102 while j < n { 103 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } 104 } 105 if ok == 1 { return 1 } 106 i = i + 1 107 } 108 return 0 109} 110 111func rg_fileeq(p1: *u8, p2: *u8) -> i64 { 112 let s1: *i64 = sys_mmap(16) as *i64 113 let s2: *i64 = sys_mmap(16) as *i64 114 let b1: *u8 = rg_readall(p1, s1) 115 let b2: *u8 = rg_readall(p2, s2) 116 if s1[0] != s2[0] { return 0 } 117 if s1[0] <= 0 { return 0 } 118 var i: i64 = 0 119 while i < s1[0] { 120 if b1[i] != b2[i] { return 0 } 121 i = i + 1 122 } 123 return 1 124} 125 126func rg_row(name: *u8, pass: i64) -> i64 { 127 rg_p("ROW " as *u8) 128 rg_p(name) 129 if pass == 1 { rg_p(" PASS\n" as *u8) } else { rg_p(" FAIL\n" as *u8) } 130 return pass 131} 132 133func rg_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 { 134 let a: *i64 = sys_mmap(64) as *i64 135 a[0] = a1 as i64 136 a[1] = a2 as i64 137 a[2] = a3 as i64 138 a[3] = a4 as i64 139 a[4] = 0 140 return a 141} 142 143func main(argc: i64, argv: *i64) -> i64 { 144 rg_p("=== DOCX RICH GATE: formatting runs (bold/italic/heading) + TABLES emit + round-trip ===\n" as *u8) 145 let docx: *u8 = "buildroot/_build/nx_docx.sov.elf" as *u8 146 let pr: i64 = sys_openat_rd(docx) 147 if pr >= 0 { sys_close(pr) } else { 148 rg_p(" instrument missing -> rebuilding via durable runner\n" as *u8) 149 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) 150 } 151 152 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) 153 154 var pass: i64 = 0 155 var r: i64 = 0 156 157 // row 1: write rich 158 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) 159 r = 0 160 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 } } 161 pass = pass + rg_row("rich-write" as *u8, r) 162 163 // row 2: text round-trips (heading + plain + a table cell + trailing) through the REAL docx reader 164 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) 165 r = 0 166 if rc2 == 0 { 167 if rg_has("/tmp/dxr_r2.txt" as *u8, "Meeting Agenda" as *u8) == 1 { 168 if rg_has("/tmp/dxr_r2.txt" as *u8, "Alice, Bob, and Carol" as *u8) == 1 { 169 if rg_has("/tmp/dxr_r2.txt" as *u8, "Budget" as *u8) == 1 { 170 if rg_has("/tmp/dxr_r2.txt" as *u8, "End of agenda" as *u8) == 1 { r = 1 } 171 } 172 } 173 } 174 } 175 pass = pass + rg_row("text-roundtrip" as *u8, r) 176 177 // row 3: formatting really EMITTED (raw STORED .docx bytes carry the run-properties) 178 r = 0 179 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:rPr><w:b/>" as *u8) == 1 { 180 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:sz w:val=\"32\"/>" as *u8) == 1 { r = 1 } 181 } 182 pass = pass + rg_row("formatting-emitted" as *u8, r) 183 184 // row 4: table really EMITTED (tbl + tblPr + tr + tc) 185 r = 0 186 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tbl>" as *u8) == 1 { 187 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tblPr>" as *u8) == 1 { 188 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tr>" as *u8) == 1 { 189 if rg_has("/tmp/dxr_rich.docx" as *u8, "<w:tc>" as *u8) == 1 { r = 1 } 190 } 191 } 192 } 193 pass = pass + rg_row("table-emitted" as *u8, r) 194 195 // row 5: italic run 196 r = rg_has("/tmp/dxr_rich.docx" as *u8, "<w:i/>" as *u8) 197 pass = pass + rg_row("italic-emitted" as *u8, r) 198 199 // row 6: determinism 200 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) 201 r = rg_fileeq("/tmp/dxr_rich.docx" as *u8, "/tmp/dxr_rich2.docx" as *u8) 202 pass = pass + rg_row("determinism" as *u8, r) 203 204 // row 7: missing spec (negative control) 205 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) 206 r = 0 207 if rc7 == 1 { r = 1 } 208 pass = pass + rg_row("loud-fail-missing-spec" as *u8, r) 209 210 let permil: i64 = (pass * 1000) / 7 211 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 212 var fdi: i64 = 0 213 while fdi < 2 { 214 var fd: i64 = 1 215 if fdi == 1 { fd = logfd } 216 if fd > 0 { 217 let line: *u8 = sys_mmap(256) 218 var o: i64 = 0 219 o = rg_cat(line, o, "DOCX-RICH-GATE epoch=" as *u8) 220 o = rg_catn(line, o, sys_now_realtime_sec()) 221 o = rg_cat(line, o, " rows=7 pass=" as *u8) 222 o = rg_catn(line, o, pass) 223 o = rg_cat(line, o, " permil=" as *u8) 224 o = rg_catn(line, o, permil) 225 if pass == 7 { 226 o = rg_cat(line, o, " verdict=GREEN\n" as *u8) 227 } else { 228 o = rg_cat(line, o, " verdict=RED\n" as *u8) 229 } 230 sys_write(fd, line, o) 231 } 232 fdi = fdi + 1 233 } 234 if logfd > 0 { sys_close(logfd) } 235 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 236 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 237 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 238 let ctr__dry: *i64 = gv_ctr() 239 ctr__dry[0] = pass 240 ctr__dry[1] = 7 241 let rc__dry: i64 = gv_verdict("DOCX-RICH-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 242 sys_exit(rc__dry) 243 return rc__dry 244}