diff --git a/apps/rocm/src/chat.rs b/apps/rocm/src/chat.rs index 59617adeab..4ddcaa6893 100644 --- a/apps/rocm/src/chat.rs +++ b/apps/rocm/src/chat.rs @@ -3,16 +3,16 @@ // SPDX-License-Identifier: MIT //! Chat/assistant tool-call support: the chat/MCP-bridge cluster's types -//! (`ChatRocmCommandAction`, `DeterministicModelRecipe`, -//! `DeterministicServiceRow`, `DeterministicEngineRow`, and others — all -//! module-private except `ChatToolApprovalRequest`, reached by -//! `dash_seam.rs` as `chat_tool_approval_request`'s return type, and -//! `ChatRocmCommandAction`, read by `driver_plan.rs`'s test module), its -//! tool-call validation/approval/execution logic (`validate_chat_tool_call` -//! and the `validate_chat_*_tool_call` family), deterministic tool-output -//! summarization, and the MCP tool-bridge entry points -//! (`run_internal_mcp_call` and its helpers). Callers in other modules, -//! production and test-only, reach it through the `chat::` module boundary. +//! (`DeterministicModelRecipe`, `DeterministicServiceRow`, +//! `DeterministicEngineRow`, and others — all module-private), its +//! tool-call execution logic, deterministic tool-output summarization, and +//! the MCP tool-bridge entry points (`run_internal_mcp_call` and its +//! helpers). The tool-call validation and approval policy +//! (`validate_chat_tool_call`, the `validate_chat_*_tool_call` family, +//! `ChatRocmCommandAction` and `ChatToolApprovalRequest`) lives in +//! `tool_policy.rs`, which this module calls into and re-exports from. +//! Callers in other modules, production and test-only, reach this module's +//! items through the `chat::` module boundary. //! //! Full domain extraction, mirroring `providers.rs`. Phase 6d (ROCMAI-88) of //! the `main.rs` modularization (see `docs/architecture.md`), split across @@ -21,25 +21,52 @@ //! bodies, and the cluster's regression tests moved out of `main.rs`'s //! shared `mod tests` block), plus the third PR's own visibility audit — //! every function still `pub(crate)` has a confirmed caller outside this -//! module, production or test-only; none of the module's types need to be, -//! bar the two exceptions above. +//! module, production or test-only; none of the module's own types need to +//! be. use crate::automations::{render_automations_text, render_daemon_text}; use crate::engine_runtime_select::{app_path_env_vars, service_model_names_match}; -use crate::freeform_planner::{ - format_structured_tool_call, infer_engine_from_request, plan_freeform_request_with_context, - serve_args_request_cpu_device, -}; +use crate::freeform_planner::{format_structured_tool_call, plan_freeform_request_with_context}; use crate::managed_service_status::{ friendly_engine_label, load_managed_services, managed_service_is_live, }; +// `any_substring` lives in `prompt_text.rs` and stays reachable here as +// `chat::any_substring` for `freeform_answer.rs`. +pub(crate) use crate::prompt_text::any_substring; +use crate::prompt_text::{ + infer_engine_from_request, requested_install_prefix_from_prompt, + requested_therock_build_date_from_prompt, requested_therock_version_from_prompt, +}; +// `ensure_flag` lives in `rocm_argv.rs` and stays reachable here as +// `chat::ensure_flag` for `freeform_execution.rs`. +pub(crate) use crate::rocm_argv::ensure_flag; +use crate::rocm_argv::{chat_cli_arg_value, chat_cli_has_flag}; use crate::runtimes_cmd::render_runtimes_text; -use crate::service_launch::is_loopback_host; use crate::services_text::{ read_tail_lines, render_logs_browser_text, render_logs_text, render_service_logs_text, - render_services_text, validate_service_id, + render_services_text, +}; +// The tool-call policy lives in `tool_policy.rs`. `chat_tool_approval_request`, +// `chat_tool_call_is_read_only` and `validate_chat_tool_call` stay reachable +// here as `chat::` for `dash_seam.rs`. `ChatRocmCommandAction` and +// `rocm_chat_tool_requested_args` are used by this module's own code, so the +// import cannot be `#[cfg(test)]`; it is also `pub(crate)` because other +// modules' tests (`driver_plan.rs`, `system_packages.rs`) read both through +// `chat::`. `chat_install_driver_action` and `chat_rocm_command_action_from_args` +// are read through `chat::` only by those tests and this module does not call +// them itself, so their re-export below is `#[cfg(test)]`. +pub(crate) use crate::tool_policy::{ + ChatRocmCommandAction, chat_tool_approval_request, chat_tool_call_is_read_only, + rocm_chat_tool_requested_args, validate_chat_tool_call, +}; +use crate::tool_policy::{ + ChatToolApprovalRequest, chat_install_prefix_is_system, chat_rocm_command_action, json_string, + normalized_chat_rocm_command_args, rocm_chat_tool_requested_command, +}; +#[cfg(test)] +pub(crate) use crate::tool_policy::{ + chat_install_driver_action, chat_rocm_command_action_from_args, }; -use crate::shell_quote::has_terminal_control_character; use crate::uninstall_plan::render_uninstall_dry_run; use crate::update_cmd::render_update_text; use crate::{ @@ -54,8 +81,7 @@ use anyhow::{Context, Result, bail}; use rocm_core::placeholder::empty_as_unknown; use rocm_core::{ AppPaths, AutomationRuntimeState, CommandCapture, DEFAULT_LOCAL_HOST, ExamineSummary, - ManagedServiceRecord, RocmCliConfig, builtin_watcher, command_capture_text, - load_recent_automation_events, runtime_install_root_is_protected, + ManagedServiceRecord, RocmCliConfig, command_capture_text, load_recent_automation_events, }; use rocm_engine_protocol::DEFAULT_LOG_TAIL_LINES; use std::fmt::Write as _; @@ -64,16 +90,6 @@ use std::net::{TcpStream, ToSocketAddrs}; use std::path::{Path, PathBuf}; use std::time::Duration; -#[derive(Debug, Clone, Eq, PartialEq)] -pub(crate) enum ChatRocmCommandAction { - ReadOnly(Vec), - Approval { - args: Vec, - pending_title: String, - command_title: String, - }, -} - fn mcp_tool_result_text(value: &serde_json::Value) -> String { value .get("content") @@ -218,26 +234,6 @@ fn parse_optional_lines(args: &[String]) -> Result { Ok(DEFAULT_LOG_TAIL_LINES) } -fn push_optional_json_cli_arg( - args: &mut Vec, - object: &serde_json::Map, - flag: &str, - key: &str, -) { - if let Some(value) = json_string(object, key) { - args.push(flag.to_owned()); - args.push(value); - } -} - -fn json_string(object: &serde_json::Map, key: &str) -> Option { - object - .get(key) - .and_then(serde_json::Value::as_str) - .map(str::to_owned) - .filter(|value| !value.trim().is_empty()) -} - #[derive(Debug, Default)] struct DeterministicModelRecipe { pub(crate) canonical_id: String, @@ -590,15 +586,6 @@ pub(crate) fn parse_positive_u32(value: &str) -> Result { Ok(parsed) } -#[derive(Debug, Clone, Eq, PartialEq)] -pub(crate) struct ChatToolApprovalRequest { - pub pending_title: String, - pub command_title: String, - pub args: Vec, - pub display_command: Option, - pub explanation: Option, -} - #[derive(Debug, Clone, Eq, PartialEq)] struct ChatPromptResult { pub rendered: String, @@ -1437,10 +1424,6 @@ fn fallback_config_tool_call_for_prompt(normalized: &str) -> Option bool { - needles.iter().any(|needle| haystack.contains(needle)) -} - fn install_sdk_without_prefix_chat_approval(prompt: &str) -> Option { install_sdk_chat_approval_for_prompt(prompt).and_then(|approval| { chat_cli_arg_value(&approval.args, "--prefix") @@ -1608,151 +1591,6 @@ fn prompt_requests_install_action(normalized: &str) -> bool { ) } -pub(crate) fn requested_install_prefix_from_prompt(prompt: &str) -> Option { - let lower = prompt.to_ascii_lowercase(); - for phrase in [ - "--prefix=", - "--prefix ", - " into folder ", - " into ", - " in folder ", - " in ", - " to folder ", - " to ", - " under ", - " at ", - " use folder ", - " use ", - "use ", - " folder is ", - " folder: ", - "folder:", - ] { - let mut search_start = 0; - while let Some(relative_index) = lower[search_start..].find(phrase) { - let value_start = search_start + relative_index + phrase.len(); - if let Some(prefix) = clean_requested_install_prefix(&prompt[value_start..]) { - return Some(prefix); - } - search_start = value_start; - } - } - requested_bare_install_prefix_from_prompt(prompt) -} - -fn requested_bare_install_prefix_from_prompt(prompt: &str) -> Option { - for quote in ['"', '\''] { - let mut remaining = prompt; - while let Some(start) = remaining.find(quote) { - let after_start = &remaining[start + quote.len_utf8()..]; - let Some(end) = after_start.find(quote) else { - break; - }; - if let Some(prefix) = clean_requested_install_prefix(&after_start[..end]) { - return Some(prefix); - } - remaining = &after_start[end + quote.len_utf8()..]; - } - } - prompt.split_whitespace().find_map(|token| { - clean_requested_install_prefix( - token.trim_matches(|ch: char| { - matches!(ch, '"' | '\'' | '`' | ',' | ';' | '.' | ')' | ']') - }), - ) - }) -} - -fn clean_requested_install_prefix(candidate: &str) -> Option { - let trimmed = candidate.trim_start(); - if trimmed.is_empty() { - return None; - } - let mut chars = trimmed.chars(); - let first = chars.next()?; - let extracted = if first == '"' || first == '\'' { - let close = trimmed[first.len_utf8()..].find(first)?; - &trimmed[first.len_utf8()..first.len_utf8().saturating_add(close)] - } else { - trimmed - .split(['\r', '\n', ',', ';']) - .next() - .unwrap_or_default() - .split(" and ") - .next() - .unwrap_or_default() - .split(" with ") - .next() - .unwrap_or_default() - .split(" then ") - .next() - .unwrap_or_default() - .trim_end_matches(['.', ')', ']']) - }; - let prefix = extracted.trim(); - if prefix.is_empty() || !looks_like_user_path(prefix) { - return None; - } - Some(prefix.to_owned()) -} - -fn looks_like_user_path(value: &str) -> bool { - value.starts_with('/') - || value.starts_with('~') - || value.starts_with("\\\\") - || value.contains(":\\") - || value.contains(":/") - || value.contains('\\') -} - -pub(crate) fn requested_therock_build_date_from_prompt(normalized: &str) -> Option { - normalized - .split(|ch: char| !ch.is_ascii_alphanumeric()) - .filter(|token| token.len() == 8 && token.chars().all(|ch| ch.is_ascii_digit())) - .find_map( - |token| match therock::RuntimeVersionSelector::build_date(token).ok()? { - therock::RuntimeVersionSelector::BuildDate(date) => Some(date), - therock::RuntimeVersionSelector::Version(_) => None, - }, - ) - .or_else(|| { - normalized - .split_whitespace() - .map(|token| { - token.trim_matches(|ch: char| !ch.is_ascii_alphanumeric() && ch != '-') - }) - .find_map(|token| { - match therock::RuntimeVersionSelector::build_date(token).ok()? { - therock::RuntimeVersionSelector::BuildDate(date) => Some(date), - therock::RuntimeVersionSelector::Version(_) => None, - } - }) - }) -} - -fn requested_therock_version_from_prompt(normalized: &str) -> Option { - normalized - .split(|ch: char| !(ch.is_ascii_alphanumeric() || matches!(ch, '.' | '+' | '-' | '_'))) - .filter(|token| looks_like_therock_runtime_version(token)) - .find_map(|token| { - therock::RuntimeVersionSelector::version(token) - .ok() - .map(|_| token.to_owned()) - }) -} - -fn looks_like_therock_runtime_version(token: &str) -> bool { - let Some((prefix, date)) = token.rsplit_once('a') else { - return false; - }; - !prefix.is_empty() - && prefix.chars().next().is_some_and(|ch| ch.is_ascii_digit()) - && prefix.contains('.') - && date.len() == 8 - && date.chars().all(|ch| ch.is_ascii_digit()) - && therock::RuntimeVersionSelector::build_date(date).is_ok() -} - fn visible_chat_content(content: &str) -> String { let mut output = String::new(); let mut remaining = content; @@ -2006,469 +1844,6 @@ fn rocm_command_args_install_sdk_without_prefix( && chat_cli_arg_value(&args, "--prefix").is_none() } -pub(crate) fn chat_tool_approval_request( - call: &providers::ChatToolCall, - assistant_explanation: Option<&str>, -) -> Result { - validate_chat_tool_call(call)?; - if call.name == "rocm_command" { - let ChatRocmCommandAction::Approval { - args, - pending_title, - command_title, - } = chat_rocm_command_action(call)? - else { - bail!("ROCm command tool is read-only and does not need approval"); - }; - // `pending_title` is deliberately short (e.g. "Install driver"); don't - // read that as all the approver sees. The full `rocm` argv rides - // along via `display_command` below and is what the approval body - // actually renders (see `dash_seam.rs`'s `RocmToolExecutor::execute`). - return Ok(ChatToolApprovalRequest { - pending_title, - command_title, - display_command: Some(format_structured_tool_call("rocm", &args)), - args, - explanation: assistant_explanation - .map(str::trim) - .filter(|value| !value.is_empty()) - .map(str::to_owned), - }); - } - // `proposal_action` (approve/reject) executes in-process in - // `run_internal_mcp_call` — it has NO CLI argv, so build its approval - // request directly rather than through `rocm_chat_tool_requested_args`. - if call.name == "proposal_action" { - let object = call - .arguments - .as_object() - .context("proposal_action arguments must be a JSON object")?; - let proposal_id = json_string(object, "proposal_id") - .context("proposal_action requires non-empty `proposal_id`")?; - let action = - json_string(object, "action").context("proposal_action requires non-empty `action`")?; - let pending_title = match action.as_str() { - "approve" => "Approve proposal", - "reject" => "Reject proposal", - other => bail!("proposal_action `{other}` does not require approval"), - }; - return Ok(ChatToolApprovalRequest { - pending_title: pending_title.to_owned(), - command_title: "Reviews".to_owned(), - display_command: Some(format!("proposal {proposal_id}")), - args: Vec::new(), - explanation: assistant_explanation - .map(str::trim) - .filter(|value| !value.is_empty()) - .map(str::to_owned), - }); - } - let args = rocm_chat_tool_requested_args(call) - .with_context(|| format!("ROCm tool `{}` is missing required arguments", call.name))?; - let (pending_title, command_title) = match call.name.as_str() { - "install_sdk" => ("Install ROCm", "Install"), - "install_engine" => ("Install engine", "Engine"), - "launch_server" => ("Start local model server", "Serve"), - "stop_server" => ("Stop local model server", "Services"), - "watcher_enable" => ("Enable automation", "Automations"), - "watcher_disable" => ("Disable automation", "Automations"), - other => bail!("ROCm tool `{other}` is read-only or unsupported for approval"), - }; - // Same as the `rocm_command` arm above: `pending_title` is short by - // design — the full command line rides along in `display_command` and - // is what the approval body renders, not just this title. - Ok(ChatToolApprovalRequest { - pending_title: pending_title.to_owned(), - command_title: command_title.to_owned(), - display_command: rocm_chat_tool_requested_command(call), - args, - explanation: assistant_explanation - .map(str::trim) - .filter(|value| !value.is_empty()) - .map(str::to_owned), - }) -} - -pub(crate) fn validate_chat_tool_call(call: &providers::ChatToolCall) -> Result<()> { - // First, because every refusal below names the tool: a model-chosen name - // carrying a control character would otherwise reach the terminal inside - // the very error that rejects it. This one names nothing. - if has_terminal_control_character(&call.name) { - bail!("local assistant requested a ROCm tool whose name contains a control character"); - } - if !call.arguments.is_object() { - bail!("ROCm tool `{}` arguments must be a JSON object", call.name); - } - reject_control_characters_in_tool_arguments(&call.name, &call.arguments)?; - match call.name.as_str() { - "examine" - // `doctor` is the dash-side LLM tool + `/doctor` overlay name for the - // same machine inspection the bin exposes as `examine`. Accept it as an - // alias so a model-issued `doctor` call resolves end-to-end. - | "doctor" - | "bridge_snapshot" - | "gpu_snapshot" - | "engines" - | "services" - | "service_logs" - | "automations" - | "natural_language_plan" - | "path_exists" - | "port_status" - | "rocm_command" - | "update_check" - | "install_sdk_dry_run" - | "install_sdk" - | "install_engine" - | "launch_server" - | "stop_server" - | "watcher_enable" - | "watcher_disable" - | "proposal_action" => {} - other => bail!("local assistant requested unsupported ROCm tool `{other}`"), - } - match call.name.as_str() { - "install_sdk" => validate_chat_install_sdk_tool_call(call)?, - "install_engine" => validate_required_chat_string(call, "engine")?, - "launch_server" => validate_chat_launch_server_tool_call(call)?, - "service_logs" | "stop_server" => validate_chat_service_tool_call(call)?, - "automations" => validate_optional_chat_integer(call, "event_limit", 1, 64)?, - "natural_language_plan" => validate_required_chat_string(call, "request")?, - "path_exists" => validate_required_chat_string(call, "path")?, - "port_status" => validate_chat_port_status_tool_call(call)?, - "rocm_command" => validate_chat_rocm_command_tool_call(call)?, - "watcher_enable" => validate_chat_watcher_tool_call(call, true)?, - "watcher_disable" => validate_chat_watcher_tool_call(call, false)?, - "proposal_action" => validate_chat_proposal_action_tool_call(call)?, - _ => {} - } - Ok(()) -} - -/// The tools exempt from [`reject_control_characters_in_tool_arguments`]. -/// -/// Deny by default: every other tool, including one added later, has its -/// whole arguments object screened before anything else looks at it. The -/// criterion that used to pick tools *in* — "its arguments reach the -/// operator's terminal" — kept missing values that reach it some other way: -/// `port_status` echoes a refused `host` in its own error, which the CLI -/// prints. A list of exceptions fails safe when it is forgotten; a list of -/// guarded tools does not. -/// -/// `rocm_command` is the one exception, and it is not unscreened: its argv is -/// checked entry by entry in [`normalized_chat_rocm_command_args`], with the -/// same predicate, after trimming each entry — so screening the raw object -/// here would refuse a padded value that path legitimately accepts. Its -/// `reason` is free-form prose that is never printed. -/// -/// Pinned by `every_tool_but_the_exempt_one_refuses_a_control_character_anywhere` -/// (adding a name here turns it red) and -/// `an_unknown_tool_is_screened_before_it_is_rejected` (going back to an -/// allow-list turns it red). -const CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS: [&str; 1] = ["rocm_command"]; - -/// Refuse a model-supplied string argument carrying a terminal control or -/// format character, for every tool not in -/// [`CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS`]. -/// -/// Those values are printed: into the command preview an operator approves, -/// into the `request plan` / `execution` blocks `rocm ` emits, into -/// the plan text `natural_language_plan` returns, into a refusal that echoes -/// the value it refused, and — for `path_exists` — into a tool-result block -/// with no gate at all. All of it -/// reaches a terminal as bytes, so a `\r` returns the cursor to column one and -/// a CSI sequence erases the line; the model, not this code, then chooses what -/// the operator reads. Quoting cannot fix that: `'…'` makes the byte literal to -/// a *shell*, not to a terminal. -/// -/// Scope note, so the guarantee is not overread: this covers model-supplied -/// *arguments*, not model-supplied prose. An assistant's reply text is still -/// printed as it comes; `strip_ansi_sequences` in `comfyui.rs` is the tool for -/// that surface, and is already used on ComfyUI log output. -/// -/// Walks the whole arguments object rather than naming fields, so a new -/// argument on one of these tools inherits the guarantee instead of having to -/// remember it. -fn reject_control_characters_in_tool_arguments( - tool: &str, - arguments: &serde_json::Value, -) -> Result<()> { - if CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS.contains(&tool) { - return Ok(()); - } - reject_control_characters(tool, arguments) -} - -fn reject_control_characters(tool: &str, arguments: &serde_json::Value) -> Result<()> { - match arguments { - serde_json::Value::String(value) => { - if has_terminal_control_character(value) { - // The wording is load-bearing for the per-tool test, which - // asserts this refusal rather than merely `is_err()` — several - // of these tools would refuse a hostile value for some other - // reason, and a test that accepted any error would pass with - // this guard removed. - bail!( - "ROCm tool `{tool}` argument contains a control character; \ - it would rewrite what the operator reads" - ); - } - Ok(()) - } - serde_json::Value::Array(values) => values - .iter() - .try_for_each(|value| reject_control_characters(tool, value)), - serde_json::Value::Object(entries) => entries - .values() - .try_for_each(|value| reject_control_characters(tool, value)), - _ => Ok(()), - } -} - -/// Validate a `proposal_action` chat-tool call: `proposal_id` must be a -/// non-empty string and `action` must be one of show | approve | reject. -fn validate_chat_proposal_action_tool_call(call: &providers::ChatToolCall) -> Result<()> { - let object = call - .arguments - .as_object() - .context("proposal_action arguments must be a JSON object")?; - let proposal_id = json_string(object, "proposal_id") - .context("proposal_action requires non-empty `proposal_id`")?; - if proposal_id.len() > 128 { - bail!("proposal_id too long"); - } - let action = - json_string(object, "action").context("proposal_action requires non-empty `action`")?; - if !matches!(action.as_str(), "show" | "approve" | "reject") { - bail!("proposal_action `action` must be one of show, approve, reject"); - } - Ok(()) -} - -fn validate_chat_install_sdk_tool_call(call: &providers::ChatToolCall) -> Result<()> { - let object = call - .arguments - .as_object() - .context("install_sdk arguments must be a JSON object")?; - let channel = json_string(object, "channel").unwrap_or_else(|| "release".to_owned()); - if !matches!(channel.as_str(), "release" | "nightly") { - bail!("local assistant requested unsupported TheRock channel `{channel}`"); - } - let format = json_string(object, "format").unwrap_or_else(|| "wheel".to_owned()); - if !matches!(format.as_str(), "wheel" | "tarball") { - bail!("local assistant requested unsupported TheRock install format `{format}`"); - } - if rocm_core::runtime_is_windows() && format != "wheel" { - bail!("local assistant cannot request `{format}` installs on Windows; use wheel"); - } - let version = json_string(object, "version"); - let build_date = json_string(object, "build_date"); - if version.is_some() && build_date.is_some() { - bail!("local assistant cannot request both `version` and `build_date`"); - } - if format != "wheel" && (version.is_some() || build_date.is_some()) { - bail!( - "local assistant can only request specific TheRock wheel versions for wheel installs" - ); - } - if let Some(version) = version { - therock::RuntimeVersionSelector::version(version)?; - } - if let Some(build_date) = build_date { - therock::RuntimeVersionSelector::build_date(build_date)?; - } - let Some(prefix) = json_string(object, "prefix") else { - bail!( - "local assistant must ask the user for a ROCm/TheRock install folder before requesting install_sdk" - ); - }; - let prefix_path = Path::new(&prefix); - if chat_install_prefix_is_system(prefix_path) { - bail!( - "local assistant cannot request system install folder `{}`", - prefix_path.display() - ); - } - Ok(()) -} - -fn validate_chat_launch_server_tool_call(call: &providers::ChatToolCall) -> Result<()> { - let object = call - .arguments - .as_object() - .context("launch_server arguments must be a JSON object")?; - if object - .get("allow_public_bind") - .and_then(serde_json::Value::as_bool) - .unwrap_or(false) - { - bail!("local assistant cannot request public network binding"); - } - let host = json_string(object, "host").unwrap_or_else(|| DEFAULT_LOCAL_HOST.to_owned()); - if !is_loopback_host(&host) { - bail!("local assistant cannot request non-local host `{host}`"); - } - if object - .get("device") - .and_then(serde_json::Value::as_str) - .is_some_and(|device| device.to_ascii_lowercase().contains("cpu")) - { - bail!("local assistant cannot request CPU execution; ROCm GPU execution is required"); - } - Ok(()) -} - -fn validate_chat_service_tool_call(call: &providers::ChatToolCall) -> Result<()> { - let object = call - .arguments - .as_object() - .context("service tool arguments must be a JSON object")?; - let service_id = - json_string(object, "service_id").context("service tool requires `service_id`")?; - validate_service_id(&service_id)?; - if call.name == "service_logs" { - validate_optional_chat_integer(call, "lines", 1, 500)?; - } - Ok(()) -} - -fn validate_chat_watcher_tool_call(call: &providers::ChatToolCall, allow_mode: bool) -> Result<()> { - let object = call - .arguments - .as_object() - .context("watcher tool arguments must be a JSON object")?; - let watcher = json_string(object, "watcher").context("watcher tool requires `watcher`")?; - if watcher.len() > 128 { - bail!("watcher id too long"); - } - if builtin_watcher(&watcher).is_none() { - bail!("local assistant requested unknown watcher `{watcher}`"); - } - if let Some(mode) = json_string(object, "mode") { - if !allow_mode { - bail!("local assistant cannot set `mode` when disabling a watcher"); - } - if !matches!(mode.as_str(), "observe" | "propose" | "contained") { - bail!("local assistant requested unsupported watcher mode `{mode}`"); - } - } - Ok(()) -} - -fn validate_chat_port_status_tool_call(call: &providers::ChatToolCall) -> Result<()> { - let object = call - .arguments - .as_object() - .context("port_status arguments must be a JSON object")?; - let host = json_string(object, "host").unwrap_or_else(|| DEFAULT_LOCAL_HOST.to_owned()); - if !is_loopback_host(&host) { - bail!("local assistant cannot inspect non-local host `{host}`"); - } - let Some(port) = object.get("port").and_then(serde_json::Value::as_u64) else { - bail!("ROCm tool `port_status` requires integer `port`"); - }; - if !(1..=u64::from(u16::MAX)).contains(&port) { - bail!("ROCm tool `port_status` argument `port` must be between 1 and 65535"); - } - Ok(()) -} - -fn validate_required_chat_string(call: &providers::ChatToolCall, key: &str) -> Result<()> { - let object = call - .arguments - .as_object() - .context("tool arguments must be a JSON object")?; - json_string(object, key) - .with_context(|| format!("ROCm tool `{}` requires non-empty `{key}`", call.name))?; - Ok(()) -} - -fn validate_optional_chat_integer( - call: &providers::ChatToolCall, - key: &str, - min: u64, - max: u64, -) -> Result<()> { - let object = call - .arguments - .as_object() - .context("tool arguments must be a JSON object")?; - let Some(value) = object.get(key) else { - return Ok(()); - }; - let Some(value) = value.as_u64() else { - bail!( - "ROCm tool `{}` argument `{key}` must be an integer", - call.name - ); - }; - if value < min || value > max { - bail!( - "ROCm tool `{}` argument `{key}` must be between {min} and {max}", - call.name - ); - } - Ok(()) -} - -fn validate_chat_rocm_command_tool_call(call: &providers::ChatToolCall) -> Result<()> { - chat_rocm_command_action(call).map(|_| ()) -} - -fn chat_rocm_command_action(call: &providers::ChatToolCall) -> Result { - let args = normalized_chat_rocm_command_args(call)?; - chat_rocm_command_action_from_args(args) -} - -fn normalized_chat_rocm_command_args(call: &providers::ChatToolCall) -> Result> { - let object = call - .arguments - .as_object() - .context("rocm_command arguments must be a JSON object")?; - let values = object - .get("args") - .and_then(serde_json::Value::as_array) - .context("rocm_command requires `args`")?; - if values.is_empty() || values.len() > 64 { - bail!("rocm_command `args` must contain 1 to 64 strings"); - } - let mut args = Vec::with_capacity(values.len()); - for value in values { - let arg = value - .as_str() - .map(str::trim) - .filter(|value| !value.is_empty()) - .context("rocm_command `args` entries must be non-empty strings")?; - // Every control character, not only NUL/LF/CR. These args are rendered - // into the approval card and the `advanced manual command:` line, which - // reach a terminal as bytes — an ESC or a C1 CSI there redraws what the - // operator is reading just as effectively as a `\r`, and this list used - // to let both through. See `has_terminal_control_character`. - if has_terminal_control_character(arg) { - bail!("rocm_command arguments must not contain control characters"); - } - if arg.len() > 512 { - bail!("rocm_command argument is too long"); - } - args.push(arg.to_owned()); - } - if args - .first() - .is_some_and(|arg| arg.eq_ignore_ascii_case("rocm")) - { - args.remove(0); - } - if args.is_empty() { - bail!("rocm_command args should omit the leading `rocm` program name"); - } - if let Some(reason) = object.get("reason") - && !reason.is_string() - { - bail!("rocm_command `reason` must be a string when present"); - } - Ok(args) -} - fn managed_service_running_state(status: &str) -> &'static str { match status { "ready" | "running" => "running", @@ -2506,519 +1881,53 @@ fn render_services_tool_result_text(records: &[ManagedServiceRecord]) -> String output } -/// The chat action for `rocm install driver`. Where the command only reports -/// (`only_reports`, the same decision the command itself makes) it is a -/// read-only call with no `--yes`; elsewhere it needs approval and carries -/// `--yes`. -/// -/// A model-supplied `--yes` is stripped in both forms, bare and `--yes=...`, -/// as the `install sdk` arm does: the approval path appends its own bare -/// `--yes`, so a stray attached form would otherwise ride along and clap would -/// refuse the whole command, and the read-only path has no consent to grant. -pub(crate) fn chat_install_driver_action( - mut args: Vec, - only_reports: bool, -) -> ChatRocmCommandAction { - args.retain(|arg| arg != "--yes" && !arg.starts_with("--yes=")); - if only_reports { - return ChatRocmCommandAction::ReadOnly(args); - } - ensure_flag(&mut args, "--yes"); - ChatRocmCommandAction::Approval { - args, - pending_title: "Install driver".to_owned(), - command_title: "Install".to_owned(), +fn deterministic_rocm_tool_summary(tool_text: &str) -> Option { + if !tool_text.contains("examine:") { + return None; + } + let mut lines = Vec::new(); + let gpu = chat_tool_value(tool_text, "driver_detail") + .filter(|value| value != "") + .or_else(|| { + let target = chat_tool_value(tool_text, "detected_gfx_target")?; + Some(format!("AMD GPU target {target}")) + }); + if let Some(gpu) = gpu { + lines.push(format!(" GPU: {gpu}")); } -} -pub(crate) fn chat_rocm_command_action_from_args( - mut args: Vec, -) -> Result { - canonicalize_chat_rocm_command(&mut args)?; - validate_chat_rocm_command_safety(&args)?; - let first = args.first().map(|value| value.to_ascii_lowercase()); - let second = args.get(1).map(|value| value.to_ascii_lowercase()); - match first.as_deref() { - Some("examine" | "version" | "model" | "models" | "daemon" | "logs") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("update") if !args.iter().any(|arg| arg == "--apply") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("runtimes") if second.as_deref().is_none_or(|value| value == "list") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) + let active_status = chat_tool_value(tool_text, "active_runtime_status"); + if active_status.as_deref() == Some("ready") { + let family = chat_tool_value(tool_text, "active_runtime_family") + .filter(|value| value != "" && value != ""); + let version = chat_tool_value(tool_text, "active_runtime_version") + .filter(|value| value != "" && value != ""); + let mut detail = "installed and active for ROCm CLI".to_owned(); + if let Some(family) = family { + let _ = write!(detail, " ({family})"); } - Some("engines") if second.as_deref().is_some_and(|value| value == "list") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) + if let Some(version) = version { + let _ = write!(detail, ", {version}"); } - Some("services") - if second - .as_deref() - .is_none_or(|value| matches!(value, "list" | "logs")) => + lines.push(format!(" ROCm/TheRock: {detail}")); + if let Some(root) = chat_tool_value(tool_text, "active_runtime_root") + .or_else(|| chat_tool_value(tool_text, "setup_runtime_root")) + .filter(|value| value != "" && value != "" && value != "") { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("automations") if second.as_deref().is_none_or(|value| value == "list") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("config") if second.as_deref() == Some("show") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) + lines.push(format!(" Install folder: {root}")); } - Some("comfyui") - if second - .as_deref() - .is_none_or(|value| matches!(value, "status" | "logs" | "log")) => + if let Some(cache) = chat_tool_value(tool_text, "active_runtime_pip_cache_dir") + .or_else(|| chat_tool_value(tool_text, "setup_runtime_pip_cache_dir")) + .filter(|value| value != "" && value != "" && value != "") { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("install") if second.as_deref() == Some("sdk") => { - // The chat/MCP surfaces spawn `rocm` with null stdin, so - // `interactive_terminal()` is false and the consent prompt would - // refuse with a "re-run with `--approve-replacing-active-default`" - // error, which the schema-constrained `install_sdk` tool gives the - // user no way to answer. - // - // Not `--yes`: that flag also approves system-package installs, and - // this spawn has no terminal on which to answer the `sudo` password - // prompt such an install can raise. Granting it here would make the - // vLLM/OpenMPI step run a sudo it cannot complete and abort the - // engine auto-install that previously warned and continued. The - // narrow flag grants exactly the consent the prompt is asking for. - // - // `--yes` is stripped rather than merely not added, because the - // generic `rocm_command` tool takes a model-supplied argv: a - // model-emitted `--yes` would otherwise reach this null-stdin spawn - // and re-grant the system-package consent `76c6aa3c` removed. The - // strip runs before the argv is rendered for human approval, so what - // is shown is still what runs. - // - // The `--yes=...` form is stripped too. Clap rejects an attached - // value on this flag today, so an exact-match strip happens to be - // airtight — but only by borrowing a property of clap's error - // taxonomy that nothing here owns. Giving `--yes` `num_args`, or a - // clap release that starts accepting `--yes=true` on a bare `bool`, - // would silently restore the sudo consent this strip exists to - // remove. Matching the prefix keeps the guarantee local to this - // function. - args.retain(|arg| arg != "--yes" && !arg.starts_with("--yes=")); - // Withheld on `--dry-run`, which returns before the consent gate and - // so needs no consent: `rocmd` and dash-tui omit it there for the - // same reason, and a preview should not be recorded as carrying an - // approval it never used. - if !args.iter().any(|arg| arg == "--dry-run") { - ensure_flag(&mut args, "--approve-replacing-active-default"); - } - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Install ROCm".to_owned(), - command_title: "Install".to_owned(), - }) - } - Some("install") if second.as_deref() == Some("driver") => Ok(chat_install_driver_action( - args, - crate::driver_install::host_install_driver_only_reports(), - )), - Some("update") if args.iter().any(|arg| arg == "--apply") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Apply ROCm update".to_owned(), - command_title: "Update".to_owned(), - }) + lines.push(format!(" Downloads/cache: {cache}")); } - Some("runtimes") - if second - .as_deref() - .is_some_and(|value| value == "uninstall" || value == "remove") - && args.iter().any(|arg| arg == "--dry-run") => + } else if let Some(status) = active_status.as_deref() { + lines.push(format!(" ROCm/TheRock: active runtime status is {status}")); + if let Some(root) = chat_tool_value(tool_text, "setup_runtime_root") + .filter(|value| value != "" && value != "" && value != "") { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("runtimes") - if second - .as_deref() - .is_some_and(|value| value == "uninstall" || value == "remove") => - { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Remove ROCm install".to_owned(), - command_title: "Runtimes".to_owned(), - }) - } - Some("runtimes") => Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Change ROCm runtime".to_owned(), - command_title: "Runtimes".to_owned(), - }), - Some("engines") if second.as_deref() == Some("install") => { - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Install engine".to_owned(), - command_title: "Engine".to_owned(), - }) - } - Some("serve") => Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Start local model server".to_owned(), - command_title: "Serve".to_owned(), - }), - Some("services") - if second - .as_deref() - .is_some_and(|value| matches!(value, "stop" | "restart")) => - { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Change local model server".to_owned(), - command_title: "Services".to_owned(), - }) - } - Some("automations") - if second - .as_deref() - .is_some_and(|value| matches!(value, "enable" | "disable")) => - { - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Change automation".to_owned(), - command_title: "Automations".to_owned(), - }) - } - Some("config") => Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Change settings".to_owned(), - command_title: "Config".to_owned(), - }), - Some("uninstall") if args.iter().any(|arg| arg == "--dry-run") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("uninstall") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Uninstall ROCm CLI".to_owned(), - command_title: "Uninstall".to_owned(), - }) - } - // `storage report` (the default subcommand) only measures folders, so - // the assistant can run it directly. The two `remove-*` verbs delete - // multi-gigabyte trees and go through the approval UI. - Some("storage") if second.as_deref().is_none_or(|value| value == "report") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("storage") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Free up disk space".to_owned(), - command_title: "Storage".to_owned(), - }) - } - Some("comfyui") if second.as_deref() == Some("install") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Install ComfyUI".to_owned(), - command_title: "ComfyUI".to_owned(), - }) - } - Some("comfyui") if second.as_deref() == Some("start") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Start ComfyUI".to_owned(), - command_title: "ComfyUI".to_owned(), - }) - } - Some("comfyui") if second.as_deref() == Some("stop") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Stop ComfyUI".to_owned(), - command_title: "ComfyUI".to_owned(), - }) - } - Some("setup") if second.as_deref().is_none_or(|value| value == "status") => { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - Some("setup") if second.as_deref() == Some("reset") => { - ensure_flag(&mut args, "--yes"); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: "Reset first-time setup".to_owned(), - command_title: "Setup".to_owned(), - }) - } - // Mirrors the daemon's `ensure_rocm_command_is_read_only`, which admits - // the same three verbs. The daemon's arm carries the reasoning; the - // invariant is that the two agree, and this PR added the arm there - // first, so the two disagreed until this was written. - Some("remote") - if second - .as_deref() - .is_some_and(|value| matches!(value, "targets" | "doctor" | "status")) => - { - Ok(ChatRocmCommandAction::ReadOnly(args)) - } - // `serve`, `attach` and `stop` start, publish or tear down something on - // another machine, so they go through approval like any other mutation. - Some("remote") - if second - .as_deref() - .is_some_and(|value| matches!(value, "serve" | "attach" | "stop")) => - { - let verb = second.as_deref().unwrap_or_default().to_owned(); - Ok(ChatRocmCommandAction::Approval { - args, - pending_title: format!("Remote {verb}"), - command_title: "Remote".to_owned(), - }) - } - Some(command) => bail!("local assistant cannot use unsupported rocm command `{command}`"), - None => bail!("rocm_command requires at least one argument"), - } -} - -fn canonicalize_chat_rocm_command(args: &mut [String]) -> Result<()> { - if args - .first() - .is_some_and(|arg| arg.eq_ignore_ascii_case("comfy")) - { - args[0] = "comfyui".to_owned(); - } - if args - .first() - .is_some_and(|arg| arg.eq_ignore_ascii_case("engine")) - { - bail!("use `engines` for rocm engine commands"); - } - Ok(()) -} - -pub(crate) fn validate_chat_rocm_command_safety(args: &[String]) -> Result<()> { - if let Some(first) = args.first() - && first.eq_ignore_ascii_case("serve") - { - if chat_cli_has_flag(args, "--allow-public-bind") { - bail!("local assistant cannot request public network binding"); - } - if serve_args_request_cpu_device(args) { - bail!("local assistant cannot request CPU execution; ROCm GPU execution is required"); - } - if let Some(host) = chat_cli_arg_value(args, "--host") - && !is_loopback_host(host) - { - bail!("local assistant cannot request non-local host `{host}`"); - } - if chat_cli_has_flag(args, "--foreground") || !chat_cli_has_flag(args, "--managed") { - bail!("local assistant must request managed serving with --managed"); - } - } - if args - .first() - .is_some_and(|arg| arg.eq_ignore_ascii_case("comfyui")) - && args - .get(1) - .is_some_and(|arg| arg.eq_ignore_ascii_case("start")) - && let Some(host) = chat_cli_arg_value(args, "--host") - && !is_loopback_host(host) - { - bail!("local assistant cannot start ComfyUI on non-local host `{host}`"); - } - if args - .first() - .is_some_and(|arg| arg.eq_ignore_ascii_case("install")) - && args - .get(1) - .is_some_and(|arg| arg.eq_ignore_ascii_case("sdk")) - { - if rocm_core::runtime_is_windows() - && chat_cli_arg_value(args, "--format") - .is_some_and(|value| !value.eq_ignore_ascii_case("wheel")) - { - bail!("local assistant cannot request non-wheel ROCm installs on Windows"); - } - let version = chat_cli_arg_value_checked(args, "--version")?; - let build_date = chat_cli_arg_value_checked(args, "--build-date")?; - if version.is_some() && build_date.is_some() { - bail!("local assistant cannot request both --version and --build-date"); - } - if version.is_some() || build_date.is_some() { - let format = chat_cli_arg_value(args, "--format").unwrap_or("wheel"); - if !format.eq_ignore_ascii_case("wheel") { - bail!( - "local assistant can only request specific TheRock wheel versions for wheel installs" - ); - } - } - if let Some(version) = version { - therock::RuntimeVersionSelector::version(version)?; - } - if let Some(build_date) = build_date { - therock::RuntimeVersionSelector::build_date(build_date)?; - } - let Some(prefix) = chat_cli_arg_value_checked(args, "--prefix")? else { - bail!( - "local assistant must ask the user for a ROCm/TheRock install folder before requesting `rocm install sdk`" - ); - }; - let prefix_path = Path::new(prefix); - if chat_install_prefix_is_system(prefix_path) { - bail!( - "local assistant cannot request system install folder `{}`", - prefix_path.display() - ); - } - } - Ok(()) -} - -pub(crate) fn ensure_flag(args: &mut Vec, flag: &str) { - if !args.iter().any(|arg| arg == flag) { - args.push(flag.to_owned()); - } -} - -pub(crate) fn chat_cli_arg_value<'a>(args: &'a [String], name: &str) -> Option<&'a str> { - let mut index = 0; - while index < args.len() { - let arg = args[index].as_str(); - if let Some((option, value)) = arg.split_once('=') - && option == name - { - return Some(value); - } - if arg == name { - return args.get(index + 1).map(String::as_str); - } - index += 1; - } - None -} - -fn chat_cli_arg_value_checked<'a>(args: &'a [String], name: &str) -> Result> { - let mut index = 0; - while index < args.len() { - let arg = args[index].as_str(); - if let Some((option, value)) = arg.split_once('=') - && option == name - { - if value.trim().is_empty() { - bail!("local assistant provided empty value for {name}"); - } - return Ok(Some(value)); - } - if arg == name { - let Some(value) = args.get(index + 1).map(String::as_str) else { - bail!("local assistant omitted the value for {name}"); - }; - if value.starts_with("--") || value.trim().is_empty() { - bail!("local assistant omitted the value for {name}"); - } - return Ok(Some(value)); - } - index += 1; - } - Ok(None) -} - -pub(crate) fn chat_cli_has_flag(args: &[String], name: &str) -> bool { - args.iter().any(|arg| { - arg == name - || arg - .strip_prefix(name) - .is_some_and(|rest| rest.starts_with('=')) - }) -} - -fn chat_install_prefix_is_system(prefix: &Path) -> bool { - prefix.as_os_str().is_empty() || runtime_install_root_is_protected(prefix) -} - -pub(crate) fn chat_tool_call_is_read_only(call: &providers::ChatToolCall) -> bool { - if call.name == "rocm_command" { - return matches!( - chat_rocm_command_action(call), - Ok(ChatRocmCommandAction::ReadOnly(_)) - ); - } - // `proposal_action` is read-only only when showing a proposal; approve/reject - // mutate the proposal status and must route through approval. - if call.name == "proposal_action" { - return call - .arguments - .as_object() - .and_then(|object| json_string(object, "action")) - .as_deref() - == Some("show"); - } - matches!( - call.name.as_str(), - "examine" - | "doctor" - | "bridge_snapshot" - | "gpu_snapshot" - | "engines" - | "services" - | "service_logs" - | "automations" - | "natural_language_plan" - | "path_exists" - | "port_status" - | "update_check" - | "install_sdk_dry_run" - ) -} - -fn deterministic_rocm_tool_summary(tool_text: &str) -> Option { - if !tool_text.contains("examine:") { - return None; - } - let mut lines = Vec::new(); - let gpu = chat_tool_value(tool_text, "driver_detail") - .filter(|value| value != "") - .or_else(|| { - let target = chat_tool_value(tool_text, "detected_gfx_target")?; - Some(format!("AMD GPU target {target}")) - }); - if let Some(gpu) = gpu { - lines.push(format!(" GPU: {gpu}")); - } - - let active_status = chat_tool_value(tool_text, "active_runtime_status"); - if active_status.as_deref() == Some("ready") { - let family = chat_tool_value(tool_text, "active_runtime_family") - .filter(|value| value != "" && value != ""); - let version = chat_tool_value(tool_text, "active_runtime_version") - .filter(|value| value != "" && value != ""); - let mut detail = "installed and active for ROCm CLI".to_owned(); - if let Some(family) = family { - let _ = write!(detail, " ({family})"); - } - if let Some(version) = version { - let _ = write!(detail, ", {version}"); - } - lines.push(format!(" ROCm/TheRock: {detail}")); - if let Some(root) = chat_tool_value(tool_text, "active_runtime_root") - .or_else(|| chat_tool_value(tool_text, "setup_runtime_root")) - .filter(|value| value != "" && value != "" && value != "") - { - lines.push(format!(" Install folder: {root}")); - } - if let Some(cache) = chat_tool_value(tool_text, "active_runtime_pip_cache_dir") - .or_else(|| chat_tool_value(tool_text, "setup_runtime_pip_cache_dir")) - .filter(|value| value != "" && value != "" && value != "") - { - lines.push(format!(" Downloads/cache: {cache}")); - } - } else if let Some(status) = active_status.as_deref() { - lines.push(format!(" ROCm/TheRock: active runtime status is {status}")); - if let Some(root) = chat_tool_value(tool_text, "setup_runtime_root") - .filter(|value| value != "" && value != "" && value != "") - { - lines.push(format!(" Selected setup folder: {root}")); + lines.push(format!(" Selected setup folder: {root}")); } if let Some(cache) = chat_tool_value(tool_text, "setup_runtime_pip_cache_dir") .filter(|value| value != "" && value != "" && value != "") @@ -4100,115 +3009,10 @@ const fn chat_read_only_tool_status_label(is_error: bool) -> &'static str { } } -fn rocm_chat_tool_requested_command(call: &providers::ChatToolCall) -> Option { - let args = rocm_chat_tool_requested_args(call)?; - Some(format_structured_tool_call("rocm", &args)) -} - -pub(crate) fn rocm_chat_tool_requested_args(call: &providers::ChatToolCall) -> Option> { - let object = call.arguments.as_object()?; - match call.name.as_str() { - "install_sdk" => { - let mut args = vec![ - "install".to_owned(), - "sdk".to_owned(), - "--channel".to_owned(), - json_string(object, "channel").unwrap_or_else(|| "release".to_owned()), - "--format".to_owned(), - json_string(object, "format").unwrap_or_else(|| "wheel".to_owned()), - // The MCP surface runs `rocm` with null stdin, so the consent - // prompt would refuse. This keeps the tool non-interactive, - // matching the chat `install sdk` arm. Deliberately not `--yes`: - // that would additionally approve a `sudo` system-package - // install this spawn has no terminal to answer. - "--approve-replacing-active-default".to_owned(), - ]; - if let Some(prefix) = json_string(object, "prefix") { - args.push("--prefix".to_owned()); - args.push(prefix); - } - if let Some(version) = json_string(object, "version") { - args.push("--version".to_owned()); - args.push(version); - } - if let Some(build_date) = json_string(object, "build_date") { - args.push("--build-date".to_owned()); - args.push(build_date); - } - Some(args) - } - "install_engine" => { - let engine = json_string(object, "engine")?; - let mut args = vec!["engines".to_owned(), "install".to_owned(), engine]; - if let Some(runtime_id) = json_string(object, "runtime_id") { - args.push("--runtime-id".to_owned()); - args.push(runtime_id); - } - if let Some(python_version) = json_string(object, "python_version") { - args.push("--python-version".to_owned()); - args.push(python_version); - } - if object - .get("reinstall") - .and_then(serde_json::Value::as_bool) - .unwrap_or(false) - { - args.push("--reinstall".to_owned()); - } - Some(args) - } - "launch_server" => { - let model = json_string(object, "model")?; - let mut args = vec!["serve".to_owned(), model, "--managed".to_owned()]; - push_optional_json_cli_arg(&mut args, object, "--engine", "engine"); - push_optional_json_cli_arg(&mut args, object, "--device", "device"); - push_optional_json_cli_arg(&mut args, object, "--runtime-id", "runtime_id"); - push_optional_json_cli_arg(&mut args, object, "--env-id", "env_id"); - push_optional_json_cli_arg(&mut args, object, "--host", "host"); - if let Some(port) = object.get("port").and_then(serde_json::Value::as_u64) { - args.push("--port".to_owned()); - args.push(port.to_string()); - } - Some(args) - } - "stop_server" => { - let service_id = json_string(object, "service_id")?; - Some(vec![ - "services".to_owned(), - "stop".to_owned(), - service_id, - "--yes".to_owned(), - ]) - } - "watcher_enable" => { - let watcher = json_string(object, "watcher")?; - let mut args = vec!["automations".to_owned(), "enable".to_owned(), watcher]; - push_optional_json_cli_arg(&mut args, object, "--mode", "mode"); - Some(args) - } - "watcher_disable" => { - let watcher = json_string(object, "watcher")?; - Some(vec![ - "automations".to_owned(), - "disable".to_owned(), - watcher, - ]) - } - "rocm_command" => match chat_rocm_command_action(call).ok()? { - ChatRocmCommandAction::Approval { args, .. } - | ChatRocmCommandAction::ReadOnly(args) => Some(args), - }, - _ => None, - } -} - #[cfg(test)] mod tests { use super::*; - use crate::runtime_install_finalize::tests::consents_for_install_sdk_argv; use crate::tests::test_paths; - use crate::{Cli, Command, InstallTarget}; - use clap::Parser; use rocm_core::ManagedServiceRecord; use std::fs; use std::net::TcpListener; @@ -4221,29 +3025,6 @@ mod tests { } } - #[test] - fn runtimes_activate_approval_modal_uses_the_runtime_noun() { - // `comfyui install`'s selection errors steer the user to `rocm runtimes` - // and say "ROCm runtime". Running a mutating `rocm runtimes …` from chat - // raises an approval modal whose title is written here, not by clap, so - // `main.rs`'s `runtimes_help_uses_the_runtime_noun_throughout` (which - // covers `--help` text) cannot reach it. It said "Change ROCm install" - // until this rename. - let action = chat_rocm_command_action_from_args(vec![ - "runtimes".to_owned(), - "activate".to_owned(), - "some-runtime-key".to_owned(), - ]) - .expect("a mutating runtimes command classifies"); - let ChatRocmCommandAction::Approval { pending_title, .. } = action else { - panic!("`rocm runtimes activate` must require approval, got {action:?}"); - }; - assert!( - pending_title.contains("runtime") && !pending_title.contains("install"), - "the `runtimes` approval modal must use the `runtime` noun, got {pending_title:?}" - ); - } - #[test] fn mcp_tool_result_text_joins_text_content_items() { let value = serde_json::json!({ @@ -4415,25 +3196,6 @@ mod tests { assert!(parse_optional_lines(&["--lines".to_owned(), "nope".to_owned()]).is_err()); } - #[test] - fn push_optional_json_cli_arg_pushes_present_values_only() { - let object = serde_json::json!({"reason": "because"}); - let object = object.as_object().unwrap(); - let mut args = Vec::new(); - push_optional_json_cli_arg(&mut args, object, "--reason", "reason"); - push_optional_json_cli_arg(&mut args, object, "--missing", "missing"); - assert_eq!(args, vec!["--reason".to_owned(), "because".to_owned()]); - } - - #[test] - fn json_string_filters_missing_and_blank_values() { - let object = serde_json::json!({"reason": " ", "note": "ok"}); - let object = object.as_object().unwrap(); - assert_eq!(json_string(object, "note"), Some("ok".to_owned())); - assert_eq!(json_string(object, "reason"), None); - assert_eq!(json_string(object, "missing"), None); - } - #[test] fn parse_deterministic_model_recipes_reads_header_fields_and_notes() { let recipes = parse_deterministic_model_recipes( @@ -4528,532 +3290,40 @@ endpoint: 127.0.0.1:8001 } #[test] - fn normalized_chat_rocm_command_args_strips_leading_program_name() { - let call = rocm_command_call(&["rocm", "engines", "list"]); - assert_eq!( - normalized_chat_rocm_command_args(&call).unwrap(), - vec!["engines".to_owned(), "list".to_owned()] - ); + fn managed_service_running_state_maps_every_known_status() { + assert_eq!(managed_service_running_state("ready"), "running"); + assert_eq!(managed_service_running_state("running"), "running"); + assert_eq!(managed_service_running_state("starting"), "starting"); + assert_eq!(managed_service_running_state("recovering"), "starting"); + assert_eq!(managed_service_running_state("failed"), "not_running"); + assert_eq!(managed_service_running_state("stopped"), "not_running"); + assert_eq!(managed_service_running_state("anything-else"), "unknown"); } #[test] - fn normalized_chat_rocm_command_args_rejects_empty_and_control_characters() { - assert_eq!( - normalized_chat_rocm_command_args(&rocm_command_call(&[])) - .unwrap_err() - .to_string(), - "rocm_command `args` must contain 1 to 64 strings" + fn services_tool_result_text_includes_running_interpretation() { + let (_root, paths) = test_paths("services-tool-text"); + let mut record = ManagedServiceRecord::new( + &paths, + "svc-vllm", + "vllm", + "Qwen/Qwen3.5", + "Qwen/Qwen3.5", + "127.0.0.1", + 11435, + "managed", + std::process::id(), + Some("therock-release".to_owned()), + None, + Some("gpu_required".to_owned()), ); - assert!(normalized_chat_rocm_command_args(&rocm_command_call(&["rocm"])).is_err()); + record.status = "ready".to_owned(); - for control_arg in ["engines\nlist", "engines\rlist", "engines\0list"] { - let call = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({"args": [control_arg]}), - }; - assert_eq!( - normalized_chat_rocm_command_args(&call) - .unwrap_err() - .to_string(), - "rocm_command arguments must not contain control characters" - ); - } - } - - #[test] - fn normalized_chat_rocm_command_args_accepts_exactly_the_count_and_length_limits() { - let max_args = vec!["engines"; 64]; - assert_eq!( - normalized_chat_rocm_command_args(&rocm_command_call(&max_args)) - .unwrap() - .len(), - 64 - ); - - let max_len_arg = "a".repeat(512); - let call = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({"args": [max_len_arg.as_str()]}), - }; - assert_eq!( - normalized_chat_rocm_command_args(&call).unwrap(), - vec![max_len_arg] - ); - } - - #[test] - fn normalized_chat_rocm_command_args_rejects_over_limit_count_and_length() { - let too_many_args = vec!["engines"; 65]; - assert_eq!( - normalized_chat_rocm_command_args(&rocm_command_call(&too_many_args)) - .unwrap_err() - .to_string(), - "rocm_command `args` must contain 1 to 64 strings" - ); - - let too_long_arg = "a".repeat(513); - let call = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({"args": [too_long_arg]}), - }; - assert_eq!( - normalized_chat_rocm_command_args(&call) - .unwrap_err() - .to_string(), - "rocm_command argument is too long" - ); - } - - #[test] - fn normalized_chat_rocm_command_args_rejects_a_non_string_reason() { - let call = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({"args": ["engines", "list"], "reason": 42}), - }; - assert!(normalized_chat_rocm_command_args(&call).is_err()); - } - - #[test] - fn managed_service_running_state_maps_every_known_status() { - assert_eq!(managed_service_running_state("ready"), "running"); - assert_eq!(managed_service_running_state("running"), "running"); - assert_eq!(managed_service_running_state("starting"), "starting"); - assert_eq!(managed_service_running_state("recovering"), "starting"); - assert_eq!(managed_service_running_state("failed"), "not_running"); - assert_eq!(managed_service_running_state("stopped"), "not_running"); - assert_eq!(managed_service_running_state("anything-else"), "unknown"); - } - - #[test] - fn services_tool_result_text_includes_running_interpretation() { - let (_root, paths) = test_paths("services-tool-text"); - let mut record = ManagedServiceRecord::new( - &paths, - "svc-vllm", - "vllm", - "Qwen/Qwen3.5", - "Qwen/Qwen3.5", - "127.0.0.1", - 11435, - "managed", - std::process::id(), - Some("therock-release".to_owned()), - None, - Some("gpu_required".to_owned()), - ); - record.status = "ready".to_owned(); - - let rendered = render_services_tool_result_text(&[record]); - - assert!(rendered.contains("status_meaning: ready/running = running")); - assert!(rendered.contains("engine=vllm")); - assert!(rendered.contains("running_state=running")); - } - - #[test] - fn chat_tool_call_mutating_install_maps_to_reviewable_rocm_command() { - let call = providers::ChatToolCall { - id: Some("call-1".to_owned()), - name: "install_sdk".to_owned(), - arguments: serde_json::json!({ - "channel": "release", - "format": "wheel", - "prefix": "D:\\ROCm\\therock_venvs" - }), - }; - - assert!(!chat_tool_call_is_read_only(&call)); - validate_chat_tool_call(&call).expect("install request should validate for review"); - assert_eq!( - rocm_chat_tool_requested_command(&call).as_deref(), - Some( - "rocm install sdk --channel release --format wheel --approve-replacing-active-default --prefix 'D:\\ROCm\\therock_venvs'" - ) - ); - let approval = chat_tool_approval_request( - &call, - Some("TheRock is not installed yet, so I need to install ROCm first."), - ) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Install ROCm"); - assert_eq!(approval.command_title, "Install"); - assert_eq!( - approval.explanation.as_deref(), - Some("TheRock is not installed yet, so I need to install ROCm first.") - ); - assert_eq!( - approval.args, - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--channel".to_owned(), - "release".to_owned(), - "--format".to_owned(), - "wheel".to_owned(), - "--approve-replacing-active-default".to_owned(), - "--prefix".to_owned(), - "D:\\ROCm\\therock_venvs".to_owned(), - ] - ); - } - - #[test] - fn chat_install_sdk_strips_model_supplied_yes_and_skips_consent_on_dry_run() { - // `rocm_command` carries a model-supplied argv that nothing else filters, - // so `--yes` can arrive here. This arm exists to grant the narrow consent - // only; letting `--yes` through would re-grant the system-package/sudo - // consent on a spawn with no terminal to answer a password prompt. - let classify = |args: &[&str]| -> Vec { - let action = chat_rocm_command_action_from_args( - args.iter().copied().map(str::to_owned).collect(), - ) - .expect("install sdk should classify"); - let ChatRocmCommandAction::Approval { args, .. } = action else { - panic!("install sdk is a mutating command"); - }; - args - }; - - assert_eq!( - classify(&["install", "sdk", "--prefix", "/tmp/therock", "--yes"]), - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--prefix".to_owned(), - "/tmp/therock".to_owned(), - "--approve-replacing-active-default".to_owned(), - ] - ); - - // A dry run returns before the consent gate, so it is not given a consent - // it never uses — matching the dry-run-aware sibling arms. - assert_eq!( - classify(&["install", "sdk", "--prefix", "/tmp/therock", "--dry-run"]), - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--prefix".to_owned(), - "/tmp/therock".to_owned(), - "--dry-run".to_owned(), - ] - ); - - // Both together: the strip is unconditional, so `--yes` still does not - // survive into a preview spawn, and the dry run still gains no consent. - // A model that emits both must not end up with either flag. - assert_eq!( - classify(&[ - "install", - "sdk", - "--prefix", - "/tmp/therock", - "--yes", - "--dry-run", - ]), - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--prefix".to_owned(), - "/tmp/therock".to_owned(), - "--dry-run".to_owned(), - ] - ); - } - - #[test] - fn chat_install_sdk_strips_a_model_supplied_yes_in_both_its_bare_and_attached_forms() { - // `--yes` and `--yes=true` are both model-supplied argv that reach the - // chat arm intact: neither `canonicalize_chat_rocm_command` nor - // `validate_chat_rocm_command_safety` splits or rejects either. Whichever - // form survives re-grants into a null-stdin spawn the system-package/sudo - // consent `76c6aa3c` removed, with no terminal to answer the password - // prompt, so the strip has to catch both. - let classify = |args: &[&str]| -> Vec { - let action = chat_rocm_command_action_from_args( - args.iter().copied().map(str::to_owned).collect(), - ) - .expect("install sdk should classify"); - let ChatRocmCommandAction::Approval { args, .. } = action else { - panic!("install sdk is a mutating command"); - }; - args - }; - - // Both terms of `arg != "--yes" && !arg.starts_with("--yes=")` are driven - // here, and each alone: the bare form is caught only by the first, the - // `=` forms only by the second, so dropping either term reddens this test - // on its own rather than leaving one half to a sibling. - for supplied in ["--yes", "--yes=true", "--yes=1", "--yes=false"] { - let args = classify(&["install", "sdk", "--prefix", "/tmp/therock", supplied]); - assert!( - !args.iter().any(|arg| arg.starts_with("--yes")), - "`{supplied}` must not survive the chat strip, got {args:?}" - ); - assert_eq!( - args, - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--prefix".to_owned(), - "/tmp/therock".to_owned(), - "--approve-replacing-active-default".to_owned(), - ], - "stripping `{supplied}` must leave the rest of the argv and the narrow consent alone" - ); - } - - // Future-proofing, not a guard on the `--yes=` term this test's other - // assertions pin: `--yes-not-a-flag` survives both the exact-match strip - // that preceded that term and the two-term strip that replaced it, so it - // would pass on either. What it does catch is the next edit — widening - // the second term to `starts_with("--yes")` to "simplify" it would start - // eating every argv token that merely begins the same way, silently - // dropping arguments the model legitimately sent. - let args = classify(&[ - "install", - "sdk", - "--prefix", - "/tmp/therock", - "--yes-not-a-flag", - ]); - assert!( - args.iter().any(|arg| arg == "--yes-not-a-flag"), - "the strip must match `--yes` and `--yes=…`, not every token starting with \ - `--yes`, got {args:?}" - ); - - // Second layer, and only the second: clap also refuses an attached value - // on this flag, so even an unstripped `--yes=true` would not parse today. - // That is what the strip above deliberately stops depending on — pinned - // here so a later `num_args` on `--yes` shows up as a failure of the - // backstop rather than passing unnoticed. - for attached in ["--yes=true", "--yes=1", "--yes=false"] { - let error = match Cli::try_parse_from(["rocm", "install", "sdk", attached]) { - Ok(cli) => panic!("`{attached}` must not parse, got {cli:?}"), - Err(error) => error, - }; - assert_eq!( - error.kind(), - clap::error::ErrorKind::TooManyValues, - "expected clap to reject the attached value on {attached}: {error}" - ); - } - - // Control: the bare form does parse, so the assertions above are about - // the `=`-form and not about `--yes` being rejected outright. - let cli = Cli::try_parse_from(["rocm", "install", "sdk", "--yes"]) - .expect("the bare flag is the form the chat arm strips"); - match cli.command { - Some(Command::Install { - target: InstallTarget::Sdk { yes, .. }, - }) => assert!(yes), - other => panic!("expected `install sdk`, got {other:?}"), - } - } - - #[test] - fn chat_tool_call_mutating_install_accepts_requested_build_date() { - let call = providers::ChatToolCall { - id: Some("call-date".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["install", "sdk", "--channel", "release", "--format", "wheel", "--prefix", "D:\\ROCm\\therock_venvs", "--build-date", "06052026"], - "reason": "The user asked for the TheRock build from 2026-06-05." - }), - }; - - validate_chat_tool_call(&call).expect("date-specific install should validate for review"); - assert!(!chat_tool_call_is_read_only(&call)); - assert_eq!( - rocm_chat_tool_requested_command(&call).as_deref(), - Some( - "rocm install sdk --channel release --format wheel --prefix 'D:\\ROCm\\therock_venvs' --build-date 06052026 --approve-replacing-active-default" - ) - ); - let approval = - chat_tool_approval_request(&call, Some("Install the requested TheRock build.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Install ROCm"); - assert_eq!( - approval.args, - vec![ - "install".to_owned(), - "sdk".to_owned(), - "--channel".to_owned(), - "release".to_owned(), - "--format".to_owned(), - "wheel".to_owned(), - "--prefix".to_owned(), - "D:\\ROCm\\therock_venvs".to_owned(), - "--build-date".to_owned(), - "06052026".to_owned(), - "--approve-replacing-active-default".to_owned(), - ] - ); - } - - #[test] - fn chat_tool_call_rejects_mutating_install_without_user_folder() { - let structured = providers::ChatToolCall { - id: Some("call-missing-prefix".to_owned()), - name: "install_sdk".to_owned(), - arguments: serde_json::json!({ - "channel": "release", - "format": "wheel" - }), - }; - let error = validate_chat_tool_call(&structured) - .unwrap_err() - .to_string(); - assert!(error.contains("ask the user"), "unexpected error: {error}"); - - let command = providers::ChatToolCall { - id: Some("call-command-missing-prefix".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["install", "sdk", "--channel", "release", "--format", "wheel"], - "reason": "Install ROCm." - }), - }; - let error = validate_chat_tool_call(&command).unwrap_err().to_string(); - assert!(error.contains("ask the user"), "unexpected error: {error}"); - } - - #[test] - fn chat_tool_call_service_and_watcher_changes_map_to_reviewable_rocm_commands() { - let stop = providers::ChatToolCall { - id: Some("call-stop".to_owned()), - name: "stop_server".to_owned(), - arguments: serde_json::json!({ "service_id": "svc-qwen" }), - }; - validate_chat_tool_call(&stop).expect("stop request should validate for review"); - assert_eq!( - rocm_chat_tool_requested_command(&stop).as_deref(), - Some("rocm services stop svc-qwen --yes") - ); - let approval = - chat_tool_approval_request(&stop, Some("This server is using memory we need.")) - .expect("stop approval should be built"); - assert_eq!(approval.pending_title, "Stop local model server"); - assert_eq!(approval.command_title, "Services"); - assert_eq!( - approval.args, - vec![ - "services".to_owned(), - "stop".to_owned(), - "svc-qwen".to_owned(), - "--yes".to_owned(), - ] - ); - assert_eq!( - approval.explanation.as_deref(), - Some("This server is using memory we need.") - ); - - let enable = providers::ChatToolCall { - id: Some("call-watch".to_owned()), - name: "watcher_enable".to_owned(), - arguments: serde_json::json!({ - "watcher": "server-recover", - "mode": "propose" - }), - }; - validate_chat_tool_call(&enable).expect("watcher enable should validate for review"); - assert_eq!( - rocm_chat_tool_requested_command(&enable).as_deref(), - Some("rocm automations enable server-recover --mode propose") - ); - let approval = - chat_tool_approval_request(&enable, Some("Recovering failed servers would help.")) - .expect("watcher approval should be built"); - assert_eq!(approval.pending_title, "Enable automation"); - assert_eq!(approval.command_title, "Automations"); - assert_eq!( - approval.args, - vec![ - "automations".to_owned(), - "enable".to_owned(), - "server-recover".to_owned(), - "--mode".to_owned(), - "propose".to_owned(), - ] - ); - - let disable = providers::ChatToolCall { - id: Some("call-disable".to_owned()), - name: "watcher_disable".to_owned(), - arguments: serde_json::json!({ "watcher": "server-recover" }), - }; - validate_chat_tool_call(&disable).expect("watcher disable should validate for review"); - assert_eq!( - rocm_chat_tool_requested_command(&disable).as_deref(), - Some("rocm automations disable server-recover") - ); - } - - #[test] - fn proposal_action_rejects_over_long_proposal_id() { - let call = providers::ChatToolCall { - id: None, - name: "proposal_action".to_owned(), - arguments: serde_json::json!({ - "proposal_id": "p".repeat(129), - "action": "show" - }), - }; - let err = validate_chat_proposal_action_tool_call(&call) - .expect_err("over-long proposal_id must be rejected"); - assert!(err.to_string().contains("proposal_id too long")); - } - - #[test] - fn chat_tool_call_accepts_expanded_read_only_bridge_tools() { - for call in [ - providers::ChatToolCall { - id: None, - name: "bridge_snapshot".to_owned(), - arguments: serde_json::json!({}), - }, - providers::ChatToolCall { - id: None, - name: "service_logs".to_owned(), - arguments: serde_json::json!({ - "service_id": "svc-qwen", - "lines": 120 - }), - }, - providers::ChatToolCall { - id: None, - name: "automations".to_owned(), - arguments: serde_json::json!({ "event_limit": 12 }), - }, - providers::ChatToolCall { - id: None, - name: "natural_language_plan".to_owned(), - arguments: serde_json::json!({ "request": "check whether ROCm needs an update" }), - }, - providers::ChatToolCall { - id: None, - name: "port_status".to_owned(), - arguments: serde_json::json!({ "host": "127.0.0.1", "port": 8188 }), - }, - providers::ChatToolCall { - id: None, - name: "update_check".to_owned(), - arguments: serde_json::json!({}), - }, - ] { - validate_chat_tool_call(&call).expect("read-only bridge tool should validate"); - assert!( - chat_tool_call_is_read_only(&call), - "{} should be read-only", - call.name - ); - } + let rendered = render_services_tool_result_text(&[record]); + + assert!(rendered.contains("status_meaning: ready/running = running")); + assert!(rendered.contains("engine=vllm")); + assert!(rendered.contains("running_state=running")); } #[test] @@ -5308,217 +3578,6 @@ model recipes )); } - #[test] - fn chat_tool_call_accepts_assistant_support_command_shapes() { - for (call, expected_command, read_only) in [ - ( - providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ "args": ["examine"] }), - }, - Some("rocm examine"), - true, - ), - ( - providers::ChatToolCall { - id: None, - name: "gpu_snapshot".to_owned(), - arguments: serde_json::json!({}), - }, - None, - true, - ), - ( - providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ "args": ["model"] }), - }, - Some("rocm model"), - true, - ), - ( - providers::ChatToolCall { - id: None, - name: "install_sdk".to_owned(), - arguments: serde_json::json!({ - "channel": "release", - "format": "wheel", - "prefix": "D:\\ROCm\\therock_venvs" - }), - }, - Some( - "rocm install sdk --channel release --format wheel --approve-replacing-active-default --prefix 'D:\\ROCm\\therock_venvs'", - ), - false, - ), - ( - providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ "args": ["comfyui", "install"] }), - }, - Some("rocm comfyui install --yes"), - false, - ), - ( - providers::ChatToolCall { - id: None, - name: "launch_server".to_owned(), - arguments: serde_json::json!({ - "model": "qwen", - "engine": "vllm", - "device": "gpu_required" - }), - }, - Some("rocm serve qwen --managed --engine vllm --device gpu_required"), - false, - ), - ] { - validate_chat_tool_call(&call).expect("assistant support tool should validate"); - assert_eq!( - chat_tool_call_is_read_only(&call), - read_only, - "{}", - call.name - ); - if let Some(expected_command) = expected_command { - assert_eq!( - rocm_chat_tool_requested_command(&call).as_deref(), - Some(expected_command) - ); - } - } - } - - #[test] - fn chat_rocm_command_routes_comfyui_and_engine_actions() { - let comfy_install = providers::ChatToolCall { - id: Some("call-comfy".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["comfyui", "install"], - "reason": "The user asked me to install ComfyUI." - }), - }; - validate_chat_tool_call(&comfy_install).expect("ComfyUI install should validate"); - assert!(!chat_tool_call_is_read_only(&comfy_install)); - assert_eq!( - rocm_chat_tool_requested_command(&comfy_install).as_deref(), - Some("rocm comfyui install --yes") - ); - let approval = chat_tool_approval_request(&comfy_install, Some("Install ComfyUI now.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Install ComfyUI"); - assert_eq!(approval.command_title, "ComfyUI"); - assert_eq!( - approval.args, - vec![ - "comfyui".to_owned(), - "install".to_owned(), - "--yes".to_owned() - ] - ); - - let lemonade = providers::ChatToolCall { - id: Some("call-lemonade".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["engines", "install", "lemonade"] - }), - }; - validate_chat_tool_call(&lemonade).expect("lemonade engine install should validate"); - assert!(!chat_tool_call_is_read_only(&lemonade)); - assert_eq!( - rocm_chat_tool_requested_command(&lemonade).as_deref(), - Some("rocm engines install lemonade") - ); - let approval = - chat_tool_approval_request(&lemonade, Some("Install Lemonade for local serving.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Install engine"); - assert_eq!(approval.command_title, "Engine"); - - let vllm = providers::ChatToolCall { - id: Some("call-vllm".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["engines", "install", "vllm"] - }), - }; - validate_chat_tool_call(&vllm).expect("vLLM engine install should validate"); - assert!(!chat_tool_call_is_read_only(&vllm)); - assert_eq!( - rocm_chat_tool_requested_command(&vllm).as_deref(), - Some("rocm engines install vllm") - ); - let approval = chat_tool_approval_request(&vllm, Some("Install vLLM for Linux/WSL.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Install engine"); - assert_eq!(approval.command_title, "Engine"); - - let comfy_start = providers::ChatToolCall { - id: Some("call-comfy-start".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["comfyui", "start"] - }), - }; - validate_chat_tool_call(&comfy_start).expect("ComfyUI start should validate"); - assert!(!chat_tool_call_is_read_only(&comfy_start)); - let approval = chat_tool_approval_request(&comfy_start, Some("Start ComfyUI locally.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Start ComfyUI"); - assert_eq!(approval.command_title, "ComfyUI"); - - let serve = providers::ChatToolCall { - id: Some("call-serve".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["serve", "qwen", "--engine", "vllm", "--device", "gpu_required", "--managed"] - }), - }; - validate_chat_tool_call(&serve).expect("managed serve should validate"); - assert!(!chat_tool_call_is_read_only(&serve)); - assert_eq!( - rocm_chat_tool_requested_command(&serve).as_deref(), - Some("rocm serve qwen --engine vllm --device gpu_required --managed") - ); - let approval = chat_tool_approval_request(&serve, Some("Start the recommended assistant.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Start local model server"); - assert_eq!(approval.command_title, "Serve"); - - let vllm_serve = providers::ChatToolCall { - id: Some("call-vllm-serve".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["serve", "Qwen/Qwen3.5-4B", "--engine", "vllm", "--device", "gpu_required", "--managed"] - }), - }; - validate_chat_tool_call(&vllm_serve).expect("managed vLLM serve should validate"); - assert!(!chat_tool_call_is_read_only(&vllm_serve)); - assert_eq!( - rocm_chat_tool_requested_command(&vllm_serve).as_deref(), - Some("rocm serve Qwen/Qwen3.5-4B --engine vllm --device gpu_required --managed") - ); - - let config = providers::ChatToolCall { - id: Some("call-config".to_owned()), - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["config", "set-default-engine", "vllm"] - }), - }; - validate_chat_tool_call(&config).expect("config change should validate"); - assert!(!chat_tool_call_is_read_only(&config)); - let approval = chat_tool_approval_request(&config, Some("Use vLLM as the default engine.")) - .expect("approval should be built"); - assert_eq!(approval.pending_title, "Change settings"); - assert_eq!(approval.command_title, "Config"); - } - #[test] fn default_engine_fallback_matches_aim_only_as_a_word() { let proposed = |prompt: &str| { @@ -5541,131 +3600,7 @@ model recipes proposed( "change the default engine so I can maintain my claim, aiming for low latency" ), - None - ); - } - - #[test] - fn setup_status_is_read_only() { - for args in [ - vec!["setup".to_owned()], - vec!["setup".to_owned(), "status".to_owned()], - ] { - let action = - chat_rocm_command_action_from_args(args.clone()).expect("setup status classifies"); - assert!( - matches!(action, ChatRocmCommandAction::ReadOnly(_)), - "setup {args:?} should be read-only, got {action:?}" - ); - } - } - - #[test] - fn storage_report_is_read_only_and_removal_requires_approval() { - for args in [ - vec!["storage".to_owned()], - vec!["storage".to_owned(), "report".to_owned()], - ] { - let action = chat_rocm_command_action_from_args(args.clone()) - .expect("storage report classifies"); - assert!( - matches!(action, ChatRocmCommandAction::ReadOnly(_)), - "storage {args:?} only measures folders, so it should be read-only, got {action:?}" - ); - } - - for verb in ["remove-old-installs", "remove-downloads"] { - let action = - chat_rocm_command_action_from_args(vec!["storage".to_owned(), verb.to_owned()]) - .expect("storage removal classifies"); - match action { - ChatRocmCommandAction::Approval { args, .. } => { - assert!( - args.iter().any(|arg| arg == "--yes"), - "approved removal runs non-interactively: {args:?}" - ); - } - other @ ChatRocmCommandAction::ReadOnly(_) => { - panic!("storage {verb} must require approval, got {other:?}") - } - } - } - } - - #[test] - fn setup_reset_requires_approval() { - let action = - chat_rocm_command_action_from_args(vec!["setup".to_owned(), "reset".to_owned()]) - .expect("setup reset classifies"); - match action { - ChatRocmCommandAction::Approval { - pending_title, - command_title, - .. - } => { - assert_eq!(pending_title, "Reset first-time setup"); - assert_eq!(command_title, "Setup"); - } - other @ ChatRocmCommandAction::ReadOnly(_) => { - panic!("setup reset should require approval, got {other:?}") - } - } - } - - #[test] - fn proposal_action_show_is_read_only() { - let call = providers::ChatToolCall { - id: None, - name: "proposal_action".to_owned(), - arguments: serde_json::json!({ "proposal_id": "p1", "action": "show" }), - }; - validate_chat_tool_call(&call).expect("show validates"); - assert!( - chat_tool_call_is_read_only(&call), - "proposal_action show must be read-only" - ); - } - - #[test] - fn proposal_action_approve_requires_approval() { - for action in ["approve", "reject"] { - let call = providers::ChatToolCall { - id: None, - name: "proposal_action".to_owned(), - arguments: serde_json::json!({ "proposal_id": "p1", "action": action }), - }; - validate_chat_tool_call(&call).expect("approve/reject validates"); - assert!( - !chat_tool_call_is_read_only(&call), - "proposal_action {action} must NOT be read-only" - ); - let req = chat_tool_approval_request(&call, None).unwrap_or_else(|err| { - panic!("proposal_action {action} should need approval: {err}") - }); - assert_eq!(req.command_title, "Reviews"); - assert!( - req.pending_title.contains("proposal") || req.pending_title.contains("Proposal") - ); - assert!( - req.display_command - .as_deref() - .unwrap_or_default() - .contains("p1"), - "display command should show the proposal id" - ); - } - } - - #[test] - fn proposal_action_rejects_unknown_action() { - let call = providers::ChatToolCall { - id: None, - name: "proposal_action".to_owned(), - arguments: serde_json::json!({ "proposal_id": "p1", "action": "delete" }), - }; - assert!( - validate_chat_tool_call(&call).is_err(), - "unknown proposal_action `action` must be rejected" + None ); } @@ -5775,301 +3710,6 @@ model recipes let _ = fs::remove_dir_all(&root); } - #[test] - fn config_set_permissions_classifies_as_approval() { - // Permission escalation MUST route through approval — the classifier - // routes any `config ` (catch-all) to Approval. Verify both modes. - for mode in ["full_access", "ask"] { - let action = chat_rocm_command_action_from_args(vec![ - "config".to_owned(), - "set-permissions".to_owned(), - mode.to_owned(), - ]) - .expect("config set-permissions classifies"); - match action { - ChatRocmCommandAction::Approval { command_title, .. } => { - assert_eq!(command_title, "Config"); - } - other @ ChatRocmCommandAction::ReadOnly(_) => { - panic!("config set-permissions {mode} must need approval, got {other:?}") - } - } - } - } - - #[test] - fn watcher_validator_rejects_unknown_and_invalid_mode() { - // Unknown watcher id → rejected. - let unknown = providers::ChatToolCall { - id: None, - name: "watcher_enable".to_owned(), - arguments: serde_json::json!({ "watcher": "no-such-watcher" }), - }; - assert!( - validate_chat_watcher_tool_call(&unknown, true).is_err(), - "unknown watcher must be rejected" - ); - // Invalid mode → rejected. - let bad_mode = providers::ChatToolCall { - id: None, - name: "watcher_enable".to_owned(), - arguments: serde_json::json!({ "watcher": "therock-update", "mode": "rampage" }), - }; - assert!( - validate_chat_watcher_tool_call(&bad_mode, true).is_err(), - "invalid watcher mode must be rejected" - ); - // Valid watcher + valid mode → accepted. - let ok = providers::ChatToolCall { - id: None, - name: "watcher_enable".to_owned(), - arguments: serde_json::json!({ "watcher": "therock-update", "mode": "observe" }), - }; - validate_chat_watcher_tool_call(&ok, true).expect("valid watcher+mode accepted"); - // Disable must reject a `mode`. - let disable_with_mode = providers::ChatToolCall { - id: None, - name: "watcher_disable".to_owned(), - arguments: serde_json::json!({ "watcher": "therock-update", "mode": "observe" }), - }; - assert!( - validate_chat_watcher_tool_call(&disable_with_mode, false).is_err(), - "disable must reject a mode argument" - ); - } - - #[test] - fn lifecycle_read_mutate_split_is_honest() { - let read_only = [ - vec!["update".to_owned()], - vec!["comfyui".to_owned(), "status".to_owned()], - vec!["comfyui".to_owned(), "logs".to_owned()], - vec!["uninstall".to_owned(), "--dry-run".to_owned()], - vec!["setup".to_owned(), "status".to_owned()], - vec![ - "runtimes".to_owned(), - "uninstall".to_owned(), - "old-runtime".to_owned(), - "--dry-run".to_owned(), - ], - vec![ - "runtimes".to_owned(), - "remove".to_owned(), - "old-runtime".to_owned(), - "--dry-run".to_owned(), - ], - // Must agree with the daemon's `ensure_rocm_command_is_read_only`. - // Its comment claims the two are mirrored, and nothing enforced - // that — the arm was added there and not here, and the classifiers - // disagreed until a reviewer noticed. - vec!["remote".to_owned(), "targets".to_owned()], - vec![ - "remote".to_owned(), - "doctor".to_owned(), - "gpu-box".to_owned(), - ], - vec!["remote".to_owned(), "status".to_owned()], - ]; - for args in read_only { - let action = chat_rocm_command_action_from_args(args.clone()) - .unwrap_or_else(|err| panic!("{args:?} should classify: {err}")); - assert!( - matches!(action, ChatRocmCommandAction::ReadOnly(_)), - "{args:?} should be read-only, got {action:?}" - ); - } - - // Paired with whether the command has a `--yes` to inject. The flag - // exists to keep a consent prompt from hanging a null-stdin spawn, so - // the demand only makes sense for commands that would prompt — and - // injecting it where clap defines no such flag would make the spawn fail - // to parse rather than succeed unattended. - let mutating = [ - (vec!["update".to_owned(), "--apply".to_owned()], true), - // These start, publish or tear down on another machine. They take no - // `--yes` because they never prompt: consent is the approval step - // itself, and every destructive choice they make is already settled - // by an explicit flag (`remote stop --force`). Give any of them an - // interactive prompt and it needs a consent flag here too. - ( - vec![ - "remote".to_owned(), - "serve".to_owned(), - "gpu-box".to_owned(), - "m".to_owned(), - ], - false, - ), - ( - vec!["remote".to_owned(), "attach".to_owned(), "sess".to_owned()], - false, - ), - ( - vec!["remote".to_owned(), "stop".to_owned(), "sess".to_owned()], - false, - ), - (vec!["comfyui".to_owned(), "install".to_owned()], true), - (vec!["comfyui".to_owned(), "start".to_owned()], true), - (vec!["comfyui".to_owned(), "stop".to_owned()], true), - (vec!["uninstall".to_owned()], true), - (vec!["setup".to_owned(), "reset".to_owned()], true), - ( - vec![ - "runtimes".to_owned(), - "uninstall".to_owned(), - "old-runtime".to_owned(), - ], - true, - ), - ( - vec![ - "runtimes".to_owned(), - "remove".to_owned(), - "old-runtime".to_owned(), - ], - true, - ), - ]; - for (args, expects_yes) in mutating { - let action = chat_rocm_command_action_from_args(args.clone()) - .unwrap_or_else(|err| panic!("{args:?} should classify: {err}")); - match &action { - ChatRocmCommandAction::Approval { args, .. } => { - assert_eq!( - args.iter().any(|arg| arg == "--yes"), - expects_yes, - "{args:?} disagrees with whether the approval path should \ - carry --yes" - ); - } - other @ ChatRocmCommandAction::ReadOnly(_) => { - panic!("{args:?} should require approval, got {other:?}") - } - } - } - } - - #[test] - fn install_sdk_chat_and_mcp_args_approve_the_replacement_for_a_non_interactive_spawn() { - // The chat/MCP surfaces spawn `rocm` with null stdin, so the consent - // prompt would refuse with "re-run with - // `--approve-replacing-active-default`" — a flag the user has no way to - // supply from chat or the dashboard. Both the chat classifier - // arm and the MCP tool-args builder must inject the consent so an - // install over the active default runtime is not silently refused. - let action = chat_rocm_command_action_from_args(vec![ - "install".to_owned(), - "sdk".to_owned(), - "--channel".to_owned(), - "release".to_owned(), - // The classifier requires a user-chosen (non-system) install folder. - "--prefix".to_owned(), - "/home/tester/rocm-managed".to_owned(), - ]) - .expect("install sdk classifies"); - let chat_args = match action { - ChatRocmCommandAction::Approval { args, .. } => args, - other @ ChatRocmCommandAction::ReadOnly(_) => { - panic!("install sdk must require approval, got {other:?}") - } - }; - - // The MCP `install_sdk` tool builds its own argv (it does not route - // through the classifier above), so it must add the flag independently. - let call = providers::ChatToolCall { - id: None, - name: "install_sdk".to_owned(), - arguments: serde_json::json!({ "channel": "release", "format": "wheel" }), - }; - let mcp_args = rocm_chat_tool_requested_args(&call).expect("install_sdk tool builds args"); - - for args in [&chat_args, &mcp_args] { - assert_eq!( - consents_for_install_sdk_argv(args).replace_active_default, - therock::SdkInstallConsent::Preapproved( - therock::SdkInstallApprovalSource::ApproveReplacingActiveDefault - ), - "the spawn must approve replacing the active default, and be credited \ - to the flag it actually passed rather than to --yes, got {args:?}" - ); - } - } - - #[test] - fn chat_rocm_command_runs_read_only_and_rejects_risky_shapes() { - let status = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["rocm", "comfy", "status"] - }), - }; - validate_chat_tool_call(&status).expect("ComfyUI status should validate"); - assert!(chat_tool_call_is_read_only(&status)); - assert_eq!( - rocm_chat_tool_requested_command(&status).as_deref(), - Some("rocm comfyui status") - ); - - let logs = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["comfyui", "logs"] - }), - }; - validate_chat_tool_call(&logs).expect("ComfyUI logs should validate"); - assert!(chat_tool_call_is_read_only(&logs)); - assert_eq!( - rocm_chat_tool_requested_command(&logs).as_deref(), - Some("rocm comfyui logs") - ); - - let cpu = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["serve", "tiny.gguf", "--engine", "vllm", "--device", "cpu"] - }), - }; - let error = validate_chat_tool_call(&cpu).unwrap_err().to_string(); - assert!(error.contains("CPU execution")); - - let public_flag = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["serve", "tiny.gguf", "--engine", "vllm", "--allow-public-bind", "--managed"] - }), - }; - let error = validate_chat_tool_call(&public_flag) - .unwrap_err() - .to_string(); - assert!(error.contains("public network binding")); - - let foreground = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["serve", "qwen", "--engine", "vllm", "--foreground"] - }), - }; - let error = validate_chat_tool_call(&foreground) - .unwrap_err() - .to_string(); - assert!(error.contains("--managed")); - - let shell = providers::ChatToolCall { - id: None, - name: "rocm_command".to_owned(), - arguments: serde_json::json!({ - "args": ["powershell", "-Command", "whoami"] - }), - }; - let error = validate_chat_tool_call(&shell).unwrap_err().to_string(); - assert!(error.contains("unsupported rocm command")); - } - #[test] fn assistant_read_only_rocm_commands_do_not_fallback_to_child_process() { let (_root, paths) = test_paths("readonly-rocm-in-process-only"); @@ -6233,48 +3873,6 @@ model recipes ); } - /// The `install driver` chat action strips `--yes` in the attached form too, - /// like the `install sdk` arm: read-only where the command only reports, and - /// a single bare `--yes` where it installs. - #[test] - fn chat_install_driver_strips_a_model_supplied_yes_in_both_forms() { - for supplied in ["--yes", "--yes=true", "--yes=1", "--yes=false"] { - let argv = |extra: &str| -> Vec { - ["install", "driver", "--dry-run", extra] - .iter() - .map(|arg| (*arg).to_owned()) - .collect() - }; - let ChatRocmCommandAction::ReadOnly(args) = - chat_install_driver_action(argv(supplied), true) - else { - panic!("a report-only platform must stay read-only"); - }; - assert_eq!(args, ["install", "driver", "--dry-run"], "{supplied}"); - - let ChatRocmCommandAction::Approval { args, .. } = - chat_install_driver_action(argv(supplied), false) - else { - panic!("an installing platform must ask approval"); - }; - assert_eq!( - args, - ["install", "driver", "--dry-run", "--yes"], - "{supplied}" - ); - } - let ChatRocmCommandAction::ReadOnly(args) = chat_install_driver_action( - ["install", "driver", "--yes-not-a-flag"] - .iter() - .map(|arg| (*arg).to_owned()) - .collect(), - true, - ) else { - panic!("read-only expected"); - }; - assert!(args.iter().any(|arg| arg == "--yes-not-a-flag"), "{args:?}"); - } - #[test] fn setup_status_renders_in_process() { let (_root, paths) = test_paths("setup-status-in-process"); @@ -6296,170 +3894,6 @@ model recipes } } - #[test] - fn chat_tool_call_rejects_bad_service_and_watcher_suggestions() { - for (call, expected) in [ - ( - providers::ChatToolCall { - id: None, - name: "service_logs".to_owned(), - arguments: serde_json::json!({ "service_id": "bad/name" }), - }, - "must not contain path separators", - ), - ( - providers::ChatToolCall { - id: None, - name: "automations".to_owned(), - arguments: serde_json::json!({ "event_limit": 1000 }), - }, - "between 1 and 64", - ), - ( - providers::ChatToolCall { - id: None, - name: "watcher_enable".to_owned(), - arguments: serde_json::json!({ - "watcher": "unknown", - "mode": "propose" - }), - }, - "unknown watcher", - ), - ( - providers::ChatToolCall { - id: None, - name: "watcher_disable".to_owned(), - arguments: serde_json::json!({ - "watcher": "server-recover", - "mode": "propose" - }), - }, - "cannot set `mode`", - ), - ] { - let error = validate_chat_tool_call(&call).unwrap_err().to_string(); - assert!(error.contains(expected), "unexpected error: {error}"); - } - } - - #[test] - fn chat_tool_call_rejects_cpu_and_public_bind_server_requests() { - let cpu = providers::ChatToolCall { - id: None, - name: "launch_server".to_owned(), - arguments: serde_json::json!({ - "model": "tiny.gguf", - "engine": "vllm", - "device": "cpu" - }), - }; - let error = validate_chat_tool_call(&cpu).unwrap_err().to_string(); - assert!(error.contains("CPU execution")); - - let public = providers::ChatToolCall { - id: None, - name: "launch_server".to_owned(), - arguments: serde_json::json!({ - "model": "tiny.gguf", - "engine": "vllm", - "host": "0.0.0.0", - "allow_public_bind": true - }), - }; - let error = validate_chat_tool_call(&public).unwrap_err().to_string(); - assert!(error.contains("public network binding")); - - let host = providers::ChatToolCall { - id: None, - name: "launch_server".to_owned(), - arguments: serde_json::json!({ - "model": "tiny.gguf", - "engine": "vllm", - "host": "0.0.0.0" - }), - }; - let error = validate_chat_tool_call(&host).unwrap_err().to_string(); - assert!(error.contains("non-local host")); - } - - #[test] - fn chat_tool_call_rejects_bad_install_suggestions() { - for (arguments, expected) in [ - ( - serde_json::json!({ "channel": "stable" }), - "unsupported TheRock channel", - ), - ( - serde_json::json!({ "format": "zip" }), - "unsupported TheRock install format", - ), - ( - serde_json::json!({ - "prefix": if cfg!(windows) { "C:\\Windows\\rocm" } else { "/opt/rocm" } - }), - "system install folder", - ), - ( - serde_json::json!({ - "version": "7.13.0a20260605", - "build_date": "2026-06-05" - }), - "both `version` and `build_date`", - ), - ( - serde_json::json!({ "build_date": "not-a-date" }), - "build date", - ), - ( - serde_json::json!({ - "format": "tarball", - "build_date": "2026-06-05" - }), - if cfg!(windows) { - "tarball` installs on Windows" - } else { - "specific TheRock wheel versions" - }, - ), - ] { - let call = providers::ChatToolCall { - id: None, - name: "install_sdk".to_owned(), - arguments, - }; - let error = validate_chat_tool_call(&call).unwrap_err().to_string(); - assert!(error.contains(expected), "unexpected error: {error}"); - } - } - - /// Guard: every read-only tool the dash side registers must have a home in - /// the bin's accept-list. A name-only tool (the `doctor` regression caught - /// in review) would otherwise fail end-to-end as "unsupported ROCm tool" - /// while passing the dash-side, name-against-itself completeness checks. - /// - /// We assert the NAME is accepted — not that empty args validate — so the - /// check stays hermetic (no real `BinToolExecutor::execute` / live I/O) and - /// independent of each tool's argument schema. - #[test] - fn read_tool_names_are_subset_of_bin_accept_list() { - for name in rocm_dash_tui::agent::ROCM_READ_TOOL_NAMES { - let call = providers::ChatToolCall { - id: None, - name: name.to_owned(), - arguments: serde_json::json!({}), - }; - if let Err(error) = validate_chat_tool_call(&call) { - let message = error.to_string(); - assert!( - !message.contains("unsupported ROCm tool"), - "ROCM_READ_TOOL_NAMES advertises `{name}` but the bin rejects \ - the name as unsupported: {message}" - ); - } - } - } - #[test] fn chat_tool_call_refusal_prioritizes_safe_review_wording() -> Result<()> { let mut output = String::new(); @@ -6906,38 +4340,6 @@ install therock"; assert!(!chat_tool_call_is_read_only(&call)); } - #[test] - fn path_exists_chat_tool_is_read_only() { - let call = providers::ChatToolCall { - id: Some("path-check".to_owned()), - name: "path_exists".to_owned(), - arguments: serde_json::json!({ "path": "D:\\ROCm\\temp" }), - }; - - validate_chat_tool_call(&call).unwrap(); - assert!(chat_tool_call_is_read_only(&call)); - } - - #[test] - fn port_status_chat_tool_is_read_only_and_loopback_only() { - let call = providers::ChatToolCall { - id: Some("port-check".to_owned()), - name: "port_status".to_owned(), - arguments: serde_json::json!({ "host": "127.0.0.1", "port": 8188 }), - }; - - validate_chat_tool_call(&call).unwrap(); - assert!(chat_tool_call_is_read_only(&call)); - - let public = providers::ChatToolCall { - id: Some("public-port-check".to_owned()), - name: "port_status".to_owned(), - arguments: serde_json::json!({ "host": "192.168.1.10", "port": 8188 }), - }; - let error = validate_chat_tool_call(&public).unwrap_err().to_string(); - assert!(error.contains("non-local host"), "{error}"); - } - #[test] fn port_status_matches_loopback_managed_services() -> Result<()> { let (root, paths) = test_paths("port-status-loopback"); @@ -7264,35 +4666,6 @@ install therock"; assert!(!error.contains("OpenAI-compatible provider")); } - /// The same question — "is this a system location?" — also keeps the local - /// assistant from installing into one, and there the folder arrives as - /// free-form JSON from a model rather than from a file the user wrote. A - /// trailing separator is exactly the sort of thing generated text carries, - /// so the refusal has to survive every spelling here too. - #[test] - #[cfg(unix)] - fn the_assistant_cannot_name_a_system_install_folder_by_respelling_it() { - let home = rocm_core::runtime_home_dir().expect("a home directory"); - let home = home.display().to_string(); - let spellings = [ - "/usr/".to_owned(), - "/opt/".to_owned(), - "//usr".to_owned(), - "/./opt".to_owned(), - format!("{home}/../../usr"), - ]; - - let accepted: Vec = spellings - .into_iter() - .filter(|text| !chat_install_prefix_is_system(Path::new(text))) - .collect(); - - assert!( - accepted.is_empty(), - "accepted as a user install folder: {accepted:?}" - ); - } - #[test] fn piped_prompt_keeps_everything_but_the_trailing_line_ending() { // A piped prompt must reach the send path byte-identical to the same @@ -7368,6 +4741,7 @@ mod command_preview_properties { }; // The one generator, shared with `shell_quote`'s own suite so both are // covered by its reach guard. See `crate::shell_quote::hostile`. + use crate::shell_quote::has_terminal_control_character; use crate::shell_quote::hostile::{arg as hostile_arg, argv as hostile_argv}; use proptest::prelude::*; diff --git a/apps/rocm/src/driver_install.rs b/apps/rocm/src/driver_install.rs index 5b72cf7728..d2f4a281c8 100644 --- a/apps/rocm/src/driver_install.rs +++ b/apps/rocm/src/driver_install.rs @@ -19,7 +19,7 @@ //! The plan this module runs (its model, rendering, per-distro builders and the //! WSL rocDXG plan) lives in `driver_plan.rs`, which does not depend on this //! module; the host's os-release comes from `system_packages.rs`. This module -//! re-exports `host_install_driver_only_reports` for `chat.rs` and +//! re-exports `host_install_driver_only_reports` for `tool_policy.rs` and //! `freeform_planner.rs`. use std::fmt::Write as _; diff --git a/apps/rocm/src/freeform_planner.rs b/apps/rocm/src/freeform_planner.rs index 4a440a68d1..42f4593052 100644 --- a/apps/rocm/src/freeform_planner.rs +++ b/apps/rocm/src/freeform_planner.rs @@ -43,9 +43,14 @@ use rocm_core::{ use serde::Deserialize; use std::fmt::Write as _; +// `format_structured_tool_call` lives in `rocm_argv.rs` and stays reachable here +// as `freeform_planner::format_structured_tool_call` (read by `comfyui.rs`, +// `freeform_execution.rs` and `providers.rs`'s tests). +pub(crate) use crate::rocm_argv::format_structured_tool_call; +use crate::rocm_argv::serve_args_request_cpu_device; use crate::service_launch::is_loopback_host; -use crate::shell_quote::{has_terminal_control_character, shell_quote}; -use crate::{Cli, chat, providers}; +use crate::shell_quote::has_terminal_control_character; +use crate::{Cli, prompt_text, providers, rocm_argv, tool_policy}; /// Builds the freeform plan once and renders it. Kept for the render-only test /// below; production code calls [`plan_freeform_request_with_context`] @@ -368,7 +373,7 @@ fn build_freeform_plan_on( .map(|recipe| recipe.canonical_model_id.as_str()) .or(requested_model) .unwrap_or(""); - let engine = infer_engine_from_request(&lower) + let engine = prompt_text::infer_engine_from_request(&lower) .or_else(|| resolved_recipe.as_ref().and_then(planner_recipe_engine)) .unwrap_or(default_engine); let device_policy = infer_device_policy_from_request(&lower).or_else(|| { @@ -376,7 +381,7 @@ fn build_freeform_plan_on( .as_ref() .map(|recipe| recipe.device_policy.as_str()) }); - if device_policy.is_some_and(device_policy_is_cpu_mode) { + if device_policy.is_some_and(rocm_argv::device_policy_is_cpu_mode) { let (parsed, confidence) = serve_plan_parsed_fields_and_confidence( model, engine, @@ -456,7 +461,7 @@ fn build_freeform_plan_on( } if planner_mentions_comfyui(&lower) { - if chat::any_substring(&lower, &["log", "logs"]) { + if prompt_text::any_substring(&lower, &["log", "logs"]) { return StructuredRequestPlan::deterministic( trimmed, PlannerIntent::Inspect, @@ -471,7 +476,7 @@ fn build_freeform_plan_on( Vec::new(), ); } - if chat::any_substring(&lower, &["start", "run", "launch", "open"]) { + if prompt_text::any_substring(&lower, &["start", "run", "launch", "open"]) { return StructuredRequestPlan::deterministic( trimmed, PlannerIntent::Serve, @@ -522,8 +527,8 @@ fn build_freeform_plan_on( } else { "release" }; - let build_date = chat::requested_therock_build_date_from_prompt(&lower); - let Some(prefix) = chat::requested_install_prefix_from_prompt(trimmed) else { + let build_date = prompt_text::requested_therock_build_date_from_prompt(&lower); + let Some(prefix) = prompt_text::requested_install_prefix_from_prompt(trimmed) else { let mut parsed = vec![ ("channel".to_owned(), channel.to_owned()), ("format".to_owned(), "wheel".to_owned()), @@ -719,31 +724,6 @@ fn planned_device_policy_without_fallback(policy: &str) -> &str { } } -fn device_policy_is_cpu_mode(policy: &str) -> bool { - matches!( - policy.trim().to_ascii_lowercase().as_str(), - "cpu" | "cpu_only" - ) -} - -pub(crate) fn serve_args_request_cpu_device(args: &[String]) -> bool { - let mut iter = args.iter().peekable(); - while let Some(arg) = iter.next() { - if let Some(value) = arg.strip_prefix("--device=") - && device_policy_is_cpu_mode(value) - { - return true; - } - if arg == "--device" - && let Some(value) = iter.peek() - && device_policy_is_cpu_mode(value) - { - return true; - } - } - false -} - fn planner_resolve_model_recipe( model_ref: &str, recipes: Option<&[ModelRecipeRecord]>, @@ -1031,12 +1011,12 @@ pub(crate) fn validate_provider_planner_tool_call( [command] if command == "examine" => {} [command, subcommand] if command == "engines" && subcommand == "list" => {} [command, subcommand, ..] if command == "install" && subcommand == "sdk" => { - chat::validate_chat_rocm_command_safety(&call.args)?; + tool_policy::validate_chat_rocm_command_safety(&call.args)?; } [command, subcommand, ..] if command == "install" && subcommand == "driver" => {} [command, ..] if command == "update" => {} [command, ..] if command == "serve" => { - if chat::chat_cli_has_flag(&call.args, "--allow-public-bind") { + if rocm_argv::chat_cli_has_flag(&call.args, "--allow-public-bind") { bail!("provider planner cannot request public network binding"); } if serve_args_request_cpu_device(&call.args) { @@ -1044,13 +1024,13 @@ pub(crate) fn validate_provider_planner_tool_call( "provider planner cannot request CPU execution; rocm-cli requires ROCm GPU execution" ); } - if let Some(host) = chat::chat_cli_arg_value(&call.args, "--host") + if let Some(host) = rocm_argv::chat_cli_arg_value(&call.args, "--host") && !is_loopback_host(host) { bail!("provider planner cannot request non-local host `{host}`"); } - if chat::chat_cli_has_flag(&call.args, "--foreground") - || !chat::chat_cli_has_flag(&call.args, "--managed") + if rocm_argv::chat_cli_has_flag(&call.args, "--foreground") + || !rocm_argv::chat_cli_has_flag(&call.args, "--managed") { bail!("provider planner must request managed serving with --managed"); } @@ -1235,11 +1215,11 @@ fn planner_is_inspect_request(lower: &str) -> bool { } fn planner_mentions_comfyui(lower: &str) -> bool { - chat::any_substring(lower, &["comfyui", "comfy ui", "comfy"]) + prompt_text::any_substring(lower, &["comfyui", "comfy ui", "comfy"]) } fn planner_requests_comfyui_install(lower: &str) -> bool { - chat::any_substring( + prompt_text::any_substring( lower, &[ "can you setup", @@ -1259,19 +1239,6 @@ fn contains_planner_word(text: &str, expected: &str) -> bool { .any(|word| word == expected) } -pub(crate) fn infer_engine_from_request(lower: &str) -> Option<&'static str> { - // Word-boundary match, not substring: "aim" as a bare `.contains` also - // matches "aiming"/"claim"/"maintain", silently steering an unrelated - // chat request onto the Docker-backed AIM engine. - let words: std::collections::HashSet<&str> = lower - .split(|c: char| !c.is_ascii_alphanumeric()) - .filter(|word| !word.is_empty()) - .collect(); - ["lemonade", "vllm", "aim"] - .into_iter() - .find(|engine| words.contains(engine)) -} - fn infer_model_from_request(request: &str) -> Option<&str> { let trimmed = request.trim(); let lower = trimmed.to_ascii_lowercase(); @@ -1321,48 +1288,8 @@ fn infer_device_policy_from_request(lower: &str) -> Option<&'static str> { } } -/// Render an argv as the command line a human is shown for it. -/// -/// This is the only thing an operator sees before approving a mutating tool -/// call, and some of its output (`rocm chat --prompt …`, the `install command:` -/// line) is offered to be copied and run. So it has to denote the argv it was -/// built from: quoted with [`shell_quote`], every argument splits back to -/// itself under a shell's own word-splitting, and nothing inside a value can -/// read as a second command. -/// -/// Values carrying terminal control characters never get here — they are -/// refused at validation ([`has_terminal_control_character`]), because no -/// amount of quoting stops a `\r` from rewriting the line the operator is -/// reading. -pub(crate) fn format_structured_tool_call(tool: &str, args: &[String]) -> String { - let mut parts = Vec::with_capacity(args.len() + 1); - parts.push(tool.to_owned()); - parts.extend(args.iter().map(|arg| shell_quote(arg))); - parts.join(" ") -} - #[cfg(test)] mod tests { - use super::infer_engine_from_request; - - /// "aim" must match as its own word, not as a substring of an unrelated - /// word like "aiming"/"claim"/"maintain" in a free-text chat request. - #[test] - fn infer_engine_from_request_does_not_match_aim_as_a_substring() { - assert_eq!( - infer_engine_from_request("serve a small model, aiming for low latency"), - None - ); - assert_eq!( - infer_engine_from_request("please explain how to maintain a claim"), - None - ); - assert_eq!( - infer_engine_from_request("serve qwen3.5-9b with aim"), - Some("aim") - ); - } - use super::*; use crate::tests::test_paths; diff --git a/apps/rocm/src/main.rs b/apps/rocm/src/main.rs index ff417698f0..7020ecef4e 100644 --- a/apps/rocm/src/main.rs +++ b/apps/rocm/src/main.rs @@ -43,9 +43,11 @@ mod install_cmd; mod logging; mod managed_service_status; mod model_fit; +mod prompt_text; mod provider_keys; mod providers; mod remote; +mod rocm_argv; mod runtime_adopt; mod runtime_install_finalize; mod runtime_registry; @@ -66,6 +68,7 @@ mod system_packages; #[cfg(test)] mod test_support; mod therock; +mod tool_policy; mod uninstall; mod uninstall_plan; mod update_cmd; @@ -3200,7 +3203,7 @@ pub(crate) mod tests { // while every subcommand below it already said "runtime". The approval // modal raised by a mutating `rocm runtimes …` from chat says the same // noun too, but clap doesn't render that title — see - // `chat::tests::runtimes_activate_approval_modal_uses_the_runtime_noun`. + // `tool_policy::tests::runtimes_activate_approval_modal_uses_the_runtime_noun`. let help = Cli::command() .find_subcommand_mut("runtimes") .expect("runtimes subcommand") diff --git a/apps/rocm/src/prompt_text.rs b/apps/rocm/src/prompt_text.rs new file mode 100644 index 0000000000..7647c532f7 --- /dev/null +++ b/apps/rocm/src/prompt_text.rs @@ -0,0 +1,208 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Text extraction from a natural-language request, shared by `chat.rs`'s +//! prompt-intent heuristics (`fallback_rocm_tool_call_for_prompt`, +//! `fallback_config_tool_call_for_prompt`, `install_sdk_chat_approval_for_prompt` +//! and the `prompt_*` predicates) and the freeform planner: which of a list of +//! phrases a prompt contains (`any_substring`), which install folder, TheRock +//! build date or TheRock version it names +//! (`requested_install_prefix_from_prompt`, +//! `requested_therock_build_date_from_prompt`, +//! `requested_therock_version_from_prompt` and their private helpers), and +//! which serving engine it asks for (`infer_engine_from_request`). +//! +//! A leaf module: it depends only on `therock::RuntimeVersionSelector`, and +//! neither `chat.rs` nor `freeform_planner.rs` has to import the other for +//! these helpers. `tool_policy.rs` does not use it. They were relocated +//! verbatim from `chat.rs` and `freeform_planner.rs`; `chat.rs` re-exports +//! `any_substring`, which `freeform_answer.rs` still reaches as +//! `chat::any_substring`. + +use crate::therock; + +pub(crate) fn any_substring(haystack: &str, needles: &[&str]) -> bool { + needles.iter().any(|needle| haystack.contains(needle)) +} + +pub(crate) fn requested_install_prefix_from_prompt(prompt: &str) -> Option { + let lower = prompt.to_ascii_lowercase(); + for phrase in [ + "--prefix=", + "--prefix ", + " into folder ", + " into ", + " in folder ", + " in ", + " to folder ", + " to ", + " under ", + " at ", + " use folder ", + " use ", + "use ", + " folder is ", + " folder: ", + "folder:", + ] { + let mut search_start = 0; + while let Some(relative_index) = lower[search_start..].find(phrase) { + let value_start = search_start + relative_index + phrase.len(); + if let Some(prefix) = clean_requested_install_prefix(&prompt[value_start..]) { + return Some(prefix); + } + search_start = value_start; + } + } + requested_bare_install_prefix_from_prompt(prompt) +} + +fn requested_bare_install_prefix_from_prompt(prompt: &str) -> Option { + for quote in ['"', '\''] { + let mut remaining = prompt; + while let Some(start) = remaining.find(quote) { + let after_start = &remaining[start + quote.len_utf8()..]; + let Some(end) = after_start.find(quote) else { + break; + }; + if let Some(prefix) = clean_requested_install_prefix(&after_start[..end]) { + return Some(prefix); + } + remaining = &after_start[end + quote.len_utf8()..]; + } + } + prompt.split_whitespace().find_map(|token| { + clean_requested_install_prefix( + token.trim_matches(|ch: char| { + matches!(ch, '"' | '\'' | '`' | ',' | ';' | '.' | ')' | ']') + }), + ) + }) +} + +fn clean_requested_install_prefix(candidate: &str) -> Option { + let trimmed = candidate.trim_start(); + if trimmed.is_empty() { + return None; + } + let mut chars = trimmed.chars(); + let first = chars.next()?; + let extracted = if first == '"' || first == '\'' { + let close = trimmed[first.len_utf8()..].find(first)?; + &trimmed[first.len_utf8()..first.len_utf8().saturating_add(close)] + } else { + trimmed + .split(['\r', '\n', ',', ';']) + .next() + .unwrap_or_default() + .split(" and ") + .next() + .unwrap_or_default() + .split(" with ") + .next() + .unwrap_or_default() + .split(" then ") + .next() + .unwrap_or_default() + .trim_end_matches(['.', ')', ']']) + }; + let prefix = extracted.trim(); + if prefix.is_empty() || !looks_like_user_path(prefix) { + return None; + } + Some(prefix.to_owned()) +} + +fn looks_like_user_path(value: &str) -> bool { + value.starts_with('/') + || value.starts_with('~') + || value.starts_with("\\\\") + || value.contains(":\\") + || value.contains(":/") + || value.contains('\\') +} + +pub(crate) fn requested_therock_build_date_from_prompt(normalized: &str) -> Option { + normalized + .split(|ch: char| !ch.is_ascii_alphanumeric()) + .filter(|token| token.len() == 8 && token.chars().all(|ch| ch.is_ascii_digit())) + .find_map( + |token| match therock::RuntimeVersionSelector::build_date(token).ok()? { + therock::RuntimeVersionSelector::BuildDate(date) => Some(date), + therock::RuntimeVersionSelector::Version(_) => None, + }, + ) + .or_else(|| { + normalized + .split_whitespace() + .map(|token| { + token.trim_matches(|ch: char| !ch.is_ascii_alphanumeric() && ch != '-') + }) + .find_map(|token| { + match therock::RuntimeVersionSelector::build_date(token).ok()? { + therock::RuntimeVersionSelector::BuildDate(date) => Some(date), + therock::RuntimeVersionSelector::Version(_) => None, + } + }) + }) +} + +pub(crate) fn requested_therock_version_from_prompt(normalized: &str) -> Option { + normalized + .split(|ch: char| !(ch.is_ascii_alphanumeric() || matches!(ch, '.' | '+' | '-' | '_'))) + .filter(|token| looks_like_therock_runtime_version(token)) + .find_map(|token| { + therock::RuntimeVersionSelector::version(token) + .ok() + .map(|_| token.to_owned()) + }) +} + +fn looks_like_therock_runtime_version(token: &str) -> bool { + let Some((prefix, date)) = token.rsplit_once('a') else { + return false; + }; + !prefix.is_empty() + && prefix.chars().next().is_some_and(|ch| ch.is_ascii_digit()) + && prefix.contains('.') + && date.len() == 8 + && date.chars().all(|ch| ch.is_ascii_digit()) + && therock::RuntimeVersionSelector::build_date(date).is_ok() +} + +pub(crate) fn infer_engine_from_request(lower: &str) -> Option<&'static str> { + // Word-boundary match, not substring: "aim" as a bare `.contains` also + // matches "aiming"/"claim"/"maintain", silently steering an unrelated + // chat request onto the Docker-backed AIM engine. + let words: std::collections::HashSet<&str> = lower + .split(|c: char| !c.is_ascii_alphanumeric()) + .filter(|word| !word.is_empty()) + .collect(); + ["lemonade", "vllm", "aim"] + .into_iter() + .find(|engine| words.contains(engine)) +} + +#[cfg(test)] +mod tests { + use super::infer_engine_from_request; + + /// "aim" must match as its own word, not as a substring of an unrelated + /// word like "aiming"/"claim"/"maintain" in a free-text chat request. + #[test] + fn infer_engine_from_request_does_not_match_aim_as_a_substring() { + assert_eq!( + infer_engine_from_request("serve a small model, aiming for low latency"), + None + ); + assert_eq!( + infer_engine_from_request("please explain how to maintain a claim"), + None + ); + assert_eq!( + infer_engine_from_request("serve qwen3.5-9b with aim"), + Some("aim") + ); + } +} diff --git a/apps/rocm/src/rocm_argv.rs b/apps/rocm/src/rocm_argv.rs new file mode 100644 index 0000000000..a257344f1f --- /dev/null +++ b/apps/rocm/src/rocm_argv.rs @@ -0,0 +1,128 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Argv helpers shared by `chat.rs`, `tool_policy.rs` and the freeform planner: +//! reading an option's value out of an argv slice (`chat_cli_arg_value`, +//! `chat_cli_arg_value_checked`, `chat_cli_has_flag`), appending a flag at +//! most once (`ensure_flag`), recognising a CPU device request in `serve` +//! arguments (`serve_args_request_cpu_device`), and rendering an argv as the +//! quoted one-line command shown in plans and prompts +//! (`format_structured_tool_call`). +//! +//! A leaf module whose only `crate::` dependency is the leaf `shell_quote.rs`: +//! `chat.rs`, `tool_policy.rs` and `freeform_planner.rs` all call into it, so +//! the planner does not have to import `chat` for these helpers. They were +//! relocated from `chat.rs` and `freeform_planner.rs`, which re-export the +//! items other modules still reach as `chat::ensure_flag` and +//! `freeform_planner::format_structured_tool_call`. + +use crate::shell_quote::shell_quote; +use anyhow::{Result, bail}; + +pub(crate) fn ensure_flag(args: &mut Vec, flag: &str) { + if !args.iter().any(|arg| arg == flag) { + args.push(flag.to_owned()); + } +} + +pub(crate) fn chat_cli_arg_value<'a>(args: &'a [String], name: &str) -> Option<&'a str> { + let mut index = 0; + while index < args.len() { + let arg = args[index].as_str(); + if let Some((option, value)) = arg.split_once('=') + && option == name + { + return Some(value); + } + if arg == name { + return args.get(index + 1).map(String::as_str); + } + index += 1; + } + None +} + +pub(crate) fn chat_cli_arg_value_checked<'a>( + args: &'a [String], + name: &str, +) -> Result> { + let mut index = 0; + while index < args.len() { + let arg = args[index].as_str(); + if let Some((option, value)) = arg.split_once('=') + && option == name + { + if value.trim().is_empty() { + bail!("local assistant provided empty value for {name}"); + } + return Ok(Some(value)); + } + if arg == name { + let Some(value) = args.get(index + 1).map(String::as_str) else { + bail!("local assistant omitted the value for {name}"); + }; + if value.starts_with("--") || value.trim().is_empty() { + bail!("local assistant omitted the value for {name}"); + } + return Ok(Some(value)); + } + index += 1; + } + Ok(None) +} + +pub(crate) fn chat_cli_has_flag(args: &[String], name: &str) -> bool { + args.iter().any(|arg| { + arg == name + || arg + .strip_prefix(name) + .is_some_and(|rest| rest.starts_with('=')) + }) +} + +pub(crate) fn device_policy_is_cpu_mode(policy: &str) -> bool { + matches!( + policy.trim().to_ascii_lowercase().as_str(), + "cpu" | "cpu_only" + ) +} + +pub(crate) fn serve_args_request_cpu_device(args: &[String]) -> bool { + let mut iter = args.iter().peekable(); + while let Some(arg) = iter.next() { + if let Some(value) = arg.strip_prefix("--device=") + && device_policy_is_cpu_mode(value) + { + return true; + } + if arg == "--device" + && let Some(value) = iter.peek() + && device_policy_is_cpu_mode(value) + { + return true; + } + } + false +} + +/// Render an argv as the command line a human is shown for it. +/// +/// This is the only thing an operator sees before approving a mutating tool +/// call, and some of its output (`rocm chat --prompt …`, the `install command:` +/// line) is offered to be copied and run. So it has to denote the argv it was +/// built from: quoted with [`shell_quote`], every argument splits back to +/// itself under a shell's own word-splitting, and nothing inside a value can +/// read as a second command. +/// +/// Values carrying terminal control characters never get here — they are +/// refused at validation +/// ([`has_terminal_control_character`](crate::shell_quote::has_terminal_control_character)), +/// because no amount of quoting stops a `\r` from rewriting the line the +/// operator is reading. +pub(crate) fn format_structured_tool_call(tool: &str, args: &[String]) -> String { + let mut parts = Vec::with_capacity(args.len() + 1); + parts.push(tool.to_owned()); + parts.extend(args.iter().map(|arg| shell_quote(arg))); + parts.join(" ") +} diff --git a/apps/rocm/src/services_text.rs b/apps/rocm/src/services_text.rs index 9d81873823..abc8998950 100644 --- a/apps/rocm/src/services_text.rs +++ b/apps/rocm/src/services_text.rs @@ -58,9 +58,11 @@ //! `pub(crate)` and read from `runtime_services.rs` (reconciling live servers //! against a runtime activation) and from `managed_service_status.rs`'s own //! test module; `read_tail_lines`, `render_logs_text`, `render_logs_browser_text`, -//! `render_services_text`, `render_service_logs_text`, and -//! `validate_service_id` are also `pub(crate)` and read from `chat.rs`'s -//! deterministic tool-output summaries; `read_optional_tail_lines` and +//! `render_services_text` and `render_service_logs_text` are also `pub(crate)` +//! and read from `chat.rs`'s deterministic tool-output summaries; +//! `validate_service_id` is also `pub(crate)` and read from `tool_policy.rs`'s +//! service-tool validation, `services_cmd.rs` and `service_records.rs`; +//! `read_optional_tail_lines` and //! `render_service_action_result` are also `pub(crate)` and read from //! `service_launch.rs`'s managed-spawn/attach path. Every other item in this //! list has no caller outside this module and stays private — including diff --git a/apps/rocm/src/tool_policy.rs b/apps/rocm/src/tool_policy.rs new file mode 100644 index 0000000000..1e842d7ce1 --- /dev/null +++ b/apps/rocm/src/tool_policy.rs @@ -0,0 +1,2502 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! The chat assistant's tool-call policy: which `rocm` commands and bridge +//! tools a model may request, which of them run immediately, and which need +//! the user's approval first. +//! +//! `validate_chat_tool_call` and the `validate_chat_*_tool_call` family +//! reject malformed or unsafe requests (a CPU or public-bind `serve`, a +//! system install folder, a service id or watcher mode that does not parse); +//! `chat_rocm_command_action` and `chat_rocm_command_action_from_args` sort a +//! `rocm` argv into a read-only command or one that needs approval +//! (`ChatRocmCommandAction`); `chat_tool_approval_request` builds the approval +//! prompt (`ChatToolApprovalRequest`) the dashboard shows; +//! `chat_tool_call_is_read_only` is the one yes/no a caller asks before +//! running a call; and `rocm_chat_tool_requested_args` and +//! `rocm_chat_tool_requested_command` turn a tool call into the argv and the +//! one-line command it stands for. +//! +//! The module was relocated verbatim from `chat.rs`, which calls into it and +//! re-exports the items other modules reach as `chat::`. It calls +//! nothing in `chat.rs` and nothing in `freeform_planner.rs`, which is what +//! lets the planner call into it without importing `chat`. + +use crate::rocm_argv::{ + chat_cli_arg_value, chat_cli_arg_value_checked, chat_cli_has_flag, ensure_flag, + format_structured_tool_call, serve_args_request_cpu_device, +}; +use crate::service_launch::is_loopback_host; +use crate::services_text::validate_service_id; +use crate::shell_quote::has_terminal_control_character; +use crate::{providers, therock}; +use anyhow::{Context, Result, bail}; +use rocm_core::{DEFAULT_LOCAL_HOST, builtin_watcher, runtime_install_root_is_protected}; +use std::path::Path; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub(crate) enum ChatRocmCommandAction { + ReadOnly(Vec), + Approval { + args: Vec, + pending_title: String, + command_title: String, + }, +} + +fn push_optional_json_cli_arg( + args: &mut Vec, + object: &serde_json::Map, + flag: &str, + key: &str, +) { + if let Some(value) = json_string(object, key) { + args.push(flag.to_owned()); + args.push(value); + } +} + +pub(crate) fn json_string( + object: &serde_json::Map, + key: &str, +) -> Option { + object + .get(key) + .and_then(serde_json::Value::as_str) + .map(str::to_owned) + .filter(|value| !value.trim().is_empty()) +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub(crate) struct ChatToolApprovalRequest { + pub pending_title: String, + pub command_title: String, + pub args: Vec, + pub display_command: Option, + pub explanation: Option, +} + +pub(crate) fn chat_tool_approval_request( + call: &providers::ChatToolCall, + assistant_explanation: Option<&str>, +) -> Result { + validate_chat_tool_call(call)?; + if call.name == "rocm_command" { + let ChatRocmCommandAction::Approval { + args, + pending_title, + command_title, + } = chat_rocm_command_action(call)? + else { + bail!("ROCm command tool is read-only and does not need approval"); + }; + // `pending_title` is deliberately short (e.g. "Install driver"); don't + // read that as all the approver sees. The full `rocm` argv rides + // along via `display_command` below and is what the approval body + // actually renders (see `dash_seam.rs`'s `RocmToolExecutor::execute`). + return Ok(ChatToolApprovalRequest { + pending_title, + command_title, + display_command: Some(format_structured_tool_call("rocm", &args)), + args, + explanation: assistant_explanation + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_owned), + }); + } + // `proposal_action` (approve/reject) executes in-process in + // `run_internal_mcp_call` — it has NO CLI argv, so build its approval + // request directly rather than through `rocm_chat_tool_requested_args`. + if call.name == "proposal_action" { + let object = call + .arguments + .as_object() + .context("proposal_action arguments must be a JSON object")?; + let proposal_id = json_string(object, "proposal_id") + .context("proposal_action requires non-empty `proposal_id`")?; + let action = + json_string(object, "action").context("proposal_action requires non-empty `action`")?; + let pending_title = match action.as_str() { + "approve" => "Approve proposal", + "reject" => "Reject proposal", + other => bail!("proposal_action `{other}` does not require approval"), + }; + return Ok(ChatToolApprovalRequest { + pending_title: pending_title.to_owned(), + command_title: "Reviews".to_owned(), + display_command: Some(format!("proposal {proposal_id}")), + args: Vec::new(), + explanation: assistant_explanation + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_owned), + }); + } + let args = rocm_chat_tool_requested_args(call) + .with_context(|| format!("ROCm tool `{}` is missing required arguments", call.name))?; + let (pending_title, command_title) = match call.name.as_str() { + "install_sdk" => ("Install ROCm", "Install"), + "install_engine" => ("Install engine", "Engine"), + "launch_server" => ("Start local model server", "Serve"), + "stop_server" => ("Stop local model server", "Services"), + "watcher_enable" => ("Enable automation", "Automations"), + "watcher_disable" => ("Disable automation", "Automations"), + other => bail!("ROCm tool `{other}` is read-only or unsupported for approval"), + }; + // Same as the `rocm_command` arm above: `pending_title` is short by + // design — the full command line rides along in `display_command` and + // is what the approval body renders, not just this title. + Ok(ChatToolApprovalRequest { + pending_title: pending_title.to_owned(), + command_title: command_title.to_owned(), + display_command: rocm_chat_tool_requested_command(call), + args, + explanation: assistant_explanation + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(str::to_owned), + }) +} + +pub(crate) fn validate_chat_tool_call(call: &providers::ChatToolCall) -> Result<()> { + // First, because every refusal below names the tool: a model-chosen name + // carrying a control character would otherwise reach the terminal inside + // the very error that rejects it. This one names nothing. + if has_terminal_control_character(&call.name) { + bail!("local assistant requested a ROCm tool whose name contains a control character"); + } + if !call.arguments.is_object() { + bail!("ROCm tool `{}` arguments must be a JSON object", call.name); + } + reject_control_characters_in_tool_arguments(&call.name, &call.arguments)?; + match call.name.as_str() { + "examine" + // `doctor` is the dash-side LLM tool + `/doctor` overlay name for the + // same machine inspection the bin exposes as `examine`. Accept it as an + // alias so a model-issued `doctor` call resolves end-to-end. + | "doctor" + | "bridge_snapshot" + | "gpu_snapshot" + | "engines" + | "services" + | "service_logs" + | "automations" + | "natural_language_plan" + | "path_exists" + | "port_status" + | "rocm_command" + | "update_check" + | "install_sdk_dry_run" + | "install_sdk" + | "install_engine" + | "launch_server" + | "stop_server" + | "watcher_enable" + | "watcher_disable" + | "proposal_action" => {} + other => bail!("local assistant requested unsupported ROCm tool `{other}`"), + } + match call.name.as_str() { + "install_sdk" => validate_chat_install_sdk_tool_call(call)?, + "install_engine" => validate_required_chat_string(call, "engine")?, + "launch_server" => validate_chat_launch_server_tool_call(call)?, + "service_logs" | "stop_server" => validate_chat_service_tool_call(call)?, + "automations" => validate_optional_chat_integer(call, "event_limit", 1, 64)?, + "natural_language_plan" => validate_required_chat_string(call, "request")?, + "path_exists" => validate_required_chat_string(call, "path")?, + "port_status" => validate_chat_port_status_tool_call(call)?, + "rocm_command" => validate_chat_rocm_command_tool_call(call)?, + "watcher_enable" => validate_chat_watcher_tool_call(call, true)?, + "watcher_disable" => validate_chat_watcher_tool_call(call, false)?, + "proposal_action" => validate_chat_proposal_action_tool_call(call)?, + _ => {} + } + Ok(()) +} + +/// The tools exempt from [`reject_control_characters_in_tool_arguments`]. +/// +/// Deny by default: every other tool, including one added later, has its +/// whole arguments object screened before anything else looks at it. The +/// criterion that used to pick tools *in* — "its arguments reach the +/// operator's terminal" — kept missing values that reach it some other way: +/// `port_status` echoes a refused `host` in its own error, which the CLI +/// prints. A list of exceptions fails safe when it is forgotten; a list of +/// guarded tools does not. +/// +/// `rocm_command` is the one exception, and it is not unscreened: its argv is +/// checked entry by entry in [`normalized_chat_rocm_command_args`], with the +/// same predicate, after trimming each entry — so screening the raw object +/// here would refuse a padded value that path legitimately accepts. Its +/// `reason` is free-form prose that is never printed. +/// +/// Pinned by `every_tool_but_the_exempt_one_refuses_a_control_character_anywhere` +/// (adding a name here turns it red) and +/// `an_unknown_tool_is_screened_before_it_is_rejected` (going back to an +/// allow-list turns it red). +const CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS: [&str; 1] = ["rocm_command"]; + +/// Refuse a model-supplied string argument carrying a terminal control or +/// format character, for every tool not in +/// [`CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS`]. +/// +/// Those values are printed: into the command preview an operator approves, +/// into the `request plan` / `execution` blocks `rocm ` emits, into +/// the plan text `natural_language_plan` returns, into a refusal that echoes +/// the value it refused, and — for `path_exists` — into a tool-result block +/// with no gate at all. All of it +/// reaches a terminal as bytes, so a `\r` returns the cursor to column one and +/// a CSI sequence erases the line; the model, not this code, then chooses what +/// the operator reads. Quoting cannot fix that: `'…'` makes the byte literal to +/// a *shell*, not to a terminal. +/// +/// Scope note, so the guarantee is not overread: this covers model-supplied +/// *arguments*, not model-supplied prose. An assistant's reply text is still +/// printed as it comes; `strip_ansi_sequences` in `comfyui.rs` is the tool for +/// that surface, and is already used on ComfyUI log output. +/// +/// Walks the whole arguments object rather than naming fields, so a new +/// argument on one of these tools inherits the guarantee instead of having to +/// remember it. +fn reject_control_characters_in_tool_arguments( + tool: &str, + arguments: &serde_json::Value, +) -> Result<()> { + if CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS.contains(&tool) { + return Ok(()); + } + reject_control_characters(tool, arguments) +} + +fn reject_control_characters(tool: &str, arguments: &serde_json::Value) -> Result<()> { + match arguments { + serde_json::Value::String(value) => { + if has_terminal_control_character(value) { + // The wording is load-bearing for the per-tool test, which + // asserts this refusal rather than merely `is_err()` — several + // of these tools would refuse a hostile value for some other + // reason, and a test that accepted any error would pass with + // this guard removed. + bail!( + "ROCm tool `{tool}` argument contains a control character; \ + it would rewrite what the operator reads" + ); + } + Ok(()) + } + serde_json::Value::Array(values) => values + .iter() + .try_for_each(|value| reject_control_characters(tool, value)), + serde_json::Value::Object(entries) => entries + .values() + .try_for_each(|value| reject_control_characters(tool, value)), + _ => Ok(()), + } +} + +/// Validate a `proposal_action` chat-tool call: `proposal_id` must be a +/// non-empty string and `action` must be one of show | approve | reject. +fn validate_chat_proposal_action_tool_call(call: &providers::ChatToolCall) -> Result<()> { + let object = call + .arguments + .as_object() + .context("proposal_action arguments must be a JSON object")?; + let proposal_id = json_string(object, "proposal_id") + .context("proposal_action requires non-empty `proposal_id`")?; + if proposal_id.len() > 128 { + bail!("proposal_id too long"); + } + let action = + json_string(object, "action").context("proposal_action requires non-empty `action`")?; + if !matches!(action.as_str(), "show" | "approve" | "reject") { + bail!("proposal_action `action` must be one of show, approve, reject"); + } + Ok(()) +} + +fn validate_chat_install_sdk_tool_call(call: &providers::ChatToolCall) -> Result<()> { + let object = call + .arguments + .as_object() + .context("install_sdk arguments must be a JSON object")?; + let channel = json_string(object, "channel").unwrap_or_else(|| "release".to_owned()); + if !matches!(channel.as_str(), "release" | "nightly") { + bail!("local assistant requested unsupported TheRock channel `{channel}`"); + } + let format = json_string(object, "format").unwrap_or_else(|| "wheel".to_owned()); + if !matches!(format.as_str(), "wheel" | "tarball") { + bail!("local assistant requested unsupported TheRock install format `{format}`"); + } + if rocm_core::runtime_is_windows() && format != "wheel" { + bail!("local assistant cannot request `{format}` installs on Windows; use wheel"); + } + let version = json_string(object, "version"); + let build_date = json_string(object, "build_date"); + if version.is_some() && build_date.is_some() { + bail!("local assistant cannot request both `version` and `build_date`"); + } + if format != "wheel" && (version.is_some() || build_date.is_some()) { + bail!( + "local assistant can only request specific TheRock wheel versions for wheel installs" + ); + } + if let Some(version) = version { + therock::RuntimeVersionSelector::version(version)?; + } + if let Some(build_date) = build_date { + therock::RuntimeVersionSelector::build_date(build_date)?; + } + let Some(prefix) = json_string(object, "prefix") else { + bail!( + "local assistant must ask the user for a ROCm/TheRock install folder before requesting install_sdk" + ); + }; + let prefix_path = Path::new(&prefix); + if chat_install_prefix_is_system(prefix_path) { + bail!( + "local assistant cannot request system install folder `{}`", + prefix_path.display() + ); + } + Ok(()) +} + +fn validate_chat_launch_server_tool_call(call: &providers::ChatToolCall) -> Result<()> { + let object = call + .arguments + .as_object() + .context("launch_server arguments must be a JSON object")?; + if object + .get("allow_public_bind") + .and_then(serde_json::Value::as_bool) + .unwrap_or(false) + { + bail!("local assistant cannot request public network binding"); + } + let host = json_string(object, "host").unwrap_or_else(|| DEFAULT_LOCAL_HOST.to_owned()); + if !is_loopback_host(&host) { + bail!("local assistant cannot request non-local host `{host}`"); + } + if object + .get("device") + .and_then(serde_json::Value::as_str) + .is_some_and(|device| device.to_ascii_lowercase().contains("cpu")) + { + bail!("local assistant cannot request CPU execution; ROCm GPU execution is required"); + } + Ok(()) +} + +fn validate_chat_service_tool_call(call: &providers::ChatToolCall) -> Result<()> { + let object = call + .arguments + .as_object() + .context("service tool arguments must be a JSON object")?; + let service_id = + json_string(object, "service_id").context("service tool requires `service_id`")?; + validate_service_id(&service_id)?; + if call.name == "service_logs" { + validate_optional_chat_integer(call, "lines", 1, 500)?; + } + Ok(()) +} + +fn validate_chat_watcher_tool_call(call: &providers::ChatToolCall, allow_mode: bool) -> Result<()> { + let object = call + .arguments + .as_object() + .context("watcher tool arguments must be a JSON object")?; + let watcher = json_string(object, "watcher").context("watcher tool requires `watcher`")?; + if watcher.len() > 128 { + bail!("watcher id too long"); + } + if builtin_watcher(&watcher).is_none() { + bail!("local assistant requested unknown watcher `{watcher}`"); + } + if let Some(mode) = json_string(object, "mode") { + if !allow_mode { + bail!("local assistant cannot set `mode` when disabling a watcher"); + } + if !matches!(mode.as_str(), "observe" | "propose" | "contained") { + bail!("local assistant requested unsupported watcher mode `{mode}`"); + } + } + Ok(()) +} + +fn validate_chat_port_status_tool_call(call: &providers::ChatToolCall) -> Result<()> { + let object = call + .arguments + .as_object() + .context("port_status arguments must be a JSON object")?; + let host = json_string(object, "host").unwrap_or_else(|| DEFAULT_LOCAL_HOST.to_owned()); + if !is_loopback_host(&host) { + bail!("local assistant cannot inspect non-local host `{host}`"); + } + let Some(port) = object.get("port").and_then(serde_json::Value::as_u64) else { + bail!("ROCm tool `port_status` requires integer `port`"); + }; + if !(1..=u64::from(u16::MAX)).contains(&port) { + bail!("ROCm tool `port_status` argument `port` must be between 1 and 65535"); + } + Ok(()) +} + +fn validate_required_chat_string(call: &providers::ChatToolCall, key: &str) -> Result<()> { + let object = call + .arguments + .as_object() + .context("tool arguments must be a JSON object")?; + json_string(object, key) + .with_context(|| format!("ROCm tool `{}` requires non-empty `{key}`", call.name))?; + Ok(()) +} + +fn validate_optional_chat_integer( + call: &providers::ChatToolCall, + key: &str, + min: u64, + max: u64, +) -> Result<()> { + let object = call + .arguments + .as_object() + .context("tool arguments must be a JSON object")?; + let Some(value) = object.get(key) else { + return Ok(()); + }; + let Some(value) = value.as_u64() else { + bail!( + "ROCm tool `{}` argument `{key}` must be an integer", + call.name + ); + }; + if value < min || value > max { + bail!( + "ROCm tool `{}` argument `{key}` must be between {min} and {max}", + call.name + ); + } + Ok(()) +} + +fn validate_chat_rocm_command_tool_call(call: &providers::ChatToolCall) -> Result<()> { + chat_rocm_command_action(call).map(|_| ()) +} + +pub(crate) fn chat_rocm_command_action( + call: &providers::ChatToolCall, +) -> Result { + let args = normalized_chat_rocm_command_args(call)?; + chat_rocm_command_action_from_args(args) +} + +pub(crate) fn normalized_chat_rocm_command_args( + call: &providers::ChatToolCall, +) -> Result> { + let object = call + .arguments + .as_object() + .context("rocm_command arguments must be a JSON object")?; + let values = object + .get("args") + .and_then(serde_json::Value::as_array) + .context("rocm_command requires `args`")?; + if values.is_empty() || values.len() > 64 { + bail!("rocm_command `args` must contain 1 to 64 strings"); + } + let mut args = Vec::with_capacity(values.len()); + for value in values { + let arg = value + .as_str() + .map(str::trim) + .filter(|value| !value.is_empty()) + .context("rocm_command `args` entries must be non-empty strings")?; + // Every control character, not only NUL/LF/CR. These args are rendered + // into the approval card and the `advanced manual command:` line, which + // reach a terminal as bytes — an ESC or a C1 CSI there redraws what the + // operator is reading just as effectively as a `\r`, and this list used + // to let both through. See `has_terminal_control_character`. + if has_terminal_control_character(arg) { + bail!("rocm_command arguments must not contain control characters"); + } + if arg.len() > 512 { + bail!("rocm_command argument is too long"); + } + args.push(arg.to_owned()); + } + if args + .first() + .is_some_and(|arg| arg.eq_ignore_ascii_case("rocm")) + { + args.remove(0); + } + if args.is_empty() { + bail!("rocm_command args should omit the leading `rocm` program name"); + } + if let Some(reason) = object.get("reason") + && !reason.is_string() + { + bail!("rocm_command `reason` must be a string when present"); + } + Ok(args) +} + +/// The chat action for `rocm install driver`. Where the command only reports +/// (`only_reports`, the same decision the command itself makes) it is a +/// read-only call with no `--yes`; elsewhere it needs approval and carries +/// `--yes`. +/// +/// A model-supplied `--yes` is stripped in both forms, bare and `--yes=...`, +/// as the `install sdk` arm does: the approval path appends its own bare +/// `--yes`, so a stray attached form would otherwise ride along and clap would +/// refuse the whole command, and the read-only path has no consent to grant. +pub(crate) fn chat_install_driver_action( + mut args: Vec, + only_reports: bool, +) -> ChatRocmCommandAction { + args.retain(|arg| arg != "--yes" && !arg.starts_with("--yes=")); + if only_reports { + return ChatRocmCommandAction::ReadOnly(args); + } + ensure_flag(&mut args, "--yes"); + ChatRocmCommandAction::Approval { + args, + pending_title: "Install driver".to_owned(), + command_title: "Install".to_owned(), + } +} + +pub(crate) fn chat_rocm_command_action_from_args( + mut args: Vec, +) -> Result { + canonicalize_chat_rocm_command(&mut args)?; + validate_chat_rocm_command_safety(&args)?; + let first = args.first().map(|value| value.to_ascii_lowercase()); + let second = args.get(1).map(|value| value.to_ascii_lowercase()); + match first.as_deref() { + Some("examine" | "version" | "model" | "models" | "daemon" | "logs") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("update") if !args.iter().any(|arg| arg == "--apply") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("runtimes") if second.as_deref().is_none_or(|value| value == "list") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("engines") if second.as_deref().is_some_and(|value| value == "list") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("services") + if second + .as_deref() + .is_none_or(|value| matches!(value, "list" | "logs")) => + { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("automations") if second.as_deref().is_none_or(|value| value == "list") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("config") if second.as_deref() == Some("show") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("comfyui") + if second + .as_deref() + .is_none_or(|value| matches!(value, "status" | "logs" | "log")) => + { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("install") if second.as_deref() == Some("sdk") => { + // The chat/MCP surfaces spawn `rocm` with null stdin, so + // `interactive_terminal()` is false and the consent prompt would + // refuse with a "re-run with `--approve-replacing-active-default`" + // error, which the schema-constrained `install_sdk` tool gives the + // user no way to answer. + // + // Not `--yes`: that flag also approves system-package installs, and + // this spawn has no terminal on which to answer the `sudo` password + // prompt such an install can raise. Granting it here would make the + // vLLM/OpenMPI step run a sudo it cannot complete and abort the + // engine auto-install that previously warned and continued. The + // narrow flag grants exactly the consent the prompt is asking for. + // + // `--yes` is stripped rather than merely not added, because the + // generic `rocm_command` tool takes a model-supplied argv: a + // model-emitted `--yes` would otherwise reach this null-stdin spawn + // and re-grant the system-package consent `76c6aa3c` removed. The + // strip runs before the argv is rendered for human approval, so what + // is shown is still what runs. + // + // The `--yes=...` form is stripped too. Clap rejects an attached + // value on this flag today, so an exact-match strip happens to be + // airtight — but only by borrowing a property of clap's error + // taxonomy that nothing here owns. Giving `--yes` `num_args`, or a + // clap release that starts accepting `--yes=true` on a bare `bool`, + // would silently restore the sudo consent this strip exists to + // remove. Matching the prefix keeps the guarantee local to this + // function. + args.retain(|arg| arg != "--yes" && !arg.starts_with("--yes=")); + // Withheld on `--dry-run`, which returns before the consent gate and + // so needs no consent: `rocmd` and dash-tui omit it there for the + // same reason, and a preview should not be recorded as carrying an + // approval it never used. + if !args.iter().any(|arg| arg == "--dry-run") { + ensure_flag(&mut args, "--approve-replacing-active-default"); + } + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Install ROCm".to_owned(), + command_title: "Install".to_owned(), + }) + } + Some("install") if second.as_deref() == Some("driver") => Ok(chat_install_driver_action( + args, + crate::driver_install::host_install_driver_only_reports(), + )), + Some("update") if args.iter().any(|arg| arg == "--apply") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Apply ROCm update".to_owned(), + command_title: "Update".to_owned(), + }) + } + Some("runtimes") + if second + .as_deref() + .is_some_and(|value| value == "uninstall" || value == "remove") + && args.iter().any(|arg| arg == "--dry-run") => + { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("runtimes") + if second + .as_deref() + .is_some_and(|value| value == "uninstall" || value == "remove") => + { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Remove ROCm install".to_owned(), + command_title: "Runtimes".to_owned(), + }) + } + Some("runtimes") => Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Change ROCm runtime".to_owned(), + command_title: "Runtimes".to_owned(), + }), + Some("engines") if second.as_deref() == Some("install") => { + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Install engine".to_owned(), + command_title: "Engine".to_owned(), + }) + } + Some("serve") => Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Start local model server".to_owned(), + command_title: "Serve".to_owned(), + }), + Some("services") + if second + .as_deref() + .is_some_and(|value| matches!(value, "stop" | "restart")) => + { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Change local model server".to_owned(), + command_title: "Services".to_owned(), + }) + } + Some("automations") + if second + .as_deref() + .is_some_and(|value| matches!(value, "enable" | "disable")) => + { + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Change automation".to_owned(), + command_title: "Automations".to_owned(), + }) + } + Some("config") => Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Change settings".to_owned(), + command_title: "Config".to_owned(), + }), + Some("uninstall") if args.iter().any(|arg| arg == "--dry-run") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("uninstall") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Uninstall ROCm CLI".to_owned(), + command_title: "Uninstall".to_owned(), + }) + } + // `storage report` (the default subcommand) only measures folders, so + // the assistant can run it directly. The two `remove-*` verbs delete + // multi-gigabyte trees and go through the approval UI. + Some("storage") if second.as_deref().is_none_or(|value| value == "report") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("storage") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Free up disk space".to_owned(), + command_title: "Storage".to_owned(), + }) + } + Some("comfyui") if second.as_deref() == Some("install") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Install ComfyUI".to_owned(), + command_title: "ComfyUI".to_owned(), + }) + } + Some("comfyui") if second.as_deref() == Some("start") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Start ComfyUI".to_owned(), + command_title: "ComfyUI".to_owned(), + }) + } + Some("comfyui") if second.as_deref() == Some("stop") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Stop ComfyUI".to_owned(), + command_title: "ComfyUI".to_owned(), + }) + } + Some("setup") if second.as_deref().is_none_or(|value| value == "status") => { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + Some("setup") if second.as_deref() == Some("reset") => { + ensure_flag(&mut args, "--yes"); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: "Reset first-time setup".to_owned(), + command_title: "Setup".to_owned(), + }) + } + // Mirrors the daemon's `ensure_rocm_command_is_read_only`, which admits + // the same three verbs. The daemon's arm carries the reasoning; the + // invariant is that the two agree, and this PR added the arm there + // first, so the two disagreed until this was written. + Some("remote") + if second + .as_deref() + .is_some_and(|value| matches!(value, "targets" | "doctor" | "status")) => + { + Ok(ChatRocmCommandAction::ReadOnly(args)) + } + // `serve`, `attach` and `stop` start, publish or tear down something on + // another machine, so they go through approval like any other mutation. + Some("remote") + if second + .as_deref() + .is_some_and(|value| matches!(value, "serve" | "attach" | "stop")) => + { + let verb = second.as_deref().unwrap_or_default().to_owned(); + Ok(ChatRocmCommandAction::Approval { + args, + pending_title: format!("Remote {verb}"), + command_title: "Remote".to_owned(), + }) + } + Some(command) => bail!("local assistant cannot use unsupported rocm command `{command}`"), + None => bail!("rocm_command requires at least one argument"), + } +} + +fn canonicalize_chat_rocm_command(args: &mut [String]) -> Result<()> { + if args + .first() + .is_some_and(|arg| arg.eq_ignore_ascii_case("comfy")) + { + args[0] = "comfyui".to_owned(); + } + if args + .first() + .is_some_and(|arg| arg.eq_ignore_ascii_case("engine")) + { + bail!("use `engines` for rocm engine commands"); + } + Ok(()) +} + +pub(crate) fn validate_chat_rocm_command_safety(args: &[String]) -> Result<()> { + if let Some(first) = args.first() + && first.eq_ignore_ascii_case("serve") + { + if chat_cli_has_flag(args, "--allow-public-bind") { + bail!("local assistant cannot request public network binding"); + } + if serve_args_request_cpu_device(args) { + bail!("local assistant cannot request CPU execution; ROCm GPU execution is required"); + } + if let Some(host) = chat_cli_arg_value(args, "--host") + && !is_loopback_host(host) + { + bail!("local assistant cannot request non-local host `{host}`"); + } + if chat_cli_has_flag(args, "--foreground") || !chat_cli_has_flag(args, "--managed") { + bail!("local assistant must request managed serving with --managed"); + } + } + if args + .first() + .is_some_and(|arg| arg.eq_ignore_ascii_case("comfyui")) + && args + .get(1) + .is_some_and(|arg| arg.eq_ignore_ascii_case("start")) + && let Some(host) = chat_cli_arg_value(args, "--host") + && !is_loopback_host(host) + { + bail!("local assistant cannot start ComfyUI on non-local host `{host}`"); + } + if args + .first() + .is_some_and(|arg| arg.eq_ignore_ascii_case("install")) + && args + .get(1) + .is_some_and(|arg| arg.eq_ignore_ascii_case("sdk")) + { + if rocm_core::runtime_is_windows() + && chat_cli_arg_value(args, "--format") + .is_some_and(|value| !value.eq_ignore_ascii_case("wheel")) + { + bail!("local assistant cannot request non-wheel ROCm installs on Windows"); + } + let version = chat_cli_arg_value_checked(args, "--version")?; + let build_date = chat_cli_arg_value_checked(args, "--build-date")?; + if version.is_some() && build_date.is_some() { + bail!("local assistant cannot request both --version and --build-date"); + } + if version.is_some() || build_date.is_some() { + let format = chat_cli_arg_value(args, "--format").unwrap_or("wheel"); + if !format.eq_ignore_ascii_case("wheel") { + bail!( + "local assistant can only request specific TheRock wheel versions for wheel installs" + ); + } + } + if let Some(version) = version { + therock::RuntimeVersionSelector::version(version)?; + } + if let Some(build_date) = build_date { + therock::RuntimeVersionSelector::build_date(build_date)?; + } + let Some(prefix) = chat_cli_arg_value_checked(args, "--prefix")? else { + bail!( + "local assistant must ask the user for a ROCm/TheRock install folder before requesting `rocm install sdk`" + ); + }; + let prefix_path = Path::new(prefix); + if chat_install_prefix_is_system(prefix_path) { + bail!( + "local assistant cannot request system install folder `{}`", + prefix_path.display() + ); + } + } + Ok(()) +} + +pub(crate) fn chat_install_prefix_is_system(prefix: &Path) -> bool { + prefix.as_os_str().is_empty() || runtime_install_root_is_protected(prefix) +} + +pub(crate) fn chat_tool_call_is_read_only(call: &providers::ChatToolCall) -> bool { + if call.name == "rocm_command" { + return matches!( + chat_rocm_command_action(call), + Ok(ChatRocmCommandAction::ReadOnly(_)) + ); + } + // `proposal_action` is read-only only when showing a proposal; approve/reject + // mutate the proposal status and must route through approval. + if call.name == "proposal_action" { + return call + .arguments + .as_object() + .and_then(|object| json_string(object, "action")) + .as_deref() + == Some("show"); + } + matches!( + call.name.as_str(), + "examine" + | "doctor" + | "bridge_snapshot" + | "gpu_snapshot" + | "engines" + | "services" + | "service_logs" + | "automations" + | "natural_language_plan" + | "path_exists" + | "port_status" + | "update_check" + | "install_sdk_dry_run" + ) +} + +pub(crate) fn rocm_chat_tool_requested_command(call: &providers::ChatToolCall) -> Option { + let args = rocm_chat_tool_requested_args(call)?; + Some(format_structured_tool_call("rocm", &args)) +} + +pub(crate) fn rocm_chat_tool_requested_args(call: &providers::ChatToolCall) -> Option> { + let object = call.arguments.as_object()?; + match call.name.as_str() { + "install_sdk" => { + let mut args = vec![ + "install".to_owned(), + "sdk".to_owned(), + "--channel".to_owned(), + json_string(object, "channel").unwrap_or_else(|| "release".to_owned()), + "--format".to_owned(), + json_string(object, "format").unwrap_or_else(|| "wheel".to_owned()), + // The MCP surface runs `rocm` with null stdin, so the consent + // prompt would refuse. This keeps the tool non-interactive, + // matching the chat `install sdk` arm. Deliberately not `--yes`: + // that would additionally approve a `sudo` system-package + // install this spawn has no terminal to answer. + "--approve-replacing-active-default".to_owned(), + ]; + if let Some(prefix) = json_string(object, "prefix") { + args.push("--prefix".to_owned()); + args.push(prefix); + } + if let Some(version) = json_string(object, "version") { + args.push("--version".to_owned()); + args.push(version); + } + if let Some(build_date) = json_string(object, "build_date") { + args.push("--build-date".to_owned()); + args.push(build_date); + } + Some(args) + } + "install_engine" => { + let engine = json_string(object, "engine")?; + let mut args = vec!["engines".to_owned(), "install".to_owned(), engine]; + if let Some(runtime_id) = json_string(object, "runtime_id") { + args.push("--runtime-id".to_owned()); + args.push(runtime_id); + } + if let Some(python_version) = json_string(object, "python_version") { + args.push("--python-version".to_owned()); + args.push(python_version); + } + if object + .get("reinstall") + .and_then(serde_json::Value::as_bool) + .unwrap_or(false) + { + args.push("--reinstall".to_owned()); + } + Some(args) + } + "launch_server" => { + let model = json_string(object, "model")?; + let mut args = vec!["serve".to_owned(), model, "--managed".to_owned()]; + push_optional_json_cli_arg(&mut args, object, "--engine", "engine"); + push_optional_json_cli_arg(&mut args, object, "--device", "device"); + push_optional_json_cli_arg(&mut args, object, "--runtime-id", "runtime_id"); + push_optional_json_cli_arg(&mut args, object, "--env-id", "env_id"); + push_optional_json_cli_arg(&mut args, object, "--host", "host"); + if let Some(port) = object.get("port").and_then(serde_json::Value::as_u64) { + args.push("--port".to_owned()); + args.push(port.to_string()); + } + Some(args) + } + "stop_server" => { + let service_id = json_string(object, "service_id")?; + Some(vec![ + "services".to_owned(), + "stop".to_owned(), + service_id, + "--yes".to_owned(), + ]) + } + "watcher_enable" => { + let watcher = json_string(object, "watcher")?; + let mut args = vec!["automations".to_owned(), "enable".to_owned(), watcher]; + push_optional_json_cli_arg(&mut args, object, "--mode", "mode"); + Some(args) + } + "watcher_disable" => { + let watcher = json_string(object, "watcher")?; + Some(vec![ + "automations".to_owned(), + "disable".to_owned(), + watcher, + ]) + } + "rocm_command" => match chat_rocm_command_action(call).ok()? { + ChatRocmCommandAction::Approval { args, .. } + | ChatRocmCommandAction::ReadOnly(args) => Some(args), + }, + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::runtime_install_finalize::tests::consents_for_install_sdk_argv; + use crate::{Cli, Command, InstallTarget}; + use clap::Parser; + + fn rocm_command_call(args: &[&str]) -> providers::ChatToolCall { + providers::ChatToolCall { + id: Some("test".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ "args": args }), + } + } + + #[test] + fn runtimes_activate_approval_modal_uses_the_runtime_noun() { + // `comfyui install`'s selection errors steer the user to `rocm runtimes` + // and say "ROCm runtime". Running a mutating `rocm runtimes …` from chat + // raises an approval modal whose title is written here, not by clap, so + // `main.rs`'s `runtimes_help_uses_the_runtime_noun_throughout` (which + // covers `--help` text) cannot reach it. It said "Change ROCm install" + // until this rename. + let action = chat_rocm_command_action_from_args(vec![ + "runtimes".to_owned(), + "activate".to_owned(), + "some-runtime-key".to_owned(), + ]) + .expect("a mutating runtimes command classifies"); + let ChatRocmCommandAction::Approval { pending_title, .. } = action else { + panic!("`rocm runtimes activate` must require approval, got {action:?}"); + }; + assert!( + pending_title.contains("runtime") && !pending_title.contains("install"), + "the `runtimes` approval modal must use the `runtime` noun, got {pending_title:?}" + ); + } + + #[test] + fn push_optional_json_cli_arg_pushes_present_values_only() { + let object = serde_json::json!({"reason": "because"}); + let object = object.as_object().unwrap(); + let mut args = Vec::new(); + push_optional_json_cli_arg(&mut args, object, "--reason", "reason"); + push_optional_json_cli_arg(&mut args, object, "--missing", "missing"); + assert_eq!(args, vec!["--reason".to_owned(), "because".to_owned()]); + } + + #[test] + fn json_string_filters_missing_and_blank_values() { + let object = serde_json::json!({"reason": " ", "note": "ok"}); + let object = object.as_object().unwrap(); + assert_eq!(json_string(object, "note"), Some("ok".to_owned())); + assert_eq!(json_string(object, "reason"), None); + assert_eq!(json_string(object, "missing"), None); + } + + #[test] + fn normalized_chat_rocm_command_args_strips_leading_program_name() { + let call = rocm_command_call(&["rocm", "engines", "list"]); + assert_eq!( + normalized_chat_rocm_command_args(&call).unwrap(), + vec!["engines".to_owned(), "list".to_owned()] + ); + } + + #[test] + fn normalized_chat_rocm_command_args_rejects_empty_and_control_characters() { + assert_eq!( + normalized_chat_rocm_command_args(&rocm_command_call(&[])) + .unwrap_err() + .to_string(), + "rocm_command `args` must contain 1 to 64 strings" + ); + assert!(normalized_chat_rocm_command_args(&rocm_command_call(&["rocm"])).is_err()); + + for control_arg in ["engines\nlist", "engines\rlist", "engines\0list"] { + let call = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({"args": [control_arg]}), + }; + assert_eq!( + normalized_chat_rocm_command_args(&call) + .unwrap_err() + .to_string(), + "rocm_command arguments must not contain control characters" + ); + } + } + + #[test] + fn normalized_chat_rocm_command_args_accepts_exactly_the_count_and_length_limits() { + let max_args = vec!["engines"; 64]; + assert_eq!( + normalized_chat_rocm_command_args(&rocm_command_call(&max_args)) + .unwrap() + .len(), + 64 + ); + + let max_len_arg = "a".repeat(512); + let call = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({"args": [max_len_arg.as_str()]}), + }; + assert_eq!( + normalized_chat_rocm_command_args(&call).unwrap(), + vec![max_len_arg] + ); + } + + #[test] + fn normalized_chat_rocm_command_args_rejects_over_limit_count_and_length() { + let too_many_args = vec!["engines"; 65]; + assert_eq!( + normalized_chat_rocm_command_args(&rocm_command_call(&too_many_args)) + .unwrap_err() + .to_string(), + "rocm_command `args` must contain 1 to 64 strings" + ); + + let too_long_arg = "a".repeat(513); + let call = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({"args": [too_long_arg]}), + }; + assert_eq!( + normalized_chat_rocm_command_args(&call) + .unwrap_err() + .to_string(), + "rocm_command argument is too long" + ); + } + + #[test] + fn normalized_chat_rocm_command_args_rejects_a_non_string_reason() { + let call = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({"args": ["engines", "list"], "reason": 42}), + }; + assert!(normalized_chat_rocm_command_args(&call).is_err()); + } + + #[test] + fn chat_tool_call_mutating_install_maps_to_reviewable_rocm_command() { + let call = providers::ChatToolCall { + id: Some("call-1".to_owned()), + name: "install_sdk".to_owned(), + arguments: serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": "D:\\ROCm\\therock_venvs" + }), + }; + + assert!(!chat_tool_call_is_read_only(&call)); + validate_chat_tool_call(&call).expect("install request should validate for review"); + assert_eq!( + rocm_chat_tool_requested_command(&call).as_deref(), + Some( + "rocm install sdk --channel release --format wheel --approve-replacing-active-default --prefix 'D:\\ROCm\\therock_venvs'" + ) + ); + let approval = chat_tool_approval_request( + &call, + Some("TheRock is not installed yet, so I need to install ROCm first."), + ) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Install ROCm"); + assert_eq!(approval.command_title, "Install"); + assert_eq!( + approval.explanation.as_deref(), + Some("TheRock is not installed yet, so I need to install ROCm first.") + ); + assert_eq!( + approval.args, + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--channel".to_owned(), + "release".to_owned(), + "--format".to_owned(), + "wheel".to_owned(), + "--approve-replacing-active-default".to_owned(), + "--prefix".to_owned(), + "D:\\ROCm\\therock_venvs".to_owned(), + ] + ); + } + + #[test] + fn chat_install_sdk_strips_model_supplied_yes_and_skips_consent_on_dry_run() { + // `rocm_command` carries a model-supplied argv that nothing else filters, + // so `--yes` can arrive here. This arm exists to grant the narrow consent + // only; letting `--yes` through would re-grant the system-package/sudo + // consent on a spawn with no terminal to answer a password prompt. + let classify = |args: &[&str]| -> Vec { + let action = chat_rocm_command_action_from_args( + args.iter().copied().map(str::to_owned).collect(), + ) + .expect("install sdk should classify"); + let ChatRocmCommandAction::Approval { args, .. } = action else { + panic!("install sdk is a mutating command"); + }; + args + }; + + assert_eq!( + classify(&["install", "sdk", "--prefix", "/tmp/therock", "--yes"]), + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--prefix".to_owned(), + "/tmp/therock".to_owned(), + "--approve-replacing-active-default".to_owned(), + ] + ); + + // A dry run returns before the consent gate, so it is not given a consent + // it never uses — matching the dry-run-aware sibling arms. + assert_eq!( + classify(&["install", "sdk", "--prefix", "/tmp/therock", "--dry-run"]), + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--prefix".to_owned(), + "/tmp/therock".to_owned(), + "--dry-run".to_owned(), + ] + ); + + // Both together: the strip is unconditional, so `--yes` still does not + // survive into a preview spawn, and the dry run still gains no consent. + // A model that emits both must not end up with either flag. + assert_eq!( + classify(&[ + "install", + "sdk", + "--prefix", + "/tmp/therock", + "--yes", + "--dry-run", + ]), + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--prefix".to_owned(), + "/tmp/therock".to_owned(), + "--dry-run".to_owned(), + ] + ); + } + + #[test] + fn chat_install_sdk_strips_a_model_supplied_yes_in_both_its_bare_and_attached_forms() { + // `--yes` and `--yes=true` are both model-supplied argv that reach the + // chat arm intact: neither `canonicalize_chat_rocm_command` nor + // `validate_chat_rocm_command_safety` splits or rejects either. Whichever + // form survives re-grants into a null-stdin spawn the system-package/sudo + // consent `76c6aa3c` removed, with no terminal to answer the password + // prompt, so the strip has to catch both. + let classify = |args: &[&str]| -> Vec { + let action = chat_rocm_command_action_from_args( + args.iter().copied().map(str::to_owned).collect(), + ) + .expect("install sdk should classify"); + let ChatRocmCommandAction::Approval { args, .. } = action else { + panic!("install sdk is a mutating command"); + }; + args + }; + + // Both terms of `arg != "--yes" && !arg.starts_with("--yes=")` are driven + // here, and each alone: the bare form is caught only by the first, the + // `=` forms only by the second, so dropping either term reddens this test + // on its own rather than leaving one half to a sibling. + for supplied in ["--yes", "--yes=true", "--yes=1", "--yes=false"] { + let args = classify(&["install", "sdk", "--prefix", "/tmp/therock", supplied]); + assert!( + !args.iter().any(|arg| arg.starts_with("--yes")), + "`{supplied}` must not survive the chat strip, got {args:?}" + ); + assert_eq!( + args, + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--prefix".to_owned(), + "/tmp/therock".to_owned(), + "--approve-replacing-active-default".to_owned(), + ], + "stripping `{supplied}` must leave the rest of the argv and the narrow consent alone" + ); + } + + // Future-proofing, not a guard on the `--yes=` term this test's other + // assertions pin: `--yes-not-a-flag` survives both the exact-match strip + // that preceded that term and the two-term strip that replaced it, so it + // would pass on either. What it does catch is the next edit — widening + // the second term to `starts_with("--yes")` to "simplify" it would start + // eating every argv token that merely begins the same way, silently + // dropping arguments the model legitimately sent. + let args = classify(&[ + "install", + "sdk", + "--prefix", + "/tmp/therock", + "--yes-not-a-flag", + ]); + assert!( + args.iter().any(|arg| arg == "--yes-not-a-flag"), + "the strip must match `--yes` and `--yes=…`, not every token starting with \ + `--yes`, got {args:?}" + ); + + // Second layer, and only the second: clap also refuses an attached value + // on this flag, so even an unstripped `--yes=true` would not parse today. + // That is what the strip above deliberately stops depending on — pinned + // here so a later `num_args` on `--yes` shows up as a failure of the + // backstop rather than passing unnoticed. + for attached in ["--yes=true", "--yes=1", "--yes=false"] { + let error = match Cli::try_parse_from(["rocm", "install", "sdk", attached]) { + Ok(cli) => panic!("`{attached}` must not parse, got {cli:?}"), + Err(error) => error, + }; + assert_eq!( + error.kind(), + clap::error::ErrorKind::TooManyValues, + "expected clap to reject the attached value on {attached}: {error}" + ); + } + + // Control: the bare form does parse, so the assertions above are about + // the `=`-form and not about `--yes` being rejected outright. + let cli = Cli::try_parse_from(["rocm", "install", "sdk", "--yes"]) + .expect("the bare flag is the form the chat arm strips"); + match cli.command { + Some(Command::Install { + target: InstallTarget::Sdk { yes, .. }, + }) => assert!(yes), + other => panic!("expected `install sdk`, got {other:?}"), + } + } + + #[test] + fn chat_tool_call_mutating_install_accepts_requested_build_date() { + let call = providers::ChatToolCall { + id: Some("call-date".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["install", "sdk", "--channel", "release", "--format", "wheel", "--prefix", "D:\\ROCm\\therock_venvs", "--build-date", "06052026"], + "reason": "The user asked for the TheRock build from 2026-06-05." + }), + }; + + validate_chat_tool_call(&call).expect("date-specific install should validate for review"); + assert!(!chat_tool_call_is_read_only(&call)); + assert_eq!( + rocm_chat_tool_requested_command(&call).as_deref(), + Some( + "rocm install sdk --channel release --format wheel --prefix 'D:\\ROCm\\therock_venvs' --build-date 06052026 --approve-replacing-active-default" + ) + ); + let approval = + chat_tool_approval_request(&call, Some("Install the requested TheRock build.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Install ROCm"); + assert_eq!( + approval.args, + vec![ + "install".to_owned(), + "sdk".to_owned(), + "--channel".to_owned(), + "release".to_owned(), + "--format".to_owned(), + "wheel".to_owned(), + "--prefix".to_owned(), + "D:\\ROCm\\therock_venvs".to_owned(), + "--build-date".to_owned(), + "06052026".to_owned(), + "--approve-replacing-active-default".to_owned(), + ] + ); + } + + #[test] + fn chat_tool_call_rejects_mutating_install_without_user_folder() { + let structured = providers::ChatToolCall { + id: Some("call-missing-prefix".to_owned()), + name: "install_sdk".to_owned(), + arguments: serde_json::json!({ + "channel": "release", + "format": "wheel" + }), + }; + let error = validate_chat_tool_call(&structured) + .unwrap_err() + .to_string(); + assert!(error.contains("ask the user"), "unexpected error: {error}"); + + let command = providers::ChatToolCall { + id: Some("call-command-missing-prefix".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["install", "sdk", "--channel", "release", "--format", "wheel"], + "reason": "Install ROCm." + }), + }; + let error = validate_chat_tool_call(&command).unwrap_err().to_string(); + assert!(error.contains("ask the user"), "unexpected error: {error}"); + } + + #[test] + fn chat_tool_call_service_and_watcher_changes_map_to_reviewable_rocm_commands() { + let stop = providers::ChatToolCall { + id: Some("call-stop".to_owned()), + name: "stop_server".to_owned(), + arguments: serde_json::json!({ "service_id": "svc-qwen" }), + }; + validate_chat_tool_call(&stop).expect("stop request should validate for review"); + assert_eq!( + rocm_chat_tool_requested_command(&stop).as_deref(), + Some("rocm services stop svc-qwen --yes") + ); + let approval = + chat_tool_approval_request(&stop, Some("This server is using memory we need.")) + .expect("stop approval should be built"); + assert_eq!(approval.pending_title, "Stop local model server"); + assert_eq!(approval.command_title, "Services"); + assert_eq!( + approval.args, + vec![ + "services".to_owned(), + "stop".to_owned(), + "svc-qwen".to_owned(), + "--yes".to_owned(), + ] + ); + assert_eq!( + approval.explanation.as_deref(), + Some("This server is using memory we need.") + ); + + let enable = providers::ChatToolCall { + id: Some("call-watch".to_owned()), + name: "watcher_enable".to_owned(), + arguments: serde_json::json!({ + "watcher": "server-recover", + "mode": "propose" + }), + }; + validate_chat_tool_call(&enable).expect("watcher enable should validate for review"); + assert_eq!( + rocm_chat_tool_requested_command(&enable).as_deref(), + Some("rocm automations enable server-recover --mode propose") + ); + let approval = + chat_tool_approval_request(&enable, Some("Recovering failed servers would help.")) + .expect("watcher approval should be built"); + assert_eq!(approval.pending_title, "Enable automation"); + assert_eq!(approval.command_title, "Automations"); + assert_eq!( + approval.args, + vec![ + "automations".to_owned(), + "enable".to_owned(), + "server-recover".to_owned(), + "--mode".to_owned(), + "propose".to_owned(), + ] + ); + + let disable = providers::ChatToolCall { + id: Some("call-disable".to_owned()), + name: "watcher_disable".to_owned(), + arguments: serde_json::json!({ "watcher": "server-recover" }), + }; + validate_chat_tool_call(&disable).expect("watcher disable should validate for review"); + assert_eq!( + rocm_chat_tool_requested_command(&disable).as_deref(), + Some("rocm automations disable server-recover") + ); + } + + #[test] + fn proposal_action_rejects_over_long_proposal_id() { + let call = providers::ChatToolCall { + id: None, + name: "proposal_action".to_owned(), + arguments: serde_json::json!({ + "proposal_id": "p".repeat(129), + "action": "show" + }), + }; + let err = validate_chat_proposal_action_tool_call(&call) + .expect_err("over-long proposal_id must be rejected"); + assert!(err.to_string().contains("proposal_id too long")); + } + + #[test] + fn chat_tool_call_accepts_expanded_read_only_bridge_tools() { + for call in [ + providers::ChatToolCall { + id: None, + name: "bridge_snapshot".to_owned(), + arguments: serde_json::json!({}), + }, + providers::ChatToolCall { + id: None, + name: "service_logs".to_owned(), + arguments: serde_json::json!({ + "service_id": "svc-qwen", + "lines": 120 + }), + }, + providers::ChatToolCall { + id: None, + name: "automations".to_owned(), + arguments: serde_json::json!({ "event_limit": 12 }), + }, + providers::ChatToolCall { + id: None, + name: "natural_language_plan".to_owned(), + arguments: serde_json::json!({ "request": "check whether ROCm needs an update" }), + }, + providers::ChatToolCall { + id: None, + name: "port_status".to_owned(), + arguments: serde_json::json!({ "host": "127.0.0.1", "port": 8188 }), + }, + providers::ChatToolCall { + id: None, + name: "update_check".to_owned(), + arguments: serde_json::json!({}), + }, + ] { + validate_chat_tool_call(&call).expect("read-only bridge tool should validate"); + assert!( + chat_tool_call_is_read_only(&call), + "{} should be read-only", + call.name + ); + } + } + + #[test] + fn chat_tool_call_accepts_assistant_support_command_shapes() { + for (call, expected_command, read_only) in [ + ( + providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ "args": ["examine"] }), + }, + Some("rocm examine"), + true, + ), + ( + providers::ChatToolCall { + id: None, + name: "gpu_snapshot".to_owned(), + arguments: serde_json::json!({}), + }, + None, + true, + ), + ( + providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ "args": ["model"] }), + }, + Some("rocm model"), + true, + ), + ( + providers::ChatToolCall { + id: None, + name: "install_sdk".to_owned(), + arguments: serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": "D:\\ROCm\\therock_venvs" + }), + }, + Some( + "rocm install sdk --channel release --format wheel --approve-replacing-active-default --prefix 'D:\\ROCm\\therock_venvs'", + ), + false, + ), + ( + providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ "args": ["comfyui", "install"] }), + }, + Some("rocm comfyui install --yes"), + false, + ), + ( + providers::ChatToolCall { + id: None, + name: "launch_server".to_owned(), + arguments: serde_json::json!({ + "model": "qwen", + "engine": "vllm", + "device": "gpu_required" + }), + }, + Some("rocm serve qwen --managed --engine vllm --device gpu_required"), + false, + ), + ] { + validate_chat_tool_call(&call).expect("assistant support tool should validate"); + assert_eq!( + chat_tool_call_is_read_only(&call), + read_only, + "{}", + call.name + ); + if let Some(expected_command) = expected_command { + assert_eq!( + rocm_chat_tool_requested_command(&call).as_deref(), + Some(expected_command) + ); + } + } + } + + #[test] + fn chat_rocm_command_routes_comfyui_and_engine_actions() { + let comfy_install = providers::ChatToolCall { + id: Some("call-comfy".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["comfyui", "install"], + "reason": "The user asked me to install ComfyUI." + }), + }; + validate_chat_tool_call(&comfy_install).expect("ComfyUI install should validate"); + assert!(!chat_tool_call_is_read_only(&comfy_install)); + assert_eq!( + rocm_chat_tool_requested_command(&comfy_install).as_deref(), + Some("rocm comfyui install --yes") + ); + let approval = chat_tool_approval_request(&comfy_install, Some("Install ComfyUI now.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Install ComfyUI"); + assert_eq!(approval.command_title, "ComfyUI"); + assert_eq!( + approval.args, + vec![ + "comfyui".to_owned(), + "install".to_owned(), + "--yes".to_owned() + ] + ); + + let lemonade = providers::ChatToolCall { + id: Some("call-lemonade".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["engines", "install", "lemonade"] + }), + }; + validate_chat_tool_call(&lemonade).expect("lemonade engine install should validate"); + assert!(!chat_tool_call_is_read_only(&lemonade)); + assert_eq!( + rocm_chat_tool_requested_command(&lemonade).as_deref(), + Some("rocm engines install lemonade") + ); + let approval = + chat_tool_approval_request(&lemonade, Some("Install Lemonade for local serving.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Install engine"); + assert_eq!(approval.command_title, "Engine"); + + let vllm = providers::ChatToolCall { + id: Some("call-vllm".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["engines", "install", "vllm"] + }), + }; + validate_chat_tool_call(&vllm).expect("vLLM engine install should validate"); + assert!(!chat_tool_call_is_read_only(&vllm)); + assert_eq!( + rocm_chat_tool_requested_command(&vllm).as_deref(), + Some("rocm engines install vllm") + ); + let approval = chat_tool_approval_request(&vllm, Some("Install vLLM for Linux/WSL.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Install engine"); + assert_eq!(approval.command_title, "Engine"); + + let comfy_start = providers::ChatToolCall { + id: Some("call-comfy-start".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["comfyui", "start"] + }), + }; + validate_chat_tool_call(&comfy_start).expect("ComfyUI start should validate"); + assert!(!chat_tool_call_is_read_only(&comfy_start)); + let approval = chat_tool_approval_request(&comfy_start, Some("Start ComfyUI locally.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Start ComfyUI"); + assert_eq!(approval.command_title, "ComfyUI"); + + let serve = providers::ChatToolCall { + id: Some("call-serve".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "qwen", "--engine", "vllm", "--device", "gpu_required", "--managed"] + }), + }; + validate_chat_tool_call(&serve).expect("managed serve should validate"); + assert!(!chat_tool_call_is_read_only(&serve)); + assert_eq!( + rocm_chat_tool_requested_command(&serve).as_deref(), + Some("rocm serve qwen --engine vllm --device gpu_required --managed") + ); + let approval = chat_tool_approval_request(&serve, Some("Start the recommended assistant.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Start local model server"); + assert_eq!(approval.command_title, "Serve"); + + let vllm_serve = providers::ChatToolCall { + id: Some("call-vllm-serve".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "Qwen/Qwen3.5-4B", "--engine", "vllm", "--device", "gpu_required", "--managed"] + }), + }; + validate_chat_tool_call(&vllm_serve).expect("managed vLLM serve should validate"); + assert!(!chat_tool_call_is_read_only(&vllm_serve)); + assert_eq!( + rocm_chat_tool_requested_command(&vllm_serve).as_deref(), + Some("rocm serve Qwen/Qwen3.5-4B --engine vllm --device gpu_required --managed") + ); + + let config = providers::ChatToolCall { + id: Some("call-config".to_owned()), + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["config", "set-default-engine", "vllm"] + }), + }; + validate_chat_tool_call(&config).expect("config change should validate"); + assert!(!chat_tool_call_is_read_only(&config)); + let approval = chat_tool_approval_request(&config, Some("Use vLLM as the default engine.")) + .expect("approval should be built"); + assert_eq!(approval.pending_title, "Change settings"); + assert_eq!(approval.command_title, "Config"); + } + + #[test] + fn setup_status_is_read_only() { + for args in [ + vec!["setup".to_owned()], + vec!["setup".to_owned(), "status".to_owned()], + ] { + let action = + chat_rocm_command_action_from_args(args.clone()).expect("setup status classifies"); + assert!( + matches!(action, ChatRocmCommandAction::ReadOnly(_)), + "setup {args:?} should be read-only, got {action:?}" + ); + } + } + + #[test] + fn storage_report_is_read_only_and_removal_requires_approval() { + for args in [ + vec!["storage".to_owned()], + vec!["storage".to_owned(), "report".to_owned()], + ] { + let action = chat_rocm_command_action_from_args(args.clone()) + .expect("storage report classifies"); + assert!( + matches!(action, ChatRocmCommandAction::ReadOnly(_)), + "storage {args:?} only measures folders, so it should be read-only, got {action:?}" + ); + } + + for verb in ["remove-old-installs", "remove-downloads"] { + let action = + chat_rocm_command_action_from_args(vec!["storage".to_owned(), verb.to_owned()]) + .expect("storage removal classifies"); + match action { + ChatRocmCommandAction::Approval { args, .. } => { + assert!( + args.iter().any(|arg| arg == "--yes"), + "approved removal runs non-interactively: {args:?}" + ); + } + other @ ChatRocmCommandAction::ReadOnly(_) => { + panic!("storage {verb} must require approval, got {other:?}") + } + } + } + } + + #[test] + fn setup_reset_requires_approval() { + let action = + chat_rocm_command_action_from_args(vec!["setup".to_owned(), "reset".to_owned()]) + .expect("setup reset classifies"); + match action { + ChatRocmCommandAction::Approval { + pending_title, + command_title, + .. + } => { + assert_eq!(pending_title, "Reset first-time setup"); + assert_eq!(command_title, "Setup"); + } + other @ ChatRocmCommandAction::ReadOnly(_) => { + panic!("setup reset should require approval, got {other:?}") + } + } + } + + #[test] + fn proposal_action_show_is_read_only() { + let call = providers::ChatToolCall { + id: None, + name: "proposal_action".to_owned(), + arguments: serde_json::json!({ "proposal_id": "p1", "action": "show" }), + }; + validate_chat_tool_call(&call).expect("show validates"); + assert!( + chat_tool_call_is_read_only(&call), + "proposal_action show must be read-only" + ); + } + + #[test] + fn proposal_action_approve_requires_approval() { + for action in ["approve", "reject"] { + let call = providers::ChatToolCall { + id: None, + name: "proposal_action".to_owned(), + arguments: serde_json::json!({ "proposal_id": "p1", "action": action }), + }; + validate_chat_tool_call(&call).expect("approve/reject validates"); + assert!( + !chat_tool_call_is_read_only(&call), + "proposal_action {action} must NOT be read-only" + ); + let req = chat_tool_approval_request(&call, None).unwrap_or_else(|err| { + panic!("proposal_action {action} should need approval: {err}") + }); + assert_eq!(req.command_title, "Reviews"); + assert!( + req.pending_title.contains("proposal") || req.pending_title.contains("Proposal") + ); + assert!( + req.display_command + .as_deref() + .unwrap_or_default() + .contains("p1"), + "display command should show the proposal id" + ); + } + } + + #[test] + fn proposal_action_rejects_unknown_action() { + let call = providers::ChatToolCall { + id: None, + name: "proposal_action".to_owned(), + arguments: serde_json::json!({ "proposal_id": "p1", "action": "delete" }), + }; + assert!( + validate_chat_tool_call(&call).is_err(), + "unknown proposal_action `action` must be rejected" + ); + } + + #[test] + fn config_set_permissions_classifies_as_approval() { + // Permission escalation MUST route through approval — the classifier + // routes any `config ` (catch-all) to Approval. Verify both modes. + for mode in ["full_access", "ask"] { + let action = chat_rocm_command_action_from_args(vec![ + "config".to_owned(), + "set-permissions".to_owned(), + mode.to_owned(), + ]) + .expect("config set-permissions classifies"); + match action { + ChatRocmCommandAction::Approval { command_title, .. } => { + assert_eq!(command_title, "Config"); + } + other @ ChatRocmCommandAction::ReadOnly(_) => { + panic!("config set-permissions {mode} must need approval, got {other:?}") + } + } + } + } + + #[test] + fn watcher_validator_rejects_unknown_and_invalid_mode() { + // Unknown watcher id → rejected. + let unknown = providers::ChatToolCall { + id: None, + name: "watcher_enable".to_owned(), + arguments: serde_json::json!({ "watcher": "no-such-watcher" }), + }; + assert!( + validate_chat_watcher_tool_call(&unknown, true).is_err(), + "unknown watcher must be rejected" + ); + // Invalid mode → rejected. + let bad_mode = providers::ChatToolCall { + id: None, + name: "watcher_enable".to_owned(), + arguments: serde_json::json!({ "watcher": "therock-update", "mode": "rampage" }), + }; + assert!( + validate_chat_watcher_tool_call(&bad_mode, true).is_err(), + "invalid watcher mode must be rejected" + ); + // Valid watcher + valid mode → accepted. + let ok = providers::ChatToolCall { + id: None, + name: "watcher_enable".to_owned(), + arguments: serde_json::json!({ "watcher": "therock-update", "mode": "observe" }), + }; + validate_chat_watcher_tool_call(&ok, true).expect("valid watcher+mode accepted"); + // Disable must reject a `mode`. + let disable_with_mode = providers::ChatToolCall { + id: None, + name: "watcher_disable".to_owned(), + arguments: serde_json::json!({ "watcher": "therock-update", "mode": "observe" }), + }; + assert!( + validate_chat_watcher_tool_call(&disable_with_mode, false).is_err(), + "disable must reject a mode argument" + ); + } + + #[test] + fn lifecycle_read_mutate_split_is_honest() { + let read_only = [ + vec!["update".to_owned()], + vec!["comfyui".to_owned(), "status".to_owned()], + vec!["comfyui".to_owned(), "logs".to_owned()], + vec!["uninstall".to_owned(), "--dry-run".to_owned()], + vec!["setup".to_owned(), "status".to_owned()], + vec![ + "runtimes".to_owned(), + "uninstall".to_owned(), + "old-runtime".to_owned(), + "--dry-run".to_owned(), + ], + vec![ + "runtimes".to_owned(), + "remove".to_owned(), + "old-runtime".to_owned(), + "--dry-run".to_owned(), + ], + // Must agree with the daemon's `ensure_rocm_command_is_read_only`. + // Its comment claims the two are mirrored, and nothing enforced + // that — the arm was added there and not here, and the classifiers + // disagreed until a reviewer noticed. + vec!["remote".to_owned(), "targets".to_owned()], + vec![ + "remote".to_owned(), + "doctor".to_owned(), + "gpu-box".to_owned(), + ], + vec!["remote".to_owned(), "status".to_owned()], + ]; + for args in read_only { + let action = chat_rocm_command_action_from_args(args.clone()) + .unwrap_or_else(|err| panic!("{args:?} should classify: {err}")); + assert!( + matches!(action, ChatRocmCommandAction::ReadOnly(_)), + "{args:?} should be read-only, got {action:?}" + ); + } + + // Paired with whether the command has a `--yes` to inject. The flag + // exists to keep a consent prompt from hanging a null-stdin spawn, so + // the demand only makes sense for commands that would prompt — and + // injecting it where clap defines no such flag would make the spawn fail + // to parse rather than succeed unattended. + let mutating = [ + (vec!["update".to_owned(), "--apply".to_owned()], true), + // These start, publish or tear down on another machine. They take no + // `--yes` because they never prompt: consent is the approval step + // itself, and every destructive choice they make is already settled + // by an explicit flag (`remote stop --force`). Give any of them an + // interactive prompt and it needs a consent flag here too. + ( + vec![ + "remote".to_owned(), + "serve".to_owned(), + "gpu-box".to_owned(), + "m".to_owned(), + ], + false, + ), + ( + vec!["remote".to_owned(), "attach".to_owned(), "sess".to_owned()], + false, + ), + ( + vec!["remote".to_owned(), "stop".to_owned(), "sess".to_owned()], + false, + ), + (vec!["comfyui".to_owned(), "install".to_owned()], true), + (vec!["comfyui".to_owned(), "start".to_owned()], true), + (vec!["comfyui".to_owned(), "stop".to_owned()], true), + (vec!["uninstall".to_owned()], true), + (vec!["setup".to_owned(), "reset".to_owned()], true), + ( + vec![ + "runtimes".to_owned(), + "uninstall".to_owned(), + "old-runtime".to_owned(), + ], + true, + ), + ( + vec![ + "runtimes".to_owned(), + "remove".to_owned(), + "old-runtime".to_owned(), + ], + true, + ), + ]; + for (args, expects_yes) in mutating { + let action = chat_rocm_command_action_from_args(args.clone()) + .unwrap_or_else(|err| panic!("{args:?} should classify: {err}")); + match &action { + ChatRocmCommandAction::Approval { args, .. } => { + assert_eq!( + args.iter().any(|arg| arg == "--yes"), + expects_yes, + "{args:?} disagrees with whether the approval path should \ + carry --yes" + ); + } + other @ ChatRocmCommandAction::ReadOnly(_) => { + panic!("{args:?} should require approval, got {other:?}") + } + } + } + } + + #[test] + fn install_sdk_chat_and_mcp_args_approve_the_replacement_for_a_non_interactive_spawn() { + // The chat/MCP surfaces spawn `rocm` with null stdin, so the consent + // prompt would refuse with "re-run with + // `--approve-replacing-active-default`" — a flag the user has no way to + // supply from chat or the dashboard. Both the chat classifier + // arm and the MCP tool-args builder must inject the consent so an + // install over the active default runtime is not silently refused. + let action = chat_rocm_command_action_from_args(vec![ + "install".to_owned(), + "sdk".to_owned(), + "--channel".to_owned(), + "release".to_owned(), + // The classifier requires a user-chosen (non-system) install folder. + "--prefix".to_owned(), + "/home/tester/rocm-managed".to_owned(), + ]) + .expect("install sdk classifies"); + let chat_args = match action { + ChatRocmCommandAction::Approval { args, .. } => args, + other @ ChatRocmCommandAction::ReadOnly(_) => { + panic!("install sdk must require approval, got {other:?}") + } + }; + + // The MCP `install_sdk` tool builds its own argv (it does not route + // through the classifier above), so it must add the flag independently. + let call = providers::ChatToolCall { + id: None, + name: "install_sdk".to_owned(), + arguments: serde_json::json!({ "channel": "release", "format": "wheel" }), + }; + let mcp_args = rocm_chat_tool_requested_args(&call).expect("install_sdk tool builds args"); + + for args in [&chat_args, &mcp_args] { + assert_eq!( + consents_for_install_sdk_argv(args).replace_active_default, + therock::SdkInstallConsent::Preapproved( + therock::SdkInstallApprovalSource::ApproveReplacingActiveDefault + ), + "the spawn must approve replacing the active default, and be credited \ + to the flag it actually passed rather than to --yes, got {args:?}" + ); + } + } + + #[test] + fn chat_rocm_command_runs_read_only_and_rejects_risky_shapes() { + let status = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["rocm", "comfy", "status"] + }), + }; + validate_chat_tool_call(&status).expect("ComfyUI status should validate"); + assert!(chat_tool_call_is_read_only(&status)); + assert_eq!( + rocm_chat_tool_requested_command(&status).as_deref(), + Some("rocm comfyui status") + ); + + let logs = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["comfyui", "logs"] + }), + }; + validate_chat_tool_call(&logs).expect("ComfyUI logs should validate"); + assert!(chat_tool_call_is_read_only(&logs)); + assert_eq!( + rocm_chat_tool_requested_command(&logs).as_deref(), + Some("rocm comfyui logs") + ); + + let cpu = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "tiny.gguf", "--engine", "vllm", "--device", "cpu"] + }), + }; + let error = validate_chat_tool_call(&cpu).unwrap_err().to_string(); + assert!(error.contains("CPU execution")); + + let public_flag = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "tiny.gguf", "--engine", "vllm", "--allow-public-bind", "--managed"] + }), + }; + let error = validate_chat_tool_call(&public_flag) + .unwrap_err() + .to_string(); + assert!(error.contains("public network binding")); + + let foreground = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "qwen", "--engine", "vllm", "--foreground"] + }), + }; + let error = validate_chat_tool_call(&foreground) + .unwrap_err() + .to_string(); + assert!(error.contains("--managed")); + + let shell = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["powershell", "-Command", "whoami"] + }), + }; + let error = validate_chat_tool_call(&shell).unwrap_err().to_string(); + assert!(error.contains("unsupported rocm command")); + } + + /// The `install driver` chat action strips `--yes` in the attached form too, + /// like the `install sdk` arm: read-only where the command only reports, and + /// a single bare `--yes` where it installs. + #[test] + fn chat_install_driver_strips_a_model_supplied_yes_in_both_forms() { + for supplied in ["--yes", "--yes=true", "--yes=1", "--yes=false"] { + let argv = |extra: &str| -> Vec { + ["install", "driver", "--dry-run", extra] + .iter() + .map(|arg| (*arg).to_owned()) + .collect() + }; + let ChatRocmCommandAction::ReadOnly(args) = + chat_install_driver_action(argv(supplied), true) + else { + panic!("a report-only platform must stay read-only"); + }; + assert_eq!(args, ["install", "driver", "--dry-run"], "{supplied}"); + + let ChatRocmCommandAction::Approval { args, .. } = + chat_install_driver_action(argv(supplied), false) + else { + panic!("an installing platform must ask approval"); + }; + assert_eq!( + args, + ["install", "driver", "--dry-run", "--yes"], + "{supplied}" + ); + } + let ChatRocmCommandAction::ReadOnly(args) = chat_install_driver_action( + ["install", "driver", "--yes-not-a-flag"] + .iter() + .map(|arg| (*arg).to_owned()) + .collect(), + true, + ) else { + panic!("read-only expected"); + }; + assert!(args.iter().any(|arg| arg == "--yes-not-a-flag"), "{args:?}"); + } + + #[test] + fn chat_tool_call_rejects_bad_service_and_watcher_suggestions() { + for (call, expected) in [ + ( + providers::ChatToolCall { + id: None, + name: "service_logs".to_owned(), + arguments: serde_json::json!({ "service_id": "bad/name" }), + }, + "must not contain path separators", + ), + ( + providers::ChatToolCall { + id: None, + name: "automations".to_owned(), + arguments: serde_json::json!({ "event_limit": 1000 }), + }, + "between 1 and 64", + ), + ( + providers::ChatToolCall { + id: None, + name: "watcher_enable".to_owned(), + arguments: serde_json::json!({ + "watcher": "unknown", + "mode": "propose" + }), + }, + "unknown watcher", + ), + ( + providers::ChatToolCall { + id: None, + name: "watcher_disable".to_owned(), + arguments: serde_json::json!({ + "watcher": "server-recover", + "mode": "propose" + }), + }, + "cannot set `mode`", + ), + ] { + let error = validate_chat_tool_call(&call).unwrap_err().to_string(); + assert!(error.contains(expected), "unexpected error: {error}"); + } + } + + #[test] + fn chat_tool_call_rejects_cpu_and_public_bind_server_requests() { + let cpu = providers::ChatToolCall { + id: None, + name: "launch_server".to_owned(), + arguments: serde_json::json!({ + "model": "tiny.gguf", + "engine": "vllm", + "device": "cpu" + }), + }; + let error = validate_chat_tool_call(&cpu).unwrap_err().to_string(); + assert!(error.contains("CPU execution")); + + let public = providers::ChatToolCall { + id: None, + name: "launch_server".to_owned(), + arguments: serde_json::json!({ + "model": "tiny.gguf", + "engine": "vllm", + "host": "0.0.0.0", + "allow_public_bind": true + }), + }; + let error = validate_chat_tool_call(&public).unwrap_err().to_string(); + assert!(error.contains("public network binding")); + + let host = providers::ChatToolCall { + id: None, + name: "launch_server".to_owned(), + arguments: serde_json::json!({ + "model": "tiny.gguf", + "engine": "vllm", + "host": "0.0.0.0" + }), + }; + let error = validate_chat_tool_call(&host).unwrap_err().to_string(); + assert!(error.contains("non-local host")); + } + + #[test] + fn chat_tool_call_rejects_bad_install_suggestions() { + for (arguments, expected) in [ + ( + serde_json::json!({ "channel": "stable" }), + "unsupported TheRock channel", + ), + ( + serde_json::json!({ "format": "zip" }), + "unsupported TheRock install format", + ), + ( + serde_json::json!({ + "prefix": if cfg!(windows) { "C:\\Windows\\rocm" } else { "/opt/rocm" } + }), + "system install folder", + ), + ( + serde_json::json!({ + "version": "7.13.0a20260605", + "build_date": "2026-06-05" + }), + "both `version` and `build_date`", + ), + ( + serde_json::json!({ "build_date": "not-a-date" }), + "build date", + ), + ( + serde_json::json!({ + "format": "tarball", + "build_date": "2026-06-05" + }), + if cfg!(windows) { + "tarball` installs on Windows" + } else { + "specific TheRock wheel versions" + }, + ), + ] { + let call = providers::ChatToolCall { + id: None, + name: "install_sdk".to_owned(), + arguments, + }; + let error = validate_chat_tool_call(&call).unwrap_err().to_string(); + assert!(error.contains(expected), "unexpected error: {error}"); + } + } + + /// Guard: every read-only tool the dash side registers must have a home in + /// the bin's accept-list. A name-only tool (the `doctor` regression caught + /// in review) would otherwise fail end-to-end as "unsupported ROCm tool" + /// while passing the dash-side, name-against-itself completeness checks. + /// + /// We assert the NAME is accepted — not that empty args validate — so the + /// check stays hermetic (no real `BinToolExecutor::execute` / live I/O) and + /// independent of each tool's argument schema. + #[test] + fn read_tool_names_are_subset_of_bin_accept_list() { + for name in rocm_dash_tui::agent::ROCM_READ_TOOL_NAMES { + let call = providers::ChatToolCall { + id: None, + name: name.to_owned(), + arguments: serde_json::json!({}), + }; + if let Err(error) = validate_chat_tool_call(&call) { + let message = error.to_string(); + assert!( + !message.contains("unsupported ROCm tool"), + "ROCM_READ_TOOL_NAMES advertises `{name}` but the bin rejects \ + the name as unsupported: {message}" + ); + } + } + } + + #[test] + fn path_exists_chat_tool_is_read_only() { + let call = providers::ChatToolCall { + id: Some("path-check".to_owned()), + name: "path_exists".to_owned(), + arguments: serde_json::json!({ "path": "D:\\ROCm\\temp" }), + }; + + validate_chat_tool_call(&call).unwrap(); + assert!(chat_tool_call_is_read_only(&call)); + } + + #[test] + fn port_status_chat_tool_is_read_only_and_loopback_only() { + let call = providers::ChatToolCall { + id: Some("port-check".to_owned()), + name: "port_status".to_owned(), + arguments: serde_json::json!({ "host": "127.0.0.1", "port": 8188 }), + }; + + validate_chat_tool_call(&call).unwrap(); + assert!(chat_tool_call_is_read_only(&call)); + + let public = providers::ChatToolCall { + id: Some("public-port-check".to_owned()), + name: "port_status".to_owned(), + arguments: serde_json::json!({ "host": "192.168.1.10", "port": 8188 }), + }; + let error = validate_chat_tool_call(&public).unwrap_err().to_string(); + assert!(error.contains("non-local host"), "{error}"); + } + + /// The same question — "is this a system location?" — also keeps the local + /// assistant from installing into one, and there the folder arrives as + /// free-form JSON from a model rather than from a file the user wrote. A + /// trailing separator is exactly the sort of thing generated text carries, + /// so the refusal has to survive every spelling here too. + #[test] + #[cfg(unix)] + fn the_assistant_cannot_name_a_system_install_folder_by_respelling_it() { + let home = rocm_core::runtime_home_dir().expect("a home directory"); + let home = home.display().to_string(); + let spellings = [ + "/usr/".to_owned(), + "/opt/".to_owned(), + "//usr".to_owned(), + "/./opt".to_owned(), + format!("{home}/../../usr"), + ]; + + let accepted: Vec = spellings + .into_iter() + .filter(|text| !chat_install_prefix_is_system(Path::new(text))) + .collect(); + + assert!( + accepted.is_empty(), + "accepted as a user install folder: {accepted:?}" + ); + } +} diff --git a/docs/architecture.md b/docs/architecture.md index a5f06fe1ba..41d2ef45cf 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -32,7 +32,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every - [`automations.rs`](../apps/rocm/src/automations.rs) — mechanical relocation, owns private types (Phase 6f, ROCMAI-210): the `rocm automations` dispatch handler (`automations()`) plus the `rocm automations` status report it prints (`render_automations_text`), the `ProposalKind` classification that report renders through (`proposal_kind`, `fallback_tool_for_automation_action`, `proposal_plain_summary`, `proposal_plain_reason`), the small plain-language label helpers (`proposal_status_label`, `proposal_bool_argument`, `automation_event_plain_summary`, `audit_event_plain_summary`, `watcher_mode_plain_label`, `watcher_last_check`, `watcher_plain_name`, `watcher_plain_trigger`, `watcher_plain_action`, `watcher_policy_note`), and the unrelated `rocm daemon --status` summary (`render_daemon_text`). `render_automations_text` and `render_daemon_text` are `pub(crate)`: [`main.rs`](../apps/rocm/src/main.rs)'s `dispatch()` calls `render_daemon_text` directly (re-imported alongside `automations` via `use crate::automations::{automations, render_daemon_text};`; `render_automations_text` has no `dispatch()` call site of its own, reached only through `automations()` above, now local to this file), and [`chat.rs`](../apps/rocm/src/chat.rs)'s deterministic tool summaries call both. Every other item in this list, including `watcher_policy_note` (called from `automations()`'s `Enable` arm and from `render_automations_text`, both in this same file), has no caller outside this module and stays private. The cluster's regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::` the same way other mechanically relocated modules do. - [`bootstrap.rs`](../apps/rocm/src/bootstrap.rs) — a further-extracted variant of full domain extraction: it owns its clap command enum (`BootstrapCommand`) and dispatch function too, rather than leaving them in [`main.rs`](../apps/rocm/src/main.rs). - [`bridge_snapshot.rs`](../apps/rocm/src/bridge_snapshot.rs) — mechanical relocation, no owned types: the host snapshot behind the internal bridge surface (`build_codex_bridge_snapshot`, `build_codex_bridge_gpu_snapshot`, `builtin_codex_bridge_engine_inventory`) and the table of built-in engine ids it derives the engine list from (`builtin_engine_inventory`, a separate table from `main.rs`'s `engine_inventory`; the two are not merged). All four are `pub(crate)`: [`main.rs`](../apps/rocm/src/main.rs)'s `dispatch()` calls `build_codex_bridge_snapshot` through a re-import, [`chat.rs`](../apps/rocm/src/chat.rs)'s internal MCP tools reach the first three as `crate::` through the same re-import, and a `main.rs` test checks `--engine`'s help against `builtin_engine_inventory`. It has no tests of its own. -- [`chat.rs`](../apps/rocm/src/chat.rs) — full domain extraction (Phase 6d, ROCMAI-88), mirrors [`providers.rs`](../apps/rocm/src/providers.rs). Owns the chat/MCP-bridge cluster's types (`ChatRocmCommandAction`, `DeterministicModelRecipe`, `DeterministicServiceRow`, `DeterministicEngineRow`, and others — all module-private except `ChatToolApprovalRequest`, reached by `dash_seam.rs` as `chat_tool_approval_request`'s return type, and `ChatRocmCommandAction`, read by `driver_plan.rs`'s test module), its validation/approval/execution logic (`validate_chat_tool_call` and the `validate_chat_*_tool_call` family), deterministic tool-output summarization (including `render_services_tool_result_text`), and the MCP tool-bridge entry points (`run_internal_mcp_call` and its helpers). Callers in other modules, production and test-only, reach it through the `chat::` module boundary. As a child module of the crate root (`mod chat;` in `main.rs`), Rust's module-privacy rule already makes most of `main.rs`'s private helpers visible to `chat.rs` without any visibility change, when `chat.rs` calls into `main.rs`. The other direction needs real widening: `chat.rs`'s own functions reached from other modules — `run_internal_mcp_call` (a production call from `main.rs`), `rocm_chat_tool_requested_args` (reached only by `runtime_install_finalize.rs`'s own test module), among others — stay `pub(crate)`; the third PR's visibility audit tightened every other function to module-private, so each function still `pub(crate)` has a confirmed external caller, production or test-only. The cluster's regression tests live in `chat.rs`'s own `mod tests`, moved out of `main.rs`'s shared `mod tests` block across the second and third PRs; `serve_negative_sampling_space_form_reaches_range_validator`, a shared clap value-parser wiring test living alongside its sibling command-parsing tests, stays in `main.rs`; the four structural checks that read [`chat_cmd.rs`](../apps/rocm/src/chat_cmd.rs)'s source for the `Command::Chat` handler's wiring (`command_chat_interactive_routes_to_dash_run_chat_not_tui`, `command_chat_interactive_notes_dropped_provider_flag`, `command_chat_honors_chat_mock_and_keeps_prompt_passthrough`, `command_chat_reads_prompt_from_piped_stdin`) live in that module's own `mod tests`. +- [`chat.rs`](../apps/rocm/src/chat.rs) — full domain extraction (Phase 6d, ROCMAI-88), mirrors [`providers.rs`](../apps/rocm/src/providers.rs). Owns the chat/MCP-bridge cluster's types (`DeterministicModelRecipe`, `DeterministicServiceRow`, `DeterministicEngineRow`, and others — all module-private), its execution logic, deterministic tool-output summarization (including `render_services_tool_result_text`), and the MCP tool-bridge entry points (`run_internal_mcp_call` and its helpers). The tool-call validation and approval policy (`validate_chat_tool_call`, the `validate_chat_*_tool_call` family, `ChatRocmCommandAction`, `ChatToolApprovalRequest`) lives in [`tool_policy.rs`](../apps/rocm/src/tool_policy.rs), which this module calls into and re-exports from. Callers in other modules, production and test-only, reach this module's items, and the re-exported ones, through the `chat::` module boundary. As a child module of the crate root (`mod chat;` in `main.rs`), Rust's module-privacy rule already makes most of `main.rs`'s private helpers visible to `chat.rs` without any visibility change, when `chat.rs` calls into `main.rs`. The other direction needs real widening: `chat.rs`'s own functions reached from other modules — `run_internal_mcp_call` (a production call from `main.rs`), `deterministic_comfyui_tool_summary` (reached only by `comfyui.rs`'s own test module), among others — stay `pub(crate)`; the third PR's visibility audit tightened every other function to module-private, so each function still `pub(crate)` has a confirmed external caller, production or test-only. The cluster's regression tests live in `chat.rs`'s own `mod tests`, moved out of `main.rs`'s shared `mod tests` block across the second and third PRs; `serve_negative_sampling_space_form_reaches_range_validator`, a shared clap value-parser wiring test living alongside its sibling command-parsing tests, stays in `main.rs`; the four structural checks that read [`chat_cmd.rs`](../apps/rocm/src/chat_cmd.rs)'s source for the `Command::Chat` handler's wiring (`command_chat_interactive_routes_to_dash_run_chat_not_tui`, `command_chat_interactive_notes_dropped_provider_flag`, `command_chat_honors_chat_mock_and_keeps_prompt_passthrough`, `command_chat_reads_prompt_from_piped_stdin`) live in that module's own `mod tests`. - [`chat_cmd.rs`](../apps/rocm/src/chat_cmd.rs) — mechanical relocation, dispatch-adjacent, no owned types: `rocm chat`'s command handler (`run_chat_command`, the body of the `Command::Chat` arm of `dispatch()`), which hands off to the dashboard chat (`dash::run_chat`) in a terminal with no `--prompt` and otherwise prints the reply or status screen through `chat::render_chat_prompt_text`/`chat::render_chat_text`. It is a module of its own, not part of [`chat.rs`](../apps/rocm/src/chat.rs), because it calls into both `chat.rs` and [`dash.rs`](../apps/rocm/src/dash.rs) while `dash.rs` already calls into `chat.rs`; this module depends on both and neither depends on it. `Provider` and `ChatInferenceParams` stay in [`main.rs`](../apps/rocm/src/main.rs); `run_chat_command` is `pub(crate)` and re-imported there for `dispatch()`. Four source-text tests in this module's own `mod tests` read this file and assert on the handler's text, so a rename of the function or of the calls they look for has to be made there too. - [`chat_host_facts.rs`](../apps/rocm/src/chat_host_facts.rs) — full domain extraction. - [`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::`. @@ -53,19 +53,21 @@ Each per-crate module inventory lists one file per line. Flat inventories (every - [`engines_cmd.rs`](../apps/rocm/src/engines_cmd.rs) — mechanical relocation, owns private types (Phase 6a): `engines()` (the `rocm engines` command), `ensure_self_managed_engine_ready` (the readiness check [`serve_cmd.rs`](../apps/rocm/src/serve_cmd.rs) runs for engines that bring their own runtime) and `env_root_for_service` (read from [`service_launch.rs`](../apps/rocm/src/service_launch.rs) and [`services_text.rs`](../apps/rocm/src/services_text.rs)). Which runtime an engine installs into and where its environment lives is answered in [`engine_install_env.rs`](../apps/rocm/src/engine_install_env.rs), which this module imports from. This module also calls into [`runtime_install_finalize.rs`](../apps/rocm/src/runtime_install_finalize.rs) for `settle_engine_install` and the host system-package installs; that module does not import this one. - [`examine_cmd.rs`](../apps/rocm/src/examine_cmd.rs) — mechanical relocation, dispatch-adjacent, owns private types: `rocm examine`'s command handler (`examine()`), which prints the human report through [`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs)'s `examine_human_report`, or with `--json` the document built from the private `ExamineJson`/`ExamineJsonSummary` types (the `Examination` verbatim plus a summary of what the CLI knows but the probe cannot). `examine()` is `pub(crate)` and re-imported in [`main.rs`](../apps/rocm/src/main.rs) for `dispatch()`. No test in the crate calls it or builds the document directly. - [`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs) — mechanical relocation (Phase 6f, ROCMAI-208, folding Phase 6f/ROCMAI-86 closed): the launch-summary/examine text surfaces (`render_launch_summary`, `render_examine_text`, `render_examine_text_with_paths`, `examine_human_report`, `render_examine_plain_header`, `plain_status_label`), the engine-inventory listing shared by `rocm engines` and `rocm examine` (`write_default_engine_legend`, `render_engine_inventory_text_with_paths`, `append_examine_runtime_state`, `append_examine_engine_inventory`), and the model-catalog rendering behind `rocm model` (alias `models`) and `rocm model --verbose` (`model_catalog_platform_heading`, `render_model_registry_text_with_context_and_host`, `model_recipe_memory_label`, `model_recipe_gpu_fit_label`, `render_model_registry_verbose_text_with_context_and_host`, `model_catalog_header`, `append_model_recipe_registry_source`, `append_model_recipe_metadata_lines`, `append_model_engine_recipe_settings_lines`, `format_list_or_none`, `format_string_map_or_none`, `append_model_artifact_lines`, `append_artifact_source_policy_lines`, `artifact_source_policy_label`, `format_bytes`, `append_model_host_ram_fit_lines`, `append_model_fit_lines`, `append_manual_alternative_lines`, `manual_alternative_recommendations`, `recipe_is_manual_fit`, `append_model_engine_support_lines`, `recipe_display_ref`, `preferred_engine_action_target`, `model_registry_reason`, `format_gib`, `append_engine_detect_summary`, `friendly_engine_runtime_kind`, `friendly_engine_detect_notes`, `friendly_engine_detect_note_fallback`, `engine_runtime_status_label`, `engine_runtime_is_native_windows_unsupported`). Two items physically interleaved in this span in `main.rs` moved with it rather than staying behind, since neither has a caller outside this cluster: the `DEFAULT_ENGINE_MARKER` constant (shared by both engine-inventory renderers so the two copies cannot drift apart) and `model_registry_adapter_availability_note` (read only from `append_model_engine_support_lines` and this module's own test); both stay private. `render_config_text` and `telemetry_policy_summary` sat immediately adjacent to this cluster's end boundary in `main.rs` and were explicitly out of scope, so that split left them where they were; they now live in [`config_cmd.rs`](../apps/rocm/src/config_cmd.rs). `dispatch()` calls directly into this module at two points — `render_model_registry_verbose_text_with_context_and_host` and `render_model_registry_text_with_context_and_host` — re-imported via `use crate::examine_report_text::{...};`; [`examine_cmd.rs`](../apps/rocm/src/examine_cmd.rs)'s `examine()`, which the `Command::Examine` arm calls, imports and calls `examine_human_report` itself. `main.rs`'s own `launch_default` calls `render_launch_summary` directly, also re-imported there. `render_examine_text`, `render_examine_text_with_paths`, and `render_model_registry_verbose_text_with_context_and_host` are also `pub(crate)`, read from [`chat.rs`](../apps/rocm/src/chat.rs)'s deterministic tool-output summaries; `render_engine_inventory_text_with_paths` is also read from `chat.rs` and from `engines_cmd.rs`'s `render_engine_inventory_text`; `append_examine_runtime_state` is also `pub(crate)`, read only from [`runtimes_cmd.rs`](../apps/rocm/src/runtimes_cmd.rs)'s own test module, so `main.rs`'s re-import of it is itself `#[cfg(test)]` rather than carrying an otherwise-unused import into a non-test build; `format_bytes` is also `pub(crate)`, read from [`storage.rs`](../apps/rocm/src/storage.rs)'s cache-size reporting and [`automations.rs`](../apps/rocm/src/automations.rs)'s plain-language summaries. Every other item in this list has no caller outside this module and stays private. This cluster also reaches three items that stayed behind in `main.rs` because they already had callers outside it: `engine_inventory()` (also read from [`update_cmd.rs`](../apps/rocm/src/update_cmd.rs)), `engine_ruled_out_by_platform` (also read from [`model_fit.rs`](../apps/rocm/src/model_fit.rs)'s `assess_model_for_host`; it has since moved there and is re-imported in `main.rs`), and `missing_packaged_engine_reason` (already noted in [`engine_ipc.rs`](../apps/rocm/src/engine_ipc.rs)'s entry above as having callers outside that cluster, plus its own test — this cluster is one of them) — all three reached via `use crate::{...};`. The cluster's regression tests live in this module's own `mod tests`, reaching the shared `test_paths` fixture through `crate::tests::` and `write_test_pip_runtime`/`runtime_manifest_path` through `crate::test_support::`/`crate::runtimes_cmd::`, the same way other mechanically relocated modules do. Two tests interleaved among this cluster's tests in `main.rs`'s old shared `mod tests` stayed behind because their primary subject is unrelated: `missing_packaged_engine_reason`'s own test and the whole-command-tree ASCII-help walk (two CLI-lifecycle-audit-log tests that were also left there have since moved to [`cli_audit.rs`](../apps/rocm/src/cli_audit.rs)). -- [`freeform_answer.rs`](../apps/rocm/src/freeform_answer.rs) — mechanical relocation, no owned types: the answers a freeform request can get without running a command — `render_freeform_read_only_answer`, which answers a single approval-free `examine`, `comfyui status` or `comfyui logs` action, and `render_freeform_examine_answer`, the plain-language ROCm status it prints for `examine`. `run_freeform` in [`main.rs`](../apps/rocm/src/main.rs) is the only caller of the first, which is `pub(crate)` and re-imported there; the second is private. Besides `chat.rs` (`any_substring`), it depends on `comfyui.rs`, `runtimes_cmd.rs` and `therock.rs`, none of which calls back into it. No unit test calls either function directly. +- [`freeform_answer.rs`](../apps/rocm/src/freeform_answer.rs) — mechanical relocation, no owned types: the answers a freeform request can get without running a command — `render_freeform_read_only_answer`, which answers a single approval-free `examine`, `comfyui status` or `comfyui logs` action, and `render_freeform_examine_answer`, the plain-language ROCm status it prints for `examine`. `run_freeform` in [`main.rs`](../apps/rocm/src/main.rs) is the only caller of the first, which is `pub(crate)` and re-imported there; the second is private. Besides `chat.rs` (`any_substring`, re-exported there from [`prompt_text.rs`](../apps/rocm/src/prompt_text.rs)), it depends on `comfyui.rs`, `runtimes_cmd.rs` and `therock.rs`, none of which calls back into it. No unit test calls either function directly. - [`freeform_execution.rs`](../apps/rocm/src/freeform_execution.rs) — mechanical relocation, dispatch-adjacent, owns a `pub(crate)` type (as of Phase 6e's follow-up, ROCMAI-726; see the Phase 6a variant note above for the owned-types pattern). Holds the freeform natural-language planner's plan-*execution*/consent half that [`freeform_planner.rs`](../apps/rocm/src/freeform_planner.rs) below deliberately left out: `validate_freeform_execution_action()`, `prepare_freeform_execution()`, `apply_freeform_execution_consent()`, `render_freeform_execution_header()`, and the `pub(crate)` `FreeformExecution` struct that `prepare_freeform_execution` returns and `render_freeform_execution_header` consumes. `main.rs`'s `execute_freeform_next_action()` and `run_freeform()` stay in `main.rs` — both are dispatch-level, not part of deciding whether a plan's next action is safe to execute — and `execute_freeform_next_action()` calls in via the unqualified re-import `use crate::freeform_execution::{prepare_freeform_execution, render_freeform_execution_header};` (`run_freeform()` only calls `execute_freeform_next_action()`, never this module directly), matching [`driver_install.rs`](../apps/rocm/src/driver_install.rs)'s dispatch-adjacent form rather than `freeform_planner.rs`'s own qualified-path form. `prepare_freeform_execution` takes the already-built `plan` by value rather than rebuilding it from `request`/`paths`/`config` (ROCMAI-725's single-build guarantee, inherited unchanged from `main.rs`); `prepare_freeform_execution`/`render_freeform_execution_header` are `pub(crate)` for that cross-module call; `FreeformExecution` was already `pub(crate)` (read directly by `main.rs` and this module's own tests); `validate_freeform_execution_action`/`apply_freeform_execution_consent` have no caller outside this module and stay module-private, unchanged from before the move. The cluster's six regression tests moved out of `main.rs`'s shared `mod tests` into this module's own `mod tests`, inheriting the qualified `freeform_planner::` path they already used to reach `freeform_planner.rs`'s items as `main.rs`'s former tests — this module reaches in from outside using the same qualified path `main.rs` and `providers.rs`'s tests use (unlike `chat.rs`/`comfyui.rs`, which import it unqualified). -- [`freeform_planner.rs`](../apps/rocm/src/freeform_planner.rs) — full domain extraction (Phase 6e, ROCMAI-87). Owns the freeform natural-language planner cluster's types (`StructuredRequestPlan`, `PlannerIntent`, `PlannedToolCall`, `FreeformPlanAction`, `ProviderPlannerResponse`, `ProviderPlannerToolCall`, all module-private except `FreeformPlanAction`, `StructuredRequestPlan` and `PlannedToolCall`, which are `pub(crate)`, and `ProviderPlannerToolCall`, `pub(crate)` together with `validate_provider_planner_tool_call` and `planner_failure_note` so [`chat.rs`](../apps/rocm/src/chat.rs)'s command-preview property tests can drive the planner's guards), the deterministic plan builder (`build_freeform_plan*`), the provider-assisted path (`resolve_freeform_plan_with_provider`, `build_provider_planner_prompt`, `provider_planner_response_to_plan`, `validate_provider_planner_tool_call`), and plan rendering (`render_structured_request_plan`). [`main.rs`](../apps/rocm/src/main.rs) calls `build_freeform_plan_with_context`/`render_structured_request_plan` directly for the dispatch/review flow ([`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs)'s `render_launch_summary` → `host_default_engine` is another call into this module, unrelated to planning: bare `rocm`'s non-interactive launch summary), while [`freeform_execution.rs`](../apps/rocm/src/freeform_execution.rs) above calls `plan_next_action`/`format_structured_tool_call` for the execution flow and reads `FreeformPlanAction`/`StructuredRequestPlan` by type for its execution-validation functions — those live there, not here, because they decide whether a plan's next action is safe to *execute*, a dispatch-adjacent concern distinct from *building* the plan; `main.rs`'s `run_freeform` builds a plan exactly once via `build_freeform_plan_with_context` and threads that same value through `execute_freeform_next_action` to `prepare_freeform_execution` rather than rebuilding it (ROCMAI-725: building a plan can call a configured cloud `planner_provider`, which is not guaranteed deterministic, so a second independent build could silently disagree with the first). [`chat.rs`](../apps/rocm/src/chat.rs)'s `natural_language_plan` MCP tool gets the same single-build guarantee from `plan_freeform_request_with_context`, which builds once and returns both the rendered text and the next action; `chat.rs` also calls `format_structured_tool_call` directly, like [`comfyui.rs`](../apps/rocm/src/comfyui.rs) does, and its `validate_chat_rocm_command_safety` reads `serve_args_request_cpu_device` directly as a no-CPU-fallback safety check on provider-proposed `serve` args. `host_default_engine`, `build_freeform_plan_with_context`, `render_structured_request_plan`, `plan_next_action`, `plan_freeform_request_with_context`, `provider_planner_response_to_plan`, `format_structured_tool_call`, and `serve_args_request_cpu_device` are `pub(crate)` for these reasons, as are `infer_engine_from_request` (read from `chat.rs`) and `plan_driver_request` (read from [`driver_plan.rs`](../apps/rocm/src/driver_plan.rs)'s test module); `freeform_plan_next_action` and `render_freeform_plan` are test-only (`#[cfg(test)]`) and `FreeformPlanAction` was already `pub(crate)` before the split. `parse_freeform_invocation`/`should_treat_as_freeform` (deciding whether an invocation is freeform at all) stay in `main.rs`, a dispatch-level concern outside both this module and `freeform_execution.rs`. The cluster's regression tests live in `freeform_planner.rs`'s own `mod tests`; the handful of `main.rs` tests that exercise the invocation classifier/clap-suggestion-avoidance stay in `main.rs`'s shared `mod tests`, since neither is specific to this module or to `freeform_execution.rs` (and neither touches `freeform_planner::` at all), and the ROCMAI-725 regression tests proving the single-build guarantee live in `providers.rs`'s `mod tests` (reusing its mock local-provider server), reaching this module's items through the qualified `freeform_planner::` path like any other external caller. +- [`freeform_planner.rs`](../apps/rocm/src/freeform_planner.rs) — full domain extraction (Phase 6e, ROCMAI-87). Owns the freeform natural-language planner cluster's types (`StructuredRequestPlan`, `PlannerIntent`, `PlannedToolCall`, `FreeformPlanAction`, `ProviderPlannerResponse`, `ProviderPlannerToolCall`, all module-private except `FreeformPlanAction`, `StructuredRequestPlan` and `PlannedToolCall`, which are `pub(crate)`, and `ProviderPlannerToolCall`, `pub(crate)` together with `validate_provider_planner_tool_call` and `planner_failure_note` so [`chat.rs`](../apps/rocm/src/chat.rs)'s command-preview property tests can drive the planner's guards), the deterministic plan builder (`build_freeform_plan*`), the provider-assisted path (`resolve_freeform_plan_with_provider`, `build_provider_planner_prompt`, `provider_planner_response_to_plan`, `validate_provider_planner_tool_call`), and plan rendering (`render_structured_request_plan`). [`main.rs`](../apps/rocm/src/main.rs) calls `build_freeform_plan_with_context`/`render_structured_request_plan` directly for the dispatch/review flow ([`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs)'s `render_launch_summary` → `host_default_engine` is another call into this module, unrelated to planning: bare `rocm`'s non-interactive launch summary), while [`freeform_execution.rs`](../apps/rocm/src/freeform_execution.rs) above calls `plan_next_action`/`format_structured_tool_call` for the execution flow and reads `FreeformPlanAction`/`StructuredRequestPlan` by type for its execution-validation functions — those live there, not here, because they decide whether a plan's next action is safe to *execute*, a dispatch-adjacent concern distinct from *building* the plan; `main.rs`'s `run_freeform` builds a plan exactly once via `build_freeform_plan_with_context` and threads that same value through `execute_freeform_next_action` to `prepare_freeform_execution` rather than rebuilding it (ROCMAI-725: building a plan can call a configured cloud `planner_provider`, which is not guaranteed deterministic, so a second independent build could silently disagree with the first). [`chat.rs`](../apps/rocm/src/chat.rs)'s `natural_language_plan` MCP tool gets the same single-build guarantee from `plan_freeform_request_with_context`, which builds once and returns both the rendered text and the next action; `chat.rs` also calls `format_structured_tool_call` directly, like [`comfyui.rs`](../apps/rocm/src/comfyui.rs) does, while [`tool_policy.rs`](../apps/rocm/src/tool_policy.rs)'s `validate_chat_rocm_command_safety` reads `serve_args_request_cpu_device` from [`rocm_argv.rs`](../apps/rocm/src/rocm_argv.rs) directly as a no-CPU-fallback safety check on provider-proposed `serve` args; `validate_provider_planner_tool_call` calls that same function in its own `serve` branch, and hands an `install sdk` call to `tool_policy::validate_chat_rocm_command_safety`. `host_default_engine`, `build_freeform_plan_with_context`, `render_structured_request_plan`, `plan_next_action`, `plan_freeform_request_with_context` and `provider_planner_response_to_plan` are `pub(crate)` for these reasons, as is `plan_driver_request` (read from [`driver_plan.rs`](../apps/rocm/src/driver_plan.rs)'s test module); `format_structured_tool_call` lives in [`rocm_argv.rs`](../apps/rocm/src/rocm_argv.rs) and is re-exported here with `pub(crate) use`, so the callers above keep their `freeform_planner::` path, while the planner's own argv lookups call `rocm_argv`, its phrase and engine readers call [`prompt_text.rs`](../apps/rocm/src/prompt_text.rs) and its safety check calls `tool_policy.rs`, directly: the planner no longer imports `chat`, and only `chat` imports the planner (`plan_freeform_request_with_context`); `freeform_plan_next_action` and `render_freeform_plan` are test-only (`#[cfg(test)]`) and `FreeformPlanAction` was already `pub(crate)` before the split. `parse_freeform_invocation`/`should_treat_as_freeform` (deciding whether an invocation is freeform at all) stay in `main.rs`, a dispatch-level concern outside both this module and `freeform_execution.rs`. The cluster's regression tests live in `freeform_planner.rs`'s own `mod tests`; the handful of `main.rs` tests that exercise the invocation classifier/clap-suggestion-avoidance stay in `main.rs`'s shared `mod tests`, since neither is specific to this module or to `freeform_execution.rs` (and neither touches `freeform_planner::` at all), and the ROCMAI-725 regression tests proving the single-build guarantee live in `providers.rs`'s `mod tests` (reusing its mock local-provider server), reaching this module's items through the qualified `freeform_planner::` path like any other external caller. - [`gpu_policy.rs`](../apps/rocm/src/gpu_policy.rs) — mechanical relocation, owns types (Phase 6f, ROCMAI-214): `--device`/`--gpu` parsing (`parse_device_policy`, `parse_gpu_selection`, `parse_gpu_indices_arg`), the managed-launch-lock GPU selection (`select_gpu_indices_under_launch_lock`, `validate_pinned_gpu_index`, `effective_gpu_count`, `auto_select_gpu_indices`, `select_auto_gpu_index`, `busy_gpu_indices`), per-GPU VRAM telemetry (the `GpuVramUsage` type, `gpu_vram_usage`, `simulated_low_vram_usage`, `gpu_vram_usage_amd_smi`, `gpu_vram_usage_sysfs`, `read_drm_vram_usage`, `read_sysfs_u64`, `drm_device_is_amd`, `parse_gpu_vram_usage`), and the serve-plan low-VRAM warning (`gpu_low_memory_warning`, `vram_capacity_is_meaningful`, `serve_gpu_low_memory_warning`), plus the best-effort `amd-smi`-based GPU count (`detect_gpu_count`). `const fn device_policy_name` sits immediately after this cluster's last function in the original file but stayed in `main.rs` rather than moving — it already had callers outside the cluster (`engine_ipc.rs`, `service_launch.rs`, `serve_cmd.rs`) plus its own test. `GpuVramUsage` (and its fields), `gpu_vram_usage`, `parse_gpu_indices_arg`, `serve_gpu_low_memory_warning`, and `vram_capacity_is_meaningful` are `pub(crate)` and read from [`model_fit.rs`](../apps/rocm/src/model_fit.rs) (`classify_accelerator_memory`, `host_accelerator_memory`) [`main.rs`](../apps/rocm/src/main.rs) (the `dispatch()` `Command::EngineServeHttp` arm) and [`serve_cmd.rs`](../apps/rocm/src/serve_cmd.rs) (`collect_serve_notes`); `detect_gpu_count`, `gpu_vram_usage`, `parse_device_policy`, `parse_gpu_selection`, `select_gpu_indices_under_launch_lock`, `serve_gpu_low_memory_warning`, and `validate_pinned_gpu_index` are also `pub(crate)` and read from [`serve_cmd.rs`](../apps/rocm/src/serve_cmd.rs); `parse_device_policy` is also read from [`engine_ipc.rs`](../apps/rocm/src/engine_ipc.rs)'s `run_builtin_engine_serve_http` and, as of Phase 6f (ROCMAI-209), from [`services_text.rs`](../apps/rocm/src/services_text.rs)'s `restart_child_argv` (moved out of `main.rs` in that same phase). `effective_gpu_count`, `auto_select_gpu_indices`, `select_auto_gpu_index`, `simulated_low_vram_usage`, `gpu_vram_usage_amd_smi`, `gpu_vram_usage_sysfs` (both the Linux and non-Linux `cfg` variants), `read_drm_vram_usage`, `read_sysfs_u64`, `drm_device_is_amd`, `parse_gpu_vram_usage`, `gpu_low_memory_warning`, and `busy_gpu_indices` have no callers outside this module and stay private. The cluster's regression tests live in `gpu_policy.rs`'s own `mod tests`; two small test-only fixture builders (`vram`, `host_gpu`) are duplicated rather than shared, since `serve_cmd.rs`'s own `serve_notes_pair_low_vram_with_the_vllm_utilization_hint` (testing `collect_serve_notes`) needs them too. - [`install_cmd.rs`](../apps/rocm/src/install_cmd.rs) — mechanical relocation, dispatch-adjacent, no owned types: `rocm install`'s command handler (`install()`, for the `sdk` and `driver` targets), the `--version`/`--build-date` selector parsing and display it uses (`therock_install_version_selector`, `therock_install_version_selector_display`) and `sdk_install_request`, which maps the parsed `install sdk` arguments onto a `therock::SdkInstallRequest` and moved here from [`chat.rs`](../apps/rocm/src/chat.rs) with its two tests. `InstallTarget` and `InstallFormat` stay in [`main.rs`](../apps/rocm/src/main.rs). `install()` is `pub(crate)` and re-imported in `main.rs` for `dispatch()`; `therock_install_version_selector` is `pub(crate)` too, and `chat.rs`'s `render_install_sdk_dry_run_for_args` reaches it as `crate::therock_install_version_selector` through the same kind of re-import, which is why `sdk_install_request` lives here and not in `chat.rs`: this module does not depend on `chat.rs`. `sdk_install_request` keeps the `pub(crate)` it had, although only `install()` and the tests call it now. - [`logging.rs`](../apps/rocm/src/logging.rs) — mechanical relocation, no owned types, reached by qualified path. - [`managed_service_status.rs`](../apps/rocm/src/managed_service_status.rs) — mechanical relocation, owns types (Phase 6f, ROCMAI-211): managed-service liveness classification (`managed_service_is_live`, `refresh_managed_service_runtime_liveness`, `existing_live_managed_service`'s idempotency guard) and the dashboard-sidebar status/counts helpers derived from it (`ManagedServiceSidebarCounts`/`managed_service_sidebar_counts`/`local_server_sidebar_status`), plus the engine display-name helper `friendly_engine_label`, read only by [`chat.rs`](../apps/rocm/src/chat.rs). `ManagedServiceSidebarCounts` is `pub(crate)` to match the visibility of the `pub(crate)` functions that return/take it; `friendly_engine_label`, `existing_live_managed_service`, `recorded_service_pids`, `refresh_managed_service_runtime_liveness`, `local_server_sidebar_status`, `managed_service_sidebar_counts`, and `load_managed_services` are `pub(crate)` for the same reason — each has a confirmed caller outside this module. [`service_records.rs`](../apps/rocm/src/service_records.rs) uses its liveness helpers (`managed_service_is_live`, `recorded_service_pids`) for the in-use check behind service-record removal and pruning, and `load_managed_services` for prune planning, while [`bridge_snapshot.rs`](../apps/rocm/src/bridge_snapshot.rs) uses `load_managed_services` for the codex-bridge snapshot; [`service_launch.rs`](../apps/rocm/src/service_launch.rs) is the only caller of the `existing_live_managed_service` duplicate-launch guard; [`services_text.rs`](../apps/rocm/src/services_text.rs) reads the sidebar helpers; other modules, including [`chat.rs`](../apps/rocm/src/chat.rs) and [`runtime_services.rs`](../apps/rocm/src/runtime_services.rs), read the registry and liveness helpers. - [`model_fit.rs`](../apps/rocm/src/model_fit.rs) — mechanical relocation, owns a `pub(crate)` type: answering whether a host can run a model for `rocm diagnose --model` (`assess_model_on_this_host`, `assess_model_with_host_paths`, `assess_model_for_host`), the accelerator-memory fact it rests on (`host_accelerator_memory`, `classify_accelerator_memory`, `examination_reports_amd_gpu`) and the platform gate that rules an engine out (`PlatformRuleOut`, `engine_ruled_out_by_platform`, `unsupported_here_for`). It is a module of its own, rather than part of the `diagnose` handler, so [`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs), which reads `PlatformRuleOut` and `engine_ruled_out_by_platform`, does not depend on a command module. It depends on [`serve_cmd.rs`](../apps/rocm/src/serve_cmd.rs) (`select_serve_engine`) and [`gpu_policy.rs`](../apps/rocm/src/gpu_policy.rs), neither of which depends on it. `assess_model_on_this_host` (called from [`diagnose_cmd.rs`](../apps/rocm/src/diagnose_cmd.rs)), `PlatformRuleOut` and `engine_ruled_out_by_platform` are `pub(crate)`, and [`main.rs`](../apps/rocm/src/main.rs) re-imports the last two so `examine_report_text.rs` reaches them as `crate::`; every other item is private. The cluster's regression tests live in this module's own `mod tests`; the one that re-runs the test binary on a helper test by its full path (`engine_ruled_out_by_platform_gates_aim_on_docker_reachability`) names the helper's new path, `model_fit::tests::docker_unreachable_subprocess_helper`. +- [`prompt_text.rs`](../apps/rocm/src/prompt_text.rs) — text extraction from a natural-language request, shared by `chat.rs`'s prompt-intent heuristics (`fallback_rocm_tool_call_for_prompt`, `fallback_config_tool_call_for_prompt`, `install_sdk_chat_approval_for_prompt` and the `prompt_*` predicates) and the freeform planner, in a leaf module that depends only on `therock::RuntimeVersionSelector` (`tool_policy.rs` does not use it): `any_substring`, the install-folder, TheRock build-date and TheRock version readers (`requested_install_prefix_from_prompt`, `requested_therock_build_date_from_prompt`, `requested_therock_version_from_prompt`, with `requested_bare_install_prefix_from_prompt`, `clean_requested_install_prefix`, `looks_like_user_path` and `looks_like_therock_runtime_version` private beside them) from `chat.rs`, and `infer_engine_from_request` from `freeform_planner.rs`. They moved verbatim, together with `infer_engine_from_request`'s regression test, so that `freeform_planner.rs` no longer imports `chat` for them; `chat.rs` re-exports `any_substring`, which [`freeform_answer.rs`](../apps/rocm/src/freeform_answer.rs) still reaches as `chat::any_substring`. - [`provider_keys.rs`](../apps/rocm/src/provider_keys.rs) — full domain extraction. - [`providers.rs`](../apps/rocm/src/providers.rs) — full domain extraction (no dedicated command enum; invoked from the existing chat/config command flows). Keeps the request/response types, the `ProviderAdapter` trait and the `provider_*` dispatch, the `LocalProvider` and `RemoteProvider` implementations, local-service selection (`select_local_chat_service`, `ready_local_services`), the remote model and endpoint resolution, the `ureq` remote transport and the `SseBytesEmitter` trait. The ROCm tool schema, the local HTTP/SSE transport and the provider wire formats live in child modules under `providers/`, below; `providers.rs` imports the request-body builders, response parsers and SSE emitters it calls from `providers/wire.rs`. - [`providers/tool_schema.rs`](../apps/rocm/src/providers/tool_schema.rs) — full domain extraction, a leaf child of `providers.rs`: the ROCm tool catalog a provider advertises to a model, as OpenAI function definitions (`rocm_openai_tool_definitions`) and reshaped for Anthropic's `tools` field (`rocm_anthropic_tool_definitions`). Both are `pub(super)`, read by the request-body builders in `providers/wire.rs`; `rocm_openai_tool`, the one-tool constructor behind them, is private. Depends on nothing else in `providers`. - [`providers/local_http.rs`](../apps/rocm/src/providers/local_http.rs) — full domain extraction, a child of `providers.rs`: the local provider's hand-rolled HTTP/1.1 transport. `post_json_to_local_endpoint` (with `post_json_to_local_endpoint_body` and `local_bearer_header`) is the blocking JSON POST; `stream_json_from_local_endpoint` is the streamed POST, which reads the response in whichever body framing the engine answers with (`read_sse_body` picks between `read_chunked_sse_body`, `read_fixed_sse_body` and `read_close_delimited_sse_body` from the headers, via `header_value`) and feeds an `OpenAiSseEmitter`; `body_truncated_at_eof` and `LOCAL_STREAM_TRUNCATED` name the failure when an engine closes the connection mid-answer; `parse_http_endpoint` reads the `http://` endpoint URL. `post_json_to_local_endpoint` and `stream_json_from_local_endpoint` are `pub(super)`, called from `LocalProvider`, and `LOCAL_STREAM_TRUNCATED` is also `pub(super)` (imported by `providers.rs` for its test module only); the rest is private. Its edges back into `providers` are `OpenAiSseEmitter` from `providers/wire.rs`, the `SseBytesEmitter` trait the emitter is pushed bytes through (which stays in `providers.rs`), and the shared `ProviderStreamEvent` type; nothing in `wire.rs` reads `local_http.rs`, so the edge is one-way. [`providers/local_http/proptests.rs`](../apps/rocm/src/providers/local_http/proptests.rs) holds the property tests of the SSE body readers and is declared from this module so they sit with the readers they test; the example-based test of `parse_http_endpoint` is in this module's own `mod tests`, and the tests that drive `LocalProvider` end to end against a mock server stay in `providers.rs`'s `mod tests` with their server helpers. - [`providers/wire.rs`](../apps/rocm/src/providers/wire.rs) — full domain extraction, a child of `providers.rs`: the OpenAI-compatible and Anthropic wire formats. The request-body builders (`openai_chat_request_body`, `openai_stream_chat_request_body`, `openai_local_chat_request_body`, `openai_local_stream_chat_request_body`, `anthropic_chat_request_body`, `anthropic_stream_chat_request_body`, and the helpers behind them, including `local_openai_compatible_request`, `apply_local_model_request_defaults` and `local_model_should_disable_thinking`), the parsers for non-streaming responses (`parse_openai_chat_message`, `parse_anthropic_chat_message`), and the SSE emitters with their line parsers (`OpenAiSseEmitter` and `AnthropicSseEmitter`, which implement the `SseBytesEmitter` trait that stays in `providers.rs`; `parse_openai_sse_line`, `parse_anthropic_sse_line`). The builders, the two chat-message parsers and the two emitters (with their `new` and `finish`) are `pub(super)`, called from `LocalProvider` and `RemoteProvider`; `OpenAiSseEmitter` is also read by `providers/local_http.rs`. Depends on the shared request/response types in `providers.rs` and on `providers/tool_schema.rs`, and on nothing else. The tests of the builders and parsers live in this module's own `mod tests`, with the tool-catalog assertions that read through the request bodies, and [`providers/wire/sse_emitter_proptests.rs`](../apps/rocm/src/providers/wire/sse_emitter_proptests.rs), the split-invariance and panic-guard properties of the emitters and line parsers, is declared from this module; the stream tests that run an emitter against a mock server stay in `providers.rs`'s `mod tests`. +- [`rocm_argv.rs`](../apps/rocm/src/rocm_argv.rs) — the argv helpers that `chat.rs`, `tool_policy.rs` and the freeform planner share, in a leaf module whose only `crate::` dependency is the leaf [`shell_quote.rs`](../apps/rocm/src/shell_quote.rs): `chat_cli_arg_value`, `chat_cli_arg_value_checked`, `chat_cli_has_flag` and `ensure_flag` (from `chat.rs`), and `format_structured_tool_call` (which quotes each argument with `shell_quote`), `serve_args_request_cpu_device` and `device_policy_is_cpu_mode` (from `freeform_planner.rs`). They moved so that `freeform_planner.rs` no longer imports `chat` for them; `chat.rs` re-exports `ensure_flag` (read by `freeform_execution.rs`) and `freeform_planner.rs` re-exports `format_structured_tool_call`, so every other caller keeps its path. - [`runtime_adopt.rs`](../apps/rocm/src/runtime_adopt.rs) — full domain extraction, crate-level (split out of [`runtimes_cmd.rs`](../apps/rocm/src/runtimes_cmd.rs)): registering a runtime that `rocm install sdk` did not install. `import_runtime_manifest` records a manifest written elsewhere, `adopt_runtime_from_python_options` (with `AdoptRuntimeOptions`, `AdoptRuntimeRequest`, `infer_adopt_runtime_request` and `adopt_runtime_from_probe`) probes an existing Python environment and registers it, and `recover_setup_runtime_registration` registers the runtime `rocm setup` created, with `newest_installed_runtime_manifest` and `setup_runtime_paths` beside it. It depends on [`runtime_registry.rs`](../apps/rocm/src/runtime_registry.rs) (manifest paths, activation validation, `paths_equivalent`) and `therock.rs`, not on `runtimes_cmd.rs`, which calls into it and re-exports `newest_installed_runtime_manifest`, `recover_setup_runtime_registration` and `setup_runtime_paths`. `import_runtime_manifest`, `AdoptRuntimeOptions`, `adopt_runtime_from_python_options` (with the fields of `AdoptRuntimeOptions` and `AdoptRuntimeRequest`) and `adopt_runtime_from_probe` are `pub(crate)` for `runtimes()` and for the `runtimes_cmd.rs` tests that adopt or import a runtime and then list or activate it; `newest_installed_runtime_manifest`, `recover_setup_runtime_registration` and `setup_runtime_paths` are `pub(crate)` too, for the modules that import them through `runtimes_cmd.rs`'s re-exports; the rest is private. Its tests cover the import and adopt rules that do not involve activation or listing. - [`runtime_install_finalize.rs`](../apps/rocm/src/runtime_install_finalize.rs) — mechanical relocation, owns types (part of [`main.rs`](../apps/rocm/src/main.rs)'s Phase 6c split, ROCMAI-89, alongside [`runtimes_cmd.rs`](../apps/rocm/src/runtimes_cmd.rs) below): settling an engine onto a runtime after an SDK install: the orchestration (`finish_sdk_install`, `settle_engine_install`, `finalize_successful_sdk_install`), the engine-dependency check, the runtime device check and the post-install verdict built on it, and `SdkInstallConsents`. The torch alignment and retention it runs live in the child module [`runtime_install_finalize/torch_alignment.rs`](../apps/rocm/src/runtime_install_finalize/torch_alignment.rs). `SdkInstallConsents` is `pub(crate)` and read from `install_cmd.rs`'s `install()`; `settle_engine_install` is `pub(crate)` and imported by [`engines_cmd.rs`](../apps/rocm/src/engines_cmd.rs), so the import edge between the two runs one way, from `engines_cmd.rs` to this module. This module (and its torch-alignment child) does not import `engines_cmd.rs`: the engine environment and runtime-selection helpers it needs (`engine_manages_own_runtime`, `env_root_for_engine_install`, `runtime_key_for_python`, and `runtime_manifest_for_selector` from the torch-alignment child) are imported from [`engine_install_env.rs`](../apps/rocm/src/engine_install_env.rs). The host system-package installs live in [`system_packages.rs`](../apps/rocm/src/system_packages.rs); this module re-exports `ensure_libatomic_for_torch`/`ensure_libnuma_for_torch` (called from both `install_cmd.rs`'s `install()` and `engines_cmd.rs`) and `ensure_openmpi_for_vllm` (called from `engines_cmd.rs` and, for the engine auto-install after an SDK install, from here) so those callers keep their imports. - [`runtime_install_finalize/torch_alignment.rs`](../apps/rocm/src/runtime_install_finalize/torch_alignment.rs) — full domain extraction, a child of `runtime_install_finalize.rs` that depends on its parent's device check (`RuntimeDeviceCheck`, `device_check_verdict`) and on `runtime_manifest_for_selector` in [`engine_install_env.rs`](../apps/rocm/src/engine_install_env.rs) (split out of [`runtime_install_finalize.rs`](../apps/rocm/src/runtime_install_finalize.rs)): what to do about the torch an engine install finds in a managed runtime. `plan_torch_alignment` decides whether the SDK's torch must be realigned to the engine's pin, `align_runtime_torch` runs the realignment, `classify_retained_torch` decides when a torch that already works is kept as an intended divergence instead, and the `render_*` and `report_*` functions with `TorchAlignment` and `TorchRetention` print and log the outcome. The manifest readers (`sdk_torch_build_for_key`, `sdk_torch_build_from_manifest`, `runtime_python_for_key`, `runtime_library_paths_for_key`, `runtime_index_url_for_key`) live here too. The parent stays the caller (`settle_engine_install` and `maybe_auto_install_sdk_preferred_engine`), so it could not be a crate-level sibling without a cycle with the parent's device check. The items the parent calls are `pub(super)`: `TorchAlignment`, `TorchRetention`, `report_torch_alignment`, `report_torch_retention`, `classify_retained_torch`, `retained_diverged_package`, `deliberately_diverged_package`, `sdk_torch_build_for_key`, `runtime_python_for_key` and `runtime_library_paths_for_key`; the rest is private. Its tests cover planning, rendering, the opt-out and the retention rules, and reach the parent's private engine-dependency check as a descendant of it. @@ -99,6 +101,7 @@ Each per-crate module inventory lists one file per line. Flat inventories (every - [`therock/consent.rs`](../apps/rocm/src/therock/consent.rs) — full domain extraction, a child of `therock.rs` that builds on `therock/source.rs` and `therock/version.rs` and reaches the registry through [`runtime_registry.rs`](../apps/rocm/src/runtime_registry.rs) (split out of `therock.rs`): the consent gate for an install that would displace the active default runtime. `active_default_runtime_relation` says what the install would replace (an upgrade, downgrade or reinstall when it is the same family and channel, otherwise the different runtime it displaces) or `None` when nothing is the active default, and it fails closed, with a relation naming what could not be resolved, when the config names an active default the registry cannot resolve; `SdkInstallConsent` and `SdkInstallApprovalSource` say how consent is obtained (asked for, or already granted by `--yes`, `--approve-replacing-active-default` or `rocm update --apply`); `sdk_install_approval` turns those and whether a terminal is attached into proceed, prompt or refuse (`SdkInstallApproval`); `preapproved_install_line`, `fresh_install_line`, `refuse_non_interactive_message` and `confirm_overwrite_existing_sdk` are the wording of each outcome, and the prompt reads its answer through `prompt_yes_no` and the shared [`confirm.rs`](../crates/rocm-core/src/confirm.rs). `SdkInstallConsent`, `SdkInstallApprovalSource` and `preapproved_install_line` are re-exported by `therock.rs`; the other items `therock.rs` calls are `pub(super)`, as is `active_default_relation_text`, which the version property tests call, and the rest is private. - [`therock/update.rs`](../apps/rocm/src/therock/update.rs) — full domain extraction, a child of `therock.rs` that builds on `therock/family.rs`, `therock/io.rs`, `therock/process.rs`, `therock/python.rs`, `therock/resolve.rs`, `therock/source.rs` and `therock/version.rs`, and on [`runtime_registry.rs`](../apps/rocm/src/runtime_registry.rs) for the manifests (split out of `therock.rs`): whether an installed runtime has something newer, and the bounded startup check that asks. `runtime_update_plan` resolves the latest source for one runtime (`resolve_latest_for_manifest` re-resolves a wheel runtime through `resolve_pip_runtime_with_timeout` and a tarball one through `resolve_tarball_artifact_with_timeout`, on the layout `manifest_source_layout` reads back from the manifest) and `runtime_freshness` classifies it (`RuntimeFreshness`: up to date, update available, repair available when the installed composition is not the one a fresh install would record, or ahead of the index) into a `RuntimeUpdatePlan`. `render_update_report` and `render_update_json` (the form the dashboard's update check parses) present the plans, and `maybe_refresh_startup_update_check` keeps one `StartupUpdateCheckRecord` in the cache so the check runs at most once every twelve hours, under a two-second download timeout. `runtime_key`, `wheel_runtime_key` and `has_nontrivial_directory_contents`, which name and inspect the install a plan targets, stay in `therock.rs`. `render_update_report`, `render_update_json`, `runtime_update_plan` and `maybe_refresh_startup_update_check` are `pub(crate)` and re-exported by `therock.rs`; `RuntimeFreshness`, `runtime_freshness` and `select_startup_update_manifest` are `pub(super)` for the version property tests, and the rest is private. - [`therock/test_support.rs`](../apps/rocm/src/therock/test_support.rs) — test-only (`#[cfg(test)]`), a child of `therock.rs`: fixtures shared by the `therock` child modules' tests and the tests left in `therock.rs` (`test_paths`, `test_runtime_manifest`, `write_test_runtime_manifest`, `family_for_test`). Separate from the crate-level [`test_support.rs`](../apps/rocm/src/test_support.rs), which holds the fixtures every module's tests share, such as `PROCESS_ENV_TEST_LOCK`. +- [`tool_policy.rs`](../apps/rocm/src/tool_policy.rs) — the chat assistant's tool-call policy: which `rocm` commands and bridge tools a model may request, which run immediately and which need the user's approval first. It owns `ChatRocmCommandAction` (read-only, or approval with the titles the approval prompt shows) and `ChatToolApprovalRequest`, the validators (`validate_chat_tool_call`, the `validate_chat_*_tool_call` family, `validate_required_chat_string`, `validate_optional_chat_integer`, `reject_control_characters_in_tool_arguments` (which refuses a model-supplied tool name or argument carrying a terminal control character, for every tool except `rocm_command`, whose argv `normalized_chat_rocm_command_args` screens entry by entry), `validate_chat_rocm_command_safety`, which refuses a public-bind, non-loopback or CPU `serve` and a system install folder (`chat_install_prefix_is_system`)), the classifiers (`chat_rocm_command_action`, `chat_rocm_command_action_from_args`, `chat_install_driver_action`, `canonicalize_chat_rocm_command`, `normalized_chat_rocm_command_args`, `chat_tool_call_is_read_only`), `chat_tool_approval_request`, and the readers that turn a tool call into its argv and one-line command (`rocm_chat_tool_requested_args`, `rocm_chat_tool_requested_command`, with `push_optional_json_cli_arg` and `json_string`). It moved verbatim from `chat.rs` with the tests that exercise only these items. It depends on [`rocm_argv.rs`](../apps/rocm/src/rocm_argv.rs), `providers::ChatToolCall`, `service_launch::is_loopback_host`, `services_text::validate_service_id`, `shell_quote::has_terminal_control_character`, `therock::RuntimeVersionSelector` and `driver_install::host_install_driver_only_reports`, none of which imports `chat`, so `freeform_planner.rs` calls `validate_chat_rocm_command_safety` here instead of through `chat`. `chat.rs` calls into it and re-exports `validate_chat_tool_call`, `chat_tool_call_is_read_only` and `chat_tool_approval_request` (read by `dash_seam.rs`), and `ChatRocmCommandAction`, `chat_install_driver_action`, `chat_rocm_command_action_from_args` and `rocm_chat_tool_requested_args`, which only other modules' tests read as `crate::chat::` (`driver_plan.rs`, `system_packages.rs`). - [`uninstall.rs`](../apps/rocm/src/uninstall.rs) — mechanical relocation, owns types: `uninstall()`, plus the installer `PATH` undo and running-executable deletion. The shared plan type and its file-removal helpers live in [`uninstall_plan.rs`](../apps/rocm/src/uninstall_plan.rs) below instead, because [`storage.rs`](../apps/rocm/src/storage.rs) reuses them too. The two modules depend on each other: `uninstall_plan.rs` reads this module's `pub(crate)` `PathEdit`, `PATH_EDIT_RECORD`, `plan_path_edits`, `plan_is_empty`, `removal_count` and `render_extra_plan`. - [`uninstall_plan.rs`](../apps/rocm/src/uninstall_plan.rs) — mechanical relocation, shares crate-visible types (Phase 6f, ROCMAI-212): the uninstall-plan types (`UninstallOptions`, `UninstallPlan`, `UninstallPlanEntry`), building the plan (`build_uninstall_plan`, `plan_installed_binaries`, and the installed-binary/shared-cache discovery helpers behind them), rendering it (`render_uninstall_dry_run`, `render_uninstall_plan`), the `[y/N]` confirmation prompt (`confirm_uninstall`), and the filesystem removal primitive both [`uninstall.rs`](../apps/rocm/src/uninstall.rs) and [`storage.rs`](../apps/rocm/src/storage.rs) call to carry a plan out (`remove_path`, and the entry-resolution helpers behind it). Kept separate from `uninstall.rs` rather than folded into it, since `uninstall.rs`'s own doc comment and name both describe `uninstall()`'s exclusive logic (the installer `PATH` undo and running-executable deletion), while this module's plan/render/removal helpers are shared with `storage.rs`. `main.rs` (`UninstallOptions`), `uninstall.rs`, `storage.rs`, `runtimes_cmd.rs`, `chat.rs`, and [`deletion_properties.rs`](../apps/rocm/src/deletion_properties.rs) (Unix-only property tests) each import the subset of these `pub(crate)` types and helpers they need; see this module's own doc comment for which symbol each one uses. - [`update_cmd.rs`](../apps/rocm/src/update_cmd.rs) — mechanical relocation, dispatch-adjacent, no owned types (Phase 6f, ROCMAI-210): `rocm update`'s command handler (`update()`, the body of the `update` arm of `dispatch()`) and its rendering/activation logic — the read-only status report (`render_update_text`, `append_update_surfaces`), the predicate deciding whether `update` should route into the mutating path instead of that report (`update_should_preview_or_apply`), applying a runtime update (`apply_runtime_update`, `append_update_activate_summary`), and the update-source/installed-runtime pickers behind it (`select_runtime_update_source`, `select_installed_update_runtime`). `const ROLLBACK_RECOVERY_HINT` stays in `main.rs` as a shared crate-root constant, because callers outside this module also use it ([`runtimes_cmd.rs`](../apps/rocm/src/runtimes_cmd.rs)'s `RuntimesCommand::Activate` handler and [`runtime_install_finalize.rs`](../apps/rocm/src/runtime_install_finalize.rs)'s `render_sdk_install_success`); this module reaches it via its `use crate::{...};` import, the same way `gpu_policy.rs`'s `device_policy_name` stays in `main.rs`. `update()` is `pub(crate)` and called from [`main.rs`](../apps/rocm/src/main.rs)'s `dispatch()` through a re-import (`use crate::update_cmd::update;`), and calls `update_should_preview_or_apply`, `apply_runtime_update` and `render_update_text` itself; `render_update_text` is also `pub(crate)` and read from [`chat.rs`](../apps/rocm/src/chat.rs) (the deterministic `rocm update` tool summary), while `apply_runtime_update` and `update_should_preview_or_apply` keep the `pub(crate)` they had when `dispatch()` called them directly, though only `update()` calls them now; `select_runtime_update_source` is also `pub(crate)`, read from [`therock_version_proptests.rs`](../apps/rocm/src/therock_version_proptests.rs)'s property tests via `crate::update_cmd::select_runtime_update_source`. `append_update_surfaces`, `append_update_activate_summary`, and `select_installed_update_runtime` have no callers outside this module and stay private. The cluster's regression tests live in `update_cmd.rs`'s own `mod tests`, reaching shared fixtures (`test_paths`, `test_runtime_manifest_for_update`, `write_test_pip_runtime`) through `crate::tests::`/`crate::test_support::` the same way other mechanically relocated modules do.