code wiki / _hdl_build / nx_pdf_gate.nx
nx_pdf_gate.nx source
↩ module page · 145 lines · 8050 B
1// nx_pdf_gate.nx -- gate for the PDF viewer (nx_pdf: nx_pdf_text extract -> language-filter -> 0-JS preview).
2// Evidence source = a REAL 634KB PubMed/Cochrane PDF fixture (knowledge/fetched/pmc_cd004504.pdf), not a toy.
3// Rows: 1 instruments 2 render-ok 3 real-content(Cochrane Library) 4 readable-head (filter kills the binary
4// prefix: "Cochrane" within the first 3600 bytes) 5 valid-0js-html 6 registry-dispatch(.pdf via nx_doc_view)
5// 7 loud-fail-missing[neg]. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_gate_verdict.nx"
8
9func pg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n }
10func pg_p(s: *u8) -> i64 { sys_write(1, s, pg_slen(s)); return 0 }
11func pg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off+i]=s[i]; i=i+1 } return off+i }
12func pg_catn(dst: *u8, off: i64, v: i64) -> i64 {
13 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m }
14 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 }
15 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
16 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k
17}
18func pg_exists(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } sys_close(fd); return 1 }
19func pg_readn(path: *u8, buf: *u8, cap: i64) -> i64 {
20 let fd: i64=sys_openat_rd(path); if fd<0 { return 0-1 }
21 var got: i64=0; var n: i64=1
22 while n>0 { if got>=cap { n=0 } else { n=sys_read(fd,(buf as i64+got) as *u8,cap-got); if n>0 { got=got+n } } }
23 sys_close(fd); return got
24}
25func pg_scan(buf: *u8, sz: i64, needle: *u8) -> i64 {
26 let n: i64=pg_slen(needle); if sz<n { return 0 }
27 var i: i64=0
28 while i+n<=sz { var ok: i64=1; var j: i64=0; while j<n { if buf[i+j]!=needle[j] { ok=0; j=n } else { j=j+1 } } if ok==1 { return 1 } i=i+1 }
29 return 0
30}
31func pg_has(path: *u8, needle: *u8) -> i64 {
32 let buf: *u8=sys_mmap(33554432)
33 let sz: i64=pg_readn(path, buf, 33554400)
34 if sz<0 { return 0 }
35 return pg_scan(buf, sz, needle)
36}
37func pg_has_pre(path: *u8, needle: *u8, cap: i64) -> i64 {
38 let buf: *u8=sys_mmap(65536)
39 let sz: i64=pg_readn(path, buf, cap)
40 if sz<0 { return 0 }
41 return pg_scan(buf, sz, needle)
42}
43func pg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
44 let pid: i64=sys_fork()
45 if pid==0 {
46 if (outpath as i64)!=0 { let ofd: i64=sys_openat_wr(outpath,0x1a4); if ofd>=0 { sys_dup3(ofd,1,0); sys_dup3(ofd,2,0) } }
47 let argv: *i64=sys_mmap(128) as *i64
48 argv[0]=elf as i64; var i: i64=0; var go: i64=1
49 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } }
50 argv[i+1]=0
51 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0
52 sys_execve(elf, argv, envp); sys_exit(127)
53 }
54 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
55 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig }
56 return (st[0]>>8)&0xff
57}
58func pg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 {
59 let a: *i64=sys_mmap(64) as *i64; a[0]=a1 as i64; a[1]=a2 as i64; a[2]=a3 as i64; a[3]=a4 as i64; a[4]=0; return a
60}
61func pg_row(name: *u8, pass: i64) -> i64 {
62 pg_p("ROW " as *u8); pg_p(name); if pass==1 { pg_p(" PASS\n" as *u8) } else { pg_p(" FAIL\n" as *u8) } return pass
63}
64
65func main(argc: i64, argv: *i64) -> i64 {
66 pg_p("=== PDF VIEWER GATE: real 634KB Cochrane PDF -> filtered text-layer preview + registry dispatch ===\n" as *u8)
67 // The fixture is the PUBLISHED copy. It was knowledge/fetched/pmc_cd004504.pdf, which does not
68 // exist anywhere in the estate (nx_shelltool find: matches=0 corpus_complete=1 over 348,831 files);
69 // the same real 634KB Cochrane PDF is served at sites/nishifamily/sample.pdf, which is exactly the
70 // download URL this gate already passes ("/sample.pdf"). It moved, it was not lost -- so the gate
71 // keeps a REAL PDF rather than being weakened to a synthetic toy.
72 let fixture: *u8="sites/nishifamily/sample.pdf" as *u8
73
74 var pass: i64=0; var r: i64=0
75 // The four instrument preconditions are asserted SEPARATELY. They used to be one AND-folded
76 // row named "instruments", which reported a single FAIL without naming the failing conjunct --
77 // the reader then guesses, and the alarming guess ("the PDF renderer is broken") is wrong when
78 // the truth is a missing fixture. A compound assertion that will not name its failing conjunct
79 // is a false-alarm generator.
80 pass=pass+pg_row("instrument-nx_pdf_text" as *u8, pg_exists("_offc/nx_pdf_text.elf" as *u8))
81 pass=pass+pg_row("instrument-nx_pdf" as *u8, pg_exists("_offc/nx_pdf.elf" as *u8))
82 pass=pass+pg_row("instrument-nx_doc_view" as *u8, pg_exists("_offc/nx_doc_view.elf" as *u8))
83 pass=pass+pg_row("fixture-cochrane-pdf" as *u8, pg_exists(fixture))
84
85 // row 2: render
86 let rc2: i64=pg_runv("_offc/nx_pdf.elf" as *u8, pg_args4("html" as *u8, fixture, "/tmp/pg_pdf.html" as *u8, "/sample.pdf" as *u8), "/tmp/pg_r2.txt" as *u8)
87 r=0; if rc2==0 { if pg_has("/tmp/pg_r2.txt" as *u8, "PDF-HTML-OK path=/tmp/pg_pdf.html bytes=" as *u8)==1 { r=1 } }
88 pass=pass+pg_row("render-ok" as *u8, r)
89
90 // row 3: real content extracted
91 r=0
92 if pg_has("/tmp/pg_pdf.html" as *u8, "Cochrane Library" as *u8)==1 {
93 if pg_has("/tmp/pg_pdf.html" as *u8, "Herbal medicine for low" as *u8)==1 { r=1 }
94 }
95 pass=pass+pg_row("real-content" as *u8, r)
96
97 // row 4: the language filter killed the binary prefix -- readable content appears EARLY
98 r=pg_has_pre("/tmp/pg_pdf.html" as *u8, "Cochrane" as *u8, 3600)
99 pass=pass+pg_row("readable-head-filtered" as *u8, r)
100
101 // row 5: valid + sovereign
102 r=0
103 if pg_has_pre("/tmp/pg_pdf.html" as *u8, "<!DOCTYPE html>" as *u8, 64)==1 {
104 if pg_has("/tmp/pg_pdf.html" as *u8, "<script" as *u8)==0 {
105 if pg_has("/tmp/pg_pdf.html" as *u8, "Download .pdf" as *u8)==1 { r=1 }
106 }
107 }
108 pass=pass+pg_row("valid-0js-download" as *u8, r)
109
110 // row 6: registry dispatch routes .pdf
111 let rc6: i64=pg_runv("_offc/nx_doc_view.elf" as *u8, pg_args4(fixture, "/tmp/pg_dv.html" as *u8, "/sample.pdf" as *u8, 0 as *u8), "/tmp/pg_r6.txt" as *u8)
112 r=0; if rc6==0 { if pg_has("/tmp/pg_r6.txt" as *u8, "DOC-VIEW-OK ext=pdf" as *u8)==1 { if pg_has("/tmp/pg_dv.html" as *u8, "Cochrane Library" as *u8)==1 { r=1 } } }
113 pass=pass+pg_row("registry-dispatch-pdf" as *u8, r)
114
115 // row 7 (neg): missing pdf loud-fails
116 let rc7: i64=pg_runv("_offc/nx_pdf.elf" as *u8, pg_args4("html" as *u8, "/tmp/pg_nope.pdf" as *u8, "/tmp/pg_x.html" as *u8, 0 as *u8), "/tmp/pg_r7.txt" as *u8)
117 r=0; if rc7==1 { if pg_has("/tmp/pg_r7.txt" as *u8, "PDF-HTML-FAIL" as *u8)==1 { r=1 } }
118 pass=pass+pg_row("loud-fail-missing" as *u8, r)
119
120 let permil: i64=(pass*1000)/10
121 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
122 var fdi: i64=0
123 while fdi<2 {
124 var fd: i64=1; if fdi==1 { fd=logfd }
125 if fd>0 {
126 let line: *u8=sys_mmap(256); var o: i64=0
127 o=pg_cat(line,o,"PDF-VIEW-GATE epoch=" as *u8); o=pg_catn(line,o,sys_now_realtime_sec())
128 o=pg_cat(line,o," rows=10 pass=" as *u8); o=pg_catn(line,o,pass)
129 o=pg_cat(line,o," permil=" as *u8); o=pg_catn(line,o,permil)
130 if pass==10 { o=pg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=pg_cat(line,o," verdict=RED\n" as *u8) }
131 sys_write(fd, line, o)
132 }
133 fdi=fdi+1
134 }
135 if logfd>0 { sys_close(logfd) }
136 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
137 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
138 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
139 let ctr__dry: *i64 = gv_ctr()
140 ctr__dry[0] = pass
141 ctr__dry[1] = 10
142 let rc__dry: i64 = gv_verdict("PDF-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
143 sys_exit(rc__dry)
144 return rc__dry
145}