Files
HomeAgent/internal/agent/core/prunepolicy_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

120 lines
4.7 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 (
"testing"
agentIO "github.com/JianFeeeee/HomeAgent/internal/agent/io"
pubsdk "github.com/JianFeeeee/homeagentsdk/sdk"
)
// 这一组测试锁死「默认不裁剪」这条语义。
//
// 改动前:每条非中断输入都无条件 Prune 一次,没有任何声明能关掉它。
// 这是破坏性行为(低相关事件被归档并从上下文移走),却无法从调用点看出
// 「谁触发的裁剪」。改成需声明后,必须逐条验证默认值确实是不裁剪。
func TestPruneDeclared_DefaultsToNoPrune(t *testing.T) {
m := agentIO.NewIOManager()
a := &Agent{io: m}
evt := &agentIO.InputEvent{Source: "unknown_source", Payload: map[string]interface{}{}}
if a.pruneDeclared(evt) {
t.Fatal("既没有通道声明也没有注入声明的输入,默认必须不裁剪")
}
// 通道注册了、但策略是 none / 空:仍然不裁剪。
m.RegisterInputChannel("quiet", pubsdk.ChannelDef{ContextPolicy: pubsdk.ContextPolicyNone})
if a.pruneDeclared(&agentIO.InputEvent{Source: "quiet", Payload: map[string]interface{}{}}) {
t.Fatal("ChannelDef.ContextPolicy=none 不应裁剪")
}
m.RegisterInputChannel("empty", pubsdk.ChannelDef{})
if a.pruneDeclared(&agentIO.InputEvent{Source: "empty", Payload: map[string]interface{}{}}) {
t.Fatal("ChannelDef 未设 ContextPolicy 不应裁剪")
}
}
// 通道显式声明 prune 才裁剪。
func TestPruneDeclared_ChannelOptIn(t *testing.T) {
m := agentIO.NewIOManager()
m.RegisterInputChannel("noisy", pubsdk.ChannelDef{ContextPolicy: pubsdk.ContextPolicyPrune})
a := &Agent{io: m}
if !a.pruneDeclared(&agentIO.InputEvent{Source: "noisy", Payload: map[string]interface{}{}}) {
t.Fatal("通道声明 prune 后应裁剪")
}
}
// 注入点声明的优先级高于通道定义:同一通道下的不同注入可以有不同意图。
func TestPruneDeclared_InjectionOverridesChannel(t *testing.T) {
m := agentIO.NewIOManager()
a := &Agent{io: m}
m.RegisterInputChannel("chan", pubsdk.ChannelDef{ContextPolicy: pubsdk.ContextPolicyPrune})
// 注入点说 none → 即使通道说 prune 也不裁。
evt := &agentIO.InputEvent{Source: "chan", Payload: map[string]interface{}{
"context_policy": pubsdk.ContextPolicyNone,
}}
if a.pruneDeclared(evt) {
t.Fatal("注入点声明 none 应覆盖通道的 prune")
}
// 通道没说,注入点说 prune → 裁。
m.RegisterInputChannel("plain", pubsdk.ChannelDef{})
evt = &agentIO.InputEvent{Source: "plain", Payload: map[string]interface{}{
"context_policy": pubsdk.ContextPolicyPrune,
}}
if !a.pruneDeclared(evt) {
t.Fatal("注入点声明 prune 应生效")
}
}
// 没有 context 时不能 panic,也不该裁剪。
func TestPruneOnInput_NilContextIsSafe(t *testing.T) {
m := agentIO.NewIOManager()
m.RegisterInputChannel("noisy", pubsdk.ChannelDef{ContextPolicy: pubsdk.ContextPolicyPrune})
a := &Agent{io: m}
if got := a.pruneOnInput(&agentIO.InputEvent{Source: "noisy", Payload: map[string]interface{}{}}, "x"); got != 0 {
t.Fatalf("nil context 应返回 0,实际 %d", got)
}
}
// cleanInputFor 的优先级:注入点声明的 cleaner > 按 source 查的 cleaner > 原文。
func TestCleanInputFor_Priority(t *testing.T) {
m := agentIO.NewIOManager()
m.RegisterInputChannel("src", pubsdk.ChannelDef{
Cleaner: func(s string) string { return "by-source:" + s },
})
m.RegisterInputChannel("explicit", pubsdk.ChannelDef{
Cleaner: func(s string) string { return "by-name:" + s },
})
a := &Agent{io: m}
// 无声明 → 用 source 的 cleaner
evt := &agentIO.InputEvent{Source: "src", Payload: map[string]interface{}{}}
if got := a.cleanInputFor(evt, "raw"); got != "by-source:raw" {
t.Fatalf("应回退到 source 的 cleaner,实际 %q", got)
}
// 注入点指定 cleaner_name → 覆盖 source 的
evt = &agentIO.InputEvent{Source: "src", Payload: map[string]interface{}{"cleaner_name": "explicit"}}
if got := a.cleanInputFor(evt, "raw"); got != "by-name:raw" {
t.Fatalf("注入点声明的 cleaner 应优先,实际 %q", got)
}
// 完全没有 cleaner → 原文
evt = &agentIO.InputEvent{Source: "nobody", Payload: map[string]interface{}{}}
if got := a.cleanInputFor(evt, "raw"); got != "raw" {
t.Fatalf("没有 cleaner 时应返回原文,实际 %q", got)
}
// 声明的名字查不到 → 回退到 source 的 cleaner(并记日志),不能 panic、不能丢内容
evt = &agentIO.InputEvent{Source: "src", Payload: map[string]interface{}{"cleaner_name": "missing"}}
if got := a.cleanInputFor(evt, "raw"); got != "by-source:raw" {
t.Fatalf("未知 cleaner_name 应回退,实际 %q", got)
}
// nil IOManager 不能 panic
if got := (&Agent{}).cleanInputFor(evt, "raw"); got != "raw" {
t.Fatalf("nil io 应返回原文,实际 %q", got)
}
}