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(memory): use last value for duplicate tag keys (#3950)
Jiahui Zhouc1cc592aaf1d
diff --git a/openviking/utils/tags.py b/openviking/utils/tags.pyindex 89c7e5309..d30eac14b 100644--- a/openviking/utils/tags.py+++ b/openviking/utils/tags.py@@ -31,17 +31,14 @@ def normalize_search_tags(tags: Iterable[str] | None) -> list[str]:if not tags:return []- normalized: list[str] = []- seen: set[str] = set()+ values_by_key: OrderedDict[str, str] = OrderedDict()for item in tags:if item is None:continuevalue = normalize_search_tag(item)- if value in seen:- continue- seen.add(value)- normalized.append(value)- return normalized+ key, raw_value = value.split("=", 1)+ values_by_key[key] = raw_value+ return [f"{key}={value}" for key, value in values_by_key.items()]def build_search_tags_filter(tags: Iterable[str] | None) -> dict[str, Any] | None:diff --git a/tests/server/test_api_sessions_event_tags.py b/tests/server/test_api_sessions_event_tags.pyindex ed5c2eb02..9736ca011 100644--- a/tests/server/test_api_sessions_event_tags.py+++ b/tests/server/test_api_sessions_event_tags.py@@ -67,3 +67,58 @@ async def test_session_event_tags_reject_invalid_values(client: httpx.AsyncClienassert response.status_code == 400assert response.json()["error"]["code"] == "INVALID_ARGUMENT"+++async def test_create_session_duplicate_event_tag_keys_keep_last_value(+ client: httpx.AsyncClient,+):+ response = await client.post(+ "/api/v1/sessions",+ json={+ "memory_extraction_config": {+ "events": {"tags": ["channel=web", "channel=app"]}+ }+ },+ )++ assert response.status_code == 200+ assert _event_tags(response) == ["channel=app"]+++async def test_patch_session_config_duplicate_event_tag_keys_keep_last_value(+ client: httpx.AsyncClient,+):+ created = await client.post("/api/v1/sessions", json={})+ session_id = created.json()["result"]["session_id"]++ response = await client.patch(+ f"/api/v1/sessions/{session_id}/config",+ json={+ "memory_extraction_config": {+ "events": {"tags": ["channel=web", "channel=app"]}+ }+ },+ )++ assert response.status_code == 200+ assert _event_tags(response) == ["channel=app"]+++async def test_commit_duplicate_event_tag_keys_keep_last_value(client: httpx.AsyncClient):+ created = await client.post("/api/v1/sessions", json={})+ session_id = created.json()["result"]["session_id"]+ await client.post(+ f"/api/v1/sessions/{session_id}/messages",+ json={"role": "user", "content": "I want a refund"},+ )++ response = await client.post(+ f"/api/v1/sessions/{session_id}/commit",+ json={+ "extraction_metadata": {+ "event": {"tags": ["channel=web", "channel=app"]}+ }+ },+ )++ assert response.status_code == 200diff --git a/tests/unit/test_search_tags_filter.py b/tests/unit/test_search_tags_filter.pyindex 2a6f4be5e..b8aea5032 100644--- a/tests/unit/test_search_tags_filter.py+++ b/tests/unit/test_search_tags_filter.py@@ -2,7 +2,7 @@# SPDX-License-Identifier: AGPL-3.0from openviking.server.routers.search import _resolve_search_filter-from openviking.utils.tags import build_search_tags_filter+from openviking.utils.tags import build_search_tags_filter, normalize_search_tagsdef test_search_tags_filter_keeps_single_tag_as_single_must():@@ -23,6 +23,10 @@ def test_search_tags_filter_dedupes_before_building_and_filter():}+def test_search_tags_duplicate_keys_keep_last_value():+ assert normalize_search_tags(["channel=web", "channel=app"]) == ["channel=app"]++def test_find_tags_filter_requires_all_tags():result = _resolve_search_filter(request_filter=None,