diff --git a/tests/agent/test_builtin_memory_disabled_surface.py b/tests/agent/test_builtin_memory_disabled_surface.py index 90bcf6db8e..7d6c00a697 100644 --- a/tests/agent/test_builtin_memory_disabled_surface.py +++ b/tests/agent/test_builtin_memory_disabled_surface.py @@ -220,6 +220,26 @@ class TestIndependentStoreWriteGates: assert result["success"] is False assert result["target"] == "memory" + def test_invalid_target_error_is_bounded_and_carries_recovery_hint(self, hermes_home): + """Model-supplied target is interpolated into the error: it must stay + capped at the shared tool_error bound and keep the recovery hint.""" + from tools.memory_tool import memory_tool + from tools.registry import _MAX_TOOL_ERROR_CHARS + + store = self._store(memory_enabled=True, user_profile_enabled=True) + result = json.loads( + memory_tool(action="add", target="x" * 10_000, content="fact", store=store) + ) + + assert result["success"] is False + assert len(result["error"]) <= _MAX_TOOL_ERROR_CHARS + 32 + + short = json.loads( + memory_tool(action="add", target="bogus", content="fact", store=store) + ) + assert short["success"] is False + assert "Use 'memory' or 'user'" in short["error"] + class TestExternalProviderSurvivesBuiltinDisable: """Dropping the built-in tool must not drop the external provider's tools. diff --git a/tools/memory_tool.py b/tools/memory_tool.py index 4832bcd4e9..54dcaa71a6 100644 --- a/tools/memory_tool.py +++ b/tools/memory_tool.py @@ -1216,7 +1216,14 @@ def check_memory_requirements() -> bool: def _memory_target_error(store: "MemoryStore", target: str) -> Optional[Dict[str, Any]]: """Return a shared validation error for an invalid or disabled target.""" if target not in {"memory", "user"}: - return {"success": False, "error": f"Invalid memory target '{target}'."} + from tools.registry import _bound_error_text + + return { + "success": False, + "error": _bound_error_text( + f"Invalid memory target '{target}'. Use 'memory' or 'user'." + ), + } if store.target_enabled(target): return None label = "USER.md" if target == "user" else "MEMORY.md"