qoder: a trimmed or wrapped 10605 is busy, not a lapsed sign-in (#185)

`failure` parses the trimmed body, so a BOM-prefixed 10605 (the SSE envelope carries it as a JSON-escaped U+FEFF that `res.text()` doesn't strip) keeps its 503/Retry-After instead of reading as a lapsed sign-in. An outer body with no code, or code 401/403, whose message is a 10605 body is unwrapped to it; explicit 105/110/406 codes stay authoritative, and malformed or array messages are not unwrapped. Refs #167, #181.

What was run at c9e39e0 (reviewer, bun 1.4.2, macOS):
- bun test packages/qoder: 137 pass, 0 fail. bun scripts/check.mjs qoder: ok.
- Broken by hand: without the unwrap, the 8 wrapper tests go red; parsing `text` instead of the trimmed `msg`, the BOM test goes red; unwrapping regardless of the outer code, the explicit-code test goes red. The `inner.trim()` in events() had no test; a follow-up commit adds one.
- magpie (yetone/magpie main 85eef5d87) in a sandbox HOME, a copy of this package with api3/openapi pointed at a local fake, a planted plugin-auth.json, `magpie provider test qoder`: a 403 `{"code":403,"message":<10605 body>}`, the SSE `{"message":<10605 body>}` envelope and a BOM-prefixed 403 each gave 503 "Qoder is busy for efficient (this plan's queue: p3); try again in 30s"; a 105 still gave 401 lapsed. main's package gave 401 lapsed for the first two.
- Not tried: real Qoder accounts.
This commit is contained in:
lokic7123-star
2026-10-17 17:54:35 +08:00
committed by GitHub
parent a73237c74c
commit 656f3a1fb5
2 changed files with 47 additions and 4 deletions
+33
View File
@@ -78,3 +78,36 @@ test("Qoder's other codes on a 403 aren't a lapsed sign-in either; 105 and no co
expect(f("105", "login expired")).toEqual({ status: 401, message: "the sign-in lapsed — sign in again (Qoder said 403: login expired)" })
expect(_internal.failure(403, "")).toEqual({ status: 401, message: "the sign-in lapsed — sign in again" })
})
test("a BOM in an SSE error body must not turn a resource limit into expired auth", async () => {
const { fetch, sets } = await chat(() => sse({headers:{},body:"\uFEFF"+QUEUED,statusCodeValue:403,statusCode:"FORBIDDEN"}))
expect(await busy(await fetch(API_CHAT,ASK))).toEqual(WANT)
expect(sets).toEqual([])
})
// #167 lists these outer message wrappers as additional ways a structured
// busy response can still reach the sign-in fallback. They are synthetic
// wrappers of the saved upstream queue fixture, not captured live bytes.
for (const [label, outer] of [
["message only", { message: QUEUED }],
["null code", { code: null, message: QUEUED }],
["HTTP code", { code: 403, message: QUEUED }],
["empty queue", { queue: {}, message: QUEUED }],
]) for (const stream of [false, true]) {
test(`a ${label} wrapper preserves the resource failure (SSE=${stream})`, async () => {
const { fetch, sets } = await chat(() => stream
? sse({ headers: {}, body: JSON.stringify(outer), statusCodeValue: 403, statusCode: "FORBIDDEN" })
: new Response(JSON.stringify(outer), { status: 403, headers: { "Content-Type": "application/json" } }))
expect(await busy(await fetch(API_CHAT, ASK))).toEqual(WANT)
expect(sets).toEqual([])
})
}
test("an explicit authentication, credit or request code is not replaced by text inside it", () => {
for (const [code, status] of [["105", 401], ["110", 429], ["406", 400]]) {
expect(_internal.failure(403, JSON.stringify({ code, message: QUEUED })).status).toBe(status)
}
expect(_internal.failure(403, JSON.stringify({ message: '{"code":"10605"' })).status).toBe(401)
expect(_internal.failure(403, JSON.stringify({ message: '[{"code":"10605"}]' })).status).toBe(401)
expect(_internal.failure(403, JSON.stringify({ message: JSON.stringify({ code: "110", message: "credits" }) })).status).toBe(401)
})
+14 -4
View File
@@ -799,16 +799,26 @@ function failure(status, text) {
let code = ""
let queue
try {
const j = JSON.parse(text)
let j = JSON.parse(msg)
// #167: an HTTP/message wrapper can hold the actual 10605 body.
// Unwrap only that known queue code; a real auth/credit/request code
// on the outer body remains authoritative.
const outerCode = j?.code == null ? "" : String(j.code)
if (["", "401", "403"].includes(outerCode) && typeof j?.message === "string") {
try {
const inner = JSON.parse(j.message.trim())
if (inner && !Array.isArray(inner) && String(inner.code) === QUEUED) j = inner
} catch {}
}
code = j?.code == null ? "" : String(j.code)
if (j?.message) msg = String(j.message)
// details is a JSON string, or the object itself (gjson reads either)
const d = typeof j?.details === "string" ? JSON.parse(j.details) : j?.details
const d = typeof j?.details === "string" ? JSON.parse(j.details.trim()) : j?.details
if (d?.error?.message) msg += ": " + d.error.message
// the queue's state: the message itself, as a JSON string, or a field
queue = j?.queue && typeof j.queue === "object" ? j.queue : undefined
if (!queue && typeof j?.message === "string" && j.message.trim().startsWith("{")) {
const q = JSON.parse(j.message)
const q = JSON.parse(j.message.trim())
if (q && typeof q === "object" && ("isQueued" in q || "serviceAvailable" in q || "retryAfterSeconds" in q)) queue = q
}
} catch {}
@@ -1029,7 +1039,7 @@ async function* events(body) {
if (inner.trim() === "[DONE]") break
let chunk
try {
chunk = JSON.parse(inner)
chunk = JSON.parse(inner.trim())
} catch {
continue
}