nx_debt.nx source
↩ module page · 1034 lines · 60482 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// nx_debt dedup [apply] [prefix] (CONSOLIDATE byte-identical OPEN rows: DRY-RUN by default, `apply`
10// commits; keeps the EARLIEST row of each group and only flips the rest open->eaten -- rule 13
11// soft-delete, no row is ever removed. ONE sts_seed per run, not one per closure.)
12// Fail-closed: bad sev / out-of-range seq commit NOTHING. flock on <prefix>plock.
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 DB_MAGIC_1469598103934665603: i64 = 1469598103934665603
18const DB_MAGIC_1099511628211: i64 = 1099511628211
19
20const DB_CAP: i64 = 33554432 // 2026-08-06: was 4 MiB and THE PLANE CROSSED IT -- measured 4,217,666B / 3,326 rows via nx_plane_check while nx_debt page reported total=3311 and show could not find the ~15 NEWEST rows. The 07-29 note predicted the next ceiling was "weeks out"; it arrived in EIGHT DAYS. ***AN APPEND-ONLY PLANE PAST A PREFIX CAP LOSES ITS NEWEST ROWS FIRST, SO THE READER DEGRADES EXACTLY AS NEW WORK ARRIVES*** -- and add/page disagreed by one on `open` (2305 vs 2304) because they read different amounts of the same file. 32 MiB is ~8x current at ~500 rows/wk. ⚠THIS IS THE THIRD RAISE, NOT A FIX: the structural answer is to compare DB_CAP against the actual plane size and REFUSE LOUDLY (or tail-anchor the read) instead of silently returning a prefix -- a cap that can be crossed in silence will be crossed again. Detector now exists: nx_capcliff scan.
21const DB_TAB: i64 = 9
22const DB_NL: i64 = 10
23const DB_STDERR: i64 = 2
24const DB_LOCK_EX: i64 = 2
25const DB_MODE: i64 = 420
26const DB_SPAN: i64 = 16
27const DB_ROWMAX: i64 = 131072 // 2026-08-06: per-row index capacity for the `dedup` verb (~39x the live
28// 3,357-row plane). REFUSES LOUDLY past this rather than deduping a prefix of the plane -- a consolidator
29// that silently saw only part of the plane would report a clean sweep while leaving duplicates behind.
30const DB_PATHCAP: i64 = 256
31const DB_MSGCAP: i64 = 512
32const DB_DESCCAP: i64 = 262144 // 2026-08-06: buffer for the CANONICAL desc (the exact bytes the row will
33// carry) so the dedup comparator and the writer share ONE form. Sized 8x the largest desc observed on the
34// live plane (~2.5KB). REFUSES LOUDLY when exceeded rather than truncating -- per DB_CAP's own law above,
35// a cap that can be crossed in silence will be crossed again, and a SILENTLY TRUNCATED desc would defeat
36// the very dedup this constant exists to make work.
37
38// Pause before the confirming re-read. Long enough for a sibling's sts_seed generation to land, short
39// enough that a genuine refusal is still prompt.
40const DB_RACE_REREAD_MS: i64 = 250
41
42// A "lossy load" verdict has TWO possible causes and the old guard asserted the wrong one AS FACT:
43// (a) a prior writer really dropped rows -- STABLE, it is still there on the next read; or
44// (b) A SIBLING IS MID-COMMIT RIGHT NOW -- sts_seed bumps the declared q:n and THEN re-seeds the
45// plane, so a reader landing inside that window sees a bumped counter with rows not yet visible.
46// MEASURED 2026-07-30 (seq1521): four consecutive refusals on the debt- plane while nx_plane_repair
47// read the SAME plane as fully self-consistent (declared 1510 / found 1510 / beyond 0) and the
48// identical add then SUCCEEDED. declared_qn had moved 1495 -> 1510 between two probes because three
49// sibling workstreams were writing continuously. No data was ever lost.
50//
51// A REAL LOSS DOES NOT HEAL; A RACE DOES. So re-read ONCE and let the plane settle the question.
52// This does NOT relax the guard: if the second read still reports fewer rows than declared, the caller
53// refuses exactly as before. Baking a truncated read back into the plane remains the catastrophe this
54// exists to prevent -- we are removing a FALSE alarm, not the alarm.
55// How far to probe for a free id before refusing. Each probe is one full-plane scan, so this bounds the
56// worst case rather than expressing a policy -- a filing burst deep enough to exhaust it is itself the
57// thing worth refusing over.
58const DB_ID_MAXPROBE: i64 = 4096
59
60// Rows actually PRESENT in the loaded buffer. sts_load_honest increments flags[1] for every row it
61// FINDS, but sts_emit_row is bounded by `cap` -- so once the buffer fills, rows are dropped while the
62// counter keeps counting. nx_plane_repair calls that mismatch ***THE LETHAL ONE*** and REFUSES to
63// reseed on it, because reseeding from a truncated buffer destroys the tail. Same detection here.
64func db_count_rows(buf: *u8, n: i64) -> i64 {
65 var r: i64 = 0
66 var i: i64 = 0
67 while i < n { if buf[i] == (DB_NL as u8) { r = r + 1 } i = i + 1 }
68 return r
69}
70
71func db_load_guarded(prefix: *u8, q: *u8, dbf: *i64) -> i64 {
72 var n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
73 if dbf[1] < dbf[0] {
74 sys_sleep_ms(DB_RACE_REREAD_MS)
75 n = sts_load_honest(prefix, q, DB_CAP, dbf)
76 }
77 // ***A READER THAT CANNOT SEE THE WHOLE LEDGER MUST SAY SO.*** Measured 2026-08-06: the plane crossed
78 // the old 4 MiB DB_CAP at 4,217,666B / 3,326 rows, and this organ answered `total=3311` and
79 // `no row matches` for the ~15 NEWEST rows -- with NO coverage claim anywhere. A correctly-filed row
80 // looked permanently lost, and I spent real time diagnosing a WRITE failure that never happened
81 // (nx_plane_check was GREEN 3326/3326 the whole time). The sibling reader nx_debt_view always printed
82 // capture_truncated + coverage_complete and named the constant to raise -- which is the ONLY reason
83 // this was diagnosable at all. THE DECLARATION, NOT THE CAP, IS THE FEATURE: a bigger buffer just moves
84 // the cliff, while an announced truncation can never again be mistaken for absence.
85 let got: i64 = db_count_rows(q, n)
86 // TWO DIRECTIONS, TWO CAUSES (2026-09-02). A buffer holding FEWER newlines than the loader counted is the truncation
87 // above (rows dropped at the cap). A buffer holding MORE newlines than rows is NOT truncation: a row VALUE carries an
88 // embedded newline, so the newline census over-counts by one per such row. Measured live: `holds 4301 rows but the
89 // loader counted 4300` with DB_CAP at 32 MiB and the plane under 5 MB -- every verb was prefixed with a false
90 // DB_CAP alarm naming a constant to raise, for a condition no cap can change. The two verdicts are named separately
91 // so a reader never raises a cap to cure a newline, and never ignores a real truncation because the last one was false.
92 if got < dbf[1] {
93 db_werr("NX-DEBT COVERAGE-TRUNCATED: the read buffer holds " as *u8)
94 db_werrn(got)
95 db_werr(" rows but the loader counted " as *u8)
96 db_werrn(dbf[1])
97 db_werr(" -- DB_CAP is too small for this plane, so every verb below is reporting a PREFIX, and the rows it cannot see are the NEWEST ones. Raise DB_CAP in nx_debt.nx (and its hand-copies DL_CAP / IC_CAP / DV_CAPTURE_CAP) and rebuild. Run: nx_capcliff scan\n" as *u8)
98 }
99 if got > dbf[1] {
100 db_werr("NX-DEBT ROW-EMBEDDED-NEWLINE: the read buffer holds " as *u8)
101 db_werrn(got)
102 db_werr(" newline-delimited lines for " as *u8)
103 db_werrn(dbf[1])
104 db_werr(" loaded rows -- " as *u8)
105 db_werrn(got - dbf[1])
106 db_werr(" row value(s) carry an embedded newline (a desc pasted with a line break). Coverage is COMPLETE; line-oriented readers (page/show/find) will see the tail of such a row as a malformed extra line. Not a cap: raising DB_CAP changes nothing. Remedy: nx_plane_repair or re-filing the row with the break removed.\n" as *u8)
107 }
108 return n
109}
110const DB_EXIT_USAGE: i64 = 2
111
112func 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 }
113func 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 }
114// stderr number, for the coverage-truncation banner: a warning that cannot print its two disagreeing
115// counts is not evidence, it is a mood.
116func db_werrn(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 } let b: *u8 = sys_mmap(28); var i: i64 = 0; while i < k { b[i] = t[k-1-i]; i = i + 1 } sys_write(DB_STDERR, b, k); sys_munmap(t, 28); sys_munmap(b, 28); return 0 }
117func db_vlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
118func 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 }
119func db_catn(d: *u8, o: i64, v: i64) -> i64 { let t: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { d[o] = 45 as u8; o = o + 1; m = 0 - m } 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 }
120func 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 }
121func 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 }
122func db_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 {
123 var col: i64 = 0
124 var p: i64 = ls
125 while col < c {
126 var s: i64 = 1
127 while s == 1 { if p >= le { return 0 } if q[p] == (DB_TAB as u8) { s = 0 } else { p = p + 1 } }
128 p = p + 1
129 col = col + 1
130 }
131 var e: i64 = p
132 var s2: i64 = 1
133 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e] == (DB_TAB as u8) { s2 = 0 } else { e = e + 1 } } }
134 out[0] = p
135 out[1] = e
136 return 1
137}
138func db_span_lit(q: *u8, s: i64, e: i64, lit: *u8) -> i64 {
139 var i: i64 = 0
140 while s + i < e { if lit[i] == (0 as u8) { return 0 } if q[s+i] != lit[i] { return 0 } i = i + 1 }
141 if lit[i] != (0 as u8) { return 0 }
142 return 1
143}
144// exact-compare q[s..e) against a C-string (used to detect an identical already-filed desc)
145func db_tail_eq(q: *u8, s: i64, e: i64, lit: *u8) -> i64 {
146 let n: i64 = db_vlen(lit)
147 if e - s != n { return 0 }
148 var i: i64 = 0
149 while i < n { if q[s+i] != lit[i] { return 0 } i = i + 1 }
150 return 1
151}
152// seq1298: a caller-supplied [prefix] is a STORE PREFIX, not free text -- validate at the intake
153// boundary so a proof-string mistake fails LOUD with the contract, not as a phantom lock failure.
154// Valid: non-empty, fits DB_PATHCAP with room for "plock", ends with '-', chars in [a-z0-9_/-.].
155func db_prefix_ok(a: *u8) -> i64 {
156 let n: i64 = db_vlen(a)
157 if n < 2 { return 0 }
158 if n > DB_PATHCAP - 8 { return 0 }
159 if a[n-1] != (45 as u8) { return 0 }
160 var i: i64 = 0
161 while i < n {
162 let c: i64 = a[i] as i64
163 var ok: i64 = 0
164 if c >= 97 { if c <= 122 { ok = 1 } }
165 if c >= 48 { if c <= 57 { ok = 1 } }
166 if c == 95 { ok = 1 }
167 if c == 47 { ok = 1 }
168 if c == 45 { ok = 1 }
169 if c == 46 { ok = 1 }
170 if ok == 0 { return 0 }
171 i = i + 1
172 }
173 return 1
174}
175func db_take_prefix(a: *u8) -> *u8 {
176 if db_prefix_ok(a) == 1 { return a }
177 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)
178 sys_exit(DB_EXIT_USAGE)
179 return a
180}
181func db_lock(prefix: *u8) -> i64 {
182 let p: *u8 = sys_mmap(DB_PATHCAP)
183 var o: i64 = db_cat(p, 0, prefix)
184 o = db_cat(p, o, "plock" as *u8)
185 p[o] = 0 as u8
186 let fd: i64 = sys_openat_append(p, DB_MODE)
187 if fd < 0 {
188 // seq1298: name the PATH -- "cannot lock" alone blamed flock when the cause was open-fail
189 let m: *u8 = sys_mmap(DB_MSGCAP)
190 var mo: i64 = db_cat(m, 0, "DEBT-FAIL lock-file OPEN failed (not flock) path=" as *u8)
191 mo = db_cat(m, mo, p)
192 m[mo] = DB_NL as u8
193 mo = mo + 1
194 sys_write(DB_STDERR, m, mo)
195 return 0 - 1
196 }
197 sys_flock(fd, DB_LOCK_EX)
198 return fd
199}
200// count open rows in a loaded row-buffer
201// ★THE LEDGER MUST BE CLOSEABLE BY CONSTRUCTION, NOT BY PARSING PROSE (2026-07-31).
202// MEASURED THIS SESSION: nx_debtconfirm's rung-5 remainder scan CAUGHT 1785453431 (52 unmigrated seg-store
203// writers) but MISSED 1785445444, which describes the SAME open population in prose containing none of its
204// keywords -- a FALSE CLEAN on an ACTIVE data-loss defect. Widening the keyword list is chasing prose
205// forever; the population of ways to say "not finished" is unbounded.
206// SO INVERT THE DEFAULT: every row carries an explicit machine-readable closure field. A filer who states
207// one keeps it; a filer who states none gets CLOSURE=UNSPECIFIED, which a confirmer reads as PARTIAL and
208// NEVER closes mechanically. Absence of a closure criterion becomes EXPLICIT instead of invisible.
209// Additive (rule 19): existing rows are untouched and every guard above is unchanged.
210func db_find_sub(h: *u8, n: i64, s: *u8) -> i64 {
211 var sl: i64 = 0
212 while s[sl] != (0 as u8) { sl = sl + 1 }
213 var r: i64 = 0 - 1
214 var i: i64 = 0
215 while i < n {
216 if i + sl <= n {
217 var m: i64 = 1
218 var k: i64 = 0
219 while k < sl { if h[i+k] != s[k] { m = 0; k = sl } else { k = k + 1 } }
220 if m == 1 { r = i; i = n } else { i = i + 1 }
221 } else { i = n }
222 }
223 return r
224}
225
226func db_open_count(q: *u8, n: i64) -> i64 {
227 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
228 var k: i64 = 0
229 var i: i64 = 0
230 while i < n {
231 let le: i64 = db_le(q, i, n)
232 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 } }
233 i = le + 1
234 }
235 return k
236}
237func main(argc: i64, argv: *i64) -> i64 {
238 if argc < 2 { db_werr("usage: nx_debt {add <sev> <scope> <desc> | eat <epoch-id-or-index> | note <epoch-id-or-index> <text> | show <epoch-id-or-index> | page <off> <lim> | list} [prefix]\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
239 let verb: *u8 = argv[1] as *u8
240 var prefix: *u8 = "knowledge/store/debt-" as *u8
241 if db_span_lit(verb, 0, db_vlen(verb), "list" as *u8) == 1 {
242 if argc > 2 { prefix = db_take_prefix(argv[2] as *u8) }
243 let q: *u8 = sys_mmap(DB_CAP)
244 // HONEST LOAD (2026-07-25): sts_load stops at the q:n count key with NO signal, so a stale
245 // q:n made this verb report total=125 AS FACT while the plane held 734+ rows. sts_load_honest
246 // loads IDENTICALLY but also reports declared/loaded/unreachable, so a PARTIAL listing says so.
247 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
248 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
249 var idx: i64 = 0
250 var i: i64 = 0
251 while i < n {
252 let le: i64 = db_le(q, i, n)
253 let m: *u8 = sys_mmap(DB_MSGCAP)
254 var o: i64 = db_catn(m, 0, idx)
255 m[o] = 32 as u8
256 o = o + 1
257 sys_write(1, m, o)
258 sys_write(1, ((q as i64) + i) as *u8, le - i)
259 sys_write(1, "\n" as *u8, 1)
260 idx = idx + 1
261 i = le + 1
262 }
263 let k: i64 = db_open_count(q, n)
264 let m2: *u8 = sys_mmap(DB_MSGCAP)
265 var o2: i64 = db_cat(m2, 0, "DEBTS total=" as *u8)
266 o2 = db_catn(m2, o2, idx)
267 o2 = db_cat(m2, o2, " open=" as *u8)
268 o2 = db_catn(m2, o2, k)
269 o2 = db_cat(m2, o2, " declared_qn=" as *u8)
270 o2 = db_catn(m2, o2, dbf[0])
271 o2 = db_cat(m2, o2, " loaded=" as *u8)
272 o2 = db_catn(m2, o2, dbf[1])
273 o2 = db_cat(m2, o2, " unreachable_beyond_qn=" as *u8)
274 o2 = db_catn(m2, o2, dbf[2])
275 m2[o2] = DB_NL as u8
276 o2 = o2 + 1
277 sys_write(1, m2, o2)
278 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) }
279 sys_exit(0)
280 return 0
281 }
282 // ---------- reseed: THE REPAIR PATH THE FAIL-CLOSED GUARD LACKED ----------
283 //
284 // ★THE DEADLOCK THIS EXISTS TO BREAK (measured 2026-07-30): a torn write left
285 // the plane declaring 1495 rows while holding 1494, so the lossy-load guard
286 // refused EVERY add/eat -- correctly, because add/eat re-seed the whole plane
287 // and would bake the loss in. But the ONLY way to correct the declared count
288 // is a write. **A fail-closed guard with no repair route is not safety, it is
289 // a permanent outage** -- and this plane is the ecosystem's coordination
290 // ledger, so every lane went read-only with no way back.
291 //
292 // This verb re-seeds the plane from exactly the rows that ARE reachable, so
293 // the declared count becomes the true count. It is EVIDENCE-GATED and can
294 // only ever repair an over-declaration:
295 // * REFUSES when rows are reachable BEYOND the declared count -- that is
296 // the orphan case, where re-seeding would genuinely destroy data.
297 // * REFUSES when the plane is already consistent, so it cannot be used as
298 // a routine write path or to paper over a healthy plane.
299 // It prints declared/loaded before and the true count after, so the repair is
300 // auditable rather than a silent mutation of the SSOT.
301 if db_span_lit(verb, 0, db_vlen(verb), "reseed" as *u8) == 1 {
302 let lk9: i64 = db_lock(prefix)
303 if lk9 < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
304 let q9: *u8 = sys_mmap(DB_CAP)
305 let f9: *i64 = sys_mmap(DB_MSGCAP) as *i64
306 let n9: i64 = sts_load_honest(prefix, q9, DB_CAP, f9)
307 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 }
308 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 }
309 let m9: *u8 = sys_mmap(DB_MSGCAP)
310 var o9: i64 = db_cat(m9, 0, "DEBT-RESEED repairing over-declaration: declared_qn=" as *u8)
311 o9 = db_catn(m9, o9, f9[0])
312 o9 = db_cat(m9, o9, " loaded=" as *u8)
313 o9 = db_catn(m9, o9, f9[1])
314 o9 = db_cat(m9, o9, " -- re-seeding from the reachable rows\n" as *u8)
315 sys_write(1, m9, o9)
316 let c9: i64 = sts_seed(prefix, q9, n9)
317 if c9 < 0 { db_werr("DEBT-RESEED FAILED: commit error; plane unchanged\n" as *u8); sys_exit(1); return 1 }
318 var p9: i64 = db_cat(m9, 0, "DEBT-RESEED OK rows=" as *u8)
319 p9 = db_catn(m9, p9, c9)
320 p9 = db_cat(m9, p9, " (declared count now equals the true count; writes unblocked)\n" as *u8)
321 sys_write(1, m9, p9)
322 sys_exit(0)
323 return 0
324 }
325
326 if db_span_lit(verb, 0, db_vlen(verb), "add" as *u8) == 1 {
327 if argc < 5 { db_werr("add needs <sev> <scope> <desc>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
328 if argc > 5 { prefix = db_take_prefix(argv[5] as *u8) }
329 let sev: *u8 = argv[2] as *u8
330 if db_vlen(sev) != 1 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
331 let sc: i64 = sev[0] as i64
332 if sc < 49 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
333 if sc > 57 { db_werr("DEBT-FAIL sev must be 1..9\n" as *u8); sys_exit(1); return 1 }
334 let scope: *u8 = argv[3] as *u8
335 let desc: *u8 = argv[4] as *u8
336 let lk: i64 = db_lock(prefix)
337 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
338 let q: *u8 = sys_mmap(DB_CAP)
339 // FAIL-CLOSED GUARD (2026-07-25): add/eat REWRITE THE WHOLE PLANE from this buffer via
340 // sts_seed. If the load is PARTIAL that rewrite drops every row past the declared q:n --
341 // ~609 rows on the live debt- plane. Refuse instead: commit NOTHING when rows are unreachable.
342 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
343 let n: i64 = db_load_guarded(prefix, q, dbf)
344 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 }
345 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 }
346 // CONTENT-IDEMPOTENT (DEC014 mechanized). A transport error is a LOST RESPONSE, not a refused
347 // request, so a caller that retries a seemingly-failed add must NOT create a second row.
348 // PROVEN NEED: seq332/333/334 are the SAME edge-availability finding filed THREE times 11
349 // seconds apart. An exact-desc match is therefore a NO-OP naming the existing row, not an error.
350 // CANONICAL FORM SHARED BY COMPARATOR AND WRITER (2026-08-06). ROOT CAUSE OF A DEAD DEDUP:
351 // the writer appended " CLOSURE=UNSPECIFIED" (21 bytes) to the stored row AFTER this scan had
352 // already compared the RAW desc, and db_tail_eq rejects on ANY length mismatch -- so a stored row
353 // could NEVER equal the incoming text, and content-idempotence was silently dead for every filer
354 // that omits CLOSURE=, i.e. every zero-Claude autofiler.
355 // MEASURED 2026-08-06: 152 byte-identical netobs-beat rows on the live plane, each one asserting
356 // "this row re-files as DUPLICATE-SKIPPED". PROVEN BOTH WAYS before the fix: a desc WITHOUT
357 // CLOSURE= filed twice -> TWO rows; the same desc WITH CLOSURE= filed twice -> DUPLICATE-SKIPPED.
358 // One variable, opposite outcomes.
359 // ★A COMPARATOR AND A WRITER THAT DISAGREE ABOUT THE CANONICAL FORM OF A RECORD NEVER CONVERGE.
360 // Both paths now consume THIS buffer, so they cannot drift apart again BY CONSTRUCTION.
361 let cdesc: *u8 = sys_mmap(DB_DESCCAP)
362 let cdvl: i64 = db_vlen(desc)
363 if cdvl > DB_DESCCAP - 64 { db_werr("DEBT-FAIL desc exceeds DB_DESCCAP -- REFUSED rather than truncated, because a silently shortened desc would defeat the content-idempotent dedup and re-file forever. Shorten the desc or raise DB_DESCCAP deliberately.\n" as *u8); sys_exit(1); return 1 }
364 var cdo: i64 = db_cat(cdesc, 0, desc)
365 if db_find_sub(desc, cdvl, "CLOSURE=" as *u8) < 0 { cdo = db_cat(cdesc, cdo, " CLOSURE=UNSPECIFIED" as *u8) }
366 let dc: *i64 = sys_mmap(DB_SPAN) as *i64
367 let dc3: *i64 = sys_mmap(DB_SPAN) as *i64
368 var dupidx: i64 = 0 - 1
369 var drow: i64 = 0
370 var di: i64 = 0
371 while di < n {
372 let dle: i64 = db_le(q, di, n)
373 if dle > di {
374 // 2026-07-29 seq1271: dedup ONLY against OPEN rows. Matching rows of ANY status meant a
375 // recurrence of an EATEN debt could never re-file (alarm beats had to carry day-bucket
376 // workarounds in their descs). The original intent -- a retry after a transport flake must
377 // not double-file -- only ever concerns the just-added OPEN row.
378 if db_col(q, di, dle, 4, dc) == 1 { if db_tail_eq(q, dc[0], dle, cdesc) == 1 {
379 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 } } }
380 } }
381 drow = drow + 1
382 }
383 di = dle + 1
384 }
385 if dupidx >= 0 {
386 let dm: *u8 = sys_mmap(DB_MSGCAP)
387 var dmo: i64 = db_cat(dm, 0, "DEBT-DUPLICATE-SKIPPED existing_idx=" as *u8)
388 dmo = db_catn(dm, dmo, dupidx)
389 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)
390 sys_write(1, dm, dmo)
391 sys_exit(0)
392 return 0
393 }
394 let out: *u8 = sys_mmap(DB_CAP)
395 var o: i64 = 0
396 // 2026-07-31: DO NOT COPY THE WHOLE PLANE IN ORDER TO APPEND ONE ROW.
397 // This loop copied all n bytes of the loaded plane into `out` one byte at a time -- ~2.9MB and
398 // ~2.9M loop iterations on the live ledger -- and then sts_append_fast below was handed ONLY the
399 // new tail (out+n, o-n-1). The copy was DEAD WORK for the append path: nothing downstream reads
400 // out[0..n) except db_open_count, which can count the already-loaded `q` instead and add 1 for
401 // the row being filed (it is always written `open`).
402 // MEASURED BEFORE THIS CHANGE, on a ~2380-row plane: add=5963ms, eat=1590ms, show=98ms -- the
403 // supposedly O(1) append was the SLOWEST verb in the tool, and every seat paid ~6s per filing.
404 // The seq724 comment below is still true (the WRITE is 47 bytes) -- the cost was never the write.
405 // The row is now built at out[0..], so the append is handed out[0..o-1).
406 // NOTE the remaining full-plane passes are DELIBERATELY left alone: the dedup scan and the
407 // id-probe both genuinely need to read every row, and removing a correctness guard to win
408 // latency is the trade this lane exists to refuse. This change removes only work that had no
409 // reader at all.
410 // AN ID THAT COLLIDES IS NOT AN IDENTITY (measured 2026-07-31). seq1539 correctly retired the
411 // POSITIONAL seq because a re-seed made it address a different row -- and replaced it with an
412 // epoch that is unique only to ONE SECOND. Two adds inside the same second (a parallel or
413 // scripted filing, which is the normal case) get the SAME "STABLE" id; `eat` then fails closed
414 // on BOTH, so the tool hands back a close instruction that is guaranteed to fail and the rows
415 // become unclosable except by the positional index seq1539 exists to warn against. Proven live:
416 // two adds seconds apart both returned id=1785503735, and `show` reported the ambiguity.
417 // FIX AT THE SOURCE: while still holding the lock, probe forward to the first id no row uses.
418 // The id stays an integer written INTO the row (so it cannot drift) and now genuinely IDENTIFIES
419 // one. A bump of a few seconds is irrelevant as a timestamp and preserves ordering.
420 var now: i64 = sys_now_realtime_sec()
421 let idb: *u8 = sys_mmap(64)
422 let cc: *i64 = sys_mmap(DB_SPAN) as *i64
423 var probe: i64 = 0
424 var freeid: i64 = 0
425 while freeid == 0 {
426 let ln: i64 = db_catn(idb, 0, now)
427 idb[ln] = 0 as u8
428 var taken: i64 = 0
429 var sc: i64 = 0
430 while sc < n {
431 let sl: i64 = db_le(q, sc, n)
432 if db_col(q, sc, sl, 0, cc) == 1 {
433 if db_span_lit(q, cc[0], cc[1], idb) == 1 { taken = 1 }
434 }
435 sc = sl + 1
436 }
437 if taken == 0 { freeid = 1 } else {
438 now = now + 1
439 probe = probe + 1
440 if probe > DB_ID_MAXPROBE { db_werr("DEBT-FAIL cannot allocate a unique id\n" as *u8); sys_exit(1); return 1 }
441 }
442 }
443 o = db_catn(out, o, now)
444 out[o] = DB_TAB as u8
445 o = o + 1
446 out[o] = sev[0]
447 o = o + 1
448 out[o] = DB_TAB as u8
449 o = o + 1
450 o = db_cat(out, o, scope)
451 out[o] = DB_TAB as u8
452 o = o + 1
453 o = db_cat(out, o, "open" as *u8)
454 out[o] = DB_TAB as u8
455 o = o + 1
456 // INVERT THE DEFAULT (see db_find_sub above): stamp an explicit closure field when the filer gave
457 // none, so "no criterion" is a VALUE in the row rather than something a reader must infer from prose.
458 // 2026-08-06: that stamping now happens EXACTLY ONCE, into cdesc, BEFORE the dedup scan. The bytes
459 // committed here are therefore byte-identical to the text the comparator matched against. Re-deriving
460 // the suffix at this point (the old shape) is what silently killed content-idempotence for 152 rows.
461 o = db_cat(out, o, cdesc)
462 out[o] = DB_NL as u8
463 o = o + 1
464 // O(1) APPEND (2026-07-30, seq724 fix). Was sts_seed(prefix,out,o) which REWRITES THE WHOLE PLANE
465 // -- 1.4MB of disk writes to file one row. The new row is out[n..o-1] (out[0..n) is the loaded plane,
466 // out[o-1] is the trailing newline), so only those bytes need committing. Measured on a 2000-row
467 // plane: 47 bytes written instead of 210969. EVERY GUARD ABOVE IS UNCHANGED -- db_load_guarded, the
468 // partial-load refusal, the lossy refusal and the content-idempotent dedup all still run, because
469 // dedup genuinely needs the full read. ONLY THE WRITE CHANGED. Two defect classes die BY
470 // CONSTRUCTION: a partial load can no longer drop rows (nothing is rewritten), and ordinals no
471 // longer shift (a re-seed RE-SEGMENTS rows; an append touches no existing key). NOTE the eat path
472 // below KEEPS sts_seed on purpose -- it MODIFIES an existing row and must rewrite.
473 let cnt: i64 = sts_append_fast(prefix, out, o - 1)
474 if cnt < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
475 // `out` now holds ONLY the new row, so the open tally comes from the loaded plane `q` plus the
476 // row just filed (it is always written `open`). The eat path below still counts `out` because
477 // there `out` genuinely IS the whole rewritten plane.
478 let k: i64 = db_open_count(q, n) + 1
479 let m: *u8 = sys_mmap(DB_MSGCAP)
480 // seq1539 -- HAND BACK A STABLE HANDLE. `seq` is cnt-1: a ROW ORDINAL, not an identity. A re-seed
481 // that RE-SEGMENTS rows (any row containing a newline splits) shifts every ordinal after it, so a
482 // seq issued before the re-seed silently addresses a DIFFERENT row afterwards -- that is exactly how
483 // `nx_debt eat <seq>` closed another lane's row today. The epoch in col0 is the row's real identity:
484 // it is written INTO the row, so it cannot drift. Echo it FIRST and name it `id`, and label seq as
485 // what it is, so the obvious copy-paste closes the right row.
486 var mo: i64 = db_cat(m, 0, "DEBT-ADDED id=" as *u8)
487 mo = db_catn(m, mo, now)
488 mo = db_cat(m, mo, " (STABLE -- close with: nx_debt eat " as *u8)
489 mo = db_catn(m, mo, now)
490 mo = db_cat(m, mo, ") seq=" as *u8)
491 mo = db_catn(m, mo, cnt - 1)
492 mo = db_cat(m, mo, " (POSITIONAL -- shifts on re-seed, do NOT close by this) open=" as *u8)
493 mo = db_catn(m, mo, k)
494 m[mo] = DB_NL as u8
495 mo = mo + 1
496 sys_write(1, m, mo)
497 sys_exit(0)
498 return 0
499 }
500 // ---- dedup: CONSOLIDATE byte-identical OPEN rows (2026-08-06) -------------------------------
501 // WHY THIS EXISTS: `add` is content-idempotent, but that guard was DEAD for every filer that omits
502 // CLOSURE= (the writer stamped the closure field AFTER the comparator ran -- root-caused and fixed
503 // above). While it was dead, zero-Claude autofilers piled up byte-identical rows: 152 netobs-beat
504 // rows on the live plane, ~11% of gating[open,sev>=6]. The add-path fix stops the BLEEDING; this
505 // verb cleans the WOUND.
506 // ★A FIX THAT STOPS NEW DUPLICATES DOES NOT REMOVE THE OLD ONES -- THE BACKLOG IS ITS OWN TASK.
507 // SAFE BY CONSTRUCTION: DRY-RUN unless argv[2]=="apply"; only ever flips status open->eaten (rule 13
508 // soft-delete -- no row is ever removed and the text survives verbatim); ALWAYS keeps the EARLIEST
509 // row of each group so a group can never be fully closed; ONE sts_seed commits, so N closures cost
510 // ONE plane rewrite instead of N (151 sequential eats would rewrite the whole plane 151 times).
511 if db_span_lit(verb, 0, db_vlen(verb), "dedup" as *u8) == 1 {
512 var apply: i64 = 0
513 if argc > 2 { if db_span_lit(argv[2] as *u8, 0, db_vlen(argv[2] as *u8), "apply" as *u8) == 1 { apply = 1 } }
514 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
515 let lk: i64 = db_lock(prefix)
516 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
517 let q: *u8 = sys_mmap(DB_CAP)
518 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
519 let n: i64 = db_load_guarded(prefix, q, dbf)
520 if dbf[2] > 0 { db_werr("DEBT-REFUSED partial load: rows are reachable BEYOND the declared q:n and dedup re-seeds the WHOLE plane from this buffer, so committing would ORPHAN them. NOTHING COMMITTED.\n" as *u8); sys_exit(1); return 1 }
521 if dbf[1] < dbf[0] { db_werr("DEBT-REFUSED lossy load: loaded FEWER rows than the declared q:n on two consecutive reads. Committing would BAKE the loss in. NOTHING COMMITTED. Verify with nx_plane_repair <prefix> and NO confirm.\n" as *u8); sys_exit(1); return 1 }
522 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
523 let hs: *i64 = sys_mmap(DB_ROWMAX * 8) as *i64
524 let dss: *i64 = sys_mmap(DB_ROWMAX * 8) as *i64
525 let dee: *i64 = sys_mmap(DB_ROWMAX * 8) as *i64
526 let opn: *i64 = sys_mmap(DB_ROWMAX * 8) as *i64
527 let dupf: *i64 = sys_mmap(DB_ROWMAX * 8) as *i64
528 let k3: *i64 = sys_mmap(DB_SPAN) as *i64
529 let k4: *i64 = sys_mmap(DB_SPAN) as *i64
530 var rows: i64 = 0
531 var i: i64 = 0
532 while i < n {
533 let le: i64 = db_le(q, i, n)
534 if le > i {
535 if rows >= DB_ROWMAX { db_werr("DEBT-REFUSED plane exceeds DB_ROWMAX -- REFUSED rather than deduping a PREFIX of the plane, because a partial sweep reports clean while duplicates remain behind it. Raise DB_ROWMAX deliberately.\n" as *u8); sys_exit(1); return 1 }
536 var isop: i64 = 0
537 if db_col(q, i, le, 3, k3) == 1 { if db_span_lit(q, k3[0], k3[1], "open" as *u8) == 1 { isop = 1 } }
538 var s0: i64 = i
539 if db_col(q, i, le, 4, k4) == 1 { s0 = k4[0] }
540 // desc is the LAST column and may itself contain tabs, so its span runs to the LINE END --
541 // exactly the span `add`'s comparator uses (db_tail_eq(q, dc[0], dle, ...)). Using db_col's
542 // own column end here would compare a TRUNCATED desc and merge rows that merely share a
543 // prefix -- a consolidator that over-matches destroys distinct findings.
544 var hv: i64 = DB_MAGIC_1469598103934665603
545 var t: i64 = s0
546 while t < le { hv = hv ^ (q[t] as i64); hv = hv * DB_MAGIC_1099511628211; t = t + 1 }
547 hs[rows] = hv
548 dss[rows] = s0
549 dee[rows] = le
550 opn[rows] = isop
551 dupf[rows] = 0
552 rows = rows + 1
553 }
554 i = le + 1
555 }
556 var groups: i64 = 0
557 var marked: i64 = 0
558 var a: i64 = 0
559 while a < rows {
560 if opn[a] == 1 { if dupf[a] == 0 {
561 var found: i64 = 0
562 let la: i64 = dee[a] - dss[a]
563 var b: i64 = a + 1
564 while b < rows {
565 if opn[b] == 1 { if dupf[b] == 0 { if hs[b] == hs[a] { if dee[b] - dss[b] == la {
566 // hash+length are only a PREFILTER; the commit decision is a full byte compare.
567 var same: i64 = 1
568 var u: i64 = 0
569 while u < la { if q[dss[a] + u] != q[dss[b] + u] { same = 0; u = la } else { u = u + 1 } }
570 if same == 1 { dupf[b] = 1; marked = marked + 1; found = 1 }
571 } } } }
572 b = b + 1
573 }
574 if found == 1 { groups = groups + 1 }
575 } }
576 a = a + 1
577 }
578 let m: *u8 = sys_mmap(DB_MSGCAP)
579 var mo: i64 = 0
580 if apply == 0 {
581 mo = db_cat(m, 0, "DEBT-DEDUP DRY-RUN rows=" as *u8)
582 mo = db_catn(m, mo, rows)
583 mo = db_cat(m, mo, " groups=" as *u8)
584 mo = db_catn(m, mo, groups)
585 mo = db_cat(m, mo, " would_eat=" as *u8)
586 mo = db_catn(m, mo, marked)
587 mo = db_cat(m, mo, " (earliest row of every group is KEPT open; NOTHING COMMITTED) -- apply with: nx_debt dedup apply [prefix]\n" as *u8)
588 sys_write(1, m, mo)
589 sys_exit(0)
590 return 0
591 }
592 let out: *u8 = sys_mmap(DB_CAP)
593 var o: i64 = 0
594 var r2: i64 = 0
595 var i2: i64 = 0
596 while i2 < n {
597 let le2: i64 = db_le(q, i2, n)
598 var sub: i64 = 0
599 if le2 > i2 { if dupf[r2] == 1 { if db_col(q, i2, le2, 3, k3) == 1 { sub = 1 } } }
600 if sub == 1 {
601 var t4: i64 = i2
602 while t4 < k3[0] { out[o] = q[t4]; o = o + 1; t4 = t4 + 1 }
603 o = db_cat(out, o, "eaten" as *u8)
604 var t5: i64 = k3[1]
605 while t5 < le2 { out[o] = q[t5]; o = o + 1; t5 = t5 + 1 }
606 } else {
607 var t6: i64 = i2
608 while t6 < le2 { out[o] = q[t6]; o = o + 1; t6 = t6 + 1 }
609 }
610 out[o] = DB_NL as u8
611 o = o + 1
612 if le2 > i2 { r2 = r2 + 1 }
613 i2 = le2 + 1
614 }
615 let cnt2: i64 = sts_seed(prefix, out, o)
616 if cnt2 < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
617 let kk: i64 = db_open_count(out, o)
618 mo = db_cat(m, 0, "DEBT-DEDUP APPLIED groups=" as *u8)
619 mo = db_catn(m, mo, groups)
620 mo = db_cat(m, mo, " eaten=" as *u8)
621 mo = db_catn(m, mo, marked)
622 mo = db_cat(m, mo, " rows=" as *u8)
623 mo = db_catn(m, mo, cnt2)
624 mo = db_cat(m, mo, " open=" as *u8)
625 mo = db_catn(m, mo, kk)
626 m[mo] = DB_NL as u8
627 mo = mo + 1
628 sys_write(1, m, mo)
629 sys_exit(0)
630 return 0
631 }
632 if db_span_lit(verb, 0, db_vlen(verb), "eat" as *u8) == 1 {
633 if argc < 3 { db_werr("eat needs <seq>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
634 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
635 let lk: i64 = db_lock(prefix)
636 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
637 let q: *u8 = sys_mmap(DB_CAP)
638 // FAIL-CLOSED GUARD (2026-07-25): add/eat REWRITE THE WHOLE PLANE from this buffer via
639 // sts_seed. If the load is PARTIAL that rewrite drops every row past the declared q:n --
640 // ~609 rows on the live debt- plane. Refuse instead: commit NOTHING when rows are unreachable.
641 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
642 let n: i64 = db_load_guarded(prefix, q, dbf)
643 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 }
644 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 }
645 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
646 let c0: *i64 = sys_mmap(DB_SPAN) as *i64
647 var target: i64 = 0 - 1
648 var idmatches: i64 = 0
649 var ridx: i64 = 0
650 var scan: i64 = 0
651 while scan < n {
652 let sle: i64 = db_le(q, scan, n)
653 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 } }
654 ridx = ridx + 1
655 scan = sle + 1
656 }
657 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 }
658 if idmatches == 0 { target = db_atoi(argv[2] as *u8) }
659 let out: *u8 = sys_mmap(DB_CAP)
660 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
661 var o: i64 = 0
662 var idx: i64 = 0
663 var hit: i64 = 0
664 var already: i64 = 0
665 var ea: i64 = 0
666 var eb: i64 = 0
667 var i: i64 = 0
668 while i < n {
669 let le: i64 = db_le(q, i, n)
670 var sub: i64 = 0
671 if idx == target {
672 if db_col(q, i, le, 3, c3) == 1 {
673 if db_span_lit(q, c3[0], c3[1], "open" as *u8) == 1 { sub = 1 } else { already = 1 }
674 hit = 1
675 ea = i
676 eb = le
677 }
678 }
679 if sub == 1 {
680 var t: i64 = i
681 while t < c3[0] { out[o] = q[t]; o = o + 1; t = t + 1 }
682 o = db_cat(out, o, "eaten" as *u8)
683 var t2: i64 = c3[1]
684 while t2 < le { out[o] = q[t2]; o = o + 1; t2 = t2 + 1 }
685 } else {
686 var t3: i64 = i
687 while t3 < le { out[o] = q[t3]; o = o + 1; t3 = t3 + 1 }
688 }
689 out[o] = DB_NL as u8
690 o = o + 1
691 idx = idx + 1
692 i = le + 1
693 }
694 if hit == 0 { db_werr("DEBT-FAIL no row matches epoch-id or index (nothing committed)\n" as *u8); sys_exit(1); return 1 }
695 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 }
696 let cnt: i64 = sts_seed(prefix, out, o)
697 if cnt < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
698 let k: i64 = db_open_count(out, o)
699 let m: *u8 = sys_mmap(DB_MSGCAP)
700 // ★LEAD WITH THE IDENTITY, NOT THE POSITION (seq1539's affordance half). The eat resolved by
701 // epoch, but v1 of this line printed "idx=<pos>" FIRST -- so the number an operator copied out
702 // of a success message was the one that shifts on the next re-seed. That is how I closed a
703 // sibling's row. The stable id leads; the position is labelled as the throwaway it is.
704 let ce: *i64 = sys_mmap(DB_SPAN) as *i64
705 db_puts("DEBT-EATEN id=" as *u8)
706 if db_col(q, ea, eb, 0, ce) == 1 { sys_write(1, ((q as i64) + ce[0]) as *u8, ce[1] - ce[0]) }
707 var mo: i64 = db_cat(m, 0, " at_idx=" as *u8)
708 mo = db_catn(m, mo, target)
709 mo = db_cat(m, mo, " (POSITIONAL -- do NOT reuse) open=" as *u8)
710 mo = db_catn(m, mo, k)
711 m[mo] = DB_NL as u8
712 mo = mo + 1
713 sys_write(1, m, mo)
714 db_puts("ROW: " as *u8)
715 sys_write(1, ((q as i64) + ea) as *u8, eb - ea)
716 sys_write(1, "\n" as *u8, 1)
717 sys_exit(0)
718 return 0
719 }
720 // NOTE (2026-09-02): append progress to a row WITHOUT closing it -- the verb the estate lacked when a lane had addressed
721 // a cross-lane debt for ONE domain (measurement-lab order, row 1788360545) and could only choose between eating another
722 // lane's row or leaving no trace. Same schema, same readers: the text is appended to the DESC column as
723 // " || NOTE <epoch>: <text>", so dashboards keep parsing positionally and `show` prints it. Same fail-closed
724 // guards and the same whole-plane re-seed as eat; an exact repeat of the last note is a no-op (idempotent).
725 if db_span_lit(verb, 0, db_vlen(verb), "note" as *u8) == 1 {
726 if argc < 4 { db_werr("note needs <epoch-id-or-index> <text>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
727 if argc > 4 { prefix = db_take_prefix(argv[4] as *u8) }
728 let lk: i64 = db_lock(prefix)
729 if lk < 0 { db_werr("DEBT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 }
730 let q: *u8 = sys_mmap(DB_CAP)
731 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
732 let n: i64 = db_load_guarded(prefix, q, dbf)
733 if dbf[2] > 0 { db_werr("DEBT-REFUSED partial load: rows are reachable BEYOND the declared q:n and note re-seeds the WHOLE plane from this buffer, so committing would ORPHAN them. NOTHING COMMITTED.\n" as *u8); sys_exit(1); return 1 }
734 if dbf[1] < dbf[0] { db_werr("DEBT-REFUSED lossy load: loaded FEWER rows than the declared q:n on two consecutive reads. NOTHING COMMITTED.\n" as *u8); sys_exit(1); return 1 }
735 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
736 let c0: *i64 = sys_mmap(DB_SPAN) as *i64
737 var target: i64 = 0 - 1
738 var idmatches: i64 = 0
739 var ridx: i64 = 0
740 var scan: i64 = 0
741 while scan < n {
742 let sle: i64 = db_le(q, scan, n)
743 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 } }
744 ridx = ridx + 1
745 scan = sle + 1
746 }
747 if idmatches > 1 { db_werr("DEBT-FAIL ambiguous epoch id matches multiple rows (fail-closed; note by index instead)\n" as *u8); sys_exit(1); return 1 }
748 if idmatches == 0 { target = db_atoi(argv[2] as *u8) }
749 let text: *u8 = argv[3] as *u8
750 let out: *u8 = sys_mmap(DB_CAP)
751 let c4: *i64 = sys_mmap(DB_SPAN) as *i64
752 var o: i64 = 0
753 var idx: i64 = 0
754 var hit: i64 = 0
755 var same: i64 = 0
756 var ea: i64 = 0
757 var eb: i64 = 0
758 var i: i64 = 0
759 while i < n {
760 let le: i64 = db_le(q, i, n)
761 var sub: i64 = 0
762 if idx == target { if db_col(q, i, le, 4, c4) == 1 { sub = 1; hit = 1; ea = i; eb = le } }
763 if sub == 1 {
764 // idempotent: if the desc already ENDS with this exact text (a retried note after a dropped response), commit nothing new
765 let tl: i64 = db_vlen(text)
766 if c4[1] - c4[0] >= tl { if db_span_lit(q, c4[1] - tl, c4[1], text) == 1 { same = 1 } }
767 var t: i64 = i
768 while t < c4[1] { out[o] = q[t]; o = o + 1; t = t + 1 }
769 if same == 0 {
770 o = db_cat(out, o, " || NOTE " as *u8)
771 o = db_catn(out, o, sys_now_realtime_sec())
772 o = db_cat(out, o, ": " as *u8)
773 o = db_cat(out, o, text)
774 }
775 var t2: i64 = c4[1]
776 while t2 < le { out[o] = q[t2]; o = o + 1; t2 = t2 + 1 }
777 } else {
778 var t3: i64 = i
779 while t3 < le { out[o] = q[t3]; o = o + 1; t3 = t3 + 1 }
780 }
781 out[o] = DB_NL as u8
782 o = o + 1
783 idx = idx + 1
784 i = le + 1
785 }
786 if hit == 0 { db_werr("DEBT-FAIL no row matches epoch-id or index (nothing committed)\n" as *u8); sys_exit(1); return 1 }
787 if same == 1 { db_puts("DEBT-NOTED already (idempotent)\n" as *u8); sys_exit(0); return 0 }
788 let cnt: i64 = sts_seed(prefix, out, o)
789 if cnt < 0 { db_werr("DEBT-FAIL commit error\n" as *u8); sys_exit(1); return 1 }
790 let ce: *i64 = sys_mmap(DB_SPAN) as *i64
791 db_puts("DEBT-NOTED id=" as *u8)
792 if db_col(q, ea, eb, 0, ce) == 1 { sys_write(1, ((q as i64) + ce[0]) as *u8, ce[1] - ce[0]) }
793 let m: *u8 = sys_mmap(DB_MSGCAP)
794 var mo: i64 = db_cat(m, 0, " at_idx=" as *u8)
795 mo = db_catn(m, mo, target)
796 mo = db_cat(m, mo, " (POSITIONAL -- do NOT reuse) rows=" as *u8)
797 mo = db_catn(m, mo, cnt)
798 m[mo] = DB_NL as u8
799 mo = mo + 1
800 sys_write(1, m, mo)
801 sys_exit(0)
802 return 0
803 }
804 if db_span_lit(verb, 0, db_vlen(verb), "page" as *u8) == 1 {
805 if argc < 4 { db_werr("page needs <offset> <limit>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
806 if argc > 4 { prefix = db_take_prefix(argv[4] as *u8) }
807 let off: i64 = db_atoi(argv[2] as *u8)
808 let lim: i64 = db_atoi(argv[3] as *u8)
809 let q: *u8 = sys_mmap(DB_CAP)
810 // READ-ONLY VERB: WARN, NEVER REFUSE. The two guards below were copy-pasted here from the
811 // add/eat paths -- comment and all -- but `page` COMMITS NOTHING, so "committing would BAKE
812 // the loss / NOTHING COMMITTED" was never true of it. The effect was that during any
813 // contention window the board became UNREADABLE: the one verb you reach for to diagnose a
814 // sick plane exited 1 and printed no rows. Refusing to READ is strictly worse than showing a
815 // possibly-incomplete view, because a read cannot damage anything. So surface the anomaly on
816 // stderr and STILL PRINT what is reachable. (seq1521/1532)
817 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
818 let n: i64 = db_load_guarded(prefix, q, dbf)
819 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) }
820 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) }
821 var idx: i64 = 0
822 var shown: i64 = 0
823 var i: i64 = 0
824 while i < n {
825 let le: i64 = db_le(q, i, n)
826 var pr: i64 = 0
827 if idx >= off { if shown < lim { pr = 1 } }
828 if pr == 1 {
829 let m: *u8 = sys_mmap(DB_MSGCAP)
830 var o: i64 = db_catn(m, 0, idx)
831 m[o] = 32 as u8
832 o = o + 1
833 sys_write(1, m, o)
834 sys_write(1, ((q as i64) + i) as *u8, le - i)
835 sys_write(1, "\n" as *u8, 1)
836 shown = shown + 1
837 }
838 idx = idx + 1
839 i = le + 1
840 }
841 let k: i64 = db_open_count(q, n)
842 let m2: *u8 = sys_mmap(DB_MSGCAP)
843 var o2: i64 = db_cat(m2, 0, "DEBTS-PAGE off=" as *u8)
844 o2 = db_catn(m2, o2, off)
845 o2 = db_cat(m2, o2, " lim=" as *u8)
846 o2 = db_catn(m2, o2, lim)
847 o2 = db_cat(m2, o2, " shown=" as *u8)
848 o2 = db_catn(m2, o2, shown)
849 o2 = db_cat(m2, o2, " total=" as *u8)
850 o2 = db_catn(m2, o2, idx)
851 o2 = db_cat(m2, o2, " open=" as *u8)
852 o2 = db_catn(m2, o2, k)
853 m2[o2] = DB_NL as u8
854 o2 = o2 + 1
855 sys_write(1, m2, o2)
856 sys_exit(0)
857 return 0
858 }
859 if db_span_lit(verb, 0, db_vlen(verb), "sev" as *u8) == 1 {
860 // sev <minsev> [prefix] -- OPEN rows with sev >= minsev over the WHOLE corpus, in-organ.
861 // 2026-07-29: nx_debt_view's 512KB fork-capture went blind past ~734 rows, so its sev hunt
862 // silently missed sev-8 rows. The eat-most-important-first pointer must see EVERYTHING;
863 // this verb walks the same honest load list/page use -- no fork, no capture cap.
864 if argc < 3 { db_werr("sev needs <minsev>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
865 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
866 let minsev: i64 = db_atoi(argv[2] as *u8)
867 let q: *u8 = sys_mmap(DB_CAP)
868 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
869 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
870 if dbf[2] > 0 { db_werr("DEBT-TRUNCATED-LOAD this listing is PARTIAL -- rows exist beyond the declared q:n\n" as *u8) }
871 let sp: *i64 = sys_mmap(DB_SPAN) as *i64
872 let c3: *i64 = sys_mmap(DB_SPAN) as *i64
873 var idx: i64 = 0
874 var hit: i64 = 0
875 var shown: i64 = 0
876 var i: i64 = 0
877 while i < n {
878 let le: i64 = db_le(q, i, n)
879 var ok: i64 = 0
880 if db_col(q, i, le, 3, c3) == 1 { if db_span_lit(q, c3[0], c3[1], "open" as *u8) == 1 { ok = 1 } }
881 if ok == 1 {
882 var sv: i64 = 0
883 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 } } } }
884 if sv >= minsev {
885 hit = hit + 1
886 if shown < 40 {
887 let m: *u8 = sys_mmap(DB_MSGCAP)
888 var o: i64 = db_catn(m, 0, idx)
889 m[o] = 32 as u8
890 o = o + 1
891 sys_write(1, m, o)
892 var rl: i64 = le - i
893 var tr: i64 = 0
894 if rl > 700 { rl = 700; tr = 1 }
895 sys_write(1, ((q as i64) + i) as *u8, rl)
896 if tr == 1 { sys_write(1, "...[ROW-TRUNC-700]" as *u8, 18) }
897 sys_write(1, "\n" as *u8, 1)
898 shown = shown + 1
899 }
900 }
901 }
902 idx = idx + 1
903 i = le + 1
904 }
905 let m3: *u8 = sys_mmap(DB_MSGCAP)
906 var o3: i64 = db_cat(m3, 0, "DEBTS-SEV min=" as *u8)
907 o3 = db_catn(m3, o3, minsev)
908 o3 = db_cat(m3, o3, " matched=" as *u8)
909 o3 = db_catn(m3, o3, hit)
910 o3 = db_cat(m3, o3, " shown=" as *u8)
911 o3 = db_catn(m3, o3, shown)
912 o3 = db_cat(m3, o3, " total=" as *u8)
913 o3 = db_catn(m3, o3, idx)
914 o3 = db_cat(m3, o3, " cap-40-shown row-trunc-700" as *u8)
915 m3[o3] = DB_NL as u8
916 o3 = o3 + 1
917 sys_write(1, m3, o3)
918 sys_exit(0)
919 return 0
920 }
921 if db_span_lit(verb, 0, db_vlen(verb), "find" as *u8) == 1 {
922 // find <substr> [prefix] -- any-status substring scan over the WHOLE corpus, in-organ (same
923 // rationale as sev: the fork-captured view was blind past ~734 rows).
924 if argc < 3 { db_werr("find needs <substr>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
925 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
926 let pat: *u8 = argv[2] as *u8
927 var pl: i64 = 0
928 while pat[pl] != (0 as u8) { pl = pl + 1 }
929 let q: *u8 = sys_mmap(DB_CAP)
930 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
931 let n: i64 = sts_load_honest(prefix, q, DB_CAP, dbf)
932 if dbf[2] > 0 { db_werr("DEBT-TRUNCATED-LOAD this listing is PARTIAL -- rows exist beyond the declared q:n\n" as *u8) }
933 var idx: i64 = 0
934 var hit: i64 = 0
935 var shown: i64 = 0
936 var i: i64 = 0
937 while i < n {
938 let le: i64 = db_le(q, i, n)
939 var mth: i64 = 0
940 if pl > 0 {
941 var j: i64 = i
942 while j + pl <= le {
943 var k2: i64 = 0
944 var eq: i64 = 1
945 while k2 < pl { if q[j + k2] != pat[k2] { eq = 0; k2 = pl } else { k2 = k2 + 1 } }
946 if eq == 1 { mth = 1; j = le } else { j = j + 1 }
947 }
948 }
949 if mth == 1 {
950 hit = hit + 1
951 if shown < 40 {
952 let m: *u8 = sys_mmap(DB_MSGCAP)
953 var o: i64 = db_catn(m, 0, idx)
954 m[o] = 32 as u8
955 o = o + 1
956 sys_write(1, m, o)
957 var rl: i64 = le - i
958 var tr: i64 = 0
959 if rl > 700 { rl = 700; tr = 1 }
960 sys_write(1, ((q as i64) + i) as *u8, rl)
961 if tr == 1 { sys_write(1, "...[ROW-TRUNC-700]" as *u8, 18) }
962 sys_write(1, "\n" as *u8, 1)
963 shown = shown + 1
964 }
965 }
966 idx = idx + 1
967 i = le + 1
968 }
969 let m4: *u8 = sys_mmap(DB_MSGCAP)
970 var o4: i64 = db_cat(m4, 0, "DEBTS-FIND matched=" as *u8)
971 o4 = db_catn(m4, o4, hit)
972 o4 = db_cat(m4, o4, " shown=" as *u8)
973 o4 = db_catn(m4, o4, shown)
974 o4 = db_cat(m4, o4, " total=" as *u8)
975 o4 = db_catn(m4, o4, idx)
976 o4 = db_cat(m4, o4, " cap-40-shown row-trunc-700" as *u8)
977 m4[o4] = DB_NL as u8
978 o4 = o4 + 1
979 sys_write(1, m4, o4)
980 sys_exit(0)
981 return 0
982 }
983 if db_span_lit(verb, 0, db_vlen(verb), "show" as *u8) == 1 {
984 if argc < 3 { db_werr("show needs <epoch-id-or-index>\n" as *u8); sys_exit(DB_EXIT_USAGE); return DB_EXIT_USAGE }
985 if argc > 3 { prefix = db_take_prefix(argv[3] as *u8) }
986 let q: *u8 = sys_mmap(DB_CAP)
987 // READ-ONLY VERB: WARN, NEVER REFUSE -- `show` commits nothing, so the write-path refusal that
988 // was copy-pasted here (comment and all) could only ever HIDE the row you asked to read.
989 // (seq1521/1532)
990 let dbf: *i64 = sys_mmap(DB_MSGCAP) as *i64
991 let n: i64 = db_load_guarded(prefix, q, dbf)
992 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) }
993 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) }
994 if n <= 0 { db_werr("DEBT-FAIL store empty\n" as *u8); sys_exit(1); return 1 }
995 let c0: *i64 = sys_mmap(DB_SPAN) as *i64
996 var target: i64 = 0 - 1
997 var idmatches: i64 = 0
998 var ridx: i64 = 0
999 var scan: i64 = 0
1000 while scan < n {
1001 let sle: i64 = db_le(q, scan, n)
1002 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 } }
1003 ridx = ridx + 1
1004 scan = sle + 1
1005 }
1006 if idmatches == 0 { target = db_atoi(argv[2] as *u8) }
1007 var idx: i64 = 0
1008 var hit: i64 = 0
1009 var i: i64 = 0
1010 while i < n {
1011 let le: i64 = db_le(q, i, n)
1012 if idx == target {
1013 hit = 1
1014 let m: *u8 = sys_mmap(DB_MSGCAP)
1015 var o: i64 = db_cat(m, 0, "ROW idx=" as *u8)
1016 o = db_catn(m, o, idx)
1017 m[o] = 32 as u8
1018 o = o + 1
1019 sys_write(1, m, o)
1020 sys_write(1, ((q as i64) + i) as *u8, le - i)
1021 sys_write(1, "\n" as *u8, 1)
1022 }
1023 idx = idx + 1
1024 i = le + 1
1025 }
1026 if idmatches > 1 { db_puts("NOTE: epoch id ambiguous (multiple rows); showed LAST match; eat would REFUSE\n" as *u8) }
1027 if hit == 0 { db_werr("DEBT-FAIL no row matches\n" as *u8); sys_exit(1); return 1 }
1028 sys_exit(0)
1029 return 0
1030 }
1031 db_werr("usage: nx_debt {add <sev> <scope> <desc> | eat <epoch-id-or-index> | note <epoch-id-or-index> <text> | show <epoch-id-or-index> | page <off> <lim> | list} [prefix]\n" as *u8)
1032 sys_exit(DB_EXIT_USAGE)
1033 return DB_EXIT_USAGE
1034}