From 4af310cc57230ac5ffabbc7298e0792a6d9638b2 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 01:52:42 +0800 Subject: [PATCH] qoder 0.2.24: Qoder's 400 "Error in upstream response" is a 502, so magpie retries or falls back (SlouchyQuill507 on Discord) SlouchyQuill507 (Discord, qoder-cn) reported Qoder failures of the model's own vendor, "Error in upstream response", reaching magpie as a 400 invalid request: the client's fault, never asked again or of another account. failure() now maps a message saying "Error in upstream response" to a 502 (no X-Magpie-Sign-In, the account untouched), after the queue, quota and request codes, so a 400 with one of Qoder's request codes (406, 416, ...) and any other 400 stay 400. It covers an HTTP 400 and a 400 in the stream's statusCodeValue envelope, JSON or plain text. magpie still tells an overflow that fails this way apart by its size (tooLongUnsaid takes 502 as well as 400). Test: usage.test.mjs "Qoder's 400 \"Error in upstream response\" is a 502" - fixtures from the reported literal text, four shapes through the loader's fetch, plus genuine 400s kept. Red on the old index.mjs (400 for each), green with the change. Run on origin/main 6fd976a: TMPDIR= HOME=/home bun test (whole repo) 1919 pass 0 fail across 173 files; bun scripts/check.mjs ok. --- packages/qoder/README.md | 1 + packages/qoder/index.mjs | 9 +++++++++ packages/qoder/package.json | 2 +- packages/qoder/usage.test.mjs | 27 +++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/qoder/README.md b/packages/qoder/README.md index 5be43a1..98ae11a 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -125,6 +125,7 @@ itself. | A quota (codes 110, 112–119, 122) | 429 | | Busy or queued for the model (code 10605) | 503, with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none); the sign-in is untouched | | A refusal of the request itself (codes 406, 416, 430, 80411, 80412, 100400–100403) | 400, with Qoder's words | +| "Error in upstream response", the model's own vendor behind Qoder failing, which Qoder sends as a 400 (since 0.2.24) | 502, so magpie asks again or another account; the sign-in is untouched | | A failure before the answer starts | Qoder's status | | A failure after the answer has started | The stream ends with an error | diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index e3e0c8a..fb0e101 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -849,6 +849,14 @@ const QUOTA_CODES = new Set(["110", "112", "113", "114", "115", "116", "117", "1 // supported, input too long, too many media, a custom model's service const REQUEST_CODES = new Set(["406", "416", "430", "80411", "80412", "100400", "100401", "100403"]) const BUSY_WAIT = 30 +// UPSTREAM_FAILED is Qoder saying the model's own vendor behind it failed: +// "Error in upstream response", which Qoder sends as a 400 (SlouchyQuill507 +// on Discord, qoder-cn). Nothing is wrong with the request or the account, +// so it is a 502, which magpie asks again or of another account, not a +// 400 that ends the turn as the client's fault. A request too long for the +// model fails the same way, and magpie tells that one apart by its size +// (tooLongUnsaid), at 502 as at 400. +const UPSTREAM_FAILED = /\berror in upstream response\b/i function failure(status, text) { if (status < 400 || status > 599) status = 502 @@ -894,6 +902,7 @@ function failure(status, text) { } if (QUOTA_CODES.has(code)) return { status: 429, message: "usage limit reached: " + msg, signIn: "" } if (REQUEST_CODES.has(code)) return { status: 400, message: `Qoder refused the request (${code}): ${msg}`, signIn: "" } + if (UPSTREAM_FAILED.test(msg)) return { status: 502, message: msg, signIn: "" } // a refused chat is the built-in's 401, which never marked the account // lapsed: only a refused refresh did (errorResponse says kept). Qoder's // own words go with it: a 401 right after signing in isn't a lapsed diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 8cfb2b2..b950d5b 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.23", + "version": "0.2.24", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/usage.test.mjs b/packages/qoder/usage.test.mjs index 8c339c3..0879031 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -181,6 +181,33 @@ test("other refusals keep their status and say nothing of the sign-in", async () expect(_internal.failure(400, JSON.stringify({ message: "bad", details: { error: { message: "effort" } } }))).toEqual({ status: 400, message: "bad: effort" }) }) +// SlouchyQuill507 (Discord, qoder-cn): Qoder answers a failure of the model's +// own vendor behind it with a 400 "Error in upstream response". It is no +// fault of the request or the account, so it reaches magpie as a 502, which +// it asks again or of another account. A 400 that is the request's fault +// stays a 400. +test("Qoder's 400 \"Error in upstream response\" is a 502, the account unmarked", async () => { + const UPSTREAM = "Error in upstream response" + const sse = (v) => new Response(`data: ${JSON.stringify(v)}\n\n`, { headers: { "Content-Type": "text/event-stream" } }) + for (const [how, reply] of [ + ["an HTTP 400 with Qoder's JSON", () => new Response(JSON.stringify({ message: UPSTREAM }), { status: 400 })], + ["an HTTP 400 in plain text", () => new Response(UPSTREAM, { status: 400 })], + ["a 400 in the stream's envelope", () => sse({ headers: {}, body: JSON.stringify({ message: UPSTREAM }), statusCodeValue: 400, statusCode: "BAD_REQUEST" })], + ["a 400 in the envelope as plain text", () => sse({ headers: {}, body: UPSTREAM, statusCodeValue: 400, statusCode: "BAD_REQUEST" })], + ]) { + const { fetch } = await chat(LISTING, reply) + const res = await fetch(API_CHAT, ASK) + expect([how, res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([how, 502, null]) + expect((await res.json()).error.message).toBe(UPSTREAM) + } + expect(_internal.failure(400, JSON.stringify({ code: "500", message: UPSTREAM }))).toEqual({ status: 502, message: UPSTREAM, signIn: "" }) + // a request Qoder refuses as the request's fault stays a 400 + expect(_internal.failure(400, JSON.stringify({ message: "invalid request: messages is empty" }))).toEqual({ status: 400, message: "invalid request: messages is empty" }) + expect(_internal.failure(400, JSON.stringify({ code: "416", message: "input too long" })).status).toBe(400) + const { fetch } = await chat(LISTING, () => new Response(JSON.stringify({ message: "invalid request" }), { status: 400 })) + expect((await fetch(API_CHAT, ASK)).status).toBe(400) +}) + test("a model list Qoder won't give is a 502, the account unmarked", async () => { for (const status of [401, 403, 500]) { const auth = { ...account(), machineId: "m1" }