Files
CyberGril/tests/test_mem0_backend.py
T
zonggongandmultica-agent e978e878df HEL-874 rework: bounded verbatim readback reaches the real provider input, real pinned Mem0 2.2.1 SDK contract verified live and the adapter's five wiring defects fixed, budget gates made provable (atomic reserve/settle embed ledger + independent llm_ops cap), definite-cap real-check execution table, 8360 isolated release package prepared (not deployed), 2026-10-26 user originals recorded source-only - semantic.context now quotes chain-verified archive rows (original words + role + when, <=200-char window with visible trim marker, summary demoted to a one-line pointer, same min(600,budget) cap) and episodic.context flips from gist-first-120-chars to verbatim-first (<=240) + role + date, both proven by tests/test_readback_hel874.py capturing the actual messages handed to the provider through the real API path: a detail absent from the gist yet quoted verbatim from a turn pushed outside the recent-20 window, the same for a semantic chain hit, and an unshared companion row never leaking through FTS, semantic or any anchor (AM-001); tools/verify_mem0_real_hel874.py runs the FIXED install itself (mem0ai 2.2.1 2c81af3, real embedded qdrant, real fastembed from the existing cache, deterministic 127.0.0.1 fake gateway, temp ledgers only): before=31970c4 dies at construction (second QdrantClient on the same path rejected by the SDK - recorded), and source-level findings confirmed live and fixed - unscoped search raises ValueError in 2.2.1 (add now carries user_id scope, search passes filters+top_k since limit is silently ignored), payload reads ride the SDK's own vector_store.client, the old outer search debit double-counted (wrapper is now the single accounting point), the SDK's internal extraction LLM was ungated (LlmLedgerGuard with its own llm_ops table, cap default 0) and check-then-debit could overshoot under concurrency (EmbedLedgerGuard now reserves atomically in BEGIN IMMEDIATE, releases on failure/cancel, purges stale holds); after fixes all 12 checks pass (construction/anchor payload/user scope/1 LLM + 2-3 embeds per add measured/extraction input set provably only prior gated batches incl. old memories and rolling history/unshared canary zero outbound/search single debit/concurrency never over cap/close-reopen intact/both zero caps refuse with ZERO outbound LLM requests), evidence in docs/rework/hel874/; mood sensitivity returns to the original repo's continuous rule 1.35-0.70*strength (bazi.sensitivity_from_strength, foundation commit 1012d8b) replacing HEL-870's invented 1.25/0.8 step and is labeled an engineering heuristic, never user-approved bazi science (external agent formulas remain not adopted); docs/budget-plan-hel874.md gives definite caps for the one-time team check (55 requests/0 embeds), first-week semantic activation (<=60 extract LLM / <=180 embeds) and user daily use with code-level hard-gate facts and authorization sources (old 20-30/100-200 were estimates, not approved quota; frozen 39/60 and 498/500 untouched and never挪用); deploy/hel874-release.md + cybergril-hel874-8360.service.example prepare the isolated fresh-empty-data-dir release with health capability matrix and data-preserving rollback (NOT executed, PR#11 not merged, no user-facing loop, no experience address); sources/comment-evidence-full-experience-20261026.json adds the two 2026-10-26 user originals (01a17806, 01a1780e full-experience-then-iterate) with authorship verified and manifest appended only - baseline/current/coverage/changes untouched; verification: 256 python tests pass on 3.11.16 venv + requirements.lock offline (3 new real-provider-input readback cases incl. AM-001 negative, mem0 fake rebuilt to mirror the real 2.2.1 surfaces, 9 cases), ruff clean, web tsc + 42 vitest + build pass, local 8360 stub smoke (loopback, isolated temp DB) health R4-assembly all green then stopped, real-SDK script re-run 12/12 after final edits, dual-target requirement checks 4/4 pass with handoff-HEL-874.json; zero real model calls, zero real embeddings, zero outbound (fake gateway on loopback + existing local model cache only), authoritative ledgers and freezes untouched, no existing services or profiles touched
Co-authored-by: multica-agent <github@multica.ai>
2026-10-26 22:13:45 +08:00

279 lines
12 KiB
Python

"""Mem0 real-adapter wiring verified against a fake SDK mirroring pinned 2.2.1.
No network, no real embeddings, no real LLM. The fake mem0 module mirrors the
surfaces the pinned install actually exposes (verified live by
tools/verify_mem0_real_hel874.py): Memory.from_config, embedding_model.embed /
embed_batch(texts, memory_action), llm.generate_response, add(user_id=...),
search(query, filters=..., limit=...) — unscoped search REJECTED in 2.2.1 —
and vector_store.client payload reads. The fixed install itself stays untouched
on disk; adapter behavior against the real install is proven separately in
docs/rework/hel874/.
"""
import asyncio
import json
import sqlite3
import sys
import types
import pytest
from cybergril.memory.semantic import EmbedLedgerGuard, LlmLedgerGuard, Mem0Backend, SemanticMemory
from cybergril.memory.store import Store
# Shared fake vector store: Memory.add writes points, the SDK's own client reads
# them back — mirroring the single-client path the adapter now uses.
POINT_LOG: dict[str, dict] = {}
class _Point:
def __init__(self, point_id, payload):
self.id = point_id
self.payload = payload
class FakeQdrantClient:
def __init__(self):
self.closed = False
def retrieve(self, collection_name, ids, with_payload=True):
bucket = POINT_LOG.setdefault(collection_name, {})
return [_Point(i, bucket[i]) for i in ids if i in bucket]
def close(self):
self.closed = True
class _FakeVectorStore:
def __init__(self):
self.client = FakeQdrantClient()
class FakeEmbedder:
"""Pure like the real fastembed: no ledger knowledge lives in the SDK."""
def embed(self, text, memory_action=None):
return [0.0]
def embed_batch(self, texts, memory_action=None):
return [[0.0]] * len(texts)
class FakeLlm:
"""Deterministic extraction: echoes the batch key as one ADD result."""
def __init__(self):
self.calls = []
def generate_response(self, messages=None, **kwargs):
self.calls.append(messages)
user = messages[1]["content"] if messages and len(messages) > 1 else ""
marker = user.split("\n")[0][:20] if user else "记忆"
return json.dumps({"memory": [{"text": f"提取:{marker}", "event": "ADD"}]},
ensure_ascii=False)
class FakeMemory:
instances = []
def __init__(self, embedder, collection):
self.embedding_model = embedder
self.vector_store = _FakeVectorStore()
self.llm = FakeLlm()
self.collection = collection
self.seq = 0
@classmethod
def from_config(cls, config):
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, user_id=None):
if not user_id:
raise ValueError("filters must contain at least one of: user_id, agent_id, run_id")
self.embedding_model.embed(text, memory_action="search") # 2.2.1 phase 1: embed first
self.llm.generate_response(messages=[
{"role": "system", "content": "extract"},
{"role": "user", "content": text},
], response_format={"type": "json_object"})
self.embedding_model.embed_batch([f"提取:{text[:20]}"], memory_action="add")
self.seq += 1
entry_id = f"m{self.seq}"
bucket = POINT_LOG.setdefault(self.collection, {})
bucket[entry_id] = {"data": f"提取:{text[:20]}", "user_id": user_id, **(metadata or {})}
return {"results": [{"id": entry_id, "memory": f"提取:{text[:20]}", "event": "ADD"}]}
def search(self, query, top_k=20, filters=None, **kwargs):
if not filters or not any(k in filters for k in ("user_id", "agent_id", "run_id")):
raise ValueError("filters must contain at least one of: user_id, agent_id, run_id")
self.embedding_model.embed(query, memory_action="search") # and on search
bucket = POINT_LOG.setdefault(self.collection, {})
return {"results": [{"id": k, "memory": v["data"]}
for k, v in bucket.items()
if v.get("user_id") == filters["user_id"] and query[:2] in v["data"]][:top_k]}
def _sys_modules():
import sys as _sys
return _sys.modules
def _make_ledger(path):
conn = sqlite3.connect(str(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()
@pytest.fixture
def fake_mem0(monkeypatch, tmp_path):
ledger_path = tmp_path / "ledger.sqlite3"
_make_ledger(ledger_path)
POINT_LOG.clear()
FakeMemory.instances.clear()
monkeypatch.setitem(sys.modules, "mem0", types.ModuleType("mem0"))
monkeypatch.setitem(sys.modules, "qdrant_client", types.ModuleType("qdrant_client"))
sys.modules["mem0"].Memory = FakeMemory
sys.modules["mem0"]._TEST_EMBEDDER = FakeEmbedder()
return ledger_path
def backend_for(tmp_path, fake_mem0, *, embedder=None, **overrides):
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(fake_mem0),
"embed_cap": 10,
"llm_cap": 10,
}
config.update(overrides)
# each backend wraps a fresh embedder, exactly like real from_config instances
sys.modules["mem0"]._TEST_EMBEDDER = embedder or FakeEmbedder()
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"]
assert payload["user_id"] == "cybergril" # scoped records, searchable
hits = await backend.search("钥匙")
assert hits and hits[0]["memory"].startswith("提取:")
await backend.aclose()
assert backend._client.closed
store.close()
async def test_search_debits_exactly_one_embed_item(tmp_path, fake_mem0):
backend = backend_for(tmp_path, fake_mem0)
guard = EmbedLedgerGuard(fake_mem0, cap=10)
rows = [{"message_id": 1, "turn_id": "t", "role": "user", "text": "一句话"}]
await backend.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
"input_sha256": "x"}, "g1")
spent = guard.used()
await backend.search("一句") # the old adapter double-debited this path
assert guard.used() == spent + 1
await backend.search("一句")
assert guard.used() == spent + 2
async def test_budget_guards_fail_closed_before_any_real_work(tmp_path, fake_mem0):
backend = backend_for(tmp_path, fake_mem0, embed_cap=0, llm_cap=0) # nothing approved
rows = [{"message_id": 1, "turn_id": "t", "role": "user", "text": "一句话"}]
anchor = {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]], "input_sha256": "x"}
with pytest.raises(RuntimeError, match="semantic_embed_budget_not_approved"):
await backend.add_group(rows, anchor, "g0")
memory = FakeMemory.instances[-1][0]
assert not memory.llm.calls # refused BEFORE the extraction LLM was touched
with pytest.raises(RuntimeError):
await backend.search("任何查询")
# llm-only exhaustion: embed budget present, extraction budget empty
backend2 = backend_for(tmp_path, fake_mem0, embed_cap=10, llm_cap=0)
with pytest.raises(RuntimeError, match="semantic_llm_budget_not_approved"):
await backend2.add_group(rows, anchor, "g0b")
# and with caps, exhausting them refuses further work
backend3 = backend_for(tmp_path, fake_mem0, embed_cap=4, llm_cap=4)
await backend3.add_group(rows, anchor, "g1") # 1 search embed + 1 batch item + 1 llm
await backend3.add_group(rows, anchor, "g2")
with pytest.raises(RuntimeError):
await backend3.add_group(rows, anchor, "g3")
async def test_reservation_releases_on_failure_and_never_overspends(tmp_path, fake_mem0):
# a failing embedder must release its hold: the ledger counts nothing
ledger_path = tmp_path / "flaky-ledger.sqlite3"
_make_ledger(ledger_path)
guard = EmbedLedgerGuard(ledger_path, cap=3)
class FlakyEmbedder(FakeEmbedder):
def embed(self, text, memory_action=None):
if "炸" in text:
raise OSError("controlled embed failure")
return super().embed(text, memory_action)
sys.modules["mem0"]._TEST_EMBEDDER = FlakyEmbedder()
backend = backend_for(tmp_path, fake_mem0, embedder=sys.modules["mem0"]._TEST_EMBEDDER,
ledger_path=str(ledger_path), embed_cap=3, llm_cap=3)
rows = [{"message_id": 1, "turn_id": "t", "role": "user", "text": "炸掉"}]
with pytest.raises(OSError):
await backend.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
"input_sha256": "x"}, "boom")
assert guard.used() == 0 # failure released every hold
# concurrent reserves cannot overshoot the cap
async def one_search(i: int):
try:
await backend.search(f"查询{i}")
return True
except RuntimeError:
return False
results = await asyncio.gather(*(one_search(i) for i in range(6)))
assert sum(results) >= 1 # some ran, and the ledger never overshot the cap
assert guard.used() <= 3 # hard cap held under concurrency
async def test_semantic_memory_end_to_end_with_fake_sdk(tmp_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"
"(1760000000000,'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("她确实记得的更早的事")
# HEL-874 verbatim readback: the archive row's original words ride along
assert "阳台的牵牛花开了" in block
store.close()
async def test_llm_ledger_guard_counts_extraction_calls(tmp_path):
guard = LlmLedgerGuard(tmp_path / "llm-ledger.sqlite3", cap=2)
guard.settle(guard.reserve(1, "extract"), "extract")
guard.settle(guard.reserve(1, "extract"), "extract")
assert guard.used() == 2
with pytest.raises(RuntimeError):
guard.reserve(1, "extract") # cap reached → refuse
guard_zero = LlmLedgerGuard(tmp_path / "llm-zero.sqlite3", cap=0)
with pytest.raises(RuntimeError, match="semantic_llm_budget_not_approved"):
guard_zero.reserve(1, "extract") # default 0 = never approved