mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Add a dual-WebSocket transport for code mode (#36812)
## Why Large nested-tool callbacks can occupy a WebSocket and delay unrelated session operations on the same code-mode connection. ## What changed - Negotiate the optional `dual-websocket-v1` capability and pair a second, token-scoped WebSocket with the control connection. - Route nested-tool callbacks and their results over the bulk socket while keeping session operations, notifications, and execution responses on the control socket. Reject messages sent on the wrong lane. - Preserve the single-connection transport when the capability is unavailable, and bound pairing, queued callbacks, and deferred cross-socket messages. - Defer callbacks that arrive before their execution-started response, and return delegate errors without disconnecting the connection. ## Testing Add protocol, transport, driver, and WebSocket integration coverage for capability negotiation, lane routing, pairing failures, out-of-order messages, and progress during large concurrent tool results. GitOrigin-RevId: fa4504653e7cbf3c4ec930ae57aa0a41345bad66
This commit is contained in:
committed by
copyberry
parent
b258c028fe
commit
60c722e075
@@ -14,17 +14,21 @@ use codex_code_mode_protocol::ExecuteRequest;
|
||||
use codex_code_mode_protocol::StartedCell;
|
||||
use codex_code_mode_protocol::WaitOutcome;
|
||||
use codex_code_mode_protocol::WaitRequest;
|
||||
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::DUAL_WEBSOCKET_CAPABILITY;
|
||||
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::MAX_FRAME_BYTES;
|
||||
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::SupportedProtocolVersions;
|
||||
use codex_code_mode_protocol::host::TransportLane;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_websocket_client::WebSocketConnector;
|
||||
use futures::StreamExt;
|
||||
@@ -36,6 +40,7 @@ use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
use tokio_tungstenite::tungstenite::http::Uri;
|
||||
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::debug;
|
||||
@@ -68,6 +73,7 @@ pub(super) enum ConnectionError {
|
||||
host_program: PathBuf,
|
||||
error: io::Error,
|
||||
},
|
||||
BulkConnectionUnavailable(String),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
@@ -98,7 +104,9 @@ impl fmt::Display for ConnectionError {
|
||||
&host_program[suffix_start..]
|
||||
)
|
||||
}
|
||||
Self::Other(message) => formatter.write_str(message),
|
||||
Self::BulkConnectionUnavailable(message) | Self::Other(message) => {
|
||||
formatter.write_str(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,6 +140,45 @@ enum ConnectionOwner {
|
||||
WebSocket,
|
||||
}
|
||||
|
||||
struct BulkConnectionOptions {
|
||||
websocket_url: String,
|
||||
http_client_factory: HttpClientFactory,
|
||||
}
|
||||
|
||||
impl BulkConnectionOptions {
|
||||
async fn connect(
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<(ConnectionReader, ConnectionWriter), ConnectionError> {
|
||||
let control_uri = self.websocket_url.parse::<Uri>().map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to build code-mode host bulk websocket URL: {error}"
|
||||
))
|
||||
})?;
|
||||
let bulk_path = format!("{}/bulk/{token}", control_uri.path().trim_end_matches('/'));
|
||||
let bulk_path_and_query = match control_uri.query() {
|
||||
Some(query) => format!("{bulk_path}?{query}"),
|
||||
None => bulk_path,
|
||||
};
|
||||
let mut bulk_uri_parts = control_uri.into_parts();
|
||||
bulk_uri_parts.path_and_query = Some(bulk_path_and_query.parse().map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to build code-mode host bulk websocket path: {error}"
|
||||
))
|
||||
})?);
|
||||
let bulk_url = Uri::from_parts(bulk_uri_parts)
|
||||
.map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to build code-mode host bulk websocket URL: {error}"
|
||||
))
|
||||
})?
|
||||
.to_string();
|
||||
connect_websocket_transport(&bulk_url, &self.http_client_factory)
|
||||
.await
|
||||
.map_err(|error| ConnectionError::BulkConnectionUnavailable(error.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl CallerCancellation {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
@@ -202,6 +249,7 @@ impl Connection {
|
||||
ConnectionReader::Stdio(FramedReader::new(stdout)),
|
||||
ConnectionWriter::Stdio(FramedWriter::new(stdin)),
|
||||
ConnectionOwner::Process(Box::new(child)),
|
||||
/*bulk_connection_options*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -210,38 +258,17 @@ impl Connection {
|
||||
websocket_url: &str,
|
||||
http_client_factory: &HttpClientFactory,
|
||||
) -> Result<Self, ConnectionError> {
|
||||
let request = websocket_url.into_client_request().map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to build code-mode host websocket request: {error}"
|
||||
))
|
||||
})?;
|
||||
let connector = WebSocketConnector::new(http_client_factory).map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to configure code-mode host websocket TLS: {error}"
|
||||
))
|
||||
})?;
|
||||
let websocket_config = WebSocketConfig::default()
|
||||
.max_frame_size(Some(MAX_WEBSOCKET_FRAME_BYTES))
|
||||
.max_message_size(Some(MAX_WEBSOCKET_FRAME_BYTES));
|
||||
let (websocket, _) = tokio::time::timeout(
|
||||
HOST_HANDSHAKE_TIMEOUT,
|
||||
connector.connect(request, websocket_config),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
ConnectionError::Other("timed out connecting to the code-mode host websocket".into())
|
||||
})?
|
||||
.map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to connect to the code-mode host websocket: {error}"
|
||||
))
|
||||
})?;
|
||||
let (writer, reader) = websocket.split();
|
||||
let (reader, writer) =
|
||||
connect_websocket_transport(websocket_url, http_client_factory).await?;
|
||||
|
||||
Self::establish(
|
||||
ConnectionReader::WebSocket(reader),
|
||||
ConnectionWriter::WebSocket(writer),
|
||||
reader,
|
||||
writer,
|
||||
ConnectionOwner::WebSocket,
|
||||
Some(BulkConnectionOptions {
|
||||
websocket_url: websocket_url.to_string(),
|
||||
http_client_factory: http_client_factory.clone(),
|
||||
}),
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -250,13 +277,22 @@ impl Connection {
|
||||
mut reader: ConnectionReader,
|
||||
mut writer: ConnectionWriter,
|
||||
mut owner: ConnectionOwner,
|
||||
bulk_connection_options: Option<BulkConnectionOptions>,
|
||||
) -> Result<Self, ConnectionError> {
|
||||
let handshake = async {
|
||||
let dual_capability =
|
||||
Capability::new(DUAL_WEBSOCKET_CAPABILITY).map_err(|error| error.to_string())?;
|
||||
let optional_capabilities = if bulk_connection_options.is_some() {
|
||||
CapabilitySet::try_new([dual_capability.clone()])
|
||||
.map_err(|error| error.to_string())?
|
||||
} else {
|
||||
CapabilitySet::empty()
|
||||
};
|
||||
let hello = ClientHello::new(
|
||||
SupportedProtocolVersions::try_new([ProtocolVersion::V1])
|
||||
.map_err(|err| err.to_string())?,
|
||||
CapabilitySet::empty(),
|
||||
CapabilitySet::empty(),
|
||||
optional_capabilities,
|
||||
)
|
||||
.map_err(|err| err.to_string())?;
|
||||
writer
|
||||
@@ -271,7 +307,20 @@ impl Connection {
|
||||
Some(HostToClient::HostHello(hello))
|
||||
if hello.selected_version() == ProtocolVersion::V1 =>
|
||||
{
|
||||
Ok(())
|
||||
if hello.capabilities().contains(&dual_capability) {
|
||||
hello
|
||||
.bulk_connection_token()
|
||||
.map(str::to_string)
|
||||
.ok_or_else(|| {
|
||||
"code-mode host advertised dual websockets without a pairing token"
|
||||
.to_string()
|
||||
})
|
||||
.map(Some)
|
||||
} else if hello.bulk_connection_token().is_some() {
|
||||
Err("code-mode host returned an unexpected bulk pairing token".to_string())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
Some(HostToClient::HandshakeRejected { reason }) => {
|
||||
Err(format!("code-mode host rejected the handshake: {reason:?}"))
|
||||
@@ -292,51 +341,85 @@ impl Connection {
|
||||
));
|
||||
}
|
||||
};
|
||||
if let Err(err) = handshake_result {
|
||||
let _ = writer.close().await;
|
||||
owner.close().await;
|
||||
return Err(ConnectionError::Other(err));
|
||||
}
|
||||
let bulk_token = match handshake_result {
|
||||
Ok(token) => token,
|
||||
Err(err) => {
|
||||
let _ = writer.close().await;
|
||||
owner.close().await;
|
||||
return Err(ConnectionError::Other(err));
|
||||
}
|
||||
};
|
||||
let (bulk_reader, bulk_writer) = if let Some(token) = bulk_token {
|
||||
let Some(options) = bulk_connection_options else {
|
||||
let _ = writer.close().await;
|
||||
owner.close().await;
|
||||
return Err(ConnectionError::Other(
|
||||
"code-mode host negotiated an unsupported bulk websocket".to_string(),
|
||||
));
|
||||
};
|
||||
match options.connect(&token).await {
|
||||
Ok((reader, writer)) => (Some(reader), Some(writer)),
|
||||
Err(error) => {
|
||||
let _ = writer.close().await;
|
||||
owner.close().await;
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
let (command_tx, command_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY);
|
||||
let (event_tx, event_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY);
|
||||
let (outgoing_tx, mut outgoing_rx) = mpsc::channel::<EncodedFrame>(IPC_CHANNEL_CAPACITY);
|
||||
let (outgoing_tx, outgoing_rx) = mpsc::channel::<EncodedFrame>(IPC_CHANNEL_CAPACITY);
|
||||
let (bulk_tx, bulk_rx) = if bulk_writer.is_some() {
|
||||
let (sender, receiver) = mpsc::channel::<EncodedFrame>(MAX_PENDING_DELEGATE_CALLS);
|
||||
(Some(sender), Some(receiver))
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
let dual_websocket = bulk_writer.is_some();
|
||||
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 {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = writer_cancellation.cancelled() => {
|
||||
return writer
|
||||
.close()
|
||||
.await
|
||||
.map_err(|error| format!("failed to close code-mode host connection: {error}"));
|
||||
}
|
||||
frame = outgoing_rx.recv() => {
|
||||
let Some(frame) = frame else {
|
||||
return Err("code-mode host outgoing stream closed".to_string());
|
||||
};
|
||||
let result = tokio::select! {
|
||||
_ = writer_cancellation.cancelled() => return Ok(()),
|
||||
result = writer.write_frame(frame) => result,
|
||||
};
|
||||
if let Err(err) = result {
|
||||
return Err(format!("failed to write code-mode host message: {err}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let (Some(bulk_writer), Some(bulk_rx)) = (bulk_writer, bulk_rx) {
|
||||
tokio::try_join!(
|
||||
drive_writer(writer, outgoing_rx, writer_cancellation.clone()),
|
||||
drive_writer(bulk_writer, bulk_rx, writer_cancellation)
|
||||
)?;
|
||||
Ok(())
|
||||
} else {
|
||||
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 reader_task = tokio::spawn(async move {
|
||||
let lane = dual_websocket.then_some(TransportLane::Control);
|
||||
if let Some(bulk_reader) = bulk_reader {
|
||||
tokio::try_join!(
|
||||
drive_reader(
|
||||
reader,
|
||||
reader_events.clone(),
|
||||
reader_cancellation.clone(),
|
||||
lane,
|
||||
),
|
||||
drive_reader(
|
||||
bulk_reader,
|
||||
reader_events,
|
||||
reader_cancellation,
|
||||
Some(TransportLane::Bulk),
|
||||
)
|
||||
)?;
|
||||
Ok(())
|
||||
} else {
|
||||
drive_reader(reader, reader_events, reader_cancellation, lane).await
|
||||
}
|
||||
});
|
||||
|
||||
let (driver, execute_claim_tx) = ConnectionDriver::new(
|
||||
command_rx,
|
||||
@@ -349,6 +432,10 @@ impl Connection {
|
||||
cancellation: cancellation.clone(),
|
||||
},
|
||||
);
|
||||
let driver = match bulk_tx {
|
||||
Some(sender) => driver.with_bulk_sender(sender),
|
||||
None => driver,
|
||||
};
|
||||
let driver_task = tokio::spawn(driver.run());
|
||||
tokio::spawn(
|
||||
ConnectionSupervisor {
|
||||
@@ -504,6 +591,73 @@ impl Connection {
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_websocket_transport(
|
||||
websocket_url: &str,
|
||||
http_client_factory: &HttpClientFactory,
|
||||
) -> Result<(ConnectionReader, ConnectionWriter), ConnectionError> {
|
||||
let request = websocket_url.into_client_request().map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to build code-mode host websocket request: {error}"
|
||||
))
|
||||
})?;
|
||||
let connector = WebSocketConnector::new(http_client_factory).map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to configure code-mode host websocket TLS: {error}"
|
||||
))
|
||||
})?;
|
||||
let websocket_config = WebSocketConfig::default()
|
||||
.max_frame_size(Some(MAX_WEBSOCKET_FRAME_BYTES))
|
||||
.max_message_size(Some(MAX_WEBSOCKET_FRAME_BYTES));
|
||||
let (websocket, _) = tokio::time::timeout(
|
||||
HOST_HANDSHAKE_TIMEOUT,
|
||||
connector.connect(request, websocket_config),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
ConnectionError::Other("timed out connecting to the code-mode host websocket".into())
|
||||
})?
|
||||
.map_err(|error| {
|
||||
ConnectionError::Other(format!(
|
||||
"failed to connect to the code-mode host websocket: {error}"
|
||||
))
|
||||
})?;
|
||||
let (writer, reader) = websocket.split();
|
||||
Ok((
|
||||
ConnectionReader::WebSocket(reader),
|
||||
ConnectionWriter::WebSocket(writer),
|
||||
))
|
||||
}
|
||||
|
||||
async fn drive_writer(
|
||||
mut writer: ConnectionWriter,
|
||||
mut outgoing: mpsc::Receiver<EncodedFrame>,
|
||||
cancellation: CancellationToken,
|
||||
) -> Result<(), String> {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = cancellation.cancelled() => {
|
||||
return writer
|
||||
.close()
|
||||
.await
|
||||
.map_err(|error| format!("failed to close code-mode host connection: {error}"));
|
||||
}
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user