code wiki / _hdl_build / nx_docportal_search_seg_gate.nx

nx_docportal_search_seg_gate.nx source

↩ module page · 937 lines · 64432 B

1// nx_docportal_search_seg_gate.nx -- GATE for the SOVEREIGN seg_store-native onsite search (nx_docportal_search_seg). 2// Writes 3 PUBLIC docs into a test shard through the RAW sovereign store (doc:<cid> keys, text values so 3// ss_write_seg builds .terms), then drives dss_search end-to-end over the REAL seg_store -- proving the sovereign 4// path (ss_open -> ss_term postings -> ss_hget -> rank) with NO tsv anywhere. Negative controls: absent term -> 0, 5// absent shard -> 0 (isolation, not a crash). license_tier: ORIGINAL 6import "nx_docportal_search_seg.nx" 7import "nx_gate_verdict.nx" // D001 migration 2026-09-16: every tooth is a gv_check, the exit code carries the verdict 8 9// evidence-line builders (local, so this gate stays self-contained) 10func dsg_cat(b: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { b[o] = s[i]; o = o + 1; i = i + 1 } return o } 11func dsg_catn(b: *u8, off: i64, v: i64) -> i64 { 12 var m: i64 = v 13 if m < 0 { m = 0 } 14 let t: *u8 = sys_mmap(28) 15 var k: i64 = 0 16 if m == 0 { t[0] = 48 as u8; k = 1 } 17 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var o: i64 = off 19 var i: i64 = 0 20 while i < k { b[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } 21 return o 22} 23 24func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 25func g_num(v: i64) -> i64 { 26 let bb: *u8 = sys_mmap(28); var m: i64 = v 27 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 28 let t: *u8 = sys_mmap(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; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 32 sys_write(1, bb, k); return 0 33} 34func g_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 35 36// S7 fixture builder: head, then npre unique pad tokens (" zq<tag>p<k>"), then mid (if any), then npost more pads. 37// Pads are unique per document so they match no query term and raise no term's document frequency; the byte 38// length is what the fixture tunes (BM25 length normalisation reads |d| in BYTES). Returns the byte length. 39func s7_build(buf: *u8, head: *u8, tag: *u8, npre: i64, mid: *u8, npost: i64) -> i64 { 40 var o: i64 = dsg_cat(buf, 0, head) 41 var k: i64 = 0 42 while k < npre { o = dsg_cat(buf, o, " zq" as *u8); o = dsg_cat(buf, o, tag); o = dsg_cat(buf, o, "p" as *u8); o = dsg_catn(buf, o, k); k = k + 1 } 43 if mid[0] != (0 as u8) { o = dsg_cat(buf, o, " " as *u8); o = dsg_cat(buf, o, mid) } 44 while k < npre + npost { o = dsg_cat(buf, o, " zq" as *u8); o = dsg_cat(buf, o, tag); o = dsg_cat(buf, o, "p" as *u8); o = dsg_catn(buf, o, k); k = k + 1 } 45 buf[o] = 0 as u8 46 return o 47} 48// one check: got n results with cids_out[0]==want_top (want_top<0 = don't care about top); expect_n<0 = don't care 49func g_check(name: *u8, got_n: i64, cids: *i64, expect_n: i64, want_top: i64, ctr: *i64) -> i64 { 50 var ok: i64 = 1 51 if expect_n >= 0 { if got_n != expect_n { ok = 0 } } 52 if want_top >= 0 { if got_n < 1 { ok = 0 } else { if cids[0] != want_top { ok = 0 } } } 53 g_puts(name); g_puts(" n="); g_num(got_n) 54 g_puts("\n") 55 gv_check(name, ok, ctr) 56 return 0 57} 58 59// S9 fixture and tooth constants (2026-09-17): the three planted pages, the page the teeth read, the matching total 60const S9G_DOC_CONTENT: i64 = 850 // the long biography carrying the whole name once 61const S9G_DOC_RESULTS: i64 = 851 // another engine's results page repeating the name 62const S9G_DOC_RARE: i64 = 852 // the page carrying only the rare word 63const S9G_DOC_SCATTER: i64 = 853 // both words present but far apart (co-occurrence is not a name) 64const S9G_RANK_FOURTH: i64 = 3 // zero-based slot of the fourth result 65const S9G_ENT_SLOTS: i64 = 4 // S11: entity announce box 66const S9G_DOC_ABSENT: i64 = 999 // S11: a pinned cid that is not in the shard 67const S9G_FILE_MODE: i64 = 420 // 0644 for the fixture table 68const S9G_FE_101: i64 = 1789600000 // S12: planted fetch epochs (doc 103 deliberately carries none) 69const S9G_FE_102: i64 = 1789650000 70const S9G_FE_NOW: i64 = 1789660000 // S12: the clock the arithmetic teeth read 71const S9G_FE_YOUNG: i64 = 5 // S12: a third observed age for the odd-count median 72const S9G_CH_Z: i64 = 122 // S11-b: the fixture jp name is zq 73const S9G_CH_Q: i64 = 113 74const S9G_PAGE: i64 = 10 // one result page 75const S9G_MATCHING: i64 = 14 // 850, 851, 852, 853 and the ten fillers all match the name query 76const S9G_TOP3: i64 = 3 // the three planted pages the order teeth read 77const S9G_TOTAL_SLOTS: i64 = 2 // dss_search_off_div's totalout pair 78const S9G_RANK_THIRD: i64 = 2 // zero-based slot of the third result 79const S9G_BUF_4K: i64 = 4096 // one page: the long-doc and S7 fixture builders 80// S11: a fixture table for the entity pin (truncate-written, never the production path) 81func s9g_write_file(path: *u8, s: *u8) -> i64 { 82 let fd: i64 = sys_openat_wr(path, S9G_FILE_MODE) 83 if fd < 0 { return 0 - 1 } 84 let n: i64 = g_strlen(s) 85 var w: i64 = 0 86 var bad: i64 = 0 87 while w < n { if bad == 1 { w = n } else { let k: i64 = sys_write(fd, ((s as i64) + w) as *u8, n - w); if k <= 0 { bad = 1 } else { w = w + k } } } 88 sys_close(fd) 89 return n 90} 91func main(argc: i64, argv: *i64) -> i64 { 92 g_puts("=== nx_docportal_search_seg gate (SOVEREIGN seg_store search, no tsv) ===\n") 93 // --- write a test PUBLIC shard through the raw sovereign store --- 94 let prefix: *u8 = "knowledge/store/dp-ssgatetest-pub-" as *u8 95 let d1: *u8 = "Estate Planning wills living trusts probate inheritance executor" as *u8 96 let d2: *u8 = "Family Law divorce child custody support alimony parenting plans" as *u8 97 let d3: *u8 = "Business Law llc corporation formation contracts operating agreement" as *u8 98 // consent rows (same segment, deterministic re-runs): 104 opted OUT of search (pol=2 = AI_BLOG only), 99 // 105 opted IN (pol=1). 101-103 stay pol-FREE = the absent-pol default-searchable path. 100 let d4: *u8 = "Adoption confidential retainer matters" as *u8 101 let d5: *u8 = "Municipal annexation ordinance guidance" as *u8 102 // IDEMPOTENT FIXTURE (2026-09-16): a seg_store commit APPENDS its segment to the shard's existing manifest, so a 103 // fixture shard that outlives the gate carries every generation of the fixture ever written -- on the NAS the 104 // exact-count teeth (T1 probate -> exactly one doc) read n=2 against docs an older generation of this file wrote, 105 // while the same binary read 51/51 on a laptop whose shard was born of the current generation. A gate that is not 106 // idempotent reports on its first run and lies about every run after. Readers see only the segments the manifest 107 // names, so unlinking the manifest before the commit gives every run a shard holding exactly this file's docs 108 // (the rewritten seg-1 replaces the old; unreferenced older segments are inert). Both fixture shards. 109 sys_unlinkat("knowledge/store/dp-ssgatetest-pub-manifest.txt" as *u8) 110 sys_unlinkat("knowledge/store/dp-ssgatefuse-pub-manifest.txt" as *u8) 111 let w: *i64 = ss_begin() 112 ss_add(w, 1, "doc:101" as *u8, d1, g_strlen(d1)) 113 ss_add(w, 1, "doc:102" as *u8, d2, g_strlen(d2)) 114 ss_add(w, 1, "doc:103" as *u8, d3, g_strlen(d3)) 115 // url rows for the site: field-filter rows (103 deliberately has NO url = not site-attributable) 116 ss_add(w, 1, "url:101" as *u8, "https://en.wikipedia.org/wiki/Trust_law" as *u8, 39) 117 ss_add(w, 1, "url:102" as *u8, "https://law.example.com/family" as *u8, 30) 118 ss_add(w, 1, "fe:101" as *u8, "1789600000" as *u8, 10) // S12: fetch epochs beside two docs; 103 has none on purpose 119 ss_add(w, 1, "fe:102" as *u8, "1789650000" as *u8, 10) 120 ss_add(w, 1, "doc:104" as *u8, d4, g_strlen(d4)) 121 ss_add(w, 1, "pol:104" as *u8, "2" as *u8, 1) 122 ss_add(w, 1, "doc:105" as *u8, d5, g_strlen(d5)) 123 ss_add(w, 1, "pol:105" as *u8, "1" as *u8, 1) 124 // IDF fixture (T9): idfrare lives in ONE doc (106); idfcommon in FOUR (107-110). Equal tf. The OLD 125 // distinct+tf scorer TIED 106 vs 107 (first candidate won = 107, postings order); only IDF ranking 126 // puts the rare-term doc on top -- this row fails on the old scorer BY CONSTRUCTION. 127 let d6: *u8 = "idfrare idfrare notice" as *u8 128 let d7: *u8 = "idfcommon idfcommon memo" as *u8 129 let d8: *u8 = "idfcommon lorem" as *u8 130 let d9: *u8 = "idfcommon ipsum" as *u8 131 let d10: *u8 = "idfcommon dolor" as *u8 132 ss_add(w, 1, "doc:106" as *u8, d6, g_strlen(d6)) 133 ss_add(w, 1, "doc:107" as *u8, d7, g_strlen(d7)) 134 ss_add(w, 1, "doc:108" as *u8, d8, g_strlen(d8)) 135 ss_add(w, 1, "doc:109" as *u8, d9, g_strlen(d9)) 136 ss_add(w, 1, "doc:110" as *u8, d10, g_strlen(d10)) 137 // length-norm fixture (T14): the LONG doc (126) is inserted FIRST -- under b=0 equal tf tied and 138 // candidate order won (126); full BM25's verbosity correction must put the SHORT doc (127) on top. 139 let dlong: *u8 = sys_mmap(S9G_BUF_4K) 140 var dlo: i64 = 0 141 var lr: i64 = 0 142 while lr < 80 { let wf: *u8 = "verbose filler prose words continue onward " as *u8; var wj: i64 = 0; while wf[wj] != (0 as u8) { dlong[dlo] = wf[wj]; dlo = dlo + 1; wj = wj + 1 } lr = lr + 1 } 143 let lm: *u8 = "lennorm appears once here" as *u8 144 var lj: i64 = 0 145 while lm[lj] != (0 as u8) { dlong[dlo] = lm[lj]; dlo = dlo + 1; lj = lj + 1 } 146 let dshort: *u8 = "lennorm short focused note" as *u8 147 ss_add(w, 1, "doc:126" as *u8, dlong, dlo) 148 ss_add(w, 1, "doc:127" as *u8, dshort, g_strlen(dshort)) 149 // stemming fixture (T18): a doc that only says the INFLECTED form -- query stem-expansion must recall it 150 let dstem: *u8 = "stemtoken indexing crawlers were ranking pages" as *u8 151 ss_add(w, 1, "doc:128" as *u8, dstem, g_strlen(dstem)) 152 // pagination fixture (T10): 15 pgseg docs -> offset/total behavior is provable (page sizes 10/5/0) 153 let dpg: *u8 = "pgseg pagination fixture entry" as *u8 154 let pgkey: *u8 = sys_mmap(24) 155 var pgi: i64 = 111 156 while pgi <= 125 { 157 var ko: i64 = 0 158 let kp: *u8 = "doc:" as *u8 159 while kp[ko] != (0 as u8) { pgkey[ko] = kp[ko]; ko = ko + 1 } 160 pgkey[ko] = (48 + (pgi / 100)) as u8 161 pgkey[ko + 1] = (48 + ((pgi / 10) % 10)) as u8 162 pgkey[ko + 2] = (48 + (pgi % 10)) as u8 163 pgkey[ko + 3] = 0 as u8 164 ss_add(w, 1, pgkey, dpg, g_strlen(dpg)) 165 pgi = pgi + 1 166 } 167 // host-diversity fixture (T22/T23): 5 docs on crowd.example.com (tf=2) + 2 on other.example.com (tf=1), 168 // all matching "diversitytest". WITHOUT the cap (webdiv=0) the 5 tf=2 crowd docs take the top 5 slots 169 // (cids[3]=304); WITH the cap (webdiv=1, DSS_HOSTCAP=3) an OTHER-host doc is promoted into slot 3 170 // (cids[3]=306) once crowd hits the cap -- distinguishing by construction (the old scorer had no cap). 171 let dcrowd: *u8 = "diversitytest diversitytest note" as *u8 172 let dother: *u8 = "diversitytest memo" as *u8 173 ss_add(w, 1, "doc:301" as *u8, dcrowd, g_strlen(dcrowd)) 174 ss_add(w, 1, "url:301" as *u8, "https://crowd.example.com/1" as *u8, 27) 175 ss_add(w, 1, "doc:302" as *u8, dcrowd, g_strlen(dcrowd)) 176 ss_add(w, 1, "url:302" as *u8, "https://crowd.example.com/2" as *u8, 27) 177 ss_add(w, 1, "doc:303" as *u8, dcrowd, g_strlen(dcrowd)) 178 ss_add(w, 1, "url:303" as *u8, "https://crowd.example.com/3" as *u8, 27) 179 ss_add(w, 1, "doc:304" as *u8, dcrowd, g_strlen(dcrowd)) 180 ss_add(w, 1, "url:304" as *u8, "https://crowd.example.com/4" as *u8, 27) 181 ss_add(w, 1, "doc:305" as *u8, dcrowd, g_strlen(dcrowd)) 182 ss_add(w, 1, "url:305" as *u8, "https://crowd.example.com/5" as *u8, 27) 183 ss_add(w, 1, "doc:306" as *u8, dother, g_strlen(dother)) 184 ss_add(w, 1, "url:306" as *u8, "https://other.example.com/6" as *u8, 27) 185 ss_add(w, 1, "doc:307" as *u8, dother, g_strlen(dother)) 186 ss_add(w, 1, "url:307" as *u8, "https://other.example.com/7" as *u8, 27) 187 // authority fusion fixture (T28/T29): two docs identical content (equal BM25), different urls; doc 601's url 188 // carries a high pr: prior, doc 600's has none. webdiv=0 -> tie broken by candidate order (600 first); 189 // webdiv=1 -> the PageRank prior lifts 601 over 600 (distinguishing: pre-fuse, webdiv=1 also gave 600). 190 let dae: *u8 = "authorityterm equalcontent alpha" as *u8 191 let u600: *u8 = "https://plain.example/b" as *u8 192 let u601: *u8 = "https://authority.example/a" as *u8 193 ss_add(w, 1, "doc:600" as *u8, dae, g_strlen(dae)) 194 ss_add(w, 1, "url:600" as *u8, u600, g_strlen(u600)) 195 ss_add(w, 1, "doc:601" as *u8, dae, g_strlen(dae)) 196 ss_add(w, 1, "url:601" as *u8, u601, g_strlen(u601)) 197 let prk: *u8 = sys_mmap(48) 198 dss_prkey(dss_urlcid(u601, g_strlen(u601)), prk) 199 let prv: *i64 = sys_mmap(16) as *i64 200 prv[0] = 500000000 201 ss_add(w, 1, prk, prv as *u8, 8) 202 // url-dedup fixture (T30/T31): docs 700+701 = two crawl SNAPSHOTS of the SAME url (identical content -> 203 // equal BM25; distinct content cids in prod, distinct doc keys here). webdiv=1 must serve ONE. 204 let ddup: *u8 = "zqduppage snapshot content for url dedup" as *u8 205 let udup: *u8 = "https://dup.example.com/page" as *u8 206 ss_add(w, 1, "doc:700" as *u8, ddup, g_strlen(ddup)) 207 ss_add(w, 1, "url:700" as *u8, udup, g_strlen(udup)) 208 ss_add(w, 1, "doc:701" as *u8, ddup, g_strlen(ddup)) 209 ss_add(w, 1, "url:701" as *u8, udup, g_strlen(udup)) 210 // entity-collapse fixture (T32/T33): 710/711/712 are the SAME article under three LANGUAGE 211 // subdomains -- identical PATH, hosts differing only in their leading label -- which is the real 212 // shape measured on 2026-08-17 (20 of 30 SERP slots were one Wikipedia article in ~20 editions). 213 // This is NOT the T30/T31 case: those are two snapshots of ONE url, these are FOUR DISTINCT urls. 214 // 713 IS THE NEGATIVE CONTROL and it is the whole reason this fixture can fail honestly: it sits on 215 // the SAME domain family but a DIFFERENT path, so a rule that collapsed by host-suffix alone would 216 // wrongly swallow it. Without 713 an entity key that merged everything on a domain would score 100%. 217 // All four carry IDENTICAL content so BM25 ties and candidate order decides -- which makes the cap 218 // the ONLY thing that can move the order, and makes the control/fire pair distinguishing by 219 // construction (the pre-change code has no entity key at all). 220 let dent: *u8 = "zqentitypage shared article body" as *u8 221 let uen: *u8 = "https://en.wikitest.org/wiki/Entity" as *u8 222 let usv: *u8 = "https://sv.wikitest.org/wiki/Entity" as *u8 223 let uit: *u8 = "https://it.wikitest.org/wiki/Entity" as *u8 224 let uot: *u8 = "https://en.wikitest.org/wiki/Other" as *u8 225 ss_add(w, 1, "doc:710" as *u8, dent, g_strlen(dent)) 226 ss_add(w, 1, "url:710" as *u8, uen, g_strlen(uen)) 227 ss_add(w, 1, "doc:711" as *u8, dent, g_strlen(dent)) 228 ss_add(w, 1, "url:711" as *u8, usv, g_strlen(usv)) 229 ss_add(w, 1, "doc:712" as *u8, dent, g_strlen(dent)) 230 ss_add(w, 1, "url:712" as *u8, uit, g_strlen(uit)) 231 ss_add(w, 1, "doc:713" as *u8, dent, g_strlen(dent)) 232 ss_add(w, 1, "url:713" as *u8, uot, g_strlen(uot)) 233 // proximity fixture (T-prox, R1b): 801 (scattered) + 800 (clustered) have the SAME length + SAME tf 234 // -> IDENTICAL BM25; ONLY the proximity boost distinguishes them. 801 is indexed FIRST (so a naive 235 // tie would pick 801); the CLUSTERED 800 must win via proximity. proxterma/proxtermb/zfil* are unique. 236 let dpxs: *u8 = "proxterma zfila zfilb zfilc zfild zfile zfilf proxtermb" as *u8 237 let dpxc: *u8 = "zfilg zfilh zfili zfilj proxterma proxtermb zfilk zfill" as *u8 238 ss_add(w, 1, "doc:801" as *u8, dpxs, g_strlen(dpxs)) 239 ss_add(w, 1, "doc:800" as *u8, dpxc, g_strlen(dpxc)) 240 // title-field fixture (T-title, R1c): 901 (body) + 900 (title) have SAME length + SAME tf -> IDENTICAL 241 // BM25; only the title-field boost distinguishes. 901 indexed FIRST; the TITLE-match 900 must win. 242 let dttl: *u8 = "titletok aaa1 aaa2 aaa3 aaa4 aaa5 aaa6 aaa7" as *u8 243 let dbod: *u8 = "bbb1 bbb2 bbb3 bbb4 bbb5 bbb6 titletok bbb7" as *u8 244 ss_add(w, 1, "doc:901" as *u8, dbod, g_strlen(dbod)) 245 ss_add(w, 1, "doc:900" as *u8, dttl, g_strlen(dttl)) 246 // multilingual fixture (2026-07-23, T33-T36): Russian (Cyrillic word tokens, case-folded), 247 // Japanese (Han bigrams), Korean (Hangul bigrams) -- end-to-end through the FULL dss path 248 // (query tokenize -> postings -> BM25 tf-scan re-tokenizing the doc). Unique terms, no fixture overlap. 249 let dru: *u8 = "\xD1\x81\xD1\x83\xD0\xB4 \xD0\xBF\xD0\xBE \xD0\xBD\xD0\xB0\xD1\x81\xD0\xBB\xD0\xB5\xD0\xB4\xD1\x81\xD1\x82\xD0\xB2\xD1\x83 \xD0\xB2 \xD0\xBC\xD0\xBE\xD1\x81\xD0\xBA\xD0\xB2\xD0\xB5 \xD1\x80\xD0\xB0\xD1\x81\xD1\x81\xD0\xBC\xD0\xBE\xD1\x82\xD1\x80\xD0\xB5\xD0\xBB \xD0\xB4\xD0\xB5\xD0\xBB\xD0\xBE" as *u8 250 let dja: *u8 = "\xE6\x9D\xB1\xE4\xBA\xAC\xE5\xA4\xA7\xE5\xAD\xA6 \xE7\xA0\x94\xE7\xA9\xB6" as *u8 251 let dko: *u8 = "\xED\x95\x9C\xEA\xB5\xAD \xEA\xB2\x80\xEC\x83\x89 \xEC\x97\x94\xEC\xA7\x84 \xEA\xB0\x9C\xEB\xB0\x9C" as *u8 252 ss_add(w, 1, "doc:950" as *u8, dru, g_strlen(dru)) 253 ss_add(w, 1, "doc:951" as *u8, dja, g_strlen(dja)) 254 ss_add(w, 1, "doc:952" as *u8, dko, g_strlen(dko)) 255 // R2b url-scope fixture (T37-T40, 2026-08-04): four docs share the term "subgadget"; only 960 lives 256 // under /r/gadgetlab on reddit. 963 = the SEGMENT-ANCHOR decoy (/r/gadgetlabmemes must NOT satisfy 257 // r/gadgetlab). 962 = the off-reddit control for inurl: (its url carries no "comments"). 258 let dsr1: *u8 = "subgadget review thread alpha" as *u8 259 let dsr2: *u8 = "subgadget review thread beta" as *u8 260 let dsr3: *u8 = "subgadget review blogpost" as *u8 261 let dsr4: *u8 = "subgadget meme collection" as *u8 262 ss_add(w, 1, "doc:960" as *u8, dsr1, g_strlen(dsr1)) 263 let u960: *u8 = "https://old.reddit.com/r/gadgetlab/comments/ab1/thread/" as *u8 264 ss_add(w, 1, "url:960" as *u8, u960, g_strlen(u960)) 265 ss_add(w, 1, "doc:961" as *u8, dsr2, g_strlen(dsr2)) 266 let u961: *u8 = "https://old.reddit.com/r/cooking/comments/cd2/thread/" as *u8 267 ss_add(w, 1, "url:961" as *u8, u961, g_strlen(u961)) 268 ss_add(w, 1, "doc:962" as *u8, dsr3, g_strlen(dsr3)) 269 let u962: *u8 = "https://techblog.example.com/subgadget-review" as *u8 270 ss_add(w, 1, "url:962" as *u8, u962, g_strlen(u962)) 271 ss_add(w, 1, "doc:963" as *u8, dsr4, g_strlen(dsr4)) 272 let u963: *u8 = "https://old.reddit.com/r/gadgetlabmemes/xyz" as *u8 273 ss_add(w, 1, "url:963" as *u8, u963, g_strlen(u963)) 274 // S7 bm25q-fusion fixture (T49, 2026-09-16) in its OWN shard (dp-ssgatefuse-pub-), so the tooth is IDEMPOTENT: 275 // the shared fixture shard accumulates a version of every doc per run and write-time dcount counts versions, so 276 // any expectation that leans on an idf RATIO drifts run over run (measured: the same corpus read moved=2 on one 277 // run and moved=0 on the next). Here the repeated term and the rare term sit in EXACTLY the same number of docs 278 // (x in 970/971/972, z in 971/972/973), so their idfs are equal on every run whatever the version count, and the 279 // cycle is set by LENGTH alone: 970 = the repeated term six times at the candidate mean (tfnorm 1.83), 971 = it 280 // once beside z at 0.69x the mean (tfnorm 1.15), 972 = the same one pad longer, 973 = z alone in a long doc. 281 // Every x-doc has x at token 0, so the title bonus plus proximity saturate its factor at 2x and cancel. Plain 282 // weights the repeat 2x: 970 (7.3w) > 971 (6.9w) > 972 > 973; BM25Q weights it 1.374x: 971 (5.4w) > 972 > 970 283 // (5.0w) > 973 -- a 3-cycle, so the fusion moves 970 and 971 (moved=2); coordination then drops 970 (no z) and 284 // 971 serves first. The tooth prints the byte lengths so a drift is readable. 285 let prefix2: *u8 = "knowledge/store/dp-ssgatefuse-pub-" as *u8 286 let w2: *i64 = ss_begin() 287 let s7buf: *u8 = sys_mmap(S9G_BUF_4K) 288 let s7l970: i64 = s7_build(s7buf, "zqxrep zqxrep zqxrep zqxrep zqxrep zqxrep" as *u8, "a" as *u8, 24, "" as *u8, 0) 289 ss_add(w2, 1, "doc:970" as *u8, s7buf, s7l970) 290 let s7l971: i64 = s7_build(s7buf, "zqxrep" as *u8, "b" as *u8, 19, "zqzrare" as *u8, 0) 291 ss_add(w2, 1, "doc:971" as *u8, s7buf, s7l971) 292 let s7l972: i64 = s7_build(s7buf, "zqxrep" as *u8, "c" as *u8, 20, "zqzrare" as *u8, 0) 293 ss_add(w2, 1, "doc:972" as *u8, s7buf, s7l972) 294 let s7l973: i64 = s7_build(s7buf, "zqzrare" as *u8, "d" as *u8, 47, "" as *u8, 0) 295 ss_add(w2, 1, "doc:973" as *u8, s7buf, s7l973) 296 // S9 ENTITY DISCRIMINATOR fixture (T50-T52, 2026-09-17): 850 = a content page carrying both name words once; 297 // (ids and page sizes named below so the magic ratchet reads purpose, not values) 298 // 851 = another engine's RESULTS PAGE (url /search?q=) repeating both words six times; 852 = a content page 299 // carrying only the RARE word sixteen times (the poem-form-article shape: under plain BM25 it outscores the 300 // page carrying the whole name); 860-869 make zqnamea the COMMON word so zqnameb's idf carries 852. 301 // 850 is LONG on purpose: a real profile page mentions the name once inside a biography, and BM25 length 302 // normalisation plus the 0.5x results-page multiplier still leaves a short results page repeating the name 303 // ABOVE it (the live shape 2026-09-16: content 88M, results pages 38M = 77M raw). The tier is what fixes it. 304 let d850: *u8 = "zqnamea zqnameb profile biography born career early life education family works awards filmography agency debut studio released series photobook interview magazine cover television appearance radio host event stage collaboration brand ambassador charity sport hobby travel language music instrument reading writing painting cooking garden pets childhood school university degree thesis mentor colleague friend partner marriage children residence hometown region country citizenship heritage ancestry retirement legacy archive museum exhibition catalogue foundation scholarship prize nomination ceremony gala festival premiere audience critic review rating chart sales" as *u8 305 let d851: *u8 = "zqnamea zqnameb zqnamea zqnameb zqnamea zqnameb zqnamea zqnameb zqnamea zqnameb zqnamea zqnameb results" as *u8 306 let d852: *u8 = "zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb zqnameb poem" as *u8 307 let u850: *u8 = "https://content.example.com/people/zqnamea-zqnameb" as *u8 308 let u851: *u8 = "https://engine.example.com/search?q=zqnamea+zqnameb" as *u8 309 let u852: *u8 = "https://content.example.com/poems/zqnameb" as *u8 310 ss_add(w, 1, "doc:850" as *u8, d850, g_strlen(d850)); ss_add(w, 1, "url:850" as *u8, u850, g_strlen(u850)) 311 ss_add(w, 1, "doc:851" as *u8, d851, g_strlen(d851)); ss_add(w, 1, "url:851" as *u8, u851, g_strlen(u851)) 312 ss_add(w, 1, "doc:852" as *u8, d852, g_strlen(d852)); ss_add(w, 1, "url:852" as *u8, u852, g_strlen(u852)) 313 // 853 (S9b): both words, SCATTERED -- the first word opens the page, the second closes it. Coverage 2, no phrase. 314 let d853: *u8 = "zqnamea opens this note about many unrelated things weather harvest markets travel lodging recipes tools repairs gardens letters accounts errands neighbours meetings schedules budgets receipts invoices ledgers archives catalogues indexes registers rosters calendars almanacs journals diaries memoirs essays reports surveys reviews digests summaries abstracts glossaries manuals handbooks guides primers tutorials lessons courses lectures seminars workshops clinics forums panels debates and closes with zqnameb" as *u8 315 let u853: *u8 = "https://content.example.com/notes/zq" as *u8 316 ss_add(w, 1, "doc:853" as *u8, d853, g_strlen(d853)); ss_add(w, 1, "url:853" as *u8, u853, g_strlen(u853)) 317 let dfil: *u8 = "zqnamea filler note" as *u8 318 ss_add(w, 1, "doc:860" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:860" as *u8, "https://f0.example.com/p" as *u8, g_strlen("https://f0.example.com/p" as *u8)) 319 ss_add(w, 1, "doc:861" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:861" as *u8, "https://f1.example.com/p" as *u8, g_strlen("https://f1.example.com/p" as *u8)) 320 ss_add(w, 1, "doc:862" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:862" as *u8, "https://f2.example.com/p" as *u8, g_strlen("https://f2.example.com/p" as *u8)) 321 ss_add(w, 1, "doc:863" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:863" as *u8, "https://f3.example.com/p" as *u8, g_strlen("https://f3.example.com/p" as *u8)) 322 ss_add(w, 1, "doc:864" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:864" as *u8, "https://f4.example.com/p" as *u8, g_strlen("https://f4.example.com/p" as *u8)) 323 ss_add(w, 1, "doc:865" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:865" as *u8, "https://f5.example.com/p" as *u8, g_strlen("https://f5.example.com/p" as *u8)) 324 ss_add(w, 1, "doc:866" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:866" as *u8, "https://f6.example.com/p" as *u8, g_strlen("https://f6.example.com/p" as *u8)) 325 ss_add(w, 1, "doc:867" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:867" as *u8, "https://f7.example.com/p" as *u8, g_strlen("https://f7.example.com/p" as *u8)) 326 ss_add(w, 1, "doc:868" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:868" as *u8, "https://f8.example.com/p" as *u8, g_strlen("https://f8.example.com/p" as *u8)) 327 ss_add(w, 1, "doc:869" as *u8, dfil, g_strlen(dfil)); ss_add(w, 1, "url:869" as *u8, "https://f9.example.com/p" as *u8, g_strlen("https://f9.example.com/p" as *u8)) 328 ss_commit(prefix2, w2, 1) 329 ss_commit(prefix, w, 1) 330 g_puts("wrote 40 docs (+2 pol, +7 host-crowding, +2 authority +1 pr:, +2 url-dup, +4 url-scope) -> dp-ssgatetest-pub-\n") 331 332 let ctr: *i64 = gv_ctr() 333 let cids: *i64 = sys_mmap(64 * 8) as *i64 334 let scores: *i64 = sys_mmap(64 * 8) as *i64 335 let dom: *u8 = "ssgatetest" as *u8 336 337 // T1: "probate" -> exactly doc 101 (only estate mentions probate) 338 let q1: *u8 = "probate" as *u8 339 let n1: i64 = dss_search(dom, q1, g_strlen(q1), cids, scores, 10) 340 g_check("T1 probate->101" as *u8, n1, cids, 1, 101, ctr) 341 342 // T2: "divorce custody" -> doc 102 top (matches BOTH terms) 343 let q2: *u8 = "divorce custody" as *u8 344 let n2: i64 = dss_search(dom, q2, g_strlen(q2), cids, scores, 10) 345 g_check("T2 divorce+custody->102" as *u8, n2, cids, 1, 102, ctr) 346 347 // T3: "law contracts" -> doc 103 TOP (matches law+contracts=2) above 102 (law=1). 2 candidates. 348 let q3: *u8 = "law contracts" as *u8 349 let n3: i64 = dss_search(dom, q3, g_strlen(q3), cids, scores, 10) 350 g_check("T3 law+contracts->103 top" as *u8, n3, cids, 2, 103, ctr) 351 352 // T4: "law" -> both 102 (Family Law) and 103 (Business Law); 101 has no 'law' 353 let q4: *u8 = "law" as *u8 354 let n4: i64 = dss_search(dom, q4, g_strlen(q4), cids, scores, 10) 355 g_check("T4 law->2 docs" as *u8, n4, cids, 2, 0 - 1, ctr) 356 357 // T5 NEG: absent term -> 0 results (no false hit) 358 let q5: *u8 = "zzznotpresentanywhere" as *u8 359 let n5: i64 = dss_search(dom, q5, g_strlen(q5), cids, scores, 10) 360 g_check("T5 NEG absent-term->0" as *u8, n5, cids, 0, 0 - 1, ctr) 361 362 // T6 NEG (isolation): a domain with NO shard -> 0, not -2, not a crash 363 let q6: *u8 = "probate" as *u8 364 let n6: i64 = dss_search("nosuchdomain" as *u8, q6, g_strlen(q6), cids, scores, 10) 365 g_check("T6 NEG absent-shard->0" as *u8, n6, cids, 0, 0 - 1, ctr) 366 367 // T7 NEG (owner consent): doc 104 exists in the pub shard but its pol row LACKS the search bit 368 // (pol=2 = AI_BLOG only) -> query-time consent filter EXCLUDES it (the retired tsv emitter's 369 // emission-time enforcement, now live per hit) 370 let q7: *u8 = "adoption" as *u8 371 let n7: i64 = dss_search(dom, q7, g_strlen(q7), cids, scores, 10) 372 g_check("T7 NEG consent opt-out excluded" as *u8, n7, cids, 0, 0 - 1, ctr) 373 374 // T8 (owner consent): doc 105's pol row HAS the search bit (pol=1) -> included 375 let q8: *u8 = "annexation" as *u8 376 let n8: i64 = dss_search(dom, q8, g_strlen(q8), cids, scores, 10) 377 g_check("T8 consent opt-in included" as *u8, n8, cids, 1, 105, ctr) 378 379 // T9 (IDF ranking): query "idfcommon idfrare" -- 5 candidates; the RARE-term doc (106) must be TOP. 380 // idf(idfrare)=log2(N/1) >> idf(idfcommon)=log2(N/4); tf equal (2 each for 106/107). The retired 381 // distinct+tf scorer tied them and candidate order picked 107 -- this row is the ranking-rung proof. 382 let q9: *u8 = "idfcommon idfrare" as *u8 383 let n9: i64 = dss_search(dom, q9, g_strlen(q9), cids, scores, 10) 384 g_check("T9 IDF: rare-term doc outranks common-term doc" as *u8, n9, cids, 5, 106, ctr) 385 386 // T10 (pagination): 15 pgseg docs -- offset windows must return 10/5/0 with total=15 and disjoint pages 387 let qp: *u8 = "pgseg" as *u8 388 let tot: *i64 = sys_mmap(16) as *i64 389 let np0: i64 = dss_search_off(dom, qp, g_strlen(qp), cids, scores, 10, 0, tot) 390 let p0top: i64 = cids[0] 391 let np1: i64 = dss_search_off(dom, qp, g_strlen(qp), cids, scores, 10, 10, tot) 392 let p1top: i64 = cids[0] 393 let np2: i64 = dss_search_off(dom, qp, g_strlen(qp), cids, scores, 10, 20, tot) 394 var t10: i64 = 0 395 if np0 == 10 { if np1 == 5 { if np2 == 0 { if tot[0] == 15 { if p0top != p1top { t10 = 1 } } } } } 396 g_puts("T10 pagination offsets 10/5/0 total=15 disjoint") 397 g_puts("\n") 398 gv_check("T10-pagination-offsets-10/5/0-total=15-disjoint", t10, ctr) 399 400 // T11 (+term AND): "law +contracts" keeps ONLY the doc that carries contracts (plain OR returned 2) 401 let q11: *u8 = "law +contracts" as *u8 402 let n11: i64 = dss_search(dom, q11, g_strlen(q11), cids, scores, 10) 403 g_check("T11 +term AND filter" as *u8, n11, cids, 1, 103, ctr) 404 405 // T12 did-you-mean: an unknown term corrects to the closest dictionary term (probete -> probate) 406 let fix: *u8 = sys_mmap(256) 407 let fl: i64 = dss_correct(dom, "probete" as *u8, 7, fix, 255) 408 var t12: i64 = 0 409 if fl == 7 { 410 let want: *u8 = "probate" as *u8 411 var eq12: i64 = 1 412 var z12: i64 = 0 413 while z12 < 7 { if fix[z12] != want[z12] { eq12 = 0; z12 = 7 } else { z12 = z12 + 1 } } 414 t12 = eq12 415 } 416 g_puts("T12 did-you-mean probete->probate") 417 g_puts("\n") 418 gv_check("T12-did-you-mean-probete->probate", t12, ctr) 419 420 // T13 suggest: the dictionary completes "prob" (top completion = probate) 421 let sg: *u8 = sys_mmap(8 * 64) 422 let n13: i64 = dss_suggest(dom, "prob" as *u8, 4, sg, 8) 423 var t13: i64 = 0 424 if n13 >= 1 { 425 let w13: *u8 = "probate" as *u8 426 var eq13: i64 = 1 427 var z13: i64 = 0 428 while z13 < 8 { if sg[z13] != w13[z13] { eq13 = 0; z13 = 8 } else { z13 = z13 + 1 } } 429 t13 = eq13 430 } 431 g_puts("T13 suggest prob->probate") 432 g_puts("\n") 433 gv_check("T13-suggest-prob->probate", t13, ctr) 434 435 // T14 (BM25 length norm): equal tf, wildly different lengths -- the SHORT doc must outrank the LONG 436 // one (b=0 tied them and insertion order picked the long 126; b=0.75 must pick 127) 437 let q14: *u8 = "lennorm" as *u8 438 let n14: i64 = dss_search(dom, q14, g_strlen(q14), cids, scores, 10) 439 g_check("T14 length-norm: short doc outranks verbose doc" as *u8, n14, cids, 2, 127, ctr) 440 441 // T15/T16 PHRASE through dss: d1 carries "... living trusts ..." adjacent -- the quoted phrase 442 // matches; the REVERSED phrase must not (order-sensitive adjacency via the NXQ1 sidecar) 443 let q15: *u8 = "\"living trusts\"" as *u8 444 let n15: i64 = dss_search(dom, q15, g_strlen(q15), cids, scores, 10) 445 g_check("T15 phrase [living trusts] -> doc 101" as *u8, n15, cids, 1, 101, ctr) 446 let q16: *u8 = "\"trusts living\"" as *u8 447 let n16: i64 = dss_search(dom, q16, g_strlen(q16), cids, scores, 10) 448 g_check("T16 NEG phrase [trusts living] -> 0" as *u8, n16, cids, 0, 0 - 1, ctr) 449 450 // T17 (-term NOT): "law -contracts" -- OR gave both law docs; the exclusion drops the contracts one 451 let q17: *u8 = "law -contracts" as *u8 452 let n17: i64 = dss_search(dom, q17, g_strlen(q17), cids, scores, 10) 453 g_check("T17 -term exclusion (law -contracts -> 102 only)" as *u8, n17, cids, 1, 102, ctr) 454 455 // T18b (NO ZERO-SCORE NOISE, operator-reported "julia kyoka" bug): doc 128 has "ranking" (matched via 456 // stem) but NOT "zznomatch". A 2-term query "ranking zznomatch" must return ONLY positively-scored docs; 457 // no doc may appear at score 0. Assert every returned score is > 0. 458 let qzs: *u8 = "ranking zznomatchword" as *u8 459 let nzs: i64 = dss_search(dom, qzs, g_strlen(qzs), cids, scores, 20) 460 var allpos: i64 = 1 461 var zi: i64 = 0 462 while zi < nzs { if scores[zi] <= 0 { allpos = 0 } zi = zi + 1 } 463 g_puts("T18b no zero-score results (n="); g_num(nzs); g_puts(")") 464 var tzs: i64 = 0 465 if allpos == 1 { if nzs >= 1 { tzs = 1 } } 466 g_puts(" allpos="); g_num(allpos); g_puts(" 467") 468 gv_check("T18b-no-zero-score-results-shown", tzs, ctr) 469 470 // T18 (STEMMING recall): the doc says "indexing"/"crawlers"/"ranking"; queries for the STEM-shared 471 // forms "index"/"crawler"/"rank" must recall it via query-time dictionary stem-expansion (no index change) 472 let q18a: *u8 = "index" as *u8 473 let n18a: i64 = dss_search(dom, q18a, g_strlen(q18a), cids, scores, 10) 474 var t18: i64 = 0 475 if n18a >= 1 { 476 // 128 must be among the results (its only 'index'-family token is 'indexing') 477 var f18: i64 = 0 478 var z18: i64 = 0 479 while z18 < n18a { if cids[z18] == 128 { f18 = 1 } z18 = z18 + 1 } 480 // and a query for "crawler" must recall 128 (which only says "crawlers") 481 let n18b: i64 = dss_search(dom, "crawler" as *u8, 7, cids, scores, 10) 482 var f18b: i64 = 0 483 var z18b: i64 = 0 484 while z18b < n18b { if cids[z18b] == 128 { f18b = 1 } z18b = z18b + 1 } 485 if f18 == 1 { if f18b == 1 { t18 = 1 } } 486 } 487 g_puts("T18 stemming recall (index->indexing, crawler->crawlers)") 488 g_puts("\n") 489 gv_check("T18-stemming-recall", t18, ctr) 490 491 // T19-T21 (site: FIELD FILTER, the faceted rung): "law" matches 102 (law.example.com) + 103 (no url). 492 // site:example.com keeps only 102 (dot-suffix host match; url-less 103 dropped as not site-attributable) 493 let q19: *u8 = "law site:example.com" as *u8 494 let n19: i64 = dss_search(dom, q19, g_strlen(q19), cids, scores, 10) 495 g_check("T19 site: filter (law site:example.com -> 102 only)" as *u8, n19, cids, 1, 102, ctr) 496 // cross-host NEG: no "law" doc lives on wikipedia.org 497 let q20: *u8 = "law site:wikipedia.org" as *u8 498 let n20: i64 = dss_search(dom, q20, g_strlen(q20), cids, scores, 10) 499 g_check("T20 NEG site: cross-host (law site:wikipedia.org -> 0)" as *u8, n20, cids, 0, 0 - 1, ctr) 500 // suffix-boundary NEG: kipedia.org must NOT match en.wikipedia.org (the '.' boundary rule) 501 let q21: *u8 = "trusts site:kipedia.org" as *u8 502 let n21: i64 = dss_search(dom, q21, g_strlen(q21), cids, scores, 10) 503 var t21b: i64 = 0 504 if n21 == 0 { 505 let q21b: *u8 = "trusts site:wikipedia.org" as *u8 506 let n21b: i64 = dss_search(dom, q21b, g_strlen(q21b), cids, scores, 10) 507 if n21b == 1 { if cids[0] == 101 { t21b = 1 } } 508 } 509 g_puts("T21 suffix boundary (kipedia.org->0, wikipedia.org->101)") 510 g_puts("\n") 511 gv_check("T21-suffix-boundary", t21b, ctr) 512 513 // T22 (host-diversity CONTROL, webdiv=0): no cap -> the 5 tf=2 crowd-host docs fill the top 5, so the 514 // 4th result (cids[3]) is a crowd-host doc (304). Non-web scopes (site/trusted) always use this path. 515 let qd: *u8 = "diversitytest" as *u8 516 let td: *i64 = sys_mmap(16) as *i64 517 let nd0: i64 = dss_search_off_div(dom, qd, g_strlen(qd), cids, scores, 10, 0, td, 0) 518 var t22: i64 = 0 519 if nd0 == 7 { if cids[3] == 304 { t22 = 1 } } 520 g_puts("T22 host-cap CONTROL (webdiv=0: crowd fills top, cids[3]=304)") 521 g_puts(" cids[3]="); g_num(cids[3]); g_puts(" n="); g_num(nd0); g_puts("\n") 522 gv_check("T22-host-cap-CONTROL", t22, ctr) 523 524 // T23 (host-diversity CAP, webdiv=1): crowd host caps at DSS_HOSTCAP=3, so an OTHER-host doc (306) is 525 // promoted into slot 3 -- and ALL 7 still return (over-cap crowd docs overflow via phase B, never 526 // deleted). Fails on the old code by construction (no cap -> cids[3]=304). 527 let nd1: i64 = dss_search_off_div(dom, qd, g_strlen(qd), cids, scores, 10, 0, td, 1) 528 var t23: i64 = 0 529 if nd1 == 7 { if cids[3] == 306 { t23 = 1 } } 530 g_puts("T23 host-cap FIRES (webdiv=1: cids[3]=306, all 7 still returned)") 531 g_puts(" cids[3]="); g_num(cids[3]); g_puts(" n="); g_num(nd1); g_puts("\n") 532 gv_check("T23-host-cap-FIRES", t23, ctr) 533 534 // T32 (entity-collapse CONTROL, webdiv=0): no entity cap -> the three language editions of ONE 535 // article keep consecutive slots, so cids[1] is another edition (711). This is the state the SERP 536 // was actually in on 2026-08-17. 537 let qe: *u8 = "zqentitypage" as *u8 538 let te: *i64 = sys_mmap(16) as *i64 539 let ne0: i64 = dss_search_off_div(dom, qe, g_strlen(qe), cids, scores, 10, 0, te, 0) 540 var t32: i64 = 0 541 if ne0 == 4 { if cids[1] == 711 { t32 = 1 } } 542 g_puts("T32 entity-cap CONTROL (webdiv=0: editions stay adjacent, cids[1]=711)") 543 g_puts(" cids[1]="); g_num(cids[1]); g_puts(" n="); g_num(ne0); g_puts("\n") 544 gv_check("T32-entity-cap-CONTROL", t32, ctr) 545 546 // T33 (entity-collapse FIRES, webdiv=1): one edition is served, then the DIFFERENT-path article on 547 // the same domain family (713) takes slot 1 -- proving the key collapses editions WITHOUT swallowing 548 // a genuinely different page. All 4 still return: over-cap editions overflow via phase B, so this 549 // DOWN-RANKS and never deletes. Fails on the pre-change code by construction (no entity key exists). 550 let ne1: i64 = dss_search_off_div(dom, qe, g_strlen(qe), cids, scores, 10, 0, te, 1) 551 var t33: i64 = 0 552 if ne1 == 4 { if cids[1] == 713 { t33 = 1 } } 553 g_puts("T33 entity-cap FIRES (webdiv=1: cids[1]=713 different-path survives, all 4 returned)") 554 g_puts(" cids[1]="); g_num(cids[1]); g_puts(" n="); g_num(ne1); g_puts("\n") 555 gv_check("T33-entity-cap-FIRES", t33, ctr) 556 557 // T24 (dss_is_web predicate): only the exact "web" shard triggers the cap; site domains + "trusted" + 558 // a "web"-prefixed decoy do NOT (so a customer site named web*.com is never wrongly capped). 559 var t24: i64 = 0 560 if dss_is_web("web" as *u8) == 1 { if dss_is_web("trusted" as *u8) == 0 { if dss_is_web("andelinwest.com" as *u8) == 0 { if dss_is_web("weblog.com" as *u8) == 0 { t24 = 1 } } } } 561 g_puts("T24 dss_is_web exact-match (web=1; trusted/site/web*=0)") 562 g_puts("\n") 563 gv_check("T24-dss_is_web-exact-match", t24, ctr) 564 565 // T25 (dss_hosthash): same host -> same fingerprint; different host -> different; no url host -> 0. 566 let hscr2: *u8 = sys_mmap(256) 567 let hh_a: i64 = dss_hosthash("https://crowd.example.com/x" as *u8, 27, hscr2) 568 let hh_b: i64 = dss_hosthash("https://crowd.example.com/y" as *u8, 27, hscr2) 569 let hh_c: i64 = dss_hosthash("https://other.example.com/z" as *u8, 27, hscr2) 570 let hh_d: i64 = dss_hosthash("relative/path/nohost" as *u8, 20, hscr2) 571 var t25: i64 = 0 572 if hh_a == hh_b { if hh_a != hh_c { if hh_a != 0 { if hh_d == 0 { t25 = 1 } } } } 573 g_puts("T25 dss_hosthash (same-host eq, diff-host neq, no-host=0)") 574 g_puts("\n") 575 gv_check("T25-dss_hosthash", t25, ctr) 576 577 // T28 (P1 authority CONTROL, webdiv=0): equal-BM25 tie -> candidate order -> doc 600 first (no pr fusion) 578 let qa: *u8 = "authorityterm" as *u8 579 let ta: *i64 = sys_mmap(16) as *i64 580 let na0: i64 = dss_search_off_div(dom, qa, g_strlen(qa), cids, scores, 10, 0, ta, 0) 581 g_check("T28 authority CONTROL (webdiv=0: tie -> 600)" as *u8, na0, cids, 2, 600, ctr) 582 // T29 (P1 authority FUSION, webdiv=1): doc 601's PageRank prior lifts it above the equal-BM25 doc 600 583 let na1: i64 = dss_search_off_div(dom, qa, g_strlen(qa), cids, scores, 10, 0, ta, 1) 584 g_check("T29 authority FUSION (webdiv=1: pr: prior -> 601 first)" as *u8, na1, cids, 2, 601, ctr) 585 586 // T30 URL-DEDUP CONTROL (webdiv=0): both snapshots of one url serve; site/trusted stay byte-identical 587 let qd: *u8 = "zqduppage" as *u8 588 let nd0: i64 = dss_search_off_div(dom, qd, g_strlen(qd), cids, scores, 10, 0, ta, 0) 589 g_check("T30 url-dedup CONTROL (webdiv=0: both snapshots serve)" as *u8, nd0, cids, 2, 700, ctr) 590 // T31 URL-DEDUP (webdiv=1): ONE result per url (tie -> first candidate) and totals = DISTINCT pages 591 let nd1: i64 = dss_search_off_div(dom, qd, g_strlen(qd), cids, scores, 10, 0, ta, 1) 592 var t31: i64 = 0 593 if nd1 == 1 { if cids[0] == 700 { if ta[0] == 1 { t31 = 1 } } } 594 g_puts("T31 url-dedup (webdiv=1: 1 result, cid 700, total=1)") 595 g_puts("\n") 596 gv_check("T31-url-dedup", t31, ctr) 597 598 // T-prox (R1b PROXIMITY): 800 (clustered) and 801 (scattered) carry IDENTICAL BM25 (same len+tf); 599 // the proximity boost must rank the CLUSTERED doc 800 first even though 801 was indexed first. This 600 // row FAILS by construction without the boost (tie -> indexed-first 801) and PASSES with it. 601 let qpx: *u8 = "proxterma proxtermb" as *u8 602 let npx: i64 = dss_search(dom, qpx, g_strlen(qpx), cids, scores, 10) 603 g_check("T-prox clustered 800 outranks scattered 801" as *u8, npx, cids, 2, 800, ctr) 604 605 // T-title (R1c): 900 (query term in the TITLE) vs 901 (in the BODY), IDENTICAL BM25; the title-field 606 // boost must rank the TITLE-match doc 900 first even though 901 was indexed first. 607 let qtt: *u8 = "titletok" as *u8 608 let ntt: i64 = dss_search(dom, qtt, g_strlen(qtt), cids, scores, 10) 609 g_check("T-title: title-field match 900 outranks body match 901" as *u8, ntt, cids, 2, 900, ctr) 610 611 // T33 RUSSIAN end-to-end: Cyrillic query term -> doc 950 (word tokens through the whole dss path) 612 let q33: *u8 = "\xD0\xBD\xD0\xB0\xD1\x81\xD0\xBB\xD0\xB5\xD0\xB4\xD1\x81\xD1\x82\xD0\xB2\xD1\x83" as *u8 613 let n33: i64 = dss_search(dom, q33, g_strlen(q33), cids, scores, 10) 614 g_check("T33 russian (nasledstvu)->950" as *u8, n33, cids, 1, 950, ctr) 615 // T34 JAPANESE end-to-end: 4-char Han query -> overlapping bigrams -> doc 951 616 let q34: *u8 = "\xE6\x9D\xB1\xE4\xBA\xAC\xE5\xA4\xA7\xE5\xAD\xA6" as *u8 617 let n34: i64 = dss_search(dom, q34, g_strlen(q34), cids, scores, 10) 618 g_check("T34 japanese (toukyoudaigaku)->951" as *u8, n34, cids, 1, 951, ctr) 619 // T35 KOREAN end-to-end: Hangul bigram query -> doc 952 620 let q35: *u8 = "\xEA\xB2\x80\xEC\x83\x89" as *u8 621 let n35: i64 = dss_search(dom, q35, g_strlen(q35), cids, scores, 10) 622 g_check("T35 korean (geomsaek)->952" as *u8, n35, cids, 1, 952, ctr) 623 // T36 CYRILLIC CASE-FOLD: uppercase query MOSKVE matches the lowercase doc 624 let q36: *u8 = "\xD0\x9C\xD0\x9E\xD0\xA1\xD0\x9A\xD0\x92\xD0\x95" as *u8 625 let n36: i64 = dss_search(dom, q36, g_strlen(q36), cids, scores, 10) 626 g_check("T36 cyrillic case-fold (MOSKVE)->950" as *u8, n36, cids, 1, 950, ctr) 627 628 // T37 SUBREDDIT idiom (R2b, 2026-08-04): "r/GadgetLab subgadget" implies site:reddit.com + a case- 629 // folded, segment-anchored /r/gadgetlab path -> ONLY 960. 961 = other sub, 962 = off-reddit, 963 = 630 // /r/gadgetlabmemes (the anchor decoy). The pre-rewrite engine returned all four ("r" died at the 631 // tokenizer's 2-char floor) -- this tooth fails without the fix BY CONSTRUCTION. 632 let q37: *u8 = "r/GadgetLab subgadget" as *u8 633 let n37: i64 = dss_search(dom, q37, g_strlen(q37), cids, scores, 10) 634 g_check("T37 r/<sub> scopes to the subreddit (960 only)" as *u8, n37, cids, 1, 960, ctr) 635 // T38 inurl: general URL filter, ANY site: docs whose url carries "comments" = 960+961 exactly 636 let q38: *u8 = "inurl:comments subgadget" as *u8 637 let n38: i64 = dss_search(dom, q38, g_strlen(q38), cids, scores, 10) 638 g_check("T38 inurl:comments -> the two thread docs" as *u8, n38, cids, 2, 0 - 1, ctr) 639 // T39 CONTROL: the bare term returns ALL FOUR docs -- queries without a url-scope clause are 640 // byte-identical to the pre-R2b engine (the ruler-safety claim, positively controlled) 641 let q39: *u8 = "subgadget" as *u8 642 let n39: i64 = dss_search(dom, q39, g_strlen(q39), cids, scores, 10) 643 g_check("T39 CONTROL bare term -> all 4 docs" as *u8, n39, cids, 4, 0 - 1, ctr) 644 // T40 NEG: an unknown subreddit -> honest 0, never a silent fallback to unscoped results 645 let q40: *u8 = "r/nosuchsub subgadget" as *u8 646 let n40: i64 = dss_search(dom, q40, g_strlen(q40), cids, scores, 10) 647 g_check("T40 NEG r/nosuchsub -> 0" as *u8, n40, cids, 0, 0 - 1, ctr) 648 649 // T41 (R2d stuffing penalty): KATs on the factored decision, pinned to the MEASURED live docs 650 // (nx_doc_lexstat 2026-08-04). bign=150000, common dcnt=40000 (blood/pressure class), 651 // rare dcnt=120 (entity name). Spam blogspot tf34/825 -> 512; real paper tf14/1226 -> 1024; 652 // a RARE name at profile density tf40/800 -> 1024 (the julia exemption); tiny doc + small 653 // corpus -> inert 1024 (why every fixture tooth above stays byte-identical). 654 var t41: i64 = 1 655 if dss_stuff_factor(34, 825, 40000, 150000) != 512 { t41 = 0 } 656 if dss_stuff_factor(14, 1226, 40000, 150000) != 1024 { t41 = 0 } 657 if dss_stuff_factor(40, 800, 120, 150000) != 1024 { t41 = 0 } 658 if dss_stuff_factor(30, 40, 40000, 150000) != 1024 { t41 = 0 } 659 if dss_stuff_factor(34, 825, 40000, 500) != 1024 { t41 = 0 } 660 g_puts("T41 stuffing factor: spam 512 / real+rare+tiny+small-corpus 1024") 661 g_puts("\n") 662 gv_check("T41-stuffing-factor", t41, ctr) 663 664 // T42 (R2d brand-label exemption): a dense term that IS a host label of the doc's url is the 665 // term's owner, not a stuffer -- and a substring without a boundary earns NOTHING (the fix for 666 // the 368->297 navigational regression; the measured spam domain must stay penalized). 667 let t42scr: *u8 = sys_mmap(256) 668 var t42: i64 = 1 669 let u42a: *u8 = "https://github.com/features" as *u8 670 let u42b: *u8 = "https://highbloodpressure67.blogspot.com/" as *u8 671 let u42c: *u8 = "https://www.rust-lang.org/learn" as *u8 672 if dss_host_label_match(u42a, g_strlen(u42a), "github" as *u8, t42scr) != 1 { t42 = 0 } 673 if dss_host_label_match(u42b, g_strlen(u42b), "blood" as *u8, t42scr) != 0 { t42 = 0 } 674 if dss_host_label_match(u42b, g_strlen(u42b), "pressure" as *u8, t42scr) != 0 { t42 = 0 } 675 if dss_host_label_match(u42c, g_strlen(u42c), "rust" as *u8, t42scr) != 1 { t42 = 0 } 676 if dss_host_label_match(u42a, g_strlen(u42a), "hub" as *u8, t42scr) != 0 { t42 = 0 } 677 g_puts("T42 brand-label: github/rust exempt, blood/pressure/hub substrings not") 678 g_puts("\n") 679 gv_check("T42-brand-label", t42, ctr) 680 681 // ---- S7 (2026-09-16) SERVED BM25Q FUSION: unit teeth on the pure pieces, then the wire through dss_search ---- 682 // T43 qtf map: alpha beta alpha gamma beta alpha -> repeats=2, firsts 1 1 0 1 0 0, counts 3 and 2 at the firsts 683 let s7ts: *u8 = sys_mmap(6 * 64) 684 let s7tp: *i64 = sys_mmap(6 * 8) as *i64 685 let s7w0: *u8 = "alpha" as *u8 686 let s7w1: *u8 = "beta" as *u8 687 let s7w2: *u8 = "gamma" as *u8 688 var s7k: i64 = 0 689 while s7k < 6 { 690 var s7src: *u8 = s7w0 691 if s7k == 1 { s7src = s7w1 } 692 if s7k == 3 { s7src = s7w2 } 693 if s7k == 4 { s7src = s7w1 } 694 let s7d: *u8 = (s7ts as i64 + s7k * 64) as *u8 695 var s7i: i64 = 0 696 while s7src[s7i] != (0 as u8) { s7d[s7i] = s7src[s7i]; s7i = s7i + 1 } 697 s7d[s7i] = 0 as u8 698 s7tp[s7k] = s7d as i64 699 s7k = s7k + 1 700 } 701 let s7qf: *i64 = sys_mmap(6 * 8) as *i64 702 let s7qc: *i64 = sys_mmap(6 * 8) as *i64 703 let s7rep: i64 = dss_qtf_map(s7tp, 6, s7qf, s7qc) 704 var t43: i64 = 1 705 if s7rep != 2 { t43 = 0 } 706 if s7qf[0] != 1 { t43 = 0 } 707 if s7qf[1] != 1 { t43 = 0 } 708 if s7qf[2] != 0 { t43 = 0 } 709 if s7qf[3] != 1 { t43 = 0 } 710 if s7qf[4] != 0 { t43 = 0 } 711 if s7qf[5] != 0 { t43 = 0 } 712 if s7qc[0] != 3 { t43 = 0 } 713 if s7qc[1] != 2 { t43 = 0 } 714 if s7qc[3] != 1 { t43 = 0 } 715 g_puts("T43 qtf-map: repeats="); g_num(s7rep); g_puts(" firsts="); g_num(s7qf[0]); g_num(s7qf[1]); g_num(s7qf[2]); g_num(s7qf[3]); g_num(s7qf[4]); g_num(s7qf[5]); g_puts(" counts="); g_num(s7qc[0]); g_puts(","); g_num(s7qc[1]); g_puts(","); g_num(s7qc[3]) 716 g_puts("\n") 717 gv_check("T43-qtf-map", t43, ctr) 718 // T44 fuse identity: equal arms -> moved=0 and every score untouched 719 let s7p: *i64 = sys_mmap(8 * 8) as *i64 720 let s7q: *i64 = sys_mmap(8 * 8) as *i64 721 s7p[0] = 50; s7p[1] = 40; s7p[2] = 30; s7p[3] = 20; s7p[4] = 10 722 s7q[0] = 50; s7q[1] = 40; s7q[2] = 30; s7q[3] = 20; s7q[4] = 10 723 let s7m44: i64 = dss_bm25q_fuse(s7p, s7q, 5) 724 var t44: i64 = 1 725 if s7m44 != 0 { t44 = 0 } 726 if s7p[0] != 50 { t44 = 0 } 727 if s7p[1] != 40 { t44 = 0 } 728 if s7p[2] != 30 { t44 = 0 } 729 if s7p[3] != 20 { t44 = 0 } 730 if s7p[4] != 10 { t44 = 0 } 731 g_puts("T44 fuse-identity: moved="); g_num(s7m44); g_puts(" scores="); g_num(s7p[0]); g_puts(","); g_num(s7p[1]); g_puts(","); g_num(s7p[2]); g_puts(","); g_num(s7p[3]); g_puts(","); g_num(s7p[4]) 732 g_puts("\n") 733 gv_check("T44-fuse-identity", t44, ctr) 734 // T45 fuse moves on a 3-cycle: plain A>B>C (300,200,100), bm25q B>C>A -> fused B,A,C: B takes 300, A 200, C 100, moved=2 735 s7p[0] = 300; s7p[1] = 200; s7p[2] = 100 736 s7q[0] = 100; s7q[1] = 300; s7q[2] = 200 737 let s7m45: i64 = dss_bm25q_fuse(s7p, s7q, 3) 738 var t45: i64 = 1 739 if s7m45 != 2 { t45 = 0 } 740 if s7p[1] != 300 { t45 = 0 } 741 if s7p[0] != 200 { t45 = 0 } 742 if s7p[2] != 100 { t45 = 0 } 743 g_puts("T45 fuse-3cycle: moved="); g_num(s7m45); g_puts(" A="); g_num(s7p[0]); g_puts(" B="); g_num(s7p[1]); g_puts(" C="); g_num(s7p[2]) 744 g_puts("\n") 745 gv_check("T45-fuse-3cycle", t45, ctr) 746 // T46 neg-control pair-swap-is-a-tie: two participants swapped by the second arm stay in plain order (moved=0); a 747 // disqualified candidate (-1) never participates and keeps -1 748 s7p[0] = 0 - 1; s7p[1] = 200; s7p[2] = 100; s7p[3] = 0 - 1 749 s7q[0] = 0 - 1; s7q[1] = 100; s7q[2] = 200; s7q[3] = 0 - 1 750 let s7m46: i64 = dss_bm25q_fuse(s7p, s7q, 4) 751 var t46: i64 = 1 752 if s7m46 != 0 { t46 = 0 } 753 if s7p[1] != 200 { t46 = 0 } 754 if s7p[2] != 100 { t46 = 0 } 755 if s7p[0] != (0 - 1) { t46 = 0 } 756 if s7p[3] != (0 - 1) { t46 = 0 } 757 g_puts("T46 neg-control pair-swap-tie-and-disqualified: moved="); g_num(s7m46); g_puts(" scores="); g_num(s7p[0]); g_puts(","); g_num(s7p[1]); g_puts(","); g_num(s7p[2]); g_puts(","); g_num(s7p[3]) 758 g_puts("\n") 759 gv_check("T46-neg-control-pair-swap-tie-and-disqualified", t46, ctr) 760 // T47 a disqualified candidate beside a 3-cycle: the -1 stays where it is, the three participants move as in T45 761 s7p[0] = 0 - 1; s7p[1] = 300; s7p[2] = 200; s7p[3] = 100 762 s7q[0] = 0 - 1; s7q[1] = 100; s7q[2] = 300; s7q[3] = 200 763 let s7m47: i64 = dss_bm25q_fuse(s7p, s7q, 4) 764 var t47: i64 = 1 765 if s7m47 != 2 { t47 = 0 } 766 if s7p[0] != (0 - 1) { t47 = 0 } 767 if s7p[2] != 300 { t47 = 0 } 768 if s7p[1] != 200 { t47 = 0 } 769 if s7p[3] != 100 { t47 = 0 } 770 g_puts("T47 fuse-3cycle-with-disqualified: moved="); g_num(s7m47); g_puts(" scores="); g_num(s7p[0]); g_puts(","); g_num(s7p[1]); g_puts(","); g_num(s7p[2]); g_puts(","); g_num(s7p[3]) 771 g_puts("\n") 772 gv_check("T47-fuse-3cycle-with-disqualified", t47, ctr) 773 // T48 THE WIRE, identity leg: a no-repeat multi-term query through dss_search announces repeats=0 moved=0 774 let s7bq: *i64 = sys_mmap(16) as *i64 775 let q48: *u8 = "divorce custody" as *u8 776 let n48: i64 = dss_search(dom, q48, g_strlen(q48), cids, scores, 10) 777 dss_bm25q_stats(s7bq) 778 var t48: i64 = 1 779 if n48 < 1 { t48 = 0 } 780 if s7bq[0] != 0 { t48 = 0 } 781 if s7bq[1] != 0 { t48 = 0 } 782 g_puts("T48 wire-identity divorce custody: n="); g_num(n48); g_puts(" repeats="); g_num(s7bq[0]); g_puts(" moved="); g_num(s7bq[1]) 783 g_puts("\n") 784 gv_check("T48-wire-identity-divorce-custody", t48, ctr) 785 // T49 THE WIRE, fusion leg (fixture 970-975, see the writer): "zqxrep zqxrep zqzrare" repeats zqxrep, so the two 786 // arms disagree beyond a pair swap and the fusion MOVES candidates -- the fixture reached the condition when 787 // repeats=1, and the tooth demands moved>=1 and 971 (the short rare-term doc) on top. The scores printed are the 788 // served ones (after coordination), the moved count is taken before it. 789 let q49: *u8 = "zqxrep zqxrep zqzrare" as *u8 790 let dom2: *u8 = "ssgatefuse" as *u8 791 let n49: i64 = dss_search(dom2, q49, g_strlen(q49), cids, scores, 10) 792 dss_bm25q_stats(s7bq) 793 var t49: i64 = 1 794 if n49 != 4 { t49 = 0 } 795 if s7bq[0] != 1 { t49 = 0 } 796 if s7bq[1] < 1 { t49 = 0 } 797 if n49 > 0 { if cids[0] != 971 { t49 = 0 } } 798 g_puts("T49 wire-fusion zqxrep zqxrep zqzrare: bytes 970/971/972/973="); g_num(s7l970); g_puts("/"); g_num(s7l971); g_puts("/"); g_num(s7l972); g_puts("/"); g_num(s7l973); g_puts(" n="); g_num(n49); g_puts(" repeats="); g_num(s7bq[0]); g_puts(" moved="); g_num(s7bq[1]); g_puts(" order="); g_num(cids[0]); g_puts(","); g_num(cids[1]); g_puts(","); g_num(cids[2]); g_puts(" served="); g_num(scores[0]); g_puts(","); g_num(scores[1]); g_puts(","); g_num(scores[2]) 799 g_puts("\n") 800 gv_check("T49-wire-fusion-zqxrep-zqxrep-zqzrare", t49, ctr) 801 802 // T50-T52 S9 ENTITY DISCRIMINATORS (2026-09-17): the two-word name over the ssgatetest shard, webdiv=1. 803 // Old code (score order under the 0.5x results-page multiplier): 852 (rare word x16) first, 850 second, 851 804 // last. New code: (coverage, content-before-results-page, ex) tiers -> 850, 851, 852; 851 precedes 852 with a 805 // LOWER score, which no score-ordered selection can produce (T50b), and webdiv=0 keeps the pure score order 806 // with the results page on top (T52) -- the discriminators live on the web path only. 807 let q50: *u8 = "zqnamea zqnameb" as *u8 808 let td5: *i64 = sys_mmap(S9G_TOTAL_SLOTS * DSS_I64_BYTES) as *i64 809 let t50n: i64 = dss_search_off_div(dom, q50, g_strlen(q50), cids, scores, S9G_PAGE, 0, td5, 1) 810 var t50: i64 = 0 811 if t50n == S9G_PAGE { if td5[0] == S9G_MATCHING { if cids[0] == S9G_DOC_CONTENT { if cids[1] == S9G_DOC_RESULTS { if cids[S9G_RANK_THIRD] == S9G_DOC_SCATTER { t50 = 1 } } } } } 812 g_puts("T50 S9 name query webdiv=1: content-both, results-page-both, rare-word-only"); g_puts(" n="); g_num(t50n); g_puts(" total="); g_num(td5[0]) 813 g_puts(" cids="); g_num(cids[0]); g_puts(","); g_num(cids[1]); g_puts(","); g_num(cids[S9G_RANK_THIRD]); g_puts(" scores="); g_num(scores[0]); g_puts(","); g_num(scores[1]); g_puts(","); g_num(scores[S9G_RANK_THIRD]); g_puts("\n") 814 gv_check("T50-S9-adjacent-content-before-adjacent-results-page-before-scattered-content", t50, ctr) 815 var t53: i64 = 0 816 if t50n == S9G_PAGE { if cids[S9G_RANK_FOURTH] == S9G_DOC_RARE { t53 = 1 } } 817 g_puts("T53 S9b scattered both-words page ranks above the rare-word-only page but below the adjacent name"); g_puts(" cids[3]="); g_num(cids[S9G_RANK_FOURTH]); g_puts("\n") 818 gv_check("T53-S9b-scattered-both-words-above-rare-word-only-below-the-adjacent-name", t53, ctr) 819 // ---- S11 ENTITY PIN: a resolved canonical page takes the band above every S9 tier; no row = unchanged; a pin 820 // whose page is not a candidate changes nothing and announces the miss ---- 821 let pinpath: *u8 = "/tmp/nx_docportal_search_seg_gate_entitypin.tsv" as *u8 822 s9g_write_file(pinpath, "zqnamea zqnameb\t853\thttps://content.example.com/notes/zq\tzq\t1\t0\n" as *u8) 823 dss_ent_set_path(pinpath) 824 let t54n: i64 = dss_search_off_div(dom, q50, g_strlen(q50), cids, scores, S9G_PAGE, 0, td5, 1) 825 let ens: *i64 = sys_mmap(S9G_ENT_SLOTS * DSS_I64_BYTES) as *i64 826 dss_entity_stats(ens) 827 var t54: i64 = 0 828 if t54n == S9G_PAGE { if cids[0] == S9G_DOC_SCATTER { if cids[1] == S9G_DOC_CONTENT { if ens[0] == 1 { if ens[1] == S9G_DOC_SCATTER { t54 = 1 } } } } } 829 g_puts("T54 S11 pinned canonical page (853) tops the S9 order"); g_puts(" cids[0]="); g_num(cids[0]); g_puts(" cids[1]="); g_num(cids[1]); g_puts(" pinned="); g_num(ens[0]); g_puts(" cid="); g_num(ens[1]); g_puts(" rows="); g_num(ens[2]); g_puts("\n") 830 gv_check("T54-S11-pinned-canonical-page-tops-the-S9-order-and-announces-pinned=1", t54, ctr) 831 // T54d S11-b: the card fields come from the matched row (jp, url, titles) and only while the pin fired 832 let cjp: *u8 = sys_mmap(S9G_BUF_4K) 833 let cjn: i64 = dss_entity_field(DSS_ENT_FIELD_JP, cjp, S9G_BUF_4K) 834 let curl: *u8 = sys_mmap(S9G_BUF_4K) 835 let cun: i64 = dss_entity_field(DSS_ENT_FIELD_URL, curl, S9G_BUF_4K) 836 var t54d: i64 = 0 837 if cjn == 2 { if cjp[0] as i64 == S9G_CH_Z { if cjp[1] as i64 == S9G_CH_Q { if cun > 0 { if dss_entity_titles() == 1 { t54d = 1 } } } } } 838 g_puts("T54d S11-b card fields jp="); g_puts(cjp); g_puts(" url="); g_puts(curl); g_puts(" titles="); g_num(dss_entity_titles()); g_puts("\n") 839 gv_check("T54d-S11b-card-fields-jp-url-titles-come-from-the-matched-row", t54d, ctr) 840 // T54g S11-c: the memo's two words carry the pin across a reset (what a memoised answer restores), and the card row 841 // is re-found by cid after the restore 842 let mw: *i64 = sys_mmap(DSQ_ENT_W * DSS_I64_BYTES) as *i64 843 dss_ent_memo_save(mw) 844 dss_ent_set(0, 0) 845 dss_entity_stats(ens) 846 var t54g_reset: i64 = 0 847 if ens[0] == 0 { if dss_entity_field(DSS_ENT_FIELD_JP, cjp, S9G_BUF_4K) == 0 { t54g_reset = 1 } } 848 gv_check("neg-control-T54g0-after-the-reset-the-announce-and-the-card-read-nothing", t54g_reset, ctr) 849 dss_ent_memo_load(mw) 850 dss_entity_stats(ens) 851 let mjn: i64 = dss_entity_field(DSS_ENT_FIELD_JP, cjp, S9G_BUF_4K) 852 var t54g: i64 = 0 853 if mw[0] == 1 { if mw[1] == S9G_DOC_SCATTER { if ens[0] == 1 { if ens[1] == S9G_DOC_SCATTER { if mjn == 2 { if dss_entity_titles() == 1 { t54g = 1 } } } } } } 854 g_puts("T54g memo words pinned="); g_num(mw[0]); g_puts(" cid="); g_num(mw[1]); g_puts(" restored pinned="); g_num(ens[0]); g_puts(" jp="); g_puts(cjp); g_puts("\n") 855 gv_check("T54g-S11c-the-memo-words-restore-the-pin-and-the-card-row-is-re-found-by-cid", t54g, ctr) 856 // ---- S12 FRESHNESS: the fetch-epoch row round-trips beside the doc, an absent row abstains, the page arithmetic holds ---- 857 let hfe: *i64 = ss_open(prefix) 858 gv_check("T55-S12-the-fixture-shard-opens-for-the-fetch-epoch-read", ((hfe as i64) != 0) as i64, ctr) 859 let fk: *u8 = sys_mmap(DSS_FE_KEY_BYTES) 860 dss_mkfekey(101, fk) 861 var t55k: i64 = 0 862 if fk[0] == (102 as u8) { if fk[1] == (101 as u8) { if fk[2] == (58 as u8) { if fk[3] == (49 as u8) { if fk[4] == (48 as u8) { if fk[5] == (49 as u8) { if fk[6] == (0 as u8) { t55k = 1 } } } } } } } 863 gv_check("T55a-the-fetch-epoch-key-is-fe-colon-cid-null-terminated", t55k, ctr) 864 gv_check_eq("T55b-a-planted-fetch-epoch-reads-back-exactly", dss_doc_fetch_epoch(hfe, 101), S9G_FE_101, ctr) 865 gv_check_eq("T55c-the-second-planted-epoch-reads-back-exactly", dss_doc_fetch_epoch(hfe, 102), S9G_FE_102, ctr) 866 gv_check_eq("neg-control-T55d-a-doc-without-the-row-abstains-as-minus-one-never-zero", dss_doc_fetch_epoch(hfe, 103), 0 - 1, ctr) 867 gv_check_eq("neg-control-T55e-an-absent-doc-abstains-too", dss_doc_fetch_epoch(hfe, S9G_DOC_ABSENT), 0 - 1, ctr) 868 let fea: *i64 = sys_mmap(8 * 8) as *i64 869 let fst: *i64 = sys_mmap(8 * DSS_FRESH_SLOTS) as *i64 870 fea[0] = S9G_FE_101; fea[1] = S9G_FE_102; fea[2] = 0 - 1 871 dss_fresh_stats(fea, 3, S9G_FE_NOW, fst) 872 gv_check_eq("T55f-observed-counts-only-the-results-with-an-epoch", fst[0], 2, ctr) 873 gv_check_eq("T55g-newest-epoch-is-the-max-over-the-observed", fst[1], S9G_FE_102, ctr) 874 gv_check_eq("T55h-even-count-median-age-is-the-mean-of-the-two-middle-ages", fst[2], ((S9G_FE_NOW - S9G_FE_101) + (S9G_FE_NOW - S9G_FE_102)) / 2, ctr) 875 fea[2] = S9G_FE_NOW - S9G_FE_YOUNG 876 dss_fresh_stats(fea, 3, S9G_FE_NOW, fst) 877 gv_check_eq("T55i-odd-count-median-age-is-the-middle-age", fst[2], S9G_FE_NOW - S9G_FE_102, ctr) 878 gv_check_eq("T55j-observed-rises-with-the-third-epoch", fst[0], 3, ctr) 879 fea[0] = 0 - 1; fea[1] = 0 - 1 880 dss_fresh_stats(fea, 2, S9G_FE_NOW, fst) 881 var t55l: i64 = 0 882 if fst[0] == 0 { if fst[1] == (0 - 1) { if fst[2] == (0 - 1) { t55l = 1 } } } 883 gv_check("neg-control-T55l-a-page-with-no-observed-epoch-abstains-on-every-field", t55l, ctr) 884 fea[0] = S9G_FE_NOW + S9G_FE_YOUNG 885 dss_fresh_stats(fea, 1, S9G_FE_NOW, fst) 886 gv_check_eq("T55m-a-fetch-epoch-ahead-of-the-clock-reads-as-age-zero-not-negative", fst[2], 0, ctr) 887 s9g_write_file(pinpath, "someone else\t853\thttps://content.example.com/notes/zq\tzq\t1\t0\n" as *u8) 888 dss_ent_set_path(pinpath) 889 let t54bn: i64 = dss_search_off_div(dom, q50, g_strlen(q50), cids, scores, S9G_PAGE, 0, td5, 1) 890 dss_entity_stats(ens) 891 var t54b: i64 = 0 892 if t54bn == S9G_PAGE { if cids[0] == S9G_DOC_CONTENT { if ens[0] == 0 { if ens[1] == 0 { t54b = 1 } } } } 893 g_puts("T54b a row for another query: order unchanged"); g_puts(" cids[0]="); g_num(cids[0]); g_puts(" pinned="); g_num(ens[0]); g_puts(" cid="); g_num(ens[1]); g_puts("\n") 894 gv_check("neg-control-T54b-a-row-for-another-query-leaves-the-S9-order-and-announces-no-pin", t54b, ctr) 895 gv_check("neg-control-T54e-no-pin-no-card-fields", (dss_entity_field(DSS_ENT_FIELD_JP, cjp, S9G_BUF_4K) == 0) as i64, ctr) 896 s9g_write_file(pinpath, "zqnamea zqnameb\t999\thttps://nowhere.example.com/\tzz\t1\t0\n" as *u8) 897 dss_ent_set_path(pinpath) 898 let t54cn: i64 = dss_search_off_div(dom, q50, g_strlen(q50), cids, scores, S9G_PAGE, 0, td5, 1) 899 dss_entity_stats(ens) 900 var t54c: i64 = 0 901 if t54cn == S9G_PAGE { if cids[0] == S9G_DOC_CONTENT { if ens[0] == 0 { if ens[1] == S9G_DOC_ABSENT { t54c = 1 } } } } 902 g_puts("T54c a pin whose page is not a candidate: order unchanged, miss announced"); g_puts(" cids[0]="); g_num(cids[0]); g_puts(" pinned="); g_num(ens[0]); g_puts(" cid="); g_num(ens[1]); g_puts("\n") 903 gv_check("T54c-a-pin-whose-page-is-not-a-candidate-changes-nothing-and-announces-the-miss", t54c, ctr) 904 dss_ent_set_path(DSS_ENT_PATH_DEFAULT) 905 var t50b: i64 = 0 906 if t50n >= S9G_TOP3 { if scores[0] < scores[1] { t50b = 1 } } 907 gv_check("T50b-neg-control-tier-overrode-a-higher-score-(the-first-result-scores-below-the-second)", t50b, ctr) 908 let t52n: i64 = dss_search_off_div(dom, q50, g_strlen(q50), cids, scores, S9G_PAGE, 0, td5, 0) 909 var t52: i64 = 0 910 if t52n == S9G_PAGE { if cids[0] == S9G_DOC_RESULTS { t52 = 1 } } 911 g_puts("T52 S9 webdiv=0 control: pure score order, the results page tops"); g_puts(" cids[0]="); g_num(cids[0]); g_puts("\n") 912 gv_check("T52-S9-webdiv0-control-results-page-tops-on-score-alone", t52, ctr) 913 914 // EVIDENCE (2026-07-30): this gate is the SEARCH lane's only NAS-runnable witness -- 36 teeth over 915 // BM25 ranking, proximity, title-field boost and Cyrillic/CJK case-folding -- and it wrote its 916 // verdict to STDOUT ONLY, so nx_gatereg_derive could not see the lane and the maturity rollup could 917 // not grade it. Same defect that hid lab-science and the browser renderer. 918 // ★It is SAFE to publish: every tooth runs against SYNTHETIC fixture docs (800/801/900/901/950-952), 919 // never sampled production content, so no dp-*-secret / dp-*-prv material can reach this log. 920 // ★Plain verdict=GREEN|RED -- gv_is_pass accepts GREEN|PASS|VALID only, and this ecosystem has 921 // already invented four different pass-words. Do not add a fifth. 922 let lfd: i64 = sys_openat_append("knowledge/status/search_seg_gate.log\x00" as *u8, 420) 923 if lfd >= 0 { 924 let lb: *u8 = sys_mmap(256) 925 var lo: i64 = 0 926 lo = dsg_cat(lb, lo, "DSS-GATE rows=" as *u8) 927 lo = dsg_catn(lb, lo, ctr[1]) 928 lo = dsg_cat(lb, lo, " passed=" as *u8) 929 lo = dsg_catn(lb, lo, ctr[0]) 930 if ctr[0] == ctr[1] { lo = dsg_cat(lb, lo, " verdict=GREEN\n" as *u8) } else { lo = dsg_cat(lb, lo, " verdict=RED\n" as *u8) } 931 sys_write(lfd, lb, lo) 932 sys_close(lfd) 933 } 934 935 // D001 migration 2026-09-16: the exit code carries the verdict (gv_verdict, positional last line), the log above keeps its shape 936 return gv_verdict("nx_docportal_search_seg_gate", ctr, "sovereign seg_store search end to end over a synthetic fixture shard (dp-ssgatetest-pub-), the S7 served BM25Q fusion wired 2026-09-16") 937}