diff --git a/assets/settings/default.json b/assets/settings/default.json index ba46ed80a58..38b266e8f6a 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -1426,6 +1426,12 @@ // // Default: true "show_merge_conflict_indicator": true, + // Whether to automatically generate a title for Zed Agent threads using the + // summary model. When disabled, titles are only created when manually + // requested (e.g. via the thread's "Regenerate Thread Title" action). + // + // Default: true + "auto_generate_thread_titles": true, }, // Whether the screen sharing icon is shown in the os status bar. "show_call_status_icon": true, diff --git a/crates/agent/src/agent.rs b/crates/agent/src/agent.rs index 091f35d080c..f20cd33e099 100644 --- a/crates/agent/src/agent.rs +++ b/crates/agent/src/agent.rs @@ -3319,11 +3319,23 @@ impl acp_thread::AgentConnection for NativeAgentConnection { session_id: acp_v2::SessionId, project: Entity, _work_dirs: PathList, - _title: Option, + title: Option, cx: &mut App, ) -> Task>> { - self.0 - .update(cx, |agent, cx| agent.open_thread(session_id, project, cx)) + let load = self + .0 + .update(cx, |agent, cx| agent.open_thread(session_id, project, cx)); + cx.spawn(async move |cx| { + let thread = load.await?; + if let Some(title) = title { + thread.update(cx, |thread, cx| { + if thread.title().is_none() { + thread.set_provisional_title(title, cx); + } + }); + } + Ok(thread) + }) } fn auth_methods(&self) -> &[acp_v2::AuthMethod] { @@ -6260,6 +6272,85 @@ mod internal_tests { }); } + #[gpui::test] + async fn test_load_session_restores_caller_title_only_without_saved_title( + cx: &mut TestAppContext, + ) { + init_test(cx); + let (connection, agent, project, acp_thread) = setup_native_agent_session(cx).await; + let session_id = acp_thread.read_with(cx, |thread, _| thread.session_id().clone()); + let thread = cx.update(|cx| native_thread_for_session(&agent, &session_id, cx)); + let database = cx + .update(|cx| ThreadsDatabase::connect(cx)) + .await + .expect("thread database should connect"); + + let untitled_session_id = acp_v2::SessionId::new("untitled"); + let mut untitled_thread = thread.read_with(cx, |thread, cx| thread.to_db(cx)).await; + untitled_thread.title = SharedString::default(); + database + .save_thread( + untitled_session_id.clone(), + untitled_thread, + PathList::new(&[Path::new("/a")]), + ) + .await + .expect("untitled thread should save"); + + let titled_session_id = acp_v2::SessionId::new("titled"); + let mut titled_thread = thread.read_with(cx, |thread, cx| thread.to_db(cx)).await; + titled_thread.title = "Saved title".into(); + database + .save_thread( + titled_session_id.clone(), + titled_thread, + PathList::new(&[Path::new("/a")]), + ) + .await + .expect("titled thread should save"); + + let restored_untitled = cx + .update(|cx| { + connection.clone().load_session( + untitled_session_id.clone(), + project.clone(), + PathList::new(&[Path::new("/a")]), + Some("Sidebar title".into()), + cx, + ) + }) + .await + .expect("untitled thread should load"); + cx.run_until_parked(); + restored_untitled.read_with(cx, |thread, _| { + assert_eq!(thread.title(), Some("Sidebar title".into())); + assert!(thread.has_provisional_title()); + }); + // The restored title is display-only, so it must not be written back + // as the native thread's own title. + let native_untitled = + cx.update(|cx| native_thread_for_session(&agent, &untitled_session_id, cx)); + native_untitled.read_with(cx, |thread, _| assert_eq!(thread.title(), None)); + + let restored_titled = cx + .update(|cx| { + connection.load_session( + titled_session_id, + project, + PathList::new(&[Path::new("/a")]), + Some("Stale title".into()), + cx, + ) + }) + .await + .expect("titled thread should load"); + cx.run_until_parked(); + restored_titled.read_with(cx, |thread, _| { + assert_eq!(thread.title(), Some("Saved title".into())); + assert!(!thread.has_provisional_title()); + }); + } + #[gpui::test] async fn test_load_session_replays_provider_native_compaction(cx: &mut TestAppContext) { init_test(cx); diff --git a/crates/agent/src/tests/mod.rs b/crates/agent/src/tests/mod.rs index edabd6065b5..2e5486f55b3 100644 --- a/crates/agent/src/tests/mod.rs +++ b/crates/agent/src/tests/mod.rs @@ -4198,6 +4198,79 @@ async fn test_title_generation(cx: &mut TestAppContext) { }); } +#[gpui::test] +async fn test_title_generation_disabled_by_settings(cx: &mut TestAppContext) { + let ThreadTest { + model, + fake, + thread, + .. + } = setup(cx, TestModel::Fake).await; + + let set_auto_generate_thread_titles = |enabled, cx: &mut TestAppContext| { + cx.update(|cx| { + let mut settings = agent_settings::AgentSettings::get_global(cx).clone(); + settings.auto_generate_thread_titles = enabled; + agent_settings::AgentSettings::override_global(settings, cx); + }) + }; + set_auto_generate_thread_titles(false, cx); + + let summary_model = fake.model("summary"); + thread.update(cx, |thread, cx| { + thread.set_summarization_model(Some(summary_model.clone()), cx) + }); + + let send = thread + .update(cx, |thread, cx| { + thread.send(ClientUserMessageId::new(), ["Hello"], cx) + }) + .unwrap(); + cx.run_until_parked(); + + fake.send_last_text(&model, "Hey!"); + fake.end_last(&model); + cx.run_until_parked(); + + thread.read_with(cx, |thread, _| assert_eq!(thread.title(), None)); + assert_eq!(fake.pending_completions_for(&summary_model), Vec::new()); + send.collect::>().await; + cx.run_until_parked(); + thread.read_with(cx, |thread, _| assert_eq!(thread.title(), None)); + + set_auto_generate_thread_titles(true, cx); + let send = thread + .update(cx, |thread, cx| { + thread.send(ClientUserMessageId::new(), ["Hello again"], cx) + }) + .unwrap(); + cx.run_until_parked(); + fake.send_last_text(&model, "Hey again!"); + fake.end_last(&model); + cx.run_until_parked(); + let summary_request = fake.pending_completions_for(&summary_model).pop().unwrap(); + fake.send_text(&summary_model, &summary_request, "Auto title\n"); + fake.end_stream(&summary_model, &summary_request); + send.collect::>().await; + cx.run_until_parked(); + thread.read_with(cx, |thread, _| { + assert_eq!(thread.title(), Some("Auto title".into())) + }); + + set_auto_generate_thread_titles(false, cx); + thread.update(cx, |thread, cx| { + assert!(thread.regenerate_title(cx)); + }); + cx.run_until_parked(); + let summary_request = fake.pending_completions_for(&summary_model).pop().unwrap(); + fake.send_text(&summary_model, &summary_request, "Manual title\n"); + fake.end_stream(&summary_model, &summary_request); + cx.run_until_parked(); + thread.read_with(cx, |thread, _| { + assert_eq!(thread.title(), Some("Manual title".into())) + }); +} + #[gpui::test] async fn test_stream_thread_title_keeps_only_first_line(cx: &mut TestAppContext) { let request = LanguageModelRequest::default(); diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs index 6be30909a5f..a6513bfce7a 100644 --- a/crates/agent/src/thread.rs +++ b/crates/agent/src/thread.rs @@ -3290,7 +3290,9 @@ impl Thread { this.update(cx, |this, cx| { if &this.prompt_id == prompt_id { this.flush_pending_message(cx); - if this.title.is_none() { + if this.title.is_none() + && AgentSettings::get_global(cx).auto_generate_thread_titles + { this.generate_title(cx); } } diff --git a/crates/agent/src/tool_permissions.rs b/crates/agent/src/tool_permissions.rs index fa38a0b0bb0..4ba7304afbb 100644 --- a/crates/agent/src/tool_permissions.rs +++ b/crates/agent/src/tool_permissions.rs @@ -608,6 +608,7 @@ mod tests { sandbox_permissions: Default::default(), show_turn_stats: false, show_merge_conflict_indicator: true, + auto_generate_thread_titles: true, max_idle_retained_threads: 5, threads_sidebar: agent_settings::ThreadsSidebarSettings { auto_open: true, diff --git a/crates/agent_settings/src/agent_settings.rs b/crates/agent_settings/src/agent_settings.rs index 331480c2e6f..f5ae752b271 100644 --- a/crates/agent_settings/src/agent_settings.rs +++ b/crates/agent_settings/src/agent_settings.rs @@ -248,6 +248,7 @@ pub struct AgentSettings { pub message_editor_min_lines: usize, pub show_turn_stats: bool, pub show_merge_conflict_indicator: bool, + pub auto_generate_thread_titles: bool, pub tool_permissions: ToolPermissions, pub sandbox_permissions: SandboxPermissions, } @@ -844,6 +845,7 @@ impl Settings for AgentSettings { message_editor_min_lines: agent.message_editor_min_lines.unwrap(), show_turn_stats: agent.show_turn_stats.unwrap(), show_merge_conflict_indicator: agent.show_merge_conflict_indicator.unwrap(), + auto_generate_thread_titles: agent.auto_generate_thread_titles.unwrap(), tool_permissions: compile_tool_permissions(agent.tool_permissions), sandbox_permissions: compile_sandbox_permissions(agent.sandbox_permissions), } diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs index aa6004eb6d7..78a91d1c991 100644 --- a/crates/agent_ui/src/agent_panel.rs +++ b/crates/agent_ui/src/agent_panel.rs @@ -3527,10 +3527,15 @@ impl AgentPanel { .read(cx) .entries() .find(|t| t.id == session_id)?; + // The native thread store has no title for threads whose title was + // never generated, e.g. with `auto_generate_thread_titles` off. + let title = ThreadMetadataStore::try_global(cx) + .and_then(|store| store.read(cx).entry_by_session(&session_id)?.title()) + .unwrap_or(thread.title); Some(AgentInitialContent::ThreadSummary { session_id: thread.id, - title: Some(thread.title), + title: Some(title), }) } @@ -4550,9 +4555,18 @@ impl AgentPanel { // also seed the message editor with any prompt text the user had // typed before closing the window (persisted in the scoped kvp // draft-prompt store). - let is_draft = ThreadMetadataStore::try_global(cx) - .and_then(|store| store.read(cx).entry(thread_id).map(|m| m.is_draft())) - .unwrap_or(false); + let (is_draft, saved_title) = ThreadMetadataStore::try_global(cx) + .and_then(|store| { + store + .read(cx) + .entry(thread_id) + .map(|metadata| (metadata.is_draft(), metadata.title.clone())) + }) + .unwrap_or((false, None)); + // Callers such as thread mentions pass a title captured when they were + // created, which can be stale or the default title. Restoring that on + // load would overwrite the title this thread last showed. + let title = saved_title.or(title); let initial_content = is_draft .then(|| crate::draft_prompt_store::read(thread_id, cx)) .flatten() @@ -11704,6 +11718,78 @@ mod tests { }); } + #[gpui::test] + async fn test_load_agent_thread_prefers_sidebar_title_over_caller_title( + cx: &mut TestAppContext, + ) { + let (panel, mut cx) = setup_panel(cx).await; + cx.run_until_parked(); + + panel.update(&mut cx, |panel, cx| { + panel.connection_store.update(cx, |store, cx| { + store.restart_connection( + Agent::Stub, + Rc::new(StubAgentServer::new( + StubAgentConnection::new().with_supports_load_session(true), + )), + cx, + ); + }); + }); + cx.run_until_parked(); + + let thread_id = ThreadId::new(); + cx.update(|_window, cx| { + ThreadMetadataStore::global(cx).update(cx, |store, cx| { + store.save( + crate::thread_metadata_store::ThreadMetadata { + thread_id, + session_id: Some(acp_v2::SessionId::new("saved-session")), + agent_id: Agent::Stub.id(), + title: Some("Sidebar title".into()), + title_override: None, + updated_at: Utc::now(), + created_at: Some(Utc::now()), + interacted_at: None, + worktree_paths: WorktreePaths::from_folder_paths(&PathList::default()), + remote_connection: None, + archived: false, + }, + cx, + ); + }); + }); + + panel.update_in(&mut cx, |panel, window, cx| { + panel.load_agent_thread( + Agent::Stub, + thread_id, + None, + Some(crate::DEFAULT_THREAD_TITLE.into()), + true, + AgentThreadSource::AgentPanel, + window, + cx, + ); + }); + cx.run_until_parked(); + + panel.read_with(&cx, |panel, cx| { + let thread = panel + .active_agent_thread(cx) + .expect("saved thread should load"); + assert_eq!(thread.read(cx).title(), Some("Sidebar title".into())); + }); + cx.update(|_window, cx| { + let metadata = ThreadMetadataStore::global(cx) + .read(cx) + .entry(thread_id) + .cloned() + .expect("sidebar entry should still exist"); + assert_eq!(metadata.title, Some("Sidebar title".into())); + }); + } + #[gpui::test] async fn test_initial_content_for_thread_summary_uses_own_session_id(cx: &mut TestAppContext) { init_test(cx); @@ -11775,6 +11861,36 @@ mod tests { _ => panic!("expected AgentInitialContent::ThreadSummary"), } + cx.update(|cx| { + ThreadMetadataStore::global(cx).update(cx, |store, cx| { + store.save( + crate::thread_metadata_store::ThreadMetadata { + thread_id: ThreadId::new(), + session_id: Some(source_session_id.clone()), + agent_id: Agent::NativeAgent.id(), + title: Some("Sidebar title".into()), + title_override: None, + updated_at: Utc::now(), + created_at: Some(Utc::now()), + interacted_at: None, + worktree_paths: WorktreePaths::from_folder_paths(&PathList::default()), + remote_connection: None, + archived: false, + }, + cx, + ); + }); + }); + let content = cx + .update(|cx| { + AgentPanel::initial_content_for_thread_summary(source_session_id.clone(), cx) + }) + .expect("initial content should be produced for a root thread"); + let AgentInitialContent::ThreadSummary { title, .. } = content else { + panic!("expected AgentInitialContent::ThreadSummary"); + }; + assert_eq!(title, Some("Sidebar title".into())); + // Unknown session ids should still produce no content. let missing = cx.update(|cx| { AgentPanel::initial_content_for_thread_summary( diff --git a/crates/agent_ui/src/agent_ui.rs b/crates/agent_ui/src/agent_ui.rs index a0ae6cc5f67..74a4075e4b3 100644 --- a/crates/agent_ui/src/agent_ui.rs +++ b/crates/agent_ui/src/agent_ui.rs @@ -1106,6 +1106,7 @@ mod tests { sandbox_permissions: Default::default(), show_turn_stats: false, show_merge_conflict_indicator: true, + auto_generate_thread_titles: true, max_idle_retained_threads: 5, threads_sidebar: agent_settings::ThreadsSidebarSettings { auto_open: true, diff --git a/crates/settings_content/src/agent.rs b/crates/settings_content/src/agent.rs index 4078f7aebb4..f9c48da8d12 100644 --- a/crates/settings_content/src/agent.rs +++ b/crates/settings_content/src/agent.rs @@ -394,6 +394,12 @@ pub struct AgentSettingsContent { /// /// Default: true pub show_merge_conflict_indicator: Option, + /// Whether to automatically generate a title for Zed Agent threads using the + /// summary model. When disabled, titles are only created when manually + /// requested (e.g. via the thread's "Regenerate Thread Title" action). + /// + /// Default: true + pub auto_generate_thread_titles: Option, /// Per-tool permission rules for granular control over which tool actions /// require confirmation. /// diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs index 790de77746d..d0801b2052c 100644 --- a/crates/settings_ui/src/page_data.rs +++ b/crates/settings_ui/src/page_data.rs @@ -9584,6 +9584,29 @@ fn ai_page(cx: &App) -> SettingsPage { metadata: None, files: USER, }), + SettingsPageItem::SettingItem(SettingItem { + title: "Auto-Generate Thread Titles", + description: "Whether to automatically generate a title for Zed Agent threads using the summary model. When disabled, titles are only created when manually requested.", + field: Box::new(SettingField { + organization_override: None, + json_path: Some("agent.auto_generate_thread_titles"), + pick: |settings_content| { + settings_content + .agent + .as_ref()? + .auto_generate_thread_titles + .as_ref() + }, + write: |settings_content, value, _| { + settings_content + .agent + .get_or_insert_default() + .auto_generate_thread_titles = value; + }, + }), + metadata: None, + files: USER, + }), ]); items.extend([ diff --git a/docs/src/ai/agent-panel.md b/docs/src/ai/agent-panel.md index c002f506d9b..005f8b769a0 100644 --- a/docs/src/ai/agent-panel.md +++ b/docs/src/ai/agent-panel.md @@ -89,6 +89,7 @@ When focus is in the message editor, you can also use {#kb agent::ScrollOutputPa Thread titles are auto-generated based on the content of the conversation. But you can also edit them manually by clicking the title and typing, or regenerate them by clicking the "Regenerate Thread Title" button in the ellipsis menu in the top right of the panel. +To stop Zed Agent thread titles from being generated automatically, set `agent.auto_generate_thread_titles` to `false`. Threads then use the first line of your first message as their title. ### Following the Agent {#following-the-agent}