From 18a3fd04c9c87c5e7c639ed8df5f85d4e3f6c8ab Mon Sep 17 00:00:00 2001 From: Channing Conger Date: Tue, 16 Jun 2026 00:59:11 +0000 Subject: [PATCH] code-mode: introduce transport-neutral session runtime --- .../code-mode/src/cell_actor/conversions.rs | 4 +- codex-rs/code-mode/src/cell_actor/mod.rs | 13 +- codex-rs/code-mode/src/cell_actor/tests.rs | 3 +- codex-rs/code-mode/src/cell_actor/types.rs | 78 +--- codex-rs/code-mode/src/lib.rs | 2 + codex-rs/code-mode/src/service.rs | 345 +++++++----------- .../code-mode/src/service_contract_tests.rs | 16 +- codex-rs/code-mode/src/service_tests.rs | 11 +- codex-rs/code-mode/src/session_runtime/mod.rs | 283 ++++++++++++++ .../code-mode/src/session_runtime/types.rs | 166 +++++++++ 10 files changed, 615 insertions(+), 306 deletions(-) create mode 100644 codex-rs/code-mode/src/session_runtime/mod.rs create mode 100644 codex-rs/code-mode/src/session_runtime/types.rs diff --git a/codex-rs/code-mode/src/cell_actor/conversions.rs b/codex-rs/code-mode/src/cell_actor/conversions.rs index ebc12d29f5..844393521b 100644 --- a/codex-rs/code-mode/src/cell_actor/conversions.rs +++ b/codex-rs/code-mode/src/cell_actor/conversions.rs @@ -5,10 +5,10 @@ use codex_code_mode_protocol::ImageDetail; use codex_code_mode_protocol::ToolDefinition; use codex_protocol::ToolName; -use super::CellImageDetail; -use super::CellOutputItem; use super::CellRequest; use super::CellToolKind; +use crate::session_runtime::ImageDetail as CellImageDetail; +use crate::session_runtime::OutputItem as CellOutputItem; pub(super) fn runtime_request(request: CellRequest) -> ExecuteRequest { ExecuteRequest { diff --git a/codex-rs/code-mode/src/cell_actor/mod.rs b/codex-rs/code-mode/src/cell_actor/mod.rs index d42ce48a24..bd67fd3e8d 100644 --- a/codex-rs/code-mode/src/cell_actor/mod.rs +++ b/codex-rs/code-mode/src/cell_actor/mod.rs @@ -22,23 +22,20 @@ use self::conversions::output_item; use self::conversions::runtime_request; use self::types::CellCommand; pub(crate) use self::types::CellError; -pub(crate) use self::types::CellEvent; pub(crate) use self::types::CellEventFuture; pub(crate) use self::types::CellHandle; pub(crate) use self::types::CellHost; -pub(crate) use self::types::CellImageDetail; -pub(crate) use self::types::CellOutputItem; -pub(crate) use self::types::CellRequest; pub(crate) use self::types::CellToolCall; -pub(crate) use self::types::CellToolDefinition; -pub(crate) use self::types::CellToolKind; -pub(crate) use self::types::CellToolName; -pub(crate) use self::types::ObserveMode; use crate::runtime::PendingRuntimeMode; use crate::runtime::RuntimeCommand; use crate::runtime::RuntimeControlCommand; use crate::runtime::RuntimeEvent; use crate::runtime::spawn_runtime; +use crate::session_runtime::CellEvent; +use crate::session_runtime::ExecuteRequest as CellRequest; +use crate::session_runtime::ObserveMode; +use crate::session_runtime::ToolKind as CellToolKind; +use crate::session_runtime::ToolName as CellToolName; pub(crate) struct CellActor; diff --git a/codex-rs/code-mode/src/cell_actor/tests.rs b/codex-rs/code-mode/src/cell_actor/tests.rs index 218f1fcde4..ca01c3b142 100644 --- a/codex-rs/code-mode/src/cell_actor/tests.rs +++ b/codex-rs/code-mode/src/cell_actor/tests.rs @@ -11,6 +11,7 @@ use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; use super::*; +use crate::session_runtime::OutputItem; struct TestHost; @@ -114,7 +115,7 @@ async fn yield_timer_preempts_buffered_runtime_output() { assert_eq!( termination.await, Ok(CellEvent::Terminated { - content_items: vec![CellOutputItem::Text { + content_items: vec![OutputItem::Text { text: "queued output".to_string(), }], }) diff --git a/codex-rs/code-mode/src/cell_actor/types.rs b/codex-rs/code-mode/src/cell_actor/types.rs index aef0b5211f..d82f898e76 100644 --- a/codex-rs/code-mode/src/cell_actor/types.rs +++ b/codex-rs/code-mode/src/cell_actor/types.rs @@ -4,40 +4,20 @@ use std::pin::Pin; use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; -use std::time::Duration; use serde_json::Value as JsonValue; use tokio::sync::mpsc; use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; +use crate::session_runtime::CellEvent; +use crate::session_runtime::ObserveMode; +use crate::session_runtime::ToolKind; +use crate::session_runtime::ToolName; + pub(crate) type CellEventFuture = Pin> + Send + 'static>>; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) enum ObserveMode { - YieldAfter(Duration), - PendingFrontier, -} - -#[derive(Clone, Debug, PartialEq)] -pub(crate) enum CellEvent { - Yielded { - content_items: Vec, - }, - Pending { - content_items: Vec, - pending_tool_call_ids: Vec, - }, - Completed { - content_items: Vec, - error_text: Option, - }, - Terminated { - content_items: Vec, - }, -} - #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) enum CellError { Busy, @@ -45,54 +25,10 @@ pub(crate) enum CellError { Closed, } -#[derive(Clone, Debug, PartialEq)] -pub(crate) enum CellOutputItem { - Text { - text: String, - }, - Image { - image_url: String, - detail: Option, - }, -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) enum CellImageDetail { - Auto, - Low, - High, - Original, -} - -pub(crate) struct CellRequest { - pub(crate) tool_call_id: String, - pub(crate) enabled_tools: Vec, - pub(crate) source: String, -} - -pub(crate) struct CellToolDefinition { - pub(crate) name: String, - pub(crate) tool_name: CellToolName, - pub(crate) description: String, - pub(crate) kind: CellToolKind, -} - -#[derive(Clone, Debug, Eq, PartialEq)] -pub(crate) struct CellToolName { - pub(crate) name: String, - pub(crate) namespace: Option, -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) enum CellToolKind { - Function, - Freeform, -} - pub(crate) struct CellToolCall { pub(crate) id: String, - pub(crate) name: CellToolName, - pub(crate) kind: CellToolKind, + pub(crate) name: ToolName, + pub(crate) kind: ToolKind, pub(crate) input: Option, } diff --git a/codex-rs/code-mode/src/lib.rs b/codex-rs/code-mode/src/lib.rs index 76f8aa9b97..079e8962d6 100644 --- a/codex-rs/code-mode/src/lib.rs +++ b/codex-rs/code-mode/src/lib.rs @@ -1,8 +1,10 @@ mod cell_actor; mod runtime; mod service; +pub mod session_runtime; pub use codex_code_mode_protocol::*; pub use service::CodeModeService; pub use service::InProcessCodeModeSessionProvider; pub use service::NoopCodeModeSessionDelegate; +pub use session_runtime::SessionRuntime; diff --git a/codex-rs/code-mode/src/service.rs b/codex-rs/code-mode/src/service.rs index 4c8fa517ae..d9964bd44f 100644 --- a/codex-rs/code-mode/src/service.rs +++ b/codex-rs/code-mode/src/service.rs @@ -1,8 +1,4 @@ -use std::collections::HashMap; use std::sync::Arc; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::AtomicU64; -use std::sync::atomic::Ordering; use std::time::Duration; use codex_code_mode_protocol::CellId; @@ -27,24 +23,11 @@ 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::Mutex; use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; -use crate::cell_actor::CellActor; -use crate::cell_actor::CellError; -use crate::cell_actor::CellEvent; -use crate::cell_actor::CellEventFuture; -use crate::cell_actor::CellHandle; -use crate::cell_actor::CellHost; -use crate::cell_actor::CellImageDetail; -use crate::cell_actor::CellOutputItem; -use crate::cell_actor::CellRequest; -use crate::cell_actor::CellToolCall; -use crate::cell_actor::CellToolDefinition; -use crate::cell_actor::CellToolKind; -use crate::cell_actor::CellToolName; -use crate::cell_actor::ObserveMode; +use crate::SessionRuntime; +use crate::session_runtime as runtime; pub struct NoopCodeModeSessionDelegate; @@ -89,16 +72,8 @@ impl CodeModeSessionProvider for InProcessCodeModeSessionProvider { } } -struct Inner { - stored_values: Mutex>, - cells: Mutex>, - delegate: Arc, - shutting_down: AtomicBool, - next_cell_id: AtomicU64, -} - pub struct CodeModeService { - inner: Arc, + runtime: SessionRuntime, } impl CodeModeService { @@ -108,45 +83,31 @@ impl CodeModeService { pub fn with_delegate(delegate: Arc) -> Self { Self { - inner: Arc::new(Inner { - stored_values: Mutex::new(HashMap::new()), - cells: Mutex::new(HashMap::new()), - delegate, - shutting_down: AtomicBool::new(false), - next_cell_id: AtomicU64::new(1), - }), + runtime: SessionRuntime::new(Arc::new(ProtocolDelegate { delegate })), } } - fn allocate_cell_id(&self) -> CellId { - CellId::new( - self.inner - .next_cell_id - .fetch_add(1, Ordering::Relaxed) - .to_string(), - ) - } - pub async fn execute(&self, request: ExecuteRequest) -> Result { let yield_time_ms = request.yield_time_ms.unwrap_or(DEFAULT_EXEC_YIELD_TIME_MS); - let cell_id = self.allocate_cell_id(); - let initial_event = self - .start_cell( - cell_id.clone(), - request, - ObserveMode::YieldAfter(Duration::from_millis(yield_time_ms)), + let started = self + .runtime + .execute( + runtime_request(request), + runtime::ObserveMode::YieldAfter(Duration::from_millis(yield_time_ms)), ) - .await?; + .await + .map_err(|error| error.to_string())?; + let cell_id = protocol_cell_id(&started.cell_id); let response_cell_id = cell_id.clone(); let (response_tx, response_rx) = oneshot::channel(); tokio::spawn(async move { - let response = initial_event + let response = started + .initial_event() .await - .map_err(|error| cell_error_text(&response_cell_id, error)) + .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)) } @@ -154,45 +115,22 @@ impl CodeModeService { &self, request: ExecuteRequest, ) -> Result { - let cell_id = self.allocate_cell_id(); - let event = self - .start_cell(cell_id.clone(), request, ObserveMode::PendingFrontier) - .await? + let started = self + .runtime + .execute( + runtime_request(request), + runtime::ObserveMode::PendingFrontier, + ) .await - .map_err(|error| cell_error_text(&cell_id, error))?; + .map_err(|error| error.to_string())?; + let cell_id = protocol_cell_id(&started.cell_id); + let event = started + .initial_event() + .await + .map_err(|error| error.to_string())?; pending_outcome(&cell_id, event) } - async fn start_cell( - &self, - cell_id: CellId, - request: ExecuteRequest, - initial_observe_mode: ObserveMode, - ) -> Result { - let stored_values = self.inner.stored_values.lock().await.clone(); - let host = Arc::new(ServiceCellHost { - cell_id: cell_id.clone(), - inner: Arc::clone(&self.inner), - }); - let mut cells = self.inner.cells.lock().await; - if self.inner.shutting_down.load(Ordering::Acquire) { - return Err("code mode session is shutting down".to_string()); - } - if cells.contains_key(&cell_id) { - return Err(format!("exec cell {cell_id} already exists")); - } - let (handle, initial_event, task) = CellActor::prepare( - cell_request(request), - stored_values, - host, - initial_observe_mode, - )?; - cells.insert(cell_id, handle); - drop(cells); - tokio::spawn(task); - Ok(initial_event) - } - pub async fn wait(&self, request: WaitRequest) -> Result { self.begin_wait(request).await.await } @@ -205,24 +143,37 @@ impl CodeModeService { cell_id, yield_time_ms, } = request; - let handle = self.inner.cells.lock().await.get(&cell_id).cloned(); - let Some(handle) = handle else { - return missing_wait(cell_id); - }; - wait_for_event( - cell_id, - handle.observe(ObserveMode::YieldAfter(Duration::from_millis( - yield_time_ms, - ))), - ) + let runtime_cell_id = runtime_cell_id(&cell_id); + match self + .runtime + .begin_observe( + &runtime_cell_id, + runtime::ObserveMode::YieldAfter(Duration::from_millis(yield_time_ms)), + ) + .await + { + Ok(pending_event) => Box::pin(async move { + match pending_event.event().await { + Ok(event) => Ok(WaitOutcome::LiveCell(runtime_response(&cell_id, event)?)), + Err(runtime::Error::MissingCell(_)) => { + Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id))) + } + Err(error) => Err(error.to_string()), + } + }), + Err(runtime::Error::MissingCell(_)) => missing_wait(cell_id), + Err(error) => Box::pin(async move { Err(error.to_string()) }), + } } pub async fn terminate(&self, cell_id: CellId) -> Result { - let handle = self.inner.cells.lock().await.get(&cell_id).cloned(); - let Some(handle) = handle else { - return Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id))); - }; - wait_for_event(cell_id, handle.terminate()).await + match self.runtime.terminate(&runtime_cell_id(&cell_id)).await { + Ok(event) => Ok(WaitOutcome::LiveCell(runtime_response(&cell_id, event)?)), + Err(runtime::Error::MissingCell(_)) => { + Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id))) + } + Err(error) => Err(error.to_string()), + } } pub async fn wait_to_pending( @@ -230,40 +181,29 @@ impl CodeModeService { request: WaitToPendingRequest, ) -> Result { let cell_id = request.cell_id; - let handle = self.inner.cells.lock().await.get(&cell_id).cloned(); - let Some(handle) = handle else { - return Ok(WaitToPendingOutcome::MissingCell(missing_cell_response( - cell_id, - ))); - }; - match handle.observe(ObserveMode::PendingFrontier).await { + match self + .runtime + .observe( + &runtime_cell_id(&cell_id), + runtime::ObserveMode::PendingFrontier, + ) + .await + { Ok(event) => Ok(WaitToPendingOutcome::LiveCell(pending_outcome( &cell_id, event, )?)), - Err(CellError::Closed) => Ok(WaitToPendingOutcome::MissingCell(missing_cell_response( - cell_id, - ))), - Err(error) => Err(cell_error_text(&cell_id, error)), + Err(runtime::Error::MissingCell(_)) => Ok(WaitToPendingOutcome::MissingCell( + missing_cell_response(cell_id), + )), + Err(error) => Err(error.to_string()), } } pub async fn shutdown(&self) -> Result<(), String> { - self.inner.shutting_down.store(true, Ordering::Release); - let handles = self - .inner - .cells - .lock() + self.runtime + .shutdown() .await - .values() - .cloned() - .collect::>(); - for handle in handles { - handle.shutdown(); - } - while !self.inner.cells.lock().await.is_empty() { - tokio::task::yield_now().await; - } - Ok(()) + .map_err(|error| error.to_string()) } } @@ -273,20 +213,9 @@ impl Default for CodeModeService { } } -impl Drop for CodeModeService { - fn drop(&mut self) { - self.inner.shutting_down.store(true, Ordering::Release); - if let Ok(cells) = self.inner.cells.try_lock() { - for handle in cells.values() { - handle.shutdown(); - } - } - } -} - impl CodeModeSession for CodeModeService { fn is_alive(&self) -> bool { - !self.inner.shutting_down.load(Ordering::Acquire) + self.runtime.is_alive() } fn execute<'a>( @@ -309,30 +238,28 @@ impl CodeModeSession for CodeModeService { } } -struct ServiceCellHost { - cell_id: CellId, - inner: Arc, +struct ProtocolDelegate { + delegate: Arc, } -impl CellHost for ServiceCellHost { +impl runtime::SessionRuntimeDelegate for ProtocolDelegate { async fn invoke_tool( &self, - invocation: CellToolCall, + invocation: runtime::NestedToolCall, cancellation_token: CancellationToken, ) -> Result { - self.inner - .delegate + self.delegate .invoke_tool( CodeModeNestedToolCall { - cell_id: self.cell_id.clone(), - runtime_tool_call_id: invocation.id, + cell_id: protocol_cell_id(&invocation.cell_id), + runtime_tool_call_id: invocation.runtime_tool_call_id, tool_name: codex_protocol::ToolName { - name: invocation.name.name, - namespace: invocation.name.namespace, + name: invocation.tool_name.name, + namespace: invocation.tool_name.namespace, }, - tool_kind: match invocation.kind { - CellToolKind::Function => CodeModeToolKind::Function, - CellToolKind::Freeform => CodeModeToolKind::Freeform, + tool_kind: match invocation.tool_kind { + runtime::ToolKind::Function => CodeModeToolKind::Function, + runtime::ToolKind::Freeform => CodeModeToolKind::Freeform, }, input: invocation.input, }, @@ -344,45 +271,42 @@ impl CellHost for ServiceCellHost { async fn notify( &self, call_id: String, + cell_id: runtime::CellId, text: String, cancellation_token: CancellationToken, ) -> Result<(), String> { - self.inner - .delegate - .notify(call_id, self.cell_id.clone(), text, cancellation_token) + self.delegate + .notify( + call_id, + protocol_cell_id(&cell_id), + text, + cancellation_token, + ) .await } - async fn commit_stored_values(&self, stored_value_writes: HashMap) { - self.inner - .stored_values - .lock() - .await - .extend(stored_value_writes); - } - - async fn closed(&self) { - self.inner.cells.lock().await.remove(&self.cell_id); - self.inner.delegate.cell_closed(&self.cell_id); + async fn cell_closed(&self, cell_id: &runtime::CellId) -> Result<(), String> { + self.delegate.cell_closed(&protocol_cell_id(cell_id)); + Ok(()) } } -fn cell_request(request: ExecuteRequest) -> CellRequest { - CellRequest { +fn runtime_request(request: ExecuteRequest) -> runtime::ExecuteRequest { + runtime::ExecuteRequest { tool_call_id: request.tool_call_id, enabled_tools: request .enabled_tools .into_iter() - .map(|definition| CellToolDefinition { + .map(|definition| runtime::ToolDefinition { name: definition.name, - tool_name: CellToolName { + tool_name: runtime::ToolName { name: definition.tool_name.name, namespace: definition.tool_name.namespace, }, description: definition.description, kind: match definition.kind { - CodeModeToolKind::Function => CellToolKind::Function, - CodeModeToolKind::Freeform => CellToolKind::Freeform, + CodeModeToolKind::Function => runtime::ToolKind::Function, + CodeModeToolKind::Freeform => runtime::ToolKind::Freeform, }, }) .collect(), @@ -390,22 +314,20 @@ fn cell_request(request: ExecuteRequest) -> CellRequest { } } -fn wait_for_event( - cell_id: CellId, - event: CellEventFuture, -) -> CodeModeSessionResultFuture<'static, WaitOutcome> { - Box::pin(async move { - match event.await { - Ok(event) => Ok(WaitOutcome::LiveCell(runtime_response(&cell_id, event)?)), - Err(CellError::Closed) => Ok(WaitOutcome::MissingCell(missing_cell_response(cell_id))), - Err(error) => Err(cell_error_text(&cell_id, error)), - } - }) +fn runtime_cell_id(cell_id: &CellId) -> runtime::CellId { + runtime::CellId::new(cell_id.as_str()) } -fn pending_outcome(cell_id: &CellId, event: CellEvent) -> Result { +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 { match event { - CellEvent::Pending { + runtime::CellEvent::Pending { content_items, pending_tool_call_ids, } => Ok(ExecuteToPendingOutcome::Pending { @@ -419,13 +341,16 @@ fn pending_outcome(cell_id: &CellId, event: CellEvent) -> Result Result { +fn runtime_response( + cell_id: &CellId, + event: runtime::CellEvent, +) -> Result { match event { - CellEvent::Yielded { content_items } => Ok(RuntimeResponse::Yielded { + runtime::CellEvent::Yielded { content_items } => Ok(RuntimeResponse::Yielded { cell_id: cell_id.clone(), content_items: content_items.into_iter().map(output_item).collect(), }), - CellEvent::Completed { + runtime::CellEvent::Completed { content_items, error_text, } => Ok(RuntimeResponse::Result { @@ -433,36 +358,30 @@ fn runtime_response(cell_id: &CellId, event: CellEvent) -> Result Ok(RuntimeResponse::Terminated { + runtime::CellEvent::Terminated { content_items } => Ok(RuntimeResponse::Terminated { cell_id: cell_id.clone(), content_items: content_items.into_iter().map(output_item).collect(), }), - CellEvent::Pending { .. } => { + runtime::CellEvent::Pending { .. } => { Err("cell returned a pending frontier unexpectedly".to_string()) } } } -fn output_item(item: CellOutputItem) -> FunctionCallOutputContentItem { +fn output_item(item: runtime::OutputItem) -> FunctionCallOutputContentItem { match item { - CellOutputItem::Text { text } => FunctionCallOutputContentItem::InputText { text }, - CellOutputItem::Image { image_url, detail } => FunctionCallOutputContentItem::InputImage { - image_url, - detail: detail.map(|detail| match detail { - CellImageDetail::Auto => ImageDetail::Auto, - CellImageDetail::Low => ImageDetail::Low, - CellImageDetail::High => ImageDetail::High, - CellImageDetail::Original => ImageDetail::Original, - }), - }, - } -} - -fn cell_error_text(cell_id: &CellId, error: CellError) -> String { - match error { - CellError::Busy => format!("exec cell {cell_id} already has an active observer"), - CellError::AlreadyTerminating => format!("exec cell {cell_id} is already terminating"), - CellError::Closed => format!("exec cell {cell_id} closed unexpectedly"), + runtime::OutputItem::Text { text } => FunctionCallOutputContentItem::InputText { text }, + runtime::OutputItem::Image { image_url, detail } => { + FunctionCallOutputContentItem::InputImage { + image_url, + detail: detail.map(|detail| match detail { + runtime::ImageDetail::Auto => ImageDetail::Auto, + runtime::ImageDetail::Low => ImageDetail::Low, + runtime::ImageDetail::High => ImageDetail::High, + runtime::ImageDetail::Original => ImageDetail::Original, + }), + } + } } } diff --git a/codex-rs/code-mode/src/service_contract_tests.rs b/codex-rs/code-mode/src/service_contract_tests.rs index 3d814e98cc..9484310323 100644 --- a/codex-rs/code-mode/src/service_contract_tests.rs +++ b/codex-rs/code-mode/src/service_contract_tests.rs @@ -288,8 +288,20 @@ async fn observed_natural_completion_wins_over_termination() { ); tokio::time::timeout(Duration::from_secs(1), async { loop { - if service.inner.stored_values.lock().await.get("finished") - == Some(&serde_json::json!(true)) + let response = service + .execute(execute_request(r#"text(String(load("finished")));"#)) + .await + .unwrap() + .initial_response() + .await + .unwrap(); + let RuntimeResponse::Result { content_items, .. } = response else { + panic!("expected stored-value probe to complete"); + }; + if content_items + == vec![FunctionCallOutputContentItem::InputText { + text: "true".to_string(), + }] { break; } diff --git a/codex-rs/code-mode/src/service_tests.rs b/codex-rs/code-mode/src/service_tests.rs index 688bd89cd2..d90d4b6afb 100644 --- a/codex-rs/code-mode/src/service_tests.rs +++ b/codex-rs/code-mode/src/service_tests.rs @@ -1,5 +1,4 @@ use std::sync::Arc; -use std::sync::atomic::Ordering; use std::time::Duration; use super::CellId; @@ -7,7 +6,6 @@ use super::CodeModeNestedToolCall; use super::CodeModeService; use super::CodeModeSessionDelegate; use super::NotificationFuture; -use super::ObserveMode; use super::RuntimeResponse; use super::ToolInvocationFuture; use super::WaitOutcome; @@ -216,20 +214,15 @@ async fn shutdown_interrupts_cpu_bound_cells() { #[tokio::test] async fn start_cell_rejects_new_cell_after_shutdown_begins() { let service = CodeModeService::new(); - service.inner.shutting_down.store(true, Ordering::Release); + service.shutdown().await.unwrap(); let error = service - .start_cell( - cell_id("late-cell"), - execute_request(""), - ObserveMode::YieldAfter(Duration::from_millis(1)), - ) + .execute(execute_request("text('late');")) .await .err() .unwrap(); assert_eq!(error, "code mode session is shutting down".to_string()); - assert!(service.inner.cells.lock().await.is_empty()); } #[tokio::test] diff --git a/codex-rs/code-mode/src/session_runtime/mod.rs b/codex-rs/code-mode/src/session_runtime/mod.rs new file mode 100644 index 0000000000..51532aa95b --- /dev/null +++ b/codex-rs/code-mode/src/session_runtime/mod.rs @@ -0,0 +1,283 @@ +mod types; + +use std::collections::HashMap; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::AtomicU64; +use std::sync::atomic::Ordering; + +use serde_json::Value as JsonValue; +use tokio::sync::Mutex; +use tokio_util::sync::CancellationToken; +use tracing::warn; + +pub use self::types::CellEvent; +pub use self::types::CellId; +pub use self::types::Error; +pub use self::types::ExecuteRequest; +pub use self::types::ImageDetail; +pub use self::types::NestedToolCall; +pub use self::types::ObserveMode; +pub use self::types::OutputItem; +pub use self::types::SessionRuntimeDelegate; +pub use self::types::ToolDefinition; +pub use self::types::ToolKind; +pub use self::types::ToolName; +use crate::cell_actor::CellActor; +use crate::cell_actor::CellError; +use crate::cell_actor::CellEventFuture; +use crate::cell_actor::CellHandle; +use crate::cell_actor::CellHost; +use crate::cell_actor::CellToolCall; + +type RuntimeEventFuture = Pin> + Send + 'static>>; + +/// Owns all cells and shared state for one transport-neutral code-mode session. +pub struct SessionRuntime { + inner: Arc>, +} + +struct Inner { + stored_values: Mutex>, + cells: Mutex>, + delegate: Arc, + shutting_down: AtomicBool, + next_cell_id: AtomicU64, +} + +impl SessionRuntime { + pub fn new(delegate: Arc) -> Self { + Self { + inner: Arc::new(Inner { + stored_values: Mutex::new(HashMap::new()), + cells: Mutex::new(HashMap::new()), + delegate, + shutting_down: AtomicBool::new(false), + next_cell_id: AtomicU64::new(1), + }), + } + } + + pub fn is_alive(&self) -> bool { + !self.inner.shutting_down.load(Ordering::Acquire) + } + + pub async fn execute( + &self, + request: ExecuteRequest, + initial_observe_mode: ObserveMode, + ) -> Result { + if self.inner.shutting_down.load(Ordering::Acquire) { + return Err(Error::ShuttingDown); + } + let cell_id = self.allocate_cell_id(); + let initial_event = self + .start_cell(cell_id.clone(), request, initial_observe_mode) + .await?; + Ok(StartedCell { + cell_id, + initial_event, + }) + } + + pub async fn observe(&self, cell_id: &CellId, mode: ObserveMode) -> Result { + self.begin_observe(cell_id, mode).await?.event().await + } + + pub async fn begin_observe( + &self, + cell_id: &CellId, + mode: ObserveMode, + ) -> Result { + let handle = self + .inner + .cells + .lock() + .await + .get(cell_id) + .cloned() + .ok_or_else(|| Error::MissingCell(cell_id.clone()))?; + Ok(PendingEvent { + event: map_actor_event(cell_id.clone(), handle.observe(mode)), + }) + } + + pub async fn terminate(&self, cell_id: &CellId) -> Result { + let handle = self + .inner + .cells + .lock() + .await + .get(cell_id) + .cloned() + .ok_or_else(|| Error::MissingCell(cell_id.clone()))?; + handle + .terminate() + .await + .map_err(|error| actor_error(cell_id, error)) + } + + pub async fn shutdown(&self) -> Result<(), Error> { + self.inner.shutting_down.store(true, Ordering::Release); + let handles = self + .inner + .cells + .lock() + .await + .values() + .cloned() + .collect::>(); + for handle in handles { + handle.shutdown(); + } + while !self.inner.cells.lock().await.is_empty() { + tokio::task::yield_now().await; + } + Ok(()) + } + + fn allocate_cell_id(&self) -> CellId { + CellId::new( + self.inner + .next_cell_id + .fetch_add(1, Ordering::Relaxed) + .to_string(), + ) + } + + async fn start_cell( + &self, + cell_id: CellId, + request: ExecuteRequest, + initial_observe_mode: ObserveMode, + ) -> Result { + let stored_values = self.inner.stored_values.lock().await.clone(); + let host = Arc::new(RuntimeCellHost { + cell_id: cell_id.clone(), + inner: Arc::clone(&self.inner), + }); + let mut cells = self.inner.cells.lock().await; + if self.inner.shutting_down.load(Ordering::Acquire) { + return Err(Error::ShuttingDown); + } + if cells.contains_key(&cell_id) { + return Err(Error::DuplicateCell(cell_id)); + } + let (handle, initial_event, task) = + CellActor::prepare(request, stored_values, host, initial_observe_mode) + .map_err(Error::Runtime)?; + cells.insert(cell_id.clone(), handle); + drop(cells); + tokio::spawn(task); + Ok(map_actor_event(cell_id, initial_event)) + } + + fn begin_shutdown(&self) { + self.inner.shutting_down.store(true, Ordering::Release); + if let Ok(cells) = self.inner.cells.try_lock() { + for handle in cells.values() { + handle.shutdown(); + } + } + } +} + +impl Drop for SessionRuntime { + fn drop(&mut self) { + self.begin_shutdown(); + } +} + +/// A cell admitted by [`SessionRuntime::execute`]. +pub struct StartedCell { + pub cell_id: CellId, + initial_event: RuntimeEventFuture, +} + +/// An admitted observation that has not reached its requested frontier yet. +pub struct PendingEvent { + event: RuntimeEventFuture, +} + +impl PendingEvent { + pub async fn event(self) -> Result { + self.event.await + } +} + +impl StartedCell { + pub async fn initial_event(self) -> Result { + self.initial_event.await + } +} + +struct RuntimeCellHost { + cell_id: CellId, + inner: Arc>, +} + +impl CellHost for RuntimeCellHost { + async fn invoke_tool( + &self, + invocation: CellToolCall, + cancellation_token: CancellationToken, + ) -> Result { + self.inner + .delegate + .invoke_tool( + NestedToolCall { + cell_id: self.cell_id.clone(), + runtime_tool_call_id: invocation.id, + tool_name: invocation.name, + tool_kind: invocation.kind, + input: invocation.input, + }, + cancellation_token, + ) + .await + } + + async fn notify( + &self, + call_id: String, + text: String, + cancellation_token: CancellationToken, + ) -> Result<(), String> { + self.inner + .delegate + .notify(call_id, self.cell_id.clone(), text, cancellation_token) + .await + } + + async fn commit_stored_values(&self, stored_value_writes: HashMap) { + self.inner + .stored_values + .lock() + .await + .extend(stored_value_writes); + } + + async fn closed(&self) { + if let Err(err) = self.inner.delegate.cell_closed(&self.cell_id).await { + warn!( + "failed to close code mode delegate state for cell {}: {err}", + self.cell_id + ); + } + self.inner.cells.lock().await.remove(&self.cell_id); + } +} + +fn map_actor_event(cell_id: CellId, event: CellEventFuture) -> RuntimeEventFuture { + Box::pin(async move { event.await.map_err(|error| actor_error(&cell_id, error)) }) +} + +fn actor_error(cell_id: &CellId, error: CellError) -> Error { + match error { + CellError::Busy => Error::BusyObserver(cell_id.clone()), + CellError::AlreadyTerminating => Error::AlreadyTerminating(cell_id.clone()), + CellError::Closed => Error::MissingCell(cell_id.clone()), + } +} diff --git a/codex-rs/code-mode/src/session_runtime/types.rs b/codex-rs/code-mode/src/session_runtime/types.rs new file mode 100644 index 0000000000..db3eaca227 --- /dev/null +++ b/codex-rs/code-mode/src/session_runtime/types.rs @@ -0,0 +1,166 @@ +use std::fmt; +use std::future::Future; +use std::time::Duration; + +use serde_json::Value as JsonValue; +use tokio_util::sync::CancellationToken; + +/// Identifies one execution cell within a session runtime. +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct CellId(String); + +impl CellId { + pub fn new(value: impl Into) -> Self { + Self(value.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +impl fmt::Display for CellId { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(self.as_str()) + } +} + +/// Selects the next observable frontier for a running cell. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ObserveMode { + YieldAfter(Duration), + PendingFrontier, +} + +/// An observable cell lifecycle event. +#[derive(Clone, Debug, PartialEq)] +pub enum CellEvent { + Yielded { + content_items: Vec, + }, + Pending { + content_items: Vec, + pending_tool_call_ids: Vec, + }, + Completed { + content_items: Vec, + error_text: Option, + }, + Terminated { + content_items: Vec, + }, +} + +/// Output emitted by a cell since its preceding observation. +#[derive(Clone, Debug, PartialEq)] +pub enum OutputItem { + Text { + text: String, + }, + Image { + image_url: String, + detail: Option, + }, +} + +/// Requested image fidelity for an output image. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ImageDetail { + Auto, + Low, + High, + Original, +} + +/// Transport-neutral input for starting a cell. +pub struct ExecuteRequest { + pub tool_call_id: String, + pub enabled_tools: Vec, + pub source: String, +} + +/// Tool metadata exposed to code running inside a cell. +pub struct ToolDefinition { + pub name: String, + pub tool_name: ToolName, + pub description: String, + pub kind: ToolKind, +} + +/// A tool name with an optional namespace. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ToolName { + pub name: String, + pub namespace: Option, +} + +/// The JavaScript calling convention for a tool. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ToolKind { + Function, + Freeform, +} + +/// A nested tool request emitted by a running cell. +pub struct NestedToolCall { + pub cell_id: CellId, + pub runtime_tool_call_id: String, + pub tool_name: ToolName, + pub tool_kind: ToolKind, + pub input: Option, +} + +/// Host callbacks used by cells owned by a [`super::SessionRuntime`]. +/// +/// Implementations must honor cancellation tokens and must not return from +/// `cell_closed` until downstream routing can no longer target the cell. +pub trait SessionRuntimeDelegate: Send + Sync + 'static { + fn invoke_tool( + &self, + invocation: NestedToolCall, + cancellation_token: CancellationToken, + ) -> impl Future> + Send; + + fn notify( + &self, + call_id: String, + cell_id: CellId, + text: String, + cancellation_token: CancellationToken, + ) -> impl Future> + Send; + + fn cell_closed(&self, cell_id: &CellId) -> impl Future> + Send; +} + +/// A failure reported by a session runtime operation. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Error { + ShuttingDown, + DuplicateCell(CellId), + MissingCell(CellId), + BusyObserver(CellId), + AlreadyTerminating(CellId), + Runtime(String), +} + +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::ShuttingDown => formatter.write_str("code mode session is shutting down"), + Self::DuplicateCell(cell_id) => write!(formatter, "exec cell {cell_id} already exists"), + Self::MissingCell(cell_id) => write!(formatter, "exec cell {cell_id} not found"), + Self::BusyObserver(cell_id) => { + write!( + formatter, + "exec cell {cell_id} already has an active observer" + ) + } + Self::AlreadyTerminating(cell_id) => { + write!(formatter, "exec cell {cell_id} is already terminating") + } + Self::Runtime(error_text) => formatter.write_str(error_text), + } + } +} + +impl std::error::Error for Error {}