code wiki / _hdl_build / nx_law_warden.nx
nx_law_warden.nx source
↩ module page · 1598 lines · 77354 B
1// ============================================================================================
2// STOP -- DO NOT BUILD OR PROMOTE THIS ORGAN. FREEZE 2026-07-31, debt 1785516173 (sev9).
3// The DEPLOYED binary exposes {scan | file | countfile | selftest} and runs a 20/20 gate including
4// T15 segamp, T16 gatedry, T17 helperdup, T18 scancap, T20 debt-id-parse. THIS SOURCE HAS NONE OF
5// countfile / scancap / gatedry / helperdup / T20 -- countfile appears in ZERO of 20016 .nx files
6// NAS-wide (only nx_magicratchet CALLS it). Meanwhile this source ALONE has valuenamed (seq1348),
7// which the deployed binary lacks. NEITHER SIDE IS A SUPERSET; THERE IS NO SAFE DIRECTION.
8// WHY IT MATTERS: nx_magicratchet is wired into /api/build and gates every build on
9// `nx_law_warden countfile`. Rebuild -> the verb is gone -> mr_parse_magic returns -1 -> the ratchet
10// FAILS OPEN BY DESIGN -> magic-number enforcement is permanently disarmed AND the hourly autofiler
11// stops, silently, WITH EVERY GATE STILL GREEN. One routine rebuild turns off both enforcement and
12// detection at once. Restore the four missing capabilities into this file FIRST, then build.
13// Already restored here: countfile (exact output contract, thr locked to the deployed 1024) and the
14// rewritten string/comment-aware inlinelit scanner plus teeth T21-T27.
15// ============================================================================================
16// nx_law_warden.nx -- MECHANICAL ENFORCEMENT for written laws (ws=cap-autonomy, 2026-07-20).
17// Operator: "make sure autonomously that our nishi ecosystem is getting sota ... what needs to become mcp or
18// api or raci or workflows or agents or other modern capabilities just gets logged and worked without me
19// having to call its need out"; + "our magic number audit and bug stuff dont appear to be autonomous ... lots
20// of workstreams are flagging them as bugs they hit and then just gave a new number to"; + "callouts of using
21// webrtc or webassembly ... not building from the first byte up to a sovereign nishi os and browser".
22//
23// THE GAP THIS EATS: every conformance surface we own is a CURATED manifest -- nx_favela_census states it
24// outright ("unnamed shanty stays invisible until a row names it"). A written law (CLAUDE rule 11, the
25// first-byte-up doctrine, F208 modernization) therefore has NO detector: a human must NOTICE a breach and
26// NAME it before anything tracks it. That is exactly the call-it-out tax the operator is paying. This organ
27// is the DETECTOR tier: laws live as DATA in the sovereign **lawreg- seg-store plane** (nx_store_put, same
28// substrate as raci-/debt-/frontier-), each row carrying its own detector + threshold + severity + RACI
29// owner, and the warden MEASURES live artifacts against them. NO flat TSV, NO truncating .log -- the
30// registry is a plane (provenanced hist- rows) and the evidence is an append-only .jrnl (history is sacred).
31//
32// RULE-11 COMPLIANT BY CONSTRUCTION: every POLICY number (threshold, severity, corpus, owner, token set) is
33// manifest data, never code. Point the warden at its own source and the property holds -- that is a gate tooth,
34// not a promise. The consts below are structural envelopes (buffer sizes, byte codes), all NAMED, never inline.
35//
36// DETECTORS (detector <param> over <corpus>)
37// valuenamed CLAUDE-11 breach, the GOODHART half (seq1348): a `const NAME = V` whose NAME
38// contains V's own digits (SS_MAGIC_65536 = 65536). Clears `inlinelit` while
39// explaining nothing, so part of the inlinelit improvement is a DETECTOR
40// ARTIFACT. Reported as its OWN law -- never folded into inlinelit, because a
41// number mixing two categories is not a measurement. Takes no param.
42// inlinelit <thr> CLAUDE-11 breach: standalone decimal literals >= thr on lines that are neither a
43// `const ` declaration nor a `//` comment, across *.nx in <corpus>. A NAMED const is
44// lawful; an inline literal buried in code is the magic number. Hex (0x..) and escapes
45// (\x..) and identifier-embedded digits (i64, p256) are NOT literals -- excluded.
46// token <csv> first-byte-up breach: borrowed-stack tokens present in <corpus> text (proposal
47// journals), i.e. the "just use webrtc/wasm" callouts, surfaced the moment they appear.
48// unregistered <conf> F208 MCP gap: *.elf organs in <corpus> whose basename is absent from field 0 of
49// <conf> -- a shipped capability that never became callable.
50//
51// HONEST BY CONSTRUCTION: findings are DERIVED, never asserted. A detector that cannot read its corpus reports
52// -1 = UNMEASURED (never a silent 0). Missing manifest = RED exit 1 (fail-closed, no verdict over no data).
53// Owner join (F207): owner_role must resolve in the raci- plane or the law is flagged UNOWNED.
54// VERDICT=GREEN means THE WARDEN MEASURED, not that the ecosystem is clean -- breaches are reported as a
55// separate count so a cron watcher can trend them without the gate lying.
56//
57// nx_law_warden {scan [planeprefix] | selftest} (plane default knowledge/store/lawreg-)
58// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
59import "nx_store_seed_lib.nx"
60import "nx_syscalls.nx"
61import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ's verdict
62import "nx_tool_run.nx"
63import "nx_gate_verdict.nx"
64
65const LW_OUT: i64 = 262144
66const LW_DIRBUF: i64 = 131072
67const LW_PLANE: i64 = 1048576
68const LW_PATH: i64 = 512
69const LW_NAMEMAX: i64 = 256
70const LW_SCRATCH: i64 = 64
71const LW_FILECAP: i64 = 600
72const LW_COLS: i64 = 8
73const LW_COLS_MAX: i64 = 9
74const LW_FLD_BYTES: i64 = 128
75const LW_OVF: i64 = 100000000000
76const LW_MODE: i64 = 0x1a4
77const LW_NL: i64 = 10
78const LW_TAB: i64 = 9
79const LW_HASH: i64 = 35
80const LW_SP: i64 = 32
81const LW_COMMA: i64 = 44
82const LW_DQ: i64 = 34
83const LW_BSL: i64 = 92
84const LW_SL: i64 = 47
85// countfile's threshold. RULE-11 NOTE, deliberate: this is a NAMED CONST and NOT a lawreg- plane row,
86// because /api/build gates on countfile via nx_magicratchet and must NOT acquire a runtime dependency on
87// plane readability -- a plane hiccup would silently re-denominate every baseline in
88// knowledge/status/magicbase/*.cnt. CONTRACT-LOCKED to the deployed binary's value (rule 19): changing it
89// re-baselines every organ at once. Moving it to the manifest is filed as its own rung, not done inline.
90const LW_MAGIC_THR: i64 = 1024
91const LW_MODE_LIT: i64 = 1
92const LW_MODE_ELF: i64 = 2
93const LW_MODE_TOK: i64 = 3
94const LW_SEGMAX: i64 = 64
95const LW_SEGNAME: i64 = 64
96const LW_SEGTAB: i64 = 4096
97const LW_SEGCNT: i64 = 512
98const LW_DIRMODE: i64 = 0x1ed
99const LW_ST_SLOTS: i64 = 64
100
101// ---------- byte/string primitives ----------
102func lw_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
103func lw_cat(o: *u8, at: i64, s: *u8) -> i64 { var a: i64=at; var i: i64=0; while s[i]!=(0 as u8){o[a]=s[i]; a=a+1; i=i+1} return a }
104func lw_catf(o: *u8, at: i64, p: *u8, n: i64) -> i64 { var a: i64=at; var i: i64=0; while i<n { o[a]=p[i]; a=a+1; i=i+1 } return a }
105// JSON-safe copy: quote/backslash/control bytes neutralised so a source sample can never break the envelope
106func lw_catesc(o: *u8, at: i64, p: *u8, n: i64) -> i64 {
107 var a: i64=at
108 var i: i64=0
109 while i<n {
110 let c: i64 = p[i] as i64
111 if c==34 { o[a]=39 as u8 } else { if c==92 { o[a]=47 as u8 } else { if c<32 { o[a]=32 as u8 } else { o[a]=p[i] } } }
112 a=a+1
113 i=i+1
114 }
115 return a
116}
117func lw_catn(o: *u8, at: i64, v: i64) -> i64 {
118 var a: i64=at
119 var x: i64=v
120 if x<0 { o[a]=45 as u8; a=a+1; x=0-x }
121 let tm: *u8=sys_mmap(LW_SCRATCH)
122 var k: i64=0
123 if x==0 { tm[0]=48 as u8; k=1 }
124 while x>0 { tm[k]=(48+x%10) as u8; x=x/10; k=k+1 }
125 var j: i64=0
126 while j<k { o[a]=tm[k-1-j]; a=a+1; j=j+1 }
127 return a
128}
129func lw_count(buf: *u8, n: i64, needle: *u8) -> i64 {
130 var nl: i64=0
131 while needle[nl]!=(0 as u8){nl=nl+1}
132 if nl==0 { return 0 }
133 var c: i64=0
134 var i: i64=0
135 while i+nl<=n {
136 var j: i64=0
137 var ok: i64=1
138 while j<nl { if buf[i+j]!=needle[j] { ok=0; j=nl } else { j=j+1 } }
139 if ok==1 { c=c+1; i=i+nl } else { i=i+1 }
140 }
141 return c
142}
143func lw_span_eq(b: *u8, s: i64, l: i64, lit: *u8) -> i64 {
144 let n: i64 = lw_slen(lit)
145 if n!=l { return 0 }
146 var i: i64=0
147 while i<n { if b[s+i]!=lit[i] { return 0 } i=i+1 }
148 return 1
149}
150func lw_ends(nm: *u8, suf: *u8) -> i64 {
151 let n: i64=lw_slen(nm)
152 let s: i64=lw_slen(suf)
153 if s>n { return 0 }
154 var i: i64=0
155 while i<s { if nm[n-s+i]!=suf[i] { return 0 } i=i+1 }
156 return 1
157}
158func lw_join(dst: *u8, dir: *u8, nm: *u8) -> i64 {
159 var a: i64=0
160 a=lw_cat(dst,a,dir)
161 dst[a]=47 as u8
162 a=a+1
163 a=lw_cat(dst,a,nm)
164 dst[a]=0 as u8
165 return a
166}
167func lw_wfile(path: *u8, content: *u8) -> i64 {
168 let fd: i64=sys_openat_wr(path, LW_MODE)
169 if fd<0 { return 0-1 }
170 sys_write(fd, content, lw_slen(content))
171 sys_close(fd)
172 return 0
173}
174
175// ---------- detector: inlinelit (CLAUDE rule 11) ----------
176func lw_line_start(b: *u8, ls: i64, le: i64) -> i64 {
177 var p: i64=ls
178 var go: i64=1
179 while go==1 { if p>=le { go=0 } else { if b[p]==(LW_SP as u8) { p=p+1 } else { go=0 } } }
180 return p
181}
182// a `const ` declaration is the LAWFUL form of a big number -- exempt
183func lw_is_const(b: *u8, ls: i64, le: i64) -> i64 {
184 let p: i64 = lw_line_start(b,ls,le)
185 if p+6>le { return 0 }
186 if b[p]!=(99 as u8) { return 0 }
187 if b[p+1]!=(111 as u8) { return 0 }
188 if b[p+2]!=(110 as u8) { return 0 }
189 if b[p+3]!=(115 as u8) { return 0 }
190 if b[p+4]!=(116 as u8) { return 0 }
191 if b[p+5]!=(32 as u8) { return 0 }
192 return 1
193}
194func lw_is_comment(b: *u8, ls: i64, le: i64) -> i64 {
195 let p: i64 = lw_line_start(b,ls,le)
196 if p+2>le { return 0 }
197 if b[p]!=(47 as u8) { return 0 }
198 if b[p+1]!=(47 as u8) { return 0 }
199 return 1
200}
201// standalone decimal literals >= thr in the CODE PART of one line. Digits inside a "string literal" are
202// DATA (a port in a message, a date in a URL), and everything after the first // that is OUTSIDE a string
203// is a comment -- neither is code, so neither can be a rule-11 magic number.
204// ROOT FIX 2026-07-31 (ws=law-l006-instrument, debt 1785515802): BOTH exclusions were missing. lw_file_lits
205// guarded with lw_is_comment, which only recognises a WHOLE-LINE comment, so the // TAIL of a code line was
206// scanned as code and a dated trailing comment counted its year as a magic number. Because nx_magicratchet
207// gates /api/build on this count, DATING A TRAILING COMMENT REFUSED REAL BUILDS (debt 1785050172, hit live
208// on nx_mmbench). The absent string state also made this counter disagree with nx_magic map, which already
209// advertised skipped=in-string -- two lexers behind one "ONE canonical counter" claim. Proven by negative
210// control (full-line comment 0, trailing comment 1, genuine literal 1), never by inspection.
211// A digit run preceded by a letter or '_' belongs to an
212// identifier (i64, p256), a hex body (0x1a4) or an escape (\x22) -- never counted.
213func lw_line_lits(b: *u8, ls: i64, le: i64, thr: i64) -> i64 {
214 var hits: i64=0
215 var p: i64=ls
216 var instr: i64=0
217 while p<le {
218 let c: i64 = b[p] as i64
219 if instr==1 {
220 if c==LW_BSL { p=p+2 } else {
221 if c==LW_DQ { instr=0 }
222 p=p+1
223 }
224 } else {
225 var isd: i64=0
226 if c>=48 { if c<=57 { isd=1 } }
227 if c==LW_DQ { instr=1; p=p+1 } else {
228 if isd==0 {
229 var cut: i64=0
230 if c==LW_SL { if p+1<le { if b[p+1]==(LW_SL as u8) { cut=1 } } }
231 if cut==1 { p=le } else { p=p+1 }
232 } else {
233 var skip: i64=0
234 if p>ls {
235 let q: i64 = b[p-1] as i64
236 if q==95 { skip=1 }
237 if q>=97 { if q<=122 { skip=1 } }
238 if q>=65 { if q<=90 { skip=1 } }
239 }
240 var v: i64=0
241 var ov: i64=0
242 var run: i64=1
243 while run==1 {
244 if p>=le { run=0 } else {
245 let d: i64 = b[p] as i64
246 var isd2: i64=0
247 if d>=48 { if d<=57 { isd2=1 } }
248 if isd2==1 {
249 if v>LW_OVF { ov=1 } else { v=v*10+(d-48) }
250 p=p+1
251 } else { run=0 }
252 }
253 }
254 if skip==0 { if ov==1 { hits=hits+1 } else { if v>=thr { hits=hits+1 } } }
255 }
256 }
257 }
258 }
259 return hits
260}
261func lw_file_lits(path: *u8, thr: i64) -> i64 {
262 let szp: *i64 = sys_mmap(LW_SCRATCH) as *i64
263 let f: *u8 = sys_read_file(path, szp)
264 if (f as i64)==0 { return 0-1 }
265 let n: i64 = szp[0]
266 var hits: i64=0
267 var i: i64=0
268 while i<n {
269 let ls: i64=i
270 var le: i64=ls
271 var go: i64=1
272 while go==1 { if le>=n { go=0 } else { if f[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
273 i=le+1
274 if lw_is_comment(f,ls,le)==0 { if lw_is_const(f,ls,le)==0 { hits=hits+lw_line_lits(f,ls,le,thr) } }
275 }
276 return hits
277}
278
279// ---------- verb: countfile ----------
280// The ONE canonical inline-magic count for a SINGLE file. nx_magicratchet is wired into /api/build and
281// gates every build on this exact contract, so it is load-bearing infrastructure, not a convenience verb.
282// OUTPUT CONTRACT, byte-stable (rule 19): exactly {"magic":N} on stdout, exit 0. N = -1 means UNMEASURED
283// (corpus unreadable) and the ratchet FAILS OPEN on it BY DESIGN -- a false refusal is worse than a miss.
284// RESTORED TO SOURCE 2026-07-31 (ws=law-l006-instrument, debt 1785516173): the deployed binary answered
285// `countfile` while the string appeared in ZERO of 20016 .nx files NAS-wide, so any rebuild would have
286// dropped the verb, made mr_parse_magic return -1 forever, and DISARMED the magic ratchet silently with
287// every gate still GREEN. Source must be a superset of the artifact before either is allowed to move.
288func lw_countfile(path: *u8) -> i64 {
289 let n: i64 = lw_file_lits(path, LW_MAGIC_THR)
290 let ob: *u8 = sys_mmap(LW_PATH)
291 var o: i64 = 0
292 o=lw_cat(ob,o,"{\x22magic\x22:" as *u8)
293 o=lw_catn(ob,o,n)
294 o=lw_cat(ob,o,"}" as *u8)
295 sys_write(1,ob,o)
296 sys_write(1,"\n" as *u8,1)
297 return 0
298}
299
300// ---------- detector: valuenamed (CLAUDE rule 11, the GOODHART half) ----------
301// WHY THIS EXISTS (filed as seq1348, built 2026-07-30). The `inlinelit` detector above counts inline
302// numeric literals and EXEMPTS `const ` lines, because a named constant is the lawful form of a big
303// number. That exemption is being gamed: nx_seg_store.nx alone declares 49 constants of the shape
304// const SS_MAGIC_65536: i64 = 65536
305// A CONSTANT NAMED AFTER ITS OWN VALUE CARRIES ZERO SEMANTIC CONTENT. It clears inlinelit while leaving
306// the code exactly as unexplainable as before -- nobody can still answer WHY 65536, which is the whole
307// point of rule 11 (you should be able to point at a config key, not a code comment). So part of the
308// measured seq274/seq315 improvement is a DETECTOR ARTIFACT, not a real reduction in unexplained
309// constants. Same class this lane keeps finding: an instrument reporting success for work not done.
310//
311// REPORTED AS ITS OWN LAW, NEVER FOLDED INTO inlinelit -- ★LAW: a number mixing two categories is not
312// a measurement. Zero false positives BY CONSTRUCTION: it fires only when the declared NAME literally
313// contains the decimal digits of its OWN value, which is never meaningful naming.
314// NOT a lint on placement -- the goal is EXPLICABILITY. Remediate by renaming to intent
315// (SS_READ_CHUNK_BYTES, SS_FNV64_PRIME, SS_UNICODE_HANGUL_LO) or moving to a config row. Re-inlining
316// the literal is NOT a fix and would merely move the breach back to inlinelit.
317const LW_MODE_VNAME: i64 = 4
318// L009/L010/L011 detectors, RECONSTRUCTED 2026-07-31 (debt 1785516173): the deployed binary has these
319// three and NO source tree did, so a rebuild would have dropped them. Semantics taken VERBATIM from the
320// lawreg- plane law rows, and each predicate was compiled + linked + RUN standalone with a POSITIVE and
321// TWO NEGATIVE controls before being wired here (_offc/l006_detectors_recon.nx, exit 9).
322// RECONSTRUCTED IS NOT RECOVERED: these are correct-to-spec, not byte-equivalent to the lost originals.
323const LW_MODE_GATEDRY: i64 = 5
324const LW_MODE_HELPERDUP: i64 = 6
325const LW_MODE_SCANCAP: i64 = 7
326
327// digits of `v` into out (no NUL); returns length. v>=0.
328func lw_digits(v: i64, out: *u8) -> i64 {
329 if v==0 { out[0]=48 as u8; return 1 }
330 let t: *u8 = sys_mmap(32)
331 var m: i64=v
332 var k: i64=0
333 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
334 var i: i64=0
335 while i<k { out[i]=t[k-1-i]; i=i+1 }
336 return k
337}
338// does the byte span [s,e) contain `pat` (len pl)?
339func lw_span_has(b: *u8, s: i64, e: i64, pat: *u8, pl: i64) -> i64 {
340 if pl<=0 { return 0 }
341 var i: i64=s
342 while i+pl<=e {
343 var j: i64=0
344 var ok: i64=1
345 while j<pl { if b[i+j]!=pat[j] { ok=0; j=pl } else { j=j+1 } }
346 if ok==1 { return 1 }
347 i=i+1
348 }
349 return 0
350}
351// 1 iff this line is `const NAME...= VALUE` where NAME contains VALUE's decimal digits.
352// Parses the name span (after "const ", up to ':' or '=') and the trailing decimal value.
353func lw_line_valuenamed(b: *u8, ls: i64, le: i64) -> i64 {
354 if lw_is_const(b,ls,le)==0 { return 0 }
355 let p0: i64 = lw_line_start(b,ls,le)
356 let ns: i64 = p0+6 // past "const "
357 if ns>=le { return 0 }
358 // name ends at the first ':' or '=' or space
359 var ne: i64 = ns
360 var go: i64 = 1
361 while go==1 {
362 if ne>=le { go=0 } else {
363 let c: i64 = b[ne] as i64
364 if c==58 { go=0 } else { if c==61 { go=0 } else { if c==32 { go=0 } else { ne=ne+1 } } }
365 }
366 }
367 if ne<=ns { return 0 }
368 // find '=' then the first decimal run after it (skip 0x.. hex: a '0' followed by 'x' is not decimal)
369 var q: i64 = ne
370 var eq: i64 = 0-1
371 while q<le { if b[q]==(61 as u8) { eq=q; q=le } else { q=q+1 } }
372 if eq<0 { return 0 }
373 var r: i64 = eq+1
374 var v: i64 = 0
375 var seen: i64 = 0
376 var scan: i64 = 1
377 while scan==1 {
378 if r>=le { scan=0 } else {
379 let d: i64 = b[r] as i64
380 var isd: i64=0
381 if d>=48 { if d<=57 { isd=1 } }
382 if isd==1 {
383 // reject hex bodies: '0' immediately followed by 'x'
384 if r+1<le { if b[r]==(48 as u8) { if b[r+1]==(120 as u8) { return 0 } } }
385 if v>LW_OVF { return 0 }
386 v=v*10+(d-48); seen=1; r=r+1
387 } else { if seen==1 { scan=0 } else { r=r+1 } }
388 }
389 }
390 if seen==0 { return 0 }
391 if v<10 { return 0 } // single digits appear in names innocently (V2, P1)
392 let db: *u8 = sys_mmap(32)
393 let dl: i64 = lw_digits(v,db)
394 return lw_span_has(b,ns,ne,db,dl)
395}
396func lw_file_valuenamed(f: *u8, n: i64) -> i64 {
397 var hits: i64=0
398 var i: i64=0
399 while i<n {
400 let ls: i64=i
401 var le: i64=ls
402 var go: i64=1
403 while go==1 { if le>=n { go=0 } else { if f[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
404 i=le+1
405 if lw_is_comment(f,ls,le)==0 { hits=hits+lw_line_valuenamed(f,ls,le) }
406 }
407 return hits
408}
409
410// ---------- detector: unregistered (F208 MCP gap) ----------
411// field 0 of a non-comment allowlist line == the tool name (nx_bench_census_lib: field 0 = the DENOMINATOR)
412func lw_field0_has(buf: *u8, n: i64, name: *u8) -> i64 {
413 let l: i64=lw_slen(name)
414 if l==0 { return 0 }
415 if n<=0 { return 0 }
416 var i: i64=0
417 while i+l<=n {
418 var atstart: i64=0
419 if i==0 { atstart=1 } else { if buf[i-1]==(LW_NL as u8) { atstart=1 } }
420 if atstart==1 {
421 var j: i64=0
422 var ok: i64=1
423 while j<l { if buf[i+j]!=name[j] { ok=0; j=l } else { j=j+1 } }
424 if ok==1 {
425 var term: i64=0
426 if i+l>=n { term=1 } else {
427 let c: i64=buf[i+l] as i64
428 if c==LW_TAB { term=1 }
429 if c==LW_SP { term=1 }
430 }
431 if term==1 { return 1 }
432 }
433 }
434 i=i+1
435 }
436 return 0
437}
438
439// ---------- detector: segamp (seg-store SEGMENT AMPLIFICATION) ----------
440// THE CLASS THAT TOOK THE BOX DOWN 2026-07-20: sts_seed commits the WHOLE plane as a NEW segment on every
441// write, so a plane's segment count grows without bound and every read walks all of them (O(rows x segs)).
442// MEASURED that day: debt- at ~464 segments / 42MB for ~200KB of rows; one add >15min then FAILED; a page
443// OOM-killed; the NAS went to load 17 and the whole sovereign stack became unreachable. Nothing detected it
444// -- it was found by hand, after the outage. This detector makes the class mechanically visible BEFORE the
445// next OOM. Counts one segment per <plane>-seg-N.docs and flags any plane over a DATA-DRIVEN threshold.
446// Separate walk (not lw_walk) on purpose: this one ACCUMULATES per-prefix state rather than per-file counts.
447func lw_segamp(dir: *u8, thr: i64, cx: *i64, worst: *u8) -> i64 {
448 let fd: i64=sys_openat_rd(dir)
449 if fd<0 { return 0-1 }
450 let dbuf: *u8=sys_mmap(LW_DIRBUF)
451 let names: *u8=sys_mmap(LW_SEGTAB)
452 let counts: *i64=sys_mmap(LW_SEGCNT) as *i64
453 var np: i64=0
454 var go: i64=1
455 while go==1 {
456 let nr: i64=sys_getdents64(fd,dbuf,LW_DIRBUF)
457 if nr<=0 { go=0 } else {
458 var off: i64=0
459 while off<nr {
460 let rec: *u8=(dbuf as i64+off) as *u8
461 let nm: *u8=dirent_name(rec)
462 if lw_ends(nm,".docs\x00" as *u8)==1 {
463 let ln: i64=lw_slen(nm)
464 var pos: i64=0-1
465 var i2: i64=0
466 while i2+5<=ln {
467 if nm[i2]==(45 as u8) { if nm[i2+1]==(115 as u8) { if nm[i2+2]==(101 as u8) { if nm[i2+3]==(103 as u8) { if nm[i2+4]==(45 as u8) { if pos<0 { pos=i2 } } } } } }
468 i2=i2+1
469 }
470 if pos>0 { if pos<LW_SEGNAME {
471 var found: i64=0-1
472 var k: i64=0
473 while k<np {
474 var same: i64=1
475 var j2: i64=0
476 while j2<pos { if names[k*LW_SEGNAME+j2]!=nm[j2] { same=0; j2=pos } else { j2=j2+1 } }
477 if same==1 { if names[k*LW_SEGNAME+pos]==(0 as u8) { found=k; k=np } }
478 k=k+1
479 }
480 if found<0 {
481 if np<LW_SEGMAX {
482 var j3: i64=0
483 while j3<pos { names[np*LW_SEGNAME+j3]=nm[j3]; j3=j3+1 }
484 names[np*LW_SEGNAME+pos]=0 as u8
485 counts[np]=1
486 np=np+1
487 } else { cx[3]=1 }
488 } else { counts[found]=counts[found]+1 }
489 } }
490 }
491 off=off+dirent_reclen(rec)
492 }
493 }
494 }
495 sys_close(fd)
496 cx[0]=np
497 var over: i64=0
498 var mx: i64=0
499 var mi: i64=0-1
500 var k2: i64=0
501 while k2<np {
502 if counts[k2]>thr { over=over+1 }
503 if counts[k2]>mx { mx=counts[k2]; mi=k2 }
504 k2=k2+1
505 }
506 cx[1]=over
507 cx[2]=mx
508 if mi>=0 {
509 var w: i64=0
510 w=lw_cat(worst,w,(names as i64+mi*LW_SEGNAME) as *u8)
511 worst[w]=58 as u8
512 w=w+1
513 w=lw_catn(worst,w,mx)
514 worst[w]=0 as u8
515 }
516 return 0
517}
518
519// ---------- shared corpus walk ----------
520// cx[0]=mode cx[1]=thr cx[2]=files cx[3]=hits cx[4]=worsthits cx[5]=capped cx[6]=allowlist_bytes
521// ---------- T20: parse the debt id out of the nx_debt reply ----------
522// RECONSTRUCTED 2026-07-31 (debt 1785516173). The deployed gate's tooth reads "the debt id is parsed from
523// the nx_debt reply; absent -> -1, NEVER A WRONG ID", and the "never a wrong id" half is the whole point.
524// THE TRAP, hit live today: nx_debt's duplicate reply is
525// DEBT-DUPLICATE-SKIPPED existing_idx=2003 -- identical desc already filed; ...
526// A naive search for "id=" matches INSIDE "existing_idx=" and returns 2003 -- a real row id belonging to a
527// DIFFERENT debt. That is worse than returning nothing: the warden would stamp law_filed.jrnl with an id
528// that closes someone else's row. So this anchors on the FULL success token and returns -1 for duplicate,
529// empty, truncated and error-page replies. Compiled + linked + RUN standalone (2 POS + 5 NEG) before wiring.
530func lw_debt_id(b: *u8, n: i64) -> i64 {
531 let tok: *u8 = "DEBT-ADDED id=" as *u8
532 let m: i64 = lw_slen(tok)
533 if m>n { return 0-1 }
534 var at: i64 = 0-1
535 var i: i64 = 0
536 while i+m <= n {
537 var j: i64 = 0
538 var ok: i64 = 1
539 while j < m { if b[i+j] != tok[j] { ok=0; j=m } else { j=j+1 } }
540 if ok==1 { at=i; i=n } else { i=i+1 }
541 }
542 if at < 0 { return 0-1 }
543 var p: i64 = at + m
544 var v: i64 = 0
545 var got: i64 = 0
546 while p < n {
547 let c: i64 = b[p] as i64
548 if c >= 48 { if c <= 57 { v = v*10 + (c-48); got = 1; p = p+1 } else { p = n } } else { p = n }
549 }
550 if got == 0 { return 0-1 }
551 return v
552}
553
554// ---------- detectors: gatedry (L009) / helperdup (L010) / scancap (L011) ----------
555// Each returns 1 if THIS FILE breaches, else 0, so the walk's per-file accounting stays uniform with
556// LW_MODE_LIT and LW_MODE_VNAME. Reuses lw_count rather than reimplementing substring search (L010 is
557// literally the law against reimplementing a shared primitive -- these detectors must not breach it).
558
559// L009 gatedry, rule=CLAUDE-6-15-DRY: "gate organs that roll their own verdict instead of inheriting the
560// nx_gate_verdict base class -- the D001 OO-consolidation duplication".
561func lw_file_gatedry(b: *u8, n: i64) -> i64 {
562 if lw_count(b,n,"verdict" as *u8)==0 { return 0 }
563 if lw_count(b,n,"nx_gate_verdict" as *u8)>0 { return 0 }
564 if lw_count(b,n,"gv_verdict" as *u8)>0 { return 0 }
565 return 1
566}
567
568// L010 helperdup, rule=CLAUDE-15-DRY: "organs that reimplement the putn/itoa emit primitive instead of
569// importing the nx_estr shared base".
570func lw_file_helperdup(b: *u8, n: i64) -> i64 {
571 var defines: i64 = 0
572 if lw_count(b,n,"func putn" as *u8)>0 { defines=1 }
573 if lw_count(b,n,"_putn(" as *u8)>0 { defines=1 }
574 if lw_count(b,n,"itoa" as *u8)>0 { defines=1 }
575 if defines==0 { return 0 }
576 if lw_count(b,n,"nx_estr" as *u8)>0 { return 0 }
577 return 1
578}
579
580// L011 scancap, rule=OPERATOR-HONESTY-selfceiling: "corpus scanners that walk a directory but HIDE their
581// coverage (no scanned/coverage_complete/capped honesty flag) -- presenting partial coverage AS complete".
582// The law names three exemptions -- an honest scanner, a non-scanner, and a PROSE-ONLY mention -- so the
583// walk evidence must be a CALL, not the word appearing in a comment.
584func lw_file_scancap(b: *u8, n: i64) -> i64 {
585 var walks: i64 = 0
586 if lw_count(b,n,"sys_getdents" as *u8)>0 { walks=1 }
587 if lw_count(b,n,"getdents64(" as *u8)>0 { walks=1 }
588 if walks==0 { return 0 }
589 if lw_count(b,n,"capped" as *u8)>0 { return 0 }
590 if lw_count(b,n,"coverage_complete" as *u8)>0 { return 0 }
591 if lw_count(b,n,"scanned" as *u8)>0 { return 0 }
592 return 1
593}
594
595func lw_walk(dir: *u8, cx: *i64, worst: *u8, abuf: *u8) -> i64 {
596 let fd: i64=sys_openat_rd(dir)
597 if fd<0 { return 0-1 }
598 let dbuf: *u8=sys_mmap(LW_DIRBUF)
599 let path: *u8=sys_mmap(LW_PATH)
600 let base: *u8=sys_mmap(LW_NAMEMAX)
601 var go: i64=1
602 while go==1 {
603 let nr: i64=sys_getdents64(fd,dbuf,LW_DIRBUF)
604 if nr<=0 { go=0 } else {
605 var off: i64=0
606 while off<nr {
607 let rec: *u8=(dbuf as i64+off) as *u8
608 let nm: *u8=dirent_name(rec)
609 if cx[2]>=LW_FILECAP { cx[5]=1 } else {
610 if cx[0]==LW_MODE_LIT {
611 if lw_ends(nm,".nx\x00" as *u8)==1 {
612 lw_join(path,dir,nm)
613 let h: i64=lw_file_lits(path,cx[1])
614 if h>=0 {
615 cx[2]=cx[2]+1
616 cx[3]=cx[3]+h
617 if h>cx[4] { cx[4]=h; let ln: i64=lw_slen(nm); lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
618 }
619 }
620 }
621 // valuenamed (seq1348): consts named after their own value -- the inlinelit exemption
622 // being gamed. Same walk/accounting shape as LW_MODE_LIT so the roll-up is uniform.
623 if cx[0]==LW_MODE_VNAME {
624 if lw_ends(nm,".nx\x00" as *u8)==1 {
625 lw_join(path,dir,nm)
626 let vszp: *i64 = sys_mmap(LW_SCRATCH) as *i64
627 let vb: *u8 = sys_read_file(path,vszp)
628 if (vb as i64)!=0 {
629 let h: i64=lw_file_valuenamed(vb,vszp[0])
630 cx[2]=cx[2]+1
631 cx[3]=cx[3]+h
632 if h>cx[4] { cx[4]=h; let ln: i64=lw_slen(nm); lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
633 }
634 }
635 }
636 // L009/L010/L011 (reconstructed 2026-07-31): identical walk/accounting shape to
637 // LW_MODE_VNAME so the roll-up stays uniform. Each per-file predicate returns 1 for a
638 // breaching file, so cx[3] counts BREACHING FILES and cx[2] counts files SCANNED --
639 // which is what the law rows' measured/scanned pair means.
640 if cx[0]==LW_MODE_GATEDRY {
641 if lw_ends(nm,".nx\x00" as *u8)==1 {
642 lw_join(path,dir,nm)
643 let gszp: *i64 = sys_mmap(LW_SCRATCH) as *i64
644 let gb: *u8 = sys_read_file(path,gszp)
645 if (gb as i64)!=0 {
646 let h: i64=lw_file_gatedry(gb,gszp[0])
647 cx[2]=cx[2]+1
648 cx[3]=cx[3]+h
649 if h>cx[4] { cx[4]=h; let ln: i64=lw_slen(nm); lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
650 }
651 }
652 }
653 if cx[0]==LW_MODE_HELPERDUP {
654 if lw_ends(nm,".nx\x00" as *u8)==1 {
655 lw_join(path,dir,nm)
656 let dszp: *i64 = sys_mmap(LW_SCRATCH) as *i64
657 let db2: *u8 = sys_read_file(path,dszp)
658 if (db2 as i64)!=0 {
659 let h: i64=lw_file_helperdup(db2,dszp[0])
660 cx[2]=cx[2]+1
661 cx[3]=cx[3]+h
662 if h>cx[4] { cx[4]=h; let ln: i64=lw_slen(nm); lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
663 }
664 }
665 }
666 if cx[0]==LW_MODE_SCANCAP {
667 if lw_ends(nm,".nx\x00" as *u8)==1 {
668 lw_join(path,dir,nm)
669 let sszp: *i64 = sys_mmap(LW_SCRATCH) as *i64
670 let sb2: *u8 = sys_read_file(path,sszp)
671 if (sb2 as i64)!=0 {
672 let h: i64=lw_file_scancap(sb2,sszp[0])
673 cx[2]=cx[2]+1
674 cx[3]=cx[3]+h
675 if h>cx[4] { cx[4]=h; let ln: i64=lw_slen(nm); lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
676 }
677 }
678 }
679 // borrowed-stack tokens ACROSS A SOURCE TREE (not just a journal): the callouts the
680 // operator keeps seeing live in code and plans, so the corpus must be the tree itself.
681 // abuf carries the csv token list for this mode (documented, mode-specific contract).
682 if cx[0]==LW_MODE_TOK {
683 if lw_ends(nm,".nx\x00" as *u8)==1 {
684 lw_join(path,dir,nm)
685 let tsz: *i64 = sys_mmap(LW_SCRATCH) as *i64
686 let tbuf: *u8 = sys_read_file(path,tsz)
687 if (tbuf as i64)!=0 {
688 cx[2]=cx[2]+1
689 let tk: *u8 = sys_mmap(LW_NAMEMAX)
690 var ti: i64=0
691 var tl2: i64=0
692 var tdone: i64=0
693 var fhits: i64=0
694 while tdone==0 {
695 let cc: i64=abuf[ti] as i64
696 var flu: i64=0
697 if cc==0 { flu=1; tdone=1 }
698 if cc==LW_COMMA { flu=1 }
699 if flu==1 {
700 if tl2>0 { tk[tl2]=0 as u8; fhits=fhits+lw_count(tbuf,tsz[0],tk) }
701 tl2=0
702 } else { tk[tl2]=abuf[ti]; tl2=tl2+1 }
703 ti=ti+1
704 }
705 if fhits>0 {
706 cx[3]=cx[3]+fhits
707 if fhits>cx[4] { cx[4]=fhits; let lnt: i64=lw_slen(nm); lw_catf(worst,0,nm,lnt); worst[lnt]=0 as u8 }
708 }
709 }
710 }
711 }
712 if cx[0]==LW_MODE_ELF {
713 if lw_ends(nm,".elf\x00" as *u8)==1 {
714 let ln: i64=lw_slen(nm)
715 let bl: i64=ln-4
716 if bl>0 {
717 cx[2]=cx[2]+1
718 var k: i64=0
719 while k<bl { base[k]=nm[k]; k=k+1 }
720 base[bl]=0 as u8
721 // fail-CLOSED: a zero-length basename would make the lookup vacuously "absent"
722 // and brand every organ unregistered -- only count when we truly resolved a name
723 let blen: i64=lw_slen(base)
724 if blen==bl {
725 let reg: i64=lw_field0_has(abuf,cx[6],base)
726 if reg==0 {
727 cx[3]=cx[3]+1
728 if cx[4]==0 { cx[4]=1; lw_catf(worst,0,nm,ln); worst[ln]=0 as u8 }
729 }
730 }
731 }
732 }
733 }
734 }
735 off=off+dirent_reclen(rec)
736 }
737 }
738 }
739 sys_close(fd)
740 return 0
741}
742
743// ---------- detector: token (first-byte-up doctrine) ----------
744// cx[0]=total occurrences cx[1]=distinct tokens present ; -1 if corpus unreadable (UNMEASURED)
745// count needle across lines, SKIPPING any line that contains excl. Why: the warden's own reports land in
746// the same journals it scans ("top hit: wasm"), so a naive buffer-wide count measures itself and inflates
747// the finding every beat. Exclusion is DATA (optional 9th law column), never a hardcoded self-reference.
748func lw_count_lines(b: *u8, n: i64, needle: *u8, excl: *u8) -> i64 {
749 let el: i64 = lw_slen(excl)
750 var total: i64 = 0
751 var i: i64 = 0
752 while i<n {
753 let ls: i64=i
754 var le: i64=ls
755 var go: i64=1
756 while go==1 { if le>=n { go=0 } else { if b[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
757 i=le+1
758 var skip: i64=0
759 if el>0 { if lw_count((b as i64+ls) as *u8, le-ls, excl)>0 { skip=1 } }
760 if skip==0 { total=total+lw_count((b as i64+ls) as *u8, le-ls, needle) }
761 }
762 return total
763}
764func lw_token_scan(corpus: *u8, csv: *u8, cx: *i64, worst: *u8, excl: *u8) -> i64 {
765 let szp: *i64=sys_mmap(LW_SCRATCH) as *i64
766 let b: *u8=sys_read_file(corpus,szp)
767 if (b as i64)==0 { return 0-1 }
768 let n: i64=szp[0]
769 let tok: *u8=sys_mmap(LW_NAMEMAX)
770 worst[0]=0 as u8
771 var i: i64=0
772 var tl: i64=0
773 var done: i64=0
774 while done==0 {
775 let c: i64=csv[i] as i64
776 var flush: i64=0
777 if c==0 { flush=1; done=1 }
778 if c==LW_COMMA { flush=1 }
779 if flush==1 {
780 if tl>0 {
781 tok[tl]=0 as u8
782 let h: i64=lw_count_lines(b,n,tok,excl)
783 if h>0 {
784 cx[0]=cx[0]+h
785 cx[1]=cx[1]+1
786 if lw_slen(worst)==0 { lw_catf(worst,0,tok,tl); worst[tl]=0 as u8 }
787 }
788 }
789 tl=0
790 } else { tok[tl]=csv[i]; tl=tl+1 }
791 i=i+1
792 }
793 return 0
794}
795
796// ---------- file: the autonomy half -- breaches become OWNED work with no human in the loop ----------
797// EVIDENCE-DERIVED, never re-asserted: reads the append-only law_warden.jrnl the scan already wrote and
798// acts on the LATEST scan block only. IDEMPOTENT (rule 10): a law already present in law_filed.jrnl is
799// never re-filed, so an hourly beat does not spam the debt plane. DRY (rule 15): the debt row is written
800// by FORKING nx_debt -- the sibling organ stays the single writer of its own plane.
801
802// ts of the final frame = the most recent scan block
803func lw_last_ts(buf: *u8, n: i64) -> i64 {
804 var ts: i64 = 0
805 var i: i64 = 0
806 var ls: i64 = 0
807 while i<n {
808 if buf[i]==(LW_NL as u8) {
809 if i>ls {
810 var v: i64=0
811 var k: i64=ls
812 var go: i64=1
813 while go==1 {
814 if k>=i { go=0 } else {
815 let d: i64=buf[k] as i64
816 if d>=48 { if d<=57 { v=v*10+(d-48); k=k+1 } else { go=0 } } else { go=0 }
817 }
818 }
819 if v>0 { ts=v }
820 }
821 ls=i+1
822 }
823 i=i+1
824 }
825 return ts
826}
827// has <lawid> already been filed? matches the exact framed token \tFILED\t<lawid>\t
828func lw_filed_has(buf: *u8, n: i64, lawid: *u8) -> i64 {
829 if n<=0 { return 0 }
830 let pat: *u8 = sys_mmap(LW_NAMEMAX)
831 var p: i64 = 0
832 pat[p]=LW_TAB as u8
833 p=p+1
834 p=lw_cat(pat,p,"FILED" as *u8)
835 pat[p]=LW_TAB as u8
836 p=p+1
837 p=lw_cat(pat,p,lawid)
838 pat[p]=LW_TAB as u8
839 p=p+1
840 pat[p]=0 as u8
841 if lw_count(buf,n,pat)>0 { return 1 }
842 return 0
843}
844
845// resolve a tool NAME to its absolute ELF via tool_allowlist.conf (field0 -> field1). NEVER hardcode a
846// path: a guessed path execve-fails as 127 and the caller silently files nothing. Ask the registry.
847func lw_allow_path(abuf: *u8, an: i64, name: *u8, out: *u8) -> i64 {
848 if an<=0 { return 0 }
849 var i: i64=0
850 while i<an {
851 let ls: i64=i
852 var le: i64=ls
853 var go: i64=1
854 while go==1 { if le>=an { go=0 } else { if abuf[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
855 i=le+1
856 if le>ls { if abuf[ls]!=(LW_HASH as u8) {
857 var p: i64=ls
858 var g1: i64=1
859 while g1==1 { if p>=le { g1=0 } else { if abuf[p]==(LW_TAB as u8) { g1=0 } else { p=p+1 } } }
860 if lw_span_eq(abuf,ls,p-ls,name)==1 {
861 let s1: i64=p+1
862 var q: i64=s1
863 var g2: i64=1
864 while g2==1 { if q>=le { g2=0 } else { if abuf[q]==(LW_TAB as u8) { g2=0 } else { q=q+1 } } }
865 let f1l: i64=q-s1
866 if f1l>0 { lw_catf(out,0,(abuf as i64+s1) as *u8,f1l); out[f1l]=0 as u8; return 1 }
867 }
868 } }
869 }
870 return 0
871}
872
873// ---------- selftest ----------
874func lw_selftest() -> i64 {
875 let ctr: *i64 = gv_ctr()
876 gv_head("nx_law_warden gate -- laws become mechanically checkable, and the warden obeys rule 11 itself" as *u8)
877 let thr: i64 = 1024
878
879 lw_wfile("/tmp/lw_a.nx\x00" as *u8, "var x: i64 = 65536\n\x00" as *u8)
880 let a: i64 = lw_file_lits("/tmp/lw_a.nx\x00" as *u8, thr)
881 var t1: i64=0
882 if a==1 { t1=1 }
883 gv_check("T1 inline literal above threshold is a BREACH" as *u8, t1, ctr)
884
885 lw_wfile("/tmp/lw_b.nx\x00" as *u8, "const LW_X: i64 = 65536\n\x00" as *u8)
886 let b: i64 = lw_file_lits("/tmp/lw_b.nx\x00" as *u8, thr)
887 var t2: i64=0
888 if b==0 { t2=1 }
889 gv_check("T2 the same number as a NAMED const is lawful (the rule-11 fix, not a breach)" as *u8, t2, ctr)
890
891 // ---- valuenamed teeth (seq1348). Each reads a REAL file through the same reader the walk uses. ----
892 // ⚠T2 above proves a NAMED const is lawful under inlinelit, and that stays true. valuenamed does not
893 // contradict it: it asks the SECOND question -- does the name actually EXPLAIN the number, or merely
894 // repeat it? LW_X = 65536 is lawful under BOTH; SS_MAGIC_65536 = 65536 is lawful only under the first.
895 let vszp: *i64 = sys_mmap(LW_SCRATCH) as *i64
896 lw_wfile("/tmp/lw_v1.nx\x00" as *u8, "const SS_MAGIC_65536: i64 = 65536\n\x00" as *u8)
897 let v1b: *u8 = sys_read_file("/tmp/lw_v1.nx\x00" as *u8, vszp)
898 var tv1: i64=0
899 if (v1b as i64)!=0 { if lw_file_valuenamed(v1b,vszp[0])==1 { tv1=1 } }
900 gv_check("TV1 a const NAMED AFTER ITS OWN VALUE is a breach (SS_MAGIC_65536 = 65536)" as *u8, tv1, ctr)
901
902 // NEGATIVE CONTROL -- the whole point is that INTENT-named consts must pass, or the detector would
903 // simply re-flag every constant and be useless. Same value, explanatory name.
904 lw_wfile("/tmp/lw_v2.nx\x00" as *u8, "const SS_READ_CHUNK_BYTES: i64 = 65536\n\x00" as *u8)
905 let v2b: *u8 = sys_read_file("/tmp/lw_v2.nx\x00" as *u8, vszp)
906 var tv2: i64=0
907 if (v2b as i64)!=0 { if lw_file_valuenamed(v2b,vszp[0])==0 { tv2=1 } }
908 gv_check("TV2 NEG-CONTROL: an INTENT-named const of the same value is NOT flagged" as *u8, tv2, ctr)
909
910 // a comment mentioning the shape is not a declaration
911 lw_wfile("/tmp/lw_v3.nx\x00" as *u8, "// const SS_MAGIC_65536: i64 = 65536\n\x00" as *u8)
912 let v3b: *u8 = sys_read_file("/tmp/lw_v3.nx\x00" as *u8, vszp)
913 var tv3: i64=0
914 if (v3b as i64)!=0 { if lw_file_valuenamed(v3b,vszp[0])==0 { tv3=1 } }
915 gv_check("TV3 NEG-CONTROL: a COMMENTED-OUT value-named const is not code" as *u8, tv3, ctr)
916
917 // hex must not be read as decimal (0x1ed's digits would otherwise chase the wrong number)
918 lw_wfile("/tmp/lw_v4.nx\x00" as *u8, "const LW_MODE_0x1ed: i64 = 0x1ed\n\x00" as *u8)
919 let v4b: *u8 = sys_read_file("/tmp/lw_v4.nx\x00" as *u8, vszp)
920 var tv4: i64=0
921 if (v4b as i64)!=0 { if lw_file_valuenamed(v4b,vszp[0])==0 { tv4=1 } }
922 gv_check("TV4 NEG-CONTROL: a HEX value is not decimal-matched (no false positive)" as *u8, tv4, ctr)
923
924 lw_wfile("/tmp/lw_c.nx\x00" as *u8, "// note 65536 here\n\x00" as *u8)
925 let c: i64 = lw_file_lits("/tmp/lw_c.nx\x00" as *u8, thr)
926 var t3: i64=0
927 if c==0 { t3=1 }
928 gv_check("T3 a number inside a comment is not code" as *u8, t3, ctr)
929
930 // T21-T24 exist because T3 above passed while the detector was BROKEN: T3 only ever writes a WHOLE-LINE
931 // comment, and lw_is_comment only recognises that shape. The live defect was the // TAIL of a CODE line --
932 // a dated trailing comment counted its year as a magic number and, through nx_magicratchet on /api/build,
933 // REFUSED REAL BUILDS (debt 1785050172). ★A TOOTH THAT CANNOT FAIL ON THE DEFECT IT NAMES IS NOT EVIDENCE.
934 // T21/T22 are the negative controls that would have caught it; T23/T24 are POSITIVE controls so the fix
935 // cannot pass by making the detector blind instead of accurate.
936 lw_wfile("/tmp/lw_tc.nx\x00" as *u8, "let w: i64 = q + 1 // dated 2026-07-31 is not code\n\x00" as *u8)
937 let tc: i64 = lw_file_lits("/tmp/lw_tc.nx\x00" as *u8, thr)
938 var t21: i64=0
939 if tc==0 { t21=1 }
940 gv_check("T21 NEG-CONTROL: a TRAILING // comment on a code line is not code (the whole-line case is not the defect)" as *u8, t21, ctr)
941
942 lw_wfile("/tmp/lw_ts.nx\x00" as *u8, "let s: *u8 = \x22listening on port 8080 since 2026\x22\n\x00" as *u8)
943 let tsq: i64 = lw_file_lits("/tmp/lw_ts.nx\x00" as *u8, thr)
944 var t22: i64=0
945 if tsq==0 { t22=1 }
946 gv_check("T22 NEG-CONTROL: digits inside a string literal are DATA, not rule-11 magic (agrees with nx_magic skipped=in-string)" as *u8, t22, ctr)
947
948 lw_wfile("/tmp/lw_tp.nx\x00" as *u8, "let z: i64 = q + 4096 // 2026 in the tail must not add to this\n\x00" as *u8)
949 let tp: i64 = lw_file_lits("/tmp/lw_tp.nx\x00" as *u8, thr)
950 var t23: i64=0
951 if tp==1 { t23=1 }
952 gv_check("T23 POS-CONTROL: a genuine inline literal is still counted EXACTLY ONCE beside a trailing comment" as *u8, t23, ctr)
953
954 lw_wfile("/tmp/lw_tx.nx\x00" as *u8, "let s: *u8 = \x22a 9999 b\x22 + 4096\n\x00" as *u8)
955 let tx: i64 = lw_file_lits("/tmp/lw_tx.nx\x00" as *u8, thr)
956 var t24: i64=0
957 if tx==1 { t24=1 }
958 gv_check("T24 POS-CONTROL: string state EXITS at the closing quote -- a literal after it is still counted" as *u8, t24, ctr)
959
960 // T25-T27 harden the string state against the idioms this corpus actually uses. Verified first as a
961 // standalone executable (scratchpad/l006_hard.nx, exit 63 = 6/6) before being seeded here.
962 lw_wfile("/tmp/lw_tu.nx\x00" as *u8, "let u: *u8 = \x22http://x\x22 let n: i64 = 8192\n\x00" as *u8)
963 let tu: i64 = lw_file_lits("/tmp/lw_tu.nx\x00" as *u8, thr)
964 var t25: i64=0
965 if tu==1 { t25=1 }
966 gv_check("T25 a // INSIDE a string is not a comment cut -- the literal after the string still counts" as *u8, t25, ctr)
967
968 lw_wfile("/tmp/lw_tm.nx\x00" as *u8, "f(\x22a\x22, 4096, \x22b9999\x22)\n\x00" as *u8)
969 let tm: i64 = lw_file_lits("/tmp/lw_tm.nx\x00" as *u8, thr)
970 var t26: i64=0
971 if tm==1 { t26=1 }
972 gv_check("T26 two adjacent strings do not merge: the literal BETWEEN them counts, the one inside does not" as *u8, t26, ctr)
973
974 lw_wfile("/tmp/lw_th.nx\x00" as *u8, "let m: i64 = 0x1a4 + p256 + 65536\n\x00" as *u8)
975 let th: i64 = lw_file_lits("/tmp/lw_th.nx\x00" as *u8, thr)
976 var t27: i64=0
977 if th==1 { t27=1 }
978 gv_check("T27 hex bodies and identifier digits stay excluded while a real literal on the same line counts" as *u8, t27, ctr)
979
980 lw_wfile("/tmp/lw_d.nx\x00" as *u8, "let m: i64 = 0x1a4\n\x00" as *u8)
981 let d: i64 = lw_file_lits("/tmp/lw_d.nx\x00" as *u8, thr)
982 var t4: i64=0
983 if d==0 { t4=1 }
984 gv_check("T4 hex bodies and escapes are not decimal literals (no false positives)" as *u8, t4, ctr)
985
986 lw_wfile("/tmp/lw_e.nx\x00" as *u8, "var y: i64 = 42\n\x00" as *u8)
987 let e: i64 = lw_file_lits("/tmp/lw_e.nx\x00" as *u8, thr)
988 var t5: i64=0
989 if e==0 { t5=1 }
990 gv_check("T5 threshold comes from manifest DATA -- below it is not flagged" as *u8, t5, ctr)
991
992 let miss: i64 = lw_file_lits("/tmp/lw_does_not_exist_zz.nx\x00" as *u8, thr)
993 var t6: i64=0
994 if miss==(0-1) { t6=1 }
995 gv_check("T6 unreadable corpus reports -1 UNMEASURED, never a silent 0" as *u8, t6, ctr)
996
997 lw_wfile("/tmp/lw_t.txt\x00" as *u8, "plan: we should just use webassembly for this\n\x00" as *u8)
998 let tcx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
999 tcx[0]=0
1000 tcx[1]=0
1001 let tw: *u8 = sys_mmap(LW_NAMEMAX)
1002 lw_token_scan("/tmp/lw_t.txt\x00" as *u8, "webassembly,cuda\x00" as *u8, tcx, tw, "\x00" as *u8)
1003 var t7: i64=0
1004 if tcx[1]==1 { if tcx[0]>=1 { t7=1 } }
1005 gv_check("T7 borrowed-stack token found, absent token NOT counted (neg-control)" as *u8, t7, ctr)
1006
1007 let ab: *u8 = sys_mmap(LW_NAMEMAX)
1008 let abn: i64 = lw_cat(ab,0,"nx_seat\tp\tGREEN\nnx_claims\tp\tGREEN\n" as *u8)
1009 let present: i64 = lw_field0_has(ab,abn,"nx_seat\x00" as *u8)
1010 let absent: i64 = lw_field0_has(ab,abn,"nx_ghost\x00" as *u8)
1011 var t8: i64=0
1012 if present==1 { if absent==0 { t8=1 } }
1013 gv_check("T8 MCP-exposure join reads field 0 exactly (shipped-but-unregistered is detectable)" as *u8, t8, ctr)
1014
1015 lw_wfile("/tmp/lw_zz_unreg.elf\x00" as *u8, "x\n\x00" as *u8)
1016 let wcx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1017 wcx[0]=LW_MODE_ELF
1018 wcx[1]=0
1019 wcx[2]=0
1020 wcx[3]=0
1021 wcx[4]=0
1022 wcx[5]=0
1023 wcx[6]=abn
1024 let ww: *u8 = sys_mmap(LW_NAMEMAX)
1025 ww[0]=0 as u8
1026 lw_walk("/tmp\x00" as *u8, wcx, ww, ab)
1027 var t9: i64=0
1028 if wcx[3]>=1 { if lw_slen(ww)>0 { t9=1 } }
1029 gv_check("T9 unregistered walk NAMES the organ (an empty sample means the basename never resolved, which would brand every organ unregistered)" as *u8, t9, ctr)
1030
1031 lw_wfile("/tmp/lw_tok.nx\x00" as *u8, "// plan: just bridge it with webrtc for now\n\x00" as *u8)
1032 let kcx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1033 kcx[0]=LW_MODE_TOK
1034 kcx[1]=0
1035 kcx[2]=0
1036 kcx[3]=0
1037 kcx[4]=0
1038 kcx[5]=0
1039 kcx[6]=0
1040 let kw: *u8 = sys_mmap(LW_NAMEMAX)
1041 kw[0]=0 as u8
1042 lw_walk("/tmp\x00" as *u8, kcx, kw, "webrtc,zzznotpresentzz\x00" as *u8)
1043 var t10: i64=0
1044 if kcx[3]>=1 { if lw_slen(kw)>0 { t10=1 } }
1045 gv_check("T10 tokendir finds borrowed-stack callouts ACROSS a source tree and names the file" as *u8, t10, ctr)
1046
1047 let lb: *u8 = sys_mmap(LW_NAMEMAX)
1048 var lp: i64=0
1049 lp=lw_cat(lb,lp,"100" as *u8)
1050 lb[lp]=LW_TAB as u8
1051 lp=lp+1
1052 lp=lw_cat(lb,lp,"LAW" as *u8)
1053 lb[lp]=LW_NL as u8
1054 lp=lp+1
1055 lp=lw_cat(lb,lp,"200" as *u8)
1056 lb[lp]=LW_TAB as u8
1057 lp=lp+1
1058 lp=lw_cat(lb,lp,"LAW" as *u8)
1059 lb[lp]=LW_NL as u8
1060 lp=lp+1
1061 var t11: i64=0
1062 if lw_last_ts(lb,lp)==200 { t11=1 }
1063 gv_check("T11 file acts on the LATEST scan block only (last ts wins, not the first)" as *u8, t11, ctr)
1064
1065 let pb: *u8 = sys_mmap(LW_NAMEMAX)
1066 var pp: i64=0
1067 pp=lw_catn(pb,pp,100)
1068 pb[pp]=LW_TAB as u8
1069 pp=pp+1
1070 pp=lw_cat(pb,pp,"FILED" as *u8)
1071 pb[pp]=LW_TAB as u8
1072 pp=pp+1
1073 pp=lw_cat(pb,pp,"L001" as *u8)
1074 pb[pp]=LW_TAB as u8
1075 pp=pp+1
1076 pp=lw_cat(pb,pp,"5" as *u8)
1077 pb[pp]=LW_NL as u8
1078 pp=pp+1
1079 var t12: i64=0
1080 if lw_filed_has(pb,pp,"L001\x00" as *u8)==1 { if lw_filed_has(pb,pp,"L002\x00" as *u8)==0 { t12=1 } }
1081 gv_check("T12 IDEMPOTENT: an already-filed law is skipped, a new one is not (hourly beat cannot spam)" as *u8, t12, ctr)
1082
1083 let rp: *u8 = sys_mmap(LW_PATH)
1084 let r1: i64 = lw_allow_path(ab,abn,"nx_seat\x00" as *u8,rp)
1085 let r2: i64 = lw_allow_path(ab,abn,"nx_ghost\x00" as *u8,rp)
1086 var t13: i64=0
1087 if r1==1 { if r2==0 { t13=1 } }
1088 gv_check("T13 filing organ resolved from the REGISTRY, unknown name refused (no guessed path -> no silent execve 127)" as *u8, t13, ctr)
1089
1090 lw_wfile("/tmp/lw_x.txt\x00" as *u8, "plan: just bridge it with webrtc\nwarden report: webrtc was the top hit\n\x00" as *u8)
1091 let xcx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1092 xcx[0]=0
1093 xcx[1]=0
1094 let xw: *u8 = sys_mmap(LW_NAMEMAX)
1095 lw_token_scan("/tmp/lw_x.txt\x00" as *u8, "webrtc\x00" as *u8, xcx, xw, "warden report\x00" as *u8)
1096 var t14: i64=0
1097 if xcx[0]==1 { t14=1 }
1098 gv_check("T14 exclusion: the warden's OWN report line is not counted as a callout (2 lines -> 1 real hit)" as *u8, t14, ctr)
1099
1100 // OWN scratch dir: /tmp is shared and already held another organ's seg-store fixtures, so asserting
1101 // on a global max was never deterministic. A test that reads a mutable shared directory is not a test.
1102 sys_mkdir("/tmp/lwseg\x00" as *u8, LW_DIRMODE)
1103 lw_wfile("/tmp/lwseg/zzlw-seg-0.docs\x00" as *u8, "x\n\x00" as *u8)
1104 lw_wfile("/tmp/lwseg/zzlw-seg-1.docs\x00" as *u8, "x\n\x00" as *u8)
1105 lw_wfile("/tmp/lwseg/yylw-seg-0.docs\x00" as *u8, "x\n\x00" as *u8)
1106 let scx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1107 scx[0]=0
1108 scx[1]=0
1109 scx[2]=0
1110 scx[3]=0
1111 let sw: *u8 = sys_mmap(LW_NAMEMAX)
1112 sw[0]=0 as u8
1113 lw_segamp("/tmp/lwseg\x00" as *u8, 1, scx, sw)
1114 var t15: i64=0
1115 if scx[0]==2 { if scx[1]==1 { if scx[2]==2 { t15=1 } } }
1116 gv_check("T15 segamp counts segments PER PLANE and flags the amplified one (the class that OOM-killed the box)" as *u8, t15, ctr)
1117
1118 let rc: i64 = gv_verdict("LAW-WARDEN-GATE" as *u8, ctr, "written laws now have detectors; warden is rule-11 clean by construction" as *u8)
1119 return rc
1120}
1121
1122// ---------- scan ----------
1123func lw_scan(prefix: *u8) -> i64 {
1124 let szp: *i64 = sys_mmap(LW_SCRATCH) as *i64
1125 // THE LAW REGISTRY IS A SOVEREIGN PLANE (seg-store behind nx_store_put), NOT a flat TSV -- the same
1126 // substrate as raci-/debt-/frontier-/featreg-, so every mutation is provenanced into its hist- rows
1127 // and parallel seats cannot clobber each other. A flat file had neither property.
1128 let mb: *u8 = sys_mmap(LW_PLANE)
1129 let mn: i64 = sts_load(prefix, mb, LW_PLANE)
1130 if mn<=0 {
1131 gv_puts("LAW-WARDEN RED -- law plane unseeded (fail-closed: no verdict over no data): " as *u8)
1132 gv_puts(prefix)
1133 gv_puts("\n" as *u8)
1134 sys_exit(1)
1135 return 1
1136 }
1137
1138 // RACI owner join (F207): the whole raci- plane as text; -1 bytes = plane unreadable = UNMEASURED
1139 let rb: *u8 = sys_mmap(LW_PLANE)
1140 let rn: i64 = sts_load("knowledge/store/raci-\x00" as *u8, rb, LW_PLANE)
1141
1142 // NOTE: the unregistered detector reads its allowlist from the law row's own param -- the conf path is
1143 // DATA like every other policy input, so the registry can never describe a file the code does not read.
1144
1145 let jb: *u8 = sys_mmap(LW_OUT)
1146 var j: i64=0
1147 let fb: *u8 = sys_mmap(LW_OUT)
1148 var fp: i64=0
1149 let now: i64 = sys_now_realtime_sec()
1150 j=lw_cat(jb,j,"{\x22v\x22:1,\x22domain\x22:\x22law-warden\x22,\x22title\x22:\x22Written laws, mechanically detected -- no human needs to name the breach\x22,\x22laws\x22:[" as *u8)
1151
1152 let fs: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1153 let fl: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1154 let cx: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1155 let worst: *u8 = sys_mmap(LW_NAMEMAX)
1156 let corpus: *u8 = sys_mmap(LW_PATH)
1157 let param: *u8 = sys_mmap(LW_PATH)
1158 let role: *u8 = sys_mmap(LW_NAMEMAX)
1159 let excl: *u8 = sys_mmap(LW_PATH)
1160
1161 var nlaw: i64=0
1162 var nmeas: i64=0
1163 var nbreach: i64=0
1164 var nunowned: i64=0
1165 var sevall: i64=0
1166 var sevbr: i64=0
1167 var first: i64=1
1168 var i: i64=0
1169 while i<mn {
1170 let ls: i64=i
1171 var le: i64=ls
1172 var go: i64=1
1173 while go==1 { if le>=mn { go=0 } else { if mb[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
1174 let lend: i64=le
1175 i=le+1
1176 let llen: i64=lend-ls
1177 if llen>3 { if mb[ls]!=(LW_HASH as u8) {
1178 var nf: i64=0
1179 var p: i64=ls
1180 var fstart: i64=ls
1181 while p<lend {
1182 if mb[p]==(LW_TAB as u8) {
1183 if nf<LW_COLS_MAX { fs[nf]=fstart; fl[nf]=p-fstart; nf=nf+1 }
1184 fstart=p+1
1185 }
1186 p=p+1
1187 }
1188 if nf<LW_COLS_MAX { fs[nf]=fstart; fl[nf]=lend-fstart; nf=nf+1 }
1189 if nf>=LW_COLS {
1190 let sev: i64 = (mb[fs[5]] as i64)-48
1191 // zero-terminate the fields the detectors need as C strings
1192 lw_catf(param,0,(mb as i64+fs[4]) as *u8,fl[4])
1193 param[fl[4]]=0 as u8
1194 lw_catf(corpus,0,(mb as i64+fs[7]) as *u8,fl[7])
1195 corpus[fl[7]]=0 as u8
1196 lw_catf(role,0,(mb as i64+fs[6]) as *u8,fl[6])
1197 role[fl[6]]=0 as u8
1198 // optional 9th column = a substring that marks a line as NOT-A-PROPOSAL (e.g. the warden's
1199 // own report frames). Absent -> no exclusion, identical to the 8-column contract (rule 19).
1200 excl[0]=0 as u8
1201 if nf>=LW_COLS_MAX { lw_catf(excl,0,(mb as i64+fs[8]) as *u8,fl[8]); excl[fl[8]]=0 as u8 }
1202
1203 // owner join: role must resolve in the raci- plane, else UNOWNED
1204 var owner_ok: i64 = 0-1
1205 if rn>0 { owner_ok=0; if lw_count(rb,rn,role)>0 { owner_ok=1 } }
1206 if owner_ok==0 { nunowned=nunowned+1 }
1207
1208 cx[0]=0
1209 cx[1]=0
1210 cx[2]=0
1211 cx[3]=0
1212 cx[4]=0
1213 cx[5]=0
1214 cx[6]=0
1215 worst[0]=0 as u8
1216 var measured: i64 = 0-1
1217 var files: i64 = 0
1218
1219 if lw_span_eq(mb,fs[3],fl[3],"inlinelit" as *u8)==1 {
1220 var thr: i64=0
1221 var q: i64=0
1222 while q<fl[4] { let dch: i64=param[q] as i64; if dch>=48 { if dch<=57 { thr=thr*10+(dch-48) } } q=q+1 }
1223 cx[0]=LW_MODE_LIT
1224 cx[1]=thr
1225 if lw_walk(corpus,cx,worst,mb)==0 { measured=cx[3]; files=cx[2] }
1226 }
1227 if lw_span_eq(mb,fs[3],fl[3],"valuenamed" as *u8)==1 {
1228 cx[0]=LW_MODE_VNAME
1229 if lw_walk(corpus,cx,worst,mb)==0 { measured=cx[3]; files=cx[2] }
1230 }
1231 if lw_span_eq(mb,fs[3],fl[3],"gatedry" as *u8)==1 {
1232 cx[0]=LW_MODE_GATEDRY
1233 if lw_walk(corpus,cx,worst,mb)==0 { measured=cx[3]; files=cx[2] }
1234 }
1235 if lw_span_eq(mb,fs[3],fl[3],"helperdup" as *u8)==1 {
1236 cx[0]=LW_MODE_HELPERDUP
1237 if lw_walk(corpus,cx,worst,mb)==0 { measured=cx[3]; files=cx[2] }
1238 }
1239 if lw_span_eq(mb,fs[3],fl[3],"scancap" as *u8)==1 {
1240 cx[0]=LW_MODE_SCANCAP
1241 if lw_walk(corpus,cx,worst,mb)==0 { measured=cx[3]; files=cx[2] }
1242 }
1243 if lw_span_eq(mb,fs[3],fl[3],"unregistered" as *u8)==1 {
1244 let csz: *i64 = sys_mmap(LW_SCRATCH) as *i64
1245 let cb: *u8 = sys_read_file(param, csz)
1246 if (cb as i64)!=0 {
1247 cx[0]=LW_MODE_ELF
1248 cx[6]=csz[0]
1249 if lw_walk(corpus,cx,worst,cb)==0 { measured=cx[3]; files=cx[2] }
1250 }
1251 }
1252 if lw_span_eq(mb,fs[3],fl[3],"token" as *u8)==1 {
1253 cx[0]=0
1254 cx[1]=0
1255 if lw_token_scan(corpus,param,cx,worst,excl)==0 { measured=cx[0]; files=cx[1] }
1256 }
1257 if lw_span_eq(mb,fs[3],fl[3],"tokendir" as *u8)==1 {
1258 cx[0]=LW_MODE_TOK
1259 if lw_walk(corpus,cx,worst,param)==0 { measured=cx[3]; files=cx[2] }
1260 }
1261 if lw_span_eq(mb,fs[3],fl[3],"segamp" as *u8)==1 {
1262 var thr2: i64=0
1263 var q2: i64=0
1264 while q2<fl[4] { let dc: i64=param[q2] as i64; if dc>=48 { if dc<=57 { thr2=thr2*10+(dc-48) } } q2=q2+1 }
1265 cx[0]=0
1266 cx[1]=0
1267 cx[2]=0
1268 cx[3]=0
1269 if lw_segamp(corpus,thr2,cx,worst)==0 { measured=cx[1]; files=cx[0]; if cx[3]==1 { cx[5]=1 } }
1270 }
1271
1272 nlaw=nlaw+1
1273 sevall=sevall+sev
1274 if measured>=0 { nmeas=nmeas+1 }
1275 if measured>0 { nbreach=nbreach+1; sevbr=sevbr+sev }
1276
1277 // one APPEND-ONLY frame per law per scan -- this is what makes a TREND derivable
1278 fp=lw_catn(fb,fp,now)
1279 fp=lw_cat(fb,fp,"\tLAW\t" as *u8)
1280 fp=lw_catf(fb,fp,(mb as i64+fs[0]) as *u8,fl[0])
1281 fp=lw_cat(fb,fp,"\t" as *u8)
1282 fp=lw_catf(fb,fp,(mb as i64+fs[3]) as *u8,fl[3])
1283 fp=lw_cat(fb,fp,"\t" as *u8)
1284 fp=lw_catn(fb,fp,measured)
1285 fp=lw_cat(fb,fp,"\t" as *u8)
1286 if measured<0 { fp=lw_cat(fb,fp,"UNMEASURED" as *u8) } else { if measured>0 { fp=lw_cat(fb,fp,"BREACH" as *u8) } else { fp=lw_cat(fb,fp,"CLEAN" as *u8) } }
1287 fp=lw_cat(fb,fp,"\t" as *u8)
1288 fp=lw_catesc(fb,fp,worst,lw_slen(worst))
1289 fp=lw_cat(fb,fp,"\n" as *u8)
1290
1291 if first==0 { j=lw_cat(jb,j,"," as *u8) }
1292 first=0
1293 j=lw_cat(jb,j,"{\x22law\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[0]) as *u8,fl[0])
1294 j=lw_cat(jb,j,"\x22,\x22title\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[1]) as *u8,fl[1])
1295 j=lw_cat(jb,j,"\x22,\x22rule\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[2]) as *u8,fl[2])
1296 j=lw_cat(jb,j,"\x22,\x22detector\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[3]) as *u8,fl[3])
1297 j=lw_cat(jb,j,"\x22,\x22param\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[4]) as *u8,fl[4])
1298 j=lw_cat(jb,j,"\x22,\x22sev\x22:" as *u8); j=lw_catn(jb,j,sev)
1299 j=lw_cat(jb,j,",\x22owner_role\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[6]) as *u8,fl[6])
1300 j=lw_cat(jb,j,"\x22,\x22owner_resolved\x22:" as *u8); j=lw_catn(jb,j,owner_ok)
1301 j=lw_cat(jb,j,",\x22corpus\x22:\x22" as *u8); j=lw_catesc(jb,j,(mb as i64+fs[7]) as *u8,fl[7])
1302 j=lw_cat(jb,j,"\x22,\x22measured\x22:" as *u8); j=lw_catn(jb,j,measured)
1303 j=lw_cat(jb,j,",\x22scanned\x22:" as *u8); j=lw_catn(jb,j,files)
1304 j=lw_cat(jb,j,",\x22capped\x22:" as *u8); j=lw_catn(jb,j,cx[5])
1305 j=lw_cat(jb,j,",\x22worst\x22:\x22" as *u8); j=lw_catesc(jb,j,worst,lw_slen(worst))
1306 j=lw_cat(jb,j,"\x22,\x22status\x22:\x22" as *u8)
1307 if measured<0 { j=lw_cat(jb,j,"UNMEASURED" as *u8) } else { if measured>0 { j=lw_cat(jb,j,"BREACH" as *u8) } else { j=lw_cat(jb,j,"CLEAN" as *u8) } }
1308 j=lw_cat(jb,j,"\x22}" as *u8)
1309 }
1310 } }
1311 }
1312
1313 var enforced: i64=0
1314 var breachw: i64=0
1315 if nlaw>0 { enforced=(nmeas*1000)/nlaw }
1316 if sevall>0 { breachw=(sevbr*1000)/sevall }
1317
1318 j=lw_cat(jb,j,"],\x22laws_total\x22:" as *u8); j=lw_catn(jb,j,nlaw)
1319 j=lw_cat(jb,j,",\x22laws_measured\x22:" as *u8); j=lw_catn(jb,j,nmeas)
1320 j=lw_cat(jb,j,",\x22laws_breached\x22:" as *u8); j=lw_catn(jb,j,nbreach)
1321 j=lw_cat(jb,j,",\x22laws_unowned\x22:" as *u8); j=lw_catn(jb,j,nunowned)
1322 j=lw_cat(jb,j,",\x22enforced_permil\x22:" as *u8); j=lw_catn(jb,j,enforced)
1323 j=lw_cat(jb,j,",\x22breach_weight_permil\x22:" as *u8); j=lw_catn(jb,j,breachw)
1324 j=lw_cat(jb,j,",\x22raci_plane_bytes\x22:" as *u8); j=lw_catn(jb,j,rn)
1325 j=lw_cat(jb,j,",\x22formula\x22:\x22enforced_permil = laws with a detector that actually RAN over laws in the registry; breach_weight_permil = severity share of breached laws. UNMEASURED is counted as un-enforced, never as clean.\x22" as *u8)
1326 j=lw_cat(jb,j,",\x22envelope\x22:\x22law roster = the lawreg- plane (grows by appended rows, NOT exhaustive); dir walks are one level, non-recursive, capped at LW_FILECAP files with capped=1 declared; owner join is substring presence in the raci- plane; -1 = UNMEASURED. tokendir counts MENTIONS not endorsements -- an anti-mention (no python) counts too, so token findings are review candidates for the router, never auto-verdicts.\x22" as *u8)
1327 j=lw_cat(jb,j,",\x22honest\x22:\x22VERDICT=GREEN means THE WARDEN MEASURED -- not that the ecosystem is clean; read laws_breached for that. A law with no row here is UNENFORCED and therefore invisible: adding a row is how a written rule becomes mechanically checkable, and doing so RAISES the breach count (that is the system working).\x22}" as *u8)
1328
1329 sys_write(1,jb,j)
1330 sys_write(1,"\n" as *u8,1)
1331
1332 // APPEND-ONLY evidence journal (rule 13: history is sacred; ws_sync/claims/actlog lineage). The old
1333 // truncating .log ERASED every prior scan, so the hourly beat could never yield a trend -- each run
1334 // overwrote the last. O_APPEND frames are conflict-free under parallel seats too.
1335 let lf: i64 = sys_openat_append("knowledge/status/law_warden.jrnl\x00" as *u8, LW_MODE)
1336 if lf>=0 {
1337 fp=lw_catn(fb,fp,now)
1338 fp=lw_cat(fb,fp,"\tVERDICT=GREEN\tlaws=" as *u8)
1339 fp=lw_catn(fb,fp,nlaw)
1340 fp=lw_cat(fb,fp,"\tmeasured=" as *u8)
1341 fp=lw_catn(fb,fp,nmeas)
1342 fp=lw_cat(fb,fp,"\tbreached=" as *u8)
1343 fp=lw_catn(fb,fp,nbreach)
1344 fp=lw_cat(fb,fp,"\tunowned=" as *u8)
1345 fp=lw_catn(fb,fp,nunowned)
1346 fp=lw_cat(fb,fp,"\tenforced_permil=" as *u8)
1347 fp=lw_catn(fb,fp,enforced)
1348 fp=lw_cat(fb,fp,"\n" as *u8)
1349 sys_write(lf,fb,fp)
1350 sys_close(lf)
1351 }
1352 return 0
1353}
1354
1355// find the lawreg- row whose col0 == lawid; fills gs/gl with its 8 column spans
1356func lw_plane_find(rb: *u8, rn: i64, lawid: *u8, gs: *i64, gl: *i64) -> i64 {
1357 var i: i64=0
1358 while i<rn {
1359 let ls: i64=i
1360 var le: i64=ls
1361 var go: i64=1
1362 while go==1 { if le>=rn { go=0 } else { if rb[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
1363 i=le+1
1364 if le>ls { if rb[ls]!=(LW_HASH as u8) {
1365 var nf: i64=0
1366 var p: i64=ls
1367 var st: i64=ls
1368 while p<le {
1369 if rb[p]==(LW_TAB as u8) { if nf<LW_COLS_MAX { gs[nf]=st; gl[nf]=p-st; nf=nf+1 } st=p+1 }
1370 p=p+1
1371 }
1372 if nf<LW_COLS_MAX { gs[nf]=st; gl[nf]=le-st; nf=nf+1 }
1373 if nf>=LW_COLS { if lw_span_eq(rb,gs[0],gl[0],lawid)==1 { return 1 } }
1374 } }
1375 }
1376 return 0
1377}
1378
1379func lw_file(prefix: *u8, toolname: *u8) -> i64 {
1380 let szp: *i64 = sys_mmap(LW_SCRATCH) as *i64
1381 // the filing organ is resolved from the REGISTRY, never from a hardcoded path
1382 let az: *i64 = sys_mmap(LW_SCRATCH) as *i64
1383 let ab: *u8 = sys_read_file("tool_allowlist.conf\x00" as *u8, az)
1384 let elfpath: *u8 = sys_mmap(LW_PATH)
1385 var okp: i64 = 0
1386 if (ab as i64)!=0 { okp=lw_allow_path(ab,az[0],toolname,elfpath) }
1387 if okp==0 {
1388 gv_puts("LAW-FILE RED -- filing organ has no tool_allowlist.conf row; refusing to fork a guessed path: " as *u8)
1389 gv_puts(toolname)
1390 gv_puts("\n" as *u8)
1391 sys_exit(1)
1392 return 1
1393 }
1394 let rb: *u8 = sys_mmap(LW_PLANE)
1395 let rn: i64 = sts_load(prefix, rb, LW_PLANE)
1396 if rn<=0 {
1397 gv_puts("LAW-FILE RED -- law plane unseeded (fail-closed): " as *u8)
1398 gv_puts(prefix)
1399 gv_puts("\n" as *u8)
1400 sys_exit(1)
1401 return 1
1402 }
1403 let jb: *u8 = sys_read_file("knowledge/status/law_warden.jrnl\x00" as *u8, szp)
1404 if (jb as i64)==0 {
1405 gv_puts("LAW-FILE RED -- no evidence journal yet; run scan first (never files on no data)\n" as *u8)
1406 sys_exit(1)
1407 return 1
1408 }
1409 let jl: i64 = szp[0]
1410 let last: i64 = lw_last_ts(jb,jl)
1411 let fz: *i64 = sys_mmap(LW_SCRATCH) as *i64
1412 let fb: *u8 = sys_read_file("knowledge/status/law_filed.jrnl\x00" as *u8, fz)
1413 var fn2: i64 = 0
1414 if (fb as i64)!=0 { fn2=fz[0] }
1415
1416 let ob: *u8 = sys_mmap(LW_OUT)
1417 var o: i64=0
1418 o=lw_cat(ob,o,"{\x22v\x22:1,\x22domain\x22:\x22law-file\x22,\x22scan_ts\x22:" as *u8)
1419 o=lw_catn(ob,o,last)
1420 o=lw_cat(ob,o,",\x22actions\x22:[" as *u8)
1421
1422 let fs: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1423 let fl: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1424 let gs: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1425 let gl: *i64 = sys_mmap(LW_FLD_BYTES) as *i64
1426 let lawid: *u8 = sys_mmap(LW_NAMEMAX)
1427 let sevb: *u8 = sys_mmap(LW_NAMEMAX)
1428 let scope: *u8 = sys_mmap(LW_NAMEMAX)
1429 let desc: *u8 = sys_mmap(LW_OUT)
1430 let capb: *u8 = sys_mmap(LW_OUT)
1431 let ol: *i64 = sys_mmap(LW_SCRATCH) as *i64
1432 let av: *i64 = sys_mmap(LW_ST_SLOTS) as *i64
1433 let fr: *u8 = sys_mmap(LW_OUT)
1434 var fp: i64=0
1435 var nfiled: i64=0
1436 var nskip: i64=0
1437 var nerr: i64=0
1438 var first: i64=1
1439
1440 var i: i64=0
1441 while i<jl {
1442 let ls: i64=i
1443 var le: i64=ls
1444 var go: i64=1
1445 while go==1 { if le>=jl { go=0 } else { if jb[le]==(LW_NL as u8) { go=0 } else { le=le+1 } } }
1446 i=le+1
1447 if le>ls {
1448 var nf: i64=0
1449 var p: i64=ls
1450 var st: i64=ls
1451 while p<le {
1452 if jb[p]==(LW_TAB as u8) { if nf<LW_COLS { fs[nf]=st; fl[nf]=p-st; nf=nf+1 } st=p+1 }
1453 p=p+1
1454 }
1455 if nf<LW_COLS { fs[nf]=st; fl[nf]=le-st; nf=nf+1 }
1456 if nf>=6 {
1457 var ts: i64=0
1458 var k: i64=fs[0]
1459 var g2: i64=1
1460 while g2==1 {
1461 if k>=fs[0]+fl[0] { g2=0 } else {
1462 let d: i64=jb[k] as i64
1463 if d>=48 { if d<=57 { ts=ts*10+(d-48); k=k+1 } else { g2=0 } } else { g2=0 }
1464 }
1465 }
1466 if ts==last { if lw_span_eq(jb,fs[1],fl[1],"LAW" as *u8)==1 { if lw_span_eq(jb,fs[5],fl[5],"BREACH" as *u8)==1 {
1467 lw_catf(lawid,0,(jb as i64+fs[2]) as *u8,fl[2])
1468 lawid[fl[2]]=0 as u8
1469 if first==0 { o=lw_cat(ob,o,"," as *u8) }
1470 first=0
1471 o=lw_cat(ob,o,"{\x22law\x22:\x22" as *u8)
1472 o=lw_catesc(ob,o,lawid,lw_slen(lawid))
1473 o=lw_cat(ob,o,"\x22,\x22action\x22:\x22" as *u8)
1474 if lw_filed_has(fb,fn2,lawid)==1 {
1475 nskip=nskip+1
1476 o=lw_cat(ob,o,"SKIP-ALREADY-FILED" as *u8)
1477 } else {
1478 if lw_plane_find(rb,rn,lawid,gs,gl)==0 {
1479 nerr=nerr+1
1480 o=lw_cat(ob,o,"ERR-LAW-NOT-IN-PLANE" as *u8)
1481 } else {
1482 sevb[0]=rb[gs[5]]
1483 sevb[1]=0 as u8
1484 var sp: i64=0
1485 sp=lw_cat(scope,sp,"law-" as *u8)
1486 sp=lw_catf(scope,sp,lawid,lw_slen(lawid))
1487 scope[sp]=0 as u8
1488 var dp: i64=0
1489 dp=lw_cat(desc,dp,"LAW " as *u8)
1490 dp=lw_catf(desc,dp,lawid,lw_slen(lawid))
1491 dp=lw_cat(desc,dp," BREACH (nx_law_warden AUTOFILED by the hourly beat, zero-Claude): " as *u8)
1492 dp=lw_catf(desc,dp,(rb as i64+gs[1]) as *u8,gl[1])
1493 dp=lw_cat(desc,dp," | rule=" as *u8)
1494 dp=lw_catf(desc,dp,(rb as i64+gs[2]) as *u8,gl[2])
1495 dp=lw_cat(desc,dp," detector=" as *u8)
1496 dp=lw_catf(desc,dp,(jb as i64+fs[3]) as *u8,fl[3])
1497 dp=lw_cat(desc,dp," measured=" as *u8)
1498 dp=lw_catf(desc,dp,(jb as i64+fs[4]) as *u8,fl[4])
1499 dp=lw_cat(desc,dp," worst=" as *u8)
1500 if nf>=7 { dp=lw_catf(desc,dp,(jb as i64+fs[6]) as *u8,fl[6]) }
1501 dp=lw_cat(desc,dp," owner_role=" as *u8)
1502 dp=lw_catf(desc,dp,(rb as i64+gs[6]) as *u8,gl[6])
1503 dp=lw_cat(desc,dp,". Corpus " as *u8)
1504 dp=lw_catf(desc,dp,(rb as i64+gs[7]) as *u8,gl[7])
1505 dp=lw_cat(desc,dp,". COVERAGE (added 2026-07-31, debts 1785519276 + 1785519488): this measurement is a ONE-LEVEL, NON-RECURSIVE directory walk CAPPED at LW_FILECAP files. It is a FLOOR, NOT A CORPUS TOTAL -- the named corpus is typically an order of magnitude larger (buildroot/runtime/_hdl_build alone holds 6555 one-level .nx, so the cap is about 9pct coverage). Do NOT size effort or trend from this number without reading files_seen/capped in the scan envelope. The warden's OWN L011 scancap law calls presenting partial coverage as complete a BUG, not a declared floor, so this sentence exists to stop the naked number travelling without its denominator." as *u8)
1506 dp=lw_cat(desc,dp,". Evidence knowledge/status/law_warden.jrnl (append-only, one frame per law per beat = the trend). Filed ONCE and never re-filed; the journal carries movement." as *u8)
1507 desc[dp]=0 as u8
1508 av[0]=elfpath as i64
1509 av[1]=("add\x00" as *u8) as i64
1510 av[2]=sevb as i64
1511 av[3]=scope as i64
1512 av[4]=desc as i64
1513 av[5]=0
1514 let rc: i64 = tr_run_capture(elfpath, av, capb, LW_OUT, ol)
1515 if rc==0 {
1516 nfiled=nfiled+1
1517 o=lw_cat(ob,o,"FILED" as *u8)
1518 fp=lw_catn(fr,fp,last)
1519 fr[fp]=LW_TAB as u8
1520 fp=fp+1
1521 fp=lw_cat(fr,fp,"FILED" as *u8)
1522 fr[fp]=LW_TAB as u8
1523 fp=fp+1
1524 fp=lw_catf(fr,fp,lawid,lw_slen(lawid))
1525 fr[fp]=LW_TAB as u8
1526 fp=fp+1
1527 fp=lw_catf(fr,fp,(jb as i64+fs[4]) as *u8,fl[4])
1528 // col 5 = the debt id parsed from the reply, or -1 if the reply was not an
1529 // unambiguous DEBT-ADDED. ADDITIVE (rule 19): lw_filed_has keys on the
1530 // "\tFILED\t<lawid>\t" substring, which is unchanged, so every existing
1531 // reader of cols 0-3 is unaffected. -1 is recorded rather than omitted, so
1532 // an unparseable reply is VISIBLE instead of looking like it never happened.
1533 fr[fp]=LW_TAB as u8
1534 fp=fp+1
1535 fp=lw_catn(fr,fp,lw_debt_id(capb,ol[0]))
1536 fr[fp]=LW_NL as u8
1537 fp=fp+1
1538 } else {
1539 nerr=nerr+1
1540 o=lw_cat(ob,o,"ERR-DEBT-FORK-RC" as *u8)
1541 o=lw_catn(ob,o,rc)
1542 }
1543 }
1544 }
1545 o=lw_cat(ob,o,"\x22}" as *u8)
1546 } } }
1547 }
1548 }
1549 }
1550
1551 if fp>0 {
1552 let ff: i64 = sys_openat_append("knowledge/status/law_filed.jrnl\x00" as *u8, LW_MODE)
1553 if ff>=0 { sys_write(ff,fr,fp); sys_close(ff) }
1554 }
1555 o=lw_cat(ob,o,"],\x22filed\x22:" as *u8)
1556 o=lw_catn(ob,o,nfiled)
1557 o=lw_cat(ob,o,",\x22skipped\x22:" as *u8)
1558 o=lw_catn(ob,o,nskip)
1559 o=lw_cat(ob,o,",\x22errors\x22:" as *u8)
1560 o=lw_catn(ob,o,nerr)
1561 o=lw_cat(ob,o,",\x22honest\x22:\x22files ONCE per law (idempotent, rule 10) so an hourly beat never spams the plane; movement lives in law_warden.jrnl, not in duplicate rows. A law filed here is OWNED work in the debt plane -- written by forking nx_debt, which stays the single writer of its own plane.\x22}" as *u8)
1562 sys_write(1,ob,o)
1563 sys_write(1,"\n" as *u8,1)
1564 return 0
1565}
1566
1567func main(argc: i64, argv: *i64) -> i64 {
1568 // ★ANCHOR FIRST (2026-08-04). MEASURED: run from buildroot this warden printed
1569 // "LAW-WARDEN RED -- law plane unseeded (fail-closed: no verdict over no data)" while the SAME
1570 // binary run from nishihost returned all 11 laws with real breach counts. Every corpus path
1571 // here ("knowledge/store/lawreg-", "buildroot/runtime/_hdl_build") is estate-root relative, so
1572 // the CWD silently decided whether the estate had ANY laws. The cron row already spells
1573 // `cd .../nishihost &&`; this makes the warden independent of whether its caller remembered.
1574 ep_anchor()
1575 var verb: *u8 = "scan" as *u8
1576 if argc>1 { verb=argv[1] as *u8 }
1577 let vl: i64 = lw_slen(verb)
1578 if lw_span_eq(verb,0,vl,"selftest" as *u8)==1 { let rc: i64=lw_selftest(); sys_exit(rc); return rc }
1579 if lw_span_eq(verb,0,vl,"countfile" as *u8)==1 {
1580 if argc<3 { gv_puts("usage: nx_law_warden countfile <path>\n" as *u8); sys_exit(2); return 2 }
1581 let rc: i64=lw_countfile(argv[2] as *u8)
1582 sys_exit(rc)
1583 return rc
1584 }
1585 var prefix: *u8 = "knowledge/store/lawreg-" as *u8
1586 if argc>2 { prefix=argv[2] as *u8 }
1587 if lw_span_eq(verb,0,vl,"scan" as *u8)==1 { let rc: i64=lw_scan(prefix); sys_exit(rc); return rc }
1588 if lw_span_eq(verb,0,vl,"file" as *u8)==1 {
1589 var toolnm: *u8 = "nx_debt" as *u8
1590 if argc>3 { toolnm=argv[3] as *u8 }
1591 let rc: i64=lw_file(prefix,toolnm)
1592 sys_exit(rc)
1593 return rc
1594 }
1595 gv_puts("usage: nx_law_warden {scan [planeprefix] | file [planeprefix] [debtelf] | countfile <path> | selftest}\n" as *u8)
1596 sys_exit(2)
1597 return 2
1598}