Stream partial thread search results and support cancellation (#62132)

## Why

Clients using `thread/search` have to wait for the search to finish before
they can show any matches. A search can also outlive the query that triggered
it, such as when a user changes their task mention query.

## What changed

- Add experimental `thread/search/stream` with best-effort
  `thread/search/partial` notifications. Each notification carries the original
  `requestId` and a batch of verified matches. Clients replace provisional
  matches with the final page, which determines results, ordering, and cursors.
- Support connection-scoped cancellation through `thread/search/stream/cancel`
  and `thread/search/cancel` for their respective search methods. Disconnecting
  also stops active searches and reaps their search subprocesses.
- Cancel superseded or timed-out TUI task mention searches without delaying
  available title matches.

## Testing

Add coverage for incremental results and final-page parity, slow clients,
connection-scoped cancellation and subprocess cleanup, and TUI task search
cancellation.

GitOrigin-RevId: b58abf7a1df424f191c8ede806d1d5266d299548
This commit is contained in:
matthewdavis-oai
2026-11-17 23:46:48 +00:00
committed by copyberry
parent c90234de95
commit 3bf4e138fe
53 changed files with 5965 additions and 420 deletions
+88 -1
View File
@@ -47,6 +47,7 @@ use crate::request_processors::ThreadGoalRequestProcessor;
use crate::request_processors::ThreadQueueRequestProcessor;
use crate::request_processors::ThreadRequestProcessor;
use crate::request_processors::ThreadResumeTarget;
use crate::request_processors::ThreadSearchDelivery;
use crate::request_processors::TurnRequestProcessor;
use crate::request_processors::WindowsSandboxRequestProcessor;
use crate::request_processors::account_processor::handle_gcp_setup;
@@ -75,6 +76,8 @@ use codex_app_server_protocol::JSONRPCNotification;
use codex_app_server_protocol::JSONRPCRequest;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::ThreadSampleCancelResponse;
use codex_app_server_protocol::ThreadSearchCancelResponse;
use codex_app_server_protocol::ThreadSearchParams;
use codex_app_server_protocol::UserVerificationCancelResponse;
use codex_app_server_protocol::experimental_required_message;
use codex_arg0::Arg0DispatchPaths;
@@ -1084,6 +1087,31 @@ impl MessageProcessor {
),
_ => None,
};
let search_cancel_target = match &codex_request {
ClientRequest::ThreadSearchCancel { params, .. } => {
self.outgoing
.search_cancellation_target(
&ConnectionRequestId {
connection_id,
request_id: params.request_id.clone(),
},
/*streaming*/ false,
)
.await
}
ClientRequest::ThreadSearchStreamCancel { params, .. } => {
self.outgoing
.search_cancellation_target(
&ConnectionRequestId {
connection_id,
request_id: params.request_id.clone(),
},
/*streaming*/ true,
)
.await
}
_ => None,
};
let serialization_scope = codex_request.serialization_scope();
let error_request_id = connection_request_id.clone();
let rpc_gate = Arc::clone(&session.rpc_gate);
@@ -1112,6 +1140,7 @@ impl MessageProcessor {
request_context,
session,
event_stream_ready,
search_cancel_target,
),
))
.await;
@@ -1135,6 +1164,32 @@ impl MessageProcessor {
Ok(())
}
async fn handle_thread_search(
&self,
params: ThreadSearchParams,
request_id: ConnectionRequestId,
request_context: &RequestContext,
session: &ConnectionSessionState,
delivery: ThreadSearchDelivery,
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
let cancellation = &request_context.cancellation;
let search =
self.thread_processor
.thread_search(params, request_id, cancellation.clone(), delivery);
tokio::pin!(search);
let response = tokio::select! {
result = &mut search => result,
_ = session.disconnect.cancelled() => {
cancellation.cancel();
search.await
}
}?;
Ok(Some(match delivery {
ThreadSearchDelivery::FinalOnly => ClientResponsePayload::ThreadSearch(response),
ThreadSearchDelivery::Streaming => ClientResponsePayload::ThreadSearchStream(response),
}))
}
async fn handle_initialized_client_request(
self: Arc<Self>,
connection_request_id: ConnectionRequestId,
@@ -1142,6 +1197,7 @@ impl MessageProcessor {
request_context: RequestContext,
session: Arc<ConnectionSessionState>,
event_stream_ready: Option<McpEventStreamReady>,
search_cancel_target: Option<tokio_util::sync::CancellationToken>,
) -> Result<(), JSONRPCErrorError> {
let connection_id = connection_request_id.connection_id;
let app_server_client_name = session.app_server_client_name().map(str::to_string);
@@ -1607,7 +1663,38 @@ impl MessageProcessor {
self.project_processor.project_delete(params).await
}
ClientRequest::ThreadSearch { params, .. } => {
self.thread_processor.thread_search(params).await
self.handle_thread_search(
params,
request_id.clone(),
&request_context,
&session,
ThreadSearchDelivery::FinalOnly,
)
.await
}
ClientRequest::ThreadSearchStream { params, .. } => {
self.handle_thread_search(
params,
request_id.clone(),
&request_context,
&session,
ThreadSearchDelivery::Streaming,
)
.await
}
ClientRequest::ThreadSearchCancel { .. } => {
if let Some(target) = search_cancel_target {
target.cancel();
}
Ok(Some(ThreadSearchCancelResponse {}.into()))
}
ClientRequest::ThreadSearchStreamCancel { .. } => {
if let Some(target) = search_cancel_target {
target.cancel();
}
Ok(Some(ClientResponsePayload::ThreadSearchStreamCancel(
ThreadSearchCancelResponse {},
)))
}
ClientRequest::ThreadSearchOccurrences { params, .. } => {
self.thread_processor