nx_compare_regen_scoped.nx source
↩ module page · 1179 lines · 75245 B
1// nx_compare_regen.nx -- ON-NAS regeneration of the whole Nishi Compare publish surface (operator 2026-07-09:
2// "no frozen snapshots"). Re-MEASURES every matrix domain against the synced source tree (buildroot/runtime),
3// GATE-CHECKED: for each artifact the generator's liar-kill gate must exit 0 FIRST -- a RED measurement is NEVER
4// published (fail-closed). Then emits json+html per domain + the hub index json/html + the in-code OpenAPI, and
5// hot-swaps each into the LIVE docroot via write-.new + sys_renameat (atomic; a concurrent reader never tears).
6// Because nx_compare_serve reads the SAME docroot files, MCP tools/call and public REST update together, single
7// source. argv: [1]=buildroot dir (default "buildroot"), [2]=docroot prefix (default "../sites/nishifamily/compare/",
8// i.e. the live docroot relative to buildroot). The MCP allowlist row PINS these args (caller argv ignored) so a
9// tools/call caller cannot redirect writes. Targets must already exist (this regenerates, never scaffolds).
10// license_tier: ORIGINAL expect_exit: 0
11import "nx_tool_run.nx"
12import "nx_compare_receipt_lib.nx"
13import "nx_compare_scope_lib.nx"
14import "nx_symdecl_lib.nx"
15import "nx_estate_path.nx"
16import "nx_forkfresh_lib.nx"
17// EC17 (2026-09-01): the engine grades ITSELF with the same ladder it applies to the estate. The lib's
18// paths are estate-relative by construction, so this works from the regen's buildroot CWD unchanged.
19import "nx_catalog_lib.nx"
20import "nx_ioadmit_lib.nx" // THE I/O-storm ruler the build gate and the clock already share (2026-09-02): a seat-launched regen must consult it too
21import "nx_heavyio_lib.nx" // THE CLASS BOUND (hub-spoke, 2026-09-03): heavy producers RUNNING across every launch surface vs the derived width
22// PURPOSE-NAMED BOUNDS (2026-08-14). These were five consts literally named K_MAGIC_<value> -- a name
23// that RESTATES the number and is LESS greppable than the bare literal, which is the estate's own banked
24// anti-pattern: in every recorded case the VALUE was right and the NAME was the defect. Worse, they were
25// SHARED ACROSS UNRELATED PURPOSES -- K_MAGIC_65536 was at once the matrix file-read cap, three roster
26// file-read caps, the debtstate OUTPUT accumulator and the getdents64 transfer buffer, and K_MAGIC_262144
27// was both the store-put capture and the debt-tool capture.
28// -- ONE CONSTANT SERVING TWO UNRELATED PURPOSES CAN NEVER BE TUNED FOR EITHER.
29// Every FILE read in this organ now composes sys_read_file (buffer sized from the file by lseek END,
30// cannot short-read), so those caps are DELETED rather than renamed: for a file there is no guess to
31// make, and raising a cap only moves the guess. What survives is only what is genuinely unknowable in
32// advance -- the size of a child process's stdout, and one syscall's transfer buffer.
33const RG_EMIT_CAPTURE_BYTES: i64 = 1048576 // generator stdout: a whole emitted page, size unknown until it runs
34const RG_HTML_MIN_BYTES: i64 = 2000 // a validity FLOOR, not a buffer: shorter than this is not a page
35const RG_DEBTSTATE_BYTES: i64 = 65536 // debtstate accumulator; every write clamps on oo < ocap
36const RG_GETDENTS_BYTES: i64 = 65536 // ONE getdents64 transfer -- the caller loops until it returns 0
37const RG_CHILD_CAPTURE_BYTES: i64 = 262144 // a forked tool's stdout, size unknowable in advance
38
39func w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
40func wn(fd: i64, v: i64) -> i64 {
41 var m: i64 = v; if m < 0 { w(fd, "-" as *u8); m = 0 - m }
42 let t: *u8 = sys_mmap(24); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
43 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
44 let o: *u8 = sys_mmap(24); var i: i64 = 0; while i < k { o[i] = t[k-1-i]; i = i + 1 } sys_write(fd, o, k); return 0
45}
46func scopy(dst: *u8, doff: i64, src: *u8) -> i64 { var i: i64 = 0; while src[i] != (0 as u8) { dst[doff+i] = src[i]; i = i + 1 } return doff + i }
47// rg_read IS GONE (2026-08-14). It was a capped whole-file reader whose loop -- `while tot < cap` --
48// returned a PREFIX and reported it as the length, with no way for any caller to tell a complete read
49// from a truncated one. Seven call sites trusted it: the domain roster (twice), the radar roster, the
50// hand roster, each domain's .matrix, and the organ under each watch contract. One of those was
51// MEASURABLY truncating (nx_parse.nx at 316,533 B against a 262,144 cap) and the rest were one growth
52// spurt away. Every site now composes sys_read_file, which sizes its buffer from the file by lseek END
53// and cannot short-read -- so the failure mode is deleted rather than made less likely.
54// -- A CEILING THAT HAS TO BE GUESSED IS A DEFECT GENERATOR IN BOTH DIRECTIONS, AND RAISING IT ONLY
55// MOVES THE GUESS.
56// atomic publish: write path.new then rename over path (the S-class content-publish primitive).
57func rg_write_atomic(path: *u8, buf: *u8, n: i64) -> i64 {
58 let evidence:*i64=rgr_alloc(16) as *i64
59 if (evidence as i64)<=0 {return RGR_E_WRITE}
60 let rc:i64=rgr_write_atomic(path,buf,n,evidence,(evidence as i64+8) as *i64)
61 if rc!=0 {
62 w(1," ATOMIC-FAIL code=" as *u8);wn(1,rc)
63 w(1," syscall=" as *u8);wn(1,evidence[1])
64 w(1," artifact_published=" as *u8);wn(1,evidence[0]);w(1,"\n" as *u8)
65 }
66 rgr_free(evidence as *u8,16)
67 return rc
68}
69// scaffold: ensure <droot><dom>/ exists before emit (mkdir 0755; EEXIST is harmless). Root-fixes the
70// "never scaffolds" gap that blocked publishing a BRAND-NEW domain (no docroot dir -> WRITE-FAIL). Fail-safe.
71func rg_scaffold(droot: *u8, dom: *u8) -> i64 {
72 let d: *u8 = sys_mmap(600)
73 var o: i64 = scopy(d, 0, droot); o = scopy(d, o, dom); d[o] = 0 as u8
74 sys_mkdir(d, 493) // 0755; ignore return -- EEXIST on an existing domain is expected
75 return 0
76}
77// run elf with up to 2 args, capture stdout. returns child exit code; outlen[0]=captured bytes.
78func rg_run(elf: *u8, a1: *u8, a2: *u8, out: *u8, cap: i64, outlen: *i64) -> i64 {
79 let av: *i64 = sys_mmap(64) as *i64
80 av[0] = elf as i64
81 var n: i64 = 1
82 if (a1 as i64) != 0 { av[n] = a1 as i64; n = n + 1 }
83 if (a2 as i64) != 0 { av[n] = a2 as i64; n = n + 1 }
84 av[n] = 0
85 return tr_run_capture(elf, av, out, cap, outlen)
86}
87// ---- Durable publish receipts; completion rows preserve the existing journal contract.
88// Extra reserve bootstrap=0 means no additional estate reserve; actual writes remain authoritative.
89const RG_PUBJRNL: *u8 = "../knowledge/status/comparepub.jrnl"
90static g_rg_receipt_failures:i64
91static g_rg_published_incomplete:i64
92// a records-file row's fixed bytes beyond its fields: the underscore + five TABs + the newline (7),
93// the longest state word LANDED (6), and a NUL (1).
94const RG_WATCHROW_OVERHEAD: i64 = 14
95func rgr_num(b:*u8,off:i64,v:i64)->i64 {return rgr_decimal(b,off,v)}
96// ---- SINGLETON + I/O ADMISSION, IN THE EMITTER, FOR EVERY LAUNCHER (2026-09-02, the I/O-storm lane) ----------
97// MEASURED: at 15:57Z and 15:59Z two seats launched this organ two minutes apart while a third ran evidence
98// sweeps; nx_dstate at a raised ceiling then showed the array's D-state roster filled with THIS organ's children
99// (nx_swcompare_matrix, nx_site_publish, nx_nav_sentinel) beside the plane writers -- load 29 -> 43, iowait equal
100// to user time, every /api/build refused for hours. The clock beat is already storm-deferred (clk_storm_defer +
101// clock_heavy.conf); a SEAT-launched regen bypassed that ruler entirely, and nothing stopped two of them running
102// at once. Both are closed here, once, for every launcher (beat, MCP nishi_compare_regen, nx_job_run):
103// (1) ONE regen at a time -- ../nx_lease.elf acquire compare-regen <owner> <ttl>. A second launcher is told the
104// holder and exits with nx_lease's own BUSY code; the lease is released before the summary line, and the
105// TTL (900 s = the measured worst run, 811 s, with margin) reaps a crashed holder on its own.
106// (2) the SAME I/O-storm ruler the build gate and the clock use (nx_ioadmit_lib): a zero spawn budget REFUSES
107// with the live figures and the build gate's QUEUE code -- a regen that starts into a storm only deepens
108// it and publishes nothing sooner. UNREADABLE never refuses (abstain toward the pre-change behaviour).
109// Neither can lose a publish: a broken lease organ is announced and the run proceeds unleased.
110const RG_LEASE_NAME: *u8 = "compare-regen"
111const RG_LEASE_TTL_S: *u8 = "900" // nx_lease takes the ttl as text; DERIVED from the measured worst run 811 s + margin
112const RG_LEASE_CAP: i64 = 4096
113const RG_EXIT_BUSY: i64 = 3 // nx_lease's own BUSY exit, relayed unchanged so a caller learns one code
114const RG_EXIT_STORM: i64 = 4 // the build gate's QUEUE exit, relayed unchanged for the same reason
115const RG_LEASE_ELF: *u8 = "../nx_lease.elf" // CWD is the build root; the promoted lease organ lives at its parent
116static g_rg_lease_owner: *u8
117// fork the lease organ with this run's owner tag; out is NUL-terminated so the holder line can be relayed verbatim
118func rg_lease_run(verb: *u8, out: *u8) -> i64 {
119 let av: *i64 = sys_mmap(64) as *i64
120 av[0] = RG_LEASE_ELF as i64
121 av[1] = verb as i64
122 av[2] = RG_LEASE_NAME as i64
123 av[3] = g_rg_lease_owner as i64
124 var n: i64 = 4
125 if verb[0] == (97 as u8) { av[n] = RG_LEASE_TTL_S as i64; n = n + 1 } // 'a'cquire carries the ttl; release does not
126 av[n] = 0
127 let ol: *i64 = sys_mmap(16) as *i64
128 ol[0] = 0
129 let rc: i64 = tr_run_capture(RG_LEASE_ELF, av, out, RG_LEASE_CAP - 1, ol)
130 if ol[0] >= 0 { if ol[0] < RG_LEASE_CAP { out[ol[0]] = 0 as u8 } }
131 return rc
132}
133func rg_lease_release() -> i64 {
134 if (g_rg_lease_owner as i64) == 0 { return 0 }
135 let out: *u8 = sys_mmap(RG_LEASE_CAP)
136 let rc: i64 = rg_lease_run("release" as *u8, out)
137 sys_munmap(out, RG_LEASE_CAP)
138 g_rg_lease_owner = 0 as *u8
139 return rc
140}
141
142static g_rg_transaction_sequence:i64
143// Existing emitter contract is retained; all audit failures return nonzero to the run.
144func rg_emit_receipted(elf: *u8, a1: *u8, mode: *u8, firstbyte: i64, minbytes: i64, target: *u8, out: *u8, cap: i64, journal:*u8) -> i64 {
145 let ol: *i64 = rgr_alloc(16) as *i64
146 if (ol as i64)<=0 {return 0-1}
147 let rc: i64 = rg_run(elf, a1, mode, out, cap, ol)
148 if rc != 0 { w(1, " EMIT-FAIL rc nonzero: " as *u8); w(1, target); w(1, "\n" as *u8); rgr_free(ol as *u8,16);return 0 - 1 }
149 if ol[0] < minbytes { w(1, " EMIT-FAIL too small: " as *u8); w(1, target); w(1, "\n" as *u8);rgr_free(ol as *u8,16);return 0 - 2 }
150 if (out[0] as i64) != firstbyte { w(1, " EMIT-FAIL wrong leading byte: " as *u8); w(1, target); w(1, "\n" as *u8);rgr_free(ol as *u8,16);return 0 - 3 }
151 var prev:i64=0-1
152 let pf:i64=sys_openat_rd(target)
153 if pf>=0 {prev=sys_lseek(pf,0,2);sys_close(pf)}
154 let tx:*RgrTx=rgr_alloc(13*8) as *RgrTx
155 if (tx as i64)<=0 {rgr_free(ol as *u8,16);return 0-6}
156 let idcap:i64=2*RGR_DECIMAL_BYTES+2
157 let id:*u8=rgr_alloc(idcap)
158 if (id as i64)<=0 {rgr_free(tx as *u8,13*8);rgr_free(ol as *u8,16);return 0-6}
159 var o:i64=rgr_decimal(id,0,sys_now_realtime_us());o=rgr_copy(id,o,"-" as *u8)
160 g_rg_transaction_sequence=g_rg_transaction_sequence+1
161 rgr_decimal(id,o,g_rg_transaction_sequence)
162 let tr:i64=rgr_transact(tx,journal,target,out,ol[0],prev,sys_now_realtime_sec(),id,RGR_NO_EXTRA_RESERVE)
163 var result:i64=tr
164 if tr!=0 {
165 g_rg_receipt_failures=g_rg_receipt_failures+1
166 if tx.published==1 {g_rg_published_incomplete=g_rg_published_incomplete+1}
167 w(1," RECEIPT-TRANSACTION-FAIL code=" as *u8);wn(1,tr)
168 w(1," syscall=" as *u8);wn(1,tx.detail)
169 w(1," artifact_published=" as *u8);wn(1,tx.published)
170 w(1," target=" as *u8);w(1,target)
171 w(1," intent=" as *u8);if (tx.intentpath as i64)>0 {w(1,tx.intentpath)}
172 w(1," reserve_extra_bytes=" as *u8);wn(1,tx.reserve)
173 w(1," available_bavail_bytes=" as *u8);wn(1,tx.available)
174 w(1," required_data_bytes=" as *u8);wn(1,tx.required)
175 w(1," next_action=reconcile retained intent before retry; no automatic crash replay\n" as *u8)
176 } else {
177 w(1, " published " as *u8); w(1, target); w(1, " (" as *u8); wn(1, ol[0]); w(1, " bytes" as *u8)
178 if prev >= 0 { w(1, " prev=" as *u8); wn(1, prev); if ol[0] < prev { w(1, " SHRANK" as *u8) } }
179 w(1, ") receipt=durable reserve_extra_bytes=0 (no additional estate reserve; bavail is advisory)\n" as *u8)
180 }
181 let release:i64=rgr_release(tx)
182 if release!=0 {
183 w(1," RECEIPT-CLOSE-FAIL syscall=" as *u8);wn(1,release);w(1,"\n" as *u8)
184 if result==0 {g_rg_receipt_failures=g_rg_receipt_failures+1;g_rg_published_incomplete=g_rg_published_incomplete+1}
185 result=RGR_E_DURABLE
186 }
187 rgr_free(tx as *u8,13*8);rgr_free(id,idcap);rgr_free(ol as *u8,16)
188 if result!=0 {return 0-6}
189 return 0
190}
191func rg_emit(elf:*u8,a1:*u8,mode:*u8,firstbyte:i64,minbytes:i64,target:*u8,out:*u8,cap:i64)->i64 {return rg_emit_receipted(elf,a1,mode,firstbyte,minbytes,target,out,cap,RG_PUBJRNL)}
192// ---- WATCHLIST PASS (2026-08-13, operator: the compare IS the workflow -- parallel workstreams
193// as a hive). Every _ABSENT_:<sym> watch row across the matrix roster is reconciled into ONE
194// machine-readable artifact, published twice: knowledge/status/compare_watchlist.tsv (the PM /
195// frontier plane intake) and <droot>watchlist.tsv (the public hive queue). Status is MEASURED here
196// with the same ruler as the page cells, so LANDED appears the moment an organ ships its symbol.
197// Additive like the census: this pass can never turn the core publish RED.
198// SUBSTRING MATCHING IS NOT OWNED HERE: the hand-rolled rgw_has that used to sit at this spot was a
199// SECOND scanner beside nx_tool_run's tr_contains, which this organ already imports transitively --
200// the duplicate-ruler defect wearing a local helper. Removed 2026-08-14 after proving it had exactly
201// ONE call site (corpus_complete=1 over 23,073 files). The only behavioural difference was on an EMPTY
202// needle (rgw_has 0, tr_contains 1), which is unreachable here: the watch parser requires at least one
203// character after "_ABSENT_:" before a row is treated as a contract at all.
204func rg_watchlist(droot: *u8) -> i64 {
205 w(1, " watchlist (the hive queue -- every watch contract across the roster):\n" as *u8)
206 // THE DOMAIN ROSTER IS READ WHOLE OR NOT AT ALL. A capped read here would drop domains off the
207 // TAIL of regen.list in silence, and a domain that never gets scanned contributes no watch rows --
208 // which is indistinguishable from a domain that has none.
209 let llen: *i64 = sys_mmap(16) as *i64
210 let lbuf: *u8 = sys_read_file("knowledge/compare/regen.list" as *u8, llen)
211 if (lbuf as i64) == 0 { w(1, " UNOBSERVABLE (regen.list unreadable)\n" as *u8); return 0 - 1 }
212 let lln: i64 = llen[0]
213 if lln <= 0 { w(1, " UNOBSERVABLE (regen.list empty)\n" as *u8); return 0 - 1 }
214 // BATCHED PLANE WRITE (2026-08-19, the 1787080869 convoy fix). The old pass forked
215 // ../nx_store_put.elf ONCE PER WATCH ROW: ~90 forks x 2 planes x fsync-under-flock serialized in
216 // the fs journal, so ONE watchlist pass ran 1-3 HOURS under load and regens stacked faster than
217 // they drained (MEASURED 2026-08-18: 23 stacked regens, the lock holder D-state in
218 // wait_log_commit, waiters 78 min in locks_lock_inode_wait). The store ALREADY grew the cure the
219 // same day -- putmany, one sts_load + one sts_seed for the whole batch ("the 88->1 fsync
220 // collapse", its own words) -- and this pass now COMPOSES it: rows accumulate in a records file,
221 // ONE fork commits them all. Records-file shape is putmany-owned: one row per line, TAB-joined,
222 // first field = id; the per-row retry is gone BECAUSE the race it paced around is gone.
223 let mlen: *i64 = sys_mmap(16) as *i64
224 let obuf: *u8 = sys_mmap(RG_CHILD_CAPTURE_BYTES)
225 let mp: *u8 = sys_mmap(600)
226 let op: *u8 = sys_mmap(600)
227 let sp: *u8 = sys_mmap(256)
228 let av: *i64 = sys_mmap(96) as *i64
229 let prl: *i64 = sys_mmap(16) as *i64
230 let olen: *i64 = sys_mmap(16) as *i64
231 let spe: *u8 = "../nx_store_put.elf" as *u8
232 let pfx: *u8 = "../knowledge/store/comparewatch-" as *u8
233 let vactor: *u8 = "nx_compare_regen" as *u8
234 let recpath: *u8 = "/tmp/comparewatch_batch.rec" as *u8
235 let rfd: i64 = sys_openat_wr(recpath, MODE_0644)
236 if rfd < 0 { w(1, " UNOBSERVABLE (cannot open the batch records file)\n" as *u8); return 0 - 1 }
237 var open_c: i64 = 0
238 var land_c: i64 = 0
239 // DARK (2026-09-06, operator: a permanent fix, we do not build and lose capability to darkness): the organ file
240 // the watch names EXISTS and was read, but does not declare the contracted symbol -- something shipped there
241 // under another name. A strict refinement of OPEN: still work, now named. The ruler already separated the two
242 // (sd_present -1 unreadable / 0 read-and-absent); every consumer folded them together until today.
243 var dark_c: i64 = 0
244 var p: i64 = 0
245 while p < lln {
246 var e: i64 = p
247 while e < lln { if lbuf[e] == (10 as u8) { break } e = e + 1 }
248 lbuf[e] = 0 as u8
249 let dom: *u8 = (lbuf as i64 + p) as *u8
250 p = e + 1
251 if dom[0] == (0 as u8) { } else { if dom[0] == (35 as u8) { } else {
252 var mo: i64 = scopy(mp, 0, "knowledge/compare/" as *u8)
253 mo = scopy(mp, mo, dom); mo = scopy(mp, mo, ".matrix" as *u8); mp[mo] = 0 as u8
254 let mbuf: *u8 = sys_read_file(mp, mlen)
255 var mn: i64 = 0
256 if (mbuf as i64) != 0 { mn = mlen[0] }
257 var q: i64 = 0
258 while q < mn {
259 var f: i64 = q
260 while f < mn { if mbuf[f] == (10 as u8) { break } f = f + 1 }
261 var p1: i64 = 0 - 1
262 var p2: i64 = 0 - 1
263 var p3: i64 = 0 - 1
264 var x: i64 = q
265 while x < f {
266 if mbuf[x] == (124 as u8) {
267 if p1 < 0 { p1 = x } else { if p2 < 0 { p2 = x } else { if p3 < 0 { p3 = x } } }
268 }
269 x = x + 1
270 }
271 var iswatch: i64 = 0
272 if p2 >= 0 { if p3 > p2 { if p3 - (p2 + 1) > 9 { if mbuf[q] != (35 as u8) {
273 let pref: *u8 = "_ABSENT_:" as *u8
274 var kk: i64 = 0
275 var okp: i64 = 1
276 var gop: i64 = 1
277 while gop == 1 {
278 if kk >= 9 { gop = 0 } else {
279 if mbuf[p2 + 1 + kk] != pref[kk] { okp = 0; gop = 0 } else { kk = kk + 1 }
280 }
281 }
282 if okp == 1 { iswatch = 1 }
283 } } } }
284 if iswatch == 1 {
285 var oc: i64 = 0
286 var y: i64 = p1 + 1
287 while y < p2 { op[oc] = mbuf[y]; oc = oc + 1; y = y + 1 }
288 op[oc] = 0 as u8
289 var sc: i64 = 0
290 y = p2 + 10
291 while y < p3 { sp[sc] = mbuf[y]; sc = sc + 1; y = y + 1 }
292 sp[sc] = 0 as u8
293 // the LANDED measurement reads the named organ source WHOLE (sys_read_file -- no cap
294 // to cross in silence) and asks THE ONE RULER (nx_symdecl_lib, 2026-08-23): a top-level
295 // declaration of the symbol. tr_contains (substring) used to sit here, so a comment or
296 // a call site naming the symbol wrote LANDED into the plane every seat reads for work
297 // (measured on lang LN8: LANDED here, OPEN in the ranker). The plane, the page cells
298 // and the ranker now share one function and cannot disagree.
299 let osrc: *u8 = sys_read_file(op, olen)
300 var landed: i64 = 0
301 let rl: *i64 = sys_mmap(16) as *i64
302 if (osrc as i64) != 0 { if olen[0] > 0 { landed = sd_present_buf(osrc, olen[0], op, sp, rl) } }
303 // one records-file row: <dom>_<symbol> TAB dom TAB label TAB organ TAB symbol TAB state --
304 // sized FROM THE FIELDS IT HOLDS (rule 11: a buffer is derived from its input, never guessed):
305 // dom twice, symbol twice, the label (p1 - q bytes), the organ, plus the fixed overhead.
306 var dlen: i64 = 0
307 while dom[dlen] != (0 as u8) { dlen = dlen + 1 }
308 let rrow: *u8 = sys_mmap(dlen + dlen + sc + sc + (p1 - q) + oc + RG_WATCHROW_OVERHEAD)
309 var ro: i64 = 0
310 ro = scopy(rrow, ro, dom)
311 rrow[ro] = 95 as u8; ro = ro + 1
312 ro = scopy(rrow, ro, sp)
313 rrow[ro] = 9 as u8; ro = ro + 1
314 ro = scopy(rrow, ro, dom)
315 rrow[ro] = 9 as u8; ro = ro + 1
316 var lc: i64 = q
317 while lc < p1 { rrow[ro] = mbuf[lc]; ro = ro + 1; lc = lc + 1 }
318 rrow[ro] = 9 as u8; ro = ro + 1
319 ro = scopy(rrow, ro, op)
320 rrow[ro] = 9 as u8; ro = ro + 1
321 ro = scopy(rrow, ro, sp)
322 rrow[ro] = 9 as u8; ro = ro + 1
323 if landed == 1 { ro = scopy(rrow, ro, "LANDED" as *u8); land_c = land_c + 1 }
324 else {
325 // DARK: the organ was READ (osrc non-null, olen > 0) and the declaration ruler said absent under a
326 // rule that can judge (the JS rule's abstention is folded to 0 by sd_present_buf and is excluded
327 // through rl[0]). Something shipped at this path under another name; the cell would read OPEN
328 // forever and the capability would be lost to darkness. Still work, now named.
329 var isdark: i64 = 0
330 if (osrc as i64) != 0 { if olen[0] > 0 { if rl[0] != SD_RULE_JSDECL { isdark = 1 } } }
331 if isdark == 1 { ro = scopy(rrow, ro, "DARK" as *u8); dark_c = dark_c + 1 }
332 else { ro = scopy(rrow, ro, "OPEN" as *u8); open_c = open_c + 1 }
333 }
334 rrow[ro] = 10 as u8; ro = ro + 1
335 sys_write(rfd, rrow, ro)
336 }
337 q = f + 1
338 }
339 } }
340 }
341 sys_close(rfd)
342 var stored: i64 = 0
343 if open_c + land_c + dark_c > 0 {
344 av[0] = spe as i64
345 av[1] = pfx as i64
346 av[2] = "putmany" as *u8 as i64
347 av[3] = vactor as i64
348 av[4] = recpath as i64
349 av[5] = 0
350 prl[0] = 0
351 let prc: i64 = tr_run_capture(spe, av, obuf, RG_CHILD_CAPTURE_BYTES, prl)
352 // PRINT THE STORE OWN WORDS -- capturing a diagnostic and not printing it is the same as
353 // not capturing it (this file own banked law).
354 if prl[0] > 0 { w(1, " " as *u8); sys_write(1, obuf, prl[0]) }
355 if prc == 0 { stored = open_c + land_c + dark_c }
356 }
357 w(1, " contracts OPEN=" as *u8); wn(1, open_c); w(1, " LANDED=" as *u8); wn(1, land_c); w(1, " DARK=" as *u8); wn(1, dark_c)
358 w(1, " -> comparewatch- plane rows stored=" as *u8); wn(1, stored)
359 w(1, " of " as *u8); wn(1, open_c + land_c + dark_c)
360 w(1, " via=putmany-single-commit" as *u8)
361 // A PARTITION IS A CLAIM -- PRINT THE VERDICT, DO NOT MAKE THE READER SUBTRACT.
362 if stored == open_c + land_c + dark_c { w(1, " partition=RECONCILES\n" as *u8) } else {
363 w(1, " partition=SHORTFALL -- the batch did not commit; the store own words are above\n" as *u8)
364 }
365 return 0
366}
367
368// ---- DEBT STATE SYNC (2026-08-14). The registers on each compare page are AUTHORED prose, but their
369// STATE must not be. This forks the debt tool per domain and writes `<dom>.debtstate` as `id<TAB>state`,
370// so the generator can show what the PLANE says rather than what the page claims.
371// ★THE SPLIT IS DELIBERATE AND WAS SETTLED BY PROBING THE TOOL: the PLANE owns STATE (open/eaten), the
372// PAGE owns NARRATIVE. Regenerating the register wholesale from the plane would destroy the written
373// remedies; pushing page prose into the plane would corrupt the audit trail. So: state only, one way.
374// It also exposes ids that are NOT in the plane at all -- page-local notes wearing the shape of a filed
375// debt -- which is the honest thing to surface rather than let them read as filed work.
376// Line shape from the tool: <index> <epoch-id>\t<sev>\t<scope>\t<state>\t<desc>
377func rg_debtstate(dom: *u8, buf: *u8, cap: i64, out: *u8, ocap: i64) -> i64 {
378 let av: *i64 = sys_mmap(64) as *i64
379 let ol: *i64 = sys_mmap(16) as *i64
380 let ELF: *u8 = "../nx_debt.elf" as *u8
381 // ⚠PASS THE PLANE PREFIX EXPLICITLY. The debt tool resolves its store RELATIVE TO CWD, and this organ
382 // runs from buildroot where that path does not exist -- so it found zero rows and wrote no file, which
383 // is indistinguishable from "this domain has no debt". Same CWD-relative trap as the two knowledge
384 // trees, the gate forking "../elf", and the watchlist writing into the wrong tree: FOUR times in one
385 // day. ★A TOOL THAT RESOLVES ITS DATA RELATIVE TO CWD IS A TOOL THAT LIES QUIETLY WHEN FORKED.
386 av[0] = ELF as i64
387 av[1] = "find" as i64
388 av[2] = dom as i64
389 av[3] = "../knowledge/store/debt-" as i64
390 av[4] = 0
391 // ⚠DO NOT GATE ON THE EXIT CODE. This is a QUERY, and a query tool's exit status is not a reliable
392 // success signal -- it may encode a match count or a no-match. The output is the answer, so parse what
393 // came back and let `rows > 0` decide whether anything is written. Gating on rc silently produced no
394 // state file at all on the first run, which reads exactly like "this domain has no debt".
395 tr_run_capture(ELF, av, buf, cap, ol)
396 let n: i64 = ol[0]
397 if n <= 0 { return 0 - 1 }
398 var oo: i64 = 0
399 var p: i64 = 0
400 var rows: i64 = 0
401 while p < n {
402 var e: i64 = p
403 while e < n { if buf[e] == (10 as u8) { break } e = e + 1 }
404 // id begins after the first space, and runs to the first tab
405 var s: i64 = p
406 while s < e { if buf[s] == (32 as u8) { break } s = s + 1 }
407 if s < e {
408 s = s + 1
409 var idend: i64 = s
410 while idend < e { if buf[idend] == (9 as u8) { break } idend = idend + 1 }
411 if idend < e {
412 // walk three tab-separated fields to reach state: sev, scope, state
413 var f: i64 = 0
414 var q: i64 = idend
415 var sts: i64 = 0 - 1
416 while q < e {
417 if buf[q] == (9 as u8) {
418 f = f + 1
419 if f == 3 { sts = q + 1; q = e } else { q = q + 1 }
420 } else { q = q + 1 }
421 }
422 if sts > 0 {
423 var ste: i64 = sts
424 while ste < e { if buf[ste] == (9 as u8) { break } ste = ste + 1 }
425 var k: i64 = s
426 while k < idend { if oo < ocap { out[oo] = buf[k]; oo = oo + 1 } k = k + 1 }
427 if oo < ocap { out[oo] = 9 as u8; oo = oo + 1 }
428 k = sts
429 while k < ste { if oo < ocap { out[oo] = buf[k]; oo = oo + 1 } k = k + 1 }
430 if oo < ocap { out[oo] = 10 as u8; oo = oo + 1 }
431 rows = rows + 1
432 }
433 }
434 }
435 p = e + 1
436 }
437 let path: *u8 = sys_mmap(600)
438 var po: i64 = scopy(path, 0, "knowledge/compare/" as *u8)
439 po = scopy(path, po, dom)
440 po = scopy(path, po, ".debtstate" as *u8)
441 path[po] = 0 as u8
442 if rows > 0 { rg_write_atomic(path, out, oo) }
443 return rows
444}
445// ---- ORPHAN CENSUS (2026-08-13, operator: one sota compare capability, no orphans) ----
446// Reconciles the LIVE docroot against every roster that feeds this emitter. Each top-level dir in
447// <droot> must be claimed by exactly one of: regen.list (matrix/sota lane), radar.list (radar lane),
448// or hand.roster (adjudicated hand pages awaiting matrix admission -- a SHRINK-ONLY worklist). An
449// unclaimed dir is an ORPHAN, printed BY NAME with the remedy. Additive by design: the census never
450// turns the core publish RED (a permanently-RED detector is one everyone learns to ignore); new
451// sprawl is loud on every run, and the partition is printed so the parts provably sum. It re-reads
452// the roster files itself because main()'s line loop NUL-terminates lines in its own buffers.
453func rgc_inlist(lst: *u8, ln: i64, name: *u8) -> i64 {
454 var p: i64 = 0
455 while p < ln {
456 var e: i64 = p
457 while e < ln { if lst[e] == (10 as u8) { break } e = e + 1 }
458 var i: i64 = 0
459 var ok: i64 = 1
460 var stop: i64 = 0
461 while stop == 0 {
462 if p + i >= e { stop = 1 } else {
463 let c: i64 = lst[p + i]
464 if c == 124 { stop = 1 } else {
465 let nc: i64 = name[i]
466 if nc == 0 { ok = 0; stop = 1 } else {
467 if nc != c { ok = 0; stop = 1 } else { i = i + 1 }
468 }
469 }
470 }
471 }
472 if ok == 1 { let nc2: i64 = name[i]; if nc2 == 0 { if i > 0 { return 1 } } }
473 p = e + 1
474 }
475 return 0
476}
477func rg_census(droot: *u8) -> i64 {
478 w(1, " orphan census (docroot dirs vs regen.list + radar.list + hand.roster):\n" as *u8)
479 // ALL THREE ROSTERS READ WHOLE. This census decides ORPHAN by absence from these lists, so a capped
480 // read is not merely lossy here -- it MANUFACTURES FALSE ORPHANS: a domain whose name sat past the cap
481 // would be reported by name as unclaimed sprawl, with a remedy attached, while its roster row existed
482 // all along. An absence verdict may never rest on a read that could have been partial.
483 let mlen2: *i64 = sys_mmap(16) as *i64
484 let rlen2: *i64 = sys_mmap(16) as *i64
485 let hlen2: *i64 = sys_mmap(16) as *i64
486 let mbuf: *u8 = sys_read_file("knowledge/compare/regen.list" as *u8, mlen2)
487 let rbuf: *u8 = sys_read_file("knowledge/compare/radar.list" as *u8, rlen2)
488 let hbuf: *u8 = sys_read_file("knowledge/compare/hand.roster" as *u8, hlen2)
489 var mln: i64 = 0
490 var rln: i64 = 0
491 var hln: i64 = 0
492 if (mbuf as i64) != 0 { mln = mlen2[0] }
493 if (rbuf as i64) != 0 { rln = rlen2[0] }
494 if (hbuf as i64) != 0 { hln = hlen2[0] }
495 if mln <= 0 { w(1, " CENSUS-UNOBSERVABLE: regen.list unreadable -- refusing to judge orphans\n" as *u8); return 0 - 1 }
496 if hln <= 0 { w(1, " (hand.roster absent -- every unroster'd dir will read ORPHAN)\n" as *u8) }
497 let dfd: i64 = sys_openat_rd(droot)
498 if dfd < 0 { w(1, " CENSUS-UNOBSERVABLE: cannot open docroot (withheld, not GREEN)\n" as *u8); return 0 - 1 }
499 let dbuf: *u8 = sys_mmap(RG_GETDENTS_BYTES)
500 var dirs: i64 = 0
501 var inm: i64 = 0
502 var inr: i64 = 0
503 var inh: i64 = 0
504 var orph: i64 = 0
505 var go: i64 = 1
506 while go == 1 {
507 let nr: i64 = sys_getdents64(dfd, dbuf, RG_GETDENTS_BYTES)
508 if nr <= 0 { go = 0 } else {
509 var o: i64 = 0
510 while o < nr {
511 let rec: *u8 = (dbuf as i64 + o) as *u8
512 let ty: i64 = dirent_type(rec)
513 let nm: *u8 = dirent_name(rec)
514 let n0: i64 = nm[0]
515 if ty == DT_DIR { if n0 != 46 {
516 dirs = dirs + 1
517 if rgc_inlist(mbuf, mln, nm) == 1 { inm = inm + 1 } else {
518 if rgc_inlist(rbuf, rln, nm) == 1 { inr = inr + 1 } else {
519 if rgc_inlist(hbuf, hln, nm) == 1 { inh = inh + 1 } else {
520 orph = orph + 1
521 w(1, " ORPHAN-DIR " as *u8); w(1, nm)
522 w(1, " (no roster claims it -- adopt: regen.list+matrix | radar.list+axes | hand.roster row; or retire)\n" as *u8)
523 } } }
524 } }
525 o = o + dirent_reclen(rec)
526 }
527 }
528 }
529 sys_close(dfd)
530 w(1, " census dirs=" as *u8); wn(1, dirs)
531 w(1, " matrix=" as *u8); wn(1, inm)
532 w(1, " radar=" as *u8); wn(1, inr)
533 w(1, " hand=" as *u8); wn(1, inh)
534 w(1, " ORPHANS=" as *u8); wn(1, orph)
535 let parts: i64 = inm + inr + inh + orph
536 if parts == dirs { w(1, " parts-sum OK\n" as *u8) } else { w(1, " PARTITION-LEAK parts!=dirs\n" as *u8) }
537 return orph
538}
539// ---- FORK FRESHNESS: THE PUBLISHER CHECKS ITS OWN FORKS BEFORE IT PUBLISHES (2026-08-31) ----
540// THE DEFECT, MEASURED TWICE IN ONE DAY. This organ chdirs into the BUILD ROOT and forks the bare path
541// _offc/<gen>.elf, so the publisher of every /compare page executes buildroot/_offc/. But /api/promote
542// writes the SERVING ROOT (and nishihost/_offc); buildroot/_offc updates ONLY via a manual nx_restage.
543// A regen therefore returned published=201 fails=0 verdict=GREEN and CHANGED NOTHING -- every publish
544// line read prev= EQUAL to the new byte count -- because all three generators were a generation behind.
545// FOUR AGENTS EACH VERIFIED THEIR PROMOTE AGAINST THE SERVING ROOT AND/OR nishihost/_offc AND ALL FOUR
546// MISSED THIS COPY. nx_catalog cannot see buildroot/_offc, so every row read BUILT==PROMOTED with_gaps=0
547// VERDICT LIVE -- the clean-looking reading that hid it. The remedy was "remember to nx_restage", which
548// is O(seats) forever, and a law that must be RECALLED at the moment of temptation is not a control.
549//
550// ANNOUNCE, DELIBERATELY -- NOT REFUSE. Read how this receipt is consumed before choosing: `fails` drives
551// verdict=GREEN/RED and this organ EXIT CODE, which the compare beat stamp depends on. Folding fork
552// staleness into `fails` would turn the whole surface RED and, because restage is a MANUAL step, would
553// leave /compare unpublishable until a human intervened -- the permanently-red detector everyone learns
554// to ignore, installed on the estate own headline surface. It is not hypothetical: on the day this
555// shipped, TWO of the four generators were stale, so a refusal would have taken the surface down at once.
556// FAIL DIRECTION, ESTABLISHED BEFORE SHIPPING: this check can only PRINT and only COUNT. It cannot stop a
557// publish that would otherwise succeed, cannot delete, cannot restage. Its worst false positive costs a
558// wrong line of text; a refusal false positive costs the surface.
559// AND AN ANNOUNCEMENT NOBODY READS IS THE ABSTENTION NOBODY READS, so the result is ALSO a NAMED,
560// MACHINE-READABLE FIELD on the summary line and a durable truncate-written status file -- a consumer can
561// branch on the staleness without the publisher becoming a single point of failure for publishing.
562const RG_FORK_MATRIX: i64 = 0
563const RG_FORK_SOTA: i64 = 1
564const RG_FORK_HUB: i64 = 2
565// the SELFCATALOG status line: ts + three counters, ~80 B measured; 4096 is a reserve for one line (the ratchet named
566// the bare literal 2026-09-02 -- a number without a purpose is a number nobody can re-derive)
567const RG_SELFCAT_STATUS_BYTES: i64 = 4096
568const RG_FORK_BOARD: i64 = 3
569const RG_FORKS: i64 = 4 // the DENOMINATOR: every generator this organ forks from _offc/
570const RG_PROMOTED_PREFIX: *u8 = "../" // CWD is the build root; /api/promote installs FLAT at its parent
571const RG_FORKFRESH_STATUS: *u8 = "../knowledge/status/compare_forkfresh.status"
572const RG_FORKSTATUS_BYTES: i64 = 4096 // derived from RG_FORKS rows plus a header, not guessed
573const RG_ELF_SUFFIX_LEN: i64 = 4
574
575static g_rg_fork_fresh: i64
576static g_rg_fork_stale: i64
577static g_rg_fork_unproven: i64
578static g_rg_fork_absent: i64
579static g_rg_fork_checked: i64
580static g_rg_fork_state: *i64
581
582// ONE TABLE OWNS THESE PATHS AND main() FORKS FROM IT (see the MATRIX/SOTA/HUB/BOARD bindings below).
583// A checker holding its own second copy of the path literals would be a duplicate ruler that drifts the
584// day somebody adds a generator -- and the check would then be silently measuring a binary nobody runs.
585// nx_swcompare_gapmap is deliberately NOT in this table: it is already forked from ../ , i.e. from the
586// promoted artifact itself, so checking it would compare a file with itself and manufacture a green.
587func rg_forkpath(idx: i64) -> *u8 {
588 if idx == RG_FORK_MATRIX { return "_offc/nx_swcompare_matrix.elf" as *u8 }
589 if idx == RG_FORK_SOTA { return "_offc/nx_swcompare_sota.elf" as *u8 }
590 if idx == RG_FORK_HUB { return "_offc/nx_swcompare_hub.elf" as *u8 }
591 return "_offc/nx_maturity_board.elf" as *u8
592}
593
594// The nx_restage TARGET name, DERIVED from the fork path rather than written a second time.
595func rg_forkname(idx: i64, out: *u8) -> i64 {
596 let p: *u8 = rg_forkpath(idx)
597 let b: i64 = ff_basename(p)
598 var n: i64 = 0
599 while p[b + n] != (0 as u8) { n = n + 1 }
600 var k: i64 = n - RG_ELF_SUFFIX_LEN
601 if k < 0 { k = n }
602 var i: i64 = 0
603 while i < k { out[i] = p[b + i]; i = i + 1 }
604 out[k] = 0 as u8
605 return k
606}
607
608// A STDOUT-ONLY ANNOUNCEMENT IN AN ORGAN RUN BY A BEAT IS AN ABSTENTION NOBODY READS -- a cron row that
609// discards its output makes its own finding unobservable. So the same measurement lands in a durable,
610// TRUNCATE-WRITTEN status file with a canonical verdict LAST (never an append-only journal, which any
611// substring check would read as vacuously green forever). It reuses rg_write_atomic, the writer this
612// organ already owns, rather than introducing a second write idiom.
613// ITS SUBJECT IS FORK FRESHNESS AND NOTHING ELSE: a RED here says the publisher is executing a binary
614// that is not the promoted one. That is a true statement about the forks and is deliberately NOT the
615// publish verdict, which keeps its own meaning on this organ own last line.
616// It reads the states RECORDED BY THE SINGLE PASS in rg_forkcheck rather than re-classifying: two passes
617// could straddle a restage and then the printed line and the status file would contradict each other.
618func rg_forkstatus() -> i64 {
619 let b: *u8 = sys_mmap(RG_FORKSTATUS_BYTES)
620 var o: i64 = 0
621 o = scopy(b, o, "ts=" as *u8); o = rgr_num(b, o, sys_now_realtime_sec())
622 o = scopy(b, o, "\nsubject=fork-freshness-of-the-compare-generators" as *u8)
623 o = scopy(b, o, "\nchecked=" as *u8); o = rgr_num(b, o, g_rg_fork_checked)
624 o = scopy(b, o, "\nfresh=" as *u8); o = rgr_num(b, o, g_rg_fork_fresh)
625 o = scopy(b, o, "\nstale=" as *u8); o = rgr_num(b, o, g_rg_fork_stale)
626 o = scopy(b, o, "\nunproven=" as *u8); o = rgr_num(b, o, g_rg_fork_unproven)
627 o = scopy(b, o, "\nabsent=" as *u8); o = rgr_num(b, o, g_rg_fork_absent)
628 let nm: *u8 = sys_mmap(FF_PATH_BYTES)
629 var i: i64 = 0
630 while i < RG_FORKS {
631 rg_forkname(i, nm)
632 o = scopy(b, o, "\n" as *u8); o = scopy(b, o, ff_state_word(g_rg_fork_state[i]))
633 o = scopy(b, o, " " as *u8); o = scopy(b, o, nm)
634 i = i + 1
635 }
636 o = scopy(b, o, "\nverdict=" as *u8)
637 if g_rg_fork_stale == 0 { o = scopy(b, o, "GREEN" as *u8) } else { o = scopy(b, o, "RED" as *u8) }
638 o = scopy(b, o, "\n" as *u8)
639 return rg_write_atomic(RG_FORKFRESH_STATUS, b, o)
640}
641
642// Runs BEFORE anything is published, so the announcement precedes the pages it is about.
643// Returns the STALE count; the caller reports it and does not act on it.
644func rg_forkcheck() -> i64 {
645 w(1, " fork freshness -- the generator copies this publisher ACTUALLY executes, vs the PROMOTED artifacts:\n" as *u8)
646 g_rg_fork_state = sys_mmap(RG_FORKS * 8) as *i64
647 let fdig: *u8 = sys_mmap(FF_DIGEST_BYTES)
648 let rdig: *u8 = sys_mmap(FF_DIGEST_BYTES)
649 let fhex: *u8 = sys_mmap(FF_HEX_BYTES)
650 let rhex: *u8 = sys_mmap(FF_HEX_BYTES)
651 let refp: *u8 = sys_mmap(FF_PATH_BYTES)
652 let nm: *u8 = sys_mmap(FF_PATH_BYTES)
653 let sz: *i64 = sys_mmap(FF_SIZES_SLOTS * 8) as *i64
654 var i: i64 = 0
655 while i < RG_FORKS {
656 let fp: *u8 = rg_forkpath(i)
657 ff_promoted_path(fp, refp, RG_PROMOTED_PREFIX)
658 let st: i64 = ff_classify(fp, refp, fdig, rdig, sz)
659 g_rg_fork_state[i] = st
660 // THE DIRECTION IS DERIVED ONCE, BESIDE THE CLASSIFICATION AND AT THE SAME SCOPE AS THE STATE IT
661 // QUALIFIES, so the printed remedy and the recorded state can never straddle two measurements.
662 // It is FF_DIR_NA by construction for every non-STALE row, so deriving it unconditionally is safe.
663 let dir: i64 = ff_direction(st, sz)
664 rg_forkname(i, nm)
665 g_rg_fork_checked = g_rg_fork_checked + 1
666 w(1, " " as *u8); w(1, ff_state_word(st)); w(1, " " as *u8); w(1, fp)
667 if sz[0] >= 0 { w(1, " " as *u8); wn(1, sz[0]); w(1, "B" as *u8) }
668 w(1, "\n" as *u8)
669 if st == FF_FRESH { g_rg_fork_fresh = g_rg_fork_fresh + 1 }
670 if st == FF_UNPROVEN {
671 g_rg_fork_unproven = g_rg_fork_unproven + 1
672 w(1, " COULD NOT LOOK: no readable promoted artifact at " as *u8); w(1, refp)
673 w(1, " -- this is NOT a finding that the fork is stale, and NOT a licence to trust it.\n" as *u8)
674 }
675 if st == FF_NOFORK {
676 g_rg_fork_absent = g_rg_fork_absent + 1
677 w(1, " the fork target itself is unreadable -- the fork will fail on its own below.\n" as *u8)
678 }
679 if st == FF_STALE {
680 g_rg_fork_stale = g_rg_fork_stale + 1
681 ff_hex(fdig, fhex, FF_DIGEST_BYTES)
682 ff_hex(rdig, rhex, FF_DIGEST_BYTES)
683 w(1, " RUNS " as *u8); w(1, fhex); w(1, "\n" as *u8)
684 w(1, " PROMOTED " as *u8); w(1, rhex); w(1, " (" as *u8); w(1, refp); w(1, " " as *u8); wn(1, sz[1]); w(1, "B)\n" as *u8)
685 w(1, " EVERY PAGE BELOW IS PUBLISHED BY THE COPY ON THE RUNS LINE, NOT BY WHAT WAS PROMOTED.\n" as *u8)
686 // THE REMEDY BRANCHES ON THE DIRECTION, BECAUSE THE TWO DIRECTIONS WANT OPPOSITE ACTIONS.
687 // This line used to print "nx_restage" UNCONDITIONALLY. That was correct for a fork that is
688 // BEHIND and was THE CAPABILITY-DESTROYING ACTION for a fork that is AHEAD -- and the check
689 // had already measured both digests and both sizes, so the comparison that decides it was in
690 // hand the whole time and only the message failed to use it. Caught live on nx_maturity_board
691 // by a publish owner who measured before obeying its instrument.
692 // IT STILL ONLY ANNOUNCES. It does not refuse and must not: 2 of 4 generators were stale the
693 // day this check shipped, and a refusal would have taken /compare down.
694 w(1, " DIRECTION " as *u8); w(1, ff_direction_word(dir))
695 w(1, " (fork " as *u8); wn(1, sz[0]); w(1, "B vs promoted " as *u8); wn(1, sz[1]); w(1, "B)\n" as *u8)
696 if ff_restage_is_safe(dir) == 1 {
697 w(1, " REMEDY: nx_restage " as *u8); w(1, nm); w(1, " (nothing else updates buildroot/_offc)\n" as *u8)
698 }
699 if dir == FF_DIR_AHEAD {
700 w(1, " DO NOT RESTAGE. The executing copy is LARGER than the promoted artifact, so it may\n" as *u8)
701 w(1, " carry runs the promoted one lacks, and a restage would overwrite exactly those.\n" as *u8)
702 w(1, " REMEDY: nx_contentdiff " as *u8); w(1, refp); w(1, " " as *u8); w(1, fp); w(1, "\n" as *u8)
703 w(1, " and if the fork proves a superset, REBUILD FROM SOURCE AND PROMOTE FORWARD so the\n" as *u8)
704 w(1, " promoted artifact GAINS it. Never restage backward. If the source can no longer\n" as *u8)
705 w(1, " rebuild what the fork carries, BANK THE FORK first -- it is unreproducible.\n" as *u8)
706 }
707 if dir == FF_DIR_UNDECIDED {
708 w(1, " UNDECIDED: the two artifacts are the SAME SIZE and still differ, so size cannot say\n" as *u8)
709 w(1, " which way, and restaging on a guess is a coin flip over a destructive action.\n" as *u8)
710 w(1, " WHAT WOULD SETTLE IT: nx_contentdiff in BOTH directions between\n" as *u8)
711 w(1, " " as *u8); w(1, refp); w(1, " and " as *u8); w(1, fp); w(1, "\n" as *u8)
712 w(1, " -- whichever side loses no runs is the one that is behind. If neither loses runs,\n" as *u8)
713 w(1, " the delta is code-only (a string ruler is blind to it) and nx_behaveprobe on a real\n" as *u8)
714 w(1, " workload is the instrument that decides.\n" as *u8)
715 }
716 if dir == FF_DIR_NA {
717 w(1, " DIRECTION UNAVAILABLE: a size was not measured, so NO remedy is named here rather\n" as *u8)
718 w(1, " than naming one that could be the destructive direction.\n" as *u8)
719 }
720 }
721 i = i + 1
722 }
723 // A PARTITION IS A CLAIM: CHECK THE PARTS SUM, AND PRINT THE SUM.
724 let parts: i64 = g_rg_fork_fresh + g_rg_fork_stale + g_rg_fork_unproven + g_rg_fork_absent
725 w(1, " forkchecked=" as *u8); wn(1, g_rg_fork_checked)
726 w(1, " fresh=" as *u8); wn(1, g_rg_fork_fresh)
727 w(1, " stale=" as *u8); wn(1, g_rg_fork_stale)
728 w(1, " unproven=" as *u8); wn(1, g_rg_fork_unproven)
729 w(1, " absent=" as *u8); wn(1, g_rg_fork_absent)
730 w(1, " parts_sum=" as *u8); wn(1, parts)
731 if parts == g_rg_fork_checked { w(1, " partition=RECONCILED\n" as *u8) } else { w(1, " partition=LEAK\n" as *u8) }
732 if rg_forkstatus() != 0 { w(1, " (status file not written -- the stdout announcement above still stands)\n" as *u8) }
733 return g_rg_fork_stale
734}
735
736// ---- EC17 (ecosystem rung): THE ENGINE GRADES ITSELF, every run ----
737// Measured 2026-09-01 before this existed: of the organs that publish and rank the boards, the gap map
738// was PROMOTED-UNREGISTERED, the maturity board PROMOTED-UNREGISTERED and behind its own staged binary,
739// the fleet ranker REGISTERED-DARK, the flywheel beat PROMOTED-UNREGISTERED, and nx_comparestale is
740// REGISTERED and AUTHORISED while having no source and no binary at all. The surface that measures the
741// estate was the estate's own worst instance of the pattern it measures. Membership is DECLARED in
742// knowledge/compare/engine.roster (never a name pattern); each row is graded by clb_classify -- the same
743// ladder every board row gets -- and every gap prints WITH ITS REMEDY. Announces and counts, never
744// refuses: 6 of the engine's own organs carried gaps the day this shipped, and folding that into the
745// publish verdict would have taken the headline surface down until a human intervened -- the permanently
746// red detector, installed on /compare itself. UNPROVEN is its own exit: a missing roster publishes NO
747// counts, because zero-members-zero-gaps would be the vacuous green wearing a self-audit's name.
748func rg_selfcatalog() -> i64 {
749 let lp: *i64 = sys_mmap(16) as *i64
750 let rb: *u8 = sys_read_file("knowledge/compare/engine.roster" as *u8, lp)
751 if (rb as i64) == 0 { w(1, "SELFCATALOG UNPROVEN -- engine.roster missing; the engine publishes ungraded this run\n" as *u8); return 3 }
752 let rn: i64 = lp[0]
753 if rn <= 0 { w(1, "SELFCATALOG UNPROVEN -- engine.roster empty\n" as *u8); return 3 }
754 let ads: *i64 = sys_mmap(CLB_N * 8) as *i64
755 clb_load(ads)
756 var members: i64 = 0
757 var full: i64 = 0
758 var gaps: i64 = 0
759 let tok: *u8 = sys_mmap(CLB_TOK_CAP)
760 let remp: *i64 = sys_mmap(8) as *i64
761 var p: i64 = 0
762 while p < rn {
763 var e: i64 = p
764 while e < rn { if rb[e] == (10 as u8) { break } e = e + 1 }
765 var t: i64 = e
766 if t > p { if rb[t-1] == (13 as u8) { t = t - 1 } }
767 rb[t] = 0 as u8
768 let nm: *u8 = ((rb as i64) + p) as *u8
769 p = e + 1
770 if nm[0] != (0 as u8) { if nm[0] != (35 as u8) {
771 members = members + 1
772 tok[0] = 0 as u8
773 remp[0] = "" as *u8 as i64
774 let ful: i64 = clb_classify(nm, ads, tok, remp)
775 if ful == CLB_FULL { full = full + 1 } else {
776 gaps = gaps + 1
777 w(1, " SELFCATALOG-GAP " as *u8); w(1, nm); w(1, " " as *u8); w(1, tok)
778 let rr: *u8 = remp[0] as *u8
779 if rr[0] != (0 as u8) { w(1, " -- " as *u8); w(1, rr) }
780 w(1, "\n" as *u8)
781 }
782 } }
783 }
784 w(1, "SELFCATALOG members=" as *u8); wn(1, members)
785 w(1, " full=" as *u8); wn(1, full)
786 w(1, " gaps=" as *u8); wn(1, gaps)
787 w(1, " (partition: full+gaps=members) -- the surface that grades the estate, graded by its own ruler\n" as *u8)
788 let sb: *u8 = sys_mmap(RG_SELFCAT_STATUS_BYTES)
789 var so: i64 = 0
790 so = scopy(sb, so, "ts=" as *u8); so = rgr_num(sb, so, sys_now_realtime_sec())
791 so = scopy(sb, so, " members=" as *u8); so = rgr_num(sb, so, members)
792 so = scopy(sb, so, " full=" as *u8); so = rgr_num(sb, so, full)
793 so = scopy(sb, so, " gaps=" as *u8); so = rgr_num(sb, so, gaps)
794 so = scopy(sb, so, "\nverdict=" as *u8)
795 if gaps == 0 { so = scopy(sb, so, "GREEN" as *u8) } else { so = scopy(sb, so, "AMBER" as *u8) }
796 sb[so] = 10 as u8; so = so + 1
797 rg_write_atomic("../knowledge/status/compare_selfcatalog.status" as *u8, sb, so)
798 return 0
799}
800
801// Selected runs retain domain gates and receipts. Aggregate views have a separate scope.
802const RG_SCOPE_PATH_BYTES: i64 = 600
803func rg_scope_len(s: *u8) -> i64 {
804 var n: i64 = 0
805 while s[n] != (0 as u8) { n = n + 1 }
806 return n
807}
808func rg_scope_list(name: *u8, n: i64) -> *u8 {
809 let b: *u8 = sys_mmap(n + 2)
810 scopy(b, 0, name)
811 b[n] = 10 as u8
812 b[n + 1] = 0 as u8
813 return b
814}
815func rg_scope_receipt(droot: *u8, domain: *u8, pubs: i64, fails: i64) -> i64 {
816 let path: *u8 = sys_mmap(RG_SCOPE_PATH_BYTES)
817 var p: i64 = scopy(path, 0, droot)
818 p = scopy(path, p, domain)
819 p = scopy(path, p, "/scope.json" as *u8)
820 path[p] = 0 as u8
821 let b: *u8 = sys_mmap(RG_SCOPE_PATH_BYTES + RG_SCOPE_PATH_BYTES)
822 var o: i64 = scopy(b, 0, "{\"schema\":1,\"record_kind\":\"last_domain_scoped_run\",\"scope\":\"domain\",\"domain\":\"" as *u8)
823 o = scopy(b, o, domain)
824 o = scopy(b, o, "\",\"aggregates_refreshed_by_this_run\":false,\"published\":" as *u8)
825 o = rgr_num(b, o, pubs)
826 o = scopy(b, o, ",\"fails\":" as *u8)
827 o = rgr_num(b, o, fails)
828 o = scopy(b,o,",\"audit_failures\":" as *u8);o=rgr_num(b,o,g_rg_receipt_failures)
829 o = scopy(b,o,",\"published_evidence_incomplete\":" as *u8);o=rgr_num(b,o,g_rg_published_incomplete)
830 o = scopy(b,o,",\"receipt_extra_reserve_bytes\":0" as *u8)
831 o = scopy(b, o, ",\"ts\":" as *u8)
832 o = rgr_num(b, o, sys_now_realtime_sec())
833 o = scopy(b, o, "}\n" as *u8)
834 return rg_write_atomic(path, b, o)
835}
836// These finalizers are shared by the real run and the isolated subprocess gate.
837func rg_published_count(successful:i64)->i64 {return successful+g_rg_published_incomplete}
838func rg_exit_verdict(fails:i64)->i64 {
839 if fails == 0 && g_rg_receipt_failures==0 { w(1, " verdict=GREEN (measured fresh from source; atomic hot-swap; MCP+REST single source)\n" as *u8); sys_exit(0); return 0 }
840 w(1, " verdict=RED\n" as *u8); sys_exit(1); return 1
841}
842func main(argc: i64, argv: *i64) -> i64 {
843 var broot: *u8 = "buildroot" as *u8
844 var droot: *u8 = "../sites/nishifamily/compare/" as *u8
845 if argc >= 2 { broot = argv[1] as *u8 }
846 if argc >= 3 { droot = argv[2] as *u8 }
847 var selected: *u8 = 0 as *u8
848 var selected_len: i64 = 0
849 if argc > 3 {
850 if argc != 5 {
851 w(1, "USAGE: nx_compare_regen [buildroot [docroot]] | nx_compare_regen buildroot docroot --domain name\n" as *u8)
852 sys_exit(2); return 2
853 }
854 let opt: *u8 = argv[3] as *u8
855 if cscope_roster_count("--domain\n" as *u8, 9, opt, rg_scope_len(opt)) != 1 {
856 w(1, "USAGE: expected --domain name; no publication started\n" as *u8)
857 sys_exit(2); return 2
858 }
859 selected = argv[4] as *u8
860 selected_len = cscope_validate_name(selected, RG_SCOPE_PATH_BYTES - 1)
861 if selected_len < 1 {
862 w(1, "REFUSED-SCOPE: invalid domain name; no publication started\n" as *u8)
863 sys_exit(2); return 2
864 }
865 if rg_scope_len(droot) + selected_len + rg_scope_len("/frontier/index.html.new" as *u8) + 1 > RG_SCOPE_PATH_BYTES {
866 w(1, "REFUSED-SCOPE: output path exceeds existing emitter path bound\n" as *u8)
867 sys_exit(2); return 2
868 }
869 if rg_scope_len("knowledge/compare/" as *u8) + selected_len + rg_scope_len(".debtstate.new" as *u8) + 1 > RG_SCOPE_PATH_BYTES {
870 w(1, "REFUSED-SCOPE: source path exceeds existing emitter path bound\n" as *u8)
871 sys_exit(2); return 2
872 }
873 }
874 w(1, "=== NX-COMPARE-REGEN -- re-measure + atomically republish the compare surface (gate-checked) ===\n" as *u8)
875 // NAME THE PATH YOU ACTUALLY TRIED. This printed the literal word "buildroot" while chdir'ing whatever
876 // argv[1] held, so a caller who passed a DOMAIN (a reasonable guess -- every sibling compare tool takes
877 // one) got "cannot chdir buildroot" and went looking for a broken build root. The message named a
878 // subject the code was not operating on, which costs the reader exactly the investigation an error
879 // exists to save them. argv[1] is the BUILD ROOT; this organ regenerates the WHOLE surface.
880 // TRY THE CALLER'S CWD FIRST, THEN ANCHOR AS A FALLBACK. Both the build root and the docroot here
881 // are CWD-relative, so this organ only ever ran correctly from the estate root -- i.e. from the
882 // beat's own `cd`. Every other launcher (nx_job_run, the MCP surface) hands it a different CWD,
883 // where this chdir fails and the organ reports a broken BUILD ROOT when the real subject is the
884 // WORKING DIRECTORY -- a message naming a subject the code was not operating on, which is the very
885 // defect the comment above already records. ep_anchor binds that ONE act instead of asking every
886 // caller to remember it. Ordered as a FALLBACK and never as a precondition: a caller passing a
887 // relative build root reachable from its own CWD keeps its exact present meaning, so no currently
888 // working invocation can change behaviour.
889 var moved: i64 = sys_chdir(broot)
890 if moved != 0 {
891 let anch: i64 = ep_anchor()
892 if anch == 1 { w(1, " anchored to the estate root (the caller's CWD held no build root)\n" as *u8) }
893 moved = sys_chdir(broot)
894 }
895 if moved != 0 {
896 w(1, "FAIL: cannot chdir " as *u8); w(1, broot)
897 w(1, " -- argv[1] is the BUILD ROOT; use buildroot docroot --domain name for a selected domain\n" as *u8)
898 sys_exit(1); return 1
899 }
900 if selected_len > 0 {
901 let sl: *i64 = sys_mmap(16) as *i64
902 let sr: *u8 = sys_read_file("knowledge/compare/regen.list" as *u8, sl)
903 if cscope_roster_count(sr, sl[0], selected, selected_len) != 1 {
904 w(1, "REFUSED-SCOPE: domain must occur exactly once in regen.list; no publication started\n" as *u8)
905 sys_exit(2); return 2
906 }
907 w(1, "SCOPE domain=" as *u8); w(1, selected)
908 w(1, " aggregates_refreshed_by_this_run=0; unrelated domain and aggregate passes excluded\n" as *u8)
909 }
910 // ---- ADMISSION + SINGLETON, before a single fork (2026-09-02; rationale beside rg_lease_run) ----
911 let ioa: *i64 = sys_mmap(16) as *i64
912 var rg_budget: i64 = IOA_UNREADABLE
913 if ioa_measure(ioa) == 0 { rg_budget = ioa_spawn_budget(ioa[0], ioa[1], IOA_BLOCKED_PER_CPU, IOA_RESERVE_SLOTS) }
914 if rg_budget == 0 {
915 w(1, "REFUSED-IO-STORM procs_blocked=" as *u8); wn(1, ioa[1]); w(1, " ncpu=" as *u8); wn(1, ioa[0])
916 w(1, " sample=instant blocked_per_cpu=" as *u8); wn(1, IOA_BLOCKED_PER_CPU); w(1, " reserve_slots=" as *u8); wn(1, IOA_RESERVE_SLOTS)
917 w(1, " -- configured spawn budget is zero. This proxy does not establish sustained storage saturation or predict completion cost. No publication started. Build admission may use different evidence; retry through this publisher admission after pressure changes.\n" as *u8)
918 sys_exit(RG_EXIT_STORM); return RG_EXIT_STORM
919 }
920 if rg_budget < 0 { w(1, " io-admission UNOBSERVABLE (/proc/stat unreadable) -- proceeding, as the clock does\n" as *u8) }
921 // ---- THE CLASS BOUND (hub-spoke, 2026-09-03). The ioadmit check above is INSTANTANEOUS and per-caller: a
922 // burst of beats can each read "not stormed" and proceed, the storm being their SUM. hio_admit counts the
923 // heavy producers RUNNING across every launch surface against the width derived from knowledge/heavyio.conf,
924 // with the storm witness as its second conjunct. DEFER -> named refusal on the clock's requeue code;
925 // UNOBSERVABLE -> announced, proceed (a periodic publisher must never be silenced by a missing conf). The
926 // conf lives at the estate root and CWD is the build root here, so the ../ twin is tried first.
927 let hio: *i64 = sys_mmap(32) as *i64
928 var hv: i64 = hio_admit_conf("../knowledge/heavyio.conf" as *u8, hio)
929 if hv == HIO_UNOBSERVABLE { hv = hio_admit(hio) }
930 hio_announce(1, hv, hio)
931 if hv == HIO_DEFER {
932 w(1, "REFUSED-HEAVYIO: the heavy-I/O class bound is reached (running > width, or the storm line) -- a regen started now only deepens it. Re-fire when the HEAVYIO line reads ADMIT\n" as *u8)
933 sys_exit(RG_EXIT_STORM); return RG_EXIT_STORM
934 }
935 g_rg_lease_owner = sys_mmap(64)
936 var lo: i64 = scopy(g_rg_lease_owner, 0, "regen-" as *u8)
937 lo = rgr_num(g_rg_lease_owner, lo, sys_now_realtime_sec())
938 g_rg_lease_owner[lo] = 0 as u8
939 let lout: *u8 = sys_mmap(RG_LEASE_CAP)
940 let lrc: i64 = rg_lease_run("acquire" as *u8, lout)
941 if lrc == RG_EXIT_BUSY {
942 w(1, "REFUSED-ALREADY-RUNNING: another nx_compare_regen holds lease " as *u8); w(1, RG_LEASE_NAME); w(1, " -- " as *u8); w(1, lout)
943 w(1, "\n two regens at once double the array's fsync load and publish nothing sooner; wait for the holder or its TTL.\n" as *u8)
944 g_rg_lease_owner = 0 as *u8
945 sys_exit(RG_EXIT_BUSY); return RG_EXIT_BUSY
946 }
947 if lrc != 0 { w(1, " lease UNAVAILABLE (nx_lease rc=" as *u8); wn(1, lrc); w(1, ") -- proceeding unleased rather than blocking the publish on a broken lock\n" as *u8); g_rg_lease_owner = 0 as *u8 }
948 if lrc == 0 { w(1, " lease compare-regen held by " as *u8); w(1, g_rg_lease_owner); w(1, " (ttl " as *u8); w(1, RG_LEASE_TTL_S); w(1, " s; a second regen is refused until release)\n" as *u8) }
949 // BOUND FROM THE FORK TABLE, NOT RE-TYPED. rg_forkcheck iterates that same table, so the binary it
950 // checks and the binary this loop forks CANNOT diverge -- which is the whole point: a checker holding
951 // its own copy of these literals would go quietly out of date the day a generator is added or moved.
952 let MATRIX: *u8 = rg_forkpath(RG_FORK_MATRIX)
953 let SOTA: *u8 = rg_forkpath(RG_FORK_SOTA)
954 let HUB: *u8 = rg_forkpath(RG_FORK_HUB)
955 // IN THE PATH, BEFORE ANY PAGE IS WRITTEN. Announces and counts; it never refuses (see the reasoning
956 // above rg_forkpath). The pages published below are published by whatever this just measured.
957 rg_forkcheck()
958 // EC17: and the engine grades ITSELF before publishing anything -- announces and counts, never refuses
959 rg_selfcatalog()
960 let sprobe: *u8 = sys_mmap(600)
961 let cap: i64 = RG_EMIT_CAPTURE_BYTES
962 let out: *u8 = sys_mmap(cap)
963 let llen0: *i64 = sys_mmap(16) as *i64
964 var sota_c: i64 = 0
965 var sota_matrix_c: i64 = 0
966 var lst: *u8 = sys_read_file("knowledge/compare/regen.list" as *u8, llen0)
967 if (lst as i64) == 0 { w(1, "FAIL: regen.list missing\n" as *u8); rg_lease_release(); sys_exit(1); return 1 }
968 var ln: i64 = llen0[0]
969 if selected_len > 0 { lst = rg_scope_list(selected, selected_len); ln = selected_len + 1 }
970 if ln <= 0 { w(1, "FAIL: regen.list empty\n" as *u8); rg_lease_release(); sys_exit(1); return 1 }
971 // DEBT STATE FIRST, before any page is emitted -- a sync that runs after the emit loop would leave
972 // every page showing the PREVIOUS run's state, which is the quiet one-run lag that makes a dashboard
973 // wrong exactly when someone acts on it.
974 w(1, " debt state sync (plane owns state, page owns narrative):\n" as *u8)
975 let dsbuf: *u8 = sys_mmap(RG_CHILD_CAPTURE_BYTES)
976 let dsout: *u8 = sys_mmap(RG_DEBTSTATE_BYTES)
977 let dlen0: *i64 = sys_mmap(16) as *i64
978 var dlst: *u8 = sys_read_file("knowledge/compare/regen.list" as *u8, dlen0)
979 if selected_len > 0 { dlst = rg_scope_list(selected, selected_len); dlen0[0] = selected_len + 1 }
980 var dln: i64 = 0
981 if (dlst as i64) != 0 { dln = dlen0[0] }
982 var dsdom: i64 = 0
983 var dsrows: i64 = 0
984 var dp: i64 = 0
985 while dp < dln {
986 var de: i64 = dp
987 while de < dln { if dlst[de] == (10 as u8) { break } de = de + 1 }
988 dlst[de] = 0 as u8
989 let dd: *u8 = (dlst as i64 + dp) as *u8
990 dp = de + 1
991 if dd[0] == (0 as u8) { } else { if dd[0] == (35 as u8) { } else {
992 // ONLY domains that declare a plan render a debt register, so only they need the sync.
993 // Forking the debt tool for all 48 domains cost 48 full reads of a 3,930-row plane on every
994 // beat to serve one page. A feature that works and hammers the box is still a defect.
995 var pl: i64 = scopy(sprobe, 0, "knowledge/compare/" as *u8)
996 pl = scopy(sprobe, pl, dd)
997 pl = scopy(sprobe, pl, ".plan" as *u8)
998 sprobe[pl] = 0 as u8
999 let pfd2: i64 = sys_openat_rd(sprobe)
1000 if pfd2 >= 0 {
1001 sys_close(pfd2)
1002 let dr: i64 = rg_debtstate(dd, dsbuf, RG_CHILD_CAPTURE_BYTES, dsout, RG_DEBTSTATE_BYTES)
1003 if dr > 0 { dsdom = dsdom + 1; dsrows = dsrows + dr }
1004 }
1005 } }
1006 }
1007 w(1, " domains with filed debt=" as *u8); wn(1, dsdom)
1008 w(1, " rows=" as *u8); wn(1, dsrows); w(1, "\n" as *u8)
1009 let target: *u8 = sys_mmap(600)
1010 let ol: *i64 = sys_mmap(16) as *i64
1011 var fails: i64 = 0
1012 var pubs: i64 = 0
1013 var p: i64 = 0
1014 while p < ln {
1015 var e: i64 = p
1016 while e < ln { if lst[e] == (10 as u8) { break } e = e + 1 }
1017 lst[e] = 0 as u8
1018 let dom: *u8 = (lst as i64 + p) as *u8
1019 p = e + 1
1020 if dom[0] == (0 as u8) { } else { if dom[0] == (35 as u8) { } else {
1021 w(1, " domain " as *u8); w(1, dom); w(1, ":\n" as *u8)
1022 // a <dom>.sota file promotes the domain to the SOTA generator (N competitors, quantitative,
1023 // categorized); otherwise the 4-column matrix generator. Same CLI shape, same gate contract.
1024 var gen: *u8 = MATRIX
1025 var so: i64 = scopy(sprobe, 0, "knowledge/compare/" as *u8); so = scopy(sprobe, so, dom); so = scopy(sprobe, so, ".sota" as *u8); sprobe[so] = 0 as u8
1026 let sfd: i64 = sys_openat_rd(sprobe)
1027 // ONE ASSEMBLER (2026-09-02): a sota-class domain is rendered by the MATRIX generator too, which now
1028 // composes the SOTA field from nx_swcompare_sota_lib. The census below still counts the class;
1029 // the SOTA fork path stays declared (and fork-fresh-checked) until the second generator is retired.
1030 if sfd >= 0 { sys_close(sfd); gen = MATRIX; w(1, " (sota-class: N-competitor quantitative -- rendered by the one assembler)\n" as *u8)
1031 // census (2026-08-23): a sota-class domain that ALSO carries a .matrix now renders its watch
1032 // contracts on the page (nx_swcompare_lib watch_pass) -- count the population that reaches.
1033 sota_c = sota_c + 1
1034 var mpo: i64 = scopy(sprobe, 0, "knowledge/compare/" as *u8); mpo = scopy(sprobe, mpo, dom); mpo = scopy(sprobe, mpo, ".matrix" as *u8); sprobe[mpo] = 0 as u8
1035 let mfd: i64 = sys_openat_rd(sprobe)
1036 if mfd >= 0 { sys_close(mfd); sota_matrix_c = sota_matrix_c + 1; w(1, " (carries a .matrix: watch contracts rendered, measured)\n" as *u8) }
1037 }
1038 let grc: i64 = rg_run(gen, dom, 0 as *u8, out, cap, ol)
1039 if grc != 0 {
1040 w(1, " GATE RED -- refusing to publish this domain\n" as *u8)
1041 // PRINT THE GENERATOR'S OWN WORDS (2026-08-07). rg_run already CAPTURED the reason
1042 // into `out` and this branch threw it away, so every refusal read as an unexplained
1043 // GATE RED and the only way to learn why was to guess. I guessed twice today (a
1044 // buildroot/ path prefix, then a missing @verdict) and was wrong twice, on a domain
1045 // whose generator had been naming the real cause all along.
1046 // ★A GATE THAT REPORTS A FAILURE WITHOUT ITS DIAGNOSTIC IS UNACTIONABLE -- the law was
1047 // already written in nx_cc_equiv_gate's own comments; this loop had not adopted it.
1048 // ★CAPTURING A DIAGNOSTIC AND NOT PRINTING IT IS THE SAME AS NOT CAPTURING IT.
1049 if ol[0] > 0 {
1050 w(1, " --- generator said ---\n" as *u8)
1051 sys_write(1, out, ol[0])
1052 w(1, " --- end ---\n" as *u8)
1053 }
1054 fails = fails + 1
1055 } else {
1056 rg_scaffold(droot, dom) // create the domain docroot dir if new (fixes WRITE-FAIL on first publish)
1057 var o: i64 = scopy(target, 0, droot); o = scopy(target, o, dom); o = scopy(target, o, "/api.json" as *u8); target[o] = 0 as u8
1058 if rg_emit(gen, dom, "json" as *u8, 123, 400, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1059 o = scopy(target, 0, droot); o = scopy(target, o, dom); o = scopy(target, o, "/index.html" as *u8); target[o] = 0 as u8
1060 if rg_emit(gen, dom, "html" as *u8, 60, RG_HTML_MIN_BYTES, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1061 // FRONTIER door (2026-08-05, debt 1785937893): a <dom>.axes file promotes the domain to ALSO
1062 // publish its researcher-fed frontier radar via the gapmap generator -- same gate contract as
1063 // matrix/sota. FAIL-SAFE by design: a RED frontier gate (banks absent / vacuous momentum) skips
1064 // LOUDLY without failing the core publish; the radar is additive, its gate is its own.
1065 var ax: i64 = scopy(sprobe, 0, "knowledge/compare/" as *u8); ax = scopy(sprobe, ax, dom); ax = scopy(sprobe, ax, ".axes" as *u8); sprobe[ax] = 0 as u8
1066 let axfd: i64 = sys_openat_rd(sprobe)
1067 if axfd >= 0 { sys_close(axfd)
1068 // promoted-elf path (nishihost root, CWD is buildroot): /api/build + /api/promote place it -- API-pure staging
1069 let GAPMAP: *u8 = "../nx_swcompare_gapmap.elf" as *u8
1070 let gfd2: i64 = sys_openat_rd(GAPMAP)
1071 if gfd2 < 0 { w(1, " frontier SKIPPED (nx_swcompare_gapmap.elf not promoted -- /api/build + /api/promote it)\n" as *u8) } else { sys_close(gfd2)
1072 let frc: i64 = rg_run(GAPMAP, dom, 0 as *u8, out, cap, ol)
1073 if frc != 0 { w(1, " frontier gate RED (corpus banks absent or vacuous) -- skipping frontier, core artifacts stand\n" as *u8) } else {
1074 var fo: i64 = scopy(target, 0, droot); fo = scopy(target, fo, dom); fo = scopy(target, fo, "/frontier" as *u8); target[fo] = 0 as u8
1075 sys_mkdir(target, 493)
1076 fo = scopy(target, 0, droot); fo = scopy(target, fo, dom); fo = scopy(target, fo, "/frontier/index.html" as *u8); target[fo] = 0 as u8
1077 if rg_emit(GAPMAP, dom, "html" as *u8, 60, RG_HTML_MIN_BYTES, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1078 }
1079 }
1080 }
1081 }
1082 } }
1083 }
1084 if selected_len == 0 {
1085 // RADAR PASS (2026-08-08). The .axes/gapmap radar domains are NOT in regen.list and have no
1086 // .matrix/.sota source, so the loop above cannot carry them: it runs MATRIX/SOTA FIRST and a missing
1087 // source prints "GATE RED -- refusing to publish this domain" and fails the whole run. They were
1088 // therefore hand-published, and went 20-21 DAYS STALE while every one of them still served HTTP 200,
1089 // so the liveness sentinel reported them perfectly healthy the entire time.
1090 // (STAR)LIVENESS AND FRESHNESS ARE DIFFERENT QUESTIONS -- A 200 SAYS NOTHING ABOUT AGE.
1091 // SAFE HALF ONLY: this emits <dom>/api.json and NOTHING ELSE. Those paths mostly 404 today so the
1092 // write cannot clobber anything; regenerating their HTML from .axes COULD destroy curated content
1093 // (/compare/crm is a 13,169 B hand-authored page) and is deliberately NOT done here.
1094 // ADDITIVE LIKE THE FRONTIER DOOR: a radar failure is reported LOUDLY but does NOT increment fails,
1095 // because the radar must never be able to turn the core publish RED (and with it the beat's stamp).
1096 w(1, " radar pass:\n" as *u8)
1097 let rlen0: *i64 = sys_mmap(16) as *i64
1098 let rlst: *u8 = sys_read_file("knowledge/compare/radar.list" as *u8, rlen0)
1099 var rln: i64 = 0
1100 if (rlst as i64) != 0 { rln = rlen0[0] }
1101 if rln <= 0 { w(1, " SKIPPED (no radar.list in this buildroot)\n" as *u8) } else {
1102 let RGAP: *u8 = "../nx_swcompare_gapmap.elf" as *u8
1103 let rgfd: i64 = sys_openat_rd(RGAP)
1104 if rgfd < 0 { w(1, " SKIPPED (nx_swcompare_gapmap.elf not promoted)\n" as *u8) } else { sys_close(rgfd)
1105 var rp: i64 = 0
1106 while rp < rln {
1107 var re: i64 = rp
1108 while re < rln { if rlst[re] == (10 as u8) { break } re = re + 1 }
1109 rlst[re] = 0 as u8
1110 let rdom: *u8 = (rlst as i64 + rp) as *u8
1111 rp = re + 1
1112 if rdom[0] == (0 as u8) { } else { if rdom[0] == (35 as u8) { } else {
1113 w(1, " domain " as *u8); w(1, rdom); w(1, ":\n" as *u8)
1114 rg_scaffold(droot, rdom)
1115 var ro: i64 = scopy(target, 0, droot); ro = scopy(target, ro, rdom); ro = scopy(target, ro, "/api.json" as *u8); target[ro] = 0 as u8
1116 let radar_emit:i64=rg_emit(RGAP, rdom, "json" as *u8, 123, 400, target, out, cap)
1117 if radar_emit == 0 { pubs = pubs + 1 } else { if radar_emit==(0-6) {fails=fails+1} w(1, " radar emission failed; published/evidence state shown above\n" as *u8) }
1118 } }
1119 }
1120 }
1121 }
1122 rg_census(droot)
1123 rg_watchlist(droot)
1124 w(1, " hub:\n" as *u8)
1125 // The hub/openapi regenerate FROM the registry. On the NAS the registry is DELIBERATELY not synced
1126 // (it is multi-session contended on the laptop; regenerating from a stale snapshot would clobber
1127 // parallel additions) -> absent registry = SKIP hub cleanly, matrices-only regen. Laptop publishes hub.
1128 let regchk: i64 = sys_openat_rd("knowledge/compare/registry" as *u8)
1129 if regchk < 0 {
1130 w(1, " SKIPPED (registry not synced here; hub + openapi are laptop-published by design)\n" as *u8)
1131 } else { sys_close(regchk)
1132 let hrc: i64 = rg_run(HUB, 0 as *u8, 0 as *u8, out, cap, ol)
1133 if hrc != 0 { w(1, " HUB GATE RED -- refusing to publish hub\n" as *u8); fails = fails + 1 } else {
1134 var o2: i64 = scopy(target, 0, droot); o2 = scopy(target, o2, "api.json" as *u8); target[o2] = 0 as u8
1135 if rg_emit(HUB, "json" as *u8, 0 as *u8, 123, 400, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1136 o2 = scopy(target, 0, droot); o2 = scopy(target, o2, "index.html" as *u8); target[o2] = 0 as u8
1137 if rg_emit(HUB, "html" as *u8, 0 as *u8, 60, RG_HTML_MIN_BYTES, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1138 o2 = scopy(target, 0, droot); o2 = scopy(target, o2, "openapi.json" as *u8); target[o2] = 0 as u8
1139 if rg_emit(HUB, "openapi" as *u8, 0 as *u8, 123, 800, target, out, cap) == 0 { pubs = pubs + 1 } else { fails = fails + 1 }
1140 } }
1141 // MATURITY BOARD (2026-07-16, operator "the high scores are liars without a maturity analysis"):
1142 // every regen re-grades ALL matrix domains with the evidence-capped census and republishes
1143 // /compare/maturity (presence vs honest maturity + inflation ratio). The board writes atomically
1144 // itself (same .nxnew+rename discipline) and FAILS CLOSED below 10 graded domains.
1145 w(1, " maturity board:\n" as *u8)
1146 let BOARD: *u8 = rg_forkpath(RG_FORK_BOARD)
1147 let bfd: i64 = sys_openat_rd(BOARD)
1148 if bfd < 0 { w(1, " SKIPPED (board organ not staged in this buildroot)\n" as *u8) } else { sys_close(bfd)
1149 let brc: i64 = rg_run(BOARD, 0 as *u8, 0 as *u8, out, cap, ol)
1150 if brc != 0 { w(1, " MATURITY-BOARD RED -- refusing\n" as *u8); fails = fails + 1 }
1151 else { w(1, " published /compare/maturity (evidence-capped census over every matrix domain)\n" as *u8); pubs = pubs + 1 }
1152 }
1153 } else {
1154 pubs=rg_published_count(pubs)
1155 if rg_scope_receipt(droot, selected, pubs, fails) != 0 {
1156 w(1, "SCOPE-RECEIPT-FAIL: domain publication cannot claim complete scope metadata\n" as *u8)
1157 fails = fails + 1
1158 }
1159 w(1, "SCOPE-COMPLETE domain=" as *u8); w(1, selected)
1160 w(1, " aggregates_refreshed_by_this_run=0; full-surface freshness not claimed\n" as *u8)
1161 }
1162 w(1, " sota-class domains=" as *u8); wn(1, sota_c); w(1, " with_matrix=" as *u8); wn(1, sota_matrix_c); w(1, " (watch contracts rendered on those pages; the rest have no contracts to render)\n" as *u8)
1163 w(1," audit_failures=" as *u8);wn(1,g_rg_receipt_failures);w(1," published_evidence_incomplete=" as *u8);wn(1,g_rg_published_incomplete);w(1," reserve_extra_bytes=0 (no additional estate reserve; bavail advisory)\n" as *u8)
1164 if selected_len==0 {pubs=rg_published_count(pubs)}
1165 rg_lease_release() // every publish is done; a second regen may now start (2026-09-02)
1166 w(1, "REGEN published=" as *u8); wn(1, pubs); w(1, " fails=" as *u8); wn(1, fails)
1167 if selected_len > 0 { w(1, " scope=domain aggregates_refreshed_by_this_run=0" as *u8) } else { w(1, " scope=full" as *u8) }
1168 // THE NAMED FIELDS A CONSUMER BRANCHES ON, so the fork check is not an announcement nobody reads.
1169 // Placed BEFORE verdict= deliberately: this estate judges by the LAST line and anchors by POSITION,
1170 // so verdict must remain the final token here. STRICTLY ADDITIVE -- none of these touch `fails`, so
1171 // no run that passed before can fail now. forkstale>0 beside verdict=GREEN is the honest and intended
1172 // reading: the surface published, AND it published from a binary that is not the promoted one.
1173 w(1, " forkchecked=" as *u8); wn(1, g_rg_fork_checked)
1174 w(1, " forkfresh=" as *u8); wn(1, g_rg_fork_fresh)
1175 w(1, " forkstale=" as *u8); wn(1, g_rg_fork_stale)
1176 w(1, " forkunproven=" as *u8); wn(1, g_rg_fork_unproven)
1177 w(1, " forkabsent=" as *u8); wn(1, g_rg_fork_absent)
1178 return rg_exit_verdict(fails)
1179}