mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Add early yielding for code-mode observations (#48123)
## What changed Add an optional `preempt` signal to `CodeModeSession::execute` and `CodeModeSession::wait` so callers can end an observation early while the cell continues running and remains available for later waits. Support the signal in the in-process runtime and across both remote transports: `operation/yield` with the negotiated `yield-observation` capability for stdio, and `YieldObservation` for gRPC. Preserve signals received before gRPC observation registration, and retain normal timeout behavior for older hosts that lack support. ## Testing Add runtime and transport tests covering early execute and wait yields followed by successful cell completion, yields before observation registration, and suppression of yield frames for older stdio hosts. GitOrigin-RevId: 3f8049d8f0582be6b8f0832cc03f2d974a0b8f18
This commit is contained in:
@@ -29,6 +29,7 @@ use codex_code_mode_protocol::host::SESSION_RESOURCE_LIMITS_CAPABILITY;
|
||||
use codex_code_mode_protocol::host::SessionId;
|
||||
use codex_code_mode_protocol::host::SupportedProtocolVersions;
|
||||
use codex_code_mode_protocol::host::WireWaitOutcome;
|
||||
use codex_code_mode_protocol::host::YIELD_OBSERVATION_CAPABILITY;
|
||||
use codex_code_mode_runtime::InProcessCodeModeSession;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncWrite;
|
||||
@@ -163,6 +164,9 @@ where
|
||||
ClientToHost::CancelRequest { id } => {
|
||||
state.cancel_request(id);
|
||||
}
|
||||
ClientToHost::YieldRequest { id } => {
|
||||
state.yield_request(id);
|
||||
}
|
||||
ClientToHost::DelegateResponse { id, result } => {
|
||||
peer.complete(id, result.into_result()).await;
|
||||
}
|
||||
@@ -253,15 +257,20 @@ async fn negotiate<R: AsyncRead + Unpin, W: AsyncWrite + Unpin>(
|
||||
return Ok(NegotiatedConnection::Rejected);
|
||||
}
|
||||
|
||||
let resource_limits_capability = Capability::new(SESSION_RESOURCE_LIMITS_CAPABILITY)?;
|
||||
let resource_limits_requested = client_hello
|
||||
.required_capabilities()
|
||||
.contains(&resource_limits_capability)
|
||||
|| client_hello
|
||||
.optional_capabilities()
|
||||
.contains(&resource_limits_capability);
|
||||
let host_capabilities =
|
||||
CapabilitySet::try_new(resource_limits_requested.then_some(resource_limits_capability))?;
|
||||
let host_capabilities = CapabilitySet::try_new(
|
||||
[
|
||||
SESSION_RESOURCE_LIMITS_CAPABILITY,
|
||||
YIELD_OBSERVATION_CAPABILITY,
|
||||
]
|
||||
.into_iter()
|
||||
.map(Capability::new)
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
.filter(|capability| {
|
||||
client_hello.required_capabilities().contains(capability)
|
||||
|| client_hello.optional_capabilities().contains(capability)
|
||||
}),
|
||||
)?;
|
||||
if let Some(capability) = client_hello
|
||||
.required_capabilities()
|
||||
.iter()
|
||||
@@ -305,7 +314,7 @@ impl HostState {
|
||||
// Include host admission and scheduling before the runtime observes the request.
|
||||
let received_at = Instant::now();
|
||||
let request_kind = RequestKind::from(&request);
|
||||
let cancellation = self
|
||||
let (cancellation, yield_signal) = self
|
||||
.requests
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
@@ -322,7 +331,7 @@ impl HostState {
|
||||
let request_task = self.request_tasks.spawn(async move {
|
||||
let _permit = permit;
|
||||
state
|
||||
.handle_request(request_id, request, cancellation, received_at)
|
||||
.handle_request(request_id, request, cancellation, yield_signal, received_at)
|
||||
.await;
|
||||
state.finish_request(request_id);
|
||||
});
|
||||
@@ -355,6 +364,7 @@ impl HostState {
|
||||
request_id: RequestId,
|
||||
request: HostRequest,
|
||||
cancellation: CancellationToken,
|
||||
yield_signal: CancellationToken,
|
||||
received_at: Instant,
|
||||
) {
|
||||
if let HostRequest::Execute { request, .. } = &request {
|
||||
@@ -416,7 +426,7 @@ impl HostState {
|
||||
session_id.clone(),
|
||||
Arc::clone(&self.peer),
|
||||
));
|
||||
let result = session.execute(request, delegate).await;
|
||||
let result = session.execute(request, delegate, Some(yield_signal)).await;
|
||||
match result {
|
||||
Ok(started) => {
|
||||
let cell_id = started.cell_id.clone();
|
||||
@@ -449,7 +459,7 @@ impl HostState {
|
||||
_ = cancellation.cancelled() => {
|
||||
Err("code-mode request cancelled".to_string())
|
||||
}
|
||||
result = session.wait(request.into()) => result.and_then(|outcome| {
|
||||
result = session.wait(request.into(), Some(yield_signal)) => result.and_then(|outcome| {
|
||||
let outcome = outcome.with_code_mode_host_duration(received_at.elapsed());
|
||||
Ok(HostResponse::WaitCompleted {
|
||||
outcome: WireWaitOutcome::try_from(outcome)
|
||||
@@ -557,6 +567,13 @@ impl HostState {
|
||||
.cancel(request_id);
|
||||
}
|
||||
|
||||
fn yield_request(&self, request_id: RequestId) {
|
||||
self.requests
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.yield_observation(request_id);
|
||||
}
|
||||
|
||||
fn finish_request(&self, request_id: RequestId) {
|
||||
self.requests
|
||||
.lock()
|
||||
@@ -623,6 +640,7 @@ impl RequestKind {
|
||||
struct ActiveRequest {
|
||||
kind: RequestKind,
|
||||
cancellation: CancellationToken,
|
||||
yield_signal: CancellationToken,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -637,19 +655,21 @@ impl RequestRegistry {
|
||||
&mut self,
|
||||
request_id: RequestId,
|
||||
kind: RequestKind,
|
||||
) -> Result<CancellationToken, anyhow::Error> {
|
||||
) -> Result<(CancellationToken, CancellationToken), anyhow::Error> {
|
||||
if self.active.contains_key(&request_id) || self.recent.contains(&request_id) {
|
||||
anyhow::bail!("duplicate code-mode request ID {request_id:?}");
|
||||
}
|
||||
let cancellation = CancellationToken::new();
|
||||
let yield_signal = CancellationToken::new();
|
||||
self.active.insert(
|
||||
request_id,
|
||||
ActiveRequest {
|
||||
kind,
|
||||
cancellation: cancellation.clone(),
|
||||
yield_signal: yield_signal.clone(),
|
||||
},
|
||||
);
|
||||
Ok(cancellation)
|
||||
Ok((cancellation, yield_signal))
|
||||
}
|
||||
|
||||
fn cancel(&mut self, request_id: RequestId) {
|
||||
@@ -660,6 +680,14 @@ impl RequestRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
fn yield_observation(&self, request_id: RequestId) {
|
||||
if let Some(request) = self.active.get(&request_id)
|
||||
&& matches!(request.kind, RequestKind::Execute | RequestKind::Wait)
|
||||
{
|
||||
request.yield_signal.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self, request_id: RequestId) {
|
||||
if self.active.remove(&request_id).is_none() {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user