mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-03 15:53:56 +00:00
feat(toolcall): 工具结果只统计不裁剪(方案 B),并治掉 seq 侧的静默截断
问题(核实过):工具结果进 f.Msgs 时**没有任何长度上限**(task.go 直接 `Content: result`),内核也**不预检**是否超长 —— 超限由上游 API 报错。 时间线那侧有预算(ContextTokens = 0.8×窗口,进消息前就裁过),但那只管 a.context 的历史事件,**不管单条工具结果** ⇒ 一条巨大结果可能直接冲破 预算而内核不会提前发现。 为什么**不裁剪**(与方案 A 的取舍): · 截断会让模型拿到**残缺**信息,而截断位置由内核武断决定; · 模型无法得知"这里被截断了",会基于残缺数据下结论 —— 与本仓反复 吃亏的「静默降级」同族(`20s` 少引号 → 静默降级 → cmd_run 失败率 34%); · 处置权应交给调度器/上层(告警、拒绝、或让模型自己换更窄的查询), 而不是内核单方面替模型决定。 改动: · core/toolresult_budget.go: checkToolResultSize 只**计数+报告**; 阈值默认 = ContextTokens/8(一条吃掉全部预算会把其它上下文全挤掉); 报告经 toolResultReporter(可替换),默认 logReporter —— **不给模型发 消息**:那是在已花掉的 token 之上再加一条 system,且对当前这轮决策无帮助。 · 接入点在 stepToolAfter 的 toolMsg 落定**之后**(那里才是模型最终看到的 内容;stepToolExec 拿到的尚未经 after_toolcall 改写)。 · TaskFrame 记 oversizeTools / oversizeToolNames,供调度器与状态面查询 "是否有工具在稳定产出超大结果"。 ★ 顺带治掉 seq 侧一处**我自己留下的静默截断**: handlers.go 里我当初随手写了 truncate(…, 160),把变量槽静默截到 160 字 且**无任何标注** —— 正是我批评过的静默降级。 改为 renderSlot:≤160 给全;超过则显式标注「已截断:共 N 字,此处显示前 160 字」并给出改法。**槽里存的始终是完整值**,截断只影响回填文本长度。 端到端判据 TestSeqRunDoesNotSilentlyTruncateSlot 抓到了这个缺陷 ("变量槽被截到 160/5000 字却没有任何标注")。 判据(toolresult_budget_test.go,4 条): · 400KB 结果触发超限报告(含工具名与 token 数) · ★ **默认不裁剪**:200KB 结果原样进 tool 消息(方案 B 的核心不变式) · 小结果不误报(噪音会淹没有效信号) · 报告文案可执行:带工具名、token 数、改法建议 变异验证:去掉统计调用 ⇒ 两条判据 FAIL("统计没生效" + "被裁剪了")。 另:检查项报 stepToolBatch 的 goroutine 竞态,-race 实测**误报**—— 循环变量显式传参(非闭包捕获)、且按索引写各自槽位(非共享 map), `-race` 下 20 轮并发判据全绿。
This commit is contained in:
@ -289,3 +289,51 @@ func TestE2E_DeleteMissingErrors(t *testing.T) {
|
||||
t.Fatal("删除不存在的序列却返回成功")
|
||||
}
|
||||
}
|
||||
|
||||
// ⑧ ★ 变量槽的值不得**静默**截断(方案 B 的要求在 seq 侧同样适用)。
|
||||
//
|
||||
// 背景:seq_run / seq_call 回填变量槽时,我当初随手写了 truncate(…, 160)。
|
||||
// 那正是本仓反复吃亏的「静默降级」——模型拿到 160 字的残缺值,
|
||||
// **不知道**后面还有内容,会基于残缺数据下结论。
|
||||
//
|
||||
// 正确做法:要么给全,要么**显式标注**被截断(并说明有多少)。
|
||||
func TestSeqRunDoesNotSilentlyTruncateSlot(t *testing.T) {
|
||||
long := strings.Repeat("L", 5000) // 远超 160
|
||||
r := newE2ERunner("uptime")
|
||||
r.results["uptime"] = long
|
||||
p := newE2EPlugin(t, r)
|
||||
|
||||
if _, err := p.dispatch("seq_create", map[string]interface{}{
|
||||
"name": "trunc",
|
||||
"groups": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "g1",
|
||||
"in": map[string]string{},
|
||||
"out": map[string]string{"summary": "string"},
|
||||
"tools": `{"tool":"uptime","args":{},"as":"summary"} ;`,
|
||||
},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("seq_create: %v", err)
|
||||
}
|
||||
|
||||
out, err := p.dispatch("seq_run", map[string]interface{}{"name": "trunc"})
|
||||
if err != nil {
|
||||
t.Fatalf("seq_run: %v", err)
|
||||
}
|
||||
res, _ := out.(string)
|
||||
if !strings.Contains(res, "summary") {
|
||||
t.Fatalf("未回填 summary 槽: %s", res)
|
||||
}
|
||||
// 若确实截断,必须**显式标注**并说明被截了多少
|
||||
if strings.Count(res, "L") < 160 {
|
||||
t.Fatalf("summary 槽几乎为空: %s", res)
|
||||
}
|
||||
if strings.Count(res, "L") < len(long) {
|
||||
// 发生了截断 —— 那必须看得见
|
||||
if !strings.Contains(res, "已截断") && !strings.Contains(res, "省略") {
|
||||
t.Errorf("变量槽被截到 %d/%d 字却**没有任何标注** —— 模型会基于残缺值下结论",
|
||||
strings.Count(res, "L"), len(long))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user