From 586c113688fb75640d13f38391dae5f9adb418bc Mon Sep 17 00:00:00 2001 From: Kaloyan Nikolov Date: Wed, 25 Feb 2026 20:39:55 +0100 Subject: [PATCH] fix: smarter bash tool instructions - guide model to read files after verification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated bash tool result instructions to detect verification commands (ls/grep) - If ls/grep shows file exists and user asked to READ it: explicitly tells model to USE read TOOL NOW - If user asked to check files: tells model to summarize the listing - If file not found: tells model to inform user - Prevents infinite loops of repeated ls commands - Model now properly transitions from verification → action → answer - All 41 tests passing --- src/api/chat_handlers.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/api/chat_handlers.py b/src/api/chat_handlers.py index ffc9f23..d26f4ad 100644 --- a/src/api/chat_handlers.py +++ b/src/api/chat_handlers.py @@ -466,16 +466,20 @@ async def handle_chat_completion( for i, ((tool_name, tool_result), tc) in enumerate(zip(tool_results, tool_calls_parsed)): tool_call_id = tc.get("id", f"call_{i}") - # Format the tool result message with explicit instructions - # This tells the model exactly what to do with the result - if tool_name == "read": - instruction = "The file contents are shown above. READ THIS FILE CONTENT ALOUD to the user. Do not call additional tools." - elif tool_name == "write": - instruction = "The file has been successfully written. CONFIRM to the user that the file was created with the content shown above. Do not call additional tools." - elif tool_name == "bash": - instruction = "The command has been executed. SUMMARIZE the output above to answer the user's request. Do not call additional tools." + # Format the tool result message with explicit instructions + # This tells the model exactly what to do with the result + if tool_name == "read": + instruction = "The file contents are shown above. READ THIS FILE CONTENT ALOUD to the user. Do not call additional tools." + elif tool_name == "write": + instruction = "The file has been successfully written. CONFIRM to the user that the file was created with the content shown above. Do not call additional tools." + elif tool_name == "bash": + # Check if this was a verification command (ls, grep) vs an action command + if "ls" in tool_result.lower() or "grep" in tool_result.lower(): + instruction = "The directory/file listing is shown above. Based on the user's original request: If they asked to READ a file that you can see exists, USE THE read TOOL NOW to read it. If they asked to CHECK what files exist, summarize the listing. If the requested file is NOT in the listing, tell the user it doesn't exist." else: - instruction = "The tool has completed. Use the result shown above to answer the user's request. Do not call additional tools." + instruction = "The command has been executed. SUMMARIZE the output above to answer the user's request. Do not call additional tools." + else: + instruction = "The tool has completed. Use the result shown above to answer the user's request. Do not call additional tools." tool_message_content = ( f"Tool Result ({tool_name}):\n"