From 33396a522aaf76d5da758666e63310e2a078894a Mon Sep 17 00:00:00 2001 From: themavik Date: Fri, 20 Feb 2026 23:52:40 -0500 Subject: [PATCH 1/2] fix(tools): provide detailed error messages in edit_file when old_text not found Uses difflib to find the best match and shows a helpful diff, making it easier to debug edit_file failures. Co-authored-by: Cursor --- nanobot/agent/tools/filesystem.py | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 419b088..d9ff265 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -150,7 +150,7 @@ class EditFileTool(Tool): content = file_path.read_text(encoding="utf-8") if old_text not in content: - return f"Error: old_text not found in file. Make sure it matches exactly." + return self._not_found_message(old_text, content, path) # Count occurrences count = content.count(old_text) @@ -166,6 +166,39 @@ class EditFileTool(Tool): except Exception as e: return f"Error editing file: {str(e)}" + @staticmethod + def _not_found_message(old_text: str, content: str, path: str) -> str: + """Build a helpful error when old_text is not found.""" + import difflib + + lines = content.splitlines(keepends=True) + old_lines = old_text.splitlines(keepends=True) + + best_ratio = 0.0 + best_start = 0 + window = len(old_lines) + + for i in range(max(1, len(lines) - window + 1)): + chunk = lines[i : i + window] + ratio = difflib.SequenceMatcher(None, old_lines, chunk).ratio() + if ratio > best_ratio: + best_ratio = ratio + best_start = i + + if best_ratio > 0.5: + best_chunk = lines[best_start : best_start + window] + diff = difflib.unified_diff( + old_lines, best_chunk, + fromfile="old_text (provided)", tofile=f"{path} (actual, line {best_start + 1})", + lineterm="", + ) + diff_str = "\n".join(diff) + return ( + f"Error: old_text not found in {path}.\n" + f"Best match ({best_ratio:.0%} similar) at line {best_start + 1}:\n{diff_str}" + ) + return f"Error: old_text not found in {path}. No similar text found. Verify the file content." + class ListDirTool(Tool): """Tool to list directory contents.""" From e0edb904bd337cf5a3aaf675f39627d022043377 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Sat, 21 Feb 2026 06:35:10 +0000 Subject: [PATCH 2/2] style(filesystem): move difflib import to top level --- nanobot/agent/tools/filesystem.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index d9ff265..e6c407e 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -1,5 +1,6 @@ """File system tools: read, write, edit.""" +import difflib from pathlib import Path from typing import Any @@ -169,8 +170,6 @@ class EditFileTool(Tool): @staticmethod def _not_found_message(old_text: str, content: str, path: str) -> str: """Build a helpful error when old_text is not found.""" - import difflib - lines = content.splitlines(keepends=True) old_lines = old_text.splitlines(keepends=True)