Skip to content

fix(transport): honor generate fallback when provider stream rejects upgrade - #154

Merged
patlux merged 3 commits into
patlux:mainfrom
karaaslanz:fix/provider-upgrade-stream-rejection-20261013
Oct 15, 2026
Merged

patlux merged 3 commits into
patlux:mainfrom
karaaslanz:fix/provider-upgrade-stream-rejection-20261013

Conversation

@karaaslanz

@karaaslanz karaaslanz commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The transport router recognizes HTTP 403 with error code upgrade_required and switches from the Provider API to Generate. Current supported Pi/OMP adapters emit an error event and end their stream, which already reaches the existing fallback. This PR adds defense in depth for alternative/future adapters whose async iterator rejects after the failed HTTP response: without the guard, that rejection exits run() before fallback. This is not a reproduced failure in the supported host adapters.

Change

  • Catch Provider stream iterator failures only around the iteration.
  • If (and only if) the Provider response was already classified as upgrade_required, continue to the existing Generate fallback path. Other errors still propagate unchanged.
  • Add hermetic regression coverage for a Provider stream throwing after 403 upgrade_required, plus a negative case for an unrelated HTTP 403 stream rejection.

Verification

  • On unchanged upstream main b61093de3f866fc533f827a25a27bc342e9d678d, the added regression fails: expected done, got error (17/18 passing).
  • On this branch: npm run test:transport 18/18 PASS, npm run typecheck PASS, changed-file Prettier PASS, git diff --check PASS.
  • The change is scoped to src/transport.ts and tests/test-transport.ts; no credentials or external requests are needed.
  • Full project suite and live authenticated Provider/Generate calls were not run locally. On exact PR HEAD 1349045, upstream GitHub CI SUCCESS (9/9 jobs): formatting, typecheck/test, Semgrep, Gitleaks, CodeQL, npm audit, dependency review, lifecycle and Oh My Pi compatibility. The separate memory benchmark is tracked independently.

Maintainer follow-up (2026-10-15)

  • Updated the existing branch with current main, retaining the contributor commits.
  • Strengthened the throwing-adapter regression: provider events emitted after the inspected 403 remain suppressed; both the initial fallback and remembered Generate route emit exactly one start and only Generate text/final content.
  • The unrelated-403 case now asserts the original error message and no Generate events.
  • Full local npm test passed with Pi 1.3.0 and OMP 18.11.1 required against isolated loopback mocks, plus format and diff checks. No live account requests were made.

@karaaslanz
karaaslanz marked this pull request as ready for review October 14, 2026 08:44
pierreraby
pierreraby previously approved these changes Oct 14, 2026

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at exact head 1349045af792af4dd7b6d18a413b6e3066c1cfa7 against base b61093de3f866fc533f827a25a27bc342e9d678d.

Verification (isolated worktree, npm ci)

  • npm run typecheck PASS, npm run test:transport 18/18 PASS, npm run test:unit exit 0 (392 checks, 0 failures), Prettier check on both changed files PASS, git diff --check PASS.
  • RED proven on base: with only the two tests from this PR applied to b61093d, test:transport is 17/18 — the new fallback test fails ("expected done, got error"), and it is GREEN at this head. The negative test (unrelated 403) is green on both.
  • An independent fresh-context review reached the same conclusion on control flow: the swallow is gated exactly on upgradeRequired (per-call closure state, no cross-call leak), transport/apiKey staleness guards are preserved, and output.end() still runs exactly once.

Reachability of the claimed failure mode (offline probe, mock fetch, mock key)

I wired the real transport router around the real provider stream implementation from @earendil-works/pi-ai (compat streamSimple), with a mocked 403 {"error":{"code":"upgrade_required"}}, for all three wires Command Code resolves (openai-responses, openai-completions, anthropic-messages), on pi-ai 1.0.4, 1.1.0 and 1.2.0. Result, identical on every version and wire:

  • the provider stream never rejects its async iterator; it pushes an {type:"error"} event and ends (AssistantMessageEventStream[Symbol.asyncIterator] has no throw path);
  • with the unmodified base src/transport.ts, that event is already dropped by the existing if (!upgradeRequired) guard and the router already reaches the Generate fallback (streamGenerate called, getTransport() === "generate", final done).

OMP's @oh-my-pi/pi-ai (18.4.2) has the same shape (catch → stream.push({type:"error"}) → stream.end()).

So I could not reproduce "provider streams may reject their async iterator after the failed HTTP response" with any supported host tested. If you have a concrete repro — a host/pi-ai version or provider implementation that does reject the iterator — could you share it? Otherwise please reword the comment/PR description as defense-in-depth. Not blocking: the guard is narrow, unrelated errors still propagate, and the change is future-proof for other peer-accepted pi-ai shims.

Non-blocking notes

  1. (P2) No CHANGELOG.md bullet. RELEASE.md lets the release PR own the dated section, so this is conventional — just make sure this fallback hardening is listed there (it turns a terminal error into a Generate response).
  2. (P2) Reachability unproven on the supported host (details above).
  3. (P3) The two new tests assert only the terminal shape and call counts; they never assert the collected events are the generate stream's events (e.g. exactly one start, generate text). A future leak of partial provider events before fallback would still pass. assert.equal(first.filter((e) => e.type === "start").length, 1) would close it.
  4. (P3) The swallowed rejection drops the original provider error with no diagnostic; if the Generate fallback then fails, the report only shows the generate-side error. Pre-existing pattern (the dropped error event behaves the same), report-only.
  5. (P3) The try/catch wraps only the for await loop; a synchronous throw from deps.streamProvider(...) still propagates to run().catch() as before — unchanged behavior, just noting the boundary.

Approving as a safe hardening.

@patlux
patlux merged commit 6bdcb5f into patlux:main Oct 15, 2026
12 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.

3 participants