Spaces:
Paused
Paused
fix: avoid false invalid codex otp detection
Browse files189 unmodified lines190191192193194195493 unmodified lines6896906916926936946954 unmodified lines70070170270370470570670715 unmodified lines723724725726727728729730731732733734735736737738739740741742743744745746189 unmodified linesreturn Falsedef login_codex_via_browser(email, password, mail_client=None):"""通过 Playwright 自动完成 Codex OAuth 登录。493 unmodified lines# 处理邮箱验证码页面(可能在 consent 流程中出现)try:otp_input = page.locator('input[name="code"], input[inputmode="numeric"]').firstif otp_input.is_visible(timeout=2000) and mail_client:import re as _re34 unmodified lines)otp = Noneotp_email_id = 0t0 = time.time()while time.time() - t0 < 120:for em in mail_client.search_emails_by_recipient(email, size=5):# 只接受比快照更新的邮件email_id = em.get("emailId", 0)15 unmodified linesbreaktime.sleep(3)if otp:_used_email_ids.add(otp_email_id)otp_input.fill(otp)time.sleep(0.5)page.locator('button[type="submit"], button:has-text("Continue"), button:has-text("继续")').first.click()time.sleep(5)logger.info("[Codex] 已输入验证码: %s", otp)# 检查验证码是否有效——如果页面还在验证码输入,说明无效try:still_code = page.locator('input[name="code"], input[inputmode="numeric"]').firstif still_code.is_visible(timeout=2000):logger.warning("[Codex] 验证码邮件 %s(code=%s)无效,标记并跳过该邮件", otp_email_id, otp)# 不 continue,让循环重新检测当前页面状态except Exception:passcontinueexcept Exception:pass189 unmodified lines190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253493 unmodified lines7477487497507517527534 unmodified lines75875976076176276376476576676776876977015 unmodified lines786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840189 unmodified linesreturn False_OTP_INPUT_SELECTORS = ('input[name="code"], input[inputmode="numeric"], input[autocomplete="one-time-code"], ''input[placeholder*="验证码"], input[placeholder*="code" i]')_OTP_INVALID_HINTS = ("invalid code","incorrect code","wrong code","expired code","check the code and try again","验证码无效","验证码错误","验证码已过期",)def _is_otp_input_visible(page, timeout=500):try:return page.locator(_OTP_INPUT_SELECTORS).first.is_visible(timeout=timeout)except Exception:return Falsedef _detect_otp_error(page):try:body = page.locator("body").inner_text(timeout=1500).lower().replace("\n", " ")except Exception:return Nonefor hint in _OTP_INVALID_HINTS:if hint in body:return hintreturn Nonedef _wait_for_otp_submit_result(page, timeout=12):"""等待验证码提交结果:- accepted: 验证码输入框已消失 / 页面已前进- invalid: 页面明确提示验证码错误- pending: 既没报错也没明显前进(常见于页面较慢或状态未稳定)"""deadline = time.time() + timeoutwhile time.time() < deadline:err = _detect_otp_error(page)if err:return "invalid", errif not _is_otp_input_visible(page, timeout=250):return "accepted", Nonetime.sleep(0.5)err = _detect_otp_error(page)if err:return "invalid", errreturn "pending", Nonedef login_codex_via_browser(email, password, mail_client=None):"""通过 Playwright 自动完成 Codex OAuth 登录。493 unmodified lines# 处理邮箱验证码页面(可能在 consent 流程中出现)try:otp_input = page.locator(_OTP_INPUT_SELECTORS).firstif otp_input.is_visible(timeout=2000) and mail_client:import re as _re34 unmodified lines)otp = Noneotp_email_id = 0page_left_code = Falset0 = time.time()while time.time() - t0 < 120:if not _is_otp_input_visible(page, timeout=300):page_left_code = Truelogger.info("[Codex] 验证码页已退出,继续后续授权流程")breakfor em in mail_client.search_emails_by_recipient(email, size=5):# 只接受比快照更新的邮件email_id = em.get("emailId", 0)15 unmodified linesbreaktime.sleep(3)if otp:submit_ok = Falsefor submit_attempt in range(1, 3):otp_input = page.locator(_OTP_INPUT_SELECTORS).firstif not otp_input.is_visible(timeout=2000):submit_ok = Truebreakotp_input.fill(otp)time.sleep(0.5)page.locator('button[type="submit"], button:has-text("Continue"), button:has-text("继续")').first.click()logger.info("[Codex] 已输入验证码: %s", otp)submit_status, submit_detail = _wait_for_otp_submit_result(page, timeout=12)if submit_status == "accepted":submit_ok = Truebreakif submit_status == "invalid":_used_email_ids.add(otp_email_id)detail_suffix = f",命中提示: {submit_detail}" if submit_detail else ""logger.warning("[Codex] 验证码邮件 %s(code=%s)被页面判定无效%s,标记并跳过该邮件",otp_email_id,otp,detail_suffix,)breakif submit_attempt < 2:logger.warning("[Codex] 验证码邮件 %s(code=%s)提交后未确认成功,准备重试第 %d/2 次",otp_email_id,otp,submit_attempt + 1,)time.sleep(2)else:_used_email_ids.add(otp_email_id)logger.warning("[Codex] 验证码邮件 %s(code=%s)提交后仍未确认成功,标记并跳过该邮件",otp_email_id,otp,)if submit_ok:_used_email_ids.add(otp_email_id)continueif page_left_code:continueexcept Exception:pass
src/autoteam/codex_auth.py+111-17