Files
CyberGril/tests/test_mem0_backend.py
T
zonggongandmultica-agent 31970c4dd3 HEL-870 assembly: memory/persona/mood/life/proactive-contact joined into one runnable candidate on an additive R4 migration (no existing table or row replaced; no visual changes beyond mechanical proactive flag passthrough) - src/cybergril/memory/semantic.py productizes the audited Mem0 path per plan-next-HEL-869: input-side pre-grouping sends only public rows outbound (user rows always; companion rows only judged+acked with all-told candidates), every batch anchor turn_ids/turn_roles/input_sha256 equals that exact input set, and the read-time gate independently recomputes the fingerprint against the live archive and re-checks every companion row's share state, fail-closed rejecting no_payload/unknown_source/archive_missing/source_mismatch/hit_text_mismatch/anchor_turn_mismatch/role_text_conflict/companion_unshared with honest row_verbatim/row_anchor/batch provenance; main archive chat_message remains the ONLY permanent quote layer; sem_batch/sem_covered carry a lease-recovered idempotent job loop; EmbedLedgerGuard continues the single authoritative embed ledger and with embed_cap=0 (no approved budget) refuses every real embedding so the frozen 498/500 and 39/60 accounts and the HEL-835 gate stay untouched; fixed Mem0 2c81af3 is lazily imported with zero upstream fork, a deterministic stub backend exists for engineering verification and is never claimed semantic; MoodRuntime makes core.mood live (event impulses scaled by bazi strength sensitivity, heartbeat decay, state_snapshot persistence, restart recovery, model sees one word only); DyadRuntime makes the relationship ledger live (completed exchanges are rituals, daily regression, behavior hints stay instructions); life_activity adds start/continue/interrupt/resume/finish continuity across slots and restarts with chat-completion interrupts; reach_out implements the approved R4 scope from character.yaml (>=6h interval, hours 8-23, salience>=0.6, arousal>=0.5, intimacy>=0.4) with traceable already-public bases only, UNIQUE(basis) so restarts and duplicate tasks can never multiply messages, night suppression, absence-rule fail-closed, phi projection, and messages persisted as companion rows (proactive=1) fully synced on reopen; API health reports R4-assembly with per-capability honesty (voice/vision/weather/materials/backup stay false, semantic_memory names its backend); config/semantic.example.yaml sanitized config and docs/runbook-hel870.md (startup, locked deps, health, independent data dir, disable/rollback, experience chain list, budget request, R1-R7 seams); verification: 250 python tests pass on 3.11 venv + requirements.lock (61 new deterministic/fake-provider cases covering every gate negative, restart recovery, dedupe, red line and the full entry chain incl. reopen epoch sync), ruff clean, web typecheck + 42 vitest + build pass with the original author frontend untouched except the one-line proactive passthrough; dual-target requirement checks 4/4 pass vs origin/main and origin/agent/agent/de8874a3803b with handoff-HEL-870.json; zero real model calls, zero embeddings, zero outbound network this card; local unified-instance smoke on port 8360 (stub semantic) serves the built web and reports the life loop waiting; read-only server check recorded in the runbook (existing 8300-8350 services and voice ports observed, end-to-end voice availability NOT verified); not merged, not deployed, no production or shared services touched
Co-authored-by: multica-agent <github@multica.ai>
2026-10-26 21:03:42 +08:00

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()