fix(agent): reset _fallback_index when previous turn exhausted chain without activating (#17446)#17824
Closed
yelog wants to merge 1 commit into
Closed
Conversation
…without activating (NousResearch#17446) When _try_activate_fallback() fails to activate any fallback (e.g. all chain entries return fb_client is None due to misconfigured credentials, or all entries have invalid provider/model), the index is incremented through the chain via recursion but _fallback_activated is never flipped to True. The session ends with _fallback_index == len(_fallback_chain) and _fallback_activated == False. On the next turn, _restore_primary_runtime() returned early at the `if not self._fallback_activated` guard without resetting the index. Every subsequent turn's _try_activate_fallback() then short-circuited at the bounds check and returned False immediately, so the user saw '⚠️ Non-retryable error — trying fallback...' followed by silence, the broken primary kept being sent on the wire, and the session was permanently pinned to the failing model until the session JSON was hand-edited. Reproducible scenario from the issue: Telegram bot with fallback_model: google/gemini-2.0-flash-001 (valid), user runs /model deepseek/deepseek-v4 (invalid OpenRouter ID). First turn fails with 400, fallback activation fails (chain entry valid but state machine wedged), every subsequent turn permanently aborts. Fix: when _restore_primary_runtime() returns early because there's no fallback to roll back, still reset _fallback_index to 0 if it has drifted past 0. This guarantees a fresh chain attempt on every turn that begins on the primary, regardless of whether the previous turn's fallback path succeeded, partially advanced the index, or fully exhausted the chain. Adds three regression tests covering: - the buggy state (chain exhausted + activated False) is now reset - no-op when index is already 0 (no spurious writes) - end-to-end: after reset, a fresh _try_activate_fallback() succeeds
This was referenced May 1, 2026
Contributor
|
This appears to be implemented on current main. Automated hermes-sweeper review found the PR's functional fix present in the refactored helper location rather than the original Evidence:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fix #17446 — sessions get permanently pinned to a broken primary model after one fallback-activation failure, even when a perfectly valid `fallback_model` is configured. Every subsequent turn prints "⚠️ Non-retryable error — trying fallback..." but the fallback never actually engages.
Why
`_try_activate_fallback()` (run_agent.py) increments `_fallback_index` before attempting activation and only flips `_fallback_activated = True` on the success path. If every chain entry fails (e.g. `fb_client is None` from a misconfigured credential, invalid provider/model, or any exception during client resolution), the recursion exhausts the chain leaving:
```
_fallback_index == len(_fallback_chain) # past the end
_fallback_activated == False # never succeeded
```
On the next turn, `run_conversation()` calls `_restore_primary_runtime()` at the top of the loop. The old guard returned early on `not self._fallback_activated`, leaving the stale index. The next `_try_activate_fallback()` then short-circuits at the `if self._fallback_index >= len(...)` bounds check, returning False instantly — so the runner announces fallback, sends nothing different, and the user sees the same primary error forever.
Reproducible scenario from the issue:
How
One-liner functional fix in `_restore_primary_runtime()`: when the early-return path runs (no fallback to roll back), still reset `_fallback_index` to 0 if it has drifted past 0. Guarantees a fresh chain attempt on every turn that begins on the primary, regardless of whether the previous turn's fallback path succeeded, partially advanced, or fully exhausted the chain.
How to test
```
scripts/run_tests.sh tests/run_agent/test_primary_runtime_restore.py
scripts/run_tests.sh tests/run_agent/test_provider_fallback.py tests/run_agent/test_compressor_fallback_update.py tests/run_agent/test_switch_model_fallback_prune.py tests/agent/test_credential_pool_routing.py
```
Three new regression tests in `test_primary_runtime_restore.py`:
All 34 tests in the file pass; 35 related fallback tests across 4 files also green.
Platforms
Affects all platforms (CLI, gateway, every messaging adapter) — the bug is in core agent state, not platform-specific code.