From 7db35a4ec101a7459216051ab8a92ec563cac7fb Mon Sep 17 00:00:00 2001 From: ENI Date: Sat, 21 Nov 2026 17:34:48 +0200 Subject: [PATCH] shim: persistent 6h cooldown on 402 (cool.json), available counter in healthz; add qw_limits.sql (iishko gates) --- qw_limits.sql | 10 ++++++++++ qw_shim.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- req.json | 1 + stream_out.txt | 10 ++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 qw_limits.sql create mode 100644 req.json create mode 100644 stream_out.txt diff --git a/qw_limits.sql b/qw_limits.sql new file mode 100644 index 0000000..81ce746 --- /dev/null +++ b/qw_limits.sql @@ -0,0 +1,10 @@ +\set ON_ERROR_STOP on +update tokens set model_limits = $$default,qw-flash,qw-standard,qw-pro,qw-advanced,qw-flagship,qw-qwen4-max,qw-qwen3.8-max,qw-qwen3.8-flash,qw-deepseek-v4.1-flash,qw-glm-5.3,qw-glm-5.3-flash,qw-kimi-k3.1-pro,qw-minimax-m3.1-flash$$ + where deleted_at is null and model_limits is null; +update tokens set model_limits = model_limits || $$,qw-flash,qw-standard,qw-pro,qw-advanced,qw-flagship,qw-qwen4-max,qw-qwen3.8-max,qw-qwen3.8-flash,qw-deepseek-v4.1-flash,qw-glm-5.3,qw-glm-5.3-flash,qw-kimi-k3.1-pro,qw-minimax-m3.1-flash$$ + where deleted_at is null and model_limits is not null and position($$qw-flash$$ in model_limits) = 0; +update channels set status = 1 where id = 313854; +update abilities set enabled = true where channel_id = 313854; +select $$ch$$, status from channels where id = 313854; +select $$ab$$, count(*) filter (where enabled), count(*) from abilities where channel_id = 313854; +select $$tk$$, count(*) from tokens where deleted_at is null and position($$qw-flash$$ in model_limits) > 0; \ No newline at end of file diff --git a/qw_shim.py b/qw_shim.py index cf6ee43..f8be912 100644 --- a/qw_shim.py +++ b/qw_shim.py @@ -60,8 +60,30 @@ MODES = { _lock = threading.Lock() _cursor = [0] _accounts = [] +_pool_mtime = 0.0 +COOL_FILE = os.environ.get("QW_COOL_FILE", "/opt/qw-shim/cool.json") _cool = {} # email -> unix ts until which the account is skipped +def load_cool(): + try: + d = json.load(open(COOL_FILE, encoding="utf-8")) + now = time.time() + for k, v in d.items(): + if isinstance(v, (int, float)) and v > now: + _cool[k] = float(v) + except Exception: + pass + +def save_cool(): + try: + now = time.time() + d = {k: v for k, v in _cool.items() if v > now} + tmp = COOL_FILE + ".tmp" + json.dump(d, open(tmp, "w", encoding="utf-8")) + os.replace(tmp, COOL_FILE) + except Exception: + pass + class QWError(Exception): def __init__(self, code, msg): @@ -71,9 +93,13 @@ class QWError(Exception): def load_pool(): - global _accounts + global _accounts, _pool_mtime acc = [] if os.path.exists(POOL_PATH): + try: + _pool_mtime = os.path.getmtime(POOL_PATH) + except OSError: + _pool_mtime = 0.0 for line in open(POOL_PATH, encoding="utf-8"): line = line.strip() if not line: @@ -85,9 +111,20 @@ def load_pool(): if d.get("jwt"): acc.append(d) _accounts = acc + load_cool() return len(acc) +def maybe_reload(): + """Pick up accounts appended by the farm without a service restart.""" + try: + mt = os.path.getmtime(POOL_PATH) + except OSError: + return + if mt != _pool_mtime: + load_pool() + + def pick(skip=None): with _lock: n = len(_accounts) @@ -219,9 +256,11 @@ class H(BaseHTTPRequestHandler): self.wfile.write(b) def do_GET(self): + maybe_reload() if self.path.startswith("/healthz"): return self._json(200, {"ok": True, "accounts": len(_accounts), "models": len(MODES), - "cooling": len([1 for v in _cool.values() if v > time.time()])}) + "cooling": len([1 for v in _cool.values() if v > time.time()]), + "available": sum(1 for a in _accounts if _cool.get(a["email"], 0) <= time.time())}) if self.path.startswith("/v1/models"): return self._json(200, {"object": "list", "data": [ {"id": k, "object": "model", "owned_by": "qwenwork"} for k in MODES]}) @@ -236,6 +275,7 @@ class H(BaseHTTPRequestHandler): except Exception: return self._json(400, {"error": {"message": "bad json"}}) + maybe_reload() model = req.get("model") or "qw-flash" mode = MODES.get(model) if not mode: @@ -262,7 +302,8 @@ class H(BaseHTTPRequestHandler): last_err = e tried.add(acc["email"]) if last_err.code in (401, 402, 403): - _cool[acc["email"]] = time.time() + COOLDOWN + _cool[acc["email"]] = time.time() + (COOLDOWN if last_err.code != 402 else int(os.environ.get("QW_COOL_402", "21600"))) + save_cool() acc = pick(skip=tried) if not acc: break diff --git a/req.json b/req.json new file mode 100644 index 0000000..c9d297e --- /dev/null +++ b/req.json @@ -0,0 +1 @@ +{"stream":true,"max_turns":6,"messages":[{"role":"user","content":"Run shell \"date +%s\" and call task_done with just the number."}]} diff --git a/stream_out.txt b/stream_out.txt new file mode 100644 index 0000000..0b9d3cd --- /dev/null +++ b/stream_out.txt @@ -0,0 +1,10 @@ +data: {"id": "chatcmpl-106a2f1dccc3497b83a37966", "object": "chat.completion.chunk", "created": 1795213382, "model": "deepseek-v4.1-flash-1017", "choices": [{"index": 0, "delta": {"role": "assistant", "content": ""}, "finish_reason": null}]} + +data: {"id": "chatcmpl-106a2f1dccc3497b83a37966", "object": "chat.completion.chunk", "created": 1795213382, "model": "deepseek-v4.1-flash-1017", "choices": [{"index": 0, "delta": {"content": "1795213381"}, "finish_reason": null}]} + +data: {"id": "chatcmpl-106a2f1dccc3497b83a37966", "object": "chat.completion.chunk", "created": 1795213382, "model": "deepseek-v4.1-flash-1017", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]} + +data: {"id": "chatcmpl-106a2f1dccc3497b83a37966", "x_qw_tools": ["shell", "task_done"]} + +data: [DONE] +