diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index fedfd070ea..2e557339b2 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2817,11 +2817,11 @@ version = "0.0.0" dependencies = [ "codex-code-mode-protocol", "codex-http-client", - "codex-install-context", "codex-otel", "codex-protocol", "futures", "http-body-util", + "opentelemetry", "pretty_assertions", "prost", "rand 0.9.3", @@ -2845,6 +2845,7 @@ dependencies = [ "codex-code-mode", "codex-code-mode-protocol", "codex-code-mode-runtime", + "codex-core", "codex-http-client", "codex-otel", "codex-otel-trace-websocket", diff --git a/codex-rs/code-mode-host/Cargo.toml b/codex-rs/code-mode-host/Cargo.toml index 7668df1157..80865b95ac 100644 --- a/codex-rs/code-mode-host/Cargo.toml +++ b/codex-rs/code-mode-host/Cargo.toml @@ -38,6 +38,7 @@ tracing-subscriber = { workspace = true } uuid = { workspace = true, features = ["v4"] } [dev-dependencies] +codex-core = { workspace = true } tokio = { workspace = true, features = ["test-util"] } codex-plugin-host = { workspace = true } codex-code-mode = { workspace = true } diff --git a/codex-rs/code-mode-host/src/delegate.rs b/codex-rs/code-mode-host/src/delegate.rs deleted file mode 100644 index 50d45e7de7..0000000000 --- a/codex-rs/code-mode-host/src/delegate.rs +++ /dev/null @@ -1,85 +0,0 @@ -use std::sync::Arc; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::CodeModeNestedToolCall; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::NotificationFuture; -use codex_code_mode_protocol::ToolInvocationFuture; -use codex_code_mode_protocol::host::DelegateRequest; -use codex_code_mode_protocol::host::DelegateResponse; -use codex_code_mode_protocol::host::SessionId; -use tokio_util::sync::CancellationToken; - -use crate::peer::HostPeer; - -pub(super) struct RemoteDelegate { - session_id: SessionId, - peer: Arc, -} - -impl RemoteDelegate { - pub(super) fn new(session_id: SessionId, peer: Arc) -> Self { - Self { session_id, peer } - } -} - -impl CodeModeSessionDelegate for RemoteDelegate { - fn invoke_tool<'a>( - &'a self, - invocation: CodeModeNestedToolCall, - cancellation_token: CancellationToken, - ) -> ToolInvocationFuture<'a> { - Box::pin(async move { - match self - .peer - .call( - self.session_id.clone(), - DelegateRequest::InvokeTool { - invocation: invocation.into(), - }, - cancellation_token, - ) - .await? - { - DelegateResponse::ToolResult { result } => Ok(result), - DelegateResponse::NotificationDelivered => { - Err("code-mode client returned an invalid tool result".to_string()) - } - } - }) - } - - fn notify<'a>( - &'a self, - call_id: String, - cell_id: CellId, - text: String, - cancellation_token: CancellationToken, - ) -> NotificationFuture<'a> { - Box::pin(async move { - match self - .peer - .call( - self.session_id.clone(), - DelegateRequest::Notify { - call_id, - cell_id: cell_id.into(), - text, - }, - cancellation_token, - ) - .await? - { - DelegateResponse::NotificationDelivered => Ok(()), - DelegateResponse::ToolResult { .. } => { - Err("code-mode client returned an invalid notification result".to_string()) - } - } - }) - } - - fn cell_closed(&self, cell_id: &CellId) { - self.peer - .close_cell(self.session_id.clone(), cell_id.clone()); - } -} diff --git a/codex-rs/code-mode-host/src/grpc/mod.rs b/codex-rs/code-mode-host/src/grpc/mod.rs index bdd902a258..e83458d6c3 100644 --- a/codex-rs/code-mode-host/src/grpc/mod.rs +++ b/codex-rs/code-mode-host/src/grpc/mod.rs @@ -71,7 +71,7 @@ fn trace_context_from_request(request: &Request) -> Option, - pub(super) tool_preloads: &'static [ToolPreload], + tool_preloads: &'static [ToolPreload], process_metrics: Option>, } diff --git a/codex-rs/code-mode-host/src/grpc/service_tests.rs b/codex-rs/code-mode-host/src/grpc/service_tests.rs index b1a782d26a..2b248915ba 100644 --- a/codex-rs/code-mode-host/src/grpc/service_tests.rs +++ b/codex-rs/code-mode-host/src/grpc/service_tests.rs @@ -2,6 +2,7 @@ use super::GrpcCodeModeHost; use super::GrpcStream; use codex_code_mode_protocol::grpc as proto; use codex_code_mode_protocol::grpc::code_mode_host_server::CodeModeHost; +use codex_code_mode_runtime::ToolPreload; use futures::FutureExt; use futures::StreamExt; use pretty_assertions::assert_eq; @@ -156,6 +157,58 @@ fn only_text(items: &[proto::ContentItem]) -> &str { &text.text } +#[tokio::test] +async fn preload_errors_preserve_other_sessions_on_the_host() { + let host = GrpcCodeModeHost::new().with_tool_preloads(&[ + ToolPreload { + tool: "first", + tool_name: "first", + tool_namespace: None, + source: "const shared = 1;", + }, + ToolPreload { + tool: "second", + tool_name: "second", + tool_namespace: None, + source: "const shared = 2;", + }, + ]); + let (broken, _broken_lease) = open_session(&host).await; + let (healthy, _healthy_lease) = open_session(&host).await; + let mut request = execute_request(&broken, "broken", "text('never runs')"); + request.enabled_tools = vec![tool("first"), tool("second")]; + for (mut request, expected_content, expected_error) in [ + ( + request, + Vec::new(), + Some("failed to execute tool preload #2 for `second`".to_string()), + ), + ( + execute_request(&healthy, "healthy", "text('still alive')"), + vec![proto::ContentItem { + item: Some(proto::content_item::Item::Text(proto::TextContent { + text: "still alive".to_string(), + })), + }], + None, + ), + ] { + request.yield_time_ms = Some(60_000); + let (_, mut events) = execute_events(&host, request).await; + let event = events.next().await.unwrap().unwrap(); + let Some(proto::execute_event::Event::Outcome(outcome)) = event.event else { + panic!("expected cell outcome"); + }; + let Some(proto::execution_outcome::Outcome::Completed(completed)) = outcome.outcome else { + panic!("expected cell completion"); + }; + assert_eq!( + (outcome.content_items, completed.error_text), + (expected_content, expected_error) + ); + } +} + #[tokio::test] async fn execute_stream_starts_immediately_and_wait_preserves_missing_cells() { let host = GrpcCodeModeHost::new(); diff --git a/codex-rs/code-mode-host/src/host_tests.rs b/codex-rs/code-mode-host/src/host_tests.rs deleted file mode 100644 index de7523f318..0000000000 --- a/codex-rs/code-mode-host/src/host_tests.rs +++ /dev/null @@ -1,992 +0,0 @@ -use std::collections::HashMap; -use std::pin::Pin; -use std::process::Stdio; -use std::sync::Arc; -use std::sync::Mutex; -use std::sync::PoisonError; -use std::sync::atomic::AtomicBool; -use std::task::Context; -use std::task::Poll; -use std::time::Duration; -use std::time::Instant; - -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::CodeModeToolKind; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::ToolDefinition; -use codex_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY; -use codex_code_mode_protocol::host::Capability; -use codex_code_mode_protocol::host::CapabilitySet; -use codex_code_mode_protocol::host::ClientHello; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::FramedReader; -use codex_code_mode_protocol::host::FramedWriter; -use codex_code_mode_protocol::host::HandshakeRejectReason; -use codex_code_mode_protocol::host::HostHello; -use codex_code_mode_protocol::host::HostRequest; -use codex_code_mode_protocol::host::HostResponse; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::LARGE_DELEGATE_CAPACITY_CAPABILITY; -use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::ProtocolVersion; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SESSION_RESOURCE_LIMITS_CAPABILITY; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::SupportedProtocolVersions; -use codex_code_mode_protocol::host::TOOL_CATALOG_CAPABILITY; -use codex_code_mode_protocol::host::WireContentItem; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_code_mode_protocol::host::WireOutputTruncation; -use codex_code_mode_protocol::host::WireResult; -use codex_code_mode_protocol::host::WireRuntimeResponse; -use codex_code_mode_protocol::host::WireWaitOutcome; -use codex_code_mode_protocol::host::WireWaitRequest; -use codex_code_mode_runtime::ToolPreload; -use codex_protocol::ToolName; -use pretty_assertions::assert_eq; -use tokio::io::AsyncBufReadExt; -use tokio::io::AsyncRead; -use tokio::io::AsyncWrite; -use tokio::io::BufReader; -use tokio::sync::Semaphore; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio_util::sync::CancellationToken; -use tokio_util::task::TaskTracker; - -use super::HostLimits; -use super::HostState; -use super::MAX_IN_FLIGHT_REQUESTS; -use super::MAX_RECENT_REQUEST_IDS; -use super::RequestKind; -use super::RequestRegistry; -use super::SeenSessionIds; -use super::peer::HostPeer; -use super::run; - -fn client_hello( - versions: impl IntoIterator, - required_capabilities: CapabilitySet, -) -> ClientToHost { - ClientToHost::ClientHello( - ClientHello::new( - SupportedProtocolVersions::try_new(versions).expect("supported versions"), - required_capabilities, - CapabilitySet::empty(), - ) - .expect("client hello"), - ) -} - -fn session_id(value: &str) -> SessionId { - SessionId::new(value).expect("session ID") -} - -fn request_id(value: i64) -> RequestId { - RequestId::new(value) -} - -async fn decode_frame(frame: EncodedFrame) -> HostToClient { - let (reader, writer) = tokio::io::duplex(/*max_buf_size*/ 4096); - let writer = tokio::spawn(async move { - FramedWriter::new(writer) - .write_frame(&frame) - .await - .expect("write encoded frame"); - }); - let message = FramedReader::new(reader) - .read() - .await - .expect("read encoded frame") - .expect("encoded frame message"); - writer.await.expect("frame writer task"); - message -} - -fn execute_request(source: &str) -> WireExecuteRequest { - WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: source.to_string(), - yield_time_ms: Some(60_000), - max_output_tokens: Some(1_000), - } -} - -async fn next_response(reader: &mut FramedReader) -> HostToClient { - loop { - match reader - .read() - .await - .expect("host response") - .expect("host connected") - { - HostToClient::CellClosed { .. } => {} - message => return message, - } - } -} - -#[tokio::test] -async fn handshake_and_multiple_session_lifecycles_are_ordered() { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 4096); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("write hello"); - assert_eq!( - reader.read::().await.expect("read hello"), - Some(HostToClient::HostHello(HostHello::new( - ProtocolVersion::V1, - CapabilitySet::empty(), - ))) - ); - - for (request_id, id) in [ - (request_id(/*value*/ 1), "session-1"), - (request_id(/*value*/ 2), "session-2"), - ] { - writer - .write(&ClientToHost::Request { - id: request_id, - request: HostRequest::OpenSession { - session_id: session_id(id), - cell_execution_limits: None, - output_truncation: None, - }, - }) - .await - .expect("open session"); - assert_eq!( - reader.read::().await.expect("session ready"), - Some(HostToClient::Response { - id: request_id, - result: WireResult::Ok { - value: HostResponse::SessionReady { - session_id: session_id(id), - }, - }, - }) - ); - } - - for (request_id, id) in [ - (request_id(/*value*/ 3), "session-1"), - (request_id(/*value*/ 4), "session-2"), - ] { - writer - .write(&ClientToHost::Request { - id: request_id, - request: HostRequest::ShutdownSession { - session_id: session_id(id), - }, - }) - .await - .expect("shutdown session"); - assert_eq!( - reader.read::().await.expect("session closed"), - Some(HostToClient::Response { - id: request_id, - result: WireResult::Ok { - value: HostResponse::SessionClosed { - session_id: session_id(id), - }, - }, - }) - ); - } - - drop(writer); - drop(reader); - host.await.expect("host task").expect("host connection"); -} - -const PRELOAD_STDIO_CHILD: &str = "CODE_MODE_HOST_PRELOAD_STDIO_CHILD"; - -#[tokio::test] -#[ignore = "stdio subprocess for preload_errors_preserve_other_sessions_on_the_connection"] -async fn preload_stdio_child() { - if std::env::var_os(PRELOAD_STDIO_CHILD).is_none() { - return; - } - super::run_stdio_with_tool_preloads(&[ - ToolPreload { - tool: "first", - tool_name: "first", - tool_namespace: None, - source: "const shared = 1;", - }, - ToolPreload { - tool: "second", - tool_name: "second", - tool_namespace: None, - source: "const shared = 2;", - }, - ]) - .await - .expect("stdio host"); -} - -#[tokio::test] -async fn preload_errors_preserve_other_sessions_on_the_connection() { - let mut command = - codex_utils_process::background_command(std::env::current_exe().expect("test executable")); - command - .args([ - "--quiet", - "--exact", - "tests::preload_stdio_child", - "--ignored", - "--nocapture", - ]) - .env(PRELOAD_STDIO_CHILD, "1") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()); - let mut child = tokio::process::Command::from(command) - .kill_on_drop(/*kill_on_drop*/ true) - .spawn() - .expect("spawn stdio host"); - let mut stdout = BufReader::new(child.stdout.take().expect("host stdout")); - // libtest emits its banner before entering the child; subsequent bytes must be frames. - let mut banner = String::new(); - for _ in 0..2 { - banner.clear(); - stdout.read_line(&mut banner).await.expect("libtest banner"); - if banner.trim() == "running 1 test" { - break; - } - } - assert_eq!(banner.trim(), "running 1 test"); - let mut reader = FramedReader::new(stdout); - let mut writer = FramedWriter::new(child.stdin.take().expect("host stdin")); - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("hello"); - assert!(matches!( - reader.read::().await.expect("host hello"), - Some(HostToClient::HostHello(_)) - )); - for (id, name) in [(1, "broken"), (2, "healthy")] { - writer - .write(&ClientToHost::Request { - id: request_id(id), - request: HostRequest::OpenSession { - session_id: session_id(name), - cell_execution_limits: None, - output_truncation: None, - }, - }) - .await - .expect("open session"); - assert!(matches!( - next_response(&mut reader).await, - HostToClient::Response { - id: actual_id, - result: WireResult::Ok { value: HostResponse::SessionReady { .. } }, - } if actual_id == request_id(id) - )); - } - let mut broken = execute_request("text('never runs')"); - broken.enabled_tools = ["first", "second"] - .into_iter() - .map(|name| { - ToolDefinition { - name: name.to_string(), - tool_name: ToolName::plain(name), - description: String::new(), - kind: CodeModeToolKind::Function, - input_schema: None, - input_schema_max_bytes: None, - output_schema: None, - } - .into() - }) - .collect(); - for (id, name, request, expected_content, expected_error) in [ - ( - 3, - "broken", - broken, - Vec::new(), - Some("failed to execute tool preload #2 for `second`".to_string()), - ), - ( - 4, - "healthy", - execute_request("text('still alive')"), - vec![WireContentItem::InputText { - text: "still alive".to_string(), - }], - None, - ), - ] { - writer - .write(&ClientToHost::Request { - id: request_id(id), - request: HostRequest::Execute { - session_id: session_id(name), - request, - }, - }) - .await - .expect("execute"); - assert!(matches!( - next_response(&mut reader).await, - HostToClient::Response { - id: actual_id, - result: WireResult::Ok { value: HostResponse::ExecutionStarted { .. } }, - } if actual_id == request_id(id) - )); - let HostToClient::InitialResponse { - id: actual_id, - result: - WireResult::Ok { - value: - WireRuntimeResponse::Result { - content_items, - error_text, - .. - }, - }, - } = next_response(&mut reader).await - else { - panic!("expected framed cell result"); - }; - assert_eq!(actual_id, request_id(id)); - assert_eq!( - (content_items, error_text), - (expected_content, expected_error) - ); - } - drop(writer); - assert!(child.wait().await.expect("host exit").success()); -} - -#[tokio::test] -async fn disconnect_cancels_a_backpressured_host_writer() { - let (host_reader, client_writer) = tokio::io::duplex(/*max_buf_size*/ 4096); - let (blocked_tx, blocked_rx) = oneshot::channel(); - let host = tokio::spawn(run( - host_reader, - BlockingWriter { - blocked_tx: Some(blocked_tx), - handshake_flushed: false, - }, - &[], - )); - let mut writer = FramedWriter::new(client_writer); - - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("write client hello"); - writer - .write(&ClientToHost::Request { - id: request_id(/*value*/ 1), - request: HostRequest::OpenSession { - session_id: session_id("backpressured-session"), - cell_execution_limits: None, - output_truncation: None, - }, - }) - .await - .expect("write session-open request"); - - tokio::time::timeout(Duration::from_secs(1), blocked_rx) - .await - .expect("host writer should reach backpressure") - .expect("host writer should report backpressure"); - - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("write invalid second client hello"); - - let error = tokio::time::timeout(Duration::from_secs(1), host) - .await - .expect("disconnect should cancel the backpressured writer") - .expect("host task should finish") - .expect_err("a second client hello should fail the connection"); - assert_eq!( - error.to_string(), - "received a second code-mode client hello" - ); -} - -struct BlockingWriter { - blocked_tx: Option>, - handshake_flushed: bool, -} - -impl AsyncWrite for BlockingWriter { - fn poll_write( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - bytes: &[u8], - ) -> Poll> { - if self.handshake_flushed { - if let Some(blocked_tx) = self.blocked_tx.take() { - let _ = blocked_tx.send(()); - } - Poll::Pending - } else { - Poll::Ready(Ok(bytes.len())) - } - } - - fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - self.handshake_flushed = true; - Poll::Ready(Ok(())) - } - - fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } -} - -#[tokio::test] -async fn supported_capabilities_are_negotiated_when_optional_or_required() { - for capability in [ - TOOL_CATALOG_CAPABILITY, - SESSION_RESOURCE_LIMITS_CAPABILITY, - BOUNDED_OUTPUT_CAPABILITY, - LARGE_DELEGATE_CAPACITY_CAPABILITY, - ] { - let capability = Capability::new(capability).expect("supported capability"); - let host_capabilities = - CapabilitySet::try_new([capability.clone()]).expect("host capabilities"); - - for (required, optional) in [ - ( - CapabilitySet::empty(), - CapabilitySet::try_new([capability]).expect("optional capabilities"), - ), - (host_capabilities.clone(), CapabilitySet::empty()), - ] { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 1024); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - - writer - .write(&ClientToHost::ClientHello( - ClientHello::new( - SupportedProtocolVersions::try_new([ProtocolVersion::V1]) - .expect("supported versions"), - required, - optional, - ) - .expect("client hello"), - )) - .await - .expect("write hello"); - assert_eq!( - reader.read::().await.expect("host hello"), - Some(HostToClient::HostHello(HostHello::new( - ProtocolVersion::V1, - host_capabilities.clone(), - ))) - ); - - drop(writer); - drop(reader); - host.await.expect("host task").expect("host connection"); - } - } -} - -#[tokio::test] -async fn output_selection_is_per_session() { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 4096); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("write hello"); - assert_eq!( - reader.read::().await.expect("host hello"), - Some(HostToClient::HostHello(HostHello::new( - ProtocolVersion::V1, - CapabilitySet::empty(), - ))) - ); - for (id, name, output_truncation) in [ - (1, "client", None), - (2, "server", Some(WireOutputTruncation::Server)), - ] { - writer - .write(&ClientToHost::Request { - id: request_id(id), - request: HostRequest::OpenSession { - session_id: session_id(name), - cell_execution_limits: None, - output_truncation, - }, - }) - .await - .expect("open session"); - assert_eq!( - reader.read::().await.expect("session ready"), - Some(HostToClient::Response { - id: request_id(id), - result: WireResult::Ok { - value: HostResponse::SessionReady { - session_id: session_id(name), - }, - }, - }) - ); - } - - for (id, name, expected_text, expected_wait) in [ - ( - 3, - "client", - "0123456789012345678901234567890123456789", - "01234567890123456789012345678901234567890123456789012345678901234567890123456789", - ), - ( - 4, - "server", - "Warning: truncated output (original token count: 10)\nTotal output lines: 1\n\n0123456789…5 tokens truncated…0123456789", - "Warning: truncated output (original token count: 20)\nTotal output lines: 1\n\n01234567890123…13 tokens truncated…67890123456789", - ), - ] { - let mut request = execute_request( - "text('0123456789'.repeat(4)); yield_control(); text('0123456789'.repeat(8));", - ); - request.max_output_tokens = Some(5); - writer - .write(&ClientToHost::Request { - id: request_id(id), - request: HostRequest::Execute { - session_id: session_id(name), - request, - }, - }) - .await - .expect("execute"); - // Admission is sent before start_cell can send its initial result. - assert!(matches!( - next_response(&mut reader).await, - HostToClient::Response { - id: actual_id, - result: WireResult::Ok { value: HostResponse::ExecutionStarted { .. } }, - } if actual_id == request_id(id) - )); - let HostToClient::InitialResponse { - id: actual_id, - result: - WireResult::Ok { - value: - WireRuntimeResponse::Yielded { - cell_id, - content_items, - .. - }, - }, - } = next_response(&mut reader).await - else { - panic!("expected initial yield"); - }; - assert_eq!(actual_id, request_id(id)); - assert_eq!( - content_items, - vec![WireContentItem::InputText { - text: expected_text.to_string() - }] - ); - writer - .write(&ClientToHost::Request { - id: request_id(id + 2), - request: HostRequest::Wait { - session_id: session_id(name), - request: WireWaitRequest { - cell_id, - yield_time_ms: 60_000, - }, - // A supplied budget only affects Server mode and belongs - // to this observation, not the preceding exec. - max_output_tokens: Some(7), - }, - }) - .await - .expect("wait"); - let HostToClient::Response { - id: actual_id, - result: - WireResult::Ok { - value: - HostResponse::WaitCompleted { - outcome: - WireWaitOutcome::LiveCell(WireRuntimeResponse::Result { - content_items, - error_text, - .. - }), - }, - }, - } = next_response(&mut reader).await - else { - panic!("expected final wait"); - }; - assert_eq!(actual_id, request_id(id + 2)); - assert_eq!(error_text, None); - assert_eq!( - content_items, - vec![WireContentItem::InputText { - text: expected_wait.to_string() - }] - ); - } - drop(writer); - drop(reader); - host.await.expect("host task").expect("host connection"); -} - -#[tokio::test] -async fn incompatible_or_invalid_handshake_is_rejected() { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 1024); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - let version_two = ProtocolVersion::new(/*value*/ 2).expect("protocol version"); - - writer - .write(&client_hello([version_two], CapabilitySet::empty())) - .await - .expect("write hello"); - assert_eq!( - reader.read::().await.expect("rejection"), - Some(HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::NoCompatibleVersion { - supported_versions: SupportedProtocolVersions::try_new([ProtocolVersion::V1]) - .expect("host versions"), - }, - }) - ); - host.await.expect("host task").expect("host connection"); - - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 1024); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - writer - .write(&ClientToHost::Request { - id: request_id(/*value*/ 1), - request: HostRequest::OpenSession { - session_id: session_id("session-1"), - cell_execution_limits: None, - output_truncation: None, - }, - }) - .await - .expect("write invalid first message"); - assert_eq!( - reader.read::().await.expect("rejection"), - Some(HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::InvalidHello { - message: "first message must be connection/hello".to_string(), - }, - }) - ); - host.await.expect("host task").expect("host connection"); -} - -#[tokio::test] -async fn unsupported_required_capability_is_rejected() { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 1024); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - let capability = Capability::new("required").expect("capability"); - - writer - .write(&client_hello( - [ProtocolVersion::V1], - CapabilitySet::try_new([capability.clone()]).expect("capabilities"), - )) - .await - .expect("write hello"); - assert_eq!( - reader.read::().await.expect("rejection"), - Some(HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::MissingRequiredCapability { capability }, - }) - ); - host.await.expect("host task").expect("host connection"); -} - -#[tokio::test] -async fn session_id_cannot_be_reused_after_shutdown() { - let (host_stream, client_stream) = tokio::io::duplex(/*max_buf_size*/ 2048); - let (host_reader, host_writer) = tokio::io::split(host_stream); - let (client_reader, client_writer) = tokio::io::split(client_stream); - let host = tokio::spawn(run(host_reader, host_writer, &[])); - let mut reader = FramedReader::new(client_reader); - let mut writer = FramedWriter::new(client_writer); - writer - .write(&client_hello([ProtocolVersion::V1], CapabilitySet::empty())) - .await - .expect("write hello"); - reader - .read::() - .await - .expect("read hello") - .expect("host hello"); - - let id = session_id("session-1"); - for (request_id, request) in [ - ( - request_id(/*value*/ 1), - HostRequest::OpenSession { - session_id: id.clone(), - cell_execution_limits: None, - output_truncation: None, - }, - ), - ( - request_id(/*value*/ 2), - HostRequest::ShutdownSession { - session_id: id.clone(), - }, - ), - ] { - writer - .write(&ClientToHost::Request { - id: request_id, - request, - }) - .await - .expect("session request"); - reader - .read::() - .await - .expect("session response") - .expect("session response message"); - } - writer - .write(&ClientToHost::Request { - id: request_id(/*value*/ 3), - request: HostRequest::OpenSession { - session_id: id, - cell_execution_limits: None, - output_truncation: None, - }, - }) - .await - .expect("reuse session ID"); - assert_eq!( - reader.read::().await.expect("reuse response"), - Some(HostToClient::Response { - id: request_id(/*value*/ 3), - result: WireResult::Err { - message: "code-mode session ID `session-1` was reused".to_string(), - }, - }) - ); - drop(writer); - drop(reader); - host.await.expect("host task").expect("host connection"); -} - -#[test] -fn request_history_is_bounded() { - let mut requests = RequestRegistry::default(); - let duplicate = request_id(/*value*/ -1); - requests - .start(duplicate, RequestKind::OpenSession) - .expect("start duplicate probe"); - assert!(requests.start(duplicate, RequestKind::OpenSession).is_err()); - requests.finish(duplicate); - for value in 1..=MAX_RECENT_REQUEST_IDS as i64 + 100 { - let id = request_id(value); - requests - .start(id, RequestKind::Wait) - .expect("start request"); - requests.cancel(id); - requests.finish(id); - } - assert!(requests.active.is_empty()); - assert_eq!(requests.recent.len(), MAX_RECENT_REQUEST_IDS); - assert_eq!(requests.recent_order.len(), MAX_RECENT_REQUEST_IDS); -} - -#[tokio::test] -async fn request_task_panic_disconnects_host() { - let (outgoing_tx, _outgoing_rx) = mpsc::channel(/*max_capacity*/ 1); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - let state = HostState { - sessions: Mutex::new(HashMap::new()), - tool_preloads: &[], - limits: Arc::new(HostLimits::new()), - seen_session_ids: Mutex::new(SeenSessionIds::default()), - requests: Mutex::new(RequestRegistry::default()), - request_tasks: TaskTracker::new(), - closing: AtomicBool::new(false), - peer: Arc::clone(&peer), - }; - let task = state.request_tasks.spawn(async { - panic!("request panic probe"); - }); - state.supervise_request_task(task); - - tokio::time::timeout(Duration::from_secs(1), peer.disconnected()) - .await - .expect("request panic should disconnect host"); - assert!( - peer.failure() - .expect("request failure") - .contains("request task failed") - ); -} - -#[tokio::test] -async fn execute_request_id_remains_active_until_initial_response() { - let (outgoing_tx, mut outgoing_rx) = mpsc::channel(/*max_capacity*/ 4); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - let state = Arc::new(HostState { - sessions: Mutex::new(HashMap::new()), - tool_preloads: &[], - limits: Arc::new(HostLimits::new()), - seen_session_ids: Mutex::new(SeenSessionIds::default()), - requests: Mutex::new(RequestRegistry::default()), - request_tasks: TaskTracker::new(), - closing: AtomicBool::new(false), - peer, - }); - let session_id = session_id("session-1"); - state - .open_session(session_id.clone(), CodeModeSessionOptions::default()) - .expect("open session"); - let request_id = request_id(/*value*/ 1); - - state - .spawn_request( - request_id, - HostRequest::Execute { - session_id: session_id.clone(), - request: execute_request("await new Promise(() => {});"), - }, - ) - .expect("spawn execute request"); - let started = decode_frame(outgoing_rx.recv().await.expect("execution started frame")).await; - let HostToClient::Response { - id, - result: - WireResult::Ok { - value: HostResponse::ExecutionStarted { cell_id }, - }, - } = started - else { - panic!("expected execution started response"); - }; - assert_eq!(id, request_id); - assert!( - state - .requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .active - .contains_key(&request_id) - ); - - state - .session(&session_id) - .expect("session") - .terminate(TerminateRequest { - cell_id: cell_id.into(), - max_output_tokens: None, - }) - .await - .expect("terminate cell"); - state.disconnect().await; -} - -#[tokio::test] -async fn active_cell_limit_rejects_execute_without_disconnecting() { - let (outgoing_tx, mut outgoing_rx) = mpsc::channel(/*max_capacity*/ 1); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - let state = HostState { - sessions: Mutex::new(HashMap::new()), - tool_preloads: &[], - limits: Arc::new(HostLimits { - request_permits: Arc::new(Semaphore::new(MAX_IN_FLIGHT_REQUESTS)), - active_cell_permits: Arc::new(Semaphore::new(/*permits*/ 0)), - }), - seen_session_ids: Mutex::new(SeenSessionIds::default()), - requests: Mutex::new(RequestRegistry::default()), - request_tasks: TaskTracker::new(), - closing: AtomicBool::new(false), - peer: Arc::clone(&peer), - }; - let session_id = session_id("session-1"); - state - .open_session(session_id.clone(), CodeModeSessionOptions::default()) - .expect("open session"); - let request_id = request_id(/*value*/ 1); - - state - .handle_request( - request_id, - HostRequest::Execute { - session_id, - request: execute_request("text(\"hello\");"), - }, - CancellationToken::new(), - CancellationToken::new(), - Instant::now(), - ) - .await; - - assert_eq!( - decode_frame(outgoing_rx.recv().await.expect("execute response frame")).await, - HostToClient::Response { - id: request_id, - result: WireResult::Err { - message: "code-mode host has too many active cells".to_string(), - }, - } - ); - assert!(!peer.is_disconnected()); - state.disconnect().await; -} - -#[tokio::test] -async fn cell_forwarding_panic_disconnects_host() { - let (outgoing_tx, _outgoing_rx) = mpsc::channel(/*max_capacity*/ 1); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - peer.spawn_critical("cell forwarding", async { - panic!("cell forwarding panic probe"); - }); - - tokio::time::timeout(Duration::from_secs(1), peer.disconnected()) - .await - .expect("cell panic should disconnect host"); - assert!( - peer.failure() - .expect("cell failure") - .contains("cell forwarding task failed") - ); -} diff --git a/codex-rs/code-mode-host/src/lib.rs b/codex-rs/code-mode-host/src/lib.rs index 77038c0944..9731a5cb71 100644 --- a/codex-rs/code-mode-host/src/lib.rs +++ b/codex-rs/code-mode-host/src/lib.rs @@ -1,68 +1,22 @@ -use std::collections::HashMap; -use std::collections::HashSet; -use std::collections::VecDeque; -use std::sync::Arc; -use std::sync::Mutex; -use std::sync::PoisonError; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; -use std::time::Duration; -use std::time::Instant; +//! Runs the gRPC code-mode host over stdio or TCP. + +use std::sync::Arc; -use anyhow::Context; use anyhow::Result; -use codex_code_mode_protocol::CodeModeOutputTruncation; -use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY; -use codex_code_mode_protocol::host::Capability; -use codex_code_mode_protocol::host::CapabilitySet; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::FramedReader; -use codex_code_mode_protocol::host::FramedWriter; -use codex_code_mode_protocol::host::HandshakeRejectReason; -use codex_code_mode_protocol::host::HostHello; -use codex_code_mode_protocol::host::HostRequest; -use codex_code_mode_protocol::host::HostResponse; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::LARGE_DELEGATE_CAPACITY_CAPABILITY; -use codex_code_mode_protocol::host::LEGACY_MAX_PENDING_DELEGATE_CALLS; use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::ProtocolVersion; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SESSION_RESOURCE_LIMITS_CAPABILITY; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::SupportedProtocolVersions; -use codex_code_mode_protocol::host::TOOL_CATALOG_CAPABILITY; -use codex_code_mode_protocol::host::WireWaitOutcome; -use codex_code_mode_protocol::host::YIELD_OBSERVATION_CAPABILITY; -use codex_code_mode_runtime::InProcessCodeModeSession; use codex_code_mode_runtime::ToolPreload; -use tokio::io::AsyncRead; -use tokio::io::AsyncWrite; use tokio::sync::OwnedSemaphorePermit; use tokio::sync::Semaphore; use tokio::sync::TryAcquireError; -use tokio::sync::mpsc; -use tokio_util::sync::CancellationToken; -use tokio_util::task::TaskTracker; - -use self::delegate::RemoteDelegate; -use self::peer::HostPeer; pub use self::grpc::GrpcCodeModeHost; pub use self::process_metrics::CodeModeProcessMetricsCollector; pub use self::transport::DEFAULT_LISTEN_URL; pub use codex_code_mode_protocol::CodeModeProcessMetrics; -mod delegate; mod grpc; mod grpc_stdio; mod grpc_transport; -mod peer; mod process_metrics; mod transport; @@ -72,16 +26,9 @@ const MAX_ACTIVE_CELLS: usize = 2_048; // Leave observation and control headroom during a full callback-completion burst. const MAX_IN_FLIGHT_REQUESTS: usize = MAX_PENDING_DELEGATE_CALLS * 2; const MAX_RECENT_REQUEST_IDS: usize = 4096; -const MAX_RECENT_SESSION_IDS: usize = 4096; -const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5); // Allow a full callback-completion burst plus concurrent control messages. const OUTGOING_CHANNEL_CAPACITY: usize = MAX_PENDING_DELEGATE_CALLS * 2; -enum NegotiatedConnection { - Rejected, - Accepted { max_pending_delegate_calls: usize }, -} - struct HostLimits { request_permits: Arc, active_cell_permits: Arc, @@ -104,7 +51,7 @@ impl HostLimits { } } -/// Runs the code-mode host on its configured stdio or gRPC transport. +/// Runs the code-mode host over gRPC on the configured endpoint. pub async fn run_main(listen_url: &str) -> Result<()> { run_main_with_tool_preloads(listen_url, &[]).await } @@ -128,7 +75,6 @@ pub async fn run_main_with_tool_preloads( } /// Runs with an optional gRPC metrics collector initialized before sandbox entry. -/// The legacy stdio transport remains unchanged. pub async fn run_main_with_process_metrics( listen_url: &str, collector: CodeModeProcessMetricsCollector, @@ -139,686 +85,3 @@ pub async fn run_main_with_process_metrics( ) .await } - -/// Runs one code-mode host connection over the process standard streams. -pub async fn run_stdio() -> Result<()> { - run_stdio_with_tool_preloads(&[]).await -} - -async fn run_stdio_with_tool_preloads(tool_preloads: &'static [ToolPreload]) -> Result<()> { - run(tokio::io::stdin(), tokio::io::stdout(), tool_preloads).await -} - -/// Runs one code-mode host connection over an ordered input/output pair. -async fn run(reader: R, writer: W, tool_preloads: &'static [ToolPreload]) -> Result<()> -where - R: AsyncRead + Send + Unpin + 'static, - W: AsyncWrite + Send + Unpin + 'static, -{ - let mut reader = FramedReader::new(reader); - let mut writer = FramedWriter::new(writer); - let max_pending_delegate_calls = match negotiate(&mut reader, &mut writer).await? { - NegotiatedConnection::Rejected => return Ok(()), - NegotiatedConnection::Accepted { - max_pending_delegate_calls, - } => max_pending_delegate_calls, - }; - let (outgoing_tx, outgoing_rx) = mpsc::channel::(OUTGOING_CHANNEL_CAPACITY); - let peer = Arc::new(HostPeer::new(outgoing_tx, max_pending_delegate_calls)); - let state = Arc::new(HostState { - sessions: Mutex::new(HashMap::new()), - tool_preloads, - limits: Arc::new(HostLimits::new()), - seen_session_ids: Mutex::new(SeenSessionIds::default()), - requests: Mutex::new(RequestRegistry::default()), - request_tasks: TaskTracker::new(), - closing: AtomicBool::new(false), - peer: Arc::clone(&peer), - }); - let writer_disconnected = peer.disconnection_token(); - let writer_task = - tokio::spawn(async move { drive_writer(writer, outgoing_rx, writer_disconnected).await }); - let writer_peer = Arc::clone(&peer); - let writer_supervisor = tokio::spawn(async move { - match writer_task.await { - Ok(Ok(())) if !writer_peer.is_disconnected() => { - writer_peer.fail("code-mode writer task exited unexpectedly".to_string()); - } - Ok(Ok(())) => {} - Ok(Err(err)) => { - writer_peer.fail(format!("code-mode writer task failed: {err:#}")); - } - Err(err) => { - writer_peer.fail(format!("code-mode writer task failed: {err}")); - } - } - }); - - let input_result = async { - loop { - let message = tokio::select! { - biased; - _ = peer.disconnected() => break, - message = reader.read() => message.context("failed to read code-mode client message")?, - }; - let Some(message) = message else { - break; - }; - match message { - ClientToHost::ClientHello(_) => { - anyhow::bail!("received a second code-mode client hello"); - } - ClientToHost::Request { id, request } => { - state.spawn_request(id, request)?; - } - ClientToHost::CancelRequest { id } => { - state.cancel_request(id); - } - ClientToHost::YieldRequest { id } => { - state.yield_request(id); - } - ClientToHost::DelegateResponse { id, result } => { - peer.complete(id, result.into_result()).await; - } - } - } - Ok::<(), anyhow::Error>(()) - } - .await; - - peer.disconnect(); - if tokio::time::timeout(SHUTDOWN_TIMEOUT, state.disconnect()) - .await - .is_err() - { - peer.fail("timed out shutting down code-mode host state".to_string()); - } - drop(state); - tokio::time::timeout(SHUTDOWN_TIMEOUT, writer_supervisor) - .await - .context("timed out supervising code-mode writer task")? - .context("code-mode writer supervisor task failed")?; - let failure = peer.failure(); - drop(peer); - input_result?; - if let Some(failure) = failure { - anyhow::bail!(failure); - } - Ok(()) -} - -async fn drive_writer( - mut writer: FramedWriter, - mut outgoing: mpsc::Receiver, - disconnected: CancellationToken, -) -> Result<()> { - loop { - tokio::select! { - _ = disconnected.cancelled() => return Ok(()), - frame = outgoing.recv() => { - let Some(frame) = frame else { - return Ok(()); - }; - tokio::select! { - _ = disconnected.cancelled() => return Ok(()), - result = writer.write_frame(&frame) => { - result.context("failed to write code-mode host message")?; - } - } - } - } - } -} - -async fn negotiate( - reader: &mut FramedReader, - writer: &mut FramedWriter, -) -> Result { - let Some(first_message) = reader - .read() - .await - .context("failed to read code-mode client hello")? - else { - return Ok(NegotiatedConnection::Rejected); - }; - let ClientToHost::ClientHello(client_hello) = first_message else { - writer - .write(&HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::InvalidHello { - message: "first message must be connection/hello".to_string(), - }, - }) - .await - .context("failed to reject invalid code-mode client hello")?; - return Ok(NegotiatedConnection::Rejected); - }; - - let supported_versions = SupportedProtocolVersions::try_new([ProtocolVersion::V1])?; - if !client_hello - .supported_versions() - .contains(ProtocolVersion::V1) - { - writer - .write(&HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::NoCompatibleVersion { supported_versions }, - }) - .await - .context("failed to reject incompatible code-mode client")?; - return Ok(NegotiatedConnection::Rejected); - } - - let host_capabilities = CapabilitySet::try_new( - [ - SESSION_RESOURCE_LIMITS_CAPABILITY, - YIELD_OBSERVATION_CAPABILITY, - BOUNDED_OUTPUT_CAPABILITY, - LARGE_DELEGATE_CAPACITY_CAPABILITY, - TOOL_CATALOG_CAPABILITY, - ] - .into_iter() - .map(Capability::new) - .collect::, _>>()? - .into_iter() - .filter(|capability| { - client_hello.required_capabilities().contains(capability) - || client_hello.optional_capabilities().contains(capability) - }), - )?; - if let Some(capability) = client_hello - .required_capabilities() - .iter() - .find(|capability| !host_capabilities.contains(capability)) - { - writer - .write(&HostToClient::HandshakeRejected { - reason: HandshakeRejectReason::MissingRequiredCapability { - capability: capability.clone(), - }, - }) - .await - .context("failed to reject unsupported code-mode capability")?; - return Ok(NegotiatedConnection::Rejected); - } - - let max_pending_delegate_calls = - if host_capabilities.contains(&Capability::new(LARGE_DELEGATE_CAPACITY_CAPABILITY)?) { - MAX_PENDING_DELEGATE_CALLS - } else { - LEGACY_MAX_PENDING_DELEGATE_CALLS - }; - let hello = HostHello::new(ProtocolVersion::V1, host_capabilities); - writer - .write(&HostToClient::HostHello(hello)) - .await - .context("failed to write code-mode host hello")?; - Ok(NegotiatedConnection::Accepted { - max_pending_delegate_calls, - }) -} - -struct HostState { - sessions: Mutex>>, - tool_preloads: &'static [ToolPreload], - limits: Arc, - seen_session_ids: Mutex, - requests: Mutex, - request_tasks: TaskTracker, - closing: AtomicBool, - peer: Arc, -} - -impl HostState { - fn spawn_request( - self: &Arc, - request_id: RequestId, - request: HostRequest, - ) -> Result<(), anyhow::Error> { - // Include host admission and scheduling before the runtime observes the request. - let received_at = Instant::now(); - let request_kind = RequestKind::from(&request); - let (cancellation, yield_signal) = self - .requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .start(request_id, request_kind)?; - let Ok(permit) = self.limits.request_permit() else { - self.respond( - request_id, - Err("code-mode host has too many in-flight requests".to_string()), - ); - self.finish_request(request_id); - return Ok(()); - }; - let state = Arc::clone(self); - let request_task = self.request_tasks.spawn(async move { - let _permit = permit; - state - .handle_request(request_id, request, cancellation, yield_signal, received_at) - .await; - state.finish_request(request_id); - }); - self.supervise_request_task(request_task); - Ok(()) - } - - fn supervise_request_task(&self, task: tokio::task::JoinHandle<()>) { - let peer = Arc::clone(&self.peer); - tokio::spawn(async move { - if let Err(err) = task.await { - peer.fail(format!("code-mode request task failed: {err}")); - } - }); - } - - #[tracing::instrument( - name = "code_mode_host.request", - level = "info", - skip_all, - fields( - otel.name = "code_mode_host.request", - request.kind = RequestKind::from(&request).as_str(), - request.id = ?request_id, - call_id = tracing::field::Empty, - ) - )] - async fn handle_request( - &self, - request_id: RequestId, - request: HostRequest, - cancellation: CancellationToken, - yield_signal: CancellationToken, - received_at: Instant, - ) { - if let HostRequest::Execute { request, .. } = &request { - tracing::Span::current().record("call_id", request.tool_call_id.as_str()); - } - if self.closing.load(Ordering::Acquire) { - self.respond( - request_id, - Err("code-mode host is shutting down".to_string()), - ); - return; - } - match request { - HostRequest::OpenSession { - session_id, - cell_execution_limits, - output_truncation, - } => { - let result = CodeModeSessionCellExecutionLimits::try_from( - cell_execution_limits.unwrap_or_default(), - ) - .map_err(|error| format!("invalid code-mode session execution limits: {error}")) - .and_then(|limits| { - self.open_session( - session_id.clone(), - CodeModeSessionOptions { - cell_execution_limits: limits, - output_truncation: output_truncation - .map(CodeModeOutputTruncation::from) - .unwrap_or_default(), - ..Default::default() - }, - ) - }) - .map(|()| HostResponse::SessionReady { session_id }); - self.respond(request_id, result); - } - HostRequest::Execute { - session_id, - request, - } => { - if cancellation.is_cancelled() { - self.respond(request_id, Err("code-mode request cancelled".to_string())); - return; - } - let catalog_revision = request.catalog_revision; - let request = match request.try_into() { - Ok(request) => request, - Err(err) => { - self.respond( - request_id, - Err(format!("invalid code-mode execute request: {err}")), - ); - return; - } - }; - let session = match self.session(&session_id) { - Ok(session) => session, - Err(err) => { - self.respond(request_id, Err(err)); - return; - } - }; - let Ok(active_cell_permit) = self.limits.cell_permit() else { - self.respond( - request_id, - Err("code-mode host has too many active cells".to_string()), - ); - return; - }; - let delegate = Arc::new(RemoteDelegate::new( - session_id.clone(), - Arc::clone(&self.peer), - )); - let result = session - .execute_with_catalog(request, catalog_revision, delegate, Some(yield_signal)) - .await; - match result { - Ok(started) => { - let cell_id = started.cell_id.clone(); - self.respond( - request_id, - Ok(HostResponse::ExecutionStarted { - cell_id: cell_id.into(), - }), - ); - let initial_response_sent = self.peer.start_cell( - session_id, - request_id, - started, - active_cell_permit, - received_at, - ); - let _ = initial_response_sent.await; - } - Err(err) => self.respond(request_id, Err(err)), - } - } - HostRequest::Wait { - session_id, - request, - max_output_tokens, - } => { - let result = match self.session(&session_id) { - Ok(session) => { - tokio::select! { - biased; - _ = cancellation.cancelled() => { - Err("code-mode request cancelled".to_string()) - } - result = session.wait(WaitRequest { - max_output_tokens, - ..request.into() - }, Some(yield_signal)) => result.and_then(|outcome| { - let outcome = outcome.with_code_mode_host_duration(received_at.elapsed()); - Ok(HostResponse::WaitCompleted { - outcome: WireWaitOutcome::try_from(outcome) - .map_err(|error| error.to_string())?, - }) - }), - } - } - Err(err) => Err(err), - }; - self.respond(request_id, result); - } - HostRequest::Terminate { - session_id, - cell_id, - max_output_tokens, - } => { - let result = match self.session(&session_id) { - Ok(session) => session - .terminate(TerminateRequest { - cell_id: cell_id.into(), - max_output_tokens, - }) - .await - .and_then(|outcome| { - let outcome = - outcome.with_code_mode_host_duration(received_at.elapsed()); - Ok(HostResponse::WaitCompleted { - outcome: WireWaitOutcome::try_from(outcome) - .map_err(|error| error.to_string())?, - }) - }), - Err(err) => Err(err), - }; - self.respond(request_id, result); - } - HostRequest::ShutdownSession { session_id } => { - let session = self - .sessions - .lock() - .unwrap_or_else(PoisonError::into_inner) - .remove(&session_id); - let result = match session { - Some(session) => match session.shutdown().await { - Ok(()) => { - self.peer.wait_for_session_cells(&session_id).await; - Ok(HostResponse::SessionClosed { session_id }) - } - Err(err) => Err(err), - }, - None => Err(format!("unknown code-mode session {session_id}")), - }; - self.respond(request_id, result); - } - } - } - - fn open_session( - &self, - session_id: SessionId, - options: CodeModeSessionOptions, - ) -> Result<(), String> { - let mut sessions = self.sessions.lock().unwrap_or_else(PoisonError::into_inner); - if sessions.contains_key(&session_id) { - return Err(format!( - "code-mode session ID `{session_id}` is already open" - )); - } - if self.closing.load(Ordering::Acquire) { - return Err("code-mode host is shutting down".to_string()); - } - if !self - .seen_session_ids - .lock() - .unwrap_or_else(PoisonError::into_inner) - .remember(session_id.clone()) - { - return Err(format!("code-mode session ID `{session_id}` was reused")); - } - let peer = Arc::downgrade(&self.peer); - let task_failure_handler = Arc::new(move |reason| { - if let Some(peer) = peer.upgrade() { - peer.fail(reason); - } - }); - sessions.insert( - session_id, - Arc::new(InProcessCodeModeSession::with_options_and_tool_preloads( - options, - Some(task_failure_handler), - self.tool_preloads, - )), - ); - Ok(()) - } - - fn session(&self, session_id: &SessionId) -> Result, String> { - self.sessions - .lock() - .unwrap_or_else(PoisonError::into_inner) - .get(session_id) - .cloned() - .ok_or_else(|| format!("unknown code-mode session {session_id}")) - } - - fn respond(&self, id: RequestId, result: Result) { - self.peer.respond(id, result); - } - - fn cancel_request(&self, request_id: RequestId) { - self.requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .cancel(request_id); - } - - fn yield_request(&self, request_id: RequestId) { - self.requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .yield_observation(request_id); - } - - fn finish_request(&self, request_id: RequestId) { - self.requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .finish(request_id); - } - - async fn disconnect(&self) { - self.closing.store(true, Ordering::Release); - self.requests - .lock() - .unwrap_or_else(PoisonError::into_inner) - .cancel_all(); - self.request_tasks.close(); - self.request_tasks.wait().await; - let sessions = self - .sessions - .lock() - .unwrap_or_else(PoisonError::into_inner) - .drain() - .map(|(_, session)| session) - .collect::>(); - for session in sessions { - let _ = session.shutdown().await; - } - } -} - -#[derive(Clone, Copy)] -enum RequestKind { - OpenSession, - Execute, - Wait, - Terminate, - ShutdownSession, -} - -impl RequestKind { - fn from(request: &HostRequest) -> Self { - match request { - HostRequest::OpenSession { .. } => Self::OpenSession, - HostRequest::Execute { .. } => Self::Execute, - HostRequest::Wait { .. } => Self::Wait, - HostRequest::Terminate { .. } => Self::Terminate, - HostRequest::ShutdownSession { .. } => Self::ShutdownSession, - } - } - - fn is_cancellable(self) -> bool { - matches!(self, Self::Execute | Self::Wait) - } - - fn as_str(self) -> &'static str { - match self { - Self::OpenSession => "open_session", - Self::Execute => "execute", - Self::Wait => "wait", - Self::Terminate => "terminate", - Self::ShutdownSession => "shutdown_session", - } - } -} - -struct ActiveRequest { - kind: RequestKind, - cancellation: CancellationToken, - yield_signal: CancellationToken, -} - -#[derive(Default)] -struct RequestRegistry { - active: HashMap, - recent: HashSet, - recent_order: VecDeque, -} - -impl RequestRegistry { - fn start( - &mut self, - request_id: RequestId, - kind: RequestKind, - ) -> Result<(CancellationToken, CancellationToken), anyhow::Error> { - if self.active.contains_key(&request_id) || self.recent.contains(&request_id) { - anyhow::bail!("duplicate code-mode request ID {request_id:?}"); - } - let cancellation = CancellationToken::new(); - let yield_signal = CancellationToken::new(); - self.active.insert( - request_id, - ActiveRequest { - kind, - cancellation: cancellation.clone(), - yield_signal: yield_signal.clone(), - }, - ); - Ok((cancellation, yield_signal)) - } - - fn cancel(&mut self, request_id: RequestId) { - if let Some(request) = self.active.get(&request_id) - && request.kind.is_cancellable() - { - request.cancellation.cancel(); - } - } - - fn yield_observation(&self, request_id: RequestId) { - if let Some(request) = self.active.get(&request_id) - && matches!(request.kind, RequestKind::Execute | RequestKind::Wait) - { - request.yield_signal.cancel(); - } - } - - fn finish(&mut self, request_id: RequestId) { - if self.active.remove(&request_id).is_none() { - return; - } - self.recent.insert(request_id); - self.recent_order.push_back(request_id); - while self.recent_order.len() > MAX_RECENT_REQUEST_IDS { - if let Some(expired) = self.recent_order.pop_front() { - self.recent.remove(&expired); - } - } - } - - fn cancel_all(&self) { - for request in self.active.values() { - request.cancellation.cancel(); - } - } -} - -#[derive(Default)] -struct SeenSessionIds { - ids: HashSet, - order: VecDeque, -} - -impl SeenSessionIds { - fn remember(&mut self, session_id: SessionId) -> bool { - if !self.ids.insert(session_id.clone()) { - return false; - } - self.order.push_back(session_id); - while self.order.len() > MAX_RECENT_SESSION_IDS { - if let Some(expired) = self.order.pop_front() { - self.ids.remove(&expired); - } - } - true - } -} - -#[cfg(test)] -#[path = "host_tests.rs"] -mod tests; diff --git a/codex-rs/code-mode-host/src/main.rs b/codex-rs/code-mode-host/src/main.rs index a4c9187462..56d23db72f 100644 --- a/codex-rs/code-mode-host/src/main.rs +++ b/codex-rs/code-mode-host/src/main.rs @@ -25,7 +25,7 @@ struct Cli { )] codex_ui_plugin_process: bool, - /// Transport endpoint: `stdio`, `stdio://`, `grpc+stdio://`, or `grpc://IP:PORT`. + /// gRPC endpoint: `grpc+stdio://` (aliases `stdio` and `stdio://`) or `grpc://IP:PORT`. #[arg( long, value_name = "URL", diff --git a/codex-rs/code-mode-host/src/peer.rs b/codex-rs/code-mode-host/src/peer.rs deleted file mode 100644 index 2980dbebf6..0000000000 --- a/codex-rs/code-mode-host/src/peer.rs +++ /dev/null @@ -1,566 +0,0 @@ -use std::collections::HashMap; -use std::collections::VecDeque; -use std::sync::Arc; -use std::sync::Mutex as StdMutex; -use std::sync::PoisonError; -use std::sync::atomic::AtomicI64; -use std::sync::atomic::Ordering; -use std::time::Instant; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::host::DelegateRequest; -use codex_code_mode_protocol::host::DelegateRequestId; -use codex_code_mode_protocol::host::DelegateResponse; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireResult; -use codex_code_mode_protocol::host::WireRuntimeResponse; -use tokio::sync::Mutex; -use tokio::sync::Notify; -use tokio::sync::OwnedSemaphorePermit; -use tokio::sync::Semaphore; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio_util::sync::CancellationToken; - -const CELL_MESSAGE_CAPACITY: usize = 128; - -pub(super) struct HostPeer { - outgoing_tx: mpsc::Sender, - pending: Mutex>, - delegate_permits: Arc, - cell_routes: StdMutex>, - cell_routes_changed: Notify, - next_request_id: AtomicI64, - disconnected: CancellationToken, - failure: StdMutex>, -} - -struct PendingDelegate { - response_tx: oneshot::Sender>, - dispatched: bool, - _permit: OwnedSemaphorePermit, -} - -enum CellRoute { - Pending(VecDeque), - Active(mpsc::Sender), -} - -enum CellMessage { - Delegate { - id: DelegateRequestId, - request: DelegateRequest, - dispatched_tx: oneshot::Sender>, - }, - Closed, -} - -struct InitialRequest { - id: RequestId, - received_at: Instant, -} - -impl HostPeer { - pub(super) fn new( - outgoing_tx: mpsc::Sender, - max_pending_delegate_calls: usize, - ) -> Self { - Self { - outgoing_tx, - pending: Mutex::new(HashMap::new()), - delegate_permits: Arc::new(Semaphore::new(max_pending_delegate_calls)), - cell_routes: StdMutex::new(HashMap::new()), - cell_routes_changed: Notify::new(), - next_request_id: AtomicI64::new(1), - disconnected: CancellationToken::new(), - failure: StdMutex::new(None), - } - } - - pub(super) fn send(&self, message: HostToClient) -> Result<(), PeerSendError> { - let frame = EncodedFrame::encode(&message) - .map_err(|err| PeerSendError::Payload(err.to_string()))?; - self.send_frame(frame) - } - - pub(super) fn respond( - &self, - id: RequestId, - result: Result, - ) { - let message = HostToClient::Response { - id, - result: WireResult::from_result(result), - }; - if let Err(PeerSendError::Payload(err)) = self.send(message) { - let _ = self.send(HostToClient::Response { - id, - result: WireResult::Err { - message: format!("code-mode host response exceeds the IPC frame limit: {err}"), - }, - }); - } - } - - fn initial_response(&self, id: RequestId, result: Result) { - let message = HostToClient::InitialResponse { - id, - result: WireResult::from_result(result), - }; - if let Err(PeerSendError::Payload(err)) = self.send(message) { - let _ = self.send(HostToClient::InitialResponse { - id, - result: WireResult::Err { - message: format!( - "code-mode initial response exceeds the IPC frame limit: {err}" - ), - }, - }); - } - } - - pub(super) async fn call( - self: &Arc, - session_id: SessionId, - request: DelegateRequest, - cancellation_token: CancellationToken, - ) -> Result { - if self.disconnected.is_cancelled() { - return Err("code-mode client connection closed".to_string()); - } - let Ok(permit) = Arc::clone(&self.delegate_permits).try_acquire_owned() else { - return Err("code-mode host has too many pending delegate calls".to_string()); - }; - let id = DelegateRequestId::new(self.next_request_id.fetch_add(1, Ordering::Relaxed)); - let (response_tx, response_rx) = oneshot::channel(); - self.pending.lock().await.insert( - id, - PendingDelegate { - response_tx, - dispatched: false, - _permit: permit, - }, - ); - let mut pending = PendingDelegateRequest::new(Arc::clone(self), id); - let cell_id = match &request { - DelegateRequest::InvokeTool { invocation } => invocation.cell_id.clone().into(), - DelegateRequest::Notify { cell_id, .. } => cell_id.clone().into(), - }; - let (dispatched_tx, dispatched_rx) = oneshot::channel(); - if let Err(err) = self.route_cell_message( - (session_id, cell_id), - CellMessage::Delegate { - id, - request, - dispatched_tx, - }, - ) { - self.pending.lock().await.remove(&id); - pending.disarm(); - return Err(err); - } - - let dispatched = tokio::select! { - dispatched = dispatched_rx => dispatched.map_err(|_| { - "code-mode cell route closed before dispatching delegate request".to_string() - })?, - _ = self.disconnected.cancelled() => { - self.pending.lock().await.remove(&id); - pending.disarm(); - return Err("code-mode client connection closed".to_string()); - } - }; - if let Err(err) = dispatched { - self.pending.lock().await.remove(&id); - pending.disarm(); - return Err(err); - } - - tokio::select! { - response = response_rx => { - pending.disarm(); - response.map_err(|_| { - "code-mode client closed before returning delegate output".to_string() - })? - } - _ = cancellation_token.cancelled() => { - if self.remove_pending(id).await.is_some() { - let _ = self.send(HostToClient::CancelDelegateRequest { id }); - } - pending.disarm(); - Err("code mode delegate request cancelled".to_string()) - } - _ = self.disconnected.cancelled() => { - self.pending.lock().await.remove(&id); - pending.disarm(); - Err("code-mode client connection closed".to_string()) - } - } - } - - pub(super) async fn complete( - &self, - id: DelegateRequestId, - response: Result, - ) { - if let Some(pending) = self.remove_pending(id).await { - let _ = pending.response_tx.send(response); - } - } - - pub(super) fn start_cell( - self: &Arc, - session_id: SessionId, - request_id: RequestId, - started: StartedCell, - active_cell_permit: OwnedSemaphorePermit, - received_at: Instant, - ) -> oneshot::Receiver<()> { - let (initial_response_sent_tx, initial_response_sent_rx) = oneshot::channel(); - let key = (session_id, started.cell_id.clone()); - let (messages_tx, messages_rx) = mpsc::channel(CELL_MESSAGE_CAPACITY); - let previous = self - .cell_routes - .lock() - .unwrap_or_else(PoisonError::into_inner) - .insert(key.clone(), CellRoute::Active(messages_tx.clone())); - match previous { - Some(CellRoute::Pending(messages)) => { - for message in messages { - if messages_tx.try_send(message).is_err() { - self.disconnect(); - return initial_response_sent_rx; - } - } - } - Some(CellRoute::Active(_)) => { - self.disconnect(); - return initial_response_sent_rx; - } - None => {} - } - let peer = Arc::clone(self); - self.spawn_critical("cell forwarding", async move { - drive_cell( - peer, - key, - InitialRequest { - id: request_id, - received_at, - }, - started, - messages_rx, - initial_response_sent_tx, - active_cell_permit, - ) - .await; - }); - initial_response_sent_rx - } - - pub(super) fn close_cell(&self, session_id: SessionId, cell_id: CellId) { - let _ = self.route_cell_message((session_id, cell_id), CellMessage::Closed); - } - - pub(super) fn disconnect(&self) { - self.disconnected.cancel(); - } - - pub(super) fn fail(&self, reason: String) { - let mut failure = self.failure.lock().unwrap_or_else(PoisonError::into_inner); - if failure.is_none() { - *failure = Some(reason); - } - drop(failure); - self.disconnect(); - } - - pub(super) fn failure(&self) -> Option { - self.failure - .lock() - .unwrap_or_else(PoisonError::into_inner) - .clone() - } - - pub(super) fn is_disconnected(&self) -> bool { - self.disconnected.is_cancelled() - } - - pub(super) async fn disconnected(&self) { - self.disconnected.cancelled().await; - } - - pub(super) fn disconnection_token(&self) -> CancellationToken { - self.disconnected.clone() - } - - pub(super) async fn wait_for_session_cells(&self, session_id: &SessionId) { - loop { - let changed = self.cell_routes_changed.notified(); - if !self - .cell_routes - .lock() - .unwrap_or_else(PoisonError::into_inner) - .keys() - .any(|(route_session_id, _)| route_session_id == session_id) - { - return; - } - tokio::select! { - _ = changed => {} - _ = self.disconnected.cancelled() => return, - } - } - } - - async fn send_delegate_if_pending( - &self, - id: DelegateRequestId, - session_id: SessionId, - request: DelegateRequest, - dispatched_tx: oneshot::Sender>, - ) { - let result = { - let mut pending = self.pending.lock().await; - let Some(pending) = pending.get_mut(&id) else { - let _ = dispatched_tx.send(Err( - "code-mode delegate request was cancelled before dispatch".to_string(), - )); - return; - }; - match self.send(HostToClient::DelegateRequest { - id, - session_id, - request, - }) { - Ok(()) => { - pending.dispatched = true; - Ok(()) - } - Err(err) => Err(err.to_string()), - } - }; - let _ = dispatched_tx.send(result); - } - - fn route_cell_message( - &self, - key: (SessionId, CellId), - message: CellMessage, - ) -> Result<(), String> { - use std::collections::hash_map::Entry; - - let result = match self - .cell_routes - .lock() - .unwrap_or_else(PoisonError::into_inner) - .entry(key) - { - Entry::Occupied(mut entry) => match entry.get_mut() { - CellRoute::Pending(messages) if messages.len() < CELL_MESSAGE_CAPACITY => { - messages.push_back(message); - Ok(()) - } - CellRoute::Pending(_) => Err("code-mode cell message queue is full".to_string()), - CellRoute::Active(sender) => sender - .try_send(message) - .map_err(|_| "code-mode cell message queue is unavailable".to_string()), - }, - Entry::Vacant(entry) => { - entry.insert(CellRoute::Pending(VecDeque::from([message]))); - Ok(()) - } - }; - if result.is_err() { - self.disconnect(); - } - result - } - - async fn remove_pending(&self, id: DelegateRequestId) -> Option { - self.pending.lock().await.remove(&id) - } - - pub(super) fn spawn_critical(self: &Arc, task_name: &'static str, future: F) - where - F: std::future::Future + Send + 'static, - { - let task = tokio::spawn(future); - let peer = Arc::clone(self); - tokio::spawn(async move { - if let Err(err) = task.await { - peer.fail(format!("code-mode {task_name} task failed: {err}")); - } - }); - } - - fn send_frame(&self, frame: EncodedFrame) -> Result<(), PeerSendError> { - match self.outgoing_tx.try_send(frame) { - Ok(()) => Ok(()), - Err(mpsc::error::TrySendError::Full(_)) => { - self.disconnect(); - Err(PeerSendError::Unavailable( - "code-mode host outgoing queue is full".to_string(), - )) - } - Err(mpsc::error::TrySendError::Closed(_)) => { - self.disconnect(); - Err(PeerSendError::Unavailable( - "code-mode client connection closed".to_string(), - )) - } - } - } -} - -async fn drive_cell( - peer: Arc, - key: (SessionId, CellId), - request: InitialRequest, - started: StartedCell, - mut messages_rx: mpsc::Receiver, - initial_response_sent_tx: oneshot::Sender<()>, - _active_cell_permit: OwnedSemaphorePermit, -) { - let mut initial_response_sent_tx = Some(initial_response_sent_tx); - let initial_response = started.initial_response(); - tokio::pin!(initial_response); - let closed = loop { - tokio::select! { - biased; - result = &mut initial_response => { - // Freeze the request duration before response conversion or delivery. - let code_mode_host_duration = request.received_at.elapsed(); - peer.initial_response( - request.id, - result.and_then(|response| { - let response = response.with_code_mode_host_duration(code_mode_host_duration); - WireRuntimeResponse::try_from(response).map_err(|error| error.to_string()) - }), - ); - if let Some(initial_response_sent_tx) = initial_response_sent_tx.take() { - let _ = initial_response_sent_tx.send(()); - } - break false; - } - message = messages_rx.recv() => match message { - Some(CellMessage::Delegate { - id, - request, - dispatched_tx, - }) => { - peer.send_delegate_if_pending(id, key.0.clone(), request, dispatched_tx).await; - } - Some(CellMessage::Closed) | None => break true, - }, - _ = peer.disconnected.cancelled() => { - peer.remove_cell_route(&key); - return; - } - } - }; - - if closed { - let result = initial_response.await; - let code_mode_host_duration = request.received_at.elapsed(); - peer.initial_response( - request.id, - result.and_then(|response| { - let response = response.with_code_mode_host_duration(code_mode_host_duration); - WireRuntimeResponse::try_from(response).map_err(|error| error.to_string()) - }), - ); - if let Some(initial_response_sent_tx) = initial_response_sent_tx.take() { - let _ = initial_response_sent_tx.send(()); - } - } else { - loop { - tokio::select! { - message = messages_rx.recv() => match message { - Some(CellMessage::Delegate { - id, - request, - dispatched_tx, - }) => { - peer.send_delegate_if_pending(id, key.0.clone(), request, dispatched_tx).await; - } - Some(CellMessage::Closed) | None => break, - }, - _ = peer.disconnected.cancelled() => { - peer.remove_cell_route(&key); - return; - } - } - } - } - let _ = peer.send(HostToClient::CellClosed { - session_id: key.0.clone(), - cell_id: (&key.1).into(), - }); - peer.remove_cell_route(&key); -} - -impl HostPeer { - fn remove_cell_route(&self, key: &(SessionId, CellId)) { - let removed = self - .cell_routes - .lock() - .unwrap_or_else(PoisonError::into_inner) - .remove(key); - if removed.is_some() { - self.cell_routes_changed.notify_waiters(); - } - } -} - -pub(super) enum PeerSendError { - Payload(String), - Unavailable(String), -} - -impl std::fmt::Display for PeerSendError { - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Payload(message) | Self::Unavailable(message) => formatter.write_str(message), - } - } -} - -struct PendingDelegateRequest { - peer: Arc, - id: Option, -} - -impl PendingDelegateRequest { - fn new(peer: Arc, id: DelegateRequestId) -> Self { - Self { peer, id: Some(id) } - } - - fn disarm(&mut self) { - self.id = None; - } -} - -impl Drop for PendingDelegateRequest { - fn drop(&mut self) { - let Some(id) = self.id.take() else { - return; - }; - let peer = Arc::clone(&self.peer); - tokio::spawn(async move { - if let Some(pending) = peer.remove_pending(id).await - && pending.dispatched - { - let _ = peer.send(HostToClient::CancelDelegateRequest { id }); - } - }); - } -} - -#[cfg(test)] -#[path = "peer_tests.rs"] -mod tests; diff --git a/codex-rs/code-mode-host/src/peer_tests.rs b/codex-rs/code-mode-host/src/peer_tests.rs deleted file mode 100644 index a05d1df4d2..0000000000 --- a/codex-rs/code-mode-host/src/peer_tests.rs +++ /dev/null @@ -1,99 +0,0 @@ -use std::sync::Arc; -use std::time::Duration; -use std::time::Instant; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::RuntimeResponse; -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::host::DelegateRequest; -use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SessionId; -use pretty_assertions::assert_eq; -use tokio::sync::Semaphore; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio::sync::oneshot::error::TryRecvError; -use tokio_util::sync::CancellationToken; - -use super::HostPeer; - -fn session_id(value: &str) -> SessionId { - SessionId::new(value).expect("session ID") -} - -#[tokio::test] -async fn start_cell_reports_when_initial_response_is_enqueued() { - let (outgoing_tx, mut outgoing_rx) = mpsc::channel(/*max_capacity*/ 4); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - let cell_id = CellId::new("cell-1".to_string()); - let (response_tx, response_rx) = oneshot::channel(); - let started = StartedCell::new(cell_id.clone(), response_rx); - let active_cell_permits = Arc::new(Semaphore::new(/*permits*/ 1)); - let active_cell_permit = Arc::clone(&active_cell_permits) - .try_acquire_owned() - .expect("active cell permit"); - - let mut initial_response_sent = peer.start_cell( - session_id("session-1"), - RequestId::new(/*value*/ 1), - started, - active_cell_permit, - Instant::now(), - ); - assert_eq!(initial_response_sent.try_recv(), Err(TryRecvError::Empty)); - - response_tx - .send(RuntimeResponse::Result { - code_mode_host_duration: None, - cell_id: cell_id.clone(), - content_items: Vec::new(), - result_buffered_for: Duration::ZERO, - error_text: None, - }) - .expect("initial response receiver"); - initial_response_sent - .await - .expect("initial response completion"); - outgoing_rx.recv().await.expect("initial response frame"); - assert_eq!(active_cell_permits.available_permits(), 0); - - peer.close_cell(session_id("session-1"), cell_id); - let permit = tokio::time::timeout( - Duration::from_secs(1), - Arc::clone(&active_cell_permits).acquire_owned(), - ) - .await - .expect("cell permit should be released") - .expect("cell permit semaphore should remain open"); - drop(permit); -} - -#[tokio::test] -async fn pending_delegate_limit_rejects_call_without_disconnecting() { - let (outgoing_tx, _outgoing_rx) = mpsc::channel(/*max_capacity*/ 1); - let peer = Arc::new(HostPeer::new(outgoing_tx, MAX_PENDING_DELEGATE_CALLS)); - let permits = Arc::clone(&peer.delegate_permits) - .acquire_many_owned(MAX_PENDING_DELEGATE_CALLS as u32) - .await - .expect("delegate permits"); - - let result = peer - .call( - session_id("session-1"), - DelegateRequest::Notify { - call_id: "call-1".to_string(), - cell_id: CellId::new("cell-1".to_string()).into(), - text: "hello".to_string(), - }, - CancellationToken::new(), - ) - .await; - - assert_eq!( - result, - Err("code-mode host has too many pending delegate calls".to_string()) - ); - assert!(!peer.is_disconnected()); - drop(permits); -} diff --git a/codex-rs/code-mode-host/src/transport.rs b/codex-rs/code-mode-host/src/transport.rs index 93c3adc0bc..f91d7fb49d 100644 --- a/codex-rs/code-mode-host/src/transport.rs +++ b/codex-rs/code-mode-host/src/transport.rs @@ -5,19 +5,16 @@ use anyhow::Result; use crate::grpc_transport; -/// The default transport retains the standalone host's original stdio behavior. -pub const DEFAULT_LISTEN_URL: &str = "stdio"; +pub const DEFAULT_LISTEN_URL: &str = "grpc+stdio://"; #[derive(Debug, Clone, Eq, PartialEq)] enum ListenTransport { - Stdio, GrpcStdio, Grpc(SocketAddr), } pub(crate) async fn run_transport(listen_url: &str, host: crate::GrpcCodeModeHost) -> Result<()> { match parse_listen_url(listen_url)? { - ListenTransport::Stdio => crate::run_stdio_with_tool_preloads(host.tool_preloads).await, ListenTransport::GrpcStdio => crate::grpc_stdio::run(host).await, ListenTransport::Grpc(bind_address) => { grpc_transport::run_tcp_listener(bind_address, host).await @@ -26,12 +23,9 @@ pub(crate) async fn run_transport(listen_url: &str, host: crate::GrpcCodeModeHos } fn parse_listen_url(listen_url: &str) -> Result { - if listen_url == "grpc+stdio://" { + if matches!(listen_url, "grpc+stdio://" | "stdio" | "stdio://") { return Ok(ListenTransport::GrpcStdio); } - if matches!(listen_url, "stdio" | "stdio://") { - return Ok(ListenTransport::Stdio); - } if let Some(socket_addr) = listen_url.strip_prefix("grpc://") { return socket_addr diff --git a/codex-rs/code-mode-host/src/transport_tests.rs b/codex-rs/code-mode-host/src/transport_tests.rs index 3f9348e69e..92fb77dc3e 100644 --- a/codex-rs/code-mode-host/src/transport_tests.rs +++ b/codex-rs/code-mode-host/src/transport_tests.rs @@ -35,11 +35,11 @@ async fn grpc_listener_disables_nagle() { fn parse_listen_url_accepts_stdio_transports() { assert_eq!( parse_listen_url("stdio").expect("stdio listen URL should parse"), - ListenTransport::Stdio + ListenTransport::GrpcStdio ); assert_eq!( parse_listen_url("stdio://").expect("stdio URL should parse"), - ListenTransport::Stdio + ListenTransport::GrpcStdio ); assert_eq!( parse_listen_url("grpc+stdio://").expect("gRPC stdio URL should parse"), diff --git a/codex-rs/code-mode-host/tests/capacity.rs b/codex-rs/code-mode-host/tests/capacity.rs index 4b628fb410..e4dcc1fb4c 100644 --- a/codex-rs/code-mode-host/tests/capacity.rs +++ b/codex-rs/code-mode-host/tests/capacity.rs @@ -11,12 +11,12 @@ use codex_code_mode::ExecuteRequest; use codex_code_mode::FunctionCallOutputContentItem; use codex_code_mode::GrpcCodeModeSessionProvider; use codex_code_mode::NotificationFuture; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_code_mode::RuntimeResponse; use codex_code_mode::ToolDefinition; use codex_code_mode::ToolInvocationFuture; use codex_code_mode::WaitOutcome; use codex_code_mode::WaitRequest; +use codex_core::ProcessOwnedGrpcCodeModeSessionProvider; use codex_protocol::ToolName; use pretty_assertions::assert_eq; use serde_json::json; @@ -187,7 +187,7 @@ async fn assert_shared_capacity( #[tokio::test] async fn stdio_shared_sessions_complete_thousands_of_callbacks() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); assert_shared_capacity(&provider, /*cells*/ 256, /*calls_per_cell*/ 16).await; diff --git a/codex-rs/code-mode-host/tests/grpc_timing.rs b/codex-rs/code-mode-host/tests/grpc_timing.rs index f96a8c6cd5..92cf0546a4 100644 --- a/codex-rs/code-mode-host/tests/grpc_timing.rs +++ b/codex-rs/code-mode-host/tests/grpc_timing.rs @@ -10,12 +10,12 @@ use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::ExecuteRequest; use codex_code_mode::GrpcCodeModeSessionProvider; use codex_code_mode::NoopCodeModeSessionDelegate; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_code_mode::RuntimeResponse; use codex_code_mode::WaitOutcome; use codex_code_mode::WaitRequest; use codex_code_mode_protocol::grpc; use codex_code_mode_protocol::grpc::code_mode_host_client::CodeModeHostClient; +use codex_core::ProcessOwnedGrpcCodeModeSessionProvider; use pretty_assertions::assert_eq; use tokio::time::sleep; use tokio::time::timeout; @@ -118,7 +118,7 @@ async fn execution_timing_includes_javascript_but_excludes_delayed_reads() -> Re /// the client reads its initial response, including JavaScript computation and timers. #[tokio::test] async fn stdio_execution_timing_includes_javascript_but_excludes_delayed_reads() -> Result<()> { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?, ); let delegate = Arc::new(RecordingDelegate::default()); @@ -184,7 +184,7 @@ async fn stdio_execution_timing_includes_javascript_but_excludes_delayed_reads() async fn observation_timing_excludes_previous_requests_and_background_time() -> Result<()> { let host = HostHarness::start("grpc://127.0.0.1:0").await?; let providers: [Arc; 2] = [ - Arc::new(ProcessOwnedCodeModeSessionProvider::with_host_program( + Arc::new(ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?, )), Arc::new(GrpcCodeModeSessionProvider::new(host.endpoint)), diff --git a/codex-rs/code-mode-host/tests/settled.rs b/codex-rs/code-mode-host/tests/settled.rs index 4aef9b3957..d3d4bf78f5 100644 --- a/codex-rs/code-mode-host/tests/settled.rs +++ b/codex-rs/code-mode-host/tests/settled.rs @@ -12,12 +12,12 @@ use codex_code_mode::ExecuteRequest; use codex_code_mode::FunctionCallOutputContentItem; use codex_code_mode::GrpcCodeModeSessionProvider; use codex_code_mode::NotificationFuture; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_code_mode::RuntimeResponse; use codex_code_mode::ToolDefinition; use codex_code_mode::ToolInvocationFuture; use codex_code_mode::WaitOutcome; use codex_code_mode::WaitRequest; +use codex_core::ProcessOwnedGrpcCodeModeSessionProvider; use codex_protocol::ToolName; use pretty_assertions::assert_eq; use serde_json::json; @@ -160,7 +160,7 @@ async fn assert_partial_results(provider: &dyn CodeModeSessionProvider) { #[tokio::test] async fn settled_results_stream_over_stdio() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); tokio::time::timeout( diff --git a/codex-rs/code-mode-host/tests/stdio.rs b/codex-rs/code-mode-host/tests/stdio.rs index 41151ee68a..fc2789c168 100644 --- a/codex-rs/code-mode-host/tests/stdio.rs +++ b/codex-rs/code-mode-host/tests/stdio.rs @@ -1,8 +1,7 @@ #![allow(clippy::expect_used)] use std::sync::Arc; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; +use std::sync::OnceLock; use std::time::Duration; #[cfg(unix)] @@ -20,13 +19,13 @@ use codex_code_mode::CodeModeToolKind; use codex_code_mode::ExecuteRequest; use codex_code_mode::FunctionCallOutputContentItem; use codex_code_mode::NotificationFuture; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_code_mode::RuntimeResponse; use codex_code_mode::ToolDefinition; use codex_code_mode::ToolInvocationFuture; use codex_code_mode::WaitOutcome; use codex_code_mode::WaitRequest; use codex_code_mode::host::MAX_FRAME_BYTES; +use codex_core::ProcessOwnedGrpcCodeModeSessionProvider; use codex_protocol::ToolName; use pretty_assertions::assert_eq; use serde_json::json; @@ -43,7 +42,6 @@ use recording_delegate::cell_id; #[derive(Debug, Eq, PartialEq)] enum CallbackEvent { Started(String), - Cancelled(String), CellClosed(CellId), } @@ -51,8 +49,7 @@ struct CancellationDelegate { events_tx: mpsc::UnboundedSender, fast_tool_release: Semaphore, slow_tool_started: Semaphore, - hold_slow_cleanup: AtomicBool, - slow_cleanup_release: Semaphore, + slow_cancellation: OnceLock, } struct OversizedResultDelegate; @@ -87,21 +84,18 @@ impl CancellationDelegate { events_tx, fast_tool_release: Semaphore::new(/*permits*/ 0), slow_tool_started: Semaphore::new(/*permits*/ 0), - hold_slow_cleanup: AtomicBool::new(false), - slow_cleanup_release: Semaphore::new(/*permits*/ 0), + slow_cancellation: OnceLock::new(), }), events_rx, ) } - #[cfg(unix)] - fn hold_slow_cleanup(&self) { - self.hold_slow_cleanup.store(true, Ordering::Release); - } - - #[cfg(unix)] - fn release_slow_cleanup(&self) { - self.slow_cleanup_release.add_permits(1); + async fn slow_tool_cancelled(&self) { + self.slow_cancellation + .get() + .expect("slow tool has started") + .cancelled() + .await; } } @@ -122,23 +116,19 @@ impl CodeModeSessionDelegate for CancellationDelegate { permit.forget(); return Ok(json!({ "tool": tool_name })); } + if tool_name == "tool_call_slow" { + self.slow_cancellation + .set(cancellation_token) + .expect("slow tool runs once"); + let _ = self.events_tx.send(CallbackEvent::Started(tool_name)); + self.slow_tool_started.add_permits(1); + // The client can drop the callback as soon as it cancels the token. + // The test observes the token independently of this future. + return std::future::pending().await; + } let _ = self .events_tx .send(CallbackEvent::Started(tool_name.clone())); - if tool_name == "tool_call_slow" { - self.slow_tool_started.add_permits(1); - cancellation_token.cancelled().await; - let _ = self.events_tx.send(CallbackEvent::Cancelled(tool_name)); - if self.hold_slow_cleanup.load(Ordering::Acquire) { - let permit = self - .slow_cleanup_release - .acquire() - .await - .map_err(|_| "slow tool cleanup release closed".to_string())?; - permit.forget(); - } - return Err("slow tool cancelled".to_string()); - } let permit = self .fast_tool_release .acquire() @@ -236,7 +226,7 @@ async fn next_callback_event( #[tokio::test] async fn interrupt_yields_observations_without_stopping_the_cell() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let session = provider.create_session().await.expect("create session"); @@ -313,7 +303,7 @@ async fn interrupt_yields_observations_without_stopping_the_cell() { #[tokio::test] async fn session_execution_limits_are_isolated_on_a_shared_process_host() { let provider: Arc = - Arc::new(ProcessOwnedCodeModeSessionProvider::with_host_program( + Arc::new(ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), )); let limited = provider @@ -385,7 +375,7 @@ async fn session_execution_limits_are_isolated_on_a_shared_process_host() { #[tokio::test] async fn remote_session_persists_values_forwards_delegates_and_controls_cells() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let delegate = Arc::new(RecordingDelegate::default()); @@ -540,7 +530,7 @@ text(result.value); #[tokio::test] async fn dropping_long_wait_releases_observer_before_next_wait() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let session = provider @@ -617,7 +607,7 @@ async fn dropping_long_wait_releases_observer_before_next_wait() { #[tokio::test] async fn unawaited_slow_tool_is_cancelled_after_parallel_tools_complete() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let (delegate, mut events_rx) = CancellationDelegate::new(); @@ -709,17 +699,10 @@ return; next_callback_event(&mut events_rx).await, CallbackEvent::Started("tool_call_slow".to_string()) ); - let mut closure_events = vec![ - next_callback_event(&mut events_rx).await, - next_callback_event(&mut events_rx).await, - ]; - closure_events.sort_by(|left, right| format!("{left:?}").cmp(&format!("{right:?}"))); + delegate.slow_tool_cancelled().await; assert_eq!( - closure_events, - vec![ - CallbackEvent::Cancelled("tool_call_slow".to_string()), - CallbackEvent::CellClosed(running_cell_id.clone()), - ] + next_callback_event(&mut events_rx).await, + CallbackEvent::CellClosed(running_cell_id.clone()) ); let actual = wait_task .await @@ -742,7 +725,7 @@ return; #[tokio::test] async fn oversized_execute_request_does_not_close_the_shared_host() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let session = provider @@ -759,7 +742,7 @@ async fn oversized_execute_request_does_not_close_the_shared_host() { .err() .expect("oversized execute should fail"); assert!( - error.contains("IPC frame limit"), + error.contains("exceeds the gRPC message limit"), "unexpected error: {error}" ); @@ -786,7 +769,7 @@ async fn oversized_execute_request_does_not_close_the_shared_host() { #[tokio::test] async fn oversized_delegate_payloads_fail_only_the_tool_call() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let session = provider @@ -886,7 +869,7 @@ try { #[tokio::test] async fn oversized_initial_response_does_not_close_the_shared_host() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").expect("host binary"), ); let session = provider @@ -906,11 +889,11 @@ async fn oversized_initial_response_does_not_close_the_shared_host() { .await .expect_err("oversized initial response should fail"); assert!( - error.contains("IPC frame limit"), + error.contains("message length too large"), "unexpected error: {error}" ); - // The default session still returns raw output after the oversized frame failed. + // The default session still returns raw output after the oversized gRPC message failed. let actual = execute( &session, ExecuteRequest { @@ -935,7 +918,7 @@ async fn oversized_initial_response_does_not_close_the_shared_host() { ); session.shutdown().await.expect("shutdown remote session"); - // With Server truncation negotiated, a script error larger than the frame limit still returns. + // With Server truncation negotiated, a script error larger than the gRPC message limit still returns. let session = provider .create_session_with_options(CodeModeSessionOptions { output_truncation: CodeModeOutputTruncation::Server, @@ -953,7 +936,7 @@ async fn oversized_initial_response_does_not_close_the_shared_host() { .. } = response else { - panic!("expected completed error within the frame limit"); + panic!("expected completed error within the gRPC message limit"); }; assert!(error.starts_with("begin:") && error.ends_with(":end")); assert!(error.len() > 20 && error.len() <= 64 * 1024); @@ -986,9 +969,8 @@ async fn child_process_loss_cleans_up_and_rebuilds_the_shared_host() { permissions.set_mode(/*mode*/ 0o700); std::fs::set_permissions(&proxy_program, permissions).expect("make host proxy executable"); - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program(proxy_program); + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program(proxy_program); let (delegate_a, mut events_a) = CancellationDelegate::new(); - delegate_a.hold_slow_cleanup(); let delegate_b = Arc::new(RecordingDelegate::default()); let session_a = provider .create_session() @@ -1114,12 +1096,11 @@ async fn child_process_loss_cleans_up_and_rebuilds_the_shared_host() { .expect("second wait task") .is_err() ); - let closure_events = [ + delegate_a.slow_tool_cancelled().await; + assert_eq!( next_callback_event(&mut events_a).await, - next_callback_event(&mut events_a).await, - ]; - assert!(closure_events.contains(&CallbackEvent::Cancelled("tool_call_slow".to_string()))); - assert!(closure_events.contains(&CallbackEvent::CellClosed(cell_a.clone()))); + CallbackEvent::CellClosed(cell_a) + ); tokio::time::timeout(Duration::from_secs(5), async { loop { if delegate_b @@ -1136,6 +1117,22 @@ async fn child_process_loss_cleans_up_and_rebuilds_the_shared_host() { .await .expect("unrelated session cleanup timeout"); + // The first operation reports the lost generation before a caller can run + // new work against the replacement host. + let reset = session_b + .execute( + execute_request("text('must not replay');"), + delegate_b.clone(), + /*preempt*/ None, + ) + .await + .err() + .expect("report the lost session"); + assert!( + reset.contains("Stored values and running cells were lost"), + "{reset}" + ); + let actual = execute( &session_b, execute_request(r#"text("replacement");"#), @@ -1176,8 +1173,6 @@ async fn child_process_loss_cleans_up_and_rebuilds_the_shared_host() { .expect("unrelated session shutdown timeout") .expect("shutdown replacement session"); - delegate_a.release_slow_cleanup(); - tokio::task::yield_now().await; assert!(matches!( events_a.try_recv(), Err(mpsc::error::TryRecvError::Empty) diff --git a/codex-rs/code-mode-protocol/Cargo.toml b/codex-rs/code-mode-protocol/Cargo.toml index cad10b47eb..ada554bd51 100644 --- a/codex-rs/code-mode-protocol/Cargo.toml +++ b/codex-rs/code-mode-protocol/Cargo.toml @@ -17,7 +17,7 @@ codex-protocol = { workspace = true } prost = "0.14.3" serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } -tokio = { workspace = true, features = ["io-util", "sync"] } +tokio = { workspace = true, features = ["sync"] } tokio-util = { workspace = true, features = ["rt"] } tonic = { workspace = true } tonic-prost = { workspace = true } diff --git a/codex-rs/code-mode-protocol/src/host.rs b/codex-rs/code-mode-protocol/src/host.rs new file mode 100644 index 0000000000..2a2cd5f546 --- /dev/null +++ b/codex-rs/code-mode-protocol/src/host.rs @@ -0,0 +1,21 @@ +//! Resource limits shared by the code-mode host, client, and runtime. + +/// Maximum encoded gRPC message size. +pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024; + +/// Maximum number of unresolved delegate callbacks allowed per host connection. +pub const MAX_PENDING_DELEGATE_CALLS: usize = 4_096; + +/// Callback capacity of clients that do not advertise the larger budget. +pub const LEGACY_MAX_PENDING_DELEGATE_CALLS: usize = 1_024; + +/// Maximum aggregate text bytes retained per server-truncated observation. +pub const BOUNDED_OUTPUT_MAX_BYTES: usize = 1024 * 1024; + +/// Maximum aggregate inline image bytes retained per server-truncated observation. +pub const BOUNDED_OUTPUT_MAX_IMAGE_BYTES: usize = 8 * 1024 * 1024; + +/// Maximum aggregate encoded audio URL bytes retained per server-truncated observation. +/// Count the whole URL, including the data URL prefix and any base64 payload. +/// This leaves heuristic transport headroom but does not bound the complete response. +pub const BOUNDED_OUTPUT_MAX_AUDIO_BYTES: usize = 62 * 1024 * 1024; diff --git a/codex-rs/code-mode-protocol/src/host/codec.rs b/codex-rs/code-mode-protocol/src/host/codec.rs deleted file mode 100644 index 10d13debe8..0000000000 --- a/codex-rs/code-mode-protocol/src/host/codec.rs +++ /dev/null @@ -1,170 +0,0 @@ -use std::io; -use std::mem::size_of; - -use serde::Serialize; -use serde::de::DeserializeOwned; -use tokio::io::AsyncRead; -use tokio::io::AsyncReadExt; -use tokio::io::AsyncWrite; -use tokio::io::AsyncWriteExt; - -/// Maximum JSON payload size accepted for one code-mode host frame. -pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024; - -/// A serialized IPC frame that has already passed the payload size limit. -#[derive(Clone, Debug)] -pub struct EncodedFrame { - payload: Vec, -} - -impl EncodedFrame { - pub fn encode(message: &T) -> io::Result - where - T: Serialize, - { - let payload = serde_json::to_vec(message).map_err(|err| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("failed to encode code-mode IPC frame: {err}"), - ) - })?; - if payload.len() > MAX_FRAME_BYTES { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!( - "code-mode IPC frame length {} exceeds {MAX_FRAME_BYTES} bytes", - payload.len() - ), - )); - } - Ok(Self { payload }) - } - - /// Returns the complete length-prefixed representation of this frame. - pub fn into_framed_bytes(self) -> Vec { - let mut bytes = Vec::with_capacity(size_of::() + self.payload.len()); - bytes.extend_from_slice(&(self.payload.len() as u32).to_le_bytes()); - bytes.extend_from_slice(&self.payload); - bytes - } - - /// Decodes exactly one complete length-prefixed frame. - pub fn decode_framed(bytes: &[u8]) -> io::Result - where - T: DeserializeOwned, - { - let length_bytes: [u8; size_of::()] = bytes - .get(..size_of::()) - .and_then(|length_bytes| length_bytes.try_into().ok()) - .ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidData, - "code-mode IPC frame is missing its length prefix", - ) - })?; - let length = u32::from_le_bytes(length_bytes) as usize; - if length > MAX_FRAME_BYTES { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!("code-mode IPC frame length {length} exceeds {MAX_FRAME_BYTES} bytes"), - )); - } - - let payload = &bytes[size_of::()..]; - if payload.len() != length { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!( - "code-mode IPC frame declares {length} payload bytes but contains {}", - payload.len() - ), - )); - } - - serde_json::from_slice(payload).map_err(|err| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("failed to decode code-mode IPC frame: {err}"), - ) - }) - } -} - -/// Decodes JSON messages prefixed by a four-byte little-endian payload length. -pub struct FramedReader { - reader: R, -} - -impl FramedReader -where - R: AsyncRead + Unpin, -{ - pub fn new(reader: R) -> Self { - Self { reader } - } - - /// Reads the next frame, returning `None` only for EOF at a frame boundary. - pub async fn read(&mut self) -> io::Result> - where - T: DeserializeOwned, - { - let mut length_bytes = [0_u8; size_of::()]; - if self.reader.read(&mut length_bytes[..1]).await? == 0 { - return Ok(None); - } - self.reader.read_exact(&mut length_bytes[1..]).await?; - - let length = u32::from_le_bytes(length_bytes) as usize; - if length > MAX_FRAME_BYTES { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!("code-mode IPC frame length {length} exceeds {MAX_FRAME_BYTES} bytes"), - )); - } - - let mut payload = vec![0; length]; - self.reader.read_exact(&mut payload).await?; - serde_json::from_slice(&payload).map(Some).map_err(|err| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("failed to decode code-mode IPC frame: {err}"), - ) - }) - } -} - -/// Encodes JSON messages with a four-byte little-endian payload length. -pub struct FramedWriter { - writer: W, -} - -impl FramedWriter -where - W: AsyncWrite + Unpin, -{ - pub fn new(writer: W) -> Self { - Self { writer } - } - - /// Writes and flushes one complete frame. - pub async fn write(&mut self, message: &T) -> io::Result<()> - where - T: Serialize, - { - self.write_frame(&EncodedFrame::encode(message)?).await - } - - /// Writes and flushes a frame encoded before it entered an I/O queue. - pub async fn write_frame(&mut self, frame: &EncodedFrame) -> io::Result<()> { - let length = u32::try_from(frame.payload.len()).map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidData, - "code-mode IPC frame length exceeds u32", - ) - })?; - - self.writer.write_all(&length.to_le_bytes()).await?; - self.writer.write_all(&frame.payload).await?; - self.writer.flush().await - } -} diff --git a/codex-rs/code-mode-protocol/src/host/codec_tests.rs b/codex-rs/code-mode-protocol/src/host/codec_tests.rs deleted file mode 100644 index 332a93766e..0000000000 --- a/codex-rs/code-mode-protocol/src/host/codec_tests.rs +++ /dev/null @@ -1,137 +0,0 @@ -use pretty_assertions::assert_eq; -use serde_json::json; -use tokio::io::AsyncReadExt; -use tokio::io::AsyncWriteExt; - -use super::EncodedFrame; -use super::FramedReader; -use super::FramedWriter; -use super::MAX_FRAME_BYTES; - -#[test] -fn complete_frame_round_trips_without_a_byte_stream() { - let value = json!({"type": "session/open", "sessionId": "session-1"}); - let bytes = EncodedFrame::encode(&value) - .expect("encode frame") - .into_framed_bytes(); - - assert_eq!( - EncodedFrame::decode_framed::(&bytes).expect("decode frame"), - value - ); -} - -#[test] -fn complete_frame_rejects_truncated_and_trailing_payloads() { - let value = json!({"value": 1}); - let bytes = EncodedFrame::encode(&value) - .expect("encode frame") - .into_framed_bytes(); - - let truncated = &bytes[..bytes.len() - 1]; - let truncated_error = EncodedFrame::decode_framed::(truncated) - .expect_err("truncated frame should fail"); - assert_eq!(truncated_error.kind(), std::io::ErrorKind::InvalidData); - - let mut trailing = bytes; - trailing.push(0); - let trailing_error = EncodedFrame::decode_framed::(&trailing) - .expect_err("frame with trailing bytes should fail"); - assert_eq!(trailing_error.kind(), std::io::ErrorKind::InvalidData); -} - -#[tokio::test] -async fn frame_wire_format_is_little_endian_length_prefixed_json() { - let (writer, mut reader) = tokio::io::duplex(/*max_buf_size*/ 128); - let write = tokio::spawn(async move { - FramedWriter::new(writer) - .write(&json!({"value": 1})) - .await - .expect("write frame"); - }); - - let mut bytes = Vec::new(); - reader.read_to_end(&mut bytes).await.expect("read bytes"); - write.await.expect("writer task"); - - let payload = br#"{"value":1}"#; - let mut expected = (payload.len() as u32).to_le_bytes().to_vec(); - expected.extend_from_slice(payload); - assert_eq!(bytes, expected); -} - -#[tokio::test] -async fn fragmented_frame_round_trips() { - let value = json!({"type": "session/open", "sessionId": "session-1"}); - let payload = serde_json::to_vec(&value).expect("serialize"); - let mut bytes = (payload.len() as u32).to_le_bytes().to_vec(); - bytes.extend(payload); - - let (mut writer, reader) = tokio::io::duplex(/*max_buf_size*/ 128); - let write = tokio::spawn(async move { - for byte in bytes { - writer.write_all(&[byte]).await.expect("write byte"); - tokio::task::yield_now().await; - } - }); - - assert_eq!( - FramedReader::new(reader) - .read::() - .await - .expect("read frame"), - Some(value) - ); - write.await.expect("writer task"); -} - -#[tokio::test] -async fn eof_is_clean_only_at_a_frame_boundary() { - let (writer, reader) = tokio::io::duplex(/*max_buf_size*/ 16); - drop(writer); - assert_eq!( - FramedReader::new(reader) - .read::() - .await - .expect("clean eof"), - None - ); - - let (mut writer, reader) = tokio::io::duplex(/*max_buf_size*/ 16); - writer - .write_all(&[1, 0]) - .await - .expect("write partial header"); - drop(writer); - let err = FramedReader::new(reader) - .read::() - .await - .expect_err("truncated header"); - assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof); -} - -#[tokio::test] -async fn oversized_and_malformed_frames_are_rejected() { - let (mut writer, reader) = tokio::io::duplex(/*max_buf_size*/ 16); - writer - .write_all(&((MAX_FRAME_BYTES as u32) + 1).to_le_bytes()) - .await - .expect("write oversized header"); - let err = FramedReader::new(reader) - .read::() - .await - .expect_err("oversized frame"); - assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); - - let (mut writer, reader) = tokio::io::duplex(/*max_buf_size*/ 16); - writer - .write_all(&(1_u32).to_le_bytes()) - .await - .expect("write length"); - writer.write_all(b"{").await.expect("write malformed json"); - let err = FramedReader::new(reader) - .read::() - .await - .expect_err("malformed frame"); - assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); -} diff --git a/codex-rs/code-mode-protocol/src/host/error.rs b/codex-rs/code-mode-protocol/src/host/error.rs deleted file mode 100644 index 423202e44b..0000000000 --- a/codex-rs/code-mode-protocol/src/host/error.rs +++ /dev/null @@ -1,19 +0,0 @@ -use serde::Deserialize; -use serde::Serialize; - -use super::Capability; -use super::SupportedProtocolVersions; - -/// Explains why connection negotiation was rejected before any session opened. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum HandshakeRejectReason { - #[serde(rename = "noCompatibleVersion")] - NoCompatibleVersion { - supported_versions: SupportedProtocolVersions, - }, - #[serde(rename = "missingRequiredCapability")] - MissingRequiredCapability { capability: Capability }, - #[serde(rename = "invalidHello")] - InvalidHello { message: String }, -} diff --git a/codex-rs/code-mode-protocol/src/host/host_tests.rs b/codex-rs/code-mode-protocol/src/host/host_tests.rs deleted file mode 100644 index 4167970dbe..0000000000 --- a/codex-rs/code-mode-protocol/src/host/host_tests.rs +++ /dev/null @@ -1,868 +0,0 @@ -use std::fmt::Debug; - -use pretty_assertions::assert_eq; -use serde::Serialize; -use serde::de::DeserializeOwned; -use serde_json::Value; -use serde_json::json; - -use super::Capability; -use super::CapabilitySet; -use super::ClientHello; -use super::ClientToHost; -use super::DelegateRequest; -use super::DelegateRequestId; -use super::DelegateResponse; -use super::HandshakeRejectReason; -use super::HostHello; -use super::HostRequest; -use super::HostResponse; -use super::HostToClient; -use super::ProtocolVersion; -use super::RequestId; -use super::SessionId; -use super::SupportedProtocolVersions; -use super::WireCellId; -use super::WireContentItem; -use super::WireExecuteRequest; -use super::WireImageDetail; -use super::WireNestedToolCall; -use super::WireOutputTruncation; -use super::WireResult; -use super::WireRuntimeResponse; -use super::WireSessionCellExecutionLimits; -use super::WireToolDefinition; -use super::WireToolKind; -use super::WireToolName; -use super::WireWaitOutcome; -use super::WireWaitRequest; -use crate::CodeModeSessionCellExecutionLimits; -use crate::ExecuteRequest; - -fn session_id() -> SessionId { - SessionId::new("session-1").expect("valid session ID") -} - -fn cell_id(value: &str) -> WireCellId { - WireCellId::new(value) -} - -fn request_id(value: i64) -> RequestId { - RequestId::new(value) -} - -fn delegate_request_id(value: i64) -> DelegateRequestId { - DelegateRequestId::new(value) -} - -fn capability(value: &str) -> Capability { - Capability::new(value).expect("valid capability") -} - -fn supported_versions() -> SupportedProtocolVersions { - SupportedProtocolVersions::try_new([ProtocolVersion::V1]) - .expect("nonempty unique protocol versions") -} - -fn assert_wire_round_trip(message: T, encoded: Value) -where - T: Debug + DeserializeOwned + PartialEq + Serialize, -{ - assert_eq!(serde_json::to_value(&message).expect("serialize"), encoded); - assert_eq!( - serde_json::from_value::(encoded).expect("deserialize"), - message - ); -} - -fn execute_request() -> WireExecuteRequest { - WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: vec![ - WireToolDefinition { - name: "function_tool".to_string(), - tool_name: WireToolName { - name: "function_tool".to_string(), - namespace: None, - }, - description: "function tool".to_string(), - kind: WireToolKind::Function, - input_schema: Some(json!({ "type": "object" })), - output_schema: None, - }, - WireToolDefinition { - name: "freeform_tool".to_string(), - tool_name: WireToolName { - name: "freeform_tool".to_string(), - namespace: Some("mcp__sample__".to_string()), - }, - description: "freeform tool".to_string(), - kind: WireToolKind::Freeform, - input_schema: None, - output_schema: Some(json!({ "type": "string" })), - }, - ], - source: "text('hello');".to_string(), - yield_time_ms: Some(25), - max_output_tokens: Some(100), - } -} - -fn content_items() -> Vec { - vec![ - WireContentItem::InputText { - text: "hello".to_string(), - }, - WireContentItem::InputImage { - image_url: "data:image/png;base64,none".to_string(), - detail: None, - }, - WireContentItem::InputImage { - image_url: "data:image/png;base64,auto".to_string(), - detail: Some(WireImageDetail::Auto), - }, - WireContentItem::InputImage { - image_url: "data:image/png;base64,low".to_string(), - detail: Some(WireImageDetail::Low), - }, - WireContentItem::InputImage { - image_url: "data:image/png;base64,high".to_string(), - detail: Some(WireImageDetail::High), - }, - WireContentItem::InputImage { - image_url: "data:image/png;base64,original".to_string(), - detail: Some(WireImageDetail::Original), - }, - WireContentItem::InputAudio { - audio_url: "data:audio/wav;base64,YXVkaW8=".to_string(), - }, - ] -} - -fn content_items_json() -> Value { - json!([ - { "type": "input_text", "text": "hello" }, - { "type": "input_image", "image_url": "data:image/png;base64,none" }, - { - "type": "input_image", - "image_url": "data:image/png;base64,auto", - "detail": "auto", - }, - { - "type": "input_image", - "image_url": "data:image/png;base64,low", - "detail": "low", - }, - { - "type": "input_image", - "image_url": "data:image/png;base64,high", - "detail": "high", - }, - { - "type": "input_image", - "image_url": "data:image/png;base64,original", - "detail": "original", - }, - { - "type": "input_audio", - "audio_url": "data:audio/wav;base64,YXVkaW8=", - }, - ]) -} - -#[test] -fn handshake_v1_variants_are_pinned() { - assert_wire_round_trip( - ClientToHost::ClientHello( - ClientHello::new( - supported_versions(), - CapabilitySet::try_new([capability("required")]).expect("valid required set"), - CapabilitySet::try_new([capability("optional")]).expect("valid optional set"), - ) - .expect("disjoint capabilities"), - ), - json!({ - "type": "connection/hello", - "supportedVersions": [1], - "requiredCapabilities": ["required"], - "optionalCapabilities": ["optional"], - }), - ); - assert_wire_round_trip( - HostToClient::HostHello(HostHello::new( - ProtocolVersion::V1, - CapabilitySet::try_new([capability("required")]).expect("valid capabilities"), - )), - json!({ - "type": "connection/ready", - "selectedVersion": 1, - "capabilities": ["required"], - }), - ); - for (reason, encoded) in [ - ( - HandshakeRejectReason::NoCompatibleVersion { - supported_versions: supported_versions(), - }, - json!({ - "type": "connection/rejected", - "reason": { - "type": "noCompatibleVersion", - "supportedVersions": [1], - }, - }), - ), - ( - HandshakeRejectReason::MissingRequiredCapability { - capability: capability("required"), - }, - json!({ - "type": "connection/rejected", - "reason": { - "type": "missingRequiredCapability", - "capability": "required", - }, - }), - ), - ( - HandshakeRejectReason::InvalidHello { - message: "invalid hello".to_string(), - }, - json!({ - "type": "connection/rejected", - "reason": { - "type": "invalidHello", - "message": "invalid hello", - }, - }), - ), - ] { - assert_wire_round_trip(HostToClient::HandshakeRejected { reason }, encoded); - } -} - -#[test] -fn open_session_serializes_optional_cell_execution_limits() { - assert_wire_round_trip( - HostRequest::OpenSession { - session_id: session_id(), - cell_execution_limits: Some(WireSessionCellExecutionLimits { - max_yield_time_ms: Some(250), - max_heap_size_bytes: Some(16 * 1024 * 1024), - }), - output_truncation: None, - }, - json!({ - "method": "session/open", - "sessionId": "session-1", - "cellExecutionLimits": { - "maxYieldTimeMs": 250, - "maxHeapSizeBytes": 16 * 1024 * 1024, - }, - }), - ); -} - -#[test] -fn session_cell_execution_limits_convert_between_domain_and_wire() { - let domain_limits = CodeModeSessionCellExecutionLimits { - max_yield_time_ms: Some(250), - max_heap_size_bytes: Some(16_usize * 1024 * 1024), - }; - let wire_limits = WireSessionCellExecutionLimits { - max_yield_time_ms: Some(250), - max_heap_size_bytes: Some(16_u64 * 1024 * 1024), - }; - - assert_eq!( - WireSessionCellExecutionLimits::try_from(domain_limits.clone()) - .expect("domain limits convert to wire limits"), - wire_limits - ); - assert_eq!( - CodeModeSessionCellExecutionLimits::try_from(wire_limits) - .expect("wire limits convert to domain limits"), - domain_limits - ); -} - -#[cfg(target_pointer_width = "32")] -#[test] -fn session_cell_execution_limits_reject_heap_sizes_that_exceed_usize() { - let wire_limits = WireSessionCellExecutionLimits { - max_yield_time_ms: None, - max_heap_size_bytes: Some(u64::from(u32::MAX) + 1), - }; - - assert!(CodeModeSessionCellExecutionLimits::try_from(wire_limits).is_err()); -} - -#[test] -fn client_to_host_v1_variants_are_pinned() { - let execute_request = execute_request(); - for (id, request, encoded_request) in [ - ( - request_id(/*value*/ 1), - HostRequest::OpenSession { - session_id: session_id(), - cell_execution_limits: None, - output_truncation: None, - }, - json!({ "method": "session/open", "sessionId": "session-1" }), - ), - ( - request_id(/*value*/ 10), - HostRequest::OpenSession { - session_id: session_id(), - cell_execution_limits: None, - output_truncation: Some(WireOutputTruncation::Server), - }, - json!({ - "method": "session/open", - "sessionId": "session-1", - "outputTruncation": "server", - }), - ), - ( - request_id(/*value*/ 2), - HostRequest::Execute { - session_id: session_id(), - request: execute_request, - }, - json!({ - "method": "session/execute", - "sessionId": "session-1", - "request": { - "tool_call_id": "call-1", - "enabled_tools": [ - { - "name": "function_tool", - "tool_name": { "name": "function_tool", "namespace": null }, - "description": "function tool", - "kind": "function", - "input_schema": { "type": "object" }, - "output_schema": null, - }, - { - "name": "freeform_tool", - "tool_name": { - "name": "freeform_tool", - "namespace": "mcp__sample__", - }, - "description": "freeform tool", - "kind": "freeform", - "input_schema": null, - "output_schema": { "type": "string" }, - }, - ], - "source": "text('hello');", - "yield_time_ms": 25, - "max_output_tokens": 100, - }, - }), - ), - ( - request_id(/*value*/ 3), - HostRequest::Wait { - session_id: session_id(), - request: WireWaitRequest { - cell_id: cell_id("cell-1"), - yield_time_ms: 50, - }, - max_output_tokens: None, - }, - json!({ - "method": "session/wait", - "sessionId": "session-1", - "request": { "cell_id": "cell-1", "yield_time_ms": 50 }, - }), - ), - ( - request_id(/*value*/ 4), - HostRequest::Terminate { - session_id: session_id(), - cell_id: cell_id("cell-1"), - max_output_tokens: None, - }, - json!({ - "method": "session/terminate", - "sessionId": "session-1", - "cellId": "cell-1", - }), - ), - ( - request_id(/*value*/ 5), - HostRequest::ShutdownSession { - session_id: session_id(), - }, - json!({ "method": "session/shutdown", "sessionId": "session-1" }), - ), - ] { - assert_wire_round_trip( - ClientToHost::Request { id, request }, - json!({ - "type": "operation/request", - "id": id, - "request": encoded_request, - }), - ); - } - - for (id, result, encoded_result) in [ - ( - delegate_request_id(/*value*/ 6), - WireResult::Ok { - value: DelegateResponse::ToolResult { - result: json!({ "answer": 42 }), - }, - }, - json!({ - "status": "ok", - "value": { "type": "tool/result", "result": { "answer": 42 } }, - }), - ), - ( - delegate_request_id(/*value*/ 7), - WireResult::Ok { - value: DelegateResponse::NotificationDelivered, - }, - json!({ - "status": "ok", - "value": { "type": "notification/delivered" }, - }), - ), - ( - delegate_request_id(/*value*/ 8), - WireResult::Err { - message: "delegate failed".to_string(), - }, - json!({ "status": "error", "message": "delegate failed" }), - ), - ] { - assert_wire_round_trip( - ClientToHost::DelegateResponse { id, result }, - json!({ - "type": "delegate/response", - "id": id, - "result": encoded_result, - }), - ); - } - - assert_wire_round_trip( - ClientToHost::CancelRequest { - id: request_id(/*value*/ 9), - }, - json!({ - "type": "operation/cancel", - "id": 9, - }), - ); -} - -#[test] -fn host_to_client_v1_variants_are_pinned() { - for (id, response, encoded_response) in [ - ( - request_id(/*value*/ 1), - HostResponse::SessionReady { - session_id: session_id(), - }, - json!({ "type": "session/ready", "sessionId": "session-1" }), - ), - ( - request_id(/*value*/ 2), - HostResponse::ExecutionStarted { - cell_id: cell_id("cell-1"), - }, - json!({ "type": "execution/started", "cellId": "cell-1" }), - ), - ( - request_id(/*value*/ 3), - HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: cell_id("cell-1"), - content_items: content_items(), - }), - }, - json!({ - "type": "wait/completed", - "outcome": { - "LiveCell": { - "Yielded": { - "cell_id": "cell-1", - "content_items": content_items_json(), - "code_mode_host_duration_ns": 0, - }, - }, - }, - }), - ), - ( - request_id(/*value*/ 4), - HostResponse::WaitCompleted { - outcome: WireWaitOutcome::MissingCell(WireRuntimeResponse::Result { - code_mode_host_duration_ns: 0, - cell_id: cell_id("missing-cell"), - content_items: Vec::new(), - result_buffered_for_ns: 0, - error_text: Some("cell not found".to_string()), - }), - }, - json!({ - "type": "wait/completed", - "outcome": { - "MissingCell": { - "Result": { - "cell_id": "missing-cell", - "content_items": [], - "error_text": "cell not found", - "result_buffered_for_ns": 0, - "code_mode_host_duration_ns": 0, - }, - }, - }, - }), - ), - ( - request_id(/*value*/ 5), - HostResponse::SessionClosed { - session_id: session_id(), - }, - json!({ "type": "session/closed", "sessionId": "session-1" }), - ), - ] { - assert_wire_round_trip( - HostToClient::Response { - id, - result: WireResult::Ok { value: response }, - }, - json!({ - "type": "operation/response", - "id": id, - "result": { "status": "ok", "value": encoded_response }, - }), - ); - } - assert_wire_round_trip( - HostToClient::Response { - id: request_id(/*value*/ 6), - result: WireResult::Err { - message: "operation failed".to_string(), - }, - }, - json!({ - "type": "operation/response", - "id": 6, - "result": { "status": "error", "message": "operation failed" }, - }), - ); - - assert_wire_round_trip( - HostToClient::InitialResponse { - id: request_id(/*value*/ 7), - result: WireResult::Ok { - value: WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: cell_id("cell-1"), - content_items: Vec::new(), - }, - }, - }, - json!({ - "type": "execute/initialResponse", - "id": 7, - "result": { - "status": "ok", - "value": { - "Terminated": { - "cell_id": "cell-1", - "content_items": [], - "code_mode_host_duration_ns": 0, - }, - }, - }, - }), - ); - assert_wire_round_trip( - HostToClient::InitialResponse { - id: request_id(/*value*/ 8), - result: WireResult::Err { - message: "execution failed".to_string(), - }, - }, - json!({ - "type": "execute/initialResponse", - "id": 8, - "result": { "status": "error", "message": "execution failed" }, - }), - ); - - assert_wire_round_trip( - HostToClient::DelegateRequest { - id: delegate_request_id(/*value*/ 9), - session_id: session_id(), - request: DelegateRequest::InvokeTool { - invocation: WireNestedToolCall { - cell_id: cell_id("cell-1"), - runtime_tool_call_id: "runtime-call-1".to_string(), - tool_name: WireToolName { - name: "freeform_tool".to_string(), - namespace: Some("mcp__sample__".to_string()), - }, - tool_kind: WireToolKind::Freeform, - input: Some(json!({ "value": 1 })), - }, - }, - }, - json!({ - "type": "delegate/request", - "id": 9, - "sessionId": "session-1", - "request": { - "type": "tool/invoke", - "invocation": { - "cell_id": "cell-1", - "runtime_tool_call_id": "runtime-call-1", - "tool_name": { - "name": "freeform_tool", - "namespace": "mcp__sample__", - }, - "tool_kind": "freeform", - "input": { "value": 1 }, - }, - }, - }), - ); - assert_wire_round_trip( - HostToClient::DelegateRequest { - id: delegate_request_id(/*value*/ 10), - session_id: session_id(), - request: DelegateRequest::Notify { - call_id: "call-1".to_string(), - cell_id: cell_id("cell-1"), - text: "important".to_string(), - }, - }, - json!({ - "type": "delegate/request", - "id": 10, - "sessionId": "session-1", - "request": { - "type": "notification/send", - "callId": "call-1", - "cellId": "cell-1", - "text": "important", - }, - }), - ); - assert_wire_round_trip( - HostToClient::CancelDelegateRequest { - id: delegate_request_id(/*value*/ 11), - }, - json!({ "type": "delegate/cancel", "id": 11 }), - ); - assert_wire_round_trip( - HostToClient::CellClosed { - session_id: session_id(), - cell_id: cell_id("cell-1"), - }, - json!({ - "type": "cell/closed", - "sessionId": "session-1", - "cellId": "cell-1", - }), - ); -} - -#[test] -fn execute_request_integer_bounds_are_enforced() { - let wire_request = execute_request(); - let domain_request = ExecuteRequest::try_from(wire_request.clone()) - .expect("valid wire request converts to the domain"); - assert_eq!( - WireExecuteRequest::try_from(domain_request.clone()) - .expect("valid domain request converts to the wire"), - wire_request - ); - - let too_large = ExecuteRequest { - max_output_tokens: Some(usize::try_from(i32::MAX).expect("i32::MAX fits usize") + 1), - ..domain_request - }; - assert!(WireExecuteRequest::try_from(too_large).is_err()); - - let negative = WireExecuteRequest { - max_output_tokens: Some(-1), - ..wire_request - }; - assert!(ExecuteRequest::try_from(negative).is_err()); -} - -#[test] -fn invalid_protocol_states_cannot_be_constructed_or_decoded() { - assert!(SessionId::new("").is_err()); - assert!(Capability::new(" ").is_err()); - assert!(ProtocolVersion::new(/*value*/ 0).is_none()); - assert!(SupportedProtocolVersions::try_new([]).is_err()); - assert!( - SupportedProtocolVersions::try_new([ProtocolVersion::V1, ProtocolVersion::V1]).is_err() - ); - assert!(CapabilitySet::try_new([capability("same"), capability("same")]).is_err()); - - let version_two = ProtocolVersion::new(/*value*/ 2).expect("valid protocol version"); - let versions = SupportedProtocolVersions::try_new([ProtocolVersion::V1, version_two]) - .expect("valid versions"); - assert!(versions.contains(ProtocolVersion::V1)); - assert_eq!( - versions.iter().collect::>(), - vec![ProtocolVersion::V1, version_two] - ); - - let overlapping = capability("overlapping"); - assert!( - ClientHello::new( - supported_versions(), - CapabilitySet::try_new([overlapping.clone()]).expect("valid required set"), - CapabilitySet::try_new([overlapping]).expect("valid optional set"), - ) - .is_err() - ); - - for invalid in [ - json!({ - "type": "operation/request", - "id": 1, - "request": { "method": "session/open", "sessionId": "" }, - }), - json!({ - "type": "connection/hello", - "supportedVersions": [], - "requiredCapabilities": [], - "optionalCapabilities": [], - }), - json!({ - "type": "connection/hello", - "supportedVersions": [1], - "requiredCapabilities": ["overlapping"], - "optionalCapabilities": ["overlapping"], - }), - ] { - assert!(serde_json::from_value::(invalid).is_err()); - } -} - -#[test] -fn every_nested_v1_object_rejects_unknown_fields() { - assert!( - serde_json::from_value::(json!({ - "type": "operation/request", - "id": 1, - "request": { "method": "session/open", "sessionId": "session-1" }, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "method": "session/open", - "sessionId": "session-1", - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "method": "session/open", - "sessionId": "session-1", - "cellExecutionLimits": { - "maxYieldTimeMs": 250, - "unexpected": true, - }, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "tool_call_id": "call-1", - "enabled_tools": [], - "source": "text('hello');", - "yield_time_ms": null, - "max_output_tokens": null, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "name": "tool", - "tool_name": { "name": "tool", "namespace": null }, - "description": "tool", - "kind": "function", - "input_schema": null, - "output_schema": null, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "name": "tool", - "namespace": null, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "cell_id": "cell-1", - "yield_time_ms": 50, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "Yielded": { - "cell_id": "cell-1", - "content_items": [], - "code_mode_host_duration_ns": 0, - "unexpected": true, - }, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "type": "input_text", - "text": "hello", - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "cell_id": "cell-1", - "runtime_tool_call_id": "runtime-call-1", - "tool_name": { "name": "tool", "namespace": null }, - "tool_kind": "function", - "input": null, - "unexpected": true, - })) - .is_err() - ); - assert!( - serde_json::from_value::(json!({ - "type": "operation/response", - "id": 1, - "result": { - "status": "ok", - "value": { "type": "session/ready", "sessionId": "session-1" }, - }, - "unexpected": true, - })) - .is_err() - ); -} diff --git a/codex-rs/code-mode-protocol/src/host/message.rs b/codex-rs/code-mode-protocol/src/host/message.rs deleted file mode 100644 index e6a4d8d28e..0000000000 --- a/codex-rs/code-mode-protocol/src/host/message.rs +++ /dev/null @@ -1,276 +0,0 @@ -use std::fmt; - -use serde::Deserialize; -use serde::Serialize; -use serde_json::Value as JsonValue; - -use super::Capability; -use super::CapabilitySet; -use super::DelegateRequestId; -use super::HandshakeRejectReason; -use super::ProtocolVersion; -use super::RequestId; -use super::SessionId; -use super::SupportedProtocolVersions; -use super::WireCellId; -use super::WireExecuteRequest; -use super::WireNestedToolCall; -use super::WireOutputTruncation; -use super::WireRuntimeResponse; -use super::WireSessionCellExecutionLimits; -use super::WireWaitOutcome; -use super::WireWaitRequest; - -#[derive(Clone, Debug, PartialEq, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ClientHello { - supported_versions: SupportedProtocolVersions, - required_capabilities: CapabilitySet, - optional_capabilities: CapabilitySet, -} - -impl ClientHello { - pub fn new( - supported_versions: SupportedProtocolVersions, - required_capabilities: CapabilitySet, - optional_capabilities: CapabilitySet, - ) -> Result { - if let Some(capability) = required_capabilities - .iter() - .find(|capability| optional_capabilities.contains(capability)) - { - return Err(ClientHelloError::OverlappingCapability(capability.clone())); - } - Ok(Self { - supported_versions, - required_capabilities, - optional_capabilities, - }) - } - - pub fn supported_versions(&self) -> &SupportedProtocolVersions { - &self.supported_versions - } - - pub fn required_capabilities(&self) -> &CapabilitySet { - &self.required_capabilities - } - - pub fn optional_capabilities(&self) -> &CapabilitySet { - &self.optional_capabilities - } -} - -#[derive(Deserialize)] -#[serde(deny_unknown_fields, rename_all = "camelCase")] -struct ClientHelloWire { - supported_versions: SupportedProtocolVersions, - required_capabilities: CapabilitySet, - optional_capabilities: CapabilitySet, -} - -impl<'de> Deserialize<'de> for ClientHello { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let wire = ClientHelloWire::deserialize(deserializer)?; - Self::new( - wire.supported_versions, - wire.required_capabilities, - wire.optional_capabilities, - ) - .map_err(serde::de::Error::custom) - } -} - -#[derive(Clone, Debug, Eq, PartialEq)] -pub enum ClientHelloError { - OverlappingCapability(Capability), -} - -impl fmt::Display for ClientHelloError { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::OverlappingCapability(capability) => write!( - formatter, - "capability `{capability}` cannot be both required and optional" - ), - } - } -} - -impl std::error::Error for ClientHelloError {} - -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(deny_unknown_fields, rename_all = "camelCase")] -pub struct HostHello { - selected_version: ProtocolVersion, - capabilities: CapabilitySet, -} - -impl HostHello { - pub fn new(selected_version: ProtocolVersion, capabilities: CapabilitySet) -> Self { - Self { - selected_version, - capabilities, - } - } - - pub fn selected_version(&self) -> ProtocolVersion { - self.selected_version - } - - pub fn capabilities(&self) -> &CapabilitySet { - &self.capabilities - } -} - -/// Messages sent from a client to the code-mode host. -#[derive(Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum ClientToHost { - #[serde(rename = "connection/hello")] - ClientHello(ClientHello), - #[serde(rename = "operation/request")] - Request { id: RequestId, request: HostRequest }, - #[serde(rename = "operation/cancel")] - CancelRequest { id: RequestId }, - /// Yield the active observation without cancelling its cell. - #[serde(rename = "operation/yield")] - YieldRequest { id: RequestId }, - #[serde(rename = "delegate/response")] - DelegateResponse { - id: DelegateRequestId, - result: WireResult, - }, -} - -/// Messages sent from the code-mode host to a client. -#[derive(Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum HostToClient { - #[serde(rename = "connection/ready")] - HostHello(HostHello), - #[serde(rename = "connection/rejected")] - HandshakeRejected { reason: HandshakeRejectReason }, - #[serde(rename = "operation/response")] - Response { - id: RequestId, - result: WireResult, - }, - #[serde(rename = "execute/initialResponse")] - InitialResponse { - id: RequestId, - result: WireResult, - }, - #[serde(rename = "delegate/request")] - DelegateRequest { - id: DelegateRequestId, - session_id: SessionId, - request: DelegateRequest, - }, - #[serde(rename = "delegate/cancel")] - CancelDelegateRequest { id: DelegateRequestId }, - #[serde(rename = "cell/closed")] - CellClosed { - session_id: SessionId, - cell_id: WireCellId, - }, -} - -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "method", rename_all_fields = "camelCase")] -pub enum HostRequest { - #[serde(rename = "session/open")] - OpenSession { - session_id: SessionId, - #[serde(default, skip_serializing_if = "Option::is_none")] - cell_execution_limits: Option, - /// Sent only when output truncation support was agreed at connection hello. - /// Omission preserves the legacy session behavior and wire shape. - #[serde(default, skip_serializing_if = "Option::is_none")] - output_truncation: Option, - }, - #[serde(rename = "session/execute")] - Execute { - session_id: SessionId, - request: WireExecuteRequest, - }, - #[serde(rename = "session/wait")] - Wait { - session_id: SessionId, - request: WireWaitRequest, - #[serde(default, skip_serializing_if = "Option::is_none")] - max_output_tokens: Option, - }, - #[serde(rename = "session/terminate")] - Terminate { - session_id: SessionId, - cell_id: WireCellId, - #[serde(default, skip_serializing_if = "Option::is_none")] - max_output_tokens: Option, - }, - #[serde(rename = "session/shutdown")] - ShutdownSession { session_id: SessionId }, -} - -#[derive(Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum HostResponse { - #[serde(rename = "session/ready")] - SessionReady { session_id: SessionId }, - #[serde(rename = "execution/started")] - ExecutionStarted { cell_id: WireCellId }, - #[serde(rename = "wait/completed")] - WaitCompleted { outcome: WireWaitOutcome }, - #[serde(rename = "session/closed")] - SessionClosed { session_id: SessionId }, -} - -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum DelegateRequest { - #[serde(rename = "tool/invoke")] - InvokeTool { invocation: WireNestedToolCall }, - #[serde(rename = "notification/send")] - Notify { - call_id: String, - cell_id: WireCellId, - text: String, - }, -} - -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all_fields = "camelCase")] -pub enum DelegateResponse { - #[serde(rename = "tool/result")] - ToolResult { result: JsonValue }, - #[serde(rename = "notification/delivered")] - NotificationDelivered, -} - -#[derive(Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "status", rename_all_fields = "camelCase")] -pub enum WireResult { - #[serde(rename = "ok")] - Ok { value: T }, - #[serde(rename = "error")] - Err { message: String }, -} - -impl WireResult { - pub fn from_result(result: Result) -> Self { - match result { - Ok(value) => Self::Ok { value }, - Err(message) => Self::Err { message }, - } - } - - pub fn into_result(self) -> Result { - match self { - Self::Ok { value } => Ok(value), - Self::Err { message } => Err(message), - } - } -} diff --git a/codex-rs/code-mode-protocol/src/host/mod.rs b/codex-rs/code-mode-protocol/src/host/mod.rs deleted file mode 100644 index 98cd26f6e3..0000000000 --- a/codex-rs/code-mode-protocol/src/host/mod.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! Messages and framing for the code-mode host boundary. -//! -//! Protocol version 1 multiplexes session operations and delegate callbacks by -//! request ID over one ordered connection. - -mod codec; -mod error; -mod message; -mod payload; -mod types; - -/// Maximum number of unresolved delegate callbacks allowed per host connection. -pub const MAX_PENDING_DELEGATE_CALLS: usize = 4_096; - -/// Callback capacity of clients that do not advertise the larger budget. -pub const LEGACY_MAX_PENDING_DELEGATE_CALLS: usize = 1_024; - -/// Stdio clients that can accept up to 4,096 unresolved delegate callbacks. -pub const LARGE_DELEGATE_CAPACITY_CAPABILITY: &str = "delegate-capacity-4096"; - -/// Stdio connection support for the optional `session/open.outputTruncation` field. -/// Each session selects its own setting after the connection hello. -pub const BOUNDED_OUTPUT_CAPABILITY: &str = "bounded-output-v1"; -/// Session-local catalog revisions on execute requests. -pub const TOOL_CATALOG_CAPABILITY: &str = "tool-catalog-revision-v1"; -/// Maximum aggregate text bytes retained per server-truncated observation. -pub const BOUNDED_OUTPUT_MAX_BYTES: usize = 1024 * 1024; - -/// Maximum aggregate inline image bytes retained per server-truncated observation. -pub const BOUNDED_OUTPUT_MAX_IMAGE_BYTES: usize = 8 * 1024 * 1024; - -/// Maximum aggregate encoded audio URL bytes retained per server-truncated observation. -/// Count the whole URL, including the data URL prefix and any base64 payload. -/// This leaves heuristic transport headroom but does not bound the complete response. -pub const BOUNDED_OUTPUT_MAX_AUDIO_BYTES: usize = 62 * 1024 * 1024; - -/// Negotiated support for cell execution resource limits on `session/open`. -pub const SESSION_RESOURCE_LIMITS_CAPABILITY: &str = "session-cell-execution-resource-limits"; - -/// Support for yielding an in-flight execute or wait observation. -pub const YIELD_OBSERVATION_CAPABILITY: &str = "yield-observation"; - -pub use codec::EncodedFrame; -pub use codec::FramedReader; -pub use codec::FramedWriter; -pub use codec::MAX_FRAME_BYTES; -pub use error::HandshakeRejectReason; -pub use message::ClientHello; -pub use message::ClientHelloError; -pub use message::ClientToHost; -pub use message::DelegateRequest; -pub use message::DelegateResponse; -pub use message::HostHello; -pub use message::HostRequest; -pub use message::HostResponse; -pub use message::HostToClient; -pub use message::WireResult; -pub use payload::WireCellId; -pub use payload::WireContentItem; -pub use payload::WireExecuteRequest; -pub use payload::WireImageDetail; -pub use payload::WireNestedToolCall; -pub use payload::WireOutputTruncation; -pub use payload::WireRuntimeResponse; -pub use payload::WireSessionCellExecutionLimits; -pub use payload::WireToolDefinition; -pub use payload::WireToolKind; -pub use payload::WireToolName; -pub use payload::WireWaitOutcome; -pub use payload::WireWaitRequest; -pub use types::Capability; -pub use types::CapabilitySet; -pub use types::DelegateRequestId; -pub use types::DuplicateCapability; -pub use types::InvalidIdentifier; -pub use types::InvalidSupportedProtocolVersions; -pub use types::ProtocolVersion; -pub use types::RequestId; -pub use types::SessionId; -pub use types::SupportedProtocolVersions; - -#[cfg(test)] -#[path = "host_tests.rs"] -mod tests; - -#[cfg(test)] -#[path = "codec_tests.rs"] -mod codec_tests; diff --git a/codex-rs/code-mode-protocol/src/host/payload.rs b/codex-rs/code-mode-protocol/src/host/payload.rs deleted file mode 100644 index e134946dae..0000000000 --- a/codex-rs/code-mode-protocol/src/host/payload.rs +++ /dev/null @@ -1,533 +0,0 @@ -use std::num::TryFromIntError; -use std::time::Duration; - -use codex_protocol::ToolName; -use serde::Deserialize; -use serde::Serialize; -use serde_json::Value as JsonValue; - -use crate::CellId; -use crate::CodeModeNestedToolCall; -use crate::CodeModeOutputTruncation; -use crate::CodeModeSessionCellExecutionLimits; -use crate::CodeModeToolKind; -use crate::ExecuteRequest; -use crate::FunctionCallOutputContentItem; -use crate::ImageDetail; -use crate::MissingCodeModeHostDuration; -use crate::RuntimeResponse; -use crate::ToolDefinition; -use crate::WaitOutcome; -use crate::WaitRequest; - -/// The per-cell execution limits carried by a V1 session-open request. -#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] -#[serde(deny_unknown_fields, rename_all = "camelCase")] -pub struct WireSessionCellExecutionLimits { - #[serde(default, skip_serializing_if = "Option::is_none")] - pub max_yield_time_ms: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub max_heap_size_bytes: Option, -} - -impl TryFrom for WireSessionCellExecutionLimits { - type Error = TryFromIntError; - - fn try_from(value: CodeModeSessionCellExecutionLimits) -> Result { - Ok(Self { - max_yield_time_ms: value.max_yield_time_ms, - max_heap_size_bytes: value.max_heap_size_bytes.map(u64::try_from).transpose()?, - }) - } -} - -impl TryFrom for CodeModeSessionCellExecutionLimits { - type Error = TryFromIntError; - - fn try_from(value: WireSessionCellExecutionLimits) -> Result { - Ok(Self { - max_yield_time_ms: value.max_yield_time_ms, - max_heap_size_bytes: value.max_heap_size_bytes.map(usize::try_from).transpose()?, - }) - } -} - -/// The output truncation setting carried by a V1 session-open request. -#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "camelCase")] -pub enum WireOutputTruncation { - Client, - Server, -} - -impl From for WireOutputTruncation { - fn from(value: CodeModeOutputTruncation) -> Self { - match value { - CodeModeOutputTruncation::Client => Self::Client, - CodeModeOutputTruncation::Server => Self::Server, - } - } -} - -impl From for CodeModeOutputTruncation { - fn from(value: WireOutputTruncation) -> Self { - match value { - WireOutputTruncation::Client => Self::Client, - WireOutputTruncation::Server => Self::Server, - } - } -} - -/// A cell identifier with a wire representation owned by protocol V1. -#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] -#[serde(transparent)] -pub struct WireCellId(String); - -impl WireCellId { - pub fn new(value: impl Into) -> Self { - Self(value.into()) - } - - pub fn as_str(&self) -> &str { - &self.0 - } -} - -impl From for WireCellId { - fn from(value: CellId) -> Self { - Self(value.as_str().to_string()) - } -} - -impl From<&CellId> for WireCellId { - fn from(value: &CellId) -> Self { - Self(value.as_str().to_string()) - } -} - -impl From for CellId { - fn from(value: WireCellId) -> Self { - Self::new(value.0) - } -} - -/// The V1 wire representation of a tool's stable name. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct WireToolName { - pub name: String, - pub namespace: Option, -} - -impl From for WireToolName { - fn from(value: ToolName) -> Self { - Self { - name: value.name, - namespace: value.namespace, - } - } -} - -impl From for ToolName { - fn from(value: WireToolName) -> Self { - Self::new(value.namespace, value.name) - } -} - -/// The tool invocation shape supported by protocol V1. -#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum WireToolKind { - Function, - Freeform, -} - -impl From for WireToolKind { - fn from(value: CodeModeToolKind) -> Self { - match value { - CodeModeToolKind::Function => Self::Function, - CodeModeToolKind::Freeform => Self::Freeform, - } - } -} - -impl From for CodeModeToolKind { - fn from(value: WireToolKind) -> Self { - match value { - WireToolKind::Function => Self::Function, - WireToolKind::Freeform => Self::Freeform, - } - } -} - -/// A V1 tool definition embedded in an execute request. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct WireToolDefinition { - pub name: String, - pub tool_name: WireToolName, - pub description: String, - pub kind: WireToolKind, - pub input_schema: Option, - pub output_schema: Option, -} - -impl From for WireToolDefinition { - fn from(value: ToolDefinition) -> Self { - Self { - name: value.name, - tool_name: value.tool_name.into(), - description: value.description, - kind: value.kind.into(), - input_schema: value.input_schema, - output_schema: value.output_schema, - } - } -} - -impl From for ToolDefinition { - fn from(value: WireToolDefinition) -> Self { - Self { - name: value.name, - tool_name: value.tool_name.into(), - description: value.description, - kind: value.kind.into(), - input_schema: value.input_schema, - input_schema_max_bytes: None, - output_schema: value.output_schema, - } - } -} - -/// The complete execute request shape supported by protocol V1. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct WireExecuteRequest { - /// A nonempty catalog installs this revision. An empty list references it. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub catalog_revision: Option, - pub tool_call_id: String, - pub enabled_tools: Vec, - pub source: String, - pub yield_time_ms: Option, - pub max_output_tokens: Option, -} - -impl TryFrom for WireExecuteRequest { - type Error = TryFromIntError; - - fn try_from(value: ExecuteRequest) -> Result { - Ok(Self { - catalog_revision: None, - tool_call_id: value.tool_call_id, - enabled_tools: value.enabled_tools.into_iter().map(Into::into).collect(), - source: value.source, - yield_time_ms: value.yield_time_ms, - max_output_tokens: value.max_output_tokens.map(i32::try_from).transpose()?, - }) - } -} - -impl TryFrom for ExecuteRequest { - type Error = TryFromIntError; - - fn try_from(value: WireExecuteRequest) -> Result { - Ok(Self { - tool_call_id: value.tool_call_id, - enabled_tools: value.enabled_tools.into_iter().map(Into::into).collect(), - source: value.source, - yield_time_ms: value.yield_time_ms, - max_output_tokens: value.max_output_tokens.map(usize::try_from).transpose()?, - }) - } -} - -/// The nested wait payload for protocol V1. Its optional budget is carried by -/// the enclosing `HostRequest::Wait`. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct WireWaitRequest { - pub cell_id: WireCellId, - pub yield_time_ms: u64, -} - -impl From for WireWaitRequest { - fn from(value: WaitRequest) -> Self { - Self { - cell_id: value.cell_id.into(), - yield_time_ms: value.yield_time_ms, - } - } -} - -impl From for WaitRequest { - fn from(value: WireWaitRequest) -> Self { - Self { - cell_id: value.cell_id.into(), - yield_time_ms: value.yield_time_ms, - max_output_tokens: None, - } - } -} - -/// Image detail values accepted in a V1 runtime response. -#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "lowercase")] -pub enum WireImageDetail { - Auto, - Low, - High, - Original, -} - -impl From for WireImageDetail { - fn from(value: ImageDetail) -> Self { - match value { - ImageDetail::Auto => Self::Auto, - ImageDetail::Low => Self::Low, - ImageDetail::High => Self::High, - ImageDetail::Original => Self::Original, - } - } -} - -impl From for ImageDetail { - fn from(value: WireImageDetail) -> Self { - match value { - WireImageDetail::Auto => Self::Auto, - WireImageDetail::Low => Self::Low, - WireImageDetail::High => Self::High, - WireImageDetail::Original => Self::Original, - } - } -} - -/// One output item emitted by a V1 runtime response. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields, tag = "type", rename_all = "snake_case")] -pub enum WireContentItem { - InputText { - text: String, - }, - InputImage { - image_url: String, - #[serde(default, skip_serializing_if = "Option::is_none")] - detail: Option, - }, - InputAudio { - audio_url: String, - }, -} - -impl From for WireContentItem { - fn from(value: FunctionCallOutputContentItem) -> Self { - match value { - FunctionCallOutputContentItem::InputText { text } => Self::InputText { text }, - FunctionCallOutputContentItem::InputImage { image_url, detail } => Self::InputImage { - image_url, - detail: detail.map(Into::into), - }, - FunctionCallOutputContentItem::InputAudio { audio_url } => { - Self::InputAudio { audio_url } - } - } - } -} - -impl From for FunctionCallOutputContentItem { - fn from(value: WireContentItem) -> Self { - match value { - WireContentItem::InputText { text } => Self::InputText { text }, - WireContentItem::InputImage { image_url, detail } => Self::InputImage { - image_url, - detail: detail.map(Into::into), - }, - WireContentItem::InputAudio { audio_url } => Self::InputAudio { audio_url }, - } - } -} - -/// Runtime output returned over the V1 host connection. -/// -/// Host time is required and covers this request, not the cell lifetime. The -/// app-server and host run at the same version; no negotiation is needed. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub enum WireRuntimeResponse { - Yielded { - cell_id: WireCellId, - content_items: Vec, - code_mode_host_duration_ns: u64, - }, - Terminated { - cell_id: WireCellId, - content_items: Vec, - code_mode_host_duration_ns: u64, - }, - Result { - cell_id: WireCellId, - content_items: Vec, - error_text: Option, - #[serde(default)] - result_buffered_for_ns: u64, - code_mode_host_duration_ns: u64, - }, -} - -impl TryFrom for WireRuntimeResponse { - type Error = MissingCodeModeHostDuration; - - /// Preserves the response's timing; the host handler must record it first. - fn try_from(value: RuntimeResponse) -> Result { - Ok(match value { - RuntimeResponse::Yielded { - cell_id, - content_items, - code_mode_host_duration, - } => { - let code_mode_host_duration = - code_mode_host_duration.ok_or(MissingCodeModeHostDuration)?; - Self::Yielded { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - code_mode_host_duration_ns: u64::try_from(code_mode_host_duration.as_nanos()) - .unwrap_or(u64::MAX), - } - } - RuntimeResponse::Terminated { - cell_id, - content_items, - code_mode_host_duration, - } => { - let code_mode_host_duration = - code_mode_host_duration.ok_or(MissingCodeModeHostDuration)?; - Self::Terminated { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - code_mode_host_duration_ns: u64::try_from(code_mode_host_duration.as_nanos()) - .unwrap_or(u64::MAX), - } - } - RuntimeResponse::Result { - cell_id, - content_items, - error_text, - result_buffered_for, - code_mode_host_duration, - } => { - let code_mode_host_duration = - code_mode_host_duration.ok_or(MissingCodeModeHostDuration)?; - Self::Result { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - error_text, - result_buffered_for_ns: u64::try_from(result_buffered_for.as_nanos()) - .unwrap_or(u64::MAX), - code_mode_host_duration_ns: u64::try_from(code_mode_host_duration.as_nanos()) - .unwrap_or(u64::MAX), - } - } - }) - } -} - -impl From for RuntimeResponse { - fn from(value: WireRuntimeResponse) -> Self { - match value { - WireRuntimeResponse::Yielded { - cell_id, - content_items, - code_mode_host_duration_ns, - } => Self::Yielded { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - code_mode_host_duration: Some(Duration::from_nanos(code_mode_host_duration_ns)), - }, - WireRuntimeResponse::Terminated { - cell_id, - content_items, - code_mode_host_duration_ns, - } => Self::Terminated { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - code_mode_host_duration: Some(Duration::from_nanos(code_mode_host_duration_ns)), - }, - WireRuntimeResponse::Result { - cell_id, - content_items, - error_text, - result_buffered_for_ns, - code_mode_host_duration_ns, - } => Self::Result { - cell_id: cell_id.into(), - content_items: content_items.into_iter().map(Into::into).collect(), - error_text, - result_buffered_for: Duration::from_nanos(result_buffered_for_ns), - code_mode_host_duration: Some(Duration::from_nanos(code_mode_host_duration_ns)), - }, - } - } -} - -/// Whether a waited-for cell remained live in protocol V1. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub enum WireWaitOutcome { - LiveCell(WireRuntimeResponse), - MissingCell(WireRuntimeResponse), -} - -impl TryFrom for WireWaitOutcome { - type Error = MissingCodeModeHostDuration; - - fn try_from(value: WaitOutcome) -> Result { - Ok(match value { - WaitOutcome::LiveCell(response) => Self::LiveCell(response.try_into()?), - WaitOutcome::MissingCell(response) => Self::MissingCell(response.try_into()?), - }) - } -} - -impl From for WaitOutcome { - fn from(value: WireWaitOutcome) -> Self { - match value { - WireWaitOutcome::LiveCell(response) => Self::LiveCell(response.into()), - WireWaitOutcome::MissingCell(response) => Self::MissingCell(response.into()), - } - } -} - -/// A nested tool invocation sent over the V1 host connection. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(deny_unknown_fields)] -pub struct WireNestedToolCall { - pub cell_id: WireCellId, - pub runtime_tool_call_id: String, - pub tool_name: WireToolName, - pub tool_kind: WireToolKind, - pub input: Option, -} - -impl From for WireNestedToolCall { - fn from(value: CodeModeNestedToolCall) -> Self { - Self { - cell_id: value.cell_id.into(), - runtime_tool_call_id: value.runtime_tool_call_id, - tool_name: value.tool_name.into(), - tool_kind: value.tool_kind.into(), - input: value.input, - } - } -} - -impl From for CodeModeNestedToolCall { - fn from(value: WireNestedToolCall) -> Self { - Self { - cell_id: value.cell_id.into(), - runtime_tool_call_id: value.runtime_tool_call_id, - tool_name: value.tool_name.into(), - tool_kind: value.tool_kind.into(), - input: value.input, - } - } -} diff --git a/codex-rs/code-mode-protocol/src/host/types.rs b/codex-rs/code-mode-protocol/src/host/types.rs deleted file mode 100644 index 40c69df90b..0000000000 --- a/codex-rs/code-mode-protocol/src/host/types.rs +++ /dev/null @@ -1,248 +0,0 @@ -use std::collections::BTreeSet; -use std::fmt; -use std::num::NonZeroU32; - -use serde::Deserialize; -use serde::Deserializer; -use serde::Serialize; -use serde::Serializer; -use serde::de::Error as _; - -/// Correlates one client operation request with the host's response. -#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -#[serde(transparent)] -pub struct RequestId(i64); - -impl RequestId { - pub const fn new(value: i64) -> Self { - Self(value) - } -} - -/// Correlates one host delegate request with the client's response. -#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -#[serde(transparent)] -pub struct DelegateRequestId(i64); - -impl DelegateRequestId { - pub const fn new(value: i64) -> Self { - Self(value) - } -} - -#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -#[serde(transparent)] -pub struct ProtocolVersion(NonZeroU32); - -impl ProtocolVersion { - pub const V1: Self = Self(NonZeroU32::MIN); - - pub const fn new(value: u32) -> Option { - match NonZeroU32::new(value) { - Some(value) => Some(Self(value)), - None => None, - } - } - - pub const fn get(self) -> u32 { - self.0.get() - } -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct InvalidIdentifier; - -impl fmt::Display for InvalidIdentifier { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("identifier must not be empty") - } -} - -impl std::error::Error for InvalidIdentifier {} - -#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -struct NonEmptyString(String); - -impl NonEmptyString { - fn new(value: impl Into) -> Result { - let value = value.into(); - if value.trim().is_empty() { - Err(InvalidIdentifier) - } else { - Ok(Self(value)) - } - } - - fn as_str(&self) -> &str { - &self.0 - } -} - -impl Serialize for NonEmptyString { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - self.0.serialize(serializer) - } -} - -impl<'de> Deserialize<'de> for NonEmptyString { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - Self::new(String::deserialize(deserializer)?).map_err(D::Error::custom) - } -} - -/// A named protocol feature advertised during connection negotiation. -#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -#[serde(transparent)] -pub struct Capability(NonEmptyString); - -impl Capability { - pub fn new(value: impl Into) -> Result { - NonEmptyString::new(value).map(Self) - } - - pub fn as_str(&self) -> &str { - self.0.as_str() - } -} - -impl fmt::Display for Capability { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str(self.as_str()) - } -} - -/// Identifies one logical code-mode session on a connection. -#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -#[serde(transparent)] -pub struct SessionId(NonEmptyString); - -impl SessionId { - pub fn new(value: impl Into) -> Result { - NonEmptyString::new(value).map(Self) - } - - pub fn as_str(&self) -> &str { - self.0.as_str() - } -} - -impl fmt::Display for SessionId { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str(self.as_str()) - } -} - -#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)] -#[serde(transparent)] -pub struct CapabilitySet(BTreeSet); - -impl CapabilitySet { - pub fn empty() -> Self { - Self::default() - } - - pub fn try_new( - capabilities: impl IntoIterator, - ) -> Result { - let mut unique = BTreeSet::new(); - for capability in capabilities { - if !unique.insert(capability.clone()) { - return Err(DuplicateCapability { capability }); - } - } - Ok(Self(unique)) - } - - pub fn contains(&self, capability: &Capability) -> bool { - self.0.contains(capability) - } - - pub fn iter(&self) -> impl Iterator { - self.0.iter() - } -} - -impl<'de> Deserialize<'de> for CapabilitySet { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - Self::try_new(Vec::::deserialize(deserializer)?).map_err(D::Error::custom) - } -} - -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct DuplicateCapability { - capability: Capability, -} - -impl fmt::Display for DuplicateCapability { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(formatter, "duplicate capability `{}`", self.capability) - } -} - -impl std::error::Error for DuplicateCapability {} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] -#[serde(transparent)] -pub struct SupportedProtocolVersions(BTreeSet); - -impl SupportedProtocolVersions { - pub fn try_new( - versions: impl IntoIterator, - ) -> Result { - let mut unique = BTreeSet::new(); - for version in versions { - if !unique.insert(version) { - return Err(InvalidSupportedProtocolVersions::Duplicate(version)); - } - } - if unique.is_empty() { - return Err(InvalidSupportedProtocolVersions::Empty); - } - Ok(Self(unique)) - } - - pub fn contains(&self, version: ProtocolVersion) -> bool { - self.0.contains(&version) - } - - pub fn iter(&self) -> impl Iterator + '_ { - self.0.iter().copied() - } -} - -impl<'de> Deserialize<'de> for SupportedProtocolVersions { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - Self::try_new(Vec::::deserialize(deserializer)?).map_err(D::Error::custom) - } -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum InvalidSupportedProtocolVersions { - Empty, - Duplicate(ProtocolVersion), -} - -impl fmt::Display for InvalidSupportedProtocolVersions { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Empty => formatter.write_str("at least one protocol version is required"), - Self::Duplicate(version) => { - write!(formatter, "duplicate protocol version {}", version.get()) - } - } - } -} - -impl std::error::Error for InvalidSupportedProtocolVersions {} diff --git a/codex-rs/code-mode-protocol/src/runtime_tests.rs b/codex-rs/code-mode-protocol/src/runtime_tests.rs index 5e72577c3c..476787f61c 100644 --- a/codex-rs/code-mode-protocol/src/runtime_tests.rs +++ b/codex-rs/code-mode-protocol/src/runtime_tests.rs @@ -4,18 +4,12 @@ use std::time::Duration; use pretty_assertions::assert_eq; -use super::MissingCodeModeHostDuration; use super::RuntimeResponse; -use super::WaitOutcome; use crate::CellId; use crate::FunctionCallOutputContentItem; -use crate::host::WireRuntimeResponse; -use crate::host::WireWaitOutcome; - -/// Raw runtime serialization preserves absent timing; stdio always carries a -/// measured duration, including zero, without changing the response payload. +/// Runtime serialization distinguishes absent timing from a measured zero. #[test] -fn code_mode_host_duration_survives_runtime_and_stdio_serialization() { +fn code_mode_host_duration_survives_runtime_serialization() { let content_items = vec![FunctionCallOutputContentItem::InputText { text: "output".to_string(), }]; @@ -65,44 +59,6 @@ fn code_mode_host_duration_survives_runtime_and_stdio_serialization() { serde_json::from_value::(payload).expect("deserialize response"), expected ); - - if duration.is_some() { - let wire_payload = serde_json::to_value( - WireRuntimeResponse::try_from(expected.clone()).expect("timed response"), - ) - .expect("serialize response over stdio"); - assert_eq!( - RuntimeResponse::from( - serde_json::from_value::(wire_payload) - .expect("deserialize stdio response") - ), - expected - ); - } } } } - -/// Encoding must not turn a missing request measurement into measured zero, -/// including when no live cell remains to supply output. -#[test] -fn stdio_encoding_rejects_untimed_runtime_output() { - let response = RuntimeResponse::Terminated { - cell_id: CellId::new("cell".to_string()), - content_items: Vec::new(), - code_mode_host_duration: None, - }; - assert_eq!( - WireRuntimeResponse::try_from(response.clone()), - Err(MissingCodeModeHostDuration) - ); - for outcome in [ - WaitOutcome::LiveCell(response.clone()), - WaitOutcome::MissingCell(response), - ] { - assert_eq!( - WireWaitOutcome::try_from(outcome), - Err(MissingCodeModeHostDuration) - ); - } -} diff --git a/codex-rs/code-mode/Cargo.toml b/codex-rs/code-mode/Cargo.toml index 52cdae6d08..e519f0bde3 100644 --- a/codex-rs/code-mode/Cargo.toml +++ b/codex-rs/code-mode/Cargo.toml @@ -15,16 +15,16 @@ workspace = true [dependencies] codex-code-mode-protocol = { workspace = true } codex-http-client = { workspace = true } -codex-install-context = { workspace = true } codex-otel = { workspace = true } codex-protocol = { workspace = true } futures = { workspace = true } http-body-util = "0.1.3" +opentelemetry = { workspace = true } prost = "0.14.3" rand = { workspace = true } reqwest = { workspace = true, features = ["stream"] } serde_json = { workspace = true } -tokio = { workspace = true, features = ["io-util", "macros", "net", "process", "rt", "sync", "time"] } +tokio = { workspace = true, features = ["macros", "rt", "sync", "time"] } tokio-util = { workspace = true, features = ["rt"] } tonic = { workspace = true, features = ["zstd"] } tower = { version = "0.5.3", features = ["util"] } diff --git a/codex-rs/code-mode/src/disabled.rs b/codex-rs/code-mode/src/disabled.rs new file mode 100644 index 0000000000..ec88fbfeb6 --- /dev/null +++ b/codex-rs/code-mode/src/disabled.rs @@ -0,0 +1,26 @@ +//! Unavailable provider used when the code-mode host is disabled. + +use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits; +use codex_code_mode_protocol::CodeModeSessionProvider; +use codex_code_mode_protocol::CodeModeSessionProviderFuture; + +/// Rejects code-mode sessions when the standalone host is disabled. +#[derive(Default)] +pub struct DisabledCodeModeSessionProvider; + +impl CodeModeSessionProvider for DisabledCodeModeSessionProvider { + fn availability(&self) -> Result<(), String> { + Err("code-mode host is disabled".to_string()) + } + + fn create_session(&self) -> CodeModeSessionProviderFuture<'_> { + Box::pin(async { Err("code-mode host is disabled".to_string()) }) + } + + fn create_session_with_limits<'a>( + &'a self, + _limits: CodeModeSessionCellExecutionLimits, + ) -> CodeModeSessionProviderFuture<'a> { + self.create_session() + } +} diff --git a/codex-rs/code-mode/src/grpc_session/callbacks.rs b/codex-rs/code-mode/src/grpc_session/callbacks.rs index 010ec35d1d..872559a2ff 100644 --- a/codex-rs/code-mode/src/grpc_session/callbacks.rs +++ b/codex-rs/code-mode/src/grpc_session/callbacks.rs @@ -4,11 +4,12 @@ use std::sync::PoisonError; use std::time::Duration; use codex_code_mode_protocol::CellId; +use codex_code_mode_protocol::CodeModeSessionDelegate; use codex_code_mode_protocol::grpc; use codex_protocol::protocol::W3cTraceContext; use futures::FutureExt; +use opentelemetry::context::FutureExt as _; use tokio_util::sync::CancellationToken; -use tracing::Instrument; use tracing::warn; use super::SessionInner; @@ -118,34 +119,19 @@ impl SessionInner { .lock() .unwrap_or_else(PoisonError::into_inner) .admit_invocation(&call)?; - let callback_span = tracing::info_span!( - "code_mode.grpc.callback", - otel.name = "code_mode.grpc.callback", - session.id = %call.session_id, - execution.id = %call.execution_id, - cell.id = %call.cell_id, - invocation.id = %call.invocation_id, - ); - if let Some(traceparent) = call.traceparent.as_ref() { - codex_otel::set_parent_from_w3c_trace_context( - &callback_span, - &W3cTraceContext { - traceparent: Some(traceparent.clone()), - tracestate: None, - }, - ); - } - let invocation_id = call.invocation_id.clone(); let cancellation = match admission { - CallbackAdmission::Active(cancellation, delegate) => Ok((cancellation, delegate)), + CallbackAdmission::Active(cancellation, delegate, trace) => { + Ok((cancellation, delegate, trace)) + } CallbackAdmission::Cancelled => return Ok(()), CallbackAdmission::Closed => Err(format!("code-mode cell {} is closed", call.cell_id)), CallbackAdmission::Rejected(error) => Err(error), }; - let (cancellation, delegate) = match cancellation { + let (cancellation, delegate, trace) = match cancellation { Ok(cancellation) => cancellation, Err(error) => { let inner = Arc::clone(self); + let invocation_id = call.invocation_id; tokio::spawn(async move { inner .complete_tool_call(invocation_id, CancellationToken::new(), Err(error)) @@ -154,38 +140,68 @@ impl SessionInner { return Ok(()); } }; - let invocation = conversion::tool_call(call); - let inner = Arc::clone(self); + // A traced host supplies the per-tool parent. A local host without an + // exporter still shares the originating execution's context. + let trace = call + .traceparent + .take() + .map(|traceparent| W3cTraceContext { + traceparent: Some(traceparent), + tracestate: None, + }) + .or(trace); + let parent = trace + .as_ref() + .and_then(codex_otel::context_from_w3c_trace_context) + .unwrap_or_default(); tokio::spawn( - async move { - let result = match invocation { - Ok(invocation) => { - let callback = AssertUnwindSafe(async { - delegate - .invoke_tool(invocation, cancellation.child_token()) - .await - }) - .catch_unwind(); - tokio::select! { - biased; - _ = cancellation.cancelled() => return, - result = callback => match result { - Ok(result) => result, - Err(_) => Err("code-mode tool delegate panicked".to_string()), - }, - } - } - Err(error) => Err(error), - }; - inner - .complete_tool_call(invocation_id, cancellation, result) - .await; - } - .instrument(callback_span), + Arc::clone(self) + .invoke_tool(call, cancellation, delegate) + .with_context(parent), ); Ok(()) } + #[tracing::instrument( + name = "code_mode.grpc.callback", + skip_all, + fields( + session.id = %call.session_id, + execution.id = %call.execution_id, + cell.id = %call.cell_id, + invocation.id = %call.invocation_id, + ) + )] + async fn invoke_tool( + self: Arc, + mut call: grpc::ToolCall, + cancellation: CancellationToken, + delegate: Arc, + ) { + let invocation_id = std::mem::take(&mut call.invocation_id); + let result = match conversion::tool_call(call) { + Ok(invocation) => { + let callback = AssertUnwindSafe(async { + delegate + .invoke_tool(invocation, cancellation.child_token()) + .await + }) + .catch_unwind(); + tokio::select! { + biased; + _ = cancellation.cancelled() => return, + result = callback => match result { + Ok(result) => result, + Err(_) => Err("code-mode tool delegate panicked".to_string()), + }, + } + } + Err(error) => Err(error), + }; + self.complete_tool_call(invocation_id, cancellation, result) + .await; + } + async fn complete_tool_call( &self, invocation_id: String, @@ -232,49 +248,73 @@ impl SessionInner { .lock() .unwrap_or_else(PoisonError::into_inner) .admit_notification(¬ification)?; - let (cancellation, delegate) = match admission { - CallbackAdmission::Active(cancellation, delegate) => (cancellation, delegate), + let (cancellation, delegate, trace) = match admission { + CallbackAdmission::Active(cancellation, delegate, trace) => { + (cancellation, delegate, trace) + } CallbackAdmission::Cancelled | CallbackAdmission::Closed => return Ok(()), CallbackAdmission::Rejected(error) => { warn!("code-mode notification was dropped: {error}"); return Ok(()); } }; - let execution_id = notification.execution_id; - let inner = Arc::clone(self); + let parent = trace + .as_ref() + .and_then(codex_otel::context_from_w3c_trace_context) + .unwrap_or_default(); // Delegate callbacks stay outside the tracked session tasks so shutdown can cancel // them without waiting for arbitrary delegate work to complete. - tokio::spawn(async move { - let callback = AssertUnwindSafe(async { - delegate - .notify( - notification.call_id, - CellId::new(notification.cell_id), - notification.text, - cancellation, - ) - .await - }) - .catch_unwind(); - let result = tokio::select! { - biased; - _ = inner.stopped.cancelled() => return, - result = callback => result, - }; - match result { - Ok(Ok(())) => {} - Ok(Err(error)) => warn!("code-mode notification delegate failed: {error}"), - Err(_) => warn!("code-mode notification delegate panicked"), - } - let cell = inner - .state - .lock() - .unwrap_or_else(PoisonError::into_inner) - .finish_notification(&execution_id); - inner.report_closed_cell(cell); - }); + tokio::spawn( + Arc::clone(self) + .deliver_notification(notification, cancellation, delegate) + .with_context(parent), + ); Ok(()) } + + #[tracing::instrument( + name = "code_mode.grpc.notify", + skip_all, + fields( + session.id = %self.id, + execution.id = %notification.execution_id, + cell.id = %notification.cell_id, + ) + )] + async fn deliver_notification( + self: Arc, + notification: grpc::Notification, + cancellation: CancellationToken, + delegate: Arc, + ) { + let callback = AssertUnwindSafe(async { + delegate + .notify( + notification.call_id, + CellId::new(notification.cell_id), + notification.text, + cancellation, + ) + .await + }) + .catch_unwind(); + let result = tokio::select! { + biased; + _ = self.stopped.cancelled() => return, + result = callback => result, + }; + match result { + Ok(Ok(())) => {} + Ok(Err(error)) => warn!("code-mode notification delegate failed: {error}"), + Err(_) => warn!("code-mode notification delegate panicked"), + } + let cell = self + .state + .lock() + .unwrap_or_else(PoisonError::into_inner) + .finish_notification(¬ification.execution_id); + self.report_closed_cell(cell); + } } #[cfg(test)] diff --git a/codex-rs/code-mode/src/grpc_session/callbacks_tests.rs b/codex-rs/code-mode/src/grpc_session/callbacks_tests.rs index c88d150156..fcf27db171 100644 --- a/codex-rs/code-mode/src/grpc_session/callbacks_tests.rs +++ b/codex-rs/code-mode/src/grpc_session/callbacks_tests.rs @@ -24,8 +24,8 @@ use super::super::GrpcClient; use super::super::MAX_IN_FLIGHT_COMPLETIONS; use super::super::state::SessionState; use super::super::transport::SharedTransport; +use super::super::wait_for_watch; use super::SessionInner; -use crate::remote_session::wait_for_watch; #[tokio::test] async fn missing_invocation_completion_racing_cancellation_preserves_session() { diff --git a/codex-rs/code-mode/src/grpc_session/mod.rs b/codex-rs/code-mode/src/grpc_session/mod.rs index 07ddf4c8cf..670eb2db18 100644 --- a/codex-rs/code-mode/src/grpc_session/mod.rs +++ b/codex-rs/code-mode/src/grpc_session/mod.rs @@ -43,8 +43,6 @@ use self::state::ClosedCell; use self::state::SessionState; use self::transport::GrpcTransport; use self::transport::SharedTransport; -use crate::remote_session::ShutdownResultReceiver; -use crate::remote_session::wait_for_watch; mod callbacks; mod completion; @@ -58,6 +56,24 @@ mod state; mod transport; type GrpcClient = CodeModeHostClient; +type ShutdownResultReceiver = watch::Receiver>>; + +async fn wait_for_watch( + mut result_rx: watch::Receiver>>, +) -> Result +where + T: Clone, +{ + loop { + if let Some(result) = result_rx.borrow().clone() { + return result; + } + result_rx + .changed() + .await + .map_err(|_| "code-mode session transition stopped".to_string())?; + } +} #[derive(Clone)] struct SessionAdmission; diff --git a/codex-rs/code-mode/src/grpc_session/operations.rs b/codex-rs/code-mode/src/grpc_session/operations.rs index 5da2713574..d76f26318e 100644 --- a/codex-rs/code-mode/src/grpc_session/operations.rs +++ b/codex-rs/code-mode/src/grpc_session/operations.rs @@ -14,7 +14,9 @@ use codex_code_mode_protocol::TerminateRequest; use codex_code_mode_protocol::WaitOutcome; use codex_code_mode_protocol::WaitRequest; use codex_code_mode_protocol::grpc; +use codex_code_mode_protocol::host::MAX_FRAME_BYTES; use codex_protocol::protocol::W3cTraceContext; +use prost::Message; use tokio::sync::OwnedMutexGuard; use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; @@ -103,6 +105,14 @@ impl SessionInner { execution_id: execution_id.clone(), armed: true, }; + // Check the actual request after catalog reuse. An encoder error in an + // HTTP/2 request body can otherwise surface as a transport reset. + let encoded_bytes = request.encoded_len(); + if encoded_bytes > MAX_FRAME_BYTES { + return Err(format!( + "code-mode execute request of {encoded_bytes} encoded bytes exceeds the gRPC message limit of {MAX_FRAME_BYTES} bytes" + )); + } let (started_tx, started_rx) = oneshot::channel(); let inner = Arc::clone(self); self.stream_tasks.spawn(async move { diff --git a/codex-rs/code-mode/src/grpc_session/reconnect.rs b/codex-rs/code-mode/src/grpc_session/reconnect.rs index f00b1a5742..4e5fb7bc40 100644 --- a/codex-rs/code-mode/src/grpc_session/reconnect.rs +++ b/codex-rs/code-mode/src/grpc_session/reconnect.rs @@ -22,10 +22,10 @@ use super::GrpcCodeModeSession; use super::GrpcCodeModeSessionProvider; use super::GrpcSessionOpenError; use super::SHUTDOWN_ERROR; +use super::ShutdownResultReceiver; use super::generation; use super::generation::GenerationDelegate; -use crate::remote_session::ShutdownResultReceiver; -use crate::remote_session::wait_for_watch; +use super::wait_for_watch; pub(super) struct ReconnectableSession { inner: Arc, diff --git a/codex-rs/code-mode/src/grpc_session/state.rs b/codex-rs/code-mode/src/grpc_session/state.rs index 8e2d13efdc..ea7a88fe8c 100644 --- a/codex-rs/code-mode/src/grpc_session/state.rs +++ b/codex-rs/code-mode/src/grpc_session/state.rs @@ -9,6 +9,7 @@ use codex_code_mode_protocol::CellId; use codex_code_mode_protocol::grpc; use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; use codex_protocol::ToolName; +use codex_protocol::protocol::W3cTraceContext; use tokio_util::sync::CancellationToken; use uuid::Uuid; @@ -22,13 +23,19 @@ struct ActiveCallback { pub(super) type ClosedCell = (CellId, Arc); pub(super) enum CallbackAdmission { - Active(CancellationToken, Arc), + Active( + CancellationToken, + Arc, + Option, + ), Cancelled, Closed, Rejected(String), } struct ExecutionRecord { + // Keep the caller's context for callbacks without extending its span across a yield. + trace: Option, delegate: Arc, cell_id: Option, tool_call_id: String, @@ -153,6 +160,7 @@ impl SessionState { self.executions.insert( request.execution_id.clone(), ExecutionRecord { + trace: codex_otel::current_span_w3c_trace_context(), tool_call_id: request.tool_call_id.clone(), enabled_tools, delegate, @@ -261,6 +269,7 @@ impl SessionState { Ok(CallbackAdmission::Active( cancellation, Arc::clone(&execution.delegate), + execution.trace.clone(), )) } @@ -293,6 +302,7 @@ impl SessionState { Ok(CallbackAdmission::Active( execution.cancellation.child_token(), Arc::clone(&execution.delegate), + execution.trace.clone(), )) } diff --git a/codex-rs/code-mode/src/grpc_session/state_tests.rs b/codex-rs/code-mode/src/grpc_session/state_tests.rs index 3df693477e..9fe77a750b 100644 --- a/codex-rs/code-mode/src/grpc_session/state_tests.rs +++ b/codex-rs/code-mode/src/grpc_session/state_tests.rs @@ -77,13 +77,13 @@ fn callbacks_use_their_execution_delegate_before_cell_admission() { for (index, delegate) in delegates.iter().enumerate() { let mut call = tool_call(&index.to_string(), index as u128 + 1); call.cell_id = format!("cell-{index}"); - let CallbackAdmission::Active(_, actual) = state.admit_invocation(&call).unwrap() else { + let CallbackAdmission::Active(_, actual, _) = state.admit_invocation(&call).unwrap() else { panic!("callback should be admitted before the execute response"); }; assert!(Arc::ptr_eq(&actual, delegate)); let mut notice = notification(&index.to_string(), index as u128 + 1); notice.cell_id = call.cell_id; - let CallbackAdmission::Active(_, actual) = state.admit_notification(¬ice).unwrap() + let CallbackAdmission::Active(_, actual, _) = state.admit_notification(¬ice).unwrap() else { panic!("notification should be admitted before the execute response"); }; @@ -104,10 +104,10 @@ fn cell_closure_drains_notifications_and_cancels_tool_callbacks() { let first = state .admit_invocation(&tool_call("execution", /*invocation_id*/ 1)) .expect("accept early invocation"); - let CallbackAdmission::Active(first_cancellation, _) = first else { + let CallbackAdmission::Active(first_cancellation, ..) = first else { panic!("first callback was not admitted"); }; - let CallbackAdmission::Active(notification_cancellation, _) = state + let CallbackAdmission::Active(notification_cancellation, ..) = state .admit_notification(¬ification("execution", /*notification_id*/ 1)) .expect("admit notification") else { @@ -261,7 +261,7 @@ fn abandonment_revokes_callbacks_and_ignores_late_events() { state .begin_execution(&request("execution"), Arc::new(NoopCodeModeSessionDelegate)) .expect("register execution"); - let CallbackAdmission::Active(invocation, _) = state + let CallbackAdmission::Active(invocation, ..) = state .admit_invocation(&tool_call("execution", /*invocation_id*/ 1)) .expect("admit callback before execution starts") else { @@ -306,7 +306,7 @@ fn invocation_cancellation_revokes_delegate_and_late_completion() { .mark_execution_ready("execution") .expect("claim execution"); let invocation = tool_call("execution", /*invocation_id*/ 1); - let CallbackAdmission::Active(cancellation, _) = state + let CallbackAdmission::Active(cancellation, ..) = state .admit_invocation(&invocation) .expect("accept invocation") else { @@ -392,7 +392,7 @@ fn tool_callbacks_must_match_the_executions_enabled_tools() { }); assert!(matches!( state.admit_invocation(&explicit_default_namespace), - Ok(CallbackAdmission::Active(_, _)) + Ok(CallbackAdmission::Active(..)) )); assert_eq!(state.require_open(), Ok(())); } @@ -438,7 +438,7 @@ fn notification_call_ids_must_match_their_execution() { ); assert!(matches!( state.admit_notification(¬ification("execution", /*notification_id*/ 3)), - Ok(CallbackAdmission::Active(_, _)) + Ok(CallbackAdmission::Active(..)) )); } @@ -491,7 +491,7 @@ fn notifications_and_tools_share_the_pending_delegate_limit() { for index in 0..MAX_PENDING_DELEGATE_CALLS { assert!(matches!( state.admit_notification(¬ification("execution", index as u128 + 1)), - Ok(CallbackAdmission::Active(_, _)) + Ok(CallbackAdmission::Active(..)) )); } @@ -509,7 +509,7 @@ fn notifications_and_tools_share_the_pending_delegate_limit() { assert!(state.finish_notification("execution").is_none()); assert!(matches!( state.admit_invocation(&tool_call("execution", /*invocation_id*/ 2)), - Ok(CallbackAdmission::Active(_, _)) + Ok(CallbackAdmission::Active(..)) )); } @@ -519,7 +519,7 @@ fn terminated_cells_cancel_pending_notifications() { state .begin_execution(&request("execution"), Arc::new(NoopCodeModeSessionDelegate)) .expect("register execution"); - let CallbackAdmission::Active(cancellation, _) = state + let CallbackAdmission::Active(cancellation, ..) = state .admit_notification(¬ification("execution", /*notification_id*/ 1)) .expect("admit notification") else { @@ -541,13 +541,13 @@ fn disconnect_revokes_callbacks_and_returns_each_live_cell_once() { state .admit_execution("execution", "cell") .expect("admit execution"); - let CallbackAdmission::Active(cancellation, _) = state + let CallbackAdmission::Active(cancellation, ..) = state .admit_invocation(&tool_call("execution", /*invocation_id*/ 1)) .expect("accept invocation") else { panic!("invocation was not admitted"); }; - let CallbackAdmission::Active(notification_cancellation, _) = state + let CallbackAdmission::Active(notification_cancellation, ..) = state .admit_notification(¬ification("execution", /*notification_id*/ 1)) .expect("admit notification") else { diff --git a/codex-rs/code-mode/src/lib.rs b/codex-rs/code-mode/src/lib.rs index c3889cdd5a..a8540b854d 100644 --- a/codex-rs/code-mode/src/lib.rs +++ b/codex-rs/code-mode/src/lib.rs @@ -1,10 +1,8 @@ +mod disabled; mod grpc_session; -mod remote_session; mod tool_catalog; pub use codex_code_mode_protocol::*; +pub use disabled::DisabledCodeModeSessionProvider; pub use grpc_session::GrpcCodeModeSessionProvider; pub use grpc_session::GrpcSessionOpenError; -pub use remote_session::DisabledCodeModeSessionProvider; -pub use remote_session::ProcessOwnedCodeModeSession; -pub use remote_session::ProcessOwnedCodeModeSessionProvider; diff --git a/codex-rs/code-mode/src/remote_session.rs b/codex-rs/code-mode/src/remote_session.rs deleted file mode 100644 index c4366fc362..0000000000 --- a/codex-rs/code-mode/src/remote_session.rs +++ /dev/null @@ -1,624 +0,0 @@ -use std::io; -use std::path::PathBuf; -use std::sync::Arc; -use std::sync::Mutex as StdMutex; -use std::sync::OnceLock; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::AtomicU64; -use std::sync::atomic::Ordering; - -use codex_code_mode_protocol::CodeModeOutputTruncation; -use codex_code_mode_protocol::CodeModeSession; -use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::CodeModeSessionProvider; -use codex_code_mode_protocol::CodeModeSessionProviderFuture; -use codex_code_mode_protocol::CodeModeSessionResultFuture; -use codex_code_mode_protocol::ExecuteRequest; -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::WaitOutcome; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::TOOL_CATALOG_CAPABILITY; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_install_context::InstallContext; -use tokio::sync::Semaphore; -use tokio::sync::watch; -use tokio_util::sync::CancellationToken; - -use crate::tool_catalog::CatalogAdmission; - -use self::connection::Connection; -use self::connection::ConnectionError; -use self::connection::RemoteSession; -use self::connection::SessionCleanup; -use self::delegate::TracedCellDelegate; - -mod connection; -mod delegate; - -pub(crate) type ShutdownResultReceiver = watch::Receiver>>; - -/// Creates code-mode sessions backed by one lazily spawned process host. -pub struct ProcessOwnedCodeModeSessionProvider { - host: Arc, -} - -/// Rejects code-mode sessions when the standalone host is disabled. -#[derive(Default)] -pub struct DisabledCodeModeSessionProvider; - -impl ProcessOwnedCodeModeSessionProvider { - pub fn with_host_program(host_program: PathBuf) -> Self { - Self { - host: Arc::new(OwnedCodeModeHost::new(host_program)), - } - } - - fn process_host(&self) -> Arc { - Arc::clone(&self.host) - } -} - -impl Default for ProcessOwnedCodeModeSessionProvider { - fn default() -> Self { - Self::with_host_program(InstallContext::current().code_mode_host_program()) - } -} - -impl CodeModeSessionProvider for ProcessOwnedCodeModeSessionProvider { - fn availability(&self) -> Result<(), String> { - let host_program = &self.host.host_program; - if host_program.is_file() { - Ok(()) - } else { - Err(ConnectionError::Spawn { - host_program: host_program.clone(), - error: io::Error::new(io::ErrorKind::NotFound, "host executable was not found"), - } - .to_string()) - } - } - - fn create_session(&self) -> CodeModeSessionProviderFuture<'_> { - self.create_session_with_limits(CodeModeSessionCellExecutionLimits::default()) - } - - fn create_session_with_limits<'a>( - &'a self, - limits: CodeModeSessionCellExecutionLimits, - ) -> CodeModeSessionProviderFuture<'a> { - self.create_session_with_options(CodeModeSessionOptions { - cell_execution_limits: limits, - ..CodeModeSessionOptions::default() - }) - } - - fn create_session_with_options<'a>( - &'a self, - options: CodeModeSessionOptions, - ) -> CodeModeSessionProviderFuture<'a> { - Box::pin(create_host_session(self.process_host(), options)) - } -} - -impl CodeModeSessionProvider for DisabledCodeModeSessionProvider { - fn availability(&self) -> Result<(), String> { - Err("code-mode host is disabled".to_string()) - } - - fn create_session(&self) -> CodeModeSessionProviderFuture<'_> { - Box::pin(async { Err("code-mode host is disabled".to_string()) }) - } - - fn create_session_with_limits<'a>( - &'a self, - _limits: CodeModeSessionCellExecutionLimits, - ) -> CodeModeSessionProviderFuture<'a> { - self.create_session() - } -} - -async fn create_host_session( - host: Arc, - options: CodeModeSessionOptions, -) -> Result, String> { - let session = ProcessOwnedCodeModeSession::with_host(host, options); - session.connection().await?; - Ok(Arc::new(session)) -} - -struct OwnedCodeModeHost { - host_program: PathBuf, - connection: StdMutex>>, - connect_permit: Semaphore, - connection_generation: AtomicU64, - last_connection_error: StdMutex>, - next_session_id: AtomicU64, -} - -impl OwnedCodeModeHost { - fn new(host_program: PathBuf) -> Self { - Self { - host_program, - connection: StdMutex::new(None), - connect_permit: Semaphore::new(/*permits*/ 1), - connection_generation: AtomicU64::new(0), - last_connection_error: StdMutex::new(None), - next_session_id: AtomicU64::new(1), - } - } - - async fn connection(&self) -> Result, ConnectionError> { - if let Some(connection) = self.live_connection() { - return Ok(connection); - } - - let observed_generation = self.connection_generation.load(Ordering::Acquire); - let _connect_permit = self.connect_permit.acquire().await.map_err(|_| { - ConnectionError::Other("code-mode host connection coordinator closed".into()) - })?; - if let Some(connection) = self.live_connection() { - return Ok(connection); - } - let completed_generation = self.connection_generation.load(Ordering::Acquire); - if completed_generation != observed_generation - && let Some((generation, error)) = self - .last_connection_error - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .as_ref() - && *generation == completed_generation - { - return Err(ConnectionError::Other(error.clone())); - } - let connection = Connection::spawn(&self.host_program).await; - let new_connection = match connection { - Ok(connection) => connection, - Err(error) => { - let generation = self.connection_generation.fetch_add(1, Ordering::AcqRel) + 1; - *self - .last_connection_error - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) = - Some((generation, error.to_string())); - return Err(error); - } - }; - let new_connection = Arc::new(new_connection); - *self - .connection - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::clone(&new_connection)); - Ok(new_connection) - } - - fn live_connection(&self) -> Option> { - self.connection - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .as_ref() - .filter(|connection| connection.is_alive()) - .cloned() - } - - fn allocate_session(&self) -> RemoteSession { - // The shared ordinal also keeps cell IDs distinct after a thread reload. - let generation = self.next_session_id.fetch_add(1, Ordering::Relaxed); - let id = match SessionId::new(format!("session-{generation}")) { - Ok(session_id) => session_id, - Err(_) => unreachable!("a generated code-mode session ID is nonempty"), - }; - RemoteSession { id, generation } - } -} - -enum SessionState { - New, - Opening { - remote: RemoteSession, - result_rx: watch::Receiver>>, - }, - Open(SessionBinding), - Closing, - Closed, -} - -#[derive(Clone)] -struct SessionBinding { - catalog: Arc>, - connection: Arc, - remote: RemoteSession, - cleanup: SessionCleanup, -} - -struct SessionInner { - host: Arc, - options: CodeModeSessionOptions, - // The first successful open fixes the output policy for all generations of this session. - output_truncation: OnceLock, - state: StdMutex, - shutdown_requested: AtomicBool, - shutdown_result: StdMutex>, - retired_cleanups: StdMutex>, -} - -/// A logical code-mode session assigned to a process host. -pub struct ProcessOwnedCodeModeSession { - inner: Arc, -} - -impl ProcessOwnedCodeModeSession { - pub fn new() -> Self { - Self::with_host( - Arc::new(OwnedCodeModeHost::new( - InstallContext::current().code_mode_host_program(), - )), - CodeModeSessionOptions::default(), - ) - } - - fn with_host(host: Arc, options: CodeModeSessionOptions) -> Self { - Self { - inner: Arc::new(SessionInner { - host, - options, - output_truncation: OnceLock::new(), - state: StdMutex::new(SessionState::New), - shutdown_requested: AtomicBool::new(false), - shutdown_result: StdMutex::new(None), - retired_cleanups: StdMutex::new(Vec::new()), - }), - } - } - - async fn connection(&self) -> Result { - self.inner.connection().await - } - - fn server_output_budget(&self, budget: Option) -> Option { - budget.filter(|_| self.output_truncation() == CodeModeOutputTruncation::Server) - } - - pub async fn execute( - &self, - mut request: ExecuteRequest, - delegate: Arc, - preempt: Option, - ) -> Result { - let binding = self.connection().await?; - let delegate = Arc::new(TracedCellDelegate { - delegate, - trace: codex_otel::current_span_w3c_trace_context(), - }); - // V1 encodes the exec budget as i32. Its maximum already exceeds the host collector limit. - request.max_output_tokens = self - .server_output_budget(request.max_output_tokens) - .map(|tokens| tokens.min(i32::MAX as usize)); - let admission = binding - .connection - .supports(TOOL_CATALOG_CAPABILITY) - .then(|| CatalogAdmission::prepare(&binding.catalog, &request.enabled_tools)) - .flatten(); - if admission - .as_ref() - .is_some_and(CatalogAdmission::reuses_catalog) - { - request.enabled_tools.clear(); - } - let mut request = WireExecuteRequest::try_from(request) - .map_err(|error| format!("failed to encode code-mode execute request: {error}"))?; - request.catalog_revision = admission.as_ref().map(CatalogAdmission::revision); - let started = binding - .connection - .execute(binding.remote, request, delegate, preempt) - .await?; - if let Some(admission) = admission { - admission.accepted(); - } - Ok(started) - } - - pub async fn wait( - &self, - mut request: WaitRequest, - preempt: Option, - ) -> Result { - let binding = self.connection().await?; - request.max_output_tokens = self.server_output_budget(request.max_output_tokens); - binding - .connection - .wait(binding.remote, request, preempt) - .await - } - - pub async fn terminate(&self, mut request: TerminateRequest) -> Result { - let binding = self.connection().await?; - request.max_output_tokens = self.server_output_budget(request.max_output_tokens); - binding.connection.terminate(binding.remote, request).await - } - - pub async fn shutdown(&self) -> Result<(), String> { - wait_for_watch(self.inner.request_shutdown()).await - } -} - -impl SessionInner { - async fn connection(self: &Arc) -> Result { - loop { - if self.shutdown_requested.load(Ordering::Acquire) { - return Err("code mode session is shutting down".to_string()); - } - let (result_rx, start) = { - let mut state = self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - match &*state { - SessionState::New => { - let remote = self.host.allocate_session(); - let (result_tx, result_rx) = watch::channel(None); - *state = SessionState::Opening { - remote: remote.clone(), - result_rx: result_rx.clone(), - }; - (result_rx, Some((remote, result_tx))) - } - SessionState::Opening { result_rx, .. } => (result_rx.clone(), None), - SessionState::Open(binding) if binding.connection.is_alive() => { - return Ok(binding.clone()); - } - SessionState::Open(binding) => { - self.retain_cleanup(binding.cleanup.clone()); - *state = SessionState::New; - continue; - } - SessionState::Closing | SessionState::Closed => { - return Err("code mode session is shutting down".to_string()); - } - } - }; - if let Some((remote, result_tx)) = start { - let inner = Arc::clone(self); - tokio::spawn(async move { - inner.open(remote, result_tx).await; - }); - } - return wait_for_watch(result_rx).await; - } - } - - async fn open( - self: Arc, - remote: RemoteSession, - result_tx: watch::Sender>>, - ) { - let result = match self.host.connection().await { - Ok(connection) => { - let cleanup = connection - .open_session( - remote.clone(), - self.options.clone(), - &self.output_truncation, - ) - .await; - cleanup.map(|cleanup| SessionBinding { - catalog: Default::default(), - connection, - remote: remote.clone(), - cleanup, - }) - } - Err(err) => Err(err.to_string()), - }; - { - let mut state = self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if matches!( - &*state, - SessionState::Opening { - remote: opening_remote, - .. - } if opening_remote == &remote - ) { - *state = match &result { - Ok(binding) => SessionState::Open(binding.clone()), - Err(_) => SessionState::New, - }; - } - } - result_tx.send_replace(Some(result)); - } - - fn request_shutdown(self: &Arc) -> ShutdownResultReceiver { - self.shutdown_requested.store(true, Ordering::Release); - let mut shutdown_result = self - .shutdown_result - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if let Some(result_rx) = shutdown_result.as_ref() { - return result_rx.clone(); - } - let (result_tx, result_rx) = watch::channel(None); - *shutdown_result = Some(result_rx.clone()); - let inner = Arc::clone(self); - tokio::spawn(async move { - let result = inner.drive_shutdown().await; - result_tx.send_replace(Some(result)); - }); - result_rx - } - - async fn drive_shutdown(self: &Arc) -> Result<(), String> { - loop { - let action = { - let mut state = self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - match &*state { - SessionState::New => { - *state = SessionState::Closed; - ShutdownAction::Finish - } - SessionState::Opening { result_rx, .. } => { - ShutdownAction::WaitForOpen(result_rx.clone()) - } - SessionState::Open(binding) if !binding.connection.is_alive() => { - let cleanup = binding.cleanup.clone(); - *state = SessionState::Closing; - ShutdownAction::WaitForSessionCleanup(cleanup) - } - SessionState::Open(binding) => { - let binding = binding.clone(); - *state = SessionState::Closing; - ShutdownAction::Close(binding) - } - SessionState::Closing => { - return Err("code-mode session shutdown driver entered twice".to_string()); - } - SessionState::Closed => return Ok(()), - } - }; - match action { - ShutdownAction::WaitForOpen(result_rx) => { - let _ = wait_for_watch(result_rx).await; - } - ShutdownAction::Finish => { - self.wait_for_retired_cleanups().await; - return Ok(()); - } - ShutdownAction::WaitForSessionCleanup(cleanup) => { - cleanup.wait().await; - self.wait_for_retired_cleanups().await; - *self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) = SessionState::Closed; - return Ok(()); - } - ShutdownAction::Close(binding) => { - let result = binding.connection.shutdown_session(binding.remote).await; - if result.is_err() && !binding.connection.is_alive() { - binding.cleanup.wait().await; - } - self.wait_for_retired_cleanups().await; - *self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) = SessionState::Closed; - return result; - } - } - } - } - - fn retain_cleanup(&self, cleanup: SessionCleanup) { - let mut retired = self - .retired_cleanups - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - retired.retain(|cleanup| !cleanup.is_complete()); - if !cleanup.is_complete() { - retired.push(cleanup); - } - } - - async fn wait_for_retired_cleanups(&self) { - let retired = std::mem::take( - &mut *self - .retired_cleanups - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner), - ); - for cleanup in retired { - cleanup.wait().await; - } - } -} - -enum ShutdownAction { - WaitForOpen(watch::Receiver>>), - Finish, - WaitForSessionCleanup(SessionCleanup), - Close(SessionBinding), -} - -pub(crate) async fn wait_for_watch( - mut result_rx: watch::Receiver>>, -) -> Result -where - T: Clone, -{ - loop { - if let Some(result) = result_rx.borrow().clone() { - return result; - } - result_rx - .changed() - .await - .map_err(|_| "code-mode session transition stopped".to_string())?; - } -} - -impl Drop for ProcessOwnedCodeModeSession { - fn drop(&mut self) { - if tokio::runtime::Handle::try_current().is_ok() { - self.inner.request_shutdown(); - } - } -} - -impl Default for ProcessOwnedCodeModeSession { - fn default() -> Self { - Self::new() - } -} - -impl CodeModeSession for ProcessOwnedCodeModeSession { - fn output_truncation(&self) -> CodeModeOutputTruncation { - self.inner - .output_truncation - .get() - .copied() - .unwrap_or_default() - } - - fn execute<'a>( - &'a self, - request: ExecuteRequest, - delegate: Arc, - preempt: Option, - ) -> CodeModeSessionResultFuture<'a, StartedCell> { - Box::pin(ProcessOwnedCodeModeSession::execute( - self, request, delegate, preempt, - )) - } - - fn wait<'a>( - &'a self, - request: WaitRequest, - preempt: Option, - ) -> CodeModeSessionResultFuture<'a, WaitOutcome> { - Box::pin(ProcessOwnedCodeModeSession::wait(self, request, preempt)) - } - - fn terminate<'a>( - &'a self, - request: TerminateRequest, - ) -> CodeModeSessionResultFuture<'a, WaitOutcome> { - Box::pin(ProcessOwnedCodeModeSession::terminate(self, request)) - } - - fn shutdown<'a>(&'a self) -> CodeModeSessionResultFuture<'a, ()> { - Box::pin(ProcessOwnedCodeModeSession::shutdown(self)) - } -} - -#[cfg(test)] -#[path = "remote_session_tests.rs"] -mod tests; diff --git a/codex-rs/code-mode/src/remote_session/connection.rs b/codex-rs/code-mode/src/remote_session/connection.rs deleted file mode 100644 index ec728b7451..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection.rs +++ /dev/null @@ -1,642 +0,0 @@ -use std::fmt; -use std::future::Future; -use std::io; -use std::path::Path; -use std::path::PathBuf; -use std::process::Stdio; -use std::sync::Arc; -use std::sync::OnceLock; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; -use std::time::Duration; - -use codex_code_mode_protocol::CodeModeOutputTruncation; -use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::WaitOutcome; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY; -use codex_code_mode_protocol::host::Capability; -use codex_code_mode_protocol::host::CapabilitySet; -use codex_code_mode_protocol::host::ClientHello; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::FramedReader; -use codex_code_mode_protocol::host::FramedWriter; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::LARGE_DELEGATE_CAPACITY_CAPABILITY; -use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::ProtocolVersion; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SESSION_RESOURCE_LIMITS_CAPABILITY; -use codex_code_mode_protocol::host::SupportedProtocolVersions; -use codex_code_mode_protocol::host::TOOL_CATALOG_CAPABILITY; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_code_mode_protocol::host::YIELD_OBSERVATION_CAPABILITY; -use codex_protocol::shell_environment::scrub_non_inheritable_env_vars; -use tokio::io::AsyncBufReadExt; -use tokio::io::BufReader; -use tokio::process::Child; -use tokio::process::ChildStdin; -use tokio::process::ChildStdout; -use tokio::process::Command; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio::task::JoinHandle; -use tokio_util::sync::CancellationToken; -use tracing::debug; -use tracing::warn; - -use self::driver::ConnectionDriver; -use self::driver::DriverCommand; -use self::driver::DriverEvent; -use self::driver::DriverLifecycle; -pub(super) use self::driver::RemoteSession; -pub(super) use self::driver::SessionCleanup; -use self::reader::drive_reader; - -mod driver; -mod reader; - -// Accommodate a completion burst and concurrent control messages from a full host. -const IPC_CHANNEL_CAPACITY: usize = MAX_PENDING_DELEGATE_CALLS * 2; -const LOCAL_HOST_STARTUP_TIMEOUT: Duration = Duration::from_secs(30); -// TODO(anp) make this timeout configurable if 60 seconds is insufficient. -const DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT: Duration = Duration::from_secs(60); -// Host spawn errors become model-visible tool output. Bound configured paths -// while preserving the executable-bearing suffix needed to diagnose failures. -const MAX_DISPLAYED_HOST_PROGRAM_BYTES: usize = 512; -const TRUNCATED_HOST_PROGRAM_PREFIX: &str = "..."; - -pub(super) enum ConnectionError { - Spawn { - host_program: PathBuf, - error: io::Error, - }, - Other(String), -} - -impl fmt::Display for ConnectionError { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Spawn { - host_program, - error, - } => { - let host_program = host_program.to_string_lossy(); - if host_program.len() <= MAX_DISPLAYED_HOST_PROGRAM_BYTES { - return write!( - formatter, - "failed to spawn code-mode host {host_program}: {error}" - ); - } - - let mut suffix_start = host_program.len() - - (MAX_DISPLAYED_HOST_PROGRAM_BYTES - TRUNCATED_HOST_PROGRAM_PREFIX.len()); - while !host_program.is_char_boundary(suffix_start) { - suffix_start += 1; - } - - write!( - formatter, - "failed to spawn code-mode host {TRUNCATED_HOST_PROGRAM_PREFIX}{}: {error}", - &host_program[suffix_start..] - ) - } - Self::Other(message) => formatter.write_str(message), - } - } -} - -pub(super) struct Connection { - command_tx: mpsc::Sender, - execute_claim_tx: mpsc::UnboundedSender, - alive: Arc, - failure: Arc>>, - cancellation: CancellationToken, - capabilities: CapabilitySet, -} - -struct CallerCancellation { - token: CancellationToken, - armed: bool, -} - -struct ConnectionSupervisor { - child: Child, - event_tx: mpsc::Sender, - cancellation: CancellationToken, - alive: Arc, - failure: Arc>>, - driver_task: JoinHandle<()>, - reader_task: JoinHandle>, - writer_task: JoinHandle>, -} - -impl CallerCancellation { - fn new() -> Self { - Self { - token: CancellationToken::new(), - armed: true, - } - } - - fn token(&self) -> CancellationToken { - self.token.clone() - } - - fn disarm(mut self) { - self.armed = false; - } -} - -impl Drop for CallerCancellation { - fn drop(&mut self) { - if self.armed { - self.token.cancel(); - } - } -} - -impl Connection { - pub(super) async fn spawn(host_program: &Path) -> Result { - let mut command = Command::new(host_program); - #[cfg(unix)] - command.process_group(0); - #[cfg(windows)] - command.creation_flags(/*flags*/ 0x0800_0000); // CREATE_NO_WINDOW - command - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .kill_on_drop(true); - scrub_non_inheritable_env_vars(command.as_std_mut()); - let mut child = command.spawn().map_err(|error| ConnectionError::Spawn { - host_program: host_program.to_path_buf(), - error, - })?; - - if let Some(stderr) = child.stderr.take() { - tokio::spawn(async move { - let mut lines = BufReader::new(stderr).lines(); - loop { - match lines.next_line().await { - Ok(Some(line)) => debug!("code-mode host stderr: {line}"), - Ok(None) => break, - Err(err) => { - warn!("failed to read code-mode host stderr: {err}"); - break; - } - } - } - }); - } - - let stdin = child - .stdin - .take() - .ok_or_else(|| ConnectionError::Other("spawned code-mode host has no stdin".into()))?; - let stdout = child - .stdout - .take() - .ok_or_else(|| ConnectionError::Other("spawned code-mode host has no stdout".into()))?; - - Self::establish(FramedReader::new(stdout), FramedWriter::new(stdin), child).await - } - - async fn establish( - mut reader: FramedReader, - mut writer: FramedWriter, - mut child: Child, - ) -> Result { - let handshake = async { - let session_limits_capability = Capability::new(SESSION_RESOURCE_LIMITS_CAPABILITY) - .map_err(|error| error.to_string())?; - let yield_capability = - Capability::new(YIELD_OBSERVATION_CAPABILITY).map_err(|error| error.to_string())?; - let output_capability = - Capability::new(BOUNDED_OUTPUT_CAPABILITY).map_err(|error| error.to_string())?; - let delegate_capacity = Capability::new(LARGE_DELEGATE_CAPACITY_CAPABILITY) - .map_err(|error| error.to_string())?; - let catalog_capability = - Capability::new(TOOL_CATALOG_CAPABILITY).map_err(|error| error.to_string())?; - let optional_capabilities = CapabilitySet::try_new([ - session_limits_capability, - yield_capability, - output_capability, - delegate_capacity, - catalog_capability, - ]) - .map_err(|error| error.to_string())?; - let hello = ClientHello::new( - SupportedProtocolVersions::try_new([ProtocolVersion::V1]) - .map_err(|err| err.to_string())?, - CapabilitySet::empty(), - optional_capabilities, - ) - .map_err(|err| err.to_string())?; - writer - .write(&ClientToHost::ClientHello(hello)) - .await - .map_err(|err| format!("failed to write code-mode host hello: {err}"))?; - match reader - .read() - .await - .map_err(|err| format!("failed to read code-mode host hello: {err}"))? - { - Some(HostToClient::HostHello(hello)) - if hello.selected_version() == ProtocolVersion::V1 => - { - Ok(hello.capabilities().clone()) - } - Some(HostToClient::HandshakeRejected { reason }) => { - Err(format!("code-mode host rejected the handshake: {reason:?}")) - } - Some(message) => Err(format!( - "code-mode host returned an invalid handshake response: {message:?}" - )), - None => Err("code-mode host exited during handshake".to_string()), - } - }; - let handshake_result = - match tokio::time::timeout(LOCAL_HOST_STARTUP_TIMEOUT, handshake).await { - Ok(result) => result, - Err(_) => { - kill_and_reap(&mut child).await; - return Err(ConnectionError::Other( - "timed out negotiating with the code-mode host".into(), - )); - } - }; - let capabilities = match handshake_result { - Ok(negotiated) => negotiated, - Err(err) => { - kill_and_reap(&mut child).await; - return Err(ConnectionError::Other(err)); - } - }; - let (command_tx, command_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY); - let (event_tx, event_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY); - let (outgoing_tx, outgoing_rx) = mpsc::channel::(IPC_CHANNEL_CAPACITY); - let cancellation = CancellationToken::new(); - let alive = Arc::new(AtomicBool::new(true)); - let failure = Arc::new(std::sync::Mutex::new(None)); - - let writer_cancellation = cancellation.clone(); - let writer_task = - tokio::spawn( - async move { drive_writer(writer, outgoing_rx, writer_cancellation).await }, - ); - - let reader_events = event_tx.clone(); - let reader_cancellation = cancellation.clone(); - let reader_task = - tokio::spawn( - async move { drive_reader(reader, reader_events, reader_cancellation).await }, - ); - - let (driver, execute_claim_tx) = ConnectionDriver::new( - command_rx, - event_rx, - event_tx.clone(), - outgoing_tx, - DriverLifecycle { - alive: Arc::clone(&alive), - failure: Arc::clone(&failure), - cancellation: cancellation.clone(), - }, - ); - let driver_task = tokio::spawn(driver.run()); - tokio::spawn( - ConnectionSupervisor { - child, - event_tx, - cancellation: cancellation.clone(), - alive: Arc::clone(&alive), - failure: Arc::clone(&failure), - driver_task, - reader_task, - writer_task, - } - .run(), - ); - - Ok(Self { - command_tx, - execute_claim_tx, - alive, - failure, - cancellation, - capabilities, - }) - } - - pub(super) fn is_alive(&self) -> bool { - if self.command_tx.is_closed() { - mark_connection_dead( - &self.alive, - &self.failure, - "code-mode connection driver closed".to_string(), - ); - } - self.alive.load(Ordering::Acquire) - } - - pub(super) fn supports(&self, capability: &str) -> bool { - self.capabilities - .iter() - .any(|value| value.as_str() == capability) - } - - pub(super) async fn open_session( - &self, - session: RemoteSession, - mut options: CodeModeSessionOptions, - pinned: &OnceLock, - ) -> Result { - if options.cell_execution_limits != CodeModeSessionCellExecutionLimits::default() - && !self.supports(SESSION_RESOURCE_LIMITS_CAPABILITY) - { - return Err(format!( - "code-mode host does not support session resource limits: missing `{SESSION_RESOURCE_LIMITS_CAPABILITY}` capability" - )); - } - let pinned_mode = pinned.get().copied(); - let output_truncation = if self.supports(BOUNDED_OUTPUT_CAPABILITY) { - pinned_mode.unwrap_or(options.output_truncation) - } else if pinned_mode == Some(CodeModeOutputTruncation::Server) { - return Err( - "code-mode host cannot preserve server output truncation after reconnect".into(), - ); - } else { - CodeModeOutputTruncation::Client - }; - options.output_truncation = output_truncation; - let cleanup = SessionCleanup::new(); - let cancellation = CallerCancellation::new(); - let (response_tx, response_rx) = oneshot::channel(); - self.send(DriverCommand::OpenSession { - session, - options, - cleanup: cleanup.clone(), - caller_cancellation: cancellation.token(), - response_tx, - }) - .await?; - let result = self.receive(response_rx).await; - cancellation.disarm(); - result?; - pinned.get_or_init(|| output_truncation); - Ok(cleanup) - } - - pub(super) async fn execute( - &self, - session: RemoteSession, - request: WireExecuteRequest, - delegate: Arc, - yield_signal: Option, - ) -> Result { - let yield_signal = self.supported_yield_signal(yield_signal); - let cancellation = CallerCancellation::new(); - let (response_tx, response_rx) = oneshot::channel(); - self.send(DriverCommand::Execute { - session, - request, - delegate, - caller_cancellation: cancellation.token(), - yield_signal, - response_tx, - }) - .await?; - let delivered = match self.receive(response_rx).await { - Ok(delivered) => delivered, - Err(err) => { - cancellation.disarm(); - return Err(err); - } - }; - self.execute_claim_tx - .send(delivered.request_id) - .map_err(|_| self.failure_message())?; - cancellation.disarm(); - Ok(delivered.started) - } - - pub(super) async fn wait( - &self, - session: RemoteSession, - request: WaitRequest, - yield_signal: Option, - ) -> Result { - let yield_signal = self.supported_yield_signal(yield_signal); - // Account for the runtime's one-second yield grace separately from transport. - let runtime_timeout = - Duration::from_millis(request.yield_time_ms).saturating_add(Duration::from_secs(1)); - let cancellation = CallerCancellation::new(); - let (response_tx, response_rx) = oneshot::channel(); - let result = self - .with_transport_deadline(runtime_timeout, "wait", async { - self.send(DriverCommand::Wait { - session, - request, - caller_cancellation: cancellation.token(), - yield_signal, - response_tx, - }) - .await?; - self.receive(response_rx).await - }) - .await; - cancellation.disarm(); - result - } - - fn supported_yield_signal( - &self, - signal: Option, - ) -> Option { - self.supports(YIELD_OBSERVATION_CAPABILITY) - .then_some(signal) - .flatten() - } - - pub(super) async fn terminate( - &self, - session: RemoteSession, - request: TerminateRequest, - ) -> Result { - let (response_tx, response_rx) = oneshot::channel(); - self.with_transport_deadline(Duration::ZERO, "terminate", async { - self.send(DriverCommand::Terminate { - session, - request, - response_tx, - }) - .await?; - self.receive(response_rx).await - }) - .await - } - - pub(super) async fn shutdown_session(&self, session: RemoteSession) -> Result<(), String> { - let (response_tx, response_rx) = oneshot::channel(); - self.send(DriverCommand::ShutdownSession { - session, - response_tx, - }) - .await?; - self.receive(response_rx).await - } - - async fn with_transport_deadline( - &self, - runtime_timeout: Duration, - request_type: &str, - request: impl Future>, - ) -> Result { - let deadline = runtime_timeout.saturating_add(DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT); - match tokio::time::timeout(deadline, request).await { - Ok(result) => result, - Err(_) => { - warn!(request_type, "code-mode host request exceeded its deadline"); - let reason = - format!("code-mode host timed out waiting for {request_type} response"); - mark_connection_dead(&self.alive, &self.failure, reason.clone()); - self.cancellation.cancel(); - Err(reason) - } - } - } - - async fn send(&self, command: DriverCommand) -> Result<(), String> { - if !self.is_alive() { - return Err(self.failure_message()); - } - self.command_tx - .send(command) - .await - .map_err(|_| self.failure_message()) - } - - async fn receive( - &self, - response_rx: oneshot::Receiver>, - ) -> Result { - response_rx.await.map_err(|_| self.failure_message())? - } - - fn failure_message(&self) -> String { - self.failure - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .clone() - .unwrap_or_else(|| "code-mode host connection closed".to_string()) - } -} - -async fn drive_writer( - mut writer: FramedWriter, - mut outgoing: mpsc::Receiver, - cancellation: CancellationToken, -) -> Result<(), String> { - loop { - tokio::select! { - _ = cancellation.cancelled() => return Ok(()), - frame = outgoing.recv() => { - let Some(frame) = frame else { - return Err("code-mode host outgoing stream closed".to_string()); - }; - tokio::select! { - _ = cancellation.cancelled() => return Ok(()), - result = writer.write_frame(&frame) => { - result.map_err(|error| { - format!("failed to write code-mode host message: {error}") - })?; - } - } - } - } - } -} - -impl Drop for Connection { - fn drop(&mut self) { - mark_connection_dead( - &self.alive, - &self.failure, - "code-mode host connection closed".to_string(), - ); - self.cancellation.cancel(); - } -} - -impl ConnectionSupervisor { - async fn run(mut self) { - let mut child_exited = false; - let reason = tokio::select! { - biased; - _ = self.cancellation.cancelled() => failure_message(&self.failure), - result = &mut self.driver_task => match result { - Ok(()) => "code-mode connection driver exited unexpectedly".to_string(), - Err(err) => format!("code-mode connection driver task failed: {err}"), - }, - result = &mut self.reader_task => task_failure("reader", result), - result = &mut self.writer_task => task_failure("writer", result), - result = self.child.wait() => { - child_exited = true; - match result { - Ok(status) => format!("code-mode host exited with status {status}"), - Err(error) => format!("failed waiting for code-mode host: {error}"), - } - } - }; - mark_connection_dead(&self.alive, &self.failure, reason.clone()); - let _ = self.event_tx.try_send(DriverEvent::Failed(reason)); - self.cancellation.cancel(); - if !child_exited { - kill_and_reap(&mut self.child).await; - } - } -} - -fn task_failure( - task_name: &str, - result: Result, tokio::task::JoinError>, -) -> String { - match result { - Ok(Ok(())) => format!("code-mode connection {task_name} exited unexpectedly"), - Ok(Err(err)) => err, - Err(err) => format!("code-mode connection {task_name} task failed: {err}"), - } -} - -fn mark_connection_dead( - alive: &AtomicBool, - failure: &std::sync::Mutex>, - reason: String, -) { - alive.store(false, Ordering::Release); - let mut failure = failure - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if failure.is_none() { - *failure = Some(reason); - } -} - -fn failure_message(failure: &std::sync::Mutex>) -> String { - failure - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .clone() - .unwrap_or_else(|| "code-mode host connection closed".to_string()) -} - -async fn kill_and_reap(child: &mut Child) { - let _ = child.start_kill(); - let _ = child.wait().await; -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver.rs b/codex-rs/code-mode/src/remote_session/connection/driver.rs deleted file mode 100644 index da30a019b8..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver.rs +++ /dev/null @@ -1,184 +0,0 @@ -use std::collections::VecDeque; -use std::panic::AssertUnwindSafe; -use std::sync::Arc; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::RequestId; -use tokio::sync::mpsc; -use tokio_util::sync::CancellationToken; - -pub(in crate::remote_session) use self::cleanup::SessionCleanup; -use self::delegate_runtime::DelegateRuntime; -use self::request_tracker::RequestTracker; -use self::session_registry::SessionRegistry; -pub(super) use self::types::DriverCommand; -pub(super) use self::types::DriverEvent; -pub(in crate::remote_session) use self::types::RemoteSession; - -mod cell_ids; -mod cleanup; -mod commands; -mod delegate_runtime; -mod request_tracker; -mod responses; -mod session_registry; -mod types; - -pub(super) struct DriverLifecycle { - pub(super) alive: Arc, - pub(super) failure: Arc>>, - pub(super) cancellation: CancellationToken, -} - -pub(super) struct ConnectionDriver { - command_rx: mpsc::Receiver, - event_rx: mpsc::Receiver, - event_tx: mpsc::Sender, - execute_claim_rx: mpsc::UnboundedReceiver, - outgoing_tx: mpsc::Sender, - requests: RequestTracker, - deferred_host_messages: VecDeque, - sessions: SessionRegistry, - delegates: DelegateRuntime, - alive: Arc, - failure: Arc>>, - cancellation: CancellationToken, - failed: bool, -} - -impl ConnectionDriver { - pub(super) fn new( - command_rx: mpsc::Receiver, - event_rx: mpsc::Receiver, - event_tx: mpsc::Sender, - outgoing_tx: mpsc::Sender, - lifecycle: DriverLifecycle, - ) -> (Self, mpsc::UnboundedSender) { - let (execute_claim_tx, execute_claim_rx) = mpsc::unbounded_channel(); - ( - Self { - command_rx, - event_rx, - event_tx: event_tx.clone(), - execute_claim_rx, - outgoing_tx, - requests: RequestTracker::new(), - deferred_host_messages: VecDeque::new(), - sessions: SessionRegistry::new(), - delegates: DelegateRuntime::new(event_tx), - alive: lifecycle.alive, - failure: lifecycle.failure, - cancellation: lifecycle.cancellation, - failed: false, - }, - execute_claim_tx, - ) - } - - pub(super) async fn run(mut self) { - loop { - tokio::select! { - biased; - _ = self.cancellation.cancelled() => { - self.fail("code-mode host connection closed".to_string()); - return; - } - event = self.event_rx.recv() => { - let Some(event) = event else { - self.fail("code-mode host event stream closed".to_string()); - return; - }; - if !self.cancel_dropped_callers() || !self.handle_event(event) { - return; - } - } - claim = self.execute_claim_rx.recv() => { - let Some(request_id) = claim else { - self.fail("code-mode execute claim stream closed".to_string()); - return; - }; - self.requests.claim_execute(request_id); - } - command = self.command_rx.recv() => { - let Some(command) = command else { - self.fail("code-mode host command stream closed".to_string()); - return; - }; - if !self.cancel_dropped_callers() || !self.handle_command(command) { - return; - } - } - } - } - } - - fn handle_event(&mut self, event: DriverEvent) -> bool { - let keep_running = match event { - DriverEvent::HostMessage(message) => self.handle_host_message(message), - DriverEvent::DelegateCompleted { id, result } => self.complete_delegate(id, result), - DriverEvent::RequestCancelled(id) => self.cancel_request(id), - DriverEvent::YieldRequest(id) => self.yield_request(id), - DriverEvent::Failed(reason) => { - self.fail(reason); - false - } - }; - if keep_running { - self.flush_deferred_waits() - } else { - false - } - } - - fn queue_frame(&mut self, frame: EncodedFrame) -> bool { - match self.outgoing_tx.try_send(frame) { - Ok(()) => true, - Err(mpsc::error::TrySendError::Full(_)) => { - self.fail("code-mode host outgoing queue is full".to_string()); - false - } - Err(mpsc::error::TrySendError::Closed(_)) => { - self.fail("code-mode host writer closed".to_string()); - false - } - } - } - - fn fail(&mut self, reason: String) { - if self.failed { - return; - } - self.failed = true; - self.alive.store(false, Ordering::Release); - let reason = { - let mut failure = self - .failure - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - failure.get_or_insert(reason).clone() - }; - self.requests.fail_all(&reason); - let failed_sessions = self.sessions.drain(); - self.delegates.fail_all(failed_sessions); - self.cancellation.cancel(); - } -} - -impl Drop for ConnectionDriver { - fn drop(&mut self) { - self.fail("code-mode connection driver stopped unexpectedly".to_string()); - } -} - -fn notify_cell_closed(delegate: &Arc, cell_id: &CellId) { - let _ = std::panic::catch_unwind(AssertUnwindSafe(|| delegate.cell_closed(cell_id))); -} - -#[cfg(test)] -#[path = "driver_tests.rs"] -mod tests; diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/cell_ids.rs b/codex-rs/code-mode/src/remote_session/connection/driver/cell_ids.rs deleted file mode 100644 index bb8234818d..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/cell_ids.rs +++ /dev/null @@ -1,119 +0,0 @@ -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::RuntimeResponse; -use codex_code_mode_protocol::WaitOutcome; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::WireCellId; -use codex_code_mode_protocol::host::WireRuntimeResponse; -use codex_code_mode_protocol::host::WireWaitOutcome; -use codex_code_mode_protocol::host::WireWaitRequest; - -use super::RemoteSession; - -pub(super) fn public_cell_id(generation: u64, cell_id: &WireCellId) -> CellId { - if generation == 1 { - CellId::new(cell_id.as_str().to_string()) - } else { - CellId::new(format!("g{generation}:{}", cell_id.as_str())) - } -} - -pub(super) fn public_cell_id_from_protocol(generation: u64, cell_id: &CellId) -> CellId { - public_cell_id(generation, &WireCellId::new(cell_id.as_str())) -} - -pub(super) fn remote_cell_id( - session: &RemoteSession, - cell_id: &CellId, -) -> Result { - if session.generation == 1 { - if cell_id.as_str().starts_with('g') && cell_id.as_str().contains(':') { - return Err(format!( - "cell {cell_id} belongs to a stale code-mode host generation" - )); - } - return Ok(WireCellId::new(cell_id.as_str())); - } - let prefix = format!("g{}:", session.generation); - let Some(remote_id) = cell_id.as_str().strip_prefix(&prefix) else { - return Err(format!( - "cell {cell_id} belongs to a stale code-mode host generation" - )); - }; - Ok(WireCellId::new(remote_id)) -} - -pub(super) fn remote_wait_request( - session: &RemoteSession, - request: WaitRequest, -) -> Result { - Ok(WireWaitRequest { - cell_id: remote_cell_id(session, &request.cell_id)?, - yield_time_ms: request.yield_time_ms, - }) -} - -pub(super) fn public_runtime_response( - generation: u64, - response: RuntimeResponse, -) -> RuntimeResponse { - match response { - RuntimeResponse::Yielded { - cell_id, - content_items, - code_mode_host_duration, - } => RuntimeResponse::Yielded { - cell_id: public_cell_id_from_protocol(generation, &cell_id), - content_items, - code_mode_host_duration, - }, - RuntimeResponse::Terminated { - cell_id, - content_items, - code_mode_host_duration, - } => RuntimeResponse::Terminated { - cell_id: public_cell_id_from_protocol(generation, &cell_id), - content_items, - code_mode_host_duration, - }, - RuntimeResponse::Result { - cell_id, - content_items, - result_buffered_for, - error_text, - code_mode_host_duration, - } => RuntimeResponse::Result { - cell_id: public_cell_id_from_protocol(generation, &cell_id), - content_items, - result_buffered_for, - error_text, - code_mode_host_duration, - }, - } -} - -pub(super) fn public_wait_outcome(generation: u64, outcome: WaitOutcome) -> WaitOutcome { - match outcome { - WaitOutcome::LiveCell(response) => { - WaitOutcome::LiveCell(public_runtime_response(generation, response)) - } - WaitOutcome::MissingCell(response) => { - WaitOutcome::MissingCell(public_runtime_response(generation, response)) - } - } -} - -pub(super) fn runtime_response_cell_id(response: &WireRuntimeResponse) -> &WireCellId { - match response { - WireRuntimeResponse::Yielded { cell_id, .. } - | WireRuntimeResponse::Terminated { cell_id, .. } - | WireRuntimeResponse::Result { cell_id, .. } => cell_id, - } -} - -pub(super) fn wait_outcome_cell_id(outcome: &WireWaitOutcome) -> &WireCellId { - match outcome { - WireWaitOutcome::LiveCell(response) | WireWaitOutcome::MissingCell(response) => { - runtime_response_cell_id(response) - } - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/cleanup.rs b/codex-rs/code-mode/src/remote_session/connection/driver/cleanup.rs deleted file mode 100644 index b994b64887..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/cleanup.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::sync::Arc; - -use tokio_util::sync::CancellationToken; - -use super::notify_cell_closed; -use super::session_registry::CellOwner; - -struct CleanupInner { - complete: CancellationToken, -} - -#[derive(Clone)] -pub(in crate::remote_session) struct SessionCleanup { - inner: Arc, -} - -impl SessionCleanup { - pub(in crate::remote_session) fn new() -> Self { - Self { - inner: Arc::new(CleanupInner { - complete: CancellationToken::new(), - }), - } - } - - pub(super) fn fail(&self, cells: Vec) { - for owner in cells { - notify_cell_closed(&owner.delegate, &owner.cell_id); - } - self.inner.complete.cancel(); - } - - pub(in crate::remote_session) async fn wait(&self) { - self.inner.complete.cancelled().await; - } - - pub(in crate::remote_session) fn is_complete(&self) -> bool { - self.inner.complete.is_cancelled() - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/commands.rs b/codex-rs/code-mode/src/remote_session/connection/driver/commands.rs deleted file mode 100644 index 2e83c5bd3b..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/commands.rs +++ /dev/null @@ -1,342 +0,0 @@ -use std::sync::Arc; - -use codex_code_mode_protocol::CodeModeOutputTruncation; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::WaitOutcome; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::HostRequest; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_code_mode_protocol::host::WireOutputTruncation; -use codex_code_mode_protocol::host::WireSessionCellExecutionLimits; -use codex_code_mode_protocol::host::WireWaitRequest; -use tokio::sync::oneshot; -use tokio_util::sync::CancellationToken; - -use super::ConnectionDriver; -use super::cell_ids::remote_cell_id; -use super::cell_ids::remote_wait_request; -use super::types::CancellableRequest; -use super::types::DeferredWait; -use super::types::DeliveredExecute; -use super::types::DriverCommand; -use super::types::ObservationYield; -use super::types::PendingRequest; -use super::types::RemoteSession; - -impl ConnectionDriver { - pub(super) fn handle_command(&mut self, command: DriverCommand) -> bool { - match command { - DriverCommand::OpenSession { - session, - options, - cleanup, - caller_cancellation, - response_tx, - } => self.open_session(session, options, cleanup, caller_cancellation, response_tx), - DriverCommand::Execute { - session, - request, - delegate, - caller_cancellation, - yield_signal, - response_tx, - } => self.execute( - session, - request, - delegate, - caller_cancellation, - yield_signal, - response_tx, - ), - DriverCommand::Wait { - session, - request, - caller_cancellation, - yield_signal, - response_tx, - } => self.wait( - session, - request, - caller_cancellation, - yield_signal, - response_tx, - ), - DriverCommand::Terminate { - session, - request, - response_tx, - } => self.terminate(session, request, response_tx), - DriverCommand::ShutdownSession { - session, - response_tx, - } => self.shutdown_session(session, response_tx), - } - } - - fn open_session( - &mut self, - session: RemoteSession, - options: CodeModeSessionOptions, - cleanup: super::cleanup::SessionCleanup, - caller_cancellation: CancellationToken, - response_tx: oneshot::Sender>, - ) -> bool { - if self.sessions.contains(&session.id) || self.requests.contains_pending_open(&session) { - let _ = response_tx.send(Err(format!( - "code-mode session {} is already open", - session.id - ))); - return true; - } - let limits = match WireSessionCellExecutionLimits::try_from(options.cell_execution_limits) { - Ok(limits) => limits, - Err(error) => { - let _ = response_tx.send(Err(format!( - "failed to encode code-mode session execution limits: {error}" - ))); - return true; - } - }; - let request_id = match self.requests.allocate_id() { - Ok(id) => id, - Err(err) => { - let _ = response_tx.send(Err(err)); - return false; - } - }; - let message = ClientToHost::Request { - id: request_id, - request: HostRequest::OpenSession { - session_id: session.id.clone(), - cell_execution_limits: (limits != WireSessionCellExecutionLimits::default()) - .then_some(limits), - output_truncation: (options.output_truncation == CodeModeOutputTruncation::Server) - .then_some(WireOutputTruncation::Server), - }, - }; - let frame = match EncodedFrame::encode(&message) { - Ok(frame) => frame, - Err(err) => { - let _ = response_tx.send(Err(format!( - "failed to encode code-mode open-session request: {err}" - ))); - return true; - } - }; - let cancellation = CancellableRequest::new(caller_cancellation); - self.requests.insert_pending( - request_id, - PendingRequest::OpenSession { - session, - cleanup, - cancellation, - response_tx, - }, - &self.event_tx, - ); - self.queue_frame(frame) - } - - fn execute( - &mut self, - session: RemoteSession, - request: WireExecuteRequest, - delegate: Arc, - caller_cancellation: CancellationToken, - yield_signal: Option, - response_tx: oneshot::Sender>, - ) -> bool { - if let Err(err) = self.sessions.require_ready(&session) { - let _ = response_tx.send(Err(err)); - return true; - } - let request_id = match self.requests.allocate_id() { - Ok(id) => id, - Err(err) => { - let _ = response_tx.send(Err(err)); - return false; - } - }; - let message = ClientToHost::Request { - id: request_id, - request: HostRequest::Execute { - session_id: session.id.clone(), - request, - }, - }; - let frame = match EncodedFrame::encode(&message) { - Ok(frame) => frame, - Err(err) => { - let _ = response_tx.send(Err(format!( - "code-mode execute request exceeds the IPC frame limit: {err}" - ))); - return true; - } - }; - let (initial_response_tx, initial_response_rx) = oneshot::channel(); - let cancellation = CancellableRequest::new(caller_cancellation); - self.requests.insert_pending( - request_id, - PendingRequest::Execute { - session, - delegate, - response_tx, - initial_response_tx, - initial_response_rx, - cancellation, - yield_observation: ObservationYield::new(yield_signal), - }, - &self.event_tx, - ); - self.queue_frame(frame) - } - - fn wait( - &mut self, - session: RemoteSession, - request: WaitRequest, - caller_cancellation: CancellationToken, - yield_signal: Option, - response_tx: oneshot::Sender>, - ) -> bool { - if let Err(err) = self.sessions.require_ready(&session) { - let _ = response_tx.send(Err(err)); - return true; - } - let max_output_tokens = request.max_output_tokens; - let request = match remote_wait_request(&session, request) { - Ok(request) => request, - Err(err) => { - let _ = response_tx.send(Err(err)); - return true; - } - }; - if self.requests.has_cancelled_wait(&session, &request.cell_id) { - self.requests.push_deferred_wait(DeferredWait { - session, - request, - max_output_tokens, - caller_cancellation, - yield_signal, - response_tx, - }); - return true; - } - self.start_wait( - session, - request, - max_output_tokens, - caller_cancellation, - yield_signal, - response_tx, - ) - } - - pub(super) fn start_wait( - &mut self, - session: RemoteSession, - request: WireWaitRequest, - max_output_tokens: Option, - caller_cancellation: CancellationToken, - yield_signal: Option, - response_tx: oneshot::Sender>, - ) -> bool { - let cell_id = request.cell_id.clone(); - self.send_request( - HostRequest::Wait { - session_id: session.id.clone(), - request, - max_output_tokens, - }, - PendingRequest::Wait { - session, - cell_id, - cancellation: CancellableRequest::new(caller_cancellation), - yield_observation: ObservationYield::new(yield_signal), - response_tx, - }, - ) - } - - fn terminate( - &mut self, - session: RemoteSession, - request: TerminateRequest, - response_tx: oneshot::Sender>, - ) -> bool { - if let Err(err) = self.sessions.require_ready(&session) { - let _ = response_tx.send(Err(err)); - return true; - } - let cell_id = match remote_cell_id(&session, &request.cell_id) { - Ok(cell_id) => cell_id, - Err(err) => { - let _ = response_tx.send(Err(err)); - return true; - } - }; - let pending_cell_id = cell_id.clone(); - self.send_request( - HostRequest::Terminate { - session_id: session.id.clone(), - cell_id, - max_output_tokens: request.max_output_tokens, - }, - PendingRequest::Terminate { - session, - cell_id: pending_cell_id, - response_tx, - }, - ) - } - - fn shutdown_session( - &mut self, - session: RemoteSession, - response_tx: oneshot::Sender>, - ) -> bool { - if let Err(err) = self.sessions.begin_shutdown(&session) { - let _ = response_tx.send(Err(err)); - return true; - } - self.send_request( - HostRequest::ShutdownSession { - session_id: session.id.clone(), - }, - PendingRequest::ShutdownSession { - session, - response_tx, - }, - ) - } - - pub(super) fn send_request(&mut self, request: HostRequest, pending: PendingRequest) -> bool { - let request_id = match self.requests.allocate_id() { - Ok(id) => id, - Err(err) => { - pending.fail(err); - return false; - } - }; - let message = ClientToHost::Request { - id: request_id, - request, - }; - let frame = match EncodedFrame::encode(&message) { - Ok(frame) => frame, - Err(err) => { - pending.fail(format!( - "code-mode request exceeds the IPC frame limit: {err}" - )); - return true; - } - }; - self.requests - .insert_pending(request_id, pending, &self.event_tx); - self.queue_frame(frame) - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/delegate_runtime.rs b/codex-rs/code-mode/src/remote_session/connection/driver/delegate_runtime.rs deleted file mode 100644 index a7353a3f75..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/delegate_runtime.rs +++ /dev/null @@ -1,353 +0,0 @@ -//! Client-side delegate task and closure lifecycle. -//! -//! Cancellation revokes the task's completion path before removing its active-call state. The -//! delegate future may finish later, but it can no longer send a response or affect cell closure. - -use std::collections::HashMap; -use std::collections::HashSet; -use std::collections::VecDeque; - -use codex_code_mode_protocol::CodeModeNestedToolCall; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::DelegateRequest; -use codex_code_mode_protocol::host::DelegateRequestId; -use codex_code_mode_protocol::host::DelegateResponse; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireCellId; -use codex_code_mode_protocol::host::WireResult; -use tokio::sync::mpsc; -use tokio_util::sync::CancellationToken; - -use super::ConnectionDriver; -use super::notify_cell_closed; -use super::session_registry::CellOwner; -use super::session_registry::DelegateTarget; -use super::session_registry::FailedSession; -use super::types::DriverEvent; - -const MAX_RECENT_DELEGATE_REQUEST_IDS: usize = 4096; - -#[derive(Clone, Eq, Hash, PartialEq)] -struct CellKey { - session_id: codex_code_mode_protocol::host::SessionId, - cell_id: codex_code_mode_protocol::CellId, -} - -impl CellKey { - fn for_owner(owner: &CellOwner) -> Self { - Self { - session_id: owner.session_id.clone(), - cell_id: owner.cell_id.clone(), - } - } -} - -struct DelegateCall { - cell: CellKey, - cancellation: CancellationToken, - completion_stop: CancellationToken, -} - -impl DelegateCall { - fn revoke(&self) { - self.cancellation.cancel(); - self.completion_stop.cancel(); - } -} - -enum DelegateTask { - InvokeTool(CodeModeNestedToolCall), - Notify { - call_id: String, - cell_id: codex_code_mode_protocol::CellId, - text: String, - }, -} - -enum DelegateStartError { - Duplicate(DelegateRequestId), - CapacityExceeded, -} - -pub(super) struct DelegateEffects { - pub(super) response: Option<(DelegateRequestId, Result)>, - pub(super) closed_cells: Vec, -} - -impl DelegateEffects { - fn empty() -> Self { - Self { - response: None, - closed_cells: Vec::new(), - } - } - - fn append(&mut self, mut other: Self) { - debug_assert!(self.response.is_none()); - self.response = other.response.take(); - self.closed_cells.append(&mut other.closed_cells); - } -} - -pub(super) struct DelegateRuntime { - calls: HashMap, - seen_requests: HashSet, - request_order: VecDeque, - event_tx: mpsc::Sender, -} - -impl DelegateRuntime { - pub(super) fn new(event_tx: mpsc::Sender) -> Self { - Self { - calls: HashMap::new(), - seen_requests: HashSet::new(), - request_order: VecDeque::new(), - event_tx, - } - } - - fn start( - &mut self, - id: DelegateRequestId, - target: DelegateTarget, - request: DelegateRequest, - ) -> Result<(), DelegateStartError> { - if self.calls.contains_key(&id) || self.seen_requests.contains(&id) { - return Err(DelegateStartError::Duplicate(id)); - } - self.remember_request(id); - if self.calls.len() >= MAX_PENDING_DELEGATE_CALLS { - return Err(DelegateStartError::CapacityExceeded); - } - let cancellation = CancellationToken::new(); - let task_request = match request { - DelegateRequest::InvokeTool { invocation } => { - let mut invocation: CodeModeNestedToolCall = invocation.into(); - invocation.cell_id = target.cell_id.clone(); - DelegateTask::InvokeTool(invocation) - } - DelegateRequest::Notify { - call_id, - cell_id: _, - text, - } => DelegateTask::Notify { - call_id, - cell_id: target.cell_id.clone(), - text, - }, - }; - let delegate = target.delegate; - let task_cancellation = cancellation.clone(); - let delegate_task = tokio::spawn(async move { - match task_request { - DelegateTask::InvokeTool(invocation) => delegate - .invoke_tool(invocation, task_cancellation) - .await - .map(|result| DelegateResponse::ToolResult { result }), - DelegateTask::Notify { - call_id, - cell_id, - text, - } => delegate - .notify(call_id, cell_id, text, task_cancellation) - .await - .map(|()| DelegateResponse::NotificationDelivered), - } - }); - let completion_stop = CancellationToken::new(); - self.calls.insert( - id, - DelegateCall { - cell: CellKey { - session_id: target.session_id, - cell_id: target.cell_id, - }, - cancellation, - completion_stop: completion_stop.clone(), - }, - ); - let event_tx = self.event_tx.clone(); - tokio::spawn(async move { - let result = tokio::select! { - biased; - _ = completion_stop.cancelled() => return, - result = delegate_task => match result { - Ok(result) => result, - Err(err) => Err(format!("code-mode delegate task failed: {err}")), - }, - }; - tokio::select! { - biased; - _ = completion_stop.cancelled() => {} - _ = event_tx.send(DriverEvent::DelegateCompleted { id, result }) => {} - } - }); - Ok(()) - } - - pub(super) fn cancel(&mut self, id: DelegateRequestId) { - if let Some(call) = self.calls.remove(&id) { - call.revoke(); - } - } - - pub(super) fn complete( - &mut self, - id: DelegateRequestId, - result: Result, - ) -> DelegateEffects { - if self.calls.remove(&id).is_none() { - return DelegateEffects::empty(); - } - let mut effects = DelegateEffects::empty(); - effects.response = Some((id, result)); - effects - } - - pub(super) fn close_cell(&mut self, owner: CellOwner) -> DelegateEffects { - let key = CellKey::for_owner(&owner); - self.calls.retain(|_, call| { - if call.cell != key { - return true; - } - call.revoke(); - false - }); - let mut effects = DelegateEffects::empty(); - effects.closed_cells.push(owner); - effects - } - - pub(super) fn close_cells(&mut self, owners: Vec) -> DelegateEffects { - let mut effects = DelegateEffects::empty(); - for owner in owners { - effects.append(self.close_cell(owner)); - } - effects - } - - pub(super) fn fail_all(&mut self, failed_sessions: Vec) { - for (_, call) in self.calls.drain() { - call.revoke(); - } - for session in failed_sessions { - session.cleanup.fail(session.cells); - } - } - - fn remember_request(&mut self, id: DelegateRequestId) { - self.seen_requests.insert(id); - self.request_order.push_back(id); - while self.request_order.len() > MAX_RECENT_DELEGATE_REQUEST_IDS { - if let Some(expired) = self.request_order.pop_front() { - self.seen_requests.remove(&expired); - } - } - } -} - -impl ConnectionDriver { - pub(super) fn start_delegate( - &mut self, - id: DelegateRequestId, - session_id: SessionId, - request: DelegateRequest, - ) -> bool { - let wire_cell_id = match &request { - DelegateRequest::InvokeTool { invocation } => &invocation.cell_id, - DelegateRequest::Notify { cell_id, .. } => cell_id, - }; - let target = match self.sessions.delegate_target(&session_id, wire_cell_id) { - Ok(target) => target, - Err(err) => return self.send_delegate_response(id, Err(err)), - }; - match self.delegates.start(id, target, request) { - Ok(()) => true, - Err(DelegateStartError::Duplicate(id)) => { - self.fail(format!("duplicate code-mode delegate request ID {id:?}")); - false - } - Err(DelegateStartError::CapacityExceeded) => self.send_delegate_response( - id, - Err(format!( - "code-mode host exceeded the limit of {MAX_PENDING_DELEGATE_CALLS} pending delegate calls" - )), - ), - } - } - - pub(super) fn complete_delegate( - &mut self, - id: DelegateRequestId, - result: Result, - ) -> bool { - let effects = self.delegates.complete(id, result); - self.apply_delegate_effects(effects) - } - - fn send_delegate_response( - &mut self, - id: DelegateRequestId, - result: Result, - ) -> bool { - let message = ClientToHost::DelegateResponse { - id, - result: WireResult::from_result(result), - }; - let frame = match EncodedFrame::encode(&message) { - Ok(frame) => frame, - Err(err) => { - let fallback = ClientToHost::DelegateResponse { - id, - result: WireResult::Err { - message: format!( - "code-mode delegate response exceeds the IPC frame limit: {err}" - ), - }, - }; - match EncodedFrame::encode(&fallback) { - Ok(frame) => frame, - Err(fallback_err) => { - self.fail(format!( - "failed to encode code-mode delegate error response: {fallback_err}" - )); - return false; - } - } - } - }; - self.queue_frame(frame) - } - - pub(super) fn close_cell(&mut self, session_id: SessionId, cell_id: WireCellId) -> bool { - let owner = match self.sessions.remove_cell(&session_id, &cell_id) { - Ok(owner) => owner, - Err(err) => { - self.fail(err); - return false; - } - }; - let effects = self.delegates.close_cell(owner); - self.apply_delegate_effects(effects) - } - - pub(super) fn close_session_locally(&mut self, session_id: &SessionId) -> DelegateEffects { - self.requests.remove_unclaimed_for_session(session_id); - let owners = self.sessions.remove_session(session_id); - self.delegates.close_cells(owners) - } - - pub(super) fn apply_delegate_effects(&mut self, effects: DelegateEffects) -> bool { - if let Some((id, result)) = effects.response - && !self.send_delegate_response(id, result) - { - return false; - } - for closed in effects.closed_cells { - notify_cell_closed(&closed.delegate, &closed.cell_id); - } - true - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/request_tracker.rs b/codex-rs/code-mode/src/remote_session/connection/driver/request_tracker.rs deleted file mode 100644 index 75f6724b65..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/request_tracker.rs +++ /dev/null @@ -1,198 +0,0 @@ -use std::collections::HashMap; -use std::collections::VecDeque; - -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireCellId; -use tokio::sync::mpsc; - -use super::types::DeferredWait; -use super::types::DriverEvent; -use super::types::InitialResponse; -use super::types::PendingRequest; -use super::types::RemoteSession; -use super::types::UnclaimedExecute; - -pub(super) enum CancellationAction { - Send(RequestId), - Terminate { - request_id: RequestId, - execute: UnclaimedExecute, - }, -} - -pub(super) struct RequestTracker { - pending: HashMap, - unclaimed_executes: HashMap, - initial_responses: HashMap, - deferred_waits: VecDeque, - next_request_id: i64, -} - -impl RequestTracker { - pub(super) fn new() -> Self { - Self { - pending: HashMap::new(), - unclaimed_executes: HashMap::new(), - initial_responses: HashMap::new(), - deferred_waits: VecDeque::new(), - next_request_id: 1, - } - } - - pub(super) fn contains_pending_open(&self, session: &RemoteSession) -> bool { - self.pending.values().any(|pending| { - matches!( - pending, - PendingRequest::OpenSession { - session: pending_session, - .. - } if pending_session.id == session.id - ) - }) - } - - pub(super) fn has_pending_execute_for_session(&self, session_id: &SessionId) -> bool { - self.pending.values().any(|request| { - matches!( - request, - PendingRequest::Execute { session, .. } if session.id == *session_id - ) - }) - } - - pub(super) fn allocate_id(&mut self) -> Result { - let id = self.next_request_id; - self.next_request_id = self - .next_request_id - .checked_add(1) - .ok_or_else(|| "code-mode host request ID space exhausted".to_string())?; - Ok(RequestId::new(id)) - } - - pub(super) fn insert_pending( - &mut self, - id: RequestId, - mut pending: PendingRequest, - event_tx: &mpsc::Sender, - ) { - if let Some(observation) = pending.yield_observation() { - observation.spawn_watcher(id, event_tx.clone()); - } - self.pending.insert(id, pending); - if let Some(cancellation) = self - .pending - .get_mut(&id) - .and_then(PendingRequest::cancellation_mut) - { - cancellation.spawn_watcher(id, event_tx.clone()); - } - } - - pub(super) fn remove_pending(&mut self, id: RequestId) -> Option { - self.pending.remove(&id) - } - - pub(super) fn insert_initial_response(&mut self, id: RequestId, response: InitialResponse) { - self.initial_responses.insert(id, response); - } - - pub(super) fn remove_initial_response(&mut self, id: RequestId) -> Option { - self.initial_responses.remove(&id) - } - - pub(super) fn insert_unclaimed_execute(&mut self, id: RequestId, execute: UnclaimedExecute) { - self.unclaimed_executes.insert(id, execute); - } - - pub(super) fn claim_execute(&mut self, id: RequestId) { - self.unclaimed_executes.remove(&id); - } - - pub(super) fn collect_cancellations(&mut self) -> Vec { - let mut actions = self - .pending - .iter_mut() - .filter_map(|(id, pending)| { - let cancellation = pending.cancellation_mut()?; - (cancellation.is_cancelled() && cancellation.mark_reported()) - .then_some(CancellationAction::Send(*id)) - }) - .collect::>(); - actions.extend( - self.unclaimed_executes - .extract_if(|_, execute| { - execute.cancellation.is_cancelled() && execute.cancellation.mark_reported() - }) - .map(|(request_id, execute)| CancellationAction::Terminate { - request_id, - execute, - }), - ); - actions - } - - pub(super) fn mark_cancelled(&mut self, id: RequestId) -> Option { - if let Some(cancellation) = self - .pending - .get_mut(&id) - .and_then(PendingRequest::cancellation_mut) - { - return cancellation - .mark_reported() - .then_some(CancellationAction::Send(id)); - } - let execute = self.unclaimed_executes.get_mut(&id)?; - if !execute.cancellation.mark_reported() { - return None; - } - self.unclaimed_executes - .remove(&id) - .map(|execute| CancellationAction::Terminate { - request_id: id, - execute, - }) - } - - pub(super) fn has_cancelled_wait(&self, session: &RemoteSession, cell_id: &WireCellId) -> bool { - self.pending.values().any(|pending| { - matches!( - pending, - PendingRequest::Wait { - session: pending_session, - cell_id: pending_cell_id, - cancellation, - .. - } if pending_session == session - && pending_cell_id == cell_id - && cancellation.is_cancelled() - ) - }) - } - - pub(super) fn push_deferred_wait(&mut self, wait: DeferredWait) { - self.deferred_waits.push_back(wait); - } - - pub(super) fn take_deferred_waits(&mut self) -> VecDeque { - std::mem::take(&mut self.deferred_waits) - } - - pub(super) fn remove_unclaimed_for_session(&mut self, session_id: &SessionId) { - self.unclaimed_executes - .retain(|_, execute| &execute.session.id != session_id); - } - - pub(super) fn fail_all(&mut self, reason: &str) { - for (_, pending) in self.pending.drain() { - pending.fail(reason.to_string()); - } - self.unclaimed_executes.clear(); - for (_, initial) in self.initial_responses.drain() { - let _ = initial.response_tx.send(Err(reason.to_string())); - } - for wait in self.deferred_waits.drain(..) { - let _ = wait.response_tx.send(Err(reason.to_string())); - } - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/responses.rs b/codex-rs/code-mode/src/remote_session/connection/driver/responses.rs deleted file mode 100644 index 7f20fb6e23..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/responses.rs +++ /dev/null @@ -1,476 +0,0 @@ -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::HostRequest; -use codex_code_mode_protocol::host::HostResponse; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::WireCellId; -use tokio::sync::oneshot; - -use super::ConnectionDriver; -use super::cell_ids::public_runtime_response; -use super::cell_ids::public_wait_outcome; -use super::cell_ids::runtime_response_cell_id; -use super::cell_ids::wait_outcome_cell_id; -use super::request_tracker::CancellationAction; -use super::session_registry::CellAdmissionError; -use super::types::DeliveredExecute; -use super::types::InitialResponse; -use super::types::PendingRequest; -use super::types::RemoteSession; -use super::types::UnclaimedExecute; - -impl ConnectionDriver { - pub(super) fn flush_deferred_waits(&mut self) -> bool { - let mut deferred = self.requests.take_deferred_waits(); - while let Some(wait) = deferred.pop_front() { - if wait.caller_cancellation.is_cancelled() { - let _ = wait - .response_tx - .send(Err("code-mode request cancelled".to_string())); - continue; - } - if self - .requests - .has_cancelled_wait(&wait.session, &wait.request.cell_id) - { - self.requests.push_deferred_wait(wait); - continue; - } - if !self.start_wait( - wait.session, - wait.request, - wait.max_output_tokens, - wait.caller_cancellation, - wait.yield_signal, - wait.response_tx, - ) { - for wait in deferred { - let _ = wait - .response_tx - .send(Err("code-mode host connection closed".to_string())); - } - return false; - } - } - true - } - - pub(super) fn handle_host_message(&mut self, message: HostToClient) -> bool { - if self.should_defer_host_message(&message) { - if self.deferred_host_messages.len() - >= codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS - { - self.fail( - "code-mode host exceeded deferred cross-socket message limit".to_string(), - ); - return false; - } - self.deferred_host_messages.push_back(message); - return true; - } - if !self.dispatch_host_message(message) { - return false; - } - for _ in 0..self.deferred_host_messages.len() { - let Some(message) = self.deferred_host_messages.pop_front() else { - break; - }; - if self.should_defer_host_message(&message) { - self.deferred_host_messages.push_back(message); - } else if !self.dispatch_host_message(message) { - return false; - } - } - true - } - - fn should_defer_host_message(&self, message: &HostToClient) -> bool { - match message { - HostToClient::DelegateRequest { - session_id, - request, - .. - } => { - let cell_id = match request { - codex_code_mode_protocol::host::DelegateRequest::InvokeTool { invocation } => { - &invocation.cell_id - } - codex_code_mode_protocol::host::DelegateRequest::Notify { cell_id, .. } => { - cell_id - } - }; - !self.sessions.contains_cell(session_id, cell_id) - && self.requests.has_pending_execute_for_session(session_id) - } - HostToClient::Response { .. } - | HostToClient::InitialResponse { .. } - | HostToClient::CellClosed { .. } - | HostToClient::CancelDelegateRequest { .. } - | HostToClient::HostHello(_) - | HostToClient::HandshakeRejected { .. } => false, - } - } - - fn dispatch_host_message(&mut self, message: HostToClient) -> bool { - match message { - HostToClient::Response { id, result } => { - self.complete_request(id, result.into_result()) - } - HostToClient::InitialResponse { id, result } => { - self.complete_initial_response(id, result.into_result()) - } - HostToClient::DelegateRequest { - id, - session_id, - request, - } => self.start_delegate(id, session_id, request), - HostToClient::CancelDelegateRequest { id } => { - self.deferred_host_messages.retain(|message| { - !matches!( - message, - HostToClient::DelegateRequest { id: deferred_id, .. } if *deferred_id == id - ) - }); - self.delegates.cancel(id); - true - } - HostToClient::CellClosed { - session_id, - cell_id, - } => self.close_cell(session_id, cell_id), - HostToClient::HostHello(_) | HostToClient::HandshakeRejected { .. } => { - self.fail("code-mode host sent a second handshake response".to_string()); - false - } - } - } - - fn complete_request(&mut self, id: RequestId, result: Result) -> bool { - let Some(pending) = self.requests.remove_pending(id) else { - self.fail(format!("code-mode host returned unknown request ID {id:?}")); - return false; - }; - match pending { - PendingRequest::OpenSession { - session, - cleanup, - cancellation, - response_tx, - } => match result { - Ok(HostResponse::SessionReady { session_id }) if session_id == session.id => { - let abandoned = cancellation.is_cancelled() || response_tx.is_closed(); - self.sessions.insert_ready(session.clone(), cleanup); - if abandoned || response_tx.send(Ok(())).is_err() { - return self.shutdown_abandoned_session(session); - } - } - Ok(_) => { - let reason = - "code-mode host returned an invalid open-session response".to_string(); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Err(err) => { - let _ = response_tx.send(Err(err)); - } - }, - PendingRequest::Execute { - session, - delegate, - response_tx, - initial_response_tx, - initial_response_rx, - cancellation, - yield_observation, - } => match result { - Ok(HostResponse::ExecutionStarted { cell_id }) => { - // The host owns a checked, never-reused ID sequence. Retain only live - // IDs so client memory scales with concurrency, not session lifetime. - let remote_cell_id = cell_id.clone(); - let public_id = match self.sessions.admit_cell(&session, cell_id, delegate) { - Ok(public_id) => public_id, - Err(CellAdmissionError::MissingSession) => { - let _ = response_tx - .send(Err("code-mode session closed during execute".to_string())); - return true; - } - Err(CellAdmissionError::DuplicateCell) => { - let reason = format!( - "code-mode host reused live cell {} in session {}", - remote_cell_id.as_str(), - session.id - ); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - }; - self.requests.insert_initial_response( - id, - InitialResponse { - generation: session.generation, - cell_id: remote_cell_id.clone(), - response_tx: initial_response_tx, - _yield_observation: yield_observation, - }, - ); - let started = StartedCell::from_result_receiver(public_id, initial_response_rx); - if cancellation.is_cancelled() || response_tx.is_closed() { - return self.terminate_abandoned_cell(session, remote_cell_id); - } - let delivered = DeliveredExecute { - request_id: id, - started, - }; - if response_tx.send(Ok(delivered)).is_err() { - return self.terminate_abandoned_cell(session, remote_cell_id); - } - self.requests.insert_unclaimed_execute( - id, - UnclaimedExecute { - session, - cell_id: remote_cell_id, - cancellation, - }, - ); - } - Ok(_) => { - let reason = "code-mode host returned an invalid execute response".to_string(); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Err(err) => { - let _ = response_tx.send(Err(err)); - } - }, - PendingRequest::Wait { - session, - cell_id, - cancellation: _, - yield_observation: _, - response_tx, - } => { - let result = match result { - Ok(HostResponse::WaitCompleted { outcome }) => { - if wait_outcome_cell_id(&outcome) != &cell_id { - let reason = format!( - "code-mode host returned cell {} for request targeting {}", - wait_outcome_cell_id(&outcome).as_str(), - cell_id.as_str() - ); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Ok(public_wait_outcome(session.generation, outcome.into())) - } - Ok(_) => { - let reason = "code-mode host returned an invalid cell response".to_string(); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Err(err) => Err(err), - }; - let _ = response_tx.send(result); - } - PendingRequest::Terminate { - session, - cell_id, - response_tx, - } => { - let result = match result { - Ok(HostResponse::WaitCompleted { outcome }) => { - if wait_outcome_cell_id(&outcome) != &cell_id { - let reason = format!( - "code-mode host returned cell {} for request targeting {}", - wait_outcome_cell_id(&outcome).as_str(), - cell_id.as_str() - ); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - public_wait_outcome(session.generation, outcome.into()) - } - Ok(_) => { - let reason = "code-mode host returned an invalid cell response".to_string(); - let _ = response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Err(err) => { - let _ = response_tx.send(Err(err)); - return true; - } - }; - let _ = response_tx.send(Ok(result)); - } - PendingRequest::ShutdownSession { - session, - response_tx, - } => match result { - Ok(HostResponse::SessionClosed { session_id }) if session_id == session.id => { - let effects = self.close_session_locally(&session.id); - if !self.apply_delegate_effects(effects) { - return false; - } - let _ = response_tx.send(Ok(())); - } - Ok(_) => { - let err = "code-mode host returned an invalid shutdown response".to_string(); - let _ = response_tx.send(Err(err.clone())); - self.fail(err); - return false; - } - Err(err) => { - let _ = response_tx.send(Err(err.clone())); - self.fail(err); - return false; - } - }, - } - true - } - - pub(super) fn cancel_dropped_callers(&mut self) -> bool { - for action in self.requests.collect_cancellations() { - if !self.apply_cancellation(action) { - return false; - } - } - true - } - - pub(super) fn cancel_request(&mut self, id: RequestId) -> bool { - self.requests - .mark_cancelled(id) - .is_none_or(|action| self.apply_cancellation(action)) - } - - fn apply_cancellation(&mut self, action: CancellationAction) -> bool { - match action { - CancellationAction::Send(id) => self.send_cancel_request(id), - CancellationAction::Terminate { - request_id, - execute, - } => { - if !self.send_cancel_request(request_id) { - return false; - } - self.terminate_abandoned_cell(execute.session, execute.cell_id) - } - } - } - - pub(super) fn yield_request(&mut self, id: RequestId) -> bool { - let frame = match EncodedFrame::encode(&ClientToHost::YieldRequest { id }) { - Ok(frame) => frame, - Err(err) => { - self.fail(format!("failed to encode code-mode yield request: {err}")); - return false; - } - }; - self.queue_frame(frame) - } - - fn send_cancel_request(&mut self, id: RequestId) -> bool { - let message = ClientToHost::CancelRequest { id }; - let frame = match EncodedFrame::encode(&message) { - Ok(frame) => frame, - Err(err) => { - self.fail(format!( - "failed to encode code-mode cancellation request: {err}" - )); - return false; - } - }; - self.queue_frame(frame) - } - - fn shutdown_abandoned_session(&mut self, session: RemoteSession) -> bool { - let Some(should_shutdown) = self.sessions.begin_abandoned_shutdown(&session.id) else { - self.fail(format!( - "code-mode host committed abandoned session {} without local state", - session.id - )); - return false; - }; - if !should_shutdown { - return true; - } - let (response_tx, response_rx) = oneshot::channel(); - drop(response_rx); - self.send_request( - HostRequest::ShutdownSession { - session_id: session.id.clone(), - }, - PendingRequest::ShutdownSession { - session, - response_tx, - }, - ) - } - - fn terminate_abandoned_cell(&mut self, session: RemoteSession, cell_id: WireCellId) -> bool { - let Some(is_closing) = self.sessions.is_closing(&session.id) else { - self.fail(format!( - "code-mode host admitted an abandoned cell in unknown session {}", - session.id - )); - return false; - }; - if is_closing { - return true; - } - let (response_tx, response_rx) = oneshot::channel(); - drop(response_rx); - self.send_request( - HostRequest::Terminate { - session_id: session.id.clone(), - cell_id: cell_id.clone(), - max_output_tokens: None, - }, - PendingRequest::Terminate { - session, - cell_id, - response_tx, - }, - ) - } - - fn complete_initial_response( - &mut self, - id: RequestId, - result: Result, - ) -> bool { - let Some(initial) = self.requests.remove_initial_response(id) else { - self.fail(format!( - "code-mode host returned initial response for unknown request ID {id:?}" - )); - return false; - }; - let response = match result { - Ok(response) if runtime_response_cell_id(&response) == &initial.cell_id => { - Ok(public_runtime_response(initial.generation, response.into())) - } - Ok(response) => { - let reason = format!( - "code-mode host returned initial response for cell {} instead of {}", - runtime_response_cell_id(&response).as_str(), - initial.cell_id.as_str() - ); - let _ = initial.response_tx.send(Err(reason.clone())); - self.fail(reason); - return false; - } - Err(err) => Err(err), - }; - let _ = initial.response_tx.send(response); - true - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/session_registry.rs b/codex-rs/code-mode/src/remote_session/connection/driver/session_registry.rs deleted file mode 100644 index d3dda96b85..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/session_registry.rs +++ /dev/null @@ -1,208 +0,0 @@ -use std::collections::HashMap; -use std::sync::Arc; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireCellId; - -use super::cell_ids::public_cell_id; -use super::cleanup::SessionCleanup; -use super::types::RemoteSession; - -pub(super) struct CellOwner { - pub(super) session_id: SessionId, - pub(super) cell_id: CellId, - pub(super) delegate: Arc, -} - -pub(super) struct DelegateTarget { - pub(super) session_id: SessionId, - pub(super) cell_id: CellId, - pub(super) delegate: Arc, -} - -pub(super) struct FailedSession { - pub(super) cleanup: SessionCleanup, - pub(super) cells: Vec, -} - -pub(super) enum CellAdmissionError { - MissingSession, - DuplicateCell, -} - -struct SessionRecord { - remote: RemoteSession, - cleanup: SessionCleanup, - phase: SessionPhase, - cells: HashMap, -} - -#[derive(Clone, Copy, Eq, PartialEq)] -enum SessionPhase { - Ready, - Closing, -} - -pub(super) struct SessionRegistry { - records: HashMap, -} - -impl SessionRegistry { - pub(super) fn new() -> Self { - Self { - records: HashMap::new(), - } - } - - pub(super) fn contains(&self, session_id: &SessionId) -> bool { - self.records.contains_key(session_id) - } - - pub(super) fn contains_cell(&self, session_id: &SessionId, cell_id: &WireCellId) -> bool { - self.records - .get(session_id) - .is_some_and(|session| session.cells.contains_key(cell_id)) - } - - pub(super) fn insert_ready(&mut self, session: RemoteSession, cleanup: SessionCleanup) { - self.records.insert( - session.id.clone(), - SessionRecord { - remote: session, - cleanup, - phase: SessionPhase::Ready, - cells: HashMap::new(), - }, - ); - } - - pub(super) fn require_ready(&self, session: &RemoteSession) -> Result<(), String> { - let record = self - .records - .get(&session.id) - .ok_or_else(|| format!("unknown code-mode session {}", session.id))?; - if record.remote != *session { - return Err("stale code-mode session generation".to_string()); - } - if record.phase != SessionPhase::Ready { - return Err("code-mode session is shutting down".to_string()); - } - Ok(()) - } - - pub(super) fn begin_shutdown(&mut self, session: &RemoteSession) -> Result<(), String> { - let record = self - .records - .get_mut(&session.id) - .ok_or_else(|| format!("unknown code-mode session {}", session.id))?; - if record.remote != *session { - return Err("stale code-mode session generation".to_string()); - } - if record.phase == SessionPhase::Closing { - return Err("code-mode session is already closing".to_string()); - } - record.phase = SessionPhase::Closing; - Ok(()) - } - - pub(super) fn begin_abandoned_shutdown(&mut self, session_id: &SessionId) -> Option { - let record = self.records.get_mut(session_id)?; - if record.phase == SessionPhase::Closing { - return Some(false); - } - record.phase = SessionPhase::Closing; - Some(true) - } - - pub(super) fn is_closing(&self, session_id: &SessionId) -> Option { - self.records - .get(session_id) - .map(|record| record.phase == SessionPhase::Closing) - } - - pub(super) fn admit_cell( - &mut self, - session: &RemoteSession, - cell_id: WireCellId, - delegate: Arc, - ) -> Result { - let Some(record) = self.records.get_mut(&session.id) else { - return Err(CellAdmissionError::MissingSession); - }; - if record.cells.contains_key(&cell_id) { - return Err(CellAdmissionError::DuplicateCell); - } - let public_id = public_cell_id(session.generation, &cell_id); - record.cells.insert( - cell_id, - CellOwner { - session_id: session.id.clone(), - cell_id: public_id.clone(), - delegate, - }, - ); - Ok(public_id) - } - - pub(super) fn delegate_target( - &self, - session_id: &SessionId, - cell_id: &WireCellId, - ) -> Result { - let session = self - .records - .get(session_id) - .ok_or_else(|| format!("code-mode host delegated for unknown session {session_id}"))?; - let owner = session.cells.get(cell_id).ok_or_else(|| { - format!( - "code-mode host delegated for unknown cell {} in session {session_id}", - cell_id.as_str() - ) - })?; - Ok(DelegateTarget { - session_id: session_id.clone(), - cell_id: owner.cell_id.clone(), - delegate: Arc::clone(&owner.delegate), - }) - } - - pub(super) fn remove_cell( - &mut self, - session_id: &SessionId, - cell_id: &WireCellId, - ) -> Result { - let session = self.records.get_mut(session_id).ok_or_else(|| { - format!( - "code-mode host closed cell {} in unknown session {session_id}", - cell_id.as_str() - ) - })?; - session - .cells - .remove(cell_id) - .ok_or_else(|| format!("code-mode host closed unknown cell in session {session_id}")) - } - - pub(super) fn remove_session(&mut self, session_id: &SessionId) -> Vec { - let Some(session) = self.records.remove(session_id) else { - return Vec::new(); - }; - session.cells.into_values().collect() - } - - pub(super) fn drain(&mut self) -> Vec { - let sessions = std::mem::take(&mut self.records); - sessions - .into_values() - .map(|session| { - let cells = session.cells.into_values().collect(); - FailedSession { - cleanup: session.cleanup, - cells, - } - }) - .collect() - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver/types.rs b/codex-rs/code-mode/src/remote_session/connection/driver/types.rs deleted file mode 100644 index 796165f747..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver/types.rs +++ /dev/null @@ -1,244 +0,0 @@ -use std::sync::Arc; - -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::RuntimeResponse; -use codex_code_mode_protocol::StartedCell; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::WaitOutcome; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::DelegateRequestId; -use codex_code_mode_protocol::host::DelegateResponse; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireCellId; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_code_mode_protocol::host::WireWaitRequest; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio_util::sync::CancellationToken; -use tokio_util::task::AbortOnDropHandle; - -use super::cleanup::SessionCleanup; - -#[derive(Clone, Debug, Eq, PartialEq)] -pub(in crate::remote_session) struct RemoteSession { - pub(in crate::remote_session) id: SessionId, - pub(in crate::remote_session) generation: u64, -} - -pub(in crate::remote_session::connection) enum DriverCommand { - OpenSession { - session: RemoteSession, - options: CodeModeSessionOptions, - cleanup: SessionCleanup, - caller_cancellation: CancellationToken, - response_tx: oneshot::Sender>, - }, - Execute { - session: RemoteSession, - delegate: Arc, - request: WireExecuteRequest, - caller_cancellation: CancellationToken, - yield_signal: Option, - response_tx: oneshot::Sender>, - }, - Wait { - session: RemoteSession, - request: WaitRequest, - caller_cancellation: CancellationToken, - yield_signal: Option, - response_tx: oneshot::Sender>, - }, - Terminate { - session: RemoteSession, - request: TerminateRequest, - response_tx: oneshot::Sender>, - }, - ShutdownSession { - session: RemoteSession, - response_tx: oneshot::Sender>, - }, -} - -pub(in crate::remote_session::connection) enum DriverEvent { - HostMessage(HostToClient), - DelegateCompleted { - id: DelegateRequestId, - result: Result, - }, - RequestCancelled(RequestId), - YieldRequest(RequestId), - Failed(String), -} - -pub(super) struct CancellableRequest { - caller_cancellation: CancellationToken, - watcher_stop: CancellationToken, - reported: bool, -} - -impl CancellableRequest { - pub(super) fn new(caller_cancellation: CancellationToken) -> Self { - Self { - caller_cancellation, - watcher_stop: CancellationToken::new(), - reported: false, - } - } - - pub(super) fn is_cancelled(&self) -> bool { - self.caller_cancellation.is_cancelled() - } - - pub(super) fn mark_reported(&mut self) -> bool { - if self.reported { - return false; - } - self.reported = true; - true - } - - pub(super) fn spawn_watcher(&self, id: RequestId, event_tx: mpsc::Sender) { - let caller_cancellation = self.caller_cancellation.clone(); - let watcher_stop = self.watcher_stop.clone(); - tokio::spawn(async move { - tokio::select! { - _ = caller_cancellation.cancelled() => { - let _ = event_tx.send(DriverEvent::RequestCancelled(id)).await; - } - _ = watcher_stop.cancelled() => {} - } - }); - } -} - -impl Drop for CancellableRequest { - fn drop(&mut self) { - self.watcher_stop.cancel(); - } -} - -/// Owns the watcher until an observation's final response is received. -pub(super) struct ObservationYield { - signal: Option, - watcher: Option>, -} - -impl ObservationYield { - pub(super) fn new(signal: Option) -> Self { - Self { - signal, - watcher: None, - } - } - - pub(super) fn spawn_watcher(&mut self, id: RequestId, event_tx: mpsc::Sender) { - let Some(signal) = self.signal.take() else { - return; - }; - self.watcher = Some(AbortOnDropHandle::new(tokio::spawn(async move { - signal.cancelled().await; - let _ = event_tx.send(DriverEvent::YieldRequest(id)).await; - }))); - } -} - -pub(super) struct InitialResponse { - pub(super) generation: u64, - pub(super) cell_id: WireCellId, - pub(super) response_tx: oneshot::Sender>, - pub(super) _yield_observation: ObservationYield, -} - -pub(in crate::remote_session::connection) struct DeliveredExecute { - pub(in crate::remote_session::connection) request_id: RequestId, - pub(in crate::remote_session::connection) started: StartedCell, -} - -pub(super) struct UnclaimedExecute { - pub(super) session: RemoteSession, - pub(super) cell_id: WireCellId, - pub(super) cancellation: CancellableRequest, -} - -pub(super) enum PendingRequest { - OpenSession { - session: RemoteSession, - cleanup: SessionCleanup, - cancellation: CancellableRequest, - response_tx: oneshot::Sender>, - }, - Execute { - session: RemoteSession, - delegate: Arc, - response_tx: oneshot::Sender>, - initial_response_tx: oneshot::Sender>, - initial_response_rx: oneshot::Receiver>, - cancellation: CancellableRequest, - yield_observation: ObservationYield, - }, - Wait { - session: RemoteSession, - cell_id: WireCellId, - cancellation: CancellableRequest, - yield_observation: ObservationYield, - response_tx: oneshot::Sender>, - }, - Terminate { - session: RemoteSession, - cell_id: WireCellId, - response_tx: oneshot::Sender>, - }, - ShutdownSession { - session: RemoteSession, - response_tx: oneshot::Sender>, - }, -} - -pub(super) struct DeferredWait { - pub(super) session: RemoteSession, - pub(super) request: WireWaitRequest, - pub(super) max_output_tokens: Option, - pub(super) caller_cancellation: CancellationToken, - pub(super) yield_signal: Option, - pub(super) response_tx: oneshot::Sender>, -} - -impl PendingRequest { - pub(super) fn cancellation_mut(&mut self) -> Option<&mut CancellableRequest> { - match self { - Self::OpenSession { cancellation, .. } - | Self::Execute { cancellation, .. } - | Self::Wait { cancellation, .. } => Some(cancellation), - Self::Terminate { .. } | Self::ShutdownSession { .. } => None, - } - } - - pub(super) fn yield_observation(&mut self) -> Option<&mut ObservationYield> { - match self { - Self::Execute { - yield_observation, .. - } - | Self::Wait { - yield_observation, .. - } => Some(yield_observation), - _ => None, - } - } - - pub(super) fn fail(self, reason: String) { - match self { - Self::OpenSession { response_tx, .. } | Self::ShutdownSession { response_tx, .. } => { - let _ = response_tx.send(Err(reason)); - } - Self::Execute { response_tx, .. } => { - let _ = response_tx.send(Err(reason)); - } - Self::Wait { response_tx, .. } | Self::Terminate { response_tx, .. } => { - let _ = response_tx.send(Err(reason)); - } - } - } -} diff --git a/codex-rs/code-mode/src/remote_session/connection/driver_tests.rs b/codex-rs/code-mode/src/remote_session/connection/driver_tests.rs deleted file mode 100644 index 14b0b42abb..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/driver_tests.rs +++ /dev/null @@ -1,2210 +0,0 @@ -use std::sync::Arc; -use std::sync::Mutex as StdMutex; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::AtomicUsize; -use std::sync::atomic::Ordering; -use std::time::Duration; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::CodeModeNestedToolCall; -use codex_code_mode_protocol::CodeModeOutputTruncation; -use codex_code_mode_protocol::CodeModeSession; -use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::CodeModeSessionOptions; -use codex_code_mode_protocol::NotificationFuture; -use codex_code_mode_protocol::TerminateRequest; -use codex_code_mode_protocol::ToolInvocationFuture; -use codex_code_mode_protocol::WaitRequest; -use codex_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY; -use codex_code_mode_protocol::host::Capability; -use codex_code_mode_protocol::host::CapabilitySet; -use codex_code_mode_protocol::host::ClientToHost; -use codex_code_mode_protocol::host::DelegateRequest; -use codex_code_mode_protocol::host::DelegateRequestId; -use codex_code_mode_protocol::host::EncodedFrame; -use codex_code_mode_protocol::host::HostRequest; -use codex_code_mode_protocol::host::HostResponse; -use codex_code_mode_protocol::host::HostToClient; -use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS; -use codex_code_mode_protocol::host::RequestId; -use codex_code_mode_protocol::host::SessionId; -use codex_code_mode_protocol::host::WireExecuteRequest; -use codex_code_mode_protocol::host::WireNestedToolCall; -use codex_code_mode_protocol::host::WireResult; -use codex_code_mode_protocol::host::WireRuntimeResponse; -use codex_code_mode_protocol::host::WireSessionCellExecutionLimits; -use codex_code_mode_protocol::host::WireWaitOutcome; -use codex_protocol::ToolName; -use pretty_assertions::assert_eq; -use serde_json::json; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio_util::sync::CancellationToken; - -use super::super::Connection; -use super::super::DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT; -use super::ConnectionDriver; -use super::DriverCommand; -use super::DriverEvent; -use super::DriverLifecycle; -use super::RemoteSession; -use super::SessionCleanup; -use crate::remote_session::OwnedCodeModeHost; -use crate::remote_session::ProcessOwnedCodeModeSession; - -struct DriverHarness { - command_tx: mpsc::Sender, - event_tx: mpsc::Sender, - execute_claim_tx: mpsc::UnboundedSender, - outgoing_rx: mpsc::Receiver, - cancellation: CancellationToken, - alive: Arc, - failure: Arc>>, - driver_task: tokio::task::JoinHandle<()>, -} - -impl DriverHarness { - fn start() -> Self { - let (command_tx, command_rx) = mpsc::channel(/*max_capacity*/ 16); - let (event_tx, event_rx) = mpsc::channel(/*max_capacity*/ 16); - let (outgoing_tx, outgoing_rx) = mpsc::channel(/*max_capacity*/ 16); - let cancellation = CancellationToken::new(); - let alive = Arc::new(AtomicBool::new(true)); - let failure = Arc::new(StdMutex::new(None)); - let (driver, execute_claim_tx) = ConnectionDriver::new( - command_rx, - event_rx, - event_tx.clone(), - outgoing_tx, - DriverLifecycle { - alive: Arc::clone(&alive), - failure: Arc::clone(&failure), - cancellation: cancellation.clone(), - }, - ); - let driver_task = tokio::spawn(driver.run()); - Self { - command_tx, - event_tx, - execute_claim_tx, - outgoing_rx, - cancellation, - alive, - failure, - driver_task, - } - } - - async fn open(&mut self, session: RemoteSession) -> SessionCleanup { - let cleanup = SessionCleanup::new(); - let (response_tx, response_rx) = oneshot::channel(); - self.command_tx - .send(DriverCommand::OpenSession { - session: session.clone(), - options: CodeModeSessionOptions::default(), - cleanup: cleanup.clone(), - caller_cancellation: CancellationToken::new(), - response_tx, - }) - .await - .expect("open command"); - self.accept_open(session.id, CodeModeOutputTruncation::Client) - .await; - response_rx - .await - .expect("open reply") - .expect("open session"); - cleanup - } - - async fn accept_open(&mut self, session_id: SessionId, mode: CodeModeOutputTruncation) { - let mut expected = json!({ "method": "session/open", "sessionId": session_id }); - if mode == CodeModeOutputTruncation::Server { - expected["outputTruncation"] = json!("server"); - } - assert_eq!(self.next_request().await, expected); - self.event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 1), - result: WireResult::Ok { - value: HostResponse::SessionReady { session_id }, - }, - })) - .await - .expect("open response"); - } - - async fn next_request(&mut self) -> serde_json::Value { - let frame = self.outgoing_rx.recv().await.expect("request"); - let mut message = - EncodedFrame::decode_framed::(&frame.into_framed_bytes()).unwrap(); - message["request"].take() - } - - async fn start_cell( - &mut self, - session: RemoteSession, - request_id: i64, - cell_id: &str, - delegate: Arc, - ) -> codex_code_mode_protocol::StartedCell { - let (response_tx, response_rx) = oneshot::channel(); - self.command_tx - .send(DriverCommand::Execute { - session, - delegate, - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: format!("call-{request_id}"), - enabled_tools: Vec::new(), - source: "await new Promise(() => {})".to_string(), - yield_time_ms: Some(1), - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx, - }) - .await - .expect("execute command"); - self.outgoing_rx.recv().await.expect("execute frame"); - self.event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(request_id), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: CellId::new(cell_id.to_string()).into(), - }, - }, - })) - .await - .expect("execute response"); - let delivered = response_rx - .await - .expect("execute reply") - .expect("execute session"); - self.execute_claim_tx - .send(delivered.request_id) - .expect("claim execute"); - delivered.started - } - - fn connection(&self, capabilities: CapabilitySet) -> Connection { - Connection { - command_tx: self.command_tx.clone(), - execute_claim_tx: self.execute_claim_tx.clone(), - alive: Arc::clone(&self.alive), - failure: Arc::clone(&self.failure), - cancellation: self.cancellation.clone(), - capabilities, - } - } - - async fn start_tool_delegate(&self, session: &RemoteSession, id: DelegateRequestId) { - self.event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id, - session_id: session.id.clone(), - request: DelegateRequest::InvokeTool { - invocation: WireNestedToolCall { - cell_id: CellId::new("1".to_string()).into(), - runtime_tool_call_id: "tool-1".to_string(), - tool_name: ToolName::plain("slow").into(), - tool_kind: codex_code_mode_protocol::CodeModeToolKind::Function.into(), - input: None, - }, - }, - })) - .await - .expect("delegate request"); - } -} - -impl Drop for DriverHarness { - fn drop(&mut self) { - self.cancellation.cancel(); - } -} - -#[tokio::test(start_paused = true)] -async fn old_host_does_not_receive_yield_frames() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let _cleanup = harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(RecordingDelegate::default()), - ) - .await; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::InitialResponse { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: WireRuntimeResponse::Yielded { - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - code_mode_host_duration_ns: 1, - }, - }, - })) - .await - .unwrap(); - let connection = harness.connection(CapabilitySet::empty()); - let signal = CancellationToken::new(); - signal.cancel(); - let wait = tokio::spawn(async move { - connection - .wait( - session, - WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 60_000, - max_output_tokens: None, - }, - Some(signal), - ) - .await - }); - let frame = harness.outgoing_rx.recv().await.unwrap(); - assert!( - matches!(EncodedFrame::decode_framed::(&frame.into_framed_bytes()).unwrap(), ClientToHost::Request { id, request: HostRequest::Wait { .. } } if id == RequestId::new(/*value*/ 3)) - ); - // Let any yield watcher run while the wait is still active. A response would - // drop the watcher and could otherwise hide an incorrectly sent yield frame. - assert!( - tokio::time::timeout(Duration::from_millis(1), harness.outgoing_rx.recv()) - .await - .is_err() - ); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded { - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - code_mode_host_duration_ns: 1, - }), - }, - }, - })) - .await - .unwrap(); - wait.await.unwrap().unwrap(); -} - -#[tokio::test] -async fn open_session_includes_nondefault_cell_execution_limits() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let limits = CodeModeSessionCellExecutionLimits { - max_yield_time_ms: Some(250), - max_heap_size_bytes: Some(16 * 1024 * 1024), - }; - let (response_tx, _response_rx) = oneshot::channel(); - - harness - .command_tx - .send(DriverCommand::OpenSession { - session: session.clone(), - options: CodeModeSessionOptions { - cell_execution_limits: limits, - ..CodeModeSessionOptions::default() - }, - cleanup: SessionCleanup::new(), - caller_cancellation: CancellationToken::new(), - response_tx, - }) - .await - .expect("limited session open command"); - let frame = harness - .outgoing_rx - .recv() - .await - .expect("limited session open frame"); - - assert_eq!( - EncodedFrame::decode_framed::(&frame.into_framed_bytes()) - .expect("decode limited session open request"), - ClientToHost::Request { - id: RequestId::new(/*value*/ 1), - request: HostRequest::OpenSession { - session_id: session.id, - cell_execution_limits: Some(WireSessionCellExecutionLimits { - max_yield_time_ms: Some(250), - max_heap_size_bytes: Some(16 * 1024 * 1024), - }), - output_truncation: None, - }, - } - ); -} - -#[tokio::test] -async fn stdio_logical_session_pins_output_mode_across_reconnect() { - use CodeModeOutputTruncation::Client; - use CodeModeOutputTruncation::Server; - - let legacy = CapabilitySet::empty(); - let bounded = - CapabilitySet::try_new([Capability::new(BOUNDED_OUTPUT_CAPABILITY).unwrap()]).unwrap(); - for (requested, hosts, expected_mode) in [ - (Client, [&bounded, &bounded].as_slice(), Client), - (Server, [&legacy, &bounded].as_slice(), Client), - (Server, [&bounded, &bounded, &legacy].as_slice(), Server), - ] { - let host = Arc::new(OwnedCodeModeHost::new("unused".into())); - let session = ProcessOwnedCodeModeSession::with_host( - Arc::clone(&host), - CodeModeSessionOptions { - output_truncation: requested, - ..CodeModeSessionOptions::default() - }, - ); - - for (index, &capabilities) in hosts.iter().enumerate() { - let mut harness = DriverHarness::start(); - *host.connection.lock().unwrap() = - Some(Arc::new(harness.connection(capabilities.clone()))); - if expected_mode == Server && capabilities == &legacy { - tokio::select! { - result = session.connection() => assert!(matches!(result, Err(error) if error.contains("cannot preserve"))), - _ = harness.outgoing_rx.recv() => panic!("opened a server session on an incompatible host"), - } - assert_eq!(session.output_truncation(), Server); - break; - } - - let session_id = SessionId::new(format!("session-{}", index + 1)).unwrap(); - tokio::try_join!(session.connection(), async { - harness.accept_open(session_id, expected_mode).await; - Ok(()) - }) - .unwrap(); - assert_eq!(session.output_truncation(), expected_mode); - - if capabilities == &legacy { - let cell_id = CellId::new("1".into()); - let wait = WaitRequest { - cell_id: cell_id.clone(), - yield_time_ms: 1, - max_output_tokens: Some(5), - }; - let terminate = TerminateRequest { - cell_id, - max_output_tokens: Some(5), - }; - let legacy_host = async { - for method in ["session/wait", "session/terminate"] { - let request = harness.next_request().await; - assert_eq!(request["method"], method); - // A null value would also be rejected by a legacy host. - assert!(request.get("maxOutputTokens").is_none()); - } - harness.cancellation.cancel(); - }; - let (wait, terminate, ()) = tokio::join!(biased; - session.wait(wait, /*preempt*/ None), session.terminate(terminate), legacy_host - ); - assert!(wait.is_err() && terminate.is_err()); - } - harness.cancellation.cancel(); - (&mut harness.driver_task).await.unwrap(); - } - } -} - -#[derive(Default)] -struct RecordingDelegate { - closed_cells: StdMutex>, - invocations: AtomicUsize, - notifications: AtomicUsize, -} - -struct PanickingDelegate; - -#[derive(Debug, Eq, PartialEq)] -enum HeldDelegateEvent { - Started, - Cancelled, - Finished, - CellClosed(CellId), -} - -struct HeldDelegate { - events_tx: mpsc::UnboundedSender, - release: CancellationToken, -} - -impl HeldDelegate { - fn new() -> ( - Arc, - mpsc::UnboundedReceiver, - CancellationToken, - ) { - let (events_tx, events_rx) = mpsc::unbounded_channel(); - let release = CancellationToken::new(); - ( - Arc::new(Self { - events_tx, - release: release.clone(), - }), - events_rx, - release, - ) - } -} - -impl CodeModeSessionDelegate for HeldDelegate { - fn invoke_tool<'a>( - &'a self, - _invocation: CodeModeNestedToolCall, - cancellation_token: CancellationToken, - ) -> ToolInvocationFuture<'a> { - let events_tx = self.events_tx.clone(); - let release = self.release.clone(); - Box::pin(async move { - let _ = events_tx.send(HeldDelegateEvent::Started); - cancellation_token.cancelled().await; - let _ = events_tx.send(HeldDelegateEvent::Cancelled); - release.cancelled().await; - let _ = events_tx.send(HeldDelegateEvent::Finished); - Err("cancelled".to_string()) - }) - } - - fn notify<'a>( - &'a self, - _call_id: String, - _cell_id: CellId, - _text: String, - _cancellation_token: CancellationToken, - ) -> NotificationFuture<'a> { - Box::pin(async { Ok(()) }) - } - - fn cell_closed(&self, cell_id: &CellId) { - let _ = self - .events_tx - .send(HeldDelegateEvent::CellClosed(cell_id.clone())); - } -} - -impl CodeModeSessionDelegate for PanickingDelegate { - fn invoke_tool<'a>( - &'a self, - _invocation: CodeModeNestedToolCall, - _cancellation_token: CancellationToken, - ) -> ToolInvocationFuture<'a> { - Box::pin(async { panic!("delegate panic probe") }) - } - - fn notify<'a>( - &'a self, - _call_id: String, - _cell_id: CellId, - _text: String, - _cancellation_token: CancellationToken, - ) -> NotificationFuture<'a> { - Box::pin(async { Ok(()) }) - } - - fn cell_closed(&self, _cell_id: &CellId) {} -} - -impl CodeModeSessionDelegate for RecordingDelegate { - fn invoke_tool<'a>( - &'a self, - _invocation: CodeModeNestedToolCall, - cancellation_token: CancellationToken, - ) -> ToolInvocationFuture<'a> { - self.invocations.fetch_add(1, Ordering::Relaxed); - Box::pin(async move { - cancellation_token.cancelled().await; - Err("cancelled".to_string()) - }) - } - - fn notify<'a>( - &'a self, - _call_id: String, - _cell_id: CellId, - _text: String, - _cancellation_token: CancellationToken, - ) -> NotificationFuture<'a> { - self.notifications.fetch_add(1, Ordering::Relaxed); - Box::pin(async { Ok(()) }) - } - - fn cell_closed(&self, cell_id: &CellId) { - self.closed_cells - .lock() - .expect("closed cells lock") - .push(cell_id.clone()); - } -} - -fn remote_session() -> RemoteSession { - RemoteSession { - id: SessionId::new("session-1").expect("session ID"), - generation: 1, - } -} - -async fn next_held_delegate_event( - events_rx: &mut mpsc::UnboundedReceiver, -) -> HeldDelegateEvent { - tokio::time::timeout(Duration::from_secs(1), events_rx.recv()) - .await - .expect("delegate event timeout") - .expect("delegate event stream") -} - -#[tokio::test] -async fn deferred_delegates_follow_cell_readiness_and_cancellation() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let first_delegate = Arc::new(RecordingDelegate::default()); - let second_delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - - let (first_response_tx, first_response_rx) = oneshot::channel(); - let (second_response_tx, second_response_rx) = oneshot::channel(); - for (tool_call_id, response_tx, delegate) in [ - ("first-cell", first_response_tx, first_delegate.clone()), - ("second-cell", second_response_tx, second_delegate.clone()), - ] { - harness - .command_tx - .send(DriverCommand::Execute { - delegate: delegate.clone(), - session: session.clone(), - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: tool_call_id.to_string(), - enabled_tools: Vec::new(), - source: "text('done')".to_string(), - yield_time_ms: Some(/*yield_time_ms*/ 1), - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx, - }) - .await - .expect("execute command"); - harness.outgoing_rx.recv().await.expect("execute frame"); - } - - let first_cell_id = CellId::new("first-cell".to_string()); - let second_cell_id = CellId::new("second-cell".to_string()); - let first_delegate_id = DelegateRequestId::new(/*value*/ 7); - let second_delegate_id = DelegateRequestId::new(/*value*/ 8); - let cancelled_delegate_id = DelegateRequestId::new(/*value*/ 9); - for (delegate_id, cell_id) in [ - (first_delegate_id, first_cell_id.clone()), - (second_delegate_id, second_cell_id.clone()), - (cancelled_delegate_id, first_cell_id.clone()), - ] { - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: delegate_id, - session_id: session.id.clone(), - request: DelegateRequest::Notify { - call_id: format!("notify-{}", cell_id.as_str()), - cell_id: (&cell_id).into(), - text: "hello".to_string(), - }, - })) - .await - .expect("early delegate request"); - } - - harness - .event_tx - .send(DriverEvent::HostMessage( - HostToClient::CancelDelegateRequest { - id: cancelled_delegate_id, - }, - )) - .await - .expect("deferred delegate cancellation"); - - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: (&second_cell_id).into(), - }, - }, - })) - .await - .expect("second execution-started response"); - let _second_started = second_response_rx - .await - .expect("second execute response") - .expect("second started cell"); - let second_response = tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("second delegate response timeout") - .expect("second delegate response frame"); - assert_eq!( - EncodedFrame::decode_framed::(&second_response.into_framed_bytes()) - .expect("decode second delegate response"), - ClientToHost::DelegateResponse { - id: second_delegate_id, - result: WireResult::Ok { - value: codex_code_mode_protocol::host::DelegateResponse::NotificationDelivered, - }, - } - ); - assert_eq!( - ( - first_delegate.notifications.load(Ordering::Relaxed), - second_delegate.notifications.load(Ordering::Relaxed) - ), - (0, 1) - ); - - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: (&first_cell_id).into(), - }, - }, - })) - .await - .expect("first execution-started response"); - let _first_started = first_response_rx - .await - .expect("first execute response") - .expect("first started cell"); - let first_response = tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("first delegate response timeout") - .expect("first delegate response frame"); - assert_eq!( - EncodedFrame::decode_framed::(&first_response.into_framed_bytes()) - .expect("decode first delegate response"), - ClientToHost::DelegateResponse { - id: first_delegate_id, - result: WireResult::Ok { - value: codex_code_mode_protocol::host::DelegateResponse::NotificationDelivered, - }, - } - ); - assert_eq!( - ( - first_delegate.notifications.load(Ordering::Relaxed), - second_delegate.notifications.load(Ordering::Relaxed) - ), - (1, 1) - ); - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - assert!(harness.alive.load(Ordering::Relaxed)); -} - -#[tokio::test] -async fn dropped_open_waiter_shuts_down_committed_session() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let (open_tx, open_rx) = oneshot::channel(); - let cleanup = SessionCleanup::new(); - harness - .command_tx - .send(DriverCommand::OpenSession { - session: session.clone(), - options: CodeModeSessionOptions::default(), - cleanup, - caller_cancellation: CancellationToken::new(), - response_tx: open_tx, - }) - .await - .expect("open command"); - drop(open_rx); - harness.outgoing_rx.recv().await.expect("open frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 1), - result: WireResult::Ok { - value: HostResponse::SessionReady { - session_id: session.id.clone(), - }, - }, - })) - .await - .expect("open response"); - harness - .outgoing_rx - .recv() - .await - .expect("abandoned session shutdown frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::SessionClosed { - session_id: session.id.clone(), - }, - }, - })) - .await - .expect("shutdown response"); - - let (execute_tx, execute_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Execute { - delegate: Arc::new(RecordingDelegate::default()), - session: session.clone(), - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: "text('ok')".to_string(), - yield_time_ms: None, - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx: execute_tx, - }) - .await - .expect("execute command"); - assert_eq!( - execute_rx - .await - .expect("execute reply") - .err() - .expect("closed session should reject execute"), - "unknown code-mode session session-1" - ); -} - -#[tokio::test] -async fn delegate_cancel_is_best_effort_and_sends_no_late_response() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let request_id = DelegateRequestId::new(/*value*/ 7); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: request_id, - session_id: session.id.clone(), - request: DelegateRequest::InvokeTool { - invocation: WireNestedToolCall { - cell_id: CellId::new("1".to_string()).into(), - runtime_tool_call_id: "tool-1".to_string(), - tool_name: ToolName::plain("slow").into(), - tool_kind: codex_code_mode_protocol::CodeModeToolKind::Function.into(), - input: None, - }, - }, - })) - .await - .expect("delegate request"); - harness - .event_tx - .send(DriverEvent::HostMessage( - HostToClient::CancelDelegateRequest { id: request_id }, - )) - .await - .expect("delegate cancel"); - tokio::task::yield_now().await; - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: request_id, - session_id: session.id, - request: DelegateRequest::Notify { - call_id: "notify-reused".to_string(), - cell_id: CellId::new("1".to_string()).into(), - text: "duplicate".to_string(), - }, - })) - .await - .expect("reused delegate request"); - tokio::task::yield_now().await; - - assert!(!harness.alive.load(Ordering::Acquire)); - assert_eq!(delegate.invocations.load(Ordering::Relaxed), 1); - assert_eq!(delegate.notifications.load(Ordering::Relaxed), 0); -} - -#[tokio::test] -async fn delegate_limit_returns_an_error_without_disconnecting() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - - for value in 1..=MAX_PENDING_DELEGATE_CALLS { - harness - .start_tool_delegate(&session, DelegateRequestId::new(value as i64)) - .await; - } - - let overflow_id = DelegateRequestId::new(MAX_PENDING_DELEGATE_CALLS as i64 + 1); - harness.start_tool_delegate(&session, overflow_id).await; - let response = tokio::time::timeout(Duration::from_secs(5), harness.outgoing_rx.recv()) - .await - .expect("delegate overflow response timeout") - .expect("delegate overflow response frame"); - - assert_eq!( - EncodedFrame::decode_framed::(&response.into_framed_bytes()) - .expect("decode delegate overflow response"), - ClientToHost::DelegateResponse { - id: overflow_id, - result: WireResult::Err { - message: format!( - "code-mode host exceeded the limit of {MAX_PENDING_DELEGATE_CALLS} pending delegate calls" - ), - }, - } - ); - assert!(harness.alive.load(Ordering::Acquire)); - - harness - .event_tx - .send(DriverEvent::HostMessage( - HostToClient::CancelDelegateRequest { - id: DelegateRequestId::new(/*value*/ 1), - }, - )) - .await - .expect("cancel pending delegate"); - harness - .start_tool_delegate( - &session, - DelegateRequestId::new(MAX_PENDING_DELEGATE_CALLS as i64 + 2), - ) - .await; - - tokio::time::timeout(Duration::from_secs(5), async { - while delegate.invocations.load(Ordering::Relaxed) <= MAX_PENDING_DELEGATE_CALLS { - tokio::task::yield_now().await; - } - }) - .await - .expect("delegate capacity should be available after cancellation"); - assert_eq!( - delegate.invocations.load(Ordering::Relaxed), - MAX_PENDING_DELEGATE_CALLS + 1 - ); - assert!(harness.alive.load(Ordering::Acquire)); -} - -#[tokio::test] -async fn terminate_closes_cell_without_waiting_for_delegate_cleanup() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let (delegate, mut events_rx, release) = HeldDelegate::new(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let delegate_id = DelegateRequestId::new(/*value*/ 7); - harness.start_tool_delegate(&session, delegate_id).await; - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Started - ); - - let (response_tx, response_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Terminate { - session: session.clone(), - request: CellId::new("1".to_string()).into(), - response_tx, - }) - .await - .expect("terminate command"); - harness.outgoing_rx.recv().await.expect("terminate frame"); - harness - .event_tx - .send(DriverEvent::HostMessage( - HostToClient::CancelDelegateRequest { id: delegate_id }, - )) - .await - .expect("delegate cancel"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id, - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("terminate response"); - - let closure_events = [ - next_held_delegate_event(&mut events_rx).await, - next_held_delegate_event(&mut events_rx).await, - ]; - assert!(closure_events.contains(&HeldDelegateEvent::Cancelled)); - assert!(closure_events.contains(&HeldDelegateEvent::CellClosed(CellId::new("1".to_string())))); - assert_eq!( - response_rx.await.expect("terminate reply"), - Ok(codex_code_mode_protocol::WaitOutcome::LiveCell( - codex_code_mode_protocol::RuntimeResponse::Terminated { - code_mode_host_duration: Some(Duration::ZERO), - cell_id: CellId::new("1".to_string()), - content_items: Vec::new(), - } - )) - ); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty | mpsc::error::TryRecvError::Disconnected) - )); - - release.cancel(); - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Finished - ); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty | mpsc::error::TryRecvError::Disconnected) - )); - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - assert!(harness.alive.load(Ordering::Acquire)); -} - -#[tokio::test] -async fn shutdown_closes_cell_without_waiting_for_delegate_cleanup() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let (delegate, mut events_rx, release) = HeldDelegate::new(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let delegate_id = DelegateRequestId::new(/*value*/ 7); - harness.start_tool_delegate(&session, delegate_id).await; - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Started - ); - - let (response_tx, response_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::ShutdownSession { - session: session.clone(), - response_tx, - }) - .await - .expect("shutdown command"); - harness.outgoing_rx.recv().await.expect("shutdown frame"); - harness - .event_tx - .send(DriverEvent::HostMessage( - HostToClient::CancelDelegateRequest { id: delegate_id }, - )) - .await - .expect("delegate cancel"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id.clone(), - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::SessionClosed { - session_id: session.id, - }, - }, - })) - .await - .expect("shutdown response"); - - let closure_events = [ - next_held_delegate_event(&mut events_rx).await, - next_held_delegate_event(&mut events_rx).await, - ]; - assert!(closure_events.contains(&HeldDelegateEvent::Cancelled)); - assert!(closure_events.contains(&HeldDelegateEvent::CellClosed(CellId::new("1".to_string())))); - assert_eq!(response_rx.await.expect("shutdown reply"), Ok(())); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - - release.cancel(); - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Finished - ); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty | mpsc::error::TryRecvError::Disconnected) - )); - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - assert!(harness.alive.load(Ordering::Acquire)); -} - -#[tokio::test] -async fn completed_delegate_request_id_cannot_be_reused() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let request_id = DelegateRequestId::new(/*value*/ 7); - let request = || DelegateRequest::Notify { - call_id: "notify-1".to_string(), - cell_id: CellId::new("1".to_string()).into(), - text: "once".to_string(), - }; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: request_id, - session_id: session.id.clone(), - request: request(), - })) - .await - .expect("delegate request"); - harness - .outgoing_rx - .recv() - .await - .expect("delegate response frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: request_id, - session_id: session.id, - request: request(), - })) - .await - .expect("reused delegate request"); - tokio::task::yield_now().await; - - assert!(!harness.alive.load(Ordering::Acquire)); - assert_eq!(delegate.notifications.load(Ordering::Relaxed), 1); -} - -#[tokio::test] -async fn delegate_task_panic_becomes_tool_error_without_killing_connection() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(PanickingDelegate), - ) - .await; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id: DelegateRequestId::new(/*value*/ 7), - session_id: session.id.clone(), - request: DelegateRequest::InvokeTool { - invocation: WireNestedToolCall { - cell_id: CellId::new("1".to_string()).into(), - runtime_tool_call_id: "tool-1".to_string(), - tool_name: ToolName::plain("panic").into(), - tool_kind: codex_code_mode_protocol::CodeModeToolKind::Function.into(), - input: None, - }, - }, - })) - .await - .expect("delegate request"); - tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("delegate response timeout") - .expect("delegate response frame"); - - assert!(harness.alive.load(Ordering::Acquire)); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id, - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); -} - -#[tokio::test] -async fn delegate_for_unknown_cell_returns_error_without_invocation() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "known", - delegate.clone(), - ) - .await; - - let id = DelegateRequestId::new(/*value*/ 7); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id, - session_id: session.id, - request: DelegateRequest::InvokeTool { - invocation: WireNestedToolCall { - cell_id: CellId::new("missing".to_string()).into(), - runtime_tool_call_id: "tool-1".to_string(), - tool_name: ToolName::plain("slow").into(), - tool_kind: codex_code_mode_protocol::CodeModeToolKind::Function.into(), - input: None, - }, - }, - })) - .await - .expect("delegate request"); - let response = tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("delegate response timeout") - .expect("delegate response frame"); - - assert_eq!( - EncodedFrame::decode_framed::(&response.into_framed_bytes()) - .expect("decode delegate response"), - ClientToHost::DelegateResponse { - id, - result: WireResult::Err { - message: "code-mode host delegated for unknown cell missing in session session-1" - .to_string(), - }, - } - ); - assert!(harness.alive.load(Ordering::Acquire)); - assert_eq!(delegate.invocations.load(Ordering::Relaxed), 0); -} - -#[tokio::test] -async fn delegate_after_cell_close_returns_error_without_invocation() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id.clone(), - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); - let id = DelegateRequestId::new(/*value*/ 7); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::DelegateRequest { - id, - session_id: session.id, - request: DelegateRequest::Notify { - call_id: "notify-1".to_string(), - cell_id: CellId::new("1".to_string()).into(), - text: "late".to_string(), - }, - })) - .await - .expect("delegate request"); - let response = tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("delegate response timeout") - .expect("delegate response frame"); - - assert_eq!( - EncodedFrame::decode_framed::(&response.into_framed_bytes()) - .expect("decode delegate response"), - ClientToHost::DelegateResponse { - id, - result: WireResult::Err { - message: "code-mode host delegated for unknown cell 1 in session session-1" - .to_string(), - }, - } - ); - assert!(harness.alive.load(Ordering::Acquire)); - assert_eq!(delegate.notifications.load(Ordering::Relaxed), 0); -} - -#[tokio::test] -async fn mismatched_initial_response_fails_connection_and_closes_cell_once() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let started = harness - .start_cell(session, /*request_id*/ 2, "1", delegate.clone()) - .await; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::InitialResponse { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("2".to_string()).into(), - content_items: Vec::new(), - }, - }, - })) - .await - .expect("initial response"); - - assert!(started.initial_response().await.is_err()); - assert!(!harness.alive.load(Ordering::Acquire)); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn mismatched_wait_response_fails_connection() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let (response_tx, response_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Wait { - session, - request: WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 1, - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx, - }) - .await - .expect("wait command"); - harness.outgoing_rx.recv().await.expect("wait frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("2".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("wait response"); - - assert!(response_rx.await.expect("wait reply").is_err()); - assert!(!harness.alive.load(Ordering::Acquire)); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn mismatched_terminate_response_fails_connection() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let (response_tx, response_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Terminate { - session, - request: CellId::new("1".to_string()).into(), - response_tx, - }) - .await - .expect("terminate command"); - harness.outgoing_rx.recv().await.expect("terminate frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::MissingCell(WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("2".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("terminate response"); - - assert!(response_rx.await.expect("terminate reply").is_err()); - assert!(!harness.alive.load(Ordering::Acquire)); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn remote_wait_accepts_durations_longer_than_five_minutes() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(RecordingDelegate::default()), - ) - .await; - let (response_tx, response_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Wait { - session, - request: WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 300_001, - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx, - }) - .await - .expect("wait command"); - tokio::time::timeout(Duration::from_secs(1), harness.outgoing_rx.recv()) - .await - .expect("wait frame timeout") - .expect("wait frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("wait response"); - - assert_eq!( - response_rx.await.expect("wait reply"), - Ok(codex_code_mode_protocol::WaitOutcome::LiveCell( - codex_code_mode_protocol::RuntimeResponse::Yielded { - code_mode_host_duration: Some(Duration::ZERO), - cell_id: CellId::new("1".to_string()), - content_items: Vec::new(), - } - )) - ); -} - -/// A stalled remote wait must expire even when its request never reaches the host. -#[tokio::test(start_paused = true)] -async fn queued_remote_wait_times_out_and_invalidates_the_connection() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(RecordingDelegate::default()), - ) - .await; - let connection = Connection { - command_tx: harness.command_tx.clone(), - execute_claim_tx: harness.execute_claim_tx.clone(), - alive: Arc::clone(&harness.alive), - failure: Arc::clone(&harness.failure), - cancellation: harness.cancellation.clone(), - capabilities: CapabilitySet::empty(), - }; - let response = tokio::spawn(async move { - let result = connection - .wait( - session, - WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 1, - max_output_tokens: None, - }, - /*yield_signal*/ None, - ) - .await; - (connection, result) - }); - while harness.outgoing_rx.is_empty() { - tokio::task::yield_now().await; - } - tokio::time::advance(DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT + Duration::from_secs(2)).await; - - let (connection, result) = response.await.expect("wait task"); - assert_eq!( - result, - Err("code-mode host timed out waiting for wait response".to_string()) - ); - assert!(!harness.alive.load(Ordering::Acquire)); - assert!(harness.cancellation.is_cancelled()); - drop(connection); -} - -/// A stalled termination must expire and invalidate the same connection as a stalled wait. -#[tokio::test(start_paused = true)] -async fn queued_remote_termination_times_out_and_invalidates_the_connection() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(RecordingDelegate::default()), - ) - .await; - let connection = Connection { - command_tx: harness.command_tx.clone(), - execute_claim_tx: harness.execute_claim_tx.clone(), - alive: Arc::clone(&harness.alive), - failure: Arc::clone(&harness.failure), - cancellation: harness.cancellation.clone(), - capabilities: CapabilitySet::empty(), - }; - let response = tokio::spawn(async move { - let result = connection - .terminate(session, CellId::new("1".to_string()).into()) - .await; - (connection, result) - }); - while harness.outgoing_rx.is_empty() { - tokio::task::yield_now().await; - } - tokio::time::advance(DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT + Duration::from_secs(1)).await; - - let (connection, result) = response.await.expect("termination task"); - assert_eq!( - result, - Err("code-mode host timed out waiting for terminate response".to_string()) - ); - assert!(!harness.alive.load(Ordering::Acquire)); - assert!(harness.cancellation.is_cancelled()); - drop(connection); -} - -#[tokio::test] -async fn cancelled_wait_is_retired_before_next_wait_is_sent() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - Arc::new(RecordingDelegate::default()), - ) - .await; - let first_cancellation = CancellationToken::new(); - let (first_tx, first_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Wait { - session: session.clone(), - request: WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 60_000, - max_output_tokens: Some(19), - }, - caller_cancellation: first_cancellation.clone(), - yield_signal: None, - response_tx: first_tx, - }) - .await - .expect("first wait command"); - harness.outgoing_rx.recv().await.expect("first wait frame"); - first_cancellation.cancel(); - drop(first_rx); - - let (second_tx, second_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Wait { - session, - request: WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 1, - max_output_tokens: Some(37), - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx: second_tx, - }) - .await - .expect("second wait command"); - harness - .outgoing_rx - .recv() - .await - .expect("cancel request frame"); - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Err { - message: "code-mode request cancelled".to_string(), - }, - })) - .await - .expect("cancelled wait response"); - assert_eq!(harness.next_request().await["maxOutputTokens"], 37); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 4), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("second wait response"); - - assert_eq!( - second_rx.await.expect("second wait reply"), - Ok(codex_code_mode_protocol::WaitOutcome::LiveCell( - codex_code_mode_protocol::RuntimeResponse::Yielded { - code_mode_host_duration: Some(Duration::ZERO), - cell_id: CellId::new("1".to_string()), - content_items: Vec::new(), - } - )) - ); -} - -#[tokio::test] -async fn abandoned_execute_is_tracked_and_terminated_after_admission() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let cancellation = CancellationToken::new(); - let (execute_tx, execute_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Execute { - delegate: delegate.clone(), - session: session.clone(), - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: "await new Promise(() => {})".to_string(), - yield_time_ms: Some(1), - max_output_tokens: None, - }, - caller_cancellation: cancellation.clone(), - yield_signal: None, - response_tx: execute_tx, - }) - .await - .expect("execute command"); - harness.outgoing_rx.recv().await.expect("execute frame"); - cancellation.cancel(); - drop(execute_rx); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: CellId::new("1".to_string()).into(), - }, - }, - })) - .await - .expect("execute response"); - - harness - .outgoing_rx - .recv() - .await - .expect("execute cancellation frame"); - harness - .outgoing_rx - .recv() - .await - .expect("abandoned cell termination frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::InitialResponse { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }, - }, - })) - .await - .expect("initial response"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("terminate response"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id, - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); - tokio::task::yield_now().await; - - assert!(harness.alive.load(Ordering::Acquire)); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn delivered_but_unclaimed_execute_is_terminated_when_the_caller_is_cancelled() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let cancellation = CancellationToken::new(); - let (execute_tx, execute_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Execute { - delegate: delegate.clone(), - session: session.clone(), - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: "await new Promise(() => {})".to_string(), - yield_time_ms: Some(1), - max_output_tokens: None, - }, - caller_cancellation: cancellation.clone(), - yield_signal: None, - response_tx: execute_tx, - }) - .await - .expect("execute command"); - harness.outgoing_rx.recv().await.expect("execute frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: CellId::new("1".to_string()).into(), - }, - }, - })) - .await - .expect("execute response"); - let delivered = execute_rx - .await - .expect("execute reply") - .expect("delivered execute"); - assert_eq!(delivered.request_id, RequestId::new(/*value*/ 2)); - cancellation.cancel(); - - harness - .outgoing_rx - .recv() - .await - .expect("execute cancellation frame"); - harness - .outgoing_rx - .recv() - .await - .expect("unclaimed cell termination frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::InitialResponse { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }, - }, - })) - .await - .expect("initial response"); - assert!(delivered.started.initial_response().await.is_ok()); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 3), - result: WireResult::Ok { - value: HostResponse::WaitCompleted { - outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Terminated { - code_mode_host_duration_ns: 0, - cell_id: CellId::new("1".to_string()).into(), - content_items: Vec::new(), - }), - }, - }, - })) - .await - .expect("terminate response"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id, - cell_id: CellId::new("1".to_string()).into(), - })) - .await - .expect("cell close"); - tokio::time::timeout(Duration::from_secs(2), async { - while Arc::strong_count(&delegate) > 1 { - tokio::task::yield_now().await; - } - }) - .await - .expect("abandoned execution should release its delegate"); - - assert!(harness.alive.load(Ordering::Acquire)); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn session_accepts_more_than_4096_cells_without_growing_a_tombstone_set() { - const CELL_COUNT: usize = 4097; - - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - - for sequence in 1..=CELL_COUNT { - let request_id = i64::try_from(sequence).expect("cell sequence fits in i64") + 1; - let cell_id = sequence.to_string(); - let started = harness - .start_cell(session.clone(), request_id, &cell_id, delegate.clone()) - .await; - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::InitialResponse { - id: RequestId::new(request_id), - result: WireResult::Ok { - value: WireRuntimeResponse::Yielded { - code_mode_host_duration_ns: 0, - cell_id: CellId::new(cell_id.clone()).into(), - content_items: Vec::new(), - }, - }, - })) - .await - .expect("initial response"); - assert!(started.initial_response().await.is_ok()); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::CellClosed { - session_id: session.id.clone(), - cell_id: CellId::new(cell_id).into(), - })) - .await - .expect("cell close"); - } - - tokio::time::timeout(Duration::from_secs(1), async { - while delegate - .closed_cells - .lock() - .expect("closed cells lock") - .len() - != CELL_COUNT - { - tokio::task::yield_now().await; - } - }) - .await - .expect("cell close callbacks timeout"); - assert!(harness.alive.load(Ordering::Acquire)); -} - -#[tokio::test] -async fn connection_failure_closes_every_live_cell_once() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - let cleanup = harness.open(session.clone()).await; - let (execute_tx, execute_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Execute { - delegate: delegate.clone(), - session, - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: "await new Promise(() => {})".to_string(), - yield_time_ms: Some(1), - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx: execute_tx, - }) - .await - .expect("execute command"); - harness.outgoing_rx.recv().await.expect("execute frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::ExecutionStarted { - cell_id: CellId::new("1".to_string()).into(), - }, - }, - })) - .await - .expect("execute response"); - let _started = execute_rx - .await - .expect("execute reply") - .expect("execute session"); - harness - .event_tx - .send(DriverEvent::Failed("host crashed".to_string())) - .await - .expect("failure event"); - tokio::time::timeout(Duration::from_secs(1), cleanup.wait()) - .await - .expect("session cleanup timeout"); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn session_cleanup_does_not_wait_for_delegate_completion() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let (delegate, mut events_rx, release) = HeldDelegate::new(); - let cleanup = harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - harness - .start_tool_delegate(&session, DelegateRequestId::new(/*value*/ 7)) - .await; - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Started - ); - - harness - .event_tx - .send(DriverEvent::Failed("host crashed".to_string())) - .await - .expect("failure event"); - let closure_events = [ - next_held_delegate_event(&mut events_rx).await, - next_held_delegate_event(&mut events_rx).await, - ]; - assert!(closure_events.contains(&HeldDelegateEvent::Cancelled)); - assert!(closure_events.contains(&HeldDelegateEvent::CellClosed(CellId::new("1".to_string())))); - tokio::time::timeout(Duration::from_secs(1), cleanup.wait()) - .await - .expect("session cleanup timeout"); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); - - release.cancel(); - assert_eq!( - next_held_delegate_event(&mut events_rx).await, - HeldDelegateEvent::Finished - ); - assert!(matches!( - events_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty | mpsc::error::TryRecvError::Disconnected) - )); -} - -#[tokio::test] -async fn aborting_driver_marks_connection_dead_and_closes_cells() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - let delegate = Arc::new(RecordingDelegate::default()); - harness.open(session.clone()).await; - let _started = harness - .start_cell( - session.clone(), - /*request_id*/ 2, - "1", - delegate.clone(), - ) - .await; - let (wait_tx, wait_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Wait { - session, - request: WaitRequest { - cell_id: CellId::new("1".to_string()), - yield_time_ms: 60_000, - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx: wait_tx, - }) - .await - .expect("wait command"); - harness.outgoing_rx.recv().await.expect("wait frame"); - - harness.driver_task.abort(); - for _ in 0..10 { - if !harness.alive.load(Ordering::Acquire) { - break; - } - tokio::task::yield_now().await; - } - - assert!(!harness.alive.load(Ordering::Acquire)); - assert!(harness.cancellation.is_cancelled()); - assert!(wait_rx.await.expect("wait failure").is_err()); - assert_eq!( - *delegate.closed_cells.lock().expect("closed cells lock"), - vec![CellId::new("1".to_string())] - ); -} - -#[tokio::test] -async fn dropped_shutdown_waiter_does_not_abort_remote_cleanup() { - let mut harness = DriverHarness::start(); - let session = remote_session(); - harness.open(session.clone()).await; - let (shutdown_tx, shutdown_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::ShutdownSession { - session: session.clone(), - response_tx: shutdown_tx, - }) - .await - .expect("shutdown command"); - drop(shutdown_rx); - harness.outgoing_rx.recv().await.expect("shutdown frame"); - harness - .event_tx - .send(DriverEvent::HostMessage(HostToClient::Response { - id: RequestId::new(/*value*/ 2), - result: WireResult::Ok { - value: HostResponse::SessionClosed { - session_id: session.id.clone(), - }, - }, - })) - .await - .expect("shutdown response"); - - let (execute_tx, execute_rx) = oneshot::channel(); - harness - .command_tx - .send(DriverCommand::Execute { - delegate: Arc::new(RecordingDelegate::default()), - session, - request: WireExecuteRequest { - catalog_revision: None, - tool_call_id: "call-2".to_string(), - enabled_tools: Vec::new(), - source: "text('unreachable')".to_string(), - yield_time_ms: None, - max_output_tokens: None, - }, - caller_cancellation: CancellationToken::new(), - yield_signal: None, - response_tx: execute_tx, - }) - .await - .expect("execute command"); - assert_eq!( - execute_rx - .await - .expect("execute reply") - .err() - .expect("closed session should reject execute"), - "unknown code-mode session session-1" - ); - assert!(matches!( - harness.outgoing_rx.try_recv(), - Err(mpsc::error::TryRecvError::Empty) - )); -} diff --git a/codex-rs/code-mode/src/remote_session/connection/reader.rs b/codex-rs/code-mode/src/remote_session/connection/reader.rs deleted file mode 100644 index cfef435d78..0000000000 --- a/codex-rs/code-mode/src/remote_session/connection/reader.rs +++ /dev/null @@ -1,28 +0,0 @@ -use codex_code_mode_protocol::host::FramedReader; -use tokio::process::ChildStdout; -use tokio::sync::mpsc; -use tokio_util::sync::CancellationToken; - -use super::driver::DriverEvent; - -pub(super) async fn drive_reader( - mut reader: FramedReader, - events: mpsc::Sender, - cancellation: CancellationToken, -) -> Result<(), String> { - loop { - let message = tokio::select! { - _ = cancellation.cancelled() => return Ok(()), - result = reader.read() => result, - }; - let message = match message { - Ok(Some(message)) => message, - Ok(None) => return Err("code-mode host closed its stdout".to_string()), - Err(err) => return Err(format!("failed to read code-mode host message: {err}")), - }; - events - .send(DriverEvent::HostMessage(message)) - .await - .map_err(|_| "code-mode connection driver closed".to_string())?; - } -} diff --git a/codex-rs/code-mode/src/remote_session/delegate.rs b/codex-rs/code-mode/src/remote_session/delegate.rs deleted file mode 100644 index b66d7422da..0000000000 --- a/codex-rs/code-mode/src/remote_session/delegate.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! Restores per-execution trace context for callbacks from the shared stdio host. -//! -//! Keep only a W3C snapshot: retaining the caller's tracing span would extend a yielded -//! exec's duration until its background cell closes. The gRPC transport has its own -//! callback propagation and does not use this wrapper. -//! Instrument manually because `#[instrument]` enters the span before its body runs, -//! which is too late to assign the remote OpenTelemetry parent. - -use std::sync::Arc; - -use codex_code_mode_protocol::CellId; -use codex_code_mode_protocol::CodeModeNestedToolCall; -use codex_code_mode_protocol::CodeModeSessionDelegate; -use codex_code_mode_protocol::NotificationFuture; -use codex_code_mode_protocol::ToolInvocationFuture; -use codex_protocol::protocol::W3cTraceContext; -use tokio_util::sync::CancellationToken; -use tracing::Instrument; - -pub(super) struct TracedCellDelegate { - pub(super) delegate: Arc, - pub(super) trace: Option, -} - -impl TracedCellDelegate { - fn with_parent(&self, span: tracing::Span) -> tracing::Span { - // Set the remote parent before entering the span: OTEL starts it on entry. - if let Some(trace) = &self.trace { - codex_otel::set_parent_from_w3c_trace_context(&span, trace); - } - span - } -} - -impl CodeModeSessionDelegate for TracedCellDelegate { - fn invoke_tool<'a>( - &'a self, - invocation: CodeModeNestedToolCall, - cancellation_token: CancellationToken, - ) -> ToolInvocationFuture<'a> { - let span = self.with_parent(tracing::info_span!(parent: None, "code_mode.stdio.callback")); - Box::pin( - async move { - self.delegate - .invoke_tool(invocation, cancellation_token) - .await - } - .instrument(span), - ) - } - - fn notify<'a>( - &'a self, - call_id: String, - cell_id: CellId, - text: String, - cancellation_token: CancellationToken, - ) -> NotificationFuture<'a> { - let span = self.with_parent(tracing::info_span!(parent: None, "code_mode.stdio.notify")); - Box::pin( - async move { - self.delegate - .notify(call_id, cell_id, text, cancellation_token) - .await - } - .instrument(span), - ) - } - - fn cell_closed(&self, cell_id: &CellId) { - self.delegate.cell_closed(cell_id); - } -} diff --git a/codex-rs/code-mode/src/remote_session_tests.rs b/codex-rs/code-mode/src/remote_session_tests.rs deleted file mode 100644 index 4336cec795..0000000000 --- a/codex-rs/code-mode/src/remote_session_tests.rs +++ /dev/null @@ -1,95 +0,0 @@ -use std::io; -use std::path::PathBuf; -use std::sync::Arc; - -use codex_code_mode_protocol::CodeModeSessionProvider; -use pretty_assertions::assert_eq; - -use super::ProcessOwnedCodeModeSession; -use super::ProcessOwnedCodeModeSessionProvider; -use super::connection::ConnectionError; -use crate::NoopCodeModeSessionDelegate; - -#[test] -fn provider_reuses_its_live_process_host() { - let provider = ProcessOwnedCodeModeSessionProvider::default(); - - let first = provider.process_host(); - let second = provider.process_host(); - - assert!(Arc::ptr_eq(&first, &second)); -} - -#[test] -fn missing_host_error_limits_the_displayed_path_to_512_bytes() { - let executable = "codex-code-mode-host-does-not-exist"; - let host_program = format!("{}{executable}", "missing-directory/".repeat(/*n*/ 64)); - let expected_suffix = &host_program[host_program.len() - (512 - "...".len())..]; - let error = ConnectionError::Spawn { - host_program: PathBuf::from(&host_program), - error: io::Error::new(io::ErrorKind::NotFound, "host unavailable"), - }; - - assert_eq!( - error.to_string(), - format!("failed to spawn code-mode host ...{expected_suffix}: host unavailable") - ); -} - -#[test] -fn missing_host_error_preserves_utf8_boundaries_when_truncating_the_path() { - let executable = "codex-code-mode-host-does-not-exist"; - let host_program = format!("{}{executable}", "🦀".repeat(/*n*/ 256)); - let error = ConnectionError::Spawn { - host_program: PathBuf::from(host_program), - error: io::Error::new(io::ErrorKind::NotFound, "host unavailable"), - } - .to_string(); - let displayed_path = error - .strip_prefix("failed to spawn code-mode host ") - .and_then(|message| message.strip_suffix(": host unavailable")) - .expect("missing-host error should contain the displayed host path"); - - assert!(displayed_path.starts_with("...")); - assert!(displayed_path.ends_with(executable)); - assert!(displayed_path.len() <= 512); -} - -#[tokio::test] -async fn provider_returns_missing_host_error() { - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( - "codex-code-mode-host-does-not-exist".into(), - ); - - let error = provider - .create_session() - .await - .err() - .expect("missing host should fail"); - - assert!(error.contains("failed to spawn code-mode host codex-code-mode-host-does-not-exist")); -} - -#[tokio::test] -async fn shutdown_before_open_does_not_spawn_the_host() { - let session = ProcessOwnedCodeModeSession::new(); - - session.shutdown().await.expect("shutdown session"); - let error = session - .execute( - codex_code_mode_protocol::ExecuteRequest { - tool_call_id: "call-1".to_string(), - enabled_tools: Vec::new(), - source: "text('unreachable')".to_string(), - yield_time_ms: None, - max_output_tokens: None, - }, - Arc::new(NoopCodeModeSessionDelegate), - /*preempt*/ None, - ) - .await - .err() - .expect("shutdown session should reject execution"); - - assert_eq!(error, "code mode session is shutting down"); -} diff --git a/codex-rs/core/src/code_mode_host.rs b/codex-rs/core/src/code_mode_host.rs index deca0e6b9c..f5ca0ea519 100644 --- a/codex-rs/core/src/code_mode_host.rs +++ b/codex-rs/core/src/code_mode_host.rs @@ -25,7 +25,6 @@ use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::CodeModeSessionProviderFuture; use codex_code_mode::CodeModeSessionResultFuture; use codex_code_mode::GrpcCodeModeSessionProvider; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_protocol::shell_environment::scrub_non_inheritable_env_vars; use codex_utils_process::background_command; use hyper_util::rt::TokioIo; @@ -45,6 +44,8 @@ use tonic::transport::Endpoint; use tower::ServiceExt; use tower::service_fn; +const MAX_DISPLAYED_HOST_PROGRAM_BYTES: usize = 512; + /// Only wraps Core's built-in providers; injected providers own their session settings. pub(crate) struct LocalServerOutputProvider { pub(crate) inner: Arc, @@ -109,8 +110,21 @@ impl ProcessOwnedGrpcCodeModeSessionProvider { impl CodeModeSessionProvider for ProcessOwnedGrpcCodeModeSessionProvider { fn availability(&self) -> Result<(), String> { - ProcessOwnedCodeModeSessionProvider::with_host_program(self.host_program.clone()) - .availability() + if self.host_program.is_file() { + return Ok(()); + } + // Spawn errors reach tool output. Keep the executable-bearing suffix of long paths. + let path = self.host_program.to_string_lossy(); + let path = if path.len() > MAX_DISPLAYED_HOST_PROGRAM_BYTES { + let suffix_bytes = MAX_DISPLAYED_HOST_PROGRAM_BYTES - "...".len(); + let start = path.ceil_char_boundary(path.len() - suffix_bytes); + format!("...{}", &path[start..]) + } else { + path.into_owned() + }; + Err(format!( + "failed to spawn code-mode host {path}: host executable was not found" + )) } fn sample_process_metrics( diff --git a/codex-rs/core/src/code_mode_host_tests.rs b/codex-rs/core/src/code_mode_host_tests.rs index db60c29bb5..70e5533f29 100644 --- a/codex-rs/core/src/code_mode_host_tests.rs +++ b/codex-rs/core/src/code_mode_host_tests.rs @@ -1,5 +1,6 @@ //! Exercises the process-owned gRPC transport against the standalone host. +use std::process::Stdio; use std::sync::Arc; use std::time::Duration; @@ -10,14 +11,19 @@ use codex_code_mode::CodeModeSession; use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::ExecuteRequest; use codex_code_mode::FunctionCallOutputContentItem; +use codex_code_mode::GrpcCodeModeSessionProvider; use codex_code_mode::NoopCodeModeSessionDelegate; use codex_code_mode::RuntimeResponse; +use codex_utils_process::background_command; #[cfg(unix)] use core_test_support::code_mode_host::CodeModeHostRecorder; +use hyper_util::rt::TokioIo; use pretty_assertions::assert_eq; +use tokio::process::Command; use tokio::time::timeout; +use tonic::transport::Endpoint; +use tower::service_fn; -use super::HostConnection; use super::ProcessOwnedGrpcCodeModeSessionProvider; const TEST_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 20); @@ -60,6 +66,23 @@ async fn execute( } } +#[test] +fn unavailable_host_keeps_a_bounded_utf8_path_suffix() { + let executable = "codex-code-mode-host-does-not-exist"; + for directory in ["missing-directory/", "🦀"] { + let path = format!("{}{executable}", directory.repeat(512)); + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program(path.into()); + let error = provider.availability().expect_err("missing host"); + let displayed = error + .strip_prefix("failed to spawn code-mode host ") + .and_then(|message| message.strip_suffix(": host executable was not found")) + .expect("missing-host diagnostic includes its path"); + assert!(displayed.starts_with("...")); + assert!(displayed.ends_with(executable)); + assert!(displayed.len() <= 512); + } +} + #[tokio::test] async fn grpc_stdio_sessions_share_a_host_and_isolate_state() -> Result<()> { #[cfg(unix)] @@ -134,11 +157,37 @@ async fn grpc_stdio_sessions_share_a_host_and_isolate_state() -> Result<()> { } #[tokio::test] -async fn grpc_stdio_host_exits_when_stdin_closes() -> Result<()> { - let mut connection = - HostConnection::spawn(&codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?)?; - let mut child = connection.child.take().context("host process")?; - drop(connection); +async fn default_host_serves_grpc_and_exits_when_stdin_closes() -> Result<()> { + let program = codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?; + let mut child = Command::from(background_command(program)) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .kill_on_drop(/*kill_on_drop*/ true) + .spawn()?; + let stdout = child.stdout.take().context("host stdout")?; + let stdin = child.stdin.take().context("host stdin")?; + let mut pipes = Some(TokioIo::new(tokio::io::join(stdout, stdin))); + let channel = timeout( + TEST_TIMEOUT, + Endpoint::from_static("http://code-mode-host").connect_with_connector(service_fn( + move |_| { + std::future::ready( + pipes + .take() + .ok_or_else(|| std::io::Error::other("host pipes already used")), + ) + }, + )), + ) + .await??; + let provider = GrpcCodeModeSessionProvider::with_channel(channel); + let session = timeout(TEST_TIMEOUT, provider.create_session()) + .await + .context("timed out opening a session on the default host")? + .map_err(anyhow::Error::msg)?; + session.shutdown().await.map_err(anyhow::Error::msg)?; + drop(session); + drop(provider); let status = timeout(TEST_TIMEOUT, child.wait()) .await .context("host did not exit on stdin EOF")??; diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 0b3bd03a18..23d1b1f8d6 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -21,6 +21,8 @@ use crate::agent::host::InheritedSessionId; use crate::agent::host::ServiceTierAcceptance; use crate::agents_md_manager::SessionInstructions; use crate::attestation::AttestationProvider; +use crate::code_mode_host::LocalServerOutputProvider; +use crate::code_mode_host::ProcessOwnedGrpcCodeModeSessionProvider; use crate::codex_thread::CodexThread; use crate::config::Config; use crate::current_time::TimeProvider; @@ -51,7 +53,6 @@ use codex_attachment_store::AttachmentStore; use codex_attachment_store::InlineAttachmentStore; use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::DisabledCodeModeSessionProvider; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_core_plugins::PluginsManager; use codex_exec_server::EnvironmentManager; use codex_execpolicy::Policy; @@ -808,23 +809,14 @@ impl ThreadManager { config: &Config, host_program: PathBuf, ) -> Arc { - let provider: Arc = - if config.features.enabled(Feature::CodeModeHostGrpc) { - Arc::new( - crate::code_mode_host::ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( - host_program, - ), - ) - } else { - Arc::new(ProcessOwnedCodeModeSessionProvider::with_host_program( - host_program, - )) - }; + let provider = Arc::new(ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( + host_program, + )); if config .features .enabled(Feature::CodeModeHostOutputTruncation) { - Arc::new(crate::code_mode_host::LocalServerOutputProvider { inner: provider }) + Arc::new(LocalServerOutputProvider { inner: provider }) } else { provider } diff --git a/codex-rs/core/tests/code_mode_tracing.rs b/codex-rs/core/tests/code_mode_tracing.rs index a6442c0ee7..4956e4ef8f 100644 --- a/codex-rs/core/tests/code_mode_tracing.rs +++ b/codex-rs/core/tests/code_mode_tracing.rs @@ -9,12 +9,12 @@ use codex_code_mode::CodeModeSessionProvider; use codex_code_mode::CodeModeToolKind; use codex_code_mode::ExecuteRequest; use codex_code_mode::NotificationFuture; -use codex_code_mode::ProcessOwnedCodeModeSessionProvider; use codex_code_mode::RuntimeResponse; use codex_code_mode::ToolDefinition; use codex_code_mode::ToolInvocationFuture; use codex_code_mode::WaitOutcome; use codex_code_mode::WaitRequest; +use codex_core::ProcessOwnedGrpcCodeModeSessionProvider; use codex_protocol::ToolName; use codex_protocol::protocol::W3cTraceContext; use opentelemetry::trace::TraceContextExt; @@ -84,7 +84,7 @@ async fn stdio_callbacks_preserve_context_across_overlapping_yielded_cells() { let subscriber = tracing_subscriber::registry() .with(tracing_opentelemetry::layer().with_tracer(telemetry.tracer("code-mode-test"))); let _subscriber = tracing::subscriber::set_default(subscriber); - let provider = ProcessOwnedCodeModeSessionProvider::with_host_program( + let provider = ProcessOwnedGrpcCodeModeSessionProvider::with_host_program( codex_utils_cargo_bin::cargo_bin("codex-code-mode-host").unwrap(), ); let session = provider.create_session().await.unwrap(); @@ -233,7 +233,7 @@ async fn stdio_callbacks_preserve_context_across_overlapping_yielded_cells() { .filter(|span| { matches!( span.name.as_ref(), - "code_mode.stdio.callback" | "code_mode.stdio.notify" + "code_mode.grpc.callback" | "code_mode.grpc.notify" ) }) .map(|span| { @@ -249,12 +249,12 @@ async fn stdio_callbacks_preserve_context_across_overlapping_yielded_cells() { callbacks, vec![ ( - "code_mode.stdio.callback", + "code_mode.grpc.callback", exec.span_context.trace_id(), exec.span_context.span_id() ), ( - "code_mode.stdio.notify", + "code_mode.grpc.notify", exec.span_context.trace_id(), exec.span_context.span_id() ), diff --git a/codex-rs/core/tests/common/code_mode_host.rs b/codex-rs/core/tests/common/code_mode_host.rs index 47aaf0e670..3703081955 100644 --- a/codex-rs/core/tests/common/code_mode_host.rs +++ b/codex-rs/core/tests/common/code_mode_host.rs @@ -8,7 +8,7 @@ use anyhow::Context; use anyhow::Result; use tempfile::TempDir; -/// Observes host process launches without replacing either transport implementation. +/// Observes launches of the real code-mode host. pub struct CodeModeHostRecorder { _directory: TempDir, program: PathBuf, diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs index 0b8cbdf86e..661416fa54 100644 --- a/codex-rs/core/tests/suite/code_mode.rs +++ b/codex-rs/core/tests/suite/code_mode.rs @@ -4386,10 +4386,8 @@ text(JSON.stringify({{ } #[cfg_attr(windows, ignore = "no exec_command on Windows")] -#[test_case(false; "legacy_stdio")] -#[test_case(true; "default_grpc_stdio")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn code_mode_only_can_call_nested_tools(grpc_stdio: bool) -> Result<()> { +async fn code_mode_only_can_call_nested_tools() -> Result<()> { skip_if_no_network!(Ok(())); #[cfg(unix)] @@ -4427,11 +4425,8 @@ text(output.output); ) .await; - let mut builder = test_codex().with_config(move |config| { + let mut builder = test_codex().with_config(|config| { let _ = config.features.enable(Feature::CodeModeOnly); - if !grpc_stdio { - config.features.disable(Feature::CodeModeHostGrpc).unwrap(); - } }); #[cfg(unix)] { @@ -4452,12 +4447,8 @@ text(output.output); #[cfg(unix)] assert_eq!( recorder.invocations()?, - vec![if grpc_stdio { - "--listen grpc+stdio://".to_string() - } else { - String::new() - }], - "the feature flag must select the expected host transport" + vec!["--listen grpc+stdio://".to_string()], + "local Codex must use gRPC over stdio" ); assert!( request @@ -5004,8 +4995,7 @@ text(`Variable truncated: ${resultVariableWasTruncated ? "True" : "False"}. Vari // limit `result.output` returned by the nested command. #[cfg_attr(windows, ignore = "no exec_command on Windows")] #[test_case(&[]; "default_grpc")] -#[test_case(&[Feature::CodeModeHostGrpc]; "server_stdio")] -#[test_case(&[Feature::CodeModeHostGrpc, Feature::CodeModeHostOutputTruncation]; "client_stdio")] +#[test_case(&[Feature::CodeModeHostOutputTruncation]; "client_grpc")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn code_mode_exec_outer_limit_truncates_emitted_output( disabled_features: &'static [Feature], @@ -5082,7 +5072,6 @@ Error:\ boom\n // An overflow notice is outside the observation budget. This distinguishes // actual server formatting from Core silently repeating client truncation. #[test_case(None; "default")] -#[test_case(Some(Feature::CodeModeHostGrpc); "server_stdio")] #[test_case(Some(Feature::CodeModeHostOutputTruncation); "client_grpc")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn code_mode_zero_budget_reports_host_truncation( @@ -5173,135 +5162,6 @@ try { Ok(()) } -/// The framed stdio host reconnects on the first exec after reporting a wait timeout. -#[tokio::test(flavor = "current_thread")] -async fn code_mode_stdio_wait_timeout_reconnects_on_next_exec() -> Result<()> { - skip_if_no_network!(Ok(())); - - let server = responses::start_mock_server().await; - let mut builder = test_codex() - .with_model("test-gpt-5.1-codex") - .with_config(|config| { - config.features.disable(Feature::CodeModeHostGrpc).unwrap(); - config - .features - .enable(Feature::CodeMode) - .expect("code mode should be enabled"); - }); - let test = builder.build_with_auto_env(&server).await?; - - let first_turn = responses::mount_sse_sequence( - &server, - vec![ - sse(vec![ - ev_response_created("resp-1"), - ev_custom_tool_call( - "call-1", - "exec", - "yield_control(); await new Promise(() => {});", - ), - ev_completed("resp-1"), - ]), - sse(vec![ - ev_assistant_message("msg-1", "waiting"), - ev_completed("resp-2"), - ]), - ], - ) - .await; - - test.submit_turn("start a stalled code-mode cell").await?; - let first_request = first_turn - .last_request() - .expect("initial exec should be returned to the model"); - let first_items = custom_tool_output_items(&first_request, "call-1"); - let cell_id = extract_running_cell_id(text_item(&first_items, /*index*/ 0)); - - let timeout_completion = responses::mount_function_call_agent_response( - &server, - "call-2", - &serde_json::to_string(&serde_json::json!({ - "cell_id": cell_id, - "yield_time_ms": 60_000, - }))?, - "wait", - ) - .await - .completion; - - test.codex - .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text { - text: "wait for the stalled cell".to_string(), - text_elements: Vec::new(), - }])) - .await?; - wait_for_event_match(&test.codex, |event| match event { - EventMsg::RawResponseItem(raw) => match &raw.item { - ResponseItem::FunctionCall { call_id, .. } if call_id == "call-2" => Some(()), - _ => None, - }, - _ => None, - }) - .await; - - tokio::time::pause(); - for _ in 0..130 { - tokio::time::advance(Duration::from_secs(1)).await; - if timeout_completion - .function_call_output_text("call-2") - .is_some() - { - break; - } - } - tokio::time::resume(); - wait_for_event(&test.codex, |event| { - matches!(event, EventMsg::TurnComplete(_)) - }) - .await; - - let timeout_output = timeout_completion - .function_call_output_text("call-2") - .expect("timed-out wait should be returned to the model"); - assert!( - timeout_output.contains("code-mode host timed out waiting for wait response"), - "unexpected wait output: {timeout_output}" - ); - - let reconnect_turn = responses::mount_sse_sequence( - &server, - vec![ - sse(vec![ - ev_response_created("resp-5"), - ev_custom_tool_call( - "call-3", - "exec", - r#"text("reconnected"); yield_control(); await new Promise(() => {});"#, - ), - ev_completed("resp-5"), - ]), - sse(vec![ - ev_assistant_message("msg-3", "reconnected"), - ev_completed("resp-6"), - ]), - ], - ) - .await; - - test.submit_turn("run a cell after the timeout").await?; - let reconnect_request = reconnect_turn - .last_request() - .expect("replacement exec should be returned to the model"); - let reconnect_items = custom_tool_output_items(&reconnect_request, "call-3"); - assert_eq!( - extract_running_cell_id(text_item(&reconnect_items, /*index*/ 0)), - "g2:1" - ); - assert_eq!(text_item(&reconnect_items, /*index*/ 1), "reconnected"); - - Ok(()) -} - #[derive(Default)] struct ResponseIdObserver { response_ids: Mutex)>>, @@ -6053,8 +5913,7 @@ impl ToolLifecycleContributor for TerminationOutputReady { #[cfg_attr(windows, ignore = "no exec_command on Windows")] #[test_case(&[]; "default_grpc")] -#[test_case(&[Feature::CodeModeHostGrpc]; "server_stdio")] -#[test_case(&[Feature::CodeModeHostGrpc, Feature::CodeModeHostOutputTruncation]; "client_stdio")] +#[test_case(&[Feature::CodeModeHostOutputTruncation]; "client_grpc")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn code_mode_wait_can_terminate_and_continue( disabled_features: &'static [Feature], @@ -6784,8 +6643,7 @@ async fn code_mode_interrupt_terminates_active_cells_and_nested_tools() -> Resul } #[test_case(&[]; "default_grpc")] -#[test_case(&[Feature::CodeModeHostGrpc]; "server_stdio")] -#[test_case(&[Feature::CodeModeHostGrpc, Feature::CodeModeHostOutputTruncation]; "client_stdio")] +#[test_case(&[Feature::CodeModeHostOutputTruncation]; "client_grpc")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn code_mode_wait_uses_its_own_max_tokens_budget( disabled_features: &'static [Feature], @@ -6796,7 +6654,7 @@ async fn code_mode_wait_uses_its_own_max_tokens_budget( let mut builder = code_mode_test_builder(disabled_features); let test = builder.build_with_auto_env(&server).await?; - // Both transports accept exec budgets above i32::MAX. The wait uses its own budget. + // Exec accepts budgets above i32::MAX. The wait uses its own budget. let code = r#"// @exec: {"max_output_tokens": 2147483648} text("phase 1"); yield_control(); diff --git a/codex-rs/core/tests/suite/code_mode_agent_message_tests.rs b/codex-rs/core/tests/suite/code_mode_agent_message_tests.rs index f418218db5..35e0644dbc 100644 --- a/codex-rs/core/tests/suite/code_mode_agent_message_tests.rs +++ b/codex-rs/core/tests/suite/code_mode_agent_message_tests.rs @@ -40,12 +40,7 @@ async fn yielded_code_mode_spawn_preserves_metadata_and_plaintext() -> Result<() .push("test_sync_tool".to_string()); }) .with_config(|config| { - for feature in [ - Feature::Collab, - Feature::MultiAgentV2, - Feature::CodeMode, - Feature::CodeModeHostGrpc, - ] { + for feature in [Feature::Collab, Feature::MultiAgentV2, Feature::CodeMode] { config .features .enable(feature) diff --git a/codex-rs/core/tests/suite/code_mode_host_recovery_tests.rs b/codex-rs/core/tests/suite/code_mode_host_recovery_tests.rs index 4fe19bb7b1..1bfd0c1063 100644 --- a/codex-rs/core/tests/suite/code_mode_host_recovery_tests.rs +++ b/codex-rs/core/tests/suite/code_mode_host_recovery_tests.rs @@ -44,7 +44,6 @@ async fn grpc_host_loss_reports_reset_without_replaying_side_effects() -> Result .with_code_mode_host_program(recorder.program()) .with_config(|config| { config.features.enable(Feature::CodeModeOnly).unwrap(); - config.features.enable(Feature::CodeModeHostGrpc).unwrap(); }); let harness = TestCodexHarness::with_auto_env_builder(builder).await?; let test = harness.test(); diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index d4fc5d5c7d..0be0bffc1d 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -159,7 +159,7 @@ pub enum Feature { CodeModeBufferedExec, /// Run JavaScript code mode in the standalone host process. CodeModeHost, - /// Use gRPC over stdio for the process-owned code-mode host. + /// Removed compatibility flag. The local code-mode host always uses gRPC over stdio. CodeModeHostGrpc, /// Truncate output in the process-owned code-mode host before sending it to Core. CodeModeHostOutputTruncation, @@ -774,6 +774,13 @@ impl Features { ); continue; } + "code_mode_host_grpc" => { + self.record_legacy_usage_force( + "features.code_mode_host_grpc", + Feature::CodeModeHostGrpc, + ); + continue; + } "transcript_v2" => { self.record_legacy_usage_force("features.transcript_v2", Feature::TranscriptV2); continue; @@ -897,6 +904,10 @@ fn legacy_usage_notice(alias: &str, feature: Feature) -> (String, Option "`[features.guardianv2].thread_context` is deprecated and ignored.".to_string(), Some("Thread-owned Guardian context is always enabled. Remove `thread_context` from [features.guardianv2] in config.toml, including profile overrides.".to_string()), ), + Feature::CodeModeHostGrpc => ( + "`[features].code_mode_host_grpc` is deprecated and ignored.".to_string(), + Some("The local code-mode host always uses gRPC over stdio. Remove this setting from your configuration.".to_string()), + ), Feature::TranscriptV2 => ( "`[features].transcript_v2` is deprecated and ignored.".to_string(), Some("Use `[tui].fullscreen_transcript` in config.toml instead.".to_string()), @@ -1368,8 +1379,8 @@ pub const FEATURES: &[FeatureSpec] = &[ FeatureSpec { id: Feature::CodeModeHostGrpc, key: "code_mode_host_grpc", - stage: Stage::Stable, - default_enabled: true, + stage: Stage::Removed, + default_enabled: false, }, FeatureSpec { id: Feature::CodeModeHostOutputTruncation, diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index bb13583dba..37133a9195 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -661,6 +661,28 @@ fn from_sources_applies_base_profile_and_overrides() { assert_eq!(features.enabled(Feature::WebSearchRequest), false); } +#[test] +fn code_mode_host_grpc_is_recognized_and_ignored() { + assert!(crate::is_known_feature_key("code_mode_host_grpc")); + for enabled in [false, true] { + let config: FeaturesToml = toml::from_str(&format!("code_mode_host_grpc = {enabled}")) + .expect("legacy transport setting should deserialize"); + let mut features = Features::with_defaults(); + features.apply_toml(&config); + assert_eq!( + features.enabled_features(), + Features::with_defaults().enabled_features() + ); + assert_eq!( + features + .legacy_feature_usages() + .map(|usage| usage.feature) + .collect::>(), + vec![Feature::CodeModeHostGrpc] + ); + } +} + #[test] fn from_sources_ignores_removed_image_detail_original_feature_key() { let features_toml = FeaturesToml::from(BTreeMap::from([(