interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / translate / lib.om

2175 lines · 40887 bytes Raw Blame History
1note apps/translate/lib.om: pure-Omega translation service core (converted from aien-translate).
2note Composable OM-2 library unit (no om2 line, no fn main).
3note Compose with: si/directives/om2.om om/tls/sha2.om apps/translate/lib.om <main.om|test.om>
4note
5note Implements:
6note - ISO-639 and translate.js allowlist validator for all 31 supported languages + auto source
7note - Strict RFC 3629 UTF-8 validator, codepoint counter, and NUL/size payload guards
8note - UTF-8-aware paragraph/sentence/word chunker (never splits multi-byte code points)
9note - Lead/core/trail splitter preserving whitespace and split URL edge fragments (split_ws)
10note - Protected-token & keep-list translation guard (URLs, code spans, placeholders, tags, keep words)
11note - Deterministic SHA-256 keyed bounded LRU cache (keyed by prompt_ver/model/src/dst/text)
12note - Strict JSONL request parser and escaped JSONL response serializer
13
14const TR_MAX_BYTES ulong 16384
15const TR_MAX_CHARS ulong 3000
16const TR_MAX_TEXTS ulong 40
17const TR_MAX_CHUNKS ulong 64
18
19const TR_LANG_COUNT ulong 31
20const TR_LANG_AUTO ulong 31
21const TR_LANG_INVALID ulong 255
22
23const TR_OK ulong 0
24const TR_ERR_EMPTY ulong 1
25const TR_ERR_OVERSIZE ulong 2
26const TR_ERR_UTF8 ulong 3
27const TR_ERR_NUL ulong 4
28const TR_ERR_LANG ulong 5
29const TR_ERR_JSON ulong 6
30const TR_ERR_GUARD ulong 7
31const TR_ERR_CHUNK ulong 8
32
33const TR_CACHE_MAX_SLOTS ulong 32
34const TR_CACHE_VAL_CAP ulong 512
35
36note Cache globals (bounded LRU, full 32-byte SHA-256 keys)
37global tr_g_cache_cap ulong 1
38global tr_g_cache_used ulong 32
39global tr_g_cache_tick ulong 32
40global tr_g_cache_clock ulong 1
41global tr_g_cache_key uchar 1024
42global tr_g_cache_src ulong 32
43global tr_g_cache_dst ulong 32
44global tr_g_cache_vlen ulong 32
45global tr_g_cache_val uchar 16384
46global tr_g_cache_hits ulong 1
47global tr_g_cache_misses ulong 1
48global tr_g_cache_evicts ulong 1
49global tr_g_cache_cnt ulong 1
50
51note Chunker & split_ws output globals
52global tr_g_chk_off ulong 64
53global tr_g_chk_len ulong 64
54global tr_g_chk_cnt ulong 1
55global tr_g_sw_lead ulong 1
56global tr_g_sw_core_end ulong 1
57
58note Parsed request globals
59global tr_g_req_from uchar 64
60global tr_g_req_from_len ulong 1
61global tr_g_req_to uchar 64
62global tr_g_req_to_len ulong 1
63global tr_g_req_model uchar 64
64global tr_g_req_model_len ulong 1
65global tr_g_req_text uchar 16385
66global tr_g_req_text_len ulong 1
67global tr_g_req_maxc ulong 1
68
69note Scratch & JSONL response globals
70global tr_g_key_buf uchar 32
71global tr_g_tmp_out uchar 1024
72global tr_g_tmp_out_len ulong 1
73global tr_g_resp uchar 32768
74global tr_g_resp_len ulong 1
75global tr_g_item_buf uchar 1024
76global tr_g_item_len ulong 1
77
78note Prompt version & default model
79text tr_t_pver "p6"
80text tr_t_defmodel "aien-local"
81text tr_t_auto "auto"
82
83note 31 translate.js language ids (exact match to upstream src/langs.rs)
84text tr_lid_0 "english"
85text tr_lid_1 "chinese_simplified"
86text tr_lid_2 "chinese_traditional"
87text tr_lid_3 "japanese"
88text tr_lid_4 "korean"
89text tr_lid_5 "spanish"
90text tr_lid_6 "french"
91text tr_lid_7 "deutsch"
92text tr_lid_8 "portuguese"
93text tr_lid_9 "italian"
94text tr_lid_10 "russian"
95text tr_lid_11 "ukrainian"
96text tr_lid_12 "polish"
97text tr_lid_13 "dutch"
98text tr_lid_14 "swedish"
99text tr_lid_15 "danish"
100text tr_lid_16 "norwegian"
101text tr_lid_17 "finnish"
102text tr_lid_18 "czech"
103text tr_lid_19 "romanian"
104text tr_lid_20 "hungarian"
105text tr_lid_21 "greek"
106text tr_lid_22 "turkish"
107text tr_lid_23 "arabic"
108text tr_lid_24 "persian"
109text tr_lid_25 "hindi"
110text tr_lid_26 "bengali"
111text tr_lid_27 "thai"
112text tr_lid_28 "vietnamese"
113text tr_lid_29 "indonesian"
114text tr_lid_30 "malay"
115
116note 31 canonical ISO-639 codes (exact match to upstream src/langs.rs)
117text tr_iso_0 "en"
118text tr_iso_1 "zh-CN"
119text tr_iso_2 "zh-TW"
120text tr_iso_3 "ja"
121text tr_iso_4 "ko"
122text tr_iso_5 "es"
123text tr_iso_6 "fr"
124text tr_iso_7 "de"
125text tr_iso_8 "pt"
126text tr_iso_9 "it"
127text tr_iso_10 "ru"
128text tr_iso_11 "uk"
129text tr_iso_12 "pl"
130text tr_iso_13 "nl"
131text tr_iso_14 "sv"
132text tr_iso_15 "da"
133text tr_iso_16 "no"
134text tr_iso_17 "fi"
135text tr_iso_18 "cs"
136text tr_iso_19 "ro"
137text tr_iso_20 "hu"
138text tr_iso_21 "el"
139text tr_iso_22 "tr"
140text tr_iso_23 "ar"
141text tr_iso_24 "fa"
142text tr_iso_25 "hi"
143text tr_iso_26 "bn"
144text tr_iso_27 "th"
145text tr_iso_28 "vi"
146text tr_iso_29 "id"
147text tr_iso_30 "ms"
148
149note Built-in DEFAULT_KEEP terms from upstream src/lib.rs
150text tr_kp_0 "AIEN"
151text tr_kp_1 "AienOS"
152text tr_kp_2 "Aienux"
153text tr_kp_3 "AIENOS"
154text tr_kp_4 "Aienix"
155text tr_kp_5 "Aien"
156text tr_kp_6 "Omega"
157text tr_kp_7 "OMEGA"
158text tr_kp_8 "Interplane"
159text tr_kp_9 "Allen"
160text tr_kp_10 "Synapse"
161text tr_kp_11 "Atlas"
162text tr_kp_12 "Cortex"
163text tr_kp_13 "Radicle"
164text tr_kp_14 "Spark"
165text tr_kp_15 "DGX"
166text tr_kp_16 "GB10"
167text tr_kp_17 "NVIDIA"
168text tr_kp_18 "translate.js"
169text tr_kp_19 "GitHub"
170text tr_kp_20 "Rust"
171text tr_kp_21 "Linux"
172
173note JSON keys and fragments
174text tr_jk_from "from"
175text tr_jk_src "src"
176text tr_jk_to "to"
177text tr_jk_dst "dst"
178text tr_jk_text "text"
179text tr_jk_model "model"
180text tr_jk_maxc "max_chunk"
181text tr_t_http "http://"
182text tr_t_https "https://"
183text tr_t_https_col "https:"
184text tr_t_http_col "http:"
185text tr_t_hex "0123456789abcdef"
186
187text tr_e_empty "empty text"
188text tr_e_over "request payload oversize"
189text tr_e_utf8 "invalid utf-8 sequence"
190text tr_e_nul "nul byte refused"
191text tr_e_lang "unsupported language code"
192text tr_e_json "malformed json request"
193text tr_e_guard "translation guard failed"
194text tr_e_chunk "chunking refused"
195
196text tr_j_ok_pre "{\"result\":1,\"from\":\""
197text tr_j_to "\",\"to\":\""
198text tr_j_chunks "\",\"chunks\":"
199text tr_j_fc ",\"from_cache\":"
200text tr_j_ms ",\"misses\":"
201text tr_j_ev ",\"evictions\":"
202text tr_j_items ",\"items\":["
203text tr_j_end "]}\n"
204text tr_j_err_pre "{\"result\":0,\"code\":"
205text tr_j_err_msg ",\"error\":\""
206text tr_j_err_end "\"}\n"
207
208note --- Character & byte classification ---
209
210fn tr_ascii_lower b uchar to uchar
211if and ge b 65 le b 90
212ret add b 32
213end
214ret b
215end
216
217fn tr_is_ws b uchar to bool
218if eq b 32
219ret true
220end
221if eq b 9
222ret true
223end
224if eq b 10
225ret true
226end
227if eq b 13
228ret true
229end
230ret false
231end
232
233fn tr_is_digit b uchar to bool
234ret and ge b 48 le b 57
235end
236
237fn tr_is_alpha b uchar to bool
238if and ge b 65 le b 90
239ret true
240end
241if and ge b 97 le b 122
242ret true
243end
244ret false
245end
246
247fn tr_is_alnum b uchar to bool
248if tr_is_alpha b
249ret true
250end
251ret tr_is_digit b
252end
253
254fn tr_eq_ci_slice a view uchar * aoff ulong alen ulong b view uchar * to bool
255if ne alen len b
256ret false
257end
258i = ulong 0
259while lt i alen bound 64
260ca = tr_ascii_lower get a add aoff i
261cb = tr_ascii_lower get b i
262if ne ca cb
263ret false
264end
265i = add i 1
266end
267ret true
268end
269
270fn tr_eq_exact_slice a view uchar * aoff ulong alen ulong b view uchar * to bool
271if ne alen len b
272ret false
273end
274i = ulong 0
275while lt i alen bound 1024
276if ne get a add aoff i get b i
277ret false
278end
279i = add i 1
280end
281ret true
282end
283
284note Helper: compare hay[i..i+nlen] against ndl[0..nlen]
285fn tr_sub_match_at hay view uchar * i ulong ndl view uchar * nlen ulong to bool
286j = ulong 0
287while lt j nlen bound 1024
288if ne get hay add i j get ndl j
289ret false
290end
291j = add j 1
292end
293ret true
294end
295
296note Find exact substring ndl[0..nlen] inside hay[0..hlen]. Returns index or hlen+1 if absent.
297fn tr_find_sub hay view uchar * hlen ulong ndl view uchar * nlen ulong to ulong
298if eq nlen 0
299ret 0
300end
301if gt nlen hlen
302ret add hlen 1
303end
304lim = add sub hlen nlen 1
305i = ulong 0
306while lt i lim bound 16385
307if tr_sub_match_at hay i ndl nlen
308ret i
309end
310i = add i 1
311end
312ret add hlen 1
313end
314
315note Helper: compare hay[i..i+nlen] against ndl[noff..noff+nlen] (nlen from tr_g_item_len)
316fn tr_slice_match_at hay view uchar * i ulong ndl view uchar * noff ulong to bool
317nlen = get tr_g_item_len 0
318j = ulong 0
319while lt j nlen bound 1024
320a = get hay add i j
321b = get ndl add noff j
322if ne a b
323ret false
324end
325j = add j 1
326end
327ret true
328end
329
330note Find slice ndl[noff..noff+nlen] inside hay[0..hlen]. Returns true if found.
331fn tr_has_slice hay view uchar * hlen ulong ndl view uchar * noff ulong to bool
332nlen = get tr_g_item_len 0
333if eq nlen 0
334ret true
335end
336if gt nlen hlen
337ret false
338end
339lim = add sub hlen nlen 1
340i = ulong 0
341while lt i lim bound 16385
342if tr_slice_match_at hay i ndl noff
343ret true
344end
345i = add i 1
346end
347ret false
348end
349
350note Check if whole word ndl appears in hay[0..hlen] (not glued to ASCII alnum bytes).
351fn tr_contains_word hay view uchar * hlen ulong ndl view uchar * to bool
352nlen = len ndl
353if eq nlen 0
354ret false
355end
356if gt nlen hlen
357ret false
358end
359lim = add sub hlen nlen 1
360i = ulong 0
361while lt i lim bound 16385
362ok = true
363j = ulong 0
364while lt j nlen bound 64
365if ne get hay add i j get ndl j
366ok = false
367break
368end
369j = add j 1
370end
371if ok
372left_ok = true
373if gt i 0
374if tr_is_alnum get hay sub i 1
375left_ok = false
376end
377end
378right_ok = true
379ep = add i nlen
380if lt ep hlen
381if tr_is_alnum get hay ep
382right_ok = false
383end
384end
385if and left_ok right_ok
386ret true
387end
388end
389i = add i 1
390end
391ret false
392end
393
394note --- ISO-639 & translate.js Language Allowlist (all 31 upstream languages) ---
395
396fn tr_lang_match_pair buf view uchar * off ulong slen ulong idx ulong to bool
397if eq idx 0
398ret or tr_eq_ci_slice buf off slen tr_lid_0 tr_eq_ci_slice buf off slen tr_iso_0
399end
400if eq idx 1
401ret or tr_eq_ci_slice buf off slen tr_lid_1 tr_eq_ci_slice buf off slen tr_iso_1
402end
403if eq idx 2
404ret or tr_eq_ci_slice buf off slen tr_lid_2 tr_eq_ci_slice buf off slen tr_iso_2
405end
406if eq idx 3
407ret or tr_eq_ci_slice buf off slen tr_lid_3 tr_eq_ci_slice buf off slen tr_iso_3
408end
409if eq idx 4
410ret or tr_eq_ci_slice buf off slen tr_lid_4 tr_eq_ci_slice buf off slen tr_iso_4
411end
412if eq idx 5
413ret or tr_eq_ci_slice buf off slen tr_lid_5 tr_eq_ci_slice buf off slen tr_iso_5
414end
415if eq idx 6
416ret or tr_eq_ci_slice buf off slen tr_lid_6 tr_eq_ci_slice buf off slen tr_iso_6
417end
418if eq idx 7
419ret or tr_eq_ci_slice buf off slen tr_lid_7 tr_eq_ci_slice buf off slen tr_iso_7
420end
421if eq idx 8
422ret or tr_eq_ci_slice buf off slen tr_lid_8 tr_eq_ci_slice buf off slen tr_iso_8
423end
424if eq idx 9
425ret or tr_eq_ci_slice buf off slen tr_lid_9 tr_eq_ci_slice buf off slen tr_iso_9
426end
427if eq idx 10
428ret or tr_eq_ci_slice buf off slen tr_lid_10 tr_eq_ci_slice buf off slen tr_iso_10
429end
430if eq idx 11
431ret or tr_eq_ci_slice buf off slen tr_lid_11 tr_eq_ci_slice buf off slen tr_iso_11
432end
433if eq idx 12
434ret or tr_eq_ci_slice buf off slen tr_lid_12 tr_eq_ci_slice buf off slen tr_iso_12
435end
436if eq idx 13
437ret or tr_eq_ci_slice buf off slen tr_lid_13 tr_eq_ci_slice buf off slen tr_iso_13
438end
439if eq idx 14
440ret or tr_eq_ci_slice buf off slen tr_lid_14 tr_eq_ci_slice buf off slen tr_iso_14
441end
442if eq idx 15
443ret or tr_eq_ci_slice buf off slen tr_lid_15 tr_eq_ci_slice buf off slen tr_iso_15
444end
445ret false
446end
447
448fn tr_lang_match_pair2 buf view uchar * off ulong slen ulong idx ulong to bool
449if eq idx 16
450ret or tr_eq_ci_slice buf off slen tr_lid_16 tr_eq_ci_slice buf off slen tr_iso_16
451end
452if eq idx 17
453ret or tr_eq_ci_slice buf off slen tr_lid_17 tr_eq_ci_slice buf off slen tr_iso_17
454end
455if eq idx 18
456ret or tr_eq_ci_slice buf off slen tr_lid_18 tr_eq_ci_slice buf off slen tr_iso_18
457end
458if eq idx 19
459ret or tr_eq_ci_slice buf off slen tr_lid_19 tr_eq_ci_slice buf off slen tr_iso_19
460end
461if eq idx 20
462ret or tr_eq_ci_slice buf off slen tr_lid_20 tr_eq_ci_slice buf off slen tr_iso_20
463end
464if eq idx 21
465ret or tr_eq_ci_slice buf off slen tr_lid_21 tr_eq_ci_slice buf off slen tr_iso_21
466end
467if eq idx 22
468ret or tr_eq_ci_slice buf off slen tr_lid_22 tr_eq_ci_slice buf off slen tr_iso_22
469end
470if eq idx 23
471ret or tr_eq_ci_slice buf off slen tr_lid_23 tr_eq_ci_slice buf off slen tr_iso_23
472end
473if eq idx 24
474ret or tr_eq_ci_slice buf off slen tr_lid_24 tr_eq_ci_slice buf off slen tr_iso_24
475end
476if eq idx 25
477ret or tr_eq_ci_slice buf off slen tr_lid_25 tr_eq_ci_slice buf off slen tr_iso_25
478end
479if eq idx 26
480ret or tr_eq_ci_slice buf off slen tr_lid_26 tr_eq_ci_slice buf off slen tr_iso_26
481end
482if eq idx 27
483ret or tr_eq_ci_slice buf off slen tr_lid_27 tr_eq_ci_slice buf off slen tr_iso_27
484end
485if eq idx 28
486ret or tr_eq_ci_slice buf off slen tr_lid_28 tr_eq_ci_slice buf off slen tr_iso_28
487end
488if eq idx 29
489ret or tr_eq_ci_slice buf off slen tr_lid_29 tr_eq_ci_slice buf off slen tr_iso_29
490end
491if eq idx 30
492ret or tr_eq_ci_slice buf off slen tr_lid_30 tr_eq_ci_slice buf off slen tr_iso_30
493end
494ret false
495end
496
497fn tr_lang_find buf view uchar * off ulong slen ulong to ulong
498if gt slen 64
499ret TR_LANG_INVALID
500end
501s = off
502e = add off slen
503while lt s e bound 64
504if not tr_is_ws get buf s
505break
506end
507s = add s 1
508end
509while gt e s bound 64
510if not tr_is_ws get buf sub e 1
511break
512end
513e = sub e 1
514end
515tlen = sub e s
516if eq tlen 0
517ret TR_LANG_INVALID
518end
519i = ulong 0
520while lt i 16 bound 16
521if tr_lang_match_pair buf s tlen i
522ret i
523end
524i = add i 1
525end
526while lt i TR_LANG_COUNT bound 16
527if tr_lang_match_pair2 buf s tlen i
528ret i
529end
530i = add i 1
531end
532ret TR_LANG_INVALID
533end
534
535fn tr_lang_find_src buf view uchar * off ulong slen ulong to ulong
536if gt slen 64
537ret TR_LANG_INVALID
538end
539s = off
540e = add off slen
541while lt s e bound 64
542if not tr_is_ws get buf s
543break
544end
545s = add s 1
546end
547while gt e s bound 64
548if not tr_is_ws get buf sub e 1
549break
550end
551e = sub e 1
552end
553tlen = sub e s
554if eq tlen 0
555ret TR_LANG_AUTO
556end
557if tr_eq_ci_slice buf s tlen tr_t_auto
558ret TR_LANG_AUTO
559end
560ret tr_lang_find buf s tlen
561end
562
563fn tr_copy_text dst edit uchar * doff ulong src view uchar * to ulong
564n = len src
565i = ulong 0
566while lt i n bound 64
567put dst add doff i get src i
568i = add i 1
569end
570ret n
571end
572
573fn tr_lang_code_write lid ulong dst edit uchar * doff ulong to ulong
574if eq lid 0
575ret tr_copy_text dst doff tr_iso_0
576end
577if eq lid 1
578ret tr_copy_text dst doff tr_iso_1
579end
580if eq lid 2
581ret tr_copy_text dst doff tr_iso_2
582end
583if eq lid 3
584ret tr_copy_text dst doff tr_iso_3
585end
586if eq lid 4
587ret tr_copy_text dst doff tr_iso_4
588end
589if eq lid 5
590ret tr_copy_text dst doff tr_iso_5
591end
592if eq lid 6
593ret tr_copy_text dst doff tr_iso_6
594end
595if eq lid 7
596ret tr_copy_text dst doff tr_iso_7
597end
598if eq lid 8
599ret tr_copy_text dst doff tr_iso_8
600end
601if eq lid 9
602ret tr_copy_text dst doff tr_iso_9
603end
604if eq lid 10
605ret tr_copy_text dst doff tr_iso_10
606end
607if eq lid 11
608ret tr_copy_text dst doff tr_iso_11
609end
610if eq lid 12
611ret tr_copy_text dst doff tr_iso_12
612end
613if eq lid 13
614ret tr_copy_text dst doff tr_iso_13
615end
616if eq lid 14
617ret tr_copy_text dst doff tr_iso_14
618end
619if eq lid 15
620ret tr_copy_text dst doff tr_iso_15
621end
622ret tr_lang_code_write2 lid dst doff
623end
624
625fn tr_lang_code_write2 lid ulong dst edit uchar * doff ulong to ulong
626if eq lid 16
627ret tr_copy_text dst doff tr_iso_16
628end
629if eq lid 17
630ret tr_copy_text dst doff tr_iso_17
631end
632if eq lid 18
633ret tr_copy_text dst doff tr_iso_18
634end
635if eq lid 19
636ret tr_copy_text dst doff tr_iso_19
637end
638if eq lid 20
639ret tr_copy_text dst doff tr_iso_20
640end
641if eq lid 21
642ret tr_copy_text dst doff tr_iso_21
643end
644if eq lid 22
645ret tr_copy_text dst doff tr_iso_22
646end
647if eq lid 23
648ret tr_copy_text dst doff tr_iso_23
649end
650if eq lid 24
651ret tr_copy_text dst doff tr_iso_24
652end
653if eq lid 25
654ret tr_copy_text dst doff tr_iso_25
655end
656if eq lid 26
657ret tr_copy_text dst doff tr_iso_26
658end
659if eq lid 27
660ret tr_copy_text dst doff tr_iso_27
661end
662if eq lid 28
663ret tr_copy_text dst doff tr_iso_28
664end
665if eq lid 29
666ret tr_copy_text dst doff tr_iso_29
667end
668if eq lid 30
669ret tr_copy_text dst doff tr_iso_30
670end
671if eq lid TR_LANG_AUTO
672ret tr_copy_text dst doff tr_t_auto
673end
674ret 0
675end
676
677note --- Strict RFC 3629 UTF-8 validation & codepoint stepping ---
678
679fn tr_is_cont b uchar to bool
680ret and ge b 0x80 le b 0xbf
681end
682
683note Returns UTF-8 sequence byte length (1..4), or 0 if invalid UTF-8 at pos.
684fn tr_utf8_step buf view uchar * pos ulong blen ulong to ulong
685if ge pos blen
686ret 0
687end
688b0 = get buf pos
689if lt b0 0x80
690ret 1
691end
692if and ge b0 0xc2 le b0 0xdf
693if ge add pos 1 blen
694ret 0
695end
696if tr_is_cont get buf add pos 1
697ret 2
698end
699ret 0
700end
701if and ge b0 0xe0 le b0 0xef
702if ge add pos 2 blen
703ret 0
704end
705b1 = get buf add pos 1
706b2 = get buf add pos 2
707if not tr_is_cont b2
708ret 0
709end
710if eq b0 0xe0
711if and ge b1 0xa0 le b1 0xbf
712ret 3
713end
714ret 0
715end
716if eq b0 0xed
717if and ge b1 0x80 le b1 0x9f
718ret 3
719end
720ret 0
721end
722if tr_is_cont b1
723ret 3
724end
725ret 0
726end
727if and ge b0 0xf0 le b0 0xf4
728if ge add pos 3 blen
729ret 0
730end
731b1 = get buf add pos 1
732b2 = get buf add pos 2
733b3 = get buf add pos 3
734if not and tr_is_cont b2 tr_is_cont b3
735ret 0
736end
737if eq b0 0xf0
738if and ge b1 0x90 le b1 0xbf
739ret 4
740end
741ret 0
742end
743if eq b0 0xf4
744if and ge b1 0x80 le b1 0x8f
745ret 4
746end
747ret 0
748end
749if tr_is_cont b1
750ret 4
751end
752ret 0
753end
754ret 0
755end
756
757fn tr_utf8_valid buf view uchar * blen ulong to bool
758if gt blen TR_MAX_BYTES
759ret false
760end
761pos = ulong 0
762while lt pos blen bound 16385
763step = tr_utf8_step buf pos blen
764if eq step 0
765ret false
766end
767pos = add pos step
768end
769ret true
770end
771
772fn tr_utf8_chars buf view uchar * blen ulong to ulong
773if gt blen TR_MAX_BYTES
774ret 0
775end
776pos = ulong 0
777cnt = ulong 0
778while lt pos blen bound 16385
779step = tr_utf8_step buf pos blen
780if eq step 0
781ret 0
782end
783pos = add pos step
784cnt = add cnt 1
785end
786ret cnt
787end
788
789note Strict request text validation: rejects empty, oversize, NUL byte, invalid UTF-8.
790fn tr_validate_text buf view uchar * blen ulong to ulong
791if eq blen 0
792ret TR_ERR_EMPTY
793end
794if gt blen TR_MAX_BYTES
795ret TR_ERR_OVERSIZE
796end
797pos = ulong 0
798chars = ulong 0
799nonws = false
800while lt pos blen bound 16385
801b0 = get buf pos
802if eq b0 0
803ret TR_ERR_NUL
804end
805step = tr_utf8_step buf pos blen
806if eq step 0
807ret TR_ERR_UTF8
808end
809if eq step 1
810if not tr_is_ws b0
811nonws = true
812end
813else
814nonws = true
815end
816pos = add pos step
817chars = add chars 1
818end
819if gt chars TR_MAX_CHARS
820ret TR_ERR_OVERSIZE
821end
822if not nonws
823ret TR_ERR_EMPTY
824end
825ret TR_OK
826end
827
828note --- UTF-8-aware paragraph/sentence/word chunker (never splits multi-byte code points) ---
829
830fn tr_is_cjk_sent buf view uchar * pos ulong blen ulong to bool
831if gt add pos 3 blen
832ret false
833end
834b0 = get buf pos
835b1 = get buf add pos 1
836b2 = get buf add pos 2
837note U+3002 IDEOGRAPHIC FULL STOP (e3 80 82)
838if and eq b0 0xe3 and eq b1 0x80 eq b2 0x82
839ret true
840end
841note U+FF01 FULLWIDTH EXCLAMATION (ef bc 81) or U+FF1F FULLWIDTH QUESTION (ef bc 9f)
842if and eq b0 0xef eq b1 0xbc
843if or eq b2 0x81 eq b2 0x9f
844ret true
845end
846end
847ret false
848end
849
850fn tr_is_sent_punct b uchar to bool
851if eq b 46
852ret true
853end
854if eq b 33
855ret true
856end
857ret eq b 63
858end
859
860fn tr_pick_chunk_end buf view uchar * pos ulong blen ulong maxb ulong to ulong
861bests = new ulong 4 0
862if le sub blen pos maxb
863ret blen
864end
865lim = add pos maxb
866cur = pos
867last_cp = pos
868while lt cur lim bound 16385
869step = tr_utf8_step buf cur blen
870if eq step 0
871ret 0
872end
873nxt = add cur step
874if gt nxt lim
875break
876end
877last_cp = nxt
878if eq step 1
879b = get buf cur
880if eq b 10
881put bests 1 nxt
882if lt nxt lim
883if eq get buf nxt 10
884put bests 0 add nxt 1
885end
886end
887end
888if tr_is_sent_punct b
889if lt nxt blen
890if tr_is_ws get buf nxt
891put bests 2 nxt
892end
893end
894end
895if eq b 32
896put bests 3 nxt
897end
898if eq b 9
899put bests 3 nxt
900end
901else
902if eq step 3
903if tr_is_cjk_sent buf cur blen
904put bests 2 nxt
905end
906end
907end
908cur = nxt
909end
910if gt get bests 0 pos
911ret get bests 0
912end
913if gt get bests 1 pos
914ret get bests 1
915end
916if gt get bests 2 pos
917ret get bests 2
918end
919if gt get bests 3 pos
920ret get bests 3
921end
922ret last_cp
923end
924
925note Splits buf[0..blen] into UTF-8-safe chunks <= maxb bytes each.
926note Writes offsets/lengths into tr_g_chk_off / tr_g_chk_len and count into tr_g_chk_cnt[0].
927fn tr_chunk_utf8 buf view uchar * blen ulong maxb ulong to ulong
928put tr_g_chk_cnt 0 0
929if lt maxb 4
930ret TR_ERR_CHUNK
931end
932verr = tr_validate_text buf blen
933if ne verr TR_OK
934ret verr
935end
936pos = ulong 0
937k = ulong 0
938while lt pos blen bound 65
939if ge k TR_MAX_CHUNKS
940ret TR_ERR_CHUNK
941end
942endp = tr_pick_chunk_end buf pos blen maxb
943if le endp pos
944ret TR_ERR_UTF8
945end
946put tr_g_chk_off k pos
947put tr_g_chk_len k sub endp pos
948k = add k 1
949pos = endp
950end
951put tr_g_chk_cnt 0 k
952ret TR_OK
953end
954
955note --- Lead / Core / Trail splitter (upstream split_ws) ---
956
957fn tr_split_ws buf view uchar * blen ulong to void
958a = ulong 0
959b = blen
960while lt a b bound 16385
961if not tr_is_ws get buf a
962break
963end
964a = add a 1
965end
966while gt b a bound 16385
967if not tr_is_ws get buf sub b 1
968break
969end
970b = sub b 1
971end
972note Preserve translate.js split URL tail ("https:" or "http:") at end of core
973clen = sub b a
974if ge clen 6
975if tr_eq_ci_slice buf sub b 6 6 tr_t_https_col
976ok_tail = true
977if gt clen 6
978if tr_is_alnum get buf sub b 7
979ok_tail = false
980end
981end
982if ok_tail
983b = sub b 6
984while gt b a bound 64
985if not tr_is_ws get buf sub b 1
986break
987end
988b = sub b 1
989end
990end
991end
992else
993if ge clen 5
994if tr_eq_ci_slice buf sub b 5 5 tr_t_http_col
995ok_tail2 = true
996if gt clen 5
997if tr_is_alnum get buf sub b 6
998ok_tail2 = false
999end
1000end
1001if ok_tail2
1002b = sub b 5
1003while gt b a bound 64
1004if not tr_is_ws get buf sub b 1
1005break
1006end
1007b = sub b 1
1008end
1009end
1010end
1011end
1012end
1013put tr_g_sw_lead 0 a
1014put tr_g_sw_core_end 0 b
1015end
1016
1017note --- Translation Guard & Protection Engine (upstream src/guard.rs) ---
1018
1019fn tr_keep_check_one src view uchar * slen ulong trn view uchar * tlen ulong to ulong
1020if tr_contains_word src slen tr_kp_0
1021if not tr_contains_word trn tlen tr_kp_0
1022ret TR_ERR_GUARD
1023end
1024end
1025if tr_contains_word src slen tr_kp_1
1026if not tr_contains_word trn tlen tr_kp_1
1027ret TR_ERR_GUARD
1028end
1029end
1030if tr_contains_word src slen tr_kp_2
1031if not tr_contains_word trn tlen tr_kp_2
1032ret TR_ERR_GUARD
1033end
1034end
1035if tr_contains_word src slen tr_kp_3
1036if not tr_contains_word trn tlen tr_kp_3
1037ret TR_ERR_GUARD
1038end
1039end
1040if tr_contains_word src slen tr_kp_4
1041if not tr_contains_word trn tlen tr_kp_4
1042ret TR_ERR_GUARD
1043end
1044end
1045if tr_contains_word src slen tr_kp_5
1046if not tr_contains_word trn tlen tr_kp_5
1047ret TR_ERR_GUARD
1048end
1049end
1050if tr_contains_word src slen tr_kp_6
1051if not tr_contains_word trn tlen tr_kp_6
1052ret TR_ERR_GUARD
1053end
1054end
1055if tr_contains_word src slen tr_kp_7
1056if not tr_contains_word trn tlen tr_kp_7
1057ret TR_ERR_GUARD
1058end
1059end
1060if tr_contains_word src slen tr_kp_8
1061if not tr_contains_word trn tlen tr_kp_8
1062ret TR_ERR_GUARD
1063end
1064end
1065if tr_contains_word src slen tr_kp_9
1066if not tr_contains_word trn tlen tr_kp_9
1067ret TR_ERR_GUARD
1068end
1069end
1070if tr_contains_word src slen tr_kp_10
1071if not tr_contains_word trn tlen tr_kp_10
1072ret TR_ERR_GUARD
1073end
1074end
1075ret TR_OK
1076end
1077
1078fn tr_keep_check_two src view uchar * slen ulong trn view uchar * tlen ulong to ulong
1079if tr_contains_word src slen tr_kp_11
1080if not tr_contains_word trn tlen tr_kp_11
1081ret TR_ERR_GUARD
1082end
1083end
1084if tr_contains_word src slen tr_kp_12
1085if not tr_contains_word trn tlen tr_kp_12
1086ret TR_ERR_GUARD
1087end
1088end
1089if tr_contains_word src slen tr_kp_13
1090if not tr_contains_word trn tlen tr_kp_13
1091ret TR_ERR_GUARD
1092end
1093end
1094if tr_contains_word src slen tr_kp_14
1095if not tr_contains_word trn tlen tr_kp_14
1096ret TR_ERR_GUARD
1097end
1098end
1099if tr_contains_word src slen tr_kp_15
1100if not tr_contains_word trn tlen tr_kp_15
1101ret TR_ERR_GUARD
1102end
1103end
1104if tr_contains_word src slen tr_kp_16
1105if not tr_contains_word trn tlen tr_kp_16
1106ret TR_ERR_GUARD
1107end
1108end
1109if tr_contains_word src slen tr_kp_17
1110if not tr_contains_word trn tlen tr_kp_17
1111ret TR_ERR_GUARD
1112end
1113end
1114if tr_contains_word src slen tr_kp_18
1115if not tr_contains_word trn tlen tr_kp_18
1116ret TR_ERR_GUARD
1117end
1118end
1119if tr_contains_word src slen tr_kp_19
1120if not tr_contains_word trn tlen tr_kp_19
1121ret TR_ERR_GUARD
1122end
1123end
1124if tr_contains_word src slen tr_kp_20
1125if not tr_contains_word trn tlen tr_kp_20
1126ret TR_ERR_GUARD
1127end
1128end
1129if tr_contains_word src slen tr_kp_21
1130if not tr_contains_word trn tlen tr_kp_21
1131ret TR_ERR_GUARD
1132end
1133end
1134ret TR_OK
1135end
1136
1137fn tr_is_url_stop c uchar to bool
1138if tr_is_ws c
1139ret true
1140end
1141if eq c 60
1142ret true
1143end
1144if eq c 62
1145ret true
1146end
1147if eq c 40
1148ret true
1149end
1150if eq c 41
1151ret true
1152end
1153if eq c 91
1154ret true
1155end
1156ret eq c 93
1157end
1158
1159fn tr_is_url_trail c uchar to bool
1160if eq c 46
1161ret true
1162end
1163if eq c 44
1164ret true
1165end
1166if eq c 59
1167ret true
1168end
1169if eq c 58
1170ret true
1171end
1172if eq c 33
1173ret true
1174end
1175ret eq c 63
1176end
1177
1178fn tr_url_end buf view uchar * pos ulong blen ulong to ulong
1179e = pos
1180while lt e blen bound 1024
1181if tr_is_url_stop get buf e
1182break
1183end
1184e = add e 1
1185end
1186while gt e pos bound 64
1187if tr_is_url_trail get buf sub e 1
1188e = sub e 1
1189else
1190break
1191end
1192end
1193ret e
1194end
1195
1196fn tr_scan_bt_end src view uchar * i ulong slen ulong to ulong
1197j = add i 1
1198while lt j slen bound 1024
1199cj = get src j
1200if eq cj 10
1201ret 0
1202end
1203if eq cj 96
1204ret add j 1
1205end
1206j = add j 1
1207end
1208ret 0
1209end
1210
1211fn tr_scan_ph_end src view uchar * i ulong slen ulong to ulong
1212j = add i 1
1213while lt j slen bound 128
1214cj = get src j
1215if eq cj 125
1216if gt j add i 1
1217ret add j 1
1218end
1219ret 0
1220end
1221if not tr_is_alnum cj
1222if ne cj 95
1223ret 0
1224end
1225end
1226j = add j 1
1227end
1228ret 0
1229end
1230
1231fn tr_scan_tag_end src view uchar * i ulong slen ulong to ulong
1232if ge add i 2 slen
1233ret 0
1234end
1235b1 = get src add i 1
1236ok_lead = false
1237if eq b1 103
1238ok_lead = true
1239end
1240if eq b1 120
1241ok_lead = true
1242end
1243if eq b1 47
1244ok_lead = true
1245end
1246if not ok_lead
1247ret 0
1248end
1249j = add i 1
1250while lt j slen bound 16
1251if eq get src j 62
1252e = add j 1
1253if le sub e i 10
1254ret e
1255end
1256ret 0
1257end
1258j = add j 1
1259end
1260ret 0
1261end
1262
1263fn tr_scan_url_end src view uchar * i ulong slen ulong to ulong
1264if le add i 8 slen
1265if tr_eq_exact_slice src i 8 tr_t_https
1266ue = tr_url_end src i slen
1267if gt ue add i 8
1268ret ue
1269end
1270end
1271end
1272if le add i 7 slen
1273if tr_eq_exact_slice src i 7 tr_t_http
1274ue2 = tr_url_end src i slen
1275if gt ue2 add i 7
1276ret ue2
1277end
1278end
1279end
1280ret 0
1281end
1282
1283note Verify URLs, backtick spans, {placeholders}, %s/%d, and <gN>/</gN>/<xN/> survive in trn.
1284fn tr_guard_tokens src view uchar * slen ulong trn view uchar * tlen ulong to ulong
1285i = ulong 0
1286while lt i slen bound 16385
1287c = get src i
1288e = ulong 0
1289if eq c 96
1290e = tr_scan_bt_end src i slen
1291end
1292if eq c 104
1293e = tr_scan_url_end src i slen
1294end
1295if eq c 123
1296e = tr_scan_ph_end src i slen
1297end
1298if eq c 60
1299e = tr_scan_tag_end src i slen
1300end
1301if eq c 37
1302if lt add i 1 slen
1303nx = get src add i 1
1304if or eq nx 115 eq nx 100
1305e = add i 2
1306end
1307end
1308end
1309if gt e i
1310put tr_g_item_len 0 sub e i
1311if not tr_has_slice trn tlen src i
1312ret TR_ERR_GUARD
1313end
1314i = e
1315else
1316i = add i 1
1317end
1318end
1319ret TR_OK
1320end
1321
1322note Full guard check matching upstream guard::check(source, translated, keep).
1323fn tr_guard_check src view uchar * slen ulong trn view uchar * tlen ulong to ulong
1324note 1. Reject empty or whitespace-only translation
1325has_nw = false
1326i = ulong 0
1327while lt i tlen bound 16385
1328if not tr_is_ws get trn i
1329has_nw = true
1330break
1331end
1332i = add i 1
1333end
1334if not has_nw
1335ret TR_ERR_GUARD
1336end
1337note 2. Reject runaway output: tr_chars > src_chars * 4 + 80
1338sc = tr_utf8_chars src slen
1339tc = tr_utf8_chars trn tlen
1340if eq tc 0
1341ret TR_ERR_GUARD
1342end
1343lim = add mul sc 4 80
1344if gt tc lim
1345ret TR_ERR_GUARD
1346end
1347note 3. Protected keep-list words & tokens
1348e1 = tr_keep_check_one src slen trn tlen
1349if ne e1 TR_OK
1350ret e1
1351end
1352e2 = tr_keep_check_two src slen trn tlen
1353if ne e2 TR_OK
1354ret e2
1355end
1356ret tr_guard_tokens src slen trn tlen
1357end
1358
1359note Upstream guard::tidy: trim whitespace, strip spurious backticks if source had none.
1360fn tr_tidy src view uchar * slen ulong trn view uchar * tlen ulong to ulong
1361src_has_bt = false
1362i = ulong 0
1363while lt i slen bound 16385
1364if eq get src i 96
1365src_has_bt = true
1366break
1367end
1368i = add i 1
1369end
1370s = ulong 0
1371e = tlen
1372while lt s e bound 16385
1373if not tr_is_ws get trn s
1374break
1375end
1376s = add s 1
1377end
1378while gt e s bound 16385
1379if not tr_is_ws get trn sub e 1
1380break
1381end
1382e = sub e 1
1383end
1384w = ulong 0
1385k = s
1386while lt k e bound 1024
1387c = get trn k
1388if or src_has_bt ne c 96
1389if lt w 1024
1390put tr_g_tmp_out w c
1391w = add w 1
1392end
1393end
1394k = add k 1
1395end
1396put tr_g_tmp_out_len 0 w
1397ret w
1398end
1399
1400note Upstream engine::list_closed: true once JSON array [...] closes outside strings.
1401fn tr_list_closed buf view uchar * blen ulong to bool
1402start = blen
1403i = ulong 0
1404while lt i blen bound 16385
1405if eq get buf i 91
1406start = i
1407break
1408end
1409i = add i 1
1410end
1411if eq start blen
1412ret false
1413end
1414depth = ulong 0
1415in_str = false
1416esc = false
1417pos = start
1418while lt pos blen bound 16385
1419c = get buf pos
1420if in_str
1421if esc
1422esc = false
1423else
1424if eq c 92
1425esc = true
1426else
1427if eq c 34
1428in_str = false
1429end
1430end
1431end
1432else
1433if eq c 34
1434in_str = true
1435else
1436if or eq c 91 eq c 123
1437depth = add depth 1
1438else
1439if or eq c 93 eq c 125
1440if gt depth 0
1441depth = sub depth 1
1442end
1443if eq depth 0
1444ret true
1445end
1446end
1447end
1448end
1449end
1450pos = add pos 1
1451end
1452ret false
1453end
1454
1455note --- Deterministic SHA-256 Keyed Bounded LRU Cache (upstream src/cache.rs) ---
1456
1457fn tr_cache_init cap ulong to void
1458c = cap
1459if eq c 0
1460c = 1
1461end
1462if gt c TR_CACHE_MAX_SLOTS
1463c = TR_CACHE_MAX_SLOTS
1464end
1465put tr_g_cache_cap 0 c
1466put tr_g_cache_clock 0 0
1467put tr_g_cache_hits 0 0
1468put tr_g_cache_misses 0 0
1469put tr_g_cache_evicts 0 0
1470put tr_g_cache_cnt 0 0
1471i = ulong 0
1472while lt i TR_CACHE_MAX_SLOTS bound 32
1473put tr_g_cache_used i 0
1474put tr_g_cache_tick i 0
1475put tr_g_cache_vlen i 0
1476i = add i 1
1477end
1478end
1479
1480note Computes SHA-256(p6\0 model\0 src_code\0 dst_code\0 txt\0) into tr_g_key_buf.
1481fn tr_cache_compute_key src_id ulong dst_id ulong txt view uchar * tlen ulong to void
1482ctx = new ulong 40 0
1483nul = new uchar 1 0
1484wbuf = new uchar 64 0
1485sha256_init ctx
1486sha256_update ctx tr_t_pver
1487sha256_update ctx nul
1488n = get tr_g_req_model_len 0
1489if eq n 0
1490sha256_update ctx tr_t_defmodel
1491else
1492if gt n 64
1493n = 64
1494end
1495i = ulong 0
1496while lt i n bound 64
1497put wbuf i get tr_g_req_model i
1498i = add i 1
1499end
1500sha256_update ctx part wbuf 0 n
1501end
1502sha256_update ctx nul
1503n = tr_lang_code_write src_id wbuf 0
1504sha256_update ctx part wbuf 0 n
1505sha256_update ctx nul
1506n = tr_lang_code_write dst_id wbuf 0
1507sha256_update ctx part wbuf 0 n
1508sha256_update ctx nul
1509if gt tlen 0
1510sha256_update ctx part txt 0 tlen
1511end
1512sha256_update ctx nul
1513sha256_final ctx wbuf
1514j = ulong 0
1515while lt j 32 bound 32
1516put tr_g_key_buf j get wbuf j
1517j = add j 1
1518end
1519end
1520
1521fn tr_cache_key_match slot ulong to bool
1522base = mul slot 32
1523i = ulong 0
1524while lt i 32 bound 32
1525if ne get tr_g_cache_key add base i get tr_g_key_buf i
1526ret false
1527end
1528i = add i 1
1529end
1530ret true
1531end
1532
1533fn tr_cache_find_slot cap ulong src_id ulong dst_id ulong to ulong
1534i = ulong 0
1535while lt i cap bound 32
1536if eq get tr_g_cache_used i 1
1537if and eq get tr_g_cache_src i src_id eq get tr_g_cache_dst i dst_id
1538if tr_cache_key_match i
1539ret i
1540end
1541end
1542end
1543i = add i 1
1544end
1545ret cap
1546end
1547
1548fn tr_cache_read_slot slot ulong to void
1549clk = add get tr_g_cache_clock 0 1
1550put tr_g_cache_clock 0 clk
1551put tr_g_cache_tick slot clk
1552put tr_g_cache_hits 0 add get tr_g_cache_hits 0 1
1553vlen = get tr_g_cache_vlen slot
1554vbase = mul slot TR_CACHE_VAL_CAP
1555j = ulong 0
1556while lt j vlen bound 512
1557put tr_g_tmp_out j get tr_g_cache_val add vbase j
1558j = add j 1
1559end
1560put tr_g_tmp_out_len 0 vlen
1561end
1562
1563note Look up (src_id, dst_id, txt[0..tlen]) in cache.
1564note On hit: copies cached output into tr_g_tmp_out, sets tr_g_tmp_out_len, returns 1.
1565note On miss: increments misses, returns 0.
1566fn tr_cache_get src_id ulong dst_id ulong txt view uchar * tlen ulong to ulong
1567cap = get tr_g_cache_cap 0
1568if eq cap 0
1569tr_cache_init 16
1570cap = 16
1571end
1572tr_cache_compute_key src_id dst_id txt tlen
1573slot = tr_cache_find_slot cap src_id dst_id
1574if lt slot cap
1575tr_cache_read_slot slot
1576ret 1
1577end
1578put tr_g_cache_misses 0 add get tr_g_cache_misses 0 1
1579ret 0
1580end
1581
1582fn tr_cache_pick_slot cap ulong src_id ulong dst_id ulong to ulong
1583slot = tr_cache_find_slot cap src_id dst_id
1584if lt slot cap
1585ret slot
1586end
1587j = ulong 0
1588while lt j cap bound 32
1589if eq get tr_g_cache_used j 0
1590put tr_g_cache_cnt 0 add get tr_g_cache_cnt 0 1
1591ret j
1592end
1593j = add j 1
1594end
1595best = ulong 0
1596best_t = get tr_g_cache_tick 0
1597k = ulong 1
1598while lt k cap bound 32
1599tk = get tr_g_cache_tick k
1600if lt tk best_t
1601best = k
1602best_t = tk
1603end
1604k = add k 1
1605end
1606put tr_g_cache_evicts 0 add get tr_g_cache_evicts 0 1
1607ret best
1608end
1609
1610fn tr_cache_write_slot slot ulong src_id ulong dst_id ulong to void
1611clk = add get tr_g_cache_clock 0 1
1612put tr_g_cache_clock 0 clk
1613put tr_g_cache_used slot 1
1614put tr_g_cache_tick slot clk
1615put tr_g_cache_src slot src_id
1616put tr_g_cache_dst slot dst_id
1617kbase = mul slot 32
1618ki = ulong 0
1619while lt ki 32 bound 32
1620put tr_g_cache_key add kbase ki get tr_g_key_buf ki
1621ki = add ki 1
1622end
1623vlen = get tr_g_tmp_out_len 0
1624if gt vlen TR_CACHE_VAL_CAP
1625vlen = TR_CACHE_VAL_CAP
1626end
1627put tr_g_cache_vlen slot vlen
1628vbase = mul slot TR_CACHE_VAL_CAP
1629vi = ulong 0
1630while lt vi vlen bound 512
1631put tr_g_cache_val add vbase vi get tr_g_tmp_out vi
1632vi = add vi 1
1633end
1634end
1635
1636note Store tr_g_tmp_out[0..tr_g_tmp_out_len] under (src_id, dst_id, txt[0..tlen]).
1637note Performs deterministic LRU eviction when cache is at capacity.
1638fn tr_cache_put src_id ulong dst_id ulong txt view uchar * tlen ulong to void
1639cap = get tr_g_cache_cap 0
1640if eq cap 0
1641tr_cache_init 16
1642cap = 16
1643end
1644tr_cache_compute_key src_id dst_id txt tlen
1645slot = tr_cache_pick_slot cap src_id dst_id
1646tr_cache_write_slot slot src_id dst_id
1647end
1648
1649note --- JSONL Response Builder ---
1650
1651fn tr_resp_reset to void
1652put tr_g_resp_len 0 0
1653end
1654
1655fn tr_resp_byte b uchar to void
1656p = get tr_g_resp_len 0
1657if lt p 32768
1658put tr_g_resp p b
1659put tr_g_resp_len 0 add p 1
1660end
1661end
1662
1663fn tr_resp_text s view uchar * to void
1664n = len s
1665i = ulong 0
1666while lt i n bound 256
1667tr_resp_byte get s i
1668i = add i 1
1669end
1670end
1671
1672fn tr_resp_u64 v ulong to void
1673digs = new uchar 24 0
1674if eq v 0
1675tr_resp_byte 48
1676ret
1677end
1678x = v
1679k = ulong 0
1680while gt x 0 bound 24
1681d = rem x 10
1682put digs k uchar add 48 d
1683x = div x 10
1684k = add k 1
1685end
1686while gt k 0 bound 24
1687k = sub k 1
1688tr_resp_byte get digs k
1689end
1690end
1691
1692fn tr_resp_json_str_g len_val ulong to void
1693note Writes tr_g_tmp_out[0..len_val] as a JSON-escaped string (with enclosing quotes)
1694tr_resp_byte 34
1695i = ulong 0
1696while lt i len_val bound 1024
1697c = get tr_g_tmp_out i
1698if eq c 34
1699tr_resp_byte 92
1700tr_resp_byte 34
1701else
1702if eq c 92
1703tr_resp_byte 92
1704tr_resp_byte 92
1705else
1706if eq c 10
1707tr_resp_byte 92
1708tr_resp_byte 110
1709else
1710if eq c 13
1711tr_resp_byte 92
1712tr_resp_byte 114
1713else
1714if eq c 9
1715tr_resp_byte 92
1716tr_resp_byte 116
1717else
1718tr_resp_byte c
1719end
1720end
1721end
1722end
1723end
1724i = add i 1
1725end
1726tr_resp_byte 34
1727end
1728
1729fn tr_resp_error code ulong to void
1730tr_resp_reset
1731tr_resp_text tr_j_err_pre
1732tr_resp_u64 code
1733tr_resp_text tr_j_err_msg
1734if eq code TR_ERR_EMPTY
1735tr_resp_text tr_e_empty
1736end
1737if eq code TR_ERR_OVERSIZE
1738tr_resp_text tr_e_over
1739end
1740if eq code TR_ERR_UTF8
1741tr_resp_text tr_e_utf8
1742end
1743if eq code TR_ERR_NUL
1744tr_resp_text tr_e_nul
1745end
1746if eq code TR_ERR_LANG
1747tr_resp_text tr_e_lang
1748end
1749if eq code TR_ERR_JSON
1750tr_resp_text tr_e_json
1751end
1752if eq code TR_ERR_GUARD
1753tr_resp_text tr_e_guard
1754end
1755if eq code TR_ERR_CHUNK
1756tr_resp_text tr_e_chunk
1757end
1758tr_resp_text tr_j_err_end
1759end
1760
1761note --- Strict JSONL Request Parser ---
1762
1763fn tr_skip_ws buf view uchar * pos ulong blen ulong to ulong
1764p = pos
1765while lt p blen bound 16385
1766if not tr_is_ws get buf p
1767break
1768end
1769p = add p 1
1770end
1771ret p
1772end
1773
1774note Parses a JSON string starting at buf[pos]=='"', storing unescaped bytes into target kind:
1775note kind 0=key(tr_g_item_buf), 1=from, 2=to, 3=model, 4=text.
1776note Returns new position after closing '"', or 0 on syntax/nul/oversize error.
1777fn tr_parse_jstr buf view uchar * pos ulong blen ulong kind ulong to ulong
1778if ge pos blen
1779ret 0
1780end
1781if ne get buf pos 34
1782ret 0
1783end
1784p = add pos 1
1785w = ulong 0
1786cap = ulong 64
1787if eq kind 4
1788cap = TR_MAX_BYTES
1789end
1790while lt p blen bound 16385
1791c = get buf p
1792if eq c 0
1793ret 0
1794end
1795if eq c 34
1796if eq kind 0
1797put tr_g_item_len 0 w
1798end
1799if eq kind 1
1800put tr_g_req_from_len 0 w
1801end
1802if eq kind 2
1803put tr_g_req_to_len 0 w
1804end
1805if eq kind 3
1806put tr_g_req_model_len 0 w
1807end
1808if eq kind 4
1809put tr_g_req_text_len 0 w
1810end
1811ret add p 1
1812end
1813outb = c
1814if eq c 92
1815p = add p 1
1816if ge p blen
1817ret 0
1818end
1819e = get buf p
1820if eq e 34
1821outb = 34
1822else
1823if eq e 92
1824outb = 92
1825else
1826if eq e 110
1827outb = 10
1828else
1829if eq e 114
1830outb = 13
1831else
1832if eq e 116
1833outb = 9
1834else
1835if eq e 47
1836outb = 47
1837else
1838ret 0
1839end
1840end
1841end
1842end
1843end
1844end
1845else
1846if lt c 32
1847ret 0
1848end
1849end
1850if ge w cap
1851ret 0
1852end
1853if eq kind 0
1854put tr_g_item_buf w outb
1855end
1856if eq kind 1
1857put tr_g_req_from w outb
1858end
1859if eq kind 2
1860put tr_g_req_to w outb
1861end
1862if eq kind 3
1863put tr_g_req_model w outb
1864end
1865if eq kind 4
1866put tr_g_req_text w outb
1867end
1868w = add w 1
1869p = add p 1
1870end
1871ret 0
1872end
1873
1874fn tr_key_kind to ulong
1875klen = get tr_g_item_len 0
1876kb = new uchar 32 0
1877if gt klen 32
1878ret 99
1879end
1880i = ulong 0
1881while lt i klen bound 32
1882put kb i get tr_g_item_buf i
1883i = add i 1
1884end
1885if or tr_eq_exact_slice kb 0 klen tr_jk_from tr_eq_exact_slice kb 0 klen tr_jk_src
1886ret 1
1887end
1888if or tr_eq_exact_slice kb 0 klen tr_jk_to tr_eq_exact_slice kb 0 klen tr_jk_dst
1889ret 2
1890end
1891if tr_eq_exact_slice kb 0 klen tr_jk_model
1892ret 3
1893end
1894if tr_eq_exact_slice kb 0 klen tr_jk_text
1895ret 4
1896end
1897if tr_eq_exact_slice kb 0 klen tr_jk_maxc
1898ret 5
1899end
1900ret 99
1901end
1902
1903fn tr_parse_maxc line view uchar * pos ulong llen ulong to ulong
1904p = pos
1905val = ulong 0
1906digits = ulong 0
1907while lt p llen bound 16
1908dc = get line p
1909if not tr_is_digit dc
1910break
1911end
1912val = add mul val 10 ulong sub dc 48
1913digits = add digits 1
1914p = add p 1
1915end
1916if eq digits 0
1917ret 0
1918end
1919put tr_g_req_maxc 0 val
1920ret p
1921end
1922
1923fn tr_parse_req_line line view uchar * llen ulong to ulong
1924put tr_g_req_from_len 0 0
1925put tr_g_req_to_len 0 0
1926put tr_g_req_model_len 0 0
1927put tr_g_req_text_len 0 0
1928put tr_g_req_maxc 0 512
1929if eq llen 0
1930ret TR_ERR_EMPTY
1931end
1932if gt llen TR_MAX_BYTES
1933ret TR_ERR_OVERSIZE
1934end
1935ni = ulong 0
1936while lt ni llen bound 16385
1937if eq get line ni 0
1938ret TR_ERR_NUL
1939end
1940ni = add ni 1
1941end
1942p = tr_skip_ws line 0 llen
1943if ge p llen
1944ret TR_ERR_EMPTY
1945end
1946if ne get line p 123
1947ret TR_ERR_JSON
1948end
1949p = tr_skip_ws line add p 1 llen
1950got_to = false
1951got_text = false
1952fields = ulong 0
1953while lt fields 16 bound 16
1954if ge p llen
1955ret TR_ERR_JSON
1956end
1957if eq get line p 125
1958p = tr_skip_ws line add p 1 llen
1959if ne p llen
1960ret TR_ERR_JSON
1961end
1962break
1963end
1964p = tr_parse_jstr line p llen 0
1965if eq p 0
1966ret TR_ERR_JSON
1967end
1968kk = tr_key_kind
1969p = tr_skip_ws line p llen
1970if ge p llen
1971ret TR_ERR_JSON
1972end
1973if ne get line p 58
1974ret TR_ERR_JSON
1975end
1976p = tr_skip_ws line add p 1 llen
1977if ge p llen
1978ret TR_ERR_JSON
1979end
1980if and ge kk 1 le kk 4
1981p = tr_parse_jstr line p llen kk
1982if eq p 0
1983ret TR_ERR_JSON
1984end
1985if eq kk 2
1986got_to = true
1987end
1988if eq kk 4
1989got_text = true
1990end
1991else
1992if eq kk 5
1993p = tr_parse_maxc line p llen
1994if eq p 0
1995ret TR_ERR_JSON
1996end
1997else
1998ret TR_ERR_JSON
1999end
2000end
2001p = tr_skip_ws line p llen
2002if ge p llen
2003ret TR_ERR_JSON
2004end
2005if eq get line p 44
2006p = tr_skip_ws line add p 1 llen
2007else
2008if ne get line p 125
2009ret TR_ERR_JSON
2010end
2011end
2012fields = add fields 1
2013end
2014if not and got_to got_text
2015ret TR_ERR_JSON
2016end
2017ret TR_OK
2018end
2019
2020note --- End-to-End Request Pipeline (validate -> chunk -> cache -> guard -> JSONL) ---
2021
2022fn tr_resp_lang_code lid ulong to void
2023lcode = new uchar 16 0
2024n = tr_lang_code_write lid lcode 0
2025i = ulong 0
2026while lt i n bound 16
2027tr_resp_byte get lcode i
2028i = add i 1
2029end
2030end
2031
2032fn tr_emit_one_item src_id ulong dst_id ulong idx ulong to void
2033cbuf = new uchar 512 0
2034coff = get tr_g_chk_off idx
2035clen = get tr_g_chk_len idx
2036if gt clen 512
2037clen = 512
2038end
2039ci = ulong 0
2040while lt ci clen bound 512
2041put cbuf ci get tr_g_req_text add coff ci
2042ci = add ci 1
2043end
2044h2 = tr_cache_get src_id dst_id part cbuf 0 clen clen
2045if eq h2 1
2046put tr_g_cache_hits 0 sub get tr_g_cache_hits 0 1
2047tr_resp_json_str_g get tr_g_tmp_out_len 0
2048else
2049put tr_g_cache_misses 0 sub get tr_g_cache_misses 0 1
2050ci2 = ulong 0
2051while lt ci2 clen bound 512
2052put tr_g_tmp_out ci2 get cbuf ci2
2053ci2 = add ci2 1
2054end
2055tr_resp_json_str_g clen
2056end
2057end
2058
2059fn tr_emit_ok_resp src_id ulong dst_id ulong cnt ulong req_hits ulong req_misses ulong to void
2060tr_resp_reset
2061tr_resp_text tr_j_ok_pre
2062tr_resp_lang_code src_id
2063tr_resp_text tr_j_to
2064tr_resp_lang_code dst_id
2065tr_resp_text tr_j_chunks
2066tr_resp_u64 cnt
2067tr_resp_text tr_j_fc
2068tr_resp_u64 req_hits
2069tr_resp_text tr_j_ms
2070tr_resp_u64 req_misses
2071tr_resp_text tr_j_ev
2072tr_resp_u64 get tr_g_cache_evicts 0
2073tr_resp_text tr_j_items
2074m = ulong 0
2075while lt m cnt bound 64
2076if gt m 0
2077tr_resp_byte 44
2078end
2079tr_emit_one_item src_id dst_id m
2080m = add m 1
2081end
2082tr_resp_text tr_j_end
2083end
2084
2085fn tr_req_from_id to ulong
2086fbuf = new uchar 64 0
2087flen = get tr_g_req_from_len 0
2088fi = ulong 0
2089while lt fi flen bound 64
2090put fbuf fi get tr_g_req_from fi
2091fi = add fi 1
2092end
2093ret tr_lang_find_src fbuf 0 flen
2094end
2095
2096fn tr_req_to_id to ulong
2097tbuf = new uchar 64 0
2098tlen = get tr_g_req_to_len 0
2099ti = ulong 0
2100while lt ti tlen bound 64
2101put tbuf ti get tr_g_req_to ti
2102ti = add ti 1
2103end
2104ret tr_lang_find tbuf 0 tlen
2105end
2106
2107fn tr_process_chunk src_id ulong dst_id ulong idx ulong to ulong
2108cbuf = new uchar 512 0
2109coff = get tr_g_chk_off idx
2110clen = get tr_g_chk_len idx
2111if gt clen TR_CACHE_VAL_CAP
2112clen = TR_CACHE_VAL_CAP
2113end
2114ci = ulong 0
2115while lt ci clen bound 512
2116put cbuf ci get tr_g_req_text add coff ci
2117ci = add ci 1
2118end
2119hit = tr_cache_get src_id dst_id part cbuf 0 clen clen
2120if eq hit 1
2121ret 1
2122end
2123ci2 = ulong 0
2124while lt ci2 clen bound 512
2125put tr_g_tmp_out ci2 get cbuf ci2
2126ci2 = add ci2 1
2127end
2128put tr_g_tmp_out_len 0 clen
2129tr_cache_put src_id dst_id part cbuf 0 clen clen
2130ret 0
2131end
2132
2133fn tr_handle_request line view uchar * llen ulong to ulong
2134txt = new uchar 16385 0
2135perr = tr_parse_req_line line llen
2136if ne perr TR_OK
2137tr_resp_error perr
2138ret perr
2139end
2140src_id = tr_req_from_id
2141if eq src_id TR_LANG_INVALID
2142tr_resp_error TR_ERR_LANG
2143ret TR_ERR_LANG
2144end
2145dst_id = tr_req_to_id
2146if eq dst_id TR_LANG_INVALID
2147tr_resp_error TR_ERR_LANG
2148ret TR_ERR_LANG
2149end
2150txlen = get tr_g_req_text_len 0
2151xi = ulong 0
2152while lt xi txlen bound 16385
2153put txt xi get tr_g_req_text xi
2154xi = add xi 1
2155end
2156cerr = tr_chunk_utf8 txt txlen get tr_g_req_maxc 0
2157if ne cerr TR_OK
2158tr_resp_error cerr
2159ret cerr
2160end
2161cnt = get tr_g_chk_cnt 0
2162req_hits = ulong 0
2163req_misses = ulong 0
2164k = ulong 0
2165while lt k cnt bound 64
2166if eq tr_process_chunk src_id dst_id k 1
2167req_hits = add req_hits 1
2168else
2169req_misses = add req_misses 1
2170end
2171k = add k 1
2172end
2173tr_emit_ok_resp src_id dst_id cnt req_hits req_misses
2174ret TR_OK
2175end