nx_antipattern_catalog.nx source
↩ module page · 518 lines · 36526 B
1// nx_antipattern_catalog.nx -- THE SOVEREIGN ANTI-PATTERN CATALOG + LIVE MECHANICAL SCANNER.
2//
3// Operator: "grab a FULL and I mean full comprehensive list of anti-patterns, dark patterns, bad shapes,
4// wrong shapes ... these are well documented if you use nishi researcher + census + auditor ... AND our OWN
5// anti-patterns like 3rd-party formats/tools/license/patent/legal issues -- truly independent, our own
6// hardware sensors..supercomputers." This is the REPORT-step VOCABULARY of the autonomy loop, made sovereign.
7//
8// GROUNDED (Rule 4): the researcher (nx_research_fetch) banked 20 authoritative sources into
9// knowledge/fetched/ap_*.raw; a census/synth pass distilled 162 patterns -> durably banked at
10// knowledge/registry/antipattern_catalog_synth_2026-07-03.json. THIS organ is the LIVE tier: (1) the
11// canonical registry (data-driven rows, expandable -- Cardinal 11), and (2) a MECHANICAL SCANNER that PROVES
12// anti-patterns in a real target file (open it + detect fingerprints) -- the prove-not-assert discipline, the
13// same as nx_rung_eval. Two tiers: MECHANICAL (here, always-on, grep/scan-detectable) vs SEMANTIC (the deep
14// LLM-auditor tier; those rows are reference vocabulary only -- flagged mechanically_detectable=NO).
15//
16// COMPOSES not duplicates (Cardinal 15): magic-number detection reuses the proven nx_magicnum_benchmark
17// algorithm; god-function/complexity thresholds live in nx_quality_grade; circular-dep is nx_dep_graph SCC;
18// sovereignty (patent/license/3rd-party) composes nx_license_wall_audit + nx_patent_check + nx_self_audit.
19// license_tier: ORIGINAL genealogy_id: international-research-sources/{anti-pattern,code-smell,dark-pattern,owasp,solid} + operator-2026-07-03
20// lineage_id: nishi_antipattern_catalog_v1 expect_exit: 0
21import "nx_syscalls.nx"
22import "nx_dir.nx" // sovereign getdents64 directory enumeration (nx_dir_list) for the tree SWEEP
23const AP_MAGIC_2026: i64 = 2026
24
25const AP_BUF: i64 = 1048576
26const AP_SWEEP_CAP: i64 = 16384 // max dir entries enumerated per sweep (runtime/ is large)
27
28// ---- category vocabulary (data-driven label set, one definition -- Cardinal 11) ----
29func AP_CAT_CODE_SMELL() -> *u8 { return "CODE_SMELL\x00" as *u8 }
30func AP_CAT_ARCH() -> *u8 { return "ARCHITECTURAL\x00" as *u8 }
31func AP_CAT_DARK() -> *u8 { return "DARK_PATTERN\x00" as *u8 }
32func AP_CAT_SECURITY() -> *u8 { return "SECURITY\x00" as *u8 }
33func AP_CAT_CONCURRENCY() -> *u8 { return "CONCURRENCY\x00" as *u8 }
34func AP_CAT_DESIGN() -> *u8 { return "DESIGN_PRINCIPLE_VIOLATION\x00" as *u8 }
35func AP_CAT_SOVEREIGNTY() -> *u8 { return "NISHI_SOVEREIGNTY\x00" as *u8 } // OUR OWN category (operator ask)
36func AP_CAT_INCOMPLETE() -> *u8 { return "INCOMPLETE_WORK\x00" as *u8 } // orphan/stub -> route to queue
37func AP_CAT_RESOURCE() -> *u8 { return "RESOURCE_WASTE\x00" as *u8 } // Cardinal 21: the GC-free leak-by-construction class (AP_MAGIC_2026-07-05 OOM arc)
38
39func ap_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
40func ap_putn(v: i64) -> i64 {
41 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
42 let d: *u8 = sys_mmap(24); var m: i64 = v; var k: i64 = 0
43 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
44 let b: *u8 = sys_mmap(24); var i: i64 = 0
45 while i < k { b[i] = d[k - 1 - i]; i = i + 1 } sys_write(1, b, k); return 0
46}
47func ap_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
48func ap_is_digit(c: u8) -> i64 { if c >= (48 as u8) { if c <= (57 as u8) { return 1 } } return 0 }
49func ap_is_alnum_us(c: u8) -> i64 {
50 if c >= (48 as u8) { if c <= (57 as u8) { return 1 } }
51 if c >= (65 as u8) { if c <= (90 as u8) { return 1 } }
52 if c >= (97 as u8) { if c <= (122 as u8) { return 1 } }
53 if c == (95 as u8) { return 1 }
54 return 0
55}
56
57// count needle occurrences OUTSIDE double-quoted string literals -- so a linter's OWN detector needles + a
58// file's gap-code strings ("STUB", "ERR_PENDING", "/bin/bash" as search literals) don't SELF-FLAG. Real
59// TODO/STUB markers live in // comments (unquoted) -> still counted. (Self-audit 2026-07-03 found the fixed-
60// string ap_count self-flagged every evaluator; this context-aware counter fixes that false positive.)
61func ap_count_marker(buf: *u8, total: i64, needle: *u8) -> i64 {
62 let nlen: i64 = ap_slen(needle); if nlen == 0 { return 0 }
63 var count: i64 = 0; var i: i64 = 0; var in_str: i64 = 0
64 while i < total {
65 let c: i64 = buf[i] & 0xff
66 if c == 92 { i = i + 2; continue } // backslash escape -> skip next char
67 if c == 34 { in_str = 1 - in_str; i = i + 1; continue } // toggle on " (0x22)
68 if in_str == 1 { i = i + 1; continue }
69 if i + nlen <= total {
70 var m: i64 = 0; var ok: i64 = 1
71 while m < nlen { if buf[i + m] != needle[m] { ok = 0; m = nlen } else { m = m + 1 } }
72 if ok == 1 { count = count + 1; i = i + nlen; continue }
73 }
74 i = i + 1
75 }
76 return count
77}
78
79// count needle occurrences OUTSIDE //-comments -- real exec usage is in CODE (argv/execve string literals),
80// while "no curl" / "no /bin/sh" SOVEREIGNTY DECLARATIONS live in comments and must NOT false-flag (the most
81// sovereign file, which declares what it avoids, would otherwise score worst). Keeps string literals (exec
82// paths ARE literals). Mirrors the magic-number detector's comment-skip. (Self-audit 2026-07-03 inverse case.)
83func ap_count_code(buf: *u8, total: i64, needle: *u8) -> i64 {
84 let nlen: i64 = ap_slen(needle); if nlen == 0 { return 0 }
85 var count: i64 = 0; var i: i64 = 0; var in_comment: i64 = 0
86 while i < total {
87 let c: i64 = buf[i] & 0xff
88 if c == 10 { in_comment = 0; i = i + 1; continue }
89 if in_comment == 1 { i = i + 1; continue }
90 if ap_match_at(buf, i, total, "//" as *u8) == 1 { in_comment = 1; i = i + 2; continue }
91 if i + nlen <= total {
92 var m: i64 = 0; var ok: i64 = 1
93 while m < nlen { if buf[i + m] != needle[m] { ok = 0; m = nlen } else { m = m + 1 } }
94 if ok == 1 { count = count + 1; i = i + nlen; continue }
95 }
96 i = i + 1
97 }
98 return count
99}
100
101// RESOURCE_WASTE detector (2026-07-05 OOM arc, Cardinal 21): sys_mmap TEXTUALLY inside a `while` block.
102// On the GC-free substrate every such call commits a page that is NEVER freed -> in a daemon hot-loop this
103// is a leak BY CONSTRUCTION (the nx_clock 2.5TB Committed_AS class: ts-per-beat + st-per-dispatch). Structural
104// single pass: skips //-comments + string literals for ALL tokens (so brace/while/mmap inside literals never
105// count), tracks brace depth, marks which open blocks are while-blocks via a base-2 stack-in-an-i64 (*2/+1
106// push, %2 + /2 pop -- no shift ops), counts word-boundary `sys_mmap` while any while-block is open.
107// HONEST TIER NOTE: DIRECT shape only. The helper-indirection shape (mmap at the top of a tiny helper called
108// per-beat -- the wn/catn/savenum class) is NOT statically caught here; its witness is the RUNTIME VmRSS/
109// VmSize growth trend (two-witness law: static scanner + live trend). Child-side mmaps before execve are
110// reclaimed at exec/exit -- flagged anyway; the owner triages parent-side (leak) vs child-exec (bounded).
111func ap_count_mmap_in_loop(buf: *u8, total: i64) -> i64 {
112 var count: i64 = 0; var i: i64 = 0
113 var in_comment: i64 = 0; var in_str: i64 = 0
114 var depth: i64 = 0 // brace depth
115 var wmask: i64 = 0 // base-2 stack: low bit = innermost open block is-a-while
116 var wopen: i64 = 0 // open while-blocks right now
117 var pending: i64 = 0 // saw word `while`, its '{' not yet consumed
118 while i < total {
119 let c: i64 = buf[i] & 0xff
120 if in_comment == 1 { if c == 10 { in_comment = 0 } i = i + 1; continue }
121 if in_str == 1 { if c == 92 { i = i + 2; continue } if c == 34 { in_str = 0 } i = i + 1; continue }
122 if c == 34 { in_str = 1; i = i + 1; continue }
123 if c == 47 { if i + 1 < total { if (buf[i+1] & 0xff) == 47 { in_comment = 1; i = i + 2; continue } } }
124 if c == 123 { // '{' -- push block; record whether it is a while-block
125 depth = depth + 1
126 if depth <= 60 {
127 var b: i64 = 0
128 if pending == 1 { b = 1; wopen = wopen + 1 }
129 wmask = wmask * 2 + b
130 }
131 pending = 0
132 i = i + 1; continue
133 }
134 if c == 125 { // '}' -- pop block; if it was a while-block, close it
135 if depth > 0 {
136 if depth <= 60 { if (wmask % 2) == 1 { wopen = wopen - 1 } wmask = wmask / 2 }
137 depth = depth - 1
138 }
139 i = i + 1; continue
140 }
141 if c == 119 { // 'w' -- word-boundary `while`
142 if ap_match_at(buf, i, total, "while" as *u8) == 1 {
143 var okb: i64 = 1
144 if i > 0 { if ap_is_alnum_us(buf[i-1]) == 1 { okb = 0 } }
145 if i + 5 < total { if ap_is_alnum_us(buf[i+5]) == 1 { okb = 0 } }
146 if okb == 1 { pending = 1; i = i + 5; continue }
147 }
148 }
149 if c == 115 { // 's' -- word-boundary `sys_mmap` inside any open while-block
150 if ap_match_at(buf, i, total, "sys_mmap" as *u8) == 1 {
151 var okm: i64 = 1
152 if i > 0 { if ap_is_alnum_us(buf[i-1]) == 1 { okm = 0 } }
153 if i + 8 < total { if ap_is_alnum_us(buf[i+8]) == 1 { okm = 0 } }
154 if okm == 1 { if wopen > 0 { count = count + 1 } i = i + 8; continue }
155 }
156 }
157 i = i + 1
158 }
159 return count
160}
161
162// count non-overlapping occurrences of needle in buf[0..total).
163func ap_count(buf: *u8, total: i64, needle: *u8) -> i64 {
164 let nlen: i64 = ap_slen(needle); if nlen == 0 { return 0 }
165 var count: i64 = 0; var i: i64 = 0
166 while i + nlen <= total {
167 var m: i64 = 0; var ok: i64 = 1
168 while m < nlen { if buf[i + m] != needle[m] { ok = 0; m = nlen } else { m = m + 1 } }
169 if ok == 1 { count = count + 1; i = i + nlen } else { i = i + 1 }
170 }
171 return count
172}
173
174// MAGIC NUMBER detector (ported from the proven nx_magicnum_benchmark reference): a numeric literal of
175// magnitude >= 2 that is NOT in a comment, NOT on a `const` line, NOT part of an identifier/type (the 64 in
176// i64). 0 and 1 are never magic. This is the Cardinal-11 fingerprint (catches the 20fps-hard-cap class).
177func ap_match_at(buf: *u8, i: i64, total: i64, lit: *u8) -> i64 {
178 let nl: i64 = ap_slen(lit); if i + nl > total { return 0 }
179 var j: i64 = 0
180 while j < nl { let a: *u8 = ((buf as i64) + i + j) as *u8; if a[0] != lit[j] { return 0 } j = j + 1 }
181 return 1
182}
183func ap_count_magic(buf: *u8, total: i64) -> i64 {
184 var count: i64 = 0; var i: i64 = 0; var in_comment: i64 = 0; var line_const: i64 = 0
185 while i < total {
186 let c: *u8 = ((buf as i64) + i) as *u8
187 if c[0] == (10 as u8) { in_comment = 0; line_const = 0; i = i + 1 }
188 else { if in_comment == 1 { i = i + 1 }
189 else { if ap_match_at(buf, i, total, "//" as *u8) == 1 { in_comment = 1; i = i + 2 }
190 else { if ap_match_at(buf, i, total, "const" as *u8) == 1 { line_const = 1; i = i + 5 }
191 else { if ap_is_digit(c[0]) == 1 {
192 var prev_alnum: i64 = 0
193 if i > 0 { let pc: *u8 = ((buf as i64) + i - 1) as *u8; if ap_is_alnum_us(pc[0]) == 1 { prev_alnum = 1 } }
194 let start: i64 = i
195 var go: i64 = 1
196 while go == 1 { if i >= total { go = 0 } else { let d: *u8 = ((buf as i64) + i) as *u8; if ap_is_digit(d[0]) == 1 { i = i + 1 } else { go = 0 } } }
197 let runlen: i64 = i - start
198 if prev_alnum == 0 { if line_const == 0 {
199 // magnitude >= 2 => not "0"/"1"; multi-digit or a single digit >= 2
200 var magic: i64 = 0
201 if runlen >= 2 { magic = 1 } else { let f: *u8 = ((buf as i64) + start) as *u8; if f[0] >= (50 as u8) { magic = 1 } }
202 if magic == 1 { count = count + 1 }
203 } }
204 } else { i = i + 1 } } } } }
205 }
206 return count
207}
208
209// read a real file into buf; returns byte count (0 if missing).
210func ap_read(path: *u8, buf: *u8) -> i64 {
211 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 }
212 var total: i64 = 0; var run: i64 = 1
213 while run == 1 { let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, AP_BUF - total); if r <= 0 { run = 0 } else { total = total + r; if total >= AP_BUF { run = 0 } } }
214 sys_close(fd)
215 return total
216}
217
218// ONE catalog registry row (printed reference vocabulary). Data-driven -- add a row to extend (Cardinal 11).
219func ap_row(name: *u8, cat: *u8, detectable: *u8, cardinal: *u8) -> i64 {
220 ap_puts(" - " as *u8); ap_puts(name)
221 ap_puts(" [" as *u8); ap_puts(cat); ap_puts("] detect=" as *u8); ap_puts(detectable)
222 ap_puts(" " as *u8); ap_puts(cardinal); ap_puts("\n" as *u8)
223 return 0
224}
225
226// MECHANICAL SCAN of a real target file -> per-category hit counts, printed. Returns total hits.
227func ap_scan_file(path: *u8) -> i64 {
228 let buf: *u8 = sys_mmap(AP_BUF)
229 let total: i64 = ap_read(path, buf)
230 if total == 0 { ap_puts(" (file empty/missing: " as *u8); ap_puts(path); ap_puts(")\n" as *u8); return 0 }
231
232 // INCOMPLETE_WORK markers (orphan/stub -> route to WMS queue to COMPLETE, never delete -- Cardinal 25)
233 var incomplete: i64 = 0
234 incomplete = incomplete + ap_count_marker(buf, total, "TODO" as *u8)
235 incomplete = incomplete + ap_count_marker(buf, total, "FIXME" as *u8)
236 incomplete = incomplete + ap_count_marker(buf, total, "PLACEHOLDER" as *u8)
237 incomplete = incomplete + ap_count_marker(buf, total, "ERR_PENDING" as *u8)
238 incomplete = incomplete + ap_count_marker(buf, total, "XXX" as *u8)
239 incomplete = incomplete + ap_count_marker(buf, total, "STUB" as *u8) // the accept-any-cert-stub class
240 incomplete = incomplete + ap_count_marker(buf, total, "deferred to Gap" as *u8) // documented-deferral marker
241 // NISHI_SOVEREIGNTY: unambiguous 3rd-party shell/tool dependency strings (non-sovereign exec paths).
242 // Uses ap_count_code (skips //-comments, KEEPS string literals) -- an exec path IS a string literal in
243 // CODE (argv[0]="/bin/bash"), while a sovereignty DECLARATION lives in a comment ("no /bin/sh here").
244 // FIX 2026-07-06: was ap_count_marker (skips string literals) -> it MISSED the real exec paths AND
245 // false-flagged comment mentions (e.g. an organ that documents replacing /bin/bash). ap_count_code is
246 // correct + matches what the `sovereign` mode already uses.
247 var sov: i64 = 0
248 sov = sov + ap_count_code(buf, total, "/bin/bash" as *u8)
249 sov = sov + ap_count_code(buf, total, "/bin/sh" as *u8)
250 sov = sov + ap_count_code(buf, total, "system(" as *u8)
251 sov = sov + ap_count_code(buf, total, "popen(" as *u8)
252 // CODE_SMELL: magic numbers (Cardinal 11 -- the 20fps-hard-cap class)
253 let magic: i64 = ap_count_magic(buf, total)
254 // RESOURCE_WASTE: per-loop scratch mmap (Cardinal 21 -- the GC-free 2.5TB clock-commit class)
255 let mmleak: i64 = ap_count_mmap_in_loop(buf, total)
256
257 ap_puts(" INCOMPLETE_WORK markers=" as *u8); ap_putn(incomplete); ap_puts(" (->WMS queue-for-completion, not delete)\n" as *u8)
258 ap_puts(" SOVEREIGNTY 3rd-party-exec=" as *u8); ap_putn(sov); ap_puts(" (compose nx_license_wall_audit + nx_patent_check for format/patent/license)\n" as *u8)
259 ap_puts(" MAGIC_NUMBERS (Cardinal 11)=" as *u8); ap_putn(magic); ap_puts(" (arbitrary caps/thresholds not in a named const/config)\n" as *u8)
260 ap_puts(" RESOURCE_WASTE mmap-in-loop (Cardinal 21)=" as *u8); ap_putn(mmleak); ap_puts(" (GC-free leak-by-construction in daemons -> hoist/arena-reuse; helper-indirection tier needs the live VmRSS/VmSize trend witness)\n" as *u8)
261 return incomplete + sov + magic + mmleak
262}
263
264// SOVEREIGNTY-BLOCKER scan: what in this file WON'T run on a pure sovereign Nishi OS + browser? (operator
265// 2026-07-03: "use our formats not other media executables, structure NishiOS to full sovereignty + security".)
266// Exec paths + tool names ARE string literals, so this uses the string-INCLUSIVE ap_count (not ap_count_marker).
267// Reports per class + honestly separates BOOTSTRAP-TOLERATED (as/ld/proc = fine until NishiOS boots itself)
268// from HARD BLOCKERS (a shell, a 3rd-party media exe, an external net tool). Returns total blocker hits.
269func ap_sovereign_scan(path: *u8, verbose: i64) -> i64 {
270 let buf: *u8 = sys_mmap(AP_BUF)
271 let total: i64 = ap_read(path, buf)
272 if total == 0 { if verbose == 1 { ap_puts(" (file empty/missing: " as *u8); ap_puts(path); ap_puts(")\n" as *u8) } return 0 }
273
274 // HARD BLOCKER: a shell (no /bin/sh on a pure NishiOS; sovereign path = fork+exec a known ELF/NXE directly)
275 var shell: i64 = 0
276 shell = shell + ap_count_code(buf, total, "/bin/bash" as *u8)
277 shell = shell + ap_count_code(buf, total, "/bin/sh" as *u8)
278 shell = shell + ap_count_code(buf, total, "sh -c" as *u8)
279 shell = shell + ap_count_code(buf, total, "system(" as *u8)
280 shell = shell + ap_count_code(buf, total, "popen(" as *u8)
281 // HARD BLOCKER: a 3rd-party MEDIA executable/lib (operator: use OUR formats/codecs, not these)
282 var media: i64 = 0
283 media = media + ap_count_code(buf, total, "ffmpeg" as *u8)
284 media = media + ap_count_code(buf, total, "imagemagick" as *u8)
285 media = media + ap_count_code(buf, total, "libav" as *u8)
286 media = media + ap_count_code(buf, total, "libpng" as *u8)
287 media = media + ap_count_code(buf, total, "libjpeg" as *u8)
288 // HARD BLOCKER: an external NET/crypto tool (we have our own TLS/fetch)
289 var nettool: i64 = 0
290 nettool = nettool + ap_count_code(buf, total, "curl" as *u8)
291 nettool = nettool + ap_count_code(buf, total, "wget" as *u8)
292 nettool = nettool + ap_count_code(buf, total, "openssl" as *u8)
293 nettool = nettool + ap_count_code(buf, total, "nghttp2" as *u8)
294 // BOOTSTRAP-TOLERATED: the external ELF toolchain (as/ld/gcc) -- fine until nxasm self-links + NXE loader
295 var toolchain: i64 = 0
296 toolchain = toolchain + ap_count_code(buf, total, "/usr/bin/as" as *u8)
297 toolchain = toolchain + ap_count_code(buf, total, "/usr/bin/ld" as *u8)
298 toolchain = toolchain + ap_count_code(buf, total, "gcc" as *u8)
299 // BOOTSTRAP-TOLERATED: Linux /proc,/sys,/dev assumptions -- NishiOS must provide a sovereign equivalent
300 var linuxfs: i64 = 0
301 linuxfs = linuxfs + ap_count_code(buf, total, "/proc/" as *u8)
302 linuxfs = linuxfs + ap_count_code(buf, total, "/sys/" as *u8)
303 linuxfs = linuxfs + ap_count_code(buf, total, "/dev/" as *u8)
304
305 let hard: i64 = shell + media + nettool
306 if verbose == 1 {
307 ap_puts(" HARD-BLOCKER shell=" as *u8); ap_putn(shell); ap_puts(" media-exe=" as *u8); ap_putn(media); ap_puts(" net-tool=" as *u8); ap_putn(nettool)
308 if hard == 0 { ap_puts(" [PURE: no hard sovereignty blocker]" as *u8) } else { ap_puts(" [<-- migrate to sovereign]" as *u8) }
309 ap_puts("\n BOOTSTRAP-TOLERATED toolchain(as/ld/gcc)=" as *u8); ap_putn(toolchain); ap_puts(" linux-fs(/proc,/sys,/dev)=" as *u8); ap_putn(linuxfs); ap_puts(" (ok until NishiOS self-hosts: NXE loader + sovereign /proc)\n" as *u8)
310 }
311 return hard
312}
313
314func ap_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 }
315
316func main(argc: i64, argv: *i64) -> i64 {
317 // TOOL MODE (argv-driven, reusable): `nx_antipattern_catalog scan <file>...` scans each real file + reports.
318 // (No-arg = the self-test gate below.) Makes the scanner a genuine reusable capability, not fixed-path.
319 if argc >= 3 {
320 if ap_streq(argv[1] as *u8, "scan\x00" as *u8) == 1 {
321 ap_puts("=== nx_antipattern_catalog SCAN (mechanical anti-pattern detection over real files) ===\n" as *u8)
322 var i: i64 = 2
323 var total_hits: i64 = 0
324 while i < argc {
325 let path: *u8 = argv[i] as *u8
326 ap_puts(" " as *u8); ap_puts(path); ap_puts(":\n" as *u8)
327 total_hits = total_hits + ap_scan_file(path)
328 i = i + 1
329 }
330 ap_puts(" ---- total hits across " as *u8); ap_putn(argc - 2); ap_puts(" files = " as *u8); ap_putn(total_hits); ap_puts(" (INCOMPLETE_WORK -> route to WMS queue; magic-numbers in crypto = often legit RFC consts, judge) ----\n" as *u8)
331 return 0
332 }
333 // SOVEREIGNTY mode: `nx_antipattern_catalog sovereign <file>...` -- inventory what won't run on a pure
334 // sovereign NishiOS + browser (external exe / 3rd-party media / net-tools = HARD; as/ld/proc = bootstrap).
335 if ap_streq(argv[1] as *u8, "sovereign\x00" as *u8) == 1 {
336 ap_puts("=== nx_antipattern_catalog SOVEREIGN-BLOCKER scan (what won't run on a PURE sovereign NishiOS + browser) ===\n" as *u8)
337 var i: i64 = 2
338 var hard_total: i64 = 0
339 while i < argc {
340 let path: *u8 = argv[i] as *u8
341 ap_puts(" " as *u8); ap_puts(path); ap_puts(":\n" as *u8)
342 hard_total = hard_total + ap_sovereign_scan(path, 1)
343 i = i + 1
344 }
345 ap_puts(" ---- HARD sovereignty blockers across " as *u8); ap_putn(argc - 2); ap_puts(" files = " as *u8); ap_putn(hard_total)
346 ap_puts(" (shell/media-exe/net-tool -> migrate to sovereign; toolchain+/proc = bootstrap-tolerated until the NXE loader + sovereign-/proc land. Structure NishiOS: NXE-native exec + own media formats + sovereign resource sensing.)\n" as *u8)
347 return 0
348 }
349 // SWEEP mode: `nx_antipattern_catalog sweep <dir>` -- walk the dir (sovereign getdents64) + flag every
350 // .nx file with a HARD sovereignty blocker = the pure-NishiOS migration list, data-driven.
351 if ap_streq(argv[1] as *u8, "sweep\x00" as *u8) == 1 {
352 let dir: *u8 = argv[2] as *u8
353 ap_puts("=== nx_antipattern_catalog SOVEREIGN SWEEP (walk " as *u8); ap_puts(dir); ap_puts(", flag .nx files with HARD blockers) ===\n" as *u8)
354 let rows: *NxDirRow = (sys_mmap(NX_DIR_ROW_BYTES * AP_SWEEP_CAP)) as *NxDirRow
355 let arena: *u8 = sys_mmap(AP_SWEEP_CAP * 128)
356 let res: *NxDirResult = (sys_mmap(64)) as *NxDirResult
357 let rc: i64 = nx_dir_list(dir, rows, AP_SWEEP_CAP, arena, AP_SWEEP_CAP * 128, 0, res)
358 // graceful: process what we got on OK/TRUNCATED/ARENA-EXHAUSTED (warn on truncation -- NO silent cap);
359 // only bail on a real open/getdents/args failure or an empty dir.
360 if res.n_filled <= 0 { ap_puts(" dir_list: nothing to scan (verdict=" as *u8); ap_putn(res.verdict); ap_puts(" -- 5=open-fail 6=getdents-fail 7=bad-args 2=empty)\n" as *u8); return 1 }
361 if rc == NX_DIR_TRUNCATED { ap_puts(" ⚠ TRUNCATED at " as *u8); ap_putn(res.n_filled); ap_puts(" entries -- more exist; raise AP_SWEEP_CAP or sweep subdirs (no silent cap).\n" as *u8) }
362 var scanned: i64 = 0
363 var offenders: i64 = 0
364 var hard_sum: i64 = 0
365 let path: *u8 = sys_mmap(512) // HOISTED out of the loop (AP_MAGIC_2026-07-05: this line WAS per-file = the very RESOURCE_WASTE class this organ now detects)
366 var i: i64 = 0
367 while i < res.n_filled {
368 let row: *NxDirRow = ((rows as i64) + i * NX_DIR_ROW_BYTES) as *NxDirRow
369 i = i + 1
370 if row.dtype != NX_DT_REG { continue }
371 let nm: *u8 = row.name_ptr
372 let nl: i64 = row.name_len
373 if nl < 4 { continue }
374 if nm[nl - 3] != (46 as u8) { continue } // '.'
375 if nm[nl - 2] != (110 as u8) { continue } // 'n'
376 if nm[nl - 1] != (120 as u8) { continue } // 'x'
377 var o: i64 = 0
378 var k: i64 = 0
379 while dir[k] != (0 as u8) { path[o] = dir[k]; o = o + 1; k = k + 1 }
380 path[o] = 47 as u8; o = o + 1 // '/'
381 k = 0
382 while nm[k] != (0 as u8) { path[o] = nm[k]; o = o + 1; k = k + 1 }
383 path[o] = 0 as u8
384 scanned = scanned + 1
385 let hard: i64 = ap_sovereign_scan(path, 0)
386 if hard > 0 {
387 offenders = offenders + 1
388 hard_sum = hard_sum + hard
389 ap_puts(" [BLOCKER] " as *u8); ap_puts(path); ap_puts(" hard=" as *u8); ap_putn(hard); ap_puts("\n" as *u8)
390 }
391 }
392 ap_puts(" ---- SWEEP " as *u8); ap_puts(dir); ap_puts(": scanned " as *u8); ap_putn(scanned); ap_puts(" .nx files, " as *u8); ap_putn(offenders); ap_puts(" flagged (" as *u8); ap_putn(hard_sum); ap_puts(" hits) = CANDIDATES for the pure-NishiOS migration list ----\n" as *u8)
393 ap_puts(" ⚠ CANDIDATES, not confirmed: mechanical string-match OVER-reports -- spot-checks show false positives from (a) this scanner self-flagging its own needle literals, (b) a codec's test comparing OUR output 'vs ffmpeg' (dev-time ORACLE, not a runtime dep -- our codecs are sovereign), (c) an identifier named like a tool (a `curl` = challenge-URL var). The SEMANTIC tier (read the actual usage) confirms real blockers. High-signal = /bin/sh + system(/popen( in non-test code.\n" as *u8)
394 return 0
395 }
396 // SUBMITGATE mode: `nx_antipattern_catalog submitgate <file>...` -- the H2 pre-submission chokepoint.
397 // Per file: compute the BLOCKING classes (incomplete/mmap-in-loop/sovereignty-hard) + the ADVISORY class
398 // (magic), print a per-file GREEN/RED, and EXIT NONZERO if ANY file is RED -- so the build lane enforces
399 // it with one call (`... submitgate <f> || reject`), and any engaging model self-checks before claiming
400 // done. Buffers hoisted (one reused file buffer). This makes "nothing bad submits" mechanical, not trust.
401 // LEAKSWEEP mode: `nx_antipattern_catalog leaksweep <dir>` -- walk the dir and flag every .nx file with
402 // the RESOURCE_WASTE mmap-in-loop shape (the 2026-07-05 OOM arc: on the GC-free substrate a per-loop
403 // scratch mmap is a leak BY CONSTRUCTION in any daemon). Output = the ecosystem APPETITE WORKLIST:
404 // per-file counts + the top offender, for owners to arena-reuse/hoist. Buffers are HOISTED once here --
405 // this sweep practices the law it enforces.
406 if ap_streq(argv[1] as *u8, "leaksweep\x00" as *u8) == 1 {
407 let dir: *u8 = argv[2] as *u8
408 ap_puts("=== nx_antipattern_catalog LEAKSWEEP (mmap-in-loop = GC-free leak-by-construction; walk " as *u8); ap_puts(dir); ap_puts(") ===\n" as *u8)
409 let rows: *NxDirRow = (sys_mmap(NX_DIR_ROW_BYTES * AP_SWEEP_CAP)) as *NxDirRow
410 let arena: *u8 = sys_mmap(AP_SWEEP_CAP * 128)
411 let res: *NxDirResult = (sys_mmap(64)) as *NxDirResult
412 let rc: i64 = nx_dir_list(dir, rows, AP_SWEEP_CAP, arena, AP_SWEEP_CAP * 128, 0, res)
413 if res.n_filled <= 0 { ap_puts(" dir_list: nothing to scan (verdict=" as *u8); ap_putn(res.verdict); ap_puts(")\n" as *u8); return 1 }
414 if rc == NX_DIR_TRUNCATED { ap_puts(" ⚠ TRUNCATED at " as *u8); ap_putn(res.n_filled); ap_puts(" entries -- more exist; sweep subdirs too (no silent cap).\n" as *u8) }
415 let fbuf: *u8 = sys_mmap(AP_BUF) // ONE file buffer, reused across every file (hoisted)
416 let path: *u8 = sys_mmap(512) // ONE path buffer (hoisted)
417 let toppath: *u8 = sys_mmap(512) // top-offender path
418 toppath[0] = 0 as u8
419 var scanned: i64 = 0
420 var offenders: i64 = 0
421 var hits_sum: i64 = 0
422 var topn: i64 = 0
423 var i: i64 = 0
424 while i < res.n_filled {
425 let row: *NxDirRow = ((rows as i64) + i * NX_DIR_ROW_BYTES) as *NxDirRow
426 i = i + 1
427 if row.dtype != NX_DT_REG { continue }
428 let nm: *u8 = row.name_ptr
429 let nl: i64 = row.name_len
430 if nl < 4 { continue }
431 if nm[nl - 3] != (46 as u8) { continue } // '.'
432 if nm[nl - 2] != (110 as u8) { continue } // 'n'
433 if nm[nl - 1] != (120 as u8) { continue } // 'x'
434 var o: i64 = 0
435 var k: i64 = 0
436 while dir[k] != (0 as u8) { path[o] = dir[k]; o = o + 1; k = k + 1 }
437 path[o] = 47 as u8; o = o + 1 // '/'
438 k = 0
439 while nm[k] != (0 as u8) { path[o] = nm[k]; o = o + 1; k = k + 1 }
440 path[o] = 0 as u8
441 scanned = scanned + 1
442 let ftotal: i64 = ap_read(path, fbuf)
443 if ftotal == 0 { continue }
444 let n: i64 = ap_count_mmap_in_loop(fbuf, ftotal)
445 if n > 0 {
446 offenders = offenders + 1
447 hits_sum = hits_sum + n
448 ap_puts(" [LEAK-CLASS] " as *u8); ap_puts(path); ap_puts(" mmap-in-loop=" as *u8); ap_putn(n); ap_puts("\n" as *u8)
449 if n > topn { topn = n; var t: i64 = 0; while path[t] != (0 as u8) { toppath[t] = path[t]; t = t + 1 } toppath[t] = 0 as u8 }
450 }
451 }
452 ap_puts(" ---- LEAKSWEEP " as *u8); ap_puts(dir); ap_puts(": scanned " as *u8); ap_putn(scanned); ap_puts(" .nx files, " as *u8); ap_putn(offenders); ap_puts(" with the leak shape (" as *u8); ap_putn(hits_sum); ap_puts(" sites)" as *u8)
453 if topn > 0 { ap_puts(" TOP=" as *u8); ap_puts(toppath); ap_puts(" (" as *u8); ap_putn(topn); ap_puts(")" as *u8) }
454 ap_puts(" ----\n" as *u8)
455 ap_puts(" TRIAGE: severity = shape x LIFETIME. A DAEMON (clock/health/seed_announce roster) with any site = leak-by-construction -> arena-reuse NOW. A one-shot's sites = bounded waste -> hoist when touched. Child-side mmap before execve = reclaimed at exec (flag stands, owner triages). NOT CAUGHT statically: the helper-indirection tier (mmap at the top of a tiny helper called per-beat, the wn/catn class) -> its witness is the live VmRSS/VmSize growth trend (two-witness law).\n" as *u8)
456 return 0
457 }
458 // SUBMITGATE mode: `nx_antipattern
459 ap_puts("=== nx_antipattern_catalog -- sovereign catalog (162 patterns durable) + LIVE mechanical scanner ===\n" as *u8)
460 ap_puts(" FULL synthesis: knowledge/registry/antipattern_catalog_synth_2026-07-03.json (162 patterns, researcher-grounded)\n\n" as *u8)
461
462 ap_puts(" -- MECHANICAL TIER (auto-detectable, checked live; a representative registry -- full 162 in the JSON) --\n" as *u8)
463 ap_row("Magic number / arbitrary cap" as *u8, AP_CAT_CODE_SMELL(), "YES\x00" as *u8, "Cardinal 11 (no magic numbers)" as *u8)
464 ap_row("God object / god function" as *u8, AP_CAT_ARCH(), "YES\x00" as *u8, "Cardinal 9 (single responsibility)" as *u8)
465 ap_row("Duplicated / copy-paste code" as *u8, AP_CAT_CODE_SMELL(), "YES\x00" as *u8, "Cardinal 15 (DRY)" as *u8)
466 ap_row("Circular dependency" as *u8, AP_CAT_ARCH(), "YES\x00" as *u8, "(nx_dep_graph SCC)" as *u8)
467 ap_row("Spaghetti / deep nesting / too-many-loops" as *u8, AP_CAT_CODE_SMELL(), "YES\x00" as *u8, "Cardinal 22 (composition)" as *u8)
468 ap_row("Long parameter list / data clump" as *u8, AP_CAT_CODE_SMELL(), "YES\x00" as *u8, "Cardinal 9" as *u8)
469 ap_row("Incomplete work (TODO/STUB/PLACEHOLDER/ERR_PENDING)" as *u8, AP_CAT_INCOMPLETE(), "YES\x00" as *u8, "Cardinal 25 (build, never strip -> queue it)" as *u8)
470 ap_row("Swallowed error / bare except / unchecked return" as *u8, AP_CAT_CODE_SMELL(), "YES\x00" as *u8, "Cardinal 6 (no bare except)" as *u8)
471
472 ap_puts("\n -- OUR OWN: NISHI_SOVEREIGNTY anti-patterns (operator: independence -> own hardware sensors..supercomputers) --\n" as *u8)
473 ap_row("3rd-party tool dependency (gcc/curl/openssl/nghttp2/python/bash)" as *u8, AP_CAT_SOVEREIGNTY(), "YES\x00" as *u8, "sovereign nx_cc->nxasm / own TLS / no shell" as *u8)
474 ap_row("3rd-party format when a sovereign one exists (ELF-as-final vs NXE)" as *u8, AP_CAT_SOVEREIGNTY(), "PARTIAL" as *u8, "prefer the sovereign format/loader" as *u8)
475 ap_row("Patent-encumbered algorithm" as *u8, AP_CAT_SOVEREIGNTY(), "PARTIAL" as *u8, "nx_patent_check (specs/nx_patent_table.txt)" as *u8)
476 ap_row("License-incompatible / non-independent-rederive source" as *u8, AP_CAT_SOVEREIGNTY(), "PARTIAL" as *u8, "nx_license_wall_audit + nx_self_audit" as *u8)
477 ap_row("Asserted-not-proven (a property claimed in a comment/TSV, not by construction)" as *u8, AP_CAT_SOVEREIGNTY(), "PARTIAL" as *u8, "prove-not-assert (nx_rung_eval) -- THE root pattern" as *u8)
478 ap_row("Ungoverned resource use / fixed cadence-or-cap (the 20fps-hard-cap class)" as *u8, AP_CAT_SOVEREIGNTY(), "PARTIAL" as *u8, "dynamic resource-based capability: compose nx_resource_governor+nx_sysload (any hw, polite, parallel-cooperative)" as *u8)
479 ap_row("Per-loop scratch mmap on the GC-free substrate (the 2.5TB clock-commit / OOM class)" as *u8, AP_CAT_RESOURCE(), "PARTIAL" as *u8, "Cardinal 21: hoist/arena-reuse (leaksweep mode = direct tier; helper-indirection tier = live VmRSS/VmSize trend witness)" as *u8)
480
481 ap_puts("\n -- SEMANTIC TIER (needs the LLM-auditor; reference vocabulary only, in the JSON) --\n" as *u8)
482 ap_row("Dark/deceptive UX patterns (bait-switch, roach-motel, confirmshaming)" as *u8, AP_CAT_DARK(), "NO\x00" as *u8, "(human/LLM judgement)" as *u8)
483 ap_row("Cargo-cult programming (code kept without understanding)" as *u8, AP_CAT_CODE_SMELL(), "NO\x00" as *u8, "(needs intent judgement)" as *u8)
484 ap_row("Premature optimization / bikeshedding / lava flow" as *u8, AP_CAT_ARCH(), "NO\x00" as *u8, "(needs context judgement)" as *u8)
485
486 // ---- GATE: prove the mechanical scanner works on real controls (prove-not-assert) ----
487 ap_puts("\n -- LIVE SCAN (prove the scanner detects on real files) --\n" as *u8)
488 ap_puts(" [DIRTY control] scanning a known-skeleton (nx_ml_dsa_65.nx -- has ERR_PENDING):\n" as *u8)
489 let dirty: i64 = ap_scan_file("runtime/nx_ml_dsa_65.nx" as *u8)
490 ap_puts(" [CLEAN-ish] scanning a proven primitive (nx_ecdsa_p256.nx):\n" as *u8)
491 let cleanish: i64 = ap_scan_file("runtime/nx_ecdsa_p256.nx" as *u8)
492
493 // liar-kill: a nonexistent file must scan to 0 (proves the scanner isn't fabricating hits)
494 let ghost: i64 = ap_scan_file("runtime/nx_does_not_exist_ap_zzz9.nx" as *u8)
495
496 // mmap-in-loop detector controls: HERMETIC synthetic buffers (not live files -- owners will FIX the live
497 // offenders, and a control that goes green when the ecosystem improves is a control coupled backwards).
498 // pos = mmap inside a while (must trip). neg = mmap before the loop + a mmap-free loop (must NOT trip).
499 let leak_pos: *u8 = "while a { let t = sys_mmap(8) }\x00" as *u8
500 let leak_neg: *u8 = "let t = sys_mmap(8)\nwhile a { x = 1 }\x00" as *u8
501 let lp: i64 = ap_count_mmap_in_loop(leak_pos, ap_slen(leak_pos))
502 let ln: i64 = ap_count_mmap_in_loop(leak_neg, ap_slen(leak_neg))
503
504 ap_puts("\n ---- SCANNER VERDICT ----\n" as *u8)
505 var ok: i64 = 1
506 if dirty < 1 { ok = 0 } // the skeleton MUST trip at least the ERR_PENDING marker
507 if ghost != 0 { ok = 0 } // a ghost file must score 0
508 if lp < 1 { ok = 0 } // mmap-inside-while control MUST trip
509 if ln != 0 { ok = 0 } // hoisted-mmap control must NOT trip
510 ap_puts(" mmap-in-loop controls: pos=" as *u8); ap_putn(lp); ap_puts(" (want>=1) neg=" as *u8); ap_putn(ln); ap_puts(" (want 0)\n" as *u8)
511 if ok == 1 {
512 ap_puts(" liar-kill PASS: dirty control tripped (" as *u8); ap_putn(dirty); ap_puts(" hits), ghost file scored 0 -> scanner PROVEN real\n" as *u8)
513 ap_puts("NX-ANTIPATTERN-CATALOG GREEN: registry live + mechanical scanner proven (composes into nx_rung_eval REPORT step)\n" as *u8)
514 sys_exit(0); return 0
515 }
516 ap_puts("NX-ANTIPATTERN-CATALOG RED: scanner failed its controls (dirty=" as *u8); ap_putn(dirty); ap_puts(" ghost=" as *u8); ap_putn(ghost); ap_puts(" leak_pos=" as *u8); ap_putn(lp); ap_puts(" leak_neg=" as *u8); ap_putn(ln); ap_puts(")\n" as *u8)
517 sys_exit(1); return 1
518}