Skip to content

Retry a Luna stream cut short once, and tell the reader their model is unavailable - #581

Merged
neuromechanist merged 8 commits into
developfrom
feature/issue-578-luna-stream-failures
Oct 7, 2026
Merged

neuromechanist merged 8 commits into
developfrom
feature/issue-578-luna-stream-failures

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Refs #578.
This makes the chat and ask streams survive the Luna failure mode and report it honestly.
It does not fix Luna itself, which is upstream.

What the evidence says

The four production error ids in the issue are all ConnectionError: Incomplete Bedrock response stream: missing messageStop event, not throttling.
CloudWatch (AWS/Bedrock, us-east-2, us.openai.gpt-6-luna) shows zero client errors, server errors and throttles from Oct 3 to Oct 6.
It also shows 8 invocations between 01:25 and 01:40 UTC on Oct 6 that finished in about 0.45 s with no error, where a real Luna answer takes 15 to 27 s.
So Bedrock closed those streams almost at once and counted them as successes.

The develop request that hung for 122 s (ea0c685f) was a different failure: a stall until our 120 s read timeout.
It surfaced as urllib3.exceptions.ReadTimeoutError, which botocore lets out raw from an open stream and classify_model_error did not recognize.
It was logged as an unexpected error with no retryable field.

Changes

  • Classify the stall. urllib3 timeout and protocol errors are model failures when they came up through langchain-aws, and the detail says urllib3. so they are not confused with botocore's classes of the same names.
  • Retry once. astream_events_with_retry runs the graph a second time after about a second when the first try failed fast (within 10 s) and the reader has been shown nothing: no text, reasoning, tool call or tool result, and no finished model call.
    It applies to /ask, /chat and /chat/resume.
    Only connection and unavailable failures are retried.
    Timeouts are not (they have already waited out their limit) and neither are throttles (see below).
    The first failure is logged with its type, kind and text; a failure that survives the retry is logged at ERROR and its metrics detail says (after one retry); a retry that works is logged at INFO.
  • Say what happened. A model that failed in a way that is not the request's fault now gets "The current model (name) is not available right now. Please choose another model."
    There is no automatic fallback to another model, by decision: it would change what a community chose and what a request costs.
    Failures that are not a model's keep their wording, and a refused request or key keeps its own.
  • Changelog entries under [Unreleased].

Decisions to review

  • Throttles are not retried here. botocore and the Anthropic SDK already retry one with backoff before the response begins, and a second try would only add load to the account being throttled.
    LiteLLM does not retry, so an OpenRouter throttle is not retried by anyone; that is pinned by a test.
  • A pre-stream 5xx from Bedrock or Anthropic is retried on top of the client's own backoff.
    A persistent outage costs up to twice the requests for those.
    Narrowing to failures that arrive after the response began is possible, at the price of a more complex classifier.
  • Reasoning counts as output.
    If Luna can stream reasoning and then drop messageStop, that case is not retried.
    The Bedrock evidence above says these streams close before any content, and invocation logging (below) will show if that is wrong.
  • A failed first attempt's tokens are not recorded, because a failed call emits no usage event.
    A retried success is visible in the INFO log, not in the metrics row.
  • The widget's 60 s "no data" check sits before reader.read() and cannot fire during a silent wait, so a stall still takes up to the 120 s read timeout to reach the reader.
    I left both alone: high-effort reasoning can legitimately be silent for a long time.
  • The non-streaming /ask and /chat paths are unchanged and still return a generic 500 for a provider failure.

Outside the repo

Bedrock model invocation logging was turned on for account 358982197581 in us-east-2: CloudWatch Logs group /osa/bedrock-invocations, 14-day retention, role OsaBedrockInvocationLogging, text delivery on.
It is account-wide and stores full prompts and answers; delete-model-invocation-logging-configuration turns it off.

Testing

  • The new tests drive the real graph, the real botocore Bedrock client with staged bytes (including a stream that dies part way with urllib3's own error, and a second request that succeeds), the real Anthropic and OpenRouter clients, and both streams.
  • The retry, the window, the throttle exclusion, the progress guards, the langchain-aws requirement and the retry report each fail when their change is removed.
  • CI on the final commit: Test (3.11), Test (3.12), Lint, Frontend Tests and the Docker build all pass.
  • Locally, the full suite on this branch differs from a clean checkout of develop only by network checks that pass on rerun: the same 19 test_chat_resume_endpoint.py tests fail with 401 on both.
  • The review ran every aspect (code, tests, error handling, comments, types, simplification) and its findings are fixed, apart from the decisions listed above.

A Bedrock stream that stalled past the read timeout surfaced as urllib3's
ReadTimeoutError, which botocore lets out raw while iterating the response.
classify_model_error only knew botocore's class, so the failure was logged as an
unexpected error with no retryable field (issue 578). urllib3 timeouts and
protocol errors are now the model call's when they came up through botocore.

Tested: new cases drive the real Bedrock client with a staged stream that raises
mid-body; they fail without the change. test_model_errors.py passes.
Bedrock can end a stream almost at once with no messageStop event (GPT-6 Luna
did, issue 578) or answer a throttle; botocore does not retry once the response
has begun, so each reached the reader as an error. The ask and chat streams now
run the graph once more when the failure can clear by itself and fails fast,
and only while the reader has seen no text, tool call or tool result and no
model call has finished. A read timeout is not retried, since it has already
spent the whole timeout.

Tested: real graph and real Bedrock client with staged streams, on both
endpoints; the retry tests and the no-retry guards fail when the change or the
guard is removed. test_stream_failures.py and test_model_errors.py pass.
When a model call fails in a way that can clear by itself (throttle, stream cut
short, stall, dropped connection) and the retry did not help, the reader got
"An error occurred while processing your request." The error event now says
the current model is not available and asks them to choose another. There is no
automatic switch, which would change what a community chose and what a request
costs. Failures that are not a model call's (a tool of ours) keep the generic
text, and a refused request or credential keeps its own.

Tested: the provider-failure cases in test_stream_failures.py now assert the new
text on both endpoints, the generic text is still asserted for non-model
failures; 149 pass across test_stream_failures.py and test_model_errors.py.
Review findings on the retry:
- retry only a failure that arrives within 10 s, so a slow failure does not
  double the reader's wait; wait a jittered second, not a fixed one
- retry only unavailable and connection failures; a throttle is already
  retried with backoff by the clients, and a second try only adds load
- match urllib3 errors only when they came up through langchain-aws, and name
  urllib3 in the detail so it is not confused with botocore's class
- treat a chunk it does not recognize as output, close the inner stream
  deterministically, and log a cancel during the wait
- log the first failure's type, kind and text, the chat session, and when the
  second try worked; a failure that survived a retry is logged at ERROR and its
  metrics detail says so
- correct docstrings the review found stale or inaccurate

Tested: new cases for the window, the throttle exclusion, a dropped connection,
the ERROR record, cancellation during the wait, and what counts as output; the
window and throttle cases fail when their change is removed. 524 pass across
the affected test files.
- the retry outcome is a required argument and the single record of whether a
  second try was made; the failure detail's retried flag has no default
- one traceback-frame check serves both _raised_in and _passed_through
- the wording branch reads as one conditional; trim docstrings that repeated
  each other
- tests: typed staged answers (a misspelled key now fails), shared CUT_SHORT and
  dropped/stalled stream errors, one real-client helper for the classifier
  tests, one retry-log filter, and the unrecognized-wording constant renamed to
  match its field

Tested: 1706 pass across test_stream_failures.py and tests/test_core; ruff
clean.
- a service error inside a Bedrock stream, OpenRouter 503, and an Anthropic
  overloaded error are answered by the second try; a throttle (in-stream and
  OpenRouter) is left alone, as decided
- the resume path sends the same request twice, carrying the browser's result,
  and labels the retry with its own endpoint
- a provider failure nobody recognizes is called unavailable and claims nothing
  about retrying; one that survives a retry is logged at ERROR
- each kind of output (a tool call alone, reasoning alone, a tool after a model
  that does not stream) ends the chance to retry
- a disconnect during the wait releases the session's turn and starts no second
  call; a urllib3 error through botocore alone is not the model's
- the retry log is found by its own field, and the unbounded-retry case fails
  fast instead of hanging

Tested: 1727 pass (11 skipped) across tests/test_core and test_stream_failures.py;
the langchain-aws requirement and the earlier guards fail when removed.
@neuromechanist neuromechanist added bug Something isn't working operations Operations, monitoring, and observability labels Oct 6, 2026
Type design review findings on RetryOutcome:
- the helper read its caller's out-parameter as control state, so an instance
  that was reused skipped the retry and logged a success that never happened;
  it now keeps its own local state and only writes the report
- the flag was sticky, so a later, unrelated failure in a run that had been
  retried was logged at ERROR as "after one retry"; it is now set only when the
  failure raised came from the second try before that try produced any output
- renamed RetryOutcome to RetryState (outcome collides with the model_outcome
  and turn_outcome modules) and its field to failed_after_retry
- tests: the classifier's tables are walked to keep kind and retryable
  consistent, and every kind is covered by the retry-policy cases; the wire
  helper's defaults are defined once and its docstring says its answers are
  indexed by transport request
- docstrings: worth_retrying_now reflowed and tied to retryable;
  _FailureWording says it holds the two messages that differ by stream

Tested: 1737 pass (11 skipped) across tests/test_core and test_stream_failures.py;
the reuse and per-failure label tests fail when their change is removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working operations Operations, monitoring, and observability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant