code wiki / _hdl_build / nx_brand_manage.nx
nx_brand_manage.nx source
↩ module page · 99 lines · 5021 B
1// nx_brand_manage.nx -- R4 MULTI-ACTOR brand management, no lock-in. Operator: "users + developers + AI-users (of
2// Claude) + local-LLM able to manage in lots of ways to NOT lock in." The unifying mechanism: ONE brand-OPS
3// language (line-based DATA any actor emits) applied through ONE governed mutator over the token SSOT:
4// set|<group>|<name>|<value> -- set/add a token
5// pack|<id> -- load a whole brand pack as the working base
6// TWO modes: lenient (dev/CLI: skip bad ops) and STRICT-GUARDED (the untrusted-proposer seam AI/local-LLM use:
7// any unsafe/unknown op rejects the WHOLE proposal, and the RESULT must still pass the R2 brand guard -- so a model
8// can neither inject (<script>/breakout) nor break the brand (sub-AA)). Because the mutator is PURE DATA->DATA, the
9// SAME ops yield BYTE-IDENTICAL output regardless of which actor sent them = no vendor/tool lock-in, and the base is
10// untouched = reversible by construction. REUSE: nx_brand_tokens (bt_set/bt_lookup), nx_brand_guard (bg_pair_contrast),
11// nx_brand_packs (bp_pack/bp_decode), nx_nishi_builder (nb_streq). 100% sovereign. license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_brand_tokens.nx"
14import "nx_brand_guard.nx"
15import "nx_brand_packs.nx"
16
17// copy src[s..e) into out as a nul-terminated string; return length.
18func bm_copy(src: *u8, s: i64, e: i64, out: *u8, cap: i64) -> i64 {
19 var o: i64 = 0
20 var i: i64 = s
21 while i < e { if o < cap - 1 { out[o] = src[i]; o = o + 1 } i = i + 1 }
22 out[o] = 0 as u8
23 return o
24}
25
26// apply a brand-OPS proposal to a base .brand. strict=1 -> reject the WHOLE proposal (return -1) on any unsafe/
27// malformed/unknown op (untrusted-proposer mode); strict=0 -> skip bad ops (lenient dev mode). Returns new length.
28func bm_apply_ops(base: *u8, bn: i64, ops: *u8, on: i64, out: *u8, cap: i64, strict: i64) -> i64 {
29 let wA: *u8 = sys_mmap(cap)
30 let wB: *u8 = sys_mmap(cap)
31 var clen: i64 = 0
32 var bi: i64 = 0
33 while bi < bn { if clen < cap - 1 { wA[clen] = base[bi]; clen = clen + 1 } bi = bi + 1 }
34 wA[clen] = 0 as u8
35 var cur: *u8 = wA
36 var nxt: *u8 = wB
37 let pipes: *i64 = sys_mmap(64) as *i64
38 let g: *u8 = sys_mmap(64)
39 let nm: *u8 = sys_mmap(64)
40 let v: *u8 = sys_mmap(256)
41 let id: *u8 = sys_mmap(64)
42 var oi: i64 = 0
43 while oi < on {
44 var le: i64 = oi
45 while le < on { if ops[le] == (10 as u8) { break } le = le + 1 }
46 if le > oi {
47 var np: i64 = 0
48 var k: i64 = oi
49 while k < le { if ops[k] == (124 as u8) { if np < 8 { pipes[np] = k; np = np + 1 } } k = k + 1 }
50 var handled: i64 = 0
51 if np >= 1 {
52 if bt_slice_eq(ops, oi, pipes[0], "set" as *u8, 3) == 1 {
53 if np >= 3 {
54 bm_copy(ops, pipes[0]+1, pipes[1], g, 64)
55 bm_copy(ops, pipes[1]+1, pipes[2], nm, 64)
56 bm_copy(ops, pipes[2]+1, le, v, 256)
57 let r: i64 = bt_set(cur, clen, g, nm, v, nxt, cap)
58 if r < 0 { if strict == 1 { return 0 - 1 } }
59 else { let t: *u8 = cur; cur = nxt; nxt = t; clen = r }
60 handled = 1
61 }
62 } else {
63 if bt_slice_eq(ops, oi, pipes[0], "pack" as *u8, 4) == 1 {
64 bm_copy(ops, pipes[0]+1, le, id, 64)
65 let tpl: *u8 = bp_pack(id)
66 if (tpl as i64) == 0 { if strict == 1 { return 0 - 1 } }
67 else { clen = bp_decode(tpl, cur, cap) }
68 handled = 1
69 }
70 }
71 }
72 if handled == 0 { if strict == 1 { return 0 - 1 } } // malformed/unknown op
73 }
74 oi = le + 1
75 }
76 var p: i64 = 0
77 var j: i64 = 0
78 while j < clen { if p < cap - 1 { out[p] = cur[j]; p = p + 1 } j = j + 1 }
79 out[p] = 0 as u8
80 return p
81}
82
83// THE AI / LOCAL-LLM SEAM: an UNTRUSTED proposal is applied strictly (any unsafe/unknown op -> reject) AND the
84// result must still pass the R2 brand guard (text/bg pairs stay >= WCAG AA) -- so a model can neither inject nor
85// break the brand. Returns new length, -1 if a bad op was proposed, -2 if the result would break the brand.
86func bm_propose(base: *u8, bn: i64, ops: *u8, on: i64, out: *u8, cap: i64) -> i64 {
87 let tmp: *u8 = sys_mmap(cap)
88 let r: i64 = bm_apply_ops(base, bn, ops, on, tmp, cap, 1)
89 if r < 0 { return 0 - 1 }
90 let c1: i64 = bg_pair_contrast(tmp, r, "color" as *u8, "ink" as *u8, "color" as *u8, "bg" as *u8)
91 if c1 >= 0 { if c1 < 450 { return 0 - 2 } }
92 let c2: i64 = bg_pair_contrast(tmp, r, "color" as *u8, "accent-ink" as *u8, "color" as *u8, "accent" as *u8)
93 if c2 >= 0 { if c2 < 450 { return 0 - 2 } }
94 var p: i64 = 0
95 var j: i64 = 0
96 while j < r { if p < cap - 1 { out[p] = tmp[j]; p = p + 1 } j = j + 1 }
97 out[p] = 0 as u8
98 return r
99}