-
Notifications
You must be signed in to change notification settings - Fork 179
pi-tilldone: 400 error at long context — orphaned tool_result after context truncation (Sonnet 4.6) #12
Description
Bug
pi-tilldone extension crashes with a 400 invalid_request_error at long conversations (~message 182) when using claude-sonnet-4-6.
Error
Error: Connection error.
Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.182.content.0: unexpected `tool_use_id` found in `tool_result` blocks: toolu_01LgY35a93nhAdnmSvZA8a9C. Each `tool_result` block must have a corresponding `tool_use` block in the previous message."},"request_id":"req_011CYWa6adk52dCS45D9XZDb"}
Root Cause
When conversation history gets long, pi's context window management prunes a tool_use message from history but keeps the corresponding tool_result block. Claude's API then rejects the request because it finds an orphaned tool_result (toolu_01LgY35a93nhAdnmSvZA8a9C) with no matching tool_use in the preceding message.
Steps to Reproduce
- Use
piwithpi-tilldoneextension enabled - Model:
claude-sonnet-4-6 - Run a long agentic session (hits around message 182 in context)
- Extension crashes with the 400 above
Environment
- pi-coding-agent (latest as of Feb 2026)
- Extension:
pi-tilldone.ts - Model:
claude-sonnet-4-6 - Failure point:
messages.182.content.0
Expected Behavior
Context truncation should prune tool_use + its corresponding tool_result together, or strip orphaned tool_result blocks before sending to the API.
Suggested Fix
Before sending message history to the API, validate that every tool_result block has a matching tool_use block in the preceding message. Remove any tool_result blocks that are orphaned due to context truncation.