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"); +});