code wiki / (root) / nx_gen_promptweight_gate.nx

nx_gen_promptweight_gate.nx source

↩ module page · 183 lines · 12193 B

1// nx_gen_promptweight_gate.nx -- the REFEREE for gen G13 (cw_prompt_weight): prompt weighting as DATA on the shared 2// photo-prompt composer, on the contract knowledge/gen_prompt_weight.conf declared before the code existed: PERMIL 3// units, a permitted ENVELOPE [min,max] as the first two ints, out-of-envelope REFUSED by named code (never clamped), 4// slot weights as ADDITIVE ints 3..5 with 1000 the identity. IN-PROCESS over nx_companion_wardrobe.nx, GPU-free: the 5// verb emits the engine's own A1111 syntax, 1.000 is the byte-identical identity (feature OFF by default), the parser 6// keeps order and survives bad data, the free-text admission scanner names the first offending group, and -- the 7// compose-level teeth -- the REAL composer run from a scratch cwd with a planted conf produces byte-different prompts 8// for 1000 vs 1300 on the trigger slot, reproduces the off bytes exactly when set back, and falls back to the off bytes 9// (the baseline, never a mutated render) when a DATA slot is out of envelope. 10// FIXTURE LAW: scratch under /tmp/<gate>/ (a planted knowledge/gen_prompt_weight.conf + a planted elara_trigger.txt), 11// recreated at SETUP; the gate chdirs into it so the composer's RELATIVE reads see the fixture and never production. 12// The live-conf tooth runs BEFORE the plant (relative path absent -> the nishihost conf) and asserts only sanity, never 13// a value, so an operator edit to production weights can never turn this gate RED. 14// license_tier: ORIGINAL expect_exit: 0 15import "nx_syscalls.nx" 16import "nx_gate_verdict.nx" 17import "nx_companion_wardrobe.nx" 18 19const PW_DIR: *u8 = "/tmp/nx_gen_promptweight_gate" 20const PW_KDIR: *u8 = "/tmp/nx_gen_promptweight_gate/knowledge" 21const PW_CONF: *u8 = "/tmp/nx_gen_promptweight_gate/knowledge/gen_prompt_weight.conf" 22const PW_TRIG: *u8 = "/tmp/nx_gen_promptweight_gate/elara_trigger.txt" 23const PW_MODE_DIR: i64 = 511 24const PW_MODE_FILE: i64 = 420 25const PW_CAP: i64 = 32768 26const PW_SENTINEL: i64 = 126 27const PW_SCENE: *u8 = "on the kitchen counter" 28const PW_TRIGGER_WORD: *u8 = "rawphotoword" 29const PW_SEED: i64 = 7 30const PW_CONF_OFF: *u8 = "# planted by nx_gen_promptweight_gate\n100 2000\n1000 1000 1000\n" 31const PW_CONF_ON: *u8 = "# planted by nx_gen_promptweight_gate\n100 2000\n1300 1000 1000\n" 32const PW_CONF_BAD: *u8 = "# planted by nx_gen_promptweight_gate\n100 2000\n9000 1000 1000\n" 33 34func pw_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 35func pw_find(buf: *u8, n: i64, needle: *u8) -> i64 { 36 let nl: i64 = pw_len(needle) 37 if nl <= 0 { return 0 - 1 } 38 var i: i64 = 0 39 while i + nl <= n { 40 var j: i64 = 0 41 var same: i64 = 1 42 var scan: i64 = 1 43 while scan == 1 { if j >= nl { scan = 0 } else { if buf[i + j] != needle[j] { same = 0; scan = 0 } else { j = j + 1 } } } 44 if same == 1 { return i } 45 i = i + 1 46 } 47 return 0 - 1 48} 49func pw_same(a: *u8, na: i64, b: *u8, nb: i64) -> i64 { 50 if na != nb { return 0 } 51 var i: i64 = 0 52 while i < na { if a[i] != b[i] { return 0 } i = i + 1 } 53 return 1 54} 55// buf[0..n) equals the NUL-terminated literal exactly 56func pw_eq(buf: *u8, n: i64, lit: *u8) -> i64 { return pw_same(buf, n, lit, pw_len(lit)) } 57func pw_fill(buf: *u8) -> i64 { var i: i64 = 0; while i < PW_CAP { buf[i] = PW_SENTINEL as u8; i = i + 1 } return 0 } 58func pw_write(path: *u8, s: *u8) -> i64 { 59 sys_unlinkat(path) 60 let fd: i64 = sys_openat_wr(path, PW_MODE_FILE) 61 if fd < 0 { return 0 - 1 } 62 let n: i64 = pw_len(s) 63 sys_write(fd, s, n) 64 sys_close(fd) 65 return n 66} 67func pw_compose(out: *u8) -> i64 { return go_compose_photo_prompt(PW_SCENE, pw_len(PW_SCENE), "" as *u8, PW_SEED, out) } 68func pw_env5(e: *i64, mn: i64, mx: i64, t: i64, c: i64, id: i64) -> i64 { e[CW_W_MIN] = mn; e[CW_W_MAX] = mx; e[CW_W_TRIGGER] = t; e[CW_W_CAMERA] = c; e[CW_W_IDENTITY] = id; return 0 } 69 70func main() -> i64 { 71 gv_head("nx_gen_promptweight_gate -- G13: prompt weighting as data in the conf's own permil envelope; 1.000 is the byte-identical identity; out-of-envelope refuses; on vs off differ through the real composer" as *u8) 72 let ctr: *i64 = gv_ctr() 73 74 // --- the live conf, read through the real loader with NO scratch conf planted yet (relative path absent) --- 75 let live: *i64 = sys_mmap(CW_W_BYTES) as *i64 76 let lg: i64 = cw_weights_load(live) 77 gv_check("live-conf-readable-and-envelope-sane" as *u8, (lg >= 2) & (live[CW_W_MIN] < live[CW_W_MAX]), ctr) 78 79 // --- the verb, against the documented envelope --- 80 let env: *i64 = sys_mmap(CW_W_BYTES) as *i64 81 cw_weights_parse(0 as *u8, 0, env) 82 gv_check("empty-conf-yields-documented-envelope-and-identity-slots" as *u8, (env[CW_W_MIN] == 100) & (env[CW_W_MAX] == 2000) & (env[CW_W_TRIGGER] == 1000) & (env[CW_W_CAMERA] == 1000) & (env[CW_W_IDENTITY] == 1000), ctr) 83 let buf: *u8 = sys_mmap(PW_CAP) 84 pw_fill(buf) 85 let n1000: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 1000, env) 86 gv_check("weight-1000-emits-the-bare-text-identity" as *u8, pw_eq(buf, n1000, "alpha" as *u8) == 1, ctr) 87 let b13: *u8 = sys_mmap(PW_CAP) 88 let n13: i64 = cw_prompt_weight(b13, 0, "alpha" as *u8, 1300, env) 89 gv_check("weight-1300-emits-a1111-syntax-three-decimals" as *u8, pw_eq(b13, n13, "(alpha:1.300)" as *u8) == 1, ctr) 90 gv_check("on-vs-off-emit-byte-different-text" as *u8, pw_same(buf, n1000, b13, n13) == 0, ctr) 91 let n950: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 950, env) 92 gv_check("weight-950-formats-a-leading-zero" as *u8, pw_eq(buf, n950, "(alpha:0.950)" as *u8) == 1, ctr) 93 let nmin: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 100, env) 94 gv_check("weight-at-envelope-min-is-admitted" as *u8, pw_eq(buf, nmin, "(alpha:0.100)" as *u8) == 1, ctr) 95 let nmax: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 2000, env) 96 gv_check("weight-at-envelope-max-is-admitted" as *u8, pw_eq(buf, nmax, "(alpha:2.000)" as *u8) == 1, ctr) 97 pw_fill(buf) 98 let rlo: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 50, env) 99 gv_check("neg-control-weight-below-envelope-refuses-and-writes-nothing" as *u8, (rlo == CW_W_REFUSED) & ((buf[0] & 0xff) == PW_SENTINEL), ctr) 100 let rhi: i64 = cw_prompt_weight(buf, 0, "alpha" as *u8, 2500, env) 101 gv_check("neg-control-weight-above-envelope-refuses-and-writes-nothing" as *u8, (rhi == CW_W_REFUSED) & ((buf[0] & 0xff) == PW_SENTINEL), ctr) 102 let nempty: i64 = cw_prompt_weight(buf, 5, "" as *u8, 1300, env) 103 gv_check("empty-text-emits-nothing-even-when-weighted" as *u8, nempty == 5, ctr) 104 105 // --- a DATA slot out of envelope falls back to the bare slot, never a mutated one --- 106 let bad: *i64 = sys_mmap(CW_W_BYTES) as *i64 107 pw_env5(bad, 100, 2000, 5000, 1000, 1000) 108 let nslot: i64 = cw_weight_slot(buf, 0, "alpha" as *u8, bad, CW_W_TRIGGER) 109 gv_check("data-slot-out-of-envelope-falls-back-to-the-bare-slot" as *u8, pw_eq(buf, nslot, "alpha" as *u8) == 1, ctr) 110 let good: *i64 = sys_mmap(CW_W_BYTES) as *i64 111 pw_env5(good, 100, 2000, 1300, 1000, 1000) 112 let nslot2: i64 = cw_weight_slot(buf, 0, "alpha" as *u8, good, CW_W_TRIGGER) 113 gv_check("data-slot-inside-envelope-is-weighted" as *u8, pw_eq(buf, nslot2, "(alpha:1.300)" as *u8) == 1, ctr) 114 115 // --- the conf parser on the incumbent contract --- 116 let w: *i64 = sys_mmap(CW_W_BYTES) as *i64 117 let c2: *u8 = "100 2000\n# a comment\n" as *u8 118 let g2: i64 = cw_weights_parse(c2, pw_len(c2), w) 119 gv_check("envelope-only-conf-reads-min-max-and-leaves-slots-at-identity" as *u8, (g2 == 2) & (w[CW_W_MIN] == 100) & (w[CW_W_MAX] == 2000) & (w[CW_W_TRIGGER] == 1000) & (w[CW_W_CAMERA] == 1000) & (w[CW_W_IDENTITY] == 1000), ctr) 120 let c5: *u8 = "200 1800\n; slots\n1300\n900\n1050\n" as *u8 121 let g5: i64 = cw_weights_parse(c5, pw_len(c5), w) 122 gv_check("five-int-conf-keeps-order-and-skips-comment-lines" as *u8, (g5 == 5) & (w[CW_W_MIN] == 200) & (w[CW_W_MAX] == 1800) & (w[CW_W_TRIGGER] == 1300) & (w[CW_W_CAMERA] == 900) & (w[CW_W_IDENTITY] == 1050), ctr) 123 let cinv: *u8 = "2000 100\n" as *u8 124 let ginv: i64 = cw_weights_parse(cinv, pw_len(cinv), w) 125 gv_check("neg-control-inverted-envelope-restores-the-documented-envelope" as *u8, (ginv == 2) & (w[CW_W_MIN] == 100) & (w[CW_W_MAX] == 2000), ctr) 126 let c1: *u8 = "150\n" as *u8 127 let g1: i64 = cw_weights_parse(c1, pw_len(c1), w) 128 gv_check("neg-control-single-int-conf-keeps-the-documented-envelope" as *u8, (g1 == 1) & (w[CW_W_MIN] == 100) & (w[CW_W_MAX] == 2000), ctr) 129 let cb: *u8 = "abc\n" as *u8 130 let gb: i64 = cw_weights_parse(cb, pw_len(cb), w) 131 gv_check("neg-control-non-numeric-conf-lands-nothing" as *u8, (gb == 0) & (w[CW_W_MIN] == 100) & (w[CW_W_MAX] == 2000) & (w[CW_W_TRIGGER] == 1000), ctr) 132 133 // --- the free-text admission scanner (user-written groups) --- 134 let badat: *i64 = sys_mmap(16) as *i64 135 let badw: *i64 = sys_mmap(16) as *i64 136 let s1: *u8 = "a (skin:1.3) c" as *u8 137 gv_check("admit-counts-one-in-envelope-group" as *u8, cw_weight_admit(s1, pw_len(s1), env, badat, badw) == 1, ctr) 138 let s2: *u8 = "a (skin:13) c" as *u8 139 badat[0] = 0 - 1; badw[0] = 0 140 let a2: i64 = cw_weight_admit(s2, pw_len(s2), env, badat, badw) 141 gv_check("neg-control-admit-refuses-the-typo-group-and-names-its-permil-and-byte" as *u8, (a2 == CW_W_REFUSED) & (badw[0] == 13000) & (badat[0] == 7), ctr) 142 let s3: *u8 = "the time is 12:30 outside any group" as *u8 143 gv_check("admit-ignores-a-colon-outside-any-group" as *u8, cw_weight_admit(s3, pw_len(s3), env, badat, badw) == 0, ctr) 144 let s4: *u8 = "((a:1.1) (b:2.5))" as *u8 145 let a4: i64 = cw_weight_admit(s4, pw_len(s4), env, badat, badw) 146 gv_check("admit-walks-nested-groups-and-refuses-the-second-over-max" as *u8, (a4 == CW_W_REFUSED) & (badw[0] == 2500), ctr) 147 let s5: *u8 = "(a:.5) and (b:2)" as *u8 148 gv_check("admit-parses-a-bare-fraction-and-a-bare-integer" as *u8, cw_weight_admit(s5, pw_len(s5), env, badat, badw) == 2, ctr) 149 let s6: *u8 = "x(lit:9)" as *u8 150 let esc: *u8 = sys_mmap(64) 151 var ei: i64 = 0 152 while s6[ei] != (0 as u8) { esc[ei] = s6[ei]; ei = ei + 1 } 153 esc[0] = CW_CH_BACKSLASH as u8 154 gv_check("admit-treats-a-backslash-escaped-paren-as-literal" as *u8, cw_weight_admit(esc, ei, env, badat, badw) == 0, ctr) 155 gv_check("fixture-reached-condition-unescaped-twin-of-that-group-would-refuse" as *u8, cw_weight_admit(s6, pw_len(s6), env, badat, badw) == CW_W_REFUSED, ctr) 156 157 // --- compose-level, through the REAL composer from a scratch cwd --- 158 sys_mkdir(PW_DIR, PW_MODE_DIR) 159 sys_mkdir(PW_KDIR, PW_MODE_DIR) 160 let tw: i64 = pw_write(PW_TRIG, PW_TRIGGER_WORD) 161 let cd: i64 = sys_chdir(PW_DIR) 162 gv_check("fixture-reached-condition-scratch-cwd-with-planted-trigger" as *u8, (tw > 0) & (cd == 0), ctr) 163 let wo: i64 = pw_write(PW_CONF, PW_CONF_OFF) 164 let a: *u8 = sys_mmap(PW_CAP) 165 let na: i64 = pw_compose(a) 166 gv_check("compose-off-carries-the-bare-trigger-first" as *u8, (wo > 0) & (na > 0) & (pw_find(a, na, "rawphotoword, " as *u8) == 0), ctr) 167 gv_check("compose-off-carries-no-weight-syntax-on-the-trigger" as *u8, pw_find(a, na, "(rawphotoword" as *u8) < 0, ctr) 168 let wn: i64 = pw_write(PW_CONF, PW_CONF_ON) 169 let b: *u8 = sys_mmap(PW_CAP) 170 let nb: i64 = pw_compose(b) 171 gv_check("compose-on-carries-the-weighted-trigger-first" as *u8, (wn > 0) & (nb > 0) & (pw_find(b, nb, "(rawphotoword:1.300), " as *u8) == 0), ctr) 172 gv_check("compose-on-vs-off-prompts-are-byte-different" as *u8, pw_same(a, na, b, nb) == 0, ctr) 173 let wb: i64 = pw_write(PW_CONF, PW_CONF_BAD) 174 let d: *u8 = sys_mmap(PW_CAP) 175 let nd: i64 = pw_compose(d) 176 gv_check("compose-with-a-data-slot-out-of-envelope-reproduces-the-off-bytes" as *u8, (wb > 0) & (pw_same(a, na, d, nd) == 1), ctr) 177 let wo2: i64 = pw_write(PW_CONF, PW_CONF_OFF) 178 let c: *u8 = sys_mmap(PW_CAP) 179 let nc: i64 = pw_compose(c) 180 gv_check("setting-the-conf-back-reproduces-the-off-bytes-exactly" as *u8, (wo2 > 0) & (pw_same(a, na, c, nc) == 1), ctr) 181 182 return gv_verdict("GEN-PROMPTWEIGHT-GATE" as *u8, ctr, "G13 weighting half on the conf's own permil envelope; wildcard expansion is the owed half" as *u8) 183}