nx_container_spec_emit.nx source
↩ module page · 154 lines · 7247 B
1// nx_container_spec_emit.nx -- R1 of the Nishi Container Host: SPEC-DRIVEN container emit (the sovereign `docker build`).
2// Reads a ContainerSpec (data: cmdline + out path) and STAMPS it onto the PROVEN governance-binary TEMPLATE
3// (nx_pe_container_sdserver.exe): .text/.idata = the immutable Job-Object base layer; .data = the per-service cmdline.
4// = ONE emitter governs ANY service per spec, killing the hand-authored-container-organ-per-service copy-paste (Rule 15).
5// NEVER-BRICK by construction: refuses a cmdline that would overflow the template .data (Rule 12, bounds at the front door).
6// GATE: same-cmdline stamp -> byte-identical to template (lossless); diff-cmdline -> only .data changes (.text/.idata
7// immutable); over-long -> refused. NO fake greens. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_runtime.nx"
10const TPL_MAGIC_8192: i64 = 8192
11const TPL_MAGIC_8191: i64 = 8191
12const TPL_MAGIC_2048: i64 = 2048
13const TPL_MAGIC_1024: i64 = 1024
14
15const TEMPLATE: *u8 = "/mnt/c/Users/elder/elder-ai-platform/services/sdcpp/elder-sdcpp/build-nishi-cuda/bin/nx_pe_container_sdserver.exe"
16const SPEC: *u8 = "knowledge/containers/elderai.spec"
17const TPL_SIZE: i64 = 0xA00
18const DATA_OFF: i64 = 0x400
19const DATA_CAP: i64 = 0x400
20const SD_CMD: *u8 = "sd-server.exe --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 --listen-port 7861"
21
22func csp_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
23func csp_wn(fd: i64, v: i64) -> i64 {
24 let bb: *u8 = sys_mmap(28)
25 var m: i64 = v
26 if m < 0 { sys_write(fd, "-" as *u8, 1); m = 0 - m }
27 let tt: *u8 = sys_mmap(28)
28 var k: i64 = 0
29 if m == 0 { tt[0] = 48 as u8; k = 1 }
30 while m > 0 { tt[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
31 var i: i64 = 0
32 while i < k { bb[i] = tt[k-1-i]; i = i + 1 }
33 sys_write(fd, bb, k)
34 return 0
35}
36func csp_p(s: *u8) -> i64 { return csp_w(1, s) }
37func csp_pn(v: i64) -> i64 { return csp_wn(1, v) }
38func csp_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
39
40func csp_read(path: *u8, buf: *u8, cap: i64) -> i64 {
41 let fd: i64 = sys_openat_rd(path)
42 if fd < 0 { return 0 }
43 var tot: i64 = 0
44 var go: i64 = 1
45 while go == 1 {
46 let r: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot)
47 if r <= 0 { go = 0 } else { tot = tot + r }
48 if tot >= cap { go = 0 }
49 }
50 sys_close(fd)
51 return tot
52}
53func csp_find(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
54 if pl <= 0 { return 0 - 1 }
55 var i: i64 = 0
56 while i + pl <= n {
57 var k: i64 = 0
58 var hit: i64 = 1
59 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
60 if hit == 1 { return i + pl }
61 i = i + 1
62 }
63 return 0 - 1
64}
65func csp_get_str(buf: *u8, n: i64, key: *u8, out: *u8, outcap: i64) -> i64 {
66 let kl: i64 = csp_strlen(key)
67 let p: i64 = csp_find(buf, n, key, kl)
68 if p < 0 { return 0 - 1 }
69 var i: i64 = p
70 var k: i64 = 0
71 while i < n {
72 let c: i64 = buf[i] as i64
73 if c == 10 { i = n } else { if c == 13 { i = n } else { if k + 1 < outcap { out[k] = buf[i]; k = k + 1; i = i + 1 } else { i = n } } }
74 }
75 out[k] = 0 as u8
76 return k
77}
78func csp_zero(buf: *u8, off: i64, len: i64) -> i64 { var i: i64 = 0; while i < len { buf[off+i] = 0 as u8; i = i + 1 } return 0 }
79func csp_put_utf16(buf: *u8, off: i64, s: *u8) -> i64 {
80 var i: i64 = 0
81 while s[i] != (0 as u8) { buf[off + i*2] = s[i]; buf[off + i*2 + 1] = 0 as u8; i = i + 1 }
82 buf[off + i*2] = 0 as u8
83 buf[off + i*2 + 1] = 0 as u8
84 return i
85}
86func csp_cmp(a: *u8, b: *u8, off: i64, len: i64) -> i64 { var i: i64 = 0; while i < len { if a[off+i] != b[off+i] { return 1 } i = i + 1 } return 0 }
87func csp_copy(dst: *u8, src: *u8, len: i64) -> i64 { var i: i64 = 0; while i < len { dst[i] = src[i]; i = i + 1 } return 0 }
88// stamp cmd onto a template copy already in out_buf. returns 0 ok, <0 refuse (never-brick overflow guard)
89func csp_stamp(out_buf: *u8, cmd: *u8) -> i64 {
90 let cl: i64 = csp_strlen(cmd)
91 if (cl + 1) * 2 > DATA_CAP { return 0 - 1 }
92 csp_zero(out_buf, DATA_OFF, DATA_CAP)
93 csp_put_utf16(out_buf, DATA_OFF, cmd)
94 return 0
95}
96
97func main() -> i64 {
98 csp_p("=== nx_container_spec_emit: R1 spec-driven container emit (sovereign docker-build) ===\n" as *u8)
99 let tpl: *u8 = sys_mmap(TPL_SIZE)
100 let tn: i64 = csp_read(TEMPLATE, tpl, TPL_SIZE)
101 var tpl_ok: i64 = 0
102 if tn == TPL_SIZE { if tpl[0] == 0x4D { if tpl[1] == 0x5A { tpl_ok = 1 } } }
103
104 // T1: stamp the SAME sd cmdline -> must reproduce the template byte-for-byte (lossless stamp)
105 let b1: *u8 = sys_mmap(TPL_SIZE)
106 csp_copy(b1, tpl, TPL_SIZE)
107 let r1: i64 = csp_stamp(b1, SD_CMD)
108 var t1: i64 = 0
109 if r1 == 0 { if csp_cmp(b1, tpl, 0, TPL_SIZE) == 0 { t1 = 1 } }
110
111 // T2: read the spec, stamp ITS cmdline -> valid PE, .text+.idata identical to template, only .data changed
112 let sbuf: *u8 = sys_mmap(TPL_MAGIC_8192)
113 let sn: i64 = csp_read(SPEC, sbuf, TPL_MAGIC_8191)
114 let cmd: *u8 = sys_mmap(TPL_MAGIC_2048)
115 let cl: i64 = csp_get_str(sbuf, sn, "cmdline=" as *u8, cmd, TPL_MAGIC_2048)
116 let outp: *u8 = sys_mmap(TPL_MAGIC_1024)
117 let ol: i64 = csp_get_str(sbuf, sn, "out=" as *u8, outp, TPL_MAGIC_1024)
118 let b2: *u8 = sys_mmap(TPL_SIZE)
119 csp_copy(b2, tpl, TPL_SIZE)
120 let r2: i64 = csp_stamp(b2, cmd)
121 var t2: i64 = 0
122 if r2 == 0 { if b2[0] == 0x4D { if csp_cmp(b2, tpl, 0, DATA_OFF) == 0 { if csp_cmp(b2, tpl, 0x800, 0x200) == 0 { if b2[DATA_OFF] == cmd[0] { t2 = 1 } } } } }
123 var wrote: i64 = 0
124 if t2 == 1 { if ol > 0 { let wfd: i64 = sys_openat_wr(outp, 0x1ED); if wfd >= 0 { sys_write(wfd, b2, TPL_SIZE); sys_close(wfd); wrote = 1 } } }
125
126 // T3 neg-control: an over-long cmdline must be REFUSED (never-brick)
127 let big: *u8 = sys_mmap(TPL_MAGIC_1024)
128 var j: i64 = 0
129 while j < 600 { big[j] = 65 as u8; j = j + 1 }
130 big[600] = 0 as u8
131 let b3: *u8 = sys_mmap(TPL_SIZE)
132 csp_copy(b3, tpl, TPL_SIZE)
133 let r3: i64 = csp_stamp(b3, big)
134 var t3: i64 = 0
135 if r3 < 0 { t3 = 1 }
136
137 csp_p(" tpl_ok=" as *u8); csp_pn(tpl_ok)
138 csp_p(" t1_byte_identical=" as *u8); csp_pn(t1)
139 csp_p(" t2_spec_emit_dataonly=" as *u8); csp_pn(t2)
140 csp_p(" wrote_out=" as *u8); csp_pn(wrote)
141 csp_p(" t3_neg_refuse_overflow=" as *u8); csp_pn(t3)
142 csp_p(" spec_cmdline_chars=" as *u8); csp_pn(cl)
143 csp_p("\n" as *u8)
144
145 var pass: i64 = 0
146 if tpl_ok == 1 { if t1 == 1 { if t2 == 1 { if wrote == 1 { if t3 == 1 { pass = 1 } } } } }
147 if pass == 1 {
148 csp_p("CONTAINERSPECGATE verdict=GREEN (spec->container byte-faithful; .text/.idata immutable; never-brick refuses overflow; artifact written)\n" as *u8)
149 csp_p("emitted container -> " as *u8); csp_p(outp); csp_p("\n" as *u8)
150 return 0
151 }
152 csp_p("CONTAINERSPECGATE verdict=RED (stamp/structure/neg-control failed)\n" as *u8)
153 return 1
154}