Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): count Japanese kana and Korean Hangul in the output-cap CJK correction
xpeng5278-web38ffd0c176f9
diff --git a/packages/agent-runtime/src/output-cap.test.ts b/packages/agent-runtime/src/output-cap.test.tsindex c36e3726e..7bf997caf 100644--- a/packages/agent-runtime/src/output-cap.test.ts+++ b/packages/agent-runtime/src/output-cap.test.ts@@ -30,6 +30,69 @@ describe("estimateOutputCapInputTokens", () => {expect(estimateOutputCapInputTokens(context)).toBe(1000);});+ it("counts Japanese kana at ~1 token per char, same as Han", () => {+ const han = estimateOutputCapInputTokens({+ messages: [{ role: "user", content: "中".repeat(1000) }],+ });+ // 9-char hiragana/katakana phrase, sliced to the same 1000 chars as Han.+ const kana = "こんにちはカタカナ".repeat(112).slice(0, 1000);+ expect(kana).toHaveLength(1000);+ // chars/4 baseline = 250; the 0.75/char correction brings this to 1000.+ expect(+ estimateOutputCapInputTokens({ messages: [{ role: "user", content: kana }] }),+ ).toBe(han);+ });++ it("counts Korean Hangul at ~1 token per char, same as Han", () => {+ const han = estimateOutputCapInputTokens({+ messages: [{ role: "user", content: "中".repeat(1000) }],+ });+ const hangul = "안녕하세요".repeat(200);+ expect(hangul).toHaveLength(1000);+ expect(+ estimateOutputCapInputTokens({+ messages: [{ role: "user", content: hangul }],+ }),+ ).toBe(han);+ });++ it("counts Hangul jamo, katakana extensions, and compatibility ideographs like Han", () => {+ const estimate = (content: string) =>+ estimateOutputCapInputTokens({ messages: [{ role: "user", content }] });+ const han = estimate("中".repeat(1000));+ expect({+ jamo: estimate("\u1100".repeat(1000)),+ compatJamo: estimate("\u3131".repeat(1000)),+ kanaExtension: estimate("\u31F0".repeat(1000)),+ compatIdeograph: estimate("\uF900".repeat(1000)),+ }).toEqual({+ jamo: han,+ compatJamo: han,+ kanaExtension: han,+ compatIdeograph: han,+ });+ });++ it("does not count ASCII, Latin, or Cyrillic as CJK", () => {+ const ascii = "a".repeat(1000);+ const latin = "café".repeat(250);+ const cyrillic = "я".repeat(1000);+ expect(latin).toHaveLength(1000);+ expect(cyrillic).toHaveLength(1000);+ // chars/4 only. Counting é or я would add ceil(n * 0.75) on top of 250.+ expect(+ estimateOutputCapInputTokens({ messages: [{ role: "user", content: ascii }] }),+ ).toBe(250);+ expect(+ estimateOutputCapInputTokens({ messages: [{ role: "user", content: latin }] }),+ ).toBe(250);+ expect(+ estimateOutputCapInputTokens({+ messages: [{ role: "user", content: cyrillic }],+ }),+ ).toBe(250);+ });+it("counts image blocks via the wire-format estimate", () => {const context: OutputCapContext = {messages: [@@ -329,6 +392,42 @@ describe("clampOutputToContext", () => {expect(clamped).toBeLessThan(92_709);});+ it("cuts the output budget for a Hangul-heavy session near the window edge", () => {+ // Same shape as the Han case. A syllable is ~1 token; chars/4 alone would+ // leave the requested output in place and let input + output pass the window.+ const context: OutputCapContext = {+ messages: [+ { role: "user", content: "안녕하세요".repeat(40_000) },+ {+ role: "assistant",+ content: [+ { type: "text", text: "已" },+ {+ type: "toolCall",+ name: "read_file",+ arguments: JSON.stringify({ path: "/tmp/a.md" }),+ },+ ],+ },+ ],+ };+ const estimate = estimateOutputCapInputTokens(context);+ const clamped = clampOutputToContext(LARGE_WINDOW_MODEL, context, 92_709);+ expect(estimate).toBeGreaterThan(160_000);+ // estimate (≥160k) + clamped + reserve (4096) must fit the 262144 window,+ // with the budget cut to the context remainder.+ expect(estimate + clamped + 4096).toBeLessThanOrEqual(+ LARGE_WINDOW_MODEL.contextWindow,+ );+ expect(clamped).toBe(LARGE_WINDOW_MODEL.contextWindow - estimate - 4096);+ expect(clamped).toBeLessThan(92_709);+ // 200k syllables at ~1 token each, independent of the estimator. Returning+ // the requested 92_709 would make this sum exceed the window.+ expect(200_000 + clamped).toBeLessThanOrEqual(+ LARGE_WINDOW_MODEL.contextWindow,+ );+ });+it("keeps the requested budget when the window is unknown", () => {const model = { contextWindow: 0, maxTokens: 4096 };expect(clampOutputToContext(model, BASE_CONTEXT, 8888)).toBe(8888);diff --git a/packages/agent-runtime/src/output-cap.ts b/packages/agent-runtime/src/output-cap.tsindex c3fa25c6a..0d9f7e81e 100644--- a/packages/agent-runtime/src/output-cap.ts+++ b/packages/agent-runtime/src/output-cap.ts@@ -112,18 +112,34 @@ const OUTPUT_SAFETY_WINDOW_RATIO = 0.01;const CHARS_PER_TOKEN = 4;const ESTIMATED_CHARS_PER_IMAGE = 4800;+// Han, Japanese kana, and Korean Hangul tokenize at ~1 token per character,+// as do CJK punctuation and full-width forms. The chars/4 baseline charges+// all of these 0.25.function countCjkChars(text: string): number {let count = 0;for (const ch of text) {const code = ch.codePointAt(0) ?? 0;if (+ // Hangul Jamo.+ (code >= 0x1100 && code <= 0x11ff) ||+ // CJK punctuation.+ (code >= 0x3000 && code <= 0x303f) ||+ // Hiragana and katakana.+ (code >= 0x3040 && code <= 0x30ff) ||+ // Hangul compatibility Jamo.+ (code >= 0x3130 && code <= 0x318f) ||+ // Katakana phonetic extensions.+ (code >= 0x31f0 && code <= 0x31ff) ||// CJK Unified Ideographs + Extension A.(code >= 0x3400 && code <= 0x9fff) ||+ // Hangul syllables.+ (code >= 0xac00 && code <= 0xd7af) ||+ // CJK compatibility ideographs.+ (code >= 0xf900 && code <= 0xfaff) ||+ // Full-width and half-width forms.+ (code >= 0xff00 && code <= 0xffef) ||// CJK Extension B..F (historical/supplementary plane).- (code >= 0x20000 && code <= 0x2ebef) ||- // CJK punctuation and full-width forms.- (code >= 0x3000 && code <= 0x303f) ||- (code >= 0xff00 && code <= 0xffef)+ (code >= 0x20000 && code <= 0x2ebef)) {count += 1;}