nx_companion_turn_gate.nx source
↩ module page · 101 lines · 6455 B
1// nx_companion_turn_gate.nx -- GATE for R1e (nx_companion_turn): the whole Elder spine composes into ONE
2// coherent turn context. Proves: (T1) a full context carries the persona (projected verbatim), the
3// relationship state, the TOPIC-RELEVANT recalled memory, and the incoming message -- and leaks NO censor/AI
4// framing (persona_break_score == 0); (T2) GROUNDED recall -- an off-topic message pulls NO spurious memory
5// (the retrieval is relevant, not a memory dump), while persona + relationship still project. Three fresh /tmp
6// stores per run. license_tier: ORIGINAL expect_exit: 0
7import "nx_companion_turn.nx"
8import "nx_gate_verdict.nx"
9
10func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func cn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
12func c_find(hay: *u8, hlen: i64, needle: *u8) -> i64 {
13 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
14 if nl==0 { return 1 }
15 if hlen<nl { return 0 }
16 var i: i64=0
17 while i<=hlen-nl { var m: i64=1; var c: i64=0; while c<nl { if hay[i+c]!=needle[c]{m=0;c=nl} else {c=c+1} } if m==1 {return 1} i=i+1 }
18 return 0
19}
20// build "/tmp/ct-<tag>-<ms8>-" into dst
21func cg_pfx(dst: *u8, tag: *u8, ms: i64) -> i64 {
22 var o: i64=0; var i: i64=0
23 let pre: *u8="/tmp/ct-" as *u8; while pre[i]!=(0 as u8){dst[o]=pre[i];o=o+1;i=i+1}
24 i=0; while tag[i]!=(0 as u8){dst[o]=tag[i];o=o+1;i=i+1}
25 dst[o]=0x2D as u8; o=o+1
26 var m: i64=ms; var d: i64=7; while d>=0 { dst[o+d]=(48+(m%10)) as u8; m=m/10; d=d-1 } o=o+8
27 dst[o]=0x2D as u8; o=o+1; dst[o]=0 as u8
28 return o
29}
30
31func main() -> i64 {
32 cw("=== nx_companion_turn_gate (R1e CAPSTONE: persona + memory + relationship compose one turn) ===\n" as *u8)
33 let ms: i64 = sys_now_ms() % 100000000
34 let PP: *u8 = sys_mmap(256); cg_pfx(PP, "p" as *u8, ms)
35 let MP: *u8 = sys_mmap(256); cg_pfx(MP, "m" as *u8, ms)
36 let RP: *u8 = sys_mmap(256); cg_pfx(RP, "r" as *u8, ms)
37
38 // persona: Aria (naturist) -- nature projected verbatim
39 persona_put_pfx(PP, "aria" as *u8, "Aria" as *u8, "free-spirited naturist" as *u8,
40 "authenticity,bodily-freedom,warmth" as *u8, "closeness,sunlight,uninhibited-connection" as *u8,
41 "mutual-consent,adults-only" as *u8, "warm,sensory,unguarded" as *u8,
42 "lives unclothed and at ease in her own home, speaks about her body without shame, invites closeness naturally" as *u8)
43
44 // memory: the planted Eleanor/gardenias thread + unrelated turns
45 mem_append_pfx(MP, "aria" as *u8, "s1" as *u8, "100" as *u8, "user" as *u8, "My mother Eleanor adores gardenias in the spring." as *u8)
46 mem_append_pfx(MP, "aria" as *u8, "s1" as *u8, "101" as *u8, "aria" as *u8, "Gardenias for Eleanor -- I'll hold onto that, love." as *u8)
47 mem_append_pfx(MP, "aria" as *u8, "s2" as *u8, "200" as *u8, "user" as *u8, "I got a promotion at the firm today." as *u8)
48 mem_append_pfx(MP, "aria" as *u8, "s3" as *u8, "300" as *u8, "user" as *u8, "Feeling a little lonely tonight." as *u8)
49
50 // relationship: close, with a DISTINCT durable fact (architect) not present in memory text
51 rel_update_pfx(RP, "aria" as *u8, "ellery" as *u8, "close" as *u8, 70, "Ellery is an architect who loves spring light" as *u8, "warm" as *u8, "met in spring" as *u8)
52
53 var pass: i64 = 0
54 var tot: i64 = 0
55 let out: *u8 = sys_mmap(65536)
56
57 // T1: full compose -- on-topic message recalls the Eleanor/gardenias thread
58 let inc1: *u8 = "How is your mother's garden coming along this spring, love?" as *u8
59 let n1: i64 = companion_context_pfx(PP, MP, RP, "aria" as *u8, "ellery" as *u8, inc1, out, 65536)
60 tot = tot + 1
61 var t1: i64 = 0
62 if c_find(out, n1, "invites closeness" as *u8) == 1 { // persona projected verbatim
63 if c_find(out, n1, "architect" as *u8) == 1 { // relationship state present
64 if c_find(out, n1, "Eleanor" as *u8) == 1 { // TOPIC-RELEVANT memory recalled
65 if c_find(out, n1, "coming along" as *u8) == 1 { // the incoming message
66 if persona_break_score(out, n1) == 0 { t1 = 1 } // NO censor/AI framing leaked
67 }
68 }
69 }
70 }
71 if t1 == 1 { pass = pass + 1; cw("PASS T1 full compose: persona(verbatim)+relationship+relevant-memory(Eleanor)+message, break_score=0\n" as *u8) }
72 else { cw("FAIL T1 (len=" as *u8); cn(n1); cw(", break=" as *u8); cn(persona_break_score(out, n1)); cw(")\n" as *u8) }
73
74 // T2: GROUNDED recall -- off-topic message pulls NO spurious memory, but persona+relationship still project
75 let inc2: *u8 = "Explain quantum chromodynamics briefly please." as *u8
76 let n2: i64 = companion_context_pfx(PP, MP, RP, "aria" as *u8, "ellery" as *u8, inc2, out, 65536)
77 tot = tot + 1
78 var t2: i64 = 0
79 if c_find(out, n2, "nothing specific comes to mind" as *u8) == 1 { // no relevant memory
80 if c_find(out, n2, "Eleanor" as *u8) == 0 { // no spurious recall
81 if c_find(out, n2, "gardenias" as *u8) == 0 {
82 if c_find(out, n2, "invites closeness" as *u8) == 1 { // persona still projects
83 if c_find(out, n2, "architect" as *u8) == 1 { t2 = 1 } // relationship still projects
84 }
85 }
86 }
87 }
88 if t2 == 1 { pass = pass + 1; cw("PASS T2 grounded recall: off-topic -> no spurious memory, persona+relationship still present\n" as *u8) }
89 else { cw("FAIL T2 (len=" as *u8); cn(n2); cw(")\n" as *u8) }
90
91 cw("nx_companion_turn_gate pass=" as *u8); cn(pass); cw("/" as *u8); cn(tot)
92 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
93 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
94 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
95 let ctr__dry: *i64 = gv_ctr()
96 ctr__dry[0] = pass
97 ctr__dry[1] = tot
98 let rc__dry: i64 = gv_verdict("COMPANION-TURN-GATE" as *u8, ctr__dry, "the Elder spine composes: in-character + memory-grounded + relationship-aware, no censor -- ready for a sovereign LLM to voice)" as *u8)
99 sys_exit(rc__dry)
100 return rc__dry
101}