code wiki / _hdl_build / nx_prim_gap.nx
nx_prim_gap.nx source
↩ module page · 234 lines · 14837 B
1// nx_prim_gap.nx -- prim lane F783 (the meet-in-the-middle COLLISION: missing-primitive detector).
2// Takes required-capability tokens as argv (intent->capabilities decomposition is the AGENT-SEAT's
3// judgment -- an LLM/operator over MCP, per sovereign doctrine, NOT baked into a no-float organ). For each
4// required capability it searches the F781 registry (prim_registry.json) and, if 0 primitives match, raises
5// a MISSING-PRIMITIVE EXCEPTION with a structural spec -- the operator's 'architectural alarm'. Matching is
6// over each primitive's VALUE fields (id + domain + capabilities + title + output + signature values), NEVER
7// the JSON key names -- so a query that is a substring of a key (sign<-signature) cannot false-HAVE and hide
8// a real gap. READ-ONLY; result to stdout + atomic out-file. Filing the gaps is the caller's deliberate act.
9// nx_prim_gap <req-capability> [<req> ...]
10// exit: 0 all-satisfied | 3 GAPS-FOUND (>=1 missing = the alarm) | 4 registry unreadable | 2 usage.
11// license_tier: ORIGINAL expect_exit: 0
12import "nx_syscalls.nx"
13const K_MAGIC_2048: i64 = 2048
14const K_MAGIC_262100: i64 = 262100
15const K_MAGIC_4090: i64 = 4090
16const K_MAGIC_1048576: i64 = 1048576
17const K_MAGIC_4096: i64 = 4096
18
19func pg_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 }
20func pg_b(rep: *u8, pos: i64, s: *u8) -> i64 { var p: i64 = pos; var i: i64 = 0; while s[i] != (0 as u8) { if p < K_MAGIC_262100 { rep[p] = s[i]; p = p + 1 } i = i + 1 } return p }
21func pg_bn(rep: *u8, pos: i64, v: i64) -> i64 { var p: i64 = pos; var m: i64 = v; if m < 0 { if p < K_MAGIC_262100 { rep[p] = 45 as u8; p = p + 1 } m = 0 - m } let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { if p < K_MAGIC_262100 { rep[p] = t[k - 1 - i]; p = p + 1 } i = i + 1 } return p }
22func pg_q(rep: *u8, pos: i64) -> i64 { var p: i64 = pos; if p < K_MAGIC_262100 { rep[p] = 34 as u8; p = p + 1 } return p }
23func pg_je(rep: *u8, pos: i64, src: *u8, s: i64, e: i64) -> i64 {
24 var p: i64 = pos
25 var i: i64 = s
26 while i < e {
27 let c: i64 = src[i]
28 if c == 34 { if p < K_MAGIC_262100 { rep[p] = 92 as u8; p = p + 1 } if p < K_MAGIC_262100 { rep[p] = 34 as u8; p = p + 1 } }
29 else { if c == 92 { if p < K_MAGIC_262100 { rep[p] = 92 as u8; p = p + 1 } if p < K_MAGIC_262100 { rep[p] = 92 as u8; p = p + 1 } }
30 else { if c < 32 { if p < K_MAGIC_262100 { rep[p] = 32 as u8; p = p + 1 } }
31 else { if p < K_MAGIC_262100 { rep[p] = c as u8; p = p + 1 } } } }
32 i = i + 1
33 }
34 return p
35}
36func pg_reads(path: *u8, buf: *u8, cap: i64) -> i64 {
37 let fd: i64 = sys_openat_rd(path)
38 if fd < 0 { return 0 - 1 }
39 var n: i64 = 0
40 var go: i64 = 1
41 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } }
42 sys_close(fd)
43 return n
44}
45func pg_has(buf: *u8, s: i64, e: i64, pat: *u8, pl: i64) -> i64 {
46 if pl == 0 { return 0 }
47 var i: i64 = s
48 var hit: i64 = 0
49 while i + pl <= e {
50 var k: i64 = 0
51 var m: i64 = 1
52 while k < pl { if buf[i + k] != pat[k] { m = 0; k = pl } else { k = k + 1 } }
53 if m == 1 { hit = 1; i = e } else { i = i + 1 }
54 }
55 return hit
56}
57func pg_off(buf: *u8, s: i64, e: i64, pat: *u8, pl: i64) -> i64 {
58 if pl == 0 { return 0 - 1 }
59 var i: i64 = s
60 var found: i64 = 0 - 1
61 while i + pl <= e {
62 var k: i64 = 0
63 var m: i64 = 1
64 while k < pl { if buf[i + k] != pat[k] { m = 0; k = pl } else { k = k + 1 } }
65 if m == 1 { found = i; i = e } else { i = i + 1 }
66 }
67 return found
68}
69func pg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
70func pg_id(buf: *u8, ostart: i64, ce: i64, out: *u8) -> i64 {
71 var i: i64 = ostart + 17
72 var d: i64 = 0
73 var go: i64 = 1
74 while go == 1 { if i >= ce { go = 0 } else { let c: i64 = buf[i]; if c == 34 { go = 0 } else { if d < 60 { out[d] = c as u8; d = d + 1 } i = i + 1 } } }
75 return d
76}
77// append the VALUE of "key" (string or [array] inner text, key EXCLUDED) from object [os,ce) into hs at hp.
78func pg_appendval(hs: *u8, hp: i64, buf: *u8, os: i64, ce: i64, key: *u8, klen: i64) -> i64 {
79 var p: i64 = hp
80 var koff: i64 = 0 - 1
81 var sc: i64 = os
82 var go: i64 = 1
83 while go == 1 {
84 let o: i64 = pg_off(buf, sc, ce, key, klen)
85 if o < 0 { go = 0 } else {
86 if o >= 1 { if buf[o-1] == (34 as u8) { if o + klen + 1 < ce { if buf[o+klen] == (34 as u8) { if buf[o+klen+1] == (58 as u8) { koff = o; go = 0 } } } } }
87 if koff < 0 { sc = o + klen }
88 }
89 }
90 if koff < 0 { return p }
91 if p < K_MAGIC_4090 { hs[p] = 32 as u8; p = p + 1 }
92 let vp: i64 = koff + klen + 2
93 if vp < ce {
94 let opener: i64 = buf[vp]
95 if opener == 34 {
96 var i: i64 = vp + 1
97 var g2: i64 = 1
98 while g2 == 1 { if i >= ce { g2 = 0 } else { let c: i64 = buf[i]; if c == 92 { if i + 1 < ce { if p < K_MAGIC_4090 { hs[p] = buf[i+1]; p = p + 1 } } i = i + 2 } else { if c == 34 { g2 = 0 } else { if p < K_MAGIC_4090 { hs[p] = c as u8; p = p + 1 } i = i + 1 } } } }
99 } else { if opener == 91 {
100 var i2: i64 = vp + 1
101 var g3: i64 = 1
102 while g3 == 1 { if i2 >= ce { g3 = 0 } else { let c2: i64 = buf[i2]; if c2 == 93 { g3 = 0 } else { if p < K_MAGIC_4090 { hs[p] = c2 as u8; p = p + 1 } i2 = i2 + 1 } } }
103 } }
104 }
105 return p
106}
107func main(argc: i64, argv: *i64) -> i64 {
108 if argc < 2 { pg_w(2, "usage: nx_prim_gap <req-capability> [<req> ...]\n" as *u8); sys_exit(2); return 2 }
109 let manifest: *u8 = "knowledge/store/prim_registry.json" as *u8
110 let outpath: *u8 = "knowledge/status/prim_gap.json" as *u8
111 let tmppath: *u8 = "knowledge/status/prim_gap.json.tmp" as *u8
112 let win: i64 = K_MAGIC_1048576
113 let buf: *u8 = sys_mmap(win + 16)
114 let n: i64 = pg_reads(manifest, buf, win)
115 if n < 0 { pg_w(1, "PRIM-GAP verdict=ABSENT manifest unreadable\n" as *u8); sys_exit(4); return 4 }
116 if n >= win { pg_w(1, "PRIM-GAP verdict=REFUSED manifest-window-saturated: raise win, a torn manifest scan undercounts primitives\n" as *u8); sys_exit(4); return 4 }
117 let ph: i64 = pg_off(buf, 0, n, "primitives" as *u8, 10)
118 if ph < 0 { pg_w(1, "PRIM-GAP verdict=MALFORMED\n" as *u8); sys_exit(4); return 4 }
119 let rstart: i64 = ph + 10
120 let endpat: *u8 = sys_mmap(16)
121 endpat[0] = 93 as u8
122 endpat[1] = 44 as u8
123 endpat[2] = 34 as u8
124 endpat[3] = 101 as u8
125 endpat[4] = 110 as u8
126 endpat[5] = 118 as u8
127 endpat[6] = 101 as u8
128 endpat[7] = 108 as u8
129 endpat[8] = 111 as u8
130 endpat[9] = 112 as u8
131 endpat[10] = 101 as u8
132 let eh: i64 = pg_off(buf, rstart, n, endpat, 11)
133 var arrend: i64 = n
134 if eh > 0 { arrend = eh }
135 let bounds: *i64 = sys_mmap(K_MAGIC_2048 * 8) as *i64
136 var nb: i64 = 0
137 var scan: i64 = rstart
138 var goo: i64 = 1
139 while goo == 1 {
140 let o: i64 = pg_off(buf, scan, arrend, "primitive_id" as *u8, 12)
141 if o < 0 { goo = 0 } else {
142 if o >= 2 { if buf[o-2] == (123 as u8) { if buf[o-1] == (34 as u8) { if nb < K_MAGIC_2048 { bounds[nb] = o - 2; nb = nb + 1 } } } }
143 scan = o + 12
144 }
145 }
146 let rep: *u8 = sys_mmap(win + 16)
147 let idb: *u8 = sys_mmap(64)
148 let hs: *u8 = sys_mmap(K_MAGIC_4096)
149 let mstart: *i64 = sys_mmap(8 * 8) as *i64
150 var p: i64 = 0
151 p = pg_b(rep, p, "{" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "v" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":1," as *u8)
152 p = pg_q(rep, p); p = pg_b(rep, p, "kind" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "missing_primitive_scan" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
153 p = pg_q(rep, p); p = pg_b(rep, p, "source" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "knowledge/store/prim_registry.json" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
154 p = pg_q(rep, p); p = pg_b(rep, p, "match_over" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "value-fields-id-domain-capabilities-title-output-signature-keys-excluded-case-sensitive" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
155 p = pg_q(rep, p); p = pg_b(rep, p, "requirements" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":[" as *u8)
156 var satisfied: i64 = 0
157 var missing: i64 = 0
158 var r: i64 = 1
159 while r < argc {
160 let req: *u8 = argv[r] as *u8
161 let rlen: i64 = pg_slen(req)
162 var cnt: i64 = 0
163 var shown: i64 = 0
164 var k: i64 = 0
165 while k < nb {
166 let ostart: i64 = bounds[k]
167 var oend: i64 = arrend
168 if k + 1 < nb { oend = bounds[k+1] }
169 var ce: i64 = oend
170 if ce > ostart { if buf[ce-1] == (44 as u8) { ce = ce - 1 } }
171 // build the value-only haystack for this object
172 var hp: i64 = 0
173 let dl0: i64 = pg_id(buf, ostart, ce, idb)
174 var z: i64 = 0
175 while z < dl0 { if hp < K_MAGIC_4090 { hs[hp] = idb[z]; hp = hp + 1 } z = z + 1 }
176 hp = pg_appendval(hs, hp, buf, ostart, ce, "domain" as *u8, 6)
177 hp = pg_appendval(hs, hp, buf, ostart, ce, "capabilities" as *u8, 12)
178 hp = pg_appendval(hs, hp, buf, ostart, ce, "title" as *u8, 5)
179 hp = pg_appendval(hs, hp, buf, ostart, ce, "output" as *u8, 6)
180 hp = pg_appendval(hs, hp, buf, ostart, ce, "signature" as *u8, 9)
181 if rlen > 0 { if pg_has(hs, 0, hp, req, rlen) == 1 { cnt = cnt + 1; if shown < 8 { mstart[shown] = ostart; shown = shown + 1 } } }
182 k = k + 1
183 }
184 if r > 1 { p = pg_b(rep, p, "," as *u8) }
185 p = pg_b(rep, p, "{" as *u8)
186 p = pg_q(rep, p); p = pg_b(rep, p, "capability" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_je(rep, p, req, 0, rlen); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
187 p = pg_q(rep, p); p = pg_b(rep, p, "match_count" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_bn(rep, p, cnt); p = pg_b(rep, p, "," as *u8)
188 if cnt > 0 {
189 satisfied = satisfied + 1
190 p = pg_q(rep, p); p = pg_b(rep, p, "status" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "HAVE" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
191 p = pg_q(rep, p); p = pg_b(rep, p, "matches" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":[" as *u8)
192 var s2: i64 = 0
193 while s2 < shown {
194 let os2: i64 = mstart[s2]
195 var oe2: i64 = arrend
196 // recompute this object's ce for id extraction bound
197 var kk: i64 = 0
198 var nextb: i64 = arrend
199 while kk < nb { if bounds[kk] == os2 { if kk + 1 < nb { nextb = bounds[kk+1] } kk = nb } else { kk = kk + 1 } }
200 oe2 = nextb
201 if oe2 > os2 { if buf[oe2-1] == (44 as u8) { oe2 = oe2 - 1 } }
202 let dl2: i64 = pg_id(buf, os2, oe2, idb)
203 if s2 > 0 { p = pg_b(rep, p, "," as *u8) }
204 p = pg_q(rep, p); p = pg_je(rep, p, idb, 0, dl2); p = pg_q(rep, p)
205 s2 = s2 + 1
206 }
207 p = pg_b(rep, p, "]" as *u8)
208 } else {
209 missing = missing + 1
210 p = pg_q(rep, p); p = pg_b(rep, p, "status" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "MISSING-PRIMITIVE" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
211 p = pg_q(rep, p); p = pg_b(rep, p, "structural_spec" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":{" as *u8)
212 p = pg_q(rep, p); p = pg_b(rep, p, "top_down_requirement" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "a sovereign primitive exposing capability " as *u8); p = pg_je(rep, p, req, 0, rlen); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
213 p = pg_q(rep, p); p = pg_b(rep, p, "bottom_up_availability" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "0 of " as *u8); p = pg_bn(rep, p, nb); p = pg_b(rep, p, " registry primitives match" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
214 p = pg_q(rep, p); p = pg_b(rep, p, "action_required" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "author a sovereign organ providing this capability, OR a bridge adapter over the nearest existing primitives (query neighbors via nx_prim_query), then re-run nx_prim_registry so it enters the graph" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "," as *u8)
215 p = pg_q(rep, p); p = pg_b(rep, p, "file_as" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "nx_debt add 6 prim MISSING-PRIMITIVE " as *u8); p = pg_je(rep, p, req, 0, rlen); p = pg_b(rep, p, " + nx_frontier_put add F-next prim lane" as *u8); p = pg_q(rep, p)
216 p = pg_b(rep, p, "}" as *u8)
217 }
218 p = pg_b(rep, p, "}" as *u8)
219 r = r + 1
220 }
221 p = pg_b(rep, p, "]," as *u8)
222 p = pg_q(rep, p); p = pg_b(rep, p, "requirements_scanned" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_bn(rep, p, argc - 1); p = pg_b(rep, p, "," as *u8)
223 p = pg_q(rep, p); p = pg_b(rep, p, "satisfied" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_bn(rep, p, satisfied); p = pg_b(rep, p, "," as *u8)
224 p = pg_q(rep, p); p = pg_b(rep, p, "missing_primitives" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_bn(rep, p, missing); p = pg_b(rep, p, "," as *u8)
225 p = pg_q(rep, p); p = pg_b(rep, p, "registry_primitives" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_bn(rep, p, nb); p = pg_b(rep, p, "," as *u8)
226 p = pg_q(rep, p); p = pg_b(rep, p, "note" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, ":" as *u8); p = pg_q(rep, p); p = pg_b(rep, p, "intent->capabilities is the agent-seat judgment; this organ is the deterministic collision + structural-spec draft. Match is value-field substring (case-sensitive); a MISSING may still have a semantic neighbor -- query nx_prim_query before authoring." as *u8); p = pg_q(rep, p)
227 p = pg_b(rep, p, "}" as *u8)
228 sys_write(1, rep, p)
229 let tmpfd: i64 = sys_openat_wr(tmppath, 420)
230 if tmpfd >= 0 { sys_write(tmpfd, rep, p); sys_close(tmpfd); sys_renameat(tmppath, outpath) }
231 if missing > 0 { sys_exit(3); return 3 }
232 sys_exit(0)
233 return 0
234}