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 lines109110111112113114115116117118119120121122123124114125126127128108 unmodified lines return load_admin_state().get("session_token", "")
def _is_valid_uuid(value: str) -> bool: """检查是否为有效的 UUID 格式""" import re
return 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_id return state.get("account_id", "") or os.environ.get("CHATGPT_ACCOUNT_ID", "") return os.environ.get("CHATGPT_ACCOUNT_ID", "")
def get_admin_password():1171 unmodified lines117211731174117511761177117811791180118111751176118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120611801207120812091210252 unmodified lines14631464146514661467146814691470147114721473147414751171 unmodified lines if 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_active else: # API 有缓存延迟,移出后可能返回旧数据,手动修正 current_count = max(0, api_count - removed_count) if api_count >= 0 else 0 # API 有缓存延迟,移出后可能返回旧数据,手动修正 current_count = max(0, api_count - removed_count) vacancies = TARGET - current_count
if vacancies <= 0: excess = current_count - TARGET if 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 = 0 for acc in local_active: if removed >= excess: break email = acc["email"] if remove_from_team(chatgpt, email): update_account(email, status=STATUS_STANDBY) logger.info("[4/5] 超员清理: %s → standby", email) removed += 1 if removed: logger.info("[4/5] 已清理 %d 个多余成员", removed) else: logger.info("[4/5] Team 已满 (%d/%d)", current_count, TARGET) logger.info("[4/5] Team 已满 (%d/%d)", current_count, TARGET) return
logger.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 -1 path = 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 -1 data = 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