Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

Feat(bot): Change memory auto-call limit and type (#2609)

DuTao4e8c3f9d6860
diff --git a/bot/README.md b/bot/README.md
index 7406084a2..9a5cb2aac 100644
--- a/bot/README.md
+++ b/bot/README.md
@@ -190,6 +190,11 @@ All configurations are under the `bot` field in `ov.conf`, with default values f
Legacy configs that put a root key in `api_key` must add `api_key_type: "root"` or move the key to `root_api_key`; otherwise `api_key` is interpreted as a User key.
- exp_write_tools: Optional list of tool names that trigger experience-memory injection before the call (self-evolving agent memory loop, see #2007). Defaults to `["write_file", "edit_file"]`. This only controls the bot-side injection trigger; stored experience generation is governed by OpenViking memory extraction and the active session `memory_policy.memory_types` whitelist.
- `recall_exp_first_round_only`: Optional. When `true`, `ContextBuilder._build_user_memory` skips per-turn user/agent experience recall and injects experiences only once on the first user turn. Defaults to `false`.
+ - Per-turn user/peer memory recall uses type-quota search by default. `profile.md` is injected through the profile path and does not occupy auto-search candidates.
+ - `memory_recall_events_limit`: Optional. Number of `events/` memories retrieved per turn. Defaults to `10`.
+ - `memory_recall_entities_limit`: Optional. Number of `entities/` memories retrieved per turn. Defaults to `10`.
+ - `memory_recall_preferences_limit`: Optional. Number of `preferences/` memories retrieved per turn. Defaults to `3`.
+ - `memory_recall_max_chars`: Optional. Character budget for injected user/peer full memories. Defaults to `4000`.
- `exp_recall_limit`: Optional. Number of experiences to retrieve per task during recall. Defaults to `5`.
- `exp_recall_max_chars`: Optional. Character budget for the formatted experience block injected into context. Defaults to `2000`.
- `channels`: Message platform configuration, see [Message Platform Configuration](bot/docs/CHANNEL.md) for details
diff --git a/bot/README_CN.md b/bot/README_CN.md
index 6be91fffe..c7843311b 100644
--- a/bot/README_CN.md
+++ b/bot/README_CN.md
@@ -192,6 +192,11 @@ bot 将连接到远程 OpenViking Server,使用前请先启动 OpenViking Serv
旧配置如果把 root key 填在 `api_key`,需要补充 `api_key_type: "root"`,或迁移到 `root_api_key`;否则 `api_key` 会按 User key 解释。
- `exp_write_tools`:可选,触发经验记忆注入的工具名列表(自演化 agent memory 循环,详见 #2007)。默认 `["write_file", "edit_file"]`。该配置只控制 bot 侧注入触发时机;已存储 experience 的生成由 OpenViking 记忆抽取和当前 session 的 `memory_policy.memory_types` 白名单控制。
- `recall_exp_first_round_only`:可选。为 `true` 时,`ContextBuilder._build_user_memory` 跳过每轮 user/agent 经验召回,仅在首个 user turn 注入一次经验。默认 `false`。
+ - 每轮 user/peer 记忆召回默认使用 type-quota 检索。`profile.md` 仍由 profile 链路单独注入,不占自动检索候选位。
+ - `memory_recall_events_limit`:可选。每轮召回的 `events/` 记忆条数。默认 `10`。
+ - `memory_recall_entities_limit`:可选。每轮召回的 `entities/` 记忆条数。默认 `10`。
+ - `memory_recall_preferences_limit`:可选。每轮召回的 `preferences/` 记忆条数。默认 `3`。
+ - `memory_recall_max_chars`:可选。注入到上下文的 user/peer 记忆全文字符预算。默认 `4000`。
- `exp_recall_limit`:可选。召回时每个任务检索的经验条数。默认 `5`。
- `exp_recall_max_chars`:可选。注入到上下文的格式化经验块的字符预算。默认 `2000`。
- `channels`:消息平台配置,详见 [消息平台配置](bot/docs/CHANNEL.md)
diff --git a/bot/tests/test_openviking_api_key_type.py b/bot/tests/test_openviking_api_key_type.py
index 522b1fe37..1553faef2 100644
--- a/bot/tests/test_openviking_api_key_type.py
+++ b/bot/tests/test_openviking_api_key_type.py
@@ -1373,11 +1373,264 @@ async def test_viking_memory_context_keeps_legacy_users_separate_from_peers(
"query": "hello",
"user_ids": ["legacy-user"],
"peer_ids": ["sender-1", "speaker-a"],
- "limit": 10,
+ "limit": 35,
}
]
+async def test_viking_memory_type_quota_groups_with_event_summaries_and_uris(
+ monkeypatch, tmp_path
+):
+ clients = []
+ base_uri = "viking://user/default/peers/sender-1/memories"
+
+ class _FakeClient:
+ def __init__(self):
+ self.find_calls = []
+ self.contents = {
+ f"{base_uri}/events/e1.md": "short event",
+ f"{base_uri}/events/e2.md": (
+ "Summary: long event summary\n"
+ "2023-01-01 (Sunday) ChatLog:\n"
+ "full long event details "
+ + ("x" * 800)
+ ),
+ f"{base_uri}/events/e3.md": "legacy event without summary",
+ f"{base_uri}/entities/en1.md": "short entity",
+ f"{base_uri}/entities/en2.md": "long entity " + ("y" * 500),
+ f"{base_uri}/preferences/p1.md": "first preference " + ("z" * 700),
+ f"{base_uri}/preferences/p2.md": "second preference",
+ }
+
+ def build_current_memory_target_uris(self, *, peer_ids=None, include_self=True):
+ return [base_uri]
+
+ async def find(self, *, query, target_uri, limit):
+ self.find_calls.append((query, target_uri, limit))
+ if target_uri.endswith("/events/"):
+ return {
+ "memories": [
+ {"uri": f"{base_uri}/events/e2.md", "score": 0.8},
+ {"uri": f"{base_uri}/events/e1.md", "score": 0.9},
+ {"uri": f"{base_uri}/events/e3.md", "score": 0.7},
+ ]
+ }
+ if target_uri.endswith("/entities/"):
+ return {
+ "memories": [
+ {"uri": f"{base_uri}/entities/en2.md", "score": 0.8},
+ {"uri": f"{base_uri}/entities/en1.md", "score": 0.9},
+ ]
+ }
+ if target_uri.endswith("/preferences/"):
+ return {
+ "memories": [
+ {"uri": f"{base_uri}/preferences/p1.md", "score": 0.9},
+ {"uri": f"{base_uri}/preferences/p2.md", "score": 0.8},
+ ]
+ }
+ return {"memories": []}
+
+ async def read_content(self, uri, level="read"):
+ return self.contents[uri]
+
+ async def close(self):
+ return None
+
+ async def _fake_create(**_kwargs):
+ client = _FakeClient()
+ clients.append(client)
+ return client
+
+ monkeypatch.setattr(
+ "vikingbot.agent.memory.load_config",
+ lambda: _make_config("root", memory_recall_max_chars=1100),
+ )
+ monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
+
+ store = MemoryStore(tmp_path)
+ result = await store.get_viking_memory_context(
+ current_message="hello",
+ workspace_id="workspace",
+ sender_id="sender-1",
+ )
+
+ assert clients[0].find_calls == [
+ ("hello", f"{base_uri}/events/", 10),
+ ("hello", f"{base_uri}/entities/", 10),
+ ("hello", f"{base_uri}/preferences/", 3),
+ ]
+ assert 'type="snippet"' not in result
+ assert result.count('type="full"') == 3
+ assert result.count('type="summary"') == 1
+ assert result.count('type="uri"') == 3
+ assert '<memory_group type="events">' in result
+ assert '<memory_group type="entities">' in result
+ assert '<memory_group type="preferences">' in result
+ assert "Event memories. The URI path includes the event date." in result
+ assert result.index('<memory_group type="events"') < result.index(
+ '<memory_group type="entities"'
+ )
+ assert result.index('<memory_group type="entities"') < result.index(
+ '<memory_group type="preferences"'
+ )
+ assert result.index("/events/e1.md") < result.index("/events/e2.md")
+ assert result.index("/events/e2.md") < result.index("/events/e3.md")
+ assert result.index("/events/e3.md") < result.index("/entities/en1.md")
+ assert result.index("/entities/en1.md") < result.index("/entities/en2.md")
+ assert result.index("/entities/en2.md") < result.index("/preferences/p1.md")
+ assert result.index("/preferences/p1.md") < result.index("/preferences/p2.md")
+ assert '<memory index="1" type="full">' in result
+ assert '<memory index="2" type="summary">' in result
+ assert '<memory index="3" type="full">' in result
+ assert '<memory index="4" type="full">' in result
+ assert '<memory index="5" type="uri">' in result
+ assert '<memory index="6" type="uri">' in result
+ assert '<memory index="7" type="uri">' in result
+ assert "long event summary" in result
+ assert "full long event details" not in result
+ assert "legacy event without summary" in result
+ assert "long entity " not in result
+ assert "first preference " not in result
+ assert "second preference" not in result
+
+
+async def test_viking_memory_type_quota_continues_after_oversized_entity(
+ monkeypatch, tmp_path
+):
+ base_uri = "viking://user/default/peers/sender-1/memories"
+
+ class _FakeClient:
+ def build_current_memory_target_uris(self, *, peer_ids=None, include_self=True):
+ return [base_uri]
+
+ async def find(self, *, query, target_uri, limit):
+ if target_uri.endswith("/entities/"):
+ return {
+ "memories": [
+ {"uri": f"{base_uri}/entities/long.md", "score": 0.9},
+ {"uri": f"{base_uri}/entities/short.md", "score": 0.8},
+ ]
+ }
+ return {"memories": []}
+
+ async def read_content(self, uri, level="read"):
+ if uri.endswith("/long.md"):
+ return "long fact " + ("x" * 500)
+ return "short fact"
+
+ async def close(self):
+ return None
+
+ async def _fake_create(**_kwargs):
+ return _FakeClient()
+
+ monkeypatch.setattr(
+ "vikingbot.agent.memory.load_config",
+ lambda: _make_config("root", memory_recall_max_chars=1200),
+ )
+ monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
+
+ store = MemoryStore(tmp_path)
+ result = await store.get_viking_memory_context(
+ current_message="hello",
+ workspace_id="workspace",
+ sender_id="sender-1",
+ )
+
+ assert '<memory index="1" type="uri">' in result
+ assert '<uri>viking://user/default/peers/sender-1/memories/entities/long.md</uri>' in result
+ assert '<memory index="2" type="full">' in result
+ assert '<uri>viking://user/default/peers/sender-1/memories/entities/short.md</uri>' in result
+ assert "<content>short fact</content>" in result
+ assert "long fact " not in result
+
+
+async def test_viking_memory_type_quota_does_not_overflow_preference_budget(
+ monkeypatch, tmp_path
+):
+ base_uri = "viking://user/default/peers/sender-1/memories"
+
+ class _FakeClient:
+ def build_current_memory_target_uris(self, *, peer_ids=None, include_self=True):
+ return [base_uri]
+
+ async def find(self, *, query, target_uri, limit):
+ if target_uri.endswith("/preferences/"):
+ return {
+ "memories": [
+ {"uri": f"{base_uri}/preferences/long.md", "score": 0.9},
+ {"uri": f"{base_uri}/preferences/short.md", "score": 0.8},
+ ]
+ }
+ return {"memories": []}
+
+ async def read_content(self, uri, level="read"):
+ if uri.endswith("/long.md"):
+ return "very long preference " + ("x" * 500)
+ return "short preference"
+
+ async def close(self):
+ return None
+
+ async def _fake_create(**_kwargs):
+ return _FakeClient()
+
+ monkeypatch.setattr(
+ "vikingbot.agent.memory.load_config",
+ lambda: _make_config("root", memory_recall_max_chars=100),
+ )
+ monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
+
+ store = MemoryStore(tmp_path)
+ result = await store.get_viking_memory_context(
+ current_message="hello",
+ workspace_id="workspace",
+ sender_id="sender-1",
+ )
+
+ assert 'type="full"' not in result
+ assert result.count('type="uri"') == 2
+ assert "very long preference" not in result
+ assert "short preference" not in result
+
+
+async def test_viking_memory_context_returns_empty_after_profile_filter(
+ monkeypatch, tmp_path
+):
+ class _FakeClient:
+ async def search_memory(self, **_kwargs):
+ return [
+ {
+ "uri": "viking://user/default/peers/sender-1/memories/profile.md",
+ "score": 0.9,
+ }
+ ]
+
+ async def close(self):
+ return None
+
+ async def _fake_create(**_kwargs):
+ return _FakeClient()
+
+ monkeypatch.setattr("vikingbot.agent.memory.load_config", lambda: _make_config("root"))
+ monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
+
+ store = MemoryStore(tmp_path)
+ result = await store.get_viking_memory_context(
+ current_message="hello",
+ workspace_id="workspace",
+ sender_id="sender-1",
+ user_ids=["legacy-user"],
+ )
+
+ assert result == ""
+
+
@pytest.mark.asyncio
async def test_openviking_search_uses_user_namespace(monkeypatch):
monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
@@ -1612,7 +1865,47 @@ async def test_context_reminds_agent_to_search_current_memory_question(tmp_path)
assert "OpenViking Memory Retrieval" in user_info
assert "use openviking_search for the current question" in user_info
- assert "A previous empty search result does not prove" in user_info
+ assert "search again when the requested fact changes" in user_info
+ assert "grouped by memory_type" in user_info
+ assert "events contain atomic time-based facts" in user_info
+ assert "entities contain stable topic/entity facts" in user_info
+ assert "preferences contain likes, habits, and recurring tendencies" in user_info
+ assert "full means the full memory content is already shown" in user_info
+ assert "summary means only a summary is shown" in user_info
+ assert "uri means only the URI is shown" in user_info
+ assert "openviking_multi_read" in user_info
+
+
+async def test_context_memory_prefix_tells_agent_to_read_summary_and_uri_details(tmp_path):
+ class _Memory:
+ async def get_viking_memory_context(self, **_kwargs):
+ return (
+ "### user memories:\n"
+ '<memory index="1" type="summary">\n'
+ " <uri>viking://user/default/peers/sender-1/memories/events/e.md</uri>\n"
+ " <summary>important clue</summary>\n"
+ "</memory>"
+ )
+
+ context = ContextBuilder(workspace=tmp_path, sender_id="sender-1")
+ context._memory = _Memory()
+
+ user_info = await context._build_user_memory(
+ session_key=SessionKey(type="cli", channel_id="default", chat_id="chat-1"),
+ current_message="问题",
+ sender_id="sender-1",
+ ov_tools_enable=True,
+ is_first_round=False,
+ )
+
+ assert "## openviking_search(query=[user_query])" in user_info
+ assert "grouped by memory_type" in user_info
+ assert "full means the full memory content is already shown" in user_info
+ assert "summary means only a summary is shown" in user_info
+ assert "uri means only the URI is shown" in user_info
+ assert "openviking_multi_read" in user_info
+ assert "important clue" in user_info
@pytest.mark.asyncio
diff --git a/bot/vikingbot/agent/context.py b/bot/vikingbot/agent/context.py
index 865cf2e1b..7096724c6 100644
--- a/bot/vikingbot/agent/context.py
+++ b/bot/vikingbot/agent/context.py
@@ -234,7 +234,10 @@ Skills with available="false" need dependencies installed first - you can try in
parts.append(
"## OpenViking Memory Retrieval\n"
"- For questions about the user's remembered facts, preferences, profile, or personal context, use openviking_search for the current question before saying there is no relevant record.\n"
- "- A previous empty search result does not prove that a different follow-up question has no memory; search again when the requested fact changes."
+ "- A previous empty search result does not prove that a different follow-up question has no memory; search again when the requested fact changes.\n"
+ "- Injected memories are grouped by memory_type: events contain atomic time-based facts; entities contain stable topic/entity facts; preferences contain likes, habits, and recurring tendencies.\n"
+ "- Injected memory entries use three types: full means the full memory content is already shown; summary means only a summary is shown and the URI has more detail; uri means only the URI is shown and it may still point to key facts.\n"
+ "- For relevant summary or uri entries, use openviking_multi_read on their URIs to fetch full details to help you to resolve the query. "
)
if exp_first_round_only:
diff --git a/bot/vikingbot/agent/memory.py b/bot/vikingbot/agent/memory.py
index 006fd472d..79fa41086 100644
--- a/bot/vikingbot/agent/memory.py
+++ b/bot/vikingbot/agent/memory.py
@@ -1,6 +1,7 @@
"""Memory system for persistent agent memory."""
import asyncio
+import re
import time
from pathlib import Path
from typing import Any
@@ -12,6 +13,25 @@ from vikingbot.openviking_mount.ov_server import VikingClient
from vikingbot.utils.helpers import ensure_dir
+_LEGACY_MEMORY_RECALL_LIMIT = 30
+_TYPE_QUOTA_MEMORY_TYPES = ("events", "entities", "preferences")
+_TYPE_QUOTA_EVENT_CHAR_RATIO = 0.75
+_TYPE_QUOTA_PREFERENCE_FULL_LIMIT = 1
+_MEMORY_TYPE_DESCRIPTIONS = {
+ "events": (
+ "Event memories. The URI path includes the event date."
+ ),
+ "entities": (
+ "Entity and topic memories. Use them for stable facts, attributes, "
+ "relationships, and background about people, hobbies, places, or concepts."
+ ),
+ "preferences": (
+ "Preference memories. Use them for likes, dislikes, habits, recurring choices, "
+ "and long-term personal tendencies."
+ ),
+}
+
+
class MemoryStore:
"""Two-layer memory: MEMORY.md (long-term facts) + HISTORY.md (grep-searchable log)."""
@@ -30,26 +50,214 @@ class MemoryStore:
except (TypeError, ValueError):
return 0.0
+ @staticmethod
+ def _get_uri(memory: Any) -> str:
+ return memory.get("uri", "") if isinstance(memory, dict) else getattr(memory, "uri", "")
+
+ @staticmethod
+ def _get_abstract(memory: Any) -> str:
+ return (
+ memory.get("abstract", "")
+ if isinstance(memory, dict)
+ else getattr(memory, "abstract", "")
+ )
+
+ @staticmethod
+ def _get_recall_type(memory: Any) -> str:
+ return (
+ memory.get("_recall_type", "")
+ if isinstance(memory, dict)
+ else getattr(memory, "_recall_type", "")
+ )
+
+ @staticmethod
+ def _get_recall_rank(memory: Any) -> int:
+ raw_rank = (
+ memory.get("_recall_rank", 0)
+ if isinstance(memory, dict)
+ else getattr(memory, "_recall_rank", 0)
+ )
+ try:
+ return int(raw_rank)
+ except (TypeError, ValueError):
+ return 0
+
+ @classmethod
+ def _infer_memory_type(cls, memory: Any) -> str:
+ recall_type = cls._get_recall_type(memory)
+ if recall_type:
+ return recall_type
+
+ uri = cls._get_uri(memory).strip("/")
+ parts = [part for part in uri.split("/") if part]
+ for idx, part in enumerate(parts):
+ if part == "memories" and idx + 1 < len(parts):
+ return parts[idx + 1]
+ return ""
+
+ @classmethod
+ def _with_recall_metadata(cls, memory: Any, memory_type: str, rank: int) -> dict[str, Any]:
+ if isinstance(memory, dict):
+ item = dict(memory)
+ else:
+ item = {
+ "uri": cls._get_uri(memory),
+ "score": cls._get_score(memory),
+ "abstract": cls._get_abstract(memory),
+ }
+ item["_recall_type"] = memory_type
+ item["_recall_rank"] = rank
+ return item
+
@classmethod
def _limit_memories(cls, result: list[Any], limit: int) -> list[Any]:
return sorted(result, key=cls._get_score, reverse=True)[:limit]
+ @staticmethod
+ def _extract_memories(result: Any) -> list[Any]:
+ if not result:
+ return []
+ if isinstance(result, list):
+ return result
+ if isinstance(result, dict):
+ memories = result.get("memories")
+ return memories if isinstance(memories, list) else []
+ memories = getattr(result, "memories", None)
+ return memories if isinstance(memories, list) else []
+
+ @classmethod
+ def _dedupe_memories(cls, memories: list[Any]) -> list[Any]:
+ deduped: list[Any] = []
+ seen_uris: set[str] = set()
+ for memory in memories:
+ uri = cls._get_uri(memory)
+ if not uri or uri in seen_uris:
+ continue
+ seen_uris.add(uri)
+ deduped.append(memory)
+ return deduped
+
+ @staticmethod
+ def _memory_type_target(base_uri: str, memory_type: str) -> str:
+ return f"{base_uri.rstrip('/')}/{memory_type.strip('/')}/"
+
+ @classmethod
+ def _order_type_quota_memories(
+ cls,
+ memories: list[Any],
+ ) -> list[Any]:
+ groups: dict[str, list[Any]] = {}
+ others: list[Any] = []
+ for memory in memories:
+ memory_type = cls._infer_memory_type(memory)
+ if memory_type:
+ groups.setdefault(memory_type, []).append(memory)
+ else:
+ others.append(memory)
+
+ for group in groups.values():
+ group.sort(key=cls._get_score, reverse=True)
+ others.sort(key=cls._get_score, reverse=True)
+
+ ordered: list[Any] = []
+ for memory_type in _TYPE_QUOTA_MEMORY_TYPES:
+ ordered.extend(groups.get(memory_type, []))
+ ordered.extend(others)
+ return cls._dedupe_memories(ordered)
+
+ @staticmethod
+ def _type_quota_char_budgets(max_chars: int) -> dict[str, int]:
+ max_chars = max(0, int(max_chars))
+ event_budget = int(max_chars * _TYPE_QUOTA_EVENT_CHAR_RATIO)
+ return {
+ "events": event_budget,
+ "entities": max_chars - event_budget,
+ }
+
+ @staticmethod
+ def _format_memory_group(memory_type: str, memories: list[str]) -> str:
+ description = _MEMORY_TYPE_DESCRIPTIONS.get(
+ memory_type,
+ "Other retrieved memories. Use them when relevant and inspect URI entries if needed.",
+ )
+ body = "\n".join(memories)
+ return (
+ f'<memory_group type="{memory_type}">\n'
+ f" <group_hint>{description}</group_hint>\n"
+ f"{body}\n"
+ f"</memory_group>"
+ )
+
+ @staticmethod
+ def _format_full_memory(idx: int, uri: str, score: float, content: str) -> str:
+ return (
+ f'<memory index="{idx}" type="full">\n'
+ f" <uri>{uri}</uri>\n"
+ f" <score>{score}</score>\n"
+ f" <content>{content}</content>\n"
+ f"</memory>"
+ )
+
+ @staticmethod
+ def _format_summary_memory(idx: int, uri: str, score: float, summary: str) -> str:
+ return (
+ f'<memory index="{idx}" type="summary">\n'
+ f" <uri>{uri}</uri>\n"
+ f" <score>{score}</score>\n"
+ f" <summary>{summary}</summary>\n"
+ f"</memory>"
+ )
+
+ @staticmethod
+ def _format_uri_memory(idx: int, uri: str, score: float) -> str:
+ return (
+ f'<memory index="{idx}" type="uri">\n'
+ f" <uri>{uri}</uri>\n"
+ f" <score>{score}</score>\n"
+ f"</memory>"
+ )
+
+ @staticmethod
+ def _extract_event_summary(content: str, fallback: str = "") -> str:
+ if content:
+ match = re.search(
+ r"(?is)^\s*Summary:\s*(.*?)(?:\n\s*\d{4}-\d{2}-\d{2}"
+ r"(?:\s*\([^)]+\))?\s*ChatLog:|\n\s*ChatLog:|\n\s*<!--\s*MEMORY_FIELDS|$)",
+ content,
+ )
+ if match:
+ return re.sub(r"\s+", " ", match.group(1)).strip()
+ return fallback.strip()
+
def read_long_term(self) -> str:
if self.memory_file.exists():
return self.memory_file.read_text(encoding="utf-8")
return ""
async def _parse_viking_memory(
- self, result: Any, client: Any, min_score: float = 0.3, max_chars: int = 4000
+ self,
+ result: Any,
+ client: Any,
+ min_score: float = 0.3,
+ max_chars: int = 4000,
+ full_limit: int | None = None,
+ type_char_budgets: dict[str, int] | None = None,
+ preference_full_limit: int = 0,
+ include_uri_entries: bool = True,
) -> str:
"""Parse viking memory with score filtering and character limit.
- Automatically reads full content for memories above threshold.
+ Automatically reads full content for memories that fit the relevant budget;
+ memories beyond budget are kept as URI-only entries when include_uri_entries is true.
Args:
result: Memory search results
client: VikingClient instance to read content
min_score: Minimum score threshold (default: 0.4)
- max_chars: Maximum character limit for output (default: 4000)
+ max_chars: Maximum character limit for full memories in global mode
+ full_limit: Number of top memories allowed to use full content in global mode
+ type_char_budgets: Per-memory-type character budgets for type_quota recall
+ preference_full_limit: Number of preference memories forced full in type_quota mode
+ include_uri_entries: Whether to keep URI-only candidates after content budgets are exhausted
Returns:
Formatted memory string within character limit
@@ -57,29 +265,37 @@ class MemoryStore:
if not result or len(result) == 0:
return ""
- # Filter by min_score and sort by score descending
- def get_score(m):
- return m.get("score", 0) if isinstance(m, dict) else getattr(m, "score", 0.0)
-
- def get_uri(m):
- return m.get("uri", "") if isinstance(m, dict) else getattr(m, "uri", "")
-
- def get_abstract(m):
- return m.get("abstract", "") if isinstance(m, dict) else getattr(m, "abstract", "")
-
- filtered_memories = [memory for memory in result if get_score(memory) >= min_score]
- filtered_memories.sort(key=get_score, reverse=True)
+ filtered_memories = [memory for memory in result if self._get_score(memory) >= min_score]
+ use_type_budgets = bool(type_char_budgets) and any(
+ self._infer_memory_type(memory) for memory in filtered_memories
+ )
+ if use_type_budgets:
+ filtered_memories = self._order_type_quota_memories(filtered_memories)
+ else:
+ filtered_memories.sort(key=self._get_score, reverse=True)
- user_memories = []
+ grouped_memories: dict[str, list[str]] = {}
total_chars = 0
+ type_chars = {memory_type: 0 for memory_type in _TYPE_QUOTA_MEMORY_TYPES}
+ preference_full_count = 0
seen_content_hashes = set()
+ full_limit = len(filtered_memories) if full_limit is None else max(0, full_limit)
+ type_char_budgets = type_char_budgets or {}
for idx, memory in enumerate(filtered_memories, start=1):
- uri = get_uri(memory)
- abstract = get_abstract(memory)
- score = get_score(memory)
+ uri = self._get_uri(memory)
+ abstract = self._get_abstract(memory)
+ score = self._get_score(memory)
+ memory_type = self._infer_memory_type(memory) or "other"
+ should_try_full = idx <= full_limit
+ if use_type_budgets:
+ should_try_full = (
+ memory_type in type_char_budgets
+ ) or (
+ memory_type == "preferences"
+ and preference_full_count < max(0, preference_full_limit)
+ )
- # First, try to build full memory with content
content = ""
try:
content = await client.read_content(uri, level="read")
@@ -87,52 +303,63 @@ class MemoryStore:
logger.warning(f"Failed to read content from {uri}: {e}")
# Deduplicate by content hash (use content or abstract as key)
- content_to_hash = content or abstract
+ content_to_hash = content or abstract or uri
content_hash = hash(content_to_hash)
if content_to_hash and content_hash in seen_content_hashes:
continue
if content_to_hash:
seen_content_hashes.add(content_hash)
- if content:
- # Try full version first (no abstract when content is present)
- full_memory_str = (
- f'<memory index="{idx}" type="full">\n'
- f" <uri>{uri}</uri>\n"
- f" <score>{score}</score>\n"
- f" <content>{content}</content>\n"
- f"</memory>"
- )
+ if should_try_full and content:
+ full_memory_str = self._format_full_memory(idx, uri, score, content)
full_chars = len(full_memory_str)
- if user_memories:
+ if any(grouped_memories.values()):
full_chars += 1
- if total_chars + full_chars <= max_chars:
- user_memories.append(full_memory_str)
+ if use_type_budgets and memory_type in type_char_budgets:
+ budget = max(0, int(type_char_budgets[memory_type]))
+ if (
+ type_chars[memory_type] + full_chars <= budget
+ and total_chars + full_chars <= max_chars
+ ):
+ grouped_memories.setdefault(memory_type, []).append(full_memory_str)
+ type_chars[memory_type] += full_chars
+ total_chars += full_chars
+ continue
+ elif use_type_budgets and memory_type == "preferences":
+ preference_full_count += 1
+ if total_chars + full_chars <= max_chars:
+ grouped_memories.setdefault(memory_type, []).append(full_memory_str)
+ total_chars += full_chars
+ continue
+ elif total_chars + full_chars <= max_chars:
+ grouped_memories.setdefault(memory_type, []).append(full_memory_str)
total_chars += full_chars
- else:
- # Full version too big, use link-only version (always add)
- link_only_str = (
- f'<memory index="{idx}" type="link">\n'
- f" <uri>{uri}</uri>\n"
- f" <score>{score}</score>\n"
- f"</memory>"
+ continue
+
+ if use_type_budgets and memory_type == "events" and content:
+ summary = self._extract_event_summary(content, fallback=abstract)
+ if summary:
+ grouped_memories.setdefault(memory_type, []).append(
+ self._format_summary_memory(idx, uri, score, summary)
)
- user_memories.append(link_only_str)
- # Don't count link-only towards max_chars
- else:
- # No content available, use link-only version (always add)
- logger.info(f"Using link-only for {uri} (read failed or empty)")
- memory_str = (
- f'<memory index="{idx}" type="link">\n'
- f" <uri>{uri}</uri>\n"
- f" <score>{score}</score>\n"
- f"</memory>"
+ continue
+
+ if include_uri_entries:
+ grouped_memories.setdefault(memory_type, []).append(
+ self._format_uri_memory(idx, uri, score)
)
- user_memories.append(memory_str)
- # Don't count link-only towards max_chars
- return "\n".join(user_memories)
+ ordered_groups: list[str] = []
+ for memory_type in (*_TYPE_QUOTA_MEMORY_TYPES, "other"):
+ memories = grouped_memories.get(memory_type)
+ if memories:
+ ordered_groups.append(self._format_memory_group(memory_type, memories))
+ for memory_type, memories in grouped_memories.items():
+ if memory_type not in (*_TYPE_QUOTA_MEMORY_TYPES, "other") and memories:
+ ordered_groups.append(self._format_memory_group(memory_type, memories))
+
+ return "\n".join(ordered_groups)
def write_long_term(self, content: str) -> None:
self.memory_file.write_text(content, encoding="utf-8")
@@ -145,6 +372,41 @@ class MemoryStore:
long_term = self.read_long_term()
return f"## Long-term Memory\n{long_term}" if long_term else ""
+ async def _search_viking_memory_by_type_quota(
+ self,
+ client: VikingClient,
+ query: str,
+ peer_ids: list[str] | None,
+ quotas: dict[str, int],
+ ) -> list[Any]:
+ base_targets = client.build_current_memory_target_uris(
+ peer_ids=peer_ids,
+ include_self=not bool(peer_ids),
+ )
+ if not base_targets:
+ return []
+
+ all_memories: list[Any] = []
+ for memory_type, quota in quotas.items():
+ if quota <= 0:
+ continue
+ type_memories: list[Any] = []
+ for base_target in base_targets:
+ target_uri = self._memory_type_target(base_target, memory_type)
+ try:
+ result = await client.find(query=query, target_uri=target_uri, limit=quota)
+ except Exception as e:
+ logger.warning(f"Failed to search {target_uri}: {e}")
+ continue
+ type_memories.extend(self._extract_memories(result))
+ type_memories = self._limit_memories(self._dedupe_memories(type_memories), quota)
+ all_memories.extend(
+ self._with_recall_metadata(memory, memory_type, rank)
+ for rank, memory in enumerate(type_memories, start=1)
+ )
+
+ return self._dedupe_memories(all_memories)
+
async def get_viking_memory_context(
self,
current_message: str,
@@ -156,11 +418,11 @@ class MemoryStore:
) -> str:
client = None
try:
- config = load_config().ov_server
+ ov_cfg = load_config().ov_server
admin_user_id = (
str(openviking_connection.get("user_id"))
if isinstance(openviking_connection, dict) and openviking_connection.get("user_id")
- else config.admin_user_id
+ else ov_cfg.admin_user_id
)
logger.info(f"workspace_id={workspace_id}")
logger.info(f"sender_id={sender_id}")
@@ -173,28 +435,63 @@ class MemoryStore:
connection=openviking_connection,
)
search_peer_ids = [sender_id, *(peer_ids or [])] if sender_id else (peer_ids or None)
- result = await client.search_memory(
- query=current_message,
- user_ids=user_ids,
- peer_ids=search_peer_ids,
- limit=10,
- )
+ type_quotas = {
+ "events": max(0, int(getattr(ov_cfg, "memory_recall_events_limit", 10))),
+ "entities": max(0, int(getattr(ov_cfg, "memory_recall_entities_limit", 10))),
+ "preferences": max(0, int(getattr(ov_cfg, "memory_recall_preferences_limit", 3))),
+ }
+ recall_max_chars = max(1, int(getattr(ov_cfg, "memory_recall_max_chars", 6500)))
+ use_type_quota = not user_ids
+ if use_type_quota:
+ result = await self._search_viking_memory_by_type_quota(
+ client=client,
+ query=current_message,
+ peer_ids=search_peer_ids,
+ quotas=type_quotas,
+ )
+ else:
+ result = await client.search_memory(
+ query=current_message,
+ user_ids=user_ids,
+ peer_ids=search_peer_ids,
+ limit=_LEGACY_MEMORY_RECALL_LIMIT + 5,
+ )
+ if not result:
+ return ""
+ result = [
+ memory
+ for memory in result
+ if not self._get_uri(memory).rstrip("/").endswith("/profile.md")
+ ]
if not result:
return ""
- result = self._limit_memories(result, limit=10)
+ if not use_type_quota:
+ result = self._limit_memories(result, limit=_LEGACY_MEMORY_RECALL_LIMIT)
# Log raw search results for debugging
+ recall_strategy = "type_quota" if use_type_quota else "global"
memory_list = []
- memory_list.append(f"user_memory[{len(result)}]:")
+ memory_list.append(f"user_memory[{len(result)}],strategy={recall_strategy}:")
for i, mem in enumerate(result):
- uri = mem.get("uri", "") if isinstance(mem, dict) else getattr(mem, "uri", "")
- score = mem.get("score", 0) if isinstance(mem, dict) else getattr(mem, "score", 0)
+ uri = self._get_uri(mem)
+ score = self._get_score(mem)
memory_list.append(f"{i},{uri},{score}")
raw_memories_log = "\n".join(memory_list)
logger.info(f"[RAW_MEMORIES]\n{raw_memories_log}")
user_memory = await self._parse_viking_memory(
- result, client, min_score=0.1, max_chars=4000
+ result,
+ client,
+ min_score=0.1,
+ max_chars=recall_max_chars,
+ full_limit=0 if use_type_quota else None,
+ type_char_budgets=(
+ self._type_quota_char_budgets(recall_max_chars) if use_type_quota else None
+ ),
+ preference_full_limit=(
+ _TYPE_QUOTA_PREFERENCE_FULL_LIMIT if use_type_quota else 0
+ ),
+ include_uri_entries=True,
)
return f"### user memories:\n{user_memory}"
except Exception as e:
diff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.py
index 63c4837a2..b829ef6e8 100644
--- a/bot/vikingbot/config/schema.py
+++ b/bot/vikingbot/config/schema.py
@@ -531,6 +531,12 @@ class OpenVikingConfig(BaseModel):
# NOTE: in True mode no memory is injected on later turns of a multi-turn session, so
# it suits single-turn / per-task runners (e.g. tau2) rather than long conversations.
recall_exp_first_round_only: bool = False
+ # Per-turn user/peer memory recall uses type-quota search by default because
+ # the lightweight profile no longer carries every stable fact.
+ memory_recall_events_limit: int = 10
+ memory_recall_entities_limit: int = 10
+ memory_recall_preferences_limit: int = 3
+ memory_recall_max_chars: int = 4000
# How many experience memories to fetch per call to get_viking_experience_context.
exp_recall_limit: int = 5
# Total character budget for the injected experience block. Memories beyond this
diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.py
index 14238fe0a..161d621fc 100644
--- a/bot/vikingbot/openviking_mount/ov_server.py
+++ b/bot/vikingbot/openviking_mount/ov_server.py
@@ -436,9 +436,10 @@ class VikingClient:
query: str,
target_uri: Optional[str] = None,
peer_id: Optional[str] = None,
+ limit: int = 10,
):
"""搜索资源"""
- kwargs: Dict[str, Any] = {}
+ kwargs: Dict[str, Any] = {"limit": limit}
if peer_id is not None:
kwargs["peer_id"] = peer_id
if target_uri:
diff --git a/bot/vikingbot/tests/unit/test_openviking_peer_identity.py b/bot/vikingbot/tests/unit/test_openviking_peer_identity.py
index fa4ab7a83..e81bf1ff0 100644
--- a/bot/vikingbot/tests/unit/test_openviking_peer_identity.py
+++ b/bot/vikingbot/tests/unit/test_openviking_peer_identity.py
@@ -6,12 +6,15 @@ import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
-config_module = types.ModuleType("vikingbot.config")
-loader_module = types.ModuleType("vikingbot.config.loader")
-loader_module.load_config = lambda: None
-config_module.load_config = loader_module.load_config
-sys.modules.setdefault("vikingbot.config", config_module)
-sys.modules.setdefault("vikingbot.config.loader", loader_module)
+try:
+ import vikingbot.config.loader # noqa: F401
+except Exception:
+ config_module = types.ModuleType("vikingbot.config")
+ loader_module = types.ModuleType("vikingbot.config.loader")
+ loader_module.load_config = lambda: None
+ config_module.load_config = loader_module.load_config
+ sys.modules.setdefault("vikingbot.config", config_module)
+ sys.modules.setdefault("vikingbot.config.loader", loader_module)
from vikingbot.openviking_mount.ov_server import VikingClient # noqa: E402