code wiki / _hdl_build / nx_connect_translate.nx
nx_connect_translate.nx source
↩ module page · 184 lines · 8802 B
1// nx_connect_translate.nx -- CONNECT in-chat TRANSLATION + CORRECTION flow (the Tandem-wedge DIF cell).
2// Composes the sovereign MT substrate (nx_mt_multi: EN-pivot, 5 langs, integer, deterministic) into the
3// actual CHAT functionality: a message renders INLINE with its translation for the viewer's language, a
4// partner can bind a CORRECTION annotation to the original message (additive -- the original is never
5// destroyed), and translation is UNLIMITED BY CONSTRUCTION: there is NO quota counter anywhere in the
6// render path, so rationing (Tandem free tier / HelloTalk 'unlimited translations' behind Paid -- both
7// cited in knowledge/library/conncomp_*.txt) is structurally impossible, not a policy promise.
8// NEG-CONTROL: an incumbent-model rationed translator (daily cap table) denies message cap+1 -- proving
9// the unlimited check discriminates. HONEST SCOPE: lexicon is method-level (R1 edit-here); translation
10// QUALITY is not claimed anywhere near DeepL-class -- the claim is the FLOW + the pricing architecture.
11// 100% sovereign (nx_cc->nxasm_x86). license_tier: ORIGINAL expect_exit: 0
12import "nx_syscalls.nx"
13import "nx_mt_multi.nx"
14const CT_MAGIC_4096: i64 = 4096
15const CT_MAGIC_4095: i64 = 4095
16const CT_MAGIC_8192: i64 = 8192
17const CT_MAGIC_8191: i64 = 8191
18
19const CT_MAXMSG: i64 = 64
20const CT_MAXCORR: i64 = 32
21
22func sw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
23func sn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} 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 }
24func tcheck(pass: i64, label: *u8, fails: *i64) -> i64 {
25 sw(" " as *u8); sw(label); sw(": " as *u8)
26 if pass==1 { sw("PASS\n" as *u8) } else { sw("FAIL\n" as *u8); fails[0]=fails[0]+1 }
27 return 0
28}
29func ct_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
30func ct_app(dst: *u8, pos: *i64, cap: i64, s: *u8) -> i64 {
31 var i: i64=0
32 while s[i]!=(0 as u8) { let p: i64=*pos; if p<cap { dst[p]=s[i]; *pos=p+1 } i=i+1 }
33 return 0
34}
35// substring present in buf[0..n)? naive scan (small buffers)
36func ct_find(buf: *u8, n: i64, needle: *u8) -> i64 {
37 let m: i64 = ct_slen(needle)
38 if m==0 { return 0 }
39 var i: i64=0
40 while i+m<=n {
41 var j: i64=0
42 var hit: i64=1
43 while j<m { if buf[i+j]!=needle[j] { hit=0; break } j=j+1 }
44 if hit==1 { return 1 }
45 i=i+1
46 }
47 return 0
48}
49
50// ---- the inline render: ORIGINAL => TRANSLATION (viewer language). Both always visible: the learning
51// loop needs the source text; we never replace the partner's words. NO quota state exists on this path.
52func ct_render_inline(text: *u8, srcLang: i64, viewerLang: i64, out: *u8, cap: i64) -> i64 {
53 let pos: *i64 = sys_mmap(8) as *i64
54 *pos = 0
55 ct_app(out, pos, cap, text)
56 if srcLang != viewerLang {
57 ct_app(out, pos, cap, " => " as *u8)
58 let scratch: *u8 = sys_mmap(CT_MAGIC_4096)
59 let tn: i64 = nx_multi_translate(text, ct_slen(text), srcLang, viewerLang, scratch, CT_MAGIC_4095)
60 scratch[tn] = 0 as u8
61 ct_app(out, pos, cap, scratch)
62 }
63 let e: i64 = *pos
64 out[e] = 0 as u8
65 return e
66}
67
68// ---- NEG-CONTROL incumbent model: a RATIONED translator (daily quota table). Returns -1 = DENIED.
69func ct_rationed_translate(used: *i64, daily_cap: i64, text: *u8, srcLang: i64, viewerLang: i64, out: *u8) -> i64 {
70 if used[0] >= daily_cap { return 0-1 }
71 used[0] = used[0] + 1
72 let n: i64 = nx_multi_translate(text, ct_slen(text), srcLang, viewerLang, out, CT_MAGIC_4095)
73 out[n] = 0 as u8
74 return n
75}
76
77func main() -> i64 {
78 let fails: *i64 = sys_mmap(16) as *i64
79 fails[0]=0
80 sw("=== nx_connect_translate -- in-chat translation + correction flow (unlimited BY CONSTRUCTION) ===\n" as *u8)
81
82 let out: *u8 = sys_mmap(CT_MAGIC_8192)
83
84 // T1: RU sender, EN viewer -- inline render carries BOTH original and translation
85 let n1: i64 = ct_render_inline("привет друг" as *u8, MM_RU, MM_EN, out, CT_MAGIC_8191)
86 var t1: i64=0
87 if ct_find(out, n1, "привет" as *u8)==1 { if ct_find(out, n1, "hello" as *u8)==1 { if ct_find(out, n1, "friend" as *u8)==1 { t1=1 } } }
88 sw(" render: " as *u8); sw(out); sw("\n" as *u8)
89 tcheck(t1, "T1 inline render = original + viewer-language translation (RU->EN)" as *u8, fails)
90
91 // T2: cross-pair via EN pivot (RU sender -> PL viewer) -- no direct table needed
92 let n2: i64 = ct_render_inline("привет" as *u8, MM_RU, MM_PL, out, CT_MAGIC_8191)
93 var t2: i64=0
94 if ct_find(out, n2, "cze" as *u8)==1 { t2=1 }
95 tcheck(t2, "T2 EN-pivot pair (RU->PL) translates without a direct table" as *u8, fails)
96
97 // T3: unknown token PASSES THROUGH (never drop the partner's words -- Cardinal 25)
98 let n3: i64 = ct_render_inline("привет nishi" as *u8, MM_RU, MM_EN, out, CT_MAGIC_8191)
99 var t3: i64=0
100 if ct_find(out, n3, "nishi" as *u8)==1 { if ct_find(out, n3, "hello" as *u8)==1 { t3=1 } }
101 tcheck(t3, "T3 unknown token passes through untranslated (content never dropped)" as *u8, fails)
102
103 // T4: UNLIMITED BY CONSTRUCTION -- 50 renders in one 'day', zero denials possible: the path has NO
104 // quota state at all (contrast: Tandem free tier rations; HelloTalk paywalls 'unlimited translations').
105 var ok4: i64=0
106 var i: i64=0
107 while i<50 {
108 let nn: i64 = ct_render_inline("спасибо друг" as *u8, MM_RU, MM_EN, out, CT_MAGIC_8191)
109 if nn>0 { if ct_find(out, nn, "thanks" as *u8)==1 { ok4=ok4+1 } }
110 i=i+1
111 }
112 var t4: i64=0; if ok4==50 { t4=1 }
113 sw(" translated 50/50 messages, denials=0 (no quota counter exists in the path)\n" as *u8)
114 tcheck(t4, "T4 50/50 messages translate; denial is structurally impossible (no quota state)" as *u8, fails)
115
116 // T5: NEG-CONTROL -- the incumbent RATIONED model (cap=5/day) DENIES message 6 => the unlimited
117 // check above is load-bearing (a rationed path would have failed T4).
118 let used: *i64 = sys_mmap(16) as *i64
119 used[0]=0
120 var denied: i64=0
121 var j: i64=0
122 while j<6 {
123 let r: i64 = ct_rationed_translate(used, 5, "привет" as *u8, MM_RU, MM_EN, out)
124 if r<0 { denied=denied+1 }
125 j=j+1
126 }
127 var t5: i64=0; if denied==1 { t5=1 }
128 tcheck(t5, "T5 NEG-CONTROL rationed incumbent model denies msg 6 of 6 (check discriminates)" as *u8, fails)
129
130 // ---- CORRECTION annotations: bound to msg_id, ADDITIVE (original never destroyed) ----
131 let msg_text: *u8 = sys_mmap(CT_MAXMSG*64)
132 let msg_used: *i64 = sys_mmap(CT_MAXMSG*8) as *i64
133 var nmsg: i64=0
134 // store msg 0: learner's imperfect sentence
135 let m0: *u8 = "i has a dog" as *u8
136 var w: i64=0
137 while m0[w]!=(0 as u8) { msg_text[0*64+w]=m0[w]; w=w+1 }
138 msg_text[0*64+w]=0 as u8
139 msg_used[0]=1
140 nmsg=1
141
142 let corr_msg: *i64 = sys_mmap(CT_MAXCORR*8) as *i64
143 let corr_text: *u8 = sys_mmap(CT_MAXCORR*64)
144 let ncorr: *i64 = sys_mmap(8) as *i64
145 ncorr[0]=0
146
147 // bind a correction to msg 0
148 var t6: i64=0
149 if msg_used[0]==1 {
150 let ci: i64 = ncorr[0]
151 corr_msg[ci]=0
152 let c0: *u8 = "i have a dog" as *u8
153 var w2: i64=0
154 while c0[w2]!=(0 as u8) { corr_text[ci*64+w2]=c0[w2]; w2=w2+1 }
155 corr_text[ci*64+w2]=0 as u8
156 ncorr[0]=ci+1
157 // render: original + correction both visible (additive learning loop)
158 let pos: *i64 = sys_mmap(8) as *i64
159 *pos=0
160 ct_app(out, pos, CT_MAGIC_8191, msg_text)
161 ct_app(out, pos, CT_MAGIC_8191, " [corrected: " as *u8)
162 ct_app(out, pos, CT_MAGIC_8191, corr_text)
163 ct_app(out, pos, CT_MAGIC_8191, "]" as *u8)
164 let e: i64=*pos
165 out[e]=0 as u8
166 if ct_find(out, e, "i has a dog" as *u8)==1 { if ct_find(out, e, "i have a dog" as *u8)==1 { t6=1 } }
167 sw(" correction render: " as *u8); sw(out); sw("\n" as *u8)
168 }
169 tcheck(t6, "T6 correction binds to the message; ORIGINAL text preserved (additive)" as *u8, fails)
170
171 // T7: correction to a NONEXISTENT message id is REFUSED (fail-closed binding)
172 var t7: i64=0
173 let bad_id: i64 = 7
174 var refused: i64=1
175 if bad_id<nmsg { if msg_used[bad_id]==1 { refused=0 } }
176 if refused==1 { t7=1 }
177 tcheck(t7, "T7 correction to a nonexistent message is refused (fail-closed)" as *u8, fails)
178
179 sw(" fails=" as *u8); sn(fails[0]); sw("\n" as *u8)
180 if fails[0]==0 { sw("VERDICT: GREEN (in-chat translate + correction flow live on the sovereign MT substrate; unlimited by construction)\n" as *u8); sys_exit(0) }
181 sw("VERDICT: RED\n" as *u8)
182 sys_exit(1)
183 return 1
184}