Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 64 additions & 16 deletions generator/plan_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ def _proxies():
return {'http': raw, 'https': raw}


# One shared session: pool keep-alive across the init/authorize/callback/poll
# chain, and trust_env=False so a 'direct' connection is actually direct.
# On Windows requests otherwise silently inherits the IE/registry proxy
# (127.0.0.1:vhost-port of whatever VPN client is running), and every
# registration request rides a flaky tunnel that drops TLS at random —
# the exact SSLEOFError/ProxyError trail that killed account registration.
_session = requests.Session()
_session.trust_env = False


def _request(method, url, *, tries=3, backoff=2.0, log=print, **kw):
"""requests call with per-request retries on transient network errors.

The device-flow endpoints sit behind an edge that intermittently answers
502-with-empty-body or resets the connection. Without this, a single
SSLError escapes get_zcode_jwt and the caller burns ALL of its outer
retry attempts at once (the exception jumps straight past the loop).

Dispatch stays patchable: tests monkeypatch ``requests.get``/``post``
at module level, so honour them when they've been replaced.
"""
short = url.split('?')[0].split('/')[-1]
meth = method.upper()
# test shims monkeypatch plan_check.requests.get/post in place; honour
# whichever function was swapped so canned envelopes keep working
shim = {"GET": requests.get, "POST": requests.post}.get(meth)
if shim is not None and shim is not requests.api.get and shim is not requests.api.post:
return shim(url, proxies=_proxies(), **kw)
last = None
for i in range(1, tries + 1):
try:
return _session.request(method, url, proxies=_proxies(), **kw)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Retrying POSTs on any RequestException can resend state-changing requests after the server already handled them. Limit the retry loop to connect-level failures for non-idempotent calls, or pass a retry_post=False flag for claim and event POSTs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At generator/plan_check.py, line 118:

<comment>Retrying POSTs on any RequestException can resend state-changing requests after the server already handled them. Limit the retry loop to connect-level failures for non-idempotent calls, or pass a `retry_post=False` flag for claim and event POSTs.</comment>

<file context>
@@ -84,6 +84,47 @@ def _proxies():
+    last = None
+    for i in range(1, tries + 1):
+        try:
+            return _session.request(method, url, proxies=_proxies(), **kw)
+        except requests.RequestException as e:
+            last = e
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: _request does not retry the documented 502-with-empty-body response because it returns every HTTP response without checking its status. Retry transient 5xx statuses such as 502/503/504 before returning the response.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At generator/plan_check.py, line 118:

<comment>`_request` does not retry the documented `502-with-empty-body` response because it returns every HTTP response without checking its status. Retry transient 5xx statuses such as 502/503/504 before returning the response.</comment>

<file context>
@@ -84,6 +84,47 @@ def _proxies():
+    last = None
+    for i in range(1, tries + 1):
+        try:
+            return _session.request(method, url, proxies=_proxies(), **kw)
+        except requests.RequestException as e:
+            last = e
</file context>
Suggested change
return _session.request(method, url, proxies=_proxies(), **kw)
response = _session.request(method, url, proxies=_proxies(), **kw)
if response.status_code in (502, 503, 504) and i < tries:
log(f" plan: {method.lower()} {short} failed "
f"(HTTP {response.status_code}), retry {i}/{tries - 1}")
time.sleep(min(backoff * i, 6))
continue
return response

except requests.RequestException as e:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: get_plan now swallows balance-request failures and returns None. main.py relies on an escaping exception to retry the plan check direct, so a proxy failure during the balance read now records the account as not starter with no retry. Let the exception propagate, or keep the direct-retry path.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At generator/plan_check.py, line 119:

<comment>`get_plan` now swallows balance-request failures and returns None. `main.py` relies on an escaping exception to retry the plan check direct, so a proxy failure during the balance read now records the account as not starter with no retry. Let the exception propagate, or keep the direct-retry path.</comment>

<file context>
@@ -84,6 +84,47 @@ def _proxies():
+    for i in range(1, tries + 1):
+        try:
+            return _session.request(method, url, proxies=_proxies(), **kw)
+        except requests.RequestException as e:
+            last = e
+            if i < tries:
</file context>

last = e
if i < tries:
log(f" plan: {method.lower()} {short} failed "
f"({type(e).__name__}), retry {i}/{tries - 1}")
time.sleep(min(backoff * i, 6))
raise last


# --- plan ids ---------------------------------------------------------------
# Every Start Plan variant shares the prefix; the id suffix changes with each
# campaign (`-0817`, `-referral`, ...). The 1024 Dev Day giveaway is a
Expand Down Expand Up @@ -169,7 +210,8 @@ def get_zcode_jwt(chat_token, log=print):

# init/poll carry the self-chosen poll token; x-api-key is dropped there.
h_init = build_headers(poll_token, include_api_key=False)
r = requests.post(INIT, headers=h_init, json={"provider": "zai"}, timeout=30, verify=_VERIFY, proxies=_proxies())
r = _request("POST", INIT, headers=h_init, json={"provider": "zai"},
timeout=30, verify=_VERIFY, log=log)
try:
env = r.json()
except Exception:
Expand All @@ -190,7 +232,7 @@ def get_zcode_jwt(chat_token, log=print):
interval = int(d.get("poll_interval_sec") or 2)

# consent with the ACCOUNT's chat.z.ai bearer token
r = requests.post(AUTHORIZE, headers={
r = _request("POST", AUTHORIZE, headers={
"authorization": "Bearer " + chat_token,
"content-type": "application/x-www-form-urlencoded",
"x-region": "overseas",
Expand All @@ -202,7 +244,7 @@ def get_zcode_jwt(chat_token, log=print):
"state": state,
"response_type": "code",
"action": "approve",
}, timeout=30, verify=_VERIFY, proxies=_proxies())
}, timeout=30, verify=_VERIFY, log=log)
code = None
try:
code = re.search(r"code=(code-[0-9a-fA-F]+)", r.json().get("redirect_url", ""))
Expand All @@ -213,11 +255,12 @@ def get_zcode_jwt(chat_token, log=print):
log(f" plan: authorize failed {r.status_code} {r.text[:180]}")
return None

requests.get(CALLBACK + f"?code={code}&state={state}", headers=h_init, timeout=30, verify=_VERIFY, proxies=_proxies())
_request("GET", CALLBACK + f"?code={code}&state={state}", headers=h_init,
timeout=30, verify=_VERIFY, log=log)

for _ in range(15):
r = requests.get(POLL + flow_id, headers=build_headers(poll_token, include_api_key=False),
timeout=30, verify=_VERIFY, proxies=_proxies())
r = _request("GET", POLL + flow_id, headers=build_headers(poll_token, include_api_key=False),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: One burst of three failed polls raises out of get_zcode_jwt and discards the flow. Catch requests.RequestException in the poll loop, log it, and continue so the remaining polls can still succeed.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At generator/plan_check.py, line 262:

<comment>One burst of three failed polls raises out of `get_zcode_jwt` and discards the flow. Catch `requests.RequestException` in the poll loop, log it, and `continue` so the remaining polls can still succeed.</comment>

<file context>
@@ -213,11 +255,12 @@ def get_zcode_jwt(chat_token, log=print):
     for _ in range(15):
-        r = requests.get(POLL + flow_id, headers=build_headers(poll_token, include_api_key=False),
-                         timeout=30, verify=_VERIFY, proxies=_proxies())
+        r = _request("GET", POLL + flow_id, headers=build_headers(poll_token, include_api_key=False),
+                     timeout=30, verify=_VERIFY, log=log)
         try:
</file context>

timeout=30, verify=_VERIFY, log=log)
try:
data = r.json().get("data") or {}
except Exception:
Expand All @@ -237,7 +280,12 @@ def _active_ids(data: dict) -> list:
def get_plan(zcode_jwt, log=print):
"""Read balance + entitlements. <=63M-token plans may appear only in
`plans[].entitlements[]` (with `effective_at`) until they go live."""
r = requests.get(BALANCE, headers=build_headers(zcode_jwt), timeout=30, verify=_VERIFY, proxies=_proxies())
try:
r = _request("GET", BALANCE, headers=build_headers(zcode_jwt), timeout=30,
verify=_VERIFY, log=log)
except requests.RequestException as e:
log(f" plan: balance failed ({type(e).__name__})")
return None
try:
j = r.json()
except Exception:
Expand Down Expand Up @@ -270,8 +318,8 @@ def get_plan(zcode_jwt, log=print):
def get_current(zcode_jwt, log=print):
"""Raw `/billing/current` data (plan list with entitlements/expiry)."""
try:
r = requests.get(CURRENT, headers=build_headers(zcode_jwt), timeout=30,
verify=_VERIFY, proxies=_proxies())
r = _request("GET", CURRENT, headers=build_headers(zcode_jwt), timeout=30,
verify=_VERIFY, log=log)
return (r.json() or {}).get("data") or {}
except Exception as e:
log(f" plan: current failed ({type(e).__name__})")
Expand Down Expand Up @@ -343,8 +391,8 @@ def report_app_activity(zcode_jwt, log=print) -> bool:
ok = False
for body in _plan_events(zcode_jwt):
try:
r = requests.post(EVENT, headers=h, json=body, timeout=30,
verify=_VERIFY, proxies=_proxies())
r = _request("POST", EVENT, headers=h, json=body, timeout=30,
verify=_VERIFY, log=log)
try:
j = r.json()
except Exception:
Expand Down Expand Up @@ -381,8 +429,8 @@ def _walk_plan_ids(node, out):
def preview_plans(zcode_jwt, log=print) -> list:
"""Plans the account is currently eligible to claim (possibly empty)."""
try:
r = requests.get(PREVIEW, headers=build_headers(zcode_jwt), timeout=30,
verify=_VERIFY, proxies=_proxies())
r = _request("GET", PREVIEW, headers=build_headers(zcode_jwt), timeout=30,
verify=_VERIFY, log=log, tries=2)
except Exception as e:
log(f" plan: preview failed ({type(e).__name__})")
return []
Expand Down Expand Up @@ -422,9 +470,9 @@ def claim_plan(zcode_jwt, plan_id, verify_param="", region="", log=print) -> dic
if region:
h["x-aliyun-captcha-verify-region"] = region
try:
r = requests.post(CLAIM, headers=h,
json={"plan_id": plan_id}, timeout=30,
verify=_VERIFY, proxies=_proxies())
r = _request("POST", CLAIM, headers=h,
json={"plan_id": plan_id}, timeout=30,
verify=_VERIFY, log=log)
except requests.RequestException as e:
return {"code": -1, "msg": f"request failed: {type(e).__name__}"}
try:
Expand Down
19 changes: 17 additions & 2 deletions zcode_gateway/api_dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,16 @@ def register_account(info: dict):
import time as _time
zjwt = None
for attempt in range(1, 4):
zjwt = get_zcode_jwt(chat_token, log=lambda *a: None)
try:
zjwt = get_zcode_jwt(chat_token, log=lambda *a: None)
except Exception as exc:
# transient network failure: cost ONE attempt, not all
# three (an escaping exception used to jump straight
# to the outer handler and drop the account).
log.warning("generator: zcodeJWT mint error for %s "
"(attempt %d/3): %s", email, attempt,
type(exc).__name__)
Comment on lines +882 to +884

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The new catch-all handler logs only the exception type, so non-network bugs inside get_zcode_jwt lose the message and traceback the outer log.exception used to record. Include the message here, and do the same in the plan read handler below. The attempt also logs a second 'no zcodeJWT' warning right after this one.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At zcode_gateway/api_dash.py, line 882:

<comment>The new catch-all handler logs only the exception type, so non-network bugs inside `get_zcode_jwt` lose the message and traceback the outer `log.exception` used to record. Include the message here, and do the same in the plan read handler below. The attempt also logs a second 'no zcodeJWT' warning right after this one.</comment>

<file context>
@@ -873,7 +873,16 @@ def register_account(info: dict):
+                        # transient network failure: cost ONE attempt, not all
+                        # three (an escaping exception used to jump straight
+                        # to the outer handler and drop the account).
+                        log.warning("generator: zcodeJWT mint error for %s "
+                                    "(attempt %d/3): %s", email, attempt,
+                                    type(exc).__name__)
</file context>
Suggested change
log.warning("generator: zcodeJWT mint error for %s "
"(attempt %d/3): %s", email, attempt,
type(exc).__name__)
log.warning("generator: zcodeJWT mint error for %s "
"(attempt %d/3): %s: %s", email, attempt,
type(exc).__name__, exc)

zjwt = None
if zjwt:
break
log.warning("generator: no zcodeJWT for %s (attempt %d/3)",
Expand All @@ -886,7 +895,13 @@ def register_account(info: dict):
return
plans = []
for attempt in range(1, 4):
plan_info = get_plan(zjwt) or {}
try:
plan_info = get_plan(zjwt) or {}
except Exception as exc:
log.warning("generator: plan read error for %s "
"(attempt %d/3): %s", email, attempt,
type(exc).__name__)
plan_info = {}
plans = plan_info.get("plans") or []
if plans:
break
Expand Down