Spaces:
Paused
fix(reuse): 三层防止 standby 被误判恢复反复洗同一批耗尽账号
Browse files现场事故:auto-check 把已耗尽的老号反复 kick → reinvite → OAuth 成功 plan=team →
再次 kick,4 个子号配额永远没涨过,最终 Team 从 4 掉到 1。根因三个,全部修复:
1. `_auto_check_loop` 标 exhausted 时只写 quota_exhausted_at,漏 quota_resets_at。
`get_standby_accounts()` 看到 resets_at=None 就默认 `_quota_recovered=True`,
把这批 0% 账号永远标"随时可复用"。补上 `quota_resets_at = now + 5h`。
2. `_replace_single` 选 standby 候选时只信 `_quota_recovered` 标志位,没做实测。
照搬 cmd_rotate 的做法,拿 auth_file 里的 access_token 打一次 check_codex_quota,
必须 status=ok 且剩余 >= threshold 才允许复用;exhausted/auth_error/无 auth_file
一律跳过,宁可创建新号也不把 0% 号塞回 Team。
3. reinvite_account OAuth 成功 plan=team 也不等于"账号真活了"。存在竞态:刚 kick 的
账号在 OpenAI 端处于 soft-removed/缓存未刷新状态,OAuth 仍能拿到 team workspace
token,但配额本身没重置。成功后必须拿新 token 实测一次 wham,达不到 threshold 就
判定"假恢复",立刻 kick 掉 + 写 quota_resets_at=now+5h,让 Team 席位交给新号。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Files changed (2)
2044 unmodified lines20452046204720482049205020512052205320542055205620572044 unmodified linescontinue_playwright_lock.release()# 先标记 exhausted,cmd_check 入口的对账在此之后再看到就会补 kick(双保险)from autoteam.accounts import STATUS_EXHAUSTED, update_accountemails_to_replace = []for email, remaining in low_accounts:logger.info("[巡检] %s 剩余 %d%%,立即替换", email, remaining)update_account(email, status=STATUS_EXHAUSTED, quota_exhausted_at=time.time())emails_to_replace.append(email)# 失效一个立即轮换一个:逐个 kick+补一个,不等凑 min_low 也不走全量 cmd_rotate。2044 unmodified lines204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672044 unmodified linescontinue_playwright_lock.release()# 先标记 exhausted,cmd_check 入口的对账在此之后再看到就会补 kick(双保险)。# 必须同时写 quota_resets_at —— 否则 get_standby_accounts() 看到 None 就默认# _quota_recovered=True,导致后续 rotate/replace 立刻把这个 0% 账号当可复用号# 反复 reinvite 进 Team,席位来回洗同一批耗尽账号永远不换新鲜的。# 阈值默认 5h(18000s),与 check_codex_quota 无返回 resets_at 时的 fallback 一致。from autoteam.accounts import STATUS_EXHAUSTED, update_accountnow_ts = time.time()emails_to_replace = []for email, remaining in low_accounts:logger.info("[巡检] %s 剩余 %d%%,立即替换", email, remaining)update_account(email,status=STATUS_EXHAUSTED,quota_exhausted_at=now_ts,quota_resets_at=now_ts + 18000,)emails_to_replace.append(email)# 失效一个立即轮换一个:逐个 kick+补一个,不等凑 min_low 也不走全量 cmd_rotate。
2084 unmodified lines20852086208720882089209059 unmodified lines2150215121522153215421552084 unmodified linesreturn Falseauth_file = save_auth_file(bundle)update_account(email, status=STATUS_ACTIVE, last_active_at=time.time(), auth_file=auth_file)logger.info("[轮转] 旧账号已恢复: %s", email)return True59 unmodified lineslogger.info("[替换] 跳过 %s(%s)", acc.get("email"), skip_reason)continuecand_email = acc.get("email")logger.info("[替换] 尝试复用 standby: %s", cand_email)if not chatgpt.browser:chatgpt.start()2084 unmodified lines208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214259 unmodified lines2202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552084 unmodified linesreturn Falseauth_file = save_auth_file(bundle)# OAuth 成功 plan=team 不等于"账号真的活了"。存在一类竞态:刚 kick 的账号在 OpenAI# 端处于 soft-removed/缓存未刷新状态,OAuth 仍能短暂拿到 team workspace token,但配额# 本身没被重置(仍是之前耗尽的 5h)。如果不验,就会把 0% 账号塞回 Team,auto-check 下# 一轮立刻再 kick,反复洗同一批耗尽账号。这里用新 token 实测一次 wham,只有确认 ok 且# 剩余 >= threshold 才算真复用成功;否则判定"假恢复",kick 掉让 Team 席位交给新号。access_token = bundle.get("access_token")quota_verified = Falseif access_token:try:try:from autoteam.api import _auto_check_configfrom autoteam.config import AUTO_CHECK_THRESHOLDthreshold = _auto_check_config.get("threshold", AUTO_CHECK_THRESHOLD)except Exception:threshold = 10status_str, info = check_codex_quota(access_token)if status_str == "ok" and isinstance(info, dict):p_remain = 100 - info.get("primary_pct", 0)if p_remain >= threshold:quota_verified = True# 顺手写一份新鲜 last_quota,下次 get_standby_accounts 判断更准update_account(email, last_quota=info)else:logger.warning("[轮转] %s OAuth 成功但实测 5h 剩余 %d%% < %d%%,判定假恢复",email,p_remain,threshold,)elif status_str == "exhausted":logger.warning("[轮转] %s OAuth 成功但实测 exhausted,判定假恢复", email)else:logger.warning("[轮转] %s OAuth 成功但额度验证返回 status=%s,保守判定假恢复", email, status_str)except Exception as exc:logger.warning("[轮转] %s 额度验证抛异常,保守判定假恢复: %s", email, exc)if not quota_verified:# 把这个"假恢复"的账号从 Team 里 kick 掉,避免占席位 + 状态降回 STANDBY 但写一个# 真实的 5h resets_at,这样短期内不会被 get_standby_accounts() 再选中。_cleanup_team_leftover("fake_recovery_quota_not_usable")now_ts = time.time()update_account(email,status=STATUS_STANDBY,auth_file=auth_file,quota_exhausted_at=now_ts,quota_resets_at=now_ts + 18000,)return Falseupdate_account(email, status=STATUS_ACTIVE, last_active_at=time.time(), auth_file=auth_file)logger.info("[轮转] 旧账号已恢复: %s", email)return True59 unmodified lineslogger.info("[替换] 跳过 %s(%s)", acc.get("email"), skip_reason)continuecand_email = acc.get("email")# 额度二次验证:不能只信 get_standby_accounts() 的 _quota_recovered(它只看# quota_resets_at 这种粗估时间)。之前有 bug 就是把还在 exhausted 窗口的# standby 反复 reinvite 进 Team,账号一进来就 0% 立马被 kick,把同一批号# 来回洗,席位始终干空。这里直接拿 auth_file 的 access_token 打一次 wham,# 只有 API 确认 "ok 且剩余 >= threshold" 才允许复用。try:from autoteam.config import AUTO_CHECK_THRESHOLDtry:from autoteam.api import _auto_check_configthreshold = _auto_check_config.get("threshold", AUTO_CHECK_THRESHOLD)except ImportError:threshold = AUTO_CHECK_THRESHOLDexcept Exception:threshold = 10auth_file = acc.get("auth_file")quota_ok = Falseif auth_file and Path(auth_file).exists():try:auth_data = json.loads(read_text(Path(auth_file)))access_token = auth_data.get("access_token")if access_token:status_str, info = check_codex_quota(access_token)if status_str == "ok" and isinstance(info, dict):p_remain = 100 - info.get("primary_pct", 0)if p_remain >= threshold:quota_ok = Trueelse:logger.info("[替换] 跳过 %s(实测 5h 剩余 %d%% < %d%%)", cand_email, p_remain, threshold)continueelif status_str == "exhausted":logger.info("[替换] 跳过 %s(实测 exhausted)", cand_email)continue# auth_error:token 失效,不是"额度真恢复"的证据,跳过elif status_str == "auth_error":logger.info("[替换] 跳过 %s(token auth_error,无法验证额度)", cand_email)continueexcept Exception as exc:logger.info("[替换] %s 额度验证抛异常(跳过): %s", cand_email, exc)continueif not quota_ok:# 没 auth_file 或验证没通过都跳过,宁可去创建新号也别把 0% 账号塞回 Teamlogger.info("[替换] 跳过 %s(无 auth_file 或额度未通过验证)", cand_email)continuelogger.info("[替换] 尝试复用 standby: %s", cand_email)if not chatgpt.browser:chatgpt.start()