code wiki / _hdl_build / nx_estate_search.nx
nx_estate_search.nx source
↩ module page · 128 lines · 7970 B
1// nx_estate_search.nx -- SEARCH THE ESTATE'S OWN CORPUS from the sovereign surface (search rungs E1/E4, 2026-09-14).
2// The estate shard (dp-estate-pub-, filled by `nx_corpus_ingest tree <root> estate estate://<root>/`) holds the code
3// base, the boards and the confs as documents. No public scope resolves to that domain, so the ONLY door is this
4// organ over MCP behind a cap: an agent in the swarm asks it the way it asks nx_txtscan, and the public /search
5// never sees the estate. The query itself is nx_estate_search_lib.es_query (gated in-process on a fixture shard).
6// usage: nx_estate_search <term> [term ...] [n=<top>] [domain=<shard domain, default estate>]
7// One line per hit (rank TAB score TAB url TAB snippet), then LAST the summary `ESTATE-SEARCH q=<q> hits=<n>
8// top=<top> shard=<prefix> segments=<k>` so a positional reader finds it. An unreadable shard exits 3 and names the
9// ingest. domain= exists so a gate can drive the organ on a fixture shard; every domain it can name is one some
10// ingest already made searchable, so it opens no door the estate had not opened. license_tier: ORIGINAL
11import "nx_estate_search_lib.nx"
12
13const ES_OUTCAP: i64 = 131072 // fifty hits of a 1 KB url and a 160 B snippet fit with room; es_query stops early otherwise
14
15func es_puts(s: *u8) -> i64 { sys_write(1, s, es_slen(s)); return 0 }
16
17func main(argc: i64, argv: *i64) -> i64 {
18 let usage: *u8 = "usage: nx_estate_search <term> [term ...] [n=<top>] [domain=<shard domain>] [scope=<word from knowledge/search_scopes.conf: code boards status estate chat>]\n" as *u8
19 if argc < 2 { es_puts(usage); return 2 }
20 let q: *u8 = sys_mmap(ES_QBUF)
21 var qn: i64 = 0
22 var top: i64 = ES_TOP_DEFAULT
23 var domain: *u8 = ES_DOMAIN
24 let nopt: *u8 = "n=" as *u8
25 let dopt: *u8 = "domain=" as *u8
26 let sopt: *u8 = "scope=" as *u8
27 var scopeword: *u8 = 0 as *u8
28 let fopt: *u8 = "format=" as *u8
29 let oopt: *u8 = "out=" as *u8
30 var format: *u8 = 0 as *u8 // E8: uxf (the canonical envelope to out=<path>, its CID on the receipt) or cid (the CID alone)
31 var outpath: *u8 = 0 as *u8
32 var ai: i64 = 1
33 while ai < argc {
34 let a: *u8 = argv[ai] as *u8
35 if es_starts(a, nopt) == 1 { top = es_atoi((a as i64 + es_slen(nopt)) as *u8) }
36 else { if es_starts(a, dopt) == 1 { domain = (a as i64 + es_slen(dopt)) as *u8 }
37 else { if es_starts(a, sopt) == 1 { scopeword = (a as i64 + es_slen(sopt)) as *u8 }
38 else { if es_starts(a, fopt) == 1 { format = (a as i64 + es_slen(fopt)) as *u8 }
39 else { if es_starts(a, oopt) == 1 { outpath = (a as i64 + es_slen(oopt)) as *u8 }
40 else {
41 if qn > 0 { if qn < ES_QBUF - 1 { q[qn] = ES_SPACE as u8; qn = qn + 1 } }
42 var k: i64 = 0
43 while a[k] != (0 as u8) { if qn < ES_QBUF - 1 { q[qn] = a[k]; qn = qn + 1 } k = k + 1 }
44 } } } } }
45 ai = ai + 1
46 }
47 q[qn] = 0 as u8
48 if qn == 0 { es_puts(usage); return 2 }
49 // E4: a scope word maps to its domain and url prefix through the ONE conf the daemon reads; this door is cap-gated,
50 // so the mcp rows are honoured here. An unknown word is refused BY NAME (exit 4), never silently ignored.
51 if (scopeword as i64) != 0 {
52 let sdom: *u8 = sys_mmap(ES_PREFIXBUF)
53 let surf: *i64 = dsv_box()
54 dsv_scopes_conf_load()
55 if dsv_scope_lookup(scopeword, es_slen(scopeword), sdom, dsv_scope_pfxbuf, surf) == 0 {
56 es_puts("ESTATE-SEARCH scope UNKNOWN word=" as *u8); es_puts(scopeword); es_puts(" -- not a row of knowledge/search_scopes.conf\n" as *u8)
57 return 4
58 }
59 domain = sdom
60 dss_set_scope_prefix(dsv_scope_pfxbuf, es_slen(dsv_scope_pfxbuf))
61 }
62 if top < 1 { top = 1 }
63 if top > ES_TOP_MAX { top = ES_TOP_MAX }
64 let prefix: *u8 = sys_mmap(ES_PREFIXBUF)
65 dss_prefix(domain, prefix)
66 let out: *u8 = sys_mmap(ES_OUTCAP)
67 let olen: *i64 = sys_mmap(ES_BOX) as *i64
68 let segs: *i64 = sys_mmap(ES_BOX) as *i64
69 let n: i64 = es_query(domain, q, qn, top, out, ES_OUTCAP, olen, segs)
70 if n == ES_UNREADABLE {
71 es_puts("ESTATE-SEARCH shard UNREADABLE prefix=" as *u8); es_puts(prefix)
72 es_puts(" -- not ingested yet or unreadable, which are different facts; ingest with: nx_corpus_ingest tree <root> " as *u8); es_puts(domain); es_puts(" estate://<root>/\n" as *u8)
73 return 3
74 }
75 sys_write(1, out, olen[0])
76 if (scopeword as i64) != 0 {
77 // the scope's own receipt: the row it resolved to and what the filter cost, before the positional summary line
78 let sl: *u8 = sys_mmap(ES_QBUF + ES_PREFIXBUF + ES_LINE_RESERVE)
79 var so: i64 = es_cat(sl, 0, "ESTATE-SCOPE word=" as *u8); so = es_cat(sl, so, scopeword)
80 so = es_cat(sl, so, " domain=" as *u8); so = es_cat(sl, so, domain)
81 so = es_cat(sl, so, " prefix=" as *u8); so = es_cat(sl, so, dsv_scope_pfxbuf)
82 so = es_cat(sl, so, " scope_us=" as *u8); so = es_catn(sl, so, dss_scope_stats())
83 sl[so] = ES_LF as u8; so = so + 1
84 sys_write(1, sl, so)
85 }
86 // E8: THE MACHINE ENVELOPE. format=cid prints the receipt (the CID names this exact answer over this shard state);
87 // format=uxf also writes the canonical bytes to out=<path>. The text above is always printed (the contract is
88 // additive). An unknown word, uxf without out=, and an answer the envelope cannot parse are refused BY NAME.
89 if (format as i64) != 0 {
90 let fuxf: *u8 = "uxf" as *u8
91 let fcid: *u8 = "cid" as *u8
92 var known: i64 = 0
93 var want_bytes: i64 = 0
94 if es_slen(format) == es_slen(fuxf) { if es_starts(format, fuxf) == 1 { known = 1; want_bytes = 1 } }
95 if es_slen(format) == es_slen(fcid) { if es_starts(format, fcid) == 1 { known = 1 } }
96 if known == 0 { es_puts("ESTATE-UXF format UNKNOWN word=" as *u8); es_puts(format); es_puts(" -- uxf (canonical bytes to out=<path>) or cid (the receipt alone)\n" as *u8); return 2 }
97 if want_bytes == 1 { if (outpath as i64) == 0 { es_puts("ESTATE-UXF format=uxf needs out=<path>: canonical bytes never go to the text stream\n" as *u8); return 2 } }
98 let need: i64 = es_uxf_envelope_size(out, olen[0], q, n, top, prefix, segs[0])
99 if need < 0 { es_puts("ESTATE-UXF REFUSED: the rendered answer did not parse as rank TAB score TAB url TAB snippet lines\n" as *u8); return 5 }
100 let canon: *u8 = sys_mmap(need)
101 let cid: *u8 = sys_mmap(UXF_CID_LEN + 1)
102 let fields: *i64 = sys_mmap(ES_BOX) as *i64
103 let got: i64 = es_uxf_envelope(out, olen[0], q, n, top, prefix, segs[0], canon, cid, fields)
104 var wrote: i64 = 0
105 if want_bytes == 1 {
106 let fd: i64 = sys_openat_wr(outpath, MODE_0644)
107 if fd < 0 { es_puts("ESTATE-UXF REFUSED: cannot open out=" as *u8); es_puts(outpath); es_puts(" for writing\n" as *u8); return 5 }
108 wrote = sys_write(fd, canon, got)
109 sys_close(fd)
110 }
111 var plen: i64 = 1
112 if (outpath as i64) != 0 { plen = es_slen(outpath) }
113 let rl: *u8 = sys_mmap(ES_LINE_RESERVE + UXF_CID_LEN + plen)
114 var ro: i64 = es_cat(rl, 0, "ESTATE-UXF cid=" as *u8); ro = es_cat(rl, ro, cid)
115 ro = es_cat(rl, ro, " profile=" as *u8); ro = es_catn(rl, ro, uxf_codec_of_cid(cid))
116 ro = es_cat(rl, ro, " bytes=" as *u8); ro = es_catn(rl, ro, got)
117 ro = es_cat(rl, ro, " fields=" as *u8); ro = es_catn(rl, ro, fields[0])
118 ro = es_cat(rl, ro, " path=" as *u8)
119 if (outpath as i64) != 0 { ro = es_cat(rl, ro, outpath) } else { ro = es_cat(rl, ro, "-" as *u8) }
120 ro = es_cat(rl, ro, " wrote=" as *u8); ro = es_catn(rl, ro, wrote)
121 rl[ro] = ES_LF as u8; ro = ro + 1
122 sys_write(1, rl, ro)
123 }
124 let sm: *u8 = sys_mmap(ES_QBUF + ES_PREFIXBUF + ES_LINE_RESERVE)
125 let sn: i64 = es_summary(sm, 0, q, n, top, prefix, segs[0])
126 sys_write(1, sm, sn)
127 return 0
128}