Repository navigation
fix(agent-runtime): bound retained CJK text to the token budget - #2340
Conversation
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.
|
@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
left a comment
There was a problem hiding this comment.
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.
Fixes #2339
Summary
Retained tails, checkpoint user messages, and a delegate’s retained task brief now shrink CJK text until
estimateOutputCapInputTokensfits 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.5estimateTokens, so some CJK messages were not truncated at all.Fix
charBudgetWithinOutputCap/truncateTextWithinOutputCapinoutput-cap.tswalk the character ceiling down until the output-cap estimate fits.truncateMessageToTail,truncateUserMessageForCheckpoint, andtruncateDelegateUserMessageuse that helper.messageTokens(retained tail) andselectRetainedUserMessagestakemax(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.tsandsubagent-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.main@62a76875e): 2 failed / 21 passed (23); 1600 × 「中」 at budget 200 still estimates 643. After: the three dedicated files 85/85.runtime.test.tsadds one passing case versus clean main; both sides share the same pre-existing local failures (including stale linked@pi-desktop/sharedafter fix(composer): reference pasted disk files with sizes #2316'sappendLocalFileReferenceContext).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 inruntime.ts, their tests, and one sentence in02-agent-runtime.md.