nx_frontier_scan_review_candidate_t346.nx source
↩ module page · 1703 lines · 92991 B
1// nx_frontier_scan.nx -- THE AUTONOMOUS FRONTIER-NOTICER for a /compare domain.
2// Operator 2026-08-20: "we want to be able to notice things like the metahuman gap without needing claude
3// by having our own research on /compare sota and best of breed and workflowed and mcp and api etc".
4//
5// WHAT THE METAHUMAN GAP ACTUALLY WAS (mined from the transcripts, not assumed -- writer session
6// 55724797-9f7b-4f68-81aa-fd2de7997175, 2026-08-18 18:15:25Z): the operator said the characters looked
7// wrong, a seat ran FIVE WebSearch calls, and appended four citations to charsim.refs whose URLs IT NEVER
8// OPENED (three carry mirror '-' pin '-'; the fourth points at a page fetched three days earlier from a
9// DIFFERENT url). The gap therefore had TWO halves and only ONE of them needed the outside:
10// (A) the INTERNAL deficit signal -- rivals coded Best on skin/hair rows where our symbol is _ABSENT_.
11// Already mechanical, already in our own data, and NOBODY HAD EVER COMPUTED IT.
12// (B) the OUTSIDE BAR -- the 2026 numbers. That genuinely needed the live web.
13// This organ computes (A) from the domain's own files and pulls (B) through the sovereign fetcher.
14//
15// IT EMITS PROPOSALS AND NEVER ADMITS ONE. It does not write <domain>.matrix, <domain>.refs, or any watch
16// cell. The measured law it obeys: no margin threshold makes auto-declaration safe (nx_memsugg, precision
17// 808->890 permil at 24 percent coverage), and A COMPLETION SIGNAL THAT KEYS ON A NAME REWARDS WRITING THE
18// NAME. The list is the answer; a seat adjudicates it.
19//
20// SEEDS ARE DATA, NEVER HAND-TYPED. They come from the domain's own board:
21// <domain>.axes field 4 is literally "frontier-kw;frontier-kw" -- the estate ALREADY declares per-row
22// frontier keywords and nothing had ever consumed them.
23// <domain>.matrix field 1 (the row label) -- always, so a domain without .axes still has seeds.
24// SOURCES ARE DATA TOO: knowledge/frontier_sources.conf. Both endpoints shipped with it were PROVEN
25// reachable through nx_research_fetch before a line of this was written (arXiv API 200/12267 B, HN Algolia
26// 200/17063 B) -- necessary because nx_websearch is OUR OWN CRAWL INDEX (0 results for "metahuman"), so
27// it structurally cannot see the outside.
28//
29// VERBS
30// seeds <domain> -- what this domain is about, from its own data. No network.
31// deficit <domain> -- the internal signal, ranked, WITH THE REASON PER ROW. No network.
32// scan <domain> [maxrows] [maxitems] -- fetch + diff + print proposals. Network. Writes nothing.
33// propose <domain> [maxrows] [maxitems] -- scan, then file the worklist + the frontierprop- plane rows.
34//
35// RESOURCE ENVELOPE (a shipping criterion, not an afterthought): outbound requests are exactly
36// rows_scanned x sources, both bounded by conf and echoed in every run line; buffers are sized FROM THE
37// INPUT (sys_read_file, matrix+refs bytes, source count) so there is no ceiling to guess; the only writes
38// are one worklist file and at most rows x sources x 2 plane upserts, keyed by url hash so a re-scan
39// UPSERTS rather than grows.
40//
41// EXIT: 0 = ran (PROPOSALS may be zero -- that is a RESULT, not a failure) | 1 = REFUSED (named reason)
42// 2 = usage | 3 = domain unreadable
43// 100% sovereign. No hardware writes (Rule 26). license_tier: ORIGINAL expect_exit: 0
44import "nx_syscalls.nx"
45import "nx_deploy_lib.nx"
46// THE ONE RESOLVER FOR A /compare DATA FILE THAT LIVES IN TWO TREES (adopted 2026-09-01).
47// WHY: this organ built its compare paths with a hardcoded "buildroot/knowledge/compare/", and .axes
48// is an AUTHORED file class that lives in the BARE knowledge/ tree. So `seeds` printed
49// "SEED-SOURCE axes ABSENT" for EVERY domain and silently fell back to matrix row LABELS -- which means
50// the per-row `frontier-kw;frontier-kw` field the estate deliberately declares, in 49 .axes files, has
51// never once been used by the frontier noticer it was written for.
52// MEASURED 2026-09-01: buildroot/knowledge/compare/search.axes is ABSENT, knowledge/compare/search.axes
53// is 2533 B. The visible cost: the label "Warm index across index updates" seeded arXiv as
54// warm AND index AND across and returned a paper on tidally locked exoplanets.
55// COMPOSED, NOT HAND-ROLLED: nx_comparetree_lib is the estate's incumbent for exactly this, and the
56// comparetree_adoption ratchet measures 75 offenders against 2 adopters and RISING. Patching a second
57// hardcoded path here would have made this organ offender 76 in the same edit that fixed its symptom.
58import "nx_comparetree_lib.nx"
59// ATTEST (2026-09-06): the bar ruler's month arithmetic and its plan-row readers are COMPOSED, never re-implemented --
60// one parser for sotabar| rows and one YYYY-MM reader, so this organ and nx_barfresh cannot disagree about a month.
61import "nx_barfresh_lib.nx"
62import "nx_buf_dyn.nx"
63import "nx_estate_path.nx"
64
65const FS_PIPE: i64 = 124
66const FS_NL: i64 = 10
67const FS_HASH: i64 = 35
68const FS_AT: i64 = 64
69const FS_SEMI: i64 = 59
70const FS_SPACE: i64 = 32
71const FS_PLUS: i64 = 43
72const FS_ZERO: i64 = 48
73const FS_NINE: i64 = 57
74const FS_UPA: i64 = 65
75const FS_UPZ: i64 = 90
76const FS_LOA: i64 = 97
77const FS_LOZ: i64 = 122
78const FS_USCORE: i64 = 95
79const FS_SLASH: i64 = 47
80const FS_COLON: i64 = 58
81const FS_LBRACE: i64 = 123
82// A SECTION TAG is the short word before a colon at the head of a board label -- CREATOR, SKIN, BODY,
83// ANIM, SIM, WORLD, PLATFORM, MESH, HAIR on charsim alone. It names a group, never the subject, so it
84// poisons a query. 16 bytes clears the longest of those with headroom and is short enough that it can
85// never swallow a sentence that merely happens to contain a colon later on.
86const FS_SECTION_MAX: i64 = 16
87const FS_PATHCAP: i64 = 1024
88const FS_URLCAP: i64 = 2048
89const FS_TXTCAP: i64 = 4096
90const FS_MAXP: i64 = 12
91const FS_MAXROWS: i64 = 64
92const FS_RIVALS: i64 = 4
93const FS_CODE_BEST: i64 = 2
94const FS_CODE_YES: i64 = 1
95const FS_HASH_BASE: i64 = 131
96const FS_HASH_MASK: i64 = 0xffffffff
97const FS_HEXDIG: i64 = 8
98const FS_HEXSHIFT: i64 = 4
99const FS_HEXRADIX: i64 = 16
100const FS_PERMIL: i64 = 1000
101const FS_DIRMODE: i64 = 0x1ed
102const FS_PLANE_ARGS: i64 = 10
103// A worklist line carries a label, a title, a url and a mirror path. Each of those four is already bounded
104// by FS_URLCAP or FS_PATHCAP elsewhere in this organ, so the line bound is DERIVED from those bounds
105// rather than picked: it is not a ceiling anyone has to guess.
106const FS_LINECAP: i64 = 4 * 2048 + 1024
107// At most TWO outside items may be proposed against any ONE deficit row. A third is evidence the seed is
108// too broad, not that the gap is bigger -- so this is a discrimination bound, not a budget.
109const FS_PROPS_PER_ROW: i64 = 2
110
111func fs_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
112func fs_w(s: *u8) -> i64 { sys_write(1, s, fs_slen(s)); return 0 }
113func fs_wn(base: *u8, off: i64, len: i64) -> i64 {
114 if len <= 0 { return 0 }
115 sys_write(1, ((base as i64) + off) as *u8, len)
116 return 0
117}
118func fs_n(v: i64) -> i64 { nxi_fd(1, v); return 0 }
119func fs_cat(dst: *u8, pos: i64, s: *u8) -> i64 {
120 var p: i64 = pos
121 var i: i64 = 0
122 let n: i64 = fs_slen(s)
123 while i < n { dst[p] = s[i]; p = p + 1; i = i + 1 }
124 return p
125}
126func fs_catn(dst: *u8, pos: i64, src: *u8, a: i64, b: i64) -> i64 {
127 var p: i64 = pos
128 var i: i64 = a
129 while i < b { dst[p] = src[i]; p = p + 1; i = i + 1 }
130 return p
131}
132func fs_eol(buf: *u8, n: i64, i: i64) -> i64 {
133 var e: i64 = i
134 while e < n { if buf[e] == (FS_NL as u8) { return e } e = e + 1 }
135 return n
136}
137// literal search of buf[from..n) -- index of the FIRST byte of the match, or -1.
138func fs_find(buf: *u8, n: i64, from: i64, lit: *u8) -> i64 {
139 let l: i64 = fs_slen(lit)
140 if l <= 0 { return 0 - 1 }
141 if n < l { return 0 - 1 }
142 var i: i64 = from
143 if i < 0 { i = 0 }
144 while i <= n - l {
145 var j: i64 = 0
146 var m: i64 = 1
147 while j < l { if buf[i+j] != lit[j] { m = 0; j = l } else { j = j + 1 } }
148 if m == 1 { return i }
149 i = i + 1
150 }
151 return 0 - 1
152}
153// THE ITEM BOUNDARY. Returns the offset one past this item's last byte: the next delimiter after this
154// one, or n when this is the last item. Every field extraction is bounded by it, so a field absent from
155// an item can never be satisfied by the NEXT item's copy of that field.
156func fs_item_end(buf: *u8, n: i64, it: i64, idlm: *u8) -> i64 {
157 let e: i64 = fs_find(buf, n, it + fs_slen(idlm), idlm)
158 if e < 0 { return n }
159 return e
160}
161// alphanumeric fold: letters lowered, digits kept, everything else -> 0 (a separator).
162func fs_fold(c: i64) -> i64 {
163 if c >= FS_ZERO { if c <= FS_NINE { return c } }
164 if c >= FS_LOA { if c <= FS_LOZ { return c } }
165 if c >= FS_UPA { if c <= FS_UPZ { return c + (FS_LOA - FS_UPA) } }
166 return 0
167}
168func fs_streq(a: *u8, ao: i64, ae: i64, lit: *u8) -> i64 {
169 let l: i64 = fs_slen(lit)
170 if ae - ao != l { return 0 }
171 var i: i64 = 0
172 while i < l { if a[ao+i] != lit[i] { return 0 } i = i + 1 }
173 return 1
174}
175func fs_prefix(a: *u8, ao: i64, ae: i64, lit: *u8) -> i64 {
176 let l: i64 = fs_slen(lit)
177 if ae - ao < l { return 0 }
178 var i: i64 = 0
179 while i < l { if a[ao+i] != lit[i] { return 0 } i = i + 1 }
180 return 1
181}
182func fs_is_row(buf: *u8, i: i64, le: i64) -> i64 {
183 if le <= i { return 0 }
184 let f: i64 = buf[i] as i64
185 if f == FS_HASH { return 0 }
186 if f == FS_AT { return 0 }
187 if f == FS_NL { return 0 }
188 return 1
189}
190func fs_pipes(buf: *u8, i: i64, le: i64, pos: *i64) -> i64 {
191 var n: i64 = 0
192 var k: i64 = i
193 while k < le {
194 if buf[k] == (FS_PIPE as u8) {
195 if n < FS_MAXP { pos[n] = k; n = n + 1 }
196 }
197 k = k + 1
198 }
199 return n
200}
201func fs_digit_at(buf: *u8, a: i64, b: i64) -> i64 {
202 var i: i64 = a
203 while i < b {
204 let c: i64 = buf[i] as i64
205 if c >= FS_ZERO { if c <= FS_NINE { return c - FS_ZERO } }
206 i = i + 1
207 }
208 return 0 - 1
209}
210func fs_atoi(s: *u8) -> i64 {
211 var v: i64 = 0
212 var any: i64 = 0
213 var i: i64 = 0
214 while s[i] != (0 as u8) {
215 let c: i64 = s[i] as i64
216 if c >= FS_ZERO { if c <= FS_NINE { v = v * 10 + (c - FS_ZERO); any = 1 } }
217 i = i + 1
218 }
219 if any == 0 { return 0 - 1 }
220 return v
221}
222
223// -------------------------------------------------------------------------------------------------
224// CONFIG. Every threshold lives in knowledge/frontier_scan.conf WITH ITS DERIVATION IN THE ROW.
225// A MISSING CONF REFUSES AND NAMES THE FIX -- it never falls back to a number nobody measured, because
226// AN UNMEASURED BUDGET IS NOT CONSERVATIVE JUST BECAUSE IT IS SMALL.
227// -------------------------------------------------------------------------------------------------
228func fs_conf_get(cb: *u8, cn: i64, key: *u8) -> i64 {
229 let pos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
230 var i: i64 = 0
231 while i < cn {
232 let le: i64 = fs_eol(cb, cn, i)
233 if fs_is_row(cb, i, le) == 1 {
234 let np: i64 = fs_pipes(cb, i, le, pos)
235 if np >= 2 {
236 if fs_streq(cb, i, pos[0], key) == 1 {
237 let v: i64 = fs_digit_at(cb, pos[0] + 1, pos[1])
238 if v < 0 { return 0 - 1 }
239 var acc: i64 = 0
240 var q: i64 = pos[0] + 1
241 var any: i64 = 0
242 while q < pos[1] {
243 let c: i64 = cb[q] as i64
244 if c >= FS_ZERO { if c <= FS_NINE { acc = acc * 10 + (c - FS_ZERO); any = 1 } }
245 q = q + 1
246 }
247 if any == 1 { return acc }
248 return 0 - 1
249 }
250 }
251 }
252 i = le + 1
253 }
254 return 0 - 1
255}
256
257// -------------------------------------------------------------------------------------------------
258// TOKEN COVERAGE. "Is this outside title already described by something on our own board?"
259// COVERED when at least cover_permil of its content tokens already appear in the domain's labels, row
260// notes and citation prose. Tokens shorter than min_token are dropped as stopword-class noise.
261// The direction of error is deliberate: a FALSE COVERED silently loses a proposal, a FALSE NEW costs a
262// seat one glance -- so the bar sits where a HALF-described title still counts as new.
263// -------------------------------------------------------------------------------------------------
264func fs_tok_in(hay: *u8, hn: i64, tok: *u8, tl: i64) -> i64 {
265 if tl <= 0 { return 0 }
266 var i: i64 = 0
267 while i <= hn - tl {
268 var j: i64 = 0
269 var m: i64 = 1
270 while j < tl {
271 if fs_fold(hay[i+j] as i64) != (tok[j] as i64) { m = 0; j = tl } else { j = j + 1 }
272 }
273 if m == 1 {
274 var okl: i64 = 1
275 if i > 0 { if fs_fold(hay[i-1] as i64) != 0 { okl = 0 } }
276 if i + tl < hn { if fs_fold(hay[i+tl] as i64) != 0 { okl = 0 } }
277 if okl == 1 { return 1 }
278 }
279 i = i + 1
280 }
281 return 0
282}
283func fs_cover_permil(title: *u8, a: i64, b: i64, corp: *u8, cn: i64, min_token: i64, tok: *u8) -> i64 {
284 var total: i64 = 0
285 var hit: i64 = 0
286 var i: i64 = a
287 var tl: i64 = 0
288 while i <= b {
289 var c: i64 = 0
290 if i < b { c = fs_fold(title[i] as i64) }
291 if c == 0 {
292 if tl >= min_token {
293 total = total + 1
294 if fs_tok_in(corp, cn, tok, tl) == 1 { hit = hit + 1 }
295 }
296 tl = 0
297 } else {
298 if tl < FS_TXTCAP - 1 { tok[tl] = c as u8; tl = tl + 1 }
299 }
300 i = i + 1
301 }
302 // A TITLE WITH NO SCORABLE TOKEN IS UNOBSERVABLE, NOT NEW. Returning full coverage makes it COVERED,
303 // i.e. the instrument abstains instead of manufacturing a proposal out of nothing.
304 if total == 0 { return FS_PERMIL }
305 return (hit * FS_PERMIL) / total
306}
307
308// URL-SAFE SEED, AND THE FIRST CUT OF THIS FUNCTION IS WHY THE FIRST REPLAY MISSED.
309// v1 kept the first maxtok alphanumeric runs of the row label verbatim. On a real board that produced
310// `anim+paired+two+character` and `skin+albedo+driven+by`: the SECTION TAG ("ANIM:", "SKIN:") and the
311// stopwords ("two", "by", "in") went into the query, arXiv matched nothing usable, and -- because the
312// request still returned HTTP 200 with the newest submissions -- the loop happily proposed a Bose-Einstein
313// condensate paper against a skin-shader row. ★★★★★★A SEED THAT MATCHES NOTHING RETURNS A SUCCESSFUL
314// RESPONSE FULL OF THE WRONG THING, AND EVERY DOWNSTREAM FILTER THEN SCORES NOISE HONESTLY.
315// v2 therefore: drop the section tag, drop tokens below min_token, cap at maxtok, and JOIN WITH THE
316// SOURCE'S OWN OPERATOR (arXiv needs +AND+ or it ORs into the whole archive; HN wants plain +).
317func fs_seed_q(dst: *u8, src: *u8, a: i64, b: i64, cap: i64, maxtok: i64, min_token: i64, join: *u8) -> i64 {
318 // A section tag is a short word before a colon at the head of a board label (CREATOR, SKIN, BODY,
319 // ANIM, SIM, WORLD, PLATFORM, MESH, HAIR). FS_SECTION_MAX covers the longest of those with headroom
320 // and is short enough that it can never swallow a sentence that merely contains a colon.
321 var s: i64 = a
322 var k: i64 = a
323 var lim: i64 = a + FS_SECTION_MAX
324 if lim > b { lim = b }
325 while k < lim { if src[k] == (FS_COLON as u8) { s = k + 1; k = lim } else { k = k + 1 } }
326 let jl: i64 = fs_slen(join)
327 var p: i64 = 0
328 var toks: i64 = 0
329 var tl: i64 = 0
330 let tok: *u8 = sys_mmap(FS_TXTCAP)
331 var i: i64 = s
332 var stop: i64 = 0
333 while i <= b {
334 if stop == 0 {
335 var c: i64 = 0
336 if i < b { c = fs_fold(src[i] as i64) }
337 if c == 0 {
338 if tl >= min_token {
339 if p + tl + jl < cap - 2 {
340 if toks > 0 { var jj: i64 = 0; while jj < jl { dst[p] = join[jj]; p = p + 1; jj = jj + 1 } }
341 var m: i64 = 0
342 while m < tl { dst[p] = tok[m]; p = p + 1; m = m + 1 }
343 toks = toks + 1
344 if toks >= maxtok { stop = 1 }
345 }
346 }
347 tl = 0
348 } else {
349 if tl < FS_TXTCAP - 1 { tok[tl] = c as u8; tl = tl + 1 }
350 }
351 }
352 i = i + 1
353 }
354 dst[p] = 0 as u8
355 return p
356}
357
358// Stable 32-bit id for a url, printed as 8 hex digits. THIS IS WHAT MAKES THE PLANE PUT IDEMPOTENT:
359// the same proposal re-scanned tomorrow upserts its own row instead of growing a duplicate every beat.
360func fs_urlhash(buf: *u8, a: i64, b: i64) -> i64 {
361 var h: i64 = 0
362 var i: i64 = a
363 while i < b { h = ((h * FS_HASH_BASE) + (buf[i] as i64)) & FS_HASH_MASK; i = i + 1 }
364 return h
365}
366func fs_hex(dst: *u8, pos: i64, v: i64) -> i64 {
367 let digits: *u8 = "0123456789abcdef" as *u8
368 var p: i64 = pos
369 var k: i64 = FS_HEXDIG - 1
370 while k >= 0 {
371 let nib: i64 = (v / (1 << (FS_HEXSHIFT * k))) % FS_HEXRADIX
372 dst[p] = digits[nib]
373 p = p + 1
374 k = k - 1
375 }
376 return p
377}
378
379// A <domain> CONTAINING A SLASH IS A PATH PREFIX, NOT A DOMAIN NAME. That one branch is what lets the
380// gate assemble its fixtures under /tmp/<gate>/ (a gate must not share its fixture with a production
381// beat) and lets the metahuman REPLAY reconstruct a historical board WITHOUT writing anything into the
382// live knowledge/compare tree, where nx_domain_admit --all and the regen census would both consume it.
383func fs_has_slash(s: *u8) -> i64 {
384 var i: i64 = 0
385 while s[i] != (0 as u8) { if s[i] == (FS_SLASH as u8) { return 1 } i = i + 1 }
386 return 0
387}
388func fs_basename(s: *u8) -> i64 {
389 var i: i64 = 0
390 var last: i64 = 0 - 1
391 while s[i] != (0 as u8) { if s[i] == (FS_SLASH as u8) { last = i } i = i + 1 }
392 return last + 1
393}
394func fs_cmp_path(dst: *u8, dom: *u8, ext: *u8) -> i64 {
395 var p: i64 = 0
396 if fs_has_slash(dom) == 0 { p = fs_cat(dst, 0, "buildroot/knowledge/compare/" as *u8) }
397 p = fs_cat(dst, p, dom)
398 p = fs_cat(dst, p, ext)
399 dst[p] = 0 as u8
400 return p
401}
402
403// -------------------------------------------------------------------------------------------------
404// SEEDS. .axes field 4 first, then matrix labels ALWAYS. Emitting the SOURCE of each seed is the point:
405// a seed nobody can trace back to the board is a hand-typed seed wearing a data costume.
406// -------------------------------------------------------------------------------------------------
407func fs_emit_seeds(dom: *u8) -> i64 {
408 let path: *u8 = sys_mmap(FS_PATHCAP)
409 let pos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
410 let alen: *i64 = sys_mmap(16) as *i64
411 var seeds: i64 = 0
412
413 // .axes NOW RESOLVES ACROSS BOTH COMPARE TREES (2026-09-01), STRICTLY ADDITIVELY.
414 // The original single read is kept EXACTLY as it was and runs FIRST, so no case that worked before
415 // can change; the two-tree resolver is consulted ONLY when that read missed. This can turn an ABSENT
416 // into a FOUND and never a FOUND into anything else -- the same shape da_read_2dir uses one organ over.
417 // A SLASH IN dom IS A LITERAL FIXTURE PATH (the gate passes /tmp/...), and it must never be redirected
418 // into a knowledge tree, so the fallback is gated on a bare domain name -- the same discrimination
419 // fs_cmp_path already makes.
420 // AUTHORED ORDER: .axes is hand-written and rendered by no page, so the bare knowledge tree is asked
421 // first. ct_readall_2dir sizes from the file, so there is no cap to guess and no silent truncation.
422 // The reported path is rebuilt from PRIMARY rather than assumed: the buildroot read above has already
423 // missed, and secondary IS buildroot, so a hit here can only have come from primary.
424 let awhich: *i64 = sys_mmap(16) as *i64
425 awhich[0] = CT_TREE_NONE
426 fs_cmp_path(path, dom, ".axes" as *u8)
427 var ab: *u8 = sys_read_file(path, alen)
428 if fs_has_slash(dom) == 0 {
429 if (ab as i64) == 0 {
430 ab = ct_readall_2dir(ct_first_authored(), ct_second_authored(), dom, ".axes" as *u8, alen, awhich)
431 if (ab as i64) != 0 {
432 var q: i64 = fs_cat(path, 0, CT_DIR_PRIMARY)
433 q = fs_cat(path, q, dom)
434 q = fs_cat(path, q, ".axes" as *u8)
435 path[q] = 0 as u8
436 }
437 }
438 }
439 if (ab as i64) != 0 {
440 let an: i64 = alen[0]
441 if an > 0 {
442 fs_w("SEED-SOURCE axes " as *u8); fs_w(path); fs_w("\n" as *u8)
443 var i: i64 = 0
444 while i < an {
445 let le: i64 = fs_eol(ab, an, i)
446 if fs_is_row(ab, i, le) == 1 {
447 let np: i64 = fs_pipes(ab, i, le, pos)
448 if np >= 3 {
449 var s: i64 = pos[2] + 1
450 var k: i64 = s
451 while k <= le {
452 var cut: i64 = 0
453 if k == le { cut = 1 } else { if ab[k] == (FS_SEMI as u8) { cut = 1 } }
454 if cut == 1 {
455 if k > s { fs_w("SEED axes " as *u8); fs_wn(ab, s, k - s); fs_w("\n" as *u8); seeds = seeds + 1 }
456 s = k + 1
457 }
458 k = k + 1
459 }
460 }
461 }
462 i = le + 1
463 }
464 }
465 } else { fs_w("SEED-SOURCE axes ABSENT (matrix labels carry the whole seed set for this domain)\n" as *u8) }
466
467 fs_cmp_path(path, dom, ".matrix" as *u8)
468 let mlen: *i64 = sys_mmap(16) as *i64
469 let mb: *u8 = sys_read_file(path, mlen)
470 if (mb as i64) == 0 { fs_w("SEEDS-REFUSED no-matrix " as *u8); fs_w(path); fs_w("\n" as *u8); return 0 - 1 }
471 let mn: i64 = mlen[0]
472 fs_w("SEED-SOURCE matrix " as *u8); fs_w(path); fs_w("\n" as *u8)
473 var j: i64 = 0
474 while j < mn {
475 let le2: i64 = fs_eol(mb, mn, j)
476 if fs_is_row(mb, j, le2) == 1 {
477 let np2: i64 = fs_pipes(mb, j, le2, pos)
478 if np2 >= 1 {
479 fs_w("SEED matrix " as *u8); fs_wn(mb, j, pos[0] - j); fs_w("\n" as *u8)
480 seeds = seeds + 1
481 }
482 }
483 j = le2 + 1
484 }
485 fs_w("SEEDS total=" as *u8); fs_n(seeds); fs_w("\n" as *u8)
486 return seeds
487}
488
489// -------------------------------------------------------------------------------------------------
490// CAPABILITY RANK FOR A NOMINAL CODE (2026-09-01) -- the fix for a comparison that contradicted its
491// own conf's stated derivation.
492// The matrix codebook is 0=No 1=Yes 2=Best 3=Part. That is NOMINAL, not ordinal: Part is a WEAKER
493// capability than Yes, but it is a LARGER integer. fs_deficit compared the raw code with
494// `c >= lead_code`, so at the shipped lead_code=2 a rival coded 3 (Part) COUNTED AS LEADING while a
495// rival coded 1 (Yes) did NOT -- the exact inverse of what knowledge/frontier_scan.conf says that key
496// means: "2 is exactly the statement a rival is Best at this".
497// WORSE THAN A MIS-SCORE, IT IS A BLIND SPOT: with no rival coded 2 or 3, `leads` stays 0 and the row
498// is never a deficit at all -- so the single clearest gap class, EVERY RIVAL FULLY HAS THIS AND WE DO
499// NOT (all rivals coded 1), was invisible to the estate's only autonomous frontier-noticer.
500// MEASURED CONSEQUENCE on the live search board: is_shard -- where Mojeek's own pinned mirror records
501// nine billion pages, coded 1=Yes -- ranked BELOW hs_render, where two rivals are merely Part.
502// FIX: rank by CAPABILITY STRENGTH and compare ranks, so the conf key keeps its declared meaning and
503// any future value of lead_code also means what it says. No(0)=0 Part(3)=1 Yes(1)=2 Best(2)=3.
504// SCOPE HELD DELIBERATELY NARROW: only the LEAD test changes. The score line below is left alone --
505// widening the scoring policy in the same edit as a correctness fix would make the two impossible to
506// tell apart in the next measurement.
507// ★A NUMERIC COMPARISON ON A NOMINAL CODE IS A BUG WEARING ARITHMETIC.
508// -------------------------------------------------------------------------------------------------
509const FS_CODE_PART: i64 = 3
510func fs_code_rank(c: i64) -> i64 {
511 if c == FS_CODE_BEST { return 3 }
512 if c == FS_CODE_YES { return 2 }
513 if c == FS_CODE_PART { return 1 }
514 return 0
515}
516
517// -------------------------------------------------------------------------------------------------
518// DEFICIT. THE REASON TRAVELS WITH THE COUNT: every row prints how many rivals lead and what our own
519// symbol is, so the worklist is triageable without re-running anything.
520// -------------------------------------------------------------------------------------------------
521func fs_deficit(mb: *u8, mn: i64, lead_code: i64, rowa: *i64, rowb: *i64, score: *i64, verbose: i64) -> i64 {
522 let pos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
523 var found: i64 = 0
524 var rows: i64 = 0
525 var i: i64 = 0
526 while i < mn {
527 let le: i64 = fs_eol(mb, mn, i)
528 if fs_is_row(mb, i, le) == 1 {
529 let np: i64 = fs_pipes(mb, i, le, pos)
530 if np >= 8 {
531 rows = rows + 1
532 let exceed: i64 = fs_digit_at(mb, pos[2] + 1, pos[3])
533 var absent: i64 = 0
534 if fs_prefix(mb, pos[1] + 1, pos[2], "_ABSENT_" as *u8) == 1 { absent = 1 }
535 var sc: i64 = 0
536 var leads: i64 = 0
537 var k: i64 = 0
538 while k < FS_RIVALS {
539 let c: i64 = fs_digit_at(mb, pos[3+k] + 1, pos[4+k])
540 if fs_code_rank(c) >= fs_code_rank(lead_code) { leads = leads + 1; sc = sc + FS_CODE_BEST }
541 if c == FS_CODE_YES { sc = sc + FS_CODE_YES }
542 k = k + 1
543 }
544 var deficit: i64 = 0
545 if exceed != 1 { if leads > 0 { deficit = 1 } }
546 if deficit == 1 {
547 if absent == 1 { sc = sc + FS_CODE_BEST }
548 if found < FS_MAXROWS {
549 rowa[found] = i
550 rowb[found] = pos[0]
551 score[found] = sc
552 found = found + 1
553 }
554 if verbose == 1 {
555 fs_w("DEFICIT score=" as *u8); fs_n(sc)
556 fs_w(" rivals_leading=" as *u8); fs_n(leads)
557 fs_w(" our_symbol=" as *u8); fs_wn(mb, pos[1] + 1, pos[2] - (pos[1] + 1))
558 fs_w(" row=" as *u8); fs_wn(mb, i, pos[0] - i)
559 fs_w("\n" as *u8)
560 }
561 }
562 }
563 }
564 i = le + 1
565 }
566 var a: i64 = 0
567 while a < found {
568 var best: i64 = a
569 var b: i64 = a + 1
570 while b < found { if score[b] > score[best] { best = b } b = b + 1 }
571 if best != a {
572 let ts: i64 = score[a]; score[a] = score[best]; score[best] = ts
573 let ta: i64 = rowa[a]; rowa[a] = rowa[best]; rowa[best] = ta
574 let tb: i64 = rowb[a]; rowb[a] = rowb[best]; rowb[best] = tb
575 }
576 a = a + 1
577 }
578 if verbose == 1 {
579 fs_w("DEFICIT-PARTITION rows_total=" as *u8); fs_n(rows)
580 fs_w(" deficit=" as *u8); fs_n(found)
581 fs_w(" not_deficit=" as *u8); fs_n(rows - found)
582 fs_w(" (deficit plus not_deficit equals rows_total)\n" as *u8)
583 }
584 return found
585}
586
587// -------------------------------------------------------------------------------------------------
588// FETCH ONE SOURCE by COMPOSING nx_research_fetch -- the estate's ONE fetcher, which mirrors into
589// knowledge/fetched and can be pinned by nx_filehash. TLS is never re-implemented here.
590// Writes the saved raw path into savepath; returns its length, or -1.
591// -------------------------------------------------------------------------------------------------
592func fs_fetch(url: *u8, tmpout: *u8, savepath: *u8, olen: *i64) -> i64 {
593 let args: *i64 = sys_mmap(32) as *i64
594 args[0] = url as i64
595 var rc: i64 = dep_run_capture("./nx_research_fetch.elf" as *u8, args, 1, tmpout)
596 if rc == 127 { rc = dep_run_capture("_offc/nx_research_fetch.elf" as *u8, args, 1, tmpout) }
597 let ob: *u8 = sys_read_file(tmpout, olen)
598 if (ob as i64) == 0 { return 0 - 1 }
599 let on: i64 = olen[0]
600 let s: i64 = fs_find(ob, on, 0, "SAVED " as *u8)
601 if s < 0 { return 0 - 1 }
602 let p: i64 = s + 6
603 // separate cursor + explicit flag: A LOOP THAT BREAKS BY CLOBBERING ITS OWN CURSOR CANNOT ALSO REPORT
604 // WHERE IT STOPPED, and the answer here IS where it stopped.
605 var q: i64 = p
606 var end: i64 = p
607 var stop: i64 = 0
608 while stop == 0 {
609 if q >= on { end = q; stop = 1 } else {
610 let c: i64 = ob[q] as i64
611 if c == FS_SPACE { end = q; stop = 1 } else {
612 if c == FS_NL { end = q; stop = 1 } else { q = q + 1 }
613 }
614 }
615 }
616 let n: i64 = fs_catn(savepath, 0, ob, p, end)
617 savepath[n] = 0 as u8
618 return n
619}
620
621// -------------------------------------------------------------------------------------------------
622// EXTRACT + DIFF for one source over one seed. Prints proposals; returns how many it emitted.
623// Partition printed and summed: seen = already_cited + covered_by_board + proposed + over_row_cap + unpaired.
624// EVERY FIELD IS SEARCHED INSIDE ITS OWN ITEM'S EXTENT, never to the end of the buffer. v1 did the latter,
625// so an item missing a field silently borrowed the NEXT item's -- see fs_item_end and the block below.
626// -------------------------------------------------------------------------------------------------
627func fs_scan_source(dom: *u8, kind: *u8, url: *u8, idlm: *u8, topen: *u8, tclose: *u8,
628 uopen: *u8, uclose: *u8, corp: *u8, corpn: i64, refs: *u8, refsn: i64,
629 label: *u8, la: i64, lb: i64, max_items: i64, cover_permil: i64, min_token: i64,
630 wl: *u8, wlout: *i64, tok: *u8, savepath: *u8, olen: *i64, rlen: *i64) -> i64 {
631 let tmpout: *u8 = "/tmp/nx_frontier_scan/fetch.out" as *u8
632 let got: i64 = fs_fetch(url, tmpout, savepath, olen)
633 if got <= 0 {
634 fs_w(" SOURCE-UNREACHABLE kind=" as *u8); fs_w(kind)
635 fs_w(" url=" as *u8); fs_w(url)
636 fs_w(" (UNOBSERVABLE, not zero -- an axis that cannot see must abstain, not acquit)\n" as *u8)
637 return 0
638 }
639 let rb: *u8 = sys_read_file(savepath, rlen)
640 if (rb as i64) == 0 {
641 fs_w(" SOURCE-UNREADABLE mirror=" as *u8); fs_w(savepath); fs_w("\n" as *u8)
642 return 0
643 }
644 let rn: i64 = rlen[0]
645 fs_w(" SOURCE kind=" as *u8); fs_w(kind)
646 fs_w(" mirror=" as *u8); fs_w(savepath)
647 fs_w(" bytes=" as *u8); fs_n(rn); fs_w("\n" as *u8)
648
649 var seen: i64 = 0
650 var cited: i64 = 0
651 var covered: i64 = 0
652 var proposed: i64 = 0
653 var overcap: i64 = 0
654 var unpaired: i64 = 0
655 var wp: i64 = wlout[0]
656 var cur: i64 = 0
657 var go: i64 = 1
658 while go == 1 {
659 if seen >= max_items { go = 0 } else {
660 let it: i64 = fs_find(rb, rn, cur, idlm)
661 if it < 0 { go = 0 } else {
662 // ---- ITEM EXTENT: THE FIX (2026-08-20, lane E) ----------------------------------------
663 // ★★★★★★A PROPOSAL WHOSE URL DOES NOT BELONG TO ITS TITLE IS A FABRICATED CITATION IN THE
664 // MAKING -- the same defect class this lane spent a day retracting off /compare/charsim,
665 // caught one step UPSTREAM, before it can ever reach a refs row.
666 // v1 searched every field from the item delimiter TO THE END OF THE BUFFER, so an item
667 // that lacks a field silently borrowed the next item's. MEASURED LIVE on HN Algolia:
668 // Ask HN and Launch HN text posts carry NO "url" key at all (the mirror holds story_text
669 // instead), and TWO ADJACENT url-less items both reported the SAME downstream url under
670 // DIFFERENT titles. The bound is the item's own end; a field absent from it makes the
671 // item UNPAIRED, which is its own bucket -- never a borrowed neighbour.
672 // fs_find's own contract does the bounding for free: it requires the WHOLE literal inside
673 // [0,n), so passing the item end as n cannot match a literal straddling the boundary.
674 let ie: i64 = fs_item_end(rb, rn, it, idlm)
675 let ts: i64 = fs_find(rb, ie, it, topen)
676 let us: i64 = fs_find(rb, ie, it, uopen)
677 if ts < 0 { seen = seen + 1; unpaired = unpaired + 1; cur = it + fs_slen(idlm) } else {
678 if us < 0 { seen = seen + 1; unpaired = unpaired + 1; cur = it + fs_slen(idlm) } else {
679 let ta: i64 = ts + fs_slen(topen)
680 let te: i64 = fs_find(rb, ie, ta, tclose)
681 let ua: i64 = us + fs_slen(uopen)
682 let ue: i64 = fs_find(rb, ie, ua, uclose)
683 if te < 0 { seen = seen + 1; unpaired = unpaired + 1; cur = it + fs_slen(idlm) } else {
684 if ue < 0 { seen = seen + 1; unpaired = unpaired + 1; cur = it + fs_slen(idlm) } else {
685 seen = seen + 1
686 cur = it + fs_slen(idlm)
687 // ALREADY CITED? the refs file carries the url verbatim in field 4.
688 var already: i64 = 0
689 if refsn > 0 {
690 let ul: i64 = ue - ua
691 var z: i64 = 0
692 var hit: i64 = 0
693 while z <= refsn - ul {
694 var j: i64 = 0
695 var m: i64 = 1
696 while j < ul { if refs[z+j] != rb[ua+j] { m = 0; j = ul } else { j = j + 1 } }
697 if m == 1 { hit = 1; z = refsn }
698 z = z + 1
699 }
700 already = hit
701 }
702 if already == 1 { cited = cited + 1 } else {
703 let cov: i64 = fs_cover_permil(rb, ta, te, corp, corpn, min_token, tok)
704 if cov >= cover_permil { covered = covered + 1 } else {
705 if proposed >= FS_PROPS_PER_ROW { overcap = overcap + 1 } else {
706 proposed = proposed + 1
707 fs_w(" PROPOSAL domain=" as *u8); fs_w(dom)
708 fs_w(" kind=" as *u8); fs_w(kind)
709 fs_w(" cover_permil=" as *u8); fs_n(cov)
710 fs_w("\n" as *u8)
711 fs_w(" challenges_row: " as *u8); fs_wn(label, la, lb - la); fs_w("\n" as *u8)
712 fs_w(" title: " as *u8); fs_wn(rb, ta, te - ta); fs_w("\n" as *u8)
713 fs_w(" url: " as *u8); fs_wn(rb, ua, ue - ua); fs_w("\n" as *u8)
714 fs_w(" mirror: " as *u8); fs_w(savepath); fs_w("\n" as *u8)
715 wp = fs_cat(wl, wp, "PROPOSAL|" as *u8)
716 wp = fs_cat(wl, wp, dom)
717 wp = fs_cat(wl, wp, "|" as *u8)
718 wp = fs_catn(wl, wp, label, la, lb)
719 wp = fs_cat(wl, wp, "|" as *u8)
720 wp = fs_catn(wl, wp, rb, ta, te)
721 wp = fs_cat(wl, wp, "|" as *u8)
722 wp = fs_catn(wl, wp, rb, ua, ue)
723 wp = fs_cat(wl, wp, "|" as *u8)
724 wp = fs_cat(wl, wp, savepath)
725 wp = fs_cat(wl, wp, "|" as *u8)
726 wp = fs_cat(wl, wp, kind)
727 wp = fs_cat(wl, wp, "|OPEN" as *u8)
728 wl[wp] = FS_NL as u8
729 wp = wp + 1
730 }
731 }
732 }
733 }
734 }
735 }
736 }
737 }
738 }
739 }
740 fs_w(" ITEMS seen=" as *u8); fs_n(seen)
741 fs_w(" already_cited=" as *u8); fs_n(cited)
742 fs_w(" covered_by_board=" as *u8); fs_n(covered)
743 fs_w(" proposed=" as *u8); fs_n(proposed)
744 fs_w(" over_row_cap=" as *u8); fs_n(overcap)
745 fs_w(" unpaired=" as *u8); fs_n(unpaired)
746 fs_w(" (the five buckets sum to seen)\n" as *u8)
747 wlout[0] = wp
748 return proposed
749}
750
751// =================================================================================================
752// ATTEST (2026-09-06). Operator: "use and upgrade the nishi research fetch system and expand its fan out
753// ... so siggraph 2026 and all those other state of the art research areas like github gitea etc all get
754// evaluated." Two legs over the SAME source rows the scan reads (a DATED row carries two more fields:
755// date-open and date-close, or month=YYYY-MM in date-open when a listing has one month for every item,
756// as a conference index does):
757// ATTEST LEG re-fetch the listing each sotabar| row cites (its ref's url), read the newest month in it,
758// append barscan|<today>|<ref>|newest=<YYYY-MM> to the plan -- idempotent per date and ref --
759// so nx_barfresh reads ATTESTED (or STALE when the listing moved) from the estate's own
760// re-reading: "nothing newer this month" is re-earned by a beat, never remembered by a seat.
761// FAN-OUT LEG ask every OTHER dated source the bar's subject, print NEWER / SAME / OLDER / UNDATED per
762// source with the newest item named, and write the NEWER items to a worklist a seat adjudicates.
763// STILL ADMITS NOTHING: a barscan row records that a listing was fetched and read and what its newest
764// month was; the claim about what that month SAYS stays the sotabar| row a seat writes from the worklist.
765// A STATIC listing (no {q} in its url) is FILTERED BY THE BAR'S SUBJECT: an item sharing no content token
766// with the subject is not evidence about it, so a 283-paper conference index reads as the papers on the
767// subject; it is fan-out only, the scan verb never proposes from it (see main).
768// UNDATED is its own answer, never zero: a listing whose items carry no readable date cannot attest a month.
769// =================================================================================================
770const FS_DASH: i64 = 45
771const FS_CR: i64 = 13
772const FS_MONTH_TAG: *u8 = "month="
773const FS_MONTH_TAG_LEN: i64 = 6
774const FS_DATED_PIPES: i64 = 10 // src|kind|url|item|topen|tclose|uopen|uclose|join|dopen|dclose
775const FS_ATT_C_BARS: i64 = 0
776const FS_ATT_C_APPENDED: i64 = 1
777const FS_ATT_C_ALREADY: i64 = 2
778const FS_ATT_C_UNATTESTABLE: i64 = 3
779const FS_ATT_C_FETCHES: i64 = 4
780const FS_ATT_C_NEWER: i64 = 5
781const FS_ATT_C_STALE: i64 = 6
782const FS_ATT_C_N: i64 = 8
783const FS_YMBUF: i64 = 16
784const FS_TODAYBUF: i64 = 32
785
786// the month index of the YYYY-MM at rb[a..b), or -1 -- ONE reader, the bar ruler's
787func fs_ym_at(rb: *u8, a: i64, b: i64) -> i64 {
788 if b - a < BF_YM_LEN { return 0 - 1 }
789 let v: i64 = bf_parse_ym(((rb as i64) + a) as *u8, b - a)
790 if v == BF_NONE { return 0 - 1 }
791 return v
792}
793// month=YYYY-MM in a date-open field -> that month; anything else -> -1 (read the item's own date)
794func fs_fixed_month(spec: *u8) -> i64 {
795 let n: i64 = fs_slen(spec)
796 if fs_prefix(spec, 0, n, FS_MONTH_TAG) == 0 { return 0 - 1 }
797 return fs_ym_at(spec, FS_MONTH_TAG_LEN, n)
798}
799func fs_w_ym(b: *u8, ix: i64) -> i64 { bf_ym_write(b, 0, ix); fs_w(b); return 0 }
800func fs_catn_num(dst: *u8, pos: i64, v: i64) -> i64 {
801 var p: i64 = pos
802 if v == 0 { dst[p] = FS_ZERO as u8; return p + 1 }
803 var m: i64 = v
804 if m < 0 { dst[p] = FS_DASH as u8; p = p + 1; m = 0 - m }
805 let tmp: *u8 = sys_mmap(FS_TODAYBUF)
806 var k: i64 = 0
807 while m > 0 { tmp[k] = (FS_ZERO + (m - (m / 10) * 10)) as u8; m = m / 10; k = k + 1 }
808 while k > 0 { k = k - 1; dst[p] = tmp[k]; p = p + 1 }
809 return p
810}
811// today as YYYY-MM-DD through the estate's civil date -- the barscan row's date field
812func fs_today(dst: *u8) -> i64 {
813 let ts: *i64 = sys_mmap(BF_CLOCK_WORDS * BF_I64) as *i64
814 ts[0] = 0
815 sys_clock_gettime_real(ts)
816 let y: *i64 = sys_mmap(BF_I64) as *i64
817 let m: *i64 = sys_mmap(BF_I64) as *i64
818 let d: *i64 = sys_mmap(BF_I64) as *i64
819 civil_from_days(ts[0] / BF_SECS_PER_DAY, y, m, d)
820 var o: i64 = bf_ym_write(dst, 0, bf_ym_index(y[0], m[0]))
821 dst[o] = FS_DASH as u8; o = o + 1
822 dst[o] = (d[0] / 10 + FS_ZERO) as u8; o = o + 1
823 dst[o] = (d[0] - (d[0] / 10) * 10 + FS_ZERO) as u8; o = o + 1
824 dst[o] = 0 as u8
825 return o
826}
827// does the item [it,ie) mention ANY content token (min_token or longer) of the subject subj[so..so+sn)?
828func fs_item_mentions(rb: *u8, it: i64, ie: i64, subj: *u8, so: i64, sn: i64, min_token: i64, tok: *u8) -> i64 {
829 let hay: *u8 = ((rb as i64) + it) as *u8
830 let hn: i64 = ie - it
831 var tl: i64 = 0
832 var i: i64 = so
833 while i <= so + sn {
834 var c: i64 = 0
835 if i < so + sn { c = fs_fold(subj[i] as i64) }
836 if c == 0 {
837 if tl >= min_token { if fs_tok_in(hay, hn, tok, tl) == 1 { return 1 } }
838 tl = 0
839 } else { if tl < FS_TXTCAP - 1 { tok[tl] = c as u8; tl = tl + 1 } }
840 i = i + 1
841 }
842 return 0
843}
844// a field (open..close) INSIDE the item's own extent: its length with off[0]=start, or -1
845func fs_item_field(rb: *u8, it: i64, ie: i64, open: *u8, close: *u8, off: *i64) -> i64 {
846 let s: i64 = fs_find(rb, ie, it, open)
847 if s < 0 { return 0 - 1 }
848 let a: i64 = s + fs_slen(open)
849 let e: i64 = fs_find(rb, ie, a, close)
850 if e < 0 { return 0 - 1 }
851 off[0] = a
852 return e - a
853}
854// THE MONTH READER. Walks the items of a mirrored listing (each bounded by its own extent), reads the date
855// between dopen and dclose -- or takes the FIXED month when the source declares one -- and returns the
856// newest month index, or -1 when no item could be dated (UNDATED, never the epoch). With sn > 0 only items
857// mentioning the subject count (a static listing is filtered by the bar's subject).
858// cnt[0]=items seen, cnt[1]=items dated, cnt[2]=offset of the newest item (-1 if none), cnt[3..4]=its date span.
859func fs_newest_month(rb: *u8, rn: i64, idlm: *u8, dopen: *u8, dclose: *u8, fixed: i64, max_items: i64,
860 subj: *u8, so: i64, sn: i64, min_token: i64, tok: *u8, cnt: *i64) -> i64 {
861 var best: i64 = 0 - 1
862 var seen: i64 = 0
863 var dated: i64 = 0
864 var cur: i64 = 0
865 var go: i64 = 1
866 cnt[2] = 0 - 1; cnt[3] = 0; cnt[4] = 0
867 while go == 1 {
868 if seen >= max_items { go = 0 } else {
869 let it: i64 = fs_find(rb, rn, cur, idlm)
870 if it < 0 { go = 0 } else {
871 let ie: i64 = fs_item_end(rb, rn, it, idlm)
872 cur = it + fs_slen(idlm)
873 var take: i64 = 1
874 if sn > 0 { take = fs_item_mentions(rb, it, ie, subj, so, sn, min_token, tok) }
875 if take == 1 {
876 seen = seen + 1
877 var ym: i64 = 0 - 1
878 var da: i64 = 0
879 var de: i64 = 0
880 if fixed >= 0 { ym = fixed } else {
881 let ds: i64 = fs_find(rb, ie, it, dopen)
882 if ds >= 0 {
883 da = ds + fs_slen(dopen)
884 de = fs_find(rb, ie, da, dclose)
885 if de > da { ym = fs_ym_at(rb, da, de) }
886 }
887 }
888 if ym >= 0 {
889 dated = dated + 1
890 if ym > best { best = ym; cnt[2] = it; cnt[3] = da; cnt[4] = de }
891 }
892 }
893 }
894 }
895 }
896 cnt[0] = seen
897 cnt[1] = dated
898 return best
899}
900// Append ONE barscan row to a plan, idempotent per (date, ref): the same attestation re-run the same day is a
901// no-op, so a beat cannot grow a plan by a row per tick. 1 appended, 0 already present, -1 unwritable.
902func fs_attest_row(plan: *u8, refkey: *u8, today: *u8, ym: i64, note: *u8) -> i64 {
903 let plen: *i64 = sys_mmap(16) as *i64
904 let pb: *u8 = sys_read_file(plan, plen)
905 if (pb as i64) == 0 { return 0 - 1 }
906 let pn: i64 = plen[0]
907 let key: *u8 = sys_mmap(FS_TXTCAP)
908 var k: i64 = fs_cat(key, 0, BF_SCAN_TAG)
909 k = fs_cat(key, k, today)
910 k = fs_cat(key, k, "|" as *u8)
911 k = fs_cat(key, k, refkey)
912 k = fs_cat(key, k, "|" as *u8)
913 key[k] = 0 as u8
914 if fs_find(pb, pn, 0, key) >= 0 { return 0 }
915 let row: *u8 = sys_mmap(FS_LINECAP)
916 var r: i64 = fs_cat(row, 0, key)
917 r = fs_cat(row, r, BF_NEWEST_TAG)
918 r = bf_ym_write(row, r, ym)
919 r = fs_cat(row, r, "|" as *u8)
920 r = fs_cat(row, r, note)
921 row[r] = FS_NL as u8; r = r + 1
922 let fd: i64 = sys_openat_append(plan, MODE_0644)
923 if fd < 0 { return 0 - 1 }
924 // a plan whose last byte is not a newline would glue this row onto its previous line
925 if pn > 0 { if pb[pn - 1] != (FS_NL as u8) { sys_write(fd, "\n" as *u8, 1) } }
926 sys_write(fd, row, r)
927 sys_close(fd)
928 return 1
929}
930// ref|<key>|<cite>|<url>|... -> the url span of the row whose key matches; length with off[0]=start, or -1
931func fs_ref_url(refs: *u8, refsn: i64, key: *u8, klen: i64, off: *i64) -> i64 {
932 let pos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
933 var i: i64 = 0
934 while i < refsn {
935 let le: i64 = fs_eol(refs, refsn, i)
936 if fs_is_row(refs, i, le) == 1 {
937 let np: i64 = fs_pipes(refs, i, le, pos)
938 if np >= 4 { if fs_streq(refs, i, pos[0], "ref" as *u8) == 1 {
939 if pos[1] - (pos[0] + 1) == klen {
940 var m: i64 = 1
941 var j: i64 = 0
942 while j < klen { if refs[pos[0] + 1 + j] != key[j] { m = 0; j = klen } else { j = j + 1 } }
943 if m == 1 { off[0] = pos[2] + 1; return pos[3] - (pos[2] + 1) }
944 }
945 } }
946 }
947 i = le + 1
948 }
949 return 0 - 1
950}
951// the nine fields of a DATED source row into NUL-terminated buffers (spos from fs_pipes, sle the row end)
952func fs_src_fields(sb: *u8, spos: *i64, sle: i64, kind: *u8, idlm: *u8, topen: *u8, tclos: *u8, uopen: *u8, uclos: *u8, join: *u8, dopen: *u8, dclos: *u8) -> i64 {
953 var q: i64 = fs_catn(kind, 0, sb, spos[0] + 1, spos[1]); kind[q] = 0 as u8
954 q = fs_catn(idlm, 0, sb, spos[2] + 1, spos[3]); idlm[q] = 0 as u8
955 q = fs_catn(topen, 0, sb, spos[3] + 1, spos[4]); topen[q] = 0 as u8
956 q = fs_catn(tclos, 0, sb, spos[4] + 1, spos[5]); tclos[q] = 0 as u8
957 q = fs_catn(uopen, 0, sb, spos[5] + 1, spos[6]); uopen[q] = 0 as u8
958 q = fs_catn(uclos, 0, sb, spos[6] + 1, spos[7]); uclos[q] = 0 as u8
959 q = fs_catn(join, 0, sb, spos[7] + 1, spos[8]); join[q] = 0 as u8
960 q = fs_catn(dopen, 0, sb, spos[8] + 1, spos[9]); dopen[q] = 0 as u8
961 q = fs_catn(dclos, 0, sb, spos[9] + 1, sle); dclos[q] = 0 as u8
962 return 0
963}
964// substitute {q} in the template sb[a..b) with qbuf into url; returns 1 when the template had a slot, 0 (static) otherwise
965func fs_build_url(url: *u8, sb: *u8, a: i64, b: i64, qbuf: *u8) -> i64 {
966 var up: i64 = 0
967 var t: i64 = a
968 var had: i64 = 0
969 while t < b {
970 var sub: i64 = 0
971 if sb[t] == (FS_LBRACE as u8) { if fs_prefix(sb, t, b, "{q}" as *u8) == 1 { sub = 1 } }
972 if sub == 1 { up = fs_cat(url, up, qbuf); t = t + 3; had = 1 } else { url[up] = sb[t]; up = up + 1; t = t + 1 }
973 }
974 url[up] = 0 as u8
975 return had
976}
977// A review row is assembled separately so any refusal preserves prior worklist bytes.
978func fs_review_span(row:*NxBufOwned,src:*u8,n:i64)->i64 {
979 if n<0{return NX_BO_INVALID};if n==0{return 0}
980 let p:i64=src as i64;if p<=0||n>NX_BO_I64_MAX-p{return NX_BO_RANGE}
981 let clean:*u8=sys_mmap_try(n);if (clean as i64)<=0{return NX_BO_ALLOCATION}
982 var i:i64=0;while i<n{let c:u8=src[i];if c==124 as u8||c==10 as u8||c==13 as u8||c==0 as u8{clean[i]=32 as u8}else{clean[i]=c};i=i+1}
983 let rc:i64=nx_bo_append(row,clean,n,0);sys_munmap_direct(clean,n);return rc
984}
985func fs_review_newer(wl:*NxBufOwned,dbase:*u8,pb:*u8,ido:i64,idl:i64,kind:*u8,ym:i64,rb:*u8,rn:i64,bit:i64,idlm:*u8,topen:*u8,tclos:*u8,uopen:*u8,uclos:*u8,mirror:*u8)->i64 {
986 if bit<0||bit>=rn||ido<0||idl<0{return NX_BO_INVALID}
987 let fo:*i64=sys_mmap(16) as *i64
988 let ie:i64=fs_item_end(rb,rn,bit,idlm)
989 let tl:i64=fs_item_field(rb,bit,ie,topen,tclos,fo);let to:i64=fo[0]
990 let ul:i64=fs_item_field(rb,bit,ie,uopen,uclos,fo);let uo:i64=fo[0]
991 let month:*u8=sys_mmap(FS_YMBUF);let mn:i64=bf_ym_write(month,0,ym)
992 let row:*NxBufOwned=sys_mmap(NX_BO_BYTES) as *NxBufOwned
993 var rc:i64=nx_bo_append(row,"NEWER|",6,0)
994 if rc==0{rc=fs_review_span(row,dbase,fs_slen(dbase))}
995 if rc==0{rc=nx_bo_append(row,"|",1,0)}
996 if rc==0{rc=fs_review_span(row,pb+ido,idl)}
997 if rc==0{rc=nx_bo_append(row,"|",1,0)}
998 if rc==0{rc=fs_review_span(row,kind,fs_slen(kind))}
999 if rc==0{rc=nx_bo_append(row,"|",1,0)}
1000 if rc==0{rc=fs_review_span(row,month,mn)}
1001 if rc==0{rc=nx_bo_append(row,"|",1,0)}
1002 if rc==0&&tl>0{rc=fs_review_span(row,rb+to,tl)}
1003 if rc==0{rc=nx_bo_append(row,"|",1,0)}
1004 if rc==0&&ul>0{rc=fs_review_span(row,rb+uo,ul)}
1005 if rc==0{rc=nx_bo_append(row,"|",1,0)}
1006 if rc==0{rc=fs_review_span(row,mirror,fs_slen(mirror))}
1007 if rc==0{rc=nx_bo_append(row,"\n",1,0)}
1008 if rc==0{rc=nx_bo_append(wl,row.buf,row.len,0)}
1009 let released:i64=nx_bo_release(row);sys_munmap_direct(row as *u8,NX_BO_BYTES);sys_munmap_direct(month,FS_YMBUF);sys_munmap_direct(fo as *u8,16)
1010 if rc==0&&released<0{return released};return rc
1011}
1012
1013func fs_read_estate(path:*u8,n:*i64)->*u8 {
1014 var prefix:i64=fs_slen(EP_ROOT);let buildPrefix:i64=fs_slen(EP_BUILDROOT);if buildPrefix>prefix{prefix=buildPrefix}
1015 let resolved:*u8=sys_mmap(prefix+fs_slen(path)+1)
1016 if ep_artifact_path(resolved,path)!=1{n[0]=0;return 0 as *u8}
1017 return sys_read_file(resolved,n)
1018}
1019
1020func fs_attest_domain(dom: *u8, dbase: *u8, sb: *u8, sn: i64, today: *u8, max_items: i64, min_token: i64, seed_tokens: i64, tot: *i64) -> i64 {
1021 let path: *u8 = sys_mmap(FS_PATHCAP)
1022 fs_cmp_path(path, dom, ".plan" as *u8)
1023 let plen: *i64 = sys_mmap(16) as *i64
1024 let pb: *u8 = sys_read_file(path, plen)
1025 if (pb as i64) == 0 { fs_w("ATTEST-REFUSED domain=" as *u8); fs_w(dom); fs_w(" reason=no-plan path=" as *u8); fs_w(path); fs_w("\n" as *u8); return 0 - 1 }
1026 let pn: i64 = plen[0]
1027 let nbars: i64 = bf_count_rows(pb, pn, BF_BAR_TAG)
1028 fs_w("=== nx_frontier_scan attest " as *u8); fs_w(dom); fs_w(" bars=" as *u8); fs_n(nbars); fs_w(" today=" as *u8); fs_w(today); fs_w("\n" as *u8)
1029 if nbars == 0 { fs_w("NO-BAR domain=" as *u8); fs_w(dom); fs_w(" -- no sotabar row, nothing to attest (a domain opts in by declaring its first bar)\n" as *u8); return 0 }
1030 let rpath: *u8 = sys_mmap(FS_PATHCAP)
1031 fs_cmp_path(rpath, dom, ".refs" as *u8)
1032 let rflen: *i64 = sys_mmap(16) as *i64
1033 let refs: *u8 = sys_read_file(rpath, rflen)
1034 var refsn: i64 = 0
1035 if (refs as i64) != 0 { refsn = rflen[0] }
1036 let spos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
1037 var ndated: i64 = 0
1038 var s0: i64 = 0
1039 while s0 < sn {
1040 let sle0: i64 = fs_eol(sb, sn, s0)
1041 if fs_is_row(sb, s0, sle0) == 1 { if fs_pipes(sb, s0, sle0, spos) >= FS_DATED_PIPES { if fs_streq(sb, s0, spos[0], "src" as *u8) == 1 { ndated = ndated + 1 } } }
1042 s0 = sle0 + 1
1043 }
1044 fs_w("DATED-SOURCES " as *u8); fs_n(ndated); fs_w(" (source rows carrying a date spec; the undated ones are scan-only)\n" as *u8)
1045 let wl: *NxBufOwned = sys_mmap(NX_BO_BYTES) as *NxBufOwned
1046 sys_mkdir("/tmp/nx_frontier_scan" as *u8, FS_DIRMODE)
1047 let tmpout: *u8 = "/tmp/nx_frontier_scan/attest_fetch.out" as *u8
1048 let url: *u8 = sys_mmap(FS_URLCAP)
1049 let qbuf: *u8 = sys_mmap(FS_URLCAP)
1050 let kind: *u8 = sys_mmap(FS_TXTCAP)
1051 let idlm: *u8 = sys_mmap(FS_TXTCAP)
1052 let topen: *u8 = sys_mmap(FS_TXTCAP)
1053 let tclos: *u8 = sys_mmap(FS_TXTCAP)
1054 let uopen: *u8 = sys_mmap(FS_TXTCAP)
1055 let uclos: *u8 = sys_mmap(FS_TXTCAP)
1056 let join: *u8 = sys_mmap(FS_TXTCAP)
1057 let dopen: *u8 = sys_mmap(FS_TXTCAP)
1058 let dclos: *u8 = sys_mmap(FS_TXTCAP)
1059 let akind: *u8 = sys_mmap(FS_TXTCAP)
1060 let refkey: *u8 = sys_mmap(FS_TXTCAP)
1061 let tok: *u8 = sys_mmap(FS_TXTCAP)
1062 let note: *u8 = sys_mmap(FS_LINECAP)
1063 let savepath: *u8 = sys_mmap(FS_PATHCAP)
1064 let olen: *i64 = sys_mmap(16) as *i64
1065 let rlen: *i64 = sys_mmap(16) as *i64
1066 let cnt: *i64 = sys_mmap(8 * 8) as *i64
1067 let fo: *i64 = sys_mmap(16) as *i64
1068 let uo: *i64 = sys_mmap(16) as *i64
1069 let ymb: *u8 = sys_mmap(FS_YMBUF)
1070 var p: i64 = 0
1071 while p < pn {
1072 let e: i64 = bf_line_end(pb, pn, p)
1073 if bf_line_starts(pb, p, e, BF_BAR_TAG) == 1 {
1074 tot[FS_ATT_C_BARS] = tot[FS_ATT_C_BARS] + 1
1075 let idl: i64 = bf_field(pb, p, e, BF_F_BAR_ID, fo)
1076 let ido: i64 = fo[0]
1077 let yml: i64 = bf_field(pb, p, e, BF_F_BAR_YM, fo)
1078 let ymo: i64 = fo[0]
1079 let sjl: i64 = bf_field(pb, p, e, BF_F_BAR_SUBJECT, fo)
1080 let sjo: i64 = fo[0]
1081 let rkl: i64 = bf_field(pb, p, e, BF_F_BAR_REF, fo)
1082 let rko: i64 = fo[0]
1083 var bar_ym: i64 = 0 - 1
1084 if yml > 0 { bar_ym = fs_ym_at(pb, ymo, ymo + yml) }
1085 var rk: i64 = 0
1086 if rkl > 0 { rk = fs_catn(refkey, 0, pb, rko, rko + rkl) }
1087 refkey[rk] = 0 as u8
1088 fs_w("BAR " as *u8); fs_wn(pb, ido, idl); fs_w(" month=" as *u8); fs_wn(pb, ymo, yml)
1089 fs_w(" ref=" as *u8); fs_w(refkey); fs_w(" subject=" as *u8); fs_wn(pb, sjo, sjl); fs_w("\n" as *u8)
1090 if bar_ym < 0 { fs_w(" ATTEST-UNATTESTABLE reason=bar-month-malformed\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1091 // ---- ATTEST LEG: the listing the bar itself cites ----
1092 akind[0] = 0 as u8
1093 let ul: i64 = fs_ref_url(refs, refsn, refkey, rk, uo)
1094 if ul <= 0 { fs_w(" ATTEST-UNATTESTABLE reason=ref-not-in-refs ref=" as *u8); fs_w(refkey); fs_w("\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1095 let un: i64 = fs_catn(url, 0, refs, uo[0], uo[0] + ul)
1096 url[un] = 0 as u8
1097 var srow: i64 = 0 - 1
1098 var si: i64 = 0
1099 while si < sn {
1100 let sle: i64 = fs_eol(sb, sn, si)
1101 if fs_is_row(sb, si, sle) == 1 { if fs_pipes(sb, si, sle, spos) >= FS_DATED_PIPES { if fs_streq(sb, si, spos[0], "src" as *u8) == 1 {
1102 var pe: i64 = fs_find(sb, spos[2], spos[1] + 1, "{q}" as *u8)
1103 if pe < 0 { pe = spos[2] }
1104 let pl: i64 = pe - (spos[1] + 1)
1105 if pl > 0 { if pl <= un {
1106 var m: i64 = 1
1107 var j: i64 = 0
1108 while j < pl { if url[j] != sb[spos[1] + 1 + j] { m = 0; j = pl } else { j = j + 1 } }
1109 if m == 1 { if srow < 0 { srow = si } }
1110 } }
1111 } } }
1112 si = sle + 1
1113 }
1114 if srow < 0 { fs_w(" ATTEST-UNATTESTABLE reason=no-dated-source-row-knows-this-listing url=" as *u8); fs_w(url); fs_w("\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1115 let sle2: i64 = fs_eol(sb, sn, srow)
1116 fs_pipes(sb, srow, sle2, spos)
1117 fs_src_fields(sb, spos, sle2, kind, idlm, topen, tclos, uopen, uclos, join, dopen, dclos)
1118 let akl: i64 = fs_cat(akind, 0, kind)
1119 akind[akl] = 0 as u8
1120 tot[FS_ATT_C_FETCHES] = tot[FS_ATT_C_FETCHES] + 1
1121 let got: i64 = fs_fetch(url, tmpout, savepath, olen)
1122 if got <= 0 { fs_w(" ATTEST-UNATTESTABLE reason=source-unreachable kind=" as *u8); fs_w(kind); fs_w(" (UNOBSERVABLE, not zero)\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1123 let rb: *u8 = sys_read_file(savepath, rlen)
1124 if (rb as i64) == 0 { fs_w(" ATTEST-UNATTESTABLE reason=mirror-unreadable\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1125 let rn: i64 = rlen[0]
1126 let fixed: i64 = fs_fixed_month(dopen)
1127 let newest: i64 = fs_newest_month(rb, rn, idlm, dopen, dclos, fixed, max_items, pb, 0, 0, min_token, tok, cnt)
1128 if newest < 0 { fs_w(" ATTEST-UNATTESTABLE reason=no-dated-item kind=" as *u8); fs_w(kind); fs_w(" items=" as *u8); fs_n(cnt[0]); fs_w("\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } else {
1129 fs_w(" ATTEST kind=" as *u8); fs_w(kind); fs_w(" items=" as *u8); fs_n(cnt[0]); fs_w(" dated=" as *u8); fs_n(cnt[1])
1130 fs_w(" newest=" as *u8); fs_w_ym(ymb, newest); fs_w(" bar=" as *u8); fs_w_ym(ymb, bar_ym)
1131 if newest > bar_ym { fs_w(" verdict=NEWER (the listing moved past the bar -- nx_barfresh reads STALE until a seat re-adjudicates the sotabar row)" as *u8); tot[FS_ATT_C_STALE] = tot[FS_ATT_C_STALE] + 1
1132 let queued:i64=fs_review_newer(wl,dbase,pb,ido,idl,kind,newest,rb,rn,cnt[2],idlm,topen,tclos,uopen,uclos,savepath)
1133 if queued<0{fs_w(" REVIEW-QUEUE-REFUSED rc=");fs_n(queued);nx_bo_release(wl);return queued}
1134 } else { if newest == bar_ym { fs_w(" verdict=SAME" as *u8) } else { fs_w(" verdict=OLDER (the listing carries nothing newer than the bar)" as *u8) } }
1135 fs_w("\n" as *u8)
1136 var np9: i64 = fs_cat(note, 0, "attested by nx_frontier_scan attest: " as *u8)
1137 np9 = fs_catn_num(note, np9, cnt[0]); np9 = fs_cat(note, np9, " items read, " as *u8)
1138 np9 = fs_catn_num(note, np9, cnt[1]); np9 = fs_cat(note, np9, " dated, newest item " as *u8)
1139 if cnt[4] > cnt[3] { np9 = fs_catn(note, np9, rb, cnt[3], cnt[4]) } else { np9 = fs_cat(note, np9, "(fixed month)" as *u8) }
1140 np9 = fs_cat(note, np9, ", mirror " as *u8); np9 = fs_cat(note, np9, savepath)
1141 note[np9] = 0 as u8
1142 let arc: i64 = fs_attest_row(path, refkey, today, newest, note)
1143 if arc == 1 { fs_w(" BARSCAN-APPENDED plan=" as *u8); fs_w(path); fs_w("\n" as *u8); tot[FS_ATT_C_APPENDED] = tot[FS_ATT_C_APPENDED] + 1 } else { if arc == 0 { fs_w(" BARSCAN-ALREADY-PRESENT (same date and ref: idempotent)\n" as *u8); tot[FS_ATT_C_ALREADY] = tot[FS_ATT_C_ALREADY] + 1 } else { fs_w(" BARSCAN-UNWRITABLE plan=" as *u8); fs_w(path); fs_w("\n" as *u8); tot[FS_ATT_C_UNATTESTABLE] = tot[FS_ATT_C_UNATTESTABLE] + 1 } }
1144 }
1145 }
1146 }
1147 }
1148 }
1149 // ---- FAN-OUT LEG: every OTHER dated source, asked the bar's own subject ----
1150 var fi: i64 = 0
1151 while fi < sn {
1152 let fle: i64 = fs_eol(sb, sn, fi)
1153 if fs_is_row(sb, fi, fle) == 1 { if fs_pipes(sb, fi, fle, spos) >= FS_DATED_PIPES { if fs_streq(sb, fi, spos[0], "src" as *u8) == 1 {
1154 fs_src_fields(sb, spos, fle, kind, idlm, topen, tclos, uopen, uclos, join, dopen, dclos)
1155 if fs_streq(kind, 0, fs_slen(kind), akind) == 0 {
1156 fs_seed_q(qbuf, pb, sjo, sjo + sjl, FS_URLCAP, seed_tokens, min_token, join)
1157 let had: i64 = fs_build_url(url, sb, spos[1] + 1, spos[2], qbuf)
1158 tot[FS_ATT_C_FETCHES] = tot[FS_ATT_C_FETCHES] + 1
1159 let fgot: i64 = fs_fetch(url, tmpout, savepath, olen)
1160 if fgot <= 0 { fs_w(" FANOUT kind=" as *u8); fs_w(kind); fs_w(" verdict=UNREACHABLE (UNOBSERVABLE, not zero) url=" as *u8); fs_w(url); fs_w("\n" as *u8) } else {
1161 let fb: *u8 = sys_read_file(savepath, rlen)
1162 if (fb as i64) == 0 { fs_w(" FANOUT kind=" as *u8); fs_w(kind); fs_w(" verdict=MIRROR-UNREADABLE\n" as *u8) } else {
1163 let fn9: i64 = rlen[0]
1164 let ffix: i64 = fs_fixed_month(dopen)
1165 var subjn: i64 = 0
1166 if had == 0 { subjn = sjl }
1167 let fnew: i64 = fs_newest_month(fb, fn9, idlm, dopen, dclos, ffix, max_items, pb, sjo, subjn, min_token, tok, cnt)
1168 fs_w(" FANOUT kind=" as *u8); fs_w(kind); fs_w(" q=" as *u8)
1169 if had == 0 { fs_w("(static listing, filtered by the subject tokens)" as *u8) } else { fs_w(qbuf) }
1170 fs_w(" items=" as *u8); fs_n(cnt[0]); fs_w(" dated=" as *u8); fs_n(cnt[1]); fs_w(" newest=" as *u8)
1171 if fnew < 0 { fs_w("NONE verdict=UNDATED\n" as *u8) } else {
1172 fs_w_ym(ymb, fnew); fs_w(" vs_bar=" as *u8); fs_w_ym(ymb, bar_ym)
1173 if fnew > bar_ym {
1174 fs_w(" verdict=NEWER\n" as *u8)
1175 tot[FS_ATT_C_NEWER] = tot[FS_ATT_C_NEWER] + 1
1176 let bit: i64 = cnt[2]
1177 if bit >= 0 {
1178 let bie: i64 = fs_item_end(fb, fn9, bit, idlm)
1179 let tl9: i64 = fs_item_field(fb, bit, bie, topen, tclos, fo)
1180 let to9: i64 = fo[0]
1181 let ul9: i64 = fs_item_field(fb, bit, bie, uopen, uclos, fo)
1182 let uo9: i64 = fo[0]
1183 fs_w(" newest: " as *u8)
1184 if tl9 > 0 { fs_wn(fb, to9, tl9) } else { fs_w("(untitled)" as *u8) }
1185 fs_w(" | " as *u8)
1186 if ul9 > 0 { fs_wn(fb, uo9, ul9) } else { fs_w("(no url)" as *u8) }
1187 fs_w(" | mirror " as *u8); fs_w(savepath); fs_w("\n" as *u8)
1188 let queued:i64=fs_review_newer(wl,dbase,pb,ido,idl,kind,fnew,fb,fn9,bit,idlm,topen,tclos,uopen,uclos,savepath)
1189 if queued<0{fs_w("REVIEW-QUEUE-REFUSED rc=");fs_n(queued);nx_bo_release(wl);return queued}
1190 }
1191 } else { if fnew == bar_ym { fs_w(" verdict=SAME\n" as *u8) } else { fs_w(" verdict=OLDER\n" as *u8) } }
1192 }
1193 }
1194 }
1195 }
1196 } } }
1197 fi = fle + 1
1198 }
1199 }
1200 }
1201 p = e + 1
1202 }
1203 if wl.len > 0 {
1204 let fld: *u8 = sys_mmap(FS_PATHCAP)
1205 var lp: i64 = 0
1206 if fs_has_slash(dom) == 1 { lp = fs_cat(fld, 0, "/tmp/nx_frontier_scan/frontier_attest_" as *u8) } else { lp = fs_cat(fld, 0, "knowledge/status/frontier_attest_" as *u8) }
1207 lp = fs_cat(fld, lp, dbase); lp = fs_cat(fld, lp, ".txt" as *u8); fld[lp] = 0 as u8
1208 let wrote: i64 = dp_writefile(fld, wl.buf, wl.len)
1209 fs_w("WORKLIST path=" as *u8); fs_w(fld); fs_w(" bytes=" as *u8); fs_n(wl.len); fs_w(" wrote=" as *u8); fs_n(wrote); fs_w(" (NEWER rows a seat adjudicates into sotabar rows -- never admitted here)\n" as *u8)
1210 }
1211 let released:i64=nx_bo_release(wl)
1212 return released
1213}
1214
1215func main(argc: i64, argv: *i64) -> i64 {
1216 if argc < 3 {
1217 fs_w("usage: nx_frontier_scan seeds <domain>\n" as *u8)
1218 fs_w(" nx_frontier_scan attest <domain>|all [YYYY-MM-DD] re-earn every sotabar row's month: barscan rows + the fan-out\n" as *u8)
1219 fs_w(" nx_frontier_scan monthprobe <file> <item-delim> <date-open|month=YYYY-MM> <date-close> (offline)\n" as *u8)
1220 fs_w(" nx_frontier_scan attestfile <plan> <ref-key> <YYYY-MM-DD> <YYYY-MM> <note> (offline, idempotent)\n" as *u8)
1221 fs_w(" nx_frontier_scan deficit <domain>\n" as *u8)
1222 fs_w(" nx_frontier_scan scan <domain> [maxrows] [maxitems]\n" as *u8)
1223 fs_w(" nx_frontier_scan propose <domain> [maxrows] [maxitems]\n" as *u8)
1224 fs_w(" <domain> is a /compare domain name, OR a path prefix if it contains a slash -- e.g.\n" as *u8)
1225 fs_w(" /tmp/nx_frontier_scan_gate/fx reads /tmp/nx_frontier_scan_gate/fx.matrix and fx.refs,\n" as *u8)
1226 fs_w(" which is how a gate keeps its fixture out of the live board and how the replay harness\n" as *u8)
1227 fs_w(" reconstructs a historical domain without writing into knowledge/compare.\n" as *u8)
1228 fs_w(" the autonomous frontier-noticer: seeds from the domain's OWN data, outside pulled through\n" as *u8)
1229 fs_w(" nx_research_fetch, diffed against the board, PROPOSALS ONLY -- it never writes .matrix or\n" as *u8)
1230 fs_w(" .refs and never flips a watch cell.\n" as *u8)
1231 sys_exit(2); return 2
1232 }
1233 let verb: *u8 = argv[1] as *u8
1234 let dom: *u8 = argv[2] as *u8
1235 // dom BUILDS PATHS; dbase LABELS OUTPUT. Keeping them separate is what stops a fixture path leaking
1236 // into a plane id or a status filename.
1237 let dbase: *u8 = ((dom as i64) + fs_basename(dom)) as *u8
1238 let vlen: i64 = fs_slen(verb)
1239
1240 if fs_streq(verb, 0, vlen, "seeds" as *u8) == 1 {
1241 fs_w("=== nx_frontier_scan seeds " as *u8); fs_w(dom); fs_w(" ===\n" as *u8)
1242 if fs_emit_seeds(dom) < 0 { sys_exit(3); return 3 }
1243 sys_exit(0); return 0
1244 }
1245
1246 if fs_streq(verb, 0, vlen, "pairprobe" as *u8) == 1 {
1247 // OFFLINE EXTRACTOR PROBE (2026-08-20). The pairing rule -- every field belongs to its OWN item --
1248 // is the one thing in this organ that can manufacture a fabricated citation, and until now it was
1249 // reachable ONLY through a live network fetch, which no gate may do. ★★★★★A RULE THAT CAN ONLY BE
1250 // EXERCISED OVER THE NETWORK IS A RULE NO GATE CAN HOLD. This verb runs the SAME fs_item_end bound
1251 // over a file, so the rule is testable hermetically and a mutant that drops the bound is killable.
1252 if argc < 8 {
1253 fs_w("usage: nx_frontier_scan pairprobe <file> <item-delim> <title-open> <title-close> <url-open> <url-close>\n" as *u8)
1254 sys_exit(2); return 2
1255 }
1256 let plen: *i64 = sys_mmap(16) as *i64
1257 let pb: *u8 = sys_read_file(dom, plen)
1258 if (pb as i64) == 0 {
1259 fs_w("PAIRPROBE REFUSED verdict=RED reason=unreadable file=" as *u8); fs_w(dom); fs_w("\n" as *u8)
1260 sys_exit(1); return 1
1261 }
1262 let pn: i64 = plen[0]
1263 let pidlm: *u8 = argv[3] as *u8
1264 let ptop: *u8 = argv[4] as *u8
1265 let ptcl: *u8 = argv[5] as *u8
1266 let puop: *u8 = argv[6] as *u8
1267 let pucl: *u8 = argv[7] as *u8
1268 fs_w("=== nx_frontier_scan pairprobe " as *u8); fs_w(dom); fs_w(" ===\n" as *u8)
1269 var pcur: i64 = 0
1270 var pitems: i64 = 0
1271 var ppaired: i64 = 0
1272 var punpaired: i64 = 0
1273 var pgo: i64 = 1
1274 while pgo == 1 {
1275 let pit: i64 = fs_find(pb, pn, pcur, pidlm)
1276 if pit < 0 { pgo = 0 } else {
1277 let pie: i64 = fs_item_end(pb, pn, pit, pidlm)
1278 pcur = pit + fs_slen(pidlm)
1279 pitems = pitems + 1
1280 let pts: i64 = fs_find(pb, pie, pit, ptop)
1281 let pus: i64 = fs_find(pb, pie, pit, puop)
1282 var pok: i64 = 1
1283 if pts < 0 { pok = 0 }
1284 if pus < 0 { pok = 0 }
1285 var pta: i64 = 0
1286 var pte: i64 = 0
1287 var pua: i64 = 0
1288 var pue: i64 = 0
1289 if pok == 1 {
1290 pta = pts + fs_slen(ptop)
1291 pte = fs_find(pb, pie, pta, ptcl)
1292 pua = pus + fs_slen(puop)
1293 pue = fs_find(pb, pie, pua, pucl)
1294 if pte < 0 { pok = 0 }
1295 if pue < 0 { pok = 0 }
1296 }
1297 if pok == 1 {
1298 ppaired = ppaired + 1
1299 var pin: i64 = 0
1300 if pua >= pit { if pue <= pie { pin = 1 } }
1301 fs_w("ITEM n=" as *u8); fs_n(pitems)
1302 fs_w(" span=" as *u8); fs_n(pit); fs_w("-" as *u8); fs_n(pie)
1303 fs_w(" url_in_own_item=" as *u8); fs_n(pin)
1304 fs_w(" title=" as *u8); fs_wn(pb, pta, pte - pta)
1305 fs_w(" url=" as *u8); fs_wn(pb, pua, pue - pua)
1306 fs_w("\n" as *u8)
1307 } else {
1308 punpaired = punpaired + 1
1309 fs_w("ITEM n=" as *u8); fs_n(pitems)
1310 fs_w(" span=" as *u8); fs_n(pit); fs_w("-" as *u8); fs_n(pie)
1311 fs_w(" UNPAIRED -- a field is absent from THIS item and is NOT borrowed from the next\n" as *u8)
1312 }
1313 }
1314 }
1315 fs_w("PAIRPROBE items=" as *u8); fs_n(pitems)
1316 fs_w(" paired=" as *u8); fs_n(ppaired)
1317 fs_w(" unpaired=" as *u8); fs_n(punpaired)
1318 fs_w(" bound=item-extent (paired plus unpaired equals items -- and every url above lies inside its own item, which is the invariant this verb exists to state out loud)\n" as *u8)
1319 sys_exit(0); return 0
1320 }
1321
1322 if fs_streq(verb, 0, vlen, "monthprobe" as *u8) == 1 {
1323 // OFFLINE MONTH READER (2026-09-06): the newest-month rule is the one thing that can make a bar read
1324 // ATTESTED, so it must be testable hermetically -- a gate may never fetch the live web on a beat.
1325 if argc < 6 {
1326 fs_w("usage: nx_frontier_scan monthprobe <file> <item-delim> <date-open|month=YYYY-MM> <date-close>\n" as *u8)
1327 sys_exit(2); return 2
1328 }
1329 let mlen9: *i64 = sys_mmap(16) as *i64
1330 let mb9: *u8 = sys_read_file(dom, mlen9)
1331 if (mb9 as i64) == 0 { fs_w("MONTHPROBE REFUSED reason=unreadable file=" as *u8); fs_w(dom); fs_w("\n" as *u8); sys_exit(1); return 1 }
1332 let cnt9: *i64 = sys_mmap(8 * 8) as *i64
1333 let tok9: *u8 = sys_mmap(FS_TXTCAP)
1334 let ymb9: *u8 = sys_mmap(FS_YMBUF)
1335 let dop9: *u8 = argv[4] as *u8
1336 // every item counts: the bound is the file's own length (an item is at least one byte), so nothing is capped in silence
1337 let nw9: i64 = fs_newest_month(mb9, mlen9[0], argv[3] as *u8, dop9, argv[5] as *u8, fs_fixed_month(dop9), mlen9[0], mb9, 0, 0, 1, tok9, cnt9)
1338 fs_w("MONTHPROBE items=" as *u8); fs_n(cnt9[0]); fs_w(" dated=" as *u8); fs_n(cnt9[1]); fs_w(" newest=" as *u8)
1339 if nw9 < 0 { fs_w("NONE (no item carried a date the reader could parse -- UNDATED abstains, it never reads as a month)\n" as *u8) } else { fs_w_ym(ymb9, nw9); fs_w("\n" as *u8) }
1340 sys_exit(0); return 0
1341 }
1342
1343 if fs_streq(verb, 0, vlen, "attestfile" as *u8) == 1 {
1344 // OFFLINE ATTESTATION WRITER: the idempotence rule (one barscan row per date and ref) held to a fixture plan.
1345 if argc < 7 {
1346 fs_w("usage: nx_frontier_scan attestfile <plan> <ref-key> <YYYY-MM-DD> <YYYY-MM> <note>\n" as *u8)
1347 sys_exit(2); return 2
1348 }
1349 let aym9: i64 = fs_ym_at(argv[5] as *u8, 0, fs_slen(argv[5] as *u8))
1350 if aym9 < 0 { fs_w("ATTESTFILE REFUSED reason=month-malformed month=" as *u8); fs_w(argv[5] as *u8); fs_w("\n" as *u8); sys_exit(1); return 1 }
1351 let arc9: i64 = fs_attest_row(dom, argv[3] as *u8, argv[4] as *u8, aym9, argv[6] as *u8)
1352 fs_w("ATTESTFILE rc=" as *u8); fs_n(arc9); fs_w(" (1=appended 0=already-present -1=unwritable) plan=" as *u8); fs_w(dom); fs_w("\n" as *u8)
1353 sys_exit(0); return 0
1354 }
1355
1356 if fs_streq(verb, 0, vlen, "attest" as *u8) == 1 {
1357 let aclen: *i64 = sys_mmap(16) as *i64
1358 let acb: *u8 = fs_read_estate("knowledge/frontier_scan.conf" as *u8, aclen)
1359 if (acb as i64) == 0 { fs_w("ATTEST REFUSED verdict=RED reason=missing-conf path=knowledge/frontier_scan.conf\n" as *u8); sys_exit(1); return 1 }
1360 let acn: i64 = aclen[0]
1361 let a_items: i64 = fs_conf_get(acb, acn, "max_items" as *u8)
1362 let a_mintok: i64 = fs_conf_get(acb, acn, "min_token" as *u8)
1363 let a_seedtok: i64 = fs_conf_get(acb, acn, "seed_tokens" as *u8)
1364 if a_items < 0 { fs_w("ATTEST REFUSED reason=conf-missing-key key=max_items\n" as *u8); sys_exit(1); return 1 }
1365 if a_mintok < 0 { fs_w("ATTEST REFUSED reason=conf-missing-key key=min_token\n" as *u8); sys_exit(1); return 1 }
1366 if a_seedtok < 0 { fs_w("ATTEST REFUSED reason=conf-missing-key key=seed_tokens\n" as *u8); sys_exit(1); return 1 }
1367 let slen9: *i64 = sys_mmap(16) as *i64
1368 let sb9: *u8 = fs_read_estate("knowledge/frontier_sources.conf" as *u8, slen9)
1369 if (sb9 as i64) == 0 { fs_w("ATTEST REFUSED verdict=RED reason=missing-conf path=knowledge/frontier_sources.conf\n" as *u8); sys_exit(1); return 1 }
1370 let today9: *u8 = sys_mmap(FS_TODAYBUF)
1371 if argc >= 4 { let tl9: i64 = fs_cat(today9, 0, argv[3] as *u8); today9[tl9] = 0 as u8 } else { fs_today(today9) }
1372 let tot9: *i64 = sys_mmap(8 * FS_ATT_C_N) as *i64
1373 var ti9: i64 = 0
1374 while ti9 < FS_ATT_C_N { tot9[ti9] = 0; ti9 = ti9 + 1 }
1375 var doms9: i64 = 0
1376 var attestFailures:i64=0
1377 if fs_streq(dom, 0, fs_slen(dom), "all" as *u8) == 1 {
1378 let llen9: *i64 = sys_mmap(16) as *i64
1379 let lb9: *u8 = fs_read_estate("buildroot/knowledge/compare/regen.list" as *u8, llen9)
1380 if (lb9 as i64) == 0 { fs_w("ATTEST REFUSED reason=no-regen-list path=buildroot/knowledge/compare/regen.list\n" as *u8); sys_exit(1); return 1 }
1381 let ln9: i64 = llen9[0]
1382 let dbuf9: *u8 = sys_mmap(FS_TXTCAP)
1383 var li9: i64 = 0
1384 while li9 < ln9 {
1385 let le9: i64 = fs_eol(lb9, ln9, li9)
1386 if fs_is_row(lb9, li9, le9) == 1 { if le9 > li9 {
1387 var dl9: i64 = fs_catn(dbuf9, 0, lb9, li9, le9)
1388 // a list edited on another host carries CR before LF; a domain name never does
1389 if dl9 > 0 { if dbuf9[dl9 - 1] == (FS_CR as u8) { dl9 = dl9 - 1 } }
1390 dbuf9[dl9] = 0 as u8
1391 doms9 = doms9 + 1
1392 let domainRc:i64=fs_attest_domain(dbuf9, dbuf9, sb9, slen9[0], today9, a_items, a_mintok, a_seedtok, tot9)
1393 if domainRc!=0{attestFailures=attestFailures+1}
1394 } }
1395 li9 = le9 + 1
1396 }
1397 } else { doms9 = 1; let domainRc:i64=fs_attest_domain(dom, dbase, sb9, slen9[0], today9, a_items, a_mintok, a_seedtok, tot9);if domainRc!=0{attestFailures=attestFailures+1} }
1398 fs_w("ATTEST-DONE domains=" as *u8); fs_n(doms9)
1399 fs_w(" bars=" as *u8); fs_n(tot9[FS_ATT_C_BARS])
1400 fs_w(" appended=" as *u8); fs_n(tot9[FS_ATT_C_APPENDED])
1401 fs_w(" already=" as *u8); fs_n(tot9[FS_ATT_C_ALREADY])
1402 fs_w(" unattestable=" as *u8); fs_n(tot9[FS_ATT_C_UNATTESTABLE])
1403 fs_w(" (appended plus already plus unattestable equals bars)" as *u8)
1404 fs_w(" fetches=" as *u8); fs_n(tot9[FS_ATT_C_FETCHES])
1405 fs_w(" newer_sources=" as *u8); fs_n(tot9[FS_ATT_C_NEWER])
1406 fs_w(" stale_bars=" as *u8); fs_n(tot9[FS_ATT_C_STALE])
1407 if attestFailures>0{fs_w(" failed_domains=" as *u8);fs_n(attestFailures);fs_w(" verdict=INCOMPLETE action=inspect-domain-failure; no successful attestation claimed\n" as *u8);sys_exit(2);return 2}
1408 var vb9: i64 = 0
1409 if tot9[FS_ATT_C_STALE] > 0 { vb9 = 1 }
1410 if vb9 == 1 { fs_w(" verdict=STALE-BARS (a cited listing moved past its bar: re-adjudicate the sotabar row)\n" as *u8) } else { fs_w(" verdict=ATTESTED\n" as *u8) }
1411 sys_exit(vb9); return vb9
1412 }
1413
1414 let clen: *i64 = sys_mmap(16) as *i64
1415 let cb: *u8 = fs_read_estate("knowledge/frontier_scan.conf" as *u8, clen)
1416 if (cb as i64) == 0 {
1417 fs_w("FRONTIER-SCAN REFUSED verdict=RED reason=missing-conf path=knowledge/frontier_scan.conf" as *u8)
1418 fs_w(" fix=seed the thresholds file with rows lead_code, cover_permil, min_token, max_rows, max_items -- each row carrying its derivation\n" as *u8)
1419 sys_exit(1); return 1
1420 }
1421 let cn: i64 = clen[0]
1422 let lead_code: i64 = fs_conf_get(cb, cn, "lead_code" as *u8)
1423 let cover_permil: i64 = fs_conf_get(cb, cn, "cover_permil" as *u8)
1424 let min_token: i64 = fs_conf_get(cb, cn, "min_token" as *u8)
1425 var max_rows: i64 = fs_conf_get(cb, cn, "max_rows" as *u8)
1426 var max_items: i64 = fs_conf_get(cb, cn, "max_items" as *u8)
1427 let seed_tokens: i64 = fs_conf_get(cb, cn, "seed_tokens" as *u8)
1428 var bad: i64 = 0
1429 if seed_tokens < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=seed_tokens\n" as *u8); bad = 1 }
1430 if lead_code < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=lead_code\n" as *u8); bad = 1 }
1431 if cover_permil < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=cover_permil\n" as *u8); bad = 1 }
1432 if min_token < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=min_token\n" as *u8); bad = 1 }
1433 if max_rows < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=max_rows\n" as *u8); bad = 1 }
1434 if max_items < 0 { fs_w("FRONTIER-SCAN REFUSED reason=conf-missing-key key=max_items\n" as *u8); bad = 1 }
1435 if bad == 1 { sys_exit(1); return 1 }
1436 if argc >= 4 { let v: i64 = fs_atoi(argv[3] as *u8); if v > 0 { max_rows = v } }
1437 if argc >= 5 { let v2: i64 = fs_atoi(argv[4] as *u8); if v2 > 0 { max_items = v2 } }
1438
1439 let path: *u8 = sys_mmap(FS_PATHCAP)
1440 fs_cmp_path(path, dom, ".matrix" as *u8)
1441 let mlen: *i64 = sys_mmap(16) as *i64
1442 let mb: *u8 = sys_read_file(path, mlen)
1443 if (mb as i64) == 0 {
1444 fs_w("FRONTIER-SCAN REFUSED verdict=RED reason=no-matrix path=" as *u8); fs_w(path); fs_w("\n" as *u8)
1445 sys_exit(3); return 3
1446 }
1447 let mn: i64 = mlen[0]
1448
1449 fs_w("=== nx_frontier_scan " as *u8); fs_w(verb); fs_w(" " as *u8); fs_w(dom); fs_w(" ===\n" as *u8)
1450 fs_w("CONF lead_code=" as *u8); fs_n(lead_code)
1451 fs_w(" cover_permil=" as *u8); fs_n(cover_permil)
1452 fs_w(" min_token=" as *u8); fs_n(min_token)
1453 fs_w(" max_rows=" as *u8); fs_n(max_rows)
1454 fs_w(" max_items=" as *u8); fs_n(max_items)
1455 fs_w(" seed_tokens=" as *u8); fs_n(seed_tokens)
1456 fs_w(" props_per_row=" as *u8); fs_n(FS_PROPS_PER_ROW); fs_w("\n" as *u8)
1457
1458 let rowa: *i64 = sys_mmap(8 * FS_MAXROWS) as *i64
1459 let rowb: *i64 = sys_mmap(8 * FS_MAXROWS) as *i64
1460 let score: *i64 = sys_mmap(8 * FS_MAXROWS) as *i64
1461 let nd: i64 = fs_deficit(mb, mn, lead_code, rowa, rowb, score, 1)
1462
1463 if fs_streq(verb, 0, vlen, "deficit" as *u8) == 1 {
1464 fs_w("DEFICIT-ROWS=" as *u8); fs_n(nd); fs_w("\n" as *u8)
1465 sys_exit(0); return 0
1466 }
1467
1468 var filing: i64 = 0
1469 if fs_streq(verb, 0, vlen, "propose" as *u8) == 1 { filing = 1 }
1470
1471 if nd == 0 {
1472 // THE POSITIVE CONTROL PATH, and it is why this organ is not a guard that refuses everything.
1473 // A domain where no rival leads a row we do not exceed yields ZERO proposals and ZERO outbound
1474 // fetches -- the input that MUST come back empty, answered without touching the network.
1475 fs_w("NO-DEFICIT domain=" as *u8); fs_w(dom)
1476 fs_w(" -- no row where a rival leads and we do not exceed; nothing to research\n" as *u8)
1477 fs_w("SCAN-DONE domain=" as *u8); fs_w(dom)
1478 fs_w(" deficit_rows=0 rows_scanned=0 fetches=0 PROPOSALS=0\n" as *u8)
1479 sys_exit(0); return 0
1480 }
1481
1482 // ---- the corpus we diff against: this domain's own labels, row notes and citation prose ----
1483 fs_cmp_path(path, dom, ".refs" as *u8)
1484 let rflen: *i64 = sys_mmap(16) as *i64
1485 let refs: *u8 = sys_read_file(path, rflen)
1486 var refsn: i64 = 0
1487 if (refs as i64) != 0 { refsn = rflen[0] }
1488 // sized FROM THE INPUT, so there is no ceiling to guess and no silent truncation to hide.
1489 let corp: *u8 = sys_mmap(mn + refsn + FS_TXTCAP)
1490 var corpn: i64 = 0
1491 let cpos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
1492 var ci: i64 = 0
1493 while ci < mn {
1494 let cle: i64 = fs_eol(mb, mn, ci)
1495 if fs_is_row(mb, ci, cle) == 1 {
1496 let cnp: i64 = fs_pipes(mb, ci, cle, cpos)
1497 if cnp >= 1 {
1498 corpn = fs_catn(corp, corpn, mb, ci, cpos[0])
1499 corp[corpn] = FS_SPACE as u8; corpn = corpn + 1
1500 if cnp >= 8 {
1501 corpn = fs_catn(corp, corpn, mb, cpos[7] + 1, cle)
1502 corp[corpn] = FS_SPACE as u8; corpn = corpn + 1
1503 }
1504 }
1505 }
1506 ci = cle + 1
1507 }
1508 if refsn > 0 { corpn = fs_catn(corp, corpn, refs, 0, refsn) }
1509 fs_w("BOARD-CORPUS bytes=" as *u8); fs_n(corpn)
1510 fs_w(" refs_bytes=" as *u8); fs_n(refsn); fs_w("\n" as *u8)
1511
1512 sys_mkdir("/tmp/nx_frontier_scan" as *u8, FS_DIRMODE)
1513
1514 let slen: *i64 = sys_mmap(16) as *i64
1515 let sb: *u8 = sys_read_file("knowledge/frontier_sources.conf" as *u8, slen)
1516 if (sb as i64) == 0 {
1517 fs_w("FRONTIER-SCAN REFUSED verdict=RED reason=missing-conf path=knowledge/frontier_sources.conf" as *u8)
1518 fs_w(" fix=seed source rows src|<kind>|<url-template-with-brace-q>|<item>|<title-open>|<title-close>|<url-open>|<url-close>\n" as *u8)
1519 sys_exit(1); return 1
1520 }
1521 let sn: i64 = slen[0]
1522 // count the sources FIRST so the worklist buffer is derived, not guessed.
1523 let spos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
1524 var nsrc: i64 = 0
1525 var sc0: i64 = 0
1526 while sc0 < sn {
1527 let sle0: i64 = fs_eol(sb, sn, sc0)
1528 if fs_is_row(sb, sc0, sle0) == 1 {
1529 let np0: i64 = fs_pipes(sb, sc0, sle0, spos)
1530 if np0 >= 8 { if fs_streq(sb, sc0, spos[0], "src" as *u8) == 1 { nsrc = nsrc + 1 } }
1531 }
1532 sc0 = sle0 + 1
1533 }
1534 if nsrc == 0 {
1535 fs_w("FRONTIER-SCAN REFUSED verdict=RED reason=no-src-rows path=knowledge/frontier_sources.conf\n" as *u8)
1536 sys_exit(1); return 1
1537 }
1538 if max_rows > nd { max_rows = nd }
1539 let wl: *u8 = sys_mmap(max_rows * nsrc * FS_PROPS_PER_ROW * FS_LINECAP + FS_TXTCAP)
1540 let wlout: *i64 = sys_mmap(16) as *i64
1541 wlout[0] = 0
1542
1543 // every buffer the inner loops need, allocated ONCE (never allocate in a hot loop)
1544 let qbuf: *u8 = sys_mmap(FS_URLCAP)
1545 let url: *u8 = sys_mmap(FS_URLCAP)
1546 let kind: *u8 = sys_mmap(FS_TXTCAP)
1547 let idlm: *u8 = sys_mmap(FS_TXTCAP)
1548 let topen: *u8 = sys_mmap(FS_TXTCAP)
1549 let tclos: *u8 = sys_mmap(FS_TXTCAP)
1550 let uopen: *u8 = sys_mmap(FS_TXTCAP)
1551 let uclos: *u8 = sys_mmap(FS_TXTCAP)
1552 let join: *u8 = sys_mmap(FS_TXTCAP)
1553 let tok: *u8 = sys_mmap(FS_TXTCAP)
1554 let savepath: *u8 = sys_mmap(FS_PATHCAP)
1555 let olen: *i64 = sys_mmap(16) as *i64
1556 let rlen: *i64 = sys_mmap(16) as *i64
1557
1558 var total_props: i64 = 0
1559 var fetches: i64 = 0
1560 var r: i64 = 0
1561 while r < max_rows {
1562 fs_w("ROW " as *u8); fs_n(r)
1563 fs_w(" score=" as *u8); fs_n(score[r])
1564 fs_w(" " as *u8); fs_wn(mb, rowa[r], rowb[r] - rowa[r]); fs_w("\n" as *u8)
1565 var si: i64 = 0
1566 while si < sn {
1567 let sle: i64 = fs_eol(sb, sn, si)
1568 if fs_is_row(sb, si, sle) == 1 {
1569 let snp: i64 = fs_pipes(sb, si, sle, spos)
1570 if snp >= 8 {
1571 if fs_streq(sb, si, spos[0], "src" as *u8) == 1 {
1572 let kp: i64 = fs_catn(kind, 0, sb, spos[0] + 1, spos[1]); kind[kp] = 0 as u8
1573 // A STATIC LISTING (no {q} in its url: a conference index) cannot be asked a seed, so the scan would
1574 // propose its first max_items papers against EVERY deficit row regardless of subject. It is fan-out
1575 // only: `attest` reads it filtered by each bar's own subject tokens (2026-09-06).
1576 if fs_find(sb, spos[2], spos[1] + 1, "{q}" as *u8) < 0 {
1577 fs_w(" SOURCE-STATIC kind=" as *u8); fs_w(kind); fs_w(" -- no query slot, fan-out only (attest); skipped by scan\n" as *u8)
1578 } else {
1579 // THE JOIN IS THE SOURCE'S OWN OPERATOR, so the seed must be built INSIDE this loop.
1580 // arXiv's search_query needs +AND+ between terms or it matches the whole archive and
1581 // returns the newest submissions with HTTP 200; HN Algolia wants a plain +.
1582 // a DATED row (attest, 2026-09-06) carries two more fields after the join, so the join ends at the
1583 // ninth pipe when there is one; an eight-pipe row still runs to the end of its line
1584 var je: i64 = sle
1585 if snp >= 9 { je = spos[8] }
1586 let jp: i64 = fs_catn(join, 0, sb, spos[7] + 1, je); join[jp] = 0 as u8
1587 fs_seed_q(qbuf, mb, rowa[r], rowb[r], FS_URLCAP, seed_tokens, min_token, join)
1588 fs_w(" seed kind=" as *u8); fs_w(kind); fs_w(" q=" as *u8); fs_w(qbuf); fs_w("\n" as *u8)
1589 var up: i64 = 0
1590 var t: i64 = spos[1] + 1
1591 while t < spos[2] {
1592 var sub: i64 = 0
1593 if sb[t] == (FS_LBRACE as u8) {
1594 if fs_prefix(sb, t, spos[2], "{q}" as *u8) == 1 { sub = 1 }
1595 }
1596 if sub == 1 { up = fs_cat(url, up, qbuf); t = t + 3 }
1597 else { url[up] = sb[t]; up = up + 1; t = t + 1 }
1598 }
1599 url[up] = 0 as u8
1600 let q1: i64 = fs_catn(idlm, 0, sb, spos[2] + 1, spos[3]); idlm[q1] = 0 as u8
1601 let q2: i64 = fs_catn(topen, 0, sb, spos[3] + 1, spos[4]); topen[q2] = 0 as u8
1602 let q3: i64 = fs_catn(tclos, 0, sb, spos[4] + 1, spos[5]); tclos[q3] = 0 as u8
1603 let q4: i64 = fs_catn(uopen, 0, sb, spos[5] + 1, spos[6]); uopen[q4] = 0 as u8
1604 let q5: i64 = fs_catn(uclos, 0, sb, spos[6] + 1, spos[7]); uclos[q5] = 0 as u8
1605 fetches = fetches + 1
1606 let got: i64 = fs_scan_source(dbase, kind, url, idlm, topen, tclos, uopen, uclos,
1607 corp, corpn, refs, refsn, mb, rowa[r], rowb[r],
1608 max_items, cover_permil, min_token,
1609 wl, wlout, tok, savepath, olen, rlen)
1610 total_props = total_props + got
1611 }
1612 }
1613 }
1614 }
1615 si = sle + 1
1616 }
1617 r = r + 1
1618 }
1619 let wp: i64 = wlout[0]
1620
1621 fs_w("SCAN-DONE domain=" as *u8); fs_w(dom)
1622 fs_w(" deficit_rows=" as *u8); fs_n(nd)
1623 fs_w(" rows_scanned=" as *u8); fs_n(max_rows)
1624 fs_w(" sources=" as *u8); fs_n(nsrc)
1625 fs_w(" fetches=" as *u8); fs_n(fetches)
1626 fs_w(" PROPOSALS=" as *u8); fs_n(total_props); fs_w("\n" as *u8)
1627
1628 if filing == 1 {
1629 // FIXTURE MODE IS HERMETIC. A path-form domain writes its worklist and its plane rows under
1630 // /tmp, never into knowledge/status or the production frontierprop- plane. This is not tidiness:
1631 // A GATE MUST NOT SHARE ITS FIXTURE WITH A PRODUCTION BEAT, and the first cut of this organ had
1632 // its own gate filing fixture rows into the live plane and blocking on that plane's flock.
1633 var fixture: i64 = 0
1634 if fs_has_slash(dom) == 1 { fixture = 1 }
1635 let fld: *u8 = sys_mmap(FS_PATHCAP)
1636 let plane: *u8 = sys_mmap(FS_PATHCAP)
1637 var lp: i64 = 0
1638 var pp: i64 = 0
1639 if fixture == 1 {
1640 lp = fs_cat(fld, 0, "/tmp/nx_frontier_scan/frontier_proposals_" as *u8)
1641 pp = fs_cat(plane, 0, "/tmp/nx_frontier_scan/fxprop-" as *u8)
1642 fs_w("FIXTURE-MODE worklist and plane are redirected under /tmp -- no production surface is touched\n" as *u8)
1643 } else {
1644 // the worklist is named for the domain's BASENAME, so a real domain always lands on its own
1645 // stable, regenerated-each-run file.
1646 lp = fs_cat(fld, 0, "knowledge/status/frontier_proposals_" as *u8)
1647 pp = fs_cat(plane, 0, "frontierprop-" as *u8)
1648 }
1649 plane[pp] = 0 as u8
1650 lp = fs_cat(fld, lp, dbase)
1651 lp = fs_cat(fld, lp, ".txt" as *u8)
1652 fld[lp] = 0 as u8
1653 let wrote: i64 = dp_writefile(fld, wl, wp)
1654 fs_w("WORKLIST path=" as *u8); fs_w(fld)
1655 fs_w(" bytes=" as *u8); fs_n(wp)
1656 fs_w(" wrote=" as *u8); fs_n(wrote); fs_w("\n" as *u8)
1657 let ppos: *i64 = sys_mmap(8 * FS_MAXP) as *i64
1658 let idb: *u8 = sys_mmap(FS_TXTCAP)
1659 let f1: *u8 = sys_mmap(FS_LINECAP)
1660 let f2: *u8 = sys_mmap(FS_LINECAP)
1661 let f3: *u8 = sys_mmap(FS_LINECAP)
1662 let f4: *u8 = sys_mmap(FS_LINECAP)
1663 let pargs: *i64 = sys_mmap(8 * (FS_PLANE_ARGS + 2)) as *i64
1664 var pi: i64 = 0
1665 var filed: i64 = 0
1666 while pi < wp {
1667 let ple: i64 = fs_eol(wl, wp, pi)
1668 if ple > pi {
1669 let pnp: i64 = fs_pipes(wl, pi, ple, ppos)
1670 if pnp >= 6 {
1671 var ip: i64 = fs_catn(idb, 0, wl, ppos[0] + 1, ppos[1])
1672 idb[ip] = FS_USCORE as u8; ip = ip + 1
1673 ip = fs_hex(idb, ip, fs_urlhash(wl, ppos[3] + 1, ppos[4]))
1674 idb[ip] = 0 as u8
1675 let g1: i64 = fs_catn(f1, 0, wl, ppos[1] + 1, ppos[2]); f1[g1] = 0 as u8
1676 let g2: i64 = fs_catn(f2, 0, wl, ppos[2] + 1, ppos[3]); f2[g2] = 0 as u8
1677 let g3: i64 = fs_catn(f3, 0, wl, ppos[3] + 1, ppos[4]); f3[g3] = 0 as u8
1678 let g4: i64 = fs_catn(f4, 0, wl, ppos[4] + 1, ppos[5]); f4[g4] = 0 as u8
1679 pargs[0] = plane as i64
1680 pargs[1] = "put" as i64
1681 pargs[2] = "nx_frontier_scan" as i64
1682 pargs[3] = idb as i64
1683 pargs[4] = dbase as i64
1684 pargs[5] = f1 as i64
1685 pargs[6] = f2 as i64
1686 pargs[7] = f3 as i64
1687 pargs[8] = f4 as i64
1688 pargs[9] = "OPEN" as i64
1689 var prc: i64 = dep_run_capture("./nx_store_put.elf" as *u8, pargs, FS_PLANE_ARGS, "/tmp/nx_frontier_scan/put.out" as *u8)
1690 if prc == 127 { prc = dep_run_capture("_offc/nx_store_put.elf" as *u8, pargs, FS_PLANE_ARGS, "/tmp/nx_frontier_scan/put.out" as *u8) }
1691 if prc == 0 { filed = filed + 1 }
1692 }
1693 }
1694 pi = ple + 1
1695 }
1696 fs_w("PLANE " as *u8); fs_w(plane); fs_w(" filed=" as *u8); fs_n(filed)
1697 fs_w(" of=" as *u8); fs_n(total_props); fs_w("\n" as *u8)
1698 }
1699
1700 fs_w("NOTE proposals only -- this organ never writes .matrix, .refs, or a watch cell\n" as *u8)
1701 sys_exit(0)
1702 return 0
1703}