Skip to content

fix: reasoning duplication, quota reported as 502, network blip aborting retries - #5

Open
maxff77 wants to merge 1 commit into
smanx:masterfrom
maxff77:pr/upstream-fixes
Open

fix: reasoning duplication, quota reported as 502, network blip aborting retries#5
maxff77 wants to merge 1 commit into
smanx:masterfrom
maxff77:pr/upstream-fixes

Conversation

@maxff77

@maxff77 maxff77 commented Aug 19, 2026

Copy link
Copy Markdown

Three independent failure modes found while exercising the live Qwen endpoint. Each is small and reproducible; none of them depend on each other.

1. Reasoning content was duplicated quadratically

Upstream thinking_summary events each carry the full summary so far, not an increment. parseQwenSsePayload concatenated them, so a 263-character summary reached the client as roughly 1.5 KB of repeated text.

Merged by replacement when a fragment starts with what we already have; genuinely incremental fragments still concatenate.

2. Quota exhaustion was reported as HTTP 502

Qwen returns You've reached the upper limit for today's usage. as HTTP 200 with an error body, which became a blanket 502. Clients read that as a gateway fault and retry blindly instead of backing off.

Rate-limit signatures now map to 429 with type: "rate_limit_error". Every other upstream failure stays on 502.

3. A network blip aborted the request instead of retrying

createChatSession called getBaxiaTokens outside its try block, so a network failure while refreshing tokens on retry escaped the function entirely: the remaining retries never ran and Express answered with an empty 500. The signature in the logs is a single exception (attempt 1) line followed by nothing.

The completion fetch was unguarded in all three call sites too, with the same result. Both now go through fetchUpstream, which turns a network exception into a synthetic failed response so the existing !ok branch reports it properly.

Testing

Adds scripts/test-core.js — 6 assert-based checks, no framework, no new dependencies — and a CI workflow, since the repo had no job running tests.

npm test

Happy to split this into three separate PRs if you would rather review them one at a time.

Independent of each other; each one is small and reproducible.

1. Reasoning content was duplicated quadratically.
   Upstream `thinking_summary` events each carry the full summary so
   far, not an increment. `parseQwenSsePayload` concatenated them, so
   a 263-character summary reached the client as roughly 1.5 KB of
   repeated text. Merge cumulative fragments by replacement.

2. Quota exhaustion was reported as HTTP 502.
   Qwen returns `You've reached the upper limit for today's usage.`
   as HTTP 200 with an error body, which became a blanket 502.
   Clients read that as a gateway fault and retry blindly instead of
   backing off. Map the rate-limit signatures to 429 with
   `type: "rate_limit_error"`; everything else stays on 502.

3. A network blip aborted the whole request instead of retrying.
   `createChatSession` called `getBaxiaTokens` outside its try block,
   so a network failure while refreshing tokens on retry escaped the
   function: the remaining retries never ran and Express answered
   with an empty 500. The completion fetch was unguarded in all three
   call sites too, with the same result. Route them through
   `fetchUpstream`, which turns a network exception into a synthetic
   failed response so the existing `!ok` branch reports it properly.

Adds `scripts/test-core.js` (6 assert-based checks, no framework, no
new dependencies) and a CI workflow, since the repo had no job
running tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the smanx's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify

netlify Bot commented Aug 19, 2026

Copy link
Copy Markdown

Deploy Preview for qwen2api failed. Why did it fail? →

Name Link
🔨 Latest commit 7655e30
🔍 Latest deploy log https://app.netlify.com/projects/qwen2api/deploys/6a85188b73e8d300080f154c

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