code wiki / _hdl_build / nx_codewiki_lib.nx
nx_codewiki_lib.nx source
↩ module page · 4817 lines · 262144 B
1// nx_codewiki_lib.nx -- THE CODE WIKI: an always-regenerable wiki OF the source tree.
2//
3// ===== WHY THIS EXISTS ============================================
4//
5// The ecosystem has a charter wiki (runtime/wiki/, ~100 curated pages) and a
6// corpus search (nishi_search), but NOTHING that answers, for a given module:
7// what does it do, what does it import, WHO IMPORTS IT, what functions does
8// it export, and where do I look next. That is the Google-Code-Wiki class of
9// artifact: generated FROM the code, never hand-maintained, stale-proof
10// because regeneration is one command and the output is deterministic.
11//
12// It is deliberately a STATIC generator, not a daemon:
13// - the charter wiki daemon is sized for ~100 docs (docs_cap) and serves
14// public charter sites; 17k code pages do not belong in its pools
15// - static bytes are gateable (byte-identical re-run), diffable, and
16// servable by any route -- or browsable as local files with zero infra
17//
18// ===== WHAT IS REUSED (the dupfunc law: the tool must not duplicate) ======
19//
20// gl_walk / gl_read (nx_gatelib) -- the ONE tree walk + file read
21// nis_scan (nx_import_scan) -- the ONE accurate import scanner
22// EcoGraph (nx_eco_graph) -- intern + CSR fan-in/fan-out
23//
24// gl_walk's edge mode is NOT used: it interns import paths verbatim, so a
25// subdir import ("wiki/nx_foo.nx") would become a node DISTINCT from the
26// basename node of the actual file and every such link would dangle. We
27// intern the BASENAME of each import ourselves -- same convention as file
28// nodes -- so links resolve, and what still cannot resolve is reported as
29// an honest unresolved count instead of a dead href.
30//
31// ===== ENVELOPES (declared, per the scale law) =====================
32// CW_MAXNODE 24000 nodes | CW_EDGECAP 262144 edges | file cap GL_FILECAP
33// CW_PAGECAP 4 MiB/page -- overflow appends a LOUD PAGE-TRUNCATED banner
34// CW_MAXDIRS 256 | desc arena 2 MiB | per-file import dedup window 128
35//
36// license_tier: ORIGINAL
37import "nx_syscalls.nx"
38import "nx_eco_graph.nx"
39import "nx_gatelib.nx"
40import "nx_import_scan.nx"
41const CW_MAGIC_2166136261: i64 = 2166136261
42const CW_MAGIC_16777619: i64 = 16777619
43const CW_MAGIC_1024: i64 = 1024
44const CW_MAGIC_65536: i64 = 65536
45const CW_MAGIC_262144: i64 = 262144
46const CW_MAGIC_86400: i64 = 86400
47const CW_MAGIC_131072: i64 = 131072
48const CW_MAGIC_4096: i64 = 4096
49const CW_MAGIC_1469598103934665603: i64 = 1469598103934665603
50const CW_MAGIC_1099511628211: i64 = 1099511628211
51
52const CW_MAXNODE: i64 = 24000 // units=modules; tree measured 17,491 today (2026-07-29 census) = 37% headroom
53const CW_ARENA: i64 = 4194304
54const CW_HASH: i64 = 65536
55const CW_PATHARENA: i64 = 8388608
56const CW_EDGECAP: i64 = 262144 // units=import edges; measured 44,399 (2026-07-27) = 5.9x headroom
57// 4 MiB: the root-directory listing alone is ~2.1 MiB (10702 rows); a module
58// page tops out near ~1 MiB worst-case (256 KiB source fully escaped).
59const CW_PAGECAP: i64 = 4194304 // units=bytes; == the measured 4MB edge serving cap (a bigger page cannot ship); overflow -> loud banner (T8)
60const CW_PAGERESERVE: i64 = 192
61const CW_MAXDIRS: i64 = 256 // units=dirs; tree measured 13 today -- headroom visible on purpose
62const CW_DESCARENA: i64 = 2097152
63const CW_DESCMAX: i64 = 160
64const CW_TOPK: i64 = 20
65const CW_IMPDEDUP: i64 = 128
66const CW_SHOWIN: i64 = 100
67// function index (search): every top-level func across the tree, DECLARED cap
68const CW_MAXFN: i64 = 131072 // units=funcs; measured 91,067 today (2026-07-29 regen) = 1.4x headroom; cap declared on the search page
69const CW_FNARENA: i64 = 4194304
70// architecture SVG: hubs plotted, DECLARED cap
71const CW_SVGN: i64 = 36
72// topic families: derived from the tree's own naming discipline (nx_<fam>_*)
73const CW_MAXFAM: i64 = 4096 // units=families; measured ~750 today (threshold-4 rule) = 5x headroom
74const CW_FAMARENA: i64 = 131072
75const CW_FAMMIN: i64 = 4
76const CW_SBFAM: i64 = 16
77// call graph: defs share CW_MAXFN; sites/edges DECLARED caps
78const CW_MAXSITE: i64 = 2097152
79const CW_SITEARENA: i64 = 33554432
80const CW_DEFHASH: i64 = 262144
81const CW_CALLSHOW: i64 = 6
82const CW_CALLDEDUP: i64 = 64
83// module KINDS -- what each file IS, by measurable rule (precedence declared
84// on the census page; first match wins; "generated fixture" needs either the
85// literal marker in its own header or family-scale + tiny size, both measured)
86const CW_NKIND: i64 = 11
87const CW_K_ARCHIVED: i64 = 0
88const CW_K_GATE: i64 = 1
89const CW_K_BENCH: i64 = 2
90const CW_K_PROBE: i64 = 3
91const CW_K_SMOKE: i64 = 4
92const CW_K_SKETCH: i64 = 5
93const CW_K_GENFIX: i64 = 6
94const CW_K_SERVICE: i64 = 7
95const CW_K_TOOL: i64 = 8
96const CW_K_LIB: i64 = 9
97const CW_K_ORPHAN: i64 = 10
98const CW_GENFIX_FAM: i64 = 200
99const CW_GENFIX_LOC: i64 = 40
100const CW_KINDSHOW: i64 = 24
101// findings: detectors -> the WORK BACKLOG. Each finding is one line in the
102// exact nx_debt grammar (sev<TAB>scope<TAB>desc) with a STABLE description --
103// no counts, no dates -- so the filing bridge is idempotent by construction
104// (nx_debt dedups on exact desc). Live numbers stay on the wiki pages the
105// descs cite; the debt row is the pointer, the wiki is the gauge.
106const CW_FINDCAP: i64 = 65536 // units=bytes; findings feed measured ~30 rows x ~200B today = 10x headroom
107const CW_SPRAWLMAX: i64 = 12
108// DEBT TAXONOMY -- the categories the operator named. Each is detected by
109// markers a module carries in its OWN comments (the tree already documents its
110// hazards; the wiki just makes them countable + backlog-fed). One bit per
111// category in a per-module mask; a marker-scan in pass 1 sets the bits.
112const CW_NCAT: i64 = 8
113const CW_CAT_LANDMINE: i64 = 0
114const CW_CAT_WORKAROUND: i64 = 1
115const CW_CAT_GOTCHA: i64 = 2
116const CW_CAT_TODO: i64 = 3
117const CW_CAT_THIRDPARTY: i64 = 4
118const CW_CAT_SECURITY: i64 = 5
119const CW_CAT_PERF: i64 = 6
120const CW_CAT_DEPRECATED: i64 = 7
121const CW_CATSHOW: i64 = 20
122const CW_LANGSHOW: i64 = 24
123// TARGETED findings: for the severe categories, name INDIVIDUAL modules so each
124// is a pickable work item, ranked by fan-in (blast radius). Capped so the board
125// gets actionable rows, not a flood; the aggregate finding still covers the tail.
126const CW_TGTCAP: i64 = 8
127const CW_TGTSEV: i64 = 7
128// exposure join (where MCP/APIs are needed): input caps DECLARED
129const CW_EXPCONF: i64 = 1048576
130const CW_EXPELFS: i64 = 4096 // units=elfs; deployed set measured ~700 (496 exposed + 211 dark) = 5x headroom
131const CW_EXPARENA: i64 = 262144
132const CW_EXPSHOW: i64 = 40
133// retrieval (ask): index read cap, hit cap, question-token cap -- all DECLARED
134// searchable header text per module (the doc block, not just line 1): the
135// difference between "the first sentence mentions it" and "the file explains
136// itself". Display still uses the short desc; retrieval reads this.
137const CW_SDOCMAX: i64 = 1400
138const CW_SDOCARENA: i64 = 41943040
139const CW_ASKDESCMAX: i64 = 150
140const CW_ASKCAP: i64 = 41943040
141// ★The ask index is a DATA FILE, not a page: it must never share the page
142// buffer's 4 MiB envelope. It silently held 10% of the corpus for a whole
143// evening because it did -- the truncation banner fired into the TSV and the
144// count sat in a line of output nobody read. Own buffer, own cap, and a
145// LOUD REFUSAL if it ever overflows again.
146const CW_ASKIDXCAP: i64 = 67108864 // units=bytes; sized for the measured 108.9k-record corpus w/ headroom; truncation REFUSES loudly (T33 + rc=4)
147const CW_ASKTOP: i64 = 10
148const CW_ASKQTOK: i64 = 16
149const CW_ASKMINTOK: i64 = 3
150// RARITY WEIGHTING, in permil of the corpus (relative, so the bands hold for a
151// 16-record fixture and a 103k-record tree alike). A token in 0.5% of records
152// carries far more signal than one in 30% -- flat scoring let "function" drown
153// "duplicate" on a real question, which is how this was found.
154const CW_IDF_P1: i64 = 5
155const CW_IDF_W1: i64 = 12
156const CW_IDF_P2: i64 = 30
157const CW_IDF_W2: i64 = 8
158const CW_IDF_P3: i64 = 100
159const CW_IDF_W3: i64 = 5
160const CW_IDF_P4: i64 = 300
161const CW_IDF_W4: i64 = 3
162const CW_IDF_W5: i64 = 1
163const CW_ASKNAMEX: i64 = 2
164
165// ---------- tiny string/number helpers (ordered before use) ----------
166
167func cw_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
168
169// bounded append; pos[0]=len, pos[1]=overflow flag. Reserve keeps room for the banner.
170func cw_app(buf: *u8, pos: *i64, cap: i64, s: *u8, n: i64) -> i64 {
171 if pos[0] + n >= cap - CW_PAGERESERVE { pos[1] = 1; return 0 }
172 var i: i64 = 0
173 while i < n { buf[pos[0] + i] = s[i]; i = i + 1 }
174 pos[0] = pos[0] + n
175 return 0
176}
177
178func cw_apps(buf: *u8, pos: *i64, cap: i64, s: *u8) -> i64 { return cw_app(buf, pos, cap, s, cw_slen(s)) }
179
180// append a decimal number, in place (digits written then reversed -- zero allocation).
181func cw_appn(buf: *u8, pos: *i64, cap: i64, v: i64) -> i64 {
182 if pos[0] + 24 >= cap - CW_PAGERESERVE { pos[1] = 1; return 0 }
183 var m: i64 = v
184 if m < 0 { buf[pos[0]] = 45 as u8; pos[0] = pos[0] + 1; m = 0 - m }
185 let start: i64 = pos[0]
186 if m == 0 { buf[pos[0]] = 48 as u8; pos[0] = pos[0] + 1; return 0 }
187 while m > 0 { buf[pos[0]] = (48 + (m % 10)) as u8; pos[0] = pos[0] + 1; m = m / 10 }
188 var a: i64 = start
189 var b: i64 = pos[0] - 1
190 while a < b {
191 let t: u8 = buf[a]; buf[a] = buf[b]; buf[b] = t
192 a = a + 1; b = b - 1
193 }
194 return 0
195}
196
197// HTML-escaped append of raw source-derived bytes.
198func cw_appe(buf: *u8, pos: *i64, cap: i64, s: *u8, n: i64) -> i64 {
199 var i: i64 = 0
200 while i < n {
201 let c: i64 = s[i] as i64
202 if c == 38 { cw_app(buf, pos, cap, "&" as *u8, 5) }
203 if c == 60 { cw_app(buf, pos, cap, "<" as *u8, 4) }
204 if c == 62 { cw_app(buf, pos, cap, ">" as *u8, 4) }
205 if c != 38 { if c != 60 { if c != 62 {
206 if pos[0] + 1 >= cap - CW_PAGERESERVE { pos[1] = 1 } else { buf[pos[0]] = s[i]; pos[0] = pos[0] + 1 }
207 } } }
208 i = i + 1
209 }
210 return 0
211}
212
213// write buf to path; if the overflow flag is set, append the LOUD banner first.
214// A silently clipped page is indistinguishable from a complete one -- the
215// banner is the honesty device, and the gate has a tooth for it.
216func cw_flush(path: *u8, buf: *u8, pos: *i64, cap: i64) -> i64 {
217 if pos[1] == 1 {
218 let bn: *u8 = "\n<p class='trunc'>PAGE-TRUNCATED at declared cap</p><!-- PAGE-TRUNCATED -->\n" as *u8
219 let bl: i64 = cw_slen(bn)
220 var i: i64 = 0
221 while i < bl { buf[pos[0] + i] = bn[i]; i = i + 1 }
222 pos[0] = pos[0] + bl
223 }
224 let fd: i64 = sys_openat_wr(path, 420)
225 if fd < 0 { return 0 - 1 }
226 var off: i64 = 0
227 while off < pos[0] {
228 let w: i64 = sys_write(fd, ((buf as i64) + off) as *u8, pos[0] - off)
229 if w <= 0 { sys_close(fd); return 0 - 1 }
230 off = off + w
231 }
232 sys_close(fd)
233 return 0
234}
235
236// basename start offset within a nul-terminated path
237func cw_base(path: *u8) -> i64 {
238 var i: i64 = 0
239 var last: i64 = 0
240 while path[i] != (0 as u8) {
241 if path[i] == (47 as u8) { last = i + 1 }
242 i = i + 1
243 }
244 return last
245}
246
247// stem length of a basename (minus trailing .nx)
248func cw_stemlen(nm: *u8) -> i64 {
249 let n: i64 = cw_slen(nm)
250 if gl_is_nx(nm, n) == 1 { return n - 3 }
251 return n
252}
253
254func cw_rawcpy(dst: *u8, at: i64, s: *u8) -> i64 {
255 var i: i64 = 0
256 while s[i] != (0 as u8) { dst[at + i] = s[i]; i = i + 1 }
257 return i
258}
259
260// build outdir/<stem>.html into dst; a module literally named index.nx would
261// clobber the wiki index, so it gets index.mod.html (guard, declared).
262func cw_pagepath(dst: *u8, outdir: *u8, nm: *u8) -> i64 {
263 var w: i64 = 0
264 var i: i64 = 0
265 while outdir[i] != (0 as u8) { dst[w] = outdir[i]; w = w + 1; i = i + 1 }
266 dst[w] = 47 as u8; w = w + 1
267 let sl: i64 = cw_stemlen(nm)
268 var guard: i64 = 0
269 if sl == 5 {
270 if nm[0] == (105 as u8) { if nm[1] == (110 as u8) { if nm[2] == (100 as u8) { if nm[3] == (101 as u8) { if nm[4] == (120 as u8) { guard = 1 } } } } }
271 }
272 var j: i64 = 0
273 while j < sl { dst[w] = nm[j]; w = w + 1; j = j + 1 }
274 if guard == 1 { cw_rawcpy(dst, w, ".mod" as *u8); w = w + 4 }
275 cw_rawcpy(dst, w, ".html" as *u8)
276 w = w + 5
277 dst[w] = 0 as u8
278 return w
279}
280
281// append <a href='stem[.html]'>name</a> for a node known to have a page.
282// noext=1 emits extensionless hrefs (the edge's canonical form -- kills the
283// per-click 301); files on disk are ALWAYS <stem>.html either way.
284func cw_applink2(buf: *u8, pos: *i64, cap: i64, nm: *u8, noext: i64) -> i64 {
285 cw_apps(buf, pos, cap, "<a href='" as *u8)
286 let sl: i64 = cw_stemlen(nm)
287 cw_app(buf, pos, cap, nm, sl)
288 var guard: i64 = 0
289 if sl == 5 {
290 if nm[0] == (105 as u8) { if nm[1] == (110 as u8) { if nm[2] == (100 as u8) { if nm[3] == (101 as u8) { if nm[4] == (120 as u8) { guard = 1 } } } } }
291 }
292 if guard == 1 { cw_apps(buf, pos, cap, ".mod" as *u8) }
293 if noext == 0 { cw_apps(buf, pos, cap, ".html" as *u8) }
294 cw_apps(buf, pos, cap, "'>" as *u8)
295 cw_appe(buf, pos, cap, nm, cw_slen(nm))
296 cw_apps(buf, pos, cap, "</a>" as *u8)
297 return 0
298}
299
300func cw_applink(buf: *u8, pos: *i64, cap: i64, nm: *u8) -> i64 {
301 return cw_applink2(buf, pos, cap, nm, 0)
302}
303
304// href suffix for generated non-module pages (search/arch/dir/index)
305func cw_appext(buf: *u8, pos: *i64, cap: i64, noext: i64) -> i64 {
306 if noext == 0 { cw_apps(buf, pos, cap, ".html" as *u8) }
307 return 0
308}
309
310// JS-string-escaped append (into generated .js index files): \ and " escaped,
311// CR dropped, < emitted as \x3c so a desc can never close the script context.
312func cw_appjse(buf: *u8, pos: *i64, cap: i64, s: *u8, n: i64) -> i64 {
313 var i: i64 = 0
314 while i < n {
315 let c: i64 = s[i] as i64
316 var done: i64 = 0
317 if c == 92 { cw_app(buf, pos, cap, "\\\\" as *u8, 2); done = 1 }
318 if c == 34 { cw_app(buf, pos, cap, "\\\"" as *u8, 2); done = 1 }
319 if c == 60 { cw_app(buf, pos, cap, "\\x3c" as *u8, 4); done = 1 }
320 if c == 13 { done = 1 }
321 if c == 10 { done = 1 }
322 if done == 0 {
323 if pos[0] + 1 >= cap - CW_PAGERESERVE { pos[1] = 1 } else { buf[pos[0]] = s[i]; pos[0] = pos[0] + 1 }
324 }
325 i = i + 1
326 }
327 return 0
328}
329
330// ---------- shared page chrome (literals kept well under the nx_cc ceiling) ----------
331
332// The app shell: a fixed sidebar + main column, tokenized dark design.
333// CSS split into small literals (nx_cc literal ceiling).
334func cw_css(buf: *u8, pos: *i64, cap: i64) -> i64 {
335 // docs-grade light theme first; dark honored via prefers-color-scheme.
336 cw_apps(buf, pos, cap, ":root{--bg:#ffffff;--sf:#f6f8fb;--sf2:#eef1f6;--br:#dfe4ec;--ink:#232936;--mut:#68748a;--acc:#1a5dc8;--acc2:#2e8555;--hd:#111726;--hov:#f0f4fa;--hl:#fff3c4;--svgb:#eef2f8;--svgs:#b9c4d8;--svgt:#28406b;--svgc:#dbe4f2}\n" as *u8)
337 cw_apps(buf, pos, cap, "@media(prefers-color-scheme:dark){:root{--bg:#0d1017;--sf:#131826;--sf2:#1a2132;--br:#252d40;--ink:#dbe2ec;--mut:#8a94a8;--acc:#5aa7f0;--acc2:#86c793;--hd:#eef2f8;--hov:#141a28;--hl:#22304d;--svgb:#1a2132;--svgs:#33405c;--svgt:#9fc0e8;--svgc:#20304a}}\n" as *u8)
338 cw_apps(buf, pos, cap, "*{box-sizing:border-box}body{font-family:system-ui,'Segoe UI',sans-serif;margin:0;background:var(--bg);color:var(--ink);display:grid;grid-template-columns:252px minmax(0,1fr);min-height:100vh}\n" as *u8)
339 cw_apps(buf, pos, cap, "a{color:var(--acc);text-decoration:none}a:hover{text-decoration:underline}\n" as *u8)
340 cw_apps(buf, pos, cap, "nav.sb{background:var(--sf);border-right:1px solid var(--br);padding:14px 12px;position:sticky;top:0;height:100vh;overflow-y:auto;font-size:.9em}\n" as *u8)
341 cw_apps(buf, pos, cap, ".sb .brand{display:block;font-weight:700;color:var(--hd);font-size:1.02em;margin:2px 6px 12px}\n" as *u8)
342 cw_apps(buf, pos, cap, ".sb a.it{display:block;padding:4px 8px;border-radius:6px;color:var(--ink)}.sb a.it:hover{background:var(--sf2);text-decoration:none;color:var(--hd)}\n" as *u8)
343 cw_apps(buf, pos, cap, ".sb .sec{margin:14px 6px 4px;font-size:.74em;letter-spacing:.09em;text-transform:uppercase;color:var(--mut)}\n" as *u8)
344 cw_apps(buf, pos, cap, ".sb .ct{float:right;color:var(--mut);font-size:.85em;font-variant-numeric:tabular-nums}\n" as *u8)
345 cw_apps(buf, pos, cap, "main{padding:22px 30px 40px;max-width:1080px;min-width:0}\n" as *u8)
346 cw_apps(buf, pos, cap, ".crumb{font-size:.85em;color:var(--mut);margin-bottom:10px}.crumb a{color:var(--mut)}.crumb a:hover{color:var(--acc)}\n" as *u8)
347 cw_apps(buf, pos, cap, "h1{font-size:1.55em;color:var(--hd);margin:2px 0 6px;letter-spacing:-.01em}\n" as *u8)
348 cw_apps(buf, pos, cap, "h2{font-size:1.02em;color:var(--hd);margin:26px 0 8px;padding-bottom:5px;border-bottom:1px solid var(--br)}h2 .cnt{color:var(--mut);font-weight:400;font-size:.85em}\n" as *u8)
349 cw_apps(buf, pos, cap, "code,pre,.sig{font-family:ui-monospace,'Cascadia Code',Consolas,monospace;font-size:.86em}\n" as *u8)
350 cw_apps(buf, pos, cap, ".meta{display:flex;flex-wrap:wrap;gap:6px;margin:10px 0 4px}.chip{background:var(--sf);border:1px solid var(--br);border-radius:20px;padding:2px 11px;font-size:.8em;color:var(--ink)}.chip b{color:var(--hd);font-weight:600}\n" as *u8)
351 cw_apps(buf, pos, cap, ".toc{display:flex;flex-wrap:wrap;gap:6px;margin:8px 0 2px}.toc a{background:var(--sf2);border-radius:6px;padding:2px 10px;font-size:.8em}\n" as *u8)
352 cw_apps(buf, pos, cap, ".fnc{margin-top:4px;font-size:.86em}.fnc .lab{color:var(--mut);font-size:.85em}\n" as *u8)
353 cw_apps(buf, pos, cap, ".cta{display:inline-block;background:var(--acc);color:#fff !important;border-radius:8px;padding:9px 20px;margin:8px 8px 8px 0;font-size:.95em}.cta:hover{text-decoration:none;filter:brightness(1.1)}\n" as *u8)
354 cw_apps(buf, pos, cap, ".doc{background:var(--sf);border:1px solid var(--br);border-left:3px solid var(--acc);border-radius:8px;padding:12px 16px;white-space:pre-wrap;font-size:.88em;color:var(--ink);line-height:1.55;overflow-x:auto}\n" as *u8)
355 cw_apps(buf, pos, cap, ".nar{background:var(--sf);border:1px solid var(--br);border-left:3px solid var(--acc2);border-radius:8px;padding:12px 16px;font-size:.92em;line-height:1.6}\n" as *u8)
356 cw_apps(buf, pos, cap, ".tiles{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px;margin:14px 0}.tile{background:var(--sf);border:1px solid var(--br);border-radius:10px;padding:12px 14px}.tile .v{font-size:1.5em;color:var(--hd);font-variant-numeric:tabular-nums}.tile .k{font-size:.78em;color:var(--mut);margin-top:2px}\n" as *u8)
357 cw_apps(buf, pos, cap, "table{border-collapse:collapse;width:100%}td,th{padding:4px 10px;border-bottom:1px solid var(--br);text-align:left;vertical-align:top}th{color:var(--mut);font-size:.85em}\n" as *u8)
358 cw_apps(buf, pos, cap, "tr:hover td{background:var(--hov)}.n{text-align:right;color:var(--mut);font-variant-numeric:tabular-nums}.muted{color:var(--mut)}.ext{color:#a89a6e}\n" as *u8)
359 cw_apps(buf, pos, cap, ".trunc{background:#4a1f1f;color:#ffb0b0;padding:6px 10px;font-weight:bold}\n" as *u8)
360 cw_apps(buf, pos, cap, ".pill{display:inline-block;background:var(--sf2);border:1px solid var(--br);border-radius:14px;padding:1px 10px;margin:2px 4px 2px 0;font-size:.83em}\n" as *u8)
361 cw_apps(buf, pos, cap, "input{background:var(--sf);color:var(--ink);border:1px solid var(--br);border-radius:8px;padding:7px 12px;margin:8px 0;width:340px;font-size:.95em}input:focus{outline:1px solid var(--acc)}\n" as *u8)
362 cw_apps(buf, pos, cap, ".svgwrap{overflow-x:auto;background:var(--sf);border:1px solid var(--br);border-radius:10px;padding:8px;margin:8px 0}\n" as *u8)
363 cw_apps(buf, pos, cap, ".srcv{background:var(--sf);border:1px solid var(--br);border-radius:8px;padding:8px 0;overflow-x:auto;font-family:ui-monospace,Consolas,monospace;font-size:.84em;line-height:1.5}\n" as *u8)
364 cw_apps(buf, pos, cap, ".srcv .l{display:block;padding:0 14px;white-space:pre}.srcv .l:target{background:var(--hl)}.srcv .ln{display:inline-block;min-width:52px;text-align:right;padding-right:16px;color:var(--mut)}\n" as *u8)
365 cw_apps(buf, pos, cap, "footer{margin-top:36px;padding-top:10px;border-top:1px solid var(--br);font-size:.8em;color:var(--mut)}\n" as *u8)
366 cw_apps(buf, pos, cap, ".vd{background:linear-gradient(135deg,var(--sf),var(--sf2));border:1px solid var(--br);border-radius:14px;padding:22px 26px;margin:14px 0}.vd .big{font-size:1.7em;font-weight:700;color:var(--hd);letter-spacing:-.01em}.vd .sub{color:var(--mut);margin-top:6px;line-height:1.55;max-width:70ch}\n" as *u8)
367 cw_apps(buf, pos, cap, ".kpis{display:grid;grid-template-columns:repeat(auto-fit,minmax(215px,1fr));gap:12px;margin:14px 0}.pk{background:var(--sf);border:1px solid var(--br);border-radius:12px;padding:16px 18px}.pk .lab{font-size:.76em;letter-spacing:.06em;text-transform:uppercase;color:var(--mut)}.pk .val{font-size:2.1em;font-weight:700;color:var(--hd);font-variant-numeric:tabular-nums;margin:2px 0}\n" as *u8)
368 cw_apps(buf, pos, cap, ".pk .d{display:inline-block;border-radius:12px;padding:1px 9px;font-size:.8em;font-weight:600}.pk .dg{background:color-mix(in srgb,var(--acc2) 16%,transparent);color:var(--acc2)}.pk .db{background:color-mix(in srgb,#c0392b 14%,transparent);color:#c0392b}.pk .goal{font-size:.75em;color:var(--mut);margin-top:5px}\n" as *u8)
369 cw_apps(buf, pos, cap, "@media(max-width:880px){body{grid-template-columns:1fr}nav.sb{position:static;height:auto;border-right:0;border-bottom:1px solid var(--br)}}\n" as *u8)
370 return 0
371}
372
373// head3: doctype + css + sidebar (sb = prebuilt nul-terminated html) + <main>
374func cw_head3(buf: *u8, pos: *i64, cap: i64, title: *u8, tn: i64, sb: *u8) -> i64 {
375 cw_apps(buf, pos, cap, "<!doctype html>\n<html lang='en'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'>\n<title>" as *u8)
376 cw_appe(buf, pos, cap, title, tn)
377 cw_apps(buf, pos, cap, " - nishi code wiki</title>\n<style>\n" as *u8)
378 cw_css(buf, pos, cap)
379 cw_apps(buf, pos, cap, "</style></head><body>\n" as *u8)
380 cw_apps(buf, pos, cap, sb)
381 // AD ANCHOR (2026-07-31). The edge injects the labeled ad slot at this marker; with NO marker it
382 // falls back to just before the closing body tag -- the absolute bottom of the page, where a
383 // reference page of tables means essentially nobody scrolls. MRC viewability needs 50% of the
384 // creative in view for one CONTINUOUS second, so footer placement made viewable impressions
385 // structurally near-zero: we measured honestly and still sold a position nobody sees.
386 // TOP-OF-CONTENT IS THE FORMAT-APPROPRIATE POSITION, not a preference: the creative is 728x90,
387 // the IAB leaderboard, which exists for exactly this slot. The size dictates the convention.
388 // TO MOVE OR REMOVE: delete this one emit line (pages regenerate with no marker and the edge
389 // reverts to the old footer behaviour), or emit the marker from a later block instead.
390 cw_apps(buf, pos, cap, "<main>\n" as *u8)
391 cw_apps(buf, pos, cap, "<!--nx-ad-slot-->\n" as *u8)
392 return 0
393}
394
395// breadcrumb row: code wiki / <dir> / <self>
396func cw_crumb(buf: *u8, pos: *i64, cap: i64, noext: i64, dlabel: *u8, dslug: *u8, self: *u8, sn: i64) -> i64 {
397 cw_apps(buf, pos, cap, "<div class='crumb'><a href='index" as *u8)
398 cw_appext(buf, pos, cap, noext)
399 cw_apps(buf, pos, cap, "'>code wiki</a>" as *u8)
400 if (dslug as i64) != 0 {
401 cw_apps(buf, pos, cap, " / <a href='" as *u8)
402 cw_apps(buf, pos, cap, dslug)
403 cw_appext(buf, pos, cap, noext)
404 cw_apps(buf, pos, cap, "'>" as *u8)
405 if cw_slen(dlabel) == 0 { cw_apps(buf, pos, cap, "(root)" as *u8) } else { cw_appe(buf, pos, cap, dlabel, cw_slen(dlabel)) }
406 cw_apps(buf, pos, cap, "</a>" as *u8)
407 }
408 if sn > 0 {
409 cw_apps(buf, pos, cap, " / " as *u8)
410 cw_appe(buf, pos, cap, self, sn)
411 }
412 cw_apps(buf, pos, cap, "</div>\n" as *u8)
413 return 0
414}
415
416func cw_foot(buf: *u8, pos: *i64, cap: i64) -> i64 {
417 cw_apps(buf, pos, cap, "\n<footer>generated by nx_codewiki from the source tree -- every number measured, nothing hand-edited. regenerate any time.</footer>\n</main></body></html>\n" as *u8)
418 return 0
419}
420
421// the tiny client-side filter used on listing pages (plain JS -- the CSP
422// doctrine allows plain script, and locally it is just a file).
423func cw_filterjs(buf: *u8, pos: *i64, cap: i64) -> i64 {
424 cw_apps(buf, pos, cap, "<input id='q' placeholder='filter...' oninput='f()'>\n<script>\n" as *u8)
425 cw_apps(buf, pos, cap, "function f(){var q=document.getElementById('q').value.toLowerCase();var r=document.querySelectorAll('tr[data-m]');for(var i=0;i<r.length;i++){var m=r[i].getAttribute('data-m');r[i].style.display=(m.indexOf(q)>=0)?'':'none';}}\n" as *u8)
426 cw_apps(buf, pos, cap, "</script>\n" as *u8)
427 return 0
428}
429
430// ---------- per-file line classification ----------
431
432func cw_line_starts(buf: *u8, ls: i64, le: i64, kw: *u8) -> i64 {
433 let kl: i64 = cw_slen(kw)
434 if ls + kl > le { return 0 }
435 var i: i64 = 0
436 while i < kl { if buf[ls + i] != kw[i] { return 0 } i = i + 1 }
437 return 1
438}
439
440// signature end: trim trailing spaces and a trailing '{'
441func cw_sigend(buf: *u8, ls: i64, le: i64) -> i64 {
442 var e: i64 = le
443 var go: i64 = 1
444 while go == 1 {
445 if e <= ls { go = 0 } else {
446 let c: i64 = buf[e - 1] as i64
447 if c == 123 { e = e - 1 } else {
448 if c == 32 { e = e - 1 } else {
449 if c == 9 { e = e - 1 } else { go = 0 }
450 }
451 }
452 }
453 }
454 return e
455}
456
457// extract quoted path range on an import line; returns start, writes len to out[0]; -1 if none
458func cw_impquote(buf: *u8, ls: i64, le: i64, out: *i64) -> i64 {
459 var q: i64 = ls
460 var s: i64 = 0 - 1
461 while q < le {
462 if buf[q] == (34 as u8) {
463 if s < 0 { s = q + 1 } else {
464 out[0] = q - s
465 return s
466 }
467 }
468 q = q + 1
469 }
470 return 0 - 1
471}
472
473// basename range within a quoted import path buf[s..s+n)
474func cw_impbase(buf: *u8, s: i64, n: i64, out: *i64) -> i64 {
475 var last: i64 = s
476 var i: i64 = s
477 while i < s + n {
478 if buf[i] == (47 as u8) { last = i + 1 }
479 i = i + 1
480 }
481 out[0] = (s + n) - last
482 return last
483}
484
485// ---------- graph analysis (transitive impact + layering) ----------
486//
487// Reach counting uses EPOCH STAMPS, not a per-node memset: clearing a 17k
488// visited array 17k times is ~580M byte-writes for ~800k useful steps. A
489// stamp array + monotonically increasing epoch makes each closure O(reachable).
490// Iterative explicit stack -- a 14k-node cone must not become 14k stack frames.
491// dirn 0 = out-closure (transitive imports), 1 = in-closure (transitive importers).
492// NEWEST SOURCE ANYWHERE IN A MODULE'S IMPORT CLOSURE (seq1721).
493//
494// The shipped-drift gauge first compared a deployed elf against its module's
495// OWN source, which under-reports badly: a binary is equally stale when a
496// DEPENDENCY changed and it was never rebuilt -- and that is the common case,
497// since a fix usually lands in a shared lib. Same epoch-stamped walk as
498// cw_reach_count (no per-node memset, explicit stack, O(reachable)); returns
499// max(srcmt) over {start} + everything start transitively imports.
500// dirn is fixed at 0 (out-closure = what this module depends on) because that
501// is the only direction that can invalidate a build.
502func cw_reach_maxmt(g: *EcoGraph, start: i64, stampv: *i64, epoch: i64, stack: *i64, srcmt: *i64) -> i64 {
503 var best: i64 = srcmt[start]
504 var sp: i64 = 0
505 stampv[start] = epoch
506 stack[sp] = start
507 sp = sp + 1
508 while sp > 0 {
509 sp = sp - 1
510 let u: i64 = stack[sp]
511 if srcmt[u] > best { best = srcmt[u] }
512 var p: i64 = g.out_head[u]
513 let e: i64 = g.out_head[u + 1]
514 while p < e {
515 let v: i64 = g.out_list[p]
516 if stampv[v] != epoch {
517 stampv[v] = epoch
518 stack[sp] = v
519 sp = sp + 1
520 }
521 p = p + 1
522 }
523 }
524 return best
525}
526
527func cw_reach_count(g: *EcoGraph, start: i64, dirn: i64, stampv: *i64, epoch: i64, stack: *i64) -> i64 {
528 var cnt: i64 = 0
529 var sp: i64 = 0
530 stampv[start] = epoch
531 stack[sp] = start
532 sp = sp + 1
533 while sp > 0 {
534 sp = sp - 1
535 let u: i64 = stack[sp]
536 var p: i64 = 0
537 var e: i64 = 0
538 if dirn == 0 { p = g.out_head[u]; e = g.out_head[u + 1] } else { p = g.in_head[u]; e = g.in_head[u + 1] }
539 while p < e {
540 var v: i64 = 0
541 if dirn == 0 { v = g.out_list[p] } else { v = g.in_list[p] }
542 if stampv[v] != epoch {
543 stampv[v] = epoch
544 cnt = cnt + 1
545 stack[sp] = v
546 sp = sp + 1
547 }
548 p = p + 1
549 }
550 }
551 return cnt
552}
553
554// longest-path depth over the import DAG: leaves (no imports) = 0, else
555// 1 + max(depth of imports). Memoized; recursion depth = longest chain (~tens).
556func cw_depth_of(g: *EcoGraph, u: i64, memo: *i64) -> i64 {
557 if memo[u] >= 0 { return memo[u] }
558 memo[u] = 0
559 var best: i64 = 0
560 var p: i64 = g.out_head[u]
561 let e: i64 = g.out_head[u + 1]
562 while p < e {
563 let d: i64 = cw_depth_of(g, g.out_list[p], memo) + 1
564 if d > best { best = d }
565 p = p + 1
566 }
567 memo[u] = best
568 return best
569}
570
571// ---------- deterministic ordering ----------
572//
573// getdents enumeration order is filesystem- and mount-state-dependent (a 9p
574// remount reordered it mid-session and silently changed the tree stamp with
575// zero content change). Every listing and the stamp therefore iterate a
576// NAME-SORTED index of file nodes, so identical tree content -> identical
577// bytes, on any filesystem. (Residual: for duplicate basenames the walk's
578// first-path-wins registration is still enumeration-order-dependent -- declared.)
579
580func cw_namecmp(g: *EcoGraph, a: i64, b: i64) -> i64 {
581 let sa: *u8 = ((g.arena as i64) + g.node_off[a]) as *u8
582 let sb: *u8 = ((g.arena as i64) + g.node_off[b]) as *u8
583 var i: i64 = 0
584 var r: i64 = 0
585 var go: i64 = 1
586 while go == 1 {
587 let ca: i64 = sa[i] as i64
588 let cb: i64 = sb[i] as i64
589 if ca < cb { r = 0 - 1; go = 0 }
590 if go == 1 { if ca > cb { r = 1; go = 0 } }
591 if go == 1 { if ca == 0 { go = 0 } }
592 i = i + 1
593 }
594 return r
595}
596
597// bottom-up merge sort of idx[0..n) by node name
598func cw_sortnodes(g: *EcoGraph, idx: *i64, tmp: *i64, n: i64) -> i64 {
599 var width: i64 = 1
600 while width < n {
601 var lo: i64 = 0
602 while lo < n {
603 var mid: i64 = lo + width
604 if mid > n { mid = n }
605 var hi: i64 = lo + width + width
606 if hi > n { hi = n }
607 var a: i64 = lo
608 var b: i64 = mid
609 var w: i64 = lo
610 while w < hi {
611 var takea: i64 = 0
612 if a < mid {
613 if b >= hi { takea = 1 } else {
614 if cw_namecmp(g, idx[a], idx[b]) <= 0 { takea = 1 }
615 }
616 }
617 if takea == 1 { tmp[w] = idx[a]; a = a + 1 } else { tmp[w] = idx[b]; b = b + 1 }
618 w = w + 1
619 }
620 lo = hi
621 }
622 var c: i64 = 0
623 while c < n { idx[c] = tmp[c]; c = c + 1 }
624 width = width + width
625 }
626 return 0
627}
628
629// stem (page basename without .html) for JS index rows -- same index.nx guard
630// as cw_applink2 so JS-built hrefs and real page names can never disagree.
631func cw_appstem(buf: *u8, pos: *i64, cap: i64, nm: *u8) -> i64 {
632 let sl: i64 = cw_stemlen(nm)
633 cw_app(buf, pos, cap, nm, sl)
634 var guard: i64 = 0
635 if sl == 5 {
636 if nm[0] == (105 as u8) { if nm[1] == (110 as u8) { if nm[2] == (100 as u8) { if nm[3] == (101 as u8) { if nm[4] == (120 as u8) { guard = 1 } } } } }
637 }
638 if guard == 1 { cw_apps(buf, pos, cap, ".mod" as *u8) }
639 return 0
640}
641
642// build outdir/<name> into dst (for the non-module emissions)
643func cw_outpath(dst: *u8, outdir: *u8, name: *u8) -> i64 {
644 var w: i64 = 0
645 var i: i64 = 0
646 while outdir[i] != (0 as u8) { dst[w] = outdir[i]; w = w + 1; i = i + 1 }
647 dst[w] = 47 as u8
648 w = w + 1
649 w = w + cw_rawcpy(dst, w, name)
650 dst[w] = 0 as u8
651 return w
652}
653
654// FNV bucket for the function-def name hash
655func cwc_hash(s: *u8, n: i64) -> i64 {
656 var h: i64 = CW_MAGIC_2166136261
657 var i: i64 = 0
658 while i < n { h = (h ^ (s[i] as i64)) * CW_MAGIC_16777619; h = h & 0x7fffffff; i = i + 1 }
659 return h % CW_DEFHASH
660}
661
662// scan a comment-STRIPPED buffer for defs AND call sites in ONE walk.
663// A def = line-start `func <name>`; a call site = identifier immediately
664// followed by '('. String literals are skipped so quoted code cannot fake a
665// call; comments are already gone (gl_strip_comments preserves line numbers).
666// Sites are attributed to the ENCLOSING function (the def just seen) and
667// stored UNRESOLVED (name text); resolution happens later against the
668// complete def table. Everything is appended to caller-owned tables.
669func cwc_scan(strip: *u8, sn: i64, mod: i64,
670 dnames: *u8, dnoff: *i64, dnlen: *i64, dmod: *i64, dline: *i64, dhead: *i64, dnext: *i64, dn: *i64, du: *i64,
671 snames: *u8, snoff: *i64, snlen: *i64, smod: *i64, scaller: *i64, ns: *i64, su: *i64) -> i64 {
672 var i: i64 = 0
673 var ln: i64 = 1
674 var atline: i64 = 1
675 var curdef: i64 = 0 - 1
676 while i < sn {
677 let c: i64 = strip[i] as i64
678 if c == 34 {
679 var j: i64 = i + 1
680 var go: i64 = 1
681 while go == 1 {
682 if j >= sn { go = 0 } else {
683 if strip[j] == (92 as u8) { j = j + 2 } else {
684 if strip[j] == (34 as u8) { go = 0 } else { j = j + 1 }
685 }
686 }
687 }
688 i = j + 1
689 atline = 0
690 } else {
691 if gl_is_ident(c) == 1 {
692 let st0: i64 = i
693 var e1: i64 = i
694 var ge: i64 = 1
695 while ge == 1 { if e1 >= sn { ge = 0 } else { if gl_is_ident(strip[e1] as i64) == 1 { e1 = e1 + 1 } else { ge = 0 } } }
696 let idl: i64 = e1 - st0
697 var isfunc: i64 = 0
698 if atline == 1 { if idl == 4 {
699 if strip[st0] == (102 as u8) { if strip[st0+1] == (117 as u8) { if strip[st0+2] == (110 as u8) { if strip[st0+3] == (99 as u8) { isfunc = 1 } } } }
700 } }
701 if isfunc == 1 {
702 var p1: i64 = e1
703 var gp: i64 = 1
704 while gp == 1 { if p1 >= sn { gp = 0 } else { if strip[p1] == (32 as u8) { p1 = p1 + 1 } else { gp = 0 } } }
705 var q1: i64 = p1
706 var gq: i64 = 1
707 while gq == 1 { if q1 >= sn { gq = 0 } else { if gl_is_ident(strip[q1] as i64) == 1 { q1 = q1 + 1 } else { gq = 0 } } }
708 let nl2: i64 = q1 - p1
709 if nl2 > 0 { if nl2 < 96 { if dn[0] < CW_MAXFN { if du[0] + nl2 + 1 < CW_FNARENA {
710 var cc0: i64 = 0
711 while cc0 < nl2 { dnames[du[0] + cc0] = strip[p1 + cc0]; cc0 = cc0 + 1 }
712 dnoff[dn[0]] = du[0]
713 dnlen[dn[0]] = nl2
714 dmod[dn[0]] = mod
715 dline[dn[0]] = ln
716 let b: i64 = cwc_hash(((strip as i64) + p1) as *u8, nl2)
717 dnext[dn[0]] = dhead[b]
718 dhead[b] = dn[0] + 1
719 du[0] = du[0] + nl2
720 dn[0] = dn[0] + 1
721 curdef = dn[0] - 1
722 } } } }
723 i = q1
724 atline = 0
725 } else {
726 if e1 < sn { if strip[e1] == (40 as u8) { if curdef >= 0 {
727 if ns[0] < CW_MAXSITE { if su[0] + idl + 1 < CW_SITEARENA { if idl < 96 {
728 var cc1: i64 = 0
729 while cc1 < idl { snames[su[0] + cc1] = strip[st0 + cc1]; cc1 = cc1 + 1 }
730 snoff[ns[0]] = su[0]
731 snlen[ns[0]] = idl
732 smod[ns[0]] = mod
733 scaller[ns[0]] = curdef
734 su[0] = su[0] + idl
735 ns[0] = ns[0] + 1
736 } } }
737 } } }
738 i = e1
739 atline = 0
740 }
741 } else {
742 if c == 10 { atline = 1; ln = ln + 1 } else { if c != 32 { if c != 9 { atline = 0 } } }
743 i = i + 1
744 }
745 }
746 }
747 return 0
748}
749
750// topic-family key of a module name: skip a leading "nx_", take the next
751// name segment (the tree's prefix discipline IS its concept map).
752func cw_famkey(nm: *u8, out: *u8) -> i64 {
753 var s: i64 = 0
754 if nm[0] == (110 as u8) { if nm[1] == (120 as u8) { if nm[2] == (95 as u8) { s = 3 } } }
755 // leading underscores are convention noise (_a2a_* is the a2a family)
756 while nm[s] == (95 as u8) { s = s + 1 }
757 var i: i64 = s
758 var w: i64 = 0
759 var go: i64 = 1
760 while go == 1 {
761 let c: i64 = nm[i] as i64
762 if c == 0 { go = 0 } else {
763 if c == 95 { go = 0 } else {
764 if c == 46 { go = 0 } else {
765 if w < 24 { out[w] = nm[i]; w = w + 1; i = i + 1 } else { go = 0 }
766 }
767 }
768 }
769 }
770 return w
771}
772
773// pill-link to a function definition: <modstem>[.html]#fn_<name>
774func cw_appdeflink(buf: *u8, pos: *i64, cap: i64, g: *EcoGraph, dmodv: *i64, dnamesv: *u8, dnoffv: *i64, dnlenv: *i64, dx: i64, noext: i64) -> i64 {
775 let mn: *u8 = ((g.arena as i64) + g.node_off[dmodv[dx]]) as *u8
776 cw_apps(buf, pos, cap, "<a class='pill' href='" as *u8)
777 cw_appstem(buf, pos, cap, mn)
778 cw_appext(buf, pos, cap, noext)
779 cw_apps(buf, pos, cap, "#fn_" as *u8)
780 cw_app(buf, pos, cap, ((dnamesv as i64) + dnoffv[dx]) as *u8, dnlenv[dx])
781 cw_apps(buf, pos, cap, "'>" as *u8)
782 cw_appe(buf, pos, cap, ((dnamesv as i64) + dnoffv[dx]) as *u8, dnlenv[dx])
783 cw_apps(buf, pos, cap, "</a>" as *u8)
784 return 0
785}
786
787// call-flow tree from an entry def: pre-order walk over the call CSR,
788// caps DECLARED (40 nodes, depth 6); a def already in the tree is recorded
789// once more with a cycle mark and not re-expanded. Emitted as a linked SVG.
790func cw_callflow(buf: *u8, pos: *i64, cap: i64, g: *EcoGraph, dmodv: *i64, dnamesv: *u8, dnoffv: *i64, dnlenv: *i64,
791 cfoh: *i64, cfol: *i64, start: i64, noext: i64) -> i64 {
792 let nd: *i64 = sys_mmap(48 * 8) as *i64
793 let ndep: *i64 = sys_mmap(48 * 8) as *i64
794 let npar: *i64 = sys_mmap(48 * 8) as *i64
795 let ncyc: *i64 = sys_mmap(48 * 8) as *i64
796 let stk: *i64 = sys_mmap(CW_MAGIC_1024 * 8) as *i64
797 var nn: i64 = 0
798 var sp: i64 = 0
799 stk[0] = start
800 stk[1] = 0
801 stk[2] = 0 - 1
802 sp = 1
803 while sp > 0 {
804 if nn >= 40 { sp = 0 } else {
805 sp = sp - 1
806 let cd: i64 = stk[sp * 3]
807 let cdep: i64 = stk[sp * 3 + 1]
808 let cpar: i64 = stk[sp * 3 + 2]
809 var seen: i64 = 0
810 var sj: i64 = 0
811 while sj < nn { if nd[sj] == cd { seen = 1 } sj = sj + 1 }
812 nd[nn] = cd
813 ndep[nn] = cdep
814 npar[nn] = cpar
815 ncyc[nn] = seen
816 let myrow: i64 = nn
817 nn = nn + 1
818 if seen == 0 { if cdep < 6 {
819 // push children in REVERSE so the LIFO pops them in CSR order
820 var cc: i64 = cfoh[cd + 1] - 1
821 while cc >= cfoh[cd] {
822 if sp < 340 {
823 stk[sp * 3] = cfol[cc]
824 stk[sp * 3 + 1] = cdep + 1
825 stk[sp * 3 + 2] = myrow
826 sp = sp + 1
827 }
828 cc = cc - 1
829 }
830 } }
831 }
832 }
833 if nn <= 1 { return 0 }
834 cw_apps(buf, pos, cap, "<div class='svgwrap'><svg class='cflow' width='1160' height='" as *u8)
835 cw_appn(buf, pos, cap, nn * 38 + 14)
836 cw_apps(buf, pos, cap, "' xmlns='http://www.w3.org/2000/svg'>\n" as *u8)
837 var r0: i64 = 0
838 while r0 < nn {
839 if npar[r0] >= 0 {
840 let px: i64 = 20 + ndep[npar[r0]] * 150 + 12
841 let py: i64 = npar[r0] * 38 + 34
842 let cy2: i64 = r0 * 38 + 20
843 cw_apps(buf, pos, cap, "<path d='M " as *u8)
844 cw_appn(buf, pos, cap, px)
845 cw_apps(buf, pos, cap, " " as *u8)
846 cw_appn(buf, pos, cap, py)
847 cw_apps(buf, pos, cap, " V " as *u8)
848 cw_appn(buf, pos, cap, cy2)
849 cw_apps(buf, pos, cap, " H " as *u8)
850 cw_appn(buf, pos, cap, 20 + ndep[r0] * 150 - 6)
851 cw_apps(buf, pos, cap, "' fill='none' style='stroke:var(--svgs)'/>\n" as *u8)
852 }
853 r0 = r0 + 1
854 }
855 var r1: i64 = 0
856 while r1 < nn {
857 let dd: i64 = nd[r1]
858 cw_apps(buf, pos, cap, "<a href='" as *u8)
859 let mn2: *u8 = ((g.arena as i64) + g.node_off[dmodv[dd]]) as *u8
860 cw_appstem(buf, pos, cap, mn2)
861 cw_appext(buf, pos, cap, noext)
862 cw_apps(buf, pos, cap, "#fn_" as *u8)
863 cw_app(buf, pos, cap, ((dnamesv as i64) + dnoffv[dd]) as *u8, dnlenv[dd])
864 cw_apps(buf, pos, cap, "'><rect x='" as *u8)
865 cw_appn(buf, pos, cap, 20 + ndep[r1] * 150)
866 cw_apps(buf, pos, cap, "' y='" as *u8)
867 cw_appn(buf, pos, cap, r1 * 38 + 8)
868 cw_apps(buf, pos, cap, "' width='240' height='26' rx='6' style='fill:var(--svgb);stroke:var(--svgs)'/><text x='" as *u8)
869 cw_appn(buf, pos, cap, 20 + ndep[r1] * 150 + 9)
870 cw_apps(buf, pos, cap, "' y='" as *u8)
871 cw_appn(buf, pos, cap, r1 * 38 + 25)
872 cw_apps(buf, pos, cap, "' style='fill:var(--svgt)' font-size='11' font-family='monospace'>" as *u8)
873 var tl: i64 = dnlenv[dd]
874 if tl > 26 { tl = 26 }
875 cw_appe(buf, pos, cap, ((dnamesv as i64) + dnoffv[dd]) as *u8, tl)
876 if ncyc[r1] == 1 { cw_apps(buf, pos, cap, " ↻" as *u8) }
877 cw_apps(buf, pos, cap, "</text></a>\n" as *u8)
878 r1 = r1 + 1
879 }
880 cw_apps(buf, pos, cap, "</svg></div>\n" as *u8)
881 return nn
882}
883
884// include <outdir>/_narrative/<name>.html verbatim (the LLM/analyst-written
885// overview sidecar). The generator only SHOWS it; the narration beat owns it.
886func cw_nar_include(page: *u8, pos: *i64, cap: i64, outdir: *u8, opath: *u8, name: *u8) -> i64 {
887 var w: i64 = 0
888 var i: i64 = 0
889 while outdir[i] != (0 as u8) { opath[w] = outdir[i]; w = w + 1; i = i + 1 }
890 opath[w] = 47 as u8
891 w = w + 1
892 w = w + cw_rawcpy(opath, w, "_narrative/" as *u8)
893 var j: i64 = 0
894 while name[j] != (0 as u8) { opath[w] = name[j]; w = w + 1; j = j + 1 }
895 w = w + cw_rawcpy(opath, w, ".html" as *u8)
896 opath[w] = 0 as u8
897 let narbuf: *u8 = sys_mmap(CW_MAGIC_65536)
898 let narn: i64 = gl_read(opath, narbuf, CW_MAGIC_65536)
899 if narn > 0 {
900 cw_apps(page, pos, cap, "<div class='nar'>" as *u8)
901 cw_app(page, pos, cap, narbuf, narn)
902 cw_apps(page, pos, cap, "</div>\n<p class='muted'>narrated overview -- maintained by the narration lane, module links verified against this wiki.</p>\n" as *u8)
903 }
904 return narn
905}
906
907// line-number link into the module's source page: src_<stem>[.html]#L<n>
908func cw_appsrclink(buf: *u8, pos: *i64, cap: i64, nm: *u8, ln: i64, noext: i64) -> i64 {
909 cw_apps(buf, pos, cap, "<a href='src_" as *u8)
910 cw_app(buf, pos, cap, nm, cw_stemlen(nm))
911 cw_appext(buf, pos, cap, noext)
912 cw_apps(buf, pos, cap, "#L" as *u8)
913 cw_appn(buf, pos, cap, ln)
914 cw_apps(buf, pos, cap, "'>" as *u8)
915 cw_appn(buf, pos, cap, ln)
916 cw_apps(buf, pos, cap, "</a>" as *u8)
917 return 0
918}
919
920// per-module neighborhood diagram: imports (left, cap 10) -> module -> importers
921// (right, cap 10), every box a link. The pill lists below it stay COMPLETE --
922// the SVG is a view, the pills are the record.
923func cw_nbr(buf: *u8, pos: *i64, cap: i64, g: *EcoGraph, v: i64, pathoff: *i64, noext: i64) -> i64 {
924 let oh: i64 = g.out_head[v]
925 let oe: i64 = g.out_head[v + 1]
926 let ih: i64 = g.in_head[v]
927 let ie: i64 = g.in_head[v + 1]
928 var nl: i64 = oe - oh
929 var nr: i64 = ie - ih
930 if nl + nr <= 0 { return 0 }
931 var xl: i64 = 0
932 if nl > 10 { xl = nl - 10; nl = 10 }
933 var xr: i64 = 0
934 if nr > 10 { xr = nr - 10; nr = 10 }
935 var rows: i64 = nl
936 if nr > rows { rows = nr }
937 let hh: i64 = rows * 42 + 26
938 cw_apps(buf, pos, cap, "<div class='svgwrap'><svg class='nbr' width='990' height='" as *u8)
939 cw_appn(buf, pos, cap, hh)
940 cw_apps(buf, pos, cap, "' xmlns='http://www.w3.org/2000/svg'>\n" as *u8)
941 let cy: i64 = hh / 2 - 14
942 // edges
943 var e0: i64 = 0
944 while e0 < nl {
945 cw_apps(buf, pos, cap, "<line x1='262' y1='" as *u8)
946 cw_appn(buf, pos, cap, 14 + e0 * 42 + 13)
947 cw_apps(buf, pos, cap, "' x2='372' y2='" as *u8)
948 cw_appn(buf, pos, cap, cy + 13)
949 cw_apps(buf, pos, cap, "' style='stroke:var(--svgs);stroke-opacity:.8'/>\n" as *u8)
950 e0 = e0 + 1
951 }
952 var e1: i64 = 0
953 while e1 < nr {
954 cw_apps(buf, pos, cap, "<line x1='618' y1='" as *u8)
955 cw_appn(buf, pos, cap, cy + 13)
956 cw_apps(buf, pos, cap, "' x2='728' y2='" as *u8)
957 cw_appn(buf, pos, cap, 14 + e1 * 42 + 13)
958 cw_apps(buf, pos, cap, "' style='stroke:var(--svgs);stroke-opacity:.8'/>\n" as *u8)
959 e1 = e1 + 1
960 }
961 // left: imports
962 var l0: i64 = 0
963 while l0 < nl {
964 let dep: i64 = g.out_list[oh + l0]
965 let dnm: *u8 = ((g.arena as i64) + g.node_off[dep]) as *u8
966 cw_apps(buf, pos, cap, "<a href='" as *u8)
967 if pathoff[dep] > 0 { cw_appstem(buf, pos, cap, dnm); cw_appext(buf, pos, cap, noext) } else { cw_apps(buf, pos, cap, "#deps" as *u8) }
968 cw_apps(buf, pos, cap, "'><rect x='10' y='" as *u8)
969 cw_appn(buf, pos, cap, 14 + l0 * 42)
970 cw_apps(buf, pos, cap, "' width='252' height='26' rx='6' style='fill:var(--svgb);stroke:var(--svgs)'/><text x='20' y='" as *u8)
971 cw_appn(buf, pos, cap, 14 + l0 * 42 + 17)
972 cw_apps(buf, pos, cap, "' style='fill:var(--svgt)' font-size='11' font-family='monospace'>" as *u8)
973 var dl: i64 = cw_slen(dnm)
974 if dl > 34 { dl = 34 }
975 cw_appe(buf, pos, cap, dnm, dl)
976 cw_apps(buf, pos, cap, "</text></a>\n" as *u8)
977 l0 = l0 + 1
978 }
979 // center: this module
980 let snm: *u8 = ((g.arena as i64) + g.node_off[v]) as *u8
981 cw_apps(buf, pos, cap, "<rect x='372' y='" as *u8)
982 cw_appn(buf, pos, cap, cy)
983 cw_apps(buf, pos, cap, "' width='246' height='28' rx='7' style='fill:var(--svgc);stroke:var(--acc)'/><text x='382' y='" as *u8)
984 cw_appn(buf, pos, cap, cy + 18)
985 cw_apps(buf, pos, cap, "' style='fill:var(--svgt)' font-size='12' font-family='monospace' font-weight='bold'>" as *u8)
986 var sl: i64 = cw_slen(snm)
987 if sl > 30 { sl = 30 }
988 cw_appe(buf, pos, cap, snm, sl)
989 cw_apps(buf, pos, cap, "</text>\n" as *u8)
990 // right: importers
991 var r0: i64 = 0
992 while r0 < nr {
993 let who: i64 = g.in_list[ih + r0]
994 let wnm: *u8 = ((g.arena as i64) + g.node_off[who]) as *u8
995 cw_apps(buf, pos, cap, "<a href='" as *u8)
996 cw_appstem(buf, pos, cap, wnm)
997 cw_appext(buf, pos, cap, noext)
998 cw_apps(buf, pos, cap, "'><rect x='728' y='" as *u8)
999 cw_appn(buf, pos, cap, 14 + r0 * 42)
1000 cw_apps(buf, pos, cap, "' width='252' height='26' rx='6' style='fill:var(--svgb);stroke:var(--svgs)'/><text x='738' y='" as *u8)
1001 cw_appn(buf, pos, cap, 14 + r0 * 42 + 17)
1002 cw_apps(buf, pos, cap, "' style='fill:var(--svgt)' font-size='11' font-family='monospace'>" as *u8)
1003 var wl: i64 = cw_slen(wnm)
1004 if wl > 34 { wl = 34 }
1005 cw_appe(buf, pos, cap, wnm, wl)
1006 cw_apps(buf, pos, cap, "</text></a>\n" as *u8)
1007 r0 = r0 + 1
1008 }
1009 cw_apps(buf, pos, cap, "</svg></div>" as *u8)
1010 if xl + xr > 0 {
1011 cw_apps(buf, pos, cap, "<p class='muted'>diagram shows first 10 each side; +" as *u8)
1012 cw_appn(buf, pos, cap, xl)
1013 cw_apps(buf, pos, cap, " more imports, +" as *u8)
1014 cw_appn(buf, pos, cap, xr)
1015 cw_apps(buf, pos, cap, " more importers in the complete lists below.</p>" as *u8)
1016 }
1017 cw_apps(buf, pos, cap, "\n" as *u8)
1018 return 0
1019}
1020
1021// display name for a module kind
1022func cw_kindname(k: i64) -> *u8 {
1023 if k == CW_K_ARCHIVED { return "archived" as *u8 }
1024 if k == CW_K_GATE { return "gate/proof" as *u8 }
1025 if k == CW_K_BENCH { return "bench" as *u8 }
1026 if k == CW_K_PROBE { return "probe" as *u8 }
1027 if k == CW_K_SMOKE { return "smoke" as *u8 }
1028 if k == CW_K_SKETCH { return "sketch/demo" as *u8 }
1029 if k == CW_K_GENFIX { return "generated fixture" as *u8 }
1030 if k == CW_K_SERVICE { return "service" as *u8 }
1031 if k == CW_K_TOOL { return "tool" as *u8 }
1032 if k == CW_K_LIB { return "library" as *u8 }
1033 return "orphan library" as *u8
1034}
1035
1036// bare substring (case-sensitive) over a nul-terminated name
1037func cw_namehas(nm: *u8, sub: *u8) -> i64 {
1038 let n: i64 = cw_slen(nm)
1039 let sl: i64 = cw_slen(sub)
1040 if sl <= 0 { return 0 }
1041 var i: i64 = 0
1042 while i + sl <= n {
1043 var m: i64 = 0
1044 var ok: i64 = 1
1045 while m < sl { if nm[i + m] != sub[m] { ok = 0; m = sl } else { m = m + 1 } }
1046 if ok == 1 { return 1 }
1047 i = i + 1
1048 }
1049 return 0
1050}
1051
1052// debt-category display name + the severity a finding for it carries
1053func cw_catname(k: i64) -> *u8 {
1054 if k == CW_CAT_LANDMINE { return "landmine" as *u8 }
1055 if k == CW_CAT_WORKAROUND { return "workaround / hack" as *u8 }
1056 if k == CW_CAT_GOTCHA { return "gotcha / trap" as *u8 }
1057 if k == CW_CAT_TODO { return "todo / unfinished" as *u8 }
1058 if k == CW_CAT_THIRDPARTY { return "third-party / foreign dependency" as *u8 }
1059 if k == CW_CAT_SECURITY { return "security note" as *u8 }
1060 if k == CW_CAT_PERF { return "performance debt" as *u8 }
1061 return "deprecated / retire" as *u8
1062}
1063// STABLE, count-free finding description per category (idempotent filing)
1064func cw_catdesc(k: i64) -> *u8 {
1065 if k == CW_CAT_LANDMINE { return "Landmine markers present (dual-copy / stale-tree / footgun hazards documented in source). https://nishifamily.com/code/debt -- these are the traps that silently regress; review + neutralize each." as *u8 }
1066 if k == CW_CAT_WORKAROUND { return "Workaround/hack markers present (code that works around a defect rather than fixing it). https://nishifamily.com/code/debt -- replace each with a real fix where the root cause now allows it." as *u8 }
1067 if k == CW_CAT_GOTCHA { return "Gotcha/trap/caveat markers present (documented traps future work must not step on). https://nishifamily.com/code/debt -- consider a gate tooth so the trap cannot recur silently." as *u8 }
1068 if k == CW_CAT_TODO { return "TODO/FIXME/unfinished markers present (declared-incomplete work). https://nishifamily.com/code/debt -- convert each to a tracked backlog item or finish it." as *u8 }
1069 if k == CW_CAT_THIRDPARTY { return "Third-party/foreign-dependency markers present (the sovereignty tax). https://nishifamily.com/code/debt and /code/sovereignty -- plan the first-byte-up replacement per dependency." as *u8 }
1070 if k == CW_CAT_SECURITY { return "Security markers present (CWE/vuln/exploit notes in source). https://nishifamily.com/code/debt -- verify each is closed and gated, not merely noted." as *u8 }
1071 if k == CW_CAT_PERF { return "Performance-debt markers present (quadratic/O(n^2)/slow-path notes). https://nishifamily.com/code/debt -- measure before optimizing; the note is a lead, the ruler decides." as *u8 }
1072 return "Deprecated/retire markers present (code flagged for removal still in the tree). https://nishifamily.com/code/debt -- migrate consumers then soft-delete (rule 13)." as *u8
1073}
1074func cw_catsev(k: i64) -> i64 {
1075 if k == CW_CAT_LANDMINE { return 7 }
1076 if k == CW_CAT_SECURITY { return 7 }
1077 if k == CW_CAT_GOTCHA { return 5 }
1078 if k == CW_CAT_WORKAROUND { return 5 }
1079 if k == CW_CAT_THIRDPARTY { return 5 }
1080 if k == CW_CAT_DEPRECATED { return 5 }
1081 if k == CW_CAT_PERF { return 4 }
1082 return 4
1083}
1084
1085// Does the byte range [s,e) describe a hazard that is ALREADY HANDLED --
1086// a defence, or a defect that was resolved -- rather than one still live?
1087//
1088// ★THE DISCRIMINATOR IS GRAMMATICAL TENSE, and it is declared on purpose:
1089// only PAST-TENSE / accomplished forms count ("avoided", "fixed", "sealed",
1090// "prevents"). The imperative is deliberately EXCLUDED -- "fix this landmine"
1091// and "avoid X" are work OWED, and a rule that swallowed those would let the
1092// gauge hide exactly the rows it exists to surface. This one function serves
1093// every hazard category (rule 15: one copy, not one per marker family).
1094func cw_resolved(src: *u8, s: i64, e: i64) -> i64 {
1095 if cw_hastok(src, s, e, "prevent" as *u8, 7) == 1 { return 1 }
1096 if cw_hastok(src, s, e, "mitigat" as *u8, 7) == 1 { return 1 }
1097 if cw_hastok(src, s, e, "harden" as *u8, 6) == 1 { return 1 }
1098 if cw_hastok(src, s, e, "structural" as *u8, 10) == 1 { return 1 }
1099 if cw_hastok(src, s, e, "defen" as *u8, 5) == 1 { return 1 }
1100 if cw_hastok(src, s, e, "closes" as *u8, 6) == 1 { return 1 }
1101 if cw_hastok(src, s, e, "closed" as *u8, 6) == 1 { return 1 }
1102 if cw_hastok(src, s, e, "sealed" as *u8, 6) == 1 { return 1 }
1103 if cw_hastok(src, s, e, "refuses" as *u8, 7) == 1 { return 1 }
1104 if cw_hastok(src, s, e, "avoided" as *u8, 7) == 1 { return 1 }
1105 if cw_hastok(src, s, e, "fixed" as *u8, 5) == 1 { return 1 }
1106 if cw_hastok(src, s, e, "killed" as *u8, 6) == 1 { return 1 }
1107 if cw_hastok(src, s, e, "retired" as *u8, 7) == 1 { return 1 }
1108 if cw_hastok(src, s, e, "resolved" as *u8, 8) == 1 { return 1 }
1109 if cw_hastok(src, s, e, "eaten" as *u8, 5) == 1 { return 1 }
1110 return 0
1111}
1112
1113// kept as the security-specific name the security tooth drives; the behaviour
1114// now lives in the shared scanner below.
1115func cw_secmit(src: *u8, s: i64, e: i64) -> i64 { return cw_resolved(src, s, e) }
1116
1117// Line-scoped hazard scan shared by every marker family. Returns 1 iff SOME
1118// line carries one of the (up to 4) markers AND neither it nor the line above
1119// says the hazard is handled. Pass length 0 to skip an unused marker slot.
1120func cw_markscan(src: *u8, n: i64, m1: *u8, l1: i64, m2: *u8, l2: i64, m3: *u8, l3: i64, m4: *u8, l4: i64) -> i64 {
1121 var ls: i64 = 0
1122 var ps: i64 = 0
1123 var pe: i64 = 0
1124 while ls < n {
1125 var le: i64 = ls
1126 var g: i64 = 1
1127 while g == 1 { if le >= n { g = 0 } else { if src[le] == (10 as u8) { g = 0 } else { le = le + 1 } } }
1128 var hit: i64 = 0
1129 if l1 > 0 { if cw_hastok(src, ls, le, m1, l1) == 1 { hit = 1 } }
1130 if hit == 0 { if l2 > 0 { if cw_hastok(src, ls, le, m2, l2) == 1 { hit = 1 } } }
1131 if hit == 0 { if l3 > 0 { if cw_hastok(src, ls, le, m3, l3) == 1 { hit = 1 } } }
1132 if hit == 0 { if l4 > 0 { if cw_hastok(src, ls, le, m4, l4) == 1 { hit = 1 } } }
1133 if hit == 1 {
1134 var done: i64 = 0
1135 if cw_resolved(src, ls, le) == 1 { done = 1 }
1136 if done == 0 { if pe > ps { if cw_resolved(src, ps, pe) == 1 { done = 1 } } }
1137 if done == 0 { return 1 }
1138 }
1139 ps = ls
1140 pe = le
1141 ls = le + 1
1142 }
1143 return 0
1144}
1145
1146// Security markers: HAZARD, or a DECLARED MITIGATION?
1147//
1148// A whole-file substring hit on cwe-/vuln/exploit cannot tell "this module HAS
1149// a vulnerability" from "this module PREVENTS one" -- and the tree is full of
1150// the latter. nx_http_server's header documents CWE-352 CSRF *structural
1151// prevention* three times; the old rule filed it as a sev-7 hazard telling the
1152// reader to go neutralize it. **A gauge that reports a defence as a defect
1153// teaches its reader to ignore the rows that are real.**
1154//
1155// Rule (DECLARED, and the same "own line or the line ABOVE" convention the
1156// bounds scanner already uses): a security-marker LINE counts as a hazard
1157// unless that line -- or the one above it -- carries mitigation vocabulary.
1158// ONE unmitigated line marks the module, so a file that prevents one CWE and
1159// still notes a real hazard is reported, correctly, as carrying a hazard.
1160func cw_secscan(src: *u8, n: i64) -> i64 {
1161 return cw_markscan(src, n, "cwe-" as *u8, 4, "vuln" as *u8, 4, "exploit" as *u8, 7, "" as *u8, 0)
1162}
1163
1164// landmine family, same rule: "sys_mkdir landmine avoided" and a dated
1165// citation of a historical repro are NOT live hazards in this file.
1166func cw_lmscan(src: *u8, n: i64) -> i64 {
1167 return cw_markscan(src, n, "landmine" as *u8, 8, "footgun" as *u8, 7, "dual-copy" as *u8, 9, "stale-tree" as *u8, 10)
1168}
1169
1170// set category bits for a module by scanning its raw source for markers.
1171// Case-insensitive; each marker maps to one category. Returns the mask.
1172func cw_catscan(src: *u8, n: i64) -> i64 {
1173 var mask: i64 = 0
1174 // landmine: the tree's own hazard vocabulary, LINE-SCOPED and
1175 // resolution-aware (see cw_markscan) -- "landmine avoided" and a dated
1176 // citation of a past repro describe handled hazards, not live ones.
1177 if cw_lmscan(src, n) == 1 { mask = mask | 1 }
1178 // workaround / hack
1179 if cw_hastok(src, 0, n, "workaround" as *u8, 10) == 1 { mask = mask | 2 }
1180 if cw_hastok(src, 0, n, "hack" as *u8, 4) == 1 { mask = mask | 2 }
1181 if cw_hastok(src, 0, n, "kludge" as *u8, 6) == 1 { mask = mask | 2 }
1182 // gotcha / trap
1183 if cw_hastok(src, 0, n, "gotcha" as *u8, 6) == 1 { mask = mask | 4 }
1184 if cw_hastok(src, 0, n, "trap" as *u8, 4) == 1 { mask = mask | 4 }
1185 if cw_hastok(src, 0, n, "caveat" as *u8, 6) == 1 { mask = mask | 4 }
1186 // todo / unfinished
1187 if cw_hastok(src, 0, n, "todo" as *u8, 4) == 1 { mask = mask | 8 }
1188 if cw_hastok(src, 0, n, "fixme" as *u8, 5) == 1 { mask = mask | 8 }
1189 if cw_hastok(src, 0, n, "unfinished" as *u8, 10) == 1 { mask = mask | 8 }
1190 // third-party / foreign dependency (sovereignty-relevant)
1191 if cw_hastok(src, 0, n, "third-party" as *u8, 11) == 1 { mask = mask | 16 }
1192 if cw_hastok(src, 0, n, "3rd-party" as *u8, 9) == 1 { mask = mask | 16 }
1193 if cw_hastok(src, 0, n, "foreign" as *u8, 7) == 1 { mask = mask | 16 }
1194 if cw_hastok(src, 0, n, "non-sovereign" as *u8, 13) == 1 { mask = mask | 16 }
1195 // security note -- LINE-SCOPED and mitigation-aware (see cw_secscan):
1196 // a documented DEFENCE is not a defect, so "CWE-352 structural prevention"
1197 // no longer files a sev-7 telling the reader to go neutralize it.
1198 if cw_secscan(src, n) == 1 { mask = mask | 32 }
1199 // performance debt
1200 if cw_hastok(src, 0, n, "quadratic" as *u8, 9) == 1 { mask = mask | 64 }
1201 if cw_hastok(src, 0, n, "o(n^2)" as *u8, 6) == 1 { mask = mask | 64 }
1202 if cw_hastok(src, 0, n, "slow path" as *u8, 9) == 1 { mask = mask | 64 }
1203 // deprecated / retire
1204 if cw_hastok(src, 0, n, "deprecated" as *u8, 10) == 1 { mask = mask | 128 }
1205 if cw_hastok(src, 0, n, "retire" as *u8, 6) == 1 { mask = mask | 128 }
1206 if cw_hastok(src, 0, n, "obsolete" as *u8, 8) == 1 { mask = mask | 128 }
1207 return mask
1208}
1209
1210// ---------- SHIPPED-DRIFT: is what RUNS what the tree SAYS? (2026-07-30) ----------
1211//
1212// ★★★THE DOMINANT SYSTEMIC CLASS ON THE BOARD. Of the open sev-9 rows, whole
1213// scopes are named for it: `eaten-but-fix-not-in-any-tree`,
1214// `breaker-exists-in-source-NOT-in-running-supervisor`, `source-loss`. I hit it
1215// FIVE times in one day -- the torrent reaper, the mgmt api, the tools daemon,
1216// nx_fsops_write and the edge -- each time rediscovered by hand, one binary at
1217// a time, because **nothing in the ecosystem answers "is the deployed artefact
1218// newer than its source?"** A fix that lives only in source is not shipped; a
1219// feature that lives only in a deployed binary is not in the tree. Both are
1220// invisible without this comparison.
1221//
1222// MEASURE: for a deployed <stem>.elf, compare its mtime against its module
1223// source mtime. elf OLDER than source => DRIFT (a source change never shipped).
1224// Declared honestly: this is the MODULE'S OWN source, not its full import
1225// closure, so it UNDER-reports (a change in a dependency will not show until
1226// the closure walk lands) -- an under-reporting gauge is safe; it never invents
1227// drift, and the rows it does show are real. mtime lives at offset 88 of the
1228// x86-64 struct stat (sys_fstatat).
1229// Returns mtime seconds, or -1 when the path is absent/unreadable.
1230func cw_mtime(path: *u8) -> i64 {
1231 let sb: *u8 = sys_mmap(256)
1232 if sys_fstatat(path, sb) != 0 { return 0 - 1 }
1233 let p: *i64 = ((sb as i64) + 88) as *i64
1234 return p[0]
1235}
1236
1237// ---------- substring-classifier scan (2026-07-30) ----------
1238//
1239// ★★★THE DEFECT CLASS THIS MEASURES cost FOUR separate bugs in ONE day, at
1240// sev 5 through 9, in four unrelated subsystems:
1241// * hazard markers -- `contains(src,"cwe-")` filed nx_http_server's CWE-352
1242// *structural prevention* as a hazard to neutralize
1243// * promote-sprawl -- `contains(name,"_v2")` flagged the edge SSOT, whose
1244// version IS its identity, as a stale-promote copy
1245// * organ role -- `contains(name,"torrent")` classified the one-shot
1246// worker nx_torrent_get as a daemon, leaving it
1247// unshippable by ANY sanctioned verb
1248// * HTTP structure -- route matching scanned the whole buffer, so BODY
1249// bytes reading `POST /route?` hijacked the request
1250//
1251// Every one is the same mistake: **A SUBSTRING STANDING IN FOR A PROPERTY.**
1252// The needle is cheap to write and reads as a rule, but it cannot distinguish
1253// a thing from a mention of a thing -- a defect from its documented cure, an
1254// identity from a copy, a role from a name, a request line from its payload.
1255//
1256// WHAT IS COUNTED (declared, deliberately narrow so the gauge stays honest):
1257// a SINGLE LINE that both tests a substring AND decides on it -- a
1258// `contains(`/`hastok(`/`streq(`-family call, a quoted literal, and a verdict
1259// on the same line (`return 0`/`return 1`/`= 1`). That is the exact shape of
1260// all four defects. A search that merely FINDS text and hands the offset
1261// onward is not counted: locating a substring is legitimate; CLASSIFYING by
1262// one is the smell. The honest fix is to read the structure (parse the field,
1263// declare the role in data, bound the scan to the header) -- so this gauge
1264// falls as classifiers become declarative.
1265// Plain substring test over [s,e) with NO word-boundary requirement.
1266//
1267// ⚠cw_hastok deliberately demands a LEFT WORD BOUNDARY (that rule is what stops
1268// "organ" matching inside nx_chem_mORGANn). Here it is exactly wrong: the call
1269// sites we are hunting are named `md_contains(`, `sd2_contains(`, `cw_hastok(`,
1270// so the needle is always preceded by an identifier character. The detector for
1271// substring classifiers was itself defeated by a substring rule -- which is the
1272// class it exists to measure.
1273func cw_hasraw(src: *u8, s: i64, e: i64, needle: *u8, nl: i64) -> i64 {
1274 if nl <= 0 { return 0 }
1275 var i: i64 = s
1276 while i + nl <= e {
1277 var k: i64 = 0
1278 var ok: i64 = 1
1279 // ⚠SELF-CONTAINED lowercase ON PURPOSE. This function sits at line ~1200,
1280 // far ABOVE cw_lc (~2298), and nx_cc forward references are unproven --
1281 // calling cw_lc here compiled clean and then returned garbage, which is
1282 // exactly why the first build of this scanner failed its own gate tooth
1283 // and the second CRASHED the generator after its header line. Depend on
1284 // nothing defined below you.
1285 while k < nl {
1286 var ch: i64 = src[i + k] as i64
1287 if ch >= 65 { if ch <= 90 { ch = ch + 32 } }
1288 if ch != (needle[k] as i64) { ok = 0; k = nl } else { k = k + 1 }
1289 }
1290 if ok == 1 { return 1 }
1291 i = i + 1
1292 }
1293 return 0
1294}
1295
1296func cw_substrclass(src: *u8, n: i64) -> i64 {
1297 var hits: i64 = 0
1298 var ls: i64 = 0
1299 while ls < n {
1300 var le: i64 = ls
1301 var g: i64 = 1
1302 while g == 1 { if le >= n { g = 0 } else { if src[le] == (10 as u8) { g = 0 } else { le = le + 1 } } }
1303 if le > ls { if src[ls] != (47 as u8) {
1304 var isc: i64 = 0
1305 if cw_hasraw(src, ls, le, "contains(" as *u8, 9) == 1 { isc = 1 }
1306 if isc == 0 { if cw_hasraw(src, ls, le, "hastok(" as *u8, 7) == 1 { isc = 1 } }
1307 if isc == 1 {
1308 var hasq: i64 = 0
1309 var q: i64 = ls
1310 while q < le { if src[q] == (34 as u8) { hasq = 1; q = le } else { q = q + 1 } }
1311 if hasq == 1 {
1312 var dec: i64 = 0
1313 if cw_hasraw(src, ls, le, "return 1" as *u8, 8) == 1 { dec = 1 }
1314 if dec == 0 { if cw_hasraw(src, ls, le, "return 0" as *u8, 8) == 1 { dec = 1 } }
1315 if dec == 0 { if cw_hasraw(src, ls, le, "= 1 " as *u8, 4) == 1 { dec = 1 } }
1316 if dec == 1 { hits = hits + 1 }
1317 }
1318 }
1319 } }
1320 ls = le + 1
1321 }
1322 return hits
1323}
1324
1325// ---------- taste-bound scan (bounds law 2026-07-29, seq1242) ----------
1326// Counts `const ..._CAP/_MAX... = N` (N >= 16) declarations whose own line or
1327// the line ABOVE carries no measurement-provenance marker. Lexical, declared;
1328// the 07-29 audit found the tree inverted (naked bounds the majority) -- this
1329// makes the law a STANDING GAUGE that falls as bounds gain derivations.
1330func cw_bs_prov(src: *u8, s: i64, e: i64) -> i64 {
1331 if cw_submatch(src, s, e, "measured" as *u8) == 1 { return 1 }
1332 if cw_submatch(src, s, e, "derived" as *u8) == 1 { return 1 }
1333 if cw_submatch(src, s, e, "sized" as *u8) == 1 { return 1 }
1334 if cw_submatch(src, s, e, "budget" as *u8) == 1 { return 1 }
1335 if cw_submatch(src, s, e, "today" as *u8) == 1 { return 1 }
1336 if cw_submatch(src, s, e, "RFC" as *u8) == 1 { return 1 }
1337 if cw_submatch(src, s, e, "rfc" as *u8) == 1 { return 1 }
1338 if cw_submatch(src, s, e, "JPL" as *u8) == 1 { return 1 }
1339 if cw_submatch(src, s, e, "== " as *u8) == 1 { return 1 }
1340 return 0
1341}
1342
1343func cw_boundscan(src: *u8, n: i64) -> i64 {
1344 var cnt: i64 = 0
1345 var ls: i64 = 0
1346 var prevs: i64 = 0
1347 var preve: i64 = 0
1348 while ls < n {
1349 var le: i64 = ls
1350 var g1: i64 = 1
1351 while g1 == 1 { if le >= n { g1 = 0 } else { if src[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
1352 var cand: i64 = 0
1353 if le > ls + 8 { if src[ls] != (47 as u8) {
1354 if cw_submatch(src, ls, le, "const " as *u8) == 1 {
1355 // "CAP:"/"MAX:" (name-fused OR underscored -- the first rule
1356 // used "_CAP/_MAX" and let PAGECAP-style names dodge; widened
1357 // 07-29 after the gauge's own first eat exposed the blind spot)
1358 if cw_submatch(src, ls, le, "CAP:" as *u8) == 1 { cand = 1 } else {
1359 if cw_submatch(src, ls, le, "MAX:" as *u8) == 1 { cand = 1 } }
1360 }
1361 } }
1362 if cand == 1 {
1363 var veq: i64 = 0 - 1
1364 var eq: i64 = ls
1365 while eq < le { if src[eq] == (61 as u8) { if veq < 0 { veq = eq } } eq = eq + 1 }
1366 var val: i64 = 0
1367 var seen: i64 = 0
1368 if veq >= 0 {
1369 var vp: i64 = veq + 1
1370 var run: i64 = 1
1371 while run == 1 {
1372 if vp >= le { run = 0 } else {
1373 let c2: i64 = src[vp] as i64
1374 if c2 >= 48 { if c2 <= 57 { val = val * 10 + (c2 - 48); seen = 1; vp = vp + 1 } else { if seen == 1 { run = 0 } else { vp = vp + 1 } } } else { if seen == 1 { run = 0 } else { vp = vp + 1 } }
1375 }
1376 }
1377 }
1378 if seen == 1 { if val >= 16 {
1379 var prov: i64 = cw_bs_prov(src, ls, le)
1380 if prov == 0 { if preve > prevs { prov = cw_bs_prov(src, prevs, preve) } }
1381 if prov == 0 { cnt = cnt + 1 }
1382 } }
1383 }
1384 prevs = ls
1385 preve = le
1386 ls = le + 1
1387 }
1388 return cnt
1389}
1390
1391// ---------- language-adoption scan (rung 16: the nishilang lane's axes) ----------
1392// Lexical, comment-INCLUSIVE, rule declared on /code/lang: bit1 = declares a
1393// fixed array `: [<digit>` (the bounds-checked [N]T form, lane r1); bit2 =
1394// uses the slice form (`: []` or `__slice(`, heap spatial safety, lane r2).
1395// A comment mentioning the syntax counts -- the rule is stated in-band and
1396// applied identically every regen, so the TREND is honest even where a stray
1397// comment inflates a count by one.
1398func cw_langscan(src: *u8, n: i64) -> i64 {
1399 var mask: i64 = 0
1400 var i: i64 = 0
1401 while i + 8 < n {
1402 if src[i] == (58 as u8) { if src[i + 1] == (32 as u8) { if src[i + 2] == (91 as u8) {
1403 if src[i + 3] >= (48 as u8) { if src[i + 3] <= (57 as u8) { mask = mask | 1 } }
1404 if src[i + 3] == (93 as u8) { mask = mask | 2 }
1405 } } }
1406 if src[i] == (95 as u8) { if src[i + 1] == (95 as u8) { if src[i + 2] == (115 as u8) {
1407 if src[i + 3] == (108 as u8) { if src[i + 4] == (105 as u8) { if src[i + 5] == (99 as u8) {
1408 if src[i + 6] == (101 as u8) { if src[i + 7] == (40 as u8) { mask = mask | 2 } }
1409 } } }
1410 } } }
1411 if mask == 3 { return 3 }
1412 i = i + 1
1413 }
1414 return mask
1415}
1416
1417// append a TARGETED finding naming one module: the desc stays stable (module
1418// name + category + citation, no counts/dates) so re-filing is idempotent, and
1419// each row is a discrete work item a maker can pick up and close.
1420func cw_fnd_mod(fbuf: *u8, fpos: *i64, sev: i64, cat: *u8, mstem: *u8, mstem_n: i64) -> i64 {
1421 // ⚠NO COUNTS IN THE DESC. Fan-in decides the RANKING (which modules get a
1422 // targeted row) but must never appear in the text: a number that drifts
1423 // makes every regen file a "new" debt and the board becomes noise.
1424 cw_appn(fbuf, fpos, CW_FINDCAP, sev)
1425 cw_apps(fbuf, fpos, CW_FINDCAP, "\tcodewiki-debt\t" as *u8)
1426 cw_apps(fbuf, fpos, CW_FINDCAP, cat)
1427 cw_apps(fbuf, fpos, CW_FINDCAP, " in " as *u8)
1428 cw_app(fbuf, fpos, CW_FINDCAP, mstem, mstem_n)
1429 cw_apps(fbuf, fpos, CW_FINDCAP, " (high blast radius -- widely imported). Review and neutralize the hazard, then add a gate tooth so it cannot recur. https://nishifamily.com/code/" as *u8)
1430 cw_app(fbuf, fpos, CW_FINDCAP, mstem, mstem_n)
1431 cw_apps(fbuf, fpos, CW_FINDCAP, "\n" as *u8)
1432 return 0
1433}
1434
1435// append one finding line: <sev>\t<scope>\t<desc>\n (the nx_debt add grammar)
1436func cw_fnd(fbuf: *u8, fpos: *i64, sev: i64, scope: *u8, desc: *u8) -> i64 {
1437 cw_appn(fbuf, fpos, CW_FINDCAP, sev)
1438 cw_apps(fbuf, fpos, CW_FINDCAP, "\t" as *u8)
1439 cw_apps(fbuf, fpos, CW_FINDCAP, scope)
1440 cw_apps(fbuf, fpos, CW_FINDCAP, "\t" as *u8)
1441 cw_apps(fbuf, fpos, CW_FINDCAP, desc)
1442 cw_apps(fbuf, fpos, CW_FINDCAP, "\n" as *u8)
1443 return 0
1444}
1445
1446// ---------- pulse: the self-sufficiency trend instrument (rung 20) ----------
1447// The gauges the engine-shift is steered by, drawn NATIVE (first-byte-up
1448// doctrine 07-29) from the machine-fed logs at every regen: DONE frames/day,
1449// non-Claude DONE frames/day (the making-plane self-sufficiency gauge), the
1450// foreign-vs-sovereign tool-mention split, and the token meter as a TABLE --
1451// a bar chart would render a missing meter day as zero, and missing is not
1452// zero. Day buckets are RELATIVE (d-0 = newest journal day): the chart is
1453// about the trend, so no calendar math is needed or pretended.
1454func cw_atoi(b: *u8, s: i64, e: i64) -> i64 {
1455 var v: i64 = 0
1456 var i: i64 = s
1457 while i < e {
1458 let c: i64 = b[i] as i64
1459 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } else { i = e } } else { i = e }
1460 i = i + 1
1461 }
1462 return v
1463}
1464
1465func cw_submatch(hay: *u8, hs: i64, he: i64, needle: *u8) -> i64 {
1466 var nl: i64 = 0
1467 while needle[nl] != (0 as u8) { nl = nl + 1 }
1468 if nl == 0 { return 0 }
1469 var i: i64 = hs
1470 while i + nl <= he {
1471 var m: i64 = 0
1472 var ok: i64 = 1
1473 while m < nl { if hay[i + m] != needle[m] { ok = 0; m = nl } else { m = m + 1 } }
1474 if ok == 1 { return 1 }
1475 i = i + 1
1476 }
1477 return 0
1478}
1479
1480// series points "x,y x,y ..." for a 14-day series into a fixed plot box,
1481// d-13 at the left edge, d-0 at the right (the reading direction of time)
1482func cw_plpts(page: *u8, pos: *i64, cap: i64, vals: *i64, mx: i64, x0: i64, y0: i64, w: i64, h: i64) -> i64 {
1483 var d: i64 = 13
1484 while d >= 0 {
1485 let x: i64 = x0 + ((13 - d) * w) / 13
1486 let y: i64 = (y0 + h) - (vals[d] * h) / mx
1487 if d < 13 { cw_apps(page, pos, cap, " " as *u8) }
1488 cw_appn(page, pos, cap, x)
1489 cw_apps(page, pos, cap, "," as *u8)
1490 cw_appn(page, pos, cap, y)
1491 d = d - 1
1492 }
1493 return 0
1494}
1495
1496// tiny sparkline for a KPI card (last 14 days, no axes -- shape only)
1497func cw_spark(page: *u8, pos: *i64, cap: i64, vals: *i64) -> i64 {
1498 var mx: i64 = 1
1499 var i: i64 = 0
1500 while i < 14 { if vals[i] > mx { mx = vals[i] } i = i + 1 }
1501 cw_apps(page, pos, cap, "<svg width='130' height='30' viewBox='0 0 130 30'><polyline fill='none' stroke='var(--acc)' stroke-width='2' stroke-linejoin='round' stroke-linecap='round' points='" as *u8)
1502 cw_plpts(page, pos, cap, vals, mx, 3, 3, 122, 22)
1503 cw_apps(page, pos, cap, "'/><circle cx='125' cy='" as *u8)
1504 cw_appn(page, pos, cap, 25 - (vals[0] * 22) / mx)
1505 cw_apps(page, pos, cap, "' r='2.6' fill='var(--acc)'/></svg>" as *u8)
1506 return 0
1507}
1508
1509// delta pill: direction arrow + signed value + wk/wk; good direction = green
1510func cw_dpill(page: *u8, pos: *i64, cap: i64, dv: i64, goodup: i64, unit: *u8) -> i64 {
1511 var good: i64 = 0
1512 if dv > 0 { if goodup == 1 { good = 1 } }
1513 if dv < 0 { if goodup == 0 { good = 1 } }
1514 if dv == 0 { good = 1 }
1515 if good == 1 { cw_apps(page, pos, cap, "<span class='d dg'>" as *u8) } else { cw_apps(page, pos, cap, "<span class='d db'>" as *u8) }
1516 if dv > 0 { cw_apps(page, pos, cap, "▲ +" as *u8); cw_appn(page, pos, cap, dv) }
1517 if dv < 0 { cw_apps(page, pos, cap, "▼ " as *u8); cw_appn(page, pos, cap, dv) }
1518 if dv == 0 { cw_apps(page, pos, cap, "− 0" as *u8) }
1519 cw_apps(page, pos, cap, unit)
1520 cw_apps(page, pos, cap, " wk/wk</span>" as *u8)
1521 return 0
1522}
1523
1524func cw_pulse(page: *u8, pos: *i64, cap: i64, jrnlpath: *u8, meterpath: *u8, noext2: i64) -> i64 {
1525 let jb: *u8 = sys_mmap(CW_EXPCONF)
1526 let jn: i64 = gl_read(jrnlpath, jb, CW_EXPCONF)
1527 let mb: *u8 = sys_mmap(CW_MAGIC_262144)
1528 let mn: i64 = gl_read(meterpath, mb, CW_MAGIC_262144)
1529 if jn <= 0 {
1530 cw_apps(page, pos, cap, "<p class='muted'>journal not present in this build environment -- the NAS regeneration carries the real gauges; absence of the INPUT is not an empty ANSWER.</p>\n" as *u8)
1531 return 0
1532 }
1533 let done: *i64 = sys_mmap(16 * 8) as *i64
1534 let noncl: *i64 = sys_mmap(16 * 8) as *i64
1535 var z: i64 = 0
1536 while z < 16 { done[z] = 0; noncl[z] = 0; z = z + 1 }
1537 var maxep: i64 = 0
1538 var pass: i64 = 0
1539 var foreignc: i64 = 0
1540 var sovc: i64 = 0
1541 var donetot: i64 = 0
1542 var noncltot: i64 = 0
1543 var done7: i64 = 0
1544 var donep: i64 = 0
1545 var nc7: i64 = 0
1546 var ncp: i64 = 0
1547 var for7: i64 = 0
1548 var forp: i64 = 0
1549 var sov7: i64 = 0
1550 var sovp: i64 = 0
1551 while pass < 2 {
1552 var ls: i64 = 0
1553 while ls < jn {
1554 var le: i64 = ls
1555 var g1: i64 = 1
1556 while g1 == 1 { if le >= jn { g1 = 0 } else { if jb[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
1557 let f1e: i64 = cw_fld(jb, ls, le)
1558 if f1e < le {
1559 let ep: i64 = cw_atoi(jb, ls, f1e)
1560 if pass == 0 { if ep > maxep { maxep = ep } } else {
1561 let f2s: i64 = f1e + 1
1562 let f2e: i64 = cw_fld(jb, f2s, le)
1563 // TYPE == DONE (4 bytes)
1564 var isdone: i64 = 0
1565 if f2e - f2s == 4 { if jb[f2s] == (68 as u8) { if jb[f2s+1] == (79 as u8) { if jb[f2s+2] == (78 as u8) { if jb[f2s+3] == (69 as u8) { isdone = 1 } } } } }
1566 if isdone == 1 {
1567 donetot = donetot + 1
1568 let dd: i64 = (maxep / CW_MAGIC_86400) - (ep / CW_MAGIC_86400)
1569 let f3s: i64 = f2e + 1
1570 let f3e: i64 = cw_fld(jb, f3s, le)
1571 let f4s: i64 = f3e + 1
1572 let f4e: i64 = cw_fld(jb, f4s, le)
1573 // actor class: claude* prefix = seat; else local/other
1574 var iscl: i64 = 0
1575 if f4e - f4s >= 6 { if jb[f4s] == (99 as u8) { if jb[f4s+1] == (108 as u8) { if jb[f4s+2] == (97 as u8) { if jb[f4s+3] == (117 as u8) { if jb[f4s+4] == (100 as u8) { if jb[f4s+5] == (101 as u8) { iscl = 1 } } } } } } }
1576 if iscl == 0 { noncltot = noncltot + 1 }
1577 var wk: i64 = 2
1578 if dd >= 0 { if dd < 7 { wk = 0 } }
1579 if dd >= 7 { if dd < 14 { wk = 1 } }
1580 if dd >= 0 { if dd < 14 {
1581 done[dd] = done[dd] + 1
1582 if iscl == 0 { noncl[dd] = noncl[dd] + 1 }
1583 } }
1584 if wk == 0 { done7 = done7 + 1; if iscl == 0 { nc7 = nc7 + 1 } }
1585 if wk == 1 { donep = donep + 1; if iscl == 0 { ncp = ncp + 1 } }
1586 let f5s: i64 = f4e + 1
1587 var isfor: i64 = 0
1588 var issov: i64 = 0
1589 if cw_submatch(jb, f5s, le, "ssh " as *u8) == 1 { isfor = 1 } else {
1590 if cw_submatch(jb, f5s, le, "scp " as *u8) == 1 { isfor = 1 } else {
1591 if cw_submatch(jb, f5s, le, "curl" as *u8) == 1 { isfor = 1 } else {
1592 if cw_submatch(jb, f5s, le, "chrome" as *u8) == 1 { isfor = 1 } else {
1593 if cw_submatch(jb, f5s, le, "powershell" as *u8) == 1 { isfor = 1 } else {
1594 if cw_submatch(jb, f5s, le, "wsl " as *u8) == 1 { isfor = 1 } } } } } }
1595 if cw_submatch(jb, f5s, le, "nx_" as *u8) == 1 { issov = 1 } else {
1596 if cw_submatch(jb, f5s, le, "tools/call" as *u8) == 1 { issov = 1 } else {
1597 if cw_submatch(jb, f5s, le, "/api/" as *u8) == 1 { issov = 1 } } }
1598 if isfor == 1 { foreignc = foreignc + 1
1599 if wk == 0 { for7 = for7 + 1 }
1600 if wk == 1 { forp = forp + 1 } }
1601 if issov == 1 { sovc = sovc + 1
1602 if wk == 0 { sov7 = sov7 + 1 }
1603 if wk == 1 { sovp = sovp + 1 } }
1604 }
1605 }
1606 }
1607 ls = le + 1
1608 }
1609 pass = pass + 1
1610 }
1611 // week-over-week deltas of the two GOAL gauges drive the verdict (rule
1612 // declared in the envelope; permil math guards div-by-zero via max(x,1))
1613 var d7g: i64 = done7
1614 if d7g < 1 { d7g = 1 }
1615 var dpg: i64 = donep
1616 if dpg < 1 { dpg = 1 }
1617 let nc7p: i64 = (nc7 * 1000) / d7g
1618 let ncpp: i64 = (ncp * 1000) / dpg
1619 var fs7: i64 = for7 + sov7
1620 if fs7 < 1 { fs7 = 1 }
1621 var fsp: i64 = forp + sovp
1622 if fsp < 1 { fsp = 1 }
1623 let fo7p: i64 = (for7 * 1000) / fs7
1624 let fopp: i64 = (forp * 1000) / fsp
1625 let dnc: i64 = nc7p - ncpp
1626 let dfo: i64 = fo7p - fopp
1627 let ddn: i64 = done7 - donep
1628 var goods: i64 = 0
1629 if dnc > 0 { goods = goods + 1 }
1630 if dfo < 0 { goods = goods + 1 }
1631 cw_apps(page, pos, cap, "<div class='vd'><div class='big'>" as *u8)
1632 if goods == 2 { cw_apps(page, pos, cap, "The shift is moving." as *u8) }
1633 if goods == 1 { cw_apps(page, pos, cap, "Mixed signals this week." as *u8) }
1634 if goods == 0 { cw_apps(page, pos, cap, "The seat still carries the making plane." as *u8) }
1635 cw_apps(page, pos, cap, "</div><div class='sub'>" as *u8)
1636 cw_appn(page, pos, cap, done7)
1637 cw_apps(page, pos, cap, " closures in the last 7 journal days (" as *u8)
1638 cw_appn(page, pos, cap, donep)
1639 cw_apps(page, pos, cap, " the week before). Non-Claude actors closed " as *u8)
1640 cw_appn(page, pos, cap, nc7p)
1641 cw_apps(page, pos, cap, "‰ of them — this number should RISE as makers and beats take the mechanical middle. Foreign plumbing appears in " as *u8)
1642 cw_appn(page, pos, cap, fo7p)
1643 cw_apps(page, pos, cap, "‰ of tool mentions — it should FALL as sovereign verbs land. What moves both is ranked below.</div></div>\n" as *u8)
1644 // KPI band -- the data-* attributes are the GATE CONTRACT; markup may evolve
1645 cw_apps(page, pos, cap, "<div class='kpis'><div class='pk'><div class='lab'>non-Claude closures (7d)</div><div class='val' data-nc='" as *u8)
1646 cw_appn(page, pos, cap, nc7p)
1647 cw_apps(page, pos, cap, "'>" as *u8)
1648 cw_appn(page, pos, cap, nc7p)
1649 cw_apps(page, pos, cap, "‰</div>" as *u8)
1650 cw_dpill(page, pos, cap, dnc, 1, "‰" as *u8)
1651 cw_apps(page, pos, cap, "<div class='goal'>goal: rising · who FINISHES the work</div>" as *u8)
1652 cw_spark(page, pos, cap, noncl)
1653 cw_apps(page, pos, cap, "</div><div class='pk'><div class='lab'>foreign-tool mentions (7d)</div><div class='val' data-fo='" as *u8)
1654 cw_appn(page, pos, cap, fo7p)
1655 cw_apps(page, pos, cap, "'>" as *u8)
1656 cw_appn(page, pos, cap, fo7p)
1657 cw_apps(page, pos, cap, "‰</div>" as *u8)
1658 cw_dpill(page, pos, cap, dfo, 0, "‰" as *u8)
1659 cw_apps(page, pos, cap, "<div class='goal'>goal: falling · ssh/scp/curl/chrome vs sovereign verbs</div></div><div class='pk'><div class='lab'>closures this week</div><div class='val' data-done='" as *u8)
1660 cw_appn(page, pos, cap, done7)
1661 cw_apps(page, pos, cap, "'>" as *u8)
1662 cw_appn(page, pos, cap, done7)
1663 cw_apps(page, pos, cap, "</div>" as *u8)
1664 cw_dpill(page, pos, cap, ddn, 1, "" as *u8)
1665 cw_apps(page, pos, cap, "<div class='goal'>velocity · all actors · all-time " as *u8)
1666 cw_appn(page, pos, cap, donetot)
1667 cw_apps(page, pos, cap, "</div>" as *u8)
1668 cw_spark(page, pos, cap, done)
1669 cw_apps(page, pos, cap, "</div></div>\n" as *u8)
1670 // ONE story chart: all closures (area) + non-Claude (line), direct-labeled
1671 // at the right edge -- no legend to decode, time reads left to right
1672 var mx: i64 = 1
1673 var mi2: i64 = 0
1674 while mi2 < 14 { if done[mi2] > mx { mx = done[mi2] } mi2 = mi2 + 1 }
1675 cw_apps(page, pos, cap, "<h2>the last 14 journal days</h2>\n<div class='svgwrap'><svg width='660' height='230' viewBox='0 0 660 230'><defs><linearGradient id='gA' x1='0' y1='0' x2='0' y2='1'><stop offset='0' stop-color='var(--acc)' stop-opacity='.25'/><stop offset='1' stop-color='var(--acc)' stop-opacity='0'/></linearGradient></defs>" as *u8)
1676 cw_apps(page, pos, cap, "<line x1='44' y1='178' x2='580' y2='178' stroke='var(--br)'/><line x1='44' y1='98' x2='580' y2='98' stroke='var(--br)' stroke-dasharray='3 4'/><line x1='44' y1='18' x2='580' y2='18' stroke='var(--br)' stroke-dasharray='3 4'/>" as *u8)
1677 cw_apps(page, pos, cap, "<text x='40' y='182' font-size='10' text-anchor='end' fill='var(--svgt)'>0</text><text x='40' y='102' font-size='10' text-anchor='end' fill='var(--svgt)'>" as *u8)
1678 cw_appn(page, pos, cap, mx / 2)
1679 cw_apps(page, pos, cap, "</text><text x='40' y='22' font-size='10' text-anchor='end' fill='var(--svgt)'>" as *u8)
1680 cw_appn(page, pos, cap, mx)
1681 cw_apps(page, pos, cap, "</text>" as *u8)
1682 cw_apps(page, pos, cap, "<path fill='url(#gA)' stroke='none' d='M44,178 L" as *u8)
1683 cw_plpts(page, pos, cap, done, mx, 44, 18, 536, 160)
1684 cw_apps(page, pos, cap, " L580,178 Z'/><polyline fill='none' stroke='var(--acc)' stroke-width='2.5' stroke-linejoin='round' stroke-linecap='round' points='" as *u8)
1685 cw_plpts(page, pos, cap, done, mx, 44, 18, 536, 160)
1686 cw_apps(page, pos, cap, "'/><polyline fill='none' stroke='var(--acc2)' stroke-width='2' stroke-linejoin='round' points='" as *u8)
1687 cw_plpts(page, pos, cap, noncl, mx, 44, 18, 536, 160)
1688 cw_apps(page, pos, cap, "'/>" as *u8)
1689 cw_apps(page, pos, cap, "<text x='588' y='" as *u8)
1690 cw_appn(page, pos, cap, 182 - (done[0] * 160) / mx)
1691 cw_apps(page, pos, cap, "' font-size='11' fill='var(--acc)'>all " as *u8)
1692 cw_appn(page, pos, cap, done[0])
1693 cw_apps(page, pos, cap, "</text><text x='588' y='" as *u8)
1694 cw_appn(page, pos, cap, 196 - (noncl[0] * 160) / mx)
1695 cw_apps(page, pos, cap, "' font-size='11' fill='var(--acc2)'>non-Claude " as *u8)
1696 cw_appn(page, pos, cap, noncl[0])
1697 cw_apps(page, pos, cap, "</text><text x='44' y='212' font-size='9' fill='var(--svgt)'>14 days ago</text><text x='580' y='212' font-size='9' text-anchor='end' fill='var(--svgt)'>today</text></svg></div>\n" as *u8)
1698 // the decision rail: where to act on what the gauges say
1699 cw_apps(page, pos, cap, "<h2>what moves these numbers</h2>\n<div class='tiles'><div class='tile'><div class='v'>🔧</div><div class='k'><a href='tools" as *u8)
1700 cw_appext(page, pos, cap, noext2)
1701 cw_apps(page, pos, cap, "'>use the capability catalog</a> — a shell lane usually means the verb existed and was not found</div></div><div class='tile'><div class='v'>📋</div><div class='k'>the ranked build order: knowledge/status/self_sufficiency_map_20260729.md (seq1231 transfer verbs · seq1126 async-job · TS03 maker dispatch)</div></div><div class='tile'><div class='v'>🤖</div><div class='k'><a href='findings" as *u8)
1702 cw_appext(page, pos, cap, noext2)
1703 cw_apps(page, pos, cap, "'>the findings backlog</a> — work the maker beats can take next</div></div></div>\n" as *u8)
1704 // the token meter: a TABLE, because a missing meter day is ABSENT, not zero
1705 cw_apps(page, pos, cap, "<h2>seat token meter <span class='cnt'>(daily harvest rows; missing days are absent, never zero)</span></h2>\n" as *u8)
1706 if mn <= 0 {
1707 cw_apps(page, pos, cap, "<p class='muted'>meter log not present in this build environment.</p>\n" as *u8)
1708 } else {
1709 cw_apps(page, pos, cap, "<table><tr><th class='n'>day (relative)</th><th>seat</th><th class='n'>sessions</th><th class='n'>output tok</th><th class='n'>cache-read tok</th></tr>\n" as *u8)
1710 var ms: i64 = 0
1711 while ms < mn {
1712 var me: i64 = ms
1713 var g2: i64 = 1
1714 while g2 == 1 { if me >= mn { g2 = 0 } else { if mb[me] == (10 as u8) { g2 = 0 } else { me = me + 1 } } }
1715 let m1e: i64 = cw_fld(mb, ms, me)
1716 if m1e < me {
1717 let mep: i64 = cw_atoi(mb, ms, m1e)
1718 let m2s: i64 = m1e + 1
1719 let m2e: i64 = cw_fld(mb, m2s, me)
1720 let m3s: i64 = m2e + 1
1721 let m3e: i64 = cw_fld(mb, m3s, me)
1722 let m4s: i64 = m3e + 1
1723 let m4e: i64 = cw_fld(mb, m4s, me)
1724 let m5s: i64 = m4e + 1
1725 let m5e: i64 = cw_fld(mb, m5s, me)
1726 cw_apps(page, pos, cap, "<tr><td class='n'>d-" as *u8)
1727 var mdd: i64 = 0
1728 if maxep > 0 { mdd = (maxep / CW_MAGIC_86400) - (mep / CW_MAGIC_86400) }
1729 cw_appn(page, pos, cap, mdd)
1730 cw_apps(page, pos, cap, "</td><td><code>" as *u8)
1731 cw_appe(page, pos, cap, ((mb as i64) + m2s) as *u8, m2e - m2s)
1732 cw_apps(page, pos, cap, "</code></td><td class='n'>" as *u8)
1733 cw_appe(page, pos, cap, ((mb as i64) + m3s) as *u8, m3e - m3s)
1734 cw_apps(page, pos, cap, "</td><td class='n'>" as *u8)
1735 cw_appe(page, pos, cap, ((mb as i64) + m4s) as *u8, m4e - m4s)
1736 cw_apps(page, pos, cap, "</td><td class='n'>" as *u8)
1737 cw_appe(page, pos, cap, ((mb as i64) + m5s) as *u8, m5e - m5s)
1738 cw_apps(page, pos, cap, "</td></tr>\n" as *u8)
1739 }
1740 ms = me + 1
1741 }
1742 cw_apps(page, pos, cap, "</table>\n" as *u8)
1743 }
1744 cw_apps(page, pos, cap, "<p class='muted'>envelope: journal read whole (declared cap), newest epoch " as *u8)
1745 cw_appn(page, pos, cap, maxep)
1746 cw_apps(page, pos, cap, "; actor class = name prefix 'claude'; foreign/sovereign = declared substring lists over DONE notes (one mention per note per side); verdict = week-over-week deltas of the two goal gauges (both good = moving, one = mixed, none = stalled). The full build order: knowledge/status/self_sufficiency_map_20260729.md</p>\n" as *u8)
1747 return donetot
1748}
1749
1750// ---------- the capability catalog: SOTA discoverability (rung 19) ----------
1751// One row per REGISTERED tool -- name, what it does (schema title), the argv
1752// grammar (schema col 8), an anchor -- plus toolindex.js so client search and
1753// /code/ask retrieve CAPABILITIES exactly like modules. The discovery failure
1754// this eats is documented and personal: nx_page_verify existed all evening
1755// while its own consumer hand-rolled page verification. Inputs are parameters
1756// (the gate fixtures them); an absent input SAYS so, never renders as "none".
1757// schema-row resolver shared by the capability catalog and the askindex
1758// T-rows: find name's row (name = conf[ns..ne)) in the schema conf. box gets
1759// [title_s, title_e, grammar_s, grammar_e] as byte offsets into sch (-1 =
1760// absent). First match wins, exactly as the catalog always behaved. One copy
1761// so the page, toolindex.js and the retrieval corpus cannot drift apart.
1762func cw_schemarow(sch: *u8, schn: i64, conf: *u8, ns: i64, ne: i64, box: *i64) -> i64 {
1763 box[0] = 0 - 1
1764 box[1] = 0 - 1
1765 box[2] = 0 - 1
1766 box[3] = 0 - 1
1767 var found: i64 = 0
1768 var sls: i64 = 0
1769 while sls < schn {
1770 var sle: i64 = sls
1771 var sg: i64 = 1
1772 while sg == 1 { if sle >= schn { sg = 0 } else { if sch[sle] == (10 as u8) { sg = 0 } else { sle = sle + 1 } } }
1773 if found == 0 { if sle > sls { if sch[sls] != (35 as u8) {
1774 let sn1: i64 = cw_fld(sch, sls, sle)
1775 if sn1 - sls == ne - ns {
1776 var seq2: i64 = 1
1777 var sc2: i64 = 0
1778 while sc2 < sn1 - sls { if sch[sls + sc2] != conf[ns + sc2] { seq2 = 0; sc2 = sn1 - sls } else { sc2 = sc2 + 1 } }
1779 if seq2 == 1 {
1780 found = 1
1781 var fs2: i64 = sn1 + 1
1782 var fe2: i64 = cw_fld(sch, fs2, sle)
1783 box[0] = fs2
1784 box[1] = fe2
1785 // FIXED 2026-08-06. The walk used `fk` BOTH as the loop counter AND as the
1786 // success flag: running out of fields set `fk = 8`, which is exactly the value a
1787 // successful walk to field 8 produces. So a 7-field row (no authored col8 -- the
1788 // overwhelming majority) fell out with fks/fke still pointing at the LAST field it
1789 // did reach, col7, the "<name> output (registered via /api/tools/register)" stub.
1790 // MEASURED: 668 of 864 rows (77.3%) of the human "call grammar (argv)" column on
1791 // /code/tools were that stub, presented to a reader as if it were a call contract.
1792 // ★ A LOOP THAT EXITS EARLY BY SETTING THE SAME SENTINEL AS SUCCESS CANNOT
1793 // DISTINGUISH "ARRIVED" FROM "GAVE UP" -- and the failure is SILENT and PLAUSIBLE.
1794 // A separate `reached` flag makes absence absence again; the caller then falls back
1795 // to the DERIVED grammar (nx_toolgrammar), or renders nothing at all.
1796 var fk: i64 = 2
1797 var fks: i64 = fs2
1798 var fke: i64 = fe2
1799 var reached: i64 = 0
1800 while fk < 8 {
1801 if fke < sle {
1802 fks = fke + 1
1803 fke = cw_fld(sch, fks, sle)
1804 fk = fk + 1
1805 if fk == 8 { reached = 1 }
1806 } else { fk = 8 }
1807 }
1808 if reached == 1 { if fke > fks { box[2] = fks; box[3] = fke } }
1809 }
1810 }
1811 } } }
1812 sls = sle + 1
1813 }
1814 return found
1815}
1816
1817// DERIVED-grammar sibling of the schema conf: `<dir>/tool_grammar.conf` next to the schema path we were
1818// GIVEN, so the gate keeps fixturing inputs by parameter (no hardcoded prod path) and no caller signature
1819// changes. Written by nx_toolgrammar from each organ's own source; consulted ONLY when the schema conf
1820// declares no col8, so an AUTHORED contract always wins over a derived one.
1821func cw_grampath(schemapath: *u8, out: *u8) -> i64 {
1822 var n: i64 = 0
1823 while schemapath[n] != (0 as u8) { n = n + 1 }
1824 var cut: i64 = n
1825 while cut > 0 { if schemapath[cut-1] == (47 as u8) { cut = 0 - cut } else { cut = cut - 1 } }
1826 var base: i64 = 0
1827 if cut < 0 { base = 0 - cut }
1828 var o: i64 = 0
1829 while o < base { out[o] = schemapath[o]; o = o + 1 }
1830 let tail: *u8 = "tool_grammar.conf" as *u8
1831 var t: i64 = 0
1832 while tail[t] != (0 as u8) { out[o] = tail[t]; o = o + 1; t = t + 1 }
1833 out[o] = 0 as u8
1834 return o
1835}
1836// find name (conf[ns..ne)) in the derived grammar conf -> box2 = [start,end] into gb; 1 found / 0 absent
1837func cw_gramrow(gb: *u8, gn: i64, conf: *u8, ns: i64, ne: i64, box2: *i64) -> i64 {
1838 box2[0] = 0 - 1
1839 box2[1] = 0 - 1
1840 if gn <= 0 { return 0 }
1841 var ls: i64 = 0
1842 while ls < gn {
1843 var le: i64 = ls
1844 var g: i64 = 1
1845 while g == 1 { if le >= gn { g = 0 } else { if gb[le] == (10 as u8) { g = 0 } else { le = le + 1 } } }
1846 if le > ls { if gb[ls] != (35 as u8) {
1847 let f1: i64 = cw_fld(gb, ls, le)
1848 if f1 - ls == ne - ns {
1849 var eq: i64 = 1
1850 var c: i64 = 0
1851 while c < f1 - ls { if gb[ls + c] != conf[ns + c] { eq = 0; c = f1 - ls } else { c = c + 1 } }
1852 if eq == 1 { if f1 < le { box2[0] = f1 + 1; box2[1] = le; return 1 } }
1853 }
1854 } }
1855 ls = le + 1
1856 }
1857 return 0
1858}
1859
1860// Emits catalog rows into page and JS rows into tix; returns row count.
1861func cw_tools(page: *u8, pos: *i64, cap: i64, tix: *u8, tpos: *i64,
1862 allowpath: *u8, schemapath: *u8) -> i64 {
1863 let conf: *u8 = sys_mmap(CW_EXPCONF)
1864 let confn: i64 = gl_read(allowpath, conf, CW_EXPCONF)
1865 let sch: *u8 = sys_mmap(CW_EXPCONF)
1866 let schn: i64 = gl_read(schemapath, sch, CW_EXPCONF)
1867 let sbox: *i64 = sys_mmap(64) as *i64
1868 let gpath: *u8 = sys_mmap(512)
1869 cw_grampath(schemapath, gpath)
1870 let gb: *u8 = sys_mmap(CW_EXPCONF)
1871 let gn: i64 = gl_read(gpath, gb, CW_EXPCONF)
1872 let gbox: *i64 = sys_mmap(64) as *i64
1873 cw_apps(tix, tpos, CW_PAGECAP, "var TOOLS=[" as *u8)
1874 var nrows: i64 = 0
1875 if confn <= 0 {
1876 cw_apps(page, pos, cap, "<p class='muted'>allowlist not present in this build environment -- the NAS regeneration carries the real catalog; absence of the INPUT is not an empty ANSWER.</p>\n" as *u8)
1877 } else {
1878 cw_apps(page, pos, cap, "<table><tr><th>capability</th><th>what it does</th><th>call grammar (argv)</th></tr>\n" as *u8)
1879 var ls: i64 = 0
1880 while ls < confn {
1881 var le: i64 = ls
1882 var g1: i64 = 1
1883 while g1 == 1 { if le >= confn { g1 = 0 } else { if conf[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
1884 if le > ls + 2 { if conf[ls] != (35 as u8) {
1885 let f1e: i64 = cw_fld(conf, ls, le)
1886 if f1e < le {
1887 nrows = nrows + 1
1888 // locate this tool's schema row via the shared resolver
1889 // (title = field 2, grammar = field 8): one copy feeds the
1890 // catalog page, toolindex.js AND the askindex T-rows.
1891 cw_schemarow(sch, schn, conf, ls, f1e, sbox)
1892 let tts: i64 = sbox[0]
1893 let tte: i64 = sbox[1]
1894 let tgs: i64 = sbox[2]
1895 let tge: i64 = sbox[3]
1896 cw_apps(page, pos, cap, "<tr data-m='" as *u8)
1897 cw_appe(page, pos, cap, ((conf as i64) + ls) as *u8, f1e - ls)
1898 cw_apps(page, pos, cap, "'><td id='t_" as *u8)
1899 cw_appe(page, pos, cap, ((conf as i64) + ls) as *u8, f1e - ls)
1900 cw_apps(page, pos, cap, "'><code>" as *u8)
1901 cw_appe(page, pos, cap, ((conf as i64) + ls) as *u8, f1e - ls)
1902 cw_apps(page, pos, cap, "</code></td><td class='muted'>" as *u8)
1903 if tte > tts { cw_appe(page, pos, cap, ((sch as i64) + tts) as *u8, tte - tts) } else { cw_apps(page, pos, cap, "(capability undocumented -- the schema-less finding covers it)" as *u8) }
1904 // PRECEDENCE, one place, feeding BOTH the human table and toolindex.js so they
1905 // cannot drift: AUTHORED col8 > DERIVED grammar (nx_toolgrammar) > nothing at all.
1906 // "Nothing" is now genuinely nothing -- before the sentinel fix above it was col7's
1907 // registration stub masquerading as a call contract on 77.3% of rows.
1908 var grs: i64 = 0 - 1
1909 var gre: i64 = 0 - 1
1910 var grbuf: *u8 = sch
1911 if tge > tgs { grs = tgs; gre = tge } else {
1912 if cw_gramrow(gb, gn, conf, ls, f1e, gbox) == 1 { grs = gbox[0]; gre = gbox[1]; grbuf = gb }
1913 }
1914 cw_apps(page, pos, cap, "</td><td class='sig'>" as *u8)
1915 if gre > grs { cw_appe(page, pos, cap, ((grbuf as i64) + grs) as *u8, gre - grs) }
1916 cw_apps(page, pos, cap, "</td></tr>\n" as *u8)
1917 cw_apps(tix, tpos, CW_PAGECAP, "[\"" as *u8)
1918 cw_appjse(tix, tpos, CW_PAGECAP, ((conf as i64) + ls) as *u8, f1e - ls)
1919 cw_apps(tix, tpos, CW_PAGECAP, "\",\"" as *u8)
1920 if tte > tts { cw_appjse(tix, tpos, CW_PAGECAP, ((sch as i64) + tts) as *u8, tte - tts) }
1921 cw_apps(tix, tpos, CW_PAGECAP, "\",\"" as *u8)
1922 if gre > grs { cw_appjse(tix, tpos, CW_PAGECAP, ((grbuf as i64) + grs) as *u8, gre - grs) }
1923 cw_apps(tix, tpos, CW_PAGECAP, "\"],\n" as *u8)
1924 }
1925 } }
1926 ls = le + 1
1927 }
1928 cw_apps(page, pos, cap, "</table>\n" as *u8)
1929 }
1930 cw_apps(tix, tpos, CW_PAGECAP, "];\n" as *u8)
1931 return nrows
1932}
1933
1934// askindex T-rows: capabilities as RETRIEVAL RECORDS (rung 23 -- the organ
1935// side of rung 19's discoverability law: nx_codewiki_ask and the answer beat
1936// must retrieve capabilities exactly like modules; the browser side already
1937// does via toolindex.js). Emits T<TAB>name<TAB>title<TAB>grammar per
1938// allowlist row (TSV-safe), returns the row count. Inputs are parameters so
1939// the gate fixtures the join without touching the prod CWD.
1940func cw_asktools(abuf: *u8, apos: *i64, acap: i64, allowpath: *u8, schemapath: *u8) -> i64 {
1941 let conf: *u8 = sys_mmap(CW_EXPCONF)
1942 let confn: i64 = gl_read(allowpath, conf, CW_EXPCONF)
1943 if confn <= 0 { return 0 }
1944 let sch: *u8 = sys_mmap(CW_EXPCONF)
1945 let schn: i64 = gl_read(schemapath, sch, CW_EXPCONF)
1946 let sbox: *i64 = sys_mmap(64) as *i64
1947 var nrows: i64 = 0
1948 var ls: i64 = 0
1949 while ls < confn {
1950 var le: i64 = ls
1951 var g1: i64 = 1
1952 while g1 == 1 { if le >= confn { g1 = 0 } else { if conf[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
1953 if le > ls + 2 { if conf[ls] != (35 as u8) {
1954 let f1e: i64 = cw_fld(conf, ls, le)
1955 if f1e < le {
1956 nrows = nrows + 1
1957 cw_schemarow(sch, schn, conf, ls, f1e, sbox)
1958 cw_apps(abuf, apos, acap, "T\t" as *u8)
1959 cw_apptsv(abuf, apos, acap, ((conf as i64) + ls) as *u8, f1e - ls)
1960 cw_apps(abuf, apos, acap, "\t" as *u8)
1961 if sbox[1] > sbox[0] { cw_apptsv(abuf, apos, acap, ((sch as i64) + sbox[0]) as *u8, sbox[1] - sbox[0]) }
1962 cw_apps(abuf, apos, acap, "\t" as *u8)
1963 if sbox[3] > sbox[2] { cw_apptsv(abuf, apos, acap, ((sch as i64) + sbox[2]) as *u8, sbox[3] - sbox[2]) }
1964 cw_apps(abuf, apos, acap, "\n" as *u8)
1965 }
1966 } }
1967 ls = le + 1
1968 }
1969 return nrows
1970}
1971
1972// ---------- atlas: the family -> domain conformance ruler (seq1220) ----------
1973//
1974// The ecosystem's domain taxonomy is DECLARED data (the atlas conf: rows
1975// family<TAB>domain, # comments) joined against the tree's MEASURED families.
1976// Renders: conformance permil by LOC (goal RISING, data-conf contract attr),
1977// the mapped domain table, the unmapped work list biggest-LOC-first, and
1978// stale conf rows (a mapping naming no current family) -- declared, never
1979// silently dropped. Files one aggregate finding while unmapped families
1980// exist. Inputs are parameters so the gate fixtures the join.
1981func cw_atlas(page: *u8, pos: *i64, cap: i64,
1982 famarena: *u8, famoff: *i64, famlen: *i64, famcnt: *i64,
1983 famloc: *i64, nfam: i64, atlaspath: *u8, noext: i64,
1984 fbuf: *u8, fpos: *i64) -> i64 {
1985 let conf: *u8 = sys_mmap(CW_EXPCONF)
1986 let confn: i64 = gl_read(atlaspath, conf, CW_EXPCONF)
1987 if confn <= 0 {
1988 cw_apps(page, pos, cap, "<p class='muted'>atlas conf not present in this build environment -- the NAS regeneration carries the real mapping; absence of the INPUT is not an empty ANSWER. Every eligible family below is UNMAPPED by definition of this run.</p>\n" as *u8)
1989 }
1990 let famdom: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
1991 let famdle: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
1992 var fz: i64 = 0
1993 while fz < nfam { famdom[fz] = 0 - 1; famdle[fz] = 0 - 1; fz = fz + 1 }
1994 var stale: i64 = 0
1995 let stbuf: *u8 = sys_mmap(CW_MAGIC_4096)
1996 let stpos: *i64 = sys_mmap(32) as *i64
1997 stpos[0] = 0
1998 stpos[1] = 0
1999 var ls: i64 = 0
2000 while ls < confn {
2001 var le: i64 = ls
2002 var g1: i64 = 1
2003 while g1 == 1 { if le >= confn { g1 = 0 } else { if conf[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
2004 if le > ls + 2 { if conf[ls] != (35 as u8) {
2005 let f1e: i64 = cw_fld(conf, ls, le)
2006 if f1e < le {
2007 let d2s: i64 = f1e + 1
2008 let d2e: i64 = cw_fld(conf, d2s, le)
2009 var fidx: i64 = 0 - 1
2010 var ff: i64 = 0
2011 while ff < nfam {
2012 if fidx < 0 { if famlen[ff] == f1e - ls {
2013 var eq: i64 = 1
2014 var fc: i64 = 0
2015 while fc < famlen[ff] { if famarena[famoff[ff] + fc] != conf[ls + fc] { eq = 0; fc = famlen[ff] } else { fc = fc + 1 } }
2016 if eq == 1 { fidx = ff }
2017 } }
2018 ff = ff + 1
2019 }
2020 if fidx >= 0 { famdom[fidx] = d2s; famdle[fidx] = d2e } else {
2021 stale = stale + 1
2022 cw_apps(stbuf, stpos, CW_MAGIC_4096, "<code>" as *u8)
2023 cw_appe(stbuf, stpos, CW_MAGIC_4096, ((conf as i64) + ls) as *u8, f1e - ls)
2024 cw_apps(stbuf, stpos, CW_MAGIC_4096, "</code> " as *u8)
2025 }
2026 }
2027 } }
2028 ls = le + 1
2029 }
2030 var elig: i64 = 0
2031 var mapped: i64 = 0
2032 var tloc: i64 = 0
2033 var mloc: i64 = 0
2034 var fe: i64 = 0
2035 while fe < nfam {
2036 if famcnt[fe] >= CW_FAMMIN {
2037 elig = elig + 1
2038 tloc = tloc + famloc[fe]
2039 if famdom[fe] >= 0 { mapped = mapped + 1; mloc = mloc + famloc[fe] }
2040 }
2041 fe = fe + 1
2042 }
2043 var permil: i64 = 0
2044 if tloc > 0 { permil = (mloc * 1000) / tloc }
2045 cw_apps(page, pos, cap, "<p>conformance: <b data-conf='" as *u8)
2046 cw_appn(page, pos, cap, permil)
2047 cw_apps(page, pos, cap, "'>" as *u8)
2048 cw_appn(page, pos, cap, permil)
2049 cw_apps(page, pos, cap, "‰</b> of eligible-family LOC is atlas-mapped (goal RISING) · " as *u8)
2050 cw_appn(page, pos, cap, mapped)
2051 cw_apps(page, pos, cap, " of " as *u8)
2052 cw_appn(page, pos, cap, elig)
2053 cw_apps(page, pos, cap, " eligible families mapped</p>\n" as *u8)
2054 cw_apps(page, pos, cap, "<h2>domains</h2>\n<table><tr><th>domain</th><th>family</th><th>modules</th><th>loc</th></tr>\n" as *u8)
2055 var fm: i64 = 0
2056 while fm < nfam {
2057 if famcnt[fm] >= CW_FAMMIN { if famdom[fm] >= 0 {
2058 cw_apps(page, pos, cap, "<tr><td>" as *u8)
2059 cw_appe(page, pos, cap, ((conf as i64) + famdom[fm]) as *u8, famdle[fm] - famdom[fm])
2060 cw_apps(page, pos, cap, "</td><td><a href='t_" as *u8)
2061 cw_appe(page, pos, cap, ((famarena as i64) + famoff[fm]) as *u8, famlen[fm])
2062 cw_appext(page, pos, cap, noext)
2063 cw_apps(page, pos, cap, "'>" as *u8)
2064 cw_appe(page, pos, cap, ((famarena as i64) + famoff[fm]) as *u8, famlen[fm])
2065 cw_apps(page, pos, cap, "</a></td><td class='n'>" as *u8)
2066 cw_appn(page, pos, cap, famcnt[fm])
2067 cw_apps(page, pos, cap, "</td><td class='n'>" as *u8)
2068 cw_appn(page, pos, cap, famloc[fm])
2069 cw_apps(page, pos, cap, "</td></tr>\n" as *u8)
2070 } }
2071 fm = fm + 1
2072 }
2073 cw_apps(page, pos, cap, "</table>\n<h2>unmapped families (biggest LOC first -- the work list)</h2>\n<table><tr><th>family</th><th>modules</th><th>loc</th></tr>\n" as *u8)
2074 // selection-emit by descending LOC; famloc entries are negated as a
2075 // visited mark and RESTORED after (the table is a view, not a mutation)
2076 var guard: i64 = 0
2077 while guard < nfam {
2078 var best: i64 = 0 - 1
2079 var bl: i64 = 0 - 1
2080 var fu: i64 = 0
2081 while fu < nfam {
2082 if famcnt[fu] >= CW_FAMMIN { if famdom[fu] < 0 { if famloc[fu] > bl {
2083 best = fu
2084 bl = famloc[fu]
2085 } } }
2086 fu = fu + 1
2087 }
2088 if best < 0 { guard = nfam } else {
2089 cw_apps(page, pos, cap, "<tr><td><a href='t_" as *u8)
2090 cw_appe(page, pos, cap, ((famarena as i64) + famoff[best]) as *u8, famlen[best])
2091 cw_appext(page, pos, cap, noext)
2092 cw_apps(page, pos, cap, "'>" as *u8)
2093 cw_appe(page, pos, cap, ((famarena as i64) + famoff[best]) as *u8, famlen[best])
2094 cw_apps(page, pos, cap, "</a></td><td class='n'>" as *u8)
2095 cw_appn(page, pos, cap, famcnt[best])
2096 cw_apps(page, pos, cap, "</td><td class='n'>" as *u8)
2097 cw_appn(page, pos, cap, famloc[best])
2098 cw_apps(page, pos, cap, "</td></tr>\n" as *u8)
2099 famloc[best] = 0 - famloc[best] - 1
2100 guard = guard + 1
2101 }
2102 }
2103 var fr: i64 = 0
2104 while fr < nfam { if famloc[fr] < 0 { famloc[fr] = 0 - (famloc[fr] + 1) } fr = fr + 1 }
2105 cw_apps(page, pos, cap, "</table>\n" as *u8)
2106 if stale > 0 {
2107 cw_apps(page, pos, cap, "<h2>stale mapping rows (conf names no current family)</h2>\n<p class='muted'>" as *u8)
2108 cw_app(page, pos, cap, stbuf, stpos[0])
2109 cw_apps(page, pos, cap, "</p>\n" as *u8)
2110 }
2111 if elig - mapped > 0 {
2112 cw_fnd(fbuf, fpos, 5, "codewiki-atlas" as *u8, "Atlas-unmapped families present: eligible families carry no domain row in the atlas conf. https://nishifamily.com/code/atlas -- add the family-to-domain row (rule 11: extend the conf); conformance rises at the next regen." as *u8)
2113 }
2114 return elig
2115}
2116
2117// Index of `needle` in NUL-terminated `name`, or -1. cw_namehas answers
2118// "is it there"; the sprawl rule needs WHERE, to derive the base name.
2119func cw_nameidx(name: *u8, needle: *u8) -> i64 {
2120 let n: i64 = gl_len(name)
2121 let m: i64 = gl_len(needle)
2122 if m <= 0 { return 0 - 1 }
2123 var i: i64 = 0
2124 while i + m <= n {
2125 var k: i64 = 0
2126 var ok: i64 = 1
2127 while k < m { if name[i + k] != needle[k] { ok = 0; k = m } else { k = k + 1 } }
2128 if ok == 1 { return i }
2129 i = i + 1
2130 }
2131 return 0 - 1
2132}
2133
2134// Is `<base>.elf` -- the name truncated at its version marker -- ALSO in the
2135// deployed set? That is the WHOLE difference between a stale-promote hazard
2136// and an organ whose NAME merely carries a version.
2137//
2138// `sites_new2.elf` beside `sites.elf` is genuinely ambiguous: a promote could
2139// pick either, which is what the finding warns about. `nx_signaling_v2.elf`
2140// with NO `nx_signaling.elf` is simply an organ called v2 -- and the edge SSOT
2141// itself is `nx_sites_daemon_v2`. Flagging those is a false alarm, and a gauge
2142// that cries wolf teaches its reader to ignore the real rows.
2143func cw_has_base_sibling(earena: *u8, eoff: *i64, nelf: i64, cut: i64, snm: *u8) -> i64 {
2144 if cut <= 0 { return 0 }
2145 let bb: *u8 = sys_mmap(CW_MAGIC_4096)
2146 var b: i64 = 0
2147 while b < cut { bb[b] = snm[b]; b = b + 1 }
2148 bb[b] = 46 as u8
2149 bb[b + 1] = 101 as u8
2150 bb[b + 2] = 108 as u8
2151 bb[b + 3] = 102 as u8
2152 bb[b + 4] = 0 as u8
2153 let bl: i64 = b + 4
2154 var e: i64 = 0
2155 while e < nelf {
2156 let o: *u8 = ((earena as i64) + eoff[e]) as *u8
2157 if gl_len(o) == bl {
2158 var q: i64 = 0
2159 var same: i64 = 1
2160 while q < bl { if o[q] != bb[q] { same = 0; q = bl } else { q = q + 1 } }
2161 if same == 1 { return 1 }
2162 }
2163 e = e + 1
2164 }
2165 return 0
2166}
2167
2168// ---------- exposure: where MCP / API coverage is and is not ----------
2169//
2170// Joins three ground truths the ecosystem already maintains -- the MCP
2171// allowlist (name<TAB>elf<TAB>GREEN[<TAB>args]), the deployed *.elf set in the
2172// host dir, and this wiki's own module+kind table -- into the map the operator
2173// asked for: what is EXPOSED, what is DEPLOYED BUT DARK (no MCP row), and how
2174// long the local-only tail is. Inputs are parameters so the gate can fixture
2175// them; when an input is absent (laptop build) the page SAYS so instead of
2176// rendering an empty section as if the answer were "none".
2177// Returns exposed-row count; writes dark count to darkbox[0].
2178func cw_exposure(page: *u8, pos: *i64, cap: i64, g: *EcoGraph, pathoff: *i64,
2179 kindv: *i64, sidx: *i64, nsorted: i64, noext: i64,
2180 confpath: *u8, elfdir: *u8, darkbox: *i64, fbuf: *u8, fpos: *i64,
2181 srcmt: *i64) -> i64 {
2182 let conf: *u8 = sys_mmap(CW_EXPCONF)
2183 let confn: i64 = gl_read(confpath, conf, CW_EXPCONF)
2184
2185 // deployed *.elf basenames (one directory, non-recursive, cap declared)
2186 let earena: *u8 = sys_mmap(CW_EXPARENA)
2187 let eoff: *i64 = sys_mmap(CW_EXPELFS * 8) as *i64
2188 var nelf: i64 = 0
2189 var eu: i64 = 0
2190 let dfd: i64 = sys_openat_rd(elfdir)
2191 if dfd >= 0 {
2192 let dbuf: *u8 = sys_mmap(CW_MAGIC_131072)
2193 var go: i64 = 1
2194 while go == 1 {
2195 let nr: i64 = sys_getdents64(dfd, dbuf, CW_MAGIC_131072)
2196 if nr <= 0 { go = 0 } else {
2197 var off: i64 = 0
2198 while off < nr {
2199 let rec: *u8 = ((dbuf as i64) + off) as *u8
2200 let nm: *u8 = dirent_name(rec)
2201 let nl: i64 = gl_len(nm)
2202 if gl_ends(nm, nl, ".elf" as *u8) == 1 { if nelf < CW_EXPELFS { if eu + nl + 1 < CW_EXPARENA {
2203 eoff[nelf] = eu
2204 var c0: i64 = 0
2205 while c0 <= nl { earena[eu + c0] = nm[c0]; c0 = c0 + 1 }
2206 eu = eu + nl + 1
2207 nelf = nelf + 1
2208 } } }
2209 off = off + dirent_reclen(rec)
2210 }
2211 }
2212 }
2213 sys_close(dfd)
2214 }
2215
2216 // the CAPABILITY layer: tool_schemas.conf carries what each tool DOES
2217 // (title) + its MCP safety annotations. Joining it here turns the exposure
2218 // page from a list of names into the team's living capability catalog.
2219 let sch: *u8 = sys_mmap(CW_EXPCONF)
2220 let schn: i64 = gl_read("knowledge/tool_schemas.conf" as *u8, sch, CW_EXPCONF)
2221
2222 cw_apps(page, pos, cap, "<h2 id='exposure'>exposure: MCP + API coverage</h2>\n" as *u8)
2223 if confn <= 0 {
2224 cw_apps(page, pos, cap, "<p class='muted'>allowlist not present in this build environment -- the NAS regeneration carries the real join; absence of the INPUT is not an empty ANSWER.</p>\n" as *u8)
2225 }
2226
2227 // pass 1 over the allowlist: emit exposed rows, remember exposed elf names
2228 let xoff: *i64 = sys_mmap(CW_EXPELFS * 8) as *i64
2229 var nexp: i64 = 0
2230 var nundoc: i64 = 0
2231 if confn > 0 {
2232 cw_apps(page, pos, cap, "<h2>MCP-exposed capabilities <span class='cnt'>what the team can DO -- title + safety annotations from the live schema registry, joined to the code</span></h2>\n<table><tr><th>capability</th><th>what it does</th><th>module</th></tr>\n" as *u8)
2233 var ls: i64 = 0
2234 while ls < confn {
2235 var le: i64 = ls
2236 var g1: i64 = 1
2237 while g1 == 1 { if le >= confn { g1 = 0 } else { if conf[le] == (10 as u8) { g1 = 0 } else { le = le + 1 } } }
2238 if le > ls + 2 { if conf[ls] != (35 as u8) {
2239 let f1e: i64 = cw_fld(conf, ls, le)
2240 if f1e < le {
2241 let f2s: i64 = f1e + 1
2242 let f2e: i64 = cw_fld(conf, f2s, le)
2243 // elf basename
2244 var bs: i64 = f2s
2245 var bi: i64 = f2s
2246 while bi < f2e { if conf[bi] == (47 as u8) { bs = bi + 1 } bi = bi + 1 }
2247 if nexp < CW_EXPELFS { xoff[nexp] = bs }
2248 nexp = nexp + 1
2249 cw_apps(page, pos, cap, "<tr><td><code>" as *u8)
2250 cw_appe(page, pos, cap, ((conf as i64) + ls) as *u8, f1e - ls)
2251 cw_apps(page, pos, cap, "</code></td><td class='muted'>" as *u8)
2252 // capability title: find this tool's row in tool_schemas.conf
2253 var tfound: i64 = 0
2254 if schn > 0 {
2255 var sls: i64 = 0
2256 while sls < schn {
2257 var sle: i64 = sls
2258 var sg: i64 = 1
2259 while sg == 1 { if sle >= schn { sg = 0 } else { if sch[sle] == (10 as u8) { sg = 0 } else { sle = sle + 1 } } }
2260 if tfound == 0 { if sle > sls { if sch[sls] != (35 as u8) {
2261 let sn1: i64 = cw_fld(sch, sls, sle)
2262 if sn1 - sls == f1e - ls {
2263 var seq2: i64 = 1
2264 var sc2: i64 = 0
2265 while sc2 < sn1 - sls { if sch[sls + sc2] != conf[ls + sc2] { seq2 = 0; sc2 = sn1 - sls } else { sc2 = sc2 + 1 } }
2266 if seq2 == 1 {
2267 let st1: i64 = sn1 + 1
2268 let st2: i64 = cw_fld(sch, st1, sle)
2269 if st2 > st1 { cw_appe(page, pos, cap, ((sch as i64) + st1) as *u8, st2 - st1); tfound = 1 }
2270 }
2271 }
2272 } } }
2273 sls = sle + 1
2274 }
2275 }
2276 if tfound == 0 { cw_apps(page, pos, cap, "<i>no schema row -- capability undocumented</i>" as *u8); nundoc = nundoc + 1 }
2277 cw_apps(page, pos, cap, "</td><td>" as *u8)
2278 // elf basename minus .elf + ".nx" -> module?
2279 let stl: i64 = f2e - bs - 4
2280 var found: i64 = 0 - 1
2281 if stl > 0 { if stl < 90 {
2282 let nmb: *u8 = sys_mmap(128)
2283 var c1: i64 = 0
2284 while c1 < stl { nmb[c1] = conf[bs + c1]; c1 = c1 + 1 }
2285 cw_rawcpy(nmb, stl, ".nx" as *u8)
2286 nmb[stl + 3] = 0 as u8
2287 found = eg_find(g, nmb, stl + 3)
2288 if found >= 0 { if pathoff[found] == 0 { found = 0 - 1 } }
2289 if found >= 0 { cw_applink2(page, pos, cap, nmb, noext) }
2290 } }
2291 if found < 0 { cw_apps(page, pos, cap, "<span class='muted'>no module in this tree</span>" as *u8) }
2292 cw_apps(page, pos, cap, "</td></tr>\n" as *u8)
2293 }
2294 } }
2295 ls = le + 1
2296 }
2297 cw_apps(page, pos, cap, "</table>\n" as *u8)
2298 if nundoc > 0 {
2299 cw_apps(page, pos, cap, "<p class='muted'>capabilities exposed but carrying no schema row (callable, undocumented): " as *u8)
2300 cw_appn(page, pos, cap, nundoc)
2301 cw_apps(page, pos, cap, " -- an agent can invoke them but cannot learn what they do.</p>\n" as *u8)
2302 cw_fnd(fbuf, fpos, 5, "capabilities" as *u8,
2303 "MCP capabilities are exposed without a schema row (callable but undocumented -- an agent can invoke them without learning what they do). https://nishifamily.com/code/exposure -- add a title via /api/tools/register (title creates the schema row) per capability." as *u8)
2304 }
2305 }
2306
2307 // deployed elfs with NO allowlist row = the dark-capability list
2308 var ndark: i64 = 0
2309 if nelf > 0 {
2310 cw_apps(page, pos, cap, "<h2>deployed but NOT MCP-exposed <span class='cnt'>the dark-capability list -- where an MCP row is likely needed</span></h2>\n<p>" as *u8)
2311 var e1: i64 = 0
2312 while e1 < nelf {
2313 let enm: *u8 = ((earena as i64) + eoff[e1]) as *u8
2314 let enl: i64 = gl_len(enm)
2315 var isexp: i64 = 0
2316 var x1: i64 = 0
2317 while x1 < nexp { if x1 < CW_EXPELFS {
2318 let xs: i64 = xoff[x1]
2319 var eq: i64 = 1
2320 var c2: i64 = 0
2321 while c2 < enl { if conf[xs + c2] != enm[c2] { eq = 0; c2 = enl } else { c2 = c2 + 1 } }
2322 if eq == 1 { isexp = 1 }
2323 } x1 = x1 + 1 }
2324 if isexp == 0 {
2325 ndark = ndark + 1
2326 if ndark <= CW_EXPSHOW {
2327 cw_apps(page, pos, cap, "<span class='pill'>" as *u8)
2328 // module link when the stem matches
2329 let stl2: i64 = enl - 4
2330 var f2: i64 = 0 - 1
2331 if stl2 > 0 { if stl2 < 90 {
2332 let nmb2: *u8 = sys_mmap(128)
2333 var c3: i64 = 0
2334 while c3 < stl2 { nmb2[c3] = enm[c3]; c3 = c3 + 1 }
2335 cw_rawcpy(nmb2, stl2, ".nx" as *u8)
2336 nmb2[stl2 + 3] = 0 as u8
2337 f2 = eg_find(g, nmb2, stl2 + 3)
2338 if f2 >= 0 { if pathoff[f2] == 0 { f2 = 0 - 1 } }
2339 if f2 >= 0 { cw_applink2(page, pos, cap, nmb2, noext) }
2340 } }
2341 if f2 < 0 { cw_appe(page, pos, cap, enm, enl) }
2342 cw_apps(page, pos, cap, "</span>" as *u8)
2343 }
2344 }
2345 e1 = e1 + 1
2346 }
2347 if ndark > CW_EXPSHOW {
2348 cw_apps(page, pos, cap, " <span class='muted'>+" as *u8)
2349 cw_appn(page, pos, cap, ndark - CW_EXPSHOW)
2350 cw_apps(page, pos, cap, " more (cap " as *u8)
2351 cw_appn(page, pos, cap, CW_EXPSHOW)
2352 cw_apps(page, pos, cap, " declared)</span>" as *u8)
2353 }
2354 if ndark == 0 { cw_apps(page, pos, cap, "<span class='muted'>none -- every deployed elf has an MCP row</span>" as *u8) }
2355 cw_apps(page, pos, cap, "</p>\n" as *u8)
2356 if ndark > 0 {
2357 cw_fnd(fbuf, fpos, 5, "exposure" as *u8,
2358 "Deployed elfs without MCP allowlist rows exist (dark capabilities). Live list: https://nishifamily.com/code/exposure -- decide expose vs retire per capability." as *u8)
2359 }
2360 }
2361
2362 // promote-sprawl: deployed binaries whose name carries a "_new" or "_v<n>"
2363 // version suffix = the sites_new2..8 class. These are stale-promote hazards
2364 // (a restart could pick the wrong one) AND naming noise. Listed + FILED.
2365 var nsp: i64 = 0
2366 if nelf > 0 {
2367 cw_apps(page, pos, cap, "<h2>versioned deployed binaries <span class='cnt'>_new / _v<n> names -- stale-promote hazard + naming noise</span></h2>\n<p>" as *u8)
2368 var sp0: i64 = 0
2369 while sp0 < nelf {
2370 let snm: *u8 = ((earena as i64) + eoff[sp0]) as *u8
2371 var isspr: i64 = 0
2372 var cut: i64 = 0 - 1
2373 let vi1: i64 = cw_nameidx(snm, "_new" as *u8)
2374 if vi1 >= 0 { isspr = 1; cut = vi1 }
2375 if isspr == 0 { let vi2: i64 = cw_nameidx(snm, "_v2" as *u8); if vi2 >= 0 { isspr = 1; cut = vi2 } }
2376 if isspr == 0 { let vi3: i64 = cw_nameidx(snm, "_v3" as *u8); if vi3 >= 0 { isspr = 1; cut = vi3 } }
2377 // A versioned NAME is only a promote hazard when the BASE binary is
2378 // deployed too -- otherwise the version is the organ's identity.
2379 if isspr == 1 { if cw_has_base_sibling(earena, eoff, nelf, cut, snm) == 0 { isspr = 0 } }
2380 if isspr == 1 {
2381 nsp = nsp + 1
2382 if nsp <= CW_SPRAWLMAX { cw_apps(page, pos, cap, "<span class='pill'>" as *u8); cw_appe(page, pos, cap, snm, gl_len(snm)); cw_apps(page, pos, cap, "</span>" as *u8) }
2383 }
2384 sp0 = sp0 + 1
2385 }
2386 if nsp == 0 { cw_apps(page, pos, cap, "<span class='muted'>none</span>" as *u8) }
2387 if nsp > CW_SPRAWLMAX { cw_apps(page, pos, cap, " <span class='muted'>+more</span>" as *u8) }
2388 // CONTRACT ATTRIBUTE, not prose: the flagged COUNT, so a tooth can pin
2389 // an exact number. Absence-of-a-name is a broken discriminator here --
2390 // the dark-capability list on this same page prints every elf basename,
2391 // so "solo_v2.elf is not on the page" can never be true. Same law the
2392 // taste-bound gauge learned: pin exact counts or data attributes.
2393 cw_apps(page, pos, cap, " <span class='cnt' data-sprawl='" as *u8)
2394 cw_appn(page, pos, cap, nsp)
2395 cw_apps(page, pos, cap, "'>" as *u8)
2396 cw_appn(page, pos, cap, nsp)
2397 cw_apps(page, pos, cap, " flagged (base binary also deployed)</span>" as *u8)
2398 cw_apps(page, pos, cap, "</p>\n" as *u8)
2399 if nsp > 1 {
2400 cw_fnd(fbuf, fpos, 5, "nishihost" as *u8,
2401 "Versioned deployed binaries (_new/_v<n> names) exist in the host dir = stale-promote hazard: a restart could pick the wrong one. https://nishifamily.com/code/exposure -- identify the live one, archive the rest (rule 13), adopt the .prev convention." as *u8)
2402 }
2403 }
2404
2405 // the local-only tail: tools in the tree with no deployed elf at all
2406 var nlocal: i64 = 0
2407 var t0: i64 = 0
2408 while t0 < nsorted {
2409 let tv: i64 = sidx[t0]
2410 if kindv[tv] == CW_K_TOOL {
2411 let tnm: *u8 = ((g.arena as i64) + g.node_off[tv]) as *u8
2412 let tsl: i64 = cw_stemlen(tnm)
2413 var dep: i64 = 0
2414 var e2: i64 = 0
2415 while e2 < nelf {
2416 let dnm2: *u8 = ((earena as i64) + eoff[e2]) as *u8
2417 if gl_len(dnm2) == tsl + 4 {
2418 var eq2: i64 = 1
2419 var c4: i64 = 0
2420 while c4 < tsl { if dnm2[c4] != tnm[c4] { eq2 = 0; c4 = tsl } else { c4 = c4 + 1 } }
2421 if eq2 == 1 { dep = 1 }
2422 }
2423 e2 = e2 + 1
2424 }
2425 if dep == 0 { nlocal = nlocal + 1 }
2426 }
2427 t0 = t0 + 1
2428 }
2429 cw_apps(page, pos, cap, "<p class='muted'>tools in the tree with no deployed elf here: " as *u8)
2430 cw_appn(page, pos, cap, nlocal)
2431 cw_apps(page, pos, cap, " -- the long tail; deploy+expose is a per-capability decision, and this number is its honest size.</p>\n" as *u8)
2432
2433 // ---- SHIPPED DRIFT: deployed binary OLDER than its own source ----
2434 // The dominant sev-9 class made visible. For every deployed <stem>.elf whose
2435 // module we know, compare mtimes: elf older than source => a source change
2436 // that was never shipped. UNDER-reports by design (module source only, not
2437 // its import closure) -- it can miss drift, never invent it.
2438 cw_apps(page, pos, cap, "<h2>shipped drift <span class='cnt' data-drift='" as *u8)
2439 let dbuf2: *u8 = sys_mmap(CW_MAGIC_4096)
2440 let dpos2: *i64 = sys_mmap(32) as *i64
2441 dpos2[0] = 0
2442 dpos2[1] = 0
2443 var ndrift: i64 = 0
2444 var dchk: i64 = 0
2445 // closure scratch: epoch stamps + explicit stack, same pattern as the
2446 // impact section (no per-node memset; one allocation for the whole sweep)
2447 let dstamp: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2448 let dstack: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2449 var dz: i64 = 0
2450 while dz < CW_MAXNODE { dstamp[dz] = 0; dz = dz + 1 }
2451 var depoch: i64 = 0
2452 var d0: i64 = 0
2453 while d0 < nsorted {
2454 let dv: i64 = sidx[d0]
2455 let dnm3: *u8 = ((g.arena as i64) + g.node_off[dv]) as *u8
2456 let dsl: i64 = cw_stemlen(dnm3)
2457 var e3: i64 = 0
2458 while e3 < nelf {
2459 let enm3: *u8 = ((earena as i64) + eoff[e3]) as *u8
2460 if gl_len(enm3) == dsl + 4 {
2461 var eq3: i64 = 1
2462 var c5: i64 = 0
2463 while c5 < dsl { if enm3[c5] != dnm3[c5] { eq3 = 0; c5 = dsl } else { c5 = c5 + 1 } }
2464 if eq3 == 1 {
2465 let ep: *u8 = sys_mmap(CW_MAGIC_4096)
2466 var eo: i64 = 0
2467 var c7: i64 = 0
2468 while elfdir[c7] != (0 as u8) { ep[eo] = elfdir[c7]; eo = eo + 1; c7 = c7 + 1 }
2469 ep[eo] = 47 as u8
2470 eo = eo + 1
2471 var c6: i64 = 0
2472 while c6 < gl_len(enm3) { ep[eo] = enm3[c6]; eo = eo + 1; c6 = c6 + 1 }
2473 ep[eo] = 0 as u8
2474 let emt: i64 = cw_mtime(ep)
2475 depoch = depoch + 1
2476 let smt: i64 = cw_reach_maxmt(g, dv, dstamp, depoch, dstack, srcmt)
2477 if emt > 0 { if smt > 0 {
2478 dchk = dchk + 1
2479 if emt < smt {
2480 ndrift = ndrift + 1
2481 if ndrift <= CW_CATSHOW {
2482 cw_apps(dbuf2, dpos2, CW_MAGIC_4096, "<span class='pill'>" as *u8)
2483 cw_appe(dbuf2, dpos2, CW_MAGIC_4096, dnm3, dsl)
2484 cw_apps(dbuf2, dpos2, CW_MAGIC_4096, "</span>" as *u8)
2485 }
2486 }
2487 } }
2488 e3 = nelf
2489 }
2490 }
2491 e3 = e3 + 1
2492 }
2493 d0 = d0 + 1
2494 }
2495 cw_appn(page, pos, cap, ndrift)
2496 cw_apps(page, pos, cap, "'>" as *u8)
2497 cw_appn(page, pos, cap, ndrift)
2498 cw_apps(page, pos, cap, " of " as *u8)
2499 cw_appn(page, pos, cap, dchk)
2500 cw_apps(page, pos, cap, " comparable · goal ZERO</span></h2>\n<p class='muted'>a deployed binary whose mtime is OLDER than its own source: <b>a fix that lives only in the tree is not shipped</b>. Whole sev-9 scopes are named for this class (<code>eaten-but-fix-not-in-any-tree</code>, <code>breaker-exists-in-source-NOT-in-running-supervisor</code>), and it was rediscovered five separate times by hand on 2026-07-30. compares the elf against the NEWEST source anywhere in the module's transitive IMPORT CLOSURE, not merely its own file: a binary is equally stale when a shared dependency changed and nobody rebuilt it, which is the common case since fixes usually land in a lib — and an mtime delta is a HYPOTHESIS about shipped state, so confirm with a reproduction before redeploying anything.</p>\n<p>" as *u8)
2501 if ndrift == 0 { cw_apps(page, pos, cap, "<span class='muted'>none detected</span>" as *u8) } else { cw_app(page, pos, cap, dbuf2, dpos2[0]) }
2502 if ndrift > CW_CATSHOW { cw_apps(page, pos, cap, " <span class='muted'>+" as *u8); cw_appn(page, pos, cap, ndrift - CW_CATSHOW); cw_apps(page, pos, cap, " more</span>" as *u8) }
2503 cw_apps(page, pos, cap, "</p>\n" as *u8)
2504 if ndrift > 0 {
2505 cw_fnd(fbuf, fpos, 6, "shipped-drift" as *u8, "Deployed binaries are OLDER than their own source: a fix that lives only in the tree is not shipped, and nothing else in the ecosystem answers that question -- it was rediscovered five separate times by hand in one day. Rebuild and redeploy each, or explain the divergence. Confirm with a REPRODUCTION first: an mtime delta is a hypothesis about shipped state, not proof. Standing gauge: https://nishifamily.com/code/exposure" as *u8)
2506 }
2507
2508 darkbox[0] = ndark
2509 return nexp
2510}
2511
2512// ---------- retrieval: ask the wiki, with citations ----------
2513//
2514// Exact-token retrieval over askindex.tsv (DECLARED: this is lexical, not
2515// semantic -- a question sharing no token with the corpus returns NO-MATCH
2516// rather than the least-bad guess, because a confident wrong answer is worse
2517// than an honest miss). It is the GROUNDING half of chat: every hit carries
2518// its citation, so an answer can be checked without trusting the answerer.
2519
2520// TSV-safe append: tabs/newlines/control bytes become spaces so source text
2521// can never break the record format.
2522func cw_apptsv(buf: *u8, pos: *i64, cap: i64, s: *u8, n: i64) -> i64 {
2523 var i: i64 = 0
2524 while i < n {
2525 var c: i64 = s[i] as i64
2526 if c < 32 { c = 32 }
2527 if pos[0] + 1 >= cap - CW_PAGERESERVE { pos[1] = 1 } else { buf[pos[0]] = c as u8; pos[0] = pos[0] + 1 }
2528 i = i + 1
2529 }
2530 return 0
2531}
2532
2533func cw_lc(c: i64) -> i64 {
2534 if c >= 65 { if c <= 90 { return c + 32 } }
2535 return c
2536}
2537
2538// Case-insensitive match at a LEFT WORD BOUNDARY over hay[hs..he).
2539//
2540// ★The boundary is the whole point. Unbounded substring matching made a
2541// question about "which organ ..." rank nx_chem_m-ORGAN-_test first: the query
2542// word appeared inside an unrelated word. Requiring the match to begin at the
2543// start of a word (or after a non-identifier byte such as _ / . space) kills
2544// that class entirely, while still letting "func" match nx_dup_FUNC and "gate"
2545// match GATElib -- prefix-of-a-segment is real signal in an identifier corpus,
2546// suffix-of-a-word is noise.
2547func cw_hastok(hay: *u8, hs: i64, he: i64, tok: *u8, tl: i64) -> i64 {
2548 if tl <= 0 { return 0 }
2549 var i: i64 = hs
2550 while i + tl <= he {
2551 var lb: i64 = 1
2552 if i > hs { if gl_is_ident(cw_lc(hay[i - 1] as i64)) == 1 { lb = 0 } }
2553 if lb == 1 {
2554 var m: i64 = 0
2555 var ok: i64 = 1
2556 while m < tl { if cw_lc(hay[i + m] as i64) != (tok[m] as i64) { ok = 0; m = tl } else { m = m + 1 } }
2557 if ok == 1 { return 1 }
2558 }
2559 i = i + 1
2560 }
2561 return 0
2562}
2563
2564// field end: index of the next TAB (or line end)
2565func cw_fld(buf: *u8, s: i64, e: i64) -> i64 {
2566 var i: i64 = s
2567 var r: i64 = e
2568 var go: i64 = 1
2569 while go == 1 {
2570 if i >= e { go = 0 } else {
2571 if buf[i] == (9 as u8) { r = i; go = 0 } else { i = i + 1 }
2572 }
2573 }
2574 return r
2575}
2576
2577// tokenize a question (argv words) into lowercase tokens >= CW_ASKMINTOK.
2578// Shared by every ask front-end so one grammar cannot drift into two.
2579func cw_qtok(words: *i64, wn: i64, first: i64, toks: *u8, toff: *i64, tlen: *i64) -> i64 {
2580 var nt: i64 = 0
2581 var u: i64 = 0
2582 var w: i64 = first
2583 while w < wn {
2584 let s: *u8 = words[w] as *u8
2585 var i: i64 = 0
2586 var go: i64 = 1
2587 while go == 1 {
2588 let c: i64 = s[i] as i64
2589 if c == 0 { go = 0 } else {
2590 if gl_is_ident(c) == 1 {
2591 var e: i64 = i
2592 var g2: i64 = 1
2593 while g2 == 1 { if gl_is_ident(s[e] as i64) == 1 { e = e + 1 } else { g2 = 0 } }
2594 let l: i64 = e - i
2595 if l >= CW_ASKMINTOK { if nt < CW_ASKQTOK { if u + l + 1 < CW_MAGIC_1024 {
2596 toff[nt] = u
2597 tlen[nt] = l
2598 var k: i64 = 0
2599 while k < l { toks[u + k] = cw_lc(s[i + k] as i64) as u8; k = k + 1 }
2600 u = u + l
2601 nt = nt + 1
2602 } } }
2603 i = e
2604 } else { i = i + 1 }
2605 }
2606 }
2607 w = w + 1
2608 }
2609 return nt
2610}
2611
2612// ask: score every record against the question tokens, print the top hits
2613// WITH citations. Returns the number of hits shown (0 = honest NO-MATCH).
2614func cw_ask(idxpath: *u8, qtoks: *u8, qoff: *i64, qlen: *i64, nq: i64, urlbase: *u8, topname: *u8) -> i64 {
2615 let buf: *u8 = sys_mmap(CW_ASKCAP)
2616 let n: i64 = gl_read(idxpath, buf, CW_ASKCAP)
2617 let out: *u8 = sys_mmap(CW_MAGIC_65536)
2618 let opos: *i64 = sys_mmap(32) as *i64
2619 opos[0] = 0
2620 opos[1] = 0
2621 if n <= 0 {
2622 cw_apps(out, opos, CW_MAGIC_65536, "ASK-INDEX-MISSING: " as *u8)
2623 cw_apps(out, opos, CW_MAGIC_65536, idxpath)
2624 cw_apps(out, opos, CW_MAGIC_65536, " (run the generator first)\n" as *u8)
2625 sys_write(1, out, opos[0])
2626 return 0 - 1
2627 }
2628 let bsc: *i64 = sys_mmap((CW_ASKTOP + 2) * 8) as *i64
2629 let bst: *i64 = sys_mmap((CW_ASKTOP + 2) * 8) as *i64
2630 let ben: *i64 = sys_mmap((CW_ASKTOP + 2) * 8) as *i64
2631 var nb: i64 = 0
2632 var scanned: i64 = 0
2633 // ★TIE ACCOUNTING (debt seq1328, measured 2026-07-30). A WIDE TIE AT A LOW SCORE IS NOT A RANKING.
2634 // The top-K below keeps the FIRST K records that clear score>0 and only displaces on STRICTLY
2635 // greater, so thousands of equally-scoring records render as ten alphabetical 'hits' and the tie is
2636 // invisible. A caller then cannot tell 'no such capability exists' from 'here are your top hits' --
2637 // on the DISCOVERY path, which is the measured bottleneck. Count the tie so it can be declared.
2638 var best: i64 = 0
2639 var nbest: i64 = 0
2640 var nmatch: i64 = 0
2641
2642 // ---- pass A: document frequency per token -> rarity weight ----
2643 let qdf: *i64 = sys_mmap(CW_ASKQTOK * 8) as *i64
2644 let qw: *i64 = sys_mmap(CW_ASKQTOK * 8) as *i64
2645 var z0: i64 = 0
2646 while z0 < nq { qdf[z0] = 0; z0 = z0 + 1 }
2647 var nrec: i64 = 0
2648 var pls: i64 = 0
2649 while pls < n {
2650 var ple: i64 = pls
2651 var pg: i64 = 1
2652 while pg == 1 { if ple >= n { pg = 0 } else { if buf[ple] == (10 as u8) { pg = 0 } else { ple = ple + 1 } } }
2653 if ple > pls + 2 {
2654 nrec = nrec + 1
2655 var pk: i64 = 0
2656 while pk < nq {
2657 let ptk: *u8 = ((qtoks as i64) + qoff[pk]) as *u8
2658 if cw_hastok(buf, pls, ple, ptk, qlen[pk]) == 1 { qdf[pk] = qdf[pk] + 1 }
2659 pk = pk + 1
2660 }
2661 }
2662 pls = ple + 1
2663 }
2664 var z1: i64 = 0
2665 while z1 < nq {
2666 var permil: i64 = 1000
2667 if nrec > 0 { permil = (qdf[z1] * 1000) / nrec }
2668 var w: i64 = CW_IDF_W5
2669 if permil <= CW_IDF_P4 { w = CW_IDF_W4 }
2670 if permil <= CW_IDF_P3 { w = CW_IDF_W3 }
2671 if permil <= CW_IDF_P2 { w = CW_IDF_W2 }
2672 if permil <= CW_IDF_P1 { w = CW_IDF_W1 }
2673 if qdf[z1] == 0 { w = 0 }
2674 qw[z1] = w
2675 z1 = z1 + 1
2676 }
2677
2678 // ---- pass B: score ----
2679 var ls: i64 = 0
2680 while ls < n {
2681 var le: i64 = ls
2682 var g0: i64 = 1
2683 while g0 == 1 { if le >= n { g0 = 0 } else { if buf[le] == (10 as u8) { g0 = 0 } else { le = le + 1 } } }
2684 if le > ls + 2 {
2685 scanned = scanned + 1
2686 let isM: i64 = buf[ls] as i64
2687 // field 1 = name (both record kinds), field 2..: kind-specific
2688 let f1s: i64 = ls + 2
2689 let f1e: i64 = cw_fld(buf, f1s, le)
2690 var score: i64 = 0
2691 var k: i64 = 0
2692 while k < nq {
2693 let tk: *u8 = ((qtoks as i64) + qoff[k]) as *u8
2694 if cw_hastok(buf, f1s, f1e, tk, qlen[k]) == 1 { score = score + qw[k] * CW_ASKNAMEX }
2695 // module records: the description field is the last one
2696 if isM == 77 {
2697 // fields: name | dir | loc | funcs | desc -- walk to the END
2698 // of `funcs` (3 hops), then +1 is the START of desc. Walking
2699 // 4 hops lands on the line end and silently scores nothing.
2700 var ds: i64 = f1e
2701 var fc: i64 = 0
2702 while fc < 3 { ds = cw_fld(buf, ds + 1, le); fc = fc + 1 }
2703 ds = ds + 1
2704 // desc = field 5, searchable header doc = field 6 (to EOL).
2705 // A token counts ONCE even though desc is a prefix of doc.
2706 if ds < le { if cw_hastok(buf, ds, le, tk, qlen[k]) == 1 { score = score + qw[k] } }
2707 }
2708 // capability records: the title+grammar tail (fields 2..EOL)
2709 // carries the question's words ("verify a live page" names no
2710 // tool), so it scores like a module description does.
2711 if isM == 84 {
2712 let cts: i64 = f1e + 1
2713 if cts < le { if cw_hastok(buf, cts, le, tk, qlen[k]) == 1 { score = score + qw[k] } }
2714 }
2715 k = k + 1
2716 }
2717 if score > 0 {
2718 nmatch = nmatch + 1
2719 if score > best { best = score; nbest = 1 } else { if score == best { nbest = nbest + 1 } }
2720 var ins: i64 = 0 - 1
2721 if nb < CW_ASKTOP { ins = nb; bsc[nb] = score; bst[nb] = ls; ben[nb] = le; nb = nb + 1 }
2722 if ins < 0 { if score > bsc[CW_ASKTOP - 1] { ins = CW_ASKTOP - 1; bsc[ins] = score; bst[ins] = ls; ben[ins] = le } }
2723 var q2: i64 = ins
2724 while q2 > 0 {
2725 if bsc[q2 - 1] < bsc[q2] {
2726 let t1: i64 = bsc[q2-1]; bsc[q2-1] = bsc[q2]; bsc[q2] = t1
2727 let t2: i64 = bst[q2-1]; bst[q2-1] = bst[q2]; bst[q2] = t2
2728 let t3: i64 = ben[q2-1]; ben[q2-1] = ben[q2]; ben[q2] = t3
2729 q2 = q2 - 1
2730 } else { q2 = 0 }
2731 }
2732 }
2733 }
2734 ls = le + 1
2735 }
2736 cw_apps(out, opos, CW_MAGIC_65536, "=== ASK THE CODE WIKI (lexical retrieval; every hit cited) ===\nrecords scanned: " as *u8)
2737 cw_appn(out, opos, CW_MAGIC_65536, scanned)
2738 cw_apps(out, opos, CW_MAGIC_65536, " hits shown: " as *u8)
2739 cw_appn(out, opos, CW_MAGIC_65536, nb)
2740 cw_apps(out, opos, CW_MAGIC_65536, " (cap " as *u8)
2741 cw_appn(out, opos, CW_MAGIC_65536, CW_ASKTOP)
2742 cw_apps(out, opos, CW_MAGIC_65536, " DECLARED)\n" as *u8)
2743 // ★WHY THESE HITS (seq1328): per-token document frequency, so a caller can SEE whether the question
2744 // carried any discriminating word. A token in 0 records contributes nothing; a token in nearly every
2745 // record contributes nothing USEFUL. Hiding this is what let a uniform tie read as a ranking.
2746 cw_apps(out, opos, CW_MAGIC_65536, "token signal (df of " as *u8)
2747 cw_appn(out, opos, CW_MAGIC_65536, nrec)
2748 cw_apps(out, opos, CW_MAGIC_65536, " records):" as *u8)
2749 var zt: i64 = 0
2750 while zt < nq {
2751 cw_apps(out, opos, CW_MAGIC_65536, " " as *u8)
2752 cw_app(out, opos, CW_MAGIC_65536, ((qtoks as i64) + qoff[zt]) as *u8, qlen[zt])
2753 cw_apps(out, opos, CW_MAGIC_65536, "=" as *u8)
2754 cw_appn(out, opos, CW_MAGIC_65536, qdf[zt])
2755 zt = zt + 1
2756 }
2757 cw_apps(out, opos, CW_MAGIC_65536, "\nmatched: " as *u8)
2758 cw_appn(out, opos, CW_MAGIC_65536, nmatch)
2759 cw_apps(out, opos, CW_MAGIC_65536, " top score: " as *u8)
2760 cw_appn(out, opos, CW_MAGIC_65536, best)
2761 cw_apps(out, opos, CW_MAGIC_65536, " records tied at it: " as *u8)
2762 cw_appn(out, opos, CW_MAGIC_65536, nbest)
2763 cw_apps(out, opos, CW_MAGIC_65536, "\n" as *u8)
2764 // ★★THE REFUSAL. If more records tie at the top score than can be shown, the order below is INDEX
2765 // ORDER and nothing more. Say so in the caller's own terms rather than letting ten arbitrary rows
2766 // impersonate an answer. This LOOSENS NOTHING -- the hits are still printed, they are just no longer
2767 // allowed to pretend they were ranked.
2768 if nbest > CW_ASKTOP {
2769 cw_apps(out, opos, CW_MAGIC_65536, "AMBIGUOUS: the top score is a TIE WIDER THAN THE DISPLAY CAP, so the rows below are in INDEX ORDER, NOT RELEVANCE ORDER. The question carried no discriminating token (see df above). Treat this as NO USABLE ANSWER and re-ask with a rarer term.\n" as *u8)
2770 }
2771 if nb == 0 {
2772 cw_apps(out, opos, CW_MAGIC_65536, "NO-MATCH: no record shares a token with the question. Not guessing.\n" as *u8)
2773 sys_write(1, out, opos[0])
2774 return 0
2775 }
2776 var r: i64 = 0
2777 while r < nb {
2778 let rs: i64 = bst[r]
2779 let re: i64 = ben[r]
2780 let kind: i64 = buf[rs] as i64
2781 let f1s: i64 = rs + 2
2782 let f1e: i64 = cw_fld(buf, f1s, re)
2783 // hand the top hit's name back to the caller (gates rank-check on it)
2784 if r == 0 { if (topname as i64) != 0 {
2785 var tc: i64 = 0
2786 while tc < f1e - f1s { if tc < 90 { topname[tc] = buf[f1s + tc]; tc = tc + 1 } else { tc = f1e - f1s } }
2787 var tl2: i64 = f1e - f1s
2788 if tl2 > 90 { tl2 = 90 }
2789 topname[tl2] = 0 as u8
2790 } }
2791 cw_apps(out, opos, CW_MAGIC_65536, "\n[" as *u8)
2792 cw_appn(out, opos, CW_MAGIC_65536, r + 1)
2793 cw_apps(out, opos, CW_MAGIC_65536, "] score=" as *u8)
2794 cw_appn(out, opos, CW_MAGIC_65536, bsc[r])
2795 if kind == 77 {
2796 cw_apps(out, opos, CW_MAGIC_65536, " MODULE " as *u8)
2797 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2798 // dir, loc, funcs, desc
2799 let d2s: i64 = f1e + 1
2800 let d2e: i64 = cw_fld(buf, d2s, re)
2801 let l3s: i64 = d2e + 1
2802 let l3e: i64 = cw_fld(buf, l3s, re)
2803 let n4s: i64 = l3e + 1
2804 let n4e: i64 = cw_fld(buf, n4s, re)
2805 cw_apps(out, opos, CW_MAGIC_65536, " (" as *u8)
2806 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + l3s) as *u8, l3e - l3s)
2807 cw_apps(out, opos, CW_MAGIC_65536, " lines, " as *u8)
2808 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + n4s) as *u8, n4e - n4s)
2809 cw_apps(out, opos, CW_MAGIC_65536, " funcs)\n cite: " as *u8)
2810 cw_apps(out, opos, CW_MAGIC_65536, urlbase)
2811 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2812 cw_apps(out, opos, CW_MAGIC_65536, "\n " as *u8)
2813 // print the DESC field only (field 5) -- field 6 is the searchable
2814 // header text and is for scoring, not for reading.
2815 let d5s: i64 = n4e + 1
2816 let d5e: i64 = cw_fld(buf, d5s, re)
2817 var dshow: i64 = d5e - d5s
2818 if dshow > CW_ASKDESCMAX { dshow = CW_ASKDESCMAX }
2819 if dshow > 0 { cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + d5s) as *u8, dshow) }
2820 cw_apps(out, opos, CW_MAGIC_65536, "\n" as *u8)
2821 } else { if kind == 84 {
2822 // capability record: label it as one, cite the catalog anchor,
2823 // and show title + argv grammar (rung 23).
2824 cw_apps(out, opos, CW_MAGIC_65536, " CAPABILITY " as *u8)
2825 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2826 cw_apps(out, opos, CW_MAGIC_65536, "\n cite: " as *u8)
2827 cw_apps(out, opos, CW_MAGIC_65536, urlbase)
2828 cw_apps(out, opos, CW_MAGIC_65536, "tools#t_" as *u8)
2829 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2830 cw_apps(out, opos, CW_MAGIC_65536, "\n " as *u8)
2831 let c2s: i64 = f1e + 1
2832 let c2e: i64 = cw_fld(buf, c2s, re)
2833 var cshow: i64 = c2e - c2s
2834 if cshow > CW_ASKDESCMAX { cshow = CW_ASKDESCMAX }
2835 if cshow > 0 { cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + c2s) as *u8, cshow) }
2836 let c3s: i64 = c2e + 1
2837 if c3s < re {
2838 cw_apps(out, opos, CW_MAGIC_65536, " -- argv: " as *u8)
2839 let c3e: i64 = cw_fld(buf, c3s, re)
2840 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + c3s) as *u8, c3e - c3s)
2841 }
2842 cw_apps(out, opos, CW_MAGIC_65536, "\n" as *u8)
2843 } else {
2844 cw_apps(out, opos, CW_MAGIC_65536, " FUNCTION " as *u8)
2845 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2846 let m2s: i64 = f1e + 1
2847 let m2e: i64 = cw_fld(buf, m2s, re)
2848 cw_apps(out, opos, CW_MAGIC_65536, " in " as *u8)
2849 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + m2s) as *u8, m2e - m2s)
2850 cw_apps(out, opos, CW_MAGIC_65536, " line " as *u8)
2851 if m2e + 1 < re { cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + m2e + 1) as *u8, re - m2e - 1) }
2852 cw_apps(out, opos, CW_MAGIC_65536, "\n cite: " as *u8)
2853 cw_apps(out, opos, CW_MAGIC_65536, urlbase)
2854 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + m2s) as *u8, m2e - m2s)
2855 cw_apps(out, opos, CW_MAGIC_65536, "#fn_" as *u8)
2856 cw_app(out, opos, CW_MAGIC_65536, ((buf as i64) + f1s) as *u8, f1e - f1s)
2857 cw_apps(out, opos, CW_MAGIC_65536, "\n" as *u8)
2858 } }
2859 r = r + 1
2860 }
2861 sys_write(1, out, opos[0])
2862 return nb
2863}
2864
2865// ---------- the generator ----------
2866//
2867// st[] result slots (DECLARED):
2868// 0 files 1 nodes 2 edges 3 funcs 4 structs 5 consts
2869// 6 unresolved-import targets 7 pages written 8 tree stamp
2870// 9 cycle(0=DAG) 10 dirs 11 truncated pages
2871// noext: 0 = hrefs carry .html (local file browsing), 1 = extensionless hrefs
2872// (the edge's canonical form). Disk filenames always carry .html.
2873// rc: 0 ok · 1 io-fail · 2 EMPTY ROOT (refused loudly -- a generator pointed
2874// at a wrong root must never report a green nothing; debt seq1107).
2875// allowpath/schemapath: the capability-join inputs (allowlist x schema
2876// registry). PARAMETERS, not CWD literals, so the gate's fixture runs are
2877// deterministic -- a prod conf leaking into a fixture corpus warps df
2878// ranking and the on-disk record counts (the rung-23 RED taught this).
2879func cw_generate2(root: *u8, outdir: *u8, noext: i64, st: *i64, allowpath: *u8, schemapath: *u8, atlaspath: *u8) -> i64 {
2880 sys_mkdir(outdir, 493)
2881
2882 let g: *EcoGraph = eg_new(CW_MAXNODE, CW_EDGECAP, CW_ARENA, CW_HASH)
2883 let patharena: *u8 = sys_mmap(CW_PATHARENA)
2884 let pathoff: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2885 let used: *i64 = sys_mmap(16)
2886 let wst: *i64 = sys_mmap(16)
2887 used[0] = 0
2888 wst[0] = 0
2889 var z: i64 = 0
2890 while z < CW_MAXNODE { pathoff[z] = 0; z = z + 1 }
2891 let path: *u8 = sys_mmap(CW_MAGIC_4096)
2892 var rn: i64 = 0
2893 while root[rn] != (0 as u8) { path[rn] = root[rn]; rn = rn + 1 }
2894 gl_walk(g, path, rn, 0 as *u8, 0 as *i64, 0 as *i64, wst, 0, patharena, pathoff, used, CW_PATHARENA)
2895 st[0] = wst[0]
2896 if st[0] == 0 { return 2 }
2897
2898 // per-node stores
2899 let fbytes: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2900 let floc: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2901 let catmask: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2902 let langv: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2903 let boundv: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2904 let subclv: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2905 let srcmt: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2906 let fnfn: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2907 let fdir: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2908 let doff: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2909 let dlen: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2910 let descarena: *u8 = sys_mmap(CW_DESCARENA)
2911 let dused: *i64 = sys_mmap(16)
2912 dused[0] = 0
2913 // searchable header-doc text (retrieval corpus, see CW_SDOCMAX)
2914 let sdocarena: *u8 = sys_mmap(CW_SDOCARENA)
2915 let sdoff: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2916 let sdlen: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2917 let sdused: *i64 = sys_mmap(16)
2918 sdused[0] = 0
2919
2920 // dir table
2921 let dirarena: *u8 = sys_mmap(CW_MAGIC_262144)
2922 let dirnoff: *i64 = sys_mmap(CW_MAXDIRS * 8) as *i64
2923 let dirslug: *i64 = sys_mmap(CW_MAXDIRS * 8) as *i64
2924 let dircnt: *i64 = sys_mmap(CW_MAXDIRS * 8) as *i64
2925 let dau: *i64 = sys_mmap(16)
2926 dau[0] = 0
2927 var ndirs: i64 = 0
2928
2929 let src: *u8 = sys_mmap(GL_FILECAP)
2930 let aoff: *i64 = sys_mmap(CW_MAGIC_1024 * 8) as *i64
2931 let alen: *i64 = sys_mmap(CW_MAGIC_1024 * 8) as *i64
2932 let seen: *i64 = sys_mmap(CW_IMPDEDUP * 8) as *i64
2933 let tbox: *i64 = sys_mmap(16)
2934
2935 var stamp: i64 = CW_MAGIC_1469598103934665603
2936 let nfilenodes: i64 = g.node_count
2937
2938 // name-sorted file-node index (see cw_sortnodes header)
2939 let sidx: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2940 let stmp: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
2941 var nsorted: i64 = 0
2942 var sv: i64 = 0
2943 while sv < nfilenodes {
2944 if pathoff[sv] > 0 { sidx[nsorted] = sv; nsorted = nsorted + 1 }
2945 sv = sv + 1
2946 }
2947 cw_sortnodes(g, sidx, stmp, nsorted)
2948
2949 // ===== pass 1: edges + stats + desc + dir + stamp =====
2950 var s1i: i64 = 0
2951 while s1i < nsorted {
2952 let v: i64 = sidx[s1i]
2953 if pathoff[v] > 0 {
2954 let fp: *u8 = ((patharena as i64) + pathoff[v] - 1) as *u8
2955 // stamp: path bytes then size
2956 var pi: i64 = 0
2957 while fp[pi] != (0 as u8) { stamp = (stamp ^ (fp[pi] as i64)) * CW_MAGIC_1099511628211; pi = pi + 1 }
2958 let n: i64 = gl_read(fp, src, GL_FILECAP)
2959 stamp = (stamp ^ n) * CW_MAGIC_1099511628211
2960 fbytes[v] = n
2961 catmask[v] = cw_catscan(src, n)
2962 langv[v] = cw_langscan(src, n)
2963 boundv[v] = cw_boundscan(src, n)
2964 subclv[v] = cw_substrclass(src, n)
2965 srcmt[v] = cw_mtime(fp)
2966 var nl: i64 = 0
2967 var q: i64 = 0
2968 while q < n { if src[q] == (10 as u8) { nl = nl + 1 } q = q + 1 }
2969 floc[v] = nl
2970
2971 // imports -> basename-interned edges, deduped per file
2972 let ic: i64 = nis_scan(src, n, aoff, alen, CW_MAGIC_1024)
2973 var nseen: i64 = 0
2974 var k: i64 = 0
2975 while k < ic {
2976 let bs: i64 = cw_impbase(src, aoff[k], alen[k], tbox)
2977 let bl: i64 = tbox[0]
2978 let dep: i64 = eg_intern(g, ((src as i64) + bs) as *u8, bl)
2979 if dep >= 0 { if dep != v {
2980 var dup: i64 = 0
2981 var s2: i64 = 0
2982 while s2 < nseen { if seen[s2] == dep { dup = 1 } s2 = s2 + 1 }
2983 if dup == 0 {
2984 if nseen < CW_IMPDEDUP { seen[nseen] = dep; nseen = nseen + 1 }
2985 eg_add_edge(g, v, dep)
2986 }
2987 } }
2988 k = k + 1
2989 }
2990
2991 // desc: first line, text after "-- " (or after "// ")
2992 var e1: i64 = 0
2993 var g1: i64 = 1
2994 while g1 == 1 { if e1 >= n { g1 = 0 } else { if src[e1] == (10 as u8) { g1 = 0 } else { e1 = e1 + 1 } } }
2995 doff[v] = 0
2996 dlen[v] = 0
2997 if e1 > 2 { if src[0] == (47 as u8) { if src[1] == (47 as u8) {
2998 var ds: i64 = 0 - 1
2999 var p3: i64 = 2
3000 while p3 + 3 <= e1 {
3001 if ds < 0 { if src[p3] == (45 as u8) { if src[p3+1] == (45 as u8) { if src[p3+2] == (32 as u8) { ds = p3 + 3 } } } }
3002 p3 = p3 + 1
3003 }
3004 if ds < 0 { if e1 > 3 { ds = 3 } }
3005 if ds > 0 { if ds < e1 {
3006 var dl: i64 = e1 - ds
3007 if dl > CW_DESCMAX { dl = CW_DESCMAX }
3008 if dused[0] + dl + 1 < CW_DESCARENA {
3009 var c2: i64 = 0
3010 while c2 < dl { descarena[dused[0] + c2] = src[ds + c2]; c2 = c2 + 1 }
3011 doff[v] = dused[0]
3012 dlen[v] = dl
3013 dused[0] = dused[0] + dl
3014 }
3015 } }
3016 } } }
3017
3018 // searchable header text: the LEADING comment block, comment
3019 // markers stripped, control bytes flattened, capped + declared.
3020 sdoff[v] = sdused[0]
3021 sdlen[v] = 0
3022 var hls: i64 = 0
3023 var hgo: i64 = 1
3024 var hw: i64 = 0
3025 while hgo == 1 {
3026 if hls >= n { hgo = 0 } else {
3027 var hle: i64 = hls
3028 var hg2: i64 = 1
3029 while hg2 == 1 { if hle >= n { hg2 = 0 } else { if src[hle] == (10 as u8) { hg2 = 0 } else { hle = hle + 1 } } }
3030 var iscm: i64 = 0
3031 if hls + 1 < hle { if src[hls] == (47 as u8) { if src[hls + 1] == (47 as u8) { iscm = 1 } } }
3032 if iscm == 0 { hgo = 0 } else {
3033 var hc: i64 = hls + 2
3034 while hc < hle {
3035 if hw < CW_SDOCMAX { if sdused[0] + hw + 2 < CW_SDOCARENA {
3036 var ch: i64 = src[hc] as i64
3037 if ch < 32 { ch = 32 }
3038 if ch == 9 { ch = 32 }
3039 sdocarena[sdused[0] + hw] = ch as u8
3040 hw = hw + 1
3041 } }
3042 hc = hc + 1
3043 }
3044 if hw < CW_SDOCMAX { if sdused[0] + hw + 2 < CW_SDOCARENA { sdocarena[sdused[0] + hw] = 32 as u8; hw = hw + 1 } }
3045 hls = hle + 1
3046 if hw >= CW_SDOCMAX { hgo = 0 }
3047 }
3048 }
3049 }
3050 sdlen[v] = hw
3051 sdused[0] = sdused[0] + hw
3052
3053 // dir: relative dir of fp under root
3054 let bo: i64 = cw_base(fp)
3055 var rs: i64 = rn + 1
3056 if bo < rs { rs = bo }
3057 var dn2: i64 = bo - rs - 1
3058 if dn2 < 0 { dn2 = 0 }
3059 // find-or-add in dir table (linear; dirs are few)
3060 var di: i64 = 0 - 1
3061 var t2: i64 = 0
3062 while t2 < ndirs {
3063 if di < 0 {
3064 let off2: i64 = dirnoff[t2]
3065 var eq: i64 = 1
3066 var c3: i64 = 0
3067 while c3 < dn2 { if dirarena[off2 + c3] != fp[rs + c3] { eq = 0; c3 = dn2 } else { c3 = c3 + 1 } }
3068 if eq == 1 { if dirarena[off2 + dn2] == (0 as u8) { di = t2 } }
3069 }
3070 t2 = t2 + 1
3071 }
3072 if di < 0 {
3073 if ndirs < CW_MAXDIRS {
3074 di = ndirs
3075 dirnoff[di] = dau[0]
3076 var c4: i64 = 0
3077 while c4 < dn2 { dirarena[dau[0] + c4] = fp[rs + c4]; c4 = c4 + 1 }
3078 dirarena[dau[0] + dn2] = 0 as u8
3079 dau[0] = dau[0] + dn2 + 1
3080 // slug: "d_" + dir with '/'->'_' (empty dir -> "d_root")
3081 dirslug[di] = dau[0]
3082 dirarena[dau[0]] = 100 as u8
3083 dirarena[dau[0] + 1] = 95 as u8
3084 var sw: i64 = dau[0] + 2
3085 if dn2 == 0 {
3086 dirarena[sw] = 114 as u8; dirarena[sw+1] = 111 as u8; dirarena[sw+2] = 111 as u8; dirarena[sw+3] = 116 as u8
3087 sw = sw + 4
3088 }
3089 var c5: i64 = 0
3090 while c5 < dn2 {
3091 var ch: i64 = fp[rs + c5] as i64
3092 if ch == 47 { ch = 95 }
3093 if ch == 46 { ch = 95 }
3094 dirarena[sw] = ch as u8
3095 sw = sw + 1
3096 c5 = c5 + 1
3097 }
3098 dirarena[sw] = 0 as u8
3099 dau[0] = sw + 1
3100 dircnt[di] = 0
3101 ndirs = ndirs + 1
3102 } else { di = 0 }
3103 }
3104 fdir[v] = di
3105 dircnt[di] = dircnt[di] + 1
3106 }
3107 s1i = s1i + 1
3108 }
3109
3110 st[1] = g.node_count
3111 st[2] = g.edge_count
3112 st[8] = stamp & 0x7fffffffffffffff
3113 st[10] = ndirs
3114 eg_finalize(g)
3115 st[9] = eg_has_cycle(g)
3116
3117 var unres: i64 = 0
3118 var u0: i64 = 0
3119 while u0 < g.node_count { if pathoff[u0] == 0 { unres = unres + 1 } u0 = u0 + 1 }
3120 st[6] = unres
3121
3122 // transitive impact + DAG depth per file node (epoch-stamped, iterative)
3123 let stampv: *i64 = sys_mmap((g.node_count + 2) * 8) as *i64
3124 let rstack: *i64 = sys_mmap((g.node_count + 2) * 8) as *i64
3125 let tri_out: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3126 let tri_in: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3127 let depthv: *i64 = sys_mmap((g.node_count + 2) * 8) as *i64
3128 var z2a: i64 = 0
3129 while z2a < g.node_count { stampv[z2a] = 0; depthv[z2a] = 0 - 1; z2a = z2a + 1 }
3130 var epoch: i64 = 0
3131 var s1b: i64 = 0
3132 while s1b < nsorted {
3133 let vv: i64 = sidx[s1b]
3134 epoch = epoch + 1
3135 tri_out[vv] = cw_reach_count(g, vv, 0, stampv, epoch, rstack)
3136 epoch = epoch + 1
3137 tri_in[vv] = cw_reach_count(g, vv, 1, stampv, epoch, rstack)
3138 cw_depth_of(g, vv, depthv)
3139 s1b = s1b + 1
3140 }
3141
3142 // function index arenas (search): DECLARED caps CW_MAXFN / CW_FNARENA
3143 let fnarena: *u8 = sys_mmap(CW_FNARENA)
3144 let fnoff: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3145 let fnlen: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3146 let fnmod: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3147 let fnline: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3148 let fnu: *i64 = sys_mmap(16)
3149 fnu[0] = 0
3150 var nfns: i64 = 0
3151
3152 // ===== topic families (derived from name prefixes, threshold DECLARED) =====
3153 let famarena: *u8 = sys_mmap(CW_FAMARENA)
3154 let famoff: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
3155 let famlen: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
3156 let famcnt: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
3157 let modfam: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3158 var famu: i64 = 0
3159 var nfam: i64 = 0
3160 let fkey: *u8 = sys_mmap(32)
3161 var sf0: i64 = 0
3162 while sf0 < nsorted {
3163 let fv: i64 = sidx[sf0]
3164 let fnm2: *u8 = ((g.arena as i64) + g.node_off[fv]) as *u8
3165 let fkl: i64 = cw_famkey(fnm2, fkey)
3166 var fidx: i64 = 0 - 1
3167 var ff0: i64 = 0
3168 while ff0 < nfam {
3169 if fidx < 0 { if famlen[ff0] == fkl {
3170 var feq: i64 = 1
3171 var fc0: i64 = 0
3172 while fc0 < fkl { if famarena[famoff[ff0] + fc0] != fkey[fc0] { feq = 0; fc0 = fkl } else { fc0 = fc0 + 1 } }
3173 if feq == 1 { fidx = ff0 }
3174 } }
3175 ff0 = ff0 + 1
3176 }
3177 if fidx < 0 { if fkl > 0 { if nfam < CW_MAXFAM { if famu + fkl + 1 < CW_FAMARENA {
3178 fidx = nfam
3179 famoff[fidx] = famu
3180 famlen[fidx] = fkl
3181 famcnt[fidx] = 0
3182 var fc1: i64 = 0
3183 while fc1 < fkl { famarena[famu + fc1] = fkey[fc1]; fc1 = fc1 + 1 }
3184 famarena[famu + fkl] = 0 as u8
3185 famu = famu + fkl + 1
3186 nfam = nfam + 1
3187 } } } }
3188 if fidx >= 0 { modfam[fv] = fidx; famcnt[fidx] = famcnt[fidx] + 1 } else { modfam[fv] = 0 - 1 }
3189 sf0 = sf0 + 1
3190 }
3191 // rank families by TOTAL LOC, not member count: a 2000-stub test corpus
3192 // must not outrank a 78-module subsystem (measured curation, no name lists)
3193 let famloc: *i64 = sys_mmap(CW_MAXFAM * 8) as *i64
3194 var fl0: i64 = 0
3195 while fl0 < nfam { famloc[fl0] = 0; fl0 = fl0 + 1 }
3196 var fl1: i64 = 0
3197 while fl1 < nsorted {
3198 let flv: i64 = sidx[fl1]
3199 if modfam[flv] >= 0 { famloc[modfam[flv]] = famloc[modfam[flv]] + floc[flv] }
3200 fl1 = fl1 + 1
3201 }
3202 var nfamE: i64 = 0
3203 var fe0: i64 = 0
3204 while fe0 < nfam { if famcnt[fe0] >= CW_FAMMIN { nfamE = nfamE + 1 } fe0 = fe0 + 1 }
3205 st[15] = nfamE + 1
3206
3207 // ===== the shared sidebar (built once, appended to every page) =====
3208 let sbbuf: *u8 = sys_mmap(CW_MAGIC_65536)
3209 let sbpos: *i64 = sys_mmap(32) as *i64
3210 sbpos[0] = 0
3211 sbpos[1] = 0
3212 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<nav class='sb'><span class='brand'>◆ nishi code wiki</span>\n<a class='it' href='index" as *u8)
3213 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3214 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>Overview</a>\n<a class='it' href='search" as *u8)
3215 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3216 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🔍 Search</a>\n<a class='it' href='ask" as *u8)
3217 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3218 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>💬 Ask (local AI)</a>\n<a class='it' href='lang" as *u8)
3219 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3220 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>📜 Language</a>\n<a class='it' href='tools" as *u8)
3221 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3222 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🔧 Tools (capabilities)</a>\n<a class='it' href='pulse" as *u8)
3223 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3224 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>📈 Pulse (shift gauges)</a>\n<a class='it' href='arch" as *u8)
3225 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3226 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🏗 Architecture</a>\n<a class='it' href='kinds" as *u8)
3227 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3228 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>📋 Inventory</a>\n<a class='it' href='exposure" as *u8)
3229 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3230 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🔌 Exposure (MCP/API)</a>\n<a class='it' href='findings" as *u8)
3231 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3232 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>⚠ Findings (backlog)</a>\n<a class='it' href='debt" as *u8)
3233 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3234 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🔨 Debt taxonomy</a>\n<a class='it' href='families" as *u8)
3235 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3236 // Work families -- GENERATED BY nx_debtcluster, not by this tool. Linked here for exactly the reason
3237 // given for the SOTA rung plan below: a page the nav does not mention is unreachable. It groups the
3238 // debt ledger's rows into families that share an anchor, ranks them by max_sev x open, and publishes
3239 // its own measured B-cubed precision/recall so the page states its own accuracy.
3240 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🧩 Work families</a>\n<a class='it' href='sovereignty" as *u8)
3241 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3242 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🏛 Sovereignty ladder</a>\n<a class='it' href='sotaplan" as *u8)
3243 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3244 // SOTA rung plan. HAND-AUTHORED page, not generated by this tool -- linked here because a page the
3245 // nav does not mention is unreachable, and DISCOVERABILITY IS THE CAPABILITY: a document nobody can
3246 // find has the same value as one nobody wrote, and costs more. The generator must therefore know
3247 // about the non-generated pages it sits beside, or publishing one silently produces nothing.
3248 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🧭 SOTA rung plan</a>\n<a class='it' href='sota" as *u8)
3249 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3250 // SOTA by generation + Research briefs. HAND-AUTHORED pages, same class as the SOTA rung plan above
3251 // and linked for the same reason: a page the nav does not mention is unreachable. sota scores each
3252 // family-tree generation on its own bar and carries the 7-cluster work order; research is the briefs
3253 // library whose entries declare a Forks-off lineage so future research forks off it.
3254 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🌳 SOTA by generation</a>\n<a class='it' href='research" as *u8)
3255 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3256 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>📚 Research briefs</a>\n<a class='it' href='atlas" as *u8)
3257 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3258 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>🗺 Atlas (domains)</a>\n<div class='sec'>topics</div>\n" as *u8)
3259 // top families by member count (threshold + cap declared on the topics page)
3260 let sbf: *i64 = sys_mmap((CW_SBFAM + 2) * 8) as *i64
3261 var nsbf: i64 = 0
3262 var tf0: i64 = 0
3263 while tf0 < CW_SBFAM {
3264 var tbest: i64 = 0 - 1
3265 var tf1: i64 = 0
3266 while tf1 < nfam {
3267 var ttk: i64 = 0
3268 var tf2: i64 = 0
3269 while tf2 < nsbf { if sbf[tf2] == tf1 { ttk = 1 } tf2 = tf2 + 1 }
3270 if ttk == 0 { if famcnt[tf1] >= CW_FAMMIN { if tbest < 0 { tbest = tf1 } else { if famloc[tf1] > famloc[tbest] { tbest = tf1 } } } }
3271 tf1 = tf1 + 1
3272 }
3273 if tbest >= 0 {
3274 sbf[nsbf] = tbest
3275 nsbf = nsbf + 1
3276 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<a class='it' href='t_" as *u8)
3277 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, ((famarena as i64) + famoff[tbest]) as *u8)
3278 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3279 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>" as *u8)
3280 cw_appe(sbbuf, sbpos, CW_MAGIC_65536, ((famarena as i64) + famoff[tbest]) as *u8, famlen[tbest])
3281 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<span class='ct'>" as *u8)
3282 cw_appn(sbbuf, sbpos, CW_MAGIC_65536, famcnt[tbest])
3283 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "</span></a>\n" as *u8)
3284 }
3285 tf0 = tf0 + 1
3286 }
3287 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<a class='it' href='topics" as *u8)
3288 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3289 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>all topics →</a>\n<div class='sec'>directories</div>\n" as *u8)
3290 var sb0: i64 = 0
3291 while sb0 < ndirs {
3292 let sdl: *u8 = ((dirarena as i64) + dirnoff[sb0]) as *u8
3293 let sds: *u8 = ((dirarena as i64) + dirslug[sb0]) as *u8
3294 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<a class='it' href='" as *u8)
3295 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, sds)
3296 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3297 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'>" as *u8)
3298 if cw_slen(sdl) == 0 { cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "(root)" as *u8) } else { cw_appe(sbbuf, sbpos, CW_MAGIC_65536, sdl, cw_slen(sdl)) }
3299 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<span class='ct'>" as *u8)
3300 cw_appn(sbbuf, sbpos, CW_MAGIC_65536, dircnt[sb0])
3301 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "</span></a>\n" as *u8)
3302 sb0 = sb0 + 1
3303 }
3304 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<div class='sec'>load-bearing</div>\n" as *u8)
3305 // top 8 by fan-in, bounded selection
3306 let sbk: *i64 = sys_mmap(16 * 8) as *i64
3307 var nsb: i64 = 0
3308 var sb1: i64 = 0
3309 while sb1 < nsorted {
3310 if nsb < 8 {
3311 var bb: i64 = 0 - 1
3312 var sb2: i64 = 0
3313 while sb2 < nsorted {
3314 let cnd3: i64 = sidx[sb2]
3315 var tk: i64 = 0
3316 var t3: i64 = 0
3317 while t3 < nsb { if sbk[t3] == cnd3 { tk = 1 } t3 = t3 + 1 }
3318 if tk == 0 { if bb < 0 { bb = cnd3 } else { if g.ca_arr[cnd3] > g.ca_arr[bb] { bb = cnd3 } } }
3319 sb2 = sb2 + 1
3320 }
3321 if bb >= 0 { if g.ca_arr[bb] > 0 {
3322 sbk[nsb] = bb
3323 nsb = nsb + 1
3324 let bnm2: *u8 = ((g.arena as i64) + g.node_off[bb]) as *u8
3325 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "<a class='it' href='" as *u8)
3326 cw_appstem(sbbuf, sbpos, CW_MAGIC_65536, bnm2)
3327 cw_appext(sbbuf, sbpos, CW_MAGIC_65536, noext)
3328 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "'><code>" as *u8)
3329 var bl2: i64 = cw_stemlen(bnm2)
3330 if bl2 > 24 { bl2 = 24 }
3331 cw_appe(sbbuf, sbpos, CW_MAGIC_65536, bnm2, bl2)
3332 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "</code></a>\n" as *u8)
3333 } }
3334 }
3335 sb1 = sb1 + 1
3336 }
3337 cw_apps(sbbuf, sbpos, CW_MAGIC_65536, "</nav>\n" as *u8)
3338 sbbuf[sbpos[0]] = 0 as u8
3339
3340 // ===== pass 1.5: THE CALL GRAPH =====
3341 // One extra read per file: comment-strip, collect defs + unresolved call
3342 // sites (cwc_scan), then resolve in memory. Resolution order: same module
3343 // -> a DIRECT import -> globally unique name; anything else is COUNTED as
3344 // ambiguous/unknown, never guessed (the honest-number rule).
3345 let dnames: *u8 = sys_mmap(CW_FNARENA)
3346 let dnoff: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3347 let dnlen: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3348 let dmod: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3349 let dline: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3350 let dhead: *i64 = sys_mmap(CW_DEFHASH * 8) as *i64
3351 let dnext: *i64 = sys_mmap(CW_MAXFN * 8) as *i64
3352 let dnbox: *i64 = sys_mmap(16)
3353 let dubox: *i64 = sys_mmap(16)
3354 dnbox[0] = 0
3355 dubox[0] = 0
3356 var zc0: i64 = 0
3357 while zc0 < CW_DEFHASH { dhead[zc0] = 0; zc0 = zc0 + 1 }
3358 let snames: *u8 = sys_mmap(CW_SITEARENA)
3359 let snoff: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3360 let snlen: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3361 let smod: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3362 let scaller: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3363 let nsbox: *i64 = sys_mmap(16)
3364 let subox: *i64 = sys_mmap(16)
3365 nsbox[0] = 0
3366 subox[0] = 0
3367 let stripbuf: *u8 = sys_mmap(GL_FILECAP)
3368 let mdefh: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3369 let mdefc: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3370 var s15: i64 = 0
3371 while s15 < nsorted {
3372 let vc: i64 = sidx[s15]
3373 let fpc: *u8 = ((patharena as i64) + pathoff[vc] - 1) as *u8
3374 let nc: i64 = gl_read(fpc, src, GL_FILECAP)
3375 let sn2: i64 = gl_strip_comments(src, nc, stripbuf)
3376 mdefh[vc] = dnbox[0]
3377 cwc_scan(stripbuf, sn2, vc, dnames, dnoff, dnlen, dmod, dline, dhead, dnext, dnbox, dubox,
3378 snames, snoff, snlen, smod, scaller, nsbox, subox)
3379 mdefc[vc] = dnbox[0] - mdefh[vc]
3380 s15 = s15 + 1
3381 }
3382 let ndefs: i64 = dnbox[0]
3383 let nsites: i64 = nsbox[0]
3384
3385 // resolve sites -> def-level edges (deduped per caller within a window)
3386 let cesrc: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3387 let cedst: *i64 = sys_mmap(CW_MAXSITE * 8) as *i64
3388 var ncedge: i64 = 0
3389 var namb: i64 = 0
3390 var nunk: i64 = 0
3391 let dwin: *i64 = sys_mmap(CW_CALLDEDUP * 8) as *i64
3392 var ndwin: i64 = 0
3393 var lastcaller: i64 = 0 - 2
3394 var k5: i64 = 0
3395 while k5 < nsites {
3396 let snm2: *u8 = ((snames as i64) + snoff[k5]) as *u8
3397 let snl2: i64 = snlen[k5]
3398 var inmod: i64 = 0 - 1
3399 var inimp: i64 = 0 - 1
3400 var glob: i64 = 0 - 1
3401 var gcount: i64 = 0
3402 var cur: i64 = dhead[cwc_hash(snm2, snl2)]
3403 while cur != 0 {
3404 let di: i64 = cur - 1
3405 if dnlen[di] == snl2 {
3406 var eq2: i64 = 1
3407 var cb: i64 = 0
3408 while cb < snl2 { if dnames[dnoff[di] + cb] != snm2[cb] { eq2 = 0; cb = snl2 } else { cb = cb + 1 } }
3409 if eq2 == 1 {
3410 gcount = gcount + 1
3411 glob = di
3412 if dmod[di] == smod[k5] { inmod = di } else {
3413 if inimp < 0 {
3414 var p7: i64 = g.out_head[smod[k5]]
3415 let e7: i64 = g.out_head[smod[k5] + 1]
3416 while p7 < e7 { if g.out_list[p7] == dmod[di] { inimp = di; p7 = e7 } else { p7 = p7 + 1 } }
3417 }
3418 }
3419 }
3420 }
3421 cur = dnext[di]
3422 }
3423 var pick: i64 = inmod
3424 if pick < 0 { pick = inimp }
3425 if pick < 0 { if gcount == 1 { pick = glob } }
3426 if pick < 0 {
3427 if gcount > 1 { namb = namb + 1 } else { nunk = nunk + 1 }
3428 } else {
3429 if scaller[k5] != lastcaller { ndwin = 0; lastcaller = scaller[k5] }
3430 var dup2: i64 = 0
3431 var w5: i64 = 0
3432 while w5 < ndwin { if dwin[w5] == pick { dup2 = 1 } w5 = w5 + 1 }
3433 if dup2 == 0 {
3434 if ndwin < CW_CALLDEDUP { dwin[ndwin] = pick; ndwin = ndwin + 1 }
3435 if ncedge < CW_MAXSITE {
3436 cesrc[ncedge] = scaller[k5]
3437 cedst[ncedge] = pick
3438 ncedge = ncedge + 1
3439 }
3440 }
3441 }
3442 k5 = k5 + 1
3443 }
3444
3445 // def-level CSR both directions (the eg_finalize pattern)
3446 let cf_oh: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3447 let cf_ih: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3448 let cf_oc: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3449 let cf_ic: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3450 let cf_ol: *i64 = sys_mmap((ncedge + 1) * 8) as *i64
3451 let cf_il: *i64 = sys_mmap((ncedge + 1) * 8) as *i64
3452 var z6: i64 = 0
3453 while z6 <= ndefs { cf_oc[z6] = 0; cf_ic[z6] = 0; z6 = z6 + 1 }
3454 var z7: i64 = 0
3455 while z7 < ncedge { cf_oc[cesrc[z7]] = cf_oc[cesrc[z7]] + 1; cf_ic[cedst[z7]] = cf_ic[cedst[z7]] + 1; z7 = z7 + 1 }
3456 cf_oh[0] = 0
3457 cf_ih[0] = 0
3458 var z8: i64 = 0
3459 while z8 < ndefs { cf_oh[z8 + 1] = cf_oh[z8] + cf_oc[z8]; cf_ih[z8 + 1] = cf_ih[z8] + cf_ic[z8]; z8 = z8 + 1 }
3460 let cur_o2: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3461 let cur_i2: *i64 = sys_mmap((ndefs + 2) * 8) as *i64
3462 var z9: i64 = 0
3463 while z9 < ndefs { cur_o2[z9] = cf_oh[z9]; cur_i2[z9] = cf_ih[z9]; z9 = z9 + 1 }
3464 var za: i64 = 0
3465 while za < ncedge {
3466 cf_ol[cur_o2[cesrc[za]]] = cedst[za]
3467 cur_o2[cesrc[za]] = cur_o2[cesrc[za]] + 1
3468 cf_il[cur_i2[cedst[za]]] = cesrc[za]
3469 cur_i2[cedst[za]] = cur_i2[cedst[za]] + 1
3470 za = za + 1
3471 }
3472 st[12] = ndefs
3473 st[13] = ncedge
3474 st[14] = namb + nunk
3475
3476 // ===== module KINDS: what each file IS, by measured rule =====
3477 let hasmain: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3478 var hm0: i64 = 0
3479 while hm0 < nsorted { hasmain[sidx[hm0]] = 0; hm0 = hm0 + 1 }
3480 var hm1: i64 = 0
3481 while hm1 < ndefs {
3482 if dnlen[hm1] == 4 {
3483 if dnames[dnoff[hm1]] == (109 as u8) { if dnames[dnoff[hm1]+1] == (97 as u8) { if dnames[dnoff[hm1]+2] == (105 as u8) { if dnames[dnoff[hm1]+3] == (110 as u8) {
3484 hasmain[dmod[hm1]] = 1
3485 } } } }
3486 }
3487 hm1 = hm1 + 1
3488 }
3489 let kindv: *i64 = sys_mmap(CW_MAXNODE * 8) as *i64
3490 let kindcnt: *i64 = sys_mmap(CW_NKIND * 8) as *i64
3491 let kindloc: *i64 = sys_mmap(CW_NKIND * 8) as *i64
3492 var kz: i64 = 0
3493 while kz < CW_NKIND { kindcnt[kz] = 0; kindloc[kz] = 0; kz = kz + 1 }
3494 var kk: i64 = 0
3495 while kk < nsorted {
3496 let kv: i64 = sidx[kk]
3497 let knm: *u8 = ((g.arena as i64) + g.node_off[kv]) as *u8
3498 let kdl: *u8 = ((dirarena as i64) + dirnoff[fdir[kv]]) as *u8
3499 var kind: i64 = 0 - 1
3500 // 1. archived: lives in a _retired* / _attic dir
3501 if cw_namehas(kdl, "_retired" as *u8) == 1 { kind = CW_K_ARCHIVED }
3502 if kind < 0 { if cw_namehas(kdl, "_attic" as *u8) == 1 { kind = CW_K_ARCHIVED } }
3503 // 2. gate/proof (both live conventions, per nx_gatelib)
3504 if kind < 0 { if gl_is_gate_name(knm, cw_slen(knm)) == 1 { kind = CW_K_GATE } }
3505 // 3-6. bench / probe / smoke / sketch-demo, by naming convention
3506 if kind < 0 { if cw_namehas(knm, "bench" as *u8) == 1 { kind = CW_K_BENCH } }
3507 if kind < 0 { if cw_namehas(knm, "_probe" as *u8) == 1 { kind = CW_K_PROBE } }
3508 if kind < 0 { if cw_namehas(knm, "smoke" as *u8) == 1 { kind = CW_K_SMOKE } }
3509 if kind < 0 { if cw_namehas(knm, "_demo" as *u8) == 1 { kind = CW_K_SKETCH } }
3510 if kind < 0 { if cw_namehas(knm, "sketch_" as *u8) == 1 { kind = CW_K_SKETCH } }
3511 // 7. generated fixture: says so in its own header, or family-scale + tiny
3512 if kind < 0 { if dlen[kv] > 0 {
3513 if cw_hastok(descarena, doff[kv], doff[kv] + dlen[kv], "auto-generated" as *u8, 14) == 1 { kind = CW_K_GENFIX }
3514 } }
3515 if kind < 0 { if modfam[kv] >= 0 { if famcnt[modfam[kv]] >= CW_GENFIX_FAM { if floc[kv] < CW_GENFIX_LOC { kind = CW_K_GENFIX } } } }
3516 // 8. service: owns main and carries a serving suffix
3517 if kind < 0 { if hasmain[kv] == 1 {
3518 if gl_ends(knm, cw_slen(knm), "_daemon.nx" as *u8) == 1 { kind = CW_K_SERVICE }
3519 if kind < 0 { if gl_ends(knm, cw_slen(knm), "_serve.nx" as *u8) == 1 { kind = CW_K_SERVICE } }
3520 if kind < 0 { if gl_ends(knm, cw_slen(knm), "_server.nx" as *u8) == 1 { kind = CW_K_SERVICE } }
3521 if kind < 0 { if gl_ends(knm, cw_slen(knm), "_gw.nx" as *u8) == 1 { kind = CW_K_SERVICE } }
3522 if kind < 0 { if gl_ends(knm, cw_slen(knm), "_api.nx" as *u8) == 1 { kind = CW_K_SERVICE } }
3523 } }
3524 // 9-11. tool (owns main) / library (imported) / orphan library
3525 if kind < 0 { if hasmain[kv] == 1 { kind = CW_K_TOOL } }
3526 if kind < 0 { if g.ca_arr[kv] > 0 { kind = CW_K_LIB } }
3527 if kind < 0 { kind = CW_K_ORPHAN }
3528 kindv[kv] = kind
3529 kindcnt[kind] = kindcnt[kind] + 1
3530 kindloc[kind] = kindloc[kind] + floc[kv]
3531 kk = kk + 1
3532 }
3533 st[18] = kindcnt[CW_K_ORPHAN]
3534
3535 // ===== pass 2: emit one page per file node =====
3536 let page: *u8 = sys_mmap(CW_PAGECAP)
3537 let pos: *i64 = sys_mmap(32) as *i64
3538 let opath: *u8 = sys_mmap(CW_MAGIC_4096)
3539 var tfuncs: i64 = 0
3540 var tstructs: i64 = 0
3541 var tconsts: i64 = 0
3542 var pages: i64 = 0
3543 var truncs: i64 = 0
3544
3545 var s2i: i64 = 0
3546 while s2i < nsorted {
3547 let v2: i64 = sidx[s2i]
3548 if pathoff[v2] > 0 {
3549 let fp2: *u8 = ((patharena as i64) + pathoff[v2] - 1) as *u8
3550 let nm: *u8 = ((g.arena as i64) + g.node_off[v2]) as *u8
3551 let n2: i64 = gl_read(fp2, src, GL_FILECAP)
3552 pos[0] = 0
3553 pos[1] = 0
3554 cw_head3(page, pos, CW_PAGECAP, nm, cw_slen(nm), sbbuf)
3555 let dslug: *u8 = ((dirarena as i64) + dirslug[fdir[v2]]) as *u8
3556 let dlab2: *u8 = ((dirarena as i64) + dirnoff[fdir[v2]]) as *u8
3557 cw_crumb(page, pos, CW_PAGECAP, noext, dlab2, dslug, nm, cw_slen(nm))
3558 cw_apps(page, pos, CW_PAGECAP, "<h1>" as *u8)
3559 cw_appe(page, pos, CW_PAGECAP, nm, cw_slen(nm))
3560 cw_apps(page, pos, CW_PAGECAP, "</h1>\n<p class='muted'><code>" as *u8)
3561 cw_appe(page, pos, CW_PAGECAP, fp2, cw_slen(fp2))
3562 cw_apps(page, pos, CW_PAGECAP, "</code></p>\n<div class='meta'><span class='chip'><b>" as *u8)
3563 cw_appn(page, pos, CW_PAGECAP, fbytes[v2])
3564 cw_apps(page, pos, CW_PAGECAP, "</b> B</span><span class='chip'><b>" as *u8)
3565 cw_appn(page, pos, CW_PAGECAP, floc[v2])
3566 cw_apps(page, pos, CW_PAGECAP, "</b> lines</span><span class='chip'>depth <b>" as *u8)
3567 cw_appn(page, pos, CW_PAGECAP, depthv[v2])
3568 cw_apps(page, pos, CW_PAGECAP, "</b></span><span class='chip'>pulls <b>" as *u8)
3569 cw_appn(page, pos, CW_PAGECAP, tri_out[v2])
3570 cw_apps(page, pos, CW_PAGECAP, "</b> transitive</span><span class='chip'>reach <b>" as *u8)
3571 cw_appn(page, pos, CW_PAGECAP, tri_in[v2])
3572 cw_apps(page, pos, CW_PAGECAP, "</b> importers</span><span class='chip'><a href='src_" as *u8)
3573 cw_app(page, pos, CW_PAGECAP, nm, cw_stemlen(nm))
3574 cw_appext(page, pos, CW_PAGECAP, noext)
3575 cw_apps(page, pos, CW_PAGECAP, "#L1'>view source</a></span><span class='chip'>kind <a href='kinds" as *u8)
3576 cw_appext(page, pos, CW_PAGECAP, noext)
3577 cw_apps(page, pos, CW_PAGECAP, "'><b>" as *u8)
3578 cw_apps(page, pos, CW_PAGECAP, cw_kindname(kindv[v2]))
3579 cw_apps(page, pos, CW_PAGECAP, "</b></a></span>" as *u8)
3580 if modfam[v2] >= 0 { if famcnt[modfam[v2]] >= CW_FAMMIN {
3581 cw_apps(page, pos, CW_PAGECAP, "<span class='chip'>topic <a href='t_" as *u8)
3582 cw_apps(page, pos, CW_PAGECAP, ((famarena as i64) + famoff[modfam[v2]]) as *u8)
3583 cw_appext(page, pos, CW_PAGECAP, noext)
3584 cw_apps(page, pos, CW_PAGECAP, "'><b>" as *u8)
3585 cw_appe(page, pos, CW_PAGECAP, ((famarena as i64) + famoff[modfam[v2]]) as *u8, famlen[modfam[v2]])
3586 cw_apps(page, pos, CW_PAGECAP, "</b></a></span>" as *u8)
3587 } }
3588 cw_apps(page, pos, CW_PAGECAP, "</div>\n<div class='toc'><a href='#docs'>docs</a><a href='#deps'>dependencies</a><a href='#structs'>structs</a><a href='#consts'>consts</a><a href='#functions'>functions</a></div>\n" as *u8)
3589
3590 // header doc block: leading // lines (cap 40)
3591 var hl: i64 = 0
3592 var ls: i64 = 0
3593 var started: i64 = 0
3594 var stop: i64 = 0
3595 while stop == 0 {
3596 if ls >= n2 { stop = 1 } else {
3597 var le2: i64 = ls
3598 var g2: i64 = 1
3599 while g2 == 1 { if le2 >= n2 { g2 = 0 } else { if src[le2] == (10 as u8) { g2 = 0 } else { le2 = le2 + 1 } } }
3600 var iscmt: i64 = 0
3601 if ls + 1 < le2 { if src[ls] == (47 as u8) { if src[ls+1] == (47 as u8) { iscmt = 1 } } }
3602 if iscmt == 1 {
3603 if started == 0 { cw_apps(page, pos, CW_PAGECAP, "<h2 id='docs'>about</h2>\n<div class='doc'>" as *u8); started = 1 }
3604 var cs: i64 = ls + 2
3605 if cs < le2 { if src[cs] == (32 as u8) { cs = cs + 1 } }
3606 cw_appe(page, pos, CW_PAGECAP, ((src as i64) + cs) as *u8, le2 - cs)
3607 cw_apps(page, pos, CW_PAGECAP, "\n" as *u8)
3608 hl = hl + 1
3609 if hl >= 40 { stop = 1 }
3610 ls = le2 + 1
3611 } else { stop = 1 }
3612 }
3613 }
3614 if started == 1 { cw_apps(page, pos, CW_PAGECAP, "</div>\n" as *u8) }
3615
3616 // dependencies: neighborhood diagram + the COMPLETE pill lists
3617 cw_apps(page, pos, CW_PAGECAP, "<h2 id='deps'>dependencies <span class='cnt'>" as *u8)
3618 cw_appn(page, pos, CW_PAGECAP, g.ce_arr[v2])
3619 cw_apps(page, pos, CW_PAGECAP, " imports · " as *u8)
3620 cw_appn(page, pos, CW_PAGECAP, g.ca_arr[v2])
3621 cw_apps(page, pos, CW_PAGECAP, " importers</span></h2>\n" as *u8)
3622 cw_nbr(page, pos, CW_PAGECAP, g, v2, pathoff, noext)
3623 cw_apps(page, pos, CW_PAGECAP, "<p><b class='muted'>imports:</b> " as *u8)
3624 var op: i64 = g.out_head[v2]
3625 let oe: i64 = g.out_head[v2 + 1]
3626 if op >= oe { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none</span>" as *u8) }
3627 while op < oe {
3628 let dep2: i64 = g.out_list[op]
3629 let dnm: *u8 = ((g.arena as i64) + g.node_off[dep2]) as *u8
3630 if pathoff[dep2] > 0 {
3631 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
3632 cw_applink2(page, pos, CW_PAGECAP, dnm, noext)
3633 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
3634 } else {
3635 cw_apps(page, pos, CW_PAGECAP, "<span class='pill ext'>" as *u8)
3636 cw_appe(page, pos, CW_PAGECAP, dnm, cw_slen(dnm))
3637 cw_apps(page, pos, CW_PAGECAP, " (unresolved)</span>" as *u8)
3638 }
3639 op = op + 1
3640 }
3641 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
3642
3643 // imported-by (in CSR), shown cap declared
3644 cw_apps(page, pos, CW_PAGECAP, "<p><b class='muted'>imported by:</b> " as *u8)
3645 var ip: i64 = g.in_head[v2]
3646 let ie: i64 = g.in_head[v2 + 1]
3647 let intotal: i64 = ie - ip
3648 if intotal <= 0 { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>nobody (leaf or entry point)</span>" as *u8) }
3649 var shown: i64 = 0
3650 while ip < ie {
3651 if shown < CW_SHOWIN {
3652 let who: i64 = g.in_list[ip]
3653 let wnm: *u8 = ((g.arena as i64) + g.node_off[who]) as *u8
3654 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
3655 cw_applink2(page, pos, CW_PAGECAP, wnm, noext)
3656 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
3657 shown = shown + 1
3658 }
3659 ip = ip + 1
3660 }
3661 if intotal > CW_SHOWIN {
3662 cw_apps(page, pos, CW_PAGECAP, " <span class='muted'>+" as *u8)
3663 cw_appn(page, pos, CW_PAGECAP, intotal - CW_SHOWIN)
3664 cw_apps(page, pos, CW_PAGECAP, " more (shown cap " as *u8)
3665 cw_appn(page, pos, CW_PAGECAP, CW_SHOWIN)
3666 cw_apps(page, pos, CW_PAGECAP, " declared)</span>" as *u8)
3667 }
3668 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
3669
3670 // call flow from main, when this module has one (the entry-point story)
3671 var maind: i64 = 0 - 1
3672 var ms0: i64 = mdefh[v2]
3673 let msE: i64 = mdefh[v2] + mdefc[v2]
3674 while ms0 < msE {
3675 if dnlen[ms0] == 4 { if dnames[dnoff[ms0]] == (109 as u8) { if dnames[dnoff[ms0]+1] == (97 as u8) { if dnames[dnoff[ms0]+2] == (105 as u8) { if dnames[dnoff[ms0]+3] == (110 as u8) { maind = ms0 } } } } }
3676 ms0 = ms0 + 1
3677 }
3678 if maind >= 0 { if cf_oh[maind + 1] - cf_oh[maind] > 0 {
3679 cw_apps(page, pos, CW_PAGECAP, "<h2>call flow from main <span class='cnt'>pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown</span></h2>\n" as *u8)
3680 cw_callflow(page, pos, CW_PAGECAP, g, dmod, dnames, dnoff, dnlen, cf_oh, cf_ol, maind, noext)
3681 } }
3682
3683 // structs / consts / functions -- one line pass, three tables built as we go
3684 // (functions into the page directly; structs+consts buffered would need
3685 // arenas, so instead: three passes over lines, each cheap and clear)
3686 var nfn2: i64 = 0
3687
3688 cw_apps(page, pos, CW_PAGECAP, "<h2 id='structs'>structs</h2>\n<table>" as *u8)
3689 var any1: i64 = 0
3690 var ls2: i64 = 0
3691 var ln: i64 = 1
3692 while ls2 < n2 {
3693 var le3: i64 = ls2
3694 var g3: i64 = 1
3695 while g3 == 1 { if le3 >= n2 { g3 = 0 } else { if src[le3] == (10 as u8) { g3 = 0 } else { le3 = le3 + 1 } } }
3696 if cw_line_starts(src, ls2, le3, "struct " as *u8) == 1 {
3697 any1 = 1
3698 tstructs = tstructs + 1
3699 cw_apps(page, pos, CW_PAGECAP, "<tr><td class='n'>" as *u8)
3700 cw_appsrclink(page, pos, CW_PAGECAP, nm, ln, noext)
3701 cw_apps(page, pos, CW_PAGECAP, "</td><td class='sig'>" as *u8)
3702 cw_appe(page, pos, CW_PAGECAP, ((src as i64) + ls2) as *u8, cw_sigend(src, ls2, le3) - ls2)
3703 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
3704 }
3705 ln = ln + 1
3706 ls2 = le3 + 1
3707 }
3708 if any1 == 0 { cw_apps(page, pos, CW_PAGECAP, "<tr><td class='muted'>none</td></tr>" as *u8) }
3709 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
3710
3711 cw_apps(page, pos, CW_PAGECAP, "<h2 id='consts'>consts</h2>\n<table>" as *u8)
3712 var any2: i64 = 0
3713 var ls3: i64 = 0
3714 var ln2: i64 = 1
3715 while ls3 < n2 {
3716 var le4: i64 = ls3
3717 var g4: i64 = 1
3718 while g4 == 1 { if le4 >= n2 { g4 = 0 } else { if src[le4] == (10 as u8) { g4 = 0 } else { le4 = le4 + 1 } } }
3719 if cw_line_starts(src, ls3, le4, "const " as *u8) == 1 {
3720 any2 = 1
3721 tconsts = tconsts + 1
3722 cw_apps(page, pos, CW_PAGECAP, "<tr><td class='n'>" as *u8)
3723 cw_appsrclink(page, pos, CW_PAGECAP, nm, ln2, noext)
3724 cw_apps(page, pos, CW_PAGECAP, "</td><td class='sig'>" as *u8)
3725 cw_appe(page, pos, CW_PAGECAP, ((src as i64) + ls3) as *u8, cw_sigend(src, ls3, le4) - ls3)
3726 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
3727 }
3728 ln2 = ln2 + 1
3729 ls3 = le4 + 1
3730 }
3731 if any2 == 0 { cw_apps(page, pos, CW_PAGECAP, "<tr><td class='muted'>none</td></tr>" as *u8) }
3732 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
3733
3734 cw_apps(page, pos, CW_PAGECAP, "<h2 id='functions'>functions</h2>\n<table>" as *u8)
3735 var any3: i64 = 0
3736 var ls4: i64 = 0
3737 var ln3: i64 = 1
3738 while ls4 < n2 {
3739 var le5: i64 = ls4
3740 var g5: i64 = 1
3741 while g5 == 1 { if le5 >= n2 { g5 = 0 } else { if src[le5] == (10 as u8) { g5 = 0 } else { le5 = le5 + 1 } } }
3742 if cw_line_starts(src, ls4, le5, "func " as *u8) == 1 {
3743 any3 = 1
3744 nfn2 = nfn2 + 1
3745 tfuncs = tfuncs + 1
3746 // function name span (for the anchor + the search index)
3747 let fs0: i64 = ls4 + 5
3748 var fe0: i64 = fs0
3749 var gnm: i64 = 1
3750 while gnm == 1 {
3751 if fe0 < le5 { if gl_is_ident(src[fe0] as i64) == 1 { fe0 = fe0 + 1 } else { gnm = 0 } } else { gnm = 0 }
3752 }
3753 let fnl: i64 = fe0 - fs0
3754 cw_apps(page, pos, CW_PAGECAP, "<tr id='fn_" as *u8)
3755 if fnl > 0 { cw_app(page, pos, CW_PAGECAP, ((src as i64) + fs0) as *u8, fnl) }
3756 cw_apps(page, pos, CW_PAGECAP, "'><td class='n'>" as *u8)
3757 cw_appsrclink(page, pos, CW_PAGECAP, nm, ln3, noext)
3758 if fnl > 0 { if fnl < 96 { if nfns < CW_MAXFN { if fnu[0] + fnl + 1 < CW_FNARENA {
3759 var fc0: i64 = 0
3760 while fc0 < fnl { fnarena[fnu[0] + fc0] = src[fs0 + fc0]; fc0 = fc0 + 1 }
3761 fnoff[nfns] = fnu[0]
3762 fnlen[nfns] = fnl
3763 fnmod[nfns] = v2
3764 fnline[nfns] = ln3
3765 fnu[0] = fnu[0] + fnl
3766 nfns = nfns + 1
3767 } } } }
3768 cw_apps(page, pos, CW_PAGECAP, "</td><td class='sig'>" as *u8)
3769 cw_appe(page, pos, CW_PAGECAP, ((src as i64) + ls4) as *u8, cw_sigend(src, ls4, le5) - ls4)
3770 // call-graph annotation: callers/callees of THIS def (matched by line)
3771 var dfx: i64 = 0 - 1
3772 var dscan: i64 = mdefh[v2]
3773 let dend: i64 = mdefh[v2] + mdefc[v2]
3774 while dscan < dend { if dline[dscan] == ln3 { dfx = dscan; dscan = dend } else { dscan = dscan + 1 } }
3775 if dfx >= 0 {
3776 let ncal: i64 = cf_ih[dfx + 1] - cf_ih[dfx]
3777 let ncee: i64 = cf_oh[dfx + 1] - cf_oh[dfx]
3778 if ncal + ncee > 0 {
3779 cw_apps(page, pos, CW_PAGECAP, "<div class='fnc'>" as *u8)
3780 if ncal > 0 {
3781 cw_apps(page, pos, CW_PAGECAP, "<span class='lab'>called by " as *u8)
3782 cw_appn(page, pos, CW_PAGECAP, ncal)
3783 cw_apps(page, pos, CW_PAGECAP, ":</span> " as *u8)
3784 var cp0: i64 = cf_ih[dfx]
3785 var csh0: i64 = 0
3786 while cp0 < cf_ih[dfx + 1] {
3787 if csh0 < CW_CALLSHOW { cw_appdeflink(page, pos, CW_PAGECAP, g, dmod, dnames, dnoff, dnlen, cf_il[cp0], noext); csh0 = csh0 + 1 }
3788 cp0 = cp0 + 1
3789 }
3790 if ncal > CW_CALLSHOW { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>+" as *u8); cw_appn(page, pos, CW_PAGECAP, ncal - CW_CALLSHOW); cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8) }
3791 }
3792 if ncee > 0 {
3793 cw_apps(page, pos, CW_PAGECAP, " <span class='lab'>calls " as *u8)
3794 cw_appn(page, pos, CW_PAGECAP, ncee)
3795 cw_apps(page, pos, CW_PAGECAP, ":</span> " as *u8)
3796 var cp1: i64 = cf_oh[dfx]
3797 var csh1: i64 = 0
3798 while cp1 < cf_oh[dfx + 1] {
3799 if csh1 < CW_CALLSHOW { cw_appdeflink(page, pos, CW_PAGECAP, g, dmod, dnames, dnoff, dnlen, cf_ol[cp1], noext); csh1 = csh1 + 1 }
3800 cp1 = cp1 + 1
3801 }
3802 if ncee > CW_CALLSHOW { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>+" as *u8); cw_appn(page, pos, CW_PAGECAP, ncee - CW_CALLSHOW); cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8) }
3803 }
3804 cw_apps(page, pos, CW_PAGECAP, "</div>" as *u8)
3805 }
3806 }
3807 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
3808 }
3809 ln3 = ln3 + 1
3810 ls4 = le5 + 1
3811 }
3812 if any3 == 0 { cw_apps(page, pos, CW_PAGECAP, "<tr><td class='muted'>none</td></tr>" as *u8) }
3813 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
3814
3815 fnfn[v2] = nfn2
3816 cw_foot(page, pos, CW_PAGECAP)
3817 cw_pagepath(opath, outdir, nm)
3818 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
3819 if pos[1] == 1 { truncs = truncs + 1 }
3820
3821 // ----- the SOURCE PAGE: line-anchored, escaped, cross-linked -----
3822 pos[0] = 0
3823 pos[1] = 0
3824 cw_head3(page, pos, CW_PAGECAP, nm, cw_slen(nm), sbbuf)
3825 cw_crumb(page, pos, CW_PAGECAP, noext, dlab2, dslug, nm, cw_slen(nm))
3826 cw_apps(page, pos, CW_PAGECAP, "<h1><code>" as *u8)
3827 cw_appe(page, pos, CW_PAGECAP, nm, cw_slen(nm))
3828 cw_apps(page, pos, CW_PAGECAP, "</code> source</h1>\n<p class='muted'><a href='" as *u8)
3829 cw_appstem(page, pos, CW_PAGECAP, nm)
3830 cw_appext(page, pos, CW_PAGECAP, noext)
3831 cw_apps(page, pos, CW_PAGECAP, "'>↩ module page</a> · " as *u8)
3832 cw_appn(page, pos, CW_PAGECAP, floc[v2])
3833 cw_apps(page, pos, CW_PAGECAP, " lines · " as *u8)
3834 cw_appn(page, pos, CW_PAGECAP, fbytes[v2])
3835 cw_apps(page, pos, CW_PAGECAP, " B</p>\n<div class='srcv'>" as *u8)
3836 var sls: i64 = 0
3837 var sln: i64 = 1
3838 while sls < n2 {
3839 var sle: i64 = sls
3840 var sg: i64 = 1
3841 while sg == 1 { if sle >= n2 { sg = 0 } else { if src[sle] == (10 as u8) { sg = 0 } else { sle = sle + 1 } } }
3842 cw_apps(page, pos, CW_PAGECAP, "<span class='l' id='L" as *u8)
3843 cw_appn(page, pos, CW_PAGECAP, sln)
3844 cw_apps(page, pos, CW_PAGECAP, "'><span class='ln'>" as *u8)
3845 cw_appn(page, pos, CW_PAGECAP, sln)
3846 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
3847 var sve: i64 = sle
3848 if sve > sls { if src[sve - 1] == (13 as u8) { sve = sve - 1 } }
3849 cw_appe(page, pos, CW_PAGECAP, ((src as i64) + sls) as *u8, sve - sls)
3850 cw_apps(page, pos, CW_PAGECAP, "</span>\n" as *u8)
3851 sln = sln + 1
3852 sls = sle + 1
3853 }
3854 cw_apps(page, pos, CW_PAGECAP, "</div>\n" as *u8)
3855 cw_foot(page, pos, CW_PAGECAP)
3856 var sw0: i64 = 0
3857 var si0: i64 = 0
3858 while outdir[si0] != (0 as u8) { opath[sw0] = outdir[si0]; sw0 = sw0 + 1; si0 = si0 + 1 }
3859 opath[sw0] = 47 as u8
3860 sw0 = sw0 + 1
3861 sw0 = sw0 + cw_rawcpy(opath, sw0, "src_" as *u8)
3862 var si1: i64 = 0
3863 let stl: i64 = cw_stemlen(nm)
3864 while si1 < stl { opath[sw0] = nm[si1]; sw0 = sw0 + 1; si1 = si1 + 1 }
3865 sw0 = sw0 + cw_rawcpy(opath, sw0, ".html" as *u8)
3866 opath[sw0] = 0 as u8
3867 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
3868 if pos[1] == 1 { truncs = truncs + 1 }
3869 }
3870 s2i = s2i + 1
3871 }
3872 st[3] = tfuncs
3873 st[4] = tstructs
3874 st[5] = tconsts
3875 st[11] = truncs
3876
3877 // ===== pass 3a: directory pages =====
3878 var d3: i64 = 0
3879 while d3 < ndirs {
3880 pos[0] = 0
3881 pos[1] = 0
3882 let dlabel: *u8 = ((dirarena as i64) + dirnoff[d3]) as *u8
3883 var dll: i64 = cw_slen(dlabel)
3884 cw_head3(page, pos, CW_PAGECAP, dlabel, dll, sbbuf)
3885 cw_crumb(page, pos, CW_PAGECAP, noext, dlabel, 0 as *u8, dlabel, dll)
3886 cw_apps(page, pos, CW_PAGECAP, "<h1>" as *u8)
3887 if dll == 0 { cw_apps(page, pos, CW_PAGECAP, "(root)" as *u8) } else { cw_appe(page, pos, CW_PAGECAP, dlabel, dll) }
3888 cw_apps(page, pos, CW_PAGECAP, "</h1>\n<p class='muted'>" as *u8)
3889 cw_appn(page, pos, CW_PAGECAP, dircnt[d3])
3890 cw_apps(page, pos, CW_PAGECAP, " modules</p>\n" as *u8)
3891 // narrative sidecar (see cw_nar_include)
3892 let nslug: *u8 = ((dirarena as i64) + dirslug[d3]) as *u8
3893 cw_nar_include(page, pos, CW_PAGECAP, outdir, opath, nslug)
3894 cw_filterjs(page, pos, CW_PAGECAP)
3895 cw_apps(page, pos, CW_PAGECAP, "<table><tr><th>module</th><th>description</th><th class='n'>lines</th><th class='n'>funcs</th></tr>\n" as *u8)
3896 var s3i: i64 = 0
3897 while s3i < nsorted {
3898 let v3: i64 = sidx[s3i]
3899 if pathoff[v3] > 0 { if fdir[v3] == d3 {
3900 let nm3: *u8 = ((g.arena as i64) + g.node_off[v3]) as *u8
3901 cw_apps(page, pos, CW_PAGECAP, "<tr data-m='" as *u8)
3902 cw_appe(page, pos, CW_PAGECAP, nm3, cw_slen(nm3))
3903 cw_apps(page, pos, CW_PAGECAP, "'><td>" as *u8)
3904 cw_applink2(page, pos, CW_PAGECAP, nm3, noext)
3905 cw_apps(page, pos, CW_PAGECAP, "</td><td class='muted'>" as *u8)
3906 if dlen[v3] > 0 { cw_appe(page, pos, CW_PAGECAP, ((descarena as i64) + doff[v3]) as *u8, dlen[v3]) }
3907 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
3908 cw_appn(page, pos, CW_PAGECAP, floc[v3])
3909 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
3910 cw_appn(page, pos, CW_PAGECAP, fnfn[v3])
3911 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
3912 } }
3913 s3i = s3i + 1
3914 }
3915 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
3916 cw_foot(page, pos, CW_PAGECAP)
3917 var w2: i64 = 0
3918 var i4: i64 = 0
3919 while outdir[i4] != (0 as u8) { opath[w2] = outdir[i4]; w2 = w2 + 1; i4 = i4 + 1 }
3920 opath[w2] = 47 as u8; w2 = w2 + 1
3921 let slg: *u8 = ((dirarena as i64) + dirslug[d3]) as *u8
3922 var i5: i64 = 0
3923 while slg[i5] != (0 as u8) { opath[w2] = slg[i5]; w2 = w2 + 1; i5 = i5 + 1 }
3924 cw_rawcpy(opath, w2, ".html" as *u8)
3925 w2 = w2 + 5
3926 opath[w2] = 0 as u8
3927 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
3928 if pos[1] == 1 { truncs = truncs + 1 }
3929 d3 = d3 + 1
3930 }
3931
3932 // ===== pass 3f: topic pages (families >= CW_FAMMIN members) + topics index =====
3933 let tnmb: *u8 = sys_mmap(64)
3934 var tp0: i64 = 0
3935 while tp0 < nfam {
3936 if famcnt[tp0] >= CW_FAMMIN {
3937 let fmn2: *u8 = ((famarena as i64) + famoff[tp0]) as *u8
3938 pos[0] = 0
3939 pos[1] = 0
3940 cw_head3(page, pos, CW_PAGECAP, fmn2, famlen[tp0], sbbuf)
3941 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, fmn2, famlen[tp0])
3942 cw_apps(page, pos, CW_PAGECAP, "<h1>topic: " as *u8)
3943 cw_appe(page, pos, CW_PAGECAP, fmn2, famlen[tp0])
3944 cw_apps(page, pos, CW_PAGECAP, "</h1>\n<p class='muted'>" as *u8)
3945 cw_appn(page, pos, CW_PAGECAP, famcnt[tp0])
3946 cw_apps(page, pos, CW_PAGECAP, " modules sharing the <code>" as *u8)
3947 cw_appe(page, pos, CW_PAGECAP, fmn2, famlen[tp0])
3948 cw_apps(page, pos, CW_PAGECAP, "</code> name family (derived from the tree's prefix discipline).</p>\n" as *u8)
3949 var tw0: i64 = 0
3950 tw0 = tw0 + cw_rawcpy(tnmb, 0, "t_" as *u8)
3951 var tw1: i64 = 0
3952 while tw1 < famlen[tp0] { tnmb[tw0] = famarena[famoff[tp0] + tw1]; tw0 = tw0 + 1; tw1 = tw1 + 1 }
3953 tnmb[tw0] = 0 as u8
3954 cw_nar_include(page, pos, CW_PAGECAP, outdir, opath, tnmb)
3955 cw_filterjs(page, pos, CW_PAGECAP)
3956 cw_apps(page, pos, CW_PAGECAP, "<table><tr><th>module</th><th>description</th><th class='n'>lines</th><th class='n'>funcs</th></tr>\n" as *u8)
3957 var tm0: i64 = 0
3958 while tm0 < nsorted {
3959 let tmv: i64 = sidx[tm0]
3960 if modfam[tmv] == tp0 {
3961 let tmn: *u8 = ((g.arena as i64) + g.node_off[tmv]) as *u8
3962 cw_apps(page, pos, CW_PAGECAP, "<tr data-m='" as *u8)
3963 cw_appe(page, pos, CW_PAGECAP, tmn, cw_slen(tmn))
3964 cw_apps(page, pos, CW_PAGECAP, "'><td>" as *u8)
3965 cw_applink2(page, pos, CW_PAGECAP, tmn, noext)
3966 cw_apps(page, pos, CW_PAGECAP, "</td><td class='muted'>" as *u8)
3967 if dlen[tmv] > 0 { cw_appe(page, pos, CW_PAGECAP, ((descarena as i64) + doff[tmv]) as *u8, dlen[tmv]) }
3968 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
3969 cw_appn(page, pos, CW_PAGECAP, floc[tmv])
3970 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
3971 cw_appn(page, pos, CW_PAGECAP, fnfn[tmv])
3972 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
3973 }
3974 tm0 = tm0 + 1
3975 }
3976 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
3977 cw_foot(page, pos, CW_PAGECAP)
3978 cw_outpath(opath, outdir, tnmb)
3979 var tw2: i64 = cw_slen(opath)
3980 cw_rawcpy(opath, tw2, ".html" as *u8)
3981 opath[tw2 + 5] = 0 as u8
3982 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
3983 if pos[1] == 1 { truncs = truncs + 1 }
3984 }
3985 tp0 = tp0 + 1
3986 }
3987 // the topics index
3988 pos[0] = 0
3989 pos[1] = 0
3990 cw_head3(page, pos, CW_PAGECAP, "topics" as *u8, 6, sbbuf)
3991 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "topics" as *u8, 6)
3992 cw_apps(page, pos, CW_PAGECAP, "<h1>topics</h1>\n<p class='muted'>every name family with at least " as *u8)
3993 cw_appn(page, pos, CW_PAGECAP, CW_FAMMIN)
3994 cw_apps(page, pos, CW_PAGECAP, " member modules (threshold declared); ordered by total lines of code.</p>\n<table><tr><th>topic</th><th class='n'>modules</th><th class='n'>lines</th></tr>\n" as *u8)
3995 let tix: *i64 = sys_mmap((nfam + 2) * 8) as *i64
3996 var ntix: i64 = 0
3997 var ti0: i64 = 0
3998 while ti0 < nfam {
3999 var tibest: i64 = 0 - 1
4000 var ti1: i64 = 0
4001 while ti1 < nfam {
4002 var titk: i64 = 0
4003 var ti2: i64 = 0
4004 while ti2 < ntix { if tix[ti2] == ti1 { titk = 1 } ti2 = ti2 + 1 }
4005 if titk == 0 { if famcnt[ti1] >= CW_FAMMIN { if tibest < 0 { tibest = ti1 } else { if famloc[ti1] > famloc[tibest] { tibest = ti1 } } } }
4006 ti1 = ti1 + 1
4007 }
4008 if tibest >= 0 {
4009 tix[ntix] = tibest
4010 ntix = ntix + 1
4011 cw_apps(page, pos, CW_PAGECAP, "<tr><td><a href='t_" as *u8)
4012 cw_apps(page, pos, CW_PAGECAP, ((famarena as i64) + famoff[tibest]) as *u8)
4013 cw_appext(page, pos, CW_PAGECAP, noext)
4014 cw_apps(page, pos, CW_PAGECAP, "'>" as *u8)
4015 cw_appe(page, pos, CW_PAGECAP, ((famarena as i64) + famoff[tibest]) as *u8, famlen[tibest])
4016 cw_apps(page, pos, CW_PAGECAP, "</a></td><td class='n'>" as *u8)
4017 cw_appn(page, pos, CW_PAGECAP, famcnt[tibest])
4018 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
4019 cw_appn(page, pos, CW_PAGECAP, famloc[tibest])
4020 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4021 }
4022 ti0 = ti0 + 1
4023 }
4024 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4025 cw_foot(page, pos, CW_PAGECAP)
4026 cw_outpath(opath, outdir, "topics.html" as *u8)
4027 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4028 if pos[1] == 1 { truncs = truncs + 1 }
4029
4030 // ===== pass 3g: the KIND CENSUS (what everything IS, by measured rule) =====
4031 pos[0] = 0
4032 pos[1] = 0
4033 cw_head3(page, pos, CW_PAGECAP, "inventory" as *u8, 9, sbbuf)
4034 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "inventory" as *u8, 9)
4035 cw_apps(page, pos, CW_PAGECAP, "<h1>inventory: what every module IS</h1>\n<p class='muted'>classified by measurable rule, first match wins: archived dir → gate/test name → bench → probe → smoke → sketch/demo → generated fixture (self-declared, or family ≥ " as *u8)
4036 cw_appn(page, pos, CW_PAGECAP, CW_GENFIX_FAM)
4037 cw_apps(page, pos, CW_PAGECAP, " members and < " as *u8)
4038 cw_appn(page, pos, CW_PAGECAP, CW_GENFIX_LOC)
4039 cw_apps(page, pos, CW_PAGECAP, " lines) → service (main + serving suffix) → tool (owns main) → library (imported) → orphan library (no main, no importers — candidate dead code). Nothing is guessed; a rule either fires or the next one is tried.</p>\n" as *u8)
4040 var kc0: i64 = 0
4041 while kc0 < CW_NKIND {
4042 cw_apps(page, pos, CW_PAGECAP, "<h2>" as *u8)
4043 cw_apps(page, pos, CW_PAGECAP, cw_kindname(kc0))
4044 cw_apps(page, pos, CW_PAGECAP, " <span class='cnt'>" as *u8)
4045 cw_appn(page, pos, CW_PAGECAP, kindcnt[kc0])
4046 cw_apps(page, pos, CW_PAGECAP, " modules · " as *u8)
4047 cw_appn(page, pos, CW_PAGECAP, kindloc[kc0])
4048 cw_apps(page, pos, CW_PAGECAP, " lines · first " as *u8)
4049 cw_appn(page, pos, CW_PAGECAP, CW_KINDSHOW)
4050 cw_apps(page, pos, CW_PAGECAP, " shown (declared)</span></h2>\n<p>" as *u8)
4051 var ks0: i64 = 0
4052 var ksh: i64 = 0
4053 while ks0 < nsorted {
4054 let ksv: i64 = sidx[ks0]
4055 if kindv[ksv] == kc0 { if ksh < CW_KINDSHOW {
4056 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
4057 cw_applink2(page, pos, CW_PAGECAP, ((g.arena as i64) + g.node_off[ksv]) as *u8, noext)
4058 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
4059 ksh = ksh + 1
4060 } }
4061 ks0 = ks0 + 1
4062 }
4063 if kindcnt[kc0] > CW_KINDSHOW {
4064 cw_apps(page, pos, CW_PAGECAP, " <span class='muted'>+" as *u8)
4065 cw_appn(page, pos, CW_PAGECAP, kindcnt[kc0] - CW_KINDSHOW)
4066 cw_apps(page, pos, CW_PAGECAP, " more (search filters by kind)</span>" as *u8)
4067 }
4068 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4069 kc0 = kc0 + 1
4070 }
4071 cw_foot(page, pos, CW_PAGECAP)
4072 cw_outpath(opath, outdir, "kinds.html" as *u8)
4073 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4074 if pos[1] == 1 { truncs = truncs + 1 }
4075
4076 // ===== THE FINDINGS ENGINE: detectors -> the work backlog =====
4077 // Findings accumulate into fbuf as nx_debt-grammar rows with STABLE descs;
4078 // the exposure page + census contribute theirs; the beat bridge files them
4079 // idempotently. This is what closes the loop: regen surfaces work, the
4080 // wiki page is the gauge, the debt row is the durable pointer.
4081 let fbuf: *u8 = sys_mmap(CW_FINDCAP)
4082 let fpos: *i64 = sys_mmap(32) as *i64
4083 fpos[0] = 0
4084 fpos[1] = 0
4085
4086 // census-derived findings (thresholds declared; descs count-free)
4087 if st[18] > 0 {
4088 cw_fnd(fbuf, fpos, 4, "codewiki" as *u8,
4089 "Orphan libraries exist (modules with no main and zero importers = dead-code candidates). Live list: https://nishifamily.com/code/kinds -- triage each: wire a caller, or retire (rule 13)." as *u8)
4090 }
4091 // import graph should be a DAG; a cycle is an architecture tangle
4092 if st[9] != 0 {
4093 cw_fnd(fbuf, fpos, 6, "codewiki" as *u8,
4094 "The import graph has a CYCLE (should be a DAG). https://nishifamily.com/code/arch -- break the tangle; sovereign layering flows one way toward nx_syscalls.")
4095 }
4096 // unresolved imports = a module names an import not present in this tree
4097 if st[6] > 0 {
4098 cw_fnd(fbuf, fpos, 4, "codewiki" as *u8,
4099 "Unresolved import targets exist (a module imports a name not found under this root -- generated, drifted, or dead imports). https://nishifamily.com/code/index -- reconcile or remove." as *u8)
4100 }
4101
4102 // ===== pass 3h: exposure page (MCP/API coverage join) =====
4103 pos[0] = 0
4104 pos[1] = 0
4105 cw_head3(page, pos, CW_PAGECAP, "exposure" as *u8, 8, sbbuf)
4106 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "exposure" as *u8, 8)
4107 cw_apps(page, pos, CW_PAGECAP, "<h1>exposure: where MCP and APIs are (and are not)</h1>\n<p class='muted'>the wiki's module+kind table joined with the live allowlist and the deployed elf set. The dark-capability section IS the where-we-need-MCP list, and it feeds the work backlog automatically.</p>\n" as *u8)
4108 let darkbox: *i64 = sys_mmap(16)
4109 darkbox[0] = 0
4110 let nexp2: i64 = cw_exposure(page, pos, CW_PAGECAP, g, pathoff, kindv, sidx, nsorted, noext,
4111 "tool_allowlist.conf" as *u8, "." as *u8, darkbox, fbuf, fpos, srcmt)
4112 st[19] = nexp2
4113 st[20] = darkbox[0]
4114 cw_foot(page, pos, CW_PAGECAP)
4115 cw_outpath(opath, outdir, "exposure.html" as *u8)
4116 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4117 if pos[1] == 1 { truncs = truncs + 1 }
4118
4119 // ===== pass 3dbt: the DEBT TAXONOMY -- every category, module-cited =====
4120 // Counts modules carrying each category's markers (from the pass-1 scan),
4121 // lists examples, and files ONE finding per non-empty category. This is the
4122 // "cover all the categories" surface: landmines, workarounds, gotchas,
4123 // todos, third-party, security, perf, deprecated -- each a backlog row.
4124 pos[0] = 0
4125 pos[1] = 0
4126 cw_head3(page, pos, CW_PAGECAP, "debt" as *u8, 4, sbbuf)
4127 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "debt" as *u8, 4)
4128 cw_apps(page, pos, CW_PAGECAP, "<h1>debt taxonomy</h1>\n<p class='muted'>every module is scanned for the debt-category markers it carries in its own comments -- the tree already documents its hazards; this makes them countable and backlog-fed. Each non-empty category files one finding.</p>\n" as *u8)
4129 var cat0: i64 = 0
4130 while cat0 < CW_NCAT {
4131 let bit: i64 = 1
4132 var shift: i64 = 0
4133 var bmask: i64 = 1
4134 while shift < cat0 { bmask = bmask * 2; shift = shift + 1 }
4135 var ccnt: i64 = 0
4136 var cq: i64 = 0
4137 while cq < nsorted { let cvv: i64 = sidx[cq]; if (catmask[cvv] & bmask) != 0 { ccnt = ccnt + 1 } cq = cq + 1 }
4138 cw_apps(page, pos, CW_PAGECAP, "<h2>" as *u8)
4139 cw_apps(page, pos, CW_PAGECAP, cw_catname(cat0))
4140 cw_apps(page, pos, CW_PAGECAP, " <span class='cnt'>" as *u8)
4141 cw_appn(page, pos, CW_PAGECAP, ccnt)
4142 cw_apps(page, pos, CW_PAGECAP, " modules · sev " as *u8)
4143 cw_appn(page, pos, CW_PAGECAP, cw_catsev(cat0))
4144 cw_apps(page, pos, CW_PAGECAP, " · first " as *u8)
4145 cw_appn(page, pos, CW_PAGECAP, CW_CATSHOW)
4146 cw_apps(page, pos, CW_PAGECAP, " shown</span></h2>\n<p>" as *u8)
4147 var csh: i64 = 0
4148 var cq2: i64 = 0
4149 while cq2 < nsorted {
4150 let cvv2: i64 = sidx[cq2]
4151 if (catmask[cvv2] & bmask) != 0 { if csh < CW_CATSHOW {
4152 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
4153 cw_applink2(page, pos, CW_PAGECAP, ((g.arena as i64) + g.node_off[cvv2]) as *u8, noext)
4154 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
4155 csh = csh + 1
4156 } }
4157 cq2 = cq2 + 1
4158 }
4159 if ccnt == 0 { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none detected</span>" as *u8) }
4160 if ccnt > CW_CATSHOW { cw_apps(page, pos, CW_PAGECAP, " <span class='muted'>+" as *u8); cw_appn(page, pos, CW_PAGECAP, ccnt - CW_CATSHOW); cw_apps(page, pos, CW_PAGECAP, " more</span>" as *u8) }
4161 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4162 // one aggregate finding per non-empty category (count-free desc)
4163 if ccnt > 0 {
4164 cw_fnd(fbuf, fpos, cw_catsev(cat0), "codewiki-debt" as *u8, cw_catdesc(cat0))
4165 }
4166 // TARGETED rows for the SEVERE categories: name the individual modules
4167 // with the largest blast radius so each is a pickable work item. The
4168 // aggregate row above still covers the tail; these are the ones to do
4169 // first, chosen by measurement (fan-in), not by taste.
4170 if cw_catsev(cat0) >= CW_TGTSEV { if ccnt > 0 {
4171 cw_apps(page, pos, CW_PAGECAP, "<p class='muted'>filed individually (top " as *u8)
4172 cw_appn(page, pos, CW_PAGECAP, CW_TGTCAP)
4173 cw_apps(page, pos, CW_PAGECAP, " by blast radius): " as *u8)
4174 let tgk: *i64 = sys_mmap((CW_TGTCAP + 2) * 8) as *i64
4175 var ntg: i64 = 0
4176 var tg0: i64 = 0
4177 while tg0 < CW_TGTCAP {
4178 var tbest: i64 = 0 - 1
4179 var tg1: i64 = 0
4180 while tg1 < nsorted {
4181 let tvv: i64 = sidx[tg1]
4182 if (catmask[tvv] & bmask) != 0 {
4183 var taken: i64 = 0
4184 var tg2: i64 = 0
4185 while tg2 < ntg { if tgk[tg2] == tvv { taken = 1 } tg2 = tg2 + 1 }
4186 if taken == 0 { if tbest < 0 { tbest = tvv } else { if g.ca_arr[tvv] > g.ca_arr[tbest] { tbest = tvv } } }
4187 }
4188 tg1 = tg1 + 1
4189 }
4190 if tbest >= 0 { if g.ca_arr[tbest] > 0 {
4191 tgk[ntg] = tbest
4192 ntg = ntg + 1
4193 let tnm2: *u8 = ((g.arena as i64) + g.node_off[tbest]) as *u8
4194 cw_fnd_mod(fbuf, fpos, cw_catsev(cat0), cw_catname(cat0), tnm2, cw_stemlen(tnm2))
4195 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
4196 cw_applink2(page, pos, CW_PAGECAP, tnm2, noext)
4197 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
4198 } }
4199 tg0 = tg0 + 1
4200 }
4201 if ntg == 0 { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none with importers</span>" as *u8) }
4202 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4203 } }
4204 cat0 = cat0 + 1
4205 }
4206 // taste-bound gauge (bounds law 2026-07-29, seq1242): the 07-29 audit
4207 // found naked bounds the MAJORITY tree-wide; this section re-measures at
4208 // every regen so the law has a standing gauge, not a one-off sweep.
4209 var tbcnt: i64 = 0
4210 var tbmods: i64 = 0
4211 var tb0: i64 = 0
4212 while tb0 < nsorted {
4213 let tbv: i64 = sidx[tb0]
4214 if boundv[tbv] > 0 { tbmods = tbmods + 1; tbcnt = tbcnt + boundv[tbv] }
4215 tb0 = tb0 + 1
4216 }
4217 cw_apps(page, pos, CW_PAGECAP, "<h2>taste-bound consts <span class='cnt'>" as *u8)
4218 cw_appn(page, pos, CW_PAGECAP, tbcnt)
4219 cw_apps(page, pos, CW_PAGECAP, " caps in " as *u8)
4220 cw_appn(page, pos, CW_PAGECAP, tbmods)
4221 cw_apps(page, pos, CW_PAGECAP, " modules · goal FALLING · first " as *u8)
4222 cw_appn(page, pos, CW_PAGECAP, CW_CATSHOW)
4223 cw_apps(page, pos, CW_PAGECAP, " shown</span></h2>\n<p class='muted'>consts named *CAP/*MAX (value >= 16) whose own line or the line above carries NO measurement provenance (markers: measured / derived / sized / budget / today / RFC / JPL / ==). The bounds law: derived and adaptive, never taste — and when a bound bites, the fix is architecture, not a different number.</p>\n<p>" as *u8)
4224 var tbs: i64 = 0
4225 var tb1: i64 = 0
4226 while tb1 < nsorted {
4227 let tbw: i64 = sidx[tb1]
4228 if boundv[tbw] > 0 { if tbs < CW_CATSHOW {
4229 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
4230 cw_applink2(page, pos, CW_PAGECAP, ((g.arena as i64) + g.node_off[tbw]) as *u8, noext)
4231 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
4232 tbs = tbs + 1
4233 } }
4234 tb1 = tb1 + 1
4235 }
4236 if tbmods == 0 { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none detected</span>" as *u8) }
4237 if tbmods > CW_CATSHOW { cw_apps(page, pos, CW_PAGECAP, " <span class='muted'>+" as *u8); cw_appn(page, pos, CW_PAGECAP, tbmods - CW_CATSHOW); cw_apps(page, pos, CW_PAGECAP, " more</span>" as *u8) }
4238 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4239 if tbmods > 0 {
4240 cw_fnd(fbuf, fpos, 5, "bounds-law/tree" as *u8, "Taste-bound consts present: cap/max constants lack measurement provenance in the adjacent comment; apply the bounds law (derive from a measured table or scale with the input; when a bound bites the fix is architecture, never a different number). Standing gauge: https://nishifamily.com/code/debt" as *u8)
4241 }
4242 // substring-classifier gauge (2026-07-30): the class that produced four
4243 // separate defects in one day, now a standing measurement instead of a
4244 // lesson somebody has to remember.
4245 var sccnt: i64 = 0
4246 var scmods: i64 = 0
4247 var sc0: i64 = 0
4248 while sc0 < nsorted {
4249 let scv: i64 = sidx[sc0]
4250 if subclv[scv] > 0 { scmods = scmods + 1; sccnt = sccnt + subclv[scv] }
4251 sc0 = sc0 + 1
4252 }
4253 cw_apps(page, pos, CW_PAGECAP, "<h2>substring classifiers <span class='cnt' data-subcl='" as *u8)
4254 cw_appn(page, pos, CW_PAGECAP, sccnt)
4255 cw_apps(page, pos, CW_PAGECAP, "'>" as *u8)
4256 cw_appn(page, pos, CW_PAGECAP, sccnt)
4257 cw_apps(page, pos, CW_PAGECAP, " decisions in " as *u8)
4258 cw_appn(page, pos, CW_PAGECAP, scmods)
4259 cw_apps(page, pos, CW_PAGECAP, " modules · goal FALLING · first " as *u8)
4260 cw_appn(page, pos, CW_PAGECAP, CW_CATSHOW)
4261 cw_apps(page, pos, CW_PAGECAP, " shown</span></h2>\n<p class='muted'>lines that both TEST a substring and DECIDE on it (a contains/hastok call, a quoted literal, and a verdict on the same line). <b>A substring standing in for a property</b> cost four separate defects on 2026-07-30 alone, sev 5 to 9: a documented CWE <i>prevention</i> filed as a hazard; the edge SSOT <code>nx_sites_daemon_v2</code> flagged as a stale copy because its name carries <code>_v2</code>; the one-shot worker <code>nx_torrent_get</code> classified a daemon by the substring <code>torrent</code>, leaving it unshippable by any sanctioned verb; and route matching reading BODY bytes as a request line. A needle cannot tell a thing from a MENTION of a thing. Locating a substring is fine — classifying by one is the smell; read the structure instead (parse the field, declare the role in data, bound the scan).</p>\n<p>" as *u8)
4262 var scs: i64 = 0
4263 var sc1: i64 = 0
4264 while sc1 < nsorted {
4265 let scw: i64 = sidx[sc1]
4266 if subclv[scw] > 0 { if scs < CW_CATSHOW {
4267 cw_apps(page, pos, CW_PAGECAP, "<span class='pill'>" as *u8)
4268 cw_applink2(page, pos, CW_PAGECAP, ((g.arena as i64) + g.node_off[scw]) as *u8, noext)
4269 cw_apps(page, pos, CW_PAGECAP, "</span>" as *u8)
4270 scs = scs + 1
4271 } }
4272 sc1 = sc1 + 1
4273 }
4274 if scmods == 0 { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none detected</span>" as *u8) }
4275 if scmods > CW_CATSHOW { cw_apps(page, pos, CW_PAGECAP, " <span class='muted'>+" as *u8); cw_appn(page, pos, CW_PAGECAP, scmods - CW_CATSHOW); cw_apps(page, pos, CW_PAGECAP, " more</span>" as *u8) }
4276 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4277 if scmods > 0 {
4278 cw_fnd(fbuf, fpos, 5, "substring-classifier/tree" as *u8, "Substring classifiers present: a line that tests a substring AND decides on it. A needle cannot distinguish a thing from a mention of a thing -- this exact shape produced four defects (sev 5-9) on 2026-07-30: a documented CWE prevention filed as a hazard, the edge SSOT flagged as a stale copy for carrying _v2, a one-shot worker classified a daemon by name and left unshippable, and route matching reading body bytes as a request line. Read the structure instead: parse the field, declare the role in data, bound the scan. Standing gauge: https://nishifamily.com/code/debt" as *u8)
4279 }
4280 cw_apps(page, pos, CW_PAGECAP, "<p class='muted'>markers are conservative substrings (landmine/footgun/dual-copy, workaround/hack/kludge, gotcha/trap/caveat, todo/fixme, third-party/foreign, cwe-/vuln/exploit, quadratic/O(n^2), deprecated/retire). A module can appear under several categories.</p>\n" as *u8)
4281 cw_foot(page, pos, CW_PAGECAP)
4282 cw_outpath(opath, outdir, "debt.html" as *u8)
4283 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4284 if pos[1] == 1 { truncs = truncs + 1 }
4285
4286 // ===== pass 3k: sovereignty ladder -- from the first byte up =====
4287 // Each rung is a layer of the stack; "sovereign" = built first-byte-up in
4288 // NishiLang with no foreign dependency. We MEASURE the tree's coverage of
4289 // each rung (modules whose family/name matches the rung's markers) and
4290 // FILE a finding for any rung that is a known gap -- the ladder itself
4291 // becomes backlog. HARDWARE is the lowest rung by doctrine (rule 26).
4292 pos[0] = 0
4293 pos[1] = 0
4294 cw_head3(page, pos, CW_PAGECAP, "sovereignty" as *u8, 11, sbbuf)
4295 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "sovereignty" as *u8, 11)
4296 cw_apps(page, pos, CW_PAGECAP, "<h1>the sovereignty ladder</h1>\n<p class='muted'>from the first byte up. Sovereign = built in NishiLang with no foreign dependency. Each rung shows the modules in this tree that implement it; a thin rung is where sovereignty is still owed, and it feeds the backlog.</p>\n<table><tr><th>rung</th><th class='n'>modules here</th><th>representative</th><th>status</th></tr>\n" as *u8)
4297 // rung markers: name-substring that identifies a rung's modules
4298 let RUNGN: i64 = 10
4299 let rname: *i64 = sys_mmap(RUNGN * 8) as *i64
4300 let rmark: *i64 = sys_mmap(RUNGN * 8) as *i64
4301 rname[0] = "hardware / firmware (CMOS, POST, never-brick)" as *u8 as i64; rmark[0] = "firmware" as *u8 as i64
4302 rname[1] = "boot / kernel / syscalls" as *u8 as i64; rmark[1] = "syscall" as *u8 as i64
4303 rname[2] = "compiler / assembler (nx_cc, nxasm)" as *u8 as i64; rmark[2] = "nxasm" as *u8 as i64
4304 rname[3] = "TLS / crypto" as *u8 as i64; rmark[3] = "tls" as *u8 as i64
4305 rname[4] = "HTTP / web edge" as *u8 as i64; rmark[4] = "http" as *u8 as i64
4306 rname[5] = "storage / data plane" as *u8 as i64; rmark[5] = "store" as *u8 as i64
4307 rname[6] = "model / inference" as *u8 as i64; rmark[6] = "llm" as *u8 as i64
4308 rname[7] = "graphics / video" as *u8 as i64; rmark[7] = "vcodec" as *u8 as i64
4309 rname[8] = "browser" as *u8 as i64; rmark[8] = "browser" as *u8 as i64
4310 rname[9] = "management / deploy" as *u8 as i64; rmark[9] = "mgmt" as *u8 as i64
4311 var ru: i64 = 0
4312 while ru < RUNGN {
4313 let mk: *u8 = rmark[ru] as *u8
4314 var cnt: i64 = 0
4315 var rep: i64 = 0 - 1
4316 var rq: i64 = 0
4317 while rq < nsorted {
4318 let rvv: i64 = sidx[rq]
4319 let rnm: *u8 = ((g.arena as i64) + g.node_off[rvv]) as *u8
4320 if cw_namehas(rnm, mk) == 1 {
4321 cnt = cnt + 1
4322 if rep < 0 { rep = rvv } else { if g.ca_arr[rvv] > g.ca_arr[rep] { rep = rvv } }
4323 }
4324 rq = rq + 1
4325 }
4326 cw_apps(page, pos, CW_PAGECAP, "<tr><td>" as *u8)
4327 cw_apps(page, pos, CW_PAGECAP, rname[ru] as *u8)
4328 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
4329 cw_appn(page, pos, CW_PAGECAP, cnt)
4330 cw_apps(page, pos, CW_PAGECAP, "</td><td>" as *u8)
4331 if rep >= 0 { cw_applink2(page, pos, CW_PAGECAP, ((g.arena as i64) + g.node_off[rep]) as *u8, noext) } else { cw_apps(page, pos, CW_PAGECAP, "<span class='muted'>none</span>" as *u8) }
4332 cw_apps(page, pos, CW_PAGECAP, "</td><td>" as *u8)
4333 if cnt == 0 {
4334 cw_apps(page, pos, CW_PAGECAP, "<b style='color:var(--acc2)'>gap — sovereignty owed</b>" as *u8)
4335 } else {
4336 cw_apps(page, pos, CW_PAGECAP, "sovereign (" as *u8)
4337 cw_appn(page, pos, CW_PAGECAP, cnt)
4338 cw_apps(page, pos, CW_PAGECAP, " modules)" as *u8)
4339 }
4340 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4341 // the hardware rung is doctrine-critical: file it if thin
4342 if ru == 0 { if cnt == 0 {
4343 cw_fnd(fbuf, fpos, 7, "sovereignty" as *u8,
4344 "The hardware/firmware rung shows no modules in the tree -- the first-byte-up hardware sovereignty (rule 26, CMOS/POST/never-brick) is not yet represented as measurable organs. https://nishifamily.com/code/sovereignty" as *u8)
4345 } }
4346 ru = ru + 1
4347 }
4348 cw_apps(page, pos, CW_PAGECAP, "</table>\n<p class='muted'>the goal is every rung sovereign from the first byte up; a gap here is a program, not a bug -- but it is TRACKED here so it is never invisible.</p>\n" as *u8)
4349 cw_foot(page, pos, CW_PAGECAP)
4350 cw_outpath(opath, outdir, "sovereignty.html" as *u8)
4351 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4352 if pos[1] == 1 { truncs = truncs + 1 }
4353
4354 // ===== sotaplan.html: the SOTA ladder -- KEEPS THE SIDEBAR PROMISE =====
4355 // The sidebar has linked 'sotaplan' since rung 23 with NO emission behind
4356 // it, so every generated page carried a dead link. A SIDEBAR LINK IS A
4357 // PROMISE THE GENERATOR MUST KEEP; a link without an emission is a 404
4358 // multiplied by the page count. Content is deliberately POINTER-ONLY: the
4359 // measured inputs already live on findings/debt/atlas/sovereignty/exposure,
4360 // and restating a number here would create a second source of truth that
4361 // drifts (the publishing-plane law).
4362 pos[0] = 0
4363 pos[1] = 0
4364 cw_head3(page, pos, CW_PAGECAP, "sotaplan" as *u8, 8, sbbuf)
4365 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "sotaplan" as *u8, 8)
4366 cw_apps(page, pos, CW_PAGECAP, "<h1>the SOTA plan</h1>\n<p class='muted'>What state of the art means here, and where the ecosystem measurably is not there yet. Every input below is measured elsewhere in this wiki and LINKED, never restated -- a number copied here would be a second source of truth that drifts.</p>\n<table><tr><th>rung</th><th>the measured input</th></tr>\n<tr><td>Open work, ranked</td><td><a href='findings'>findings</a> -- the backlog the generator files against itself</td></tr>\n<tr><td>Debt by category</td><td><a href='debt'>debt taxonomy</a> -- landmines, workarounds, gotchas, third-party, security, perf</td></tr>\n<tr><td>Organisation</td><td><a href='atlas'>atlas</a> -- family to domain conformance; extend the conf, never the generator</td></tr>\n<tr><td>First-byte-up sovereignty</td><td><a href='sovereignty'>sovereignty ladder</a> -- a thin rung is sovereignty owed</td></tr>\n<tr><td>Reach</td><td><a href='exposure'>exposure</a> -- what is callable over MCP/API versus deployed but dark</td></tr>\n<tr><td>External frontier</td><td><a href='research'>research briefs</a> -- sourced, dated SOTA censuses with declared gaps</td></tr>\n</table>\n<p class='muted'>The plan is the join of those six: what is broken (findings, debt), what is disorganised (atlas), what is not ours yet (sovereignty), what nobody can reach (exposure), and where the outside world actually is (research).</p>\n" as *u8)
4367 cw_foot(page, pos, CW_PAGECAP)
4368 cw_outpath(opath, outdir, "sotaplan.html" as *u8)
4369 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4370 if pos[1] == 1 { truncs = truncs + 1 }
4371
4372 // ===== atlas.html: the family -> domain conformance ruler (seq1220) =====
4373 pos[0] = 0
4374 pos[1] = 0
4375 cw_head3(page, pos, CW_PAGECAP, "atlas" as *u8, 5, sbbuf)
4376 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "atlas" as *u8, 5)
4377 cw_apps(page, pos, CW_PAGECAP, "<h1>atlas: families mapped to ecosystem domains</h1>\n<p class='muted'>the tree's naming discipline (nx_<family>_*) joined to the DECLARED family-to-domain mapping conf (knowledge/codewiki_atlas.conf). Extend the conf to raise conformance -- never rebuild the generator (rule 11). Unmapped families are the work list, biggest LOC first; stale rows are declared, never dropped silently.</p>\n" as *u8)
4378 cw_atlas(page, pos, CW_PAGECAP, famarena, famoff, famlen, famcnt, famloc, nfam, atlaspath, noext, fbuf, fpos)
4379 cw_foot(page, pos, CW_PAGECAP)
4380 cw_outpath(opath, outdir, "atlas.html" as *u8)
4381 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4382 if pos[1] == 1 { truncs = truncs + 1 }
4383
4384 // ===== findings.tsv + findings.html: emitted LAST so every detector (census,
4385 // exposure, sovereignty) has contributed. findings.tsv IS the backlog feed
4386 // (exactly the rows the debt bridge files); findings.html is its human view.
4387 var nfind: i64 = 0
4388 var nfi: i64 = 0
4389 while nfi < fpos[0] { if fbuf[nfi] == (10 as u8) { nfind = nfind + 1 } nfi = nfi + 1 }
4390 st[21] = nfind
4391 cw_outpath(opath, outdir, "findings.tsv" as *u8)
4392 if cw_flush(opath, fbuf, fpos, CW_FINDCAP) == 0 { pages = pages + 1 }
4393
4394 pos[0] = 0
4395 pos[1] = 0
4396 cw_head3(page, pos, CW_PAGECAP, "findings" as *u8, 8, sbbuf)
4397 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "findings" as *u8, 8)
4398 cw_apps(page, pos, CW_PAGECAP, "<h1>findings: the wiki's own work backlog</h1>\n<p class='muted'>each regeneration runs the detectors; every finding is filed into the debt board with a stable description (idempotent -- refiling the same finding is a no-op). Fix the issue and the next regen drops the finding. This is the loop: code → wiki → findings → backlog → work → code.</p>\n<table><tr><th class='n'>sev</th><th>scope</th><th>finding</th></tr>\n" as *u8)
4399 var fls: i64 = 0
4400 while fls < fpos[0] {
4401 var fle: i64 = fls
4402 var fg: i64 = 1
4403 while fg == 1 { if fle >= fpos[0] { fg = 0 } else { if fbuf[fle] == (10 as u8) { fg = 0 } else { fle = fle + 1 } } }
4404 if fle > fls {
4405 let s1: i64 = cw_fld(fbuf, fls, fle)
4406 let s2s: i64 = s1 + 1
4407 let s2e: i64 = cw_fld(fbuf, s2s, fle)
4408 cw_apps(page, pos, CW_PAGECAP, "<tr><td class='n'>" as *u8)
4409 cw_appe(page, pos, CW_PAGECAP, ((fbuf as i64) + fls) as *u8, s1 - fls)
4410 cw_apps(page, pos, CW_PAGECAP, "</td><td class='muted'>" as *u8)
4411 cw_appe(page, pos, CW_PAGECAP, ((fbuf as i64) + s2s) as *u8, s2e - s2s)
4412 cw_apps(page, pos, CW_PAGECAP, "</td><td>" as *u8)
4413 if s2e + 1 < fle { cw_appe(page, pos, CW_PAGECAP, ((fbuf as i64) + s2e + 1) as *u8, fle - s2e - 1) }
4414 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4415 }
4416 fls = fle + 1
4417 }
4418 if nfind == 0 { cw_apps(page, pos, CW_PAGECAP, "<tr><td colspan='3' class='muted'>no open findings -- the detectors are all green</td></tr>" as *u8) }
4419 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4420 cw_foot(page, pos, CW_PAGECAP)
4421 cw_outpath(opath, outdir, "findings.html" as *u8)
4422 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4423 if pos[1] == 1 { truncs = truncs + 1 }
4424
4425 // ===== pass 3b: index =====
4426 pos[0] = 0
4427 pos[1] = 0
4428 cw_head3(page, pos, CW_PAGECAP, "index" as *u8, 5, sbbuf)
4429 cw_apps(page, pos, CW_PAGECAP, "<h1>nishi code wiki</h1>\n<p class='muted'>generated from the source tree; every number below is measured, not asserted.</p>\n<p><a class='cta' href='search" as *u8)
4430 cw_appext(page, pos, CW_PAGECAP, noext)
4431 cw_apps(page, pos, CW_PAGECAP, "'>🔍 Search " as *u8)
4432 cw_appn(page, pos, CW_PAGECAP, st[12])
4433 cw_apps(page, pos, CW_PAGECAP, " functions</a> <a class='cta' style='background:var(--acc2)' href='topics" as *u8)
4434 cw_appext(page, pos, CW_PAGECAP, noext)
4435 cw_apps(page, pos, CW_PAGECAP, "'>📚 Browse topics</a> <a class='cta' href='ask" as *u8)
4436 cw_appext(page, pos, CW_PAGECAP, noext)
4437 cw_apps(page, pos, CW_PAGECAP, "'>💬 Ask (local AI)</a></p>\n<div class='tiles'>" as *u8)
4438 cw_apps(page, pos, CW_PAGECAP, "<div class='tile'><div class='v'>" as *u8)
4439 cw_appn(page, pos, CW_PAGECAP, st[0])
4440 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>modules</div></div><div class='tile'><div class='v'>" as *u8)
4441 cw_appn(page, pos, CW_PAGECAP, st[2])
4442 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>import edges</div></div><div class='tile'><div class='v'>" as *u8)
4443 cw_appn(page, pos, CW_PAGECAP, st[3])
4444 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>functions</div></div><div class='tile'><div class='v'>" as *u8)
4445 cw_appn(page, pos, CW_PAGECAP, st[4])
4446 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>structs</div></div><div class='tile'><div class='v'>" as *u8)
4447 cw_appn(page, pos, CW_PAGECAP, st[5])
4448 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>consts</div></div><div class='tile'><div class='v'>" as *u8)
4449 if st[9] == 0 { cw_apps(page, pos, CW_PAGECAP, "DAG" as *u8) } else { cw_apps(page, pos, CW_PAGECAP, "CYCLE" as *u8) }
4450 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>import graph</div></div></div>\n<p class='muted'>unresolved import targets: " as *u8)
4451 cw_appn(page, pos, CW_PAGECAP, st[6])
4452 cw_apps(page, pos, CW_PAGECAP, " · tree stamp (content-derived): " as *u8)
4453 cw_appn(page, pos, CW_PAGECAP, st[8])
4454 cw_apps(page, pos, CW_PAGECAP, "</p>\n" as *u8)
4455 cw_nar_include(page, pos, CW_PAGECAP, outdir, opath, "index" as *u8)
4456
4457 cw_apps(page, pos, CW_PAGECAP, "<h2>directories</h2>\n<table>" as *u8)
4458 var d4: i64 = 0
4459 while d4 < ndirs {
4460 let dl2: *u8 = ((dirarena as i64) + dirnoff[d4]) as *u8
4461 let sl2: *u8 = ((dirarena as i64) + dirslug[d4]) as *u8
4462 cw_apps(page, pos, CW_PAGECAP, "<tr><td><a href='" as *u8)
4463 cw_apps(page, pos, CW_PAGECAP, sl2)
4464 cw_appext(page, pos, CW_PAGECAP, noext)
4465 cw_apps(page, pos, CW_PAGECAP, "'>" as *u8)
4466 if cw_slen(dl2) == 0 { cw_apps(page, pos, CW_PAGECAP, "(root)" as *u8) } else { cw_appe(page, pos, CW_PAGECAP, dl2, cw_slen(dl2)) }
4467 cw_apps(page, pos, CW_PAGECAP, "</a></td><td class='n'>" as *u8)
4468 cw_appn(page, pos, CW_PAGECAP, dircnt[d4])
4469 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4470 d4 = d4 + 1
4471 }
4472 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4473
4474 // hubs: top-K by fan-in among file nodes, then top-K by fan-out
4475 let hk: *i64 = sys_mmap((CW_TOPK + 2) * 8) as *i64
4476 let hv: *i64 = sys_mmap((CW_TOPK + 2) * 8) as *i64
4477 var hn: i64 = 0
4478 var s4i: i64 = 0
4479 while s4i < nsorted {
4480 let v4: i64 = sidx[s4i]
4481 if pathoff[v4] > 0 {
4482 let ca: i64 = g.ca_arr[v4]
4483 var ins: i64 = 0 - 1
4484 if hn < CW_TOPK { ins = hn; hk[hn] = v4; hv[hn] = ca; hn = hn + 1 }
4485 if ins < 0 { if ca > hv[CW_TOPK - 1] { ins = CW_TOPK - 1; hk[ins] = v4; hv[ins] = ca } }
4486 var q3: i64 = ins
4487 while q3 > 0 {
4488 if hv[q3 - 1] < hv[q3] {
4489 let t5: i64 = hv[q3-1]; hv[q3-1] = hv[q3]; hv[q3] = t5
4490 let t6: i64 = hk[q3-1]; hk[q3-1] = hk[q3]; hk[q3] = t6
4491 q3 = q3 - 1
4492 } else { q3 = 0 }
4493 }
4494 }
4495 s4i = s4i + 1
4496 }
4497 cw_apps(page, pos, CW_PAGECAP, "<h2>most imported (load-bearing modules, top " as *u8)
4498 cw_appn(page, pos, CW_PAGECAP, CW_TOPK)
4499 cw_apps(page, pos, CW_PAGECAP, " declared)</h2>\n<table><tr><th>module</th><th class='n'>importers</th></tr>\n" as *u8)
4500 var r1: i64 = 0
4501 while r1 < hn {
4502 if hv[r1] > 0 {
4503 let hnm: *u8 = ((g.arena as i64) + g.node_off[hk[r1]]) as *u8
4504 cw_apps(page, pos, CW_PAGECAP, "<tr><td>" as *u8)
4505 cw_applink2(page, pos, CW_PAGECAP, hnm, noext)
4506 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
4507 cw_appn(page, pos, CW_PAGECAP, hv[r1])
4508 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4509 }
4510 r1 = r1 + 1
4511 }
4512 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4513
4514 var hn2: i64 = 0
4515 var s5i: i64 = 0
4516 while s5i < nsorted {
4517 let v5: i64 = sidx[s5i]
4518 if pathoff[v5] > 0 {
4519 let ce: i64 = g.ce_arr[v5]
4520 var ins2: i64 = 0 - 1
4521 if hn2 < CW_TOPK { ins2 = hn2; hk[hn2] = v5; hv[hn2] = ce; hn2 = hn2 + 1 }
4522 if ins2 < 0 { if ce > hv[CW_TOPK - 1] { ins2 = CW_TOPK - 1; hk[ins2] = v5; hv[ins2] = ce } }
4523 var q4: i64 = ins2
4524 while q4 > 0 {
4525 if hv[q4 - 1] < hv[q4] {
4526 let t7: i64 = hv[q4-1]; hv[q4-1] = hv[q4]; hv[q4] = t7
4527 let t8: i64 = hk[q4-1]; hk[q4-1] = hk[q4]; hk[q4] = t8
4528 q4 = q4 - 1
4529 } else { q4 = 0 }
4530 }
4531 }
4532 s5i = s5i + 1
4533 }
4534 cw_apps(page, pos, CW_PAGECAP, "<h2>widest importers (composition roots, top " as *u8)
4535 cw_appn(page, pos, CW_PAGECAP, CW_TOPK)
4536 cw_apps(page, pos, CW_PAGECAP, " declared)</h2>\n<table><tr><th>module</th><th class='n'>imports</th></tr>\n" as *u8)
4537 var r2: i64 = 0
4538 while r2 < hn2 {
4539 if hv[r2] > 0 {
4540 let hnm2: *u8 = ((g.arena as i64) + g.node_off[hk[r2]]) as *u8
4541 cw_apps(page, pos, CW_PAGECAP, "<tr><td>" as *u8)
4542 cw_applink2(page, pos, CW_PAGECAP, hnm2, noext)
4543 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
4544 cw_appn(page, pos, CW_PAGECAP, hv[r2])
4545 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4546 }
4547 r2 = r2 + 1
4548 }
4549 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4550
4551 // unresolved import targets (honesty section)
4552 if st[6] > 0 {
4553 cw_apps(page, pos, CW_PAGECAP, "<h2>unresolved import targets (first 20)</h2>\n<p class='muted'>imported by modules in this tree but not found under this root -- generated files, other trees, or genuine dead imports.</p>\n<table><tr><th>name</th><th class='n'>importers</th></tr>\n" as *u8)
4554 var shown2: i64 = 0
4555 var u1: i64 = 0
4556 while u1 < g.node_count {
4557 if pathoff[u1] == 0 { if shown2 < 20 {
4558 let unm: *u8 = ((g.arena as i64) + g.node_off[u1]) as *u8
4559 cw_apps(page, pos, CW_PAGECAP, "<tr><td class='ext'>" as *u8)
4560 cw_appe(page, pos, CW_PAGECAP, unm, cw_slen(unm))
4561 cw_apps(page, pos, CW_PAGECAP, "</td><td class='n'>" as *u8)
4562 cw_appn(page, pos, CW_PAGECAP, g.ca_arr[u1])
4563 cw_apps(page, pos, CW_PAGECAP, "</td></tr>\n" as *u8)
4564 shown2 = shown2 + 1
4565 } }
4566 u1 = u1 + 1
4567 }
4568 cw_apps(page, pos, CW_PAGECAP, "</table>\n" as *u8)
4569 }
4570
4571 cw_foot(page, pos, CW_PAGECAP)
4572 var w3: i64 = 0
4573 var i6: i64 = 0
4574 while outdir[i6] != (0 as u8) { opath[w3] = outdir[i6]; w3 = w3 + 1; i6 = i6 + 1 }
4575 opath[w3] = 47 as u8; w3 = w3 + 1
4576 cw_rawcpy(opath, w3, "index.html" as *u8)
4577 w3 = w3 + 10
4578 opath[w3] = 0 as u8
4579 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4580 if pos[1] == 1 { truncs = truncs + 1 }
4581
4582 // ===== pass 3c: search indexes (modindex.js + fnindex.js) =====
4583 // Client-side symbol search: no daemon, no third-party lib -- two generated
4584 // JS arrays a plain page filters. Sizes declared on the search page.
4585 pos[0] = 0
4586 pos[1] = 0
4587 cw_apps(page, pos, CW_PAGECAP, "var MODS=[" as *u8)
4588 var m1: i64 = 0
4589 while m1 < nsorted {
4590 let mv: i64 = sidx[m1]
4591 let mnm: *u8 = ((g.arena as i64) + g.node_off[mv]) as *u8
4592 cw_apps(page, pos, CW_PAGECAP, "[\"" as *u8)
4593 cw_appstem(page, pos, CW_PAGECAP, mnm)
4594 cw_apps(page, pos, CW_PAGECAP, "\",\"" as *u8)
4595 let mdl: *u8 = ((dirarena as i64) + dirnoff[fdir[mv]]) as *u8
4596 cw_appjse(page, pos, CW_PAGECAP, mdl, cw_slen(mdl))
4597 cw_apps(page, pos, CW_PAGECAP, "\",\"" as *u8)
4598 if dlen[mv] > 0 { cw_appjse(page, pos, CW_PAGECAP, ((descarena as i64) + doff[mv]) as *u8, dlen[mv]) }
4599 cw_apps(page, pos, CW_PAGECAP, "\"," as *u8)
4600 cw_appn(page, pos, CW_PAGECAP, floc[mv])
4601 cw_apps(page, pos, CW_PAGECAP, "," as *u8)
4602 cw_appn(page, pos, CW_PAGECAP, fnfn[mv])
4603 cw_apps(page, pos, CW_PAGECAP, ",\"" as *u8)
4604 cw_apps(page, pos, CW_PAGECAP, cw_kindname(kindv[mv]))
4605 cw_apps(page, pos, CW_PAGECAP, "\"],\n" as *u8)
4606 m1 = m1 + 1
4607 }
4608 cw_apps(page, pos, CW_PAGECAP, "];\n" as *u8)
4609 cw_outpath(opath, outdir, "modindex.js" as *u8)
4610 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4611 if pos[1] == 1 { truncs = truncs + 1 }
4612
4613 pos[0] = 0
4614 pos[1] = 0
4615 cw_apps(page, pos, CW_PAGECAP, "var FNS=[" as *u8)
4616 var f1: i64 = 0
4617 while f1 < nfns {
4618 cw_apps(page, pos, CW_PAGECAP, "[\"" as *u8)
4619 cw_app(page, pos, CW_PAGECAP, ((fnarena as i64) + fnoff[f1]) as *u8, fnlen[f1])
4620 cw_apps(page, pos, CW_PAGECAP, "\",\"" as *u8)
4621 let fmn: *u8 = ((g.arena as i64) + g.node_off[fnmod[f1]]) as *u8
4622 cw_appstem(page, pos, CW_PAGECAP, fmn)
4623 cw_apps(page, pos, CW_PAGECAP, "\"," as *u8)
4624 cw_appn(page, pos, CW_PAGECAP, fnline[f1])
4625 cw_apps(page, pos, CW_PAGECAP, "],\n" as *u8)
4626 f1 = f1 + 1
4627 }
4628 cw_apps(page, pos, CW_PAGECAP, "];\n" as *u8)
4629 cw_outpath(opath, outdir, "fnindex.js" as *u8)
4630 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4631 if pos[1] == 1 { truncs = truncs + 1 }
4632
4633 // askindex.tsv -- the RETRIEVAL corpus (one flat file, one read):
4634 // M <TAB> stem <TAB> dir <TAB> loc <TAB> funcs <TAB> desc
4635 // F <TAB> fname <TAB> modstem <TAB> line
4636 // T <TAB> tool <TAB> schema-title <TAB> argv-grammar (capabilities --
4637 // rung 23, the organ-side half of the discoverability law)
4638 // Tabs/newlines inside a desc are mapped to spaces so the format cannot
4639 // be broken by source text (cw_apptsv).
4640 let abuf: *u8 = sys_mmap(CW_ASKIDXCAP)
4641 let apos: *i64 = sys_mmap(32) as *i64
4642 apos[0] = 0
4643 apos[1] = 0
4644 var arecs: i64 = 0
4645 var a1: i64 = 0
4646 while a1 < nsorted {
4647 let av: i64 = sidx[a1]
4648 let anm: *u8 = ((g.arena as i64) + g.node_off[av]) as *u8
4649 cw_apps(abuf, apos, CW_ASKIDXCAP, "M\t" as *u8)
4650 cw_appstem(abuf, apos, CW_ASKIDXCAP, anm)
4651 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4652 let adl: *u8 = ((dirarena as i64) + dirnoff[fdir[av]]) as *u8
4653 cw_apptsv(abuf, apos, CW_ASKIDXCAP, adl, cw_slen(adl))
4654 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4655 cw_appn(abuf, apos, CW_ASKIDXCAP, floc[av])
4656 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4657 cw_appn(abuf, apos, CW_ASKIDXCAP, fnfn[av])
4658 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4659 if dlen[av] > 0 { cw_apptsv(abuf, apos, CW_ASKIDXCAP, ((descarena as i64) + doff[av]) as *u8, dlen[av]) }
4660 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4661 if sdlen[av] > 0 { cw_apptsv(abuf, apos, CW_ASKIDXCAP, ((sdocarena as i64) + sdoff[av]) as *u8, sdlen[av]) }
4662 cw_apps(abuf, apos, CW_ASKIDXCAP, "\n" as *u8)
4663 arecs = arecs + 1
4664 a1 = a1 + 1
4665 }
4666 var a2: i64 = 0
4667 while a2 < nfns {
4668 cw_apps(abuf, apos, CW_ASKIDXCAP, "F\t" as *u8)
4669 cw_app(abuf, apos, CW_ASKIDXCAP, ((fnarena as i64) + fnoff[a2]) as *u8, fnlen[a2])
4670 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4671 let afm: *u8 = ((g.arena as i64) + g.node_off[fnmod[a2]]) as *u8
4672 cw_appstem(abuf, apos, CW_ASKIDXCAP, afm)
4673 cw_apps(abuf, apos, CW_ASKIDXCAP, "\t" as *u8)
4674 cw_appn(abuf, apos, CW_ASKIDXCAP, fnline[a2])
4675 cw_apps(abuf, apos, CW_ASKIDXCAP, "\n" as *u8)
4676 arecs = arecs + 1
4677 a2 = a2 + 1
4678 }
4679 // T-rows: every registered capability joins the retrieval corpus, so the
4680 // organ-side ask can serve "which tool does X" (rung 23). Same live confs
4681 // the catalog page reads; a missing conf just contributes zero rows.
4682 let atr: i64 = cw_asktools(abuf, apos, CW_ASKIDXCAP, allowpath, schemapath)
4683 arecs = arecs + atr
4684 cw_outpath(opath, outdir, "askindex.tsv" as *u8)
4685 if cw_flush(opath, abuf, apos, CW_ASKIDXCAP) == 0 { pages = pages + 1 }
4686 st[16] = arecs
4687 st[17] = apos[1]
4688 if apos[1] == 1 { truncs = truncs + 1 }
4689
4690 // ===== pass 3d: search.html =====
4691 pos[0] = 0
4692 pos[1] = 0
4693 cw_head3(page, pos, CW_PAGECAP, "search" as *u8, 6, sbbuf)
4694 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "search" as *u8, 6)
4695 cw_apps(page, pos, CW_PAGECAP, "<h1>search the code wiki</h1>\n<p class='muted'>" as *u8)
4696 cw_appn(page, pos, CW_PAGECAP, nsorted)
4697 cw_apps(page, pos, CW_PAGECAP, " modules and " as *u8)
4698 cw_appn(page, pos, CW_PAGECAP, nfns)
4699 cw_apps(page, pos, CW_PAGECAP, " functions, filtered in your browser (no server round-trip).</p>\n" as *u8)
4700 cw_apps(page, pos, CW_PAGECAP, "<input id='q' placeholder='module, function, or capability...' oninput='go()' autofocus style='width:420px'>\n<div id='out'></div>\n<script src='/code/modindex.js'></script><script src='/code/fnindex.js'></script><script src='/code/toolindex.js'></script>\n<script>\n" as *u8)
4701 if noext == 0 { cw_apps(page, pos, CW_PAGECAP, "var EXT=\".html\";\n" as *u8) } else { cw_apps(page, pos, CW_PAGECAP, "var EXT=\"\";\n" as *u8) }
4702 cw_apps(page, pos, CW_PAGECAP, "function row(h){return \"<tr>\"+h+\"</tr>\"}\n" as *u8)
4703 cw_apps(page, pos, CW_PAGECAP, "function go(){var q=document.getElementById(\"q\").value.toLowerCase();var o=document.getElementById(\"out\");if(q.length<2){o.innerHTML=\"\";return}\n" as *u8)
4704 cw_apps(page, pos, CW_PAGECAP, "var mh=\"\";var mc=0;for(var i=0;i<MODS.length&&mc<40;i++){var r=MODS[i];if(r[0].indexOf(q)>=0||r[2].toLowerCase().indexOf(q)>=0){mc++;mh+=row(\"<td><a href='\"+r[0]+EXT+\"'>\"+r[0]+\"</a> <span class='muted'>[\"+(r[5]||\"\")+\"]</span></td><td class='muted'>\"+r[2]+\"</td><td class='n'>\"+r[3]+\" loc</td>\")}}\n" as *u8)
4705 cw_apps(page, pos, CW_PAGECAP, "var fh=\"\";var fc=0;for(var j=0;j<FNS.length&&fc<60;j++){var s=FNS[j];if(s[0].toLowerCase().indexOf(q)>=0){fc++;fh+=row(\"<td><a href='\"+s[1]+EXT+\"#fn_\"+s[0]+\"'>\"+s[0]+\"</a></td><td class='muted'>\"+s[1]+\"</td><td class='n'>line \"+s[2]+\"</td>\")}}\n" as *u8)
4706 cw_apps(page, pos, CW_PAGECAP, "var th2=\"\";var tc=0;if(typeof TOOLS!==\"undefined\"){for(var k=0;k<TOOLS.length&&tc<30;k++){var t=TOOLS[k];if(t[0].indexOf(q)>=0||t[1].toLowerCase().indexOf(q)>=0){tc++;th2+=row(\"<td><a href='tools\"+EXT+\"#t_\"+t[0]+\"'>\"+t[0]+\"</a></td><td class='muted'>\"+t[1]+\"</td><td class='sig'>\"+t[2]+\"</td>\")}}}\n" as *u8)
4707 cw_apps(page, pos, CW_PAGECAP, "o.innerHTML=\"<h2>capabilities (\"+tc+\" shown, cap 30)</h2><table>\"+th2+\"</table><h2>modules (\"+mc+\" shown, cap 40)</h2><table>\"+mh+\"</table><h2>functions (\"+fc+\" shown, cap 60)</h2><table>\"+fh+\"</table>\"}\n</script>\n" as *u8)
4708 cw_foot(page, pos, CW_PAGECAP)
4709 cw_outpath(opath, outdir, "search.html" as *u8)
4710 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4711 if pos[1] == 1 { truncs = truncs + 1 }
4712
4713 // ===== pass 3d2: ask.html -- grounded chat over the wiki (rung 15) =====
4714 // The rung-14 answer pipeline (_ops/codewiki_answer.ps1) as an in-wiki
4715 // surface, its four disciplines enforced IN THE PAGE: 1. no evidence, no
4716 // answer (NO-MATCH never invokes the model); 2. the prompt carries only
4717 // retrieved records; 3. every identifier in the answer is mechanically
4718 // verified against the retrieved set -- one unverified name REJECTS the
4719 // whole answer; 4. all local -- retrieval is client-side over the wiki's
4720 // own indexes, generation is the operator's loopback llama-server (CORS
4721 // origin-echo verified live 2026-07-28). Engine absent => cited evidence
4722 // only, declared -- an honest degrade, never a dead page.
4723 pos[0] = 0
4724 pos[1] = 0
4725 cw_head3(page, pos, CW_PAGECAP, "ask" as *u8, 3, sbbuf)
4726 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "ask" as *u8, 3)
4727 cw_apps(page, pos, CW_PAGECAP, "<h1>ask the code wiki</h1>\n<p class='muted'>grounded Q&A: retrieval over " as *u8)
4728 cw_appn(page, pos, CW_PAGECAP, nsorted)
4729 cw_apps(page, pos, CW_PAGECAP, " modules and " as *u8)
4730 cw_appn(page, pos, CW_PAGECAP, nfns)
4731 cw_apps(page, pos, CW_PAGECAP, " functions runs in your browser; prose comes from a local engine at <code>http://127.0.0.1:7862</code>. The question never leaves your machine -- not even to this site.</p>\n" as *u8)
4732 cw_apps(page, pos, CW_PAGECAP, "<div id='eng'><span class='pill'>engine: checking...</span></div>\n<input id='q' style='width:520px' placeholder='e.g. which organ detects duplicate function bodies' onkeydown='if(event.key==\"Enter\")ask()'>\n<a class='cta' href='#' onclick='ask();return false'>ask</a>\n<div id='out'></div>\n" as *u8)
4733 cw_apps(page, pos, CW_PAGECAP, "<h2>the discipline</h2>\n<p class='muted'>1. no evidence, no answer -- NO-MATCH never invokes the model. 2. the prompt carries only retrieved records. 3. every identifier in the answer is mechanically verified against the evidence; one unverified name rejects the whole answer. 4. everything is local; your browser may ask permission to reach a local device. Engine recipe: _ops/codewiki_answer.ps1 (llama-server, port 7862).</p>\n" as *u8)
4734 cw_apps(page, pos, CW_PAGECAP, "<script src='/code/modindex.js'></script><script src='/code/fnindex.js'></script><script src='/code/toolindex.js'></script>\n<script>\n" as *u8)
4735 if noext == 0 { cw_apps(page, pos, CW_PAGECAP, "var EXT=\".html\";\n" as *u8) } else { cw_apps(page, pos, CW_PAGECAP, "var EXT=\"\";\n" as *u8) }
4736 cw_apps(page, pos, CW_PAGECAP, "var ENG=\"http://127.0.0.1:7862\";var eng=0;\n" as *u8)
4737 cw_apps(page, pos, CW_PAGECAP, "function esc(s){return s.replace(/&/g,\"&\").replace(/</g,\"<\").replace(/>/g,\">\")}\n" as *u8)
4738 cw_apps(page, pos, CW_PAGECAP, "function pill(t,c){document.getElementById(\"eng\").innerHTML=\"<span class='pill'>engine: \"+t+\"</span>\"+(c?\" <span class='muted'>\"+c+\"</span>\":\"\")}\n" as *u8)
4739 cw_apps(page, pos, CW_PAGECAP, "function detect(){return fetch(ENG+\"/health\").then(function(r){eng=r.ok?1:0;pill(eng?\"LIVE (local model, 127.0.0.1:7862)\":\"not detected\",\"\")}).catch(function(){eng=0;pill(\"not detected\",\"evidence retrieval still works; start the local engine for prose\")})}\n" as *u8)
4740 cw_apps(page, pos, CW_PAGECAP, "var qp=new URLSearchParams(location.search).get(\"q\");if(qp)document.getElementById(\"q\").value=qp;\n" as *u8)
4741 cw_apps(page, pos, CW_PAGECAP, "detect().then(function(){if(qp)ask()});\n" as *u8)
4742 cw_apps(page, pos, CW_PAGECAP, "var STOP={the:1,and:1,for:1,what:1,which:1,how:1,does:1,that:1,this:1,with:1,are:1,was:1,not:1,you:1,can:1,use:1,used:1,uses:1,our:1,any:1,all:1,one:1,has:1,have:1,its:1,into:1,from:1,when:1,where:1,who:1,why:1,there:1,they:1};\n" as *u8)
4743 cw_apps(page, pos, CW_PAGECAP, "function toks(q){var raw=q.toLowerCase().split(/[^a-z0-9_]+/);var out=[];for(var i=0;i<raw.length;i++){var t=raw[i];if(t.length>=3&&!STOP[t]&&out.indexOf(t)<0)out.push(t)}return out}\n" as *u8)
4744 cw_apps(page, pos, CW_PAGECAP, "function wb(hay,t){return new RegExp(\"(^|[^a-z0-9_])\"+t+\"($|[^a-z0-9_])\").test(hay)}\n" as *u8)
4745 cw_apps(page, pos, CW_PAGECAP, "function stemhit(stem,t){if(stem===t)return 3;return stem.replace(/\\.nx$/,\"\").split(\"_\").indexOf(t)>=0?2:0}\n" as *u8)
4746 cw_apps(page, pos, CW_PAGECAP, "function rank(ts){var df=[],i,j;for(j=0;j<ts.length;j++){var d=0;for(i=0;i<MODS.length;i++){if(stemhit(MODS[i][0],ts[j])>0||wb(MODS[i][2].toLowerCase(),ts[j]))d++}df.push(d)}\n" as *u8)
4747 cw_apps(page, pos, CW_PAGECAP, "var sc=[];for(i=0;i<MODS.length;i++){var r=MODS[i],s=0;for(j=0;j<ts.length;j++){var w=1000/(3+df[j]);var h=stemhit(r[0],ts[j]);if(h)s+=h*2*w;if(wb(r[2].toLowerCase(),ts[j]))s+=w}if(s>0)sc.push([s,i])}sc.sort(function(a,b){return b[0]-a[0]});return sc.slice(0,8)}\n" as *u8)
4748 cw_apps(page, pos, CW_PAGECAP, "function fnhits(ts){var out=[];for(var i=0;i<FNS.length&&out.length<8;i++){var nm=FNS[i][0].toLowerCase();for(var j=0;j<ts.length;j++){if(nm===ts[j]||nm.split(\"_\").indexOf(ts[j])>=0){out.push(FNS[i]);break}}}return out}\n" as *u8)
4749 cw_apps(page, pos, CW_PAGECAP, "function tooltop(ts){if(typeof TOOLS===\"undefined\")return[];var sc=[];for(var i=0;i<TOOLS.length;i++){var s=0,nm=TOOLS[i][0],tt=TOOLS[i][1].toLowerCase();for(var j=0;j<ts.length;j++){var h=stemhit(nm,ts[j]);if(h)s+=h*3;if(wb(tt,ts[j]))s+=2}if(s>0)sc.push([s,i])}sc.sort(function(a,b){return b[0]-a[0]});return sc.slice(0,4)}\n" as *u8)
4750 cw_apps(page, pos, CW_PAGECAP, "function evhtml(top,fns,tls){var h=\"<h2>retrieved evidence</h2><table>\";for(var w=0;w<tls.length;w++){var tv=TOOLS[tls[w][1]];h+=\"<tr><td><a href='tools\"+EXT+\"#t_\"+tv[0]+\"'>\"+tv[0]+\"</a> <span class='muted'>[capability]</span></td><td class='muted'>\"+esc(tv[1])+\"</td></tr>\"}\n" as *u8)
4751 cw_apps(page, pos, CW_PAGECAP, "for(var i=0;i<top.length;i++){var r=MODS[top[i][1]];h+=\"<tr><td><a href='\"+r[0]+EXT+\"'>\"+r[0]+\"</a> <span class='muted'>[\"+(r[5]||\"\")+\"]</span></td><td class='muted'>\"+esc(r[2])+\"</td></tr>\"}\n" as *u8)
4752 cw_apps(page, pos, CW_PAGECAP, "for(i=0;i<fns.length;i++){h+=\"<tr><td><a href='\"+fns[i][1]+EXT+\"#fn_\"+fns[i][0]+\"'>\"+fns[i][0]+\"()</a></td><td class='muted'>in \"+fns[i][1]+\" line \"+fns[i][2]+\"</td></tr>\"}return h+\"</table>\"}\n" as *u8)
4753 cw_apps(page, pos, CW_PAGECAP, "function ask(){var q=document.getElementById(\"q\").value.trim();var o=document.getElementById(\"out\");if(q.length<4)return;var ts=toks(q);\n" as *u8)
4754 cw_apps(page, pos, CW_PAGECAP, "if(ts.length===0){o.innerHTML=\"<p class='doc'>NO EVIDENCE IN THE WIKI -- the question has no usable terms. Not guessing.</p>\";return}\n" as *u8)
4755 cw_apps(page, pos, CW_PAGECAP, "var top=rank(ts),fns=fnhits(ts),tls=tooltop(ts);\n" as *u8)
4756 cw_apps(page, pos, CW_PAGECAP, "if(top.length===0&&fns.length===0&&tls.length===0){o.innerHTML=\"<p class='doc'>NO EVIDENCE IN THE WIKI for: \"+esc(q)+\"<br>No module, function, or capability record shares a token with this question. Not guessing -- the model was not invoked.</p>\";return}\n" as *u8)
4757 cw_apps(page, pos, CW_PAGECAP, "var ev=evhtml(top,fns,tls);\n" as *u8)
4758 cw_apps(page, pos, CW_PAGECAP, "if(!eng){o.innerHTML=\"<p class='muted'>local engine not detected -- cited evidence only; nothing was generated, nothing left this machine.</p>\"+ev;return}\n" as *u8)
4759 cw_apps(page, pos, CW_PAGECAP, "o.innerHTML=\"<p class='muted'>reading cited pages + asking the local model...</p>\";\n" as *u8)
4760 cw_apps(page, pos, CW_PAGECAP, "var picks=top.slice(0,4);\n" as *u8)
4761 cw_apps(page, pos, CW_PAGECAP, "var proms=picks.map(function(p){return fetch(MODS[p[1]][0]+EXT).then(function(r){return r.text()}).then(function(h){var b=new DOMParser().parseFromString(h,\"text/html\").querySelector(\".doc\");return b?b.textContent.slice(0,1100):\"\"}).catch(function(){return \"\"})});\n" as *u8)
4762 cw_apps(page, pos, CW_PAGECAP, "Promise.all(proms).then(function(docs){var allowed={},recs=\"\",i;\n" as *u8)
4763 cw_apps(page, pos, CW_PAGECAP, "for(i=0;i<top.length;i++){var r=MODS[top[i][1]];allowed[r[0]]=1;recs+=\"MODULE \"+r[0]+\" (dir \"+r[1]+\", \"+r[3]+\" loc, kind \"+r[5]+\"): \"+r[2]+\"\\n\"}\n" as *u8)
4764 cw_apps(page, pos, CW_PAGECAP, "for(i=0;i<picks.length;i++){if(docs[i])recs+=\"HEADER-DOC of \"+MODS[picks[i][1]][0]+\": \"+docs[i]+\"\\n\"}\n" as *u8)
4765 cw_apps(page, pos, CW_PAGECAP, "for(i=0;i<fns.length;i++){allowed[fns[i][0]]=1;allowed[fns[i][1]]=1;recs+=\"FUNCTION \"+fns[i][0]+\" in \"+fns[i][1]+\" line \"+fns[i][2]+\"\\n\"}\n" as *u8)
4766 cw_apps(page, pos, CW_PAGECAP, "for(i=0;i<tls.length;i++){var tz=TOOLS[tls[i][1]];allowed[tz[0]]=1;recs+=\"CAPABILITY \"+tz[0]+\": \"+tz[1]+(tz[2]?\" -- call: \"+tz[2]:\"\")+\"\\n\"}\n" as *u8)
4767 cw_apps(page, pos, CW_PAGECAP, "var names=Object.keys(allowed).sort().join(\", \");\n" as *u8)
4768 cw_apps(page, pos, CW_PAGECAP, "var prompt=\"You answer questions about the Nishi sovereign ecosystem codebase (all NishiLang, one binary per organ) using ONLY the retrieved records below.\\n\\nRETRIEVED RECORDS:\\n\"+recs+\"\\nQUESTION: \"+q+\"\\n\\n\";\n" as *u8)
4769 cw_apps(page, pos, CW_PAGECAP, "prompt+=\"Rules: answer in 2-4 sentences using only the records above. You may name ONLY these identifiers: \"+names+\". If the records do not answer the question, say exactly \\\"The retrieved records do not answer this.\\\" Do not speculate, do not invent module names, no bullet points. /no_think\";\n" as *u8)
4770 cw_apps(page, pos, CW_PAGECAP, "return fetch(ENG+\"/v1/chat/completions\",{method:\"POST\",headers:{\"Content-Type\":\"application/json\"},body:JSON.stringify({model:\"local\",max_tokens:400,temperature:0.2,messages:[{role:\"user\",content:prompt}]})}).then(function(r){return r.json()}).then(function(j){\n" as *u8)
4771 cw_apps(page, pos, CW_PAGECAP, "var ans=(j.choices&&j.choices[0]&&j.choices[0].message)?j.choices[0].message.content:\"\";ans=ans.split(/<\\/?think>/).pop().trim();\n" as *u8)
4772 cw_apps(page, pos, CW_PAGECAP, "var un=[],m,re=/\\b(nx_[A-Za-z0-9_]+|[a-z]{2,}_[a-z0-9_]{3,})\\b/g;\n" as *u8)
4773 cw_apps(page, pos, CW_PAGECAP, "while((m=re.exec(ans)))if(!allowed[m[1]]&&un.indexOf(m[1])<0)un.push(m[1]);\n" as *u8)
4774 cw_apps(page, pos, CW_PAGECAP, "if(!ans){o.innerHTML=\"<p class='doc'>the engine returned nothing -- evidence below.</p>\"+ev;return}\n" as *u8)
4775 cw_apps(page, pos, CW_PAGECAP, "if(un.length){o.innerHTML=\"<p class='doc'>ANSWER REJECTED -- the model named identifiers retrieval never returned: \"+esc(un.join(\", \"))+\". Refusing to show an answer whose citations cannot be checked. The evidence:</p>\"+ev;return}\n" as *u8)
4776 cw_apps(page, pos, CW_PAGECAP, "var src=\"\";for(var s=0;s<picks.length;s++){var r2=MODS[picks[s][1]];src+=\"<a class='pill' href='\"+r2[0]+EXT+\"'>\"+r2[0]+\"</a> \"}\n" as *u8)
4777 cw_apps(page, pos, CW_PAGECAP, "o.innerHTML=\"<div class='nar'>\"+esc(ans)+\"</div><p class='muted'>every identifier verified present in the retrieved evidence; the question never left this machine.</p><p>sources: \"+src+\"</p>\"+ev\n" as *u8)
4778 cw_apps(page, pos, CW_PAGECAP, "})\n}).catch(function(e){o.innerHTML=\"<p class='doc'>engine call failed (\"+esc(String(e))+\") -- evidence below.</p>\"+ev})\n}\n</script>\n" as *u8)
4779 cw_foot(page, pos, CW_PAGECAP)
4780 cw_outpath(opath, outdir, "ask.html" as *u8)
4781 if cw_flush(opath, page, pos, CW_PAGECAP) == 0 { pages = pages + 1 }
4782 if pos[1] == 1 { truncs = truncs + 1 }
4783
4784 // ===== pass 3d3: lang.html -- the NishiLang adoption instrument (rung 16) =====
4785 // The language lane's coverage/adoption axes measured over the WHOLE tree
4786 // at every regen: [N]T fixed arrays (bounds-checked spatial safety, lane
4787 // r1) and []T slices (heap spatial safety, lane r2). The lane's one-off
4788 // hand counts become a live instrument; the measured cross-language
4789 // rankings + the SOTA plan live at /nishilang.
4790 var lgfix: i64 = 0
4791 var lgsli: i64 = 0
4792 var lg0: i64 = 0
4793 while lg0 < nsorted {
4794 let lgv: i64 = sidx[lg0]
4795 if (langv[lgv] & 1) != 0 { lgfix = lgfix + 1 }
4796 if (langv[lgv] & 2) != 0 { lgsli = lgsli + 1 }
4797 lg0 = lg0 + 1
4798 }
4799 var lgfp: i64 = 0
4800 var lgsp: i64 = 0
4801 if nsorted > 0 { lgfp = (lgfix * 1000) / nsorted; lgsp = (lgsli * 1000) / nsorted }
4802 pos[0] = 0
4803 pos[1] = 0
4804 cw_head3(page, pos, CW_PAGECAP, "language" as *u8, 8, sbbuf)
4805 cw_crumb(page, pos, CW_PAGECAP, noext, 0 as *u8, 0 as *u8, "language" as *u8, 8)
4806 cw_apps(page, pos, CW_PAGECAP, "<h1>the language: NishiLang</h1>\n<p class='muted'>every module in this tree is NishiLang, compiled by the sovereign nx_cc (no gcc, no llvm). The measured cross-language rankings, safety head-to-heads, and the SOTA plan live at <a href='/nishilang'>/nishilang</a>; this page is the tree-side instrument: the language lane's adoption axes, re-measured on every regeneration.</p>\n" as *u8)
4807 cw_apps(page, pos, CW_PAGECAP, "<div class='tiles'><div class='tile'><div class='v'>" as *u8)
4808 cw_appn(page, pos, CW_PAGECAP, nsorted)
4809 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>NishiLang modules</div></div><div class='tile'><div class='v'>" as *u8)
4810 cw_appn(page, pos, CW_PAGECAP, lgfix)
4811 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>[N]T fixed-array modules (" as *u8)
4812 cw_appn(page, pos, CW_PAGECAP, lgfp)
4813 cw_apps(page, pos, CW_PAGECAP, " permil)</div></div><div class='tile'><div class='v'>" as *u8)
4814 cw_appn(page, pos, CW_PAGECAP, lgsli)
4815 cw_apps(page, pos, CW_PAGECAP, "</div><div class='k'>[]T slice modules (" as *u8)
4816 cw_appn(page, pos, CW_PAGECAP, lgsp)
4817 cw_apps(page, pos, CW_PAGECAP, " permil)</div></div></div>\n" as *u8)
4818 cw_apps(page, pos, CW_PAGECAP, "<h2>spatial-safety adoption (measured at every regen)</h2