Support cua_repl as a Node REPL-backed MCP server (#40257)

## What changed

- Recognize `cua_repl` alongside `node_repl` when collecting Guardian review evidence, applying computer-use policy, and capturing transcript images.
- Render `cua_repl.js` results with the compact REPL history and transcript views.
- Preserve failed MCP call status and result content when replaying app-server history.

## Testing

- Cover both REPL server names across Guardian policy, evidence, image capture, history rendering, and failed-call replay tests.

GitOrigin-RevId: 8c13a721f718add403b07e727c1634b59203c51a
This commit is contained in:
Ankush Gupta authored and copyberry committed 2026-08-23 17:02:21 +00:00
1 parent c9b19deb09
commit 2161ec272a
16 files changed
+260 -82

No files matched your search

@@ -1187,6 +1187,70 @@ async fn replayed_in_progress_mcp_tool_call_stays_active() {
assert!(!active.contains("MCP tool call completed without a result"));
}
#[tokio::test]
async fn failed_repl_mcp_tool_call_preserves_status_and_result() {
for server in ["node_repl", "cua_repl"] {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let _ = drain_insert_history(&mut rx);
chat.handle_server_notification(
ServerNotification::ItemCompleted(ItemCompletedNotification {
thread_id: "thread-1".to_string(),
turn_id: "turn-1".to_string(),
completed_at_ms: 0,
item: AppServerThreadItem::McpToolCall {
id: "mcp-failed".to_string(),
server: server.to_string(),
tool: "js".to_string(),
status: codex_app_server_protocol::McpToolCallStatus::Failed,
arguments: json!({"title": "Inspect workspace"}),
app_context: None,
mcp_app_resource_uri: None,
plugin_id: None,
read_only_hint: None,
result: Some(Box::new(codex_app_server_protocol::McpToolCallResult {
content: vec![
json!({"type": "text", "text": "Script failed"}),
json!({"type": "text", "text": r#"{"exit_code":0,"output":"ready","chunk_id":"chunk-1"}"#}),
json!({"type": "text", "text": "Script error:\npermission denied"}),
],
structured_content: None,
meta: None,
})),
error: None,
duration_ms: Some(5),
},
}),
/*replay_kind*/ None,
);
let cells = drain_insert_history(&mut rx);
let [lines] = cells.as_slice() else {
panic!("expected one completed MCP tool call for {server}");
};
insta::allow_duplicates! {
insta::assert_snapshot!(lines_to_single_string(lines), @r#"
• Called Inspect workspace
└ Script failed
{"exit_code": 0, "output": "ready", "chunk_id": "chunk-1"}
Script error:
permission denied
"#);
}
assert_eq!(
lines.first(),
Some(&Line::from(vec![
"•".red().bold(),
" ".into(),
"Called".bold(),
" ".into(),
"Inspect workspace".cyan(),
])),
"{server}",
);
}
}
#[tokio::test]
async fn deferred_mcp_lifecycle_events_keep_fifo_after_stream_finishes() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
@@ -197,6 +197,7 @@ impl ChatWidget {
id,
server,
tool,
status,
arguments,
result,
error,
@@ -219,7 +220,7 @@ impl ChatWidget {
Ok(codex_protocol::mcp::CallToolResult {
content: result.content,
structured_content: result.structured_content,
is_error: Some(false),
is_error: Some(status == codex_app_server_protocol::McpToolCallStatus::Failed),
meta: None,
})
}
+4 -2
View File
@@ -2,6 +2,8 @@
use super::*;
use codex_protocol::mcp::is_node_repl_backed_server;
#[path = "mcp_result.rs"]
mod result;
@@ -119,7 +121,7 @@ impl McpToolCallCell {
}
fn result_kind(&self) -> McpResultKind {
if self.invocation.server == "node_repl" && self.invocation.tool == "js" {
if is_node_repl_backed_server(&self.invocation.server) && self.invocation.tool == "js" {
McpResultKind::NodeRepl
} else {
McpResultKind::Standard
@@ -157,7 +159,7 @@ impl McpToolCallCell {
.map(|title| title.split_whitespace().collect::<Vec<_>>().join(" "))
.filter(|title| !title.is_empty())
.map(|title| title.graphemes(true).take(80).collect::<String>())
.unwrap_or_else(|| "node_repl.js".to_string());
.unwrap_or_else(|| format!("{}.{}", self.invocation.server, self.invocation.tool));
Line::from(title.cyan())
} else {
line_to_static(&format_mcp_invocation(&self.invocation))
+5 -3
View File
@@ -109,10 +109,12 @@ impl McpToolResult {
}
impl McpContentBlock {
/// Returns the untruncated top-level text used by node_repl's compact and transcript views.
/// Returns the untruncated top-level text used by node_repl and cua_repl's compact and
/// transcript views.
///
/// Valid text blocks reuse their display storage. Only node_repl retains this extra field on
/// malformed or non-text blocks, where it takes precedence over the usual display content.
/// Valid text blocks reuse their display storage. Only node_repl and cua_repl retain this extra
/// field on malformed or non-text blocks, where it takes precedence over the usual display
/// content.
pub(super) fn text(&self) -> Option<&str> {
match &self.display {
McpContentDisplay::Text(text) => Some(text),
+43 -7
View File
@@ -207,13 +207,11 @@ fn code_mode_preserves_text_fields_on_nontext_and_unknown_blocks() {
);
let unknown =
json!({"type": "future_block", "text": "Script completed\nOutput:\nunknown-side output"});
cell.complete(
Duration::ZERO,
Ok(result(vec![
json!({"type": "image", "mimeType": "image/png", "data": PNG, "text": "Script completed\nOutput:\nimage-side output"}),
unknown.clone(),
])),
);
let tool_result = result(vec![
json!({"type": "image", "mimeType": "image/png", "data": PNG, "text": "Script completed\nOutput:\nimage-side output"}),
unknown.clone(),
]);
cell.complete(Duration::ZERO, Ok(tool_result.clone()));
let display = cell
.display_lines(/*width*/ 200)
@@ -254,4 +252,42 @@ fn code_mode_preserves_text_fields_on_nontext_and_unknown_blocks() {
)),
],
);
let mut cua_cell = new_active_mcp_tool_call(
"call-cua-repl".to_string(),
McpInvocation {
server: "cua_repl".to_string(),
tool: "js".to_string(),
arguments: None,
},
/*animations_enabled*/ false,
);
cua_cell.complete(Duration::ZERO, Ok(tool_result));
let display = cua_cell
.display_lines(/*width*/ 200)
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n");
let transcript = cua_cell
.transcript_lines(/*width*/ 200)
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n");
insta::assert_snapshot!(format!("history:\n{display}\n\ntranscript:\n{transcript}"), @r"
history:
• Called cua_repl.js
└ image-side output
unknown-side output
transcript:
• Called cua_repl.js()
└ Script completed
Output:
image-side output
Script completed
Output:
unknown-side output
");
}