From 2ea3d9f2fd2ffcffbc459e9dbdc32a1e616e5991 Mon Sep 17 00:00:00 2001 From: Jensen Zhang Date: Thu, 12 Mar 2026 08:24:54 +0000 Subject: [PATCH] fix(token): reset consumed when token enters cooling state - Reset consumed to 0 when quota reaches 0 in default mode (consume()) - Reset consumed to 0 when 429 rate limit is triggered (mark_rate_limited()) - Remove consumed reset on API sync, only restore active status - This ensures consumed represents 'usage count per cooling cycle' --- app/services/token/manager.py | 11 +++++------ app/services/token/models.py | 3 ++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/token/manager.py b/app/services/token/manager.py index 3fa9bbe9..0bd2cc45 100644 --- a/app/services/token/manager.py +++ b/app/services/token/manager.py @@ -673,6 +673,7 @@ class TokenManager: old_quota = token.quota token.quota = 0 token.status = TokenStatus.COOLING + token.consumed = 0 # 进入冷却时重置本轮消耗 logger.warning( f"Token {raw_token[:10]}...: marked as rate limited " f"(quota {old_quota} -> 0, status -> cooling)" @@ -959,15 +960,13 @@ class TokenManager: if consumed_mode: # Consumed 模式:使用新逻辑 token_info.update_quota_with_consumed(new_quota) - # 刷新成功后重置消耗记录 - if new_quota > 0: - token_info.consumed = 0 else: # 默认模式:使用旧逻辑 token_info.update_quota(new_quota) - # 刷新成功后如果 quota > 0,清除冷却状态 - if new_quota > 0: - token_info.status = TokenStatus.ACTIVE + + # 刷新成功后如果 quota > 0,恢复活跃状态 + if new_quota > 0: + token_info.status = TokenStatus.ACTIVE token_info.mark_synced() window_size = self._extract_window_size_seconds(result) diff --git a/app/services/token/models.py b/app/services/token/models.py index f256586b..775958f5 100644 --- a/app/services/token/models.py +++ b/app/services/token/models.py @@ -136,9 +136,10 @@ class TokenInfo(BaseModel): self.use_count += actual_cost self.quota = max(0, self.quota - actual_cost) - # 默认行为:quota 耗尽时标记冷却 + # 默认行为:quota 耗尽时标记冷却,并重置消耗记录 if self.quota == 0: self.status = TokenStatus.COOLING + self.consumed = 0 # 进入冷却时重置本轮消耗 elif self.status == TokenStatus.COOLING: # 只从 COOLING 恢复,不从 EXPIRED 恢复 self.status = TokenStatus.ACTIVE