mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Scope code mode callback delegates to individual executions (#44865)
## Why A delegate bound at session creation cannot provide different callbacks for cells sharing that session. Each execution needs to retain its own delegate across yields and release it when the cell is cleaned up. ## What changed - Pass `CodeModeSessionDelegate` to `execute` instead of session creation. - Route tool calls, notifications, and cell closure callbacks through the execution's delegate in the in-process runtime and the gRPC and stdio transports. - Retain delegates with pending executions and live cells, releasing them through closure and cancellation cleanup. ## Testing Add coverage for distinct delegates across yielded cells, gRPC callbacks before cell admission, and delegate release after completion or abandoned execution cleanup. Update transport tests to verify callbacks reach the owning cell's delegate. GitOrigin-RevId: 7469f52104b993e790a40c65fb800ad02b7fd606
This commit is contained in:
@@ -38,27 +38,25 @@ use crate::cell_actor::CompletionCommit;
|
||||
type RuntimeEventFuture = Pin<Box<dyn Future<Output = Result<CellEvent, Error>> + Send + 'static>>;
|
||||
|
||||
/// Owns all cells and shared state for one transport-neutral code-mode session.
|
||||
pub(crate) struct SessionRuntime<D: SessionRuntimeDelegate> {
|
||||
inner: Arc<Inner<D>>,
|
||||
pub(crate) struct SessionRuntime {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
struct Inner<D: SessionRuntimeDelegate> {
|
||||
struct Inner {
|
||||
stored_values: Mutex<HashMap<String, JsonValue>>,
|
||||
cells: Mutex<HashMap<CellId, CellHandle>>,
|
||||
cell_tasks: TaskTracker,
|
||||
shutdown_token: CancellationToken,
|
||||
delegate: Arc<D>,
|
||||
task_failure_handler: Option<TaskFailureHandler>,
|
||||
next_cell_id: AtomicU64,
|
||||
}
|
||||
|
||||
impl<D: SessionRuntimeDelegate> SessionRuntime<D> {
|
||||
pub(crate) fn new(delegate: Arc<D>) -> Self {
|
||||
Self::new_with_task_failure_handler(delegate, /*task_failure_handler*/ None)
|
||||
impl SessionRuntime {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self::new_with_task_failure_handler(/*task_failure_handler*/ None)
|
||||
}
|
||||
|
||||
pub(crate) fn new_with_task_failure_handler(
|
||||
delegate: Arc<D>,
|
||||
task_failure_handler: Option<TaskFailureHandler>,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -67,24 +65,24 @@ impl<D: SessionRuntimeDelegate> SessionRuntime<D> {
|
||||
cells: Mutex::new(HashMap::new()),
|
||||
cell_tasks: TaskTracker::new(),
|
||||
shutdown_token: CancellationToken::new(),
|
||||
delegate,
|
||||
task_failure_handler,
|
||||
next_cell_id: AtomicU64::new(1),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn execute(
|
||||
pub(crate) async fn execute<D: SessionRuntimeDelegate>(
|
||||
&self,
|
||||
request: CreateCellRequest,
|
||||
initial_observe_mode: ObserveMode,
|
||||
delegate: Arc<D>,
|
||||
) -> Result<StartedCell, Error> {
|
||||
if self.inner.shutdown_token.is_cancelled() {
|
||||
return Err(Error::ShuttingDown);
|
||||
}
|
||||
let cell_id = self.allocate_cell_id()?;
|
||||
let initial_event = self
|
||||
.start_cell(cell_id.clone(), request, initial_observe_mode)
|
||||
.start_cell(cell_id.clone(), request, initial_observe_mode, delegate)
|
||||
.await?;
|
||||
Ok(StartedCell {
|
||||
cell_id,
|
||||
@@ -154,14 +152,16 @@ impl<D: SessionRuntimeDelegate> SessionRuntime<D> {
|
||||
.map_err(|_| Error::CellIdSpaceExhausted)
|
||||
}
|
||||
|
||||
async fn start_cell(
|
||||
async fn start_cell<D: SessionRuntimeDelegate>(
|
||||
&self,
|
||||
cell_id: CellId,
|
||||
request: CreateCellRequest,
|
||||
initial_observe_mode: ObserveMode,
|
||||
delegate: Arc<D>,
|
||||
) -> Result<RuntimeEventFuture, Error> {
|
||||
let stored_values = self.inner.stored_values.lock().await.clone();
|
||||
let host = Arc::new(RuntimeCellHost {
|
||||
delegate,
|
||||
cell_id: cell_id.clone(),
|
||||
inner: Arc::clone(&self.inner),
|
||||
execution_context: opentelemetry::Context::current(),
|
||||
@@ -205,7 +205,7 @@ impl<D: SessionRuntimeDelegate> SessionRuntime<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: SessionRuntimeDelegate> Drop for SessionRuntime<D> {
|
||||
impl Drop for SessionRuntime {
|
||||
fn drop(&mut self) {
|
||||
self.begin_shutdown();
|
||||
}
|
||||
@@ -235,8 +235,9 @@ impl PendingEvent {
|
||||
}
|
||||
|
||||
struct RuntimeCellHost<D: SessionRuntimeDelegate> {
|
||||
delegate: Arc<D>,
|
||||
cell_id: CellId,
|
||||
inner: Arc<Inner<D>>,
|
||||
inner: Arc<Inner>,
|
||||
// Callbacks outlive the initial request and run in separate tasks. Preserve
|
||||
// their trace parent without retaining the request's tracing span.
|
||||
execution_context: opentelemetry::Context,
|
||||
@@ -248,8 +249,7 @@ impl<D: SessionRuntimeDelegate> CellHost for RuntimeCellHost<D> {
|
||||
invocation: CellToolCall,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> Result<JsonValue, String> {
|
||||
self.inner
|
||||
.delegate
|
||||
self.delegate
|
||||
.invoke_tool(
|
||||
NestedToolCall {
|
||||
cell_id: self.cell_id.clone(),
|
||||
@@ -270,8 +270,7 @@ impl<D: SessionRuntimeDelegate> CellHost for RuntimeCellHost<D> {
|
||||
text: String,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> Result<(), String> {
|
||||
self.inner
|
||||
.delegate
|
||||
self.delegate
|
||||
.notify(call_id, self.cell_id.clone(), text, cancellation_token)
|
||||
.await
|
||||
}
|
||||
@@ -298,7 +297,7 @@ impl<D: SessionRuntimeDelegate> CellHost for RuntimeCellHost<D> {
|
||||
|
||||
async fn closed(&self) {
|
||||
self.inner.cells.lock().await.remove(&self.cell_id);
|
||||
self.inner.delegate.cell_closed(&self.cell_id);
|
||||
self.delegate.cell_closed(&self.cell_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,16 +66,14 @@ impl SessionRuntimeDelegate for PanickingClosedDelegate {
|
||||
#[tokio::test]
|
||||
async fn reports_cell_actor_panics_to_the_owner() {
|
||||
let (failure_tx, mut failure_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let runtime = SessionRuntime::new_with_task_failure_handler(
|
||||
Arc::new(PanickingClosedDelegate),
|
||||
Some(Arc::new(move |reason| {
|
||||
let _ = failure_tx.send(reason);
|
||||
})),
|
||||
);
|
||||
let runtime = SessionRuntime::new_with_task_failure_handler(Some(Arc::new(move |reason| {
|
||||
let _ = failure_tx.send(reason);
|
||||
})));
|
||||
let started = runtime
|
||||
.execute(
|
||||
execute_request(r#"text("done");"#),
|
||||
ObserveMode::YieldAfter(Duration::from_secs(1)),
|
||||
Arc::new(PanickingClosedDelegate),
|
||||
)
|
||||
.await
|
||||
.expect("start cell");
|
||||
@@ -97,9 +95,10 @@ async fn reports_cell_actor_panics_to_the_owner() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn termination_rejects_a_waiting_store_commit_before_the_next_cell_can_load_it() {
|
||||
let runtime = SessionRuntime::new(Arc::new(RecordingDelegate));
|
||||
let runtime = SessionRuntime::new();
|
||||
let cell_state = Arc::new(CellState::new(CancellationToken::new()));
|
||||
let host = RuntimeCellHost {
|
||||
delegate: Arc::new(RecordingDelegate),
|
||||
cell_id: CellId::new("terminating-writer"),
|
||||
inner: Arc::clone(&runtime.inner),
|
||||
execution_context: opentelemetry::Context::new(),
|
||||
@@ -154,6 +153,7 @@ async fn termination_rejects_a_waiting_store_commit_before_the_next_cell_can_loa
|
||||
source: r#"text(String(load("candidate")));"#.to_string(),
|
||||
},
|
||||
ObserveMode::YieldAfter(Duration::from_secs(1)),
|
||||
Arc::new(RecordingDelegate),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -179,7 +179,7 @@ fn execute_request(source: &str) -> CreateCellRequest {
|
||||
|
||||
#[tokio::test]
|
||||
async fn cell_id_allocation_fails_before_wrapping() {
|
||||
let runtime = SessionRuntime::new(Arc::new(RecordingDelegate));
|
||||
let runtime = SessionRuntime::new();
|
||||
runtime
|
||||
.inner
|
||||
.next_cell_id
|
||||
@@ -190,6 +190,7 @@ async fn cell_id_allocation_fails_before_wrapping() {
|
||||
.execute(
|
||||
execute_request(r#"text("unreachable");"#),
|
||||
ObserveMode::YieldAfter(Duration::from_secs(1)),
|
||||
Arc::new(RecordingDelegate)
|
||||
)
|
||||
.await
|
||||
.err(),
|
||||
@@ -203,12 +204,13 @@ async fn cell_id_allocation_fails_before_wrapping() {
|
||||
reason = "test holds the registry lock to force admission ahead of shutdown"
|
||||
)]
|
||||
async fn shutdown_rejects_cell_admission_queued_before_the_registry_lock() {
|
||||
let runtime = Arc::new(SessionRuntime::new(Arc::new(RecordingDelegate)));
|
||||
let runtime = Arc::new(SessionRuntime::new());
|
||||
let cells = runtime.inner.cells.lock().await;
|
||||
|
||||
let execution = runtime.execute(
|
||||
execute_request("while (true) {}"),
|
||||
ObserveMode::YieldAfter(Duration::from_millis(/*millis*/ 1)),
|
||||
Arc::new(RecordingDelegate),
|
||||
);
|
||||
tokio::pin!(execution);
|
||||
std::future::poll_fn(|context| match execution.as_mut().poll(context) {
|
||||
@@ -238,11 +240,12 @@ async fn shutdown_rejects_cell_admission_queued_before_the_registry_lock() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn drop_terminates_cells_when_the_registry_is_locked() {
|
||||
let runtime = SessionRuntime::new(Arc::new(RecordingDelegate));
|
||||
let runtime = SessionRuntime::new();
|
||||
let started = runtime
|
||||
.execute(
|
||||
execute_request("while (true) {}"),
|
||||
ObserveMode::YieldAfter(Duration::from_millis(/*millis*/ 1)),
|
||||
Arc::new(RecordingDelegate),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user