Skip to content

The agent that grows with you

fix(memory): mirror a patch to providers as a whole-entry replace

emozilla5e0ec82d1f25
diff --git a/agent/memory_manager.py b/agent/memory_manager.py
index 2de403ac5f3..76c2cd5c467 100644
--- a/agent/memory_manager.py
+++ b/agent/memory_manager.py
@@ -919,7 +919,14 @@ class MemoryManager:
previous = result.get("patched_entry_before")
if isinstance(previous, str) and previous:
metadata["previous_content"] = previous
- self.on_memory_write(action, target, str(op.get("content") or op.get("new_text") or ""), metadata=metadata)
+ mirrored_action, content = action, str(op.get("content") or op.get("new_text") or "")
+ if action == "patch":
+ # To a provider a patch replaced the whole entry with its edited form; mirror it as
+ # that replace so providers that know add/replace/remove handle it unchanged.
+ mirrored_action = "replace"
+ content = (previous.replace(str(old_text).strip(), content.strip()) if batched
+ else str(result.get("patched_entry_after") or ""))
+ self.on_memory_write(mirrored_action, target, content, metadata=metadata)
except Exception as e:
logger.debug("notify_memory_tool_write failed for op %s: %s", action, e)
diff --git a/agent/memory_provider.py b/agent/memory_provider.py
index e0a4dd706e1..8641636b4c5 100644
--- a/agent/memory_provider.py
+++ b/agent/memory_provider.py
@@ -195,7 +195,8 @@ class MemoryProvider(ABC):
"""Mirror a built-in memory-tool write (``action``: add | replace | remove; ``target``:
memory | user; ``metadata``: provenance such as write_origin, session_id, tool_name).
For replace/remove, ``metadata["previous_content"]`` is the full entry selected
- under the native-store lock. Notifications follow a successful complete write
+ under the native-store lock. A memory-tool patch arrives as a replace whose
+ ``content`` is the whole entry after the edit. Notifications follow a successful complete write
or batch; each batch operation sees the preceding operation's result. Older
callers may omit this field: ``old_text`` alone is not authoritative identity.
"""
diff --git a/tests/agent/test_memory_write_bridge.py b/tests/agent/test_memory_write_bridge.py
index 2782b0ed2ae..76b86862509 100644
--- a/tests/agent/test_memory_write_bridge.py
+++ b/tests/agent/test_memory_write_bridge.py
@@ -162,7 +162,8 @@ def test_committed_entry_identity_comes_from_locked_store(
assert reloaded._entries_for(target) == store._entries_for(target)
manager.notify_memory_tool_write(result, args, build_metadata=lambda: {'session_id': 'test'})
- assert [call['action'] for call in provider.calls] == [op['action'] for op in operations]
+ assert [call['action'] for call in provider.calls] == [
+ 'replace' if op['action'] == 'patch' else op['action'] for op in operations]
assert [call['metadata'].get('previous_content') for call in provider.calls] == previous
assert all(call['metadata']['session_id'] == 'test' for call in provider.calls)
assert 'Prefers tea with milk' in store._entries_for(target)
@@ -175,3 +176,24 @@ def test_previous_content_cannot_come_from_uncommitted_arguments():
{'success': True}, args, build_metadata=lambda: {'previous_content': 'Uncommitted metadata'}
)
assert provider.calls[0]['metadata'] == {'old_text': 'partial'}
+
+
[email protected]('batch', [False, True])
+def test_patch_mirrors_as_a_replace_of_the_whole_entry(tmp_path, monkeypatch, batch):
+ from tools.memory_tool import MemoryStore, memory_tool
+
+ monkeypatch.setattr('tools.memory_tool.get_memory_dir', lambda: tmp_path)
+ store = MemoryStore()
+ store.load_from_disk()
+ store.add('memory', 'Server runs on port 8080 behind nginx')
+ manager, provider = _manager_with_provider()
+ ops = [{'action': 'patch', 'old_text': 'port 8080', 'content': 'port 9090'},
+ {'action': 'patch', 'old_text': 'nginx', 'content': 'caddy'}]
+ for args in ([{'target': 'memory', 'operations': ops}] if batch else [{'target': 'memory', **op} for op in ops]):
+ result = memory_tool(store=store, **args)
+ assert json.loads(result)['success'] is True
+ manager.notify_memory_tool_write(result, args)
+ assert [(c['action'], c['content'], c['metadata']['previous_content']) for c in provider.calls] == [
+ ('replace', 'Server runs on port 9090 behind nginx', 'Server runs on port 8080 behind nginx'),
+ ('replace', 'Server runs on port 9090 behind caddy', 'Server runs on port 9090 behind nginx'),
+ ]