nx_gen_inpaint_gate.nx source
↩ module page · 97 lines · 5430 B
1// nx_gen_inpaint_gate.nx -- the REFEREE for gen G7 (gw_inpaint_mask): mask inpaint as a NishiLang verb over the
2// engine's A1111 img2img surface. IN-PROCESS over nx_gen_worker.nx (no GPU, no daemon): it composes the img2img body
3// shape the daemon composes and proves FEATURE ON vs OFF yields byte-different bodies, the on-body carries exactly
4// the two fields the served sd-server parses ("mask", "inpainting_mask_invert" -- elder-sdcpp
5// examples/server/main.cpp, sdapi_any2img img2img branch), invert 0 vs 1 differ, the verb is deterministic, and
6// every refusal path is NAMED by code and writes nothing past the caller's offset.
7// DONE-RULE (gen.plan G7): mask rides the A1111 img2img shape the engine already serves; the edited region judged by
8// the coherence organ -- that RENDER half needs a free GPU and is owed; this gate is the COMPOSITION half.
9// license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_gate_verdict.nx"
12import "nx_gen_worker.nx"
13
14const IG_CAP: i64 = 4096
15const IG_SENTINEL: i64 = 126
16const IG_MASK_OK: *u8 = "iVBORw0KGgo="
17const IG_MASK_SPACE: *u8 = "iVBO Rw0K"
18const IG_MASK_DATAURI: *u8 = "data:image/png;base64,iVBORw0KGgo="
19const IG_BASE: *u8 = "{\"init_images\":[\"iVBORw0KGgo=\"],\"prompt\":\"x\",\"denoising_strength\":0.6"
20
21func ig_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
22func ig_find(buf: *u8, n: i64, needle: *u8) -> i64 {
23 let nl: i64 = ig_len(needle)
24 if nl <= 0 { return 0 - 1 }
25 var i: i64 = 0
26 while i + nl <= n {
27 var j: i64 = 0
28 var same: i64 = 1
29 var scan: i64 = 1
30 while scan == 1 { if j >= nl { scan = 0 } else { if buf[i + j] != needle[j] { same = 0; scan = 0 } else { j = j + 1 } } }
31 if same == 1 { return i }
32 i = i + 1
33 }
34 return 0 - 1
35}
36func ig_same(a: *u8, na: i64, b: *u8, nb: i64) -> i64 {
37 if na != nb { return 0 }
38 var i: i64 = 0
39 while i < na { if a[i] != b[i] { return 0 } i = i + 1 }
40 return 1
41}
42// a fresh buffer: sentinel-filled so "wrote nothing past the offset" is a byte you can read, then the base body
43func ig_base(buf: *u8) -> i64 {
44 var i: i64 = 0
45 while i < IG_CAP { buf[i] = IG_SENTINEL as u8; i = i + 1 }
46 return gw_cat(buf, 0, IG_BASE)
47}
48
49func main() -> i64 {
50 gv_head("nx_gen_inpaint_gate -- G7: the mask verb composes the engine's own img2img fields; on vs off differ by bytes; refusals are named codes" as *u8)
51 let ctr: *i64 = gv_ctr()
52 let mlen: i64 = ig_len(IG_MASK_OK)
53
54 // OFF: the plain img2img body (what go_handle_img2img sends with no mask)
55 let off: *u8 = sys_mmap(IG_CAP)
56 let noff: i64 = ig_base(off)
57 gv_check("fixture-reached-condition-base-body-composed" as *u8, noff > 0, ctr)
58 gv_check("feature-off-body-carries-no-mask-key" as *u8, ig_find(off, noff, "\"mask\"" as *u8) < 0, ctr)
59
60 // ON: the same body with the mask appended, invert 0
61 let on0: *u8 = sys_mmap(IG_CAP)
62 let b0: i64 = ig_base(on0)
63 let n0: i64 = gw_inpaint_mask(on0, b0, IG_MASK_OK, mlen, 0)
64 gv_check("feature-on-returns-a-longer-offset" as *u8, n0 > b0, ctr)
65 gv_check("on-vs-off-bodies-are-byte-different" as *u8, ig_same(off, noff, on0, n0) == 0, ctr)
66 gv_check("on-body-keeps-the-off-body-as-its-prefix" as *u8, (n0 > noff) & (ig_same(off, noff, on0, noff) == 1), ctr)
67 gv_check("on-body-carries-mask-field-spelled-as-the-server-parses-it" as *u8, ig_find(on0, n0, ",\"mask\":\"iVBORw0KGgo=\"" as *u8) >= 0, ctr)
68 gv_check("on-body-carries-inpainting-mask-invert-0" as *u8, ig_find(on0, n0, "\"inpainting_mask_invert\":0" as *u8) >= 0, ctr)
69
70 // ON with invert 1
71 let on1: *u8 = sys_mmap(IG_CAP)
72 let b1: i64 = ig_base(on1)
73 let n1: i64 = gw_inpaint_mask(on1, b1, IG_MASK_OK, mlen, 1)
74 gv_check("invert-1-emits-inpainting-mask-invert-1" as *u8, (n1 > b1) & (ig_find(on1, n1, "\"inpainting_mask_invert\":1" as *u8) >= 0), ctr)
75 gv_check("invert-0-and-invert-1-bodies-differ" as *u8, ig_same(on0, n0, on1, n1) == 0, ctr)
76
77 // determinism
78 let on2: *u8 = sys_mmap(IG_CAP)
79 let b2: i64 = ig_base(on2)
80 let n2: i64 = gw_inpaint_mask(on2, b2, IG_MASK_OK, mlen, 0)
81 gv_check("two-identical-calls-compose-identical-bytes" as *u8, ig_same(on0, n0, on2, n2) == 1, ctr)
82
83 // NEG-CONTROLS: each refusal is a named code, and a refusal writes nothing past the offset
84 let r: *u8 = sys_mmap(IG_CAP)
85 let br: i64 = ig_base(r)
86 let re: i64 = gw_inpaint_mask(r, br, IG_MASK_OK, 0, 0)
87 gv_check("neg-control-empty-mask-refuses-GW_MASK_EMPTY" as *u8, re == GW_MASK_EMPTY, ctr)
88 let rs: i64 = gw_inpaint_mask(r, br, IG_MASK_SPACE, ig_len(IG_MASK_SPACE), 0)
89 gv_check("neg-control-whitespace-in-mask-refuses-GW_MASK_NOT_B64" as *u8, rs == GW_MASK_NOT_B64, ctr)
90 let rd: i64 = gw_inpaint_mask(r, br, IG_MASK_DATAURI, ig_len(IG_MASK_DATAURI), 0)
91 gv_check("neg-control-data-uri-prefix-refuses-GW_MASK_NOT_B64" as *u8, rd == GW_MASK_NOT_B64, ctr)
92 let ri: i64 = gw_inpaint_mask(r, br, IG_MASK_OK, mlen, 2)
93 gv_check("neg-control-invert-2-refuses-GW_MASK_BAD_INVERT" as *u8, ri == GW_MASK_BAD_INVERT, ctr)
94 gv_check("neg-control-every-refusal-left-the-body-untouched" as *u8, ((r[br] & 0xff) == IG_SENTINEL) & (ig_same(off, noff, r, br) == 1), ctr)
95
96 return gv_verdict("GEN-INPAINT-GATE" as *u8, ctr, "G7 composition half over the engine's own img2img mask fields; the render half awaits a free GPU" as *u8)
97}