From 718881ef90f6dd9614292c7790bb9711824a70e0 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Wed, 4 Nov 2026 13:25:59 +0000 Subject: [PATCH 1/5] refactor(comfyui): move runtime selection and environment into comfyui/runtime_env.rs comfyui.rs mixes install, process lifecycle, runtime selection and environment, and status. This moves the runtime selection (select_runtime and its default/selector/ready-runtime helpers and their error messages) and the child-process environment builders (runtime_environment_*, apply_runtime_environment and the path helpers) into a child module, with the tests that exercise them. The types they return stay in comfyui.rs. Pure move: no behaviour change; items are pub(super) only where comfyui.rs still calls them. The shared test fixtures (test_paths, ready_runtime_manifest, write_runtime_manifest) stay in comfyui.rs's tests module and are widened to pub(super) so the moved tests can reach them. docs/architecture.md describes the new module. Signed-off-by: Jussi Elo --- apps/rocm/src/comfyui.rs | 942 +------------------------- apps/rocm/src/comfyui/runtime_env.rs | 967 +++++++++++++++++++++++++++ docs/architecture.md | 3 +- 3 files changed, 978 insertions(+), 934 deletions(-) create mode 100644 apps/rocm/src/comfyui/runtime_env.rs diff --git a/apps/rocm/src/comfyui.rs b/apps/rocm/src/comfyui.rs index 5875cd1d38..ca0f046da4 100644 --- a/apps/rocm/src/comfyui.rs +++ b/apps/rocm/src/comfyui.rs @@ -4,7 +4,6 @@ use crate::cli_progress::AnimatedSpinner; use crate::freeform_planner::format_structured_tool_call; -use crate::runtimes_cmd::runtime_usability_status; use crate::therock; use anyhow::{Context, Result, bail}; use flate2::read::GzDecoder; @@ -12,11 +11,13 @@ use rocm_core::browser::{Opener, SystemOpener}; use rocm_core::{ AppPaths, RocmCliConfig, download_file_to_path_with_progress, ensure_uv_binary, format_http_base_url, powershell_quote, runtime_is_linux, runtime_is_windows, - runtime_path_for_windows_child, runtime_path_list_join, runtime_path_list_split, - runtime_paths_equivalent, unix_time_millis, uv_command_env, uv_pip_install_base, + runtime_path_for_windows_child, unix_time_millis, uv_command_env, uv_pip_install_base, +}; +use runtime_env::{ + apply_runtime_environment, runtime_environment_assignments, runtime_environment_for_manifest, + runtime_environment_from_runtime, same_path_text, select_runtime, }; use serde::{Deserialize, Serialize}; -use std::ffi::OsString; use std::fmt::Write as FmtWrite; use std::fs; use std::io::{self, IsTerminal, Read, Write as IoWrite}; @@ -26,6 +27,7 @@ use std::process::{Command, ExitStatus, Stdio}; use std::thread; use std::time::{Duration, SystemTime}; +mod runtime_env; mod source_swap; const APP_ID: &str = "comfyui"; @@ -1368,367 +1370,6 @@ fn save_state(paths: &AppPaths, state: &ComfyUiState) -> Result<()> { .with_context(|| format!("failed to write {}", path.display())) } -fn select_runtime( - paths: &AppPaths, - config: &RocmCliConfig, - selector: Option<&str>, -) -> Result { - let manifests = therock::load_runtime_manifests(paths)?; - if manifests.is_empty() { - bail!( - "Install ROCm first: run `rocm` and pick \"Set up this system\", then install ComfyUI." - ); - } - let manifest = match selector.map(str::trim).filter(|value| !value.is_empty()) { - Some(selector) => select_runtime_by_selector(&manifests, selector)?.clone(), - None => select_default_runtime(config, &manifests)?.clone(), - }; - if runtime_usability_status(&manifest) != "ready" { - bail!( - "The selected ROCm runtime is not ready: {}", - runtime_usability_status(&manifest) - ); - } - if manifest.format != "wheel" { - bail!("ComfyUI installs require a rocm-cli managed Python ROCm runtime."); - } - // Defence in depth, not a reachable branch on a settled filesystem: the - // `ready` check above ran `validate_wheel_runtime_manifest`, which already - // proved `python_executable` is `Some` and `is_file()`. This re-check only - // fires if the interpreter disappears in the window between the two calls, - // so keep it (a missing interpreter must not reach the installer) and do - // not spend further polish on its wording. - let python = manifest - .python_executable - .as_deref() - .map(PathBuf::from) - .filter(|path| path.is_file()) - .with_context(|| { - "The selected ROCm runtime has no Python executable. Pick another in `/runtimes` or pass `--runtime-id `. See `rocm runtimes list`." - .to_string() - })?; - Ok(SelectedRuntime { manifest, python }) -} - -/// Render a bare `key, key` list. Sound only where the caller has already -/// filtered `manifests` by `runtime_usability_status(...) == "ready"`, because -/// that is the only filter that makes every listed key a valid answer to the -/// `rocm runtimes activate ` advice these messages carry; a per-key status -/// would then be noise. Matching a selector is *not* such a filter: two -/// manifests can share a `runtime_id` with one of them unusable, so an -/// id-matched set can contain a key that cannot be activated. Every branch -/// whose set is not ready-filtered — the whole-registry ones and the two -/// id-ambiguity ones — uses `format_runtime_statuses` instead; see its doc -/// comment for why the two labels differ. -fn format_available_runtime_keys<'a>( - manifests: impl IntoIterator, -) -> String { - manifests - .into_iter() - .map(|manifest| manifest.runtime_key.as_str()) - .collect::>() - .join(", ") -} - -/// Render each known runtime as `` `key` (status) ``, surfacing the per-manifest -/// reason `runtime_usability_status` already computed (e.g. -/// `unusable (install root is missing: …)`) instead of dropping it. -/// -/// Used by every branch whose set is not ready-filtered: the whole-registry -/// ones ("none ready" and the two not-found branches) and the two branches that -/// report an ambiguous `runtime_id`. None of them can promise the listed -/// runtimes are pickable — a shared `runtime_id` says nothing about readiness — -/// so printing bare keys under `Available:` would invite the user to -/// `rocm runtimes activate` an unusable one and land on a second error. The -/// label split is deliberate and load-bearing: `Available:` means "any of these -/// works", `Runtimes found:` means "here is everything registered, with why", -/// and `Runtimes matched:` means "here is what the selector matched, with why". -fn format_runtime_statuses<'a>( - manifests: impl IntoIterator, -) -> String { - manifests - .into_iter() - .map(|manifest| { - format!( - "`{}` ({})", - manifest.runtime_key, - runtime_usability_status(manifest) - ) - }) - .collect::>() - .join(", ") -} - -fn select_default_runtime<'a>( - config: &RocmCliConfig, - manifests: &'a [therock::InstalledRuntimeManifest], -) -> Result<&'a therock::InstalledRuntimeManifest> { - if let Some(active_key) = config.active_runtime_key.as_deref() { - return select_runtime_by_selector(manifests, active_key); - } - let Some(default_runtime_id) = config.default_runtime_id.as_deref() else { - return select_single_ready_runtime(manifests); - }; - let matches = manifests - .iter() - .filter(|manifest| manifest.runtime_id.eq_ignore_ascii_case(default_runtime_id)) - .collect::>(); - match matches.as_slice() { - [manifest] => Ok(*manifest), - [] => { - // Whole-registry list, so it carries statuses: a not-found default - // is no evidence the survivors are usable, and advising `activate` - // on an unusable one just produces a second error. - let statuses = format_runtime_statuses(manifests); - bail!( - "The configured default ROCm runtime was not found. Pick one in `/runtimes`, re-activate one with `rocm runtimes activate `, or pass `--runtime-id `. Runtimes found: {statuses}. See `rocm runtimes list`." - ) - } - _ => { - // `matches` is filtered by `runtime_id` alone, so it can hold an - // unusable runtime next to a ready one. Statuses, not bare keys: - // `rocm runtimes activate` on the unusable match is a second error. - let statuses = format_runtime_statuses(matches.iter().copied()); - bail!( - "More than one ROCm runtime matches the configured default. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass `--runtime-id `. Runtimes matched: {statuses}. See `rocm runtimes list`." - ) - } - } -} - -fn select_single_ready_runtime( - manifests: &[therock::InstalledRuntimeManifest], -) -> Result<&therock::InstalledRuntimeManifest> { - let ready = manifests - .iter() - .filter(|manifest| runtime_usability_status(manifest) == "ready") - .collect::>(); - match ready.as_slice() { - [manifest] => Ok(*manifest), - [] => { - let statuses = format_runtime_statuses(manifests); - bail!( - "No ROCm runtime is ready. Fix one or install another: run `rocm` and pick \"Set up this system\" (or run `rocm install sdk`), then install ComfyUI. Runtimes found: {statuses}. See `rocm runtimes list`." - ) - } - _ => { - let available = format_available_runtime_keys(ready.iter().copied()); - bail!( - "Multiple ROCm runtimes are ready. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass `--runtime-id `. Available: {available}. See `rocm runtimes list`." - ) - } - } -} - -fn select_runtime_by_selector<'a>( - manifests: &'a [therock::InstalledRuntimeManifest], - selector: &str, -) -> Result<&'a therock::InstalledRuntimeManifest> { - if let Some(manifest) = manifests - .iter() - .find(|manifest| manifest.runtime_key.eq_ignore_ascii_case(selector)) - { - return Ok(manifest); - } - let matches = manifests - .iter() - .filter(|manifest| manifest.runtime_id.eq_ignore_ascii_case(selector)) - .collect::>(); - match matches.as_slice() { - [manifest] => Ok(*manifest), - [] => { - // Same reasoning as the configured-default not-found branch: this - // enumerates every registered runtime, so each entry carries its - // status rather than implying all of them are pickable. - let statuses = format_runtime_statuses(manifests); - bail!( - "ROCm runtime not found: `{selector}`. Pick one in `/runtimes`, re-activate one with `rocm runtimes activate `, or pass the exact runtime key with `--runtime-id `. Runtimes found: {statuses}. See `rocm runtimes list`." - ) - } - _ => { - // Same reasoning as the ambiguous configured-default branch: an - // id match is not a readiness check, so each entry carries its - // status rather than reading as pickable. - let statuses = format_runtime_statuses(matches.iter().copied()); - bail!( - "More than one ROCm runtime matches `{selector}`. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass the exact runtime key with `--runtime-id `. Runtimes matched: {statuses}. See `rocm runtimes list`." - ) - } - } -} - -fn runtime_environment_for_manifest( - paths: &AppPaths, - manifest: &ComfyUiManifest, -) -> Result { - let runtimes = therock::load_runtime_manifests(paths)?; - if let Some(runtime) = runtimes.iter().find(|runtime| { - runtime - .runtime_key - .eq_ignore_ascii_case(&manifest.runtime_key) - || runtime - .runtime_id - .eq_ignore_ascii_case(&manifest.runtime_id) - }) { - return Ok(runtime_environment_from_runtime( - runtime, - &manifest.python_executable, - )); - } - - let mut env = ComfyUiRuntimeEnvironment { - venv_root: infer_python_env_root(&manifest.python_executable), - rocm_root: Some(manifest.runtime_root.clone()), - path_entries: Vec::new(), - library_entries: Vec::new(), - }; - if let Some(bin_dir) = manifest.python_executable.parent() { - push_existing_path(&mut env.path_entries, bin_dir.to_path_buf()); - } - collect_rocm_runtime_paths(&manifest.runtime_root, &mut env); - Ok(env) -} - -fn runtime_environment_from_runtime( - manifest: &therock::InstalledRuntimeManifest, - python: &Path, -) -> ComfyUiRuntimeEnvironment { - let mut env = ComfyUiRuntimeEnvironment { - venv_root: infer_python_env_root(python), - rocm_root: manifest - .rocm_sdk - .as_ref() - .and_then(|sdk| sdk.root_path.clone()) - .or_else(|| Some(manifest.install_root.clone())), - path_entries: Vec::new(), - library_entries: Vec::new(), - }; - if let Some(bin_dir) = python.parent() { - push_existing_path(&mut env.path_entries, bin_dir.to_path_buf()); - } - if let Some(sdk) = manifest.rocm_sdk.as_ref() { - if let Some(bin_path) = sdk.bin_path.as_ref() { - push_existing_path(&mut env.path_entries, bin_path.clone()); - } - for bin_path in &sdk.bin_paths { - push_existing_path(&mut env.path_entries, bin_path.clone()); - } - for library_path in &sdk.library_paths { - push_existing_path(&mut env.library_entries, library_path.clone()); - } - if let Some(root_path) = sdk.root_path.as_ref() { - collect_rocm_runtime_paths(root_path, &mut env); - } - for root_path in &sdk.runtime_roots { - collect_rocm_runtime_paths(root_path, &mut env); - } - } - collect_rocm_runtime_paths(&manifest.install_root, &mut env); - if runtime_is_linux() { - push_existing_path(&mut env.library_entries, PathBuf::from("/usr/lib/wsl/lib")); - } - env -} - -fn collect_rocm_runtime_paths(root: &Path, env: &mut ComfyUiRuntimeEnvironment) { - for path in [ - root.join("bin"), - root.join("lib"), - root.join("lib64"), - root.join("lib").join("rocm_sysdeps").join("lib"), - ] { - if !path.is_dir() { - continue; - } - if path.file_name().and_then(|value| value.to_str()) == Some("bin") { - push_existing_path(&mut env.path_entries, path.clone()); - } - push_existing_path(&mut env.library_entries, path); - } -} - -fn infer_python_env_root(python: &Path) -> Option { - let bin_dir = python.parent()?; - let name = bin_dir.file_name()?.to_string_lossy().to_ascii_lowercase(); - if matches!(name.as_str(), "scripts" | "bin") { - return bin_dir.parent().map(Path::to_path_buf); - } - None -} - -fn apply_runtime_environment(command: &mut Command, env: &ComfyUiRuntimeEnvironment) -> Result<()> { - for (key, value) in runtime_environment_assignments(env)? { - command.env(key, value); - } - Ok(()) -} - -fn runtime_environment_assignments( - env: &ComfyUiRuntimeEnvironment, -) -> Result> { - let mut values = Vec::new(); - if let Some(venv_root) = env.venv_root.as_ref() { - values.push(("VIRTUAL_ENV", venv_root.as_os_str().to_owned())); - } - if let Some(rocm_root) = env.rocm_root.as_ref() { - values.push(("ROCM_PATH", rocm_root.as_os_str().to_owned())); - } - let mut path_entries = env.path_entries.clone(); - if runtime_is_windows() { - path_entries.extend(env.library_entries.iter().cloned()); - } - if let Some(path) = prepend_env_paths(&path_entries, std::env::var_os("PATH"))? { - values.push(("PATH", path)); - } - if runtime_is_linux() - && let Some(ld_library_path) = - prepend_env_paths(&env.library_entries, std::env::var_os("LD_LIBRARY_PATH"))? - { - values.push(("LD_LIBRARY_PATH", ld_library_path)); - } - Ok(values) -} - -fn prepend_env_paths(entries: &[PathBuf], current: Option) -> Result> { - let mut parts = Vec::new(); - for entry in entries { - push_existing_path(&mut parts, entry.clone()); - } - if let Some(current) = current - && !current.is_empty() - { - for entry in split_runtime_paths(¤t) { - push_existing_path(&mut parts, entry); - } - } - if parts.is_empty() { - Ok(None) - } else { - join_runtime_paths(parts) - } -} - -fn split_runtime_paths(paths: &OsString) -> Vec { - runtime_path_list_split(paths) -} - -fn join_runtime_paths(paths: Vec) -> Result> { - runtime_path_list_join(paths).map(Some) -} - -fn push_existing_path(paths: &mut Vec, path: PathBuf) { - if !path.is_dir() { - return; - } - if !paths.iter().any(|seen| same_path_text(seen, &path)) { - paths.push(path); - } -} - -fn same_path_text(left: &Path, right: &Path) -> bool { - runtime_paths_equivalent(left, right) -} - /// Downloads (if not already cached) and unpacks the ComfyUI source archive /// into a staging folder, then installs its code at `source_path` with /// [`source_swap::install_release_tree`]. Returns the source URL it resolved @@ -2735,572 +2376,7 @@ mod tests { Ok(()) } - #[test] - fn default_runtime_selection_uses_single_ready_runtime() -> Result<()> { - let paths = test_paths("comfyui-single-ready-runtime"); - let runtime_root = paths.data_dir.join("runtimes").join("default"); - let python = runtime_python_executable_in_env(&runtime_root); - let python_bin = python - .parent() - .context("runtime Python path has no parent")? - .to_path_buf(); - let sdk_root = runtime_root.join("sdk"); - let sdk_bin = sdk_root.join("bin"); - fs::create_dir_all(&python_bin)?; - fs::create_dir_all(&sdk_bin)?; - fs::write(runtime_root.join(".rocm-cli-runtime.json"), "{}")?; - fs::write(&python, "python")?; - let amdhip = sdk_bin.join(runtime_rocm_library_filename("amdhip64")); - let hipblas = sdk_bin.join(runtime_rocm_library_filename("hipblas")); - fs::write(&amdhip, "amdhip")?; - fs::write(&hipblas, "hipblas")?; - - let manifest = therock::InstalledRuntimeManifest { - runtime_key: "release-pip-gfx120x-all-7-13-0a20260511".to_owned(), - runtime_id: "therock-release:gfx120X-all".to_owned(), - channel: "release".to_owned(), - format: "wheel".to_owned(), - family: "gfx120X-all".to_owned(), - family_source: "test".to_owned(), - version: "7.13.0a20260511".to_owned(), - install_root: runtime_root, - selected_artifact_url: "https://example.invalid/simple".to_owned(), - source_layout_generation: None, - index_url: None, - tarball_file_name: None, - python_launcher: None, - python_executable: Some(python.display().to_string()), - pip_cache_dir: None, - rocm_sdk: Some(therock::RocmSdkPythonProbe { - import_ok: true, - root_path: Some(sdk_root), - bin_path: Some(sdk_bin), - resolved_libraries: vec![ - therock::RocmSdkLibraryProbe { - shortname: "amdhip64".to_owned(), - paths: vec![amdhip], - }, - therock::RocmSdkLibraryProbe { - shortname: "hipblas".to_owned(), - paths: vec![hipblas], - }, - ], - ..therock::RocmSdkPythonProbe::default() - }), - sdk_torch: None, - wheel_composition: None, - read_only: false, - imported_from: None, - devel: true, - installed_at_unix_ms: 100, - }; - - let manifests = [manifest]; - let selected = select_default_runtime(&RocmCliConfig::default(), &manifests)?; - assert_eq!( - selected.runtime_key, - "release-pip-gfx120x-all-7-13-0a20260511" - ); - Ok(()) - } - - /// Every ambiguity / not-found remediation message must steer the user to a - /// concrete next step: the TUI picker, the durable activate command, the - /// per-invocation flag, and the discovery command. Asserted centrally so a - /// future reworded branch that silently drops one is caught. - fn assert_actionable(message: &str) { - for needle in [ - "/runtimes", - "rocm runtimes activate", - "--runtime-id ", - "rocm runtimes list", - ] { - assert!( - message.contains(needle), - "actionable error must mention {needle:?}, got: {message}" - ); - } - } - - /// Build a runtime that registers but is not usable, so the "no ready - /// runtime" path has a per-manifest reason to surface. A missing install - /// root fails `validate_runtime_manifest_for_activation`'s first on-disk - /// check, yielding `unusable (install root is missing: …)`. - fn unusable_runtime_manifest( - paths: &AppPaths, - runtime_key: &str, - ) -> Result { - let mut manifest = ready_runtime_manifest(paths, runtime_key)?; - manifest.install_root = paths - .data_dir - .join("missing-runtime-root") - .join(runtime_key); - Ok(manifest) - } - - #[test] - fn ambiguous_ready_runtimes_name_flag_and_list_keys() -> Result<()> { - let paths = test_paths("comfyui-ambiguous-ready-runtimes"); - let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - - let manifests = [release, nightly]; - let error = select_default_runtime(&RocmCliConfig::default(), &manifests) - .expect_err("two ready runtimes should be ambiguous"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("Multiple ROCm runtimes are ready"), - "error should be the multi-ready branch, got: {message}" - ); - assert!( - message.contains("release-wheel-gfx94x-dcgpu-7-13-0") - && message.contains("nightly-wheel-gfx94x-dcgpu-7-14-0"), - "error should list the available runtime keys, got: {message}" - ); - // This set is ready-filtered, so `format_available_runtime_keys`' bare - // `key, key` is the deliberate rendering (see its doc comment): a - // per-key status here would annotate every entry `(ready)` and say - // nothing. Without this, swapping in `format_runtime_statuses` would - // still pass the key assertions above. - assert!( - !message.contains("(ready)"), - "a ready-filtered list must stay bare keys, not per-key statuses, got: {message}" - ); - Ok(()) - } - - #[test] - fn no_ready_runtime_lists_each_key_and_reason() -> Result<()> { - // The "none ready" branch used to duplicate the zero-runtime advice and - // drop the reason each runtime is unusable. It must now name the key and - // surface the reason `runtime_usability_status` already computed. - let paths = test_paths("comfyui-no-ready-runtime"); - let unusable = unusable_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - - let manifests = [unusable]; - let error = select_default_runtime(&RocmCliConfig::default(), &manifests) - .expect_err("an unusable runtime is not ready"); - let message = error.to_string(); - - assert!( - message.contains("release-wheel-gfx94x-dcgpu-7-13-0"), - "error should name the unusable runtime key, got: {message}" - ); - assert!( - message.contains("install root is missing"), - "error should surface the per-manifest unusable reason, got: {message}" - ); - assert!( - message.contains("rocm runtimes list"), - "error should point to `rocm runtimes list`, got: {message}" - ); - // The setup pointer must name a destination that actually exists. The - // only ROCm-setup entry point a user can see is the bare-`rocm` - // launcher row labelled "Set up this system" - // (`crates/rocm-dash-tui/src/ui/launcher.rs`, rendered verbatim from - // `ROWS`); there has never been a screen called "Set Up ROCm". - // - // The positive assertion already catches a plain revert to either wrong - // wording on its own: neither the invented label this branch emitted - // until now ("install another from Set Up ROCm") nor the superseded - // pre-`31957ed` wording ("Set up ROCm first from Set Up ROCm") contains - // `pick "Set up this system"`, so under either revert the positive - // needle stops matching and reddens. Measured, not assumed: reverting - // the production string to the invented label fails the positive - // assertion, and fails the negative one too once the positive is - // neutralized. - // - // The negative assertion is therefore not redundant, but its value is - // the case the positive one cannot see: a message carrying *both* - // labels. Re-adding "Set Up ROCm" alongside the real row still - // satisfies the positive needle, so only the negative one reddens — - // measured the same way. That is the drift worth guarding here, since - // the invented screen name is likelier to creep back as an "extra" - // pointer than as a wholesale revert. - // - // Nor can `assert_actionable` cover this branch — it is deliberately - // excluded, since the none-ready message carries only one of its four - // needles (`rocm runtimes list`); the other three are remedies for - // choosing between runtimes, which do not apply when none is usable. - assert!( - message.contains("pick \"Set up this system\""), - "error should still point at the launcher's \"Set up this system\" row, got: {message}" - ); - assert!( - !message.contains("Set Up ROCm"), - "the setup pointer must not name the nonexistent `Set Up ROCm` screen, got: {message}" - ); - Ok(()) - } - - #[test] - fn zero_runtime_setup_pointers_name_the_real_launcher_row() -> Result<()> { - // The none-ready pointer above is one of three siblings that send a - // user off to install ROCm; the other two fire when the registry is - // empty rather than merely unusable. Until now only the none-ready one - // was pinned, so these two could drift back to the invented `Set Up - // ROCm` screen name without reddening anything. Same needles, same - // reasoning as `no_ready_runtime_lists_each_key_and_reason`: the - // positive one catches a wholesale revert, the negative one catches the - // invented label creeping back *alongside* the real row. - // - // `test_paths` never creates the runtime registry directory, so - // `load_runtime_manifests` returns empty and both zero-runtime branches - // are the ones under test. - let paths = test_paths("comfyui-zero-runtime-setup-pointers"); - - // `render_status`' "ROCm" note (the zero-runtime arm of `render_status`). - let status = render_status(&paths, &RocmCliConfig::default())?; - assert!( - status.contains("pick \"Set up this system\""), - "status should point at the launcher's \"Set up this system\" row, got: {status}" - ); - assert!( - !status.contains("Set Up ROCm"), - "status must not name the nonexistent `Set Up ROCm` screen, got: {status}" - ); - - // `select_runtime`'s bail when nothing is registered at all. - let error = select_runtime(&paths, &RocmCliConfig::default(), None) - .expect_err("no registered runtime means no selection"); - let message = error.to_string(); - assert!( - message.contains("pick \"Set up this system\""), - "the zero-runtime refusal should point at the launcher's \"Set up this system\" row, got: {message}" - ); - assert!( - !message.contains("Set Up ROCm"), - "the zero-runtime refusal must not name the nonexistent `Set Up ROCm` screen, got: {message}" - ); - Ok(()) - } - - #[test] - fn configured_default_not_found_is_actionable() -> Result<()> { - // This branch enumerates the WHOLE registry, so it is planted with one - // ready and one unusable runtime: a bare key list here would advise - // `rocm runtimes activate` on the unusable one and land the user on a - // second error, so each entry must carry its status. - let paths = test_paths("comfyui-default-not-found"); - let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - - let manifests = [ready, unusable]; - let config = RocmCliConfig { - default_runtime_id: Some("no-such-runtime-id".to_owned()), - ..Default::default() - }; - let error = select_default_runtime(&config, &manifests) - .expect_err("configured default id matches nothing"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("The configured default ROCm runtime was not found"), - "error should be the configured-default-not-found branch, got: {message}" - ); - assert!( - message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), - "the usable runtime must be listed as ready, got: {message}" - ); - assert!( - message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") - && message.contains("install root is missing"), - "the unusable runtime must carry its reason, not read as pickable, got: {message}" - ); - Ok(()) - } - - #[test] - fn multiple_configured_default_matches_is_actionable() -> Result<()> { - // `ready_runtime_manifest` hardcodes the same runtime_id for every key, - // so a configured default resolving to that id matches both entries and - // exercises `select_default_runtime`'s own ambiguity arm (distinct from - // the single-ready arm the other test hits). - let paths = test_paths("comfyui-default-ambiguous"); - let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - - let manifests = [release, nightly]; - let config = RocmCliConfig { - default_runtime_id: Some("therock-release:gfx120X-all".to_owned()), - ..Default::default() - }; - let error = select_default_runtime(&config, &manifests) - .expect_err("two manifests share the configured default id"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("More than one ROCm runtime matches the configured default"), - "error should be the ambiguous-configured-default branch, got: {message}" - ); - Ok(()) - } - - #[test] - fn ambiguous_configured_default_marks_the_unusable_match() -> Result<()> { - // The ambiguity set is filtered by `runtime_id` alone, and a shared - // runtime_id says nothing about readiness: one of the two matches here - // has a missing install root. A bare key list would tell the user to - // `rocm runtimes activate` it and land them on a second error, so each - // match must carry the status `runtime_usability_status` computes. - let paths = test_paths("comfyui-default-ambiguous-unusable"); - let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - assert_eq!( - ready.runtime_id, unusable.runtime_id, - "the two manifests must share a runtime_id to reach the ambiguity arm" - ); - - let manifests = [ready, unusable]; - let config = RocmCliConfig { - default_runtime_id: Some("therock-release:gfx120X-all".to_owned()), - ..Default::default() - }; - let error = select_default_runtime(&config, &manifests) - .expect_err("two manifests share the configured default id"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("More than one ROCm runtime matches the configured default"), - "error should be the ambiguous-configured-default branch, got: {message}" - ); - assert!( - message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), - "the usable match must be listed as ready, got: {message}" - ); - assert!( - message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") - && message.contains("install root is missing"), - "the unusable match must carry its reason, not read as pickable, got: {message}" - ); - Ok(()) - } - - #[test] - fn unknown_selector_is_actionable() -> Result<()> { - // Whole-registry list again — see `configured_default_not_found_is_actionable`. - let paths = test_paths("comfyui-selector-not-found"); - let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - - let manifests = [ready, unusable]; - let error = select_runtime_by_selector(&manifests, "no-such-key") - .expect_err("selector matches no runtime"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("ROCm runtime not found: `no-such-key`"), - "error should be the unknown-selector branch, got: {message}" - ); - assert!( - message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), - "the usable runtime must be listed as ready, got: {message}" - ); - assert!( - message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") - && message.contains("install root is missing"), - "the unusable runtime must carry its reason, not read as pickable, got: {message}" - ); - Ok(()) - } - - #[test] - fn ambiguous_selector_is_actionable() -> Result<()> { - // Both manifests share runtime_id `therock-release:gfx120X-all`; neither - // runtime_key equals it, so selection falls through to the id filter and - // matches both, hitting `select_runtime_by_selector`'s ambiguity arm. - let paths = test_paths("comfyui-selector-ambiguous"); - let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - - let manifests = [release, nightly]; - let error = select_runtime_by_selector(&manifests, "therock-release:gfx120X-all") - .expect_err("selector id matches two runtimes"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("More than one ROCm runtime matches `therock-release:gfx120X-all`"), - "error should be the ambiguous-selector branch, got: {message}" - ); - Ok(()) - } - - #[test] - fn ambiguous_selector_marks_the_unusable_match() -> Result<()> { - // Selector twin of `ambiguous_configured_default_marks_the_unusable_match`: - // the selector falls through to the `runtime_id` filter, which is not a - // readiness check, so the unusable match must be rendered with its - // reason rather than as a bare activatable key. - let paths = test_paths("comfyui-selector-ambiguous-unusable"); - let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; - assert_eq!( - ready.runtime_id, unusable.runtime_id, - "the two manifests must share a runtime_id to reach the ambiguity arm" - ); - - let manifests = [ready, unusable]; - let error = select_runtime_by_selector(&manifests, "therock-release:gfx120X-all") - .expect_err("selector id matches two runtimes"); - let message = error.to_string(); - - assert_actionable(&message); - assert!( - message.contains("More than one ROCm runtime matches `therock-release:gfx120X-all`"), - "error should be the ambiguous-selector branch, got: {message}" - ); - assert!( - message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), - "the usable match must be listed as ready, got: {message}" - ); - assert!( - message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") - && message.contains("install root is missing"), - "the unusable match must carry its reason, not read as pickable, got: {message}" - ); - Ok(()) - } - - #[test] - fn selected_unusable_runtime_reports_not_ready_reason() -> Result<()> { - // Naming a runtime explicitly by selector bypasses the readiness filter - // in `select_default_runtime` / `select_single_ready_runtime`, so - // `select_runtime` re-checks it and bails with the reworked - // "The selected ROCm runtime is not ready: " message. This is - // also why the sibling "has no Python executable" arm is unreachable: - // `validate_wheel_runtime_manifest` already rejects a ready wheel runtime - // whose python is missing, so any such runtime lands on this not-ready - // arm — carrying the reason — rather than on the no-python arm. - let paths = test_paths("comfyui-selected-unusable"); - let unusable = unusable_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; - write_runtime_manifest(&paths, &unusable)?; - - let error = select_runtime( - &paths, - &RocmCliConfig::default(), - Some("release-wheel-gfx94x-dcgpu-7-13-0"), - ) - .expect_err("an explicitly selected unusable runtime is not ready"); - let message = error.to_string(); - - assert!( - message.contains("The selected ROCm runtime is not ready"), - "error should use the reworked `ROCm runtime` noun, got: {message}" - ); - assert!( - message.contains("install root is missing"), - "error should surface the per-manifest unusable reason, got: {message}" - ); - Ok(()) - } - - #[test] - fn runtime_environment_preloads_managed_rocm_paths() -> Result<()> { - let paths = test_paths("comfyui-runtime-env"); - let env_root = paths.data_dir.join("envs").join("therock"); - let python = runtime_python_executable_in_env(&env_root); - let python_bin = python - .parent() - .context("runtime Python path has no parent")? - .to_path_buf(); - let sdk_root = paths.data_dir.join("runtimes").join("sdk"); - let sdk_bin = sdk_root.join("bin"); - let sdk_lib = sdk_root.join("lib"); - let runtime_root = paths.data_dir.join("runtimes").join("default"); - let runtime_bin = runtime_root.join("bin"); - let runtime_lib = runtime_root.join("lib"); - let runtime_sysdeps = runtime_root.join("lib").join("rocm_sysdeps").join("lib"); - for dir in [ - &python_bin, - &sdk_bin, - &sdk_lib, - &runtime_bin, - &runtime_lib, - &runtime_sysdeps, - ] { - fs::create_dir_all(dir)?; - } - - let manifest = therock::InstalledRuntimeManifest { - runtime_key: "therock-release-gfx120x-all".to_owned(), - runtime_id: "therock-release:gfx120X-all".to_owned(), - channel: "release".to_owned(), - format: "wheel".to_owned(), - family: "gfx120X-all".to_owned(), - family_source: "test".to_owned(), - version: "7.13.0a20260511".to_owned(), - install_root: runtime_root.clone(), - selected_artifact_url: "https://example.invalid/simple".to_owned(), - source_layout_generation: None, - index_url: None, - tarball_file_name: None, - python_launcher: None, - python_executable: Some(python.display().to_string()), - pip_cache_dir: None, - rocm_sdk: Some(therock::RocmSdkPythonProbe { - import_ok: true, - root_path: Some(sdk_root.clone()), - bin_path: Some(sdk_bin.clone()), - runtime_roots: vec![runtime_root], - bin_paths: vec![sdk_bin.clone()], - library_paths: vec![sdk_lib.clone()], - ..Default::default() - }), - sdk_torch: None, - wheel_composition: None, - read_only: false, - imported_from: None, - devel: true, - installed_at_unix_ms: 100, - }; - - let env = runtime_environment_from_runtime(&manifest, &python); - let mut command = Command::new(&python); - apply_runtime_environment(&mut command, &env)?; - - assert_eq!( - command_env_value(&command, "VIRTUAL_ENV").as_deref(), - Some(env_root.as_os_str()) - ); - assert_eq!( - command_env_value(&command, "ROCM_PATH").as_deref(), - Some(sdk_root.as_os_str()) - ); - let path = command_env_value(&command, "PATH").context("PATH should be set")?; - let path_entries = split_runtime_paths(&path); - assert!(path_entries.contains(&python_bin)); - assert!(path_entries.contains(&sdk_bin)); - assert!(path_entries.contains(&runtime_bin)); - - if runtime_is_windows() { - assert!(path_entries.contains(&sdk_lib)); - assert!(path_entries.contains(&runtime_lib)); - } else if runtime_is_linux() { - let ld_library_path = - command_env_value(&command, "LD_LIBRARY_PATH").context("LD_LIBRARY_PATH")?; - let library_entries = split_runtime_paths(&ld_library_path); - assert!(library_entries.contains(&sdk_lib)); - assert!(library_entries.contains(&runtime_lib)); - assert!(library_entries.contains(&runtime_sysdeps)); - } - Ok(()) - } - - fn command_env_value(command: &Command, key: &str) -> Option { - command - .get_envs() - .find(|(name, _)| name.to_string_lossy() == key) - .and_then(|(_, value)| value.map(OsString::from)) - } - - fn ready_runtime_manifest( + pub(super) fn ready_runtime_manifest( paths: &AppPaths, runtime_key: &str, ) -> Result { @@ -3362,7 +2438,7 @@ mod tests { }) } - fn write_runtime_manifest( + pub(super) fn write_runtime_manifest( paths: &AppPaths, manifest: &therock::InstalledRuntimeManifest, ) -> Result<()> { @@ -3375,7 +2451,7 @@ mod tests { Ok(()) } - fn test_paths(name: &str) -> AppPaths { + pub(super) fn test_paths(name: &str) -> AppPaths { let nonce = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap_or_default() diff --git a/apps/rocm/src/comfyui/runtime_env.rs b/apps/rocm/src/comfyui/runtime_env.rs new file mode 100644 index 0000000000..3e496900be --- /dev/null +++ b/apps/rocm/src/comfyui/runtime_env.rs @@ -0,0 +1,967 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Which installed ROCm runtime ComfyUI uses, and the process environment that +//! runtime needs. +//! +//! [`select_runtime`] turns an explicit `--runtime-id` or the configured +//! default into the one ready managed Python runtime an install runs against, +//! and explains how to pick one when none or several qualify. The +//! `runtime_environment_*` functions build the `VIRTUAL_ENV`, `ROCM_PATH`, +//! `PATH` and library-path settings a ComfyUI child process runs with, and +//! [`apply_runtime_environment`] puts them on a command. +//! +//! The types these functions return ([`SelectedRuntime`], +//! [`ComfyUiRuntimeEnvironment`]) stay in the parent module with the other +//! ComfyUI types. + +use super::{ComfyUiManifest, ComfyUiRuntimeEnvironment, SelectedRuntime}; +use crate::runtimes_cmd::runtime_usability_status; +use crate::therock; +use anyhow::{Context, Result, bail}; +use rocm_core::{ + AppPaths, RocmCliConfig, runtime_is_linux, runtime_is_windows, runtime_path_list_join, + runtime_path_list_split, runtime_paths_equivalent, +}; +use std::ffi::OsString; +use std::path::{Path, PathBuf}; +use std::process::Command; + +pub(super) fn select_runtime( + paths: &AppPaths, + config: &RocmCliConfig, + selector: Option<&str>, +) -> Result { + let manifests = therock::load_runtime_manifests(paths)?; + if manifests.is_empty() { + bail!( + "Install ROCm first: run `rocm` and pick \"Set up this system\", then install ComfyUI." + ); + } + let manifest = match selector.map(str::trim).filter(|value| !value.is_empty()) { + Some(selector) => select_runtime_by_selector(&manifests, selector)?.clone(), + None => select_default_runtime(config, &manifests)?.clone(), + }; + if runtime_usability_status(&manifest) != "ready" { + bail!( + "The selected ROCm runtime is not ready: {}", + runtime_usability_status(&manifest) + ); + } + if manifest.format != "wheel" { + bail!("ComfyUI installs require a rocm-cli managed Python ROCm runtime."); + } + // Defence in depth, not a reachable branch on a settled filesystem: the + // `ready` check above ran `validate_wheel_runtime_manifest`, which already + // proved `python_executable` is `Some` and `is_file()`. This re-check only + // fires if the interpreter disappears in the window between the two calls, + // so keep it (a missing interpreter must not reach the installer) and do + // not spend further polish on its wording. + let python = manifest + .python_executable + .as_deref() + .map(PathBuf::from) + .filter(|path| path.is_file()) + .with_context(|| { + "The selected ROCm runtime has no Python executable. Pick another in `/runtimes` or pass `--runtime-id `. See `rocm runtimes list`." + .to_string() + })?; + Ok(SelectedRuntime { manifest, python }) +} + +/// Render a bare `key, key` list. Sound only where the caller has already +/// filtered `manifests` by `runtime_usability_status(...) == "ready"`, because +/// that is the only filter that makes every listed key a valid answer to the +/// `rocm runtimes activate ` advice these messages carry; a per-key status +/// would then be noise. Matching a selector is *not* such a filter: two +/// manifests can share a `runtime_id` with one of them unusable, so an +/// id-matched set can contain a key that cannot be activated. Every branch +/// whose set is not ready-filtered — the whole-registry ones and the two +/// id-ambiguity ones — uses `format_runtime_statuses` instead; see its doc +/// comment for why the two labels differ. +fn format_available_runtime_keys<'a>( + manifests: impl IntoIterator, +) -> String { + manifests + .into_iter() + .map(|manifest| manifest.runtime_key.as_str()) + .collect::>() + .join(", ") +} + +/// Render each known runtime as `` `key` (status) ``, surfacing the per-manifest +/// reason `runtime_usability_status` already computed (e.g. +/// `unusable (install root is missing: …)`) instead of dropping it. +/// +/// Used by every branch whose set is not ready-filtered: the whole-registry +/// ones ("none ready" and the two not-found branches) and the two branches that +/// report an ambiguous `runtime_id`. None of them can promise the listed +/// runtimes are pickable — a shared `runtime_id` says nothing about readiness — +/// so printing bare keys under `Available:` would invite the user to +/// `rocm runtimes activate` an unusable one and land on a second error. The +/// label split is deliberate and load-bearing: `Available:` means "any of these +/// works", `Runtimes found:` means "here is everything registered, with why", +/// and `Runtimes matched:` means "here is what the selector matched, with why". +fn format_runtime_statuses<'a>( + manifests: impl IntoIterator, +) -> String { + manifests + .into_iter() + .map(|manifest| { + format!( + "`{}` ({})", + manifest.runtime_key, + runtime_usability_status(manifest) + ) + }) + .collect::>() + .join(", ") +} + +fn select_default_runtime<'a>( + config: &RocmCliConfig, + manifests: &'a [therock::InstalledRuntimeManifest], +) -> Result<&'a therock::InstalledRuntimeManifest> { + if let Some(active_key) = config.active_runtime_key.as_deref() { + return select_runtime_by_selector(manifests, active_key); + } + let Some(default_runtime_id) = config.default_runtime_id.as_deref() else { + return select_single_ready_runtime(manifests); + }; + let matches = manifests + .iter() + .filter(|manifest| manifest.runtime_id.eq_ignore_ascii_case(default_runtime_id)) + .collect::>(); + match matches.as_slice() { + [manifest] => Ok(*manifest), + [] => { + // Whole-registry list, so it carries statuses: a not-found default + // is no evidence the survivors are usable, and advising `activate` + // on an unusable one just produces a second error. + let statuses = format_runtime_statuses(manifests); + bail!( + "The configured default ROCm runtime was not found. Pick one in `/runtimes`, re-activate one with `rocm runtimes activate `, or pass `--runtime-id `. Runtimes found: {statuses}. See `rocm runtimes list`." + ) + } + _ => { + // `matches` is filtered by `runtime_id` alone, so it can hold an + // unusable runtime next to a ready one. Statuses, not bare keys: + // `rocm runtimes activate` on the unusable match is a second error. + let statuses = format_runtime_statuses(matches.iter().copied()); + bail!( + "More than one ROCm runtime matches the configured default. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass `--runtime-id `. Runtimes matched: {statuses}. See `rocm runtimes list`." + ) + } + } +} + +fn select_single_ready_runtime( + manifests: &[therock::InstalledRuntimeManifest], +) -> Result<&therock::InstalledRuntimeManifest> { + let ready = manifests + .iter() + .filter(|manifest| runtime_usability_status(manifest) == "ready") + .collect::>(); + match ready.as_slice() { + [manifest] => Ok(*manifest), + [] => { + let statuses = format_runtime_statuses(manifests); + bail!( + "No ROCm runtime is ready. Fix one or install another: run `rocm` and pick \"Set up this system\" (or run `rocm install sdk`), then install ComfyUI. Runtimes found: {statuses}. See `rocm runtimes list`." + ) + } + _ => { + let available = format_available_runtime_keys(ready.iter().copied()); + bail!( + "Multiple ROCm runtimes are ready. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass `--runtime-id `. Available: {available}. See `rocm runtimes list`." + ) + } + } +} + +fn select_runtime_by_selector<'a>( + manifests: &'a [therock::InstalledRuntimeManifest], + selector: &str, +) -> Result<&'a therock::InstalledRuntimeManifest> { + if let Some(manifest) = manifests + .iter() + .find(|manifest| manifest.runtime_key.eq_ignore_ascii_case(selector)) + { + return Ok(manifest); + } + let matches = manifests + .iter() + .filter(|manifest| manifest.runtime_id.eq_ignore_ascii_case(selector)) + .collect::>(); + match matches.as_slice() { + [manifest] => Ok(*manifest), + [] => { + // Same reasoning as the configured-default not-found branch: this + // enumerates every registered runtime, so each entry carries its + // status rather than implying all of them are pickable. + let statuses = format_runtime_statuses(manifests); + bail!( + "ROCm runtime not found: `{selector}`. Pick one in `/runtimes`, re-activate one with `rocm runtimes activate `, or pass the exact runtime key with `--runtime-id `. Runtimes found: {statuses}. See `rocm runtimes list`." + ) + } + _ => { + // Same reasoning as the ambiguous configured-default branch: an + // id match is not a readiness check, so each entry carries its + // status rather than reading as pickable. + let statuses = format_runtime_statuses(matches.iter().copied()); + bail!( + "More than one ROCm runtime matches `{selector}`. Pick one in `/runtimes`, set a default with `rocm runtimes activate `, or pass the exact runtime key with `--runtime-id `. Runtimes matched: {statuses}. See `rocm runtimes list`." + ) + } + } +} + +pub(super) fn runtime_environment_for_manifest( + paths: &AppPaths, + manifest: &ComfyUiManifest, +) -> Result { + let runtimes = therock::load_runtime_manifests(paths)?; + if let Some(runtime) = runtimes.iter().find(|runtime| { + runtime + .runtime_key + .eq_ignore_ascii_case(&manifest.runtime_key) + || runtime + .runtime_id + .eq_ignore_ascii_case(&manifest.runtime_id) + }) { + return Ok(runtime_environment_from_runtime( + runtime, + &manifest.python_executable, + )); + } + + let mut env = ComfyUiRuntimeEnvironment { + venv_root: infer_python_env_root(&manifest.python_executable), + rocm_root: Some(manifest.runtime_root.clone()), + path_entries: Vec::new(), + library_entries: Vec::new(), + }; + if let Some(bin_dir) = manifest.python_executable.parent() { + push_existing_path(&mut env.path_entries, bin_dir.to_path_buf()); + } + collect_rocm_runtime_paths(&manifest.runtime_root, &mut env); + Ok(env) +} + +pub(super) fn runtime_environment_from_runtime( + manifest: &therock::InstalledRuntimeManifest, + python: &Path, +) -> ComfyUiRuntimeEnvironment { + let mut env = ComfyUiRuntimeEnvironment { + venv_root: infer_python_env_root(python), + rocm_root: manifest + .rocm_sdk + .as_ref() + .and_then(|sdk| sdk.root_path.clone()) + .or_else(|| Some(manifest.install_root.clone())), + path_entries: Vec::new(), + library_entries: Vec::new(), + }; + if let Some(bin_dir) = python.parent() { + push_existing_path(&mut env.path_entries, bin_dir.to_path_buf()); + } + if let Some(sdk) = manifest.rocm_sdk.as_ref() { + if let Some(bin_path) = sdk.bin_path.as_ref() { + push_existing_path(&mut env.path_entries, bin_path.clone()); + } + for bin_path in &sdk.bin_paths { + push_existing_path(&mut env.path_entries, bin_path.clone()); + } + for library_path in &sdk.library_paths { + push_existing_path(&mut env.library_entries, library_path.clone()); + } + if let Some(root_path) = sdk.root_path.as_ref() { + collect_rocm_runtime_paths(root_path, &mut env); + } + for root_path in &sdk.runtime_roots { + collect_rocm_runtime_paths(root_path, &mut env); + } + } + collect_rocm_runtime_paths(&manifest.install_root, &mut env); + if runtime_is_linux() { + push_existing_path(&mut env.library_entries, PathBuf::from("/usr/lib/wsl/lib")); + } + env +} + +fn collect_rocm_runtime_paths(root: &Path, env: &mut ComfyUiRuntimeEnvironment) { + for path in [ + root.join("bin"), + root.join("lib"), + root.join("lib64"), + root.join("lib").join("rocm_sysdeps").join("lib"), + ] { + if !path.is_dir() { + continue; + } + if path.file_name().and_then(|value| value.to_str()) == Some("bin") { + push_existing_path(&mut env.path_entries, path.clone()); + } + push_existing_path(&mut env.library_entries, path); + } +} + +fn infer_python_env_root(python: &Path) -> Option { + let bin_dir = python.parent()?; + let name = bin_dir.file_name()?.to_string_lossy().to_ascii_lowercase(); + if matches!(name.as_str(), "scripts" | "bin") { + return bin_dir.parent().map(Path::to_path_buf); + } + None +} + +pub(super) fn apply_runtime_environment( + command: &mut Command, + env: &ComfyUiRuntimeEnvironment, +) -> Result<()> { + for (key, value) in runtime_environment_assignments(env)? { + command.env(key, value); + } + Ok(()) +} + +pub(super) fn runtime_environment_assignments( + env: &ComfyUiRuntimeEnvironment, +) -> Result> { + let mut values = Vec::new(); + if let Some(venv_root) = env.venv_root.as_ref() { + values.push(("VIRTUAL_ENV", venv_root.as_os_str().to_owned())); + } + if let Some(rocm_root) = env.rocm_root.as_ref() { + values.push(("ROCM_PATH", rocm_root.as_os_str().to_owned())); + } + let mut path_entries = env.path_entries.clone(); + if runtime_is_windows() { + path_entries.extend(env.library_entries.iter().cloned()); + } + if let Some(path) = prepend_env_paths(&path_entries, std::env::var_os("PATH"))? { + values.push(("PATH", path)); + } + if runtime_is_linux() + && let Some(ld_library_path) = + prepend_env_paths(&env.library_entries, std::env::var_os("LD_LIBRARY_PATH"))? + { + values.push(("LD_LIBRARY_PATH", ld_library_path)); + } + Ok(values) +} + +fn prepend_env_paths(entries: &[PathBuf], current: Option) -> Result> { + let mut parts = Vec::new(); + for entry in entries { + push_existing_path(&mut parts, entry.clone()); + } + if let Some(current) = current + && !current.is_empty() + { + for entry in split_runtime_paths(¤t) { + push_existing_path(&mut parts, entry); + } + } + if parts.is_empty() { + Ok(None) + } else { + join_runtime_paths(parts) + } +} + +fn split_runtime_paths(paths: &OsString) -> Vec { + runtime_path_list_split(paths) +} + +fn join_runtime_paths(paths: Vec) -> Result> { + runtime_path_list_join(paths).map(Some) +} + +fn push_existing_path(paths: &mut Vec, path: PathBuf) { + if !path.is_dir() { + return; + } + if !paths.iter().any(|seen| same_path_text(seen, &path)) { + paths.push(path); + } +} + +pub(super) fn same_path_text(left: &Path, right: &Path) -> bool { + runtime_paths_equivalent(left, right) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::comfyui::render_status; + use crate::comfyui::tests::{ready_runtime_manifest, test_paths, write_runtime_manifest}; + use rocm_core::{runtime_python_executable_in_env, runtime_rocm_library_filename}; + use std::fs; + + #[test] + fn default_runtime_selection_uses_single_ready_runtime() -> Result<()> { + let paths = test_paths("comfyui-single-ready-runtime"); + let runtime_root = paths.data_dir.join("runtimes").join("default"); + let python = runtime_python_executable_in_env(&runtime_root); + let python_bin = python + .parent() + .context("runtime Python path has no parent")? + .to_path_buf(); + let sdk_root = runtime_root.join("sdk"); + let sdk_bin = sdk_root.join("bin"); + fs::create_dir_all(&python_bin)?; + fs::create_dir_all(&sdk_bin)?; + fs::write(runtime_root.join(".rocm-cli-runtime.json"), "{}")?; + fs::write(&python, "python")?; + let amdhip = sdk_bin.join(runtime_rocm_library_filename("amdhip64")); + let hipblas = sdk_bin.join(runtime_rocm_library_filename("hipblas")); + fs::write(&amdhip, "amdhip")?; + fs::write(&hipblas, "hipblas")?; + + let manifest = therock::InstalledRuntimeManifest { + runtime_key: "release-pip-gfx120x-all-7-13-0a20260511".to_owned(), + runtime_id: "therock-release:gfx120X-all".to_owned(), + channel: "release".to_owned(), + format: "wheel".to_owned(), + family: "gfx120X-all".to_owned(), + family_source: "test".to_owned(), + version: "7.13.0a20260511".to_owned(), + install_root: runtime_root, + selected_artifact_url: "https://example.invalid/simple".to_owned(), + source_layout_generation: None, + index_url: None, + tarball_file_name: None, + python_launcher: None, + python_executable: Some(python.display().to_string()), + pip_cache_dir: None, + rocm_sdk: Some(therock::RocmSdkPythonProbe { + import_ok: true, + root_path: Some(sdk_root), + bin_path: Some(sdk_bin), + resolved_libraries: vec![ + therock::RocmSdkLibraryProbe { + shortname: "amdhip64".to_owned(), + paths: vec![amdhip], + }, + therock::RocmSdkLibraryProbe { + shortname: "hipblas".to_owned(), + paths: vec![hipblas], + }, + ], + ..therock::RocmSdkPythonProbe::default() + }), + sdk_torch: None, + wheel_composition: None, + read_only: false, + imported_from: None, + devel: true, + installed_at_unix_ms: 100, + }; + + let manifests = [manifest]; + let selected = select_default_runtime(&RocmCliConfig::default(), &manifests)?; + assert_eq!( + selected.runtime_key, + "release-pip-gfx120x-all-7-13-0a20260511" + ); + Ok(()) + } + + /// Every ambiguity / not-found remediation message must steer the user to a + /// concrete next step: the TUI picker, the durable activate command, the + /// per-invocation flag, and the discovery command. Asserted centrally so a + /// future reworded branch that silently drops one is caught. + fn assert_actionable(message: &str) { + for needle in [ + "/runtimes", + "rocm runtimes activate", + "--runtime-id ", + "rocm runtimes list", + ] { + assert!( + message.contains(needle), + "actionable error must mention {needle:?}, got: {message}" + ); + } + } + + /// Build a runtime that registers but is not usable, so the "no ready + /// runtime" path has a per-manifest reason to surface. A missing install + /// root fails `validate_runtime_manifest_for_activation`'s first on-disk + /// check, yielding `unusable (install root is missing: …)`. + fn unusable_runtime_manifest( + paths: &AppPaths, + runtime_key: &str, + ) -> Result { + let mut manifest = ready_runtime_manifest(paths, runtime_key)?; + manifest.install_root = paths + .data_dir + .join("missing-runtime-root") + .join(runtime_key); + Ok(manifest) + } + + #[test] + fn ambiguous_ready_runtimes_name_flag_and_list_keys() -> Result<()> { + let paths = test_paths("comfyui-ambiguous-ready-runtimes"); + let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + + let manifests = [release, nightly]; + let error = select_default_runtime(&RocmCliConfig::default(), &manifests) + .expect_err("two ready runtimes should be ambiguous"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("Multiple ROCm runtimes are ready"), + "error should be the multi-ready branch, got: {message}" + ); + assert!( + message.contains("release-wheel-gfx94x-dcgpu-7-13-0") + && message.contains("nightly-wheel-gfx94x-dcgpu-7-14-0"), + "error should list the available runtime keys, got: {message}" + ); + // This set is ready-filtered, so `format_available_runtime_keys`' bare + // `key, key` is the deliberate rendering (see its doc comment): a + // per-key status here would annotate every entry `(ready)` and say + // nothing. Without this, swapping in `format_runtime_statuses` would + // still pass the key assertions above. + assert!( + !message.contains("(ready)"), + "a ready-filtered list must stay bare keys, not per-key statuses, got: {message}" + ); + Ok(()) + } + + #[test] + fn no_ready_runtime_lists_each_key_and_reason() -> Result<()> { + // The "none ready" branch used to duplicate the zero-runtime advice and + // drop the reason each runtime is unusable. It must now name the key and + // surface the reason `runtime_usability_status` already computed. + let paths = test_paths("comfyui-no-ready-runtime"); + let unusable = unusable_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + + let manifests = [unusable]; + let error = select_default_runtime(&RocmCliConfig::default(), &manifests) + .expect_err("an unusable runtime is not ready"); + let message = error.to_string(); + + assert!( + message.contains("release-wheel-gfx94x-dcgpu-7-13-0"), + "error should name the unusable runtime key, got: {message}" + ); + assert!( + message.contains("install root is missing"), + "error should surface the per-manifest unusable reason, got: {message}" + ); + assert!( + message.contains("rocm runtimes list"), + "error should point to `rocm runtimes list`, got: {message}" + ); + // The setup pointer must name a destination that actually exists. The + // only ROCm-setup entry point a user can see is the bare-`rocm` + // launcher row labelled "Set up this system" + // (`crates/rocm-dash-tui/src/ui/launcher.rs`, rendered verbatim from + // `ROWS`); there has never been a screen called "Set Up ROCm". + // + // The positive assertion already catches a plain revert to either wrong + // wording on its own: neither the invented label this branch emitted + // until now ("install another from Set Up ROCm") nor the superseded + // pre-`31957ed` wording ("Set up ROCm first from Set Up ROCm") contains + // `pick "Set up this system"`, so under either revert the positive + // needle stops matching and reddens. Measured, not assumed: reverting + // the production string to the invented label fails the positive + // assertion, and fails the negative one too once the positive is + // neutralized. + // + // The negative assertion is therefore not redundant, but its value is + // the case the positive one cannot see: a message carrying *both* + // labels. Re-adding "Set Up ROCm" alongside the real row still + // satisfies the positive needle, so only the negative one reddens — + // measured the same way. That is the drift worth guarding here, since + // the invented screen name is likelier to creep back as an "extra" + // pointer than as a wholesale revert. + // + // Nor can `assert_actionable` cover this branch — it is deliberately + // excluded, since the none-ready message carries only one of its four + // needles (`rocm runtimes list`); the other three are remedies for + // choosing between runtimes, which do not apply when none is usable. + assert!( + message.contains("pick \"Set up this system\""), + "error should still point at the launcher's \"Set up this system\" row, got: {message}" + ); + assert!( + !message.contains("Set Up ROCm"), + "the setup pointer must not name the nonexistent `Set Up ROCm` screen, got: {message}" + ); + Ok(()) + } + + #[test] + fn zero_runtime_setup_pointers_name_the_real_launcher_row() -> Result<()> { + // The none-ready pointer above is one of three siblings that send a + // user off to install ROCm; the other two fire when the registry is + // empty rather than merely unusable. Until now only the none-ready one + // was pinned, so these two could drift back to the invented `Set Up + // ROCm` screen name without reddening anything. Same needles, same + // reasoning as `no_ready_runtime_lists_each_key_and_reason`: the + // positive one catches a wholesale revert, the negative one catches the + // invented label creeping back *alongside* the real row. + // + // `test_paths` never creates the runtime registry directory, so + // `load_runtime_manifests` returns empty and both zero-runtime branches + // are the ones under test. + let paths = test_paths("comfyui-zero-runtime-setup-pointers"); + + // `render_status`' "ROCm" note (the zero-runtime arm of `render_status`). + let status = render_status(&paths, &RocmCliConfig::default())?; + assert!( + status.contains("pick \"Set up this system\""), + "status should point at the launcher's \"Set up this system\" row, got: {status}" + ); + assert!( + !status.contains("Set Up ROCm"), + "status must not name the nonexistent `Set Up ROCm` screen, got: {status}" + ); + + // `select_runtime`'s bail when nothing is registered at all. + let error = select_runtime(&paths, &RocmCliConfig::default(), None) + .expect_err("no registered runtime means no selection"); + let message = error.to_string(); + assert!( + message.contains("pick \"Set up this system\""), + "the zero-runtime refusal should point at the launcher's \"Set up this system\" row, got: {message}" + ); + assert!( + !message.contains("Set Up ROCm"), + "the zero-runtime refusal must not name the nonexistent `Set Up ROCm` screen, got: {message}" + ); + Ok(()) + } + + #[test] + fn configured_default_not_found_is_actionable() -> Result<()> { + // This branch enumerates the WHOLE registry, so it is planted with one + // ready and one unusable runtime: a bare key list here would advise + // `rocm runtimes activate` on the unusable one and land the user on a + // second error, so each entry must carry its status. + let paths = test_paths("comfyui-default-not-found"); + let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + + let manifests = [ready, unusable]; + let config = RocmCliConfig { + default_runtime_id: Some("no-such-runtime-id".to_owned()), + ..Default::default() + }; + let error = select_default_runtime(&config, &manifests) + .expect_err("configured default id matches nothing"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("The configured default ROCm runtime was not found"), + "error should be the configured-default-not-found branch, got: {message}" + ); + assert!( + message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), + "the usable runtime must be listed as ready, got: {message}" + ); + assert!( + message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") + && message.contains("install root is missing"), + "the unusable runtime must carry its reason, not read as pickable, got: {message}" + ); + Ok(()) + } + + #[test] + fn multiple_configured_default_matches_is_actionable() -> Result<()> { + // `ready_runtime_manifest` hardcodes the same runtime_id for every key, + // so a configured default resolving to that id matches both entries and + // exercises `select_default_runtime`'s own ambiguity arm (distinct from + // the single-ready arm the other test hits). + let paths = test_paths("comfyui-default-ambiguous"); + let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + + let manifests = [release, nightly]; + let config = RocmCliConfig { + default_runtime_id: Some("therock-release:gfx120X-all".to_owned()), + ..Default::default() + }; + let error = select_default_runtime(&config, &manifests) + .expect_err("two manifests share the configured default id"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("More than one ROCm runtime matches the configured default"), + "error should be the ambiguous-configured-default branch, got: {message}" + ); + Ok(()) + } + + #[test] + fn ambiguous_configured_default_marks_the_unusable_match() -> Result<()> { + // The ambiguity set is filtered by `runtime_id` alone, and a shared + // runtime_id says nothing about readiness: one of the two matches here + // has a missing install root. A bare key list would tell the user to + // `rocm runtimes activate` it and land them on a second error, so each + // match must carry the status `runtime_usability_status` computes. + let paths = test_paths("comfyui-default-ambiguous-unusable"); + let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + assert_eq!( + ready.runtime_id, unusable.runtime_id, + "the two manifests must share a runtime_id to reach the ambiguity arm" + ); + + let manifests = [ready, unusable]; + let config = RocmCliConfig { + default_runtime_id: Some("therock-release:gfx120X-all".to_owned()), + ..Default::default() + }; + let error = select_default_runtime(&config, &manifests) + .expect_err("two manifests share the configured default id"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("More than one ROCm runtime matches the configured default"), + "error should be the ambiguous-configured-default branch, got: {message}" + ); + assert!( + message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), + "the usable match must be listed as ready, got: {message}" + ); + assert!( + message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") + && message.contains("install root is missing"), + "the unusable match must carry its reason, not read as pickable, got: {message}" + ); + Ok(()) + } + + #[test] + fn unknown_selector_is_actionable() -> Result<()> { + // Whole-registry list again — see `configured_default_not_found_is_actionable`. + let paths = test_paths("comfyui-selector-not-found"); + let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + + let manifests = [ready, unusable]; + let error = select_runtime_by_selector(&manifests, "no-such-key") + .expect_err("selector matches no runtime"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("ROCm runtime not found: `no-such-key`"), + "error should be the unknown-selector branch, got: {message}" + ); + assert!( + message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), + "the usable runtime must be listed as ready, got: {message}" + ); + assert!( + message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") + && message.contains("install root is missing"), + "the unusable runtime must carry its reason, not read as pickable, got: {message}" + ); + Ok(()) + } + + #[test] + fn ambiguous_selector_is_actionable() -> Result<()> { + // Both manifests share runtime_id `therock-release:gfx120X-all`; neither + // runtime_key equals it, so selection falls through to the id filter and + // matches both, hitting `select_runtime_by_selector`'s ambiguity arm. + let paths = test_paths("comfyui-selector-ambiguous"); + let release = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let nightly = ready_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + + let manifests = [release, nightly]; + let error = select_runtime_by_selector(&manifests, "therock-release:gfx120X-all") + .expect_err("selector id matches two runtimes"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("More than one ROCm runtime matches `therock-release:gfx120X-all`"), + "error should be the ambiguous-selector branch, got: {message}" + ); + Ok(()) + } + + #[test] + fn ambiguous_selector_marks_the_unusable_match() -> Result<()> { + // Selector twin of `ambiguous_configured_default_marks_the_unusable_match`: + // the selector falls through to the `runtime_id` filter, which is not a + // readiness check, so the unusable match must be rendered with its + // reason rather than as a bare activatable key. + let paths = test_paths("comfyui-selector-ambiguous-unusable"); + let ready = ready_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + let unusable = unusable_runtime_manifest(&paths, "nightly-wheel-gfx94x-dcgpu-7-14-0")?; + assert_eq!( + ready.runtime_id, unusable.runtime_id, + "the two manifests must share a runtime_id to reach the ambiguity arm" + ); + + let manifests = [ready, unusable]; + let error = select_runtime_by_selector(&manifests, "therock-release:gfx120X-all") + .expect_err("selector id matches two runtimes"); + let message = error.to_string(); + + assert_actionable(&message); + assert!( + message.contains("More than one ROCm runtime matches `therock-release:gfx120X-all`"), + "error should be the ambiguous-selector branch, got: {message}" + ); + assert!( + message.contains("`release-wheel-gfx94x-dcgpu-7-13-0` (ready)"), + "the usable match must be listed as ready, got: {message}" + ); + assert!( + message.contains("`nightly-wheel-gfx94x-dcgpu-7-14-0` (unusable") + && message.contains("install root is missing"), + "the unusable match must carry its reason, not read as pickable, got: {message}" + ); + Ok(()) + } + + #[test] + fn selected_unusable_runtime_reports_not_ready_reason() -> Result<()> { + // Naming a runtime explicitly by selector bypasses the readiness filter + // in `select_default_runtime` / `select_single_ready_runtime`, so + // `select_runtime` re-checks it and bails with the reworked + // "The selected ROCm runtime is not ready: " message. This is + // also why the sibling "has no Python executable" arm is unreachable: + // `validate_wheel_runtime_manifest` already rejects a ready wheel runtime + // whose python is missing, so any such runtime lands on this not-ready + // arm — carrying the reason — rather than on the no-python arm. + let paths = test_paths("comfyui-selected-unusable"); + let unusable = unusable_runtime_manifest(&paths, "release-wheel-gfx94x-dcgpu-7-13-0")?; + write_runtime_manifest(&paths, &unusable)?; + + let error = select_runtime( + &paths, + &RocmCliConfig::default(), + Some("release-wheel-gfx94x-dcgpu-7-13-0"), + ) + .expect_err("an explicitly selected unusable runtime is not ready"); + let message = error.to_string(); + + assert!( + message.contains("The selected ROCm runtime is not ready"), + "error should use the reworked `ROCm runtime` noun, got: {message}" + ); + assert!( + message.contains("install root is missing"), + "error should surface the per-manifest unusable reason, got: {message}" + ); + Ok(()) + } + + #[test] + fn runtime_environment_preloads_managed_rocm_paths() -> Result<()> { + let paths = test_paths("comfyui-runtime-env"); + let env_root = paths.data_dir.join("envs").join("therock"); + let python = runtime_python_executable_in_env(&env_root); + let python_bin = python + .parent() + .context("runtime Python path has no parent")? + .to_path_buf(); + let sdk_root = paths.data_dir.join("runtimes").join("sdk"); + let sdk_bin = sdk_root.join("bin"); + let sdk_lib = sdk_root.join("lib"); + let runtime_root = paths.data_dir.join("runtimes").join("default"); + let runtime_bin = runtime_root.join("bin"); + let runtime_lib = runtime_root.join("lib"); + let runtime_sysdeps = runtime_root.join("lib").join("rocm_sysdeps").join("lib"); + for dir in [ + &python_bin, + &sdk_bin, + &sdk_lib, + &runtime_bin, + &runtime_lib, + &runtime_sysdeps, + ] { + fs::create_dir_all(dir)?; + } + + let manifest = therock::InstalledRuntimeManifest { + runtime_key: "therock-release-gfx120x-all".to_owned(), + runtime_id: "therock-release:gfx120X-all".to_owned(), + channel: "release".to_owned(), + format: "wheel".to_owned(), + family: "gfx120X-all".to_owned(), + family_source: "test".to_owned(), + version: "7.13.0a20260511".to_owned(), + install_root: runtime_root.clone(), + selected_artifact_url: "https://example.invalid/simple".to_owned(), + source_layout_generation: None, + index_url: None, + tarball_file_name: None, + python_launcher: None, + python_executable: Some(python.display().to_string()), + pip_cache_dir: None, + rocm_sdk: Some(therock::RocmSdkPythonProbe { + import_ok: true, + root_path: Some(sdk_root.clone()), + bin_path: Some(sdk_bin.clone()), + runtime_roots: vec![runtime_root], + bin_paths: vec![sdk_bin.clone()], + library_paths: vec![sdk_lib.clone()], + ..Default::default() + }), + sdk_torch: None, + wheel_composition: None, + read_only: false, + imported_from: None, + devel: true, + installed_at_unix_ms: 100, + }; + + let env = runtime_environment_from_runtime(&manifest, &python); + let mut command = Command::new(&python); + apply_runtime_environment(&mut command, &env)?; + + assert_eq!( + command_env_value(&command, "VIRTUAL_ENV").as_deref(), + Some(env_root.as_os_str()) + ); + assert_eq!( + command_env_value(&command, "ROCM_PATH").as_deref(), + Some(sdk_root.as_os_str()) + ); + let path = command_env_value(&command, "PATH").context("PATH should be set")?; + let path_entries = split_runtime_paths(&path); + assert!(path_entries.contains(&python_bin)); + assert!(path_entries.contains(&sdk_bin)); + assert!(path_entries.contains(&runtime_bin)); + + if runtime_is_windows() { + assert!(path_entries.contains(&sdk_lib)); + assert!(path_entries.contains(&runtime_lib)); + } else if runtime_is_linux() { + let ld_library_path = + command_env_value(&command, "LD_LIBRARY_PATH").context("LD_LIBRARY_PATH")?; + let library_entries = split_runtime_paths(&ld_library_path); + assert!(library_entries.contains(&sdk_lib)); + assert!(library_entries.contains(&runtime_lib)); + assert!(library_entries.contains(&runtime_sysdeps)); + } + Ok(()) + } + + fn command_env_value(command: &Command, key: &str) -> Option { + command + .get_envs() + .find(|(name, _)| name.to_string_lossy() == key) + .and_then(|(_, value)| value.map(OsString::from)) + } +} diff --git a/docs/architecture.md b/docs/architecture.md index 97264a51ae..6b358d7f01 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -38,7 +38,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every - [`cli_audit.rs`](../apps/rocm/src/cli_audit.rs) — mechanical relocation, no owned types: the CLI audit and lifecycle log (`record_cli_audit_event`, plus the helpers behind it that write `logs/cli-lifecycle.log` and the per-action files under `logs/cli/`). Its production code depends only on `rocm-core`. `record_cli_audit_event`, `cli_lifecycle_log_path` and `sanitize_log_value` are `pub(crate)` and reached from other modules as `crate::` through a re-import in [`main.rs`](../apps/rocm/src/main.rs); every other item is private. The two regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::`. - [`cli_progress.rs`](../apps/rocm/src/cli_progress.rs) — shared CLI-output component (`Spinner`, `AnimatedSpinner`). - [`cli_report.rs`](../apps/rocm/src/cli_report.rs) — shared CLI-output component (`ActionReport`). -- [`comfyui.rs`](../apps/rocm/src/comfyui.rs) — full domain extraction; serves the `comfyui` subcommand, whose command enum (`ComfyuiCommand`) and dispatch fn stay in [`main.rs`](../apps/rocm/src/main.rs) (unlike [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs) above, which owns its own). +- [`comfyui.rs`](../apps/rocm/src/comfyui.rs) — full domain extraction; serves the `comfyui` subcommand, whose command enum (`ComfyuiCommand`) and dispatch fn stay in [`main.rs`](../apps/rocm/src/main.rs) (unlike [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs) above, which owns its own). It owns the ComfyUI types and constants, the install and state paths, the manifest and state files, `status` and `logs` rendering, the install and process-lifecycle code, and the helpers that run a Python child and capture its output. Choosing the ROCm runtime and building its environment is in the child module [`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs), listed below with the other `apps/rocm` modules. - [`config_cmd.rs`](../apps/rocm/src/config_cmd.rs) — mechanical relocation, dispatch-adjacent, no owned types: `rocm config`'s command handler (`config()`), the provider-key prompt it uses (`read_provider_key_from_user`) and the text `rocm config show` prints (`render_config_text`, `telemetry_policy_summary`). `ConfigCommand` stays in [`main.rs`](../apps/rocm/src/main.rs). `config()` and `render_config_text` are `pub(crate)`: `dispatch()` calls `config()` and [`chat.rs`](../apps/rocm/src/chat.rs)'s read-only handling of `rocm config show` calls `render_config_text`, both through re-imports in `main.rs`; the other two functions are private. The two `render_config_text` regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::`. - [`dash.rs`](../apps/rocm/src/dash.rs) — full domain extraction. - [`dash_seam.rs`](../apps/rocm/src/dash_seam.rs) — full domain extraction. @@ -101,6 +101,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every Further `apps/rocm` modules, not part of the `main.rs` split: - [`advised_commands.rs`](../apps/rocm/src/advised_commands.rs) — test-only (`#[cfg(test)]`): checks that every `rocm …`/`rocmd …` command the repository advises — in `--help` text, production string literals, the markdown docs and skills, and the VHS tapes — exists and parses, by running it through the parser of the binary it names (`rocm`'s clap `Cli`, or `rocmd`'s `parse_args`). +- [`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs) — child module of [`comfyui.rs`](../apps/rocm/src/comfyui.rs): choosing which installed ROCm runtime ComfyUI runs against (`select_runtime`, with the messages that tell the user how to pick one when none or several are ready) and building the `VIRTUAL_ENV`, `ROCM_PATH`, `PATH` and library-path environment a ComfyUI child process runs with. The types it returns (`SelectedRuntime`, `ComfyUiRuntimeEnvironment`) stay in `comfyui.rs`, which the module imports from; it imports nothing from the other `comfyui` children. - [`comfyui/source_swap.rs`](../apps/rocm/src/comfyui/source_swap.rs) — child module of [`comfyui.rs`](../apps/rocm/src/comfyui.rs): replacing ComfyUI's code in an existing `source/` folder on reinstall without touching anything else there. - [`remote/mod.rs`](../apps/rocm/src/remote/mod.rs) — `rocm remote`: driving a remote GPU host from this machine, with SSH as the control channel and the remote's own service published onto the tailnet for inference traffic. Like [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs), it owns its command enum (`RemoteCommand`) and dispatch fn (`run`). Its `pub(crate)` submodules: - [`remote/bootstrap.rs`](../apps/rocm/src/remote/bootstrap.rs) — deciding whether a remote machine can host a model before asking it to. From 3dd878b1a6fd5aa092aaf183c45f1d6c40bafe88 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Wed, 4 Nov 2026 13:32:24 +0000 Subject: [PATCH 2/5] refactor(comfyui): move process lifecycle into comfyui/process.rs Moves start, stop, the background spawn (a detached child on Linux, a hidden PowerShell launcher on Windows), the running-state evaluation that status uses, the process probe and termination helpers, and the reinstall guard (running_from, refuse_while_running, running_advice) into a child module, with the test that exercises evaluate_running_state. comfyui.rs re-exports start and stop, so callers still write comfyui::start and comfyui::stop. Pure move: no behaviour change. None of the moved items is cfg-gated (the Windows paths are selected at run time), so the Linux build compiles them all. The saved state and its types stay in comfyui.rs. process imports runtime_env and the parent's types and helpers, and nothing else. docs/architecture.md and the path in rocm-core's quoting module doc describe the new location. Signed-off-by: Jussi Elo --- apps/rocm/src/comfyui.rs | 498 +---------------------------- apps/rocm/src/comfyui/process.rs | 527 +++++++++++++++++++++++++++++++ crates/rocm-core/src/quoting.rs | 4 +- docs/architecture.md | 3 +- 4 files changed, 543 insertions(+), 489 deletions(-) create mode 100644 apps/rocm/src/comfyui/process.rs diff --git a/apps/rocm/src/comfyui.rs b/apps/rocm/src/comfyui.rs index ca0f046da4..10390f0a9e 100644 --- a/apps/rocm/src/comfyui.rs +++ b/apps/rocm/src/comfyui.rs @@ -7,29 +7,31 @@ use crate::freeform_planner::format_structured_tool_call; use crate::therock; use anyhow::{Context, Result, bail}; use flate2::read::GzDecoder; -use rocm_core::browser::{Opener, SystemOpener}; +use process::{ + comfyui_run_state_cli_label, endpoint_is_reachable, evaluate_running_state, + refuse_while_running, running_advice, running_from, +}; use rocm_core::{ AppPaths, RocmCliConfig, download_file_to_path_with_progress, ensure_uv_binary, - format_http_base_url, powershell_quote, runtime_is_linux, runtime_is_windows, - runtime_path_for_windows_child, unix_time_millis, uv_command_env, uv_pip_install_base, -}; -use runtime_env::{ - apply_runtime_environment, runtime_environment_assignments, runtime_environment_for_manifest, - runtime_environment_from_runtime, same_path_text, select_runtime, + format_http_base_url, runtime_is_windows, runtime_path_for_windows_child, unix_time_millis, + uv_command_env, uv_pip_install_base, }; +use runtime_env::{apply_runtime_environment, runtime_environment_from_runtime, select_runtime}; use serde::{Deserialize, Serialize}; use std::fmt::Write as FmtWrite; use std::fs; use std::io::{self, IsTerminal, Read, Write as IoWrite}; -use std::net::{TcpStream, ToSocketAddrs}; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus, Stdio}; use std::thread; use std::time::{Duration, SystemTime}; +mod process; mod runtime_env; mod source_swap; +pub(crate) use process::{start, stop}; + const APP_ID: &str = "comfyui"; const APP_NAME: &str = "ComfyUI"; const COMFYUI_SOURCE_ARCHIVE_URL: &str = @@ -661,101 +663,6 @@ pub(crate) fn install( Ok(output) } -pub(crate) fn start(paths: &AppPaths, options: ComfyUiStartOptions) -> Result { - let manifest = load_manifest(paths)?.context("ComfyUI is not installed yet")?; - ensure_source_is_startable(&manifest)?; - let runtime_env = runtime_environment_for_manifest(paths, &manifest)?; - let models_folder = models_folder_for_manifest(&manifest); - fs::create_dir_all(&models_folder) - .with_context(|| format!("failed to create {}", models_folder.display()))?; - let probe = probe_comfyui( - &manifest.python_executable, - &manifest.source_path, - Some(&runtime_env), - )?; - if !probe.torch_cuda_available { - bail!("ComfyUI cannot start because the AMD GPU check failed. No CPU mode was used."); - } - let url = format_http_base_url(&options.host, options.port); - let log_path = start_log_path_for_manifest(&manifest); - fs::create_dir_all( - log_path - .parent() - .context("ComfyUI start log path has no parent directory")?, - )?; - fs::File::create(&log_path) - .with_context(|| format!("failed to create {}", log_path.display()))?; - let pid = spawn_comfyui_background(paths, &manifest, &options, &runtime_env, &log_path)?; - let state = ComfyUiState { - app_id: APP_ID.to_owned(), - url: url.clone(), - host: options.host.clone(), - port: options.port, - pid, - source_path: manifest.source_path.clone(), - python_executable: manifest.python_executable, - log_path: log_path.clone(), - started_at_unix_ms: unix_time_millis(), - }; - save_state(paths, &state)?; - let run_report = wait_for_running_state(&state, Duration::from_mins(3)); - if run_report.state == ComfyUiRunState::Stopped { - bail!( - "ComfyUI stopped before the local URL was ready. Check the log at {}", - log_path.display() - ); - } - let browser_status = if options.no_open_browser { - "not opened (--no-open-browser)".to_owned() - } else { - match SystemOpener.open(&url) { - Ok(()) => "opened".to_owned(), - Err(error) => format!("not opened ({error})"), - } - }; - - let mut output = String::new(); - writeln!(output, "{APP_NAME}")?; - writeln!( - output, - " status: {}", - comfyui_run_state_cli_label(run_report.state) - )?; - writeln!( - output, - " AMD GPU check: ready ({} device{})", - probe.device_count, - if probe.device_count == 1 { "" } else { "s" } - )?; - if !probe.devices.is_empty() { - writeln!(output, " GPU: {}", probe.devices.join(", "))?; - } - writeln!(output, " URL: {url}")?; - writeln!(output, " models path: {}", models_folder.display())?; - writeln!(output, " browser: {browser_status}")?; - if run_report.state == ComfyUiRunState::Starting { - writeln!(output, " note: still loading")?; - } - Ok(output) -} - -/// The checks `start` makes on the installed folder before launching it. -fn ensure_source_is_startable(manifest: &ComfyUiManifest) -> Result<()> { - if source_swap::swap_interrupted(&manifest.source_path) { - bail!( - "{}; it would start a half-replaced ComfyUI", - interrupted_reinstall_advice(&manifest.runtime_key) - ); - } - if !manifest.source_path.join("main.py").is_file() { - bail!( - "ComfyUI main.py is missing from {}; reinstall ComfyUI", - manifest.source_path.display() - ); - } - Ok(()) -} - /// What `start` and `status` say about a reinstall that stopped part-way. The /// command names the runtime the install belongs to, so it finishes that /// install rather than one in whichever runtime is the default now; an install @@ -767,56 +674,6 @@ fn interrupted_reinstall_advice(runtime_key: &str) -> String { ) } -/// The saved state of the ComfyUI this CLI started, when it is still running -/// from `source_path`: it keeps the old code loaded and keeps writing into the -/// folder a reinstall changes, so the reinstall waits for it. `rocm comfyui -/// stop` stops it and removes the saved state this reads, which is what clears -/// it. A saved state that cannot be read counts as not running, and -/// `warnings` says so: `stop` cannot read it either, so a refusal would have -/// nothing to clear it. What a running ComfyUI risks is the code it runs; the -/// preserved folders are left alone whatever is running. -fn running_from( - paths: &AppPaths, - source_path: &Path, - warnings: &mut Vec, -) -> Option { - let state = match load_state(paths) { - Ok(state) => state?, - Err(error) => { - warnings.push(format!( - "{error:#}; whether a ComfyUI started by rocm-cli is running from {} is unknown, \ - so the reinstall is not refused for it", - source_path.display() - )); - return None; - } - }; - (same_path_text(&state.source_path, source_path) - && evaluate_running_state(&state).state != ComfyUiRunState::Stopped) - .then_some(state) -} - -/// The refusal of a reinstall while the ComfyUI [`running_from`] found runs. -fn refuse_while_running(source_path: &Path, running: Option) -> Result<()> { - match running { - Some(state) => bail!( - "{}, then reinstall. Nothing was changed.", - running_advice(source_path, &state) - ), - None => Ok(()), - } -} - -/// What a reinstall says about a ComfyUI found by [`running_from`]. -fn running_advice(source_path: &Path, state: &ComfyUiState) -> String { - format!( - "ComfyUI is running from {} (process {}, {}). Stop it with `rocm comfyui stop`", - source_path.display(), - state.pid, - state.url - ) -} - /// Names the entries a finished swap replaced or set aside, if any. fn swap_changes_summary(swap: &source_swap::SwapReport) -> Option { let mut parts = Vec::new(); @@ -834,177 +691,6 @@ fn swap_changes_summary(swap: &source_swap::SwapReport) -> Option { (!parts.is_empty()).then(|| parts.join("; ")) } -pub(crate) fn stop(paths: &AppPaths) -> Result { - let Some(state) = load_state(paths)? else { - return Ok(format!( - "{APP_NAME}\n status: stopped\n note: ComfyUI was not running\n" - )); - }; - let report = evaluate_running_state(&state); - if report.process_running { - terminate_process_tree(state.pid)?; - wait_until_stopped(state.pid); - } - let _ = fs::remove_file(state_path(paths)); - - let mut output = String::new(); - writeln!(output, "{APP_NAME}")?; - writeln!(output, " status: stopped")?; - writeln!(output, " url: {}", state.url)?; - Ok(output) -} - -fn spawn_comfyui_background( - paths: &AppPaths, - manifest: &ComfyUiManifest, - options: &ComfyUiStartOptions, - runtime_env: &ComfyUiRuntimeEnvironment, - log_path: &Path, -) -> Result { - if runtime_is_windows() { - return spawn_comfyui_background_windows(paths, manifest, options, runtime_env, log_path); - } - spawn_comfyui_background_unix(manifest, options, runtime_env, log_path) -} - -fn spawn_comfyui_background_unix( - manifest: &ComfyUiManifest, - options: &ComfyUiStartOptions, - runtime_env: &ComfyUiRuntimeEnvironment, - log_path: &Path, -) -> Result { - let log = fs::OpenOptions::new() - .append(true) - .open(log_path) - .with_context(|| format!("failed to open {}", log_path.display()))?; - let stdout = log - .try_clone() - .context("failed to clone ComfyUI log file")?; - let stderr = log - .try_clone() - .context("failed to clone ComfyUI log file")?; - let mut command = Command::new(&manifest.python_executable); - command - .current_dir(&manifest.source_path) - .arg("main.py") - .arg("--listen") - .arg(&options.host) - .arg("--port") - .arg(options.port.to_string()) - .stdin(Stdio::null()) - .stdout(Stdio::from(stdout)) - .stderr(Stdio::from(stderr)); - apply_runtime_environment(&mut command, runtime_env)?; - let child = command.spawn().with_context(|| { - format!( - "failed to start ComfyUI with {}", - manifest.python_executable.display() - ) - })?; - Ok(child.id()) -} - -fn spawn_comfyui_background_windows( - paths: &AppPaths, - manifest: &ComfyUiManifest, - options: &ComfyUiStartOptions, - runtime_env: &ComfyUiRuntimeEnvironment, - log_path: &Path, -) -> Result { - let state_dir = app_root(paths).join("state"); - fs::create_dir_all(&state_dir) - .with_context(|| format!("failed to create {}", state_dir.display()))?; - let runner_path = state_dir.join("run-comfyui.ps1"); - let launcher_path = state_dir.join("launch-comfyui.ps1"); - let pid_path = state_dir.join("comfyui-launch.pid"); - - let mut runner = String::new(); - writeln!(runner, "$ErrorActionPreference = 'Continue'")?; - writeln!( - runner, - "Set-Location -LiteralPath {}", - powershell_quote(&child_path_string(&manifest.source_path)) - )?; - for (key, value) in runtime_environment_assignments(runtime_env)? { - if key == "PATH" { - continue; - } - writeln!( - runner, - "$env:{} = {}", - key, - powershell_quote(&value.to_string_lossy()) - )?; - } - let mut path_prefix_entries = runtime_env.path_entries.clone(); - path_prefix_entries.extend(runtime_env.library_entries.iter().cloned()); - let path_prefix = path_prefix_entries - .iter() - .map(|path| child_path_string(path)) - .collect::>() - .join(";"); - if !path_prefix.is_empty() { - writeln!( - runner, - "$env:PATH = {} + ';' + $env:PATH", - powershell_quote(&path_prefix) - )?; - } - let mut command_line = powershell_quote(&child_path_string(&manifest.python_executable)); - for arg in [ - "main.py", - "--listen", - options.host.as_str(), - "--port", - &options.port.to_string(), - ] { - write!(command_line, " {}", powershell_quote(arg))?; - } - writeln!( - runner, - "& {} *>> {}", - command_line, - powershell_quote(&child_path_string(log_path)) - )?; - fs::write(&runner_path, runner) - .with_context(|| format!("failed to write {}", runner_path.display()))?; - - let launcher = format!( - "$p = Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-File',{}) -WindowStyle Hidden -PassThru\nSet-Content -LiteralPath {} -Value $p.Id -Encoding ASCII\n", - powershell_quote(&child_path_string(&runner_path)), - powershell_quote(&child_path_string(&pid_path)) - ); - fs::write(&launcher_path, launcher) - .with_context(|| format!("failed to write {}", launcher_path.display()))?; - - let mut command = Command::new("powershell.exe"); - command - .arg("-NoProfile") - .arg("-ExecutionPolicy") - .arg("Bypass") - .arg("-File") - .arg(&launcher_path); - let output = capture_configured_command( - command, - &format!("run ComfyUI launcher {}", launcher_path.display()), - )?; - let pid_text = fs::read_to_string(&pid_path).unwrap_or_default(); - let _ = fs::remove_file(&pid_path); - if !output.status.success() && pid_text.trim().is_empty() { - bail!( - "failed to launch ComfyUI in the background: {}", - String::from_utf8_lossy(&output.stderr).trim() - ); - } - let pid = pid_text - .split_whitespace() - .next() - .context("PowerShell launcher did not return a process id")? - .parse::() - .context("PowerShell launcher returned an invalid process id")?; - Ok(pid) -} - fn app_root(paths: &AppPaths) -> PathBuf { paths.data_dir.join("apps").join(APP_ID) } @@ -1169,144 +855,6 @@ fn strip_ansi_sequences(text: &str) -> String { cleaned } -fn evaluate_running_state(state: &ComfyUiState) -> ComfyUiRunReport { - let process_running = process_is_running(state.pid); - let endpoint_reachable = endpoint_is_reachable(&state.host, state.port); - let state = if endpoint_reachable { - ComfyUiRunState::Running - } else if process_running { - ComfyUiRunState::Starting - } else { - ComfyUiRunState::Stopped - }; - ComfyUiRunReport { - state, - process_running, - endpoint_reachable, - } -} - -fn wait_for_running_state(state: &ComfyUiState, timeout: Duration) -> ComfyUiRunReport { - let deadline = std::time::Instant::now() + timeout; - let mut last_report = evaluate_running_state(state); - while std::time::Instant::now() < deadline { - if last_report.state == ComfyUiRunState::Running { - return last_report; - } - thread::sleep(Duration::from_millis(500)); - last_report = evaluate_running_state(state); - } - last_report -} - -const fn comfyui_run_state_cli_label(state: ComfyUiRunState) -> &'static str { - match state { - ComfyUiRunState::Running => "running", - ComfyUiRunState::Starting => "starting", - ComfyUiRunState::Stopped => "stopped", - } -} - -fn endpoint_is_reachable(host: &str, port: u16) -> bool { - let Ok(addresses) = (host, port).to_socket_addrs() else { - return false; - }; - addresses - .into_iter() - .any(|address| TcpStream::connect_timeout(&address, Duration::from_millis(200)).is_ok()) -} - -fn process_is_running(pid: u32) -> bool { - if pid == 0 { - return false; - } - if runtime_is_windows() { - return windows_process_is_running(pid); - } - if runtime_is_linux() { - return unix_process_is_running(pid); - } - true -} - -fn windows_process_is_running(pid: u32) -> bool { - let filter = format!("PID eq {pid}"); - let mut command = Command::new("tasklist"); - command.args(["/FI", &filter, "/FO", "CSV", "/NH"]); - let Ok(output) = capture_configured_command(command, "check ComfyUI process") else { - return false; - }; - let stdout = String::from_utf8_lossy(&output.stdout); - if stdout.contains(&format!("\"{pid}\"")) { - return true; - } - if !output.status.success() { - return false; - } - false -} - -fn unix_process_is_running(pid: u32) -> bool { - Command::new("kill") - .arg("-0") - .arg(pid.to_string()) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .is_ok_and(|status| status.success()) -} - -fn wait_until_stopped(pid: u32) { - for _ in 0..20 { - if !process_is_running(pid) { - break; - } - thread::sleep(Duration::from_millis(100)); - } -} - -fn terminate_process_tree(pid: u32) -> Result<()> { - if !process_is_running(pid) { - return Ok(()); - } - if runtime_is_windows() { - return terminate_windows_process_tree(pid); - } - if runtime_is_linux() { - return terminate_unix_process(pid); - } - Ok(()) -} - -fn terminate_windows_process_tree(pid: u32) -> Result<()> { - let status = Command::new("taskkill") - .args(["/PID", &pid.to_string(), "/T", "/F"]) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .context("failed to run taskkill for ComfyUI")?; - if !status.success() && process_is_running(pid) { - bail!("failed to stop ComfyUI process {pid}"); - } - Ok(()) -} - -fn terminate_unix_process(pid: u32) -> Result<()> { - let status = Command::new("kill") - .arg(pid.to_string()) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .context("failed to run kill for ComfyUI")?; - if !status.success() && process_is_running(pid) { - bail!("failed to stop ComfyUI process {pid}"); - } - Ok(()) -} - fn load_manifest(paths: &AppPaths) -> Result> { let path = manifest_path(paths); if !path.is_file() { @@ -1938,6 +1486,8 @@ fn child_path_string(path: &Path) -> String { #[cfg(test)] mod tests { + #[cfg(unix)] + use super::process::ensure_source_is_startable; use super::*; #[cfg(unix)] use crate::chat; @@ -2312,30 +1862,6 @@ mod tests { Ok(()) } - #[test] - fn running_state_requires_saved_process_and_reachable_port() -> Result<()> { - let listener = TcpListener::bind("127.0.0.1:0")?; - let port = listener.local_addr()?.port(); - let state = ComfyUiState { - app_id: APP_ID.to_owned(), - url: format!("http://127.0.0.1:{port}"), - host: "127.0.0.1".to_owned(), - port, - pid: std::process::id(), - source_path: PathBuf::from("source"), - python_executable: PathBuf::from("python"), - log_path: PathBuf::from("start.log"), - started_at_unix_ms: 200, - }; - - let report = evaluate_running_state(&state); - - assert_eq!(report.state, ComfyUiRunState::Running); - assert!(report.process_running); - assert!(report.endpoint_reachable); - Ok(()) - } - #[test] fn unmanaged_running_url_reports_reachable_loopback_endpoint() -> Result<()> { let listener = TcpListener::bind("127.0.0.1:0")?; diff --git a/apps/rocm/src/comfyui/process.rs b/apps/rocm/src/comfyui/process.rs new file mode 100644 index 0000000000..ec324098d5 --- /dev/null +++ b/apps/rocm/src/comfyui/process.rs @@ -0,0 +1,527 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Starting, stopping and watching the ComfyUI process this CLI launches. +//! +//! [`start`] checks the installed folder, runs the GPU check, launches ComfyUI +//! in the background (a detached child on Linux, a hidden PowerShell launcher on +//! Windows) and waits for its port to answer; [`stop`] ends the process tree the +//! saved state names. [`evaluate_running_state`] is how `status` and the +//! reinstall guard ([`running_from`], [`refuse_while_running`], +//! [`running_advice`]) tell a running, a still-loading and a stopped ComfyUI +//! apart. The saved state itself, and the types in it, stay in the parent +//! module. + +use super::runtime_env::{ + apply_runtime_environment, runtime_environment_assignments, runtime_environment_for_manifest, + same_path_text, +}; +use super::{ + APP_ID, APP_NAME, ComfyUiManifest, ComfyUiRunReport, ComfyUiRunState, + ComfyUiRuntimeEnvironment, ComfyUiStartOptions, ComfyUiState, app_root, + capture_configured_command, child_path_string, interrupted_reinstall_advice, load_manifest, + load_state, models_folder_for_manifest, probe_comfyui, save_state, source_swap, + start_log_path_for_manifest, state_path, +}; +use anyhow::{Context, Result, bail}; +use rocm_core::browser::{Opener, SystemOpener}; +use rocm_core::{ + AppPaths, format_http_base_url, powershell_quote, runtime_is_linux, runtime_is_windows, + unix_time_millis, +}; +use std::fmt::Write as FmtWrite; +use std::fs; +use std::net::{TcpStream, ToSocketAddrs}; +use std::path::Path; +use std::process::{Command, Stdio}; +use std::thread; +use std::time::Duration; + +pub(crate) fn start(paths: &AppPaths, options: ComfyUiStartOptions) -> Result { + let manifest = load_manifest(paths)?.context("ComfyUI is not installed yet")?; + ensure_source_is_startable(&manifest)?; + let runtime_env = runtime_environment_for_manifest(paths, &manifest)?; + let models_folder = models_folder_for_manifest(&manifest); + fs::create_dir_all(&models_folder) + .with_context(|| format!("failed to create {}", models_folder.display()))?; + let probe = probe_comfyui( + &manifest.python_executable, + &manifest.source_path, + Some(&runtime_env), + )?; + if !probe.torch_cuda_available { + bail!("ComfyUI cannot start because the AMD GPU check failed. No CPU mode was used."); + } + let url = format_http_base_url(&options.host, options.port); + let log_path = start_log_path_for_manifest(&manifest); + fs::create_dir_all( + log_path + .parent() + .context("ComfyUI start log path has no parent directory")?, + )?; + fs::File::create(&log_path) + .with_context(|| format!("failed to create {}", log_path.display()))?; + let pid = spawn_comfyui_background(paths, &manifest, &options, &runtime_env, &log_path)?; + let state = ComfyUiState { + app_id: APP_ID.to_owned(), + url: url.clone(), + host: options.host.clone(), + port: options.port, + pid, + source_path: manifest.source_path.clone(), + python_executable: manifest.python_executable, + log_path: log_path.clone(), + started_at_unix_ms: unix_time_millis(), + }; + save_state(paths, &state)?; + let run_report = wait_for_running_state(&state, Duration::from_mins(3)); + if run_report.state == ComfyUiRunState::Stopped { + bail!( + "ComfyUI stopped before the local URL was ready. Check the log at {}", + log_path.display() + ); + } + let browser_status = if options.no_open_browser { + "not opened (--no-open-browser)".to_owned() + } else { + match SystemOpener.open(&url) { + Ok(()) => "opened".to_owned(), + Err(error) => format!("not opened ({error})"), + } + }; + + let mut output = String::new(); + writeln!(output, "{APP_NAME}")?; + writeln!( + output, + " status: {}", + comfyui_run_state_cli_label(run_report.state) + )?; + writeln!( + output, + " AMD GPU check: ready ({} device{})", + probe.device_count, + if probe.device_count == 1 { "" } else { "s" } + )?; + if !probe.devices.is_empty() { + writeln!(output, " GPU: {}", probe.devices.join(", "))?; + } + writeln!(output, " URL: {url}")?; + writeln!(output, " models path: {}", models_folder.display())?; + writeln!(output, " browser: {browser_status}")?; + if run_report.state == ComfyUiRunState::Starting { + writeln!(output, " note: still loading")?; + } + Ok(output) +} + +/// The checks `start` makes on the installed folder before launching it. +pub(super) fn ensure_source_is_startable(manifest: &ComfyUiManifest) -> Result<()> { + if source_swap::swap_interrupted(&manifest.source_path) { + bail!( + "{}; it would start a half-replaced ComfyUI", + interrupted_reinstall_advice(&manifest.runtime_key) + ); + } + if !manifest.source_path.join("main.py").is_file() { + bail!( + "ComfyUI main.py is missing from {}; reinstall ComfyUI", + manifest.source_path.display() + ); + } + Ok(()) +} + +/// The saved state of the ComfyUI this CLI started, when it is still running +/// from `source_path`: it keeps the old code loaded and keeps writing into the +/// folder a reinstall changes, so the reinstall waits for it. `rocm comfyui +/// stop` stops it and removes the saved state this reads, which is what clears +/// it. A saved state that cannot be read counts as not running, and +/// `warnings` says so: `stop` cannot read it either, so a refusal would have +/// nothing to clear it. What a running ComfyUI risks is the code it runs; the +/// preserved folders are left alone whatever is running. +pub(super) fn running_from( + paths: &AppPaths, + source_path: &Path, + warnings: &mut Vec, +) -> Option { + let state = match load_state(paths) { + Ok(state) => state?, + Err(error) => { + warnings.push(format!( + "{error:#}; whether a ComfyUI started by rocm-cli is running from {} is unknown, \ + so the reinstall is not refused for it", + source_path.display() + )); + return None; + } + }; + (same_path_text(&state.source_path, source_path) + && evaluate_running_state(&state).state != ComfyUiRunState::Stopped) + .then_some(state) +} + +/// The refusal of a reinstall while the ComfyUI [`running_from`] found runs. +pub(super) fn refuse_while_running( + source_path: &Path, + running: Option, +) -> Result<()> { + match running { + Some(state) => bail!( + "{}, then reinstall. Nothing was changed.", + running_advice(source_path, &state) + ), + None => Ok(()), + } +} + +/// What a reinstall says about a ComfyUI found by [`running_from`]. +pub(super) fn running_advice(source_path: &Path, state: &ComfyUiState) -> String { + format!( + "ComfyUI is running from {} (process {}, {}). Stop it with `rocm comfyui stop`", + source_path.display(), + state.pid, + state.url + ) +} + +pub(crate) fn stop(paths: &AppPaths) -> Result { + let Some(state) = load_state(paths)? else { + return Ok(format!( + "{APP_NAME}\n status: stopped\n note: ComfyUI was not running\n" + )); + }; + let report = evaluate_running_state(&state); + if report.process_running { + terminate_process_tree(state.pid)?; + wait_until_stopped(state.pid); + } + let _ = fs::remove_file(state_path(paths)); + + let mut output = String::new(); + writeln!(output, "{APP_NAME}")?; + writeln!(output, " status: stopped")?; + writeln!(output, " url: {}", state.url)?; + Ok(output) +} + +fn spawn_comfyui_background( + paths: &AppPaths, + manifest: &ComfyUiManifest, + options: &ComfyUiStartOptions, + runtime_env: &ComfyUiRuntimeEnvironment, + log_path: &Path, +) -> Result { + if runtime_is_windows() { + return spawn_comfyui_background_windows(paths, manifest, options, runtime_env, log_path); + } + spawn_comfyui_background_unix(manifest, options, runtime_env, log_path) +} + +fn spawn_comfyui_background_unix( + manifest: &ComfyUiManifest, + options: &ComfyUiStartOptions, + runtime_env: &ComfyUiRuntimeEnvironment, + log_path: &Path, +) -> Result { + let log = fs::OpenOptions::new() + .append(true) + .open(log_path) + .with_context(|| format!("failed to open {}", log_path.display()))?; + let stdout = log + .try_clone() + .context("failed to clone ComfyUI log file")?; + let stderr = log + .try_clone() + .context("failed to clone ComfyUI log file")?; + let mut command = Command::new(&manifest.python_executable); + command + .current_dir(&manifest.source_path) + .arg("main.py") + .arg("--listen") + .arg(&options.host) + .arg("--port") + .arg(options.port.to_string()) + .stdin(Stdio::null()) + .stdout(Stdio::from(stdout)) + .stderr(Stdio::from(stderr)); + apply_runtime_environment(&mut command, runtime_env)?; + let child = command.spawn().with_context(|| { + format!( + "failed to start ComfyUI with {}", + manifest.python_executable.display() + ) + })?; + Ok(child.id()) +} + +fn spawn_comfyui_background_windows( + paths: &AppPaths, + manifest: &ComfyUiManifest, + options: &ComfyUiStartOptions, + runtime_env: &ComfyUiRuntimeEnvironment, + log_path: &Path, +) -> Result { + let state_dir = app_root(paths).join("state"); + fs::create_dir_all(&state_dir) + .with_context(|| format!("failed to create {}", state_dir.display()))?; + let runner_path = state_dir.join("run-comfyui.ps1"); + let launcher_path = state_dir.join("launch-comfyui.ps1"); + let pid_path = state_dir.join("comfyui-launch.pid"); + + let mut runner = String::new(); + writeln!(runner, "$ErrorActionPreference = 'Continue'")?; + writeln!( + runner, + "Set-Location -LiteralPath {}", + powershell_quote(&child_path_string(&manifest.source_path)) + )?; + for (key, value) in runtime_environment_assignments(runtime_env)? { + if key == "PATH" { + continue; + } + writeln!( + runner, + "$env:{} = {}", + key, + powershell_quote(&value.to_string_lossy()) + )?; + } + let mut path_prefix_entries = runtime_env.path_entries.clone(); + path_prefix_entries.extend(runtime_env.library_entries.iter().cloned()); + let path_prefix = path_prefix_entries + .iter() + .map(|path| child_path_string(path)) + .collect::>() + .join(";"); + if !path_prefix.is_empty() { + writeln!( + runner, + "$env:PATH = {} + ';' + $env:PATH", + powershell_quote(&path_prefix) + )?; + } + let mut command_line = powershell_quote(&child_path_string(&manifest.python_executable)); + for arg in [ + "main.py", + "--listen", + options.host.as_str(), + "--port", + &options.port.to_string(), + ] { + write!(command_line, " {}", powershell_quote(arg))?; + } + writeln!( + runner, + "& {} *>> {}", + command_line, + powershell_quote(&child_path_string(log_path)) + )?; + fs::write(&runner_path, runner) + .with_context(|| format!("failed to write {}", runner_path.display()))?; + + let launcher = format!( + "$p = Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-File',{}) -WindowStyle Hidden -PassThru\nSet-Content -LiteralPath {} -Value $p.Id -Encoding ASCII\n", + powershell_quote(&child_path_string(&runner_path)), + powershell_quote(&child_path_string(&pid_path)) + ); + fs::write(&launcher_path, launcher) + .with_context(|| format!("failed to write {}", launcher_path.display()))?; + + let mut command = Command::new("powershell.exe"); + command + .arg("-NoProfile") + .arg("-ExecutionPolicy") + .arg("Bypass") + .arg("-File") + .arg(&launcher_path); + let output = capture_configured_command( + command, + &format!("run ComfyUI launcher {}", launcher_path.display()), + )?; + let pid_text = fs::read_to_string(&pid_path).unwrap_or_default(); + let _ = fs::remove_file(&pid_path); + if !output.status.success() && pid_text.trim().is_empty() { + bail!( + "failed to launch ComfyUI in the background: {}", + String::from_utf8_lossy(&output.stderr).trim() + ); + } + let pid = pid_text + .split_whitespace() + .next() + .context("PowerShell launcher did not return a process id")? + .parse::() + .context("PowerShell launcher returned an invalid process id")?; + Ok(pid) +} + +pub(super) fn evaluate_running_state(state: &ComfyUiState) -> ComfyUiRunReport { + let process_running = process_is_running(state.pid); + let endpoint_reachable = endpoint_is_reachable(&state.host, state.port); + let state = if endpoint_reachable { + ComfyUiRunState::Running + } else if process_running { + ComfyUiRunState::Starting + } else { + ComfyUiRunState::Stopped + }; + ComfyUiRunReport { + state, + process_running, + endpoint_reachable, + } +} + +fn wait_for_running_state(state: &ComfyUiState, timeout: Duration) -> ComfyUiRunReport { + let deadline = std::time::Instant::now() + timeout; + let mut last_report = evaluate_running_state(state); + while std::time::Instant::now() < deadline { + if last_report.state == ComfyUiRunState::Running { + return last_report; + } + thread::sleep(Duration::from_millis(500)); + last_report = evaluate_running_state(state); + } + last_report +} + +pub(super) const fn comfyui_run_state_cli_label(state: ComfyUiRunState) -> &'static str { + match state { + ComfyUiRunState::Running => "running", + ComfyUiRunState::Starting => "starting", + ComfyUiRunState::Stopped => "stopped", + } +} + +pub(super) fn endpoint_is_reachable(host: &str, port: u16) -> bool { + let Ok(addresses) = (host, port).to_socket_addrs() else { + return false; + }; + addresses + .into_iter() + .any(|address| TcpStream::connect_timeout(&address, Duration::from_millis(200)).is_ok()) +} + +fn process_is_running(pid: u32) -> bool { + if pid == 0 { + return false; + } + if runtime_is_windows() { + return windows_process_is_running(pid); + } + if runtime_is_linux() { + return unix_process_is_running(pid); + } + true +} + +fn windows_process_is_running(pid: u32) -> bool { + let filter = format!("PID eq {pid}"); + let mut command = Command::new("tasklist"); + command.args(["/FI", &filter, "/FO", "CSV", "/NH"]); + let Ok(output) = capture_configured_command(command, "check ComfyUI process") else { + return false; + }; + let stdout = String::from_utf8_lossy(&output.stdout); + if stdout.contains(&format!("\"{pid}\"")) { + return true; + } + if !output.status.success() { + return false; + } + false +} + +fn unix_process_is_running(pid: u32) -> bool { + Command::new("kill") + .arg("-0") + .arg(pid.to_string()) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .is_ok_and(|status| status.success()) +} + +fn wait_until_stopped(pid: u32) { + for _ in 0..20 { + if !process_is_running(pid) { + break; + } + thread::sleep(Duration::from_millis(100)); + } +} + +fn terminate_process_tree(pid: u32) -> Result<()> { + if !process_is_running(pid) { + return Ok(()); + } + if runtime_is_windows() { + return terminate_windows_process_tree(pid); + } + if runtime_is_linux() { + return terminate_unix_process(pid); + } + Ok(()) +} + +fn terminate_windows_process_tree(pid: u32) -> Result<()> { + let status = Command::new("taskkill") + .args(["/PID", &pid.to_string(), "/T", "/F"]) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .context("failed to run taskkill for ComfyUI")?; + if !status.success() && process_is_running(pid) { + bail!("failed to stop ComfyUI process {pid}"); + } + Ok(()) +} + +fn terminate_unix_process(pid: u32) -> Result<()> { + let status = Command::new("kill") + .arg(pid.to_string()) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .context("failed to run kill for ComfyUI")?; + if !status.success() && process_is_running(pid) { + bail!("failed to stop ComfyUI process {pid}"); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::net::TcpListener; + use std::path::PathBuf; + + #[test] + fn running_state_requires_saved_process_and_reachable_port() -> Result<()> { + let listener = TcpListener::bind("127.0.0.1:0")?; + let port = listener.local_addr()?.port(); + let state = ComfyUiState { + app_id: APP_ID.to_owned(), + url: format!("http://127.0.0.1:{port}"), + host: "127.0.0.1".to_owned(), + port, + pid: std::process::id(), + source_path: PathBuf::from("source"), + python_executable: PathBuf::from("python"), + log_path: PathBuf::from("start.log"), + started_at_unix_ms: 200, + }; + + let report = evaluate_running_state(&state); + + assert_eq!(report.state, ComfyUiRunState::Running); + assert!(report.process_running); + assert!(report.endpoint_reachable); + Ok(()) + } +} diff --git a/crates/rocm-core/src/quoting.rs b/crates/rocm-core/src/quoting.rs index 68f267ed23..15839c3335 100644 --- a/crates/rocm-core/src/quoting.rs +++ b/crates/rocm-core/src/quoting.rs @@ -13,8 +13,8 @@ //! `posix_shell_quote`'s output is sent over SSH to a remote shell //! (`apps/rocm/src/remote/mod.rs`'s `remote_serve_command` and friends), and //! `powershell_quote`'s output is written into a `.ps1` script executed via -//! `powershell.exe -File` (`apps/rocm/src/comfyui.rs`'s Windows runtime -//! launch) or interpolated into a `-Command` string +//! `powershell.exe -File` (`apps/rocm/src/comfyui/process.rs`'s Windows +//! runtime launch) or interpolated into a `-Command` string //! (`crate::fix::path_export_windows`, `crate::diagnose`'s matching //! printed copy). A mistake in either one is a real command-injection or //! script-corruption bug, which is why both carry direct unit coverage here diff --git a/docs/architecture.md b/docs/architecture.md index 6b358d7f01..9d29c235c9 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -38,7 +38,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every - [`cli_audit.rs`](../apps/rocm/src/cli_audit.rs) — mechanical relocation, no owned types: the CLI audit and lifecycle log (`record_cli_audit_event`, plus the helpers behind it that write `logs/cli-lifecycle.log` and the per-action files under `logs/cli/`). Its production code depends only on `rocm-core`. `record_cli_audit_event`, `cli_lifecycle_log_path` and `sanitize_log_value` are `pub(crate)` and reached from other modules as `crate::` through a re-import in [`main.rs`](../apps/rocm/src/main.rs); every other item is private. The two regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::`. - [`cli_progress.rs`](../apps/rocm/src/cli_progress.rs) — shared CLI-output component (`Spinner`, `AnimatedSpinner`). - [`cli_report.rs`](../apps/rocm/src/cli_report.rs) — shared CLI-output component (`ActionReport`). -- [`comfyui.rs`](../apps/rocm/src/comfyui.rs) — full domain extraction; serves the `comfyui` subcommand, whose command enum (`ComfyuiCommand`) and dispatch fn stay in [`main.rs`](../apps/rocm/src/main.rs) (unlike [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs) above, which owns its own). It owns the ComfyUI types and constants, the install and state paths, the manifest and state files, `status` and `logs` rendering, the install and process-lifecycle code, and the helpers that run a Python child and capture its output. Choosing the ROCm runtime and building its environment is in the child module [`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs), listed below with the other `apps/rocm` modules. +- [`comfyui.rs`](../apps/rocm/src/comfyui.rs) — full domain extraction; serves the `comfyui` subcommand, whose command enum (`ComfyuiCommand`) and dispatch fn stay in [`main.rs`](../apps/rocm/src/main.rs) (unlike [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs) above, which owns its own). It owns the ComfyUI types and constants, the install and state paths, the manifest and state files, `status` and `logs` rendering, the install code, and the helpers that run a Python child and capture its output. Choosing the ROCm runtime and building its environment ([`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs)) and the process lifecycle ([`comfyui/process.rs`](../apps/rocm/src/comfyui/process.rs)) are in child modules, listed below with the other `apps/rocm` modules; `start` and `stop` are re-exported from `process` so callers still write `comfyui::start`/`comfyui::stop`. - [`config_cmd.rs`](../apps/rocm/src/config_cmd.rs) — mechanical relocation, dispatch-adjacent, no owned types: `rocm config`'s command handler (`config()`), the provider-key prompt it uses (`read_provider_key_from_user`) and the text `rocm config show` prints (`render_config_text`, `telemetry_policy_summary`). `ConfigCommand` stays in [`main.rs`](../apps/rocm/src/main.rs). `config()` and `render_config_text` are `pub(crate)`: `dispatch()` calls `config()` and [`chat.rs`](../apps/rocm/src/chat.rs)'s read-only handling of `rocm config show` calls `render_config_text`, both through re-imports in `main.rs`; the other two functions are private. The two `render_config_text` regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::`. - [`dash.rs`](../apps/rocm/src/dash.rs) — full domain extraction. - [`dash_seam.rs`](../apps/rocm/src/dash_seam.rs) — full domain extraction. @@ -101,6 +101,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every Further `apps/rocm` modules, not part of the `main.rs` split: - [`advised_commands.rs`](../apps/rocm/src/advised_commands.rs) — test-only (`#[cfg(test)]`): checks that every `rocm …`/`rocmd …` command the repository advises — in `--help` text, production string literals, the markdown docs and skills, and the VHS tapes — exists and parses, by running it through the parser of the binary it names (`rocm`'s clap `Cli`, or `rocmd`'s `parse_args`). +- [`comfyui/process.rs`](../apps/rocm/src/comfyui/process.rs) — child module of [`comfyui.rs`](../apps/rocm/src/comfyui.rs): the lifecycle of the ComfyUI process this CLI launches. `start` checks the installed folder, runs the GPU check, launches ComfyUI in the background (a detached child on Linux, a hidden PowerShell launcher on Windows) and waits for its port; `stop` ends the saved process tree. `evaluate_running_state` tells a running, a still-loading and a stopped ComfyUI apart for `status`, and `running_from`, `refuse_while_running` and `running_advice` are the guard that refuses a reinstall while ComfyUI runs from the folder it would replace. The saved state and the types in it stay in `comfyui.rs`. It imports from [`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs) and nothing else among the `comfyui` children. - [`comfyui/runtime_env.rs`](../apps/rocm/src/comfyui/runtime_env.rs) — child module of [`comfyui.rs`](../apps/rocm/src/comfyui.rs): choosing which installed ROCm runtime ComfyUI runs against (`select_runtime`, with the messages that tell the user how to pick one when none or several are ready) and building the `VIRTUAL_ENV`, `ROCM_PATH`, `PATH` and library-path environment a ComfyUI child process runs with. The types it returns (`SelectedRuntime`, `ComfyUiRuntimeEnvironment`) stay in `comfyui.rs`, which the module imports from; it imports nothing from the other `comfyui` children. - [`comfyui/source_swap.rs`](../apps/rocm/src/comfyui/source_swap.rs) — child module of [`comfyui.rs`](../apps/rocm/src/comfyui.rs): replacing ComfyUI's code in an existing `source/` folder on reinstall without touching anything else there. - [`remote/mod.rs`](../apps/rocm/src/remote/mod.rs) — `rocm remote`: driving a remote GPU host from this machine, with SSH as the control channel and the remote's own service published onto the tailnet for inference traffic. Like [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs), it owns its command enum (`RemoteCommand`) and dispatch fn (`run`). Its `pub(crate)` submodules: From afe37c29a6edb172cb2d996d868cb6aadcc9ff27 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Wed, 4 Nov 2026 13:38:52 +0000 Subject: [PATCH 3/5] refactor(comfyui): move install into comfyui/install.rs Moves the install action (including its dry run and the reinstall guard call sites), the source download and unpack, the requirements filtering, the torch-stack pinning and the logged uv command runner into a child module, together with the install unit tests and the Unix-only archive_properties property tests for the source unpack. comfyui.rs re-exports install, so callers still write comfyui::install. Pure move: no behaviour change; the manifest, the log paths and the GPU probe stay in comfyui.rs. install imports runtime_env and, for the running-ComfyUI guard, process; neither imports it. The fixtures the install tests share with the tests left in comfyui.rs stay there and are widened to pub(super). The two spinner-label entries in xtask's ASCII output contract and the cargo test filter in docs/testing.md follow the code to the new path; docs/architecture.md describes the module. Signed-off-by: Jussi Elo --- apps/rocm/src/comfyui.rs | 2239 +-------------------------- apps/rocm/src/comfyui/install.rs | 2263 ++++++++++++++++++++++++++++ docs/architecture.md | 3 +- docs/testing.md | 9 +- xtask/src/ascii_output_contract.rs | 4 +- 5 files changed, 2293 insertions(+), 2225 deletions(-) create mode 100644 apps/rocm/src/comfyui/install.rs diff --git a/apps/rocm/src/comfyui.rs b/apps/rocm/src/comfyui.rs index 10390f0a9e..6bb6fd59ea 100644 --- a/apps/rocm/src/comfyui.rs +++ b/apps/rocm/src/comfyui.rs @@ -2,34 +2,28 @@ // // SPDX-License-Identifier: MIT -use crate::cli_progress::AnimatedSpinner; -use crate::freeform_planner::format_structured_tool_call; use crate::therock; use anyhow::{Context, Result, bail}; -use flate2::read::GzDecoder; -use process::{ - comfyui_run_state_cli_label, endpoint_is_reachable, evaluate_running_state, - refuse_while_running, running_advice, running_from, -}; +use process::{comfyui_run_state_cli_label, endpoint_is_reachable, evaluate_running_state}; use rocm_core::{ - AppPaths, RocmCliConfig, download_file_to_path_with_progress, ensure_uv_binary, - format_http_base_url, runtime_is_windows, runtime_path_for_windows_child, unix_time_millis, - uv_command_env, uv_pip_install_base, + AppPaths, RocmCliConfig, format_http_base_url, runtime_is_windows, + runtime_path_for_windows_child, unix_time_millis, }; -use runtime_env::{apply_runtime_environment, runtime_environment_from_runtime, select_runtime}; +use runtime_env::apply_runtime_environment; use serde::{Deserialize, Serialize}; use std::fmt::Write as FmtWrite; use std::fs; -use std::io::{self, IsTerminal, Read, Write as IoWrite}; +use std::io; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus, Stdio}; -use std::thread; -use std::time::{Duration, SystemTime}; +use std::time::SystemTime; +mod install; mod process; mod runtime_env; mod source_swap; +pub(crate) use install::install; pub(crate) use process::{start, stop}; const APP_ID: &str = "comfyui"; @@ -374,295 +368,6 @@ fn render_logs_with_options( Ok(output) } -pub(crate) fn install( - paths: &AppPaths, - config: &RocmCliConfig, - options: ComfyUiInstallOptions, -) -> Result { - paths.ensure()?; - let runtime = select_runtime(paths, config, options.runtime_id.as_deref())?; - let app_root = runtime_app_root(&runtime.manifest); - let source_path = source_path_from_app_root(&app_root); - let log_path = install_log_path_from_app_root(&app_root); - let requirements_path = source_path.join("requirements.txt"); - let models_folder = models_folder_for_source(&source_path); - let runtime_env = runtime_environment_from_runtime(&runtime.manifest, &runtime.python); - - let mut output = String::new(); - writeln!(output, "{APP_NAME}")?; - writeln!(output, " action: install")?; - writeln!( - output, - " ROCm runtime: {}", - therock::runtime_version_display(&runtime.manifest.version) - )?; - writeln!(output, " folder: {}", source_path.display())?; - writeln!(output, " models path: {}", models_folder.display())?; - - // A reinstall that was interrupted part-way through replacing the code is - // finished by the next install, with or without `--reinstall`, rather than - // left as a half-replaced tree that would be reused as it is. - let finishes_interrupted = source_swap::swap_interrupted(&source_path); - let replaces_existing = source_path.exists() && (options.reinstall || finishes_interrupted); - if options.dry_run { - writeln!(output, " mode: dry-run")?; - if replaces_existing { - if finishes_interrupted { - writeln!( - output, - " note: a previous reinstall was interrupted; installing finishes it" - )?; - } - writeln!( - output, - " reinstall: replaces the ComfyUI code in {}", - source_path.display() - )?; - // The real run checks the same thing and refuses. - let mut warnings = Vec::new(); - if let Some(state) = running_from(paths, &source_path, &mut warnings) { - writeln!( - output, - " note: {}; a reinstall is refused until then", - running_advice(&source_path, &state) - )?; - } - for warning in warnings { - writeln!(output, " note: {warning}")?; - } - // The same list the real run reports as `kept:` — read from the - // interrupted run's marker when there is one. - let keeps = source_swap::planned_kept(&source_path); - if !keeps.is_empty() { - writeln!(output, " keeps: {}", keeps.join(", "))?; - } - // Which entries those are is only known once the new release is - // unpacked. `source_swap`'s - // `swap_replaces_release_code_and_leaves_everything_else_in_place`, - // `user_entry_named_like_new_release_code_is_set_aside` and - // `install_from_before_the_record_removes_only_what_the_new_release_ships` - // prove these lines; comfyui-05 and comfyui-10 the lines the real - // run prints. - if source_swap::has_release_record(&source_path) { - writeln!( - output, - " leaves in place: everything else in that folder that the installed release \ - did not ship" - )?; - writeln!( - output, - " sets aside: anything of yours named like code the new release adds, \ - renamed to {}