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

170 lines
5.5 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
// N2c 验收:**轻量内核 profile**。
//
// 设计 docs/zh/resident-subagent-design.md §16.0(窄接口 + nil 即禁用)与 §5.5。
//
// 轻量内核(驻留子)的记忆装配:
// - graph = *memory.LightMemory(读 temp∪main,只写 temp)
// - memory = nil ⇒ 既有的 `if a.memory != nil` 关卡自动禁掉**全部**整理面:
// 记忆整理流水线(distill.go 的 archive/review/merge 循环)、记忆块与媒体桥
// (graphmedia.go / medialoop.go)、记忆整理工具(merge/delete/purge/edit/block_merge)
//
// 因此这里要钉住四件事:
// ① 子的写入只落 temp,主库不受影响;
// ② 子的读是并集(看得到主库 + 自己的 temp);
// ③ 整理类工具**不进子的工具表**;
// ④ 即便被直调,整理类操作也明确报"轻量内核不支持"(纵深防御,不静默降级)。
import (
"path/filepath"
"strings"
"testing"
agentAPI "github.com/JianFeeeee/HomeAgent/internal/agent/api"
agentIO "github.com/JianFeeeee/HomeAgent/internal/agent/io"
"github.com/JianFeeeee/HomeAgent/internal/memory"
)
// newLightAgent 构造一个轻量内核 agent(驻留子形态):有 LightMemory,没有整理面。
func newLightAgent(t *testing.T, main *memory.GraphDB, tempPath string) *Agent {
t.Helper()
light, err := memory.NewLightMemory(main, tempPath, true)
if err != nil {
t.Fatal(err)
}
a := New(AgentConfig{
ID: "sub-1",
Provider: &scriptProvider{},
ProviderManager: agentAPI.NewProviderManager(),
IO: agentIO.NewIOManager(),
StageHost: NewStageHost(),
LightMemory: light, // 轻量内核:只给图记忆共同面
})
t.Cleanup(func() { light.Close() })
return a
}
func TestLightProfile_MemoryFaceWiring(t *testing.T) {
dir := t.TempDir()
main, err := memory.NewGraphDB(filepath.Join(dir, "main.db"))
if err != nil {
t.Fatal(err)
}
defer main.Close()
if _, _, err := main.Commit([]memory.Triple{
{Subject: "主记忆实体", Relation: "属于", Object: "主库"},
}, "sess", 1); err != nil {
t.Fatal(err)
}
a := newLightAgent(t, main, filepath.Join(dir, "sub.db"))
// ① 整理面必须为 nil —— 这正是"nil 即禁用"的开关。
if a.memory != nil {
t.Fatal("轻量内核不该有整理面(a.memory 必须为 nil)")
}
if a.graphMem() == nil {
t.Fatal("轻量内核必须有图记忆共同面")
}
// ② 写入只落 temp:主库不得出现子才知道的实体。
if _, _, _, err := a.commitTriplesWithMedia([]memory.Triple{
{Subject: "子独有实体", Relation: "来自", Object: "子的temp"},
}, "sess", 1, nil); err != nil {
t.Fatalf("子的写入应成功(落 temp): %v", err)
}
mainRes, err := main.Recall([]string{"子独有实体"}, nil, 1, "")
if err != nil {
t.Fatal(err)
}
for _, e := range mainRes.Entities {
if e.Name == "子独有实体" {
t.Fatalf("子的写入不该进主库:%v", e.Name)
}
}
// ③ 读是并集:主库的实体与 temp 的实体都要看得到。
got := a.executeMemoryTool(agentAPI.ToolCall{
ID: "c1", Name: "memory_recall",
Arguments: map[string]interface{}{"query_intent": "主记忆实体,子独有实体"},
}, nil)
if !strings.Contains(got, "主记忆实体") {
t.Fatalf("子应看得到主记忆:%s", got)
}
if !strings.Contains(got, "子独有实体") {
t.Fatalf("子应看得到自己的 temp:%s", got)
}
}
func TestLightProfile_OrganizeToolsAbsentAndRefused(t *testing.T) {
dir := t.TempDir()
main, err := memory.NewGraphDB(filepath.Join(dir, "main.db"))
if err != nil {
t.Fatal(err)
}
defer main.Close()
a := newLightAgent(t, main, filepath.Join(dir, "sub.db"))
// ① 整理类工具不进子的工具表(不是进去再报不可用)。
names := toolNames(a)
for _, banned := range []string{
"memory_merge", "memory_delete_entity", "memory_block_merge",
"memory_purge", "memory_edit",
} {
if hasTool(names, banned) {
t.Fatalf("轻量内核不该声明整理类工具 %s:%v", banned, names)
}
}
// 对照:共同面/无关能力照常在。
if !hasTool(names, "input_channels") {
t.Fatalf("轻量内核仍应有共同面工具:%v", names)
}
// ② 纵深防御:即便被直调,整理类操作也必须明确报"轻量内核不支持"。
for _, tool := range []string{
"memory_merge", "memory_delete_entity", "memory_block_merge",
"memory_purge", "memory_edit", "memory_introspect",
} {
got := a.executeMemoryTool(agentAPI.ToolCall{
ID: "x", Name: tool,
Arguments: map[string]interface{}{
"name": "任意", "source": "a", "target": "b", "criteria": map[string]interface{}{},
},
}, nil)
if !strings.Contains(got, "轻量内核") {
t.Fatalf("%s 在轻量内核里必须明确报不支持,实际 %q", tool, got)
}
}
}
// 对照:完整内核(根 agent)仍有整理面与整理工具。
func TestFullProfile_KeepsOrganizeFace(t *testing.T) {
dir := t.TempDir()
main, err := memory.NewGraphDB(filepath.Join(dir, "main.db"))
if err != nil {
t.Fatal(err)
}
defer main.Close()
a := New(AgentConfig{
ID: "root",
Provider: &scriptProvider{},
ProviderManager: agentAPI.NewProviderManager(),
IO: agentIO.NewIOManager(),
StageHost: NewStageHost(),
Memory: main,
})
if a.memory == nil {
t.Fatal("根 agent 必须有整理面")
}
if a.graphMem() == nil {
t.Fatal("根 agent 必须有图记忆共同面")
}
names := toolNames(a)
if !hasTool(names, "memory_merge") {
t.Fatalf("根 agent 应保留整理类工具:%v", names)
}
}