Skip to content

fix: distinguish stale-stream from APIConnectionError in fallback gate (#69186) - #69419

Closed
aurorabotticus-svg wants to merge 1 commit into
NousResearch:mainfrom
aurorabotticus-svg:fix/69186-api-connection-error-fallback
Closed

aurorabotticus-svg wants to merge 1 commit into
NousResearch:mainfrom
aurorabotticus-svg:fix/69186-api-connection-error-fallback

Conversation

@aurorabotticus-svg

Copy link
Copy Markdown
Contributor

Fixes #69186

Problem

The eager fallback gate groups two materially different failure sources:

_is_transport_failure = classified.reason in {
    FailoverReason.timeout,
    FailoverReason.overloaded,
}
_should_fallback = (
    is_rate_limited
    or (_is_transport_failure and retry_count >= 2)
)

FailoverReason.timeout covers both stale-stream kills (expensive, each retry costs minutes) and ordinary APIConnectionError (DNS failure, TCP reset — returns in milliseconds). Two fast connection failures trigger fallback before api_max_retries is exhausted, skipping _try_recover_primary_transport() entirely.

Fix

Use the stale-stream detector's own counter (agent._consecutive_stale_streams) to distinguish the two cases:

_is_stale_stream = (
    classified.reason == FailoverReason.timeout
    and getattr(agent, "_consecutive_stale_streams", 0) > 0
)
_is_overloaded = classified.reason == FailoverReason.overloaded
_is_transport_failure = _is_stale_stream or _is_overloaded

_should_fallback = (
    is_rate_limited
    or (_is_overloaded and retry_count >= 2)
    or (_is_stale_stream and retry_count >= 1)
)

Ordinary APIConnectionError now flows through: retry → _try_recover_primary_transport() (client/pool rebuild, retry_count reset) → fresh retry cycle → fallback only if recovered primary still fails.

What stays unchanged:

Verification

  • py_compile AST parse passes
  • The _consecutive_stale_streams attribute is only set by the stale-stream detector in chat_completion_helpers.py — fast APIConnectionError does not bump it, so the gate cleanly separates the two cases

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint area/streaming Streaming responses: gateway delivery, provider wire needs-decision Awaiting maintainer decision before any implementation labels Jul 22, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Related to #69190 for #69186, but not a duplicate: this patch switches stale streams after one retry, while #69190 retains the two-retry threshold. Please choose the intended stale-stream fallback policy.

@aurorabotticus-svg

Copy link
Copy Markdown
Contributor Author

Closing — duplicate of existing PR. Apologies for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/streaming Streaming responses: gateway delivery, provider wire comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint needs-decision Awaiting maintainer decision before any implementation P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Fast APIConnectionError bypasses primary transport recovery and falls back after two failures

2 participants