diff --git a/CHANGELOG.md b/CHANGELOG.md index 13a923005..fee05b851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,9 @@ history. is preserved and reported with manual reinstall guidance. Installer diagnostics stay on stderr in JSON/NDJSON modes, which end with one success or error envelope. +- 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`. + - `comfy node deps` reports malformed installed package versions as `unknown` consistently across supported `packaging` versions. One metadata probe preserves raw installed versions with or without pip. Every `===` clause diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 1583cd4cc..ab6ce9926 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -2816,7 +2816,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", @@ -2825,15 +2826,16 @@ 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"] + # 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 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: @@ -2849,12 +2851,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", ) @@ -2934,6 +2941,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..6262b997f --- /dev/null +++ b/comfy_cli/desktop_install.py @@ -0,0 +1,469 @@ +"""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``). 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 (see :data:`_PATH_ERRORS`). +""" + +from __future__ import annotations + +import json +import os +import re +import stat +from dataclasses import dataclass, field +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 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() + 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 + + @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) + + 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 + 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, + "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, + } + + +#: 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 +#: 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 _PATH_ERRORS: + pass + try: + return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b)) + except _PATH_ERRORS: + 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 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: + 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 _is_file(candidate): + 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 _is_dir(nested) and _is_file(install_path / "main.py"): + 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 _PATH_ERRORS: + 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=_is_dir(path))) + return tuple(roots) + + +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 + 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 _is_file(adopted_python): + 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=_is_file(code_dir / "main.py"), + ) + + +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 detect( + app_data_dir: Path | None = None, + legacy_dir: Path | None = None, + *, + settings_dir: Path | None = None, +) -> 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`). The selection is + a registry-derived candidate; nothing here identifies a running server. + """ + if app_data_dir is 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) + selected, reason = _select(installs) + origin = next( + (found.app_data_dir for found in detections if selected in found.installs), detections[0].app_data_dir + ) + # 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) + + +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 _PATH_ERRORS 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" + 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] = [] + try: + records = _read_json(installations_file) + 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( + app_data_dir=str(app_dir), problems=(f"{installations_file}: expected a list of installs",) + ) + + shared_dirs: list[str] = [] + 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 _PATH_ERRORS 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() + 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: + 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 _PATH_ERRORS as error: + legacy_base_path = None + problems.append(f"{legacy_file}: {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) + for record in records + if isinstance(record, dict) + ) + if install is not None + ) + selected, reason = _select(installs) + return DesktopDetection( + app_data_dir=str(app_dir), + installs=installs, + selected=selected, + selected_reason=reason, + problems=tuple(problems), + ) + + +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 f6c0d14fc..caadc50e7 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 diff --git a/comfy_cli/output/panels.py b/comfy_cli/output/panels.py index e98e31266..14696b2f5 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,23 @@ 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), ] + # 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"]))), + ("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..2e6f646b4 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,33 @@ "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"]}, + "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/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/comfy_cli/schemas/which.json b/comfy_cli/schemas/which.json index a727e4510..ab8080b4e 100644 --- a/comfy_cli/schemas/which.json +++ b/comfy_cli/schemas/which.json @@ -6,6 +6,41 @@ "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"]}, + "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..0db01e652 --- /dev/null +++ b/docs/adr/DESKTOP-DISCOVERY-0006-preserve-runtime-path-identity.md @@ -0,0 +1,81 @@ +# 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 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 + +- 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. 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. +- 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. 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. + +## Alternatives considered + +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. 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, 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 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/docs/json-output.md b/docs/json-output.md index ef463790b..57e760ff2 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -807,6 +807,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/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 2fa85f4b7..3277591f7 100644 --- a/tests/comfy_cli/conftest.py +++ b/tests/comfy_cli/conftest.py @@ -173,3 +173,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/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 new file mode 100644 index 000000000..3f2834b1a --- /dev/null +++ b/tests/comfy_cli/test_desktop_install.py @@ -0,0 +1,441 @@ +"""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 +import os +import sys +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_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) + 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) + 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) + + +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 new file mode 100644 index 000000000..0103a4428 --- /dev/null +++ b/tests/comfy_cli/test_which_desktop.py @@ -0,0 +1,375 @@ +"""`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`` 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 + +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 == 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 = 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"]) + 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["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"]} == { + "inst-test": False, + "inst-adopted": True, + } + + +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 == 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_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 == 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): + 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_output(comfy_desktop, fallback_workspace): + (comfy_desktop["app"] / "installations.json").write_text("{not json") + + result, envelope = _invoke("--json", "which") + + 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] + + +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") + + result, envelope = _invoke("--json", "which") + + assert result.exit_code == 0, result.output + desktop = envelope["data"]["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_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") + + 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 + + +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 diff --git a/tests/comfy_cli/test_workspace_manager.py b/tests/comfy_cli/test_workspace_manager.py index b5bd3aa67..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 @@ -721,17 +722,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 json.dumps(str(workspace)) in result.output def test_gitpython_is_not_imported(self): import subprocess