#3622: fix(agents): drop orphan tool results
agents
Cluster:
Error Handling in Agent Tools
## Summary
Drops orphan toolResult messages that arrive without a matching pending tool call, preventing corrupted transcripts that can trigger repeated "No tool call found" loops.
## Details
- Adds a guard option (default enabled) to ignore orphan toolResult messages.
- Wires the option through the guard wrapper.
- Adds a test to cover the orphan-drop behavior.
## Why
A session can end up with a toolResult persisted without its toolCall (e.g., interrupted tool call). Replaying that session repeatedly hits the error and can burn tokens. This prevents new corruption at persistence time.
## Tests
- `pnpm vitest src/agents/session-tool-result-guard.test.ts`
## Issue
Closes #3621
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR extends the session tool-result guard to optionally drop “orphan” `toolResult` messages (default enabled) that don’t correspond to a currently-pending tool call, and threads the option through the guard wrapper. It also adds a vitest case asserting that an orphan `toolResult` is ignored.
Overall this fits the existing guard’s responsibility (normalizing transcript/tool-call pairing and optionally synthesizing missing results) and should help prevent persistence-time corruption loops when replaying sessions.
<h3>Confidence Score: 4/5</h3>
- This PR looks safe to merge and is narrowly scoped to transcript persistence behavior.
- Changes are small and localized (an additional guard branch plus wiring and a targeted test). Main functional concern is that malformed `toolResult` messages without an id are still persisted, which may still allow corruption in some edge cases, but the default orphan-drop should mitigate the reported loop for id-bearing orphans.
- src/agents/session-tool-result-guard.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**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
#7525: Agents: skip errored tool calls during pairing
by justinhuangcode · 2026-02-02
85.0%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
84.0%
#22011: fix(transcript): drop empty toolCallId toolResults during persisten...
by sauerdaniel · 2026-02-20
83.2%
#21195: fix: suppress orphaned tool_use/tool_result errors after session co...
by ruslansychov-git · 2026-02-19
82.8%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
82.4%
#9011: fix(session): auto-recovery for corrupted tool responses [AI-assisted]
by cheenu1092-oss · 2026-02-04
81.9%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
81.9%
#9416: fix: drop errored/aborted assistant tool pairs in transcript repair
by xandorklein · 2026-02-05
81.8%
#4922: fix(agents): ensure parallel tool results have correct parentId
by jduartedj · 2026-01-30
81.0%
#20538: fix: handle orphaned tool_result errors gracefully instead of leaki...
by echoVic · 2026-02-19
80.8%