nx_io_envelope_census.nx source
↩ module page · 1564 lines · 92249 B
1// nx_io_envelope_census.nx -- EVERY CAPPED READER ANNOUNCES OR DERIVES (/compare/dataio DI2, 2026-09-05).
2//
3// THE CLASS: a read into a buffer whose length is a CONSTANT silently returns a PREFIX of anything larger, and the
4// caller reports success. Measured on the day this was written: the literal 4194304 appears 1,430 times in 717 source
5// files; the action miner's mine/freq/harden verbs read a 54 MB journal through a 4 MiB head for weeks; the ledger lib
6// behind the estate's idempotency keys read its ledger through 262,144 bytes. Each was a correct program reading the
7// wrong amount and saying nothing. The fix for a file read is sys_read_file (it sizes from the file and cannot short-
8// read); the fix for a genuinely unknowable size (a stream) is a NAMED cap whose truncation ANNOUNCES itself.
9//
10// THIS ORGAN MEASURES WHERE THE ESTATE STANDS ON THAT, whole population, and makes SILENT the worklist:
11// sys_read( fd, buf, LEN ) call sites, LEN classified as
12// LITERAL a decimal literal -> FIXED
13// NAMED an ALL-CAPS identifier declared `const NAME: i64 = <decimal>` in the same file -> FIXED
14// DYNAMIC anything else (a computed remainder, a variable, an unresolved name) -> NOT JUDGED here
15// a FIXED site is ANNOUNCING when its file carries an envelope token that tells the reader the read may be partial
16// (TRUNCATED, OUTPUT-IS-PARTIAL, coverage_complete, capture_cap, scan_cap, truncated=, partial=), else SILENT.
17// sys_read_file( call sites are counted on a SEPARATE axis as DERIVED (the remedy, adopted).
18// COMPOSES, NEVER DUPLICATES: the walk is nx_shelltool grep (the estate's budgeted, envelope-honest walker -- its own
19// coverage_complete / corpus_complete are read and PROPAGATED, so a partial walk yields UNPROVEN, never a smaller
20// number that reads as better news); code-vs-prose is jc_strip_noncode from nx_cap_detect_lib (the same stripper the
21// silent-cap detector uses), so a `sys_read(` inside a string or a comment is never a site.
22// LOOPED (DI12, 2026-09-06) a read INSIDE a while block, or one whose result variable is the subject of a
23// `while <v> > 0` / `while <v> != 0` in the same file: the streaming idiom that consumes to EOF -> NOT JUDGED.
24// The first census put nx_sizeguard's own read loop on the SILENT worklist -- a correct streaming reader named
25// as a capped one -- so the class is separated and counted, never acquitted (a loop that breaks early is
26// still a cap; the loop's exit is not inspected here).
27// RATCHET (DI12 ie_ratchet): the SILENT set is a NAMED SET in knowledge/status/io_envelope_silent.baseline, one row per
28// site, keyed `path<TAB>k<TAB>text<TAB>line` where text is the stripped, whitespace-collapsed source line and k its
29// ordinal among identical lines in that file -- so an edit ABOVE a site does not change its identity (a file:line key
30// turned every conversion into a spurious rise plus a spurious fall; the line number is carried for humans only).
31// A RISE (a silent site not in the baseline) is RED and NAMED, and the baseline is NOT rewritten (a ratchet that
32// rewrites its baseline on a rise launders itself green); a FALL (a baseline site no longer silent) is NAMED with what
33// it became now (converted / announced / dynamic / looped), journaled to knowledge/status/io_envelope_ratchet.jrnl, and
34// the baseline is TIGHTENED to the rows that remain (a ratchet that does not tighten when you improve is a threshold).
35// --accept rewrites the baseline to the current set deliberately. A legacy `path:line` baseline is matched by line once
36// and MIGRATED to the keyed form. A count-only ratchet on a shared tree reports a regression without saying whose.
37// FALSE-POSITIVE RATE (DI12): derived from the ledger, never asserted. A site that LEFT the set as converted/announced/
38// dynamic was a true positive (a real capped reader got fixed); one that left as `looped` was the OLD ruler's false
39// positive; a hand row `kind=false site=<path:line> reason=<why>` adjudicates a standing site as bounded by
40// construction. false_positive_permil = fp * 1000 / (tp + fp), printed UNMEASURED while nothing is adjudicated.
41// HONESTY LIMITS, stated: DYNAMIC and LOOPED are "not judged", not "safe" -- a computed length can still be a cap
42// wearing a variable, and a loop can break before EOF.
43// nx_io_envelope_census [scan_root] [--accept] default scan_root = buildroot/runtime
44// nx_io_envelope_census selftest
45// exit: 0 GREEN (no new silent site) | 1 RED (a new silent site, named) | 3 UNPROVEN (the walker was partial) | 2 usage
46// license_tier: ORIGINAL No hw writes (Rule 26).
47import "nx_syscalls.nx"
48import "nx_tool_run.nx"
49import "nx_gate_verdict.nx"
50import "nx_cap_detect_lib.nx"
51
52const IE_GREP_CAP: i64 = 8388608 // the walker's output is a STREAM of unknown size: a NAMED cap, and hitting it ANNOUNCES (cap_hit=1 -> UNPROVEN)
53const IE_SILENT_CAP: i64 = 1048576 // the silent-site worklist buffer; overflow is COUNTED (silent_overflow), never dropped in silence
54const IE_PATH_CAP: i64 = 1024
55const IE_NAME_CAP: i64 = 128
56const IE_MODE_644: i64 = 420
57const IE_EXIT_GREEN: i64 = 0
58const IE_EXIT_RED: i64 = 1
59const IE_EXIT_UNPROVEN: i64 = 3
60const IE_EXIT_USAGE: i64 = 2
61const IE_BASELINE: *u8 = "knowledge/status/io_envelope_silent.baseline" as *u8
62const IE_LEDGER: *u8 = "knowledge/status/io_envelope_ratchet.jrnl" as *u8
63const IE_STATUS: *u8 = "knowledge/status/io_envelope.status" as *u8
64const IE_TAB: i64 = 9
65const IE_TEXT_CAP: i64 = 1024 // a key's text field; a longer source line is cut here (the cut is deterministic, so the key still identifies the site)
66const IE_ROW_CAP: i64 = 2048
67const IE_FIX_DIR: *u8 = "/tmp/nx_io_envelope_census" as *u8
68const IE_MODE_755: i64 = 493
69const IE_BS_CREATED: i64 = 0
70const IE_BS_PRESENT: i64 = 1
71const IE_BS_REWRITTEN: i64 = 2
72const IE_BS_TIGHTENED: i64 = 3
73const IE_BS_MIGRATED: i64 = 4
74const IE_FP_UNMEASURED: i64 = 0 - 1
75const IE_SHELLTOOL: *u8 = "./nx_shelltool.elf" as *u8
76const IE_DEFAULT_ROOT: *u8 = "buildroot/runtime" as *u8
77
78// classes (a partition of every grep hit)
79const IE_NOT_A_SITE: i64 = 0 // the hit was inside a string or a comment, or the line could not be located
80const IE_LITERAL_ANN: i64 = 1
81const IE_LITERAL_SILENT: i64 = 2
82const IE_NAMED_ANN: i64 = 3
83const IE_NAMED_SILENT: i64 = 4
84const IE_DYNAMIC: i64 = 5
85const IE_LOOPED: i64 = 6
86// DI15 (2026-09-06): a LOOPED read is JUDGED BY ITS LOOP'S EXIT, never parked. Three classes, a partition of LOOPED:
87// STREAMING the loop ends when the read returns nothing (its result, or a flag the result sets, is tested against 0):
88// the correct way to read a stream of unknown size -- ACQUITTED
89// CAPPED the loop ALSO compares a running count against a literal, an ALL-CAPS constant or a cap-named variable:
90// a cap wearing a loop -- joins the silent worklist with the bound NAMED (a tail window is a legitimate
91// cap, so the row is adjudicated, never assumed a defect)
92// UNJUDGED the loop's exit could not be read (a call, a foreign flag): counted, never acquitted
93const IE_LOOP_STREAMING: i64 = 1
94const IE_LOOP_CAPPED: i64 = 2
95const IE_LOOP_UNJUDGED: i64 = 3
96const IE_BOUND_CAP: i64 = 64
97
98func ie_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
99func ie_putn(v: i64) -> i64 {
100 var m: i64 = v
101 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
102 let t: *u8 = sys_mmap(32)
103 var k: i64 = 0
104 if m == 0 { t[0] = 48 as u8; k = 1 }
105 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
106 let o: *u8 = sys_mmap(32)
107 var i: i64 = 0
108 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
109 sys_write(1, o, k)
110 return 0
111}
112func ie_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
113func ie_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var oo: i64 = o; while s[i] != (0 as u8) { d[oo] = s[i]; oo = oo + 1; i = i + 1 } d[oo] = 0 as u8; return oo }
114func ie_catn(d: *u8, o: i64, v: i64) -> i64 {
115 var m: i64 = v
116 var oo: i64 = o
117 if m < 0 { d[oo] = 45 as u8; oo = oo + 1; m = 0 - m }
118 let t: *u8 = sys_mmap(32)
119 var k: i64 = 0
120 if m == 0 { t[0] = 48 as u8; k = 1 }
121 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
122 while k > 0 { k = k - 1; d[oo] = t[k]; oo = oo + 1 }
123 d[oo] = 0 as u8
124 return oo
125}
126func ie_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] == b[i] { if a[i] == (0 as u8) { return 1 } i = i + 1 } return 0 }
127func ie_find(q: *u8, n: i64, lit: *u8) -> i64 { return jc_find_sub(q, n, lit, ie_slen(lit)) }
128func ie_is_digit(c: i64) -> i64 { if c >= 48 { if c <= 57 { return 1 } } return 0 }
129func ie_is_upper_ident(c: i64) -> i64 { if c >= 65 { if c <= 90 { return 1 } } if c == 95 { return 1 } return ie_is_digit(c) }
130// first index >= p that is not a space (bounded by e)
131func ie_skip_spaces(buf: *u8, e: i64, p: i64) -> i64 { var i: i64 = p; var go: i64 = 1; while go == 1 { if i >= e { go = 0 } else { if buf[i] == (32 as u8) { i = i + 1 } else { go = 0 } } } return i }
132// index of the newline ending the line that contains p, or n
133func ie_line_end(buf: *u8, n: i64, p: i64) -> i64 { var i: i64 = p; var go: i64 = 1; while go == 1 { if i >= n { go = 0 } else { if buf[i] == (10 as u8) { go = 0 } else { i = i + 1 } } } return i }
134// the byte offset of 1-based line ln, or -1
135func ie_line_off(buf: *u8, n: i64, ln: i64) -> i64 {
136 if ln <= 1 { return 0 }
137 var cur: i64 = 1
138 var i: i64 = 0
139 var found: i64 = 0 - 1
140 var go: i64 = 1
141 while go == 1 {
142 if i >= n { go = 0 } else {
143 if buf[i] == (10 as u8) { cur = cur + 1; if cur == ln { found = i + 1; go = 0 } }
144 i = i + 1
145 }
146 }
147 return found
148}
149// after "needle" somewhere in buf: the decimal that follows it, or -1
150// THE WALKER'S ENVELOPE IS ITS LAST `-- matches=` LINE, AND ONLY THAT LINE IS PARSED. Before 2026-09-06 the fields
151// were searched over the whole capture, and the first `coverage_complete=` found was a grep HIT -- a selftest
152// fixture in this very file that spells the token in a string -- so the census read its own data as its answer and
153// reported a complete walk UNPROVEN twice. out[0]=matches out[1]=coverage_complete out[2]=corpus_complete (-1 = absent)
154func ie_walker_env(gout: *u8, gn: i64, out: *i64) -> i64 {
155 out[0] = 0 - 1
156 out[1] = 0 - 1
157 out[2] = 0 - 1
158 // the last line that STARTS with "-- matches="
159 var ls: i64 = 0 - 1
160 var i: i64 = 0
161 while i < gn {
162 if jc_match_at(gout, i, "-- matches=" as *u8, 11) == 1 { if i == 0 { ls = i } else { if gout[i - 1] == (10 as u8) { ls = i } } }
163 i = ie_line_end(gout, gn, i) + 1
164 }
165 if ls < 0 { return 0 }
166 let le: i64 = ie_line_end(gout, gn, ls)
167 let line: *u8 = ((gout as i64) + ls) as *u8
168 let ln: i64 = le - ls
169 out[0] = ie_int_after(line, ln, "-- matches=" as *u8)
170 out[1] = ie_int_after(line, ln, "coverage_complete=" as *u8)
171 out[2] = ie_int_after(line, ln, "corpus_complete=" as *u8)
172 return 1
173}
174func ie_int_after(buf: *u8, n: i64, needle: *u8) -> i64 {
175 let nl: i64 = ie_slen(needle)
176 var i: i64 = 0
177 var at: i64 = 0 - 1
178 var go: i64 = 1
179 while go == 1 { if i + nl > n { go = 0 } else { if jc_match_at(buf, i, needle, nl) == 1 { at = i + nl; go = 0 } else { i = i + 1 } } }
180 if at < 0 { return 0 - 1 }
181 var v: i64 = 0
182 var nd: i64 = 0
183 var p: i64 = at
184 var d: i64 = 1
185 while d == 1 { if p >= n { d = 0 } else { let c: i64 = buf[p] as i64; if ie_is_digit(c) == 1 { v = v * 10 + (c - 48); nd = nd + 1; p = p + 1 } else { d = 0 } } }
186 if nd == 0 { return 0 - 1 }
187 return v
188}
189
190// does the file ANNOUNCE partial reads anywhere? (raw bytes: the tokens live in the strings it prints)
191func ie_file_announces(raw: *u8, n: i64) -> i64 {
192 if ie_find(raw, n, "TRUNCATED" as *u8) == 1 { return 1 }
193 if ie_find(raw, n, "OUTPUT-IS-PARTIAL" as *u8) == 1 { return 1 }
194 if ie_find(raw, n, "coverage_complete" as *u8) == 1 { return 1 }
195 if ie_find(raw, n, "capture_cap" as *u8) == 1 { return 1 }
196 if ie_find(raw, n, "scan_cap" as *u8) == 1 { return 1 }
197 if ie_find(raw, n, "truncated=" as *u8) == 1 { return 1 }
198 if ie_find(raw, n, "partial=" as *u8) == 1 { return 1 }
199 return 0
200}
201
202// is NAME (nl bytes) declared `const NAME: i64 = <decimal>` in the stripped file? the decimal, or -1
203func ie_const_value(code: *u8, n: i64, name: *u8, nl: i64) -> i64 {
204 var i: i64 = 0
205 var result: i64 = 0 - 1
206 var scanning: i64 = 1
207 while scanning == 1 {
208 if i + 6 > n { scanning = 0 } else {
209 var advance: i64 = 1
210 if jc_match_at(code, i, "const " as *u8, 6) == 1 {
211 let j: i64 = ie_skip_spaces(code, n, i + 6)
212 if j + nl <= n {
213 if jc_match_at(code, j, name, nl) == 1 {
214 var longer: i64 = 0
215 if j + nl < n { if ie_is_upper_ident(code[j + nl] as i64) == 1 { longer = 1 } }
216 if longer == 0 {
217 let e: i64 = ie_line_end(code, n, j)
218 // find '=' on this line
219 var m: i64 = j + nl
220 var eq: i64 = 0 - 1
221 var lk: i64 = 1
222 while lk == 1 { if m >= e { lk = 0 } else { if code[m] == (61 as u8) { eq = m; lk = 0 } else { m = m + 1 } } }
223 if eq >= 0 {
224 var p: i64 = ie_skip_spaces(code, e, eq + 1)
225 var v: i64 = 0
226 var nd: i64 = 0
227 var dg: i64 = 1
228 while dg == 1 { if p >= e { dg = 0 } else { let c: i64 = code[p] as i64; if ie_is_digit(c) == 1 { v = v * 10 + (c - 48); nd = nd + 1; p = p + 1 } else { dg = 0 } } }
229 if nd > 0 { result = v }
230 }
231 scanning = 0
232 advance = 0
233 }
234 }
235 }
236 }
237 if advance == 1 { i = i + 1 }
238 }
239 }
240 return result
241}
242
243func ie_is_ident(c: i64) -> i64 { if c >= 97 { if c <= 122 { return 1 } } return ie_is_upper_ident(c) }
244// substring within [s,e)
245func ie_span_has(code: *u8, s: i64, e: i64, lit: *u8) -> i64 { if e <= s { return 0 } return jc_find_sub(((code as i64) + s) as *u8, e - s, lit, ie_slen(lit)) }
246// start of the line containing p
247func ie_line_start(code: *u8, p: i64) -> i64 { var i: i64 = p; var go: i64 = 1; while go == 1 { if i <= 0 { go = 0 } else { if code[i - 1] == (10 as u8) { go = 0 } else { i = i - 1 } } } return i }
248// Is the site inside a while block? Walk back over balanced braces: every unmatched `{` opens an ENCLOSING block; a
249// `while` line opens a loop (LOOPED), a `func` line is the boundary (not looped). Braces inside strings and comments
250// were blanked by the stripper, so they do not count.
251func ie_in_while(code: *u8, lo: i64) -> i64 {
252 var i: i64 = lo - 1
253 var d: i64 = 0
254 while i >= 0 {
255 let c: i64 = code[i] as i64
256 if c == 125 { d = d + 1 }
257 if c == 123 {
258 if d > 0 { d = d - 1 } else {
259 let ls: i64 = ie_line_start(code, i)
260 if ie_span_has(code, ls, i, "while " as *u8) == 1 { return 1 }
261 if ie_span_has(code, ls, i, "func " as *u8) == 1 { return 0 }
262 }
263 }
264 i = i - 1
265 }
266 return 0
267}
268// the variable a `<v> = sys_read(` / `var <v>: i64 = sys_read(` line assigns, copied to name; its length, or 0
269func ie_assigned_var(code: *u8, lo: i64, site: i64, name: *u8) -> i64 {
270 var p: i64 = site - 1
271 var go: i64 = 1
272 while go == 1 { if p < lo { go = 0 } else { if code[p] == (32 as u8) { p = p - 1 } else { go = 0 } } }
273 if p < lo { return 0 }
274 if code[p] != (61 as u8) { return 0 }
275 p = p - 1
276 var vs: i64 = 0 - 1
277 var ve: i64 = 0 - 1
278 var again: i64 = 1
279 var pass: i64 = 0
280 while again == 1 {
281 go = 1
282 while go == 1 { if p < lo { go = 0 } else { if code[p] == (32 as u8) { p = p - 1 } else { go = 0 } } }
283 if p < lo { return 0 }
284 let te: i64 = p + 1
285 var ts: i64 = p
286 go = 1
287 while go == 1 { if ts < lo { go = 0 } else { if ie_is_ident(code[ts] as i64) == 1 { ts = ts - 1 } else { go = 0 } } }
288 ts = ts + 1
289 if te <= ts { return 0 }
290 vs = ts
291 ve = te
292 // a ':' before this token means it was the TYPE; the variable is the token before the ':'
293 var q: i64 = ts - 1
294 go = 1
295 while go == 1 { if q < lo { go = 0 } else { if code[q] == (32 as u8) { q = q - 1 } else { go = 0 } } }
296 again = 0
297 if q >= lo { if code[q] == (58 as u8) { p = q - 1; again = 1 } }
298 pass = pass + 1
299 if pass > 2 { again = 0 }
300 }
301 let nl: i64 = ve - vs
302 if nl >= IE_NAME_CAP { return 0 }
303 var k: i64 = 0
304 while k < nl { name[k] = code[vs + k]; k = k + 1 }
305 name[nl] = 0 as u8
306 return nl
307}
308// LOOPED: inside a while block, or the read's result variable drives a `while <v> > 0` / `while <v> != 0` in this file
309// (the prime-read-then-loop idiom, e.g. nx_sizeguard). Not judged, not acquitted -- a loop can still break early.
310// does the sys_read( call at site carry a RUNNING fill in its arguments (`buf + got`, `cap - got`)? A '+' or '-'
311// anywhere inside the call's parentheses; a fd, a bare buffer, a literal or a named constant never carry one.
312func ie_read_args_running(code: *u8, n: i64, site: i64) -> i64 {
313 var i: i64 = site + 9
314 var d: i64 = 1
315 while i < n {
316 let c: i64 = code[i] as i64
317 if c == 40 { d = d + 1 }
318 if c == 41 { d = d - 1; if d == 0 { return 0 } }
319 if c == 43 { return 1 }
320 if c == 45 { return 1 }
321 if c == 10 { return 0 }
322 i = i + 1
323 }
324 return 0
325}
326// LOOPED means the read DRIVES its loop: its result is tested against zero inside the loop, or its arguments carry a
327// running fill, or (the prime read) its result drives a later `while v > 0`. A read that merely SITS inside some loop
328// -- one request read per accepted connection, say -- is a FIXED site and is judged by its length like any other.
329// MEASURED 2026-09-06 before this rule: 2 of 5 sampled "capped loops" were per-connection reads inside accept loops
330// whose "bound" was an unrelated comparison elsewhere in a 200-line loop body.
331func ie_is_looped(code: *u8, n: i64, lo: i64, site: i64) -> i64 {
332 let name: *u8 = sys_mmap(IE_NAME_CAP)
333 let nl: i64 = ie_assigned_var(code, lo, site, name)
334 let ws: i64 = ie_enclosing_while(code, site)
335 if ws >= 0 {
336 let we: i64 = ie_loop_span_end(code, n, ws)
337 let cb: *u8 = sys_mmap((we - ws) * 2 + 16)
338 let cn: i64 = ie_canon_span(code, ws, we, cb)
339 if nl > 0 { if ie_span_eof(cb, 0, cn, name) == 1 { return 1 } }
340 if ie_read_args_running(code, n, site) == 1 { return 1 }
341 return 0
342 }
343 if nl <= 0 { return 0 }
344 // the prime read: its result drives a LATER while -- searched over the CANONICAL spelling, so
345 // `while r>0 {` and `while r > 0 {` are one idiom (offsets are not needed here, only presence)
346 let cf: *u8 = sys_mmap(n * 2 + 16)
347 let cfn: i64 = ie_canon_span(code, 0, n, cf)
348 let pat: *u8 = sys_mmap(IE_NAME_CAP + 16)
349 var o: i64 = ie_cat(pat, 0, "while " as *u8)
350 o = ie_cat(pat, o, name)
351 o = ie_cat(pat, o, " > 0" as *u8)
352 if ie_find(cf, cfn, pat) == 1 { return 1 }
353 o = ie_cat(pat, 0, "while " as *u8)
354 o = ie_cat(pat, o, name)
355 o = ie_cat(pat, o, " != 0" as *u8)
356 if ie_find(cf, cfn, pat) == 1 { return 1 }
357 return 0
358}
359// selftest witness: is the sys_read( on line ln LOOPED? (strip, find the site, then the SAME judge the scan uses)
360func ie_is_looped_src(raw: *u8, n: i64, ln: i64) -> i64 {
361 let code: *u8 = sys_mmap(n + 1)
362 jc_strip_noncode(raw, n, code)
363 let lo: i64 = ie_line_off(code, n, ln)
364 if lo < 0 { return 0 - 1 }
365 let site: i64 = ie_site_on_line(code, n, lo)
366 if site < 0 { return 0 - 1 }
367 return ie_is_looped(code, n, lo, site)
368}
369// offset of the first `sys_read(` on the line starting at lo, or -1 (ONE scanner for the three judges that need it)
370func ie_site_on_line(code: *u8, n: i64, lo: i64) -> i64 {
371 let e: i64 = ie_line_end(code, n, lo)
372 var p: i64 = lo
373 while p + 9 <= e { if jc_match_at(code, p, "sys_read(" as *u8, 9) == 1 { return p } p = p + 1 }
374 return 0 - 1
375}
376// 1-based line number of offset at in buf (newlines are preserved by every transform here, so a line number
377// computed on a canonical copy addresses the same line in the stripped code)
378func ie_line_of(buf: *u8, at: i64) -> i64 {
379 var ln: i64 = 1
380 var i: i64 = 0
381 while i < at { if buf[i] == (10 as u8) { ln = ln + 1 } i = i + 1 }
382 return ln
383}
384// CANONICAL SPELLING of a span for the idiom matchers: every comparison operator (< <= > >= == !=) is
385// emitted as ` op ` and runs of blanks collapse to one, so `if k<=0 {go=0}` and `if k <= 0 { go = 0 }` are
386// the SAME idiom to ie_span_eof / ie_span_bound. `->` and the shifts `<<` `>>` are copied verbatim (an
387// arrow is not a comparison and a shift amount is not a bound). Newlines are preserved. Offsets in the
388// canonical copy do NOT address the stripped code -- use ie_line_of to carry a position back by LINE.
389// MEASURED 2026-09-06: nx_ccheck, nx_media_server, nx_gallery_gateway one-line streaming copies were
390// listed as capped one-shots because the idiom was spelled without spaces and the brace sat on the read's
391// own line; both halves are fixed here (the walk starts at the read site, the matchers read canonical text).
392func ie_canon_span(code: *u8, s: i64, e: i64, out: *u8) -> i64 {
393 var o: i64 = 0
394 var i: i64 = s
395 var lastsp: i64 = 1
396 while i < e {
397 let c: i64 = code[i] as i64
398 var nx: i64 = 0
399 if i + 1 < e { nx = code[i + 1] as i64 }
400 var op: i64 = 0
401 var verb: i64 = 0
402 if c == 45 { if nx == 62 { verb = 2 } } // ->
403 if c == 60 { if nx == 60 { verb = 2 } } // <<
404 if c == 62 { if nx == 62 { verb = 2 } } // >>
405 if verb == 0 {
406 if c == 60 { op = 1 }
407 if c == 62 { op = 1 }
408 if op == 1 { if nx == 61 { op = 2 } }
409 if c == 61 { if nx == 61 { op = 2 } }
410 if c == 33 { if nx == 61 { op = 2 } }
411 }
412 if verb > 0 {
413 out[o] = c as u8; out[o + 1] = nx as u8; o = o + 2; i = i + 2; lastsp = 0
414 } else {
415 if op > 0 {
416 if lastsp == 0 { out[o] = 32 as u8; o = o + 1 }
417 out[o] = c as u8; o = o + 1
418 if op == 2 { out[o] = nx as u8; o = o + 1 }
419 out[o] = 32 as u8; o = o + 1
420 lastsp = 1
421 i = i + op
422 } else {
423 if c == 32 { if lastsp == 0 { out[o] = 32 as u8; o = o + 1; lastsp = 1 } i = i + 1 }
424 else { if c == 9 { if lastsp == 0 { out[o] = 32 as u8; o = o + 1; lastsp = 1 } i = i + 1 }
425 else { if c == 10 { out[o] = 10 as u8; o = o + 1; lastsp = 1; i = i + 1 }
426 else { out[o] = c as u8; o = o + 1; lastsp = 0; i = i + 1 } } }
427 }
428 }
429 }
430 out[o] = 0 as u8
431 return o
432}
433// ---- DI15: the loop's exit --------------------------------------------------------------------------------------
434// line start of the while that ENCLOSES position lo (the same brace walk as ie_in_while), or -1.
435// CALL IT WITH THE READ SITE, NOT THE LINE START: a `while go == 1 { ... sys_read(...) ... }` written on
436// one line has its brace BEFORE the read on the same line, and a walk that starts at the line start
437// never sees it (measured 2026-09-06 -- three one-line streaming copies listed as capped one-shots).
438func ie_enclosing_while(code: *u8, lo: i64) -> i64 {
439 var i: i64 = lo - 1
440 var d: i64 = 0
441 while i >= 0 {
442 let c: i64 = code[i] as i64
443 if c == 125 { d = d + 1 }
444 if c == 123 {
445 if d > 0 { d = d - 1 } else {
446 let ls: i64 = ie_line_start(code, i)
447 if ie_span_has(code, ls, i, "while " as *u8) == 1 { return ls }
448 if ie_span_has(code, ls, i, "func " as *u8) == 1 { return 0 - 1 }
449 }
450 }
451 i = i - 1
452 }
453 return 0 - 1
454}
455// index of the '}' matching the '{' at ob, or n
456func ie_block_end(code: *u8, n: i64, ob: i64) -> i64 {
457 var d: i64 = 1
458 var i: i64 = ob + 1
459 while i < n {
460 let c: i64 = code[i] as i64
461 if c == 123 { d = d + 1 }
462 if c == 125 { d = d - 1; if d == 0 { return i } }
463 i = i + 1
464 }
465 return n
466}
467// the span [ws, we) of the while statement whose line starts at ws: from the line start to its body's closing brace
468func ie_loop_span_end(code: *u8, n: i64, ws: i64) -> i64 {
469 var i: i64 = ws
470 var go: i64 = 1
471 while go == 1 { if i >= n { go = 0 } else { if code[i] == (123 as u8) { go = 0 } else { if code[i] == (10 as u8) { return ie_line_end(code, n, ws) } i = i + 1 } } }
472 if i >= n { return n }
473 return ie_block_end(code, n, i)
474}
475// does span [s,e) test the loop variable against zero (the EOF idiom)? name is NUL-terminated
476func ie_span_eof(code: *u8, s: i64, e: i64, name: *u8) -> i64 {
477 let pat: *u8 = sys_mmap(IE_NAME_CAP + 16)
478 var o: i64 = ie_cat(pat, 0, name); o = ie_cat(pat, o, " > 0" as *u8)
479 if ie_span_has(code, s, e, pat) == 1 { return 1 }
480 o = ie_cat(pat, 0, name); o = ie_cat(pat, o, " != 0" as *u8)
481 if ie_span_has(code, s, e, pat) == 1 { return 1 }
482 o = ie_cat(pat, 0, name); o = ie_cat(pat, o, " <= 0" as *u8)
483 if ie_span_has(code, s, e, pat) == 1 { return 1 }
484 o = ie_cat(pat, 0, name); o = ie_cat(pat, o, " < 0" as *u8)
485 if ie_span_has(code, s, e, pat) == 1 { return 1 }
486 o = ie_cat(pat, 0, name); o = ie_cat(pat, o, " == 0" as *u8)
487 if ie_span_has(code, s, e, pat) == 1 { return 1 }
488 o = ie_cat(pat, 0, name); o = ie_cat(pat, o, " < 1" as *u8)
489 if ie_span_has(code, s, e, pat) == 1 { return 1 }
490 return 0
491}
492func ie_is_lower(c: i64) -> i64 { if c >= 97 { if c <= 122 { return 1 } } return 0 }
493// is the token [ts,te) a BOUND: digits other than 0 or 1, an ALL-CAPS name, or a lowercase name that names a cap
494func ie_token_is_bound(code: *u8, ts: i64, te: i64) -> i64 {
495 if te <= ts { return 0 }
496 var alld: i64 = 1
497 var allu: i64 = 1
498 var i: i64 = ts
499 while i < te { let c: i64 = code[i] as i64; if ie_is_digit(c) == 0 { alld = 0 } if ie_is_upper_ident(c) == 0 { allu = 0 } i = i + 1 }
500 if alld == 1 {
501 if te - ts == 1 { if code[ts] == (48 as u8) { return 0 } if code[ts] == (49 as u8) { return 0 } }
502 return 1
503 }
504 if allu == 1 { return 1 }
505 if ie_span_has(code, ts, te, "cap" as *u8) == 1 { return 1 }
506 if ie_span_has(code, ts, te, "max" as *u8) == 1 { return 1 }
507 if ie_span_has(code, ts, te, "lim" as *u8) == 1 { return 1 }
508 if ie_span_has(code, ts, te, "bound" as *u8) == 1 { return 1 }
509 if ie_span_has(code, ts, te, "budget" as *u8) == 1 { return 1 }
510 return 0
511}
512// does span [s,e) compare something against a BOUND (` >= B`, ` < B`, ` <= B`, ` > B`)? copies the bound token out
513func ie_span_bound(code: *u8, s: i64, e: i64, bound: *u8) -> i64 {
514 bound[0] = 0 as u8
515 var i: i64 = s
516 while i + 3 < e {
517 var oplen: i64 = 0
518 if code[i] == (32 as u8) {
519 if code[i + 1] == (62 as u8) { if code[i + 2] == (61 as u8) { if code[i + 3] == (32 as u8) { oplen = 4 } } else { if code[i + 2] == (32 as u8) { oplen = 3 } } }
520 if code[i + 1] == (60 as u8) { if code[i + 2] == (61 as u8) { if code[i + 3] == (32 as u8) { oplen = 4 } } else { if code[i + 2] == (32 as u8) { oplen = 3 } } }
521 }
522 if oplen > 0 {
523 let ts: i64 = i + oplen
524 var te: i64 = ts
525 var go: i64 = 1
526 while go == 1 { if te >= e { go = 0 } else { if ie_is_ident(code[te] as i64) == 1 { te = te + 1 } else { go = 0 } } }
527 if ie_token_is_bound(code, ts, te) == 1 {
528 var k: i64 = 0
529 while k < te - ts { if k + 1 < IE_BOUND_CAP { bound[k] = code[ts + k] } k = k + 1 }
530 if k + 1 < IE_BOUND_CAP { bound[k] = 0 as u8 } else { bound[IE_BOUND_CAP - 1] = 0 as u8 }
531 return 1
532 }
533 i = te
534 } else { i = i + 1 }
535 }
536 return 0
537}
538// JUDGE the loop around the sys_read( on line ln. code is the stripped file. bound receives the named bound for CAPPED.
539func ie_loop_exit(code: *u8, n: i64, ln: i64, bound: *u8) -> i64 {
540 bound[0] = 0 as u8
541 let lo: i64 = ie_line_off(code, n, ln)
542 if lo < 0 { return IE_LOOP_UNJUDGED }
543 let site: i64 = ie_site_on_line(code, n, lo)
544 if site < 0 { return IE_LOOP_UNJUDGED }
545 let name: *u8 = sys_mmap(IE_NAME_CAP)
546 let nl: i64 = ie_assigned_var(code, lo, site, name)
547 var ws: i64 = ie_enclosing_while(code, site)
548 if ws < 0 {
549 // the prime read: its result drives a LATER while -- find that while (canonical spelling) and judge its body
550 if nl <= 0 { return IE_LOOP_UNJUDGED }
551 let cf: *u8 = sys_mmap(n * 2 + 16)
552 let cfn: i64 = ie_canon_span(code, 0, n, cf)
553 let pat: *u8 = sys_mmap(IE_NAME_CAP + 16)
554 var o: i64 = ie_cat(pat, 0, "while " as *u8); o = ie_cat(pat, o, name); o = ie_cat(pat, o, " > 0" as *u8)
555 var at: i64 = jc_find_sub_at(cf, cfn, pat, ie_slen(pat))
556 if at < 0 { o = ie_cat(pat, 0, "while " as *u8); o = ie_cat(pat, o, name); o = ie_cat(pat, o, " != 0" as *u8); at = jc_find_sub_at(cf, cfn, pat, ie_slen(pat)) }
557 if at < 0 { return IE_LOOP_UNJUDGED }
558 ws = ie_line_off(code, n, ie_line_of(cf, at))
559 if ws < 0 { return IE_LOOP_UNJUDGED }
560 }
561 let we: i64 = ie_loop_span_end(code, n, ws)
562 let cb: *u8 = sys_mmap((we - ws) * 2 + 16)
563 let cn: i64 = ie_canon_span(code, ws, we, cb)
564 if ie_span_bound(cb, 0, cn, bound) == 1 { return IE_LOOP_CAPPED }
565 if nl > 0 { if ie_span_eof(cb, 0, cn, name) == 1 { return IE_LOOP_STREAMING } }
566 // the flag idiom: the read's result is tested against zero inside the body even when the while tests a flag
567 return IE_LOOP_UNJUDGED
568}
569// position of the first occurrence of pat (NUL-terminated) in code[0,n), or -1
570func jc_find_sub_at(code: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
571 var i: i64 = 0
572 while i + pl <= n { if jc_match_at(code, i, pat, pl) == 1 { return i } i = i + 1 }
573 return 0 - 1
574}
575func ie_loop_exit_src(raw: *u8, n: i64, ln: i64, bound: *u8) -> i64 {
576 let code: *u8 = sys_mmap(n + 1)
577 jc_strip_noncode(raw, n, code)
578 return ie_loop_exit(code, n, ln, bound)
579}
580// does a LOOPED read at line ln belong on the silent worklist? 1 = CAPPED in a file that does not announce its
581// envelope; 0 = STREAMING, UNJUDGED, or CAPPED in a file that announces (the same file-level test fixed sites use)
582func ie_loop_worklist(code: *u8, raw: *u8, n: i64, ln: i64, bound: *u8) -> i64 {
583 if ie_loop_exit(code, n, ln, bound) != IE_LOOP_CAPPED { return 0 }
584 if ie_file_announces(raw, n) == 1 { return 0 }
585 return 1
586}
587func ie_loop_worklist_src(raw: *u8, n: i64, ln: i64, bound: *u8) -> i64 {
588 let code: *u8 = sys_mmap(n + 1)
589 jc_strip_noncode(raw, n, code)
590 return ie_loop_worklist(code, raw, n, ln, bound)
591}
592func ie_loop_name(lx: i64) -> *u8 {
593 if lx == IE_LOOP_STREAMING { return "STREAMING" as *u8 }
594 if lx == IE_LOOP_CAPPED { return "CAPPED" as *u8 }
595 return "UNJUDGED" as *u8
596}
597
598// the identity text of line ln in the STRIPPED file: trimmed, whitespace runs collapsed to one space, cut at IE_TEXT_CAP
599func ie_key_text(code: *u8, n: i64, ln: i64, dst: *u8) -> i64 {
600 dst[0] = 0 as u8
601 let lo: i64 = ie_line_off(code, n, ln)
602 if lo < 0 { return 0 }
603 let e: i64 = ie_line_end(code, n, lo)
604 var o: i64 = 0
605 var i: i64 = lo
606 var pend: i64 = 0
607 while i < e {
608 var c: i64 = code[i] as i64
609 if c == IE_TAB { c = 32 }
610 if c == 13 { c = 32 }
611 if c == 32 { if o > 0 { pend = 1 } } else {
612 if pend == 1 { if o + 1 < IE_TEXT_CAP { dst[o] = 32 as u8; o = o + 1 } pend = 0 }
613 if o + 1 < IE_TEXT_CAP { dst[o] = c as u8; o = o + 1 }
614 }
615 i = i + 1
616 }
617 dst[o] = 0 as u8
618 return o
619}
620// the idx-th TAB-separated field of the row [rs,re): sp[0]=start sp[1]=end; 1 when present
621func ie_field(buf: *u8, rs: i64, re: i64, idx: i64, sp: *i64) -> i64 {
622 var f: i64 = 0
623 var s: i64 = rs
624 var i: i64 = rs
625 while i <= re {
626 var cut: i64 = 0
627 if i == re { cut = 1 } else { if buf[i] == (IE_TAB as u8) { cut = 1 } }
628 if cut == 1 {
629 if f == idx { sp[0] = s; sp[1] = i; return 1 }
630 f = f + 1
631 s = i + 1
632 }
633 i = i + 1
634 }
635 return 0
636}
637func ie_span_eq(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 { let ll: i64 = ie_slen(lit); if e - s != ll { return 0 } return jc_match_at(buf, s, lit, ll) }
638// how many rows already in silent[0..so) carry this path and this text (the ordinal k of the next such row)
639func ie_count_same(silent: *u8, so: i64, path: *u8, text: *u8) -> i64 {
640 var k: i64 = 0
641 var rs: i64 = 0
642 let sp: *i64 = sys_mmap(16) as *i64
643 while rs < so {
644 let re: i64 = ie_line_end(silent, so, rs)
645 if re > rs {
646 if ie_field(silent, rs, re, 0, sp) == 1 { if ie_span_eq(silent, sp[0], sp[1], path) == 1 {
647 if ie_field(silent, rs, re, 2, sp) == 1 { if ie_span_eq(silent, sp[0], sp[1], text) == 1 { k = k + 1 } }
648 } }
649 }
650 rs = re + 1
651 }
652 return k
653}
654// append one keyed row `path TAB k TAB text TAB line NL` to silent at so; the new so
655func ie_row_put(silent: *u8, so: i64, path: *u8, k: i64, text: *u8, ln: i64) -> i64 {
656 var o: i64 = ie_cat(silent, so, path)
657 silent[o] = IE_TAB as u8; o = o + 1
658 o = ie_catn(silent, o, k)
659 silent[o] = IE_TAB as u8; o = o + 1
660 o = ie_cat(silent, o, text)
661 silent[o] = IE_TAB as u8; o = o + 1
662 o = ie_catn(silent, o, ln)
663 silent[o] = 10 as u8; o = o + 1
664 silent[o] = 0 as u8
665 return o
666}
667
668// CLASSIFY the sys_read( site on line ln. code = the stripped file (strings/comments blanked, newlines kept), raw = the
669// file as written. out_val receives the resolved length for a FIXED class, else -1. Returns an IE_ class.
670func ie_classify(code: *u8, raw: *u8, n: i64, ln: i64, out_val: *i64) -> i64 {
671 out_val[0] = 0 - 1
672 let lo: i64 = ie_line_off(code, n, ln)
673 if lo < 0 { return IE_NOT_A_SITE }
674 let e: i64 = ie_line_end(code, n, lo)
675 let site: i64 = ie_site_on_line(code, n, lo)
676 if site < 0 { return IE_NOT_A_SITE }
677 if ie_is_looped(code, n, lo, site) == 1 { return IE_LOOPED }
678 // the third argument at paren depth 0
679 var q: i64 = site + 9
680 var depth: i64 = 0
681 var commas: i64 = 0
682 var a3s: i64 = 0 - 1
683 var a3e: i64 = 0 - 1
684 var go: i64 = 1
685 while go == 1 {
686 if q >= e { go = 0 } else {
687 let c: i64 = code[q] as i64
688 if c == 40 { depth = depth + 1 }
689 if c == 41 { if depth == 0 { if commas == 2 { a3e = q } go = 0 } else { depth = depth - 1 } }
690 if c == 44 { if depth == 0 { commas = commas + 1; if commas == 2 { a3s = q + 1 } } }
691 if go == 1 { q = q + 1 }
692 }
693 }
694 if a3s < 0 { return IE_DYNAMIC }
695 if a3e < 0 { return IE_DYNAMIC }
696 let s: i64 = ie_skip_spaces(code, a3e, a3s)
697 var t: i64 = a3e
698 var tr: i64 = 1
699 while tr == 1 { if t <= s { tr = 0 } else { if code[t - 1] == (32 as u8) { t = t - 1 } else { tr = 0 } } }
700 if t <= s { return IE_DYNAMIC }
701 let ann: i64 = ie_file_announces(raw, n)
702 // all digits -> LITERAL
703 var alld: i64 = 1
704 var i: i64 = s
705 while i < t { if ie_is_digit(code[i] as i64) == 0 { alld = 0 } i = i + 1 }
706 if alld == 1 {
707 var v: i64 = 0
708 i = s
709 while i < t { v = v * 10 + ((code[i] as i64) - 48); i = i + 1 }
710 out_val[0] = v
711 if ann == 1 { return IE_LITERAL_ANN }
712 return IE_LITERAL_SILENT
713 }
714 // all upper-ident -> NAMED if declared as a numeric const in this file
715 var allu: i64 = 1
716 i = s
717 while i < t { if ie_is_upper_ident(code[i] as i64) == 0 { allu = 0 } i = i + 1 }
718 if allu == 1 {
719 let nl: i64 = t - s
720 if nl < IE_NAME_CAP {
721 let name: *u8 = sys_mmap(IE_NAME_CAP)
722 var k: i64 = 0
723 while k < nl { name[k] = code[s + k]; k = k + 1 }
724 name[nl] = 0 as u8
725 let v: i64 = ie_const_value(code, n, name, nl)
726 if v >= 0 {
727 out_val[0] = v
728 if ann == 1 { return IE_NAMED_ANN }
729 return IE_NAMED_SILENT
730 }
731 }
732 }
733 return IE_DYNAMIC
734}
735
736// classify a whole in-memory source (strips it first): the unit the selftest and the census both use
737func ie_classify_src(raw: *u8, n: i64, ln: i64, out_val: *i64) -> i64 {
738 let code: *u8 = sys_mmap(n + 1)
739 jc_strip_noncode(raw, n, code)
740 return ie_classify(code, raw, n, ln, out_val)
741}
742
743// ---- the ratchet (DI12) -------------------------------------------------------------------------------------------
744// the byte length of a file, or -1 when absent (never a NUL-scan over a buffer that may not carry one)
745func ie_file_len(path: *u8) -> i64 {
746 let l: *i64 = sys_mmap(16) as *i64
747 let b: *u8 = sys_read_file(path, l)
748 if (b as i64) == 0 { return 0 - 1 }
749 return l[0]
750}
751// 1 when the file's bytes equal s[0..n) exactly
752func ie_file_eq(path: *u8, s: *u8, n: i64) -> i64 {
753 let l: *i64 = sys_mmap(16) as *i64
754 let b: *u8 = sys_read_file(path, l)
755 if (b as i64) == 0 { return 0 }
756 if l[0] != n { return 0 }
757 var i: i64 = 0
758 while i < n { if b[i] != s[i] { return 0 } i = i + 1 }
759 return 1
760}
761func ie_write_file(path: *u8, s: *u8, n: i64) -> i64 {
762 let fd: i64 = sys_openat_wr(path, IE_MODE_644)
763 if fd < 0 { return 0 - 1 }
764 sys_write(fd, s, n)
765 sys_close(fd)
766 return n
767}
768func ie_ledger_append(ledger: *u8, row: *u8) -> i64 {
769 let fd: i64 = sys_openat_append(ledger, IE_MODE_644)
770 if fd < 0 { return 0 - 1 }
771 let n: i64 = ie_slen(row)
772 sys_write(fd, row, n)
773 sys_close(fd)
774 return n
775}
776func ie_class_name(cls: i64) -> *u8 {
777 if cls == IE_NOT_A_SITE { return "converted" as *u8 }
778 if cls == IE_LITERAL_ANN { return "announced" as *u8 }
779 if cls == IE_NAMED_ANN { return "announced" as *u8 }
780 if cls == IE_DYNAMIC { return "dynamic" as *u8 }
781 if cls == IE_LOOPED { return "looped" as *u8 }
782 return "silent" as *u8
783}
784// What did a site that LEFT the silent set become? Re-read its file: a keyed row is located by its text's k-th
785// occurrence, a legacy row by its line number; an absent file or an absent line is "converted" (the reader is gone).
786func ie_now_class(path: *u8, k: i64, text: *u8, ln: i64, legacy: i64) -> *u8 {
787 let rl: *i64 = sys_mmap(16) as *i64
788 let raw: *u8 = sys_read_file(path, rl)
789 if (raw as i64) == 0 { return "converted" as *u8 }
790 let n: i64 = rl[0]
791 let code: *u8 = sys_mmap(n + 1)
792 jc_strip_noncode(raw, n, code)
793 let v: *i64 = sys_mmap(16) as *i64
794 if legacy == 1 { return ie_class_name(ie_classify(code, raw, n, ln, v)) }
795 let kt: *u8 = sys_mmap(IE_TEXT_CAP)
796 var cur: i64 = 1
797 var seen: i64 = 0
798 var i: i64 = 0
799 var go: i64 = 1
800 while go == 1 {
801 if i > n { go = 0 } else {
802 ie_key_text(code, n, cur, kt)
803 if ie_streq(kt, text) == 1 { if seen == k { return ie_class_name(ie_classify(code, raw, n, cur, v)) } seen = seen + 1 }
804 let e: i64 = ie_line_end(code, n, i)
805 i = e + 1
806 cur = cur + 1
807 }
808 }
809 return "converted" as *u8
810}
811// the true/false positive tally from the ledger: out[0]=true_positive out[1]=false_positive
812func ie_rate(ledger: *u8, out: *i64) -> i64 {
813 out[0] = 0
814 out[1] = 0
815 let ll: *i64 = sys_mmap(16) as *i64
816 let lb: *u8 = sys_read_file(ledger, ll)
817 if (lb as i64) == 0 { return 0 }
818 let n: i64 = ll[0]
819 var rs: i64 = 0
820 while rs < n {
821 let re: i64 = ie_line_end(lb, n, rs)
822 if re > rs {
823 if ie_span_has(lb, rs, re, "kind=left" as *u8) == 1 {
824 if ie_span_has(lb, rs, re, " now=looped" as *u8) == 1 { out[1] = out[1] + 1 } else { out[0] = out[0] + 1 }
825 }
826 if ie_span_has(lb, rs, re, "kind=false" as *u8) == 1 { out[1] = out[1] + 1 }
827 }
828 rs = re + 1
829 }
830 return out[0] + out[1]
831}
832// THE RATCHET. silent[0..so) holds the current keyed rows. Returns new_silent (the rise); out[0]=new out[1]=left
833// out[2]=baseline state. A rise never rewrites the baseline (unless --accept); a fall tightens it; a legacy baseline
834// (path:line rows, no TAB) is matched by line once and migrated to the keyed form.
835func ie_ratchet(silent: *u8, so: i64, baseline: *u8, ledger: *u8, accept: i64, out: *i64) -> i64 {
836 out[0] = 0
837 out[1] = 0
838 out[2] = IE_BS_PRESENT
839 let bl: *i64 = sys_mmap(16) as *i64
840 let bb0: *u8 = sys_read_file(baseline, bl)
841 var have_base: i64 = 0
842 var bn: i64 = 0
843 if (bb0 as i64) != 0 { have_base = 1; bn = bl[0] }
844 var legacy: i64 = 0
845 let tb: *u8 = sys_mmap(2)
846 tb[0] = IE_TAB as u8
847 tb[1] = 0 as u8
848 if have_base == 1 { if bn > 0 { if jc_find_sub(bb0, bn, tb, 1) == 0 { legacy = 1 } } }
849 // "\n"-framed copies for membership tests
850 let bb: *u8 = sys_mmap(bn + 2)
851 bb[0] = 10 as u8
852 var k: i64 = 0
853 while k < bn { bb[k + 1] = bb0[k]; k = k + 1 }
854 bb[bn + 1] = 0 as u8
855 let cs: *u8 = sys_mmap(so + 2)
856 cs[0] = 10 as u8
857 k = 0
858 while k < so { cs[k + 1] = silent[k]; k = k + 1 }
859 cs[so + 1] = 0 as u8
860 // the legacy view of the current set: path:line rows
861 let lv: *u8 = sys_mmap(so + IE_ROW_CAP)
862 lv[0] = 10 as u8
863 var lo: i64 = 1
864 let sp: *i64 = sys_mmap(16) as *i64
865 let sp2: *i64 = sys_mmap(16) as *i64
866 var rows: i64 = 0
867 var rs: i64 = 0
868 while rs < so {
869 let re: i64 = ie_line_end(silent, so, rs)
870 if re > rs {
871 rows = rows + 1
872 if ie_field(silent, rs, re, 0, sp) == 1 { if ie_field(silent, rs, re, 3, sp2) == 1 {
873 var m: i64 = sp[0]
874 while m < sp[1] { lv[lo] = silent[m]; lo = lo + 1; m = m + 1 }
875 lv[lo] = 58 as u8; lo = lo + 1
876 m = sp2[0]
877 while m < sp2[1] { lv[lo] = silent[m]; lo = lo + 1; m = m + 1 }
878 lv[lo] = 10 as u8; lo = lo + 1
879 } }
880 }
881 rs = re + 1
882 }
883 lv[lo] = 0 as u8
884 // RISE: current rows absent from the baseline. inb[i]=1 when row i was in the baseline (kept on tighten)
885 let inb: *i64 = sys_mmap(8 * (rows + 1)) as *i64
886 let needle: *u8 = sys_mmap(IE_ROW_CAP + 8)
887 let ts: i64 = sys_now_realtime_sec()
888 let lrow: *u8 = sys_mmap(IE_ROW_CAP * 2)
889 var ri: i64 = 0
890 rs = 0
891 while rs < so {
892 let re: i64 = ie_line_end(silent, so, rs)
893 if re > rs {
894 var found: i64 = 0
895 if have_base == 1 {
896 if legacy == 1 {
897 // "\n" + path:line + "\n"
898 var o: i64 = 0
899 needle[o] = 10 as u8; o = o + 1
900 ie_field(silent, rs, re, 0, sp)
901 var m: i64 = sp[0]
902 while m < sp[1] { needle[o] = silent[m]; o = o + 1; m = m + 1 }
903 needle[o] = 58 as u8; o = o + 1
904 ie_field(silent, rs, re, 3, sp2)
905 m = sp2[0]
906 while m < sp2[1] { needle[o] = silent[m]; o = o + 1; m = m + 1 }
907 needle[o] = 10 as u8; o = o + 1
908 found = jc_find_sub(bb, bn + 1, needle, o)
909 } else {
910 // "\n" + path TAB k TAB text TAB (the key; the line number is not identity)
911 ie_field(silent, rs, re, 2, sp)
912 let kl: i64 = sp[1] + 1 - rs
913 if kl + 1 < IE_ROW_CAP {
914 needle[0] = 10 as u8
915 var m2: i64 = 0
916 while m2 < kl { needle[1 + m2] = silent[rs + m2]; m2 = m2 + 1 }
917 found = jc_find_sub(bb, bn + 1, needle, kl + 1)
918 }
919 }
920 }
921 inb[ri] = found
922 if found == 0 { if have_base == 1 {
923 out[0] = out[0] + 1
924 ie_puts("NEW-SILENT " as *u8)
925 ie_field(silent, rs, re, 0, sp); sys_write(1, ((silent as i64) + sp[0]) as *u8, sp[1] - sp[0])
926 ie_puts(":" as *u8)
927 ie_field(silent, rs, re, 3, sp); sys_write(1, ((silent as i64) + sp[0]) as *u8, sp[1] - sp[0])
928 ie_puts(" (not in the baseline: a reader that fell out of derivation, or one nobody has adjudicated yet; the baseline is NOT rewritten)\n" as *u8)
929 var o2: i64 = ie_cat(lrow, 0, "ts=" as *u8); o2 = ie_catn(lrow, o2, ts); o2 = ie_cat(lrow, o2, " kind=new site=" as *u8)
930 ie_field(silent, rs, re, 0, sp)
931 var m3: i64 = sp[0]
932 while m3 < sp[1] { lrow[o2] = silent[m3]; o2 = o2 + 1; m3 = m3 + 1 }
933 lrow[o2] = 58 as u8; o2 = o2 + 1
934 ie_field(silent, rs, re, 3, sp)
935 m3 = sp[0]
936 while m3 < sp[1] { lrow[o2] = silent[m3]; o2 = o2 + 1; m3 = m3 + 1 }
937 lrow[o2] = 10 as u8; o2 = o2 + 1
938 lrow[o2] = 0 as u8
939 ie_ledger_append(ledger, lrow)
940 } }
941 ri = ri + 1
942 }
943 rs = re + 1
944 }
945 // FALL: baseline rows absent from the current set
946 if have_base == 1 {
947 let path: *u8 = sys_mmap(IE_PATH_CAP)
948 let text: *u8 = sys_mmap(IE_TEXT_CAP)
949 var brs: i64 = 0
950 while brs < bn {
951 let bre: i64 = ie_line_end(bb0, bn, brs)
952 if bre > brs {
953 var present: i64 = 0
954 var kk: i64 = 0
955 var ln: i64 = 0
956 if legacy == 1 {
957 var o: i64 = 0
958 needle[o] = 10 as u8; o = o + 1
959 var m: i64 = brs
960 while m < bre { needle[o] = bb0[m]; o = o + 1; m = m + 1 }
961 needle[o] = 10 as u8; o = o + 1
962 present = jc_find_sub(lv, lo, needle, o)
963 // path:line -> path, line
964 var c: i64 = bre
965 var seek: i64 = 1
966 while seek == 1 { if c <= brs { seek = 0 } else { if bb0[c - 1] == (58 as u8) { seek = 0 } else { c = c - 1 } } }
967 var pi: i64 = brs
968 var po: i64 = 0
969 while pi < c - 1 { path[po] = bb0[pi]; po = po + 1; pi = pi + 1 }
970 path[po] = 0 as u8
971 var li: i64 = c
972 while li < bre { ln = ln * 10 + ((bb0[li] as i64) - 48); li = li + 1 }
973 text[0] = 0 as u8
974 } else {
975 ie_field(bb0, brs, bre, 2, sp)
976 let kl: i64 = sp[1] + 1 - brs
977 if kl + 1 < IE_ROW_CAP {
978 needle[0] = 10 as u8
979 var m2: i64 = 0
980 while m2 < kl { needle[1 + m2] = bb0[brs + m2]; m2 = m2 + 1 }
981 present = jc_find_sub(cs, so + 1, needle, kl + 1)
982 }
983 ie_field(bb0, brs, bre, 0, sp)
984 var po2: i64 = 0
985 var pi2: i64 = sp[0]
986 while pi2 < sp[1] { path[po2] = bb0[pi2]; po2 = po2 + 1; pi2 = pi2 + 1 }
987 path[po2] = 0 as u8
988 ie_field(bb0, brs, bre, 1, sp)
989 var ki: i64 = sp[0]
990 while ki < sp[1] { kk = kk * 10 + ((bb0[ki] as i64) - 48); ki = ki + 1 }
991 ie_field(bb0, brs, bre, 2, sp)
992 var to: i64 = 0
993 var ti: i64 = sp[0]
994 while ti < sp[1] { if to + 1 < IE_TEXT_CAP { text[to] = bb0[ti]; to = to + 1 } ti = ti + 1 }
995 text[to] = 0 as u8
996 if ie_field(bb0, brs, bre, 3, sp) == 1 { var li2: i64 = sp[0]; while li2 < sp[1] { ln = ln * 10 + ((bb0[li2] as i64) - 48); li2 = li2 + 1 } }
997 }
998 if present == 0 {
999 out[1] = out[1] + 1
1000 let now: *u8 = ie_now_class(path, kk, text, ln, legacy)
1001 ie_puts("LEFT-SILENT " as *u8); ie_puts(path); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" now=" as *u8); ie_puts(now); ie_puts(" (no longer in the silent set; the baseline is tightened)\n" as *u8)
1002 var o3: i64 = ie_cat(lrow, 0, "ts=" as *u8); o3 = ie_catn(lrow, o3, ts); o3 = ie_cat(lrow, o3, " kind=left site=" as *u8); o3 = ie_cat(lrow, o3, path); o3 = ie_cat(lrow, o3, ":" as *u8); o3 = ie_catn(lrow, o3, ln)
1003 o3 = ie_cat(lrow, o3, " now=" as *u8); o3 = ie_cat(lrow, o3, now); o3 = ie_cat(lrow, o3, " key=" as *u8); o3 = ie_cat(lrow, o3, path); o3 = ie_cat(lrow, o3, "|" as *u8); o3 = ie_catn(lrow, o3, kk); o3 = ie_cat(lrow, o3, "|" as *u8); o3 = ie_cat(lrow, o3, text); o3 = ie_cat(lrow, o3, "\n" as *u8)
1004 ie_ledger_append(ledger, lrow)
1005 }
1006 }
1007 brs = bre + 1
1008 }
1009 }
1010 // the baseline write: CREATED (first sight) | REWRITTEN (--accept) | MIGRATED (legacy form) | TIGHTENED (a fall) | PRESENT
1011 var write_all: i64 = 0
1012 var write_kept: i64 = 0
1013 if have_base == 0 { write_all = 1; out[2] = IE_BS_CREATED }
1014 if have_base == 1 { if accept == 1 { write_all = 1; out[2] = IE_BS_REWRITTEN } }
1015 if write_all == 0 { if have_base == 1 { if legacy == 1 { write_kept = 1; out[2] = IE_BS_MIGRATED } else { if out[1] > 0 { write_kept = 1; out[2] = IE_BS_TIGHTENED } } } }
1016 if write_all == 1 { ie_write_file(baseline, silent, so) }
1017 if write_kept == 1 {
1018 let kept: *u8 = sys_mmap(so + 2)
1019 var ko: i64 = 0
1020 var rj: i64 = 0
1021 rs = 0
1022 while rs < so {
1023 let re: i64 = ie_line_end(silent, so, rs)
1024 if re > rs {
1025 if inb[rj] == 1 { var m4: i64 = rs; while m4 <= re { kept[ko] = silent[m4]; ko = ko + 1; m4 = m4 + 1 } }
1026 rj = rj + 1
1027 }
1028 rs = re + 1
1029 }
1030 ie_write_file(baseline, kept, ko)
1031 }
1032 ie_puts("baseline=" as *u8)
1033 if out[2] == IE_BS_CREATED { ie_puts("CREATED" as *u8) }
1034 if out[2] == IE_BS_PRESENT { ie_puts("PRESENT" as *u8) }
1035 if out[2] == IE_BS_REWRITTEN { ie_puts("REWRITTEN(--accept)" as *u8) }
1036 if out[2] == IE_BS_TIGHTENED { ie_puts("TIGHTENED" as *u8) }
1037 if out[2] == IE_BS_MIGRATED { ie_puts("MIGRATED(legacy path:line rows re-keyed)" as *u8) }
1038 ie_puts(" rows=" as *u8); ie_putn(rows); ie_puts(" new_silent=" as *u8); ie_putn(out[0]); ie_puts(" left_silent=" as *u8); ie_putn(out[1]); ie_puts("\n" as *u8)
1039 return out[0]
1040}
1041
1042// ---- the census over the walker's output ------------------------------------------------------------------------
1043func ie_scan(root: *u8, accept: i64) -> i64 {
1044 ie_puts("=== nx_io_envelope_census -- every capped reader announces or derives (root=" as *u8); ie_puts(root); ie_puts(") ===\n" as *u8)
1045 // 1. the walker: nx_shelltool grep "sys_read(" <root> .nx (budgeted, envelope-honest)
1046 let av: *i64 = sys_mmap(64) as *i64
1047 av[0] = IE_SHELLTOOL as i64
1048 av[1] = "grep" as *u8 as i64
1049 av[2] = "sys_read(" as *u8 as i64
1050 av[3] = root as i64
1051 av[4] = ".nx" as *u8 as i64
1052 av[5] = 0
1053 let gout: *u8 = sys_mmap(IE_GREP_CAP)
1054 let glen: *i64 = sys_mmap(16) as *i64
1055 glen[0] = 0
1056 let grc: i64 = tr_run_capture(IE_SHELLTOOL, av, gout, IE_GREP_CAP - 8, glen)
1057 let gn: i64 = glen[0]
1058 var cap_hit: i64 = 0
1059 if gn >= IE_GREP_CAP - 8 { cap_hit = 1 }
1060 let env: *i64 = sys_mmap(32) as *i64
1061 ie_walker_env(gout, gn, env)
1062 let matches: i64 = env[0]
1063 let cov: i64 = env[1]
1064 let corp: i64 = env[2]
1065 var walker_ok: i64 = 1
1066 if grc != 0 { walker_ok = 0 }
1067 if matches < 0 { walker_ok = 0 }
1068 if cov != 1 { walker_ok = 0 }
1069 if corp != 1 { walker_ok = 0 }
1070 if cap_hit == 1 { walker_ok = 0 }
1071 ie_puts("walker=nx_shelltool grep rc=" as *u8); ie_putn(grc); ie_puts(" matches=" as *u8); ie_putn(matches)
1072 ie_puts(" coverage_complete=" as *u8); ie_putn(cov); ie_puts(" corpus_complete=" as *u8); ie_putn(corp)
1073 ie_puts(" capture_bytes=" as *u8); ie_putn(gn); ie_puts(" cap_hit=" as *u8); ie_putn(cap_hit); ie_puts("\n" as *u8)
1074 // 2. the derived axis: sys_read_file( call sites (count only)
1075 av[2] = "sys_read_file(" as *u8 as i64
1076 let dout: *u8 = sys_mmap(IE_GREP_CAP)
1077 let dlen: *i64 = sys_mmap(16) as *i64
1078 dlen[0] = 0
1079 tr_run_capture(IE_SHELLTOOL, av, dout, IE_GREP_CAP - 8, dlen)
1080 let derived: i64 = ie_int_after(dout, dlen[0], "-- matches=" as *u8)
1081 // 3. iterate hits: path:line:text (grouped per file by the walker)
1082 var hits: i64 = 0
1083 var not_site: i64 = 0
1084 var lit_ann: i64 = 0
1085 var lit_silent: i64 = 0
1086 var named_ann: i64 = 0
1087 var named_silent: i64 = 0
1088 var dynamic: i64 = 0
1089 var looped: i64 = 0
1090 var looped_streaming: i64 = 0
1091 var looped_capped: i64 = 0
1092 var looped_capped_ann: i64 = 0
1093 var looped_unjudged: i64 = 0
1094 var files_seen: i64 = 0
1095 var files_unreadable: i64 = 0
1096 var silent_overflow: i64 = 0
1097 let silent: *u8 = sys_mmap(IE_SILENT_CAP)
1098 let ktext: *u8 = sys_mmap(IE_TEXT_CAP)
1099 let bound: *u8 = sys_mmap(IE_BOUND_CAP)
1100 var so: i64 = 0
1101 let curpath: *u8 = sys_mmap(IE_PATH_CAP)
1102 curpath[0] = 0 as u8
1103 var raw: *u8 = "" as *u8
1104 var rawn: i64 = 0 - 1
1105 var code: *u8 = "" as *u8
1106 let val: *i64 = sys_mmap(16) as *i64
1107 var ls: i64 = 0
1108 var iter: i64 = 1
1109 while iter == 1 {
1110 if ls >= gn { iter = 0 } else {
1111 let le: i64 = ie_line_end(gout, gn, ls)
1112 // skip the envelope line and blanks
1113 var isrow: i64 = 1
1114 if le <= ls { isrow = 0 }
1115 if isrow == 1 { if gout[ls] == (45 as u8) { isrow = 0 } }
1116 if isrow == 1 {
1117 // path = up to the first ':'
1118 var c1: i64 = ls
1119 var f1: i64 = 1
1120 while f1 == 1 { if c1 >= le { f1 = 0 } else { if gout[c1] == (58 as u8) { f1 = 0 } else { c1 = c1 + 1 } } }
1121 if c1 < le {
1122 let pl: i64 = c1 - ls
1123 if pl > 0 { if pl < IE_PATH_CAP {
1124 // line number
1125 var ln: i64 = 0
1126 var nd: i64 = 0
1127 var c2: i64 = c1 + 1
1128 var f2: i64 = 1
1129 while f2 == 1 { if c2 >= le { f2 = 0 } else { let ch: i64 = gout[c2] as i64; if ie_is_digit(ch) == 1 { ln = ln * 10 + (ch - 48); nd = nd + 1; c2 = c2 + 1 } else { f2 = 0 } } }
1130 if nd > 0 {
1131 hits = hits + 1
1132 // new file?
1133 var same: i64 = 1
1134 var k: i64 = 0
1135 while k < pl { if curpath[k] != gout[ls + k] { same = 0 } k = k + 1 }
1136 if curpath[pl] != (0 as u8) { same = 0 }
1137 if same == 0 {
1138 k = 0
1139 while k < pl { curpath[k] = gout[ls + k]; k = k + 1 }
1140 curpath[pl] = 0 as u8
1141 files_seen = files_seen + 1
1142 let rl: *i64 = sys_mmap(16) as *i64
1143 let rb: *u8 = sys_read_file(curpath, rl)
1144 if (rb as i64) == 0 { rawn = 0 - 1; files_unreadable = files_unreadable + 1 } else {
1145 raw = rb
1146 rawn = rl[0]
1147 code = sys_mmap(rawn + 1)
1148 jc_strip_noncode(raw, rawn, code)
1149 }
1150 }
1151 var cls: i64 = IE_NOT_A_SITE
1152 if rawn >= 0 { cls = ie_classify(code, raw, rawn, ln, val) }
1153 if cls == IE_NOT_A_SITE { not_site = not_site + 1 }
1154 if cls == IE_LITERAL_ANN { lit_ann = lit_ann + 1 }
1155 if cls == IE_NAMED_ANN { named_ann = named_ann + 1 }
1156 if cls == IE_DYNAMIC { dynamic = dynamic + 1 }
1157 if cls == IE_LOOPED { looped = looped + 1 }
1158 if cls == IE_LITERAL_SILENT { lit_silent = lit_silent + 1 }
1159 if cls == IE_NAMED_SILENT { named_silent = named_silent + 1 }
1160 var is_silent: i64 = 0
1161 if cls == IE_LITERAL_SILENT { is_silent = 1 }
1162 if cls == IE_NAMED_SILENT { is_silent = 1 }
1163 // DI15: a LOOPED read is judged by its loop's exit; CAPPED joins the worklist with its bound named
1164 var lx: i64 = 0
1165 var lann: i64 = 0
1166 if cls == IE_LOOPED {
1167 lx = ie_loop_exit(code, rawn, ln, bound)
1168 if lx == IE_LOOP_STREAMING { looped_streaming = looped_streaming + 1 }
1169 if lx == IE_LOOP_CAPPED {
1170 lann = ie_file_announces(raw, rawn)
1171 if lann == 1 { looped_capped_ann = looped_capped_ann + 1 } else { looped_capped = looped_capped + 1; is_silent = 1 }
1172 }
1173 if lx == IE_LOOP_UNJUDGED { looped_unjudged = looped_unjudged + 1 }
1174 }
1175 if is_silent == 1 {
1176 let tl: i64 = ie_key_text(code, rawn, ln, ktext)
1177 if so + pl + tl + 64 < IE_SILENT_CAP { let kk: i64 = ie_count_same(silent, so, curpath, ktext); so = ie_row_put(silent, so, curpath, kk, ktext, ln) } else { silent_overflow = silent_overflow + 1 }
1178 }
1179 if cls == IE_LITERAL_SILENT { ie_puts("SILENT " as *u8); ie_puts(curpath); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" len=literal value=" as *u8); ie_putn(val[0]); ie_puts("\n" as *u8) }
1180 if cls == IE_NAMED_SILENT { ie_puts("SILENT " as *u8); ie_puts(curpath); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" len=named value=" as *u8); ie_putn(val[0]); ie_puts("\n" as *u8) }
1181 if cls == IE_LOOPED {
1182 if lx == IE_LOOP_STREAMING { ie_puts("LOOPED-STREAMING " as *u8); ie_puts(curpath); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" (the loop ends when the read returns nothing -- acquitted)\n" as *u8) }
1183 if lx == IE_LOOP_CAPPED {
1184 if lann == 1 { ie_puts("LOOPED-CAPPED-ANNOUNCING " as *u8) } else { ie_puts("LOOPED-CAPPED " as *u8) }
1185 ie_puts(curpath); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" bound=" as *u8); ie_puts(bound)
1186 if lann == 1 { ie_puts(" (the loop stops at a count bound and the file announces its envelope -- not a worklist row)\n" as *u8) } else { ie_puts(" (the loop also stops at a count bound and nothing announces it -- a cap wearing a loop; joins the silent worklist for adjudication: a tail window is legitimate, a head prefix is not)\n" as *u8) }
1187 }
1188 if lx == IE_LOOP_UNJUDGED { ie_puts("LOOPED-UNJUDGED " as *u8); ie_puts(curpath); ie_puts(":" as *u8); ie_putn(ln); ie_puts(" (the loop's exit could not be read -- counted, never acquitted)\n" as *u8) }
1189 }
1190 }
1191 } }
1192 }
1193 }
1194 ls = le + 1
1195 }
1196 }
1197 // 4. partition (every hit lands in exactly one class)
1198 let summed: i64 = not_site + lit_ann + lit_silent + named_ann + named_silent + dynamic + looped
1199 let fixed: i64 = lit_ann + lit_silent + named_ann + named_silent
1200 let silent_n: i64 = lit_silent + named_silent + looped_capped
1201 let looped_summed: i64 = looped_streaming + looped_capped + looped_capped_ann + looped_unjudged
1202 ie_puts("hits=" as *u8); ie_putn(hits); ie_puts(" not_a_site=" as *u8); ie_putn(not_site)
1203 ie_puts(" dynamic=" as *u8); ie_putn(dynamic); ie_puts(" looped=" as *u8); ie_putn(looped)
1204 ie_puts(" looped_streaming=" as *u8); ie_putn(looped_streaming); ie_puts(" looped_capped=" as *u8); ie_putn(looped_capped); ie_puts(" looped_capped_announcing=" as *u8); ie_putn(looped_capped_ann); ie_puts(" looped_unjudged=" as *u8); ie_putn(looped_unjudged)
1205 if looped_summed == looped { ie_puts(" looped_partition=RECONCILES" as *u8) } else { ie_puts(" looped_partition=LEAK summed=" as *u8); ie_putn(looped_summed) }
1206 ie_puts(" fixed=" as *u8); ie_putn(fixed)
1207 ie_puts(" fixed_literal_announcing=" as *u8); ie_putn(lit_ann); ie_puts(" fixed_literal_silent=" as *u8); ie_putn(lit_silent)
1208 ie_puts(" fixed_named_announcing=" as *u8); ie_putn(named_ann); ie_puts(" fixed_named_silent=" as *u8); ie_putn(named_silent)
1209 ie_puts(" silent=" as *u8); ie_putn(silent_n); ie_puts(" silent_overflow=" as *u8); ie_putn(silent_overflow)
1210 ie_puts(" derived_sys_read_file_sites=" as *u8); ie_putn(derived); ie_puts(" (separate axis)" as *u8)
1211 ie_puts(" files_seen=" as *u8); ie_putn(files_seen); ie_puts(" files_unreadable=" as *u8); ie_putn(files_unreadable)
1212 if summed == hits { ie_puts(" partition=RECONCILES\n" as *u8) } else { ie_puts(" partition=LEAK summed=" as *u8); ie_putn(summed); ie_puts("\n" as *u8) }
1213 // 5. the named-set ratchet (DI12): rise named and refused, fall named, journaled and tightened
1214 let rout: *i64 = sys_mmap(32) as *i64
1215 var new_silent: i64 = 0
1216 // A PARTIAL WALK MUST NOT TOUCH THE BASELINE (2026-09-06): a scan whose walker failed sees zero sites, and
1217 // running the ratchet on that would CREATE an empty baseline on first sight (every real site then reads as a
1218 // rise) or TIGHTEN a real one to nothing on a fall (every real site then reads as a rise with its history
1219 // journaled as "left"). The ratchet only ever sees a complete walk; UNPROVEN leaves the fossil untouched.
1220 if walker_ok == 1 { new_silent = ie_ratchet(silent, so, IE_BASELINE, IE_LEDGER, accept, rout) }
1221 else { rout[0] = 0; rout[1] = 0; rout[2] = IE_BS_PRESENT; ie_puts("baseline=UNTOUCHED (walk UNPROVEN: the ratchet is not run on a partial population)\n" as *u8) }
1222 let rate: *i64 = sys_mmap(32) as *i64
1223 ie_rate(IE_LEDGER, rate)
1224 let adjudicated: i64 = rate[0] + rate[1]
1225 var fp_permil: i64 = IE_FP_UNMEASURED
1226 if adjudicated > 0 { fp_permil = rate[1] * 1000 / adjudicated }
1227 ie_puts("false_positive_permil=" as *u8)
1228 if fp_permil < 0 { ie_puts("UNMEASURED" as *u8) } else { ie_putn(fp_permil) }
1229 ie_puts(" adjudicated=" as *u8); ie_putn(adjudicated); ie_puts(" true_positive=" as *u8); ie_putn(rate[0]); ie_puts(" false_positive=" as *u8); ie_putn(rate[1])
1230 ie_puts(" silent_now=" as *u8); ie_putn(silent_n); ie_puts(" (from the ratchet ledger: a site that left as converted/announced/dynamic was real, one that left as looped was the old ruler's error, a hand kind=false row adjudicates a standing site; SILENT is a worklist = fixed silent + looped-capped, LOOPED-STREAMING is acquitted by its exit, DYNAMIC and LOOPED-UNJUDGED are not judged)\n" as *u8)
1231 // 6. verdict: UNPROVEN beats RED beats GREEN; the status file is truncate-written with the verdict LAST
1232 var verdict: *u8 = "GREEN" as *u8
1233 var rc: i64 = IE_EXIT_GREEN
1234 if new_silent > 0 { verdict = "RED" as *u8; rc = IE_EXIT_RED }
1235 if walker_ok == 0 { verdict = "UNPROVEN" as *u8; rc = IE_EXIT_UNPROVEN }
1236 let st: *u8 = sys_mmap(4096)
1237 var o: i64 = ie_cat(st, 0, "ts=" as *u8); o = ie_catn(st, o, sys_now_realtime_sec())
1238 o = ie_cat(st, o, " subject=io-envelope-census root=" as *u8); o = ie_cat(st, o, root)
1239 o = ie_cat(st, o, "\nhits=" as *u8); o = ie_catn(st, o, hits)
1240 o = ie_cat(st, o, " fixed=" as *u8); o = ie_catn(st, o, fixed)
1241 o = ie_cat(st, o, " silent=" as *u8); o = ie_catn(st, o, silent_n)
1242 o = ie_cat(st, o, " dynamic=" as *u8); o = ie_catn(st, o, dynamic)
1243 o = ie_cat(st, o, " looped=" as *u8); o = ie_catn(st, o, looped)
1244 o = ie_cat(st, o, " looped_streaming=" as *u8); o = ie_catn(st, o, looped_streaming)
1245 o = ie_cat(st, o, " looped_capped=" as *u8); o = ie_catn(st, o, looped_capped)
1246 o = ie_cat(st, o, " looped_capped_announcing=" as *u8); o = ie_catn(st, o, looped_capped_ann)
1247 o = ie_cat(st, o, " looped_unjudged=" as *u8); o = ie_catn(st, o, looped_unjudged)
1248 o = ie_cat(st, o, " derived=" as *u8); o = ie_catn(st, o, derived)
1249 o = ie_cat(st, o, " new_silent=" as *u8); o = ie_catn(st, o, new_silent)
1250 o = ie_cat(st, o, " left_silent=" as *u8); o = ie_catn(st, o, rout[1])
1251 o = ie_cat(st, o, " adjudicated=" as *u8); o = ie_catn(st, o, adjudicated)
1252 o = ie_cat(st, o, " false_positive_permil=" as *u8)
1253 if fp_permil < 0 { o = ie_cat(st, o, "UNMEASURED" as *u8) } else { o = ie_catn(st, o, fp_permil) }
1254 o = ie_cat(st, o, " coverage_complete=" as *u8); o = ie_catn(st, o, cov)
1255 o = ie_cat(st, o, "\nverdict=" as *u8); o = ie_cat(st, o, verdict); o = ie_cat(st, o, "\n" as *u8)
1256 let sfd: i64 = sys_openat_wr(IE_STATUS, IE_MODE_644)
1257 if sfd >= 0 { sys_write(sfd, st, o); sys_close(sfd) }
1258 ie_puts("NX-IO-ENVELOPE-CENSUS verdict=" as *u8); ie_puts(verdict); ie_puts("\n" as *u8)
1259 return rc
1260}
1261
1262// ---- selftest: the classifier on planted sources, on the base class -------------------------------------------
1263func ie_selftest() -> i64 {
1264 let ctr: *i64 = gv_ctr()
1265 gv_head("nx_io_envelope_census selftest -- the classifier on planted readers" as *u8)
1266 let v: *i64 = sys_mmap(16) as *i64
1267 let f1: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, 4096)\n return n\n}\n" as *u8
1268 gv_check_eq("T1 a literal length in a file with no envelope is FIXED-LITERAL-SILENT" as *u8, ie_classify_src(f1, ie_slen(f1), 2, v), IE_LITERAL_SILENT, ctr)
1269 gv_check_eq("T1b the literal is read back exactly" as *u8, v[0], 4096, ctr)
1270 let f2: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, 4096)\n if n == 4096 { ie_puts(\"[NX-TRUNCATED OUTPUT-IS-PARTIAL capture_cap=4096]\") }\n return n\n}\n" as *u8
1271 gv_check_eq("T2 the same literal in a file that prints an envelope token is FIXED-LITERAL-ANNOUNCING" as *u8, ie_classify_src(f2, ie_slen(f2), 2, v), IE_LITERAL_ANN, ctr)
1272 let f3: *u8 = "const XX_CAP: i64 = 65536\nfunc r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, XX_CAP)\n return n\n}\n" as *u8
1273 gv_check_eq("T3 an ALL-CAPS name declared as a numeric const is FIXED-NAMED-SILENT" as *u8, ie_classify_src(f3, ie_slen(f3), 3, v), IE_NAMED_SILENT, ctr)
1274 gv_check_eq("T3b the named constant resolves to its declared value" as *u8, v[0], 65536, ctr)
1275 let f4: *u8 = "func r(fd: i64, b: *u8, cap: i64, total: i64) -> i64 {\n let n: i64 = sys_read(fd, ((b as i64) + total) as *u8, cap - total)\n return n\n}\n" as *u8
1276 gv_check_eq("T4 a computed remainder is DYNAMIC (not judged, not acquitted)" as *u8, ie_classify_src(f4, ie_slen(f4), 2, v), IE_DYNAMIC, ctr)
1277 let f5: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n // the old reader was sys_read(fd, b, 4096) and it truncated\n return 0\n}\n" as *u8
1278 gv_check_eq("neg-control-T5 a sys_read( inside a COMMENT is not a site" as *u8, ie_classify_src(f5, ie_slen(f5), 2, v), IE_NOT_A_SITE, ctr)
1279 let f6: *u8 = "func r() -> *u8 {\n let s: *u8 = \"sys_read(fd, b, 4096)\" as *u8\n return s\n}\n" as *u8
1280 gv_check_eq("neg-control-T6 a sys_read( inside a STRING is not a site" as *u8, ie_classify_src(f6, ie_slen(f6), 2, v), IE_NOT_A_SITE, ctr)
1281 let f7: *u8 = "func r(p: *u8) -> *u8 {\n let ln: *i64 = sys_mmap(16) as *i64\n let b: *u8 = sys_read_file(p, ln)\n return b\n}\n" as *u8
1282 gv_check_eq("neg-control-T7 a sys_read_file( caller is never a sys_read( site (the remedy is not flagged)" as *u8, ie_classify_src(f7, ie_slen(f7), 3, v), IE_NOT_A_SITE, ctr)
1283 let f8: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, NX_UNKNOWN_CAP)\n return n\n}\n" as *u8
1284 gv_check_eq("T8 an ALL-CAPS name with NO numeric const in the file is DYNAMIC (unresolved is not judged)" as *u8, ie_classify_src(f8, ie_slen(f8), 2, v), IE_DYNAMIC, ctr)
1285 let f9: *u8 = "const XX_CAP: i64 = 65536\nconst XX_CAPX: i64 = 7\nfunc r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, XX_CAP)\n return n\n}\n" as *u8
1286 ie_classify_src(f9, ie_slen(f9), 4, v)
1287 gv_check_eq("T9 a name that is a PREFIX of another const resolves to its own declaration, not the longer one" as *u8, v[0], 65536, ctr)
1288 let f10: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n let n: i64 = sys_read(fd, b, ie_len(b, 8192))\n return n\n}\n" as *u8
1289 gv_check_eq("T10 a call expression as the length is DYNAMIC (its inner literal is not the read length)" as *u8, ie_classify_src(f10, ie_slen(f10), 2, v), IE_DYNAMIC, ctr)
1290 // DI12: the streaming idiom is LOOPED, not SILENT
1291 let f11: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n var r: i64 = sys_read(fd, b, 65536)\n while r > 0 {\n r = sys_read(fd, b, 65536)\n }\n return 0\n}\n" as *u8
1292 gv_check_eq("T11 a read INSIDE a while block is LOOPED (the streaming idiom, not judged)" as *u8, ie_classify_src(f11, ie_slen(f11), 4, v), IE_LOOPED, ctr)
1293 gv_check_eq("T11b the PRIME read whose result drives `while r > 0` is LOOPED too (nx_sizeguard's shape)" as *u8, ie_classify_src(f11, ie_slen(f11), 2, v), IE_LOOPED, ctr)
1294 let f12: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n var go: i64 = 1\n while go == 1 {\n let r: i64 = sys_read(fd, b, 65536)\n if r <= 0 { go = 0 }\n }\n return 0\n}\n" as *u8
1295 gv_check_eq("T12 the flag-driven loop (`while go == 1` around a `let r = sys_read`) is LOOPED (nx_restage's shape)" as *u8, ie_classify_src(f12, ie_slen(f12), 4, v), IE_LOOPED, ctr)
1296 let f13: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n if fd > 0 {\n let n: i64 = sys_read(fd, b, 4096)\n return n\n }\n return 0\n}\n" as *u8
1297 gv_check_eq("neg-control-T13 a read inside an if block (no loop) stays FIXED-LITERAL-SILENT" as *u8, ie_classify_src(f13, ie_slen(f13), 3, v), IE_LITERAL_SILENT, ctr)
1298 // DI12: the ratchet on /tmp fixtures (created at setup; a crashed run leaves nothing a later run misreads)
1299 sys_mkdir(IE_FIX_DIR, IE_MODE_755)
1300 let fb: *u8 = "/tmp/nx_io_envelope_census/baseline" as *u8
1301 let fl: *u8 = "/tmp/nx_io_envelope_census/ledger.jrnl" as *u8
1302 sys_unlinkat(fl)
1303 let rowA: *u8 = sys_mmap(IE_ROW_CAP)
1304 let rowB: *u8 = sys_mmap(IE_ROW_CAP)
1305 let rowC: *u8 = sys_mmap(IE_ROW_CAP)
1306 ie_row_put(rowA, 0, "/tmp/nx_io_envelope_census/a.nx" as *u8, 0, "let n: i64 = sys_read(fd, b, 4096)" as *u8, 2)
1307 ie_row_put(rowB, 0, "/tmp/nx_io_envelope_census/b.nx" as *u8, 0, "let n: i64 = sys_read(fd, b, 8192)" as *u8, 2)
1308 ie_row_put(rowC, 0, "/tmp/nx_io_envelope_census/c.nx" as *u8, 0, "let n: i64 = sys_read(fd, b, 1024)" as *u8, 2)
1309 let cur: *u8 = sys_mmap(IE_ROW_CAP * 4)
1310 let out: *i64 = sys_mmap(32) as *i64
1311 // T14 RISE: baseline {A,B}, current {A,B,C} -> one new, named; the baseline is NOT rewritten
1312 var o: i64 = ie_cat(cur, 0, rowA)
1313 o = ie_cat(cur, o, rowB)
1314 ie_write_file(fb, cur, o)
1315 let base_before: i64 = o
1316 o = ie_cat(cur, 0, rowA)
1317 o = ie_cat(cur, o, rowB)
1318 o = ie_cat(cur, o, rowC)
1319 let rise: i64 = ie_ratchet(cur, o, fb, fl, 0, out)
1320 gv_check_eq("T14 a silent site not in the baseline is a RISE of exactly one" as *u8, rise, 1, ctr)
1321 gv_check_eq("T14b a rise leaves the baseline UNCHANGED (a ratchet that rewrites on a rise launders itself)" as *u8, ie_file_len(fb), base_before, ctr)
1322 gv_check_eq("T14c the baseline state on a rise is PRESENT" as *u8, out[2], IE_BS_PRESENT, ctr)
1323 // T15 FALL: baseline {A,B,C}, current {A,B} -> one left, journaled as converted, baseline TIGHTENED to {A,B}
1324 ie_write_file(fb, cur, o)
1325 o = ie_cat(cur, 0, rowA)
1326 o = ie_cat(cur, o, rowB)
1327 let fall: i64 = ie_ratchet(cur, o, fb, fl, 0, out)
1328 gv_check_eq("T15 a baseline site no longer silent is a FALL of exactly one and no rise" as *u8, fall * 100 + out[1], 1, ctr)
1329 gv_check_eq("T15b the baseline is TIGHTENED to the rows that remain (byte-exact)" as *u8, ie_file_eq(fb, cur, o), 1, ctr)
1330 let led: *u8 = sys_read_file(fl, v)
1331 var led_ok: i64 = 0
1332 if (led as i64) != 0 { if ie_find(led, v[0], "kind=left site=/tmp/nx_io_envelope_census/c.nx:2 now=converted" as *u8) == 1 { led_ok = 1 } }
1333 gv_check("T15c the fall is journaled with what the site became (an absent file reads as converted)" as *u8, led_ok, ctr)
1334 // T16 --accept: baseline {A}, current {A,B} -> rise named AND the baseline rewritten to the current set
1335 let oa: i64 = ie_cat(rowA, ie_slen(rowA), "" as *u8)
1336 ie_write_file(fb, rowA, oa)
1337 let acc: i64 = ie_ratchet(cur, o, fb, fl, 1, out)
1338 gv_check_eq("T16 --accept still NAMES the rise" as *u8, acc, 1, ctr)
1339 gv_check_eq("T16b --accept rewrites the baseline to the current set" as *u8, ie_file_eq(fb, cur, o), 1, ctr)
1340 // neg-control: identical sets change nothing
1341 let ln_before: i64 = ie_file_len(fl)
1342 let same: i64 = ie_ratchet(cur, o, fb, fl, 0, out)
1343 gv_check_eq("neg-control-T17 identical sets: no rise, no fall, baseline PRESENT, ledger not appended" as *u8, same * 1000 + out[1] * 100 + out[2] * 10 + (ie_file_len(fl) != ln_before) as i64, IE_BS_PRESENT * 10, ctr)
1344 // T18 legacy migration: a path:line baseline is matched by line and re-keyed
1345 let legacy: *u8 = "/tmp/nx_io_envelope_census/a.nx:2\n/tmp/nx_io_envelope_census/b.nx:2\n" as *u8
1346 ie_write_file(fb, legacy, ie_slen(legacy))
1347 let mig: i64 = ie_ratchet(cur, o, fb, fl, 0, out)
1348 gv_check_eq("T18 a legacy path:line baseline matches the same sites by line: no rise" as *u8, mig, 0, ctr)
1349 gv_check_eq("T18b and is MIGRATED to the keyed form (byte-exact current rows)" as *u8, out[2] * 10 + ie_file_eq(fb, cur, o), IE_BS_MIGRATED * 10 + 1, ctr)
1350 // T19 the rate is arithmetic over the ledger
1351 let lfix: *u8 = "ts=1 kind=left site=x:1 now=converted key=x|0|t\nts=1 kind=left site=y:1 now=looped key=y|0|t\nts=1 kind=left site=z:1 now=announced key=z|0|t\nts=1 kind=false site=w:1 reason=bounded-by-construction\nts=1 kind=new site=q:1\n" as *u8
1352 ie_write_file(fl, lfix, ie_slen(lfix))
1353 let rate: *i64 = sys_mmap(32) as *i64
1354 ie_rate(fl, rate)
1355 gv_check_eq("T19 true positives = left rows that became converted/announced/dynamic" as *u8, rate[0], 2, ctr)
1356 gv_check_eq("T19b false positives = left-as-looped rows plus hand kind=false rows; kind=new rows are not adjudications" as *u8, rate[1], 2, ctr)
1357 // DI15: a LOOPED read is judged by its loop's EXIT -- streaming acquitted, a count bound named, an unreadable exit counted
1358 let bnd: *u8 = sys_mmap(IE_BOUND_CAP)
1359 gv_check_eq("T20 nx_sizeguard's shape (prime read, `while r > 0`, re-read in the body) is LOOPED-STREAMING" as *u8, ie_loop_exit_src(f11, ie_slen(f11), 4, bnd), IE_LOOP_STREAMING, ctr)
1360 gv_check_eq("T20b the PRIME read of that loop is judged by the loop its result drives: STREAMING" as *u8, ie_loop_exit_src(f11, ie_slen(f11), 2, bnd), IE_LOOP_STREAMING, ctr)
1361 gv_check_eq("T21 nx_restage's shape (`while go == 1`, `if r <= 0 { go = 0 }`) is LOOPED-STREAMING" as *u8, ie_loop_exit_src(f12, ie_slen(f12), 4, bnd), IE_LOOP_STREAMING, ctr)
1362 let f22: *u8 = "const RX_LIMIT: i64 = 262144\nfunc r(fd: i64, b: *u8) -> i64 {\n var total: i64 = 0\n var r: i64 = sys_read(fd, b, 65536)\n while r > 0 {\n total = total + r\n if total >= RX_LIMIT { r = 0 } else { r = sys_read(fd, b, 65536) }\n }\n return total\n}\n" as *u8
1363 gv_check_eq("T22 a streaming loop that ALSO stops at `total >= RX_LIMIT` is LOOPED-CAPPED (a cap wearing a loop)" as *u8, ie_loop_exit_src(f22, ie_slen(f22), 7, bnd), IE_LOOP_CAPPED, ctr)
1364 gv_check_eq("T22b the bound is NAMED from the code: RX_LIMIT" as *u8, jc_find_sub(bnd, ie_slen(bnd), "RX_LIMIT" as *u8, 8), 1, ctr)
1365 let f23: *u8 = "func r(fd: i64, b: *u8, cap: i64) -> i64 {\n var got: i64 = 0\n var n: i64 = sys_read(fd, b, cap)\n while n > 0 {\n got = got + n\n if got >= cap { n = 0 } else { n = sys_read(fd, b, cap - got) }\n }\n return got\n}\n" as *u8
1366 gv_check_eq("T23 a lowercase cap-named bound (`got >= cap`) is LOOPED-CAPPED too" as *u8, ie_loop_exit_src(f23, ie_slen(f23), 6, bnd), IE_LOOP_CAPPED, ctr)
1367 // DI16 precision (2026-09-06): a while written on ONE line puts its brace BEFORE the read on the read's own
1368 // line, and the idiom is often spelled without spaces -- both were invisible to the judge
1369 let f31: *u8 = "const K_MAGIC_65536: i64 = 65536\nfunc c(rfd: i64, buf: *u8) -> i64 {\n var go: i64 = 1\n while go==1 { let k: i64=sys_read(rfd, buf, K_MAGIC_65536); if k<=0 { go=0 } }\n return 0\n}\n" as *u8
1370 gv_check_eq("T31 a one-line streaming loop (`while go==1 { k=sys_read(..); if k<=0 { go=0 } }`) is LOOPED" as *u8, ie_is_looped_src(f31, ie_slen(f31), 4), 1, ctr)
1371 gv_check_eq("T31b and its exit is STREAMING: the read's result is tested against zero inside the body, spaces or not" as *u8, ie_loop_exit_src(f31, ie_slen(f31), 4, bnd), IE_LOOP_STREAMING, ctr)
1372 let f31c: *u8 = "func e(fd: i64, buf: *u8, cap: i64) -> i64 {\n var t: i64 = 0\n var r: i64 = 1\n while r > 0 { r = sys_read(fd, (buf as i64 + t) as *u8, cap - 1 - t); if r > 0 { t = t + r } if t >= cap - 1 { r = 0 } }\n return t\n}\n" as *u8
1373 gv_check_eq("T31c a one-line loop that stops at `t >= cap - 1` is LOOPED-CAPPED with the bound named" as *u8, ie_loop_exit_src(f31c, ie_slen(f31c), 4, bnd) * 10 + jc_find_sub(bnd, ie_slen(bnd), "cap" as *u8, 3), IE_LOOP_CAPPED * 10 + 1, ctr)
1374 let f31d: *u8 = "func d(fd: i64, buf: *u8) -> i64 {\n var n: i64 = 0\n if fd >= 0 { n = sys_read(fd, buf, 1023); sys_close(fd) }\n return n\n}\n" as *u8
1375 gv_check_eq("neg-control-T31d a one-line `if fd >= 0 { n = sys_read(..) }` is NOT looped: a brace on the read's line is not a loop" as *u8, ie_is_looped_src(f31d, ie_slen(f31d), 3), 0, ctr)
1376 let f31e: *u8 = "func s(fd: i64, b: *u8) -> i64 {\n var n: i64 = sys_read(fd, b, 8)\n let v: i64 = ((b[0] as i64)<<24)|((b[1] as i64)<<16)\n return v\n}\n" as *u8
1377 gv_check_eq("neg-control-T31e a shift amount (`<<24`) is not a comparison bound: the one-shot read stays a one-shot, not a capped loop" as *u8, ie_is_looped_src(f31e, ie_slen(f31e), 2), 0, ctr)
1378 let f24: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n while more(fd) == 1 {\n let r: i64 = sys_read(fd, b, 65536)\n use(b, r)\n }\n return 0\n}\n" as *u8
1379 gv_check_eq("neg-control-T24 a loop whose exit is a foreign call (`while more(fd) == 1`, result never tested) is LOOPED-UNJUDGED, never acquitted" as *u8, ie_loop_exit_src(f24, ie_slen(f24), 3, bnd), IE_LOOP_UNJUDGED, ctr)
1380 let f25: *u8 = "func r(fd: i64, b: *u8) -> i64 {\n var i: i64 = 0\n var r: i64 = 1\n while r > 0 {\n r = sys_read(fd, b, 65536)\n i = i + 1\n }\n return i\n}\n" as *u8
1381 gv_check_eq("neg-control-T25 a counter that is only incremented (never compared to a bound) does not make a streaming loop CAPPED" as *u8, ie_loop_exit_src(f25, ie_slen(f25), 5, bnd), IE_LOOP_STREAMING, ctr)
1382 // a read that merely SITS in a loop does not drive it: one request read per accepted connection is a FIXED site
1383 let f26: *u8 = "const TG_SHSZ: i64 = 4096\nfunc serve(lfd: i64, req: *u8) -> i64 {\n var run: i64 = 1\n while run == 1 {\n let afd: i64 = sys_accept(lfd)\n sys_read(afd, req, 2047)\n var got: i64 = 0\n if got >= TG_SHSZ { run = 0 }\n sys_close(afd)\n }\n return 0\n}\n" as *u8
1384 gv_check_eq("neg-control-T26 a per-connection read inside an accept loop (result never tested, no running fill) is a FIXED literal site, not LOOPED -- the loop's unrelated bound must not make it CAPPED" as *u8, ie_classify_src(f26, ie_slen(f26), 6, v), IE_LITERAL_SILENT, ctr)
1385 let f27: *u8 = "func fill(fd: i64, buf: *u8, cap: i64) -> i64 {\n var got: i64 = 0\n while got < cap {\n got = got + sys_read(fd, ((buf as i64) + got) as *u8, cap - got)\n }\n return got\n}\n" as *u8
1386 gv_check_eq("T27 a fill loop with a running remainder length and no result variable is LOOPED (its arguments drive it)" as *u8, ie_classify_src(f27, ie_slen(f27), 4, v), IE_LOOPED, ctr)
1387 gv_check_eq("T27b and it is CAPPED by the loop's own bound `cap`" as *u8, ie_loop_exit_src(f27, ie_slen(f27), 4, bnd), IE_LOOP_CAPPED, ctr)
1388 gv_check_eq("T28 a CAPPED loop in a file that never announces its envelope is a worklist row" as *u8, ie_loop_worklist_src(f22, ie_slen(f22), 7, bnd), 1, ctr)
1389 let f28: *u8 = "const RX_LIMIT: i64 = 262144\nfunc r(fd: i64, b: *u8) -> i64 {\n var total: i64 = 0\n var r: i64 = sys_read(fd, b, 65536)\n while r > 0 {\n total = total + r\n if total >= RX_LIMIT { r = 0 } else { r = sys_read(fd, b, 65536) }\n }\n if total >= RX_LIMIT { ie_puts(\x22coverage_complete=0\x22 as *u8) }\n return total\n}\n" as *u8
1390 gv_check_eq("neg-control-T28b the same CAPPED loop in a file that announces (coverage_complete) is NOT a worklist row" as *u8, ie_loop_worklist_src(f28, ie_slen(f28), 7, bnd), 0, ctr)
1391 // the walker's envelope is parsed from its LAST `-- matches=` line only; a grep hit that spells the field is data
1392 let env: *i64 = sys_mmap(32) as *i64
1393 let wcap: *u8 = "x.nx:3: puts(\x22coverage_complete=0 corpus_complete=0\x22)\nx.nx:9: -- matches=7 coverage_complete=0\n-- matches=2 bytes=10 files=1 coverage_complete=1 corpus_complete=1\n" as *u8
1394 ie_walker_env(wcap, ie_slen(wcap), env)
1395 gv_check_eq("T29 walker matches come from the envelope line, not from a hit that spells `-- matches=` mid-line" as *u8, env[0], 2, ctr)
1396 gv_check_eq("neg-control-T29b coverage_complete comes from the envelope line (1), never from the data line that spells it (0)" as *u8, env[1], 1, ctr)
1397 gv_check_eq("T29c corpus_complete likewise" as *u8, env[2], 1, ctr)
1398 let wcap2: *u8 = "x.nx:3: sys_read(fd, b, 10)\n" as *u8
1399 ie_walker_env(wcap2, ie_slen(wcap2), env)
1400 gv_check_eq("neg-control-T29d a capture with NO envelope line reports matches=-1 (UNPROVEN), never a count taken from the data" as *u8, env[0], 0 - 1, ctr)
1401 // DI16: the worklist groups the baseline by file and orders the biggest file first
1402 let wl: *u8 = sys_mmap(1024)
1403 var wo: i64 = ie_row_put(wl, 0, "a.nx" as *u8, 0, "sys_read(fd, b, 4096)" as *u8, 3)
1404 wo = ie_row_put(wl, wo, "b.nx" as *u8, 0, "sys_read(fd, b, 8192)" as *u8, 5)
1405 wo = ie_row_put(wl, wo, "a.nx" as *u8, 1, "sys_read(fd, b, 4096)" as *u8, 9)
1406 wo = ie_row_put(wl, wo, "b.nx" as *u8, 1, "sys_read(fd, b, 16)" as *u8, 12)
1407 wo = ie_row_put(wl, wo, "b.nx" as *u8, 2, "sys_read(fd, b, 32)" as *u8, 20)
1408 let wf: *i64 = sys_mmap(64) as *i64
1409 let wc: *i64 = sys_mmap(64) as *i64
1410 let nf: i64 = ie_worklist_scan(wl, wo, wf, wc)
1411 gv_check_eq("T30 the worklist scan finds every distinct file in the baseline (two files across five rows)" as *u8, nf, 2, ctr)
1412 gv_check_eq("T30b per-file counts partition the rows (2 + 3 = 5)" as *u8, wc[0] + wc[1], 5, ctr)
1413 gv_check_eq("T30c the second file carries three rows (grouping by path, not by adjacency)" as *u8, wc[1], 3, ctr)
1414 let ns: i64 = ie_worklist_group(wl, wo, 2, wf, wc)
1415 gv_check_eq("T30d SHAPES group by reader TEXT across files: four distinct texts in five rows (one shape shared by two files)" as *u8, ns, 4, ctr)
1416 gv_check_eq("T30e the shared shape counts both of its rows (a cause is sized by its copies)" as *u8, wc[0], 2, ctr)
1417 return gv_verdict("nx_io_envelope_census" as *u8, ctr, "the classifier separates literal, named-constant, dynamic and looped lengths, reads the envelope from the file itself, never counts prose, strings or the derived primitive as a site, and the ratchet refuses a rise by name, tightens on a fall, journals what each site became and derives its false-positive rate from that ledger" as *u8)
1418}
1419
1420// ---- DI16: the worklist, grouped by file, biggest file first -------------------------------------------------------
1421// The baseline IS the worklist (path TAB k TAB text TAB line per row). files[] receives the row-start offset of one
1422// representative row per distinct path, counts[] the rows sharing it; returns the number of distinct paths. Both arrays
1423// are sized by the caller from the row count (a distinct-path table can never exceed the rows it is drawn from).
1424func ie_worklist_scan(bb: *u8, n: i64, files: *i64, counts: *i64) -> i64 { return ie_worklist_group(bb, n, 0, files, counts) }
1425// the same grouping over any TAB field: 0 = path (files), 2 = text (SHAPES -- the same reader copied across files is
1426// ONE cause, and a campaign is sized by causes, not by sites)
1427func ie_worklist_group(bb: *u8, n: i64, field: i64, files: *i64, counts: *i64) -> i64 {
1428 var nf: i64 = 0
1429 var rs: i64 = 0
1430 let sp: *i64 = sys_mmap(32) as *i64
1431 let sq: *i64 = sys_mmap(32) as *i64
1432 while rs < n {
1433 let re: i64 = ie_line_end(bb, n, rs)
1434 if re > rs { if ie_field(bb, rs, re, field, sp) == 1 {
1435 var f: i64 = 0
1436 var found: i64 = 0 - 1
1437 while f < nf {
1438 let fs: i64 = files[f]
1439 let fe: i64 = ie_line_end(bb, n, fs)
1440 if ie_field(bb, fs, fe, field, sq) == 1 {
1441 if sq[1] - sq[0] == sp[1] - sp[0] {
1442 var k: i64 = 0
1443 var same: i64 = 1
1444 while k < sp[1] - sp[0] { if bb[sp[0] + k] != bb[sq[0] + k] { same = 0; k = sp[1] - sp[0] } else { k = k + 1 } }
1445 if same == 1 { found = f; f = nf }
1446 }
1447 }
1448 f = f + 1
1449 }
1450 if found >= 0 { counts[found] = counts[found] + 1 } else { files[nf] = rs; counts[nf] = 1; nf = nf + 1 }
1451 } }
1452 rs = re + 1
1453 }
1454 return nf
1455}
1456func ie_worklist(baseline: *u8) -> i64 {
1457 let bl: *i64 = sys_mmap(16) as *i64
1458 let bb: *u8 = sys_read_file(baseline, bl)
1459 if (bb as i64) == 0 { ie_puts("NX-IO-ENVELOPE-WORKLIST baseline=ABSENT rows=0 files=0 (run a scan first)\n" as *u8); return IE_EXIT_UNPROVEN }
1460 let n: i64 = bl[0]
1461 var rows: i64 = 0
1462 var p: i64 = 0
1463 while p < n { let e: i64 = ie_line_end(bb, n, p); if e > p { rows = rows + 1 } p = e + 1 }
1464 let files: *i64 = sys_mmap((rows + 1) * 8) as *i64
1465 let counts: *i64 = sys_mmap((rows + 1) * 8) as *i64
1466 let nf: i64 = ie_worklist_scan(bb, n, files, counts)
1467 // order: biggest file first (selection sort over the distinct-path table; the table is small by construction)
1468 var a: i64 = 0
1469 while a < nf {
1470 var best: i64 = a
1471 var b: i64 = a + 1
1472 while b < nf { if counts[b] > counts[best] { best = b } b = b + 1 }
1473 if best != a { let tf: i64 = files[a]; files[a] = files[best]; files[best] = tf; let tc: i64 = counts[a]; counts[a] = counts[best]; counts[best] = tc }
1474 a = a + 1
1475 }
1476 let sp: *i64 = sys_mmap(32) as *i64
1477 let sq: *i64 = sys_mmap(32) as *i64
1478 ie_puts("=== nx_io_envelope_census worklist -- every remaining SILENT site, grouped by file, biggest file first (take a file at a time) ===\n" as *u8)
1479 var f: i64 = 0
1480 var summed: i64 = 0
1481 while f < nf {
1482 let fs: i64 = files[f]
1483 let fe: i64 = ie_line_end(bb, n, fs)
1484 ie_field(bb, fs, fe, 0, sp)
1485 ie_puts("FILE " as *u8); sys_write(1, ((bb as i64) + sp[0]) as *u8, sp[1] - sp[0]); ie_puts(" rows=" as *u8); ie_putn(counts[f]); ie_puts("\n" as *u8)
1486 // every row of this path, in baseline order
1487 var rs: i64 = 0
1488 while rs < n {
1489 let re: i64 = ie_line_end(bb, n, rs)
1490 if re > rs { if ie_field(bb, rs, re, 0, sq) == 1 {
1491 if sq[1] - sq[0] == sp[1] - sp[0] {
1492 var k: i64 = 0
1493 var same: i64 = 1
1494 while k < sp[1] - sp[0] { if bb[sp[0] + k] != bb[sq[0] + k] { same = 0; k = sp[1] - sp[0] } else { k = k + 1 } }
1495 if same == 1 {
1496 let lp: *i64 = sys_mmap(32) as *i64
1497 let tp: *i64 = sys_mmap(32) as *i64
1498 ie_puts(" :" as *u8)
1499 if ie_field(bb, rs, re, 3, lp) == 1 { sys_write(1, ((bb as i64) + lp[0]) as *u8, lp[1] - lp[0]) }
1500 ie_puts(" " as *u8)
1501 if ie_field(bb, rs, re, 2, tp) == 1 { sys_write(1, ((bb as i64) + tp[0]) as *u8, tp[1] - tp[0]) }
1502 ie_puts("\n" as *u8)
1503 summed = summed + 1
1504 }
1505 }
1506 } }
1507 rs = re + 1
1508 }
1509 f = f + 1
1510 }
1511 // SHAPES: the distinct reader texts, biggest first -- one shape copied across N files is ONE cause (extract it
1512 // to a shared lib and convert once), so the campaign is sized here, never by the row count above
1513 let shf: *i64 = sys_mmap((rows + 1) * 8) as *i64
1514 let shc: *i64 = sys_mmap((rows + 1) * 8) as *i64
1515 let ns: i64 = ie_worklist_group(bb, n, 2, shf, shc)
1516 a = 0
1517 while a < ns {
1518 var best: i64 = a
1519 var b: i64 = a + 1
1520 while b < ns { if shc[b] > shc[best] { best = b } b = b + 1 }
1521 if best != a { let tf: i64 = shf[a]; shf[a] = shf[best]; shf[best] = tf; let tc: i64 = shc[a]; shc[a] = shc[best]; shc[best] = tc }
1522 a = a + 1
1523 }
1524 ie_puts("=== SHAPES -- distinct reader texts, biggest first (a shape shared by N files is one cause) ===\n" as *u8)
1525 var s: i64 = 0
1526 var multi: i64 = 0
1527 var multi_rows: i64 = 0
1528 while s < ns {
1529 if shc[s] > 1 {
1530 multi = multi + 1
1531 multi_rows = multi_rows + shc[s]
1532 let ss: i64 = shf[s]
1533 let se: i64 = ie_line_end(bb, n, ss)
1534 ie_puts("SHAPE x" as *u8); ie_putn(shc[s]); ie_puts(" " as *u8)
1535 if ie_field(bb, ss, se, 2, sp) == 1 { sys_write(1, ((bb as i64) + sp[0]) as *u8, sp[1] - sp[0]) }
1536 ie_puts("\n" as *u8)
1537 }
1538 s = s + 1
1539 }
1540 ie_puts("NX-IO-ENVELOPE-WORKLIST rows=" as *u8); ie_putn(rows); ie_puts(" files=" as *u8); ie_putn(nf); ie_puts(" listed=" as *u8); ie_putn(summed)
1541 ie_puts(" shapes=" as *u8); ie_putn(ns); ie_puts(" shared_shapes=" as *u8); ie_putn(multi); ie_puts(" rows_in_shared_shapes=" as *u8); ie_putn(multi_rows); ie_puts(" singleton_rows=" as *u8); ie_putn(rows - multi_rows)
1542 if summed == rows { ie_puts(" partition=RECONCILES\n" as *u8) } else { ie_puts(" partition=LEAK\n" as *u8) }
1543 if summed == rows { return IE_EXIT_GREEN }
1544 return IE_EXIT_RED
1545}
1546
1547func main(argc: i64, argv: *i64) -> i64 {
1548 var root: *u8 = IE_DEFAULT_ROOT
1549 var accept: i64 = 0
1550 var i: i64 = 1
1551 while i < argc {
1552 let a: *u8 = argv[i] as *u8
1553 if ie_streq(a, "selftest" as *u8) == 1 { let rc: i64 = ie_selftest(); sys_exit(rc); return rc }
1554 if ie_streq(a, "worklist" as *u8) == 1 { let rc: i64 = ie_worklist(IE_BASELINE); sys_exit(rc); return rc }
1555 if ie_streq(a, "--accept" as *u8) == 1 { accept = 1 } else {
1556 if a[0] == (45 as u8) { ie_puts("usage: nx_io_envelope_census [scan_root] [--accept] | worklist | selftest\n" as *u8); sys_exit(IE_EXIT_USAGE); return IE_EXIT_USAGE }
1557 root = a
1558 }
1559 i = i + 1
1560 }
1561 let rc: i64 = ie_scan(root, accept)
1562 sys_exit(rc)
1563 return rc
1564}