132 lines
6.9 KiB
Python
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
|