From 7b84fd09983e2a539f5d6be392d253b3e8b1889a Mon Sep 17 00:00:00 2001 From: Hyeonsang Cho Date: Sun, 22 Nov 2026 10:41:03 +0900 Subject: [PATCH] fix(memory): let OpenViking's write policy decide a refused non-owner write (#8475) * fix(memory): let OpenViking's write policy decide a refused non-owner write A write for a DeerFlow user other than owner_user_id was refused with MemoryManagerError before the write policy was consulted, so under the default write: log_and_drop the after-agent hook and the pre-compaction flush failed every turn for that user after the answer was generated. The owner check still runs before any OpenViking call. Under log_and_drop the refused write is now logged and dropped; write: raise still raises. * docs(memory): say the refused OpenViking write is what gets logged --- .../harness/deerflow/agents/memory/AGENTS.md | 3 +- .../backends/openviking/openviking_manager.py | 11 +++- .../tests/test_openviking_memory_backend.py | 66 ++++++++++++++++--- docs/OPENVIKING.md | 5 +- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/backend/packages/harness/deerflow/agents/memory/AGENTS.md b/backend/packages/harness/deerflow/agents/memory/AGENTS.md index 36e418a005..ac847bce90 100644 --- a/backend/packages/harness/deerflow/agents/memory/AGENTS.md +++ b/backend/packages/harness/deerflow/agents/memory/AGENTS.md @@ -84,7 +84,8 @@ client or the real storage, because the payload shape is the contract. with values redacted. Programming errors still propagate. - **Do not deduplicate by text.** Honcho capture is incremental by stable source ID. Two messages with the same text and different IDs are both stored. -- **Reject another owner before any remote call.** +- **Reject another owner before any remote call.** The failure policy still + decides the turn: a refused write under `log_and_drop` must not fail the run. - **Shutdown drains active work** before it closes a client. ## Injection diff --git a/backend/packages/harness/deerflow/agents/memory/backends/openviking/openviking_manager.py b/backend/packages/harness/deerflow/agents/memory/backends/openviking/openviking_manager.py index bfbba6b9fa..1d235c5730 100644 --- a/backend/packages/harness/deerflow/agents/memory/backends/openviking/openviking_manager.py +++ b/backend/packages/harness/deerflow/agents/memory/backends/openviking/openviking_manager.py @@ -334,7 +334,16 @@ class OpenVikingMemoryManager(MemoryManager): try: if not thread_id: raise ValueError("OpenViking memory write requires thread_id") - peer_id = self._resolve_scope(user_id, agent_name) + try: + peer_id = self._resolve_scope(user_id, agent_name) + except MemoryManagerError as exc: + # The owner check has already refused the write before any + # remote call; the write policy decides whether that refusal + # also fails a turn whose answer was already generated. + if self._config.write_failure_policy == "raise": + raise + logger.error("%s Dropping the memory write (thread=%s).", exc, thread_id) + return session_id = _session_id( self._config.owner_user_id, peer_id, diff --git a/backend/tests/test_openviking_memory_backend.py b/backend/tests/test_openviking_memory_backend.py index abc9c50c14..72cd85c208 100644 --- a/backend/tests/test_openviking_memory_backend.py +++ b/backend/tests/test_openviking_memory_backend.py @@ -5,6 +5,7 @@ from __future__ import annotations import copy import gc import json +import logging import socket import threading import weakref @@ -272,6 +273,55 @@ def test_memory_admission_redacts_call_views_before_openviking_recorder(tmp_path manager.close() +@pytest.mark.parametrize("write_policy", ["log_and_drop", "raise"]) +@pytest.mark.parametrize("entry", ["after-agent", "async-after-agent", "compaction"]) +def test_another_owner_write_is_refused_and_follows_the_write_policy(tmp_path, monkeypatch, official_integration, caplog, write_policy: str, entry: str) -> None: + import asyncio + + from langgraph.runtime import Runtime + + from deerflow.agents.memory import summarization_hook + from deerflow.agents.middlewares import memory_middleware + from deerflow.agents.middlewares.memory_middleware import MemoryMiddleware + from deerflow.agents.middlewares.summarization_middleware import SummarizationEvent + from deerflow.config.memory_config import MemoryConfig + + messages = [HumanMessage("remember short replies"), AIMessage("noted")] + manager = _manager(tmp_path, monkeypatch, failure_policy={"write": write_policy}) + monkeypatch.setattr(memory_middleware, "get_memory_manager", lambda: manager) + monkeypatch.setattr(memory_middleware, "resolve_runtime_user_id", lambda runtime: "bob") + monkeypatch.setattr(summarization_hook, "get_memory_manager", lambda: manager) + monkeypatch.setattr(summarization_hook, "get_memory_config", lambda: MemoryConfig(enabled=True)) + monkeypatch.setattr(summarization_hook, "resolve_runtime_user_id", lambda runtime: "bob") + runtime = Runtime(context={"thread_id": "thread-1", "user_id": "bob"}) + middleware = MemoryMiddleware(memory_config=MemoryConfig(enabled=True)) + + def write() -> None: + if entry == "after-agent": + middleware.after_agent({"messages": messages}, runtime) + elif entry == "async-after-agent": + asyncio.run(middleware.aafter_agent({"messages": messages}, runtime)) + else: + event = SummarizationEvent(messages_to_summarize=tuple(messages), preserved_messages=(), thread_id="thread-1", agent_name=None, runtime=runtime) + summarization_hook.memory_flush_hook(event) + + try: + with caplog.at_level(logging.ERROR, logger=OpenVikingMemoryManager.__module__): + if write_policy == "raise": + with pytest.raises(MemoryManagerError, match="owner_user_id"): + write() + else: + write() + assert manager._recorder.calls == [] + assert manager._recorder.flushes == [] + assert not list((tmp_path / "openviking").rglob("*.json")) + if write_policy == "log_and_drop": + assert any("'bob'" in record.getMessage() and "Dropping the memory write" in record.getMessage() for record in caplog.records) + assert "remember short replies" not in caplog.text + finally: + manager.close() + + @contextmanager def _use_actor_peer(peer_id: str | None): token = _ACTOR_PEER.set(peer_id) @@ -496,8 +546,8 @@ def test_user_key_connection_uses_owner_key_without_ambient_authorization( assert len(requests) == before with pytest.raises(MemoryReadError, match="owner_user_id"): manager.search("private data", user_id="another-owner") - with pytest.raises(MemoryManagerError, match="owner_user_id"): - manager.add("thread-1", messages, user_id="another-owner") + # The default write policy drops the refused write instead of raising. + manager.add("thread-1", messages, user_id="another-owner") assert len(requests) == before finally: manager.close() @@ -748,19 +798,19 @@ def test_manager_refuses_to_share_single_user_key_across_deerflow_users( official_integration: None, ) -> None: manager = _manager(tmp_path, monkeypatch) + strict = _manager(tmp_path / "strict", monkeypatch, failure_policy={"write": "raise"}) + write = {"thread_id": "thread-1", "messages": [HumanMessage("private", id="h1")], "user_id": "bob", "agent_name": "research"} with pytest.raises(MemoryManagerError, match="owner_user_id 'alice'"): manager.get_context("bob", agent_name="research") + # The default write policy drops the refused write; `raise` propagates it. + assert manager.add(**write) is None with pytest.raises(MemoryManagerError, match="owner_user_id 'alice'"): - manager.add( - "thread-1", - [HumanMessage("private", id="h1")], - user_id="bob", - agent_name="research", - ) + strict.add(**write) assert manager._retriever.calls == [] assert manager._recorder.calls == [] + assert strict._recorder.calls == [] def test_manager_records_only_unseen_suffix( diff --git a/docs/OPENVIKING.md b/docs/OPENVIKING.md index 57e9183a59..ceb8b0c938 100644 --- a/docs/OPENVIKING.md +++ b/docs/OPENVIKING.md @@ -45,7 +45,10 @@ configurations and are outside this adapter's supported setup. `default` when DeerFlow authentication is disabled. In an authenticated single-user deployment, use that user's DeerFlow ID. A request for another DeerFlow user is rejected before OpenViking is contacted, preventing one USER -key from silently sharing memory across users. +key from silently sharing memory across users. The failure policies below +decide what that rejection does to the turn: with `write: log_and_drop` the +other user's write is logged as refused, without message content, and dropped, +and the run still completes. Multi-user credential provisioning and storage are intentionally outside this first adapter PR.