fix(memory): bound invalid-target error and restore recovery hint

Route the model-supplied target through _bound_error_text so a huge
bogus target can't bloat context, and restore the "Use 'memory' or
'user'" hint. Follow-up to HexLab98's review note on the salvage.
This commit is contained in:
Teknium
2026-08-20 20:02:00 -07:00
parent 2cf7b36e11
commit 7c9285aa14
2 changed files with 28 additions and 1 deletions

View File

@@ -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.

View File

@@ -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"