nx_entity_dossier.nx source
↩ module page · 579 lines · 30280 B
1// nx_entity_dossier.nx -- ENTITY DOSSIER: a superior-to-Google entity landing page, composed 100% from the LIVE
2// sovereign corpus (NO external fetch -> fast, robust, content-agnostic: lawful content is never filtered/blurred).
3// nx_entity_dossier <entity name>
4// Pipeline (all over loopback 127.0.0.1:18456, the docportal search daemon):
5// 1. RESOLVE -- GET /api/search?scope=web&q=<name> ; pick the canonical landing (first result whose url is an
6// /idols/ profile, else the top hit) + read the matched-doc total (coverage denominator side A).
7// 2. FETCH -- GET /api/doc?cid=<landing> ; the landing page's full indexed text.
8// 3. EXTRACT -- CARD (jp-name/age/dob/birthplace/measurements/cup/height/hair/tags/bio = the knowledge panel),
9// PORTFOLIO (authority work-count + a de-duped sample of work CODES = the oeuvre), RELATED
10// entities (the M:N graph edges), and COVERAGE honesty (indexed-matched vs authority-total ->
11// the deep-crawl gap the operator flagged: "less than hundreds of results is below toy grade").
12// Emits ONE JSON object to stdout (agent/workflow/MCP-shaped) AND knowledge/status/dossier-<slug>.txt (durable,
13// survives an edge-dropped tools/call). Fixed loopback host/port + url-encoded query => no SSRF/injection (same
14// posture as nx_websearch). Graceful degradation: any missing field -> "" (rule 14). license_tier: ORIGINAL
15import "nx_syscalls.nx"
16
17const ED_PORT: i64 = 18456
18const ED_CAP: i64 = 1048576 // 1 MiB per-response cap (idol doc ~6KB, search page ~30KB: ample)
19const ED_MAXCODES: i64 = 40 // oeuvre sample cap (the authority_total carries the true scale)
20
21static ed_buf: *u8 // output JSON accumulator
22static ed_bo: i64
23
24func ed_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
25func ed_out(s: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { ed_buf[ed_bo] = s[i]; ed_bo = ed_bo + 1; i = i + 1 } return 0 }
26func ed_puts(s: *u8) -> i64 { ed_out(s, ed_slen(s)); return 0 }
27func ed_num(v: i64) -> i64 {
28 var m: i64 = v
29 if m < 0 { ed_out("-" as *u8, 1); m = 0 - m }
30 let t: *u8 = sys_mmap(28); var k: i64 = 0
31 if m == 0 { t[0] = 48 as u8; k = 1 }
32 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
33 let b: *u8 = sys_mmap(28); var i: i64 = 0
34 while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
35 ed_out(b, k); return 0
36}
37// JSON-escape src[0..n) straight into the output buffer (" \ escaped; controls -> space).
38func ed_jout(src: *u8, n: i64) -> i64 {
39 var i: i64 = 0
40 while i < n {
41 let c: i64 = src[i] as i64; var done: i64 = 0
42 if c == 34 { ed_buf[ed_bo]=92 as u8; ed_bo=ed_bo+1; ed_buf[ed_bo]=34 as u8; ed_bo=ed_bo+1; done=1 }
43 if done == 0 { if c == 92 { ed_buf[ed_bo]=92 as u8; ed_bo=ed_bo+1; ed_buf[ed_bo]=92 as u8; ed_bo=ed_bo+1; done=1 } }
44 if done == 0 { if c < 32 { ed_buf[ed_bo]=32 as u8; ed_bo=ed_bo+1; done=1 } }
45 if done == 0 { ed_buf[ed_bo]=c as u8; ed_bo=ed_bo+1 }
46 i = i + 1
47 }
48 return 0
49}
50func ed_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { d[o + i] = s[i]; i = i + 1 } return o + i }
51func ed_catb(d: *u8, o: i64, s: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { d[o + i] = s[i]; i = i + 1 } return o + n }
52func ed_hex(v: i64) -> i64 { if v < 10 { return 48 + v } return 55 + v }
53func ed_urlenc(src: *u8, slen: i64, dst: *u8) -> i64 {
54 var o: i64 = 0; var i: i64 = 0
55 while i < slen {
56 let c: i64 = src[i] as i64; var keep: i64 = 0
57 if c >= 48 { if c <= 57 { keep = 1 } }
58 if c >= 65 { if c <= 90 { keep = 1 } }
59 if c >= 97 { if c <= 122 { keep = 1 } }
60 if c == 45 { keep = 1 } if c == 46 { keep = 1 } if c == 95 { keep = 1 } if c == 126 { keep = 1 }
61 if keep == 1 { dst[o] = c as u8; o = o + 1 } else {
62 dst[o] = 37 as u8; o = o + 1
63 dst[o] = ed_hex((c >> 4) & 15) as u8; o = o + 1
64 dst[o] = ed_hex(c & 15) as u8; o = o + 1
65 }
66 i = i + 1
67 }
68 return o
69}
70// first index of ndl in hay[from..hn), else -1.
71func ed_find(hay: *u8, hn: i64, ndl: *u8, from: i64) -> i64 {
72 let nl: i64 = ed_slen(ndl); if nl == 0 { return 0 - 1 }
73 var i: i64 = from
74 while i + nl <= hn {
75 var m: i64 = 1; var j: i64 = 0
76 while j < nl { if hay[i + j] != ndl[j] { m = 0; j = nl } else { j = j + 1 } }
77 if m == 1 { return i }
78 i = i + 1
79 }
80 return 0 - 1
81}
82// index of next '"' at/after s (else n).
83func ed_qend(buf: *u8, s: i64, n: i64) -> i64 { var i: i64 = s; while i < n { if buf[i]==(34 as u8) { return i } i = i + 1 } return n }
84// is ndl a substring of buf[s..e)? 1/0.
85func ed_sub(buf: *u8, s: i64, e: i64, ndl: *u8) -> i64 {
86 let nl: i64 = ed_slen(ndl); if nl == 0 { return 0 }
87 var i: i64 = s
88 while i + nl <= e {
89 var m: i64 = 1; var j: i64 = 0
90 while j < nl { if buf[i + j] != ndl[j] { m = 0; j = nl } else { j = j + 1 } }
91 if m == 1 { return 1 }
92 i = i + 1
93 }
94 return 0
95}
96// unsigned int parsed immediately after label (label INCLUDES any trailing '('); -1 if label absent / no digit.
97func ed_uint_after(hay: *u8, hn: i64, label: *u8) -> i64 {
98 let p0: i64 = ed_find(hay, hn, label, 0); if p0 < 0 { return 0 - 1 }
99 var p: i64 = p0 + ed_slen(label); var v: i64 = 0; var any: i64 = 0; var go: i64 = 1
100 while go == 1 {
101 if p >= hn { go = 0 } else {
102 let c: i64 = hay[p] as i64; var d: i64 = 0
103 if c >= 48 { if c <= 57 { d = 1 } }
104 if d == 1 { v = v * 10 + (c - 48); any = 1; p = p + 1 } else { go = 0 }
105 }
106 }
107 if any == 0 { return 0 - 1 }
108 return v
109}
110// single-line field: value after label up to a delimiter (backslash=\n / '"' ; and " - " when stopdash=1). trimmed.
111func ed_field(hay: *u8, hn: i64, label: *u8, dst: *u8, cap: i64, stopdash: i64) -> i64 {
112 let p0: i64 = ed_find(hay, hn, label, 0); if p0 < 0 { return 0 }
113 var p: i64 = p0 + ed_slen(label); var o: i64 = 0; var go: i64 = 1
114 while go == 1 {
115 if p >= hn { go = 0 } else {
116 if o >= cap { go = 0 } else {
117 let c: i64 = hay[p] as i64; var stop: i64 = 0
118 if c == 92 { stop = 1 }
119 if c == 34 { stop = 1 }
120 if stopdash == 1 { if c == 32 { if p + 2 < hn { if hay[p+1]==(45 as u8) { if hay[p+2]==(32 as u8) { stop = 1 } } } } }
121 if stop == 1 { go = 0 } else { dst[o] = c as u8; o = o + 1; p = p + 1 }
122 } }
123 }
124 var s0: i64 = 0; var ls: i64 = 1
125 while ls == 1 { if s0 < o { if dst[s0]==(32 as u8) { s0 = s0 + 1 } else { ls = 0 } } else { ls = 0 } }
126 if s0 > 0 { var k: i64 = 0; while s0 + k < o { dst[k] = dst[s0 + k]; k = k + 1 } o = o - s0 }
127 var t: i64 = o; var tr: i64 = 1
128 while tr == 1 { if t > 0 { if dst[t-1]==(32 as u8) { t = t - 1 } else { tr = 0 } } else { tr = 0 } }
129 return t
130}
131// block field: text between label and stopw, backslash-escapes collapsed to a single space, trimmed both ends.
132func ed_block(hay: *u8, hn: i64, label: *u8, stopw: *u8, dst: *u8, cap: i64) -> i64 {
133 let p0: i64 = ed_find(hay, hn, label, 0); if p0 < 0 { return 0 }
134 var p: i64 = p0 + ed_slen(label); let swl: i64 = ed_slen(stopw); var o: i64 = 0; var go: i64 = 1
135 while go == 1 {
136 if p >= hn { go = 0 } else {
137 if o >= cap { go = 0 } else {
138 var hit: i64 = 0
139 if swl > 0 { if p + swl <= hn { var m: i64 = 1; var j: i64 = 0
140 while j < swl { if hay[p+j] != stopw[j] { m = 0; j = swl } else { j = j + 1 } }
141 if m == 1 { hit = 1 } } }
142 if hit == 1 { go = 0 } else {
143 let c: i64 = hay[p] as i64
144 if c == 92 { p = p + 2; if o > 0 { if dst[o-1] != (32 as u8) { dst[o] = 32 as u8; o = o + 1 } } }
145 else { if c < 32 { p = p + 1 } else { dst[o] = c as u8; o = o + 1; p = p + 1 } }
146 }
147 } }
148 }
149 var s0: i64 = 0; var ls: i64 = 1
150 while ls == 1 { if s0 < o { if dst[s0]==(32 as u8) { s0 = s0 + 1 } else { ls = 0 } } else { ls = 0 } }
151 if s0 > 0 { var k: i64 = 0; while s0 + k < o { dst[k] = dst[s0 + k]; k = k + 1 } o = o - s0 }
152 var t: i64 = o; var tr: i64 = 1
153 while tr == 1 { if t > 0 { if dst[t-1]==(32 as u8) { t = t - 1 } else { tr = 0 } } else { tr = 0 } }
154 return t
155}
156// dedupe-add a work code (<=14 bytes) into a 16-byte-slot table. returns 1 if added, 0 if dup/full.
157func ed_addcode(codes: *u8, nc: *i64, tok: *u8, tl: i64) -> i64 {
158 if tl > 14 { return 0 }
159 let n: i64 = nc[0]; if n >= ED_MAXCODES { return 0 }
160 var k: i64 = 0
161 while k < n {
162 let slot: *u8 = ((codes as i64) + k * 16) as *u8
163 if (slot[0] as i64) == tl { var m: i64 = 1; var j: i64 = 0
164 while j < tl { if slot[1+j] != tok[j] { m = 0; j = tl } else { j = j + 1 } }
165 if m == 1 { return 0 } }
166 k = k + 1
167 }
168 let slot2: *u8 = ((codes as i64) + n * 16) as *u8
169 slot2[0] = tl as u8; var j: i64 = 0; while j < tl { slot2[1+j] = tok[j]; j = j + 1 }
170 nc[0] = n + 1; return 1
171}
172// is c an ASCII letter or digit?
173func ed_alnum(c: i64) -> i64 {
174 if c >= 48 { if c <= 57 { return 1 } }
175 if c >= 65 { if c <= 90 { return 1 } }
176 if c >= 97 { if c <= 122 { return 1 } }
177 return 0
178}
179// scan hay for product codes [A-Z]{2..6}-[0-9]{2..5} on word boundaries -> codes table.
180func ed_scan_codes(hay: *u8, hn: i64, codes: *u8, nc: *i64) -> i64 {
181 var i: i64 = 0
182 while i < hn {
183 var adv: i64 = 1
184 var bound: i64 = 1
185 if i > 0 { if ed_alnum(hay[i-1] as i64) == 1 { bound = 0 } }
186 if bound == 1 {
187 var ul: i64 = 0; var scan: i64 = 1
188 while scan == 1 {
189 if i + ul < hn { let c: i64 = hay[i+ul] as i64; var isU: i64 = 0
190 if c >= 65 { if c <= 90 { isU = 1 } }
191 if isU == 1 { ul = ul + 1 } else { scan = 0 }
192 } else { scan = 0 }
193 }
194 if ul >= 2 { if ul <= 6 { if i + ul < hn { if hay[i+ul]==(45 as u8) {
195 var dl: i64 = 0; var ds: i64 = 1
196 while ds == 1 {
197 let p: i64 = i + ul + 1 + dl
198 if p < hn { let c2: i64 = hay[p] as i64; var isD: i64 = 0
199 if c2 >= 48 { if c2 <= 57 { isD = 1 } }
200 if isD == 1 { dl = dl + 1 } else { ds = 0 }
201 } else { ds = 0 }
202 }
203 if dl >= 2 { if dl <= 5 {
204 let tl: i64 = ul + 1 + dl
205 ed_addcode(codes, nc, ((hay as i64) + i) as *u8, tl)
206 adv = tl
207 } }
208 } } } }
209 }
210 i = i + adv
211 }
212 return 0
213}
214// host between "://" and next '/' of url[0..ulen) -> dst; returns length.
215func ed_host(url: *u8, ulen: i64, dst: *u8) -> i64 {
216 let sp: i64 = ed_find(url, ulen, "://" as *u8, 0); var s: i64 = 0; if sp >= 0 { s = sp + 3 }
217 var o: i64 = 0; var go: i64 = 1
218 while go == 1 { if s >= ulen { go = 0 } else { if url[s]==(47 as u8) { go = 0 } else { dst[o] = url[s]; o = o + 1; s = s + 1 } } }
219 return o
220}
221// slug: lowercase; [a-z0-9] kept, else '-'.
222func ed_slug(name: *u8, nn: i64, dst: *u8) -> i64 {
223 var o: i64 = 0; var i: i64 = 0
224 while i < nn { let c: i64 = name[i] as i64; var oc: i64 = 45
225 if c >= 65 { if c <= 90 { oc = c + 32 } }
226 if c >= 97 { if c <= 122 { oc = c } }
227 if c >= 48 { if c <= 57 { oc = c } }
228 dst[o] = oc as u8; o = o + 1; i = i + 1 }
229 return o
230}
231func ed_loopback_sa(sa: *u8, port: i64) -> i64 {
232 sa[0]=2 as u8; sa[1]=0 as u8; sa[2]=((port/256)&255) as u8; sa[3]=(port&255) as u8
233 sa[4]=127 as u8; sa[5]=0 as u8; sa[6]=0 as u8; sa[7]=1 as u8
234 var i: i64 = 8; while i < 16 { sa[i]=0 as u8; i = i + 1 }
235 return 0
236}
237// GET <path> over loopback:18456 -> body copied to resp[0..], returns body length (0 on failure).
238func ed_fetch(path: *u8, pathlen: i64, resp: *u8) -> i64 {
239 let req: *u8 = sys_mmap(pathlen + 128)
240 var o: i64 = ed_cat(req, 0, "GET " as *u8)
241 o = ed_catb(req, o, path, pathlen)
242 o = ed_cat(req, o, " HTTP/1.0\r\nHost: nishifamily.com\r\nConnection: close\r\n\r\n" as *u8)
243 let fd: i64 = sys_socket(2, 1, 0); if fd < 0 { return 0 }
244 let sa: *u8 = sys_mmap(16); ed_loopback_sa(sa, ED_PORT)
245 if sys_connect(fd, sa, 16) < 0 { sys_close(fd); return 0 }
246 var w: i64 = 0
247 while w < o { let x: i64 = sys_write(fd, ((req as i64)+w) as *u8, o-w); if x <= 0 { w = o } else { w = w + x } }
248 var tot: i64 = 0; var go: i64 = 1
249 while go == 1 { if tot >= ED_CAP { go = 0 } else { let r: i64 = sys_read(fd, ((resp as i64)+tot) as *u8, ED_CAP-tot); if r <= 0 { go = 0 } else { tot = tot + r } } }
250 sys_close(fd)
251 var bo: i64 = 0 - 1; var j: i64 = 0
252 while j + 3 < tot { if resp[j]==(13 as u8) { if resp[j+1]==(10 as u8) { if resp[j+2]==(13 as u8) { if resp[j+3]==(10 as u8) { bo = j + 4; j = tot } } } } j = j + 1 }
253 if bo < 0 { return 0 }
254 var k: i64 = 0
255 while bo + k < tot { resp[k] = resp[bo + k]; k = k + 1 }
256 return k
257}
258
259// does url slice [s..e) look like a SEARCH page? (mirror of dss_is_search_url in the ranking core)
260func ed_is_search(buf: *u8, s: i64, e: i64) -> i64 {
261 if ed_sub(buf, s, e, "/search" as *u8) == 1 { return 1 }
262 if ed_sub(buf, s, e, "?q=" as *u8) == 1 { return 1 }
263 if ed_sub(buf, s, e, "&q=" as *u8) == 1 { return 1 }
264 if ed_sub(buf, s, e, "?s=" as *u8) == 1 { return 1 }
265 if ed_sub(buf, s, e, "&s=" as *u8) == 1 { return 1 }
266 if ed_sub(buf, s, e, "?query=" as *u8) == 1 { return 1 }
267 if ed_sub(buf, s, e, "?search=" as *u8) == 1 { return 1 }
268 return 0
269}
270// A CANONICAL LANDING IS A STABLE PATH, NEVER A FACETED QUERY (2026-08-15). MEASURED: the dossier for a
271// real named person returned found:true with canonical_url=".../idols/?_body_type=<facet>" -- a LISTING
272// page -- because the profile test was a bare "/idols/" substring and ed_is_search only knows seven
273// search-shaped keys; ANY other facet key sails through and is then promoted to the STRONGEST verdict.
274// DELIBERATELY NOT a blanket '?' reject: that is the R8 mistake (graphis.ne.jp galleries ARE model.php?ID=,
275// and blanket-rejecting queries made a whole vertical unreachable). The query bar applies ONLY to the
276// canonical-PROFILE lane; the weaker first-result lane is untouched.
277// ---- VERTICAL PRIORS AS DATA (2026-08-15) ----------------------------------------------------
278// The resolve query appended a HARDCODED "idols profile" to every entity name and the profile test
279// hardcoded "/idols/": one vertical's domain prior baked into code. Rows are DATA now; row 0 is the
280// default and reproduces the 2026-07-24 behaviour exactly, so an absent conf changes nothing.
281// A field too long for its slot REFUSES ITS ROW rather than truncating -- a silently shortened prefix
282// would match the wrong urls, which is worse than one vertical being absent and countable.
283const ED_VERT_CONF: *u8 = "knowledge/entity_verticals.conf"
284const ED_VERT_MAX: i64 = 8
285const ED_VSLOT: i64 = 96
286static ed_vid_g: *u8
287static ed_vprior_g: *u8
288static ed_vpfx_g: *u8
289static ed_nvert_g: i64
290static ed_vloaded_g: i64
291static ed_vactive_g: i64
292func ed_vid(i: i64) -> *u8 { return ((ed_vid_g as i64) + i * ED_VSLOT) as *u8 }
293func ed_vprior(i: i64) -> *u8 { return ((ed_vprior_g as i64) + i * ED_VSLOT) as *u8 }
294func ed_vpfx(i: i64) -> *u8 { return ((ed_vpfx_g as i64) + i * ED_VSLOT) as *u8 }
295// -1 = does not fit (caller drops the row); else the length written, NUL-terminated
296func ed_vcopy(dst: *u8, b: *u8, s: i64, e: i64) -> i64 {
297 let n: i64 = e - s
298 if n < 0 { return 0 - 1 }
299 if n >= ED_VSLOT - 1 { return 0 - 1 }
300 var o: i64 = 0
301 while o < n { dst[o] = b[s + o]; o = o + 1 }
302 dst[n] = 0 as u8
303 return n
304}
305func ed_vlit(dst: *u8, s: *u8) -> i64 { let n: i64 = ed_cat(dst, 0, s); dst[n] = 0 as u8; return n }
306func ed_load_verticals() -> i64 {
307 if ed_vloaded_g == 1 { return ed_nvert_g }
308 ed_vloaded_g = 1
309 ed_vid_g = sys_mmap(ED_VSLOT * ED_VERT_MAX)
310 ed_vprior_g = sys_mmap(ED_VSLOT * ED_VERT_MAX)
311 ed_vpfx_g = sys_mmap(ED_VSLOT * ED_VERT_MAX)
312 ed_nvert_g = 0
313 let lenbox: *i64 = sys_mmap(16) as *i64
314 lenbox[0] = 0
315 let b: *u8 = sys_read_file(ED_VERT_CONF, lenbox)
316 let n: i64 = lenbox[0]
317 if (b as i64) != 0 { if n > 0 {
318 var i: i64 = 0
319 while i < n {
320 let ls: i64 = i
321 var le: i64 = ls
322 var go: i64 = 1
323 while go == 1 { if le >= n { go = 0 } else { if b[le] == (10 as u8) { go = 0 } else { le = le + 1 } } }
324 i = le + 1
325 if le > ls { if b[ls] != (35 as u8) {
326 var t1: i64 = 0 - 1
327 var t2: i64 = 0 - 1
328 var j: i64 = ls
329 while j < le {
330 if b[j] == (9 as u8) { if t1 < 0 { t1 = j } else { if t2 < 0 { t2 = j } } }
331 j = j + 1
332 }
333 if t1 > ls { if t2 > t1 { if ed_nvert_g < ED_VERT_MAX {
334 let k: i64 = ed_nvert_g
335 let r1: i64 = ed_vcopy(ed_vid(k), b, ls, t1)
336 let r2: i64 = ed_vcopy(ed_vprior(k), b, t1 + 1, t2)
337 let r3: i64 = ed_vcopy(ed_vpfx(k), b, t2 + 1, le)
338 if r1 >= 0 { if r2 >= 0 { if r3 >= 0 { ed_nvert_g = k + 1 } } }
339 } } }
340 } }
341 }
342 } }
343 if ed_nvert_g == 0 {
344 ed_vlit(ed_vid(0), "idols" as *u8)
345 ed_vlit(ed_vprior(0), "idols profile" as *u8)
346 ed_vlit(ed_vpfx(0), "/idols/" as *u8)
347 ed_nvert_g = 1
348 }
349 return ed_nvert_g
350}
351// the ACTIVE vertical's profile prefix; falls back to the 2026-07-24 literal when unloaded
352func ed_active_pfx() -> *u8 {
353 if ed_nvert_g <= 0 { return "/idols/" as *u8 }
354 if ed_vactive_g < 0 { return "/idols/" as *u8 }
355 if ed_vactive_g >= ed_nvert_g { return "/idols/" as *u8 }
356 return ed_vpfx(ed_vactive_g)
357}
358func ed_has_query(buf: *u8, s: i64, e: i64) -> i64 {
359 var i: i64 = s
360 while i < e { if buf[i] == (63 as u8) { return 1 } i = i + 1 }
361 return 0
362}
363// 1 iff url[s..e) names a PROFILE: a real path segment after the collection prefix, and no query facet.
364func ed_is_profile(buf: *u8, s: i64, e: i64) -> i64 {
365 if ed_has_query(buf, s, e) == 1 { return 0 }
366 let p: i64 = ed_find(buf, e, "/idols/" as *u8, s)
367 if p < 0 { return 0 }
368 let a: i64 = p + 7
369 if a >= e { return 0 }
370 if buf[a] == (35 as u8) { return 0 }
371 return 1
372}
373// pick the canonical landing from a search response: out4=[cid-start,cid-end,url-start,url-end].
374// returns 2 = non-search /idols/ profile, 1 = first non-search result, 0 = nothing usable.
375func ed_pick(resp: *u8, n: i64, out4: *i64) -> i64 {
376 var lcs: i64 = 0 - 1; var lce: i64 = 0; var lus: i64 = 0 - 1; var lue: i64 = 0
377 var fcs: i64 = 0 - 1; var fce: i64 = 0; var fus: i64 = 0 - 1; var fue: i64 = 0
378 var pos: i64 = 0; var idx: i64 = 0; var scan: i64 = 1
379 while scan == 1 {
380 let cp: i64 = ed_find(resp, n, "\"cid\":\"" as *u8, pos)
381 if cp < 0 { scan = 0 } else {
382 let cs: i64 = cp + 7; let ce: i64 = ed_qend(resp, cs, n)
383 let np: i64 = ed_find(resp, n, "\"cid\":\"" as *u8, ce)
384 var lim: i64 = n; if np >= 0 { lim = np }
385 let up: i64 = ed_find(resp, n, "\"url\":\"" as *u8, ce)
386 if up >= 0 { if up < lim {
387 let us: i64 = up + 7; let ue: i64 = ed_qend(resp, us, n)
388 idx = idx + 1
389 if ed_is_search(resp, us, ue) == 0 {
390 if fcs < 0 { fcs = cs; fce = ce; fus = us; fue = ue }
391 if lcs < 0 { if ed_is_profile(resp, us, ue) == 1 { lcs = cs; lce = ce; lus = us; lue = ue } }
392 }
393 } }
394 pos = ce
395 if idx > 60 { scan = 0 }
396 }
397 }
398 if lcs >= 0 { out4[0]=lcs; out4[1]=lce; out4[2]=lus; out4[3]=lue; return 2 }
399 if fcs >= 0 { out4[0]=fcs; out4[1]=fce; out4[2]=fus; out4[3]=fue; return 1 }
400 return 0
401}
402
403func main(argc: i64, argv: *i64) -> i64 {
404 ed_buf = sys_mmap(262144); ed_bo = 0
405 if argc < 2 { ed_puts("{\"error\":\"usage: nx_entity_dossier <entity name>\"}\n" as *u8); sys_write(1, ed_buf, ed_bo); return 2 }
406 let name: *u8 = argv[1] as *u8
407 let nn: i64 = ed_slen(name)
408 if nn == 0 { ed_puts("{\"error\":\"empty entity name\"}\n" as *u8); sys_write(1, ed_buf, ed_bo); return 2 }
409
410 // ---- 1. RESOLVE: search the name, biased toward the entity's PROFILE ----
411 // A person-dossier is looking for the subject's profile/idol page, so "idols profile" is a sound domain
412 // prior (not a hack): measured 2026-07-24, it resolves the AUTHORITY host (javdatabase) + real work codes
413 // and clean card empties, where the bare name lands on a random aggregator with false-positive extractions.
414 let enc: *u8 = sys_mmap(nn*3 + 16); let el: i64 = ed_urlenc(name, nn, enc)
415 let spath: *u8 = sys_mmap(el + 96)
416 var sp: i64 = ed_cat(spath, 0, "/api/search?scope=web&q=" as *u8)
417 sp = ed_catb(spath, sp, enc, el)
418 sp = ed_cat(spath, sp, "%20idols%20profile&page=0" as *u8)
419 let sresp: *u8 = sys_mmap(ED_CAP + 16)
420 let sn: i64 = ed_fetch(spath, sp, sresp)
421 if sn <= 0 {
422 ed_puts("{\"v\":1,\"entity\":\"" as *u8); ed_jout(name, nn)
423 ed_puts("\",\"found\":false,\"error\":\"search daemon unreachable on 127.0.0.1:18456\"}\n" as *u8)
424 sys_write(1, ed_buf, ed_bo); return 4
425 }
426 let total: i64 = ed_uint_after(sresp, sn, "\"total\":" as *u8)
427 // PORTFOLIO from the whole SERP (not just the landing doc): every result title/URL that surfaces for the
428 // entity contributes its work CODE (REBD-687, CJOD-364, ...). This is the oeuvre we actually have INDEXED --
429 // real coverage from reachable data, independent of whether the single profile page wins candidacy. Scanned
430 // NOW, before any alias-contraction retry reuses the sresp buffer.
431 let codes: *u8 = sys_mmap(ED_MAXCODES * 16 + 16)
432 let nc: *i64 = sys_mmap(16) as *i64; nc[0] = 0
433 ed_scan_codes(sresp, sn, codes, nc)
434
435 // pick the canonical landing: non-search /idols/ profile preferred, else first non-search result.
436 let pick: *i64 = sys_mmap(64) as *i64
437 var kind: i64 = ed_pick(sresp, sn, pick)
438 let cidbuf: *u8 = sys_mmap(64); var cidlen: i64 = 0
439 let urlbuf: *u8 = sys_mmap(1024); var urllen: i64 = 0
440 var i: i64 = 0
441 if kind >= 1 {
442 cidlen = pick[1] - pick[0]
443 i = 0; while i < cidlen { cidbuf[i] = sresp[pick[0] + i]; i = i + 1 }
444 urllen = pick[3] - pick[2]
445 i = 0; while i < urllen { urlbuf[i] = sresp[pick[2] + i]; i = i + 1 }
446 }
447 // ALIAS CONTRACTION: a multi-token name with no profile hit is often an alias of a MONONYM entity
448 // ("julia kyoka" -> "julia" -> /idols/julia/). Retry with the first token; adopt only a kind-2 pick.
449 if kind < 2 {
450 var t1: i64 = 0 - 1; var i0: i64 = 0; var lk: i64 = 1
451 while lk == 1 { if i0 >= nn { lk = 0 } else { if name[i0]==(32 as u8) { t1 = i0; lk = 0 } else { i0 = i0 + 1 } } }
452 if t1 > 0 {
453 let enc1: *u8 = sys_mmap(t1*3 + 16); let el1: i64 = ed_urlenc(name, t1, enc1)
454 let spath1: *u8 = sys_mmap(el1 + 96)
455 var sp1: i64 = ed_cat(spath1, 0, "/api/search?scope=web&q=" as *u8)
456 sp1 = ed_catb(spath1, sp1, enc1, el1)
457 sp1 = ed_cat(spath1, sp1, "%20idols%20profile&page=0" as *u8)
458 let sn1: i64 = ed_fetch(spath1, sp1, sresp)
459 if sn1 > 0 {
460 let kind1: i64 = ed_pick(sresp, sn1, pick)
461 if kind1 == 2 {
462 kind = 2
463 cidlen = pick[1] - pick[0]
464 i = 0; while i < cidlen { cidbuf[i] = sresp[pick[0] + i]; i = i + 1 }
465 urllen = pick[3] - pick[2]
466 i = 0; while i < urllen { urlbuf[i] = sresp[pick[2] + i]; i = i + 1 }
467 }
468 }
469 }
470 }
471 if kind == 0 {
472 ed_puts("{\"v\":1,\"entity\":\"" as *u8); ed_jout(name, nn)
473 var tm0: i64 = total
474 if tm0 < 0 { tm0 = 0 }
475 ed_puts("\",\"found\":false,\"indexed_matched\":" as *u8); ed_num(tm0)
476 ed_puts(",\"note\":\"no non-search result on page 0 -- crawl this entity (nx_page_ingest) then retry\"}\n" as *u8)
477 sys_write(1, ed_buf, ed_bo); return 0
478 }
479 let hostbuf: *u8 = sys_mmap(256); let hostlen: i64 = ed_host(urlbuf, urllen, hostbuf)
480
481 // ---- 2. FETCH the landing doc ----
482 let dpath: *u8 = sys_mmap(cidlen + 64)
483 var dp: i64 = ed_cat(dpath, 0, "/api/doc?scope=web&cid=" as *u8)
484 dp = ed_catb(dpath, dp, cidbuf, cidlen)
485 let dresp: *u8 = sys_mmap(ED_CAP + 16)
486 let dn: i64 = ed_fetch(dpath, dp, dresp)
487 // haystack = the doc "text" field (JSON-escaped, but our label scans are escaping-agnostic)
488 var hoff: i64 = 0; var hn: i64 = dn
489 let tp: i64 = ed_find(dresp, dn, "\"text\":\"" as *u8, 0)
490 if tp >= 0 { hoff = tp + 8; hn = dn - hoff }
491 let hay: *u8 = ((dresp as i64) + hoff) as *u8
492
493 // ---- 3. EXTRACT ----
494 let jpb: *u8 = sys_mmap(64); let jpl: i64 = ed_field(hay, hn, "JP: " as *u8, jpb, 62, 0)
495 let ageb: *u8 = sys_mmap(16); let agel: i64 = ed_field(hay, hn, "Age: " as *u8, ageb, 14, 1)
496 let dobb: *u8 = sys_mmap(24); let dobl: i64 = ed_field(hay, hn, "DOB: " as *u8, dobb, 22, 1)
497 let bpb: *u8 = sys_mmap(64); let bpl: i64 = ed_field(hay, hn, "Birthplace: " as *u8, bpb, 62, 1)
498 let measb: *u8 = sys_mmap(32); let measl: i64 = ed_field(hay, hn, "Measurements: " as *u8, measb, 30, 1)
499 let cupb: *u8 = sys_mmap(16); let cupl: i64 = ed_field(hay, hn, "Cup: " as *u8, cupb, 14, 1)
500 let htb: *u8 = sys_mmap(24); let htl: i64 = ed_field(hay, hn, "Height: " as *u8, htb, 22, 1)
501 let hairb: *u8 = sys_mmap(32); let hairl: i64 = ed_field(hay, hn, "Hair Color(s): " as *u8, hairb, 30, 1)
502 let tagsb: *u8 = sys_mmap(96); let tagsl: i64 = ed_field(hay, hn, "Tags: " as *u8, tagsb, 94, 0)
503 let biob: *u8 = sys_mmap(600); let biol: i64 = ed_block(hay, hn, "Biography" as *u8, "FAQ" as *u8, biob, 512)
504 let relb: *u8 = sys_mmap(600); let rell: i64 = ed_block(hay, hn, "Related Idols" as *u8, "Share" as *u8, relb, 512)
505
506 let authtotal: i64 = ed_uint_after(hay, hn, "Movies (" as *u8) // "...Censored JAV Movies (1051)"
507 ed_scan_codes(hay, hn, codes, nc) // add any codes from the landing doc too (deduped)
508
509 // ---- coverage (honest): indexed-matched docs for this name vs the authority's work-count ----
510 var at: i64 = authtotal; if at < 0 { at = 0 }
511 var tm: i64 = total; if tm < 0 { tm = 0 }
512 var permil: i64 = 0; if at > 0 { permil = (tm * 1000) / at }
513
514 // ---- 4. FOUND IS A MEASUREMENT, NOT A CONSTANT (2026-08-15) ----
515 // `found` was the literal `true` baked into the format string below, so this organ could not report
516 // NOT-finding on any path that reached the emit. MEASURED: a real named person came back found:true
517 // with authority_total:0, EVERY card field empty, and a faceted listing as canonical_url.
518 // Evidence is PUBLISHED beside the boolean, never just asserted -- a verdict a reader cannot audit is
519 // the same defect one level up.
520 var card_fields: i64 = 0
521 if jpl > 0 { card_fields = card_fields + 1 }
522 if agel > 0 { card_fields = card_fields + 1 }
523 if dobl > 0 { card_fields = card_fields + 1 }
524 if bpl > 0 { card_fields = card_fields + 1 }
525 if measl > 0 { card_fields = card_fields + 1 }
526 if htl > 0 { card_fields = card_fields + 1 }
527 if hairl > 0 { card_fields = card_fields + 1 }
528 if biol > 0 { card_fields = card_fields + 1 }
529 var found: i64 = 0
530 if kind >= 2 { found = 1 }
531 if at > 0 { found = 1 }
532 if card_fields > 0 { found = 1 }
533
534 // ---- 5. EMIT dossier JSON ----
535 ed_puts("{\"v\":1,\"entity\":\"" as *u8); ed_jout(name, nn)
536 if found == 1 { ed_puts("\",\"found\":true" as *u8) } else { ed_puts("\",\"found\":false" as *u8) }
537 ed_puts(",\"evidence\":{\"pick_kind\":" as *u8); ed_num(kind)
538 ed_puts(",\"card_fields\":" as *u8); ed_num(card_fields)
539 ed_puts(",\"authority_total\":" as *u8); ed_num(at)
540 ed_puts("},\"canonical_url\":\"" as *u8); ed_jout(urlbuf, urllen)
541 ed_puts("\",\"home_host\":\"" as *u8); ed_jout(hostbuf, hostlen)
542 ed_puts("\",\"landing_cid\":\"" as *u8); ed_out(cidbuf, cidlen)
543 ed_puts("\",\"card\":{\"jp\":\"" as *u8); ed_jout(jpb, jpl)
544 ed_puts("\",\"age\":\"" as *u8); ed_jout(ageb, agel)
545 ed_puts("\",\"dob\":\"" as *u8); ed_jout(dobb, dobl)
546 ed_puts("\",\"birthplace\":\"" as *u8); ed_jout(bpb, bpl)
547 ed_puts("\",\"measurements\":\"" as *u8); ed_jout(measb, measl)
548 ed_puts("\",\"cup\":\"" as *u8); ed_jout(cupb, cupl)
549 ed_puts("\",\"height\":\"" as *u8); ed_jout(htb, htl)
550 ed_puts("\",\"hair\":\"" as *u8); ed_jout(hairb, hairl)
551 ed_puts("\",\"tags\":\"" as *u8); ed_jout(tagsb, tagsl)
552 ed_puts("\",\"bio\":\"" as *u8); ed_jout(biob, biol)
553 ed_puts("\"},\"portfolio\":{\"authority_total\":" as *u8); ed_num(at)
554 ed_puts(",\"codes_sampled\":" as *u8); ed_num(nc[0])
555 ed_puts(",\"codes\":[" as *u8)
556 i = 0
557 while i < nc[0] {
558 if i > 0 { ed_puts("," as *u8) }
559 let slot: *u8 = ((codes as i64) + i * 16) as *u8
560 ed_puts("\"" as *u8); ed_out(((slot as i64) + 1) as *u8, slot[0] as i64); ed_puts("\"" as *u8)
561 i = i + 1
562 }
563 ed_puts("]},\"related_raw\":\"" as *u8); ed_jout(relb, rell)
564 ed_puts("\",\"coverage\":{\"authority_total\":" as *u8); ed_num(at)
565 ed_puts(",\"indexed_matched\":" as *u8); ed_num(tm)
566 ed_puts(",\"permil\":" as *u8); ed_num(permil)
567 ed_puts("},\"sources\":[\"" as *u8); ed_jout(hostbuf, hostlen)
568 ed_puts("\"]}\n" as *u8)
569
570 // ---- durable copy (survives an edge-dropped tools/call; read via nx_fs) ----
571 let slug: *u8 = sys_mmap(nn + 8); let sl: i64 = ed_slug(name, nn, slug)
572 let fpath: *u8 = sys_mmap(nn + 64)
573 var fo: i64 = ed_cat(fpath, 0, "knowledge/status/dossier-" as *u8)
574 fo = ed_catb(fpath, fo, slug, sl); fo = ed_cat(fpath, fo, ".txt" as *u8); fpath[fo] = 0 as u8
575 let wfd: i64 = sys_openat_wr(fpath, 420)
576 if wfd >= 0 { sys_write(wfd, ed_buf, ed_bo); sys_close(wfd) }
577 sys_write(1, ed_buf, ed_bo)
578 return 0
579}