Files
memoryweave/go/internal/storage/scan_test.go
T
小唯 1060e62066 fix(admin): /memories 与 /memories/export 改用全表扫描(Scan),弃零向量 ANN
病根:两个列表/导出端点一直用『零向量 ANN』冒充『列出全部』——零向量没有方向,
ANN 退化成本存储序前 N 行,再按 ns 客户端过滤 ⇒ 计数完全失真
(实测 export hermes-main 报 1 条,真实 139+;shared 报 0,真实 29)。

修复:
① Rust lancedb_ops: scan_for_forgetting 加 namespace 下推(only_if),limit 作用于过滤后结果集
② Go 接口 LanceDB 新增 Scan(limit, ns):
   - RustLanceDBClient → lancedb_scan RPC
   - SQLiteClient → SELECT ... WHERE is_deleted=0 [AND ns] ORDER BY created_at DESC LIMIT
   - MemLanceClient → 内存 map 过滤 + created_at 倒序
③ 两个端点 ListMemories / ExportMemoriesMD 改用 aa.LanceDB.Scan(limit, ns)
④ 补 mock(zz_commit mockLance / write-buffer fakeLDB)+ 5 条新判据
   (全表 3 / hermes-main 2 / shared 1 / limit 生效 / 不存在 ns 0)
2026-09-21 16:36:41 +08:00

54 lines
1.8 KiB
Go

package storage
import "testing"
// TestMemLanceScanFiltersAndLimits 验证全表扫描的「is_deleted 过滤 + namespace 下推 + limit」。
// 这是 /api/v1/memories 与 /api/v1/memories/export 的正确底座(修前它们用零向量 ANN 冒充列表,
// 零向量无方向 ⇒ 计数失真)。
func TestMemLanceScanFiltersAndLimits(t *testing.T) {
mlc := NewMemLanceClient(&Embedder{endpoint: "http://127.0.0.1:1/never", dim: 1024})
mlc.memories = map[string]*memEntry{
"m1": {ID: "m1", Namespace: "hermes-main", Category: "distilled", Content: "x"},
"m2": {ID: "m2", Namespace: "hermes-main", Category: "distilled", Content: "x"},
"m3": {ID: "m3", Namespace: "shared", Category: "distilled", Content: "x"},
}
// ① 全表(无 ns)→ 3 条
all, err := mlc.Scan(100, "")
if err != nil {
t.Fatalf("Scan 出错: %v", err)
}
if len(all) != 3 {
t.Errorf("✗ 全表应得 3 条,实际 %d", len(all))
}
t.Logf("✓ 全表扫描 %d 条", len(all))
// ② namespace=hermes-main → 2 条
hm, _ := mlc.Scan(100, "hermes-main")
if len(hm) != 2 {
t.Errorf("✗ hermes-main 应得 2 条,实际 %d", len(hm))
}
t.Logf("✓ hermes-main 过滤 %d 条", len(hm))
// ③ namespace=shared → 1 条
sh, _ := mlc.Scan(100, "shared")
if len(sh) != 1 {
t.Errorf("✗ shared 应得 1 条,实际 %d", len(sh))
}
t.Logf("✓ shared 过滤 %d 条", len(sh))
// ④ limit 生效(limit=1 → 最多 1 条)
one, _ := mlc.Scan(1, "")
if len(one) > 1 {
t.Errorf("✗ limit=1 应 ≤1 条,实际 %d", len(one))
}
t.Logf("✓ limit 生效 %d 条", len(one))
// ⑤ 空 ns(不存在的命名空间)→ 0 条
none, _ := mlc.Scan(100, "does-not-exist")
if len(none) != 0 {
t.Errorf("✗ 不存在 ns 应得 0 条,实际 %d", len(none))
}
t.Logf("✓ 空 ns 过滤 %d 条", len(none))
}