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>
This commit is contained in:
co-authored by
multica-agent
parent
31970c4dd3
commit
e978e878df
+165
-62
@@ -1,22 +1,28 @@
|
||||
"""Mem0 real-adapter wiring verified against a fake SDK: config, anchors, budget guard.
|
||||
"""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
|
||||
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.
|
||||
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, Mem0Backend, SemanticMemory
|
||||
from cybergril.memory.semantic import EmbedLedgerGuard, LlmLedgerGuard, 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] = {}
|
||||
# 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:
|
||||
@@ -26,24 +32,25 @@ class _Point:
|
||||
|
||||
|
||||
class FakeQdrantClient:
|
||||
def __init__(self, path=None):
|
||||
self.path = path
|
||||
def __init__(self):
|
||||
self.closed = False
|
||||
|
||||
def retrieve(self, collection_name, ids, with_payload=True):
|
||||
bucket = POINTS.setdefault(collection_name, {})
|
||||
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 __init__(self, ledger=None):
|
||||
self.ledger = ledger
|
||||
|
||||
def embed(self, text, memory_action=None):
|
||||
return [0.0]
|
||||
|
||||
@@ -51,74 +58,103 @@ class FakeEmbedder:
|
||||
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):
|
||||
import sys as _sys
|
||||
|
||||
instance = cls(_sys.modules["mem0"]._TEST_EMBEDDER,
|
||||
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
|
||||
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 = POINTS.setdefault(self.collection, {})
|
||||
bucket[entry_id] = {"data": text, **(metadata or {})}
|
||||
return {"results": [{"id": entry_id, "memory": text, "event": "ADD"}]}
|
||||
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, limit=5):
|
||||
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 = POINTS.setdefault(self.collection, {})
|
||||
bucket = POINT_LOG.setdefault(self.collection, {})
|
||||
return {"results": [{"id": k, "memory": v["data"]}
|
||||
for k, v in bucket.items() if query[:2] in v["data"]][:limit]}
|
||||
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"
|
||||
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
|
||||
_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, **overrides):
|
||||
guard, embedder, ledger_path = fake_mem0
|
||||
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(ledger_path),
|
||||
"ledger_path": str(fake_mem0),
|
||||
"embed_cap": 10,
|
||||
"llm_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"]))
|
||||
# each backend wraps a fresh embedder, exactly like real from_config instances
|
||||
sys.modules["mem0"]._TEST_EMBEDDER = embedder or FakeEmbedder()
|
||||
return Mem0Backend(config)
|
||||
|
||||
|
||||
@@ -136,40 +172,107 @@ async def test_add_carries_anchor_and_search_roundtrips(tmp_path, fake_mem0):
|
||||
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"] == "钥匙在抽屉"
|
||||
assert hits and hits[0]["memory"].startswith("提取:")
|
||||
await backend.aclose()
|
||||
assert backend._client.closed
|
||||
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
|
||||
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("任何查询")
|
||||
# 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")
|
||||
# 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 backend2.add_group(rows, {"turn_ids": ["t"], "turn_roles": [["t", "user", 1]],
|
||||
"input_sha256": "x"}, "g3")
|
||||
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):
|
||||
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')")
|
||||
"(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
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
"""HEL-874 bounded verbatim readback proven on the REAL provider input.
|
||||
|
||||
Nothing here asserts on logs or intermediate returns: every case captures the
|
||||
exact message list handed to the model provider's ``stream()`` through the real
|
||||
API chat path, then checks the original words, role and time of a past turn
|
||||
that has ALREADY left the recent-conversation window (21 filler turns push the
|
||||
probed turn out of the last-20 history the model sees for free).
|
||||
"""
|
||||
import json
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from test_api import CREATE, events, turn
|
||||
from test_r4_api import r4_app
|
||||
|
||||
FILLER_TURNS = 21 # the probed turn must be outside the recent-20 window
|
||||
|
||||
DETAIL_TURN = "t-detail-hel874"
|
||||
DETAIL_TEXT = "我的工牌编号是A8823,蓝色挂绳那一张"
|
||||
DETAIL_GIST = "说到过工牌" # deliberately loses the number and the color
|
||||
PRIVATE_TURN = "t-private-hel874"
|
||||
PRIVATE_TEXT = "陪伴者私记紫铜风铃关键词只属于她自己的日记"
|
||||
|
||||
|
||||
def seed_turn(store, turn_id, user_text, companion_text, ts):
|
||||
with store.conn:
|
||||
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
||||
"(?,'user',?,?,'sent')", (ts, user_text, turn_id))
|
||||
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
||||
"(?,'companion',?,?,'sent')", (ts + 1, companion_text, turn_id))
|
||||
|
||||
|
||||
def push_out_of_recent_window(client, app):
|
||||
"""Filler exchanges so the probed turn is older than the recent-20 window."""
|
||||
store = app.state.store
|
||||
for i in range(FILLER_TURNS):
|
||||
with store.conn:
|
||||
tid = f"t-filler-{i}"
|
||||
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
||||
"(?,'user',?,?,'sent')", (1_700_000_000_000 + i, f"闲聊填句第{i}号", tid))
|
||||
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES"
|
||||
"(?,'companion',?,?,'sent')", (1_700_000_000_001 + i, f"填充回应第{i}号", tid))
|
||||
# one live chat turn keeps the window realistic (it lands in the recent-20)
|
||||
turn(client, "今天先聊到这")
|
||||
|
||||
|
||||
def _system_text(provider_call):
|
||||
return "\n".join(m.content for m in provider_call if m.role == "system")
|
||||
|
||||
|
||||
def test_provider_input_carries_verbatim_original_from_fts(tmp_path):
|
||||
"""FTS channel: detail absent from the gist, original quoted into the model input."""
|
||||
app, provider = r4_app(tmp_path, semantic="off")
|
||||
with TestClient(app) as client:
|
||||
client.post("/api/companion", json=CREATE)
|
||||
store = app.state.store
|
||||
seed_turn(store, DETAIL_TURN, DETAIL_TEXT, "好,我记住了你的工牌", 1_760_000_000_000)
|
||||
app.state.memories.record_turn(DETAIL_TURN)
|
||||
for row in store.query("SELECT * FROM mem_episodic WHERE turn_id=?", (DETAIL_TURN,)):
|
||||
store.set_gist(row["id"], DETAIL_GIST if row["role"] == "user" else row["raw"],
|
||||
{"v": 0.1, "a": 0.1, "d": 0.1})
|
||||
push_out_of_recent_window(client, app)
|
||||
|
||||
response = turn(client, "我的工牌编号是多少来着")
|
||||
assert [e["t"] for e in events(response)][-1] == "chat.final"
|
||||
sent = _system_text(provider.calls[-1])
|
||||
# the recent window no longer carries it for free
|
||||
assert all(DETAIL_TEXT not in m.content for m in provider.calls[-1]
|
||||
if m.role in ("user", "assistant"))
|
||||
# yet the model input holds the ORIGINAL words plus role and time
|
||||
assert "A8823" in sent and "蓝色挂绳" in sent
|
||||
assert "工牌编号是A8823" in sent # verbatim contiguous original, not gist
|
||||
assert '"role": "user"' in sent
|
||||
assert "月" in sent and "日" in sent # the when stamp travels with the quote
|
||||
|
||||
|
||||
async def test_provider_input_carries_verbatim_original_from_semantic(tmp_path):
|
||||
"""Semantic channel: a chain-verified hit quotes its anchored archive rows."""
|
||||
app, provider = r4_app(tmp_path, semantic="stub")
|
||||
with TestClient(app) as client:
|
||||
client.post("/api/companion", json=CREATE)
|
||||
store = app.state.store
|
||||
seed_turn(store, DETAIL_TURN, "阳台的蓝色牵牛花是朋友送的那盆", "下次开花拍给你看", 1_761_000_000_000)
|
||||
semantic = app.state.semantic
|
||||
semantic.enqueue_uncovered()
|
||||
await semantic.tick()
|
||||
assert store.query_one("SELECT state FROM sem_batch")["state"] == "done"
|
||||
# chain-resolved originals now carry the archive text and timestamp
|
||||
rows = semantic.public_rows_for_turn(DETAIL_TURN) # exactly the enqueued input set
|
||||
chain = semantic.chain_resolve({"id": "stub-1",
|
||||
"memory": "\n".join(r["text"] for r in rows)})
|
||||
assert chain["resolved"]
|
||||
assert all(o["text"] and o["ts"] for o in chain["originals"])
|
||||
|
||||
push_out_of_recent_window(client, app)
|
||||
turn(client, "蓝色牵牛花 是谁送的来着")
|
||||
sent = _system_text(provider.calls[-1])
|
||||
assert "阳台的蓝色牵牛花是朋友送的那盆" in sent # verbatim original quoted
|
||||
assert '"role": "user"' in sent and "月" in sent and "日" in sent
|
||||
|
||||
|
||||
async def test_unshared_companion_row_never_reaches_provider_input(tmp_path):
|
||||
"""AM-001 negative: an unshared companion row's original never leaks, FTS or semantic."""
|
||||
app, provider = r4_app(tmp_path, semantic="stub")
|
||||
with TestClient(app) as client:
|
||||
client.post("/api/companion", json=CREATE)
|
||||
store = app.state.store
|
||||
seed_turn(store, PRIVATE_TURN, "你今天想我了吗", PRIVATE_TEXT, 1_762_000_000_000)
|
||||
# episodic side: row exists with a gist, but her row was never shared
|
||||
app.state.memories.record_turn(PRIVATE_TURN)
|
||||
for row in store.query("SELECT * FROM mem_episodic WHERE turn_id=?", (PRIVATE_TURN,)):
|
||||
store.set_gist(row["id"], PRIVATE_TEXT, {"v": 0.0, "a": 0.0, "d": 0.0})
|
||||
# semantic side: the enqueuer batches only the USER row of that turn —
|
||||
# her unshared companion row must never join any extraction input set
|
||||
semantic = app.state.semantic
|
||||
assert semantic.enqueue_uncovered() == 1
|
||||
batch = store.query_one("SELECT * FROM sem_batch")
|
||||
anchor_json = json.loads(batch["anchor_json"])
|
||||
anchored_ids = {tr[2] for tr in anchor_json["turn_roles"]}
|
||||
companion_id = store.query_one(
|
||||
"SELECT id FROM chat_message WHERE turn_id=? AND role='companion'",
|
||||
(PRIVATE_TURN,))["id"]
|
||||
assert companion_id not in anchored_ids
|
||||
await semantic.tick()
|
||||
entry = semantic.backend.payload(
|
||||
json.loads(store.query_one("SELECT entry_ids FROM sem_batch")["entry_ids"])[0])
|
||||
assert PRIVATE_TEXT not in json.dumps(entry, ensure_ascii=False)
|
||||
|
||||
push_out_of_recent_window(client, app)
|
||||
turn(client, "紫铜风铃 关键词是什么")
|
||||
everything = "\n".join(m.content for m in provider.calls[-1])
|
||||
assert PRIVATE_TEXT not in everything # her private original never left the server
|
||||
Reference in New Issue
Block a user