fix: harden provider, tool, and desktop behavior

This commit is contained in:
leokun committed 2026-08-24 15:37:42 +08:00
1 parent 4bb4ca5c8d
commit 7fa4953883
10 files changed
+376 -51

No files matched your search

+10 -1
View File
@@ -7,7 +7,7 @@ use crate::{
Error, Result,
};
use super::{mcp_state, ReadImage, ToolCompletion};
use super::{gate, mcp_state, ReadImage, ToolCompletion};
use crate::cursor::tools::{
edit,
runtime::{ExecStage, PendingExec},
@@ -18,6 +18,15 @@ pub(crate) fn from_exec(
wire_result: &pb::exec_client_message::Message,
) -> Result<ToolCompletion> {
use pb::{exec_client_message::Message, tool_call::Tool};
let mut gated_shell = matches!(
wire_result,
Message::ShellResult(_) | Message::MiniSweAgentBashResult(_)
)
.then(|| wire_result.clone());
if let Some(message) = gated_shell.as_mut() {
gate::exec_message(message);
}
let wire_result = gated_shell.as_ref().unwrap_or(wire_result);
if let Message::McpStateExecResult(result) = wire_result {
return mcp_state::complete(pending, result);
}
+169
View File
@@ -0,0 +1,169 @@
use crate::cursor::proto::agent::v1 as pb;
const KIB: usize = 1024;
const SHELL_STREAM_LIMIT: usize = 16 * KIB;
const SHELL_CONTENT_LIMIT: usize = 32 * KIB;
pub(super) fn model_content(tool: &pb::tool_call::Tool, content: &mut String) {
if matches!(tool, pb::tool_call::Tool::ShellToolCall(_)) {
*content = truncate_edges("Shell", content, SHELL_CONTENT_LIMIT);
}
}
pub(super) fn exec_message(message: &mut pb::exec_client_message::Message) {
use pb::exec_client_message::Message;
match message {
Message::ShellResult(result) | Message::MiniSweAgentBashResult(result) => {
gate_shell_result(result)
}
_ => {}
}
}
fn gate_shell_result(result: &mut pb::ShellResult) {
use pb::shell_result::Result;
match result.result.as_mut() {
Some(Result::Success(success)) => {
success.stdout = truncate_edges("Shell stdout", &success.stdout, SHELL_STREAM_LIMIT);
success.stderr = truncate_edges("Shell stderr", &success.stderr, SHELL_STREAM_LIMIT);
if let Some(interleaved) = success.interleaved_output.as_mut() {
*interleaved =
truncate_edges("Shell interleaved output", interleaved, SHELL_CONTENT_LIMIT);
}
}
Some(Result::Failure(failure)) => {
failure.stdout = truncate_edges("Shell stdout", &failure.stdout, SHELL_STREAM_LIMIT);
failure.stderr = truncate_edges("Shell stderr", &failure.stderr, SHELL_STREAM_LIMIT);
if let Some(interleaved) = failure.interleaved_output.as_mut() {
*interleaved =
truncate_edges("Shell interleaved output", interleaved, SHELL_CONTENT_LIMIT);
}
}
_ => {}
}
}
fn truncate_edges(tool_name: &str, content: &str, limit: usize) -> String {
if content.len() <= limit {
return content.to_string();
}
let original = content.len();
let mut shown = limit;
loop {
let notice = format!(
"\n\n[truncated: {tool_name} result exceeded {limit} bytes; omitted middle; showing {shown} of {original} bytes]\n\n"
);
let available = limit.saturating_sub(notice.len());
let head = utf8_prefix(content, available / 2);
let tail = utf8_suffix(content, available.saturating_sub(head.len()));
let next_shown = head.len().saturating_add(tail.len());
if next_shown == shown {
return format!("{head}{notice}{tail}");
}
shown = next_shown;
}
}
fn utf8_prefix(value: &str, limit: usize) -> &str {
let mut end = limit.min(value.len());
while end > 0 && !value.is_char_boundary(end) {
end -= 1;
}
&value[..end]
}
fn utf8_suffix(value: &str, limit: usize) -> &str {
let mut start = value.len().saturating_sub(limit);
while start < value.len() && !value.is_char_boundary(start) {
start += 1;
}
&value[start..]
}
#[cfg(test)]
mod tests {
use super::*;
fn shell_tool() -> pb::tool_call::Tool {
pb::tool_call::Tool::ShellToolCall(pb::ShellToolCall::default())
}
#[test]
fn shell_output_keeps_both_ends_within_its_budget() {
let mut content = format!("HEAD{}TAIL", " ".repeat(1024 * KIB));
model_content(&shell_tool(), &mut content);
assert!(content.len() <= SHELL_CONTENT_LIMIT);
assert!(content.starts_with("HEAD"));
assert!(content.ends_with("TAIL"));
assert!(content.contains("omitted middle"));
}
#[test]
fn non_shell_output_is_unchanged() {
let mut content = "x".repeat(64 * KIB);
let original = content.clone();
model_content(
&pb::tool_call::Tool::ReadToolCall(pb::ReadToolCall::default()),
&mut content,
);
assert_eq!(content, original);
}
#[test]
fn shell_streams_are_limited_before_rendering() {
let mut message = pb::exec_client_message::Message::ShellResult(pb::ShellResult {
result: Some(pb::shell_result::Result::Success(pb::ShellSuccess {
stdout: format!("HEAD{}TAIL", "x".repeat(64 * KIB)),
stderr: format!("ERROR_HEAD{}ERROR_TAIL", "y".repeat(64 * KIB)),
interleaved_output: Some(format!("START{}END", "z".repeat(64 * KIB))),
..Default::default()
})),
..Default::default()
});
exec_message(&mut message);
let pb::exec_client_message::Message::ShellResult(result) = message else {
panic!("expected Shell result");
};
let Some(pb::shell_result::Result::Success(success)) = result.result else {
panic!("expected Shell success");
};
assert!(success.stdout.len() <= SHELL_STREAM_LIMIT);
assert!(success.stdout.starts_with("HEAD"));
assert!(success.stdout.ends_with("TAIL"));
assert!(success.stderr.len() <= SHELL_STREAM_LIMIT);
assert!(success.stderr.starts_with("ERROR_HEAD"));
assert!(success.stderr.ends_with("ERROR_TAIL"));
assert!(success.interleaved_output.unwrap().len() <= SHELL_CONTENT_LIMIT);
}
#[test]
fn failed_shell_streams_are_limited() {
let mut message = pb::exec_client_message::Message::ShellResult(pb::ShellResult {
result: Some(pb::shell_result::Result::Failure(pb::ShellFailure {
stdout: "x".repeat(64 * KIB),
stderr: "y".repeat(64 * KIB),
interleaved_output: Some("z".repeat(64 * KIB)),
..Default::default()
})),
..Default::default()
});
exec_message(&mut message);
let pb::exec_client_message::Message::ShellResult(result) = message else {
panic!("expected Shell result");
};
let Some(pb::shell_result::Result::Failure(failure)) = result.result else {
panic!("expected Shell failure");
};
assert!(failure.stdout.len() <= SHELL_STREAM_LIMIT);
assert!(failure.stderr.len() <= SHELL_STREAM_LIMIT);
assert!(failure.interleaved_output.unwrap().len() <= SHELL_CONTENT_LIMIT);
}
}
+3 -1
View File
@@ -1,5 +1,6 @@
mod await_shell;
mod exec;
mod gate;
mod interaction;
mod local;
mod mcp;
@@ -87,9 +88,10 @@ impl ToolCompletion {
pub(crate) fn new(
call: &ToolCall,
started_at_ms: u64,
result: ToolResult,
mut result: ToolResult,
tool: pb::tool_call::Tool,
) -> Self {
gate::model_content(&tool, &mut result.content);
Self {
result,
tool_call: pb::ToolCall {