← Back to PRs

#4922: fix(agents): ensure parallel tool results have correct parentId

by jduartedj open 2026-01-30 21:22 View on GitHub →
agents
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