Files
yetone 9410e2a510 commandcode 0.1.19, devin 0.1.26, kiro 0.1.19: a refused sign-in marks the account signed out
blue12345655 (magpie Discord #help): a Command Code account signed in
through the plugin was signed out, and magpie offered no 重新登录, on
v0.1.1844 and still on v0.1.2021.

Cause: commandcode said X-Magpie-Sign-In "kept" on every answer and
signIn "kept" on every usage read, so a revoked key came back as a card
saying "Unauthorized" while magpie kept the account signed in, routed to
it, and never showed Sign in again on its rows. Devin and Kiro did the
same in their usage reads: their errors already said "sign in again",
but "kept" stops magpie from reading those words.

- commandcode: a usage read whose credits are refused (401/403), or a
  request answered 401, asks /alpha/whoami; when whoami refuses the key
  too, the usage says signIn "expired" with "Command Code's sign-in has
  expired; sign in again", and the answer carries X-Magpie-Sign-In:
  expired, its body unchanged. A 403 for a model the plan lacks, a 401
  whoami still takes, and a whoami 500 keep the account. A clean usage
  read says "renewed", which takes a mark off.
- devin: a usage read Devin answers 401 says "expired"; a clean read
  "renewed"; other failures "kept". Requests unchanged.
- kiro: a usage read whose sign-in Kiro refused (the errors made by
  gone) says "expired"; a clean read "renewed"; other failures "kept".
  auth.refresh and requests unchanged.

The response bytes in commandcode/signed-out.test.mjs are what
api.commandcode.ai answered an unknown key on 2026-10-29 (whoami,
billing/credits, billing/subscriptions, provider/v1/chat/completions).

Run (WSL magpie-desktop, Bun 1.4.2, HOME=$(mktemp -d)):
- bun test in packages/commandcode: 82 pass, 0 fail; packages/devin:
  142 pass; packages/kiro: 67 pass.
- bun scripts/check.mjs: every package ok.

Broken by hand: signed-out.test.mjs against the 0.1.18 index.mjs, 4 of
9 red (usage expired, clean read renewed, Provider API expired, Go
expired). kiro's and devin's changed usage tests against their old
index.mjs: red ("kept" received where "expired" expected).

Not tried: a real revoked Command Code, Devin or Kiro account; the
whoami check is only against an unknown key's real answers.
2026-10-29 17:04:52 +08:00

87 lines
4.7 KiB
JavaScript

// A Go account's failures read as magpie's built-in Command Code account
// answers them (internal/gateway/commandcode.go, cmdFailure), so magpie's
// gateway moves on to the next account or member just as it did.
import { afterEach, expect, test } from "bun:test"
import { CommandCodePlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.subsSeen.clear()
})
// ask sends a chat completion as a Go account, /alpha/generate answering
// with reply.
async function ask(reply, stream = false) {
globalThis.fetch = async (url) => {
const path = new URL(String(url)).pathname
if (path === "/alpha/generate") return reply()
return new Response("", { status: 404 })
}
const auth = { type: "api", key: "go-key", metadata: { plan: "Go" } }
const hooks = await CommandCodePlugin()
const opts = await hooks.auth.loader(async () => auth)
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
method: "POST",
body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", stream, messages: [{ role: "user", content: "hi" }] }),
})
return { status: res.status, message: (await res.json()).error?.message }
}
// asks as ask does, with every answer's X-Magpie-Sign-In
async function signIn(reply, plan = "Go", path = "/alpha/generate") {
globalThis.fetch = async (url) => (new URL(String(url)).pathname === path ? reply() : new Response("", { status: 404 }))
const auth = { type: "api", key: "key", metadata: { plan } }
const hooks = await CommandCodePlugin()
const opts = await hooks.auth.loader(async () => auth)
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
method: "POST",
body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "hi" }] }),
})
return { status: res.status, signIn: res.headers.get("X-Magpie-Sign-In") }
}
test("an empty 429 reads as Go's http.StatusText says it, which quotaWords knows", async () => {
expect(await ask(() => new Response("", { status: 429 }))).toEqual({ status: 429, message: "Too Many Requests" })
})
test("credits out whose words aren't quotaWords' say so, as cmdFailure does", async () => {
const r = await ask(() => Response.json({ error: { type: "payment", message: "PREMIUM_CREDITS_EXHAUSTED: Monthly limit hit" } }, { status: 403 }))
expect(r).toEqual({ status: 402, message: "out of credits: Monthly limit hit" })
})
test("credits out already in quotaWords' words are left as they are", async () => {
const r = await ask(() => Response.json({ error: { message: "PREMIUM_CREDITS_EXHAUSTED: 余额不足" } }, { status: 400 }))
expect(r).toEqual({ status: 402, message: "余额不足" })
})
test("an error line before any content keeps its status", async () => {
const line = JSON.stringify({ type: "error", error: { message: '429 {"error":{"message":"RATE_LIMITED: slow down"}}' } })
expect(await ask(() => new Response(line + "\n"), true)).toEqual({ status: 429, message: "RATE_LIMITED: slow down" })
})
// a 401 of /alpha/generate's (cmdFailure) or of the Provider API's goes on
// with the account kept while whoami doesn't refuse the key too (here it
// answers 404); one it refuses is signed out (signed-out.test.mjs)
test("a Go key Command Code turns away is a 401, the account kept", async () => {
expect(await signIn(() => Response.json({ error: { message: "Invalid API key" } }, { status: 401 }))).toEqual({ status: 401, signIn: "kept" })
})
test("an error line saying 401 keeps the account too", async () => {
const line = JSON.stringify({ type: "error", error: { message: "Unauthorized", statusCode: 401 } })
expect(await signIn(() => new Response(line + "\n"))).toEqual({ status: 401, signIn: "kept" })
})
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" })
})
// 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" })
})