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 保留。
139 lines
4.5 KiB
Go
139 lines
4.5 KiB
Go
package core
|
||
|
||
// N1b:输出通道授权集合 + 输出通道 → 目标 agent 的 inputch 解析。
|
||
//
|
||
// 设计依据 docs/zh/resident-subagent-design.md §4.4(通道分配:不对称)与 R2
|
||
// (插件与工具由父授权,**默认完整授权**)。
|
||
//
|
||
// 三个过滤点必须一致,否则会出现"列表里看不到、但按名字还能调"的裂缝:
|
||
// ① 工具表(不为未授权的通道生成 output_send__X)
|
||
// ② 列表工具(output_list_channels 只列授权的)
|
||
// ③ 调用点(凭名字直调也必须被拒 —— 纵深防御)
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
|
||
agentAPI "github.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||
agentIO "github.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||
)
|
||
|
||
// registerFakeOutput 注册一个假的输出通道(device 通道)。
|
||
func registerFakeOutput(t *testing.T, a *Agent, name string) {
|
||
t.Helper()
|
||
if err := a.io.RegisterDevice(&mockOutputDevice{name: name, caps: agentIO.CapText}); err != nil {
|
||
t.Fatalf("注册测试通道 %s 失败: %v", name, err)
|
||
}
|
||
}
|
||
|
||
func toolNames(a *Agent) []string {
|
||
var names []string
|
||
for _, t := range a.buildToolDefs() {
|
||
m, ok := t.(map[string]interface{})
|
||
if !ok {
|
||
continue
|
||
}
|
||
fn, _ := m["function"].(map[string]interface{})
|
||
if n, _ := fn["name"].(string); n != "" {
|
||
names = append(names, n)
|
||
}
|
||
}
|
||
return names
|
||
}
|
||
|
||
func hasTool(names []string, want string) bool {
|
||
for _, n := range names {
|
||
if n == want {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// 默认(未配置白名单)= 完整授权:所有输出通道都能用。
|
||
func TestOutputGrant_DefaultIsFull(t *testing.T) {
|
||
a := newPreemptAgent(t, newPreemptProvider())
|
||
registerFakeOutput(t, a, "qq")
|
||
registerFakeOutput(t, a, "webui")
|
||
|
||
if !a.IsOutputAllowed("qq") || !a.IsOutputAllowed("webui") {
|
||
t.Fatal("默认应为完整授权")
|
||
}
|
||
names := toolNames(a)
|
||
if !hasTool(names, "output_send__qq") || !hasTool(names, "output_send__webui") {
|
||
t.Fatalf("默认完整授权下应生成全部输出门,实际 %v", names)
|
||
}
|
||
if out := a.executeOutputListChannels(); !strings.Contains(out, "qq") || !strings.Contains(out, "webui") {
|
||
t.Fatalf("默认完整授权下列表应含全部通道:\n%s", out)
|
||
}
|
||
}
|
||
|
||
// 白名单收窄:三个过滤点必须一致。
|
||
func TestOutputGrant_NarrowedWhitelist(t *testing.T) {
|
||
a := newPreemptAgent(t, newPreemptProvider())
|
||
a.allowedOutputs = []string{"webui"} // 模拟父创建子时收窄
|
||
registerFakeOutput(t, a, "qq")
|
||
registerFakeOutput(t, a, "webui")
|
||
|
||
if a.IsOutputAllowed("qq") {
|
||
t.Fatal("白名单外的通道不应被授权")
|
||
}
|
||
if !a.IsOutputAllowed("webui") {
|
||
t.Fatal("白名单内的通道应被授权")
|
||
}
|
||
|
||
// ① 工具表
|
||
names := toolNames(a)
|
||
if hasTool(names, "output_send__qq") {
|
||
t.Fatalf("未授权的通道不该生成输出门工具:%v", names)
|
||
}
|
||
if !hasTool(names, "output_send__webui") {
|
||
t.Fatalf("已授权的通道应生成输出门工具:%v", names)
|
||
}
|
||
|
||
// ② 列表工具
|
||
out := a.executeOutputListChannels()
|
||
if strings.Contains(out, "qq") {
|
||
t.Fatalf("列表不应含未授权通道:\n%s", out)
|
||
}
|
||
if !strings.Contains(out, "webui") {
|
||
t.Fatalf("列表应含已授权通道:\n%s", out)
|
||
}
|
||
|
||
// ③ 调用点(凭名字直调)
|
||
got := a.executeOutputSendTool(agentAPI.ToolCall{
|
||
ID: "c1", Name: "output_send__qq",
|
||
Arguments: map[string]interface{}{"payload": "hi", "type": "text"},
|
||
})
|
||
if !strings.Contains(got, "未授权") {
|
||
t.Fatalf("未授权的输出门必须被拒,实际 %q", got)
|
||
}
|
||
}
|
||
|
||
// 输出通道 → 目标 agent 的 inputch 的解析("输出可寻址到具体 agent")。
|
||
func TestOutputTarget_Resolution(t *testing.T) {
|
||
a := newPreemptAgent(t, newPreemptProvider())
|
||
reg := a.io.ChannelRegistry()
|
||
|
||
if err := reg.BindOutputTarget("to-child-1", "child-1", "sub/in"); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
tgt, ok := a.ResolveOutputTarget("to-child-1")
|
||
if !ok || tgt.AgentID != "child-1" || tgt.InputCh != "sub/in" {
|
||
t.Fatalf("解析结果=%+v ok=%v", tgt, ok)
|
||
}
|
||
// 未登记的输出通道由传输层处理(如 qq/webui 这类 device 通道)。
|
||
if _, ok := a.ResolveOutputTarget("qq"); ok {
|
||
t.Fatal("未登记目标解析的输出通道不应解析出 agent")
|
||
}
|
||
if err := reg.BindOutputTarget("", "x", "y"); err == nil {
|
||
t.Fatal("空输出通道名应报错")
|
||
}
|
||
|
||
// 列表工具在已登记时带出目标,便于模型知道"这条通道发给谁"。
|
||
registerFakeOutput(t, a, "to-child-1")
|
||
if out := a.executeOutputListChannels(); !strings.Contains(out, "child-1") {
|
||
t.Fatalf("已登记目标的输出通道应在列表里标出目标:\n%s", out)
|
||
}
|
||
}
|