Skip to content

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

fix(agent-runtime): apply the CJK correction to tool-call arguments in the output cap

xpeng5278-web59a0e6df39e3
diff --git a/packages/agent-runtime/src/output-cap.test.ts b/packages/agent-runtime/src/output-cap.test.ts
index c36e3726e..61d623fbb 100644
--- a/packages/agent-runtime/src/output-cap.test.ts
+++ b/packages/agent-runtime/src/output-cap.test.ts
@@ -30,6 +30,46 @@ 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 image blocks via the wire-format estimate", () => {
const context: OutputCapContext = {
messages: [
@@ -298,6 +338,31 @@ describe("clampOutputToContext", () => {
expect(clamped).toBe(1);
});
+ it("cuts the output budget when CJK tool arguments fill a 128k window", () => {
+ // 110k Han characters in a Write payload. chars/4 alone is 27505 and
+ // would leave a requested 32768 max_tokens untouched on a 128k window.
+ const model = { contextWindow: 128_000, maxTokens: 32_768 };
+ const context: OutputCapContext = {
+ messages: [
+ {
+ role: "assistant",
+ content: [
+ {
+ type: "toolCall",
+ name: "Write",
+ arguments: { content: "中".repeat(110_000) },
+ },
+ ],
+ },
+ ],
+ };
+ const estimate = estimateOutputCapInputTokens(context);
+ const clamped = clampOutputToContext(model, context, 32_768);
+ expect(estimate).toBe(110_005);
+ expect(clamped).toBeLessThan(32_768);
+ expect(estimate + clamped + 4096).toBeLessThanOrEqual(128_000);
+ });
+
it("cuts the output budget for a CJK-heavy session near the window edge", () => {
// A CJK-heavy session near the edge of the window must have its output
// budget cut so `estimated input + output + reserve` still fits. The
diff --git a/packages/agent-runtime/src/output-cap.ts b/packages/agent-runtime/src/output-cap.ts
index c3fa25c6a..adf4ccc6e 100644
--- a/packages/agent-runtime/src/output-cap.ts
+++ b/packages/agent-runtime/src/output-cap.ts
@@ -172,7 +172,11 @@ function estimateMessageTokens(
) {
const payload = stringifyForEstimate(b.arguments ?? b.input);
chars += b.name.length + payload.length;
- cjkChars += countCjkChars(b.name);
+ // JSON.stringify leaves Han unescaped, so the payload the chars/4
+ // baseline measured is the same string the correction has to see.
+ // A \uXXXX form would already be ASCII in that string; decoding it
+ // would charge the character twice.
+ cjkChars += countCjkChars(b.name) + countCjkChars(payload);
} else if (b.type === "hostedSearch") {
// 与适配器共用回放投影;搜索结果不是图片,也不是普通工具参数。
// 投影会校验结构,失败必须保留为本地错误,不能按零成本继续请求。