copilot 0.1.1: a refused token exchange marks the sign-in, a quota read GitHub answers clears it (#618)
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.
This commit is contained in:
@@ -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.<enterpriseUrl>` 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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user