code wiki / _hdl_build / nx_feeder.nx

nx_feeder.nx source

↩ module page · 605 lines · 24193 B

1// nx_feeder.nx -- AUTONOMOUS BACKLOG FEEDER, operational-gap half (ws=ws-intel8, 2026-07-20). 2// The INPUT side of the intake pipeline: until now a request only existed if a human or an 3// agent thought to file one. This organ makes the ECOSYSTEM NOTICE ITS OWN DROPPED THREADS 4// and file its own tickets, from signals it already records: 5// (1) DROPPED WORKSTREAM -- a ws_sync lane with a KICKOFF, no DONE, silent > threshold 6// (2) EXPIRED LEASE -- an nx_claims resource held past its TTL, never released 7// Each finding becomes ONE nx_pm_intake REQ carrying a STABLE MARKER, so a re-run never 8// re-files it (idempotent by construction), and each run is CAPPED so the queue can never 9// be flooded. NOTHING IS FABRICATED: every proposal cites its measured age/holder/checkpoint; 10// a source with no finding files nothing. Downstream: nx_triage routes it, /intake renders it. 11// Composes nx_ws_sync + nx_claims + nx_pm_intake; duplicates none of them. 12// F208 COORDINATION: the ATLAS-sourced feeder half (maturity-gap x momentum -> F-rows, 13// researcher+architect, nx_atlas_discover/recombine) is NOT claimed here -- different 14// sources, complementary; this is the operational-gap half owned by the coordination lane. 15// SCALE LAW: windowed reads + declared envelope (window_bytes/truncated/filed/skipped/capped). 16// license_tier: ORIGINAL No hw writes (Rule 26). 17// scan <ws_jrnl> <claims_jrnl> <intake_jrnl> -> JSON findings (READ-ONLY) 18// run <ws_jrnl> <claims_jrnl> <intake_jrnl> <actor> -> file them, JSON summary 19// selftest <scratch-journal> -> gate T1..T8 (pre-cleaned) 20import "nx_syscalls.nx" 21import "nx_gate_verdict.nx" 22const FD_MAGIC_4096: i64 = 4096 23const FD_MAGIC_2000: i64 = 2000 24const FD_MAGIC_1024: i64 = 1024 25const FD_MAGIC_1000000: i64 = 1000000 26 27const FD_WIN: i64 = 4194304 28const FD_OUT: i64 = 262144 29const FD_SOFT: i64 = 49152 30const FD_NOTE: i64 = 1024 31const FD_NAME_MAX: i64 = 120 32const FD_NOTE_MAX: i64 = 200 33// POLICY (residual: promote to config rows): a lane silent this long with no DONE is dropped. 34const FD_DROPPED_SEC: i64 = 172800 35const FD_MAXFILE: i64 = 5 36 37func fd_cat(d: *u8, o: i64, s: *u8) -> i64 { var p: i64=o; var i: i64=0; while s[i]!=(0 as u8){ d[p]=s[i]; p=p+1; i=i+1 } return p } 38func fd_catn(d: *u8, o: i64, v: i64) -> i64 { 39 let t: *u8 = sys_mmap(28) 40 var m: i64 = v 41 var p: i64 = o 42 if m < 0 { d[p]=45 as u8; p=p+1; m=0-m } 43 var k: i64 = 0 44 if m == 0 { t[0]=48 as u8; k=1 } 45 while m > 0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 46 var i: i64 = 0 47 while i < k { d[p]=t[k-1-i]; p=p+1; i=i+1 } 48 return p 49} 50func fd_vlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 51func fd_read(path: *u8, buf: *u8, cap: i64) -> i64 { 52 let fd: i64 = sys_openat_rd(path) 53 if fd < 0 { return 0 } 54 var n: i64 = 0 55 var go: i64 = 1 56 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64)+n) as *u8, cap-n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 57 sys_close(fd) 58 return n 59} 60func fd_le(q: *u8, i: i64, n: i64) -> i64 { 61 var le: i64 = i 62 var s: i64 = 1 63 while s==1 { if le>=n { s=0 } else { if q[le]==(10 as u8){s=0} else {le=le+1} } } 64 return le 65} 66func fd_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 { 67 var col: i64 = 0 68 var p: i64 = ls 69 while col < c { 70 var s: i64 = 1 71 while s == 1 { if p >= le { return 0 } if q[p]==(9 as u8) { s = 0 } else { p = p+1 } } 72 p = p + 1 73 col = col + 1 74 } 75 var e: i64 = p 76 var s2: i64 = 1 77 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e]==(9 as u8) { s2 = 0 } else { e = e+1 } } } 78 out[0] = p 79 out[1] = e 80 return 1 81} 82func fd_lit_eq(q: *u8, s: i64, e: i64, lit: *u8) -> i64 { 83 var i: i64 = 0 84 while s+i < e { if lit[i]==(0 as u8) { return 0 } if q[s+i]!=lit[i] { return 0 } i=i+1 } 85 if lit[i]!=(0 as u8) { return 0 } 86 return 1 87} 88func fd_span_eq(q: *u8, s1: i64, e1: i64, s2: i64, e2: i64) -> i64 { 89 if e1-s1 != e2-s2 { return 0 } 90 var i: i64 = 0 91 while s1+i < e1 { if q[s1+i]!=q[s2+i] { return 0 } i=i+1 } 92 return 1 93} 94func fd_atoi(q: *u8, s: i64, e: i64) -> i64 { 95 var v: i64 = 0 96 var i: i64 = s 97 while i < e { let c: i64 = q[i] as i64; if c>=48 { if c<=57 { v = v*10 + (c-48) } } i=i+1 } 98 return v 99} 100// substring search of a NUL-terminated needle inside a buffer 101func fd_find(d: *u8, dn: i64, lit: *u8) -> i64 { 102 let ll: i64 = fd_vlen(lit) 103 if ll == 0 { return 0 } 104 var i: i64 = 0 105 while i + ll <= dn { 106 var k: i64 = 0 107 var ok: i64 = 1 108 while k < ll { if d[i+k]!=lit[k] { ok=0; k=ll } else { k=k+1 } } 109 if ok==1 { return 1 } 110 i = i + 1 111 } 112 return 0 113} 114// append an nx_pm_intake REQ frame (contract: <ts>\t<VERB>\t<id>\t<actor>\t<text>) 115func fd_append_req(journal: *u8, id: i64, actor: *u8, note: *u8) -> i64 { 116 let ln: *u8 = sys_mmap(FD_MAGIC_4096) 117 var o: i64 = 0 118 o = fd_catn(ln, o, id) 119 ln[o]=9 as u8; o=o+1 120 o = fd_cat(ln, o, "REQ" as *u8) 121 ln[o]=9 as u8; o=o+1 122 o = fd_catn(ln, o, id) 123 ln[o]=9 as u8; o=o+1 124 o = fd_cat(ln, o, actor) 125 ln[o]=9 as u8; o=o+1 126 var i: i64 = 0 127 var used: i64 = 0 128 while note[i]!=(0 as u8) { 129 if used < FD_MAGIC_2000 { 130 let c: i64 = note[i] as i64 131 if c==9 { ln[o]=32 as u8; o=o+1 } else { if c==10 { ln[o]=32 as u8; o=o+1 } else { if c==13 { ln[o]=32 as u8; o=o+1 } else { ln[o]=c as u8; o=o+1 } } } 132 used = used + 1 133 } 134 i = i + 1 135 } 136 ln[o]=10 as u8; o=o+1 137 let fd: i64 = sys_openat_append(journal, 0x1a4) 138 if fd < 0 { return -1 } 139 sys_write(fd, ln, o) 140 sys_close(fd) 141 return 0 142} 143// does the intake journal already carry this REQ id? (collision-safe id minting) 144func fd_id_taken(q: *u8, n: i64, idv: i64) -> i64 { 145 let ci: *i64 = sys_mmap(16) as *i64 146 var i: i64 = 0 147 while i < n { 148 let le: i64 = fd_le(q,i,n) 149 if fd_col(q,i,le,2,ci)==1 { let v: i64 = fd_atoi(q,ci[0],ci[1]); if v==idv { return 1 } } 150 i = le + 1 151 } 152 return 0 153} 154 155// ---------- source 1: dropped workstreams (ws_sync contract) ---------- 156func fd_ws_first_kick(q: *u8, upto: i64, ws_s: i64, ws_e: i64) -> i64 { 157 let cv: *i64 = sys_mmap(16) as *i64 158 let cw: *i64 = sys_mmap(16) as *i64 159 var i: i64 = 0 160 while i < upto { 161 let le: i64 = fd_le(q,i,upto) 162 if fd_col(q,i,le,1,cv)==1 { if fd_lit_eq(q,cv[0],cv[1],"KICKOFF" as *u8)==1 { 163 if fd_col(q,i,le,2,cw)==1 { if fd_span_eq(q,cw[0],cw[1],ws_s,ws_e)==1 { return 0 } } 164 } } 165 i = le + 1 166 } 167 return 1 168} 169func fd_ws_has(q: *u8, n: i64, verb: *u8, ws_s: i64, ws_e: i64) -> i64 { 170 let cv: *i64 = sys_mmap(16) as *i64 171 let cw: *i64 = sys_mmap(16) as *i64 172 var i: i64 = 0 173 while i < n { 174 let le: i64 = fd_le(q,i,n) 175 if fd_col(q,i,le,1,cv)==1 { if fd_lit_eq(q,cv[0],cv[1],verb)==1 { 176 if fd_col(q,i,le,2,cw)==1 { if fd_span_eq(q,cw[0],cw[1],ws_s,ws_e)==1 { return 1 } } 177 } } 178 i = le + 1 179 } 180 return 0 181} 182func fd_ws_last_ts(q: *u8, n: i64, ws_s: i64, ws_e: i64) -> i64 { 183 let cw: *i64 = sys_mmap(16) as *i64 184 let c0: *i64 = sys_mmap(16) as *i64 185 var last: i64 = -1 186 var i: i64 = 0 187 while i < n { 188 let le: i64 = fd_le(q,i,n) 189 if fd_col(q,i,le,2,cw)==1 { if fd_span_eq(q,cw[0],cw[1],ws_s,ws_e)==1 { 190 if fd_col(q,i,le,0,c0)==1 { last = fd_atoi(q,c0[0],c0[1]) } 191 } } 192 i = le + 1 193 } 194 return last 195} 196func fd_ws_last_note(q: *u8, n: i64, ws_s: i64, ws_e: i64, out: *i64) -> i64 { 197 let cw: *i64 = sys_mmap(16) as *i64 198 let cn: *i64 = sys_mmap(16) as *i64 199 var ns: i64 = -1 200 var ne: i64 = -1 201 var i: i64 = 0 202 while i < n { 203 let le: i64 = fd_le(q,i,n) 204 if fd_col(q,i,le,2,cw)==1 { if fd_span_eq(q,cw[0],cw[1],ws_s,ws_e)==1 { 205 if fd_col(q,i,le,4,cn)==1 { ns=cn[0]; ne=cn[1] } 206 } } 207 i = le + 1 208 } 209 if ns >= 0 { out[0]=ns; out[1]=ne; return 1 } 210 return 0 211} 212// is this lane DROPPED? (no DONE frame AND silent longer than the threshold) 213func fd_ws_dropped(q: *u8, n: i64, now: i64, ws_s: i64, ws_e: i64) -> i64 { 214 if fd_ws_has(q,n,"DONE" as *u8,ws_s,ws_e)==1 { return 0 } 215 let lt: i64 = fd_ws_last_ts(q,n,ws_s,ws_e) 216 if lt < 0 { return 0 } 217 if now - lt > FD_DROPPED_SEC { return 1 } 218 return 0 219} 220 221// ---------- source 2: expired leases (nx_claims contract) ---------- 222// state: -1 absent / 1 HELD / 2 EXPIRED / 3 FREE; out[0..1]=holder span, out[2]=ttl, out[3]=last 223func fd_cl_state(q: *u8, n: i64, now: i64, res_s: i64, res_e: i64, out: *i64) -> i64 { 224 let cv: *i64 = sys_mmap(16) as *i64 225 let cr: *i64 = sys_mmap(16) as *i64 226 let ca: *i64 = sys_mmap(16) as *i64 227 let ct: *i64 = sys_mmap(16) as *i64 228 let c0: *i64 = sys_mmap(16) as *i64 229 var mode: i64 = 0 230 var ever: i64 = 0 231 var hs: i64 = -1 232 var he: i64 = -1 233 var ttl: i64 = 0 234 var last: i64 = 0 235 var i: i64 = 0 236 while i < n { 237 let le: i64 = fd_le(q,i,n) 238 if fd_col(q,i,le,2,cr)==1 { if fd_span_eq(q,cr[0],cr[1],res_s,res_e)==1 { 239 if fd_col(q,i,le,1,cv)==1 { if fd_col(q,i,le,0,c0)==1 { if fd_col(q,i,le,3,ca)==1 { 240 let ts: i64 = fd_atoi(q,c0[0],c0[1]) 241 if fd_lit_eq(q,cv[0],cv[1],"CLAIM" as *u8)==1 { 242 mode=1 243 ever=1 244 hs=ca[0] 245 he=ca[1] 246 last=ts 247 if fd_col(q,i,le,4,ct)==1 { ttl = fd_atoi(q,ct[0],ct[1]) } 248 } 249 if fd_lit_eq(q,cv[0],cv[1],"BEAT" as *u8)==1 { if mode==1 { if fd_span_eq(q,ca[0],ca[1],hs,he)==1 { last=ts } } } 250 if fd_lit_eq(q,cv[0],cv[1],"RELEASE" as *u8)==1 { if mode==1 { if fd_span_eq(q,ca[0],ca[1],hs,he)==1 { mode=0 } } } 251 } } } 252 } } 253 i = le + 1 254 } 255 if ever==0 { return -1 } 256 if mode==0 { return 3 } 257 out[0]=hs 258 out[1]=he 259 out[2]=ttl 260 out[3]=last 261 if now - last > ttl { return 2 } 262 return 1 263} 264func fd_res_seen(q: *u8, upto: i64, r_s: i64, r_e: i64) -> i64 { 265 let cr: *i64 = sys_mmap(16) as *i64 266 var i: i64 = 0 267 while i < upto { 268 let le: i64 = fd_le(q,i,upto) 269 if fd_col(q,i,le,2,cr)==1 { if fd_span_eq(q,cr[0],cr[1],r_s,r_e)==1 { return 1 } } 270 i = le + 1 271 } 272 return 0 273} 274 275// ---------- proposal note builders (marker = the dedupe key) ---------- 276func fd_note_dropped(nb: *u8, q: *u8, ws_s: i64, ws_e: i64, age_min: i64, note_s: i64, note_e: i64, hasnote: i64) -> i64 { 277 var p: i64 = 0 278 p = fd_cat(nb,p,"DROPPED WORKSTREAM (measured by nx_feeder): ws=" as *u8) 279 var i: i64 = ws_s 280 while i < ws_e { nb[p]=q[i]; p=p+1; i=i+1 } 281 p = fd_cat(nb,p," has a KICKOFF but NO DONE frame and has been silent " as *u8) 282 p = fd_catn(nb,p,age_min) 283 p = fd_cat(nb,p," min. Decide: resume it, hand it off, or close it with a DONE frame. Last checkpoint: " as *u8) 284 if hasnote==1 { 285 var j: i64 = note_s 286 var used: i64 = 0 287 while j < note_e { if used < FD_NOTE_MAX { let c: i64 = q[j] as i64; if c==9 { nb[p]=32 as u8 } else { nb[p]=c as u8 } p=p+1; used=used+1 } j=j+1 } 288 } 289 p = fd_cat(nb,p," [FEEDER:dropped-ws:" as *u8) 290 var k: i64 = ws_s 291 while k < ws_e { nb[p]=q[k]; p=p+1; k=k+1 } 292 p = fd_cat(nb,p,"]" as *u8) 293 nb[p]=0 as u8 294 return p 295} 296func fd_note_expired(nb: *u8, q: *u8, r_s: i64, r_e: i64, h_s: i64, h_e: i64, over_sec: i64, ttl: i64) -> i64 { 297 var p: i64 = 0 298 p = fd_cat(nb,p,"EXPIRED LEASE (measured by nx_feeder): resource=" as *u8) 299 var i: i64 = r_s 300 while i < r_e { nb[p]=q[i]; p=p+1; i=i+1 } 301 p = fd_cat(nb,p," held by " as *u8) 302 var j: i64 = h_s 303 while j < h_e { nb[p]=q[j]; p=p+1; j=j+1 } 304 p = fd_cat(nb,p," is " as *u8) 305 p = fd_catn(nb,p,over_sec) 306 p = fd_cat(nb,p,"s past its " as *u8) 307 p = fd_catn(nb,p,ttl) 308 p = fd_cat(nb,p,"s TTL with no RELEASE (crash-expiry). Reap it or re-claim it. [FEEDER:expired-claim:" as *u8) 309 var k: i64 = r_s 310 while k < r_e { nb[p]=q[k]; p=p+1; k=k+1 } 311 p = fd_cat(nb,p,"]" as *u8) 312 nb[p]=0 as u8 313 return p 314} 315// marker-only string, for the dedupe probe 316func fd_marker_dropped(mb: *u8, q: *u8, ws_s: i64, ws_e: i64) -> i64 { 317 var p: i64 = 0 318 p = fd_cat(mb,p,"[FEEDER:dropped-ws:" as *u8) 319 var i: i64 = ws_s 320 while i < ws_e { mb[p]=q[i]; p=p+1; i=i+1 } 321 p = fd_cat(mb,p,"]" as *u8) 322 mb[p]=0 as u8 323 return p 324} 325func fd_marker_expired(mb: *u8, q: *u8, r_s: i64, r_e: i64) -> i64 { 326 var p: i64 = 0 327 p = fd_cat(mb,p,"[FEEDER:expired-claim:" as *u8) 328 var i: i64 = r_s 329 while i < r_e { mb[p]=q[i]; p=p+1; i=i+1 } 330 p = fd_cat(mb,p,"]" as *u8) 331 mb[p]=0 as u8 332 return p 333} 334 335// ---------- the sweep: mode 0 = count/report only, mode 1 = file ---------- 336// cx[0]=filed cx[1]=skipped_dup cx[2]=capped cx[3]=findings cx[4]=next_id 337func fd_sweep(wq: *u8, wn: i64, cq: *u8, cn: i64, iq: *u8, iN: i64, ij: *u8, actor: *u8, now: i64, mode: i64, maxfile: i64, cx: *i64) -> i64 { 338 cx[0]=0 339 cx[1]=0 340 cx[2]=0 341 cx[3]=0 342 let cv: *i64 = sys_mmap(16) as *i64 343 let cw: *i64 = sys_mmap(16) as *i64 344 let cr: *i64 = sys_mmap(16) as *i64 345 let st: *i64 = sys_mmap(64) as *i64 346 let no: *i64 = sys_mmap(16) as *i64 347 let nb: *u8 = sys_mmap(FD_NOTE) 348 let mb: *u8 = sys_mmap(512) 349 var nextid: i64 = now 350 // source 1: dropped workstreams 351 var i: i64 = 0 352 while i < wn { 353 let le: i64 = fd_le(wq,i,wn) 354 if fd_col(wq,i,le,1,cv)==1 { if fd_lit_eq(wq,cv[0],cv[1],"KICKOFF" as *u8)==1 { 355 if fd_col(wq,i,le,2,cw)==1 { if fd_ws_first_kick(wq,i,cw[0],cw[1])==1 { 356 if fd_ws_dropped(wq,wn,now,cw[0],cw[1])==1 { 357 cx[3] = cx[3] + 1 358 fd_marker_dropped(mb,wq,cw[0],cw[1]) 359 if fd_find(iq,iN,mb)==1 { cx[1] = cx[1] + 1 } else { 360 if cx[0] >= maxfile { cx[2] = cx[2] + 1 } else { 361 if mode == 1 { 362 let lt: i64 = fd_ws_last_ts(wq,wn,cw[0],cw[1]) 363 let agem: i64 = (now - lt) / 60 364 let hn: i64 = fd_ws_last_note(wq,wn,cw[0],cw[1],no) 365 fd_note_dropped(nb,wq,cw[0],cw[1],agem,no[0],no[1],hn) 366 var go: i64 = 1 367 while go == 1 { if fd_id_taken(iq,iN,nextid)==1 { nextid = nextid + 1 } else { go = 0 } } 368 let rc: i64 = fd_append_req(ij,nextid,actor,nb) 369 if rc==0 { cx[0] = cx[0] + 1; nextid = nextid + 1 } 370 } else { cx[0] = cx[0] + 1 } 371 } 372 } 373 } 374 } } 375 } } 376 i = le + 1 377 } 378 // source 2: expired leases 379 var j: i64 = 0 380 while j < cn { 381 let je: i64 = fd_le(cq,j,cn) 382 if fd_col(cq,j,je,2,cr)==1 { if fd_res_seen(cq,j,cr[0],cr[1])==0 { 383 let s: i64 = fd_cl_state(cq,cn,now,cr[0],cr[1],st) 384 if s == 2 { 385 cx[3] = cx[3] + 1 386 fd_marker_expired(mb,cq,cr[0],cr[1]) 387 if fd_find(iq,iN,mb)==1 { cx[1] = cx[1] + 1 } else { 388 if cx[0] >= maxfile { cx[2] = cx[2] + 1 } else { 389 if mode == 1 { 390 let over: i64 = (now - st[3]) - st[2] 391 fd_note_expired(nb,cq,cr[0],cr[1],st[0],st[1],over,st[2]) 392 var go2: i64 = 1 393 while go2 == 1 { if fd_id_taken(iq,iN,nextid)==1 { nextid = nextid + 1 } else { go2 = 0 } } 394 let rc2: i64 = fd_append_req(ij,nextid,actor,nb) 395 if rc2==0 { cx[0] = cx[0] + 1; nextid = nextid + 1 } 396 } else { cx[0] = cx[0] + 1 } 397 } 398 } 399 } 400 } } 401 j = je + 1 402 } 403 cx[4] = nextid 404 return cx[0] 405} 406func fd_key(d: *u8, o: i64, name: *u8) -> i64 { 407 d[o]=34 as u8 408 var p: i64 = o + 1 409 p = fd_cat(d,p,name) 410 d[p]=34 as u8 411 p = p + 1 412 d[p]=58 as u8 413 p = p + 1 414 return p 415} 416func fd_qlit(d: *u8, o: i64, s: *u8) -> i64 { 417 var p: i64 = o 418 d[p]=34 as u8; p=p+1 419 p = fd_cat(d,p,s) 420 d[p]=34 as u8; p=p+1 421 return p 422} 423func fd_summary(d: *u8, modename: *u8, cx: *i64, wn: i64, cn: i64, iN: i64, trunc: i64, maxfile: i64) -> i64 { 424 var p: i64 = 0 425 d[p]=123 as u8; p=p+1 426 p = fd_key(d,p,"v" as *u8) 427 p = fd_catn(d,p,1) 428 d[p]=44 as u8; p=p+1 429 p = fd_key(d,p,"tool" as *u8) 430 p = fd_qlit(d,p,"nx_feeder" as *u8) 431 d[p]=44 as u8; p=p+1 432 p = fd_key(d,p,"mode" as *u8) 433 p = fd_qlit(d,p,modename) 434 d[p]=44 as u8; p=p+1 435 p = fd_key(d,p,"findings" as *u8) 436 p = fd_catn(d,p,cx[3]) 437 d[p]=44 as u8; p=p+1 438 p = fd_key(d,p,"filed" as *u8) 439 p = fd_catn(d,p,cx[0]) 440 d[p]=44 as u8; p=p+1 441 p = fd_key(d,p,"skipped_already_filed" as *u8) 442 p = fd_catn(d,p,cx[1]) 443 d[p]=44 as u8; p=p+1 444 p = fd_key(d,p,"capped" as *u8) 445 p = fd_catn(d,p,cx[2]) 446 d[p]=44 as u8; p=p+1 447 p = fd_key(d,p,"max_per_run" as *u8) 448 p = fd_catn(d,p,maxfile) 449 d[p]=44 as u8; p=p+1 450 p = fd_key(d,p,"dropped_after_sec" as *u8) 451 p = fd_catn(d,p,FD_DROPPED_SEC) 452 d[p]=44 as u8; p=p+1 453 p = fd_key(d,p,"ws_bytes" as *u8) 454 p = fd_catn(d,p,wn) 455 d[p]=44 as u8; p=p+1 456 p = fd_key(d,p,"claims_bytes" as *u8) 457 p = fd_catn(d,p,cn) 458 d[p]=44 as u8; p=p+1 459 p = fd_key(d,p,"intake_bytes" as *u8) 460 p = fd_catn(d,p,iN) 461 d[p]=44 as u8; p=p+1 462 p = fd_key(d,p,"window_bytes" as *u8) 463 p = fd_catn(d,p,FD_WIN) 464 d[p]=44 as u8; p=p+1 465 p = fd_key(d,p,"truncated" as *u8) 466 p = fd_catn(d,p,trunc) 467 d[p]=125 as u8; p=p+1 468 d[p]=10 as u8; p=p+1 469 return p 470} 471func fd_mkpath(dst: *u8, src: *u8, suf: *u8) -> i64 { 472 var p: i64 = 0 473 p = fd_cat(dst,p,src) 474 p = fd_cat(dst,p,suf) 475 dst[p]=0 as u8 476 return p 477} 478func fd_wr(path: *u8, body: *u8) -> i64 { 479 let fd: i64 = sys_openat_wr(path, 0x1a4) 480 if fd < 0 { return -1 } 481 sys_write(fd, body, fd_vlen(body)) 482 sys_close(fd) 483 return 0 484} 485 486// ---------- selftest gate (T1..T8) ---------- 487func fd_selftest(base: *u8) -> i64 { 488 let ctr: *i64 = gv_ctr() 489 gv_head("nx_feeder selftest -- autonomous gap-detection teeth (caller pre-cleans paths)" as *u8) 490 let wq: *u8 = sys_mmap(FD_WIN) 491 let cq: *u8 = sys_mmap(FD_WIN) 492 let iq: *u8 = sys_mmap(FD_WIN) 493 let cx: *i64 = sys_mmap(64) as *i64 494 let wp: *u8 = sys_mmap(FD_MAGIC_1024) 495 let cp: *u8 = sys_mmap(FD_MAGIC_1024) 496 let ip: *u8 = sys_mmap(FD_MAGIC_1024) 497 let rcap: i64 = FD_WIN - 8 498 let now: i64 = FD_MAGIC_1000000 499 fd_mkpath(wp, base, ".ws" as *u8) 500 fd_mkpath(cp, base, ".claims" as *u8) 501 fd_mkpath(ip, base, ".intake" as *u8) 502 // T1 empty sources -> nothing found, nothing filed 503 var wn: i64 = fd_read(wp,wq,rcap) 504 var cn: i64 = fd_read(cp,cq,rcap) 505 var iN: i64 = fd_read(ip,iq,rcap) 506 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"t" as *u8,now,0,FD_MAXFILE,cx) 507 var ok1: i64 = 0 508 if cx[3]==0 { if cx[0]==0 { ok1=1 } } 509 gv_check("T1 empty sources find nothing fabricate nothing" as *u8, ok1, ctr) 510 // seed ws: laneDrop (old kickoff, no done) - laneDone (old, DONE) - laneFresh (recent, no done) 511 fd_wr(wp, "100\tKICKOFF\tlaneDrop\ts1\tstarted the drop lane\n200\tBEAT\tlaneDrop\ts1\tlast checkpoint here\n100\tKICKOFF\tlaneDone\ts2\tstarted\n300\tDONE\tlaneDone\ts2\tfinished cleanly\n999000\tKICKOFF\tlaneFresh\ts3\tjust started\n" as *u8) 512 // seed claims: resExp (old claim, ttl 10, no release) - resHeld (fresh claim, ttl 100000) 513 fd_wr(cp, "500\tCLAIM\tresExp\tsessA\t10\tclaimed long ago\n999500\tCLAIM\tresHeld\tsessB\t100000\tclaimed just now\n" as *u8) 514 wn = fd_read(wp,wq,rcap) 515 cn = fd_read(cp,cq,rcap) 516 iN = fd_read(ip,iq,rcap) 517 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"t" as *u8,now,0,FD_MAXFILE,cx) 518 // T2 exactly two findings: laneDrop + resExp 519 var ok2: i64 = 0 520 if cx[3]==2 { ok2=1 } 521 gv_check("T2 finds exactly the dropped lane and expired lease" as *u8, ok2, ctr) 522 // T3/T4/T5 neg-controls proven by the run's written frames 523 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"nx_feeder" as *u8,now,1,FD_MAXFILE,cx) 524 iN = fd_read(ip,iq,rcap) 525 var ok3: i64 = 0 526 if fd_find(iq,iN,"[FEEDER:dropped-ws:laneDrop]" as *u8)==1 { if fd_find(iq,iN,"[FEEDER:dropped-ws:laneDone]" as *u8)==0 { ok3=1 } } 527 gv_check("T3 neg-control DONE lane never proposed" as *u8, ok3, ctr) 528 var ok4: i64 = 0 529 if fd_find(iq,iN,"[FEEDER:dropped-ws:laneFresh]" as *u8)==0 { ok4=1 } 530 gv_check("T4 neg-control fresh lane never proposed" as *u8, ok4, ctr) 531 var ok5: i64 = 0 532 if fd_find(iq,iN,"[FEEDER:expired-claim:resExp]" as *u8)==1 { if fd_find(iq,iN,"[FEEDER:expired-claim:resHeld]" as *u8)==0 { ok5=1 } } 533 gv_check("T5 expired lease proposed held lease never" as *u8, ok5, ctr) 534 // T6 IDEMPOTENCE: a second run files nothing new, marks both as already-filed 535 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"nx_feeder" as *u8,now,1,FD_MAXFILE,cx) 536 var ok6: i64 = 0 537 if cx[0]==0 { if cx[1]==2 { ok6=1 } } 538 gv_check("T6 neg-control rerun files nothing already-filed 2" as *u8, ok6, ctr) 539 // T7 per-run CAP honored (maxfile 1 against a clean intake) 540 fd_wr(ip, "# cleared for the cap tooth\n" as *u8) 541 iN = fd_read(ip,iq,rcap) 542 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"nx_feeder" as *u8,now,1,1,cx) 543 var ok7: i64 = 0 544 if cx[0]==1 { if cx[2]==1 { ok7=1 } } 545 gv_check("T7 per-run cap honored and reported" as *u8, ok7, ctr) 546 // T8 frames are readable under the intake contract (REQ verb + id in cols) 547 iN = fd_read(ip,iq,rcap) 548 let cvv: *i64 = sys_mmap(16) as *i64 549 var reqs: i64 = 0 550 var z: i64 = 0 551 while z < iN { 552 let ze: i64 = fd_le(iq,z,iN) 553 if fd_col(iq,z,ze,1,cvv)==1 { if fd_lit_eq(iq,cvv[0],cvv[1],"REQ" as *u8)==1 { reqs = reqs + 1 } } 554 z = ze + 1 555 } 556 var ok8: i64 = 0 557 if reqs==1 { ok8=1 } 558 gv_check("T8 filed frames parse under the intake REQ contract" as *u8, ok8, ctr) 559 let rc: i64 = gv_verdict("FEEDER-GATE" as *u8, ctr, "autonomous gap-detection teeth green" as *u8) 560 return rc 561} 562 563func main(argc: i64, argv: *i64) -> i64 { 564 if argc < 3 { gv_puts("usage: nx_feeder {scan <ws_jrnl> <claims_jrnl> <intake_jrnl> | run <ws_jrnl> <claims_jrnl> <intake_jrnl> <actor> | selftest <base>}\n" as *u8); sys_exit(2); return 2 } 565 let verb: *u8 = argv[1] as *u8 566 let vl: i64 = fd_vlen(verb) 567 if fd_lit_eq(verb,0,vl,"selftest" as *u8)==1 { let rc: i64 = fd_selftest(argv[2] as *u8); sys_exit(rc); return rc } 568 if argc < 5 { gv_puts("scan|run need <ws_jrnl> <claims_jrnl> <intake_jrnl>\n" as *u8); sys_exit(2); return 2 } 569 let wp: *u8 = argv[2] as *u8 570 let cp: *u8 = argv[3] as *u8 571 let ip: *u8 = argv[4] as *u8 572 let wq: *u8 = sys_mmap(FD_WIN) 573 let cq: *u8 = sys_mmap(FD_WIN) 574 let iq: *u8 = sys_mmap(FD_WIN) 575 let rcap: i64 = FD_WIN - 8 576 let wn: i64 = fd_read(wp,wq,rcap) 577 let cn: i64 = fd_read(cp,cq,rcap) 578 let iN: i64 = fd_read(ip,iq,rcap) 579 var trunc: i64 = 0 580 if wn >= rcap { trunc = 1 } 581 if cn >= rcap { trunc = 1 } 582 if iN >= rcap { trunc = 1 } 583 let now: i64 = sys_now_realtime_sec() 584 let cx: *i64 = sys_mmap(64) as *i64 585 let d: *u8 = sys_mmap(FD_OUT) 586 if fd_lit_eq(verb,0,vl,"scan" as *u8)==1 { 587 fd_sweep(wq,wn,cq,cn,iq,iN,ip,"scan" as *u8,now,0,FD_MAXFILE,cx) 588 let dl: i64 = fd_summary(d,"scan" as *u8,cx,wn,cn,iN,trunc,FD_MAXFILE) 589 sys_write(1,d,dl) 590 sys_exit(0) 591 return 0 592 } 593 if fd_lit_eq(verb,0,vl,"run" as *u8)==1 { 594 if argc < 6 { gv_puts("run needs <ws_jrnl> <claims_jrnl> <intake_jrnl> <actor>\n" as *u8); sys_exit(2); return 2 } 595 let actor: *u8 = argv[5] as *u8 596 fd_sweep(wq,wn,cq,cn,iq,iN,ip,actor,now,1,FD_MAXFILE,cx) 597 let dl: i64 = fd_summary(d,"run" as *u8,cx,wn,cn,iN,trunc,FD_MAXFILE) 598 sys_write(1,d,dl) 599 sys_exit(0) 600 return 0 601 } 602 gv_puts("unknown verb\n" as *u8) 603 sys_exit(2) 604 return 2 605}