agent: Add setting for controlling terminal tool stop button behavior (#47521)

Danilo Leal created

Addressing the feedback left on a previous PR of mine:
https://github.com/zed-industries/zed/pull/46663. I feel like this is
worthy of a setting. Note, however, that this does not apply to typing
`ctrl-c` in the terminal from within the agent panel; this is purely
controlling what happens when you click on the "Stop" button in the
terminal tool call card.

Release Notes:

- Agent: Added a setting for controlling the behavior of the stop button
in the terminal tool call card (between cancelling the command to run
and the thread, or just the command).

Change summary

assets/settings/default.json                |  5 ++++
crates/agent_settings/src/agent_settings.rs |  2 +
crates/agent_ui/src/acp/thread_view.rs      |  4 ++
crates/agent_ui/src/agent_ui.rs             |  1 
crates/settings_content/src/agent.rs        |  5 ++++
crates/settings_ui/src/page_data.rs         | 24 ++++++++++++++++++++++
6 files changed, 39 insertions(+), 2 deletions(-)

Detailed changes

assets/settings/default.json 🔗

@@ -1112,6 +1112,11 @@
     //
     // Default: true
     "expand_terminal_card": true,
+    // Whether clicking the stop button on a running terminal tool should also cancel the agent's generation.
+    // Note that this only applies to the stop button, not to ctrl+c inside the terminal.
+    //
+    // Default: true
+    "cancel_generation_on_terminal_stop": true,
     // Whether to always use cmd-enter (or ctrl-enter on Linux or Windows) to send messages in the agent panel.
     //
     // Default: false

crates/agent_settings/src/agent_settings.rs 🔗

@@ -46,6 +46,7 @@ pub struct AgentSettings {
     pub enable_feedback: bool,
     pub expand_edit_card: bool,
     pub expand_terminal_card: bool,
+    pub cancel_generation_on_terminal_stop: bool,
     pub use_modifier_to_send: bool,
     pub message_editor_min_lines: usize,
     pub show_turn_stats: bool,
@@ -256,6 +257,7 @@ impl Settings for AgentSettings {
             enable_feedback: agent.enable_feedback.unwrap(),
             expand_edit_card: agent.expand_edit_card.unwrap(),
             expand_terminal_card: agent.expand_terminal_card.unwrap(),
+            cancel_generation_on_terminal_stop: agent.cancel_generation_on_terminal_stop.unwrap(),
             use_modifier_to_send: agent.use_modifier_to_send.unwrap(),
             message_editor_min_lines: agent.message_editor_min_lines.unwrap(),
             show_turn_stats: agent.show_turn_stats.unwrap(),

crates/agent_ui/src/acp/thread_view.rs 🔗

@@ -4633,7 +4633,9 @@ impl AcpThreadView {
                                 terminal.update(cx, |terminal, cx| {
                                     terminal.stop_by_user(cx);
                                 });
-                                this.cancel_generation(cx);
+                                if AgentSettings::get_global(cx).cancel_generation_on_terminal_stop {
+                                    this.cancel_generation(cx);
+                                }
                             })
                         }),
                     )

crates/agent_ui/src/agent_ui.rs 🔗

@@ -538,6 +538,7 @@ mod tests {
             enable_feedback: false,
             expand_edit_card: true,
             expand_terminal_card: true,
+            cancel_generation_on_terminal_stop: true,
             use_modifier_to_send: true,
             message_editor_min_lines: 1,
             tool_permissions: Default::default(),

crates/settings_content/src/agent.rs 🔗

@@ -109,6 +109,11 @@ pub struct AgentSettingsContent {
     ///
     /// Default: true
     pub expand_terminal_card: Option<bool>,
+    /// Whether clicking the stop button on a running terminal tool should also cancel the agent's generation.
+    /// Note that this only applies to the stop button, not to ctrl+c inside the terminal.
+    ///
+    /// Default: true
+    pub cancel_generation_on_terminal_stop: Option<bool>,
     /// Whether to always use cmd-enter (or ctrl-enter on Linux or Windows) to send messages in the agent panel.
     ///
     /// Default: false

crates/settings_ui/src/page_data.rs 🔗

@@ -6830,7 +6830,7 @@ fn ai_page() -> SettingsPage {
         ]
     }
 
-    fn agent_configuration_section() -> [SettingsPageItem; 11] {
+    fn agent_configuration_section() -> [SettingsPageItem; 12] {
         [
             SettingsPageItem::SectionHeader("Agent Configuration"),
             SettingsPageItem::SettingItem(SettingItem {
@@ -6975,6 +6975,28 @@ fn ai_page() -> SettingsPage {
                 metadata: None,
                 files: USER,
             }),
+            SettingsPageItem::SettingItem(SettingItem {
+                title: "Cancel Generation On Terminal Stop",
+                description: "Whether clicking the stop button on a running terminal tool should also cancel the agent's generation. Note that this only applies to the stop button, not to ctrl+c inside the terminal.",
+                field: Box::new(SettingField {
+                    json_path: Some("agent.cancel_generation_on_terminal_stop"),
+                    pick: |settings_content| {
+                        settings_content
+                            .agent
+                            .as_ref()?
+                            .cancel_generation_on_terminal_stop
+                            .as_ref()
+                    },
+                    write: |settings_content, value| {
+                        settings_content
+                            .agent
+                            .get_or_insert_default()
+                            .cancel_generation_on_terminal_stop = value;
+                    },
+                }),
+                metadata: None,
+                files: USER,
+            }),
             SettingsPageItem::SettingItem(SettingItem {
                 title: "Use Modifier To Send",
                 description: "Whether to always use cmd-enter (or ctrl-enter on Linux or Windows) to send messages.",