foreground-fallback infinite loop when quota-exhausted error is misclassified as retryable

Author: joshua-zyyCreated Aug 6, 2026Updated Sep 21, 2026
Labelsgood-to-code

Bug Report

Summary

When a provider's quota/usage is exhausted (a non-transient error), the foreground-fallback hook enters an infinite re-fallback loop: the fixer agent session repeatedly aborts and re-prompts every ~10 seconds, and the session never completes or fails. The fallback chain is never exhausted; the manager just resets the tried-set and retries forever.

Environment

  • Plugin version: 2.2.10
  • opencode version: 1.18.14
  • OS: Windows
  • Config: fallback chain ["openai/gpt-5.6-luna", "ollama-cloud/glm-5.2"] for the fixer agent (array form of presets.<name>.<agent>.model)

Root cause

  1. Quota-exhausted errors are classified as retryable. RETRYABLE_ERROR_PATTERNS in src/hooks/foreground-fallback/index.ts includes /usage limit/i (and similar patterns like /quota.?exceeded/i, /usage.?exceeded/i, /monthly usage limit/i). A "usage limit has been reached" error is therefore treated exactly like a transient 429, even though it will not recover for hours.

    Actual error observed:

    stream error providerID=openai modelID=gpt-5.6-luna agent=fixer
    error.error="AI_APICallError: The usage limit has been reached"
  2. The chain-exhaustion path loops forever instead of aborting. In execFallback (dist/index.js ~26527-26554):

    javascript
    let nextModel = chain.find((m) => !tried.has(m));
    if (!nextModel) {
      if (chain.length > 1) {
        // resetting tried set for re-fallback ...
        tried = new Set;
        tried.add(primary);
        if (currentModel && currentModel !== primary) tried.add(currentModel);
        this.sessionTried.set(sessionID, tried);
        nextModel = stickyFallback;   // chain[chain.length - 1]
      } else { ... abort ... }
    }

    After both models in the chain have been tried, instead of aborting, it resets the tried-set and re-prompts with the sticky fallback model — unconditionally and without any counter/limit. Meanwhile the re-prompt is issued with the agent's configured model (the chain head gpt-5.6-luna), which fails again, re-triggering fallback. This produces the observed infinite loop.

    Log evidence (oh-my-opencode-slim log, repeated every ~10s forever):

    [foreground-fallback] resetting tried set for re-fallback {"sessionID":"ses_...","agentName":"fixer","currentModel":"ollama-cloud/glm-5.2","prevTried":["openai/gpt-5.6-luna","ollama-cloud/glm-5.2"],"nextModel":"ollama-cloud/glm-5.2"}
    [foreground-fallback] switched to fallback model {"sessionID":"ses_...","agentName":"fixer","from":"ollama-cloud/glm-5.2","to":"ollama-cloud/glm-5.2"}

    Interleaved with opencode log:

    stream error providerID=openai modelID=gpt-5.6-luna session.id=ses_... agent=fixer mode=subagent error.error="AI_APICallError: The usage limit has been reached"
    prompt_async failed sessionID=ses_... cause="Cause([Die(UnknownError: UnknownError)])"

Expected behavior

  • Non-transient errors (quota/usage exhausted, budget exceeded, "usage limit reached") should either abort the session with a clear error, or trigger at most one pass through the fallback chain and then surface the error to the user — never loop.
  • The sticky re-fallback path needs an upper bound (e.g. N attempts, or only when the error is truly transient like a 429), otherwise any persistent failure in a multi-model chain hangs the session indefinitely.

Suggested fix direction

  1. Split "usage/quota exhausted" patterns out of RETRYABLE_ERROR_PATTERNS (or add a separate NON_TRANSIENT_ERROR_PATTERNS checked first in isFailoverError).
  2. Add a per-session fallback attempt counter; once the full chain has been cycled, abort and surface the last error instead of resetting the tried-set.

Impact

  • Sessions hang indefinitely (the user must manually cancel).
  • With a quota-exhausted model anywhere in a chain, the whole chain becomes unusable because the sticky fallback keeps re-prompting the failed model.

Source: alvinunreal/oh-my-opencode-slim