nx_identity_persist.nx source
↩ module page · 88 lines · 4219 B
1// nx_identity_persist.nx -- SOVEREIGN identity-persistence drift-catcher. The #1 old-system failure (census
2// BEHIND, disasters.json): a render can be great QUALITY but the WRONG PERSON ("canonical Elara is platinum
3// blonde; this is a brunette"). The old platform caught it with a triangulation panel = face_embedding +
4// hair_color + caption_check -> 0/3 PASS = drift. This is the sovereign, no-3rd-party version: at PROMPT stage
5// it runs the two TEXT checks (hair_color: no forbidden hair terms; caption: all required identity tokens
6// present) so a wrong-identity prompt is REJECTED BEFORE a gen is wasted -- this alone catches the exact brunette
7// error I made. The 3rd check (face_embedding on the RENDERED image) wires to nx_sovereign_image_fr (our own
8// LBP face recognition, no 3rd-party lib) post-render. Identity spec (required/forbidden tokens) is DATA per
9// persona (rule 11/25). license_tier: ORIGINAL
10import "nx_syscalls.nx"
11
12// verdict codes
13const IDP_PRESERVED: i64 = 2 // both text checks pass -> identity preserved (proceed)
14const IDP_DRIFT: i64 = 0 // a check failed -> DRIFT (reject/regenerate)
15
16func idp_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
17func idp_lc(c: u8) -> u8 { if c >= (65 as u8) { if c <= (90 as u8) { return (c + (32 as u8)) as u8 } } return c }
18// case-insensitive: does needle[0..nl) occur in hay[0..hlen)?
19func idp_ci_find(hay: *u8, hlen: i64, needle: *u8, nl: i64) -> i64 {
20 if nl == 0 { return 1 }
21 if hlen < nl { return 0 }
22 var i: i64 = 0
23 while i <= hlen - nl {
24 var m: i64 = 1; var c: i64 = 0
25 while c < nl { if idp_lc(hay[i+c]) != idp_lc(needle[c]) { m = 0; c = nl } else { c = c + 1 } }
26 if m == 1 { return 1 }
27 i = i + 1
28 }
29 return 0
30}
31// idx-th '|'-item of list: out2[0]=abs offset, out2[1]=len; 1 if found
32func idp_item_at(list: *u8, llen: i64, idx: i64, out2: *i64) -> i64 {
33 var f: i64 = 0; var s: i64 = 0; var i: i64 = 0
34 while i <= llen {
35 var sep: i64 = 0
36 if i == llen { sep = 1 } else { if list[i] == (0x7C as u8) { sep = 1 } }
37 if sep == 1 { if f == idx { out2[0] = s; out2[1] = i - s; return 1 } f = f + 1; s = i + 1 }
38 i = i + 1
39 }
40 return 0
41}
42func idp_item_count(list: *u8, llen: i64) -> i64 { if llen<=0 {return 0} var c: i64=1; var i: i64=0; while i<llen { if list[i]==(0x7C as u8){c=c+1} i=i+1 } return c }
43
44// ANY forbidden term present in text? (hair-color drift signal)
45func idp_list_any(text: *u8, tlen: i64, list: *u8) -> i64 {
46 let llen: i64 = idp_slen(list)
47 let n: i64 = idp_item_count(list, llen)
48 let it: *i64 = sys_mmap(16) as *i64
49 var i: i64 = 0
50 while i < n {
51 if idp_item_at(list, llen, i, it) == 1 {
52 if idp_ci_find(text, tlen, (list as i64 + it[0]) as *u8, it[1]) == 1 { return 1 }
53 }
54 i = i + 1
55 }
56 return 0
57}
58// ALL required tokens present in text? (caption / identity check)
59func idp_list_all(text: *u8, tlen: i64, list: *u8) -> i64 {
60 let llen: i64 = idp_slen(list)
61 let n: i64 = idp_item_count(list, llen)
62 let it: *i64 = sys_mmap(16) as *i64
63 var i: i64 = 0
64 while i < n {
65 if idp_item_at(list, llen, i, it) == 1 {
66 if idp_ci_find(text, tlen, (list as i64 + it[0]) as *u8, it[1]) == 0 { return 0 }
67 }
68 i = i + 1
69 }
70 return 1
71}
72
73// PROMPT-STAGE verdict: out2[0]=hair_ok (no forbidden hair term), out2[1]=caption_ok (all required present).
74// Returns hair_ok + caption_ok (0..2). 2 = identity PRESERVED (safe to render); <2 = DRIFT (reject/fix the prompt).
75func idp_prompt_verdict(prompt: *u8, plen: i64, required: *u8, forbidden: *u8, out2: *i64) -> i64 {
76 let hair_ok: i64 = 1 - idp_list_any(prompt, plen, forbidden) // 1 iff NO forbidden term
77 let cap_ok: i64 = idp_list_all(prompt, plen, required)
78 out2[0] = hair_ok
79 out2[1] = cap_ok
80 return hair_ok + cap_ok
81}
82
83// convenience: 1 if the prompt PRESERVES identity (both checks pass), else 0 (drift).
84func idp_prompt_ok(prompt: *u8, plen: i64, required: *u8, forbidden: *u8) -> i64 {
85 let o: *i64 = sys_mmap(16) as *i64
86 if idp_prompt_verdict(prompt, plen, required, forbidden, o) == 2 { return 1 }
87 return 0
88}