Route TUI request replies to their originating connection and thread (#53436)

## Why

Pending requests can outlive a session switch, and different threads or connections can reuse request IDs. Replies and resolution notifications must stay associated with the request that prompted them.

## What changed

- Retain the original app-server reply handle for pending and buffered requests, including requests replayed after thread attachment or session reset.
- Scope user input, MCP elicitation, and user verification bookkeeping by thread, and send user input answers to the requesting thread.
- Keep background resolution notifications from dismissing foreground prompts.
- Consume failed replies to closed connections and clear pending replay state without falling through to another request route.

## Testing

Add WebSocket regression tests for direct and buffered approvals with colliding request IDs and replies to closed connections. Extend coverage for foreground prompt preservation and thread-scoped user verification cancellation.

GitOrigin-RevId: 541acb9910e92f6f2e83c27d2a2384185d4f8fec
This commit is contained in:
Benjamin Carlsson
2026-10-12 17:39:17 +00:00
committed by copyberry
parent b3c3bad976
commit 6f47fcb0be
19 changed files with 881 additions and 403 deletions
+29 -31
View File
@@ -798,11 +798,16 @@ impl App {
pub(super) async fn reset_thread_event_state(&mut self) {
let voice_owner = self.voice_owner_thread_id();
let mut previous_pending = std::mem::take(&mut self.pending_app_server_requests);
// Move retained tasks' approvals to background routing before clearing request bookkeeping.
for (thread_id, requests) in &mut self.agents_overview.dispatched_requests {
if let Some(channel) = self.thread_event_channels.get(thread_id) {
for request in channel.store.lock().await.pending_replay_requests() {
if !requests.iter().any(|pending| pending.id() == request.id()) {
if !requests
.iter()
.any(|pending| pending.request.id() == request.id())
&& let Some(request) = previous_pending.buffered_request(request)
{
requests.push(request);
}
}
@@ -812,8 +817,7 @@ impl App {
for (thread_id, channel) in &self.thread_event_channels {
if Some(*thread_id) != voice_owner && !self.side_threads.contains_key(thread_id) {
for request in channel.store.lock().await.pending_replay_requests() {
self.pending_app_server_requests
.resolve_notification(&thread_id.to_string(), request.id());
previous_pending.resolve_notification(&thread_id.to_string(), request.id());
}
}
}
@@ -844,17 +848,20 @@ impl App {
self.primary_session_configured = None;
self.pending_primary_events.clear();
if voice_owner.is_none() {
self.pending_app_server_requests.clear();
let side_thread_ids: Vec<_> = self.side_threads.keys().copied().collect();
for thread_id in side_thread_ids {
if let Some(channel) = self.thread_event_channels.get(&thread_id) {
for request in channel.store.lock().await.pending_replay_requests() {
let _ = self
.pending_app_server_requests
.note_server_request(&request);
if let Some(request) = previous_pending.buffered_request(request) {
let _ = self
.pending_app_server_requests
.note_buffered_request(&request);
}
}
}
}
} else {
self.pending_app_server_requests = previous_pending;
}
self.pending_startup_thread_start = false;
self.pending_server_version_notice = None;
@@ -904,31 +911,22 @@ impl App {
ThreadBufferedEvent::HistoryEntryResponse(_)
| ThreadBufferedEvent::FeedbackSubmission(_) => true,
});
self.pending_app_server_requests.clear();
let mut unsupported_requests = Vec::new();
let previous_pending = std::mem::take(&mut self.pending_app_server_requests);
for event in &self.pending_primary_events {
if let ThreadBufferedEvent::Request(request) = event
&& let Some(unsupported) = self
.pending_app_server_requests
.note_server_request(request)
{
unsupported_requests.push(unsupported);
}
}
for unsupported in unsupported_requests {
self.pending_primary_events.retain(|event| {
!matches!(event, ThreadBufferedEvent::Request(request)
if request.id() == &unsupported.request_id)
});
if let Err(error) = self
.reject_app_server_request(
app_server,
unsupported.request_id,
unsupported.message,
)
.await
{
tracing::warn!("{error}");
if let ThreadBufferedEvent::Request(request) = event {
let retained = previous_pending.buffered_request((**request).clone());
#[cfg(test)]
let retained = retained.or_else(|| {
Some(super::app_server_requests::BufferedServerRequest::new(
(**request).clone(),
&app_server.request_handle(),
))
});
if let Some(request) = retained {
let _ = self
.pending_app_server_requests
.note_buffered_request(&request);
}
}
}
if started.blocks_direct_input {