Skip to content

fix: strip orphaned tool_result messages after conversation truncation#416

Merged
yaojin3616 merged 1 commit intodataelement:mainfrom
suxinshuo:hotifx/tool_result_400
Apr 16, 2026
Merged

fix: strip orphaned tool_result messages after conversation truncation#416
yaojin3616 merged 1 commit intodataelement:mainfrom
suxinshuo:hotifx/tool_result_400

Conversation

@suxinshuo
Copy link
Copy Markdown
Contributor

@suxinshuo suxinshuo commented Apr 15, 2026

Summary

  • Fix HTTP 400 errors caused by orphaned tool_result blocks in the conversation history sent to the Anthropic API.
  • After truncating the conversation to ctx_size, strip any leading role="tool" messages whose corresponding assistant (tool_use) message was cut off by the truncation boundary.

Closes #415

Root Cause

Each tool_call DB record expands into 2 messages (assistant + tool) during history reconstruction, so conversation[-ctx_size:] can split a pair — keeping the tool result but dropping the tool use. The Anthropic API requires every tool_result to have a matching tool_use in the preceding assistant message.

Test plan

  • Reproduce with a session containing ~50 tool-call rounds and context_window_size = 100
  • Verify the LLM call succeeds after the fix
  • Verify normal sessions without tool calls are unaffected

@yaojin3616 yaojin3616 merged commit 4921623 into dataelement:main Apr 16, 2026
up-guillaume pushed a commit to lesmoires/Clawith that referenced this pull request Apr 20, 2026
Upstream changes integrated:
- v1.8.3-beta.2: A2A async communication (notify/consult/task_delegate)
- v1.8.3-beta.2: Multimodal image context persistence
- v1.8.3-beta.2: New search tools (Exa, DuckDuckGo, Tavily, Google, Bing)
- v1.8.3-beta.2: Drag-and-drop file upload UI
- dataelement#246: Security patches (4 critical vulnerabilities)
- dataelement#410: N+1 query fix for session list API
- dataelement#416: Tool call message pair preservation
- dataelement#437: Idempotent alembic migrations
- LLM refactoring (call_llm moved to services/llm/)

Fork customizations preserved:
- SSO relaxation in tenants.py
- Tuple guards in gateway.py
- session_id removal from execute_tool (caller.py)
- Hetzner Cloud tools in tool_seeder.py
- Infisical/AgentMail integrations
- LiteLLM MCP wrappers
- AgentBay multi-region + browser_latest fixes
- Custom extensions loader
- Agent credentials router

Conflict resolutions:
- agentbay_control.py: upstream 3-phase _get_client + our browser_latest fix
- agent_tools.py: our fork base + upstream A2A strip helper + search tools
- caller.py: removed session_id=session_id (NameError fix)
- websocket.py: retained our session_id removal
- tool_seeder.py: added parameters_schema + new search tools
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] HTTP 400: orphaned tool_result in conversation history due to dual truncation

2 participants