fix(llm): max_tokens 截断不再静默降级成空参数

长参数工具调用(整段脚本/大 JSON)被 core.llm.max_tokens 从中间切断时,
上游回 finish_reason=length,而旧实现把这个信号整个丢掉:残缺 JSON 解析失败
后静默降级成空 map,工具只看到参数为空并报 'path is required'。模型因此完全
看不出真因,原样重试四遍、次次撞同一堵墙(2026-09-19 实测 4 次 files_write 失败)。

注:files_read 并未失败——是写挂之后模型反复重写把读卷进同一轮,看起来像两者都报错。

改法:
- finish_reason=length 时不再静默降级,改为塞入 __truncated_error 指引,
  告诉模型「参数被截断 + 请拆成多次调用/追加写 + 勿原样重试」;
- executeToolCallInner 见到该标记即短路,不拿空参数去调工具;
- 非截断的残缺 JSON 保持旧行为(避免把「厂商不回 finish_reason」误判成截断)。

回归测试 2 条钉死这两面。
This commit is contained in:
JianFeeeee
2026-09-19 13:49:21 +08:00
parent fa61247eec
commit ee4d079c49
4 changed files with 114 additions and 5 deletions

View File

@ -44,6 +44,14 @@ func (a *Agent) executeToolCall(tc agentAPI.ToolCall, channel string, turnScenes
}
func (a *Agent) executeToolCallInner(tc agentAPI.ToolCall, channel string, turnScenes []string) string {
// 参数被 max_tokens 截断(见 accumulateStream):**不要**拿着残缺/空参数去调工具。
// 否则工具会报 "path is required" 这类与真因无关的错,模型看不出是截断,
// 只会原样重试(实测连续 4 次)。直接把可执行的指引交回模型。
if msg, ok := tc.Arguments["__truncated_error"].(string); ok && msg != "" {
log.Printf("[agent] tool %s skipped: arguments were truncated by max_tokens", tc.Name)
return msg
}
switch {
case tc.Name == "persona_set":
return a.executePersonaTool(tc)