mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-15 12:15:16 +00:00
## 背景 仓库早已迁移到 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 保留。
146 lines
5.5 KiB
Go
146 lines
5.5 KiB
Go
package core
|
||
|
||
import (
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
|
||
agentAPI "github.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||
agentIO "github.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||
)
|
||
|
||
// outputTestDevice 是最小的输出通道替身(io 里输出通道就是 Device)。
|
||
type outputTestDevice struct {
|
||
name string
|
||
sent []map[string]interface{}
|
||
}
|
||
|
||
func (d *outputTestDevice) Name() string { return d.name }
|
||
func (d *outputTestDevice) Type() agentIO.DeviceType { return agentIO.DeviceOutput }
|
||
func (d *outputTestDevice) Description() string { return "测试输出通道" }
|
||
func (d *outputTestDevice) Tools() []agentIO.ToolDef { return nil }
|
||
func (d *outputTestDevice) Start() error { return nil }
|
||
func (d *outputTestDevice) Stop() error { return nil }
|
||
func (d *outputTestDevice) OutputCapabilities() agentIO.OutputCapability { return agentIO.CapText }
|
||
func (d *outputTestDevice) ChannelDef() agentIO.ChannelDef { return agentIO.ChannelDef{} }
|
||
func (d *outputTestDevice) Execute(tool string, args map[string]interface{}) (interface{}, error) {
|
||
d.sent = append(d.sent, map[string]interface{}{"tool": tool, "args": args})
|
||
return map[string]interface{}{"status": "sent"}, nil
|
||
}
|
||
|
||
func outputSendTool(name, payload string) agentAPI.ToolCall {
|
||
return agentAPI.ToolCall{
|
||
Name: "output_send__" + name,
|
||
Arguments: map[string]interface{}{"payload": payload, "type": "text"},
|
||
}
|
||
}
|
||
|
||
// 驻留子必须能看见并使用**父**登记的输出通道。
|
||
//
|
||
// 现场缺陷(联调实录):父侧通道装载完整、子侧 childIO 空壳 ——
|
||
// 子调 output_send__X 被 `GetChannelCapabilities` 判 0 ⇒
|
||
// 「通道 [X] 不存在或不可用。可用输出工具列表见 output_list_channels」,
|
||
// 而 output_list_channels 也是空的。根因是子的 io 是新建的、设备表为空,
|
||
// 而输出通道(io 的 Device)由插件登记在父的 io 上。
|
||
func TestResident_InheritsParentOutputChannels(t *testing.T) {
|
||
parent, _, dir := newRootForResidents(t)
|
||
defer parent.Stop()
|
||
|
||
fake := &outputTestDevice{name: "fakeout"}
|
||
other := &outputTestDevice{name: "other"}
|
||
if err := parent.io.RegisterDevice(fake); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := parent.io.RegisterDevice(other); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
if _, err := parent.SpawnResident(ResidentOptions{
|
||
ID: "r-out",
|
||
TaskPrompt: "有情况就发到 fakeout",
|
||
// 白名单只放行一个:验证"继承可见"不等于"绕过授权"
|
||
AllowedOutputs: []string{"fakeout"},
|
||
TempPath: filepath.Join(dir, "residents", "r-out", "graph.db"),
|
||
}); err != nil {
|
||
t.Fatalf("创建驻留子失败: %v", err)
|
||
}
|
||
child := parent.residents["r-out"].agent
|
||
|
||
// ① 看得见:修复前这里是 0(childIO 空壳)
|
||
if caps := child.io.GetChannelCapabilities("fakeout"); caps == 0 {
|
||
t.Fatal("驻留子看不见父的输出通道(childIO 空壳)")
|
||
}
|
||
// ② 发得出去:真走 dev.Execute("output", ...)
|
||
if out := child.executeOutputSendTool(outputSendTool("fakeout", "子发来的消息")); out != "ok" {
|
||
t.Fatalf("子发送应成功,得到 %q", out)
|
||
}
|
||
if len(fake.sent) != 1 {
|
||
t.Fatalf("父通道应收到 1 次输出,得到 %d", len(fake.sent))
|
||
}
|
||
|
||
// ③ 授权闸不被回退绕过:白名单外的通道照样拒绝
|
||
if out := child.executeOutputSendTool(outputSendTool("other", "越权")); !strings.Contains(out, "未授权") {
|
||
t.Fatalf("白名单外的通道应被拒,得到 %q", out)
|
||
}
|
||
if len(other.sent) != 0 {
|
||
t.Fatal("越权输出不应真的送达")
|
||
}
|
||
|
||
// ④ 工具面一致:子应生成 output_send__fakeout(含配套 _help),
|
||
// 而**不生成**白名单外通道的工具 —— 模型看不到就不会去调。
|
||
var names []string
|
||
for _, td := range child.buildToolDefs() {
|
||
entry, _ := td.(map[string]interface{})
|
||
fn, _ := entry["function"].(map[string]interface{})
|
||
if n, _ := fn["name"].(string); strings.HasPrefix(n, "output_send__") {
|
||
names = append(names, n)
|
||
}
|
||
}
|
||
has := func(want string) bool {
|
||
for _, n := range names {
|
||
if n == want {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
if !has("output_send__fakeout") || !has("output_send__fakeout_help") {
|
||
t.Fatalf("子缺少授权通道的输出工具,得到 %v", names)
|
||
}
|
||
for _, n := range names {
|
||
if strings.HasPrefix(n, "output_send__other") {
|
||
t.Fatalf("白名单外的通道不该生成工具,得到 %v", names)
|
||
}
|
||
}
|
||
|
||
// ⑤ 实时性:父之后新登记的通道,子立刻可见(设备随资源生灭)
|
||
late := &outputTestDevice{name: "late"}
|
||
if err := parent.io.RegisterDevice(late); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if caps := child.io.GetChannelCapabilities("late"); caps == 0 {
|
||
t.Fatal("父新登记的通道未实时反映到子(说明是快照而非回退)")
|
||
}
|
||
}
|
||
|
||
// 默认授权(AllowedOutputs 空)= 完整授权:子用父的全部输出通道。
|
||
func TestResident_DefaultOutputsAreFull(t *testing.T) {
|
||
parent, _, dir := newRootForResidents(t)
|
||
defer parent.Stop()
|
||
|
||
dev := &outputTestDevice{name: "anywhere"}
|
||
if err := parent.io.RegisterDevice(dev); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err := parent.SpawnResident(ResidentOptions{
|
||
ID: "r-full", TaskPrompt: "待命",
|
||
TempPath: filepath.Join(dir, "residents", "r-full", "graph.db"),
|
||
}); err != nil {
|
||
t.Fatalf("创建驻留子失败: %v", err)
|
||
}
|
||
child := parent.residents["r-full"].agent
|
||
if out := child.executeOutputSendTool(outputSendTool("anywhere", "默认授权")); out != "ok" {
|
||
t.Fatalf("默认应完整授权,得到 %q", out)
|
||
}
|
||
}
|