fix: fall back to compact tool preamble before rejecting oversized tool payloads#74
Closed
baily-zhang wants to merge 1 commit into
Closed
fix: fall back to compact tool preamble before rejecting oversized tool payloads#74baily-zhang wants to merge 1 commit into
baily-zhang wants to merge 1 commit into
Conversation
Owner
Owner
|
谢了 baily,思路对得不能再对。这条线一周已经走过两轮:
你这版的 root cause 诊断跟现有实现重合,差别基本上只是命名和分支风格。为了不让 history 里同一条改动两个人重写,先按现有 commit 关掉这个 PR。 反代复用机制能走到今天 #36 / #45 / #61 / #62 都是你扛起来的,下一版 release notes 会再写一次。下次有诡异行为可以直接发 issue 带个最小复现,几小时内能落 commit,比 PR 快。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

问题
最近引入的 tool preamble 预算逻辑会在 完整 tool 定义 超过 hard cap 时直接返回:
{ "type": "error", "error": { "type": "invalid_request_error", "message": "Tool definitions are too large (74KB > 47KB). Reduce the number of tools or shorten parameter schemas." } }在 Claude Code / opencode / Cline 这类客户端里,首轮请求通常会附带 20+ 个工具和较大的参数 schema。这样会导致:
buildCompactToolPreambleForProto(),但这条 fallback 根本没有被用到影响
这不是偶发上游错误,而是本地预算逻辑的回归:
根因
当前逻辑顺序是:
full > hard cap时直接tool_preamble_too_largefull > soft cap && full <= hard cap时才会走 compact fallback也就是说,最需要 compact fallback 的场景反而被提前 reject 了。
修复
这次改动把预算决策抽成独立 helper,并改成下面的顺序:
full <= soft:继续使用完整 preamblefull > soft:先尝试 compact fallbackcompact <= hard:使用 compact preamble 继续请求full和compact都超过 hard cap 时,才返回 400这样可以保证:
74KB > 47KB在入口直接报错验证
新增回归测试覆盖两种情况:
full > hard且compact <= hard:必须 fallback 到 compactfull > hard且compact > hard:才允许 reject本地聚焦测试:
另外用一份 30 个工具的大请求实测,本地日志现在会出现:
请求可以继续返回 200,而不是直接 400。