Files
magpie/internal/plugin/testdata/renew/index.js
T
yetone 6e2dcc4a10 plugin: a sign-in that won't renew is asked again later and later, not at once (JZGiVvkSOA75376 on X)
Two dead ima accounts (magpie-ima-auth 0.3.0) were renewed ~80 times a
minute: its auth.refresh catches ima's "sign in again", warns and gives
back undefined, and the host took that as "nothing to do" without noting
a failure, so every request, models listing and usage read asked again.

A refresh that gives nothing back while the sign-in is due now counts as
a failed renewal. A failed renewal of the same sign-in waits 30s, then
doubles each time up to 30 minutes, for every plugin; a new sign-in
starts afresh, and one the vendor turned away for good is still never
tried again.

Run: go test -run 'TestPluginRenewsSignIns|TestRenewRetryBacksOff|TestAuthLock'
./internal/plugin on mote: ok.
Broken by hand: dropping the failure note on an empty refresh makes
TestPluginRenewsSignIns red (r-none renewed three times); a flat 30s
retry makes TestRenewRetryBacksOff red.
2026-11-10 09:35:32 +08:00

47 lines
2.1 KiB
JavaScript

// A plugin that leaves renewing its sign-ins to magpie (auth.refresh),
// its requests going to $FAKE_BASE. Each renewal is written to
// $RENEW_LOG as a line naming the refresh token it spent. The refresh
// token says how the vendor takes it: "r-ok…" renews (slowly, as a
// vendor does, so requests arriving meanwhile find it under way),
// "r-gone…" is turned away for good, "r-down…" can't be reached,
// "r-none…" gives nothing back (as magpie-ima-auth's does when its vendor
// refuses), and "r-slow…" renews after 4 seconds.
import fs from "node:fs"
export const RenewPlugin = async () => ({
config: async (cfg) => {
cfg.provider = cfg.provider ?? {}
cfg.provider.renewco = {
name: "RenewCo",
npm: "@ai-sdk/openai-compatible",
api: "https://renew.invalid/v1",
models: { "renew-1": { name: "Renew One", limit: { context: 1000, output: 100 } } },
}
},
auth: {
provider: "renewco",
methods: [{ type: "api", label: "API key" }],
async refresh(auth) {
fs.appendFileSync(process.env.RENEW_LOG, auth.refresh + "\n")
await new Promise((ok) => setTimeout(ok, auth.refresh.startsWith("r-slow") ? 4000 : 300))
if (auth.refresh.startsWith("r-gone")) throw Object.assign(new Error("RenewCo turned the refresh token away"), { signIn: "expired" })
if (auth.refresh.startsWith("r-down")) throw new Error("RenewCo can't be reached")
if (auth.refresh.startsWith("r-none")) return undefined
const n = fs.readFileSync(process.env.RENEW_LOG, "utf8").trim().split("\n").length
// a vendor that spends a refresh token once gives a new one
return { access: "a-" + n, refresh: auth.refresh + "+", expires: Date.now() + 3600e3 }
},
loader: async (getAuth) => ({
baseURL: process.env.FAKE_BASE,
async fetch(url, init) {
const a = await getAuth()
const h = new Headers(init.headers)
h.set("authorization", "Bearer " + a.access)
// the test this host runs for (testenv.FakeVendor)
if (process.env.FAKE_RUN) h.set("x-fake-run", process.env.FAKE_RUN)
return fetch(url, { ...init, headers: h })
},
}),
},
})