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

139 lines
5.2 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 (
"strings"
"testing"
agentAPI "github.com/JianFeeeee/HomeAgent/internal/agent/api"
sdk "github.com/JianFeeeee/homeagentsdk/sdk"
)
// appendPlaceholder 复刻 process() 循环顶部的补位逻辑。
func appendPlaceholder(msgs []agentAPI.Message, replyOnly bool) []agentAPI.Message {
msgs = dropContinuationPlaceholders(msgs)
if last := msgs[len(msgs)-1]; last.Role == "assistant" || last.Role == "tool" {
msgs = append(msgs, agentAPI.Message{Role: "user", Content: continuationFor(replyOnly)})
}
return msgs
}
func countPlaceholders(msgs []agentAPI.Message) int {
n := 0
for _, m := range msgs {
if isContinuationPlaceholder(m) {
n++
}
}
return n
}
// 占位是核心插入的传输层附加物,不是用户发言——它不能随轮次线性累积。
//
// 旧实现每轮无条件追加而从不移除,跑 N 轮 prompt 里就叠了 N 条一模一样的
// “继续”,把前缀上下文(含记忆注入)往后挤。
func TestPlaceholderDoesNotAccumulate(t *testing.T) {
msgs := []agentAPI.Message{{Role: "user", Content: "用户请求"}}
const rounds = 20
for turn := 0; turn < rounds; turn++ {
msgs = append(msgs, agentAPI.Message{Role: "assistant", Content: "调用工具"})
msgs = append(msgs, agentAPI.Message{Role: "tool", Content: "结果"})
// 交替普通工具轮 / 纯发送轮,确保两种文案都参与去重。
msgs = appendPlaceholder(msgs, turn%2 == 1)
if n := countPlaceholders(msgs); n != 1 {
t.Fatalf("第 %d 轮后占位数=%d,期望恰好 1 条(旧实现会累积到 %d 条)", turn+1, n, turn+1)
}
}
// 末尾那一轮是纯发送轮,留下的应是“允许收尾”的文案。
if last := msgs[len(msgs)-1]; last.Content != replyDeliveredPlaceholder {
t.Fatalf("最后应是回复已交付的文案,实际: %q", last.Content)
}
}
// 首轮 system/真实用户输入结尾不补位:补了会覆盖实际用户输入。
func TestPlaceholderNotAppendedOnFirstTurn(t *testing.T) {
msgs := []agentAPI.Message{
{Role: "system", Content: "系统说明"},
{Role: "user", Content: "真实用户输入"},
}
got := appendPlaceholder(msgs, false)
if len(got) != 2 {
t.Fatalf("首轮不应补位,得到 %d 条: %+v", len(got), got)
}
if got[1].Content != "真实用户输入" {
t.Fatalf("真实用户输入被覆盖: %q", got[1].Content)
}
}
// 内容相近的真实用户消息不能被当作占位删掉。
func TestDropOnlyExactPlaceholder(t *testing.T) {
msgs := []agentAPI.Message{
{Role: "user", Content: continuationPlaceholder + "补充"},
{Role: "user", Content: replyDeliveredPlaceholder + "补充"},
{Role: "user", Content: continuationPlaceholder},
}
got := dropContinuationPlaceholders(msgs)
if len(got) != 2 {
t.Fatalf("只应删掉精确匹配的那条,得到 %d 条: %+v", len(got), got)
}
}
// 纯输出通道调用之后的补位不能再是「请继续」。
//
// 异步通道的回复只能经 output_send__* 交付,所以模型「已完成回复」的形式就是
// 一个工具调用;紧跟一句「请继续」会被读成「还要再做一步」,而能做的
// 「一步」恰好还是再发一条消息。(生产实测:单轮 34 次发送、514 秒)
func TestContinuationForReplyDoesNotPushToContinue(t *testing.T) {
reply := continuationFor(true)
if reply == continuationPlaceholder {
t.Fatal("回复已交付后不应再补「请继续」,会驱动重复发送")
}
if !strings.Contains(reply, "纯文本") || !strings.Contains(reply, "结束") {
t.Fatalf("应明确告知可返回纯文本收尾,实际: %q", reply)
}
if got := continuationFor(false); got != continuationPlaceholder {
t.Fatalf("普通工具轮补位应保持不变,实际: %q", got)
}
}
// 只有真正的发送动作算「交付回复」;_help 是查询用法。
func TestIsOutputDeliveryTool(t *testing.T) {
cases := map[string]bool{
"output_send__qq": true,
"output_send__webui": true,
"output_send__qq_help": false,
"output_list_channels": false,
"cmd_run": false,
"qq_get_message": false,
}
for name, want := range cases {
if got := isOutputDeliveryTool(name); got != want {
t.Errorf("isOutputDeliveryTool(%q) = %v, want %v", name, got, want)
}
}
}
// 插件在 before_toolcall 里给出的拒绝理由必须原样进入工具结果。
// 若被通用文案覆盖,模型不知道「为什么被拒 / 能不能重试」,会反复重试同一个调用。
func TestDenialReasonReachesModel(t *testing.T) {
reason := "QQ 权限策略拒绝私人资源工具 calendar_list;请不要重试"
ctx := &sdk.StageContext{Response: &reason}
if got := denialResultText(ctx, "calendar_list"); got != reason {
t.Fatalf("拒绝理由被丢弃,实际: %q", got)
}
// 插件没给理由时退回通用文案(保持既有行为)。
if got := denialResultText(&sdk.StageContext{}, "calendar_list"); !strings.Contains(got, "已被插件拒绝") {
t.Fatalf("无理由时应退回通用文案,实际: %q", got)
}
// 空白理由不算理由。
blank := " "
if got := denialResultText(&sdk.StageContext{Response: &blank}, "x"); !strings.Contains(got, "已被插件拒绝") {
t.Fatalf("空白理由应退回通用文案,实际: %q", got)
}
}