nx_forge_local_gen.nx source
↩ module page · 241 lines · 8737 B
1// nx_forge_local_gen.nx -- L4 generation phase: the LOCAL Coder-0.5B (integer no-float tower)
2// writes fbench candidates. nsv_init(coder) ONCE, then per task: prompt = mini-pack + task
3// instruction -> nsv_generate (mode 0 i32 greedy) -> fence-strip -> write candidate file
4// IMMEDIATELY (so a mid-run reap preserves finished tasks). Prints per-task nprompt/ngen: an
5// nprompt > 384 also PROVES the L2 capacity raise (the pack prompt could not fit the old cap).
6// Phase 2 = the existing nx_forge driver (fg_run) judges these candidate files byte-exact.
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_nofloat_serve_core.nx"
9import "nx_forge_ctx.nx"
10const K_MAGIC_32768: i64 = 32768
11const K_MAGIC_16384: i64 = 16384
12const K_MAGIC_30000: i64 = 30000
13
14// strip a leading ```lang fence + trailing ```; idempotent on unfenced. writes into out, ret len.
15func lg_strip(src: *u8, n: i64, out: *u8) -> i64 {
16 var f0: i64 = 0 - 1
17 var i: i64 = 0
18 let stop: i64 = n - 3
19 while i <= stop {
20 let c0: i64 = src[i] as i64
21 let c1: i64 = src[i + 1] as i64
22 let c2: i64 = src[i + 2] as i64
23 if c0 == 96 && c1 == 96 && c2 == 96 { f0 = i; i = stop + 1 }
24 if f0 < 0 { i = i + 1 }
25 }
26 if f0 < 0 {
27 var k: i64 = 0
28 while k < n { out[k] = src[k]; k = k + 1 }
29 out[n] = 0 as u8
30 return n
31 }
32 var cs: i64 = f0 + 3
33 var sc: i64 = 1
34 while sc == 1 {
35 if cs >= n { sc = 0 }
36 if sc == 1 {
37 let c: i64 = src[cs] as i64
38 cs = cs + 1
39 if c == 10 { sc = 0 }
40 }
41 }
42 var f1: i64 = n
43 var j: i64 = cs
44 let stop2: i64 = n - 3
45 while j <= stop2 {
46 let d0: i64 = src[j] as i64
47 let d1: i64 = src[j + 1] as i64
48 let d2: i64 = src[j + 2] as i64
49 if d0 == 96 && d1 == 96 && d2 == 96 { f1 = j }
50 j = j + 1
51 }
52 var o: i64 = 0
53 var p: i64 = cs
54 while p < f1 { out[o] = src[p]; o = o + 1; p = p + 1 }
55 out[o] = 0 as u8
56 return o
57}
58
59// build prompt = COMPACT crib (~130 tok; the 1300-tok mini-pack is impractical at per-token
60// prefill ~2.4 tok/s -> ~9min/task, the N6 speed wall) + task instruction into buf. return length
61func lg_prompt(taskinstr: *u8, buf: *u8, cap: i64) -> i64 {
62 let crib: *u8 = "NishiLang, NOT C/Rust/Go. Skeleton:\nimport \"nx_syscalls.nx\"\nimport \"nx_lib_std.nx\"\nfunc main(argc: i64, argv: *i64) -> i64 { sys_exit(0) return 0 }\nUse std_puts(\"x\" as *u8), std_pdec(n); let/var: i64; while; if/else; + - * / %. One stmt per line. Output ONLY source." as *u8
63 var o: i64 = 0
64 var j: i64 = 0
65 while crib[j] != (0 as u8) {
66 buf[o] = crib[j]
67 o = o + 1
68 j = j + 1
69 }
70 var i: i64 = 0
71 while taskinstr[i] != (0 as u8) {
72 if o >= cap - 1 { return 0 - 1 }
73 buf[o] = taskinstr[i]
74 o = o + 1
75 i = i + 1
76 }
77 buf[o] = 0 as u8
78 return o
79}
80
81// L4 MINIPACK arm (2026-07-15): prompt = the VERIFIED mini-pack file (3911B, emitted by
82// nx_forge_ctx fc_emit_mini, fits the raised serve window) + task. The pilot proved pack context
83// lifts 0/3 -> 3/3 on API tiers; this arm measures the LOCAL tier with the same lever.
84func lg_read_pack(buf: *u8, cap: i64) -> i64 {
85 let fd: i64 = sys_openat_rd("knowledge/forge/nishilang_pack_mini_v1.txt" as *u8)
86 if fd < 0 { return 0 - 1 }
87 var o: i64 = 0
88 var reading: i64 = 1
89 while reading == 1 {
90 let q: *u8 = buf + o
91 let rem: i64 = cap - 1 - o
92 if rem <= 0 { reading = 0 } else {
93 let r: i64 = sys_read(fd, q, rem)
94 if r <= 0 { reading = 0 } else { o = o + r }
95 }
96 }
97 sys_close(fd)
98 buf[o] = 0 as u8
99 if o < 1000 { return 0 - 1 }
100 return o
101}
102
103func lg_prompt_mp(taskinstr: *u8, buf: *u8, cap: i64) -> i64 {
104 var o: i64 = lg_read_pack(buf, cap)
105 if o <= 0 { return 0 - 1 }
106 var i: i64 = 0
107 while taskinstr[i] != (0 as u8) {
108 if o >= cap - 1 { return 0 - 1 }
109 buf[o] = taskinstr[i]
110 o = o + 1
111 i = i + 1
112 }
113 // the crib arm's output contract, verbatim -- without it the 0.5B echoes the expected OUTPUT
114 // line instead of authoring a program (measured 2026-07-15: all 3 tasks output-shaped echoes)
115 let contract: *u8 = "Write a COMPLETE NishiLang program. One stmt per line. Output ONLY source.\n" as *u8
116 var j: i64 = 0
117 while contract[j] != (0 as u8) {
118 if o >= cap - 1 { return 0 - 1 }
119 buf[o] = contract[j]
120 o = o + 1
121 j = j + 1
122 }
123 buf[o] = 0 as u8
124 return o
125}
126
127// one task: generate + strip + write candidate; single fixed callee (nsv_generate). ret ngen.
128func lg_task(taskinstr: *u8, candpath: *u8, tnum: i64, mp: i64, maxnew: i64) -> i64 {
129 let pbuf: *u8 = sys_mmap(K_MAGIC_32768) as *u8
130 var plen: i64 = 0
131 if mp == 1 { plen = lg_prompt_mp(taskinstr, pbuf, K_MAGIC_32768) } else { plen = lg_prompt(taskinstr, pbuf, K_MAGIC_32768) }
132 if plen <= 0 { return 0 - 1 }
133 let outb: *u8 = sys_mmap(K_MAGIC_16384) as *u8
134 let meta: *i64 = sys_mmap(64) as *i64
135 let gp: *i64 = sys_mmap(128) as *i64
136 gp[0] = pbuf as i64
137 gp[1] = plen
138 gp[2] = maxnew
139 gp[3] = 0
140 gp[4] = outb as i64
141 gp[5] = K_MAGIC_16384
142 gp[6] = meta as i64
143 let neg: i64 = 0 - 1
144 gp[7] = neg
145 gp[8] = 0
146 gp[9] = 0
147 gp[10] = 0
148 gp[11] = 1
149 gp[12] = 1
150 // PREFIX-KV-CACHE: task 1 prefills the full prompt + snapshots it; tasks 2+ reuse the shared crib/header
151 // prefix (boundary-safe common-prefix match) and prefill only their divergent suffix -> ~crib fewer matmuls/task.
152 var usnap: i64 = 1
153 if tnum == 1 { usnap = 0 }
154 let tl: i64 = nsv_generate_pfx(gp, K_MAGIC_30000, usnap)
155 std_puts("LOCAL T" as *u8)
156 std_pdec(tnum)
157 std_puts(" pbytes=" as *u8)
158 std_pdec(plen)
159 std_puts(" nprompt=" as *u8)
160 std_pdec(meta[0])
161 std_puts(" ngen=" as *u8)
162 std_pdec(meta[1])
163 std_puts(" err=" as *u8)
164 std_pdec(meta[5])
165 std_puts(" textlen=" as *u8)
166 std_pdec(tl)
167 std_puts(" ms=" as *u8)
168 std_pdec(meta[2])
169 let cand: *u8 = sys_mmap(K_MAGIC_16384) as *u8
170 var cl: i64 = 0
171 if tl > 0 { cl = lg_strip(outb, tl, cand) }
172 let fd: i64 = sys_openat_wr(candpath, 420)
173 if fd >= 0 {
174 if cl > 0 {
175 var off: i64 = 0
176 while off < cl {
177 let q: *u8 = cand + off
178 let rem: i64 = cl - off
179 let w: i64 = sys_write(fd, q, rem)
180 if w <= 0 { off = cl }
181 if w > 0 { off = off + w }
182 }
183 }
184 sys_close(fd)
185 }
186 std_puts(" wrote=" as *u8)
187 std_pdec(cl)
188 std_puts("\n" as *u8)
189 return tl
190}
191
192func main(argc: i64, argv: *i64) -> i64 {
193 var mp: i64 = 0
194 if argc >= 2 {
195 let a1: i64 = argv[1]
196 let arg1: *u8 = a1 as *u8
197 if std_streq(arg1, "minipack" as *u8) == 1 { mp = 1 }
198 }
199 var maxnew: i64 = 150
200 if mp == 1 { maxnew = 400 }
201 // argv[2] = optional model gguf path (default = Coder-0.5B) -- the 1.5B tier runs the SAME arm
202 var mpath: *u8 = "/home/elderwesto/nx_stage/nx_coder_model.gguf\x00" as *u8
203 if argc >= 3 {
204 let a2: i64 = argv[2]
205 mpath = a2 as *u8
206 }
207 let ir: i64 = nsv_init(mpath)
208 std_puts("LOCAL-GEN init rc=" as *u8)
209 std_pdec(ir)
210 std_puts(" arm=" as *u8)
211 std_pdec(mp)
212 std_puts(" (0=crib 1=minipack) maxnew=" as *u8)
213 std_pdec(maxnew)
214 std_puts("\n" as *u8)
215 if ir != 0 {
216 std_putln("LOCAL-GEN init FAIL" as *u8)
217 sys_exit(1)
218 return 1
219 }
220 let t1: *u8 = "\nTASK: print EXACTLY this line then exit 0:\nFBENCH-T1 fib10=55 sum100=5050\n(fib(10), fib(1)=fib(2)=1; sum 1..100.)\n" as *u8
221 if mp == 1 {
222 lg_task(t1, "knowledge/forge/fix/cand_mpT1.nx" as *u8, 1, mp, maxnew)
223 } else {
224 lg_task(t1, "knowledge/forge/fix/cand_localT1.nx" as *u8, 1, mp, maxnew)
225 }
226 let t2: *u8 = "\nTASK: print EXACTLY this line then exit 0:\nFBENCH-T2 rev=egrof ngierevos vowels=6\n(reverse \"sovereign forge\"; count vowels aeiou.)\n" as *u8
227 if mp == 1 {
228 lg_task(t2, "knowledge/forge/fix/cand_mpT2.nx" as *u8, 2, mp, maxnew)
229 } else {
230 lg_task(t2, "knowledge/forge/fix/cand_localT2.nx" as *u8, 2, mp, maxnew)
231 }
232 let t3: *u8 = "\nTASK: print EXACTLY this line then exit 0:\nFBENCH-T3 sum=42 bad=1\n(parse \"k1=7;k2=21;bad;k3=14\" split by ;; valid if has =, value=int after =; sum valid, count invalid.)\n" as *u8
233 if mp == 1 {
234 lg_task(t3, "knowledge/forge/fix/cand_mpT3.nx" as *u8, 3, mp, maxnew)
235 } else {
236 lg_task(t3, "knowledge/forge/fix/cand_localT3.nx" as *u8, 3, mp, maxnew)
237 }
238 std_putln("LOCAL-GEN done" as *u8)
239 sys_exit(0)
240 return 0
241}