Files
HomeAgent/internal/plugin/proc_load_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

193 lines
6.0 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.

//go:build linux || darwin
package plugin
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/JianFeeeee/HomeAgent/internal/plugin/proc"
isdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
)
// 子进程插件经 Registry 加载的接线测试(Part 3 收尾)。
//
// 这里验证的是"内核侧接线",不是共享段语义本身——后者由
// internal/plugin/proc 的 34 项测试(含 -race)覆盖。
// 共享段 Host 必须惰性创建且**全局唯一**。
//
// 每插件一段会让「内核 ctx → 段 → 插件改 → 回读 ctx」在多插件下
// 退化成副本模型,lost update 原样复现(§8.4 实测 35.8~36.8%)。
func TestRegistry_ProcHostIsSharedAndLazy(t *testing.T) {
r := NewRegistry()
defer r.closeProcHost()
if r.procHost != nil {
t.Error("共享段应惰性创建,未加载 .bin 插件时不该存在")
}
h1, err := r.ensureProcHost()
if err != nil {
t.Fatalf("创建共享段: %v", err)
}
h2, err := r.ensureProcHost()
if err != nil {
t.Fatalf("二次获取共享段: %v", err)
}
if h1 != h2 {
t.Fatal("共享段必须全局唯一——每插件一段会退化成副本模型,lost update 复现")
}
if h1.ShmSize() <= 0 {
t.Errorf("共享段大小应为正数,实际 %d", h1.ShmSize())
}
}
func TestRegistry_CloseProcHostIsIdempotent(t *testing.T) {
r := NewRegistry()
if _, err := r.ensureProcHost(); err != nil {
t.Fatalf("创建共享段: %v", err)
}
r.closeProcHost()
r.closeProcHost() // 二次关闭不应 panic
if r.procHost != nil {
t.Error("关闭后 procHost 应为 nil")
}
}
// loadProc 对非 proc 目录返回 nil,nil(交由后续探测通道)。
func TestRegistry_LoadProcSkipsNonProcDir(t *testing.T) {
r := NewRegistry()
defer r.closeProcHost()
plg, err := r.loadProc(t.TempDir(), "demo", nil)
if plg != nil || err != nil {
t.Fatalf("无 plugin.bin 应返回 nil,nil,实际 plg=%v err=%v", plg, err)
}
if r.procHost != nil {
t.Error("非 proc 目录不该触发共享段创建")
}
}
// 缺可执行权限时报明确错误——常见于经 zip/hmap 分发丢失权限位。
func TestRegistry_LoadProcRejectsNonExecutable(t *testing.T) {
r := NewRegistry()
defer r.closeProcHost()
dir := t.TempDir()
path := filepath.Join(dir, binEntry)
if err := os.WriteFile(path, []byte("#!/bin/sh\n"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
_, err := r.loadProc(dir, "demo", nil)
if err == nil {
t.Fatal("缺少可执行权限应报错")
}
// 错误消息须给出可直接执行的修复指令
if !strings.Contains(err.Error(), "chmod +x") {
t.Errorf("错误消息应含 chmod +x 修复指令,实际: %v", err)
}
}
// 构造出的插件必须能满足 registry 的 sdk.Plugin 接口(含 Close 供重载 kill 进程)。
func TestRegistry_LoadProcReturnsAdapter(t *testing.T) {
r := NewRegistry()
defer r.closeProcHost()
dir := t.TempDir()
path := filepath.Join(dir, binEntry)
if err := os.WriteFile(path, []byte("#!/bin/sh\nexec cat\n"), 0o755); err != nil {
t.Fatalf("write: %v", err)
}
plg, err := r.loadProc(dir, "demo", nil)
if err != nil {
t.Fatalf("loadProc: %v", err)
}
if plg == nil {
t.Fatal("应返回插件实体")
}
if plg.Name() != "demo" {
t.Errorf("Name() = %q, want demo", plg.Name())
}
// Close 必须可见:registry.closeDynamic 靠它真正 kill 子进程。
// 对比 cabi 路径的 Close 只做 dlclose,而 dlclose 对 Go c-shared 是 no-op
// (§1.1,热重载静默失效的根因)。
if _, ok := plg.(interface{ Close() error }); !ok {
t.Error("proc 插件须暴露 Close(),否则重载时子进程不会被回收")
}
if r.procHost == nil {
t.Error("加载 .bin 插件应触发共享段创建")
}
}
// procCore 必须满足 proc.CoreSDK,且**刻意不暴露**内核内部机制。
//
// 这是 §3.8 的核心:权限梯度从「C ABI 表达能力的意外产物」
// 变成显式声明并强制的策略。
//
// 守住的不变量:procCore 用**命名字段**持有内核 SDK。若日后有人改成
// 嵌入 *isdk.PluginSDK,全部方法会被提升,外部插件就能经类型断言拿到
// 这些内核能力——下面的断言会当场拦住。
func TestProcCore_SatisfiesCoreSDKAndWithholdsInternals(t *testing.T) {
var _ proc.CoreSDK = procCore{}
var c interface{} = procCore{}
if _, has := c.(interface{ Selftest() *isdk.VirtualInstance }); has {
t.Error("procCore 不应暴露 Selftest(§3.8 权限梯度)")
}
if _, has := c.(interface{ Supervisor() isdk.SupervisorAPI }); has {
t.Error("procCore 不应暴露 Supervisor(§3.8 权限梯度)")
}
if _, has := c.(interface{ Tracker() isdk.TrackerAPI }); has {
t.Error("procCore 不应暴露 Tracker(§3.8 权限梯度)")
}
if _, has := c.(interface{ Adapter() isdk.AdapterAPI }); has {
t.Error("procCore 不应暴露 Adapter(§3.8 权限梯度)")
}
if _, has := c.(interface{ Indexer() isdk.IndexerAPI }); has {
t.Error("procCore 不应暴露 Indexer(§3.8 权限梯度)")
}
if _, has := c.(interface{ Status() isdk.StatusAPI }); has {
t.Error("procCore 不应暴露 Status(§3.8 权限梯度)")
}
}
// nil SDK 下各能力访问器必须返回真 nil(而非类型化 nil)。
//
// corehandler 用 `if xxx == nil` 判断能力不可用并返回 errUnavailable;
// 类型化 nil 会让判空失效,插件收到的是 panic 而不是"能力不可用"。
func TestProcCore_NilCapabilitiesAreTrueNil(t *testing.T) {
c := newProcCore(&isdk.PluginSDK{})
if c.Settings() != nil {
t.Error("Settings() 应为真 nil")
}
if c.Memory() != nil {
t.Error("Memory() 应为真 nil")
}
if c.TextMemory() != nil {
t.Error("TextMemory() 应为真 nil")
}
if c.DocMemory() != nil {
t.Error("DocMemory() 应为真 nil")
}
if c.Knowledge() != nil {
t.Error("Knowledge() 应为真 nil")
}
if c.LLM() != nil {
t.Error("LLM() 应为真 nil")
}
if c.PluginMgr() != nil {
t.Error("PluginMgr() 应为真 nil")
}
// 无 IOManager 时同步注入返回空串,不 panic
if got := c.InjectInputSync("s", "c", "t"); got != "" {
t.Errorf("无 IOManager 时应返回空串,实际 %q", got)
}
}