mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-15 12:15:16 +00:00
test(memory)!: 块 union 化判据重写(CAS 淘汰第二阶段:测试)
生产已在上一提交(86572d13)全绿;本提交把 CAS 专属测试改到块语义,
并修掉几处判据自身的缺陷(判据错 ≠ 生产错):
- sdk/memory_impl_test: media.Store→块;NewGraphMemoryWithPlugin
- core/{graphmedia,mediaref,medialive,medialoop,inputunify}_test:
mediaStore→memory;PayloadDigest→Data/BlockData;shortDigest→shortID
- graphmedia: 无引用句子用独立块 ID(shortID 20 字符会与 blk_src_ 撞长度)
- memory/sentence_block: BlockByIDPrefix 加 len<12 下限(短引用守卫)
- core/medialoop: reembedOneBlock 按 BlockData 兜底取字节(lean 列无 data)
- knowledge: broken.png 改目录造读失败(chmod 0000 对 root 无效);
dense/attach 测试补 Data 并先落盘后 AttachMedia
- webui: graph_pulse fixture payload_digest→has_data
注:internal/plugin/proc 的 shmVersion 不匹配(内核1/插件2)是
SDK 仓(777c3a0,v2 模板)与本分支内核(v1)的跨仓版本错位,
与块 union 无关(stash 全部改动后仍复现),属 proc-extra 特性线。
This commit is contained in:
@ -8,80 +8,30 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/JianFeeeee/HomeAgent/internal/memory"
|
||||
"github.com/JianFeeeee/HomeAgent/internal/memory/document"
|
||||
"github.com/JianFeeeee/HomeAgent/internal/memory/media"
|
||||
"github.com/JianFeeeee/HomeAgent/internal/memory/vector"
|
||||
)
|
||||
|
||||
// 媒体与记忆块的生命周期测试。
|
||||
// 媒体与记忆块的生命周期测试(块 union 化之后)。
|
||||
//
|
||||
// 媒体没有独立生命周期管理(没有 GC、没有引用计数):blob 是记忆块的内容,
|
||||
// 块的创建/迁移/删除由记忆系统决定。图片也不靠文本描述索引。
|
||||
// 媒体没有独立生命周期管理(没有 GC、没有引用计数):字节是记忆块的内容,
|
||||
// 块的创建/迁移/删除由记忆系统决定。块被删即字节被删——没有独立层要清理。
|
||||
//
|
||||
// 需要特殊对待的只有一项:向量空间更换(换模型/换维度)时,
|
||||
// 历史图像块的向量要迁移到新空间,否则永远无法被召回。
|
||||
|
||||
func newMediaLoopAgent(t *testing.T) (*Agent, *media.Store) {
|
||||
func newMediaLoopAgent(t *testing.T) *Agent {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
ms, err := media.New(filepath.Join(dir, "media"))
|
||||
if err != nil {
|
||||
t.Fatalf("media.New: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { ms.Close() })
|
||||
|
||||
a := &Agent{mediaStore: ms}
|
||||
a.ctx, a.cancel = context.WithCancel(context.Background())
|
||||
t.Cleanup(a.cancel)
|
||||
return a, ms
|
||||
}
|
||||
|
||||
// heldMediaDigests 汇总三层记忆持有的媒体:只有这些才可被召回。
|
||||
func TestHeldMediaDigests_CollectsAcrossLayers(t *testing.T) {
|
||||
a, ms := newMediaLoopAgent(t)
|
||||
d1, _ := ms.Put([]byte("ctx-layer"), media.Item{MIME: "image/png"})
|
||||
d2, _ := ms.Put([]byte("doc-layer"), media.Item{MIME: "image/png"})
|
||||
d3, _ := ms.Put([]byte("graph-layer"), media.Item{MIME: "image/png"})
|
||||
d4, _ := ms.Put([]byte("orphan"), media.Item{MIME: "image/png"})
|
||||
|
||||
a.context = NewRelevanceContext("", memory.NewStaticEmbedder(""))
|
||||
a.context.Append(ContextEvent{Input: "带图的一轮", Blocks: []memory.MemoryBlock{
|
||||
{ID: "blk_ctx", Modality: memory.BlockImage, PayloadDigest: d1},
|
||||
}})
|
||||
|
||||
dir := t.TempDir()
|
||||
bo, ok := a.blockFromDigest(d2)
|
||||
if !ok {
|
||||
t.Fatal("blockFromDigest 失败")
|
||||
}
|
||||
ds := document.NewStore(filepath.Join(dir, "docs"), memory.TokenizeWords)
|
||||
if err := ds.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ds.Stop()
|
||||
if err := ds.Insert(&document.Doc{ID: "doc_1", Summary: "s", Blocks: []memory.MemoryBlock{bo}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.docStore = ds
|
||||
|
||||
g, err := memory.NewGraphDB(filepath.Join(dir, "graph.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Fatalf("NewGraphDB: %v", err)
|
||||
}
|
||||
defer g.Close()
|
||||
if err := g.PutMemoryBlocks([]memory.MemoryBlock{
|
||||
{ID: "blk_g", Modality: memory.BlockImage, PayloadDigest: d3},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.memory = g
|
||||
t.Cleanup(func() { g.Close() })
|
||||
|
||||
held := a.heldMediaDigests()
|
||||
for _, want := range []string{d1, d2, d3} {
|
||||
if !held[want] {
|
||||
t.Errorf("层次持有 %s 却不在结果里: %v", shortDigest(want), held)
|
||||
}
|
||||
}
|
||||
if held[d4] {
|
||||
t.Errorf("无人持有的 %s 不该出现在结果里", shortDigest(d4))
|
||||
}
|
||||
a := &Agent{memory: g}
|
||||
a.ctx, a.cancel = context.WithCancel(context.Background())
|
||||
t.Cleanup(a.cancel)
|
||||
return a
|
||||
}
|
||||
|
||||
// fakeSpace 是一个只覆盖图像的假统一空间,用来验证「不在本空间」与
|
||||
@ -106,82 +56,49 @@ func (fakeSpace) Close() {}
|
||||
// - 把音频当失败反复重试;
|
||||
// - 更不能拿另一个模型的向量顶替音频(那会污染统一空间且静默)。
|
||||
func TestReembedStaleMedia_SkipsUnsupportedWithoutFaking(t *testing.T) {
|
||||
a, ms := newMediaLoopAgent(t)
|
||||
img, _ := ms.Put([]byte("img-bytes"), media.Item{MIME: "image/png"})
|
||||
aud, _ := ms.Put([]byte("aud-bytes"), media.Item{MIME: "audio/wav"})
|
||||
a := newMediaLoopAgent(t)
|
||||
// 图像块与音频块各落一条:字节都进块(union)
|
||||
img := mediaBlock("image/png", []byte("img-bytes"))
|
||||
aud := mediaBlock("audio/wav", []byte("aud-bytes"))
|
||||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{img, aud}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a.multimodalSpace = fakeSpace{}
|
||||
a.reembedStaleMedia()
|
||||
|
||||
it, err := ms.Stat(img)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
// 图像块应拿到本空间向量(fp 写回,dim=2)
|
||||
imgGot, ok, err := a.memory.BlockByIDPrefix(img.ID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("BlockByIDPrefix(img): %v / %v", ok, err)
|
||||
}
|
||||
if len(it.Vec) != 2 || it.VecModel != "fake-space" {
|
||||
t.Fatalf("图像应拿到本空间向量,实际 vec=%v model=%q", it.Vec, it.VecModel)
|
||||
if len(imgGot.Vector) != 2 || imgGot.Fingerprint != "fake-space" {
|
||||
t.Fatalf("图像应拿到本空间向量,实际 vec=%v model=%q", imgGot.Vector, imgGot.Fingerprint)
|
||||
}
|
||||
|
||||
audIt, err := ms.Stat(aud)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
// 音频块不得被写入任何向量(不能用别的模型顶替),fp 应保持为空
|
||||
audGot, ok, err := a.memory.BlockByIDPrefix(aud.ID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("BlockByIDPrefix(aud): %v / %v", ok, err)
|
||||
}
|
||||
if len(audIt.Vec) != 0 || audIt.VecModel != "" {
|
||||
if len(audGot.Vector) != 0 || audGot.Fingerprint != "" {
|
||||
t.Fatalf("音频不得被写入任何向量(不能用别的模型顶替),实际 vec=%v model=%q",
|
||||
audIt.Vec, audIt.VecModel)
|
||||
audGot.Vector, audGot.Fingerprint)
|
||||
}
|
||||
}
|
||||
|
||||
// payloadHeld 是删除前的活查询。
|
||||
func TestPayloadHeld(t *testing.T) {
|
||||
a, ms := newMediaLoopAgent(t)
|
||||
d, _ := ms.Put([]byte("held"), media.Item{MIME: "image/png"})
|
||||
if a.payloadHeld(d) {
|
||||
t.Fatal("尚无块持有时不该报已持有")
|
||||
// 无多模态空间可用时迁移路径必须整体跳过,不报错也不写脏向量。
|
||||
func TestReembedStaleMedia_NilSpaceNoop(t *testing.T) {
|
||||
a := newMediaLoopAgent(t)
|
||||
b := mediaBlock("image/png", []byte("x"))
|
||||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{b}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a.context = NewRelevanceContext("", memory.NewStaticEmbedder(""))
|
||||
a.context.Append(ContextEvent{Input: "x", Blocks: []memory.MemoryBlock{
|
||||
{ID: "blk_1", Modality: memory.BlockImage, PayloadDigest: d},
|
||||
}})
|
||||
if !a.payloadHeld(d) {
|
||||
t.Fatal("L0 持有却报未持有")
|
||||
}
|
||||
if a.payloadHeld("") {
|
||||
t.Fatal("空 digest 应为 false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestForgetPayloads_DeletesOnlyUnheldContent 验证删除语义:
|
||||
// 块被删除后内容才被删;仍被其它记忆块共享的 digest 不会被误删。
|
||||
func TestForgetPayloads_DeletesOnlyUnheldContent(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
ms, err := media.New(filepath.Join(dir, "media"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ms.Close()
|
||||
|
||||
d1, _ := ms.Put([]byte("held-by-graph"), media.Item{MIME: "image/png"})
|
||||
d2, _ := ms.Put([]byte("being-forgotten"), media.Item{MIME: "image/png"})
|
||||
|
||||
g, err := memory.NewGraphDB(filepath.Join(dir, "graph.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer g.Close()
|
||||
if err := g.PutMemoryBlocks([]memory.MemoryBlock{
|
||||
{ID: "blk_keep", Modality: memory.BlockImage, PayloadDigest: d1},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a := &Agent{mediaStore: ms, memory: g}
|
||||
a.forgetPayloads([]string{d1, d2})
|
||||
|
||||
if _, err := ms.Stat(d1); err != nil {
|
||||
t.Fatalf("仍被 L3 块持有的内容不该被删: %v", err)
|
||||
}
|
||||
if _, err := ms.Stat(d2); err == nil {
|
||||
t.Fatal("无人持有的内容应被删除")
|
||||
// multimodalSpace 为 nil:整体跳过
|
||||
a.multimodalSpace = nil
|
||||
a.reembedStaleMedia()
|
||||
got, _, _ := a.memory.BlockByIDPrefix(b.ID)
|
||||
if len(got.Vector) != 0 || got.Fingerprint != "" {
|
||||
t.Errorf("无空间可用时不该写向量: vec=%v model=%q", got.Vector, got.Fingerprint)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user