#6464: fix: trigger model failover on malformed tool-call JSON
agents
Cluster:
Error Handling in Agent Tools
## Summary
- Adds detection for JSON parse errors to trigger model failover
- Covers OpenAI-style errors ("is not valid JSON")
- Covers Node JSON.parse errors ("Unexpected token")
- Covers truncated JSON errors ("Unexpected end of JSON input")
- Adds test coverage for new error patterns
## Test plan
- [x] Unit tests pass (`pnpm test src/agents/pi-embedded-helpers.classifyfailoverreason.test.ts`)
- [x] Linting passes (`pnpm lint`)
- [x] Formatting passes (`pnpm format`)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR extends the model failover classification logic to treat malformed tool-call JSON parse errors as a `format` failover reason, and adds unit coverage for several common error message variants (OpenAI-style “is not valid JSON”, Node `JSON.parse` “Unexpected token …”, and truncated JSON “Unexpected end of JSON input”). The change plugs a gap where tool-call argument parse failures wouldn’t previously trigger a retry/failover path, improving robustness when models emit malformed tool argument payloads.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, with a small risk of over-triggering failover on non-parse errors.
- Change is localized to failover classification and backed by targeted tests, but the new regex is broad enough that it may classify some non-JSON-parse failures as `format`, causing unnecessary failovers in edge cases.
- src/agents/pi-embedded-helpers/errors.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#20534: fix: handle malformed tool call JSON parse errors gracefully
by echoVic · 2026-02-19
80.4%
#10178: fix: trigger fallback when model resolution fails with unknown model
by Yida-Dev · 2026-02-06
79.1%
#15815: Fallback LLM doesn't trigger if primary model is local
by shihanqu · 2026-02-13
78.0%
#12314: fix: treat HTTP 5xx server errors as failover-worthy
by hsssgdtc · 2026-02-09
77.8%
#21491: fix: classify Google 503 UNAVAILABLE as transient failover [AI-assi...
by ZPTDclaw · 2026-02-20
77.2%
#4097: fix: classify AWS SSO token errors as auth for model fallback (AI-a...
by guyelia · 2026-01-29
76.9%
#9427: fix: trigger model fallback on all 4xx HTTP errors
by dbottme · 2026-02-05
76.6%
#21152: fix(agents): throw FailoverError for unknown model so fallback chai...
by Mellowambience · 2026-02-19
76.0%
#7229: fix: add network error resilience to agentic loop failover
by ai-fanatic · 2026-02-02
75.8%
#23648: fix: detect truncated file paths from partial JSON streaming
by davidemanuelDEV · 2026-02-22
75.7%