code wiki / _hdl_build / nx_skullfit_beat.nx
nx_skullfit_beat.nx source
↩ module page · 424 lines · 22726 B
1// nx_skullfit_beat.nx -- no-arg BEAT: the skull generator's RECURSIVE LEARNING cycle (operator
2// 2026-08-09: "i want nishi ecosystem to gain these functionalities as a recursive learning setup so
3// that it can keep growing and get its capabilities to sota"). Each run advances the whole loop with no
4// seat attached: (1) `nx_skullsdf tune` refines the 76-parameter fit AGAINST THE SCANNED ORACLE,
5// seeded FROM knowledge/skull_fit.dat and saving BACK to it -- successive beats converge instead of
6// restarting; (2) an emit must report fit_src=1 -- the ARTIFACT proves the fit landed, never the
7// tuner's receipt (a builtin-defaults emit means the loop is broken, not neutral); (3) `gapmap`
8// re-measures the per-region gap so the trend line carries WHERE the skull still deviates, not just a
9// scalar; (4) THE CANON GATE measures the FACE, landmark by landmark against the oracle (anatomy AN17:
10// the gapmap's regions are dominated by the vault and were blind to a 49 mm muzzle); (5) ONE line
11// appends to the trend journal. BEAT-FAIL lines are evidence too.
12//
13// ★★★★★★THE WORK PER RUN IS DERIVED FROM THE DISPATCH BOUND, NEVER TYPED (AN17, 2026-09-18). The clock
14// kills any dispatch at knowledge/skullfit_beat.conf dispatch_deadline_ms (the scheduler's own constant,
15// carried as data with its provenance). MEASURED: one tune pass = 477 s on the NAS, so the old three-pass
16// run was killed at 901 s on every dispatch since 2026-08-14 and appended NOTHING -- a beat whose one run
17// cannot fit the deadline is indistinguishable from a beat that never fires. Now every run reads its own
18// previous timings from knowledge/status/skullfit_beat.timing (the MAXIMUM seen per step, so a slow box
19// only makes the plan more conservative), plans passes = floor((bound - emit - gapmap - canon) / pass),
20// at least one, and when even one pass plus the measurements cannot fit it ALTERNATES phases (tune-only,
21// then measure-only) so both still happen. A run stamps `started` before the work and `finished` after:
22// a run the clock killed leaves started without finished, and the NEXT run names it on its trend row
23// (prev_killed=1) and halves its plan -- a named refusal, never silence. The coordinate descent's step
24// ladder (4, 2, 1) is carried ACROSS runs in the same file: a pass that moves the objective keeps its
25// step, a pass that does not halves it.
26// ***VACUOUS-OBJECTIVE GUARD (nx_fitloop's law): final_centi==0 against a real scanned skull is a
27// BROKEN RULER, never perfection -- scored INVALID, exit 2.
28// Scheduler contract: the clockjobs- plane fork+execs this elf with NO args.
29// trend: knowledge/status/skullfit_trend.jsonl line: <epoch>\tTUNE ...\tFIT ...\tGAP ...\tCANON ...\tPLAN ...\n
30// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
31import "nx_sovjson_lib.nx"
32import "nx_syscalls.nx"
33import "nx_tool_run.nx"
34
35const SB_OUT: i64 = 65536
36const SB_LINE: i64 = 16384
37const SB_MODE: i64 = 420
38const SB_STEP0: i64 = 4 // the coordinate descent's first step, mm (nx_skullsdf tune's own default ladder 4,2,1)
39const SB_STRIDE: i64 = 15 // every 15th oracle triangle, the sampling the trend was built on
40const SB_ELF_SDF: *u8 = "/volume1/homes/elderwesto/nishihost/nx_skullsdf.elf"
41const SB_ELF_CANON: *u8 = "/volume1/homes/elderwesto/nishihost/nx_skull_canon_gate.elf"
42const SB_CONF: *u8 = "knowledge/skullfit_beat.conf"
43const SB_TIMING: *u8 = "knowledge/status/skullfit_beat.timing"
44const SB_TREND: *u8 = "knowledge/status/skullfit_trend.jsonl"
45const SB_FIT: *u8 = "knowledge/skull_fit.dat"
46const SB_ORACLE: *u8 = "knowledge/skull.nxmesh"
47const SB_EMIT: *u8 = "/tmp/skullfit_beat.nxmesh"
48const SB_HEAT_OURS: *u8 = "/tmp/skullfit_heat_ours.nxmesh"
49const SB_HEAT_ORACLE: *u8 = "/tmp/skullfit_heat_oracle.nxmesh"
50const SB_RESIDUAL: *u8 = "knowledge/skull_res.dat"
51const SB_FITBANK: *u8 = "knowledge/status/bank/skull_fit.dat.prebeat" // the pre-tune vector: the ruler's rollback target
52const SB_PHASE_BOTH: i64 = 0
53const SB_PHASE_MEASURE: i64 = 1
54
55func sb_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
56func sb_append(path: *u8, buf: *u8, n: i64) -> i64 {
57 let fd: i64 = sys_openat_append(path, SB_MODE)
58 if fd < 0 { return 0 - 1 }
59 sys_write(fd, buf, n)
60 sys_fsync(fd)
61 sys_close(fd)
62 return 0
63}
64// truncate-write a whole file (the timing stamp: one writer, rewritten every run, never appended)
65func sb_write(path: *u8, buf: *u8, n: i64) -> i64 {
66 let fd: i64 = sys_openat_wr(path, SB_MODE)
67 if fd < 0 { return 0 - 1 }
68 sys_write(fd, buf, n)
69 sys_fsync(fd)
70 sys_close(fd)
71 return 0
72}
73// anchored integer field: find the FULL needle (e.g. "\x22final_centi\x22:" or "pass_ms_max|") and parse the
74// integer after it; -1 when absent. Anchored on the field name (parser law: never a bare regex over prose),
75// flag-exited loops (the cursor-sentinel law: a loop that clobbers its cursor to break cannot report where it stopped).
76func sb_field(out: *u8, n: i64, needle: *u8) -> i64 {
77 var nl: i64 = 0
78 while needle[nl] != (0 as u8) { nl = nl + 1 }
79 var i: i64 = 0
80 var res: i64 = 0 - 1
81 var done: i64 = 0
82 while done == 0 {
83 if i + nl > n { done = 1 } else {
84 var j: i64 = 0
85 var hit: i64 = 1
86 var go: i64 = 1
87 while go == 1 {
88 if j >= nl { go = 0 } else {
89 if out[i+j] != needle[j] { hit = 0; go = 0 } else { j = j + 1 }
90 }
91 }
92 if hit == 1 {
93 var p: i64 = i + nl
94 var neg: i64 = 0
95 if p < n { if out[p] == (45 as u8) { neg = 1; p = p + 1 } }
96 var v: i64 = 0
97 var any: i64 = 0
98 var run: i64 = 1
99 while run == 1 {
100 var c: i64 = 0
101 if p < n { c = out[p] as i64 }
102 var isd: i64 = 0
103 if c >= 48 { if c <= 57 { isd = 1 } }
104 if isd == 1 { v = v*10 + (c-48); any = 1; p = p + 1 } else { run = 0 }
105 }
106 if any == 1 {
107 if neg == 1 { res = 0 - v } else { res = v }
108 }
109 done = 1
110 } else {
111 i = i + 1
112 }
113 }
114 }
115 return res
116}
117// copy the rest of the line that starts with `needle` (the needle included) into ln at o; returns the new o
118func sb_copy_line(out: *u8, n: i64, needle: *u8, ln: *u8, o0: i64, cap: i64) -> i64 {
119 var nl: i64 = 0
120 while needle[nl] != (0 as u8) { nl = nl + 1 }
121 var i: i64 = 0
122 var start: i64 = 0 - 1
123 var done: i64 = 0
124 while done == 0 {
125 if i + nl > n { done = 1 } else {
126 var j: i64 = 0
127 var hit: i64 = 1
128 var go: i64 = 1
129 while go == 1 {
130 if j >= nl { go = 0 } else {
131 if out[i+j] != needle[j] { hit = 0; go = 0 } else { j = j + 1 }
132 }
133 }
134 if hit == 1 { start = i; done = 1 } else { i = i + 1 }
135 }
136 }
137 var o: i64 = o0
138 if start < 0 { return o }
139 var c: i64 = start
140 var copying: i64 = 1
141 while copying == 1 {
142 if c >= n { copying = 0 } else {
143 if out[c] == (10 as u8) { copying = 0 } else {
144 if o < cap - 2 { ln[o] = out[c]; o = o + 1 }
145 c = c + 1
146 }
147 }
148 }
149 return o
150}
151func sb_read(path: *u8, len: *i64) -> *u8 {
152 len[0] = 0
153 let b: *u8 = sys_read_file(path, len)
154 if (b as i64) == 0 { len[0] = 0 }
155 return b
156}
157func sb_max(a: i64, b: i64) -> i64 { if a > b { return a } return b }
158func sb_ms() -> i64 { return sys_now_ms() }
159// the timing stamp: every measured maximum, the plan, the step ladder, and the started/finished pair
160func sb_stamp(started: i64, finished: i64, planned: i64, phase: i64, step: i64, pass_ms: i64, emit_ms: i64, gapmap_ms: i64, canon_ms: i64, inband: i64) -> i64 {
161 let b: *u8 = sys_mmap(SB_LINE)
162 var o: i64 = 0
163 o = sj_cat(b, o, "inband|" as *u8); o = sj_catn(b, o, inband); b[o] = 10 as u8; o = o + 1
164 o = sj_cat(b, o, "started|" as *u8); o = sj_catn(b, o, started); b[o] = 10 as u8; o = o + 1
165 o = sj_cat(b, o, "finished|" as *u8); o = sj_catn(b, o, finished); b[o] = 10 as u8; o = o + 1
166 o = sj_cat(b, o, "planned|" as *u8); o = sj_catn(b, o, planned); b[o] = 10 as u8; o = o + 1
167 o = sj_cat(b, o, "phase|" as *u8); o = sj_catn(b, o, phase); b[o] = 10 as u8; o = o + 1
168 o = sj_cat(b, o, "step|" as *u8); o = sj_catn(b, o, step); b[o] = 10 as u8; o = o + 1
169 o = sj_cat(b, o, "pass_ms_max|" as *u8); o = sj_catn(b, o, pass_ms); b[o] = 10 as u8; o = o + 1
170 o = sj_cat(b, o, "emit_ms_max|" as *u8); o = sj_catn(b, o, emit_ms); b[o] = 10 as u8; o = o + 1
171 o = sj_cat(b, o, "gapmap_ms_max|" as *u8); o = sj_catn(b, o, gapmap_ms); b[o] = 10 as u8; o = o + 1
172 o = sj_cat(b, o, "canon_ms_max|" as *u8); o = sj_catn(b, o, canon_ms); b[o] = 10 as u8; o = o + 1
173 return sb_write(SB_TIMING, b, o)
174}
175func main(argc: i64, argv: *i64) -> i64 {
176 // ---- the bound, as data with provenance; absent = a named refusal on the trend spine ----
177 let cl: *i64 = sys_mmap(16) as *i64
178 let cb: *u8 = sb_read(SB_CONF, cl)
179 let budget: i64 = sb_field(cb, cl[0], "dispatch_deadline_ms|" as *u8)
180 let now0: i64 = sys_now_realtime_sec()
181 let ln: *u8 = sys_mmap(SB_LINE)
182 if budget <= 0 {
183 var o0: i64 = sj_catn(ln, 0, now0)
184 ln[o0] = 9 as u8; o0 = o0 + 1
185 o0 = sj_cat(ln, o0, "BEAT-FAIL conf-missing dispatch_deadline_ms in " as *u8)
186 o0 = sj_cat(ln, o0, SB_CONF)
187 ln[o0] = 10 as u8; o0 = o0 + 1
188 sb_append(SB_TREND, ln, o0)
189 sb_puts("{\x22organ\x22:\x22nx_skullfit_beat\x22,\x22verdict\x22:\x22RED\x22,\x22why\x22:\x22conf-missing\x22}\n" as *u8)
190 return 1
191 }
192 // ---- the previous run's timings and fate ----
193 let tl: *i64 = sys_mmap(16) as *i64
194 let tb: *u8 = sb_read(SB_TIMING, tl)
195 var pass_ms: i64 = sb_max(sb_field(tb, tl[0], "pass_ms_max|" as *u8), 0)
196 var emit_ms: i64 = sb_max(sb_field(tb, tl[0], "emit_ms_max|" as *u8), 0)
197 var gapmap_ms: i64 = sb_max(sb_field(tb, tl[0], "gapmap_ms_max|" as *u8), 0)
198 var canon_ms: i64 = sb_max(sb_field(tb, tl[0], "canon_ms_max|" as *u8), 0)
199 let prev_started: i64 = sb_field(tb, tl[0], "started|" as *u8)
200 let prev_finished: i64 = sb_field(tb, tl[0], "finished|" as *u8)
201 let prev_planned: i64 = sb_field(tb, tl[0], "planned|" as *u8)
202 var phase: i64 = sb_max(sb_field(tb, tl[0], "phase|" as *u8), 0)
203 var step: i64 = sb_field(tb, tl[0], "step|" as *u8)
204 if step < 1 { step = SB_STEP0 }
205 let prev_inb: i64 = sb_field(tb, tl[0], "inband|" as *u8) // the ruler's in-band count the last accepted vector earned; -1 = never measured
206 var prev_killed: i64 = 0
207 if prev_started > 0 { if prev_finished == 0 { prev_killed = 1 } }
208 // ---- the plan: passes from the bound and the measured maxima; a kill halves; unmeasured = one pass ----
209 let fixed: i64 = emit_ms + gapmap_ms + canon_ms
210 var passes: i64 = 1
211 var do_tune: i64 = 1
212 var do_measure: i64 = 1
213 if pass_ms > 0 {
214 passes = (budget - fixed) / pass_ms
215 if passes < 1 {
216 // one pass and the measurements cannot share a dispatch: alternate
217 passes = 1
218 if phase == SB_PHASE_BOTH { do_measure = 0; phase = SB_PHASE_MEASURE } else { do_tune = 0; phase = SB_PHASE_BOTH }
219 }
220 }
221 if prev_killed == 1 { if prev_planned > 1 { passes = prev_planned / 2 } else { if do_tune * do_measure == 1 { do_measure = 0; phase = SB_PHASE_MEASURE } } }
222 if passes < 1 { passes = 1 }
223 sb_stamp(now0, 0, passes, phase, step, pass_ms, emit_ms, gapmap_ms, canon_ms, prev_inb)
224 sb_puts("{\x22organ\x22:\x22nx_skullfit_beat\x22,\x22plan\x22:{\x22budget_ms\x22:" as *u8)
225 let nb: *u8 = sys_mmap(64)
226 var no_: i64 = sj_catn(nb, 0, budget); sys_write(1, nb, no_)
227 sb_puts(",\x22fixed_ms\x22:" as *u8); no_ = sj_catn(nb, 0, fixed); sys_write(1, nb, no_)
228 sb_puts(",\x22pass_ms_max\x22:" as *u8); no_ = sj_catn(nb, 0, pass_ms); sys_write(1, nb, no_)
229 sb_puts(",\x22passes\x22:" as *u8); no_ = sj_catn(nb, 0, passes); sys_write(1, nb, no_)
230 sb_puts(",\x22step\x22:" as *u8); no_ = sj_catn(nb, 0, step); sys_write(1, nb, no_)
231 sb_puts(",\x22tune\x22:" as *u8); no_ = sj_catn(nb, 0, do_tune); sys_write(1, nb, no_)
232 sb_puts(",\x22measure\x22:" as *u8); no_ = sj_catn(nb, 0, do_measure); sys_write(1, nb, no_)
233 sb_puts(",\x22prev_killed\x22:" as *u8); no_ = sj_catn(nb, 0, prev_killed); sys_write(1, nb, no_)
234 sb_puts("}}\n" as *u8)
235 // ---- 1: tune, seeded from and saving to the fit file; the step carried across runs ----
236 var rc1: i64 = 0
237 var start: i64 = 0 - 1
238 var fin: i64 = 0 - 1
239 var tune_ms: i64 = 0
240 if do_tune == 1 {
241 // bank the pre-tune vector first: the ruler below may send this pass back (a pass that trades the face for the vault)
242 let bl: *i64 = sys_mmap(16) as *i64
243 let bb: *u8 = sb_read(SB_FIT, bl)
244 if bl[0] > 0 { sb_write(SB_FITBANK, bb, bl[0]) }
245 let av1: *i64 = sys_mmap(96) as *i64
246 let sp: *u8 = sys_mmap(32)
247 sj_catn(sp, 0, passes)
248 let ss: *u8 = sys_mmap(32)
249 sj_catn(ss, 0, step)
250 let st: *u8 = sys_mmap(32)
251 sj_catn(st, 0, SB_STRIDE)
252 av1[0] = SB_ELF_SDF as i64
253 av1[1] = ("tune" as *u8) as i64
254 av1[2] = SB_ORACLE as i64
255 av1[3] = ("0" as *u8) as i64
256 av1[4] = ("500" as *u8) as i64
257 av1[5] = st as i64
258 av1[6] = sp as i64
259 av1[7] = ss as i64
260 av1[8] = SB_FIT as i64
261 av1[9] = 0
262 let out1: *u8 = sys_mmap(SB_OUT)
263 let ol1: *i64 = sys_mmap(16) as *i64
264 let t1: i64 = sb_ms()
265 rc1 = tr_run_capture(SB_ELF_SDF, av1, out1, SB_OUT, ol1)
266 tune_ms = sb_ms() - t1
267 start = sb_field(out1, ol1[0], "\x22start_centi\x22:" as *u8)
268 fin = sb_field(out1, ol1[0], "\x22final_centi\x22:" as *u8)
269 pass_ms = sb_max(pass_ms, tune_ms / passes)
270 // the ladder: no movement at this step means the step is spent; halve it for the next run (floor 1)
271 if fin >= 0 { if start >= 0 { if fin >= start { if step > 1 { step = step / 2 } } } }
272 }
273 // ---- 2: emit; the ARTIFACT proves the fit loaded (fit_src=1), never the tuner's receipt ----
274 var rc2: i64 = 0
275 var fsrc: i64 = 0 - 1
276 var rc3: i64 = 0
277 var rc4: i64 = 0
278 let out3: *u8 = sys_mmap(SB_OUT)
279 let ol3: *i64 = sys_mmap(16) as *i64
280 let out4: *u8 = sys_mmap(SB_OUT)
281 let ol4: *i64 = sys_mmap(16) as *i64
282 ol3[0] = 0
283 ol4[0] = 0
284 if do_measure == 1 {
285 let av2: *i64 = sys_mmap(96) as *i64
286 av2[0] = SB_ELF_SDF as i64
287 av2[1] = SB_EMIT as i64
288 av2[2] = ("0" as *u8) as i64
289 av2[3] = ("500" as *u8) as i64
290 av2[4] = ("160" as *u8) as i64
291 av2[5] = ("3000" as *u8) as i64
292 av2[6] = ("0" as *u8) as i64
293 // k default, redist on, RESIDUAL OFF: the measured artifact is the rules; the residual (rewritten by the gapmap
294 // below in the corrected frame) is the oracle's field folded back and belongs to the consumers that ask for it
295 av2[7] = ("-1" as *u8) as i64
296 av2[8] = ("1" as *u8) as i64
297 av2[9] = ("0" as *u8) as i64
298 av2[10] = 0
299 let out2: *u8 = sys_mmap(SB_OUT)
300 let ol2: *i64 = sys_mmap(16) as *i64
301 let t2: i64 = sb_ms()
302 rc2 = tr_run_capture(SB_ELF_SDF, av2, out2, SB_OUT, ol2)
303 emit_ms = sb_max(emit_ms, sb_ms() - t2)
304 fsrc = sb_field(out2, ol2[0], "\x22fit_src\x22:" as *u8)
305 // ---- 3: gapmap re-measures WHERE the skull still deviates (regions; the nasal box follows the fit) ----
306 let av3: *i64 = sys_mmap(96) as *i64
307 av3[0] = SB_ELF_SDF as i64
308 av3[1] = ("gapmap" as *u8) as i64
309 av3[2] = SB_ORACLE as i64
310 av3[3] = SB_HEAT_OURS as i64
311 av3[4] = SB_HEAT_ORACLE as i64
312 av3[5] = ("0" as *u8) as i64
313 av3[6] = ("500" as *u8) as i64
314 av3[7] = ("3000" as *u8) as i64
315 // the residual layer, REGENERATED in the corrected frame every measure phase: the file the emit's default path
316 // loads was derived against the supine oracle and could not be retired under this lane's caps, so it is rewritten
317 av3[8] = SB_RESIDUAL as i64
318 av3[9] = 0
319 let t3: i64 = sb_ms()
320 rc3 = tr_run_capture(SB_ELF_SDF, av3, out3, SB_OUT, ol3)
321 gapmap_ms = sb_max(gapmap_ms, sb_ms() - t3)
322 // ---- 4: the canon gate on the emitted mesh: the FACE, landmark by landmark; its exit code is its verdict ----
323 let av4: *i64 = sys_mmap(32) as *i64
324 av4[0] = SB_ELF_CANON as i64
325 av4[1] = SB_EMIT as i64
326 av4[2] = 0
327 let t4: i64 = sb_ms()
328 rc4 = tr_run_capture(SB_ELF_CANON, av4, out4, SB_OUT, ol4)
329 canon_ms = sb_max(canon_ms, sb_ms() - t4)
330 }
331 // ---- verdicts, each named (a compound assertion must name its failing conjunct) ----
332 var bad: *u8 = 0 as *u8
333 var code: i64 = 0
334 if do_tune == 1 {
335 if rc1 != 0 { bad = "tune-rc" as *u8; code = 1 }
336 if code == 0 { if fin < 0 { bad = "tune-unparsed" as *u8; code = 1 } }
337 if code == 0 { if fin == 0 { bad = "vacuous-objective-zero" as *u8; code = 2 } }
338 if code == 0 { if start >= 0 { if fin > start { bad = "objective-regressed" as *u8; code = 2 } } }
339 }
340 if do_measure == 1 {
341 if code == 0 { if rc2 != 0 { bad = "emit-rc" as *u8; code = 3 } }
342 if code == 0 { if fsrc != 1 { bad = "fit-not-loaded" as *u8; code = 3 } }
343 if code == 0 { if rc3 != 0 { bad = "gapmap-rc" as *u8; code = 4 } }
344 if code == 0 { if rc4 < 0 { bad = "canon-gate-did-not-run" as *u8; code = 5 } }
345 }
346 // ---- THE RULER GATES THE PASS (AN17, 2026-09-19). MEASURED: the surface objective fell 629 to 531 while the chin
347 // depth moved +4 to +19.6 mm, so a lower objective is not evidence the face improved. A pass is ACCEPTED only if the
348 // canon ruler's in-band count did not fall from the count the last accepted vector earned; otherwise the banked
349 // pre-tune vector is restored and the trend row says so. No weight is typed: the ruler's own count decides.
350 var inb: i64 = 0 - 1
351 var rejected: i64 = 0
352 if do_measure == 1 { inb = sb_field(out4, ol4[0], "inband=" as *u8) }
353 var stamp_inb: i64 = prev_inb
354 if inb >= 0 { stamp_inb = inb }
355 if do_tune == 1 { if do_measure == 1 { if code == 0 { if prev_inb >= 0 { if inb >= 0 { if inb < prev_inb {
356 let rl: *i64 = sys_mmap(16) as *i64
357 let rb: *u8 = sb_read(SB_FITBANK, rl)
358 if rl[0] > 0 { if sb_write(SB_FIT, rb, rl[0]) == 0 { rejected = 1; stamp_inb = prev_inb } }
359 } } } } } }
360 // ---- ONE trend line, written on success AND failure (a beat with no evidence line is a claim) ----
361 let now: i64 = sys_now_realtime_sec()
362 var o: i64 = sj_catn(ln, 0, now)
363 ln[o] = 9 as u8; o = o + 1
364 o = sj_cat(ln, o, "TUNE start_centi=" as *u8); o = sj_catn(ln, o, start)
365 o = sj_cat(ln, o, " final_centi=" as *u8); o = sj_catn(ln, o, fin)
366 o = sj_cat(ln, o, " passes=" as *u8); o = sj_catn(ln, o, passes * do_tune)
367 o = sj_cat(ln, o, " step=" as *u8); o = sj_catn(ln, o, step)
368 o = sj_cat(ln, o, " tune_ms=" as *u8); o = sj_catn(ln, o, tune_ms)
369 ln[o] = 9 as u8; o = o + 1
370 o = sj_cat(ln, o, "FIT src=" as *u8); o = sj_catn(ln, o, fsrc)
371 ln[o] = 9 as u8; o = o + 1
372 if code == 0 {
373 o = sj_cat(ln, o, "GAP " as *u8)
374 if do_measure == 1 {
375 let before: i64 = o
376 o = sb_copy_line(out3, ol3[0], "\x22regions\x22:" as *u8, ln, o, SB_LINE)
377 if o == before { o = sj_cat(ln, o, "regions-unparsed" as *u8) }
378 } else { o = sj_cat(ln, o, "not-measured-this-run" as *u8) }
379 ln[o] = 9 as u8; o = o + 1
380 o = sj_cat(ln, o, "CANON rc=" as *u8); o = sj_catn(ln, o, rc4); o = sj_cat(ln, o, " " as *u8)
381 if do_measure == 1 {
382 let before2: i64 = o
383 o = sb_copy_line(out4, ol4[0], "SKC-LANDMARKS" as *u8, ln, o, SB_LINE)
384 if o == before2 { o = sj_cat(ln, o, "landmarks-unparsed" as *u8) }
385 } else { o = sj_cat(ln, o, "not-measured-this-run" as *u8) }
386 } else {
387 o = sj_cat(ln, o, "BEAT-FAIL " as *u8)
388 o = sj_cat(ln, o, bad)
389 o = sj_cat(ln, o, " rc1=" as *u8); o = sj_catn(ln, o, rc1)
390 o = sj_cat(ln, o, " rc2=" as *u8); o = sj_catn(ln, o, rc2)
391 o = sj_cat(ln, o, " rc3=" as *u8); o = sj_catn(ln, o, rc3)
392 o = sj_cat(ln, o, " rc4=" as *u8); o = sj_catn(ln, o, rc4)
393 }
394 ln[o] = 9 as u8; o = o + 1
395 o = sj_cat(ln, o, "PLAN budget_ms=" as *u8); o = sj_catn(ln, o, budget)
396 o = sj_cat(ln, o, " fixed_ms=" as *u8); o = sj_catn(ln, o, emit_ms + gapmap_ms + canon_ms)
397 o = sj_cat(ln, o, " pass_ms_max=" as *u8); o = sj_catn(ln, o, pass_ms)
398 o = sj_cat(ln, o, " phase=" as *u8); o = sj_catn(ln, o, phase)
399 o = sj_cat(ln, o, " prev_killed=" as *u8); o = sj_catn(ln, o, prev_killed)
400 if prev_killed == 1 { o = sj_cat(ln, o, " prev_planned=" as *u8); o = sj_catn(ln, o, prev_planned) }
401 ln[o] = 9 as u8; o = o + 1
402 o = sj_cat(ln, o, "RULER inband=" as *u8); o = sj_catn(ln, o, inb)
403 o = sj_cat(ln, o, " inband_prev=" as *u8); o = sj_catn(ln, o, prev_inb)
404 o = sj_cat(ln, o, " pass_rejected=" as *u8); o = sj_catn(ln, o, rejected)
405 if rejected == 1 { o = sj_cat(ln, o, " fit_restored_from=" as *u8); o = sj_cat(ln, o, SB_FITBANK) }
406 ln[o] = 10 as u8; o = o + 1
407 if sb_append(SB_TREND, ln, o) != 0 {
408 sb_puts("SKULLFIT-BEAT FAIL cannot append trend file\n" as *u8)
409 return 1
410 }
411 sb_stamp(now0, now, passes, phase, step, pass_ms, emit_ms, gapmap_ms, canon_ms, stamp_inb)
412 // canonical last line: the verdict plus the numbers it rests on
413 sb_puts("{\x22organ\x22:\x22nx_skullfit_beat\x22,\x22start_centi\x22:" as *u8)
414 no_ = sj_catn(nb, 0, start); sys_write(1, nb, no_)
415 sb_puts(",\x22final_centi\x22:" as *u8); no_ = sj_catn(nb, 0, fin); sys_write(1, nb, no_)
416 sb_puts(",\x22fit_src\x22:" as *u8); no_ = sj_catn(nb, 0, fsrc); sys_write(1, nb, no_)
417 sb_puts(",\x22canon_rc\x22:" as *u8); no_ = sj_catn(nb, 0, rc4); sys_write(1, nb, no_)
418 sb_puts(",\x22ruler_inband\x22:" as *u8); no_ = sj_catn(nb, 0, inb); sys_write(1, nb, no_)
419 sb_puts(",\x22pass_rejected\x22:" as *u8); no_ = sj_catn(nb, 0, rejected); sys_write(1, nb, no_)
420 sb_puts(",\x22verdict\x22:\x22" as *u8)
421 if code == 0 { sb_puts("GREEN" as *u8) } else { sb_puts("RED" as *u8) }
422 sb_puts("\x22}\n" as *u8)
423 return code
424}