Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ history.

### Fixed

- Outputs from `comfy run` on a UI-format workflow (the canvas `nodes` +
`links` shape, which is how the Comfy agent runs a canvas) now drag back onto
the ComfyUI canvas as a workflow. The run sends the canvas graph as
`extra_data.extra_pnginfo.workflow`, the same as the editor's Queue button,
so save nodes (SaveImage, VHS Video Combine and others) write `workflow`
next to `prompt`. Before, the files carried only `prompt` and dropped in as a
plain image or video. Applies to local and `--where cloud` runs; API-format
inputs are unchanged.
- A long partner-node run on a local ComfyUI, signed in with `comfy cloud
login`, no longer stops with "Unauthorized: Please login first" after the
provider task was created, wherever Comfy Cloud issues partner-node tokens
Expand Down
25 changes: 23 additions & 2 deletions comfy_cli/command/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from comfy_cli.command.run.loader import _node_errors_to_list as _node_errors_to_list
from comfy_cli.command.run.loader import is_ui_workflow as is_ui_workflow
from comfy_cli.command.run.loader import pop_compose_meta as pop_compose_meta
from comfy_cli.command.run.loader import ui_workflow_extra_data as ui_workflow_extra_data
from comfy_cli.command.run.preflight import PARTNER_NODE_CATEGORY_PREFIXES as PARTNER_NODE_CATEGORY_PREFIXES
from comfy_cli.command.run.preflight import _detect_partner_nodes as _detect_partner_nodes
from comfy_cli.command.run.preflight import _fetch_object_info as _fetch_object_info
Expand Down Expand Up @@ -442,6 +443,14 @@ def execute(
raise typer.Exit(code=1)
extra_data = {cred[0]: cred[1]}

# Send the canvas graph with the run, as the editor's Queue button does, so
# save nodes write `workflow` into the file and it drags back as a workflow.
# Merged only after the credential gate above, which reads an
# empty `extra_data` as "no explicit key".
pnginfo = ui_workflow_extra_data(raw_workflow) if is_ui else None
if pnginfo:
extra_data = {**(extra_data or {}), **pnginfo}

# Pre-submit validation via pure-Python CQL engine (checks class_types + input shapes).
_preflight_validate(
renderer, run_graph, object_info, target_label="server", where="local", submitted_workflow=workflow
Expand Down Expand Up @@ -1262,6 +1271,10 @@ def execute_cloud(
# which must NOT trigger a second round-trip.
cloud_object_info: dict | None = None

# Captured before the conversion below replaces `raw_workflow` with the API
# graph: the canvas graph rides the submit as extra_pnginfo.
submit_extra_data = ui_workflow_extra_data(raw_workflow) if is_ui else None
Comment thread
christian-byrne marked this conversation as resolved.
Resolved

if is_ui:
# Frontend-format workflows (the `nodes`+`links` shape from the canvas
# exporter and `comfy templates fetch`) have to be lowered to the API
Expand Down Expand Up @@ -1417,11 +1430,19 @@ def execute_cloud(
if not wait and renderer.is_pretty():
with renderer.console().status("[cyan]Submitting to Comfy Cloud…", spinner="dots"):
submit = client.submit_prompt(
parsed_workflow, client_id, workflow_id=workflow_id, output_node_ids=output_node_ids
parsed_workflow,
client_id,
workflow_id=workflow_id,
output_node_ids=output_node_ids,
extra_data=submit_extra_data,
)
else:
submit = client.submit_prompt(
parsed_workflow, client_id, workflow_id=workflow_id, output_node_ids=output_node_ids
parsed_workflow,
client_id,
workflow_id=workflow_id,
output_node_ids=output_node_ids,
extra_data=submit_extra_data,
)
except Unauthenticated as e:
renderer.error(code="cloud_unauthorized", message=str(e), hint="run: comfy cloud login")
Expand Down
19 changes: 19 additions & 0 deletions comfy_cli/command/run/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ def is_ui_workflow(workflow) -> bool:
)


def ui_workflow_extra_data(ui_workflow) -> dict | None:
"""The ``extra_data`` slot that carries a UI-format workflow with its run.

The editor's Queue button sends the canvas graph as
``extra_data.extra_pnginfo.workflow``. ComfyUI hands ``extra_pnginfo`` to
every output node, and save nodes (SaveImage, VHS Video Combine, ...) write
it into the file, which is what lets the file drag back onto the canvas as
a workflow. Without it the file holds only ``prompt``. Returns None for an
API-format input: there is no canvas graph to send.

Not copied: the converter works on its own deep copy, so the loaded graph
is never mutated, and a deep copy here would hit RecursionError on a
deeply nested graph before the conversion error handling runs.
"""
if not is_ui_workflow(ui_workflow):
return None
return {"extra_pnginfo": {"workflow": ui_workflow}}


def _classify_api_workflow(workflow):
"""Classify a parsed JSON object as API workflow / empty / invalid.

Expand Down
76 changes: 76 additions & 0 deletions tests/comfy_cli/command/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ def test_queue_lets_explicit_extra_data_override_the_usage_source(self, workflow
body = json.loads(mock_open.call_args[0][0].data)
assert body["extra_data"] == {"comfy_usage_source": "some-other-surface"}

def test_queue_sends_extra_pnginfo_in_the_body(self, workflow):
"""The canvas graph handed in as ``extra_data`` reaches the ``/prompt``
body under ``extra_pnginfo.workflow``."""
canvas = {"nodes": [{"id": 1}], "links": []}
ex = self._make_exec(workflow, extra_data={"extra_pnginfo": {"workflow": canvas}})
with patch("comfy_cli.http._AUTHED_OPENER.open") as mock_open:
mock_open.return_value.__enter__.return_value.read.return_value = json.dumps({"prompt_id": "abc"}).encode()
ex.queue()
body = json.loads(mock_open.call_args[0][0].data)
assert body["extra_data"]["extra_pnginfo"] == {"workflow": canvas}

def test_queue_sends_usage_source_header(self, workflow):
ex = self._make_exec(workflow)
with patch("comfy_cli.http._AUTHED_OPENER.open") as mock_open:
Expand Down Expand Up @@ -2546,6 +2557,49 @@ def test_ui_workflow_plumbs_api_key_through_to_execution(self, ui_workflow_file)
assert mock_fetch.call_args.args == ("127.0.0.1", 8188, 30)
assert MockExec.call_args.kwargs["extra_data"]["api_key_comfy_org"] == "sk-test"

def test_ui_workflow_run_carries_the_canvas_graph_as_extra_pnginfo(self, ui_workflow_file):
"""The editor's Queue button sends the canvas graph as
``extra_data.extra_pnginfo.workflow``; save nodes write it into the
output file, which is what lets the file drag back as a workflow. A UI
run from the CLI (how the Comfy agent submits) must send the same."""
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.fetch_object_info", return_value=self.OBJECT_INFO),
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
mock_exec.borrowed_client_id = False
MockExec.return_value = mock_exec
mock_exec.outputs = []

execute(ui_workflow_file, host="127.0.0.1", port=8188, wait=True, timeout=30, api_key="sk-test")

extra = MockExec.call_args.kwargs["extra_data"]
assert extra["extra_pnginfo"] == {"workflow": self.UI}
# The explicit key still rides alongside it.
assert extra["api_key_comfy_org"] == "sk-test"

def test_api_workflow_run_sends_no_extra_pnginfo(self, tmp_path):
"""An API-format input has no canvas graph to send."""
path = tmp_path / "api.json"
path.write_text(json.dumps({"1": {"class_type": "PreviewImage", "inputs": {}}}))
with (
patch("comfy_cli.command.run.check_comfy_server_running", return_value=True),
patch("comfy_cli.command.run.fetch_object_info", return_value=self.OBJECT_INFO),
patch("comfy_cli.command.run._preflight_validate"),
patch("comfy_cli.command.run.ExecutionProgress"),
patch("comfy_cli.command.run.WorkflowExecution") as MockExec,
):
mock_exec = MagicMock()
mock_exec.borrowed_client_id = False
MockExec.return_value = mock_exec
mock_exec.outputs = []

execute(str(path), host="127.0.0.1", port=8188, wait=True, timeout=30)

assert "extra_pnginfo" not in (MockExec.call_args.kwargs["extra_data"] or {})

def test_ui_workflow_exits_when_conversion_yields_nothing(self):
# All nodes are UI-only (Note/PrimitiveNode/Reroute/GetNode/SetNode) and
# therefore stripped by the converter → execute() should bail before
Expand Down Expand Up @@ -2697,6 +2751,28 @@ def test_ui_workflow_converts_and_submits(self, ui_workflow_file, fake_target):
submitted_args, _ = mock_client.submit_prompt.call_args
assert submitted_args[0] == self.CONVERTED

def test_ui_workflow_submit_carries_the_canvas_graph_as_extra_pnginfo(self, ui_workflow_file, fake_target):
"""Cloud path: the API graph is what runs, and the UI graph it
came from rides ``extra_data.extra_pnginfo.workflow`` like an editor run."""
from comfy_cli.comfy_client import SubmitResult
from comfy_cli.command.run import execute_cloud

mock_client = MagicMock()
mock_client.submit_prompt.return_value = SubmitResult(prompt_id="prompt-png", number=1, node_errors={})

with (
patch("comfy_cli.target.resolve_target", return_value=fake_target),
patch("comfy_cli.command.run.convert_ui_to_api", return_value=self.CONVERTED),
patch("comfy_cli.cql.engine._load_from_target", return_value={"KSampler": {"output_node": True}}),
patch("comfy_cli.comfy_client.Client", return_value=mock_client),
patch("comfy_cli.command.run._spawn_watcher"),
):
execute_cloud(ui_workflow_file, wait=False)

submitted_args, submitted_kwargs = mock_client.submit_prompt.call_args
assert submitted_args[0] == self.CONVERTED
assert submitted_kwargs["extra_data"] == {"extra_pnginfo": {"workflow": self.UI_WORKFLOW}}

def test_ui_workflow_conversion_honors_object_info_file_env(
self, ui_workflow_file, fake_target, tmp_path, monkeypatch
):
Expand Down
Loading