agent_ui: Keep focus when background threads auto-send queued messages (#66787)

## Summary

Typing in one conversation no longer loses keyboard focus when a
different, background conversation finishes a turn and automatically
sends its next queued follow-up. Sending a queued message yourself (Send
Now, or fast-tracking the next queued message by pressing Send in the
empty composer) still returns focus to that thread's composer.

The focus decision stays in the shared queued-entry dispatch. Each
caller now passes an explicit source (automatic or manual), and the
dispatch focuses the composer only for manual sends. Queue validation,
cancellation, sending and error recovery are unchanged.

Closes #66416

## Testing

Automated checks run locally on the initial fixed implementation:

- New regression test for automatic sends failed without the fix (at the
foreground composer focus assertion) and passes with it. A second new
test covers manual sends keeping focus. Both passed scheduler seeds
0-19.
- All `agent_ui` and `gpui_apple` tests passed, `cargo fmt --check` and
`./script/clippy -p agent_ui -p gpui_apple` passed, and a full
development build succeeded.
- The full workspace suite and Windows/Linux validation were not run
locally.

Automated native desktop UI test on macOS with a local simulated ACP
agent (not a real external agent or model, and not a human manual test):
thread A had two queued follow-ups, and numbered chunks 01-18 were typed
into thread B one character at a time. A's turns completed mid-typing
and each follow-up auto-submitted, with no clicks, refocuses or window
activations. B's draft was then submitted and received exactly `Typing
in B: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18`.

After the follow-up change that passes an explicit dispatch source (an
internal refactor of the same behavior), all `agent_ui` and `gpui_apple`
tests, both focus tests across seeds 0-19, Clippy and formatting were
rerun and passed. The development app and the desktop test above were
not rebuilt or repeated for that change, and the screenshots come from
the initial implementation.

To reproduce:

1. Open two threads, A and B, in the same window.
2. In A, send a message and, while it generates, queue two follow-ups.
3. Switch to B and start typing continuously.
4. When A's turn finishes, its next follow-up is sent. Typing in B
should continue uninterrupted.

AI assistance was used to prepare this change and its description.

## Self-review

- [ ] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

## Screenshots

<img width="800" height="125" alt="Image"
src="https://github.com/user-attachments/assets/69e7a496-d379-40de-bd0f-9febd5786ff7"
/>

Thread A – Queued followups

<img width="800" height="155" alt="Image"
src="https://github.com/user-attachments/assets/67a5b79c-de25-48f6-9f9b-eeec534b2398"
/>


Thread B – Typing focus retained

Release Notes:

- Fixed typing in an agent thread losing focus when another thread
automatically sends its queued messages.

---------

Co-authored-by: David Jensen <288440181+daejey@users.noreply.github.com>
This commit is contained in:
David Jensen
2026-11-16 13:29:34 +00:00
committed by GitHub
parent 89d3da6578
commit d39178f6c9
2 changed files with 116 additions and 4 deletions
+101
View File
@@ -5082,6 +5082,107 @@ pub(crate) mod tests {
);
}
#[gpui::test]
async fn test_auto_sending_queued_messages_preserves_focus(cx: &mut TestAppContext) {
init_test(cx);
let connection = StubAgentConnection::new();
let (background_conversation, cx) =
setup_conversation_view(StubAgentServer::new(connection.clone()), cx).await;
add_to_workspace(background_conversation.clone(), cx);
let background_thread = active_thread(&background_conversation, cx);
message_editor(&background_conversation, cx).update_in(cx, |editor, window, cx| {
editor.set_text("first", window, cx);
});
background_thread.update_in(cx, |view, window, cx| view.send(window, cx));
cx.run_until_parked();
let session_id = background_thread.update_in(cx, |view, window, cx| {
view.add_to_queue(vec!["second".into()], vec![], window, cx);
view.add_to_queue(vec!["third".into()], vec![], window, cx);
view.thread.read(cx).session_id().clone()
});
let foreground_conversation = background_conversation.update_in(cx, |view, window, cx| {
cx.new(|cx| {
ConversationView::new(
view.agent.clone(),
view.connection_store.clone(),
view.connection_key.clone(),
None,
None,
None,
None,
None,
view.workspace.clone(),
view.project.clone(),
view.thread_store.clone(),
AgentThreadSource::AgentPanel,
window,
cx,
)
})
});
cx.run_until_parked();
add_to_workspace(foreground_conversation.clone(), cx);
let foreground_editor = message_editor(&foreground_conversation, cx);
foreground_editor.update_in(cx, |editor, window, cx| {
editor.focus_handle(cx).focus(window, cx);
});
cx.simulate_input("before");
for (remaining, expected_text) in [(1, "before typing"), (0, "before typing typing")] {
connection.end_turn(session_id.clone(), acp_v1::StopReason::EndTurn);
cx.run_until_parked();
background_thread.read_with(cx, |view, cx| {
assert_eq!(view.message_queue.len(), remaining);
assert_eq!(view.thread.read(cx).status(), ThreadStatus::Generating);
});
foreground_editor.update_in(cx, |editor, window, cx| {
assert!(editor.focus_handle(cx).is_focused(window));
});
cx.simulate_input(" typing");
assert_eq!(
foreground_editor.read_with(cx, |editor, cx| editor.text(cx)),
expected_text
);
assert!(
message_editor(&background_conversation, cx)
.read_with(cx, |editor, cx| { editor.is_empty(cx) })
);
}
}
#[gpui::test]
async fn test_manual_queued_message_sends_focus_composer(cx: &mut TestAppContext) {
init_test(cx);
for send_now in [false, true] {
let (conversation, cx) =
setup_conversation_view(StubAgentServer::default_response(), cx).await;
add_to_workspace(conversation.clone(), cx);
let thread_view = active_thread(&conversation, cx);
thread_view.update_in(cx, |view, window, cx| {
view.add_to_queue(vec!["queued".into()], vec![], window, cx);
let entry = view.message_queue.first().expect("queued message");
let id = entry.id;
entry.editor.focus_handle(cx).focus(window, cx);
if send_now {
view.send_queued_message_now(id, window, cx);
} else {
view.send(window, cx);
}
assert!(view.message_queue.is_empty());
assert!(view.message_editor.focus_handle(cx).is_focused(window));
});
cx.run_until_parked();
message_editor(&conversation, cx).update_in(cx, |editor, window, cx| {
assert!(editor.focus_handle(cx).is_focused(window));
});
}
}
#[gpui::test]
async fn test_queued_message_steer_defaults_off_and_toggles(cx: &mut TestAppContext) {
init_test(cx);
@@ -842,6 +842,11 @@ pub struct TurnFields {
pub reported_activity_generation: Option<u64>,
}
enum QueuedMessageDispatchSource {
Automatic,
Manual,
}
enum ThreadControlsPosition {
/// Below a finalized assistant message.
TurnEnd {
@@ -2216,7 +2221,7 @@ impl ThreadView {
return;
}
if let Some(entry) = self.message_queue.try_fast_track(is_generating) {
self.dispatch_queued_entry(entry, window, cx);
self.dispatch_queued_entry(entry, QueuedMessageDispatchSource::Manual, window, cx);
}
return;
}
@@ -3102,7 +3107,7 @@ impl ThreadView {
.message_queue
.on_generation_stopped(is_first_editor_focused)
{
self.dispatch_queued_entry(entry, window, cx);
self.dispatch_queued_entry(entry, QueuedMessageDispatchSource::Automatic, window, cx);
true
} else {
false
@@ -3120,7 +3125,7 @@ impl ThreadView {
}
let is_generating = self.thread.read(cx).status() == acp_thread::ThreadStatus::Generating;
if let Some(entry) = self.message_queue.send_now(id, is_generating) {
self.dispatch_queued_entry(entry, window, cx);
self.dispatch_queued_entry(entry, QueuedMessageDispatchSource::Manual, window, cx);
}
}
@@ -3130,6 +3135,7 @@ impl ThreadView {
fn dispatch_queued_entry(
&mut self,
entry: QueueEntry,
source: QueuedMessageDispatchSource,
window: &mut Window,
cx: &mut Context<Self>,
) {
@@ -3137,7 +3143,12 @@ impl ThreadView {
cx.emit(AcpThreadViewEvent::Interacted);
self.message_editor.focus_handle(cx).focus(window, cx);
match source {
QueuedMessageDispatchSource::Automatic => {}
QueuedMessageDispatchSource::Manual => {
self.message_editor.focus_handle(cx).focus(window, cx);
}
}
// A queued message can itself be a built-in command (e.g. the user typed
// `/compact` while a turn was generating). Detect that so we run it as a