mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
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:
committed by
copyberry
parent
c90234de95
commit
3bf4e138fe
@@ -213,6 +213,7 @@ pub(super) fn server_notification_thread_target(
|
||||
| ServerNotification::ExternalAgentConfigImportCompleted(_)
|
||||
| ServerNotification::DeprecationNotice(_)
|
||||
| ServerNotification::ConfigWarning(_)
|
||||
| ServerNotification::ThreadSearchPartial(_)
|
||||
| ServerNotification::FuzzyFileSearchSessionUpdated(_)
|
||||
| ServerNotification::FuzzyFileSearchSessionCompleted(_)
|
||||
| ServerNotification::CommandExecOutputDelta(_)
|
||||
|
||||
@@ -2115,6 +2115,7 @@ impl App {
|
||||
self.skill_load_warnings.startup_complete = true;
|
||||
}
|
||||
AppEvent::StartFileSearch(query) => {
|
||||
app_server.task_search.cancel();
|
||||
self.file_search.on_user_query(query.clone());
|
||||
if let Some(thread_id) = self.active_thread_id
|
||||
&& app_server.task_tools_available(thread_id)
|
||||
@@ -2130,12 +2131,12 @@ impl App {
|
||||
.map(std::path::Path::to_path_buf)
|
||||
})
|
||||
.unwrap_or_else(|| self.config.cwd.to_path_buf());
|
||||
crate::task_mentions::spawn_search(
|
||||
app_server.request_handle(),
|
||||
let handle = app_server.request_handle();
|
||||
app_server.task_search.spawn(
|
||||
handle,
|
||||
query,
|
||||
thread_id,
|
||||
cwd,
|
||||
app_server.task_search_generation(),
|
||||
self.app_event_tx.clone(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -776,6 +776,7 @@ impl App {
|
||||
thread_id: ThreadId,
|
||||
selection: Selection,
|
||||
) -> Result<()> {
|
||||
app_server.task_search.cancel();
|
||||
let Selection {
|
||||
mode,
|
||||
cached_session,
|
||||
|
||||
@@ -2438,12 +2438,12 @@ async fn external_transport_registers_dynamic_tools_and_finds_task_mentions() ->
|
||||
.await?;
|
||||
let (sender, mut events) = tokio::sync::mpsc::unbounded_channel();
|
||||
|
||||
crate::task_mentions::spawn_search(
|
||||
restarted_app_server.request_handle(),
|
||||
let handle = restarted_app_server.request_handle();
|
||||
restarted_app_server.task_search.spawn(
|
||||
handle,
|
||||
"bbd".to_string(),
|
||||
startup.session.thread_id,
|
||||
app.config.cwd.to_path_buf(),
|
||||
restarted_app_server.task_search_generation(),
|
||||
crate::app_event_sender::AppEventSender::new(sender),
|
||||
);
|
||||
|
||||
|
||||
@@ -125,6 +125,7 @@ impl App {
|
||||
app_server: &mut AppServerSession,
|
||||
destination: Option<ThreadId>,
|
||||
) {
|
||||
app_server.task_search.cancel();
|
||||
let thread_ids: Vec<_> = self
|
||||
.thread_event_channels
|
||||
.iter()
|
||||
|
||||
@@ -177,7 +177,6 @@ use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
use tokio::sync::Semaphore;
|
||||
@@ -410,7 +409,7 @@ pub(crate) struct AppServerSession {
|
||||
pub(crate) unread_revisions: HashMap<ThreadId, String>,
|
||||
task_tool_threads: HashSet<ThreadId>,
|
||||
task_tool_capabilities_dir: Option<AbsolutePathBuf>,
|
||||
task_search_generation: Arc<AtomicU64>,
|
||||
pub(crate) task_search: crate::task_mentions::TaskSearch,
|
||||
remote_cwd_override: Option<PathBuf>,
|
||||
thread_params_mode: ThreadParamsMode,
|
||||
/// Explicit CLI provider selection, distinct from resolved configuration defaults.
|
||||
@@ -663,7 +662,7 @@ impl AppServerSession {
|
||||
unread_revisions: HashMap::new(),
|
||||
task_tool_threads: HashSet::new(),
|
||||
task_tool_capabilities_dir: None,
|
||||
task_search_generation: Arc::new(AtomicU64::new(0)),
|
||||
task_search: crate::task_mentions::TaskSearch::default(),
|
||||
remote_cwd_override: None,
|
||||
thread_params_mode,
|
||||
model_provider_override: None,
|
||||
@@ -897,10 +896,6 @@ impl AppServerSession {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn task_search_generation(&self) -> Arc<AtomicU64> {
|
||||
Arc::clone(&self.task_search_generation)
|
||||
}
|
||||
|
||||
pub(crate) fn codex_home_path(
|
||||
&self,
|
||||
local_codex_home: &AbsolutePathBuf,
|
||||
@@ -2356,7 +2351,8 @@ impl AppServerSession {
|
||||
client.resolve_server_request(request_id, result).await
|
||||
}
|
||||
|
||||
pub(crate) async fn shutdown(self) -> std::io::Result<()> {
|
||||
pub(crate) async fn shutdown(mut self) -> std::io::Result<()> {
|
||||
self.task_search.cancel();
|
||||
match self.connection_owner {
|
||||
Some(client) => client.shutdown().await,
|
||||
None => Ok(()),
|
||||
|
||||
@@ -83,7 +83,8 @@
|
||||
//! the Chinese closing book-title bracket (`》`), takes the place of a newly inserted separator.
|
||||
//! Existing whitespace stays part of the draft.
|
||||
//!
|
||||
//! `Esc` records the active token as dismissed. A completed value that begins with `@` or `$` is
|
||||
//! `Esc` records the active token as dismissed and resets any pending unified mention search.
|
||||
//! A completed value that begins with `@` or `$` is
|
||||
//! also re-dismissed before popup synchronization, because separator affinity can still identify
|
||||
//! the completed token to the left of the cursor. Synchronization keeps the popup hidden only while
|
||||
//! the query, complete token text, and ordinal among matching occurrences remain the same.
|
||||
@@ -2514,6 +2515,10 @@ impl ChatComposer {
|
||||
self.popups.dismissed_mention_token =
|
||||
Some(DismissedToken::new(&self.draft.textarea, range, query));
|
||||
}
|
||||
if self.popups.current_file_query.take().is_some() {
|
||||
self.app_event_tx
|
||||
.send(AppEvent::StartFileSearch(String::new()));
|
||||
}
|
||||
self.popups.active = ActivePopup::None;
|
||||
(InputResult::None, true)
|
||||
}
|
||||
@@ -8055,6 +8060,11 @@ mod tests {
|
||||
composer.on_file_search_result("foo".to_string(), Vec::new());
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
assert!(matches!(composer.popups.active, ActivePopup::None));
|
||||
assert!(matches!(
|
||||
rx.try_recv(),
|
||||
Ok(AppEvent::StartFileSearch(query)) if query.is_empty()
|
||||
));
|
||||
assert!(composer.popups.current_file_query.is_none());
|
||||
|
||||
composer.draft.textarea.set_cursor("@foo @foo".len());
|
||||
composer.sync_popups();
|
||||
|
||||
@@ -419,6 +419,7 @@ impl ChatWidget {
|
||||
| ServerNotification::ExternalAgentConfigImportProgress(_)
|
||||
| ServerNotification::ExternalAgentConfigImportCompleted(_)
|
||||
| ServerNotification::FsChanged(_)
|
||||
| ServerNotification::ThreadSearchPartial(_)
|
||||
| ServerNotification::FuzzyFileSearchSessionUpdated(_)
|
||||
| ServerNotification::FuzzyFileSearchSessionCompleted(_)
|
||||
| ServerNotification::ThreadRealtimeItemAdded(_)
|
||||
|
||||
+182
-105
@@ -1,9 +1,6 @@
|
||||
//! Same-host task discovery and bounded task-reference prompt context.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_app_server_client::AppServerRequestHandle;
|
||||
@@ -13,6 +10,8 @@ use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::SortDirection;
|
||||
use codex_app_server_protocol::ThreadListParams;
|
||||
use codex_app_server_protocol::ThreadListResponse;
|
||||
use codex_app_server_protocol::ThreadSearchCancelParams;
|
||||
use codex_app_server_protocol::ThreadSearchCancelResponse;
|
||||
use codex_app_server_protocol::ThreadSearchParams;
|
||||
use codex_app_server_protocol::ThreadSearchResponse;
|
||||
use codex_app_server_protocol::ThreadSearchResult;
|
||||
@@ -22,6 +21,8 @@ use codex_app_server_protocol::ThreadSource;
|
||||
use codex_app_server_protocol::UserInput;
|
||||
use codex_protocol::ThreadId;
|
||||
use serde_json::json;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event_sender::AppEventSender;
|
||||
@@ -30,6 +31,8 @@ use crate::thread_label::ThreadLabelStyle;
|
||||
|
||||
const MAX_SEARCH_RESULTS: u32 = 50;
|
||||
const SEARCH_DEBOUNCE: Duration = Duration::from_millis(/*millis*/ 100);
|
||||
const SEARCH_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 10);
|
||||
const CANCEL_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 1);
|
||||
pub(crate) const MAX_REFERENCED_TASKS: usize = 16;
|
||||
pub(crate) const MAX_TASK_TITLE_CHARS: usize = 160;
|
||||
const MAX_REFERENCED_THREAD_ID_BYTES: usize = 768;
|
||||
@@ -44,104 +47,128 @@ pub(crate) struct TaskMention {
|
||||
pub(crate) snippet: String,
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_search(
|
||||
handle: AppServerRequestHandle,
|
||||
query: String,
|
||||
current_thread_id: ThreadId,
|
||||
cwd: PathBuf,
|
||||
generation: Arc<AtomicU64>,
|
||||
app_event_tx: AppEventSender,
|
||||
) {
|
||||
let label_style =
|
||||
ThreadLabelStyle::from_is_cloud(matches!(handle, AppServerRequestHandle::Cloud(_)));
|
||||
let request_generation = generation.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
if query.trim().is_empty() {
|
||||
app_event_tx.send(AppEvent::TaskSearchResult {
|
||||
thread_id: current_thread_id,
|
||||
query,
|
||||
matches: Vec::new(),
|
||||
});
|
||||
return;
|
||||
/// Owns the pending task search for a session and cancels it when replaced or dropped.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct TaskSearch {
|
||||
pending: Option<CancellationToken>,
|
||||
}
|
||||
|
||||
impl Drop for TaskSearch {
|
||||
fn drop(&mut self) {
|
||||
self.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
impl TaskSearch {
|
||||
pub(crate) fn cancel(&mut self) {
|
||||
if let Some(pending) = self.pending.take() {
|
||||
pending.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(SEARCH_DEBOUNCE).await;
|
||||
if generation.load(Ordering::Relaxed) != request_generation {
|
||||
pub(crate) fn spawn(
|
||||
&mut self,
|
||||
handle: AppServerRequestHandle,
|
||||
query: String,
|
||||
current_thread_id: ThreadId,
|
||||
cwd: PathBuf,
|
||||
app_event_tx: AppEventSender,
|
||||
) {
|
||||
let label_style =
|
||||
ThreadLabelStyle::from_is_cloud(matches!(handle, AppServerRequestHandle::Cloud(_)));
|
||||
self.cancel();
|
||||
if query.trim().is_empty() {
|
||||
app_event_tx.send(AppEvent::TaskSearchResult {
|
||||
thread_id: current_thread_id,
|
||||
query,
|
||||
matches: Vec::new(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let timeout = Duration::from_secs(/*secs*/ 10);
|
||||
let (response, titled_threads) = tokio::join!(
|
||||
tokio::time::timeout(
|
||||
timeout,
|
||||
handle.request_typed::<ThreadSearchResponse>(ClientRequest::ThreadSearch {
|
||||
request_id: RequestId::String(format!("task-search-{request_generation}")),
|
||||
params: ThreadSearchParams {
|
||||
include_attached_threads: false,
|
||||
current_account_only: true,
|
||||
cursor: None,
|
||||
limit: Some(MAX_SEARCH_RESULTS),
|
||||
sort_key: Some(ThreadSearchSortKey::RecencyAt),
|
||||
sort_direction: Some(SortDirection::Desc),
|
||||
source_kinds: Some(Vec::new()),
|
||||
archived: Some(false),
|
||||
search_term: query.trim().to_string(),
|
||||
let cancellation = CancellationToken::new();
|
||||
self.pending = Some(cancellation.clone());
|
||||
tokio::spawn(async move {
|
||||
tokio::select! {
|
||||
biased;
|
||||
() = cancellation.cancelled() => return,
|
||||
() = tokio::time::sleep(SEARCH_DEBOUNCE) => {}
|
||||
}
|
||||
|
||||
let request_id = RequestId::String(format!("task-search-{}", Uuid::new_v4()));
|
||||
let (response, titled_threads) = tokio::join!(
|
||||
search_with_cancellation(
|
||||
&handle,
|
||||
&cancellation,
|
||||
ClientRequest::ThreadSearch {
|
||||
request_id: request_id.clone(),
|
||||
params: ThreadSearchParams {
|
||||
include_attached_threads: false,
|
||||
current_account_only: true,
|
||||
cursor: None,
|
||||
limit: Some(MAX_SEARCH_RESULTS),
|
||||
sort_key: Some(ThreadSearchSortKey::RecencyAt),
|
||||
sort_direction: Some(SortDirection::Desc),
|
||||
source_kinds: Some(Vec::new()),
|
||||
archived: Some(false),
|
||||
search_term: query.trim().to_string(),
|
||||
},
|
||||
},
|
||||
})
|
||||
),
|
||||
tokio::time::timeout(
|
||||
timeout,
|
||||
handle.request_typed::<ThreadListResponse>(ClientRequest::ThreadList {
|
||||
request_id: RequestId::String(format!(
|
||||
"task-title-search-{request_generation}"
|
||||
)),
|
||||
params: ThreadListParams {
|
||||
include_attached_threads: false,
|
||||
current_account_only: true,
|
||||
included_thread_ids: None,
|
||||
excluded_thread_ids: None,
|
||||
attached_to_thread_id: None,
|
||||
originators: None,
|
||||
cursor: None,
|
||||
limit: Some(MAX_SEARCH_RESULTS),
|
||||
sort_key: Some(ThreadSortKey::UpdatedAt),
|
||||
sort_direction: Some(SortDirection::Desc),
|
||||
model_providers: Some(Vec::new()),
|
||||
source_kinds: Some(Vec::new()),
|
||||
archived: Some(false),
|
||||
section_id: None,
|
||||
project_id: None,
|
||||
cwd: None,
|
||||
use_state_db_only: true,
|
||||
search_term: None,
|
||||
parent_thread_id: None,
|
||||
ancestor_thread_id: None,
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
request_id,
|
||||
),
|
||||
async {
|
||||
tokio::select! {
|
||||
biased;
|
||||
() = cancellation.cancelled() => None,
|
||||
result = tokio::time::timeout(SEARCH_TIMEOUT,
|
||||
handle.request_typed::<ThreadListResponse>(ClientRequest::ThreadList {
|
||||
request_id: RequestId::String(format!("task-title-search-{}", Uuid::new_v4())),
|
||||
params: ThreadListParams {
|
||||
include_attached_threads: false,
|
||||
current_account_only: true,
|
||||
included_thread_ids: None,
|
||||
excluded_thread_ids: None,
|
||||
attached_to_thread_id: None,
|
||||
originators: None,
|
||||
cursor: None,
|
||||
limit: Some(MAX_SEARCH_RESULTS),
|
||||
sort_key: Some(ThreadSortKey::UpdatedAt),
|
||||
sort_direction: Some(SortDirection::Desc),
|
||||
model_providers: Some(Vec::new()),
|
||||
source_kinds: Some(Vec::new()),
|
||||
archived: Some(false),
|
||||
section_id: None,
|
||||
project_id: None,
|
||||
cwd: None,
|
||||
use_state_db_only: true,
|
||||
search_term: None,
|
||||
parent_thread_id: None,
|
||||
ancestor_thread_id: None,
|
||||
},
|
||||
})) => result.ok().and_then(Result::ok),
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if generation.load(Ordering::Relaxed) != request_generation {
|
||||
return;
|
||||
}
|
||||
if cancellation.is_cancelled() {
|
||||
return;
|
||||
}
|
||||
|
||||
let response = response.ok().and_then(Result::ok);
|
||||
let titled_threads = titled_threads.ok().and_then(Result::ok);
|
||||
if response.is_none() && titled_threads.is_none() {
|
||||
return;
|
||||
}
|
||||
let mut results = response.map(|response| response.data).unwrap_or_default();
|
||||
if let Some(response) = titled_threads {
|
||||
for thread in response.data {
|
||||
if results.iter().all(|result| result.thread.id != thread.id) {
|
||||
results.push(ThreadSearchResult {
|
||||
thread,
|
||||
snippet: String::new(),
|
||||
});
|
||||
if response.is_none() && titled_threads.is_none() {
|
||||
return;
|
||||
}
|
||||
let mut results = response.map(|response| response.data).unwrap_or_default();
|
||||
if let Some(response) = titled_threads {
|
||||
for thread in response.data {
|
||||
if results.iter().all(|result| result.thread.id != thread.id) {
|
||||
results.push(ThreadSearchResult {
|
||||
thread,
|
||||
snippet: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut matches = results
|
||||
let mut matches = results
|
||||
.into_iter()
|
||||
.filter_map(|result| {
|
||||
let thread = result.thread;
|
||||
@@ -174,22 +201,72 @@ pub(crate) fn spawn_search(
|
||||
})
|
||||
.take(MAX_SEARCH_RESULTS as usize)
|
||||
.collect::<Vec<_>>();
|
||||
let cwd = cwd.to_string_lossy();
|
||||
let windows_cwd = cwd.as_bytes().get(1) == Some(&b':') || cwd.starts_with(r"\\");
|
||||
matches.sort_by_key(|task| {
|
||||
let Some((prefix, suffix)) = task.cwd.get(..cwd.len()).zip(task.cwd.get(cwd.len()..))
|
||||
else {
|
||||
return true;
|
||||
};
|
||||
!(prefix == cwd.as_ref() || windows_cwd && prefix.eq_ignore_ascii_case(cwd.as_ref()))
|
||||
|| !(suffix.is_empty() || suffix.starts_with(['/', '\\']))
|
||||
let cwd = cwd.to_string_lossy();
|
||||
let windows_cwd = cwd.as_bytes().get(1) == Some(&b':') || cwd.starts_with(r"\\");
|
||||
matches.sort_by_key(|task| {
|
||||
let Some((prefix, suffix)) =
|
||||
task.cwd.get(..cwd.len()).zip(task.cwd.get(cwd.len()..))
|
||||
else {
|
||||
return true;
|
||||
};
|
||||
!(prefix == cwd.as_ref()
|
||||
|| windows_cwd && prefix.eq_ignore_ascii_case(cwd.as_ref()))
|
||||
|| !(suffix.is_empty() || suffix.starts_with(['/', '\\']))
|
||||
});
|
||||
if cancellation.is_cancelled() {
|
||||
return;
|
||||
}
|
||||
app_event_tx.send(AppEvent::TaskSearchResult {
|
||||
thread_id: current_thread_id,
|
||||
query,
|
||||
matches,
|
||||
});
|
||||
});
|
||||
app_event_tx.send(AppEvent::TaskSearchResult {
|
||||
thread_id: current_thread_id,
|
||||
query,
|
||||
matches,
|
||||
}
|
||||
}
|
||||
|
||||
async fn search_with_cancellation(
|
||||
handle: &AppServerRequestHandle,
|
||||
cancellation: &CancellationToken,
|
||||
request: ClientRequest,
|
||||
request_id: RequestId,
|
||||
) -> Option<ThreadSearchResponse> {
|
||||
// Native Cloud supports title listing, but does not implement thread/search or its
|
||||
// cancellation method. Avoid queuing an unsupported detached Cloud command.
|
||||
if matches!(handle, AppServerRequestHandle::Cloud(_)) {
|
||||
return None;
|
||||
}
|
||||
tokio::select! {
|
||||
biased;
|
||||
() = cancellation.cancelled() => {},
|
||||
result = tokio::time::timeout(SEARCH_TIMEOUT, handle.request_typed::<ThreadSearchResponse>(request)) => {
|
||||
if let Ok(result) = result {
|
||||
return result.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Dropping the request future cancels an in-process search through the facade. Remote
|
||||
// requests need an explicit connection-scoped cancellation message. Send it in the
|
||||
// background so a slow cancellation reply cannot delay already available title results.
|
||||
if matches!(handle, AppServerRequestHandle::Remote(_)) {
|
||||
let handle = handle.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = tokio::time::timeout(
|
||||
CANCEL_TIMEOUT,
|
||||
handle.request_typed::<ThreadSearchCancelResponse>(
|
||||
ClientRequest::ThreadSearchCancel {
|
||||
request_id: RequestId::String(format!(
|
||||
"task-search-cancel-{}",
|
||||
Uuid::new_v4()
|
||||
)),
|
||||
params: ThreadSearchCancelParams { request_id },
|
||||
},
|
||||
),
|
||||
)
|
||||
.await;
|
||||
});
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn valid_thread_path(path: &str) -> Option<&str> {
|
||||
|
||||
@@ -3,7 +3,134 @@ use crate::mention_codec::LinkedMention;
|
||||
use crate::mention_codec::decode_history_mentions_with_at_mentions;
|
||||
use crate::mention_codec::encode_history_mentions_at_elements;
|
||||
use codex_protocol::user_input::TextElement;
|
||||
use futures::SinkExt;
|
||||
use futures::StreamExt;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_task_search_cancels_without_delaying_results() {
|
||||
for timeout in [false, true] {
|
||||
check_remote_task_search_cancellation(timeout).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_remote_task_search_cancellation(timeout: bool) {
|
||||
use codex_app_server_client::RemoteAppServerClient;
|
||||
use codex_app_server_client::RemoteAppServerConnectArgs;
|
||||
use codex_app_server_client::RemoteAppServerEndpoint;
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let websocket_url = format!("ws://{}", listener.local_addr().unwrap());
|
||||
let (search_started, started) = tokio::sync::oneshot::channel();
|
||||
let (cancel_sent, cancelled) = tokio::sync::oneshot::channel();
|
||||
let rpc = tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
let mut socket = tokio_tungstenite::accept_async(stream).await.unwrap();
|
||||
let mut search_started = Some(search_started);
|
||||
let mut search_id = None;
|
||||
while let Some(Ok(Message::Text(text))) = socket.next().await {
|
||||
let request: serde_json::Value = serde_json::from_str(&text).unwrap();
|
||||
let result = match request["method"].as_str() {
|
||||
Some("initialize") => {
|
||||
json!({"userAgent":"task-search-test", "codexHome":"/unused"})
|
||||
}
|
||||
Some("thread/list") => json!({
|
||||
"data": [{
|
||||
"id": "task-123", "sessionId": "task-123", "preview": "needle",
|
||||
"name": "needle title", "ephemeral": false, "modelProvider": "openai",
|
||||
"createdAt": 0, "updatedAt": 0, "status": {"type": "notLoaded"},
|
||||
"cwd": std::env::temp_dir(), "cliVersion": "test", "source": "cli",
|
||||
"turns": []
|
||||
}],
|
||||
"nextCursor": null
|
||||
}),
|
||||
Some("thread/search") => {
|
||||
search_id = Some(request["id"].clone());
|
||||
search_started.take().unwrap().send(()).unwrap();
|
||||
continue;
|
||||
}
|
||||
Some("thread/search/cancel") => {
|
||||
assert_eq!(Some(&request["params"]["requestId"]), search_id.as_ref());
|
||||
cancel_sent.send(()).unwrap();
|
||||
// Leave cancellation unanswered to verify it cannot delay title results.
|
||||
std::future::pending::<()>().await;
|
||||
return;
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
socket
|
||||
.send(Message::Text(
|
||||
json!({"jsonrpc":"2.0", "id":request["id"], "result":result})
|
||||
.to_string()
|
||||
.into(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
panic!("remote disconnected before cancelling the search");
|
||||
});
|
||||
let remote = RemoteAppServerClient::connect(RemoteAppServerConnectArgs {
|
||||
required_human_approval: false,
|
||||
endpoint: RemoteAppServerEndpoint::WebSocket {
|
||||
websocket_url,
|
||||
auth_token: None,
|
||||
},
|
||||
client_name: "task-search-test".into(),
|
||||
client_version: "test".into(),
|
||||
experimental_api: true,
|
||||
mcp_server_openai_form_elicitation: false,
|
||||
opt_out_notification_methods: Vec::new(),
|
||||
channel_capacity: 8,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let handle = AppServerRequestHandle::Remote(remote.request_handle());
|
||||
let (tx, mut events) = tokio::sync::mpsc::unbounded_channel();
|
||||
let current = ThreadId::from_u128(/*value*/ 1);
|
||||
let mut search = TaskSearch::default();
|
||||
search.spawn(
|
||||
handle.clone(),
|
||||
"needle".into(),
|
||||
current,
|
||||
PathBuf::from("/tmp"),
|
||||
AppEventSender::new(tx.clone()),
|
||||
);
|
||||
tokio::time::timeout(Duration::from_secs(/*secs*/ 5), started)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
if timeout {
|
||||
tokio::time::pause();
|
||||
let deadline = tokio::time::Instant::now() + SEARCH_TIMEOUT;
|
||||
tokio::time::advance(SEARCH_TIMEOUT).await;
|
||||
let event = events.recv().await.unwrap();
|
||||
assert!(
|
||||
matches!(event, AppEvent::TaskSearchResult { query, matches, .. }
|
||||
if query == "needle" && matches.len() == 1 && matches[0].title == "needle title")
|
||||
);
|
||||
assert!(tokio::time::Instant::now() < deadline + CANCEL_TIMEOUT / 2);
|
||||
tokio::time::resume();
|
||||
} else {
|
||||
search.spawn(
|
||||
handle,
|
||||
String::new(),
|
||||
current,
|
||||
PathBuf::from("/tmp"),
|
||||
AppEventSender::new(tx),
|
||||
);
|
||||
let event = events.recv().await.unwrap();
|
||||
assert!(
|
||||
matches!(event, AppEvent::TaskSearchResult { query, matches, .. } if query.is_empty() && matches.is_empty())
|
||||
);
|
||||
}
|
||||
tokio::time::timeout(Duration::from_secs(/*secs*/ 5), cancelled)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
rpc.abort();
|
||||
let _ = remote.shutdown().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn task_mention_history_round_trips_multiword_and_escaped_titles() {
|
||||
|
||||
Reference in New Issue
Block a user