#3880: fix: drop assistant messages with stopReason 'error' to avoid orphaning tool results (#3860)
agents
Cluster:
Error Handling in Agent Tools
This PR fixes issue #3860 where assistant messages with stopReason 'error' (e.g., from content filtering) would have synthetic tool results added by the transcript repair logic. These tool results would then become orphaned if the provider filtered out the errored assistant message on subsequent requests, leading to 400 errors.
The fix ensures that assistant messages with an error stop reason are dropped during transcript repair, preventing the addition of synthetic tool results for them.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `repairToolUseResultPairing` to drop assistant messages whose `stopReason` is `"error"`, preventing transcript-repair from inserting synthetic tool results that could later become orphaned if a provider filters the errored assistant turn (the #3860 scenario). The change fits into the existing transcript sanitizer’s responsibilities: keeping tool calls/results strictly adjacent and removing free-floating toolResult entries so strict providers don’t reject requests.
Main follow-ups: avoid the new `as any` access for `stopReason` and add a regression test covering an errored assistant toolCall turn to ensure the fix stays in place.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and addresses a real provider-compatibility failure mode.
- The change is small and localized to transcript repair logic. Main concerns are maintainability (new `as any`) and missing regression coverage for the new behavior, rather than functional correctness of the fix itself.
- src/agents/session-transcript-repair.ts (type-safety + add targeted test coverage)
<!-- greptile_other_comments_section -->
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
88.4%
#9416: fix: drop errored/aborted assistant tool pairs in transcript repair
by xandorklein · 2026-02-05
87.8%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
86.4%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
85.7%
#16966: fix: strip tool_use blocks from aborted/errored assistant messages
by StressTestor · 2026-02-15
84.5%
#4700: fix: deduplicate tool_use IDs and enable sanitization for Anthropic
by marcelomar21 · 2026-01-30
84.4%
#6687: fix(session-repair): strip malformed tool_use blocks to prevent per...
by NSEvent · 2026-02-01
83.4%
#7525: Agents: skip errored tool calls during pairing
by justinhuangcode · 2026-02-02
83.3%
#8270: fix: support snake_case 'tool_use' in transcript repair (#8264)
by heliosarchitect · 2026-02-03
83.2%
#4922: fix(agents): ensure parallel tool results have correct parentId
by jduartedj · 2026-01-30
83.0%