Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): cut stateText budgets at code points like Python

Rahul Ranjanb87976eab51c
diff --git a/laya-ts/src/lang.ts b/laya-ts/src/lang.ts
index bca4e77c..f4f5c53e 100644
--- a/laya-ts/src/lang.ts
+++ b/laya-ts/src/lang.ts
@@ -213,6 +213,19 @@ function scriptOf(ch: string): string | null {
return null;
}
+/** Python's `text[:count]`: a prefix cut at code points, so a surrogate pair is never split
+ * in half. Python's `lang.py` slices the same budgets by code points (`state_text`,
+ * `_non_english_segment`, the 4000-character leaf samples), and the email port already cuts this
+ * way (`cutCodePoints` in `email.ts`, added when a budget cut an emoji in half). `String.slice`
+ * counts UTF-16 units, so `stateText("A".repeat(3999) + "\uD83D\uDE00" + "...")` used to return a
+ * string ending in the orphaned high half of the emoji -- text Python never produces, and a lone
+ * surrogate the serve side already refuses as malformed (#454, #814). */
+function cutCodePoints(text: string, count: number): string {
+ const chars = Array.from(text);
+ const end = count < 0 ? Math.max(0, chars.length + count) : Math.min(count, chars.length);
+ return chars.slice(0, end).join("");
+}
+
/** String leaves of a state. Keys are ignored: they are usually English field names. */
function iterText(state: unknown, depth = 0): string[] {
if (depth > 6 || state == null) return [];
@@ -238,7 +251,7 @@ function iterText(state: unknown, depth = 0): string[] {
}
export function stateText(state: unknown, maxChars = 4000): string {
- return iterText(state).join(" ").slice(0, maxChars);
+ return cutCodePoints(iterText(state).join(" "), maxChars);
}
export function detectScript(text: string): string {
@@ -493,8 +506,8 @@ function nonEnglishSegment(state: unknown, maxChars = 4000): [string, string] |
for (const leaf of iterText(state)) {
for (const seg of leaf.split("\n")) {
if (seen >= maxChars) return null;
- const capped = seg.slice(0, maxChars - seen);
- seen += capped.length;
+ const capped = cutCodePoints(seg, maxChars - seen);
+ seen += [...capped].length;
const lang = namedProseLanguage(capped);
if (lang) return [lang, capped.trim()];
}
@@ -610,7 +623,7 @@ function alphaCount(text: string): number {
/** A string value that is itself not safe for the English checkpoint, else null. */
function leafNonEnglish(leaf: string): AnalyseResult | null {
- const sample = leaf.slice(0, 4000);
+ const sample = cutCodePoints(leaf, 4000);
if (!sample.trim()) return null;
const det = analyseText(sample);
if (det.isEnglish) return null;
@@ -663,7 +676,7 @@ export function analyse(state: unknown): AnalyseResult {
for (const leaf of iterText(state)) {
const det = leafNonEnglish(leaf);
if (!det) continue;
- const n = alphaCount(leaf.slice(0, 4000));
+ const n = alphaCount(cutCodePoints(leaf, 4000));
if (n > bestN) {
bestN = n;
best = det;
diff --git a/laya-ts/tests/lang.test.ts b/laya-ts/tests/lang.test.ts
index 5e459b81..35f409cf 100644
--- a/laya-ts/tests/lang.test.ts
+++ b/laya-ts/tests/lang.test.ts
@@ -1,11 +1,24 @@
// laya-ts/tests/lang.test.ts
import { describe, expect, it } from "vitest";
-import { analyse, detectScript, guessLatinLanguage, isEnglish } from "../src/lang.js";
+import { analyse, detectScript, guessLatinLanguage, isEnglish, stateText } from "../src/lang.js";
import { Router } from "../src/router.js";
describe("lang", () => {
it("detects devanagari as non-latin", () => {
expect(detectScript("मुझसे दो बार शुल्क लिया गया")).toBe("devanagari");
});
+ // Python's state_text slices its 4000-character budget by code points; this port sliced by
+ // UTF-16 units, so a state whose 4000th unit was the high half of an emoji returned text
+ // ending in an orphaned surrogate -- a malformed string Python never produces, and the same
+ // lone-surrogate shape laya-serve already refuses as a request (#454, #814). The email port
+ // carries cutCodePoints for the same reason ("a budget cut an emoji in half").
+ // Expectations from Python's laya.lang.state_text on the same input.
+ it("stateText cuts its budget at code points, never splitting a surrogate pair", () => {
+ const state = "A".repeat(3999) + "\uD83D\uDE00" + "tail text that will be cut";
+ const text = stateText(state);
+ expect([...text].length).toBe(4000);
+ expect(text.codePointAt(text.length - 2)).toBe(0x1f600); // the whole emoji survived
+ expect(text.endsWith("\uD83D\uDE00")).toBe(true); // no orphaned high surrogate at the cut
+ });
it("routes english latin to english", () => {
expect(analyse("Please refund the duplicate charge").isEnglish).toBe(true);
});