From 62a1bf4c553c4c6ea3da15f725d0f1fb2677f374 Mon Sep 17 00:00:00 2001 From: kyssta-exe Date: Tue, 9 Jun 2026 02:13:09 +0000 Subject: [PATCH] fix(tools): return previews on zero-match in replace/remove to prevent memory retry loops (#42405) - replace() and remove() now return entry previews and current_entries when no entry matches old_text, matching the multi-match and add-limit error behavior - add() limit error also now returns previews for consistency - Agent can self-correct after a failed replace/remove instead of looping blindly until turn budget is exhausted with no user response --- tools/memory_tool.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/memory_tool.py b/tools/memory_tool.py index 96a8a1a0a..17b52e3c3 100644 --- a/tools/memory_tool.py +++ b/tools/memory_tool.py @@ -330,6 +330,7 @@ class MemoryStore: if new_total > limit: current = self._char_count(target) + previews = [e[:80] + ("..." if len(e) > 80 else "") for e in entries] return { "success": False, "error": ( @@ -339,6 +340,7 @@ class MemoryStore: f"shorter ones or 'remove' stale or less important entries (see " f"current_entries below), then retry this add — all in this turn." ), + "previews": previews, "current_entries": entries, "usage": f"{current:,}/{limit:,}", } @@ -372,7 +374,13 @@ class MemoryStore: matches = [(i, e) for i, e in enumerate(entries) if old_text in e] if not matches: - return {"success": False, "error": f"No entry matched '{old_text}'."} + previews = [e[:80] + ("..." if len(e) > 80 else "") for e in entries] + return { + "success": False, + "error": f"No entry matched '{old_text}'. Check the previews below and retry with the exact text of the entry you want to replace.", + "previews": previews, + "current_entries": entries, + } if len(matches) > 1: # If all matches are identical (exact duplicates), operate on the first one @@ -429,7 +437,13 @@ class MemoryStore: matches = [(i, e) for i, e in enumerate(entries) if old_text in e] if not matches: - return {"success": False, "error": f"No entry matched '{old_text}'."} + previews = [e[:80] + ("..." if len(e) > 80 else "") for e in entries] + return { + "success": False, + "error": f"No entry matched '{old_text}'. Check the previews below and retry with the exact text of the entry you want to remove.", + "previews": previews, + "current_entries": entries, + } if len(matches) > 1: # If all matches are identical (exact duplicates), remove the first one