account_scheduling_threshold_eval.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package service
  2. import (
  3. "encoding/json"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. // AccountSchedulingThresholdDecision captures the pure pause decision for one account.
  10. type AccountSchedulingThresholdDecision struct {
  11. ShouldPause bool
  12. Platform string
  13. Window string
  14. Scope string
  15. ThresholdPercent int
  16. UsedPercent float64
  17. Until *time.Time
  18. }
  19. type accountSchedulingThresholdCandidate struct {
  20. window string
  21. scope string
  22. usedPercent float64
  23. until *time.Time
  24. }
  25. const accountSchedulingThresholdCredentialKey = "account_scheduling_threshold"
  26. // EvaluateAccountSchedulingThreshold evaluates whether an account should be paused
  27. // based on the current per-platform scheduling threshold snapshot.
  28. func EvaluateAccountSchedulingThreshold(account *Account, thresholds map[string]int, now time.Time) AccountSchedulingThresholdDecision {
  29. decision := AccountSchedulingThresholdDecision{}
  30. if account == nil {
  31. return decision
  32. }
  33. decision.Platform = strings.ToLower(strings.TrimSpace(account.Platform))
  34. if decision.Platform == "" {
  35. return decision
  36. }
  37. if !isAllowedSchedulingThresholdPlatform(decision.Platform) {
  38. return decision
  39. }
  40. threshold, ok := resolveEffectiveAccountSchedulingThreshold(account, thresholds, decision.Platform)
  41. decision.ThresholdPercent = threshold
  42. if !ok || threshold >= 100 {
  43. return decision
  44. }
  45. var winner *accountSchedulingThresholdCandidate
  46. switch decision.Platform {
  47. case PlatformOpenAI:
  48. winner = pickLatestResetSchedulingCandidate(openAIThresholdCandidates(account, now), threshold, now)
  49. case PlatformAnthropic:
  50. winner = pickLatestResetSchedulingCandidate(anthropicThresholdCandidates(account), threshold, now)
  51. case PlatformGrok:
  52. winner = pickLatestResetSchedulingCandidate(grokThresholdCandidates(account), threshold, now)
  53. case PlatformKimi, PlatformZhipu, PlatformMiniMax:
  54. winner = pickLatestResetSchedulingCandidate(cnProviderThresholdCandidates(account, decision.Platform), threshold, now)
  55. default:
  56. return decision
  57. }
  58. if winner == nil {
  59. return decision
  60. }
  61. decision.ShouldPause = true
  62. decision.Window = winner.window
  63. decision.Scope = winner.scope
  64. decision.UsedPercent = winner.usedPercent
  65. decision.Until = winner.until
  66. return decision
  67. }
  68. func evaluateAnthropicFableSchedulingThreshold(account *Account, thresholds map[string]int, now time.Time) AccountSchedulingThresholdDecision {
  69. decision := AccountSchedulingThresholdDecision{}
  70. if account == nil || !strings.EqualFold(strings.TrimSpace(account.Platform), PlatformAnthropic) {
  71. return decision
  72. }
  73. decision.Platform = PlatformAnthropic
  74. threshold, ok := resolveEffectiveAccountSchedulingThreshold(account, thresholds, PlatformAnthropic)
  75. decision.ThresholdPercent = threshold
  76. if !ok || threshold >= 100 {
  77. return decision
  78. }
  79. candidate := anthropicFableThresholdCandidate(account)
  80. if !candidateMatchesThreshold(candidate, threshold, now) {
  81. return decision
  82. }
  83. decision.ShouldPause = true
  84. decision.Window = candidate.window
  85. decision.Scope = candidate.scope
  86. decision.UsedPercent = candidate.usedPercent
  87. decision.Until = candidate.until
  88. return decision
  89. }
  90. func isAllowedSchedulingThresholdPlatform(platform string) bool {
  91. for _, allowed := range AllowedSchedulingThresholdPlatforms {
  92. if platform == allowed {
  93. return true
  94. }
  95. }
  96. return false
  97. }
  98. func resolveEffectiveAccountSchedulingThreshold(account *Account, thresholds map[string]int, platform string) (int, bool) {
  99. if account != nil {
  100. if threshold, ok := accountSchedulingThresholdOverride(account); ok {
  101. return threshold, true
  102. }
  103. }
  104. return lookupAccountSchedulingThreshold(thresholds, platform)
  105. }
  106. func accountSchedulingThresholdOverride(account *Account) (int, bool) {
  107. if account == nil || len(account.Credentials) == 0 {
  108. return 0, false
  109. }
  110. raw, ok := account.Credentials[accountSchedulingThresholdCredentialKey]
  111. if !ok {
  112. return 0, false
  113. }
  114. return parseAccountSchedulingThresholdValue(raw)
  115. }
  116. func parseAccountSchedulingThresholdValue(raw any) (int, bool) {
  117. var value int
  118. switch v := raw.(type) {
  119. case int:
  120. value = v
  121. case int64:
  122. value = int(v)
  123. case float64:
  124. value = int(math.Round(v))
  125. case float32:
  126. value = int(math.Round(float64(v)))
  127. case json.Number:
  128. parsed, err := v.Float64()
  129. if err != nil {
  130. return 0, false
  131. }
  132. value = int(math.Round(parsed))
  133. case string:
  134. raw := strings.TrimSpace(v)
  135. parsed, err := strconv.Atoi(raw)
  136. if err == nil {
  137. value = parsed
  138. break
  139. }
  140. parsedFloat, floatErr := strconv.ParseFloat(raw, 64)
  141. if floatErr != nil {
  142. return 0, false
  143. }
  144. value = int(math.Round(parsedFloat))
  145. default:
  146. return 0, false
  147. }
  148. if value < 1 || value > 100 {
  149. return 0, false
  150. }
  151. return value, true
  152. }
  153. func lookupAccountSchedulingThreshold(thresholds map[string]int, platform string) (int, bool) {
  154. if len(thresholds) == 0 {
  155. return 0, false
  156. }
  157. value, ok := thresholds[platform]
  158. return value, ok
  159. }
  160. func openAIThresholdCandidates(account *Account, now time.Time) []*accountSchedulingThresholdCandidate {
  161. if account == nil {
  162. return nil
  163. }
  164. if !openAICodexSnapshotIdentityTrusted(account) {
  165. return nil
  166. }
  167. return []*accountSchedulingThresholdCandidate{
  168. openAIThresholdCandidate(account.Extra, "5h", now),
  169. openAIThresholdCandidate(account.Extra, "7d", now),
  170. }
  171. }
  172. func openAICodexSnapshotIdentityTrusted(account *Account) bool {
  173. if account == nil || !account.IsOpenAIOAuth() || len(account.Extra) == 0 {
  174. return true
  175. }
  176. if identityValuesConflict(
  177. firstStringValue(account.Credentials, "email"),
  178. firstStringValue(account.Extra, "email", "email_address"),
  179. ) {
  180. return false
  181. }
  182. if identityValuesConflict(
  183. firstStringValue(account.Credentials, "chatgpt_account_id"),
  184. firstStringValue(account.Extra, "chatgpt_account_id", "account_id"),
  185. ) {
  186. return false
  187. }
  188. if identityValuesConflict(
  189. firstStringValue(account.Credentials, "workspace_id", "chatgpt_workspace_id", "organization_id", "org_id"),
  190. firstStringValue(account.Extra, "workspace_id", "chatgpt_workspace_id", "organization_id", "org_id"),
  191. ) {
  192. return false
  193. }
  194. return true
  195. }
  196. func identityValuesConflict(left, right string) bool {
  197. left = strings.TrimSpace(left)
  198. right = strings.TrimSpace(right)
  199. return left != "" && right != "" && !strings.EqualFold(left, right)
  200. }
  201. // firstStringValue returns the first non-empty string among the given map keys.
  202. // Used by OpenAI codex snapshot identity matching for scheduling thresholds.
  203. func firstStringValue(values map[string]any, keys ...string) string {
  204. if len(values) == 0 {
  205. return ""
  206. }
  207. for _, key := range keys {
  208. raw, ok := values[key]
  209. if !ok || raw == nil {
  210. continue
  211. }
  212. switch typed := raw.(type) {
  213. case string:
  214. if v := strings.TrimSpace(typed); v != "" {
  215. return v
  216. }
  217. default:
  218. if v := strings.TrimSpace(stringValue(raw)); v != "" {
  219. return v
  220. }
  221. }
  222. }
  223. return ""
  224. }
  225. func openAIThresholdCandidate(extra map[string]any, window string, now time.Time) *accountSchedulingThresholdCandidate {
  226. if len(extra) == 0 {
  227. return nil
  228. }
  229. var (
  230. usedPercentKey string
  231. resetAtKey string
  232. )
  233. switch window {
  234. case "5h":
  235. usedPercentKey = "codex_5h_used_percent"
  236. resetAtKey = "codex_5h_reset_at"
  237. case "7d":
  238. usedPercentKey = "codex_7d_used_percent"
  239. resetAtKey = "codex_7d_reset_at"
  240. default:
  241. return nil
  242. }
  243. usedPercent, ok := extra[usedPercentKey]
  244. if !ok {
  245. return nil
  246. }
  247. if openAIQuotaWindowReset(extra, window, now) || openAICodexSnapshotStaleForPause(extra, now) {
  248. return nil
  249. }
  250. return &accountSchedulingThresholdCandidate{
  251. window: window,
  252. usedPercent: schedulingPercentValue(usedPercent),
  253. until: parseSchedulingResetAt(extra[resetAtKey]),
  254. }
  255. }
  256. func anthropicThresholdCandidates(account *Account) []*accountSchedulingThresholdCandidate {
  257. if account == nil {
  258. return nil
  259. }
  260. var candidates []*accountSchedulingThresholdCandidate
  261. if usedPercent := utilizationAsPercent(account.Extra["session_window_utilization"]); usedPercent > 0 {
  262. candidates = append(candidates, &accountSchedulingThresholdCandidate{
  263. window: "5h",
  264. usedPercent: usedPercent,
  265. until: cloneTimePtr(account.SessionWindowEnd),
  266. })
  267. }
  268. if usedPercent := utilizationAsPercent(account.Extra["passive_usage_7d_utilization"]); usedPercent > 0 {
  269. candidates = append(candidates, &accountSchedulingThresholdCandidate{
  270. window: "7d",
  271. usedPercent: usedPercent,
  272. until: parseSchedulingResetAt(account.Extra["passive_usage_7d_reset"]),
  273. })
  274. }
  275. return candidates
  276. }
  277. func anthropicFableThresholdCandidate(account *Account) *accountSchedulingThresholdCandidate {
  278. if account == nil {
  279. return nil
  280. }
  281. usedPercent := utilizationAsPercent(account.Extra["passive_usage_7d_oi_utilization"])
  282. if usedPercent <= 0 {
  283. return nil
  284. }
  285. return &accountSchedulingThresholdCandidate{
  286. window: "7d_oi",
  287. scope: anthropicFableRateLimitKey,
  288. usedPercent: usedPercent,
  289. until: parseSchedulingResetAt(account.Extra["passive_usage_7d_oi_reset"]),
  290. }
  291. }
  292. // NOTE: Gemini / Kiro / Antigravity are intentionally NOT threshold-pausing
  293. // platforms (see AllowedSchedulingThresholdPlatforms and the evaluator switch,
  294. // asserted by TestEvaluateAccountSchedulingThreshold_UnsupportedPlatformsDoNotPause).
  295. // Their former per-platform candidate readers were dead code — never reachable
  296. // from EvaluateAccountSchedulingThreshold — and have been removed to avoid the
  297. // false impression that configuring a threshold for them has any effect. The
  298. // kiro_sched_* / antigravity_sched_* extras are still written purely as
  299. // observability snapshots.
  300. // grokThresholdCandidates uses only header-projected
  301. // grok_sched_utilization / grok_sched_reset_at (rolling quota window, reset
  302. // capped at ~25h when written). Official billing 7d/30d windows are not used
  303. // for auto-pause here.
  304. func grokThresholdCandidates(account *Account) []*accountSchedulingThresholdCandidate {
  305. if account == nil {
  306. return nil
  307. }
  308. return []*accountSchedulingThresholdCandidate{
  309. {
  310. window: "quota",
  311. scope: "grok",
  312. usedPercent: schedulingPercentValue(account.Extra["grok_sched_utilization"]),
  313. until: parseSchedulingResetAt(account.Extra["grok_sched_reset_at"]),
  314. },
  315. }
  316. }
  317. // cnProviderThresholdCandidates 读取国产供应商 Coding Plan 账号的 5h / weekly 滚动窗口
  318. // 用量快照(由 CNProviderQuotaService 写入 account.Extra,键形如
  319. // <provider>_5h_used_percent / <provider>_weekly_reset_at)。payg 账号无此快照,
  320. // 候选为空 → 不触发阈值停调(余额型走余额检测)。与 openai 的快照驱动停调一致:
  321. // 仅当用量超阈值且窗口尚未重置时才停调。
  322. func cnProviderThresholdCandidates(account *Account, provider string) []*accountSchedulingThresholdCandidate {
  323. if account == nil || len(account.Extra) == 0 {
  324. return nil
  325. }
  326. return []*accountSchedulingThresholdCandidate{
  327. cnThresholdCandidate(account.Extra, provider, "5h"),
  328. cnThresholdCandidate(account.Extra, provider, "weekly"),
  329. }
  330. }
  331. func cnThresholdCandidate(extra map[string]any, provider, window string) *accountSchedulingThresholdCandidate {
  332. var usedKey, resetKey string
  333. switch window {
  334. case "5h":
  335. usedKey = cnExtraKey(provider, cnExtraSuffix5hUsed)
  336. resetKey = cnExtraKey(provider, cnExtraSuffix5hReset)
  337. case "weekly":
  338. usedKey = cnExtraKey(provider, cnExtraSuffixWeeklyUsed)
  339. resetKey = cnExtraKey(provider, cnExtraSuffixWeeklyReset)
  340. default:
  341. return nil
  342. }
  343. usedPercent, ok := extra[usedKey]
  344. if !ok {
  345. return nil
  346. }
  347. return &accountSchedulingThresholdCandidate{
  348. window: window,
  349. scope: provider,
  350. usedPercent: schedulingPercentValue(usedPercent),
  351. until: parseSchedulingResetAt(extra[resetKey]),
  352. }
  353. }
  354. func pickLatestResetSchedulingCandidate(candidates []*accountSchedulingThresholdCandidate, threshold int, now time.Time) *accountSchedulingThresholdCandidate {
  355. var winner *accountSchedulingThresholdCandidate
  356. for _, candidate := range candidates {
  357. if !candidateMatchesThreshold(candidate, threshold, now) {
  358. continue
  359. }
  360. if winner == nil || candidate.until.After(*winner.until) {
  361. winner = candidate
  362. continue
  363. }
  364. if winner.until.Equal(*candidate.until) && candidate.usedPercent > winner.usedPercent {
  365. winner = candidate
  366. }
  367. }
  368. return winner
  369. }
  370. func candidateMatchesThreshold(candidate *accountSchedulingThresholdCandidate, threshold int, now time.Time) bool {
  371. if candidate == nil || candidate.until == nil || !candidate.until.After(now) {
  372. return false
  373. }
  374. return candidate.usedPercent >= float64(threshold)
  375. }
  376. func utilizationAsPercent(raw any) float64 {
  377. switch v := raw.(type) {
  378. case float64:
  379. if v >= 0 && v <= 1 {
  380. return v * 100
  381. }
  382. return v
  383. case float32:
  384. value := float64(v)
  385. if value >= 0 && value <= 1 {
  386. return value * 100
  387. }
  388. return value
  389. case int:
  390. return float64(v)
  391. case int64:
  392. return float64(v)
  393. case json.Number:
  394. value, err := v.Float64()
  395. if err != nil {
  396. return 0
  397. }
  398. if strings.Contains(v.String(), ".") && value >= 0 && value <= 1 {
  399. return value * 100
  400. }
  401. return value
  402. case string:
  403. trimmed := strings.TrimSpace(v)
  404. value, err := strconv.ParseFloat(trimmed, 64)
  405. if err != nil {
  406. return 0
  407. }
  408. if strings.Contains(trimmed, ".") && value >= 0 && value <= 1 {
  409. return value * 100
  410. }
  411. return value
  412. default:
  413. return 0
  414. }
  415. }
  416. func schedulingPercentValue(raw any) float64 {
  417. switch v := raw.(type) {
  418. case float64:
  419. return v
  420. case float32:
  421. return float64(v)
  422. case int:
  423. return float64(v)
  424. case int64:
  425. return float64(v)
  426. case json.Number:
  427. value, err := v.Float64()
  428. if err != nil {
  429. return 0
  430. }
  431. return value
  432. case string:
  433. value, err := strconv.ParseFloat(strings.TrimSpace(v), 64)
  434. if err != nil {
  435. return 0
  436. }
  437. return value
  438. default:
  439. return 0
  440. }
  441. }
  442. func parseSchedulingResetAt(raw any) *time.Time {
  443. switch v := raw.(type) {
  444. case nil:
  445. return nil
  446. case time.Time:
  447. ts := v
  448. return &ts
  449. case *time.Time:
  450. return cloneTimePtr(v)
  451. case string:
  452. trimmed := strings.TrimSpace(v)
  453. if trimmed == "" {
  454. return nil
  455. }
  456. ts, err := parseSchedulingTime(trimmed)
  457. if err != nil {
  458. return nil
  459. }
  460. return &ts
  461. case json.Number:
  462. if value, err := v.Int64(); err == nil && value > 0 {
  463. ts := time.Unix(value, 0)
  464. return &ts
  465. }
  466. if value, err := v.Float64(); err == nil && value > 0 {
  467. ts := time.Unix(int64(value), 0)
  468. return &ts
  469. }
  470. case float64:
  471. if v > 0 {
  472. ts := time.Unix(int64(v), 0)
  473. return &ts
  474. }
  475. case float32:
  476. if v > 0 {
  477. ts := time.Unix(int64(v), 0)
  478. return &ts
  479. }
  480. case int:
  481. if v > 0 {
  482. ts := time.Unix(int64(v), 0)
  483. return &ts
  484. }
  485. case int64:
  486. if v > 0 {
  487. ts := time.Unix(v, 0)
  488. return &ts
  489. }
  490. }
  491. return nil
  492. }
  493. func parseSchedulingTime(raw string) (time.Time, error) {
  494. formats := []string{
  495. time.RFC3339,
  496. time.RFC3339Nano,
  497. "2006-01-02T15:04:05Z",
  498. "2006-01-02T15:04:05.000Z",
  499. }
  500. for _, format := range formats {
  501. if ts, err := time.Parse(format, raw); err == nil {
  502. return ts, nil
  503. }
  504. }
  505. return time.Time{}, strconv.ErrSyntax
  506. }
  507. func cloneTimePtr(src *time.Time) *time.Time {
  508. if src == nil {
  509. return nil
  510. }
  511. value := *src
  512. return &value
  513. }