From 53d48137437a6793b798ae26ffab0c0e1d84851a Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 11:46:50 +0800 Subject: [PATCH] copilot 0.1.1: a refused token exchange marks the sign-in, a quota read GitHub answers clears it (#618) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jinpy666 found idle Copilot slots saying "sign-in has expired — sign in again" while their OAuth token still traded at /copilot_internal/v2/token. Two things in the plugin kept the mark wrong: - The loader's token exchange threw a plain "Token refresh failed: N" for every failure, so a refresh GitHub really refused (401) never told magpie the sign-in was gone. It now carries signIn "expired" on a 401 only; 403, 429 and 5xx carry nothing, as before. - Every quota read said signIn "kept", so a read GitHub answered with the OAuth token never cleared a mark. It now says "renewed" on any 200 with a JSON object (readable quota or not). A 401 still says "expired"; every other failure still "kept". Accounts marked before this are cleared on their next quota read. magpie's side (a bare 401 on a stored access token with a refresh token makes the token due instead of marking the account) ships in magpie itself. Run (mote, bun 1.4.2, at this commit): `bun test` in packages/copilot, 42 pass; `bun scripts/check.mjs copilot` ok. Broken by hand: with index.mjs and usage.mjs back at 0.1.0 the two new tests fail (the exchange's 401 has no signIn; the 200 read says kept). Not tried: a real Copilot account, a real 401 from the exchange. --- packages/copilot/README.md | 7 ++++++- packages/copilot/index.mjs | 9 ++++++++- packages/copilot/index.test.mjs | 20 ++++++++++++++++++++ packages/copilot/package.json | 2 +- packages/copilot/usage.mjs | 5 ++++- packages/copilot/usage.test.mjs | 13 +++++++++++++ 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/copilot/README.md b/packages/copilot/README.md index a17dfbc..97bd718 100644 --- a/packages/copilot/README.md +++ b/packages/copilot/README.md @@ -54,6 +54,9 @@ magpie calls `auth.usage(getAuth, provider)` independently of model requests. It reads `GET /copilot_internal/user` on `api.github.com` or the account's `api.` using the GitHub OAuth token, **not** the short-lived inference token. Reading quota neither renews nor persists either token. +When the inference token exchange itself answers 401, the request fails +with `signIn: "expired"` and magpie asks for a new sign-in; any other +failure there leaves the account for the next request. OpenCode ignores magpie's additional hook. - Only `chat`, `completions` and `premium_interactions` snapshots are read; @@ -82,7 +85,9 @@ OpenCode ignores magpie's additional hook. overage permission or adds the separate overage counter to included usage. - Empty, malformed or unavailable quota data is unknown, not zero usage. Only an explicit HTTP 401 marks the sign-in expired. Network errors, rate - limits, 403s and server errors keep the sign-in. No response body or token is + limits, 403s and server errors keep the sign-in. A read GitHub answers + clears a mark, since the OAuth token it took is what renews the inference + token (an idle account whose inference token lapsed, #618). No response body or token is included in error messages. Quota requests have a timeout and reject redirects. GitHub's quota endpoint is internal and may change. An organization may not diff --git a/packages/copilot/index.mjs b/packages/copilot/index.mjs index f8c0e87..56e8cc1 100644 --- a/packages/copilot/index.mjs +++ b/packages/copilot/index.mjs @@ -74,7 +74,14 @@ export async function CopilotAuthPlugin({ client }) { }); if (!response.ok) { - throw new Error(`Token refresh failed: ${response.status}`); + // GitHub's 401 here is the OAuth token itself turned away: + // magpie marks the account to sign in again. Anything + // else (no seat, a rate limit, an outage) leaves it as + // it is. + throw Object.assign( + new Error(`Token refresh failed: ${response.status}`), + response.status === 401 ? { signIn: "expired" } : {}, + ); } const tokenData = await response.json(); diff --git a/packages/copilot/index.test.mjs b/packages/copilot/index.test.mjs index 28c6516..9bb9581 100644 --- a/packages/copilot/index.test.mjs +++ b/packages/copilot/index.test.mjs @@ -260,3 +260,23 @@ test("device flow keeps Enterprise metadata without redirecting auth to another } } }); + +// plugins#618: only GitHub turning the OAuth token away at the token +// exchange marks the account; another failure leaves it for the next try. +test("a refused token exchange says the sign-in expired, another failure doesn't", async () => { + for (const [status, signIn] of [[401, "expired"], [403, undefined], [500, undefined]]) { + const seen = []; + globalThis.fetch = async (url) => { + seen.push(url); + return new Response("{}", { status }); + }; + const hooks = await CopilotAuthPlugin({ client: { auth: { set: async () => assert.fail("saved a refused exchange") } } }); + const loader = await hooks.auth.loader(async () => signedIn({ access: "", expires: 0 }), { models: {} }); + const err = await loader.fetch(`${loader.baseURL}/responses`, { body: "{}", headers: {} }).then(() => undefined, (e) => e); + assert.ok(err, `HTTP ${status} went through`); + assert.equal(err.signIn, signIn); + assert.match(err.message, new RegExp(String(status))); + assert.ok(!err.message.includes("mock-github-oauth")); + assert.equal(seen.length, 1); + } +}); diff --git a/packages/copilot/package.json b/packages/copilot/package.json index f6c1e4f..9355e3b 100644 --- a/packages/copilot/package.json +++ b/packages/copilot/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-copilot-auth", - "version": "0.1.0", + "version": "0.1.1", "description": "GitHub Copilot on GitHub.com and GitHub Enterprise, with account quota and credit balances for magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/copilot/usage.mjs b/packages/copilot/usage.mjs index f1a0ab8..b05bd6f 100644 --- a/packages/copilot/usage.mjs +++ b/packages/copilot/usage.mjs @@ -153,7 +153,10 @@ export async function readUsage(getAuth) { if (!data || typeof data !== "object" || Array.isArray(data)) { throw new Error("Invalid quota response"); } - return parseUsage(data); + // GitHub took the OAuth token, the sign-in that renews the inference + // token: an account magpie marked for a dropped inference token (an + // idle slot, plugins#618) is signed in, whatever the quota says. + return { ...parseUsage(data), signIn: "renewed" }; } catch { // Never include request headers, OAuth tokens or response bodies in errors. return { diff --git a/packages/copilot/usage.test.mjs b/packages/copilot/usage.test.mjs index bf881fc..7e9d79b 100644 --- a/packages/copilot/usage.test.mjs +++ b/packages/copilot/usage.test.mjs @@ -398,3 +398,16 @@ test("a free seat omits absent premium allowance but counts chat and completion assert.deepEqual(result.windows.map((w) => w.used), [20, 0]); assert.equal(result.windows[1].aside, true); }); + +// plugins#618 (jinpy666): an idle slot magpie had marked for its dropped +// inference token is told signed in again by a quota read GitHub answers +// with the OAuth token; a 401 there still marks it. +test("a quota read GitHub answers clears a stale mark, a 401 still sets one", async () => { + const ok = await readUsage(getAuth, async () => response(premium({ entitlement: 300, remaining: 200 }))); + assert.equal(ok.signIn, "renewed"); + const unreadable = await readUsage(getAuth, async () => response({ copilot_plan: "business" })); + assert.equal(unreadable.signIn, "renewed"); + assert.ok(unreadable.error); + const refused = await readUsage(getAuth, async () => response({}, 401)); + assert.equal(refused.signIn, "expired"); +});