Fix tool_use/tool_result mismatch error when resuming after clarification
Browse files- Add validation in Claude client to remove dangling tool_use blocks
- Recreate fresh agent instances when resuming after clarification
- Prevents stale conversation state from causing API errors
Files changed (4)
12345# HuggingFace Spaces Dockerfile for CodePilot# BUILD_VERSION: 12 (v3.3.3 simple coder - only task + plan)FROM python:3.11-slim# Set working directory12345# HuggingFace Spaces Dockerfile for CodePilot# BUILD_VERSION: 13 (v3.3.4 resume-fix - fix tool_use/tool_result bug)FROM python:3.11-slim# Set working directory
19 unmodified lines202122232425262719 unmodified lines# ============================================================# STARTUP VERSION CHECK - Change this to detect if rebuild worked# ============================================================APP_VERSION = "3.3.3-simple-coder"BUILD_ID = "2024-12-19-v11"print("=" * 60)print(f"[STARTUP] CodePilot Chainlit App")print(f"[STARTUP] APP_VERSION: {APP_VERSION}")19 unmodified lines202122232425262719 unmodified lines# ============================================================# STARTUP VERSION CHECK - Change this to detect if rebuild worked# ============================================================APP_VERSION = "3.3.4-resume-fix"BUILD_ID = "2024-12-19-v12"print("=" * 60)print(f"[STARTUP] CodePilot Chainlit App")print(f"[STARTUP] APP_VERSION: {APP_VERSION}")
8 unmodified lines9101112131415283 unmodified lines2993003013023033048 unmodified lines"""# VERSION CHECK - If you see this, new code is running!ORCHESTRATOR_VERSION = "3.3.3-simple-coder"print(f"[ORCHESTRATOR] ========== LOADING VERSION {ORCHESTRATOR_VERSION} ==========")from enum import Enum283 unmodified linesReturns:Result dict from continued workflow"""self.provide_user_answers(user_answers)return self._run_full_workflow(self.context.task_description)8 unmodified lines9101112131415283 unmodified lines2993003013023033043053063073083093103113128 unmodified lines"""# VERSION CHECK - If you see this, new code is running!ORCHESTRATOR_VERSION = "3.3.4-resume-fix"print(f"[ORCHESTRATOR] ========== LOADING VERSION {ORCHESTRATOR_VERSION} ==========")from enum import Enum283 unmodified linesReturns:Result dict from continued workflow"""print(f"[ORCHESTRATOR] Resuming after clarification...")# BUGFIX: Recreate agent instances to ensure fresh conversation state# This prevents any stale tool_use/tool_result state from previous runsself.planner = PlannerAgent(model="claude-sonnet-4-5-20250929")self.coder = CoderAgent(model="claude-sonnet-4-5-20250929")self.reviewer = ReviewerAgent(model="claude-sonnet-4-5-20250929")self.provide_user_answers(user_answers)return self._run_full_workflow(self.context.task_description)
121 unmodified lines122123124125126127121 unmodified lines"content": pending_tool_results})# Build request parametersrequest_params = {"model": self.model,121 unmodified lines122123124125126127128129130131132133134135136137138139140141142143121 unmodified lines"content": pending_tool_results})# BUGFIX: Validate no dangling tool_use blocks at end of conversation# Claude API requires tool_result immediately after tool_useif conversation_messages:last_msg = conversation_messages[-1]if last_msg.get("role") == "assistant":content = last_msg.get("content", [])if isinstance(content, list):has_tool_use = any(isinstance(b, dict) and b.get("type") == "tool_use"for b in content)if has_tool_use:# Dangling tool_use - remove this message to prevent API errorprint(f"[CLAUDE] Warning: Removing dangling assistant message with tool_use (no tool_result)")conversation_messages = conversation_messages[:-1]# Build request parametersrequest_params = {"model": self.model,
Dockerfile+1-1
chainlit_app.py+2-2
codepilot/agents/orchestrator.py+9-1
codepilot/llm/claude_client.py+16