Merge pull request #9073 from mengkzhaoyun/fix/grok-scheduler-tier-projection

fix(scheduler): preserve resolved Grok subscription_tier in metadata projection
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-12-02 12:04:43 +08:00
commit 8ff16c5cc0
2 files changed
+71 -1

No files matched your search

+17 -1
View File
@@ -16,6 +16,7 @@ import (
"sync/atomic"
"time"
"github.com/Wei-Shaw/sub2api/internal/pkg/xai"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/redis/go-redis/v9"
"github.com/tidwall/gjson"
@@ -1393,7 +1394,7 @@ func (c *schedulerCache) mgetChunked(ctx context.Context, keys []string) ([]any,
}
func buildSchedulerMetadataAccount(account service.Account) service.Account {
return service.Account{
metadata := service.Account{
ID: account.ID,
Name: account.Name,
Platform: account.Platform,
@@ -1422,6 +1423,21 @@ func buildSchedulerMetadataAccount(account service.Account) service.Account {
Credentials: filterSchedulerCredentials(account.Credentials),
Extra: filterSchedulerExtra(account.Extra),
}
// Media admission runs on this projection before full-account hydration.
// Preserve the live JWT tier precedence without copying tokens to metadata.
if account.IsGrokOAuth() {
tier := xai.SubscriptionTierFromJWT(account.GetCredential("access_token"))
if tier == "" {
tier = account.GetCredential("subscription_tier")
}
if tier != "" {
if metadata.Credentials == nil {
metadata.Credentials = make(map[string]any)
}
metadata.Credentials["subscription_tier"] = tier
}
}
return metadata
}
func filterSchedulerAccountGroups(accountGroups []service.AccountGroup) []service.AccountGroup {
@@ -4,6 +4,7 @@ package repository
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
@@ -372,6 +373,59 @@ func TestBuildSchedulerMetadataAccount_KeepsGrokMediaEligibility(t *testing.T) {
})
}
func TestBuildSchedulerMetadataAccount_ProjectsGrokSubscriptionTier(t *testing.T) {
jwt := func(tier int) string {
return "e30." + base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf(`{"tier":%d}`, tier))) + ".signature"
}
cases := []struct {
name string
platform string
token string
saved string
wantTier string
wantEligible bool
}{
{"paid_live_jwt", service.PlatformGrok, jwt(7), "free", "supergrok_plus", true},
{"paid_saved_tier", service.PlatformGrok, "", "supergrok_plus", "supergrok_plus", true},
{"live_free_downgrade", service.PlatformGrok, jwt(0), "supergrok_plus", "free", false},
{"live_basic_downgrade", service.PlatformGrok, jwt(2), "supergrok_plus", "x_basic", false},
{"unclassified_billing", service.PlatformGrok, "", "", "", false},
{"malformed_token_fallback", service.PlatformGrok, "invalid", "supergrok_plus", "supergrok_plus", true},
{"other_platform_unchanged", service.PlatformOpenAI, jwt(7), "supergrok_plus", "", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
account := service.Account{
Platform: tc.platform,
Type: service.AccountTypeOAuth,
Credentials: map[string]any{
"access_token": tc.token,
"refresh_token": "never-copy",
"subscription_tier": tc.saved,
},
Extra: map[string]any{
"grok_billing_snapshot": map[string]any{
"status_code": 200,
"monthly_limit_cents": 0,
},
},
}
meta := buildSchedulerMetadataAccount(account)
require.Equal(t, tc.wantTier, meta.GetCredential("subscription_tier"))
require.NotContains(t, meta.Credentials, "access_token")
require.NotContains(t, meta.Credentials, "refresh_token")
eligible, _ := meta.GrokMediaGenerationEligibility()
require.Equal(t, tc.wantEligible, eligible)
require.Equal(t, tc.saved, account.GetCredential("subscription_tier"), "projection must not mutate original credentials")
})
}
}
func TestBuildSchedulerMetadataAccount_KeepsSlimGroupMembership(t *testing.T) {
account := service.Account{
ID: 42,