From d4e775ba0166a98e26d3d1fc8ab832a9343c3a5b Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 18:43:34 -0500 Subject: [PATCH 1/6] fix(discovery): report Desktop installs and their runtime paths `comfy which` and `comfy env` now find Comfy Desktop installs from both the modern and legacy registries and report Desktop's split code, data, Python and model roots. With running-server launch metadata, discovery picks that exact instance and resolves its effective paths; ambiguous or relative identities stay unresolved. A selected workspace that does not exist no longer passes `comfy which`, and unreadable Desktop settings are reported as warnings. Install and launch selection is unchanged. Records the decision in ADR-DESKTOP-DISCOVERY-0006. Carries over #1043 by @wealthwiselabs, rebased on main. --- CHANGELOG.md | 3 + comfy_cli/cmdline.py | 27 +- comfy_cli/desktop_install.py | 581 +++++++++++++++++ comfy_cli/error_codes.py | 10 + comfy_cli/output/panels.py | 13 +- comfy_cli/schemas/env.json | 38 ++ comfy_cli/schemas/which.json | 38 ++ comfy_cli/workspace_manager.py | 52 +- ...ERY-0006-preserve-runtime-path-identity.md | 100 +++ tests/comfy_cli/command/test_manager_gui.py | 15 +- tests/comfy_cli/conftest.py | 80 +++ .../comfy_cli/output/test_envelope_schemas.py | 10 +- tests/comfy_cli/test_desktop_install.py | 615 ++++++++++++++++++ tests/comfy_cli/test_which_desktop.py | 304 +++++++++ tests/comfy_cli/test_workspace_manager.py | 8 +- 15 files changed, 1878 insertions(+), 16 deletions(-) create mode 100644 comfy_cli/desktop_install.py create mode 100644 docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md create mode 100644 tests/comfy_cli/test_desktop_install.py create mode 100644 tests/comfy_cli/test_which_desktop.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ade79741..d4170b701 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,9 @@ history. ### Fixed +- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. +- `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. A nonexistent selected workspace no longer succeeds in `which`; discovery reports malformed Desktop configuration and marks inferred data roots as unverified. + - UI→API conversion expands a `COMFY_DYNAMICCOMBO_V3` selector's sub-inputs in the order the workflow was SAVED with, when the saved graph records one, instead of the option's current order. An option that has since gained a diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 7f71001ec..caadde0f6 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -2775,7 +2775,8 @@ def set_default( @tracking.track_command() def which(): renderer = get_renderer() - comfy_path = workspace_manager.workspace_path + workspace = workspace_manager.discovery_data() + comfy_path = workspace["path"] if comfy_path is None: renderer.error( code="not_in_workspace", @@ -2784,15 +2785,25 @@ def which(): ) raise typer.Exit(code=1) - workspace_type_value = ( - workspace_manager.workspace_type.value if workspace_manager.workspace_type is not None else None - ) + workspace_type_value = workspace["type"] + if not workspace["exists"]: + details = {"path": comfy_path, "workspace_type": workspace_type_value} + if "desktop" in workspace: + details["desktop"] = workspace["desktop"] + renderer.error( + code="workspace_not_found", + message=f"Workspace directory does not exist: {comfy_path}", + hint="run: comfy install (or pass --workspace /path/to/ComfyUI)", + details=details, + ) + raise typer.Exit(code=1) if renderer.is_pretty(): import sys as _sys from comfy_cli.env_checker import _display_url from comfy_cli.local_address import resolve_local_host_port from comfy_cli.output.panels import which_panel + from comfy_cli.output.sanitize import sanitize_value host, port = resolve_local_host_port(None, None, background=ConfigManager().background) try: @@ -2808,12 +2819,17 @@ def which(): server_running=server_running, server_url=_display_url(host, port), version=ConfigManager().get_cli_version(), + desktop=workspace.get("desktop"), ) ) + for warning in workspace.get("warnings", []): + renderer.console().print(sanitize_value(warning["message"]), markup=False) renderer.emit( { "workspace_path": str(comfy_path), "workspace_type": workspace_type_value, + "exists": workspace["exists"], + **{key: workspace[key] for key in ("desktop", "warnings") if key in workspace}, }, command="which", ) @@ -2893,6 +2909,9 @@ def env(): # JSON path: collect structured data without printing the table. data = EnvChecker().fill_data() data["workspace"] = workspace_manager.fill_data() + warnings = data["workspace"].pop("warnings", None) + if warnings: + data["warnings"] = warnings renderer.emit(data, command="env") diff --git a/comfy_cli/desktop_install.py b/comfy_cli/desktop_install.py new file mode 100644 index 000000000..789af470c --- /dev/null +++ b/comfy_cli/desktop_install.py @@ -0,0 +1,581 @@ +"""Find a Comfy Desktop 2 install from the records Desktop keeps on disk. + +Desktop 2 splits one install across up to three places, and none of them is +where comfy-cli's own default workspace lives: + +* the **code dir** — a ComfyUI checkout at ``/ComfyUI``; +* the **data dir** — ``models/``, ``custom_nodes/``, ``user/`` and the venv. + For an install adopted from Desktop 1 that is ``adoptedBaseDir`` (Desktop + launches it with ``--base-directory``); otherwise it is the code dir; +* **model roots** — the data dir's ``models/`` plus the shared library in + ``settings.json`` → ``modelsDirs`` (unless the install opted out with + ``useSharedModels: false``) plus the install's own ``modelDirs``. + +Everything is read from files Desktop maintains in its app-data dir +(``installations.json``, ``settings.json``). Nothing here starts Desktop, +talks to a server, or writes a file. +""" + +from __future__ import annotations + +import json +import os +import re +from dataclasses import dataclass, field, replace +from datetime import datetime, timezone +from pathlib import Path +from typing import Any + +from comfy_cli.constants import OS +from comfy_cli.utils import get_os + +#: Desktop 2's Electron ``userData`` folder name. +APP_DIR_NAME = "Comfy Desktop" +MODERN_APP_DIR_NAME = "comfyui-desktop-2" +#: Desktop 1's ``userData`` folder, which holds ``config.json`` → ``basePath``. +LEGACY_APP_DIR_NAME = "ComfyUI" + +_BARE_RELEASE_RE = re.compile(r"^\d+(?:\.\d+)+$") +_VERSION_MARKER_RE = re.compile(r"""^__version__\s*=\s*['"]([^'"\n]+)['"]""", re.MULTILINE) + + +def desktop_app_data_dir() -> Path | None: + """Desktop 2's app-data dir on this OS (where ``installations.json`` lives).""" + current = get_os() + if current == OS.MACOS: + return Path.home() / "Library" / "Application Support" / APP_DIR_NAME + if current == OS.WINDOWS: + appdata = os.environ.get("APPDATA") + return Path(appdata) / APP_DIR_NAME if appdata else None + base = os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share") + return Path(base) / APP_DIR_NAME + + +def legacy_config_dir() -> Path | None: + """Desktop 1's app-data dir (``config.json`` → ``basePath``); none on Linux.""" + current = get_os() + if current == OS.MACOS: + return Path.home() / "Library" / "Application Support" / LEGACY_APP_DIR_NAME + if current == OS.WINDOWS: + appdata = os.environ.get("APPDATA") + return Path(appdata) / LEGACY_APP_DIR_NAME if appdata else None + return None + + +@dataclass(frozen=True) +class ModelRoot: + path: str + #: ``data`` (the install's own ``models/``), ``shared`` (``settings.json`` + #: ``modelsDirs``) or ``install`` (the install record's own ``modelDirs``). + source: str + exists: bool + + def to_dict(self) -> dict[str, Any]: + return {"path": self.path, "source": self.source, "exists": self.exists} + + +@dataclass(frozen=True) +class DesktopInstall: + install_id: str | None + name: str | None + code_dir: str + data_dir: str + python: str | None + model_roots: tuple[ModelRoot, ...] + comfyui_version: str | None + comfyui_commit: str | None + adopted: bool + status: str | None + last_launched_ms: int | None + #: The code dir holds a ComfyUI checkout (``main.py``) — only such an + #: install can be selected. + exists: bool + #: Internal registry ownership for generated config identity; never serialized. + registry_dir: str | None = field(default=None, repr=False) + + @property + def last_launched_at(self) -> str | None: + if not self.last_launched_ms: + return None + try: + stamp = datetime.fromtimestamp(self.last_launched_ms / 1000, tz=timezone.utc) + except (OSError, OverflowError, ValueError): + return None + return stamp.isoformat(timespec="seconds").replace("+00:00", "Z") + + def summary(self, *, selected: bool) -> dict[str, Any]: + """One row of ``desktop.installs``: enough to tell the installs apart.""" + return { + "install_id": self.install_id, + "name": self.name, + "code_dir": self.code_dir, + "data_dir": self.data_dir, + "comfyui_version": self.comfyui_version, + "last_launched_at": self.last_launched_at, + "status": self.status, + "exists": self.exists, + "selected": selected, + } + + +@dataclass(frozen=True) +class DesktopDetection: + app_data_dir: str + installs: tuple[DesktopInstall, ...] = () + selected: DesktopInstall | None = None + selected_reason: str | None = None + #: Records that exist but could not be read, as ``": "``. + problems: tuple[str, ...] = field(default_factory=tuple) + input_dir: str | None = None + output_dir: str | None = None + output_dir_source: str | None = None + + def to_dict(self) -> dict[str, Any]: + """The ``desktop`` block of ``comfy which`` / ``comfy env``. + + The selected install's fields sit at the top level so an agent reads + ``desktop.data_dir`` directly; ``installs`` lists every local install. + """ + chosen = self.selected + block: dict[str, Any] = { + "install_id": chosen.install_id if chosen else None, + "name": chosen.name if chosen else None, + "code_dir": chosen.code_dir if chosen else None, + "data_dir": chosen.data_dir if chosen else None, + "custom_nodes_dir": os.path.join(chosen.data_dir, "custom_nodes") if chosen else None, + "python": chosen.python if chosen else None, + "model_roots": [root.to_dict() for root in chosen.model_roots] if chosen else [], + "comfyui_version": chosen.comfyui_version if chosen else None, + "comfyui_commit": chosen.comfyui_commit if chosen else None, + "selected_reason": self.selected_reason, + "installs": [install.summary(selected=install is chosen) for install in self.installs], + "app_data_dir": self.app_data_dir, + } + if self.input_dir is not None: + block["input_dir"] = self.input_dir + if self.output_dir is not None: + block["output_dir"] = self.output_dir + block["output_dir_source"] = self.output_dir_source + return block + + +def _same_path(a: str, b: str) -> bool: + try: + return os.path.samefile(a, b) + except OSError: + return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b)) + + +def _read_json(path: Path) -> Any: + with open(path, encoding="utf-8") as fh: + return json.load(fh) + + +def _venv_python(venv: Path) -> Path | None: + for candidate in (venv / "bin" / "python3", venv / "bin" / "python", venv / "Scripts" / "python.exe"): + if candidate.is_file(): + return candidate + return None + + +def _code_dir(install_path: Path) -> Path: + """Mirror Desktop's ``resolveComfyDir``: ``/ComfyUI``, or the + install path itself when the checkout sits there directly.""" + nested = install_path / "ComfyUI" + if not nested.is_dir() and (install_path / "main.py").is_file(): + return install_path + return nested + + +def _version_from_marker(code_dir: Path) -> str | None: + try: + match = _VERSION_MARKER_RE.search((code_dir / "comfyui_version.py").read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError): + return None + return match.group(1) if match else None + + +def _as_tag(version: str) -> str: + version = version.strip() + return "v" + version if _BARE_RELEASE_RE.match(version) else version + + +def _str_list(value: Any) -> list[str]: + return [item for item in value if isinstance(item, str) and item] if isinstance(value, list) else [] + + +def _model_roots(data_dir: Path, record: dict, shared_dirs: list[str]) -> tuple[ModelRoot, ...]: + """Every root ComfyUI searches for this install, in search order, de-duplicated. + + Desktop writes the shared and per-install dirs into an + ``--extra-model-paths-config`` YAML at launch; ComfyUI always searches the + data dir's own ``models/`` as well. + """ + candidates: list[tuple[str, str]] = [(str(data_dir / "models"), "data")] + if record.get("useSharedModels") is not False: + candidates += [(os.path.abspath(os.path.expanduser(d)), "shared") for d in shared_dirs] + candidates += [(os.path.abspath(os.path.expanduser(d)), "install") for d in _str_list(record.get("modelDirs"))] + roots: list[ModelRoot] = [] + for path, source in candidates: + if any(_same_path(path, root.path) for root in roots): + continue + roots.append(ModelRoot(path=path, source=source, exists=os.path.isdir(path))) + return tuple(roots) + + +def _install_from_record( + record: dict, shared_dirs: list[str], legacy_base_path: str | None, *, registry_dir: Path | None = None +) -> DesktopInstall | None: + install_path = record.get("installPath") + if not isinstance(install_path, str) or not install_path: + return None # a remote entry (Comfy Cloud) has no local files + install_root = Path(os.path.expanduser(install_path)) + code_dir = _code_dir(install_root) + adopted = record.get("adopted") is True + base_dir = record.get("adoptedBaseDir") if adopted else None + if adopted and not (isinstance(base_dir, str) and base_dir): + base_dir = legacy_base_path + data_dir = Path(os.path.expanduser(base_dir)) if isinstance(base_dir, str) and base_dir else code_dir + + python: Path | None = None + adopted_python = record.get("adoptedPythonPath") if adopted else None + if isinstance(adopted_python, str) and Path(adopted_python).is_file(): + python = Path(adopted_python) + if python is None: + python = _venv_python(data_dir / ".venv") + + version_record = record.get("comfyVersion") if isinstance(record.get("comfyVersion"), dict) else {} + base_tag = version_record.get("baseTag") + version = base_tag if isinstance(base_tag, str) and base_tag else _version_from_marker(code_dir) + commit = version_record.get("commit") + last_launched = record.get("lastLaunchedAt") + + return DesktopInstall( + install_id=record.get("id") if isinstance(record.get("id"), str) else None, + name=record.get("name") if isinstance(record.get("name"), str) else None, + code_dir=str(code_dir), + data_dir=str(data_dir), + python=str(python) if python else None, + model_roots=_model_roots(data_dir, record, shared_dirs), + comfyui_version=_as_tag(version) if version else None, + comfyui_commit=commit if isinstance(commit, str) and commit else None, + adopted=adopted, + status=record.get("status") if isinstance(record.get("status"), str) else None, + last_launched_ms=last_launched if isinstance(last_launched, (int, float)) and last_launched > 0 else None, + exists=(code_dir / "main.py").is_file(), + registry_dir=str(registry_dir) if registry_dir is not None else None, + ) + + +def _select(installs: tuple[DesktopInstall, ...]) -> tuple[DesktopInstall | None, str | None]: + """Pick the install an agent should assume, and say why.""" + usable = [install for install in installs if install.exists] + if not usable: + return None, None + if len(usable) == 1: + return usable[0], "the only Comfy Desktop install with a ComfyUI checkout on disk" + launched = [install for install in usable if install.last_launched_at] + if launched: + chosen = max(launched, key=lambda install: install.last_launched_ms or 0) + return chosen, ( + f"the most recently launched of {len(usable)} Comfy Desktop installs (last launched " + f"{chosen.last_launched_at}); pass --workspace to pick another" + ) + return usable[0], ( + f"the first of {len(usable)} Comfy Desktop installs in installations.json (none has been launched); " + "pass --workspace to pick another" + ) + + +def app_data_dirs() -> tuple[Path, ...]: + """Known registry folders, keeping an explicit/test override isolated.""" + primary = desktop_app_data_dir() + if primary is None: + return () + if primary.name not in (APP_DIR_NAME, MODERN_APP_DIR_NAME): + return (primary,) + return tuple(dict.fromkeys((primary, primary.parent / MODERN_APP_DIR_NAME, primary.parent / APP_DIR_NAME))) + + +def _argv_values(argv: list[str], flag: str) -> list[str]: + values: list[str] = [] + for index, item in enumerate(argv): + if item.startswith(flag + "="): + values.append(item.split("=", 1)[1]) + elif item == flag and index + 1 < len(argv): + for value in argv[index + 1 :]: + if value.startswith("--"): + break + values.append(value) + if flag != "--extra-model-paths-config": + break + return values + + +def server_launch_paths(stats: dict[str, Any]) -> dict[str, list[str]]: + """Return only known filesystem launch arguments, excluding unrelated credential flags.""" + system = stats.get("system") or {} + argv = system.get("argv") if isinstance(system, dict) else None + if not isinstance(argv, list) or not all(isinstance(arg, str) for arg in argv): + return {} + return { + name: values + for name in ("base_directory", "input_directory", "output_directory", "extra_model_paths_config") + if (values := _argv_values(argv, "--" + name.replace("_", "-"))) + } + + +def _config_identifies_install(config: str, install: DesktopInstall) -> bool: + install_id = install.install_id + if ( + not install.registry_dir + or not install_id + or install_id in (".", "..") + or any(c in install_id for c in "/\\:\x00") + ): + return False + path = Path(config).expanduser() + if not path.is_absolute(): + return False + expected = Path(install.registry_dir) / "instance-model-paths" / f"{install_id}.yaml" + return _same_path(str(path), str(expected)) + + +def _for_server( + found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool = False +) -> DesktopDetection: + """Select using launch identity, never a remembered last-launched record.""" + if not filesystem_verified: + return replace( + found, + selected=None, + selected_reason=None, + input_dir=None, + output_dir=None, + output_dir_source=None, + problems=(*found.problems, "running server filesystem is unverified; pass explicit model/input roots"), + ) + system = stats.get("system") or {} + if not isinstance(system, dict): + system = {} + argv = system.get("argv") or [] + if not isinstance(argv, list) or not all(isinstance(arg, str) for arg in argv): + argv = [] + bases = _argv_values(argv, "--base-directory") + configs = _argv_values(argv, "--extra-model-paths-config") + scripts = [arg for arg in argv if Path(arg).is_absolute() and Path(arg).name == "main.py"] + matches = [ + install + for install in found.installs + if install.exists + and ( + any(_same_path(base, install.data_dir) for base in bases) + or any(_same_path(str(Path(script).parent), install.code_dir) for script in scripts) + or any(_config_identifies_install(config, install) for config in configs) + ) + ] + if len(matches) != 1: + detail = "does not identify one recorded Desktop installation" + return replace( + found, + selected=None, + selected_reason=None, + problems=(*found.problems, f"running server {detail}; pass explicit model/input roots"), + ) + chosen = matches[0] + outputs = _argv_values(argv, "--output-directory") + if any( + not Path(path).expanduser().is_absolute() + for path in (*bases, *_argv_values(argv, "--input-directory"), *outputs) + ): + return replace( + found, + selected=None, + selected_reason=None, + problems=( + *found.problems, + "running server uses relative data/input/output paths; verify its working directory", + ), + ) + data_dir = str(Path(bases[-1]).expanduser()) if bases else chosen.data_dir + roots = [root for root in chosen.model_roots if root.source != "data"] if not configs else [] + roots.insert(0, ModelRoot(str(Path(data_dir) / "models"), "data", (Path(data_dir) / "models").is_dir())) + problems = list(found.problems) + for config in configs: + config_path = Path(config).expanduser() + if not config_path.is_absolute(): + problems.append(f"running server model config is relative: {config}; verify its working directory") + continue + try: + import yaml + + contents = yaml.safe_load(config_path.read_text(encoding="utf-8")) + if not isinstance(contents, dict): + raise ValueError("expected a mapping") + for row in contents.values(): + if not isinstance(row, dict): + continue + base = row.get("base_path") + base_path = None + if isinstance(base, str) and base: + base_path = Path(os.path.expandvars(base)).expanduser() + if not base_path.is_absolute(): + base_path = config_path.parent / base_path + base_path = base_path.resolve() + paths = [base_path] if base_path is not None else [] + for kind, value in row.items(): + if kind in ("base_path", "is_default"): + continue + if not isinstance(value, str): + raise ValueError(f"{kind}: expected newline-separated model paths") + for entry in value.splitlines(): + if not entry: + continue + path = Path(entry) + if not path.is_absolute(): + path = (base_path or config_path.parent) / path + path = path.resolve() + # A base covers its descendant type folders. Keep a + # separate root for absolute paths outside that base. + if base_path is None or not path.is_relative_to(base_path): + paths.append(path) + for path in paths: + if not any(_same_path(str(path), root.path) for root in roots): + roots.append(ModelRoot(str(path), "install", path.is_dir())) + except (OSError, ValueError, yaml.YAMLError) as error: + problems.append(f"{config}: {error}") + python = system.get("python") + version = system.get("comfyui_version") + chosen = replace( + chosen, + comfyui_version=_as_tag(version) if isinstance(version, str) and version else chosen.comfyui_version, + data_dir=data_dir, + model_roots=tuple(roots), + python=python if isinstance(python, str) and Path(python).is_file() else None, + ) + inputs = _argv_values(argv, "--input-directory") + input_dir = str(Path(inputs[-1]).expanduser()) if inputs else str(Path(data_dir) / "input") + return replace( + found, + selected=chosen, + selected_reason="matches the running server launch arguments", + installs=tuple(chosen if item is matches[0] else item for item in found.installs), + input_dir=input_dir, + output_dir=str(Path(outputs[-1]).expanduser()) if outputs else str(Path(data_dir) / "output"), + output_dir_source="server_argv" if outputs else "server_default", + problems=tuple(problems), + ) + + +def detect( + app_data_dir: Path | None = None, + legacy_dir: Path | None = None, + *, + server_stats: dict[str, Any] | None = None, + server_filesystem_verified: bool = False, +) -> DesktopDetection | None: + """Read Desktop 2's install records. ``None`` means Desktop 2 is not installed. + + The app-data dirs default to this OS's; tests pass fixture dirs instead. + Server paths require caller-established local process/filesystem provenance; + matching strings or a loopback URL alone are insufficient. + """ + if app_data_dir is None: + detections = [found for directory in app_data_dirs() if (found := detect(directory, legacy_dir)) is not None] + if not detections: + return None + installs = tuple(install for found in detections for install in found.installs) + selected, reason = _select(installs) + origin = next( + (found.app_data_dir for found in detections if selected in found.installs), detections[0].app_data_dir + ) + found = DesktopDetection( + origin, installs, selected, reason, tuple(problem for item in detections for problem in item.problems) + ) + return ( + _for_server(found, server_stats, filesystem_verified=server_filesystem_verified) + if server_stats is not None + else found + ) + app_dir = app_data_dir + if app_dir is None: + return None + installations_file = app_dir / "installations.json" + if not installations_file.is_file(): + return None + + problems: list[str] = [] + try: + records = _read_json(installations_file) + except (OSError, UnicodeDecodeError, ValueError) as error: + return DesktopDetection(app_data_dir=str(app_dir), problems=(f"{installations_file}: {error}",)) + if not isinstance(records, list): + return DesktopDetection( + app_data_dir=str(app_dir), problems=(f"{installations_file}: expected a list of installs",) + ) + + shared_dirs: list[str] = [] + settings_file = app_dir / "settings.json" + if settings_file.is_file(): + try: + settings = _read_json(settings_file) + shared_dirs = _str_list(settings.get("modelsDirs")) if isinstance(settings, dict) else [] + except (OSError, UnicodeDecodeError, ValueError) as error: + problems.append(f"{settings_file}: {error} (shared model roots are missing from model_roots)") + + legacy_base_path: str | None = None + legacy = legacy_dir if legacy_dir is not None else legacy_config_dir() + if legacy is not None and (legacy / "config.json").is_file(): + try: + config = _read_json(legacy / "config.json") + base = config.get("basePath") if isinstance(config, dict) else None + legacy_base_path = base if isinstance(base, str) and base else None + except (OSError, UnicodeDecodeError, ValueError) as error: + legacy_base_path = None + problems.append(f"{legacy / 'config.json'}: {error}") + + if legacy_base_path is None and any( + isinstance(record, dict) + and record.get("adopted") is True + and not (isinstance(record.get("adoptedBaseDir"), str) and record["adoptedBaseDir"]) + for record in records + ): + problems.append( + "an adopted Desktop installation has no readable data path; its data_dir and derived model roots " + "fall back to the code directory and are unverified" + ) + + installs = tuple( + install + for install in ( + _install_from_record(record, shared_dirs, legacy_base_path, registry_dir=app_dir) + for record in records + if isinstance(record, dict) + ) + if install is not None + ) + selected, reason = _select(installs) + found = DesktopDetection( + app_data_dir=str(app_dir), + installs=installs, + selected=selected, + selected_reason=reason, + problems=tuple(problems), + ) + return ( + _for_server(found, server_stats, filesystem_verified=server_filesystem_verified) + if server_stats is not None + else found + ) + + +def install_for_dir(path: str | os.PathLike[str], detection: DesktopDetection | None = None) -> DesktopInstall | None: + """The Desktop install whose code dir or data dir is ``path``, if any.""" + found = detection if detection is not None else detect() + if found is None: + return None + target = os.fspath(path) + for install in found.installs: + if _same_path(target, install.data_dir) or _same_path(target, install.code_dir): + return install + return None diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index ff11c8db7..7d23e115a 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -65,6 +65,16 @@ class ErrorCode: "Resolved no workspace where one was required (e.g. `comfy which`).", "run `comfy install`, or pass `--workspace`", ), + ErrorCode( + "workspace_not_found", + "The workspace reported by `comfy which` does not exist on disk; details.path names the missing directory.", + "run `comfy install`, or pass `--workspace /path/to/ComfyUI`", + ), + ErrorCode( + "desktop_config_unreadable", + "Comfy Desktop installation records or settings could not be read; discovery may be incomplete.", + "check the Desktop configuration file named in the warning", + ), # --- launch / stop lifecycle --------------------------------------------- ErrorCode( "server_already_running", diff --git a/comfy_cli/output/panels.py b/comfy_cli/output/panels.py index e98e31266..0db8d4bcb 100644 --- a/comfy_cli/output/panels.py +++ b/comfy_cli/output/panels.py @@ -224,6 +224,7 @@ def which_panel( server_running: bool = False, server_url: str | None = None, version: str = "0.0.0", + desktop: Mapping[str, Any] | None = None, ) -> Panel: type_badge = Text(workspace_type or "—", style="bold green") if workspace_type else Text("—", style="dim") python_value = Text("—", style="dim") @@ -247,11 +248,21 @@ def which_panel( ) rows: list[tuple[str, Any]] = [ - ("Path", Text(workspace_path, style="bold white")), + ("Path", Text(sanitize_value(workspace_path), style="bold white")), ("Type", type_badge), ("Python", python_value), ("Server", server_value), ] + if desktop: + rows.extend( + [ + ("Desktop code", Text(sanitize_value(desktop["code_dir"]))), + ("Desktop data", Text(sanitize_value(desktop["data_dir"]))), + ("Desktop Python", Text(sanitize_value(desktop["python"] or "—"))), + ("Model roots", Text("\n".join(sanitize_value(root["path"]) for root in desktop["model_roots"]))), + ("Selected because", Text(sanitize_value(desktop["selected_reason"] or "—"))), + ] + ) tbl = Table.grid(padding=(0, 2), expand=False) tbl.add_column(justify="right", style="bold cyan", no_wrap=True) tbl.add_column(overflow="fold") diff --git a/comfy_cli/schemas/env.json b/comfy_cli/schemas/env.json index fced0c67a..0e91f88f9 100644 --- a/comfy_cli/schemas/env.json +++ b/comfy_cli/schemas/env.json @@ -6,6 +6,14 @@ "required": ["python", "config", "workspace", "server"], "additionalProperties": true, "properties": { + "warnings": { + "type": "array", + "items": { + "type": "object", + "required": ["code", "message"], + "properties": {"code": {"type": "string"}, "message": {"type": "string"}} + } + }, "python": { "type": "object", "required": ["version", "executable"], @@ -31,6 +39,36 @@ "type": "object", "additionalProperties": true, "properties": { + "exists": {"type": "boolean", "description": "Whether the workspace directory exists on disk."}, + "desktop": { + "type": "object", + "description": "Desktop installation records, including separate code, data, Python, and model roots.", + "properties": { + "code_dir": {"type": ["string", "null"]}, + "data_dir": {"type": ["string", "null"]}, + "custom_nodes_dir": {"type": ["string", "null"]}, + "python": {"type": ["string", "null"]}, + "input_dir": {"type": "string", "description": "Effective input directory of the selected running Desktop server."}, + "output_dir": {"type": "string", "description": "Effective output directory of the selected running Desktop server."}, + "output_dir_source": {"enum": ["server_argv", "server_default"]}, + "selected_reason": {"type": ["string", "null"]}, + "comfyui_version": {"type": ["string", "null"]}, + "model_roots": { + "type": "array", + "items": { + "type": "object", + "required": ["path", "source", "exists"], + "properties": { + "path": {"type": "string"}, + "source": {"enum": ["data", "shared", "install"]}, + "exists": {"type": "boolean"} + } + } + }, + "installs": {"type": "array", "items": {"type": "object"}}, + "problems": {"type": "array", "items": {"type": "string"}} + } + }, "path": {"type": ["string", "null"]}, "type": {"type": ["string", "null"]}, "manager_mode": {"type": ["string", "null"]}, diff --git a/comfy_cli/schemas/which.json b/comfy_cli/schemas/which.json index a727e4510..f160595ef 100644 --- a/comfy_cli/schemas/which.json +++ b/comfy_cli/schemas/which.json @@ -6,6 +6,44 @@ "required": ["workspace_path"], "additionalProperties": true, "properties": { + "exists": {"type": "boolean", "description": "Whether the workspace directory exists on disk."}, + "desktop": { + "type": "object", + "description": "Desktop installation records, including separate code, data, Python, and model roots.", + "properties": { + "code_dir": {"type": ["string", "null"]}, + "data_dir": {"type": ["string", "null"]}, + "custom_nodes_dir": {"type": ["string", "null"]}, + "python": {"type": ["string", "null"]}, + "input_dir": {"type": "string", "description": "Effective input directory of the selected running Desktop server."}, + "output_dir": {"type": "string", "description": "Effective output directory of the selected running Desktop server."}, + "output_dir_source": {"enum": ["server_argv", "server_default"]}, + "selected_reason": {"type": ["string", "null"]}, + "comfyui_version": {"type": ["string", "null"]}, + "model_roots": { + "type": "array", + "items": { + "type": "object", + "required": ["path", "source", "exists"], + "properties": { + "path": {"type": "string"}, + "source": {"enum": ["data", "shared", "install"]}, + "exists": {"type": "boolean"} + } + } + }, + "installs": {"type": "array", "items": {"type": "object"}}, + "problems": {"type": "array", "items": {"type": "string"}} + } + }, + "warnings": { + "type": "array", + "items": { + "type": "object", + "required": ["code", "message"], + "properties": {"code": {"type": "string"}, "message": {"type": "string"}} + } + }, "workspace_path": {"type": ["string", "null"]}, "workspace_type": {"type": ["string", "null"]} } diff --git a/comfy_cli/workspace_manager.py b/comfy_cli/workspace_manager.py index 4be5b4af7..53d5b774b 100644 --- a/comfy_cli/workspace_manager.py +++ b/comfy_cli/workspace_manager.py @@ -12,6 +12,7 @@ # Route prints through the output shim so they land on stderr in JSON mode # rather than corrupting the envelope on stdout. from comfy_cli.output import rprint as print # noqa: A001 - intentional shadowing +from comfy_cli.output.sanitize import sanitize_markup from comfy_cli.utils import singleton @@ -378,6 +379,36 @@ def _reported_manager_state(self) -> tuple[str, str]: return mode, detected + def discovery_data(self) -> dict: + """Describe the selected workspace, including Desktop's split layout. + + Registry-derived Desktop candidates remain separate from the workspace + used by commands that install or launch code. + """ + from dataclasses import replace + + from comfy_cli import desktop_install + + path = self.workspace_path + kind = self.workspace_type.value if self.workspace_type is not None else None + exists = bool(path and os.path.isdir(path)) + data = {"path": str(path) if path else None, "type": kind, "exists": exists} + found = desktop_install.detect() + selected = desktop_install.install_for_dir(path, found) if path and found else None + if found and (selected or not data["exists"]): + if selected: + reason = found.selected_reason if selected is found.selected else "matches the selected workspace" + found = replace(found, selected=selected, selected_reason=reason) + data["desktop"] = found.to_dict() + if found.problems: + data["desktop"]["problems"] = list(found.problems) + data["warnings"] = [{"code": "desktop_config_unreadable", "message": p} for p in found.problems] + if not data["exists"]: + data.setdefault("warnings", []).insert( + 0, {"code": "workspace_not_found", "message": f"Workspace directory does not exist: {data['path']}"} + ) + return data + def fill_print_table(self): config_manager = ConfigManager() mode, _detected = self._reported_manager_state() @@ -398,11 +429,25 @@ def fill_print_table(self): else: uv_compile_status = "[dim]Disabled[/dim]" - return [ - ("Current selected workspace", f"[bold green]→ {self.workspace_path}[/bold green]"), + discovery = self.discovery_data() + rows = [ + ("Current selected workspace", f"[bold green]→ {sanitize_markup(discovery['path'])}[/bold green]"), ("Manager", manager_status), ("UV Compile Default", uv_compile_status), ] + desktop = discovery.get("desktop") + if desktop and desktop["code_dir"]: + rows.extend( + [ + ("Desktop code", sanitize_markup(desktop["code_dir"])), + ("Desktop data", sanitize_markup(desktop["data_dir"])), + ] + ) + rows.append( + ("Desktop model roots", "\n".join(sanitize_markup(root["path"]) for root in desktop["model_roots"])) + ) + rows.extend(("Warning", sanitize_markup(warning["message"])) for warning in discovery.get("warnings", [])) + return rows def fill_data(self) -> dict: """Structured workspace info for ``comfy env --json``.""" @@ -410,8 +455,7 @@ def fill_data(self) -> dict: uv_compile_value = config_manager.get(constants.CONFIG_KEY_UV_COMPILE_DEFAULT) mode, detected = self._reported_manager_state() return { - "path": str(self.workspace_path) if self.workspace_path else None, - "type": self.workspace_type.value if self.workspace_type is not None else None, + **self.discovery_data(), "manager_mode": mode, "manager_detected": detected, "uv_compile_default": (str(uv_compile_value).lower() == "true" if uv_compile_value is not None else False), diff --git a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md new file mode 100644 index 000000000..a7d30de29 --- /dev/null +++ b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md @@ -0,0 +1,100 @@ +# ADR-DESKTOP-DISCOVERY-0006: Preserve Runtime Path Identity + +Date: 2026-10-25 + +## Status + +Proposed + +Relation: Builds on [ADR-AUTOMATION-0003](AUTOMATION-0003-one-versioned-machine-output-dialect.md). +The versioned envelope, additive field/error compatibility and separation of +human diagnostics from machine output remain unchanged. + +## Context + +Desktop separates its code checkout, data directory, interpreter and shared +model folders. Modern and legacy registries can coexist, and an existing +last-launched installation is not necessarily the process serving a workflow. +Model-path YAML and launch flags can override the remembered data/input/output +roots. Choosing another installation silently can inventory or upload unrelated +files while reporting the wrong version. + +## Decision + +- Discover modern `comfyui-desktop-2`, `Comfy Desktop`, and supported legacy + records. Keep each registry's model-root provenance with its installation; + explicit caller/test registry overrides remain isolated. +- Without running-server evidence, describe selection as a registry-derived + candidate and expose its selection reason. Do not claim it identifies the + running process. +- Server launch metadata is not evidence of a shared filesystem, even for a + loopback endpoint (which may be a forwarder). Before using its paths to select + a local Desktop installation or read local configurations/interpreters/roots, + require caller-established direct-endpoint and local-process provenance. Bind + the request to its resolved address, bypass proxies, refuse redirects, match + the address-specific socket owner across the request using PID/create time, + and corroborate the actual Python script/argv/cwd. On Linux, require the same + mount namespace and process root as the CLI; inability to read that evidence + leaves provenance unknown. The pure discovery API defaults provenance to + unknown. Remote, forwarded, container, inaccessible or ambiguous endpoints + retain their reported catalog/metadata but require explicit local roots. +- With verified shared-filesystem provenance, require one recorded existing + installation to match its absolute code/data path or the full resolved generated + config path: that record's owning registry directory plus + `instance-model-paths/.yaml`. A copied filename/stem is insufficient; + retain per-record ownership across coexisting registries and reject path + separators and Windows drive/stream separators in IDs used for config identity. This preserves bundled/shared-code + discovery without conflating registries. Internal ownership adds no public + output fields. Zero or conflicting matches leave selection unresolved instead of combining + unrelated installations. +- Derive effective code/data/model/input/output roots from that selected process. + Resolve its model-path configuration and retain external paths explicitly. + Report the running version when supplied. The output root is the explicit + launch path or that process's data/output default, with its source recorded. +- Relative data/input/output paths cannot be resolved without the process's cwd: + retain uncertainty rather than anchoring them to the agent's cwd. Relative or + unreadable model config paths remain diagnostic evidence, never a claim that + all required model folders were found. +- Surface missing directories with existence flags and warnings. Discovery is + read-only: it creates no default workspace, installs nothing and grants no + permission to inspect/upload every file under a shared root. +- Optional which/env discovery fields remain additive. Runtime launch reporting + contains known path flags only, not arbitrary argv values or credentials. + File-upload scope and approvals belong to the calling operation; discovering + an output directory does not authorize uploading its whole contents. + +## Alternatives considered + +Selecting the newest registry entry alone is insufficient when a caller has +actual process evidence. Combining every existing install/model directory hides +identity errors and expands scope. Guessing relative paths from the CLI's cwd +can point at unrelated files. These approaches are rejected even when their +paths happen to exist. + +## Consequences + +Discovery can support both installed Desktop generations while callers retain +explicit path/selection provenance. Ambiguous, stale or relative process metadata +can require a user's verified path/cwd instead of automatic continuation. +Registry layouts and server metadata remain external compatibility surfaces; +unknown/malformed records are diagnostics rather than evidence of availability. +An interpreter is not inferred solely from a remembered data directory when +verified instance/process information is available. + +## Validation + +`test_desktop_install.py` covers registry isolation, actual process matching, +model-config roots, explicit/default input/output paths, relative-cwd ambiguity +and serialization. `test_which_desktop.py`, workspace-manager and Manager GUI +tests cover public reporting, missing roots and warning compatibility. Run those +tests with the worktree's package first on `PYTHONPATH`, plus Ruff lint/format, +`python scripts/check_adrs.py` and its native validator tests. + +## Revisit triggers + +Reopen if one running process resolves to another installation, if an unresolved +relative path is reported as verified, if server metadata bypasses direct local +process/filesystem proof, if shared folders lose provenance, or if +new Desktop layouts/launch flags require an additional documented discovery +source. A change that allows discovery to mutate installations or grants upload +permission requires a separate decision. diff --git a/tests/comfy_cli/command/test_manager_gui.py b/tests/comfy_cli/command/test_manager_gui.py index a84532090..1ed8e2403 100644 --- a/tests/comfy_cli/command/test_manager_gui.py +++ b/tests/comfy_cli/command/test_manager_gui.py @@ -834,11 +834,12 @@ def test_fill_print_table_disable_mode(self, mock_resolve, mock_workspace_config result = ws.fill_print_table() - assert len(result) == 3 + assert len(result) == 4 assert result[0][0] == "Current selected workspace" assert result[1][0] == "Manager" assert "Disabled" in result[1][1] assert result[2][0] == "UV Compile Default" + assert result[3] == ("Warning", "Workspace directory does not exist: /fake/workspace") @patch("comfy_cli.command.custom_nodes.cm_cli_util.resolve_manager_gui_mode", return_value="enable-gui") def test_fill_print_table_enable_gui_mode(self, mock_resolve, mock_workspace_config_manager): @@ -1056,9 +1057,19 @@ def test_fill_data_keeps_existing_fields(self): with self._env(configured_mode=None, has_cm_cli=True, has_legacy_clone=False) as ws: data = ws.fill_data() - assert set(data) == {"path", "type", "manager_mode", "manager_detected", "uv_compile_default"} + assert set(data) == { + "path", + "type", + "manager_mode", + "manager_detected", + "uv_compile_default", + "exists", + "warnings", + } assert data["path"] == "/fake/workspace" assert data["uv_compile_default"] is False + assert data["exists"] is False + assert data["warnings"][0]["code"] == "workspace_not_found" def test_fill_print_table_reports_legacy_clone(self): """The table view matches the JSON view for the legacy-clone case.""" diff --git a/tests/comfy_cli/conftest.py b/tests/comfy_cli/conftest.py index 522edcf38..c16a41126 100644 --- a/tests/comfy_cli/conftest.py +++ b/tests/comfy_cli/conftest.py @@ -169,3 +169,83 @@ def test_my_pretty_thing(pretty_no_stdout): "that to be empty (all output goes to stderr in pretty mode):\n" f" stdout = {captured.out!r}\n" ) + + +@pytest.fixture(autouse=True) +def _isolate_comfy_desktop(tmp_path, monkeypatch): + """Point Comfy Desktop detection at an empty per-test dir. + + ``comfy which`` / ``comfy env`` (and ``comfy build init``) read Desktop's + ``installations.json`` and ``settings.json`` from the real app-data dir. + On a developer machine with Desktop installed, every test touching those + commands would otherwise see that machine's installs. A test that wants a + Desktop install builds one with the ``comfy_desktop`` fixture. + """ + from comfy_cli import desktop_install + + monkeypatch.setattr(desktop_install, "desktop_app_data_dir", lambda: tmp_path / "no-comfy-desktop") + monkeypatch.setattr(desktop_install, "legacy_config_dir", lambda: None) + yield + + +@pytest.fixture +def comfy_desktop(tmp_path, monkeypatch): + """A Comfy Desktop 2 install laid out the way the bug report found it. + + An install adopted from Desktop 1: the ComfyUI checkout under + ``/ComfyUI``, the data (models, custom_nodes, venv) in the + Desktop 1 base path (``config.json`` → ``basePath``, copied into the + record as ``adoptedBaseDir``), and a shared model library listed in + ``settings.json`` → ``modelsDirs`` alongside the data dir's own + ``models/``. Detection is pointed at it; the returned dict names each part. + """ + import json + + from comfy_cli import desktop_install + + install_path = tmp_path / "ComfyUI-Installs" / "ComfyUI" + code = install_path / "ComfyUI" + code.mkdir(parents=True) + (code / "main.py").write_text("") + data = tmp_path / "Documents" / "ComfyUI" + for sub in ("models/checkpoints", "custom_nodes", "user"): + (data / sub).mkdir(parents=True) + python = data / ".venv" / "bin" / "python3" + python.parent.mkdir(parents=True) + python.write_text("") + shared = tmp_path / "ComfyUI-Shared" / "models" + shared.mkdir(parents=True) + legacy = tmp_path / "Application Support" / "ComfyUI" + legacy.mkdir(parents=True) + (legacy / "config.json").write_text(json.dumps({"basePath": str(data)})) + record = { + "id": "inst-adopted", + "name": "ComfyUI", + "sourceId": "standalone", + "installPath": str(install_path), + "adopted": True, + "adoptedBaseDir": str(data), + "adoptedPythonPath": str(python), + "comfyVersion": {"commit": "b53f77a7", "baseTag": "v0.34.2", "commitsAhead": 0}, + "useSharedModels": True, + "status": "installed", + "lastLaunchedAt": 1792188627164, + } + cloud = {"id": "inst-cloud", "name": "Comfy Cloud", "sourceId": "cloud", "remoteUrl": "https://cloud.comfy.org/"} + app = tmp_path / "Application Support" / "Comfy Desktop" + app.mkdir(parents=True) + (app / "installations.json").write_text(json.dumps([record, cloud])) + (app / "settings.json").write_text(json.dumps({"modelsDirs": [str(shared), str(data / "models")]})) + + monkeypatch.setattr(desktop_install, "desktop_app_data_dir", lambda: app) + monkeypatch.setattr(desktop_install, "legacy_config_dir", lambda: legacy) + return { + "app": app, + "legacy": legacy, + "install_path": install_path, + "code": code, + "data": data, + "python": python, + "shared": shared, + "record": record, + } diff --git a/tests/comfy_cli/output/test_envelope_schemas.py b/tests/comfy_cli/output/test_envelope_schemas.py index bcd387916..ae22891d8 100644 --- a/tests/comfy_cli/output/test_envelope_schemas.py +++ b/tests/comfy_cli/output/test_envelope_schemas.py @@ -119,10 +119,16 @@ def test_env_json_validates(): assert envelope["ok"] is True -def test_which_json_validates(): - envelope = _run_cli(["--json", "which"]) +def test_which_json_validates(tmp_path): + workspace = tmp_path / "ComfyUI" + workspace.mkdir() + (workspace / "main.py").write_text("") + envelope = _run_cli(["--json", "--workspace", str(workspace), "which"]) _validator_for("envelope.json").validate(envelope) + assert envelope["ok"] is True _validator_for("which.json").validate(envelope["data"]) + assert envelope["data"]["workspace_path"] == str(workspace) + assert envelope["data"]["exists"] is True def test_help_json_validates(): diff --git a/tests/comfy_cli/test_desktop_install.py b/tests/comfy_cli/test_desktop_install.py new file mode 100644 index 000000000..3d069bd29 --- /dev/null +++ b/tests/comfy_cli/test_desktop_install.py @@ -0,0 +1,615 @@ +"""Comfy Desktop 2 detection from the records Desktop keeps on disk. + +Every test builds its own fixture layout under ``tmp_path``; none reads the +machine's real Desktop files (the autouse ``_isolate_comfy_desktop`` fixture +points detection at an empty dir unless a test passes its own). +""" + +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +from comfy_cli import desktop_install +from comfy_cli.constants import OS + +# Bound at import, before the autouse isolation fixture replaces the module +# attributes, so the per-OS resolution itself can still be tested. +_real_app_data_dir = desktop_install.desktop_app_data_dir +_real_legacy_config_dir = desktop_install.legacy_config_dir + + +def _checkout(code_dir: Path, version: str | None = None) -> Path: + code_dir.mkdir(parents=True, exist_ok=True) + (code_dir / "main.py").write_text("") + if version: + (code_dir / "comfyui_version.py").write_text(f'__version__ = "{version}"\n') + return code_dir + + +def _venv(root: Path) -> Path: + py = root / ".venv" / "bin" / "python3" + py.parent.mkdir(parents=True, exist_ok=True) + py.write_text("") + return py + + +def _write_desktop(app_dir: Path, installs: list, *, models_dirs: list[str] | None = None) -> Path: + app_dir.mkdir(parents=True, exist_ok=True) + (app_dir / "installations.json").write_text(json.dumps(installs)) + (app_dir / "settings.json").write_text(json.dumps({"modelsDirs": models_dirs or []})) + return app_dir + + +def test_no_installations_file_means_no_desktop(tmp_path): + assert desktop_install.detect(tmp_path / "Comfy Desktop", tmp_path / "legacy") is None + + +def test_adopted_install_reports_its_split_layout(comfy_desktop): + layout = comfy_desktop + + found = desktop_install.detect(layout["app"], layout["legacy"]) + + assert found is not None + install = found.selected + assert install is not None + assert install.install_id == "inst-adopted" + assert install.code_dir == str(layout["code"]) + assert install.data_dir == str(layout["data"]) + assert install.python == str(layout["python"]) + assert install.comfyui_version == "v0.34.2" + assert install.comfyui_commit == "b53f77a7" + # The data dir's own models/ is listed once even though settings.json also + # names it; the shared root follows. + assert [(r.path, r.source) for r in install.model_roots] == [ + (str(layout["data"] / "models"), "data"), + (str(layout["shared"]), "shared"), + ] + assert all(r.exists for r in install.model_roots) + # The Comfy Cloud entry has no local files and is not an install here. + assert [i.install_id for i in found.installs] == ["inst-adopted"] + assert found.selected_reason == "the only Comfy Desktop install with a ComfyUI checkout on disk" + assert found.problems == () + + +def test_to_dict_puts_the_selected_install_at_the_top(comfy_desktop): + layout = comfy_desktop + + block = desktop_install.detect(layout["app"], layout["legacy"]).to_dict() + + assert block["code_dir"] == str(layout["code"]) + assert block["data_dir"] == str(layout["data"]) + assert block["custom_nodes_dir"] == str(layout["data"] / "custom_nodes") + assert block["python"] == str(layout["python"]) + assert block["comfyui_version"] == "v0.34.2" + assert [r["path"] for r in block["model_roots"]] == [str(layout["data"] / "models"), str(layout["shared"])] + assert block["installs"] == [ + { + "install_id": "inst-adopted", + "name": "ComfyUI", + "code_dir": str(layout["code"]), + "data_dir": str(layout["data"]), + "comfyui_version": "v0.34.2", + "last_launched_at": "2026-10-16T22:10:27Z", + "status": "installed", + "exists": True, + "selected": True, + } + ] + assert block["app_data_dir"] == str(layout["app"]) + + +def test_standalone_install_keeps_data_beside_the_code(tmp_path): + install_path = tmp_path / "ComfyUI-Installs" / "Fresh" + code = _checkout(install_path / "ComfyUI") + python = _venv(code) + app = _write_desktop( + tmp_path / "Comfy Desktop", + [{"id": "inst-fresh", "installPath": str(install_path), "comfyVersion": {"baseTag": "v0.41.1"}}], + ) + + install = desktop_install.detect(app, tmp_path / "legacy").selected + + assert install.code_dir == str(code) + assert install.data_dir == str(code) + assert install.python == str(python) + assert [r.path for r in install.model_roots] == [str(code / "models")] + + +def test_most_recently_launched_install_is_selected_and_the_reason_says_so(tmp_path): + older = tmp_path / "Installs" / "Old" + newer = tmp_path / "Installs" / "New" + _checkout(older / "ComfyUI") + _checkout(newer / "ComfyUI") + app = _write_desktop( + tmp_path / "Comfy Desktop", + [ + {"id": "old", "installPath": str(older), "lastLaunchedAt": 1_700_000_000_000}, + {"id": "new", "installPath": str(newer), "lastLaunchedAt": 1_790_000_000_000}, + {"id": "never", "installPath": str(tmp_path / "Installs" / "Gone")}, + ], + ) + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found.selected.install_id == "new" + assert "most recently launched of 2 Comfy Desktop installs" in found.selected_reason + assert [i.install_id for i in found.installs] == ["old", "new", "never"] + rows = {row["install_id"]: row for row in found.to_dict()["installs"]} + assert rows["new"]["selected"] is True + assert rows["old"]["selected"] is False + # An install whose checkout is gone is listed but never selected. + assert rows["never"]["exists"] is False + assert rows["never"]["selected"] is False + + +def test_no_launch_history_picks_the_first_listed_install(tmp_path): + first = tmp_path / "Installs" / "A" + second = tmp_path / "Installs" / "B" + _checkout(first / "ComfyUI") + _checkout(second / "ComfyUI") + app = _write_desktop( + tmp_path / "Comfy Desktop", + [{"id": "a", "installPath": str(first)}, {"id": "b", "installPath": str(second)}], + ) + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found.selected.install_id == "a" + assert "none has been launched" in found.selected_reason + + +def test_no_install_with_a_checkout_selects_nothing(tmp_path): + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "gone", "installPath": str(tmp_path / "missing")}]) + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found.selected is None + assert found.selected_reason is None + assert [i.install_id for i in found.installs] == ["gone"] + + +def test_opting_out_of_shared_models_drops_the_shared_roots(tmp_path): + install_path = tmp_path / "Installs" / "Isolated" + code = _checkout(install_path / "ComfyUI") + extra = tmp_path / "extra-models" + app = _write_desktop( + tmp_path / "Comfy Desktop", + [{"id": "iso", "installPath": str(install_path), "useSharedModels": False, "modelDirs": [str(extra)]}], + models_dirs=[str(tmp_path / "shared")], + ) + + install = desktop_install.detect(app, tmp_path / "legacy").selected + + assert [(r.path, r.source, r.exists) for r in install.model_roots] == [ + (str(code / "models"), "data", False), + (str(extra), "install", False), + ] + + +def test_adopted_record_without_a_base_dir_falls_back_to_desktop_1_config(comfy_desktop): + layout = comfy_desktop + record = dict(layout["record"]) + del record["adoptedBaseDir"] + del record["adoptedPythonPath"] + _write_desktop(layout["app"], [record]) + + install = desktop_install.detect(layout["app"], layout["legacy"]).selected + + assert install.data_dir == str(layout["data"]) + assert install.python == str(layout["python"]) + + +def test_version_falls_back_to_the_checkout_marker(tmp_path): + install_path = tmp_path / "Installs" / "Marker" + _checkout(install_path / "ComfyUI", version="0.41.2") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "m", "installPath": str(install_path)}]) + + install = desktop_install.detect(app, tmp_path / "legacy").selected + + assert install.comfyui_version == "v0.41.2" + + +def test_unreadable_installations_file_is_a_problem_not_a_crash(tmp_path): + app = tmp_path / "Comfy Desktop" + app.mkdir() + (app / "installations.json").write_text("{not json") + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found is not None + assert found.selected is None + assert len(found.problems) == 1 + assert "installations.json" in found.problems[0] + + +def test_unreadable_settings_file_still_detects_the_install(comfy_desktop): + layout = comfy_desktop + (layout["app"] / "settings.json").write_text("[broken") + + found = desktop_install.detect(layout["app"], layout["legacy"]) + + assert found.selected.install_id == "inst-adopted" + assert [r.source for r in found.selected.model_roots] == ["data"] + assert len(found.problems) == 1 + assert "settings.json" in found.problems[0] + + +def test_install_for_dir_matches_the_code_dir_and_the_data_dir(comfy_desktop, tmp_path): + layout = comfy_desktop + found = desktop_install.detect(layout["app"], layout["legacy"]) + + assert desktop_install.install_for_dir(layout["data"], found).install_id == "inst-adopted" + assert desktop_install.install_for_dir(layout["code"], found).install_id == "inst-adopted" + assert desktop_install.install_for_dir(tmp_path, found) is None + + +@pytest.mark.parametrize( + ("os_value", "env", "expected"), + [ + (OS.MACOS, {}, Path.home() / "Library" / "Application Support" / "Comfy Desktop"), + (OS.WINDOWS, {"APPDATA": "/appdata"}, Path("/appdata") / "Comfy Desktop"), + (OS.LINUX, {"XDG_DATA_HOME": "/xdg"}, Path("/xdg") / "Comfy Desktop"), + ], +) +def test_app_data_dir_per_os(monkeypatch, os_value, env, expected): + monkeypatch.setattr(desktop_install, "get_os", lambda: os_value) + for key in ("APPDATA", "XDG_DATA_HOME"): + monkeypatch.delenv(key, raising=False) + for key, value in env.items(): + monkeypatch.setenv(key, value) + + assert _real_app_data_dir() == expected + + +def test_legacy_config_dir_per_os(monkeypatch): + monkeypatch.setattr(desktop_install, "get_os", lambda: OS.MACOS) + assert _real_legacy_config_dir() == Path.home() / "Library" / "Application Support" / "ComfyUI" + monkeypatch.setattr(desktop_install, "get_os", lambda: OS.LINUX) + assert _real_legacy_config_dir() is None + + +@pytest.mark.parametrize("timestamp", [1e30, float("inf")]) +def test_invalid_launch_timestamp_does_not_break_discovery(tmp_path, timestamp): + install_path = tmp_path / "Installs" / "Valid" + _checkout(install_path / "ComfyUI") + app = _write_desktop( + tmp_path / "Comfy Desktop", + [{"id": "valid", "installPath": str(install_path), "lastLaunchedAt": timestamp}], + ) + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found.to_dict()["installs"][0]["last_launched_at"] is None + assert found.selected.install_id == "valid" + + +def test_adopted_install_with_unreadable_legacy_config_marks_data_root_uncertain(tmp_path): + code = _checkout(tmp_path / "checkout") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "adopted", "installPath": str(code), "adopted": True}]) + legacy = tmp_path / "legacy" + legacy.mkdir() + (legacy / "config.json").write_text("{broken") + + found = desktop_install.detect(app, legacy) + + assert any("config.json" in problem for problem in found.problems) + assert any("unverified" in problem and "data" in problem for problem in found.problems) + + +def test_detect_scans_modern_userdata_and_keeps_each_registry_roots(tmp_path, monkeypatch): + old = tmp_path / "Comfy Desktop" + modern = tmp_path / "comfyui-desktop-2" + for app, name in ((old, "old"), (modern, "modern")): + code = _checkout(tmp_path / name / "ComfyUI") + _write_desktop( + app, [{"id": name, "installPath": str(code.parent)}], models_dirs=[str(tmp_path / name / "shared")] + ) + monkeypatch.setattr(desktop_install, "desktop_app_data_dir", lambda: old) + found = desktop_install.detect() + assert {install.install_id for install in found.installs} == {"old", "modern"} + modern_install = next(install for install in found.installs if install.install_id == "modern") + assert str(tmp_path / "modern" / "shared") in [root.path for root in modern_install.model_roots] + assert str(tmp_path / "old" / "shared") not in [root.path for root in modern_install.model_roots] + + +def test_running_server_selects_matching_instance_instead_of_latest(tmp_path): + records = [] + for name, launched in (("running", 1), ("latest", 2)): + code = _checkout(tmp_path / name / "ComfyUI") + records.append({"id": name, "installPath": str(code.parent), "lastLaunchedAt": launched}) + app = _write_desktop(tmp_path / "Comfy Desktop", records) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(tmp_path / "running" / "ComfyUI" / "main.py")]}}, + server_filesystem_verified=True, + ) + assert found.selected.install_id == "running" + assert "running server" in found.selected_reason + + +def test_server_instance_config_matches_install_and_applies_effective_paths(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "inst-live", "installPath": str(code.parent)}]) + config = app / "instance-model-paths" / "inst-live.yaml" + config.parent.mkdir() + root = tmp_path / "effective-models" + config.write_text(f"live:\n base_path: {root}\n checkpoints: checkpoints\n") + data = tmp_path / "effective-data" + inputs = tmp_path / "private-inputs" + stats = { + "system": { + "argv": [ + "main.py", + "--extra-model-paths-config", + str(config), + "--base-directory", + str(data), + "--input-directory", + str(inputs), + ] + } + } + found = desktop_install.detect(app, server_stats=stats, server_filesystem_verified=True) + assert found.selected.install_id == "inst-live" + assert found.selected.data_dir == str(data) + assert str(root) in [root.path for root in found.selected.model_roots] + assert found.to_dict()["input_dir"] == str(inputs) + + +def test_server_that_does_not_match_records_is_not_silently_assumed(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "candidate", "installPath": str(code.parent)}]) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(tmp_path / "different" / "main.py")]}}, + server_filesystem_verified=True, + ) + assert found.selected is None + assert any("running server" in problem for problem in found.problems) + + +def test_server_model_configs_replace_stale_shared_settings(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop( + tmp_path / "Comfy Desktop", + [{"id": "live", "installPath": str(code.parent)}], + models_dirs=[str(tmp_path / "stale")], + ) + first, second = tmp_path / "one.yaml", tmp_path / "two.yaml" + first.write_text(f"one:\n base_path: {tmp_path / 'models-one'}\n checkpoints: checkpoints\n") + second.write_text(f"two:\n base_path: {tmp_path / 'models-two'}\n loras: loras\n") + stats = { + "system": { + "argv": [str(code / "main.py"), "--extra-model-paths-config", str(first), str(second)], + "comfyui_version": "0.99.0", + } + } + found = desktop_install.detect(app, server_stats=stats, server_filesystem_verified=True) + paths = [root.path for root in found.selected.model_roots] + assert str(tmp_path / "stale") not in paths + assert str(tmp_path / "models-one") in paths + assert str(tmp_path / "models-two") in paths + assert found.selected.comfyui_version == "v0.99.0" + + +def test_relative_server_data_paths_are_unverified(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(code / "main.py"), "--base-directory", "relative-data"]}}, + server_filesystem_verified=True, + ) + assert found.selected is None + assert any("relative" in problem for problem in found.problems) + + +def test_server_config_includes_absolute_paths_outside_base_and_no_base_sections(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + config = tmp_path / "models.yaml" + outside = tmp_path / "private-loras" + config.write_text( + f"one:\n base_path: {tmp_path / 'models'}\n loras: {outside}\ntwo:\n checkpoints: relative-checkpoints\n" + ) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + paths = [root.path for root in found.selected.model_roots] + assert str(outside) in paths + assert str(tmp_path / "relative-checkpoints") in paths + assert not found.problems + + +@pytest.mark.parametrize("explicit", [True, False]) +def test_running_server_output_directory_is_reported_with_source(tmp_path, explicit): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + output = tmp_path / "actual-output" if explicit else code / "output" + argv = [str(code / "main.py")] + if explicit: + argv += ["--output-directory", str(output)] + found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}, server_filesystem_verified=True) + assert found.to_dict()["output_dir"] == str(output) + assert found.to_dict()["output_dir_source"] == ("server_argv" if explicit else "server_default") + + +def test_relative_output_directory_requires_process_cwd_verification(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(code / "main.py"), "--output-directory", "relative-output"]}}, + server_filesystem_verified=True, + ) + assert found.selected is None + assert "output_dir" not in found.to_dict() + + +@pytest.mark.parametrize("override_data", [False, True], ids=["registry-data", "process-data"]) +@pytest.mark.parametrize( + "process_python", ["missing", "none", "empty", "wrong-type", "missing-file", "directory", "valid-file"] +) +def test_server_interpreter_uses_only_a_verified_process_path(tmp_path, override_data, process_python): + code = _checkout(tmp_path / "install" / "ComfyUI") + registry_python = _venv(code) + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + legacy = tmp_path / "legacy" + assert desktop_install.detect(app, legacy).selected.python == str(registry_python) + + data = tmp_path / "process-data" if override_data else code + if override_data: + _venv(data) # Even an existing data-directory interpreter is not process evidence. + runtime_python = tmp_path / "process-python" + runtime_python.write_text("") + system = {"argv": [str(code / "main.py"), "--base-directory", str(data)]} + if process_python != "missing": + system["python"] = { + "none": None, + "empty": "", + "wrong-type": 42, + "missing-file": str(tmp_path / "missing-python"), + "directory": str(data), + "valid-file": str(runtime_python), + }[process_python] + + found = desktop_install.detect(app, legacy, server_stats={"system": system}, server_filesystem_verified=True) + + expected_python = str(runtime_python) if process_python == "valid-file" else None + assert found.selected.data_dir == str(data) + assert found.selected.python == expected_python + assert found.to_dict()["python"] == expected_python + + +@pytest.mark.parametrize("match", ["code-path", "config-stem"]) +def test_server_paths_without_filesystem_provenance_do_not_select_local_install(tmp_path, monkeypatch, match): + code = _checkout(tmp_path / "unrelated-local" / "ComfyUI") + _venv(code) + app = _write_desktop(tmp_path / "registry", [{"id": "collision", "installPath": str(code.parent)}]) + config = app / "collision.yaml" + config.write_text("private:\n base_path: /unrelated-local-models\n checkpoints: checkpoints\n") + argv = [str(code / "main.py")] if match == "code-path" else ["/remote/main.py"] + argv += ["--extra-model-paths-config", str(config)] + original = Path.read_text + + def read(path, *args, **kwargs): + assert path != config, "unverified server metadata must not read a local model config" + return original(path, *args, **kwargs) + + monkeypatch.setattr(Path, "read_text", read) + found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}) + + assert found.selected is None + block = found.to_dict() + assert block["python"] is None + assert block["model_roots"] == [] + assert "input_dir" not in block and "output_dir" not in block + assert any("filesystem" in problem for problem in found.problems) + + +def test_verified_local_server_cannot_select_desktop_by_copied_config_stem(tmp_path, monkeypatch): + code = _checkout(tmp_path / "unrelated-desktop" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "collision", "installPath": str(code.parent)}]) + copied = tmp_path / "outside-registry" / "collision.yaml" + copied.parent.mkdir() + copied.write_text("private:\n base_path: /unrelated-private\n loras: loras\n") + original = Path.read_text + + def read(path, *args, **kwargs): + assert path != copied, "a copied config name is not Desktop launch identity" + return original(path, *args, **kwargs) + + monkeypatch.setattr(Path, "read_text", read) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": ["/other/main.py", "--extra-model-paths-config", str(copied)]}}, + server_filesystem_verified=True, + ) + assert found.selected is None + assert found.to_dict()["model_roots"] == [] + + +@pytest.mark.parametrize("selected_registry", [0, 1]) +def test_generated_config_identity_keeps_owning_registry_with_duplicate_ids(tmp_path, monkeypatch, selected_registry): + apps = [] + codes = [] + for name in ("comfyui-desktop-2", "Comfy Desktop"): + code = _checkout(tmp_path / name / "install" / "ComfyUI") + app = _write_desktop(tmp_path / name / "registry", [{"id": "same-id", "installPath": str(code.parent)}]) + config = app / "instance-model-paths" / "same-id.yaml" + config.parent.mkdir() + config.write_text("{}") + apps.append(app) + codes.append(code) + monkeypatch.setattr(desktop_install, "app_data_dirs", lambda: tuple(apps)) + config = apps[selected_registry] / "instance-model-paths" / "same-id.yaml" + found = desktop_install.detect( + server_stats={"system": {"argv": ["/shared/bundled/main.py", "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + assert found.selected.code_dir == str(codes[selected_registry]) + assert [i.summary(selected=i is found.selected)["selected"] for i in found.installs] == [ + selected_registry == 0, + selected_registry == 1, + ] + block = found.to_dict() + assert "registry_dir" not in block + assert all("registry_dir" not in item for item in block["installs"]) + + +@pytest.mark.parametrize( + "install_id", ["../collision", "nested/id", "nested\\id", "/absolute", "C:collision", "null\x00id"] +) +def test_config_identity_rejects_path_separators_in_record_id(tmp_path, install_id): + if ":" in install_id: + from pathlib import PureWindowsPath + + # A drive-relative component discards the owning registry on Windows. + expected = PureWindowsPath("D:/registry/instance-model-paths") / f"{install_id}.yaml" + assert expected.drive == "C:" and not expected.is_absolute() + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": install_id, "installPath": str(code.parent)}]) + config = app / "instance-model-paths" / f"{install_id}.yaml" + found = desktop_install.detect( + app, + server_stats={"system": {"argv": ["/other/main.py", "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + assert found.selected is None + + +def test_unknown_server_provenance_clears_previously_derived_directories(tmp_path): + from dataclasses import replace + + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + found = replace( + desktop_install.detect(app), + input_dir="/previous/input", + output_dir="/previous/output", + output_dir_source="server_argv", + ) + unresolved = desktop_install._for_server(found, {"system": {"argv": [str(code / "main.py")]}}) + assert unresolved.selected is None + assert unresolved.input_dir is None and unresolved.output_dir is None and unresolved.output_dir_source is None + + +def test_generated_config_identity_accepts_verified_file_alias(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + config = app / "instance-model-paths" / "live.yaml" + config.parent.mkdir() + config.write_text("{}") + alias = tmp_path / "verified-config-alias.yaml" + alias.symlink_to(config) + found = desktop_install.detect( + app, + server_stats={"system": {"argv": ["/shared/main.py", "--extra-model-paths-config", str(alias)]}}, + server_filesystem_verified=True, + ) + assert found.selected.install_id == "live" diff --git a/tests/comfy_cli/test_which_desktop.py b/tests/comfy_cli/test_which_desktop.py new file mode 100644 index 000000000..24a24ee59 --- /dev/null +++ b/tests/comfy_cli/test_which_desktop.py @@ -0,0 +1,304 @@ +"""`comfy which` / `comfy env` on a machine where the workspace is missing or is Comfy Desktop's. + +The default workspace comfy-cli falls back to (``~/Documents/comfy/ComfyUI``) +does not exist on a Desktop-only machine. These commands used to report it with +``ok: true``; now a missing path is an error (`which`) or a flagged warning +(`env`), and a Desktop 2 install is reported with its real split layout. +""" + +from __future__ import annotations + +import json +from dataclasses import replace +from pathlib import Path + +import jsonschema +import pytest +from typer.testing import CliRunner + +from comfy_cli import desktop_install, utils +from comfy_cli.cmdline import app, workspace_manager +from comfy_cli.env_checker import EnvChecker + +SCHEMAS_DIR = Path(__file__).resolve().parents[2] / "comfy_cli" / "schemas" + + +def _schema(name: str) -> dict: + return json.loads((SCHEMAS_DIR / name).read_text()) + + +@pytest.fixture +def fallback_workspace(tmp_path, monkeypatch) -> Path: + """comfy-cli's own default workspace, pointed into tmp and not created. + + The cwd is moved to an empty dir so the cwd auto-detect step finds nothing. + """ + fallback = tmp_path / "Documents" / "comfy" / "ComfyUI" + monkeypatch.setattr(utils, "get_not_user_set_default_workspace", lambda: str(fallback)) + elsewhere = tmp_path / "elsewhere" + elsewhere.mkdir() + monkeypatch.chdir(elsewhere) + return fallback + + +def _invoke(*args: str): + result = CliRunner().invoke(app, list(args)) + lines = [line for line in result.stdout.splitlines() if line.strip()] + envelope = json.loads(lines[-1]) if lines and lines[-1].startswith("{") else None + return result, envelope + + +def test_which_keeps_missing_selected_workspace_separate_from_desktop_candidate(comfy_desktop, fallback_workspace): + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 1, result.output + assert envelope["ok"] is False + assert envelope["error"]["code"] == "workspace_not_found" + details = envelope["error"]["details"] + assert details["workspace_type"] == workspace_manager.workspace_type.value == "default" + assert details["path"] == workspace_manager.workspace_path == str(fallback_workspace) + assert not Path(workspace_manager.workspace_path).is_dir() + desktop = details["desktop"] + assert desktop["code_dir"] == str(comfy_desktop["code"]) + assert desktop["data_dir"] == str(comfy_desktop["data"]) + assert desktop["python"] == str(comfy_desktop["python"]) + assert desktop["comfyui_version"] == "v0.34.2" + assert [root["path"] for root in desktop["model_roots"]] == [ + str(comfy_desktop["data"] / "models"), + str(comfy_desktop["shared"]), + ] + assert desktop["selected_reason"] + assert [row["install_id"] for row in desktop["installs"]] == ["inst-adopted"] + + +def test_which_lists_every_desktop_install_and_says_which_it_picked(comfy_desktop, fallback_workspace, tmp_path): + other = tmp_path / "ComfyUI-Installs" / "Test" + (other / "ComfyUI").mkdir(parents=True) + (other / "ComfyUI" / "main.py").write_text("") + records = [ + {"id": "inst-test", "name": "Test", "installPath": str(other), "lastLaunchedAt": 1_700_000_000_000}, + comfy_desktop["record"], + ] + (comfy_desktop["app"] / "installations.json").write_text(json.dumps(records)) + + _, envelope = _invoke("--json", "which") + + desktop = envelope["error"]["details"]["desktop"] + assert desktop["install_id"] == "inst-adopted" + assert "most recently launched of 2" in desktop["selected_reason"] + assert {row["install_id"]: row["selected"] for row in desktop["installs"]} == { + "inst-test": False, + "inst-adopted": True, + } + + +def test_which_errors_when_the_default_workspace_does_not_exist(fallback_workspace): + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 1 + assert envelope["ok"] is False + assert envelope["error"]["code"] == "workspace_not_found" + assert envelope["error"]["details"]["path"] == str(fallback_workspace) + assert envelope["error"]["details"]["workspace_type"] == "default" + assert envelope["error"]["hint"] + + +def test_which_errors_for_a_missing_specified_workspace_even_with_desktop_installed( + comfy_desktop, fallback_workspace, tmp_path +): + missing = tmp_path / "typo" / "ComfyUI" + + result, envelope = _invoke("--json", "--workspace", str(missing), "which") + + assert result.exit_code == 1 + assert envelope["error"]["code"] == "workspace_not_found" + assert envelope["error"]["details"]["path"] == str(missing) + assert envelope["error"]["details"]["workspace_type"] == "specified" + + +def test_which_keeps_an_installed_default_workspace_over_desktop(comfy_desktop, fallback_workspace): + fallback_workspace.mkdir(parents=True) + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 0, result.output + assert envelope["data"]["workspace_path"] == str(fallback_workspace) + assert envelope["data"]["workspace_type"] == "default" + assert envelope["data"]["exists"] is True + assert "desktop" not in envelope["data"] + + +def test_which_names_unreadable_desktop_records_in_the_error(comfy_desktop, fallback_workspace): + (comfy_desktop["app"] / "installations.json").write_text("{not json") + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 1 + assert envelope["error"]["code"] == "workspace_not_found" + problems = envelope["error"]["details"]["desktop"]["problems"] + assert len(problems) == 1 + assert "installations.json" in problems[0] + + +def test_which_warns_when_desktop_settings_are_unreadable(comfy_desktop, fallback_workspace): + (comfy_desktop["app"] / "settings.json").write_text("[broken") + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 1, result.output + assert envelope["error"]["code"] == "workspace_not_found" + desktop = envelope["error"]["details"]["desktop"] + assert any("settings.json" in problem for problem in desktop["problems"]) + + +def test_pretty_which_shows_the_desktop_data_dir(comfy_desktop, fallback_workspace): + result = CliRunner().invoke( + app, + ["--workspace", str(comfy_desktop["code"]), "which"], + env={"COMFY_OUTPUT": "pretty", "COLUMNS": "400"}, + ) + + assert result.exit_code == 0, result.output + assert str(comfy_desktop["data"]) in result.output + assert "Desktop data" in result.output + assert "specified" in result.output + + +def test_env_reports_the_desktop_install(comfy_desktop, fallback_workspace): + result, envelope = _invoke("--json", "env") + + assert result.exit_code == 0, result.output + data = envelope["data"] + jsonschema.validate(data, _schema("env.json")) + workspace = data["workspace"] + assert workspace["type"] == workspace_manager.workspace_type.value == "default" + assert workspace["path"] == workspace_manager.workspace_path == str(fallback_workspace) + assert workspace["exists"] is False + assert workspace["desktop"]["data_dir"] == str(comfy_desktop["data"]) + assert [w["code"] for w in data["warnings"]] == ["workspace_not_found"] + + +def test_env_flags_a_missing_workspace_instead_of_a_silent_default(fallback_workspace): + result, envelope = _invoke("--json", "env") + + assert result.exit_code == 0, result.output + data = envelope["data"] + jsonschema.validate(data, _schema("env.json")) + assert data["workspace"]["path"] == str(fallback_workspace) + assert data["workspace"]["exists"] is False + assert [w["code"] for w in data["warnings"]] == ["workspace_not_found"] + assert str(fallback_workspace) in data["warnings"][0]["message"] + + +@pytest.mark.parametrize("part", ["code", "data"]) +def test_which_enriches_an_explicit_desktop_workspace(comfy_desktop, fallback_workspace, part): + result, envelope = _invoke("--json", "--workspace", str(comfy_desktop[part]), "which") + + assert result.exit_code == 0, result.output + jsonschema.validate(envelope["data"], _schema("which.json")) + assert envelope["data"]["workspace_type"] == "specified" + assert envelope["data"]["workspace_path"] == str(comfy_desktop[part]) + assert envelope["data"]["desktop"]["data_dir"] == str(comfy_desktop["data"]) + assert envelope["data"]["desktop"]["code_dir"] == str(comfy_desktop["code"]) + + +def test_env_warns_when_desktop_settings_are_unreadable(comfy_desktop, fallback_workspace): + (comfy_desktop["app"] / "settings.json").write_text("[broken") + + result, envelope = _invoke("--json", "env") + + assert result.exit_code == 0, result.output + assert [w["code"] for w in envelope["data"]["warnings"]] == ["workspace_not_found", "desktop_config_unreadable"] + + +def test_env_renders_hostile_discovery_values_literally_without_changing_json( + comfy_desktop, fallback_workspace, monkeypatch +): + raw = "[/]literal[red]x[/red]\x1b[2J\x1b]0;osc-title\x07\x9b2J\r\u202e世界" + visible = "[/]literal[red]x[/red]\u202e世界" + selected_path = str(fallback_workspace.parent / raw) + monkeypatch.setattr(utils, "get_not_user_set_default_workspace", lambda: selected_path) + found = desktop_install.detect() + candidate = replace( + found.selected, + code_dir=f"code:{raw}", + data_dir=f"data:{raw}", + model_roots=(desktop_install.ModelRoot(f"model:{raw}", "data", True),), + ) + found = replace(found, installs=(candidate,), selected=candidate, problems=(f"warning:{raw}",)) + monkeypatch.setattr(desktop_install, "detect", lambda: found) + monkeypatch.setattr(EnvChecker(), "fill_print_table", lambda: []) + + json_result, envelope = _invoke("--json", "env") + assert json_result.exit_code == 0, json_result.output + workspace = envelope["data"]["workspace"] + assert workspace["path"] == selected_path + assert workspace["desktop"]["code_dir"] == f"code:{raw}" + assert workspace["desktop"]["data_dir"] == f"data:{raw}" + assert workspace["desktop"]["model_roots"][0]["path"] == f"model:{raw}" + assert any(warning["message"] == f"warning:{raw}" for warning in envelope["data"]["warnings"]) + + result = CliRunner().invoke(app, ["env"], env={"COMFY_OUTPUT": "pretty", "COLUMNS": "600", "NO_COLOR": "1"}) + + assert result.exit_code == 0, result.output + assert str(fallback_workspace.parent / visible) in result.output + assert f"code:{visible}" in result.output + assert f"data:{visible}" in result.output + assert f"model:{visible}" in result.output + assert f"warning:{visible}" in result.output + assert "\x1b" not in result.output and "\x9b" not in result.output + assert "\x07" not in result.output and "\r" not in result.output + assert "osc-title" not in result.output + assert "\u202e世界" in result.output + + +def test_which_renders_hostile_desktop_fields_literally_without_changing_json( + comfy_desktop, fallback_workspace, monkeypatch +): + raw = "[/]literal[red]x[/red]\x1b[2J\x1b]0;osc-title\x07\x9b2J\r\u202e世界" + visible = "[/]literal[red]x[/red]\u202e世界" + code = fallback_workspace.parent / "selected-comfy" + desktop_code = fallback_workspace.parent / f"code:{raw}" + code.mkdir(parents=True) + (code / "main.py").write_text("") + found = desktop_install.detect() + candidate = replace( + found.selected, + code_dir=str(desktop_code), + data_dir=f"data:{raw}", + python=f"python:{raw}", + model_roots=(desktop_install.ModelRoot(f"model:{raw}", "data", True),), + ) + found = replace( + found, installs=(candidate,), selected=candidate, selected_reason=f"reason:{raw}", problems=(f"warning:{raw}",) + ) + monkeypatch.setattr(desktop_install, "detect", lambda: found) + monkeypatch.setattr(desktop_install, "install_for_dir", lambda path, detected: {str(code): candidate}.get(path)) + monkeypatch.setattr("comfy_cli.cmdline.env_checker.check_comfy_server_running", lambda **kwargs: False) + + json_result, envelope = _invoke("--json", "--workspace", str(code), "which") + assert json_result.exit_code == 0, json_result.output + data = envelope["data"] + assert data["workspace_path"] == str(code) + assert data["desktop"]["code_dir"] == str(desktop_code) + assert data["desktop"]["data_dir"] == f"data:{raw}" + assert data["desktop"]["python"] == f"python:{raw}" + assert data["desktop"]["model_roots"][0]["path"] == f"model:{raw}" + assert data["desktop"]["selected_reason"] == f"reason:{raw}" + assert data["warnings"][0]["message"] == f"warning:{raw}" + + result = CliRunner().invoke( + app, ["--workspace", str(code), "which"], env={"COMFY_OUTPUT": "pretty", "COLUMNS": "600", "NO_COLOR": "1"} + ) + + assert result.exit_code == 0, result.output + assert str(fallback_workspace.parent / f"code:{visible}") in result.output + assert f"data:{visible}" in result.output + assert f"python:{visible}" in result.output + assert f"model:{visible}" in result.output + assert f"reason:{visible}" in result.output + assert f"warning:{visible}" in result.output + assert "\x1b" not in result.output and "\x9b" not in result.output + assert "\x07" not in result.output and "\r" not in result.output + assert "osc-title" not in result.output + assert "\u202e世界" in result.output diff --git a/tests/comfy_cli/test_workspace_manager.py b/tests/comfy_cli/test_workspace_manager.py index b5bd3aa67..49a615089 100644 --- a/tests/comfy_cli/test_workspace_manager.py +++ b/tests/comfy_cli/test_workspace_manager.py @@ -721,17 +721,19 @@ def test_no_workspace_probe(self, command): result = self._invoke(["--json", *command]) assert not isinstance(result.exception, AssertionError), result.exception - def test_which_still_resolves_the_workspace(self): + def test_which_still_resolves_the_workspace(self, tmp_path): from typer.testing import CliRunner import comfy_cli.workspace_manager as workspace_manager_module from comfy_cli.cmdline import app - with patch.object(workspace_manager_module, "check_comfy_repo", return_value=(True, "/sentinel/ComfyUI")): + workspace = tmp_path / "ComfyUI" + workspace.mkdir() + with patch.object(workspace_manager_module, "check_comfy_repo", return_value=(True, str(workspace))): result = CliRunner().invoke(app, ["--json", "which"]) assert result.exit_code == 0, result.output - assert "/sentinel/ComfyUI" in result.output + assert str(workspace) in result.output def test_gitpython_is_not_imported(self): import subprocess From c7e8c39b474070a4e2ae37072b1f1a8cbb7d8b62 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 19:54:21 -0500 Subject: [PATCH 2/6] fix(discovery): read Linux settings from XDG config, contain path errors, keep which exit 0 - Linux Desktop keeps settings.json under XDG_CONFIG_HOME and installations.json under XDG_DATA_HOME; read settings from the config folder of the same name. - Filesystem probes on Desktop paths use os.path and stat checks that report permission errors as problems instead of raising; each registry folder and the running-server pass have one error boundary. - Running-server roots follow ComfyUI: the checkout's extra_model_paths.yaml is read before --extra-model-paths-config, and --models-directory replaces the data dir's models/. - comfy which keeps exit 0 and ok: true for a missing workspace (envelope/1); exists: false and a workspace_not_found warning report it. --- CHANGELOG.md | 4 +- comfy_cli/cmdline.py | 13 +- comfy_cli/desktop_install.py | 249 +++++++++++++----- comfy_cli/error_codes.py | 7 +- ...ERY-0006-preserve-runtime-path-identity.md | 21 +- tests/comfy_cli/test_desktop_install.py | 158 +++++++++++ tests/comfy_cli/test_which_desktop.py | 93 +++++-- 7 files changed, 423 insertions(+), 122 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4170b701..0dd3cc511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,8 +65,8 @@ history. ### Fixed -- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. -- `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. A nonexistent selected workspace no longer succeeds in `which`; discovery reports malformed Desktop configuration and marks inferred data roots as unverified. +- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML (the checkout's own `extra_model_paths.yaml` first, then `--extra-model-paths-config`), `--models-directory` and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. +- `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. On Linux, shared model roots are read from `settings.json` under `XDG_CONFIG_HOME`, where Desktop keeps it. A nonexistent selected workspace still exits 0 but reports `exists: false` and a `workspace_not_found` warning; discovery reports malformed or unreadable Desktop configuration as `desktop_config_unreadable` warnings and marks inferred data roots as unverified. - UI→API conversion expands a `COMFY_DYNAMICCOMBO_V3` selector's sub-inputs in the order the workflow was SAVED with, when the saved graph records one, diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index caadde0f6..698c04f78 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -2786,17 +2786,8 @@ def which(): raise typer.Exit(code=1) workspace_type_value = workspace["type"] - if not workspace["exists"]: - details = {"path": comfy_path, "workspace_type": workspace_type_value} - if "desktop" in workspace: - details["desktop"] = workspace["desktop"] - renderer.error( - code="workspace_not_found", - message=f"Workspace directory does not exist: {comfy_path}", - hint="run: comfy install (or pass --workspace /path/to/ComfyUI)", - details=details, - ) - raise typer.Exit(code=1) + # A missing directory stays a success (exit 0, as in envelope/1 before): + # ``exists: false`` and a ``workspace_not_found`` warning say so. if renderer.is_pretty(): import sys as _sys diff --git a/comfy_cli/desktop_install.py b/comfy_cli/desktop_install.py index 789af470c..3c663202c 100644 --- a/comfy_cli/desktop_install.py +++ b/comfy_cli/desktop_install.py @@ -12,8 +12,11 @@ ``useSharedModels: false``) plus the install's own ``modelDirs``. Everything is read from files Desktop maintains in its app-data dir -(``installations.json``, ``settings.json``). Nothing here starts Desktop, -talks to a server, or writes a file. +(``installations.json``, ``settings.json``). On Linux Desktop splits that dir +per XDG: ``installations.json`` under ``XDG_DATA_HOME`` and ``settings.json`` +under ``XDG_CONFIG_HOME``. Nothing here starts Desktop, talks to a server, or +writes a file, and a path that cannot be checked is reported as a problem +rather than raised. """ from __future__ import annotations @@ -21,6 +24,7 @@ import json import os import re +import stat from dataclasses import dataclass, field, replace from datetime import datetime, timezone from pathlib import Path @@ -51,6 +55,22 @@ def desktop_app_data_dir() -> Path | None: return Path(base) / APP_DIR_NAME +def desktop_settings_dirs(registry_dir: Path) -> tuple[Path, ...]: + """Where ``settings.json`` for the registry at ``registry_dir`` may live, in order. + + On Linux Desktop keeps ``installations.json`` under ``XDG_DATA_HOME`` and + ``settings.json`` under ``XDG_CONFIG_HOME``, in a folder of the same name. + Everywhere else, and for a registry outside the XDG data dir, both files + share one folder. + """ + if get_os() not in (OS.MACOS, OS.WINDOWS): + data_base = os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share") + if _same_path(str(registry_dir.parent), data_base): + config_base = os.environ.get("XDG_CONFIG_HOME") or str(Path.home() / ".config") + return (Path(config_base) / registry_dir.name, registry_dir) + return (registry_dir,) + + def legacy_config_dir() -> Path | None: """Desktop 1's app-data dir (``config.json`` → ``basePath``); none on Linux.""" current = get_os() @@ -162,8 +182,33 @@ def to_dict(self) -> dict[str, Any]: def _same_path(a: str, b: str) -> bool: try: return os.path.samefile(a, b) - except OSError: + except (OSError, ValueError): + pass + try: return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b)) + except (OSError, ValueError): + return os.path.normcase(os.path.abspath(a)) == os.path.normcase(os.path.abspath(b)) + + +# ``Path.is_file()`` / ``is_dir()`` raise on a permission error (EACCES) on +# Python 3.10; ``os.path`` returns False. Every probe of a path taken from +# Desktop's records or a server's argv goes through these. +def _is_file(path: str | os.PathLike[str]) -> bool: + return os.path.isfile(path) + + +def _is_dir(path: str | os.PathLike[str]) -> bool: + return os.path.isdir(path) + + +def _regular_file(path: Path) -> bool | OSError: + """True for a regular file, False when absent, or the error when it cannot be checked.""" + try: + return stat.S_ISREG(path.stat().st_mode) + except (FileNotFoundError, NotADirectoryError): + return False + except OSError as error: + return error def _read_json(path: Path) -> Any: @@ -173,7 +218,7 @@ def _read_json(path: Path) -> Any: def _venv_python(venv: Path) -> Path | None: for candidate in (venv / "bin" / "python3", venv / "bin" / "python", venv / "Scripts" / "python.exe"): - if candidate.is_file(): + if _is_file(candidate): return candidate return None @@ -182,7 +227,7 @@ def _code_dir(install_path: Path) -> Path: """Mirror Desktop's ``resolveComfyDir``: ``/ComfyUI``, or the install path itself when the checkout sits there directly.""" nested = install_path / "ComfyUI" - if not nested.is_dir() and (install_path / "main.py").is_file(): + if not _is_dir(nested) and _is_file(install_path / "main.py"): return install_path return nested @@ -219,7 +264,7 @@ def _model_roots(data_dir: Path, record: dict, shared_dirs: list[str]) -> tuple[ for path, source in candidates: if any(_same_path(path, root.path) for root in roots): continue - roots.append(ModelRoot(path=path, source=source, exists=os.path.isdir(path))) + roots.append(ModelRoot(path=path, source=source, exists=_is_dir(path))) return tuple(roots) @@ -239,7 +284,7 @@ def _install_from_record( python: Path | None = None adopted_python = record.get("adoptedPythonPath") if adopted else None - if isinstance(adopted_python, str) and Path(adopted_python).is_file(): + if isinstance(adopted_python, str) and _is_file(adopted_python): python = Path(adopted_python) if python is None: python = _venv_python(data_dir / ".venv") @@ -262,7 +307,7 @@ def _install_from_record( adopted=adopted, status=record.get("status") if isinstance(record.get("status"), str) else None, last_launched_ms=last_launched if isinstance(last_launched, (int, float)) and last_launched > 0 else None, - exists=(code_dir / "main.py").is_file(), + exists=_is_file(code_dir / "main.py"), registry_dir=str(registry_dir) if registry_dir is not None else None, ) @@ -320,7 +365,13 @@ def server_launch_paths(stats: dict[str, Any]) -> dict[str, list[str]]: return {} return { name: values - for name in ("base_directory", "input_directory", "output_directory", "extra_model_paths_config") + for name in ( + "base_directory", + "models_directory", + "input_directory", + "output_directory", + "extra_model_paths_config", + ) if (values := _argv_values(argv, "--" + name.replace("_", "-"))) } @@ -341,6 +392,48 @@ def _config_identifies_install(config: str, install: DesktopInstall) -> bool: return _same_path(str(path), str(expected)) +def _add_config_roots(config_path: Path, roots: list[ModelRoot], problems: list[str]) -> None: + """Append the model roots an ``extra_model_paths.yaml`` adds, the way ComfyUI reads it.""" + try: + import yaml + + contents = yaml.safe_load(config_path.read_text(encoding="utf-8")) + if not isinstance(contents, dict): + raise ValueError("expected a mapping") + for row in contents.values(): + if not isinstance(row, dict): + continue + base = row.get("base_path") + base_path = None + if isinstance(base, str) and base: + base_path = Path(os.path.expandvars(base)).expanduser() + if not base_path.is_absolute(): + base_path = config_path.parent / base_path + base_path = base_path.resolve() + paths = [base_path] if base_path is not None else [] + for kind, value in row.items(): + if kind in ("base_path", "is_default"): + continue + if not isinstance(value, str): + raise ValueError(f"{kind}: expected newline-separated model paths") + for entry in value.splitlines(): + if not entry: + continue + path = Path(entry) + if not path.is_absolute(): + path = (base_path or config_path.parent) / path + path = path.resolve() + # A base covers its descendant type folders. Keep a + # separate root for absolute paths outside that base. + if base_path is None or not path.is_relative_to(base_path): + paths.append(path) + for path in paths: + if not any(_same_path(str(path), root.path) for root in roots): + roots.append(ModelRoot(str(path), "install", _is_dir(path))) + except (OSError, ValueError, yaml.YAMLError) as error: + problems.append(f"{config_path}: {error}") + + def _for_server( found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool = False ) -> DesktopDetection: @@ -384,9 +477,10 @@ def _for_server( ) chosen = matches[0] outputs = _argv_values(argv, "--output-directory") + models_dirs = _argv_values(argv, "--models-directory") if any( not Path(path).expanduser().is_absolute() - for path in (*bases, *_argv_values(argv, "--input-directory"), *outputs) + for path in (*bases, *models_dirs, *_argv_values(argv, "--input-directory"), *outputs) ): return replace( found, @@ -394,56 +488,29 @@ def _for_server( selected_reason=None, problems=( *found.problems, - "running server uses relative data/input/output paths; verify its working directory", + "running server uses relative data/models/input/output paths; verify its working directory", ), ) data_dir = str(Path(bases[-1]).expanduser()) if bases else chosen.data_dir + # ComfyUI's own models dir: ``--models-directory`` replaces ``/models``. + models_dir = str(Path(models_dirs[-1]).expanduser()) if models_dirs else str(Path(data_dir) / "models") roots = [root for root in chosen.model_roots if root.source != "data"] if not configs else [] - roots.insert(0, ModelRoot(str(Path(data_dir) / "models"), "data", (Path(data_dir) / "models").is_dir())) + roots.insert(0, ModelRoot(models_dir, "data", _is_dir(models_dir))) problems = list(found.problems) + # ComfyUI loads ``extra_model_paths.yaml`` beside its main.py before any + # ``--extra-model-paths-config`` file. + local_config = Path(os.path.realpath(chosen.code_dir)) / "extra_model_paths.yaml" + local_state = _regular_file(local_config) + if isinstance(local_state, OSError): + problems.append(f"{local_config}: {local_state}") + elif local_state: + _add_config_roots(local_config, roots, problems) for config in configs: config_path = Path(config).expanduser() if not config_path.is_absolute(): problems.append(f"running server model config is relative: {config}; verify its working directory") continue - try: - import yaml - - contents = yaml.safe_load(config_path.read_text(encoding="utf-8")) - if not isinstance(contents, dict): - raise ValueError("expected a mapping") - for row in contents.values(): - if not isinstance(row, dict): - continue - base = row.get("base_path") - base_path = None - if isinstance(base, str) and base: - base_path = Path(os.path.expandvars(base)).expanduser() - if not base_path.is_absolute(): - base_path = config_path.parent / base_path - base_path = base_path.resolve() - paths = [base_path] if base_path is not None else [] - for kind, value in row.items(): - if kind in ("base_path", "is_default"): - continue - if not isinstance(value, str): - raise ValueError(f"{kind}: expected newline-separated model paths") - for entry in value.splitlines(): - if not entry: - continue - path = Path(entry) - if not path.is_absolute(): - path = (base_path or config_path.parent) / path - path = path.resolve() - # A base covers its descendant type folders. Keep a - # separate root for absolute paths outside that base. - if base_path is None or not path.is_relative_to(base_path): - paths.append(path) - for path in paths: - if not any(_same_path(str(path), root.path) for root in roots): - roots.append(ModelRoot(str(path), "install", path.is_dir())) - except (OSError, ValueError, yaml.YAMLError) as error: - problems.append(f"{config}: {error}") + _add_config_roots(config_path, roots, problems) python = system.get("python") version = system.get("comfyui_version") chosen = replace( @@ -451,7 +518,7 @@ def _for_server( comfyui_version=_as_tag(version) if isinstance(version, str) and version else chosen.comfyui_version, data_dir=data_dir, model_roots=tuple(roots), - python=python if isinstance(python, str) and Path(python).is_file() else None, + python=python if isinstance(python, str) and _is_file(python) else None, ) inputs = _argv_values(argv, "--input-directory") input_dir = str(Path(inputs[-1]).expanduser()) if inputs else str(Path(data_dir) / "input") @@ -467,21 +534,41 @@ def _for_server( ) +def _with_server(found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool) -> DesktopDetection: + try: + return _for_server(found, stats, filesystem_verified=filesystem_verified) + except (OSError, ValueError) as error: + return replace( + found, + selected=None, + selected_reason=None, + input_dir=None, + output_dir=None, + output_dir_source=None, + problems=(*found.problems, f"running server paths could not be checked: {error}"), + ) + + def detect( app_data_dir: Path | None = None, legacy_dir: Path | None = None, *, + settings_dir: Path | None = None, server_stats: dict[str, Any] | None = None, server_filesystem_verified: bool = False, ) -> DesktopDetection | None: """Read Desktop 2's install records. ``None`` means Desktop 2 is not installed. The app-data dirs default to this OS's; tests pass fixture dirs instead. + ``settings_dir`` is where ``settings.json`` lives; by default it is found + from ``app_data_dir`` (see :func:`desktop_settings_dirs`). Server paths require caller-established local process/filesystem provenance; matching strings or a loopback URL alone are insufficient. """ if app_data_dir is None: - detections = [found for directory in app_data_dirs() if (found := detect(directory, legacy_dir)) is not None] + detections = [ + found for directory in app_data_dirs() if (found := _detect_dir(directory, legacy_dir)) is not None + ] if not detections: return None installs = tuple(install for found in detections for install in found.installs) @@ -492,16 +579,29 @@ def detect( found = DesktopDetection( origin, installs, selected, reason, tuple(problem for item in detections for problem in item.problems) ) - return ( - _for_server(found, server_stats, filesystem_verified=server_filesystem_verified) - if server_stats is not None - else found - ) - app_dir = app_data_dir - if app_dir is None: - return None + else: + found = _detect_dir(app_data_dir, legacy_dir, settings_dir) + if found is None: + return None + if server_stats is None: + return found + return _with_server(found, server_stats, filesystem_verified=server_filesystem_verified) + + +def _detect_dir(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | None = None) -> DesktopDetection | None: + """One registry folder. A filesystem error becomes a problem, never an exception.""" + try: + return _read_registry(app_dir, legacy_dir, settings_dir) + except (OSError, ValueError) as error: + return DesktopDetection(app_data_dir=str(app_dir), problems=(f"{app_dir}: {error}",)) + + +def _read_registry(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | None) -> DesktopDetection | None: installations_file = app_dir / "installations.json" - if not installations_file.is_file(): + state = _regular_file(installations_file) + if isinstance(state, OSError): + return DesktopDetection(app_data_dir=str(app_dir), problems=(f"{installations_file}: {state}",)) + if not state: return None problems: list[str] = [] @@ -515,24 +615,34 @@ def detect( ) shared_dirs: list[str] = [] - settings_file = app_dir / "settings.json" - if settings_file.is_file(): + for directory in (settings_dir,) if settings_dir is not None else desktop_settings_dirs(app_dir): + settings_file = directory / "settings.json" + state = _regular_file(settings_file) + if state is False: + continue try: + if isinstance(state, OSError): + raise state settings = _read_json(settings_file) shared_dirs = _str_list(settings.get("modelsDirs")) if isinstance(settings, dict) else [] except (OSError, UnicodeDecodeError, ValueError) as error: problems.append(f"{settings_file}: {error} (shared model roots are missing from model_roots)") + break legacy_base_path: str | None = None legacy = legacy_dir if legacy_dir is not None else legacy_config_dir() - if legacy is not None and (legacy / "config.json").is_file(): + legacy_file = legacy / "config.json" if legacy is not None else None + state = _regular_file(legacy_file) if legacy_file is not None else False + if state is not False: try: - config = _read_json(legacy / "config.json") + if isinstance(state, OSError): + raise state + config = _read_json(legacy_file) base = config.get("basePath") if isinstance(config, dict) else None legacy_base_path = base if isinstance(base, str) and base else None except (OSError, UnicodeDecodeError, ValueError) as error: legacy_base_path = None - problems.append(f"{legacy / 'config.json'}: {error}") + problems.append(f"{legacy_file}: {error}") if legacy_base_path is None and any( isinstance(record, dict) @@ -555,18 +665,13 @@ def detect( if install is not None ) selected, reason = _select(installs) - found = DesktopDetection( + return DesktopDetection( app_data_dir=str(app_dir), installs=installs, selected=selected, selected_reason=reason, problems=tuple(problems), ) - return ( - _for_server(found, server_stats, filesystem_verified=server_filesystem_verified) - if server_stats is not None - else found - ) def install_for_dir(path: str | os.PathLike[str], detection: DesktopDetection | None = None) -> DesktopInstall | None: diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index 7d23e115a..c20dc73a9 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -67,12 +67,15 @@ class ErrorCode: ), ErrorCode( "workspace_not_found", - "The workspace reported by `comfy which` does not exist on disk; details.path names the missing directory.", + "The workspace `comfy which` / `comfy env` report does not exist on disk (`exists: false`); the message " + "names the missing directory. Surfaced in `data.warnings[]` (not as an error envelope) so the command " + "still succeeds.", "run `comfy install`, or pass `--workspace /path/to/ComfyUI`", ), ErrorCode( "desktop_config_unreadable", - "Comfy Desktop installation records or settings could not be read; discovery may be incomplete.", + "Comfy Desktop installation records or settings could not be read; discovery may be incomplete. Surfaced " + "in `data.warnings[]` (not as an error envelope) so the command still succeeds.", "check the Desktop configuration file named in the warning", ), # --- launch / stop lifecycle --------------------------------------------- diff --git a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md index a7d30de29..12e7c01f5 100644 --- a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md +++ b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md @@ -23,7 +23,9 @@ files while reporting the wrong version. - Discover modern `comfyui-desktop-2`, `Comfy Desktop`, and supported legacy records. Keep each registry's model-root provenance with its installation; - explicit caller/test registry overrides remain isolated. + explicit caller/test registry overrides remain isolated. On Linux, read + `settings.json` from the `XDG_CONFIG_HOME` folder of the same name as the + `XDG_DATA_HOME` registry, where Desktop writes it. - Without running-server evidence, describe selection as a registry-derived candidate and expose its selection reason. Do not claim it identifies the running process. @@ -48,16 +50,23 @@ files while reporting the wrong version. output fields. Zero or conflicting matches leave selection unresolved instead of combining unrelated installations. - Derive effective code/data/model/input/output roots from that selected process. - Resolve its model-path configuration and retain external paths explicitly. + Resolve its model-path configuration the way ComfyUI loads it: the checkout's + own `extra_model_paths.yaml` first, then each `--extra-model-paths-config`; + `--models-directory` replaces the data dir's `models/`. Retain external paths + explicitly. Report the running version when supplied. The output root is the explicit launch path or that process's data/output default, with its source recorded. -- Relative data/input/output paths cannot be resolved without the process's cwd: +- Relative data/models/input/output paths cannot be resolved without the process's cwd: retain uncertainty rather than anchoring them to the agent's cwd. Relative or unreadable model config paths remain diagnostic evidence, never a claim that all required model folders were found. -- Surface missing directories with existence flags and warnings. Discovery is - read-only: it creates no default workspace, installs nothing and grants no - permission to inspect/upload every file under a shared root. +- Surface missing directories with existence flags and warnings. A missing + selected workspace leaves `which`'s exit code and `ok: true` envelope as they + were under envelope/1; `exists: false` and a `workspace_not_found` warning + report it. A Desktop path that cannot be checked (for example, permission + denied) is a warning, never a failed command. Discovery is read-only: it + creates no default workspace, installs nothing and grants no permission to + inspect/upload every file under a shared root. - Optional which/env discovery fields remain additive. Runtime launch reporting contains known path flags only, not arbitrary argv values or credentials. File-upload scope and approvals belong to the calling operation; discovering diff --git a/tests/comfy_cli/test_desktop_install.py b/tests/comfy_cli/test_desktop_install.py index 3d069bd29..a2837a312 100644 --- a/tests/comfy_cli/test_desktop_install.py +++ b/tests/comfy_cli/test_desktop_install.py @@ -8,6 +8,8 @@ from __future__ import annotations import json +import os +import sys from pathlib import Path import pytest @@ -613,3 +615,159 @@ def test_generated_config_identity_accepts_verified_file_alias(tmp_path): server_filesystem_verified=True, ) assert found.selected.install_id == "live" + + +def test_linux_settings_are_read_from_xdg_config_beside_xdg_data_registry(tmp_path, monkeypatch): + """Desktop on Linux: installations.json under XDG_DATA_HOME, settings.json under XDG_CONFIG_HOME.""" + monkeypatch.setattr(desktop_install, "get_os", lambda: OS.LINUX) + monkeypatch.setattr(desktop_install, "desktop_app_data_dir", _real_app_data_dir) + data_home, config_home = tmp_path / "xdg-data", tmp_path / "xdg-config" + monkeypatch.setenv("XDG_DATA_HOME", str(data_home)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(config_home)) + code = _checkout(tmp_path / "install" / "ComfyUI") + registry = data_home / "comfyui-desktop-2" + registry.mkdir(parents=True) + (registry / "installations.json").write_text(json.dumps([{"id": "linux", "installPath": str(code.parent)}])) + shared = tmp_path / "shared-models" + (config_home / "comfyui-desktop-2").mkdir(parents=True) + (config_home / "comfyui-desktop-2" / "settings.json").write_text(json.dumps({"modelsDirs": [str(shared)]})) + + found = desktop_install.detect() + + assert found.selected.install_id == "linux" + assert [(root.path, root.source) for root in found.selected.model_roots] == [ + (str(code / "models"), "data"), + (str(shared), "shared"), + ] + assert not found.problems + + +def test_settings_dir_is_the_registry_dir_off_linux_and_outside_xdg_data(tmp_path, monkeypatch): + registry = tmp_path / "comfyui-desktop-2" + monkeypatch.setattr(desktop_install, "get_os", lambda: OS.MACOS) + assert desktop_install.desktop_settings_dirs(registry) == (registry,) + monkeypatch.setattr(desktop_install, "get_os", lambda: OS.LINUX) + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "elsewhere")) + assert desktop_install.desktop_settings_dirs(registry) == (registry,) + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) + assert desktop_install.desktop_settings_dirs(registry) == (tmp_path / "config" / "comfyui-desktop-2", registry) + + +def test_denied_registry_file_is_a_problem_not_a_crash(tmp_path, monkeypatch): + app = _write_desktop(tmp_path / "Comfy Desktop", []) + real_stat = Path.stat + + def stat(self, *args, **kwargs): + if self.name == "installations.json": + raise PermissionError(13, "Permission denied", str(self)) + return real_stat(self, *args, **kwargs) + + monkeypatch.setattr(Path, "stat", stat) + + found = desktop_install.detect(app, tmp_path / "legacy") + + assert found is not None + assert found.selected is None + assert len(found.problems) == 1 + assert "installations.json" in found.problems[0] and "Permission denied" in found.problems[0] + + +@pytest.mark.skipif(sys.platform == "win32" or os.geteuid() == 0, reason="needs POSIX permissions as non-root") +def test_untraversable_desktop_paths_are_contained(tmp_path): + """EACCES from a stat must not escape discovery (pathlib's is_file raises it on 3.10).""" + good = _checkout(tmp_path / "good" / "ComfyUI") + locked = tmp_path / "locked" + _checkout(locked / "install" / "ComfyUI") + settings_dir = tmp_path / "locked-settings" + settings_dir.mkdir() + (settings_dir / "settings.json").write_text("{}") + app = _write_desktop( + tmp_path / "Comfy Desktop", + [ + { + "id": "locked", + "installPath": str(locked / "install"), + "adopted": True, + "adoptedBaseDir": str(locked / "data"), + "adoptedPythonPath": str(locked / "python"), + }, + {"id": "good", "installPath": str(good.parent)}, + ], + ) + locked.chmod(0) + settings_dir.chmod(0) + try: + found = desktop_install.detect(app, tmp_path / "legacy", settings_dir=settings_dir) + server = desktop_install.detect( + app, + tmp_path / "legacy", + server_stats={"system": {"argv": [str(good / "main.py"), "--models-directory", str(locked / "models")]}}, + server_filesystem_verified=True, + ) + finally: + locked.chmod(0o755) + settings_dir.chmod(0o755) + + assert found.selected.install_id == "good" + assert {install.install_id: install.exists for install in found.installs} == {"locked": False, "good": True} + assert any("settings.json" in problem for problem in found.problems) + assert server.selected.install_id == "good" + assert server.selected.model_roots[0].exists is False + + +def test_running_server_reads_the_checkout_extra_model_paths_yaml_first(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + local_root = tmp_path / "local-models" + (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {local_root}\n checkpoints: checkpoints\n") + explicit = tmp_path / "explicit.yaml" + explicit.write_text(f"explicit:\n base_path: {tmp_path / 'explicit-models'}\n loras: loras\n") + + only_local = desktop_install.detect( + app, server_stats={"system": {"argv": [str(code / "main.py")]}}, server_filesystem_verified=True + ) + both = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(explicit)]}}, + server_filesystem_verified=True, + ) + + assert (str(local_root), "install") in [(root.path, root.source) for root in only_local.selected.model_roots] + assert [root.path for root in both.selected.model_roots] == [ + str(code / "models"), + str(local_root), + str(tmp_path / "explicit-models"), + ] + assert not both.problems + + +def test_running_server_models_directory_replaces_the_data_models_root(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + external = tmp_path / "external" / "models" + external.mkdir(parents=True) + argv = [str(code / "main.py"), "--models-directory", str(external)] + + found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}, server_filesystem_verified=True) + + roots = [(root.path, root.source, root.exists) for root in found.selected.model_roots] + assert roots[0] == (str(external), "data", True) + assert str(code / "models") not in [path for path, _, _ in roots] + assert desktop_install.server_launch_paths({"system": {"argv": [*argv, "--api-key", "secret"]}}) == { + "models_directory": [str(external)] + } + + +def test_relative_models_directory_is_unverified(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) + + found = desktop_install.detect( + app, + server_stats={"system": {"argv": [str(code / "main.py"), "--models-directory", "models"]}}, + server_filesystem_verified=True, + ) + + assert found.selected is None + assert any("relative" in problem for problem in found.problems) diff --git a/tests/comfy_cli/test_which_desktop.py b/tests/comfy_cli/test_which_desktop.py index 24a24ee59..069aa4018 100644 --- a/tests/comfy_cli/test_which_desktop.py +++ b/tests/comfy_cli/test_which_desktop.py @@ -2,8 +2,9 @@ The default workspace comfy-cli falls back to (``~/Documents/comfy/ComfyUI``) does not exist on a Desktop-only machine. These commands used to report it with -``ok: true``; now a missing path is an error (`which`) or a flagged warning -(`env`), and a Desktop 2 install is reported with its real split layout. +``ok: true`` and nothing else; they still succeed (exit 0, envelope/1 unchanged) +but now say ``exists: false`` with a ``workspace_not_found`` warning, and a +Desktop 2 install is reported with its real split layout. """ from __future__ import annotations @@ -51,14 +52,16 @@ def _invoke(*args: str): def test_which_keeps_missing_selected_workspace_separate_from_desktop_candidate(comfy_desktop, fallback_workspace): result, envelope = _invoke("--json", "which") - assert result.exit_code == 1, result.output - assert envelope["ok"] is False - assert envelope["error"]["code"] == "workspace_not_found" - details = envelope["error"]["details"] - assert details["workspace_type"] == workspace_manager.workspace_type.value == "default" - assert details["path"] == workspace_manager.workspace_path == str(fallback_workspace) + assert result.exit_code == 0, result.output + assert envelope["ok"] is True + data = envelope["data"] + jsonschema.validate(data, _schema("which.json")) + assert data["workspace_type"] == workspace_manager.workspace_type.value == "default" + assert data["workspace_path"] == workspace_manager.workspace_path == str(fallback_workspace) + assert data["exists"] is False + assert [w["code"] for w in data["warnings"]] == ["workspace_not_found"] assert not Path(workspace_manager.workspace_path).is_dir() - desktop = details["desktop"] + desktop = data["desktop"] assert desktop["code_dir"] == str(comfy_desktop["code"]) assert desktop["data_dir"] == str(comfy_desktop["data"]) assert desktop["python"] == str(comfy_desktop["python"]) @@ -83,7 +86,7 @@ def test_which_lists_every_desktop_install_and_says_which_it_picked(comfy_deskto _, envelope = _invoke("--json", "which") - desktop = envelope["error"]["details"]["desktop"] + desktop = envelope["data"]["desktop"] assert desktop["install_id"] == "inst-adopted" assert "most recently launched of 2" in desktop["selected_reason"] assert {row["install_id"]: row["selected"] for row in desktop["installs"]} == { @@ -92,28 +95,41 @@ def test_which_lists_every_desktop_install_and_says_which_it_picked(comfy_deskto } -def test_which_errors_when_the_default_workspace_does_not_exist(fallback_workspace): +def test_which_still_succeeds_but_flags_a_missing_default_workspace(fallback_workspace): + """envelope/1 contract: same exit code and data keys as before, plus additive fields.""" result, envelope = _invoke("--json", "which") - assert result.exit_code == 1 - assert envelope["ok"] is False - assert envelope["error"]["code"] == "workspace_not_found" - assert envelope["error"]["details"]["path"] == str(fallback_workspace) - assert envelope["error"]["details"]["workspace_type"] == "default" - assert envelope["error"]["hint"] + assert result.exit_code == 0, result.output + assert envelope["ok"] is True + data = envelope["data"] + assert data["workspace_path"] == str(fallback_workspace) + assert data["workspace_type"] == "default" + assert data["exists"] is False + assert [w["code"] for w in data["warnings"]] == ["workspace_not_found"] + assert str(fallback_workspace) in data["warnings"][0]["message"] + assert "desktop" not in data -def test_which_errors_for_a_missing_specified_workspace_even_with_desktop_installed( +def test_which_flags_a_missing_specified_workspace_even_with_desktop_installed( comfy_desktop, fallback_workspace, tmp_path ): missing = tmp_path / "typo" / "ComfyUI" result, envelope = _invoke("--json", "--workspace", str(missing), "which") - assert result.exit_code == 1 - assert envelope["error"]["code"] == "workspace_not_found" - assert envelope["error"]["details"]["path"] == str(missing) - assert envelope["error"]["details"]["workspace_type"] == "specified" + assert result.exit_code == 0, result.output + data = envelope["data"] + assert data["workspace_path"] == str(missing) + assert data["workspace_type"] == "specified" + assert data["exists"] is False + assert data["warnings"][0]["code"] == "workspace_not_found" + + +def test_pretty_which_prints_the_missing_workspace_warning(fallback_workspace): + result = CliRunner().invoke(app, ["which"], env={"COMFY_OUTPUT": "pretty", "COLUMNS": "400"}) + + assert result.exit_code == 0, result.output + assert f"Workspace directory does not exist: {fallback_workspace}" in result.output def test_which_keeps_an_installed_default_workspace_over_desktop(comfy_desktop, fallback_workspace): @@ -128,14 +144,14 @@ def test_which_keeps_an_installed_default_workspace_over_desktop(comfy_desktop, assert "desktop" not in envelope["data"] -def test_which_names_unreadable_desktop_records_in_the_error(comfy_desktop, fallback_workspace): +def test_which_names_unreadable_desktop_records_in_the_output(comfy_desktop, fallback_workspace): (comfy_desktop["app"] / "installations.json").write_text("{not json") result, envelope = _invoke("--json", "which") - assert result.exit_code == 1 - assert envelope["error"]["code"] == "workspace_not_found" - problems = envelope["error"]["details"]["desktop"]["problems"] + assert result.exit_code == 0, result.output + assert [w["code"] for w in envelope["data"]["warnings"]] == ["workspace_not_found", "desktop_config_unreadable"] + problems = envelope["data"]["desktop"]["problems"] assert len(problems) == 1 assert "installations.json" in problems[0] @@ -145,9 +161,8 @@ def test_which_warns_when_desktop_settings_are_unreadable(comfy_desktop, fallbac result, envelope = _invoke("--json", "which") - assert result.exit_code == 1, result.output - assert envelope["error"]["code"] == "workspace_not_found" - desktop = envelope["error"]["details"]["desktop"] + assert result.exit_code == 0, result.output + desktop = envelope["data"]["desktop"] assert any("settings.json" in problem for problem in desktop["problems"]) @@ -302,3 +317,23 @@ def test_which_renders_hostile_desktop_fields_literally_without_changing_json( assert "\x07" not in result.output and "\r" not in result.output assert "osc-title" not in result.output assert "\u202e世界" in result.output + + +def test_which_reports_an_existing_workspace_when_the_desktop_registry_is_denied( + comfy_desktop, fallback_workspace, monkeypatch +): + fallback_workspace.mkdir(parents=True) + real_stat = Path.stat + + def stat(self, *args, **kwargs): + if self.name == "installations.json": + raise PermissionError(13, "Permission denied", str(self)) + return real_stat(self, *args, **kwargs) + + monkeypatch.setattr(Path, "stat", stat) + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 0, result.output + assert envelope["data"]["workspace_path"] == str(fallback_workspace) + assert envelope["data"]["exists"] is True From 4c162668ae335b9f372188cdf7a54c3d5c49fbc6 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 21:11:58 -0500 Subject: [PATCH 3/6] fix(discovery): read local model YAML from the running script, contain path errors, keep warnings out of the error registry - The extra_model_paths.yaml beside a running server's main.py is taken from the real directory of argv[0], as ComfyUI does with realpath(__file__), not from the recorded checkout. A script path that is not absolute leaves that file unresolved and says so. - One tuple of path errors (OSError, RuntimeError, ValueError) is caught at every discovery boundary, so a symlink loop or a missing home directory becomes a problem instead of a crash on Python 3.10. - workspace_not_found and desktop_config_unreadable are only ever warnings, so they leave the error-code registry and comfy discover. The registry test now tells warning objects from failures and stops new warning-only registrations; the older ones stay listed, since codes are append-only. --- CHANGELOG.md | 4 +- comfy_cli/desktop_install.py | 64 ++++-- comfy_cli/error_codes.py | 19 +- comfy_cli/schemas/error_codes.md | 7 +- ...ERY-0006-preserve-runtime-path-identity.md | 16 +- docs/json-output.md | 17 ++ .../output/test_error_code_registry.py | 190 +++++++++++++++--- tests/comfy_cli/test_desktop_install.py | 169 ++++++++++++++++ tests/comfy_cli/test_which_desktop.py | 19 ++ 9 files changed, 440 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd3cc511..64f22621c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,8 +65,8 @@ history. ### Fixed -- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML (the checkout's own `extra_model_paths.yaml` first, then `--extra-model-paths-config`), `--models-directory` and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. -- `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. On Linux, shared model roots are read from `settings.json` under `XDG_CONFIG_HOME`, where Desktop keeps it. A nonexistent selected workspace still exits 0 but reports `exists: false` and a `workspace_not_found` warning; discovery reports malformed or unreadable Desktop configuration as `desktop_config_unreadable` warnings and marks inferred data roots as unverified. +- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML (the `extra_model_paths.yaml` beside the `main.py` the process runs first, then `--extra-model-paths-config`), `--models-directory` and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. +- `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. On Linux, shared model roots are read from `settings.json` under `XDG_CONFIG_HOME`, where Desktop keeps it. A nonexistent selected workspace still exits 0 but reports `exists: false` and a `workspace_not_found` warning; discovery reports malformed or unreadable Desktop configuration, and Desktop paths that cannot be checked (permission denied, a symlink loop, no home directory), as `desktop_config_unreadable` warnings and marks inferred data roots as unverified. Both codes appear only in `data.warnings[]`; they are not error codes and `comfy discover` does not list them under `data.error_codes`. - UI→API conversion expands a `COMFY_DYNAMICCOMBO_V3` selector's sub-inputs in the order the workflow was SAVED with, when the saved graph records one, diff --git a/comfy_cli/desktop_install.py b/comfy_cli/desktop_install.py index 3c663202c..d7b64c1e1 100644 --- a/comfy_cli/desktop_install.py +++ b/comfy_cli/desktop_install.py @@ -16,7 +16,7 @@ per XDG: ``installations.json`` under ``XDG_DATA_HOME`` and ``settings.json`` under ``XDG_CONFIG_HOME``. Nothing here starts Desktop, talks to a server, or writes a file, and a path that cannot be checked is reported as a problem -rather than raised. +rather than raised (see :data:`_PATH_ERRORS`). """ from __future__ import annotations @@ -179,14 +179,23 @@ def to_dict(self) -> dict[str, Any]: return block +#: What checking a path from Desktop's records or a server's argv can raise: +#: ``OSError`` (permission denied, a missing parent), ``ValueError`` (an +#: embedded NUL) and ``RuntimeError`` (on Python 3.10, ``Path.resolve()`` on a +#: symlink loop and ``Path.expanduser()``/``Path.home()`` without a home +#: directory; ``RecursionError`` from a deeply nested file is one too). Every +#: boundary that turns a path failure into a problem catches exactly this. +_PATH_ERRORS = (OSError, RuntimeError, ValueError) + + def _same_path(a: str, b: str) -> bool: try: return os.path.samefile(a, b) - except (OSError, ValueError): + except _PATH_ERRORS: pass try: return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b)) - except (OSError, ValueError): + except _PATH_ERRORS: return os.path.normcase(os.path.abspath(a)) == os.path.normcase(os.path.abspath(b)) @@ -235,7 +244,7 @@ def _code_dir(install_path: Path) -> Path: def _version_from_marker(code_dir: Path) -> str | None: try: match = _VERSION_MARKER_RE.search((code_dir / "comfyui_version.py").read_text(encoding="utf-8")) - except (OSError, UnicodeDecodeError): + except _PATH_ERRORS: return None return match.group(1) if match else None @@ -430,7 +439,7 @@ def _add_config_roots(config_path: Path, roots: list[ModelRoot], problems: list[ for path in paths: if not any(_same_path(str(path), root.path) for root in roots): roots.append(ModelRoot(str(path), "install", _is_dir(path))) - except (OSError, ValueError, yaml.YAMLError) as error: + except (*_PATH_ERRORS, yaml.YAMLError) as error: problems.append(f"{config_path}: {error}") @@ -497,14 +506,24 @@ def _for_server( roots = [root for root in chosen.model_roots if root.source != "data"] if not configs else [] roots.insert(0, ModelRoot(models_dir, "data", _is_dir(models_dir))) problems = list(found.problems) - # ComfyUI loads ``extra_model_paths.yaml`` beside its main.py before any - # ``--extra-model-paths-config`` file. - local_config = Path(os.path.realpath(chosen.code_dir)) / "extra_model_paths.yaml" - local_state = _regular_file(local_config) - if isinstance(local_state, OSError): - problems.append(f"{local_config}: {local_state}") - elif local_state: - _add_config_roots(local_config, roots, problems) + # ComfyUI loads ``extra_model_paths.yaml`` beside the main.py it runs + # (``realpath(__file__)``) before any ``--extra-model-paths-config`` file. + # That is the server's own script, ``argv[0]``, not the recorded checkout: + # a process matched by its base dir or generated config can run a bundled + # main.py elsewhere. A script path that is not absolute leaves it unknown. + script = argv[0] if argv and Path(argv[0]).name == "main.py" and Path(argv[0]).is_absolute() else None + if script is None: + problems.append( + "running server script location is unknown; the extra_model_paths.yaml beside its main.py " + "is not in model_roots" + ) + else: + local_config = Path(os.path.dirname(os.path.realpath(script))) / "extra_model_paths.yaml" + local_state = _regular_file(local_config) + if isinstance(local_state, OSError): + problems.append(f"{local_config}: {local_state}") + elif local_state: + _add_config_roots(local_config, roots, problems) for config in configs: config_path = Path(config).expanduser() if not config_path.is_absolute(): @@ -537,7 +556,7 @@ def _for_server( def _with_server(found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool) -> DesktopDetection: try: return _for_server(found, stats, filesystem_verified=filesystem_verified) - except (OSError, ValueError) as error: + except _PATH_ERRORS as error: return replace( found, selected=None, @@ -566,9 +585,12 @@ def detect( matching strings or a loopback URL alone are insufficient. """ if app_data_dir is None: - detections = [ - found for directory in app_data_dirs() if (found := _detect_dir(directory, legacy_dir)) is not None - ] + try: + directories = app_data_dirs() + except _PATH_ERRORS as error: + # No home directory to find Desktop's app-data dir under. + return DesktopDetection(app_data_dir="", problems=(f"Desktop app-data dir: {error}",)) + detections = [found for directory in directories if (found := _detect_dir(directory, legacy_dir)) is not None] if not detections: return None installs = tuple(install for found in detections for install in found.installs) @@ -592,7 +614,7 @@ def _detect_dir(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | Non """One registry folder. A filesystem error becomes a problem, never an exception.""" try: return _read_registry(app_dir, legacy_dir, settings_dir) - except (OSError, ValueError) as error: + except _PATH_ERRORS as error: return DesktopDetection(app_data_dir=str(app_dir), problems=(f"{app_dir}: {error}",)) @@ -607,7 +629,7 @@ def _read_registry(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | problems: list[str] = [] try: records = _read_json(installations_file) - except (OSError, UnicodeDecodeError, ValueError) as error: + except _PATH_ERRORS as error: return DesktopDetection(app_data_dir=str(app_dir), problems=(f"{installations_file}: {error}",)) if not isinstance(records, list): return DesktopDetection( @@ -625,7 +647,7 @@ def _read_registry(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | raise state settings = _read_json(settings_file) shared_dirs = _str_list(settings.get("modelsDirs")) if isinstance(settings, dict) else [] - except (OSError, UnicodeDecodeError, ValueError) as error: + except _PATH_ERRORS as error: problems.append(f"{settings_file}: {error} (shared model roots are missing from model_roots)") break @@ -640,7 +662,7 @@ def _read_registry(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | config = _read_json(legacy_file) base = config.get("basePath") if isinstance(config, dict) else None legacy_base_path = base if isinstance(base, str) and base else None - except (OSError, UnicodeDecodeError, ValueError) as error: + except _PATH_ERRORS as error: legacy_base_path = None problems.append(f"{legacy_file}: {error}") diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index c20dc73a9..e2f42ffcf 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -12,6 +12,12 @@ or remove one, you're breaking the contract and the tests fail before merge. Codes are snake_case and match ``^[a-z][a-z0-9_]*$``. + +Registered codes are failure categories (ADR-AUTOMATION-0003). A code a +command only emits inside ``data.warnings[]`` of a successful result is not +registered here; the warning object carries its own ``code`` string. The +warning-only codes registered before this rule stay (the registry is +append-only) and are listed in the registry test. """ from __future__ import annotations @@ -65,19 +71,6 @@ class ErrorCode: "Resolved no workspace where one was required (e.g. `comfy which`).", "run `comfy install`, or pass `--workspace`", ), - ErrorCode( - "workspace_not_found", - "The workspace `comfy which` / `comfy env` report does not exist on disk (`exists: false`); the message " - "names the missing directory. Surfaced in `data.warnings[]` (not as an error envelope) so the command " - "still succeeds.", - "run `comfy install`, or pass `--workspace /path/to/ComfyUI`", - ), - ErrorCode( - "desktop_config_unreadable", - "Comfy Desktop installation records or settings could not be read; discovery may be incomplete. Surfaced " - "in `data.warnings[]` (not as an error envelope) so the command still succeeds.", - "check the Desktop configuration file named in the warning", - ), # --- launch / stop lifecycle --------------------------------------------- ErrorCode( "server_already_running", diff --git a/comfy_cli/schemas/error_codes.md b/comfy_cli/schemas/error_codes.md index b2c4eba88..abe22e53c 100644 --- a/comfy_cli/schemas/error_codes.md +++ b/comfy_cli/schemas/error_codes.md @@ -24,9 +24,14 @@ comfy --json discover | jq '.data.error_codes[]' ## Tests -Two tests pin the contract: +Three tests pin the contract: - Every `renderer.error(code="…")` site is registered. - Every registered code is raised somewhere (no dead codes). +- A code emitted only as a warning (an object in a `warnings` list of a + successful result) is not registered. Registered codes are failure + categories; a warning keeps its `code` string in `warnings[]`. Warning-only + codes registered before this rule stay registered, since codes are + append-only. A code goes into `error_codes.py` *before* it lands in any call site. diff --git a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md index 12e7c01f5..2f00b0764 100644 --- a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md +++ b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md @@ -50,9 +50,12 @@ files while reporting the wrong version. output fields. Zero or conflicting matches leave selection unresolved instead of combining unrelated installations. - Derive effective code/data/model/input/output roots from that selected process. - Resolve its model-path configuration the way ComfyUI loads it: the checkout's - own `extra_model_paths.yaml` first, then each `--extra-model-paths-config`; - `--models-directory` replaces the data dir's `models/`. Retain external paths + Resolve its model-path configuration the way ComfyUI loads it: the + `extra_model_paths.yaml` beside the `main.py` the process runs (ComfyUI takes + it from its script's real path, which for a bundled launch is not the + recorded checkout) first, then each `--extra-model-paths-config`; + `--models-directory` replaces the data dir's `models/`. A script path that is + not absolute leaves that local file unresolved. Retain external paths explicitly. Report the running version when supplied. The output root is the explicit launch path or that process's data/output default, with its source recorded. @@ -63,8 +66,11 @@ files while reporting the wrong version. - Surface missing directories with existence flags and warnings. A missing selected workspace leaves `which`'s exit code and `ok: true` envelope as they were under envelope/1; `exists: false` and a `workspace_not_found` warning - report it. A Desktop path that cannot be checked (for example, permission - denied) is a warning, never a failed command. Discovery is read-only: it + report it. A Desktop path that cannot be checked (permission denied, a + symlink loop, a `~` with no home directory to expand) is a warning, never a + failed command. These warning codes stay in `warnings[]` and are not + registered error codes: under ADR-AUTOMATION-0003 registered codes are + failure categories. Discovery is read-only: it creates no default workspace, installs nothing and grants no permission to inspect/upload every file under a shared root. - Optional which/env discovery fields remain additive. Runtime launch reporting diff --git a/docs/json-output.md b/docs/json-output.md index 523082a5f..0ab39aac3 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -799,6 +799,23 @@ userinfo are replaced with `***`. The full traceback still goes to stderr. Whether anything was written depends on where the command crashed, so re-read state before retrying. +### Warning codes + +A warning is an object in `data.warnings[]` of a successful envelope +(`ok: true`, exit `0`), with its own `code` and `message`. Registered error +codes are failure categories, so a code a command only emits as a warning is +not registered and is not listed in `comfy --json discover` under +`data.error_codes`. `comfy which` and `comfy env` emit two such codes: + +| `code` | Emitted when | +| --------------------------- | -------------------------------------------------------------------------------------------------------------- | +| `workspace_not_found` | The selected workspace does not exist on disk (`exists: false`); the message names the missing directory | +| `desktop_config_unreadable` | A Comfy Desktop record, settings file or path could not be read or checked; Desktop discovery may be incomplete | + +Some warning codes registered before this rule, such as `partial_execution`, +are still listed under `data.error_codes`, because registered codes are never +removed. Read a code inside `data.warnings[]` as a warning either way. + ### Workflow-edit refusal fields `workflow set-widget` / `set-slot` / `ls-nodes` add these fields for agents: diff --git a/tests/comfy_cli/output/test_error_code_registry.py b/tests/comfy_cli/output/test_error_code_registry.py index bba81920c..0931d4df2 100644 --- a/tests/comfy_cli/output/test_error_code_registry.py +++ b/tests/comfy_cli/output/test_error_code_registry.py @@ -13,6 +13,13 @@ deprecated or removed but left in the registry fails the test, forcing the dead entry to be deleted. +Warning objects (``{"code": …, "message": …}`` placed in a ``warnings`` list) +are collected separately from failures. Registered codes are failure +categories (ADR-AUTOMATION-0003), so a code a command only ever emits as a +warning stays out of the registry; the codes registered before that boundary +was enforced are listed in :data:`LEGACY_WARNING_REGISTRATIONS` and stay +(the registry is append-only). + The shape of the AST scan is conservative (literal strings only). Dynamic codes (e.g. constructed from variables) are excluded — there are none in the tree today. @@ -110,32 +117,93 @@ def _extract_class_code_attrs(tree: ast.Module) -> list[str]: return codes -def _extract_dict_code_values(tree: ast.Module) -> list[str]: - """Find ``"code": "some_code"`` in dict literals. +def _dict_code(node: ast.Dict) -> str | None: + """The literal ``"code": "some_code"`` value of a dict literal, if it has one.""" + for key, value in zip(node.keys, node.values): + if ( + isinstance(key, ast.Constant) + and key.value == "code" + and isinstance(value, ast.Constant) + and isinstance(value.value, str) + and error_codes.CODE_PATTERN.match(value.value) + ): + return value.value + return None + + +def _names_warnings(node: ast.AST) -> bool: + """An expression naming a warnings list: ``warnings``, ``op.warnings``, + ``data["warnings"]`` or ``data.setdefault("warnings", [])``.""" + if isinstance(node, ast.Name): + return "warning" in node.id + if isinstance(node, ast.Attribute): + return "warning" in node.attr + if isinstance(node, ast.Subscript): + key = node.slice + return isinstance(key, ast.Constant) and isinstance(key.value, str) and "warning" in key.value + if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr in ("setdefault", "get"): + first = node.args[0] if node.args else None + return isinstance(first, ast.Constant) and isinstance(first.value, str) and "warning" in first.value + return False + + +#: Nodes a warning dict can sit inside on its way into a warnings list. +_CARRIERS = (ast.List, ast.Tuple, ast.ListComp, ast.GeneratorExp, ast.IfExp, ast.Starred) + + +def _is_warning_dict(node: ast.Dict, parents: dict[ast.AST, ast.AST]) -> bool: + """True when the dict literal is placed into a warnings list. + + Followed up through list/comprehension wrappers to what holds it: a + ``"warnings": [...]`` key, an assignment to a warnings target, a + ``warnings.append/insert/extend(...)`` call, or a ``warnings=`` keyword. + """ + child: ast.AST = node + holder = parents.get(child) + while isinstance(holder, _CARRIERS): + child, holder = holder, parents.get(holder) + if isinstance(holder, ast.Dict): + return any( + value is child and isinstance(key, ast.Constant) and isinstance(key.value, str) and "warning" in key.value + for key, value in zip(holder.keys, holder.values) + ) + if isinstance(holder, ast.Assign): + return any(_names_warnings(target) for target in holder.targets) + if isinstance(holder, (ast.AugAssign, ast.AnnAssign)): + return _names_warnings(holder.target) + if isinstance(holder, ast.keyword): + return holder.arg is not None and "warning" in holder.arg + if isinstance(holder, ast.Call) and isinstance(holder.func, ast.Attribute): + return holder.func.attr in ("append", "insert", "extend") and _names_warnings(holder.func.value) + return False + + +def _extract_dict_code_values(tree: ast.Module) -> tuple[list[str], list[str]]: + """Find ``"code": "some_code"`` in dict literals, as ``(failures, warnings)``. The watcher and state-file paths build error dicts inline (e.g. ``{"code": "watcher_crashed", ...}``), not via ``renderer.error()``. - This picks up those codes so the registry stays honest. + This picks up those codes so the registry stays honest. A dict placed + into a warnings list is a warning object, not a failure, and is + returned separately. """ - codes: list[str] = [] + parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)} + failures: list[str] = [] + warnings: list[str] = [] for node in ast.walk(tree): if not isinstance(node, ast.Dict): continue - for key, value in zip(node.keys, node.values): - if ( - isinstance(key, ast.Constant) - and key.value == "code" - and isinstance(value, ast.Constant) - and isinstance(value.value, str) - and error_codes.CODE_PATTERN.match(value.value) - ): - codes.append(value.value) - return codes + code = _dict_code(node) + if code is None: + continue + (warnings if _is_warning_dict(node, parents) else failures).append(code) + return failures, warnings -def _collect_raised_codes() -> dict[str, list[Path]]: - """Walk every .py under comfy_cli and collect distinct error codes raised.""" +def _collect_codes() -> tuple[dict[str, list[Path]], dict[str, list[Path]]]: + """Walk every .py under comfy_cli and collect distinct codes, as ``(raised, warned)``.""" raised: dict[str, list[Path]] = {} + warned: dict[str, list[Path]] = {} for path in _iter_python_files(SRC_ROOT): try: tree = ast.parse(path.read_text(encoding="utf-8")) @@ -149,15 +217,55 @@ def _collect_raised_codes() -> dict[str, list[Path]]: # Also pick up class-level ``code = "..."`` (e.g. OAuthError subclasses). for code in _extract_class_code_attrs(tree): raised.setdefault(code, []).append(path) - # Also pick up ``"code": "..."`` in dict literals (inline error dicts). - for code in _extract_dict_code_values(tree): + # Also pick up ``"code": "..."`` in dict literals: inline error dicts + # are failures; dicts placed into a warnings list are warnings. + failures, warnings = _extract_dict_code_values(tree) + for code in failures: raised.setdefault(code, []).append(path) - return raised + for code in warnings: + warned.setdefault(code, []).append(path) + return raised, warned + + +#: Warning-only codes registered before warnings were kept out of the error +#: registry. They stay registered because registered codes are append-only; +#: no code joins this list. A new warning keeps its code in ``warnings[]`` +#: and is not registered. +LEGACY_WARNING_REGISTRATIONS = frozenset( + { + "description_ignored", + "dynamic_group_unsupported", + "installed_versions_unavailable", + "pack_read_error", + "partial_execution", + "partial_execution_targets_ignored", + "primitive_feed_unrepresentable", + "registry_invalid_node_id", + "registry_no_dependency_metadata", + "registry_partial_dependency_metadata", + "select_no_match", + "ui_only_link_dropped", + "ui_only_node_skipped", + "workflow_content_not_json", + } +) + + +@pytest.fixture(scope="module") +def collected_codes() -> tuple[dict[str, list[Path]], dict[str, list[Path]]]: + return _collect_codes() + + +@pytest.fixture(scope="module") +def raised_codes(collected_codes) -> dict[str, list[Path]]: + """Codes emitted as failures (error envelopes, inline error dicts, error classes).""" + return collected_codes[0] @pytest.fixture(scope="module") -def raised_codes() -> dict[str, list[Path]]: - return _collect_raised_codes() +def warned_codes(collected_codes) -> dict[str, list[Path]]: + """Codes emitted only inside a ``warnings`` list of an otherwise successful result.""" + return collected_codes[1] def test_every_raised_code_is_registered(raised_codes): @@ -175,6 +283,35 @@ def test_every_raised_code_is_registered(raised_codes): ) +def test_warning_only_codes_stay_out_of_the_registry(raised_codes, warned_codes): + """Registered codes are failure categories (ADR-AUTOMATION-0003), and + ``comfy discover`` publishes the registry under ``data.error_codes``. A code + a command only ever emits inside ``warnings[]`` is not a failure, so + registering it would advertise an error branch nothing can take. + + If this fails: drop the registration and keep the code in ``warnings[]``, + or raise it as an error somewhere it really is one. + """ + # Guards the check below against a scanner that silently finds no warnings. + assert {"workspace_not_found", "desktop_config_unreadable"} <= set(warned_codes) + + warning_only = {code for code in warned_codes if code not in raised_codes} + registered = sorted( + code for code in warning_only if error_codes.is_registered(code) and code not in LEGACY_WARNING_REGISTRATIONS + ) + assert not registered, ( + f"Warning-only codes registered as error codes: {registered}\n" + "Remove them from comfy_cli/error_codes.REGISTRY; warnings keep their code in warnings[]." + ) + + # The legacy list only shrinks: an entry that is no longer a registered + # warning-only code is deleted from it. + stale = sorted( + code for code in LEGACY_WARNING_REGISTRATIONS if code not in warning_only or not error_codes.is_registered(code) + ) + assert not stale, f"no longer registered warning-only codes, delete from LEGACY_WARNING_REGISTRATIONS: {stale}" + + def test_every_error_code_is_a_navigation_signal(): """An error message must point toward correctness — every registered code carries a navigation `hint` (the valid set, the close match, the next @@ -197,12 +334,19 @@ def test_every_error_code_is_a_navigation_signal(): ) -def test_every_registered_code_is_raised(raised_codes): +def test_every_registered_code_is_raised(raised_codes, warned_codes): """If this fails: a code in the registry is no longer raised anywhere. + A legacy warning registration (:data:`LEGACY_WARNING_REGISTRATIONS`) + counts as used while its warning is still emitted. + Fix: delete the dead entry from the registry, or wire the code up. """ - dead = [c for c in error_codes.all_codes() if c not in raised_codes] + dead = [ + c + for c in error_codes.all_codes() + if c not in raised_codes and not (c in LEGACY_WARNING_REGISTRATIONS and c in warned_codes) + ] assert not dead, ( f"Registered but never raised:\n{dead}\nEither delete these from comfy_cli/error_codes.REGISTRY or use them." ) diff --git a/tests/comfy_cli/test_desktop_install.py b/tests/comfy_cli/test_desktop_install.py index a2837a312..5d80e978d 100644 --- a/tests/comfy_cli/test_desktop_install.py +++ b/tests/comfy_cli/test_desktop_install.py @@ -771,3 +771,172 @@ def test_relative_models_directory_is_unverified(tmp_path): assert found.selected is None assert any("relative" in problem for problem in found.problems) + + +def _generated_config(app: Path, install_id: str) -> Path: + config = app / "instance-model-paths" / f"{install_id}.yaml" + config.parent.mkdir(parents=True, exist_ok=True) + config.write_text("{}") + return config + + +def test_bundled_process_reads_the_yaml_beside_its_own_main_py_not_the_recorded_checkout(tmp_path): + """ComfyUI loads extra_model_paths.yaml from ``realpath(__file__)``'s dir: the running script's.""" + code = _checkout(tmp_path / "recorded" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + unused = tmp_path / "unused-private-models" + (code / "extra_model_paths.yaml").write_text(f"unused:\n base_path: {unused}\n checkpoints: checkpoints\n") + bundled = _checkout(tmp_path / "bundled") + bundled_root = tmp_path / "bundled-models" + (bundled / "extra_model_paths.yaml").write_text(f"bundled:\n base_path: {bundled_root}\n loras: loras\n") + config = _generated_config(app, "live") + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + server_stats={"system": {"argv": [str(bundled / "main.py"), "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + + assert found.selected.install_id == "live" + paths = [root.path for root in found.selected.model_roots] + assert str(unused) not in paths + assert str(bundled_root) in paths + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlinks") +def test_local_yaml_is_found_through_the_real_path_of_a_symlinked_main_py(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + local_root = tmp_path / "local-models" + (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {local_root}\n checkpoints: checkpoints\n") + launcher = tmp_path / "launcher" + launcher.mkdir() + (launcher / "main.py").symlink_to(code / "main.py") + config = _generated_config(app, "live") + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + server_stats={"system": {"argv": [str(launcher / "main.py"), "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + + assert str(local_root) in [root.path for root in found.selected.model_roots] + + +def test_relative_server_script_leaves_the_local_yaml_unresolved(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + checkout_root = tmp_path / "checkout-models" + (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {checkout_root}\n checkpoints: checkpoints\n") + config = _generated_config(app, "live") + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + server_stats={"system": {"argv": ["main.py", "--extra-model-paths-config", str(config)]}}, + server_filesystem_verified=True, + ) + + assert found.selected.install_id == "live" + assert str(checkout_root) not in [root.path for root in found.selected.model_roots] + assert any("script location is unknown" in problem for problem in found.problems) + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlinks") +def test_local_yaml_symlink_loop_does_not_crash_discovery(tmp_path): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + loop = tmp_path / "loop" + loop.symlink_to(loop.name) + (code / "extra_model_paths.yaml").write_text(f"loop:\n checkpoints: {loop}\n") + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + server_stats={"system": {"argv": [str(code / "main.py")]}}, + server_filesystem_verified=True, + ) + + assert found.selected.install_id == "live" + assert found.selected.model_roots[0].path == str(code / "models") + + +@pytest.mark.parametrize("failure", ["symlink-loop", "no-home"]) +def test_path_resolution_and_expansion_failures_in_a_yaml_are_problems(tmp_path, monkeypatch, failure): + """Python 3.10 raises RuntimeError from Path.resolve() on a loop and from + Path.expanduser() without a home; either is a problem for that file only.""" + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + local = code / "extra_model_paths.yaml" + local.write_text("local:\n base_path: ~/models\n checkpoints: checkpoints\n") + explicit = tmp_path / "explicit.yaml" + explicit.write_text(f"explicit:\n loras: {tmp_path / 'explicit-loras'}\n") + if failure == "symlink-loop": + real_resolve = Path.resolve + + def resolve(self, *args, **kwargs): + if "models" in self.parts: + raise RuntimeError(f"Symlink loop from {str(self)!r}") + return real_resolve(self, *args, **kwargs) + + monkeypatch.setattr(Path, "resolve", resolve) + else: + real_expanduser = Path.expanduser + + def expanduser(self): + if str(self).startswith("~"): + raise RuntimeError("Could not determine home directory.") + return real_expanduser(self) + + monkeypatch.setattr(Path, "expanduser", expanduser) + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + settings_dir=app, + server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(explicit)]}}, + server_filesystem_verified=True, + ) + + assert found.selected.install_id == "live" + assert [problem for problem in found.problems if problem.startswith(str(local))] + assert str(tmp_path / "explicit-loras") in [root.path for root in found.selected.model_roots] + + +def test_expansion_failure_in_server_argv_is_a_problem_not_a_crash(tmp_path, monkeypatch): + code = _checkout(tmp_path / "install" / "ComfyUI") + app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) + real_expanduser = Path.expanduser + + def expanduser(self): + if str(self).startswith("~"): + raise RuntimeError("Could not determine home directory.") + return real_expanduser(self) + + monkeypatch.setattr(Path, "expanduser", expanduser) + + found = desktop_install.detect( + app, + tmp_path / "no-legacy", + settings_dir=app, + server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", "~/models.yaml"]}}, + server_filesystem_verified=True, + ) + + assert found.selected is None + assert any("could not be checked" in problem and "home directory" in problem for problem in found.problems) + + +def test_no_home_directory_for_the_default_app_data_dir_is_a_problem(monkeypatch): + def no_home(): + raise RuntimeError("Could not determine home directory.") + + monkeypatch.setattr(desktop_install, "app_data_dirs", no_home) + + found = desktop_install.detect() + + assert found is not None + assert found.selected is None + assert any("home directory" in problem for problem in found.problems) diff --git a/tests/comfy_cli/test_which_desktop.py b/tests/comfy_cli/test_which_desktop.py index 069aa4018..8494aa3c4 100644 --- a/tests/comfy_cli/test_which_desktop.py +++ b/tests/comfy_cli/test_which_desktop.py @@ -156,6 +156,25 @@ def test_which_names_unreadable_desktop_records_in_the_output(comfy_desktop, fal assert "installations.json" in problems[0] +def test_workspace_warning_codes_are_not_published_as_error_codes(comfy_desktop, fallback_workspace): + """Both codes only ever appear in ``warnings[]`` of a successful result, so + ``comfy discover`` does not list them among the failure categories.""" + from comfy_cli import error_codes + from comfy_cli.discovery import load_error_codes + + (comfy_desktop["app"] / "installations.json").write_text("{not json") + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 0, result.output + assert envelope["ok"] is True and envelope["error"] is None + warned = [w["code"] for w in envelope["data"]["warnings"]] + assert warned == ["workspace_not_found", "desktop_config_unreadable"] + published = {row["code"] for row in load_error_codes()} + assert not set(warned) & published + assert not any(error_codes.is_registered(code) for code in warned) + + def test_which_warns_when_desktop_settings_are_unreadable(comfy_desktop, fallback_workspace): (comfy_desktop["app"] / "settings.json").write_text("[broken") From 4f5e85183d2758c819e5f69a76b59f579b837180 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 22:24:11 -0500 Subject: [PATCH 4/6] fix(discovery): drop running-server selection no command reaches, skip null Desktop rows in pretty which detect() no longer takes server launch metadata: which and env never pass it, so the selection, its helpers, the input_dir/output_dir schema fields and their tests go. The ADR and changelog now describe only the registry-derived candidate. Pretty which printed 'Desktop code None' when the workspace is missing and the Desktop registry has no usable install; it now shows the Desktop rows only when an install is selected, as fill_print_table does. --- CHANGELOG.md | 2 +- comfy_cli/desktop_install.py | 260 +-------- comfy_cli/output/panels.py | 4 +- comfy_cli/schemas/env.json | 3 - comfy_cli/schemas/which.json | 3 - ...ERY-0006-preserve-runtime-path-identity.md | 108 ++-- tests/comfy_cli/test_desktop_install.py | 521 ------------------ tests/comfy_cli/test_which_desktop.py | 17 + 8 files changed, 68 insertions(+), 850 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f22621c..044d3b2ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,7 +65,7 @@ history. ### Fixed -- Desktop discovery reads both modern and legacy registries. Callers with running-server launch metadata select that exact instance, resolve its model YAML (the `extra_model_paths.yaml` beside the `main.py` the process runs first, then `--extra-model-paths-config`), `--models-directory` and effective data/input/output paths, and keep ambiguous or relative runtime identities unresolved. The optional discovery fields are documented in the which/env schemas; unrelated launch arguments are excluded. +- Desktop discovery reads both modern and legacy registries and keeps each registry's shared model roots with its own installs. The selected Desktop install is a registry-derived candidate (the only usable install, or the most recently launched); it is not matched to a running server. - `comfy which` and `comfy env` report Desktop's split code, data and model roots without changing launch/install selection. On Linux, shared model roots are read from `settings.json` under `XDG_CONFIG_HOME`, where Desktop keeps it. A nonexistent selected workspace still exits 0 but reports `exists: false` and a `workspace_not_found` warning; discovery reports malformed or unreadable Desktop configuration, and Desktop paths that cannot be checked (permission denied, a symlink loop, no home directory), as `desktop_config_unreadable` warnings and marks inferred data roots as unverified. Both codes appear only in `data.warnings[]`; they are not error codes and `comfy discover` does not list them under `data.error_codes`. - UI→API conversion expands a `COMFY_DYNAMICCOMBO_V3` selector's sub-inputs in diff --git a/comfy_cli/desktop_install.py b/comfy_cli/desktop_install.py index d7b64c1e1..2758730dc 100644 --- a/comfy_cli/desktop_install.py +++ b/comfy_cli/desktop_install.py @@ -25,7 +25,7 @@ import os import re import stat -from dataclasses import dataclass, field, replace +from dataclasses import dataclass, field from datetime import datetime, timezone from pathlib import Path from typing import Any @@ -110,8 +110,6 @@ class DesktopInstall: #: The code dir holds a ComfyUI checkout (``main.py``) — only such an #: install can be selected. exists: bool - #: Internal registry ownership for generated config identity; never serialized. - registry_dir: str | None = field(default=None, repr=False) @property def last_launched_at(self) -> str | None: @@ -146,9 +144,6 @@ class DesktopDetection: selected_reason: str | None = None #: Records that exist but could not be read, as ``": "``. problems: tuple[str, ...] = field(default_factory=tuple) - input_dir: str | None = None - output_dir: str | None = None - output_dir_source: str | None = None def to_dict(self) -> dict[str, Any]: """The ``desktop`` block of ``comfy which`` / ``comfy env``. @@ -157,7 +152,7 @@ def to_dict(self) -> dict[str, Any]: ``desktop.data_dir`` directly; ``installs`` lists every local install. """ chosen = self.selected - block: dict[str, Any] = { + return { "install_id": chosen.install_id if chosen else None, "name": chosen.name if chosen else None, "code_dir": chosen.code_dir if chosen else None, @@ -171,15 +166,9 @@ def to_dict(self) -> dict[str, Any]: "installs": [install.summary(selected=install is chosen) for install in self.installs], "app_data_dir": self.app_data_dir, } - if self.input_dir is not None: - block["input_dir"] = self.input_dir - if self.output_dir is not None: - block["output_dir"] = self.output_dir - block["output_dir_source"] = self.output_dir_source - return block -#: What checking a path from Desktop's records or a server's argv can raise: +#: What checking a path from Desktop's records can raise: #: ``OSError`` (permission denied, a missing parent), ``ValueError`` (an #: embedded NUL) and ``RuntimeError`` (on Python 3.10, ``Path.resolve()`` on a #: symlink loop and ``Path.expanduser()``/``Path.home()`` without a home @@ -201,7 +190,7 @@ def _same_path(a: str, b: str) -> bool: # ``Path.is_file()`` / ``is_dir()`` raise on a permission error (EACCES) on # Python 3.10; ``os.path`` returns False. Every probe of a path taken from -# Desktop's records or a server's argv goes through these. +# Desktop's records goes through these. def _is_file(path: str | os.PathLike[str]) -> bool: return os.path.isfile(path) @@ -277,9 +266,7 @@ def _model_roots(data_dir: Path, record: dict, shared_dirs: list[str]) -> tuple[ return tuple(roots) -def _install_from_record( - record: dict, shared_dirs: list[str], legacy_base_path: str | None, *, registry_dir: Path | None = None -) -> DesktopInstall | None: +def _install_from_record(record: dict, shared_dirs: list[str], legacy_base_path: str | None) -> DesktopInstall | None: install_path = record.get("installPath") if not isinstance(install_path, str) or not install_path: return None # a remote entry (Comfy Cloud) has no local files @@ -317,7 +304,6 @@ def _install_from_record( status=record.get("status") if isinstance(record.get("status"), str) else None, last_launched_ms=last_launched if isinstance(last_launched, (int, float)) and last_launched > 0 else None, exists=_is_file(code_dir / "main.py"), - registry_dir=str(registry_dir) if registry_dir is not None else None, ) @@ -351,238 +337,18 @@ def app_data_dirs() -> tuple[Path, ...]: return tuple(dict.fromkeys((primary, primary.parent / MODERN_APP_DIR_NAME, primary.parent / APP_DIR_NAME))) -def _argv_values(argv: list[str], flag: str) -> list[str]: - values: list[str] = [] - for index, item in enumerate(argv): - if item.startswith(flag + "="): - values.append(item.split("=", 1)[1]) - elif item == flag and index + 1 < len(argv): - for value in argv[index + 1 :]: - if value.startswith("--"): - break - values.append(value) - if flag != "--extra-model-paths-config": - break - return values - - -def server_launch_paths(stats: dict[str, Any]) -> dict[str, list[str]]: - """Return only known filesystem launch arguments, excluding unrelated credential flags.""" - system = stats.get("system") or {} - argv = system.get("argv") if isinstance(system, dict) else None - if not isinstance(argv, list) or not all(isinstance(arg, str) for arg in argv): - return {} - return { - name: values - for name in ( - "base_directory", - "models_directory", - "input_directory", - "output_directory", - "extra_model_paths_config", - ) - if (values := _argv_values(argv, "--" + name.replace("_", "-"))) - } - - -def _config_identifies_install(config: str, install: DesktopInstall) -> bool: - install_id = install.install_id - if ( - not install.registry_dir - or not install_id - or install_id in (".", "..") - or any(c in install_id for c in "/\\:\x00") - ): - return False - path = Path(config).expanduser() - if not path.is_absolute(): - return False - expected = Path(install.registry_dir) / "instance-model-paths" / f"{install_id}.yaml" - return _same_path(str(path), str(expected)) - - -def _add_config_roots(config_path: Path, roots: list[ModelRoot], problems: list[str]) -> None: - """Append the model roots an ``extra_model_paths.yaml`` adds, the way ComfyUI reads it.""" - try: - import yaml - - contents = yaml.safe_load(config_path.read_text(encoding="utf-8")) - if not isinstance(contents, dict): - raise ValueError("expected a mapping") - for row in contents.values(): - if not isinstance(row, dict): - continue - base = row.get("base_path") - base_path = None - if isinstance(base, str) and base: - base_path = Path(os.path.expandvars(base)).expanduser() - if not base_path.is_absolute(): - base_path = config_path.parent / base_path - base_path = base_path.resolve() - paths = [base_path] if base_path is not None else [] - for kind, value in row.items(): - if kind in ("base_path", "is_default"): - continue - if not isinstance(value, str): - raise ValueError(f"{kind}: expected newline-separated model paths") - for entry in value.splitlines(): - if not entry: - continue - path = Path(entry) - if not path.is_absolute(): - path = (base_path or config_path.parent) / path - path = path.resolve() - # A base covers its descendant type folders. Keep a - # separate root for absolute paths outside that base. - if base_path is None or not path.is_relative_to(base_path): - paths.append(path) - for path in paths: - if not any(_same_path(str(path), root.path) for root in roots): - roots.append(ModelRoot(str(path), "install", _is_dir(path))) - except (*_PATH_ERRORS, yaml.YAMLError) as error: - problems.append(f"{config_path}: {error}") - - -def _for_server( - found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool = False -) -> DesktopDetection: - """Select using launch identity, never a remembered last-launched record.""" - if not filesystem_verified: - return replace( - found, - selected=None, - selected_reason=None, - input_dir=None, - output_dir=None, - output_dir_source=None, - problems=(*found.problems, "running server filesystem is unverified; pass explicit model/input roots"), - ) - system = stats.get("system") or {} - if not isinstance(system, dict): - system = {} - argv = system.get("argv") or [] - if not isinstance(argv, list) or not all(isinstance(arg, str) for arg in argv): - argv = [] - bases = _argv_values(argv, "--base-directory") - configs = _argv_values(argv, "--extra-model-paths-config") - scripts = [arg for arg in argv if Path(arg).is_absolute() and Path(arg).name == "main.py"] - matches = [ - install - for install in found.installs - if install.exists - and ( - any(_same_path(base, install.data_dir) for base in bases) - or any(_same_path(str(Path(script).parent), install.code_dir) for script in scripts) - or any(_config_identifies_install(config, install) for config in configs) - ) - ] - if len(matches) != 1: - detail = "does not identify one recorded Desktop installation" - return replace( - found, - selected=None, - selected_reason=None, - problems=(*found.problems, f"running server {detail}; pass explicit model/input roots"), - ) - chosen = matches[0] - outputs = _argv_values(argv, "--output-directory") - models_dirs = _argv_values(argv, "--models-directory") - if any( - not Path(path).expanduser().is_absolute() - for path in (*bases, *models_dirs, *_argv_values(argv, "--input-directory"), *outputs) - ): - return replace( - found, - selected=None, - selected_reason=None, - problems=( - *found.problems, - "running server uses relative data/models/input/output paths; verify its working directory", - ), - ) - data_dir = str(Path(bases[-1]).expanduser()) if bases else chosen.data_dir - # ComfyUI's own models dir: ``--models-directory`` replaces ``/models``. - models_dir = str(Path(models_dirs[-1]).expanduser()) if models_dirs else str(Path(data_dir) / "models") - roots = [root for root in chosen.model_roots if root.source != "data"] if not configs else [] - roots.insert(0, ModelRoot(models_dir, "data", _is_dir(models_dir))) - problems = list(found.problems) - # ComfyUI loads ``extra_model_paths.yaml`` beside the main.py it runs - # (``realpath(__file__)``) before any ``--extra-model-paths-config`` file. - # That is the server's own script, ``argv[0]``, not the recorded checkout: - # a process matched by its base dir or generated config can run a bundled - # main.py elsewhere. A script path that is not absolute leaves it unknown. - script = argv[0] if argv and Path(argv[0]).name == "main.py" and Path(argv[0]).is_absolute() else None - if script is None: - problems.append( - "running server script location is unknown; the extra_model_paths.yaml beside its main.py " - "is not in model_roots" - ) - else: - local_config = Path(os.path.dirname(os.path.realpath(script))) / "extra_model_paths.yaml" - local_state = _regular_file(local_config) - if isinstance(local_state, OSError): - problems.append(f"{local_config}: {local_state}") - elif local_state: - _add_config_roots(local_config, roots, problems) - for config in configs: - config_path = Path(config).expanduser() - if not config_path.is_absolute(): - problems.append(f"running server model config is relative: {config}; verify its working directory") - continue - _add_config_roots(config_path, roots, problems) - python = system.get("python") - version = system.get("comfyui_version") - chosen = replace( - chosen, - comfyui_version=_as_tag(version) if isinstance(version, str) and version else chosen.comfyui_version, - data_dir=data_dir, - model_roots=tuple(roots), - python=python if isinstance(python, str) and _is_file(python) else None, - ) - inputs = _argv_values(argv, "--input-directory") - input_dir = str(Path(inputs[-1]).expanduser()) if inputs else str(Path(data_dir) / "input") - return replace( - found, - selected=chosen, - selected_reason="matches the running server launch arguments", - installs=tuple(chosen if item is matches[0] else item for item in found.installs), - input_dir=input_dir, - output_dir=str(Path(outputs[-1]).expanduser()) if outputs else str(Path(data_dir) / "output"), - output_dir_source="server_argv" if outputs else "server_default", - problems=tuple(problems), - ) - - -def _with_server(found: DesktopDetection, stats: dict[str, Any], *, filesystem_verified: bool) -> DesktopDetection: - try: - return _for_server(found, stats, filesystem_verified=filesystem_verified) - except _PATH_ERRORS as error: - return replace( - found, - selected=None, - selected_reason=None, - input_dir=None, - output_dir=None, - output_dir_source=None, - problems=(*found.problems, f"running server paths could not be checked: {error}"), - ) - - def detect( app_data_dir: Path | None = None, legacy_dir: Path | None = None, *, settings_dir: Path | None = None, - server_stats: dict[str, Any] | None = None, - server_filesystem_verified: bool = False, ) -> DesktopDetection | None: """Read Desktop 2's install records. ``None`` means Desktop 2 is not installed. The app-data dirs default to this OS's; tests pass fixture dirs instead. ``settings_dir`` is where ``settings.json`` lives; by default it is found - from ``app_data_dir`` (see :func:`desktop_settings_dirs`). - Server paths require caller-established local process/filesystem provenance; - matching strings or a loopback URL alone are insufficient. + from ``app_data_dir`` (see :func:`desktop_settings_dirs`). The selection is + a registry-derived candidate; nothing here identifies a running server. """ if app_data_dir is None: try: @@ -598,16 +364,10 @@ def detect( origin = next( (found.app_data_dir for found in detections if selected in found.installs), detections[0].app_data_dir ) - found = DesktopDetection( + return DesktopDetection( origin, installs, selected, reason, tuple(problem for item in detections for problem in item.problems) ) - else: - found = _detect_dir(app_data_dir, legacy_dir, settings_dir) - if found is None: - return None - if server_stats is None: - return found - return _with_server(found, server_stats, filesystem_verified=server_filesystem_verified) + return _detect_dir(app_data_dir, legacy_dir, settings_dir) def _detect_dir(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | None = None) -> DesktopDetection | None: @@ -680,7 +440,7 @@ def _read_registry(app_dir: Path, legacy_dir: Path | None, settings_dir: Path | installs = tuple( install for install in ( - _install_from_record(record, shared_dirs, legacy_base_path, registry_dir=app_dir) + _install_from_record(record, shared_dirs, legacy_base_path) for record in records if isinstance(record, dict) ) diff --git a/comfy_cli/output/panels.py b/comfy_cli/output/panels.py index 0db8d4bcb..14696b2f5 100644 --- a/comfy_cli/output/panels.py +++ b/comfy_cli/output/panels.py @@ -253,7 +253,9 @@ def which_panel( ("Python", python_value), ("Server", server_value), ] - if desktop: + # A registry with no usable install leaves the Desktop fields null; show + # the rows only for a selected install, as ``fill_print_table`` does. + if desktop and desktop.get("code_dir"): rows.extend( [ ("Desktop code", Text(sanitize_value(desktop["code_dir"]))), diff --git a/comfy_cli/schemas/env.json b/comfy_cli/schemas/env.json index 0e91f88f9..2e6f646b4 100644 --- a/comfy_cli/schemas/env.json +++ b/comfy_cli/schemas/env.json @@ -48,9 +48,6 @@ "data_dir": {"type": ["string", "null"]}, "custom_nodes_dir": {"type": ["string", "null"]}, "python": {"type": ["string", "null"]}, - "input_dir": {"type": "string", "description": "Effective input directory of the selected running Desktop server."}, - "output_dir": {"type": "string", "description": "Effective output directory of the selected running Desktop server."}, - "output_dir_source": {"enum": ["server_argv", "server_default"]}, "selected_reason": {"type": ["string", "null"]}, "comfyui_version": {"type": ["string", "null"]}, "model_roots": { diff --git a/comfy_cli/schemas/which.json b/comfy_cli/schemas/which.json index f160595ef..ab8080b4e 100644 --- a/comfy_cli/schemas/which.json +++ b/comfy_cli/schemas/which.json @@ -15,9 +15,6 @@ "data_dir": {"type": ["string", "null"]}, "custom_nodes_dir": {"type": ["string", "null"]}, "python": {"type": ["string", "null"]}, - "input_dir": {"type": "string", "description": "Effective input directory of the selected running Desktop server."}, - "output_dir": {"type": "string", "description": "Effective output directory of the selected running Desktop server."}, - "output_dir_source": {"enum": ["server_argv", "server_default"]}, "selected_reason": {"type": ["string", "null"]}, "comfyui_version": {"type": ["string", "null"]}, "model_roots": { diff --git a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md index 2f00b0764..0db01e652 100644 --- a/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md +++ b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md @@ -13,11 +13,10 @@ human diagnostics from machine output remain unchanged. ## Context Desktop separates its code checkout, data directory, interpreter and shared -model folders. Modern and legacy registries can coexist, and an existing -last-launched installation is not necessarily the process serving a workflow. -Model-path YAML and launch flags can override the remembered data/input/output -roots. Choosing another installation silently can inventory or upload unrelated -files while reporting the wrong version. +model folders. Modern and legacy registries can coexist, and the last-launched +installation is not necessarily the process serving a workflow. Choosing +another installation silently can inventory or upload unrelated files while +reporting the wrong version. ## Decision @@ -26,90 +25,57 @@ files while reporting the wrong version. explicit caller/test registry overrides remain isolated. On Linux, read `settings.json` from the `XDG_CONFIG_HOME` folder of the same name as the `XDG_DATA_HOME` registry, where Desktop writes it. -- Without running-server evidence, describe selection as a registry-derived - candidate and expose its selection reason. Do not claim it identifies the - running process. -- Server launch metadata is not evidence of a shared filesystem, even for a - loopback endpoint (which may be a forwarder). Before using its paths to select - a local Desktop installation or read local configurations/interpreters/roots, - require caller-established direct-endpoint and local-process provenance. Bind - the request to its resolved address, bypass proxies, refuse redirects, match - the address-specific socket owner across the request using PID/create time, - and corroborate the actual Python script/argv/cwd. On Linux, require the same - mount namespace and process root as the CLI; inability to read that evidence - leaves provenance unknown. The pure discovery API defaults provenance to - unknown. Remote, forwarded, container, inaccessible or ambiguous endpoints - retain their reported catalog/metadata but require explicit local roots. -- With verified shared-filesystem provenance, require one recorded existing - installation to match its absolute code/data path or the full resolved generated - config path: that record's owning registry directory plus - `instance-model-paths/.yaml`. A copied filename/stem is insufficient; - retain per-record ownership across coexisting registries and reject path - separators and Windows drive/stream separators in IDs used for config identity. This preserves bundled/shared-code - discovery without conflating registries. Internal ownership adds no public - output fields. Zero or conflicting matches leave selection unresolved instead of combining - unrelated installations. -- Derive effective code/data/model/input/output roots from that selected process. - Resolve its model-path configuration the way ComfyUI loads it: the - `extra_model_paths.yaml` beside the `main.py` the process runs (ComfyUI takes - it from its script's real path, which for a bundled launch is not the - recorded checkout) first, then each `--extra-model-paths-config`; - `--models-directory` replaces the data dir's `models/`. A script path that is - not absolute leaves that local file unresolved. Retain external paths - explicitly. - Report the running version when supplied. The output root is the explicit - launch path or that process's data/output default, with its source recorded. -- Relative data/models/input/output paths cannot be resolved without the process's cwd: - retain uncertainty rather than anchoring them to the agent's cwd. Relative or - unreadable model config paths remain diagnostic evidence, never a claim that - all required model folders were found. +- Describe the selection as a registry-derived candidate and expose its + selection reason. Do not claim it identifies a running process. `which` and + `env` report the candidate next to the selected workspace and never swap it + in for install or launch. +- Selecting an installation from a running server's launch metadata is not + part of this decision. Server metadata is not evidence of a shared + filesystem, even for a loopback endpoint (which may be a forwarder); the + command that first reads it records how it establishes that provenance. - Surface missing directories with existence flags and warnings. A missing selected workspace leaves `which`'s exit code and `ok: true` envelope as they were under envelope/1; `exists: false` and a `workspace_not_found` warning report it. A Desktop path that cannot be checked (permission denied, a symlink loop, a `~` with no home directory to expand) is a warning, never a - failed command. These warning codes stay in `warnings[]` and are not - registered error codes: under ADR-AUTOMATION-0003 registered codes are - failure categories. Discovery is read-only: it - creates no default workspace, installs nothing and grants no permission to + failed command. An adopted install whose data path cannot be read is marked + unverified rather than reported as known. These warning codes stay in + `warnings[]` and are not registered error codes: under ADR-AUTOMATION-0003 + registered codes are failure categories. Discovery is read-only: it creates + no default workspace, installs nothing and grants no permission to inspect/upload every file under a shared root. -- Optional which/env discovery fields remain additive. Runtime launch reporting - contains known path flags only, not arbitrary argv values or credentials. - File-upload scope and approvals belong to the calling operation; discovering - an output directory does not authorize uploading its whole contents. +- Optional which/env discovery fields remain additive. ## Alternatives considered -Selecting the newest registry entry alone is insufficient when a caller has -actual process evidence. Combining every existing install/model directory hides -identity errors and expands scope. Guessing relative paths from the CLI's cwd -can point at unrelated files. These approaches are rejected even when their -paths happen to exist. +Combining every existing install/model directory hides identity errors and +expands scope. Swapping the Desktop candidate in as the selected workspace +would change which checkout install and launch commands touch. These +approaches are rejected even when their paths happen to exist. ## Consequences Discovery can support both installed Desktop generations while callers retain -explicit path/selection provenance. Ambiguous, stale or relative process metadata -can require a user's verified path/cwd instead of automatic continuation. -Registry layouts and server metadata remain external compatibility surfaces; -unknown/malformed records are diagnostics rather than evidence of availability. -An interpreter is not inferred solely from a remembered data directory when -verified instance/process information is available. +explicit path/selection provenance. A caller that must know the running +process's roots still needs explicit paths or its own verified evidence. +Registry layouts remain external compatibility surfaces; unknown/malformed +records are diagnostics rather than evidence of availability. ## Validation -`test_desktop_install.py` covers registry isolation, actual process matching, -model-config roots, explicit/default input/output paths, relative-cwd ambiguity -and serialization. `test_which_desktop.py`, workspace-manager and Manager GUI -tests cover public reporting, missing roots and warning compatibility. Run those -tests with the worktree's package first on `PYTHONPATH`, plus Ruff lint/format, +`test_desktop_install.py` covers registry isolation, model roots, the Linux +XDG split, unreadable records and paths, and serialization. +`test_which_desktop.py`, workspace-manager and Manager GUI tests cover public +reporting, missing roots and warning compatibility. Run those tests with the +worktree's package first on `PYTHONPATH`, plus Ruff lint/format, `python scripts/check_adrs.py` and its native validator tests. ## Revisit triggers -Reopen if one running process resolves to another installation, if an unresolved -relative path is reported as verified, if server metadata bypasses direct local -process/filesystem proof, if shared folders lose provenance, or if -new Desktop layouts/launch flags require an additional documented discovery -source. A change that allows discovery to mutate installations or grants upload +Reopen if a registry-derived candidate is reported as the running process, if +an inferred path is reported as verified, if shared folders lose provenance, +or if new Desktop layouts require an additional documented discovery source. A +command that selects an installation from running-server metadata needs its +own decision on how it proves local process and filesystem provenance. A +change that allows discovery to mutate installations or grants upload permission requires a separate decision. diff --git a/tests/comfy_cli/test_desktop_install.py b/tests/comfy_cli/test_desktop_install.py index 5d80e978d..f9e9eda8f 100644 --- a/tests/comfy_cli/test_desktop_install.py +++ b/tests/comfy_cli/test_desktop_install.py @@ -317,306 +317,6 @@ def test_detect_scans_modern_userdata_and_keeps_each_registry_roots(tmp_path, mo assert str(tmp_path / "old" / "shared") not in [root.path for root in modern_install.model_roots] -def test_running_server_selects_matching_instance_instead_of_latest(tmp_path): - records = [] - for name, launched in (("running", 1), ("latest", 2)): - code = _checkout(tmp_path / name / "ComfyUI") - records.append({"id": name, "installPath": str(code.parent), "lastLaunchedAt": launched}) - app = _write_desktop(tmp_path / "Comfy Desktop", records) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(tmp_path / "running" / "ComfyUI" / "main.py")]}}, - server_filesystem_verified=True, - ) - assert found.selected.install_id == "running" - assert "running server" in found.selected_reason - - -def test_server_instance_config_matches_install_and_applies_effective_paths(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "inst-live", "installPath": str(code.parent)}]) - config = app / "instance-model-paths" / "inst-live.yaml" - config.parent.mkdir() - root = tmp_path / "effective-models" - config.write_text(f"live:\n base_path: {root}\n checkpoints: checkpoints\n") - data = tmp_path / "effective-data" - inputs = tmp_path / "private-inputs" - stats = { - "system": { - "argv": [ - "main.py", - "--extra-model-paths-config", - str(config), - "--base-directory", - str(data), - "--input-directory", - str(inputs), - ] - } - } - found = desktop_install.detect(app, server_stats=stats, server_filesystem_verified=True) - assert found.selected.install_id == "inst-live" - assert found.selected.data_dir == str(data) - assert str(root) in [root.path for root in found.selected.model_roots] - assert found.to_dict()["input_dir"] == str(inputs) - - -def test_server_that_does_not_match_records_is_not_silently_assumed(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "candidate", "installPath": str(code.parent)}]) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(tmp_path / "different" / "main.py")]}}, - server_filesystem_verified=True, - ) - assert found.selected is None - assert any("running server" in problem for problem in found.problems) - - -def test_server_model_configs_replace_stale_shared_settings(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop( - tmp_path / "Comfy Desktop", - [{"id": "live", "installPath": str(code.parent)}], - models_dirs=[str(tmp_path / "stale")], - ) - first, second = tmp_path / "one.yaml", tmp_path / "two.yaml" - first.write_text(f"one:\n base_path: {tmp_path / 'models-one'}\n checkpoints: checkpoints\n") - second.write_text(f"two:\n base_path: {tmp_path / 'models-two'}\n loras: loras\n") - stats = { - "system": { - "argv": [str(code / "main.py"), "--extra-model-paths-config", str(first), str(second)], - "comfyui_version": "0.99.0", - } - } - found = desktop_install.detect(app, server_stats=stats, server_filesystem_verified=True) - paths = [root.path for root in found.selected.model_roots] - assert str(tmp_path / "stale") not in paths - assert str(tmp_path / "models-one") in paths - assert str(tmp_path / "models-two") in paths - assert found.selected.comfyui_version == "v0.99.0" - - -def test_relative_server_data_paths_are_unverified(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(code / "main.py"), "--base-directory", "relative-data"]}}, - server_filesystem_verified=True, - ) - assert found.selected is None - assert any("relative" in problem for problem in found.problems) - - -def test_server_config_includes_absolute_paths_outside_base_and_no_base_sections(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - config = tmp_path / "models.yaml" - outside = tmp_path / "private-loras" - config.write_text( - f"one:\n base_path: {tmp_path / 'models'}\n loras: {outside}\ntwo:\n checkpoints: relative-checkpoints\n" - ) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - paths = [root.path for root in found.selected.model_roots] - assert str(outside) in paths - assert str(tmp_path / "relative-checkpoints") in paths - assert not found.problems - - -@pytest.mark.parametrize("explicit", [True, False]) -def test_running_server_output_directory_is_reported_with_source(tmp_path, explicit): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - output = tmp_path / "actual-output" if explicit else code / "output" - argv = [str(code / "main.py")] - if explicit: - argv += ["--output-directory", str(output)] - found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}, server_filesystem_verified=True) - assert found.to_dict()["output_dir"] == str(output) - assert found.to_dict()["output_dir_source"] == ("server_argv" if explicit else "server_default") - - -def test_relative_output_directory_requires_process_cwd_verification(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(code / "main.py"), "--output-directory", "relative-output"]}}, - server_filesystem_verified=True, - ) - assert found.selected is None - assert "output_dir" not in found.to_dict() - - -@pytest.mark.parametrize("override_data", [False, True], ids=["registry-data", "process-data"]) -@pytest.mark.parametrize( - "process_python", ["missing", "none", "empty", "wrong-type", "missing-file", "directory", "valid-file"] -) -def test_server_interpreter_uses_only_a_verified_process_path(tmp_path, override_data, process_python): - code = _checkout(tmp_path / "install" / "ComfyUI") - registry_python = _venv(code) - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - legacy = tmp_path / "legacy" - assert desktop_install.detect(app, legacy).selected.python == str(registry_python) - - data = tmp_path / "process-data" if override_data else code - if override_data: - _venv(data) # Even an existing data-directory interpreter is not process evidence. - runtime_python = tmp_path / "process-python" - runtime_python.write_text("") - system = {"argv": [str(code / "main.py"), "--base-directory", str(data)]} - if process_python != "missing": - system["python"] = { - "none": None, - "empty": "", - "wrong-type": 42, - "missing-file": str(tmp_path / "missing-python"), - "directory": str(data), - "valid-file": str(runtime_python), - }[process_python] - - found = desktop_install.detect(app, legacy, server_stats={"system": system}, server_filesystem_verified=True) - - expected_python = str(runtime_python) if process_python == "valid-file" else None - assert found.selected.data_dir == str(data) - assert found.selected.python == expected_python - assert found.to_dict()["python"] == expected_python - - -@pytest.mark.parametrize("match", ["code-path", "config-stem"]) -def test_server_paths_without_filesystem_provenance_do_not_select_local_install(tmp_path, monkeypatch, match): - code = _checkout(tmp_path / "unrelated-local" / "ComfyUI") - _venv(code) - app = _write_desktop(tmp_path / "registry", [{"id": "collision", "installPath": str(code.parent)}]) - config = app / "collision.yaml" - config.write_text("private:\n base_path: /unrelated-local-models\n checkpoints: checkpoints\n") - argv = [str(code / "main.py")] if match == "code-path" else ["/remote/main.py"] - argv += ["--extra-model-paths-config", str(config)] - original = Path.read_text - - def read(path, *args, **kwargs): - assert path != config, "unverified server metadata must not read a local model config" - return original(path, *args, **kwargs) - - monkeypatch.setattr(Path, "read_text", read) - found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}) - - assert found.selected is None - block = found.to_dict() - assert block["python"] is None - assert block["model_roots"] == [] - assert "input_dir" not in block and "output_dir" not in block - assert any("filesystem" in problem for problem in found.problems) - - -def test_verified_local_server_cannot_select_desktop_by_copied_config_stem(tmp_path, monkeypatch): - code = _checkout(tmp_path / "unrelated-desktop" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "collision", "installPath": str(code.parent)}]) - copied = tmp_path / "outside-registry" / "collision.yaml" - copied.parent.mkdir() - copied.write_text("private:\n base_path: /unrelated-private\n loras: loras\n") - original = Path.read_text - - def read(path, *args, **kwargs): - assert path != copied, "a copied config name is not Desktop launch identity" - return original(path, *args, **kwargs) - - monkeypatch.setattr(Path, "read_text", read) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": ["/other/main.py", "--extra-model-paths-config", str(copied)]}}, - server_filesystem_verified=True, - ) - assert found.selected is None - assert found.to_dict()["model_roots"] == [] - - -@pytest.mark.parametrize("selected_registry", [0, 1]) -def test_generated_config_identity_keeps_owning_registry_with_duplicate_ids(tmp_path, monkeypatch, selected_registry): - apps = [] - codes = [] - for name in ("comfyui-desktop-2", "Comfy Desktop"): - code = _checkout(tmp_path / name / "install" / "ComfyUI") - app = _write_desktop(tmp_path / name / "registry", [{"id": "same-id", "installPath": str(code.parent)}]) - config = app / "instance-model-paths" / "same-id.yaml" - config.parent.mkdir() - config.write_text("{}") - apps.append(app) - codes.append(code) - monkeypatch.setattr(desktop_install, "app_data_dirs", lambda: tuple(apps)) - config = apps[selected_registry] / "instance-model-paths" / "same-id.yaml" - found = desktop_install.detect( - server_stats={"system": {"argv": ["/shared/bundled/main.py", "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - assert found.selected.code_dir == str(codes[selected_registry]) - assert [i.summary(selected=i is found.selected)["selected"] for i in found.installs] == [ - selected_registry == 0, - selected_registry == 1, - ] - block = found.to_dict() - assert "registry_dir" not in block - assert all("registry_dir" not in item for item in block["installs"]) - - -@pytest.mark.parametrize( - "install_id", ["../collision", "nested/id", "nested\\id", "/absolute", "C:collision", "null\x00id"] -) -def test_config_identity_rejects_path_separators_in_record_id(tmp_path, install_id): - if ":" in install_id: - from pathlib import PureWindowsPath - - # A drive-relative component discards the owning registry on Windows. - expected = PureWindowsPath("D:/registry/instance-model-paths") / f"{install_id}.yaml" - assert expected.drive == "C:" and not expected.is_absolute() - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": install_id, "installPath": str(code.parent)}]) - config = app / "instance-model-paths" / f"{install_id}.yaml" - found = desktop_install.detect( - app, - server_stats={"system": {"argv": ["/other/main.py", "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - assert found.selected is None - - -def test_unknown_server_provenance_clears_previously_derived_directories(tmp_path): - from dataclasses import replace - - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - found = replace( - desktop_install.detect(app), - input_dir="/previous/input", - output_dir="/previous/output", - output_dir_source="server_argv", - ) - unresolved = desktop_install._for_server(found, {"system": {"argv": [str(code / "main.py")]}}) - assert unresolved.selected is None - assert unresolved.input_dir is None and unresolved.output_dir is None and unresolved.output_dir_source is None - - -def test_generated_config_identity_accepts_verified_file_alias(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - config = app / "instance-model-paths" / "live.yaml" - config.parent.mkdir() - config.write_text("{}") - alias = tmp_path / "verified-config-alias.yaml" - alias.symlink_to(config) - found = desktop_install.detect( - app, - server_stats={"system": {"argv": ["/shared/main.py", "--extra-model-paths-config", str(alias)]}}, - server_filesystem_verified=True, - ) - assert found.selected.install_id == "live" - - def test_linux_settings_are_read_from_xdg_config_beside_xdg_data_registry(tmp_path, monkeypatch): """Desktop on Linux: installations.json under XDG_DATA_HOME, settings.json under XDG_CONFIG_HOME.""" monkeypatch.setattr(desktop_install, "get_os", lambda: OS.LINUX) @@ -699,12 +399,6 @@ def test_untraversable_desktop_paths_are_contained(tmp_path): settings_dir.chmod(0) try: found = desktop_install.detect(app, tmp_path / "legacy", settings_dir=settings_dir) - server = desktop_install.detect( - app, - tmp_path / "legacy", - server_stats={"system": {"argv": [str(good / "main.py"), "--models-directory", str(locked / "models")]}}, - server_filesystem_verified=True, - ) finally: locked.chmod(0o755) settings_dir.chmod(0o755) @@ -712,221 +406,6 @@ def test_untraversable_desktop_paths_are_contained(tmp_path): assert found.selected.install_id == "good" assert {install.install_id: install.exists for install in found.installs} == {"locked": False, "good": True} assert any("settings.json" in problem for problem in found.problems) - assert server.selected.install_id == "good" - assert server.selected.model_roots[0].exists is False - - -def test_running_server_reads_the_checkout_extra_model_paths_yaml_first(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - local_root = tmp_path / "local-models" - (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {local_root}\n checkpoints: checkpoints\n") - explicit = tmp_path / "explicit.yaml" - explicit.write_text(f"explicit:\n base_path: {tmp_path / 'explicit-models'}\n loras: loras\n") - - only_local = desktop_install.detect( - app, server_stats={"system": {"argv": [str(code / "main.py")]}}, server_filesystem_verified=True - ) - both = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(explicit)]}}, - server_filesystem_verified=True, - ) - - assert (str(local_root), "install") in [(root.path, root.source) for root in only_local.selected.model_roots] - assert [root.path for root in both.selected.model_roots] == [ - str(code / "models"), - str(local_root), - str(tmp_path / "explicit-models"), - ] - assert not both.problems - - -def test_running_server_models_directory_replaces_the_data_models_root(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - external = tmp_path / "external" / "models" - external.mkdir(parents=True) - argv = [str(code / "main.py"), "--models-directory", str(external)] - - found = desktop_install.detect(app, server_stats={"system": {"argv": argv}}, server_filesystem_verified=True) - - roots = [(root.path, root.source, root.exists) for root in found.selected.model_roots] - assert roots[0] == (str(external), "data", True) - assert str(code / "models") not in [path for path, _, _ in roots] - assert desktop_install.server_launch_paths({"system": {"argv": [*argv, "--api-key", "secret"]}}) == { - "models_directory": [str(external)] - } - - -def test_relative_models_directory_is_unverified(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "Comfy Desktop", [{"id": "live", "installPath": str(code.parent)}]) - - found = desktop_install.detect( - app, - server_stats={"system": {"argv": [str(code / "main.py"), "--models-directory", "models"]}}, - server_filesystem_verified=True, - ) - - assert found.selected is None - assert any("relative" in problem for problem in found.problems) - - -def _generated_config(app: Path, install_id: str) -> Path: - config = app / "instance-model-paths" / f"{install_id}.yaml" - config.parent.mkdir(parents=True, exist_ok=True) - config.write_text("{}") - return config - - -def test_bundled_process_reads_the_yaml_beside_its_own_main_py_not_the_recorded_checkout(tmp_path): - """ComfyUI loads extra_model_paths.yaml from ``realpath(__file__)``'s dir: the running script's.""" - code = _checkout(tmp_path / "recorded" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - unused = tmp_path / "unused-private-models" - (code / "extra_model_paths.yaml").write_text(f"unused:\n base_path: {unused}\n checkpoints: checkpoints\n") - bundled = _checkout(tmp_path / "bundled") - bundled_root = tmp_path / "bundled-models" - (bundled / "extra_model_paths.yaml").write_text(f"bundled:\n base_path: {bundled_root}\n loras: loras\n") - config = _generated_config(app, "live") - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - server_stats={"system": {"argv": [str(bundled / "main.py"), "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - - assert found.selected.install_id == "live" - paths = [root.path for root in found.selected.model_roots] - assert str(unused) not in paths - assert str(bundled_root) in paths - - -@pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlinks") -def test_local_yaml_is_found_through_the_real_path_of_a_symlinked_main_py(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - local_root = tmp_path / "local-models" - (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {local_root}\n checkpoints: checkpoints\n") - launcher = tmp_path / "launcher" - launcher.mkdir() - (launcher / "main.py").symlink_to(code / "main.py") - config = _generated_config(app, "live") - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - server_stats={"system": {"argv": [str(launcher / "main.py"), "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - - assert str(local_root) in [root.path for root in found.selected.model_roots] - - -def test_relative_server_script_leaves_the_local_yaml_unresolved(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - checkout_root = tmp_path / "checkout-models" - (code / "extra_model_paths.yaml").write_text(f"local:\n base_path: {checkout_root}\n checkpoints: checkpoints\n") - config = _generated_config(app, "live") - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - server_stats={"system": {"argv": ["main.py", "--extra-model-paths-config", str(config)]}}, - server_filesystem_verified=True, - ) - - assert found.selected.install_id == "live" - assert str(checkout_root) not in [root.path for root in found.selected.model_roots] - assert any("script location is unknown" in problem for problem in found.problems) - - -@pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlinks") -def test_local_yaml_symlink_loop_does_not_crash_discovery(tmp_path): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - loop = tmp_path / "loop" - loop.symlink_to(loop.name) - (code / "extra_model_paths.yaml").write_text(f"loop:\n checkpoints: {loop}\n") - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - server_stats={"system": {"argv": [str(code / "main.py")]}}, - server_filesystem_verified=True, - ) - - assert found.selected.install_id == "live" - assert found.selected.model_roots[0].path == str(code / "models") - - -@pytest.mark.parametrize("failure", ["symlink-loop", "no-home"]) -def test_path_resolution_and_expansion_failures_in_a_yaml_are_problems(tmp_path, monkeypatch, failure): - """Python 3.10 raises RuntimeError from Path.resolve() on a loop and from - Path.expanduser() without a home; either is a problem for that file only.""" - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - local = code / "extra_model_paths.yaml" - local.write_text("local:\n base_path: ~/models\n checkpoints: checkpoints\n") - explicit = tmp_path / "explicit.yaml" - explicit.write_text(f"explicit:\n loras: {tmp_path / 'explicit-loras'}\n") - if failure == "symlink-loop": - real_resolve = Path.resolve - - def resolve(self, *args, **kwargs): - if "models" in self.parts: - raise RuntimeError(f"Symlink loop from {str(self)!r}") - return real_resolve(self, *args, **kwargs) - - monkeypatch.setattr(Path, "resolve", resolve) - else: - real_expanduser = Path.expanduser - - def expanduser(self): - if str(self).startswith("~"): - raise RuntimeError("Could not determine home directory.") - return real_expanduser(self) - - monkeypatch.setattr(Path, "expanduser", expanduser) - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - settings_dir=app, - server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", str(explicit)]}}, - server_filesystem_verified=True, - ) - - assert found.selected.install_id == "live" - assert [problem for problem in found.problems if problem.startswith(str(local))] - assert str(tmp_path / "explicit-loras") in [root.path for root in found.selected.model_roots] - - -def test_expansion_failure_in_server_argv_is_a_problem_not_a_crash(tmp_path, monkeypatch): - code = _checkout(tmp_path / "install" / "ComfyUI") - app = _write_desktop(tmp_path / "registry", [{"id": "live", "installPath": str(code.parent)}]) - real_expanduser = Path.expanduser - - def expanduser(self): - if str(self).startswith("~"): - raise RuntimeError("Could not determine home directory.") - return real_expanduser(self) - - monkeypatch.setattr(Path, "expanduser", expanduser) - - found = desktop_install.detect( - app, - tmp_path / "no-legacy", - settings_dir=app, - server_stats={"system": {"argv": [str(code / "main.py"), "--extra-model-paths-config", "~/models.yaml"]}}, - server_filesystem_verified=True, - ) - - assert found.selected is None - assert any("could not be checked" in problem and "home directory" in problem for problem in found.problems) def test_no_home_directory_for_the_default_app_data_dir_is_a_problem(monkeypatch): diff --git a/tests/comfy_cli/test_which_desktop.py b/tests/comfy_cli/test_which_desktop.py index 8494aa3c4..0103a4428 100644 --- a/tests/comfy_cli/test_which_desktop.py +++ b/tests/comfy_cli/test_which_desktop.py @@ -198,6 +198,23 @@ def test_pretty_which_shows_the_desktop_data_dir(comfy_desktop, fallback_workspa assert "specified" in result.output +def test_pretty_which_skips_desktop_rows_when_no_install_is_usable(comfy_desktop, fallback_workspace): + """A registry whose only install has no checkout leaves the Desktop fields null; + pretty ``which`` must not print them as ``None``.""" + (comfy_desktop["code"] / "main.py").unlink() + + _, envelope = _invoke("--json", "which") + assert envelope["data"]["desktop"]["code_dir"] is None + + result = CliRunner().invoke(app, ["which"], env={"COMFY_OUTPUT": "pretty", "COLUMNS": "400"}) + + assert result.exit_code == 0, result.output + assert "Desktop code" not in result.output + assert "Desktop data" not in result.output + assert "None" not in result.output + assert f"Workspace directory does not exist: {fallback_workspace}" in result.output + + def test_env_reports_the_desktop_install(comfy_desktop, fallback_workspace): result, envelope = _invoke("--json", "env") From e244ebc61c458ef0b1032752f387fb53e1ebc158 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 22:33:52 -0500 Subject: [PATCH 5/6] fix(discovery): list each Desktop problem once when registries share the legacy config --- comfy_cli/desktop_install.py | 7 ++++--- tests/comfy_cli/test_desktop_install.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/comfy_cli/desktop_install.py b/comfy_cli/desktop_install.py index 2758730dc..6262b997f 100644 --- a/comfy_cli/desktop_install.py +++ b/comfy_cli/desktop_install.py @@ -364,9 +364,10 @@ def detect( origin = next( (found.app_data_dir for found in detections if selected in found.installs), detections[0].app_data_dir ) - return DesktopDetection( - origin, installs, selected, reason, tuple(problem for item in detections for problem in item.problems) - ) + # Every registry folder reads the same legacy config.json, so one problem + # there would otherwise be listed once per folder. + problems = tuple(dict.fromkeys(problem for item in detections for problem in item.problems)) + return DesktopDetection(origin, installs, selected, reason, problems) return _detect_dir(app_data_dir, legacy_dir, settings_dir) diff --git a/tests/comfy_cli/test_desktop_install.py b/tests/comfy_cli/test_desktop_install.py index f9e9eda8f..3f2834b1a 100644 --- a/tests/comfy_cli/test_desktop_install.py +++ b/tests/comfy_cli/test_desktop_install.py @@ -317,6 +317,26 @@ def test_detect_scans_modern_userdata_and_keeps_each_registry_roots(tmp_path, mo assert str(tmp_path / "old" / "shared") not in [root.path for root in modern_install.model_roots] +def test_registries_sharing_a_broken_legacy_config_list_each_problem_once(tmp_path, monkeypatch): + old = tmp_path / "Comfy Desktop" + modern = tmp_path / "comfyui-desktop-2" + for app, name in ((old, "old"), (modern, "modern")): + code = _checkout(tmp_path / name / "ComfyUI") + _write_desktop(app, [{"id": name, "installPath": str(code), "adopted": True}]) + legacy = tmp_path / "legacy" + legacy.mkdir() + (legacy / "config.json").write_text("{broken") + monkeypatch.setattr(desktop_install, "desktop_app_data_dir", lambda: old) + + found = desktop_install.detect(legacy_dir=legacy) + + assert {install.install_id for install in found.installs} == {"old", "modern"} + assert len(found.problems) == 2 + assert len(set(found.problems)) == 2 + assert sum("config.json" in problem for problem in found.problems) == 1 + assert sum("unverified" in problem for problem in found.problems) == 1 + + def test_linux_settings_are_read_from_xdg_config_beside_xdg_data_registry(tmp_path, monkeypatch): """Desktop on Linux: installations.json under XDG_DATA_HOME, settings.json under XDG_CONFIG_HOME.""" monkeypatch.setattr(desktop_install, "get_os", lambda: OS.LINUX) From 710831c7be6d073600b793e8bd7df12193ea5743 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Wed, 4 Nov 2026 22:41:22 -0500 Subject: [PATCH 6/6] test(which): match the workspace path as JSON encodes it --- tests/comfy_cli/test_workspace_manager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/comfy_cli/test_workspace_manager.py b/tests/comfy_cli/test_workspace_manager.py index 49a615089..d3af2486c 100644 --- a/tests/comfy_cli/test_workspace_manager.py +++ b/tests/comfy_cli/test_workspace_manager.py @@ -1,3 +1,4 @@ +import json import os from unittest.mock import MagicMock, patch @@ -733,7 +734,7 @@ def test_which_still_resolves_the_workspace(self, tmp_path): result = CliRunner().invoke(app, ["--json", "which"]) assert result.exit_code == 0, result.output - assert str(workspace) in result.output + assert json.dumps(str(workspace)) in result.output def test_gitpython_is_not_imported(self): import subprocess