Skip to content

fix(agent-runtime): bound retained CJK text to the token budget - #2340

Merged
vastsa merged 1 commit into
vastsa:mainfrom
xpeng5278-web:fix/cjk-retention-truncation
Nov 13, 2026
Merged

vastsa merged 1 commit into
vastsa:mainfrom
xpeng5278-web:fix/cjk-retention-truncation

Conversation

@xpeng5278-web

Copy link
Copy Markdown
Contributor

Fixes #2339

Summary

Retained tails, checkpoint user messages, and a delegate’s retained task brief now shrink CJK text until estimateOutputCapInputTokens fits the token budget. ASCII still starts from the historical 3.5 chars-per-token ceiling and is left at that ceiling when it already fits.

Root cause

Those three paths converted a token budget to a character budget with ESTIMATED_TEXT_CHARS_PER_TOKEN (3.5) only. The next request is gated on the output-cap estimate, which charges CJK at about one token per character, so a long Chinese brief was kept at several times the intended budget. Selection used pi-ai’s chars/3.5 estimateTokens, so some CJK messages were not truncated at all.

Fix

charBudgetWithinOutputCap / truncateTextWithinOutputCap in output-cap.ts walk the character ceiling down until the output-cap estimate fits. truncateMessageToTail, truncateUserMessageForCheckpoint, and truncateDelegateUserMessage use that helper. messageTokens (retained tail) and selectRetainedUserMessages take max(estimateTokens, estimateOutputCapInputTokens) so a long CJK message is treated as large enough to truncate. The spec sentence on token-budget truncation records the CJK shrink. The global 3.5 ratio is unchanged.

Tests

  • output-cap.test.ts: ASCII stays at the character ceiling; CJK above the ceiling, and CJK that fits the ceiling but not the token budget, shrink to ≤ the budget.
  • compaction-tail.test.ts: 1600 × 「中」 at budget 200 is ≤ 200 after the cut (on main it was 643); ASCII of the same ceiling keeps its length.
  • runtime.test.ts and subagent-context.test.ts: a CJK user message / delegate brief that the 3.5 estimate would have kept whole is truncated, and an ASCII brief at the ceiling is not pulled down.
  • Before the fix (compaction-tail tests only, on main @ 62a76875e): 2 failed / 21 passed (23); 1600 × 「中」 at budget 200 still estimates 643. After: the three dedicated files 85/85. runtime.test.ts adds one passing case versus clean main; both sides share the same pre-existing local failures (including stale linked @pi-desktop/shared after fix(composer): reference pasted disk files with sizes #2316's appendLocalFileReferenceContext).
  • tsc -p packages/agent-runtime/tsconfig.json --noEmit: no new diagnostics in the touched files. Pre-existing local failures come from a stale linked @pi-desktop/shared / @earendil-works/pi-ai.

Scope

output-cap.ts, compaction-tail.ts, subagent-context.ts, the truncation/selection helpers in runtime.ts, their tests, and one sentence in 02-agent-runtime.md.

Retained tails, checkpoint user messages, and a delegate's retained
task brief converted a token budget to characters with the 3.5
chars-per-token ratio. That under-counts Han, kana, and Hangul, so a
long Chinese brief was kept at several times the budget the output
cap uses. ASCII still uses the historical ceiling; CJK is shrunk
until the output-cap estimate fits.
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

@pwhatcode is attempting to deploy a commit to the vastsa's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independently verified against main d2a2556: replacing the four production modules with baseline versions makes the two CJK retained-tail regressions fail (21 pass / 2 fail). Restoring this head passes all 507 tests across output-cap, compaction-tail, subagent-context and runtime, including controlled-provider compaction coverage. Shared/plugin SDK builds and runtime typecheck pass with isolated frozen-lockfile dependencies (host links were stale). The fix aligns selection and truncation with the existing output-cap estimator while preserving the ASCII ceiling and tool identities. No contributor source changes. Actual PR merge ref and tested head have identical tree 5b206016182320cd4420963fe6adfc8f05579630; latest main is an ancestor. All remote code checks, including Electron and Windows E2E, pass; Vercel site deployment has unrelated team authorization failure. No live-provider calls or local packaged-app/cross-platform E2E claimed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Compaction retained tails, checkpoint user messages, and delegate briefs keep several times the token budget when the text is CJK

3 participants