fix(agent): reset _fallback_index at turn start even when no fallback activated

Original commit 33528b428 by konsisumer targeted _restore_primary_runtime
in pre-refactor run_agent.py. The body now lives in
agent/agent_runtime_helpers.restore_primary_runtime — re-applied there.

Fixes #20465

Co-authored-by: konsisumer <der@konsi.org>
This commit is contained in:
teknium1 2026-05-16 23:41:45 -07:00
parent aa05ffba53
commit 4ab9a06a51
No known key found for this signature in database

View file

@ -819,6 +819,14 @@ def restore_primary_runtime(agent) -> bool:
``gateway/run.py``), so this restoration IS needed there too.
"""
if not agent._fallback_activated:
# Reset the chain index even when no fallback was activated this
# turn. Without this, a turn where _try_activate_fallback() was
# called but returned False (chain exhausted or provider not
# configured) leaves _fallback_index >= len(_fallback_chain) while
# _fallback_activated stays False. The next turn skips this block
# entirely, stranding the index and silently blocking all future
# fallback attempts for the session. Fixes #20465.
agent._fallback_index = 0
return False
if getattr(agent, "_rate_limited_until", 0) > time.monotonic():