Skip to content

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

Closed
xpeng5278-web wants to merge 1 commit into
vastsa:mainfrom
xpeng5278-web:fix/summary-prompt-cjk
Closed

xpeng5278-web wants to merge 1 commit into
vastsa:mainfrom
xpeng5278-web:fix/summary-prompt-cjk

Conversation

@xpeng5278-web

Copy link
Copy Markdown
Contributor

Fixes #2364

Summary

The compaction summary preflight, previous-summary reserve, chunk planner, and single-message truncation now size the serialized prompt with the chars/3.5 baseline plus the output-cap CJK correction. A CJK prompt that used to look several times cheaper than it is is reduced or split instead of being sent whole.

Root cause

The summary request uses completeSimple and never passes streamFn, so output-cap cannot catch an underestimate. estimateSummaryPromptTokens divided serialized length by 3.5, and the planner used a raw message estimate with a budget * 3.5 character cut — enough for ASCII, several times too long for Han, kana, and Hangul.

Fix

Tests

  • compaction-summary-input.test.ts: 1600 中 characters now estimate above 800 (and near the output-cap figure); a CJK previousSummary is reserved at the same rate; a short CJK history outranks a longer ASCII turn prefix; the CJK correction applies after the serialized tool-result cap; CJK ranges split so each chunk stays inside the budget; one over-budget CJK message is cut; ASCII still uses the budget * 3.5 character ceiling.
  • Before the fix (tests only, on main @ 525ec4986): compaction-summary-input 7 failed / 23 passed (30). After: 30/30.
  • Related: runtime.test.ts -t "summarizes a range no single prompt can carry" still passes.
  • tsc -p packages/agent-runtime/tsconfig.json --noEmit: exit 0.

Scope

compaction-summary-input.ts, its tests, and one sentence in 02-agent-runtime.md.

…e token estimate

The summary preflight measured the serialized prompt at 3.5 characters
per token. That ratio counts Han, kana, and Hangul as about a third of
a token, so a prompt that overflows the model still looked small enough
to send whole. The chunk planner sized messages with a different raw
estimate, which is the same split that previously let a reserved
summary leave the window it was planned for.

Keep the chars/3.5 baseline for ASCII, add the output-cap CJK
correction on the serialized text, and plan, reserve, and truncate
chunks with that same estimate.
@vastsa

vastsa commented Nov 15, 2026

Copy link
Copy Markdown
Owner

复查 23645c9:已独立确认原 CJK 估算缺陷(新用例在基线 7 失败、候选 30/30 通过)。但现有 subagent-context.test.ts:394 的长 CJK task brief 回归在本地稳定复现:期望 compacted,实际 overflow(24 通过、1 失败),与当前 JS CI 相同。请继续修复真实子代理压缩路径,确保修正 summary 预算后仍能完成分块/保留活动 brief;不要仅改成接受 overflow 或跳过该测试。需要覆盖长 CJK brief 经 prepareDelegateTurnContext 成功压缩、保留受预算约束的 brief,并重跑相邻回归与完整 runtime 套件后再合入。

@Tioit-Wang

Tioit-Wang commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

感谢 @xpeng5278-web 提交这个 PR,也谢谢你为 PI-Desktop 花的时间!🙏

不过目前 CI 还有报错,需要麻烦你再处理一下:

  • JS build / typecheck / lint / architecture / test → 失败步骤 Unit tests:
    • packages/agent-runtime/src/subagent-context.test.ts → truncates a long CJK brief to the output-cap retention budget
    • AssertionError: expected 'overflow' to be 'compacted'(src/subagent-context.test.ts:394)
    • 即本 PR 的 summary 预算修正生效后,子代理压缩路径在这条长 CJK brief 用例上仍然判定为 overflow,没有完成 compacted。
  • Vercel – pi-desktop → 部署授权失败(团队侧问题,贡献者无需处理,可忽略)。

换句话说:CJK 估算的根因修复方向是对的(新增用例在基线失败、在候选通过),但 prepareDelegateTurnContext 的真实子代理压缩路径还需要一并修好,才能让整条 subagent-context 回归转绿。

CI 全绿是本仓库的合入硬门槛(AGENTS.md §12 / §15),所以在这一项转绿之前,这个 PR 无法进入评审与合入流程。

⏳ 超时提醒:本仓库对 CI 未通过的 PR 设有 72 小时超时机制。以本评论发出时刻(2026-11-19 11:50 (UTC+8))起算,截止时间为 2026-11-22 11:50 (UTC+8)。若届时 CI 仍未转绿,该 PR 将进入超时状态并被关闭。

关闭并不等于否定这份贡献——修好后随时可以重新打开或另开 PR 继续推进。如果在收敛这条压缩路径时遇到困难,或者你希望把 CJK 估算修正单独拆成更小的 PR,直接在这里回复就好。

再次感谢你的贡献!

@Tioit-Wang

Copy link
Copy Markdown
Collaborator

感谢 @xpeng5278-web 提交这个 PR,也谢谢你为 PI-Desktop 花的时间!🙏

补充一条提醒(与此前那条 CI 提醒一并处理):当前这个 PR 与最新的 main 存在合并冲突(GitHub 标记 mergeable = false / mergeable_state = dirty),需要先解决冲突,CI 才有意义地复跑。

冲突文件

  • packages/agent-runtime/src/compaction-summary-input.ts
  • packages/agent-runtime/src/compaction-summary-input.test.ts

冲突性质

两处都是代码级冲突,说明 main 上同期已经有其他改动动到了 compaction summary 的预算逻辑。请以当前 main 的实现为基线重新对齐你的修改,而不是强行保留旧版本 —— 否则可能把主线新增的修复覆盖掉。

建议处理方式

git fetch origin main
git rebase origin/main
# 解决两个文件的冲突后
git rebase --continue
git push --force-with-lease

验证

  • pnpm -r --if-present test(至少覆盖 packages/agent-runtime)
  • 确认此前那条 subagent-context.test.ts:394 的 expected 'overflow' to be 'compacted' 仍然修复、没有因为 rebase 回退

CI 全绿 + 无冲突是本仓库的合入前置条件(AGENTS.md §12 / §15)。

如果真的与 main 的实现冲突到难以调和,也可以考虑先关掉这个 PR,基于最新 main 用更小的范围重开一个 —— 这比在一棵冲突较多的分支上反复 rebase 更省力。

再次感谢你的贡献!

@Tioit-Wang

Copy link
Copy Markdown
Collaborator

补充一条更正,比上面那条“请 rebase”的提醒更重要,请先看这条。

我在核对冲突细节时发现:

也就是说:你在这个 PR 里做的事情已经在上游落地了,只是由另一个 PR 先合入。这也解释了为什么这里会报冲突 —— 冲突的两个文件正是 main 上已被 #2366 改写的同一批文件。

建议

  • 如果本 PR 没有 fix(agent-runtime): size compaction summary prompts with the CJK-aware estimate #2366 未覆盖的额外内容,直接关闭即可,不需要再花力气 rebase。你的分析和实现方向是对的,最终也确实按同样的思路被合入了。
  • 如果你认为自己的版本有 main 上缺失的部分(例如某条边界用例、某种截断策略),请在评论里指出具体是哪一处,我们可以基于最新 main 单独开一个小 PR 补上。

抱歉上面那条先给了 rebase 建议,那是在还没定位到 #2366 时的通用提示,以这条为准。

再次感谢你的贡献 —— 这个 CJK 估算问题本身很实在。

@vastsa

vastsa commented Nov 20, 2026

Copy link
Copy Markdown
Owner

This branch currently conflicts with main in packages/agent-runtime/src/compaction-summary-input.ts and its test (161 commits behind), and the last JS build / typecheck / lint / architecture / test run failed. Please merge the latest main, resolve the conflict, and get CI green; we'll review after that.

@vastsa

vastsa commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for your work on this PR and the time you put into PI-Desktop! 🙏

The branch has fallen well behind main and still has merge conflicts, with no updates since the last review, so we are closing it for now to keep the queue manageable. This is not a rejection of the fix — you are very welcome to rework it on top of the latest main and open a new PR, addressing the earlier review comments.

@vastsa vastsa closed this Nov 23, 2026
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

4 participants