Skip to content

fix(agent-runtime): size compaction summary prompts with the CJK-aware estimate - #2366

Merged
vastsa merged 2 commits into
vastsa:mainfrom
koeria:fix/compaction-summary-cjk-2364
Nov 16, 2026
Merged

vastsa merged 2 commits into
vastsa:mainfrom
koeria:fix/compaction-summary-cjk-2364

Conversation

@koeria

@koeria koeria commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

根因

Compaction 的摘要请求(compaction-request.ts → completeSimple)不走 agent 的 streamFn,因此没有 output-cap 闸门;唯一的预算检查 estimateSummaryPromptTokens 用 chars/3.5 的英文比例估算序列化后的 prompt。Han/kana/Hangul 实际按约 1 token/字编码,一个「看起来能放下」的 CJK prompt 被整段发出,被 provider 以 context overflow 拒绝,随后该 turn 回退到 retained-tail,摘要被丢弃。

#2340 已用 output-cap 的 CJK 估算器约束 retained tail、checkpoint user message 与 delegate brief,唯独摘要请求本身仍留在英文比例上。

修复

  • estimateSummaryPromptTokens(及共享的 estimatePromptTextTokens):在 chars/3.5 基线上加回 0.75 token/CJK 字符的缺口,与 output-cap.ts 的 countCjkChars 同一口径;ASCII 估算不变。
  • planSummaryChunks:每条消息的 chunk 成本 = pi 估算 + CJK 缺口;单条超预算的 CJK 消息改为按 CJK 口径二分截断(纯 ASCII 保持原 chars/3.5 截断上限,行为不变)。
  • compactionSummaryReserveTokens 对携带的 previous summary 使用同一 CJK 口径。
  • subagent-context.ts 的 delegate brief 截断同样改为 CJK-aware(否则截断后的 CJK brief 仍会越过新估算下的预算而被误判 overflow)。
  • docs/spec/03-runtime/02-agent-runtime.md 同步说明预检守卫的 CJK 校正。

验证

  • packages/agent-runtime:vitest run compaction-summary-input / subagent-context / output-cap / runtime / session-context / context-usage-compaction / pi-coding-agent-compaction / hosted-search-compaction 全绿(538 tests)。
  • 新增用例覆盖 issue 复现:1600 个 CJK 字符不再被估成 ~460 token(≥1500),ASCII 1600 字符仍 ~457;summaryInputLimit=800 不再放行 1600 字 CJK prompt;CJK 范围被正确拆块/截断。
  • tsc --noEmit(4GB heap)0 错误。

Fixes #2364

@koeria

koeria commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Note: the earlier #2365 for this issue fails the JS unit-test step (subagent-context.test.ts:394, expected "compacted" got "overflow") because its shared-estimator change makes the delegate CJK brief, truncated at the chars/3.5 cap, still exceed the budget it is then measured against. This PR sizes the delegate brief truncation with the same CJK-aware estimate so the truncation result and the guard agree; see truncateDelegateUserMessage.

vastsa commented Nov 15, 2026

Copy link
Copy Markdown
Owner

独立复查 head 5ebb31a,并合入最新 main 2270533 生成 integration 819f4bb4b 后执行三组生产测试(compaction-summary-input / subagent-context / output-cap):84 通过、5 失败;相同 main 基线 84/84 通过。CJK 摘要低估问题已在 #2365 审查中复现,但本 PR 目前带入明确回归,暂不合入。

  1. output-cap.ts 删除 charBudgetWithinOutputCap / truncateTextWithinOutputCap,runtime.ts、compaction-tail.ts 仍导入调用;output-cap.test.ts 三例直接 TypeError: truncateTextWithinOutputCap is not a function。请保留既有共享截断函数,仅导出/复用 countCjkChars。
  2. compactDelegateContext 撤销 historyOffset 对 system row 的处理,已有 second boundary 测试不再出现 ,把上次 checkpoint 当普通历史重新摘要。请保留已有摘要续接逻辑。
  3. long CJK brief 既有测试仍从 compacted 变为 overflow。新增 truncateDelegateUserMessage 使用 summary estimator,不等同于实际 output-cap retention guard;请沿用已有 output-cap 截断口径,验证 retained brief 真正落在 guard 预算内。

建议先移除这些既有逻辑的回退,以最小范围补齐 summary prompt/chunk/previous-summary 的 CJK 估算;保留所有现有回归断言,在最新 main 集成候选上重跑后再审。

@koeria
koeria force-pushed the fix/compaction-summary-cjk-2364 branch from 5ebb31a to 509bad6 Compare November 16, 2026 12:25
koeria and others added 2 commits November 16, 2026 12:30
…elegate summaries (vastsa#2364)

- output-cap: align with main and export countCjkChars; keep all shared truncation helpers
- compaction-summary-input: single CJK-aware estimate shared by the session guard, the chunk planner, and chunk truncation (issue vastsa#2364)
- subagent-context: when even the reduced delegate summary input cannot fit one prompt, split it with the shared planSummaryChunks and chain the summaries instead of skipping the summary (issue vastsa#827 machinery)
- specs: document the delegate compaction chunking behavior
@vastsa

vastsa commented Nov 16, 2026

Copy link
Copy Markdown
Owner

复查更新:此前三类回归均已消除。已在独立 worktree 合入最新 main e17e5e7,集成候选 64352a7 上运行 runtime 全量测试:135 文件、2039 项通过;三组重点测试 89/89 通过。重新 bundle 后,生产 sidecar + 本地 Responses fixture 的 e2e-context-usage-compaction 三组场景通过(cached-input、partial-buckets、normal-usage),diff check 通过。原始 CJK 低估缺陷此前已独立复现。本轮没有调用真实付费 provider。

已保留作者提交,非强制推送最新 main 的合入提交,修复 Head contains latest base 门禁。等待此精确 head 的全部 CI 通过后再作最终合入检查。

@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.

复查更新:此前三类回归均已消除。已在独立 worktree 合入最新 main e17e5e7,集成候选 64352a7 上运行 runtime 全量测试:135 文件、2039 项通过;三组重点测试 89/89 通过。重新 bundle 后,生产 sidecar + 本地 Responses fixture 的 e2e-context-usage-compaction 三组场景通过(cached-input、partial-buckets、normal-usage),diff check 通过。原始 CJK 低估缺陷此前已独立复现。本轮没有调用真实付费 provider。

已保留作者提交,非强制推送最新 main 的合入提交,修复 Head contains latest base 门禁。等待此精确 head 的全部 CI 通过后再作最终合入检查。

追加验收:生产 Host 的 e2e-compaction-tail 通过(restart=true),验证压缩尾部的持久化及重启恢复。

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 summary prompts keep several times the token budget when the text is CJK

2 participants