A plugin that answered "this model isn't available on your plan" with 401
marked the account as needing a new sign-in, and the gateway fails a 401
over to the provider's next account (retryable in fallback.go), so every
account of the provider was marked "sign-in has expired" for credentials
that were fine.
notePluginSignIn now reads a 401 that carries no X-Magpie-Sign-In as
"kept" when its body says the model isn't served (provider.ModelUnserved,
the gateway's unservedWords moved here, read decoded by catalog.Words)
and nothing of a token, key, credential, session or sign-in. Only the
first 64KB is read, and the body goes on whole.
Not changed: the gateway still rests the account under its failure
backoff for that answer; modelRefused still reads only 400/403/404/422 as
the model refused. The guide asks for 403/404 (docs commit).
The 401 bodies the community plugins hold for a sign-in that is gone
(magpie-community/plugins 95fa761, every package's tests), each in the
test's "marks" list and none read as kept (none names a model, which
ModelUnserved needs):
- devin: {"code":"unauthenticated","message":"invalid api key" / "bad key"}
- zed: "unauthorized", "", {"code":"upstream","message":"bad key",...}
- cline: {"error":{"message":"nope"}}, {"success":false,"error":
"unauthorized"}, {"message":"expired"}, "the sign-in lapsed — sign in
again"
- raccoon: "unauthorized", {"code":200003,"message":
"authorization_verify_error"}
- joycode: {"code":401,"msg":"登录票据已失效"}, "private-marker"
- zcode: "", {"code":1001,"msg":"Authorization Token非法"},
{"error":{"message":"token expired or incorrect"}}
- mimo: "", {"code":401}, {"error":{"message":"stale"}},
{"error":{"message":"Invalid API Key"}}
- cursor: "no", "Unauthorized — sign in to Cursor again in magpie",
Connect's ERROR_NOT_LOGGED_IN body
- search1api: {"message":"expired"}
No plugin source builds a 401 whose text names a model.
Run (mote, at this tree): gofmt -l clean; go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./...; go test -tags nogui
-timeout 45m ./...
(go test -tags nogui -timeout 45m ./... at base 5bfd76cd8: 77 packages ok, none failed (log kept whole). Rebased onto 602ad84ad and run again there: vet on three OSes ok; go test -tags nogui -count=1 ./internal/provider (195s) ./internal/plugin (268s) ./internal/gateway (677s) ok. Rebased onto b5d2858f4 and run again: usage, provider, plugin, gateway ok. Rebased onto 87a983423, the pushed base, and run again at this tree: gofmt -l clean; go vet ./internal/... ok, GOOS=linux and windows go vet of provider, plugin, gateway ok; go test -tags nogui -count=1 ./internal/provider (126s) ./internal/plugin (180s) ./internal/gateway (588s) ./internal/codexcat (1s) ok. Then rebased onto 3ec9babe7 to push; its three commits (WorkBuddy mover min, redact, sessions) share no file with this one, and the same packages are run again at the pushed head after the push).
Broken by hand: with the check disabled (if false &&),
TestPluginModel401KeepsSignIn is red: "a 401 about the model marked the
sign-in: The model claude-opus-5 is not available on your plan".
Not tried: a real vendor answering a model-level 401 through a plugin.
101 lines
4.1 KiB
Go
101 lines
4.1 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// A plugin answering 401 for a model the account may not use, and saying
|
|
// nothing of the sign-in, leaves the account's sign-in as it is: the
|
|
// gateway fails the request over to every account, and each was marked
|
|
// as needing a new sign-in for a credential that was fine (plugins#468).
|
|
// A 401 about the sign-in still marks it, one naming a model beside its
|
|
// token words included, and the body reaches the gateway whole.
|
|
func TestPluginModel401KeepsSignIn(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
claudeHome(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Cleanup(plugin.Settle)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key, err := plugin.Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r-ok", "access": "a", "expires": 9e15, "accountId": "ok@fake"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
saveLogins(t, savedLogin{Agent: "plugin:fakeco", User: "ok@fake", Home: key, On: true})
|
|
if _, err := plugin.Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pp := mustPlugin(t)
|
|
lapsed := func() bool {
|
|
for _, l := range pluginLogins(mustPlugin(t)) {
|
|
return l.Lapsed != ""
|
|
}
|
|
t.Fatal("no account")
|
|
return false
|
|
}
|
|
answer := func(status int, body string) {
|
|
t.Helper()
|
|
resp := &http.Response{StatusCode: status, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))}
|
|
notePluginSignIn(pp, key, resp)
|
|
got, err := io.ReadAll(resp.Body)
|
|
if err != nil || string(got) != body {
|
|
t.Fatalf("the body after the check: %q, %v; want %q", got, err, body)
|
|
}
|
|
}
|
|
for _, body := range []string{
|
|
`{"error":{"message":"The model claude-opus-5 is not available on your plan"}}`,
|
|
`{"error":{"code":"model_not_found","message":"unknown model"}}`,
|
|
`{"error":{"message":"模型不支持"}}`, // 模型不支持, escaped as a vendor sends it
|
|
} {
|
|
answer(http.StatusUnauthorized, body)
|
|
if lapsed() {
|
|
t.Fatalf("a 401 about the model marked the sign-in: %s", body)
|
|
}
|
|
}
|
|
// and every 401 body the community plugins' tests hold for a sign-in
|
|
// that is gone (magpie-community/plugins at 95fa761): each one marks
|
|
for _, body := range []string{
|
|
`{"error":{"message":"invalid token"}}`,
|
|
`{"error":{"message":"model gpt-x: invalid api key"}}`,
|
|
``,
|
|
`{"code":"unauthenticated","message":"invalid api key"}`, // devin
|
|
`{"code":"unauthenticated","message":"bad key"}`, // devin
|
|
`{"code":"upstream","message":"bad key","upstream_status":401}`, // zed
|
|
`unauthorized`, // zed, raccoon
|
|
`{"error":{"message":"nope"}}`, // cline
|
|
`{"success":false,"error":"unauthorized"}`, // cline
|
|
`{"message":"expired"}`, // cline, search1api
|
|
`the sign-in lapsed — sign in again`, // cline
|
|
`{"code":200003,"message":"authorization_verify_error"}`, // raccoon
|
|
`{"code":401,"data":null,"msg":"登录票据已失效"}`, // joycode
|
|
`private-marker`, // joycode
|
|
`{"code":1001,"msg":"Authorization Token非法"}`, // zcode
|
|
`{"error":{"message":"token expired or incorrect"}}`, // zcode
|
|
`{"code":401}`, // mimo
|
|
`{"error":{"message":"stale"}}`, // mimo
|
|
`{"error":{"message":"Invalid API Key"}}`, // mimo
|
|
`no`, // cursor
|
|
`Unauthorized — sign in to Cursor again in magpie`, // cursor
|
|
`{"code":"unauthenticated","message":"Error","details":[{"debug":{"error":"ERROR_NOT_LOGGED_IN","details":{"title":"Authentication error","detail":"If you are logged in, try logging out and back in."}}}]}`, // cursor
|
|
} {
|
|
answer(http.StatusOK, "{}")
|
|
answer(http.StatusUnauthorized, body)
|
|
if !lapsed() {
|
|
t.Fatalf("a 401 about the sign-in didn't mark it: %q", body)
|
|
}
|
|
}
|
|
}
|