nx_debt.nx source
↩ module page · 744 lines · 41037 B
1// nx_debt.nx -- debt-store intake CLI (autonomous-loop design-of-record 2026-07-18, R1).
2// knowledge/store/debt- rows appended by judges/sessions; nx_ws_cycle REFUSES to emit WORK while
3// an open row exists (eat-debt-first mechanized). Row: <epoch>\t<sev1-9>\t<scope>\t<open|eaten>\t<desc>
4// nx_debt add <sev> <scope> <desc> [prefix]
5// nx_debt eat <epoch-id-or-index> [prefix] (epoch col0 exact-match FIRST, index fallback; echoes the row; idempotent)
6// nx_debt show <epoch-id-or-index> [prefix] (read-before-eat: echo one row, no mutation)
7// nx_debt page <offset> <limit> [prefix] (64KB-safe paged list w/ declared envelope)
8// nx_debt list [prefix]
9// Fail-closed: bad sev / out-of-range seq commit NOTHING. flock on <prefix>plock.
10// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
11import "nx_store_seed_lib.nx"
12import "nx_seg_store.nx"
13import "nx_syscalls.nx"
14
15const DB_CAP: i64 = 4194304 // 2026-07-29: was 1MB; the plane passed 850KB (~1219 rows, +~500/wk) -- the NEXT silent ceiling was weeks out (re-probe-the-new-boundary law)
16const DB_TAB: i64 = 9
17const DB_NL: i64 = 10
18const DB_STDERR: i64 = 2
19const DB_LOCK_EX: i64 = 2
20const DB_MODE: i64 = 420
21const DB_SPAN: i64 = 16
22const DB_PATHCAP: i64 = 256
23const DB_MSGCAP: i64 = 512
24
25// Pause before the confirming re-read. Long enough for a sibling's sts_seed generation to land, short
26// enough that a genuine refusal is still prompt.
27const DB_RACE_REREAD_MS: i64 = 250
28
29// A "lossy load" verdict has TWO possible causes and the old guard asserted the wrong one AS FACT:
30// (a) a prior writer really dropped rows -- STABLE, it is still there on the next read; or
31// (b) A SIBLING IS MID-COMMIT RIGHT NOW -- sts_seed bumps the declared q:n and THEN re-seeds the
32// plane, so a reader landing inside that window sees a bumped counter with rows not yet visible.
33// MEASURED 2026-07-30 (seq1521): four consecutive refusals on the debt- plane while nx_plane_repair
34// read the SAME plane as fully self-consistent (declared 1510 / found 1510 / beyond 0) and the
35// identical add then SUCCEEDED. declared_qn had moved 1495 -> 1510 between two probes because three
36// sibling workstreams were writing continuously. No data was ever lost.
37//
38// A REAL LOSS DOES NOT HEAL; A RACE DOES. So re-read ONCE and let the plane settle the question.
39// This does NOT relax the guard: if the second read still reports fewer rows than declared, the caller
40// refuses exactly as before. Baking a truncated read back into the plane remains the catastrophe this
41// exists to prevent -- we are removing a FALSE alarm, not the alarm.
42// How far to probe for a free id before refusing. Each probe is one full-plane scan, so this bounds the
43// worst case rather than expressing a policy -- a filing burst deep enough to exhaust it is itself the
44// thing worth refusing over.
45const DB_ID_MAXPROBE: i64 = 4096
46
47func db_load_guarded(prefix: *u8, q: *u8, dbf: *i64) -> i64 {
48 var n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
49 if dbf[1] < dbf[0] {
50 sys_sleep_ms(DB_RACE_REREAD_MS)
51 n = sts_load_honest(prefix, q, DB_CAP, dbf)
52 }
53 return n
54}
55const DB_EXIT_USAGE: i64 = 2
56
57func db_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
58func db_werr(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(DB_STDERR, s, n); return 0 }
59func db_vlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
60func db_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 }
61func db_catn(d: *u8, o: i64, v: i64) -> i64 { let t: *u8 = sys_mmap(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 }
62func db_atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 } return v }
63func db_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] == (DB_NL as u8) { s = 0 } else { e = e + 1 } } } return e }
64func db_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 {
65 var col: i64 = 0
66 var p: i64 = ls
67 while col < c {
68 var s: i64 = 1
69 while s == 1 { if p >= le { return 0 } if q[p] == (DB_TAB as u8) { s = 0 } else { p = p + 1 } }
70 p = p + 1
71 col = col + 1
72 }
73 var e: i64 = p
74 var s2: i64 = 1
75 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e] == (DB_TAB as u8) { s2 = 0 } else { e = e + 1 } } }
76 out[0] = p
77 out[1] = e
78 return 1
79}
80func db_span_lit(q: *u8, s: i64, e: i64, lit: *u8) -> i64 {
81 var i: i64 = 0
82 while s + i < e { if lit[i] == (0 as u8) { return 0 } if q[s+i] != lit[i] { return 0 } i = i + 1 }
83 if lit[i] != (0 as u8) { return 0 }
84 return 1
85}
86// exact-compare q[s..e) against a C-string (used to detect an identical already-filed desc)
87func db_tail_eq(q: *u8, s: i64, e: i64, lit: *u8) -> i64 {
88 let n: i64 = db_vlen(lit)
89 if e - s != n { return 0 }
90 var i: i64 = 0
91 while i < n { if q[s+i] != lit[i] { return 0 } i = i + 1 }
92 return 1
93}
94// seq1298: a caller-supplied [prefix] is a STORE PREFIX, not free text -- validate at the intake
95// boundary so a proof-string mistake fails LOUD with the contract, not as a phantom lock failure.
96// Valid: non-empty, fits DB_PATHCAP with room for "plock", ends with '-', chars in [a-z0-9_/-.].
97func db_prefix_ok(a: *u8) -> i64 {
98 let n: i64 = db_vlen(a)
99 if n < 2 { return 0 }
100 if n > DB_PATHCAP - 8 { return 0 }
101 if a[n-1] != (45 as u8) { return 0 }
102 var i: i64 = 0
103 while i < n {
104 let c: i64 = a[i] as i64
105 var ok: i64 = 0
106 if c >= 97 { if c <= 122 { ok = 1 } }
107 if c >= 48 { if c <= 57 { ok = 1 } }
108 if c == 95 { ok = 1 }
109 if c == 47 { ok = 1 }
110 if c == 45 { ok = 1 }
111 if c == 46 { ok = 1 }
112 if ok == 0 { return 0 }
113 i = i + 1
114 }
115 return 1
116}
117func db_take_prefix(a: *u8) -> *u8 {
118 if db_prefix_ok(a) == 1 { return a }
119 db_werr("DEBT-FAIL bad prefix arg: [prefix] is a STORE PREFIX like knowledge/store/debt- (ends with -, chars [a-z0-9_/-.]) -- NOT free text; eat takes NO proof arg\n" as *u8)
120 sys_exit(DB_EXIT_USAGE)
121 return a
122}
123func db_lock(prefix: *u8) -> i64 {
124 let p: *u8 = sys_mmap(DB_PATHCAP)
125 var o: i64 = db_cat(p, 0, prefix)
126 o = db_cat(p, o, "plock" as *u8)
127 p[o] = 0 as u8
128 let fd: i64 = sys_openat_append(p, DB_MODE)
129 if fd < 0 {
130 // seq1298: name the PATH -- "cannot lock" alone blamed flock when the cause was open-fail
131 let m: *u8 = sys_mmap(DB_MSGCAP)
132 var mo: i64 = db_cat(m, 0, "DEBT-FAIL lock-file OPEN failed (not flock) path=" as *u8)
133 mo = db_cat(m, mo, p)
134 m[mo] = DB_NL as u8
135 mo = mo + 1
136 sys_write(DB_STDERR, m, mo)
137 return 0 - 1
138 }
139 sys_flock(fd, DB_LOCK_EX)
140 return fd
141}
142// count open rows in a loaded row-buffer
143// ★THE LEDGER MUST BE CLOSEABLE BY CONSTRUCTION, NOT BY PARSING PROSE (2026-07-31).
144// MEASURED THIS SESSION: nx_debtconfirm's rung-5 remainder scan CAUGHT 1785453431 (52 unmigrated seg-store
145// writers) but MISSED 1785445444, which describes the SAME open population in prose containing none of its
146// keywords -- a FALSE CLEAN on an ACTIVE data-loss defect. Widening the keyword list is chasing prose
147// forever; the population of ways to say "not finished" is unbounded.
148// SO INVERT THE DEFAULT: every row carries an explicit machine-readable closure field. A filer who states
149// one keeps it; a filer who states none gets CLOSURE=UNSPECIFIED, which a confirmer reads as PARTIAL and
150// NEVER closes mechanically. Absence of a closure criterion becomes EXPLICIT instead of invisible.
151// Additive (rule 19): existing rows are untouched and every guard above is unchanged.
152func db_find_sub(h: *u8, n: i64, s: *u8) -> i64 {
153 var sl: i64 = 0
154 while s[sl] != (0 as u8) { sl = sl + 1 }
155 var r: i64 = 0 - 1
156 var i: i64 = 0
157 while i < n {
158 if i + sl <= n {
159 var m: i64 = 1
160 var k: i64 = 0
161 while k < sl { if h[i+k] != s[k] { m = 0; k = sl } else { k = k + 1 } }
162 if m == 1 { r = i; i = n } else { i = i + 1 }
163 } else { i = n }
164 }
165 return r
166}
167
168func db_open_count(q: *u8, n: i64) -> i64 {
169 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
170 var k: i64 = 0
171 var i: i64 = 0
172 while i < n {
173 let le: i64 = db_le(q, i, n)
174 if db_col(q, i, le, 3, c3) == 1 { if db_span_lit(q, c3[0], c3[1], "open" as *u8) == 1 { k = k + 1 } }
175 i = le + 1
176 }
177 return k
178}
179func main(argc: i64, argv: *i64) -> i64 {
180 if argc < 2 { db_werr("usage: nx_debt {add <sev> <scope> <desc> | eat <epoch-id-or-index> | show <epoch-id-or-index> | page <off> <lim> | list} [prefix]\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
181 let verb: *u8 = argv[1] as *u8
182 var prefix: *u8 = "knowledge/store/debt-" as *u8
183 if db_span_lit(verb, 0, db_vlen(verb), "list" as *u8) == 1 {
184 if argc > 2 { prefix = db_take_prefix(argv[2] as *u8) }
185 let q: *u8 = sys_mmap(DB_CAP)
186 // HONEST LOAD (2026-07-25): sts_load stops at the q:n count key with NO signal, so a stale
187 // q:n made this verb report total=125 AS FACT while the plane held 734+ rows. sts_load_honest
188 // loads IDENTICALLY but also reports declared/loaded/unreachable, so a PARTIAL listing says so.
189 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
190 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
191 var idx: i64 = 0
192 var i: i64 = 0
193 while i < n {
194 let le: i64 = db_le(q, i, n)
195 let m: *u8 = sys_mmap(DB_MSGCAP)
196 var o: i64 = db_catn(m, 0, idx)
197 m[o] = 32 as u8
198 o = o + 1
199 sys_write(1, m, o)
200 sys_write(1, ((q as i64) + i) as *u8, le - i)
201 sys_write(1, "\n" as *u8, 1)
202 idx = idx + 1
203 i = le + 1
204 }
205 let k: i64 = db_open_count(q, n)
206 let m2: *u8 = sys_mmap(DB_MSGCAP)
207 var o2: i64 = db_cat(m2, 0, "DEBTS total=" as *u8)
208 o2 = db_catn(m2, o2, idx)
209 o2 = db_cat(m2, o2, " open=" as *u8)
210 o2 = db_catn(m2, o2, k)
211 o2 = db_cat(m2, o2, " declared_qn=" as *u8)
212 o2 = db_catn(m2, o2, dbf[0])
213 o2 = db_cat(m2, o2, " loaded=" as *u8)
214 o2 = db_catn(m2, o2, dbf[1])
215 o2 = db_cat(m2, o2, " unreachable_beyond_qn=" as *u8)
216 o2 = db_catn(m2, o2, dbf[2])
217 m2[o2] = DB_NL as u8
218 o2 = o2 + 1
219 sys_write(1, m2, o2)
220 if dbf[2] > 0 { db_werr("DEBT-TRUNCATED-LOAD this listing is PARTIAL -- rows are reachable BEYOND the declared q:n; do NOT treat it as the whole corpus, and do NOT run add/eat (they re-seed the plane from this partial buffer)\n" as *u8) }
221 sys_exit(0)
222 return 0
223 }
224 // ---------- reseed: THE REPAIR PATH THE FAIL-CLOSED GUARD LACKED ----------
225 //
226 // ★THE DEADLOCK THIS EXISTS TO BREAK (measured 2026-07-30): a torn write left
227 // the plane declaring 1495 rows while holding 1494, so the lossy-load guard
228 // refused EVERY add/eat -- correctly, because add/eat re-seed the whole plane
229 // and would bake the loss in. But the ONLY way to correct the declared count
230 // is a write. **A fail-closed guard with no repair route is not safety, it is
231 // a permanent outage** -- and this plane is the ecosystem's coordination
232 // ledger, so every lane went read-only with no way back.
233 //
234 // This verb re-seeds the plane from exactly the rows that ARE reachable, so
235 // the declared count becomes the true count. It is EVIDENCE-GATED and can
236 // only ever repair an over-declaration:
237 // * REFUSES when rows are reachable BEYOND the declared count -- that is
238 // the orphan case, where re-seeding would genuinely destroy data.
239 // * REFUSES when the plane is already consistent, so it cannot be used as
240 // a routine write path or to paper over a healthy plane.
241 // It prints declared/loaded before and the true count after, so the repair is
242 // auditable rather than a silent mutation of the SSOT.
243 if db_span_lit(verb, 0, db_vlen(verb), "reseed" as *u8) == 1 {
244 let lk9: i64 = db_lock(prefix)
245 if lk9 < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
246 let q9: *u8 = sys_mmap(DB_CAP)
247 let f9: *i64 = sys_mmap(DB_MSGCAP) as *i64
248 let n9: i64 = sts_load_honest(prefix, q9, DB_CAP, f9)
249 if f9[2] > 0 { db_werr("DEBT-RESEED REFUSED: rows are reachable BEYOND the declared q:n -- re-seeding would ORPHAN them. This is data loss, not an over-declaration. NOTHING COMMITTED.\n" as *u8); sys_exit(1); return 1 }
250 if f9[1] >= f9[0] { db_werr("DEBT-RESEED REFUSED: the plane is already consistent (loaded >= declared); reseed is a REPAIR path, not a write path. NOTHING COMMITTED.\n" as *u8); sys_exit(1); return 1 }
251 let m9: *u8 = sys_mmap(DB_MSGCAP)
252 var o9: i64 = db_cat(m9, 0, "DEBT-RESEED repairing over-declaration: declared_qn=" as *u8)
253 o9 = db_catn(m9, o9, f9[0])
254 o9 = db_cat(m9, o9, " loaded=" as *u8)
255 o9 = db_catn(m9, o9, f9[1])
256 o9 = db_cat(m9, o9, " -- re-seeding from the reachable rows\n" as *u8)
257 sys_write(1, m9, o9)
258 let c9: i64 = sts_seed(prefix, q9, n9)
259 if c9 < 0 { db_werr("DEBT-RESEED FAILED: commit error; plane unchanged\n" as *u8); sys_exit(1); return 1 }
260 var p9: i64 = db_cat(m9, 0, "DEBT-RESEED OK rows=" as *u8)
261 p9 = db_catn(m9, p9, c9)
262 p9 = db_cat(m9, p9, " (declared count now equals the true count; writes unblocked)\n" as *u8)
263 sys_write(1, m9, p9)
264 sys_exit(0)
265 return 0
266 }
267
268 if db_span_lit(verb, 0, db_vlen(verb), "add" as *u8) == 1 {
269 if argc < 5 { db_werr("add needs <sev> <scope> <desc>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
270 if argc > 5 { prefix = db_take_prefix(argv[5] as *u8) }
271 let sev: *u8 = argv[2] as *u8
272 if db_vlen(sev) != 1 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
273 let sc: i64 = sev[0] as i64
274 if sc < 49 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
275 if sc > 57 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
276 let scope: *u8 = argv[3] as *u8
277 let desc: *u8 = argv[4] as *u8
278 let lk: i64 = db_lock(prefix)
279 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
280 let q: *u8 = sys_mmap(DB_CAP)
281 // FAIL-CLOSED GUARD (2026-07-25): add/eat REWRITE THE WHOLE PLANE from this buffer via
282 // sts_seed. If the load is PARTIAL that rewrite drops every row past the declared q:n --
283 // ~609 rows on the live debt- plane. Refuse instead: commit NOTHING when rows are unreachable.
284 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
285 let n: i64 = db_load_guarded(prefix, q, dbf)
286 if dbf[2] > 0 { db_werr("DEBT-REFUSED partial load: rows are reachable BEYOND the declared q:n and add/eat re-seed the WHOLE plane from this buffer, so committing would ORPHAN them. NOTHING COMMITTED. Run nx_debt list for declared_qn vs unreachable_beyond_qn.\n" as *u8); sys_exit(1); return 1 }
287 if dbf[1] < dbf[0] { db_werr("DEBT-REFUSED lossy load: loaded FEWER rows than the declared q:n ON TWO CONSECUTIVE READS (the re-read already ruled out a mid-commit sibling), so this is a real prior-writer drop -- the STS_WCAP ceiling class. Committing would BAKE the loss into the next generation. NOTHING COMMITTED. VERIFY BEFORE BELIEVING IT: run nx_plane_repair <prefix> with NO confirm -- it reads with a 32MiB buffer and will say NOTHING TO REPAIR if the plane is actually intact.\n" as *u8); sys_exit(1); return 1 }
288 // CONTENT-IDEMPOTENT (DEC014 mechanized). A transport error is a LOST RESPONSE, not a refused
289 // request, so a caller that retries a seemingly-failed add must NOT create a second row.
290 // PROVEN NEED: seq332/333/334 are the SAME edge-availability finding filed THREE times 11
291 // seconds apart. An exact-desc match is therefore a NO-OP naming the existing row, not an error.
292 let dc: *i64 = sys_mmap(DB_SPAN) as *i64
293 let dc3: *i64 = sys_mmap(DB_SPAN) as *i64
294 var dupidx: i64 = 0 - 1
295 var drow: i64 = 0
296 var di: i64 = 0
297 while di < n {
298 let dle: i64 = db_le(q, di, n)
299 if dle > di {
300 // 2026-07-29 seq1271: dedup ONLY against OPEN rows. Matching rows of ANY status meant a
301 // recurrence of an EATEN debt could never re-file (alarm beats had to carry day-bucket
302 // workarounds in their descs). The original intent -- a retry after a transport flake must
303 // not double-file -- only ever concerns the just-added OPEN row.
304 if db_col(q, di, dle, 4, dc) == 1 { if db_tail_eq(q, dc[0], dle, desc) == 1 {
305 if db_col(q, di, dle, 3, dc3) == 1 { if db_span_lit(q, dc3[0], dc3[1], "open" as *u8) == 1 { if dupidx < 0 { dupidx = drow } } }
306 } }
307 drow = drow + 1
308 }
309 di = dle + 1
310 }
311 if dupidx >= 0 {
312 let dm: *u8 = sys_mmap(DB_MSGCAP)
313 var dmo: i64 = db_cat(dm, 0, "DEBT-DUPLICATE-SKIPPED existing_idx=" as *u8)
314 dmo = db_catn(dm, dmo, dupidx)
315 dmo = db_cat(dm, dmo, " -- identical desc already filed; add is CONTENT-IDEMPOTENT so a retry after a transport flake is a NO-OP\n" as *u8)
316 sys_write(1, dm, dmo)
317 sys_exit(0)
318 return 0
319 }
320 let out: *u8 = sys_mmap(DB_CAP)
321 var o: i64 = 0
322 // 2026-07-31: DO NOT COPY THE WHOLE PLANE IN ORDER TO APPEND ONE ROW.
323 // This loop copied all n bytes of the loaded plane into `out` one byte at a time -- ~2.9MB and
324 // ~2.9M loop iterations on the live ledger -- and then sts_append_fast below was handed ONLY the
325 // new tail (out+n, o-n-1). The copy was DEAD WORK for the append path: nothing downstream reads
326 // out[0..n) except db_open_count, which can count the already-loaded `q` instead and add 1 for
327 // the row being filed (it is always written `open`).
328 // MEASURED BEFORE THIS CHANGE, on a ~2380-row plane: add=5963ms, eat=1590ms, show=98ms -- the
329 // supposedly O(1) append was the SLOWEST verb in the tool, and every seat paid ~6s per filing.
330 // The seq724 comment below is still true (the WRITE is 47 bytes) -- the cost was never the write.
331 // The row is now built at out[0..], so the append is handed out[0..o-1).
332 // NOTE the remaining full-plane passes are DELIBERATELY left alone: the dedup scan and the
333 // id-probe both genuinely need to read every row, and removing a correctness guard to win
334 // latency is the trade this lane exists to refuse. This change removes only work that had no
335 // reader at all.
336 // AN ID THAT COLLIDES IS NOT AN IDENTITY (measured 2026-07-31). seq1539 correctly retired the
337 // POSITIONAL seq because a re-seed made it address a different row -- and replaced it with an
338 // epoch that is unique only to ONE SECOND. Two adds inside the same second (a parallel or
339 // scripted filing, which is the normal case) get the SAME "STABLE" id; `eat` then fails closed
340 // on BOTH, so the tool hands back a close instruction that is guaranteed to fail and the rows
341 // become unclosable except by the positional index seq1539 exists to warn against. Proven live:
342 // two adds seconds apart both returned id=1785503735, and `show` reported the ambiguity.
343 // FIX AT THE SOURCE: while still holding the lock, probe forward to the first id no row uses.
344 // The id stays an integer written INTO the row (so it cannot drift) and now genuinely IDENTIFIES
345 // one. A bump of a few seconds is irrelevant as a timestamp and preserves ordering.
346 var now: i64 = sys_now_realtime_sec()
347 let idb: *u8 = sys_mmap(64)
348 let cc: *i64 = sys_mmap(DB_SPAN) as *i64
349 var probe: i64 = 0
350 var freeid: i64 = 0
351 while freeid == 0 {
352 let ln: i64 = db_catn(idb, 0, now)
353 idb[ln] = 0 as u8
354 var taken: i64 = 0
355 var sc: i64 = 0
356 while sc < n {
357 let sl: i64 = db_le(q, sc, n)
358 if db_col(q, sc, sl, 0, cc) == 1 {
359 if db_span_lit(q, cc[0], cc[1], idb) == 1 { taken = 1 }
360 }
361 sc = sl + 1
362 }
363 if taken == 0 { freeid = 1 } else {
364 now = now + 1
365 probe = probe + 1
366 if probe > DB_ID_MAXPROBE { db_werr("DEBT-FAIL cannot allocate a unique id\n" as *u8); sys_exit(1); return 1 }
367 }
368 }
369 o = db_catn(out, o, now)
370 out[o] = DB_TAB as u8
371 o = o + 1
372 out[o] = sev[0]
373 o = o + 1
374 out[o] = DB_TAB as u8
375 o = o + 1
376 o = db_cat(out, o, scope)
377 out[o] = DB_TAB as u8
378 o = o + 1
379 o = db_cat(out, o, "open" as *u8)
380 out[o] = DB_TAB as u8
381 o = o + 1
382 o = db_cat(out, o, desc)
383 // INVERT THE DEFAULT (see db_find_sub above): stamp an explicit closure field when the filer gave
384 // none, so "no criterion" is a VALUE in the row rather than something a reader must infer from prose.
385 var dl: i64 = 0
386 while desc[dl] != (0 as u8) { dl = dl + 1 }
387 if db_find_sub(desc, dl, "CLOSURE=" as *u8) < 0 { o = db_cat(out, o, " CLOSURE=UNSPECIFIED" as *u8) }
388 out[o] = DB_NL as u8
389 o = o + 1
390 // O(1) APPEND (2026-07-30, seq724 fix). Was sts_seed(prefix,out,o) which REWRITES THE WHOLE PLANE
391 // -- 1.4MB of disk writes to file one row. The new row is out[n..o-1] (out[0..n) is the loaded plane,
392 // out[o-1] is the trailing newline), so only those bytes need committing. Measured on a 2000-row
393 // plane: 47 bytes written instead of 210969. EVERY GUARD ABOVE IS UNCHANGED -- db_load_guarded, the
394 // partial-load refusal, the lossy refusal and the content-idempotent dedup all still run, because
395 // dedup genuinely needs the full read. ONLY THE WRITE CHANGED. Two defect classes die BY
396 // CONSTRUCTION: a partial load can no longer drop rows (nothing is rewritten), and ordinals no
397 // longer shift (a re-seed RE-SEGMENTS rows; an append touches no existing key). NOTE the eat path
398 // below KEEPS sts_seed on purpose -- it MODIFIES an existing row and must rewrite.
399 let cnt: i64 = sts_append_fast(prefix, out, o - 1)
400 if cnt < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
401 // `out` now holds ONLY the new row, so the open tally comes from the loaded plane `q` plus the
402 // row just filed (it is always written `open`). The eat path below still counts `out` because
403 // there `out` genuinely IS the whole rewritten plane.
404 let k: i64 = db_open_count(q, n) + 1
405 let m: *u8 = sys_mmap(DB_MSGCAP)
406 // seq1539 -- HAND BACK A STABLE HANDLE. `seq` is cnt-1: a ROW ORDINAL, not an identity. A re-seed
407 // that RE-SEGMENTS rows (any row containing a newline splits) shifts every ordinal after it, so a
408 // seq issued before the re-seed silently addresses a DIFFERENT row afterwards -- that is exactly how
409 // `nx_debt eat <seq>` closed another lane's row today. The epoch in col0 is the row's real identity:
410 // it is written INTO the row, so it cannot drift. Echo it FIRST and name it `id`, and label seq as
411 // what it is, so the obvious copy-paste closes the right row.
412 var mo: i64 = db_cat(m, 0, "DEBT-ADDED id=" as *u8)
413 mo = db_catn(m, mo, now)
414 mo = db_cat(m, mo, " (STABLE -- close with: nx_debt eat " as *u8)
415 mo = db_catn(m, mo, now)
416 mo = db_cat(m, mo, ") seq=" as *u8)
417 mo = db_catn(m, mo, cnt - 1)
418 mo = db_cat(m, mo, " (POSITIONAL -- shifts on re-seed, do NOT close by this) open=" as *u8)
419 mo = db_catn(m, mo, k)
420 m[mo] = DB_NL as u8
421 mo = mo + 1
422 sys_write(1, m, mo)
423 sys_exit(0)
424 return 0
425 }
426 if db_span_lit(verb, 0, db_vlen(verb), "eat" as *u8) == 1 {
427 if argc < 3 { db_werr("eat needs <seq>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
428 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
429 let lk: i64 = db_lock(prefix)
430 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
431 let q: *u8 = sys_mmap(DB_CAP)
432 // FAIL-CLOSED GUARD (2026-07-25): add/eat REWRITE THE WHOLE PLANE from this buffer via
433 // sts_seed. If the load is PARTIAL that rewrite drops every row past the declared q:n --
434 // ~609 rows on the live debt- plane. Refuse instead: commit NOTHING when rows are unreachable.
435 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
436 let n: i64 = db_load_guarded(prefix, q, dbf)
437 if dbf[2] > 0 { db_werr("DEBT-REFUSED partial load: rows are reachable BEYOND the declared q:n and add/eat re-seed the WHOLE plane from this buffer, so committing would ORPHAN them. NOTHING COMMITTED. Run nx_debt list for declared_qn vs unreachable_beyond_qn.\n" as *u8); sys_exit(1); return 1 }
438 if dbf[1] < dbf[0] { db_werr("DEBT-REFUSED lossy load: loaded FEWER rows than the declared q:n ON TWO CONSECUTIVE READS (the re-read already ruled out a mid-commit sibling), so this is a real prior-writer drop -- the STS_WCAP ceiling class. Committing would BAKE the loss into the next generation. NOTHING COMMITTED. VERIFY BEFORE BELIEVING IT: run nx_plane_repair <prefix> with NO confirm -- it reads with a 32MiB buffer and will say NOTHING TO REPAIR if the plane is actually intact.\n" as *u8); sys_exit(1); return 1 }
439 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
440 let c0: *i64 = sys_mmap(DB_SPAN) as *i64
441 var target: i64 = 0 - 1
442 var idmatches: i64 = 0
443 var ridx: i64 = 0
444 var scan: i64 = 0
445 while scan < n {
446 let sle: i64 = db_le(q, scan, n)
447 if db_col(q, scan, sle, 0, c0) == 1 { if db_span_lit(q, c0[0], c0[1], argv[2] as *u8) == 1 { idmatches = idmatches + 1; target = ridx } }
448 ridx = ridx + 1
449 scan = sle + 1
450 }
451 if idmatches > 1 { db_werr("DEBT-FAIL ambiguous epoch id matches multiple rows (fail-closed; eat by index instead)\n" as *u8); sys_exit(1); return 1 }
452 if idmatches == 0 { target = db_atoi(argv[2] as *u8) }
453 let out: *u8 = sys_mmap(DB_CAP)
454 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
455 var o: i64 = 0
456 var idx: i64 = 0
457 var hit: i64 = 0
458 var already: i64 = 0
459 var ea: i64 = 0
460 var eb: i64 = 0
461 var i: i64 = 0
462 while i < n {
463 let le: i64 = db_le(q, i, n)
464 var sub: i64 = 0
465 if idx == target {
466 if db_col(q, i, le, 3, c3) == 1 {
467 if db_span_lit(q, c3[0], c3[1], "open" as *u8) == 1 { sub = 1 } else { already = 1 }
468 hit = 1
469 ea = i
470 eb = le
471 }
472 }
473 if sub == 1 {
474 var t: i64 = i
475 while t < c3[0] { out[o] = q[t]; o = o + 1; t = t + 1 }
476 o = db_cat(out, o, "eaten" as *u8)
477 var t2: i64 = c3[1]
478 while t2 < le { out[o] = q[t2]; o = o + 1; t2 = t2 + 1 }
479 } else {
480 var t3: i64 = i
481 while t3 < le { out[o] = q[t3]; o = o + 1; t3 = t3 + 1 }
482 }
483 out[o] = DB_NL as u8
484 o = o + 1
485 idx = idx + 1
486 i = le + 1
487 }
488 if hit == 0 { db_werr("DEBT-FAIL no row matches epoch-id or index (nothing committed)\n" as *u8); sys_exit(1); return 1 }
489 if already == 1 { db_puts("DEBT-EATEN already (idempotent)\n" as *u8); db_puts("ROW: " as *u8); sys_write(1, ((q as i64) + ea) as *u8, eb - ea); sys_write(1, "\n" as *u8, 1); sys_exit(0); return 0 }
490 let cnt: i64 = sts_seed(prefix, out, o)
491 if cnt < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
492 let k: i64 = db_open_count(out, o)
493 let m: *u8 = sys_mmap(DB_MSGCAP)
494 // ★LEAD WITH THE IDENTITY, NOT THE POSITION (seq1539's affordance half). The eat resolved by
495 // epoch, but v1 of this line printed "idx=<pos>" FIRST -- so the number an operator copied out
496 // of a success message was the one that shifts on the next re-seed. That is how I closed a
497 // sibling's row. The stable id leads; the position is labelled as the throwaway it is.
498 let ce: *i64 = sys_mmap(DB_SPAN) as *i64
499 db_puts("DEBT-EATEN id=" as *u8)
500 if db_col(q, ea, eb, 0, ce) == 1 { sys_write(1, ((q as i64) + ce[0]) as *u8, ce[1] - ce[0]) }
501 var mo: i64 = db_cat(m, 0, " at_idx=" as *u8)
502 mo = db_catn(m, mo, target)
503 mo = db_cat(m, mo, " (POSITIONAL -- do NOT reuse) open=" as *u8)
504 mo = db_catn(m, mo, k)
505 m[mo] = DB_NL as u8
506 mo = mo + 1
507 sys_write(1, m, mo)
508 db_puts("ROW: " as *u8)
509 sys_write(1, ((q as i64) + ea) as *u8, eb - ea)
510 sys_write(1, "\n" as *u8, 1)
511 sys_exit(0)
512 return 0
513 }
514 if db_span_lit(verb, 0, db_vlen(verb), "page" as *u8) == 1 {
515 if argc < 4 { db_werr("page needs <offset> <limit>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
516 if argc > 4 { prefix = db_take_prefix(argv[4] as *u8) }
517 let off: i64 = db_atoi(argv[2] as *u8)
518 let lim: i64 = db_atoi(argv[3] as *u8)
519 let q: *u8 = sys_mmap(DB_CAP)
520 // READ-ONLY VERB: WARN, NEVER REFUSE. The two guards below were copy-pasted here from the
521 // add/eat paths -- comment and all -- but `page` COMMITS NOTHING, so "committing would BAKE
522 // the loss / NOTHING COMMITTED" was never true of it. The effect was that during any
523 // contention window the board became UNREADABLE: the one verb you reach for to diagnose a
524 // sick plane exited 1 and printed no rows. Refusing to READ is strictly worse than showing a
525 // possibly-incomplete view, because a read cannot damage anything. So surface the anomaly on
526 // stderr and STILL PRINT what is reachable. (seq1521/1532)
527 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
528 let n: i64 = db_load_guarded(prefix, q, dbf)
529 if dbf[2] > 0 { db_werr("DEBT-WARN partial load: rows are reachable BEYOND the declared q:n. Showing what loaded; this verb commits nothing. Run nx_debt list for declared_qn vs unreachable_beyond_qn.\n" as *u8) }
530 if dbf[1] < dbf[0] { db_werr("DEBT-WARN lossy load: loaded fewer rows than the declared q:n on two consecutive reads. Showing what loaded; this verb commits nothing, so it cannot bake the loss. Run nx_plane_repair <prefix> with NO confirm to check whether the plane is actually damaged.\n" as *u8) }
531 var idx: i64 = 0
532 var shown: i64 = 0
533 var i: i64 = 0
534 while i < n {
535 let le: i64 = db_le(q, i, n)
536 var pr: i64 = 0
537 if idx >= off { if shown < lim { pr = 1 } }
538 if pr == 1 {
539 let m: *u8 = sys_mmap(DB_MSGCAP)
540 var o: i64 = db_catn(m, 0, idx)
541 m[o] = 32 as u8
542 o = o + 1
543 sys_write(1, m, o)
544 sys_write(1, ((q as i64) + i) as *u8, le - i)
545 sys_write(1, "\n" as *u8, 1)
546 shown = shown + 1
547 }
548 idx = idx + 1
549 i = le + 1
550 }
551 let k: i64 = db_open_count(q, n)
552 let m2: *u8 = sys_mmap(DB_MSGCAP)
553 var o2: i64 = db_cat(m2, 0, "DEBTS-PAGE off=" as *u8)
554 o2 = db_catn(m2, o2, off)
555 o2 = db_cat(m2, o2, " lim=" as *u8)
556 o2 = db_catn(m2, o2, lim)
557 o2 = db_cat(m2, o2, " shown=" as *u8)
558 o2 = db_catn(m2, o2, shown)
559 o2 = db_cat(m2, o2, " total=" as *u8)
560 o2 = db_catn(m2, o2, idx)
561 o2 = db_cat(m2, o2, " open=" as *u8)
562 o2 = db_catn(m2, o2, k)
563 m2[o2] = DB_NL as u8
564 o2 = o2 + 1
565 sys_write(1, m2, o2)
566 sys_exit(0)
567 return 0
568 }
569 if db_span_lit(verb, 0, db_vlen(verb), "sev" as *u8) == 1 {
570 // sev <minsev> [prefix] -- OPEN rows with sev >= minsev over the WHOLE corpus, in-organ.
571 // 2026-07-29: nx_debt_view's 512KB fork-capture went blind past ~734 rows, so its sev hunt
572 // silently missed sev-8 rows. The eat-most-important-first pointer must see EVERYTHING;
573 // this verb walks the same honest load list/page use -- no fork, no capture cap.
574 if argc < 3 { db_werr("sev needs <minsev>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
575 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
576 let minsev: i64 = db_atoi(argv[2] as *u8)
577 let q: *u8 = sys_mmap(DB_CAP)
578 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
579 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
580 if dbf[2] > 0 { db_werr("DEBT-TRUNCATED-LOAD this listing is PARTIAL -- rows exist beyond the declared q:n\n" as *u8) }
581 let sp: *i64 = sys_mmap(DB_SPAN) as *i64
582 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
583 var idx: i64 = 0
584 var hit: i64 = 0
585 var shown: i64 = 0
586 var i: i64 = 0
587 while i < n {
588 let le: i64 = db_le(q, i, n)
589 var ok: i64 = 0
590 if db_col(q, i, le, 3, c3) == 1 { if db_span_lit(q, c3[0], c3[1], "open" as *u8) == 1 { ok = 1 } }
591 if ok == 1 {
592 var sv: i64 = 0
593 if db_col(q, i, le, 1, sp) == 1 { if sp[1] - sp[0] == 1 { let c: i64 = q[sp[0]]; if c >= 49 { if c <= 57 { sv = c - 48 } } } }
594 if sv >= minsev {
595 hit = hit + 1
596 if shown < 40 {
597 let m: *u8 = sys_mmap(DB_MSGCAP)
598 var o: i64 = db_catn(m, 0, idx)
599 m[o] = 32 as u8
600 o = o + 1
601 sys_write(1, m, o)
602 var rl: i64 = le - i
603 var tr: i64 = 0
604 if rl > 700 { rl = 700; tr = 1 }
605 sys_write(1, ((q as i64) + i) as *u8, rl)
606 if tr == 1 { sys_write(1, "...[ROW-TRUNC-700]" as *u8, 18) }
607 sys_write(1, "\n" as *u8, 1)
608 shown = shown + 1
609 }
610 }
611 }
612 idx = idx + 1
613 i = le + 1
614 }
615 let m3: *u8 = sys_mmap(DB_MSGCAP)
616 var o3: i64 = db_cat(m3, 0, "DEBTS-SEV min=" as *u8)
617 o3 = db_catn(m3, o3, minsev)
618 o3 = db_cat(m3, o3, " matched=" as *u8)
619 o3 = db_catn(m3, o3, hit)
620 o3 = db_cat(m3, o3, " shown=" as *u8)
621 o3 = db_catn(m3, o3, shown)
622 o3 = db_cat(m3, o3, " total=" as *u8)
623 o3 = db_catn(m3, o3, idx)
624 o3 = db_cat(m3, o3, " cap-40-shown row-trunc-700" as *u8)
625 m3[o3] = DB_NL as u8
626 o3 = o3 + 1
627 sys_write(1, m3, o3)
628 sys_exit(0)
629 return 0
630 }
631 if db_span_lit(verb, 0, db_vlen(verb), "find" as *u8) == 1 {
632 // find <substr> [prefix] -- any-status substring scan over the WHOLE corpus, in-organ (same
633 // rationale as sev: the fork-captured view was blind past ~734 rows).
634 if argc < 3 { db_werr("find needs <substr>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
635 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
636 let pat: *u8 = argv[2] as *u8
637 var pl: i64 = 0
638 while pat[pl] != (0 as u8) { pl = pl + 1 }
639 let q: *u8 = sys_mmap(DB_CAP)
640 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
641 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
642 if dbf[2] > 0 { db_werr("DEBT-TRUNCATED-LOAD this listing is PARTIAL -- rows exist beyond the declared q:n\n" as *u8) }
643 var idx: i64 = 0
644 var hit: i64 = 0
645 var shown: i64 = 0
646 var i: i64 = 0
647 while i < n {
648 let le: i64 = db_le(q, i, n)
649 var mth: i64 = 0
650 if pl > 0 {
651 var j: i64 = i
652 while j + pl <= le {
653 var k2: i64 = 0
654 var eq: i64 = 1
655 while k2 < pl { if q[j + k2] != pat[k2] { eq = 0; k2 = pl } else { k2 = k2 + 1 } }
656 if eq == 1 { mth = 1; j = le } else { j = j + 1 }
657 }
658 }
659 if mth == 1 {
660 hit = hit + 1
661 if shown < 40 {
662 let m: *u8 = sys_mmap(DB_MSGCAP)
663 var o: i64 = db_catn(m, 0, idx)
664 m[o] = 32 as u8
665 o = o + 1
666 sys_write(1, m, o)
667 var rl: i64 = le - i
668 var tr: i64 = 0
669 if rl > 700 { rl = 700; tr = 1 }
670 sys_write(1, ((q as i64) + i) as *u8, rl)
671 if tr == 1 { sys_write(1, "...[ROW-TRUNC-700]" as *u8, 18) }
672 sys_write(1, "\n" as *u8, 1)
673 shown = shown + 1
674 }
675 }
676 idx = idx + 1
677 i = le + 1
678 }
679 let m4: *u8 = sys_mmap(DB_MSGCAP)
680 var o4: i64 = db_cat(m4, 0, "DEBTS-FIND matched=" as *u8)
681 o4 = db_catn(m4, o4, hit)
682 o4 = db_cat(m4, o4, " shown=" as *u8)
683 o4 = db_catn(m4, o4, shown)
684 o4 = db_cat(m4, o4, " total=" as *u8)
685 o4 = db_catn(m4, o4, idx)
686 o4 = db_cat(m4, o4, " cap-40-shown row-trunc-700" as *u8)
687 m4[o4] = DB_NL as u8
688 o4 = o4 + 1
689 sys_write(1, m4, o4)
690 sys_exit(0)
691 return 0
692 }
693 if db_span_lit(verb, 0, db_vlen(verb), "show" as *u8) == 1 {
694 if argc < 3 { db_werr("show needs <epoch-id-or-index>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
695 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
696 let q: *u8 = sys_mmap(DB_CAP)
697 // READ-ONLY VERB: WARN, NEVER REFUSE -- `show` commits nothing, so the write-path refusal that
698 // was copy-pasted here (comment and all) could only ever HIDE the row you asked to read.
699 // (seq1521/1532)
700 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
701 let n: i64 = db_load_guarded(prefix, q, dbf)
702 if dbf[2] > 0 { db_werr("DEBT-WARN partial load: rows are reachable BEYOND the declared q:n. Showing anyway; this verb commits nothing.\n" as *u8) }
703 if dbf[1] < dbf[0] { db_werr("DEBT-WARN lossy load: loaded fewer rows than the declared q:n on two consecutive reads. Showing anyway; this verb commits nothing. Run nx_plane_repair <prefix> with NO confirm to check for real damage.\n" as *u8) }
704 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
705 let c0: *i64 = sys_mmap(DB_SPAN) as *i64
706 var target: i64 = 0 - 1
707 var idmatches: i64 = 0
708 var ridx: i64 = 0
709 var scan: i64 = 0
710 while scan < n {
711 let sle: i64 = db_le(q, scan, n)
712 if db_col(q, scan, sle, 0, c0) == 1 { if db_span_lit(q, c0[0], c0[1], argv[2] as *u8) == 1 { idmatches = idmatches + 1; target = ridx } }
713 ridx = ridx + 1
714 scan = sle + 1
715 }
716 if idmatches == 0 { target = db_atoi(argv[2] as *u8) }
717 var idx: i64 = 0
718 var hit: i64 = 0
719 var i: i64 = 0
720 while i < n {
721 let le: i64 = db_le(q, i, n)
722 if idx == target {
723 hit = 1
724 let m: *u8 = sys_mmap(DB_MSGCAP)
725 var o: i64 = db_cat(m, 0, "ROW idx=" as *u8)
726 o = db_catn(m, o, idx)
727 m[o] = 32 as u8
728 o = o + 1
729 sys_write(1, m, o)
730 sys_write(1, ((q as i64) + i) as *u8, le - i)
731 sys_write(1, "\n" as *u8, 1)
732 }
733 idx = idx + 1
734 i = le + 1
735 }
736 if idmatches > 1 { db_puts("NOTE: epoch id ambiguous (multiple rows); showed LAST match; eat would REFUSE\n" as *u8) }
737 if hit == 0 { db_werr("DEBT-FAIL no row matches\n" as *u8); sys_exit(1); return 1 }
738 sys_exit(0)
739 return 0
740 }
741 db_werr("usage: nx_debt {add <sev> <scope> <desc> | eat <epoch-id-or-index> | show <epoch-id-or-index> | page <off> <lim> | list} [prefix]\n" as *u8)
742 sys_exit(DB_EXIT_USAGE)
743 return DB_EXIT_USAGE
744}