diff --git a/app/agent_host.py b/app/agent_host.py index 9db531b0f69..2043260e4f1 100644 --- a/app/agent_host.py +++ b/app/agent_host.py @@ -22,11 +22,16 @@ import logging import os +from packaging.version import InvalidVersion, Version + import folder_paths +from comfy.cli_args import DEFAULT_VERSION_STRING from comfy_api import feature_flags ENTRY_POINT_GROUP = "comfyui.agent" PROTOCOL = "v1" +# The first frontend that mounts the agent panel from the `agent` feature flag. +MIN_FRONTEND_VERSION = "1.58.1" _host = None # Set when loading or starting the host failed, so the flag reports why rather @@ -34,6 +39,8 @@ _failure = None # feature_flag() can be read on every connection; log a raising status() once. _status_logged = False +# Set when the served frontend predates the agent panel. The host keeps running. +_frontend_outdated = False def emit_event(event, /, **fields) -> None: @@ -74,6 +81,34 @@ def load() -> bool: return _failure is None +def check_frontend(served: str | None, pinned: str | None, front_end_root: str | None = None) -> None: + """Report a frontend too old to show the agent panel, so a page with no Agent + button isn't the only sign of it. `served` is None when the version is unknown; + `pinned` is the --front-end-version argument and `front_end_root` the + --front-end-root one. A custom root has no version to compare. The host's state + is left alone: it describes the host, and a newer frontend can still use it.""" + global _frontend_outdated + if _failure is not None or _host is None or served is None or front_end_root is not None: + return + if pinned == DEFAULT_VERSION_STRING: + pinned = None + try: + if Version(served) >= Version(MIN_FRONTEND_VERSION): + return + except InvalidVersion: + return + if not pinned: + cause = "update comfyui-frontend-package" + elif pinned.rpartition("@")[2].lstrip("v") == served: + cause = f"pin a newer version than {pinned}" + else: + cause = f"check the log above for why {pinned} did not load" + logging.warning(f"Frontend {served} is too old for the agent panel, which needs {MIN_FRONTEND_VERSION} or newer, so it won't appear: {cause}.") + _frontend_outdated = True + emit_event("frontend_outdated", served=served, pinned=pinned) + _publish_flag() + + def register(app) -> None: """Mount the agent's own API routes onto ComfyUI's aiohttp ``app``. @@ -163,6 +198,8 @@ def feature_flag(): flag = {"state": status["state"]} if status.get("reason"): flag["reason"] = status["reason"] + if _frontend_outdated: + flag["frontend_outdated"] = True return flag except SystemExit: # a wheel that exits is broken, not briefly unhealthy, so disable it logging.exception("The local agent's status could not be read; it is disabled.") diff --git a/app/frontend_management.py b/app/frontend_management.py index 6d27bfa129d..9e3cf0a065f 100644 --- a/app/frontend_management.py +++ b/app/frontend_management.py @@ -241,12 +241,18 @@ def get_comfy_package_versions(cls): return get_comfy_package_versions() @classmethod - def default_frontend_path(cls) -> str: + def _package_frontend_path(cls) -> Optional[str]: + """The frontend bundled in comfyui-frontend-package, or None if the package can't be imported.""" try: import comfyui_frontend_package - - return str(importlib.resources.files(comfyui_frontend_package) / "static") except ImportError: + return None + return str(importlib.resources.files(comfyui_frontend_package) / "static") + + @classmethod + def default_frontend_path(cls) -> str: + path = cls._package_frontend_path() + if path is None: logging.error( f""" ********** ERROR *********** @@ -259,6 +265,20 @@ def default_frontend_path(cls) -> str: """.strip() ) sys.exit(-1) + return path + + @classmethod + def served_version(cls, web_root: str) -> Optional[str]: + """The version of the official frontend at web_root, or None if it isn't one we can identify.""" + path = Path(web_root) + if path.parent.parent == Path(cls.CUSTOM_FRONTENDS_ROOT): + return path.name if path.parent.name.lower() == "comfy-org_comfyui_frontend" else None + if web_root != cls._package_frontend_path(): # not default_frontend_path(), which exits without the package + return None + try: + return version("comfyui-frontend-package") + except Exception: + return None @classmethod def template_asset_map(cls) -> Optional[Dict[str, str]]: diff --git a/server.py b/server.py index 3eb75d925bb..46d86cfeacb 100644 --- a/server.py +++ b/server.py @@ -1326,6 +1326,7 @@ def add_routes(self): # router freezes in start_multi_address(). if args.enable_agent: agent_host.register(self.app) + agent_host.check_frontend(FrontendManager.served_version(self.web_root), args.front_end_version, args.front_end_root) self.user_manager.add_routes(self.routes) self.model_file_manager.add_routes(self.routes) self.custom_node_manager.add_routes(self.routes, self.app, nodes.LOADED_MODULE_DIRS.items()) diff --git a/tests-unit/app_test/agent_host_test.py b/tests-unit/app_test/agent_host_test.py index aa463d8a628..d6974c476fd 100644 --- a/tests-unit/app_test/agent_host_test.py +++ b/tests-unit/app_test/agent_host_test.py @@ -14,6 +14,7 @@ import folder_paths from app import agent_host +from comfy.cli_args import DEFAULT_VERSION_STRING from comfy_api import feature_flags @@ -46,6 +47,7 @@ def _reset(monkeypatch): monkeypatch.setattr(agent_host, "_host", None) monkeypatch.setattr(agent_host, "_failure", None) monkeypatch.setattr(agent_host, "_status_logged", False) + monkeypatch.setattr(agent_host, "_frontend_outdated", False) # The loader publishes the agent flag by mutating this shared dict; keep that # leakage out of other tests. feature_flags.SERVER_FEATURE_FLAGS.pop("agent", None) @@ -297,3 +299,87 @@ def slow_start(**kwargs): loop.run_until_complete(asyncio.wait_for(_until(lambda: agent_host._host.started), 5)) finally: loop.close() + + +def _ready_host(monkeypatch): + host = FakeHost() + host.state["state"] = "ready" + _entry_points(monkeypatch, {"v1": lambda: host}) + assert agent_host.load() is True + return host + + +def test_outdated_frontend_warns_and_is_reported_alongside_the_host_state(monkeypatch, caplog): + host = _ready_host(monkeypatch) + with caplog.at_level(logging.INFO): + agent_host.check_frontend("1.57.6", "Comfy-Org/ComfyUI_frontend@1.59.0") + + assert agent_host.feature_flag() == {"state": "ready", "frontend_outdated": True} + assert feature_flags.SERVER_FEATURE_FLAGS["agent"] == {"state": "ready", "frontend_outdated": True} + warning = next(r.getMessage() for r in caplog.records if r.levelno == logging.WARNING) + assert "1.57.6" in warning and "Comfy-Org/ComfyUI_frontend@1.59.0" in warning and agent_host.MIN_FRONTEND_VERSION in warning + assert "[agent-event] frontend_outdated served=1.57.6 pinned=Comfy-Org/ComfyUI_frontend@1.59.0" in _agent_records(caplog) + + host.state.update(state="failed", reason="crashed") # later host changes keep the note + assert agent_host.feature_flag() == {"state": "failed", "reason": "crashed", "frontend_outdated": True} + + +def test_old_pin_that_loaded_says_to_pin_newer(monkeypatch, caplog): + _ready_host(monkeypatch) + with caplog.at_level(logging.WARNING): + agent_host.check_frontend("1.57.0", "Comfy-Org/ComfyUI_frontend@v1.57.0") + assert "pin a newer version" in caplog.text and "did not load" not in caplog.text + + +def test_outdated_default_frontend_says_to_update_the_package(monkeypatch, caplog): + _ready_host(monkeypatch) + with caplog.at_level(logging.WARNING): + agent_host.check_frontend("1.57.6", None) + assert "update comfyui-frontend-package" in caplog.text + + +@pytest.mark.parametrize("served", ["1.58.1", "1.59.0", "1.59.0.dev2", "2.0.0"]) +def test_supported_frontend_is_not_flagged(monkeypatch, served): + _ready_host(monkeypatch) + agent_host.check_frontend(served, None) + assert agent_host.feature_flag() == {"state": "ready"} + + +@pytest.mark.parametrize("served", ["1.58.0", "1.58.1.dev0", "1.58.1rc1"]) +def test_builds_before_the_minimum_are_flagged(monkeypatch, served): + _ready_host(monkeypatch) + agent_host.check_frontend(served, None) + assert agent_host.feature_flag() == {"state": "ready", "frontend_outdated": True} + + +@pytest.mark.parametrize("served", [None, "not-a-version"]) +def test_unknown_frontend_version_is_not_flagged(monkeypatch, served): + _ready_host(monkeypatch) + agent_host.check_frontend(served, None) + assert agent_host.feature_flag() == {"state": "ready"} + + +def test_no_frontend_warning_when_the_agent_failed_to_load(monkeypatch, caplog): + _entry_points(monkeypatch, {}) + assert agent_host.load() is False + caplog.clear() + with caplog.at_level(logging.INFO): + agent_host.check_frontend("1.57.6", None) + assert agent_host.feature_flag() == {"state": "failed", "reason": "package_missing"} + assert "too old" not in caplog.text and not _agent_records(caplog) + + +def test_default_version_argument_counts_as_no_pin(monkeypatch, caplog): + _ready_host(monkeypatch) + with caplog.at_level(logging.WARNING): + agent_host.check_frontend("1.57.6", DEFAULT_VERSION_STRING, None) + assert "update comfyui-frontend-package" in caplog.text + assert DEFAULT_VERSION_STRING not in caplog.text + + +def test_custom_frontend_root_is_not_checked(monkeypatch, caplog): + _ready_host(monkeypatch) + with caplog.at_level(logging.INFO): + agent_host.check_frontend("1.57.6", "Comfy-Org/ComfyUI_frontend@1.59.0", "/srv/my-frontend") + assert agent_host.feature_flag() == {"state": "ready"} + assert "too old" not in caplog.text and not _agent_records(caplog) diff --git a/tests-unit/app_test/frontend_manager_test.py b/tests-unit/app_test/frontend_manager_test.py index f8d72b62dd9..ae788da6b3b 100644 --- a/tests-unit/app_test/frontend_manager_test.py +++ b/tests-unit/app_test/frontend_manager_test.py @@ -468,3 +468,39 @@ def get_asset_path(template_id, filename): asset_map = FrontendManager.template_asset_map() assert asset_map == {"example.json": "/templates/example.json"} + + +def test_served_version_of_a_downloaded_frontend(): + root = FrontendManager.CUSTOM_FRONTENDS_ROOT + assert FrontendManager.served_version(f"{root}/Comfy-Org_ComfyUI_frontend/1.59.0") == "1.59.0" + assert FrontendManager.served_version(f"{root}/comfy-org_comfyui_frontend/1.57.0") == "1.57.0" + + +def test_served_version_ignores_other_downloaded_frontends(): + root = FrontendManager.CUSTOM_FRONTENDS_ROOT + assert FrontendManager.served_version(f"{root}/someone_ComfyUI_frontend/1.2.3") is None + + +def test_served_version_of_the_default_frontend(): + with patch.object(FrontendManager, "_package_frontend_path", return_value="/site/static"): + with patch("app.frontend_management.version", return_value="1.57.6") as installed: + assert FrontendManager.served_version("/site/static") == "1.57.6" + installed.assert_called_once_with("comfyui-frontend-package") + + +def test_served_version_does_not_exit_without_the_frontend_package(): + with patch.object(FrontendManager, "_package_frontend_path", return_value=None): + with patch.object(FrontendManager, "default_frontend_path", side_effect=SystemExit(-1)): + assert FrontendManager.served_version("/some/other/frontend") is None + + +def test_served_version_does_not_exit_when_the_frontend_package_fails_to_import(): + real_import = __import__ + + def broken_import(name, *args, **kwargs): + if name == "comfyui_frontend_package": + raise ImportError("broken install") + return real_import(name, *args, **kwargs) + + with patch("builtins.__import__", side_effect=broken_import): + assert FrontendManager.served_version("/some/other/frontend") is None