Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(comfyui): keep --json stdout parseable (#414)

PAN CHAOa5ad2499f07d
diff --git a/README.md b/README.md
index 95d361434..1b2d8ec41 100644
--- a/README.md
+++ b/README.md
@@ -1366,7 +1366,7 @@ eth2-quickstart 18 passed ✅ (18 unit + 3 e2e skipped)
mermaid 10 passed ✅ (5 unit + 5 e2e)
anygen 50 passed ✅ (40 unit + 10 e2e)
notebooklm 21 passed ✅ (21 unit + 0 e2e)
-comfyui 70 passed ✅ (60 unit + 10 e2e)
+comfyui 73 passed ✅ (63 unit + 10 e2e)
adguardhome 36 passed ✅ (24 unit + 12 e2e)
ollama 98 passed ✅ (87 unit + 11 e2e)
sketch 19 passed ✅ (19 jest, Node.js)
@@ -1380,7 +1380,7 @@ cloudanalyzer 14 passed ✅ (7 unit + 7 e2e)
3mf 50 passed ✅ (50 unit)
joplin 134 passed ✅ (107 unit + 27 e2e, 1 skipped on Windows)
──────────────────────────────────────────────────────────────────────────────
-TOTAL 2,461 passed ✅ 100% pass rate
+TOTAL 2,464 passed ✅ 100% pass rate
```
---
@@ -1460,7 +1460,7 @@ cli-anything/
├── ⛓️ eth2-quickstart/agent-harness/ # ETH2 QuickStart CLI (18 unit, 3 e2e skipped)
├── 🧜 mermaid/agent-harness/ # Mermaid Live Editor CLI (10 tests)
├── ✨ anygen/agent-harness/ # AnyGen CLI (50 tests)
-├── 🖼️ comfyui/agent-harness/ # ComfyUI CLI (70 tests)
+├── 🖼️ comfyui/agent-harness/ # ComfyUI CLI (73 tests)
├── 🧠 notebooklm/agent-harness/ # NotebookLM CLI (experimental, 21 tests)
├── 🧩 dify-workflow/agent-harness/ # Dify Workflow CLI wrapper (11 tests)
├── 🛡️ adguardhome/agent-harness/ # AdGuard Home CLI (36 tests)
diff --git a/comfyui/agent-harness/cli_anything/comfyui/comfyui_cli.py b/comfyui/agent-harness/cli_anything/comfyui/comfyui_cli.py
index 78b8799e1..3bbbb768d 100644
--- a/comfyui/agent-harness/cli_anything/comfyui/comfyui_cli.py
+++ b/comfyui/agent-harness/cli_anything/comfyui/comfyui_cli.py
@@ -58,6 +58,17 @@ def output(data, message: str = ""):
click.echo(str(data))
+def echo_human(message: str, err: bool = False):
+ """Print a human-facing message. No-op in JSON mode.
+
+ Keeps `--json` output a single parseable document — anything written for
+ humans must go through here so it never lands beside the JSON payload.
+ """
+ if _json_output:
+ return
+ click.echo(message, err=err)
+
+
def _print_dict(d: dict, indent: int = 0):
prefix = " " * indent
for k, v in d.items():
@@ -153,9 +164,9 @@ def workflow_validate(path):
result = workflow_mod.validate_workflow(wf)
output(result, f"Validation: {path}")
if result["valid"]:
- click.echo(" Workflow is valid.")
+ echo_human(" Workflow is valid.")
else:
- click.echo(f" {len(result['errors'])} error(s) found.", err=True)
+ echo_human(f" {len(result['errors'])} error(s) found.", err=True)
# ── Queue Commands ──────────────────────────────────────────────
@@ -191,7 +202,8 @@ def queue_status():
def queue_clear(confirm):
"""Clear all pending items from the queue."""
if not confirm:
- click.confirm("Clear the queue?", abort=True)
+ # In JSON mode the prompt goes to stderr so stdout stays parseable.
+ click.confirm("Clear the queue?", abort=True, err=_json_output)
result = queue_mod.clear_queue(_base_url)
output(result, "Queue cleared.")
diff --git a/comfyui/agent-harness/cli_anything/comfyui/tests/TEST.md b/comfyui/agent-harness/cli_anything/comfyui/tests/TEST.md
index 9c803ef2b..51a10c1cf 100644
--- a/comfyui/agent-harness/cli_anything/comfyui/tests/TEST.md
+++ b/comfyui/agent-harness/cli_anything/comfyui/tests/TEST.md
@@ -41,6 +41,8 @@ python -m pytest cli_anything/comfyui/tests/ --cov=cli_anything.comfyui --cov-re
- **Images:** list outputs, download single image, download all for prompt
- **Backend:** GET/POST/DELETE/raw byte wrappers, connection errors, timeouts
- **CLI:** all command groups in both human and `--json` output modes
+- **JSON purity:** in `--json` mode stdout stays a single parseable document — no
+ human-readable summary lines, no confirmation prompts
- **Errors:** connection refused, server rejects workflow, file not found, overwrite protection
## Mock Patterns
diff --git a/comfyui/agent-harness/cli_anything/comfyui/tests/test_core.py b/comfyui/agent-harness/cli_anything/comfyui/tests/test_core.py
index 3f55565c9..c5ee0b18c 100644
--- a/comfyui/agent-harness/cli_anything/comfyui/tests/test_core.py
+++ b/comfyui/agent-harness/cli_anything/comfyui/tests/test_core.py
@@ -14,11 +14,15 @@ Run with:
"""
import json
+import os
+import subprocess
+import sys
from pathlib import Path
from unittest.mock import patch, MagicMock
import pytest
from click.testing import CliRunner
+from cli_anything.comfyui import comfyui_cli as cli_module
from cli_anything.comfyui.comfyui_cli import cli
from cli_anything.comfyui.core import workflows as workflow_mod
from cli_anything.comfyui.core import queue as queue_mod
@@ -34,6 +38,30 @@ def runner():
return CliRunner()
+def run_cli_subprocess(args, stdin=""):
+ """Run the CLI in a real subprocess and return the completed process.
+
+ `CliRunner` emulates a terminal by echoing the answer typed at a prompt
+ onto stdout — on click < 8.2 it lands there even when the prompt itself
+ was sent to stderr. That echo would hide exactly the kind of stdout
+ contamination the JSON-purity tests are looking for, so those tests read
+ the real streams instead.
+ """
+ harness_root = Path(cli_module.__file__).resolve().parents[2]
+ env = dict(os.environ)
+ env["PYTHONPATH"] = os.pathsep.join(
+ p for p in (str(harness_root), env.get("PYTHONPATH", "")) if p
+ )
+ return subprocess.run(
+ [sys.executable, "-m", "cli_anything.comfyui.comfyui_cli", *args],
+ input=stdin,
+ capture_output=True,
+ text=True,
+ env=env,
+ timeout=60,
+ )
+
+
@pytest.fixture
def sample_workflow():
"""Minimal valid ComfyUI workflow (API format)."""
@@ -564,6 +592,29 @@ class TestCLIWorkflow:
assert "valid" in data
assert "node_count" in data
+ def test_workflow_validate_json_output_invalid_workflow(self, runner, tmp_path):
+ """--json output must stay parseable when validation fails."""
+ p = tmp_path / "not_a_workflow.json"
+ p.write_text(json.dumps({"tokens": "abc", "pages": [1, 2]}))
+
+ result = runner.invoke(cli, ["--json", "workflow", "validate", str(p)])
+ assert result.exit_code == 0
+ data = json.loads(result.output)
+ assert data["valid"] is False
+ assert len(data["errors"]) == 2
+
+ def test_workflow_validate_human_summary(self, runner, tmp_path, workflow_file):
+ """Without --json the human-readable summary line is still printed."""
+ result = runner.invoke(cli, ["workflow", "validate", workflow_file])
+ assert result.exit_code == 0
+ assert "Workflow is valid." in result.output
+
+ p = tmp_path / "not_a_workflow.json"
+ p.write_text(json.dumps({"tokens": "abc", "pages": [1, 2]}))
+ result = runner.invoke(cli, ["workflow", "validate", str(p)])
+ assert result.exit_code == 0
+ assert "2 error(s) found." in result.output
+
class TestCLIQueue:
"""Test CLI queue commands."""
@@ -594,6 +645,19 @@ class TestCLIQueue:
assert result.exit_code == 0
assert "cleared" in result.output
+ def test_queue_clear_json_prompt_not_on_stdout(self):
+ """The confirmation prompt must not corrupt --json stdout.
+
+ Answering "n" aborts before any request is made, so no server is
+ needed.
+ """
+ proc = run_cli_subprocess(["--json", "queue", "clear"], stdin="n\n")
+
+ assert proc.returncode == 1
+ data = json.loads(proc.stdout)
+ assert data["type"] == "Abort"
+ assert "Clear the queue?" in proc.stderr
+
def test_queue_history_json(self, runner):
"""queue history --json should return valid JSON."""
mock_response = {