mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-22 09:58:06 +00:00
按「声明式的也要支持,相当于主动被动两条路」落实。此前两者只是恰好并存,
没有边界,实测会互相吃掉(下面的坑就是)。
- Triple.Scenes []string(多值):一轮写下的记忆**两条路都挂**。
只挂一条会丢东西——只挂声明则细粒度唤起丢失,只挂涌现则首次交互
(场景还没长出来)没有兜底。单值 Scene 保留兼容。
- TurnScene:Primary 用于写(优先涌现场景,首次退到声明场景兜底),
Keys 是两条路的并集,用于召回(声明+涌动的场景一起进 RecallByScene)。
- EnterSceneWithHint:主动路 EnsureScene(声明即建场景,不等第二次),
被动路 EnterScene(指纹聚类)。写侧由 executeToolCall 把本轮场景集合
传给 memory_commit,模型不需要知道"场景"这回事。
踩到并修掉的坑(两条路互相吞噬):
最初让声明场景也吸收**整轮指纹**,于是 chan:qq 的相似度永远是 1.0,
把后续所有同类轮次全部吃掉 → 被动路再也长不出更细的场面,
实测 turn2.Emergent=true 但 Primary 仍是 chan:qq、没有 auto: 场景。
修法:给场景加 origin(declared/emergent):
- 被动聚类只认 origin='emergent' 的场景(声明场景不进相似度空间);
- 声明场景的特征**只从键自身解析**(chan:qq/peer:group_1 → {chan:qq, peer:group_1}),
白名单 kind(chan/peer/peer_group/tool/topic/part),不猜——
「老大2026-09-04_12:27_qq私聊图片」里的 12:27 也是 kind:value 形态,
放进特征空间就是往相似度里灌垃圾(有测试钉住)。
- 声明路的泛化靠**层级键前缀**(chan:qq 覆盖 chan:qq/peer:x),机制各归各。
- memgc -scene-stats 增加 [declared|emergent] 与 strength/features 两栏,
可直接观察两条路各自在长什么。
新增/改写用例:
- TestDeclaredAndEmergentBothLearn:首次交互兜底到声明场景 → 第 2 轮长出
细粒度涌现场景且**优先用于写入** → 声明场景不进相似度空间(防止压死被动路)
但仍走声明键取回 → 两条路都进召回集合 → 声明键特征解析与白名单。
- TestEffectiveScenes:多值+单值合并去重保序。
go build/vet 干净,go test -count=1 ./... 全绿。
170 lines
5.5 KiB
Go
170 lines
5.5 KiB
Go
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 "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||
agentIO "gitcode.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||
"gitcode.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)
|
||
}
|
||
}
|