Repository navigation
fix(transport): honor generate fallback when provider stream rejects upgrade - #154
Conversation
pierreraby
left a comment
There was a problem hiding this comment.
Reviewed at exact head 1349045af792af4dd7b6d18a413b6e3066c1cfa7 against base b61093de3f866fc533f827a25a27bc342e9d678d.
Verification (isolated worktree, npm ci)
npm run typecheckPASS,npm run test:transport18/18 PASS,npm run test:unitexit 0 (392 checks, 0 failures), Prettier check on both changed files PASS,git diff --checkPASS.- RED proven on base: with only the two tests from this PR applied to
b61093d,test:transportis 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, andoutput.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 existingif (!upgradeRequired)guard and the router already reaches the Generate fallback (streamGeneratecalled,getTransport() === "generate", finaldone).
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
- (P2) No
CHANGELOG.mdbullet.RELEASE.mdlets 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). - (P2) Reachability unproven on the supported host (details above).
- (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. - (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.
- (P3) The
try/catchwraps only thefor awaitloop; a synchronous throw fromdeps.streamProvider(...)still propagates torun().catch()as before — unchanged behavior, just noting the boundary.
Approving as a safe hardening.
Problem
The transport router recognizes HTTP 403 with error code
upgrade_requiredand 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 exitsrun()before fallback. This is not a reproduced failure in the supported host adapters.Change
upgrade_required, continue to the existing Generate fallback path. Other errors still propagate unchanged.403 upgrade_required, plus a negative case for an unrelated HTTP 403 stream rejection.Verification
b61093de3f866fc533f827a25a27bc342e9d678d, the added regression fails: expecteddone, goterror(17/18 passing).npm run test:transport18/18 PASS,npm run typecheckPASS, changed-file Prettier PASS,git diff --checkPASS.src/transport.tsandtests/test-transport.ts; no credentials or external requests are needed.Maintainer follow-up (2026-10-15)
npm testpassed 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.