mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Use gRPC for all code-mode host connections (#62476)
## What changed Remove the legacy framed stdio protocol and its client and server. Local Codex launches the standalone host with `--listen grpc+stdio://`, which is also the host's default. The `stdio` and `stdio://` aliases now select gRPC over stdio. The `features.code_mode_host_grpc` setting is deprecated and ignored. Carry execution trace context into gRPC tool callbacks and notifications. Reject oversized execute requests before sending them so an encoding failure does not appear as a connection reset. ## Testing Exercise gRPC callback cancellation, trace context across overlapping yielded cells, and isolation of tool preload failures between sessions. Check that both values of the deprecated feature setting are ignored. GitOrigin-RevId: f5faff3901dc60958627f677f50cfad834291b69
This commit is contained in:
committed by
copyberry
parent
083bf59b6b
commit
336dc645fb
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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<Self>,
|
||||
mut call: grpc::ToolCall,
|
||||
cancellation: CancellationToken,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
) {
|
||||
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<Self>,
|
||||
notification: grpc::Notification,
|
||||
cancellation: CancellationToken,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
) {
|
||||
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)]
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<GrpcTransport>;
|
||||
type ShutdownResultReceiver = watch::Receiver<Option<Result<(), String>>>;
|
||||
|
||||
async fn wait_for_watch<T>(
|
||||
mut result_rx: watch::Receiver<Option<Result<T, String>>>,
|
||||
) -> Result<T, String>
|
||||
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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<ReconnectInner>,
|
||||
|
||||
@@ -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<dyn CodeModeSessionDelegate>);
|
||||
|
||||
pub(super) enum CallbackAdmission {
|
||||
Active(CancellationToken, Arc<dyn CodeModeSessionDelegate>),
|
||||
Active(
|
||||
CancellationToken,
|
||||
Arc<dyn CodeModeSessionDelegate>,
|
||||
Option<W3cTraceContext>,
|
||||
),
|
||||
Cancelled,
|
||||
Closed,
|
||||
Rejected(String),
|
||||
}
|
||||
|
||||
struct ExecutionRecord {
|
||||
// Keep the caller's context for callbacks without extending its span across a yield.
|
||||
trace: Option<W3cTraceContext>,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
cell_id: Option<CellId>,
|
||||
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(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Option<Result<(), String>>>;
|
||||
|
||||
/// Creates code-mode sessions backed by one lazily spawned process host.
|
||||
pub struct ProcessOwnedCodeModeSessionProvider {
|
||||
host: Arc<OwnedCodeModeHost>,
|
||||
}
|
||||
|
||||
/// 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<OwnedCodeModeHost> {
|
||||
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<OwnedCodeModeHost>,
|
||||
options: CodeModeSessionOptions,
|
||||
) -> Result<Arc<dyn CodeModeSession>, String> {
|
||||
let session = ProcessOwnedCodeModeSession::with_host(host, options);
|
||||
session.connection().await?;
|
||||
Ok(Arc::new(session))
|
||||
}
|
||||
|
||||
struct OwnedCodeModeHost {
|
||||
host_program: PathBuf,
|
||||
connection: StdMutex<Option<Arc<Connection>>>,
|
||||
connect_permit: Semaphore,
|
||||
connection_generation: AtomicU64,
|
||||
last_connection_error: StdMutex<Option<(u64, String)>>,
|
||||
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<Arc<Connection>, 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<Arc<Connection>> {
|
||||
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<Option<Result<SessionBinding, String>>>,
|
||||
},
|
||||
Open(SessionBinding),
|
||||
Closing,
|
||||
Closed,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SessionBinding {
|
||||
catalog: Arc<tokio::sync::Mutex<crate::tool_catalog::Catalog>>,
|
||||
connection: Arc<Connection>,
|
||||
remote: RemoteSession,
|
||||
cleanup: SessionCleanup,
|
||||
}
|
||||
|
||||
struct SessionInner {
|
||||
host: Arc<OwnedCodeModeHost>,
|
||||
options: CodeModeSessionOptions,
|
||||
// The first successful open fixes the output policy for all generations of this session.
|
||||
output_truncation: OnceLock<CodeModeOutputTruncation>,
|
||||
state: StdMutex<SessionState>,
|
||||
shutdown_requested: AtomicBool,
|
||||
shutdown_result: StdMutex<Option<ShutdownResultReceiver>>,
|
||||
retired_cleanups: StdMutex<Vec<SessionCleanup>>,
|
||||
}
|
||||
|
||||
/// A logical code-mode session assigned to a process host.
|
||||
pub struct ProcessOwnedCodeModeSession {
|
||||
inner: Arc<SessionInner>,
|
||||
}
|
||||
|
||||
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<OwnedCodeModeHost>, 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<SessionBinding, String> {
|
||||
self.inner.connection().await
|
||||
}
|
||||
|
||||
fn server_output_budget(&self, budget: Option<usize>) -> Option<usize> {
|
||||
budget.filter(|_| self.output_truncation() == CodeModeOutputTruncation::Server)
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
&self,
|
||||
mut request: ExecuteRequest,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
preempt: Option<CancellationToken>,
|
||||
) -> Result<StartedCell, String> {
|
||||
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<CancellationToken>,
|
||||
) -> Result<WaitOutcome, String> {
|
||||
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<WaitOutcome, String> {
|
||||
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<Self>) -> Result<SessionBinding, String> {
|
||||
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<Self>,
|
||||
remote: RemoteSession,
|
||||
result_tx: watch::Sender<Option<Result<SessionBinding, String>>>,
|
||||
) {
|
||||
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<Self>) -> 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<Self>) -> 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<Option<Result<SessionBinding, String>>>),
|
||||
Finish,
|
||||
WaitForSessionCleanup(SessionCleanup),
|
||||
Close(SessionBinding),
|
||||
}
|
||||
|
||||
pub(crate) async fn wait_for_watch<T>(
|
||||
mut result_rx: watch::Receiver<Option<Result<T, String>>>,
|
||||
) -> Result<T, String>
|
||||
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<dyn CodeModeSessionDelegate>,
|
||||
preempt: Option<CancellationToken>,
|
||||
) -> CodeModeSessionResultFuture<'a, StartedCell> {
|
||||
Box::pin(ProcessOwnedCodeModeSession::execute(
|
||||
self, request, delegate, preempt,
|
||||
))
|
||||
}
|
||||
|
||||
fn wait<'a>(
|
||||
&'a self,
|
||||
request: WaitRequest,
|
||||
preempt: Option<CancellationToken>,
|
||||
) -> 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;
|
||||
@@ -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<DriverCommand>,
|
||||
execute_claim_tx: mpsc::UnboundedSender<RequestId>,
|
||||
alive: Arc<AtomicBool>,
|
||||
failure: Arc<std::sync::Mutex<Option<String>>>,
|
||||
cancellation: CancellationToken,
|
||||
capabilities: CapabilitySet,
|
||||
}
|
||||
|
||||
struct CallerCancellation {
|
||||
token: CancellationToken,
|
||||
armed: bool,
|
||||
}
|
||||
|
||||
struct ConnectionSupervisor {
|
||||
child: Child,
|
||||
event_tx: mpsc::Sender<DriverEvent>,
|
||||
cancellation: CancellationToken,
|
||||
alive: Arc<AtomicBool>,
|
||||
failure: Arc<std::sync::Mutex<Option<String>>>,
|
||||
driver_task: JoinHandle<()>,
|
||||
reader_task: JoinHandle<Result<(), String>>,
|
||||
writer_task: JoinHandle<Result<(), String>>,
|
||||
}
|
||||
|
||||
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<Self, ConnectionError> {
|
||||
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<ChildStdout>,
|
||||
mut writer: FramedWriter<ChildStdin>,
|
||||
mut child: Child,
|
||||
) -> Result<Self, ConnectionError> {
|
||||
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::<EncodedFrame>(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<CodeModeOutputTruncation>,
|
||||
) -> Result<SessionCleanup, String> {
|
||||
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<dyn CodeModeSessionDelegate>,
|
||||
yield_signal: Option<CancellationToken>,
|
||||
) -> Result<StartedCell, String> {
|
||||
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<CancellationToken>,
|
||||
) -> Result<WaitOutcome, String> {
|
||||
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<CancellationToken>,
|
||||
) -> Option<CancellationToken> {
|
||||
self.supports(YIELD_OBSERVATION_CAPABILITY)
|
||||
.then_some(signal)
|
||||
.flatten()
|
||||
}
|
||||
|
||||
pub(super) async fn terminate(
|
||||
&self,
|
||||
session: RemoteSession,
|
||||
request: TerminateRequest,
|
||||
) -> Result<WaitOutcome, String> {
|
||||
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<T>(
|
||||
&self,
|
||||
runtime_timeout: Duration,
|
||||
request_type: &str,
|
||||
request: impl Future<Output = Result<T, String>>,
|
||||
) -> Result<T, String> {
|
||||
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<T>(
|
||||
&self,
|
||||
response_rx: oneshot::Receiver<Result<T, String>>,
|
||||
) -> Result<T, String> {
|
||||
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<ChildStdin>,
|
||||
mut outgoing: mpsc::Receiver<EncodedFrame>,
|
||||
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<Result<(), String>, 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<Option<String>>,
|
||||
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<Option<String>>) -> 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;
|
||||
}
|
||||
@@ -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<AtomicBool>,
|
||||
pub(super) failure: Arc<std::sync::Mutex<Option<String>>>,
|
||||
pub(super) cancellation: CancellationToken,
|
||||
}
|
||||
|
||||
pub(super) struct ConnectionDriver {
|
||||
command_rx: mpsc::Receiver<DriverCommand>,
|
||||
event_rx: mpsc::Receiver<DriverEvent>,
|
||||
event_tx: mpsc::Sender<DriverEvent>,
|
||||
execute_claim_rx: mpsc::UnboundedReceiver<RequestId>,
|
||||
outgoing_tx: mpsc::Sender<EncodedFrame>,
|
||||
requests: RequestTracker,
|
||||
deferred_host_messages: VecDeque<HostToClient>,
|
||||
sessions: SessionRegistry,
|
||||
delegates: DelegateRuntime,
|
||||
alive: Arc<AtomicBool>,
|
||||
failure: Arc<std::sync::Mutex<Option<String>>>,
|
||||
cancellation: CancellationToken,
|
||||
failed: bool,
|
||||
}
|
||||
|
||||
impl ConnectionDriver {
|
||||
pub(super) fn new(
|
||||
command_rx: mpsc::Receiver<DriverCommand>,
|
||||
event_rx: mpsc::Receiver<DriverEvent>,
|
||||
event_tx: mpsc::Sender<DriverEvent>,
|
||||
outgoing_tx: mpsc::Sender<EncodedFrame>,
|
||||
lifecycle: DriverLifecycle,
|
||||
) -> (Self, mpsc::UnboundedSender<RequestId>) {
|
||||
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<dyn CodeModeSessionDelegate>, cell_id: &CellId) {
|
||||
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| delegate.cell_closed(cell_id)));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "driver_tests.rs"]
|
||||
mod tests;
|
||||
@@ -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<WireCellId, String> {
|
||||
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<WireWaitRequest, String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<CleanupInner>,
|
||||
}
|
||||
|
||||
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<CellOwner>) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -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<Result<(), String>>,
|
||||
) -> 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<dyn CodeModeSessionDelegate>,
|
||||
caller_cancellation: CancellationToken,
|
||||
yield_signal: Option<CancellationToken>,
|
||||
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
|
||||
) -> 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<CancellationToken>,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
) -> 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<usize>,
|
||||
caller_cancellation: CancellationToken,
|
||||
yield_signal: Option<CancellationToken>,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
) -> 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<Result<WaitOutcome, String>>,
|
||||
) -> 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<Result<(), String>>,
|
||||
) -> 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)
|
||||
}
|
||||
}
|
||||
@@ -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<DelegateResponse, String>)>,
|
||||
pub(super) closed_cells: Vec<CellOwner>,
|
||||
}
|
||||
|
||||
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<DelegateRequestId, DelegateCall>,
|
||||
seen_requests: HashSet<DelegateRequestId>,
|
||||
request_order: VecDeque<DelegateRequestId>,
|
||||
event_tx: mpsc::Sender<DriverEvent>,
|
||||
}
|
||||
|
||||
impl DelegateRuntime {
|
||||
pub(super) fn new(event_tx: mpsc::Sender<DriverEvent>) -> 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<DelegateResponse, String>,
|
||||
) -> 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<CellOwner>) -> 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<FailedSession>) {
|
||||
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<DelegateResponse, String>,
|
||||
) -> bool {
|
||||
let effects = self.delegates.complete(id, result);
|
||||
self.apply_delegate_effects(effects)
|
||||
}
|
||||
|
||||
fn send_delegate_response(
|
||||
&mut self,
|
||||
id: DelegateRequestId,
|
||||
result: Result<DelegateResponse, String>,
|
||||
) -> 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
|
||||
}
|
||||
}
|
||||
@@ -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<RequestId, PendingRequest>,
|
||||
unclaimed_executes: HashMap<RequestId, UnclaimedExecute>,
|
||||
initial_responses: HashMap<RequestId, InitialResponse>,
|
||||
deferred_waits: VecDeque<DeferredWait>,
|
||||
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<RequestId, String> {
|
||||
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<DriverEvent>,
|
||||
) {
|
||||
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<PendingRequest> {
|
||||
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<InitialResponse> {
|
||||
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<CancellationAction> {
|
||||
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::<Vec<_>>();
|
||||
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<CancellationAction> {
|
||||
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<DeferredWait> {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<HostResponse, String>) -> 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<codex_code_mode_protocol::host::WireRuntimeResponse, String>,
|
||||
) -> 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
|
||||
}
|
||||
}
|
||||
@@ -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<dyn CodeModeSessionDelegate>,
|
||||
}
|
||||
|
||||
pub(super) struct DelegateTarget {
|
||||
pub(super) session_id: SessionId,
|
||||
pub(super) cell_id: CellId,
|
||||
pub(super) delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
}
|
||||
|
||||
pub(super) struct FailedSession {
|
||||
pub(super) cleanup: SessionCleanup,
|
||||
pub(super) cells: Vec<CellOwner>,
|
||||
}
|
||||
|
||||
pub(super) enum CellAdmissionError {
|
||||
MissingSession,
|
||||
DuplicateCell,
|
||||
}
|
||||
|
||||
struct SessionRecord {
|
||||
remote: RemoteSession,
|
||||
cleanup: SessionCleanup,
|
||||
phase: SessionPhase,
|
||||
cells: HashMap<WireCellId, CellOwner>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
enum SessionPhase {
|
||||
Ready,
|
||||
Closing,
|
||||
}
|
||||
|
||||
pub(super) struct SessionRegistry {
|
||||
records: HashMap<SessionId, SessionRecord>,
|
||||
}
|
||||
|
||||
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<bool> {
|
||||
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<bool> {
|
||||
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<dyn CodeModeSessionDelegate>,
|
||||
) -> Result<CellId, CellAdmissionError> {
|
||||
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<DelegateTarget, String> {
|
||||
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<CellOwner, String> {
|
||||
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<CellOwner> {
|
||||
let Some(session) = self.records.remove(session_id) else {
|
||||
return Vec::new();
|
||||
};
|
||||
session.cells.into_values().collect()
|
||||
}
|
||||
|
||||
pub(super) fn drain(&mut self) -> Vec<FailedSession> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -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<Result<(), String>>,
|
||||
},
|
||||
Execute {
|
||||
session: RemoteSession,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
request: WireExecuteRequest,
|
||||
caller_cancellation: CancellationToken,
|
||||
yield_signal: Option<CancellationToken>,
|
||||
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
|
||||
},
|
||||
Wait {
|
||||
session: RemoteSession,
|
||||
request: WaitRequest,
|
||||
caller_cancellation: CancellationToken,
|
||||
yield_signal: Option<CancellationToken>,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
},
|
||||
Terminate {
|
||||
session: RemoteSession,
|
||||
request: TerminateRequest,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
},
|
||||
ShutdownSession {
|
||||
session: RemoteSession,
|
||||
response_tx: oneshot::Sender<Result<(), String>>,
|
||||
},
|
||||
}
|
||||
|
||||
pub(in crate::remote_session::connection) enum DriverEvent {
|
||||
HostMessage(HostToClient),
|
||||
DelegateCompleted {
|
||||
id: DelegateRequestId,
|
||||
result: Result<DelegateResponse, String>,
|
||||
},
|
||||
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<DriverEvent>) {
|
||||
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<CancellationToken>,
|
||||
watcher: Option<AbortOnDropHandle<()>>,
|
||||
}
|
||||
|
||||
impl ObservationYield {
|
||||
pub(super) fn new(signal: Option<CancellationToken>) -> Self {
|
||||
Self {
|
||||
signal,
|
||||
watcher: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn spawn_watcher(&mut self, id: RequestId, event_tx: mpsc::Sender<DriverEvent>) {
|
||||
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<Result<RuntimeResponse, String>>,
|
||||
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<Result<(), String>>,
|
||||
},
|
||||
Execute {
|
||||
session: RemoteSession,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
|
||||
initial_response_tx: oneshot::Sender<Result<RuntimeResponse, String>>,
|
||||
initial_response_rx: oneshot::Receiver<Result<RuntimeResponse, String>>,
|
||||
cancellation: CancellableRequest,
|
||||
yield_observation: ObservationYield,
|
||||
},
|
||||
Wait {
|
||||
session: RemoteSession,
|
||||
cell_id: WireCellId,
|
||||
cancellation: CancellableRequest,
|
||||
yield_observation: ObservationYield,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
},
|
||||
Terminate {
|
||||
session: RemoteSession,
|
||||
cell_id: WireCellId,
|
||||
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
},
|
||||
ShutdownSession {
|
||||
session: RemoteSession,
|
||||
response_tx: oneshot::Sender<Result<(), String>>,
|
||||
},
|
||||
}
|
||||
|
||||
pub(super) struct DeferredWait {
|
||||
pub(super) session: RemoteSession,
|
||||
pub(super) request: WireWaitRequest,
|
||||
pub(super) max_output_tokens: Option<usize>,
|
||||
pub(super) caller_cancellation: CancellationToken,
|
||||
pub(super) yield_signal: Option<CancellationToken>,
|
||||
pub(super) response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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<ChildStdout>,
|
||||
events: mpsc::Sender<DriverEvent>,
|
||||
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())?;
|
||||
}
|
||||
}
|
||||
@@ -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<dyn CodeModeSessionDelegate>,
|
||||
pub(super) trace: Option<W3cTraceContext>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user