qoder 0.2.48: a job token Qoder refuses as Login expired is renewed once and asked again (#474)

ahang39 (plugins#474): international Qoder answered dfmodel with 403
"Login expired" in bursts of ~16 requests over 2-5 minutes, about every
24h, shown by magpie as 401 "the sign-in lapsed". The report's times put
the burst's start where magpie's renewal came due (saved end less the
10-minute lead) and its end where the renewed pair was saved: Qoder
refused a job token the plugin still had as minutes from its end, and
nothing asked again with a renewed one. Why Qoder ended it early isn't
proven from the report.

- A chat's 401/403 now marks the failure lapsed. ask re-reads the saved
  sign-in under the refresh lock: a pair renewed meanwhile (another
  request, magpie's auth.refresh, another process) is used with no
  refresh; else the refresh token is spent once, the pair saved, and the
  request sent once more. A burst shares one refresh. A token this
  process renewed under 5 minutes ago that is refused too isn't renewed
  again, so a refusal not about the token's end doesn't churn refresh
  tokens; the 401 is said as before.
- A refreshed job token's end is read as an exchanged PAT's was:
  expire_time / expires_at (a date, or an epoch in s or ms) first, then
  expires_in, then a day. An end already past is ignored.

Run (mote, mote-run, HOME=$(mktemp -d), --timeout 60000; mote at load
20-28):
- packages/qoder: 320 pass, 0 fail.
- whole repo: 3129 pass, 1 fail: commandcode "a plan saved stands in
  for a subscription slow to read" (16.8s against its 3s bar), passes
  alone 13/13, filed as #475.
- bun scripts/check.mjs: ok.
- lapsed.test.mjs on the old index.mjs: 8 of 9 red. The 9th (magpie's
  own renewal begun during the refusal) was rewritten after that run and
  not run against the old code again.

Not tried: a real international Qoder account at the moment its token
is refused; the fake Qoder (refusal body {"message":"Login expired"},
single-use refresh tokens) is built from the report's words only.
This commit is contained in:
yetone
2026-10-29 10:59:24 +08:00
parent 50e99c2524
commit cc3f000c6d
6 changed files with 313 additions and 30 deletions
+17 -6
View File
@@ -177,16 +177,23 @@ test("pools in different units are not added up", () => {
// the loader's fetch, as OpenCode's engine calls it, against a listing and
// a chat reply
// a refresh is answered with a new pair, counted in refreshes: a refused
// job token is renewed once before the refusal is said (plugins#474)
async function chat(listing, reply) {
const auth = { ...account(), machineId: "m1", name: "One" }
globalThis.fetch = async (url) => {
const refreshes = []
globalThis.fetch = async (url, init = {}) => {
const u = new URL(String(url))
if (u.pathname.endsWith("/model/list")) return json(listing)
if (u.pathname === "/api/v1/jobToken/refresh") {
refreshes.push(JSON.parse(init.body).refresh_token)
return json({ token: "jt-two", refresh_token: "rt-two", expires_in: 3_600_000 })
}
return reply()
}
const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } })
const l = await hooks.auth.loader(async () => auth)
return { fetch: l.fetch, hooks, auth }
return { fetch: l.fetch, hooks, auth, refreshes }
}
const LISTING = {
@@ -200,25 +207,29 @@ const LISTING = {
const ASK = { method: "POST", body: JSON.stringify({ model: "qmodel", messages: [{ role: "user", content: "hi" }] }) }
// Refused again after the one renewal a refusal gets: the job token was
// renewed (the account's mark comes off, as the built-in's renewal took it
// off), and the refusal is said as the built-in's 401, never "expired".
test("a refused chat, 401 or 403, is the built-in's 401 and leaves the account unmarked", async () => {
for (const status of [401, 403]) {
const { fetch } = await chat(LISTING, () => new Response("", { status }))
const { fetch, refreshes } = await chat(LISTING, () => new Response("", { status }))
const res = await fetch(API_CHAT, ASK)
expect(res.status).toBe(401)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect(res.headers.get("X-Magpie-Sign-In")).toBe("renewed")
expect((await res.json()).error.message).toBe("the sign-in lapsed — sign in again")
expect(refreshes).toEqual(["rt-one"])
}
// what Qoder says of the refusal is kept: a 401 just after signing in
// has some other reason, and only Qoder's words tell it
const said = await chat(LISTING, () => new Response(JSON.stringify({ message: "device not trusted" }), { status: 401 }))
const r = await said.fetch(API_CHAT, ASK)
expect([r.status, r.headers.get("X-Magpie-Sign-In")]).toEqual([401, "kept"])
expect([r.status, r.headers.get("X-Magpie-Sign-In")]).toEqual([401, "renewed"])
expect((await r.json()).error.message).toBe("the sign-in lapsed — sign in again (Qoder said 401: device not trusted)")
// refused in the stream, before any answer: the same
const sse = (v) => new Response(`data: ${JSON.stringify(v)}\n\n`, { headers: { "Content-Type": "text/event-stream" } })
const { fetch } = await chat(LISTING, () => sse({ statusCodeValue: 403, body: "" }))
const res = await fetch(API_CHAT, ASK)
expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([401, "kept"])
expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([401, "renewed"])
})
test("other refusals keep their status and say nothing of the sign-in", async () => {