#21201: fix(compaction): repair tool_use/tool_result pairing after compaction
agents
size: XS
Cluster:
Tool Result Compaction Fixes
## Problem
Session compaction can drop or move toolResult messages in ways that leave `tool_use` blocks without matching `tool_result` blocks (or leave free-floating tool results). Strict providers (Anthropic-compatible APIs, Cloud Code Assist) reject the next request with a 400.
## Fix
After `session.compact()` completes, re-run `sanitizeToolUseResultPairing(session.messages)` (when transcript policy enables it) and replace session messages with the repaired transcript.
This is a post-compaction safety net that complements pre-compaction truncation/repair.
## Testing
- Compiles clean
- Verified logic is a pure transcript transformation already used in other truncation paths.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds post-compaction tool pairing repair to prevent 400 errors from strict providers when `tool_use` blocks are left without matching `tool_result` blocks after compaction. The fix applies the existing `sanitizeToolUseResultPairing` function (already used pre-compaction at line 594) to repair the transcript after `session.compact()` completes.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix reuses an existing, well-tested transcript repair function that's already applied pre-compaction. The conditional check matches the existing pattern (line 593-595), and the implementation is defensive (checks length > 0 before replacing). No new logic is introduced, only applying an existing safety net at an additional point in the flow.
- No files require special attention
<sub>Last reviewed commit: 2102876</sub>
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#16095: fix: remove orphaned tool_result blocks during compaction (#15691)
by claw-sylphx · 2026-02-14
89.4%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
84.0%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
82.2%
#13062: fix: remove orphaned tool_result blocks from user message content d...
by trevorgordon981 · 2026-02-10
82.2%
#6687: fix(session-repair): strip malformed tool_use blocks to prevent per...
by NSEvent · 2026-02-01
81.0%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
80.0%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
79.7%
#15143: fix(transcript-repair): validate tool call id and name to prevent G...
by GreyC · 2026-02-13
79.5%
#11825: fix: keep tool_use/tool_result pairs together during session compac...
by C31gordon · 2026-02-08
79.5%
#21195: fix: suppress orphaned tool_use/tool_result errors after session co...
by ruslansychov-git · 2026-02-19
79.1%