Skip to content
Python

fix(intention): preserve isolation scope in L7 recall and dedup (#81)

u7-u7514bf5c47bc2
diff --git a/hy_memory/pipelines/_retrieval/intention.py b/hy_memory/pipelines/_retrieval/intention.py
index 113545a..f82a0b7 100644
--- a/hy_memory/pipelines/_retrieval/intention.py
+++ b/hy_memory/pipelines/_retrieval/intention.py
@@ -10,8 +10,8 @@ HY Memory - Intention (L7) 召回 + 惰性过期转换 + 复本判定
节点的 layer 改写为 l2_fact(持久化 update_payload),并从意图结果里剔除——
过期意图变成普通历史事实,下次走 normal 召回;
- **判重(重点)**:对幸存意图按下述规则分组,同组只留一条本次返回:
- 同 user_id + 同一天 memory_at + valid_until 相同(都为 None 或都相等)
- + cosine 相似度 ≥ MEMORY_DEDUP_THRESHOLD (默认 0.95)
+ 同 user_id + 同 agent_id + 同一天 memory_at + valid_until 相同
+ (都为 None 或都相等)+ cosine 相似度 ≥ MEMORY_DEDUP_THRESHOLD (默认 0.95)
→ 视为同一个意图的复本(extractor 复读机 / 多 session 重复表达)。
保留组里 `gmt_created` 最早的(老的先来先得,稳定 id);
- **异步删库**:其余复本 fire-and-forget 从 VDB 删除,不影响本次调用 RT。
@@ -112,11 +112,19 @@ def _partition_duplicates(
if len(hits) < 2:
return list(hits), []
- # 1. 按 (memory_at day, valid_until day) 分桶
- buckets: Dict[Tuple[Any, Any], List[int]] = {}
+ # 1. 按 (user_id, agent_id, memory_at day, valid_until day) 分桶。
+ # user_id / agent_id 必须进键:多用户检索时不同用户的同一天意图可能
+ # 文本、日期都相同,但它们是两个独立的租户数据;只按日期分桶会把其中
+ # 一个判成复本并硬删除。
+ buckets: Dict[Tuple[Any, Any, Any, Any], List[int]] = {}
for idx, h in enumerate(hits):
node = h.get("node")
- key = (_memory_at_day(node), _valid_until_key(node))
+ key = (
+ getattr(node, "user_id", None) if node else None,
+ getattr(node, "agent_id", None) if node else None,
+ _memory_at_day(node),
+ _valid_until_key(node),
+ )
buckets.setdefault(key, []).append(idx)
def _gmt(h: Dict[str, Any]) -> float:
@@ -229,6 +237,8 @@ async def recall_intentions(
*,
user_ids: Optional[List[str]] = None,
agent_ids: Optional[List[str]] = None,
+ isolation_key: str = "",
+ isolation_keys: Optional[List[str]] = None,
limit: int = 10,
now: Optional[datetime] = None,
stats_out: Optional[Dict[str, Any]] = None,
@@ -243,6 +253,9 @@ async def recall_intentions(
query_embedding: 查询向量
user_ids: 用户隔离(单/多用户)
agent_ids: 可选 agent 过滤
+ isolation_key / isolation_keys: 精确的 (user, agent, session) 隔离键。
+ 与其它召回通道一致:调用方在按 session 过滤时只生成这两项,
+ user_ids / agent_ids 为 None;不把它们透传下去就会退化成全库检索。
limit: 召回上限(返回给上游的条数)
now: 当前时间(默认 datetime.now(),测试可注入)
stats_out: 可选 dict,本函数把分段耗时与数量**原地回填**进去,供调用方落
@@ -283,6 +296,8 @@ async def recall_intentions(
query_embedding=query_embedding,
user_ids=user_ids or None,
agent_ids=agent_ids,
+ isolation_key=isolation_key,
+ isolation_keys=isolation_keys,
layers=[MemoryLayer.L7_INTENTION],
limit=fetch_limit,
score_threshold=0.0, # intention 不设语义门槛
diff --git a/hy_memory/pipelines/reader_base.py b/hy_memory/pipelines/reader_base.py
index cc2d541..5a5180b 100644
--- a/hy_memory/pipelines/reader_base.py
+++ b/hy_memory/pipelines/reader_base.py
@@ -1840,6 +1840,8 @@ class BaseReadPipeline(ReadPipeline):
query_embedding,
user_ids=isolation_params.get("user_ids"),
agent_ids=isolation_params.get("agent_ids"),
+ isolation_key=isolation_params.get("isolation_key") or "",
+ isolation_keys=isolation_params.get("isolation_keys"),
limit=request.intention_limit,
stats_out=_intent_stats,
include_sources=request.include_sources,
diff --git a/tests/test_intention_dedup.py b/tests/test_intention_dedup.py
index e0d748b..89d5bce 100644
--- a/tests/test_intention_dedup.py
+++ b/tests/test_intention_dedup.py
@@ -97,6 +97,8 @@ class _FakeVectorStore:
self,
*,
query_embedding=None,
+ isolation_key: str = "",
+ isolation_keys=None,
user_ids=None,
agent_ids=None,
layers=None,
@@ -110,6 +112,12 @@ class _FakeVectorStore:
for nid, n in self._nodes.items():
if layers and n.layer not in layers:
continue
+ if isolation_keys:
+ if n.get_isolation_key() not in isolation_keys:
+ continue
+ elif isolation_key:
+ if n.get_isolation_key() != isolation_key:
+ continue
if user_ids and n.user_id not in user_ids:
continue
if agent_ids and n.agent_id not in agent_ids:
@@ -256,6 +264,50 @@ def test_partition_duplicates_different_valid_until_keeps_both():
assert delete_ids == []
+def test_partition_duplicates_different_users_keeps_both():
+ """不同用户的同一天、同意图文本不能被判为复本(更不能硬删他人数据)。"""
+ same_ma = datetime(2025, 8, 6)
+ same_vu = datetime(2025, 8, 8)
+ alice = _make_intention_node(
+ "用户明天要开会", user_id="alice", memory_at=same_ma, valid_until=same_vu,
+ gmt_created=datetime(2025, 8, 1),
+ )
+ bob = _make_intention_node(
+ "用户明天要开会", user_id="bob", memory_at=same_ma, valid_until=same_vu,
+ gmt_created=datetime(2025, 8, 2),
+ )
+ hits = [
+ {"node_id": alice.node_id, "node": alice, "score": 0.9},
+ {"node_id": bob.node_id, "node": bob, "score": 0.9},
+ ]
+ kept, delete_ids = _partition_duplicates(hits, threshold=0.95)
+ assert {h["node_id"] for h in kept} == {alice.node_id, bob.node_id}
+ assert delete_ids == []
+
+
+def test_partition_duplicates_different_agents_keeps_both():
+ """同 user 下不同 agent 的意图也属于不同作用域,不能互相覆盖。"""
+ same_ma = datetime(2025, 8, 6)
+ same_vu = datetime(2025, 8, 8)
+ a1 = _make_intention_node(
+ "用户明天要开会", user_id="u1", agent_id="agent_a",
+ memory_at=same_ma, valid_until=same_vu,
+ gmt_created=datetime(2025, 8, 1),
+ )
+ a2 = _make_intention_node(
+ "用户明天要开会", user_id="u1", agent_id="agent_b",
+ memory_at=same_ma, valid_until=same_vu,
+ gmt_created=datetime(2025, 8, 2),
+ )
+ hits = [
+ {"node_id": a1.node_id, "node": a1, "score": 0.9},
+ {"node_id": a2.node_id, "node": a2, "score": 0.9},
+ ]
+ kept, delete_ids = _partition_duplicates(hits, threshold=0.95)
+ assert {h["node_id"] for h in kept} == {a1.node_id, a2.node_id}
+ assert delete_ids == []
+
+
def test_partition_duplicates_low_cosine_keeps_both():
"""content 不同(cosine 低)→ 不判重,即使 memory_at / valid_until 同。"""
same_ma = datetime(2025, 8, 6)
@@ -368,6 +420,61 @@ def test_recall_intentions_dedup_and_async_delete_end_to_end():
assert set(vs.delete_calls) == {p_ids[1], p_ids[2]}
+def test_recall_intentions_forwards_isolation_keys():
+ """session 过滤路径只生成 isolation_keys;intention 必须原样透传给 VDB。"""
+
+ class _RecordingStore:
+ def __init__(self):
+ self.kwargs = None
+
+ async def search(self, **kwargs):
+ self.kwargs = kwargs
+ return []
+
+ vs = _RecordingStore()
+ _run(recall_intentions(
+ vs, [0.0], isolation_keys=["u1::a1::s1"], limit=5,
+ ))
+ assert vs.kwargs["isolation_keys"] == ["u1::a1::s1"]
+ assert vs.kwargs["user_ids"] is None
+ assert vs.kwargs["agent_ids"] is None
+
+ vs2 = _RecordingStore()
+ _run(recall_intentions(
+ vs2, [0.0], isolation_key="u1::a1::s1", limit=5,
+ ))
+ assert vs2.kwargs["isolation_key"] == "u1::a1::s1"
+
+
+def test_recall_intentions_isolation_keys_prevent_cross_user_leak():
+ """带 isolation_keys 时只能召回该作用域的意图,且不会误删其他用户。"""
+ vs = _FakeVectorStore()
+ emb = _FakeEmbedByHash(dims=32)
+ ma = datetime(2025, 8, 6)
+ vu = datetime(2025, 8, 8)
+
+ alice = _make_intention_node(
+ "用户明天要开会", user_id="u1", dims=32,
+ memory_at=ma, valid_until=vu,
+ embedding=emb.vec_for("用户明天要开会"),
+ )
+ bob = _make_intention_node(
+ "用户明天要开会", user_id="u2", dims=32,
+ memory_at=ma, valid_until=vu,
+ embedding=emb.vec_for("用户明天要开会"),
+ )
+ _run(vs.upsert(alice))
+ _run(vs.upsert(bob))
+
+ hits = _run(recall_intentions(
+ vs, emb.vec_for("用户明天要开会"),
+ isolation_keys=["u1::a1::s1"], limit=10,
+ now=datetime(2025, 8, 6, 12, 0),
+ ))
+ assert {h["node_id"] for h in hits} == {alice.node_id}
+ assert vs.delete_calls == []
+
+
def test_recall_intentions_different_valid_until_all_returned():
"""两个 memory_at 相同、valid_until 不同的意图 → 都召回,都不删。"""
vs = _FakeVectorStore()