star21321321124_93024 (Discord #general, 2026-11-01): a Qoder CN personal
account with all 1500 credits left listed one model in magpie, the free
Flash ("Qoder CN: 1 model"), so agents saw only that one.
Cause, from Qoder CN's real bytes: gateway.qoder.com.cn's
/algo/api/v2/model/list answers a caller it doesn't take for an account
with every model that costs credits `enable: false` and the free
Qwen3.8-Flash on, in every scene (captured 2026-11-01 for an unknown uid,
kept as testdata/cn-listing-free-view.json). plugins#484 saw the same view
for a stale job token on qoder.com. 0.2.49 renewed the job token when a
chat asked for a model that list had off, but provider.models (the list
magpie and its agents see) read it as it came, so the account showed
the free model alone and nothing renewed it.
Now models() marks such a listing (freeView: some paid model, none on),
and the provider.models hook renews the job token once through relogin
(same lock and 5-minute rule as #474/#484), saves it, reads the list
again and says the renewal on the next usage read or answer. A renewed
token that still gets the free view is remembered and not renewed for the
list again, so an account that really has only the free models doesn't
spend a refresh token each time magpie lists.
Tests (cn-free-view.test.mjs, from the captured bytes; the account's list
is the same bytes with each model on):
- free view on jt-1 -> one refresh, all 10 app models listed, jt-2 saved;
listing again spends none.
- the next usage read says "renewed".
- free view on the renewed token too: listed as it is, one refresh over
three lists an hour apart.
- a list with paid models on spends none; freeView's edge cases.
Run on mote (Bun 1.4.2):
- cn-free-view.test.mjs against origin/main's index.mjs: 4 of 6 fail
(the list is ["qfmodel"]); with this change 6 pass.
- Broken by hand: the stillFree guard off, the "renewed once" test fails
(3 refreshes).
- bun test in packages/qoder: 345 pass, 0 fail. bun scripts/check.mjs: ok.
Checked live (read only): qoder.com's listing for the owner's account has
every model on in every scene, the same with its device token and with a
bogus token (qoder.com keys the list on the uid). Qoder CN's CLI
(@qodercn-ai/qoderclicn 1.1.81) reads the same /api/v2/model/list.
Not tried: a real Qoder CN account (none here), so it is not confirmed
that a renewal gives the reporter back the full list; if their token is
honoured and the list is still the free view, the account itself has
the paid models off and this change lists it as it is.
150 lines
7.2 KiB
JavaScript
150 lines
7.2 KiB
JavaScript
// star21321321124_93024 on Discord (2026-11-01): a Qoder CN personal account,
|
|
// its 1500 credits all left, listed one model in magpie, the free Flash, so
|
|
// agents saw that one alone.
|
|
//
|
|
// testdata/cn-listing-free-view.json is what gateway.qoder.com.cn's
|
|
// /algo/api/v2/model/list answered on 2026-11-01 to a caller it doesn't take
|
|
// for an account: in every scene the free Qwen3.8-Flash on, and Auto, Fast,
|
|
// Qwen3.8-Max, DeepSeek, GLM, Kimi and MiniMax off. The account's own list
|
|
// is the same bytes with each model on. The fake gives the free view to a
|
|
// job token it no longer takes in full and the account's list to one it
|
|
// does, as plugins#484's fake does for qoder.com; 0.2.49 renewed such a token
|
|
// for a chat but not for the list magpie reads (provider.models), which is
|
|
// the list agents see. Calls are COSY-signed; the fake opens the envelope
|
|
// with the private half of a key the test puts in place of Qoder's.
|
|
import { afterEach, beforeEach, expect, test } from "bun:test"
|
|
import { readFileSync } from "node:fs"
|
|
import { constants, createDecipheriv, generateKeyPairSync, privateDecrypt } from "node:crypto"
|
|
import { QoderCNAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
const site = _internal.SITES["qoder-cn"]
|
|
const { publicKey, privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 })
|
|
beforeEach(() => (site.rsaKey = publicKey.export({ type: "spki", format: "pem" })))
|
|
afterEach(() => {
|
|
globalThis.fetch = real
|
|
delete site.rsaKey
|
|
})
|
|
|
|
const HOUR = 3_600_000
|
|
const FREE_VIEW = readFileSync(new URL("./testdata/cn-listing-free-view.json", import.meta.url), "utf8")
|
|
const ACCOUNT = JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(FREE_VIEW)).map(([k, v]) => [k, Array.isArray(v) ? v.map((m) => ({ ...m, enable: true })) : v])))
|
|
// the app scene's models an agent can pick: all but auto
|
|
const APP = JSON.parse(FREE_VIEW).app.map((m) => m.key).filter((k) => k !== "auto")
|
|
|
|
const json = (v, status = 200) => new Response(typeof v === "string" ? v : JSON.stringify(v), { status, headers: { "Content-Type": "application/json" } })
|
|
|
|
// tokenOf is the job token a COSY-signed call carries
|
|
function tokenOf(headers) {
|
|
const block = privateDecrypt({ key: privateKey, padding: constants.RSA_NO_PADDING }, Buffer.from(headers["Cosy-Key"], "base64"))
|
|
const key = block.subarray(block.indexOf(0, 2) + 1)
|
|
const [payload] = headers.Authorization.slice("Bearer COSY.".length).split(".")
|
|
const { info } = JSON.parse(Buffer.from(payload, "base64").toString())
|
|
const d = createDecipheriv("aes-128-cbc", key, key)
|
|
return JSON.parse(Buffer.concat([d.update(Buffer.from(info, "base64")), d.final()]).toString()).security_oauth_token
|
|
}
|
|
|
|
// qoderCN is the fake. honoured is the job tokens it lists the account's
|
|
// models for; renewing gives a token it honours unless stale says not.
|
|
function qoderCN({ honoured = new Set(), stale = false } = {}) {
|
|
const pairs = new Map([["jrt-1", "jt-1"]])
|
|
const calls = { refresh: [], listings: [] }
|
|
let n = 1
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
const u = new URL(String(url))
|
|
if (u.host === "openapi.qoder.com.cn" && u.pathname === "/api/v1/jobToken/refresh") {
|
|
const rt = JSON.parse(init.body).refresh_token
|
|
calls.refresh.push(rt)
|
|
if (!pairs.has(rt)) return json({ message: "invalid refresh token" }, 401)
|
|
pairs.delete(rt)
|
|
n++
|
|
if (!stale) honoured.add(`jt-${n}`)
|
|
pairs.set(`jrt-${n}`, `jt-${n}`)
|
|
return json({ token: `jt-${n}`, refresh_token: `jrt-${n}`, expires_in: 24 * HOUR })
|
|
}
|
|
if (u.host === "gateway.qoder.com.cn" && u.pathname === "/algo/api/v2/model/list") {
|
|
const t = tokenOf(init.headers)
|
|
calls.listings.push(t)
|
|
return json(honoured.has(t) ? ACCOUNT : FREE_VIEW)
|
|
}
|
|
return new Response("", { status: 404 })
|
|
}
|
|
return calls
|
|
}
|
|
|
|
// account is a Qoder CN personal account as magpie saved it, its job token a
|
|
// day from its saved end
|
|
async function account() {
|
|
const box = { stored: { type: "oauth", access: "jt-1", refresh: "jrt-1", expires: Date.now() + 24 * HOUR, accountId: "cn@x", uid: "u-cn", machineId: "m-cn", deviceToken: "dt-cn", deviceRefresh: "drt-cn" } }
|
|
const hooks = await QoderCNAuthPlugin({ client: { auth: { set: async ({ body }) => (box.stored = { ...body }) } } })
|
|
return { hooks, box, list: () => hooks.provider.models({ models: { fallback: {} } }, { auth: box.stored }) }
|
|
}
|
|
|
|
test("the free view is Qoder CN's real answer: only the free model on", () => {
|
|
const listing = JSON.parse(FREE_VIEW)
|
|
const on = listing.app.filter((m) => m.enable).map((m) => m.display_name)
|
|
expect(on).toEqual(["Qwen3.8-Flash"])
|
|
expect(APP.length).toBeGreaterThan(5)
|
|
})
|
|
|
|
test("an account listed the free view on its job token lists every model after one renewal", async () => {
|
|
const calls = qoderCN()
|
|
const a = await account()
|
|
const ms = await a.list()
|
|
expect(Object.keys(ms)).toEqual(APP)
|
|
expect(ms.qfmodel.free).toBe(true)
|
|
expect(ms.qmodel_38max.free).toBe(false)
|
|
expect(calls.refresh).toEqual(["jrt-1"])
|
|
expect(calls.listings).toEqual(["jt-1", "jt-2"])
|
|
expect(a.box.stored).toMatchObject({ access: "jt-2", refresh: "jrt-2" })
|
|
// the renewed token lists them again with no renewal
|
|
expect(Object.keys(await a.list())).toEqual(APP)
|
|
expect(calls.refresh).toEqual(["jrt-1"])
|
|
})
|
|
|
|
test("the account's next usage read says the renewal the list made", async () => {
|
|
qoderCN()
|
|
const a = await account()
|
|
await a.list()
|
|
globalThis.fetch = async (url) => {
|
|
if (new URL(String(url)).pathname === "/sash/api/v2/me/usage") return json({ displayMode: "qoder", qoderUsage: { userType: "personal", userQuota: { total: 1500, used: 0 } } })
|
|
return json({ app: [] })
|
|
}
|
|
expect((await a.hooks.auth.usage(async () => a.box.stored)).signIn).toBe("renewed")
|
|
})
|
|
|
|
test("an account that lists the free view on a renewed token too is listed as it is, renewed once", async () => {
|
|
const calls = qoderCN({ stale: true })
|
|
const a = await account()
|
|
expect(Object.keys(await a.list())).toEqual(["qfmodel"])
|
|
// past the five minutes a refused renewed token isn't renewed again for:
|
|
// the list magpie reads again later spends no refresh token
|
|
const now = Date.now
|
|
try {
|
|
for (let i = 1; i <= 2; i++) {
|
|
Date.now = () => now() + i * 30 * 60_000
|
|
expect(Object.keys(await a.list())).toEqual(["qfmodel"])
|
|
}
|
|
} finally {
|
|
Date.now = now
|
|
}
|
|
expect(calls.refresh).toEqual(["jrt-1"])
|
|
})
|
|
|
|
test("an account whose list has its paid models on spends no refresh token", async () => {
|
|
const calls = qoderCN({ honoured: new Set(["jt-1"]) })
|
|
const a = await account()
|
|
expect(Object.keys(await a.list())).toEqual(APP)
|
|
expect(calls.refresh).toEqual([])
|
|
expect(calls.listings).toEqual(["jt-1"])
|
|
})
|
|
|
|
test("freeView: a list with a paid model on, or with none that costs credits, isn't the free view", () => {
|
|
const { freeView } = _internal
|
|
expect(freeView(JSON.parse(FREE_VIEW).app)).toBe(true)
|
|
expect(freeView(JSON.parse(ACCOUNT).app)).toBe(false)
|
|
expect(freeView([{ key: "qfmodel", enable: true, price_factor: 0 }])).toBe(false)
|
|
expect(freeView([{ key: "auto", enable: false, price_factor: 0.5 }, { key: "qfmodel", enable: true, price_factor: 0 }])).toBe(false)
|
|
expect(freeView([{ key: "gmodel", enable: false, price_factor: 0.8 }, { key: "kmodel", enable: true, price_factor: 0.8 }])).toBe(false)
|
|
})
|