mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-10 17:02:32 +00:00
背景:gpt-5.3-codex-spark 使用独立于 codex 全局(5h/7d)的配额窗口(数据源是 /wham/usage 响应体的 codex_bengalfox,而非 codex 全局用的 x-codex-* 响应头),且 只能挂在已完成 OAuth 授权的 OpenAI 账号下复用其登录态,不能作为独立账号单独接入。 为此新增“链接型影子账号”(spark shadow account):影子账号本身不持有任何凭据, 通过 parent_account_id 指向母账号,凭据/token/代理透传自母账号并共享母账号的刷新 周期,仅在配额维度(quota_dimension=spark)和用量窗口上与母账号完全独立调度、互不 连坐。 实现: - 数据模型:migration 154(+154a)给 accounts 表加 parent_account_id / quota_dimension 列 + 4 条约束(维度合法 / parent⟺非 global 维度一致 / 禁自指 / FK)+ 2 个 CONCURRENTLY 索引(母账号索引 + 每母账号至多一个影子的唯一索引)。 - 创建:POST /api/v1/admin/accounts/:id/shadow(CreateShadow)—— 一母一影(唯一 索引兜底并发竞态),继承母账号 proxy/分组/并发/优先级(显式传参可覆盖),默认 model_mapping 恒等映射到 spark(拒绝非 spark 模型),母账号必须是真实的 OpenAI OAuth 账号(非影子)。 - 凭据透传:resolveCredentialAccount 把影子解析回母账号,GetAccessToken / 请求头 / WS 三条路径统一走此函数;影子自身 Credentials 恒为空(仅允许写 model_mapping), 凭据写入的汇聚点 persistAccountCredentials 对影子早返 no-op,防止误写。 - 调度:parentHealthyForShadow 只看母账号是否仍是 OpenAI OAuth + 凭据/传输是否 可用(active、token 未过期、未处于 401/刷新失败/传输故障导致的临时不可调度冷却), 刻意不看母账号的 global 限流窗口——两条 429 道互不连坐。 - 用量:影子的 codex_5h/7d 走 OpenAIQuotaService.QueryUsage(/wham/usage 的 codex_bengalfox),与母账号走的 WSv2 探测(/responses 头)完全独立的数据源、 刷新节流与 staleness 判定。 - 备份:ExportData 显式排除影子账号(影子不持凭据,通用凭据型导入强制 credentials 非空、无法表达父子链接),按 skipped_shadows 计数提示前端。 - 前端:账号操作菜单新增“创建 Spark 影子”入口,影子行展示回填的母账号信息 (邮箱 / plan / 隐私模式 / 订阅到期 / chatgpt_account_id),批量操作自动跳过 影子账号。 说明:migrations 目录用完整文件名(而非纯数字前缀)标识迁移,故本次新增的 154_account_spark_shadow.sql / 154a_..._notx.sql 与已有的 154_add_ops_system_logs_api_key_id.sql 按序号共存,与目录里 145/151 已有的 先例一致。 测试:新增约 20 个测试文件,覆盖 handler(CreateShadow 校验 / 母账号信息回填)、 repository(影子 round-trip / 一母一影唯一索引 / 迁移 schema)、service(凭据 透传三路径 / 调度母健康门 / 用量窗口来源与刷新节流 / CRS 母账号不变量 / 各类 早返与 fail-closed 场景)及前端组件(账号列表 / 操作菜单 / 用量重置)。 验证(镜像 CI;golangci-lint 首次全量分析耗时过长被跳过,其余全部实测): - gofmt -l:干净 - go build ./... / go vet ./...:通过 - go test ./... -count=1:全绿(全部包 ok,含 internal/service、 internal/repository、migrations) - go test -tags integration ./internal/repository/... ./internal/service/... (真实 Postgres,testcontainers):全绿,含迁移幂等性 (TestMigrationsRunner_IsIdempotent_AndSchemaIsUpToDate)与影子相关全部用例 - pnpm lint:check / pnpm typecheck / pnpm build(真实 vite 构建)/ pnpm vitest run:全绿(124 文件 760 用例) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
161 lines
6.5 KiB
Go
161 lines
6.5 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestOpenAI429FastPath_MarksOAuthAccountCoolingDown(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 42, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
|
apiKeyAccount := &Account{ID: 43, Platform: PlatformOpenAI, Type: AccountTypeAPIKey}
|
|
|
|
shouldDisable := svc.handleOpenAIAccountUpstreamError(context.Background(), account, http.StatusTooManyRequests, http.Header{}, nil)
|
|
apiKeyShouldDisable := svc.handleOpenAIAccountUpstreamError(context.Background(), apiKeyAccount, http.StatusTooManyRequests, http.Header{}, nil)
|
|
|
|
require.False(t, shouldDisable)
|
|
require.False(t, apiKeyShouldDisable)
|
|
require.True(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
require.False(t, svc.isOpenAIAccountRuntimeBlocked(apiKeyAccount))
|
|
}
|
|
|
|
// TestOpenAI429FastPath_SkipsSparkShadow 外审第8轮 P1:spark 影子被选中后若 /responses 返回 429,
|
|
// 不得按 global x-codex-* 信号写内存运行时熔断(否则 spark 被冷却到 global reset、单影子场景无可用账号)。
|
|
func TestOpenAI429FastPath_SkipsSparkShadow(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
parentID := int64(800)
|
|
shadow := &Account{
|
|
ID: 801,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
ParentAccountID: &parentID,
|
|
QuotaDimension: QuotaDimensionSpark,
|
|
}
|
|
normal := &Account{ID: 802, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
|
|
|
headers := http.Header{}
|
|
headers.Set("x-codex-primary-used-percent", "100")
|
|
headers.Set("x-codex-primary-reset-after-seconds", "18000")
|
|
headers.Set("x-codex-primary-window-minutes", "300")
|
|
|
|
svc.markOpenAIOAuth429RateLimited(context.Background(), shadow, headers, nil)
|
|
svc.markOpenAIOAuth429RateLimited(context.Background(), normal, headers, nil)
|
|
|
|
require.False(t, svc.isOpenAIAccountRuntimeBlocked(shadow), "spark shadow must not be runtime-blocked by /responses global 429")
|
|
require.True(t, svc.isOpenAIAccountRuntimeBlocked(normal), "normal OpenAI OAuth account should still be runtime-blocked")
|
|
}
|
|
|
|
func TestOpenAIRuntimeBlock_AppliesToOpenAIAPIKeyWhenRateLimitServiceStopsScheduling(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 44, Platform: PlatformOpenAI, Type: AccountTypeAPIKey}
|
|
|
|
svc.BlockAccountScheduling(account, time.Time{}, "custom_error_code")
|
|
|
|
require.True(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
}
|
|
|
|
func TestOpenAIRuntimeBlock_DoesNotApplyToOtherPlatforms(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 45, Platform: PlatformGemini, Type: AccountTypeOAuth}
|
|
|
|
svc.BlockAccountScheduling(account, time.Time{}, "custom_error_code")
|
|
|
|
require.False(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
}
|
|
|
|
func TestOpenAIRuntimeBlocker_IgnoresNonOpenAIFromRateLimitService(t *testing.T) {
|
|
gateway := &OpenAIGatewayService{}
|
|
repo := &rateLimitAccountRepoStub{}
|
|
rateLimitService := NewRateLimitService(repo, nil, &config.Config{}, nil, nil)
|
|
rateLimitService.SetAccountRuntimeBlocker(gateway)
|
|
account := &Account{ID: 45, Platform: PlatformGemini, Type: AccountTypeOAuth}
|
|
|
|
shouldDisable := rateLimitService.HandleUpstreamError(context.Background(), account, http.StatusForbidden, http.Header{}, []byte("forbidden"))
|
|
|
|
require.True(t, shouldDisable)
|
|
require.False(t, gateway.isOpenAIAccountRuntimeBlocked(account))
|
|
}
|
|
|
|
func TestOpenAIModelNotFound_DoesNotRuntimeBlockWholeAccount(t *testing.T) {
|
|
repo := &modelNotFoundAccountRepoStub{}
|
|
svc := &OpenAIGatewayService{
|
|
rateLimitService: &RateLimitService{accountRepo: repo},
|
|
}
|
|
account := openAIModelNotFoundTempAccount()
|
|
|
|
shouldDisable := svc.handleOpenAIAccountUpstreamError(
|
|
context.Background(),
|
|
account,
|
|
http.StatusNotFound,
|
|
http.Header{},
|
|
[]byte(`{"error":{"code":"model_not_found","message":"model not found"}}`),
|
|
"gpt-5.4",
|
|
)
|
|
|
|
require.True(t, shouldDisable)
|
|
require.False(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
require.Zero(t, repo.tempCalls)
|
|
require.Len(t, repo.modelRateLimitCalls, 1)
|
|
}
|
|
|
|
func TestOpenAIRuntimeBlock_DoesNotShortenExistingBlock(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 46, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
|
longUntil := time.Now().Add(10 * time.Minute)
|
|
|
|
svc.BlockAccountScheduling(account, longUntil, "oauth_401")
|
|
svc.BlockAccountScheduling(account, time.Time{}, "upstream_disable")
|
|
|
|
value, ok := svc.openaiAccountRuntimeBlockUntil.Load(account.ID)
|
|
require.True(t, ok)
|
|
actualUntil, ok := value.(time.Time)
|
|
require.True(t, ok)
|
|
require.WithinDuration(t, longUntil, actualUntil, time.Second)
|
|
}
|
|
|
|
func TestOpenAIRuntimeBlock_ClearAccountSchedulingBlock(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 47, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
|
|
|
svc.BlockAccountScheduling(account, time.Now().Add(time.Minute), "429")
|
|
require.True(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
|
|
svc.ClearAccountSchedulingBlock(account.ID)
|
|
require.False(t, svc.isOpenAIAccountRuntimeBlocked(account))
|
|
}
|
|
|
|
func TestShouldStopOpenAIOAuth429Failover_OnlyDuringStorm(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 42, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
|
|
apiKeyAccount := &Account{ID: 43, Platform: PlatformOpenAI, Type: AccountTypeAPIKey}
|
|
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusTooManyRequests, 1))
|
|
|
|
for i := 0; i < openAIOAuth429StormThreshold; i++ {
|
|
svc.recordOpenAIOAuth429()
|
|
}
|
|
|
|
require.True(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusTooManyRequests, 1))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(apiKeyAccount, http.StatusTooManyRequests, 1))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusInternalServerError, 1))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusTooManyRequests, 0))
|
|
}
|
|
|
|
func TestShouldStopOpenAIOAuth429Failover_StopsGrokAfterFirst429Switch(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
account := &Account{ID: 44, Platform: PlatformGrok, Type: AccountTypeOAuth}
|
|
apiKeyAccount := &Account{ID: 45, Platform: PlatformGrok, Type: AccountTypeAPIKey}
|
|
|
|
require.True(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusTooManyRequests, 1))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusTooManyRequests, 0))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(apiKeyAccount, http.StatusTooManyRequests, 1))
|
|
require.False(t, svc.ShouldStopOpenAIOAuth429Failover(account, http.StatusInternalServerError, 1))
|
|
}
|