Files
sub2api/backend/internal/service/antigravity_compat_committed_no_content.go
T
William Drake 5cbcb1b591 fix(antigravity): release the sticky account after a committed empty compat stream
A compat stream (Responses / Chat Completions over Antigravity) sends a
pre-content keepalive after 15 s, which commits HTTP 200. If the upstream
then ends or times out with no content, the response can no longer fail
over: the client gets an SSE empty_stream / stream_timeout error and
retries. The session stayed bound to the same account, so every retry went
back to it; on 2026-11-15 one Codex session at ~390k tokens hit
empty_stream 22 times in 30 minutes on one account while the others served
the same model.

The stream now returns a typed committed-no-content error for both cases,
and the Responses and Chat Completions handlers remove the session's sticky
binding while it still points at the failed account. Only that session is
affected: the account is not cooled or excluded for anyone else, so a
request-caused empty stream cannot take a shared account offline.
2026-11-14 21:03:17 -07:00

51 lines
2.1 KiB
Go

package service
import (
"context"
"errors"
"log/slog"
)
// antigravityCompatCommittedNoContentError reports a compat stream that
// committed HTTP 200 (pre-content keepalive) and then ended or timed out
// without any content. The response can no longer fail over, so the client
// retries; the handler releases the session's sticky account so that retry is
// scheduled afresh instead of returning to the account that just failed.
type antigravityCompatCommittedNoContentError struct {
msg string
}
func newAntigravityCompatCommittedNoContentError(msg string) error {
return &antigravityCompatCommittedNoContentError{msg: msg}
}
func (e *antigravityCompatCommittedNoContentError) Error() string { return e.msg }
// IsAntigravityCompatCommittedNoContent reports whether err is a compat stream
// that committed its response and then produced no content.
func IsAntigravityCompatCommittedNoContent(err error) bool {
var target *antigravityCompatCommittedNoContentError
return errors.As(err, &target)
}
// ReleaseStickySessionAfterCommittedNoContent removes the session's sticky
// binding when err is a committed no-content compat stream and the session is
// still bound to accountID. Only this session is affected: the account is not
// cooled or excluded for anyone else. Returns true when a binding was removed.
func (s *GatewayService) ReleaseStickySessionAfterCommittedNoContent(ctx context.Context, groupID *int64, sessionHash string, accountID int64, err error) bool {
if sessionHash == "" || accountID <= 0 || s.cache == nil || !IsAntigravityCompatCommittedNoContent(err) {
return false
}
gid := derefGroupID(groupID)
bound, getErr := s.cache.GetSessionAccountID(ctx, gid, sessionHash)
if getErr != nil || bound != accountID {
return false
}
if delErr := s.cache.DeleteSessionAccountID(ctx, gid, sessionHash); delErr != nil {
slog.Warn("antigravity_compat_sticky_release_failed", "group_id", gid, "account_id", accountID, "error", delErr)
return false
}
slog.Info("antigravity_compat_sticky_released", "group_id", gid, "account_id", accountID, "reason", err.Error())
return true
}