#18908: fix(payloads): place tool warnings before assistant reply
channel: mattermost
agents
size: S
Cluster:
Tool Execution and Error Handling
## Summary
- Reorder payload items so tool failure warnings (`⚠️ ... failed`) appear **before** the assistant's final reply text in the output array
- Previously, the warning was appended after the reply, making it the last (most visible) message in channels like Telegram that deliver messages sequentially — this overshadowed the actual answer
- Now the assistant's reply is always the last item sent, ensuring it's the most visible
- Dedup logic extended to check `answerTexts` in addition to already-emitted items, preserving the behavior where identical warning text in the assistant output prevents duplicate messages
Closes #18687
## Test plan
- [x] Updated e2e test expectation: "shows mutating tool errors before assistant output for visibility" verifies `[warning, reply]` order
- [x] `pnpm build` passes (TypeScript + lint)
- [x] All existing payload tests remain compatible (dedup, suppression, recoverable errors, etc.)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Reorders the `buildEmbeddedRunPayloads` output so tool failure warnings (`⚠️ ... failed`) appear **before** the assistant's reply text, making the final answer the last (most visible) message in sequential-delivery channels like Telegram.
- Moved the `answerTexts` processing loop to after the tool-error-warning block, so warnings are appended to `replyItems` first
- Pre-computed `hasUserFacingAssistantReply` via `.some()` since the decision needs to happen before answer items are pushed
- Extended dedup logic with a new `duplicateInAnswers` check against raw `answerTexts`, compensating for answer texts not yet being in `replyItems` at dedup time
- Updated the e2e test to verify `[warning, reply]` ordering and added a changelog entry
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minimal risk — it's a well-scoped reordering of output items with preserved dedup semantics and updated tests.
- The change is focused and well-tested. The reordering logic is straightforward: tool warnings are pushed before answer texts instead of after. The pre-computed hasUserFacingAssistantReply preserves the same boolean semantics. The extended duplicateInAnswers check correctly compensates for answer texts not yet being in replyItems. One minor subtlety — the new dedup compares against raw answerTexts rather than parseReplyDirectives-cleaned text — is inconsequential for the warning text format. All existing tests pass and the updated test verifies the new ordering.
- No files require special attention
<sub>Last reviewed commit: f9a9b28</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17552: fix(agents): suppress tool error warnings when assistant already re...
by AytuncYildizli · 2026-02-15
82.2%
#22087: Preserve assistant reply when exec fails under suppressToolErrors
by graysurf · 2026-02-20
78.7%
#19235: fix(telegram): tool error warnings no longer overwrite streamed rep...
by gatewaybuddy · 2026-02-17
78.0%
#19375: telegram: align tool-error summaries
by NorthyIE · 2026-02-17
77.1%
#19632: fix: suppressToolErrors now suppresses exec tool failure notifications
by Gitjay11 · 2026-02-18
75.7%
#20382: fix: move suppressToolErrors check before mutating tool check
by klawdius-noodle · 2026-02-18
74.7%
#18992: fix: suppress spurious tool error warnings for read-only exec commands
by Phineas1500 · 2026-02-17
74.0%
#19406: fix(heartbeat): filter error payloads from heartbeat reply selection
by namabile · 2026-02-17
73.7%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
73.6%
#13501: fix: extend assistant text dedup across message boundaries
by shakir-abdo · 2026-02-10
73.3%