nx_planefit.nx source
↩ module page · 244 lines · 11008 B
1// nx_planefit.nx -- READ-ONLY: does a plane FIT a caller's read cap, and by how much does it miss?
2//
3// WHY THIS EXISTS. sts_load silently drops bytes once its output buffer fills and returns a byte
4// count that looks perfectly healthy, so an under-capped reader reports on a PARTIAL board and is
5// indistinguishable from a healthy one. The truncation WARNING added 2026-08-06 makes that visible
6// at the moment of the read -- but only for someone watching stderr of a run that already happened.
7// This answers the question BEFORE the run, for any plane and any cap, without writing anything.
8//
9// MEASURED MOTIVATION (2026-08-06): knowledge/store/debt- loads to 4,396,892B while nx_sheriff
10// (SHF_CAP), nx_pm_board (PB_CAP) and nx_dora (DCAP) each read it with a 1 MiB cap -- each losing
11// 76% of the board, newest rows first. On-disk bytes are NOT the answer (they include superseded
12// segment versions), so the only honest number is what sts_load actually returns.
13//
14// nx_planefit <plane-prefix> [cap] -- one plane; exit 1 if it truncates at that cap
15// nx_planefit sweep [cap] -- EVERY plane under knowledge/store; exit 1 if any truncates
16//
17// ★THE SWEEP IS THE POINT. Checking four planes by hand is a spot check; the defect class is
18// "which of my readers is silently blind RIGHT NOW", and that is a census question. This is the
19// complement to nx_capcliff, which scans FLAT LEDGERS against ONE hardcoded cap and has no notion
20// of seg-store planes.
21// ⚠TWO DECLARED BOUNDS, NEITHER SILENT: (1) the probe ceiling PF_CEIL -- if true_bytes comes back
22// EQUAL to it, the number is a FLOOR and the tool says so; (2) PF_SWEEP_ROWCAP -- a plane declaring
23// more rows than this is REPORTED AND SKIPPED rather than walked, because sts_load walks
24// q:0..q:n-1 and a multi-million-row plane would turn a census into an outage. A skipped plane is
25// printed with its declared count, so the sweep can never pass off partial coverage as complete.
26// license_tier: ORIGINAL No hw writes (Rule 26).
27import "nx_store_seed_lib.nx"
28import "nx_seg_store.nx"
29import "nx_syscalls.nx"
30
31const PF_CEIL: i64 = 67108864 // 64 MiB probe ceiling -- far above any measured organ cap
32const PF_DEFAULT_CAP: i64 = 1048576 // 1 MiB: the cap nx_sheriff/nx_pm_board/nx_dora actually used
33const PF_SWEEP_ROWCAP: i64 = 60000 // declared-row bound for the sweep; above this: report + skip
34const PF_DIRBUF: i64 = 262144
35const PF_STORE: *u8 = "knowledge/store"
36const PF_PATHCAP: i64 = 512
37const PF_OUTCAP: i64 = 16
38const PF_ZERO: i64 = 48
39const PF_NINE: i64 = 57
40const PF_B10: i64 = 10
41const PF_EXIT_FITS: i64 = 0
42const PF_EXIT_TRUNC: i64 = 1
43const PF_EXIT_USAGE: i64 = 2
44
45func pf_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
46func pf_n(v: i64) -> i64 {
47 if v == 0 { pf_w("0" as *u8); return 0 }
48 var x: i64 = v
49 if x < 0 { pf_w("-" as *u8); x = 0 - x }
50 let b: *u8 = sys_mmap(32)
51 var i: i64 = 0
52 while x > 0 { b[i] = ((x % PF_B10) + PF_ZERO) as u8; x = x / PF_B10; i = i + 1 }
53 while i > 0 { i = i - 1; sys_write(1, ((b as i64) + i) as *u8, 1) }
54 return 0
55}
56func pf_atoi(s: *u8) -> i64 {
57 var v: i64 = 0
58 var i: i64 = 0
59 var go: i64 = 1
60 while go == 1 {
61 let c: i64 = s[i] as i64
62 if c >= PF_ZERO { if c <= PF_NINE { v = v * PF_B10 + (c - PF_ZERO); i = i + 1 } else { go = 0 } } else { go = 0 }
63 }
64 return v
65}
66func pf_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
67func pf_eq(a: *u8, b: *u8) -> i64 {
68 var i: i64 = 0
69 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
70 if b[i] != (0 as u8) { return 0 }
71 return 1
72}
73// 1 if `nm` ends with the exact literal "manifest.txt". Deliberately EXACT: "manifest-archive.txt"
74// and "manifest.tmp" must NOT match -- the archive is retired history and the tmp is a half-written
75// commit, and counting either as a live plane would inflate the census with things nobody reads.
76func pf_is_manifest(nm: *u8) -> i64 {
77 let ln: i64 = pf_len(nm)
78 let suf: *u8 = "manifest.txt" as *u8
79 let ls: i64 = pf_len(suf)
80 if ln <= ls { return 0 }
81 var i: i64 = 0
82 while i < ls { if nm[ln - ls + i] != suf[i] { return 0 } i = i + 1 }
83 return 1
84}
85// declared row count from q:n without walking the plane -- O(1)-ish, so the sweep can decide
86// whether a plane is safe to fully load BEFORE committing to it. -1 = no q:n (unseeded).
87func pf_declared_rows(prefix: *u8) -> i64 {
88 let pq: *i64 = sys_mmap(PF_OUTCAP) as *i64
89 let lq: *i64 = sys_mmap(PF_OUTCAP) as *i64
90 if ss_get(prefix, "q:n" as *u8, pq, lq) != 1 { return 0 - 1 }
91 return sts_atoi(pq[0] as *u8, lq[0])
92}
93
94// measure ONE plane. returns 1 if it truncates at `cap`, 0 if it fits, -1 if unreadable/skipped.
95func pf_one(prefix: *u8, cap: i64, big: *u8, verbose: i64) -> i64 {
96 let truen: i64 = sts_load(prefix, big, PF_CEIL)
97 if truen <= 0 {
98 if verbose == 1 {
99 pf_w("PLANEFIT plane="); pf_w(prefix)
100 pf_w(" UNREADABLE or EMPTY (sts_load returned "); pf_n(truen); pf_w(")\n")
101 }
102 return 0 - 1
103 }
104 if truen > cap {
105 pf_w(" OVER "); pf_w(prefix)
106 pf_w(" true_bytes="); pf_n(truen)
107 pf_w(" cap="); pf_n(cap)
108 pf_w(" LOSES="); pf_n(truen - cap)
109 pf_w(" bytes -- readers at this cap are ALREADY BLIND, newest rows first\n")
110 if truen >= PF_CEIL {
111 pf_w(" WARNING true_bytes equals the probe ceiling: this is a FLOOR, not a measurement\n")
112 }
113 return 1
114 }
115 if verbose == 1 {
116 pf_w(" OK "); pf_w(prefix)
117 pf_w(" true_bytes="); pf_n(truen)
118 pf_w(" cap="); pf_n(cap)
119 pf_w(" headroom="); pf_n(cap - truen); pf_w("\n")
120 }
121 return 0
122}
123
124func pf_sweep(cap: i64, verbose: i64) -> i64 {
125 let fd: i64 = sys_openat_rd(PF_STORE)
126 if fd < 0 {
127 pf_w("PLANEFIT SWEEP: cannot open "); pf_w(PF_STORE); pf_w(" -- run from the nishihost CWD\n")
128 sys_exit(PF_EXIT_USAGE)
129 return PF_EXIT_USAGE
130 }
131 let dbuf: *u8 = sys_mmap(PF_DIRBUF)
132 let big: *u8 = sys_mmap(PF_CEIL)
133 let path: *u8 = sys_mmap(PF_PATHCAP)
134 var planes: i64 = 0
135 var over: i64 = 0
136 var skipped: i64 = 0
137 var empty: i64 = 0
138 pf_w("=== nx_planefit sweep -- every seg-store plane vs a "); pf_n(cap); pf_w("-byte reader cap ===\n")
139 var go: i64 = 1
140 while go == 1 {
141 let n: i64 = sys_getdents64(fd, dbuf, PF_DIRBUF)
142 if n <= 0 { go = 0 } else {
143 var off: i64 = 0
144 while off < n {
145 let rec: *u8 = ((dbuf as i64) + off) as *u8
146 let rl: i64 = dirent_reclen(rec)
147 if rl <= 0 { off = n } else {
148 let nm: *u8 = dirent_name(rec)
149 if pf_is_manifest(nm) == 1 {
150 // prefix = "knowledge/store/" + basename minus the trailing "manifest.txt"
151 var o: i64 = ss_cat(path, 0, PF_STORE)
152 o = ss_cat(path, o, "/")
153 let ln: i64 = pf_len(nm)
154 let keep: i64 = ln - pf_len("manifest.txt")
155 var k: i64 = 0
156 while k < keep { path[o] = nm[k]; o = o + 1; k = k + 1 }
157 path[o] = 0 as u8
158 planes = planes + 1
159 let rows: i64 = pf_declared_rows(path)
160 if rows < 0 {
161 empty = empty + 1
162 } else {
163 if rows > PF_SWEEP_ROWCAP {
164 skipped = skipped + 1
165 pf_w(" SKIP "); pf_w(path)
166 pf_w(" declared_rows="); pf_n(rows)
167 pf_w(" -- above the declared probe bound "); pf_n(PF_SWEEP_ROWCAP)
168 pf_w("; NOT measured, so this sweep is INCOMPLETE for it\n")
169 } else {
170 if pf_one(path, cap, big, verbose) == 1 { over = over + 1 }
171 }
172 }
173 }
174 off = off + rl
175 }
176 }
177 }
178 }
179 sys_close(fd)
180 pf_w("NX-PLANEFIT-SWEEP planes="); pf_n(planes)
181 pf_w(" over="); pf_n(over)
182 pf_w(" skipped="); pf_n(skipped)
183 pf_w(" unseeded="); pf_n(empty)
184 pf_w(" cap="); pf_n(cap)
185 if over > 0 {
186 pf_w(" verdict=RED (a reader at this cap silently returns a PREFIX as the whole board)\n")
187 sys_exit(PF_EXIT_TRUNC)
188 return PF_EXIT_TRUNC
189 }
190 pf_w(" verdict=GREEN (every measured plane fits)\n")
191 sys_exit(PF_EXIT_FITS)
192 return PF_EXIT_FITS
193}
194
195func main(argc: i64, argv: *i64) -> i64 {
196 if argc < 2 {
197 pf_w("usage: nx_planefit <plane-prefix> [cap] (one plane; exit 1 = truncates)\n")
198 pf_w(" nx_planefit sweep [cap] (EVERY plane; exit 1 = any truncates)\n")
199 pf_w(" nx_planefit sweepv [cap] (sweep, listing the planes that FIT too)\n")
200 sys_exit(PF_EXIT_USAGE)
201 return PF_EXIT_USAGE
202 }
203 let a1: *u8 = argv[1] as *u8
204 var cap: i64 = PF_DEFAULT_CAP
205 if argc >= 3 { cap = pf_atoi(argv[2] as *u8) }
206 if cap <= 0 { cap = PF_DEFAULT_CAP }
207
208 if pf_eq(a1, "sweep") == 1 { return pf_sweep(cap, 0) }
209 if pf_eq(a1, "sweepv") == 1 { return pf_sweep(cap, 1) }
210
211 let big: *u8 = sys_mmap(PF_CEIL)
212 let truen: i64 = sts_load(a1, big, PF_CEIL)
213 if truen <= 0 {
214 pf_w("PLANEFIT plane="); pf_w(a1)
215 pf_w(" UNREADABLE or EMPTY (sts_load returned "); pf_n(truen); pf_w(")\n")
216 sys_exit(PF_EXIT_USAGE)
217 return PF_EXIT_USAGE
218 }
219 let small: *u8 = sys_mmap(cap)
220 let gotn: i64 = sts_load(a1, small, cap)
221 pf_w("PLANEFIT plane="); pf_w(a1)
222 pf_w(" true_bytes="); pf_n(truen)
223 pf_w(" cap="); pf_n(cap)
224 pf_w(" got="); pf_n(gotn)
225 // THE STRUCTURAL ANSWER, MEASURED BESIDE THE CAPPED ONE. sts_load_fit chooses no number: it grows
226 // until the load comes back STRICTLY SHORT, which proves completeness. If fit_bytes equals
227 // true_bytes while got is stuck at the cap, the class is closed rather than postponed.
228 let fl: *i64 = sys_mmap(PF_OUTCAP) as *i64
229 let fbuf: *u8 = sts_load_fit(a1, fl)
230 pf_w(" fit_bytes="); pf_n(fl[0])
231 if (fbuf as i64) == 0 { pf_w(" (sts_load_fit REFUSED)") }
232 if truen >= PF_CEIL {
233 pf_w("\n WARNING true_bytes EQUALS the probe ceiling -- this is a FLOOR, not a measurement. Raise PF_CEIL.\n")
234 }
235 if gotn < truen {
236 pf_w("\n TRUNCATES: a caller with this cap loses "); pf_n(truen - gotn)
237 pf_w(" bytes and CANNOT TELL -- sts_load returns a healthy-looking count.\n")
238 sys_exit(PF_EXIT_TRUNC)
239 return PF_EXIT_TRUNC
240 }
241 pf_w("\n FITS: the whole plane is reachable at this cap.\n")
242 sys_exit(PF_EXIT_FITS)
243 return PF_EXIT_FITS
244}