nx_forge_engine.nx source
↩ module page · 140 lines · 6058 B
1// nx_forge_engine.nx -- THE ASSEMBLED AUTONOMOUS-CODING ENGINE v1 (pure core; CLI =
2// nx_forge_engine_run, gate = nx_forge_engine_gate). One invocation turns a task spec into
3// judged NishiLang code using every proven lever, in measured-lift order:
4// STAGE 1 GREEDY + REPAIR: the gated A1 closed loop (nx_forge_loop) -- deterministic
5// generation, byte-exact judge, verbatim-stderr re-prompt, bounded cycles.
6// STAGE 2 BEST-OF-N SAMPLED (the Large-Language-Monkeys / dossier-N3 lever): if the loop
7// exhausts, fan N independent sampled candidates ("temp">0, DETERMINISTIC per-sample
8// seeds FE_SEED_BASE+k so engine runs are reproducible) against the FREE byte-exact
9// verifier -- first GREEN wins. No repair ctx between samples (independence).
10// Relationship to the sibling nx_forge_bestof (2026-07-16): that organ is the IN-PROCESS 1.5B
11// bench arm (imports serve core, prefix-cache); THIS is the API-native engine stage over HTTP
12// /gen per the operator's no-one-off-shell directive -- the serve already parses temp/top_p/
13// top_k/seed (gp[8..11]) so sampling needs zero serve surgery.
14// rc: 1..7 GREEN at loop cycle N · 100+k GREEN at sample k · -40 both stages exhausted ·
15// hard errors pass through from the loop (-2/-3/-4/-22/-30/-31/-33/-34).
16// Engine clients do NOT claim the model lane -- the DAEMON launcher owns the RAM claim (M6 law).
17// license_tier: ORIGINAL
18import "nx_forge_loop.nx"
19const FE_MAGIC_8192: i64 = 8192
20const FE_MAGIC_65536: i64 = 65536
21const FE_MAGIC_32768: i64 = 32768
22const FE_MAGIC_131072: i64 = 131072
23
24const FE_SEED_BASE: i64 = 11000 // reproducibility anchor: sample k posts seed BASE+k
25
26// append the engine's own ledger row (fg idiom; distinct from the loop's row by rc>=100/-40).
27func fe_ledger(organ: *u8, tried: i64, rc: i64) -> i64 {
28 let lfd: i64 = sys_openat_append("knowledge/forge/forge_ledger.txt" as *u8, 420)
29 if lfd < 0 { return 0 - 1 }
30 fc_ws(lfd, "FORGE|" as *u8)
31 fc_ws(lfd, organ)
32 fc_ws(lfd, "|cycles=" as *u8)
33 fg_wdec(lfd, tried)
34 fc_ws(lfd, "|verdict=" as *u8)
35 if rc > 0 { fc_ws(lfd, "GREEN" as *u8) }
36 if rc <= 0 { fc_ws(lfd, "RED" as *u8) }
37 fc_ws(lfd, "|rc=" as *u8)
38 fg_wdec(lfd, rc)
39 fc_ws(lfd, "|us=" as *u8)
40 let now: i64 = sys_now_us()
41 fg_wdec(lfd, now)
42 fc_ws(lfd, "\n" as *u8)
43 sys_close(lfd)
44 return 0
45}
46
47// the engine: loop first, then sampled best-of-N, first GREEN wins.
48func fe_run(organ: *u8, taskpath: *u8, expectpath: *u8, packpath: *u8, port: i64, maxc: i64, maxnew: i64, chat: i64, nsamples: i64, temp_pm: i64) -> i64 {
49 let ctxpath: *u8 = "/tmp/forge_loop_ctx.txt" as *u8
50 let bldout: *u8 = "/tmp/forge_loop_build.out" as *u8
51 let runout: *u8 = "/tmp/forge_loop_run.out" as *u8
52 let nullp: *u8 = 0 as *u8
53 std_putln("FORGE-ENGINE stage1 greedy+repair" as *u8)
54 let r1: i64 = flp_run(organ, taskpath, expectpath, packpath, port, maxc, maxnew, chat)
55 if r1 > 0 {
56 fe_ledger(organ, r1, r1)
57 std_putln("FORGE-ENGINE GREEN at loop stage" as *u8)
58 return r1
59 }
60 if r1 + 21 != 0 {
61 fe_ledger(organ, 0, r1)
62 std_putln("FORGE-ENGINE hard-stop from loop stage" as *u8)
63 return r1
64 }
65 if nsamples <= 0 {
66 fe_ledger(organ, maxc, 0 - 40)
67 std_putln("FORGE-ENGINE exhausted (no sampling stage requested)" as *u8)
68 return 0 - 40
69 }
70 std_putln("FORGE-ENGINE stage2 best-of-N sampled (first GREEN wins)" as *u8)
71 // sample-stage buffers + param block (flp_run prologue lineage; bp slots per nx_forge_loop)
72 let taskb: *u8 = sys_mmap(FE_MAGIC_8192) as *u8
73 let ebuf: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
74 let prompt: *u8 = sys_mmap(FE_MAGIC_32768) as *u8
75 let body: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
76 let resp: *u8 = sys_mmap(FE_MAGIC_131072) as *u8
77 let cand: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
78 let cand2: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
79 let srcpath: *u8 = sys_mmap(256) as *u8
80 let elfpath: *u8 = sys_mmap(256) as *u8
81 let av: *i64 = sys_mmap(64) as *i64
82 let envp: *i64 = sys_mmap(32) as *i64
83 let st: *i64 = sys_mmap(64) as *i64
84 let tmpb: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
85 let rbuf: *u8 = sys_mmap(FE_MAGIC_65536) as *u8
86 let bp: *i64 = sys_mmap(256) as *i64
87 let tn: i64 = fc_read(taskpath, taskb, FE_MAGIC_8192)
88 if tn <= 0 { fe_ledger(organ, 0, 0 - 2); return 0 - 2 }
89 let en: i64 = fc_read(expectpath, ebuf, FE_MAGIC_65536)
90 if en <= 0 { fe_ledger(organ, 0, 0 - 4); return 0 - 4 }
91 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
92 envp[1] = 0
93 fg_mkpath("runtime/" as *u8, organ, ".nx" as *u8, srcpath)
94 fg_mkpath("/tmp/" as *u8, organ, ".sov.elf" as *u8, elfpath)
95 bp[0] = organ as i64
96 bp[1] = taskb as i64
97 bp[2] = packpath as i64
98 bp[3] = nullp as i64
99 bp[4] = port
100 bp[5] = maxnew
101 bp[6] = chat
102 bp[7] = ebuf as i64
103 bp[8] = en
104 bp[9] = prompt as i64
105 bp[10] = body as i64
106 bp[11] = resp as i64
107 bp[12] = cand as i64
108 bp[13] = cand2 as i64
109 bp[14] = srcpath as i64
110 bp[15] = elfpath as i64
111 bp[16] = av as i64
112 bp[17] = envp as i64
113 bp[18] = st as i64
114 bp[19] = tmpb as i64
115 bp[20] = rbuf as i64
116 bp[21] = ctxpath as i64
117 bp[22] = bldout as i64
118 bp[23] = runout as i64
119 bp[24] = temp_pm
120 bp[25] = 0
121 var k: i64 = 1
122 var verdict: i64 = 0 - 40
123 var done: i64 = 0
124 while done == 0 {
125 if k > nsamples { done = 1 }
126 if done == 0 {
127 std_puts("FORGE-ENGINE sample=" as *u8)
128 std_pdec(k)
129 bp[25] = FE_SEED_BASE + k
130 let r: i64 = flp_cycle(bp)
131 if r == 1 { verdict = 100 + k; done = 1 }
132 if r < 0 { verdict = r; done = 1 }
133 if r == 0 { k = k + 1 }
134 }
135 }
136 fe_ledger(organ, k, verdict)
137 if verdict > 0 { std_putln("FORGE-ENGINE GREEN at sample stage" as *u8) }
138 if verdict <= 0 { std_putln("FORGE-ENGINE RED (both stages exhausted or hard error)" as *u8) }
139 return verdict
140}