nx_pe_container_elderai.nx source
↩ module page · 233 lines · 15437 B
1// nx_pe_container_elderai.nx -- THE SOURCE for the Elara/Elder-AI GPU engine container.
2//
3// WHY THIS FILE EXISTS: nx_pe_container_elderai.exe was shipping as a binary with NO
4// SOURCE IN THE TREE -- it had been built from an edited copy of the sdserver variant
5// and renamed, so nothing could rebuild it and every fix to it was a hand-patch that
6// the next build would lose. A binary nothing can emit was never really ours. This is
7// that source, recovered by reading the deployed PE's own baked cmdline (2026-07-25).
8//
9// It differs from nx_pe_container_sdserver.nx in exactly two arguments:
10// --listen-ip 0.0.0.0 so the NAS gen orchestrator can reach this laptop worker
11// --lora-model-dir <dir> WITHOUT WHICH EVERY LoRA IS SILENTLY IGNORED (see below)
12//
13// Everything else is the W4-b container unchanged; that original header follows.
14//
15// ---- nx_pe_container_win_io.nx -- W4-b-1: a governed Nishi container with WIRED stdout.
16//
17// W4-a (nx_pe_container_win) ran a Nishi organ as a Job-governed child and reaped exit 88, but the
18// organ's "NX" stdout was lost (CreateProcessW used bInheritHandles=FALSE + CREATE_NO_WINDOW). This
19// rung wires container output: the child inherits the supervisor's stdout, so the organ's output is
20// CAPTURED -- making the container usable for real services that log to stdout (Elder-AI svcs do).
21//
22// CHANGE vs W4-a (two immediates only; everything else byte-identical):
23// - CreateProcessW arg5 bInheritHandles: FALSE(0) -> TRUE(1) ([rsp+0x20])
24// - CreateProcessW arg6 dwCreationFlags: CREATE_NO_WINDOW(0x08000000) -> 0 ([rsp+0x28])
25// With handle inheritance on and no NO_WINDOW, the child (console app) inherits the supervisor's
26// stdout handle, so its writes flow to wherever the supervisor's stdout points (console or file).
27//
28// Pipeline (same governed run+reap as W2c/W4-a): CreateJobObjectW -> SetInformationJobObject(limit=8)
29// -> AssignProcessToJobObject(self,-1) -> CreateProcessW("nx_win_compiled_organ.exe", inherit=TRUE,
30// flags=0) -> WaitForSingleObject(INFINITE) -> GetExitCodeProcess -> ExitProcess(childExit).
31//
32// NO-FALSE-GREEN: the organ prints "NX\n" and exits 88. With output wired, BOTH must hold: stdout
33// captures "NX" (container output flows out) AND supervisor exit == 88 (governed child reaped). The
34// W4-a non-existent-child control (-> exit 0, rep-stosq-25 zeroes exitCode) still applies.
35//
36// lineage_id: substrate_pe_container_win_io_v1 (evolves substrate_pe_container_win_v1).
37
38import "nx_syscalls.nx"
39import "nx_hal.nx"
40import "nx_pe_writer.nx"
41
42const PE_CWIO_FILE_SIZE: i64 = 0xA00 // enlarged: .data 0x400 holds sd-server's long cmdline; .idata shifted to file 0x800 (RVA fixed)
43
44// .data is 0x400 bytes and .idata begins at file 0x800. A UTF-16 cmdline longer than
45// half that would run straight over the import table and produce a PE that loads and
46// then jumps through a corrupted IAT. The cap is CHECKED BEFORE THE FIRST BYTE IS
47// WRITTEN, so an over-long cmdline is a clean build failure, never a broken binary.
48const PE_CWIO_CMDLINE_MAX_CHARS: i64 = 511 // 511 chars + NUL = 0x400 bytes exactly
49
50func nxcwio_strlen(s: *u8) -> i64 {
51 var n: i64 = 0
52 while s[n] != 0 { n = n + 1 }
53 return n
54}
55
56func nxcwio_put_utf16(buf: *u8, off: i64, s: *u8) -> i64 {
57 if nxcwio_strlen(s) > PE_CWIO_CMDLINE_MAX_CHARS { return 0 - 1 }
58 var i: i64 = 0
59 while s[i] != 0 {
60 _w16(buf, off + i * 2, s[i] as i64)
61 i = i + 1
62 }
63 _w16(buf, off + i * 2, 0)
64 return i
65}
66
67func nx_pe_emit_container_io(buf: *u8) -> i64 {
68 if (buf as i64) == 0 { return 0 - NX_PE_BAD_INPUT }
69
70 _w16(buf, 0, 0x5A4D)
71 _w32(buf, 0x3C, FOFF_PE_SIG)
72 _w32(buf, FOFF_PE_SIG, 0x00004550)
73
74 _w16(buf, FOFF_COFF + 0, PE_MACHINE_AMD64)
75 _w16(buf, FOFF_COFF + 2, 3)
76 _w16(buf, FOFF_COFF + 16, 0xF0)
77 _w16(buf, FOFF_COFF + 18, PE_CHAR_EXEC | PE_CHAR_LARGE_ADDR)
78
79 _w16(buf, FOFF_OPT + 0, PE_OH_MAGIC_PEPLUS)
80 _w8(buf, FOFF_OPT + 2, 1)
81 _w32(buf, FOFF_OPT + 4, 0x200)
82 _w32(buf, FOFF_OPT + 8, 0x400)
83 _w32(buf, FOFF_OPT + 16, PE_HELLO_RVA_TEXT)
84 _w32(buf, FOFF_OPT + 20, PE_HELLO_RVA_TEXT)
85 _w64(buf, FOFF_OPT + 24, IMG_BASE_LO, IMG_BASE_HI)
86 _w32(buf, FOFF_OPT + 32, 0x1000)
87 _w32(buf, FOFF_OPT + 36, 0x200)
88 _w16(buf, FOFF_OPT + 40, 6)
89 _w16(buf, FOFF_OPT + 48, 6)
90 _w32(buf, FOFF_OPT + 56, 0x4000)
91 _w32(buf, FOFF_OPT + 60, 0x200)
92 _w16(buf, FOFF_OPT + 68, PE_SUBSYSTEM_CONSOLE)
93 _w64(buf, FOFF_OPT + 72, 0x100000, 0)
94 _w64(buf, FOFF_OPT + 80, 0x1000, 0)
95 _w64(buf, FOFF_OPT + 88, 0x100000, 0)
96 _w64(buf, FOFF_OPT + 96, 0x1000, 0)
97 _w32(buf, FOFF_OPT + 108, 16)
98 _w32(buf, FOFF_OPT + 112 + 8, PE_HELLO_RVA_IDATA)
99 _w32(buf, FOFF_OPT + 112 + 12, 0x28)
100
101 _emit_section_header(buf, FOFF_SECT_TBL, 46, 116, 101, 120, 116, 0, 0, 0, 0xF8, PE_HELLO_RVA_TEXT, 0x200, PE_HELLO_FOFF_TEXT, PE_SECT_CODE_X_R)
102 _emit_section_header(buf, FOFF_SECT_TBL + 40, 46, 100, 97, 116, 97, 0, 0, 0, 0x400, PE_HELLO_RVA_RDATA, 0x400, PE_HELLO_FOFF_RDATA, PE_SECT_DATA_RW)
103 _emit_section_header(buf, FOFF_SECT_TBL + 80, 46, 105, 100, 97, 116, 97, 0, 0, 0x4B, PE_HELLO_RVA_IDATA, 0x200, 0x800, PE_SECT_DATA_R)
104 _w32(buf, FOFF_SECT_TBL + 80 + 8, 0x14B)
105
106 // ===== .text (W2c/W4-a pipeline; TWO immediates differ -- see bInheritHandles/dwCreationFlags below) =====
107 let t: i64 = PE_HELLO_FOFF_TEXT
108 _w8(buf, t+0, 0x48); _w8(buf, t+1, 0x81); _w8(buf, t+2, 0xEC); _w32(buf, t+3, 0x138)
109 _w8(buf, t+7, 0x31); _w8(buf, t+8, 0xC0)
110 _w8(buf, t+9, 0x48); _w8(buf, t+10, 0x8D); _w8(buf, t+11, 0x7C); _w8(buf, t+12, 0x24); _w8(buf, t+13, 0x58)
111 _w8(buf, t+14, 0xB9); _w32(buf, t+15, 25) // zero 25 qwords (PROCESS_INFORMATION + exitCode -> launch-fail=exit 0)
112 _w8(buf, t+19, 0xF3); _w8(buf, t+20, 0x48); _w8(buf, t+21, 0xAB)
113 _w8(buf, t+22, 0xC7); _w8(buf, t+23, 0x44); _w8(buf, t+24, 0x24); _w8(buf, t+25, 0x58); _w32(buf, t+26, 0x68)
114 _w8(buf, t+30, 0x31); _w8(buf, t+31, 0xC9); _w8(buf, t+32, 0x31); _w8(buf, t+33, 0xD2)
115 _w8(buf, t+34, 0xFF); _w8(buf, t+35, 0x15); _w32(buf, t+36, 0x2040)
116 _w8(buf, t+40, 0x48); _w8(buf, t+41, 0x89); _w8(buf, t+42, 0x44); _w8(buf, t+43, 0x24); _w8(buf, t+44, 0x50)
117 _w8(buf, t+45, 0xC7); _w8(buf, t+46, 0x84); _w8(buf, t+47, 0x24); _w32(buf, t+48, 0xE8); _w32(buf, t+52, 8)
118 _w8(buf, t+56, 0xC7); _w8(buf, t+57, 0x84); _w8(buf, t+58, 0x24); _w32(buf, t+59, 0x100); _w32(buf, t+63, 8)
119 _w8(buf, t+67, 0x48); _w8(buf, t+68, 0x8B); _w8(buf, t+69, 0x4C); _w8(buf, t+70, 0x24); _w8(buf, t+71, 0x50)
120 _w8(buf, t+72, 0xBA); _w32(buf, t+73, 2)
121 _w8(buf, t+77, 0x4C); _w8(buf, t+78, 0x8D); _w8(buf, t+79, 0x84); _w8(buf, t+80, 0x24); _w32(buf, t+81, 0xD8)
122 _w8(buf, t+85, 0x41); _w8(buf, t+86, 0xB9); _w32(buf, t+87, 0x40)
123 _w8(buf, t+91, 0xFF); _w8(buf, t+92, 0x15); _w32(buf, t+93, 0x200F)
124 _w8(buf, t+97, 0x48); _w8(buf, t+98, 0x8B); _w8(buf, t+99, 0x4C); _w8(buf, t+100, 0x24); _w8(buf, t+101, 0x50)
125 _w8(buf, t+102, 0x48); _w8(buf, t+103, 0xC7); _w8(buf, t+104, 0xC2); _w8(buf, t+105, 0xFF); _w8(buf, t+106, 0xFF); _w8(buf, t+107, 0xFF); _w8(buf, t+108, 0xFF)
126 _w8(buf, t+109, 0xFF); _w8(buf, t+110, 0x15); _w32(buf, t+111, 0x2005)
127 _w8(buf, t+115, 0x31); _w8(buf, t+116, 0xC9)
128 _w8(buf, t+117, 0x48); _w8(buf, t+118, 0x8D); _w8(buf, t+119, 0x15); _w32(buf, t+120, 0xF84)
129 _w8(buf, t+124, 0x45); _w8(buf, t+125, 0x31); _w8(buf, t+126, 0xC0)
130 _w8(buf, t+127, 0x45); _w8(buf, t+128, 0x31); _w8(buf, t+129, 0xC9)
131 // arg5 bInheritHandles = TRUE (W4-b: was 0 in W4-a)
132 _w8(buf, t+130, 0xC7); _w8(buf, t+131, 0x44); _w8(buf, t+132, 0x24); _w8(buf, t+133, 0x20); _w32(buf, t+134, 1)
133 // arg6 dwCreationFlags = 0 (W4-b: was 0x08000000 CREATE_NO_WINDOW in W4-a)
134 _w8(buf, t+138, 0xC7); _w8(buf, t+139, 0x44); _w8(buf, t+140, 0x24); _w8(buf, t+141, 0x28); _w32(buf, t+142, 0)
135 _w8(buf, t+146, 0x48); _w8(buf, t+147, 0xC7); _w8(buf, t+148, 0x44); _w8(buf, t+149, 0x24); _w8(buf, t+150, 0x30); _w32(buf, t+151, 0)
136 _w8(buf, t+155, 0x48); _w8(buf, t+156, 0xC7); _w8(buf, t+157, 0x44); _w8(buf, t+158, 0x24); _w8(buf, t+159, 0x38); _w32(buf, t+160, 0)
137 _w8(buf, t+164, 0x48); _w8(buf, t+165, 0x8D); _w8(buf, t+166, 0x44); _w8(buf, t+167, 0x24); _w8(buf, t+168, 0x58)
138 _w8(buf, t+169, 0x48); _w8(buf, t+170, 0x89); _w8(buf, t+171, 0x44); _w8(buf, t+172, 0x24); _w8(buf, t+173, 0x40)
139 _w8(buf, t+174, 0x48); _w8(buf, t+175, 0x8D); _w8(buf, t+176, 0x84); _w8(buf, t+177, 0x24); _w32(buf, t+178, 0xC0)
140 _w8(buf, t+182, 0x48); _w8(buf, t+183, 0x89); _w8(buf, t+184, 0x44); _w8(buf, t+185, 0x24); _w8(buf, t+186, 0x48)
141 _w8(buf, t+187, 0xFF); _w8(buf, t+188, 0x15); _w32(buf, t+189, 0x1FBF)
142 _w8(buf, t+193, 0x48); _w8(buf, t+194, 0x8B); _w8(buf, t+195, 0x8C); _w8(buf, t+196, 0x24); _w32(buf, t+197, 0xC0)
143 _w8(buf, t+201, 0xBA); _w8(buf, t+202, 0xFF); _w8(buf, t+203, 0xFF); _w8(buf, t+204, 0xFF); _w8(buf, t+205, 0xFF)
144 _w8(buf, t+206, 0xFF); _w8(buf, t+207, 0x15); _w32(buf, t+208, 0x1FB4)
145 _w8(buf, t+212, 0x48); _w8(buf, t+213, 0x8B); _w8(buf, t+214, 0x8C); _w8(buf, t+215, 0x24); _w32(buf, t+216, 0xC0)
146 _w8(buf, t+220, 0x48); _w8(buf, t+221, 0x8D); _w8(buf, t+222, 0x94); _w8(buf, t+223, 0x24); _w32(buf, t+224, 0x118)
147 _w8(buf, t+228, 0xFF); _w8(buf, t+229, 0x15); _w32(buf, t+230, 0x1FA6)
148 _w8(buf, t+234, 0x8B); _w8(buf, t+235, 0x8C); _w8(buf, t+236, 0x24); _w32(buf, t+237, 0x118)
149 _w8(buf, t+241, 0xFF); _w8(buf, t+242, 0x15); _w32(buf, t+243, 0x1FA1)
150 _w8(buf, t+247, 0xCC)
151
152 // ===== .data: child command line (UTF-16) =====
153 // --listen-ip 0.0.0.0 so the NAS gen orchestrator can reach this worker, and
154 // --lora-model-dir because WITHOUT it sd-server silently drops every <lora:...>
155 // tag: extract_and_remove_lora() returns immediately on an empty dir, so the tags
156 // ride into the text encoder as literal tokens and no LoRA weights are ever
157 // applied. The realism LoRAs were dead this way (2026-07-25); the cmdline is the
158 // only place that can prevent it recurring on the next watchdog restart.
159 if nxcwio_put_utf16(buf, PE_HELLO_FOFF_RDATA, "sd-server.exe --listen-ip 0.0.0.0 --diffusion-model C:/Users/elder/elder-ai-platform/models/unified/diffusion/zimage_turbo_nsfw_2602-Q8_0.gguf --llm C:/Users/elder/elder-ai-platform/models/unified/text_encoder/Z-Image_Qwen_3_4b-Q6_K.gguf --vae C:/Users/elder/elder-ai-platform/models/unified/vae/ae.safetensors --lora-model-dir C:/Users/elder/elder-ai-platform/models/unified/loras --listen-port 7861" as *u8) < 0 { return NX_PE_ERR }
160
161 // ===== .idata (7 imports, identical to W2c/W4-a) =====
162 let d: i64 = 0x800 // .idata content shifted to file 0x800 (RVA still 0x3000 -> .text IAT refs unchanged)
163 _w32(buf, d+0, 0x3028)
164 _w32(buf, d+12, 0x313E)
165 _w32(buf, d+16, 0x3068)
166 _w64(buf, d+0x28, 0x30A8, 0)
167 _w64(buf, d+0x30, 0x30BC, 0)
168 _w64(buf, d+0x38, 0x30D6, 0)
169 _w64(buf, d+0x40, 0x30F2, 0)
170 _w64(buf, d+0x48, 0x3104, 0)
171 _w64(buf, d+0x50, 0x311A, 0)
172 _w64(buf, d+0x58, 0x3130, 0)
173 _w64(buf, d+0x60, 0, 0)
174 _w64(buf, d+0x68, 0x30A8, 0)
175 _w64(buf, d+0x70, 0x30BC, 0)
176 _w64(buf, d+0x78, 0x30D6, 0)
177 _w64(buf, d+0x80, 0x30F2, 0)
178 _w64(buf, d+0x88, 0x3104, 0)
179 _w64(buf, d+0x90, 0x311A, 0)
180 _w64(buf, d+0x98, 0x3130, 0)
181 _w64(buf, d+0xA0, 0, 0)
182 _w16(buf, d+0xA8, 0)
183 _w8(buf,d+0xAA,67);_w8(buf,d+0xAB,114);_w8(buf,d+0xAC,101);_w8(buf,d+0xAD,97);_w8(buf,d+0xAE,116);_w8(buf,d+0xAF,101)
184 _w8(buf,d+0xB0,74);_w8(buf,d+0xB1,111);_w8(buf,d+0xB2,98);_w8(buf,d+0xB3,79);_w8(buf,d+0xB4,98);_w8(buf,d+0xB5,106)
185 _w8(buf,d+0xB6,101);_w8(buf,d+0xB7,99);_w8(buf,d+0xB8,116);_w8(buf,d+0xB9,87);_w8(buf,d+0xBA,0)
186 _w16(buf, d+0xBC, 0)
187 _w8(buf,d+0xBE,83);_w8(buf,d+0xBF,101);_w8(buf,d+0xC0,116);_w8(buf,d+0xC1,73);_w8(buf,d+0xC2,110);_w8(buf,d+0xC3,102)
188 _w8(buf,d+0xC4,111);_w8(buf,d+0xC5,114);_w8(buf,d+0xC6,109);_w8(buf,d+0xC7,97);_w8(buf,d+0xC8,116);_w8(buf,d+0xC9,105)
189 _w8(buf,d+0xCA,111);_w8(buf,d+0xCB,110);_w8(buf,d+0xCC,74);_w8(buf,d+0xCD,111);_w8(buf,d+0xCE,98);_w8(buf,d+0xCF,79)
190 _w8(buf,d+0xD0,98);_w8(buf,d+0xD1,106);_w8(buf,d+0xD2,101);_w8(buf,d+0xD3,99);_w8(buf,d+0xD4,116);_w8(buf,d+0xD5,0)
191 _w16(buf, d+0xD6, 0)
192 _w8(buf,d+0xD8,65);_w8(buf,d+0xD9,115);_w8(buf,d+0xDA,115);_w8(buf,d+0xDB,105);_w8(buf,d+0xDC,103);_w8(buf,d+0xDD,110)
193 _w8(buf,d+0xDE,80);_w8(buf,d+0xDF,114);_w8(buf,d+0xE0,111);_w8(buf,d+0xE1,99);_w8(buf,d+0xE2,101);_w8(buf,d+0xE3,115)
194 _w8(buf,d+0xE4,115);_w8(buf,d+0xE5,84);_w8(buf,d+0xE6,111);_w8(buf,d+0xE7,74);_w8(buf,d+0xE8,111);_w8(buf,d+0xE9,98)
195 _w8(buf,d+0xEA,79);_w8(buf,d+0xEB,98);_w8(buf,d+0xEC,106);_w8(buf,d+0xED,101);_w8(buf,d+0xEE,99);_w8(buf,d+0xEF,116);_w8(buf,d+0xF0,0)
196 _w16(buf, d+0xF2, 0)
197 _w8(buf,d+0xF4,67);_w8(buf,d+0xF5,114);_w8(buf,d+0xF6,101);_w8(buf,d+0xF7,97);_w8(buf,d+0xF8,116);_w8(buf,d+0xF9,101)
198 _w8(buf,d+0xFA,80);_w8(buf,d+0xFB,114);_w8(buf,d+0xFC,111);_w8(buf,d+0xFD,99);_w8(buf,d+0xFE,101);_w8(buf,d+0xFF,115)
199 _w8(buf,d+0x100,115);_w8(buf,d+0x101,87);_w8(buf,d+0x102,0)
200 _w16(buf, d+0x104, 0)
201 _w8(buf,d+0x106,87);_w8(buf,d+0x107,97);_w8(buf,d+0x108,105);_w8(buf,d+0x109,116);_w8(buf,d+0x10A,70);_w8(buf,d+0x10B,111)
202 _w8(buf,d+0x10C,114);_w8(buf,d+0x10D,83);_w8(buf,d+0x10E,105);_w8(buf,d+0x10F,110);_w8(buf,d+0x110,103);_w8(buf,d+0x111,108)
203 _w8(buf,d+0x112,101);_w8(buf,d+0x113,79);_w8(buf,d+0x114,98);_w8(buf,d+0x115,106);_w8(buf,d+0x116,101);_w8(buf,d+0x117,99)
204 _w8(buf,d+0x118,116);_w8(buf,d+0x119,0)
205 _w16(buf, d+0x11A, 0)
206 _w8(buf,d+0x11C,71);_w8(buf,d+0x11D,101);_w8(buf,d+0x11E,116);_w8(buf,d+0x11F,69);_w8(buf,d+0x120,120);_w8(buf,d+0x121,105)
207 _w8(buf,d+0x122,116);_w8(buf,d+0x123,67);_w8(buf,d+0x124,111);_w8(buf,d+0x125,100);_w8(buf,d+0x126,101);_w8(buf,d+0x127,80)
208 _w8(buf,d+0x128,114);_w8(buf,d+0x129,111);_w8(buf,d+0x12A,99);_w8(buf,d+0x12B,101);_w8(buf,d+0x12C,115);_w8(buf,d+0x12D,115)
209 _w8(buf,d+0x12E,0)
210 _w16(buf, d+0x130, 0)
211 _w8(buf,d+0x132,69);_w8(buf,d+0x133,120);_w8(buf,d+0x134,105);_w8(buf,d+0x135,116);_w8(buf,d+0x136,80);_w8(buf,d+0x137,114)
212 _w8(buf,d+0x138,111);_w8(buf,d+0x139,99);_w8(buf,d+0x13A,101);_w8(buf,d+0x13B,115);_w8(buf,d+0x13C,115);_w8(buf,d+0x13D,0)
213 _w8(buf,d+0x13E,107);_w8(buf,d+0x13F,101);_w8(buf,d+0x140,114);_w8(buf,d+0x141,110);_w8(buf,d+0x142,101);_w8(buf,d+0x143,108)
214 _w8(buf,d+0x144,51);_w8(buf,d+0x145,50);_w8(buf,d+0x146,46);_w8(buf,d+0x147,100);_w8(buf,d+0x148,108);_w8(buf,d+0x149,108);_w8(buf,d+0x14A,0)
215
216 return NX_PE_OK
217}
218
219func main() -> i64 {
220 let buf: *u8 = nx_hal_alloc_pages(PE_CWIO_FILE_SIZE)
221 if (buf as i64) == 0 { return 1 }
222 let rc: i64 = nx_pe_emit_container_io(buf)
223 if rc != NX_PE_OK { return 2 }
224 if buf[0] != 0x4D { return 10 }
225 if buf[0x200] != 0x48 { return 12 }
226 if buf[0x400] != 0x73 { return 13 } // .data 's' (sd-server.exe cmdline, UTF-16)
227 if buf[0x286] != 0x01 { return 15 } // bInheritHandles imm = 1 (t+134 = 0x200+134 = 0x286) -- .text unchanged
228 let p: *u8 = "/mnt/c/Users/elder/elder-ai-platform/services/sdcpp/elder-sdcpp/build-nishi-cuda/bin/nx_pe_container_elderai.exe" as *u8
229 if nx_pe_write_to_file(p, buf, PE_CWIO_FILE_SIZE) != NX_PE_OK { return 70 }
230 let msg: *u8 = "[substrate] nx_pe_container_elderai.exe written (Job-governs sd-server, LoRA dir wired)\n" as *u8
231 sys_write(1, msg, 88)
232 return 0
233}