#4844: fix(agents): skip error/aborted assistant messages in transcript repair
agents
Cluster:
Error Handling in Agent Tools
## Summary
When an assistant response is interrupted mid-tool-call (`stopReason: error` or `aborted`), `repairToolUseResultPairing()` was still extracting tool calls and inserting synthetic `tool_result` entries. This caused orphan `tool_result` blocks after `transformMessages()` dropped the errored assistant, leading to permanent session corruption with `unexpected tool_use_id` API errors.
## Changes
- Modified `repairToolUseResultPairing()` in `src/agents/session-transcript-repair.ts` to skip tool call extraction for `stopReason === "error" || "aborted"` assistant messages
- Added 4 new unit tests covering error/aborted scenarios
## Root Cause
Two safety mechanisms interacted badly:
1. `session-tool-result-guard` inserts synthetic `toolResult` for orphaned tool calls
2. `transformMessages()` drops error/aborted assistant messages but **not** their corresponding `toolResult`
This created orphaned `tool_result` entries that referenced non-existent `tool_use` blocks.
## Testing
- All 8 tests in `session-transcript-repair.test.ts` pass
- Related tests in `session-tool-result-guard.test.ts` and `pi-embedded-runner.guard.test.ts` pass
## Fixes
- #4600 - Terminated assistant with toolCall causes infinite rejection loop
- #4597 - handle stopReason 'aborted' in transcript repair
- #4475 - Session corruption when tool call aborted mid-stream
- #4814 - repairToolUseResultPairing failed to catch orphaned tool_use
- #4815 - repairToolUseResultPairing failed to catch orphaned tool_use
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `repairToolUseResultPairing()` to avoid extracting tool calls (and thus avoid inserting synthetic `toolResult` entries) from assistant messages with `stopReason: "error"` or `"aborted"`. This prevents creating orphan `toolResult` blocks later when other sanitization (`transformMessages()`) drops those assistant turns but leaves tool results behind.
Unit tests were added to ensure error/aborted assistant messages do not trigger synthetic `toolResult` insertion, while normal tool-using assistants are still repaired as before.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and directly addresses a real session-corruption failure mode.
- The change is small, localized, and covered by new unit tests for the targeted stopReason cases. Main remaining uncertainty is behavioral: skipping errored/aborted assistants could drop legitimate tool results that followed those turns, which may or may not be desired depending on how transcripts are recorded in practice.
- src/agents/session-transcript-repair.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#9416: fix: drop errored/aborted assistant tool pairs in transcript repair
by xandorklein · 2026-02-05
92.4%
#7525: Agents: skip errored tool calls during pairing
by justinhuangcode · 2026-02-02
91.2%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
91.1%
#3880: fix: drop assistant messages with stopReason 'error' to avoid orpha...
by SalimBinYousuf1 · 2026-01-29
88.4%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
88.0%
#15050: fix: transcript corruption resilience — strip aborted tool_use bloc...
by yashchitneni · 2026-02-12
87.8%
#16966: fix: strip tool_use blocks from aborted/errored assistant messages
by StressTestor · 2026-02-15
87.6%
#14328: fix: strip incomplete tool_use blocks from errored/aborted messages...
by Kropiunig · 2026-02-12
87.5%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
87.2%
#8270: fix: support snake_case 'tool_use' in transcript repair (#8264)
by heliosarchitect · 2026-02-03
86.7%