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
perf: parallelize type quota recall searches (#3175)
huangruiteng90b2c910d7e1
diff --git a/openviking/retrieve/type_quota_recall.py b/openviking/retrieve/type_quota_recall.pyindex a657f27c2..b9672d9ed 100644--- a/openviking/retrieve/type_quota_recall.py+++ b/openviking/retrieve/type_quota_recall.py@@ -9,6 +9,7 @@ block that degrades from full content to summary to URI-only entries.from __future__ import annotations+import asyncioimport refrom dataclasses import dataclass, field, replacefrom typing import Any, Mapping@@ -303,17 +304,12 @@ async def search_type_quota_recall(user_root = canonical_user_root(ctx)roots = memory_target_roots(ctx)open_ctx = replace(ctx, actor_peer_id=None, legacy_agent_id=None)- raw_by_type: dict[str, list[Any]] = {}+ raw_by_type: dict[str, list[Any]] = {memory_type: [] for memory_type in TYPE_ORDER}selected: list[tuple[str, Any, int, str, RequestContext]] = []- for memory_type in TYPE_ORDER:- quota = normalized_quotas.get(memory_type, 0)- if quota <= 0:- raw_by_type[memory_type] = []- continue- found: list[Any] = []- for root in roots:- result = await service.search.find(+ async def search_type(memory_type: str, quota: int) -> list[Any]:+ searches = [+ service.search.find(query=query,ctx=ctx,target_uri=_type_target(root, memory_type),@@ -321,22 +317,42 @@ async def search_type_quota_recall(score_threshold=min_score,level=None,)- found.extend(_extract_memories(result))+ for root in roots+ ]if peer_scope == "all":- result = await service.search.find(- query=query,- ctx=open_ctx,- target_uri=f"{user_root}/peers",- limit=max(quota * OTHER_PEER_OVERFETCH, quota),- score_threshold=min_score,- level=None,+ searches.append(+ service.search.find(+ query=query,+ ctx=open_ctx,+ target_uri=f"{user_root}/peers",+ limit=max(quota * OTHER_PEER_OVERFETCH, quota),+ score_threshold=min_score,+ level=None,+ ))++ results = await asyncio.gather(*searches)+ found = [item for result in results[: len(roots)] for item in _extract_memories(result)]+ if peer_scope == "all":found.extend(item- for item in _extract_memories(result)+ for item in _extract_memories(results[-1])if f"/memories/{memory_type}/" in _uri(item)and _origin_for_uri(_uri(item), ctx.actor_peer_id, user_root) == "other_peer")+ return found++ active_types = [+ (memory_type, normalized_quotas[memory_type])+ for memory_type in TYPE_ORDER+ if normalized_quotas[memory_type] > 0+ ]+ found_by_type = await asyncio.gather(+ *(search_type(memory_type, quota) for memory_type, quota in active_types)+ )++ for (memory_type, quota), found in zip(active_types, found_by_type, strict=True):+ if peer_scope == "all":found = _dedupe(found)raw_by_type[memory_type] = foundranked = _limit_with_peer_penalties(diff --git a/tests/retrieve/test_type_quota_recall.py b/tests/retrieve/test_type_quota_recall.pynew file mode 100644index 000000000..2efa528ec--- /dev/null+++ b/tests/retrieve/test_type_quota_recall.py@@ -0,0 +1,114 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0++import asyncio+from types import SimpleNamespace++from openviking.retrieve.type_quota_recall import search_type_quota_recall+from openviking.server.identity import RequestContext, Role+from openviking_cli.session.user_id import UserIdentifier+++class _FakeFindResult:+ def __init__(self, memories=None):+ self.memories = memories or []+++async def test_independent_type_searches_start_concurrently():+ started_targets: list[str] = []+ all_started = asyncio.Event()++ async def fake_find(**kwargs):+ started_targets.append(kwargs["target_uri"])+ if len(started_targets) == 4:+ all_started.set()+ await all_started.wait()+ return _FakeFindResult()++ service = SimpleNamespace(+ search=SimpleNamespace(find=fake_find),+ fs=SimpleNamespace(),+ )+ ctx = RequestContext(+ user=UserIdentifier.the_default_user("test_user"),+ role=Role.USER,+ actor_peer_id="current",+ )++ result = await asyncio.wait_for(+ search_type_quota_recall(+ service=service,+ ctx=ctx,+ query="parallel recall",+ peer_scope="actor",+ quotas={+ "events": 1,+ "entities": 1,+ "preferences": 0,+ "experiences": 0,+ },+ ),+ timeout=1.0,+ )++ assert set(started_targets) == {+ "viking://user/test_user/memories/events",+ "viking://user/test_user/peers/current/memories/events",+ "viking://user/test_user/memories/entities",+ "viking://user/test_user/peers/current/memories/entities",+ }+ assert result.stats["searched"] == {+ "events": 0,+ "entities": 0,+ "preferences": 0,+ "experiences": 0,+ }+++async def test_parallel_search_preserves_type_order():+ async def fake_find(**kwargs):+ target_uri = kwargs["target_uri"]+ memory_type = target_uri.rsplit("/", 1)[-1]+ if memory_type == "events":+ await asyncio.sleep(0.02)+ if "/peers/" in target_uri:+ return _FakeFindResult()+ return _FakeFindResult(+ [+ {+ "uri": f"{target_uri}/{memory_type}.md",+ "score": 0.9,+ "abstract": f"{memory_type} abstract",+ }+ ]+ )++ async def fake_read(uri, **kwargs):+ del kwargs+ return f"content for {uri}"++ service = SimpleNamespace(+ search=SimpleNamespace(find=fake_find),+ fs=SimpleNamespace(read=fake_read),+ )+ ctx = RequestContext(+ user=UserIdentifier.the_default_user("test_user"),+ role=Role.USER,+ actor_peer_id="current",+ )++ result = await search_type_quota_recall(+ service=service,+ ctx=ctx,+ query="deterministic recall",+ peer_scope="actor",+ quotas={+ "events": 1,+ "entities": 1,+ "preferences": 0,+ "experiences": 0,+ },+ )++ assert [entry.type for entry in result.entries] == ["events", "entities"]+ assert [entry.rank for entry in result.entries] == [1, 1]