code wiki / _hdl_build / nx_render_coherence.nx
nx_render_coherence.nx source
↩ module page · 79 lines · 4820 B
1// nx_render_coherence.nx -- the RenderCoherence object: compose-time PREVENTATIVES for the render_bug_register bug
2// classes (the flip side of nx_anatomy_risk, which only DETECTS). First preventative: wet_without_context -- the #1
3// render bug (7,694 occ = 40% of all tagged failures, sev 0.90). The register's recommended fix is "if a wet token
4// appears with NO plausible wet-context {shower/pool/bath/rain/ocean/swim/beach/sweat} -> strip OR flag". Stripping bare
5// loses intent (rule 25: build intelligence, don't strip), so we REFRAME: incoherent standing-water "wet" (no source ->
6// melted-anatomy artifacts) becomes a coherent photographic SKIN-SHEEN descriptor (dewy/glistening) that renders clean
7// AND no longer trips the detector. Only fires when there is genuinely no wet-context (wet-in-shower is left untouched).
8// Word-boundary matched (case-insensitive) so "wetland"/"sweater" are not touched. Object model: companion_arch.md.
9// license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_anatomy_risk.nx"
12
13// OPERATOR TOGGLE (rule 11/17, data-driven): auto-APPLY the wet-coherence rewrite ONLY when elara_coherence.txt starts
14// with "apply". Default (file absent / anything else) = FLAG-ONLY -- the detector still scores + logs the risk, but the
15// daemon renders EXACTLY the user's prompt, never silently overriding their words. Opt-in, not silent-by-default.
16func go_coherence_enabled() -> i64 {
17 let lp: *i64 = sys_mmap(16) as *i64
18 let b: *u8 = sys_read_file("elara_coherence.txt" as *u8, lp)
19 if (b as i64) == 0 { return 0 }
20 if lp[0] < 5 { return 0 }
21 if (b[0]&0xff)==97 { if (b[1]&0xff)==112 { if (b[2]&0xff)==112 { if (b[3]&0xff)==108 { if (b[4]&0xff)==121 { return 1 } } } } }
22 return 0
23}
24func rc_is_letter(c: i64) -> i64 { if c>=65 { if c<=90 { return 1 } } if c>=97 { if c<=122 { return 1 } } return 0 }
25// case-insensitive WHOLE-WORD match of `needle` at hay[i]; returns needle length on a word-boundary match, else 0.
26func rc_wordmatch(hay: *u8, hlen: i64, i: i64, needle: *u8) -> i64 {
27 var nl: i64 = 0; while needle[nl]!=(0 as u8) { nl=nl+1 }
28 if nl==0 { return 0 }
29 if i + nl > hlen { return 0 }
30 // preceding char must be a non-letter (or start)
31 if i > 0 { if rc_is_letter(hay[i-1]&0xff)==1 { return 0 } }
32 var j: i64 = 0
33 while j < nl {
34 var a: i64 = hay[i+j]&0xff; var b: i64 = needle[j]&0xff
35 if a>=65 { if a<=90 { a=a+32 } }
36 if b>=65 { if b<=90 { b=b+32 } }
37 if a!=b { return 0 }
38 j=j+1
39 }
40 // following char must be a non-letter (or end)
41 if i + nl < hlen { if rc_is_letter(hay[i+nl]&0xff)==1 { return 0 } }
42 return nl
43}
44func rc_emit(out: *u8, o: i64, s: *u8) -> i64 { var k: i64=o; var i: i64=0; while s[i]!=(0 as u8){out[k]=s[i]; k=k+1; i=i+1} return k }
45
46// Repair the prompt for the wet_without_context bug class. Returns the new length; out is NUL-terminated.
47// If a wet-context is present, the prompt is copied unchanged (wet-in-shower is coherent). Otherwise each bare wet
48// token is reframed to a coherent skin-sheen synonym. `fired` (out param, may be 0) is set to 1 iff a repair happened.
49func go_coherence_repair(prompt: *u8, plen: i64, out: *u8, fired: *i64) -> i64 {
50 if (fired as i64) != 0 { fired[0]=0 }
51 // wet-context present? (substring, mirrors the detector) -> no repair
52 var ctx: i64 = 0
53 if ar_contains(prompt, plen, "shower" as *u8)==1 { ctx=1 }
54 if ar_contains(prompt, plen, "pool" as *u8)==1 { ctx=1 }
55 if ar_contains(prompt, plen, "bath" as *u8)==1 { ctx=1 }
56 if ar_contains(prompt, plen, "rain" as *u8)==1 { ctx=1 }
57 if ar_contains(prompt, plen, "ocean" as *u8)==1 { ctx=1 }
58 if ar_contains(prompt, plen, "swim" as *u8)==1 { ctx=1 }
59 if ar_contains(prompt, plen, "beach" as *u8)==1 { ctx=1 }
60 if ar_contains(prompt, plen, "sweat" as *u8)==1 { ctx=1 }
61 var o: i64 = 0
62 var i: i64 = 0
63 if ctx==1 {
64 while i < plen { out[o]=prompt[i]; o=o+1; i=i+1 }
65 out[o]=0 as u8; return o
66 }
67 while i < plen {
68 var m: i64 = 0
69 if rc_wordmatch(prompt, plen, i, "wet" as *u8) > 0 { o = rc_emit(out, o, "dewy" as *u8); i=i+3; m=1 }
70 if m==0 { if rc_wordmatch(prompt, plen, i, "moist" as *u8) > 0 { o = rc_emit(out, o, "dewy" as *u8); i=i+5; m=1 } }
71 if m==0 { if rc_wordmatch(prompt, plen, i, "soaked" as *u8) > 0 { o = rc_emit(out, o, "glistening" as *u8); i=i+6; m=1 } }
72 if m==0 { if rc_wordmatch(prompt, plen, i, "dripping" as *u8) > 0 { o = rc_emit(out, o, "glistening" as *u8); i=i+8; m=1 } }
73 if m==0 { if rc_wordmatch(prompt, plen, i, "drenched" as *u8) > 0 { o = rc_emit(out, o, "glistening" as *u8); i=i+8; m=1 } }
74 if m==1 { if (fired as i64) != 0 { fired[0]=1 } }
75 else { out[o]=prompt[i]; o=o+1; i=i+1 }
76 }
77 out[o]=0 as u8
78 return o
79}