all: a usage read says what it means for the sign-in, and a renewal clears the mark whatever follows, as the built-ins did (要保证插件跟内置的所有逻辑都要一模一样) — auth.usage returns signIn on every path: expired where the built-in's quota read marked the account (Zed's /users/me 401, Factory's and MiMo's refused renewals, Qoder's refused job refresh), renewed after a renewal, which took the mark off, kept everywhere else, so magpie no longer guesses from the words 'sign in again' (Kiro's, Devin's and ZCode's usage errors marked accounts their built-ins never did) nor clears on a clean read. A request after a renewal says renewed on any status; one without says kept, as the built-ins cleared only on renewal. Kiro, Cursor, Devin, Command Code, WorkBuddy, Grok and ZCode, whose built-ins never marked an account, say kept on every answer. New tests fail against 85d7dd0's index.mjs.

This commit is contained in:
yetone
2026-10-01 04:53:29 +08:00
parent 85d7dd0d0a
commit 01aee5da28
31 changed files with 541 additions and 229 deletions
+8 -1
View File
@@ -74,5 +74,12 @@ test("an error line saying 401 keeps the account too", async () => {
test("the Provider API's 401 goes on as it came, the account kept", async () => {
expect(await signIn(() => Response.json({ error: { message: "Invalid API key" } }, { status: 401 }), "Pro", "/provider/v1/chat/completions"))
.toEqual({ status: 401, signIn: "kept" })
expect(await signIn(() => Response.json({}), "Pro", "/provider/v1/chat/completions")).toEqual({ status: 200, signIn: null })
})
// nor cleared one: an answer that went through leaves the mark as it is
test("an answer that goes through keeps the account's mark, Go's and the Provider API's", async () => {
expect(await signIn(() => Response.json({}), "Pro", "/provider/v1/chat/completions")).toEqual({ status: 200, signIn: "kept" })
expect(await signIn(() => Response.json({}, { status: 404 }), "Pro", "/provider/v1/chat/completions")).toEqual({ status: 404, signIn: "kept" })
const line = JSON.stringify({ type: "text-delta", text: "hi" }) + "\n" + JSON.stringify({ type: "finish", finishReason: "stop" }) + "\n"
expect(await signIn(() => new Response(line))).toEqual({ status: 200, signIn: "kept" })
})
+8 -6
View File
@@ -917,11 +917,11 @@ export async function CommandCodePlugin({ client } = {}) {
} catch {
return errorResponse({ status: 400, message: "a request that isn't JSON" })
}
return generate(key, chat, init.signal)
return kept(await generate(key, chat, init.signal))
}
// the Provider API's 401 went on as it came, the account kept
const res = await fetch(input, { ...init, headers })
return res.status === 401 ? kept(res) : res
// the Provider API's answer goes on as it came, the account
// kept: the built-in neither marked one lapsed nor cleared it
return kept(await fetch(input, { ...init, headers }))
},
}
},
@@ -933,7 +933,9 @@ export async function CommandCodePlugin({ client } = {}) {
// magpie's: the plan and how much of it is used
async usage(getAuth) {
const auth = await getAuth()
if (auth?.type !== "api" || !auth.key) return { error: "not signed in" }
// signIn kept on each: the built-in's usage read never marked the
// account lapsed nor cleared it
if (auth?.type !== "api" || !auth.key) return { error: "not signed in", signIn: "kept" }
const md = auth.metadata ?? {}
const { out, read } = await usage(await liveKey(auth), md.plan ? { id: md.planId ?? "", plan: md.plan } : undefined)
// the plan read is saved with the key, so a start doesn't wait on
@@ -943,7 +945,7 @@ export async function CommandCodePlugin({ client } = {}) {
await client.auth.set({ path: { id: ID }, body: { ...auth, metadata: { ...md, plan: read.plan, planId: read.id } } })
} catch {}
}
return out
return { ...out, signIn: "kept" }
},
},
async config(config) {
+11 -9
View File
@@ -36,6 +36,7 @@ test("a Max account: its 5 hours, its week and its credits, and its period", asy
},
})
expect(u).toEqual({
signIn: "kept",
plan: "Max",
until: "2026-10-28T00:00:00.000Z",
renew: "off",
@@ -53,7 +54,7 @@ test("no plan: the credits bought are a balance", async () => {
"/alpha/billing/subscriptions": { success: true, data: null },
"/alpha/billing/credits": { credits: { monthlyCredits: 0, purchasedCredits: 7.25, freeCredits: 0 } },
})
expect(u).toEqual({ plan: "No plan", balance: "$7.25", windows: [] })
expect(u).toEqual({ signIn: "kept", plan: "No plan", balance: "$7.25", windows: [] })
})
test("Go: the month's $10 is the pool", async () => {
@@ -61,7 +62,7 @@ test("Go: the month's $10 is the pool", async () => {
"/alpha/billing/subscriptions": { data: { planId: "individual-go", status: "active" } },
"/alpha/billing/credits": { credits: { monthlyCredits: 4 } },
})
expect(u).toEqual({ plan: "Go", windows: [{ name: "Credits", used: 60, display: "$6.00 / $10.00" }] })
expect(u).toEqual({ signIn: "kept", plan: "Go", windows: [{ name: "Credits", used: 60, display: "$6.00 / $10.00" }] })
})
test("credits that can't be read keep the plan and say why", async () => {
@@ -69,15 +70,15 @@ test("credits that can't be read keep the plan and say why", async () => {
"/alpha/billing/subscriptions": { data: { planId: "individual-pro-monthly", status: "active", cancelAtPeriodEnd: false } },
"/alpha/billing/credits": 503,
})
expect(u).toEqual({ plan: "Pro", renew: "auto", error: "Service Unavailable", windows: [] })
expect(u).toEqual({ signIn: "kept", plan: "Pro", renew: "auto", error: "Service Unavailable", windows: [] })
})
test("a canceled subscription is No plan; an unread one leaves the plan unsaid", async () => {
let { u } = await run(api, { "/alpha/billing/subscriptions": { data: { planId: "individual-pro", status: "canceled" } }, "/alpha/billing/credits": 413 })
expect(u).toEqual({ plan: "No plan", error: "Request Entity Too Large", windows: [] })
expect(u).toEqual({ signIn: "kept", plan: "No plan", error: "Request Entity Too Large", windows: [] })
_internal.subsSeen.clear() // a plan read is kept ten minutes
;({ u } = await run(api, { "/alpha/billing/subscriptions": 401, "/alpha/billing/credits": 401 }))
expect(u).toEqual({ error: "Unauthorized", windows: [] })
expect(u).toEqual({ signIn: "kept", error: "Unauthorized", windows: [] })
})
test("seconds and string amounts read as magpie reads them; a half cent goes to the even one", async () => {
@@ -86,6 +87,7 @@ test("seconds and string amounts read as magpie reads them; a half cent goes to
"/alpha/billing/credits": { credits: { monthlyCredits: "29.875" }, windowLimits: { fiveHour: { used: "5", cap: 0 }, weekly: { used: 150, cap: "100", resetAt: 0 } } },
})
expect(u).toEqual({
signIn: "kept",
plan: "Pro",
until: new Date(1790000000 * 1000).toISOString(),
windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "$0.12 / $30.00" }],
@@ -93,7 +95,7 @@ test("seconds and string amounts read as magpie reads them; a half cent goes to
})
test("a sign-in that isn't a key is no account", async () => {
expect((await run({ type: "oauth" }, {})).u).toEqual({ error: "not signed in" })
expect((await run({ type: "oauth" }, {})).u).toEqual({ signIn: "kept", error: "not signed in" })
})
test("a slow subscription: the windows go without it, and the next card has it", async () => {
@@ -111,11 +113,11 @@ test("a slow subscription: the windows go without it, and the next card has it",
}
const hooks = await CommandCodePlugin()
const first = await hooks.auth.usage(async () => api)
expect(first).toEqual({ windows: [{ name: "5 hours", used: 1, span: 18000 }] })
expect(first).toEqual({ signIn: "kept", windows: [{ name: "5 hours", used: 1, span: 18000 }] })
answer()
await new Promise((r) => setTimeout(r, 20))
const next = await hooks.auth.usage(async () => api)
expect(next).toEqual({ plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
expect(next).toEqual({ signIn: "kept", plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
})
test("a plan read is saved with the key, once", async () => {
@@ -148,7 +150,7 @@ test("a plan saved stands in for a subscription slow to read: waited for a momen
const t = Date.now()
const u = await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT", planId: "individual-goat" } }))
expect(Date.now() - t).toBeLessThan(3_000)
expect(u).toEqual({ plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
expect(u).toEqual({ signIn: "kept", plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
answer()
})