Skip to content

Resend a model step after a mid-stream gateway failure - #190

Merged
sarath-menon merged 1 commit into
mainfrom
fix/midstream-provider-retry
Nov 18, 2026
Merged

sarath-menon merged 1 commit into
mainfrom
fix/midstream-provider-retry

Conversation

@sarath-menon

@sarath-menon sarath-menon commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Cloud V4 BrowserCode runs fail when a provider error arrives after the reply started streaming (about 6 runs a day): the gateway sends it in shapes that parseStreamError does not recognise (the Chat Completions SDK passes only the message string, the Anthropic SDK passes {type: "api_error", message}, and Responses carries upstream_stream_failed without a transport prefix), so it becomes UnknownError and the run ends. With BCODE_STREAM_RETRY set (the Cloud worker sets it per project through the stream-retry release switch), those shapes map to the existing upstream_stream_failed transport recovery: at most two resends of the same request on the same model, never after a tool executed, removing only the failed attempts parts. Only the gateways transport prefixes and its three generic failure messages qualify, so refusals, request rejections and context overflow stay terminal; without the variable nothing changes. Red-green: a local table test over the exact production error values (packages/opencode/test/zz_midstream_retry_local.test.ts, kept out of the repo) fails 3/7 on main and passes 7/7 here; the existing gateway transport recovery tests pass 7/7 and typecheck is clean. Codex review found two over-broad matches (Anthropic and Responses permanent errors), both fixed and re-verified.

Changelog

  • Fix: Runs no longer fail when the model provider errors partway through a reply; the step is retried up to twice on the same model.

🤖 Generated with Claude Code


Summary by cubic

Fixes Cloud V4 BrowserCode runs failing when a provider error arrives after the reply started streaming. The model step is now retried up to twice on the same model instead of ending the run.

Bug Fixes

  • Maps mid-stream error shapes from Chat Completions (bare message), Anthropic (api_error/overloaded_error), and Responses (unprefixed upstream_stream_failed) to the existing upstream_stream_failed recovery.
  • Adds the BCODE_STREAM_RETRY flag to gate the new behavior; without it, nothing changes.
  • Keeps refusals, rejections, and context overflow terminal.

Written for commit 570d997. Summary will update on new commits.

View guided diff Turn on auto-fix

With BCODE_STREAM_RETRY set by the Cloud V4 worker, mid-stream gateway errors in the Chat Completions (bare message), Anthropic (api_error, overloaded_error) and unprefixed Responses shapes now take the existing same-route transport recovery: at most two resends, never after a tool executed. Content policy refusals and request rejections stay terminal; without the variable nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Collaborator Author

Red-green (local test packages/opencode/test/zz_midstream_retry_local.test.ts, not committed).

Red on main (3 production shapes not recognised):

27 |   ] as const) {
28 |     test(`${name}: resent at most twice on the same route when enabled`, () => {
29 |       process.env.BCODE_STREAM_RETRY = "1"
30 |       const error = MessageV2.fromError(input, { providerID: ProviderV2.ID.make("v4-gateway") })
31 |       expect(SessionRetry.isTransportError(error)).toBe(true)
                                                        ^
error: expect(received).toBe(expected)

Expected: true
Received: false

      at <anonymous> (/home/exedev/bc/packages/opencode/test/zz_midstream_retry_local.test.ts:31:52)
(fail) mid-stream provider errors > chat: resent at most twice on the same route when enabled [0.05ms]
(pass) mid-stream provider errors > chat: unchanged when disabled [0.04ms]
(pass) mid-stream provider errors > rejections are never retried [0.16ms]

 4 pass
 3 fail
 9 expect() calls
Ran 7 tests across 1 file. [3.25s]

Green on this branch:

(pass) mid-stream provider errors > responses: resent at most twice on the same route when enabled [1.30ms]
(pass) mid-stream provider errors > responses: unchanged when disabled [0.26ms]
(pass) mid-stream provider errors > anthropic: resent at most twice on the same route when enabled [0.11ms]
(pass) mid-stream provider errors > anthropic: unchanged when disabled [0.06ms]
(pass) mid-stream provider errors > chat: resent at most twice on the same route when enabled [0.06ms]
(pass) mid-stream provider errors > chat: unchanged when disabled [0.03ms]
(pass) mid-stream provider errors > rejections are never retried [0.38ms]

 7 pass
 0 fail
 17 expect() calls
Ran 7 tests across 1 file. [2.60s]

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/opencode/src/provider/error.ts Resolved
@sarath-menon
sarath-menon merged commit 451127a into main Nov 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant