From 84d86bb1a588464a426ec976b811b3c5ee7ceccd Mon Sep 17 00:00:00 2001 From: Ahmed Butt Date: Sun, 12 Apr 2026 22:18:48 +0500 Subject: [PATCH] fix: correct write/codemap tool bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - create_file: added missing existence check (docstring promised it but code was silently overwriting existing files with open("w") mode) - create_file: guard os.makedirs against empty dirname when path has no directory component, which previously raised FileNotFoundError - get_function_implementation: return descriptive string instead of None when function is not found or file type is unsupported — LangChain tools must always return strings, not None - get_raw_file_content: added try/except so missing or unreadable files return an error string instead of crashing the agent mid-run Co-Authored-By: Claude Sonnet 4.6 --- agent/tools/codemap.py | 17 +++++++++++------ agent/tools/write.py | 10 +++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/agent/tools/codemap.py b/agent/tools/codemap.py index 8e0b0aa..88b41e6 100644 --- a/agent/tools/codemap.py +++ b/agent/tools/codemap.py @@ -100,7 +100,7 @@ def get_code_definitions(file_path: str) -> str: return "\n".join(output_lines) @tool(parse_docstring=True) -def get_function_implementation(file_path: str, function_name: str) -> Optional[str]: +def get_function_implementation(file_path: str, function_name: str) -> str: """ Extract the implementation of a specific function or method from a file. @@ -119,7 +119,7 @@ def get_function_implementation(file_path: str, function_name: str) -> Optional[ } lang = lang_map.get(suffix) if not lang: - return None + return f"Unsupported file type: {suffix}" # Initialize parser language = get_language(lang) @@ -178,7 +178,7 @@ def get_function_implementation(file_path: str, function_name: str) -> Optional[ return "\n".join(output_lines) - return None + return f"Function '{function_name}' not found in {file_path}" @tool(parse_docstring=True) def get_code_definitions_multi(file_paths: list[str]) -> str: @@ -202,12 +202,17 @@ def get_code_definitions_multi(file_paths: list[str]) -> str: def get_raw_file_content(file_path: str) -> str: """ Get the raw content of the file. good for a non-code files - + Args: file_path: file path to read """ - with open(file_path, "rb") as f: - return f.read().decode('utf-8') + try: + with open(file_path, "rb") as f: + return f.read().decode('utf-8') + except FileNotFoundError: + return f"Error: File '{file_path}' not found" + except Exception as e: + return f"Error reading file '{file_path}': {str(e)}" # List of available tools codemap_tools = [get_code_definitions, get_function_implementation, get_code_definitions_multi, get_raw_file_content] diff --git a/agent/tools/write.py b/agent/tools/write.py index 91ca99d..eb3f981 100644 --- a/agent/tools/write.py +++ b/agent/tools/write.py @@ -16,9 +16,13 @@ def create_file(path: str, content: str) -> str: str: A success message with the file path, or an error message if creation failed """ try: - # Check if file already exists - # Ensure the directory exists - os.makedirs(os.path.dirname(path), exist_ok=True) + if os.path.exists(path): + return f"Error: File {path} already exists. Use write_to_file to overwrite it." + + # Ensure the directory exists (only if there is a directory component) + dir_name = os.path.dirname(path) + if dir_name: + os.makedirs(dir_name, exist_ok=True) with open(path, "w", encoding="utf-8") as f: f.write(content)