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:
Abhinav
2026-09-11 17:00:07 +00:00
committed by copyberry
parent bc5957eac9
commit 3305c4f31d
30 changed files with 1027 additions and 637 deletions
+120 -103
View File
@@ -1,3 +1,4 @@
use codex_code_mode_protocol::NoopCodeModeSessionDelegate;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
@@ -43,13 +44,10 @@ fn resolve_yield_timeout_applies_grace_before_session_limits() {
(Some(10_500), 5_000, Duration::from_secs(5)),
(Some(u64::MAX), u64::MAX, Duration::from_millis(u64::MAX)),
] {
let session = InProcessCodeModeSession::with_delegate_and_limits(
Arc::new(ReleasableToolDelegate::default()),
CodeModeSessionCellExecutionLimits {
max_yield_time_ms,
max_heap_size_bytes: None,
},
);
let session = InProcessCodeModeSession::with_limits(CodeModeSessionCellExecutionLimits {
max_yield_time_ms,
max_heap_size_bytes: None,
});
assert_eq!(
session.resolve_yield_timeout(requested_yield_time_ms),
@@ -62,14 +60,14 @@ fn resolve_yield_timeout_applies_grace_before_session_limits() {
#[tokio::test(start_paused = true)]
async fn execute_waits_for_nested_tool_during_yield_grace() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = InProcessCodeModeSession::with_delegate(delegate.clone());
let service = InProcessCodeModeSession::new();
let request = ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"await tools.echo({}); text("done");"#.to_string(),
yield_time_ms: Some(10_000),
..execute_request("")
};
let started = service.execute(request).await.unwrap();
let started = service.execute(request, delegate.clone()).await.unwrap();
let response = tokio::spawn(started.initial_response());
wait_until_tool_started(&delegate).await;
tokio::time::advance(Duration::from_millis(10_500)).await;
@@ -93,20 +91,20 @@ async fn execute_waits_for_nested_tool_during_yield_grace() {
#[tokio::test(start_paused = true)]
async fn execute_and_wait_clamp_yield_grace_without_stopping_the_cell() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = InProcessCodeModeSession::with_delegate_and_limits(
delegate.clone(),
CodeModeSessionCellExecutionLimits {
max_yield_time_ms: Some(/*value*/ 10_000),
max_heap_size_bytes: None,
},
);
let service = InProcessCodeModeSession::with_limits(CodeModeSessionCellExecutionLimits {
max_yield_time_ms: Some(/*value*/ 10_000),
max_heap_size_bytes: None,
});
let started = service
.execute(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"await tools.echo({}); text("done");"#.to_string(),
yield_time_ms: None,
..execute_request("")
})
.execute(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"await tools.echo({}); text("done");"#.to_string(),
yield_time_ms: None,
..execute_request("")
},
delegate.clone(),
)
.await
.unwrap();
let initial_response = tokio::spawn(started.initial_response());
@@ -169,13 +167,16 @@ async fn execute_and_wait_clamp_yield_grace_without_stopping_the_cell() {
#[tokio::test(start_paused = true)]
async fn wait_waits_for_nested_tool_during_yield_grace() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = InProcessCodeModeSession::with_delegate(delegate.clone());
let service = InProcessCodeModeSession::new();
let initial_response = service
.execute_to_pending(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"await tools.echo({}); text("done");"#.to_string(),
..execute_request("")
})
.execute_to_pending(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"await tools.echo({}); text("done");"#.to_string(),
..execute_request("")
},
delegate.clone(),
)
.await
.unwrap();
assert_eq!(
@@ -215,29 +216,30 @@ async fn wait_waits_for_nested_tool_during_yield_grace() {
#[tokio::test(start_paused = true)]
async fn zero_yield_limit_is_immediate_and_scoped_to_its_session() {
let zero_delegate = Arc::new(ReleasableToolDelegate::default());
let zero_session = InProcessCodeModeSession::with_delegate_and_limits(
zero_delegate.clone(),
CodeModeSessionCellExecutionLimits {
max_yield_time_ms: Some(/*value*/ 0),
max_heap_size_bytes: None,
},
);
let zero_session = InProcessCodeModeSession::with_limits(CodeModeSessionCellExecutionLimits {
max_yield_time_ms: Some(/*value*/ 0),
max_heap_size_bytes: None,
});
let limited_delegate = Arc::new(ReleasableToolDelegate::default());
let limited_session = InProcessCodeModeSession::with_delegate_and_limits(
limited_delegate.clone(),
CodeModeSessionCellExecutionLimits {
let limited_session =
InProcessCodeModeSession::with_limits(CodeModeSessionCellExecutionLimits {
max_yield_time_ms: Some(/*value*/ 10),
max_heap_size_bytes: None,
},
);
});
let request = ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: "await tools.echo({});".to_string(),
yield_time_ms: Some(/*value*/ 60_000),
..execute_request("")
};
let zero_started = zero_session.execute(request.clone()).await.unwrap();
let limited_started = limited_session.execute(request).await.unwrap();
let zero_started = zero_session
.execute(request.clone(), zero_delegate.clone())
.await
.unwrap();
let limited_started = limited_session
.execute(request, limited_delegate.clone())
.await
.unwrap();
let zero_response = tokio::spawn(zero_started.initial_response());
let limited_response = tokio::spawn(limited_started.initial_response());
wait_until_tool_started(&zero_delegate).await;
@@ -372,7 +374,7 @@ fn echo_tool() -> ToolDefinition {
async fn execute(service: &InProcessCodeModeSession, request: ExecuteRequest) -> RuntimeResponse {
service
.execute(request)
.execute(request, Arc::new(NoopCodeModeSessionDelegate))
.await
.unwrap()
.initial_response()
@@ -521,10 +523,13 @@ async fn shutdown_interrupts_cpu_bound_cells() {
let service = InProcessCodeModeSession::new();
let cell = service
.execute(ExecuteRequest {
source: "while (true) {}".to_string(),
..execute_request("")
})
.execute(
ExecuteRequest {
source: "while (true) {}".to_string(),
..execute_request("")
},
Arc::new(NoopCodeModeSessionDelegate),
)
.await
.unwrap();
assert_eq!(
@@ -548,7 +553,10 @@ async fn start_cell_rejects_new_cell_after_shutdown_begins() {
service.shutdown().await.unwrap();
let error = service
.execute(execute_request("text('late');"))
.execute(
execute_request("text('late');"),
Arc::new(NoopCodeModeSessionDelegate),
)
.await
.err()
.unwrap();
@@ -561,11 +569,14 @@ async fn execute_to_pending_returns_completed_for_synchronous_results() {
let service = InProcessCodeModeSession::new();
let response = service
.execute_to_pending(ExecuteRequest {
source: r#"text("done");"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.execute_to_pending(
ExecuteRequest {
source: r#"text("done");"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
Arc::new(NoopCodeModeSessionDelegate),
)
.await
.unwrap();
@@ -588,11 +599,14 @@ async fn execute_to_pending_returns_once_the_runtime_is_quiescent() {
let response = tokio::time::timeout(
Duration::from_secs(1),
service.execute_to_pending(ExecuteRequest {
source: r#"text("before"); await new Promise(() => {});"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
}),
service.execute_to_pending(
ExecuteRequest {
source: r#"text("before"); await new Promise(() => {});"#.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
Arc::new(NoopCodeModeSessionDelegate),
),
)
.await
.unwrap()
@@ -626,18 +640,21 @@ async fn execute_to_pending_identifies_tool_calls_in_paused_frontier() {
let service = InProcessCodeModeSession::new();
let response = service
.execute_to_pending(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
.execute_to_pending(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await Promise.all([
tools.echo({ value: "first" }),
tools.echo({ value: "second" }),
]);
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
Arc::new(NoopCodeModeSessionDelegate),
)
.await
.unwrap();
@@ -667,9 +684,10 @@ async fn execute_to_pending_excludes_delayed_timeout_tool_calls_until_wait() {
let service = InProcessCodeModeSession::new();
let initial_response = service
.execute_to_pending(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
.execute_to_pending(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
setTimeout(() => {
tools.echo({ value: "delayed" });
}, 1000);
@@ -678,10 +696,12 @@ await Promise.all([
tools.echo({ value: "third" }),
]);
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
Arc::new(NoopCodeModeSessionDelegate),
)
.await
.unwrap();
@@ -730,20 +750,23 @@ await Promise.all([
#[tokio::test]
async fn wait_to_pending_returns_after_resumed_runtime_becomes_quiescent_again() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = InProcessCodeModeSession::with_delegate(delegate.clone());
let service = InProcessCodeModeSession::new();
let initial_response = service
.execute_to_pending(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
.execute_to_pending(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await tools.echo({});
text("after");
await new Promise(() => {});
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
delegate.clone(),
)
.await
.unwrap();
@@ -794,19 +817,22 @@ await new Promise(() => {});
#[tokio::test]
async fn wait_to_pending_returns_completed_after_resumed_runtime_finishes() {
let delegate = Arc::new(ReleasableToolDelegate::default());
let service = InProcessCodeModeSession::with_delegate(delegate.clone());
let service = InProcessCodeModeSession::new();
let initial_response = service
.execute_to_pending(ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
.execute_to_pending(
ExecuteRequest {
enabled_tools: vec![echo_tool()],
source: r#"
await tools.echo({});
text("done");
"#
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
})
.to_string(),
yield_time_ms: Some(60_000),
..execute_request("")
},
delegate.clone(),
)
.await
.unwrap();
@@ -1271,9 +1297,7 @@ async fn audio_helper_rejects_non_data_urls() {
async fn image_helper_accepts_raw_mcp_image_block_with_original_detail() {
let service = InProcessCodeModeSession::new();
let response = execute(
&service,
ExecuteRequest {
let response = execute(&service, ExecuteRequest {
source: r#"
image({
type: "image",
@@ -1285,8 +1309,7 @@ image({
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
})
.await;
assert_eq!(
@@ -1383,9 +1406,7 @@ image(
async fn image_helper_second_arg_overrides_raw_mcp_image_detail() {
let service = InProcessCodeModeSession::new();
let response = execute(
&service,
ExecuteRequest {
let response = execute(&service, ExecuteRequest {
source: r#"
image(
{
@@ -1400,8 +1421,7 @@ image(
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
})
.await;
assert_eq!(
@@ -1559,9 +1579,7 @@ image({
async fn image_helper_rejects_raw_mcp_result_container() {
let service = InProcessCodeModeSession::new();
let response = execute(
&service,
ExecuteRequest {
let response = execute(&service, ExecuteRequest {
source: r#"
image({
content: [
@@ -1578,8 +1596,7 @@ image({
.to_string(),
yield_time_ms: None,
..execute_request("")
},
)
})
.await;
assert_eq!(