nx_docportal_search_gate.nx source
↩ module page · 120 lines · 7398 B
1// nx_docportal_search_gate.nx -- END-TO-END measured gate: a document UPLOADED through the portal is findable on
2// the site's onsite search, and a PRIVATE upload is NOT -- proven through the REAL production search engine
3// (nx_search_inverted_persist inverted index, the same one nx_onsite_index/nx_onsite_search use), not a stand-in.
4// Self-contained per the sovereign-exec doctrine (in-process; no forked CLI, no /tmp staging):
5// 1. dp_ingest 2 PUBLIC docs + 1 PRIVATE doc into a throwaway domain (dpsearch).
6// 2. build the inverted index from the portal-generated search source (knowledge/index/dpsearch_src.tsv) and
7// round-trip it through save+load (the exact proven onsite_index/onsite_search path).
8// 3. query the live index:
9// T1 the search source has exactly 2 docs -> the private upload never entered it
10// T2 "probate" is found -> public upload #0 is searchable
11// T3 "divorce" is found -> public upload #1 is searchable
12// T4 "retainer" is NOT found -> private upload is invisible to the public search BY CONSTRUCTION
13// Verdict MEASURED from real query results. GREEN iff 4/4. Appends knowledge/status/docportal_search_gate.log.
14// license_tier: ORIGINAL
15import "nx_docportal_lib.nx"
16import "nx_search_inverted_persist.nx"
17
18func s_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
19func s_num(v: i64) -> i64 {
20 let bb: *u8 = sys_mmap(28); var m: i64 = v
21 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
22 let t: *u8 = sys_mmap(28); var k: i64 = 0
23 if m == 0 { t[0] = 48 as u8; k = 1 }
24 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
25 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
26}
27func s_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
28func s_wn(fd: i64, v: i64) -> i64 {
29 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
30 let t: *u8 = sys_mmap(28); var k: i64 = 0; 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; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(fd, bb, k); return 0
33}
34
35// how many docs match a term in the live index (presence is enough; ranking is proven elsewhere)
36func s_qcount(idx: *NxInvIndex, term: *u8, tl: i64) -> i64 {
37 let rowids: *i64 = sys_mmap(8 * 4096) as *i64
38 let res: *NxInvQueryResult = sys_mmap(64) as *NxInvQueryResult
39 nx_inv_query_term(idx, term, tl, rowids, 4096, res)
40 return res.n_rowids_filled
41}
42
43func main() -> i64 {
44 s_puts("=== DOCPORTAL SEARCH GATE (upload -> onsite search; public found, private hidden; REAL engine) ===\n" as *u8)
45
46 // clean the throwaway domain's derived search source so the doc count is deterministic across re-runs
47 let srcp: *u8 = sys_mmap(512); dp_pubsrc("dpsearch" as *u8, srcp)
48 let tfd: i64 = sys_openat_wr(srcp, 0x1a4); if tfd >= 0 { sys_close(tfd) }
49
50 let pub0: *u8 = "Estate planning and probate. We prepare a last will and testament, living trusts, and powers of attorney." as *u8
51 let pub1: *u8 = "Family law representation. Divorce, child custody, parent-time, and child support under Utah law." as *u8
52 let prv0: *u8 = "CONFIDENTIAL retainer agreement. Attorney-client privileged. Client SSN and financial disclosures." as *u8
53 let cidp: *i64 = sys_mmap(16) as *i64
54 dp_ingest("dpsearch" as *u8, DP_VIS_PUBLIC, pub0, dp_len(pub0), cidp)
55 dp_ingest("dpsearch" as *u8, DP_VIS_PUBLIC, pub1, dp_len(pub1), cidp)
56 dp_ingest("dpsearch" as *u8, DP_VIS_PRIVATE, prv0, dp_len(prv0), cidp)
57
58 // read the portal-generated search source + parse id<TAB>text lines (docid = line index)
59 let szp: *i64 = sys_mmap(16) as *i64
60 let buf: *u8 = sys_read_file(srcp, szp)
61 if (buf as i64) == 0 { s_puts("GATE-FAIL: no search source produced\n" as *u8); sys_exit(1); return 1 }
62 let sz: i64 = szp[0]
63 let tptr: *i64 = sys_mmap(8 * 64) as *i64
64 let tlen: *i64 = sys_mmap(8 * 64) as *i64
65 var ndocs: i64 = 0
66 var ls: i64 = 0; var i: i64 = 0
67 while i <= sz {
68 var eol: i64 = 0
69 if i == sz { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } }
70 if eol == 1 {
71 if i > ls {
72 var tab: i64 = 0 - 1; var k: i64 = ls
73 while k < i { if buf[k] == (9 as u8) { if tab < 0 { tab = k } } k = k + 1 }
74 if tab > ls {
75 tptr[ndocs] = (buf as i64) + tab + 1
76 tlen[ndocs] = i - tab - 1
77 ndocs = ndocs + 1
78 }
79 }
80 ls = i + 1
81 }
82 i = i + 1
83 }
84 s_puts(" search-source docs="); s_num(ndocs); s_puts("\n" as *u8)
85
86 // build the inverted index over the public docs, then save+load (the exact proven onsite path)
87 let idx: *NxInvIndex = nx_inv_new_sized(2048)
88 var d: i64 = 0
89 while d < ndocs { nx_inv_index_row(idx, tptr[d] as *u8, tlen[d], d); d = d + 1 }
90 idx.n_rows = ndocs
91 nx_inv_finalize_offsets(idx)
92 d = 0
93 while d < ndocs { nx_inv_emit_row(idx, tptr[d] as *u8, tlen[d], d); d = d + 1 }
94 if nx_inv_save(idx, "knowledge/index/dpsearch.idx" as *u8) != NX_INV_OK { s_puts("GATE-FAIL: index save\n" as *u8); sys_exit(1); return 1 }
95 let lidx: *NxInvIndex = nx_inv_load("knowledge/index/dpsearch.idx" as *u8)
96 if lidx == 0 as *NxInvIndex { s_puts("GATE-FAIL: index load\n" as *u8); sys_exit(1); return 1 }
97
98 var pass: i64 = 0
99 let t1: i64 = ndocs // expect exactly the 2 public uploads
100 let qpro: i64 = s_qcount(lidx, "probate" as *u8, 7)
101 let qdiv: i64 = s_qcount(lidx, "divorce" as *u8, 7)
102 let qret: i64 = s_qcount(lidx, "retainer" as *u8, 8)
103
104 if t1 == 2 { pass = pass + 1; s_puts(" T1 only public in search source (docs=2): PASS\n" as *u8) } else { s_puts(" T1 only public in search source: FAIL docs="); s_num(t1); s_puts("\n" as *u8) }
105 if qpro >= 1 { pass = pass + 1; s_puts(" T2 public 'probate' found: PASS\n" as *u8) } else { s_puts(" T2 public 'probate' found: FAIL\n" as *u8) }
106 if qdiv >= 1 { pass = pass + 1; s_puts(" T3 public 'divorce' found: PASS\n" as *u8) } else { s_puts(" T3 public 'divorce' found: FAIL\n" as *u8) }
107 if qret == 0 { pass = pass + 1; s_puts(" T4 private 'retainer' NOT found: PASS\n" as *u8) } else { s_puts(" T4 private 'retainer' NOT found: FAIL count="); s_num(qret); s_puts("\n" as *u8) }
108
109 s_puts("----\nDOCPORTAL-SEARCH rows=4 pass="); s_num(pass); s_puts("\n" as *u8)
110 let lg: i64 = sys_openat_append("knowledge/status/docportal_search_gate.log" as *u8, 0x1a4)
111 if lg >= 0 {
112 s_w(lg, "DOCPORTAL-SEARCH only_public_indexed="); s_wn(lg, t1); s_w(lg, " probate_found="); s_wn(lg, qpro)
113 s_w(lg, " divorce_found="); s_wn(lg, qdiv); s_w(lg, " retainer_found="); s_wn(lg, qret)
114 s_w(lg, " rows=4 pass="); s_wn(lg, pass)
115 if pass == 4 { s_w(lg, " verdict=GREEN\n" as *u8) } else { s_w(lg, " verdict=RED\n" as *u8) }
116 sys_close(lg)
117 }
118 if pass == 4 { s_puts("DOCPORTAL-SEARCH GREEN (upload->searchable, public found, private hidden -- real engine, measured)\n" as *u8); sys_exit(0); return 0 }
119 s_puts("DOCPORTAL-SEARCH RED\n" as *u8); sys_exit(1); return 1
120}