gateway: an empty reply on two accounts of a model ends the request there (vhjjkkhg on X)

Alma on Responses to Antigravity's Gemini 3.8 Flash got "an empty reply"
now and then in multi-turn tool turns, and each account's in-place
re-asks plus the next account took 8 to 9 tries over 10+ minutes. A reply
that said nothing on a second account of the same provider's model is
about the request, not the account: the model's remaining accounts are
not asked (emptyAccounts = 2, `emptied` in the candidate loop). Other
providers and a group's other members still are, and nobody rests (the
#1221 branch is unchanged).

Cause of the empty reply itself: NOT found. Chat and Responses build
byte-identical Code Assist contents for a tool conversation (compared),
and about 50 real-account runs in a sandbox HOME (Antigravity login only)
across efforts none to xhigh, multi-user-turn tool loops over both
protocols, gave no empty reply. It looks like upstream's occasional
behaviour (#667 / #1221), so this commit bounds the cost and does not
claim a translation fix.

Run (mote-run): TestEmptyReplyOnTwoAccountsEndsTheRequest (Responses and
Chat; 5 accounts), TestGeminiEmpty*, TestLateRests*, full
./internal/gateway, gofmt, go vet darwin/linux/windows (nogui).
Broken by hand: with gateway.go's change reverted the new test is red
(12 asks, 5 accounts tried; 8 asks and 2 accounts with it).
Not tried: a real Alma turn on an account that returns the empty reply.
This commit is contained in:
yetone
2026-11-11 03:32:27 +08:00
parent 72f2deebee
commit 6d78627693
4 changed files with 88 additions and 6 deletions
File diff suppressed because one or more lines are too long
@@ -0,0 +1,58 @@
package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync/atomic"
"testing"
)
// A reply that said nothing on two accounts running is about the request,
// not the account: the third, fourth and fifth aren't asked, as each ask
// (an in-place retry or two, then the next account) took minutes on a long
// conversation (vhjjkkhg on X: 8 to 9 tries, 10+ minutes, from Alma on
// Responses). No account rests for it.
func TestEmptyReplyOnTwoAccountsEndsTheRequest(t *testing.T) {
users := []string{"u1@example.com", "u2@example.com", "u3@example.com", "u4@example.com", "u5@example.com"}
for _, path := range []string{"/v1/responses", "/v1/chat/completions"} {
t.Run(path, func(t *testing.T) {
var asked atomic.Int32
s, _ := antigravityGroupFor(t, users, "gemini-3.8-flash", func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"thinking...","thought":true}]}}]}}`,
`data: {"response":{"candidates":[{"content":{"role":"model","parts":[]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"thoughtsTokenCount":5}}}`))
})
body := `{"model":"antigravity/gemini-3.8-flash","stream":true,"input":"hi","messages":[{"role":"user","content":"hi"}]}`
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
if !strings.Contains(rec.Body.String(), "an empty reply") {
t.Fatalf("not told the empty reply: %d %s", rec.Code, rec.Body.String())
}
// two accounts, each asked again in place a few times (the old
// code asked all five: 12 asks)
if n := asked.Load(); n > 8 {
t.Errorf("an empty reply went round %d asks, over two accounts' worth", n)
}
r := lastRoute(s)
accounts := map[string]bool{}
var ids []string
for _, x := range r.Tries {
accounts[x.ID] = true
ids = append(ids, x.ID+"/"+strconv.Itoa(x.Status))
}
if len(accounts) > 3 { // the group's own entry, and two accounts
t.Errorf("%d tries: %v", len(r.Tries), ids)
}
for _, u := range users {
if _, ok := restOf("antigravity@" + u); ok {
t.Errorf("antigravity@%s rested for an empty reply", u)
}
}
})
}
}
+7
View File
@@ -56,6 +56,13 @@ func emptyRetries(w http.ResponseWriter, ctx context.Context) int {
return streamRetries
}
// emptyAccounts is how many accounts of one model may answer a request
// with a reply that said nothing before the rest of its accounts aren't
// asked: the same conversation ends empty on whichever account it is sent
// to, and each ask took minutes on a long one (vhjjkkhg on X: 8 to 9 tries
// over 10+ minutes). The others of its group, and other providers, are.
const emptyAccounts = 2
// loopReasks is how many times a reply whose reasoning was stuck in a
// loop (loopGuard) before it said anything is asked again in place, rather
// than ended with the loop's error.
+22 -6
View File
@@ -3327,12 +3327,13 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
roomFor, roomCap := "", 0
// the key or subscription account the last try went to (#557)
providerKeyID, providerKeyName, providerAccount := "", "", ""
var other *Try // the first failure that wasn't an allowance run out
var forbade []forbidden // the plain 403s and 5xx rested so far (forbiddenAlike)
sameRefusal := false // the last try's 5xx was a mate's word for word: the request's
autoReset := resetFirst != nil // a Codex or Claude reset looked at, once a request
creditsAsked := map[string]bool{} // Codex accounts asked again onto their credits, once each (creditsAgain)
regionAsked := map[string]int{} // a provider's model refused for where the request came from, times this request (#5182)
var other *Try // the first failure that wasn't an allowance run out
var forbade []forbidden // the plain 403s and 5xx rested so far (forbiddenAlike)
sameRefusal := false // the last try's 5xx was a mate's word for word: the request's
autoReset := resetFirst != nil // a Codex or Claude reset looked at, once a request
creditsAsked := map[string]bool{} // Codex accounts asked again onto their credits, once each (creditsAgain)
emptied := map[string]map[string]bool{} // a provider's model's accounts whose reply said nothing, this request (emptyAccounts)
regionAsked := map[string]int{} // a provider's model refused for where the request came from, times this request (#5182)
// the subscription/model pairs an account refused as a model it doesn't
// serve, for the accounts whose own list lacks it to be asked next
// (unlistedNext); none for a request pinned to an account, a
@@ -4829,6 +4830,21 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
skipped = append(skipped, c.label()+": "+call.Error)
continue
}
if !last && hw.failed() && strings.Contains(call.Error, emptyReply) {
// said by a second account of the model, it is about the
// request and no account: the rest of that model's accounts
// are not asked (emptyAccounts); other providers and models
// still are
ek := c.p.ID + "\x00" + c.model
if emptied[ek] == nil {
emptied[ek] = map[string]bool{}
}
emptied[ek][c.who()] = true
if len(emptied[ek]) >= emptyAccounts {
cands = append(cands[:i+1:i+1], notMates(cands[i+1:], c)...)
last = i == len(cands)-1
}
}
if !last && hw.failed() && strings.Contains(call.Error, emptyReply) {
// a reply that said nothing is a failure of the model or
// request, not the account (#1221): the account doesn't rest,