Spaces:
Paused
Paused
fix: get_team_member_count 失败兜底 + account_id UUID 校验 + rotate 超员自动清理
Browse files- get_team_member_count 返回 -1 时用本地 active 数兜底,加详细错误日志
- get_chatgpt_account_id 校验 UUID 格式,state.json 存了 user ID 时 fallback 到 .env
- rotate 发现超员时自动清理多余的本地管理账号,优先移除额度最低的
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Files changed (2)
108 unmodified lines109110111112113114115116117108 unmodified linesreturn load_admin_state().get("session_token", "")def get_chatgpt_account_id():state = load_admin_state()return state.get("account_id", "") or os.environ.get("CHATGPT_ACCOUNT_ID", "")def get_admin_password():108 unmodified lines109110111112113114115116117118119120121122123124125126127128108 unmodified linesreturn load_admin_state().get("session_token", "")def _is_valid_uuid(value: str) -> bool:"""检查是否为有效的 UUID 格式"""import rereturn bool(re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", value, re.I))def get_chatgpt_account_id():state = load_admin_state()state_id = state.get("account_id", "")# state.json 里的值必须是 UUID 格式才有效(user-xxx 是 user ID 不是 account ID)if state_id and _is_valid_uuid(state_id):return state_idreturn os.environ.get("CHATGPT_ACCOUNT_ID", "")def get_admin_password():
1171 unmodified lines117211731174117511761177117811791180118111821183252 unmodified lines1436143714381439144014411442144314441171 unmodified linesif not chatgpt or not chatgpt.browser:ensure_chatgpt()api_count = get_team_member_count(chatgpt)# API 有缓存延迟,移出后可能返回旧数据,手动修正current_count = max(0, api_count - removed_count) if api_count >= 0 else 0vacancies = TARGET - current_countif vacancies <= 0:logger.info("[4/5] Team 已满 (%d/%d)", current_count, TARGET)returnlogger.info("[4/5] 填补 %d 个空缺 (当前 %d/%d)...", vacancies, current_count, TARGET)252 unmodified linesdef get_team_member_count(chatgpt_api):"""获取当前 Team 成员数"""account_id = get_chatgpt_account_id()path = f"/backend-api/accounts/{account_id}/users"result = chatgpt_api._api_fetch("GET", path)if result["status"] != 200:return -1data = json.loads(result["body"])members = data.get("items", data.get("users", data.get("members", [])))1171 unmodified lines117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210252 unmodified lines14631464146514661467146814691470147114721473147414751171 unmodified linesif not chatgpt or not chatgpt.browser:ensure_chatgpt()api_count = get_team_member_count(chatgpt)logger.info("[4/5] API 返回成员数: %d(本轮移出: %d)", api_count, removed_count)if api_count <= 0:# API 返回异常,用本地 active 账号数兜底local_active = sum(1 for a in load_accounts() if a["status"] == STATUS_ACTIVE)logger.warning("[4/5] API 成员数异常 (%d),使用本地 active 数: %d", api_count, local_active)current_count = local_activeelse:# API 有缓存延迟,移出后可能返回旧数据,手动修正current_count = max(0, api_count - removed_count)vacancies = TARGET - current_countif vacancies <= 0:excess = current_count - TARGETif excess > 0:logger.info("[4/5] Team 超员 (%d/%d),清理 %d 个多余成员...", current_count, TARGET, excess)# 只移除本地管理的账号,优先移除额度最低的all_accs = load_accounts()local_active = [a for a in all_accs if a["status"] == STATUS_ACTIVE]# 按额度排序,额度低的优先移除local_active.sort(key=lambda a: 100 - (a.get("last_quota") or {}).get("primary_pct", 0))removed = 0for acc in local_active:if removed >= excess:breakemail = acc["email"]if remove_from_team(chatgpt, email):update_account(email, status=STATUS_STANDBY)logger.info("[4/5] 超员清理: %s → standby", email)removed += 1if removed:logger.info("[4/5] 已清理 %d 个多余成员", removed)else:logger.info("[4/5] Team 已满 (%d/%d)", current_count, TARGET)returnlogger.info("[4/5] 填补 %d 个空缺 (当前 %d/%d)...", vacancies, current_count, TARGET)252 unmodified linesdef get_team_member_count(chatgpt_api):"""获取当前 Team 成员数"""account_id = get_chatgpt_account_id()if not account_id:logger.error("[Team] account_id 为空,无法查询成员数")return -1path = f"/backend-api/accounts/{account_id}/users"result = chatgpt_api._api_fetch("GET", path)if result["status"] != 200:logger.error("[Team] 获取成员列表失败: %d %s", result["status"], result["body"][:200])return -1data = json.loads(result["body"])members = data.get("items", data.get("users", data.get("members", [])))
src/autoteam/admin_state.py+12-1
src/autoteam/manager.py+34-3