diff --git a/README.md b/README.md index 9719b59b1..472992d69 100644 --- a/README.md +++ b/README.md @@ -1801,6 +1801,20 @@ standard input, it sends one prompt and prints the reply. 2 minutes. The timeout message names the wait that applied and this variable. +With `--prompt`, the reply and tool output are printed with terminal escape +sequences and invisible characters removed, so a reply cannot hide part of a +line, retitle the window or write the clipboard; text it tried to hide is +shown. Every `rocm` error report is printed the same way. A change the assistant asks for with `--tools` is not made from +`--prompt`: it is shown as an `advanced manual command:` line, quoted for a +POSIX shell. A value the assistant chose, in that line or in a plan it asked +for, is refused if it holds a character a Windows shell reads as syntax even +inside those quotes, such as `'`, `&` or `%`; the message tells you to run the +command yourself with that value. A folder the CLI takes from your own +request, such as `install therock into `, is shown as you typed it; a +folder that reaches the CLI through the assistant's request is held to the +rule, even if you named it first. +See the LLM tool-use design notes in the documentation for the exact set. + In the interactive chat session, if no local engine is reachable and no API key or URL is configured, chat falls back to the no-key ChatGPT OAuth sign-in. That backend logs a warning and ignores all three sampling flags. This is an diff --git a/apps/rocm/src/chat.rs b/apps/rocm/src/chat.rs index c2425836d..3ccbe04e0 100644 --- a/apps/rocm/src/chat.rs +++ b/apps/rocm/src/chat.rs @@ -63,6 +63,7 @@ pub(crate) use crate::tool_policy::{ }; pub(crate) use crate::tool_policy::{ chat_tool_approval_request, chat_tool_call_is_read_only, validate_chat_tool_call, + validate_model_chosen_plan_result, validate_model_chosen_tool_call, }; use crate::{ChatInferenceParams, providers}; use anyhow::{Context, Result}; @@ -130,23 +131,30 @@ fn piped_prompt_from_input(input: &str) -> Option { } pub(crate) fn render_chat_text(paths: &AppPaths, provider: &str) -> Result { - let status = providers::provider_status(paths, provider)?; + Ok(render_chat_status_text(&providers::provider_status( + paths, provider, + )?)) +} + +/// The `rocm chat` status screen for `status`. The model names come from a +/// provider's or a local server's listing, which this process did not write, +/// so each is sanitized on its own before it is composed with the screen's +/// lines; everything else on the screen is this process's own text. +fn render_chat_status_text(status: &providers::ProviderStatus) -> String { + use rocm_core::terminal::sanitize_terminal_text; let mut output = String::new(); let _ = writeln!(output, "Chat assistant"); let _ = writeln!(output); - let _ = writeln!( - output, - "Assistant source: {}", - if status.provider == "local" { - "local model on this computer" - } else { - status.provider.as_str() - } - ); + let source = if status.provider == "local" { + "local model on this computer".to_owned() + } else { + sanitize_terminal_text(&status.provider) + }; + let _ = writeln!(output, "Assistant source: {source}"); let _ = writeln!( output, "Status: {}", - plain_provider_auth_status(&status.auth_status) + sanitize_terminal_text(&plain_provider_auth_status(&status.auth_status)) ); let _ = writeln!( output, @@ -161,8 +169,8 @@ pub(crate) fn render_chat_text(paths: &AppPaths, provider: &str) -> Result Result String { @@ -235,6 +243,39 @@ pub(crate) fn parse_positive_u32(value: &str) -> Result { mod tests { use super::*; + /// The status screen prints model names a server listed and a provider + /// name from configuration: each is printed with its escape sequences + /// removed, and one left open cannot take the screen's own lines with it. + #[test] + fn the_status_screen_prints_listed_names_without_their_escapes() { + let status = providers::ProviderStatus { + provider: "remote\u{1b}]0;title\u{7}x".to_owned(), + auth_status: "ready".to_owned(), + models: vec![ + "qwen\u{1b}]0;never closed".to_owned(), + "llama\u{9b}2K".to_owned(), + ], + tool_call_schema: String::new(), + }; + assert_eq!( + render_chat_status_text(&status), + "Chat assistant\n\nAssistant source: remotex\nStatus: Ready\nROCm help: not available\n\ + Models:\n - qwen\n - llama\n\n\ + Choose Chat source to switch between local and remote assistants.\n\ + Choose Settings to manage saved keys and defaults.\n" + ); + // The status itself can come from a provider, so it is cleaned too. + let status = providers::ProviderStatus { + auth_status: "key_\u{1b}]0;title\u{7}rejected\u{9b}2K".to_owned(), + ..status + }; + assert!( + render_chat_status_text(&status).contains("\nStatus: key rejected\n"), + "{:?}", + render_chat_status_text(&status) + ); + } + #[test] fn piped_prompt_keeps_everything_but_the_trailing_line_ending() { // A piped prompt must reach the send path byte-identical to the same diff --git a/apps/rocm/src/chat/mcp_bridge.rs b/apps/rocm/src/chat/mcp_bridge.rs index cf5ea7c6a..5da85b3f7 100644 --- a/apps/rocm/src/chat/mcp_bridge.rs +++ b/apps/rocm/src/chat/mcp_bridge.rs @@ -253,7 +253,8 @@ pub(crate) fn run_internal_mcp_call( // same `StructuredRequestPlan`, so a provider-assisted plan (not // guaranteed deterministic across calls) cannot describe one // command in `text` and a different one in `action`. - let (text, action) = plan_freeform_request_with_context(&request, paths, &config); + let (text, action, planned_args) = + plan_freeform_request_with_context(&request, paths, &config); let action = action.map(|action| { serde_json::json!({ "args": action.args, @@ -268,6 +269,10 @@ pub(crate) fn run_internal_mcp_call( "request": request, "text": text, "action": action, + // Every action's argv, as its `tool_call:` line in `text` + // prints it, so a caller can hold a model's request to the + // model-only rules (`tool_policy::validate_model_chosen_plan`). + "planned_args": planned_args, }), )) } diff --git a/apps/rocm/src/chat/turn.rs b/apps/rocm/src/chat/turn.rs index fcef7a8b3..3addf2a74 100644 --- a/apps/rocm/src/chat/turn.rs +++ b/apps/rocm/src/chat/turn.rs @@ -34,9 +34,11 @@ use crate::rocm_argv::chat_cli_arg_value; use crate::tool_policy::{ ChatToolApprovalRequest, chat_tool_approval_request, chat_tool_call_is_read_only, json_string, normalized_chat_rocm_command_args, rocm_chat_tool_requested_command, validate_chat_tool_call, + validate_model_chosen_plan_result, validate_model_chosen_tool_call, }; use crate::{ChatInferenceParams, chat_host_facts, providers}; use anyhow::Result; +use rocm_core::terminal::sanitize_terminal_text; use rocm_core::{AppPaths, DEFAULT_LOCAL_HOST}; use std::fmt::Write as _; @@ -166,6 +168,48 @@ pub(super) fn render_chat_prompt_result_with_progress( rocm_tools: bool, params: ChatInferenceParams, progress: Option<&mut dyn FnMut(String)>, +) -> Result { + render_chat_prompt_result_unsanitized( + paths, provider, model, prompt, rocm_tools, params, progress, + ) + .map(sanitize_chat_prompt_result) +} + +/// Strip what a terminal would act on from everything in a chat result that +/// came from the model or a tool. +/// +/// `rendered` is written straight to stdout by `rocm chat --prompt`, and it +/// carries the model's reply, the follow-up answer and tool results verbatim. +/// Unlike a tool *argument*, prose cannot simply be refused, so it is cleaned +/// instead: an SGR conceal, a clipboard-writing OSC or a cursor move in a reply +/// never reaches the terminal, and the concealed text is shown. The approval's +/// `explanation` is the model's prose too. `args` and `display_command` are +/// left alone: they are held to refusal at validation, and cleaning them here +/// would make the command shown differ from the argv that runs. +/// +/// The reply, the model name, each tool result and the follow-up answer are +/// also cleaned where they are composed, but this pass is the one every line +/// of `rendered` goes through, the summary built from tool output and the +/// prompt echoed in a hint included. A string sequence left unterminated ends +/// at its own line here, so it cannot hide the lines after it. +fn sanitize_chat_prompt_result(result: ChatPromptResult) -> ChatPromptResult { + ChatPromptResult { + rendered: sanitize_terminal_text(&result.rendered), + approval: result.approval.map(|approval| ChatToolApprovalRequest { + explanation: approval.explanation.as_deref().map(sanitize_terminal_text), + ..approval + }), + } +} + +fn render_chat_prompt_result_unsanitized( + paths: &AppPaths, + provider: &str, + model: Option<&str>, + prompt: &str, + rocm_tools: bool, + params: ChatInferenceParams, + progress: Option<&mut dyn FnMut(String)>, ) -> Result { let mut progress = progress; let user_prompt = latest_user_chat_message(prompt); @@ -195,6 +239,10 @@ pub(super) fn render_chat_prompt_result_with_progress( role: "user".to_owned(), content: prompt.to_owned(), }); + // Whether `response.tool_calls` are the model's own choice, as opposed to + // calls this code built from the user's prompt. Only the model's are held + // to `validate_model_chosen_tool_call`: a value the user typed is theirs. + let mut model_chose_tool_calls = false; let mut response = if rocm_tools { report_chat_tool_progress(&mut progress, "Asking the assistant."); if let Some(call) = deterministic_mutating_tool_call_for_prompt(user_prompt)? { @@ -224,7 +272,10 @@ pub(super) fn render_chat_prompt_result_with_progress( rocm_tools, }, ) { - Ok(response) => response, + Ok(response) => { + model_chose_tool_calls = true; + response + } Err(error) if provider == "local" && local_provider_missing_service_error(&error) => { @@ -277,6 +328,13 @@ pub(super) fn render_chat_prompt_result_with_progress( Err(error) => return Err(error), } }; + // Counted before the fallback and supplemental calls below are appended, + // so it covers exactly the calls the provider's reply carried. + let model_tool_call_count = if model_chose_tool_calls { + response.tool_calls.len() + } else { + 0 + }; let fallback_tool_call = if rocm_tools && response.tool_calls.is_empty() { fallback_rocm_tool_call_for_prompt(user_prompt) } else { @@ -295,7 +353,12 @@ pub(super) fn render_chat_prompt_result_with_progress( let mut output = String::new(); let _ = writeln!(output, "chat response"); let _ = writeln!(output, " provider: {}", response.provider); - let _ = writeln!(output, " model: {}", response.model); + // The model name comes back from the server, so it is untrusted text too. + let _ = writeln!( + output, + " model: {}", + sanitize_terminal_text(&response.model) + ); let _ = writeln!( output, " rocm tools: {}", @@ -304,7 +367,7 @@ pub(super) fn render_chat_prompt_result_with_progress( let _ = writeln!(output); let initial_content_is_intermediate = fallback_tool_call_used || local_tool_call_content_is_intermediate(provider, rocm_tools, &response); - let initial_content = visible_chat_content(&response.content); + let initial_content = sanitize_terminal_text(&visible_chat_content(&response.content)); if initial_content_is_intermediate { let _ = writeln!(output, "Assistant is preparing the next step."); } else if !initial_content.trim().is_empty() { @@ -313,7 +376,14 @@ pub(super) fn render_chat_prompt_result_with_progress( let tool_result = if rocm_tools { let explanation = (!fallback_tool_call_used && !initial_content.trim().is_empty()) .then_some(initial_content.as_str()); - append_chat_tool_results(paths, &response, &mut output, explanation, &mut progress)? + append_chat_tool_results( + paths, + &response, + model_tool_call_count, + &mut output, + explanation, + &mut progress, + )? } else { ChatToolRunResult { approval: None, @@ -360,7 +430,8 @@ pub(super) fn render_chat_prompt_result_with_progress( }, )?; if follow_up.tool_calls.is_empty() { - let follow_up_content = visible_chat_content(&follow_up.content); + let follow_up_content = + sanitize_terminal_text(&visible_chat_content(&follow_up.content)); if local_follow_up_content_is_final(&follow_up) && !follow_up_content.trim().is_empty() { let _ = writeln!(output); @@ -592,7 +663,7 @@ fn render_install_sdk_folder_needed_chat_text(prompt: &str, args: &[String]) -> } let _ = writeln!(output); let _ = writeln!(output, "Your request"); - let _ = writeln!(output, " {}", prompt.trim()); + let _ = writeln!(output, " {}", sanitize_terminal_text(prompt.trim())); output.trim_end().to_owned() } @@ -733,9 +804,14 @@ fn local_chat_service_needed_text(model: Option<&str>, prompt: &str, rocm_tools: output.trim_end().to_owned() } +/// Run or stage each tool call in `response` and render the `ROCm checks used` +/// block. The first `model_tool_call_count` calls are the model's own and are +/// held to `validate_model_chosen_tool_call` as well; the rest were built from +/// the user's prompt. fn append_chat_tool_results( paths: &AppPaths, response: &providers::ChatResponse, + model_tool_call_count: usize, output: &mut String, assistant_explanation: Option<&str>, progress: &mut Option<&mut dyn FnMut(String)>, @@ -758,7 +834,7 @@ fn append_chat_tool_results( let mut ran_read_only_tool = false; let mut read_only_tool_error = false; let mut needs_install_folder = false; - for call in &response.tool_calls { + for (index, call) in response.tool_calls.iter().enumerate() { if chat_tool_call_requests_therock_install_without_prefix(call) { needs_install_folder = true; report_chat_tool_progress(progress, "Waiting for the ROCm install folder."); @@ -770,10 +846,19 @@ fn append_chat_tool_results( continue; } validate_chat_tool_call(call)?; + if index < model_tool_call_count { + validate_model_chosen_tool_call(call)?; + } let label = chat_tool_call_display_label(call); if chat_tool_call_is_read_only(call) { report_chat_tool_progress(progress, &format!("Running ROCm check: {label}.")); let result = run_chat_read_only_tool(paths, call)?; + // A model's request planned into `tool_call:` lines puts values the + // model chose on lines meant to be pasted, so the plan is held to + // the same rules as a call, before any of it is shown. + if index < model_tool_call_count && call.name == "natural_language_plan" { + validate_model_chosen_plan_result(&result)?; + } ran_read_only_tool = true; let is_error = mcp_tool_result_is_error(&result); read_only_tool_error |= is_error; @@ -793,7 +878,7 @@ fn append_chat_tool_results( let result_text = mcp_tool_result_text(&result); let _ = writeln!(follow_up_text, "{}:", call.name); let _ = writeln!(follow_up_text, "{result_text}"); - for line in result_text.lines() { + for line in sanitize_terminal_text(&result_text).lines() { let _ = writeln!(output, " {line}"); } } else { @@ -1174,6 +1259,7 @@ model recipes append_chat_tool_results( &paths, &response, + response.tool_calls.len(), &mut output, Some("I can install ROCm."), &mut progress, @@ -1187,6 +1273,88 @@ model recipes Ok(()) } + fn install_sdk_response(prefix: &str) -> providers::ChatResponse { + providers::ChatResponse { + provider: "local".to_owned(), + model: "tiny.gguf".to_owned(), + content: "I can install ROCm.".to_owned(), + tool_calls: vec![providers::ChatToolCall { + id: None, + name: "install_sdk".to_owned(), + arguments: serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": prefix, + }), + }], + } + } + + /// A folder the model chose with `&` in it would start a second command + /// when the `advanced manual command:` line is pasted into `cmd.exe`, so + /// the call is refused before any line is printed or approval staged. The + /// same folder in a call built from the user's own prompt is theirs, and is + /// rendered, quoted. + #[test] + fn a_model_chosen_folder_a_windows_shell_would_split_is_never_offered_to_paste() { + let (_root, paths) = test_paths("chat-tool-paste-unsafe"); + let response = install_sdk_response("D:\\R&D\\rocm"); + + let mut output = String::new(); + let error = append_chat_tool_results(&paths, &response, 1, &mut output, None, &mut None) + .err() + .expect("a model-chosen folder with `&` must be refused") + .to_string(); + assert!( + error.contains("supply it yourself by running `rocm install sdk`"), + "{error}" + ); + assert!( + !output.contains("advanced manual command"), + "nothing may be offered to paste for a refused call:\n{output}" + ); + + let mut output = String::new(); + let result = append_chat_tool_results(&paths, &response, 0, &mut output, None, &mut None) + .expect("the user's own folder is accepted"); + assert!(result.approval.is_some(), "{output}"); + assert!( + output.contains("advanced manual command: rocm install sdk") + && output.contains("--prefix 'D:\\R&D\\rocm'"), + "{output}" + ); + } + + /// `rocm chat --prompt` writes `rendered` straight to stdout, and the + /// model's reply and tool results reach it verbatim. A reply that conceals + /// part of itself, retitles the window or writes the clipboard must reach + /// the terminal as plain, visible text. + #[test] + fn a_chat_result_carries_no_terminal_escape_to_stdout() { + let result = ChatPromptResult { + rendered: "chat response\n\nrun: rocm serve qwen\u{1b}[8m --port 9\u{1b}[28m --managed\n\u{1b}]52;c;aGVsbG8=\u{7}done\u{9d}0;title\u{9c}\n".to_owned(), + approval: Some(ChatToolApprovalRequest { + pending_title: "Start local model server".to_owned(), + command_title: "Serve".to_owned(), + args: vec!["serve".to_owned(), "qwen".to_owned(), "--managed".to_owned()], + display_command: Some("rocm serve qwen --managed".to_owned()), + explanation: Some("Starting \u{1b}]0;title\u{7}qwen\u{202e}.".to_owned()), + }), + }; + let cleaned = sanitize_chat_prompt_result(result); + assert_eq!( + cleaned.rendered, "chat response\n\nrun: rocm serve qwen --port 9 --managed\ndone\n", + "the concealed text must be shown, and the escapes gone" + ); + let approval = cleaned.approval.expect("approval survives"); + assert_eq!(approval.explanation.as_deref(), Some("Starting qwen.")); + assert_eq!( + approval.display_command.as_deref(), + Some("rocm serve qwen --managed"), + "the rendered command is held to refusal at validation, not rewritten here" + ); + } + #[test] fn local_rocm_tools_chat_uses_fixed_lemonade_qwen_assistant() { assert_eq!( @@ -1326,6 +1494,35 @@ model recipes Ok(()) } + /// The other half of the model-only refusal, through the whole turn: a + /// folder the user typed is theirs, `&` and all, so the turn the CLI builds + /// from it stages the install and prints it quoted, without asking a model. + #[test] + fn a_folder_the_user_typed_is_rendered_whatever_it_holds() -> Result<()> { + let (_root, paths) = test_paths("chat-install-user-typed-ampersand"); + let result = render_chat_prompt_result( + &paths, + "local", + None, + "install therock into /mnt/R&D/rocm", + true, + )?; + let approval = result.approval.as_ref().expect("the install is staged"); + assert_eq!( + chat_cli_arg_value(&approval.args, "--prefix"), + Some("/mnt/R&D/rocm") + ); + assert!( + result + .rendered + .contains("advanced manual command: rocm install sdk") + && result.rendered.contains("--prefix '/mnt/R&D/rocm'"), + "{}", + result.rendered + ); + Ok(()) + } + #[test] fn chat_install_intent_ignores_old_conversation_words() -> Result<()> { let (_root, paths) = test_paths("chat-install-intent-latest-message"); diff --git a/apps/rocm/src/cli.rs b/apps/rocm/src/cli.rs index 55f8eb157..3662f8cfe 100644 --- a/apps/rocm/src/cli.rs +++ b/apps/rocm/src/cli.rs @@ -356,9 +356,13 @@ pub(crate) enum Command { /// In a terminal with no --prompt, opens the interactive chat (switch /// providers there with /provider; --provider, --model, and --tools don't /// apply). With --prompt, or a prompt piped on standard input, sends that one - /// prompt and prints the reply. Use `rocm config enable-provider` and - /// `rocm config set-provider-key` first to configure a remote provider; the - /// `local` provider talks to a running server. + /// prompt and prints the reply, with terminal escape sequences removed. With + /// --tools, a value the assistant chose that a Windows shell would read as + /// syntax, such as ', & or %, is refused rather than shown in a command to + /// paste or in a plan; a folder the CLI takes from your own request, as in + /// `install therock into `, is not held to that. Use `rocm config + /// enable-provider` and `rocm config set-provider-key` first to configure a + /// remote provider; the `local` provider talks to a running server. #[command(after_help = "EXAMPLES:\n \ rocm chat --prompt \"Explain ROCm in one sentence\"\n \ rocm chat --provider local --model qwen --prompt \"Hello\"\n \ diff --git a/apps/rocm/src/dash_seam.rs b/apps/rocm/src/dash_seam.rs index cd5eaef03..d0b28bdac 100644 --- a/apps/rocm/src/dash_seam.rs +++ b/apps/rocm/src/dash_seam.rs @@ -61,6 +61,34 @@ impl RocmToolExecutor for BinToolExecutor { } } + /// The dashboard agent's own tool calls. The approval body built by + /// [`Self::execute`] shows the command line, which the operator may copy, + /// so a model-chosen value is held to `validate_model_chosen_tool_call` + /// first; a slash command the operator typed goes to [`Self::execute`] + /// directly and is not. + fn execute_model_call(&self, name: &str, args: &serde_json::Value) -> RocmToolOutcome { + let call = providers::ChatToolCall { + id: None, + name: name.to_owned(), + arguments: args.clone(), + }; + if let Err(e) = chat::validate_chat_tool_call(&call) + .and_then(|()| chat::validate_model_chosen_tool_call(&call)) + { + return RocmToolOutcome::Error(e.to_string()); + } + let outcome = self.execute(name, args); + // The plan for a request the model wrote holds values the model chose + // on its `tool_call:` lines, so it is checked before it is shown. + if name == "natural_language_plan" + && let RocmToolOutcome::Result(value) = &outcome + && let Err(e) = chat::validate_model_chosen_plan_result(value) + { + return RocmToolOutcome::Error(e.to_string()); + } + outcome + } + fn execute_approved(&self, name: &str, args: &serde_json::Value) -> RocmToolOutcome { // Replay the approved mutating call via the captured-subprocess path // (`allow_mutation = true`). `run_internal_mcp_call` re-validates the @@ -158,6 +186,98 @@ mod tests { } } + /// A safe call the dashboard agent makes reaches the same outcome as the + /// operator's: the model-only checks refuse, and otherwise change nothing. + /// A read-only tool returns its result (only `natural_language_plan`'s + /// result is read as a plan), a mutating one is staged for approval, and a + /// plan with nothing unsafe in it is shown. + #[test] + fn seam_passes_a_safe_model_call_through_like_the_operators_own() { + let exec = BinToolExecutor::new(temp_paths()); + match exec.execute_model_call("engines", &serde_json::json!({})) { + RocmToolOutcome::Result(v) => assert!( + v.pointer("/structuredContent/engines") + .is_some_and(serde_json::Value::is_array), + "{v}" + ), + other => panic!("a safe read-only model call must run, got {other:?}"), + } + let install = serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": "/tmp/rocm-seam-model-prefix", + }); + match exec.execute_model_call("install_sdk", &install) { + RocmToolOutcome::ApprovalRequired(intent) => assert_eq!(intent.name, "install_sdk"), + other => panic!("a safe mutating model call must be staged, got {other:?}"), + } + let plan = serde_json::json!({ "request": "install therock into /mnt/rocm" }); + match exec.execute_model_call("natural_language_plan", &plan) { + RocmToolOutcome::Result(v) => assert!( + v.pointer("/structuredContent/planned_args") + .is_some_and(serde_json::Value::is_array), + "{v}" + ), + other => panic!("a safe plan must be shown, got {other:?}"), + } + } + + /// The dashboard agent's own call is held to the model-only rules, so a + /// folder with `&` in it never reaches the approval body the operator may + /// copy; the same folder typed by the operator (`/install `, which + /// goes through `execute`) is theirs and is staged for approval. + #[test] + fn seam_refuses_a_model_chosen_value_but_not_the_operators_own() { + let exec = BinToolExecutor::new(temp_paths()); + let args = serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": "/tmp/rocm-seam R&D", + }); + match exec.execute_model_call("install_sdk", &args) { + RocmToolOutcome::Error(error) => assert!( + error.contains("supply it yourself by running `rocm install sdk`"), + "{error}" + ), + other => panic!("a model-chosen `&` must be refused, got {other:?}"), + } + match exec.execute("install_sdk", &args) { + RocmToolOutcome::ApprovalRequired(intent) => assert!( + intent + .body + .iter() + .any(|line| line.contains("--prefix '/tmp/rocm-seam R&D'")), + "{:?}", + intent.body + ), + other => panic!("the operator's own folder must be staged, got {other:?}"), + } + } + + /// A request the dashboard agent hands to the planner is the model's, so + /// the plan it comes back as is checked before the dashboard shows it; the + /// same request typed by the operator (`/plan`, through `execute`) is not. + #[test] + fn seam_refuses_a_plan_for_a_model_request_but_not_the_operators_own() { + let exec = BinToolExecutor::new(temp_paths()); + let args = serde_json::json!({ "request": "install therock into /mnt/R&D/rocm" }); + match exec.execute_model_call("natural_language_plan", &args) { + RocmToolOutcome::Error(error) => assert!( + error.starts_with("the plan for the local assistant's request") + && error.contains("'&'"), + "{error}" + ), + other => panic!("the model's plan must be refused, got {other:?}"), + } + assert!( + matches!( + exec.execute("natural_language_plan", &args), + RocmToolOutcome::Result(_) + ), + "the operator's own request is planned" + ); + } + #[test] fn seam_execute_rejects_public_bind_before_approval() { // (d) an UNSAFE mutating call fails validation in execute() → Error, NOT diff --git a/apps/rocm/src/freeform_planner.rs b/apps/rocm/src/freeform_planner.rs index 877c91e90..651e660b7 100644 --- a/apps/rocm/src/freeform_planner.rs +++ b/apps/rocm/src/freeform_planner.rs @@ -101,15 +101,25 @@ pub(crate) fn freeform_plan_next_action( /// answer short-circuits rendering), so it builds once via /// [`build_freeform_plan_with_context`] directly and passes that same `plan` /// through to `execute_freeform_next_action`. +/// +/// The third value is the argv of every action the text lists as a +/// `tool_call:` line, from the same build, so the `natural_language_plan` tool +/// can report them and a caller can hold a model's request to the model-only +/// rules (`tool_policy::validate_model_chosen_plan`) without planning again. pub(crate) fn plan_freeform_request_with_context( request: &str, paths: &AppPaths, config: &RocmCliConfig, -) -> (String, Option) { +) -> (String, Option, Vec>) { let plan = build_freeform_plan_with_context(request, paths, config); let text = render_structured_request_plan(&plan, paths); + let planned_args = plan + .actions + .iter() + .map(|action| action.args.clone()) + .collect(); let action = plan_next_action(plan); - (text, action) + (text, action, planned_args) } pub(crate) fn plan_next_action(plan: StructuredRequestPlan) -> Option { @@ -370,7 +380,7 @@ fn build_freeform_plan_on( .as_ref() .map(|recipe| recipe.canonical_model_id.as_str()) .or(requested_model) - .unwrap_or(""); + .unwrap_or(tool_policy::PLAN_MODEL_PLACEHOLDER); let engine = prompt_text::infer_engine_from_request(&lower) .or_else(|| resolved_recipe.as_ref().and_then(planner_recipe_engine)) .unwrap_or(default_engine); @@ -699,7 +709,7 @@ fn serve_plan_parsed_fields_and_confidence( ("device_policy".to_owned(), device_policy_value.to_owned()), ("mode".to_owned(), "managed".to_owned()), ]; - if model == "" { + if model == tool_policy::PLAN_MODEL_PLACEHOLDER { parsed.push(("missing".to_owned(), "model".to_owned())); } if let Some(requested_model) = requested_model @@ -711,7 +721,11 @@ fn serve_plan_parsed_fields_and_confidence( parsed.push(("recipe_source".to_owned(), recipe.source.clone())); parsed.push(("recipe_dtype".to_owned(), recipe.dtype.clone())); } - let confidence = if model == "" { "medium" } else { "high" }; + let confidence = if model == tool_policy::PLAN_MODEL_PLACEHOLDER { + "medium" + } else { + "high" + }; (parsed, confidence) } @@ -1022,6 +1036,10 @@ pub(crate) fn validate_provider_planner_tool_call( format_structured_tool_call("rocm", &call.args) ), } + // The planner is a model, and its argv becomes the plan's `tool_call:` and + // `next_tool_call:` lines, which are offered to be run. Checked last, so + // the rules above keep their own refusals. + tool_policy::validate_model_chosen_argv(&call.args, "provider planner")?; Ok(call.args.clone()) } @@ -1786,6 +1804,29 @@ mod tests { assert!(rendered.contains("approval: required")); } + /// The argvs reported beside the text are the plan's `tool_call:` lines, + /// every one and in order, so checking them checks what is printed. + #[test] + fn the_planned_args_are_the_tool_call_lines_of_the_text() { + let (_root, paths) = test_paths("planned-args"); + let (text, _, planned_args) = plan_freeform_request_with_context( + "serve qwen3.5 with vllm", + &paths, + &RocmCliConfig::default(), + ); + let lines = text + .lines() + .filter_map(|line| line.trim_start().strip_prefix("tool_call: ")) + .map(str::to_owned) + .collect::>(); + let from_args = planned_args + .iter() + .map(|args| format_structured_tool_call("rocm", args)) + .collect::>(); + assert_eq!(lines.len(), 3, "{text}"); + assert_eq!(from_args, lines); + } + #[test] fn provider_planner_response_reduces_to_validated_rocm_tool_call() -> Result<()> { let content = r#"{ @@ -1885,6 +1926,40 @@ mod tests { } } + /// The planner's argv becomes the plan's `tool_call:` line, which is offered + /// to be run, so a value a Windows shell reads as syntax is refused there + /// as it is from the chat assistant, and a serve reference is held to the + /// same character set. + #[test] + fn provider_planner_refuses_values_unsafe_to_paste() { + for args in [ + vec!["install", "sdk", "--prefix", "/mnt/R&D"], + vec!["install", "sdk", "--prefix", "/mnt/o'brien"], + vec!["serve", "qwen%PATH%", "--managed"], + vec!["serve", "qwen;id", "--managed"], + ] { + let call = ProviderPlannerToolCall { + tool: "rocm".to_owned(), + args: args.iter().map(|arg| (*arg).to_owned()).collect(), + }; + let error = validate_provider_planner_tool_call(&call) + .expect_err(&format!("{args:?}")) + .to_string(); + assert!( + error.starts_with("provider planner ") + && (error.contains("supply it yourself") || error.contains("only letters")), + "{args:?}: {error}" + ); + } + let ordinary = ProviderPlannerToolCall { + tool: "rocm".to_owned(), + args: ["install", "sdk", "--prefix", "/mnt/my rocm"] + .map(str::to_owned) + .to_vec(), + }; + validate_provider_planner_tool_call(&ordinary).expect("a space is quoted, not refused"); + } + #[test] fn provider_planner_requires_user_folder_for_therock_install() { let call = ProviderPlannerToolCall { diff --git a/apps/rocm/src/main.rs b/apps/rocm/src/main.rs index 5d385cf23..342c7d832 100644 --- a/apps/rocm/src/main.rs +++ b/apps/rocm/src/main.rs @@ -279,6 +279,12 @@ fn main() -> ExitCode { /// standard `Result<(), anyhow::Error>` `Termination` behavior (print the /// error to stderr, exit 1). fn exit_code_for(result: Result<()>) -> ExitCode { + exit_code_reporting_to(result, &mut io::stderr()) +} + +/// [`exit_code_for`] with the error report written to `stderr`, so a test can +/// read what the user would see. +fn exit_code_reporting_to(result: Result<()>, stderr: &mut dyn Write) -> ExitCode { match result { Ok(()) => ExitCode::SUCCESS, Err(e) => { @@ -291,13 +297,28 @@ fn exit_code_for(result: Result<()>) -> ExitCode { // ignore a failed write here rather than `eprintln!`, which panics. // A caller with a closed stderr pipe must still see exit code 1, not // a panic that replaces it. - let _ = writeln!(io::stderr(), "Error: {e:?}"); + let _ = writeln!(stderr, "{}", render_error_for_terminal(&e)); ExitCode::FAILURE } } } } +/// The `Error: …` report `exit_code_for` prints, in the standard `{:?}` form, +/// with terminal escape sequences and other display-altering characters +/// removed: an error can quote text this process did not write — a model's +/// tool name or argument, a provider's error body — and stderr is a terminal +/// too. +/// +/// Sanitized as a whole rather than cause by cause: the standard report puts +/// each cause on its own line, and a string sequence left open ends at its +/// line, so an open sequence in one message cannot reach the next cause or the +/// `Caused by:` heading. Rebuilding the report from sanitized causes would have +/// to reproduce anyhow's layout and would drop its backtrace. +fn render_error_for_terminal(error: &anyhow::Error) -> String { + rocm_core::terminal::sanitize_terminal_text(&format!("Error: {error:?}")) +} + fn run() -> Result<()> { reset_sigpipe(); @@ -1412,6 +1433,49 @@ pub(crate) mod tests { assert_eq!(super::exit_code_for(Err(err)), ExitCode::FAILURE); } + /// `report` without the `Stack backtrace:` section anyhow appends when + /// `RUST_BACKTRACE` or `RUST_LIB_BACKTRACE` is set, so the tests below + /// compare the same text whether or not it is. + fn without_backtrace(report: &str) -> &str { + report + .split_once("\n\nStack backtrace") + .map_or(report, |(head, _)| head) + } + + /// The report as `main` writes it to stderr: an OSC left open in the + /// outermost context costs the rest of that line, never the `Caused by:` + /// heading or the causes printed after it. + #[test] + fn an_error_report_on_stderr_carries_no_escape_and_loses_no_line() { + let error = anyhow::anyhow!("provider said `x`") + .context("chat request \u{9b}2Kfailed") + .context("rocm chat could not answer\u{1b}]0;never closed"); + let mut stderr = Vec::new(); + assert_eq!( + super::exit_code_reporting_to(Err(error), &mut stderr), + ExitCode::FAILURE + ); + let stderr = String::from_utf8(stderr).expect("utf-8"); + assert!(stderr.ends_with('\n'), "{stderr:?}"); + assert_eq!( + without_backtrace(stderr.trim_end_matches('\n')), + "Error: rocm chat could not answer\n\nCaused by:\n 0: chat request failed\n 1: provider said `x`" + ); + } + + /// An error can quote a model's or a provider's text, and it is printed to + /// a terminal: the escape sequences go, the text around them stays, and + /// the `Caused by:` layout of the standard report survives. + #[test] + fn an_error_quoting_untrusted_text_is_printed_without_its_escapes() { + let error = anyhow::anyhow!("provider said `x\u{1b}[2K\u{1b}]0;title\u{7}y\u{9b}1A`") + .context("chat request failed"); + assert_eq!( + without_backtrace(&super::render_error_for_terminal(&error)), + "Error: chat request failed\n\nCaused by:\n provider said `xy`" + ); + } + /// Exercises the real `dispatch -> fix -> FixExitCode -> exit_code_for` /// chain end to end, not just `exit_code_for` in isolation. Guards against /// a future change at the `Command::Fix` dispatch arm (e.g. discarding the diff --git a/apps/rocm/src/providers.rs b/apps/rocm/src/providers.rs index f3b9fd7e6..6e6bc7318 100644 --- a/apps/rocm/src/providers.rs +++ b/apps/rocm/src/providers.rs @@ -1035,6 +1035,181 @@ mod tests { .to_string() } + /// A completion whose message asks for `calls`, each `(tool, arguments)`. + fn tool_call_completion_body(calls: &[(&str, serde_json::Value)]) -> String { + let tool_calls = calls + .iter() + .enumerate() + .map(|(index, (name, arguments))| { + serde_json::json!({ + "id": format!("call-{index}"), + "type": "function", + "function": { "name": name, "arguments": arguments.to_string() }, + }) + }) + .collect::>(); + serde_json::json!({ + "choices": [{"message": {"content": "", "tool_calls": tool_calls}}] + }) + .to_string() + } + + /// A question none of the chat's own shortcuts answers, so the model is + /// asked and its tool calls are the ones that run. + const OPEN_CHAT_QUESTION: &str = "I would like some help figuring out what to do with my GPU"; + + /// What a one-shot chat turn left behind: what `rocm chat --prompt` would + /// print (it prints only an `Ok`), and the state the refusal message says + /// the turn did not change. + struct OneShotTurn { + rendered: Result, + /// The install folder the model asked for, or its parent, exists + /// after the turn. + prefix_created: bool, + /// The managed services after the turn, by id; the planted assistant + /// is the only one before it. + service_ids: Vec, + /// Runtimes registered after the turn; there are none before it. + registered_runtimes: usize, + } + + /// Ask the one-shot chat, with ROCm tools, against a local model that + /// answers once with the body `make_body` builds from the folder the + /// model's calls name (inside this test's own root, so whether it was + /// created can be checked). + fn one_shot_chat_against( + name: &str, + make_body: impl FnOnce(&str) -> String, + ) -> Result { + let (root, paths) = temp_app_paths(name); + paths.ensure()?; + let prefix = root.join("R&D").join("rocm"); + let body = make_body(&prefix.to_string_lossy()); + let listener = TcpListener::bind("127.0.0.1:0")?; + let port = listener.local_addr()?.port(); + let server = spawn_sequenced_local_provider_test_server( + listener, + LEMONADE_ASSISTANT_MODEL_ID, + vec![body], + ); + ready_builtin_planner_service(&paths, port)?; + let rendered = crate::chat::render_chat_prompt_text( + &paths, + "local", + None, + OPEN_CHAT_QUESTION, + true, + crate::ChatInferenceParams::default(), + ); + assert_eq!( + server.join().expect("server thread")?, + 1, + "the model is asked once" + ); + let registry = crate::runtime_registry::runtime_registry_dir(&paths); + let turn = OneShotTurn { + rendered, + prefix_created: prefix.exists() || prefix.parent().is_some_and(std::path::Path::exists), + service_ids: crate::managed_service_status::load_managed_services(&paths)? + .into_iter() + .map(|record| record.service_id) + .collect(), + registered_runtimes: fs::read_dir(®istry).map_or(0, Iterator::count), + }; + let _ = fs::remove_dir_all(root); + Ok(turn) + } + + /// The state a refused turn must leave: nothing printed (so no + /// `advanced manual command:` line and no approval reaches the user), no + /// folder created, no server started, no runtime registered. + fn assert_nothing_changed_and_nothing_offered(turn: &OneShotTurn) { + assert!(turn.rendered.is_err(), "a refused turn prints nothing"); + assert!(!turn.prefix_created, "the folder must not be created"); + assert_eq!( + turn.service_ids, + ["svc-qwen-planner"], + "no server may start" + ); + assert_eq!(turn.registered_runtimes, 0, "no runtime may be registered"); + } + + fn install_into(prefix: &str) -> serde_json::Value { + serde_json::json!({ "channel": "release", "format": "wheel", "prefix": prefix }) + } + + /// The calls in the provider's reply are the model's, so the model-only + /// rules apply to them through the real turn: a folder with `&` is refused, + /// and the message's claim (nothing changed, nothing offered to paste) is + /// checked against the state the turn left. The same folder from the + /// user's own prompt is accepted: + /// `a_folder_the_user_typed_is_rendered_whatever_it_holds`. + #[test] + fn a_model_reply_asking_for_an_unsafe_folder_is_refused_through_the_chat_turn() -> Result<()> { + let turn = one_shot_chat_against("chat-model-unsafe-folder", |prefix| { + tool_call_completion_body(&[("install_sdk", install_into(prefix))]) + })?; + assert_nothing_changed_and_nothing_offered(&turn); + let error = turn.rendered.expect_err("refused").to_string(); + assert!( + error.contains("'&'") + && error.contains("supply it yourself by running `rocm install sdk`"), + "{error}" + ); + Ok(()) + } + + /// A refusal after a read-only check and an approvable call the same reply + /// asked for: the check ran, and the approvable call's command line is not + /// printed either, so "nothing was changed, and no command was offered" + /// holds for the whole turn, not only for the refused call. + #[test] + fn a_refusal_after_earlier_calls_says_and_leaves_nothing_changed() -> Result<()> { + let turn = one_shot_chat_against("chat-model-refusal-after-check", |prefix| { + tool_call_completion_body(&[ + ("path_exists", serde_json::json!({ "path": "/" })), + ("install_sdk", install_into(&prefix.replace("R&D", "plain"))), + ("install_sdk", install_into(prefix)), + ]) + })?; + assert_nothing_changed_and_nothing_offered(&turn); + let error = turn + .rendered + .expect_err("the third call is refused") + .to_string(); + assert!( + error.contains( + "Nothing was changed, and no command was offered to paste for this call." + ) && !error.contains("Nothing was run"), + "{error}" + ); + Ok(()) + } + + /// A model's request handed to the planner comes back with the values it + /// chose on the plan's `tool_call:` lines; those are held to the same + /// rules before the plan is shown. + #[test] + fn a_plan_for_a_model_request_with_an_unsafe_folder_is_refused() -> Result<()> { + let turn = one_shot_chat_against("chat-model-unsafe-plan", |prefix| { + tool_call_completion_body(&[( + "natural_language_plan", + serde_json::json!({ "request": format!("install therock into {prefix}") }), + )]) + })?; + assert_nothing_changed_and_nothing_offered(&turn); + let error = turn + .rendered + .expect_err("the plan's folder must be refused") + .to_string(); + assert!( + error.starts_with("the plan for the local assistant's request") + && error.contains("'&'"), + "{error}" + ); + Ok(()) + } + fn ready_builtin_planner_service(paths: &AppPaths, port: u16) -> Result<()> { let mut qwen = ManagedServiceRecord::new( paths, @@ -1130,7 +1305,7 @@ mod tests { // The fix: one build, reused for both the rendered text and the next // action. This is exactly what `plan_freeform_request_with_context` // (what `natural_language_plan` calls today) does. - let (text, action) = + let (text, action, _) = crate::freeform_planner::plan_freeform_request_with_context("serve", &paths, &config); let action = action.expect("provider-resolved plan should produce a next action"); assert!( diff --git a/apps/rocm/src/rocm_argv.rs b/apps/rocm/src/rocm_argv.rs index a257344f1..f6dfed11f 100644 --- a/apps/rocm/src/rocm_argv.rs +++ b/apps/rocm/src/rocm_argv.rs @@ -112,8 +112,13 @@ pub(crate) fn serve_args_request_cpu_device(args: &[String]) -> bool { /// 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. +/// itself under a POSIX shell's own word-splitting, and nothing inside a value +/// can read there as a second command. Windows shells read the same line +/// differently (PowerShell an embedded `'`, `cmd.exe` characters such as `&` +/// even inside single quotes), so a value a *model* chose is refused at +/// validation if it holds one +/// ([`paste_unsafe_character`](crate::shell_quote::paste_unsafe_character)); a +/// value the user typed is rendered as typed. /// /// Values carrying terminal control characters never get here — they are /// refused at validation diff --git a/apps/rocm/src/shell_quote.rs b/apps/rocm/src/shell_quote.rs index caf97f910..42fdfcb77 100644 --- a/apps/rocm/src/shell_quote.rs +++ b/apps/rocm/src/shell_quote.rs @@ -33,7 +33,9 @@ //! That implementation is [`rocm_core::posix_shell_quote`], the quoter //! `rocm-core` keeps for command lines with execution stakes, with its own //! real-`sh` read-back test. This module is the CLI's name for it, plus the -//! control-character predicate the chat guards share. +//! two predicates the chat guards share: the control-character one, and the +//! characters a model-chosen value may not carry onto a line meant to be +//! pasted. //! //! `rocm-dash-tui` renders its approval previews with a copy, //! `ui::exec::quote_display_arg`, because it may not depend on `rocm-core` @@ -54,9 +56,13 @@ //! double-quoted form happened to paste cleanly into `cmd.exe`; `'…'` does not. //! It is correct in any POSIX shell (bash, sh, WSL). PowerShell also reads //! `'…'` literally, but escapes an embedded `'` as `''` rather than `'\''`, so a -//! value containing a single quote is the one case that does not paste cleanly -//! there. On Windows it is still unambiguous to a *reader* — which is what an -//! approval gate needs — where the old form was ambiguous everywhere. +//! value containing a single quote does not paste cleanly there — and not +//! merely untidily: PowerShell reads `'\''` as the end of the string followed +//! by live syntax. `cmd.exe` gives single quotes no meaning, so its own +//! metacharacters stay live inside them. For a value the *model* chose, both +//! are closed by refusal rather than quoting ([`paste_unsafe_character`]); a +//! value the user typed is rendered as typed and reads correctly in a POSIX +//! shell. /// Quote `value` so a POSIX shell reads it back as exactly one word equal to /// `value`. @@ -80,23 +86,64 @@ pub(crate) fn shell_quote(value: &str) -> String { /// whoever supplied the argument. A value in this shape is refused upstream /// rather than rendered. /// -/// The predicate is [`rocm_core::terminal::is_control_or_format`] — C0, DEL -/// and C1 (so ESC and the 8-bit CSI alike), plus the Unicode format characters: -/// a bidi override such as U+202E reorders how the rest of the line is drawn, -/// and a zero-width character hides a difference between what is shown and -/// what runs. U+2028/U+2029 are added, through -/// [`rocm_core::terminal::is_control_or_line_separator`], because a terminal -/// may start a new row at either, which would let a value forge a line of its -/// own. That is the predicate the terminal classifier and the vLLM symptom -/// guard share for the same reason, so the set of line-breaking characters is -/// decided in one place. +/// The predicate is [`rocm_core::terminal::hides_or_moves_text`]: C0, DEL and +/// C1 (so ESC and the 8-bit CSI alike); the Unicode format characters, where a +/// bidi override such as U+202E reorders how the rest of the line is drawn and +/// a zero-width character hides a difference between what is shown and what +/// runs; U+2028/U+2029, at which a terminal may start a new row, letting a +/// value forge a line of its own; and the rest of Unicode's default-ignorable +/// set, such as the tag characters, which can carry a whole string with no +/// glyph. The one exception is a single joiner, non-joiner or emoji/text +/// presentation selector directly after a drawn character, and a joiner +/// directly after such a selector, so a folder named with `❤️` or `🏳️‍🌈` is +/// not refused. That is exactly what `rocm chat` keeps when it +/// prints prose ([`rocm_core::terminal::sanitize_terminal_text`]): a value is +/// refused for precisely what printing it would remove or change, plus the +/// newline and tab a one-line value must not carry. pub(crate) fn has_terminal_control_character(value: &str) -> bool { - value.chars().any(|character| { - rocm_core::terminal::is_control_or_format(character) - || rocm_core::terminal::is_control_or_line_separator(character) + rocm_core::terminal::hides_or_moves_text(value) +} + +/// The characters that make a value unsafe on a command line meant to be +/// pasted, when the value is one the model chose: `'` and the typographic +/// single quotes (U+2018 to U+201B), and `& | < > ^ % " !`. +/// +/// Every value is POSIX-quoted ([`shell_quote`]), which is enough for `sh`, +/// bash and WSL. Windows shells read the same line differently: +/// +/// * PowerShell reads `'…'` literally too, but escapes an embedded single +/// quote as `''`, and takes the typographic single quotes as quotes as well. +/// POSIX's `'\''` therefore reads there as the end of the string followed by +/// live syntax, so the rest of the value would run. +/// * `cmd.exe` gives single quotes no meaning at all, so `& | < > ^ % "` keep +/// theirs inside them: `&` and `|` start another command, `<` and `>` +/// redirect, `^` escapes, `%` expands a variable and `"` toggles quoting, and +/// with delayed expansion `!` expands a variable too. +/// +/// Quoting cannot cover all three readings at once, so a value the model chose +/// is refused if it holds one of these, and the user supplies it themselves. +/// Values the user typed are never held to this; they are rendered as typed. +/// Everything else a POSIX shell treats specially (`$`, `` ` ``, `;`, `(`, +/// spaces…) is inert inside single quotes in POSIX shells and PowerShell, and +/// stays accepted and quoted. `cmd.exe` still splits an unquoted space, so a +/// value with a space does not paste there as one argument, but nothing in it +/// can run as a command of its own. +pub(crate) fn paste_unsafe_character(value: &str) -> Option { + value.chars().find(|&character| { + matches!( + character, + '\'' | '&' | '|' | '<' | '>' | '^' | '%' | '"' | '!' + ) || TYPOGRAPHIC_SINGLE_QUOTES.contains(&u32::from(character)) }) } +/// U+2018 to U+201B, the typographic single quotes PowerShell also takes as +/// quote characters. Held as code points rather than `char` literals: they +/// are only compared against, never printed, and the ASCII-output contract +/// (`xtask/src/ascii_output_contract.rs`) reads a non-ASCII `char` literal as +/// text that may reach a console. +const TYPOGRAPHIC_SINGLE_QUOTES: std::ops::RangeInclusive = 0x2018..=0x201b; + /// The argument generator the CLI's preview property tests share. /// /// One copy in this crate, deliberately: a second generator here would not be @@ -322,4 +369,63 @@ mod tests { assert!(!has_terminal_control_character(value), "{value:?}"); } } + + /// Default-ignorable characters outside the `Cf` category: invisible, + /// and each one lets two values that look identical differ. + #[test] + fn invisible_non_format_characters_are_refused_too() { + for value in [ + "qwen\u{3164}", // Hangul filler, renders as blank space + "qwen\u{e0041}", // tag character, spells ASCII with no glyph + "qwen\u{fe00}", // variation selector other than the emoji pair + "qwen\u{fe0f}\u{fe0f}", // a run of presentation selectors + "qwen\u{034f}x", // combining grapheme joiner + ] { + assert!(has_terminal_control_character(value), "{value:?}"); + } + // A single presentation selector or joiner on a drawn character is + // what an emoji is made of, and a folder a user names may hold one. + for value in [ + "C:\\Users\\me\\\u{2764}\u{fe0f}\\rocm", + "👩\u{200d}💻", + "🏳\u{fe0f}\u{200d}🌈", + ] { + assert!(!has_terminal_control_character(value), "{value:?}"); + } + } + + #[test] + fn paste_unsafe_characters_are_the_quotes_and_the_cmd_metacharacters() { + for (value, found) in [ + ("it's", '\''), + ("a\u{2018}b", '\u{2018}'), + ("a\u{2019}b", '\u{2019}'), + ("a\u{201a}b", '\u{201a}'), + ("a\u{201b}b", '\u{201b}'), + ("C:\\R&D", '&'), + ("a|b", '|'), + ("ab", '>'), + ("a^b", '^'), + ("%USERPROFILE%\\rocm", '%'), + ("a\"b", '"'), + ("a!b", '!'), + ] { + assert_eq!(paste_unsafe_character(value), Some(found), "{value:?}"); + } + // Inert inside single quotes in POSIX shells and PowerShell, and with no + // command meaning in cmd.exe: still accepted, and quoted. + for value in [ + "/mnt/my folder", + "$(id)", + "`id`", + "a;b", + "C:\\Program Files\\ROCm", + "qwen2.5:7b", + "repo@main", + "a\u{201c}b\u{201d}", + ] { + assert_eq!(paste_unsafe_character(value), None, "{value:?}"); + } + } } diff --git a/apps/rocm/src/tool_policy.rs b/apps/rocm/src/tool_policy.rs index e32af6f58..3cc779919 100644 --- a/apps/rocm/src/tool_policy.rs +++ b/apps/rocm/src/tool_policy.rs @@ -29,7 +29,7 @@ use crate::rocm_argv::{ }; use crate::service_launch::is_loopback_host; use crate::services_text::validate_service_id; -use crate::shell_quote::has_terminal_control_character; +use crate::shell_quote::{has_terminal_control_character, paste_unsafe_character}; use crate::{providers, therock}; use anyhow::{Context, Result, bail}; use rocm_core::{DEFAULT_LOCAL_HOST, builtin_watcher, runtime_install_root_is_protected}; @@ -265,9 +265,9 @@ const CHAT_TOOLS_SCREENING_THEIR_OWN_ARGUMENTS: [&str; 1] = ["rocm_command"]; /// 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; `rocm_core::strip_ansi_sequences` is the tool for -/// that surface, and is already used on ComfyUI log output. +/// *arguments*, which are refused. Model-supplied *prose* — the reply, tool +/// results, an error quoting either — cannot be refused, so `rocm chat` cleans +/// it instead, with [`rocm_core::terminal::sanitize_terminal_text`]. /// /// 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 @@ -308,6 +308,331 @@ fn reject_control_characters(tool: &str, arguments: &serde_json::Value) -> Resul } } +/// The checks a tool call gets when the *model* chose it, on top of +/// [`validate_chat_tool_call`], which every call gets whoever made it. +/// +/// Two rules, both about the command line `rocm chat` and the dashboard print +/// for the operator to approve or paste — the argv the call is rendered as +/// ([`model_rendered_argv`]), not the JSON it arrived in, so a field that is +/// never printed (a `rocm_command` `reason`) is never refused: +/// +/// * no argument may carry a [`paste_unsafe_character`], which a Windows shell +/// reads as syntax however the line is quoted; +/// * a model-requested `serve` uses only options `rocm serve` has, and names +/// its model and every option value only with +/// [`is_model_requested_serve_token`] characters. +/// +/// A tool that renders no command line (`path_exists`, `natural_language_plan`, +/// the other read-only tools) has nothing to check here; the plan +/// `natural_language_plan` returns is checked on its own, by +/// [`validate_model_chosen_plan`]. +/// +/// Not part of [`validate_chat_tool_call`] on purpose. That function also +/// checks calls the CLI builds from what the user typed — an install folder +/// named in the prompt, say — and a value the user typed is theirs to use. So +/// the callers that receive a call from a model run this as well: the chat +/// turn for the calls in the provider's reply, and the dashboard's tool +/// executor. Run it after [`validate_chat_tool_call`], so a value carrying a +/// control character is refused for that first and never echoed. +pub(crate) fn validate_model_chosen_tool_call(call: &providers::ChatToolCall) -> Result<()> { + match model_rendered_argv(call)? { + Some(args) => validate_model_chosen_argv(&args, "local assistant"), + None => Ok(()), + } +} + +/// The `rocm` argv (without the program name) a model's tool call is shown +/// as, or `None` for a tool that shows no command line. Derived from +/// [`rocm_chat_tool_requested_args`], the same function that renders the +/// approval card and the `advanced manual command:` line, so a tool that +/// starts rendering a command is checked without being listed anywhere. +/// `install_sdk_dry_run` previews `rocm install sdk` with the same fields, and +/// `rocm_command` is checked on the argv as the model wrote it, before the +/// flags the policy adds. +fn model_rendered_argv(call: &providers::ChatToolCall) -> Result>> { + match call.name.as_str() { + "rocm_command" => normalized_chat_rocm_command_args(call).map(Some), + "install_sdk_dry_run" => Ok(rocm_chat_tool_requested_args(&providers::ChatToolCall { + name: "install_sdk".to_owned(), + ..call.clone() + })), + _ => Ok(rocm_chat_tool_requested_args(call)), + } +} + +/// [`validate_model_chosen_tool_call`]'s two rules for a whole `rocm` argv +/// (without the program name): a `rocm_command`, the provider planner's +/// `tool_call`, or a tool's rendered argv. `actor` opens each refusal. +pub(crate) fn validate_model_chosen_argv(args: &[String], actor: &str) -> Result<()> { + check_model_chosen_argv(args, actor, Placeholders::Refused) +} + +/// The plans `natural_language_plan` returned for a request the *model* +/// wrote, each a `rocm` argv as it is printed in the plan's `tool_call:` lines. +/// +/// The request is prose, and the planner turns words from it into argv values +/// — an install folder, a model name — so the line it prints holds values the +/// model chose as surely as a tool call does. The same two rules apply, except +/// that a planner placeholder such as `` is the planner's own text, not +/// a value, and passes. +pub(crate) fn validate_model_chosen_plan(planned: &[Vec]) -> Result<()> { + planned.iter().try_for_each(|args| { + check_model_chosen_argv( + args, + "the plan for the local assistant's request", + Placeholders::Allowed, + ) + }) +} + +/// [`validate_model_chosen_plan`] over the result of a `natural_language_plan` +/// tool call: the `planned_args` it reports beside the rendered text. Read +/// from the result rather than planned again, because a provider-assisted plan +/// need not come out the same twice. Call it before the result is shown. +/// +/// A result without `planned_args` as a list of string lists is refused, so +/// a result whose plans cannot be read is never shown unchecked. That includes +/// an `isError` envelope: the tool reports a failed plan as an error of the +/// call (`chat::mcp_bridge`'s `natural_language_plan` arm returns only +/// `internal_mcp_tool_success`), so an envelope without plans is not one it +/// produces, and refusing it costs nothing. +pub(crate) fn validate_model_chosen_plan_result(result: &serde_json::Value) -> Result<()> { + let planned = result + .pointer("/structuredContent/planned_args") + .and_then(serde_json::Value::as_array) + .and_then(|plans| { + plans + .iter() + .map(|args| { + args.as_array()? + .iter() + .map(|arg| arg.as_str().map(str::to_owned)) + .collect::>>() + }) + .collect::>>() + }) + .with_context(|| { + format!( + "the plan for the local assistant's request could not be read. {NOTHING_CHANGED}" + ) + })?; + validate_model_chosen_plan(&planned) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Placeholders { + Refused, + Allowed, +} + +/// The one argument the free-form planner writes where it has no value yet: +/// a `rocm serve` with no model named. Shared with `freeform_planner.rs`, which +/// writes it, so the plan check exempts exactly that string, and only as the +/// model of a `serve`, the one place the planner writes it. +pub(crate) const PLAN_MODEL_PLACEHOLDER: &str = ""; + +fn is_plan_placeholder(arg: &str) -> bool { + arg == PLAN_MODEL_PLACEHOLDER +} + +fn check_model_chosen_argv(args: &[String], actor: &str, placeholders: Placeholders) -> Result<()> { + let command = command_head(args); + let is_serve = args.first().is_some_and(|first| first == "serve"); + for (index, arg) in args.iter().enumerate() { + if placeholders == Placeholders::Allowed + && is_serve + && index == 1 + && is_plan_placeholder(arg) + { + continue; + } + reject_unpasteable_value(actor, &command, arg)?; + reject_invisible_value(actor, &command, arg)?; + } + if args + .first() + .is_some_and(|first| first.eq_ignore_ascii_case("serve")) + { + validate_model_requested_serve_argv(args, actor, placeholders)?; + } + Ok(()) +} + +/// `rocm` plus the subcommand words at the head of `args`, for naming the +/// command a refused value belongs to: `rocm install sdk`, `rocm serve`. A word +/// is taken only while it names a subcommand of the command before it in the +/// real clap tree, so the command named exists, and nothing the model chose +/// beyond a subcommand name is ever echoed by it. +fn command_head(args: &[String]) -> String { + let cli = ::command(); + let mut command = &cli; + let mut head = vec!["rocm"]; + for arg in args { + let Some(subcommand) = command.find_subcommand(arg.as_str()) else { + break; + }; + head.push(arg.as_str()); + command = subcommand; + } + head.join(" ") +} + +/// How every model-only refusal ends. "Nothing was changed" rather than +/// "nothing was run": the refusal can follow a read-only check the same reply +/// asked for, which did run, but nothing that changes the machine runs from a +/// chat turn without approval, and the refused call never reaches one. +const NOTHING_CHANGED: &str = + "Nothing was changed, and no command was offered to paste for this call."; + +/// Refuse a model-chosen `value` holding any invisible character, the single +/// joiner or presentation selector [`has_terminal_control_character`] lets +/// through included. That exemption is for a value a person typed, where an +/// emoji folder name is theirs; a model has no use for one, and after a plain +/// character it makes two values look identical on the approval card while +/// naming different things. +fn reject_invisible_value(actor: &str, command: &str, value: &str) -> Result<()> { + if value.chars().any(rocm_core::terminal::is_display_altering) { + let value = value.escape_default(); + bail!( + "{actor} chose the argument \"{value}\" for `{command}`, which holds an invisible \ + character that would make it look like a different value. {NOTHING_CHANGED} If you \ + meant that value, supply it yourself by running `{command}` with it" + ); + } + Ok(()) +} + +/// Refuse `value` if it holds a [`paste_unsafe_character`]. The message names +/// the character and the command, never the whole line: printing the line is +/// what the refusal exists to avoid. The value and the character are shown +/// escaped to ASCII (`escape_default`), so a typographic quote reaches a +/// console on any code page as `\u{2019}` rather than as mojibake; the value +/// has already passed the control-character check. +fn reject_unpasteable_value(actor: &str, command: &str, value: &str) -> Result<()> { + if let Some(character) = paste_unsafe_character(value) { + let value = value.escape_default(); + let character = character.escape_default(); + bail!( + "{actor} chose the argument \"{value}\" for `{command}`, and its '{character}' is not \ + safe on a command line meant to be pasted: a Windows shell reads it as syntax \ + (PowerShell a single quote, cmd.exe characters such as & or %). {NOTHING_CHANGED} If \ + you meant that value, supply it yourself by running `{command}` with it" + ); + } + Ok(()) +} + +/// The characters a model may use in a `rocm serve` model reference or option +/// value it asks for: ASCII letters and digits and `. _ - / : @ + \`, at most +/// 256 of them, not starting with `-`. +/// +/// That covers model ids (`qwen2.5-7b-instruct`, `Qwen/Qwen2.5-7B-Instruct`), +/// tags and quantisations (`qwen2.5:7b`, `repo:Q4_K_M`), revisions +/// (`repo@main`) and local model files on Linux and Windows +/// (`/models/a.gguf`, `C:\models\a.gguf`), and nothing a shell, a terminal or +/// a second reading could take for anything but those. The `serve` line is the +/// one a model is most likely to be asked for, and a model reference has no +/// business carrying `;`, `$`, a quote or a space, so such a value is refused +/// rather than quoted and left to the operator to read correctly. A model file +/// whose path has a space in it can still be served by running `rocm serve` +/// directly; `rocm serve` itself is not held to this. +fn is_model_requested_serve_token(value: &str) -> bool { + !value.is_empty() + && value.len() <= 256 + && !value.starts_with('-') + && value.chars().all(|character| { + character.is_ascii_alphanumeric() + || matches!(character, '.' | '_' | '-' | '/' | ':' | '@' | '+' | '\\') + }) +} + +fn validate_model_requested_serve_token( + actor: &str, + what: &str, + value: &str, + placeholders: Placeholders, +) -> Result<()> { + if placeholders == Placeholders::Allowed && is_plan_placeholder(value) { + return Ok(()); + } + if !is_model_requested_serve_token(value) { + let value = value.escape_default(); + bail!( + "{actor} requested serve {what} \"{value}\"; only letters, digits and \ + `. _ - / : @ + \\` are accepted, not starting with `-`. {NOTHING_CHANGED} To serve \ + anything else, run `rocm serve` yourself" + ); + } + Ok(()) +} + +/// Hold a model-requested `serve` argv to the shape a model has any reason to +/// ask for: every `--option` one `rocm serve` actually has, its value — the +/// next argument, or attached as `--option=value` — and the model reference +/// held to [`is_model_requested_serve_token`], and no short or unknown option. +/// +/// The options are read from the CLI definition itself, so an option added to +/// `rocm serve` is accepted without being listed here, and one that is not +/// there cannot be used to slide a value past the check. A value has to be a +/// serve token, so it cannot begin with `-` either: `--gpu-memory-utilization +/// -0.9;id` is refused, not read as a flag followed by nothing. +fn validate_model_requested_serve_argv( + args: &[String], + actor: &str, + placeholders: Placeholders, +) -> Result<()> { + let cli = ::command(); + let serve = cli + .find_subcommand("serve") + .context("the CLI definition has no serve command")?; + let mut tokens = args.iter().skip(1); + let mut models = 0_usize; + while let Some(token) = tokens.next() { + if let Some(long) = token.strip_prefix("--") { + let (name, attached) = match long.split_once('=') { + Some((name, value)) => (name, Some(value)), + None => (long, None), + }; + let Some(option) = serve + .get_arguments() + .find(|argument| argument.get_long() == Some(name)) + else { + bail!( + "{actor} requested serve option \"--{}\", which `rocm serve` does not have. \ + {NOTHING_CHANGED}", + name.escape_default() + ); + }; + let what = format!("`--{name}` value"); + if let Some(value) = attached { + validate_model_requested_serve_token(actor, &what, value, placeholders)?; + } else if option.get_action().takes_values() { + let Some(value) = tokens.next() else { + bail!( + "{actor} requested serve option `--{name}` without a value. {NOTHING_CHANGED}" + ); + }; + validate_model_requested_serve_token(actor, &what, value, placeholders)?; + } + } else { + // The model reference, or a lone `-x`: a model-requested serve uses + // long options only, and the token check refuses a leading `-`. + validate_model_requested_serve_token(actor, "model", token, placeholders)?; + models += 1; + } + } + // Exactly one model reference. A `launch_server` model of + // `--allow-public-bind` renders as an option, not a model, and leaves + // none; that line must not be offered either. + if models != 1 { + bail!( + "{actor} requested a serve with {models} model references instead of one. \ + {NOTHING_CHANGED} To serve anything else, run `rocm serve` yourself" + ); + } + 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<()> { @@ -2886,4 +3211,466 @@ mod tests { .expect("a managed serve passes in any case"); } } + + fn install_sdk_call(name: &str, prefix: &str) -> providers::ChatToolCall { + providers::ChatToolCall { + id: None, + name: name.to_owned(), + arguments: serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": prefix, + }), + } + } + + /// Every character a Windows shell reads as syntax inside a POSIX + /// single-quoted word, in a value the model chose, is refused on every + /// surface a model can put an argv on — and the refusal tells the user to + /// supply the value themselves. The same value from the user passes. + #[test] + fn a_model_chosen_value_a_windows_shell_would_read_as_syntax_is_refused() { + for character in [ + '\'', '\u{2018}', '\u{2019}', '\u{201a}', '\u{201b}', '&', '|', '<', '>', '^', '%', + '"', '!', + ] { + let prefix = format!("/mnt/data/rocm{character}x"); + for tool in ["install_sdk", "install_sdk_dry_run"] { + let call = install_sdk_call(tool, &prefix); + // Not a structural problem: the user may use this folder. + validate_chat_tool_call(&call) + .unwrap_or_else(|error| panic!("{tool} {prefix:?}: {error:#}")); + let error = validate_model_chosen_tool_call(&call) + .expect_err(&format!("{tool} accepted a model-chosen {prefix:?}")) + .to_string(); + assert!( + error.contains(&format!("'{}'", character.escape_default())) + && error.is_ascii() + && error.contains( + "If you meant that value, supply it yourself by running \ + `rocm install sdk` with it" + ), + "{tool}: {error}" + ); + } + let call = rocm_command_call(&["install", "sdk", "--prefix", &prefix]); + validate_chat_tool_call(&call).expect("structurally valid"); + let error = validate_model_chosen_tool_call(&call) + .expect_err(&format!("rocm_command accepted {prefix:?}")) + .to_string(); + assert!(error.contains("`rocm install sdk`"), "{error}"); + } + } + + /// The rest of what a POSIX shell treats specially is inert inside single + /// quotes in POSIX shells and PowerShell and starts nothing in `cmd.exe`, + /// so it is still accepted from the model, and quoted. + #[test] + fn other_shell_special_characters_in_a_model_chosen_value_are_still_quoted() { + let call = install_sdk_call("install_sdk", "/mnt/data/a;b $(c) `d` e"); + validate_chat_tool_call(&call).expect("valid"); + validate_model_chosen_tool_call(&call).expect("nothing here is live in a Windows shell"); + let request = chat_tool_approval_request(&call, None).expect("accepted"); + assert!( + request + .display_command + .as_deref() + .is_some_and(|line| line.contains("--prefix '/mnt/data/a;b $(c) `d` e'")), + "{:?}", + request.display_command + ); + } + + /// Tools whose arguments are never rendered into a command line keep + /// accepting the characters: `path_exists` only probes a path. + #[test] + fn a_probe_path_may_hold_an_apostrophe() { + let probe = providers::ChatToolCall { + id: None, + name: "path_exists".to_owned(), + arguments: serde_json::json!({ "path": "/home/o'brien & co" }), + }; + validate_chat_tool_call(&probe).expect("valid"); + validate_model_chosen_tool_call(&probe).expect("nothing is rendered to paste"); + } + + /// A model reference built to run a second command when the printed `serve` + /// line is pasted. `${IFS}` rather than spaces, so a check that looked only + /// for whitespace leaves it whole. + const SERVE_INJECTION_MODEL: &str = "qwen;curl${IFS}-s${IFS}https://example.invalid/x"; + + fn launch_server_call(model: &str) -> providers::ChatToolCall { + providers::ChatToolCall { + id: None, + name: "launch_server".to_owned(), + arguments: serde_json::json!({ "model": model, "host": "127.0.0.1" }), + } + } + + #[test] + fn a_model_requested_serve_reference_outside_the_charset_is_refused() { + for model in [ + SERVE_INJECTION_MODEL, + "qwen$(id)", + "qwen`id`", + "my model", + "--allow-public-bind", + "qwen=x", + ] { + let error = validate_model_chosen_tool_call(&launch_server_call(model)) + .expect_err(&format!("launch_server accepted {model:?}")) + .to_string(); + // `--allow-public-bind` renders as an option, leaving no model. + assert!( + (error.contains("only letters, digits") || error.contains("0 model references")) + && error.contains("Nothing was changed") + && error.contains("run `rocm serve` yourself"), + "{model:?}: {error}" + ); + } + // The generic tool reaches `serve` too, and is held to the same rule, + // attached option values included. + for args in [ + vec!["serve", SERVE_INJECTION_MODEL, "--managed"], + vec!["serve", "qwen", "--managed", "--engine=vllm;id"], + vec!["serve", "qwen", "--managed", "--runtime-id", "a b"], + ] { + let error = validate_model_chosen_tool_call(&rocm_command_call(&args)) + .expect_err(&format!("{args:?}")) + .to_string(); + assert!(error.contains("only letters, digits"), "{args:?}: {error}"); + } + } + + #[test] + fn ordinary_model_references_are_still_servable() { + for model in [ + "qwen", + "qwen2.5-7b-instruct", + "Qwen/Qwen2.5-7B-Instruct", + "qwen2.5:7b", + "unsloth/Qwen3-4B-GGUF:Q4_K_M", + "repo@main", + "/models/a.gguf", + "C:\\models\\a.gguf", + ] { + let call = launch_server_call(model); + validate_chat_tool_call(&call).expect("valid"); + validate_model_chosen_tool_call(&call) + .unwrap_or_else(|error| panic!("{model:?} must be servable: {error:#}")); + } + let call = rocm_command_call(&[ + "serve", + "Qwen/Qwen2.5-7B-Instruct", + "--managed", + "--engine", + "vllm", + "--port", + "8001", + "--host=127.0.0.1", + ]); + validate_model_chosen_tool_call(&call).expect("an ordinary serve line is accepted"); + } + + #[test] + fn a_refusal_names_only_the_subcommand_never_a_model_value() { + use crate::test_support::owned_argv; + + assert_eq!( + command_head(&owned_argv(&["install", "sdk", "--prefix", "x"])), + "rocm install sdk" + ); + assert_eq!( + command_head(&owned_argv(&["serve", "qwen", "--managed"])), + "rocm serve" + ); + assert_eq!( + command_head(&owned_argv(&["engines", "install", "vllm"])), + "rocm engines install" + ); + assert_eq!( + command_head(&owned_argv(&["update", "--apply"])), + "rocm update" + ); + assert_eq!(command_head(&owned_argv(&["x&y", "sdk"])), "rocm"); + // A second word is kept only if it is a subcommand of the first, so + // the command named always exists. + assert_eq!( + command_head(&owned_argv(&["config", "bogus", "x&"])), + "rocm config" + ); + assert_eq!( + command_head(&owned_argv(&["engines", "qwen", "a&b"])), + "rocm engines" + ); + } + + /// One minimal call per tool that may render a command line, as the + /// approval card does, and one per tool that renders none. + fn rendering_tool_calls() -> Vec { + let call = |name: &str, arguments: serde_json::Value| providers::ChatToolCall { + id: None, + name: name.to_owned(), + arguments, + }; + vec![ + install_sdk_call("install_sdk", "/mnt/data/rocm"), + install_sdk_call("install_sdk_dry_run", "/mnt/data/rocm"), + call("install_engine", serde_json::json!({ "engine": "vllm" })), + launch_server_call("qwen"), + call("stop_server", serde_json::json!({ "service_id": "svc-1" })), + call( + "watcher_enable", + serde_json::json!({ "watcher": "server-recover" }), + ), + call( + "watcher_disable", + serde_json::json!({ "watcher": "server-recover" }), + ), + rocm_command_call(&["serve", "qwen", "--managed"]), + ] + } + + /// Completeness, so dropping a tool from the check cannot pass unnoticed: + /// every call whose approval card shows a command line is checked on that + /// argv, and a tool that shows none is not. + #[test] + fn every_tool_that_shows_a_command_line_is_checked_on_it() { + for call in rendering_tool_calls() { + let argv = model_rendered_argv(&call) + .unwrap_or_else(|error| panic!("{}: {error:#}", call.name)) + .unwrap_or_else(|| panic!("{} shows a command but is not checked", call.name)); + assert!(!argv.is_empty(), "{}", call.name); + if call.name != "install_sdk_dry_run" { + let approval = chat_tool_approval_request(&call, None) + .unwrap_or_else(|error| panic!("{}: {error:#}", call.name)); + let shown = approval.display_command.expect("a command line is shown"); + for arg in &argv { + assert!( + shown.contains(&crate::shell_quote::shell_quote(arg)), + "{}: {arg:?} is checked but not in {shown:?}", + call.name + ); + } + } + } + for name in [ + "path_exists", + "natural_language_plan", + "examine", + "services", + ] { + let call = providers::ChatToolCall { + id: None, + name: name.to_owned(), + arguments: serde_json::json!({ "path": "/tmp", "request": "x" }), + }; + assert_eq!( + model_rendered_argv(&call).expect("no argv to build"), + None, + "{name}" + ); + } + } + + /// Each value the approval card renders is checked, field by field: a + /// check that read only `model` or only `prefix` would leave the rest. + #[test] + fn every_rendered_field_of_a_model_call_is_checked() { + let engine = providers::ChatToolCall { + id: None, + name: "install_engine".to_owned(), + arguments: serde_json::json!({ "engine": "vllm&next" }), + }; + validate_chat_tool_call(&engine).expect("structurally valid"); + assert!(validate_model_chosen_tool_call(&engine).is_err()); + let engine_runtime = providers::ChatToolCall { + id: None, + name: "install_engine".to_owned(), + arguments: serde_json::json!({ "engine": "vllm", "runtime_id": "r|x" }), + }; + assert!(validate_model_chosen_tool_call(&engine_runtime).is_err()); + for (field, value) in [ + ("engine", "vllm;id"), + ("device", "gpu;id"), + ("runtime_id", "r;id"), + ("env_id", "e;id"), + ] { + let mut call = launch_server_call("qwen"); + call.arguments[field] = serde_json::json!(value); + let error = validate_model_chosen_tool_call(&call) + .expect_err(&format!("launch_server accepted {field}={value:?}")) + .to_string(); + assert!(error.contains("only letters, digits"), "{field}: {error}"); + } + for (field, value) in [("version", "7.0&x"), ("prefix", "/mnt/a%b%")] { + let mut call = install_sdk_call("install_sdk", "/mnt/data/rocm"); + call.arguments[field] = serde_json::json!(value); + let error = validate_model_chosen_tool_call(&call) + .expect_err(&format!("install_sdk accepted {field}={value:?}")) + .to_string(); + assert!( + error.contains("supply it yourself") || error.contains("version"), + "{field}: {error}" + ); + } + } + + /// A field that is never printed is not refused on behalf of a line it + /// never reaches: `rocm_command`'s `reason` is prose for the model. + #[test] + fn a_field_that_is_never_shown_is_not_refused() { + let call = providers::ChatToolCall { + id: None, + name: "rocm_command".to_owned(), + arguments: serde_json::json!({ + "args": ["serve", "qwen", "--managed"], + "reason": "the user's folder & the model's \"default\"", + }), + }; + validate_chat_tool_call(&call).expect("valid"); + validate_model_chosen_tool_call(&call).expect("the reason is never printed"); + } + + /// A `serve` the model asks for cannot reach the approval card with a + /// value hidden behind a leading `-`, an option `rocm serve` lacks, a + /// short option, or no model at all. + #[test] + fn a_model_requested_serve_cannot_slip_a_value_past_the_check() { + for args in [ + vec![ + "serve", + "qwen", + "--managed", + "--gpu-memory-utilization", + "-0.9;id", + ], + vec!["serve", "qwen", "--managed", "--x;id"], + vec!["serve", "qwen", "--managed", "--engine", "-a;b"], + vec!["serve", "qwen", "--managed", "--engine=-a"], + vec!["serve", "qwen", "--managed", "-x"], + vec!["serve", "qwen", "--managed", "--engine"], + vec!["serve", "--managed"], + vec!["serve", "qwen", "other", "--managed"], + ] { + let error = validate_model_chosen_tool_call(&rocm_command_call(&args)) + .expect_err(&format!("{args:?} was accepted")) + .to_string(); + assert!(error.contains("Nothing was changed"), "{args:?}: {error}"); + } + } + + /// The edges of the serve character set: `+` belongs to it, and so does a + /// reference of exactly 256 characters; one more does not. + #[test] + fn the_serve_character_set_has_exact_edges() { + validate_model_chosen_tool_call(&launch_server_call("repo+lora:Q4")) + .expect("`+` is in the set"); + let longest = "a".repeat(256); + validate_model_chosen_tool_call(&launch_server_call(&longest)).expect("256 fits"); + let too_long = "a".repeat(257); + assert!(validate_model_chosen_tool_call(&launch_server_call(&too_long)).is_err()); + } + + /// The plan `natural_language_plan` returns for a model's request is held + /// to the same rules; a planner placeholder is the planner's own text. + #[test] + fn a_plan_for_a_model_request_is_held_to_the_model_rules() { + use crate::test_support::owned_argv; + + for plan in [ + owned_argv(&["install", "sdk", "--prefix", "D:\\R&D\\rocm"]), + owned_argv(&["serve", "qwen & more", "--managed"]), + owned_argv(&["serve", "qwen", "--managed", "--x;id"]), + ] { + let error = validate_model_chosen_plan(std::slice::from_ref(&plan)) + .expect_err(&format!("{plan:?}")) + .to_string(); + assert!( + error.starts_with("the plan for the local assistant's request") + && error.contains("Nothing was changed"), + "{error}" + ); + } + validate_model_chosen_plan(&[owned_argv(&["serve", "", "--managed"])]) + .expect("a placeholder is not a value"); + validate_model_chosen_plan(&[owned_argv(&["install", "sdk", "--prefix", ""])]) + .expect_err("only the planner's own placeholder passes"); + validate_model_chosen_plan(&[owned_argv(&["serve", "", "--managed"])]) + .expect_err("a value shaped like a placeholder is still a value"); + // The placeholder passes only as a serve's model, where the planner + // writes it; a request that puts the same text anywhere else is refused. + for elsewhere in [ + owned_argv(&["install", "sdk", "--prefix", ""]), + // Second like a serve's model, but not a serve. + owned_argv(&["engines", ""]), + owned_argv(&["serve", "qwen", "--engine", "", "--managed"]), + owned_argv(&["serve", "qwen", "--engine=", "--managed"]), + ] { + validate_model_chosen_plan(std::slice::from_ref(&elsewhere)) + .expect_err(&format!("{elsewhere:?}")); + } + // And never in a call: only a plan has a placeholder. + validate_model_chosen_argv( + &owned_argv(&["serve", "", "--managed"]), + "local assistant", + ) + .expect_err("a model's call has no placeholder"); + // Every plan is checked, not only the last: the unsafe one is first. + let result = serde_json::json!({ + "structuredContent": { + "planned_args": [["install", "sdk", "--prefix", "/mnt/R&D"], ["examine"]] + } + }); + let error = validate_model_chosen_plan_result(&result) + .expect_err("an earlier plan is checked") + .to_string(); + assert!(error.contains("/mnt/R&D"), "{error}"); + let clean = serde_json::json!({ + "structuredContent": { "planned_args": [["examine"], ["serve", "", "--managed"]] } + }); + validate_model_chosen_plan_result(&clean).expect("nothing a model chose is unsafe"); + } + + /// A result whose plans cannot be read is refused rather than passed with + /// nothing checked, an error envelope included. + #[test] + fn a_plan_result_that_cannot_be_read_is_refused() { + for result in [ + serde_json::json!({ "isError": false, "structuredContent": {} }), + serde_json::json!({ "structuredContent": { "planned_args": "serve x" } }), + serde_json::json!({ "structuredContent": { "planned_args": [["serve", 7]] } }), + serde_json::json!({ "structuredContent": { "planned_args": [["examine"], "serve"] } }), + serde_json::json!({ "isError": true }), + ] { + let error = validate_model_chosen_plan_result(&result) + .expect_err(&format!("{result}")) + .to_string(); + assert!(error.contains("could not be read"), "{error}"); + } + } + + /// The emoji exemption is for values a person typed. A model-chosen value + /// holding even a single joiner or presentation selector is refused: after + /// a plain character it shapes nothing, and the value would look like + /// another on the approval card while naming something else. + #[test] + fn a_model_chosen_value_with_any_invisible_character_is_refused() { + for prefix in [ + "/mnt/data\u{200d}", + "/mnt/dat\u{fe0f}a", + "/mnt/\u{2764}\u{fe0f}", + ] { + let call = install_sdk_call("install_sdk", prefix); + validate_chat_tool_call(&call) + .unwrap_or_else(|error| panic!("a user may name {prefix:?}: {error:#}")); + let error = validate_model_chosen_tool_call(&call) + .expect_err(&format!("a model-chosen {prefix:?} must be refused")) + .to_string(); + assert!( + error.contains("invisible character") && error.is_ascii(), + "{prefix:?}: {error}" + ); + } + } } diff --git a/crates/rocm-core/src/gguf_fit.rs b/crates/rocm-core/src/gguf_fit.rs index 1a97dd0e6..e4ecf570a 100644 --- a/crates/rocm-core/src/gguf_fit.rs +++ b/crates/rocm-core/src/gguf_fit.rs @@ -511,15 +511,14 @@ fn cannot( } /// Text from a stranger's header as it may be shown: every character that would -/// change how a terminal renders the text around it (a control, a -/// bidirectional override, a line separator) becomes `?`. The architecture name -/// is the one header string the report prints. +/// change how a terminal renders the text around it, or that it would not show +/// (a control, a bidirectional override, a line separator, any other +/// default-ignorable character: `terminal::is_display_altering`) becomes `?`. +/// The architecture name is the one header string the report prints. fn shown_text(text: &str) -> String { text.chars() .map(|c| { - if crate::terminal::is_control_or_format(c) - || crate::terminal::is_control_or_line_separator(c) - { + if crate::terminal::is_display_altering(c) { '?' } else { c @@ -2204,6 +2203,23 @@ mod tests { assert!(evidence_lines(&result).iter().all(|line| line.is_ascii())); } + /// Invisible characters outside the `Cf` category are replaced too: a + /// Hangul filler draws as blank space and a variation selector draws + /// nothing, so either would let a name differ from what is shown. + #[test] + fn an_architecture_name_cannot_hide_text_behind_an_invisible_character() { + let builder = llama_like(10).string("general.architecture", "lla\u{3164}ma\u{FE0F}"); + let (header, size) = header_of(&builder, 4096); + let result = estimate( + &header, + size, + 1024, + KvCacheType::F16, + AcceleratorMemory::Dedicated(8.0), + ); + assert_eq!(result.architecture.as_deref(), Some("lla?ma?")); + } + /// U+2028 and U+2029 are line separators, not controls: a terminal and a /// log viewer may break the line on them, so they are replaced as well. #[test] diff --git a/crates/rocm-core/src/gguf_source.rs b/crates/rocm-core/src/gguf_source.rs index 74b0b12da..cec725f44 100644 --- a/crates/rocm-core/src/gguf_source.rs +++ b/crates/rocm-core/src/gguf_source.rs @@ -579,14 +579,11 @@ impl LiveGgufSource { /// Whether a file name from a repository listing may be requested and shown: no /// empty, `.` or `..` path segment, and no character that changes how a terminal -/// shows the text around it: the predicates in `terminal` decide which, so this -/// and the other untrusted text the CLI prints are held to one set. +/// shows the text around it, or that it does not show at all: +/// `terminal::is_display_altering` decides which. fn is_safe_listed_name(name: &str) -> bool { !name.is_empty() - && !name.chars().any(|c| { - crate::terminal::is_control_or_format(c) - || crate::terminal::is_control_or_line_separator(c) - }) + && !name.chars().any(crate::terminal::is_display_altering) && name .split('/') .all(|segment| !segment.is_empty() && segment != "." && segment != "..") @@ -1052,6 +1049,11 @@ mod tests { "m-Q4_K_M\u{2064}.gguf", "m-Q4\u{00AD}_K_M.gguf", "m-Q4_K_M\u{E0041}.gguf", + // Invisible but outside `Cf`, which the shared predicate adds: + // a Hangul filler draws as blank space, a variation selector draws + // nothing. + "m-Q4_K_M\u{3164}.gguf", + "m-Q4_K_M\u{FE0F}.gguf", ] { assert!(!is_safe_listed_name(name), "{name:?}"); } diff --git a/crates/rocm-core/src/terminal.rs b/crates/rocm-core/src/terminal.rs index c3250e30e..1cf4d429c 100644 --- a/crates/rocm-core/src/terminal.rs +++ b/crates/rocm-core/src/terminal.rs @@ -19,14 +19,21 @@ //! * The surfaces that render such a line back inside a command the user is //! invited to paste reject it rather than rewrite it //! ([`quotable_in_single_quotes`]). +//! * `rocm chat` prints a model's reply, tool output and error text, which are +//! untrusted too and span many lines; [`sanitize_terminal_text`] removes the +//! sequences from them but keeps the line structure and shows any concealed +//! text. Values a model chose for a command line are refused instead, for any +//! [`is_display_altering`] character. //! -//! A second, independent scan for the second caller would have been a second -//! grammar to get wrong, and the first one took three rounds to get right. So -//! the grammar lives here once, in one private stepping function, and each -//! caller interprets the classified tokens it yields. The third needs no walk -//! at all, only the character predicates ([`is_control_or_format`] and -//! [`is_control_or_line_separator`]) the other two end on — which is why it -//! lives here rather than beside either of its two callers. +//! An independent scan for each further caller would have been another grammar +//! to get wrong, and the first one took three rounds to get right. So the +//! grammar lives here once, in one private stepping function and the +//! escape-skipping helpers it uses: the stripper and the line splitter +//! interpret the classified tokens it yields, and [`sanitize_terminal_text`] +//! skips sequences with the same helpers. The quotability check needs no walk +//! at all, only a character predicate ([`is_display_altering`]) that takes in +//! the characters the others end on — which is why it lives here rather than +//! beside any one caller. //! //! # Line breaks inside a string body: merging and losing rows //! @@ -46,7 +53,7 @@ //! //! A terminator is not what creates the shape, as the second example shows: it //! contains no `BEL` and no `ST` anywhere, and the two rows still merge. The -//! body scan runs to the next `BEL`, to `ST` (`ESC \`), to an unrelated `ESC` +//! body scan runs to the next `BEL`, to `ST` (`ESC \` or U+009C), to an unrelated `ESC` //! — which it leaves in place for the next step to re-read, so the hole stays //! one sequence wide — or to the end of the input, and the break is swallowed //! in every one of those cases, terminated or not. @@ -164,19 +171,119 @@ pub fn is_control_or_format(c: char) -> bool { /// `U+2028 LINE SEPARATOR` and `U+2029 PARAGRAPH SEPARATOR` are `Zl`/`Zp`, so /// neither `char::is_control` nor [`is_control_or_format`]'s enumerated `Cf` /// set covers them, yet a terminal draws the text after either on the next row. -/// The two places that must not let that happen — this module's classifier and -/// [`quotable_in_single_quotes`], the guard on what may be quoted into the -/// `rocm diagnose --symptom '...'` command the OOM surfaces print — therefore -/// share this predicate instead of each deciding for itself what breaks a line. -/// The classifier was widened to the pair when this walk moved out of the vLLM -/// engine (the commit that moved it says "behaviour is unchanged", which is -/// true of everything except this); the guard was not, and a line separator -/// went on riding into the printed command. +/// This module's classifier ends a line on it, and [`is_display_altering`] +/// includes it, so [`quotable_in_single_quotes`], the guard on what may be +/// quoted into the `rocm diagnose --symptom '...'` command the OOM surfaces +/// print, refuses a line separator too. #[must_use] pub fn is_control_or_line_separator(c: char) -> bool { c.is_control() || matches!(c, '\u{2028}' | '\u{2029}') } +/// Whether `c` has Unicode's `Default_Ignorable_Code_Point` property. +/// +/// Those are the code points (`DerivedCoreProperties.txt`) a renderer shows +/// nothing for when it has no special handling for them, which is how a +/// terminal treats them. +/// +/// Written out from the property rather than picked by hand, and wider than +/// [`is_control_or_format`]'s `Cf` set: the Hangul fillers (`Lo`) render as +/// blank space, the variation selectors (`Mn`) and the combining grapheme +/// joiner draw nothing, and the tag block holds a run that can spell out +/// arbitrary ASCII with no glyph at all. A value carrying any of them looks +/// identical to one without. +#[must_use] +pub const fn is_default_ignorable(c: char) -> bool { + matches!(c, + '\u{00ad}' // soft hyphen + | '\u{034f}' // combining grapheme joiner + | '\u{061c}' // Arabic letter mark + | '\u{115f}'..='\u{1160}' // Hangul choseong and jungseong fillers + | '\u{17b4}'..='\u{17b5}' // Khmer inherent vowels + | '\u{180b}'..='\u{180f}' // Mongolian variation selectors, vowel separator + | '\u{200b}'..='\u{200f}' // zero-width space and joiners, LRM, RLM + | '\u{202a}'..='\u{202e}' // bidi embeddings and overrides + | '\u{2060}'..='\u{206f}' // word joiner, invisible operators, bidi isolates + | '\u{3164}' // Hangul filler + | '\u{fe00}'..='\u{fe0f}' // variation selectors + | '\u{feff}' // zero-width no-break space (byte-order mark) + | '\u{ffa0}' // halfwidth Hangul filler + | '\u{fff0}'..='\u{fff8}' // unassigned specials + | '\u{1bca0}'..='\u{1bca3}' // shorthand format controls + | '\u{1d173}'..='\u{1d17a}' // musical symbol format controls + | '\u{e0000}'..='\u{e0fff}') // tags, variation selectors supplement, reserved +} + +/// Whether `c` changes how text is displayed rather than being displayed: +/// a control, a `Cf` format character, a line or paragraph separator, or any +/// other default-ignorable code point. +/// +/// The union of [`is_control_or_format`], [`is_control_or_line_separator`] and +/// [`is_default_ignorable`]: what [`sanitize_terminal_text`] removes from prose +/// apart from the layout characters and the glyph-shaping characters it keeps +/// ([`is_kept_glyph_shaper`]), and what [`hides_or_moves_text`] refuses in a +/// value on the same terms. +#[must_use] +pub fn is_display_altering(c: char) -> bool { + is_control_or_format(c) || is_control_or_line_separator(c) || is_default_ignorable(c) +} + +/// The four invisible characters that may stay in printed text: the +/// zero-width non-joiner and joiner, and the text and emoji presentation +/// selectors (VS15, VS16). They pick how the visible character before them is +/// drawn — joining an emoji sequence, choosing the emoji form of `❤` — and +/// move nothing. The other variation selectors (VS1–VS14, the supplement at +/// U+E0100, the Mongolian ones) are not kept: almost no terminal text needs +/// them, and 256 of them can spell a payload with no glyph. +const fn shapes_the_preceding_glyph(c: char) -> bool { + matches!(c, '\u{200c}' | '\u{200d}' | '\u{fe0e}' | '\u{fe0f}') +} + +/// Whether `c`, coming after `before` and then `previous`, is one of the +/// glyph-shaping characters kept in printed text: [`shapes_the_preceding_glyph`] +/// directly after a character that is itself drawn, or a joiner directly after +/// a presentation selector that is itself kept, as in `🏳` VS16 ZWJ `🌈`, which +/// is how most joined emoji with an emoji-form base are spelled. Anything +/// else — a selector after a joiner, a second joiner or a second selector in +/// a row, one at the start of the text or after a line break — shapes nothing +/// and is dropped, so a run of them cannot carry hidden text. One kept shaper +/// per drawn character can still carry a little, which is why a value a model +/// chose is refused for any of them. One rule for prose and for the values a +/// user supplies, so such a value is refused for exactly what printing would +/// remove. +fn is_kept_glyph_shaper(before: Option, previous: Option, c: char) -> bool { + let is_drawn = |c: Option| c.is_some_and(|c| !is_display_altering(c)); + if !shapes_the_preceding_glyph(c) { + return false; + } + is_drawn(previous) + || (matches!(c, '\u{200c}' | '\u{200d}') + && matches!(previous, Some('\u{fe0e}' | '\u{fe0f}')) + && is_drawn(before)) +} + +/// Whether `value` holds anything a terminal would not show as it is. +/// +/// That is a control (newline and tab included), an escape sequence, a format +/// character, a line separator, or a default-ignorable character other than a +/// glyph-shaping one after a drawn character ([`is_kept_glyph_shaper`]). +/// +/// That is exactly the set [`sanitize_terminal_text`] would remove or change, +/// plus `\n` and `\t`, which prose keeps but a one-line value must not carry. +/// So a value is refused for precisely what printing it as prose would not +/// show, and an emoji such as `❤️` (heart plus VS16) is not refused. +#[must_use] +pub fn hides_or_moves_text(value: &str) -> bool { + let (mut before, mut previous) = (None, None); + for c in value.chars() { + if is_display_altering(c) && !is_kept_glyph_shaper(before, previous, c) { + return true; + } + (before, previous) = (previous, Some(c)); + } + false +} + /// Whether `symptom` can be placed inside a `'...'` shell word verbatim. /// /// The value is untrusted subprocess output (the vLLM startup log tail) and the @@ -212,17 +319,15 @@ pub fn is_control_or_line_separator(c: char) -> bool { /// `rocm serve` summary's OOM note — and a guard that protects only one of them /// is the bug it was written to prevent. /// -/// The character test is [`is_control_or_format`] together with -/// [`is_control_or_line_separator`], because each covers scalars the other does -/// not and `symptom` is the *raw* log line, not the stripped one: a `Cf` bidi -/// override reordered how the printed command renders, and a `Zl` line -/// separator broke it across two rows. +/// The character test is [`is_display_altering`], which takes in both +/// [`is_control_or_format`] and [`is_control_or_line_separator`] (each covers +/// scalars the other does not) and the rest of the default-ignorable set, +/// because `symptom` is the *raw* log line, not the stripped one: a `Cf` bidi +/// override reordered how the printed command renders, a `Zl` line separator +/// broke it across two rows, and a tag character would sit in it unseen. #[must_use] pub fn quotable_in_single_quotes(symptom: &str) -> bool { - !symptom.contains('\'') - && !symptom - .chars() - .any(|c| is_control_or_format(c) || is_control_or_line_separator(c)) + !symptom.contains('\'') && !symptom.chars().any(is_display_altering) } /// Consumes one glyph, control character or escape sequence and says which of @@ -289,43 +394,72 @@ fn next_token(chars: &mut std::iter::Peekable>) -> Option Token::Ignorable, + Escape::Csi { sgr: false } => Token::LineBreak, + // Consuming a string body whole is also the one hole in the + // never-merge property, since a line break inside the body goes with + // it — and the body scan ends on a bare `ESC` or on end of input as + // readily as on a terminator, so a terminator is not what creates the + // hole. The module documentation is where that exception is stated, + // and it is stated there rather than here because a body comment in a + // private function reaches no reader of the public docs. Any change to + // what `skip_escape` consumes for a string sequence has to change that + // section, `rendered_lines`'s own qualifier, and the test that pins the + // worked examples, in one commit. + Escape::String | Escape::Bare => Token::Ignorable, + // This is where `ESC E` (NEL) and `ESC D` (IND) live. + Escape::Other => Token::LineBreak, + }) +} + +/// What [`skip_escape`] consumed after an `ESC`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Escape { + /// A CSI (`ESC [`), and whether it was a well-formed `SGR`. + Csi { sgr: bool }, + /// One of the string-argument sequences: OSC, DCS, SOS, PM, APC. Their + /// bodies are arbitrary text (a window title, say) and must not be emitted + /// as if the process had printed it, but they draw nothing at the cursor. + String, + /// `ESC` with nothing after it, or `ESC ESC`: only this `ESC` goes, and the + /// next one is left to be re-read as a fresh introducer. A bare `ESC` draws + /// nothing and moves nothing on its own. + Bare, + /// Any other escape: optional intermediates, then one final byte. + Other, +} + +/// How far a string-argument sequence's body may run; see +/// [`skip_string_sequence_body`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Body { + Grammar, + ToLineEnd, +} + +/// Consumes the rest of an escape sequence from just after its `ESC` and says +/// which kind it was. The one place the 7-bit introducers are told apart, so +/// [`next_token`] and [`sanitize_terminal_text`] read the same grammar. +fn skip_escape(chars: &mut std::iter::Peekable>, body: Body) -> Escape { match chars.peek() { - // CSI — what a colourised logger emits. Some('[') => { chars.next(); - Some(if skip_csi_body(chars) { - Token::Ignorable - } else { - Token::LineBreak - }) + Escape::Csi { + sgr: skip_csi_body(chars), + } } - // The string-argument sequences: OSC, DCS, SOS, PM, APC. Their bodies - // are arbitrary text (a window title, say) and must not be emitted as if - // the process had printed it, but they draw nothing at the cursor. - // - // Consuming the body whole is also the one hole in the never-merge - // property, since a line break inside the body goes with it — and the - // scan below ends on a bare `ESC` or on end of input as readily as on a - // terminator, so a terminator is not what creates the hole. - // The module documentation is where that exception is stated, and it is - // stated there rather than here because a body comment in a private - // function reaches no reader of the public docs. Any change to what this - // arm consumes has to change that section, `rendered_lines`'s own - // qualifier, and the test that pins the worked examples, in one commit. Some(']' | 'P' | 'X' | '^' | '_') => { chars.next(); - skip_string_sequence_body(chars); - Some(Token::Ignorable) + skip_string_sequence_body(chars, body); + Escape::String } - // `ESC` with nothing after it, or `ESC ESC`: drop just this one and let - // the loop re-read the next as a fresh introducer. A bare `ESC` draws - // nothing and moves nothing on its own. - None | Some('\u{1b}') => Some(Token::Ignorable), - // Any other escape: optional intermediates, then one final byte. This is - // where `ESC E` (NEL) and `ESC D` (IND) live. + None | Some('\u{1b}') => Escape::Bare, Some(_) => { skip_simple_escape_body(chars); - Some(Token::LineBreak) + Escape::Other } } } @@ -374,12 +508,21 @@ fn skip_csi_body(chars: &mut std::iter::Peekable>) -> bool { } /// Consumes a string-argument sequence's body and terminator (`BEL`, or `ST` = -/// `ESC \`) from just after the introducer. A body truncated by anything else — -/// including a bare `ESC` starting the next sequence — ends the scan with that -/// byte left in place. -fn skip_string_sequence_body(chars: &mut std::iter::Peekable>) { +/// `ESC \` or its 8-bit form U+009C) from just after the introducer. A body +/// truncated by anything else — including a bare `ESC` starting the next +/// sequence — ends the scan with that byte left in place. +/// +/// `Body::ToLineEnd` also ends the scan, without consuming it, at a line break +/// (`\n`, `\r`, U+2028, U+2029). [`sanitize_terminal_text`] reads it that way, +/// so an unterminated sequence in one line of untrusted prose cannot swallow +/// the lines after it. [`next_token`] keeps `Body::Grammar`, whose consumption +/// of a break the module documentation states and its tests pin. +fn skip_string_sequence_body(chars: &mut std::iter::Peekable>, body: Body) { while let Some(&c) = chars.peek() { - if c == '\u{7}' { + if body == Body::ToLineEnd && matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}') { + return; + } + if c == '\u{7}' || c == '\u{9c}' { chars.next(); return; } @@ -431,6 +574,78 @@ pub fn strip_terminal_control_sequences(line: &str) -> String { out } +/// Makes multi-line prose from an untrusted source safe to write to a terminal: +/// what is left is exactly what the reader will see. +/// +/// For text that cannot be refused outright — a model's reply, a tool's +/// output, an error message quoting either. Escape sequences are removed whole, +/// introducer and payload alike, in their 7-bit and 8-bit (C1) forms: an SGR +/// "conceal" cannot hide the text after it, an OSC cannot retitle the window or +/// write the clipboard (OSC 52), a DCS or APC payload is never drawn, and +/// nothing can move the cursor. Every other [`is_display_altering`] character +/// is dropped, except: +/// +/// * `\n` and `\t`, which prose needs and which only move to the next line or +/// column; +/// * a lone `\r`, and the line and paragraph separators, which become `\n`: +/// returning to column one would overwrite what was already printed, but the +/// text after it is still worth reading; `\r\n` becomes a plain `\n`; +/// * a single zero-width joiner or non-joiner, or emoji or text presentation +/// selector, directly after a drawn character, and a joiner directly after +/// such a selector ([`is_kept_glyph_shaper`]), which shape that glyph and +/// move nothing. +/// +/// Concealed text therefore becomes *visible*, rather than disappearing with +/// its escape sequence: a reader should see what was sent, not a tidied +/// version of it. +/// +/// A string sequence (OSC, DCS, SOS, PM, APC) with no terminator ends at the +/// next line break, which is kept: an unterminated one costs at most the rest +/// of its own line, never the lines after it. +/// +/// Unlike [`strip_terminal_control_sequences`], which serves single log lines, +/// this keeps the line structure. A command line meant to be pasted is not +/// prose and is not passed through here: its values are refused instead (see +/// [`hides_or_moves_text`]), because cleaning them would make the line shown +/// differ from the one that runs. +#[must_use] +pub fn sanitize_terminal_text(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + let (mut before, mut previous) = (None, None); + let mut chars = text.chars().peekable(); + while let Some(c) = chars.next() { + let shown = match c { + '\n' | '\t' => Some(c), + '\r' => (chars.peek() != Some(&'\n')).then_some('\n'), + '\u{2028}' | '\u{2029}' => Some('\n'), + '\u{1b}' => { + skip_escape(&mut chars, Body::ToLineEnd); + None + } + // The 8-bit CSI, and the 8-bit DCS, SOS, OSC, PM and APC. + '\u{9b}' => { + skip_csi_body(&mut chars); + None + } + '\u{90}' | '\u{98}' | '\u{9d}' | '\u{9e}' | '\u{9f}' => { + skip_string_sequence_body(&mut chars, Body::ToLineEnd); + None + } + _ if is_kept_glyph_shaper(before, previous, c) => Some(c), + _ if is_display_altering(c) => None, + _ => Some(c), + }; + if let Some(shown) = shown { + out.push(shown); + } + // What came directly before in the input, removed or not, as in + // `hides_or_moves_text`: a shaper separated from its base by anything + // invisible shapes nothing. + (before, previous) = (previous, Some(c)); + } + out +} + /// Approximates the lines a terminal would have rendered `text` as, with the /// escape sequences and non-drawing characters removed from each segment. /// @@ -515,6 +730,15 @@ mod tests { quotable_in_single_quotes("vllm: HIP out of memory"), "the rejection must not be so broad that ordinary lines stop qualifying" ); + // Invisible characters that are neither controls nor format characters + // would sit in the printed command unseen, so they make a line + // unquotable too. + for invisible in ['\u{3164}', '\u{e0041}', '\u{fe0f}'] { + assert!( + !quotable_in_single_quotes(&format!("vllm: HIP{invisible} out of memory")), + "{invisible:?} must make a line unquotable" + ); + } } #[test] @@ -772,4 +996,302 @@ mod tests { ["vllm", "x"] ); } + + #[test] + fn concealed_text_is_shown_instead_of_hidden() { + // SGR 8 hides what follows on the terminal while the bytes stay in the + // line; the reader must see them. + let sent = "rocm serve qwen\u{1b}[8m --port 9\u{1b}[28m --managed"; + assert_eq!( + sanitize_terminal_text(sent), + "rocm serve qwen --port 9 --managed" + ); + } + + #[test] + fn escape_sequences_are_removed_whole_in_both_widths() { + for (sent, shown) in [ + // OSC 52 clipboard write, BEL-terminated. + ("a\u{1b}]52;c;aGVsbG8=\u{7}b", "ab"), + // OSC 0 window title, ST-terminated. + ("a\u{1b}]0;title\u{1b}\\b", "ab"), + // 8-bit OSC and 8-bit ST. + ("a\u{9d}0;title\u{9c}b", "ab"), + // 7-bit OSC ended by the 8-bit ST. + ("a\u{1b}]0;title\u{9c}b", "ab"), + // DCS and APC, 7-bit and 8-bit. + ("a\u{1b}Pq#0;2\u{1b}\\b", "ab"), + ("a\u{1b}_body\u{1b}\\b", "ab"), + ("a\u{90}q#0\u{9c}b", "ab"), + ("a\u{9f}body\u{9c}b", "ab"), + // Cursor up, erase line, colours. + ("a\u{1b}[1A\u{1b}[2K\u{1b}[1;31mb\u{1b}[0m", "ab"), + // 8-bit CSI: the parameters and final byte go with it. + ("a\u{9b}2Kb", "ab"), + // Two-character and nF escapes. + ("a\u{1b}cb\u{1b}(Bc", "abc"), + // An unterminated OSC swallows the rest of its line, as on a terminal. + ("a\u{1b}]0;never closed", "a"), + // A lone ESC at the end. + ("ab\u{1b}", "ab"), + // Every string introducer, 7-bit and 8-bit, takes its payload with + // it: SOS and PM included, which nothing above reaches. + ("a\u{1b}Xsos\u{1b}\\b", "ab"), + ("a\u{1b}^pm\u{7}b", "ab"), + ("a\u{98}sos\u{9c}b", "ab"), + ("a\u{9e}pm\u{9c}b", "ab"), + // A CSI's intermediate bytes (`SP`, `$`) go with it, so its final + // byte is not left behind as text. + ("a\u{1b}[2 qb", "ab"), + ("a\u{1b}[1$}b", "ab"), + ("a\u{9b}1 qb", "ab"), + ] { + assert_eq!(sanitize_terminal_text(sent), shown, "{sent:?}"); + } + } + + /// A string sequence left open must not take the lines after it with it: + /// those are often the CLI's own (a status line, the next tool result), and + /// an open sequence anywhere in a reply would otherwise blank the rest of + /// the output. + #[test] + fn an_unterminated_string_sequence_ends_at_its_line() { + for open in [ + "\u{1b}]0;x", // OSC + "\u{1b}Pq", // DCS + "\u{1b}_x", // APC + "\u{1b}Xx", // SOS + "\u{1b}^x", // PM + "\u{9d}0;x", // 8-bit OSC + "\u{90}q", // 8-bit DCS + "\u{98}x", // 8-bit SOS + "\u{9e}x", // 8-bit PM + "\u{9f}x", // 8-bit APC + ] { + for (newline, shown) in [ + ("\n", "\n"), + ("\r\n", "\n"), + ("\r", "\n"), + ("\u{2028}", "\n"), + ("\u{2029}", "\n"), + ] { + let sent = format!("reply{open}{newline}CLI line one\nCLI line two"); + assert_eq!( + sanitize_terminal_text(&sent), + format!("reply{shown}CLI line one\nCLI line two"), + "{sent:?}" + ); + } + } + } + + #[test] + fn prose_keeps_its_layout_and_its_script() { + let text = + "line one\n\tindented 模型 é 👩\u{200d}💻 \u{2764}\u{fe0f} a\u{200c}b \u{2764}\u{fe0e}"; + assert_eq!(sanitize_terminal_text(text), text); + assert_eq!(sanitize_terminal_text("a\r\nb"), "a\nb"); + assert_eq!( + sanitize_terminal_text("shown\rhidden"), + "shown\nhidden", + "a carriage return must not overwrite what is already on the line" + ); + assert_eq!(sanitize_terminal_text("a\u{2028}b"), "a\nb"); + assert_eq!(sanitize_terminal_text("a\u{202e}b\u{8}c\u{7}"), "abc"); + assert_eq!( + sanitize_terminal_text("run\u{e0020}\u{e0069}\u{e0064} it\u{3164}"), + "run it", + "tag characters spell text the reader cannot see; it must not pass" + ); + } + + /// The characters a value is refused for, beyond `Cc`: every one is + /// invisible or reorders the line, and none is `char::is_control`, which is + /// why a check written as `is_control` alone let them through. + #[test] + fn invisible_characters_are_display_altering() { + for c in [ + '\u{202e}', + '\u{202a}', + '\u{2066}', + '\u{2069}', // bidi + '\u{200e}', + '\u{061c}', // directional marks + '\u{200b}', + '\u{200d}', + '\u{2060}', + '\u{feff}', // zero-width + '\u{2028}', + '\u{2029}', // separators + '\u{00ad}', + '\u{034f}', + '\u{fe0f}', // soft hyphen, CGJ, VS16 + '\u{115f}', + '\u{3164}', + '\u{ffa0}', // Hangul fillers + '\u{e0001}', + '\u{e0041}', + '\u{e007f}', // tags + ] { + assert!(!c.is_control(), "U+{:04X}", c as u32); + assert!(is_display_altering(c), "U+{:04X}", c as u32); + } + for c in ['a', 'é', '模', '\'', '&', ' ', '👩'] { + assert!(!is_display_altering(c), "{c:?}"); + } + } + + /// Both ends of every range of the default-ignorable table, and a point + /// just outside each, so narrowing or widening a range is seen. + #[test] + fn the_default_ignorable_table_has_exact_edges() { + let ranges: [(u32, u32); 17] = [ + (0x00ad, 0x00ad), + (0x034f, 0x034f), + (0x061c, 0x061c), + (0x115f, 0x1160), + (0x17b4, 0x17b5), + (0x180b, 0x180f), + (0x200b, 0x200f), + (0x202a, 0x202e), + (0x2060, 0x206f), + (0x3164, 0x3164), + (0xfe00, 0xfe0f), + (0xfeff, 0xfeff), + (0xffa0, 0xffa0), + (0xfff0, 0xfff8), + (0x1bca0, 0x1bca3), + (0x1d173, 0x1d17a), + (0xe0000, 0xe0fff), + ]; + for (first, last) in ranges { + for inside in [first, last] { + let c = char::from_u32(inside).expect("scalar"); + assert!(is_default_ignorable(c), "U+{inside:04X} is in the set"); + } + for outside in [first - 1, last + 1] { + let in_another = ranges.iter().any(|&(a, b)| (a..=b).contains(&outside)); + if let Some(c) = char::from_u32(outside) + && !in_another + { + assert!( + !is_default_ignorable(c), + "U+{outside:04X} is not in the set" + ); + } + } + } + } + + /// The glyph-shaping characters stay only one at a time, directly after a + /// character that is drawn; anything else, and the variation selectors + /// beyond the presentation pair, is removed, so they cannot carry text. + #[test] + fn glyph_shapers_stay_only_singly_after_a_drawn_character() { + for (sent, shown) in [ + ("a\u{200c}b", "a\u{200c}b"), + ("a\u{200d}b", "a\u{200d}b"), + ("\u{2764}\u{fe0f}", "\u{2764}\u{fe0f}"), + ("\u{2764}\u{fe0e}", "\u{2764}\u{fe0e}"), + // A run keeps its first member only. + ("a\u{fe0f}\u{fe0f}\u{fe0f}b", "a\u{fe0f}b"), + ("a\u{200d}\u{200c}b", "a\u{200d}b"), + // At the start, or after a break or a tab, there is nothing to shape. + ("\u{200d}a", "a"), + ("a\n\u{fe0f}b", "a\nb"), + ("a\t\u{200d}b", "a\tb"), + // Anything invisible between the base and the shaper, an escape + // sequence included, leaves it shaping nothing. + ("a\u{1b}[0m\u{fe0f}", "a"), + ("a\u{200b}\u{fe0f}b", "ab"), + // The other variation selectors are never kept. + ("a\u{e0100}\u{e0101}\u{fe0f}\u{200d}b", "ab"), + ("a\u{fe00}b", "ab"), + ("\u{1820}\u{180b}\u{180c}\u{180d}\u{180f}", "\u{1820}"), + ] { + assert_eq!(sanitize_terminal_text(sent), shown, "{sent:?}"); + } + } + + /// A value is refused for exactly what printing would not show, plus the + /// newline and tab a one-line value must not carry. Each accepted row is + /// also printed unchanged as prose, so the rows pin `sanitize_terminal_text` + /// keeping those shapers as much as `hides_or_moves_text` allowing them. + #[test] + fn a_value_hides_text_exactly_where_printing_would_change_it() { + for value in [ + "C:\\Users\\me\\\u{2764}\u{fe0f}\\rocm", + "👩\u{200d}💻", + // A presentation selector, then a joiner: how most joined emoji + // with an emoji-form base are spelled. + "🏳\u{fe0f}\u{200d}🌈", + "❤\u{fe0f}\u{200d}🔥", + "/mnt/my folder", + "模型", + ] { + assert!(!hides_or_moves_text(value), "{value:?}"); + assert_eq!(sanitize_terminal_text(value), value); + } + for value in [ + "a\nb", + "a\tb", + "a\rb", + "a\u{1b}[2Kb", + "a\u{9b}b", + "a\u{202e}b", + "a\u{200b}b", + "\u{fe0f}a", + "a\u{fe0f}\u{fe0f}", + // A joiner may follow a selector, but not the other way round, + // and never two joiners in a row. + "a\u{200d}\u{fe0f}", + "a\u{fe0f}\u{200d}\u{200d}", + "a\u{fe0f}\u{fe0f}\u{200d}", + "a\u{e0100}", + "a\u{3164}", + "a\u{2028}b", + ] { + assert!(hides_or_moves_text(value), "{value:?}"); + } + } + + proptest::proptest! { + /// Whatever goes in, what comes out is what the refusal predicate + /// accepts as a value, apart from the line breaks and tabs prose keeps: + /// nothing display-altering survives except the glyph shapers + /// `is_kept_glyph_shaper` keeps. + #[test] + fn nothing_display_altering_survives_sanitizing(text in proptest::prelude::any::()) { + let cleaned = sanitize_terminal_text(&text); + for line in cleaned.split(['\n', '\t']) { + proptest::prop_assert!(!hides_or_moves_text(line), "{:?} -> {:?}", text, cleaned); + } + } + + /// The same over input made of the characters that matter: every + /// escape introducer, 7-bit and 8-bit, their terminators, line breaks, + /// glyph shapers and ordinary text. The text after an introducer's + /// line is never lost: every `Z` sent after the last line break in the + /// input comes back. + #[test] + fn nothing_display_altering_survives_hostile_sequences( + text in "[\u{1b}\u{9b}\u{9c}\u{9d}\u{90}\u{98}\u{9e}\u{9f}\u{7}\\[\\]\\\\P_^X;0-9a-y \r\n\u{202e}\u{e0041}\u{fe0f}\u{200d}]*" + ) { + let cleaned = sanitize_terminal_text(&text); + for line in cleaned.split(['\n', '\t']) { + proptest::prop_assert!(!hides_or_moves_text(line), "{:?} -> {:?}", text, cleaned); + } + let tail = format!("{text}\nZZZ"); + proptest::prop_assert!( + sanitize_terminal_text(&tail).ends_with("\nZZZ"), + "a line after {:?} was lost", text + ); + } + + /// Text with nothing to remove is returned unchanged. + #[test] + fn plain_text_is_untouched(text in "[ -~\n\té模型]*") { + proptest::prop_assert_eq!(sanitize_terminal_text(&text), text); + } + } } diff --git a/crates/rocm-dash-tui/src/agent/clients.rs b/crates/rocm-dash-tui/src/agent/clients.rs index 33d34c936..0303bfcf3 100644 --- a/crates/rocm-dash-tui/src/agent/clients.rs +++ b/crates/rocm-dash-tui/src/agent/clients.rs @@ -1057,6 +1057,13 @@ mod tests { read_calls: Arc>>, } impl crate::tool_exec::RocmToolExecutor for RecordingExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, name: &str, args: &Value) -> crate::tool_exec::RocmToolOutcome { if name == "watcher_disable" { crate::tool_exec::RocmToolOutcome::ApprovalRequired( diff --git a/crates/rocm-dash-tui/src/agent/tools.rs b/crates/rocm-dash-tui/src/agent/tools.rs index edfe6974d..0bcf2db82 100644 --- a/crates/rocm-dash-tui/src/agent/tools.rs +++ b/crates/rocm-dash-tui/src/agent/tools.rs @@ -431,7 +431,7 @@ fn truncated_tool_result( /// any argv the bin classifies as mutating, since a read tool has no approval /// channel). /// -/// `execute()` is synchronous and can block for seconds or longer: the bin's +/// `execute_model_call()` is synchronous and can block for seconds or longer: the bin's /// read tools run host-probe subprocesses, make HTTP readiness probes of /// running managed services, and can fetch a package index (the SDK install /// preview) or call a configured model endpoint (a provider-assisted plan). So @@ -449,7 +449,7 @@ async fn run_rocm_read_tool( let Some(exec) = exec.cloned() else { return json!({ "error": "ROCm tools are unavailable in this mode (demo/replay/mock)." }); }; - match tokio::task::spawn_blocking(move || exec.execute(name, &args)).await { + match tokio::task::spawn_blocking(move || exec.execute_model_call(name, &args)).await { Ok(RocmToolOutcome::Result(v)) => model_facing_tool_result(v, TOOL_RESULT_CHAR_BUDGET), Ok(RocmToolOutcome::Error(s)) => json!({ "error": s }), Ok(RocmToolOutcome::ApprovalRequired(_)) => { @@ -674,8 +674,9 @@ pub const ROCM_READ_TOOL_NAMES: [&str; 14] = [ // --------------------------------------------------------------------------- // Mutating ROCm tools (group D, Phase 4). These do NOT execute inside the rig -// tool loop. `execute()` returns `ApprovalRequired(intent)` (a descriptor that -// the bin's validators already accepted); the tool posts the intent to the app +// tool loop. `execute_model_call()` returns `ApprovalRequired(intent)` (a +// descriptor that the bin's validators, the model-only ones included, already +// accepted); the tool posts the intent to the app // via `approval_tx` (a `ClientMsg::ChatApprovalRequired`) and returns a // "surfaced for approval" note to the model. The actual action runs only after // the operator approves the modal, via `execute_approved` off the event loop. @@ -711,7 +712,7 @@ macro_rules! rocm_mutating_tool { record(&self.fired, $name); match self.executor.as_ref() { None => Ok(json!({ "error": "ROCm tools unavailable in this mode." })), - Some(e) => match e.execute($name, &args) { + Some(e) => match e.execute_model_call($name, &args) { RocmToolOutcome::ApprovalRequired(intent) => { if let Some(tx) = self.approval_tx.as_ref() { let _ = tx.send(ClientMsg::ChatApprovalRequired { intent }); @@ -1163,6 +1164,13 @@ mod tests { #[derive(Debug)] struct FakeExec(serde_json::Value); impl crate::tool_exec::RocmToolExecutor for FakeExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, _name: &str, _args: &Value) -> RocmToolOutcome { RocmToolOutcome::Result(self.0.clone()) } @@ -1392,6 +1400,13 @@ mod tests { approved: Arc>>, } impl crate::tool_exec::RocmToolExecutor for RecordingMutatingExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, name: &str, args: &Value) -> RocmToolOutcome { RocmToolOutcome::ApprovalRequired(crate::tool_exec::ApprovalIntent { title: "T".to_string(), @@ -1521,6 +1536,13 @@ mod tests { struct NeedsRuntimeProgressExec; const STALL_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); impl crate::tool_exec::RocmToolExecutor for NeedsRuntimeProgressExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, _name: &str, _args: &Value) -> RocmToolOutcome { let (tx, rx) = std::sync::mpsc::channel(); tokio::runtime::Handle::current().spawn(async move { @@ -1568,6 +1590,13 @@ mod tests { #[derive(Debug)] struct PanickingExec; impl crate::tool_exec::RocmToolExecutor for PanickingExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, _name: &str, _args: &Value) -> RocmToolOutcome { panic!("executor bug"); } @@ -1576,6 +1605,72 @@ mod tests { } } + /// Executor that answers differently through its two entry points, so a + /// test can tell which one a tool used. + #[derive(Debug)] + struct EntryPointExec; + impl crate::tool_exec::RocmToolExecutor for EntryPointExec { + fn execute(&self, _name: &str, _args: &Value) -> RocmToolOutcome { + RocmToolOutcome::Result(json!({ "via": "execute" })) + } + fn execute_model_call(&self, _name: &str, _args: &Value) -> RocmToolOutcome { + RocmToolOutcome::Result(json!({ "via": "execute_model_call" })) + } + fn execute_approved(&self, name: &str, args: &Value) -> RocmToolOutcome { + self.execute(name, args) + } + } + + /// The agent's tools are the model's calls, so they go through + /// `execute_model_call`, where the bin applies the rules only a + /// model-chosen value is held to; `execute` is for the operator's own + /// slash commands. + #[tokio::test] + async fn the_agents_tools_reach_the_executor_as_model_calls() { + let read = ExamineRocmTool { + executor: Some(Arc::new(EntryPointExec)), + fired: Arc::new(Mutex::new(Vec::new())), + }; + assert_eq!( + read.call(json!({})).await.expect("tool call ok"), + json!({ "via": "execute_model_call" }) + ); + let mutating = InstallSdkRocmTool { + executor: Some(Arc::new(EntryPointExec)), + approval_tx: None, + fired: Arc::new(Mutex::new(Vec::new())), + }; + assert_eq!( + mutating + .call(json!({ "prefix": "/tmp/x" })) + .await + .expect("tool call ok"), + json!({ "via": "execute_model_call" }) + ); + } + + /// Every tool the agent registers is a model's call, so none of them may + /// reach the executor through `execute`, the operator's entry point: a tool + /// written that way would skip the model-only rules without failing + /// anything else. Read from the source so a new tool is covered as well. + #[test] + fn no_agent_tool_calls_the_operators_entry_point() { + let source = include_str!("tools.rs"); + let production = source + .split("#[cfg(test)]") + .next() + .expect("the file has a production part"); + let offending: Vec<&str> = production + .lines() + .filter(|line| line.contains(".execute(") && !line.trim_start().starts_with("//")) + .collect(); + assert!( + offending.is_empty(), + "agent tools must call `execute_model_call`, not `execute`: {offending:?}" + ); + assert!(production.contains(".execute_model_call(")); + } + /// In-memory `tracing` writer, so a test can read back what was logged. #[derive(Clone, Default)] struct LogCapture(Arc>>); @@ -1648,6 +1743,13 @@ mod tests { #[derive(Debug)] struct FakeErrorExec; impl crate::tool_exec::RocmToolExecutor for FakeErrorExec { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute(&self, _name: &str, _args: &Value) -> RocmToolOutcome { RocmToolOutcome::Error("boom".to_string()) } diff --git a/crates/rocm-dash-tui/src/app/tool_approval.rs b/crates/rocm-dash-tui/src/app/tool_approval.rs index 97596be38..e97a61b66 100644 --- a/crates/rocm-dash-tui/src/app/tool_approval.rs +++ b/crates/rocm-dash-tui/src/app/tool_approval.rs @@ -164,6 +164,13 @@ mod tests { } impl crate::tool_exec::RocmToolExecutor for RecordingExecutor { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute( &self, name: &str, @@ -217,6 +224,13 @@ mod tests { struct CapturedFailureExecutor; impl crate::tool_exec::RocmToolExecutor for CapturedFailureExecutor { + fn execute_model_call( + &self, + name: &str, + args: &serde_json::Value, + ) -> crate::tool_exec::RocmToolOutcome { + self.execute(name, args) + } fn execute( &self, name: &str, diff --git a/crates/rocm-dash-tui/src/tool_exec.rs b/crates/rocm-dash-tui/src/tool_exec.rs index 8ad611e74..2320c42e4 100644 --- a/crates/rocm-dash-tui/src/tool_exec.rs +++ b/crates/rocm-dash-tui/src/tool_exec.rs @@ -7,8 +7,10 @@ //! Plain-data signatures ONLY (serde_json / std / serde). The bin (`apps/rocm`, //! which owns `rocm-core` and the tool engine) implements [`RocmToolExecutor`]; //! the dash holds it as `Option>` and never depends on -//! `rocm-core`. The trait has a read-only `execute` and a mutating -//! `execute_approved` path, plus the approval descriptor. +//! `rocm-core`. The trait has `execute` for the operator's own calls, +//! `execute_model_call` for the agent's (the same, plus the checks only a value +//! a model chose gets), and the mutating `execute_approved` path, plus the +//! approval descriptor. use std::sync::Arc; @@ -48,11 +50,21 @@ pub trait RocmToolExecutor: std::fmt::Debug + Send + Sync { /// (Return value carries `#[must_use]` via the `RocmToolOutcome` enum.) fn execute(&self, name: &str, args: &serde_json::Value) -> RocmToolOutcome; + /// [`Self::execute`] for a call the chat *model* chose, as opposed to one + /// built from what the operator typed (a slash command). The bin holds a + /// model's values to stricter rules than the operator's own — a value the + /// operator typed is theirs to use — so the agent's tools call this and the + /// slash commands call [`Self::execute`]. Required, with no default, so a + /// new executor has to decide what a model's call is held to rather than + /// silently skipping the rules; one with no extra rules delegates to + /// [`Self::execute`]. + fn execute_model_call(&self, name: &str, args: &serde_json::Value) -> RocmToolOutcome; + /// Run an *approved* mutating action via the bin's captured-subprocess path /// (piped stdout/stderr → JSON; no printing to the TUI terminal, so it is /// TUI-safe). Called only after the operator approves the modal, with the /// `(name, arguments)` taken from the [`ApprovalIntent`] that `execute()` - /// returned. It re-validates the call, so the safety validators remain the + /// or `execute_model_call()` returned. It re-validates the call, so the safety validators remain the /// single gate and an unapproved/invalid call can never run here. This is a /// blocking call — invoke it off the async event loop (spawn_blocking). fn execute_approved(&self, name: &str, args: &serde_json::Value) -> RocmToolOutcome; diff --git a/docs/architecture.md b/docs/architecture.md index b929c3165..9c74aa467 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -43,9 +43,9 @@ The `rocm` binary: the clap command tree, a handler module for most commands, an | [`bridge_snapshot.rs`](../apps/rocm/src/bridge_snapshot.rs) | The host snapshot behind the internal bridge surface (`build_codex_bridge_snapshot`): examination, config, automation state, managed services, GPUs and the built-in engine list. | relocation | | [`chat.rs`](../apps/rocm/src/chat.rs) | The chat assistant's entry points (`render_chat_text`, `render_chat_prompt_text`, `read_piped_prompt`), the sampling-flag value parsers, and the re-exports other modules reach `chat/` through. | flat | | [`chat/intent.rs`](../apps/rocm/src/chat/intent.rs) | Prompt-intent heuristics: decides from the user's latest message alone which tool call the assistant proposes without the local model (`fallback_rocm_tool_call_for_prompt`) and when an install needs approval. | relocation | -| [`chat/mcp_bridge.rs`](../apps/rocm/src/chat/mcp_bridge.rs) | The tool dispatch behind `rocm mcp-call`, the dash execution seam and the assistant's read-only tools (`run_internal_mcp_call`, which runs the argument object exactly as given and never unwraps a nested `arguments` field), the `rocm mcp-call` params-envelope unwrap (`mcp_call_cli_arguments`), and the in-process runner for read-only `rocm` commands. | relocation | +| [`chat/mcp_bridge.rs`](../apps/rocm/src/chat/mcp_bridge.rs) | The tool dispatch behind `rocm mcp-call`, the dash execution seam and the assistant's read-only tools (`run_internal_mcp_call`, which runs the argument object exactly as given and never unwraps a nested `arguments` field), the `rocm mcp-call` params-envelope unwrap (`mcp_call_cli_arguments`), and the in-process runner for read-only `rocm` commands. Its `natural_language_plan` result also reports each planned argv (`planned_args`), so a plan for a model's request can be checked as printed. | relocation | | [`chat/summaries.rs`](../apps/rocm/src/chat/summaries.rs) | Deterministic plain-language summaries of read-only tool output (`deterministic_chat_tool_summary`), shown instead of asking the local model to restate it. | domain | -| [`chat/turn.rs`](../apps/rocm/src/chat/turn.rs) | The chat turn loop and transcript (`render_chat_prompt_result_with_progress`): the system prompt, the provider call, running the read-only tools a reply requests, and rendering the reply. | relocation | +| [`chat/turn.rs`](../apps/rocm/src/chat/turn.rs) | The chat turn loop and transcript (`render_chat_prompt_result_with_progress`): the system prompt, the provider call, running the read-only tools a reply requests, and rendering the reply. Every untrusted piece (the reply, the model name, each tool result) is sanitized before it is composed with the CLI's own lines, and the whole rendered turn once more. | relocation | | [`chat_cmd.rs`](../apps/rocm/src/chat_cmd.rs) | `rocm chat` (`run_chat_command`): hands off to the dashboard chat in an interactive terminal with no `--prompt`, otherwise prints the one-shot reply or the status screen. | relocation | | [`chat_host_facts.rs`](../apps/rocm/src/chat_host_facts.rs) | The OS, WSL and GPU facts the binary detects cheaply and renders as a plain-text block for the chat assistant's system prompt. | domain | | [`cli.rs`](../apps/rocm/src/cli.rs) | The `rocm` clap definition: `Cli`, `Command`, the nested subcommand enums and `SUPPORTED_ENGINES`, and a re-export of the value enums behind flags such as `--provider` and `--device`, which [`cli_args.rs`](../apps/rocm/src/cli_args.rs) defines. | flat | @@ -60,7 +60,7 @@ The `rocm` binary: the clap command tree, a handler module for most commands, an | [`comfyui/source_swap.rs`](../apps/rocm/src/comfyui/source_swap.rs) | Replacing ComfyUI's code in an existing `source/` folder on reinstall while keeping the user's models, workflows, outputs and other files there. | domain | | [`config_cmd.rs`](../apps/rocm/src/config_cmd.rs) | `rocm config` (`config`): applies each subcommand to the saved configuration, prompts for a provider API key, and renders `rocm config show`. | relocation | | [`dash.rs`](../apps/rocm/src/dash.rs) | `rocm dash` and the other ways into the dashboard TUI (launcher, chat, bench, setup): builds the telemetry daemon options and starts an embedded daemon when none is listening. | domain | -| [`dash_seam.rs`](../apps/rocm/src/dash_seam.rs) | The binary's implementation of the dashboard's `RocmToolExecutor` seam, reusing the CLI's own command functions with plain data in and out. The seam exists because `rocm-dash-tui` (like `rocm-dash-core`) must not depend on `rocm-core`; `rocm-dash-collectors` and `rocm-dash-daemon` do depend on it. | domain | +| [`dash_seam.rs`](../apps/rocm/src/dash_seam.rs) | The binary's implementation of the dashboard's `RocmToolExecutor` seam, reusing the CLI's own command functions with plain data in and out. Its `execute_model_call`, which the dashboard agent's tools use, puts `tool_policy.rs`'s `validate_model_chosen_tool_call` in front of `execute`, which the operator's slash commands use. The seam exists because `rocm-dash-tui` (like `rocm-dash-core`) must not depend on `rocm-core`; `rocm-dash-collectors` and `rocm-dash-daemon` do depend on it. | domain | | [`deletion_properties.rs`](../apps/rocm/src/deletion_properties.rs) | Unix-only property tests for the recursive deletions behind `rocm uninstall` and `rocm storage remove-downloads`, checked against a real sandbox filesystem. | flat | | [`diagnose_cmd.rs`](../apps/rocm/src/diagnose_cmd.rs) | `rocm diagnose` and `rocm fix` (`diagnose`, `fix`), and the `--report` step that builds a report naming this build's leading version (`prepare_cli_report`), shows it and offers to deliver it. | relocation | | [`diagnose_model.rs`](../apps/rocm/src/diagnose_model.rs) | The binary's side of `rocm diagnose --model`'s GGUF context estimate: the `--kv-cache-type` and model flags, the `--report` note, and the device-visibility-mask caveat. | domain | @@ -76,7 +76,7 @@ The `rocm` binary: the clap command tree, a handler module for most commands, an | [`examine_report_text.rs`](../apps/rocm/src/examine_report_text.rs) | The `rocm examine` text reports, the bare-`rocm` launch summary, and the engine-inventory listing shared by `rocm engines` and `rocm examine`. | relocation | | [`freeform_answer.rs`](../apps/rocm/src/freeform_answer.rs) | Answers a freeform request gets without running a command: a single approval-free `examine`, `comfyui status` or `comfyui logs` action. | relocation | | [`freeform_execution.rs`](../apps/rocm/src/freeform_execution.rs) | Whether a freeform plan's next action may run and the consent flags it needs (`prepare_freeform_execution`), and the header printed before it runs. | relocation | -| [`freeform_planner.rs`](../apps/rocm/src/freeform_planner.rs) | The freeform natural-language planner: turns a prose request into a `StructuredRequestPlan` of `rocm` tool calls, deterministically or with a configured planner provider, and renders it. | domain | +| [`freeform_planner.rs`](../apps/rocm/src/freeform_planner.rs) | The freeform natural-language planner: turns a prose request into a `StructuredRequestPlan` of `rocm` tool calls, deterministically or with a configured planner provider, and renders it. A provider plan's argvs go through `tool_policy.rs`'s `validate_model_chosen_argv`, and the plan reports its argvs beside the text (`planned_args`) so a `natural_language_plan` result can be checked as printed. | domain | | [`gpu_policy.rs`](../apps/rocm/src/gpu_policy.rs) | `--device`/`--gpu` parsing, GPU selection under the managed-launch lock, per-GPU VRAM readings (`amd-smi`, with a DRM sysfs fallback) and the serve low-VRAM warning. | relocation | | [`gpu_selection_proptests.rs`](../apps/rocm/src/gpu_selection_proptests.rs) | Property tests for `--gpu` auto-selection and explicit-index validation, declared from [`gpu_policy.rs`](../apps/rocm/src/gpu_policy.rs). | flat | | [`install_cmd.rs`](../apps/rocm/src/install_cmd.rs) | `rocm install` (`install`) for the `sdk` and `driver` targets, the `--version`/`--build-date` selector, and mapping `install sdk` arguments onto a `therock::SdkInstallRequest`. | relocation | @@ -128,7 +128,7 @@ The `rocm` binary: the clap command tree, a handler module for most commands, an | [`services_cmd.rs`](../apps/rocm/src/services_cmd.rs) | `rocm services` (`services`) for `list`, `logs`, `stop`, `restart`, `remove` and `prune`, and the approval-gated stop and restart path. | relocation | | [`services_text.rs`](../apps/rocm/src/services_text.rs) | Log rendering and the `rocm services` and `rocm logs` status, browser and `--follow` surfaces, managed-service record reads, and the `stop_server` answer shape. | relocation | | [`setup_cmd.rs`](../apps/rocm/src/setup_cmd.rs) | `rocm setup` (`setup`): the `setup status` text and `setup reset`, which clears the saved first-time prompt state. | relocation | -| [`shell_quote.rs`](../apps/rocm/src/shell_quote.rs) | The CLI's one POSIX shell quoter (`shell_quote`, over `rocm-core`'s) and `has_terminal_control_character`, the predicate the chat tool-call guards refuse on. | flat | +| [`shell_quote.rs`](../apps/rocm/src/shell_quote.rs) | The CLI's one POSIX shell quoter (`shell_quote`, over `rocm-core`'s) and `has_terminal_control_character`, the predicate the chat tool-call guards refuse on, and `paste_unsafe_character`, the characters a model-chosen value may not carry onto a line meant to be pasted, because PowerShell or `cmd.exe` reads them as syntax inside POSIX single quotes. | flat | | [`storage.rs`](../apps/rocm/src/storage.rs) | `rocm storage`: a read-only report of what runtimes, downloads and engine installs occupy on disk, and guarded removal of old installs and downloads. | domain | | [`symptom_input.rs`](../apps/rocm/src/symptom_input.rs) | `rocm diagnose --symptom-file`: turns a file, or standard input (`-`), into the symptom string `--symptom` would have delivered (`resolve`): one trailing line break removed, a UTF-8 byte-order mark dropped, UTF-16 with a byte-order mark decoded, a size cap (`MAX_SYMPTOM_BYTES`), and the hint printed when standard input is a terminal (`TERMINAL_PASTE_HINT`). Its `SymptomFileError` becomes a clap usage error (exit code 2) through `usage_error`, which `dispatch()` in [`main.rs`](../apps/rocm/src/main.rs) hands to `clap_exit_code` before calling `diagnose()` in [`diagnose_cmd.rs`](../apps/rocm/src/diagnose_cmd.rs), so the handler still receives a plain `Option`. Reached by qualified path. | domain | | [`system_packages.rs`](../apps/rocm/src/system_packages.rs) | Host system-package installs for SDK and engine installs: the distro-aware OpenMPI, libatomic and libnuma plans, the consent decision, and running them without a shell. | domain | @@ -154,7 +154,7 @@ The `rocm` binary: the clap command tree, a handler module for most commands, an | [`therock/version.rs`](../apps/rocm/src/therock/version.rs) | Version grammar for TheRock runtimes: the strict install gate (`parse_version`), PEP 440 ordering, host-version parsing and the build-date helpers. | domain | | [`therock/wheel_plan.rs`](../apps/rocm/src/therock/wheel_plan.rs) | The `sdk install` plan a wheel install prints, from one writer for a preview and a real install, resolved or not. | domain | | [`therock_version_proptests.rs`](../apps/rocm/src/therock_version_proptests.rs) | Property tests for version ordering, update verdicts and update-source selection, declared from [`therock.rs`](../apps/rocm/src/therock.rs). | flat | -| [`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 at once, which need approval, and the validators that refuse unsafe calls, including any call carrying a nested `arguments` field, so an approval card and the command approval runs cannot read different payloads. | flat | +| [`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 at once, which need approval, and the validators that refuse unsafe calls, including any call carrying a nested `arguments` field, so an approval card and the command approval runs cannot read different payloads, and the checks only a call the model chose gets (`validate_model_chosen_tool_call`, `validate_model_chosen_argv`, `validate_model_chosen_plan_result`): no paste-unsafe or invisible character in a value, and a model-requested `serve` held to its long options, one model and a plain value alphabet. | flat | | [`uninstall.rs`](../apps/rocm/src/uninstall.rs) | `rocm uninstall` (`uninstall`): what only uninstall does beyond removing files: undoing the installer's `PATH` change, removing the fish completion script the Linux installer recorded writing (`plan_completion_removals` takes only a regular `completions/rocm.fish` whose first line is the installer's marker naming this install folder, tied to [`install.sh`](../install.sh) by a test; `execute_plan` rechecks the file before removing it and reports a removal failure as a warning instead of stopping, for the `SHELL_COMPLETIONS_KIND` plan entries it adds), and deleting the running `rocm.exe` on Windows. | relocation | | [`uninstall_plan.rs`](../apps/rocm/src/uninstall_plan.rs) | The uninstall plan shared with `rocm storage`: building it, rendering it for its audience, the `[y/N]` confirmation, and the `remove_path` primitive. | relocation | | [`update_cmd.rs`](../apps/rocm/src/update_cmd.rs) | `rocm update` (`update`): the read-only status report, whether to route into preview or apply, applying a runtime update, and the update-source pickers. | relocation | @@ -354,7 +354,7 @@ The library every first-party app and engine crate depends on. It holds host exa | [`service_record.rs`](../crates/rocm-core/src/service_record.rs) | `ManagedServiceRecord`, the on-disk manifest of a managed inference service, and the engine state-file keys that tie it to readiness probing. | domain | | [`sigpipe.rs`](../crates/rocm-core/src/sigpipe.rs) | `with_sigpipe_ignored`, which turns a write to a pipe whose reader has gone into an ordinary error instead of a signal; `rocm` runs with SIGPIPE at its default action, so the engine stdio client wraps its write to the child, and each write forwarding the child's stderr to its own, in it. Guarded regions may overlap, across threads too; the disposition in force before the first is put back when the last ends. | domain | | [`tensorizer_override.rs`](../crates/rocm-core/src/tensorizer_override.rs) | The tensorizer release the vLLM install puts over vLLM's own exact pin so vLLM imports on Python 3.14, and the rule that tells that deliberate `uv pip check` finding from a defect, shared by the engine and the CLI. | domain | -| [`terminal.rs`](../crates/rocm-core/src/terminal.rs) | One ECMA-48 walk over untrusted terminal output, and the stripping, line-splitting and quotability decisions built on it. | flat | +| [`terminal.rs`](../crates/rocm-core/src/terminal.rs) | One ECMA-48 walk over untrusted terminal output, and the stripping, line-splitting and quotability decisions built on it, including `sanitize_terminal_text`, which `rocm chat` prints model text and error reports through. | flat | | [`test_env.rs`](../crates/rocm-core/src/test_env.rs) | `RestoredEnvVar`: restores a process environment variable on drop, so a panicking test cannot leak it. | flat | | [`test_support.rs`](../crates/rocm-core/src/test_support.rs) | Test fixtures for throwaway per-test `AppPaths` directories and for seeding the automation audit store. | flat | | [`user_path.rs`](../crates/rocm-core/src/user_path.rs) | Removes the install directory from the per-user Windows `Path` that [`install.ps1`](../install.ps1) added it to. | flat | @@ -488,7 +488,7 @@ The dashboard client: the event loop and reducer (`app/`), the chat agent (`agen | [`reconnect.rs`](../crates/rocm-dash-tui/src/reconnect.rs) | `Backoff`, the exponential delay the client sleeps between connection attempts. | flat | | [`registry_probe.rs`](../crates/rocm-dash-tui/src/registry_probe.rs) | `ManagedRegistryProbe`, the seam through which the dashboard reads the managed-service registry without depending on `rocm-core` or the daemon. | flat | | [`replay.rs`](../crates/rocm-dash-tui/src/replay.rs) | The offline source behind `--replay`: posts a session file's events through the live client's channel, with pause, speed and seek through `ReplayController`. | flat | -| [`tool_exec.rs`](../crates/rocm-dash-tui/src/tool_exec.rs) | `RocmToolExecutor`, the plain-data seam for chat tool calls that need `rocm-core`: a read-only `execute` and an approval-gated `execute_approved`. | flat | +| [`tool_exec.rs`](../crates/rocm-dash-tui/src/tool_exec.rs) | `RocmToolExecutor`, the plain-data seam for chat tool calls that need `rocm-core`: `execute` for the operator's own calls, `execute_model_call` for the agent's (which adds the model-only checks), and an approval-gated `execute_approved`. | flat | | [`transport.rs`](../crates/rocm-dash-tui/src/transport.rs) | NDJSON framing for the client: `write_line` and `read_line`. | flat | #### `agent/` @@ -850,7 +850,7 @@ Gherkin `.feature` files under `features/` backed by Rust step functions that dr | [`tests/e2e/automations_steps.rs`](../tests/e2e-cucumber/tests/e2e/automations_steps.rs) | Steps for [`automations.feature`](../tests/e2e-cucumber/features/automations.feature): `rocm automations` enable, disable and listing, with the daemon planted as already running so none is spawned. | flat | | [`tests/e2e/bench_steps.rs`](../tests/e2e-cucumber/tests/e2e/bench_steps.rs) | Steps for [`bench.feature`](../tests/e2e-cucumber/features/bench.feature): the chat route `rocm bench load` posts to, and an all-failed run reported as a failure. | flat | | [`tests/e2e/bootstrap_steps.rs`](../tests/e2e-cucumber/tests/e2e/bootstrap_steps.rs) | Steps for [`bootstrap.feature`](../tests/e2e-cucumber/features/bootstrap.feature): the non-interactive fallback of `rocm bootstrap setup`, run piped. | flat | -| [`tests/e2e/chat_steps.rs`](../tests/e2e-cucumber/tests/e2e/chat_steps.rs) | Steps for [`chat.feature`](../tests/e2e-cucumber/features/chat.feature) and [`networking.feature`](../tests/e2e-cucumber/features/networking.feature): a served mock model, service listing, and chat requests through the API and the CLI. | flat | +| [`tests/e2e/chat_steps.rs`](../tests/e2e-cucumber/tests/e2e/chat_steps.rs) | Steps for [`chat.feature`](../tests/e2e-cucumber/features/chat.feature) and [`networking.feature`](../tests/e2e-cucumber/features/networking.feature): a served mock model, service listing, and chat requests through the API and the CLI, including a model whose reply or tool calls carry text the CLI must clean or refuse. | flat | | [`tests/e2e/comfyui_steps.rs`](../tests/e2e-cucumber/tests/e2e/comfyui_steps.rs) | Steps for [`comfyui.feature`](../tests/e2e-cucumber/features/comfyui.feature): `rocm comfyui install` against a planted runtime with the `fake-python` and `fake-uv` stand-ins, and runtime selection. | flat | | [`tests/e2e/config_steps.rs`](../tests/e2e-cucumber/tests/e2e/config_steps.rs) | Steps for [`config.feature`](../tests/e2e-cucumber/features/config.feature): `rocm config` mutations against the scenario's isolated config dir. | flat | | [`tests/e2e/dash_steps.rs`](../tests/e2e-cucumber/tests/e2e/dash_steps.rs) | Steps for [`dash.feature`](../tests/e2e-cucumber/features/dash.feature) and the TUI scenarios of other features: the dashboard and chat TUI driven through a pseudo-terminal. | flat | @@ -876,7 +876,7 @@ Gherkin `.feature` files under `features/` backed by Rust step functions that dr | [`tests/e2e/skill_steps.rs`](../tests/e2e-cucumber/tests/e2e/skill_steps.rs) | Steps for [`rocm_doctor_skill.feature`](../tests/e2e-cucumber/features/rocm_doctor_skill.feature): drive `rocm` through the sequence the rocm-doctor skill prescribes and check its literal claims. | flat | | [`tests/e2e/storage_steps.rs`](../tests/e2e-cucumber/tests/e2e/storage_steps.rs) | Steps for [`storage.feature`](../tests/e2e-cucumber/features/storage.feature) and [`local_server_records.feature`](../tests/e2e-cucumber/features/local_server_records.feature): `rocm storage report` and the confirmation gate on `remove-downloads`. | flat | | [`tests/e2e/symptom_file_steps.rs`](../tests/e2e-cucumber/tests/e2e/symptom_file_steps.rs) | Steps for the `rocm diagnose --symptom-file` scenarios of [`diagnose.feature`](../tests/e2e-cucumber/features/diagnose.feature) (diagnose-59 to diagnose-61): a hostile paste piped through each available shell, a paste typed at a pseudo-terminal through [`tui_driver.rs`](../tests/e2e-cucumber/tests/e2e/tui_driver.rs), and error files that cannot be read or are not text. | flat | -| [`tests/e2e/therock_steps.rs`](../tests/e2e-cucumber/tests/e2e/therock_steps.rs) | Steps for [`therock_next_generation.feature`](../tests/e2e-cucumber/features/therock_next_generation.feature) and [`download_progress_pty.feature`](../tests/e2e-cucumber/features/download_progress_pty.feature): release layouts served from a loopback server, and one real GPU install. | flat | +| [`tests/e2e/therock_steps.rs`](../tests/e2e-cucumber/tests/e2e/therock_steps.rs) | Steps for [`therock_next_generation.feature`](../tests/e2e-cucumber/features/therock_next_generation.feature) and [`download_progress_pty.feature`](../tests/e2e-cucumber/features/download_progress_pty.feature): release layouts served from a loopback server, and one real GPU install. Also the install folder chat-21's assistant asks for (`ampersand_folder`) and the user's own preview into it, so the two scenarios name the same folder. | flat | | [`tests/e2e/tui_driver.rs`](../tests/e2e-cucumber/tests/e2e/tui_driver.rs) | Pseudo-terminal driver for the dash and chat TUI: spawns the real binary under `portable-pty`, sends keys and reads the emulated `vt100` screen. | flat | | [`tests/e2e/uninstall_steps.rs`](../tests/e2e-cucumber/tests/e2e/uninstall_steps.rs) | Steps for [`uninstall.feature`](../tests/e2e-cucumber/features/uninstall.feature): `rocm uninstall` confined to the scenario root with `--keep-binaries` and redirected caches. | flat | | [`tests/e2e/update_steps.rs`](../tests/e2e-cucumber/tests/e2e/update_steps.rs) | Steps for [`update.feature`](../tests/e2e-cucumber/features/update.feature): the report-only `rocm update`, its feed status, and a loopback catalog where runtimes are planted. | flat | diff --git a/docs/llm-tool-use.md b/docs/llm-tool-use.md index 7a6cd01b2..48fc58977 100644 --- a/docs/llm-tool-use.md +++ b/docs/llm-tool-use.md @@ -24,14 +24,74 @@ rocm-cli local assistants use structured tools, not shell commands. result shows it is useful. - The model must use argv-style `rocm` arguments. It must not invent PowerShell, Bash, `cmd`, `git`, or arbitrary package-manager commands. -- A tool call is refused if its name or any string in its arguments carries a - terminal control or format character (an escape sequence, a carriage return, - a bidi override, a zero-width character). Those values are printed — in the - approval card, the request plan, tool output and error messages — and a - terminal acts on such a character instead of showing it. This applies to - every tool; `rocm_command` checks each `args` entry itself, after trimming it. - The provider-assisted planner's argv is held to the same rule, and a planner - note carrying such a character is left out and counted. +- A tool call is refused if its name or any string in its arguments carries + something a terminal would not show as it is: a control (an escape + sequence, a carriage return, a newline, a tab), a format character (a bidi + override, a zero-width space), a line separator, or any other character + Unicode marks default-ignorable, such as the invisible tag characters. This + applies to every tool, whoever built the call; `rocm_command` checks each + `args` entry itself, after trimming it. A provider-assisted planner note + carrying such a character is left out and counted. +- The one exception to that rule is a single zero-width joiner or non-joiner, + or emoji or text presentation selector (VS15, VS16), directly after a + character that is drawn, which is how an emoji such as `❤️` is spelled, and + a joiner directly after such a selector, as in `🏳️‍🌈`; a run of them, or + one anywhere else, is refused. +- The exception does not hold for a value the model chose on a command line + the CLI shows (the next rule says which): there any of these characters is + refused, a single joiner included, since after a plain character it shapes + nothing and makes two values look alike. Model-chosen values that are not + on such a line get only the first rule, with its exception: a + `path_exists` path, a `port_status` host, a `natural_language_plan` + request, and a `proposal_action` proposal id, although its approval card + shows it as `proposal `. +- The command lines shown for a model's request (the approval card, the + `advanced manual command:` line, the install preview's `command:` line, the + request plan's `tool_call:` and `next_tool_call:` lines) quote every + argument for a POSIX shell, so each reads back as one word in `sh`, bash or + WSL. Windows shells read such a line differently, so a value the model + chose is refused if it carries a character they treat as syntax even inside + single quotes. The set, stated here once: a single quote, straight (`'`) or + typographic (U+2018 to U+201B), which PowerShell reads as the end of the + string, and `& | < > ^ % " !`, which `cmd.exe` acts on because it gives + single quotes no meaning. The check reads the argv the call is shown as, + not the JSON it arrived in, so a field that is never printed (a + `rocm_command` `reason`) is not refused. It covers the model's tool calls in + `rocm chat` and in the dashboard, the provider-assisted planner, and the + plan `natural_language_plan` returns for a request the model wrote: the + planner turns words of that request into argv values, so the plan's + `tool_call:` lines are checked before the plan is shown (the planner's own + placeholder `` passes as the model of a `rocm serve`, where the + planner writes it, and nowhere else). The + refusal names the character and tells the user to supply the value + themselves by running the `rocm` command it belongs to, and says that + nothing was changed and no command was offered to paste for that call; a + read-only check the same reply asked for may already have run, and in + `rocm chat --prompt` nothing from the turn reaches standard output. Values the + CLI builds from what the user typed are never held to this: an install + folder in a request the CLI recognises itself (`install therock into + `), or in a dashboard slash command, is rendered as typed. A folder + the user named in other words reaches the CLI through the model's call, and + is held to this like any value the model chose. `cmd.exe` still splits a + quoted value at a space, so such a line does not paste there as one + argument, but nothing in a model-chosen value can run as a command of its + own. +- A `rocm serve` the model requests — through `launch_server`, + `rocm_command`, the provider-assisted planner or a planned request — may + only use long options `rocm serve` has, must name exactly one model, and may + only name the model and each option value with letters, digits and + `. _ - / : @ + \`, at most 256 of them and not starting with `-`; anything + else, such as a model file whose path contains a space, is for the user to + run with `rocm serve` directly. +- Prose that cannot be refused — the model's reply, the model name the server + reports, tool results, an error message quoting either — is printed by + `rocm chat --prompt` with terminal escape sequences (CSI, OSC including + clipboard writes, DCS, SOS, PM, APC, and their 8-bit forms) and invisible + characters removed and line breaks kept, so text a reply tries to conceal is + shown in plain sight instead. A sequence left unterminated ends at its own + line, so it cannot hide the lines after it. The dashboard chat draws + through its terminal UI library, which drops control characters but not the + other invisible ones. - CPU fallback is not a supported path. GPU-required ROCm commands must fail loudly when the ROCm GPU path is not ready. Keeping some of a model's layers in system RAM on a GPU that is ready (llama.cpp's `--n-gpu-layers`), which diff --git a/docs/manual-testing.md b/docs/manual-testing.md index d425d7cd0..1a889c51c 100644 --- a/docs/manual-testing.md +++ b/docs/manual-testing.md @@ -1653,6 +1653,16 @@ Expected result: - The plan still asks for review before mutating actions. - `rocm --yes "serve a model"` refuses to auto-run a provider-assisted plan; run the displayed structured command directly after reviewing it. +- A plan whose command carries a value a Windows shell reads as syntax inside + single quotes, such as `'`, `&` or `%` (the full set is in + [llm-tool-use.md](llm-tool-use.md)), is not shown: + the CLI falls back to its own plan and its note says to supply that value + yourself. The same holds for `rocm chat --tools --prompt …`, where the + assistant's request is refused with that message on stderr and no + `advanced manual command:` line is printed. A folder in a request the CLI + recognises itself (`install therock into D:\R&D\rocm`) is shown as typed; + in other wording the folder reaches the CLI through the assistant's request + and is refused like any value the assistant chose. To turn provider-assisted planning back off: diff --git a/docs/testing.md b/docs/testing.md index f2e113962..91174269e 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -2367,6 +2367,80 @@ cargo test -p rocm --bin rocm freeform_execution_reuses_the_rendered_plan_withou cargo test -p e2e-cucumber --lib chat_script_tests ``` +What the chat prints from a model, and what it offers to paste, is covered by +three more mock-lane scenarios in the same feature file (steps in +[chat_steps.rs](../tests/e2e-cucumber/tests/e2e/chat_steps.rs)), and the +error report every command prints by one in +[runtime_lifecycle.feature](../tests/e2e-cucumber/features/runtime_lifecycle.feature) +(steps in +[runtime_lifecycle_steps.rs](../tests/e2e-cucumber/tests/e2e/runtime_lifecycle_steps.rs)): + +- `chat-20` (`chat-cli-reply-escapes-stripped`): a reply that conceals part of + a line with SGR 8, retitles the window and writes the clipboard (OSC 52, and + an 8-bit OSC) reaches stdout of `rocm chat --prompt` with no control + character and no sequence payload, and the concealed text printed where it + was. A title sequence the reply leaves open removes the rest of its own line + only: the line after it (`Still here.`) is printed. +- `chat-21` (`chat-assistant-value-unsafe-to-paste-refused`): the assistant + asks `install_sdk` for a folder whose name holds `&`, which `cmd.exe` acts + on even inside the single quotes the line is rendered in. The CLI exits + non-zero, asked the assistant once, and prints nothing to stdout; stderr + holds the refusal, which names the character and says to run + `rocm install sdk` with the value, and no `advanced manual command:` or + `tool_call:` line. The user's own `rocm install sdk --dry-run` into that + folder (therock-next-15's pip index fixture and installed `uv`) is + accepted and names it as + one argument. That nothing was installed is checked by the chat-turn unit + tests in [providers.rs](../apps/rocm/src/providers.rs) listed below, not by + the scenario. +- `chat-22` (`chat-assistant-plan-unsafe-to-paste-refused`): the assistant + asks `natural_language_plan` to plan an install into the same kind of + folder. The plan's `tool_call:` line would carry the folder, so the CLI + refuses the plan the same way, with the refusal naming the plan for the + assistant's request, and prints nothing to stdout. +- `runtime-lifecycle-32` (`runtime-lifecycle-error-report-carries-no-escape`): + the error report every `rocm` command prints goes through the same cleaning. + `rocm runtimes activate` with a runtime name carrying a window-title and an + erase-line sequence fails, its report names the runtime with the sequences + removed and holds no control character, and no runtime is active. + +```bash +cargo xtask e2e -- -n "chat-20" +cargo xtask e2e -- -n "chat-21" +cargo xtask e2e -- -n "chat-22" +cargo xtask e2e -- -n "runtime-lifecycle-32" +``` + +The unit tests behind them: `sanitize_terminal_text`, `hides_or_moves_text` +and `is_display_altering` in [terminal.rs](../crates/rocm-core/src/terminal.rs) +(exact output for every introducer, an unterminated sequence ending at its +line, the glyph-shaper rule and the default-ignorable table's edges), the +paste-unsafe set in [shell_quote.rs](../apps/rocm/src/shell_quote.rs), the +model-only refusals, the serve option and character rules and the plan check +in [tool_policy.rs](../apps/rocm/src/tool_policy.rs) (including that the same +value passes `validate_chat_tool_call`, and that every tool that shows a +command line is checked on it), the model-versus-user split in [turn.rs](../apps/rocm/src/chat/turn.rs) +and [dash_seam.rs](../apps/rocm/src/dash_seam.rs), the chat turn against a +local model in [providers.rs](../apps/rocm/src/providers.rs) (a refused +folder, a refusal after a read-only check, a refused plan), the planner's +check in [freeform_planner.rs](../apps/rocm/src/freeform_planner.rs), the +status screen in [chat.rs](../apps/rocm/src/chat.rs), the error report as +written to stderr in [main.rs](../apps/rocm/src/main.rs), and the dashboard +agent's entry point in +[tools.rs](../crates/rocm-dash-tui/src/agent/tools.rs): + +```bash +cargo test -p rocm-core terminal:: +cargo test -p rocm --bin rocm shell_quote:: +cargo test -p rocm --bin rocm tool_policy:: +cargo test -p rocm --bin rocm chat:: +cargo test -p rocm --bin rocm dash_seam:: +cargo test -p rocm --bin rocm freeform_planner::tests::provider_planner_refuses +cargo test -p rocm --bin rocm providers::tests::a_ +cargo test -p rocm --bin rocm tests::an_error +cargo test -p rocm-dash-tui --lib agent::tools +``` + Log browser smoke: ```bash diff --git a/skills/rocm-cli-assistant/SKILL.md b/skills/rocm-cli-assistant/SKILL.md index 9fe934054..f1b921a1d 100644 --- a/skills/rocm-cli-assistant/SKILL.md +++ b/skills/rocm-cli-assistant/SKILL.md @@ -25,6 +25,7 @@ Use this skill when answering ROCm CLI local assistant questions. - `active_runtime_status=ready` means ROCm CLI has an active managed ROCm/TheRock runtime even if legacy system ROCm is not detected. - If the user asks to install ROCm/TheRock, require an explicit install folder or use the guided folder picker. Preserve the exact path with `--prefix`. +- ROCm CLI refuses a value you choose for a tool that shows the user a command (install, engine install, serve, service stop, automations, `rocm_command`, a plan you request) if it holds a character a Windows shell reads as syntax, such as `'`, `&` or `%`; `path_exists` is not affected. A model or option value you give `serve` may use only letters, digits and `. _ - / : @ + \`, must not start with `-`, and only options `rocm serve` has. If the user's folder or model needs anything else, do not rewrite it; tell the user to run the `rocm` command themselves with that value. - After a non-TUI SDK install succeeds, tell the user it installed successfully and to run `rocm help`. ## Engines And Assistant diff --git a/tests/e2e-cucumber/features/chat.feature b/tests/e2e-cucumber/features/chat.feature index 16a360d0b..75f5f4aee 100644 --- a/tests/e2e-cucumber/features/chat.feature +++ b/tests/e2e-cucumber/features/chat.feature @@ -221,3 +221,48 @@ Feature: Chat and endpoint detection When a tool-using client asks the CLI to check a path with a second set of arguments nested inside the first Then the CLI refuses the request for its nested arguments And no tool result is printed + + # A reply is model output, and model output is not trusted: it may carry + # terminal escape sequences that conceal part of a line, retitle the window + # or write the clipboard. `rocm chat --prompt` prints the reply to a terminal, + # so what reaches it must be what the user sees: the concealed part shown in + # plain text, the sequences and their payloads gone, and a sequence left + # open costing only the rest of its own line. Runs on mock (no GPU). + @id:chat-cli-reply-escapes-stripped + Scenario: chat-20 - The chat CLI shows a reply's hidden text instead of passing on its escape sequences + Given a local chat assistant whose reply hides part of itself with terminal escape sequences + When the user sends a one-shot chat prompt through the CLI + Then the CLI output carries no terminal escape sequence + And the hidden part of the reply is shown in plain text + + # The command lines the chat prints for the user to approve or paste are + # POSIX-quoted, which Windows shells do not read the same way: `cmd.exe` + # gives `&` its meaning even inside single quotes. So a value the assistant + # chose may not carry one, and the user is told to supply it themselves; the + # same folder from the user is theirs to use, and the install preview they + # run keeps it as one argument. The pip index fixture and the installed `uv` + # are therock-next-15's (an unpinned preview reads the ROCm 10 index, which + # needs a `uv` to resolve), so the preview resolves without network or GPU. + # Runs on mock (no GPU). + @id:chat-assistant-value-unsafe-to-paste-refused + Scenario: chat-21 - A folder the chat assistant chose that a Windows shell would split is refused, not offered to paste + Given a canonical release pip index fixture and a ROCm 10 pip index fixture + And a uv is already installed + And a local chat assistant that asks to install ROCm into a folder whose name holds an ampersand + When the user asks the chat assistant with ROCm tools a question in one-shot chat + Then the CLI refuses the assistant's folder and tells the user to supply it themselves + And no command line holding that folder is offered to paste + When the user previews that install into the same folder themselves + Then the dry-run command line names the folder with an ampersand as a single argument + + # The plan `natural_language_plan` returns for a request the assistant wrote + # turns words of that request into the values on its `tool_call:` lines, + # which are offered to paste, so the plan is held to the rule chat-21's call + # is: a folder with an ampersand in it is refused before the plan is shown. + # Planning runs no install, so no pip index is needed. Runs on mock (no GPU). + @id:chat-assistant-plan-unsafe-to-paste-refused + Scenario: chat-22 - A plan for the chat assistant's request that names a folder a Windows shell would split is refused, not shown + Given a local chat assistant that asks for a plan to install ROCm into a folder whose name holds an ampersand + When the user asks the chat assistant with ROCm tools a question in one-shot chat + Then the CLI refuses the plan for the assistant's request + And no command line holding that folder is offered to paste diff --git a/tests/e2e-cucumber/features/runtime_lifecycle.feature b/tests/e2e-cucumber/features/runtime_lifecycle.feature index 19bfe62a5..16c829ed0 100644 --- a/tests/e2e-cucumber/features/runtime_lifecycle.feature +++ b/tests/e2e-cucumber/features/runtime_lifecycle.feature @@ -455,3 +455,16 @@ Feature: Runtime lifecycle state machine And the JSON gives no value for each fact the older runtime did not record instead of guessing And the JSON gives no package source for the adopted runtime And the text listing names the same provenance as the JSON for every runtime + + # Every `rocm` error report goes through the same cleaning as a chat reply, + # because an error can quote text a user, a model or a remote service + # supplied. Here the runtime the user names carries a window-title sequence + # and an erase-line sequence: the report still names that runtime, with the + # sequences removed, and nothing is activated. Mock lane: no SDK or GPU. + @id:runtime-lifecycle-error-report-carries-no-escape + Scenario: runtime-lifecycle-32 - An error naming a runtime prints the name without its terminal escape sequences + Given two registered runtimes and none active + When the user tries to activate a runtime whose name carries terminal escape sequences + Then the activation fails naming that runtime without its escape sequences + And the error report carries no terminal control character + And no runtime is active diff --git a/tests/e2e-cucumber/tests/e2e/chat_steps.rs b/tests/e2e-cucumber/tests/e2e/chat_steps.rs index dbfeda1d0..fdd0c0fa6 100644 --- a/tests/e2e-cucumber/tests/e2e/chat_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/chat_steps.rs @@ -3,7 +3,9 @@ // SPDX-License-Identifier: MIT use cucumber::{given, then, when}; -use e2e_cucumber::mock_server::MockServer; +use e2e_cucumber::mock_server::{ + MockServer, ServiceRecordOptions, chat_text_reply, chat_tool_call_reply, +}; use crate::E2eWorld; @@ -298,3 +300,241 @@ async fn no_tool_result_printed(world: &mut E2eWorld) { "a tool result was printed for a refused request:\n{stdout}" ); } + +// ── Model output reaching the terminal (chat-20, chat-21, chat-22) ── + +/// The model id the CLI's built-in assistant is served under; see the same +/// constant in `planner_steps.rs` for why the harness repeats it. +const BUILTIN_ASSISTANT_MODEL_ID: &str = "Qwen3-4B-Instruct-2507-GGUF"; + +/// A question none of the CLI's own shortcuts answers, so the assistant is +/// asked (the wording chat-11 uses, for the same reason). +const OPEN_QUESTION: &str = "I would like some help figuring out what to do with my GPU"; + +/// The text a hostile reply conceals with SGR 8. On a terminal that honours it, +/// the line reads as if this were not there. +const CONCEALED_REPLY_TEXT: &str = " --port 9"; + +/// A reply that hides [`CONCEALED_REPLY_TEXT`], retitles the window (OSC 0), +/// writes the clipboard (OSC 52) and repeats the title in the 8-bit form: the +/// shapes a model can use to make the terminal show something other than what +/// was sent. +fn hostile_reply() -> String { + format!( + "Run rocm serve qwen\u{1b}[8m{CONCEALED_REPLY_TEXT}\u{1b}[28m --managed\n\ + \u{1b}]0;rocm\u{7}\u{1b}]52;c;aGVsbG8=\u{7}\u{9d}0;rocm\u{9c}Done.\n\ + An open title\u{1b}]0;never closed\n\ + Still here." + ) +} + +/// Start a scripted mock as the ready built-in assistant: it answers each chat +/// completion with the next entry of `script`, and an unscripted one with an +/// HTTP 500, so a CLI that asked more often than expected fails loudly. +async fn start_scripted_assistant(world: &mut E2eWorld, script: Vec) { + let mock = MockServer::start_with_chat_script(BUILTIN_ASSISTANT_MODEL_ID, script).await; + world.endpoint = Some(mock.base_url()); + world.model_name = Some(BUILTIN_ASSISTANT_MODEL_ID.to_string()); + world.mock = Some(mock); + world.register_mock_service_with(ServiceRecordOptions { + // Without the latch the CLI's readiness check sends its own completion + // probe first, which would consume the first scripted reply. + inference_verified: true, + ..ServiceRecordOptions::default() + }); +} + +#[given("a local chat assistant whose reply hides part of itself with terminal escape sequences")] +async fn assistant_reply_hides_part_of_itself(world: &mut E2eWorld) { + start_scripted_assistant( + world, + vec![chat_text_reply( + BUILTIN_ASSISTANT_MODEL_ID, + &hostile_reply(), + )], + ) + .await; +} + +#[given( + "a local chat assistant that asks to install ROCm into a folder whose name holds an ampersand" +)] +async fn assistant_asks_for_an_ampersand_folder(world: &mut E2eWorld) { + let prefix = crate::e2e::therock_steps::ampersand_folder(world) + .to_string_lossy() + .into_owned(); + start_scripted_assistant( + world, + vec![chat_tool_call_reply( + BUILTIN_ASSISTANT_MODEL_ID, + "install_sdk", + &serde_json::json!({ + "channel": "release", + "format": "wheel", + "prefix": prefix, + }), + )], + ) + .await; +} + +#[when("the user asks the chat assistant with ROCm tools a question in one-shot chat")] +async fn user_asks_the_assistant_a_question(world: &mut E2eWorld) { + // The exit code is checked by the `Then` steps: chat-21 expects a refusal. + let (stdout, stderr, rc) = crate::run_rocm( + world, + &[ + "chat", + "--tools", + "--provider", + "local", + "--prompt", + OPEN_QUESTION, + ], + ); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + +/// The control characters in whatever the CLI wrote, apart from newline and +/// tab. `char::is_control` rather than the CLI's own predicate, so the check +/// does not share a gap with the code it checks; the scripted replies carry +/// only controls (ESC, BEL and the 8-bit introducers), so it sees all of them. +fn terminal_controls(text: &str) -> Vec { + text.chars() + .filter(|character| character.is_control() && !matches!(character, '\n' | '\t')) + .collect() +} + +#[then("the CLI output carries no terminal escape sequence")] +async fn cli_output_has_no_escape(world: &mut E2eWorld) { + // The `When` step has already required a zero exit. + let output = world.cli_output.as_deref().expect("no chat CLI output"); + // Proves the reply really went through this path: the scenario would + // otherwise pass with the mock never consulted. + assert!( + output.contains("Run rocm serve qwen") && output.contains("Done."), + "the CLI did not print the scripted reply:\n{output}" + ); + let offending = terminal_controls(output); + assert!( + offending.is_empty(), + "the CLI wrote control characters {offending:?} to the terminal:\n{output:?}" + ); + // The payloads go with their sequences rather than being printed as text. + assert!( + !output.contains("aGVsbG8=") && !output.contains("0;rocm"), + "an escape sequence's payload reached the output:\n{output:?}" + ); + // A sequence left open costs the rest of its own line, never the lines + // after it. + assert!( + output.contains("An open title\nStill here."), + "a line after an unterminated sequence was lost:\n{output:?}" + ); +} + +#[then("the hidden part of the reply is shown in plain text")] +async fn hidden_reply_text_is_shown(world: &mut E2eWorld) { + let output = world.cli_output.as_deref().expect("no chat CLI output"); + // Shown, not dropped along with its escape sequence: the user must see what + // the model actually sent, where it sent it. + assert!( + output.contains(&format!( + "Run rocm serve qwen{CONCEALED_REPLY_TEXT} --managed" + )), + "the concealed text must be printed where it was, in plain sight:\n{output:?}" + ); +} + +/// The refusal the one-shot chat turn ended with, on stderr: the turn failed, +/// the assistant was asked once (so the refusal came from its reply), and the +/// message names the `&` and the command the user can run with the value. +/// The state is checked in the step after: nothing offered to paste. +fn refused_ampersand_value(world: &E2eWorld) -> &str { + let stdout = world.cli_output.as_deref().expect("no command was run"); + let stderr = world.cli_stderr.as_deref().unwrap_or_default(); + let rc = world.cli_rc.expect("no command was run"); + assert_ne!(rc, 0, "the request must be refused:\n{stdout}\n{stderr}"); + let requests = world + .mock + .as_ref() + .expect("no mock server running") + .chat_requests(); + assert_eq!( + requests.len(), + 1, + "the assistant must have been asked once, and the refusal must come from its reply" + ); + assert!( + stderr.contains("'&'") + && stderr.contains( + "If you meant that value, supply it yourself by running `rocm install sdk` with it" + ), + "the refusal must name the character and say how to supply the value:\n{stderr}" + ); + assert!( + terminal_controls(stderr).is_empty(), + "the refusal reached the terminal with control characters:\n{stderr:?}" + ); + stderr +} + +#[then("the CLI refuses the assistant's folder and tells the user to supply it themselves")] +async fn cli_refuses_the_assistants_folder(world: &mut E2eWorld) { + let stderr = refused_ampersand_value(world); + assert!( + stderr.contains("local assistant chose the argument"), + "the refusal must be the one for the assistant's own call:\n{stderr}" + ); +} + +#[given( + "a local chat assistant that asks for a plan to install ROCm into a folder whose name holds an ampersand" +)] +async fn assistant_asks_for_a_plan_into_an_ampersand_folder(world: &mut E2eWorld) { + let prefix = crate::e2e::therock_steps::ampersand_folder(world) + .to_string_lossy() + .into_owned(); + start_scripted_assistant( + world, + vec![chat_tool_call_reply( + BUILTIN_ASSISTANT_MODEL_ID, + "natural_language_plan", + &serde_json::json!({ "request": format!("install therock into {prefix}") }), + )], + ) + .await; +} + +#[then("the CLI refuses the plan for the assistant's request")] +async fn cli_refuses_the_plan_for_the_assistants_request(world: &mut E2eWorld) { + let stderr = refused_ampersand_value(world); + assert!( + stderr.contains("the plan for the local assistant's request chose the argument"), + "the refusal must be the one for the plan, not for the call:\n{stderr}" + ); +} + +#[then("no command line holding that folder is offered to paste")] +async fn no_line_holding_the_folder_is_offered(world: &mut E2eWorld) { + // The refusal is on stderr; stdout is where the transcript, and with it the + // `advanced manual command:` line and the approval it stands for, would + // have been. The whole turn is withheld, so stdout is empty: nothing at all + // was offered. `rocm chat --prompt` never makes a change itself, so there + // is no folder or server here whose absence the refusal would prove; the + // same chat turn's state is checked by `assert_nothing_changed_and_nothing_offered` + // in `providers.rs`. + let stdout = world.cli_output.as_deref().expect("no command was run"); + assert!( + stdout.is_empty(), + "a refused turn must print nothing to stdout:\n{stdout}" + ); + // stderr carries the refusal, which quotes the value, but no command line. + let stderr = world.cli_stderr.as_deref().unwrap_or_default(); + assert!( + !stderr.contains("advanced manual command:") && !stderr.contains("tool_call:"), + "a refused turn must offer no command line on stderr either:\n{stderr}" + ); +} diff --git a/tests/e2e-cucumber/tests/e2e/runtime_lifecycle_steps.rs b/tests/e2e-cucumber/tests/e2e/runtime_lifecycle_steps.rs index 429d3573c..e79228099 100644 --- a/tests/e2e-cucumber/tests/e2e/runtime_lifecycle_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/runtime_lifecycle_steps.rs @@ -729,6 +729,20 @@ async fn try_activate_second(world: &mut E2eWorld) { activate_second(world).await; } +/// A runtime name that is not registered, with a window-title sequence and an +/// erase-line sequence inside it. The report quotes it, so it is printed with +/// the sequences removed: [`ESCAPED_RUNTIME_NAME_SHOWN`]. +const ESCAPED_RUNTIME_NAME: &str = "release-tarball\u{1b}]0;rocm\u{7}-gfx9\u{1b}[2K42-copy"; +/// [`ESCAPED_RUNTIME_NAME`] with its sequences, and only them, removed. +const ESCAPED_RUNTIME_NAME_SHOWN: &str = "release-tarball-gfx942-copy"; + +#[when("the user tries to activate a runtime whose name carries terminal escape sequences")] +async fn activate_escaped_name(world: &mut E2eWorld) { + let (stdout, stderr, rc) = + crate::run_rocm(world, &["runtimes", "activate", ESCAPED_RUNTIME_NAME]); + record(world, stdout, stderr, rc); +} + #[when("the user tries to activate the first runtime restarting services without confirming")] async fn activate_restart_services_without_yes(world: &mut E2eWorld) { let (stdout, stderr, rc) = crate::run_rocm( @@ -898,6 +912,37 @@ async fn import_replace(world: &mut E2eWorld) { // ── Then ─────────────────────────────────────────────────────────── +#[then("the activation fails naming that runtime without its escape sequences")] +async fn activation_fails_naming_escaped_runtime(world: &mut E2eWorld) { + let stderr = world.cli_stderr.as_deref().unwrap_or_default(); + assert_ne!( + world.cli_rc, + Some(0), + "activating an unknown runtime must fail:\n{stderr}" + ); + assert!( + stderr.contains(&format!( + "installed runtime not found: {ESCAPED_RUNTIME_NAME_SHOWN}" + )), + "the report must name the runtime, its sequences removed:\n{stderr:?}" + ); +} + +#[then("the error report carries no terminal control character")] +async fn error_report_has_no_control(world: &mut E2eWorld) { + let stderr = world.cli_stderr.as_deref().unwrap_or_default(); + let controls = stderr + .chars() + .filter(|character| character.is_control() && !matches!(character, '\n' | '\t')) + .collect::>(); + assert!( + controls.is_empty(), + "control characters {controls:?} in:\n{stderr:?}" + ); + // The title the sequence carried goes with it rather than being printed. + assert!(!stderr.contains("0;rocm"), "{stderr:?}"); +} + #[then("that runtime becomes active having changed from nothing")] async fn active_changed_from_nothing(world: &mut E2eWorld) { let out = ok_output(world); diff --git a/tests/e2e-cucumber/tests/e2e/therock_steps.rs b/tests/e2e-cucumber/tests/e2e/therock_steps.rs index f96f4f26d..a10963d5a 100644 --- a/tests/e2e-cucumber/tests/e2e/therock_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/therock_steps.rs @@ -1484,10 +1484,27 @@ const QUOTED_FOLDER_NAME: &str = "it's-here"; "the user previews a wheel SDK install for arch gfx1200 into a folder whose name has a quote" )] async fn preview_unpinned_wheel_install_into_quoted_folder(world: &mut E2eWorld) { - let prefix = root(world) - .join(QUOTED_FOLDER_NAME) - .to_string_lossy() - .into_owned(); + preview_unpinned_wheel_install_into(world, QUOTED_FOLDER_NAME); +} + +/// The install folder chat-21's assistant asks for, inside the scenario's root: +/// `&` starts a second command when a line holding it is pasted into `cmd.exe`, +/// even inside the POSIX single quotes the CLI renders it in. +pub(crate) const AMPERSAND_FOLDER_NAME: &str = "R&D-rocm"; + +/// Where chat-21's folder lives, so the assistant's step and the user's own +/// preview name the same one. +pub(crate) fn ampersand_folder(world: &E2eWorld) -> std::path::PathBuf { + root(world).join(AMPERSAND_FOLDER_NAME) +} + +#[when("the user previews that install into the same folder themselves")] +async fn preview_install_into_the_ampersand_folder(world: &mut E2eWorld) { + preview_unpinned_wheel_install_into(world, AMPERSAND_FOLDER_NAME); +} + +fn preview_unpinned_wheel_install_into(world: &mut E2eWorld, folder_name: &str) { + let prefix = root(world).join(folder_name).to_string_lossy().into_owned(); preview_ok( world, &[ @@ -1508,6 +1525,15 @@ async fn preview_unpinned_wheel_install_into_quoted_folder(world: &mut E2eWorld) #[then("the dry-run command line names that folder as a single argument")] async fn assert_dry_run_command_keeps_quoted_folder_whole(world: &mut E2eWorld) { + assert_dry_run_command_names_folder(world, QUOTED_FOLDER_NAME); +} + +#[then("the dry-run command line names the folder with an ampersand as a single argument")] +async fn assert_dry_run_command_keeps_ampersand_folder_whole(world: &mut E2eWorld) { + assert_dry_run_command_names_folder(world, AMPERSAND_FOLDER_NAME); +} + +fn assert_dry_run_command_names_folder(world: &E2eWorld, folder_name: &str) { let output = stdout(world); let line = output .lines() @@ -1519,9 +1545,9 @@ async fn assert_dry_run_command_keeps_quoted_folder_whole(world: &mut E2eWorld) // through symlinks, so the folder's parent may be spelled differently from // the one this step built. What must hold is that one whole word names it. assert!( - words.iter().any(|word| { - Path::new(word).file_name() == Some(std::ffi::OsStr::new(QUOTED_FOLDER_NAME)) - }), + words + .iter() + .any(|word| { Path::new(word).file_name() == Some(std::ffi::OsStr::new(folder_name)) }), "the dry-run `command:` line must name the install folder as one argument, got `{line}`" ); } diff --git a/xtask/src/ascii_output_contract.rs b/xtask/src/ascii_output_contract.rs index 18827f11d..26f2bf1e3 100644 --- a/xtask/src/ascii_output_contract.rs +++ b/xtask/src/ascii_output_contract.rs @@ -101,13 +101,14 @@ mod tests { "crates/rocm-core/src/terminal.rs", "A classifier for untrusted terminal output, and the one file in the tree \ whose job is to name non-ASCII characters rather than print them. Every \ - literal here is a `Cf`/`Zl`/`Zp` code point in a `matches!` arm of \ - `is_control_or_format`/`is_control_or_line_separator`, enumerated so it can \ - be *removed* from a captured subprocess log before that log is quoted back \ - at the user. The module emits no text of its own: both public functions \ - return substrings of their input. Transliterating a code point here would \ - not change any output, it would stop the stripper recognising the thing it \ - exists to strip.", + non-ASCII literal outside its tests is a code point it compares a \ + character against (a control, format, separator or other \ + default-ignorable character), named so it can be removed from untrusted text, or refused in a \ + value, before it is printed. The module prints nothing: the text its \ + functions return is made of their input's characters, plus `\\n` where \ + a line break is normalised. Transliterating a code point here would not \ + change any output, it would stop the module recognising the thing it \ + exists to remove.", ), ( "apps/rocm/src/cli_progress.rs",