Keep the gateway serving: concurrency, account identity, and the tray
One PR for the three things that kept the gateway from serving reliably: requests lost to ZCode's per-account concurrency limit, accounts suspended for looking like one machine, and closing the window killing the process.
Problem
Concurrency. ZCode answers a second concurrent request on one account with 3008/3009/3010 (user concurrency limit exceeded). The gateway admitted up to four requests per account, treated the refusal like a generic error, and retried the same account immediately. A burst of real sessions therefore lost requests even when the pool had free accounts. A 405/3012 suspension parked only the requested model, so the banned account stayed in rotation and returned 405 on every request, and the dashboard hid all of it behind an N err tooltip.
Account identity. Every account imported or logged in on one machine shared the same x-device-mid and the same timezone, so upstream saw one device cycling through many credentials — the pattern its risk control suspends. Hard state (DEAD / EXHAUSTED / cooling) lived only in memory, so a restart resurrected suspended accounts as READY and kept retrying them.
No way to stay running. Closing the window quit the gateway.
Changes
Concurrency refusals
- Treat
3008/3009/3010as retryable capacity errors and normalize them to HTTP 429. - Admit one in-flight request per account and queue the rest (
acquire_wait_seconds, default 120s) instead of over-admitting. - After a refusal, hold the account with exponential backoff (10s doubling to 60s, cleared by the next success). State stays
READY: no cooldown, model park, or error history. - Refusal strikes decay after a quiet gap instead of stacking to the maximum hold forever.
- A released account waits
concurrency_settle_seconds(default 5) before re-admission, because upstream still counts the finished request; this removes most of the remaining 3008 churn. - Detect the account-suspension message and mark the account
DEADwithdeath_reason=suspended, log the upstream text, and abort the request instead of rotating the ban onto the next account.
Account identity and ban safety
- Each account gets its own stable device id:
uuid4stored with the credentials for new logins,uuid5of the account id for imports and backfills. The generator (plan_check.py,invite_claim.py) derives the same id, so a plan check and a message report the same device instead of a hardcoded shared one. - Each account keeps its own timezone instead of the host's.
- DEAD, EXHAUSTED and active COOLING states are written back to the live account source, so a restart no longer re-admits a suspended account.
scripts/accounts/backfill_device_mids.pymigrates existing snapshots and is a dry run unless--applyis passed.
Tray
- Closing the window hides it to a notification-area icon and the gateway keeps serving. The menu has the dashboard, the address, the log, the data folder and Quit.
- The icon is drawn with
ctypes(Shell_NotifyIconon a hidden top-level window) rather than pystray/Pillow, which would add roughly 15 MB to a onefile exe that is already 60 MB. The hidden window is top-level soTaskbarCreatedre-registers the icon after an Explorer restart. --no-trayrestores close-to-quit,--traystarts hidden in the tray,--headlessis unchanged.- Tray failure is non-fatal:
start()returns False and the app keeps its old behaviour. If the tray thread dies, closing the window quits instead of stranding a process with no window and no icon.
Accounts page
- Add an Issues metric and a Needs attention panel naming each blocked account, its reason, and an Enable action for dead accounts.
- Show the last error inline with its age, and show concurrency holds / cooldowns under the state badge.
Testing
python -m pytest tests -q→ 1424 passed, 147 skipped.ruff check --select E9,F63,F7,F82,F811,F841→ clean.- New regressions cover queueing, hold escalation and clearing, the settle window, affinity skipping a held account, burst routing, suspension removal, per-account device ids, hard-state write-back, and the tray registration flags.
- Built with
scripts/build/windows.ps1and run as the deployed exe: 12/12 concurrent requests returned 200 with 9/9 accounts ready, a suspended account left rotation on the first upstream 405, closing the window hid it while both processes and/healthstayed up, the icon was present in the notification area (Shell_NotifyIconGetRectreturnsS_OKwith a real rect), left-click reopened the dashboard, the menu opened, Quit exited cleanly, and a restart came back healthy.
Notes
- The new pool settings have defaults, so an existing
config.yamlkeeps working without edits. backfill_device_mids.pyis the only migration step; without it, existing snapshots keep their shared mid until they are next rewritten.- Supersedes #3, which carried the first four commits on their own.