Files
HomeAgent/internal/agent/core/agent_functions_test.go
JianFeeeee e273924511 fix(llm): 参数无法解析时给出真因,不再静默丢弃整条调用
★ 上次修复误判了成因。真实根因(本次运行日志 34/34 同形):
    {"command": "…完好的长命令…", "timeout": 20s}
  command 一字节没错,只是 timeout 值少了引号 —— cmd_run 的 schema 把 timeout
  声明成 string、示例写着 "10s, 1m, 30s",模型照抄格式却忘了引号。
  finish_reason=length 出现 0 次 ⇒ 上次那条"截断"分支从不生效。

旧行为把**整个参数**丢掉,模型只看到 "command is required",看不出坏在 timeout,
只能原样重试。实测本次运行 cmd_run 失败率 35%(34 败 / 71 成),
12 分钟的任务里更是 48% 时间耗在这上面 —— 每次失败都付一次完整 LLM 往返。

三处改动:
1. repairToolArgsJSON:解析失败时先试窄修复 —— 只给"值位置上未加引号的带单位
   数字"补引号,且修完必须真能解析成功才接受。不碰合法 JSON、不动正文里的 20s、
   不会把真截断"修好"。
2. 修复仍失败时不再静默降级成空 map,改为带 __arg_error 交给模型,并按成因
   分流文案:截断→拆小参数;JSON 写坏→提醒带单位的值要加引号。
3. 统一键名 __arg_error(原 __truncated_error 只覆盖截断,语义过窄)。

同一缺陷面不止 cmd:agentcli/healthcheck/timer 都有 string 类型却以
"5m, 1h" 作示例的参数,此修复一并覆盖。

回归测试:真实日志样本修复、保守性(不碰合法/正文/截断)、
端到端(修复后 timeout 仍能被 time.ParseDuration 接受)。
2026-09-19 16:49:16 +08:00

100 lines
2.3 KiB
Go

package core
import (
"testing"
"gitcode.com/JianFeeeee/HomeAgent/internal/memory/document"
)
func TestEntitySimilarity(t *testing.T) {
tests := []struct {
a, b string
want float64
}{
{"", "", 0}, // empty → 0
{"a", "b", 0}, // single char → 0
{"张三", "张三", 1.0}, // identical → 1.0
{"张三", "李四", 0}, // no common bigrams
{"iPhone", "iPhone 15", 0.625}, // partial overlap
}
for _, tt := range tests {
got := entitySimilarity(tt.a, tt.b)
if got != tt.want {
t.Errorf("entitySimilarity(%q, %q) = %.3f, want %.3f", tt.a, tt.b, got, tt.want)
}
}
}
func TestDocToTriples(t *testing.T) {
doc := &document.Doc{
Summary: "用户喜欢编程",
Content: "用户提到喜欢Go和Python",
Source: "context",
}
triples := docToTriples(doc, nil)
foundSummary := false
foundSource := false
for _, tr := range triples {
switch {
case tr.Subject == "文档" && tr.Relation == "主题":
foundSummary = true
case tr.Subject == "文档" && tr.Relation == "来源":
foundSource = true
}
}
if !foundSummary {
t.Error("missing '主题' triple")
}
if !foundSource {
t.Error("missing '来源' triple")
}
}
func TestDocToTriplesNil(t *testing.T) {
triples := docToTriples(nil, nil)
if len(triples) != 0 {
t.Errorf("expected empty for nil doc, got %d", len(triples))
}
}
func TestDocToTriplesNoSource(t *testing.T) {
doc := &document.Doc{
Summary: "无来源文档",
Content: "content",
}
triples := docToTriples(doc, nil)
for _, tr := range triples {
if tr.Relation == "来源" {
t.Error("should not have source triple when Source is empty")
}
}
}
func TestDocToTriplesTypes(t *testing.T) {
doc := &document.Doc{
Summary: "测试三元组类型",
Content: "用于验证 SubjectType 和 ObjectType",
Source: "test",
}
triples := docToTriples(doc, nil)
for _, tr := range triples {
if tr.Subject == "文档" {
if tr.SubjectType != "Concept" {
t.Errorf("文档 subject_type should be Concept, got %q", tr.SubjectType)
}
if tr.Confidence != 1.0 {
t.Errorf("文档 triple confidence should be 1.0, got %f", tr.Confidence)
}
}
// all should have SubjectType/ObjectType set
if tr.SubjectType == "" || tr.ObjectType == "" {
t.Errorf("triple %+v missing SubjectType or ObjectType", tr)
}
}
}