mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-25 03:18:08 +00:00
上一版场景是声明/派生的:调用方写 scene="chan:qq",或由通道机械派生。 那不是涌现,是贴标签——标签谁定、怎么定全靠人。按「像人一样:干了什么事, 后续类似场面自动唤起对应记忆」的要求重做。 机制(全部取自运行时可观察量,无需模型配合、无需人工标注): - **场面指纹 Situation**:每轮采集 `chan:xx / peer:xx / peer_group:xx / tool:xx / topic:xx / part:xx`。权重按种类:通道与对象最强(1.0), 工具次之(0.8),话题是软信号(0.4),时段最弱(0.2)。 - **归属判定用加权 Jaccard**(不是字符串相等):共享特征权重和 / 并集权重和。 加权是必须的——`chan:qq` 与 `topic:排班` 的证据力差 2.5 倍,不加权会让 一次偶然的话题重合把两个不同场面并成一个。 - **涌现**:同类指纹重复到 minSceneEvidence=2 次才长出场景 (首次只登记 situation_evidence 足迹)。一次性的交互不是「场面」, 给它建场景会让库被一次性事件撑满、之后每次路过都召回一堆只发生过一次的事。 - **强化**:场景每次重现 strength+1、并入新特征。 - **唤起**:RecallBySituation 按**相似度**取回(阈值 0.35,比归属阈值 0.5 低 ——想不起来是损失,多想起一条只是多几行上下文),与措辞无关。 - **遗忘**:DecaySceneRefs 按半衰期让久未重现的关联淡出,低于 floor 直接删; 已接进 archive 心跳(半衰期 30 天,比「这个月没做过这类事」更久)。 三个必须讲清的边界: 1. 一轮只解析一次场景(TaskFrame 缓存)——多解析一次就多记一次强度, 「工具调得多」会被误读成「这个场面更常出现」。 2. 声明与涌现**并存**:声明是「我知道这是哪个场面」(插件注入点最清楚), 涌现是「这轮看起来像哪个场面」。两者都进召回。 3. 记忆挂载全自动:memory_commit 没写 scene 时落到本轮涌现场景, 模型不需要知道场景这回事。 验证:go build/vet 干净,go test -count=1 ./... 全绿。 新增用例(核心证据): - TestSceneEmergesFromRepetition:首次不建场景 → 第 2 次同类场面长出场景 → 同场面**不同话题**仍并入同一场景 → 换通道的场面自己长出独立场景(共 2 个)→ 强度随重现增长、特征多条。全程没有任何人声明过场景键。 - TestSceneRecallsBySituationNotWording:场面里写下的规则,换措辞后仍被 自动唤起(含原句),无关场面不唤起。 - TestSceneRefDecay:一个半衰期权重减半、第二个半衰期低于 floor 被清掉, 仍在重现的场景不受影响。 - TestSituationFeaturesFor:指纹维度齐全、归一化、数值 group_id 转换、nil 安全。
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": "主记忆实体,子独有实体"},
|
||
}, "")
|
||
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{}{},
|
||
},
|
||
}, "")
|
||
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)
|
||
}
|
||
}
|