The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
[Partner Nodes] Renew partner-node sign-in tokens in API nodes (#16943)
Dante8f04fd6ecf4a
diff --git a/comfy_api/feature_flags.py b/comfy_api/feature_flags.pyindex 569bbfaa5..22c99721e 100644--- a/comfy_api/feature_flags.py+++ b/comfy_api/feature_flags.py@@ -35,6 +35,16 @@ CLI_FEATURE_FLAG_REGISTRY: dict[str, FeatureFlagInfo] = {"default": True,"description": "Gate the local Run button behind sign-in when the graph contains partner nodes",},+ "partner_node_token": {+ "type": "bool",+ "default": False,+ "description": "Queue Local API-node prompts with a renewable partner-node token instead of the workspace token",+ },+ "local_web_sso": {+ "type": "bool",+ "default": False,+ "description": "Offer Sign in with browser (Cloud OAuth + PKCE) on Local ComfyUI opened at localhost or 127.0.0.1",+ },}diff --git a/comfy_api_nodes/nodes_sonilo.py b/comfy_api_nodes/nodes_sonilo.pyindex 2ad35531a..7aba8970c 100644--- a/comfy_api_nodes/nodes_sonilo.py+++ b/comfy_api_nodes/nodes_sonilo.py@@ -19,6 +19,7 @@ from comfy_api_nodes.util._helpers import (get_comfy_api_headers,get_node_id,is_processing_interrupted,+ refresh_partner_token,)from comfy_api_nodes.util.common_exceptions import ProcessingInterruptedfrom server import PromptServer@@ -165,6 +166,7 @@ async def _stream_sonilo_music("""POST ``form`` to Sonilo, read the NDJSON stream, and return the first stream's audio bytes."""url = urljoin(default_base_url().rstrip("/") + "/", endpoint.path.lstrip("/"))+ await refresh_partner_token(cls)headers = get_comfy_api_headers(cls)headers.update(endpoint.headers)diff --git a/comfy_api_nodes/util/_helpers.py b/comfy_api_nodes/util/_helpers.pyindex 9dbf25c7c..bb309b0da 100644--- a/comfy_api_nodes/util/_helpers.py+++ b/comfy_api_nodes/util/_helpers.py@@ -1,5 +1,9 @@import asyncio+import base64+import binasciiimport contextlib+import json+import loggingimport osimport reimport time@@ -7,7 +11,7 @@ from collections.abc import Callablefrom datetime import datetime, timezonefrom email.utils import parsedate_to_datetimefrom io import BytesIO-from urllib.parse import urlparse+from urllib.parse import urljoin, urlparseimport aiohttpfrom aiohttp.client_exceptions import ClientError@@ -37,9 +41,164 @@ def get_node_id(node_cls: type[IO.ComfyNode]) -> str:return node_cls.hidden.unique_id+PARTNER_NODE_TOKEN_AUDIENCE = "comfy-partner-node"+PARTNER_NODE_TOKEN_RENEW_PATH = "/auth/partner-node/renew"+_PARTNER_NODE_TOKEN_RENEW_MARGIN = 300.0 # renew a partner-node token this many seconds before it expires+_PARTNER_NODE_TOKEN_RENEW_RETRY_DELAYS = (1.0, 2.0) # pauses between renewal retries++_partner_token_lineages: dict[str, str] = {}+"""Renewed partner-node token -> the token it descends from. Only tokens comfy-api issued on renewal are entered."""+_renewed_partner_tokens: dict[str, str] = {}+"""Newest renewed partner-node token per lineage. In memory only; renewal does not rotate the token."""+_partner_token_renewals: dict[str, asyncio.Task] = {}+_capped_partner_token_lineages: set[str] = set()+"""Lineages whose last renewed token did not extend ``exp`` and is already inside the renew margin (session end reached): no pre-emptive renewal, only on a 401."""+_partner_token_renew_not_before: dict[str, float] = {}+"""Lineage -> wall-clock time before which comfy-api asked (HTTP 429) not to be sent another renewal."""+_rejected_partner_tokens: set[str] = set()+"""Tokens comfy-api answered with a 401: a renewal replaces them even when it expires sooner."""+++def _partner_token_claims(token: str) -> dict | None:+ """Unverified JWT payload of a partner-node token, or None for any other credential.++ The signature is not checked: the claims only decide when to renew, comfy-api still validates the token.+ """+ parts = token.split(".")+ if len(parts) != 3:+ return None+ try:+ claims = json.loads(base64.urlsafe_b64decode(parts[1] + "=" * (-len(parts[1]) % 4)))+ except (ValueError, binascii.Error):+ return None+ if not isinstance(claims, dict) or claims.get("aud") not in (PARTNER_NODE_TOKEN_AUDIENCE, [PARTNER_NODE_TOKEN_AUDIENCE]):+ return None+ if not isinstance(claims.get("sid"), str) or not isinstance(claims.get("exp"), (int, float)):+ return None+ return claims+++def _partner_token_lineage(token: str) -> str:+ """The token a credential was first presented as.++ Unverified claims such as ``sid`` can be forged, so a cached renewal is only shared with the token it was renewed from.+ """+ return _partner_token_lineages.get(token, token)+++def _newest_partner_token(token: str, claims: dict) -> tuple[str, float]:+ renewed = _renewed_partner_tokens.get(_partner_token_lineage(token))+ if renewed is not None:+ renewed_exp = _partner_token_claims(renewed)["exp"]+ if renewed_exp >= claims["exp"] or token in _rejected_partner_tokens:+ return renewed, renewed_exp+ return token, claims["exp"]+++async def _request_partner_token_renewal(lineage: str, sid: str, token: str) -> str | None:+ url = urljoin(default_base_url().rstrip("/") + "/", PARTNER_NODE_TOKEN_RENEW_PATH.lstrip("/"))+ body = error = None+ retry_delays = _PARTNER_NODE_TOKEN_RENEW_RETRY_DELAYS+ if token not in _rejected_partner_tokens and _partner_token_claims(token)["exp"] > time.time():+ retry_delays = ()+ deadline = time.monotonic() + 30.0+ for retry_delay in (*retry_delays, None):+ if is_processing_interrupted():+ return None+ try:+ async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=max(deadline - time.monotonic(), 1.0))) as session:+ async with session.post(url, headers={"Authorization": f"Bearer {token}"}) as resp:+ if 400 <= resp.status < 500:+ logging.warning("Comfy API refused to renew the sign-in token (HTTP %s).", resp.status)+ if resp.status in (401, 403):+ _renewed_partner_tokens.pop(lineage, None)+ if resp.status == 429:+ wait = _retry_after_wait(resp.headers.get("Retry-After"), 60.0, 600.0)+ _partner_token_renew_not_before[lineage] = time.time() + wait+ return None+ if resp.status < 400:+ body = await resp.json(content_type=None)+ break+ error = f"HTTP {resp.status}"+ except (ValueError, asyncio.TimeoutError) as e:+ logging.warning("Could not renew the sign-in token: %s", type(e).__name__)+ return None+ except (ClientError, OSError) as e:+ error = type(e).__name__+ if retry_delay is None or time.monotonic() + 2 * retry_delay > deadline or is_processing_interrupted():+ logging.warning("Could not renew the sign-in token: %s", error)+ return None+ await asyncio.sleep(retry_delay)+ renewed = body.get("token") if isinstance(body, dict) else None+ claims = _partner_token_claims(renewed) if isinstance(renewed, str) else None+ if claims is None or claims["sid"] != sid:+ logging.warning("Comfy API returned an unusable renewed sign-in token.")+ return None+ if claims["exp"] <= _partner_token_claims(token)["exp"] and claims["exp"] - time.time() < _PARTNER_NODE_TOKEN_RENEW_MARGIN:+ _capped_partner_token_lineages.add(lineage)+ else:+ _capped_partner_token_lineages.discard(lineage)+ _partner_token_lineages[renewed] = lineage+ _renewed_partner_tokens[lineage] = renewed+ return renewed+++async def _renew_partner_token(token: str, claims: dict) -> str | None:+ """Renew once per lineage at a time; concurrent callers share the in-flight renewal."""+ lineage = _partner_token_lineage(token)+ if time.time() < _partner_token_renew_not_before.get(lineage, 0.0):+ return None+ task = _partner_token_renewals.get(lineage)+ if task is None or task.done() or task.get_loop() is not asyncio.get_running_loop():+ task = asyncio.create_task(_request_partner_token_renewal(lineage, claims["sid"], token))+ _partner_token_renewals[lineage] = task+ task.add_done_callback(+ lambda t: _partner_token_renewals.pop(lineage, None) if _partner_token_renewals.get(lineage) is t else None+ )+ while True:+ if is_processing_interrupted():+ raise ProcessingInterrupted("Task cancelled")+ if task.done():+ return task.result()+ await asyncio.wait({task}, timeout=1.0)+++async def refresh_partner_token(node_cls: type[IO.ComfyNode]) -> None:+ """Renew the node's partner-node token if it expires soon. Any other credential is left untouched."""+ token = node_cls.hidden.auth_token_comfy_org+ claims = _partner_token_claims(token) if token else None+ if claims is None or _partner_token_lineage(token) in _capped_partner_token_lineages or is_processing_interrupted():+ return+ newest, exp = _newest_partner_token(token, claims)+ if exp - time.time() < _PARTNER_NODE_TOKEN_RENEW_MARGIN:+ await _renew_partner_token(newest, claims)+++async def renew_rejected_partner_token(authorization: str | None) -> bool:+ """After comfy-api rejected ``authorization`` with a 401, make a fresh partner-node token current.++ Returns True when a resend would carry a different, fresh token.+ """+ if not authorization or not authorization.startswith("Bearer "):+ return False+ rejected = authorization[len("Bearer "):]+ claims = _partner_token_claims(rejected)+ if claims is None:+ return False+ _rejected_partner_tokens.add(rejected)+ newest, exp = _newest_partner_token(rejected, claims)+ if newest != rejected and exp - time.time() >= _PARTNER_NODE_TOKEN_RENEW_MARGIN:+ return True+ return await _renew_partner_token(newest, claims) is not None++def get_auth_header(node_cls: type[IO.ComfyNode]) -> dict[str, str]:- if node_cls.hidden.auth_token_comfy_org:- return {"Authorization": f"Bearer {node_cls.hidden.auth_token_comfy_org}"}+ token = node_cls.hidden.auth_token_comfy_org+ if token:+ claims = _partner_token_claims(token)+ if claims is not None:+ token = _newest_partner_token(token, claims)[0]+ return {"Authorization": f"Bearer {token}"}if node_cls.hidden.api_key_comfy_org:return {"X-API-KEY": node_cls.hidden.api_key_comfy_org}return {}diff --git a/comfy_api_nodes/util/client.py b/comfy_api_nodes/util/client.pyindex 0fe3e01be..df02c214c 100644--- a/comfy_api_nodes/util/client.py+++ b/comfy_api_nodes/util/client.py@@ -34,6 +34,8 @@ from ._helpers import (get_comfy_api_headers,get_node_id,is_processing_interrupted,+ refresh_partner_token,+ renew_rejected_partner_token,sleep_with_interrupt,)from .common_exceptions import ApiServerError, LocalNetworkError, ProcessingInterrupted@@ -897,6 +899,7 @@ async def _request_base(cfg: _RequestConfig, expect_binary: bool):rate_limit_attempts = 0rate_limit_delay = cfg.retry_delayin_flight_waits = 0+ auth_renewed = Falseoperation_succeeded: bool = Falsefinal_elapsed_seconds: int | None = Noneextracted_price: float | None = None@@ -912,6 +915,7 @@ async def _request_base(cfg: _RequestConfig, expect_binary: bool):payload_headers = {"Accept": "*/*"} if expect_binary else {"Accept": "application/json"}if is_comfy_api_request:+ await refresh_partner_token(cfg.node_cls)payload_headers.update(get_comfy_api_headers(cfg.node_cls))if cfg.endpoint.headers:payload_headers.update(cfg.endpoint.headers)@@ -993,6 +997,24 @@ async def _request_base(cfg: _RequestConfig, expect_binary: bool):error_message=msg,)raise Exception(msg)+ # A request rejected by comfy-api authentication never reached a provider, so resending it is safe.+ if (+ resp.status == 401+ and is_comfy_api_request+ and not auth_renewed+ and await renew_rejected_partner_token(payload_headers.get("Authorization"))+ ):+ auth_renewed = True+ request_logger.log_request_response(+ operation_id=operation_id,+ request_method=method,+ request_url=url,+ response_status_code=resp.status,+ response_headers=dict(resp.headers),+ response_content=body,+ error_message="HTTP 401 (sign-in token renewed, resending once)",+ )+ continueshould_retry = Falsein_flight = Falsewait_time = 0.0diff --git a/comfy_api_nodes/util/download_helpers.py b/comfy_api_nodes/util/download_helpers.pyindex d6427af58..edd739789 100644--- a/comfy_api_nodes/util/download_helpers.py+++ b/comfy_api_nodes/util/download_helpers.py@@ -20,6 +20,7 @@ from ._helpers import (diagnose_connectivity,get_comfy_api_headers,is_processing_interrupted,+ refresh_partner_token,sleep_with_interrupt,to_aiohttp_url,)@@ -69,6 +70,10 @@ async def download_url_to_bytesio(if cls is None:raise ValueError("For relative 'cloud' paths, the `cls` parameter is required.")url = urljoin(default_base_url().rstrip("/") + "/", url.lstrip("/"))+ try:+ await refresh_partner_token(cls)+ except asyncio.CancelledError:+ raise ProcessingInterrupted("Task cancelled") from Noneheaders = get_comfy_api_headers(cls)while True:diff --git a/tests-unit/comfy_api_nodes_test/partner_node_token_test.py b/tests-unit/comfy_api_nodes_test/partner_node_token_test.pynew file mode 100644index 000000000..82dd6bc48--- /dev/null+++ b/tests-unit/comfy_api_nodes_test/partner_node_token_test.py@@ -0,0 +1,376 @@+import asyncio+import base64+import json+import logging+import time+from io import BytesIO+from types import SimpleNamespace++import pytest+import torch+from aiohttp import web++from comfy.cli_args import args++if not torch.cuda.is_available():+ args.cpu = True++from comfy_api_nodes.util import _helpers+from comfy_api_nodes.util.client import ApiEndpoint, sync_op_raw+from comfy_api_nodes.util.common_exceptions import ProcessingInterrupted+from comfy_api_nodes.util.download_helpers import download_url_to_bytesio+++def _jwt(claims: dict) -> str:+ def enc(part: dict) -> str:+ return base64.urlsafe_b64encode(json.dumps(part).encode()).decode().rstrip("=")+ return f"{enc({'alg': 'ES256', 'typ': 'JWT'})}.{enc(claims)}.signature"+++def _partner_token(expires_in: float, sid: str = "session-1", generation: int = 0, now: float | None = None) -> str:+ exp = (time.time() if now is None else now) + expires_in+ return _jwt({"aud": "comfy-partner-node", "sid": sid, "exp": exp, "gen": generation})+++def _node(auth_token: str | None = None, api_key: str | None = None) -> type:+ hidden = SimpleNamespace(+ auth_token_comfy_org=auth_token, api_key_comfy_org=api_key, comfy_usage_source=None, unique_id="1"+ )+ return type("FakeApiNode", (), {"hidden": hidden})+++class FakeComfyApi:+ """comfy-api stand-in: a partner route that accepts only ``accepted`` tokens, plus the renew route."""++ def __init__(+ self,+ accepted: set[str],+ renewed_token: str | None,+ renew_status: int = 200,+ accept_renewed: bool = True,+ renew_headers: dict[str, str] | None = None,+ ):+ self.accepted = accepted+ self.renewed_token = renewed_token+ self.renew_status = renew_status+ self.renew_headers = renew_headers+ self.accept_renewed = accept_renewed+ self.partner_calls: list[tuple[str | None, dict]] = []+ self.renew_calls: list[str | None] = []++ async def partner(self, request: web.Request) -> web.Response:+ auth = request.headers.get("Authorization")+ self.partner_calls.append((auth, await request.json()))+ if auth is None or auth.removeprefix("Bearer ") not in self.accepted:+ return web.json_response({"message": "token expired"}, status=401)+ return web.json_response({"ok": True})++ async def download(self, request: web.Request) -> web.Response:+ auth = request.headers.get("Authorization")+ self.partner_calls.append((auth, {}))+ if auth is None or auth.removeprefix("Bearer ") not in self.accepted:+ return web.Response(status=401)+ return web.Response(body=b"result-bytes")++ async def renew(self, request: web.Request) -> web.Response:+ self.renew_calls.append(request.headers.get("Authorization"))+ await asyncio.sleep(0.05)+ if self.renew_status != 200:+ return web.json_response({"message": "session revoked"}, status=self.renew_status, headers=self.renew_headers)+ if self.accept_renewed:+ self.accepted.add(self.renewed_token)+ return web.json_response({"token": self.renewed_token})+++async def _with_server(api: FakeComfyApi, monkeypatch, body):+ app = web.Application()+ app.router.add_post("/proxy/partner/generate", api.partner)+ app.router.add_get("/proxy/partner/result", api.download)+ app.router.add_post(_helpers.PARTNER_NODE_TOKEN_RENEW_PATH, api.renew)+ runner = web.AppRunner(app)+ await runner.setup()+ site = web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ port = site._server.sockets[0].getsockname()[1]+ monkeypatch.setattr(args, "comfy_api_base", f"http://127.0.0.1:{port}")+ try:+ return await body()+ finally:+ await runner.cleanup()+++def _generate(node: type):+ return sync_op_raw(+ node,+ ApiEndpoint("/proxy/partner/generate", "POST"),+ data={"prompt": "fennec ears"},+ monitor_progress=False,+ max_retries=0,+ )+++_CACHES = (+ _helpers._partner_token_lineages,+ _helpers._renewed_partner_tokens,+ _helpers._partner_token_renewals,+ _helpers._capped_partner_token_lineages,+ _helpers._partner_token_renew_not_before,+ _helpers._rejected_partner_tokens,+)++[email protected](autouse=True)+def _clear_renewed_tokens():+ for cache in _CACHES:+ cache.clear()+ yield+ for cache in _CACHES:+ cache.clear()+++ ("token", "is_partner"),+ [+ (_jwt({"aud": "comfy-partner-node", "sid": "s", "exp": 1}), True),+ (_jwt({"aud": ["comfy-partner-node"], "sid": "s", "exp": 1}), True),+ (_jwt({"aud": "comfy-api", "sid": "s", "exp": 1}), False),+ (_jwt({"aud": ["comfy-partner-node", "comfy-api"], "sid": "s", "exp": 1}), False),+ (_jwt({"aud": "comfy-partner-node", "exp": 1}), False),+ (_jwt({"aud": "comfy-partner-node", "sid": "s"}), False),+ ("comfyui-api-key-without-dots", False),+ ("a.%%%.c", False),+ ("a." + base64.urlsafe_b64encode(b"[1]").decode() + ".c", False),+ ],+)+def test_only_partner_node_tokens_are_recognized(token, is_partner):+ assert (_helpers._partner_token_claims(token) is not None) is is_partner+++def test_token_near_expiry_is_renewed_before_the_request(monkeypatch):+ old, fresh = _partner_token(60), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted={old}, renewed_token=fresh)++ result = asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(old))))++ assert result == {"ok": True}+ assert api.renew_calls == [f"Bearer {old}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {fresh}"]+++def test_token_far_from_expiry_is_sent_as_is(monkeypatch):+ token = _partner_token(5400)+ api = FakeComfyApi(accepted={token}, renewed_token=None)++ asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(token))))++ assert api.renew_calls == []+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {token}"]+++def test_401_renews_and_resends_the_post_once(monkeypatch):+ now = time.time()+ rejected, fresh = _partner_token(5400, now=now), _partner_token(5400, generation=1, now=now)+ api = FakeComfyApi(accepted=set(), renewed_token=fresh)++ result = asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(rejected))))++ assert result == {"ok": True}+ assert api.renew_calls == [f"Bearer {rejected}"]+ assert api.partner_calls == [+ (f"Bearer {rejected}", {"prompt": "fennec ears"}),+ (f"Bearer {fresh}", {"prompt": "fennec ears"}),+ ]+++def test_401_resends_with_a_renewed_token_that_expires_sooner(monkeypatch):+ now = time.time()+ rejected, shorter = _partner_token(5400, now=now), _partner_token(5000, generation=1, now=now)+ api = FakeComfyApi(accepted=set(), renewed_token=shorter)++ result = asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(rejected))))++ assert result == {"ok": True}+ assert api.renew_calls == [f"Bearer {rejected}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {rejected}", f"Bearer {shorter}"]+++def test_401_after_renewal_is_not_resent_again(monkeypatch):+ rejected, fresh = _partner_token(5400), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted=set(), renewed_token=fresh, accept_renewed=False)++ with pytest.raises(Exception, match="Unauthorized: Please login first"):+ asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(rejected))))++ assert len(api.renew_calls) == 1+ assert len(api.partner_calls) == 2+++def test_renew_refusal_surfaces_the_original_auth_error_without_logging_tokens(monkeypatch, caplog):+ rejected = _partner_token(5400)+ api = FakeComfyApi(accepted=set(), renewed_token=None, renew_status=401)++ with caplog.at_level(logging.DEBUG):+ with pytest.raises(Exception, match="Unauthorized: Please login first"):+ asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(rejected))))++ assert len(api.renew_calls) == 1+ assert len(api.partner_calls) == 1+ assert rejected not in caplog.text+ assert rejected.split(".")[1] not in caplog.text++[email protected](("renewed_exp_delta", "sent"), [(0, "renewed"), (-30, "snapshot")])+def test_renewal_capped_at_session_end_is_not_repeated(monkeypatch, renewed_exp_delta, sent):+ now = time.time()+ snapshot = _partner_token(120, now=now)+ renewed = _partner_token(120 + renewed_exp_delta, generation=1, now=now)+ api = FakeComfyApi(accepted={snapshot}, renewed_token=renewed)++ async def three_prompts():+ for _ in range(3):+ await _generate(_node(snapshot))++ asyncio.run(_with_server(api, monkeypatch, three_prompts))++ expected = renewed if sent == "renewed" else snapshot+ assert api.renew_calls == [f"Bearer {snapshot}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {expected}"] * 3+++def test_rate_limited_renewal_surfaces_the_auth_error_and_waits_for_retry_after(monkeypatch):+ rejected = _partner_token(5400)+ api = FakeComfyApi(accepted=set(), renewed_token=None, renew_status=429, renew_headers={"Retry-After": "120"})++ async def two_prompts():+ for _ in range(2):+ with pytest.raises(Exception, match="Unauthorized: Please login first"):+ await _generate(_node(rejected))++ asyncio.run(_with_server(api, monkeypatch, two_prompts))++ assert api.renew_calls == [f"Bearer {rejected}"]+ assert len(api.partner_calls) == 2+++ ("auth_token", "api_key", "expected_header"),+ [+ (_jwt({"aud": "comfy-api", "sid": "s", "exp": time.time() - 10}), None, "Authorization"),+ (None, "comfyui-api-key", "X-API-KEY"),+ ],+)+def test_other_credentials_are_never_renewed(monkeypatch, auth_token, api_key, expected_header):+ api = FakeComfyApi(accepted=set(), renewed_token=_partner_token(5400))+ sent_headers = []++ async def partner(request: web.Request) -> web.Response:+ sent_headers.append(request.headers.get(expected_header))+ return web.json_response({"message": "Unauthorized"}, status=401)++ api.partner = partner++ with pytest.raises(Exception, match="Unauthorized: Please login first"):+ asyncio.run(_with_server(api, monkeypatch, lambda: _generate(_node(auth_token, api_key))))++ assert api.renew_calls == []+ assert sent_headers == [f"Bearer {auth_token}" if auth_token else api_key]+++def test_concurrent_requests_share_one_renewal(monkeypatch):+ old, fresh = _partner_token(60), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted={old}, renewed_token=fresh)+ node = _node(old)++ async def three_requests():+ return await asyncio.gather(_generate(node), _generate(node), _generate(node))++ results = asyncio.run(_with_server(api, monkeypatch, three_requests))++ assert results == [{"ok": True}] * 3+ assert api.renew_calls == [f"Bearer {old}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {fresh}"] * 3+++def test_prompts_carrying_the_same_snapshot_reuse_the_renewed_token(monkeypatch):+ snapshot, fresh = _partner_token(60), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted={snapshot}, renewed_token=fresh)++ async def two_prompts():+ await _generate(_node(snapshot))+ await _generate(_node(snapshot))++ asyncio.run(_with_server(api, monkeypatch, two_prompts))++ assert api.renew_calls == [f"Bearer {snapshot}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {fresh}"] * 2+++def test_a_token_claiming_another_session_id_never_gets_its_renewed_token(monkeypatch):+ victim, fresh = _partner_token(60), _partner_token(5400, generation=1)+ forged = _partner_token(30, generation=99)+ api = FakeComfyApi(accepted={victim}, renewed_token=fresh)++ async def victim_then_forged():+ await _generate(_node(victim))+ api.renewed_token, api.renew_status = None, 401+ with pytest.raises(Exception, match="Unauthorized: Please login first"):+ await _generate(_node(forged))+ api.renew_status = 200+ await _generate(_node(victim))++ asyncio.run(_with_server(api, monkeypatch, victim_then_forged))++ assert api.renew_calls == [f"Bearer {victim}", f"Bearer {forged}", f"Bearer {forged}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {fresh}", f"Bearer {forged}", f"Bearer {fresh}"]+++def test_renewing_a_renewed_token_keeps_the_snapshot_on_the_newest_token(monkeypatch):+ now = time.time()+ snapshot = _partner_token(60, now=now)+ first, second = _partner_token(200, generation=1, now=now), _partner_token(5400, generation=2, now=now)+ api = FakeComfyApi(accepted={snapshot}, renewed_token=first)++ async def renew_twice():+ await _generate(_node(snapshot))+ api.renewed_token = second+ await _generate(_node(snapshot))+ await _generate(_node(first))++ asyncio.run(_with_server(api, monkeypatch, renew_twice))++ assert api.renew_calls == [f"Bearer {snapshot}", f"Bearer {first}"]+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {first}", f"Bearer {second}", f"Bearer {second}"]+++def test_result_download_uses_a_renewed_token(monkeypatch):+ old, fresh = _partner_token(60), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted=set(), renewed_token=fresh)+ buf = BytesIO()++ asyncio.run(+ _with_server(+ api, monkeypatch, lambda: download_url_to_bytesio("/proxy/partner/result", buf, cls=_node(old), max_retries=0)+ )+ )++ assert buf.getvalue() == b"result-bytes"+ assert [auth for auth, _ in api.partner_calls] == [f"Bearer {fresh}"]+++def test_cancelling_a_download_during_token_renewal_raises_processing_interrupted(monkeypatch):+ old, fresh = _partner_token(60), _partner_token(5400, generation=1)+ api = FakeComfyApi(accepted={fresh}, renewed_token=fresh)++ async def cancel_while_renewing():+ download = asyncio.create_task(+ download_url_to_bytesio("/proxy/partner/result", BytesIO(), cls=_node(old), max_retries=0)+ )+ while not api.renew_calls:+ await asyncio.sleep(0.005)+ download.cancel()+ with pytest.raises(ProcessingInterrupted):+ await download++ asyncio.run(_with_server(api, monkeypatch, cancel_while_renewing))++ assert api.partner_calls == []diff --git a/tests-unit/feature_flags_test.py b/tests-unit/feature_flags_test.pyindex 2ee505870..cb2b58b88 100644--- a/tests-unit/feature_flags_test.py+++ b/tests-unit/feature_flags_test.py@@ -160,6 +160,11 @@ class TestParseCliFeatureFlags:result = _parse_cli_feature_flags()assert result == {"show_signin_button": True}+ @pytest.mark.parametrize("key", ["partner_node_token", "local_web_sso"])+ def test_frontend_strict_bool_flags_parse_to_bool(self, monkeypatch, key):+ monkeypatch.setattr("comfy_api.feature_flags.args", type("Args", (), {"feature_flag": [f"{key}=true"]})())+ assert _parse_cli_feature_flags()[key] is True+def test_missing_equals_defaults_to_true(self, monkeypatch):"""Bare flag without '=' is treated as the string 'true' (and coerced if registered)."""monkeypatch.setattr("comfy_api.feature_flags.args", type("Args", (), {"feature_flag": ["show_signin_button", "valid=1"]})())