Files
HomeAgent/internal/agent/core/recallpolicy_test.go
JianFeeeee 6d38a164eb chore(module): Go module 路径随托管地迁移 gitcode → github
## 背景

仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑),
gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。
但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源
不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。

## 关键区分(这是本次改动安全的前提)

两个 module 是**不同**的,影响面差一个数量级:

| module | 谁依赖 | 改它的后果 |
|---|---|---|
| `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 |
| `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 |

★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0),
只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串**
(`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。
⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。

SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配,
否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。

## 改动

- `go.mod` / `go.sum`:两个 module 路径 + require/replace
- 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径
- SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板
- 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等)

## 明确**保留** gitcode 的地方(镜像机制,不能动)

- `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job)
- `gitcode.com/api/v5/...`(release 附件上传)
- git remote `gitcode`(镜像拉取/推送)
- `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维)

## 验证

- `go build ./...` / `go vet ./...` 绿
- `go test ./... -count=1` → 48 包 ok / 0 FAIL
- hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`)
- 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0
- 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好

## 顺带修正

`hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」——
module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**:
离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
2026-10-08 10:40:17 +08:00

142 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package core
import (
"path/filepath"
"strings"
"testing"
agentAPI "github.com/JianFeeeee/HomeAgent/internal/agent/api"
agentIO "github.com/JianFeeeee/HomeAgent/internal/agent/io"
"github.com/JianFeeeee/HomeAgent/internal/memory"
pubsdk "github.com/JianFeeeee/homeagentsdk/sdk"
)
// 这一组测试锁死「默认召回、可显式关闭」这条语义。
//
// 与 prune 刻意相反:裁剪是破坏性的、默认关;召回是只读增量、默认开。
// 两者正交,一根 ContextPolicy 表达不了 2×2 的组合(只召回不裁剪 / 只裁不召回)。
func TestRecallDeclared_DefaultsToRecall(t *testing.T) {
m := agentIO.NewIOManager()
a := &Agent{io: m}
// 没有任何声明 → 默认召回(保持既有"每条输入都召回"的行为)。
if !a.recallDeclared(&agentIO.InputEvent{Source: "unknown", Payload: map[string]interface{}{}}) {
t.Fatal("未声明的输入默认必须召回")
}
// 通道注册了但没设 RecallPolicy → 仍默认召回。
m.RegisterInputChannel("plain", pubsdk.ChannelDef{})
if !a.recallDeclared(&agentIO.InputEvent{Source: "plain", Payload: map[string]interface{}{}}) {
t.Fatal("ChannelDef 未设 RecallPolicy 应默认召回")
}
// nil 事件不能 panic,且按默认召回。
if !a.recallDeclared(nil) {
t.Fatal("nil 事件应默认召回")
}
}
func TestRecallDeclared_ChannelOptOut(t *testing.T) {
m := agentIO.NewIOManager()
m.RegisterInputChannel("meta", pubsdk.ChannelDef{RecallPolicy: pubsdk.RecallPolicyNone})
m.RegisterInputChannel("talk", pubsdk.ChannelDef{RecallPolicy: pubsdk.RecallPolicyAuto})
a := &Agent{io: m}
if a.recallDeclared(&agentIO.InputEvent{Source: "meta", Payload: map[string]interface{}{}}) {
t.Fatal("通道声明 none 不应召回")
}
if !a.recallDeclared(&agentIO.InputEvent{Source: "talk", Payload: map[string]interface{}{}}) {
t.Fatal("通道声明 auto 应召回")
}
}
// 注入点声明优先于通道定义:同一通道下的不同注入可以有不同意图。
func TestRecallDeclared_InjectionOverridesChannel(t *testing.T) {
m := agentIO.NewIOManager()
a := &Agent{io: m}
m.RegisterInputChannel("qq", pubsdk.ChannelDef{RecallPolicy: pubsdk.RecallPolicyNone})
evt := &agentIO.InputEvent{Source: "qq", Payload: map[string]interface{}{
"recall_policy": pubsdk.RecallPolicyAuto,
}}
if !a.recallDeclared(evt) {
t.Fatal("注入点声明 auto 应覆盖通道的 none")
}
m.RegisterInputChannel("plain", pubsdk.ChannelDef{RecallPolicy: pubsdk.RecallPolicyAuto})
evt = &agentIO.InputEvent{Source: "plain", Payload: map[string]interface{}{
"recall_policy": pubsdk.RecallPolicyNone,
}}
if a.recallDeclared(evt) {
t.Fatal("注入点声明 none 应覆盖通道的 auto")
}
}
// buildTaskMemoryContext 在声明 none 时必须返回空串(不注入记忆索引)。
func TestBuildTaskMemoryContext_RespectsPolicy(t *testing.T) {
m := agentIO.NewIOManager()
m.RegisterInputChannel("meta", pubsdk.ChannelDef{RecallPolicy: pubsdk.RecallPolicyNone})
a := &Agent{io: m, indexer: newTestIndexer(t, "咖啡", "张三")}
f := &TaskFrame{Evt: &agentIO.InputEvent{Source: "meta", Payload: map[string]interface{}{}}}
if got := a.buildTaskMemoryContext(f, "咖啡", 0); got != "" {
t.Fatalf("声明 none 时不应注入记忆,实际 %q", got)
}
f2 := &TaskFrame{Evt: &agentIO.InputEvent{Source: "plain", Payload: map[string]interface{}{}}}
if got := a.buildTaskMemoryContext(f2, "咖啡", 0); !strings.Contains(got, "【记忆索引】") {
t.Fatalf("默认应注入记忆索引,实际 %q", got)
}
}
// 工具触发的召回:以(清洗后的)工具输出为 query,产出可注入的记忆文本。
func TestRecallTextFor_UsesQuery(t *testing.T) {
a := &Agent{indexer: newTestIndexer(t, "咖啡", "张三")}
got := a.recallTextFor("咖啡", "tool:test", nil)
if !strings.Contains(got, "【记忆索引】") {
t.Fatalf("应产出记忆索引文本,实际 %q", got)
}
// 空 query 或无 indexer 时不产出、不 panic。
if got := a.recallTextFor("", "tool:test", nil); got != "" {
t.Fatalf("空 query 应返回空串,实际 %q", got)
}
if got := (&Agent{}).recallTextFor("咖啡", "tool:test", nil); got != "" {
t.Fatalf("无 indexer 应返回空串,实际 %q", got)
}
}
// 召回文本以 system 消息挂在末尾;同一任务内多次触发是**替换**而非累加。
func TestAppendOrReplaceRecall(t *testing.T) {
msgs := []agentAPI.Message{{Role: "user", Content: "hi"}}
msgs = appendOrReplaceRecall(msgs, "第一段")
if len(msgs) != 2 || msgs[1].Role != "system" || !strings.Contains(msgs[1].Content, "第一段") {
t.Fatalf("首次应追加一条 system 召回消息,实际 %+v", msgs)
}
msgs = appendOrReplaceRecall(msgs, "第二段")
if len(msgs) != 2 {
t.Fatalf("再次触发应替换而非累加,实际 %d 条", len(msgs))
}
if !strings.Contains(msgs[1].Content, "第二段") || strings.Contains(msgs[1].Content, "第一段") {
t.Fatalf("替换后应只含最新召回,实际 %q", msgs[1].Content)
}
if msgs = appendOrReplaceRecall(msgs, ""); len(msgs) != 2 {
t.Fatalf("空召回不应改变消息,实际 %d 条", len(msgs))
}
}
// newTestIndexer 造一个只含给定实体的图记忆 + 已同步的索引器。
func newTestIndexer(t *testing.T, subject, object string) *memory.Indexer {
t.Helper()
db, err := memory.NewGraphDB(filepath.Join(t.TempDir(), "graph.db"))
if err != nil {
t.Fatalf("NewGraphDB: %v", err)
}
t.Cleanup(func() { db.Close() })
if _, _, err := db.Commit([]memory.Triple{{Subject: subject, Relation: "喜欢", Object: object}}, "s", 0); err != nil {
t.Fatalf("Commit: %v", err)
}
idx := memory.NewIndexer(db)
if err := idx.Sync(); err != nil {
t.Fatalf("Sync: %v", err)
}
return idx
}