diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 5319ebe62..122233bad 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -3684,11 +3684,12 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro t.Prompt = livePrompt(t.Prompt, window) } }) - held := false // answered as its vendor did a moment ago, without asking - var heldBack time.Time // when the first of those held out of their allowance is back - var queued int64 // ms it waited for a slot of its key's or account's - var laneErr error // turned away by its key's or account's queue - at := &wireAt{} // the level a translated request was built at + held := false // answered as its vendor did a moment ago, without asking + heldRestricted := false // held as an account its vendor restricted (restrictedHeld) + var heldBack time.Time // when the first of those held out of their allowance is back + var queued int64 // ms it waited for a slot of its key's or account's + var laneErr error // turned away by its key's or account's queue + at := &wireAt{} // the level a translated request was built at // the models Copilot's Auto picked for it, where from, and which // were refused (#256) autoPicked := func() []provider.AutoPick { return nil } @@ -3699,6 +3700,13 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro held, call.Status, call.Error = true, http.StatusForbidden, said noteMember(hw, r, c.p, c.model) writeError(hw, from, call.Status, said) + } else if status, said, ok := restrictedHeld(c); ok && last { + // its vendor restricted the account, and it rests so: the + // refusal is given again without asking (restrictedHeld), + // until the rest ends or is lifted + held, heldRestricted, call.Status, call.Error = true, true, status, said + noteMember(hw, r, c.p, c.model) + writeError(hw, from, call.Status, said) } else if rested, back, ok := quotaHeld(cands[i:]); ok && last { // it rests out of its allowance, and so does every key that // was left (#1772: four Ollama Cloud keys out for the week, @@ -3840,8 +3848,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro stop() } if handed.release != nil && handed.who == c.who() { - // answered without asking (verifyHeld, quotaHeld): the slot - // taken for it goes back + // answered without asking (verifyHeld, restrictedHeld, + // quotaHeld): the slot taken for it goes back handed.release() handed.release = nil } @@ -5034,6 +5042,11 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro try.Fail = failPrompt } else { try.Fail = failureOf(c, call.Status, []byte(call.Error)) + if heldRestricted { + // its words may be the vendor's alone, read as + // restricted by its code (WorkBuddy's 11140), gone here + try.Fail = failRestricted + } if offlineFailure(c, call.Status, []byte(call.Error)) { // the last one left failed for want of a network // (offline.go): told so, and it doesn't rest @@ -5041,7 +5054,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro } // and one its vendor restricted (riskRefused), for the next // request not to be sent to it again - if (try.Fail == failVerify || try.Fail == failRestricted) && !held || strictAlone { + // A refusal given again without asking (restrictedHeld) + // rests nobody anew: its rest stands as it was taken, and + // ends when it would + if ((try.Fail == failVerify || try.Fail == failRestricted) && !held || strictAlone) && !heldRestricted { // the last one left rests too, for the app to show and the // next requests to be held rest := s.restAfter(c, call.Status, hw.header, []byte(call.Error)) diff --git a/internal/gateway/restricted_held_test.go b/internal/gateway/restricted_held_test.go new file mode 100644 index 000000000..c10b8f71f --- /dev/null +++ b/internal/gateway/restricted_held_test.go @@ -0,0 +1,161 @@ +package gateway + +import ( + "io" + "net/http" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// agyTurn sends one chat turn for an Antigravity model to s. +func agyTurn(s *Server) (int, string) { + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"antigravity/gemini-3.8-flash","stream":false,"messages":[{"role":"user","content":"hi"}]}`))) + return rec.Code, rec.Body.String() +} + +// Every account Google suspended, resting as restricted, is not asked +// again while it rests: the agent is given Google's refusal at once, and +// the rest isn't taken anew, so it ends when it would. Before, restLast +// and probeOne kept one resting account for each request, and every +// request was sent to one of them and refused (leowang on Discord: two +// suspended Antigravity accounts, 23 and 22 tries, none answered). Lifted +// on the Routing page, the account is asked again. +func TestSuspendedAccountsNotAskedWhileTheyRest(t *testing.T) { + var asked []string + s, _ := antigravityGroupFor(t, []string{"u1@example.com", "u2@example.com"}, "gemini-3.8-flash", func(w http.ResponseWriter, r *http.Request) { + asked = append(asked, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusForbidden) + io.WriteString(w, antigravitySuspended) + }) + if code, out := agyTurn(s); code != 403 || !strings.Contains(out, "violation of Terms of Service") || !slices.Equal(asked, []string{"u1@example.com", "u2@example.com"}) { + t.Fatalf("first turn: %d %s, asked %v", code, out, asked) + } + r1, ok1 := restOf("antigravity@u1@example.com") + r2, ok2 := restOf("antigravity@u2@example.com") + if !ok1 || !ok2 || r1.Why != failRestricted || r2.Why != failRestricted { + t.Fatalf("rests %+v %v, %+v %v", r1, ok1, r2, ok2) + } + for n := 2; n <= 6; n++ { + asked = nil + code, out := agyTurn(s) + if code != 403 || !strings.Contains(out, "violation of Terms of Service") || len(asked) != 0 { + t.Fatalf("turn %d: %d %s, asked %v", n, code, out, asked) + } + if tr := lastRoute(s).Tries; len(tr) == 0 || tr[len(tr)-1].Fail != failRestricted { + t.Fatalf("turn %d's tries %+v", n, tr) + } + } + if r, _ := restOf("antigravity@u1@example.com"); !r.Until.Equal(r1.Until) { + t.Fatalf("u1's rest moved from %v to %v", r1.Until, r.Until) + } + if r, _ := restOf("antigravity@u2@example.com"); !r.Until.Equal(r2.Until) { + t.Fatalf("u2's rest moved from %v to %v", r2.Until, r.Until) + } + // an appeal went through for u1: it is asked again, and u2 still not + if !s.Unrest("antigravity@u1@example.com") { + t.Fatal("u1's rest not lifted") + } + asked = nil + if code, _ := agyTurn(s); code != 403 || !slices.Equal(asked, []string{"u1@example.com"}) { + t.Fatalf("after the lift: %d, asked %v", code, asked) + } +} + +// The same for any vendor's account restriction (here magpie's words for +// ZCode's risk control, on a ChatGPT account): the lone account resting +// as restricted is not asked by the next requests. +func TestRestrictedLoneAccountNotAskedWhileItRests(t *testing.T) { + codexSignedIn(t) + var tried []string + chatgptRefusing(t, &tried, riskBlocked) + s := New() + for n := 1; n <= 3; n++ { + code, body := postAs(t, s, "", `{"model":"codex/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`) + if code != http.StatusForbidden || !strings.Contains(body, provider.ZCodeRestrictedHint) || strings.Join(tried, ",") != "acct-1" { + t.Fatalf("request %d: %d %s; asked %v", n, code, body, tried) + } + } +} + +// A refusal of the request's content is not the account's: it rests +// nobody, and the next request asks the same account again. +func TestContentRefusalStillAsksTheAccount(t *testing.T) { + n := 0 + s, _ := antigravityGroupFor(t, []string{"u1@example.com"}, "gemini-3.8-flash", func(w http.ResponseWriter, r *http.Request) { + n++ + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + io.WriteString(w, `{"error":{"code":400,"message":"`+highRiskRefusal+`","status":"INVALID_ARGUMENT"}}`) + }) + for want := 1; want <= 3; want++ { + if code, out := agyTurn(s); code < 400 || n != want { + t.Fatalf("turn %d: %d %s, asked %d times", want, code, out, n) + } + } + if r, ok := restOf("antigravity@u1@example.com"); ok { + t.Fatalf("a content refusal rests the account: %+v", r) + } +} + +// A conversation kept strictly on its account (Stays: Strict) whose +// account Google suspended is given the refusal again without asking, and +// the account's rest isn't taken anew by each such turn: it ends when it +// would, rather than never while the conversation goes on. +func TestSuspendedStrictConversationKeepsItsRest(t *testing.T) { + var asked []string + suspended := false + s, _ := antigravityGroupFor(t, []string{"u1@example.com", "u2@example.com"}, "gemini-3.8-flash", func(w http.ResponseWriter, r *http.Request) { + asked = append(asked, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")) + if suspended { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusForbidden) + io.WriteString(w, antigravitySuspended) + return + } + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse(`data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"answered"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":3}}}`)) + }) + p, err := provider.Find("antigravity") + if err != nil { + t.Fatal(err) + } + p.Affinity = provider.AffinityStrict + if err := provider.Save(*p); err != nil { + t.Fatal(err) + } + turn := func() (int, string) { + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"antigravity/gemini-3.8-flash","stream":false,"messages":[{"role":"user","content":"hi"}]}`)) + req.Header.Set("x-session-id", "s1") + s.Handler().ServeHTTP(rec, req) + return rec.Code, rec.Body.String() + } + if code, out := turn(); code != 200 || len(asked) != 1 { + t.Fatalf("first turn: %d %s, asked %v", code, out, asked) + } + kept := asked[0] + suspended, asked = true, nil + if code, out := turn(); code != 403 || !slices.Equal(asked, []string{kept}) { + t.Fatalf("suspended turn: %d %s, asked %v", code, out, asked) + } + first, ok := restOf("antigravity@" + kept) + if !ok || first.Why != failRestricted { + t.Fatalf("%s rests %+v %v", kept, first, ok) + } + for n := 3; n <= 5; n++ { + asked = nil + code, out := turn() + if code != 403 || !strings.Contains(out, "violation of Terms of Service") || len(asked) != 0 { + t.Fatalf("turn %d: %d %s, asked %v", n, code, out, asked) + } + if r, _ := restOf("antigravity@" + kept); !r.Until.Equal(first.Until) { + t.Fatalf("turn %d moved the rest from %v to %v", n, first.Until, r.Until) + } + } +} diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go index b3f033d44..6a432ffbd 100644 --- a/internal/gateway/routing.go +++ b/internal/gateway/routing.go @@ -1041,6 +1041,33 @@ func verifyHeld(c candidate) (string, bool) { return r.said, true } +// restrictedHeld is the refusal to give again, without asking, and its +// status, for a candidate resting as one its vendor restricted +// (failRestricted), while it rests: whole, or its model alone where that +// is how it rests (a Remote magpie's model). Left the last one, it was +// asked by every request all the same, as restLast and probeOne keep one +// resting member for a request to get a real answer, and refused by every +// one (leowang on Discord: two Antigravity accounts Google had suspended, +// 23 and 22 tries, none answered, while the rest was meant to cost one +// refused request each per restrictedRest). The refusal is the answer: +// no wait mends it, and the vendor's risk control counts each request +// against the account. Its rest ending, or lifted on the Routing page, +// it is asked again. +func restrictedHeld(c candidate) (int, string, bool) { + r, ok := restOf(c.restKey()) + if !ok && c.restID() != c.restKey() { + r, ok = restOf(c.restID()) + } + if !ok || r.Why != failRestricted || r.said == "" { + return 0, "", false + } + status := r.Status + if status < 400 { + status = http.StatusForbidden + } + return status, r.said, true +} + // resetsIn is how long until a used-up ChatGPT account is back, as its // refusal says: {"error":{"type":"usage_limit_reached","resets_at":, // "resets_in_seconds":}}. Zero when it doesn't say. @@ -1551,8 +1578,10 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo } } case failRestricted: - // out until the vendor lifts it: an appeal, not a wait (#1849) + // out until the vendor lifts it: an appeal, not a wait (#1849). + // Its words, for restrictedHeld to give again while it rests d, r.By = restrictedRest, "restricted" + r.said = provider.APIError(body, http.StatusText(status)) case failVerify: d, r.By = verifyRest, "verify" // the error as the agent was given it, and the link in it