Report blocked code-mode tool frontiers

This commit is contained in:
Danny Zhang
2026-05-11 14:48:52 -07:00
parent d65ed92a5e
commit a0fd0e36f3
2 changed files with 171 additions and 0 deletions
+100
View File
@@ -31,6 +31,7 @@ pub struct ExecuteRequest {
pub stored_values: HashMap<String, JsonValue>,
pub yield_time_ms: Option<u64>,
pub max_output_tokens: Option<usize>,
pub yield_on_blocked_tool_calls: bool,
}
#[derive(Clone, Debug)]
@@ -45,6 +46,7 @@ pub enum RuntimeResponse {
Yielded {
cell_id: String,
content_items: Vec<FunctionCallOutputContentItem>,
pending_tool_call_count: usize,
},
Terminated {
cell_id: String,
@@ -86,6 +88,9 @@ pub(crate) enum RuntimeEvent {
Started,
ContentItem(FunctionCallOutputContentItem),
YieldRequested,
BlockedOnToolCalls {
pending_tool_call_count: usize,
},
ToolCall {
id: String,
name: ToolName,
@@ -239,6 +244,7 @@ fn run_runtime(
}
CompletionState::Pending => {}
}
send_blocked_on_tool_calls_if_needed(scope, &event_tx);
let mut pending_promise = pending_promise;
loop {
@@ -290,6 +296,22 @@ fn run_runtime(
pending_promise = None;
}
}
send_blocked_on_tool_calls_if_needed(scope, &event_tx);
}
}
fn send_blocked_on_tool_calls_if_needed(
scope: &mut v8::PinScope<'_, '_>,
event_tx: &mpsc::UnboundedSender<RuntimeEvent>,
) {
let pending_tool_call_count = scope
.get_slot::<RuntimeState>()
.map(|state| state.pending_tool_calls.len())
.unwrap_or(0);
if pending_tool_call_count > 0 {
let _ = event_tx.send(RuntimeEvent::BlockedOnToolCalls {
pending_tool_call_count,
});
}
}
@@ -322,9 +344,14 @@ mod tests {
use std::collections::HashMap;
use std::time::Duration;
use codex_protocol::ToolName;
use pretty_assertions::assert_eq;
use serde_json::json;
use tokio::sync::mpsc;
use crate::description::CodeModeToolKind;
use crate::description::ToolDefinition;
use super::ExecuteRequest;
use super::RuntimeEvent;
use super::spawn_runtime;
@@ -337,6 +364,25 @@ mod tests {
stored_values: HashMap::new(),
yield_time_ms: Some(1),
max_output_tokens: None,
yield_on_blocked_tool_calls: false,
}
}
fn resolve_city_alias_tool() -> ToolDefinition {
ToolDefinition {
name: "resolve_city_alias".to_string(),
tool_name: ToolName::plain("resolve_city_alias".to_string()),
description: "Resolve a city alias.".to_string(),
kind: CodeModeToolKind::Function,
input_schema: Some(json!({
"type": "object",
"properties": {
"alias": {"type": "string"}
},
"required": ["alias"],
"additionalProperties": false,
})),
output_schema: None,
}
}
@@ -375,4 +421,58 @@ mod tests {
.is_none()
);
}
#[tokio::test]
async fn parallel_tool_calls_emit_blocked_frontier_after_all_calls() {
let (event_tx, mut event_rx) = mpsc::unbounded_channel();
let (runtime_tx, _runtime_terminate_handle) = spawn_runtime(
ExecuteRequest {
enabled_tools: vec![resolve_city_alias_tool()],
source: r#"
await Promise.all([1, 2, 3, 4, 5].map(id =>
tools.resolve_city_alias({ alias: String(id) })
));
"#
.to_string(),
..execute_request("")
},
event_tx,
)
.unwrap();
let mut calls = Vec::new();
loop {
let event = tokio::time::timeout(Duration::from_secs(1), event_rx.recv())
.await
.unwrap()
.unwrap();
match event {
RuntimeEvent::Started => {}
RuntimeEvent::ToolCall { name, input, .. } => {
calls.push((name.display().to_string(), input));
}
RuntimeEvent::BlockedOnToolCalls {
pending_tool_call_count,
} => {
assert_eq!(pending_tool_call_count, 5);
break;
}
other => panic!("unexpected runtime event before blocked frontier: {other:?}"),
}
}
assert_eq!(
calls,
(1..=5)
.map(|index| {
(
"resolve_city_alias".to_string(),
Some(json!({"alias": index.to_string()})),
)
})
.collect::<Vec<_>>()
);
let _ = runtime_tx.send(super::RuntimeCommand::Terminate);
}
}
+71
View File
@@ -106,6 +106,7 @@ impl CodeModeService {
control_rx,
response_tx,
request.yield_time_ms.unwrap_or(DEFAULT_EXEC_YIELD_TIME_MS),
request.yield_on_blocked_tool_calls,
));
response_rx
@@ -290,6 +291,7 @@ async fn run_session_control(
mut control_rx: mpsc::UnboundedReceiver<SessionControlCommand>,
initial_response_tx: oneshot::Sender<RuntimeResponse>,
initial_yield_time_ms: u64,
yield_on_blocked_tool_calls: bool,
) {
let SessionControlContext {
cell_id,
@@ -353,9 +355,24 @@ async fn run_session_control(
let _ = response_tx.send(RuntimeResponse::Yielded {
cell_id: cell_id.clone(),
content_items: std::mem::take(&mut content_items),
pending_tool_call_count: 0,
});
}
}
RuntimeEvent::BlockedOnToolCalls {
pending_tool_call_count,
} => {
if yield_on_blocked_tool_calls {
yield_timer = None;
if let Some(response_tx) = response_tx.take() {
let _ = response_tx.send(RuntimeResponse::Yielded {
cell_id: cell_id.clone(),
content_items: std::mem::take(&mut content_items),
pending_tool_call_count,
});
}
}
}
RuntimeEvent::Notify { call_id, text } => {
let _ = inner.turn_message_tx.send(TurnMessage::Notify {
cell_id: cell_id.clone(),
@@ -454,6 +471,7 @@ async fn run_session_control(
let _ = response_tx.send(RuntimeResponse::Yielded {
cell_id: cell_id.clone(),
content_items: std::mem::take(&mut content_items),
pending_tool_call_count: 0,
});
}
}
@@ -496,6 +514,7 @@ mod tests {
stored_values: HashMap::new(),
yield_time_ms: Some(1),
max_output_tokens: None,
yield_on_blocked_tool_calls: false,
}
}
@@ -712,6 +731,7 @@ text(JSON.stringify(returnsUndefined));
control_rx,
initial_response_tx,
/*initial_yield_time_ms*/ 60_000,
false,
));
event_tx.send(RuntimeEvent::Started).unwrap();
@@ -721,6 +741,7 @@ text(JSON.stringify(returnsUndefined));
RuntimeResponse::Yielded {
cell_id: "cell-1".to_string(),
content_items: Vec::new(),
pending_tool_call_count: 0,
}
);
@@ -750,4 +771,54 @@ text(JSON.stringify(returnsUndefined));
let _ = runtime_tx.send(RuntimeCommand::Terminate);
}
#[tokio::test]
async fn blocked_tool_call_frontier_yields_without_timer() {
let inner = test_inner();
let (event_tx, event_rx) = mpsc::unbounded_channel();
let (_control_tx, control_rx) = mpsc::unbounded_channel();
let (initial_response_tx, initial_response_rx) = oneshot::channel();
let (runtime_event_tx, _runtime_event_rx) = mpsc::unbounded_channel();
let (runtime_tx, runtime_terminate_handle) = spawn_runtime(
ExecuteRequest {
source: "await new Promise(() => {})".to_string(),
yield_time_ms: None,
..execute_request("")
},
runtime_event_tx,
)
.unwrap();
tokio::spawn(run_session_control(
inner,
SessionControlContext {
cell_id: "cell-1".to_string(),
runtime_tx: runtime_tx.clone(),
runtime_terminate_handle,
},
event_rx,
control_rx,
initial_response_tx,
/*initial_yield_time_ms*/ 60_000,
true,
));
event_tx.send(RuntimeEvent::Started).unwrap();
event_tx
.send(RuntimeEvent::BlockedOnToolCalls {
pending_tool_call_count: 5,
})
.unwrap();
assert_eq!(
initial_response_rx.await.unwrap(),
RuntimeResponse::Yielded {
cell_id: "cell-1".to_string(),
content_items: Vec::new(),
pending_tool_call_count: 5,
}
);
let _ = runtime_tx.send(RuntimeCommand::Terminate);
}
}