← Back to PRs

#13062: fix: remove orphaned tool_result blocks from user message content during compaction (#13020)

by trevorgordon981 open 2026-02-10 02:52 View on GitHub →
docs commands agents stale
## Summary This PR fixes issue #13020 where tool_result blocks embedded within user message content arrays were not being removed when their corresponding tool_use blocks were dropped during conversation compaction. ## Problem When OpenClaw compacts conversation history: 1. It may drop assistant messages containing tool_use blocks 2. But leaves corresponding tool_result blocks embedded in user message content arrays 3. This causes API errors: `unexpected tool_use_id found in tool_result blocks` The existing repair logic only handled separate `toolResult` messages, not tool_result blocks within user message content arrays (which is how Anthropic's API actually formats them). ## Solution Enhanced `repairToolUseResultPairing` to handle both formats: - Separate messages with role "toolResult" (existing behavior) - tool_result blocks within user message content arrays (new behavior) ### Implementation Details 1. **First pass**: Collect all valid tool_use IDs from assistant messages 2. **Process user messages**: Remove orphaned tool_result blocks from content arrays 3. **Track orphans**: Count removed blocks in `droppedOrphanCount` for diagnostics ## Testing Added comprehensive test coverage in `compaction-13020.test.ts`: - ✅ Removes orphaned tool_result blocks from user message content - ✅ Keeps tool_result blocks when their tool_use is also kept - ✅ Handles mixed content (text + tool_results) correctly - ✅ All existing compaction tests still pass ## Changes - `src/agents/session-transcript-repair.ts`: Added logic to handle tool_result blocks in user content - `src/agents/compaction-13020.test.ts`: New test file with comprehensive coverage Fixes #13020 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR primarily fixes transcript compaction by extending `repairToolUseResultPairing` to remove orphaned `tool_result` blocks embedded inside `user` message content arrays (not just standalone `toolResult` messages). It adds targeted tests reproducing the API error scenario when a chunk containing the assistant `toolUse` is dropped but the user’s `tool_result` block remains. In addition, the PR introduces a new `doctor-memory` diagnostic that warns when memory search embeddings are enabled but no provider/API keys/local model are configured, and it adds a new cache infrastructure module (manager, LRU provider, and integrations for model-response and web-search caching). <h3>Confidence Score: 3/5</h3> - This PR is mergeable after addressing a couple of correctness issues in newly added diagnostics/caching code. - The core compaction/transcript repair change is straightforward and covered by new tests, but the PR also introduces new doctor-memory and cache modules where there are at least two concrete logic issues: a `.trim()` call that can throw on malformed config types in a diagnostic path, and a key-shape mismatch in model-response similarity lookups that makes the similarity path ineffective (or not aligned with the stored entries). - src/commands/doctor-memory.ts, src/infra/cache/integrations/model-response-cache.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs