nx_autofix_auto.nx source
↩ module page · 856 lines · 46116 B
1// nx_autofix_auto.nx -- THE SELF-LOCALIZING FIX LOOP (2026-07-16): removes the last a-priori knowledge
2// from the autonomous fix episode. nx_autofix_localfix proved the LOCAL MAKER (model-generated fix,
3// machine-verified) but was TOLD the buggy line. This organ DISCOVERS it: it parses the candidate
4// grader's own per-function FNRES rows to find WHICH function fails, extracts that function's current
5// source line from the file (brace-depth clip), and only then hands it to the local maker. The
6// independent grader (fresh compile+run) remains the only judge at every stage.
7//
8// THE NOT-HARDCODED TOOTH: the SAME organ runs against TWO candidates whose seeded bugs live in
9// DIFFERENT functions (candidate2 -> sgn, candidate3 -> dbl; neither is in the few-shot template).
10// Correct localization must name a different function in each episode -- data-driven, not baked in.
11//
12// Episode: [1 IDENTIFY+LOCATE] grader run -> failing FNRES row -> function name [2 FLAG w/ the
13// DISCOVERED name] [3 BACKUP] [4 FIX = local 1.5B i8 greedy, few-shot, buggy line = the EXTRACTED
14// source] [5 VERIFY = no failing FNRES row] [6 REVERT + re-grade = original failing row returns].
15// RETRY LADDER (2026-07-17): round-2 = ONE greedy retry on a reframed prompt (echo-aware + machine
16// report); rounds 3+ = AF_BON_N SAMPLED retries (pinned temp/top-p/top-k, FIXED seed ladder -> the
17// whole sweep stays deterministic: seeds are part of the input, xorshift64* is seed-deterministic).
18// Greedy is a fixed point (same prompt -> same bytes -- the v2 sweep showed every greedy round-2 on
19// a miss reproduced round-1 byte-identically); independent samples break it (A1 best-of-N precedent).
20// The fresh compile+run grader stays the ONLY judge at every rung; failed samples are reverted.
21// argv: <model> [manifest] [ledger-override] -- the override is the TESTING LANE: a validation run
22// writes episode rows to a scratch ledger so it can never masquerade as the newest official batch
23// (the swebench gate + autograde pulse read the real ledger's newest ts batch).
24// Green-only: CGB localpass row per verified fix (generator=15b-autofix-auto). Misses bank curriculum.
25// exit: 0 = every episode's loop sound (fix green OR honest miss) | 1 = a loop-integrity failure.
26// license_tier: ORIGINAL
27import "nx_tool_run.nx"
28import "nx_propose_verify_lib.nx"
29import "nx_gramdec_lib.nx"
30import "nx_autofix_lib.nx"
31const AF_MAGIC_131072: i64 = 131072
32const AF_MAGIC_262144: i64 = 262144
33const AF_MAGIC_4000: i64 = 4000
34const AF_MAGIC_1500: i64 = 1500
35const AF_MAGIC_2048: i64 = 2048
36const AF_MAGIC_1048576: i64 = 1048576
37const AF_MAGIC_1024: i64 = 1024
38const AF_MAGIC_4096: i64 = 4096
39const AF_MAGIC_65536: i64 = 65536
40const AF_MAGIC_65500: i64 = 65500
41
42// Best-of-N sampling params: PINNED consts, not per-run knobs -- they are part of the standing
43// benchmark's contract (changing them = a benchmark revision). temp 800 permille = the A1 precedent.
44const AF_BON_N: i64 = 3 // sampled retries after both greedy rounds miss
45const AF_BON_TEMP_PM: i64 = 800 // permille temperature (800 = 0.8)
46const AF_BON_TOPP_PM: i64 = 950 // permille nucleus mass (950 = 0.95)
47const AF_BON_TOPK: i64 = 40 // top-k cutoff (nsv sampler caps at 256)
48const AF_BON_SEED0: i64 = 1000003 // seed ladder: seed(s) = SEED0*(s+1); deterministic, never 0
49
50// NB10 grammar-constrained decode flag (argv[5] starts with 'g' -> 1). Module flag at the TOP of the
51// file (before any reader -- the forward-static-ref rule). 0 = the proven nsv path, byte-identical.
52static g_af_gc: i64
53
54// ---- M0 COMPOSITION (autodev AD2-AD7, 2026-08-27): the rulers live in nx_autofix_lib; these statics
55// carry the per-run configuration (conf-read in main -- AD12 retires the sampler literals) and the
56// per-episode sandbox. The af_* functions below are THE CONTRACT SYMBOLS the /compare/autodev board
57// watches: each is the loop's own call site composing the one proven ruler, never a second copy.
58static g_bon_n: i64
59static g_bon_temp: i64
60static g_bon_topp: i64
61static g_bon_topk: i64
62static g_af_sb_addr: i64
63static g_af_wall_ms: i64
64static g_af_as_bytes: i64
65static g_af_null_max: i64
66static g_af_prior_addr: i64
67static g_af_harness_addr: i64
68static g_af_led_default: i64
69
70// FNV-1a 64-bit (offset basis 14695981039346656037 spelled as this signed literal; prime
71// 1099511628211): the config-identity hash behind the harness token. Deliberately NOT a content hash
72// of the weights (hashing 1.1 GB per run is a beat's job; the model PATH names the pin) -- a declared
73// limit, printed with the token wherever it travels.
74const AF_FNV_OFFSET: i64 = 0 - 3750763034362895579
75const AF_FNV_PRIME: i64 = 1099511628211
76const AF_HEX_DIGITS: i64 = 16
77const AF_HEX_A_OFF: i64 = 87
78const AF_ASCII_ZERO: i64 = 48
79
80func af_sandbox_root(ts: i64, cname: *u8, srcpath: *u8, out: *u8) -> i64 { return afl_sandbox_make(ts, cname, srcpath, out) }
81func af_episode_ledger(row: *u8, n: i64, where: *u8) -> i64 { return afl_ledger_append(row, n, where) }
82func af_admit_untrusted(row: *u8, rl: i64, name: *u8, path: *u8, prov: *u8) -> i64 { return afl_admit_row(row, rl, name, path, prov) }
83func af_null_control(sb: *u8, cname: *u8, sbsrc: *u8, snap: *u8, buggy: *u8, prior: *u8, out: *u8) -> i64 { return afl_null_controls(sb, cname, sbsrc, snap, buggy, prior, out, AF_MAGIC_1048576, g_af_wall_ms, g_af_as_bytes) }
84func af_intake_board(outpath: *u8) -> i64 { let e: *u8 = "" as *u8; return afl_intake_emit(outpath, e, e, e, e, e, 1) }
85func af_harness_calc(mpath: *u8, strict: i64, gc: i64) -> i64 {
86 var h: i64 = AF_FNV_OFFSET
87 var i: i64 = 0
88 while mpath[i] != (0 as u8) { h = (h ^ (mpath[i] as i64)) * AF_FNV_PRIME; i = i + 1 }
89 h = (h ^ (AF_ASCII_ZERO + strict)) * AF_FNV_PRIME
90 h = (h ^ (AF_ASCII_ZERO + gc)) * AF_FNV_PRIME
91 h = (h ^ g_bon_n) * AF_FNV_PRIME
92 h = (h ^ g_bon_temp) * AF_FNV_PRIME
93 h = (h ^ g_bon_topp) * AF_FNV_PRIME
94 h = (h ^ g_bon_topk) * AF_FNV_PRIME
95 let buf: *u8 = sys_mmap(24)
96 buf[0] = 104 as u8
97 var k: i64 = 0
98 while k < AF_HEX_DIGITS {
99 let nib: i64 = (h >> ((AF_HEX_DIGITS - 1 - k) * 4)) & 15
100 if nib < 10 { buf[1 + k] = (AF_ASCII_ZERO + nib) as u8 } else { buf[1 + k] = (AF_HEX_A_OFF + nib) as u8 }
101 k = k + 1
102 }
103 buf[17] = 0 as u8
104 return buf as i64
105}
106
107func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
108func wn(v: i64) -> i64 {
109 var m: i64 = v
110 if m < 0 { w("-" as *u8); m = 0 - m }
111 let t: *u8 = sys_mmap(24)
112 var k: i64 = 0
113 if m == 0 { t[0] = 48 as u8; k = 1 }
114 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
115 let o: *u8 = sys_mmap(24)
116 var i: i64 = 0
117 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
118 sys_write(1, o, k)
119 return 0
120}
121func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
122func find_first(hay: *u8, hn: i64, needle: *u8, from: i64) -> i64 {
123 let m: i64 = slen(needle)
124 if m == 0 { return 0 - 1 }
125 var i: i64 = from
126 while i + m <= hn {
127 var j: i64 = 0
128 var ok: i64 = 1
129 while j < m { if hay[i+j] != needle[j] { ok = 0; j = m } else { j = j + 1 } }
130 if ok == 1 { return i }
131 i = i + 1
132 }
133 return 0 - 1
134}
135func bcat(b: *u8, off: i64, s: *u8) -> i64 {
136 var i: i64 = 0
137 var o: i64 = off
138 while s[i] != (0 as u8) { b[o] = s[i]; o = o + 1; i = i + 1 }
139 return o
140}
141func bcatn(b: *u8, off: i64, v: i64) -> i64 {
142 var m: i64 = v
143 var o: i64 = off
144 if m < 0 { b[o] = 45 as u8; o = o + 1; m = 0 - m }
145 let t: *u8 = sys_mmap(24)
146 var k: i64 = 0
147 if m == 0 { t[0] = 48 as u8; k = 1 }
148 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
149 var i: i64 = 0
150 while i < k { b[o] = t[k - 1 - i]; o = o + 1; i = i + 1 }
151 return o
152}
153func copyfile(srcp: *u8, dstp: *u8) -> i64 { return afl_copyfile(srcp, dstp) } // ONE copy: nx_autofix_lib
154func apply_fix(path: *u8, finds: *u8, repls: *u8) -> i64 { return afl_apply_fix(path, finds, repls) } // ONE copy: nx_autofix_lib
155func append_line(path: *u8, line: *u8, ln: i64) -> i64 {
156 let ob: *i64 = sys_mmap(8) as *i64
157 let old: *u8 = sys_read_file(path, ob)
158 let full: *u8 = sys_mmap(AF_MAGIC_262144)
159 var o: i64 = 0
160 if (old as i64) != 0 { var z: i64 = 0; while z < ob[0] { full[o] = old[z]; o = o + 1; z = z + 1 } }
161 var z2: i64 = 0
162 while z2 < ln { full[o] = line[z2]; o = o + 1; z2 = z2 + 1 }
163 let fd: i64 = sys_openat_wr(path, 0x1a4)
164 if fd < 0 { return 0 - 1 }
165 sys_write(fd, full, o)
166 sys_close(fd)
167 return o
168}
169func lf_gen(pb: *u8, plen: i64, tx: *u8, mt: *i64, max_new: i64) -> i64 {
170 let gp: *i64 = sys_mmap(16 * 8) as *i64
171 gp[0] = pb as i64
172 gp[1] = plen
173 gp[2] = max_new
174 gp[3] = 1
175 gp[4] = tx as i64
176 gp[5] = AF_MAGIC_4000
177 gp[6] = mt as i64
178 gp[7] = 0 - 1
179 gp[8] = 0
180 gp[9] = 0
181 gp[10] = 0
182 gp[11] = 0
183 gp[12] = 0
184 if g_af_gc == 1 { return gdx_generate(gp) }
185 return nsv_generate(gp)
186}
187// sampled variant: temp>0 + explicit seed (gp[8]=temp_pm, gp[9]=top_p_pm, gp[10]=top_k, gp[11]=seed).
188// Deterministic per seed -- the seed ladder makes best-of-N reproducible sweep-to-sweep.
189func lf_gen_t(pb: *u8, plen: i64, tx: *u8, mt: *i64, max_new: i64, temp_pm: i64, seed: i64) -> i64 {
190 let gp: *i64 = sys_mmap(16 * 8) as *i64
191 gp[0] = pb as i64
192 gp[1] = plen
193 gp[2] = max_new
194 gp[3] = 1
195 gp[4] = tx as i64
196 gp[5] = AF_MAGIC_4000
197 gp[6] = mt as i64
198 gp[7] = 0 - 1
199 gp[8] = temp_pm
200 gp[9] = g_bon_topp
201 gp[10] = g_bon_topk
202 gp[11] = seed
203 gp[12] = 0
204 if g_af_gc == 1 { return gdx_generate(gp) }
205 return nsv_generate(gp)
206}
207
208// run the candidate's grader; capture stdout; return capture length via box (negative rc -> 0)
209func grader_run(name: *u8, out: *u8, cap: i64) -> i64 {
210 // M0 AD7: when an episode has opened a sandbox the judge builds and runs THERE, under the conf
211 // bounds; the legacy in-tree path remains for a caller that opened none (none does today).
212 if g_af_sb_addr != 0 {
213 let st: *i64 = sys_mmap(16) as *i64
214 let n: i64 = afl_sandbox_run(g_af_sb_addr as *u8, name, out, cap, g_af_wall_ms, g_af_as_bytes, st)
215 if st[0] == 1 { w(" [SANDBOX] candidate KILLED at the wall deadline -- judged as build-broke\n" as *u8) }
216 return n
217 }
218 let av: *i64 = sys_mmap(64) as *i64
219 av[0] = "_offc/nx_sov_build_run.elf" as *u8 as i64
220 av[1] = name as i64
221 av[2] = 0
222 let olen: *i64 = sys_mmap(8) as *i64
223 olen[0] = 0
224 tr_run_capture("_offc/nx_sov_build_run.elf" as *u8, av, out, cap, olen)
225 return olen[0]
226}
227
228// parse FNRES rows in out[0,n): find the FIRST failing one (passed < total); copy its name into
229// namebuf (null-terminated, cap 24). returns 1 found-failing / 0 all-pass / -1 no FNRES rows at all.
230// ONE copy: nx_autofix_lib (the offset-desync fix travels with it); callers allocate 64-byte name
231// buffers because the lib clamps at AFL_NAME-1=63, not the old 23.
232func fn_failing(out: *u8, n: i64, namebuf: *u8) -> i64 { return afl_fn_failing(out, n, namebuf) }
233
234// extract the one-line `func <name>...` definition from the source file into linebuf (null-terminated).
235func extract_fn_line(path: *u8, name: *u8, linebuf: *u8) -> i64 {
236 let lb: *i64 = sys_mmap(8) as *i64
237 let src: *u8 = sys_read_file(path, lb)
238 if (src as i64) == 0 { return 0 }
239 let n: i64 = lb[0]
240 let needle: *u8 = sys_mmap(64)
241 var no: i64 = 0
242 no = bcat(needle, no, "func " as *u8)
243 no = bcat(needle, no, name)
244 needle[no] = 0 as u8
245 let fs: i64 = find_first(src, n, needle, 0)
246 if fs < 0 { return 0 }
247 // multi-line: stop only at brace-closure (depth 0), never at a newline
248 var fe: i64 = fs
249 var depth: i64 = 0
250 var seen: i64 = 0
251 var go: i64 = 1
252 while go == 1 {
253 if fe >= n { go = 0 }
254 else {
255 let c: i64 = src[fe] as i64
256 if c == 123 { depth = depth + 1; seen = 1 }
257 if c == 125 { depth = depth - 1 }
258 fe = fe + 1
259 if seen == 1 { if depth == 0 { go = 0 } }
260 }
261 }
262 var fl: i64 = 0
263 var z: i64 = fs
264 while z < fe { if fl < AF_MAGIC_1500 { linebuf[fl] = src[z]; fl = fl + 1 } z = z + 1 }
265 linebuf[fl] = 0 as u8
266 return fl
267}
268
269// collect the FNCASE rows for `name` from a grader capture into fb (newline-joined, null-terminated).
270func collect_cases(cap: *u8, n: i64, name: *u8, fb: *u8) -> i64 {
271 var fbn: i64 = 0
272 let cnee: *u8 = sys_mmap(64)
273 var cno: i64 = 0
274 cno = bcat(cnee, cno, "FNCASE " as *u8)
275 cno = bcat(cnee, cno, name)
276 cnee[cno] = 32 as u8
277 cnee[cno + 1] = 0 as u8
278 var cp: i64 = 0
279 var cdone: i64 = 0
280 while cdone == 0 {
281 let cr: i64 = find_first(cap, n, cnee, cp)
282 if cr < 0 { cdone = 1 }
283 else {
284 var ce: i64 = cr
285 var sc: i64 = 1
286 while sc == 1 { if ce >= n { sc = 0 } else { if cap[ce] == (10 as u8) { sc = 0 } else { ce = ce + 1 } } }
287 var zz: i64 = cr
288 while zz < ce { if fbn < 1000 { fb[fbn] = cap[zz]; fbn = fbn + 1 } zz = zz + 1 }
289 if fbn < 1000 { fb[fbn] = 10 as u8; fbn = fbn + 1 }
290 cp = ce
291 }
292 }
293 fb[fbn] = 0 as u8
294 return fbn
295}
296
297// clip `func <name>...` from generated text tx[0,gl) through brace-depth 0 into dst; returns length.
298// MULTI-LINE (2026-07-16): stops ONLY when brace depth returns to 0 (or buffer end) -- newlines are
299// part of the function body now, so multi-line definitions extract and splice whole. One-line
300// behavior is unchanged (depth-0 lands on the same closing brace).
301// NB7 general-model bridge: normalize a generated fix (else-if/else -> valid NishiLang early-return)
302// by forking the gated nx_nishilang_norm organ. Idempotent on clean NishiLang (proven, gate T3) so
303// the 1.5B is untouched; unlocks general coders (coder15/Qwen-class). Fail-open: any hiccup -> raw.
304func nrm_fix(raw: *u8, rawlen: i64, out: *u8) -> i64 {
305 let fd: i64 = sys_openat_wr("/tmp/afx_nn_in.txt" as *u8, 0x1a4)
306 if fd < 0 { var z: i64 = 0; while z < rawlen { out[z] = raw[z]; z = z + 1 } out[rawlen] = 0 as u8; return rawlen }
307 sys_write(fd, raw, rawlen)
308 sys_close(fd)
309 let pid: i64 = sys_fork()
310 if pid == 0 {
311 let av: *i64 = sys_mmap(32) as *i64
312 av[0] = "_offc/nx_nishilang_norm.elf" as *u8 as i64
313 av[1] = "/tmp/afx_nn_in.txt" as *u8 as i64
314 av[2] = "/tmp/afx_nn_out.txt" as *u8 as i64
315 av[3] = 0
316 let envp: *i64 = sys_mmap(16) as *i64
317 envp[0] = 0
318 sys_execve("_offc/nx_nishilang_norm.elf" as *u8, av, envp)
319 sys_exit(127)
320 }
321 let st: *i64 = sys_mmap(16) as *i64
322 sys_wait4(pid, st, 0)
323 let lb: *i64 = sys_mmap(8) as *i64
324 let res: *u8 = sys_read_file("/tmp/afx_nn_out.txt" as *u8, lb)
325 if (res as i64) == 0 { var z2: i64 = 0; while z2 < rawlen { out[z2] = raw[z2]; z2 = z2 + 1 } out[rawlen] = 0 as u8; return rawlen }
326 let rn: i64 = lb[0]
327 var i: i64 = 0
328 while i < rn { out[i] = res[i]; i = i + 1 }
329 out[rn] = 0 as u8
330 return rn
331}
332func clip_fn(tx: *u8, gl: i64, needle: *u8, dst: *u8) -> i64 {
333 var fl: i64 = 0
334 if gl <= 0 { dst[0] = 0 as u8; return 0 }
335 var fs: i64 = find_first(tx, gl, needle, 0)
336 if fs < 0 { fs = 0 }
337 var fe: i64 = fs
338 var depth: i64 = 0
339 var seen: i64 = 0
340 var go: i64 = 1
341 while go == 1 {
342 if fe >= gl { go = 0 }
343 else {
344 let c: i64 = tx[fe] as i64
345 if c == 123 { depth = depth + 1; seen = 1 }
346 if c == 125 { depth = depth - 1 }
347 fe = fe + 1
348 if seen == 1 { if depth == 0 { go = 0 } }
349 }
350 }
351 var z: i64 = fs
352 while z < fe { if fl < AF_MAGIC_1500 { dst[fl] = tx[z]; fl = fl + 1 } z = z + 1 }
353 dst[fl] = 0 as u8
354 // normalize else-if/else -> valid NishiLang (general-model bridge); idempotent on clean input
355 let nb: *u8 = sys_mmap(AF_MAGIC_2048)
356 let nl: i64 = nrm_fix(dst, fl, nb)
357 var k: i64 = 0
358 while k < nl { dst[k] = nb[k]; k = k + 1 }
359 dst[nl] = 0 as u8
360 return nl
361}
362
363// one full self-localizing episode against candidate build-name `cname` with source path `cpath`.
364// `ledp` = the episode-row ledger (production default or the argv[3] testing-lane override).
365// returns 1 maker-green / 0 honest-miss / -1 loop-unsound.
366// strict=1 -> append the NishiLang-syntax rule to the maker prompt (for GENERAL coders like coder15
367// that emit else-if/abs); strict=0 -> clean prompt (the tuned 1.5B is confused by the rule -> the
368// negation mention makes it add spurious negative branches, regressing dbl). Per-maker, set from argv[4].
369func episode(cname: *u8, cpath: *u8, bakp: *u8, ts: i64, out: *u8, ledp: *u8, strict: i64) -> i64 {
370 w("\n--- EPISODE: " as *u8); w(cname); w(" ---\n" as *u8)
371 // M0 AD7 (af_sandbox_root): resolve the SOURCE tree copy (never the caller's CWD), open a scratch
372 // root on an exec-capable filesystem, and mutate ONLY the sandbox copy from here on. The tree copy
373 // is the witness: byte-identical at the end or the episode is unsound.
374 let srcres: *u8 = sys_mmap(4096)
375 var cp2: *u8 = cpath
376 if afl_exists(cp2) == 0 { if ep_src_path(srcres, cname) == 1 { cp2 = srcres } }
377 let sb: *u8 = sys_mmap(4096)
378 if af_sandbox_root(ts, cname, cp2, sb) < 1 { w(" [SANDBOX] could not open a scratch root -> episode refused\n" as *u8); return 0 - 1 }
379 g_af_sb_addr = sb as i64
380 let sbp: *u8 = sys_mmap(4096)
381 afl_sandbox_src(sb, cname, sbp)
382 let bak2: *u8 = sys_mmap(4096)
383 var bo2: i64 = afl_cat(bak2, 0, sb)
384 afl_cat(bak2, bo2, "snapshot.nx" as *u8)
385 w(" [SANDBOX] root=" as *u8); w(sb); w(" (closure copied; the tree copy is read-only from here)\n" as *u8)
386 // 1 IDENTIFY + LOCATE
387 let n0: i64 = grader_run(cname, out, AF_MAGIC_1048576)
388 let name: *u8 = sys_mmap(64)
389 let loc: i64 = fn_failing(out, n0, name)
390 if loc != 1 { w(" [1 LOCATE] no failing function found -> nothing to fix\n" as *u8); return 0 }
391 w(" [1 LOCATE] grader rows name the failing function: " as *u8); w(name); w(" (DISCOVERED, not told)\n" as *u8)
392 // extract its current source line
393 let buggy: *u8 = sys_mmap(AF_MAGIC_2048)
394 let bl: i64 = extract_fn_line(sbp, name, buggy)
395 if bl <= 0 { w(" extract failed -> miss\n" as *u8); return 0 }
396 w(" [1 EXTRACT] current source: " as *u8); w(buggy); w("\n" as *u8)
397 // collect the grader's FNCASE rows for the located function -- pure machine feedback the maker
398 // sees verbatim (present only on candidates that emit per-case rows; absent rows change nothing).
399 let fb: *u8 = sys_mmap(AF_MAGIC_1024)
400 let fbn: i64 = collect_cases(out, n0, name, fb)
401 if fbn > 0 { w(" [1 CASES] machine-reported failing cases fed to the maker:\n" as *u8); w(fb) }
402 // 2 FLAG with the discovered name
403 let qrow: *u8 = sys_mmap(512)
404 var qo: i64 = 0
405 qo = bcat(qrow, qo, "AUTOFIX-FLAG ts=" as *u8)
406 qo = bcatn(qrow, qo, ts)
407 qo = bcat(qrow, qo, " file=" as *u8)
408 qo = bcat(qrow, qo, cname)
409 qo = bcat(qrow, qo, ".nx bug=LOCATED:" as *u8)
410 qo = bcat(qrow, qo, name)
411 qo = bcat(qrow, qo, " proposed=LOCAL-MODEL-GENERATED-FIX status=PENDING-OPERATOR-APPROVAL\n" as *u8)
412 append_line("/home/elderwesto/nx_stage/autofix_approval_queue.log" as *u8, qrow, qo)
413 w(" [2 FLAG] finding (with the discovered function) -> approval queue; approval simulated for this measured episode\n" as *u8)
414 // 3 BACKUP
415 let bk: i64 = copyfile(sbp, bak2)
416 if bk <= 0 { w(" BACKUP FAILED -> refusing to mutate\n" as *u8); return 0 - 1 }
417 w(" [3 BACKUP] " as *u8); wn(bk); w("B saved before any mutation\n" as *u8)
418 // M0 AD4 (af_null_control): the null resolvers run through the SAME judge FIRST; any GREEN null is
419 // an oracle defect and the episode is refused rather than graded (the conf row decides the bar).
420 var priorp: *u8 = "" as *u8
421 if g_af_prior_addr != 0 { priorp = g_af_prior_addr as *u8 }
422 let ng_null: i64 = af_null_control(sb, cname, sbp, bak2, buggy, priorp, out)
423 if ng_null > g_af_null_max { w(" [4n NULL] REFUSED-ORACLE-DEFECT: a null resolver scored GREEN -- nothing about this instance is publishable\n" as *u8); return 0 - 1 }
424 // 4 FIX -- local maker on the EXTRACTED line
425 let pbuf: *u8 = sys_mmap(AF_MAGIC_4096)
426 let tx: *u8 = sys_mmap(AF_MAGIC_4096)
427 let mt: *i64 = sys_mmap(64) as *i64
428 var po: i64 = 0
429 po = bcat(pbuf, po, "Fix each buggy NishiLang function. Output only the corrected function on one line.\n" as *u8)
430 if strict == 1 { po = bcat(pbuf, po, "NishiLang syntax (STRICT): NO else, NO else-if -- separate `if COND { return V }` lines with early return. Call NO helper functions (no abs/min/max/pow) -- inline with only + - * / % and comparisons. Negate as `0 - x`, never `-x`.\n" as *u8) }
431 po = bcat(pbuf, po, "Buggy: func min2(a: i64, b: i64) -> i64 { return b }\n" as *u8)
432 po = bcat(pbuf, po, "Fixed: func min2(a: i64, b: i64) -> i64 { if a < b { return a } return b }\n" as *u8)
433 po = bcat(pbuf, po, "Buggy: func abs1(x: i64) -> i64 { return x }\n" as *u8)
434 po = bcat(pbuf, po, "Fixed: func abs1(x: i64) -> i64 { if x < 0 { return 0 - x } return x }\n" as *u8)
435 if fbn > 0 {
436 po = bcat(pbuf, po, "Machine test report for the next function:\n" as *u8)
437 po = bcat(pbuf, po, fb)
438 }
439 po = bcat(pbuf, po, "Buggy: " as *u8)
440 po = bcat(pbuf, po, buggy)
441 po = bcat(pbuf, po, "\nFixed: " as *u8)
442 let gl: i64 = lf_gen(pbuf, po, tx, mt, 120)
443 // clip: from "func <name>" through brace-depth 0
444 let needle: *u8 = sys_mmap(64)
445 var no: i64 = 0
446 no = bcat(needle, no, "func " as *u8)
447 no = bcat(needle, no, name)
448 needle[no] = 0 as u8
449 let fixline: *u8 = sys_mmap(AF_MAGIC_2048)
450 let fl: i64 = clip_fn(tx, gl, needle, fixline)
451 w(" [4 FIX] model output (" as *u8); wn(fl); w("B): " as *u8)
452 if fl > 0 { sys_write(1, fixline, fl) }
453 w("\n" as *u8)
454 var applied: i64 = 0
455 var same: i64 = 0
456 var echoed: i64 = 0
457 if fl == bl { var zz: i64 = 0; same = 1; while zz < fl { if fixline[zz] != buggy[zz] { same = 0; zz = fl } else { zz = zz + 1 } } }
458 if fl > 10 { if same == 0 {
459 let ap: i64 = apply_fix(sbp, buggy, fixline)
460 if ap > 0 { applied = 1; w(" [4 FIX] LOCAL-GENERATED line spliced (" as *u8); wn(ap); w("B)\n" as *u8) }
461 } }
462 if fl > 10 { if same == 1 { echoed = 1; w(" [4 FIX] generation REPEATED the buggy line verbatim -> ECHO (rejected pre-verify, never spliced)\n" as *u8) } }
463 if applied == 0 { if echoed == 0 { w(" [4 FIX] unusable generation (empty) -> MAKER-MISS\n" as *u8) } }
464 // 5 VERIFY: no failing FNRES row anymore
465 var green: i64 = 0
466 var n1: i64 = 0
467 var attempts: i64 = 1
468 if applied == 1 {
469 n1 = grader_run(cname, out, AF_MAGIC_1048576)
470 let nm2: *u8 = sys_mmap(64)
471 let loc1: i64 = fn_failing(out, n1, nm2)
472 w(" [5 VERIFY] grader re-run -> " as *u8)
473 if loc1 == 0 { green = 1; w("ALL FUNCTIONS PASS => LOCAL FIX CONFIRMED (discovered, generated, machine-verified)\n" as *u8) }
474 else { w("still failing (" as *u8); if loc1 == 1 { w(nm2) } else { w("no rows -- build broke" as *u8) } w(") -> trying round 2\n" as *u8) }
475 }
476 // ROUND 2 (bounded -- exactly one retry, no cascade): restore the original, then feed the maker
477 // ITS OWN failed attempt + the machine-reported failing cases. TWO ways in: (a) a WRONG-but-different
478 // fix that verified red (post-fix cases available in n1), or (b) an ECHO of the buggy line rejected
479 // pre-verify (no post-fix run happened -> n1=0 -> reuse the ORIGINAL report). Greedy decoding
480 // reproduces an echo from an identical prompt, so the echo retry MUST reframe: a diff-framed
481 // "change the line, do not repeat it" instruction is what breaks the fixed point.
482 var try2: i64 = 0
483 if green == 0 { if applied == 1 { try2 = 1 } if echoed == 1 { try2 = 1 } }
484 if try2 == 1 {
485 attempts = 2
486 let fb2: *u8 = sys_mmap(AF_MAGIC_1024)
487 let fbn2: i64 = collect_cases(out, n1, name, fb2)
488 // echo path never ran a post-fix grader (n1=0) -> fall back to the ORIGINAL machine report
489 var rep: *u8 = fb2
490 var repn: i64 = fbn2
491 if repn <= 0 { rep = fb; repn = fbn }
492 copyfile(bak2, sbp)
493 var po2: i64 = 0
494 po2 = bcat(pbuf, po2, "Fix each buggy NishiLang function. Output only the corrected function on one line.\n" as *u8)
495 if strict == 1 { po2 = bcat(pbuf, po2, "NishiLang syntax (STRICT): NO else, NO else-if -- separate `if COND { return V }` lines with early return. Call NO helper functions (no abs/min/max/pow) -- inline with only + - * / % and comparisons. Negate as `0 - x`, never `-x`.\n" as *u8) }
496 po2 = bcat(pbuf, po2, "Buggy: func min2(a: i64, b: i64) -> i64 { return b }\n" as *u8)
497 po2 = bcat(pbuf, po2, "Fixed: func min2(a: i64, b: i64) -> i64 { if a < b { return a } return b }\n" as *u8)
498 if echoed == 1 {
499 po2 = bcat(pbuf, po2, "Your previous answer was IDENTICAL to the buggy line and was rejected. The buggy line is WRONG -- you MUST change its operator or logic so the function is correct. Do not repeat the buggy line.\n" as *u8)
500 }
501 if repn > 0 {
502 po2 = bcat(pbuf, po2, "Machine test report for the buggy function:\n" as *u8)
503 po2 = bcat(pbuf, po2, rep)
504 }
505 po2 = bcat(pbuf, po2, "Wrong attempt: " as *u8)
506 po2 = bcat(pbuf, po2, fixline)
507 po2 = bcat(pbuf, po2, "\nBuggy: " as *u8)
508 po2 = bcat(pbuf, po2, buggy)
509 po2 = bcat(pbuf, po2, "\nFixed: " as *u8)
510 let gl2: i64 = lf_gen(pbuf, po2, tx, mt, 120)
511 let fix2: *u8 = sys_mmap(AF_MAGIC_2048)
512 let fl2: i64 = clip_fn(tx, gl2, needle, fix2)
513 w(" [4 ROUND-2] model output (" as *u8); wn(fl2); w("B): " as *u8)
514 if fl2 > 0 { sys_write(1, fix2, fl2) }
515 w("\n" as *u8)
516 var same2: i64 = 0
517 if fl2 == fl { var zz3: i64 = 0; same2 = 1; while zz3 < fl2 { if fix2[zz3] != fixline[zz3] { same2 = 0; zz3 = fl2 } else { zz3 = zz3 + 1 } } }
518 // reject a fresh echo of the buggy line pre-verify (same discipline as round 1)
519 var echo2: i64 = 0
520 if fl2 == bl { var zc: i64 = 0; echo2 = 1; while zc < fl2 { if fix2[zc] != buggy[zc] { echo2 = 0; zc = fl2 } else { zc = zc + 1 } } }
521 if fl2 > 10 { if same2 == 0 { if echo2 == 0 {
522 let ap2: i64 = apply_fix(sbp, buggy, fix2)
523 if ap2 > 0 {
524 n1 = grader_run(cname, out, AF_MAGIC_1048576)
525 let nm4: *u8 = sys_mmap(64)
526 let loc4: i64 = fn_failing(out, n1, nm4)
527 w(" [5 VERIFY-2] grader re-run -> " as *u8)
528 if loc4 == 0 { green = 1; w("ALL FUNCTIONS PASS => ROUND-2 FIX CONFIRMED (self-corrected on machine feedback)\n" as *u8) }
529 else { w("still failing -> greedy rounds exhausted; sampled retries next\n" as *u8) }
530 }
531 } } }
532 if green == 0 { if fl2 <= 10 { w(" [4 ROUND-2] unusable generation -> sampled retries next\n" as *u8) } else { if same2 == 1 { w(" [4 ROUND-2] repeated the identical attempt -> sampled retries next\n" as *u8) } else { if echo2 == 1 { w(" [4 ROUND-2] echoed the buggy line again -> sampled retries next\n" as *u8) } } } }
533 // ROUNDS 3+ -- BEST-OF-N SAMPLED RETRIES (only when BOTH greedy rounds missed). Greedy retries
534 // beyond this point are provably useless (fixed point); independent samples on the SAME reframed
535 // prompt explore alternatives. Pinned seed ladder keeps the sweep deterministic. Every candidate:
536 // pre-verify rejected if short/echo/known-wrong/dup (no grader spend on repeats), spliced, judged
537 // by the fresh compile+run, and the candidate file RESTORED before the next attempt unless green.
538 if green == 0 {
539 copyfile(bak2, sbp)
540 let fix3: *u8 = sys_mmap(AF_MAGIC_2048)
541 let tried: *u8 = sys_mmap(g_bon_n * AF_MAGIC_2048)
542 let triedl: *i64 = sys_mmap(g_bon_n * 8) as *i64
543 var ntried: i64 = 0
544 var s: i64 = 0
545 while s < g_bon_n {
546 if green == 0 {
547 attempts = attempts + 1
548 let gl3: i64 = lf_gen_t(pbuf, po2, tx, mt, 120, g_bon_temp, AF_BON_SEED0 * (s + 1))
549 let fl3: i64 = clip_fn(tx, gl3, needle, fix3)
550 w(" [4 SAMPLE-" as *u8); wn(s + 1); w("] model output (" as *u8); wn(fl3); w("B): " as *u8)
551 if fl3 > 0 { sys_write(1, fix3, fl3) }
552 w("\n" as *u8)
553 var reject: i64 = 0
554 if fl3 <= 10 { reject = 1 }
555 if reject == 0 { if fl3 == bl { var za: i64 = 0; var eqa: i64 = 1; while za < fl3 { if fix3[za] != buggy[za] { eqa = 0; za = fl3 } else { za = za + 1 } } if eqa == 1 { reject = 1 } } }
556 if reject == 0 { if fl3 == fl { var zb: i64 = 0; var eqb: i64 = 1; while zb < fl3 { if fix3[zb] != fixline[zb] { eqb = 0; zb = fl3 } else { zb = zb + 1 } } if eqb == 1 { reject = 1 } } }
557 if reject == 0 { if fl3 == fl2 { var zc2: i64 = 0; var eqc: i64 = 1; while zc2 < fl3 { if fix3[zc2] != fix2[zc2] { eqc = 0; zc2 = fl3 } else { zc2 = zc2 + 1 } } if eqc == 1 { reject = 1 } } }
558 if reject == 0 {
559 var d: i64 = 0
560 while d < ntried {
561 if fl3 == triedl[d] {
562 let tp: *u8 = ((tried as i64) + d * AF_MAGIC_2048) as *u8
563 var zd: i64 = 0
564 var eqd: i64 = 1
565 while zd < fl3 { if fix3[zd] != tp[zd] { eqd = 0; zd = fl3 } else { zd = zd + 1 } }
566 if eqd == 1 { reject = 1; d = ntried }
567 }
568 d = d + 1
569 }
570 }
571 if reject == 1 { w(" [4 SAMPLE-" as *u8); wn(s + 1); w("] rejected pre-verify (short/echo/known-wrong/dup) -> no grader spend\n" as *u8) }
572 if reject == 0 {
573 let tp2: *u8 = ((tried as i64) + ntried * AF_MAGIC_2048) as *u8
574 var ze: i64 = 0
575 while ze < fl3 { tp2[ze] = fix3[ze]; ze = ze + 1 }
576 tp2[fl3] = 0 as u8
577 triedl[ntried] = fl3
578 ntried = ntried + 1
579 let ap3: i64 = apply_fix(sbp, buggy, fix3)
580 if ap3 > 0 {
581 n1 = grader_run(cname, out, AF_MAGIC_1048576)
582 let nm5: *u8 = sys_mmap(64)
583 let loc5: i64 = fn_failing(out, n1, nm5)
584 w(" [5 VERIFY-S" as *u8); wn(s + 1); w("] grader re-run -> " as *u8)
585 if loc5 == 0 { green = 1; w("ALL FUNCTIONS PASS => SAMPLED FIX CONFIRMED (best-of-N, machine-verified)\n" as *u8) }
586 else { w("still failing -> restored; next sample\n" as *u8); copyfile(bak2, sbp) }
587 } else {
588 w(" [4 SAMPLE-" as *u8); wn(s + 1); w("] splice failed -> restored, skipped\n" as *u8)
589 copyfile(bak2, sbp)
590 }
591 }
592 }
593 s = s + 1
594 }
595 if green == 0 { w(" [4 BEST-OF-N] sampled retries exhausted -> MAKER-MISS (all attempts banked)\n" as *u8) }
596 }
597 }
598 // 5b SYMJUDGE (2026-07-20, autonomous-builder lane): the SYMBOLIC JUDGE -- a tests-green fix
599 // must ALSO hold the function's property contract (knowledge/store/symprop- plane row) over a
600 // deterministic domain sweep (exhaustive when the domain fits the row's budget). Kills the
601 // overfit-patch class the baked tests cannot see (fix passes tests, wrong elsewhere) and the
602 // crash class (SIGFPE/hang-free bounded sweep). Fn with NO contract row -> stage honestly
603 // SKIPPED (tests-only verdict stands, ledger symj=SKIP) so uncontracted lanes are unchanged.
604 var symj: i64 = 2
605 if green == 1 {
606 let sjav: *i64 = sys_mmap(64) as *i64
607 sjav[0] = "_offc/nx_symjudge.elf" as *u8 as i64
608 sjav[1] = cpath as i64
609 sjav[2] = name as i64
610 sjav[3] = "knowledge/store/symprop-" as *u8 as i64
611 sjav[4] = "nx_symj_af" as *u8 as i64
612 sjav[5] = 0
613 let sjb: *u8 = sys_mmap(AF_MAGIC_65536)
614 let sjl: *i64 = sys_mmap(8) as *i64
615 tr_run_capture("_offc/nx_symjudge.elf" as *u8, sjav, sjb, AF_MAGIC_65500, sjl)
616 let sn: i64 = sjl[0]
617 var sgreen: i64 = 0
618 var sskip: i64 = 0
619 if find_first(sjb, sn, " verdict=GREEN" as *u8, 0) >= 0 { sgreen = 1 }
620 if find_first(sjb, sn, "REFUSED row-missing" as *u8, 0) >= 0 { sskip = 1 }
621 if find_first(sjb, sn, "REFUSED plane-empty" as *u8, 0) >= 0 { sskip = 1 }
622 if sgreen == 1 { symj = 1; w(" [5b SYMJUDGE] property contract HOLDS over the sweep => fix stands\n" as *u8) }
623 if sgreen == 0 { if sskip == 1 { w(" [5b SYMJUDGE] no property contract for this fn -> stage SKIPPED (tests-only verdict)\n" as *u8) } }
624 if sgreen == 0 { if sskip == 0 {
625 symj = 0
626 green = 0
627 w(" [5b SYMJUDGE] tests-green fix VIOLATES the property contract (overfit/crash class) => DEMOTED to MISS:\n " as *u8)
628 let vat: i64 = find_first(sjb, sn, "SYMJ" as *u8, 0)
629 if vat >= 0 {
630 var vee: i64 = vat
631 var gvs: i64 = 1
632 while gvs == 1 { if vee >= sn { gvs = 0 } else { if sjb[vee] == (10 as u8) { gvs = 0 } else { vee = vee + 1 } } }
633 sys_write(1, ((sjb as i64) + vat) as *u8, vee - vat)
634 w("\n" as *u8)
635 }
636 } }
637 }
638 // bank the winning line as the next episode's REPLAY null control (a foreign fix that must not
639 // satisfy a different instance's judge)
640 if green == 1 {
641 if g_af_prior_addr == 0 { g_af_prior_addr = sys_mmap(AF_MAGIC_2048) as i64 }
642 let priorbuf: *u8 = g_af_prior_addr as *u8
643 extract_fn_line(sbp, name, priorbuf)
644 }
645 // 6 REVERT + prove the original failing function returns
646 copyfile(bak2, sbp)
647 let n2: i64 = grader_run(cname, out, AF_MAGIC_1048576)
648 let nm3: *u8 = sys_mmap(64)
649 let loc2: i64 = fn_failing(out, n2, nm3)
650 var revert_ok: i64 = 0
651 if loc2 == 1 { var zz2: i64 = 0; revert_ok = 1; var gg: i64 = 1; while gg == 1 { if name[zz2] != nm3[zz2] { revert_ok = 0; gg = 0 } else { if name[zz2] == (0 as u8) { gg = 0 } else { zz2 = zz2 + 1 } } } }
652 w(" [6 REVERT] restored; failing function is again: " as *u8)
653 if loc2 == 1 { w(nm3) } else { w("(none?)" as *u8) }
654 if revert_ok == 1 { w(" => revert proven exact\n" as *u8) } else { w(" => REVERT MISMATCH\n" as *u8) }
655 // M0 AD7 witness: the TREE copy is byte-identical to the episode snapshot (never opened for write)
656 let tree_ok: i64 = afl_bytes_eq(cp2, bak2)
657 w(" [6 TREE] tree_untouched=" as *u8); wn(tree_ok); w("\n" as *u8)
658 // episode ledger row
659 let erow: *u8 = sys_mmap(512)
660 var eo: i64 = 0
661 eo = bcat(erow, eo, "AUTOFIX-AUTO ts=" as *u8)
662 eo = bcatn(erow, eo, ts)
663 eo = bcat(erow, eo, " cand=" as *u8)
664 eo = bcat(erow, eo, cname)
665 eo = bcat(erow, eo, " located=" as *u8)
666 eo = bcat(erow, eo, name)
667 eo = bcat(erow, eo, " attempts=" as *u8)
668 eo = bcatn(erow, eo, attempts)
669 eo = bcat(erow, eo, " maker=" as *u8)
670 if green == 1 { eo = bcat(erow, eo, "GREEN" as *u8) } else { eo = bcat(erow, eo, "MISS" as *u8) }
671 eo = bcat(erow, eo, " revert=" as *u8)
672 eo = bcatn(erow, eo, revert_ok)
673 eo = bcat(erow, eo, " symj=" as *u8)
674 if symj == 1 { eo = bcat(erow, eo, "GREEN" as *u8) }
675 if symj == 0 { eo = bcat(erow, eo, "RED" as *u8) }
676 if symj == 2 { eo = bcat(erow, eo, "SKIP" as *u8) }
677 eo = bcat(erow, eo, " gc=" as *u8)
678 eo = bcatn(erow, eo, g_af_gc)
679 eo = bcat(erow, eo, " tree=" as *u8)
680 eo = bcatn(erow, eo, tree_ok)
681 if g_af_harness_addr != 0 {
682 eo = bcat(erow, eo, " harness=" as *u8)
683 eo = bcat(erow, eo, g_af_harness_addr as *u8)
684 }
685 erow[eo] = 10 as u8
686 eo = eo + 1
687 // M0 AD5 (af_episode_ledger): the DEFAULT ledger is the estate journal, appended AS DECIDED and
688 // resolved by name (never a home-directory literal); an argv override keeps the testing lane.
689 if g_af_led_default == 1 {
690 let lwhere: *u8 = sys_mmap(4096)
691 af_episode_ledger(erow, eo, lwhere)
692 w(" [LEDGER] row appended: " as *u8); w(lwhere); w("\n" as *u8)
693 } else { append_line(ledp, erow, eo) }
694 g_af_sb_addr = 0
695 afl_sandbox_reap(sb, cname)
696 if green == 1 {
697 let crow: *u8 = sys_mmap(256)
698 var co: i64 = 0
699 co = bcat(crow, co, "CGB ts=" as *u8)
700 co = bcatn(crow, co, ts)
701 co = bcat(crow, co, " generator=15b-autofix-auto tasks=1 localpass=1\n" as *u8)
702 append_line("/home/elderwesto/nx_stage/codegen_bench_ledger.log" as *u8, crow, co)
703 }
704 if revert_ok == 0 { return 0 - 1 }
705 return green
706}
707
708func main(argc: i64, argv: *i64) -> i64 {
709 w("=== NX-AUTOFIX-AUTO -- SELF-LOCALIZING fix loop: discover the failing function, generate, verify ===\n" as *u8)
710 let ts: i64 = sys_now_realtime_sec()
711 // M0 AD3 (af_intake_board): `nx_autofix_auto intake` folds the estate's own boards into the task
712 // plane and exits -- no model load, no episodes, safe on any host.
713 if argc >= 2 { if afl_streq(argv[1] as *u8, "intake" as *u8) == 1 {
714 let op: *u8 = sys_mmap(4096)
715 if afl_status_dir(op) == 1 { afl_cat(op, afl_slen(op), "autodev_intake.jrnl" as *u8) } else { afl_cat(op, 0, "/tmp/autodev_intake.jrnl" as *u8) }
716 let tot: i64 = af_intake_board(op)
717 w("NX-AUTOFIX-AUTO af_intake_board plane=" as *u8)
718 w(op)
719 w("\n" as *u8)
720 if tot < 0 { return 1 }
721 return 0
722 } }
723 var mpath: *u8 = "/home/elderwesto/nx_stage/nx_15b_model.gguf" as *u8
724 if argc >= 2 { mpath = argv[1] as *u8 }
725 // argv[3] = ledger override (testing lane): episode rows land in a scratch ledger, never the
726 // official one, so a validation run cannot become the newest batch the gate/pulse read.
727 var ledp: *u8 = "/home/elderwesto/nx_stage/autofix_ledger.log" as *u8
728 if argc >= 4 { ledp = argv[3] as *u8 }
729 // argv[4] = prompt mode: "strict" -> append the NishiLang-syntax rule (general coders); anything
730 // else (default "clean") -> no rule (the tuned 1.5B is regressed by it). Per-maker, set by the caller.
731 var strict: i64 = 0
732 if argc >= 5 { let sc: *u8 = argv[4] as *u8; if sc[0] == (115 as u8) { strict = 1 } }
733 // argv[5] = decode mode: "gc" -> NB10 grammar-constrained decode (token mask at sample time).
734 if argc >= 6 { let gm: *u8 = argv[5] as *u8; if gm[0] == (103 as u8) { g_af_gc = 1 } }
735 // M0: bounds and sampler shape from knowledge/autofix.conf (rule 17); the defaults are the July
736 // contract values, each with its reason beside it in the lib.
737 g_bon_n = afl_conf_int(AFL_KEY_BON_N, AF_BON_N)
738 g_bon_temp = afl_conf_int(AFL_KEY_BON_TEMP, AF_BON_TEMP_PM)
739 g_bon_topp = afl_conf_int(AFL_KEY_BON_TOPP, AF_BON_TOPP_PM)
740 g_bon_topk = afl_conf_int(AFL_KEY_BON_TOPK, AF_BON_TOPK)
741 g_af_wall_ms = afl_conf_int(AFL_KEY_WALL, AFL_WALL_MS_DEFAULT)
742 g_af_as_bytes = afl_conf_int(AFL_KEY_AS, AFL_AS_BYTES_DEFAULT)
743 g_af_null_max = afl_conf_int(AFL_KEY_NULL_MAX, AFL_NULL_MAX_DEFAULT)
744 var led_default: i64 = 1
745 if argc >= 4 { led_default = 0 }
746 g_af_led_default = led_default
747 // M0 AD2 (the loop's harness token): a 16-hex config identity over the model PATH, prompt mode,
748 // decode mode and sampler shape; travels on every episode row and into the published envelope.
749 g_af_harness_addr = af_harness_calc(mpath, strict, g_af_gc)
750 let irc: i64 = nsv_init_i8(mpath)
751 if irc != 0 { w("model init failed -> cannot run maker episodes\n" as *u8); return 1 }
752 let out: *u8 = sys_mmap(AF_MAGIC_1048576)
753
754 // MANIFEST MODE (argv[2] = verified manifest from nx_autofix_intake_gate; rows `name|path`):
755 // the episode list becomes DATA -- any lane's verified instances run through the same loop.
756 if argc >= 3 {
757 let manp: *u8 = argv[2] as *u8
758 let mlb: *i64 = sys_mmap(8) as *i64
759 let man: *u8 = sys_read_file(manp, mlb)
760 if (man as i64) == 0 { w("cannot read manifest\n" as *u8); return 1 }
761 let mn: i64 = mlb[0]
762 var greens: i64 = 0
763 var misses: i64 = 0
764 var unsound: i64 = 0
765 var data_only: i64 = 0
766 var refused: i64 = 0
767 var total: i64 = 0
768 var p: i64 = 0
769 while p < mn {
770 var e: i64 = p
771 var f: i64 = 0
772 while f == 0 { if e >= mn { f = 1 } else { if man[e] == (10 as u8) { f = 1 } else { e = e + 1 } } }
773 var isrow: i64 = 0
774 if e > p { if man[p] != (35 as u8) { isrow = 1 } }
775 if isrow == 1 {
776 let cname: *u8 = sys_mmap(96)
777 let cpath: *u8 = sys_mmap(160)
778 let bakp: *u8 = sys_mmap(176)
779 // locate the pipe with a flag-exit scan (a negated index does not leave
780 // `while b < e` -- the `b = 0 - b` pseudo-break spins forever), then copy halves.
781 var pipe: i64 = 0 - 1
782 var b: i64 = p
783 var bd: i64 = 0
784 while bd == 0 { if b >= e { bd = 1 } else { if man[b] == (124 as u8) { pipe = b; bd = 1 } else { b = b + 1 } } }
785 var nl: i64 = 0
786 var pl: i64 = 0
787 if pipe > p {
788 var x: i64 = p
789 while x < pipe { if nl < 90 { cname[nl] = man[x]; nl = nl + 1 } x = x + 1 }
790 var y: i64 = pipe + 1
791 while y < e { if pl < 150 { cpath[pl] = man[y]; pl = pl + 1 } y = y + 1 }
792 }
793 cname[nl] = 0 as u8
794 cpath[pl] = 0 as u8
795 if nl > 0 { if pl > 0 {
796 // M0 AD6 (af_admit_untrusted): deny by default -- an external row is DATA ONLY
797 // when provenance-pinned AND maintainer-merged, refused BY NAME otherwise; only
798 // LOCAL rows ever run an episode.
799 let arow: *u8 = ((man as i64) + p) as *u8
800 let an6: *u8 = sys_mmap(64)
801 let ap6: *u8 = sys_mmap(4096)
802 let av6: *u8 = sys_mmap(128)
803 let adm: i64 = af_admit_untrusted(arow, e - p, an6, ap6, av6)
804 w(" [ADMIT] " as *u8); w(cname); w(" -> " as *u8); w(afl_admit_name(adm)); w("\n" as *u8)
805 if adm == AFL_ADMIT_DATA_ONLY { data_only = data_only + 1 }
806 if adm < 0 { refused = refused + 1 }
807 if adm == AFL_ADMIT_LOCAL {
808 var z: i64 = 0
809 while z < pl { bakp[z] = cpath[z]; z = z + 1 }
810 bakp[pl] = 46 as u8
811 bakp[pl+1] = 98 as u8
812 bakp[pl+2] = 97 as u8
813 bakp[pl+3] = 107 as u8
814 bakp[pl+4] = 51 as u8
815 bakp[pl+5] = 0 as u8
816 total = total + 1
817 let g: i64 = episode(cname, cpath, bakp, ts, out, ledp, strict)
818 if g == 1 { greens = greens + 1 }
819 if g == 0 { misses = misses + 1 }
820 if g == (0 - 1) { unsound = unsound + 1 }
821 }
822 } }
823 }
824 p = e + 1
825 }
826 w("\nNX-AUTOFIX-AUTO manifest sweep: " as *u8)
827 wn(total); w(" instances | " as *u8)
828 wn(greens); w(" GREEN | " as *u8)
829 wn(misses); w(" MISS | " as *u8)
830 wn(unsound); w(" UNSOUND | " as *u8); wn(data_only); w(" DATA-ONLY | " as *u8); wn(refused); w(" REFUSED (resolve rate " as *u8)
831 if total > 0 { wn((greens * 100) / total) } else { wn(0) }
832 w("% under the SWE-bench contract; every green machine-verified, zero Claude content)\n" as *u8)
833 if unsound > 0 { return 1 }
834 return 0
835 }
836
837 let g1: i64 = episode("nx_autofix_candidate2" as *u8, "runtime/nx_autofix_candidate2.nx" as *u8, "runtime/nx_autofix_candidate2.nx.bak3" as *u8, ts, out, ledp, strict)
838 let g2: i64 = episode("nx_autofix_candidate3" as *u8, "runtime/nx_autofix_candidate3.nx" as *u8, "runtime/nx_autofix_candidate3.nx.bak3" as *u8, ts, out, ledp, strict)
839 let g3: i64 = episode("nx_autofix_candidate4" as *u8, "runtime/nx_autofix_candidate4.nx" as *u8, "runtime/nx_autofix_candidate4.nx.bak3" as *u8, ts, out, ledp, strict)
840 let g4: i64 = episode("nx_autofix_candidate5" as *u8, "runtime/nx_autofix_candidate5.nx" as *u8, "runtime/nx_autofix_candidate5.nx.bak3" as *u8, ts, out, ledp, strict)
841 w("\nNX-AUTOFIX-AUTO episodes: cand2(sgn)=" as *u8)
842 if g1 == 1 { w("GREEN" as *u8) } else { if g1 == 0 { w("MISS" as *u8) } else { w("UNSOUND" as *u8) } }
843 w(" cand3(dbl)=" as *u8)
844 if g2 == 1 { w("GREEN" as *u8) } else { if g2 == 0 { w("MISS" as *u8) } else { w("UNSOUND" as *u8) } }
845 w(" cand4(absdiff+FNCASE-feedback)=" as *u8)
846 if g3 == 1 { w("GREEN" as *u8) } else { if g3 == 0 { w("MISS" as *u8) } else { w("UNSOUND" as *u8) } }
847 w(" cand5(WILD:model-authored sum100)=" as *u8)
848 if g4 == 1 { w("GREEN" as *u8) } else { if g4 == 0 { w("MISS" as *u8) } else { w("UNSOUND" as *u8) } }
849 w(" (localization DISCOVERED per-episode from FNRES rows; greens are machine-verified, zero Claude content)\n" as *u8)
850 if g1 == (0 - 1) { return 1 }
851 if g2 == (0 - 1) { return 1 }
852 if g3 == (0 - 1) { return 1 }
853 if g4 == (0 - 1) { return 1 }
854 return 0
855}
856