code-mode: expose create and observe operations

This commit is contained in:
Channing Conger
2026-06-21 07:20:01 +00:00
parent d969450436
commit e81e125cb1
17 changed files with 964 additions and 436 deletions
@@ -1,5 +1,5 @@
use codex_code_mode_protocol::CodeModeToolKind;
use codex_code_mode_protocol::ExecuteRequest;
use codex_code_mode_protocol::CreateCellRequest;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use codex_code_mode_protocol::ImageDetail;
use codex_code_mode_protocol::ToolDefinition;
@@ -10,8 +10,8 @@ use crate::session_runtime::ImageDetail as CellImageDetail;
use crate::session_runtime::OutputItem as CellOutputItem;
use crate::session_runtime::ToolKind as CellToolKind;
pub(super) fn runtime_request(request: CellRequest) -> ExecuteRequest {
ExecuteRequest {
pub(super) fn runtime_request(request: CellRequest) -> CreateCellRequest {
CreateCellRequest {
tool_call_id: request.tool_call_id,
enabled_tools: request
.enabled_tools
@@ -32,8 +32,6 @@ pub(super) fn runtime_request(request: CellRequest) -> ExecuteRequest {
})
.collect(),
source: request.source,
yield_time_ms: None,
max_output_tokens: None,
}
}
+2 -4
View File
@@ -5,7 +5,7 @@ use std::sync::atomic::Ordering;
use std::sync::mpsc as std_mpsc;
use std::time::Duration;
use codex_code_mode_protocol::ExecuteRequest;
use codex_code_mode_protocol::CreateCellRequest;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use pretty_assertions::assert_eq;
use serde_json::Value as JsonValue;
@@ -109,12 +109,10 @@ fn spawn_cell_actor_harness_with_host<H: CellHost>(host: Arc<H>) -> CellActorHar
let (runtime_event_tx, runtime_event_rx) = mpsc::unbounded_channel();
let (runtime_tx, runtime_pause_tx, runtime_terminate_handle) = spawn_runtime(
HashMap::new(),
ExecuteRequest {
CreateCellRequest {
tool_call_id: "call-1".to_string(),
enabled_tools: Vec::new(),
source: "await new Promise(() => {});".to_string(),
yield_time_ms: None,
max_output_tokens: None,
},
runtime_event_tx,
PendingRuntimeMode::PauseUntilResumed,
+5 -7
View File
@@ -10,8 +10,8 @@ use std::sync::mpsc as std_mpsc;
use std::thread;
use codex_code_mode_protocol::CodeModeToolKind;
use codex_code_mode_protocol::CreateCellRequest;
use codex_code_mode_protocol::EnabledToolMetadata;
use codex_code_mode_protocol::ExecuteRequest;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use codex_code_mode_protocol::enabled_tool_metadata;
use codex_protocol::ToolName;
@@ -67,7 +67,7 @@ pub(crate) enum RuntimeEvent {
pub(crate) fn spawn_runtime(
stored_values: HashMap<String, JsonValue>,
request: ExecuteRequest,
request: CreateCellRequest,
event_tx: mpsc::UnboundedSender<RuntimeEvent>,
pending_mode: PendingRuntimeMode,
) -> Result<
@@ -330,7 +330,7 @@ mod tests {
use pretty_assertions::assert_eq;
use tokio::sync::mpsc;
use super::ExecuteRequest;
use super::CreateCellRequest;
use super::PendingRuntimeMode;
use super::RuntimeCommand;
use super::RuntimeControlCommand;
@@ -338,13 +338,11 @@ mod tests {
use super::spawn_runtime;
use crate::FunctionCallOutputContentItem;
fn execute_request(source: &str) -> ExecuteRequest {
ExecuteRequest {
fn execute_request(source: &str) -> CreateCellRequest {
CreateCellRequest {
tool_call_id: "call_1".to_string(),
enabled_tools: Vec::new(),
source: source.to_string(),
yield_time_ms: Some(1),
max_output_tokens: None,
}
}
+43 -86
View File
@@ -2,6 +2,7 @@ use std::sync::Arc;
use std::time::Duration;
use codex_code_mode_protocol::CellId;
use codex_code_mode_protocol::CellOutcome;
use codex_code_mode_protocol::CodeModeNestedToolCall;
use codex_code_mode_protocol::CodeModeSession;
use codex_code_mode_protocol::CodeModeSessionDelegate;
@@ -9,21 +10,17 @@ use codex_code_mode_protocol::CodeModeSessionProvider;
use codex_code_mode_protocol::CodeModeSessionProviderFuture;
use codex_code_mode_protocol::CodeModeSessionResultFuture;
use codex_code_mode_protocol::CodeModeToolKind;
use codex_code_mode_protocol::DEFAULT_EXEC_YIELD_TIME_MS;
use codex_code_mode_protocol::ExecuteRequest;
use codex_code_mode_protocol::ExecuteToPendingOutcome;
use codex_code_mode_protocol::CreateCellRequest;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use codex_code_mode_protocol::ImageDetail;
use codex_code_mode_protocol::NotificationFuture;
use codex_code_mode_protocol::ObserveRequest;
use codex_code_mode_protocol::ObserveToPendingOutcome;
use codex_code_mode_protocol::ObserveToPendingRequest;
use codex_code_mode_protocol::PendingOutcome;
use codex_code_mode_protocol::RuntimeResponse;
use codex_code_mode_protocol::StartedCell;
use codex_code_mode_protocol::ToolInvocationFuture;
use codex_code_mode_protocol::WaitOutcome;
use codex_code_mode_protocol::WaitRequest;
use codex_code_mode_protocol::WaitToPendingOutcome;
use codex_code_mode_protocol::WaitToPendingRequest;
use serde_json::Value as JsonValue;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;
use crate::session_runtime as runtime;
@@ -87,62 +84,24 @@ impl CodeModeService {
}
}
pub async fn execute(&self, request: ExecuteRequest) -> Result<StartedCell, String> {
let yield_time_ms = request.yield_time_ms.unwrap_or(DEFAULT_EXEC_YIELD_TIME_MS);
pub async fn create_cell(&self, request: CreateCellRequest) -> Result<CellId, String> {
let runtime_cell_id = self
.runtime
.create_cell(runtime_request(request))
.await
.map_err(|error| error.to_string())?;
let pending_event = self
.runtime
.begin_observe(
&runtime_cell_id,
runtime::ObserveMode::YieldAfter(Duration::from_millis(yield_time_ms)),
)
.await
.map_err(|error| error.to_string())?;
let cell_id = protocol_cell_id(&runtime_cell_id);
let response_cell_id = cell_id.clone();
let (response_tx, response_rx) = oneshot::channel();
tokio::spawn(async move {
let response = pending_event
.event()
.await
.map_err(|error| error.to_string())
.and_then(|event| runtime_response(&response_cell_id, event));
let _ = response_tx.send(response);
});
Ok(StartedCell::from_result_receiver(cell_id, response_rx))
Ok(protocol_cell_id(&runtime_cell_id))
}
pub async fn execute_to_pending(
pub async fn observe(&self, request: ObserveRequest) -> Result<CellOutcome, String> {
self.begin_observe(request).await.await
}
async fn begin_observe(
&self,
request: ExecuteRequest,
) -> Result<ExecuteToPendingOutcome, String> {
let runtime_cell_id = self
.runtime
.create_cell(runtime_request(request))
.await
.map_err(|error| error.to_string())?;
let cell_id = protocol_cell_id(&runtime_cell_id);
let event = self
.runtime
.observe(&runtime_cell_id, runtime::ObserveMode::PendingFrontier)
.await
.map_err(|error| error.to_string())?;
pending_outcome(&cell_id, event)
}
pub async fn wait(&self, request: WaitRequest) -> Result<WaitOutcome, String> {
self.begin_wait(request).await.await
}
async fn begin_wait(
&self,
request: WaitRequest,
) -> CodeModeSessionResultFuture<'static, WaitOutcome> {
let WaitRequest {
request: ObserveRequest,
) -> CodeModeSessionResultFuture<'static, CellOutcome> {
let ObserveRequest {
cell_id,
yield_time_ms,
} = request;
@@ -157,34 +116,34 @@ impl CodeModeService {
{
Ok(pending_event) => Box::pin(async move {
match pending_event.event().await {
Ok(event) => Ok(WaitOutcome::LiveCell(runtime_response(&cell_id, event)?)),
Ok(event) => Ok(CellOutcome::LiveCell(runtime_response(&cell_id, event)?)),
Err(runtime::Error::MissingCell(_) | runtime::Error::ClosedCell(_)) => {
Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id)))
Ok(CellOutcome::MissingCell(missing_cell_response(cell_id)))
}
Err(error) => Err(error.to_string()),
}
}),
Err(runtime::Error::MissingCell(_) | runtime::Error::ClosedCell(_)) => {
missing_wait(cell_id)
missing_observation(cell_id)
}
Err(error) => Box::pin(async move { Err(error.to_string()) }),
}
}
pub async fn terminate(&self, cell_id: CellId) -> Result<WaitOutcome, String> {
pub async fn terminate(&self, cell_id: CellId) -> Result<CellOutcome, String> {
match self.runtime.terminate(&runtime_cell_id(&cell_id)).await {
Ok(event) => Ok(WaitOutcome::LiveCell(runtime_response(&cell_id, event)?)),
Ok(event) => Ok(CellOutcome::LiveCell(runtime_response(&cell_id, event)?)),
Err(runtime::Error::MissingCell(_) | runtime::Error::ClosedCell(_)) => {
Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id)))
Ok(CellOutcome::MissingCell(missing_cell_response(cell_id)))
}
Err(error) => Err(error.to_string()),
}
}
pub async fn wait_to_pending(
pub async fn observe_to_pending(
&self,
request: WaitToPendingRequest,
) -> Result<WaitToPendingOutcome, String> {
request: ObserveToPendingRequest,
) -> Result<ObserveToPendingOutcome, String> {
let cell_id = request.cell_id;
match self
.runtime
@@ -194,11 +153,11 @@ impl CodeModeService {
)
.await
{
Ok(event) => Ok(WaitToPendingOutcome::LiveCell(pending_outcome(
Ok(event) => Ok(ObserveToPendingOutcome::LiveCell(pending_outcome(
&cell_id, event,
)?)),
Err(runtime::Error::MissingCell(_) | runtime::Error::ClosedCell(_)) => Ok(
WaitToPendingOutcome::MissingCell(missing_cell_response(cell_id)),
ObserveToPendingOutcome::MissingCell(missing_cell_response(cell_id)),
),
Err(error) => Err(error.to_string()),
}
@@ -223,18 +182,21 @@ impl CodeModeSession for CodeModeService {
self.runtime.is_alive()
}
fn execute<'a>(
fn create_cell<'a>(
&'a self,
request: ExecuteRequest,
) -> CodeModeSessionResultFuture<'a, StartedCell> {
Box::pin(CodeModeService::execute(self, request))
request: CreateCellRequest,
) -> CodeModeSessionResultFuture<'a, CellId> {
Box::pin(CodeModeService::create_cell(self, request))
}
fn wait<'a>(&'a self, request: WaitRequest) -> CodeModeSessionResultFuture<'a, WaitOutcome> {
Box::pin(CodeModeService::wait(self, request))
fn observe<'a>(
&'a self,
request: ObserveRequest,
) -> CodeModeSessionResultFuture<'a, CellOutcome> {
Box::pin(CodeModeService::observe(self, request))
}
fn terminate<'a>(&'a self, cell_id: CellId) -> CodeModeSessionResultFuture<'a, WaitOutcome> {
fn terminate<'a>(&'a self, cell_id: CellId) -> CodeModeSessionResultFuture<'a, CellOutcome> {
Box::pin(CodeModeService::terminate(self, cell_id))
}
@@ -295,7 +257,7 @@ impl runtime::SessionRuntimeDelegate for ProtocolDelegate {
}
}
fn runtime_request(request: ExecuteRequest) -> runtime::CreateCellRequest {
fn runtime_request(request: CreateCellRequest) -> runtime::CreateCellRequest {
runtime::CreateCellRequest {
tool_call_id: request.tool_call_id,
enabled_tools: request
@@ -326,22 +288,17 @@ fn protocol_cell_id(cell_id: &runtime::CellId) -> CellId {
CellId::new(cell_id.as_str().to_string())
}
fn pending_outcome(
cell_id: &CellId,
event: runtime::CellEvent,
) -> Result<ExecuteToPendingOutcome, String> {
fn pending_outcome(cell_id: &CellId, event: runtime::CellEvent) -> Result<PendingOutcome, String> {
match event {
runtime::CellEvent::Pending {
content_items,
pending_tool_call_ids,
} => Ok(ExecuteToPendingOutcome::Pending {
} => Ok(PendingOutcome::Pending {
cell_id: cell_id.clone(),
content_items: content_items.into_iter().map(output_item).collect(),
pending_tool_call_ids,
}),
event => Ok(ExecuteToPendingOutcome::Completed(runtime_response(
cell_id, event,
)?)),
event => Ok(PendingOutcome::Completed(runtime_response(cell_id, event)?)),
}
}
@@ -397,8 +354,8 @@ fn missing_cell_response(cell_id: CellId) -> RuntimeResponse {
}
}
fn missing_wait(cell_id: CellId) -> CodeModeSessionResultFuture<'static, WaitOutcome> {
Box::pin(async move { Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id))) })
fn missing_observation(cell_id: CellId) -> CodeModeSessionResultFuture<'static, CellOutcome> {
Box::pin(async move { Ok(CellOutcome::MissingCell(missing_cell_response(cell_id))) })
}
#[cfg(test)]
+125 -98
View File
@@ -178,13 +178,45 @@ fn cell_id(value: &str) -> CellId {
CellId::new(value.to_string())
}
fn execute_request(source: &str) -> ExecuteRequest {
ExecuteRequest {
fn execute_request(source: &str) -> CreateCellRequest {
CreateCellRequest {
tool_call_id: "call-1".to_string(),
enabled_tools: Vec::new(),
source: source.to_string(),
yield_time_ms: Some(1),
max_output_tokens: None,
}
}
async fn execute(service: &CodeModeService, request: CreateCellRequest) -> RuntimeResponse {
execute_with_yield_time(service, request, /*yield_time_ms*/ 1).await
}
async fn execute_with_yield_time(
service: &CodeModeService,
request: CreateCellRequest,
yield_time_ms: u64,
) -> RuntimeResponse {
let cell_id = service.create_cell(request).await.unwrap();
service
.observe(ObserveRequest {
cell_id,
yield_time_ms,
})
.await
.unwrap()
.into()
}
async fn create_and_observe_to_pending(
service: &CodeModeService,
request: CreateCellRequest,
) -> Result<PendingOutcome, String> {
let cell_id = service.create_cell(request).await?;
match service
.observe_to_pending(ObserveToPendingRequest { cell_id })
.await?
{
ObserveToPendingOutcome::LiveCell(outcome) => Ok(outcome),
ObserveToPendingOutcome::MissingCell(response) => Ok(PendingOutcome::Completed(response)),
}
}
@@ -209,17 +241,18 @@ async fn next_event(events_rx: &mut mpsc::UnboundedReceiver<DelegateEvent>) -> D
#[tokio::test]
async fn yields_and_resumes() {
let service = CodeModeService::new();
let cell = service
.execute(ExecuteRequest {
let cell = execute_with_yield_time(
&service,
CreateCellRequest {
source: r#"text("before"); yield_control(); text("after");"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
/*yield_time_ms*/ 60_000,
)
.await;
assert_eq!(
cell.initial_response().await.unwrap(),
cell,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
@@ -229,13 +262,13 @@ async fn yields_and_resumes() {
);
assert_eq!(
service
.wait(WaitRequest {
.observe(ObserveRequest {
cell_id: cell_id("1"),
yield_time_ms: 60_000,
})
.await
.unwrap(),
WaitOutcome::LiveCell(RuntimeResponse::Result {
CellOutcome::LiveCell(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "after".to_string(),
@@ -251,20 +284,21 @@ async fn returns_and_resumes_from_the_pending_frontier() {
let service = CodeModeService::with_delegate(delegate.clone());
assert_eq!(
service
.execute_to_pending(ExecuteRequest {
create_and_observe_to_pending(
&service,
CreateCellRequest {
enabled_tools: vec![blocking_tool()],
source: r#"
await tools.block({});
text("after");
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap(),
ExecuteToPendingOutcome::Pending {
},
)
.await
.unwrap(),
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-1".to_string()],
@@ -276,53 +310,51 @@ text("after");
assert_eq!(
service
.wait_to_pending(WaitToPendingRequest {
.observe_to_pending(ObserveToPendingRequest {
cell_id: cell_id("1"),
})
.await
.unwrap(),
WaitToPendingOutcome::LiveCell(ExecuteToPendingOutcome::Completed(
RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "after".to_string(),
}],
error_text: None,
}
))
ObserveToPendingOutcome::LiveCell(PendingOutcome::Completed(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "after".to_string(),
}],
error_text: None,
}))
);
}
#[tokio::test]
async fn observed_natural_completion_wins_over_termination() {
let service = CodeModeService::new();
let cell = service
.execute(execute_request(
r#"yield_control(); store("finished", true); text("done");"#,
))
.await
.unwrap();
let cell = execute(
&service,
execute_request(r#"yield_control(); store("finished", true); text("done");"#),
)
.await;
assert_eq!(
cell.initial_response().await.unwrap(),
cell,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: Vec::new(),
}
);
tokio::time::timeout(Duration::from_secs(1), async {
tokio::time::timeout(Duration::from_secs(10), async {
loop {
let response = service
.execute(ExecuteRequest {
yield_time_ms: Some(60_000),
// Observe the synchronous probe through completion so scheduler load
// cannot turn its short yield deadline into a false lifecycle failure.
let response = create_and_observe_to_pending(
&service,
CreateCellRequest {
..execute_request(r#"text(String(load("finished")));"#)
})
.await
.unwrap()
.initial_response()
.await
.unwrap();
let RuntimeResponse::Result { content_items, .. } = response else {
},
)
.await
.unwrap();
let PendingOutcome::Completed(RuntimeResponse::Result { content_items, .. }) = response
else {
panic!("expected stored-value probe to complete");
};
if content_items
@@ -339,7 +371,7 @@ async fn observed_natural_completion_wins_over_termination() {
.unwrap();
assert_eq!(
service.terminate(cell_id("1")).await.unwrap(),
WaitOutcome::LiveCell(RuntimeResponse::Result {
CellOutcome::LiveCell(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
@@ -353,19 +385,18 @@ async fn observed_natural_completion_wins_over_termination() {
async fn termination_discards_pending_callbacks_before_responding() {
let (delegate, mut events_rx) = BlockingDelegate::new();
let service = CodeModeService::with_delegate(delegate.clone());
let cell = service
.execute(execute_request(
r#"notify("pending"); await new Promise(() => {});"#,
))
.await
.unwrap();
let cell = execute(
&service,
execute_request(r#"notify("pending"); await new Promise(() => {});"#),
)
.await;
assert_eq!(
next_event(&mut events_rx).await,
DelegateEvent::NotificationStarted
);
assert_eq!(
cell.initial_response().await.unwrap(),
cell,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: Vec::new(),
@@ -373,7 +404,7 @@ async fn termination_discards_pending_callbacks_before_responding() {
);
assert_eq!(
service.terminate(cell_id("1")).await.unwrap(),
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -388,15 +419,14 @@ async fn termination_discards_pending_callbacks_before_responding() {
async fn termination_discards_stored_writes_before_the_next_cell_can_load_them() {
let (delegate, mut events_rx) = BlockingDelegate::new();
let service = CodeModeService::with_delegate(delegate.clone());
let cell = service
.execute(ExecuteRequest {
let created_cell_id = service
.create_cell(CreateCellRequest {
enabled_tools: vec![blocking_tool()],
source: r#"
store("candidate", "leaked");
await tools.block({});
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
@@ -405,8 +435,8 @@ await tools.block({});
// Reaching the delegate proves that the store ran before execution became gated.
assert_eq!(next_event(&mut events_rx).await, DelegateEvent::ToolStarted);
assert_eq!(
service.terminate(cell.cell_id).await.unwrap(),
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
service.terminate(created_cell_id).await.unwrap(),
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -418,16 +448,12 @@ await tools.block({});
);
assert_eq!(
service
.execute(ExecuteRequest {
yield_time_ms: Some(60_000),
..execute_request(r#"text(String(load("candidate")));"#)
})
.await
.unwrap()
.initial_response()
.await
.unwrap(),
execute_with_yield_time(
&service,
execute_request(r#"text(String(load("candidate")));"#),
/*yield_time_ms*/ 60_000,
)
.await,
RuntimeResponse::Result {
cell_id: cell_id("2"),
content_items: vec![FunctionCallOutputContentItem::InputText {
@@ -443,7 +469,7 @@ async fn shutdown_does_not_await_notifications_during_natural_completion() {
let (delegate, mut events_rx) = NeverResolvingNotificationDelegate::new();
let service = Arc::new(CodeModeService::with_delegate(delegate));
service
.execute(execute_request(r#"notify("pending");"#))
.create_cell(execute_request(r#"notify("pending");"#))
.await
.unwrap();
@@ -469,8 +495,8 @@ async fn shutdown_does_not_await_notifications_during_natural_completion() {
async fn shutdown_does_not_await_a_non_cooperative_nested_tool() {
let (delegate, mut events_rx) = NeverResolvingToolDelegate::new();
let service = CodeModeService::with_delegate(delegate);
let _started = service
.execute(ExecuteRequest {
let _cell_id = service
.create_cell(CreateCellRequest {
enabled_tools: vec![blocking_tool()],
source: r#"await tools.block({});"#.to_string(),
..execute_request("")
@@ -493,19 +519,18 @@ async fn shutdown_does_not_await_a_non_cooperative_nested_tool() {
async fn termination_does_not_await_a_non_cooperative_notification() {
let (delegate, mut events_rx) = NeverResolvingNotificationDelegate::new();
let service = CodeModeService::with_delegate(delegate);
let cell = service
.execute(execute_request(
r#"notify("pending"); await new Promise(() => {});"#,
))
.await
.unwrap();
let cell = execute(
&service,
execute_request(r#"notify("pending"); await new Promise(() => {});"#),
)
.await;
assert_eq!(
next_event(&mut events_rx).await,
DelegateEvent::NotificationStarted
);
assert_eq!(
cell.initial_response().await.unwrap(),
cell,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: Vec::new(),
@@ -520,7 +545,7 @@ async fn termination_does_not_await_a_non_cooperative_notification() {
.await
.expect("termination should not await a non-cooperative notification")
.unwrap(),
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -534,13 +559,10 @@ async fn termination_does_not_await_a_non_cooperative_notification() {
#[tokio::test]
async fn second_observer_is_rejected_without_displacing_the_first() {
let service = CodeModeService::new();
let cell = service
.execute(execute_request("await new Promise(() => {});"))
.await
.unwrap();
let cell = execute(&service, execute_request("await new Promise(() => {});")).await;
assert_eq!(
cell.initial_response().await.unwrap(),
cell,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: Vec::new(),
@@ -548,14 +570,14 @@ async fn second_observer_is_rejected_without_displacing_the_first() {
);
let first_observer = service
.begin_wait(WaitRequest {
.begin_observe(ObserveRequest {
cell_id: cell_id("1"),
yield_time_ms: 60_000,
})
.await;
assert_eq!(
service
.wait(WaitRequest {
.observe(ObserveRequest {
cell_id: cell_id("1"),
yield_time_ms: 60_000,
})
@@ -570,11 +592,11 @@ async fn second_observer_is_rejected_without_displacing_the_first() {
};
assert_eq!(
service.terminate(cell_id("1")).await.unwrap(),
WaitOutcome::LiveCell(terminated.clone())
CellOutcome::LiveCell(terminated.clone())
);
assert_eq!(
first_observer.await.unwrap(),
WaitOutcome::LiveCell(terminated)
CellOutcome::LiveCell(terminated)
);
}
@@ -582,8 +604,8 @@ async fn second_observer_is_rejected_without_displacing_the_first() {
async fn natural_completion_cleans_up_callbacks_before_responding() {
let (delegate, mut events_rx) = BlockingDelegate::new();
let service = CodeModeService::with_delegate(delegate.clone());
let cell = service
.execute(ExecuteRequest {
let created_cell_id = service
.create_cell(CreateCellRequest {
enabled_tools: vec![blocking_tool()],
source: concat!(
"tools.block({});",
@@ -591,7 +613,6 @@ async fn natural_completion_cleans_up_callbacks_before_responding() {
"text('done');",
)
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
@@ -599,14 +620,20 @@ async fn natural_completion_cleans_up_callbacks_before_responding() {
assert_eq!(next_event(&mut events_rx).await, DelegateEvent::ToolStarted);
assert_eq!(
cell.initial_response().await.unwrap(),
RuntimeResponse::Result {
service
.observe(ObserveRequest {
cell_id: created_cell_id,
yield_time_ms: 60_000,
})
.await
.unwrap(),
CellOutcome::LiveCell(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
}],
error_text: None,
}
})
);
assert!(delegate.tool_future_dropped.load(Ordering::Acquire));
}
+136 -123
View File
@@ -2,20 +2,20 @@ use std::sync::Arc;
use std::time::Duration;
use super::CellId;
use super::CellOutcome;
use super::CodeModeNestedToolCall;
use super::CodeModeService;
use super::CodeModeSessionDelegate;
use super::NotificationFuture;
use super::ObserveRequest;
use super::ObserveToPendingOutcome;
use super::ObserveToPendingRequest;
use super::RuntimeResponse;
use super::ToolInvocationFuture;
use super::WaitOutcome;
use super::WaitRequest;
use super::WaitToPendingOutcome;
use super::WaitToPendingRequest;
use crate::CodeModeToolKind;
use crate::ExecuteRequest;
use crate::ExecuteToPendingOutcome;
use crate::CreateCellRequest;
use crate::FunctionCallOutputContentItem;
use crate::PendingOutcome;
use crate::ToolDefinition;
use codex_protocol::ToolName;
use pretty_assertions::assert_eq;
@@ -61,13 +61,11 @@ impl CodeModeSessionDelegate for ReleasableToolDelegate {
fn cell_closed(&self, _cell_id: &CellId) {}
}
fn execute_request(source: &str) -> ExecuteRequest {
ExecuteRequest {
fn execute_request(source: &str) -> CreateCellRequest {
CreateCellRequest {
tool_call_id: "call_1".to_string(),
enabled_tools: Vec::new(),
source: source.to_string(),
yield_time_ms: Some(1),
max_output_tokens: None,
}
}
@@ -86,14 +84,38 @@ fn echo_tool() -> ToolDefinition {
}
}
async fn execute(service: &CodeModeService, request: ExecuteRequest) -> RuntimeResponse {
async fn execute(service: &CodeModeService, request: CreateCellRequest) -> RuntimeResponse {
execute_with_yield_time(service, request, /*yield_time_ms*/ 10_000).await
}
async fn execute_with_yield_time(
service: &CodeModeService,
request: CreateCellRequest,
yield_time_ms: u64,
) -> RuntimeResponse {
let cell_id = service.create_cell(request).await.unwrap();
service
.execute(request)
.await
.unwrap()
.initial_response()
.observe(ObserveRequest {
cell_id,
yield_time_ms,
})
.await
.unwrap()
.into()
}
async fn create_and_observe_to_pending(
service: &CodeModeService,
request: CreateCellRequest,
) -> Result<PendingOutcome, String> {
let cell_id = service.create_cell(request).await?;
match service
.observe_to_pending(ObserveToPendingRequest { cell_id })
.await?
{
ObserveToPendingOutcome::LiveCell(outcome) => Ok(outcome),
ObserveToPendingOutcome::MissingCell(response) => Ok(PendingOutcome::Completed(response)),
}
}
#[tokio::test]
@@ -102,9 +124,8 @@ async fn synchronous_exit_returns_successfully() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"text("before"); exit(); text("after");"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -129,9 +150,8 @@ async fn stored_values_are_shared_between_cells_but_not_sessions() {
let write_response = execute(
&first_session,
ExecuteRequest {
CreateCellRequest {
source: r#"store("key", "visible");"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -139,18 +159,16 @@ async fn stored_values_are_shared_between_cells_but_not_sessions() {
let same_session = execute(
&first_session,
ExecuteRequest {
CreateCellRequest {
source: r#"text(String(load("key")));"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
.await;
let other_session = execute(
&second_session,
ExecuteRequest {
CreateCellRequest {
source: r#"text(String(load("key")));"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -190,15 +208,17 @@ async fn stored_values_are_shared_between_cells_but_not_sessions() {
async fn shutdown_interrupts_cpu_bound_cells() {
let service = CodeModeService::new();
let cell = service
.execute(ExecuteRequest {
let response = execute_with_yield_time(
&service,
CreateCellRequest {
source: "while (true) {}".to_string(),
..execute_request("")
})
.await
.unwrap();
},
/*yield_time_ms*/ 1,
)
.await;
assert_eq!(
cell.initial_response().await.unwrap(),
response,
RuntimeResponse::Yielded {
cell_id: cell_id("1"),
content_items: Vec::new(),
@@ -217,7 +237,7 @@ async fn start_cell_rejects_new_cell_after_shutdown_begins() {
service.shutdown().await.unwrap();
let error = service
.execute(execute_request("text('late');"))
.create_cell(execute_request("text('late');"))
.await
.err()
.unwrap();
@@ -226,21 +246,22 @@ async fn start_cell_rejects_new_cell_after_shutdown_begins() {
}
#[tokio::test]
async fn execute_to_pending_returns_completed_for_synchronous_results() {
async fn create_and_observe_to_pending_returns_completed_for_synchronous_results() {
let service = CodeModeService::new();
let response = service
.execute_to_pending(ExecuteRequest {
let response = create_and_observe_to_pending(
&service,
CreateCellRequest {
source: r#"text("done");"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(
response,
ExecuteToPendingOutcome::Completed(RuntimeResponse::Result {
PendingOutcome::Completed(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
@@ -251,16 +272,18 @@ async fn execute_to_pending_returns_completed_for_synchronous_results() {
}
#[tokio::test]
async fn execute_to_pending_returns_once_the_runtime_is_quiescent() {
async fn create_and_observe_to_pending_returns_once_the_runtime_is_quiescent() {
let service = CodeModeService::new();
let response = tokio::time::timeout(
Duration::from_secs(1),
service.execute_to_pending(ExecuteRequest {
source: r#"text("before"); await new Promise(() => {});"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
}),
create_and_observe_to_pending(
&service,
CreateCellRequest {
source: r#"text("before"); await new Promise(() => {});"#.to_string(),
..execute_request("")
},
),
)
.await
.unwrap()
@@ -268,7 +291,7 @@ async fn execute_to_pending_returns_once_the_runtime_is_quiescent() {
assert_eq!(
response,
ExecuteToPendingOutcome::Pending {
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "before".to_string(),
@@ -281,7 +304,7 @@ async fn execute_to_pending_returns_once_the_runtime_is_quiescent() {
assert_eq!(
termination,
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -289,11 +312,12 @@ async fn execute_to_pending_returns_once_the_runtime_is_quiescent() {
}
#[tokio::test]
async fn execute_to_pending_identifies_tool_calls_in_paused_frontier() {
async fn create_and_observe_to_pending_identifies_tool_calls_in_paused_frontier() {
let service = CodeModeService::new();
let response = service
.execute_to_pending(ExecuteRequest {
let response = create_and_observe_to_pending(
&service,
CreateCellRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await Promise.all([
@@ -302,15 +326,15 @@ await Promise.all([
]);
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(
response,
ExecuteToPendingOutcome::Pending {
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-1".to_string(), "tool-2".to_string()],
@@ -321,7 +345,7 @@ await Promise.all([
assert_eq!(
termination,
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -329,11 +353,12 @@ await Promise.all([
}
#[tokio::test]
async fn execute_to_pending_excludes_delayed_timeout_tool_calls_until_wait() {
async fn create_and_observe_to_pending_excludes_delayed_timeout_tool_calls_until_observe() {
let service = CodeModeService::new();
let initial_response = service
.execute_to_pending(ExecuteRequest {
let initial_response = create_and_observe_to_pending(
&service,
CreateCellRequest {
enabled_tools: vec![echo_tool()],
source: r#"
setTimeout(() => {
@@ -345,15 +370,15 @@ await Promise.all([
]);
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(
initial_response,
ExecuteToPendingOutcome::Pending {
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-1".to_string(), "tool-2".to_string()],
@@ -367,13 +392,13 @@ await Promise.all([
let resumed_response = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let response = service
.wait_to_pending(WaitToPendingRequest {
.observe_to_pending(ObserveToPendingRequest {
cell_id: cell_id("1"),
})
.await?;
if !matches!(
&response,
WaitToPendingOutcome::LiveCell(ExecuteToPendingOutcome::Pending {
ObserveToPendingOutcome::LiveCell(PendingOutcome::Pending {
pending_tool_call_ids,
..
}) if pending_tool_call_ids.is_empty()
@@ -388,7 +413,7 @@ await Promise.all([
assert_eq!(
resumed_response,
WaitToPendingOutcome::LiveCell(ExecuteToPendingOutcome::Pending {
ObserveToPendingOutcome::LiveCell(PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-3".to_string()],
@@ -399,7 +424,7 @@ await Promise.all([
assert_eq!(
termination,
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -407,12 +432,13 @@ await Promise.all([
}
#[tokio::test]
async fn wait_to_pending_returns_after_resumed_runtime_becomes_quiescent_again() {
async fn observe_to_pending_returns_after_resumed_runtime_becomes_quiescent_again() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = CodeModeService::with_delegate(delegate.clone());
let initial_response = service
.execute_to_pending(ExecuteRequest {
let initial_response = create_and_observe_to_pending(
&service,
CreateCellRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await tools.echo({});
@@ -420,15 +446,15 @@ text("after");
await new Promise(() => {});
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(
initial_response,
ExecuteToPendingOutcome::Pending {
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-1".to_string()],
@@ -439,7 +465,7 @@ await new Promise(() => {});
let resumed_response = tokio::time::timeout(
Duration::from_secs(1),
service.wait_to_pending(WaitToPendingRequest {
service.observe_to_pending(ObserveToPendingRequest {
cell_id: cell_id("1"),
}),
)
@@ -449,7 +475,7 @@ await new Promise(() => {});
assert_eq!(
resumed_response,
WaitToPendingOutcome::LiveCell(ExecuteToPendingOutcome::Pending {
ObserveToPendingOutcome::LiveCell(PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "after".to_string(),
@@ -462,7 +488,7 @@ await new Promise(() => {});
assert_eq!(
termination,
WaitOutcome::LiveCell(RuntimeResponse::Terminated {
CellOutcome::LiveCell(RuntimeResponse::Terminated {
cell_id: cell_id("1"),
content_items: Vec::new(),
})
@@ -470,27 +496,28 @@ await new Promise(() => {});
}
#[tokio::test]
async fn wait_to_pending_returns_completed_after_resumed_runtime_finishes() {
async fn observe_to_pending_returns_completed_after_resumed_runtime_finishes() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = CodeModeService::with_delegate(delegate.clone());
let initial_response = service
.execute_to_pending(ExecuteRequest {
let initial_response = create_and_observe_to_pending(
&service,
CreateCellRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await tools.echo({});
text("done");
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(
initial_response,
ExecuteToPendingOutcome::Pending {
PendingOutcome::Pending {
cell_id: cell_id("1"),
content_items: Vec::new(),
pending_tool_call_ids: vec!["tool-1".to_string()],
@@ -501,7 +528,7 @@ text("done");
let resumed_response = tokio::time::timeout(
Duration::from_secs(1),
service.wait_to_pending(WaitToPendingRequest {
service.observe_to_pending(ObserveToPendingRequest {
cell_id: cell_id("1"),
}),
)
@@ -511,15 +538,13 @@ text("done");
assert_eq!(
resumed_response,
WaitToPendingOutcome::LiveCell(ExecuteToPendingOutcome::Completed(
RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
}],
error_text: None,
}
))
ObserveToPendingOutcome::LiveCell(PendingOutcome::Completed(RuntimeResponse::Result {
cell_id: cell_id("1"),
content_items: vec![FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
}],
error_text: None,
}))
);
}
@@ -529,9 +554,8 @@ async fn v8_console_is_not_exposed_on_global_this() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"text(String(Object.hasOwn(globalThis, "console")));"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -555,7 +579,7 @@ async fn date_locale_string_formats_with_icu_data() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
const value = new Date("2025-01-02T03:04:05Z")
.toLocaleString("fr-FR", {
@@ -571,7 +595,6 @@ const value = new Date("2025-01-02T03:04:05Z")
text(value);
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -595,7 +618,7 @@ async fn intl_date_time_format_formats_with_icu_data() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
const formatter = new Intl.DateTimeFormat("fr-FR", {
weekday: "long",
@@ -610,7 +633,6 @@ const formatter = new Intl.DateTimeFormat("fr-FR", {
text(formatter.format(new Date("2025-01-02T03:04:05Z")));
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -634,7 +656,7 @@ async fn output_helpers_return_undefined() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
const returnsUndefined = [
text("first"),
@@ -644,7 +666,6 @@ const returnsUndefined = [
text(JSON.stringify(returnsUndefined));
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -677,7 +698,7 @@ async fn image_helper_accepts_raw_mcp_image_block_with_original_detail() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image({
type: "image",
@@ -687,7 +708,6 @@ image({
});
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -712,7 +732,7 @@ async fn generated_image_helper_appends_image_and_output_hint() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
generatedImage({
image_url: "data:image/png;base64,AAA",
@@ -720,7 +740,6 @@ generatedImage({
});
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -750,7 +769,7 @@ async fn image_helper_second_arg_overrides_explicit_object_detail() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image(
{
@@ -761,7 +780,6 @@ image(
);
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -786,7 +804,7 @@ async fn image_helper_second_arg_overrides_raw_mcp_image_detail() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image(
{
@@ -799,7 +817,6 @@ image(
);
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -824,7 +841,7 @@ async fn image_helper_accepts_low_detail() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image({
image_url: "data:image/png;base64,AAA",
@@ -832,7 +849,6 @@ image({
});
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -865,9 +881,8 @@ async fn image_helpers_reject_remote_urls() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source,
yield_time_ms: None,
..execute_request("")
},
)
@@ -893,7 +908,7 @@ async fn image_helper_rejects_unsupported_detail() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image({
image_url: "data:image/png;base64,AAA",
@@ -901,7 +916,6 @@ image({
});
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -923,7 +937,7 @@ async fn image_helper_rejects_raw_mcp_result_container() {
let response = execute(
&service,
ExecuteRequest {
CreateCellRequest {
source: r#"
image({
content: [
@@ -938,7 +952,6 @@ image({
});
"#
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
@@ -957,11 +970,11 @@ image({
}
#[tokio::test]
async fn wait_reports_missing_cell_separately_from_runtime_results() {
async fn observe_reports_missing_cell_separately_from_runtime_results() {
let service = CodeModeService::new();
let response = service
.wait(WaitRequest {
.observe(ObserveRequest {
cell_id: cell_id("missing"),
yield_time_ms: 1,
})
@@ -970,7 +983,7 @@ async fn wait_reports_missing_cell_separately_from_runtime_results() {
assert_eq!(
response,
WaitOutcome::MissingCell(RuntimeResponse::Result {
CellOutcome::MissingCell(RuntimeResponse::Result {
cell_id: cell_id("missing"),
content_items: Vec::new(),
error_text: Some("exec cell missing not found".to_string()),