Skip to content

fix(agents): normalize provider errors for better failover - #58856

Merged
steipete merged 2 commits into
openclaw:mainfrom
aaron-he-zhu:fix/provider-error-normalization
Apr 1, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
aaron-he-zhu:fix/provider-error-normalization

Conversation

@aaron-he-zhu

Copy link
Copy Markdown
Contributor

Fixes #58837

Related: #58347 #58305 #58377 #58392 (LiveSessionModelSwitchError cluster)

Summary

Add provider-specific error patterns for AWS Bedrock, Ollama, Mistral, Cohere, DeepSeek, Together AI, and Cloudflare Workers AI. These providers return errors in non-standard formats that the generic classifiers miss, causing incorrect failover behavior.

  • Create provider-error-patterns.ts with provider-specific matchers
  • matchesProviderContextOverflow() catches context overflow patterns the generic regex misses
  • classifyProviderSpecificError() maps provider errors to FailoverReason
  • Wire into isContextOverflowError() and classifyFailoverReason() as catch-all layers

Examples fixed:

Provider Error Was Now
Bedrock ThrottlingException unclassified rate_limit
Bedrock ModelNotReadyException unclassified overloaded
Groq model_is_deactivated unclassified model_not_found
Together AI concurrency limit reached unclassified rate_limit

Test plan

  • Unit tests for matchesProviderContextOverflow (8 provider samples + negatives)
  • Unit tests for classifyProviderSpecificError (5 provider mappings)
  • Integration tests with isContextOverflowError and classifyFailoverReason
  • Existing failover tests pass (35 tests)
  • tsgo --noEmit clean, oxlint clean, oxfmt --check clean

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Apr 1, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2308727178

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


// Together AI / Fireworks: specific rate limit messages
{
test: /\bconcurrency limit\b.*\breached\b/i,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Match concurrency-limit reached errors

The Together AI/Fireworks classifier only matches concurrency limit ... breached, but the provider message this change targets is commonly phrased as concurrency limit ... reached (including the new test case), so these rate-limit responses still fall through as unclassified. In that scenario, classifyFailoverReason returns null and the model-switch failover path is skipped when it should treat the error as rate_limit.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a new provider-error-patterns.ts module that captures provider-specific error message formats (AWS Bedrock, Ollama, Mistral, Cohere, Groq, Together AI, Cloudflare Workers AI) that the existing generic classifiers miss. The two exported functions — matchesProviderContextOverflow and classifyProviderSpecificError — are wired into isContextOverflowError() and classifyFailoverReason() as catch-all layers. The overall approach is clean and well-structured.

Key issues found:

  • Vertex context overflow mismatch (P1): The INVALID_ARGUMENT Vertex pattern is added only to PROVIDER_SPECIFIC_PATTERNS (returning \"format\") but not to PROVIDER_CONTEXT_OVERFLOW_PATTERNS. This means isContextOverflowError() still returns false for Vertex context overflow errors while classifyFailoverReason() returns \"format\". Any code path that calls isContextOverflowError() to trigger context compression will be bypassed for Vertex, potentially causing the wrong recovery strategy.
  • Overly broad model is not ready alternation (P1): The pattern /ModelNotReadyException|model is not ready/i would match generic phrases from any provider, not just Bedrock. This can cause non-Bedrock errors to be misclassified as \"overloaded\" and trigger indefinite retries against permanently unavailable models.
  • Stale JSDoc reference (P2): matchesProviderContextOverflow JSDoc says to call from isLikelyContextOverflowError(), but the actual caller is isContextOverflowError().
  • Unused provider field (P2): The ProviderErrorPattern type declares provider?: string for per-provider scoping but classifyProviderSpecificError never reads it.

Confidence Score: 4/5

Mostly safe to merge, but two P1 issues should be addressed: the Vertex context-overflow/format mismatch and the overly broad "model is not ready" pattern that could cause wrong failover behavior in production.

Two P1 findings remain: (1) Vertex INVALID_ARGUMENT context overflow is classified as "format" but not detected by isContextOverflowError(), which could silently bypass context-compression recovery logic; (2) the "model is not ready" alternation is broad enough to match non-Bedrock errors and cause infinite retries. The P2 findings are minor (stale JSDoc, unused field, missing test variant) and don't block merge on their own.

src/agents/pi-embedded-helpers/provider-error-patterns.ts — lines 60-62 (Vertex format mapping) and lines 72-74 (model is not ready broadness)

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 60-62

Comment:
**Vertex context overflow bypasses `isContextOverflowError()`**

The Vertex `INVALID_ARGUMENT` pattern is added to `PROVIDER_SPECIFIC_PATTERNS` (returning `"format"`) but is NOT added to `PROVIDER_CONTEXT_OVERFLOW_PATTERNS`. This creates a behavioral split: `classifyFailoverReason()` will return `"format"` for these errors, but `isContextOverflowError()` will still return `false`.

If the failover engine calls `isContextOverflowError()` to decide whether to compress the context before retrying, Vertex context overflow errors will never trigger that code path — they'll be treated as format errors and take the wrong recovery strategy instead.

To fix this, add the Vertex pattern to `PROVIDER_CONTEXT_OVERFLOW_PATTERNS` as well, or remove it from `PROVIDER_SPECIFIC_PATTERNS` entirely (leaving classification as `null`) until the two functions can be made consistent. Classifying it as `"format"` is semantically incorrect for a context overflow.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 72-74

Comment:
**`model is not ready` alternation is dangerously broad**

The second alternation `model is not ready` will match any error containing that phrase from any provider, not just AWS Bedrock. This phrase can appear in generic user-facing messages (e.g., a model that is still initializing in a local/Ollama deployment, or a message from a completely unrelated service). Classifying all such errors as `"overloaded"` — which implies transient overload with retry logic — could cause infinite retries against a model that is permanently unavailable.

Consider tightening this to require the Bedrock exception class prefix:
```suggestion
    test: /ModelNotReadyException/i,
```
If `model is not ready` is needed as a standalone pattern, it should at minimum require a Bedrock-specific namespace prefix (e.g., `bedrock.*model is not ready`) to avoid false positives.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 98-103

Comment:
**Stale JSDoc function name**

The JSDoc for `matchesProviderContextOverflow` references `isLikelyContextOverflowError()`, but the actual function in `errors.ts` that calls it is `isContextOverflowError()`. This will confuse anyone trying to trace the call chain.

```suggestion
 * Call this from `isContextOverflowError()` to catch provider-specific wording.
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 11-18

Comment:
**Unused `provider` field in `ProviderErrorPattern`**

The `provider?: string` field is declared and documented ("Optional: only match when this provider is in use"), but `classifyProviderSpecificError` never reads it — it only calls `pattern.test.test(errorMessage)`. This gives a false impression that patterns are scoped per-provider. Either implement the provider-scoping logic (pass the current provider into `classifyProviderSpecificError`) or remove the dead field to avoid misleading future contributors.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.test.ts
Line: 54-56

Comment:
**Test string doesn't match the PR's stated example**

The PR description lists the fixed Together AI example as `"concurrency limit reached"`, but the test uses `"concurrency limit has been reached"`. Both happen to match the pattern `/\bconcurrency limit\b.*\breached\b/i`, so the test passes. However, it would be more valuable to also test the exact string from the PR description table to ensure the documented real-world error is covered:

```suggestion
    expect(classifyProviderSpecificError("concurrency limit has been reached")).toBe("rate_limit");
    expect(classifyProviderSpecificError("concurrency limit reached")).toBe("rate_limit");
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(agents): normalize provider errors f..." | Re-trigger Greptile

Comment on lines +60 to +62
test: /INVALID_ARGUMENT.*(?:exceeds? the (?:maximum|max)|input.*too (?:long|large))/i,
reason: "format", // context overflow manifests as format error in Vertex
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Vertex context overflow bypasses isContextOverflowError()

The Vertex INVALID_ARGUMENT pattern is added to PROVIDER_SPECIFIC_PATTERNS (returning "format") but is NOT added to PROVIDER_CONTEXT_OVERFLOW_PATTERNS. This creates a behavioral split: classifyFailoverReason() will return "format" for these errors, but isContextOverflowError() will still return false.

If the failover engine calls isContextOverflowError() to decide whether to compress the context before retrying, Vertex context overflow errors will never trigger that code path — they'll be treated as format errors and take the wrong recovery strategy instead.

To fix this, add the Vertex pattern to PROVIDER_CONTEXT_OVERFLOW_PATTERNS as well, or remove it from PROVIDER_SPECIFIC_PATTERNS entirely (leaving classification as null) until the two functions can be made consistent. Classifying it as "format" is semantically incorrect for a context overflow.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 60-62

Comment:
**Vertex context overflow bypasses `isContextOverflowError()`**

The Vertex `INVALID_ARGUMENT` pattern is added to `PROVIDER_SPECIFIC_PATTERNS` (returning `"format"`) but is NOT added to `PROVIDER_CONTEXT_OVERFLOW_PATTERNS`. This creates a behavioral split: `classifyFailoverReason()` will return `"format"` for these errors, but `isContextOverflowError()` will still return `false`.

If the failover engine calls `isContextOverflowError()` to decide whether to compress the context before retrying, Vertex context overflow errors will never trigger that code path — they'll be treated as format errors and take the wrong recovery strategy instead.

To fix this, add the Vertex pattern to `PROVIDER_CONTEXT_OVERFLOW_PATTERNS` as well, or remove it from `PROVIDER_SPECIFIC_PATTERNS` entirely (leaving classification as `null`) until the two functions can be made consistent. Classifying it as `"format"` is semantically incorrect for a context overflow.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +72 to +74
test: /ModelNotReadyException|model is not ready/i,
reason: "overloaded",
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 model is not ready alternation is dangerously broad

The second alternation model is not ready will match any error containing that phrase from any provider, not just AWS Bedrock. This phrase can appear in generic user-facing messages (e.g., a model that is still initializing in a local/Ollama deployment, or a message from a completely unrelated service). Classifying all such errors as "overloaded" — which implies transient overload with retry logic — could cause infinite retries against a model that is permanently unavailable.

Consider tightening this to require the Bedrock exception class prefix:

Suggested change
test: /ModelNotReadyException|model is not ready/i,
reason: "overloaded",
},
test: /ModelNotReadyException/i,

If model is not ready is needed as a standalone pattern, it should at minimum require a Bedrock-specific namespace prefix (e.g., bedrock.*model is not ready) to avoid false positives.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 72-74

Comment:
**`model is not ready` alternation is dangerously broad**

The second alternation `model is not ready` will match any error containing that phrase from any provider, not just AWS Bedrock. This phrase can appear in generic user-facing messages (e.g., a model that is still initializing in a local/Ollama deployment, or a message from a completely unrelated service). Classifying all such errors as `"overloaded"` — which implies transient overload with retry logic — could cause infinite retries against a model that is permanently unavailable.

Consider tightening this to require the Bedrock exception class prefix:
```suggestion
    test: /ModelNotReadyException/i,
```
If `model is not ready` is needed as a standalone pattern, it should at minimum require a Bedrock-specific namespace prefix (e.g., `bedrock.*model is not ready`) to avoid false positives.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +98 to +103
/**
* Check if an error message matches any provider-specific context overflow pattern.
* Call this from `isLikelyContextOverflowError()` to catch provider-specific wording.
*/
export function matchesProviderContextOverflow(errorMessage: string): boolean {
return PROVIDER_CONTEXT_OVERFLOW_PATTERNS.some((pattern) => pattern.test(errorMessage));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Stale JSDoc function name

The JSDoc for matchesProviderContextOverflow references isLikelyContextOverflowError(), but the actual function in errors.ts that calls it is isContextOverflowError(). This will confuse anyone trying to trace the call chain.

Suggested change
/**
* Check if an error message matches any provider-specific context overflow pattern.
* Call this from `isLikelyContextOverflowError()` to catch provider-specific wording.
*/
export function matchesProviderContextOverflow(errorMessage: string): boolean {
return PROVIDER_CONTEXT_OVERFLOW_PATTERNS.some((pattern) => pattern.test(errorMessage));
* Call this from `isContextOverflowError()` to catch provider-specific wording.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 98-103

Comment:
**Stale JSDoc function name**

The JSDoc for `matchesProviderContextOverflow` references `isLikelyContextOverflowError()`, but the actual function in `errors.ts` that calls it is `isContextOverflowError()`. This will confuse anyone trying to trace the call chain.

```suggestion
 * Call this from `isContextOverflowError()` to catch provider-specific wording.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +11 to +18
type ProviderErrorPattern = {
/** Regex or string to match against the raw error message. */
test: RegExp;
/** The failover reason this pattern maps to. */
reason: FailoverReason;
/** Optional: only match when this provider is in use. */
provider?: string;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unused provider field in ProviderErrorPattern

The provider?: string field is declared and documented ("Optional: only match when this provider is in use"), but classifyProviderSpecificError never reads it — it only calls pattern.test.test(errorMessage). This gives a false impression that patterns are scoped per-provider. Either implement the provider-scoping logic (pass the current provider into classifyProviderSpecificError) or remove the dead field to avoid misleading future contributors.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.ts
Line: 11-18

Comment:
**Unused `provider` field in `ProviderErrorPattern`**

The `provider?: string` field is declared and documented ("Optional: only match when this provider is in use"), but `classifyProviderSpecificError` never reads it — it only calls `pattern.test.test(errorMessage)`. This gives a false impression that patterns are scoped per-provider. Either implement the provider-scoping logic (pass the current provider into `classifyProviderSpecificError`) or remove the dead field to avoid misleading future contributors.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +54 to +56
it("classifies concurrency limit as rate_limit", () => {
expect(classifyProviderSpecificError("concurrency limit has been reached")).toBe("rate_limit");
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test string doesn't match the PR's stated example

The PR description lists the fixed Together AI example as "concurrency limit reached", but the test uses "concurrency limit has been reached". Both happen to match the pattern /\bconcurrency limit\b.*\breached\b/i, so the test passes. However, it would be more valuable to also test the exact string from the PR description table to ensure the documented real-world error is covered:

Suggested change
it("classifies concurrency limit as rate_limit", () => {
expect(classifyProviderSpecificError("concurrency limit has been reached")).toBe("rate_limit");
});
expect(classifyProviderSpecificError("concurrency limit has been reached")).toBe("rate_limit");
expect(classifyProviderSpecificError("concurrency limit reached")).toBe("rate_limit");
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/provider-error-patterns.test.ts
Line: 54-56

Comment:
**Test string doesn't match the PR's stated example**

The PR description lists the fixed Together AI example as `"concurrency limit reached"`, but the test uses `"concurrency limit has been reached"`. Both happen to match the pattern `/\bconcurrency limit\b.*\breached\b/i`, so the test passes. However, it would be more valuable to also test the exact string from the PR description table to ensure the documented real-world error is covered:

```suggestion
    expect(classifyProviderSpecificError("concurrency limit has been reached")).toBe("rate_limit");
    expect(classifyProviderSpecificError("concurrency limit reached")).toBe("rate_limit");
```

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 29082578d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/\bdeepseek\b.*(?:input.*too long|context.*exceed)/i,

// Google Vertex / Gemini: INVALID_ARGUMENT with token-related messages is context overflow.
/INVALID_ARGUMENT.*(?:exceeds? the (?:maximum|max)|input.*too (?:long|large))/i,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Narrow Vertex INVALID_ARGUMENT overflow matching

The new Vertex/Gemini pattern INVALID_ARGUMENT.*(?:exceeds? the (?:maximum|max)|...) now treats any INVALID_ARGUMENT mentioning a maximum value as context overflow, including non-context validation errors such as parameter bounds (for example, temperature/topK limits). Because isContextOverflowError() now calls this matcher, these requests can be misreported as context-overflow and routed through overflow recovery paths instead of format/validation handling.

Useful? React with 👍 / 👎.

@aaron-he-zhu

Copy link
Copy Markdown
Contributor Author

CI failures are in test/extension-test-boundary.test.ts and src/cron/service.every-jobs-fire.test.ts, unrelated to this change. All failover/error classification tests pass (55/55).

@steipete

steipete commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

This seems incomplete and only additive, not refactoring the existing classifications.

Aaron Zhu and others added 2 commits April 1, 2026 21:28
Add provider-specific error patterns for AWS Bedrock, Ollama, Mistral,
Cohere, DeepSeek, Together AI, and Cloudflare Workers AI. These providers
return errors in non-standard formats that the generic classifiers miss,
causing incorrect failover behavior (e.g., context overflow misclassified
as format error, ThrottlingException not recognized as rate limit).

Wire provider patterns into isContextOverflowError() and
classifyFailoverReason() as catch-all layers after generic classifiers.
@steipete
steipete merged commit cd4b03c into openclaw:main Apr 1, 2026
@steipete

steipete commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.

  • Gate: pnpm check && pnpm build && pnpm test (plus post-rebase pnpm check and targeted failover tests)
  • Land commit: b157eae
  • Merge commit: cd4b03c

Thanks @aaron-he-zhu!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b157eaefa3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


// Together AI / Fireworks: specific rate limit messages
{
test: /\bconcurrency limit\b.*\breached\b/i,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match breached concurrency-limit errors

The Together/Fireworks pattern uses \breached\b, which in JS regex means a word boundary plus reached (not the literal word breached), so messages like "concurrency limit has been breached" do not match this classifier. In those cases classifyProviderSpecificError() returns null, and rate-limit failover is skipped when the error text only contains this provider-specific wording.

Useful? React with 👍 / 👎.

lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(agents): provider-specific errors misclassified during failover

2 participants