Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/cua-driver/rust/crates/mcp-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod protocol;
pub mod recording;
pub mod recording_tools;
pub mod server;
pub mod text_sanitize;
pub mod tool;
pub mod tool_args;

Expand Down
250 changes: 250 additions & 0 deletions libs/cua-driver/rust/crates/mcp-server/src/text_sanitize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
//! Defensive sanitization for `text` payloads passed to `type_text`.
//!
//! When an LLM agent calls a tool with a long multi-line `text` parameter
//! it occasionally hallucinates its own tool-protocol closing tags into
//! the trailing whitespace. Anthropic's tool-use format uses tags such
//! as `[/text]`, `[/invoke]`, `[/parameter]`, `[/function_calls]` (shown
//! here with brackets so this module itself doesn't recursively trip
//! the sanitizer). The model expects those next in its output buffer
//! and lets a few leak past the parameter boundary. The tool-invocation
//! transport parses the call correctly (it strips these at the protocol
//! layer) but the value of the `text` field still contains them. They
//! get faithfully delivered to the target window. The target then has
//! e.g. `[/text]` and `[/invoke]` typed at the end of its document, and
//! the agent often can't recover (Backspace is blocked on VCL targets
//! under background dispatch, the agent can't see the trailing content
//! without re-snapshotting, etc.).
//!
//! This sanitizer strips a TRAILING run of recognized agent-protocol
//! closing tags before the text leaves the tool layer. Three guardrails
//! against false positives:
//!
//! 1. Only tags whose names are in our known set are eligible.
//! Generic HTML closing tags (`[/div]`, `[/span]`) are left alone.
//! 2. Only tags appearing at the very end of the string (after
//! trimming trailing whitespace) are eligible.
//! 3. Only UNBALANCED tags are stripped. If an opening `[text]` (or
//! `[parameter ...]` etc.) appears earlier in the text, the
//! closing is treated as legitimate document content.
//!
//! Emits a `tracing::warn` when sanitization fires so daemon operators
//! can see how often agents stumble into this footgun.

use std::borrow::Cow;

/// Known agent-protocol tag names. These appear in Anthropic's tool-use
/// format (`invoke`, `parameter`, `function_calls`, `text`), in some
/// alternate vendors' formats (`tool_use`, `tool_call`, `function_call`),
/// and in the antml-namespaced variant Claude Code uses for nested
/// invocations.
const PROTOCOL_TAG_NAMES: &[&str] = &[
"text",
"parameter",
"invoke",
"function_calls",
"function_call",
"tool_use",
"tool_call",
"antml:parameter",
"antml:invoke",
"antml:function_calls",
];

/// Strip a trailing run of agent-protocol closing tags from `text`.
///
/// Returns `Cow::Borrowed(text)` when no sanitization was needed (the
/// common case — fast path). Returns `Cow::Owned(stripped)` when the
/// trailing tail matched and a logged warning was emitted.
///
/// See module docs for the false-positive guardrails. The function is
/// pure and allocation-free in the no-match path.
pub fn strip_trailing_agent_protocol_tags(text: &str) -> Cow<'_, str> {
let trimmed = text.trim_end();
if trimmed.is_empty() {
return Cow::Borrowed(text);
}

// Walk the tail right-to-left collecting closing tags. We accept
// `</NAME>` patterns where NAME is in the known set. Any text that
// isn't whitespace AND isn't one of those tags terminates the walk.
let mut cursor = trimmed.len();
let mut stripped_any = false;
let mut stripped_names: Vec<&str> = Vec::new();
loop {
// Skip whitespace between tags.
let head = &trimmed[..cursor];
let trimmed_head = head.trim_end_matches(|c: char| c.is_whitespace());
cursor = trimmed_head.len();
if cursor == 0 {
break;
}
// Must end with `>`.
if !trimmed_head.ends_with('>') {
break;
}
// Find the matching `</` opener.
let open = match trimmed_head.rfind("</") {
Some(i) => i,
None => break,
};
// Extract the tag name.
let inner = &trimmed_head[open + 2..trimmed_head.len() - 1];
// No whitespace / attrs allowed in a closing tag.
if inner.is_empty() || inner.chars().any(char::is_whitespace) {
break;
}
if !PROTOCOL_TAG_NAMES.iter().any(|name| inner.eq_ignore_ascii_case(name)) {
break;
}
// Balance check: is there an opening `<NAME` (case-insensitive,
// optionally followed by `>` or whitespace) earlier in the head?
// If so, treat the closing as legitimate document content and stop.
let preceding = &trimmed_head[..open];
if has_matching_opener(preceding, inner) {
break;
}
Comment on lines +99 to +105

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Balance guard can miss orphaned duplicate trailing closers.

If the body contains one legitimate pair and then an extra trailing closer (e.g., <text>x</text></text>), Line 103 sees an opener and stops, so the orphaned tail is not stripped.

Suggested fix direction
-        if has_matching_opener(preceding, inner) {
+        if has_unclosed_opener(preceding, inner) {
             break;
         }

Implement has_unclosed_opener as a count-aware check for the same tag name in preceding (open_count > close_count), and add a regression test for <text>ok</text></text> expecting "<text>ok</text>".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libs/cua-driver/rust/crates/mcp-server/src/text_sanitize.rs` around lines 99
- 105, The current balance check using has_matching_opener can stop too early
when there is a legitimate opener/closer pair followed by an orphaned trailing
closer; modify the logic around trimmed_head, open and inner to call a new
has_unclosed_opener(preceding, inner) that counts occurrences of the same tag
name (opening vs closing) in preceding and returns true only if open_count >
close_count (i.e., there is an unmatched opener), replace the
has_matching_opener call with this count-aware check, and add a regression test
asserting that input "<text>ok</text></text>" becomes "<text>ok</text>" to
ensure the orphaned trailing closer is stripped.

// Strip the closing tag. Continue walking; the agent may have
// emitted multiple in a row (`[/text][/invoke]`).
stripped_any = true;
stripped_names.push("</tag>"); // logged generically; specific name in trace below
let _ = stripped_names; // suppress unused warning if log macro short-circuits
cursor = open;
// Don't actually shift `trimmed_head` here — next iteration
// re-slices `trimmed[..cursor]`.
}

if !stripped_any {
return Cow::Borrowed(text);
}

let kept = &trimmed[..cursor];
let kept_trimmed = kept.trim_end();
tracing::warn!(
target: "cua-driver::sanitize",
"type_text: stripped {} byte(s) of trailing agent-protocol closing tags from text payload. \
Likely an LLM hallucinating its own tool-invocation tags into the parameter content. \
Original length: {}, kept: {}.",
text.len() - kept_trimmed.len(),
text.len(),
kept_trimmed.len(),
);
Cow::Owned(kept_trimmed.to_owned())
}

/// Returns true iff `prelude` contains an opening tag matching `name`
/// (case-insensitive, with or without attributes). Used by the
/// balance-check guardrail so legitimately-balanced document content
/// like `[text]foo[/text]` is left alone.
fn has_matching_opener(prelude: &str, name: &str) -> bool {
// Look for `<NAME>` or `<NAME ` (attributes / namespaces follow with
// whitespace). Case-insensitive match.
let needle_lower = format!("<{}", name.to_ascii_lowercase());
let prelude_lower = prelude.to_ascii_lowercase();
let mut from = 0usize;
while let Some(idx) = prelude_lower[from..].find(&needle_lower) {
let abs = from + idx;
let after = abs + needle_lower.len();
let next = prelude_lower.as_bytes().get(after).copied();
// Valid opener: next char is `>`, whitespace, or end-of-string.
match next {
None => return true,
Some(b'>') => return true,
Some(b) if (b as char).is_whitespace() => return true,
_ => {
from = after;
continue;
}
}
}
false
}



#[cfg(test)]
mod tests {
use super::*;

// Test inputs use `{C}` / `{I}` / `{P}` / `{F}` placeholders, replaced
// at runtime with the real closing-tag strings. This lets us write
// the test source without typing the tags literally — useful if your
// editor / tool chain is itself sensitive to those sequences.
fn substitute(s: &str) -> String {
s.replace("{C}", "\x3c/text\x3e")
.replace("{I}", "\x3c/invoke\x3e")
.replace("{P}", "\x3c/parameter\x3e")
.replace("{F}", "\x3c/function_calls\x3e")
.replace("{aP}", "\x3c/antml:parameter\x3e")
.replace("{aI}", "\x3c/antml:invoke\x3e")
.replace("{aF}", "\x3c/antml:function_calls\x3e")
.replace("{Ot}", "\x3ctext\x3e")
.replace("{Op}", "\x3cparameter name=\"foo\"\x3e")
}

#[test]
fn strips_trailing_text_invoke_tail() {
let bad = substitute("End of document.\n{C}\n{I}");
let out = strip_trailing_agent_protocol_tags(&bad);
assert!(matches!(out, Cow::Owned(_)));
assert_eq!(out, "End of document.");
}

#[test]
fn strips_with_antml_namespace() {
let bad = substitute("Some body.\n{aP}\n{aI}\n{aF}");
let out = strip_trailing_agent_protocol_tags(&bad);
assert!(matches!(out, Cow::Owned(_)));
assert_eq!(out, "Some body.");
}

#[test]
fn leaves_balanced_text_alone() {
// User legitimately typed `<text>foo</text>` as document content.
// Balance check sees the opener and stops.
let ok = substitute("{Ot}foo{C}");
let out = strip_trailing_agent_protocol_tags(&ok);
assert!(matches!(out, Cow::Borrowed(_)));
assert_eq!(out, ok);
}

#[test]
fn leaves_balanced_parameter_alone() {
let ok = substitute("{Op}body{P}");
let out = strip_trailing_agent_protocol_tags(&ok);
assert!(matches!(out, Cow::Borrowed(_)));
assert_eq!(out, ok);
}

#[test]
fn leaves_unrelated_html_alone() {
// Generic HTML closing tag — not in the protocol set.
let ok = "Hello world </div>";
let out = strip_trailing_agent_protocol_tags(ok);
assert!(matches!(out, Cow::Borrowed(_)));
assert_eq!(out, ok);
}

#[test]
fn leaves_plain_text_alone() {
let ok = "Lorem ipsum dolor sit amet.";
let out = strip_trailing_agent_protocol_tags(ok);
assert!(matches!(out, Cow::Borrowed(_)));
assert_eq!(out, ok);
}

#[test]
fn strips_only_trailing_protocol_tags_keeps_inner_html() {
// Inner `<b>...</b>` is legitimate document content; only the
// trailing protocol tags should be stripped.
let bad = substitute("Hello <b>world</b>\n{C}");
let out = strip_trailing_agent_protocol_tags(&bad);
assert_eq!(out, "Hello <b>world</b>");
}

#[test]
fn empty_input_is_borrowed() {
let out = strip_trailing_agent_protocol_tags("");
assert!(matches!(out, Cow::Borrowed(_)));
assert_eq!(out, "");
}
}
12 changes: 10 additions & 2 deletions libs/cua-driver/rust/crates/platform-linux/src/tools/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,11 @@ impl Tool for TypeTextTool {
async fn invoke(&self, args: Value) -> ToolResult {
use mcp_server::tool_args::ArgsExt;
let pid = args.u64_or("pid", 0) as u32;
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Strip trailing agent-protocol closing tags — see
// mcp_server::text_sanitize docs for rationale.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let xid_opt = args.opt_u64("window_id");

// Resolve XID: use window_id if given, else first window for pid.
Expand Down Expand Up @@ -1983,7 +1987,11 @@ impl Tool for TypeTextCharsTool {
async fn invoke(&self, args: Value) -> ToolResult {
use mcp_server::tool_args::ArgsExt;
let pid = args.u64_or("pid", 0) as u32;
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Same trailing-protocol-tag scrub as TypeTextTool — see
// mcp_server::text_sanitize for rationale.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let delay_ms = args.u64_or("delay_ms", 30);
let xid_opt = args.opt_u64("window_id");
let xid = match xid_opt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ impl Tool for TypeTextTool {
async fn invoke(&self, args: Value) -> ToolResult {
use mcp_server::tool_args::ArgsExt;
let pid = match args.require_i32("pid") { Ok(v) => v, Err(e) => return e };
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Strip trailing agent-protocol closing tags — see
// mcp_server::text_sanitize docs for rationale.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let element_index = args.opt_u64("element_index").map(|v| v as usize);
let window_id = args.opt_u64("window_id").map(|v| v as u32);
let delay_ms = args.u64_or("delay_ms", 30);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ impl Tool for TypeTextCharsTool {
async fn invoke(&self, args: Value) -> ToolResult {
use mcp_server::tool_args::ArgsExt;
let pid = match args.require_i32("pid") { Ok(v) => v, Err(e) => return e };
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Same trailing-protocol-tag scrub as TypeTextTool — see
// mcp_server::text_sanitize for rationale.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let delay_ms = args.u64_or("delay_ms", 30);
let element_index = args.opt_u64("element_index").map(|v| v as usize);
let window_id = args.opt_u64("window_id").map(|v| v as u32);
Expand Down
15 changes: 13 additions & 2 deletions libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,14 @@ impl Tool for TypeTextTool {
use crate::input::dispatch::{DispatchMode, EventKind, background_unavailable_error};
let raw_pid = match args.require_i64("pid") { Ok(v) => v, Err(e) => return e };
let pid = raw_pid as u32;
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Strip trailing agent-protocol closing tags before delivery —
// catches the case where an LLM hallucinated its own tool-
// invocation tags into the text param (see text_sanitize docs).
// Returns Cow::Borrowed on the no-match fast path so the common
// case is allocation-free.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let hwnd_opt = args.opt_u64("window_id");
let elem_idx = args.opt_u64("element_index");
let dispatch = DispatchMode::from_args(&args);
Expand Down Expand Up @@ -4576,7 +4583,11 @@ impl Tool for TypeTextCharsTool {
async fn invoke(&self, args: Value) -> ToolResult {
use mcp_server::tool_args::ArgsExt;
let pid = args.u64_or("pid", 0) as u32;
let text = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
let text_raw = match args.require_str("text") { Ok(v) => v, Err(e) => return e };
// Same trailing-protocol-tag scrub as the main TypeTextTool — see
// mcp_server::text_sanitize for rationale.
let text = mcp_server::text_sanitize::strip_trailing_agent_protocol_tags(&text_raw)
.into_owned();
let delay_ms = args.u64_or("delay_ms", 30);
let hwnd_opt = args.opt_u64("window_id");
let hwnd = match hwnd_opt {
Expand Down
Loading