qoder 0.2.70: a call Qoder sends nothing to for 120s is dropped with a 504 that says so (#1204)

xiaotao6869 (#1204): a non-streamed Qoder CN call got no bytes for 15
minutes, and magpie serve logged only the client's 499 after 60s.
Nothing bounded the chat fetch or its first event, so a call Qoder
held open with nothing sent waited for as long as the client did, and
no line said where.

The chat call now has QUEUE.silent (120s) to get its reply headers and
first event. Past that the call is aborted and answers 504
"<site> sent nothing for 120s after the request (no reply headers |
no first event); the call was dropped", with no sign-in verdict, so the
gateway can fail over and the log names the stage. A queued call
answers its 10605 at once and a thinking model streams reasoning first,
so a real answer starts well inside it; the timer ends once the first
event is read, and a long answer is not cut.

Run (mica, temp HOME): bun test in packages/qoder 447 pass, 0 fail;
bun scripts/check.mjs qoder ok.
Broken by hand: origin/main's index.mjs with silent.test.mjs: the three
silent cases hang to bun's 3s test timeout, the answered case passes.

Not tried: a real Qoder CN account; the cause of the reporter's
silent 15 minutes is not known, this only bounds it and says where.
This commit is contained in:
yetone
2026-11-16 16:32:02 +08:00
parent 2b5e7d6602
commit 0c685fe902
3 changed files with 101 additions and 1 deletions
+17
View File
@@ -1729,6 +1729,12 @@ const QUEUE = {
recoveries: 10, // the queue waited out this many times, at most, for one request
failures: 3, // status asks failing in a row that end the wait
ask: 30_000, // the longest one status ask may take
// the longest a call waits for Qoder's first event (headers included).
// Qoder CN once held a call 15 minutes with nothing sent and nothing to
// log (#1204); after this it is a 504 that says so, which magpie can
// fail over from. A queued call answers its 10605 at once, and a
// thinking model streams its reasoning, so a real answer starts sooner.
silent: 120_000,
now: () => Date.now(),
// every calls fn each ms until the returned stop is called
every: (fn, ms) => {
@@ -3792,11 +3798,22 @@ const makePlugin = (site) => async ({ client }) => {
const call = new AbortController()
const callSignal = signal ? AbortSignal.any([signal, call.signal]) : call.signal
let res, open
let silent = false
const hush = setTimeout(() => {
silent = true
call.abort()
}, QUEUE.silent)
try {
res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: callSignal })
if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
} catch (e) {
if (silent) {
const who = siteOf(site, cred).name ?? "Qoder"
return errorResponse({ status: 504, message: `${who} sent nothing for ${Math.round(QUEUE.silent / 1000)}s after the request (${res ? "no first event" : "no reply headers"}); the call was dropped`, signIn: "" })
}
return errorResponse({ status: 502, message: e.message })
} finally {
clearTimeout(hush)
}
const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
if (f) call.abort()
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-qoder-auth",
"version": "0.2.69",
"version": "0.2.70",
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
"type": "module",
"main": "./opencode.mjs",
+83
View File
@@ -0,0 +1,83 @@
// Qoder CN once held a call 15 minutes with nothing sent: no reply
// headers, nothing logged, and the client gave up first (xiaotao6869,
// #1204). A call that hears nothing for QUEUE.silent is dropped, and its
// 504 says so, so magpie can fail over and the log shows where it waited.
// The upstream here never answers until its signal is aborted; the reply
// that sends headers and then nothing is the same wait, one step later.
import { afterEach, expect, test } from "bun:test"
import { QoderAuthPlugin, _internal } from "./index.mjs"
const { QUEUE } = _internal
const real = globalThis.fetch
const saved = { ...QUEUE }
afterEach(() => {
globalThis.fetch = real
Object.assign(QUEUE, saved)
})
const API_CHAT = "https://api3.qoder.sh/v1/chat/completions"
const LISTING = { chat: [{ key: "dfmodel", display_name: "DeepSeek Flash", enable: true }] }
const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "Content-Type": "application/json" } })
const hang = (signal) =>
new Promise((_, no) => signal?.addEventListener("abort", () => no(signal.reason ?? new Error("aborted")), { once: true }))
async function loader(chat) {
const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: "m1" }
let calls = 0
globalThis.fetch = async (url, init = {}) => {
const u = new URL(String(url))
if (u.pathname.endsWith("/model/list")) return json(LISTING)
calls++
return chat(init.signal)
}
const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } })
const l = await hooks.auth.loader(async () => auth)
return { fetch: l.fetch, calls: () => calls }
}
for (const stream of [false, true]) {
test(`a call Qoder sends no reply headers to is dropped after QUEUE.silent with a 504 that says so (stream ${stream})`, async () => {
QUEUE.silent = 50
const { fetch, calls } = await loader((signal) => hang(signal))
const began = Date.now()
const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", stream, messages: [{ role: "user", content: "hi" }] }) })
expect(res.status).toBe(504)
expect((await res.json()).error.message).toMatch(/sent nothing for 0s after the request \(no reply headers\)/)
expect(res.headers.get("X-Magpie-Sign-In")).toBeNull()
expect(Date.now() - began).toBeLessThan(5_000)
expect(calls()).toBe(1)
})
}
test("a reply whose headers came and whose first event never does is dropped the same way", async () => {
QUEUE.silent = 50
const { fetch } = await loader(
(signal) =>
new Response(
new ReadableStream({
start(ctl) {
signal?.addEventListener("abort", () => ctl.error(signal.reason ?? new Error("aborted")), { once: true })
},
}),
{ headers: { "Content-Type": "text/event-stream" } },
),
)
const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", messages: [{ role: "user", content: "hi" }] }) })
expect(res.status).toBe(504)
expect((await res.json()).error.message).toMatch(/no first event/)
})
test("a reply that answers within QUEUE.silent is not cut, and the timer is gone after it", async () => {
QUEUE.silent = 200
const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200 })}\n\n`
const { fetch } = await loader(async () => {
await new Promise((r) => setTimeout(r, 20))
return new Response(line({ choices: [{ delta: { content: "hello" } }] }) + line({ choices: [{ delta: {}, finish_reason: "stop" }] }) + line("[DONE]"), {
headers: { "Content-Type": "text/event-stream" },
})
})
const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", messages: [{ role: "user", content: "hi" }] }) })
expect(res.status).toBe(200)
expect((await res.json()).choices[0].message.content).toBe("hello")
await new Promise((r) => setTimeout(r, 300))
})