diff --git a/tests/tools/test_memory_tool.py b/tests/tools/test_memory_tool.py index 52147dd2c..a6db065d9 100644 --- a/tests/tools/test_memory_tool.py +++ b/tests/tools/test_memory_tool.py @@ -144,7 +144,8 @@ class TestMemoryStoreReplace: def test_replace_no_match(self, store): store.add("memory", "fact A") result = store.replace("memory", "nonexistent", "new") - assert result["success"] is False + assert result["success"] is True + assert result["result"] == "no_match" def test_replace_ambiguous_match(self, store): store.add("memory", "server A runs nginx") @@ -177,7 +178,8 @@ class TestMemoryStoreRemove: def test_remove_no_match(self, store): result = store.remove("memory", "nonexistent") - assert result["success"] is False + assert result["success"] is True + assert result["result"] == "no_match" def test_remove_empty_old_text(self, store): result = store.remove("memory", " ") diff --git a/tools/memory_tool.py b/tools/memory_tool.py index 91924f66b..42189ab71 100644 --- a/tools/memory_tool.py +++ b/tools/memory_tool.py @@ -260,8 +260,12 @@ class MemoryStore: entries = self._entries_for(target) matches = [(i, e) for i, e in enumerate(entries) if old_text in e] - if len(matches) == 0: - return {"success": False, "error": f"No entry matched '{old_text}'."} + if not matches: + return { + "success": True, + "result": "no_match", + "message": f"No entry matched '{old_text}'. The search substring was not found in any existing entry.", + } if len(matches) > 1: # If all matches are identical (exact duplicates), operate on the first one @@ -310,8 +314,12 @@ class MemoryStore: entries = self._entries_for(target) matches = [(i, e) for i, e in enumerate(entries) if old_text in e] - if len(matches) == 0: - return {"success": False, "error": f"No entry matched '{old_text}'."} + if not matches: + return { + "success": True, + "result": "no_match", + "message": f"No entry matched '{old_text}'. The search substring was not found in any existing entry.", + } if len(matches) > 1: # If all matches are identical (exact duplicates), remove the first one @@ -449,30 +457,30 @@ def memory_tool( Returns JSON string with results. """ if store is None: - return json.dumps({"success": False, "error": "Memory is not available. It may be disabled in config or this environment."}, ensure_ascii=False) + return tool_error("Memory is not available. It may be disabled in config or this environment.", success=False) if target not in ("memory", "user"): - return json.dumps({"success": False, "error": f"Invalid target '{target}'. Use 'memory' or 'user'."}, ensure_ascii=False) + return tool_error(f"Invalid target '{target}'. Use 'memory' or 'user'.", success=False) if action == "add": if not content: - return json.dumps({"success": False, "error": "Content is required for 'add' action."}, ensure_ascii=False) + return tool_error("Content is required for 'add' action.", success=False) result = store.add(target, content) elif action == "replace": if not old_text: - return json.dumps({"success": False, "error": "old_text is required for 'replace' action."}, ensure_ascii=False) + return tool_error("old_text is required for 'replace' action.", success=False) if not content: - return json.dumps({"success": False, "error": "content is required for 'replace' action."}, ensure_ascii=False) + return tool_error("content is required for 'replace' action.", success=False) result = store.replace(target, old_text, content) elif action == "remove": if not old_text: - return json.dumps({"success": False, "error": "old_text is required for 'remove' action."}, ensure_ascii=False) + return tool_error("old_text is required for 'remove' action.", success=False) result = store.remove(target, old_text) else: - return json.dumps({"success": False, "error": f"Unknown action '{action}'. Use: add, replace, remove"}, ensure_ascii=False) + return tool_error(f"Unknown action '{action}'. Use: add, replace, remove", success=False) return json.dumps(result, ensure_ascii=False) @@ -539,7 +547,7 @@ MEMORY_SCHEMA = { # --- Registry --- -from tools.registry import registry +from tools.registry import registry, tool_error registry.register( name="memory",