Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): estimate compaction tokens from UTF-8 bytes (#4293)

e0-gh-vercel-connect[bot]69888f12c4f4
diff --git a/.changeset/cjk-token-estimate.md b/.changeset/cjk-token-estimate.md
new file mode 100644
index 000000000..07c1907d0
--- /dev/null
+++ b/.changeset/cjk-token-estimate.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Compaction now estimates tokens from UTF-8 bytes instead of string length, so Chinese, Japanese, Korean, and other non-Latin text no longer counts at a fraction of its real cost. A large tool result in those scripts now triggers compaction before the request overflows the context window.
diff --git a/packages/eve/src/harness/compaction-prompt.ts b/packages/eve/src/harness/compaction-prompt.ts
index 1ea46fc88..38873b174 100644
--- a/packages/eve/src/harness/compaction-prompt.ts
+++ b/packages/eve/src/harness/compaction-prompt.ts
@@ -79,7 +79,7 @@ export function createCompactionPrompt(input: {
* Re-renders the oldest entries with degraded (capped) conversational text
* until the estimated prompt fits the budget. Mutates `entries` in place.
* Savings are tracked per entry instead of re-estimating the whole prompt per
- * iteration; the char-length delta divided by 4 matches the
+ * iteration; the UTF-8 byte delta divided by 4 matches the
* {@link estimateTokens} ruler closely enough for a soft budget.
*/
function degradeOversizedTranscript(
@@ -113,7 +113,7 @@ function degradeOversizedTranscript(
continue;
}
- excessTokens -= (entry.content.length - degraded.length) / 4;
+ excessTokens -= (Buffer.byteLength(entry.content) - Buffer.byteLength(degraded)) / 4;
entries[index] = { content: degraded, role: entry.role };
}
}
diff --git a/packages/eve/src/harness/compaction.test.ts b/packages/eve/src/harness/compaction.test.ts
index 8e4759280..04e180793 100644
--- a/packages/eve/src/harness/compaction.test.ts
+++ b/packages/eve/src/harness/compaction.test.ts
@@ -308,6 +308,38 @@ describe("shouldCompact", () => {
it("does not compact an empty history based on prompt overhead alone", () => {
expect(shouldCompact([], { ...config, threshold: 0 })).toBe(false);
});
+
+ // #4292: a 40,000-character Chinese tool result costs about 30k o200k and
+ // 40k cl100k tokens, but serialized length / 4 counted it as about 10k, so a
+ // measured 165k prompt reached the 200k window without compacting.
+ it("compacts when a large CJK tool result pushes a measured prompt over the threshold", () => {
+ const prompt: ModelMessage[] = [
+ { content: "Summarize the attached Chinese report.", role: "user" },
+ ];
+ const report: ModelMessage = {
+ content: [
+ {
+ output: {
+ type: "text",
+ value: "大型语言模型在长上下文任务中的表现取决于其对检索证据的整合能力。".repeat(1_250),
+ },
+ toolCallId: "call-1",
+ toolName: "read_document",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ };
+
+ expect(
+ shouldCompact([...prompt, report], {
+ lastKnownInputTokens: 165_000,
+ lastKnownPromptMessageCount: prompt.length,
+ recentWindowSize: 4,
+ threshold: 180_000,
+ }),
+ ).toBe(true);
+ });
});
describe("resolveCompactionModel", () => {
diff --git a/packages/eve/src/harness/token-estimate.ts b/packages/eve/src/harness/token-estimate.ts
index f37cd0bee..5aa5a5f5c 100644
--- a/packages/eve/src/harness/token-estimate.ts
+++ b/packages/eve/src/harness/token-estimate.ts
@@ -14,10 +14,12 @@ const HEADER_BASE64_CHARS = 96 * 1024;
const REMOTE_URL = /^https?:\/\//i;
/**
- * Rough token estimate: serialized JSON length / 4, with file parts counted
- * at what providers bill for media instead of their base64 length. Good
- * enough for deciding whether compaction is needed; the real token count
- * comes back from the model each step via
+ * Rough token estimate: serialized JSON UTF-8 bytes / 4, with file parts
+ * counted at what providers bill for media instead of their base64 length.
+ * Bytes, not string length: a CJK character is one UTF-16 unit but about one
+ * token, and its three UTF-8 bytes track that cost far better. Good enough
+ * for deciding whether compaction is needed; the real token count comes back
+ * from the model each step via
* `CompactionConfig.lastKnownInputTokens`.
*
* Accepts any JSON-serializable value so callers can apply the same heuristic
@@ -35,7 +37,7 @@ export function estimateTokens(value: unknown): number {
};
return { mediaType, type };
});
- return (serialized?.length ?? 0) / 4 + mediaTokens;
+ return Buffer.byteLength(serialized ?? "") / 4 + mediaTokens;
}
/**