Files
CyberGril/tests/test_readback_hel874.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

132 lines
6.9 KiB
Python

"""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