From 8aa5c2a4ba3627072f4b866d4dcabfa2d1588f2e Mon Sep 17 00:00:00 2001 From: shaw Date: Wed, 2 Dec 2026 11:42:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20omit=20Retry-After=20hint=20when=20any?= =?UTF-8?q?=20supporting=20account=20is=20not=20in=20persisted=20cooldown?= =?UTF-8?q?=EF=BC=88audit=20follow-up=20for=20#9197=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/gateway_model_availability.go | 9 +++ .../gateway_model_availability_test.go | 65 +++++++++++++++++++ .../openai_gateway_model_availability.go | 9 +++ 3 files changed, 83 insertions(+) diff --git a/backend/internal/service/gateway_model_availability.go b/backend/internal/service/gateway_model_availability.go index fbe727c886..71c36d6dc4 100644 --- a/backend/internal/service/gateway_model_availability.go +++ b/backend/internal/service/gateway_model_availability.go @@ -109,6 +109,10 @@ func (s *GatewayService) DiagnoseModelAvailabilityForPlatform( diag := ModelAvailabilityDiagnosis{} var earliestRecovery time.Duration + // A supporting account with no persisted cooldown is waiting on a runtime + // filter (quota, RPM, window cost, ...) whose recovery time is unknown, so + // the pool-level hint must be omitted rather than inflated by a cooling peer. + hasUncooledCandidate := false for i := range accounts { if useMixed && accounts[i].Platform == PlatformAntigravity && !accounts[i].IsMixedSchedulingEnabled() { continue @@ -120,9 +124,14 @@ func (s *GatewayService) DiagnoseModelAvailabilityForPlatform( if earliestRecovery == 0 || remaining < earliestRecovery { earliestRecovery = remaining } + } else { + hasUncooledCandidate = true } } } + if hasUncooledCandidate { + earliestRecovery = 0 + } diag.RetryAfterSeconds = ceilRetryAfterSeconds(earliestRecovery, DownstreamRetryAfterMaxSeconds(s.cfg)) return diag } diff --git a/backend/internal/service/gateway_model_availability_test.go b/backend/internal/service/gateway_model_availability_test.go index ed32f620e8..bf5ba12fde 100644 --- a/backend/internal/service/gateway_model_availability_test.go +++ b/backend/internal/service/gateway_model_availability_test.go @@ -249,3 +249,68 @@ func TestDiagnoseModelAvailabilityForPlatform_WrongPlatformFiltersOut(t *testing require.False(t, diag.HasAccountsInPool, "OpenAI route must not see Anthropic accounts in pool") require.False(t, diag.HasModelSupport) } + +func retryAfterTestAccount(id int64, platform string, groupID int64, cooldown time.Duration) Account { + acc := Account{ + ID: id, + Platform: platform, + Status: StatusActive, + Schedulable: true, + AccountGroups: []AccountGroup{{GroupID: groupID}}, + Credentials: map[string]any{ + "model_mapping": map[string]any{"claude-opus-4-8": "claude-opus-4-8"}, + }, + } + if cooldown > 0 { + until := time.Now().Add(cooldown) + acc.RateLimitResetAt = &until + } + return acc +} + +func TestDiagnoseModelAvailability_RetryAfterHint(t *testing.T) { + groupID := int64(44) + tests := []struct { + name string + cooldowns []time.Duration + want func(t *testing.T, seconds int) + }{ + { + name: "mixed pool with runtime-filtered account omits hint", + cooldowns: []time.Duration{0, 2 * time.Hour}, + want: func(t *testing.T, s int) { require.Equal(t, 0, s) }, + }, + { + name: "all cooling picks earliest", + cooldowns: []time.Duration{90 * time.Second, 2 * time.Hour}, + want: func(t *testing.T, s int) { require.InDelta(t, 90, s, 2) }, + }, + { + name: "all cooling is capped at the default maximum", + cooldowns: []time.Duration{2 * time.Hour, 3 * time.Hour}, + want: func(t *testing.T, s int) { require.Equal(t, DownstreamRetryAfterMaxSeconds(nil), s) }, + }, + } + for _, tt := range tests { + t.Run("anthropic/"+tt.name, func(t *testing.T) { + repo := &mockAccountRepoForPlatform{accountsByID: map[int64]*Account{}} + for i, c := range tt.cooldowns { + repo.accounts = append(repo.accounts, retryAfterTestAccount(int64(i+1), PlatformAnthropic, groupID, c)) + } + svc := &GatewayService{accountRepo: repo, cfg: testConfig(), schedulerSnapshot: &SchedulerSnapshotService{}} + diag := svc.DiagnoseModelAvailabilityForPlatform(context.Background(), &groupID, "claude-opus-4-8", PlatformAnthropic) + require.True(t, diag.HasModelSupport) + tt.want(t, diag.RetryAfterSeconds) + }) + t.Run("openai/"+tt.name, func(t *testing.T) { + repo := &mockAccountRepoForPlatform{accountsByID: map[int64]*Account{}} + for i, c := range tt.cooldowns { + repo.accounts = append(repo.accounts, retryAfterTestAccount(int64(i+1), PlatformOpenAI, groupID, c)) + } + svc := &OpenAIGatewayService{accountRepo: repo, cfg: testConfig(), schedulerSnapshot: &SchedulerSnapshotService{}} + diag := svc.DiagnoseModelAvailabilityForPlatform(context.Background(), &groupID, "claude-opus-4-8", PlatformOpenAI) + require.True(t, diag.HasModelSupport) + tt.want(t, diag.RetryAfterSeconds) + }) + } +} diff --git a/backend/internal/service/openai_gateway_model_availability.go b/backend/internal/service/openai_gateway_model_availability.go index b253ef1764..eec311d4af 100644 --- a/backend/internal/service/openai_gateway_model_availability.go +++ b/backend/internal/service/openai_gateway_model_availability.go @@ -56,6 +56,10 @@ func (s *OpenAIGatewayService) DiagnoseModelAvailabilityForPlatform( diag := ModelAvailabilityDiagnosis{} var earliestRecovery time.Duration + // A supporting account with no persisted cooldown is waiting on a runtime + // filter (quota, RPM, window cost, ...) whose recovery time is unknown, so + // the pool-level hint must be omitted rather than inflated by a cooling peer. + hasUncooledCandidate := false for i := range accounts { diag.HasAccountsInPool = true if isRetiredOpenAISubscriptionModel(&accounts[i], resolveOpenAIAccountUpstreamModelForRequest(&accounts[i], requestedModel, false)) { @@ -71,9 +75,14 @@ func (s *OpenAIGatewayService) DiagnoseModelAvailabilityForPlatform( if earliestRecovery == 0 || remaining < earliestRecovery { earliestRecovery = remaining } + } else { + hasUncooledCandidate = true } } } + if hasUncooledCandidate { + earliestRecovery = 0 + } diag.RetryAfterSeconds = ceilRetryAfterSeconds(earliestRecovery, DownstreamRetryAfterMaxSeconds(s.cfg)) return diag }