mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
Problem: on /v1/chat/completions and /v1/messages the upstream responses stream is converted as it arrives, so client output starts with the first event (a role chunk or message_start written for response.created). A terminal error event that arrives after that can no longer fail over, and the branch that handles it only records the error. Unlike /v1/responses and the WebSocket forwarder, which call handleOpenAIStreamTerminalAccountSideEffects in the same situation, the account health is never updated: a rate limited, quota exhausted or deauthorized account stays schedulable and every new request routed to it fails the same way. Fix: call handleOpenAIStreamTerminalAccountSideEffects in the non-failover branch of both compat stream handlers, before the error is recorded, with the same arguments the failover constructor uses. The failover branch is unchanged; it already applies the side effects inside newOpenAIStreamFailoverErrorWithModel, so each upstream attempt is still counted once. Test: TestForwardAsChatCompletions_StreamErrorAfterOutputAppliesAccountSideEffects and TestForwardAsAnthropic_StreamErrorAfterOutputAppliesAccountSideEffects check that an auth error after output marks the account exactly once. The two StreamErrorBeforeOutputCountsOnce tests check that the failover path still marks the account exactly once.