nx_search_neutrality_gate.nx source
↩ module page · 193 lines · 8359 B
1// nx_search_neutrality_gate.nx -- the CONTENT-NEUTRALITY CHARTER as a gate.
2//
3// module: nishi-core.search.neutrality_gate
4// depends: nx_bm25.nx (the real inverted-index/tf kernels on the live path)
5// capability: GATE
6//
7// OPERATOR CHARTER (2026-06-10): "we want to be agnostic and not filter out
8// anything that our search returns -- the user may find value; no dead-internet
9// walls like current providers; we have freedom of speech and respect it."
10//
11// Mechanical meaning, proven here against the SAME index kernels the live SERP
12// uses (never a parallel mock):
13// N1 COMPLETENESS -- every indexed doc that matches the query IS in the
14// result set. Retrieval drops nothing.
15// N2 ORDER-NOT-EXCLUDE -- the ranked output contains EVERY doc (zero-match
16// docs rank below all matches -- the live SERP's
17// relevance hard-gate ORDERS, it never REMOVES).
18// N3 SUPPRESSION-TAMPER RED -- a simulated host-blocklist lane (what
19// incumbents do to vk.com/bunkr-class hosts) MUST be
20// caught: the checker sees the missing doc and flags
21// RED. A filter cannot creep in silently.
22//
23// Gate = N1 PASS && N2 PASS && N3 tamper DETECTED. One durable machine row to
24// knowledge/status/search_neutrality.log for the Examiner (arc NEUTRALITY).
25
26import "nx_str.nx"
27import "nx_syscalls.nx"
28import "nx_bm25.nx"
29import "nx_search_inverted.nx" // NxInvIndex + nx_inv_* + nx_bm25_tf. The header above claims nx_bm25.nx
30 // carries "the real inverted-index/tf kernels" -- it does not, and never
31 // did, so this gate could not compile. 6 of its 7 undefined names were
32 // always here; only nx_bm25_tf had to be written.
33
34func np(s: *u8) -> i64 { sys_write(1, s, nx_str_len(s)); return 0 }
35func nn(v: i64) -> i64 {
36 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
37 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
38 let t: *u8 = sys_mmap(28); var k: i64 = 0
39 while m > 0 { t[k] = 0x30 + (m - (m/10)*10); m = m/10; k = k + 1 }
40 while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) }
41 return 0
42}
43func nfp(fd: i64, s: *u8) -> i64 { sys_write(fd, s, nx_str_len(s)); return 0 }
44func nfn(fd: i64, v: i64) -> i64 {
45 let b: *u8 = sys_mmap(28); let t: *u8 = sys_mmap(28)
46 var m: i64 = v; if m < 0 { m = 0 - m }
47 var k: i64 = 0
48 if m == 0 { t[0] = 48; k = 1 }
49 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }
50 var i: i64 = 0
51 while i < k { b[i] = t[k-1-i]; i = i + 1 }
52 sys_write(fd, b, k)
53 return 0
54}
55
56const NG_NDOC: i64 = 6
57
58// synthetic corpus: docs 0,2,4,5 match "falcon"; 1,3 do not. Doc 5 stands in
59// for a doc on an incumbent-deindexed host (vk/bunkr class) -- content-wise
60// identical citizenship to every other doc; that IS the charter.
61func ng_doc(d: i64) -> *u8 {
62 if d == 0 { return "the falcon flies over the canyon at dawn" }
63 if d == 1 { return "a quiet page about gardening and soil ph" }
64 if d == 2 { return "falcon falcon migration routes mapped by volunteers" }
65 if d == 3 { return "notes on bread baking and hydration ratios" }
66 if d == 4 { return "rescue center releases rehabilitated falcon back to the wild" }
67 if d == 5 { return "deindexed-host mirror discussing the falcon rescue with photos" }
68 return ""
69}
70// 1 iff doc d lives on the simulated "controversial" host the tamper lane blocks
71func ng_on_blocked_host(d: i64) -> i64 { if d == 5 { return 1 } return 0 }
72
73// retrieve into hits[]: docids with tf>0 for the query term, through the REAL
74// tf kernel. blocklist=1 simulates the incumbent host-filter (tamper lane).
75func ng_retrieve(texts: **u8, lens: *i64, h: i64, blocklist: i64, hits: *i64) -> i64 {
76 var nh: i64 = 0
77 var d: i64 = 0
78 while d < NG_NDOC {
79 var admit: i64 = 1
80 if blocklist == 1 { if ng_on_blocked_host(d) == 1 { admit = 0 } }
81 if admit == 1 {
82 if nx_bm25_tf(texts[d], lens[d], h) > 0 { hits[nh] = d; nh = nh + 1 }
83 }
84 d = d + 1
85 }
86 return nh
87}
88
89func main() -> i64 {
90 np("=== SEARCH NEUTRALITY GATE: agnostic retrieval -- order, never exclude ===\n")
91 let texts: **u8 = sys_mmap(NG_NDOC * 8) as **u8
92 let lens: *i64 = sys_mmap(NG_NDOC * 8) as *i64
93 let idx: *NxInvIndex = nx_inv_new(NG_NDOC + 1)
94 var d: i64 = 0
95 while d < NG_NDOC {
96 texts[d] = ng_doc(d)
97 lens[d] = nx_str_len(texts[d])
98 nx_inv_index_row(idx, texts[d], lens[d], d)
99 d = d + 1
100 }
101 nx_inv_finalize_offsets(idx)
102 d = 0
103 while d < NG_NDOC { nx_inv_emit_row(idx, texts[d], lens[d], d); d = d + 1 }
104
105 let h: i64 = nx_inv_hash_bytes_lower("falcon", 6)
106 let expect: i64 = 4 // docs 0,2,4,5 contain the term
107
108 // N1 COMPLETENESS: clean retrieval returns every matching doc
109 let hits: *i64 = sys_mmap(NG_NDOC * 8) as *i64
110 let nh: i64 = ng_retrieve(texts, lens, h, 0, hits)
111 var n1: i64 = 0
112 if nh == expect {
113 // every expected docid present (0,2,4,5)
114 var seen0: i64 = 0
115 var seen2: i64 = 0
116 var seen4: i64 = 0
117 var seen5: i64 = 0
118 var i: i64 = 0
119 while i < nh {
120 if hits[i] == 0 { seen0 = 1 }
121 if hits[i] == 2 { seen2 = 1 }
122 if hits[i] == 4 { seen4 = 1 }
123 if hits[i] == 5 { seen5 = 1 }
124 i = i + 1
125 }
126 if seen0 + seen2 + seen4 + seen5 == 4 { n1 = 1 }
127 }
128 np(" N1 completeness: matches="); nn(nh); np("/"); nn(expect)
129 if n1 == 1 { np(" PASS\n") } else { np(" FAIL\n") }
130
131 // N2 ORDER-NOT-EXCLUDE: rank ALL docs by tf desc (zero-match below) -- the
132 // ranked list must contain every doc exactly once. Filtering would shrink it.
133 let scores: *i64 = sys_mmap(NG_NDOC * 8) as *i64
134 d = 0
135 while d < NG_NDOC { scores[d] = nx_bm25_tf(texts[d], lens[d], h); d = d + 1 }
136 let present: *i64 = sys_mmap(NG_NDOC * 8) as *i64
137 d = 0
138 while d < NG_NDOC { present[d] = 0; d = d + 1 }
139 // simple selection order (the live SERP's order kernel semantics): every
140 // doc placed once, highest score first -- zero-match docs at the tail
141 var placedn: i64 = 0
142 var last_score: i64 = 0
143 var ord_ok: i64 = 1
144 while placedn < NG_NDOC {
145 var best: i64 = 0 - 1
146 var bs: i64 = 0 - 1
147 d = 0
148 while d < NG_NDOC {
149 if present[d] == 0 { if scores[d] > bs { bs = scores[d]; best = d } }
150 d = d + 1
151 }
152 if placedn > 0 { if bs > last_score { ord_ok = 0 } }
153 last_score = bs
154 present[best] = 1
155 placedn = placedn + 1
156 }
157 var n2: i64 = 0
158 var allp: i64 = 0
159 d = 0
160 while d < NG_NDOC { allp = allp + present[d]; d = d + 1 }
161 if allp == NG_NDOC { if ord_ok == 1 { n2 = 1 } }
162 np(" N2 order-not-exclude: ranked="); nn(allp); np("/"); nn(NG_NDOC)
163 if n2 == 1 { np(" PASS (zero-match docs ranked, never removed)\n") } else { np(" FAIL\n") }
164
165 // N3 SUPPRESSION-TAMPER: the host-blocklist lane MUST be caught
166 let thits: *i64 = sys_mmap(NG_NDOC * 8) as *i64
167 let tnh: i64 = ng_retrieve(texts, lens, h, 1, thits)
168 var n3: i64 = 0
169 if tnh != expect { n3 = 1 } // checker sees the missing doc -> tamper DETECTED
170 np(" N3 suppression-tamper: blocklist lane returned "); nn(tnh); np("/"); nn(expect)
171 if n3 == 1 { np(" -> DETECTED (filter cannot creep in silently)\n") } else { np(" -> NOT DETECTED: FAIL\n") }
172
173 var green: i64 = 0
174 if n1 == 1 { if n2 == 1 { if n3 == 1 { green = 1 } } }
175
176 let lfd: i64 = sys_openat_append("knowledge/status/search_neutrality.log" as *u8, 0x1a4)
177 if lfd >= 0 {
178 nfp(lfd, "NEUTRALITY-GATE epoch=" as *u8); nfn(lfd, sys_now_realtime_sec())
179 nfp(lfd, " n1_completeness=" as *u8); nfn(lfd, n1)
180 nfp(lfd, " n2_order_not_exclude=" as *u8); nfn(lfd, n2)
181 nfp(lfd, " n3_tamper_detected=" as *u8); nfn(lfd, n3)
182 if green == 1 { nfp(lfd, " verdict=GREEN\n" as *u8) } else { nfp(lfd, " verdict=RED\n" as *u8) }
183 sys_close(lfd)
184 }
185 if green == 1 {
186 np(" NEUTRALITY-GATE: GREEN (charter holds mechanically)\n")
187 sys_exit(0)
188 return 0
189 }
190 np(" NEUTRALITY-GATE: RED\n")
191 sys_exit(1)
192 return 1
193}