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
+6 -4
View File
@@ -9,6 +9,7 @@ use codex_core::config::Config;
use codex_feedback::CodexFeedback;
use codex_state::LogWriteFailureReporter;
use crate::outgoing_message::NotificationTarget;
use crate::outgoing_message::OutgoingMessageSender;
const LOG_WRITE_WARNING_WITH_FEEDBACK: &str = "Codex couldn't save diagnostic logs to its local database. Use /feedback with logs included before closing Codex, or run `codex doctor` for diagnostics.";
@@ -45,12 +46,13 @@ impl LogWriteWarningReporter {
if !self.has_failure.swap(true, Ordering::Relaxed)
&& let Some(outgoing) = self.outgoing.upgrade()
{
outgoing.try_send_server_notification(ServerNotification::Warning(
WarningNotification {
outgoing.try_send_server_notification(
NotificationTarget::Broadcast,
ServerNotification::Warning(WarningNotification {
thread_id: None,
message: self.message.to_string(),
},
));
}),
);
}
}
}
+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
@@ -139,6 +139,7 @@ pub(crate) fn without_notification_media(notification: ServerNotification) -> Se
| ServerNotification::WindowsWorldWritableWarning(_)
| ServerNotification::WindowsSandboxSetupCompleted(_)
| ServerNotification::AccountLoginCompleted(_)
| ServerNotification::ThreadSearchPartial(_)
| ServerNotification::GatewayOAuthChanged(_) => notification,
}
}
+135 -6
View File
@@ -78,6 +78,8 @@ enum RequestCancellationScope {
Unavailable,
UserVerification,
ThreadSample,
ThreadSearch,
ThreadSearchStream,
}
impl RequestContext {
@@ -100,6 +102,8 @@ impl RequestContext {
| "userVerification/delete"
| "userVerification/verify" => RequestCancellationScope::UserVerification,
"thread/sample" => RequestCancellationScope::ThreadSample,
"thread/search" => RequestCancellationScope::ThreadSearch,
"thread/search/stream" => RequestCancellationScope::ThreadSearchStream,
_ => RequestCancellationScope::Unavailable,
},
span,
@@ -133,6 +137,11 @@ pub(crate) enum OutgoingEnvelope {
},
}
pub(crate) enum NotificationTarget {
Broadcast,
Connection(ConnectionId),
}
/// Sends messages to the client and manages request callbacks.
pub(crate) struct OutgoingMessageSender {
verification_auth: OnceLock<Arc<codex_login::AuthManager>>,
@@ -319,6 +328,25 @@ impl OutgoingMessageSender {
}
}
/// Capture the exact target at ordered ingress; cancellation must not resolve a reused ID later.
pub(crate) async fn search_cancellation_target(
&self,
request_id: &ConnectionRequestId,
streaming: bool,
) -> Option<tokio_util::sync::CancellationToken> {
let scope = if streaming {
RequestCancellationScope::ThreadSearchStream
} else {
RequestCancellationScope::ThreadSearch
};
self.request_contexts
.lock()
.await
.get(request_id)
.filter(|context| context.cancellation_scope == scope)
.map(|context| context.cancellation.clone())
}
pub(crate) async fn enable_human_approval_connection(&self, connection_id: ConnectionId) {
self.human_approval_connections
.lock()
@@ -334,7 +362,19 @@ impl OutgoingMessageSender {
self.disconnect_user_verification_connection(connection_id)
.await;
let mut request_contexts = self.request_contexts.lock().await;
request_contexts.retain(|request_id, _| request_id.connection_id != connection_id);
request_contexts.retain(|request_id, context| {
if request_id.connection_id != connection_id {
return true;
}
if matches!(
context.cancellation_scope,
RequestCancellationScope::ThreadSearch
| RequestCancellationScope::ThreadSearchStream
) {
context.cancellation.cancel();
}
false
});
}
pub(crate) async fn request_trace_context(
@@ -858,12 +898,35 @@ impl OutgoingMessageSender {
.await;
}
pub(crate) fn try_send_server_notification(&self, notification: ServerNotification) {
if let Err(error) = self.sender.try_send(OutgoingEnvelope::Broadcast {
message: timestamped_server_notification(notification),
}) {
warn!("failed to queue server notification to clients: {error}");
/// Queue an optional notification without delaying the request or other connections.
pub(crate) fn try_send_server_notification(
&self,
target: NotificationTarget,
notification: ServerNotification,
) -> bool {
let permit = match self.sender.try_reserve() {
Ok(permit) => permit,
Err(error) => {
if matches!(target, NotificationTarget::Broadcast) {
warn!("failed to queue server notification to clients: {error}");
}
return false;
}
};
// Leave room for the authoritative response when progress fills the queue.
if matches!(target, NotificationTarget::Connection(_)) && self.sender.capacity() == 0 {
return false;
}
let message = timestamped_server_notification(notification);
permit.send(match target {
NotificationTarget::Broadcast => OutgoingEnvelope::Broadcast { message },
NotificationTarget::Connection(connection_id) => OutgoingEnvelope::ToConnection {
connection_id,
message,
write_complete_tx: None,
},
});
true
}
pub(crate) async fn send_server_notification_to_connections(
@@ -1410,6 +1473,72 @@ mod tests {
}
}
/// Search cancellation is scoped to the owning connection, including on disconnect.
#[tokio::test]
async fn thread_search_cancellation_is_connection_scoped() {
let (tx, _rx) = mpsc::channel::<OutgoingEnvelope>(4);
let outgoing =
OutgoingMessageSender::new(tx, codex_analytics::AnalyticsEventsClient::disabled());
let request = |connection_id| ConnectionRequestId {
connection_id: ConnectionId(connection_id),
request_id: RequestId::String("bf76b226-4c49-4394-a28c-3ff976abb756".to_string()),
};
let first = RequestContext::new(
request(1),
"thread/search",
Span::none(),
/*parent_trace*/ None,
);
let second = RequestContext::new(
request(2),
"thread/search/stream",
Span::none(),
/*parent_trace*/ None,
);
outgoing.register_request_context(first.clone()).await;
outgoing.register_request_context(second.clone()).await;
assert!(
outgoing
.search_cancellation_target(&request(2), /*streaming*/ false)
.await
.is_none()
);
assert!(
outgoing
.search_cancellation_target(&request(1), /*streaming*/ true)
.await
.is_none()
);
assert!(!first.cancellation.is_cancelled());
assert!(!second.cancellation.is_cancelled());
outgoing
.search_cancellation_target(&request(2), /*streaming*/ true)
.await
.expect("stream target")
.cancel();
assert!(second.cancellation.is_cancelled());
assert!(!first.cancellation.is_cancelled());
let captured = outgoing
.search_cancellation_target(&request(1), /*streaming*/ false)
.await
.expect("original target");
let replacement = RequestContext::new(
request(1),
"thread/search",
Span::none(),
/*parent_trace*/ None,
);
outgoing.register_request_context(replacement.clone()).await;
captured.cancel();
assert!(first.cancellation.is_cancelled());
assert!(!replacement.cancellation.is_cancelled());
outgoing.connection_closed(ConnectionId(1)).await;
assert!(replacement.cancellation.is_cancelled());
}
#[tokio::test]
async fn send_response_clears_registered_request_context() {
let (tx, _rx) = mpsc::channel::<OutgoingEnvelope>(4);
@@ -604,6 +604,7 @@ pub(crate) use search::SearchRequestProcessor;
pub(crate) use thread_goal_processor::ThreadGoalRequestProcessor;
pub(crate) use thread_processor::ThreadRequestProcessor;
pub(crate) use thread_processor::ThreadResumeTarget;
pub(crate) use thread_processor::ThreadSearchDelivery;
pub(crate) use thread_queue_processor::ThreadQueueRequestProcessor;
pub(crate) use turn_processor::TurnRequestProcessor;
pub(crate) use windows_sandbox_processor::WindowsSandboxRequestProcessor;
@@ -40,6 +40,7 @@ use codex_app_server_protocol::ThreadRevertResponse;
use codex_app_server_protocol::ThreadRevertedNotification;
use codex_app_server_protocol::ThreadSampleParams;
use codex_app_server_protocol::ThreadSampleResponse;
use codex_app_server_protocol::ThreadSearchPartialNotification;
use codex_app_server_protocol::ThreadSection;
use codex_app_server_protocol::ThreadSectionAppearance;
use codex_app_server_protocol::ThreadSectionChangedNotification;
@@ -57,7 +58,11 @@ use codex_protocol::protocol::ThreadHistoryMode;
use codex_thread_store::LocalHistoryAccount;
use codex_thread_store::PendingDaybreakPreference;
use codex_thread_store::PersistContext;
use codex_thread_store::StoredThreadSearchResult;
use codex_thread_store::ThreadSearchObserver;
use codex_thread_store::ThreadSearchSourceKind;
use std::ops::ControlFlow;
use tokio::sync::mpsc;
pub(super) const THREAD_LIST_DEFAULT_LIMIT: usize = 25;
pub(super) const THREAD_LIST_MAX_LIMIT: usize = 100;
@@ -65,6 +70,14 @@ const THREAD_LIST_MAX_FILTER_IDS: usize = 100;
const PAGINATED_FULL_HISTORY_DEPRECATION_SUMMARY: &str = "Full-history hydration is deprecated for paginated threads; use `excludeTurns: true`, then page with `thread/turns/list` and `thread/items/list`.";
const PAGINATED_THREAD_READ_DEPRECATION_SUMMARY: &str = "Full-history hydration is deprecated for paginated threads; omit `includeTurns` or set it to `false`, then page with `thread/turns/list` and `thread/items/list`.";
const MAX_STREAM_SEARCH_TERM_BYTES: usize = 64 * 1024;
#[derive(Clone, Copy)]
pub(crate) enum ThreadSearchDelivery {
FinalOnly,
Streaming,
}
async fn stage_pending_thread_metadata(
thread_manager: &ThreadManager,
thread_store: &dyn ThreadStore,
@@ -967,10 +980,12 @@ impl ThreadRequestProcessor {
pub(crate) async fn thread_search(
&self,
params: ThreadSearchParams,
) -> Result<Option<ClientResponsePayload>, JSONRPCErrorError> {
self.thread_search_response_inner(params)
request_id: ConnectionRequestId,
cancellation: CancellationToken,
delivery: ThreadSearchDelivery,
) -> Result<ThreadSearchResponse, JSONRPCErrorError> {
self.thread_search_response_inner(params, request_id, cancellation, delivery)
.await
.map(|response| Some(response.into()))
}
pub(crate) async fn thread_search_occurrences(
@@ -2940,6 +2955,9 @@ impl ThreadRequestProcessor {
async fn thread_search_response_inner(
&self,
params: ThreadSearchParams,
request_id: ConnectionRequestId,
cancellation: CancellationToken,
delivery: ThreadSearchDelivery,
) -> Result<ThreadSearchResponse, JSONRPCErrorError> {
let ThreadSearchParams {
include_attached_threads,
@@ -2956,6 +2974,13 @@ impl ThreadRequestProcessor {
let search_term = (!search_term.is_empty())
.then_some(search_term)
.ok_or_else(|| invalid_request("thread/search requires a non-empty searchTerm"))?;
if matches!(delivery, ThreadSearchDelivery::Streaming)
&& search_term.len() > MAX_STREAM_SEARCH_TERM_BYTES
{
return Err(invalid_params(format!(
"thread/search/stream searchTerm exceeds {MAX_STREAM_SEARCH_TERM_BYTES} UTF-8 bytes"
)));
}
let requested_page_size = limit
.map(|value| value as usize)
.unwrap_or(THREAD_LIST_DEFAULT_LIMIT)
@@ -2967,66 +2992,117 @@ impl ThreadRequestProcessor {
};
let store_sort_direction = sort_direction.unwrap_or(SortDirection::Desc);
let (allowed_sources, source_kind_filter) = compute_source_filters(source_kinds);
let mut cursor_obj = cursor;
let mut last_cursor = cursor_obj.clone();
let mut remaining = requested_page_size;
let account = self.local_history_account(current_account_only);
let mut search_results = Vec::with_capacity(requested_page_size);
let mut next_cursor = None;
while remaining > 0 {
let page = self
.thread_store
.search_threads(StoreSearchThreadsParams {
include_attached_threads,
account: account.clone(),
page_size: remaining.min(THREAD_LIST_MAX_LIMIT),
cursor: cursor_obj.clone(),
sort_key: store_sort_key,
sort_direction: match store_sort_direction {
SortDirection::Asc => StoreSortDirection::Asc,
SortDirection::Desc => StoreSortDirection::Desc,
},
allowed_sources: allowed_sources.clone(),
archived: archived.unwrap_or(false),
search_term: search_term.clone(),
let source_kinds = source_kind_filter.map(|kinds| {
kinds
.into_iter()
.map(|kind| match kind {
ThreadSourceKind::Cli => ThreadSearchSourceKind::Cli,
ThreadSourceKind::VsCode => ThreadSearchSourceKind::VsCode,
ThreadSourceKind::Exec => ThreadSearchSourceKind::Exec,
ThreadSourceKind::AppServer => ThreadSearchSourceKind::AppServer,
ThreadSourceKind::SubAgent => ThreadSearchSourceKind::SubAgent,
ThreadSourceKind::SubAgentReview => ThreadSearchSourceKind::SubAgentReview,
ThreadSourceKind::SubAgentCompact => ThreadSearchSourceKind::SubAgentCompact,
ThreadSourceKind::SubAgentThreadSpawn => {
ThreadSearchSourceKind::SubAgentThreadSpawn
}
ThreadSourceKind::SubAgentOther => ThreadSearchSourceKind::SubAgentOther,
ThreadSourceKind::Unknown => ThreadSearchSourceKind::Unknown,
})
.await
.map_err(thread_store_list_error)?;
for result in page.items {
let source = with_thread_spawn_agent_metadata(
result.thread.source.clone(),
result.thread.agent_nickname.clone(),
result.thread.agent_role.clone(),
);
if source_kind_filter
.as_ref()
.is_none_or(|filter| source_kind_matches(&source, filter))
{
search_results.push(result);
if search_results.len() >= requested_page_size {
break;
.collect()
});
let account = self.local_history_account(current_account_only);
let (progress, mut batches) = if matches!(delivery, ThreadSearchDelivery::Streaming) {
let (sender, receiver) = mpsc::channel(2);
(Some(sender), Some(receiver))
} else {
(None, None)
};
let search = self.thread_store.search_threads_observed(
StoreSearchThreadsParams {
include_attached_threads,
account,
page_size: requested_page_size,
cursor,
sort_key: store_sort_key,
sort_direction: match store_sort_direction {
SortDirection::Asc => StoreSortDirection::Asc,
SortDirection::Desc => StoreSortDirection::Desc,
},
allowed_sources,
source_kinds,
archived: archived.unwrap_or(false),
search_term,
},
ThreadSearchObserver {
progress,
cancelled: Box::pin(cancellation.clone().cancelled_owned()),
},
);
tokio::pin!(search);
let mut emitted = HashSet::new();
let page = loop {
tokio::select! {
biased;
page = &mut search => break page,
batch = async {
match &mut batches {
Some(receiver) => receiver.recv().await,
None => std::future::pending().await,
}
} => {
let Some(batch) = batch else {
batches = None;
continue;
};
let mut batch_ids = HashSet::new();
let mut accepted = Vec::new();
for result in batch {
let id = result.thread.thread_id;
if !emitted.contains(&id) && batch_ids.insert(id) {
accepted.push(result);
if accepted.len() == requested_page_size.saturating_sub(emitted.len()) {
break;
}
}
}
let batch = accepted;
if batch.is_empty() {
continue;
}
let data = tokio::select! {
biased;
page = &mut search => break page,
data = self.thread_search_provisional_rows(batch) => data,
};
if self.outgoing.try_send_server_notification(
crate::outgoing_message::NotificationTarget::Connection(request_id.connection_id),
ServerNotification::ThreadSearchPartial(ThreadSearchPartialNotification {
request_id: request_id.request_id.clone(),
data,
}),
) {
emitted.extend(batch_ids);
}
if emitted.len() >= requested_page_size {
batches = None;
}
}
}
remaining = requested_page_size.saturating_sub(search_results.len());
next_cursor = page.next_cursor;
if remaining == 0 {
break;
}
let Some(cursor_val) = next_cursor.clone() else {
break;
};
if last_cursor.as_ref() == Some(&cursor_val) {
next_cursor = None;
break;
}
last_cursor = Some(cursor_val.clone());
cursor_obj = Some(cursor_val);
};
if cancellation.is_cancelled() {
return Err(invalid_request("thread search cancelled"));
}
let page = page.map_err(|err| match err {
ThreadStoreError::InvalidRequest { message }
if matches!(delivery, ThreadSearchDelivery::Streaming) =>
{
invalid_params(message)
}
err => thread_store_list_error(err),
})?;
let search_results = page.items;
let next_cursor = page.next_cursor;
let backwards_cursor = search_results.first().and_then(|result| {
thread_backwards_cursor_for_sort_key(
@@ -3035,12 +3111,27 @@ impl ThreadRequestProcessor {
store_sort_direction,
)
});
let fallback_provider = self.config.model_provider_id.clone();
let data = tokio::select! {
biased;
() = cancellation.cancelled() => return Err(invalid_request("thread search cancelled")),
data = self.thread_search_rows(search_results) => data,
};
Ok(ThreadSearchResponse {
data,
next_cursor,
backwards_cursor,
})
}
fn thread_search_stored_rows(
&self,
search_results: Vec<StoredThreadSearchResult>,
) -> Vec<ThreadSearchResult> {
let mut data = Vec::with_capacity(search_results.len());
for result in search_results {
let (thread, _) = thread_from_stored_thread(
result.thread,
fallback_provider.as_str(),
&self.config.model_provider_id,
&self.config.cwd,
);
data.push(ThreadSearchResult {
@@ -3049,6 +3140,31 @@ impl ThreadRequestProcessor {
});
}
data
}
async fn thread_search_provisional_rows(
&self,
search_results: Vec<StoredThreadSearchResult>,
) -> Vec<ThreadSearchResult> {
let mut data = self.thread_search_stored_rows(search_results);
let statuses = self
.thread_watch_manager
.loaded_statuses_for_threads(data.iter().map(|result| result.thread.id.clone()))
.await;
for result in &mut data {
if let Some(status) = statuses.get(&result.thread.id) {
result.thread.status = status.clone();
}
}
data
}
async fn thread_search_rows(
&self,
search_results: Vec<StoredThreadSearchResult>,
) -> Vec<ThreadSearchResult> {
let mut data = self.thread_search_stored_rows(search_results);
enrich_loaded_threads(
&self.thread_manager,
&self.thread_watch_manager,
@@ -3057,11 +3173,7 @@ impl ThreadRequestProcessor {
)
.await;
Ok(ThreadSearchResponse {
data,
next_cursor,
backwards_cursor,
})
data
}
async fn thread_loaded_list_response_inner(
+16 -1
View File
@@ -1,6 +1,7 @@
use crate::message_processor::ConnectionSessionState;
use crate::outgoing_message::OutgoingEnvelope;
use codex_app_server_protocol::ExperimentalApi;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ServerRequest;
use std::collections::HashMap;
use std::collections::HashSet;
@@ -154,11 +155,25 @@ async fn send_message_to_connection(
}
let writer = connection_state.writer.clone();
let optional_progress = matches!(
&message,
OutgoingMessage::AppServerNotification(envelope)
if matches!(&envelope.notification, ServerNotification::ThreadSearchPartial(_))
);
let queued_message = QueuedOutgoingMessage {
message,
write_complete_tx,
};
if connection_state.can_disconnect() {
if optional_progress {
// Keep room for the required response that follows optional progress.
if writer.capacity() <= 1 {
return false;
}
if let Err(mpsc::error::TrySendError::Closed(_)) = writer.try_send(queued_message) {
return disconnect_connection(connections, connection_id);
}
false
} else if connection_state.can_disconnect() {
match writer.try_send(queued_message) {
Ok(()) => false,
Err(mpsc::error::TrySendError::Full(_)) => {
@@ -5,6 +5,7 @@ use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ServerNotificationEnvelope;
use codex_app_server_protocol::ThreadRealtimeStartedNotification;
use codex_app_server_protocol::ThreadSearchPartialNotification;
use codex_protocol::protocol::RealtimeConversationVersion;
use codex_utils_absolute_path::AbsolutePathBuf;
use pretty_assertions::assert_eq;
@@ -31,6 +32,50 @@ fn app_server_notification(notification: ServerNotification) -> OutgoingMessage
})
}
#[tokio::test]
async fn optional_search_progress_does_not_disconnect_a_slow_connection() {
let connection_id = ConnectionId(7);
let (writer_tx, mut writer_rx) = mpsc::channel(1);
let disconnect = CancellationToken::new();
let mut connections = HashMap::from([(
connection_id,
OutboundConnectionState::new(
writer_tx.clone(),
Arc::new(AtomicBool::new(true)),
Arc::new(AtomicBool::new(true)),
Arc::new(RwLock::new(HashSet::new())),
Some(disconnect.clone()),
),
)]);
route_outgoing_envelope(
&mut connections,
OutgoingEnvelope::ToConnection {
connection_id,
message: app_server_notification(ServerNotification::ThreadSearchPartial(
ThreadSearchPartialNotification {
request_id: RequestId::Integer(4),
data: Vec::new(),
},
)),
write_complete_tx: None,
},
)
.await;
route_outgoing_envelope(
&mut connections,
OutgoingEnvelope::ToConnection {
connection_id,
message: app_server_notification(thread_realtime_started_notification()),
write_complete_tx: None,
},
)
.await;
assert!(!disconnect.is_cancelled());
assert!(connections.contains_key(&connection_id));
assert!(writer_rx.try_recv().is_ok());
assert!(writer_rx.try_recv().is_err());
}
#[tokio::test]
async fn to_connection_notification_respects_opt_out_filters() {
let connection_id = ConnectionId(7);