Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

merge(main): retain both CJK estimator regressions

vastsa5a33f85a1345
diff --cc packages/agent-runtime/src/output-cap.test.ts
index 61d623fbb,7bf997caf..88b58a39a
--- a/packages/agent-runtime/src/output-cap.test.ts
+++ b/packages/agent-runtime/src/output-cap.test.ts
@@@ -30,46 -30,69 +30,109 @@@ describe("estimateOutputCapInputTokens"
expect(estimateOutputCapInputTokens(context)).toBe(1000);
});
+ it("counts CJK inside tool-call arguments at ~1 token per char", () => {
+ // JSON.stringify leaves Han unescaped. "Write" (5) plus
+ // {"content":"<1000 中>"} (1014) is 1019 chars: baseline 255, and the
+ // correction adds ceil(1000 * 0.75) = 750.
+ const context: OutputCapContext = {
+ messages: [
+ {
+ role: "assistant",
+ content: [
+ {
+ type: "toolCall",
+ name: "Write",
+ arguments: { content: "中".repeat(1000) },
+ },
+ ],
+ },
+ ],
+ };
+ expect(estimateOutputCapInputTokens(context)).toBe(1005);
+ });
+
+ it("leaves ASCII tool-call arguments on the chars/4 baseline", () => {
+ const context: OutputCapContext = {
+ messages: [
+ {
+ role: "assistant",
+ content: [
+ {
+ type: "toolCall",
+ name: "Write",
+ arguments: { content: "a".repeat(1000) },
+ },
+ ],
+ },
+ ],
+ };
+ // Same name and JSON wrapper as the CJK case, so the correction adds 0.
+ expect(estimateOutputCapInputTokens(context)).toBe(255);
+ });
+
+ 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: [