Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
Fix/peer fix (#2469)
chenjw738cee73953b
diff --git a/benchmark/locomo/vikingbot/import_to_ov.py b/benchmark/locomo/vikingbot/import_to_ov.pyindex a54eb4955..3b8bcb410 100644--- a/benchmark/locomo/vikingbot/import_to_ov.py+++ b/benchmark/locomo/vikingbot/import_to_ov.py@@ -30,6 +30,14 @@ def _get_session_number(session_key: str) -> int:return int(session_key.split("_")[1])+def build_memory_policy(group_chat: bool) -> Dict[str, Dict[str, bool]]:+ """Build session/commit memory policy for benchmark ingest."""+ return {+ "self": {"enabled": True},+ "peer": {"enabled": bool(group_chat)},+ }++def parse_test_file(path: str) -> List[Dict[str, Any]]:"""Parse txt test file into sessions.@@ -302,6 +310,7 @@ async def viking_ingest(user_id: Optional[str] = None,account: str = "default",api_key: Optional[str] = None,+ group_chat: bool = False,) -> Dict[str, int]:"""Save messages to OpenViking via OpenViking SDK client.Returns token usage dict with embedding and vlm token counts.@@ -313,6 +322,7 @@ async def viking_ingest(user_id: User identifier for separate userspace (e.g., "conv-26")account: OpenViking account identifierapi_key: Optional API key for OpenViking client authentication+ group_chat: Whether to enable peer-memory extraction for group-chat sessions"""# 解析 session_time - 为每条消息计算递增的时间戳base_datetime = None@@ -331,10 +341,11 @@ async def viking_ingest(timeout=600,)await client.initialize()+ memory_policy = build_memory_policy(group_chat)try:# Create session- create_res = await client.create_session()+ create_res = await client.create_session(memory_policy=memory_policy)session_id = create_res["session_id"]# Add messages one by one with created_at@@ -354,7 +365,11 @@ async def viking_ingest()# Commit- result = await client.commit_session(session_id, telemetry=True)+ result = await client.commit_session(+ session_id,+ telemetry=True,+ memory_policy=memory_policy,+ )# Accept both "committed" and "accepted" as success - accepted means the session was archivedif result.get("status") not in ("committed", "accepted"):@@ -423,6 +438,7 @@ async def process_single_session(user_id=user_id,account=account,api_key=args.api_key,+ group_chat=args.group_chat,)duration_seconds = round(time.perf_counter() - started_at, 3)token_usage = result["token_usage"]diff --git a/benchmark/locomo/vikingbot/run_eval.py b/benchmark/locomo/vikingbot/run_eval.pyindex 4ddbd457a..ef379b511 100644--- a/benchmark/locomo/vikingbot/run_eval.py+++ b/benchmark/locomo/vikingbot/run_eval.py@@ -404,7 +404,7 @@ def main():"--group-chat",action="store_true",default=False,- help="Group-chat mode: pass --memory-user to vikingbot chat. Default is group-chat mode.",+ help="Group-chat mode: pass --memory-peer to vikingbot chat. Default is group-chat mode.",)parser.add_argument("--retry-wrong",diff --git a/bot/vikingbot/agent/memory.py b/bot/vikingbot/agent/memory.pyindex 80cb2b9dd..e2522e160 100644--- a/bot/vikingbot/agent/memory.py+++ b/bot/vikingbot/agent/memory.py@@ -31,38 +31,8 @@ class MemoryStore:return 0.0@classmethod- def _limit_memory_groups(- cls,- result: dict[str, list[Any]],- limit: int,- ) -> dict[str, list[Any]]:- user_memories = result.get("user_memory", [])- agent_memories = result.get("agent_memory", [])- ranked: list[tuple[float, str, int, Any]] = []-- for group, memories in (- ("user_memory", user_memories),- ("agent_memory", agent_memories),- ):- for index, memory in enumerate(memories):- ranked.append((cls._get_score(memory), group, index, memory))-- selected = {- (group, index)- for _, group, index, _ in sorted(ranked, key=lambda item: item[0], reverse=True)[:limit]- }- return {- "user_memory": [- memory- for index, memory in enumerate(user_memories)- if ("user_memory", index) in selected- ],- "agent_memory": [- memory- for index, memory in enumerate(agent_memories)- if ("agent_memory", index) in selected- ],- }+ def _limit_memories(cls, result: list[Any], limit: int) -> list[Any]:+ return sorted(result, key=cls._get_score, reverse=True)[:limit]def read_long_term(self) -> str:if self.memory_file.exists():@@ -191,10 +161,9 @@ class MemoryStore:if isinstance(openviking_connection, dict) and openviking_connection.get("user_id")else config.admin_user_id)- # Use provided user_ids or fall back to sender_id- search_user_ids = user_ids if user_ids else [sender_id]logger.info(f"workspace_id={workspace_id}")- logger.info(f"user_ids={search_user_ids}")+ logger.info(f"sender_id={sender_id}")+ logger.info(f"user_ids={user_ids}")logger.info(f"admin_user_id={admin_user_id}")client = await VikingClient.create(@@ -203,12 +172,13 @@ class MemoryStore:)result = await client.search_memory(query=current_message,- user_ids=search_user_ids,+ owner_user_id=sender_id,+ peer_ids=user_ids,limit=30,)if not result:return ""- result = self._limit_memory_groups(result, limit=10)+ result = self._limit_memories(result, limit=10)# Log raw search results for debuggingmemory_list = []diff --git a/bot/vikingbot/agent/tools/ov_file.py b/bot/vikingbot/agent/tools/ov_file.pyindex d3041e9e4..e658919e4 100644--- a/bot/vikingbot/agent/tools/ov_file.py+++ b/bot/vikingbot/agent/tools/ov_file.py@@ -272,21 +272,44 @@ class VikingSearchTool(OVFileTool):try:client = await self._get_client(tool_context)admin_user_id = client.admin_user_id+ memory_user_ids = getattr(tool_context, "memory_user_ids", None)+ memory_peer_ids = getattr(tool_context, "memory_peer_ids", None)- if not target_uri and tool_context.memory_user_ids:- user_ids = tool_context.memory_user_ids if client.should_sender_fanout() else [None]+ if not target_uri and (memory_user_ids or memory_peer_ids):grouped_items = {"memory": [],"resource": [],"skill": [],}- for memory_user_id in user_ids:+ if memory_user_ids:+ user_ids = memory_user_ids if client.should_sender_fanout() else [None]+ deduped_user_ids: list[str | None] = []+ for user_id in user_ids or []:+ if user_id in deduped_user_ids:+ continue+ deduped_user_ids.append(user_id)+ search_requests = [+ {"target_uri": client._memory_target_uri(user_id), "peer_id": None}+ for user_id in deduped_user_ids+ ]+ else:+ search_requests = client.build_memory_search_requests(+ owner_user_id=tool_context.sender_id if client.should_sender_fanout() else None,+ peer_ids=memory_peer_ids,+ )++ for request in search_requests:+ search_kwargs = {+ "target_uri": request["target_uri"],+ "limit": 10,+ "user_id": admin_user_id,+ }+ if request.get("peer_id") is not None:+ search_kwargs["peer_id"] = request.get("peer_id")results = await client.search(query,- target_uri=client._memory_target_uri(memory_user_id),- limit=10,- user_id=admin_user_id,+ **search_kwargs,)filtered_items = self._filter_search_items(results, min_score=min_score)for item_type, items in filtered_items.items():diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.pyindex 7016630b5..ef8e08d63 100644--- a/bot/vikingbot/openviking_mount/ov_server.py+++ b/bot/vikingbot/openviking_mount/ov_server.py@@ -264,14 +264,103 @@ class VikingClient:return f"viking://user/{user_space}/memories/"return "viking://user/memories/"+ def _peer_memory_target_uri(self, user_id: Optional[str], peer_id: str) -> str:+ user_space = self._user_space_fragment(user_id)+ normalized_peer_id = str(peer_id or "").strip()+ if not normalized_peer_id:+ raise ValueError("peer_id is required for peer memory target")+ if not user_space:+ raise ValueError("peer memory target requires explicit user_id")+ return f"viking://user/{user_space}/peers/{normalized_peer_id}/memories/"++ @staticmethod+ def _dedupe_strings(values: List[str]) -> List[str]:+ deduped: List[str] = []+ seen: set[str] = set()+ for value in values:+ if not value or value in seen:+ continue+ seen.add(value)+ deduped.append(value)+ return deduped++ def build_memory_search_requests(+ self,+ *,+ user_ids: Optional[List[str]] = None,+ owner_user_id: Optional[str] = None,+ peer_ids: Optional[List[str]] = None,+ ) -> List[Dict[str, Optional[str]]]:+ requests: List[Dict[str, Optional[str]]] = []+ normalized_user_ids = self._dedupe_strings(+ [str(user_id).strip() for user_id in (user_ids or []) if str(user_id).strip()]+ )+ normalized_peer_ids = self._dedupe_strings(+ [str(peer_id).strip() for peer_id in (peer_ids or []) if str(peer_id).strip()]+ )+ effective_owner_user_id = self._effective_user_id(owner_user_id) if owner_user_id else None++ for user_id in normalized_user_ids:+ requests.append({"target_uri": self._memory_target_uri(user_id), "peer_id": None})++ if effective_owner_user_id and (not requests or normalized_peer_ids):+ requests.append(+ {"target_uri": self._memory_target_uri(effective_owner_user_id), "peer_id": None}+ )++ if effective_owner_user_id:+ for peer_id in normalized_peer_ids:+ try:+ requests.append(+ {+ "target_uri": self._peer_memory_target_uri(+ effective_owner_user_id, peer_id+ ),+ "peer_id": None,+ }+ )+ except ValueError as exc:+ logger.warning(+ f"Skip invalid peer memory target owner_user_id={effective_owner_user_id}, peer_id={peer_id}: {exc}"+ )+ elif normalized_peer_ids:+ for peer_id in normalized_peer_ids:+ requests.append({"target_uri": "viking://user/memories/", "peer_id": peer_id})++ if not requests:+ requests.append({"target_uri": self._memory_target_uri(None), "peer_id": None})++ deduped: List[Dict[str, Optional[str]]] = []+ seen: set[tuple[str, str]] = set()+ for request in requests:+ target_uri = str(request.get("target_uri") or "")+ peer_id = str(request.get("peer_id") or "")+ key = (target_uri, peer_id)+ if not target_uri or key in seen:+ continue+ seen.add(key)+ deduped.append({"target_uri": target_uri, "peer_id": request.get("peer_id")})+ return deduped+def _skill_memory_uri(self, skill_name: str, user_id: Optional[str] = None) -> str:return f"{self._memory_target_uri(user_id)}skills/{skill_name}.md"- async def find(self, query: str, target_uri: Optional[str] = None):+ def should_sender_fanout(self) -> bool:+ return self._is_root_key_mode()++ async def find(+ self,+ query: str,+ target_uri: Optional[str] = None,+ peer_id: Optional[str] = None,+ ):"""搜索资源"""+ kwargs: Dict[str, Any] = {}+ if peer_id is not None:+ kwargs["peer_id"] = peer_idif target_uri:- return await self.client.find(query, target_uri=target_uri)- return await self.client.find(query)+ return await self.client.find(query, target_uri=target_uri, **kwargs)+ return await self.client.find(query, **kwargs)async def add_resource(self, local_path: str, desc: str) -> Optional[Dict[str, Any]]:"""添加资源到 Viking"""@@ -328,6 +417,7 @@ class VikingClient:target_uri: str | list[str] | None = None,limit: int = 10,user_id: Optional[str] = None,+ peer_id: Optional[str] = None,) -> Dict[str, Any]:client = self.clientshould_close = False@@ -335,7 +425,12 @@ class VikingClient:client, should_close = await self._get_user_scoped_client(user_id)try:- result = await client.search(query, target_uri=target_uri, limit=limit)+ result = await client.search(+ query,+ target_uri=target_uri,+ limit=limit,+ peer_id=peer_id,+ )finally:if should_close:await client.close()@@ -478,9 +573,12 @@ class VikingClient:async def search_memory(self,query: str,- user_ids: str | list[str],+ user_ids: str | list[str] | None = None,limit: int = 10,agent_user_id: Optional[str] = None,+ *,+ owner_user_id: Optional[str] = None,+ peer_ids: Optional[list[str]] = None,) -> list[Any] | dict[str, list[Any]]:"""通过上下文消息检索用户 memory。"""@@ -498,21 +596,43 @@ class VikingClient:if isinstance(user_ids, str):user_ids = [user_ids]- all_user_memories = []+ normalized_user_ids = [str(user_id).strip() for user_id in (user_ids or []) if user_id]+ normalized_peer_ids = [str(peer_id).strip() for peer_id in (peer_ids or []) if peer_id]+ effective_owner_user_id = self._effective_user_id(owner_user_id) if owner_user_id else None++ user_ids_to_check = self._dedupe_strings(+ [+ *normalized_user_ids,+ *( [effective_owner_user_id] if effective_owner_user_id else [] ),+ ]+ )- for user_id in user_ids:+ for user_id in user_ids_to_check:effective_user_id = self._effective_user_id(user_id)+ if not effective_user_id:+ continueuser_exists = await self._check_user_exists(effective_user_id)if not user_exists:await self._initialize_user(effective_user_id)- continue- uri_user_memory = self._memory_target_uri(effective_user_id)- user_memory = await self.client.find(- query=query,- target_uri=uri_user_memory,- limit=limit,- )+ search_requests = self.build_memory_search_requests(+ user_ids=normalized_user_ids,+ owner_user_id=effective_owner_user_id,+ peer_ids=normalized_peer_ids,+ )+ if not search_requests:+ return []++ all_user_memories = []+ for request in search_requests:+ find_kwargs: Dict[str, Any] = {+ "query": query,+ "target_uri": request["target_uri"],+ "limit": limit,+ }+ if request.get("peer_id") is not None:+ find_kwargs["peer_id"] = request.get("peer_id")+ user_memory = await self.client.find(**find_kwargs)all_user_memories.extend(_extract_memories(user_memory))if agent_user_id is not None:diff --git a/openviking/core/directories.py b/openviking/core/directories.pyindex e83b025bf..9a95ee075 100644--- a/openviking/core/directories.py+++ b/openviking/core/directories.py@@ -175,7 +175,12 @@ class DirectoryInitializer:return countasync def initialize_user_directories(self, ctx: RequestContext) -> int:- """Initialize user-space tree lazily for the current user."""+ """Initialize only the user namespace root for the current user.++ Child directories under ``viking://user/{user_space}`` are created lazily on+ first write. This avoids materializing empty namespaces such as+ ``memories/patterns`` or ``skills`` before any actual content exists.+ """if "user" not in PRESET_DIRECTORIES:return 0user_space_root = canonical_user_root(ctx)@@ -188,11 +193,7 @@ class DirectoryInitializer:scope="user",ctx=ctx,)- count = 1 if created else 0- count += await self._initialize_children(- "user", user_tree.children, user_space_root, ctx=ctx- )- return count+ return 1 if created else 0async def initialize_agent_directories(self, ctx: RequestContext) -> int:"""Deprecated compatibility hook; agent directories are no longer initialized."""diff --git a/openviking/session/memory/memory_isolation_handler.py b/openviking/session/memory/memory_isolation_handler.pyindex 68a75a596..4384372d8 100644--- a/openviking/session/memory/memory_isolation_handler.py+++ b/openviking/session/memory/memory_isolation_handler.py@@ -56,6 +56,25 @@ class MemoryIsolationHandler:"""No-op hook kept for the extraction pipeline."""return+ def _messages(self) -> List[Any]:+ messages = getattr(self._extract_context, "messages", None)+ return messages if isinstance(messages, list) else []++ def _has_self_user_message(self) -> bool:+ for msg in self._messages():+ if getattr(msg, "role", None) != "user":+ continue+ if safe_peer_id(getattr(msg, "peer_id", None)) is None:+ return True+ return False++ def _first_peer_id_in_messages(self) -> Optional[str]:+ for msg in self._messages():+ peer_id = safe_peer_id(getattr(msg, "peer_id", None))+ if peer_id and self._can_write_peer(peer_id):+ return peer_id+ return None+def get_read_scope(self) -> RoleScope:user_ids = set()peer_ids = set()@@ -170,9 +189,17 @@ class MemoryIsolationHandler:return []peer_ids_to_write = [peer_id]operation.memory_fields["peer_id"] = peer_id- elif self.allow_self:- include_self = True+ else:operation.memory_fields.pop("peer_id", None)+ if self.allow_self and self._has_self_user_message():+ include_self = True+ else:+ fallback_peer_id = self._first_peer_id_in_messages()+ if fallback_peer_id:+ peer_ids_to_write = [fallback_peer_id]+ operation.memory_fields["peer_id"] = fallback_peer_id+ elif self.allow_self:+ include_self = Trueif not include_self and not peer_ids_to_write:return []diff --git a/openviking/session/memory/schema_model_generator.py b/openviking/session/memory/schema_model_generator.pyindex a2baeb68d..e02fad582 100644--- a/openviking/session/memory/schema_model_generator.py+++ b/openviking/session/memory/schema_model_generator.py@@ -78,19 +78,15 @@ class SchemaModelGenerator:Args:memory_type: The memory type schema- role_scope: Role scope to determine if user_id fields are needed+ role_scope: Role scope to determine if peer_id fields are neededReturns:Dynamically created flat Pydantic model class"""# Determine cache key based on role_scopehas_peer_scope = bool(role_scope and role_scope.peer_ids)- if role_scope and len(role_scope.user_ids) > 1:- cache_key = f"{memory_type.memory_type}_multi_user"- model_name = f"{to_pascal_case(memory_type.memory_type)}DataMultiUser"- else:- cache_key = memory_type.memory_type- model_name = f"{to_pascal_case(memory_type.memory_type)}Data"+ cache_key = memory_type.memory_type+ model_name = f"{to_pascal_case(memory_type.memory_type)}Data"if has_peer_scope:cache_key = f"{cache_key}_peer"model_name = f"{model_name}Peer"@@ -102,14 +98,9 @@ class SchemaModelGenerator:# Build field definitions - no memory_type field neededfield_definitions: Dict[str, Tuple[Type[Any], Any]] = {}- # Add user_id when multiple users are in scope- # Skip if schema has "ranges" field (like events) - these are message-based and don't need user isolation+ # Skip if schema has "ranges" field (like events) - these are message-based and+ # their self/peer targets are derived from message ranges instead of explicit routing fields.has_ranges = any(field.name == "ranges" for field in memory_type.fields)- if role_scope and len(role_scope.user_ids) > 1 and not has_ranges:- field_definitions["user_id"] = (- str,- Field(..., description="User ID to distinguish which user's memory to write"),- )if has_peer_scope and not has_ranges:peer_values = ", ".join(role_scope.peer_ids)field_definitions["peer_id"] = (diff --git a/openviking/session/memory/utils/json_parser.py b/openviking/session/memory/utils/json_parser.pyindex 4cda6ea11..6643aab95 100644--- a/openviking/session/memory/utils/json_parser.py+++ b/openviking/session/memory/utils/json_parser.py@@ -431,7 +431,7 @@ def parse_json_with_stability(tracer.info("Extracted first item from list response")if not isinstance(parsed_data, dict):- return None, f"Expected dict after parsing, got {type(parsed_data)}"+ return None, f"Expected dict after parsing, got {parsed_data}"# Filter to only expected fields if providedif expected_fields:diff --git a/tests/session/memory/test_memory_isolation_handler.py b/tests/session/memory/test_memory_isolation_handler.pyindex 04c41b3cd..8e7ebcf35 100644--- a/tests/session/memory/test_memory_isolation_handler.py+++ b/tests/session/memory/test_memory_isolation_handler.py@@ -441,3 +441,89 @@ class TestCalculateMemoryUris:assert uris == []mock_generate_uri.assert_not_called()++ @patch("openviking.session.memory.memory_isolation_handler.generate_uri")+ def test_calculate_memory_uris_missing_peer_id_prefers_self_when_self_user_message_exists(+ self, mock_generate_uri+ ):+ mock_generate_uri.side_effect = lambda **kwargs: (+ f"viking://user/{kwargs.get('user_space')}/memories/preferences"+ )++ ctx = create_ctx(user_id="support_bot")+ messages = [+ create_message("user", "self turn"),+ create_message("assistant", "ack", peer_id="web:visitor:alice"),+ create_message("user", "peer turn", peer_id="web:visitor:alice"),+ ]+ extract_ctx = create_mock_extract_context(messages)+ handler = MemoryIsolationHandler(+ ctx,+ extract_ctx,+ allow_self=True,+ allowed_peer_ids={"web:visitor:alice"},+ )++ from openviking.session.memory.dataclass import MemoryTypeSchema, ResolvedOperation++ schema = MemoryTypeSchema(+ memory_type="preferences",+ filename_template="preferences.md",+ directory="viking://user/{user_space}/memories",+ )+ operation = ResolvedOperation(+ old_memory_file_content=None,+ memory_fields={},+ memory_type="preferences",+ uris=[],+ )++ uris = handler.calculate_memory_uris(schema, operation, extract_ctx)++ assert uris == ["viking://user/support_bot/memories/preferences"]+ assert operation.memory_fields["user_id"] == "support_bot"+ assert "peer_id" not in operation.memory_fields++ @patch("openviking.session.memory.memory_isolation_handler.generate_uri")+ def test_calculate_memory_uris_missing_peer_id_falls_back_to_first_peer_when_self_absent(+ self, mock_generate_uri+ ):+ mock_generate_uri.side_effect = lambda **kwargs: (+ f"viking://user/{kwargs.get('user_space')}/memories/preferences"+ )++ ctx = create_ctx(user_id="support_bot")+ messages = [+ create_message("user", "peer turn one", peer_id="web:visitor:bob"),+ create_message("assistant", "ack", peer_id="web:visitor:bob"),+ create_message("user", "peer turn two", peer_id="web:visitor:alice"),+ ]+ extract_ctx = create_mock_extract_context(messages)+ handler = MemoryIsolationHandler(+ ctx,+ extract_ctx,+ allow_self=True,+ allowed_peer_ids={"web:visitor:alice", "web:visitor:bob"},+ )++ from openviking.session.memory.dataclass import MemoryTypeSchema, ResolvedOperation++ schema = MemoryTypeSchema(+ memory_type="preferences",+ filename_template="preferences.md",+ directory="viking://user/{user_space}/memories",+ )+ operation = ResolvedOperation(+ old_memory_file_content=None,+ memory_fields={},+ memory_type="preferences",+ uris=[],+ )++ uris = handler.calculate_memory_uris(schema, operation, extract_ctx)++ assert uris == [+ "viking://user/support_bot/peers/web:visitor:bob/memories/preferences"+ ]+ assert operation.memory_fields["user_id"] == "support_bot"+ assert operation.memory_fields["peer_id"] == "web:visitor:bob"