jinpy666 (magpie-community/plugins#618): with Copilot plugin 0.1.0, the two Copilot slots that carried no traffic said "sign-in has expired — sign in again" within a day or two, while their GitHub OAuth token still traded at /copilot_internal/v2/token. A plugin's bare 401 (no X-Magpie-Sign-In) marked the account whatever the cause, and a mark is cleared only by a success or a usage read that says so, which an idle slot never gets. The host now judges such a 401 against the sign-in as the request found it (staleAccess in host.js). For an OAuth sign-in with a refresh token whose access token is still the stored one and not due yet, it makes the token due (expires 1, under the auth lock, only while the access token is unchanged), drops the account's loader and says "kept": the gateway fails the request over as before, and the next request renews the token through the plugin's loader (or magpie's auth.refresh) and goes through. Still marked: an access token renewed for this request and turned away, one that was due already and not renewed, an account with no refresh token, and a plugin saying "expired" (a refused refresh). A sign-in that can't be read when the 401 comes is unknown: kept, not marked. A marked account stays routable (pluginAlsoOn doesn't filter marks), so a slot marked before this is renewed on its next request and the success clears it. Copilot isn't a mover; its plugin 0.1.1 (plugins 53d4813, on the dist branch; never on npm) says "expired" for the exchange's own 401 and "renewed" for a quota read GitHub answers, which clears old marks. TestPluginStaleAccessRenewsNotLapses (new, fake plugin + local vendor, no network): idle account kept and due after the 401, renewed and 200 on the next request; a renewed token refused is marked; a refused refresh (signIn "expired") is marked; plugin-auth.json unreadable at the 401 marks nothing; a marked account is still routed. TestPluginFailover's "gone" case asserted one 401 marks; it now asserts no mark after that 401 and the mark once the account's renewed token is refused too. docs/subsystems/provider-plugins.md has the rule. Run at this commit's tree (mote, rebased on541102eba): - go test -count=1 -timeout 45m ./internal/plugin/ ./internal/provider/ ./internal/gateway/: plugin ok, provider ok; gateway red on TestCodexPoolSaysTheClientsVersion (#4807) and TestUnroutedHeldReportSaved (#4300), both filed, full-run-only; both pass alone here and at541102eba. - GOOS=linux and GOOS=windows go vet of the three packages: ok. - gofmt -l: clean. Broken by hand: host.js at the old code, the new test is red ("idle@fake was marked for a dropped access token its refresh token renews"); the unknown branch returning "", red on unknown@fake. Not tried: a real Copilot account (or any real vendor's dropped access token); which 401 marked the reporter's slots first isn't confirmed (a request failed over to them, most likely); a plugin that keeps a future expires but never renews (it is marked on its second 401, not its first); GUI unchanged, no GUI run.
194 lines
6.7 KiB
Go
194 lines
6.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// An idle OAuth account whose access token the vendor dropped before the
|
|
// expiry it was stored with, while its refresh token still renews, is not
|
|
// a sign-in gone (plugins#618, jinpy666: three Copilot slots, the two
|
|
// with no traffic said "sign-in has expired — sign in again" though their
|
|
// refresh tokens still traded at /copilot_internal/v2/token). The bare
|
|
// 401 makes the access due and keeps the account; the next request
|
|
// renews it through the plugin's loader and goes through. An account
|
|
// whose renewed token is turned away too is marked, and so is one whose
|
|
// plugin says its refresh was refused; a sign-in that can't be read when
|
|
// the 401 comes is unknown, not marked. A marked account is still one a
|
|
// request can be routed to, for the loader to renew on use.
|
|
func TestPluginStaleAccessRenewsNotLapses(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
claudeHome(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Cleanup(plugin.Settle)
|
|
var unreadable func()
|
|
testenv.FakeVendor(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
switch r.Header.Get("Authorization") {
|
|
case "Bearer fresh-r-ok", "Bearer fresh-r-unknown":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"x","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`)
|
|
return
|
|
case "Bearer a-unknown":
|
|
unreadable()
|
|
}
|
|
// the access token turned away, as Copilot's API answers one it
|
|
// no longer takes, whatever the stored expiry said
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
io.WriteString(w, `{"error":{"message":"unauthorized: token expired","code":"token_expired"}}`)
|
|
}))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the reporter's shape: access stored with an expiry still ahead,
|
|
// refresh good; "r-bad"'s renewed token is turned away too
|
|
accts := []struct{ user, refresh, access string }{
|
|
{"idle@fake", "r-ok", "a"},
|
|
{"bad@fake", "r-bad", "a"},
|
|
{"revoked@fake", "r-revoked", "a"},
|
|
{"unknown@fake", "r-unknown", "a-unknown"},
|
|
{"first@fake", "r-first", "fresh-r-first"},
|
|
}
|
|
keys := map[string]string{}
|
|
var rows []savedLogin
|
|
for _, a := range accts {
|
|
k, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": a.refresh, "access": a.access, "expires": float64(time.Now().Add(20 * time.Hour).UnixMilli()), "accountId": a.user})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys[a.user] = k
|
|
rows = append(rows, savedLogin{Agent: "plugin:fakeco", User: a.user, Home: k, On: true})
|
|
}
|
|
saveLogins(t, rows...)
|
|
if _, err := plugin.Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
authPath := plugin.AuthPath()
|
|
unreadable = func() {
|
|
for _, p := range []string{authPath, authPath + ".bak"} {
|
|
if err := os.WriteFile(p, []byte("{half"), 0o600); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
}
|
|
stored := func(user string) map[string]any {
|
|
t.Helper()
|
|
b, err := os.ReadFile(authPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var all map[string]map[string]any
|
|
if err := json.Unmarshal(b, &all); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return all[keys[user]]
|
|
}
|
|
lapsed := func(user string) bool {
|
|
for _, l := range pluginLogins(mustPlugin(t)) {
|
|
if l.User == user {
|
|
return l.Lapsed != ""
|
|
}
|
|
}
|
|
t.Fatalf("no row for %s", user)
|
|
return false
|
|
}
|
|
ask := func(user string) int {
|
|
t.Helper()
|
|
pp := mustPlugin(t)
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", pluginBase+pp.ID+"/v1/chat/completions", strings.NewReader(`{"model":"fake-claude","messages":[{"role":"user","content":"hi"}]}`))
|
|
resp, err := pluginFetch(pp, keys[user], req)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
if resp.Header.Get(SignInHeader) != "" {
|
|
t.Errorf("%s: %s went on to the client", user, SignInHeader)
|
|
}
|
|
return resp.StatusCode
|
|
}
|
|
|
|
// idle: the first request meets the dropped token, the second renews
|
|
if got := ask("idle@fake"); got != http.StatusUnauthorized {
|
|
t.Fatalf("idle@fake's first request: %d", got)
|
|
}
|
|
if lapsed("idle@fake") {
|
|
t.Fatal("idle@fake was marked for a dropped access token its refresh token renews")
|
|
}
|
|
if a := stored("idle@fake"); a["refresh"] != "r-ok" || a["access"] != "a" || a["expires"] != float64(1) {
|
|
t.Fatalf("idle@fake isn't due for renewal: %v", a)
|
|
}
|
|
if got := ask("idle@fake"); got != http.StatusOK {
|
|
t.Fatalf("idle@fake's second request: %d", got)
|
|
}
|
|
if a := stored("idle@fake"); a["access"] != "fresh-r-ok" || a["refresh"] != "r-ok" {
|
|
t.Fatalf("idle@fake wasn't renewed: %v", a)
|
|
}
|
|
if lapsed("idle@fake") {
|
|
t.Fatal("idle@fake is marked after it renewed")
|
|
}
|
|
|
|
// bad: renewed, and the renewed token turned away too
|
|
if got := ask("bad@fake"); got != http.StatusUnauthorized || lapsed("bad@fake") {
|
|
t.Fatalf("bad@fake's first request: %d, lapsed %v", got, lapsed("bad@fake"))
|
|
}
|
|
if got := ask("bad@fake"); got != http.StatusUnauthorized {
|
|
t.Fatalf("bad@fake's second request: %d", got)
|
|
}
|
|
if !lapsed("bad@fake") {
|
|
t.Fatal("bad@fake isn't marked though its renewed token was turned away")
|
|
}
|
|
// a lapsed account is still one to route to: the first of the
|
|
// provider, or one beside it
|
|
routed := slices.ContainsFunc(pluginAlsoOn(mustPlugin(t)), func(p Provider) bool { return p.Account != nil && p.Account.pluginKey == keys["bad@fake"] })
|
|
for _, l := range pluginLogins(mustPlugin(t)) {
|
|
routed = routed || l.User == "bad@fake" && l.Active
|
|
}
|
|
if !routed {
|
|
t.Error("bad@fake left the accounts a request is routed to")
|
|
}
|
|
|
|
// revoked: the plugin says its refresh was refused, and is believed
|
|
if got := ask("revoked@fake"); got != 0 {
|
|
t.Fatalf("revoked@fake's request went through: %d", got)
|
|
}
|
|
for deadline := time.Now().Add(10 * time.Second); !lapsed("revoked@fake"); time.Sleep(50 * time.Millisecond) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("revoked@fake isn't marked though its plugin said its refresh was refused")
|
|
}
|
|
}
|
|
|
|
// unknown: plugin-auth.json can't be read when the 401 comes
|
|
good, err := os.ReadFile(authPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ask("unknown@fake"); got != http.StatusUnauthorized {
|
|
t.Fatalf("unknown@fake's request: %d", got)
|
|
}
|
|
if err := os.WriteFile(authPath, good, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if lapsed("unknown@fake") {
|
|
t.Fatal("unknown@fake was marked while its sign-in couldn't be read")
|
|
}
|
|
if lapsed("first@fake") || lapsed("idle@fake") {
|
|
t.Fatal("an account no request failed was marked")
|
|
}
|
|
}
|