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 保留。
172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
//go:build linux || darwin
|
||
|
||
package plugin
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/JianFeeeee/HomeAgent/internal/events"
|
||
"github.com/JianFeeeee/HomeAgent/internal/plugin/proc"
|
||
pubsdk "github.com/JianFeeeee/homeagentsdk/sdk"
|
||
)
|
||
|
||
// 事件环基础测试:Host 创建事件环 → EventRing 写入 → 消费者读到。
|
||
func TestEventRing_BasicWriteAndConsume(t *testing.T) {
|
||
host, err := proc.NewHost()
|
||
if err != nil {
|
||
t.Fatalf("NewHost: %v", err)
|
||
}
|
||
defer host.Close()
|
||
|
||
bus := events.NewBus()
|
||
er := NewEventRing(host.EvtRing(), int(host.Evtfd().Fd()), bus)
|
||
|
||
// 消费者:从事件环段读取事件
|
||
received := make(chan *pubsdk.Event, 10)
|
||
consumer := proc.NewEvtConsumer(
|
||
host.EvtData(),
|
||
host.EvtfdReadFile(),
|
||
0, // typeMask = 0:接收全部事件
|
||
func(evt *pubsdk.Event) error {
|
||
received <- evt
|
||
return nil
|
||
},
|
||
)
|
||
go consumer.Run()
|
||
// LIFO:先 Stop(打断阻塞的 Read)再 Wait(等 Run 退出),
|
||
// 两者都必须在 host.Close(munmap 整个区域)之前完成。
|
||
defer consumer.Wait()
|
||
defer consumer.Stop()
|
||
|
||
// 订阅 agent_output 事件
|
||
unsub := er.Subscribe(pubsdk.EventAgentOutput)
|
||
defer unsub()
|
||
|
||
// 发布事件
|
||
bus.Publish(&events.Event{
|
||
Type: events.EventAgentOutput,
|
||
Payload: map[string]interface{}{"text": "hello"},
|
||
})
|
||
|
||
// 等待消费者读到
|
||
select {
|
||
case evt := <-received:
|
||
if evt.Type != pubsdk.EventType(events.EventAgentOutput) {
|
||
t.Errorf("事件类型 = %v,期望 %v", evt.Type, events.EventAgentOutput)
|
||
}
|
||
case <-time.After(2 * time.Second):
|
||
t.Error("消费者在 2s 内未收到事件")
|
||
}
|
||
}
|
||
|
||
// 事件环溢出测试:写入超过 cap 时消费者仍能读到最新事件。
|
||
func TestEventRing_OverflowStillDelivers(t *testing.T) {
|
||
host, err := proc.NewHost()
|
||
if err != nil {
|
||
t.Fatalf("NewHost: %v", err)
|
||
}
|
||
defer host.Close()
|
||
|
||
bus := events.NewBus()
|
||
er := NewEventRing(host.EvtRing(), int(host.Evtfd().Fd()), bus)
|
||
|
||
// 不启动消费者,直接写入超过 cap 的事件(需先订阅,否则 Bus 不会触发事件环写入)
|
||
unsub := er.Subscribe(pubsdk.EventSystem)
|
||
defer unsub()
|
||
for i := uint32(0); i < 8192+100; i++ {
|
||
bus.Publish(&events.Event{
|
||
Type: events.EventSystem,
|
||
Payload: map[string]interface{}{"seq": i},
|
||
})
|
||
}
|
||
|
||
// 启动消费者,应能读到最新事件
|
||
received := make(chan *pubsdk.Event, 10)
|
||
consumer := proc.NewEvtConsumer(
|
||
host.EvtData(),
|
||
host.EvtfdReadFile(),
|
||
0,
|
||
func(evt *pubsdk.Event) error {
|
||
// 非阻塞投递:本用例写入了 8292 条事件,若这里阻塞在
|
||
// channel 上,drainEvents 会卡在 handler 里,Stop 就无法
|
||
// 让 Run 退出。
|
||
select {
|
||
case received <- evt:
|
||
default:
|
||
}
|
||
return nil
|
||
},
|
||
)
|
||
go consumer.Run()
|
||
defer consumer.Wait()
|
||
defer consumer.Stop()
|
||
|
||
select {
|
||
case evt := <-received:
|
||
if evt == nil {
|
||
t.Error("收到 nil 事件")
|
||
}
|
||
case <-time.After(2 * time.Second):
|
||
t.Error("溢出后消费者在 2s 内未收到事件")
|
||
}
|
||
}
|
||
|
||
// typeMask 过滤测试:订阅者只收到匹配类型的事件。
|
||
func TestEventRing_TypeMaskFiltering(t *testing.T) {
|
||
host, err := proc.NewHost()
|
||
if err != nil {
|
||
t.Fatalf("NewHost: %v", err)
|
||
}
|
||
defer host.Close()
|
||
|
||
bus := events.NewBus()
|
||
er := NewEventRing(host.EvtRing(), int(host.Evtfd().Fd()), bus)
|
||
|
||
received := make(chan *pubsdk.Event, 10)
|
||
// typeMask 只订阅 tool_call(bit 3 = 8)
|
||
consumer := proc.NewEvtConsumer(
|
||
host.EvtData(),
|
||
host.EvtfdReadFile(),
|
||
1<<3, // tool_call
|
||
func(evt *pubsdk.Event) error {
|
||
received <- evt
|
||
return nil
|
||
},
|
||
)
|
||
go consumer.Run()
|
||
defer consumer.Wait()
|
||
defer consumer.Stop()
|
||
|
||
unsub := er.Subscribe(pubsdk.EventToolCall)
|
||
defer unsub()
|
||
|
||
// 发一个 tool_call 和一个 agent_output
|
||
bus.Publish(&events.Event{
|
||
Type: events.EventToolCall,
|
||
Payload: map[string]interface{}{"tool": "test"},
|
||
})
|
||
bus.Publish(&events.Event{
|
||
Type: events.EventAgentOutput,
|
||
Payload: map[string]interface{}{"text": "should be filtered"},
|
||
})
|
||
|
||
// 只应收到 tool_call
|
||
select {
|
||
case evt := <-received:
|
||
if evt.Type != pubsdk.EventType(events.EventToolCall) {
|
||
t.Errorf("收到错误类型 %v,期望 tool_call", evt.Type)
|
||
}
|
||
case <-time.After(2 * time.Second):
|
||
t.Error("消费者在 2s 内未收到 tool_call 事件")
|
||
}
|
||
|
||
// agent_output 不应到达
|
||
select {
|
||
case evt := <-received:
|
||
t.Errorf("不应收到 agent_output,实际收到 %v", evt)
|
||
case <-time.After(200 * time.Millisecond):
|
||
// 正确:agent_output 被过滤
|
||
}
|
||
}
|