#4922: fix(agents): ensure parallel tool results have correct parentId
agents
Cluster:
Error Handling in Agent Tools
Fixes parallel tool results getting chained parentIds instead of all pointing to assistant message. Causes API 400 errors.
Related: #4553 #4728 #4839
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes transcript parentId behavior for parallel tool results by branching back to the originating assistant message before appending each `toolResult` (including synthetic ones generated during flush). It adds tests to ensure multiple tool results from the same assistant message all share that assistant message as `parentId`, avoiding chained toolResult parents that can violate strict provider ordering (e.g., Anthropic’s requirement that `tool_result` blocks immediately follow the assistant message containing the `tool_use` blocks).
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, but has a plausible edge case that could re-parent unexpected tool results.
- Core change is localized and covered by new tests for the intended parallel tool-result behavior. The main concern is that branching currently applies to any `toolResult` while `assistantEntryId` is set, even if the result doesn’t correspond to a pending tool call, which could corrupt transcript structure in uncommon ordering scenarios.
- src/agents/session-tool-result-guard.ts (toolResult branching guard condition)
<!-- 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>
**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
#3880: fix: drop assistant messages with stopReason 'error' to avoid orpha...
by SalimBinYousuf1 · 2026-01-29
83.0%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
81.6%
#11825: fix: keep tool_use/tool_result pairs together during session compac...
by C31gordon · 2026-02-08
81.6%
#3622: fix(agents): drop orphan tool results
by mickobizzle · 2026-01-28
81.0%
#7525: Agents: skip errored tool calls during pairing
by justinhuangcode · 2026-02-02
80.8%
#9416: fix: drop errored/aborted assistant tool pairs in transcript repair
by xandorklein · 2026-02-05
80.3%
#2557: fix(agents): preserve tool call/result pairing in history limiting
by steve-rodri · 2026-01-27
80.2%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
80.2%
#7760: fix(agents): resolve message ordering conflict during tool execution
by aryan877 · 2026-02-03
79.4%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
79.4%