Avoid starting dormant REPL servers for cleanup hooks (#61437)

## Why

Bundled `turn_ended` hooks can start an unused REPL server solely to clean it up, even though its deferred startup means there is no process to clean up.

## What changed

Allow bundled cleanup calls for `node_repl` and `cua_repl` to return an empty successful result when startup has never been requested. Limit this behavior to configured, unshared local stdio connections using the legacy MCP protocol. Other calls continue to wait for server startup.

## Testing

Add coverage for the connection eligibility checks and extend subagent hook tests to verify that bundled cleanup leaves unused REPL servers dormant while ordinary hooks can still start cached tools hidden from the model.

GitOrigin-RevId: 0a07772fccd670c53348ff7a702b4bee353e263c
This commit is contained in:
dob-oai
2026-11-14 01:34:12 +00:00
committed by copyberry
parent 9ac8c3f8d7
commit 8afe928a76
12 changed files with 214 additions and 27 deletions
+4
View File
@@ -4,6 +4,7 @@ use std::time::Instant;
use anyhow::Context;
use anyhow::Result;
use codex_protocol::mcp::is_node_repl_backed_server;
use codex_protocol::models::ContentItemMetadata;
use regex::Regex;
use serde_json::Map;
@@ -73,6 +74,9 @@ pub(crate) async fn run_mcp_tool(
}
executor
.execute(HookMcpCall {
skip_dormant_repl_cleanup: handler.builtin
&& is_node_repl_backed_server(server)
&& tool == "turn_ended",
server: server.to_string(),
tool: tool.to_string(),
environment_id,
@@ -134,6 +134,7 @@ async fn mcp_tool_results_use_command_hook_output_contract() {
assert_eq!(
*calls.lock().expect("lock calls"),
vec![HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "scan".to_string(),
environment_id: None,
+8
View File
@@ -2425,6 +2425,7 @@ fn executor_stop_hook_fixture() -> (
};
let expected_executor_call = HookMcpCall {
skip_dormant_repl_cleanup: true,
server: "node_repl".to_string(),
tool: "turn_ended".to_string(),
environment_id: Some("executor-a".to_string()),
@@ -2560,6 +2561,7 @@ async fn executor_stop_hooks_run_unless_regular_hooks_block_without_stopping() {
vec![
expected_executor_call.clone(),
HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "check".to_string(),
environment_id: None,
@@ -2598,6 +2600,7 @@ async fn executor_stop_hooks_run_unless_regular_hooks_block_without_stopping() {
vec![
expected_executor_call.clone(),
HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "check".to_string(),
environment_id: None,
@@ -2606,6 +2609,7 @@ async fn executor_stop_hooks_run_unless_regular_hooks_block_without_stopping() {
timeout: Duration::from_secs(30),
},
HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "check".to_string(),
environment_id: None,
@@ -2614,6 +2618,7 @@ async fn executor_stop_hooks_run_unless_regular_hooks_block_without_stopping() {
timeout: Duration::from_secs(30),
},
HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "terminate".to_string(),
environment_id: None,
@@ -2747,6 +2752,7 @@ async fn memory_consolidation_stop_preserves_policy_and_executor_cleanup() {
executor_stop_hook_fixture();
request.target = StopHookTarget::MemoryConsolidation;
let policy_call = HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "check".to_string(),
environment_id: None,
@@ -2913,6 +2919,7 @@ async fn mcp_tool_hooks_expand_event_input_and_apply_pre_tool_decisions() {
assert_eq!(
*calls.lock().expect("lock MCP calls"),
vec![HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "scan".to_string(),
environment_id: None,
@@ -3015,6 +3022,7 @@ async fn mcp_interrupt_hooks_expand_event_input_and_bound_timeout() {
assert_eq!(
*calls.lock().expect("lock MCP calls"),
vec![HookMcpCall {
skip_dormant_repl_cleanup: false,
server: "security".to_string(),
tool: "notify".to_string(),
environment_id: None,