diff --git a/CHANGELOG.md b/CHANGELOG.md index 8493648..d560ee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +* `mqlaunch comfyui [start|stop|status]` and Dev → 10. Menus → + `5. Start ComfyUI` / `6. Stop ComfyUI`. Starts ComfyUI from the MQ-AI drive + in the background on `127.0.0.1:8188` (`--lowvram --reserve-vram 2`), waits + until it answers, and opens the browser; the log goes to + `~/Library/Logs/mqlaunch/comfyui.log`. Stop frees the memory again. Both + refuse to touch a port listener whose working directory is not the ComfyUI + directory. `COMFYUI_DIR` and `COMFYUI_PORT` override the defaults. + ## [2.7.0] - 2026-11-16 ### Changed diff --git a/docs/COMMANDS.md b/docs/COMMANDS.md index 7dfb7a7..f38bb3e 100644 --- a/docs/COMMANDS.md +++ b/docs/COMMANDS.md @@ -921,6 +921,7 @@ its own when the two were set separately. mqlaunch bundle # create debug/support bundle mqlaunch workspace # save, list, show and restore workspace snapshots mqlaunch excalidraw # open the Excalidraw drawing workflow +mqlaunch comfyui [start|stop|status] # ComfyUI from the MQ-AI drive (background, 127.0.0.1:8188) mqlaunch b2tui # B2 Atlas Prompt OS terminal interface ``` diff --git a/mqlaunch/lib/command-registry.json b/mqlaunch/lib/command-registry.json index 37e9a08..34a526b 100644 --- a/mqlaunch/lib/command-registry.json +++ b/mqlaunch/lib/command-registry.json @@ -111,6 +111,23 @@ "operator_surface": false, "local_role": "thin-entrypoint" }, + { + "name": "comfyui", + "aliases": [], + "namespace": null, + "summary": "Start, stop or check ComfyUI on the MQ-AI drive", + "owner": "macos-scripts", + "safety": "local-write", + "output_modes": [ + "human" + ], + "json": false, + "interactive": false, + "compat_only": false, + "delegates_to": null, + "operator_surface": false, + "local_role": "thin-entrypoint" + }, { "name": "review", "aliases": [ diff --git a/terminal/launchers/mqlaunch-command-mode.sh b/terminal/launchers/mqlaunch-command-mode.sh index 0c2c756..fb63722 100644 --- a/terminal/launchers/mqlaunch-command-mode.sh +++ b/terminal/launchers/mqlaunch-command-mode.sh @@ -59,8 +59,8 @@ nearest_cli_command() { local unknown="${1:-}" printf '%s\n' \ - about agent architecture ask auth brain bundle check commands demo dev doctor feedback \ - excalidraw fix flow focus ghost git guard hal help index learn mc mcp-status \ + about agent architecture ask auth brain bundle check comfyui commands demo dev doctor \ + feedback excalidraw fix flow focus ghost git guard hal help index learn mc mcp-status \ memory netpulse network next notes obsidian palette perf release release-check \ repo-health repos review risk-review route scan selftest skills srm stack system \ theme tools ui version workflows workspace | awk -v target="$unknown" ' @@ -258,6 +258,7 @@ Usage: Quick commands: mqlaunch demo mqlaunch excalidraw + mqlaunch comfyui [start|stop|status] mqlaunch perf mqlaunch network mqlaunch check @@ -436,6 +437,15 @@ dispatch_cli_command() { return $? ;; + comfyui) + # Arguments forwarded: comfyui.sh owns start, stop and status, and + # rejects anything else with exit 2. + shift + "$BASE_DIR/tools/scripts/comfyui.sh" "$@" + command_status=$? + return "$command_status" + ;; + focus) # Arguments forwarded rather than enumerated: focus.sh owns start, deep # and log, and rejects anything else with exit 1. Declaring them here too diff --git a/terminal/menus/mq-dev-menu.sh b/terminal/menus/mq-dev-menu.sh index 59c9469..ef4b8d4 100755 --- a/terminal/menus/mq-dev-menu.sh +++ b/terminal/menus/mq-dev-menu.sh @@ -122,7 +122,7 @@ dev_menus_menu_loop() { local choice while true; do _dev_submenu_panel "Menus" "1. Network tools" "2. Themes" "3. Tools menu" \ - "4. HAL terminal guide" + "4. HAL terminal guide" "5. Start ComfyUI" "6. Stop ComfyUI" read_menu_choice "" "menus" || return choice="$REPLY" echo @@ -131,6 +131,9 @@ dev_menus_menu_loop() { 2) open_themes_menu ;; 3) open_tools_menu ;; 4) run_dev_script "HAL TERMINAL GUIDE" "$(dev_repo_path "tools/scripts/hal-terminal-guide.sh")" ;; + # ComfyUI sits here because every front menu already holds ten choices. + 5) "$BASE_DIR/bin/mqlaunch" comfyui start; pause_enter ;; + 6) "$BASE_DIR/bin/mqlaunch" comfyui stop; pause_enter ;; b|B|back|x|X|exit) return ;; "") ;; *) echo "${C_ERR}Invalid dev selection:${C_RESET} $choice"; pause_enter ;; diff --git a/tests/comfyui-smoke.sh b/tests/comfyui-smoke.sh new file mode 100755 index 0000000..824c47e --- /dev/null +++ b/tests/comfyui-smoke.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +set -euo pipefail + +# tools/scripts/comfyui.sh starts ComfyUI from the MQ-AI drive in the +# background and stops it again. These checks run against a fake ComfyUI +# directory whose .venv/bin/python serves HTTP on a spare port, so no drive, +# model or GPU is needed. `open` is stubbed so no browser starts. + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +SCRIPT="$ROOT/tools/scripts/comfyui.sh" + +echo "SMOKE: comfyui start/stop" + +TMP="$(mktemp -d)" +STRAY_PID="" +cleanup() { + COMFYUI_DIR="$TMP/comfy" COMFYUI_PORT="${PORT:-0}" "$SCRIPT" stop >/dev/null 2>&1 || true + [[ -n "$STRAY_PID" ]] && kill "$STRAY_PID" 2>/dev/null || true + rm -rf "$TMP" +} +trap cleanup EXIT + +free_port() { + python3 -c 'import socket;s=socket.socket();s.bind(("127.0.0.1",0));print(s.getsockname()[1]);s.close()' +} + +mkdir -p "$TMP/bin" "$TMP/comfy/.venv/bin" "$TMP/logs" +printf '#!/usr/bin/env bash\nexit 0\n' >"$TMP/bin/open" +chmod +x "$TMP/bin/open" +touch "$TMP/comfy/main.py" +# Fake interpreter: picks --port out of the ComfyUI arguments and serves HTTP. +cat >"$TMP/comfy/.venv/bin/python" <<'EOF' +#!/usr/bin/env bash +port="" +while [[ $# -gt 0 ]]; do + [[ "$1" == "--port" ]] && port="$2" + shift +done +exec python3 -m http.server "$port" --bind 127.0.0.1 +EOF +chmod +x "$TMP/comfy/.venv/bin/python" + +PORT="$(free_port)" +export PATH="$TMP/bin:$PATH" COMFYUI_PORT="$PORT" COMFYUI_LOG_DIR="$TMP/logs" + +echo "[1/7] syntax check" +bash -n "$SCRIPT" + +echo "[2/7] missing drive fails with exit 1" +set +e +out="$(COMFYUI_DIR="$TMP/nope" "$SCRIPT" start 2>&1)" +rc=$? +set -e +[[ $rc -eq 1 ]] || { echo " expected exit 1, got $rc" >&2; exit 1; } +grep -q 'MQ-AI' <<<"$out" || { echo " message does not name the drive: $out" >&2; exit 1; } + +echo "[3/7] unknown subcommand fails with exit 2" +set +e +COMFYUI_DIR="$TMP/comfy" "$SCRIPT" nonsense >/dev/null 2>&1 +rc=$? +set -e +[[ $rc -eq 2 ]] || { echo " expected exit 2, got $rc" >&2; exit 1; } + +export COMFYUI_DIR="$TMP/comfy" + +echo "[4/7] start returns once the server answers, and does not block" +"$SCRIPT" start >/dev/null +curl -fsS "http://127.0.0.1:$PORT/" >/dev/null +"$SCRIPT" status | grep -q 'running' || { echo " status does not report running" >&2; exit 1; } +# A second start reuses the running server. +"$SCRIPT" start | grep -q 'already running' || { echo " second start did not reuse" >&2; exit 1; } + +echo "[5/7] stop frees the port" +"$SCRIPT" stop >/dev/null +if lsof -nP -iTCP:"$PORT" -sTCP:LISTEN >/dev/null 2>&1; then + echo " port $PORT still listening after stop" >&2 + exit 1 +fi +"$SCRIPT" status | grep -q 'not running' || { echo " status after stop is wrong" >&2; exit 1; } +"$SCRIPT" stop | grep -q 'not running' || { echo " stop when stopped is not a no-op" >&2; exit 1; } + +echo "[6/7] stop refuses a listener that is not ComfyUI" +(cd "$TMP" && exec python3 -m http.server "$PORT" --bind 127.0.0.1 >/dev/null 2>&1) & +STRAY_PID=$! +for _ in {1..50}; do + lsof -nP -iTCP:"$PORT" -sTCP:LISTEN >/dev/null 2>&1 && break + sleep 0.1 +done +set +e +"$SCRIPT" stop >/dev/null 2>&1 +rc=$? +set -e +[[ $rc -eq 1 ]] || { echo " expected exit 1, got $rc" >&2; exit 1; } +kill -0 "$STRAY_PID" 2>/dev/null || { echo " stop killed a foreign process" >&2; exit 1; } +set +e +"$SCRIPT" start >/dev/null 2>&1 +rc=$? +set -e +[[ $rc -eq 1 ]] || { echo " start over a foreign listener: expected exit 1, got $rc" >&2; exit 1; } + +echo "[7/7] the menu path main 5 -> Dev 10 -> Menus 5/6 reaches comfyui" +MAIN="$ROOT/terminal/menus/mq-main-menu.sh" +DEV="$ROOT/terminal/menus/mq-dev-menu.sh" +grep -qE '^[[:space:]]+5\) open_dev_menu ;;' "$MAIN" || { echo " main 5 does not open Dev" >&2; exit 1; } +grep -qE '^[[:space:]]+10\) dev_menus_menu_loop ;;' "$DEV" || { echo " Dev 10 does not open Menus" >&2; exit 1; } +menus="$(sed -n '/^dev_menus_menu_loop()/,/^}/p' "$DEV")" +for n in "5:start" "6:stop"; do + grep -qE "^[[:space:]]+${n%%:*}\) \"\\\$BASE_DIR/bin/mqlaunch\" comfyui ${n##*:};" <<<"$menus" || { + echo " Menus ${n%%:*} does not run comfyui ${n##*:}" >&2; exit 1; } +done +grep -q '"5. Start ComfyUI" "6. Stop ComfyUI"' <<<"$menus" || { echo " Menus panel does not draw the ComfyUI rows" >&2; exit 1; } + +echo "OK: comfyui smoke test passed" diff --git a/tests/manifest.tsv b/tests/manifest.tsv index c542f31..c42726b 100644 --- a/tests/manifest.tsv +++ b/tests/manifest.tsv @@ -24,6 +24,7 @@ # name class note command-docs-smoke.sh active - apps-menu-surface-smoke.sh active - +comfyui-smoke.sh active - command-registry-smoke.sh active - command-word-normalization-smoke.sh active - compat-path-delegation-smoke.sh active - diff --git a/tools/scripts/comfyui.sh b/tools/scripts/comfyui.sh new file mode 100755 index 0000000..694a7bb --- /dev/null +++ b/tools/scripts/comfyui.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Starts and stops ComfyUI from the MQ-AI drive. It runs in the background on +# 127.0.0.1 with its own venv, so the MQ stack and Ollama are untouched; stop +# frees the RAM after image generation. + +COMFYUI_DIR="${COMFYUI_DIR:-/Volumes/MQ-AI/AI-Apps/ComfyUI}" +COMFYUI_PORT="${COMFYUI_PORT:-8188}" +COMFYUI_WAIT="${COMFYUI_WAIT:-120}" +LOG_DIR="${COMFYUI_LOG_DIR:-$HOME/Library/Logs/mqlaunch}" +LOG_FILE="$LOG_DIR/comfyui.log" +URL="http://127.0.0.1:$COMFYUI_PORT" + +# Prints an error and exits 1. +die() { + printf 'comfyui: %s\n' "$*" >&2 + exit 1 +} + +# Prints usage. +usage() { + cat </dev/null | head -n 1 || true +} + +# True when the PID runs with the ComfyUI directory as its working directory. +# That is what separates our server from anything else on the port. +is_comfyui_pid() { + local pid="$1" cwd want + want="$(cd "$COMFYUI_DIR" 2>/dev/null && pwd -P)" || return 1 + cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | head -n 1)" + [[ -n "$cwd" && "$cwd" == "$want" ]] +} + +# Starts the server in the background and waits until it answers. +cmd_start() { + if [[ ! -f "$COMFYUI_DIR/main.py" || ! -x "$COMFYUI_DIR/.venv/bin/python" ]]; then + die "ComfyUI not found at $COMFYUI_DIR — is the MQ-AI drive connected?" + fi + + local pid + pid="$(listener_pid)" + if [[ -n "$pid" ]]; then + is_comfyui_pid "$pid" || die "port $COMFYUI_PORT is used by another process (PID $pid)" + printf 'OK: ComfyUI already running at %s (PID %s)\n' "$URL" "$pid" + open "$URL" >/dev/null 2>&1 || true + return 0 + fi + + mkdir -p "$LOG_DIR" + printf 'Starting ComfyUI from %s...\n' "$COMFYUI_DIR" + ( + cd "$COMFYUI_DIR" + nohup .venv/bin/python main.py \ + --listen 127.0.0.1 \ + --port "$COMFYUI_PORT" \ + --lowvram \ + --reserve-vram 2 \ + --preview-method none >"$LOG_FILE" 2>&1 & + ) + + local i + for ((i = 0; i < COMFYUI_WAIT; i++)); do + if curl -fsS "$URL" >/dev/null 2>&1; then + printf 'OK: ComfyUI ready at %s\n' "$URL" + printf 'Log: %s\n' "$LOG_FILE" + open "$URL" >/dev/null 2>&1 || true + return 0 + fi + sleep 1 + done + + die "ComfyUI did not answer at $URL within ${COMFYUI_WAIT}s — see $LOG_FILE" +} + +# Stops the server; refuses to touch a process that is not ComfyUI. +cmd_stop() { + local pid + pid="$(listener_pid)" + if [[ -z "$pid" ]]; then + printf 'OK: ComfyUI not running\n' + return 0 + fi + + is_comfyui_pid "$pid" || die "port $COMFYUI_PORT is used by another process (PID $pid); not stopping it" + + printf 'Stopping ComfyUI (PID %s)...\n' "$pid" + kill "$pid" 2>/dev/null || true + + local i + for ((i = 0; i < 100; i++)); do + if ! kill -0 "$pid" 2>/dev/null; then + printf 'OK: ComfyUI stopped\n' + return 0 + fi + sleep 0.1 + done + + kill -9 "$pid" 2>/dev/null || true + printf 'OK: ComfyUI stopped (forced after 10s)\n' +} + +# Reports whether the server is running. +cmd_status() { + local pid + pid="$(listener_pid)" + if [[ -z "$pid" ]]; then + printf 'ComfyUI: not running\n' + elif is_comfyui_pid "$pid"; then + printf 'ComfyUI: running at %s (PID %s)\n' "$URL" "$pid" + else + printf 'ComfyUI: not running (port %s used by PID %s)\n' "$COMFYUI_PORT" "$pid" + fi +} + +# Runs the main entry point. +main() { + case "${1:-start}" in + start) cmd_start ;; + stop) cmd_stop ;; + status) cmd_status ;; + -h|--help|help) usage ;; + *) + usage >&2 + exit 2 + ;; + esac +} + +main "$@" diff --git a/tools/scripts/test-all.sh b/tools/scripts/test-all.sh index 6cff8ba..576955a 100755 --- a/tools/scripts/test-all.sh +++ b/tools/scripts/test-all.sh @@ -92,6 +92,7 @@ bash "$PROJECT_ROOT/tests/doctor-ollama-smoke.sh" bash "$PROJECT_ROOT/tests/hal-guide-ollama-smoke.sh" "$PROJECT_ROOT/tests/auth-status-smoke.sh" "$PROJECT_ROOT/tests/apps-menu-surface-smoke.sh" +"$PROJECT_ROOT/tests/comfyui-smoke.sh" "$PROJECT_ROOT/tests/delegate-missing-message-smoke.sh" "$PROJECT_ROOT/tests/command-registry-smoke.sh" "$PROJECT_ROOT/tests/output-mode-parity-smoke.sh"