code wiki / _hdl_build / nx_ws_cycle.nx

nx_ws_cycle.nx source

↩ module page · 697 lines · 35591 B

1// nx_ws_cycle.nx -- THE AUTONOMOUS LOOP conductor organ, R1 READ-ONLY (design-of-record 2// project-nishi-autonomous-loop-design-2026-07-18). Derives a workstream's phase + next_action 3// from MACHINE ARTIFACTS ONLY (the ws_sync journal + the debt store) -- no self-report advances 4// anything. DEBT-GATE BY CONSTRUCTION: while any open debt row exists, next NEVER emits WORK. 5// nx_ws_cycle seedphases [cycleprefix] (idempotent: CONFIG + 13 phase-contract rows) 6// nx_ws_cycle phases [cycleprefix] (dump the loop contract) 7// nx_ws_cycle board [journal] [debtprefix] [cycleprefix] (JSON: every ws + derived phase, scoped gate) 8// nx_ws_cycle next <ws> [journal] [debtprefix] [cycleprefix] (JSON: the one next action, scoped-gated) 9// nx_ws_cycle phase <ws> [tool] [page] [journal] [debtprefix] [cycleprefix] (R2b EVIDENCE ladder: 10// derived from artifacts only -- journal frames, scoped debt state, a tool_allowlist.conf row 11// (EXPOSE proven), a published file's bytes (PUBLISH proven). The caller can only POINT at 12// artifacts, never assert a phase; a missing artifact = the phase is NOT advanced, fail-closed.) 13// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 14import "nx_store_seed_lib.nx" 15import "nx_seg_store.nx" 16import "nx_syscalls.nx" 17const WC_MAGIC_7200: i64 = 7200 18 19// CAPS RAISED (2026-07-30, debt id=1785446819). MEASURED by nx_planecap_gate: knowledge/store/ws- is 20// 1,414,535 bytes -- the workstream COORDINATION plane had outgrown its own reader by 366 KB, so every 21// verdict this organ derived from that plane was silently computed over a TRUNCATED view. 22// Fourth instance of one class in a single session: nx_debt_hygiene (1 MiB -> hid 21 of 23 sev-9 debts), 23// nx_dbthyg (same bug in the superset organ), nx_tooldiff (512 rows -> INVENTED a capability regression), 24// and now this. Every one: a fixed compile-time cap the corpus outgrew, truncated scan presented as answer. 25// THE DANGEROUS PLANES ARE THE ONES THAT RECENTLY CROSSED, NOT THE GIANTS -- a reader of a 174 MB plane 26// must already be sized for it or it would be visibly broken; ws- at 1.35 MB is exactly the size where a 27// verdict goes quietly wrong while everything still reports green. 28const WC_CAP: i64 = 33554432 29const WC_JCAP: i64 = 33554432 30const WC_SEEK_SET: i64 = 0 31const WC_SEEK_END: i64 = 2 32const WC_TAB: i64 = 9 33const WC_NL: i64 = 10 34const WC_STDERR: i64 = 2 35const WC_SPAN: i64 = 16 36const WC_EXIT_USAGE: i64 = 2 37const WC_EXIT_MMAP: i64 = 9 38const WC_MMAP_ERRMAX: i64 = 4096 39 40func wc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 41func wc_werr(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(WC_STDERR, s, n); return 0 } 42// CHECKED allocator (seq350, INCIDENT-0720): on the OOM'd host sys_mmap returned -ENOMEM and every 43// caller wrote to the error page -> segfault at 0xfffffffffffffff4 + multi-GB core dumps. An 44// allocation this organ cannot get is UNRECOVERABLE for a one-shot CLI: fail LOUD with a structured 45// constant error (allocation-free by construction -- printing a number here would need memory we 46// just proved we don't have), never a segfault, never a core. 47func wc_mm(n: i64) -> *u8 { 48 let p: *u8 = sys_mmap(n) 49 let v: i64 = p as i64 50 if v < 0 { if v > (0 - WC_MMAP_ERRMAX) { 51 wc_werr("WSCYCLE-RED {\"error\":\"mmap-fail\",\"organ\":\"nx_ws_cycle\",\"hint\":\"host memory exhausted; see debt seq350 / INCIDENT-0720\"}\n" as *u8) 52 sys_exit(WC_EXIT_MMAP) 53 } } 54 return p 55} 56func wc_vlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 57func wc_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { d[o] = s[i]; o = o + 1; i = i + 1 } return o } 58func wc_catn(d: *u8, o: i64, v: i64) -> i64 { let t: *u8 = wc_mm(28); var m: i64 = v; if m < 0 { m = 0 } var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { d[o] = t[k-1-i]; o = o + 1; i = i + 1 } return o } 59// JSON-safe span append: double-quote -> single-quote, backslash -> slash, ctrl -> space 60func wc_cat_esc(d: *u8, o: i64, q: *u8, s: i64, e: i64) -> i64 { 61 var i: i64 = s 62 while i < e { 63 var c: i64 = q[i] as i64 64 if c == 34 { c = 39 } 65 if c == 92 { c = 47 } 66 if c < 32 { c = 32 } 67 d[o] = c as u8 68 o = o + 1 69 i = i + 1 70 } 71 return o 72} 73// HTML-SINK ESCAPER (seq620 ROOT FIX 2026-07-23). wc_cat_esc above is a JSON-STRING escaper (quote, 74// backslash, controls) -- it does NOT neutralise markup. It was being used on the HTML standup sink, so 75// journal callout text flowed RAW into a PUBLIC page: a sibling's note containing <main>/<nav> corrupted 76// the markup, and a note containing <script> would have EXECUTED. Context-correct escaping is the rule: 77// the JSON sink keeps wc_cat_esc (rule 19, contract unchanged), the HTML sink uses this. Bounded write. 78func wc_cat_esc_html(d: *u8, o: i64, q: *u8, s: i64, e: i64) -> i64 { 79 var i: i64 = s 80 var w: i64 = o 81 while i < e { 82 if w > WC_CAP - 16 { i = e } else { 83 let c: i64 = q[i] as i64 84 if c == 38 { w = wc_cat(d, w, "&amp;" as *u8) } else { if c == 60 { w = wc_cat(d, w, "&lt;" as *u8) } else { if c == 62 { w = wc_cat(d, w, "&gt;" as *u8) } else { if c == 34 { w = wc_cat(d, w, "&quot;" as *u8) } else { if c < 32 { d[w] = 32 as u8; w = w + 1 } else { d[w] = c as u8; w = w + 1 } } } } } 85 i = i + 1 86 } 87 } 88 return w 89} 90// SCALE-LAW read (2026-07-18: "toy things are worse than gaps"): the journal is APPEND-ONLY and 91// unbounded; a head-first capped read would silently drop the NEWEST frames at scale -> false 92// phases (recent DONEs invisible). So: TAIL-window read (lseek size-cap, drop the partial first 93// line) + the caller DECLARES journal_bytes vs window_bytes in every output -- a consumer can 94// never be silently windowed. sizep[0] = the file's true size. 95func wc_read(path: *u8, buf: *u8, cap: i64, sizep: *i64) -> i64 { 96 sizep[0] = 0 97 let fd: i64 = sys_openat_rd(path) 98 if fd < 0 { return 0 } 99 var size: i64 = sys_lseek(fd, 0, WC_SEEK_END) 100 if size < 0 { size = 0 } 101 sizep[0] = size 102 var off: i64 = 0 103 if size > cap { off = size - cap } 104 sys_lseek(fd, off, WC_SEEK_SET) 105 var n: i64 = 0 106 var go: i64 = 1 107 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 } } 108 sys_close(fd) 109 if off > 0 { 110 // drop the partial first line so every frame in the window is whole 111 var fnl: i64 = 0 112 var s: i64 = 1 113 while s == 1 { if fnl >= n { s = 0 } else { if buf[fnl] == (WC_NL as u8) { s = 0 } else { fnl = fnl + 1 } } } 114 if fnl < n { 115 var t: i64 = 0 116 let sk: i64 = fnl + 1 117 while sk + t < n { buf[t] = buf[sk + t]; t = t + 1 } 118 n = t 119 } 120 } 121 return n 122} 123func wc_le(q: *u8, i: i64, n: i64) -> i64 { var e: i64 = i; var s: i64 = 1; while s == 1 { if e >= n { s = 0 } else { if q[e] == (WC_NL as u8) { s = 0 } else { e = e + 1 } } } return e } 124func wc_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 { 125 var col: i64 = 0 126 var p: i64 = ls 127 while col < c { 128 var s: i64 = 1 129 while s == 1 { if p >= le { return 0 } if q[p] == (WC_TAB as u8) { s = 0 } else { p = p + 1 } } 130 p = p + 1 131 col = col + 1 132 } 133 var e: i64 = p 134 var s2: i64 = 1 135 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e] == (WC_TAB as u8) { s2 = 0 } else { e = e + 1 } } } 136 out[0] = p 137 out[1] = e 138 return 1 139} 140func wc_lit_eq(q: *u8, s: i64, e: i64, lit: *u8) -> i64 { 141 var i: i64 = 0 142 while s + i < e { if lit[i] == (0 as u8) { return 0 } if q[s+i] != lit[i] { return 0 } i = i + 1 } 143 if lit[i] != (0 as u8) { return 0 } 144 return 1 145} 146func wc_span_eq(q: *u8, s1: i64, e1: i64, s2: i64, e2: i64) -> i64 { 147 if e1 - s1 != e2 - s2 { return 0 } 148 var i: i64 = 0 149 while s1 + i < e1 { if q[s1+i] != q[s2+i] { return 0 } i = i + 1 } 150 return 1 151} 152func wc_atoi_span(q: *u8, s: i64, e: i64) -> i64 { var v: i64 = 0; var i: i64 = s; while i < e { let c: i64 = q[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 } return v } 153// verb frame exists for ws span? 154func wc_has(q: *u8, n: i64, verb: *u8, ws_s: i64, ws_e: i64) -> i64 { 155 let cv: *i64 = wc_mm(WC_SPAN) as *i64 156 let cw: *i64 = wc_mm(WC_SPAN) as *i64 157 var i: i64 = 0 158 while i < n { 159 let le: i64 = wc_le(q, i, n) 160 if wc_col(q, i, le, 1, cv) == 1 { if wc_lit_eq(q, cv[0], cv[1], verb) == 1 { 161 if wc_col(q, i, le, 2, cw) == 1 { if wc_span_eq(q, cw[0], cw[1], ws_s, ws_e) == 1 { return 1 } } 162 } } 163 i = le + 1 164 } 165 return 0 166} 167// like wc_has but ws given as a literal string 168func wc_has_lit(q: *u8, n: i64, verb: *u8, ws: *u8) -> i64 { 169 let cv: *i64 = wc_mm(WC_SPAN) as *i64 170 let cw: *i64 = wc_mm(WC_SPAN) as *i64 171 var i: i64 = 0 172 while i < n { 173 let le: i64 = wc_le(q, i, n) 174 if wc_col(q, i, le, 1, cv) == 1 { if wc_lit_eq(q, cv[0], cv[1], verb) == 1 { 175 if wc_col(q, i, le, 2, cw) == 1 { if wc_lit_eq(q, cw[0], cw[1], ws) == 1 { return 1 } } 176 } } 177 i = le + 1 178 } 179 return 0 180} 181// is line i the FIRST kickoff for its ws? (no earlier KICKOFF same ws) 182func wc_first_kick(q: *u8, upto: i64, ws_s: i64, ws_e: i64) -> i64 { 183 let cv: *i64 = wc_mm(WC_SPAN) as *i64 184 let cw: *i64 = wc_mm(WC_SPAN) as *i64 185 var i: i64 = 0 186 while i < upto { 187 let le: i64 = wc_le(q, i, upto) 188 if wc_col(q, i, le, 1, cv) == 1 { if wc_lit_eq(q, cv[0], cv[1], "KICKOFF" as *u8) == 1 { 189 if wc_col(q, i, le, 2, cw) == 1 { if wc_span_eq(q, cw[0], cw[1], ws_s, ws_e) == 1 { return 0 } } 190 } } 191 i = le + 1 192 } 193 return 1 194} 195// last frame ts for ws span (-1 none); also fills note span into ons[0..1] when found 196func wc_last(q: *u8, n: i64, ws_s: i64, ws_e: i64, ons: *i64) -> i64 { 197 let cw: *i64 = wc_mm(WC_SPAN) as *i64 198 let c0: *i64 = wc_mm(WC_SPAN) as *i64 199 let cn: *i64 = wc_mm(WC_SPAN) as *i64 200 var last: i64 = 0 - 1 201 var i: i64 = 0 202 while i < n { 203 let le: i64 = wc_le(q, i, n) 204 if wc_col(q, i, le, 2, cw) == 1 { if wc_span_eq(q, cw[0], cw[1], ws_s, ws_e) == 1 { 205 if wc_col(q, i, le, 0, c0) == 1 { last = wc_atoi_span(q, c0[0], c0[1]) } 206 if wc_col(q, i, le, 4, cn) == 1 { ons[0] = cn[0]; ons[1] = cn[1] } 207 } } 208 i = le + 1 209 } 210 return last 211} 212// count open debts on an ALREADY-LOADED plane buffer [buf,n); fills first open row span into 213// fs[0..1] (-1 if none). seq350: board/standup used to RE-LOAD the whole debt plane per workstream 214// row (rows x store_bytes = the 16.4GB OOM class that killed the host on INCIDENT-0720); loop 215// callers now sts_load ONCE per run and pass the buffer down. The plane read itself goes through 216// the LIVE manifest only (bounded <=8 segs by the nx_segguard 600s clock beat -- the bound is 217// segguard.conf data, not a literal here). 218func wc_debts_in(buf: *u8, n: i64, fs: *i64) -> i64 { 219 let c3: *i64 = wc_mm(WC_SPAN) as *i64 220 fs[0] = 0 - 1 221 fs[1] = 0 - 1 222 var k: i64 = 0 223 var i: i64 = 0 224 while i < n { 225 let le: i64 = wc_le(buf, i, n) 226 if wc_col(buf, i, le, 3, c3) == 1 { if wc_lit_eq(buf, c3[0], c3[1], "open" as *u8) == 1 { 227 k = k + 1 228 if fs[0] < 0 { fs[0] = i; fs[1] = le } 229 } } 230 i = le + 1 231 } 232 return k 233} 234// loading wrapper (single-shot callers): load once, delegate to the _in body. 235func wc_debts(dprefix: *u8, buf: *u8, fs: *i64) -> i64 { 236 let n: i64 = sts_load(dprefix, buf, WC_CAP) 237 return wc_debts_in(buf, n, fs) 238} 239// data-driven CONFIG lookup, KEY-AWARE (rule 11: thresholds are store data; with 2+ CONFIG rows a 240// first-row-wins read would return the WRONG key's value -- key match required). absent -> defv. 241func wc_cfg(cprefix: *u8, key: *u8, defv: i64) -> i64 { 242 let b: *u8 = wc_mm(WC_CAP) 243 let n: i64 = sts_load(cprefix, b, WC_CAP) 244 if n <= 0 { return defv } 245 let c1: *i64 = wc_mm(WC_SPAN) as *i64 246 let c2: *i64 = wc_mm(WC_SPAN) as *i64 247 let c3: *i64 = wc_mm(WC_SPAN) as *i64 248 var i: i64 = 0 249 while i < n { 250 let le: i64 = wc_le(b, i, n) 251 if wc_col(b, i, le, 1, c1) == 1 { if wc_lit_eq(b, c1[0], c1[1], "CONFIG" as *u8) == 1 { 252 if wc_col(b, i, le, 2, c2) == 1 { if wc_lit_eq(b, c2[0], c2[1], key) == 1 { 253 if wc_col(b, i, le, 3, c3) == 1 { return wc_atoi_span(b, c3[0], c3[1]) } 254 } } 255 } } 256 i = le + 1 257 } 258 return defv 259} 260// R2a global-gate severity floor; unreadable/absent -> 0 = EVERY open debt gates (fail-closed). 261func wc_sevmin(cprefix: *u8) -> i64 { return wc_cfg(cprefix, "global-sev-min" as *u8, 0) } 262// does span [s,e) of q appear inside the NUL-term string ws (or ws inside the span)? crude scope match. 263func wc_scope_hits_ws(q: *u8, s: i64, e: i64, ws: *u8) -> i64 { 264 if wc_lit_eq(q, s, e, "global" as *u8) == 1 { return 1 } 265 if wc_lit_eq(q, s, e, ws) == 1 { return 1 } 266 // span-substring-of-ws 267 var wl: i64 = 0 268 while ws[wl] != (0 as u8) { wl = wl + 1 } 269 let sl: i64 = e - s 270 if sl > 0 { if sl <= wl { 271 var i: i64 = 0 272 while i + sl <= wl { 273 var j: i64 = 0 274 var ok: i64 = 1 275 while j < sl { if ws[i+j] != q[s+j] { ok = 0; j = sl } else { j = j + 1 } } 276 if ok == 1 { return 1 } 277 i = i + 1 278 } 279 } } 280 // ws-substring-of-span 281 if wl > 0 { if wl <= sl { 282 var i2: i64 = s 283 while i2 + wl <= e { 284 var j2: i64 = 0 285 var ok2: i64 = 1 286 while j2 < wl { if q[i2+j2] != ws[j2] { ok2 = 0; j2 = wl } else { j2 = j2 + 1 } } 287 if ok2 == 1 { return 1 } 288 i2 = i2 + 1 289 } 290 } } 291 return 0 292} 293// col0 all-digits (len>=8) = a legacy epoch-keyed 5-col debt row (ts sev scope status note); 294// else the v2 7-col plane row (id title sev status owner scope note). Fix 2026-07-18 sess 33b46798: 295// the sev/scope columns DIFFER per family -- reading col1/col2 unconditionally mis-gated v2 rows 296// (sev scraped digits out of TITLES, scope read the title) = silent false-gate on half the plane. 297func wc_row_legacy(q: *u8, ls: i64, le: i64, c0: *i64) -> i64 { 298 if wc_col(q, ls, le, 0, c0) == 0 { return 0 } 299 if c0[1] - c0[0] < 8 { return 0 } 300 var i: i64 = c0[0] 301 while i < c0[1] { 302 let c: i64 = q[i] as i64 303 if c < 48 { return 0 } 304 if c > 57 { return 0 } 305 i = i + 1 306 } 307 return 1 308} 309// R2a scoped gate: open debts that gate THIS ws = sev >= sevmin (global floor) OR scope matches ws. 310// fills first gating row span into fs[0..1] (-1 none). ws==0 -> every open debt counts (board totals). 311// Schema-aware per row: legacy sev@1 scope@2; v2 sev@2 scope@5 (status@3 in BOTH by convergence design). 312// _in variant on an ALREADY-LOADED buffer [buf,n) -- the seq350 de-amplification (see wc_debts_in). 313func wc_debts_scoped_in(buf: *u8, n: i64, fs: *i64, ws: *u8, sevmin: i64) -> i64 { 314 let c1: *i64 = wc_mm(WC_SPAN) as *i64 315 let c2: *i64 = wc_mm(WC_SPAN) as *i64 316 let c3: *i64 = wc_mm(WC_SPAN) as *i64 317 fs[0] = 0 - 1 318 fs[1] = 0 - 1 319 var k: i64 = 0 320 var i: i64 = 0 321 while i < n { 322 let le: i64 = wc_le(buf, i, n) 323 if wc_col(buf, i, le, 3, c3) == 1 { if wc_lit_eq(buf, c3[0], c3[1], "open" as *u8) == 1 { 324 var gates: i64 = 0 325 var sevcol: i64 = 2 326 var scopecol: i64 = 5 327 if wc_row_legacy(buf, i, le, c1) == 1 { sevcol = 1; scopecol = 2 } 328 if (ws as i64) == 0 { gates = 1 } else { 329 if wc_col(buf, i, le, sevcol, c1) == 1 { if wc_atoi_span(buf, c1[0], c1[1]) >= sevmin { gates = 1 } } 330 if gates == 0 { if wc_col(buf, i, le, scopecol, c2) == 1 { if wc_scope_hits_ws(buf, c2[0], c2[1], ws) == 1 { gates = 1 } } } 331 } 332 if gates == 1 { 333 k = k + 1 334 if fs[0] < 0 { fs[0] = i; fs[1] = le } 335 } 336 } } 337 i = le + 1 338 } 339 return k 340} 341// loading wrapper (single-shot callers: next/phase): load once, delegate to the _in body. 342func wc_debts_scoped(dprefix: *u8, buf: *u8, fs: *i64, ws: *u8, sevmin: i64) -> i64 { 343 let n: i64 = sts_load(dprefix, buf, WC_CAP) 344 return wc_debts_scoped_in(buf, n, fs, ws, sevmin) 345} 346// R2b evidence: is <tool> a registered row in tool_allowlist.conf (col0, tab-sep)? bounded read. 347func wc_tool_exposed(tool: *u8) -> i64 { 348 let b: *u8 = wc_mm(WC_JCAP) 349 let szp: *i64 = wc_mm(WC_SPAN) as *i64 350 let n: i64 = wc_read("tool_allowlist.conf" as *u8, b, WC_JCAP - 4, szp) 351 if n <= 0 { return 0 } 352 let c0: *i64 = wc_mm(WC_SPAN) as *i64 353 var i: i64 = 0 354 while i < n { 355 let le: i64 = wc_le(b, i, n) 356 if wc_col(b, i, le, 0, c0) == 1 { if wc_lit_eq(b, c0[0], c0[1], tool) == 1 { return 1 } } 357 i = le + 1 358 } 359 return 0 360} 361// R2b evidence: byte-count of a published artifact file (0 = absent/empty = NOT published). 362func wc_file_bytes(path: *u8) -> i64 { 363 let fd: i64 = sys_openat_rd(path) 364 if fd < 0 { return 0 } 365 let b: *u8 = wc_mm(WC_CAP) 366 var n: i64 = 0 367 var go: i64 = 1 368 while go == 1 { let r: i64 = sys_read(fd, ((b as i64) + n) as *u8, WC_CAP - n); if r <= 0 { go = 0 } else { n = n + r } if n >= WC_CAP { go = 0 } } 369 sys_close(fd) 370 return n 371} 372func wc_seedphases(prefix: *u8) -> i64 { 373 let b: *u8 = wc_mm(WC_CAP) 374 var o: i64 = 0 375 o = wc_cat(b, o, "0\tCONFIG\tglobal-sev-min\t6\tdebts-at-or-above-gate-every-lane\n" as *u8) 376 o = wc_cat(b, o, "0\tCONFIG\tstandup-active-secs\t7200\tin-progress-freshness-window\n" as *u8) 377 o = wc_cat(b, o, "1\tRECON\tconductor\tboard+claims+drift+health\tjournal+board-bytes\n" as *u8) 378 o = wc_cat(b, o, "2\tCLAIM\tsession\tleash-claim\tleash-row\n" as *u8) 379 o = wc_cat(b, o, "3\tDEBT\towner\teat-in-scope\tdebt-store-open-zero\n" as *u8) 380 o = wc_cat(b, o, "4\tWORK\towner\tbuild+seed\tgate-artifacts\n" as *u8) 381 o = wc_cat(b, o, "5\tPROVE\treferee\tgate+negcontrol+judge\tgate-stdout\n" as *u8) 382 o = wc_cat(b, o, "6\tCOMMIT\tsession\tpathspec-commit\tcommit-id\n" as *u8) 383 o = wc_cat(b, o, "7\tEXPOSE\tengineer\tmcp-register+api+ws-register\tallowlist-row\n" as *u8) 384 o = wc_cat(b, o, "8\tPUBLISH\tlibrarian\tsite+compare+evidence\tpublished-bytes\n" as *u8) 385 o = wc_cat(b, o, "9\tVERIFY-LIVE\treferee\tedge-get\thttp-200-bytes\n" as *u8) 386 o = wc_cat(b, o, "10\tGRADE\tcensus\tautograde+rollup\trollup-delta\n" as *u8) 387 o = wc_cat(b, o, "11\tBANK\tlibrarian\tcoindex-append\tjournal-row\n" as *u8) 388 o = wc_cat(b, o, "12\tREVIEW\tteam\traci-recon\treview-notes\n" as *u8) 389 o = wc_cat(b, o, "13\tRELEASE\tsession\trelease-claim\tleash-released\n" as *u8) 390 let cnt: i64 = sts_seed(prefix, b, o) 391 if cnt < 0 { wc_werr("CYCLE-FAIL commit error\n" as *u8); return 1 } 392 let m: *u8 = wc_mm(64) 393 var mo: i64 = wc_cat(m, 0, "CYCLE-SEEDED rows=" as *u8) 394 mo = wc_catn(m, mo, cnt) 395 m[mo] = WC_NL as u8 396 mo = mo + 1 397 sys_write(1, m, mo) 398 return 0 399} 400func main(argc: i64, argv: *i64) -> i64 { 401 if argc < 2 { wc_werr("usage: nx_ws_cycle {seedphases [prefix] | phases [prefix] | board [journal] [debtprefix] | next <ws> [journal] [debtprefix]}\n" as *u8); sys_exit(WC_EXIT_USAGE); return WC_EXIT_USAGE } 402 let verb: *u8 = argv[1] as *u8 403 if wc_lit_eq(verb, 0, wc_vlen(verb), "seedphases" as *u8) == 1 { 404 var cp: *u8 = "knowledge/store/cycle-" as *u8 405 if argc > 2 { cp = argv[2] as *u8 } 406 let rc: i64 = wc_seedphases(cp) 407 sys_exit(rc) 408 return rc 409 } 410 if wc_lit_eq(verb, 0, wc_vlen(verb), "phases" as *u8) == 1 { 411 var cp2: *u8 = "knowledge/store/cycle-" as *u8 412 if argc > 2 { cp2 = argv[2] as *u8 } 413 let b: *u8 = wc_mm(WC_CAP) 414 let n: i64 = sts_load(cp2, b, WC_CAP) 415 if n <= 0 { wc_werr("CYCLE-FAIL phases store empty (run seedphases)\n" as *u8); sys_exit(1); return 1 } 416 sys_write(1, b, n) 417 sys_exit(0) 418 return 0 419 } 420 if wc_lit_eq(verb, 0, wc_vlen(verb), "board" as *u8) == 1 { 421 var jr: *u8 = "knowledge/status/ws_sync.jrnl" as *u8 422 var dp: *u8 = "knowledge/store/debt-" as *u8 423 var cp4: *u8 = "knowledge/store/cycle-" as *u8 424 if argc > 2 { jr = argv[2] as *u8 } 425 if argc > 3 { dp = argv[3] as *u8 } 426 if argc > 4 { cp4 = argv[4] as *u8 } 427 let sevmin: i64 = wc_sevmin(cp4) 428 let q: *u8 = wc_mm(WC_JCAP) 429 let jsz: *i64 = wc_mm(WC_SPAN) as *i64 430 let n: i64 = wc_read(jr, q, WC_JCAP - 4, jsz) 431 let dbuf: *u8 = wc_mm(WC_CAP) 432 let fs: *i64 = wc_mm(WC_SPAN) as *i64 433 let dlen: i64 = sts_load(dp, dbuf, WC_CAP) 434 let k: i64 = wc_debts_in(dbuf, dlen, fs) 435 let wsb: *u8 = wc_mm(WC_SPAN * 16) 436 let out: *u8 = wc_mm(WC_CAP) 437 var o: i64 = 0 438 o = wc_cat(out, o, "{\"epoch\":" as *u8) 439 let now: i64 = sys_now_realtime_sec() 440 o = wc_catn(out, o, now) 441 o = wc_cat(out, o, ",\"journal_bytes\":" as *u8) 442 o = wc_catn(out, o, jsz[0]) 443 o = wc_cat(out, o, ",\"window_bytes\":" as *u8) 444 o = wc_catn(out, o, n) 445 o = wc_cat(out, o, ",\"derivation\":\"r2a-journal+debt-scoped\",\"debt_gate\":\"scoped-r2a\",\"sev_min\":" as *u8) 446 o = wc_catn(out, o, sevmin) 447 o = wc_cat(out, o, ",\"open_debts\":" as *u8) 448 o = wc_catn(out, o, k) 449 o = wc_cat(out, o, ",\"ws\":[" as *u8) 450 let cv: *i64 = wc_mm(WC_SPAN) as *i64 451 let cw: *i64 = wc_mm(WC_SPAN) as *i64 452 let ons: *i64 = wc_mm(WC_SPAN) as *i64 453 var emitted: i64 = 0 454 var i: i64 = 0 455 while i < n { 456 let le: i64 = wc_le(q, i, n) 457 if wc_col(q, i, le, 1, cv) == 1 { if wc_lit_eq(q, cv[0], cv[1], "KICKOFF" as *u8) == 1 { 458 if wc_col(q, i, le, 2, cw) == 1 { if wc_first_kick(q, i, cw[0], cw[1]) == 1 { 459 if emitted > 0 { o = wc_cat(out, o, "," as *u8) } 460 o = wc_cat(out, o, "{\"ws\":\"" as *u8) 461 o = wc_cat_esc(out, o, q, cw[0], cw[1]) 462 o = wc_cat(out, o, "\",\"phase\":\"" as *u8) 463 let done: i64 = wc_has(q, n, "DONE" as *u8, cw[0], cw[1]) 464 var wl: i64 = cw[1] - cw[0] 465 if wl > WC_SPAN * 16 - 2 { wl = WC_SPAN * 16 - 2 } 466 var wi: i64 = 0 467 while wi < wl { wsb[wi] = q[cw[0]+wi]; wi = wi + 1 } 468 wsb[wl] = 0 as u8 469 let fsx: *i64 = wc_mm(WC_SPAN) as *i64 470 var kws: i64 = 0 471 if done == 0 { kws = wc_debts_scoped_in(dbuf, dlen, fsx, wsb, sevmin) } 472 if done == 1 { o = wc_cat(out, o, "DONE" as *u8) } else { if kws > 0 { o = wc_cat(out, o, "DEBT-GATED" as *u8) } else { o = wc_cat(out, o, "WORK" as *u8) } } 473 o = wc_cat(out, o, "\",\"gated\":" as *u8) 474 o = wc_catn(out, o, kws) 475 o = wc_cat(out, o, ",\"last\":" as *u8) 476 ons[0] = 0 - 1 477 ons[1] = 0 - 1 478 let lt: i64 = wc_last(q, n, cw[0], cw[1], ons) 479 o = wc_catn(out, o, lt) 480 o = wc_cat(out, o, ",\"note\":\"" as *u8) 481 if ons[0] >= 0 { o = wc_cat_esc(out, o, q, ons[0], ons[1]) } 482 o = wc_cat(out, o, "\"}" as *u8) 483 emitted = emitted + 1 484 } } 485 } } 486 i = le + 1 487 } 488 o = wc_cat(out, o, "]}" as *u8) 489 out[o] = WC_NL as u8 490 o = o + 1 491 sys_write(1, out, o) 492 sys_exit(0) 493 return 0 494 } 495 if wc_lit_eq(verb, 0, wc_vlen(verb), "next" as *u8) == 1 { 496 if argc < 3 { wc_werr("next needs <ws>\n" as *u8); sys_exit(WC_EXIT_USAGE); return WC_EXIT_USAGE } 497 let ws: *u8 = argv[2] as *u8 498 var jr2: *u8 = "knowledge/status/ws_sync.jrnl" as *u8 499 var dp2: *u8 = "knowledge/store/debt-" as *u8 500 var cp3: *u8 = "knowledge/store/cycle-" as *u8 501 if argc > 3 { jr2 = argv[3] as *u8 } 502 if argc > 4 { dp2 = argv[4] as *u8 } 503 if argc > 5 { cp3 = argv[5] as *u8 } 504 let sevmin: i64 = wc_sevmin(cp3) 505 let dbuf2: *u8 = wc_mm(WC_CAP) 506 let fs2: *i64 = wc_mm(WC_SPAN) as *i64 507 let k2: i64 = wc_debts_scoped(dp2, dbuf2, fs2, ws, sevmin) 508 let out2: *u8 = wc_mm(WC_CAP) 509 var o2: i64 = 0 510 if k2 > 0 { 511 o2 = wc_cat(out2, o2, "{\"action\":\"EAT-DEBT\",\"gate\":\"scoped-r2a\",\"sev_min\":" as *u8) 512 o2 = wc_catn(out2, o2, sevmin) 513 o2 = wc_cat(out2, o2, ",\"gating_debts\":" as *u8) 514 o2 = wc_catn(out2, o2, k2) 515 o2 = wc_cat(out2, o2, ",\"debt\":\"" as *u8) 516 o2 = wc_cat_esc(out2, o2, dbuf2, fs2[0], fs2[1]) 517 o2 = wc_cat(out2, o2, "\"}" as *u8) 518 } else { 519 let q2: *u8 = wc_mm(WC_JCAP) 520 let jsz2: *i64 = wc_mm(WC_SPAN) as *i64 521 let n2: i64 = wc_read(jr2, q2, WC_JCAP - 4, jsz2) 522 var inflight: i64 = 0 523 if wc_has_lit(q2, n2, "KICKOFF" as *u8, ws) == 1 { if wc_has_lit(q2, n2, "DONE" as *u8, ws) == 0 { inflight = 1 } } 524 if inflight == 1 { 525 o2 = wc_cat(out2, o2, "{\"action\":\"WORK\",\"ws\":\"" as *u8) 526 o2 = wc_cat(out2, o2, ws) 527 o2 = wc_cat(out2, o2, "\",\"hint\":\"in-flight; finish the rung, PROVE with gate+negcontrol, then EXPOSE mcp+api+workflow\"}" as *u8) 528 } else { 529 o2 = wc_cat(out2, o2, "{\"action\":\"CLAIM\",\"ws\":\"" as *u8) 530 o2 = wc_cat(out2, o2, ws) 531 o2 = wc_cat(out2, o2, "\",\"hint\":\"not in-flight; pick a READY row on /frontier, claim via nx_claim_check, kickoff via nx_ws_sync\"}" as *u8) 532 } 533 } 534 out2[o2] = WC_NL as u8 535 o2 = o2 + 1 536 sys_write(1, out2, o2) 537 sys_exit(0) 538 return 0 539 } 540 if wc_lit_eq(verb, 0, wc_vlen(verb), "phase" as *u8) == 1 { 541 if argc < 3 { wc_werr("phase needs <ws>\n" as *u8); sys_exit(WC_EXIT_USAGE); return WC_EXIT_USAGE } 542 let ws3: *u8 = argv[2] as *u8 543 var tool: *u8 = 0 as *u8 544 var page: *u8 = 0 as *u8 545 var jr3: *u8 = "knowledge/status/ws_sync.jrnl" as *u8 546 var dp3: *u8 = "knowledge/store/debt-" as *u8 547 var cp5: *u8 = "knowledge/store/cycle-" as *u8 548 if argc > 3 { let a3: *u8 = argv[3] as *u8; if a3[0] != (45 as u8) { tool = a3 } } 549 if argc > 4 { let a4: *u8 = argv[4] as *u8; if a4[0] != (45 as u8) { page = a4 } } 550 if argc > 5 { jr3 = argv[5] as *u8 } 551 if argc > 6 { dp3 = argv[6] as *u8 } 552 if argc > 7 { cp5 = argv[7] as *u8 } 553 let q3: *u8 = wc_mm(WC_JCAP) 554 let jsz3: *i64 = wc_mm(WC_SPAN) as *i64 555 let n3: i64 = wc_read(jr3, q3, WC_JCAP - 4, jsz3) 556 let kicked: i64 = wc_has_lit(q3, n3, "KICKOFF" as *u8, ws3) 557 let done3: i64 = wc_has_lit(q3, n3, "DONE" as *u8, ws3) 558 let sev3: i64 = wc_sevmin(cp5) 559 let dbuf3: *u8 = wc_mm(WC_CAP) 560 let fs3: *i64 = wc_mm(WC_SPAN) as *i64 561 let kws3: i64 = wc_debts_scoped(dp3, dbuf3, fs3, ws3, sev3) 562 var exposed: i64 = 0 - 1 563 if (tool as i64) != 0 { exposed = wc_tool_exposed(tool) } 564 var pubb: i64 = 0 - 1 565 if (page as i64) != 0 { pubb = wc_file_bytes(page) } 566 let out3: *u8 = wc_mm(WC_CAP) 567 var o3: i64 = 0 568 o3 = wc_cat(out3, o3, "{\"ws\":\"" as *u8) 569 o3 = wc_cat(out3, o3, ws3) 570 o3 = wc_cat(out3, o3, "\",\"derived_phase\":\"" as *u8) 571 // the ladder, artifacts only, fail-closed at the FIRST missing rung 572 if done3 == 1 { o3 = wc_cat(out3, o3, "DONE" as *u8) } else { 573 if kicked == 0 { o3 = wc_cat(out3, o3, "CLAIM" as *u8) } else { 574 if kws3 > 0 { o3 = wc_cat(out3, o3, "DEBT" as *u8) } else { 575 var ph: i64 = 0 576 if (tool as i64) != 0 { if exposed == 0 { o3 = wc_cat(out3, o3, "EXPOSE-PENDING" as *u8); ph = 1 } } 577 if ph == 0 { if (page as i64) != 0 { if pubb <= 0 { o3 = wc_cat(out3, o3, "PUBLISH-PENDING" as *u8); ph = 1 } } } 578 if ph == 0 { if (page as i64) != 0 { o3 = wc_cat(out3, o3, "PUBLISHED" as *u8); ph = 1 } } 579 if ph == 0 { if (tool as i64) != 0 { o3 = wc_cat(out3, o3, "EXPOSED" as *u8); ph = 1 } } 580 if ph == 0 { o3 = wc_cat(out3, o3, "WORK" as *u8) } 581 } 582 } 583 } 584 o3 = wc_cat(out3, o3, "\",\"evidence\":{\"kickoff\":" as *u8) 585 o3 = wc_catn(out3, o3, kicked) 586 o3 = wc_cat(out3, o3, ",\"done\":" as *u8) 587 o3 = wc_catn(out3, o3, done3) 588 o3 = wc_cat(out3, o3, ",\"gating_debts\":" as *u8) 589 o3 = wc_catn(out3, o3, kws3) 590 o3 = wc_cat(out3, o3, ",\"exposed\":" as *u8) 591 if exposed < 0 { o3 = wc_cat(out3, o3, "-1" as *u8) } else { o3 = wc_catn(out3, o3, exposed) } 592 o3 = wc_cat(out3, o3, ",\"published_bytes\":" as *u8) 593 if pubb < 0 { o3 = wc_cat(out3, o3, "-1" as *u8) } else { o3 = wc_catn(out3, o3, pubb) } 594 o3 = wc_cat(out3, o3, "},\"journal_bytes\":" as *u8) 595 o3 = wc_catn(out3, o3, jsz3[0]) 596 o3 = wc_cat(out3, o3, ",\"window_bytes\":" as *u8) 597 o3 = wc_catn(out3, o3, n3) 598 o3 = wc_cat(out3, o3, ",\"derivation\":\"r2b-artifacts-only\"}" as *u8) 599 out3[o3] = WC_NL as u8 600 o3 = o3 + 1 601 sys_write(1, out3, o3) 602 sys_exit(0) 603 return 0 604 } 605 if wc_lit_eq(verb, 0, wc_vlen(verb), "standup" as *u8) == 1 { 606 // THE HUMAN WINDOW (operator 07-18: "in progress callouts like daily standups on an agile 607 // sprint... not wasting tokens"): emitted zero-Claude from the SAME journal beats sessions 608 // already file. HTML to stdout; the cron runner publishes it. Liveness window = store data. 609 var jr4: *u8 = "knowledge/status/ws_sync.jrnl" as *u8 610 var dp4: *u8 = "knowledge/store/debt-" as *u8 611 var cp6: *u8 = "knowledge/store/cycle-" as *u8 612 if argc > 2 { jr4 = argv[2] as *u8 } 613 if argc > 3 { dp4 = argv[3] as *u8 } 614 if argc > 4 { cp6 = argv[4] as *u8 } 615 let sev4: i64 = wc_sevmin(cp6) 616 let act4: i64 = wc_cfg(cp6, "standup-active-secs" as *u8, WC_MAGIC_7200) 617 let q4: *u8 = wc_mm(WC_JCAP) 618 let jsz4: *i64 = wc_mm(WC_SPAN) as *i64 619 let n4: i64 = wc_read(jr4, q4, WC_JCAP - 4, jsz4) 620 let dbuf4: *u8 = wc_mm(WC_CAP) 621 let fs4: *i64 = wc_mm(WC_SPAN) as *i64 622 let dlen4: i64 = sts_load(dp4, dbuf4, WC_CAP) 623 let k4: i64 = wc_debts_in(dbuf4, dlen4, fs4) 624 let now4: i64 = sys_now_realtime_sec() 625 let wsb4: *u8 = wc_mm(WC_SPAN * 16) 626 let out4: *u8 = wc_mm(WC_CAP) 627 let dn4: *u8 = wc_mm(WC_CAP) 628 var dno: i64 = 0 629 var o4: i64 = 0 630 o4 = wc_cat(out4, o4, "<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>Nishi Standup</title><style>body{background:black;color:gainsboro;font:15px sans-serif;margin:24px}h1{color:white}table{border-collapse:collapse;width:100%}td,th{border-bottom:1px solid dimgray;padding:5px 9px;text-align:left}th{color:gray}.a{color:springgreen}.s{color:orange}.d{color:gray}.g{color:tomato}</style></head><body><main><h1>Nishi Standup</h1><p class=d>epoch " as *u8) 631 o4 = wc_catn(out4, o4, now4) 632 o4 = wc_cat(out4, o4, " &middot; journal " as *u8) 633 o4 = wc_catn(out4, o4, jsz4[0]) 634 o4 = wc_cat(out4, o4, "B window " as *u8) 635 o4 = wc_catn(out4, o4, n4) 636 o4 = wc_cat(out4, o4, "B &middot; open debts " as *u8) 637 o4 = wc_catn(out4, o4, k4) 638 o4 = wc_cat(out4, o4, " &middot; active-window " as *u8) 639 o4 = wc_catn(out4, o4, act4) 640 o4 = wc_cat(out4, o4, "s</p><h2>IN PROGRESS</h2><table><tr><th>workstream</th><th>phase</th><th>live</th><th>age-min</th><th>last callout</th></tr>" as *u8) 641 let cv4: *i64 = wc_mm(WC_SPAN) as *i64 642 let cw4: *i64 = wc_mm(WC_SPAN) as *i64 643 let ons4: *i64 = wc_mm(WC_SPAN) as *i64 644 var i4: i64 = 0 645 while i4 < n4 { 646 let le: i64 = wc_le(q4, i4, n4) 647 if wc_col(q4, i4, le, 1, cv4) == 1 { if wc_lit_eq(q4, cv4[0], cv4[1], "KICKOFF" as *u8) == 1 { 648 if wc_col(q4, i4, le, 2, cw4) == 1 { if wc_first_kick(q4, i4, cw4[0], cw4[1]) == 1 { 649 ons4[0] = 0 - 1 650 ons4[1] = 0 - 1 651 let lt: i64 = wc_last(q4, n4, cw4[0], cw4[1], ons4) 652 if wc_has(q4, n4, "DONE" as *u8, cw4[0], cw4[1]) == 1 { 653 dno = wc_cat(dn4, dno, "<li><b>" as *u8) 654 dno = wc_cat_esc_html(dn4, dno, q4, cw4[0], cw4[1]) 655 dno = wc_cat(dn4, dno, "</b> &mdash; " as *u8) 656 if ons4[0] >= 0 { dno = wc_cat_esc_html(dn4, dno, q4, ons4[0], ons4[1]) } 657 dno = wc_cat(dn4, dno, "</li>" as *u8) 658 } else { 659 var wl4: i64 = cw4[1] - cw4[0] 660 if wl4 > WC_SPAN * 16 - 2 { wl4 = WC_SPAN * 16 - 2 } 661 var wi4: i64 = 0 662 while wi4 < wl4 { wsb4[wi4] = q4[cw4[0]+wi4]; wi4 = wi4 + 1 } 663 wsb4[wl4] = 0 as u8 664 let fsx4: *i64 = wc_mm(WC_SPAN) as *i64 665 let kws4: i64 = wc_debts_scoped_in(dbuf4, dlen4, fsx4, wsb4, sev4) 666 var age: i64 = now4 - lt 667 if age < 0 { age = 0 } 668 o4 = wc_cat(out4, o4, "<tr><td><b>" as *u8) 669 o4 = wc_cat_esc_html(out4, o4, q4, cw4[0], cw4[1]) 670 o4 = wc_cat(out4, o4, "</b></td><td>" as *u8) 671 if kws4 > 0 { o4 = wc_cat(out4, o4, "<span class=g>DEBT-GATED</span>" as *u8) } else { o4 = wc_cat(out4, o4, "WORK" as *u8) } 672 o4 = wc_cat(out4, o4, "</td><td>" as *u8) 673 if age <= act4 { o4 = wc_cat(out4, o4, "<span class=a>ACTIVE</span>" as *u8) } else { o4 = wc_cat(out4, o4, "<span class=s>STALE</span>" as *u8) } 674 o4 = wc_cat(out4, o4, "</td><td>" as *u8) 675 o4 = wc_catn(out4, o4, age / 60) 676 o4 = wc_cat(out4, o4, "</td><td>" as *u8) 677 if ons4[0] >= 0 { o4 = wc_cat_esc_html(out4, o4, q4, ons4[0], ons4[1]) } 678 o4 = wc_cat(out4, o4, "</td></tr>" as *u8) 679 } 680 } } 681 } } 682 i4 = le + 1 683 } 684 o4 = wc_cat(out4, o4, "</table><h2>DONE (window)</h2><ul>" as *u8) 685 var ci4: i64 = 0 686 while ci4 < dno { out4[o4] = dn4[ci4]; o4 = o4 + 1; ci4 = ci4 + 1 } 687 o4 = wc_cat(out4, o4, "</ul></main></body></html>" as *u8) 688 out4[o4] = WC_NL as u8 689 o4 = o4 + 1 690 sys_write(1, out4, o4) 691 sys_exit(0) 692 return 0 693 } 694 wc_werr("usage: nx_ws_cycle {seedphases [prefix] | phases [prefix] | board [journal] [debtprefix] [cycleprefix] | next <ws> [journal] [debtprefix] [cycleprefix] | phase <ws> [tool] [page] [journal] [debtprefix] [cycleprefix] | standup [journal] [debtprefix] [cycleprefix]}\n" as *u8) 695 sys_exit(WC_EXIT_USAGE) 696 return WC_EXIT_USAGE 697}