176 lines
6.8 KiB
Python
176 lines
6.8 KiB
Python
"""Mem0 real-adapter wiring verified against a fake SDK: config, anchors, budget guard.
|
|
|
|
No network, no real embeddings, no real LLM. The fake mem0 module mirrors the
|
|
2.2.1 surfaces the adapter touches (Memory.from_config, add/search metadata,
|
|
vector-store payload reads) so the adapter's contract is executable; the fixed
|
|
install itself stays untouched on disk.
|
|
"""
|
|
import sqlite3
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
|
|
from cybergril.memory.semantic import EmbedLedgerGuard, Mem0Backend, SemanticMemory
|
|
from cybergril.memory.store import Store
|
|
|
|
# Shared fake vector store: Memory.add writes points, QdrantClient reads them,
|
|
# mirroring the on-disk qdrant path the real SDK shares between the two.
|
|
POINTS: dict[str, dict] = {}
|
|
|
|
|
|
class _Point:
|
|
def __init__(self, point_id, payload):
|
|
self.id = point_id
|
|
self.payload = payload
|
|
|
|
|
|
class FakeQdrantClient:
|
|
def __init__(self, path=None):
|
|
self.path = path
|
|
self.closed = False
|
|
|
|
def retrieve(self, collection_name, ids, with_payload=True):
|
|
bucket = POINTS.setdefault(collection_name, {})
|
|
return [_Point(i, bucket[i]) for i in ids if i in bucket]
|
|
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
|
|
class FakeEmbedder:
|
|
"""Pure like the real fastembed: no ledger knowledge lives in the SDK."""
|
|
|
|
def __init__(self, ledger=None):
|
|
self.ledger = ledger
|
|
|
|
def embed(self, text, memory_action=None):
|
|
return [0.0]
|
|
|
|
def embed_batch(self, texts, memory_action=None):
|
|
return [[0.0]] * len(texts)
|
|
|
|
|
|
class FakeMemory:
|
|
instances = []
|
|
|
|
def __init__(self, embedder, collection):
|
|
self.embedding_model = embedder
|
|
self.collection = collection
|
|
self.seq = 0
|
|
|
|
@classmethod
|
|
def from_config(cls, config):
|
|
import sys as _sys
|
|
|
|
instance = cls(_sys.modules["mem0"]._TEST_EMBEDDER,
|
|
config["vector_store"]["config"]["collection_name"])
|
|
cls.instances.append((instance, config))
|
|
return instance
|
|
|
|
def add(self, text, metadata=None):
|
|
self.embedding_model.embed(text, memory_action="add") # real SDK embeds on add
|
|
self.seq += 1
|
|
entry_id = f"m{self.seq}"
|
|
bucket = POINTS.setdefault(self.collection, {})
|
|
bucket[entry_id] = {"data": text, **(metadata or {})}
|
|
return {"results": [{"id": entry_id, "memory": text, "event": "ADD"}]}
|
|
|
|
def search(self, query, limit=5):
|
|
self.embedding_model.embed(query, memory_action="search") # and on search
|
|
bucket = POINTS.setdefault(self.collection, {})
|
|
return {"results": [{"id": k, "memory": v["data"]}
|
|
for k, v in bucket.items() if query[:2] in v["data"]][:limit]}
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_mem0(monkeypatch, tmp_path):
|
|
ledger_path = tmp_path / "ledger.sqlite3"
|
|
conn = sqlite3.connect(str(ledger_path))
|
|
conn.execute("CREATE TABLE embed_ops (id INTEGER PRIMARY KEY AUTOINCREMENT, ts TEXT, pid INTEGER,"
|
|
" kind TEXT, items INTEGER, note TEXT)")
|
|
conn.commit()
|
|
conn.close()
|
|
guard = EmbedLedgerGuard(ledger_path, cap=10)
|
|
|
|
memory_cls = FakeMemory
|
|
embedder = FakeEmbedder(guard)
|
|
mem0 = types.ModuleType("mem0")
|
|
mem0.Memory = memory_cls
|
|
mem0._TEST_EMBEDDER = embedder
|
|
qdrant_client = types.ModuleType("qdrant_client")
|
|
qdrant_client.QdrantClient = FakeQdrantClient
|
|
monkeypatch.setitem(sys.modules, "mem0", mem0)
|
|
monkeypatch.setitem(sys.modules, "qdrant_client", qdrant_client)
|
|
return guard, embedder, ledger_path
|
|
|
|
|
|
def backend_for(tmp_path, fake_mem0, **overrides):
|
|
guard, embedder, ledger_path = fake_mem0
|
|
config = {
|
|
"gateway_base_url": "http://192.168.200.11:8731/v1",
|
|
"gateway_model": "halogen-qwen3.8-flash-next",
|
|
"vector_root": str(tmp_path / "vec"),
|
|
"history_root": str(tmp_path / "hist"),
|
|
"collection": "cg_public",
|
|
"ledger_path": str(ledger_path),
|
|
"embed_cap": 10,
|
|
}
|
|
config.update(overrides)
|
|
# the fake embedder must debit the same guard the backend builds from config
|
|
sys.modules["mem0"]._TEST_EMBEDDER = FakeEmbedder(EmbedLedgerGuard(ledger_path, config["embed_cap"]))
|
|
return Mem0Backend(config)
|
|
|
|
|
|
async def test_add_carries_anchor_and_search_roundtrips(tmp_path, fake_mem0):
|
|
backend = backend_for(tmp_path, fake_mem0)
|
|
store = Store(tmp_path / "mem0.db")
|
|
with store.conn:
|
|
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
|
"(1,'user','钥匙在抽屉','t9','sent')")
|
|
rows = [{"message_id": 1, "turn_id": "t9", "role": "user", "text": "钥匙在抽屉"}]
|
|
anchor = {"turn_ids": ["t9"], "turn_roles": [["t9", "user", 1]],
|
|
"input_sha256": "x"}
|
|
created = await backend.add_group(rows, anchor, "chat_public:t9:abc")
|
|
assert created and created[0]["id"].startswith("m")
|
|
payload = backend.payload(created[0]["id"])
|
|
assert payload["src_group"] == "chat_public:t9:abc"
|
|
assert payload["turn_roles"] == anchor["turn_roles"]
|
|
hits = await backend.search("钥匙")
|
|
assert hits and hits[0]["memory"] == "钥匙在抽屉"
|
|
await backend.aclose()
|
|
store.close()
|
|
|
|
|
|
async def test_budget_guard_fails_closed_without_or_beyond_cap(tmp_path, fake_mem0, monkeypatch):
|
|
guard, embedder, ledger_path = fake_mem0
|
|
backend = backend_for(tmp_path, fake_mem0, embed_cap=0) # no approved budget
|
|
with pytest.raises(RuntimeError):
|
|
await backend.search("任何查询")
|
|
# and with a cap, exhausting it refuses further work
|
|
backend2 = backend_for(tmp_path, fake_mem0, embed_cap=2)
|
|
rows = [{"message_id": 1, "turn_id": "t", "role": "user", "text": "一句话"}]
|
|
await backend2.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
|
|
"input_sha256": "x"}, "g1")
|
|
await backend2.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
|
|
"input_sha256": "x"}, "g2")
|
|
with pytest.raises(RuntimeError):
|
|
await backend2.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
|
|
"input_sha256": "x"}, "g3")
|
|
|
|
|
|
async def test_semantic_memory_end_to_end_with_fake_sdk(tmp_path, fake_mem0):
|
|
guard, embedder, ledger_path = fake_mem0
|
|
backend = backend_for(tmp_path, fake_mem0)
|
|
store = Store(tmp_path / "e2e.db")
|
|
semantic = SemanticMemory(store, backend)
|
|
with store.conn:
|
|
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
|
"(1,'user','阳台的牵牛花开了','t1','sent')")
|
|
semantic.enqueue_uncovered()
|
|
await semantic.tick()
|
|
assert store.query_one("SELECT state FROM sem_batch")["state"] == "done"
|
|
block = await semantic.context("牵牛花", [], 600)
|
|
assert "牵牛花" in block and block.startswith("她确实记得的更早的事")
|
|
store.close()
|