Skip to content

Commit 0a66f66

Browse files
fix: make CJK regexes lint-safe
Co-authored-by: qingminlong <[email protected]>
1 parent e11aad2 commit 0a66f66

4 files changed

Lines changed: 185 additions & 30 deletions

File tree

‎packages/agent-core/src/harness/compaction/compaction.test.ts‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,67 @@ describe("calculateContextTokens", () => {
197197
});
198198

199199
describe("session-entry compaction budgeting", () => {
200-
it("weights CJK text as roughly one token per character", () => {
201-
const extensionGText = String.fromCodePoint(0x30000).repeat(4);
202-
200+
it("applies the shared common-CJK budget heuristic", () => {
203201
expect(estimateTokens({ role: "user", content: "hello world", timestamp: 1 })).toBe(3);
204202
expect(estimateTokens({ role: "user", content: "你好世界", timestamp: 1 })).toBe(4);
205203
expect(estimateTokens({ role: "user", content: "こんにちは", timestamp: 1 })).toBe(5);
206-
expect(estimateTokens({ role: "user", content: "コンニチハ", timestamp: 1 })).toBe(5);
207-
expect(estimateTokens({ role: "user", content: extensionGText, timestamp: 1 })).toBe(4);
204+
expect(estimateTokens({ role: "user", content: "안녕하세요", timestamp: 1 })).toBe(5);
205+
});
206+
207+
it("uses conservative weights for halfwidth and supplementary CJK", () => {
208+
expect(estimateTokens({ role: "user", content: "コンニチハ", timestamp: 1 })).toBe(10);
209+
expect(
210+
estimateTokens({ role: "user", content: String.fromCodePoint(0xffa1), timestamp: 1 }),
211+
).toBe(2);
212+
expect(
213+
estimateTokens({ role: "user", content: String.fromCodePoint(0x20000), timestamp: 1 }),
214+
).toBe(4);
215+
expect(
216+
estimateTokens({ role: "user", content: String.fromCodePoint(0x30000), timestamp: 1 }),
217+
).toBe(4);
218+
});
219+
220+
it("uses a conservative weight for rare BMP CJK", () => {
221+
expect(
222+
estimateTokens({ role: "user", content: String.fromCodePoint(0x3400), timestamp: 1 }),
223+
).toBe(3);
224+
expect(
225+
estimateTokens({ role: "user", content: String.fromCodePoint(0x9fff), timestamp: 1 }),
226+
).toBe(3);
227+
});
228+
229+
it("accounts for decomposed Hangul and compatibility forms", () => {
230+
expect(
231+
estimateTokens({ role: "user", content: "안녕하세요".normalize("NFD"), timestamp: 1 }),
232+
).toBe(36);
233+
expect(
234+
estimateTokens({ role: "user", content: String.fromCodePoint(0xfe10), timestamp: 1 }),
235+
).toBe(2);
236+
expect(
237+
estimateTokens({ role: "user", content: String.fromCodePoint(0xffe0), timestamp: 1 }),
238+
).toBe(2);
239+
});
240+
241+
it("uses a conservative weight for supplementary Japanese forms", () => {
242+
expect(
243+
estimateTokens({ role: "user", content: String.fromCodePoint(0x1aff0), timestamp: 1 }),
244+
).toBe(4);
245+
expect(
246+
estimateTokens({ role: "user", content: String.fromCodePoint(0x1f200), timestamp: 1 }),
247+
).toBe(4);
248+
});
249+
250+
it("uses measured weights for CJK script-extension marks", () => {
251+
expect(
252+
estimateTokens({ role: "user", content: String.fromCodePoint(0x00b7), timestamp: 1 }),
253+
).toBe(1);
254+
expect(estimateTokens({ role: "user", content: "·".repeat(32), timestamp: 1 })).toBe(32);
255+
expect(
256+
estimateTokens({ role: "user", content: String.fromCodePoint(0x02ca), timestamp: 1 }),
257+
).toBe(2);
258+
expect(
259+
estimateTokens({ role: "user", content: String.fromCodePoint(0x1d360), timestamp: 1 }),
260+
).toBe(3);
208261
});
209262

210263
it("uses CJK-aware token estimates when choosing the retained tail", () => {

‎packages/normalization-core/src/cjk-chars.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,48 @@ describe("normalization-core/cjk-chars", () => {
1616
expect(estimateStringChars("hi\u4F60\u597D")).toBe(10);
1717
});
1818

19-
it("counts CJK extension surrogate pairs as one weighted code point", () => {
20-
expect(estimateStringChars(String.fromCodePoint(0x20000))).toBe(CHARS_PER_TOKEN_ESTIMATE);
21-
expect(estimateStringChars(String.fromCodePoint(0x30000))).toBe(CHARS_PER_TOKEN_ESTIMATE);
19+
it("uses measured weights for halfwidth and supplementary CJK", () => {
20+
expect(estimateStringChars("コンニチハ")).toBe(5 * CHARS_PER_TOKEN_ESTIMATE * 2);
21+
expect(estimateStringChars(String.fromCodePoint(0xffa1))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
22+
expect(estimateStringChars(String.fromCodePoint(0x20000))).toBe(CHARS_PER_TOKEN_ESTIMATE * 4);
23+
expect(estimateStringChars(String.fromCodePoint(0x30000))).toBe(CHARS_PER_TOKEN_ESTIMATE * 4);
24+
});
25+
26+
it("weights decomposed Hangul and compatibility forms", () => {
27+
const decomposedHangul = "안녕하세요".normalize("NFD");
28+
expect(estimateStringChars(decomposedHangul)).toBe(
29+
decomposedHangul.length * CHARS_PER_TOKEN_ESTIMATE * 3,
30+
);
31+
expect(estimateStringChars(String.fromCodePoint(0xa960))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
32+
expect(estimateStringChars(String.fromCodePoint(0xd7b0))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
33+
expect(estimateStringChars(String.fromCodePoint(0xfe10))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
34+
expect(estimateStringChars(String.fromCodePoint(0xffe0))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
35+
});
36+
37+
it.each([0x2e80, 0x3400, 0x9fff, 0xa000, 0xf900])(
38+
"uses a conservative rare-BMP weight for U+%s",
39+
(codePoint) => {
40+
expect(estimateStringChars(String.fromCodePoint(codePoint))).toBe(
41+
CHARS_PER_TOKEN_ESTIMATE * 3,
42+
);
43+
},
44+
);
45+
46+
it.each([0x16fe3, 0x1aff0, 0x1b001, 0x1b11f, 0x1b132, 0x1f200])(
47+
"uses a conservative supplementary-CJK weight for U+%s",
48+
(codePoint) => {
49+
expect(estimateStringChars(String.fromCodePoint(codePoint))).toBe(
50+
CHARS_PER_TOKEN_ESTIMATE * 4,
51+
);
52+
},
53+
);
54+
55+
it("covers CJK script-extension marks with measured weights", () => {
56+
expect(estimateStringChars(String.fromCodePoint(0x00b7))).toBe(CHARS_PER_TOKEN_ESTIMATE);
57+
expect(estimateStringChars("·".repeat(32))).toBe(32 * CHARS_PER_TOKEN_ESTIMATE);
58+
expect(estimateStringChars(String.fromCodePoint(0x02ca))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
59+
expect(estimateStringChars(String.fromCodePoint(0xa700))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
60+
expect(estimateStringChars(String.fromCodePoint(0x1d360))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
2261
});
2362

2463
it("does not collapse non-CJK surrogate pairs", () => {

‎packages/normalization-core/src/cjk-chars.ts‎

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
/**
22
* Shared CJK-aware character counting for approximate token estimates.
33
*
4-
* Most LLM tokenizers encode CJK characters as roughly one token per
5-
* character, while Latin text averages about one token per four characters.
6-
* The helpers here inflate CJK characters before callers apply the shared
7-
* chars-per-token heuristic.
4+
* This is a provider-independent budget heuristic, not an exact tokenizer.
5+
* Weighting common CJK, rare BMP characters, width-compatibility forms, and
6+
* supplementary ideographs separately keeps current tokenizers within a
7+
* conservative budget range while preserving the existing Latin behavior.
88
*/
99

1010
export const CHARS_PER_TOKEN_ESTIMATE = 4;
1111

12-
const NON_LATIN_RE =
13-
/[\u2E80-\u9FFF\uA000-\uA4FF\uAC00-\uD7AF\uF900-\uFAFF\uFF01-\uFF9F\uFFE0-\uFFE6\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]/gu;
12+
const NON_ASCII_RE = /[\u0080-\u{10FFFF}]/u;
13+
const COMMON_CJK_RE = /[\u00B7\u3000-\u319F\u4E00-\u9FA5\uAC00-\uD7AF\uFF01-\uFF60]/gu;
14+
const RARE_BMP_CJK_RE =
15+
/[\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF]/gu;
16+
const TWO_TOKEN_CJK_RE =
17+
/[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6]|\u{0305}|\u{0323}/gu;
18+
const THREE_TOKEN_SUPPLEMENTARY_CJK_RE = /[\u{1D360}-\u{1D371}]/gu;
19+
const SUPPLEMENTARY_CJK_RE =
20+
/[\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]/gu;
21+
const SPECIAL_CJK_RE =
22+
/[\u{02C7}\u{02C9}-\u{02CB}\u{02D9}\u{02EA}-\u{02EB}\u1100-\u11FF\u2E80-\u2FFF\u31A0-\u4DFF\u9FA6-\u9FFF\uA000-\uA4FF\uA700-\uA707\uA960-\uA97F\uD7B0-\uD7FF\uF900-\uFAFF\uFE10-\uFE4F\uFF61-\uFFDC\uFFE0-\uFFE6\u{16FE0}-\u{16FFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B16F}\u{1D360}-\u{1D371}\u{1F200}-\u{1F2FF}\u{20000}-\u{2FA1F}\u{30000}-\u{3347F}]|\u{0305}|\u{0323}/u;
1423

15-
const CJK_SURROGATE_RE =
16-
/(?:[\uD840-\uD87E][\uDC00-\uDFFF]|[\uD880-\uD88C][\uDC00-\uDFFF]|\uD88D[\uDC00-\uDC7F])/g;
24+
function countMatches(text: string, pattern: RegExp): number {
25+
return (text.match(pattern) ?? []).length;
26+
}
1727

1828
export function estimateStringChars(text: string): number {
19-
if (text.length === 0) {
20-
return 0;
29+
if (!NON_ASCII_RE.test(text)) {
30+
return text.length;
31+
}
32+
const commonCjkCount = countMatches(text, COMMON_CJK_RE);
33+
const commonEstimate = text.length + commonCjkCount * (CHARS_PER_TOKEN_ESTIMATE - 1);
34+
if (!SPECIAL_CJK_RE.test(text)) {
35+
return commonEstimate;
2136
}
22-
const nonLatinCount = (text.match(NON_LATIN_RE) ?? []).length;
23-
const cjkSurrogates = nonLatinCount === 0 ? 0 : (text.match(CJK_SURROGATE_RE) ?? []).length;
24-
const codePointLength = text.length - cjkSurrogates;
25-
return codePointLength + nonLatinCount * (CHARS_PER_TOKEN_ESTIMATE - 1);
37+
const rareBmpCjkCount = countMatches(text, RARE_BMP_CJK_RE);
38+
const twoTokenCjkCount = countMatches(text, TWO_TOKEN_CJK_RE);
39+
const threeTokenSupplementaryCjkCount = countMatches(text, THREE_TOKEN_SUPPLEMENTARY_CJK_RE);
40+
const supplementaryCjkCount = countMatches(text, SUPPLEMENTARY_CJK_RE);
41+
return (
42+
commonEstimate +
43+
rareBmpCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 1) +
44+
twoTokenCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 2 - 1) +
45+
threeTokenSupplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 3 - 2) +
46+
supplementaryCjkCount * (CHARS_PER_TOKEN_ESTIMATE * 4 - 2)
47+
);
2648
}
2749

2850
export function estimateTokensFromChars(chars: number): number {

‎src/utils/cjk-chars.test.ts‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ describe("estimateStringChars", () => {
5454
});
5555

5656
it("handles CJK punctuation and symbols in the extended range", () => {
57-
// "⺀" (U+2E80) is in CJK Radicals Supplement range
58-
expect(estimateStringChars("⺀")).toBe(CHARS_PER_TOKEN_ESTIMATE);
57+
// "⺀" (U+2E80) is a rare radical that current tokenizers encode as 3 tokens.
58+
expect(estimateStringChars("⺀")).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
5959
});
6060

6161
it("does not inflate standard Latin characters", () => {
@@ -68,15 +68,56 @@ describe("estimateStringChars", () => {
6868
expect(estimateStringChars(text)).toBe(text.length);
6969
});
7070

71-
it("counts CJK Extension B characters as one code point", () => {
71+
it("weights CJK Extension B characters for their measured token cost", () => {
7272
// "𠀀" (U+20000) is represented as a surrogate pair in UTF-16.
73-
// Result = 1 + 1 * 3 = 4 (exactly CHARS_PER_TOKEN_ESTIMATE)
74-
expect(estimateStringChars("𠀀")).toBe(CHARS_PER_TOKEN_ESTIMATE);
73+
expect(estimateStringChars("𠀀")).toBe(CHARS_PER_TOKEN_ESTIMATE * 4);
7574
});
7675

77-
it("handles mixed BMP and Extension B CJK consistently", () => {
78-
// 3 CJK code points total: 你 + 𠀀 + 好 => 3 * 4 = 12
79-
expect(estimateStringChars("你𠀀好")).toBe(12);
76+
it("handles mixed BMP and Extension B CJK weights", () => {
77+
expect(estimateStringChars("你𠀀好")).toBe(CHARS_PER_TOKEN_ESTIMATE * 6);
78+
});
79+
80+
it("weights halfwidth Japanese, halfwidth Hangul, and supplementary CJK", () => {
81+
expect(estimateStringChars("コンニチハ")).toBe(CHARS_PER_TOKEN_ESTIMATE * 10);
82+
expect(estimateStringChars(String.fromCodePoint(0xffa1))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
83+
expect(estimateStringChars(String.fromCodePoint(0x30000))).toBe(CHARS_PER_TOKEN_ESTIMATE * 4);
84+
});
85+
86+
it("weights decomposed Hangul and compatibility forms", () => {
87+
const decomposedHangul = "안녕하세요".normalize("NFD");
88+
expect(estimateStringChars(decomposedHangul)).toBe(
89+
decomposedHangul.length * CHARS_PER_TOKEN_ESTIMATE * 3,
90+
);
91+
expect(estimateStringChars(String.fromCodePoint(0xa960))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
92+
expect(estimateStringChars(String.fromCodePoint(0xd7b0))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
93+
expect(estimateStringChars(String.fromCodePoint(0xfe10))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
94+
expect(estimateStringChars(String.fromCodePoint(0xffe0))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
95+
});
96+
97+
it.each([0x2e80, 0x3400, 0x9fff, 0xa000, 0xf900])(
98+
"weights rare BMP CJK U+%s conservatively",
99+
(codePoint) => {
100+
expect(estimateStringChars(String.fromCodePoint(codePoint))).toBe(
101+
CHARS_PER_TOKEN_ESTIMATE * 3,
102+
);
103+
},
104+
);
105+
106+
it.each([0x16fe3, 0x1aff0, 0x1b001, 0x1b11f, 0x1b132, 0x1f200])(
107+
"weights supplementary CJK U+%s conservatively",
108+
(codePoint) => {
109+
expect(estimateStringChars(String.fromCodePoint(codePoint))).toBe(
110+
CHARS_PER_TOKEN_ESTIMATE * 4,
111+
);
112+
},
113+
);
114+
115+
it("covers CJK script-extension marks with measured weights", () => {
116+
expect(estimateStringChars(String.fromCodePoint(0x00b7))).toBe(CHARS_PER_TOKEN_ESTIMATE);
117+
expect(estimateStringChars("·".repeat(32))).toBe(32 * CHARS_PER_TOKEN_ESTIMATE);
118+
expect(estimateStringChars(String.fromCodePoint(0x02ca))).toBe(CHARS_PER_TOKEN_ESTIMATE * 2);
119+
expect(estimateStringChars(String.fromCodePoint(0xa700))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
120+
expect(estimateStringChars(String.fromCodePoint(0x1d360))).toBe(CHARS_PER_TOKEN_ESTIMATE * 3);
80121
});
81122

82123
it("does not collapse non-CJK surrogate pairs like emoji", () => {

0 commit comments

Comments
 (0)