Python
fix(coding): apply agent and session scope to coding retrieval (#87)
u7-u74bc918af7a0a
diff --git a/hy_memory/client.py b/hy_memory/client.pyindex be454b7..a81c35d 100644--- a/hy_memory/client.py+++ b/hy_memory/client.py@@ -1213,6 +1213,8 @@ class HyMemoryClient:target_query: str,*,user_ids: List[str],+ agent_ids: Optional[List[str]] = None,+ session_ids: Optional[List[str]] = None,workspace_id: Optional[str],branch: Optional[str],limit: int,@@ -1346,6 +1348,7 @@ class HyMemoryClient:try:fact_hits = await _store.search_facts(embedding, user_id=primary_user,+ agent_ids=agent_ids, session_ids=session_ids,workspace_id=workspace_id, branch=branch,top=max(limit, 5),# 词面通道 + 把下面那道闸门下移到重排之前:先砍掉过不了闸的候选再重排,@@ -1425,6 +1428,8 @@ class HyMemoryClient:hits = await _store.search_by_query_embedding(embedding,user_id=primary_user,+ agent_ids=agent_ids,+ session_ids=session_ids,workspace_id=workspace_id,branch=branch,top=max(limit * 4, 8), # 过采样为 dedup 留余量@@ -3401,6 +3406,8 @@ class HyMemoryClient:return await self._search_coding(effective_target_query,user_ids=effective_user_ids,+ agent_ids=effective_agent_ids,+ session_ids=effective_session_ids,workspace_id=workspace_id,branch=branch,limit=limit,diff --git a/hy_memory/coding/store.py b/hy_memory/coding/store.pyindex 9124601..0b0d232 100644--- a/hy_memory/coding/store.py+++ b/hy_memory/coding/store.py@@ -209,6 +209,14 @@ def _scope_where(return sql, params+def _in_clause(column: str, values: Optional[List[str]]) -> Tuple[str, List[Any]]:+ """SQL ``column IN (...)`` + parameters; empty values mean no condition."""+ if not values:+ return "", []+ placeholders = ",".join("?" for _ in values)+ return f"{column} IN ({placeholders})", list(values)++def _scope_matches(row_workspace_id: Optional[str],row_branch: Optional[str],@@ -683,6 +691,8 @@ class CodingMemoryStore:*,agent_id: Optional[str] = None,session_id: Optional[str] = None,+ agent_ids: Optional[List[str]] = None,+ session_ids: Optional[List[str]] = None,workspace_id: Optional[str] = None,branch: Optional[str] = None,top: int = 40,@@ -700,21 +710,27 @@ class CodingMemoryStore:},...]- agent_id / session_id 是写授权作用域,传入时精确匹配;workspace_id / branch- 的规则见 _scope_where。不传任何作用域则维持纯语义召回;传了则只保留 SQLite- 里落在作用域内的经验,并按选择率放大向量取数,避免"先取 top-N 再过滤"把- 召回吃掉。+ agent_id / session_id(或 agent_ids / session_ids)是写授权作用域,+ 传入时精确匹配;workspace_id / branch 的规则见 _scope_where。+ 不传任何作用域则维持纯语义召回;传了则只保留 SQLite 里落在作用域内的经验,+ 并按选择率放大向量取数,避免"先取 top-N 再过滤"把召回吃掉。"""if self._vec is None:return []+ effective_agent_ids = list(agent_ids) if agent_ids else (+ [agent_id] if agent_id else []+ )+ effective_session_ids = list(session_ids) if session_ids else (+ [session_id] if session_id else []+ )allowed: Optional[Set[str]] = None- if agent_id or session_id or workspace_id:+ if effective_agent_ids or effective_session_ids or workspace_id:allowed, total = await _run_in_pool(self._authority_scoped_experience_ids_sync,user_id,- agent_id,- session_id,+ effective_agent_ids,+ effective_session_ids,workspace_id,branch,)@@ -799,6 +815,8 @@ class CodingMemoryStore:*,agent_id: Optional[str] = None,session_id: Optional[str] = None,+ agent_ids: Optional[List[str]] = None,+ session_ids: Optional[List[str]] = None,workspace_id: Optional[str] = None,branch: Optional[str] = None,top: int = 5,@@ -812,9 +830,9 @@ class CodingMemoryStore:SUPERSEDE 时向量行已被删掉,理论上召不到 superseded;这里再按 SQLite 的status 过一道,避免两边不一致时把过期结论放出去。- agent_id / session_id 是写授权作用域,传入时精确匹配;workspace_id / branch- 的规则见 _scope_where。不传任何作用域则不过滤,传了按 SQLite 里的值筛,并- 按选择率放大向量取数。+ agent_id / session_id(或 agent_ids / session_ids)是写授权作用域,+ 传入时精确匹配;workspace_id / branch 的规则见 _scope_where。+ 不传任何作用域则不过滤,传了按 SQLite 里的值筛,并按选择率放大向量取数。向量库按 top 的 4 倍过采样:向量行和 SQLite 行不是一个事务里写的,中间挂掉就会留下"向量还在、SQLite 没了"的孤儿,软删时删向量失败也会留下 superseded@@ -830,20 +848,27 @@ class CodingMemoryStore:if self._fact_vec is None:return []+ effective_agent_ids = list(agent_ids) if agent_ids else (+ [agent_id] if agent_id else []+ )+ effective_session_ids = list(session_ids) if session_ids else (+ [session_id] if session_id else []+ )fetch = top * 4- if agent_id or session_id or workspace_id:+ if effective_agent_ids or effective_session_ids or workspace_id:scoped, total = await _run_in_pool(self._count_active_facts_sync,user_id,- agent_id,- session_id,+ effective_agent_ids,+ effective_session_ids,workspace_id,branch,)if not scoped:logger.info(f"[coding-store] no active fact in scope: "- f"agent_id={agent_id!r} session_id={session_id!r} "+ f"agent_ids={effective_agent_ids!r} "+ f"session_ids={effective_session_ids!r} "f"workspace_id={workspace_id!r} branch={branch!r} "f"(user has {total}); skip fact vdb search")@@ -880,8 +905,8 @@ class CodingMemoryStore:alive = [(f, scores[f.fact_id]) for f in factsif f.status == "active"- and (not agent_id or f.agent_id == agent_id)- and (not session_id or f.session_id == session_id)+ and (not effective_agent_ids or f.agent_id in effective_agent_ids)+ and (not effective_session_ids or f.session_id in effective_session_ids)and _scope_matches(f.workspace_id, f.branch, workspace_id, branch)]kept = [(f, s) for f, s in alive if s >= min_score]@@ -1083,22 +1108,25 @@ class CodingMemoryStore:def _authority_scoped_experience_ids_sync(self,user_id: str,- agent_id: Optional[str],- session_id: Optional[str],+ agent_ids: Optional[List[str]],+ session_ids: Optional[List[str]],workspace_id: Optional[str],branch: Optional[str],) -> Tuple[Set[str], int]:"""返回 (落在授权作用域内的 experience_id 集合, 该 user 的经验总数)。一次查询同时拿到过滤结果和选择率分母:前者供向量命中做交集,后者定放大倍数。+ agent_ids / session_ids 为空表示该维度不筛;非空按 IN 精确匹配。"""scope_sql, scope_params = _scope_where(workspace_id, branch)- if agent_id:- scope_sql = f"agent_id = ? AND ({scope_sql})"- scope_params = [agent_id, *scope_params]- if session_id:- scope_sql = f"session_id = ? AND ({scope_sql})"- scope_params = [session_id, *scope_params]+ agent_sql, agent_params = _in_clause("agent_id", agent_ids)+ if agent_sql:+ scope_sql = f"({agent_sql}) AND ({scope_sql})"+ scope_params = [*agent_params, *scope_params]+ session_sql, session_params = _in_clause("session_id", session_ids)+ if session_sql:+ scope_sql = f"({session_sql}) AND ({scope_sql})"+ scope_params = [*session_params, *scope_params]with self._lock:assert self._conn is not Nonecur = self._conn.execute(@@ -1112,22 +1140,25 @@ class CodingMemoryStore:def _count_active_facts_sync(self,user_id: str,- agent_id: Optional[str],- session_id: Optional[str],+ agent_ids: Optional[List[str]],+ session_ids: Optional[List[str]],workspace_id: Optional[str],branch: Optional[str],) -> Tuple[int, int]:"""返回 (作用域内 active 事实数, 该 user 的 active 事实总数)。事实层的过滤在 get_facts 拿到对象之后用 _scope_matches 做,这里只要选择率。+ agent_ids / session_ids 为空表示该维度不筛;非空按 IN 精确匹配。"""scope_sql, scope_params = _scope_where(workspace_id, branch)- if agent_id:- scope_sql = f"agent_id = ? AND ({scope_sql})"- scope_params = [agent_id, *scope_params]- if session_id:- scope_sql = f"session_id = ? AND ({scope_sql})"- scope_params = [session_id, *scope_params]+ agent_sql, agent_params = _in_clause("agent_id", agent_ids)+ if agent_sql:+ scope_sql = f"({agent_sql}) AND ({scope_sql})"+ scope_params = [*agent_params, *scope_params]+ session_sql, session_params = _in_clause("session_id", session_ids)+ if session_sql:+ scope_sql = f"({session_sql}) AND ({scope_sql})"+ scope_params = [*session_params, *scope_params]with self._lock:assert self._conn is not Nonerow = self._conn.execute(diff --git a/tests/coding/test_search_scope_filter.py b/tests/coding/test_search_scope_filter.pyindex 0a5e2b5..f5f24df 100644--- a/tests/coding/test_search_scope_filter.py+++ b/tests/coding/test_search_scope_filter.py@@ -63,11 +63,21 @@ async def scope_ctx(tmp_data_dir, mock_embed):await store.close()-async def _search(ctx, query, *, workspace_id=None, branch=None, limit=10):+async def _search(+ ctx,+ query,+ *,+ workspace_id=None,+ branch=None,+ limit=10,+ agent_ids=None,+ session_ids=None,+):"""闸门全开,只让作用域过滤起作用。"""return await HyMemoryClient._search_coding(ctx,target_query=query, user_ids=["u1"],+ agent_ids=agent_ids, session_ids=session_ids,workspace_id=workspace_id, branch=branch,limit=limit,working_fact_min_score=0.0, working_experience_min_score=0.0,@@ -98,6 +108,33 @@ def _fids(resp):return sorted(f["fact_id"] for f in resp["facts"])+class TestAgentSessionScope:+ async def test_search_coding_forwards_agent_and_session_scope(self, scope_ctx):+ store = scope_ctx._coding_store+ for suffix, agent_id, session_id in (+ ("a1", "a1", "s1"),+ ("a2", "a2", "s2"),+ ):+ await store.insert(CodingMemory(+ experience_id=f"e_{suffix}", user_id="u1",+ agent_id=agent_id, session_id=session_id,+ task="打包发布到私有包索引", solution=f"S-{suffix}",+ ))+ await store.insert_fact(WorkingFact(+ fact_id=f"f_{suffix}", user_id="u1",+ agent_id=agent_id, session_id=session_id,+ statement="打包发布到私有包索引",+ ))++ resp = await _search(+ scope_ctx, "打包发布到私有包索引",+ agent_ids=["a1"], session_ids=["s1"],+ )+ assert _eids(resp) == ["e_a1"]+ assert _fids(resp) == ["f_a1"]++# ================================================================# 规则本体(纯函数,不碰库)# ================================================================diff --git a/tests/coding/test_store.py b/tests/coding/test_store.pyindex dda9848..946eb51 100644--- a/tests/coding/test_store.py+++ b/tests/coding/test_store.py@@ -541,6 +541,59 @@ class TestFactStore:assert [f.fact_id for f, _ in hits] == ["f1"]assert hits[0][1] > 0.9+ async def test_search_facts_filters_agent_and_session_lists(self, coding_store):+ for fact_id, agent_id, session_id in (+ ("f_a1", "a1", "s1"),+ ("f_a2", "a2", "s2"),+ ):+ await coding_store.insert_fact(WorkingFact(+ fact_id=fact_id, user_id="u1", agent_id=agent_id,+ session_id=session_id, statement="同一个事实",+ ))+ emb = await coding_store.embed_service.embed("同一个事实")++ hits = await coding_store.search_facts(+ emb, user_id="u1", agent_ids=["a1"], top=5,+ )+ assert [f.fact_id for f, _ in hits] == ["f_a1"]++ hits = await coding_store.search_facts(+ emb, user_id="u1", session_ids=["s2"], top=5,+ )+ assert [f.fact_id for f, _ in hits] == ["f_a2"]++ hits = await coding_store.search_facts(+ emb, user_id="u1", agent_ids=["a1", "a2"], top=5,+ )+ assert {f.fact_id for f, _ in hits} == {"f_a1", "f_a2"}++ async def test_search_experiences_filters_agent_and_session_lists(self, coding_store):+ for experience_id, agent_id, session_id in (+ ("e_a1", "a1", "s1"),+ ("e_a2", "a2", "s2"),+ ):+ await coding_store.insert(CodingMemory(+ experience_id=experience_id, user_id="u1",+ agent_id=agent_id, session_id=session_id,+ task="同一个任务", search_keys=["同一个任务"], solution="S",+ ))+ emb = await coding_store.embed_service.embed("同一个任务")++ hits = await coding_store.search_by_query_embedding(+ emb, user_id="u1", agent_ids=["a1"], top=5,+ )+ assert {h["experience_id"] for h in hits} == {"e_a1"}++ hits = await coding_store.search_by_query_embedding(+ emb, user_id="u1", session_ids=["s2"], top=5,+ )+ assert {h["experience_id"] for h in hits} == {"e_a2"}++ hits = await coding_store.search_by_query_embedding(+ emb, user_id="u1", agent_ids=["a1", "a2"], top=5,+ )+ assert {h["experience_id"] for h in hits} == {"e_a1", "e_a2"}+async def test_supersede_soft_deletes(self, coding_store):await coding_store.insert_fact(WorkingFact(fact_id="f_old", user_id="u1", statement="旧结论",