问题聚类的类 id 由成员定,「不是口径」按成员记

类的 id 以前按种子问题的原文算:种子是一类里最新的那一句,时间相同的几句读出来先后不定、来了一句新问法种子就换了,
两次读取 id 就变,认证队列里点过的「不是口径」随之失效。

- app/chat/questions:Question.key 是稳定的 id(问数据的那一轮 turn:…、别的探索运行 run:…);QuestionCluster.id 是成员的
  id 排好之后取的哈希,同一批问题两次读取一样;recent_questions 同一时刻问的几句按 id 排出先后,不靠库里读出来的先后。
- ledger/certify:问题聚类那一项的 id 用类的 id;「不是口径」时服务端按同一批问题重新聚一次,记下这一类现在的成员(不收
  客户端传的成员,找不到、取向量出错就只记 id);之后这一类过半的成员是某一次记下的,就照样不进队——来了几句新问法 id
  会变,按成员认不失效。以前按种子原文算的 id 记的照样认;记的成员读不懂的照实忽略。
- api/ledger:dismiss 把当前身份交给 certify(重新聚类要按这个人读得到的问题)。

测试:tests/test_ledger_certify.py 新增三条——同一批问题换个先后读,种子不同、类的 id 一样;点过「不是口径」之后换个说法
再问一句,成员变了照样不进队,读不懂的老记录不报错也不藏;按旧的种子原文 id 记的照样生效。把 id 改回按种子原文、把按
成员认去掉再跑,各自的用例失败。
This commit is contained in:
ThearchyHelios
2026-10-17 12:15:07 +08:00
parent 72d9d2770c
commit 1b31c49a00
4 changed files with 151 additions and 8 deletions
+75
View File
@@ -347,3 +347,78 @@ async def test_a_not_a_caliber_recorded_by_the_old_text_id_still_counts(client,
res = await client.post("/api/ledger/certify-queue/dismiss", json={"item_id": old, "kind": "shape"})
assert res.status_code == 200
assert shape_items((await client.get("/api/ledger/certify-queue")).json()) == []
# --------------------------------------------------------------------------
# 问题聚类的 id 由成员定;「不是口径」按成员记
# --------------------------------------------------------------------------
async def test_cluster_ids_come_from_the_members_and_do_not_change_between_reads():
from datetime import datetime, timezone
from app.chat.questions import Question, QuestionCluster, cluster_questions
at = datetime(2026, 10, 1, tzinfo=timezone.utc)
# 同一时刻问的三句(时间相同,库里读出来的先后不定):两次读出来先后不同,种子就不同,类的 id 仍然一样
texts = ["仓库乙本月出库多少件", "仓库乙本月出库多少件?", "仓库乙本月出库了多少件"]
questions = [Question(text=t, at=at, turn_id=f"t-{i}") for i, t in enumerate(texts)]
first = await cluster_questions(questions)
again = await cluster_questions(list(reversed(questions)))
assert len(first) == 1 and len(again) == 1 and first[0].members[0].text != again[0].members[0].text
assert [c.id for c in first] == [c.id for c in again]
assert first[0].keys == ["turn:t-0", "turn:t-1", "turn:t-2"]
assert QuestionCluster(members=list(reversed(questions))).id == first[0].id
# 成员不同,id 就不同
assert QuestionCluster(members=questions[:2]).id != first[0].id
async def test_not_a_caliber_on_a_cluster_survives_a_new_member_and_old_records_still_count(client, monkeypatch):
import uuid
from app.auth.audit import record
text = f"仓库甲本周入库了多少件({uuid.uuid4().hex[:6]})"
a, b = await conversation(), await conversation()
asked = {(await ask(monkeypatch, conv, weekly(*WEEKS[0]), question=text)).id for conv in (a, a, b)}
async def mine() -> list[dict]:
body = (await client.get("/api/ledger/certify-queue")).json()
return [i for i in body["items"] if i["kind"] == "cluster" and asked & {t["run_id"] for t in i["turns"]}]
[item] = await mine()
[again] = await mine()
assert item["id"] == again["id"], "同一批问题两次读取,类的 id 一样"
# 读不懂的老记录照实忽略:不报错、不把这一类藏起来
async with SessionLocal() as session:
await record(session, certify.DISMISS_ACTION, certify.SUBJECT_KIND, "cluster:0000000000000000",
data={"kind": "cluster", "members": "看不懂"})
await session.commit()
assert [i["id"] for i in await mine()] == [item["id"]]
res = await client.post("/api/ledger/certify-queue/dismiss", json={"item_id": item["id"], "kind": "cluster"})
assert res.status_code == 200
assert await mine() == []
# 再换个说法问一句:成员多了一个、种子换成了新的这句,类的 id 变了;过半的成员是点「不是口径」时记下的,照样不进队
newer = await ask(monkeypatch, b, weekly(*WEEKS[1]), question=text + "?")
asked.add(newer.id)
assert await mine() == []
async def test_a_not_a_caliber_recorded_by_the_old_seed_text_id_still_counts(client, monkeypatch):
import uuid
from app.auth.audit import record
text = f"仓库丙上周退货多少件({uuid.uuid4().hex[:6]})"
a, b = await conversation(), await conversation()
for conv in (a, a, b):
await ask(monkeypatch, conv, weekly(*WEEKS[0]), question=text)
body = (await client.get("/api/ledger/certify-queue")).json()
assert [i for i in body["items"] if i["kind"] == "cluster" and i["question"] == text]
async with SessionLocal() as session:
await record(session, certify.DISMISS_ACTION, certify.SUBJECT_KIND, certify.item_id("cluster", text),
data={"kind": "cluster"})
await session.commit()
body = (await client.get("/api/ledger/certify-queue")).json()
assert not [i for i in body["items"] if i["kind"] == "cluster" and i["question"] == text]