Files
magpie/internal/provider/copilot_lapsed.go
T
yetone 86b2f4d053 provider, gateway, gui: an account whose sign-in its vendor revoked or refused reads as signed out and is passed over, not asked on every request (#1793 ysicing, echo_ts on Discord)
What changed, one fix for the class:

- ChatGPT and the other built-in subscriptions (#1793): ChatGPT answered
  401 token_revoked on every request, request after request (~18 rows in
  the reporter's log), because a refused sign-in was just "other" and
  rested a while, then asked again. Now a 401 whose words say the sign-in
  is gone (provider.SignInRevoked: token_revoked, token_invalidated,
  invalid_grant, refresh_token_reused/invalidated/expired; never
  token_expired, a 403, a 5xx or a network error) marks the account for
  the credential it was sent with (a 12-byte hash of the header, never the
  credential), in logins.json as Refused "revoked:<mark>" plus Lapsed.
  A saved ChatGPT account is refreshed first, as Codex does on a 401; a
  refresh that goes through marks nothing. The routing loop fails it as
  failAuth (next member at once, no rest); forwardOnce answers its later
  requests with magpie's own 401 without asking the vendor; compaction
  on a magpie account (compactOn) picks a marked one last. Signed in anew
  (upsertLogin with another credential, addSideLogin), refreshed, or a
  request served, the mark is gone. Claude, Copilot and plugins keep
  their own marks; a key's provider answering the same words rests as
  before.
- Copilot (echo_ts): a built-in Copilot account whose GitHub token GitHub
  refused (401/403 on the token exchange, the CLI stand-in or the usage
  read) is marked lapsed on its own account (copilot_lapsed.go), unless a
  working CLI stand-in serves; cleared when a token is served or it signs
  in again. 5xx and network errors never mark it.
- GUI: such an account's row says Sign-in required with Sign in again
  (Copilot's on its GHE.com host) and Remove, the reason on its allowance
  line in the user's language (zh, zh-TW, ja, de added); the card says
  Signed out once every account in use is.
- docs/subsystems: providers-accounts "A revoked sign-in", gateway-routing.

What was run (sandbox HOME, caches pinned, worktree only):
- go test -race on the new tests: provider TestSignInRevokedWords,
  TestRevokedSideLoginMarkedTillSignedInAgain, TestRevokedCodexRefreshedFirst,
  TestRevokedSignInLeavesOthersMarks, TestUpsertLoginKeepsRevokedMarkForTheSameSignIn,
  TestCopilot* (7); gateway TestRevokedSignInPassedOverTillSignedInAgain
  (fake ChatGPT backend, zstd, real routing: turn 1 asks acct-1 then
  acct-2, turns 2-4 and after a restart acct-2 only with no rests, after a
  new auth.json acct-1 again and the mark gone),
  TestRevokedSignInNotCompactedOnAgain, TestRevokedWordsFromAKeyRestAsBefore.
- Broken by hand, each turned a test red: the forwardOnce short-circuit,
  the failureOf clause, noteSignIn in codexUpstreamOn, compactOn's skip,
  ServedSignIn, its in-memory delete, the 401-only check, refresh-first,
  upsertLogin's keep and its sameAuth, sideLogins' Lapsed and
  addSideLogin's clear; and the six Copilot breaks. GUI: the signedOut
  and quotaError lines each turn revoked-signin-account red.
- GUI (Chromium and WebKit, 420 and 1100, en/zh/zh-TW/ja/de):
  revoked-signin-account (new), copilot-signed-out-account (new), and the
  account-*, claude-*, codex-*, plugin-*, quota-*, signin-*, routing-*,
  usage-accounts* suites: 1605 pass, 7 fail. Six are WebKit
  routing-group-fold/routing-sessions/routing-steady cases that fail the
  same on a clean 9964535ad run alone (#1672, #1612, #1307, #1804); one,
  signin-proxy Chromium de 380, timed out under load (load ~60-75) and
  passed 2 of 2 after, filed as #1814.
- gofmt; go vet darwin, linux (nogui) and windows; linux and windows
  builds. go test ./...: every package ok but gateway, where
  TestOTelFastRequestTranscriptDiscovery and
  TestOTelWaterfallIncludesUnbilledFallback failed under load (#1794);
  both pass alone, -count=3.

Not verified: no real revoked ChatGPT or Copilot account was tried; the
words come from the reporter's screenshot and OpenAI's documented codes.
Codex's own relayed sign-in (not one of magpie's accounts) isn't marked:
Codex refreshes and signs itself in. The short-circuit in
codexUpstreamOn (compaction on a marked account, reached only when every
account is marked) has no test of its own; plugins are left to their
plugin.
2026-10-12 22:16:17 +08:00

144 lines
4.7 KiB
Go

package provider
// A Copilot account whose GitHub token GitHub refuses is signed out: GitHub
// revokes a token (signed out on github.com, the OAuth app's access taken
// back, a token unused for a year), and every request then failed with
// "Copilot is signed out …; sign in to Copilot again" while the account
// still read as signed in, to be removed and added anew (echo_ts on
// Discord). Such an account is now marked lapsed on its own row, where it
// is signed in again in place, its settings kept.
//
// Only GitHub's refusal of the token marks it — 401 on the trade for a
// session token or on the CLI's /copilot_internal/user, 403 on the trade
// — never a 5xx or a network error; and an editor's token refused while
// the Copilot CLI's sign-in of the same account stands in for it is no
// lapse (#1238). The mark is kept with a digest of the token refused, so
// a new sign-in (magpie's, the editor's or the CLI's) is never called
// signed out for the old one, and a request served on the account clears it.
import (
"crypto/sha256"
"encoding/hex"
"errors"
"slices"
"strings"
"sync"
)
// copilotLapsedText is what a lapsed Copilot account's row says.
const copilotLapsedText = "GitHub refused this Copilot sign-in; sign in again"
// copilotTokenMark tells a GitHub token from another without keeping it.
func copilotTokenMark(token string) string {
sum := sha256.Sum256([]byte("copilot\x00" + token))
return hex.EncodeToString(sum[:12])
}
// copilotLapsedOn says a saved Copilot account is signed out with token:
// it was marked so while it had this very token.
func copilotLapsedOn(l savedLogin, token string) bool {
return l.Lapsed != "" && token != "" && slices.Contains(strings.Split(l.Refused, ","), copilotTokenMark(token))
}
// copilotLapsedSeen is the Copilot accounts known marked lapsed, by
// accountKey: a request served on one clears its mark without reading
// logins.json at every request.
var (
copilotLapsedMu sync.Mutex
copilotLapsedSeen = map[string]bool{}
)
func copilotLapsedNote(user string, lapsed bool) {
copilotLapsedMu.Lock()
defer copilotLapsedMu.Unlock()
if lapsed {
copilotLapsedSeen[accountKey(user)] = true
} else {
delete(copilotLapsedSeen, accountKey(user))
}
}
var errUnchanged = errors.New("unchanged")
// copilotLapsedBy says GitHub refused app's sign-in, with nothing standing
// in for it: its token refused, and an editor's token's stand-in, the
// Copilot CLI's sign-in of the same account, refused too or not there.
func copilotLapsedBy(app copilotApp) bool {
if !copilotRefusedToken(app.Token) {
return false
}
if app.cli || app.src == "" {
return true
}
if cli, ok := copilotStandIn(app); ok && !copilotRefusedToken(cli.Token) {
return false
}
return true
}
// copilotLapse marks the account of app signed out, with the token GitHub
// refused (and, for an editor's, its stand-in's too, which copilotLogins
// may list it with).
func copilotLapse(app copilotApp) {
if app.User == "" {
return
}
marks := []string{copilotTokenMark(app.Token)}
if app.src != "" && !app.cli {
if cli, ok := copilotStandIn(app); ok {
marks = append(marks, copilotTokenMark(cli.Token))
}
}
err := editSideLogin("copilot", app.User, func(ls []savedLogin, i int) ([]savedLogin, error) {
was := strings.Split(ls[i].Refused, ",")
if ls[i].Lapsed == copilotLapsedText && !slices.ContainsFunc(marks, func(m string) bool { return !slices.Contains(was, m) }) {
return nil, errUnchanged
}
if ls[i].Lapsed != copilotLapsedText {
was = nil
}
for _, m := range marks {
if !slices.Contains(was, m) {
was = append(was, m)
}
}
ls[i].Lapsed, ls[i].Refused = copilotLapsedText, strings.Join(slices.DeleteFunc(was, func(m string) bool { return m == "" }), ",")
return ls, nil
})
if err == nil || errors.Is(err, errUnchanged) {
copilotLapsedNote(app.User, true)
}
}
// copilotUnlapse clears the mark of the account of app: GitHub served it.
func copilotUnlapse(app copilotApp) {
copilotLapsedMu.Lock()
seen := copilotLapsedSeen[accountKey(app.User)]
copilotLapsedMu.Unlock()
if !seen || app.User == "" {
return
}
err := editSideLogin("copilot", app.User, func(ls []savedLogin, i int) ([]savedLogin, error) {
if ls[i].Lapsed == "" && ls[i].Refused == "" {
return nil, errUnchanged
}
ls[i].Lapsed, ls[i].Refused = "", ""
return ls, nil
})
if err == nil || errors.Is(err, errUnchanged) {
copilotLapsedNote(app.User, false)
}
}
// copilotSessionDone notes what GitHub said of app's sign-in at a trade:
// served, its mark goes; refused with nothing in its place, it is marked.
func copilotSessionDone(app copilotApp, err error) {
if err == nil {
copilotUnlapse(app)
return
}
if copilotLapsedBy(app) {
copilotLapse(app)
}
}