File size: 9,967 Bytes
afa0cbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use std::sync::PoisonError;
use std::sync::atomic::Ordering;
use std::time::Duration;
use codex_code_mode_protocol::CellId;
use codex_code_mode_protocol::grpc;
use codex_protocol::protocol::W3cTraceContext;
use futures::FutureExt;
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use tracing::warn;
use super::SessionInner;
use super::completion;
use super::conversion;
use super::deadline;
use super::state::CallbackAdmission;
impl SessionInner {
pub(super) fn spawn_session_events(
self: &Arc<Self>,
events: tonic::Streaming<grpc::SessionEvent>,
) {
self.spawn_stream(events, "session lease", Self::handle_session_event);
}
pub(super) fn spawn_tool_subscription(
self: &Arc<Self>,
calls: tonic::Streaming<grpc::ToolCall>,
) {
self.spawn_stream(calls, "tool subscription", Self::handle_tool_call);
}
fn spawn_stream<T: Send + 'static>(
self: &Arc<Self>,
mut stream: tonic::Streaming<T>,
stream_name: &'static str,
handle: fn(&Arc<Self>, T) -> Result<(), String>,
) {
let inner = Arc::clone(self);
self.stream_tasks.spawn(async move {
loop {
let message = tokio::select! {
biased;
_ = inner.stopped.cancelled() => return,
message = stream.message() => message,
};
match message {
Ok(Some(message)) => {
if let Err(error) = handle(&inner, message) {
inner.fail(error);
return;
}
}
Ok(None) => {
if !inner.shutdown_requested.load(Ordering::Acquire) {
inner.fail(format!("gRPC code-mode {stream_name} closed unexpectedly"));
}
return;
}
Err(error) => {
if !inner.shutdown_requested.load(Ordering::Acquire) {
inner.fail(deadline::failure(stream_name, error));
}
return;
}
}
}
});
}
fn handle_session_event(self: &Arc<Self>, event: grpc::SessionEvent) -> Result<(), String> {
match event
.event
.ok_or_else(|| "gRPC code-mode host sent an empty session event".to_string())?
{
grpc::session_event::Event::Opened(_) => {
Err("gRPC code-mode host repeated the session opening event".to_string())
}
grpc::session_event::Event::ToolCallCancelled(cancelled) => {
self.state
.lock()
.unwrap_or_else(PoisonError::into_inner)
.cancel_invocation(&cancelled.invocation_id)?;
Ok(())
}
grpc::session_event::Event::Notification(notification) => {
self.handle_notification(notification)
}
grpc::session_event::Event::NotificationCancelled(_) => Ok(()),
grpc::session_event::Event::CellClosed(closed) => {
let cell = self
.state
.lock()
.unwrap_or_else(PoisonError::into_inner)
.close_cell(closed)?;
self.report_closed_cell(cell);
Ok(())
}
}
}
fn handle_tool_call(self: &Arc<Self>, call: grpc::ToolCall) -> Result<(), String> {
if call.session_id != self.id {
return Err(format!(
"gRPC code-mode tool invocation belongs to session {} instead of {}",
call.session_id, self.id
));
}
let admission = self
.state
.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::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 {
Ok(cancellation) => cancellation,
Err(error) => {
let inner = Arc::clone(self);
tokio::spawn(async move {
inner
.complete_tool_call(invocation_id, CancellationToken::new(), Err(error))
.await;
});
return Ok(());
}
};
let invocation = conversion::tool_call(call);
let inner = Arc::clone(self);
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),
);
Ok(())
}
async fn complete_tool_call(
&self,
invocation_id: String,
cancellation: CancellationToken,
result: Result<serde_json::Value, String>,
) {
let request = completion::request(&self.id, &invocation_id, result);
let mut client = self.client();
tokio::select! {
biased;
_ = cancellation.cancelled() => {}
result = deadline::request(
self,
"tool invocation completion",
Duration::ZERO,
client.complete_tool_call(request),
) => {
if let Err(error) = result
&& !cancellation.is_cancelled()
&& !self.stopped.is_cancelled()
{
self.fail(error);
}
}
}
self.state
.lock()
.unwrap_or_else(PoisonError::into_inner)
.finish_invocation(&invocation_id);
}
fn handle_notification(
self: &Arc<Self>,
notification: grpc::Notification,
) -> Result<(), String> {
let admission = self
.state
.lock()
.unwrap_or_else(PoisonError::into_inner)
.admit_notification(¬ification)?;
let (cancellation, delegate) = match admission {
CallbackAdmission::Active(cancellation, delegate) => (cancellation, delegate),
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);
// 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);
});
Ok(())
}
}
|