From 630154a4313e575f3b69ed725cce0b6b9c944721 Mon Sep 17 00:00:00 2001 From: Roman Sirokov Date: Wed, 11 Nov 2026 15:17:42 +0200 Subject: [PATCH 1/3] fix(dash): bound chat tool output so a small-context model can answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Asking the dash Chat tab anything that makes the model call `rocm_command` with `["model"]` ended the turn with the provider's raw HTTP 400 instead of an answer: the tool result measured 204,470 characters (~58k tokens), more than a locally served model's entire context (llama.cpp defaults `n_ctx` to 32768). Four things compounded, and all four are addressed. The chat `model`/`models` tool rendered the `rocm model --verbose` diagnostics — every recipe with its aliases, engine recipes, flags and policy notes — rather than the curated list `rocm model` prints. It now renders the curated list; the diagnostics need an explicit `["model", "--verbose"]`. The MCP envelope carries a command's output twice: as the content text and again as `structuredContent.stdout`. `rocm mcp-call` consumers read the structured field, so the envelope is unchanged and the duplicate is dropped in the dash instead, where the model is the only reader. Nothing bounded a tool result at all. A read-only result is now capped at 16 KiB of serialized JSON, cut on a line boundary and closed with a marker naming how much was omitted, so neither the model nor the user can take a truncated list for a complete one. The `rocm_command` description and its `args` schema both used `["model"]` as the example, which a small local model copies literally; they now use `["config", "show"]`. Separately, a provider context overflow now reads as a plain sentence in the Chat tab naming the token counts and a way forward, with the raw provider body still logged to rocm-cli.log. Signed-off-by: Roman Sirokov --- apps/rocm/src/chat/mcp_bridge.rs | 100 +++++- crates/rocm-dash-tui/src/agent/tools.rs | 342 ++++++++++++++++++- crates/rocm-dash-tui/src/app/chat_runtime.rs | 174 +++++++++- docs/architecture.md | 4 +- docs/llm-tool-use.md | 37 ++ docs/manual-testing.md | 42 +++ skills/rocm-cli-assistant/SKILL.md | 5 + 7 files changed, 694 insertions(+), 10 deletions(-) diff --git a/apps/rocm/src/chat/mcp_bridge.rs b/apps/rocm/src/chat/mcp_bridge.rs index fa1f88097c..34f3d6e052 100644 --- a/apps/rocm/src/chat/mcp_bridge.rs +++ b/apps/rocm/src/chat/mcp_bridge.rs @@ -41,8 +41,9 @@ use crate::{ build_codex_bridge_gpu_snapshot, build_codex_bridge_snapshot, builtin_codex_bridge_engine_inventory, cli_version_string, record_cli_audit_event, render_config_text, render_engine_inventory_text_with_paths, render_examine_text, - render_examine_text_with_paths, render_model_registry_verbose_text_with_context_and_host, - render_setup_status_text, therock_install_version_selector, + render_examine_text_with_paths, render_model_registry_text_with_context_and_host, + render_model_registry_verbose_text_with_context_and_host, render_setup_status_text, + therock_install_version_selector, }; use crate::{comfyui, providers, therock}; use anyhow::{Context, Result, bail}; @@ -497,8 +498,28 @@ fn run_rocm_read_only_in_process(paths: &AppPaths, args: &[String]) -> Result + { + Ok(render_model_registry_text_with_context_and_host( + Some(paths), + None, + None, + )) + } + [command, flag] + if (command.eq_ignore_ascii_case("model") + || command.eq_ignore_ascii_case("models")) + && flag.eq_ignore_ascii_case("--verbose") => { Ok(render_model_registry_verbose_text_with_context_and_host( Some(paths), @@ -1177,6 +1198,81 @@ mod tests { assert!(text.lines().any(|line| line == version_line), "{text}"); } + /// What the chat `model` tool hands back has to be what `rocm model` + /// prints, not the `--verbose` diagnostics. The verbose dump is every + /// recipe with its aliases, engine recipes, flags and policy notes; on a + /// developer host it measured 204,470 characters against 864 for plain + /// `rocm model`, which is more than a locally served model's whole + /// context. Asking a small model "which LLMs can this machine support" + /// makes it call exactly this, so the one question the tool exists for + /// used to end in a provider context-overflow error. + #[test] + fn chat_model_tool_returns_the_curated_list_rocm_model_prints() { + let (_root, paths) = test_paths("mcp-model-curated"); + + let result = run_internal_mcp_call( + &paths, + "rocm_command", + serde_json::json!({ "args": ["model"] }), + false, + ) + .expect("read-only rocm_command model should run"); + let text = mcp_tool_result_text(&result); + + // The same recipes, from the same renderer `rocm model` calls. + let curated = render_model_registry_text_with_context_and_host(Some(&paths), None, None); + assert!( + text.contains(curated.trim()), + "the tool must return what `rocm model` prints:\n{text}" + ); + // And not the per-recipe diagnostics, which are what made it huge. + assert!( + !text.contains("aliases=["), + "the verbose dump must not be the default:\n{text}" + ); + // Comfortably inside the dash's 16 KiB per-result budget + // (`TOOL_RESULT_CHAR_BUDGET` in `crates/rocm-dash-tui/src/agent/tools.rs`), + // so a chat turn carrying it is never truncated on the built-in + // catalog. Asserted here rather than imported so the bin does not + // depend on a dash constant. + assert!( + text.len() < 16 * 1024, + "the curated list is {} characters, past the dash budget", + text.len() + ); + } + + /// The diagnostics are still reachable — the model has to ask for them, + /// the same way a person types `rocm model --verbose`. + #[test] + fn chat_model_tool_still_renders_the_verbose_dump_on_request() { + let (_root, paths) = test_paths("mcp-model-verbose"); + + let result = run_internal_mcp_call( + &paths, + "rocm_command", + serde_json::json!({ "args": ["model", "--verbose"] }), + false, + ) + .expect("read-only rocm_command model --verbose should run"); + let text = mcp_tool_result_text(&result); + + assert!(text.contains("model recipes"), "{text}"); + assert!( + text.contains("aliases=["), + "--verbose must still render the per-recipe diagnostics:\n{text}" + ); + // The two really are different sizes; without this the test above + // could pass against a renderer that lost its diagnostics entirely. + let curated = render_model_registry_text_with_context_and_host(Some(&paths), None, None); + assert!( + text.len() > curated.len(), + "the verbose dump ({}) should be longer than the curated list ({})", + text.len(), + curated.len() + ); + } + #[test] fn natural_language_plan_returns_structured_mutating_action() { let (_root, paths) = test_paths("nl-plan-structured-mutating"); diff --git a/crates/rocm-dash-tui/src/agent/tools.rs b/crates/rocm-dash-tui/src/agent/tools.rs index 0a6907278c..8bc8ba95d7 100644 --- a/crates/rocm-dash-tui/src/agent/tools.rs +++ b/crates/rocm-dash-tui/src/agent/tools.rs @@ -10,6 +10,12 @@ //! seam types (`StateSnapshot`, `AgentError`, `AgentClient`) stay in //! `agent/mod.rs`; the JSON summarization these tools call lives in //! `agent::snapshot`. +//! +//! This is also where a read-only tool result is shaped for the model: +//! [`model_facing_tool_result`] drops the command output the bin's MCP +//! envelope carries twice and bounds what is left to +//! [`TOOL_RESULT_CHAR_BUDGET`], so one large result cannot exhaust a locally +//! served model's context. See `docs/llm-tool-use.md`. use std::sync::{Arc, Mutex}; @@ -230,6 +236,178 @@ pub const TELEMETRY_TOOL_NAMES: [&str; 4] = [ // `rocm-core` into this crate; the `agent` module stays the sole `rig` namer. // --------------------------------------------------------------------------- +/// How many characters of one tool result may reach the model. +/// +/// A tool result is not free: it is sent back as a message and counts against +/// the model's context on that request and on every later turn of the same +/// conversation. A locally served model is typically configured with a 32k +/// context (llama.cpp's default `n_ctx`), of which the system prompt and the +/// tool schemas already take a few thousand tokens, so the remaining room has +/// to cover several turns. 16 KiB is roughly 4.5k tokens — one large result +/// still fits alongside the prompt and a handful of turns, where an uncapped +/// one (`rocm_command` with `["model"]` measured 204,470 characters, about +/// 58k tokens) cannot fit in such a context at all and the provider rejects +/// the whole request. +pub(crate) const TOOL_RESULT_CHAR_BUDGET: usize = 16 * 1024; + +/// Shape one executor result for the model: drop the command output the +/// envelope carries twice, then bound what is left to `budget`. +/// +/// Both halves exist because the result is JSON the dash hands to the model +/// verbatim, unlike the CLI chat path (`apps/rocm/src/chat/turn.rs`), which +/// reads only the envelope's text. +fn model_facing_tool_result(value: Value, budget: usize) -> Value { + cap_tool_result(drop_duplicated_command_output(value), budget) +} + +/// Drop `structuredContent.stdout` / `.stderr` when the envelope's own text +/// already carries them. +/// +/// The bin's `internal_mcp_tool_result_from_command` puts a command's captured +/// output in `content[0].text` *and* again in `structuredContent.stdout` (and +/// `.stderr`). The MCP envelope keeps both — `rocm mcp-call` and the approval +/// replay path read the structured fields — but the model only needs one copy, +/// and charging it twice for the same bytes is what makes a big read-only +/// result twice as expensive as it has to be. Only the duplicated keys go; a +/// tool whose data lives *only* in `structuredContent` (`engines`, +/// `bridge_snapshot`, `automations`, `gpu_snapshot`) is untouched. +fn drop_duplicated_command_output(mut value: Value) -> Value { + let text = tool_result_content_text(&value); + if text.is_empty() { + return value; + } + let Some(structured) = value + .get_mut("structuredContent") + .and_then(Value::as_object_mut) + else { + return value; + }; + for key in ["stdout", "stderr"] { + let duplicated = structured + .get(key) + .and_then(Value::as_str) + .is_some_and(|s| { + let trimmed = s.trim(); + trimmed.is_empty() || text.contains(trimmed) + }); + if duplicated { + structured.remove(key); + } + } + value +} + +/// The text items of an MCP tool-result envelope, joined. Empty when the value +/// is not such an envelope (a bare `{"error": …}`, say). +fn tool_result_content_text(value: &Value) -> String { + value + .get("content") + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .filter(|item| item.get("type").and_then(Value::as_str) == Some("text")) + .filter_map(|item| item.get("text").and_then(Value::as_str)) + .collect::>() + .join("\n") + }) + .unwrap_or_default() +} + +/// Everything the envelope carries, rendered as plain text. +/// +/// Used only when a result has to be truncated, so the model reads prose +/// rather than a JSON document cut off mid-escape. Nothing is dropped +/// silently: the text items come first, then whatever `structuredContent` +/// still holds after [`drop_duplicated_command_output`]. +fn tool_result_plain_text(value: &Value) -> String { + let mut parts = Vec::new(); + let text = tool_result_content_text(value); + if !text.is_empty() { + parts.push(text); + } + match value.get("structuredContent") { + Some(structured) if !structured.is_null() => { + parts.push( + serde_json::to_string_pretty(structured).unwrap_or_else(|_| structured.to_string()), + ); + } + _ => {} + } + if parts.is_empty() { + serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string()) + } else { + parts.join("\n") + } +} + +/// The line appended to a truncated result. +/// +/// Explicit about being a partial result and about how much is missing, so +/// neither the model nor the user reading the model's answer can take a cut +/// list for a complete one. +fn truncation_marker(shown_chars: usize, total_chars: usize) -> String { + format!( + "\n[output truncated: {shown_chars} of {total_chars} characters shown, \ + {omitted} omitted. This is NOT the whole result — say so when you use it, \ + and run the matching `rocm` command in a terminal for the full output.]", + omitted = total_chars.saturating_sub(shown_chars) + ) +} + +/// Bound a tool result to `budget` characters of serialized JSON. +/// +/// A result that already fits is returned untouched, so the common case keeps +/// its exact shape. One that does not is replaced by the plain-text rendering +/// cut on a line boundary plus [`truncation_marker`]; `budget` is measured +/// against the *serialized* value, which is what actually reaches the model, +/// so JSON escaping cannot push it back over. +fn cap_tool_result(value: Value, budget: usize) -> Value { + if value.to_string().len() <= budget { + return value; + } + let full = tool_result_plain_text(&value); + let total_chars = full.chars().count(); + // Candidate cuts: the start, then the end of every line, so a cut never + // lands inside a line (and never inside a UTF-8 character). + let mut cuts = vec![0usize]; + cuts.extend(full.match_indices('\n').map(|(index, _)| index + 1)); + if !full.is_empty() && !full.ends_with('\n') { + cuts.push(full.len()); + } + // Largest cut that still fits. `best` is only ever set to a cut that was + // measured as fitting, so the returned value is within budget even where + // the length is not perfectly monotonic in the cut (the marker's digit + // count shifts by one across a power of ten). + let (mut low, mut high, mut best) = (0usize, cuts.len() - 1, 0usize); + while low <= high { + let mid = low.midpoint(high); + if truncated_tool_result(&full, cuts[mid], total_chars) + .to_string() + .len() + <= budget + { + best = mid; + low = mid + 1; + } else if mid == 0 { + break; + } else { + high = mid - 1; + } + } + truncated_tool_result(&full, cuts[best], total_chars) +} + +/// The truncated form of `full` kept up to the byte offset `cut`. +fn truncated_tool_result(full: &str, cut: usize, total_chars: usize) -> Value { + let kept = &full[..cut]; + let shown_chars = kept.chars().count(); + json!({ + "truncated": true, + "tool_output": format!("{kept}{}", truncation_marker(shown_chars, total_chars)), + }) +} + /// Shared body for every read-only ROCm tool: forward the intent to the injected /// executor (None ⇒ a clear "not available in this mode" message; ApprovalRequired /// ⇒ a "needs approval" error and nothing runs — `rocm_command` lands here for @@ -255,7 +433,7 @@ async fn run_rocm_read_tool( return json!({ "error": "ROCm tools are unavailable in this mode (demo/replay/mock)." }); }; match tokio::task::spawn_blocking(move || exec.execute(name, &args)).await { - Ok(RocmToolOutcome::Result(v)) => v, + Ok(RocmToolOutcome::Result(v)) => model_facing_tool_result(v, TOOL_RESULT_CHAR_BUDGET), Ok(RocmToolOutcome::Error(s)) => json!({ "error": s }), Ok(RocmToolOutcome::ApprovalRequired(_)) => { json!({ "error": "this action requires approval (interactive chat only)" }) @@ -418,17 +596,24 @@ rocm_read_tool!( rocm_read_tool!( RocmCommandRocmTool, "rocm_command", + // The examples are deliberately the cheap commands. A small local model + // copies the example in a tool description literally, and the example used + // to be `["model"]` — the single largest read-only result there is, which + // on a 32k-context model used to end the turn with a provider + // context-overflow error instead of an answer. "Run a READ-ONLY rocm CLI subcommand and return its output. Allowed: \ - model/models, config show, runtimes (list), logs, daemon status. Pass the \ - argv as `args`, e.g. [\"model\"] or [\"config\",\"show\"]. Mutating \ - subcommands are rejected.", + config show, runtimes (list), logs, daemon status, model/models. Pass the \ + argv as `args`, e.g. [\"config\",\"show\"] or [\"runtimes\",\"list\"]. \ + [\"model\"] lists the recommended models and [\"model\",\"--verbose\"] \ + adds the per-recipe diagnostics, which are long. Mutating subcommands are \ + rejected.", { "type": "object", "properties": { "args": { "type": "array", "items": { "type": "string" }, - "description": "Argv for the rocm subcommand, e.g. [\"model\"]." + "description": "Argv for the rocm subcommand, e.g. [\"config\",\"show\"]." } }, "required": ["args"] @@ -920,6 +1105,153 @@ mod tests { ); } + /// A command-backed MCP envelope of roughly `text_chars` characters, + /// shaped exactly as the bin's `internal_mcp_tool_result_from_command` + /// builds one: the command output in `content[0].text` and again in + /// `structuredContent.stdout`. + fn command_envelope(text_chars: usize) -> (String, serde_json::Value) { + let line = "Qwen/Qwen3.5-4B Q4_K_M fits this GPU\n"; + let stdout = line.repeat(text_chars.div_ceil(line.len())); + let envelope = json!({ + "content": [{ "type": "text", "text": format!("Ran `rocm` command.\n\n{stdout}") }], + "structuredContent": { + "argv": ["rocm", "model"], + "exit_status": 0, + "stdout": stdout, + "stderr": "", + }, + "isError": false, + }); + (stdout, envelope) + } + + /// The `[output truncated: A of B characters shown, C omitted` figures. + fn truncation_figures(text: &str) -> (usize, usize, usize) { + let marker = text + .split_once("[output truncated: ") + .unwrap_or_else(|| panic!("no truncation marker in:\n{text}")) + .1; + let numbers = marker + .split(|c: char| !c.is_ascii_digit()) + .filter(|part| !part.is_empty()) + .take(3) + .map(|part| part.parse::().expect("marker figure")) + .collect::>(); + (numbers[0], numbers[1], numbers[2]) + } + + #[tokio::test] + async fn an_oversized_read_only_tool_result_is_capped_before_the_model_sees_it() { + // A ~200 KB result is what `rocm_command` with `["model"]` used to + // return (204,470 characters measured on a developer host). Sent whole + // to a locally served model it exceeds the entire context and the + // provider refuses the request, so the user gets an HTTP 400 instead + // of an answer. + let (_, envelope) = command_envelope(200 * 1024); + assert!( + envelope.to_string().len() > 200 * 1024, + "the fixture must be bigger than the budget or it proves nothing" + ); + let tool = RocmCommandRocmTool { + executor: Some(Arc::new(FakeExec(envelope)) as SharedRocmToolExecutor), + fired: Arc::new(Mutex::new(Vec::new())), + }; + + let out = tool.call(json!({ "args": ["model"] })).await.expect("ok"); + + // What the model is handed is the serialized value, so that is what + // the budget is measured against. + let rendered = out.to_string(); + assert!( + rendered.len() <= TOOL_RESULT_CHAR_BUDGET, + "a tool result of {} characters reached the model; the budget is {TOOL_RESULT_CHAR_BUDGET}", + rendered.len() + ); + // It says it is partial, rather than reading as a complete answer. + assert_eq!(out["truncated"], json!(true), "{out}"); + let text = out["tool_output"].as_str().expect("tool_output text"); + assert!( + text.contains("[output truncated:") && text.trim_end().ends_with(']'), + "the marker must close the result so the model cannot miss it: {text}" + ); + // …and says how much is missing, consistently. + let (shown, total, omitted) = truncation_figures(text); + assert_eq!(shown + omitted, total, "the marker's figures must add up"); + assert!( + omitted > total / 2, + "a 200 KB result cut to {TOOL_RESULT_CHAR_BUDGET} characters must report most of it omitted, got {omitted} of {total}" + ); + // The part that is shown is real output, cut on a line boundary. + assert!(text.contains("Qwen/Qwen3.5-4B"), "{text}"); + assert!( + text.split_once("\n[output truncated:") + .is_some_and(|(kept, _)| kept.is_empty() || kept.ends_with('\n')), + "the cut must land on a line boundary: {text}" + ); + } + + #[tokio::test] + async fn a_command_backed_result_reaches_the_model_once_not_twice() { + // The bin's envelope carries the command output twice — once as the + // text a reader sees, once as `structuredContent.stdout` — because + // `rocm mcp-call` consumers read the structured field. The dash hands + // the whole value to the model, so without this the model pays for + // every byte twice. + let (stdout, envelope) = command_envelope(2 * 1024); + let before = envelope.to_string().len(); + let tool = RocmCommandRocmTool { + executor: Some(Arc::new(FakeExec(envelope)) as SharedRocmToolExecutor), + fired: Arc::new(Mutex::new(Vec::new())), + }; + + let out = tool + .call(json!({ "args": ["config", "show"] })) + .await + .expect("ok"); + + // One copy survives, and it is the readable one. + let text = out["content"][0]["text"].as_str().expect("content text"); + assert!(text.contains(stdout.trim()), "{out}"); + assert!( + out["structuredContent"].get("stdout").is_none(), + "the duplicated copy must not reach the model: {out}" + ); + assert!( + out["structuredContent"].get("stderr").is_none(), + "an empty stderr is pure overhead for the model: {out}" + ); + // The fields that are not duplicates stay, so the model can still see + // what ran and whether it worked. + assert_eq!(out["structuredContent"]["exit_status"], json!(0), "{out}"); + assert_eq!(out["structuredContent"]["argv"][1], json!("model"), "{out}"); + assert!( + out.to_string().len() < before / 2 + 512, + "dropping the duplicate should roughly halve the result: {} vs {before}", + out.to_string().len() + ); + } + + #[tokio::test] + async fn a_result_within_the_budget_keeps_its_exact_shape() { + // The cap must not reshape ordinary results: a tool whose data lives + // only in `structuredContent` (engines, bridge_snapshot, automations) + // would lose it if every result were flattened to text. + let value = json!({ + "content": [{ "type": "text", "text": "Found 3 engine entries." }], + "structuredContent": { "engines": [{ "id": "lemonade", "available": true }] }, + "isError": false, + }); + let tool = EnginesRocmTool { + executor: Some(Arc::new(FakeExec(value.clone())) as SharedRocmToolExecutor), + fired: Arc::new(Mutex::new(Vec::new())), + }; + let out = tool.call(json!({})).await.expect("ok"); + assert_eq!( + out, value, + "an in-budget result must pass through unchanged" + ); + } + /// Recording executor for the mutating-tool surfacing test: `execute` /// returns `ApprovalRequired`; `execute_approved` records a call (which must /// NOT happen during the rig tool loop). diff --git a/crates/rocm-dash-tui/src/app/chat_runtime.rs b/crates/rocm-dash-tui/src/app/chat_runtime.rs index 2b4d8cd019..002b7f0e99 100644 --- a/crates/rocm-dash-tui/src/app/chat_runtime.rs +++ b/crates/rocm-dash-tui/src/app/chat_runtime.rs @@ -11,6 +11,10 @@ //! one `drain_*` method per one-shot edge on `AppState` (`provider_switch`, //! `chat_endpoint_rebuild`, `chat_dispatch`) consumes that edge. The detect and //! persist edges do not touch the backend and stay in `event_loop`. +//! +//! [`chat_error_message`] sits on the dispatch edge: it turns a failed round +//! trip into the line the transcript shows, rewriting a provider context +//! overflow into plain language and leaving every other error as it is. use tokio::sync::mpsc; @@ -27,6 +31,82 @@ use super::{AppState, ProviderSwitch}; type SharedAgent = std::sync::Arc; +/// Turn a backend error into the line the Chat tab shows. +/// +/// Most errors are already readable and pass through unchanged. The one that +/// is not is a context overflow: the provider answers HTTP 400 with its own +/// JSON body, and the transcript used to show the whole thing — status line, +/// error type, token counts and all — which tells a user nothing about what +/// happened or what to do next. That one case is rewritten into a sentence +/// that names the cause and a way forward, and the raw body is logged so the +/// detail is still available in `rocm-cli.log`. +fn chat_error_message(raw: &str) -> String { + let Some(message) = context_overflow_message(raw) else { + return raw.to_owned(); + }; + // Only on the rewritten path: an error shown verbatim is already logged + // by the backend, and this is the one case where what the user sees no + // longer contains what a maintainer would need. + tracing::warn!(error = %raw, "chat request exceeded the model's context"); + message +} + +/// The plain-language form of a provider context-overflow error, or `None` +/// when `raw` is some other failure. +/// +/// Matches on the provider's own error type rather than on the HTTP status: +/// a 400 covers many unrelated mistakes, and only this one has a cause the +/// user can act on. The token counts come out of the same body; a body that +/// omits them still gets the sentence, without the numbers. +fn context_overflow_message(raw: &str) -> Option { + if !raw.contains("exceed_context_size_error") { + return None; + } + let counts = json_number_field(raw, "n_prompt_tokens") + .zip(json_number_field(raw, "n_ctx")) + .map(|(used, available)| { + format!( + " ({} of {} tokens)", + group_thousands(used), + group_thousands(available) + ) + }) + .unwrap_or_default(); + Some(format!( + "A ROCm check returned more text than this model can read{counts}. \ + Try a more specific question, or a model with a larger context." + )) +} + +/// Read `"": ` out of a JSON fragment embedded in a longer +/// message. Deliberately not a full parse: the body arrives wrapped in the +/// backend's own error prose, so there is no single value to deserialize. +fn json_number_field(raw: &str, field: &str) -> Option { + let needle = format!("\"{field}\""); + let after = raw.split_once(&needle)?.1; + let after = after.trim_start().strip_prefix(':')?.trim_start(); + let digits = after + .split(|c: char| !c.is_ascii_digit()) + .next() + .filter(|digits| !digits.is_empty())?; + digits.parse().ok() +} + +/// `53820` → `53,820`. The counts are the point of the message, and an +/// ungrouped five-digit number next to another one is hard to compare at a +/// glance. +fn group_thousands(value: u64) -> String { + let digits = value.to_string(); + let mut out = String::with_capacity(digits.len() + digits.len() / 3); + for (index, ch) in digits.chars().enumerate() { + if index > 0 && (digits.len() - index).is_multiple_of(3) { + out.push(','); + } + out.push(ch); + } + out +} + pub(super) struct ChatRuntime { /// The backend the next submit is routed to; `None` when none could be /// built, in which case a submit surfaces an error turn. @@ -345,7 +425,7 @@ impl ChatRuntime { let msg = match agent.complete(&history, snapshot).await { Ok(text) => ClientMsg::ChatReply { text }, Err(e) => ClientMsg::ChatError { - message: e.to_string(), + message: chat_error_message(&e.to_string()), }, }; let _ = reply_tx.send(msg); @@ -710,6 +790,98 @@ mod tests { } } + // --- error messages ------------------------------------------------------ + + /// The body a local llama.cpp-style server answers a too-large request + /// with, wrapped in the backend prose the Chat tab used to show verbatim. + const CONTEXT_OVERFLOW_ERROR: &str = "chat request failed: CompletionError: HttpError: \ + Invalid status code 400 Bad Request with message: \ + {\"error\":{\"code\":400,\"message\":\"request (53820 tokens) exceeds the available \ + context size (32768 tokens), try increasing it\",\ + \"type\":\"exceed_context_size_error\",\"n_prompt_tokens\":53820,\"n_ctx\":32768}}"; + + /// In-memory `tracing` writer so a test can read back what was logged. + #[derive(Clone, Default)] + struct LogCapture(Arc>>); + impl std::io::Write for LogCapture { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().unwrap().extend_from_slice(buf); + Ok(buf.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + impl tracing_subscriber::fmt::MakeWriter<'_> for LogCapture { + type Writer = Self; + fn make_writer(&self) -> Self::Writer { + self.clone() + } + } + + #[test] + fn a_context_overflow_reads_as_plain_language_and_keeps_the_raw_body_in_the_log() { + let logs = LogCapture::default(); + let subscriber = tracing_subscriber::fmt() + .with_writer(logs.clone()) + .with_ansi(false) + .finish(); + let guard = tracing::subscriber::set_default(subscriber); + let message = chat_error_message(CONTEXT_OVERFLOW_ERROR); + drop(guard); + + // What the user reads says what happened and what to do about it… + assert_eq!( + message, + "A ROCm check returned more text than this model can read \ + (53,820 of 32,768 tokens). Try a more specific question, or a model \ + with a larger context." + ); + // …and none of the provider's wire detail. + for noise in ["HttpError", "400", "exceed_context_size_error", "n_ctx"] { + assert!( + !message.contains(noise), + "`{noise}` leaked into the chat message: {message}" + ); + } + // The detail is not lost: it is in the log a maintainer reads. + let logged = String::from_utf8(logs.0.lock().unwrap().clone()).expect("utf-8 log"); + assert!( + logged.contains("exceed_context_size_error") && logged.contains("n_prompt_tokens"), + "the raw provider body must still reach rocm-cli.log, got: {logged}" + ); + } + + #[test] + fn a_context_overflow_without_token_counts_still_reads_as_plain_language() { + let message = chat_error_message( + "chat request failed: HttpError: 400 {\"error\":{\"type\":\"exceed_context_size_error\"}}", + ); + assert!( + message.starts_with("A ROCm check returned more text"), + "{message}" + ); + assert!( + !message.contains('('), + "with no counts in the body the sentence carries no numbers: {message}" + ); + } + + #[test] + fn every_other_backend_error_is_left_alone() { + // Only the one error with an actionable cause is rewritten; rewriting + // the rest would hide detail the user needs to report a real fault. + for raw in [ + "chat request failed: connection refused", + "request timed out after 120s", + "failed to build chat client: no endpoint configured", + // A 400 that is not a context overflow keeps its own text. + "chat request failed: HttpError: Invalid status code 400 Bad Request", + ] { + assert_eq!(chat_error_message(raw), raw); + } + } + #[tokio::test] async fn a_submit_with_no_backend_surfaces_the_no_backend_error_turn() { let (tx, mut rx) = channel(); diff --git a/docs/architecture.md b/docs/architecture.md index fbd94b8757..a42f9f102b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -489,7 +489,7 @@ The dashboard client: the event loop and reducer (`app/`), the chat agent (`agen | [`agent/mod.rs`](../crates/rocm-dash-tui/src/agent/mod.rs) | The chat-agent seam: the `AgentClient` trait, `AgentError`, `StateSnapshot`, `InferenceParams` and `REQUEST_TIMEOUT`. | relocation | | [`agent/clients.rs`](../crates/rocm-dash-tui/src/agent/clients.rs) | The `AgentClient` backends: Rig (OpenAI-compatible), ChatGPT (OAuth device code), Anthropic, and the in-memory `MockAgentClient`. | relocation | | [`agent/snapshot.rs`](../crates/rocm-dash-tui/src/agent/snapshot.rs) | Pure JSON summaries of the cached telemetry snapshot for the agent's tools, with no `rig` dependency. | relocation | -| [`agent/tools.rs`](../crates/rocm-dash-tui/src/agent/tools.rs) | The Rig `Tool` wrappers: the telemetry tools over the cached snapshot, the read-only and mutating ROCm tools, and their registration on an agent builder. | relocation | +| [`agent/tools.rs`](../crates/rocm-dash-tui/src/agent/tools.rs) | The Rig `Tool` wrappers: the telemetry tools over the cached snapshot, the read-only and mutating ROCm tools, their registration on an agent builder, and the per-result size cap and duplicate-output trim applied before a tool result reaches the model. | relocation | #### `app/` @@ -498,7 +498,7 @@ The dashboard client: the event loop and reducer (`app/`), the chat agent (`agen | [`app/mod.rs`](../crates/rocm-dash-tui/src/app/mod.rs) | The reducer's front: `AppState` and its constructor, the theme picker, row selection, and `apply_event`, which folds daemon events into state. | flat | | [`app/actions.rs`](../crates/rocm-dash-tui/src/app/actions.rs) | `KeyAction` dispatch: maps a key press or a resolved mouse hit to a `KeyAction` and applies it to reducer state (`apply_action`). | domain | | [`app/chat.rs`](../crates/rocm-dash-tui/src/app/chat.rs) | Chat-backend construction, local-engine detection, and saving an accepted endpoint to the dashboard config. | relocation | -| [`app/chat_runtime.rs`](../crates/rocm-dash-tui/src/app/chat_runtime.rs) | `ChatRuntime`, the live chat backend the tick loop owns: which agent answers the next turn, and the drains for the provider-switch, endpoint-rebuild and dispatch edges. | flat | +| [`app/chat_runtime.rs`](../crates/rocm-dash-tui/src/app/chat_runtime.rs) | `ChatRuntime`, the live chat backend the tick loop owns: which agent answers the next turn, the drains for the provider-switch, endpoint-rebuild and dispatch edges, and the mapping from a backend error to the line the Chat tab shows. | flat | | [`app/chat_state.rs`](../crates/rocm-dash-tui/src/app/chat_state.rs) | An `impl AppState` block for chat consent, the local-endpoint detection offer, submitting a turn, and applying the agent's reply or error. | flat | | [`app/event_loop.rs`](../crates/rocm-dash-tui/src/app/event_loop.rs) | The tick loop: `run` (set up, run, tear down) and `event_loop`, with the frame-draw gate and the registry-probe seeding. | domain | | [`app/focus.rs`](../crates/rocm-dash-tui/src/app/focus.rs) | Focused-host policy: what a dashboard launched for Setup, Serve or Examine does differently (no daemon client or chat, one overlay opened at start, console close keys swallowed). | flat | diff --git a/docs/llm-tool-use.md b/docs/llm-tool-use.md index 9626d47799..67667c83c5 100644 --- a/docs/llm-tool-use.md +++ b/docs/llm-tool-use.md @@ -55,6 +55,43 @@ rocm-cli local assistants use structured tools, not shell commands. where vLLM is the supported path. The assistant should answer platform questions from the injected facts and must never suggest CPU fallback. +## Tool Result Shape And Size + +A tool result is an MCP envelope: a `content` array of text items, an optional +`structuredContent` object, and `isError`. For a command-backed tool the +envelope carries the command's output twice — as the `content` text and again +as `structuredContent.stdout` — because `rocm mcp-call` consumers read the +structured field. That full envelope is what `rocm mcp-call` prints and what +the approval replay path stores. + +What reaches the *model* is narrower, because a tool result is not free: it is +sent back as a message and counts against the model's context on that request +and on every later turn of the same conversation. A locally served model is +commonly configured with a 32k-token context (llama.cpp's default `n_ctx`), +and the system prompt plus the tool schemas already take a few thousand of +those. So the dash shapes each result before the model sees it +(`run_rocm_read_tool` in +[tools.rs](../crates/rocm-dash-tui/src/agent/tools.rs)): + +- **One copy.** `structuredContent.stdout` and `.stderr` are dropped when the + envelope's own text already carries them. Fields that are not duplicates — + `argv`, `exit_status`, and the data of tools whose payload lives only in + `structuredContent` — are kept. +- **A size cap.** A result is bounded to `TOOL_RESULT_CHAR_BUDGET` characters + of serialized JSON (16 KiB, roughly 4.5k tokens). A result within it passes + through unchanged. One that is not is replaced by its plain-text rendering, + cut on a line boundary, ending in + `[output truncated: A of B characters shown, C omitted ...]`. The marker is + part of the contract: the assistant skill tells the model to report a + truncated result as partial rather than as a complete answer. + +The CLI chat path (`rocm chat --prompt --tools`) reads only the envelope's +text, so it never sent the duplicate copy; it does not apply the cap. + +Read-only commands are rendered so that the cheap form is the default: +`rocm_command` with `["model"]` returns the curated list `rocm model` prints, +and the per-recipe diagnostics need an explicit `["model", "--verbose"]`. + This follows the same shape described by current tool-use docs: the application defines tool schemas, the model requests a tool, the application executes the tool, and the result is returned to the model for the next response. MCP tool diff --git a/docs/manual-testing.md b/docs/manual-testing.md index a2eb7dd454..5a8a325e7e 100644 --- a/docs/manual-testing.md +++ b/docs/manual-testing.md @@ -1556,3 +1556,45 @@ anything. Finally, `rocm diagnose --report --model /:Q4_K_M --context-size 8192` prints the report with no model section, and stderr says that `--model, --context-size were ignored` and nothing about the model was fetched. + +## 13. Dashboard Chat With A Small-Context Local Model + +Needs a local OpenAI-compatible endpoint serving a model with a small context +— llama.cpp's default `n_ctx` is 32768, which is the case this checks. The +point is that an answer survives a tool call that returns a lot of text. + +```bash +rocm dash +``` + +In the Chat tab, ask `which LLMs can this machine support`, which is the +question that makes the model call `rocm_command` with `["model"]`. + +Expected result: + +- A written answer listing recommended models. Not + `chat request failed: ... 400 Bad Request`, and not a raw JSON error body. +- The same question asked of `rocm mcp-call` returns the curated list, and the + per-recipe diagnostics only when asked for: + + ```bash + echo '{"args":["model"]}' > args.json + rocm mcp-call rocm_command --arguments-file args.json | wc -c + echo '{"args":["model","--verbose"]}' > args.json + rocm mcp-call rocm_command --arguments-file args.json | wc -c + ``` + + The first is a few kilobytes and matches what `rocm model` prints in a + terminal; the second is far larger. + +- To see the truncation path, ask something that makes the model read a long + service log (`rocm_command` with `["logs"]` on a host with a lot of history). + The model's answer should say the output was partial and point at running the + command in a terminal — it must not present a cut list as a complete one. + +- To see the overflow message, point the Chat tab at a model with a very small + context (serve one with `--ctx-size 2048`) and ask the same question. The + transcript should read `A ROCm check returned more text than this model can + read (N of M tokens). Try a more specific question, or a model with a larger + context.` — and `rocm-cli.log` should still carry the provider's own body + with `exceed_context_size_error` in it. diff --git a/skills/rocm-cli-assistant/SKILL.md b/skills/rocm-cli-assistant/SKILL.md index 799ddfc92f..9fe9340549 100644 --- a/skills/rocm-cli-assistant/SKILL.md +++ b/skills/rocm-cli-assistant/SKILL.md @@ -16,6 +16,11 @@ Use this skill when answering ROCm CLI local assistant questions. - Interpret `running_state=running` as running, `running_state=starting` as starting, `running_state=not_running` as not running, and no matching row as unknown or not managed by ROCm CLI. - Treat `localhost` and `127.0.0.1` as the same loopback endpoint. +## Tool Results + +- A result ending in `[output truncated: ... omitted ...]` is a partial result. Say in your answer that the list is partial and name the command the user can run in a terminal for the rest. Never present a truncated list as the complete one. +- `rocm_command` with `["model"]` lists the recommended models, which is what most model questions need. `["model", "--verbose"]` adds the per-recipe diagnostics and is long; ask for it only when the user wants that detail. + ## Setup And Install - `active_runtime_status=ready` means ROCm CLI has an active managed ROCm/TheRock runtime even if legacy system ROCm is not detected. From ede0719bccf79170aee898e596fb4eac515b42a4 Mon Sep 17 00:00:00 2001 From: Roman Sirokov Date: Wed, 11 Nov 2026 15:24:45 +0200 Subject: [PATCH 2/3] test(e2e): cover the model catalog a tool-using client is given The dash TUI's chat path has no Gherkin coverage, but the change behind it is observable from the CLI: `rocm mcp-call rocm_command` with `["model"]` is the same tool dispatch the Chat tab drives, so the scenarios assert there rather than leaving the behaviour to unit tests alone. One pins the recommendation list and its size, the other that the per-recipe diagnostics are still reachable on request. Signed-off-by: Roman Sirokov --- .../features/model_catalog.feature | 19 +++++ .../tests/e2e/model_catalog_steps.rs | 81 +++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/tests/e2e-cucumber/features/model_catalog.feature b/tests/e2e-cucumber/features/model_catalog.feature index 458dd80d7e..7d355ec8e6 100644 --- a/tests/e2e-cucumber/features/model_catalog.feature +++ b/tests/e2e-cucumber/features/model_catalog.feature @@ -37,3 +37,22 @@ Feature: Model catalog listing Given a recipe catalog whose AIM entry was last verified long ago When the user asks for the model catalog in verbose detail Then the catalog warns that the AIM entry may no longer be current + + # The catalog is also read by tool-using clients — the dashboard's Chat tab + # and any MCP client — and what they were handed was the verbose listing, not + # the recommendation list a person sees. On a developer host that is 189 KB + # against under 1 KB, which is more than a locally served model's whole + # context, so asking the assistant what this machine can run ended with the + # model's provider refusing the request instead of an answer. + @id:model-catalog-tool-client-gets-the-recommendations + Scenario: model-catalog-04 - A tool-using client asking about models gets the recommendations + When a tool-using client asks for the model catalog + Then the client is given the recommended models without the per-recipe diagnostics + And what the client is given is small enough for a locally served model to read + + # The diagnostics are not gone, they are opt-in: a client that wants them + # asks for them, the same way a person types `rocm model --verbose`. + @id:model-catalog-tool-client-can-ask-for-diagnostics + Scenario: model-catalog-05 - A tool-using client can still ask for the full diagnostics + When a tool-using client asks for the model catalog in verbose detail + Then the client is given the per-recipe diagnostics diff --git a/tests/e2e-cucumber/tests/e2e/model_catalog_steps.rs b/tests/e2e-cucumber/tests/e2e/model_catalog_steps.rs index b79c66ff17..03a871c4bc 100644 --- a/tests/e2e-cucumber/tests/e2e/model_catalog_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/model_catalog_steps.rs @@ -16,6 +16,87 @@ async fn user_asks_for_verbose_catalog(world: &mut E2eWorld) { world.cli_rc = Some(rc); } +/// Ask the `rocm_command` tool for the catalog, the way the dashboard's Chat +/// tab and any MCP client do. `mcp-call` is the CLI's own entry to that tool, +/// so the same dispatch the dash drives is exercised without a model or a TUI. +fn ask_the_tool_for_the_catalog(world: &mut E2eWorld, args: &[&str]) { + let arguments = serde_json::json!({ "args": args }).to_string(); + let (stdout, _, rc) = crate::run_rocm( + world, + &["mcp-call", "rocm_command", "--arguments-json", &arguments], + ); + world.cli_output = Some(stdout); + world.cli_rc = Some(rc); +} + +/// The catalog text inside the tool's reply, with the call's success asserted +/// first so a failure reads as the CLI's own rather than as a missing field. +fn catalog_text_from_the_tool(world: &E2eWorld) -> String { + let output = world.cli_output.as_ref().expect("no tool reply"); + assert_eq!(world.cli_rc, Some(0), "the tool call failed:\n{output}"); + let reply: serde_json::Value = serde_json::from_str(output.trim()) + .unwrap_or_else(|error| panic!("the tool reply is not JSON ({error}):\n{output}")); + assert_eq!(reply["isError"], serde_json::json!(false), "{reply}"); + reply["content"][0]["text"] + .as_str() + .unwrap_or_else(|| panic!("the tool reply carries no text:\n{output}")) + .to_owned() +} + +#[when("a tool-using client asks for the model catalog")] +async fn tool_client_asks_for_catalog(world: &mut E2eWorld) { + ask_the_tool_for_the_catalog(world, &["model"]); +} + +#[when("a tool-using client asks for the model catalog in verbose detail")] +async fn tool_client_asks_for_verbose_catalog(world: &mut E2eWorld) { + ask_the_tool_for_the_catalog(world, &["model", "--verbose"]); +} + +#[then("the client is given the recommended models without the per-recipe diagnostics")] +async fn tool_client_gets_the_recommendations(world: &mut E2eWorld) { + let text = catalog_text_from_the_tool(world); + // The same two lines the recommendation listing ends with for the built-in + // catalog, so this pins the curated renderer rather than merely "some + // text with a model id in it". + assert!( + text.contains( + "These are recommendations - you can serve any compatible Hugging Face model" + ) && text.contains("rocm serve "), + "expected the recommendation listing:\n{text}" + ); + // `aliases=[` opens every row of the verbose listing and appears nowhere + // in the curated one. + assert!( + !text.contains("aliases=["), + "the per-recipe diagnostics must not be the default:\n{text}" + ); +} + +#[then("what the client is given is small enough for a locally served model to read")] +async fn tool_catalog_is_small_enough_to_read(world: &mut E2eWorld) { + let text = catalog_text_from_the_tool(world); + // 16 KiB is the dash's per-result budget (`TOOL_RESULT_CHAR_BUDGET` in + // `crates/rocm-dash-tui/src/agent/tools.rs`), chosen to leave room in a + // 32k-token context for the system prompt and several turns. Asserted as + // a literal rather than imported so the e2e suite does not take a + // dependency on a dash internal; the listing measured under 2 KB. + assert!( + text.len() < 16 * 1024, + "the catalog handed to a tool client is {} characters, past the dash's budget:\n{text}", + text.len() + ); +} + +#[then("the client is given the per-recipe diagnostics")] +async fn tool_client_gets_the_diagnostics(world: &mut E2eWorld) { + let text = catalog_text_from_the_tool(world); + assert!( + text.contains("model recipes") && text.contains("aliases=["), + "expected the verbose per-recipe listing:\n{text}" + ); +} + #[then("the catalog lists an AIM engine recipe")] async fn catalog_lists_aim_recipe(world: &mut E2eWorld) { let output = world.cli_output.as_ref().expect("no model catalog output"); From 74c835dffbbebb009023e74b9697a2c0cf3baec6 Mon Sep 17 00:00:00 2001 From: Roman Sirokov Date: Thu, 12 Nov 2026 13:12:05 +0200 Subject: [PATCH 3/3] fix(dash): keep a truncated tool result a failure when the command failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Capping an oversized read-only tool result replaces the envelope with a fresh object, so anything the chat path reads off it has to be carried over deliberately. `isError` was not, which meant a command that failed with more output than the budget allows reached the model looking like a success — `turn.rs` reads exactly that field to decide. Carry it over when the envelope had one, and leave it absent when it did not, so the shape still mirrors its source. Two tests pin both directions; both fail if the carry is dropped. Signed-off-by: Roman Sirokov --- crates/rocm-dash-tui/src/agent/tools.rs | 77 +++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/crates/rocm-dash-tui/src/agent/tools.rs b/crates/rocm-dash-tui/src/agent/tools.rs index 8bc8ba95d7..97f9784571 100644 --- a/crates/rocm-dash-tui/src/agent/tools.rs +++ b/crates/rocm-dash-tui/src/agent/tools.rs @@ -366,6 +366,12 @@ fn cap_tool_result(value: Value, budget: usize) -> Value { if value.to_string().len() <= budget { return value; } + // The truncated form is a fresh object, so whatever the rest of the chat + // path reads off the envelope has to be carried over deliberately. + // `isError` is the success/failure signal (`turn.rs` reads it), and a + // command that failed must not read as a success merely because its + // output was too long to keep. + let is_error = value.get("isError").and_then(Value::as_bool); let full = tool_result_plain_text(&value); let total_chars = full.chars().count(); // Candidate cuts: the start, then the end of every line, so a cut never @@ -382,7 +388,7 @@ fn cap_tool_result(value: Value, budget: usize) -> Value { let (mut low, mut high, mut best) = (0usize, cuts.len() - 1, 0usize); while low <= high { let mid = low.midpoint(high); - if truncated_tool_result(&full, cuts[mid], total_chars) + if truncated_tool_result(&full, cuts[mid], total_chars, is_error) .to_string() .len() <= budget @@ -395,17 +401,28 @@ fn cap_tool_result(value: Value, budget: usize) -> Value { high = mid - 1; } } - truncated_tool_result(&full, cuts[best], total_chars) + truncated_tool_result(&full, cuts[best], total_chars, is_error) } -/// The truncated form of `full` kept up to the byte offset `cut`. -fn truncated_tool_result(full: &str, cut: usize, total_chars: usize) -> Value { +/// The truncated form of `full` kept up to the byte offset `cut`. `is_error` +/// is the envelope's `isError`, carried over when it had one so truncating a +/// failed command's output does not turn it into a success. +fn truncated_tool_result( + full: &str, + cut: usize, + total_chars: usize, + is_error: Option, +) -> Value { let kept = &full[..cut]; let shown_chars = kept.chars().count(); - json!({ + let mut truncated = json!({ "truncated": true, "tool_output": format!("{kept}{}", truncation_marker(shown_chars, total_chars)), - }) + }); + if let Some(is_error) = is_error { + truncated["isError"] = json!(is_error); + } + truncated } /// Shared body for every read-only ROCm tool: forward the intent to the injected @@ -1190,6 +1207,54 @@ mod tests { ); } + #[tokio::test] + async fn truncating_a_failed_command_keeps_it_a_failure() { + // The capped form is a fresh object, so `isError` survives only if it + // is carried over. A command that failed with a lot of output would + // otherwise reach the model looking like a success, and the chat path + // reads that field to decide exactly that. + let (_, mut envelope) = command_envelope(200 * 1024); + envelope["isError"] = json!(true); + envelope["structuredContent"]["exit_status"] = json!(1); + let tool = RocmCommandRocmTool { + executor: Some(Arc::new(FakeExec(envelope)) as SharedRocmToolExecutor), + fired: Arc::new(Mutex::new(Vec::new())), + }; + + let out = tool.call(json!({ "args": ["model"] })).await.expect("ok"); + + assert_eq!( + out["truncated"], + json!(true), + "the fixture must truncate: {out}" + ); + assert_eq!( + out["isError"], + json!(true), + "a failed command must still read as failed after truncation: {out}" + ); + } + + #[tokio::test] + async fn truncating_a_successful_command_keeps_it_a_success() { + // The mirror of the case above: carrying `isError` over must not + // invent a failure for output that was merely long. + let (_, envelope) = command_envelope(200 * 1024); + let tool = RocmCommandRocmTool { + executor: Some(Arc::new(FakeExec(envelope)) as SharedRocmToolExecutor), + fired: Arc::new(Mutex::new(Vec::new())), + }; + + let out = tool.call(json!({ "args": ["model"] })).await.expect("ok"); + + assert_eq!( + out["truncated"], + json!(true), + "the fixture must truncate: {out}" + ); + assert_eq!(out["isError"], json!(false), "{out}"); + } + #[tokio::test] async fn a_command_backed_result_reaches_the_model_once_not_twice() { // The bin's envelope carries the command output twice — once as the