nx_build_firsttry_lib.nx source
↩ module page · 281 lines · 17444 B
1// nx_build_firsttry_lib.nx -- CE2 (codeeffectiveness ce_build_firsttry): THE BUILD PILLAR'S TWO RATES, READ FROM THE
2// SHIP JOURNAL (knowledge/status/organ_ship.jrnl, rows epoch<TAB>target<TAB>stage<TAB>verdict<TAB>detail).
3// FIRST-TRY: an EPISODE is a target's run of BUILD rows up to its next SHIPPED row (retries inside it are the same
4// episode). An episode is classified by its FIRST BUILD row: BUILT = first_pass, REFUSED-ADMIT = box_refused (the
5// box, not the code -- its own bucket, outside the rate's denominator), anything else = code_fail. The partition
6// first_pass + code_fail + box_refused = episodes is checked and printed.
7// REWORK: a SHIPPED row whose same-target previous SHIPPED lies within the window is a REWORK event; one carrying
8// bytes= smaller than its predecessor's is a REVERT (a byte decrease after an addition); a pair lacking bytes= on
9// either side is REVERT-UNOBSERVABLE, counted, never guessed. Rows before the window feed STATE (a predecessor ship)
10// but never COUNT. A window with no episode reads first_try UNOBSERVABLE and one with no ship reads rework
11// UNOBSERVABLE -- never 0 permil -- and a run with neither writes NO spine row (an unobservable run must not
12// fabricate a point on the trend). One spine row per MEASURED run, path derived from the journal (the production
13// spine iff the journal is the production one, else a <jrnl>.firsttry.spine sidecar), announced fd= wrote= of=.
14// ONE place holds the counts (the r[] slots below): the emitter, the spine and the gate read the same array.
15// license_tier: ORIGINAL No hw writes.
16import "nx_syscalls.nx"
17
18const BFT_DAY_S: i64 = 86400
19const BFT_DEFAULT_WINDOW_DAYS: i64 = 14 // the rung's 14-day build-rework window (GitClear's two-week churn horizon)
20const BFT_MIN_ROW_B: i64 = 16 // 1<TAB>x<TAB>BUILD<TAB>BUILT<TAB>\n -- the shortest well-formed row bounds the row count from the byte count
21const BFT_FIELDS: i64 = 5
22const BFT_WORD: i64 = 8
23const BFT_PATH: i64 = 1024
24const BFT_LINE: i64 = 512
25const BFT_NUMBUF: i64 = 32
26const BFT_MODE644: i64 = 420
27const BFT_PERMIL: i64 = 1000
28const BFT_TAB: i64 = 9
29const BFT_NL: i64 = 10
30const BFT_ZERO: i64 = 48
31const BFT_NINE: i64 = 57
32const BFT_MINUS: i64 = 45
33const BFT_PROD_JRNL: *u8 = "knowledge/status/organ_ship.jrnl"
34const BFT_SPINE_PROD: *u8 = "knowledge/status/build_firsttry.spine"
35const BFT_SPINE_SIDECAR: *u8 = ".firsttry.spine"
36const BFT_BYTES_KEY: *u8 = "bytes="
37const BFT_UNOBSERVABLE: i64 = 0 - 1
38const BFT_VERDICT_MEASURED: i64 = 0
39const BFT_VERDICT_UNOBSERVABLE: i64 = 1
40const BFT_VERDICT_UNREADABLE: i64 = 2
41const BFT_VERDICT_LEAK: i64 = 3
42// per-target state slots
43const BFT_T_OFF: i64 = 0
44const BFT_T_LEN: i64 = 1
45const BFT_T_OPEN: i64 = 2
46const BFT_T_LASTSHIP: i64 = 3
47const BFT_T_LASTBYTES: i64 = 4
48const BFT_T_SLOTS: i64 = 5
49// result slots
50const BFT_R_ROWS: i64 = 0
51const BFT_R_ROWS_IN: i64 = 1
52const BFT_R_EPISODES: i64 = 2
53const BFT_R_FIRST_PASS: i64 = 3
54const BFT_R_CODE_FAIL: i64 = 4
55const BFT_R_BOX_REFUSED: i64 = 5
56const BFT_R_RETRIES: i64 = 6
57const BFT_R_SHIPPED: i64 = 7
58const BFT_R_REWORK: i64 = 8
59const BFT_R_REVERT: i64 = 9
60const BFT_R_NON_REVERT: i64 = 10
61const BFT_R_REVERT_UNOBS: i64 = 11
62const BFT_R_FIRST_PERMIL: i64 = 12
63const BFT_R_REWORK_PERMIL: i64 = 13
64const BFT_R_TARGETS: i64 = 14
65const BFT_R_NEG_CLASS_ABSENT: i64 = 15
66const BFT_R_SLOTS: i64 = 16
67
68func bft_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
69func bft_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } dst[o] = 0 as u8; return o }
70func bft_catn(dst: *u8, off: i64, v: i64) -> i64 {
71 var o: i64 = off
72 var x: i64 = v
73 if x < 0 { dst[o] = BFT_MINUS as u8; o = o + 1; x = 0 - x }
74 let tmp: *u8 = sys_mmap(BFT_NUMBUF)
75 var k: i64 = 0
76 if x == 0 { tmp[0] = BFT_ZERO as u8; k = 1 }
77 while x > 0 { tmp[k] = (BFT_ZERO + x % 10) as u8; k = k + 1; x = x / 10 }
78 while k > 0 { k = k - 1; dst[o] = tmp[k]; o = o + 1 }
79 dst[o] = 0 as u8
80 return o
81}
82func bft_puts(s: *u8) -> i64 { return sys_write(1, s, bft_slen(s)) }
83func bft_num(v: i64) -> i64 { let b: *u8 = sys_mmap(BFT_NUMBUF); let o: i64 = bft_catn(b, 0, v); return sys_write(1, b, o) }
84func bft_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] == b[i] { if a[i] == (0 as u8) { return 1 } i = i + 1 } return 0 }
85// a decimal parsed from an unsigned digit run; -1 when the run holds no digit
86func bft_atoi(s: *u8) -> i64 {
87 var v: i64 = 0
88 var any: i64 = 0
89 var i: i64 = 0
90 var go: i64 = 1
91 while go == 1 { let c: i64 = s[i] as i64; if c < BFT_ZERO { go = 0 } else { if c > BFT_NINE { go = 0 } else { v = v * 10 + (c - BFT_ZERO); any = 1; i = i + 1 } } }
92 if any == 0 { return 0 - 1 }
93 return v
94}
95// span helpers: a field is (off, len) into the journal buffer
96func bft_span_eq(buf: *u8, off: i64, len: i64, s: *u8) -> i64 {
97 if bft_slen(s) != len { return 0 }
98 var i: i64 = 0
99 while i < len { if buf[off + i] != s[i] { return 0 } i = i + 1 }
100 return 1
101}
102func bft_span_starts(buf: *u8, off: i64, len: i64, s: *u8) -> i64 {
103 let m: i64 = bft_slen(s)
104 if m > len { return 0 }
105 var i: i64 = 0
106 while i < m { if buf[off + i] != s[i] { return 0 } i = i + 1 }
107 return 1
108}
109func bft_span_eq_span(buf: *u8, aoff: i64, alen: i64, boff: i64, blen: i64) -> i64 {
110 if alen != blen { return 0 }
111 var i: i64 = 0
112 while i < alen { if buf[aoff + i] != buf[boff + i] { return 0 } i = i + 1 }
113 return 1
114}
115func bft_span_atoi(buf: *u8, off: i64, len: i64) -> i64 {
116 var v: i64 = 0
117 var any: i64 = 0
118 var i: i64 = 0
119 var go: i64 = 1
120 while go == 1 { if i >= len { go = 0 } else { let c: i64 = buf[off + i] as i64; if c < BFT_ZERO { go = 0 } else { if c > BFT_NINE { go = 0 } else { v = v * 10 + (c - BFT_ZERO); any = 1; i = i + 1 } } } }
121 if any == 0 { return 0 - 1 }
122 return v
123}
124func bft_span_find(buf: *u8, off: i64, len: i64, needle: *u8) -> i64 {
125 let m: i64 = bft_slen(needle)
126 if m == 0 { return 0 - 1 }
127 var i: i64 = 0
128 while i + m <= len { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[off + i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return off + i } i = i + 1 }
129 return 0 - 1
130}
131
132// THE RULER. buf/n = the journal bytes; now/window_days = the window; r = BFT_R_SLOTS result slots (zeroed here).
133func bft_compute(buf: *u8, n: i64, now: i64, window_days: i64, r: *i64) -> i64 {
134 var z: i64 = 0
135 while z < BFT_R_SLOTS { r[z] = 0; z = z + 1 }
136 let maxrows: i64 = n / BFT_MIN_ROW_B + 1
137 let tt: *i64 = sys_mmap(maxrows * BFT_T_SLOTS * BFT_WORD) as *i64
138 var nt: i64 = 0
139 let wlen: i64 = window_days * BFT_DAY_S
140 let wstart: i64 = now - wlen
141 let f: *i64 = sys_mmap(BFT_FIELDS * 2 * BFT_WORD) as *i64
142 let keylen: i64 = bft_slen(BFT_BYTES_KEY)
143 var i: i64 = 0
144 while i < n {
145 var e: i64 = i
146 var go: i64 = 1
147 while go == 1 { if e >= n { go = 0 } else { if buf[e] == (BFT_NL as u8) { go = 0 } else { e = e + 1 } } }
148 var fi: i64 = 0
149 while fi < BFT_FIELDS { f[fi * 2] = i; f[fi * 2 + 1] = 0; fi = fi + 1 }
150 fi = 0
151 var s: i64 = i
152 var p: i64 = i
153 while p <= e {
154 var is_end: i64 = 0
155 if p == e { is_end = 1 } else { if buf[p] == (BFT_TAB as u8) { if fi < BFT_FIELDS - 1 { is_end = 1 } } }
156 if is_end == 1 { if fi < BFT_FIELDS { f[fi * 2] = s; f[fi * 2 + 1] = p - s; fi = fi + 1 } s = p + 1 }
157 p = p + 1
158 }
159 if fi >= 4 {
160 let epoch: i64 = bft_span_atoi(buf, f[0], f[1])
161 if epoch > 0 {
162 r[BFT_R_ROWS] = r[BFT_R_ROWS] + 1
163 var inwin: i64 = 0
164 if epoch >= wstart { inwin = 1; r[BFT_R_ROWS_IN] = r[BFT_R_ROWS_IN] + 1 }
165 var t: i64 = 0 - 1
166 var k: i64 = 0
167 while k < nt { if t < 0 { if bft_span_eq_span(buf, f[2], f[3], tt[k * BFT_T_SLOTS + BFT_T_OFF], tt[k * BFT_T_SLOTS + BFT_T_LEN]) == 1 { t = k } } k = k + 1 }
168 if t < 0 { t = nt; nt = nt + 1; tt[t * BFT_T_SLOTS + BFT_T_OFF] = f[2]; tt[t * BFT_T_SLOTS + BFT_T_LEN] = f[3]; tt[t * BFT_T_SLOTS + BFT_T_OPEN] = 0; tt[t * BFT_T_SLOTS + BFT_T_LASTSHIP] = 0; tt[t * BFT_T_SLOTS + BFT_T_LASTBYTES] = 0 }
169 let base: i64 = t * BFT_T_SLOTS
170 if bft_span_eq(buf, f[4], f[5], "BUILD" as *u8) == 1 {
171 if tt[base + BFT_T_OPEN] == 0 {
172 tt[base + BFT_T_OPEN] = 1
173 if inwin == 1 {
174 r[BFT_R_EPISODES] = r[BFT_R_EPISODES] + 1
175 if bft_span_eq(buf, f[6], f[7], "BUILT" as *u8) == 1 { r[BFT_R_FIRST_PASS] = r[BFT_R_FIRST_PASS] + 1 } else {
176 if bft_span_starts(buf, f[6], f[7], "REFUSED-ADMIT" as *u8) == 1 { r[BFT_R_BOX_REFUSED] = r[BFT_R_BOX_REFUSED] + 1 } else { r[BFT_R_CODE_FAIL] = r[BFT_R_CODE_FAIL] + 1 }
177 }
178 }
179 } else { if inwin == 1 { r[BFT_R_RETRIES] = r[BFT_R_RETRIES] + 1 } }
180 }
181 if bft_span_eq(buf, f[4], f[5], "SHIPPED" as *u8) == 1 {
182 tt[base + BFT_T_OPEN] = 0
183 var b: i64 = 0 - 1
184 if fi >= 5 { let at: i64 = bft_span_find(buf, f[8], f[9], BFT_BYTES_KEY); if at >= 0 { b = bft_span_atoi(buf, at + keylen, f[8] + f[9] - (at + keylen)) } }
185 let pe: i64 = tt[base + BFT_T_LASTSHIP]
186 let pb: i64 = tt[base + BFT_T_LASTBYTES]
187 if inwin == 1 {
188 r[BFT_R_SHIPPED] = r[BFT_R_SHIPPED] + 1
189 if pe > 0 { if epoch - pe <= wlen {
190 r[BFT_R_REWORK] = r[BFT_R_REWORK] + 1
191 if b > 0 { if pb > 0 { if b < pb { r[BFT_R_REVERT] = r[BFT_R_REVERT] + 1 } else { r[BFT_R_NON_REVERT] = r[BFT_R_NON_REVERT] + 1 } } else { r[BFT_R_REVERT_UNOBS] = r[BFT_R_REVERT_UNOBS] + 1 } } else { r[BFT_R_REVERT_UNOBS] = r[BFT_R_REVERT_UNOBS] + 1 }
192 } }
193 }
194 tt[base + BFT_T_LASTSHIP] = epoch
195 if b > 0 { tt[base + BFT_T_LASTBYTES] = b } else { tt[base + BFT_T_LASTBYTES] = 0 }
196 }
197 }
198 }
199 i = e + 1
200 }
201 r[BFT_R_TARGETS] = nt
202 let den: i64 = r[BFT_R_FIRST_PASS] + r[BFT_R_CODE_FAIL]
203 if den > 0 { r[BFT_R_FIRST_PERMIL] = r[BFT_R_FIRST_PASS] * BFT_PERMIL / den } else { r[BFT_R_FIRST_PERMIL] = BFT_UNOBSERVABLE }
204 // A window with retries but NOT ONE recorded failure is a journal that never carried the negative class (the loop
205 // journaled BUILT only before 2026-09-06): a 1000-permil rate read from it is a bare percentage, not a measurement.
206 // MEASURED on the first production beat: 253 episodes, 119 retries, 0 failures, first_try 1000 -- abstain by name.
207 if r[BFT_R_CODE_FAIL] + r[BFT_R_BOX_REFUSED] == 0 { if r[BFT_R_RETRIES] > 0 { r[BFT_R_FIRST_PERMIL] = BFT_UNOBSERVABLE; r[BFT_R_NEG_CLASS_ABSENT] = 1 } }
208 if r[BFT_R_SHIPPED] > 0 { r[BFT_R_REWORK_PERMIL] = r[BFT_R_REWORK] * BFT_PERMIL / r[BFT_R_SHIPPED] } else { r[BFT_R_REWORK_PERMIL] = BFT_UNOBSERVABLE }
209 if r[BFT_R_FIRST_PASS] + r[BFT_R_CODE_FAIL] + r[BFT_R_BOX_REFUSED] != r[BFT_R_EPISODES] { return BFT_VERDICT_LEAK }
210 if r[BFT_R_REVERT] + r[BFT_R_NON_REVERT] + r[BFT_R_REVERT_UNOBS] != r[BFT_R_REWORK] { return BFT_VERDICT_LEAK }
211 if den == 0 { if r[BFT_R_SHIPPED] == 0 { return BFT_VERDICT_UNOBSERVABLE } }
212 return BFT_VERDICT_MEASURED
213}
214
215// read + compute, no output: what a gate calls
216func bft_measure(jrnl: *u8, window_days: i64, now: i64, r: *i64) -> i64 {
217 let ln: *i64 = sys_mmap(16) as *i64
218 let buf: *u8 = sys_read_file(jrnl, ln)
219 if (buf as i64) == 0 { var z: i64 = 0; while z < BFT_R_SLOTS { r[z] = 0; z = z + 1 } return BFT_VERDICT_UNREADABLE }
220 return bft_compute(buf, ln[0], now, window_days, r)
221}
222func bft_spine_path(jrnl: *u8, out: *u8) -> i64 {
223 if bft_streq(jrnl, BFT_PROD_JRNL) == 1 { return bft_cat(out, 0, BFT_SPINE_PROD) }
224 let o: i64 = bft_cat(out, 0, jrnl)
225 return bft_cat(out, o, BFT_SPINE_SIDECAR)
226}
227func bft_verdict_name(code: i64) -> *u8 {
228 if code == BFT_VERDICT_MEASURED { return "MEASURED" as *u8 }
229 if code == BFT_VERDICT_UNOBSERVABLE { return "UNOBSERVABLE" as *u8 }
230 if code == BFT_VERDICT_UNREADABLE { return "UNREADABLE" as *u8 }
231 return "LEAK" as *u8
232}
233func bft_kv(k: *u8, v: i64) -> i64 { bft_puts(k); bft_num(v); return 0 }
234func bft_state_name(permil: i64) -> *u8 { if permil == BFT_UNOBSERVABLE { return "UNOBSERVABLE" as *u8 } return "MEASURED" as *u8 }
235// one spine row per MEASURED run; the fd and byte count are announced so an absent row is one number, not a hunt
236func bft_spine_append(jrnl: *u8, now: i64, window_days: i64, r: *i64) -> i64 {
237 let path: *u8 = sys_mmap(BFT_PATH)
238 bft_spine_path(jrnl, path)
239 let row: *u8 = sys_mmap(BFT_LINE)
240 var o: i64 = bft_catn(row, 0, now)
241 o = bft_cat(row, o, "|window_days=" as *u8); o = bft_catn(row, o, window_days)
242 o = bft_cat(row, o, "|episodes=" as *u8); o = bft_catn(row, o, r[BFT_R_EPISODES])
243 o = bft_cat(row, o, "|first_pass=" as *u8); o = bft_catn(row, o, r[BFT_R_FIRST_PASS])
244 o = bft_cat(row, o, "|code_fail=" as *u8); o = bft_catn(row, o, r[BFT_R_CODE_FAIL])
245 o = bft_cat(row, o, "|box_refused=" as *u8); o = bft_catn(row, o, r[BFT_R_BOX_REFUSED])
246 o = bft_cat(row, o, "|first_try_permil=" as *u8); o = bft_catn(row, o, r[BFT_R_FIRST_PERMIL])
247 o = bft_cat(row, o, "|shipped=" as *u8); o = bft_catn(row, o, r[BFT_R_SHIPPED])
248 o = bft_cat(row, o, "|rework=" as *u8); o = bft_catn(row, o, r[BFT_R_REWORK])
249 o = bft_cat(row, o, "|rework_permil=" as *u8); o = bft_catn(row, o, r[BFT_R_REWORK_PERMIL])
250 o = bft_cat(row, o, "|reverts=" as *u8); o = bft_catn(row, o, r[BFT_R_REVERT])
251 o = bft_cat(row, o, "|revert_unobservable=" as *u8); o = bft_catn(row, o, r[BFT_R_REVERT_UNOBS])
252 o = bft_cat(row, o, "|neg_class_absent=" as *u8); o = bft_catn(row, o, r[BFT_R_NEG_CLASS_ABSENT])
253 row[o] = BFT_NL as u8; o = o + 1; row[o] = 0 as u8
254 let fd: i64 = sys_openat_append(path, BFT_MODE644)
255 var wrote: i64 = 0 - 1
256 if fd >= 0 { wrote = sys_write(fd, row, o); sys_close(fd) }
257 bft_puts("# FIRSTTRY spine=" as *u8); bft_puts(path); bft_kv(" fd=" as *u8, fd); bft_kv(" wrote=" as *u8, wrote); bft_kv(" of=" as *u8, o); bft_puts("\n" as *u8)
258 if wrote == o { return 0 }
259 return 0 - 1
260}
261// THE REPORT: measure, print the anchored lines, append the spine on a MEASURED run, verdict LAST. Returns the code.
262func bft_report(jrnl: *u8, window_days: i64, now: i64) -> i64 {
263 let r: *i64 = sys_mmap(BFT_R_SLOTS * BFT_WORD) as *i64
264 let code: i64 = bft_measure(jrnl, window_days, now, r)
265 bft_puts("# FIRSTTRY jrnl=" as *u8); bft_puts(jrnl); bft_kv(" window_days=" as *u8, window_days); bft_kv(" now=" as *u8, now)
266 bft_kv(" rows=" as *u8, r[BFT_R_ROWS]); bft_kv(" rows_in_window=" as *u8, r[BFT_R_ROWS_IN]); bft_kv(" targets=" as *u8, r[BFT_R_TARGETS]); bft_puts("\n" as *u8)
267 if code == BFT_VERDICT_UNREADABLE { bft_puts("# FIRSTTRY journal UNREADABLE -- nothing measured, nothing written\n" as *u8) } else {
268 bft_puts("# FIRSTTRY" as *u8); bft_kv(" episodes=" as *u8, r[BFT_R_EPISODES]); bft_kv(" first_pass=" as *u8, r[BFT_R_FIRST_PASS]); bft_kv(" code_fail=" as *u8, r[BFT_R_CODE_FAIL]); bft_kv(" box_refused=" as *u8, r[BFT_R_BOX_REFUSED])
269 bft_puts(" partition=" as *u8); if r[BFT_R_FIRST_PASS] + r[BFT_R_CODE_FAIL] + r[BFT_R_BOX_REFUSED] == r[BFT_R_EPISODES] { bft_puts("RECONCILES" as *u8) } else { bft_puts("LEAK" as *u8) }
270 bft_kv(" retries=" as *u8, r[BFT_R_RETRIES]); bft_puts("\n" as *u8)
271 bft_puts("# FIRSTTRY" as *u8); bft_kv(" first_try_permil=" as *u8, r[BFT_R_FIRST_PERMIL]); bft_puts(" first_try_state=" as *u8); bft_puts(bft_state_name(r[BFT_R_FIRST_PERMIL]))
272 bft_kv(" denominator=" as *u8, r[BFT_R_FIRST_PASS] + r[BFT_R_CODE_FAIL]); bft_kv(" neg_class_absent=" as *u8, r[BFT_R_NEG_CLASS_ABSENT]); bft_puts(" (box refusals excluded: the box, not the code; neg_class_absent=1 means retries without one recorded failure, a journal that lacks the negative class)\n" as *u8)
273 bft_puts("# FIRSTTRY" as *u8); bft_kv(" shipped=" as *u8, r[BFT_R_SHIPPED]); bft_kv(" rework=" as *u8, r[BFT_R_REWORK]); bft_kv(" rework_permil=" as *u8, r[BFT_R_REWORK_PERMIL]); bft_puts(" rework_state=" as *u8); bft_puts(bft_state_name(r[BFT_R_REWORK_PERMIL])); bft_puts("\n" as *u8)
274 bft_puts("# FIRSTTRY" as *u8); bft_kv(" reverts=" as *u8, r[BFT_R_REVERT]); bft_kv(" non_revert=" as *u8, r[BFT_R_NON_REVERT]); bft_kv(" revert_unobservable=" as *u8, r[BFT_R_REVERT_UNOBS])
275 bft_puts(" partition=" as *u8); if r[BFT_R_REVERT] + r[BFT_R_NON_REVERT] + r[BFT_R_REVERT_UNOBS] == r[BFT_R_REWORK] { bft_puts("RECONCILES" as *u8) } else { bft_puts("LEAK" as *u8) }
276 bft_puts(" (of rework pairs; a pair without bytes= on both sides is counted, never guessed)\n" as *u8)
277 if code == BFT_VERDICT_MEASURED { bft_spine_append(jrnl, now, window_days, r) } else { bft_puts("# FIRSTTRY spine=NONE (an unobservable run writes no trend point)\n" as *u8) }
278 }
279 bft_puts("NX-FIRSTTRY verdict=" as *u8); bft_puts(bft_verdict_name(code)); bft_puts("\n" as *u8)
280 return code
281}