code wiki / _hdl_build / nx_prim_query.nx
nx_prim_query.nx source
↩ module page · 167 lines · 11288 B
1// nx_prim_query.nx -- prim lane F782 (query_primitives: the 'what do I have' half of meet-in-the-middle).
2// Reads the F781 manifest knowledge/store/prim_registry.json and returns every primitive object whose
3// text matches the query (substring facet over domain/capabilities/signature/output/id) -- the sovereign
4// query the top-down orchestrator (F783) calls to find matching blocks. Objects copied VERBATIM (already
5// valid JSON). Result to stdout AND an atomic out-file (verifiable). READ-ONLY. Empty query = match-all.
6// Object boundaries found via PLAIN-WORD anchors + byte confirm ({" before primitive_id) so the
7// match logic carries NO string-escape dependency (cannot silently 0-match).
8//
9// ENVELOPES ARE DERIVED, NOT PICKED (2026-09-14, search rung E7; operator: the estate-wide magic numbers go).
10// Three caps used to live here and every one failed in silence or was one generation from it: the manifest
11// was read through a 1 MiB window (the manifest is 957 KB today and grows with every registered tool), the
12// object table held 2048 boundaries and DROPPED the rest without a word, and the reply buffer was bounded at
13// 262100 bytes -- a match-all query over a 957 KB manifest was being cut to invalid JSON silently. Now the
14// manifest is read WHOLE by sys_read_file (sized from the file), the object table is sized from the input
15// (no object can be shorter than its own primitive_id key, PQ_OBJ_MIN bytes), and the reply window is the
16// input plus the fixed header (every matched object is a verbatim copy of input bytes). The only bound left
17// refuses LOUDLY and cannot fire by construction.
18// nx_prim_query <query> [manifest.json] [out.json]
19// exit: 0 GREEN | 4 ABSENT/MALFORMED | 5 WINDOW (cannot happen by construction; kept loud) | 2 usage.
20// license_tier: ORIGINAL expect_exit: 0
21import "nx_syscalls.nx"
22// every literal named for its ONE purpose; none is a tunable
23const PQ_OBJ_MIN: i64 = 16 // a primitive object is never shorter than its {"primitive_id":" key, so input/PQ_OBJ_MIN bounds the object count
24const PQ_HEAD_OVERHEAD: i64 = 512 // the fixed reply header and footer bytes beyond the copied objects and the escaped query (measured under 300)
25const PQ_DIGITS: i64 = 28 // decimal digits an i64 can need, with its sign
26const PQ_QUOTE: i64 = 34
27const PQ_BACKSLASH: i64 = 92
28const PQ_SPACE: i64 = 32
29const PQ_LBRACE: i64 = 123
30const PQ_COMMA: i64 = 44
31const PQ_MINUS: i64 = 45
32const PQ_ZERO: i64 = 48
33const PQ_MODE_RW: i64 = 420
34const PQ_EXIT_USAGE: i64 = 2
35const PQ_EXIT_ABSENT: i64 = 4
36const PQ_EXIT_WINDOW: i64 = 5
37// the derived reply window (set once in main from the input; every emitter bounds on it)
38static pq_cap_g: i64
39
40func pq_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
41func pq_b(rep: *u8, pos: i64, s: *u8) -> i64 { var p: i64 = pos; var i: i64 = 0; while s[i] != (0 as u8) { if p < pq_cap_g { rep[p] = s[i]; p = p + 1 } i = i + 1 } return p }
42func pq_bn(rep: *u8, pos: i64, v: i64) -> i64 { var p: i64 = pos; var m: i64 = v; if m < 0 { if p < pq_cap_g { rep[p] = PQ_MINUS as u8; p = p + 1 } m = 0 - m } let t: *u8 = sys_mmap(PQ_DIGITS); var k: i64 = 0; if m == 0 { t[0] = PQ_ZERO as u8; k = 1 } while m > 0 { t[k] = (PQ_ZERO + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { if p < pq_cap_g { rep[p] = t[k - 1 - i]; p = p + 1 } i = i + 1 } sys_munmap(t, PQ_DIGITS); return p }
43func pq_q(rep: *u8, pos: i64) -> i64 { var p: i64 = pos; if p < pq_cap_g { rep[p] = PQ_QUOTE as u8; p = p + 1 } return p }
44func pq_je(rep: *u8, pos: i64, src: *u8, s: i64, e: i64) -> i64 {
45 var p: i64 = pos
46 var i: i64 = s
47 while i < e {
48 let c: i64 = src[i]
49 if c == PQ_QUOTE { if p < pq_cap_g { rep[p] = PQ_BACKSLASH as u8; p = p + 1 } if p < pq_cap_g { rep[p] = PQ_QUOTE as u8; p = p + 1 } }
50 else { if c == PQ_BACKSLASH { if p < pq_cap_g { rep[p] = PQ_BACKSLASH as u8; p = p + 1 } if p < pq_cap_g { rep[p] = PQ_BACKSLASH as u8; p = p + 1 } }
51 else { if c < PQ_SPACE { if p < pq_cap_g { rep[p] = PQ_SPACE as u8; p = p + 1 } }
52 else { if p < pq_cap_g { rep[p] = c as u8; p = p + 1 } } } }
53 i = i + 1
54 }
55 return p
56}
57func pq_cp(rep: *u8, pos: i64, src: *u8, s: i64, e: i64) -> i64 { var p: i64 = pos; var i: i64 = s; while i < e { if p < pq_cap_g { rep[p] = src[i]; p = p + 1 } i = i + 1 } return p }
58func pq_has(buf: *u8, s: i64, e: i64, pat: *u8, pl: i64) -> i64 {
59 if pl == 0 { return 1 }
60 var i: i64 = s
61 var hit: i64 = 0
62 while i + pl <= e {
63 var k: i64 = 0
64 var m: i64 = 1
65 while k < pl { if buf[i + k] != pat[k] { m = 0; k = pl } else { k = k + 1 } }
66 if m == 1 { hit = 1; i = e } else { i = i + 1 }
67 }
68 return hit
69}
70func pq_off(buf: *u8, s: i64, e: i64, pat: *u8, pl: i64) -> i64 {
71 if pl == 0 { return 0 - 1 }
72 var i: i64 = s
73 var found: i64 = 0 - 1
74 while i + pl <= e {
75 var k: i64 = 0
76 var m: i64 = 1
77 while k < pl { if buf[i + k] != pat[k] { m = 0; k = pl } else { k = k + 1 } }
78 if m == 1 { found = i; i = e } else { i = i + 1 }
79 }
80 return found
81}
82func pq_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
83func main(argc: i64, argv: *i64) -> i64 {
84 if argc < 2 { pq_w(2, "usage: nx_prim_query <query> [manifest.json] [out.json]\n" as *u8); sys_exit(PQ_EXIT_USAGE); return PQ_EXIT_USAGE }
85 let query: *u8 = argv[1] as *u8
86 var manifest: *u8 = "knowledge/store/prim_registry.json" as *u8
87 var outpath: *u8 = "knowledge/status/prim_query.json" as *u8
88 var tmppath: *u8 = "knowledge/status/prim_query.json.tmp" as *u8
89 if argc >= 3 { manifest = argv[2] as *u8 }
90 if argc >= 4 { outpath = argv[3] as *u8 }
91 let qlen: i64 = pq_slen(query)
92 // the manifest, whole: sized from the file by sys_read_file, so there is no window to saturate on the way in
93 let nbx: *i64 = sys_mmap(16) as *i64
94 let buf: *u8 = sys_read_file(manifest, nbx)
95 if (buf as i64) == 0 { pq_w(1, "PRIM-QUERY verdict=ABSENT manifest unreadable\n" as *u8); sys_exit(PQ_EXIT_ABSENT); return PQ_EXIT_ABSENT }
96 let n: i64 = nbx[0]
97 // PLAIN-WORD region anchors (no embedded quotes -> escape-independent)
98 let primkey: *u8 = "primitives" as *u8
99 let ph: i64 = pq_off(buf, 0, n, primkey, pq_slen(primkey))
100 if ph < 0 { pq_w(1, "PRIM-QUERY verdict=MALFORMED no-primitives-key\n" as *u8); sys_exit(PQ_EXIT_ABSENT); return PQ_EXIT_ABSENT }
101 let rstart: i64 = ph + pq_slen(primkey)
102 let endpat: *u8 = "],\"envelope" as *u8
103 let eh: i64 = pq_off(buf, rstart, n, endpat, pq_slen(endpat))
104 var arrend: i64 = n
105 if eh > 0 { arrend = eh }
106 // object boundaries: the word primitive_id whose preceding two bytes are { and the quote; the table is sized
107 // from the input (an object is never shorter than that key), so no boundary can ever be dropped
108 let idkey: *u8 = "primitive_id" as *u8
109 let idlen: i64 = pq_slen(idkey)
110 let bcap: i64 = n / PQ_OBJ_MIN + 2
111 let bounds: *i64 = sys_mmap(8 * bcap) as *i64
112 var nb: i64 = 0
113 var scan: i64 = rstart
114 var go: i64 = 1
115 while go == 1 {
116 let o: i64 = pq_off(buf, scan, arrend, idkey, idlen)
117 if o < 0 { go = 0 } else {
118 if o >= 2 { if buf[o-2] == (PQ_LBRACE as u8) { if buf[o-1] == (PQ_QUOTE as u8) { if nb < bcap { bounds[nb] = o - 2; nb = nb + 1 } } } }
119 scan = o + idlen
120 }
121 }
122 // the reply window, DERIVED: every matched object is a verbatim copy of input bytes, plus the escaped query and the fixed header
123 pq_cap_g = n + 2 * qlen + PQ_HEAD_OVERHEAD
124 let rep: *u8 = sys_mmap(pq_cap_g + 16)
125 var p: i64 = 0
126 p = pq_b(rep, p, "{" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, "v" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":1," as *u8)
127 p = pq_q(rep, p); p = pq_b(rep, p, "query" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_q(rep, p); p = pq_je(rep, p, query, 0, qlen); p = pq_q(rep, p); p = pq_b(rep, p, "," as *u8)
128 p = pq_q(rep, p); p = pq_b(rep, p, "source" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, "knowledge/store/prim_registry.json" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, "," as *u8)
129 p = pq_q(rep, p); p = pq_b(rep, p, "match_kind" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, "substring-facet-domain-capabilities-signature-output-id-case-sensitive" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, "," as *u8)
130 p = pq_q(rep, p); p = pq_b(rep, p, "primitives" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":[" as *u8)
131 var matched: i64 = 0
132 var first: i64 = 1
133 var k: i64 = 0
134 while k < nb {
135 let ostart: i64 = bounds[k]
136 var oend: i64 = arrend
137 if k + 1 < nb { oend = bounds[k+1] }
138 var ce: i64 = oend
139 if ce > ostart { if buf[ce-1] == (PQ_COMMA as u8) { ce = ce - 1 } }
140 var hit: i64 = 0
141 if qlen == 0 { hit = 1 } else { hit = pq_has(buf, ostart, ce, query, qlen) }
142 if hit == 1 {
143 if first == 1 { first = 0 } else { p = pq_b(rep, p, "," as *u8) }
144 p = pq_cp(rep, p, buf, ostart, ce)
145 matched = matched + 1
146 }
147 k = k + 1
148 }
149 p = pq_b(rep, p, "]," as *u8)
150 p = pq_q(rep, p); p = pq_b(rep, p, "matched" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_bn(rep, p, matched); p = pq_b(rep, p, "," as *u8)
151 p = pq_q(rep, p); p = pq_b(rep, p, "scanned" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_bn(rep, p, nb); p = pq_b(rep, p, "," as *u8)
152 // cap 0 BY CONSTRUCTION (kept as a key so older readers still parse); the table and the window are derived from the input
153 p = pq_q(rep, p); p = pq_b(rep, p, "cap" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":0," as *u8)
154 p = pq_q(rep, p); p = pq_b(rep, p, "objects_capacity" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_bn(rep, p, bcap); p = pq_b(rep, p, "," as *u8)
155 p = pq_q(rep, p); p = pq_b(rep, p, "input_bytes" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_bn(rep, p, n); p = pq_b(rep, p, "," as *u8)
156 p = pq_q(rep, p); p = pq_b(rep, p, "reply_window_bytes" as *u8); p = pq_q(rep, p); p = pq_b(rep, p, ":" as *u8); p = pq_bn(rep, p, pq_cap_g)
157 p = pq_b(rep, p, "}" as *u8)
158 if p >= pq_cap_g { pq_w(1, "PRIM-QUERY verdict=REFUSED reply-window-saturated: the derived window (input + 2 x query + PQ_HEAD_OVERHEAD) was exceeded, which cannot happen unless the header overhead measurement is stale -- a cut reply is invalid JSON and is NOT written\n" as *u8); sys_exit(PQ_EXIT_WINDOW); return PQ_EXIT_WINDOW }
159 // THE DURABLE ARTIFACT FIRST (2026-09-14): a caller whose capture cap is smaller than the reply kills the reader at
160 // the cap, and a file written AFTER stdout is then never written -- measured on the match-all query through the sync
161 // MCP lane (reply 957060 B, capture cap 163840, out file ABSENT while the job lane wrote it whole). stdout follows.
162 let tmpfd: i64 = sys_openat_wr(tmppath, PQ_MODE_RW)
163 if tmpfd >= 0 { sys_write(tmpfd, rep, p); sys_close(tmpfd); sys_renameat(tmppath, outpath) }
164 sys_write(1, rep, p)
165 sys_exit(0)
166 return 0
167}