fix(agent): add protocol validation for tool_use finish reason (#827)#1003
Open
kianwoon wants to merge 1 commit intoqwibitai:mainfrom
Open
fix(agent): add protocol validation for tool_use finish reason (#827)#1003kianwoon wants to merge 1 commit intoqwibitai:mainfrom
kianwoon wants to merge 1 commit intoqwibitai:mainfrom
Conversation
…wibitai#827 Add defensive validation in agent-runner to detect protocol violations where LLM returns stop_reason="tool_use" but includes zero tool_use blocks. Changes: - Add hasToolUseBlocks() helper to detect tool_use content blocks - Add validateAssistantResponse() for protocol violation detection - Track last assistant message content for validation - Enhanced result processing with protocol state logging Note: This is a defensive check. The complete fix requires SDK-level changes in the EZ loop where stop_reason is directly available. Related: qwibitai#827
Author
Implementation NotesThis PR adds defensive validation at the agent-runner level to detect protocol violations where What This Fix Does
Why This Is ImportantWhen the LLM API signals it wants to use tools (
Current LimitationsThe agent-runner is a consumer of the Claude Agent SDK and doesn't have direct access to the raw API
For a Complete FixThe ideal fix would be in the SDK's EZ loop (core agent loop) where // Pseudocode for SDK-level fix
if finish_reason == FinishReason::ToolUse && pending_tool_calls.is_empty() {
return AgentTermination::Error("Protocol violation: tool_use finish reason with no tool calls");
}This would catch the violation at the source before it reaches consumer code. Testing
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #827 - Add defensive validation in agent-runner to detect protocol violations where the LLM returns
stop_reason="tool_use"but includes zero tool_use blocks.Changes
hasToolUseBlocks()helper to detect tool_use content blocks in assistant messagesvalidateAssistantResponse()for protocol violation detectionBackground
When the LLM API returns a finish reason indicating tool use (
stop_reason="tool_use") but the response contains notool_useblocks, this is a protocol violation. The agent loop should treat this as an error, not accept it as success.This can happen due to:
Technical Details
The SDK's EZ loop (core agent loop) should ideally handle this validation where
stop_reasonis directly available. Since the agent-runner is a consumer of the SDK and only sees processed messages, this implementation adds:A complete fix would require SDK-level changes in the EZ loop to check
pending_tool_calls.is_empty()whenfinish_reason == ToolUseand return an appropriate error.Testing
npm run build)Related