From 17e8dba79f66229de921fc6487a2202dafc817e3 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 5 Nov 2026 00:54:44 -0500 Subject: [PATCH 1/5] feat(workflow): add `comfy workflow check --where cloud` Says whether Comfy Cloud runs a workflow with no custom build: covered, covered_with_remaps, covered_needs_check or needs_build, with each blocking node type or model, its models folder and any local copy. --models-dir proves a model the same file by blake3 hash; --apply-remaps --out writes a remapped copy and never changes the input. --- CHANGELOG.md | 8 + comfy_cli/command/workflow.py | 8 + comfy_cli/command/workflow_check.py | 344 ++++++++++++++ comfy_cli/discovery.py | 2 + comfy_cli/error_codes.py | 17 +- comfy_cli/schemas/workflow_check.json | 139 ++++++ comfy_cli/workflow_coverage.py | 445 ++++++++++++++++++ ...overage-by-name-and-prove-files-by-hash.md | 116 +++++ docs/json-output.md | 65 +++ .../comfy_cli/command/test_workflow_check.py | 265 +++++++++++ .../comfy_cli/output/test_envelope_schemas.py | 2 + tests/comfy_cli/test_workflow_coverage.py | 297 ++++++++++++ 12 files changed, 1706 insertions(+), 2 deletions(-) create mode 100644 comfy_cli/command/workflow_check.py create mode 100644 comfy_cli/schemas/workflow_check.json create mode 100644 comfy_cli/workflow_coverage.py create mode 100644 docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md create mode 100644 tests/comfy_cli/command/test_workflow_check.py create mode 100644 tests/comfy_cli/test_workflow_coverage.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e3f07fb2..b067a712f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,14 @@ history. ### Added +- `comfy workflow check --where cloud` says whether Comfy Cloud runs a + workflow with no custom build. Signed in, it reads Cloud's node list and your + uploaded models, and answers `covered`, `covered_with_remaps` (a model sits in + another folder on Cloud), `covered_needs_check` (a model is not proven the + same file) or `needs_build`, with each blocking node type or model, its models + folder and any local copy. `--models-dir` hashes local copies against Cloud's; + `--apply-remaps --out ` writes a remapped copy and leaves the original + alone. - `comfy workflow save` on Comfy Cloud, when the workspace's `cli_cloud_save_enabled` flag is on, writes an editor-format workflow into a new folder in the Cloud Workflows sidebar (``, then ` (2)` and so diff --git a/comfy_cli/command/workflow.py b/comfy_cli/command/workflow.py index 7ec5c4a3c..2478ba417 100644 --- a/comfy_cli/command/workflow.py +++ b/comfy_cli/command/workflow.py @@ -2752,3 +2752,11 @@ def validate_cmd( app.command("foreach", help="Instantiate a recipe over N param-sets → N workflows (bulk generation).")( _wedit.foreach_cmd ) + +# `comfy workflow check --where cloud`: can Comfy Cloud run this workflow as it is? +from comfy_cli.command import workflow_check as _wcheck # noqa: E402 + +app.command( + "check", + help="Check whether Comfy Cloud runs a workflow as it is, with model remaps, or only with a build.", +)(_wcheck.check_cmd) diff --git a/comfy_cli/command/workflow_check.py b/comfy_cli/command/workflow_check.py new file mode 100644 index 000000000..ab2eedb4a --- /dev/null +++ b/comfy_cli/command/workflow_check.py @@ -0,0 +1,344 @@ +"""``comfy workflow check --where cloud``: can Comfy Cloud run this workflow as it is? + +Signed in, the check reads Cloud's node list (``object_info``) and, for a model +Cloud's dropdowns do not list, the person's uploaded models (``/api/assets``). The +comparing lives in :mod:`comfy_cli.workflow_coverage`; this module fetches, writes +the remapped copy, and reports. Mounted on ``comfy workflow`` in ``workflow.py``. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Annotated, Any + +import typer + +from comfy_cli import tracking +from comfy_cli.output import get_renderer + +_ASSET_PAGE = 200 + +_SUMMARY = { + "covered": "Runs on Comfy Cloud as it is.", + "covered_with_remaps": "Runs on Comfy Cloud once {remaps} pointed at Cloud's copies.", + "covered_needs_check": ( + "Nothing is missing on Comfy Cloud, but {unproven} not proven the same file as Cloud's copy." + ), + "needs_build": "Needs a build to run on Comfy Cloud: {blocking} below.", +} + + +def _plural(count: int, word: str, verb: str = "") -> str: + noun = word if count == 1 else word + "s" + if verb: + verb = {"is": "is" if count == 1 else "are"}.get(verb, verb) + return f"{count} {noun} {verb}" + return f"{count} {noun}" + + +def _load(renderer, workflow: str) -> tuple[Path, Any]: + from comfy_cli.workflow_coverage import workflow_format + + path = Path(workflow).expanduser() + if not path.is_file(): + renderer.error( + code="workflow_not_found", + message=f"Workflow file not found: {workflow}", + hint="check the path", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) + try: + text = path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError) as e: + renderer.error( + code="workflow_read_error", + message=f"Unable to read workflow file: {e}", + hint="check file permissions and encoding", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) from e + try: + data = json.loads(text) + except (ValueError, RecursionError) as e: + renderer.error( + code="workflow_invalid_json", + message=f"Workflow file is not valid JSON: {e}", + hint="save the workflow again from ComfyUI", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) from e + if workflow_format(data) is None: + renderer.error( + code="workflow_format_unknown", + message="The file is JSON but not a ComfyUI workflow: no `nodes[]` and not an API export.", + hint="pass a workflow saved from ComfyUI (Workflow > Save) or its API export (Workflow > Export (API))", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) + return path.resolve(), data + + +def _fetch_model_assets(target, name: str, *, include_public: bool) -> list[dict[str, Any]]: + """Model assets whose name contains ``name``. Raises on any failure; callers decide what it means.""" + import urllib.parse + + from comfy_cli.command.cloud_http import http_request + + query = urllib.parse.urlencode( + { + "include_tags": "models", + "include_public": "true" if include_public else "false", + "name_contains": name, + "limit": _ASSET_PAGE, + } + ) + _, body = http_request(target.url("assets") + "?" + query, target, strict_json=True) + rows = body.get("assets") if isinstance(body, dict) else None + if not isinstance(rows, list): + raise ValueError("unexpected response shape from /api/assets (expected an object with assets[])") + return [row for row in rows if isinstance(row, dict) and "models" in (row.get("tags") or [])] + + +def _own_models(renderer, target): + """``own_models`` for :func:`resolve_models`. A failed read ends the command: + without it a model the person uploaded would be reported missing.""" + import http.client + import urllib.error + + from comfy_cli.command._cloud_errors import handle_cloud_http_error + from comfy_cli.command.cloud_http import ResponseUnparseable + + def lookup(name: str) -> list[dict[str, Any]]: + try: + return _fetch_model_assets(target, name, include_public=False) + except (urllib.error.HTTPError, urllib.error.URLError, OSError, http.client.HTTPException) as e: + raise handle_cloud_http_error( + renderer, + e, + operation="model lookup", + not_found_code="cloud_http_error", + not_found_message="Comfy Cloud has no model list at /api/assets", + not_found_hint="check the Cloud base URL with `comfy cloud status`", + id_label="model", + resource_id=name, + ) from e + except (ResponseUnparseable, ValueError) as e: + renderer.error( + code="cloud_http_error", + message=f"unexpected response from /api/assets while looking up {name!r}: {e}", + hint="check whether a proxy or captive portal is intercepting the request", + details={"operation": "model lookup", "model": name}, + ) + raise typer.Exit(code=1) from e + + return lookup + + +def _cloud_file(target, cloud_value: str, warnings: list[dict]) -> tuple[int | None, str | None]: + """Size and blake3 hash of Cloud's model file named ``cloud_value``, or ``(None, None)``.""" + import http.client + import urllib.error + + from comfy_cli.command.cloud_http import ResponseUnparseable + from comfy_cli.workflow_coverage import content_hash + + name = cloud_value.replace("\\", "/").rsplit("/", 1)[-1] + try: + rows = _fetch_model_assets(target, name, include_public=True) + except ( + urllib.error.URLError, + OSError, + http.client.HTTPException, + ResponseUnparseable, + ValueError, + ) as e: + warnings.append( + {"code": "cloud_model_lookup_failed", "message": f"could not read Cloud's file {cloud_value!r}: {e}"} + ) + return None, None + rows = [row for row in rows if row.get("name") in (cloud_value, name)] + sizes = {row.get("size") for row in rows} + hashes = {content_hash(row.get("hash")) for row in rows} + size = sizes.pop() if len(sizes) == 1 else None + return (size if isinstance(size, int) else None), (hashes.pop() if len(hashes) == 1 else None) + + +def _print_summary(renderer, payload: dict[str, Any]) -> None: + models = payload["models"] + remaps = [m for m in models if m["status"] == "remap"] + unproven = [m for m in models if m["identity"] not in (None, "same")] + renderer.info( + _SUMMARY[payload["verdict"]].format( + remaps=_plural(len(remaps), "model", "is"), + unproven=_plural(len(unproven), "model", "is"), + blocking=_plural(len(payload["blocking"]), "item"), + ) + ) + for item in payload["blocking"]: + if item["kind"] == "node": + renderer.warn(f" node type {item['class_type']} (node {', '.join(item['node_ids'])}): not on Comfy Cloud") + else: + reason = { + "not_on_cloud": "not on Comfy Cloud", + "ambiguous": "several Cloud files share this name", + "different_file": "Cloud's file of this name is a different file", + }[item["reason"]] + where = f" (belongs in models/{item['folder']})" if item["folder"] else "" + renderer.warn(f" model {item['value']} (node {item['node_id']}): {reason}{where}") + for local in item["local_paths"]: + renderer.info(f" local copy: {local}") + for m in remaps: + renderer.info(f" remap node {m['node_id']}: {m['value']} -> {m['cloud_value']}") + for m in unproven: + renderer.info(f" {m['value']}: {m['identity']}") + if payload["out"]: + renderer.success(f"Wrote the copy with {_plural(payload['remaps_applied'], 'remap')} to {payload['out']}") + for warning in payload["warnings"]: + renderer.warn(warning["message"]) + + +@tracking.track_command("workflow") +def check_cmd( + workflow: Annotated[ + str, + typer.Argument(help="Workflow JSON file: a ComfyUI save (with nodes[]) or an API export."), + ], + where: Annotated[ + str | None, + typer.Option("--where", show_default=False, help="Where to check. Only 'cloud' today; needs sign-in."), + ] = None, + models_dir: Annotated[ + list[str] | None, + typer.Option( + "--models-dir", + show_default=False, + help="A local models folder, repeatable. A model found there is hashed and compared with Cloud's copy; " + "without one, a model Cloud has is `unverified`.", + ), + ] = None, + apply_remaps: Annotated[ + bool, + typer.Option( + "--apply-remaps", help="Write a copy with each remap applied to --out. The original is not changed." + ), + ] = False, + out: Annotated[ + str | None, + typer.Option("--out", show_default=False, help="Where --apply-remaps writes the remapped copy."), + ] = None, +): + """Report whether Comfy Cloud runs a workflow as it is, with model remaps, or only with a build.""" + from comfy_cli import workflow_coverage as cov + from comfy_cli.command.cloud_http import cloud_target_or_local_error + from comfy_cli.cql.engine import LoadError + from comfy_cli.cql.loader import resilient_load_object_info + from comfy_cli.file_utils import atomic_write_text + from comfy_cli.hashing import blake3_file + + renderer = get_renderer() + if apply_remaps != (out is not None): + renderer.error( + code="missing_argument", + message="--apply-remaps and --out go together: --out names the file the remapped copy is written to.", + hint="pass both, e.g. `--apply-remaps --out remapped.json`, or neither", + details={"missing": "--out" if apply_remaps else "--apply-remaps"}, + ) + raise typer.Exit(code=1) + path, data = _load(renderer, workflow) + out_path = Path(out).expanduser().resolve() if out is not None else None + if out_path is not None and out_path == path: + renderer.error( + code="workflow_out_is_input", + message="--out names the workflow being checked; the check never changes its input.", + hint="pass a new file name to --out", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) + + target = cloud_target_or_local_error(where, renderer) + renderer.where = "cloud" + if not (target.auth_token or target.api_key): + renderer.error( + code="auth_not_signed_in", + message="`comfy workflow check --where cloud` reads your models on Comfy Cloud and needs a sign-in.", + hint="run `comfy cloud login`", + ) + raise typer.Exit(code=1) + try: + object_info = resilient_load_object_info(mode="cloud") + except LoadError as e: + renderer.error( + code="cql_no_graph", + message=f"Could not read Comfy Cloud's node list: {e}", + hint="check `comfy cloud status`, then run the check again", + ) + raise typer.Exit(code=1) from e + + uses = cov.find_uses(data) + models = cov.resolve_models(uses, object_info, _own_models(renderer, target)) + warnings: list[dict[str, str]] = [] + roots = list(models_dir or []) + for root in roots: + if not Path(root).expanduser().is_dir(): + warnings.append({"code": "models_dir_not_found", "message": f"--models-dir is not a folder: {root}"}) + index = cov.local_index(roots) if roots else None + cloud_files: dict[str, tuple[int | None, str | None]] = {} + digests: dict[str, str] = {} + + def hash_file(file: str) -> str: + if file not in digests: + digests[file] = blake3_file(Path(file)) + return digests[file] + + cov.add_sources(models, data, index) + for model in models: + model["identity"] = None + if model["status"] not in ("present", "remap"): + continue + local = None if index is None else index.get(model["filename"], []) + size, digest = model.get("cloud_size"), model.get("cloud_hash") + if model["source"] == "cloud" and local: + if model["cloud_value"] not in cloud_files: + cloud_files[model["cloud_value"]] = _cloud_file(target, model["cloud_value"], warnings) + size, digest = cloud_files[model["cloud_value"]] + model["identity"], _ = cov.identity(local, size, digest, hash_file) + + blocked = cov.blocking(uses, object_info, models) + remaps_applied = 0 + if out_path is not None: + copy_, remaps_applied = cov.apply_remaps(data, models) + try: + out_path.parent.mkdir(parents=True, exist_ok=True) + atomic_write_text(out_path, json.dumps(copy_, indent=2, ensure_ascii=False) + "\n") + except OSError as e: + renderer.error( + code="workflow_write_error", + message=f"could not write the remapped workflow to {out_path}: {e}", + hint="check the --out path is writable and the disk has space", + details={"path": str(out_path)}, + ) + raise typer.Exit(code=1) from e + + for model in models: + for key in ("loc", "cloud_size", "cloud_hash"): + model.pop(key, None) + payload: dict[str, Any] = { + "workflow": str(path), + "format": uses["format"], + "verdict": cov.verdict(blocked, models), + "blocking": blocked, + "models": models, + "nodes": [ + {"class_type": class_type, "node_ids": node_ids, "on_cloud": class_type in object_info} + for class_type, node_ids in sorted(uses["nodes"].items()) + ], + "skipped": uses["skipped"], + "out": str(out_path) if out_path is not None else None, + "remaps_applied": remaps_applied, + "warnings": warnings, + } + if renderer.is_pretty(): + _print_summary(renderer, payload) + renderer.emit(payload, command="workflow check", where="cloud", changed=out_path is not None) diff --git a/comfy_cli/discovery.py b/comfy_cli/discovery.py index 3568735e8..7898860e8 100644 --- a/comfy_cli/discovery.py +++ b/comfy_cli/discovery.py @@ -129,6 +129,8 @@ "comfy workflow fragment ls": "workflow", "comfy workflow fragment show": "workflow", "comfy workflow fragment validate": "workflow", + # Comfy Cloud coverage: its own payload (verdict, blocking[], models[]). + "comfy workflow check": "workflow_check", # the local comfy agent's approvals "comfy agent permissions": "agent", "comfy agent allow": "agent", diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index 0690acc0e..2e3846667 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -153,10 +153,23 @@ class ErrorCode: ), ErrorCode( "workflow_write_error", - "`workflow get --out` could not write the fetched workflow to disk (OSError: permissions, " - "missing parent dir, full disk, invalid path).", + "`workflow get --out` could not write the fetched workflow to disk, or `workflow check --apply-remaps " + "--out` could not write the remapped copy (OSError: permissions, missing parent dir, full disk, " + "invalid path).", "check the --out path is writable and the disk has space", ), + ErrorCode( + "workflow_format_unknown", + "`workflow check` read valid JSON that is neither a ComfyUI save (an object with `nodes[]`) nor an " + "API export (an object of nodes, each with a `class_type`). `details.path` carries the file.", + "pass a workflow saved from ComfyUI or its API export", + ), + ErrorCode( + "workflow_out_is_input", + "`workflow check --apply-remaps --out` named the workflow being checked; the check never changes " + "its input, so nothing was written. `details.path` carries the file.", + "pass a new file name to --out", + ), ErrorCode( "workflow_too_large", "A local ComfyUI `/userdata` response exceeded the in-memory read cap, so the CLI refused to " diff --git a/comfy_cli/schemas/workflow_check.json b/comfy_cli/schemas/workflow_check.json new file mode 100644 index 000000000..5650b1492 --- /dev/null +++ b/comfy_cli/schemas/workflow_check.json @@ -0,0 +1,139 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://comfy.org/schemas/workflow_check.json", + "title": "comfy workflow check --json data payload", + "description": "Whether Comfy Cloud runs a workflow as it is. verdict: covered (every node type and model is on Cloud, each model proven the same file), covered_with_remaps (runs once each remap in models[] is applied), covered_needs_check (nothing missing, but a model is not proven the same file as Cloud's copy), needs_build (blocking[] lists what is missing).", + "type": "object", + "required": [ + "workflow", + "format", + "verdict", + "blocking", + "models", + "nodes", + "skipped", + "out", + "remaps_applied", + "warnings" + ], + "additionalProperties": true, + "$defs": { + "filename": {"type": "string", "description": "The model's file name, without folders."}, + "folder": { + "type": ["string", "null"], + "description": "The ComfyUI models folder the file belongs in (e.g. checkpoints, loras), from the workflow's download notes or the stock loader; null when unknown." + }, + "url": {"type": ["string", "null"], "description": "Download URL from the workflow's own download notes, if any."}, + "local_paths": { + "type": "array", + "items": {"type": "string"}, + "description": "Files of this name under --models-dir; empty without --models-dir." + } + }, + "properties": { + "workflow": {"type": "string", "description": "Absolute path of the checked file; never changed."}, + "format": {"enum": ["ui", "api"]}, + "verdict": {"enum": ["covered", "covered_with_remaps", "covered_needs_check", "needs_build"]}, + "blocking": { + "type": "array", + "items": { + "oneOf": [ + { + "type": "object", + "required": ["kind", "class_type", "node_ids", "reason"], + "properties": { + "kind": {"const": "node"}, + "class_type": {"type": "string"}, + "node_ids": {"type": "array", "items": {"type": "string"}}, + "reason": {"const": "not_on_cloud"} + } + }, + { + "type": "object", + "required": ["kind", "value", "node_id", "class_type", "reason", "filename", "folder", "url", "local_paths"], + "properties": { + "kind": {"const": "model"}, + "value": {"type": "string"}, + "node_id": {"type": "string"}, + "class_type": {"type": "string"}, + "reason": {"enum": ["not_on_cloud", "ambiguous", "different_file"]}, + "filename": {"$ref": "#/$defs/filename"}, + "folder": {"$ref": "#/$defs/folder"}, + "url": {"$ref": "#/$defs/url"}, + "local_paths": {"$ref": "#/$defs/local_paths"} + } + } + ] + } + }, + "models": { + "type": "array", + "items": { + "type": "object", + "required": [ + "node_id", + "class_type", + "widget", + "value", + "status", + "source", + "cloud_value", + "identity", + "filename", + "folder", + "url", + "local_paths" + ], + "properties": { + "node_id": {"type": "string", "description": "Node id; inside a subgraph, /."}, + "class_type": {"type": "string"}, + "widget": {"type": ["string", "null"], "description": "Input name, when known."}, + "value": {"type": "string", "description": "The value saved in the workflow."}, + "status": {"enum": ["present", "remap", "missing", "not_checked"]}, + "source": {"enum": ["cloud", "yours", null]}, + "cloud_value": {"type": ["string", "null"], "description": "The value Cloud runs; differs from value on a remap."}, + "identity": {"enum": ["same", "different", "unverified", null]}, + "candidates": {"type": "array", "items": {"type": "string"}}, + "filename": {"$ref": "#/$defs/filename"}, + "folder": {"$ref": "#/$defs/folder"}, + "url": {"$ref": "#/$defs/url"}, + "local_paths": {"$ref": "#/$defs/local_paths"} + } + } + }, + "nodes": { + "type": "array", + "items": { + "type": "object", + "required": ["class_type", "node_ids", "on_cloud"], + "properties": { + "class_type": {"type": "string"}, + "node_ids": {"type": "array", "items": {"type": "string"}}, + "on_cloud": {"type": "boolean"} + } + } + }, + "skipped": { + "type": "array", + "items": { + "type": "object", + "required": ["node_id", "class_type", "why"], + "properties": { + "node_id": {"type": "string"}, + "class_type": {"type": ["string", "null"]}, + "why": {"enum": ["muted", "bypassed"]} + } + } + }, + "out": {"type": ["string", "null"], "description": "Where --apply-remaps wrote the copy; null without it."}, + "remaps_applied": {"type": "integer", "minimum": 0}, + "warnings": { + "type": "array", + "items": { + "type": "object", + "required": ["code", "message"], + "properties": {"code": {"type": "string"}, "message": {"type": "string"}} + } + } + } +} diff --git a/comfy_cli/workflow_coverage.py b/comfy_cli/workflow_coverage.py new file mode 100644 index 000000000..cc5d3f0df --- /dev/null +++ b/comfy_cli/workflow_coverage.py @@ -0,0 +1,445 @@ +"""Can Comfy Cloud run one workflow as it is, and what is missing if not? + +``comfy workflow check --where cloud`` asks this. The command fetches; this module +only reads and compares, so every rule here is testable offline: + +- :func:`find_uses` reads a workflow (the editor format with ``nodes[]``, subgraphs + included, or an API export) and lists the node types and model files its active + nodes use. Muted and bypassed nodes load nothing, so they are listed as skipped. +- :func:`resolve_models` places each model on Cloud: in Cloud's own dropdown, among + the person's uploaded models, as a remap to the one Cloud file of the same name in + another folder, or missing. +- :func:`identity` says whether a local copy is proven to be the same file as + Cloud's: the same name is not the same bytes. +- :func:`verdict` and :func:`apply_remaps` turn that into the answer and the copy. + +Lifted from the Cloud inventory check in the run-on-cloud draft, cut to what +this one question needs. +""" + +from __future__ import annotations + +import copy +import os +import re +from collections.abc import Callable, Iterator, Mapping +from typing import Any + +MODEL_EXTENSIONS = (".safetensors", ".ckpt", ".pt", ".pth", ".bin", ".gguf", ".onnx", ".sft") + +# Node modes the executor skips: these nodes load nothing. +SKIPPED_MODES = {2: "muted", 4: "bypassed"} + +# Canvas-only nodes: no object_info carries them and they load nothing. +UI_ONLY_NODES = frozenset({"Note", "MarkdownNote", "Reroute", "PrimitiveNode", "GetNode", "SetNode"}) + +_HASH_RE = re.compile(r"(?:blake3:)?([0-9a-f]{64})(?:\.[A-Za-z0-9]+)?") + +VERDICTS = ("covered", "covered_with_remaps", "covered_needs_check", "needs_build") + + +def workflow_format(data: Any) -> str | None: + """``"ui"`` for the editor format, ``"api"`` for an API export, ``None`` for neither.""" + if isinstance(data, dict) and isinstance(data.get("nodes"), list): + return "ui" + if ( + isinstance(data, dict) + and data + and all(isinstance(node, dict) and isinstance(node.get("class_type"), str) for node in data.values()) + ): + return "api" + return None + + +def is_model_value(value: Any) -> bool: + return ( + isinstance(value, str) + and value.lower().endswith(MODEL_EXTENSIONS) + and "\n" not in value + and "\r" not in value + and not value.startswith(("http://", "https://")) + ) + + +def _basename(value: str) -> str: + # A workflow saved on Windows names its subfolder with "\". + return value.replace("\\", "/").rsplit("/", 1)[-1] + + +def _ui_model_strings(node: Mapping[str, Any]) -> list[str]: + if str(node.get("type", "")).startswith("CLIPTextEncode"): + return [] # a prompt can end in ".safetensors" and still be a prompt + values = node.get("widgets_values") + if isinstance(values, dict): + values = list(values.values()) + found: list[str] = [] + for value in values if isinstance(values, list) else []: + if is_model_value(value): + found.append(value) + elif isinstance(value, dict): # LoRA stacks keep rows such as {"lora": "...", "on": true} + found.extend(v for v in value.values() if is_model_value(v)) + return found + + +def _subgraphs(workflow: Mapping[str, Any]) -> list[Any]: + definitions = workflow.get("definitions") + subgraphs = definitions.get("subgraphs") if isinstance(definitions, dict) else None + return subgraphs if isinstance(subgraphs, list) else [] + + +def _ui_nodes(workflow: Mapping[str, Any]) -> Iterator[tuple[str, Mapping[str, Any], tuple]]: + """Every node an instance reaches, with its scoped id and where it lives in the file. + + A subgraph instance's interior nodes get ids ``/``. ``loc`` is + ``("nodes", i)`` or ``("subgraph", j, i)``: two instances of one definition share + one ``loc``, which is what a remap rewrites. + """ + by_id = { + sg["id"]: (j, sg) + for j, sg in enumerate(_subgraphs(workflow)) + if isinstance(sg, dict) and isinstance(sg.get("id"), str) + } + + def visit(nodes, prefix: str, base: tuple, ancestors: tuple): + for i, node in enumerate(nodes if isinstance(nodes, list) else []): + if not isinstance(node, dict): + continue + node_id = f"{prefix}/{node.get('id')}" if prefix else str(node.get("id")) + yield node_id, node, (*base, i) + definition = by_id.get(node["type"]) if isinstance(node.get("type"), str) else None + if definition is None or node.get("mode") in SKIPPED_MODES or node.get("type") in ancestors: + continue + j, sg = definition + yield from visit(sg.get("nodes"), node_id, ("subgraph", j), (*ancestors, node.get("type"))) + + yield from visit(workflow.get("nodes"), "", ("nodes",), ()) + + +def find_uses(workflow: Mapping[str, Any]) -> dict[str, Any]: + """The node types and model files a workflow's active nodes use. + + Returns ``{"format", "nodes": {class_type: [node_id, ...]}, "models": [...], + "skipped": [...]}``. Each model row carries ``node_id``, ``class_type``, + ``value``, ``widget`` (the input name in an API export, else ``None``) and an + internal ``loc`` that :func:`apply_remaps` uses and the command drops. + """ + fmt = workflow_format(workflow) + if fmt is None: + raise ValueError("not a ComfyUI workflow: expected an editor save with nodes[] or an API export") + nodes: dict[str, list[str]] = {} + models: list[dict[str, Any]] = [] + skipped: list[dict[str, Any]] = [] + seen: set[tuple[Any, ...]] = set() + + if fmt == "api": + for node_id, node in workflow.items(): + class_type = node["class_type"] + nodes.setdefault(class_type, []).append(str(node_id)) + inputs = node.get("inputs") if isinstance(node.get("inputs"), dict) else {} + for name, value in inputs.items(): + if is_model_value(value): + models.append( + { + "node_id": str(node_id), + "class_type": class_type, + "value": value, + "widget": name, + "loc": ("api", str(node_id)), + } + ) + return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} + + subgraph_ids = {sg["id"] for sg in _subgraphs(workflow) if isinstance(sg, dict) and isinstance(sg.get("id"), str)} + for node_id, node, loc in _ui_nodes(workflow): + class_type = node.get("type") + if node.get("mode") in SKIPPED_MODES: # a muted subgraph's interior is never walked + skipped.append({"node_id": node_id, "class_type": class_type, "why": SKIPPED_MODES[node["mode"]]}) + continue + if not isinstance(class_type, str) or class_type in UI_ONLY_NODES or class_type in subgraph_ids: + continue + nodes.setdefault(class_type, []).append(node_id) + for value in _ui_model_strings(node): + if (loc, value) in seen: # a second instance of the same subgraph + continue + seen.add((loc, value)) + models.append({"node_id": node_id, "class_type": class_type, "value": value, "widget": None, "loc": loc}) + return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} + + +def combo_choices(class_info: Any) -> dict[str, str]: + """Every string a node type's dropdowns accept, mapped to the input that accepts it. + + Reads both object_info forms: ``[["a", "b"], {...}]`` and ``["COMBO", {"options": [...]}]``. + """ + choices: dict[str, str] = {} + inputs = class_info.get("input") if isinstance(class_info, dict) else None + for section in ("required", "optional"): + group = inputs.get(section) if isinstance(inputs, dict) else None + for name, spec in (group or {}).items() if isinstance(group, dict) else (): + if not isinstance(spec, list) or not spec: + continue + options = spec[0] + if not isinstance(options, list) and len(spec) > 1 and isinstance(spec[1], dict): + options = spec[1].get("options") + for option in options if isinstance(options, list) else []: + if isinstance(option, str): + choices.setdefault(option, name) + return choices + + +def content_hash(value: Any) -> str | None: + """Cloud reports a hash as ``blake3:`` or as the stored ``.``; normalize to the first.""" + match = _HASH_RE.fullmatch(value) if isinstance(value, str) else None + return f"blake3:{match[1]}" if match else None + + +def resolve_models( + uses: Mapping[str, Any], + object_info: Mapping[str, Any], + own_models: Callable[[str], list[dict[str, Any]]], +) -> list[dict[str, Any]]: + """Place each model on Cloud. + + ``own_models(basename)`` returns the person's uploaded model assets whose name + contains ``basename``. It is asked only about models Cloud's dropdowns do not + list, once per name. + + ``status`` is ``present`` (Cloud runs the saved value), ``remap`` (exactly one + Cloud file has the same name in another folder: the saved value must change to + ``cloud_value``), ``missing``, or ``not_checked`` when the node type itself is + not on Cloud. ``source`` is ``cloud`` for Cloud's own models and ``yours`` for + the person's uploads. + """ + asked: dict[str, list[dict[str, Any]]] = {} + rows: list[dict[str, Any]] = [] + for use in uses["models"]: + value, class_type = use["value"], use["class_type"] + row: dict[str, Any] = { + "node_id": use["node_id"], + "class_type": class_type, + "widget": use["widget"], + "value": value, + "status": "missing", + "source": None, + "cloud_value": None, + "loc": use["loc"], + } + rows.append(row) + if class_type not in object_info: + row["status"] = "not_checked" + continue + choices = combo_choices(object_info[class_type]) + name = _basename(value) + same_name = sorted(c for c in choices if _basename(c) == name) + if value in choices: + row.update(status="present", source="cloud", cloud_value=value, widget=row["widget"] or choices[value]) + continue + if len(same_name) == 1: + to = same_name[0] + row.update(status="remap", source="cloud", cloud_value=to, widget=row["widget"] or choices[to]) + continue + if same_name: + row["candidates"] = same_name # several Cloud files share the name: no lossless pick + continue + if name not in asked: + asked[name] = [a for a in own_models(name) if isinstance(a, dict)] + mine = [a for a in asked[name] if a.get("name") in (value, name)] + exact = [a for a in mine if a.get("name") == value] or mine + if not exact: + continue + asset = exact[0] + row.update( + status="present" if asset["name"] == value else "remap", + source="yours", + cloud_value=asset["name"], + cloud_hash=content_hash(asset.get("hash")), + cloud_size=asset.get("size") if isinstance(asset.get("size"), int) else None, + ) + if len({content_hash(a.get("hash")) for a in exact}) > 1: + row["cloud_hash"] = None # two uploads share the name: which one runs is not ours to say + return rows + + +def identity( + local: list[tuple[str, int]] | None, + cloud_size: int | None, + cloud_hash: str | None, + hash_file: Callable[[str], str], +) -> tuple[str, str | None]: + """``same``, ``different`` or ``unverified``, and the local path compared. + + ``local`` is every local file with the model's name, as ``(path, size)``; + ``None`` when no models folder was given. Only a blake3 match proves ``same``; + a size mismatch alone proves ``different``. + """ + if not local or len({size for _, size in local}) > 1: + return "unverified", None + path, size = local[0] + if cloud_size is not None and cloud_size != size: + return "different", path + if cloud_hash is None: + return "unverified", path + digests = {hash_file(p) for p, _ in local} + if len(digests) > 1: + return "unverified", None + return ("same" if digests == {cloud_hash} else "different"), path + + +def local_index(roots: list[str]) -> dict[str, list[tuple[str, int]]]: + """Every model file under ``roots``, by file name.""" + index: dict[str, list[tuple[str, int]]] = {} + for root in roots: + for dirpath, _dirs, files in os.walk(os.path.expanduser(root), followlinks=True): + for filename in files: + if filename.lower().endswith(MODEL_EXTENSIONS) and not filename.startswith("."): + full = os.path.join(dirpath, filename) + try: + index.setdefault(filename, []).append((full, os.stat(full).st_size)) + except OSError: + continue + return index + + +# Where ComfyUI keeps a model, by the loader input that names it and, for an editor +# save (whose widgets carry no names), by the loader type. Only the stock loaders: +# anything else reports ``folder: null`` unless the workflow's own download notes say. +_FOLDER_BY_INPUT = { + "ckpt_name": "checkpoints", + "lora_name": "loras", + "vae_name": "vae", + "unet_name": "diffusion_models", + "clip_name": "text_encoders", + "clip_name1": "text_encoders", + "clip_name2": "text_encoders", + "clip_name3": "text_encoders", + "control_net_name": "controlnet", + "style_model_name": "style_models", + "gligen_name": "gligen", + "hypernetwork_name": "hypernetworks", +} +_FOLDER_BY_CLASS = { + "CheckpointLoaderSimple": "checkpoints", + "ImageOnlyCheckpointLoader": "checkpoints", + "LoraLoader": "loras", + "LoraLoaderModelOnly": "loras", + "VAELoader": "vae", + "UNETLoader": "diffusion_models", + "CLIPLoader": "text_encoders", + "DualCLIPLoader": "text_encoders", + "TripleCLIPLoader": "text_encoders", + "ControlNetLoader": "controlnet", + "UpscaleModelLoader": "upscale_models", + "CLIPVisionLoader": "clip_vision", + "StyleModelLoader": "style_models", +} + + +def download_notes(workflow: Mapping[str, Any]) -> dict[str, dict[str, str | None]]: + """``{filename: {"folder", "url"}}`` from the download notes an editor save may carry + (top-level ``models[]`` and each node's ``properties.models[]``, as the ComfyUI + templates write them).""" + entries: list[Any] = [] + if isinstance(workflow.get("models"), list): + entries.extend(workflow["models"]) + if workflow_format(workflow) == "ui": + for _, node, _ in _ui_nodes(workflow): + props = node.get("properties") + if isinstance(props, dict) and isinstance(props.get("models"), list): + entries.extend(props["models"]) + notes: dict[str, dict[str, str | None]] = {} + for entry in entries: + if isinstance(entry, dict) and isinstance(entry.get("name"), str): + folder, url = entry.get("directory"), entry.get("url") + notes.setdefault( + _basename(entry["name"]), + {"folder": folder if isinstance(folder, str) else None, "url": url if isinstance(url, str) else None}, + ) + return notes + + +def add_sources( + models: list[dict[str, Any]], + workflow: Mapping[str, Any], + index: Mapping[str, list[tuple[str, int]]] | None, +) -> None: + """Give each model row what a person needs to find or upload the file: ``filename``, + ``folder`` (the ComfyUI models folder, or ``None`` when unknown), ``url`` (from the + workflow's download notes, else ``None``) and ``local_paths`` (copies under + ``--models-dir``; empty without one).""" + notes = download_notes(workflow) + for model in models: + name = _basename(model["value"]) + note = notes.get(name, {}) + model["filename"] = name + model["folder"] = ( + note.get("folder") + or _FOLDER_BY_INPUT.get(model.get("widget") or "") + or _FOLDER_BY_CLASS.get(model["class_type"]) + ) + model["url"] = note.get("url") + model["local_paths"] = [path for path, _ in (index or {}).get(name, [])] + + +def blocking(uses: Mapping[str, Any], object_info: Mapping[str, Any], models: list[dict[str, Any]]) -> list[dict]: + """What stops the workflow from running on Comfy Cloud: each missing node type, then each model.""" + found: list[dict[str, Any]] = [ + {"kind": "node", "class_type": class_type, "node_ids": node_ids, "reason": "not_on_cloud"} + for class_type, node_ids in uses["nodes"].items() + if class_type not in object_info + ] + for model in models: + reason = None + if model["status"] == "missing": + reason = "ambiguous" if model.get("candidates") else "not_on_cloud" + elif model.get("identity") == "different": + reason = "different_file" + if reason: + found.append( + { + "kind": "model", + "value": model["value"], + "node_id": model["node_id"], + "class_type": model["class_type"], + "reason": reason, + **{key: model[key] for key in ("filename", "folder", "url", "local_paths") if key in model}, + } + ) + return found + + +def verdict(blocked: list[dict], models: list[dict[str, Any]]) -> str: + """A remap outranks an unproven model: the unchanged original does not run while a remap is due.""" + if blocked: + return "needs_build" + if any(m["status"] == "remap" for m in models): + return "covered_with_remaps" + if any(m.get("identity") != "same" for m in models): + return "covered_needs_check" + return "covered" + + +def apply_remaps(workflow: Mapping[str, Any], models: list[dict[str, Any]]) -> tuple[dict[str, Any], int]: + """A copy of ``workflow`` with each remap's value replaced; the original is not touched.""" + out = copy.deepcopy(workflow) + changed = 0 + for model in models: + if model["status"] != "remap": + continue + loc, old, new = model["loc"], model["value"], model["cloud_value"] + if loc[0] == "api": + out[loc[1]]["inputs"][model["widget"]] = new + changed += 1 + continue + node = out["nodes"][loc[1]] if loc[0] == "nodes" else out["definitions"]["subgraphs"][loc[1]]["nodes"][loc[2]] + values = node.get("widgets_values") + items = list(values.items()) if isinstance(values, dict) else list(enumerate(values or [])) + for key, value in items: + if value == old: + values[key] = new + changed += 1 + elif isinstance(value, dict): + for inner, inner_value in list(value.items()): + if inner_value == old: + value[inner] = new + changed += 1 + return out, changed diff --git a/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md b/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md new file mode 100644 index 000000000..00f8d8b26 --- /dev/null +++ b/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md @@ -0,0 +1,116 @@ +# ADR-CHECK-0010: Judge Cloud Coverage by Name and Prove Files by Hash + +Date: 2026-11-05 + +## Status + +Proposed + +Relation: Builds on ADR-AUTOMATION-0003. `comfy workflow check` emits one +`envelope/1` envelope with its own `workflow_check` data schema. + +## Context + +A person or an agent holds a workflow file and wants to know whether Comfy +Cloud runs it as it is, or whether it needs a custom build first. Comfy Cloud +answers part of this already: its node list (`/api/object_info`) names every +node type it has and, in each loader's dropdown, every model file it ships. +Models the person uploaded are not in those dropdowns; they are in the +person's model list (`/api/assets`). + +Three things make the question harder than a lookup: + +- A model saved as `ink.safetensors` may sit on Cloud as + `styles/ink.safetensors`. The workflow runs once the value is changed. +- Two files with the same name are not proven the same bytes. A LoRA trained + twice keeps its name. +- Several Cloud files may share one name in different folders. Picking one + would be a guess. + +## Decision + +`comfy workflow check --where cloud` reads the workflow, Cloud's node +list and, for models the dropdowns do not list, the signed-in person's model +list. It answers with one of four verdicts, checked in this order: + +1. `needs_build`: a node type or model is missing, several Cloud files share a + model's name, or a local copy is proven a different file from Cloud's. +2. `covered_with_remaps`: everything is on Cloud, but a saved value must change + to point at Cloud's copy in another folder. +3. `covered_needs_check`: nothing is missing, but a model is not proven the + same file as Cloud's copy. +4. `covered`: every node type and model is on Cloud and each model is proven + the same file. + +A remap outranks an unproven model: the unchanged file does not run while a +remap is due. + +Presence is judged by name. Identity is judged by bytes: a model is `same` +only when a local copy under `--models-dir` has the blake3 hash Cloud reports. +A size mismatch alone proves `different`. Without a local copy, or when +several local copies disagree, the model is `unverified`. + +A remap happens only when exactly one Cloud file has the name. The check never +writes its input: `--apply-remaps --out ` writes a remapped copy, and an +`--out` that names the input is refused. + +A failed read of the person's model list ends the command with an error. Without +it, a model the person uploaded would be reported missing. + +## Alternatives considered + +- Treat the same name as the same file. Simple, and right most of the time, but + a retrained LoRA would run on Cloud with the old weights and nobody would + know. The `covered_needs_check` verdict keeps that case visible. +- Hash every local model on each check. That reads every byte of every model + on the machine; the check hashes only files of the workflow's model names + under the folders the person passes with `--models-dir`. +- Pick the first match when several Cloud files share a name. The pick would be + a guess; the check lists `candidates` instead and blocks. +- Rewrite the workflow in place. A person keeps the file they saved; a copy is + cheap. + +## What we reject + +- A verdict of `covered` for a model whose bytes were never compared. +- Any write to the checked workflow file. +- Treating a failed Cloud read as "missing". + +## Consequences + +### Positive + +- An agent can route on one field: run the workflow as it is, apply remaps + first, or start a build. +- Each blocking model carries its file name, its ComfyUI models folder (from + the workflow's own download notes or the stock loader) and any local copy, + which is what an upload needs. + +### Negative + +- Without `--models-dir`, a workflow that uses any model is at best + `covered_needs_check`. +- Folder names for custom loaders are unknown unless the workflow's download + notes give them. + +### Known limits + +- A model value is recognized by its file extension; a loader that takes a name + without one is not checked. +- The check compares node types, not node versions. + +## Validation + +`tests/comfy_cli/test_workflow_coverage.py` covers the offline rules: muted and +bypassed nodes, subgraphs, both dropdown forms, remaps, shared names, uploads, +identity and the verdict order. `tests/comfy_cli/command/test_workflow_check.py` +covers each verdict end to end against a mocked Cloud, that `--apply-remaps` +leaves the input unchanged, and each refusal. Each output validates against +`comfy_cli/schemas/workflow_check.json`. + +## Revisit triggers + +- Cloud's node list starts listing the person's uploads in loader dropdowns: + the model-list read can go. +- Cloud publishes a per-file hash in its node list: identity could be checked + without the extra model-list read per model. diff --git a/docs/json-output.md b/docs/json-output.md index 57e760ff2..30423ca3e 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -746,6 +746,71 @@ a populated `errors` array, `partner_nodes`/`spends_credits` still present) and exit code `1`. Structural failures (missing file, non-object JSON, an unconvertible UI export) emit an [error object](#error-object) instead. +## `comfy workflow check --json` envelope + +`comfy --json workflow check --where cloud` answers one question: can Comfy +Cloud run this workflow with no custom build? It needs a sign-in, reads Cloud's +node list and, for a model Cloud's dropdowns do not list, your uploaded models. +The file is a ComfyUI save (with `nodes[]`, subgraphs included) or an API +export. Muted and bypassed nodes load nothing and are listed in `skipped`. + +| Field | Type | Description | +| ---------------- | ------------- | --------------------------------------------------------------------------------------------------- | +| `workflow` | str | Absolute path of the checked file. The check never changes it | +| `format` | str | `ui` (a ComfyUI save) or `api` (an API export) | +| `verdict` | str | `covered`, `covered_with_remaps`, `covered_needs_check` or `needs_build` (below) | +| `blocking` | array of dict | What stops the workflow running on Cloud; empty unless `needs_build` | +| `models` | array of dict | Every model file the active nodes load, one row each | +| `nodes` | array of dict | `{class_type, node_ids, on_cloud}` for each node type used | +| `skipped` | array of dict | `{node_id, class_type, why}` for each muted or bypassed node; `why` is `muted` or `bypassed` | +| `out` | str or null | Where `--apply-remaps --out` wrote the remapped copy | +| `remaps_applied` | number | Values changed in that copy; `0` without `--out` | +| `warnings` | array of dict | `{code, message}`: `models_dir_not_found`, `cloud_model_lookup_failed` | + +`verdict`, checked in this order: + +- `needs_build`: a node type or model is not on Cloud, or a model is proven a different file. `blocking[]` lists each. +- `covered_with_remaps`: everything is on Cloud, but a model sits in another folder there, so the saved value must + change. Each such row has `status: "remap"` and the value Cloud runs in `cloud_value`; `--apply-remaps --out + ` writes a copy with those values changed. +- `covered_needs_check`: nothing is missing, but a model is not proven the same file as Cloud's copy (`identity` is + not `same`). +- `covered`: every node type and model is on Cloud, and each model is proven the same file. + +A model is proven the same file only when a local copy under `--models-dir` has the blake3 hash Cloud reports for +its copy. The same name is not the same bytes. Without `--models-dir`, a model Cloud has is `unverified`. + +Each `models[]` row: + +| Field | Type | Description | +| ------------- | ------------- | ------------------------------------------------------------------------------------------------- | +| `node_id` | str | Node id; inside a subgraph, `/` | +| `class_type` | str | The node type | +| `widget` | str or null | The input name, when known (always in an API export) | +| `value` | str | The value saved in the workflow | +| `status` | str | `present`, `remap`, `missing`, or `not_checked` when the node type itself is not on Cloud | +| `source` | str or null | `cloud` for Cloud's own models, `yours` for your uploads | +| `cloud_value` | str or null | The value Cloud runs; differs from `value` on a remap | +| `identity` | str or null | `same`, `different` or `unverified` for a model Cloud has; null otherwise | +| `candidates` | array of str | Present when several Cloud files share the name, so no remap is picked | +| `filename` | str | The file name, without folders | +| `folder` | str or null | The ComfyUI models folder it belongs in (`checkpoints`, `loras`, ...), from the workflow's own download notes or the stock loader; null when unknown | +| `url` | str or null | The download link from the workflow's own download notes, if it has one | +| `local_paths` | array of str | Files of this name under `--models-dir`; empty without it | + +Each `blocking[]` item has a `kind`: + +- `node`: `{kind, class_type, node_ids, reason: "not_on_cloud"}`. +- `model`: `{kind, value, node_id, class_type, reason, filename, folder, url, local_paths}`, where `reason` is + `not_on_cloud`, `ambiguous` (several Cloud files share the name; see `candidates` in `models[]`) or + `different_file`. `folder` and `local_paths` say where the file goes and where a copy is on this machine. + +Every verdict exits `0`: the check is a report. `changed` is `true` only when `--out` wrote a file. +Refusals emit an [error object](#error-object): `missing_argument` (`--apply-remaps` without `--out`, or the +reverse), `workflow_out_is_input`, `workflow_not_found`, `workflow_read_error`, `workflow_invalid_json`, +`workflow_format_unknown`, `cloud_only_command`, `auth_not_signed_in`, `cql_no_graph` (Cloud's node list could not +be read), `cloud_unauthorized` / `cloud_http_error` (the model list could not be read) and `workflow_write_error`. + ## Error object Every failure envelope carries: diff --git a/tests/comfy_cli/command/test_workflow_check.py b/tests/comfy_cli/command/test_workflow_check.py new file mode 100644 index 000000000..0c97e3899 --- /dev/null +++ b/tests/comfy_cli/command/test_workflow_check.py @@ -0,0 +1,265 @@ +"""`comfy workflow check --where cloud`: the command around comfy_cli.workflow_coverage. + +Cloud's node list and model list are mocked; the comparing rules are covered in +tests/comfy_cli/test_workflow_coverage.py. +""" + +from __future__ import annotations + +import json +import urllib.error +import urllib.parse +from pathlib import Path +from typing import Any + +import jsonschema +import pytest +from typer.testing import CliRunner + +from comfy_cli.caller import Caller +from comfy_cli.command import workflow as workflow_cmd +from comfy_cli.output.renderer import OutputMode, Renderer, reset_renderer_for_testing, set_renderer + +_SCHEMA = json.loads( + (Path(__file__).resolve().parents[3] / "comfy_cli" / "schemas" / "workflow_check.json").read_text() +) + +OBJECT_INFO = { + "CheckpointLoaderSimple": {"input": {"required": {"ckpt_name": [["sd15.safetensors"], {}]}}}, + "LoraLoader": {"input": {"required": {"lora_name": [["styles/ink.safetensors"], {}]}}}, + "KSampler": {"input": {"required": {"seed": ["INT", {}]}}}, +} +SD15 = b"sd15 weights" + + +@pytest.fixture(autouse=True) +def reset_singleton(): + reset_renderer_for_testing() + yield + reset_renderer_for_testing() + + +def _force_json_renderer(): + r = Renderer.resolve( + is_stdout_tty=False, + env={}, + caller=Caller(kind="user", agentic=False, source_env=None), + json_flag=True, + ) + r.mode = OutputMode.JSON + set_renderer(r) + + +def _run(args: list[str], capsys: pytest.CaptureFixture[str]) -> dict[str, Any]: + _force_json_renderer() + result = CliRunner().invoke(workflow_cmd.app, ["check", *args], standalone_mode=False) + captured = capsys.readouterr().out or result.stdout or "" + assert captured.strip(), f"no envelope on stdout (rc={result.exit_code}, exc={result.exception})" + return json.loads(captured.strip().splitlines()[-1]) + + +def _cloud_target(**creds): + from comfy_cli.target import Target + + return Target(kind="cloud", base_url="https://cloud.example.com", path_prefix="/api", **creds) + + +@pytest.fixture +def cloud(monkeypatch: pytest.MonkeyPatch): + """A signed-in Cloud: OBJECT_INFO as its node list, ``state["assets"]`` as its model list.""" + state: dict[str, Any] = {"assets": [], "requests": []} + monkeypatch.setattr("comfy_cli.target.resolve_target", lambda **kw: _cloud_target(api_key="k")) + monkeypatch.setattr("comfy_cli.cql.loader.resilient_load_object_info", lambda **kw: OBJECT_INFO) + + def fake_http(url, target, **kw): + query = urllib.parse.parse_qs(urllib.parse.urlsplit(url).query) + state["requests"].append(query) + public = query["include_public"] == ["true"] + needle = query["name_contains"][0] + rows = [a for a in state["assets"] if needle in a["name"] and (public or not a.get("public"))] + return 200, {"assets": rows, "has_more": False} + + monkeypatch.setattr("comfy_cli.command.cloud_http.http_request", fake_http) + return state + + +def _write(tmp_path: Path, data: Any, name: str = "wf.json") -> Path: + path = tmp_path / name + path.write_text(json.dumps(data)) + return path + + +def _ui(*nodes): + return {"nodes": [{"id": i, "type": t, "mode": 0, "widgets_values": v} for i, t, v in nodes], "links": []} + + +def _ok(env: dict[str, Any]) -> dict[str, Any]: + assert env["ok"] is True, env + assert env["command"] == "workflow check" + assert env["where"] == "cloud" + jsonschema.validate(env["data"], _SCHEMA) + return env["data"] + + +# --- the four verdicts ----------------------------------------------------- + + +def test_needs_check_when_models_are_not_proven(cloud, tmp_path, capsys): + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]), (2, "KSampler", [1]))) + data = _ok(_run([str(wf), "--where", "cloud"], capsys)) + assert data["verdict"] == "covered_needs_check" + assert data["blocking"] == [] + [model] = data["models"] + assert model["status"] == "present" and model["source"] == "cloud" and model["identity"] == "unverified" + assert model["folder"] == "checkpoints" and model["filename"] == "sd15.safetensors" + assert {n["class_type"]: n["on_cloud"] for n in data["nodes"]} == {"CheckpointLoaderSimple": True, "KSampler": True} + assert data["out"] is None and data["remaps_applied"] == 0 + + +def test_covered_when_a_local_copy_hashes_the_same(cloud, tmp_path, capsys): + from comfy_cli.hashing import blake3_file + + models = tmp_path / "models" / "checkpoints" + models.mkdir(parents=True) + (models / "sd15.safetensors").write_bytes(SD15) + digest = blake3_file(models / "sd15.safetensors") + cloud["assets"] = [ + {"name": "sd15.safetensors", "hash": digest, "size": len(SD15), "tags": ["models"], "public": True} + ] + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + data = _ok(_run([str(wf), "--where", "cloud", "--models-dir", str(tmp_path / "models")], capsys)) + assert data["verdict"] == "covered" + assert data["models"][0]["identity"] == "same" + assert data["models"][0]["local_paths"] == [str(models / "sd15.safetensors")] + + +def test_a_different_local_file_blocks(cloud, tmp_path, capsys): + (tmp_path / "sd15.safetensors").write_bytes(SD15) + cloud["assets"] = [ + {"name": "sd15.safetensors", "hash": "f" * 64, "size": len(SD15), "tags": ["models"], "public": True} + ] + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + data = _ok(_run([str(wf), "--where", "cloud", "--models-dir", str(tmp_path)], capsys)) + assert data["verdict"] == "needs_build" + assert [(b["kind"], b["reason"]) for b in data["blocking"]] == [("model", "different_file")] + + +def test_remaps_and_apply_remaps_writes_a_copy(cloud, tmp_path, capsys): + original = _ui((1, "LoraLoader", ["ink.safetensors", 1.0, 1.0])) + wf = _write(tmp_path, original) + before = wf.read_bytes() + out = tmp_path / "out" / "remapped.json" + data = _ok(_run([str(wf), "--where", "cloud", "--apply-remaps", "--out", str(out)], capsys)) + assert data["verdict"] == "covered_with_remaps" + [model] = data["models"] + assert (model["status"], model["value"], model["cloud_value"]) == ( + "remap", + "ink.safetensors", + "styles/ink.safetensors", + ) + assert data["out"] == str(out.resolve()) and data["remaps_applied"] == 1 + assert wf.read_bytes() == before + assert json.loads(out.read_text())["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" + + +def test_needs_build_lists_missing_nodes_and_models_with_their_folder(cloud, tmp_path, capsys): + (tmp_path / "local" / "loras").mkdir(parents=True) + (tmp_path / "local" / "loras" / "mine.safetensors").write_bytes(b"x") + wf = _write( + tmp_path, + _ui((1, "LoraLoader", ["mine.safetensors", 1, 1]), (2, "MyCustomNode", [])), + ) + data = _ok(_run([str(wf), "--where", "cloud", "--models-dir", str(tmp_path / "local")], capsys)) + assert data["verdict"] == "needs_build" + node, model = data["blocking"] + assert node == {"kind": "node", "class_type": "MyCustomNode", "node_ids": ["2"], "reason": "not_on_cloud"} + assert model["reason"] == "not_on_cloud" and model["folder"] == "loras" + assert model["local_paths"] == [str(tmp_path / "local" / "loras" / "mine.safetensors")] + assert {n["class_type"]: n["on_cloud"] for n in data["nodes"]}["MyCustomNode"] is False + + +def test_the_persons_upload_counts_and_only_private_assets_are_asked(cloud, tmp_path, capsys): + cloud["assets"] = [{"name": "mine.safetensors", "hash": "a" * 64, "size": 1, "tags": ["models", "loras"]}] + wf = _write(tmp_path, {"1": {"class_type": "LoraLoader", "inputs": {"lora_name": "mine.safetensors"}}}) + data = _ok(_run([str(wf), "--where", "cloud"], capsys)) + assert data["format"] == "api" + assert data["verdict"] == "covered_needs_check" + assert data["models"][0]["source"] == "yours" and data["models"][0]["widget"] == "lora_name" + assert [q["include_public"] for q in cloud["requests"]] == [["false"]] + + +def test_muted_nodes_are_skipped(cloud, tmp_path, capsys): + wf = _write(tmp_path, {"nodes": [{"id": 1, "type": "MyCustomNode", "mode": 4, "widgets_values": []}]}) + data = _ok(_run([str(wf), "--where", "cloud"], capsys)) + assert data["verdict"] == "covered" + assert data["skipped"] == [{"node_id": "1", "class_type": "MyCustomNode", "why": "bypassed"}] + + +# --- refusals ---------------------------------------------------------------- + + +def _err(env: dict[str, Any], code: str) -> dict[str, Any]: + assert env["ok"] is False, env + assert env["error"]["code"] == code, env["error"] + return env["error"] + + +def test_out_without_apply_remaps_and_the_reverse(cloud, tmp_path, capsys): + wf = _write(tmp_path, _ui()) + _err(_run([str(wf), "--where", "cloud", "--out", str(tmp_path / "o.json")], capsys), "missing_argument") + _err(_run([str(wf), "--where", "cloud", "--apply-remaps"], capsys), "missing_argument") + + +def test_out_may_not_name_the_input(cloud, tmp_path, capsys): + wf = _write(tmp_path, _ui((1, "LoraLoader", ["ink.safetensors", 1, 1]))) + before = wf.read_bytes() + _err(_run([str(wf), "--where", "cloud", "--apply-remaps", "--out", str(wf)], capsys), "workflow_out_is_input") + assert wf.read_bytes() == before + + +@pytest.mark.parametrize( + ("content", "code"), + [("{not json", "workflow_invalid_json"), ('{"hello": "world"}', "workflow_format_unknown")], +) +def test_bad_files(cloud, tmp_path, capsys, content, code): + wf = tmp_path / "wf.json" + wf.write_text(content) + _err(_run([str(wf), "--where", "cloud"], capsys), code) + + +def test_missing_file(cloud, tmp_path, capsys): + _err(_run([str(tmp_path / "nope.json"), "--where", "cloud"], capsys), "workflow_not_found") + + +def test_local_target_is_refused(monkeypatch, tmp_path, capsys): + from comfy_cli.target import Target + + local = Target(kind="local", base_url="http://127.0.0.1:8188", host="127.0.0.1", port=8188) + monkeypatch.setattr("comfy_cli.target.resolve_target", lambda **kw: local) + _err(_run([str(_write(tmp_path, _ui())), "--where", "local"], capsys), "cloud_only_command") + + +def test_signed_out_is_refused(monkeypatch, tmp_path, capsys): + monkeypatch.setattr("comfy_cli.target.resolve_target", lambda **kw: _cloud_target()) + _err(_run([str(_write(tmp_path, _ui())), "--where", "cloud"], capsys), "auth_not_signed_in") + + +def test_node_list_failure(monkeypatch, tmp_path, capsys): + from comfy_cli.cql.engine import LoadError + + def boom(**kw): + raise LoadError("down") + + monkeypatch.setattr("comfy_cli.target.resolve_target", lambda **kw: _cloud_target(api_key="k")) + monkeypatch.setattr("comfy_cli.cql.loader.resilient_load_object_info", boom) + _err(_run([str(_write(tmp_path, _ui())), "--where", "cloud"], capsys), "cql_no_graph") + + +def test_model_list_failure_is_an_error_not_a_missing_model(cloud, monkeypatch, tmp_path, capsys): + def unauthorized(url, target, **kw): + raise urllib.error.HTTPError(url, 401, "Unauthorized", {}, None) + + monkeypatch.setattr("comfy_cli.command.cloud_http.http_request", unauthorized) + wf = _write(tmp_path, _ui((1, "LoraLoader", ["mine.safetensors", 1, 1]))) + error = _err(_run([str(wf), "--where", "cloud"], capsys), "cloud_unauthorized") + assert error["details"]["operation"] == "model lookup" diff --git a/tests/comfy_cli/output/test_envelope_schemas.py b/tests/comfy_cli/output/test_envelope_schemas.py index ae22891d8..5c0ff1393 100644 --- a/tests/comfy_cli/output/test_envelope_schemas.py +++ b/tests/comfy_cli/output/test_envelope_schemas.py @@ -66,6 +66,8 @@ def _validator_for(name: str) -> jsonschema.Validator: # The progress object is the deploy service's own and may grow fields, # so the event stays open where the envelopes around it are closed. "deploy_progress_event.json", + # Enums on verdict / status / identity and a oneOf for blocking rows. + "workflow_check.json", ], ) def test_schemas_are_well_formed(schema_name): diff --git a/tests/comfy_cli/test_workflow_coverage.py b/tests/comfy_cli/test_workflow_coverage.py new file mode 100644 index 000000000..0e7162733 --- /dev/null +++ b/tests/comfy_cli/test_workflow_coverage.py @@ -0,0 +1,297 @@ +"""Offline rules behind `comfy workflow check --where cloud` (comfy_cli.workflow_coverage).""" + +from __future__ import annotations + +import copy + +import pytest + +from comfy_cli import workflow_coverage as cov + +H1 = "blake3:" + "1" * 64 +H2 = "blake3:" + "2" * 64 + +OBJECT_INFO = { + "CheckpointLoaderSimple": { + "input": {"required": {"ckpt_name": [["sd15.safetensors", "sdxl/base.safetensors"], {}]}} + }, + # The newer COMBO form. + "LoraLoader": {"input": {"required": {"lora_name": ["COMBO", {"options": ["styles/ink.safetensors"]}]}}}, + "VAELoader": {"input": {"required": {"vae_name": [["a/vae.safetensors", "b/vae.safetensors"], {}]}}}, + "KSampler": {"input": {"required": {"seed": ["INT", {}]}}}, + "CLIPTextEncode": {"input": {"required": {"text": ["STRING", {}]}}}, +} + + +def _no_uploads(_name): + return [] + + +def _ui(*nodes, **extra): + return {"nodes": list(nodes), "links": [], **extra} + + +def _node(node_id, type_, values=None, mode=0, **extra): + return {"id": node_id, "type": type_, "mode": mode, "widgets_values": values or [], **extra} + + +# --- reading the workflow -------------------------------------------------- + + +def test_format_detects_ui_api_and_neither(): + assert cov.workflow_format({"nodes": []}) == "ui" + assert cov.workflow_format({"1": {"class_type": "KSampler", "inputs": {}}}) == "api" + assert cov.workflow_format({}) is None + assert cov.workflow_format([1, 2]) is None + assert cov.workflow_format({"1": {"inputs": {}}}) is None + + +def test_find_uses_ui_skips_muted_bypassed_notes_and_prompts(): + wf = _ui( + _node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]), + _node(2, "LoraLoader", ["muted.safetensors", 1.0, 1.0], mode=2), + _node(3, "LoraLoader", ["bypassed.safetensors", 1.0, 1.0], mode=4), + _node(4, "Note", ["read me.safetensors"]), + _node(5, "CLIPTextEncode", ["a photo.safetensors"]), + ) + uses = cov.find_uses(wf) + assert uses["format"] == "ui" + assert uses["nodes"] == {"CheckpointLoaderSimple": ["1"], "CLIPTextEncode": ["5"]} + assert [m["value"] for m in uses["models"]] == ["sd15.safetensors"] + assert uses["skipped"] == [ + {"node_id": "2", "class_type": "LoraLoader", "why": "muted"}, + {"node_id": "3", "class_type": "LoraLoader", "why": "bypassed"}, + ] + + +def test_find_uses_ui_reads_lora_stack_rows(): + wf = _ui(_node(1, "Power Lora Loader", [{}, {"on": True, "lora": "x.safetensors", "strength": 1}])) + assert [m["value"] for m in cov.find_uses(wf)["models"]] == ["x.safetensors"] + + +def test_find_uses_walks_subgraphs_with_scoped_ids_and_skips_muted_instances(): + sg_id = "aaaaaaaa-0000-0000-0000-000000000001" + wf = _ui( + _node(10, sg_id), + _node(11, sg_id), + _node(12, sg_id, mode=2), + definitions={"subgraphs": [{"id": sg_id, "nodes": [_node(1, "LoraLoader", ["ink.safetensors", 1, 1])]}]}, + ) + uses = cov.find_uses(wf) + assert uses["nodes"] == {"LoraLoader": ["10/1", "11/1"]} + # Two instances of one definition share one place in the file: one model row. + assert [(m["node_id"], m["value"]) for m in uses["models"]] == [("10/1", "ink.safetensors")] + assert uses["skipped"] == [{"node_id": "12", "class_type": sg_id, "why": "muted"}] + + +def test_find_uses_api_reads_inputs_and_ignores_urls(): + wf = { + "1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "sd15.safetensors"}}, + "2": {"class_type": "LoadFromUrl", "inputs": {"url": "https://example.com/m.safetensors"}}, + } + uses = cov.find_uses(wf) + assert uses["format"] == "api" + assert uses["models"] == [ + { + "node_id": "1", + "class_type": "CheckpointLoaderSimple", + "value": "sd15.safetensors", + "widget": "ckpt_name", + "loc": ("api", "1"), + } + ] + + +def test_find_uses_rejects_non_workflows(): + with pytest.raises(ValueError): + cov.find_uses({"hello": "world"}) + + +# --- placing models on Cloud ----------------------------------------------- + + +def _resolve(wf, own=_no_uploads): + return cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, own) + + +def test_combo_choices_reads_both_forms(): + assert cov.combo_choices(OBJECT_INFO["LoraLoader"]) == {"styles/ink.safetensors": "lora_name"} + assert cov.combo_choices(OBJECT_INFO["CheckpointLoaderSimple"])["sd15.safetensors"] == "ckpt_name" + assert cov.combo_choices(None) == {} + + +def test_resolve_present_remap_ambiguous_missing_and_not_checked(): + wf = _ui( + _node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]), + _node(2, "LoraLoader", ["ink.safetensors", 1, 1]), + _node(3, "VAELoader", ["vae.safetensors"]), + _node(4, "CheckpointLoaderSimple", ["nowhere.safetensors"]), + _node(5, "CustomLoader", ["custom.safetensors"]), + ) + rows = {r["node_id"]: r for r in _resolve(wf)} + assert rows["1"]["status"] == "present" and rows["1"]["source"] == "cloud" + assert rows["1"]["widget"] == "ckpt_name" + assert rows["2"]["status"] == "remap" and rows["2"]["cloud_value"] == "styles/ink.safetensors" + assert rows["3"]["status"] == "missing" + assert rows["3"]["candidates"] == ["a/vae.safetensors", "b/vae.safetensors"] + assert rows["4"]["status"] == "missing" and "candidates" not in rows["4"] + assert rows["5"]["status"] == "not_checked" + + +def test_resolve_finds_the_persons_uploads_and_asks_once_per_name(): + asked = [] + + def own(name): + asked.append(name) + return [{"name": "mine.safetensors", "hash": "1" * 64 + ".safetensors", "size": 5, "tags": ["models"]}] + + wf = _ui( + _node(1, "CheckpointLoaderSimple", ["mine.safetensors"]), + _node(2, "CheckpointLoaderSimple", ["sub/mine.safetensors"]), + ) + rows = cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, own) + assert asked == ["mine.safetensors"] + assert rows[0]["status"] == "present" and rows[0]["source"] == "yours" + assert rows[0]["cloud_hash"] == H1 and rows[0]["cloud_size"] == 5 + assert rows[1]["status"] == "remap" and rows[1]["cloud_value"] == "mine.safetensors" + + +def test_resolve_two_uploads_with_one_name_leave_the_hash_unknown(): + def own(_name): + return [{"name": "m.safetensors", "hash": H1}, {"name": "m.safetensors", "hash": H2}] + + uses = cov.find_uses(_ui(_node(1, "CheckpointLoaderSimple", ["m.safetensors"]))) + rows = cov.resolve_models(uses, OBJECT_INFO, own) + assert rows[0]["status"] == "present" and rows[0]["cloud_hash"] is None + + +def test_content_hash_normalizes_both_spellings(): + assert cov.content_hash("1" * 64 + ".safetensors") == H1 + assert cov.content_hash(H1) == H1 + assert cov.content_hash("not-a-hash") is None + assert cov.content_hash(None) is None + + +# --- same file or not ------------------------------------------------------ + + +def test_identity_needs_a_hash_match_to_say_same(): + def hashes(path): + return {"/a": H1, "/b": H2}[path] + + assert cov.identity(None, 5, H1, hashes) == ("unverified", None) + assert cov.identity([("/a", 5)], 5, H1, hashes) == ("same", "/a") + assert cov.identity([("/a", 5)], 5, H2, hashes) == ("different", "/a") + assert cov.identity([("/a", 5)], 6, H1, hashes) == ("different", "/a") # size alone proves different + assert cov.identity([("/a", 5)], 5, None, hashes) == ("unverified", "/a") + assert cov.identity([("/a", 5), ("/b", 6)], 5, H1, hashes) == ("unverified", None) + assert cov.identity([("/a", 5), ("/b", 5)], 5, H1, hashes) == ("unverified", None) + + +def test_local_index_finds_model_files_by_name(tmp_path): + (tmp_path / "loras").mkdir() + (tmp_path / "loras" / "ink.safetensors").write_bytes(b"abc") + (tmp_path / "notes.txt").write_text("x") + index = cov.local_index([str(tmp_path)]) + assert index == {"ink.safetensors": [(str(tmp_path / "loras" / "ink.safetensors"), 3)]} + + +# --- where a missing file goes --------------------------------------------- + + +def test_add_sources_prefers_the_workflows_download_notes(tmp_path): + wf = _ui( + _node( + 1, + "UNETLoader", + ["flux.safetensors", "default"], + properties={"models": [{"name": "flux.safetensors", "url": "https://x/flux", "directory": "unet"}]}, + ), + _node(2, "LoraLoader", ["sub/ink.safetensors", 1, 1]), + _node(3, "SomeCustomLoader", ["odd.safetensors"]), + ) + rows = cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, _no_uploads) + cov.add_sources(rows, wf, {"ink.safetensors": [("/m/loras/ink.safetensors", 3)]}) + by_id = {r["node_id"]: r for r in rows} + assert by_id["1"]["folder"] == "unet" and by_id["1"]["url"] == "https://x/flux" + assert by_id["2"]["filename"] == "ink.safetensors" and by_id["2"]["folder"] == "loras" + assert by_id["2"]["local_paths"] == ["/m/loras/ink.safetensors"] + assert by_id["3"]["folder"] is None and by_id["3"]["local_paths"] == [] + + +def test_add_sources_uses_the_api_input_name(): + wf = {"1": {"class_type": "Anything", "inputs": {"vae_name": "v.safetensors"}}} + rows = cov.resolve_models(cov.find_uses(wf), {}, _no_uploads) + cov.add_sources(rows, wf, None) + assert rows[0]["folder"] == "vae" and rows[0]["local_paths"] == [] + + +# --- the answer ------------------------------------------------------------- + + +def test_blocking_lists_missing_nodes_then_models_with_where_to_find_them(): + wf = _ui( + _node(1, "CustomLoader", ["custom.safetensors"]), + _node(2, "VAELoader", ["vae.safetensors"]), + _node(3, "CheckpointLoaderSimple", ["gone.safetensors"]), + ) + uses = cov.find_uses(wf) + rows = cov.resolve_models(uses, OBJECT_INFO, _no_uploads) + cov.add_sources(rows, wf, None) + blocked = cov.blocking(uses, OBJECT_INFO, rows) + assert blocked[0] == {"kind": "node", "class_type": "CustomLoader", "node_ids": ["1"], "reason": "not_on_cloud"} + assert [(b["value"], b["reason"]) for b in blocked[1:]] == [ + ("vae.safetensors", "ambiguous"), + ("gone.safetensors", "not_on_cloud"), + ] + assert blocked[2]["folder"] == "checkpoints" and blocked[2]["filename"] == "gone.safetensors" + + +def test_blocking_flags_a_proven_different_file(): + uses = cov.find_uses(_ui(_node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + rows = cov.resolve_models(uses, OBJECT_INFO, _no_uploads) + rows[0]["identity"] = "different" + assert [b["reason"] for b in cov.blocking(uses, OBJECT_INFO, rows)] == ["different_file"] + + +SAME = {"status": "present", "identity": "same"} +UNPROVEN = {"status": "present", "identity": "unverified"} +REMAP = {"status": "remap", "identity": "same"} + + +@pytest.mark.parametrize( + ("blocked", "models", "expected"), + [ + ([], [], "covered"), + ([], [SAME], "covered"), + ([], [SAME, UNPROVEN], "covered_needs_check"), + ([], [REMAP, UNPROVEN], "covered_with_remaps"), + ([{"kind": "node"}], [REMAP], "needs_build"), + ], +) +def test_verdict_precedence(blocked, models, expected): + assert cov.verdict(blocked, models) == expected + + +def test_apply_remaps_ui_changes_only_the_copy(): + sg_id = "aaaaaaaa-0000-0000-0000-000000000002" + wf = _ui( + _node(1, "LoraLoader", ["ink.safetensors", 1, 1]), + _node(2, sg_id), + definitions={"subgraphs": [{"id": sg_id, "nodes": [_node(7, "LoraLoader", ["ink.safetensors", 1, 1])]}]}, + ) + before = copy.deepcopy(wf) + rows = _resolve(wf) + out, changed = cov.apply_remaps(wf, rows) + assert wf == before + assert changed == 2 + assert out["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" + assert out["definitions"]["subgraphs"][0]["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" + + +def test_apply_remaps_api_sets_the_input(): + wf = {"1": {"class_type": "LoraLoader", "inputs": {"lora_name": "ink.safetensors", "strength_model": 1}}} + out, changed = cov.apply_remaps(wf, _resolve(wf)) + assert changed == 1 + assert out["1"]["inputs"] == {"lora_name": "styles/ink.safetensors", "strength_model": 1} + assert wf["1"]["inputs"]["lora_name"] == "ink.safetensors" From c46114ebf216bb6e4d605bc403c4c1cc3fa3ac19 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 5 Nov 2026 00:57:08 -0500 Subject: [PATCH 2/5] fix(workflow): judge Cloud library models by size; they carry no content hash Cloud's /api/assets reports a library model's folder and file name as `hash`. Such a model with a local copy is now compared by size only: a different size blocks as different_file, the same size stays unverified, and the local file is not hashed. Tests, docs and the ADR say so. --- comfy_cli/workflow_coverage.py | 8 +++-- ...overage-by-name-and-prove-files-by-hash.md | 13 +++++++ docs/json-output.md | 6 ++++ .../comfy_cli/command/test_workflow_check.py | 34 +++++++++++++++++++ tests/comfy_cli/test_workflow_coverage.py | 11 ++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/comfy_cli/workflow_coverage.py b/comfy_cli/workflow_coverage.py index cc5d3f0df..8e1e77c0e 100644 --- a/comfy_cli/workflow_coverage.py +++ b/comfy_cli/workflow_coverage.py @@ -188,7 +188,11 @@ def combo_choices(class_info: Any) -> dict[str, str]: def content_hash(value: Any) -> str | None: - """Cloud reports a hash as ``blake3:`` or as the stored ``.``; normalize to the first.""" + """Cloud reports a hash as ``blake3:`` or as the stored ``.``; normalize to the first. + + Anything else is no hash: for its own library models Cloud puts the folder and + file name there (``checkpoints/sd_xl_turbo_1.0_fp16.safetensors``). + """ match = _HASH_RE.fullmatch(value) if isinstance(value, str) else None return f"blake3:{match[1]}" if match else None @@ -277,7 +281,7 @@ def identity( path, size = local[0] if cloud_size is not None and cloud_size != size: return "different", path - if cloud_hash is None: + if cloud_hash is None: # Cloud's library models report a path as `hash`: nothing to compare, so no hashing return "unverified", path digests = {hash_file(p) for p, _ in local} if len(digests) > 1: diff --git a/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md b/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md index 00f8d8b26..dd284749a 100644 --- a/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md +++ b/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md @@ -50,6 +50,15 @@ only when a local copy under `--models-dir` has the blake3 hash Cloud reports. A size mismatch alone proves `different`. Without a local copy, or when several local copies disagree, the model is `unverified`. +Cloud's library models carry no content hash. Measured on 2026-11-05, +`/api/assets` returns their folder and file name as `hash` +(`checkpoints/sd_xl_turbo_1.0_fp16.safetensors`) and the byte count as `size`. +For them the check compares sizes only and never hashes the local file: a +different size is `different_file`, the same size stays `unverified`. A +workflow that uses a library model is at best `covered_needs_check`, and the +proof is one real job on Cloud. The hash proof applies to the person's own +uploads. + A remap happens only when exactly one Cloud file has the name. The check never writes its input: `--apply-remaps --out ` writes a remapped copy, and an `--out` that names the input is refused. @@ -90,6 +99,8 @@ it, a model the person uploaded would be reported missing. - Without `--models-dir`, a workflow that uses any model is at best `covered_needs_check`. +- A workflow that uses any of Cloud's library models is at best + `covered_needs_check`, with or without `--models-dir`. - Folder names for custom loaders are unknown unless the workflow's download notes give them. @@ -112,5 +123,7 @@ leaves the input unchanged, and each refusal. Each output validates against - Cloud's node list starts listing the person's uploads in loader dropdowns: the model-list read can go. +- Cloud's `/api/assets` reports a content hash for its library models: the + hash proof then reaches them, and `covered` becomes reachable for them. - Cloud publishes a per-file hash in its node list: identity could be checked without the extra model-list read per model. diff --git a/docs/json-output.md b/docs/json-output.md index 30423ca3e..566d88546 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -780,6 +780,12 @@ export. Muted and bypassed nodes load nothing and are listed in `skipped`. A model is proven the same file only when a local copy under `--models-dir` has the blake3 hash Cloud reports for its copy. The same name is not the same bytes. Without `--models-dir`, a model Cloud has is `unverified`. +Only your own uploads carry a content hash. Cloud's library models (`source: "cloud"`) report their folder and file +name where the hash would be, so for them a local copy is compared by size alone: a different size is +`different` (blocking, `different_file`), the same size stays `unverified`, and the local file is not hashed. A +workflow that uses a library model is therefore at best `covered_needs_check`; running one real job on Cloud is the +proof. + Each `models[]` row: | Field | Type | Description | diff --git a/tests/comfy_cli/command/test_workflow_check.py b/tests/comfy_cli/command/test_workflow_check.py index 0c97e3899..932bcc407 100644 --- a/tests/comfy_cli/command/test_workflow_check.py +++ b/tests/comfy_cli/command/test_workflow_check.py @@ -144,6 +144,40 @@ def test_a_different_local_file_blocks(cloud, tmp_path, capsys): assert [(b["kind"], b["reason"]) for b in data["blocking"]] == [("model", "different_file")] +@pytest.mark.parametrize( + ("size", "identity", "verdict"), + [ + (len(SD15), "unverified", "covered_needs_check"), + (len(SD15) + 1, "different", "needs_build"), + ], +) +def test_a_library_model_with_a_path_for_hash_is_judged_by_size( + cloud, monkeypatch, tmp_path, capsys, size, identity, verdict +): + """Measured on Cloud 2026-11-05: its library models report `hash` as folder/filename.""" + + def never(path): + raise AssertionError(f"hashed {path} with nothing to compare it to") + + monkeypatch.setattr("comfy_cli.hashing.blake3_file", never) + (tmp_path / "sd15.safetensors").write_bytes(SD15) + cloud["assets"] = [ + { + "name": "sd15.safetensors", + "hash": "checkpoints/sd15.safetensors", + "size": size, + "tags": ["models", "checkpoints"], + "public": True, + } + ] + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + data = _ok(_run([str(wf), "--where", "cloud", "--models-dir", str(tmp_path)], capsys)) + assert data["models"][0]["identity"] == identity + assert data["verdict"] == verdict + if identity == "different": + assert [b["reason"] for b in data["blocking"]] == ["different_file"] + + def test_remaps_and_apply_remaps_writes_a_copy(cloud, tmp_path, capsys): original = _ui((1, "LoraLoader", ["ink.safetensors", 1.0, 1.0])) wf = _write(tmp_path, original) diff --git a/tests/comfy_cli/test_workflow_coverage.py b/tests/comfy_cli/test_workflow_coverage.py index 0e7162733..1d595c539 100644 --- a/tests/comfy_cli/test_workflow_coverage.py +++ b/tests/comfy_cli/test_workflow_coverage.py @@ -169,6 +169,9 @@ def test_content_hash_normalizes_both_spellings(): assert cov.content_hash("1" * 64 + ".safetensors") == H1 assert cov.content_hash(H1) == H1 assert cov.content_hash("not-a-hash") is None + # Cloud's library models carry their folder and file name in `hash`. + assert cov.content_hash("checkpoints/x.safetensors") is None + assert cov.content_hash("checkpoints/" + "1" * 64 + ".safetensors") is None assert cov.content_hash(None) is None @@ -188,6 +191,14 @@ def hashes(path): assert cov.identity([("/a", 5), ("/b", 5)], 5, H1, hashes) == ("unverified", None) +def test_identity_without_a_cloud_hash_uses_size_and_never_hashes(): + def never(path): + raise AssertionError(f"hashed {path} with nothing to compare it to") + + assert cov.identity([("/a", 5)], 5, None, never) == ("unverified", "/a") + assert cov.identity([("/a", 5)], 6, None, never) == ("different", "/a") + + def test_local_index_finds_model_files_by_name(tmp_path): (tmp_path / "loras").mkdir() (tmp_path / "loras" / "ink.safetensors").write_bytes(b"abc") From f92efbfcd01bc4f00cde436187a216877703c228 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 5 Nov 2026 01:30:42 -0500 Subject: [PATCH 3/5] workflow check: read saves as ComfyUI submits them, match uploads as Cloud does - An editor save goes through the save-to-API converter, so a subgraph instance's own promoted value is the one checked, one row per instance (: ids). --apply-remaps rewrites the value where it is stored: the remapped loader type, subgraph instances and quarantined host values. - Whether an input names a file comes from its input spec (a dropdown does, STRING does not), not from a node type list, so prompt text is never read as a model in either format. - Add .pt2 and .pkl to the model extensions. - A local copy that cannot be read leaves the model unverified with a local_model_unreadable warning instead of an internal error. - A cached node list sets stale: true and adds an object_info_stale warning. - Read every page of the model list. - Match an upload kept under a folder by Cloud's suffix rule; several are ambiguous. - Redact --models-dir and --out paths from analytics. - A save the converter refuses is workflow_conversion_failed. - Rename the ADR to COVERAGE-0010 (the old id read as a ticket id). --- comfy_cli/command/workflow_check.py | 88 +++-- comfy_cli/error_codes.py | 4 +- comfy_cli/schemas/workflow_check.json | 24 +- comfy_cli/tracking.py | 4 + comfy_cli/workflow_coverage.py | 310 +++++++++++++----- ...verage-by-name-and-prove-files-by-hash.md} | 37 ++- docs/json-output.md | 27 +- .../comfy_cli/command/test_workflow_check.py | 73 ++++- tests/comfy_cli/test_tracking.py | 11 + tests/comfy_cli/test_workflow_coverage.py | 189 ++++++++++- 10 files changed, 615 insertions(+), 152 deletions(-) rename docs/adr/{CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md => COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md} (73%) diff --git a/comfy_cli/command/workflow_check.py b/comfy_cli/command/workflow_check.py index ab2eedb4a..3540a9a1a 100644 --- a/comfy_cli/command/workflow_check.py +++ b/comfy_cli/command/workflow_check.py @@ -18,6 +18,7 @@ from comfy_cli.output import get_renderer _ASSET_PAGE = 200 +_ASSET_PAGES = 50 # 10,000 rows: a server that never says it is done cannot loop forever _SUMMARY = { "covered": "Runs on Comfy Cloud as it is.", @@ -81,24 +82,38 @@ def _load(renderer, workflow: str) -> tuple[Path, Any]: def _fetch_model_assets(target, name: str, *, include_public: bool) -> list[dict[str, Any]]: - """Model assets whose name contains ``name``. Raises on any failure; callers decide what it means.""" + """Every model asset whose name contains ``name``, all pages of it. + + ``name_contains`` is a substring match, so the exact file can sit past the + first page; an answer from part of the list could call a model missing. + Raises on any failure, a list that never ends included; callers decide what + it means. + """ import urllib.parse from comfy_cli.command.cloud_http import http_request - query = urllib.parse.urlencode( - { - "include_tags": "models", - "include_public": "true" if include_public else "false", - "name_contains": name, - "limit": _ASSET_PAGE, - } - ) - _, body = http_request(target.url("assets") + "?" + query, target, strict_json=True) - rows = body.get("assets") if isinstance(body, dict) else None - if not isinstance(rows, list): - raise ValueError("unexpected response shape from /api/assets (expected an object with assets[])") - return [row for row in rows if isinstance(row, dict) and "models" in (row.get("tags") or [])] + found: list[dict[str, Any]] = [] + offset = 0 + for _ in range(_ASSET_PAGES): + query = urllib.parse.urlencode( + { + "include_tags": "models", + "include_public": "true" if include_public else "false", + "name_contains": name, + "limit": _ASSET_PAGE, + "offset": offset, + } + ) + _, body = http_request(target.url("assets") + "?" + query, target, strict_json=True) + rows = body.get("assets") if isinstance(body, dict) else None + if not isinstance(rows, list): + raise ValueError("unexpected response shape from /api/assets (expected an object with assets[])") + found.extend(row for row in rows if isinstance(row, dict) and "models" in (row.get("tags") or [])) + if not rows or not body.get("has_more"): + return found + offset += len(rows) + raise ValueError(f"/api/assets kept paging past {_ASSET_PAGES * _ASSET_PAGE} models named like {name!r}") def _own_models(renderer, target): @@ -236,6 +251,7 @@ def check_cmd( from comfy_cli.cql.loader import resilient_load_object_info from comfy_cli.file_utils import atomic_write_text from comfy_cli.hashing import blake3_file + from comfy_cli.workflow_to_api import WorkflowConversionError renderer = get_renderer() if apply_remaps != (out is not None): @@ -266,8 +282,11 @@ def check_cmd( hint="run `comfy cloud login`", ) raise typer.Exit(code=1) + stale: dict[str, str] = {} try: - object_info = resilient_load_object_info(mode="cloud") + object_info = resilient_load_object_info( + mode="cloud", on_stale=lambda source, reason: stale.update(source=source, reason=reason) + ) except LoadError as e: renderer.error( code="cql_no_graph", @@ -276,20 +295,46 @@ def check_cmd( ) raise typer.Exit(code=1) from e - uses = cov.find_uses(data) + try: + uses = cov.find_uses(data, object_info) + except WorkflowConversionError as e: + renderer.error( + code="workflow_conversion_failed", + message=f"Could not read the workflow the way ComfyUI submits it: {e}", + hint="open it in ComfyUI and save it again, or check its API export (Workflow > Export (API))", + details={"path": str(path)}, + ) + raise typer.Exit(code=1) from e models = cov.resolve_models(uses, object_info, _own_models(renderer, target)) - warnings: list[dict[str, str]] = [] + warnings: list[dict[str, Any]] = [] + if stale: + # Cloud's node list could not be refreshed: an older copy answered, and a + # node removed since may still read as available. + warnings.append( + { + "code": "object_info_stale", + "message": "Cloud's node list could not be refreshed; a cached copy answered " + f"({stale['source']}): {stale['reason']}", + "details": dict(stale), + } + ) roots = list(models_dir or []) for root in roots: if not Path(root).expanduser().is_dir(): warnings.append({"code": "models_dir_not_found", "message": f"--models-dir is not a folder: {root}"}) index = cov.local_index(roots) if roots else None cloud_files: dict[str, tuple[int | None, str | None]] = {} - digests: dict[str, str] = {} + digests: dict[str, str | None] = {} - def hash_file(file: str) -> str: + def hash_file(file: str) -> str | None: if file not in digests: - digests[file] = blake3_file(Path(file)) + try: + digests[file] = blake3_file(Path(file)) + except OSError as e: # unreadable, or gone since the folder was listed + digests[file] = None + warnings.append( + {"code": "local_model_unreadable", "message": f"could not read {file} to compare it: {e}"} + ) return digests[file] cov.add_sources(models, data, index) @@ -322,12 +367,13 @@ def hash_file(file: str) -> str: raise typer.Exit(code=1) from e for model in models: - for key in ("loc", "cloud_size", "cloud_hash"): + for key in ("cloud_size", "cloud_hash"): model.pop(key, None) payload: dict[str, Any] = { "workflow": str(path), "format": uses["format"], "verdict": cov.verdict(blocked, models), + "stale": bool(stale), "blocking": blocked, "models": models, "nodes": [ diff --git a/comfy_cli/error_codes.py b/comfy_cli/error_codes.py index 2e3846667..387e467c1 100644 --- a/comfy_cli/error_codes.py +++ b/comfy_cli/error_codes.py @@ -925,8 +925,8 @@ class ErrorCode: ), ErrorCode( "workflow_conversion_failed", - "`comfy workflow decompose` could not flatten a frontend-format workflow to " - "API format (malformed graph, or object_info that doesn't match the nodes).", + "`comfy workflow decompose` or `comfy workflow check` could not flatten a frontend-format workflow " + "to API format (malformed graph, or object_info that doesn't match the nodes).", "re-export from ComfyUI, or pass a matching --input object_info.json", ), ErrorCode( diff --git a/comfy_cli/schemas/workflow_check.json b/comfy_cli/schemas/workflow_check.json index 5650b1492..99db9f20a 100644 --- a/comfy_cli/schemas/workflow_check.json +++ b/comfy_cli/schemas/workflow_check.json @@ -8,6 +8,7 @@ "workflow", "format", "verdict", + "stale", "blocking", "models", "nodes", @@ -34,6 +35,10 @@ "workflow": {"type": "string", "description": "Absolute path of the checked file; never changed."}, "format": {"enum": ["ui", "api"]}, "verdict": {"enum": ["covered", "covered_with_remaps", "covered_needs_check", "needs_build"]}, + "stale": { + "type": "boolean", + "description": "true when Cloud's node list could not be refreshed and a cached copy answered (warning object_info_stale): a node type removed since may read as on Cloud." + }, "blocking": { "type": "array", "items": { @@ -85,15 +90,19 @@ "local_paths" ], "properties": { - "node_id": {"type": "string", "description": "Node id; inside a subgraph, /."}, + "node_id": {"type": "string", "description": "Node id; inside a subgraph, :, one row per instance."}, "class_type": {"type": "string"}, - "widget": {"type": ["string", "null"], "description": "Input name, when known."}, + "widget": {"type": ["string", "null"], "description": "Input name; null when the node type is not on Cloud and its saved values carry no names."}, "value": {"type": "string", "description": "The value saved in the workflow."}, "status": {"enum": ["present", "remap", "missing", "not_checked"]}, "source": {"enum": ["cloud", "yours", null]}, "cloud_value": {"type": ["string", "null"], "description": "The value Cloud runs; differs from value on a remap."}, "identity": {"enum": ["same", "different", "unverified", null]}, - "candidates": {"type": "array", "items": {"type": "string"}}, + "candidates": { + "type": "array", + "items": {"type": "string"}, + "description": "The names that fit when several do; the model is then missing and blocking says ambiguous." + }, "filename": {"$ref": "#/$defs/filename"}, "folder": {"$ref": "#/$defs/folder"}, "url": {"$ref": "#/$defs/url"}, @@ -132,7 +141,14 @@ "items": { "type": "object", "required": ["code", "message"], - "properties": {"code": {"type": "string"}, "message": {"type": "string"}} + "properties": { + "code": { + "type": "string", + "description": "object_info_stale, models_dir_not_found, local_model_unreadable or cloud_model_lookup_failed." + }, + "message": {"type": "string"}, + "details": {"type": "object"} + } } } } diff --git a/comfy_cli/tracking.py b/comfy_cli/tracking.py index 4abecff6b..310af3154 100644 --- a/comfy_cli/tracking.py +++ b/comfy_cli/tracking.py @@ -125,6 +125,10 @@ def _resolve_posthog_token() -> str: # `workflow add-node --text`: a note body is verbatim user content, like # `--prompt` — analytics only learns that the option was supplied. "text", + # `--models-dir` (repeatable, so a list: the whole list is redacted) and + # `--out` are local paths, typed `str` like `--workflow`. + "models_dir", + "out", } ) diff --git a/comfy_cli/workflow_coverage.py b/comfy_cli/workflow_coverage.py index 8e1e77c0e..99b4c25c3 100644 --- a/comfy_cli/workflow_coverage.py +++ b/comfy_cli/workflow_coverage.py @@ -6,15 +6,16 @@ - :func:`find_uses` reads a workflow (the editor format with ``nodes[]``, subgraphs included, or an API export) and lists the node types and model files its active nodes use. Muted and bypassed nodes load nothing, so they are listed as skipped. + An editor save is read through the save-to-API converter, so a subgraph + instance's own value for a promoted widget is the one checked, as the frontend + submits it. Whether an input names a file is read from the node type's input + spec in Cloud's node list, never from a list of node types. - :func:`resolve_models` places each model on Cloud: in Cloud's own dropdown, among the person's uploaded models, as a remap to the one Cloud file of the same name in another folder, or missing. - :func:`identity` says whether a local copy is proven to be the same file as Cloud's: the same name is not the same bytes. - :func:`verdict` and :func:`apply_remaps` turn that into the answer and the copy. - -Lifted from the Cloud inventory check in the run-on-cloud draft, cut to what -this one question needs. """ from __future__ import annotations @@ -25,7 +26,21 @@ from collections.abc import Callable, Iterator, Mapping from typing import Any -MODEL_EXTENSIONS = (".safetensors", ".ckpt", ".pt", ".pth", ".bin", ".gguf", ".onnx", ".sft") +# ComfyUI's own list (folder_paths.supported_pt_extensions), plus the formats +# custom loaders read. +MODEL_EXTENSIONS = ( + ".safetensors", + ".ckpt", + ".pt", + ".pt2", + ".pth", + ".bin", + ".pkl", + ".sft", + ".gguf", + ".onnx", + ".torchscript", +) # Node modes the executor skips: these nodes load nothing. SKIPPED_MODES = {2: "muted", 4: "bypassed"} @@ -66,19 +81,53 @@ def _basename(value: str) -> str: return value.replace("\\", "/").rsplit("/", 1)[-1] +def _model_strings(value: Any) -> list[str]: + """The file names in one widget value: the value itself, or the values of a + row such as a LoRA stack's ``{"lora": "...", "on": true}``.""" + if is_model_value(value): + return [value] + if isinstance(value, dict): + return [v for v in value.values() if is_model_value(v)] + return [] + + def _ui_model_strings(node: Mapping[str, Any]) -> list[str]: - if str(node.get("type", "")).startswith("CLIPTextEncode"): - return [] # a prompt can end in ".safetensors" and still be a prompt values = node.get("widgets_values") if isinstance(values, dict): values = list(values.values()) - found: list[str] = [] - for value in values if isinstance(values, list) else []: - if is_model_value(value): - found.append(value) - elif isinstance(value, dict): # LoRA stacks keep rows such as {"lora": "...", "on": true} - found.extend(v for v in value.values() if is_model_value(v)) - return found + return [found for value in (values if isinstance(values, list) else []) for found in _model_strings(value)] + + +def _quarantined_host_values(node: Mapping[str, Any]) -> list[dict[str, Any]]: + """A subgraph instance's legacy promoted values: the frontend reads these before ``widgets_values``.""" + props = node.get("properties") + raw = props.get("proxyWidgetErrorQuarantine") if isinstance(props, dict) else None + return [e for e in raw if isinstance(e, dict) and "hostValue" in e] if isinstance(raw, list) else [] + + +def _declared_spec(class_info: Any, name: str) -> Any: + inputs = class_info.get("input") if isinstance(class_info, dict) else None + for section in ("required", "optional"): + group = inputs.get(section) if isinstance(inputs, dict) else None + if isinstance(group, dict) and name in group: + return group[name] + return None + + +def names_a_file(class_info: Any, name: str) -> bool: + """Whether input ``name`` of a node type can hold a model file name, from its input spec. + + A dropdown (a choice list, ``COMBO`` or a dynamic combo) can. A typed input + such as ``STRING`` cannot: a prompt that ends in ``.safetensors`` is still a + prompt. An input the spec does not declare (the node type is not on Cloud, or + the input is a dynamic row such as a LoRA stack's) can, and is judged by its + value alone. + """ + spec = _declared_spec(class_info, name) + if not isinstance(spec, (list, tuple)) or not spec: + return True + kind = spec[0] + return isinstance(kind, (list, tuple)) or (isinstance(kind, str) and "COMBO" in kind) def _subgraphs(workflow: Mapping[str, Any]) -> list[Any]: @@ -87,41 +136,41 @@ def _subgraphs(workflow: Mapping[str, Any]) -> list[Any]: return subgraphs if isinstance(subgraphs, list) else [] -def _ui_nodes(workflow: Mapping[str, Any]) -> Iterator[tuple[str, Mapping[str, Any], tuple]]: - """Every node an instance reaches, with its scoped id and where it lives in the file. +def _ui_nodes(workflow: Mapping[str, Any]) -> Iterator[tuple[str, Mapping[str, Any]]]: + """Every node an instance reaches, with its scoped id. - A subgraph instance's interior nodes get ids ``/``. ``loc`` is - ``("nodes", i)`` or ``("subgraph", j, i)``: two instances of one definition share - one ``loc``, which is what a remap rewrites. + A subgraph instance's interior nodes get ids ``:``, the ids the + save-to-API converter and the frontend give them. """ - by_id = { - sg["id"]: (j, sg) - for j, sg in enumerate(_subgraphs(workflow)) - if isinstance(sg, dict) and isinstance(sg.get("id"), str) - } + by_id = {sg["id"]: sg for sg in _subgraphs(workflow) if isinstance(sg, dict) and isinstance(sg.get("id"), str)} - def visit(nodes, prefix: str, base: tuple, ancestors: tuple): - for i, node in enumerate(nodes if isinstance(nodes, list) else []): + def visit(nodes, prefix: str, ancestors: tuple): + for node in nodes if isinstance(nodes, list) else []: if not isinstance(node, dict): continue - node_id = f"{prefix}/{node.get('id')}" if prefix else str(node.get("id")) - yield node_id, node, (*base, i) + node_id = f"{prefix}:{node.get('id')}" if prefix else str(node.get("id")) + yield node_id, node definition = by_id.get(node["type"]) if isinstance(node.get("type"), str) else None if definition is None or node.get("mode") in SKIPPED_MODES or node.get("type") in ancestors: continue - j, sg = definition - yield from visit(sg.get("nodes"), node_id, ("subgraph", j), (*ancestors, node.get("type"))) + yield from visit(definition.get("nodes"), node_id, (*ancestors, node.get("type"))) - yield from visit(workflow.get("nodes"), "", ("nodes",), ()) + yield from visit(workflow.get("nodes"), "", ()) -def find_uses(workflow: Mapping[str, Any]) -> dict[str, Any]: +def find_uses(workflow: Mapping[str, Any], object_info: Mapping[str, Any]) -> dict[str, Any]: """The node types and model files a workflow's active nodes use. Returns ``{"format", "nodes": {class_type: [node_id, ...]}, "models": [...], "skipped": [...]}``. Each model row carries ``node_id``, ``class_type``, - ``value``, ``widget`` (the input name in an API export, else ``None``) and an - internal ``loc`` that :func:`apply_remaps` uses and the command drops. + ``value`` and ``widget`` (the input that holds it, or ``None`` when unknown). + + An editor save is converted the way it is submitted (subgraphs expanded, each + instance's promoted values applied), so every instance of a subgraph gets its + own rows. A node the converter cannot read, or whose type is not on Cloud, is + read from its saved widget values instead. Raises ``ValueError`` for a file + that is neither format and :class:`~comfy_cli.workflow_to_api.WorkflowConversionError` + for an editor save the converter refuses. """ fmt = workflow_format(workflow) if fmt is None: @@ -129,28 +178,43 @@ def find_uses(workflow: Mapping[str, Any]) -> dict[str, Any]: nodes: dict[str, list[str]] = {} models: list[dict[str, Any]] = [] skipped: list[dict[str, Any]] = [] - seen: set[tuple[Any, ...]] = set() + + def add(node_id: str, class_type: str, inputs: Mapping[str, Any], saved: set[str] | None = None) -> None: + class_info = object_info.get(class_type) + for name, value in inputs.items(): + if not names_a_file(class_info, name): + continue + for found in _model_strings(value): + # The converter fills an input the save left out with Cloud's first + # choice; only a file the workflow itself names is reported. + if saved is None or found.lower() in saved: + models.append({"node_id": node_id, "class_type": class_type, "value": found, "widget": name}) if fmt == "api": for node_id, node in workflow.items(): class_type = node["class_type"] nodes.setdefault(class_type, []).append(str(node_id)) - inputs = node.get("inputs") if isinstance(node.get("inputs"), dict) else {} - for name, value in inputs.items(): - if is_model_value(value): - models.append( - { - "node_id": str(node_id), - "class_type": class_type, - "value": value, - "widget": name, - "loc": ("api", str(node_id)), - } - ) + add(str(node_id), class_type, node.get("inputs") if isinstance(node.get("inputs"), dict) else {}) return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} + from comfy_cli.workflow_to_api import convert_ui_to_api + + links = workflow.get("links") + prompt = convert_ui_to_api( + {**workflow, "links": links if isinstance(links, list) else []}, + object_info if isinstance(object_info, dict) else dict(object_info), + ) + walked = list(_ui_nodes(workflow)) + saved = { + found.lower() + for _, node in walked + for found in [ + *_ui_model_strings(node), + *(v for e in _quarantined_host_values(node) for v in _model_strings(e["hostValue"])), + ] + } subgraph_ids = {sg["id"] for sg in _subgraphs(workflow) if isinstance(sg, dict) and isinstance(sg.get("id"), str)} - for node_id, node, loc in _ui_nodes(workflow): + for node_id, node in walked: class_type = node.get("type") if node.get("mode") in SKIPPED_MODES: # a muted subgraph's interior is never walked skipped.append({"node_id": node_id, "class_type": class_type, "why": SKIPPED_MODES[node["mode"]]}) @@ -158,11 +222,12 @@ def find_uses(workflow: Mapping[str, Any]) -> dict[str, Any]: if not isinstance(class_type, str) or class_type in UI_ONLY_NODES or class_type in subgraph_ids: continue nodes.setdefault(class_type, []).append(node_id) - for value in _ui_model_strings(node): - if (loc, value) in seen: # a second instance of the same subgraph - continue - seen.add((loc, value)) - models.append({"node_id": node_id, "class_type": class_type, "value": value, "widget": None, "loc": loc}) + entry = prompt.get(node_id) if class_type in object_info else None + if isinstance(entry, dict) and isinstance(entry.get("inputs"), dict): + add(node_id, class_type, entry["inputs"], saved) + else: + for found in _ui_model_strings(node): + models.append({"node_id": node_id, "class_type": class_type, "value": found, "widget": None}) return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} @@ -197,6 +262,30 @@ def content_hash(value: Any) -> str | None: return f"blake3:{match[1]}" if match else None +def _pick_upload(value: str, uploads: list[dict[str, Any]]) -> tuple[str, str | None, list[str]]: + """Where Cloud finds ``value`` among the person's uploads: ``(status, name, candidates)``. + + Cloud's own rule when it runs the workflow: an upload named exactly the saved + value, else the one upload whose name ends in ``/`` (an upload + kept under a folder, such as ``Wan22_FunReward/model.safetensors``). Both run + the saved value unchanged, so both are ``present``. Several names ending that + way are ambiguous and Cloud refuses to guess. Failing those, the one upload + with the same file name under another path is a ``remap``. + """ + names = sorted({a["name"] for a in uploads if isinstance(a.get("name"), str)}) + if value in names: + return "present", value, [] + under_a_folder = [n for n in names if n.endswith("/" + value)] + if len(under_a_folder) == 1: + return "present", under_a_folder[0], [] + if under_a_folder: + return "missing", None, under_a_folder + same_name = [n for n in names if _basename(n) == _basename(value)] + if len(same_name) == 1: + return "remap", same_name[0], [] + return "missing", None, same_name + + def resolve_models( uses: Mapping[str, Any], object_info: Mapping[str, Any], @@ -204,15 +293,15 @@ def resolve_models( ) -> list[dict[str, Any]]: """Place each model on Cloud. - ``own_models(basename)`` returns the person's uploaded model assets whose name - contains ``basename``. It is asked only about models Cloud's dropdowns do not - list, once per name. + ``own_models(basename)`` returns every one of the person's uploaded model + assets whose name contains ``basename``. It is asked only about models Cloud's + dropdowns do not list, once per name. ``status`` is ``present`` (Cloud runs the saved value), ``remap`` (exactly one Cloud file has the same name in another folder: the saved value must change to ``cloud_value``), ``missing``, or ``not_checked`` when the node type itself is not on Cloud. ``source`` is ``cloud`` for Cloud's own models and ``yours`` for - the person's uploads. + the person's uploads. ``candidates`` lists the names when several fit. """ asked: dict[str, list[dict[str, Any]]] = {} rows: list[dict[str, Any]] = [] @@ -226,7 +315,6 @@ def resolve_models( "status": "missing", "source": None, "cloud_value": None, - "loc": use["loc"], } rows.append(row) if class_type not in object_info: @@ -247,20 +335,23 @@ def resolve_models( continue if name not in asked: asked[name] = [a for a in own_models(name) if isinstance(a, dict)] - mine = [a for a in asked[name] if a.get("name") in (value, name)] - exact = [a for a in mine if a.get("name") == value] or mine - if not exact: + status, picked, candidates = _pick_upload(value, asked[name]) + if picked is None: + if candidates: + row["candidates"] = candidates continue - asset = exact[0] + assets = [a for a in asked[name] if a.get("name") == picked] + sizes = {a.get("size") for a in assets} + # Two uploads under one name: which one runs is not ours to say, so neither is proof. + hashes = {content_hash(a.get("hash")) for a in assets} + size = sizes.pop() if len(sizes) == 1 else None row.update( - status="present" if asset["name"] == value else "remap", + status=status, source="yours", - cloud_value=asset["name"], - cloud_hash=content_hash(asset.get("hash")), - cloud_size=asset.get("size") if isinstance(asset.get("size"), int) else None, + cloud_value=picked, + cloud_hash=hashes.pop() if len(hashes) == 1 else None, + cloud_size=size if isinstance(size, int) else None, ) - if len({content_hash(a.get("hash")) for a in exact}) > 1: - row["cloud_hash"] = None # two uploads share the name: which one runs is not ours to say return rows @@ -268,13 +359,14 @@ def identity( local: list[tuple[str, int]] | None, cloud_size: int | None, cloud_hash: str | None, - hash_file: Callable[[str], str], + hash_file: Callable[[str], str | None], ) -> tuple[str, str | None]: """``same``, ``different`` or ``unverified``, and the local path compared. ``local`` is every local file with the model's name, as ``(path, size)``; ``None`` when no models folder was given. Only a blake3 match proves ``same``; - a size mismatch alone proves ``different``. + a size mismatch alone proves ``different``. ``hash_file`` returns ``None`` for + a file it could not read, which leaves the model ``unverified``. """ if not local or len({size for _, size in local}) > 1: return "unverified", None @@ -284,7 +376,7 @@ def identity( if cloud_hash is None: # Cloud's library models report a path as `hash`: nothing to compare, so no hashing return "unverified", path digests = {hash_file(p) for p, _ in local} - if len(digests) > 1: + if None in digests or len(digests) > 1: return "unverified", None return ("same" if digests == {cloud_hash} else "different"), path @@ -346,7 +438,7 @@ def download_notes(workflow: Mapping[str, Any]) -> dict[str, dict[str, str | Non if isinstance(workflow.get("models"), list): entries.extend(workflow["models"]) if workflow_format(workflow) == "ui": - for _, node, _ in _ui_nodes(workflow): + for _, node in _ui_nodes(workflow): props = node.get("properties") if isinstance(props, dict) and isinstance(props.get("models"), list): entries.extend(props["models"]) @@ -422,28 +514,64 @@ def verdict(blocked: list[dict], models: list[dict[str, Any]]) -> str: return "covered" +def _replace(values: Any, old: str, new: str) -> int: + """Replace ``old`` with ``new`` in one node's widget values, rows included.""" + changed = 0 + items = list(values.items()) if isinstance(values, dict) else list(enumerate(values or [])) + for key, value in items: + if value == old: + values[key] = new + changed += 1 + elif isinstance(value, dict): + for inner, inner_value in list(value.items()): + if inner_value == old: + value[inner] = new + changed += 1 + return changed + + def apply_remaps(workflow: Mapping[str, Any], models: list[dict[str, Any]]) -> tuple[dict[str, Any], int]: - """A copy of ``workflow`` with each remap's value replaced; the original is not touched.""" + """A copy of ``workflow`` with each remap's value replaced; the original is not touched. + + An API export changes the remapped input of that node. An editor save changes + the value wherever it is stored: on the nodes of the remapped type (top level + and inside every subgraph definition) and on subgraph instances, which hold a + promoted widget's value, legacy ``proxyWidgetErrorQuarantine`` entries included. + """ out = copy.deepcopy(workflow) + remaps = [m for m in models if m["status"] == "remap"] changed = 0 - for model in models: - if model["status"] != "remap": - continue - loc, old, new = model["loc"], model["value"], model["cloud_value"] - if loc[0] == "api": - out[loc[1]]["inputs"][model["widget"]] = new - changed += 1 - continue - node = out["nodes"][loc[1]] if loc[0] == "nodes" else out["definitions"]["subgraphs"][loc[1]]["nodes"][loc[2]] - values = node.get("widgets_values") - items = list(values.items()) if isinstance(values, dict) else list(enumerate(values or [])) - for key, value in items: - if value == old: - values[key] = new - changed += 1 - elif isinstance(value, dict): - for inner, inner_value in list(value.items()): - if inner_value == old: - value[inner] = new + if workflow_format(out) == "api": + for model in remaps: + node = out.get(model["node_id"]) + inputs = node.get("inputs") if isinstance(node, dict) else None + if isinstance(inputs, dict): + changed += _replace(inputs, model["value"], model["cloud_value"]) + return out, changed + + by_value: dict[str, set[str]] = {} + for model in remaps: + by_value.setdefault(model["value"], set()).add(model["cloud_value"]) + # One saved value remapped two ways (two loader types, two folders) has no + # single replacement: it is left alone rather than guessed. + targets: dict[str, tuple[str, set[str]]] = {} + for old, new in by_value.items(): + if len(new) == 1: + targets[old] = (next(iter(new)), {m["class_type"] for m in remaps if m["value"] == old}) + subgraph_ids = {sg["id"] for sg in _subgraphs(out) if isinstance(sg, dict) and isinstance(sg.get("id"), str)} + every_node = [n for n in out.get("nodes") or [] if isinstance(n, dict)] + for sg in _subgraphs(out): + if isinstance(sg, dict) and isinstance(sg.get("nodes"), list): + every_node.extend(n for n in sg["nodes"] if isinstance(n, dict)) + for node in every_node: + instance = node.get("type") in subgraph_ids + for old, (new, class_types) in targets.items(): + if not instance and node.get("type") not in class_types: + continue + changed += _replace(node.get("widgets_values"), old, new) + if instance: + for entry in _quarantined_host_values(node): + if entry["hostValue"] == old: + entry["hostValue"] = new changed += 1 return out, changed diff --git a/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md b/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md similarity index 73% rename from docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md rename to docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md index dd284749a..e4cba7324 100644 --- a/docs/adr/CHECK-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md +++ b/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md @@ -1,4 +1,4 @@ -# ADR-CHECK-0010: Judge Cloud Coverage by Name and Prove Files by Hash +# ADR-COVERAGE-0010: Judge Cloud Coverage by Name and Prove Files by Hash Date: 2026-11-05 @@ -7,7 +7,9 @@ Date: 2026-11-05 Proposed Relation: Builds on ADR-AUTOMATION-0003. `comfy workflow check` emits one -`envelope/1` envelope with its own `workflow_check` data schema. +`envelope/1` envelope with its own `workflow_check` data schema. Follows +ADR-CONVERSION-0004: a save is read through the save-to-API converter, and no +node type gets a rule of its own. ## Context @@ -45,10 +47,29 @@ list. It answers with one of four verdicts, checked in this order: A remap outranks an unproven model: the unchanged file does not run while a remap is due. +The check reads what Cloud would run. A ComfyUI save goes through the +save-to-API converter (`convert_ui_to_api`), so subgraphs are expanded and each +instance's own value for a promoted widget is the one checked, with one row per +instance (`:`). Whether an input holds a file comes from +the node type's input spec in Cloud's node list: a dropdown (`COMBO` or a +choice list) can, a typed input such as `STRING` cannot. A node type Cloud does +not have is read from its saved values by file extension. The converter fills +an input the save left out with Cloud's first choice; only values the save +itself names are reported. + +The person's uploads are matched by Cloud's own rule when it runs a workflow: +the upload named exactly the saved value, else the one upload whose name ends +in `/`. Both run unchanged and are `present`; several such +uploads are ambiguous. Every page of the model list is read. + +When Cloud's node list cannot be refreshed and a cached copy answers, the +output says so in a `stale` field and an `object_info_stale` warning. + Presence is judged by name. Identity is judged by bytes: a model is `same` only when a local copy under `--models-dir` has the blake3 hash Cloud reports. A size mismatch alone proves `different`. Without a local copy, or when -several local copies disagree, the model is `unverified`. +several local copies disagree, or a local copy cannot be read, the model is +`unverified`. Cloud's library models carry no content hash. Measured on 2026-11-05, `/api/assets` returns their folder and file name as `hash` @@ -113,10 +134,12 @@ it, a model the person uploaded would be reported missing. ## Validation `tests/comfy_cli/test_workflow_coverage.py` covers the offline rules: muted and -bypassed nodes, subgraphs, both dropdown forms, remaps, shared names, uploads, -identity and the verdict order. `tests/comfy_cli/command/test_workflow_check.py` -covers each verdict end to end against a mocked Cloud, that `--apply-remaps` -leaves the input unchanged, and each refusal. Each output validates against +bypassed nodes, subgraphs and promoted values (on a shipped template), the +input-spec rule for what names a file, both dropdown forms, remaps, shared +names, uploads under a folder, identity and the verdict order. `tests/comfy_cli/command/test_workflow_check.py` +covers each verdict end to end against a mocked Cloud, paging, a stale node +list, an unreadable local copy, that `--apply-remaps` leaves the input +unchanged, and each refusal. Each output validates against `comfy_cli/schemas/workflow_check.json`. ## Revisit triggers diff --git a/docs/json-output.md b/docs/json-output.md index 566d88546..403ce0c54 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -752,20 +752,24 @@ unconvertible UI export) emit an [error object](#error-object) instead. Cloud run this workflow with no custom build? It needs a sign-in, reads Cloud's node list and, for a model Cloud's dropdowns do not list, your uploaded models. The file is a ComfyUI save (with `nodes[]`, subgraphs included) or an API -export. Muted and bypassed nodes load nothing and are listed in `skipped`. +export. Muted and bypassed nodes load nothing and are listed in `skipped`. A save is read the way ComfyUI submits it, +through the same converter as `comfy workflow decompose`: each subgraph instance's own value for a promoted widget is +the one checked. Whether an input holds a model file comes from its input spec in Cloud's node list: a dropdown does, a +text input does not, so a prompt that ends in `.safetensors` is not a model. | Field | Type | Description | | ---------------- | ------------- | --------------------------------------------------------------------------------------------------- | | `workflow` | str | Absolute path of the checked file. The check never changes it | | `format` | str | `ui` (a ComfyUI save) or `api` (an API export) | | `verdict` | str | `covered`, `covered_with_remaps`, `covered_needs_check` or `needs_build` (below) | +| `stale` | bool | `true` when Cloud's node list could not be refreshed and a cached copy answered (see `warnings`) | | `blocking` | array of dict | What stops the workflow running on Cloud; empty unless `needs_build` | | `models` | array of dict | Every model file the active nodes load, one row each | | `nodes` | array of dict | `{class_type, node_ids, on_cloud}` for each node type used | | `skipped` | array of dict | `{node_id, class_type, why}` for each muted or bypassed node; `why` is `muted` or `bypassed` | | `out` | str or null | Where `--apply-remaps --out` wrote the remapped copy | | `remaps_applied` | number | Values changed in that copy; `0` without `--out` | -| `warnings` | array of dict | `{code, message}`: `models_dir_not_found`, `cloud_model_lookup_failed` | +| `warnings` | array of dict | `{code, message, details?}`: `object_info_stale` (`details` has `source` and `reason`), `models_dir_not_found`, `local_model_unreadable`, `cloud_model_lookup_failed` | `verdict`, checked in this order: @@ -778,7 +782,13 @@ export. Muted and bypassed nodes load nothing and are listed in `skipped`. - `covered`: every node type and model is on Cloud, and each model is proven the same file. A model is proven the same file only when a local copy under `--models-dir` has the blake3 hash Cloud reports for -its copy. The same name is not the same bytes. Without `--models-dir`, a model Cloud has is `unverified`. +its copy. The same name is not the same bytes. Without `--models-dir`, a model Cloud has is `unverified`. A local +copy that cannot be read stays `unverified` and adds a `local_model_unreadable` warning. + +Your uploads are matched the way Cloud matches them when it runs the workflow: the upload named exactly the saved +value, else the one upload whose name ends in `/` (such as `Wan22_FunReward/model.safetensors` for a +saved `model.safetensors`). Both run the saved value unchanged, so both are `present`. Several uploads ending that +way are `missing` with `candidates` (blocking, `ambiguous`). Every page of your model list is read. Only your own uploads carry a content hash. Cloud's library models (`source: "cloud"`) report their folder and file name where the hash would be, so for them a local copy is compared by size alone: a different size is @@ -790,15 +800,15 @@ Each `models[]` row: | Field | Type | Description | | ------------- | ------------- | ------------------------------------------------------------------------------------------------- | -| `node_id` | str | Node id; inside a subgraph, `/` | +| `node_id` | str | Node id; inside a subgraph, `:`, one row per instance | | `class_type` | str | The node type | -| `widget` | str or null | The input name, when known (always in an API export) | +| `widget` | str or null | The input name; null only for a node type Cloud does not have | | `value` | str | The value saved in the workflow | | `status` | str | `present`, `remap`, `missing`, or `not_checked` when the node type itself is not on Cloud | | `source` | str or null | `cloud` for Cloud's own models, `yours` for your uploads | | `cloud_value` | str or null | The value Cloud runs; differs from `value` on a remap | | `identity` | str or null | `same`, `different` or `unverified` for a model Cloud has; null otherwise | -| `candidates` | array of str | Present when several Cloud files share the name, so no remap is picked | +| `candidates` | array of str | Present when several Cloud files or uploads fit the name, so none is picked | | `filename` | str | The file name, without folders | | `folder` | str or null | The ComfyUI models folder it belongs in (`checkpoints`, `loras`, ...), from the workflow's own download notes or the stock loader; null when unknown | | `url` | str or null | The download link from the workflow's own download notes, if it has one | @@ -814,8 +824,9 @@ Each `blocking[]` item has a `kind`: Every verdict exits `0`: the check is a report. `changed` is `true` only when `--out` wrote a file. Refusals emit an [error object](#error-object): `missing_argument` (`--apply-remaps` without `--out`, or the reverse), `workflow_out_is_input`, `workflow_not_found`, `workflow_read_error`, `workflow_invalid_json`, -`workflow_format_unknown`, `cloud_only_command`, `auth_not_signed_in`, `cql_no_graph` (Cloud's node list could not -be read), `cloud_unauthorized` / `cloud_http_error` (the model list could not be read) and `workflow_write_error`. +`workflow_format_unknown`, `workflow_conversion_failed` (the save could not be converted the way ComfyUI submits +it), `cloud_only_command`, `auth_not_signed_in`, `cql_no_graph` (Cloud's node list could not be read), +`cloud_unauthorized` / `cloud_http_error` (the model list could not be read) and `workflow_write_error`. ## Error object diff --git a/tests/comfy_cli/command/test_workflow_check.py b/tests/comfy_cli/command/test_workflow_check.py index 932bcc407..6538716b8 100644 --- a/tests/comfy_cli/command/test_workflow_check.py +++ b/tests/comfy_cli/command/test_workflow_check.py @@ -77,7 +77,8 @@ def fake_http(url, target, **kw): public = query["include_public"] == ["true"] needle = query["name_contains"][0] rows = [a for a in state["assets"] if needle in a["name"] and (public or not a.get("public"))] - return 200, {"assets": rows, "has_more": False} + offset, limit = int(query.get("offset", ["0"])[0]), int(query["limit"][0]) + return 200, {"assets": rows[offset : offset + limit], "has_more": offset + limit < len(rows)} monkeypatch.setattr("comfy_cli.command.cloud_http.http_request", fake_http) return state @@ -222,6 +223,64 @@ def test_the_persons_upload_counts_and_only_private_assets_are_asked(cloud, tmp_ assert [q["include_public"] for q in cloud["requests"]] == [["false"]] +def test_a_model_past_the_first_page_of_the_lookup_is_found(cloud, tmp_path, capsys): + """`name_contains` is a substring match: the exact file can sit on a later page.""" + lookalikes = [ + {"name": f"v{i}-mine.safetensors", "hash": "b" * 64, "size": 1, "tags": ["models"]} for i in range(250) + ] + cloud["assets"] = [*lookalikes, {"name": "mine.safetensors", "hash": "a" * 64, "size": 1, "tags": ["models"]}] + wf = _write(tmp_path, {"1": {"class_type": "LoraLoader", "inputs": {"lora_name": "mine.safetensors"}}}) + data = _ok(_run([str(wf), "--where", "cloud"], capsys)) + assert (data["models"][0]["status"], data["models"][0]["source"]) == ("present", "yours") + assert [q["offset"] for q in cloud["requests"]] == [["0"], ["200"]] + + +def test_a_lookup_that_never_ends_is_an_error(cloud, monkeypatch, tmp_path, capsys): + def endless(url, target, **kw): + return 200, {"assets": [{"name": "x-mine.safetensors", "tags": ["models"]}], "has_more": True} + + monkeypatch.setattr("comfy_cli.command.cloud_http.http_request", endless) + wf = _write(tmp_path, _ui((1, "LoraLoader", ["mine.safetensors", 1, 1]))) + env = _run([str(wf), "--where", "cloud"], capsys) + assert env["ok"] is False + assert env["error"]["details"]["operation"] == "model lookup" + + +def test_a_cached_node_list_is_flagged_stale(cloud, monkeypatch, tmp_path, capsys): + def cached(**kw): + kw["on_stale"]("https://cloud.example.com", "timed out") + return OBJECT_INFO + + monkeypatch.setattr("comfy_cli.cql.loader.resilient_load_object_info", cached) + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + data = _ok(_run([str(wf), "--where", "cloud"], capsys)) + assert data["stale"] is True + [warning] = data["warnings"] + assert warning["code"] == "object_info_stale" + assert warning["details"] == {"source": "https://cloud.example.com", "reason": "timed out"} + + +def test_a_fresh_node_list_is_not_stale(cloud, tmp_path, capsys): + data = _ok(_run([str(_write(tmp_path, _ui())), "--where", "cloud"], capsys)) + assert data["stale"] is False and data["warnings"] == [] + + +def test_an_unreadable_local_copy_leaves_the_model_unverified(cloud, monkeypatch, tmp_path, capsys): + def unreadable(path): + raise PermissionError(13, "Permission denied", str(path)) + + monkeypatch.setattr("comfy_cli.hashing.blake3_file", unreadable) + (tmp_path / "sd15.safetensors").write_bytes(SD15) + cloud["assets"] = [ + {"name": "sd15.safetensors", "hash": "f" * 64, "size": len(SD15), "tags": ["models"], "public": True} + ] + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + data = _ok(_run([str(wf), "--where", "cloud", "--models-dir", str(tmp_path)], capsys)) + assert data["models"][0]["identity"] == "unverified" + assert data["verdict"] == "covered_needs_check" + assert [w["code"] for w in data["warnings"]] == ["local_model_unreadable"] + + def test_muted_nodes_are_skipped(cloud, tmp_path, capsys): wf = _write(tmp_path, {"nodes": [{"id": 1, "type": "MyCustomNode", "mode": 4, "widgets_values": []}]}) data = _ok(_run([str(wf), "--where", "cloud"], capsys)) @@ -261,6 +320,18 @@ def test_bad_files(cloud, tmp_path, capsys, content, code): _err(_run([str(wf), "--where", "cloud"], capsys), code) +def test_a_workflow_the_converter_refuses(cloud, monkeypatch, tmp_path, capsys): + from comfy_cli.workflow_to_api import WorkflowConversionError + + def refuse(_workflow, _object_info): + raise WorkflowConversionError("subgraph expansion exceeded its safe limit") + + monkeypatch.setattr("comfy_cli.workflow_to_api.convert_ui_to_api", refuse) + wf = _write(tmp_path, _ui((1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + error = _err(_run([str(wf), "--where", "cloud"], capsys), "workflow_conversion_failed") + assert error["details"]["path"] == str(wf.resolve()) + + def test_missing_file(cloud, tmp_path, capsys): _err(_run([str(tmp_path / "nope.json"), "--where", "cloud"], capsys), "workflow_not_found") diff --git a/tests/comfy_cli/test_tracking.py b/tests/comfy_cli/test_tracking.py index 78858ef13..e489ee746 100644 --- a/tests/comfy_cli/test_tracking.py +++ b/tests/comfy_cli/test_tracking.py @@ -681,6 +681,17 @@ def test_snapshot_path_presence_is_still_reported(self, tracking_module): assert "from_workflow" not in filter_command_kwargs({"name": "demo"}) assert filter_command_kwargs({"from_workflow": "/Users/someone/wf.json"})["from_workflow"] == "" + def test_local_model_folders_and_out_paths_are_redacted(self, tracking_module): + """`workflow check --models-dir` is a repeatable `str` option (a list) and + `--out` a `str` path: both are JSON-serializable, so both would ship verbatim.""" + from comfy_cli.tracking import filter_command_kwargs + + props = filter_command_kwargs( + {"models_dir": ["/Users/someone/Private Clients/models"], "out": "/Users/someone/acme/wf.json"} + ) + assert props == {"models_dir": "", "out": ""} + assert filter_command_kwargs({"models_dir": None, "out": None}) == {"models_dir": None, "out": None} + def test_underscore_ctx_is_excluded(self, tracking_module): import click diff --git a/tests/comfy_cli/test_workflow_coverage.py b/tests/comfy_cli/test_workflow_coverage.py index 1d595c539..59f69157c 100644 --- a/tests/comfy_cli/test_workflow_coverage.py +++ b/tests/comfy_cli/test_workflow_coverage.py @@ -3,10 +3,15 @@ from __future__ import annotations import copy +import json +from pathlib import Path import pytest from comfy_cli import workflow_coverage as cov +from comfy_cli.workflow_to_api import WorkflowConversionError + +_FIXTURES = Path(__file__).resolve().parent / "fixtures" H1 = "blake3:" + "1" * 64 H2 = "blake3:" + "2" * 64 @@ -20,6 +25,9 @@ "VAELoader": {"input": {"required": {"vae_name": [["a/vae.safetensors", "b/vae.safetensors"], {}]}}}, "KSampler": {"input": {"required": {"seed": ["INT", {}]}}}, "CLIPTextEncode": {"input": {"required": {"text": ["STRING", {}]}}}, + # Not stock nodes: what makes an input a file is its spec, not the node type. + "CaptionWriter": {"input": {"required": {"caption": ["STRING", {"multiline": True}]}}}, + "OddLoader": {"input": {"required": {"weights": [["odd/w.pkl"], {}]}}}, } @@ -31,6 +39,14 @@ def _ui(*nodes, **extra): return {"nodes": list(nodes), "links": [], **extra} +def _subgraph(sg_id, *nodes): + return {"subgraphs": [{"id": sg_id, "nodes": list(nodes), "links": [], "inputs": [], "outputs": []}]} + + +def _uses(wf, object_info=None): + return cov.find_uses(wf, OBJECT_INFO if object_info is None else object_info) + + def _node(node_id, type_, values=None, mode=0, **extra): return {"id": node_id, "type": type_, "mode": mode, "widgets_values": values or [], **extra} @@ -54,7 +70,7 @@ def test_find_uses_ui_skips_muted_bypassed_notes_and_prompts(): _node(4, "Note", ["read me.safetensors"]), _node(5, "CLIPTextEncode", ["a photo.safetensors"]), ) - uses = cov.find_uses(wf) + uses = _uses(wf) assert uses["format"] == "ui" assert uses["nodes"] == {"CheckpointLoaderSimple": ["1"], "CLIPTextEncode": ["5"]} assert [m["value"] for m in uses["models"]] == ["sd15.safetensors"] @@ -66,7 +82,7 @@ def test_find_uses_ui_skips_muted_bypassed_notes_and_prompts(): def test_find_uses_ui_reads_lora_stack_rows(): wf = _ui(_node(1, "Power Lora Loader", [{}, {"on": True, "lora": "x.safetensors", "strength": 1}])) - assert [m["value"] for m in cov.find_uses(wf)["models"]] == ["x.safetensors"] + assert [m["value"] for m in _uses(wf)["models"]] == ["x.safetensors"] def test_find_uses_walks_subgraphs_with_scoped_ids_and_skips_muted_instances(): @@ -75,21 +91,77 @@ def test_find_uses_walks_subgraphs_with_scoped_ids_and_skips_muted_instances(): _node(10, sg_id), _node(11, sg_id), _node(12, sg_id, mode=2), - definitions={"subgraphs": [{"id": sg_id, "nodes": [_node(1, "LoraLoader", ["ink.safetensors", 1, 1])]}]}, + definitions=_subgraph(sg_id, _node(1, "LoraLoader", ["ink.safetensors", 1, 1])), ) - uses = cov.find_uses(wf) - assert uses["nodes"] == {"LoraLoader": ["10/1", "11/1"]} - # Two instances of one definition share one place in the file: one model row. - assert [(m["node_id"], m["value"]) for m in uses["models"]] == [("10/1", "ink.safetensors")] + uses = _uses(wf) + # The converter's and the frontend's ids, so a row names the node that runs. + assert uses["nodes"] == {"LoraLoader": ["10:1", "11:1"]} + # Each instance can hold its own promoted value, so each gets its own row. + assert [(m["node_id"], m["value"], m["widget"]) for m in uses["models"]] == [ + ("10:1", "ink.safetensors", "lora_name"), + ("11:1", "ink.safetensors", "lora_name"), + ] assert uses["skipped"] == [{"node_id": "12", "class_type": sg_id, "why": "muted"}] +def _promoted_music_workflow(): + """A shipped template whose subgraph instance (node 37) promotes the interior + UNETLoader's ``unet_name``; the instance's own value is ``widgets_values[4]``.""" + wf = json.loads((_FIXTURES / "gallery" / "audio_minimax_music_3.json").read_text(encoding="utf-8")) + object_info = json.loads((_FIXTURES / "object_info_subgraph_promoted.json").read_text(encoding="utf-8")) + host = next(n for n in wf["nodes"] if n["id"] == 37) + host["widgets_values"][4] = "mine.safetensors" + return wf, host, object_info + + +def _unet_rows(wf, object_info): + return [m for m in cov.find_uses(wf, object_info)["models"] if m["class_type"] == "UNETLoader"] + + +def test_find_uses_checks_the_instances_promoted_value_not_the_interior_default(): + wf, _host, object_info = _promoted_music_workflow() + assert [(m["node_id"], m["value"], m["widget"]) for m in _unet_rows(wf, object_info)] == [ + ("37:6", "mine.safetensors", "unet_name") + ] + + +def test_apply_remaps_rewrites_the_promoted_value_on_the_instance(): + wf, host, object_info = _promoted_music_workflow() + [row] = _unet_rows(wf, object_info) + row.update(status="remap", cloud_value="music/mine.safetensors") + out, changed = cov.apply_remaps(wf, [row]) + assert changed == 1 + assert next(n for n in out["nodes"] if n["id"] == 37)["widgets_values"][4] == "music/mine.safetensors" + assert host["widgets_values"][4] == "mine.safetensors" # the original is untouched + # The interior default is not what runs, and it is left as saved. + interior = [n for sg in out["definitions"]["subgraphs"] for n in sg["nodes"] if n["type"] == "UNETLoader"] + assert interior[0]["widgets_values"][0] == "minimax_music3_dit_fp16.safetensors" + # The copy now submits the remapped value. + assert [m["value"] for m in _unet_rows(out, object_info)] == ["music/mine.safetensors"] + + +def test_apply_remaps_rewrites_a_quarantined_host_value(): + sg_id = "aaaaaaaa-0000-0000-0000-000000000003" + quarantine = [{"originalEntry": ["-1", "lora_name"], "hostValue": "ink.safetensors"}] + wf = _ui( + _node(10, sg_id, properties={"proxyWidgetErrorQuarantine": quarantine}), + definitions=_subgraph(sg_id, _node(1, "LoraLoader", ["other.safetensors", 1, 1])), + ) + row = {"status": "remap", "class_type": "LoraLoader", "value": "ink.safetensors"} + out, changed = cov.apply_remaps(wf, [{**row, "cloud_value": "styles/ink.safetensors"}]) + assert changed == 1 + entry = out["nodes"][0]["properties"]["proxyWidgetErrorQuarantine"][0] + assert entry["hostValue"] == "styles/ink.safetensors" + assert out["definitions"]["subgraphs"][0]["nodes"][0]["widgets_values"][0] == "other.safetensors" + assert quarantine[0]["hostValue"] == "ink.safetensors" + + def test_find_uses_api_reads_inputs_and_ignores_urls(): wf = { "1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "sd15.safetensors"}}, "2": {"class_type": "LoadFromUrl", "inputs": {"url": "https://example.com/m.safetensors"}}, } - uses = cov.find_uses(wf) + uses = _uses(wf) assert uses["format"] == "api" assert uses["models"] == [ { @@ -97,21 +169,67 @@ def test_find_uses_api_reads_inputs_and_ignores_urls(): "class_type": "CheckpointLoaderSimple", "value": "sd15.safetensors", "widget": "ckpt_name", - "loc": ("api", "1"), } ] +def test_find_uses_api_does_not_read_prompt_text_as_a_model(): + wf = { + "1": {"class_type": "CLIPTextEncode", "inputs": {"text": "a photo.safetensors", "clip": ["2", 1]}}, + "2": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "sd15.safetensors"}}, + } + assert [m["value"] for m in _uses(wf)["models"]] == ["sd15.safetensors"] + + +@pytest.mark.parametrize("fmt", ["ui", "api"]) +def test_whether_an_input_names_a_file_comes_from_its_spec_not_the_node_type(fmt): + """No list of prompt node types: a STRING input is text, a dropdown names a file.""" + if fmt == "ui": + wf = _ui(_node(1, "CaptionWriter", ["a cat.ckpt"]), _node(2, "OddLoader", ["w.pkl"])) + else: + wf = { + "1": {"class_type": "CaptionWriter", "inputs": {"caption": "a cat.ckpt"}}, + "2": {"class_type": "OddLoader", "inputs": {"weights": "w.pkl"}}, + } + assert [(m["node_id"], m["value"], m["widget"]) for m in _uses(wf)["models"]] == [("2", "w.pkl", "weights")] + + +def test_names_a_file_reads_the_input_spec(): + assert cov.names_a_file(OBJECT_INFO["CheckpointLoaderSimple"], "ckpt_name") + assert cov.names_a_file(OBJECT_INFO["LoraLoader"], "lora_name") # the COMBO form + assert not cov.names_a_file(OBJECT_INFO["CLIPTextEncode"], "text") + assert not cov.names_a_file(OBJECT_INFO["KSampler"], "seed") + assert cov.names_a_file(OBJECT_INFO["CLIPTextEncode"], "lora_1") # undeclared: judged by its value + assert cov.names_a_file(None, "anything") # a node type not on Cloud + + +@pytest.mark.parametrize("name", ["m.pkl", "m.pt2", "m.torchscript", "M.SAFETENSORS"]) +def test_every_model_format_comfyui_loads_is_found(name, tmp_path): + uses = _uses(_ui(_node(1, "SomeCustomLoader", [name]))) + assert [m["value"] for m in uses["models"]] == [name] + (tmp_path / name).write_bytes(b"x") + assert list(cov.local_index([str(tmp_path)])) == [name] + + +def test_find_uses_passes_on_a_converter_refusal(monkeypatch): + def refuse(_workflow, _object_info): + raise WorkflowConversionError("subgraph expansion exceeded its safe limit") + + monkeypatch.setattr("comfy_cli.workflow_to_api.convert_ui_to_api", refuse) + with pytest.raises(WorkflowConversionError): + _uses(_ui(_node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + + def test_find_uses_rejects_non_workflows(): with pytest.raises(ValueError): - cov.find_uses({"hello": "world"}) + _uses({"hello": "world"}) # --- placing models on Cloud ----------------------------------------------- def _resolve(wf, own=_no_uploads): - return cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, own) + return cov.resolve_models(_uses(wf), OBJECT_INFO, own) def test_combo_choices_reads_both_forms(): @@ -149,7 +267,7 @@ def own(name): _node(1, "CheckpointLoaderSimple", ["mine.safetensors"]), _node(2, "CheckpointLoaderSimple", ["sub/mine.safetensors"]), ) - rows = cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, own) + rows = cov.resolve_models(_uses(wf), OBJECT_INFO, own) assert asked == ["mine.safetensors"] assert rows[0]["status"] == "present" and rows[0]["source"] == "yours" assert rows[0]["cloud_hash"] == H1 and rows[0]["cloud_size"] == 5 @@ -160,11 +278,42 @@ def test_resolve_two_uploads_with_one_name_leave_the_hash_unknown(): def own(_name): return [{"name": "m.safetensors", "hash": H1}, {"name": "m.safetensors", "hash": H2}] - uses = cov.find_uses(_ui(_node(1, "CheckpointLoaderSimple", ["m.safetensors"]))) + uses = _uses(_ui(_node(1, "CheckpointLoaderSimple", ["m.safetensors"]))) rows = cov.resolve_models(uses, OBJECT_INFO, own) assert rows[0]["status"] == "present" and rows[0]["cloud_hash"] is None +def test_resolve_finds_an_upload_kept_under_a_folder_as_cloud_does(): + """Cloud runs a saved bare name against the one upload whose name ends in ``/``.""" + + def own(_name): + return [{"name": "Wan22_FunReward/model.safetensors", "hash": H1, "size": 5}] + + rows = _resolve(_ui(_node(1, "CheckpointLoaderSimple", ["model.safetensors"])), own) + assert rows[0]["status"] == "present" and rows[0]["source"] == "yours" + assert rows[0]["cloud_value"] == "Wan22_FunReward/model.safetensors" + assert rows[0]["cloud_hash"] == H1 + + +def test_resolve_two_uploads_under_folders_are_ambiguous(): + def own(_name): + return [{"name": "a/model.safetensors"}, {"name": "b/model.safetensors"}] + + uses = _uses(_ui(_node(1, "CheckpointLoaderSimple", ["model.safetensors"]))) + rows = cov.resolve_models(uses, OBJECT_INFO, own) + assert rows[0]["status"] == "missing" + assert rows[0]["candidates"] == ["a/model.safetensors", "b/model.safetensors"] + assert [b["reason"] for b in cov.blocking(uses, OBJECT_INFO, rows)] == ["ambiguous"] + + +def test_pick_upload_prefers_the_exact_name_then_the_folder_suffix_then_a_remap(): + exact = [{"name": "model.safetensors"}, {"name": "a/model.safetensors"}] + assert cov._pick_upload("model.safetensors", exact) == ("present", "model.safetensors", []) + other_path = [{"name": "x/model.safetensors"}] + assert cov._pick_upload("y/model.safetensors", other_path) == ("remap", "x/model.safetensors", []) + assert cov._pick_upload("model.safetensors", []) == ("missing", None, []) + + def test_content_hash_normalizes_both_spellings(): assert cov.content_hash("1" * 64 + ".safetensors") == H1 assert cov.content_hash(H1) == H1 @@ -191,6 +340,10 @@ def hashes(path): assert cov.identity([("/a", 5), ("/b", 5)], 5, H1, hashes) == ("unverified", None) +def test_identity_is_unverified_when_the_local_file_cannot_be_read(): + assert cov.identity([("/a", 5)], 5, H1, lambda _path: None) == ("unverified", None) + + def test_identity_without_a_cloud_hash_uses_size_and_never_hashes(): def never(path): raise AssertionError(f"hashed {path} with nothing to compare it to") @@ -221,7 +374,7 @@ def test_add_sources_prefers_the_workflows_download_notes(tmp_path): _node(2, "LoraLoader", ["sub/ink.safetensors", 1, 1]), _node(3, "SomeCustomLoader", ["odd.safetensors"]), ) - rows = cov.resolve_models(cov.find_uses(wf), OBJECT_INFO, _no_uploads) + rows = cov.resolve_models(_uses(wf), OBJECT_INFO, _no_uploads) cov.add_sources(rows, wf, {"ink.safetensors": [("/m/loras/ink.safetensors", 3)]}) by_id = {r["node_id"]: r for r in rows} assert by_id["1"]["folder"] == "unet" and by_id["1"]["url"] == "https://x/flux" @@ -232,7 +385,7 @@ def test_add_sources_prefers_the_workflows_download_notes(tmp_path): def test_add_sources_uses_the_api_input_name(): wf = {"1": {"class_type": "Anything", "inputs": {"vae_name": "v.safetensors"}}} - rows = cov.resolve_models(cov.find_uses(wf), {}, _no_uploads) + rows = cov.resolve_models(_uses(wf, {}), {}, _no_uploads) cov.add_sources(rows, wf, None) assert rows[0]["folder"] == "vae" and rows[0]["local_paths"] == [] @@ -246,7 +399,7 @@ def test_blocking_lists_missing_nodes_then_models_with_where_to_find_them(): _node(2, "VAELoader", ["vae.safetensors"]), _node(3, "CheckpointLoaderSimple", ["gone.safetensors"]), ) - uses = cov.find_uses(wf) + uses = _uses(wf) rows = cov.resolve_models(uses, OBJECT_INFO, _no_uploads) cov.add_sources(rows, wf, None) blocked = cov.blocking(uses, OBJECT_INFO, rows) @@ -259,7 +412,7 @@ def test_blocking_lists_missing_nodes_then_models_with_where_to_find_them(): def test_blocking_flags_a_proven_different_file(): - uses = cov.find_uses(_ui(_node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) + uses = _uses(_ui(_node(1, "CheckpointLoaderSimple", ["sd15.safetensors"]))) rows = cov.resolve_models(uses, OBJECT_INFO, _no_uploads) rows[0]["identity"] = "different" assert [b["reason"] for b in cov.blocking(uses, OBJECT_INFO, rows)] == ["different_file"] @@ -289,7 +442,7 @@ def test_apply_remaps_ui_changes_only_the_copy(): wf = _ui( _node(1, "LoraLoader", ["ink.safetensors", 1, 1]), _node(2, sg_id), - definitions={"subgraphs": [{"id": sg_id, "nodes": [_node(7, "LoraLoader", ["ink.safetensors", 1, 1])]}]}, + definitions=_subgraph(sg_id, _node(7, "LoraLoader", ["ink.safetensors", 1, 1])), ) before = copy.deepcopy(wf) rows = _resolve(wf) From 184443899ab1f8d06a3547db1a8114d5eeea3d73 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 5 Nov 2026 01:52:56 -0500 Subject: [PATCH 4/5] fix(workflow-check): write each remap only at the input that was checked --- comfy_cli/command/workflow_check.py | 16 +- comfy_cli/schemas/workflow_check.json | 2 +- comfy_cli/workflow_coverage.py | 218 +++++++++++++----- docs/json-output.md | 7 +- .../comfy_cli/command/test_workflow_check.py | 54 +++++ tests/comfy_cli/test_workflow_coverage.py | 99 ++++++-- 6 files changed, 315 insertions(+), 81 deletions(-) diff --git a/comfy_cli/command/workflow_check.py b/comfy_cli/command/workflow_check.py index 3540a9a1a..704042247 100644 --- a/comfy_cli/command/workflow_check.py +++ b/comfy_cli/command/workflow_check.py @@ -353,7 +353,8 @@ def hash_file(file: str) -> str | None: blocked = cov.blocking(uses, object_info, models) remaps_applied = 0 if out_path is not None: - copy_, remaps_applied = cov.apply_remaps(data, models) + not_applied: list[dict[str, Any]] = [] + copy_, remaps_applied = cov.apply_remaps(data, models, not_applied) try: out_path.parent.mkdir(parents=True, exist_ok=True) atomic_write_text(out_path, json.dumps(copy_, indent=2, ensure_ascii=False) + "\n") @@ -365,9 +366,20 @@ def hash_file(file: str) -> str | None: details={"path": str(out_path)}, ) raise typer.Exit(code=1) from e + for model in not_applied: + # The copy keeps this value, so the row must not read as done. + warnings.append( + { + "code": "remap_not_applied", + "message": f"the copy keeps {model['value']} on node {model['node_id']}, not " + f"{model['cloud_value']}: the place it is saved is unknown, or an input that needs " + "another value reads the same place", + "details": {key: model[key] for key in ("node_id", "class_type", "widget", "value", "cloud_value")}, + } + ) for model in models: - for key in ("cloud_size", "cloud_hash"): + for key in ("loc", "cloud_size", "cloud_hash"): model.pop(key, None) payload: dict[str, Any] = { "workflow": str(path), diff --git a/comfy_cli/schemas/workflow_check.json b/comfy_cli/schemas/workflow_check.json index 99db9f20a..bf4e4c687 100644 --- a/comfy_cli/schemas/workflow_check.json +++ b/comfy_cli/schemas/workflow_check.json @@ -144,7 +144,7 @@ "properties": { "code": { "type": "string", - "description": "object_info_stale, models_dir_not_found, local_model_unreadable or cloud_model_lookup_failed." + "description": "object_info_stale, models_dir_not_found, local_model_unreadable, cloud_model_lookup_failed or remap_not_applied (the --out copy keeps that remap's saved value; details names the row)." }, "message": {"type": "string"}, "details": {"type": "object"} diff --git a/comfy_cli/workflow_coverage.py b/comfy_cli/workflow_coverage.py index 99b4c25c3..26c75792e 100644 --- a/comfy_cli/workflow_coverage.py +++ b/comfy_cli/workflow_coverage.py @@ -136,6 +136,52 @@ def _subgraphs(workflow: Mapping[str, Any]) -> list[Any]: return subgraphs if isinstance(subgraphs, list) else [] +def _places_in(path: tuple, value: Any) -> Iterator[tuple[tuple, str]]: + if is_model_value(value): + yield path, value + elif isinstance(value, dict): + for key, inner in value.items(): + if is_model_value(inner): + yield (*path, key), inner + + +def _saved_places(workflow: Mapping[str, Any]) -> Iterator[tuple[tuple, str, int, bool]]: + """Every file name an editor save stores, as ``(path from the root, value, id of + its node, whether it is a host's quarantined value)``. + + The places are each node's ``widgets_values`` (top level and in every subgraph + definition), a row's fields one level down, and a subgraph instance's legacy + quarantined host values. + """ + owners: list[tuple[tuple, Any]] = [(("nodes",), workflow.get("nodes"))] + for index, sg in enumerate(_subgraphs(workflow)): + if isinstance(sg, dict): + owners.append((("definitions", "subgraphs", index, "nodes"), sg.get("nodes"))) + for prefix, nodes in owners: + for i, node in enumerate(nodes if isinstance(nodes, list) else []): + if not isinstance(node, dict): + continue + values = node.get("widgets_values") + if not isinstance(values, (dict, list)): + values = [] + for key, value in values.items() if isinstance(values, dict) else enumerate(values): + for path, found in _places_in((*prefix, i, "widgets_values", key), value): + yield path, found, id(node), False + props = node.get("properties") + raw = props.get("proxyWidgetErrorQuarantine") if isinstance(props, dict) else None + for q, entry in enumerate(raw if isinstance(raw, list) else []): + if isinstance(entry, dict) and "hostValue" in entry: + path = (*prefix, i, "properties", "proxyWidgetErrorQuarantine", q, "hostValue") + for at, found in _places_in(path, entry["hostValue"]): + yield at, found, id(node), True + + +def _at(root: Any, path: tuple) -> Any: + for key in path: + root = root[key] + return root + + def _ui_nodes(workflow: Mapping[str, Any]) -> Iterator[tuple[str, Mapping[str, Any]]]: """Every node an instance reaches, with its scoped id. @@ -164,6 +210,9 @@ def find_uses(workflow: Mapping[str, Any], object_info: Mapping[str, Any]) -> di Returns ``{"format", "nodes": {class_type: [node_id, ...]}, "models": [...], "skipped": [...]}``. Each model row carries ``node_id``, ``class_type``, ``value`` and ``widget`` (the input that holds it, or ``None`` when unknown). + A row from an editor save also carries ``loc``, the path from the workflow's + root to the one place its value is saved (``None`` when that cannot be told); + :func:`apply_remaps` writes there. An editor save is converted the way it is submitted (subgraphs expanded, each instance's promoted values applied), so every instance of a subgraph gets its @@ -178,17 +227,36 @@ def find_uses(workflow: Mapping[str, Any], object_info: Mapping[str, Any]) -> di nodes: dict[str, list[str]] = {} models: list[dict[str, Any]] = [] skipped: list[dict[str, Any]] = [] - - def add(node_id: str, class_type: str, inputs: Mapping[str, Any], saved: set[str] | None = None) -> None: + markers: dict[str, tuple[tuple, str]] = {} + + def add( + node_id: str, + class_type: str, + inputs: Mapping[str, Any], + saved: set[str] | None = None, + probed: Mapping[str, Any] | None = None, + ) -> None: class_info = object_info.get(class_type) for name, value in inputs.items(): if not names_a_file(class_info, name): continue - for found in _model_strings(value): + marked = probed.get(name) if probed is not None else None + if is_model_value(value): + pairs = [(value, marked)] + elif isinstance(value, dict): + pairs = [(v, marked.get(k) if isinstance(marked, dict) else None) for k, v in value.items()] + else: + pairs = [] + for found, marker in pairs: # The converter fills an input the save left out with Cloud's first # choice; only a file the workflow itself names is reported. - if saved is None or found.lower() in saved: - models.append({"node_id": node_id, "class_type": class_type, "value": found, "widget": name}) + if not is_model_value(found) or (saved is not None and found.lower() not in saved): + continue + row: dict[str, Any] = {"node_id": node_id, "class_type": class_type, "value": found, "widget": name} + if saved is not None: + place = markers.get(marker) if isinstance(marker, str) else None + row["loc"] = place[0] if place is not None and place[1] == found else None + models.append(row) if fmt == "api": for node_id, node in workflow.items(): @@ -197,13 +265,26 @@ def add(node_id: str, class_type: str, inputs: Mapping[str, Any], saved: set[str add(str(node_id), class_type, node.get("inputs") if isinstance(node.get("inputs"), dict) else {}) return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} - from comfy_cli.workflow_to_api import convert_ui_to_api + from comfy_cli.workflow_to_api import WorkflowConversionError, convert_ui_to_api links = workflow.get("links") - prompt = convert_ui_to_api( - {**workflow, "links": links if isinstance(links, list) else []}, - object_info if isinstance(object_info, dict) else dict(object_info), - ) + catalog = object_info if isinstance(object_info, dict) else dict(object_info) + prompt = convert_ui_to_api({**workflow, "links": links if isinstance(links, list) else []}, catalog) + # Where each value is saved: the same conversion again, with every saved file + # name swapped for a marker. The marker an input ends up with names the one + # place its value came from (a node's own widget, the instance slot holding a + # promoted value, a quarantined host value), whatever precedence picked it, so + # a remap writes there and nowhere else. + places = list(_saved_places(workflow)) + probe = copy.deepcopy({**workflow, "links": links if isinstance(links, list) else []}) + for n, (path, value, _owner, _host) in enumerate(places): + marker = f"\x00saved-at-{n}\x00" + markers[marker] = (path, value) + _at(probe, path[:-1])[path[-1]] = marker + try: + probed = convert_ui_to_api(probe, catalog) + except WorkflowConversionError: + probed = {} walked = list(_ui_nodes(workflow)) saved = { found.lower() @@ -224,10 +305,16 @@ def add(node_id: str, class_type: str, inputs: Mapping[str, Any], saved: set[str nodes.setdefault(class_type, []).append(node_id) entry = prompt.get(node_id) if class_type in object_info else None if isinstance(entry, dict) and isinstance(entry.get("inputs"), dict): - add(node_id, class_type, entry["inputs"], saved) + probed_entry = probed.get(node_id) + probed_inputs = probed_entry.get("inputs") if isinstance(probed_entry, dict) else None + add(node_id, class_type, entry["inputs"], saved, probed_inputs if isinstance(probed_inputs, dict) else {}) else: - for found in _ui_model_strings(node): - models.append({"node_id": node_id, "class_type": class_type, "value": found, "widget": None}) + # Read from the node's own widget values, in the order they are saved. + own = [(path, value) for path, value, owner, host in places if owner == id(node) and not host] + for found, (path, _value) in zip(_ui_model_strings(node), own): + models.append( + {"node_id": node_id, "class_type": class_type, "value": found, "widget": None, "loc": path} + ) return {"format": fmt, "nodes": nodes, "models": models, "skipped": skipped} @@ -301,7 +388,8 @@ def resolve_models( Cloud file has the same name in another folder: the saved value must change to ``cloud_value``), ``missing``, or ``not_checked`` when the node type itself is not on Cloud. ``source`` is ``cloud`` for Cloud's own models and ``yours`` for - the person's uploads. ``candidates`` lists the names when several fit. + the person's uploads. ``candidates`` lists the names when several fit. A use's + ``loc`` is kept for :func:`apply_remaps`. """ asked: dict[str, list[dict[str, Any]]] = {} rows: list[dict[str, Any]] = [] @@ -316,6 +404,8 @@ def resolve_models( "source": None, "cloud_value": None, } + if "loc" in use: + row["loc"] = use["loc"] rows.append(row) if class_type not in object_info: row["status"] = "not_checked" @@ -514,64 +604,66 @@ def verdict(blocked: list[dict], models: list[dict[str, Any]]) -> str: return "covered" -def _replace(values: Any, old: str, new: str) -> int: - """Replace ``old`` with ``new`` in one node's widget values, rows included.""" +def _write(root: Any, path: tuple, old: str, new: str) -> int: + """Set the value saved at ``path`` from ``old`` to ``new``: 0 when it does not hold + ``old``. A row there (an API input such as a LoRA stack's) changes each field + that holds ``old``.""" + try: + parent = _at(root, path[:-1]) + current = parent[path[-1]] + except (KeyError, IndexError, TypeError): + return 0 + if current == old: + parent[path[-1]] = new + return 1 changed = 0 - items = list(values.items()) if isinstance(values, dict) else list(enumerate(values or [])) - for key, value in items: - if value == old: - values[key] = new - changed += 1 - elif isinstance(value, dict): - for inner, inner_value in list(value.items()): - if inner_value == old: - value[inner] = new - changed += 1 + if isinstance(current, dict): + for key, value in list(current.items()): + if value == old: + current[key] = new + changed += 1 return changed -def apply_remaps(workflow: Mapping[str, Any], models: list[dict[str, Any]]) -> tuple[dict[str, Any], int]: - """A copy of ``workflow`` with each remap's value replaced; the original is not touched. +def apply_remaps( + workflow: Mapping[str, Any], models: list[dict[str, Any]], not_applied: list[dict[str, Any]] | None = None +) -> tuple[dict[str, Any], int]: + """A copy of ``workflow`` with each remap written where its value is saved; the original is not touched. An API export changes the remapped input of that node. An editor save changes - the value wherever it is stored: on the nodes of the remapped type (top level - and inside every subgraph definition) and on subgraph instances, which hold a - promoted widget's value, legacy ``proxyWidgetErrorQuarantine`` entries included. + the one place :func:`find_uses` read the value from (the row's ``loc``): the + node's own widget, the subgraph instance slot that holds a promoted value, or + its legacy quarantined host value. The same file name saved anywhere else + (another loader, a promoted caption) is left as it is. + + Rows that read one place (a subgraph default several instances run) write it + once. A row with no known place, or whose place another row needs set to a + different value, is not written; it is appended to ``not_applied`` when given. + Returns the copy and the number of values changed. """ out = copy.deepcopy(workflow) remaps = [m for m in models if m["status"] == "remap"] + api = workflow_format(out) == "api" + + def place(model: Mapping[str, Any]) -> tuple | None: + if api: + return (model["node_id"], "inputs", model["widget"]) if model.get("widget") else None + loc = model.get("loc") + return tuple(loc) if loc else None + + wanted: dict[tuple, set[str]] = {} + for model in remaps: + at = place(model) + if at is not None: + wanted.setdefault(at, set()).add(model["cloud_value"]) + written: dict[tuple, int] = {} changed = 0 - if workflow_format(out) == "api": - for model in remaps: - node = out.get(model["node_id"]) - inputs = node.get("inputs") if isinstance(node, dict) else None - if isinstance(inputs, dict): - changed += _replace(inputs, model["value"], model["cloud_value"]) - return out, changed - - by_value: dict[str, set[str]] = {} for model in remaps: - by_value.setdefault(model["value"], set()).add(model["cloud_value"]) - # One saved value remapped two ways (two loader types, two folders) has no - # single replacement: it is left alone rather than guessed. - targets: dict[str, tuple[str, set[str]]] = {} - for old, new in by_value.items(): - if len(new) == 1: - targets[old] = (next(iter(new)), {m["class_type"] for m in remaps if m["value"] == old}) - subgraph_ids = {sg["id"] for sg in _subgraphs(out) if isinstance(sg, dict) and isinstance(sg.get("id"), str)} - every_node = [n for n in out.get("nodes") or [] if isinstance(n, dict)] - for sg in _subgraphs(out): - if isinstance(sg, dict) and isinstance(sg.get("nodes"), list): - every_node.extend(n for n in sg["nodes"] if isinstance(n, dict)) - for node in every_node: - instance = node.get("type") in subgraph_ids - for old, (new, class_types) in targets.items(): - if not instance and node.get("type") not in class_types: - continue - changed += _replace(node.get("widgets_values"), old, new) - if instance: - for entry in _quarantined_host_values(node): - if entry["hostValue"] == old: - entry["hostValue"] = new - changed += 1 + at = place(model) + # One place two rows need remapped two ways has no single right value: left as saved. + if at is not None and len(wanted[at]) == 1 and at not in written: + written[at] = _write(out, at, model["value"], model["cloud_value"]) + changed += written[at] + if not written.get(at) and not_applied is not None: + not_applied.append(model) return out, changed diff --git a/docs/json-output.md b/docs/json-output.md index 403ce0c54..ca5fa4afb 100644 --- a/docs/json-output.md +++ b/docs/json-output.md @@ -769,14 +769,17 @@ text input does not, so a prompt that ends in `.safetensors` is not a model. | `skipped` | array of dict | `{node_id, class_type, why}` for each muted or bypassed node; `why` is `muted` or `bypassed` | | `out` | str or null | Where `--apply-remaps --out` wrote the remapped copy | | `remaps_applied` | number | Values changed in that copy; `0` without `--out` | -| `warnings` | array of dict | `{code, message, details?}`: `object_info_stale` (`details` has `source` and `reason`), `models_dir_not_found`, `local_model_unreadable`, `cloud_model_lookup_failed` | +| `warnings` | array of dict | `{code, message, details?}`: `object_info_stale` (`details` has `source` and `reason`), `models_dir_not_found`, `local_model_unreadable`, `cloud_model_lookup_failed`, `remap_not_applied` (`details` has `node_id`, `class_type`, `widget`, `value` and `cloud_value`) | `verdict`, checked in this order: - `needs_build`: a node type or model is not on Cloud, or a model is proven a different file. `blocking[]` lists each. - `covered_with_remaps`: everything is on Cloud, but a model sits in another folder there, so the saved value must change. Each such row has `status: "remap"` and the value Cloud runs in `cloud_value`; `--apply-remaps --out - ` writes a copy with those values changed. + ` writes a copy with those values changed. Each is changed only where that input's value is saved (the + node's own widget, or the subgraph instance slot that holds a promoted value), so the same file name in another + loader or in a text input stays as it is. A remap the copy does not carry (the place its value is saved is + unknown, or an input that needs another value reads the same place) adds a `remap_not_applied` warning. - `covered_needs_check`: nothing is missing, but a model is not proven the same file as Cloud's copy (`identity` is not `same`). - `covered`: every node type and model is on Cloud, and each model is proven the same file. diff --git a/tests/comfy_cli/command/test_workflow_check.py b/tests/comfy_cli/command/test_workflow_check.py index 6538716b8..9ecddb9f9 100644 --- a/tests/comfy_cli/command/test_workflow_check.py +++ b/tests/comfy_cli/command/test_workflow_check.py @@ -197,6 +197,60 @@ def test_remaps_and_apply_remaps_writes_a_copy(cloud, tmp_path, capsys): assert json.loads(out.read_text())["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" +def test_apply_remaps_gives_each_loader_its_own_folder_for_one_file_name(cloud, monkeypatch, tmp_path, capsys): + object_info = { + **OBJECT_INFO, + "CheckpointLoaderSimple": {"input": {"required": {"ckpt_name": [["checkpoints/shared.safetensors"], {}]}}}, + "LoraLoader": {"input": {"required": {"lora_name": [["loras/shared.safetensors"], {}]}}}, + } + monkeypatch.setattr("comfy_cli.cql.loader.resilient_load_object_info", lambda **kw: object_info) + wf = _write( + tmp_path, + _ui((1, "CheckpointLoaderSimple", ["shared.safetensors"]), (2, "LoraLoader", ["shared.safetensors", 1.0, 1.0])), + ) + out = tmp_path / "remapped.json" + data = _ok(_run([str(wf), "--where", "cloud", "--apply-remaps", "--out", str(out)], capsys)) + assert [(m["node_id"], m["status"], m["cloud_value"]) for m in data["models"]] == [ + ("1", "remap", "checkpoints/shared.safetensors"), + ("2", "remap", "loras/shared.safetensors"), + ] + assert all("loc" not in m for m in data["models"]) + assert data["remaps_applied"] == 2 and data["warnings"] == [] + written = json.loads(out.read_text()) + assert [n["widgets_values"][0] for n in written["nodes"]] == [ + "checkpoints/shared.safetensors", + "loras/shared.safetensors", + ] + + +def test_a_remap_the_copy_does_not_carry_is_a_warning(cloud, monkeypatch, tmp_path, capsys): + from comfy_cli import workflow_coverage as cov + + real = cov.find_uses + + def unplaced(*args): + uses = real(*args) + for model in uses["models"]: + model["loc"] = None # as when the conversion cannot say where a value is saved + return uses + + monkeypatch.setattr(cov, "find_uses", unplaced) + wf = _write(tmp_path, _ui((1, "LoraLoader", ["ink.safetensors", 1.0, 1.0]))) + out = tmp_path / "remapped.json" + data = _ok(_run([str(wf), "--where", "cloud", "--apply-remaps", "--out", str(out)], capsys)) + assert data["remaps_applied"] == 0 + [warning] = data["warnings"] + assert warning["code"] == "remap_not_applied" + assert warning["details"] == { + "node_id": "1", + "class_type": "LoraLoader", + "widget": "lora_name", + "value": "ink.safetensors", + "cloud_value": "styles/ink.safetensors", + } + assert json.loads(out.read_text())["nodes"][0]["widgets_values"][0] == "ink.safetensors" + + def test_needs_build_lists_missing_nodes_and_models_with_their_folder(cloud, tmp_path, capsys): (tmp_path / "local" / "loras").mkdir(parents=True) (tmp_path / "local" / "loras" / "mine.safetensors").write_bytes(b"x") diff --git a/tests/comfy_cli/test_workflow_coverage.py b/tests/comfy_cli/test_workflow_coverage.py index 59f69157c..f677def62 100644 --- a/tests/comfy_cli/test_workflow_coverage.py +++ b/tests/comfy_cli/test_workflow_coverage.py @@ -9,7 +9,7 @@ import pytest from comfy_cli import workflow_coverage as cov -from comfy_cli.workflow_to_api import WorkflowConversionError +from comfy_cli.workflow_to_api import WorkflowConversionError, convert_ui_to_api _FIXTURES = Path(__file__).resolve().parent / "fixtures" @@ -141,19 +141,39 @@ def test_apply_remaps_rewrites_the_promoted_value_on_the_instance(): def test_apply_remaps_rewrites_a_quarantined_host_value(): - sg_id = "aaaaaaaa-0000-0000-0000-000000000003" - quarantine = [{"originalEntry": ["-1", "lora_name"], "hostValue": "ink.safetensors"}] - wf = _ui( - _node(10, sg_id, properties={"proxyWidgetErrorQuarantine": quarantine}), - definitions=_subgraph(sg_id, _node(1, "LoraLoader", ["other.safetensors", 1, 1])), - ) - row = {"status": "remap", "class_type": "LoraLoader", "value": "ink.safetensors"} - out, changed = cov.apply_remaps(wf, [{**row, "cloud_value": "styles/ink.safetensors"}]) + wf, host, object_info = _promoted_music_workflow() + # The frontend reads a quarantined legacy value before the positional slot. + host["widgets_values"][4] = "slot.safetensors" + quarantine = [{"originalEntry": ["-1", "unet_name"], "hostValue": "mine.safetensors"}] + host["properties"]["proxyWidgetErrorQuarantine"] = quarantine + [row] = _unet_rows(wf, object_info) + assert row["value"] == "mine.safetensors" + row.update(status="remap", cloud_value="music/mine.safetensors") + out, changed = cov.apply_remaps(wf, [row]) + assert changed == 1 + out_host = next(n for n in out["nodes"] if n["id"] == 37) + assert out_host["properties"]["proxyWidgetErrorQuarantine"][0]["hostValue"] == "music/mine.safetensors" + assert out_host["widgets_values"][4] == "slot.safetensors" + interior = [n for sg in out["definitions"]["subgraphs"] for n in sg["nodes"] if n["type"] == "UNETLoader"] + assert interior[0]["widgets_values"][0] == "minimax_music3_dit_fp16.safetensors" + assert quarantine[0]["hostValue"] == "mine.safetensors" + assert [m["value"] for m in _unet_rows(out, object_info)] == ["music/mine.safetensors"] + + +def test_remapping_a_top_level_loader_leaves_an_equal_promoted_caption_alone(): + wf, host, object_info = _promoted_music_workflow() + host["widgets_values"][0] = "ink.safetensors" # the promoted caption: text that runs, not a model + wf["nodes"].append(_node(999, "LoraLoader", ["ink.safetensors", 1, 1])) + object_info = {**object_info, "LoraLoader": OBJECT_INFO["LoraLoader"]} + assert convert_ui_to_api(wf, object_info)["37:13"]["inputs"]["caption"] == "ink.safetensors" + rows = cov.resolve_models(cov.find_uses(wf, object_info), object_info, _no_uploads) + rows = [r for r in rows if r["node_id"] == "999"] + assert [(r["status"], r["cloud_value"]) for r in rows] == [("remap", "styles/ink.safetensors")] + out, changed = cov.apply_remaps(wf, rows) assert changed == 1 - entry = out["nodes"][0]["properties"]["proxyWidgetErrorQuarantine"][0] - assert entry["hostValue"] == "styles/ink.safetensors" - assert out["definitions"]["subgraphs"][0]["nodes"][0]["widgets_values"][0] == "other.safetensors" - assert quarantine[0]["hostValue"] == "ink.safetensors" + assert next(n for n in out["nodes"] if n["id"] == 999)["widgets_values"][0] == "styles/ink.safetensors" + assert next(n for n in out["nodes"] if n["id"] == 37)["widgets_values"][0] == "ink.safetensors" + assert convert_ui_to_api(out, object_info)["37:13"]["inputs"]["caption"] == "ink.safetensors" def test_find_uses_api_reads_inputs_and_ignores_urls(): @@ -453,6 +473,59 @@ def test_apply_remaps_ui_changes_only_the_copy(): assert out["definitions"]["subgraphs"][0]["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" +def test_apply_remaps_writes_each_loader_its_own_destination_for_one_file_name(): + object_info = { + "CheckpointLoaderSimple": {"input": {"required": {"ckpt_name": [["checkpoints/shared.safetensors"], {}]}}}, + "LoraLoader": {"input": {"required": {"lora_name": [["loras/shared.safetensors"], {}]}}}, + } + wf = _ui( + _node(1, "CheckpointLoaderSimple", ["shared.safetensors"]), + _node(2, "LoraLoader", ["shared.safetensors", 1, 1]), + ) + rows = cov.resolve_models(_uses(wf, object_info), object_info, _no_uploads) + assert [(r["status"], r["cloud_value"]) for r in rows] == [ + ("remap", "checkpoints/shared.safetensors"), + ("remap", "loras/shared.safetensors"), + ] + not_applied: list = [] + out, changed = cov.apply_remaps(wf, rows, not_applied) + assert changed == 2 and not_applied == [] + assert out["nodes"][0]["widgets_values"] == ["checkpoints/shared.safetensors"] + assert out["nodes"][1]["widgets_values"] == ["loras/shared.safetensors", 1, 1] + + +def test_apply_remaps_writes_a_default_two_instances_run_once(): + sg_id = "aaaaaaaa-0000-0000-0000-000000000004" + wf = _ui( + _node(10, sg_id), + _node(11, sg_id), + definitions=_subgraph(sg_id, _node(1, "LoraLoader", ["ink.safetensors", 1, 1])), + ) + rows = _resolve(wf) + assert [r["node_id"] for r in rows if r["status"] == "remap"] == ["10:1", "11:1"] + not_applied: list = [] + out, changed = cov.apply_remaps(wf, rows, not_applied) + assert changed == 1 and not_applied == [] + assert out["definitions"]["subgraphs"][0]["nodes"][0]["widgets_values"][0] == "styles/ink.safetensors" + + +def test_apply_remaps_leaves_a_place_it_cannot_settle_and_says_so(): + wf = _ui(_node(1, "LoraLoader", ["shared.safetensors", 1, 1])) + at = ("nodes", 0, "widgets_values", 0) + row = {"node_id": "1", "class_type": "LoraLoader", "status": "remap", "value": "shared.safetensors"} + rows = [ + # One saved value two rows need remapped two ways. + {**row, "cloud_value": "a/shared.safetensors", "loc": at}, + {**row, "cloud_value": "b/shared.safetensors", "loc": at}, + # A value whose saved place could not be told. + {**row, "node_id": "2", "value": "gone.safetensors", "cloud_value": "c/gone.safetensors", "loc": None}, + ] + not_applied: list = [] + out, changed = cov.apply_remaps(wf, rows, not_applied) + assert (out, changed) == (wf, 0) + assert not_applied == rows + + def test_apply_remaps_api_sets_the_input(): wf = {"1": {"class_type": "LoraLoader", "inputs": {"lora_name": "ink.safetensors", "strength_model": 1}}} out, changed = cov.apply_remaps(wf, _resolve(wf)) From 9e4e84dcee4dd6cd44bae425a6ea01d8ca21b33c Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Thu, 5 Nov 2026 02:27:15 -0500 Subject: [PATCH 5/5] docs(adr): type the relation to ADR-CONVERSION-0004 --- ...-judge-cloud-coverage-by-name-and-prove-files-by-hash.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md b/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md index e4cba7324..b6b5c04e6 100644 --- a/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md +++ b/docs/adr/COVERAGE-0010-judge-cloud-coverage-by-name-and-prove-files-by-hash.md @@ -7,9 +7,9 @@ Date: 2026-11-05 Proposed Relation: Builds on ADR-AUTOMATION-0003. `comfy workflow check` emits one -`envelope/1` envelope with its own `workflow_check` data schema. Follows -ADR-CONVERSION-0004: a save is read through the save-to-API converter, and no -node type gets a rule of its own. +`envelope/1` envelope with its own `workflow_check` data schema. Builds on +ADR-CONVERSION-0004: a save is still read only through the save-to-API +converter, and no node type gets a rule of its own. ## Context