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
pi with pi-tilldone extension 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.
Bug
pi-tilldoneextension crashes with a400 invalid_request_errorat long conversations (~message 182) when usingclaude-sonnet-4-6.Error
Root Cause
When conversation history gets long, pi's context window management prunes a
tool_usemessage from history but keeps the correspondingtool_resultblock. Claude's API then rejects the request because it finds an orphanedtool_result(toolu_01LgY35a93nhAdnmSvZA8a9C) with no matchingtool_usein the preceding message.Steps to Reproduce
piwithpi-tilldoneextension enabledclaude-sonnet-4-6Environment
pi-tilldone.tsclaude-sonnet-4-6messages.182.content.0Expected Behavior
Context truncation should prune
tool_use+ its correspondingtool_resulttogether, or strip orphanedtool_resultblocks before sending to the API.Suggested Fix
Before sending message history to the API, validate that every
tool_resultblock has a matchingtool_useblock in the preceding message. Remove anytool_resultblocks that are orphaned due to context truncation.