diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs index 8d5acd10a67..72529d5e159 100644 --- a/crates/agent_ui/src/conversation_view.rs +++ b/crates/agent_ui/src/conversation_view.rs @@ -400,26 +400,26 @@ impl Conversation { pattern_index: usize, cx: &mut Context, ) { - let Some(request) = self.permission_request(session_id, request_id, cx) else { + let Some(PermissionOptions::DropdownWithPatterns { patterns, .. }) = self + .permission_request(session_id, request_id, cx) + .and_then(PermissionRequest::legacy_options) + else { return; }; - let Some(options) = request.legacy_options() else { + if pattern_index >= patterns.len() { return; - }; - let pattern_count = match options { - PermissionOptions::DropdownWithPatterns { patterns, .. } => patterns.len(), - _ => 0, - }; + } match self.permission_selections.get_mut(&request_id) { Some(selection @ thread_view::PermissionSelection::SelectedPatterns(_)) => { selection.toggle_pattern(pattern_index); + if !selection.has_any_checked_patterns() { + self.permission_selections.remove(&request_id); + } } _ => { self.permission_selections.insert( request_id, - thread_view::PermissionSelection::SelectedPatterns( - (0..pattern_count).collect(), - ), + thread_view::PermissionSelection::SelectedPatterns(vec![pattern_index]), ); } } @@ -16959,6 +16959,53 @@ pub(crate) mod tests { }); } + #[gpui::test] + async fn test_toggle_command_pattern_toggles_only_clicked_pattern(cx: &mut TestAppContext) { + init_test(cx); + let (conversation_view, cx) = + setup_conversation_view(StubAgentServer::default_response(), cx).await; + let (thread, conversation) = active_thread(&conversation_view, cx) + .read_with(cx, |view, _| { + (view.thread.clone(), view.conversation.clone()) + }); + let session_id = thread.read_with(cx, |thread, _| thread.session_id().clone()); + // A chained command gets one pattern row per sub-command (`ls`, `git status`, `git log`). + let options = ToolPermissionContext::new( + TerminalTool::NAME, + vec!["ls && git status && git log".into()], + ) + .build_permission_options(); + let (request_id, _response) = + request_test_tool_authorization_with_options(&thread, "pipeline", options, cx); + + let toggle = |pattern_index, cx: &mut VisualTestContext| { + conversation.update(cx, |conversation, cx| { + conversation.toggle_permission_pattern(&session_id, request_id, pattern_index, cx); + match conversation.permission_selections.get(&request_id) { + Some(thread_view::PermissionSelection::SelectedPatterns(checked)) => { + let mut checked = checked.clone(); + checked.sort(); + Some(checked) + } + Some(thread_view::PermissionSelection::Choice(_)) => { + panic!("toggling a pattern must not select a dropdown choice") + } + None => None, + } + }) + }; + // An index with no matching row must not enter pattern mode. + assert_eq!(toggle(99, cx), None); + // The first click on a row other than the first checks only that row. + assert_eq!(toggle(1, cx), Some(vec![1])); + // Later clicks add only their own row... + assert_eq!(toggle(0, cx), Some(vec![0, 1])); + // ...and remove only their own row. + assert_eq!(toggle(1, cx), Some(vec![0])); + // Unchecking the last row goes back to the default selection. + assert_eq!(toggle(0, cx), None); + } + #[gpui::test] async fn test_allow_button_uses_selected_granularity(cx: &mut TestAppContext) { init_test(cx); @@ -17669,6 +17716,12 @@ pub(crate) mod tests { cx.run_until_parked(); if with_patterns { + let pattern = cx + .debug_bounds("MENU_ITEM-Always for `cargo test` commands") + .expect("the rendered persistent dropdown should offer the cargo pattern"); + cx.simulate_click(pattern.center(), gpui::Modifiers::default()); + cx.run_until_parked(); + // Toggling `sort` on and off must leave only `cargo test` checked. for _ in 0..2 { let pattern = cx .debug_bounds("MENU_ITEM-Always for `sort` commands") diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs index 502cc6a1bdd..57bca0f2b51 100644 --- a/crates/agent_ui/src/conversation_view/thread_view.rs +++ b/crates/agent_ui/src/conversation_view/thread_view.rs @@ -691,7 +691,8 @@ pub(crate) enum PermissionSelection { Choice(usize), /// "Select options…" mode where individual command patterns can be toggled. /// Contains the indices of checked patterns in the `patterns` list. - /// All patterns start checked when this mode is first activated. + /// Only the pattern the user clicked is checked when this mode is first activated; + /// the user must explicitly check any additional patterns. SelectedPatterns(Vec), } @@ -712,7 +713,7 @@ impl PermissionSelection { } } - fn has_any_checked_patterns(&self) -> bool { + pub(super) fn has_any_checked_patterns(&self) -> bool { match self { Self::SelectedPatterns(checked) => !checked.is_empty(), _ => false,