#7163: fix #7151
agents
Cluster:
Error Handling Improvements
This PR fixes #7151
### Checklist
- [x] Mark as AI-assisted in the PR title or description - This is AI Assisted (Kimi K2.5)
- [x] Tested with unit tests, added new test too
- [x] Confirm you understand what the code does - Yes I do
### Root Cause
The regex pattern on line 344 of `src/agents/pi-embedded-helpers/errors.ts`:
`/incorrect role information|roles must alternate|400.*role|"message".*role.*information/i`
The `400.*role` pattern was too broad - it matched ANY error message containing "400" followed by "role" anywhere in the string.
This caused DeepSeek's legitimate 400 errors (like parameter validation errors that mention "role") to be incorrectly classified as role ordering conflicts.
### Fix
Changed the pattern from `400.*role` to `400.*incorrect.*role` to make it more specific and only match actual role ordering errors that contain both "400" and "incorrect" and "role".
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR narrows the role-ordering detection regex used by `formatAssistantErrorText` to avoid misclassifying certain DeepSeek `400 Bad Request` validation errors that include the word “role”. It also adds a regression test ensuring those errors no longer get rewritten to the “Message ordering conflict” UX message.
The change fits into the existing error-normalization flow in `src/agents/pi-embedded-helpers/errors.ts`, where specific known error shapes are detected early and rewritten to user-friendly, actionable messages; otherwise raw HTTP/JSON error payloads are formatted for display.
<h3>Confidence Score: 4/5</h3>
- This PR looks safe to merge; it’s a small, targeted regex tightening with a focused regression test.
- Review covered the full changed context in the error formatting pipeline. The main risk is behavioral: narrowing the regex may miss other 400-prefixed role-ordering variants, and the new test’s expectations could be brittle if output formatting changes. No compilation-risk changes were introduced.
- src/agents/pi-embedded-helpers/errors.ts (regex behavior/coverage), src/agents/pi-embedded-helpers.formatassistanterrortext.test.ts (test assertion stability)
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</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
#19451: fix(errors): surface provider hint for role-ordering failures
by rafaelipuente · 2026-02-17
82.7%
#12974: fix: intermittent (no output) reported by users
by vincentkoc · 2026-02-10
78.0%
#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversa...
by hleliofficiel · 2026-02-10
77.5%
#8175: fix: suppress raw API errors for non-owners
by Rakshi2609 · 2026-02-03
77.0%
#15109: fix: distinguish transient API errors from billing errors
by jwchmodx · 2026-02-13
76.5%
#5920: fix: DeepSeek API compatibility - convert developer role to system ...
by coupclawbot · 2026-02-01
74.4%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
74.1%
#7760: fix(agents): resolve message ordering conflict during tool execution
by aryan877 · 2026-02-03
74.1%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
74.1%
#6678: fix(agents): support thinking tags with attributes
by GHesericsu · 2026-02-01
74.0%