Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Unify draft and background threads into retained threads (#53737)

Nathan Sobo55e47138e90e
diff --git a/.gitignore b/.gitignore
index b1e12db755d..ba6b74f6c54 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
**/*.db
+**/*.proptest-regressions
**/cargo-target
**/target
**/venv
diff --git a/.handoff-introduce-thread-id.md b/.handoff-introduce-thread-id.md
new file mode 100644
index 00000000000..a1ea81142be
--- /dev/null
+++ b/.handoff-introduce-thread-id.md
@@ -0,0 +1,41 @@
+# Handoff: Introduce ThreadId — Fix sidebar compilation
+
+## Branch: `introduce-thread-id`
+## Worktree: `/Users/nathan/src/worktrees/zed/pure-stork/zed`
+
+## What's done
+
+All `agent_ui` crate changes are complete and compiling:
+- `thread_metadata_store.rs`: New `ThreadId` type, DB migrations, all store methods updated
+- `thread_import.rs`, `thread_worktree_archive.rs`, `threads_archive_view.rs`: Updated
+- `thread_view.rs`: Added `is_draft()` method
+- `agent_ui.rs`: Re-exports `ThreadId`
+- `agent_panel.rs`: Minimal fixes (`entry_by_session`, `unarchive` lookup)
+
+Verify: `cargo check -p agent_ui` passes.
+
+## What remains
+
+`crates/sidebar/src/sidebar.rs` has ~58 compilation errors because `ThreadMetadata.session_id` changed from `acp::SessionId` to `Option<acp::SessionId>`, and several store methods changed signatures.
+
+Run `cargo check -p sidebar 2>&1` to see all errors.
+
+### Mechanical fix patterns needed:
+
+1. **Comparisons**: `metadata.session_id == *session_id` → `metadata.session_id.as_ref() == Some(session_id)`
+2. **HashSet collection**: Where `session_id` is collected into `HashSet<SessionId>`, use `.filter_map(|m| m.session_id.clone())` to extract from Option
+3. **Field access**: `session_id.0` → `session_id.as_ref().unwrap().0` or `if let Some(sid) = &session_id { sid.0 ... }`
+4. **CancelRestore event**: Pattern `CancelRestore { session_id }` → `CancelRestore { thread_id }`. Import `agent_ui::ThreadId`.
+5. **Store lookups**: `store.entry(&session_id)` → `store.entry_by_session(&session_id)`
+6. **Store mutations** (`delete`, `archive`, `unarchive`, `update_working_directories`): These now take `ThreadId`. Look up via `store.entry_by_session(&session_id).map(|t| t.thread_id)` first, then call with the `ThreadId`.
+7. **`entry_ids()`**: Now returns `Iterator<Item = ThreadId>` not `SessionId`
+8. **`cleanup_thread_archived_worktrees`**: Now takes `ThreadId` not `&acp::SessionId`
+
+### After fixing sidebar:
+
+1. Run tests: `cargo test -p agent_ui -- thread_metadata` and `cargo test -p agent_ui -- thread_import`
+2. Verify: `cargo check -p sidebar`
+
+## Reference
+
+The full target implementation exists on the `project-group-refactor` branch at `/Users/nathan/src/zed` if you need to check anything.
diff --git a/Cargo.lock b/Cargo.lock
index ca2e15a8ecf..51015dec06f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -16079,6 +16079,7 @@ dependencies = [
"chrono",
"client",
"clock",
+ "db",
"editor",
"extension",
"fs",
diff --git a/crates/acp_thread/src/connection.rs b/crates/acp_thread/src/connection.rs
index 32bb8abde9a..fbccdd1b93a 100644
--- a/crates/acp_thread/src/connection.rs
+++ b/crates/acp_thread/src/connection.rs
@@ -665,6 +665,23 @@ mod test_support {
)
}
+ /// Test-scoped counter for generating unique session IDs across all
+ /// `StubAgentConnection` instances within a test case. Set as a GPUI
+ /// global in test init so each case starts fresh.
+ pub struct StubSessionCounter(pub AtomicUsize);
+ impl gpui::Global for StubSessionCounter {}
+
+ impl StubSessionCounter {
+ pub fn next(cx: &App) -> usize {
+ cx.try_global::<Self>()
+ .map(|g| g.0.fetch_add(1, Ordering::SeqCst))
+ .unwrap_or_else(|| {
+ static FALLBACK: AtomicUsize = AtomicUsize::new(0);
+ FALLBACK.fetch_add(1, Ordering::SeqCst)
+ })
+ }
+ }
+
#[derive(Clone)]
pub struct StubAgentConnection {
sessions: Arc<Mutex<HashMap<acp::SessionId, Session>>>,
@@ -823,9 +840,7 @@ mod test_support {
work_dirs: PathList,
cx: &mut gpui::App,
) -> Task<gpui::Result<Entity<AcpThread>>> {
- static NEXT_SESSION_ID: AtomicUsize = AtomicUsize::new(0);
- let session_id =
- acp::SessionId::new(NEXT_SESSION_ID.fetch_add(1, Ordering::SeqCst).to_string());
+ let session_id = acp::SessionId::new(StubSessionCounter::next(cx).to_string());
let thread = self.create_session(session_id, project, work_dirs, None, cx);
Task::ready(Ok(thread))
}
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs
index a9debf3ff0f..fd6bd63c103 100644
--- a/crates/agent_ui/src/agent_panel.rs
+++ b/crates/agent_ui/src/agent_panel.rs
@@ -24,7 +24,7 @@ use zed_actions::agent::{
ResolveConflictsWithAgent, ReviewBranchDiff,
};
-use crate::thread_metadata_store::ThreadMetadataStore;
+use crate::thread_metadata_store::{ThreadId, ThreadMetadata, ThreadMetadataStore};
use crate::{
AddContextServer, AgentDiffPane, ConversationView, CopyThreadToClipboard, CycleStartThreadIn,
Follow, InlineAssistant, LoadThreadFromClipboard, NewThread, NewWorktreeBranchTarget,
@@ -56,7 +56,7 @@ use extension_host::ExtensionStore;
use fs::Fs;
use gpui::{
Action, Animation, AnimationExt, AnyElement, App, AsyncWindowContext, ClipboardItem, Corner,
- DismissEvent, Entity, EntityId, EventEmitter, ExternalPaths, FocusHandle, Focusable, Global,
+ DismissEvent, Entity, EntityId, EventEmitter, ExternalPaths, FocusHandle, Focusable,
KeyContext, Pixels, Subscription, Task, UpdateGlobal, WeakEntity, prelude::*,
pulsating_between,
};
@@ -64,7 +64,7 @@ use language::LanguageRegistry;
use language_model::LanguageModelRegistry;
use project::git_store::{GitStoreEvent, RepositoryEvent};
use project::project_settings::ProjectSettings;
-use project::{Project, ProjectPath, Worktree, linked_worktree_short_name};
+use project::{Project, ProjectPath, Worktree, WorktreePaths, linked_worktree_short_name};
use prompt_store::{PromptStore, UserPromptId};
use remote::RemoteConnectionOptions;
use rules_library::{RulesLibrary, open_rules_library};
@@ -80,7 +80,7 @@ use ui::{
use util::{ResultExt as _, debug_panic};
use workspace::{
CollaboratorId, DraggedSelection, DraggedTab, PathList, SerializedPathList,
- ToggleWorkspaceSidebar, ToggleZoom, Workspace, WorkspaceId,
+ ToggleWorkspaceSidebar, ToggleZoom, Workspace, WorkspaceId, WorkspaceSidebarDelegate,
dock::{DockPosition, Panel, PanelEvent},
};
use zed_actions::{
@@ -93,6 +93,49 @@ const AGENT_PANEL_KEY: &str = "agent_panel";
const MIN_PANEL_WIDTH: Pixels = px(300.);
const RECENTLY_UPDATED_MENU_LIMIT: usize = 6;
const LAST_USED_AGENT_KEY: &str = "agent_panel__last_used_external_agent";
+/// Maximum number of idle threads kept in the agent panel's retained list.
+/// Set as a GPUI global to override; otherwise defaults to 5.
+pub struct MaxIdleRetainedThreads(pub usize);
+impl gpui::Global for MaxIdleRetainedThreads {}
+
+impl MaxIdleRetainedThreads {
+ pub fn global(cx: &App) -> usize {
+ cx.try_global::<Self>().map_or(5, |g| g.0)
+ }
+}
+
+#[derive(Default)]
+struct AgentPanelSidebarDelegate;
+
+impl WorkspaceSidebarDelegate for AgentPanelSidebarDelegate {
+ fn reconcile_group(
+ &self,
+ workspace: &mut Workspace,
+ group_key: &workspace::ProjectGroupKey,
+ window: &mut Window,
+ cx: &mut Context<Workspace>,
+ ) -> bool {
+ if workspace.project_group_key(cx) != *group_key {
+ return false;
+ }
+
+ let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
+ return false;
+ };
+
+ panel.update(cx, |panel, cx| {
+ if panel.pending_thread_loads > 0 {
+ return false;
+ }
+ if panel.draft_thread_ids(cx).is_empty() {
+ panel.create_thread(window, cx);
+ true
+ } else {
+ false
+ }
+ })
+ }
+}
#[derive(Serialize, Deserialize)]
struct LastUsedAgent {
@@ -170,6 +213,7 @@ struct SerializedActiveThread {
pub fn init(cx: &mut App) {
cx.observe_new(
|workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
+ workspace.set_sidebar_delegate(Arc::new(AgentPanelSidebarDelegate));
workspace
.register_action(|workspace, action: &NewThread, window, cx| {
if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
@@ -209,8 +253,8 @@ pub fn init(cx: &mut App) {
if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
workspace.focus_panel::<AgentPanel>(window, cx);
panel.update(cx, |panel, cx| {
- let id = panel.create_draft(window, cx);
- panel.activate_draft(id, true, window, cx);
+ let id = panel.create_thread(window, cx);
+ panel.activate_retained_thread(id, true, window, cx);
});
}
})
@@ -594,36 +638,37 @@ fn build_conflicted_files_resolution_prompt(
content
}
-/// Unique identifier for a sidebar draft thread. Not persisted across restarts.
-/// IDs are globally unique across all AgentPanel instances within the same app.
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
-pub struct DraftId(pub usize);
-
-#[derive(Default)]
-struct DraftIdCounter(usize);
-
-impl Global for DraftIdCounter {}
-
-impl DraftId {
- fn next(cx: &mut App) -> Self {
- let counter = cx.default_global::<DraftIdCounter>();
- let id = counter.0;
- counter.0 += 1;
- Self(id)
- }
+pub(crate) struct AgentThread {
+ conversation_view: Entity<ConversationView>,
}
-enum ActiveView {
+enum BaseView {
Uninitialized,
AgentThread {
conversation_view: Entity<ConversationView>,
},
- History {
- view: Entity<ThreadHistoryView>,
- },
+}
+
+impl From<AgentThread> for BaseView {
+ fn from(thread: AgentThread) -> Self {
+ BaseView::AgentThread {
+ conversation_view: thread.conversation_view,
+ }
+ }
+}
+
+enum OverlayView {
+ History { view: Entity<ThreadHistoryView> },
Configuration,
}
+enum VisibleSurface<'a> {
+ Uninitialized,
+ AgentThread(&'a Entity<ConversationView>),
+ History(&'a Entity<ThreadHistoryView>),
+ Configuration(Option<&'a Entity<AgentConfiguration>>),
+}
+
enum WhichFontSize {
AgentFont,
None,
@@ -785,13 +830,17 @@ enum WorktreeCreationArgs {
},
}
-impl ActiveView {
+impl BaseView {
+ pub fn which_font_size_used(&self) -> WhichFontSize {
+ WhichFontSize::AgentFont
+ }
+}
+
+impl OverlayView {
pub fn which_font_size_used(&self) -> WhichFontSize {
match self {
- ActiveView::Uninitialized
- | ActiveView::AgentThread { .. }
- | ActiveView::History { .. } => WhichFontSize::AgentFont,
- ActiveView::Configuration => WhichFontSize::None,
+ OverlayView::History { .. } => WhichFontSize::AgentFont,
+ OverlayView::Configuration => WhichFontSize::None,
}
}
}
@@ -811,10 +860,9 @@ pub struct AgentPanel {
configuration: Option<Entity<AgentConfiguration>>,
configuration_subscription: Option<Subscription>,
focus_handle: FocusHandle,
- active_view: ActiveView,
- previous_view: Option<ActiveView>,
- background_threads: HashMap<acp::SessionId, Entity<ConversationView>>,
- draft_threads: HashMap<DraftId, Entity<ConversationView>>,
+ base_view: BaseView,
+ overlay_view: Option<OverlayView>,
+ retained_threads: HashMap<ThreadId, Entity<ConversationView>>,
new_thread_menu_handle: PopoverMenuHandle<ContextMenu>,
start_thread_in_menu_handle: PopoverMenuHandle<ThreadWorktreePicker>,
thread_branch_menu_handle: PopoverMenuHandle<ThreadBranchPicker>,
@@ -832,12 +880,13 @@ pub struct AgentPanel {
agent_layout_onboarding_dismissed: AtomicBool,
selected_agent: Agent,
start_thread_in: StartThreadIn,
+ pending_thread_loads: usize,
worktree_creation_status: Option<(EntityId, WorktreeCreationStatus)>,
_thread_view_subscription: Option<Subscription>,
_active_thread_focus_subscription: Option<Subscription>,
_worktree_creation_task: Option<Task<()>>,
show_trust_workspace_message: bool,
- _active_view_observation: Option<Subscription>,
+ _base_view_observation: Option<Subscription>,
}
impl AgentPanel {
@@ -913,17 +962,20 @@ impl AgentPanel {
.and_then(|p| p.last_active_thread.as_ref())
{
let session_id = acp::SessionId::new(thread_info.session_id.clone());
- let has_metadata = cx
+ let is_restorable = cx
.update(|_window, cx| {
let store = ThreadMetadataStore::global(cx);
- store.read(cx).entry(&session_id).is_some()
+ store
+ .read(cx)
+ .entry_by_session(&session_id)
+ .is_some_and(|entry| !entry.archived)
})
.unwrap_or(false);
- if has_metadata {
+ if is_restorable {
Some(thread_info)
} else {
- log::warn!(
- "last active thread {} has no metadata, skipping restoration",
+ log::info!(
+ "last active thread {} is archived or missing, skipping restoration",
thread_info.session_id
);
None
@@ -1019,7 +1071,7 @@ impl AgentPanel {
let thread_store = ThreadStore::global(cx);
- let active_view = ActiveView::Uninitialized;
+ let base_view = BaseView::Uninitialized;
let weak_panel = cx.entity().downgrade();
@@ -1179,7 +1231,8 @@ impl AgentPanel {
let mut panel = Self {
workspace_id,
- active_view,
+ base_view,
+ overlay_view: None,
workspace,
user_store,
project: project.clone(),
@@ -1191,9 +1244,7 @@ impl AgentPanel {
configuration_subscription: None,
focus_handle: cx.focus_handle(),
context_server_registry,
- previous_view: None,
- background_threads: HashMap::default(),
- draft_threads: HashMap::default(),
+ retained_threads: HashMap::default(),
new_thread_menu_handle: PopoverMenuHandle::default(),
start_thread_in_menu_handle: PopoverMenuHandle::default(),
thread_branch_menu_handle: PopoverMenuHandle::default(),
@@ -1210,6 +1261,7 @@ impl AgentPanel {
thread_store,
selected_agent: Agent::default(),
start_thread_in: StartThreadIn::default(),
+ pending_thread_loads: 0,
worktree_creation_status: None,
_thread_view_subscription: None,
_active_thread_focus_subscription: None,
@@ -1219,7 +1271,7 @@ impl AgentPanel {
agent_layout_onboarding_dismissed: AtomicBool::new(AgentLayoutOnboarding::dismissed(
cx,
)),
- _active_view_observation: None,
+ _base_view_observation: None,
};
// Initial sync of agent servers from extensions
@@ -1310,84 +1362,142 @@ impl AgentPanel {
.unwrap_or(false)
}
- /// Reset the panel to the uninitialized state, clearing any active
- /// thread without creating a new draft. Running threads are retained
- /// in the background. The sidebar suppresses the uninitialized state
- /// so no "Draft" entry appears.
+ /// Clear any active conversation while preserving a real empty draft.
+ /// Running non-draft threads are retained in the background.
pub fn clear_active_thread(&mut self, window: &mut Window, cx: &mut Context<Self>) {
- self.set_active_view(ActiveView::Uninitialized, false, window, cx);
+ self.show_or_create_empty_draft(window, cx);
+ }
+
+ fn show_or_create_empty_draft(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if self.active_thread_is_draft(cx) {
+ self.clear_overlay_state();
+ self.refresh_base_view_subscriptions(window, cx);
+ self.serialize(cx);
+ cx.emit(AgentPanelEvent::ActiveViewChanged);
+ cx.notify();
+ return;
+ }
+
+ if let Some(draft_id) = self.draft_thread_ids(cx).into_iter().next() {
+ self.activate_retained_thread(draft_id, false, window, cx);
+ cx.notify();
+ return;
+ }
+
+ let id = self.create_thread(window, cx);
+ self.activate_retained_thread(id, false, window, cx);
+ cx.notify();
}
pub fn new_thread(&mut self, _action: &NewThread, window: &mut Window, cx: &mut Context<Self>) {
- let id = self.create_draft(window, cx);
- self.activate_draft(id, true, window, cx);
+ let id = self.create_thread(window, cx);
+ self.activate_retained_thread(id, true, window, cx);
}
- /// Creates a new empty draft thread and stores it. Returns the DraftId.
- /// The draft is NOT activated — call `activate_draft` to show it.
- pub fn create_draft(&mut self, window: &mut Window, cx: &mut Context<Self>) -> DraftId {
- let id = DraftId::next(cx);
- let workspace = self.workspace.clone();
- let project = self.project.clone();
- let fs = self.fs.clone();
- let thread_store = self.thread_store.clone();
+ pub fn create_thread(&mut self, window: &mut Window, cx: &mut Context<Self>) -> ThreadId {
let agent = if self.project.read(cx).is_via_collab() {
Agent::NativeAgent
} else {
self.selected_agent.clone()
};
- let server = agent.server(fs, thread_store);
- let conversation_view = self.create_agent_thread(
- server, None, None, None, None, workspace, project, agent, window, cx,
- );
- self.draft_threads.insert(id, conversation_view);
- id
+ let thread = self.create_agent_thread(agent, None, None, None, None, window, cx);
+ let thread_id = thread.conversation_view.read(cx).thread_id;
+ self.retained_threads
+ .insert(thread_id, thread.conversation_view);
+ thread_id
}
- pub fn activate_draft(
+ pub fn activate_retained_thread(
&mut self,
- id: DraftId,
+ id: ThreadId,
focus: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
- let Some(conversation_view) = self.draft_threads.get(&id).cloned() else {
+ let Some(conversation_view) = self.retained_threads.remove(&id) else {
return;
};
- self.set_active_view(
- ActiveView::AgentThread { conversation_view },
+ self.set_base_view(
+ BaseView::AgentThread { conversation_view },
focus,
window,
cx,
);
}
- /// Removes a draft thread. If it's currently active, does nothing to
- /// the active view — the caller should activate something else first.
- pub fn remove_draft(&mut self, id: DraftId) {
- self.draft_threads.remove(&id);
+ pub fn remove_thread(&mut self, id: ThreadId, cx: &mut Context<Self>) {
+ self.retained_threads.remove(&id);
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.delete(id, cx);
+ });
+
+ if self.active_thread_id(cx) == Some(id) && self.active_thread_is_draft(cx) {
+ self.base_view = BaseView::Uninitialized;
+ self.clear_overlay_state();
+ self._thread_view_subscription = None;
+ self._active_thread_focus_subscription = None;
+ self._base_view_observation = None;
+ self.serialize(cx);
+ cx.emit(AgentPanelEvent::ActiveViewChanged);
+ cx.notify();
+ }
}
- /// Returns the DraftId of the currently active draft, if the active
- /// view is a draft thread tracked in `draft_threads`.
- pub fn active_draft_id(&self) -> Option<DraftId> {
- let active_cv = self.active_conversation_view()?;
- self.draft_threads
- .iter()
- .find_map(|(id, cv)| (cv.entity_id() == active_cv.entity_id()).then_some(*id))
+ pub fn active_thread_id(&self, cx: &App) -> Option<ThreadId> {
+ match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
+ Some(conversation_view.read(cx).thread_id)
+ }
+ _ => None,
+ }
}
- /// Returns all draft IDs, sorted newest-first.
- pub fn draft_ids(&self) -> Vec<DraftId> {
- let mut ids: Vec<DraftId> = self.draft_threads.keys().copied().collect();
- ids.sort_by_key(|id| std::cmp::Reverse(id.0));
+ pub fn draft_thread_ids(&self, cx: &App) -> Vec<ThreadId> {
+ let is_draft = |cv: &Entity<ConversationView>| -> bool {
+ let cv = cv.read(cx);
+ match cv.root_thread(cx) {
+ Some(tv) => tv.read(cx).is_draft(cx),
+ None => cv.is_new_draft(),
+ }
+ };
+
+ let mut ids: Vec<ThreadId> = self
+ .retained_threads
+ .iter()
+ .filter(|(_, cv)| is_draft(cv))
+ .map(|(id, _)| *id)
+ .collect();
+
+ if let BaseView::AgentThread { conversation_view } = &self.base_view {
+ let thread_id = conversation_view.read(cx).thread_id;
+ if is_draft(conversation_view) && !ids.contains(&thread_id) {
+ ids.push(thread_id);
+ }
+ }
+
+ if let Some(store) = ThreadMetadataStore::try_global(cx) {
+ let store = store.read(cx);
+ ids.sort_by(|a, b| {
+ let a_time = store.entry(*a).and_then(|m| m.created_at);
+ let b_time = store.entry(*b).and_then(|m| m.created_at);
+ b_time.cmp(&a_time)
+ });
+ }
ids
}
- /// Returns the text from a draft's message editor, or `None` if the
- /// draft doesn't exist or has no text.
- pub fn draft_editor_text(&self, id: DraftId, cx: &App) -> Option<String> {
- let cv = self.draft_threads.get(&id)?;
+ pub fn editor_text(&self, id: ThreadId, cx: &App) -> Option<String> {
+ let cv = self
+ .retained_threads
+ .get(&id)
+ .or_else(|| match &self.base_view {
+ BaseView::AgentThread { conversation_view }
+ if conversation_view.read(cx).thread_id == id =>
+ {
+ Some(conversation_view)
+ }
+ _ => None,
+ })?;
let tv = cv.read(cx).active_thread()?;
let text = tv.read(cx).message_editor.read(cx).text(cx);
if text.trim().is_empty() {
@@ -1397,11 +1507,19 @@ impl AgentPanel {
}
}
- /// Clears the message editor text of a tracked draft.
- pub fn clear_draft_editor(&self, id: DraftId, window: &mut Window, cx: &mut Context<Self>) {
- let Some(cv) = self.draft_threads.get(&id) else {
- return;
- };
+ pub fn clear_editor(&self, id: ThreadId, window: &mut Window, cx: &mut Context<Self>) {
+ let cv = self
+ .retained_threads
+ .get(&id)
+ .or_else(|| match &self.base_view {
+ BaseView::AgentThread { conversation_view }
+ if conversation_view.read(cx).thread_id == id =>
+ {
+ Some(conversation_view)
+ }
+ _ => None,
+ });
+ let Some(cv) = cv else { return };
let Some(tv) = cv.read(cx).active_thread() else {
return;
};
@@ -1411,7 +1529,7 @@ impl AgentPanel {
});
}
- fn take_active_draft_initial_content(
+ fn take_active_initial_content(
&mut self,
cx: &mut Context<Self>,
) -> Option<AgentInitialContent> {
@@ -1496,11 +1614,6 @@ impl AgentPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let workspace = self.workspace.clone();
- let project = self.project.clone();
- let fs = self.fs.clone();
- let thread_store = self.thread_store.clone();
-
let agent = agent_choice.unwrap_or_else(|| {
if self.project.read(cx).is_via_collab() {
Agent::NativeAgent
@@ -1508,26 +1621,16 @@ impl AgentPanel {
self.selected_agent.clone()
}
});
-
- let server = agent.server(fs, thread_store);
- let conversation_view = self.create_agent_thread(
- server,
+ let thread = self.create_agent_thread(
+ agent,
resume_session_id,
work_dirs,
title,
initial_content,
- workspace,
- project,
- agent,
- window,
- cx,
- );
- self.set_active_view(
- ActiveView::AgentThread { conversation_view },
- focus,
window,
cx,
);
+ self.set_base_view(thread.into(), focus, window, cx);
}
fn deploy_rules_library(
@@ -1619,32 +1722,23 @@ impl AgentPanel {
}
fn open_history(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if matches!(self.overlay_view, Some(OverlayView::History { .. })) {
+ self.clear_overlay(true, window, cx);
+ return;
+ }
+
let Some(view) = self.history_for_selected_agent(window, cx) else {
return;
};
- if let ActiveView::History { view: active_view } = &self.active_view {
- if active_view == &view {
- if let Some(previous_view) = self.previous_view.take() {
- self.set_active_view(previous_view, true, window, cx);
- }
- return;
- }
- }
-
- self.set_active_view(ActiveView::History { view }, true, window, cx);
+ self.set_overlay(OverlayView::History { view }, true, window, cx);
cx.notify();
}
pub fn go_back(&mut self, _: &workspace::GoBack, window: &mut Window, cx: &mut Context<Self>) {
- match self.active_view {
- ActiveView::Configuration | ActiveView::History { .. } => {
- if let Some(previous_view) = self.previous_view.take() {
- self.set_active_view(previous_view, true, window, cx);
- }
- cx.notify();
- }
- _ => {}
+ if self.overlay_view.is_some() {
+ self.clear_overlay(true, window, cx);
+ cx.notify();
}
}
@@ -1697,7 +1791,7 @@ impl AgentPanel {
}
fn handle_font_size_action(&mut self, persist: bool, delta: Pixels, cx: &mut Context<Self>) {
- match self.active_view.which_font_size_used() {
+ match self.visible_font_size() {
WhichFontSize::AgentFont => {
if persist {
update_settings_file(self.fs.clone(), cx, move |settings, cx| {
@@ -1757,11 +1851,15 @@ impl AgentPanel {
}
pub(crate) fn open_configuration(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if matches!(self.overlay_view, Some(OverlayView::Configuration)) {
+ self.clear_overlay(true, window, cx);
+ return;
+ }
+
let agent_server_store = self.project.read(cx).agent_server_store().clone();
let context_server_store = self.project.read(cx).context_server_store();
let fs = self.fs.clone();
- self.set_active_view(ActiveView::Configuration, true, window, cx);
self.configuration = Some(cx.new(|cx| {
AgentConfiguration::new(
fs,
@@ -1782,7 +1880,11 @@ impl AgentPanel {
window,
Self::handle_agent_configuration_event,
));
+ }
+
+ self.set_overlay(OverlayView::Configuration, true, window, cx);
+ if let Some(configuration) = self.configuration.as_ref() {
configuration.focus_handle(cx).focus(window, cx);
}
}
@@ -1990,13 +2092,13 @@ impl AgentPanel {
self.workspace_id
}
- pub fn background_threads(&self) -> &HashMap<acp::SessionId, Entity<ConversationView>> {
- &self.background_threads
+ pub fn retained_threads(&self) -> &HashMap<ThreadId, Entity<ConversationView>> {
+ &self.retained_threads
}
pub fn active_conversation_view(&self) -> Option<&Entity<ConversationView>> {
- match &self.active_view {
- ActiveView::AgentThread { conversation_view } => Some(conversation_view),
+ match &self.base_view {
+ BaseView::AgentThread { conversation_view } => Some(conversation_view),
_ => None,
}
}
@@ -2005,7 +2107,7 @@ impl AgentPanel {
self.active_conversation_view()
.into_iter()
.cloned()
- .chain(self.background_threads.values().cloned())
+ .chain(self.retained_threads.values().cloned())
.collect()
}
@@ -2015,10 +2117,8 @@ impl AgentPanel {
}
pub fn active_agent_thread(&self, cx: &App) -> Option<Entity<AcpThread>> {
- match &self.active_view {
- ActiveView::AgentThread {
- conversation_view, ..
- } => conversation_view
+ match &self.base_view {
+ BaseView::AgentThread { conversation_view } => conversation_view
.read(cx)
.active_thread()
.map(|r| r.read(cx).thread.clone()),
@@ -2026,21 +2126,22 @@ impl AgentPanel {
}
}
- /// Returns the primary thread views for all retained connections: the
- pub fn is_background_thread(&self, session_id: &acp::SessionId) -> bool {
- self.background_threads.contains_key(session_id)
+ pub fn is_retained_thread(&self, id: &ThreadId) -> bool {
+ self.retained_threads.contains_key(id)
}
- pub fn cancel_thread(&self, session_id: &acp::SessionId, cx: &mut Context<Self>) -> bool {
+ pub fn cancel_thread(&self, thread_id: &ThreadId, cx: &mut Context<Self>) -> bool {
let conversation_views = self
.active_conversation_view()
.into_iter()
- .chain(self.background_threads.values());
+ .chain(self.retained_threads.values());
for conversation_view in conversation_views {
- if let Some(thread_view) = conversation_view.read(cx).thread_view(session_id) {
- thread_view.update(cx, |view, cx| view.cancel_generation(cx));
- return true;
+ if *thread_id == conversation_view.read(cx).thread_id {
+ if let Some(thread_view) = conversation_view.read(cx).root_thread_view(cx) {
+ thread_view.update(cx, |view, cx| view.cancel_generation(cx));
+ return true;
+ }
}
}
false
@@ -2057,7 +2158,7 @@ impl AgentPanel {
}
}
- for server_view in self.background_threads.values() {
+ for server_view in self.retained_threads.values() {
if let Some(thread_view) = server_view.read(cx).root_thread(cx) {
views.push(thread_view);
}
@@ -2068,6 +2169,7 @@ impl AgentPanel {
fn update_thread_work_dirs(&self, cx: &mut Context<Self>) {
let new_work_dirs = self.project.read(cx).default_path_list(cx);
+ let new_worktree_paths = self.project.read(cx).worktree_paths(cx);
if let Some(conversation_view) = self.active_conversation_view() {
conversation_view.update(cx, |conversation_view, cx| {
@@ -2075,48 +2177,100 @@ impl AgentPanel {
});
}
- for conversation_view in self.background_threads.values() {
+ for conversation_view in self.retained_threads.values() {
conversation_view.update(cx, |conversation_view, cx| {
conversation_view.set_work_dirs(new_work_dirs.clone(), cx);
});
}
+
+ // Update metadata store so threads' path lists stay in sync with
+ // the project's current worktrees. Without this, threads saved
+ // before a worktree was added would have stale paths and not
+ // appear under the correct sidebar group.
+ let mut thread_ids: Vec<ThreadId> = self.retained_threads.keys().copied().collect();
+ if let Some(active_id) = self.active_thread_id(cx) {
+ thread_ids.push(active_id);
+ }
+ if !thread_ids.is_empty() {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.update_worktree_paths(&thread_ids, new_worktree_paths, cx);
+ });
+ }
}
- fn retain_running_thread(&mut self, old_view: ActiveView, cx: &mut Context<Self>) {
- let ActiveView::AgentThread { conversation_view } = old_view else {
+ fn retain_running_thread(&mut self, old_view: BaseView, cx: &mut Context<Self>) {
+ let BaseView::AgentThread { conversation_view } = old_view else {
return;
};
- // If this ConversationView is a tracked draft, it's already
- // stored in `draft_threads` — don't drop it.
- let is_tracked_draft = self
- .draft_threads
- .values()
- .any(|cv| cv.entity_id() == conversation_view.entity_id());
- if is_tracked_draft {
+ let thread_id = conversation_view.read(cx).thread_id;
+
+ if self.retained_threads.contains_key(&thread_id) {
return;
}
- let Some(thread_view) = conversation_view.read(cx).root_thread(cx) else {
- return;
+ let is_empty_draft = match conversation_view.read(cx).root_thread(cx) {
+ Some(tv) => tv.read(cx).is_draft(cx),
+ None => conversation_view.read(cx).is_new_draft(),
};
-
- if thread_view.read(cx).thread.read(cx).entries().is_empty() {
+ if is_empty_draft {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.delete(thread_id, cx);
+ });
return;
}
- self.background_threads
- .insert(thread_view.read(cx).id.clone(), conversation_view);
- self.cleanup_background_threads(cx);
+ self.retained_threads.insert(thread_id, conversation_view);
+ self.cleanup_retained_threads(cx);
+ }
+
+ fn remove_empty_draft(&mut self, cx: &mut Context<Self>) {
+ let draft_ids: Vec<ThreadId> = self
+ .retained_threads
+ .iter()
+ .filter(|(_, cv)| match cv.read(cx).root_thread(cx) {
+ Some(tv) => tv.read(cx).is_draft(cx),
+ None => cv.read(cx).is_new_draft(),
+ })
+ .map(|(id, _)| *id)
+ .collect();
+ for id in draft_ids {
+ self.retained_threads.remove(&id);
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.delete(id, cx);
+ });
+ }
+
+ // Also clean up orphaned draft metadata in the store for this
+ // panel's worktree paths (e.g. from a previously removed workspace).
+ let path_list = {
+ let project = self.project.read(cx);
+ let worktree_paths = project.worktree_paths(cx);
+ worktree_paths.main_worktree_path_list().clone()
+ };
+ if let Some(store) = ThreadMetadataStore::try_global(cx) {
+ let orphaned: Vec<ThreadId> = {
+ let store = store.read(cx);
+ store
+ .entries_for_path(&path_list)
+ .chain(store.entries_for_main_worktree_path(&path_list))
+ .filter(|entry| entry.is_draft())
+ .map(|entry| entry.thread_id)
+ .collect()
+ };
+ if !orphaned.is_empty() {
+ store.update(cx, |store, cx| {
+ for id in orphaned {
+ store.delete(id, cx);
+ }
+ });
+ }
+ }
}
- /// We keep threads that are:
- /// - Still running
- /// - Do not support reloading the full session
- /// - Have had the most recent events (up to 5 idle threads)
- fn cleanup_background_threads(&mut self, cx: &App) {
+ fn cleanup_retained_threads(&mut self, cx: &App) {
let mut potential_removals = self
- .background_threads
+ .retained_threads
.iter()
.filter(|(_id, view)| {
let Some(thread_view) = view.read(cx).root_thread(cx) else {
@@ -2127,65 +2281,42 @@ impl AgentPanel {
})
.collect::<Vec<_>>();
- const MAX_IDLE_BACKGROUND_THREADS: usize = 5;
+ let max_idle = MaxIdleRetainedThreads::global(cx);
potential_removals.sort_unstable_by_key(|(_, view)| view.read(cx).updated_at(cx));
- let n = potential_removals
- .len()
- .saturating_sub(MAX_IDLE_BACKGROUND_THREADS);
+ let n = potential_removals.len().saturating_sub(max_idle);
let to_remove = potential_removals
.into_iter()
- .map(|(id, _)| id.clone())
+ .map(|(id, _)| *id)
.take(n)
.collect::<Vec<_>>();
for id in to_remove {
- self.background_threads.remove(&id);
+ self.retained_threads.remove(&id);
}
}
pub(crate) fn active_native_agent_thread(&self, cx: &App) -> Option<Entity<agent::Thread>> {
- match &self.active_view {
- ActiveView::AgentThread {
- conversation_view, ..
- } => conversation_view.read(cx).as_native_thread(cx),
+ match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
+ conversation_view.read(cx).as_native_thread(cx)
+ }
_ => None,
}
}
- fn set_active_view(
+ fn set_base_view(
&mut self,
- new_view: ActiveView,
+ new_view: BaseView,
focus: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
- let was_in_agent_history = matches!(self.active_view, ActiveView::History { .. });
- let current_is_uninitialized = matches!(self.active_view, ActiveView::Uninitialized);
- let current_is_history = matches!(self.active_view, ActiveView::History { .. });
- let new_is_history = matches!(new_view, ActiveView::History { .. });
-
- let current_is_config = matches!(self.active_view, ActiveView::Configuration);
- let new_is_config = matches!(new_view, ActiveView::Configuration);
+ self.clear_overlay_state();
- let current_is_overlay = current_is_history || current_is_config;
- let new_is_overlay = new_is_history || new_is_config;
-
- if current_is_uninitialized || (current_is_overlay && !new_is_overlay) {
- self.active_view = new_view;
- } else if !current_is_overlay && new_is_overlay {
- self.previous_view = Some(std::mem::replace(&mut self.active_view, new_view));
- } else {
- let old_view = std::mem::replace(&mut self.active_view, new_view);
- if !new_is_overlay {
- if let Some(previous) = self.previous_view.take() {
- self.retain_running_thread(previous, cx);
- }
- }
- self.retain_running_thread(old_view, cx);
- }
+ let old_view = std::mem::replace(&mut self.base_view, new_view);
+ self.retain_running_thread(old_view, cx);
- // Keep the toolbar's selected agent in sync with the active thread's agent.
- if let ActiveView::AgentThread { conversation_view } = &self.active_view {
+ if let BaseView::AgentThread { conversation_view } = &self.base_view {
let thread_agent = conversation_view.read(cx).agent_key().clone();
if self.selected_agent != thread_agent {
self.selected_agent = thread_agent;
@@ -2193,12 +2324,57 @@ impl AgentPanel {
}
}
- // Subscribe to the active ThreadView's events (e.g. FirstSendRequested)
- // so the panel can intercept the first send for worktree creation.
- // Re-subscribe whenever the ConnectionView changes, since the inner
- // ThreadView may have been replaced (e.g. navigating between threads).
- self._active_view_observation = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ self.refresh_base_view_subscriptions(window, cx);
+
+ if focus {
+ self.focus_handle(cx).focus(window, cx);
+ }
+ cx.emit(AgentPanelEvent::ActiveViewChanged);
+ }
+
+ fn set_overlay(
+ &mut self,
+ overlay: OverlayView,
+ focus: bool,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let was_in_history = matches!(self.overlay_view, Some(OverlayView::History { .. }));
+ self.overlay_view = Some(overlay);
+
+ if let Some(OverlayView::History { view }) = &self.overlay_view
+ && !was_in_history
+ {
+ view.update(cx, |view, cx| {
+ view.history()
+ .update(cx, |history, cx| history.refresh_full_history(cx))
+ });
+ }
+
+ if focus {
+ self.focus_handle(cx).focus(window, cx);
+ }
+ cx.emit(AgentPanelEvent::ActiveViewChanged);
+ }
+
+ fn clear_overlay(&mut self, focus: bool, window: &mut Window, cx: &mut Context<Self>) {
+ self.clear_overlay_state();
+
+ if focus {
+ self.focus_handle(cx).focus(window, cx);
+ }
+ cx.emit(AgentPanelEvent::ActiveViewChanged);
+ }
+
+ fn clear_overlay_state(&mut self) {
+ self.overlay_view = None;
+ self.configuration_subscription = None;
+ self.configuration = None;
+ }
+
+ fn refresh_base_view_subscriptions(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ self._base_view_observation = match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
self._thread_view_subscription =
Self::subscribe_to_active_thread_view(conversation_view, window, cx);
let focus_handle = conversation_view.focus_handle(cx);
@@ -2219,26 +2395,46 @@ impl AgentPanel {
},
))
}
- _ => {
+ BaseView::Uninitialized => {
self._thread_view_subscription = None;
self._active_thread_focus_subscription = None;
None
}
};
+ self.serialize(cx);
+ }
- if let ActiveView::History { view } = &self.active_view {
- if !was_in_agent_history {
- view.update(cx, |view, cx| {
- view.history()
- .update(cx, |history, cx| history.refresh_full_history(cx))
- });
- }
+ fn visible_surface(&self) -> VisibleSurface<'_> {
+ if let Some(overlay_view) = &self.overlay_view {
+ return match overlay_view {
+ OverlayView::History { view } => VisibleSurface::History(view),
+ OverlayView::Configuration => {
+ VisibleSurface::Configuration(self.configuration.as_ref())
+ }
+ };
}
- if focus {
- self.focus_handle(cx).focus(window, cx);
+ match &self.base_view {
+ BaseView::Uninitialized => VisibleSurface::Uninitialized,
+ BaseView::AgentThread { conversation_view } => {
+ VisibleSurface::AgentThread(conversation_view)
+ }
}
- cx.emit(AgentPanelEvent::ActiveViewChanged);
+ }
+
+ fn is_overlay_open(&self) -> bool {
+ self.overlay_view.is_some()
+ }
+
+ fn is_history_or_configuration_visible(&self) -> bool {
+ self.is_overlay_open()
+ }
+
+ fn visible_font_size(&self) -> WhichFontSize {
+ self.overlay_view.as_ref().map_or_else(
+ || self.base_view.which_font_size_used(),
+ OverlayView::which_font_size_used,
+ )
}
fn populate_recently_updated_menu_section(
@@ -2312,14 +2508,11 @@ impl AgentPanel {
this.handle_first_send_requested(view.clone(), content.clone(), window, cx);
}
AcpThreadViewEvent::MessageSentOrQueued => {
- // When a draft sends its first message it becomes a
- // real thread. Remove it from `draft_threads` so the
- // sidebar stops showing a stale draft entry.
- if let Some(draft_id) = this.active_draft_id() {
- this.draft_threads.remove(&draft_id);
- }
- let session_id = view.read(cx).thread.read(cx).session_id().clone();
- cx.emit(AgentPanelEvent::MessageSentOrQueued { session_id });
+ let Some(thread_id) = this.active_thread_id(cx) else {
+ return;
+ };
+ this.retained_threads.remove(&thread_id);
+ cx.emit(AgentPanelEvent::MessageSentOrQueued { thread_id });
}
},
)
@@ -2574,7 +2767,7 @@ impl AgentPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let initial_content = self.take_active_draft_initial_content(cx);
+ let initial_content = self.take_active_initial_content(cx);
self.external_thread(
Some(agent),
None,
@@ -2587,6 +2780,10 @@ impl AgentPanel {
);
}
+ pub fn begin_loading_thread(&mut self) {
+ self.pending_thread_loads += 1;
+ }
+
pub fn load_agent_thread(
&mut self,
agent: Agent,
@@ -2597,41 +2794,49 @@ impl AgentPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
+ self.pending_thread_loads = self.pending_thread_loads.saturating_sub(1);
if let Some(store) = ThreadMetadataStore::try_global(cx) {
- store.update(cx, |store, cx| store.unarchive(&session_id, cx));
- }
-
- if let Some(conversation_view) = self.background_threads.remove(&session_id) {
- self.set_active_view(
- ActiveView::AgentThread { conversation_view },
- focus,
- window,
- cx,
- );
- return;
+ let thread_id = store
+ .read(cx)
+ .entry_by_session(&session_id)
+ .map(|t| t.thread_id);
+ if let Some(thread_id) = thread_id {
+ store.update(cx, |store, cx| {
+ store.unarchive(thread_id, cx);
+ });
+ }
}
- if let ActiveView::AgentThread { conversation_view } = &self.active_view {
- if conversation_view
- .read(cx)
+ let has_session = |cv: &Entity<ConversationView>| -> bool {
+ cv.read(cx)
.active_thread()
- .map(|t| t.read(cx).id.clone())
- == Some(session_id.clone())
- {
+ .is_some_and(|tv| tv.read(cx).thread.read(cx).session_id() == &session_id)
+ };
+
+ // Check if the active view already has this session.
+ if let BaseView::AgentThread { conversation_view } = &self.base_view {
+ if has_session(conversation_view) {
+ self.clear_overlay_state();
cx.emit(AgentPanelEvent::ActiveViewChanged);
return;
}
}
- if let Some(ActiveView::AgentThread { conversation_view }) = &self.previous_view {
- if conversation_view
- .read(cx)
- .active_thread()
- .map(|t| t.read(cx).id.clone())
- == Some(session_id.clone())
- {
- let view = self.previous_view.take().unwrap();
- self.set_active_view(view, focus, window, cx);
+ // Check if a retained thread has this session — promote it.
+ let retained_key = self
+ .retained_threads
+ .iter()
+ .find(|(_, cv)| has_session(cv))
+ .map(|(id, _)| *id);
+ if let Some(thread_id) = retained_key {
+ if let Some(conversation_view) = self.retained_threads.remove(&thread_id) {
+ self.set_base_view(
+ BaseView::AgentThread { conversation_view },
+ focus,
+ window,
+ cx,
+ );
+ self.remove_empty_draft(cx);
return;
}
}
@@ -2646,21 +2851,82 @@ impl AgentPanel {
window,
cx,
);
+ self.remove_empty_draft(cx);
}
pub(crate) fn create_agent_thread(
&mut self,
- server: Rc<dyn AgentServer>,
+ agent: Agent,
resume_session_id: Option<acp::SessionId>,
work_dirs: Option<PathList>,
title: Option<SharedString>,
initial_content: Option<AgentInitialContent>,
- workspace: WeakEntity<Workspace>,
- project: Entity<Project>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) -> AgentThread {
+ self.create_agent_thread_with_server(
+ agent,
+ None,
+ resume_session_id,
+ work_dirs,
+ title,
+ initial_content,
+ window,
+ cx,
+ )
+ }
+
+ fn create_agent_thread_with_server(
+ &mut self,
agent: Agent,
+ server_override: Option<Rc<dyn AgentServer>>,
+ resume_session_id: Option<acp::SessionId>,
+ work_dirs: Option<PathList>,
+ title: Option<SharedString>,
+ initial_content: Option<AgentInitialContent>,
window: &mut Window,
cx: &mut Context<Self>,
- ) -> Entity<ConversationView> {
+ ) -> AgentThread {
+ let existing_metadata = resume_session_id.as_ref().and_then(|sid| {
+ ThreadMetadataStore::try_global(cx)
+ .and_then(|store| store.read(cx).entry_by_session(sid).cloned())
+ });
+ let thread_id = existing_metadata
+ .as_ref()
+ .map(|m| m.thread_id)
+ .unwrap_or_else(ThreadId::new);
+ let workspace = self.workspace.clone();
+ let project = self.project.clone();
+ let mut worktree_paths = project.read(cx).worktree_paths(cx);
+ if let Some(existing) = &existing_metadata {
+ // When resuming a session (e.g. clicking a linked-worktree thread
+ // in the sidebar), the current workspace's project may not have
+ // completed its git scan yet. At that point `from_project()` would
+ // compute main_worktree_paths from the raw folder path instead of
+ // the git repo root, overwriting the thread's canonical project
+ // group association. Preserve the existing main_worktree_paths so
+ // the thread stays in the correct sidebar group.
+ if !existing.main_worktree_paths().is_empty() {
+ worktree_paths = WorktreePaths::from_path_lists(
+ existing.main_worktree_paths().clone(),
+ worktree_paths.folder_path_list().clone(),
+ )
+ .unwrap_or(worktree_paths);
+ }
+ }
+ let remote_connection = project.read(cx).remote_connection_options(cx);
+ let metadata = ThreadMetadata::new_draft(
+ thread_id,
+ resume_session_id.clone(),
+ agent.id(),
+ title.clone(),
+ worktree_paths,
+ remote_connection,
+ );
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.save_all(vec![metadata], cx);
+ });
+
if self.selected_agent != agent {
self.selected_agent = agent.clone();
self.serialize(cx);
@@ -2675,6 +2941,8 @@ impl AgentPanel {
})
.detach();
+ let server = server_override
+ .unwrap_or_else(|| agent.server(self.fs.clone(), self.thread_store.clone()));
let thread_store = server
.clone()
.downcast::<agent::NativeAgentServer>()
@@ -2689,6 +2957,7 @@ impl AgentPanel {
connection_store,
agent,
resume_session_id,
+ Some(thread_id),
work_dirs,
title,
initial_content,
@@ -2709,13 +2978,13 @@ impl AgentPanel {
cx.emit(AgentPanelEvent::ActiveViewChanged);
this.serialize(cx);
} else {
- cx.emit(AgentPanelEvent::BackgroundThreadChanged);
+ cx.emit(AgentPanelEvent::RetainedThreadChanged);
}
cx.notify();
})
.detach();
- conversation_view
+ AgentThread { conversation_view }
}
fn active_thread_has_messages(&self, cx: &App) -> bool {
@@ -3029,7 +3298,7 @@ impl AgentPanel {
if let Some((_, status)) = &mut self.worktree_creation_status {
*status = WorktreeCreationStatus::Error(message);
}
- if matches!(self.active_view, ActiveView::Uninitialized) {
+ if matches!(self.base_view, BaseView::Uninitialized) {
let selected_agent = self.selected_agent.clone();
self.new_agent_thread(selected_agent, window, cx);
}
@@ -3489,14 +3758,12 @@ impl AgentPanel {
impl Focusable for AgentPanel {
fn focus_handle(&self, cx: &App) -> FocusHandle {
- match &self.active_view {
- ActiveView::Uninitialized => self.focus_handle.clone(),
- ActiveView::AgentThread {
- conversation_view, ..
- } => conversation_view.focus_handle(cx),
- ActiveView::History { view } => view.read(cx).focus_handle(cx),
- ActiveView::Configuration => {
- if let Some(configuration) = self.configuration.as_ref() {
+ match self.visible_surface() {
+ VisibleSurface::Uninitialized => self.focus_handle.clone(),
+ VisibleSurface::AgentThread(conversation_view) => conversation_view.focus_handle(cx),
+ VisibleSurface::History(view) => view.read(cx).focus_handle(cx),
+ VisibleSurface::Configuration(configuration) => {
+ if let Some(configuration) = configuration {
configuration.focus_handle(cx)
} else {
self.focus_handle.clone()
@@ -3513,8 +3780,8 @@ fn agent_panel_dock_position(cx: &App) -> DockPosition {
pub enum AgentPanelEvent {
ActiveViewChanged,
ThreadFocused,
- BackgroundThreadChanged,
- MessageSentOrQueued { session_id: acp::SessionId },
+ RetainedThreadChanged,
+ MessageSentOrQueued { thread_id: ThreadId },
}
impl EventEmitter<PanelEvent> for AgentPanel {}
@@ -3578,18 +3845,7 @@ impl Panel for AgentPanel {
});
}
- fn set_active(&mut self, active: bool, window: &mut Window, cx: &mut Context<Self>) {
- if active
- && matches!(self.active_view, ActiveView::Uninitialized)
- && !matches!(
- self.worktree_creation_status,
- Some((_, WorktreeCreationStatus::Creating))
- )
- {
- let id = self.create_draft(window, cx);
- self.activate_draft(id, false, window, cx);
- }
- }
+ fn set_active(&mut self, _active: bool, _window: &mut Window, _cx: &mut Context<Self>) {}
fn remote_id() -> Option<proto::PanelId> {
Some(proto::PanelId::AssistantPanel)
@@ -3631,8 +3887,8 @@ impl Panel for AgentPanel {
impl AgentPanel {
fn render_title_view(&self, _window: &mut Window, cx: &Context<Self>) -> AnyElement {
- let content = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ let content = match self.visible_surface() {
+ VisibleSurface::AgentThread(conversation_view) => {
let server_view_ref = conversation_view.read(cx);
let is_generating_title = server_view_ref.as_native_thread(cx).is_some()
&& server_view_ref.root_thread(cx).map_or(false, |tv| {
@@ -3684,9 +3940,11 @@ impl AgentPanel {
.into_any_element()
}
}
- ActiveView::History { .. } => Label::new("History").truncate().into_any_element(),
- ActiveView::Configuration => Label::new("Settings").truncate().into_any_element(),
- ActiveView::Uninitialized => Label::new("Agent").truncate().into_any_element(),
+ VisibleSurface::History(_) => Label::new("History").truncate().into_any_element(),
+ VisibleSurface::Configuration(_) => {
+ Label::new("Settings").truncate().into_any_element()
+ }
+ VisibleSurface::Uninitialized => Label::new("Agent").truncate().into_any_element(),
};
h_flex()
@@ -3717,18 +3975,18 @@ impl AgentPanel {
) -> impl IntoElement {
let focus_handle = self.focus_handle(cx);
- let conversation_view = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => Some(conversation_view.clone()),
+ let conversation_view = match &self.base_view {
+ BaseView::AgentThread { conversation_view } => Some(conversation_view.clone()),
_ => None,
};
- let thread_with_messages = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ let thread_with_messages = match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
conversation_view.read(cx).has_user_submitted_prompt(cx)
}
_ => false,
};
- let has_auth_methods = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ let has_auth_methods = match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
conversation_view.read(cx).has_auth_methods()
}
_ => false,
@@ -3950,13 +4208,11 @@ impl AgentPanel {
(None, self.selected_agent.label())
};
- let active_thread = match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ let active_thread = match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
conversation_view.read(cx).as_native_thread(cx)
}
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {
- None
- }
+ BaseView::Uninitialized => None,
};
let new_thread_menu_builder: Rc<
@@ -4021,8 +4277,10 @@ impl AgentPanel {
panel.update(cx, |panel, cx| {
panel.selected_agent = Agent::NativeAgent;
panel.reset_start_thread_in_to_default(cx);
- let id = panel.create_draft(window, cx);
- panel.activate_draft(id, true, window, cx);
+ let id = panel.create_thread(window, cx);
+ panel.activate_retained_thread(
+ id, true, window, cx,
+ );
});
}
});
@@ -4109,8 +4367,9 @@ impl AgentPanel {
panel.reset_start_thread_in_to_default(
cx,
);
- let id = panel.create_draft(window, cx);
- panel.activate_draft(
+ let id =
+ panel.create_thread(window, cx);
+ panel.activate_retained_thread(
id, true, window, cx,
);
});
@@ -4190,10 +4449,7 @@ impl AgentPanel {
let is_empty_state = !self.active_thread_has_messages(cx);
- let is_in_history_or_config = matches!(
- &self.active_view,
- ActiveView::History { .. } | ActiveView::Configuration
- );
+ let is_in_history_or_config = self.is_history_or_configuration_visible();
let is_full_screen = self.is_zoomed(window, cx);
let full_screen_button = if is_full_screen {
@@ -4327,11 +4583,10 @@ impl AgentPanel {
.size_full()
.gap(DynamicSpacing::Base04.rems(cx))
.pl(DynamicSpacing::Base04.rems(cx))
- .child(match &self.active_view {
- ActiveView::History { .. } | ActiveView::Configuration => {
- self.render_toolbar_back_button(cx).into_any_element()
- }
- _ => selected_agent.into_any_element(),
+ .child(if self.is_overlay_open() {
+ self.render_toolbar_back_button(cx).into_any_element()
+ } else {
+ selected_agent.into_any_element()
})
.child(self.render_title_view(window, cx)),
)
@@ -4415,8 +4670,8 @@ impl AgentPanel {
return false;
}
- match &self.active_view {
- ActiveView::AgentThread { .. } => {
+ match &self.base_view {
+ BaseView::AgentThread { .. } => {
if LanguageModelRegistry::global(cx)
.read(cx)
.default_model()
@@ -4427,7 +4682,7 @@ impl AgentPanel {
return false;
}
}
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {
+ BaseView::Uninitialized => {
return false;
}
}
@@ -4453,11 +4708,9 @@ impl AgentPanel {
return false;
}
- match &self.active_view {
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {
- false
- }
- ActiveView::AgentThread { .. } => {
+ match &self.base_view {
+ BaseView::Uninitialized => false,
+ BaseView::AgentThread { .. } => {
let existing_user = self
.new_user_onboarding_upsell_dismissed
.load(Ordering::Acquire);
@@ -4526,14 +4779,14 @@ impl AgentPanel {
&& provider.id() != language_model::ZED_CLOUD_PROVIDER_ID
});
- match &self.active_view {
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {
+ match &self.base_view {
+ BaseView::Uninitialized => false,
+ BaseView::AgentThread { conversation_view }
+ if conversation_view.read(cx).as_native_thread(cx).is_none() =>
+ {
false
}
- ActiveView::AgentThread {
- conversation_view, ..
- } if conversation_view.read(cx).as_native_thread(cx).is_none() => false,
- ActiveView::AgentThread { conversation_view } => {
+ BaseView::AgentThread { conversation_view } => {
let history_is_empty = conversation_view
.read(cx)
.history()
@@ -4654,13 +4907,13 @@ impl AgentPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- match &self.active_view {
- ActiveView::AgentThread { conversation_view } => {
+ match &self.base_view {
+ BaseView::AgentThread { conversation_view } => {
conversation_view.update(cx, |conversation_view, cx| {
conversation_view.insert_dragged_files(paths, added_worktrees, window, cx);
});
}
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {}
+ BaseView::Uninitialized => {}
}
}
@@ -4699,9 +4952,9 @@ impl AgentPanel {
fn key_context(&self) -> KeyContext {
let mut key_context = KeyContext::new_with_defaults();
key_context.add("AgentPanel");
- match &self.active_view {
- ActiveView::AgentThread { .. } => key_context.add("acp_thread"),
- ActiveView::Uninitialized | ActiveView::History { .. } | ActiveView::Configuration => {}
+ match &self.base_view {
+ BaseView::AgentThread { .. } => key_context.add("acp_thread"),
+ BaseView::Uninitialized => {}
}
key_context
}
@@ -4752,20 +5005,20 @@ impl Render for AgentPanel {
.children(self.render_workspace_trust_message(cx))
.children(self.render_new_user_onboarding(window, cx))
.children(self.render_agent_layout_onboarding(window, cx))
- .map(|parent| match &self.active_view {
- ActiveView::Uninitialized => parent,
- ActiveView::AgentThread {
- conversation_view, ..
- } => parent
+ .map(|parent| match self.visible_surface() {
+ VisibleSurface::Uninitialized => parent,
+ VisibleSurface::AgentThread(conversation_view) => parent
.child(conversation_view.clone())
.child(self.render_drag_target(cx)),
- ActiveView::History { view } => parent.child(view.clone()),
- ActiveView::Configuration => parent.children(self.configuration.clone()),
+ VisibleSurface::History(view) => parent.child(view.clone()),
+ VisibleSurface::Configuration(configuration) => {
+ parent.children(configuration.cloned())
+ }
})
.children(self.render_worktree_creation_status(cx))
.children(self.render_trial_end_upsell(window, cx));
- match self.active_view.which_font_size_used() {
+ match self.visible_font_size() {
WhichFontSize::AgentFont => {
WithRemSize::new(ThemeSettings::get_global(cx).agent_ui_font_size(cx))
.size_full()
@@ -4872,22 +5125,31 @@ impl AgentPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let workspace = self.workspace.clone();
- let project = self.project.clone();
+ let active_id = self.active_thread_id(cx);
+ let empty_draft_ids: Vec<ThreadId> = self
+ .draft_thread_ids(cx)
+ .into_iter()
+ .filter(|id| Some(*id) != active_id && self.editor_text(*id, cx).is_none())
+ .collect();
+ for id in empty_draft_ids {
+ self.remove_thread(id, cx);
+ }
let ext_agent = Agent::Custom {
id: server.agent_id(),
};
- let conversation_view = self.create_agent_thread(
- server, None, None, None, None, workspace, project, ext_agent, window, cx,
- );
- self.set_active_view(
- ActiveView::AgentThread { conversation_view },
- true,
+ let thread = self.create_agent_thread_with_server(
+ ext_agent,
+ Some(server),
+ None,
+ None,
+ None,
+ None,
window,
cx,
);
+ self.set_base_view(thread.into(), true, window, cx);
}
/// Returns the currently active thread view, if any.
@@ -4966,8 +5228,8 @@ mod tests {
use super::*;
use crate::conversation_view::tests::{StubAgentServer, init_test};
use crate::test_support::{
- active_session_id, open_thread_with_connection, open_thread_with_custom_connection,
- send_message,
+ active_session_id, active_thread_id, open_thread_with_connection,
+ open_thread_with_custom_connection, send_message,
};
use acp_thread::{StubAgentConnection, ThreadStatus};
use agent_servers::CODEX_ID;
@@ -4990,7 +5252,9 @@ mod tests {
// Create a MultiWorkspace window with two workspaces.
let fs = FakeFs::new(cx.executor());
- let project_a = Project::test(fs.clone(), [], cx).await;
+ fs.insert_tree("/project_a", json!({ "file.txt": "" }))
+ .await;
+ let project_a = Project::test(fs.clone(), [Path::new("/project_a")], cx).await;
let project_b = Project::test(fs, [], cx).await;
let multi_workspace =
@@ -5449,9 +5713,10 @@ mod tests {
panel: &Entity<AgentPanel>,
connection: &StubAgentConnection,
cx: &mut VisualTestContext,
- ) -> acp::SessionId {
+ ) -> (acp::SessionId, ThreadId) {
open_thread_with_custom_connection(panel, connection.clone(), cx);
let session_id = active_session_id(panel, cx);
+ let thread_id = active_thread_id(panel, cx);
send_message(panel, cx);
cx.update(|_, cx| {
connection.send_update(
@@ -5461,23 +5726,24 @@ mod tests {
);
});
cx.run_until_parked();
- session_id
+ (session_id, thread_id)
}
fn open_idle_thread_with_non_loadable_connection(
panel: &Entity<AgentPanel>,
connection: &StubAgentConnection,
cx: &mut VisualTestContext,
- ) -> acp::SessionId {
+ ) -> (acp::SessionId, ThreadId) {
open_thread_with_custom_connection(panel, connection.clone(), cx);
let session_id = active_session_id(panel, cx);
+ let thread_id = active_thread_id(panel, cx);
connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
acp::ContentChunk::new("done".into()),
)]);
send_message(panel, cx);
- session_id
+ (session_id, thread_id)
}
async fn setup_panel(cx: &mut TestAppContext) -> (Entity<AgentPanel>, VisualTestContext) {
@@ -5507,12 +5773,11 @@ mod tests {
}
#[gpui::test]
- async fn test_empty_draft_thread_not_retained_when_navigating_away(cx: &mut TestAppContext) {
+ async fn test_empty_draft_discarded_when_navigating_away(cx: &mut TestAppContext) {
let (panel, mut cx) = setup_panel(cx).await;
let connection_a = StubAgentConnection::new();
open_thread_with_connection(&panel, connection_a, &mut cx);
- let session_id_a = active_session_id(&panel, &cx);
panel.read_with(&cx, |panel, cx| {
let thread = panel.active_agent_thread(cx).unwrap();
@@ -5520,7 +5785,7 @@ mod tests {
thread.read(cx).entries().is_empty(),
"newly opened draft thread should have no entries"
);
- assert!(panel.background_threads.is_empty());
+ assert!(panel.retained_threads.is_empty());
});
let connection_b = StubAgentConnection::new();
@@ -5528,12 +5793,8 @@ mod tests {
panel.read_with(&cx, |panel, _cx| {
assert!(
- panel.background_threads.is_empty(),
- "empty draft thread should not be retained in background_threads"
- );
- assert!(
- !panel.background_threads.contains_key(&session_id_a),
- "empty draft thread should not be keyed in background_threads"
+ panel.retained_threads.is_empty(),
+ "empty draft should be discarded, not retained"
);
});
}
@@ -5547,6 +5808,7 @@ mod tests {
send_message(&panel, &mut cx);
let session_id_a = active_session_id(&panel, &cx);
+ let thread_id_a = active_thread_id(&panel, &cx);
// Send a chunk to keep thread A generating (don't end the turn).
cx.update(|_, cx| {
@@ -5562,7 +5824,7 @@ mod tests {
panel.read_with(&cx, |panel, cx| {
let thread = panel.active_agent_thread(cx).unwrap();
assert_eq!(thread.read(cx).status(), ThreadStatus::Generating);
- assert!(panel.background_threads.is_empty());
+ assert!(panel.retained_threads.is_empty());
});
// Open a new thread B — thread A should be retained in background.
@@ -5571,13 +5833,13 @@ mod tests {
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
- panel.background_threads.len(),
+ panel.retained_threads.len(),
1,
- "Running thread A should be retained in background_views"
+ "Running thread A should be retained in retained_threads"
);
assert!(
- panel.background_threads.contains_key(&session_id_a),
- "Background view should be keyed by thread A's session ID"
+ panel.retained_threads.contains_key(&thread_id_a),
+ "Retained thread should be keyed by thread A's thread ID"
);
});
}
@@ -5596,7 +5858,7 @@ mod tests {
let weak_view_a = panel.read_with(&cx, |panel, _cx| {
panel.active_conversation_view().unwrap().downgrade()
});
- let session_id_a = active_session_id(&panel, &cx);
+ let thread_id_a = active_thread_id(&panel, &cx);
// Thread A should be idle (auto-completed via set_next_prompt_updates).
panel.read_with(&cx, |panel, cx| {
@@ -5610,13 +5872,13 @@ mod tests {
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
- panel.background_threads.len(),
+ panel.retained_threads.len(),
1,
- "Idle non-loadable thread A should be retained in background_views"
+ "Idle non-loadable thread A should be retained in retained_threads"
);
assert!(
- panel.background_threads.contains_key(&session_id_a),
- "Background view should be keyed by thread A's session ID"
+ panel.retained_threads.contains_key(&thread_id_a),
+ "Retained thread should be keyed by thread A's thread ID"
);
});
@@ -5635,6 +5897,7 @@ mod tests {
send_message(&panel, &mut cx);
let session_id_a = active_session_id(&panel, &cx);
+ let thread_id_a = active_thread_id(&panel, &cx);
// Keep thread A generating.
cx.update(|_, cx| {
@@ -5651,11 +5914,11 @@ mod tests {
open_thread_with_connection(&panel, connection_b, &mut cx);
send_message(&panel, &mut cx);
- let session_id_b = active_session_id(&panel, &cx);
+ let thread_id_b = active_thread_id(&panel, &cx);
panel.read_with(&cx, |panel, _cx| {
- assert_eq!(panel.background_threads.len(), 1);
- assert!(panel.background_threads.contains_key(&session_id_a));
+ assert_eq!(panel.retained_threads.len(), 1);
+ assert!(panel.retained_threads.contains_key(&thread_id_a));
});
// Load thread A back via load_agent_thread — should promote from background.
@@ -5680,18 +5943,18 @@ mod tests {
panel.read_with(&cx, |panel, _cx| {
assert!(
- !panel.background_threads.contains_key(&session_id_a),
- "Promoted thread A should no longer be in background_views"
+ !panel.retained_threads.contains_key(&thread_id_a),
+ "Promoted thread A should no longer be in retained_threads"
);
assert!(
- panel.background_threads.contains_key(&session_id_b),
- "Thread B (idle, non-loadable) should remain retained in background_views"
+ panel.retained_threads.contains_key(&thread_id_b),
+ "Thread B (idle, non-loadable) should remain retained in retained_threads"
);
});
}
#[gpui::test]
- async fn test_cleanup_background_threads_keeps_five_most_recent_idle_loadable_threads(
+ async fn test_cleanup_retained_threads_keeps_five_most_recent_idle_loadable_threads(
cx: &mut TestAppContext,
) {
let (panel, mut cx) = setup_panel(cx).await;
@@ -5700,13 +5963,13 @@ mod tests {
.with_agent_id("loadable-stub".into())
.with_telemetry_id("loadable-stub".into());
let mut session_ids = Vec::new();
+ let mut thread_ids = Vec::new();
for _ in 0..7 {
- session_ids.push(open_generating_thread_with_loadable_connection(
- &panel,
- &connection,
- &mut cx,
- ));
+ let (session_id, thread_id) =
+ open_generating_thread_with_loadable_connection(&panel, &connection, &mut cx);
+ session_ids.push(session_id);
+ thread_ids.push(thread_id);
}
let base_time = Instant::now();
@@ -5717,67 +5980,71 @@ mod tests {
cx.run_until_parked();
panel.update(&mut cx, |panel, cx| {
- for (index, session_id) in session_ids.iter().take(6).enumerate() {
+ for (index, thread_id) in thread_ids.iter().take(6).enumerate() {
let conversation_view = panel
- .background_threads
- .get(session_id)
- .expect("background thread should exist")
+ .retained_threads
+ .get(thread_id)
+ .expect("retained thread should exist")
.clone();
conversation_view.update(cx, |view, cx| {
view.set_updated_at(base_time + Duration::from_secs(index as u64), cx);
});
}
- panel.cleanup_background_threads(cx);
+ panel.cleanup_retained_threads(cx);
});
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
- panel.background_threads.len(),
+ panel.retained_threads.len(),
5,
- "cleanup should keep at most five idle loadable background threads"
+ "cleanup should keep at most five idle loadable retained threads"
);
assert!(
- !panel.background_threads.contains_key(&session_ids[0]),
- "oldest idle loadable background thread should be removed"
+ !panel.retained_threads.contains_key(&thread_ids[0]),
+ "oldest idle loadable retained thread should be removed"
);
- for session_id in &session_ids[1..6] {
+ for thread_id in &thread_ids[1..6] {
assert!(
- panel.background_threads.contains_key(session_id),
- "more recent idle loadable background threads should be retained"
+ panel.retained_threads.contains_key(thread_id),
+ "more recent idle loadable retained threads should be retained"
);
}
assert!(
- !panel.background_threads.contains_key(&session_ids[6]),
- "the active thread should not also be stored as a background thread"
+ !panel.retained_threads.contains_key(&thread_ids[6]),
+ "the active thread should not also be stored as a retained thread"
);
});
}
#[gpui::test]
- async fn test_cleanup_background_threads_preserves_idle_non_loadable_threads(
+ async fn test_cleanup_retained_threads_preserves_idle_non_loadable_threads(
cx: &mut TestAppContext,
) {
let (panel, mut cx) = setup_panel(cx).await;
let non_loadable_connection = StubAgentConnection::new();
- let non_loadable_session_id = open_idle_thread_with_non_loadable_connection(
- &panel,
- &non_loadable_connection,
- &mut cx,
- );
+ let (_non_loadable_session_id, non_loadable_thread_id) =
+ open_idle_thread_with_non_loadable_connection(
+ &panel,
+ &non_loadable_connection,
+ &mut cx,
+ );
let loadable_connection = StubAgentConnection::new()
.with_supports_load_session(true)
.with_agent_id("loadable-stub".into())
.with_telemetry_id("loadable-stub".into());
let mut loadable_session_ids = Vec::new();
+ let mut loadable_thread_ids = Vec::new();
for _ in 0..7 {
- loadable_session_ids.push(open_generating_thread_with_loadable_connection(
+ let (session_id, thread_id) = open_generating_thread_with_loadable_connection(
&panel,
&loadable_connection,
&mut cx,
- ));
+ );
+ loadable_session_ids.push(session_id);
+ loadable_thread_ids.push(thread_id);
}
let base_time = Instant::now();
@@ -5788,52 +6055,109 @@ mod tests {
cx.run_until_parked();
panel.update(&mut cx, |panel, cx| {
- for (index, session_id) in loadable_session_ids.iter().take(6).enumerate() {
+ for (index, thread_id) in loadable_thread_ids.iter().take(6).enumerate() {
let conversation_view = panel
- .background_threads
- .get(session_id)
- .expect("background thread should exist")
+ .retained_threads
+ .get(thread_id)
+ .expect("retained thread should exist")
.clone();
conversation_view.update(cx, |view, cx| {
view.set_updated_at(base_time + Duration::from_secs(index as u64), cx);
});
}
- panel.cleanup_background_threads(cx);
+ panel.cleanup_retained_threads(cx);
});
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
- panel.background_threads.len(),
+ panel.retained_threads.len(),
6,
"cleanup should keep the non-loadable idle thread in addition to five loadable ones"
);
assert!(
- panel
- .background_threads
- .contains_key(&non_loadable_session_id),
- "idle non-loadable background threads should not be cleanup candidates"
+ panel.retained_threads.contains_key(&non_loadable_thread_id),
+ "idle non-loadable retained threads should not be cleanup candidates"
);
assert!(
- !panel
- .background_threads
- .contains_key(&loadable_session_ids[0]),
- "oldest idle loadable background thread should still be removed"
+ !panel.retained_threads.contains_key(&loadable_thread_ids[0]),
+ "oldest idle loadable retained thread should still be removed"
);
- for session_id in &loadable_session_ids[1..6] {
+ for thread_id in &loadable_thread_ids[1..6] {
assert!(
- panel.background_threads.contains_key(session_id),
- "more recent idle loadable background threads should be retained"
+ panel.retained_threads.contains_key(thread_id),
+ "more recent idle loadable retained threads should be retained"
);
}
assert!(
- !panel
- .background_threads
- .contains_key(&loadable_session_ids[6]),
- "the active loadable thread should not also be stored as a background thread"
+ !panel.retained_threads.contains_key(&loadable_thread_ids[6]),
+ "the active loadable thread should not also be stored as a retained thread"
);
});
}
+ #[gpui::test]
+ async fn test_clear_active_thread_creates_real_empty_draft(cx: &mut TestAppContext) {
+ let (panel, mut cx) = setup_panel(cx).await;
+
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("done".into()),
+ )]);
+ open_thread_with_connection(&panel, connection, &mut cx);
+ send_message(&panel, &mut cx);
+
+ panel.read_with(&cx, |panel, cx| {
+ assert!(
+ panel.draft_thread_ids(cx).is_empty(),
+ "sent thread should not leave any draft entries before clearing"
+ );
+ });
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.clear_active_thread(window, cx);
+ });
+ cx.run_until_parked();
+
+ panel.read_with(&cx, |panel, cx| {
+ assert!(panel.active_thread_is_draft(cx));
+ assert_eq!(panel.draft_thread_ids(cx).len(), 1);
+ });
+ }
+
+ #[gpui::test]
+ async fn test_clear_active_thread_reuses_retained_empty_draft(cx: &mut TestAppContext) {
+ let (panel, mut cx) = setup_panel(cx).await;
+
+ let connection_a = StubAgentConnection::new();
+ connection_a.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("done".into()),
+ )]);
+ open_thread_with_connection(&panel, connection_a, &mut cx);
+ send_message(&panel, &mut cx);
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.new_thread(&NewThread, window, cx);
+ });
+ cx.run_until_parked();
+
+ let retained_draft_id = panel.read_with(&cx, |panel, cx| {
+ let ids = panel.draft_thread_ids(cx);
+ assert_eq!(ids.len(), 1);
+ ids[0]
+ });
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.clear_active_thread(window, cx);
+ });
+ cx.run_until_parked();
+
+ panel.read_with(&cx, |panel, cx| {
+ assert_eq!(panel.active_thread_id(cx), Some(retained_draft_id));
+ assert!(panel.active_thread_is_draft(cx));
+ assert_eq!(panel.draft_thread_ids(cx), vec![retained_draft_id]);
+ });
+ }
+
#[gpui::test]
async fn test_thread_target_local_project(cx: &mut TestAppContext) {
init_test(cx);
@@ -6176,38 +6500,13 @@ mod tests {
cx.run_until_parked();
- // Simulate worktree creation in progress and reset to Uninitialized
+ // set_active no longer creates threads — verify it's a no-op.
panel.update_in(cx, |panel, window, cx| {
- panel.worktree_creation_status =
- Some((EntityId::from(0u64), WorktreeCreationStatus::Creating));
- panel.active_view = ActiveView::Uninitialized;
+ panel.base_view = BaseView::Uninitialized;
Panel::set_active(panel, true, window, cx);
assert!(
- matches!(panel.active_view, ActiveView::Uninitialized),
- "set_active should not create a thread while worktree is being created"
- );
- });
-
- // Clear the creation status and use open_external_thread_with_server
- // (which bypasses new_agent_thread) to verify the panel can transition
- // out of Uninitialized. We can't call set_active directly because
- // new_agent_thread requires full agent server infrastructure.
- panel.update_in(cx, |panel, window, cx| {
- panel.worktree_creation_status = None;
- panel.active_view = ActiveView::Uninitialized;
- panel.open_external_thread_with_server(
- Rc::new(StubAgentServer::default_response()),
- window,
- cx,
- );
- });
-
- cx.run_until_parked();
-
- panel.read_with(cx, |panel, _cx| {
- assert!(
- !matches!(panel.active_view, ActiveView::Uninitialized),
- "panel should transition out of Uninitialized once worktree creation is cleared"
+ matches!(panel.base_view, BaseView::Uninitialized),
+ "set_active should not create a thread"
);
});
}
@@ -6506,11 +6805,12 @@ mod tests {
});
// Open thread A and send a message. With empty next_prompt_updates it
- // stays generating, so opening B will move A to background_threads.
+ // stays generating, so opening B will move A to retained_threads.
let connection_a = StubAgentConnection::new().with_agent_id("agent-a".into());
open_thread_with_custom_connection(&panel, connection_a.clone(), &mut cx);
send_message(&panel, &mut cx);
let session_id_a = active_session_id(&panel, &cx);
+ let thread_id_a = active_thread_id(&panel, &cx);
// Open thread C — thread A (generating) moves to background.
// Thread C completes immediately (idle), then opening B moves C to background too.
@@ -6520,24 +6820,25 @@ mod tests {
)]);
open_thread_with_custom_connection(&panel, connection_c.clone(), &mut cx);
send_message(&panel, &mut cx);
- let session_id_c = active_session_id(&panel, &cx);
+ let thread_id_c = active_thread_id(&panel, &cx);
// Open thread B — thread C (idle, non-loadable) is retained in background.
let connection_b = StubAgentConnection::new().with_agent_id("agent-b".into());
open_thread_with_custom_connection(&panel, connection_b.clone(), &mut cx);
send_message(&panel, &mut cx);
let session_id_b = active_session_id(&panel, &cx);
+ let _thread_id_b = active_thread_id(&panel, &cx);
let metadata_store = cx.update(|_, cx| ThreadMetadataStore::global(cx));
panel.read_with(&cx, |panel, _cx| {
assert!(
- panel.background_threads.contains_key(&session_id_a),
- "Thread A should be in background_threads"
+ panel.retained_threads.contains_key(&thread_id_a),
+ "Thread A should be in retained_threads"
);
assert!(
- panel.background_threads.contains_key(&session_id_c),
- "Thread C should be in background_threads"
+ panel.retained_threads.contains_key(&thread_id_c),
+ "Thread C should be in retained_threads"
);
});
@@ -6580,7 +6881,7 @@ mod tests {
// Verify thread A's (background) work_dirs are also updated.
let updated_a_paths = panel.read_with(&cx, |panel, cx| {
- let bg_view = panel.background_threads.get(&session_id_a).unwrap();
+ let bg_view = panel.retained_threads.get(&thread_id_a).unwrap();
let root_thread = bg_view.read(cx).root_thread(cx).unwrap();
root_thread
.read(cx)
@@ -6600,7 +6901,7 @@ mod tests {
// Verify thread idle C was also updated.
let updated_c_paths = panel.read_with(&cx, |panel, cx| {
- let bg_view = panel.background_threads.get(&session_id_c).unwrap();
+ let bg_view = panel.retained_threads.get(&thread_id_c).unwrap();
let root_thread = bg_view.read(cx).root_thread(cx).unwrap();
root_thread
.read(cx)
@@ -6623,7 +6924,7 @@ mod tests {
for (label, session_id) in [("thread B", &session_id_b), ("thread A", &session_id_a)] {
let metadata_paths = metadata_store.read_with(&cx, |store, _cx| {
let metadata = store
- .entry(session_id)
+ .entry_by_session(session_id)
.unwrap_or_else(|| panic!("{label} thread metadata should exist"));
metadata.folder_paths().clone()
});
@@ -6654,7 +6955,7 @@ mod tests {
);
let after_remove_a = panel.read_with(&cx, |panel, cx| {
- let bg_view = panel.background_threads.get(&session_id_a).unwrap();
+ let bg_view = panel.retained_threads.get(&thread_id_a).unwrap();
let root_thread = bg_view.read(cx).root_thread(cx).unwrap();
root_thread
.read(cx)
@@ -7374,61 +7675,48 @@ mod tests {
async fn test_selected_agent_syncs_when_navigating_between_threads(cx: &mut TestAppContext) {
let (panel, mut cx) = setup_panel(cx).await;
- let custom_agent = Agent::Custom {
- id: "my-custom-agent".into(),
- };
-
- // Create a draft thread with the custom agent.
- panel.update(&mut cx, |panel, _cx| {
- panel.selected_agent = custom_agent.clone();
- });
- panel.update_in(&mut cx, |panel, window, cx| {
- panel.new_thread(&NewThread, window, cx);
- });
- let draft_id = panel.read_with(&cx, |panel, _cx| {
- assert_eq!(panel.selected_agent, custom_agent);
- panel
- .active_draft_id()
- .expect("should have an active draft")
- });
-
- // Open a different thread (stub agent) — this navigates away from the draft.
- let connection = StubAgentConnection::new();
let stub_agent = Agent::Custom { id: "Test".into() };
- open_thread_with_connection(&panel, connection.clone(), &mut cx);
- let other_session_id = active_session_id(&panel, &cx);
- // Send a message so the thread is retained when we navigate away.
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("response".into()),
+ // Open thread A and send a message so it is retained.
+ let connection_a = StubAgentConnection::new();
+ connection_a.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("response a".into()),
)]);
+ open_thread_with_connection(&panel, connection_a, &mut cx);
+ let session_id_a = active_session_id(&panel, &cx);
send_message(&panel, &mut cx);
cx.run_until_parked();
panel.read_with(&cx, |panel, _cx| {
- assert_ne!(
- panel.selected_agent, custom_agent,
- "selected_agent should have changed to the stub agent"
- );
+ assert_eq!(panel.selected_agent, stub_agent);
});
- // Navigate back to the draft thread.
- panel.update_in(&mut cx, |panel, window, cx| {
- panel.activate_draft(draft_id, true, window, cx);
- });
+ // Open thread B with a different agent — thread A goes to retained.
+ let custom_agent = Agent::Custom {
+ id: "my-custom-agent".into(),
+ };
+ let connection_b = StubAgentConnection::new()
+ .with_agent_id("my-custom-agent".into())
+ .with_telemetry_id("my-custom-agent".into());
+ connection_b.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("response b".into()),
+ )]);
+ open_thread_with_custom_connection(&panel, connection_b, &mut cx);
+ send_message(&panel, &mut cx);
+ cx.run_until_parked();
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
panel.selected_agent, custom_agent,
- "selected_agent should sync back to the draft's agent"
+ "selected_agent should have changed to the custom agent"
);
});
- // Navigate to the other thread via load_agent_thread (simulating history click).
+ // Navigate back to thread A via load_agent_thread.
panel.update_in(&mut cx, |panel, window, cx| {
panel.load_agent_thread(
stub_agent.clone(),
- other_session_id,
+ session_id_a.clone(),
None,
None,
true,
@@ -7440,7 +7728,7 @@ mod tests {
panel.read_with(&cx, |panel, _cx| {
assert_eq!(
panel.selected_agent, stub_agent,
- "selected_agent should sync to the loaded thread's agent"
+ "selected_agent should sync back to thread A's agent"
);
});
}
diff --git a/crates/agent_ui/src/agent_ui.rs b/crates/agent_ui/src/agent_ui.rs
index 2e2e4018e3c..74145b4b384 100644
--- a/crates/agent_ui/src/agent_ui.rs
+++ b/crates/agent_ui/src/agent_ui.rs
@@ -65,9 +65,12 @@ use std::any::TypeId;
use workspace::Workspace;
use crate::agent_configuration::{ConfigureContextServerModal, ManageProfilesModal};
-pub use crate::agent_panel::{AgentPanel, AgentPanelEvent, DraftId, WorktreeCreationStatus};
+pub use crate::agent_panel::{
+ AgentPanel, AgentPanelEvent, MaxIdleRetainedThreads, WorktreeCreationStatus,
+};
use crate::agent_registry_ui::AgentRegistryPage;
pub use crate::inline_assistant::InlineAssistant;
+pub use crate::thread_metadata_store::ThreadId;
pub use agent_diff::{AgentDiffPane, AgentDiffToolbar};
pub use conversation_view::ConversationView;
pub use external_source_prompt::ExternalSourcePrompt;
@@ -79,7 +82,7 @@ pub(crate) use thread_history_view::*;
pub use thread_import::{AcpThreadImportOnboarding, ThreadImportModal};
use zed_actions;
-pub const DEFAULT_THREAD_TITLE: &str = "New Thread";
+pub const DEFAULT_THREAD_TITLE: &str = "New Agent Thread";
const PARALLEL_AGENT_LAYOUT_BACKFILL_KEY: &str = "parallel_agent_layout_backfilled";
actions!(
agent,
@@ -823,6 +826,7 @@ mod tests {
.unwrap();
cx.update(|cx| {
+ cx.set_global(db::AppDatabase::test_new());
let store = SettingsStore::test(cx);
cx.set_global(store);
AgentSettings::register(cx);
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs
index 9861a408694..33027076eff 100644
--- a/crates/agent_ui/src/conversation_view.rs
+++ b/crates/agent_ui/src/conversation_view.rs
@@ -82,6 +82,7 @@ use crate::entry_view_state::{EntryViewEvent, ViewEvent};
use crate::message_editor::{MessageEditor, MessageEditorEvent};
use crate::profile_selector::{ProfileProvider, ProfileSelector};
+use crate::thread_metadata_store::ThreadId;
use crate::ui::{AgentNotification, AgentNotificationEvent};
use crate::{
Agent, AgentDiffPane, AgentInitialContent, AgentPanel, AllowAlways, AllowOnce,
@@ -246,44 +247,46 @@ pub(crate) struct Conversation {
impl Conversation {
pub fn register_thread(&mut self, thread: Entity<AcpThread>, cx: &mut Context<Self>) {
let session_id = thread.read(cx).session_id().clone();
- let subscription = cx.subscribe(&thread, move |this, _thread, event, _cx| {
- this.updated_at = Some(Instant::now());
- match event {
- AcpThreadEvent::ToolAuthorizationRequested(id) => {
- this.permission_requests
- .entry(session_id.clone())
- .or_default()
- .push(id.clone());
- }
- AcpThreadEvent::ToolAuthorizationReceived(id) => {
- if let Some(tool_calls) = this.permission_requests.get_mut(&session_id) {
- tool_calls.retain(|tool_call_id| tool_call_id != id);
- if tool_calls.is_empty() {
- this.permission_requests.shift_remove(&session_id);
+ let subscription = cx.subscribe(&thread, {
+ let session_id = session_id.clone();
+ move |this, _thread, event, _cx| {
+ this.updated_at = Some(Instant::now());
+ match event {
+ AcpThreadEvent::ToolAuthorizationRequested(id) => {
+ this.permission_requests
+ .entry(session_id.clone())
+ .or_default()
+ .push(id.clone());
+ }
+ AcpThreadEvent::ToolAuthorizationReceived(id) => {
+ if let Some(tool_calls) = this.permission_requests.get_mut(&session_id) {
+ tool_calls.retain(|tool_call_id| tool_call_id != id);
+ if tool_calls.is_empty() {
+ this.permission_requests.shift_remove(&session_id);
+ }
}
}
+ AcpThreadEvent::NewEntry
+ | AcpThreadEvent::TitleUpdated
+ | AcpThreadEvent::TokenUsageUpdated
+ | AcpThreadEvent::EntryUpdated(_)
+ | AcpThreadEvent::EntriesRemoved(_)
+ | AcpThreadEvent::Retry(_)
+ | AcpThreadEvent::SubagentSpawned(_)
+ | AcpThreadEvent::Stopped(_)
+ | AcpThreadEvent::Error
+ | AcpThreadEvent::LoadError(_)
+ | AcpThreadEvent::PromptCapabilitiesUpdated
+ | AcpThreadEvent::Refusal
+ | AcpThreadEvent::AvailableCommandsUpdated(_)
+ | AcpThreadEvent::ModeUpdated(_)
+ | AcpThreadEvent::ConfigOptionsUpdated(_)
+ | AcpThreadEvent::WorkingDirectoriesUpdated => {}
}
- AcpThreadEvent::NewEntry
- | AcpThreadEvent::TitleUpdated
- | AcpThreadEvent::TokenUsageUpdated
- | AcpThreadEvent::EntryUpdated(_)
- | AcpThreadEvent::EntriesRemoved(_)
- | AcpThreadEvent::Retry(_)
- | AcpThreadEvent::SubagentSpawned(_)
- | AcpThreadEvent::Stopped(_)
- | AcpThreadEvent::Error
- | AcpThreadEvent::LoadError(_)
- | AcpThreadEvent::PromptCapabilitiesUpdated
- | AcpThreadEvent::Refusal
- | AcpThreadEvent::AvailableCommandsUpdated(_)
- | AcpThreadEvent::ModeUpdated(_)
- | AcpThreadEvent::ConfigOptionsUpdated(_)
- | AcpThreadEvent::WorkingDirectoriesUpdated => {}
}
});
self.subscriptions.push(subscription);
- self.threads
- .insert(thread.read(cx).session_id().clone(), thread);
+ self.threads.insert(session_id, thread);
}
pub fn pending_tool_call<'a>(
@@ -293,25 +296,21 @@ impl Conversation {
) -> Option<(acp::SessionId, acp::ToolCallId, &'a PermissionOptions)> {
let thread = self.threads.get(session_id)?;
let is_subagent = thread.read(cx).parent_session_id().is_some();
- let (thread, tool_id) = if is_subagent {
+ let (result_session_id, thread, tool_id) = if is_subagent {
let id = self.permission_requests.get(session_id)?.iter().next()?;
- (thread, id)
+ (session_id.clone(), thread, id)
} else {
let (id, tool_calls) = self.permission_requests.first()?;
let thread = self.threads.get(id)?;
- let id = tool_calls.iter().next()?;
- (thread, id)
+ let tool_id = tool_calls.iter().next()?;
+ (id.clone(), thread, tool_id)
};
let (_, tool_call) = thread.read(cx).tool_call(tool_id)?;
let ToolCallStatus::WaitingForConfirmation { options, .. } = &tool_call.status else {
return None;
};
- Some((
- thread.read(cx).session_id().clone(),
- tool_id.clone(),
- options,
- ))
+ Some((result_session_id, tool_id.clone(), options))
}
pub fn subagents_awaiting_permission(&self, cx: &App) -> Vec<(acp::SessionId, usize)> {
@@ -334,10 +333,11 @@ impl Conversation {
kind: acp::PermissionOptionKind,
cx: &mut Context<Self>,
) -> Option<()> {
- let (_, tool_call_id, options) = self.pending_tool_call(session_id, cx)?;
+ let (authorize_session_id, tool_call_id, options) =
+ self.pending_tool_call(session_id, cx)?;
let option = options.first_option_of_kind(kind)?;
self.authorize_tool_call(
- session_id.clone(),
+ authorize_session_id,
tool_call_id,
SelectedPermissionOutcome::new(option.option_id.clone(), option.kind),
cx,
@@ -356,6 +356,7 @@ impl Conversation {
return;
};
let agent_telemetry_id = thread.read(cx).connection().telemetry_id();
+ let session_id = thread.read(cx).session_id().clone();
telemetry::event!(
"Agent Tool Call Authorized",
@@ -379,6 +380,34 @@ impl Conversation {
}
}
+pub(crate) struct RootThreadUpdated;
+
+impl EventEmitter<RootThreadUpdated> for ConversationView {}
+
+fn affects_thread_metadata(event: &AcpThreadEvent) -> bool {
+ match event {
+ AcpThreadEvent::NewEntry
+ | AcpThreadEvent::TitleUpdated
+ | AcpThreadEvent::EntryUpdated(_)
+ | AcpThreadEvent::EntriesRemoved(_)
+ | AcpThreadEvent::ToolAuthorizationRequested(_)
+ | AcpThreadEvent::ToolAuthorizationReceived(_)
+ | AcpThreadEvent::Retry(_)
+ | AcpThreadEvent::Stopped(_)
+ | AcpThreadEvent::Error
+ | AcpThreadEvent::LoadError(_)
+ | AcpThreadEvent::Refusal
+ | AcpThreadEvent::WorkingDirectoriesUpdated => true,
+ // --
+ AcpThreadEvent::TokenUsageUpdated
+ | AcpThreadEvent::PromptCapabilitiesUpdated
+ | AcpThreadEvent::AvailableCommandsUpdated(_)
+ | AcpThreadEvent::ModeUpdated(_)
+ | AcpThreadEvent::ConfigOptionsUpdated(_)
+ | AcpThreadEvent::SubagentSpawned(_) => false,
+ }
+}
+
pub enum AcpServerViewEvent {
ActiveThreadChanged,
}
@@ -394,12 +423,17 @@ pub struct ConversationView {
project: Entity<Project>,
thread_store: Option<Entity<ThreadStore>>,
prompt_store: Option<Entity<PromptStore>>,
+ pub(crate) thread_id: ThreadId,
+ root_session_id: Option<acp::SessionId>,
server_state: ServerState,
focus_handle: FocusHandle,
notifications: Vec<WindowHandle<AgentNotification>>,
notification_subscriptions: HashMap<WindowHandle<AgentNotification>, Vec<Subscription>>,
auth_task: Option<Task<()>>,
_subscriptions: Vec<Subscription>,
+ /// True when this conversation was created as a new draft (no resume
+ /// session). False when resuming an existing session from history.
+ is_new_draft: bool,
}
impl ConversationView {
@@ -409,6 +443,10 @@ impl ConversationView {
})
}
+ pub fn is_new_draft(&self) -> bool {
+ self.is_new_draft
+ }
+
pub fn active_thread(&self) -> Option<&Entity<ThreadView>> {
match &self.server_state {
ServerState::Connected(connected) => connected.active_view(),
@@ -420,23 +458,29 @@ impl ConversationView {
&'a self,
cx: &'a App,
) -> Option<(acp::SessionId, acp::ToolCallId, &'a PermissionOptions)> {
- let id = &self.active_thread()?.read(cx).id;
+ let session_id = self
+ .active_thread()?
+ .read(cx)
+ .thread
+ .read(cx)
+ .session_id()
+ .clone();
self.as_connected()?
.conversation
.read(cx)
- .pending_tool_call(id, cx)
+ .pending_tool_call(&session_id, cx)
}
pub fn root_thread_has_pending_tool_call(&self, cx: &App) -> bool {
let Some(root_thread) = self.root_thread(cx) else {
return false;
};
- let root_id = root_thread.read(cx).id.clone();
+ let root_session_id = root_thread.read(cx).thread.read(cx).session_id().clone();
self.as_connected().is_some_and(|connected| {
connected
.conversation
.read(cx)
- .pending_tool_call(&root_id, cx)
+ .pending_tool_call(&root_session_id, cx)
.is_some()
})
}
@@ -445,8 +489,10 @@ impl ConversationView {
match &self.server_state {
ServerState::Connected(connected) => {
let mut current = connected.active_view()?;
- while let Some(parent_id) = current.read(cx).parent_id.clone() {
- if let Some(parent) = connected.threads.get(&parent_id) {
+ while let Some(parent_session_id) =
+ current.read(cx).thread.read(cx).parent_session_id()
+ {
+ if let Some(parent) = connected.threads.get(parent_session_id) {
current = parent;
} else {
break;
@@ -458,7 +504,28 @@ impl ConversationView {
}
}
- pub fn thread_view(&self, session_id: &acp::SessionId) -> Option<Entity<ThreadView>> {
+ pub(crate) fn root_acp_thread(&self, cx: &App) -> Option<Entity<AcpThread>> {
+ let connected = self.as_connected()?;
+ let root_session_id = self.root_session_id.as_ref()?;
+ connected
+ .conversation
+ .read(cx)
+ .threads
+ .get(root_session_id)
+ .cloned()
+ }
+
+ pub fn root_thread_view(&self, cx: &App) -> Option<Entity<ThreadView>> {
+ self.root_session_id
+ .as_ref()
+ .and_then(|sid| self.thread_view(sid, cx))
+ }
+
+ pub fn thread_view(
+ &self,
+ session_id: &acp::SessionId,
+ _cx: &App,
+ ) -> Option<Entity<ThreadView>> {
let connected = self.as_connected()?;
connected.threads.get(session_id).cloned()
}
@@ -482,7 +549,7 @@ impl ConversationView {
.and_then(|connected| connected.conversation.read(cx).updated_at)
}
- pub fn navigate_to_session(
+ pub fn navigate_to_thread(
&mut self,
session_id: acp::SessionId,
window: &mut Window,
@@ -492,7 +559,7 @@ impl ConversationView {
return;
};
- connected.navigate_to_session(session_id);
+ connected.navigate_to_thread(session_id);
if let Some(view) = self.active_thread() {
view.focus_handle(cx).focus(window, cx);
}
@@ -510,11 +577,8 @@ impl ConversationView {
}
enum ServerState {
- Loading(Entity<LoadingView>),
- LoadError {
- error: LoadError,
- session_id: Option<acp::SessionId>,
- },
+ Loading { _loading: Entity<LoadingView> },
+ LoadError { error: LoadError },
Connected(ConnectedServerState),
}
@@ -547,7 +611,6 @@ impl AuthState {
}
struct LoadingView {
- session_id: Option<acp::SessionId>,
_load_task: Task<()>,
}
@@ -561,16 +624,17 @@ impl ConnectedServerState {
.map_or(false, |view| view.read(cx).thread_error.is_some())
}
- pub fn navigate_to_session(&mut self, session_id: acp::SessionId) {
+ pub fn navigate_to_thread(&mut self, session_id: acp::SessionId) {
if self.threads.contains_key(&session_id) {
self.active_id = Some(session_id);
}
}
pub fn close_all_sessions(&self, cx: &mut App) -> Task<()> {
- let tasks = self.threads.keys().filter_map(|id| {
+ let tasks = self.threads.values().filter_map(|view| {
if self.connection.supports_close_session() {
- Some(self.connection.clone().close_session(id, cx))
+ let session_id = view.read(cx).thread.read(cx).session_id().clone();
+ Some(self.connection.clone().close_session(&session_id, cx))
} else {
None
}
@@ -588,6 +652,7 @@ impl ConversationView {
connection_store: Entity<AgentConnectionStore>,
connection_key: Agent,
resume_session_id: Option<acp::SessionId>,
+ thread_id: Option<ThreadId>,
work_dirs: Option<PathList>,
title: Option<SharedString>,
initial_content: Option<AgentInitialContent>,
@@ -623,6 +688,9 @@ impl ConversationView {
})
.detach();
+ let is_new_draft = resume_session_id.is_none();
+ let thread_id = thread_id.unwrap_or_else(ThreadId::new);
+
Self {
agent: agent.clone(),
connection_store: connection_store.clone(),
@@ -632,6 +700,8 @@ impl ConversationView {
project: project.clone(),
thread_store,
prompt_store,
+ thread_id,
+ root_session_id: None,
server_state: Self::initial_state(
agent.clone(),
connection_store,
@@ -649,6 +719,7 @@ impl ConversationView {
auth_task: None,
_subscriptions: subscriptions,
focus_handle: cx.focus_handle(),
+ is_new_draft,
}
}
@@ -666,7 +737,8 @@ impl ConversationView {
let (resume_session_id, cwd, title) = self
.active_thread()
.map(|thread_view| {
- let thread = thread_view.read(cx).thread.read(cx);
+ let tv = thread_view.read(cx);
+ let thread = tv.thread.read(cx);
(
Some(thread.session_id().clone()),
thread.work_dirs().cloned(),
@@ -718,7 +790,6 @@ impl ConversationView {
error: LoadError::Other(
"External agents are not yet supported in shared projects.".into(),
),
- session_id: resume_session_id.clone(),
};
}
let session_work_dirs = work_dirs.unwrap_or_else(|| project.read(cx).default_path_list(cx));
@@ -741,7 +812,6 @@ impl ConversationView {
let connect_result = connection_entry.read(cx).wait_for_connection();
- let load_session_id = resume_session_id.clone();
let load_task = cx.spawn_in(window, async move |this, cx| {
let (connection, history) = match connect_result.await {
Ok(AgentConnectedState {
@@ -750,7 +820,7 @@ impl ConversationView {
}) => (connection, history),
Err(err) => {
this.update_in(cx, |this, window, cx| {
- this.handle_load_error(load_session_id.clone(), err, window, cx);
+ this.handle_load_error(err, window, cx);
cx.notify();
})
.log_err();
@@ -761,7 +831,7 @@ impl ConversationView {
telemetry::event!("Agent Thread Started", agent = connection.telemetry_id());
let mut resumed_without_history = false;
- let result = if let Some(session_id) = load_session_id.clone() {
+ let result = if let Some(session_id) = resume_session_id.clone() {
cx.update(|_, cx| {
if connection.supports_load_session() {
connection.clone().load_session(
@@ -824,6 +894,8 @@ impl ConversationView {
this.update_in(cx, |this, window, cx| {
match result {
Ok(thread) => {
+ let root_session_id = thread.read(cx).session_id().clone();
+
let conversation = cx.new(|cx| {
let mut conversation = Conversation::default();
conversation.register_thread(thread.clone(), cx);
@@ -831,7 +903,6 @@ impl ConversationView {
});
let current = this.new_thread_view(
- None,
thread,
conversation.clone(),
resumed_without_history,
@@ -849,13 +920,13 @@ impl ConversationView {
.focus(window, cx);
}
- let id = current.read(cx).thread.read(cx).session_id().clone();
+ this.root_session_id = Some(root_session_id.clone());
this.set_server_state(
ServerState::Connected(ConnectedServerState {
connection,
auth_state: AuthState::Ok,
- active_id: Some(id.clone()),
- threads: HashMap::from_iter([(id, current)]),
+ active_id: Some(root_session_id.clone()),
+ threads: HashMap::from_iter([(root_session_id, current)]),
conversation,
history,
_connection_entry_subscription: connection_entry_subscription,
@@ -865,7 +936,6 @@ impl ConversationView {
}
Err(err) => {
this.handle_load_error(
- load_session_id.clone(),
LoadError::Other(err.to_string().into()),
window,
cx,
@@ -877,16 +947,16 @@ impl ConversationView {
});
let loading_view = cx.new(|_cx| LoadingView {
- session_id: resume_session_id,
_load_task: load_task,
});
- ServerState::Loading(loading_view)
+ ServerState::Loading {
+ _loading: loading_view,
+ }
}
fn new_thread_view(
&self,
- parent_id: Option<acp::SessionId>,
thread: Entity<AcpThread>,
conversation: Entity<Conversation>,
resumed_without_history: bool,
@@ -990,7 +1060,6 @@ impl ConversationView {
cx.observe(&action_log, |_, _, cx| cx.notify()),
];
- let parent_session_id = thread.read(cx).session_id().clone();
let subagent_sessions = thread
.read(cx)
.entries()
@@ -1005,6 +1074,7 @@ impl ConversationView {
.collect::<Vec<_>>();
if !subagent_sessions.is_empty() {
+ let parent_session_id = thread.read(cx).session_id().clone();
cx.spawn_in(window, async move |this, cx| {
this.update_in(cx, |this, window, cx| {
for subagent_id in subagent_sessions {
@@ -1058,7 +1128,6 @@ impl ConversationView {
let weak = cx.weak_entity();
cx.new(|cx| {
ThreadView::new(
- parent_id,
thread,
conversation,
weak,
@@ -1170,13 +1239,7 @@ impl ConversationView {
.ok();
}
- fn handle_load_error(
- &mut self,
- session_id: Option<acp::SessionId>,
- err: LoadError,
- window: &mut Window,
- cx: &mut Context<Self>,
- ) {
+ fn handle_load_error(&mut self, err: LoadError, window: &mut Window, cx: &mut Context<Self>) {
if let Some(view) = self.active_thread() {
if view
.read(cx)
@@ -1188,13 +1251,7 @@ impl ConversationView {
}
}
self.emit_load_error_telemetry(&err);
- self.set_server_state(
- ServerState::LoadError {
- error: err,
- session_id,
- },
- cx,
- );
+ self.set_server_state(ServerState::LoadError { error: err }, cx);
}
fn handle_agent_servers_updated(
@@ -1208,7 +1265,7 @@ impl ConversationView {
// when agent.connect() fails during loading), retry loading the thread.
// This handles the case where a thread is restored before authentication completes.
let should_retry = match &self.server_state {
- ServerState::Loading(_) => false,
+ ServerState::Loading { .. } => false,
ServerState::LoadError { .. } => true,
ServerState::Connected(connected) => {
connected.auth_state.is_ok() && connected.has_thread_error(cx)
@@ -1239,7 +1296,7 @@ impl ConversationView {
.active_view()
.and_then(|v| v.read(cx).thread.read(cx).title())
.unwrap_or_else(|| DEFAULT_THREAD_TITLE.into()),
- ServerState::Loading(_) => "Loading…".into(),
+ ServerState::Loading { .. } => "Loading…".into(),
ServerState::LoadError { error, .. } => match error {
LoadError::Unsupported { .. } => {
format!("Upgrade {}", self.agent.agent_id()).into()
@@ -1261,15 +1318,8 @@ impl ConversationView {
}
}
- // The parent ID is None if we haven't created a thread yet
- pub fn parent_id(&self, cx: &App) -> Option<acp::SessionId> {
- match &self.server_state {
- ServerState::Connected(_) => self
- .root_thread(cx)
- .map(|thread| thread.read(cx).id.clone()),
- ServerState::Loading(loading) => loading.read(cx).session_id.clone(),
- ServerState::LoadError { session_id, .. } => session_id.clone(),
- }
+ pub fn parent_id(&self) -> ThreadId {
+ self.thread_id
}
pub fn is_loading(&self) -> bool {
@@ -1326,13 +1376,22 @@ impl ConversationView {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let thread_id = thread.read(cx).session_id().clone();
+ let session_id = thread.read(cx).session_id().clone();
+ let has_thread = self
+ .as_connected()
+ .is_some_and(|connected| connected.threads.contains_key(&session_id));
+ if !has_thread {
+ return;
+ };
let is_subagent = thread.read(cx).parent_session_id().is_some();
+ if !is_subagent && affects_thread_metadata(event) {
+ cx.emit(RootThreadUpdated);
+ }
match event {
AcpThreadEvent::NewEntry => {
let len = thread.read(cx).entries().len();
let index = len - 1;
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
let entry_view_state = active.read(cx).entry_view_state.clone();
let list_state = active.read(cx).list_state.clone();
entry_view_state.update(cx, |view_state, cx| {
@@ -1350,7 +1409,7 @@ impl ConversationView {
}
}
AcpThreadEvent::EntryUpdated(index) => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
let entry_view_state = active.read(cx).entry_view_state.clone();
let list_state = active.read(cx).list_state.clone();
entry_view_state.update(cx, |view_state, cx| {
@@ -1363,7 +1422,7 @@ impl ConversationView {
}
}
AcpThreadEvent::EntriesRemoved(range) => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
let entry_view_state = active.read(cx).entry_view_state.clone();
let list_state = active.read(cx).list_state.clone();
entry_view_state.update(cx, |view_state, _cx| view_state.remove(range.clone()));
@@ -1373,25 +1432,22 @@ impl ConversationView {
});
}
}
- AcpThreadEvent::SubagentSpawned(session_id) => self.load_subagent_session(
- session_id.clone(),
- thread.read(cx).session_id().clone(),
- window,
- cx,
- ),
+ AcpThreadEvent::SubagentSpawned(subagent_session_id) => {
+ self.load_subagent_session(subagent_session_id.clone(), session_id, window, cx)
+ }
AcpThreadEvent::ToolAuthorizationRequested(_) => {
self.notify_with_sound("Waiting for tool confirmation", IconName::Info, window, cx);
}
AcpThreadEvent::ToolAuthorizationReceived(_) => {}
AcpThreadEvent::Retry(retry) => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
active.update(cx, |active, _cx| {
active.thread_retry_status = Some(retry.clone());
});
}
}
AcpThreadEvent::Stopped(stop_reason) => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
let is_generating =
matches!(thread.read(cx).status(), ThreadStatus::Generating);
active.update(cx, |active, cx| {
@@ -1455,7 +1511,7 @@ impl ConversationView {
}
AcpThreadEvent::Refusal => {
let error = ThreadError::Refusal;
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
active.update(cx, |active, cx| {
active.handle_thread_error(error, cx);
active.thread_retry_status.take();
@@ -1469,7 +1525,7 @@ impl ConversationView {
}
}
AcpThreadEvent::Error => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
let is_generating =
matches!(thread.read(cx).status(), ThreadStatus::Generating);
active.update(cx, |active, cx| {
@@ -1505,14 +1561,13 @@ impl ConversationView {
self.set_server_state(
ServerState::LoadError {
error: error.clone(),
- session_id: Some(thread_id),
},
cx,
);
}
AcpThreadEvent::TitleUpdated => {
if let Some(title) = thread.read(cx).title()
- && let Some(active_thread) = self.thread_view(&thread_id)
+ && let Some(active_thread) = self.thread_view(&session_id, cx)
{
let title_editor = active_thread.read(cx).title_editor.clone();
title_editor.update(cx, |editor, cx| {
@@ -1524,7 +1579,7 @@ impl ConversationView {
cx.notify();
}
AcpThreadEvent::PromptCapabilitiesUpdated => {
- if let Some(active) = self.thread_view(&thread_id) {
+ if let Some(active) = self.thread_view(&session_id, cx) {
active.update(cx, |active, _cx| {
active
.session_capabilities
@@ -1538,7 +1593,7 @@ impl ConversationView {
self.emit_token_limit_telemetry_if_needed(thread, cx);
}
AcpThreadEvent::AvailableCommandsUpdated(available_commands) => {
- if let Some(thread_view) = self.thread_view(&thread_id) {
+ if let Some(thread_view) = self.thread_view(&session_id, cx) {
let has_commands = !available_commands.is_empty();
let agent_display_name = self
@@ -1716,7 +1771,7 @@ impl ConversationView {
fn load_subagent_session(
&mut self,
subagent_id: acp::SessionId,
- parent_id: acp::SessionId,
+ parent_session_id: acp::SessionId,
window: &mut Window,
cx: &mut Context<Self>,
) {
@@ -1728,7 +1783,7 @@ impl ConversationView {
{
return;
}
- let Some(parent_thread) = connected.threads.get(&parent_id) else {
+ let Some(parent_thread) = connected.threads.get(&parent_session_id) else {
return;
};
let work_dirs = parent_thread
@@ -1740,7 +1795,7 @@ impl ConversationView {
.unwrap_or_else(|| self.project.read(cx).default_path_list(cx));
let subagent_thread_task = connected.connection.clone().load_session(
- subagent_id.clone(),
+ subagent_id,
self.project.clone(),
work_dirs,
None,
@@ -1756,11 +1811,11 @@ impl ConversationView {
else {
return;
};
+ let subagent_session_id = subagent_thread.read(cx).session_id().clone();
conversation.update(cx, |conversation, cx| {
conversation.register_thread(subagent_thread.clone(), cx);
});
let view = this.new_thread_view(
- Some(parent_id),
subagent_thread,
conversation,
false,
@@ -1772,7 +1827,7 @@ impl ConversationView {
let Some(connected) = this.as_connected_mut() else {
return;
};
- connected.threads.insert(subagent_id, view);
+ connected.threads.insert(subagent_session_id, view);
})
})
.detach();
@@ -3095,6 +3150,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace.downgrade(),
project,
Some(thread_store),
@@ -3230,6 +3286,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace.downgrade(),
project,
Some(thread_store),
@@ -3307,6 +3364,7 @@ pub(crate) mod tests {
connection_store,
Agent::Custom { id: "Test".into() },
Some(SessionId::new("session-1")),
+ None,
Some(PathList::new(&[PathBuf::from("/project/subdir")])),
None,
None,
@@ -3382,7 +3440,7 @@ pub(crate) mod tests {
other => panic!(
"Expected LoadError::Other, got: {}",
match other {
- ServerState::Loading(_) => "Loading (stuck!)",
+ ServerState::Loading { .. } => "Loading (stuck!)",
ServerState::LoadError { .. } => "LoadError (wrong variant)",
ServerState::Connected(_) => "Connected",
}
@@ -3631,6 +3689,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace.downgrade(),
project.clone(),
Some(thread_store),
@@ -3742,6 +3801,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace1.downgrade(),
project1.clone(),
Some(thread_store),
@@ -3987,6 +4047,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
initial_content,
workspace.downgrade(),
project,
@@ -4757,6 +4818,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace.downgrade(),
project.clone(),
Some(thread_store.clone()),
@@ -6769,6 +6831,7 @@ pub(crate) mod tests {
let project = Project::test(fs, [], cx).await;
let connection: Rc<dyn AgentConnection> = Rc::new(StubAgentConnection::new());
+ let session_id = acp::SessionId::new("session-1");
let (thread, conversation) = cx.update(|cx| {
let thread =
create_test_acp_thread(None, "session-1", connection.clone(), project.clone(), cx);
@@ -6784,7 +6847,6 @@ pub(crate) mod tests {
let _task2 = request_test_tool_authorization(&thread, "tc-2", "allow-2", cx);
cx.read(|cx| {
- let session_id = acp::SessionId::new("session-1");
let (_, tool_call_id, _) = conversation
.read(cx)
.pending_tool_call(&session_id, cx)
@@ -6795,7 +6857,7 @@ pub(crate) mod tests {
cx.update(|cx| {
conversation.update(cx, |conversation, cx| {
conversation.authorize_tool_call(
- acp::SessionId::new("session-1"),
+ session_id.clone(),
acp::ToolCallId::new("tc-1"),
SelectedPermissionOutcome::new(
acp::PermissionOptionId::new("allow-1"),
@@ -6809,7 +6871,6 @@ pub(crate) mod tests {
cx.run_until_parked();
cx.read(|cx| {
- let session_id = acp::SessionId::new("session-1");
let (_, tool_call_id, _) = conversation
.read(cx)
.pending_tool_call(&session_id, cx)
@@ -6820,7 +6881,7 @@ pub(crate) mod tests {
cx.update(|cx| {
conversation.update(cx, |conversation, cx| {
conversation.authorize_tool_call(
- acp::SessionId::new("session-1"),
+ session_id.clone(),
acp::ToolCallId::new("tc-2"),
SelectedPermissionOutcome::new(
acp::PermissionOptionId::new("allow-2"),
@@ -6834,7 +6895,6 @@ pub(crate) mod tests {
cx.run_until_parked();
cx.read(|cx| {
- let session_id = acp::SessionId::new("session-1");
assert!(
conversation
.read(cx)
@@ -6853,6 +6913,8 @@ pub(crate) mod tests {
let project = Project::test(fs, [], cx).await;
let connection: Rc<dyn AgentConnection> = Rc::new(StubAgentConnection::new());
+ let parent_session_id = acp::SessionId::new("parent");
+ let subagent_session_id = acp::SessionId::new("subagent");
let (parent_thread, subagent_thread, conversation) = cx.update(|cx| {
let parent_thread =
create_test_acp_thread(None, "parent", connection.clone(), project.clone(), cx);
@@ -6880,24 +6942,22 @@ pub(crate) mod tests {
// Querying with the subagent's session ID returns only the
// subagent's own tool call (subagent path is scoped to its session)
cx.read(|cx| {
- let subagent_id = acp::SessionId::new("subagent");
- let (session_id, tool_call_id, _) = conversation
+ let (returned_session_id, tool_call_id, _) = conversation
.read(cx)
- .pending_tool_call(&subagent_id, cx)
+ .pending_tool_call(&subagent_session_id, cx)
.expect("Expected subagent's pending tool call");
- assert_eq!(session_id, acp::SessionId::new("subagent"));
+ assert_eq!(returned_session_id, subagent_session_id);
assert_eq!(tool_call_id, acp::ToolCallId::new("subagent-tc"));
});
// Querying with the parent's session ID returns the first pending
// request in FIFO order across all sessions
cx.read(|cx| {
- let parent_id = acp::SessionId::new("parent");
- let (session_id, tool_call_id, _) = conversation
+ let (returned_session_id, tool_call_id, _) = conversation
.read(cx)
- .pending_tool_call(&parent_id, cx)
+ .pending_tool_call(&parent_session_id, cx)
.expect("Expected a pending tool call from parent query");
- assert_eq!(session_id, acp::SessionId::new("parent"));
+ assert_eq!(returned_session_id, parent_session_id);
assert_eq!(tool_call_id, acp::ToolCallId::new("parent-tc"));
});
}
@@ -6912,6 +6972,8 @@ pub(crate) mod tests {
let project = Project::test(fs, [], cx).await;
let connection: Rc<dyn AgentConnection> = Rc::new(StubAgentConnection::new());
+ let session_id_a = acp::SessionId::new("thread-a");
+ let session_id_b = acp::SessionId::new("thread-b");
let (thread_a, thread_b, conversation) = cx.update(|cx| {
let thread_a =
create_test_acp_thread(None, "thread-a", connection.clone(), project.clone(), cx);
@@ -6932,26 +6994,23 @@ pub(crate) mod tests {
// Both threads are non-subagent, so pending_tool_call always returns
// the first entry from permission_requests (FIFO across all sessions)
cx.read(|cx| {
- let session_a = acp::SessionId::new("thread-a");
- let (session_id, tool_call_id, _) = conversation
+ let (returned_session_id, tool_call_id, _) = conversation
.read(cx)
- .pending_tool_call(&session_a, cx)
+ .pending_tool_call(&session_id_a, cx)
.expect("Expected a pending tool call");
- assert_eq!(session_id, acp::SessionId::new("thread-a"));
+ assert_eq!(returned_session_id, session_id_a);
assert_eq!(tool_call_id, acp::ToolCallId::new("tc-a"));
});
// Querying with thread-b also returns thread-a's tool call,
// because non-subagent queries always use permission_requests.first()
cx.read(|cx| {
- let session_b = acp::SessionId::new("thread-b");
- let (session_id, tool_call_id, _) = conversation
+ let (returned_session_id, tool_call_id, _) = conversation
.read(cx)
- .pending_tool_call(&session_b, cx)
+ .pending_tool_call(&session_id_b, cx)
.expect("Expected a pending tool call from thread-b query");
assert_eq!(
- session_id,
- acp::SessionId::new("thread-a"),
+ returned_session_id, session_id_a,
"Non-subagent queries always return the first pending request in FIFO order"
);
assert_eq!(tool_call_id, acp::ToolCallId::new("tc-a"));
@@ -6961,7 +7020,7 @@ pub(crate) mod tests {
cx.update(|cx| {
conversation.update(cx, |conversation, cx| {
conversation.authorize_tool_call(
- acp::SessionId::new("thread-a"),
+ session_id_a.clone(),
acp::ToolCallId::new("tc-a"),
SelectedPermissionOutcome::new(
acp::PermissionOptionId::new("allow-a"),
@@ -6975,12 +7034,11 @@ pub(crate) mod tests {
cx.run_until_parked();
cx.read(|cx| {
- let session_b = acp::SessionId::new("thread-b");
- let (session_id, tool_call_id, _) = conversation
+ let (returned_session_id, tool_call_id, _) = conversation
.read(cx)
- .pending_tool_call(&session_b, cx)
+ .pending_tool_call(&session_id_b, cx)
.expect("Expected thread-b's tool call after thread-a's was authorized");
- assert_eq!(session_id, acp::SessionId::new("thread-b"));
+ assert_eq!(returned_session_id, session_id_b);
assert_eq!(tool_call_id, acp::ToolCallId::new("tc-b"));
});
}
@@ -7092,6 +7150,7 @@ pub(crate) mod tests {
None,
None,
None,
+ None,
workspace.downgrade(),
project,
Some(thread_store),
@@ -7183,12 +7242,12 @@ pub(crate) mod tests {
!connected.connection.supports_close_session(),
"StubAgentConnection should not support close"
);
- let session_id = connected
+ let thread_view = connected
.threads
- .keys()
+ .values()
.next()
- .expect("Should have at least one thread")
- .clone();
+ .expect("Should have at least one thread");
+ let session_id = thread_view.read(cx).thread.read(cx).session_id().clone();
connected.connection.clone().close_session(&session_id, cx)
})
.await;
diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs
index a3e36cdafbf..bd0cfb13acc 100644
--- a/crates/agent_ui/src/conversation_view/thread_view.rs
+++ b/crates/agent_ui/src/conversation_view/thread_view.rs
@@ -10,6 +10,7 @@ use editor::actions::OpenExcerpts;
use crate::StartThreadIn;
use crate::message_editor::SharedSessionCapabilities;
+
use gpui::{Corner, List};
use heapless::Vec as ArrayVec;
use language_model::{LanguageModelEffortLevel, Speed};
@@ -262,8 +263,8 @@ impl PermissionSelection {
}
pub struct ThreadView {
- pub id: acp::SessionId,
- pub parent_id: Option<acp::SessionId>,
+ pub session_id: acp::SessionId,
+ pub parent_session_id: Option<acp::SessionId>,
pub thread: Entity<AcpThread>,
pub(crate) conversation: Entity<super::Conversation>,
pub server_view: WeakEntity<ConversationView>,
@@ -294,7 +295,7 @@ pub struct ThreadView {
pub expanded_thinking_blocks: HashSet<(usize, usize)>,
auto_expanded_thinking_block: Option<(usize, usize)>,
user_toggled_thinking_blocks: HashSet<(usize, usize)>,
- pub subagent_scroll_handles: RefCell<HashMap<agent_client_protocol::SessionId, ScrollHandle>>,
+ pub subagent_scroll_handles: RefCell<HashMap<acp::SessionId, ScrollHandle>>,
pub edits_expanded: bool,
pub plan_expanded: bool,
pub queue_expanded: bool,
@@ -337,7 +338,7 @@ pub struct ThreadView {
}
impl Focusable for ThreadView {
fn focus_handle(&self, cx: &App) -> FocusHandle {
- if self.parent_id.is_some() {
+ if self.parent_session_id.is_some() {
self.focus_handle.clone()
} else {
self.active_editor(cx).focus_handle(cx)
@@ -357,7 +358,6 @@ pub struct TurnFields {
impl ThreadView {
pub(crate) fn new(
- parent_id: Option<acp::SessionId>,
thread: Entity<AcpThread>,
conversation: Entity<super::Conversation>,
server_view: WeakEntity<ConversationView>,
@@ -383,7 +383,8 @@ impl ThreadView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
- let id = thread.read(cx).session_id().clone();
+ let session_id = thread.read(cx).session_id().clone();
+ let parent_session_id = thread.read(cx).parent_session_id().cloned();
let has_commands = !session_capabilities.read().available_commands().is_empty();
let placeholder = placeholder_text(agent_display_name.as_ref(), has_commands);
@@ -507,8 +508,8 @@ impl ThreadView {
.unwrap_or_default();
let mut this = Self {
- id,
- parent_id,
+ session_id,
+ parent_session_id,
focus_handle: cx.focus_handle(),
thread,
conversation,
@@ -644,6 +645,10 @@ impl ThreadView {
}
}
+ pub fn is_draft(&self, cx: &App) -> bool {
+ self.thread.read(cx).entries().is_empty()
+ }
+
pub(crate) fn as_native_connection(
&self,
cx: &App,
@@ -692,7 +697,7 @@ impl ThreadView {
}
fn is_subagent(&self) -> bool {
- self.parent_id.is_some()
+ self.parent_session_id.is_some()
}
/// Returns the currently active editor, either for a message that is being
@@ -1739,8 +1744,9 @@ impl ThreadView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<()> {
+ let session_id = self.thread.read(cx).session_id().clone();
self.conversation.update(cx, |conversation, cx| {
- conversation.authorize_pending_tool_call(&self.id, kind, cx)
+ conversation.authorize_pending_tool_call(&session_id, kind, cx)
})?;
if self.should_be_following {
self.workspace
@@ -1780,8 +1786,9 @@ impl ThreadView {
_ => acp::PermissionOptionKind::AllowOnce,
};
+ let session_id = self.thread.read(cx).session_id().clone();
self.authorize_tool_call(
- self.id.clone(),
+ session_id,
tool_call_id,
SelectedPermissionOutcome::new(option_id, option_kind),
window,
@@ -1859,10 +1866,20 @@ impl ThreadView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Option<()> {
- let (session_id, tool_call_id, options) =
- self.conversation.read(cx).pending_tool_call(&self.id, cx)?;
+ let session_id = self.thread.read(cx).session_id().clone();
+ let (returned_session_id, tool_call_id, options) = self
+ .conversation
+ .read(cx)
+ .pending_tool_call(&session_id, cx)?;
let options = options.clone();
- self.authorize_with_granularity(session_id, tool_call_id, &options, is_allow, window, cx)
+ self.authorize_with_granularity(
+ returned_session_id,
+ tool_call_id,
+ &options,
+ is_allow,
+ window,
+ cx,
+ )
}
fn authorize_with_granularity(
@@ -2548,6 +2565,35 @@ impl ThreadView {
)
}
+ fn collect_subagent_items_for_sessions(
+ entries: &[AgentThreadEntry],
+ awaiting_session_ids: &[acp::SessionId],
+ cx: &App,
+ ) -> Vec<(SharedString, usize)> {
+ let tool_calls_by_session: HashMap<_, _> = entries
+ .iter()
+ .enumerate()
+ .filter_map(|(entry_ix, entry)| {
+ let AgentThreadEntry::ToolCall(tool_call) = entry else {
+ return None;
+ };
+ let info = tool_call.subagent_session_info.as_ref()?;
+ let summary_text = tool_call.label.read(cx).source().to_string();
+ let subagent_summary = if summary_text.is_empty() {
+ SharedString::from("Subagent")
+ } else {
+ SharedString::from(summary_text)
+ };
+ Some((info.session_id.clone(), (subagent_summary, entry_ix)))
+ })
+ .collect();
+
+ awaiting_session_ids
+ .iter()
+ .filter_map(|session_id| tool_calls_by_session.get(session_id).cloned())
+ .collect()
+ }
+
fn render_subagents_awaiting_permission(&self, cx: &Context<Self>) -> Option<AnyElement> {
let awaiting = self.conversation.read(cx).subagents_awaiting_permission(cx);
@@ -2555,30 +2601,15 @@ impl ThreadView {
return None;
}
+ let awaiting_session_ids: Vec<_> = awaiting
+ .iter()
+ .map(|(session_id, _)| session_id.clone())
+ .collect();
+
let thread = self.thread.read(cx);
let entries = thread.entries();
- let mut subagent_items: Vec<(SharedString, usize)> = Vec::new();
-
- for (session_id, _) in &awaiting {
- for (entry_ix, entry) in entries.iter().enumerate() {
- if let AgentThreadEntry::ToolCall(tool_call) = entry {
- if let Some(info) = &tool_call.subagent_session_info {
- if &info.session_id == session_id {
- let subagent_summary: SharedString = {
- let summary_text = tool_call.label.read(cx).source().to_string();
- if !summary_text.is_empty() {
- summary_text.into()
- } else {
- "Subagent".into()
- }
- };
- subagent_items.push((subagent_summary, entry_ix));
- break;
- }
- }
- }
- }
- }
+ let subagent_items =
+ Self::collect_subagent_items_for_sessions(entries, &awaiting_session_ids, cx);
if subagent_items.is_empty() {
return None;
@@ -3092,7 +3123,7 @@ impl ThreadView {
}
fn is_subagent_canceled_or_failed(&self, cx: &App) -> bool {
- let Some(parent_session_id) = self.parent_id.as_ref() else {
+ let Some(parent_session_id) = self.parent_session_id.as_ref() else {
return false;
};
@@ -3100,7 +3131,7 @@ impl ThreadView {
self.server_view
.upgrade()
- .and_then(|sv| sv.read(cx).thread_view(parent_session_id))
+ .and_then(|sv| sv.read(cx).thread_view(parent_session_id, cx))
.is_some_and(|parent_view| {
parent_view
.read(cx)
@@ -3119,9 +3150,10 @@ impl ThreadView {
}
pub(crate) fn render_subagent_titlebar(&mut self, cx: &mut Context<Self>) -> Option<Div> {
- let Some(parent_session_id) = self.parent_id.clone() else {
+ if self.parent_session_id.is_none() {
return None;
- };
+ }
+ let parent_session_id = self.thread.read(cx).parent_session_id()?.clone();
let server_view = self.server_view.clone();
let thread = self.thread.clone();
@@ -3189,7 +3221,7 @@ impl ThreadView {
.tooltip(Tooltip::text("Minimize Subagent"))
.on_click(move |_, window, cx| {
let _ = server_view.update(cx, |server_view, cx| {
- server_view.navigate_to_session(
+ server_view.navigate_to_thread(
parent_session_id.clone(),
window,
cx,
@@ -4690,7 +4722,7 @@ impl ThreadView {
}
AgentThreadEntry::ToolCall(tool_call) => self
.render_any_tool_call(
- &self.id,
+ self.thread.read(cx).session_id(),
entry_ix,
tool_call,
&self.focus_handle(cx),
@@ -6124,7 +6156,7 @@ impl ThreadView {
.when_some(confirmation_options, |this, options| {
let is_first = self.is_first_tool_call(active_session_id, &tool_call.id, cx);
this.child(self.render_permission_buttons(
- self.id.clone(),
+ self.thread.read(cx).session_id().clone(),
is_first,
options,
entry_ix,
@@ -6146,7 +6178,8 @@ impl ThreadView {
.read(cx)
.pending_tool_call(active_session_id, cx)
.map_or(false, |(pending_session_id, pending_tool_call_id, _)| {
- self.id == pending_session_id && tool_call_id == &pending_tool_call_id
+ self.thread.read(cx).session_id() == &pending_session_id
+ && tool_call_id == &pending_tool_call_id
})
}
@@ -6358,7 +6391,7 @@ impl ThreadView {
)
})
.child(self.render_permission_buttons(
- self.id.clone(),
+ self.thread.read(cx).session_id().clone(),
self.is_first_tool_call(active_session_id, &tool_call.id, cx),
options,
entry_ix,
@@ -7117,10 +7150,10 @@ impl ThreadView {
})
.label_size(LabelSize::Small)
.on_click(cx.listener({
- let session_id = session_id.clone();
let tool_call_id = tool_call_id.clone();
let option_id = option.option_id.clone();
let option_kind = option.kind;
+ let session_id = session_id.clone();
move |this, _, window, cx| {
this.authorize_tool_call(
session_id.clone(),
@@ -7673,11 +7706,11 @@ impl ThreadView {
window: &Window,
cx: &Context<Self>,
) -> Div {
- let subagent_thread_view = subagent_session_id.and_then(|id| {
+ let subagent_thread_view = subagent_session_id.and_then(|session_id| {
self.server_view
.upgrade()
.and_then(|server_view| server_view.read(cx).as_connected())
- .and_then(|connected| connected.threads.get(&id))
+ .and_then(|connected| connected.threads.get(&session_id))
});
let content = self.render_subagent_card(
@@ -7714,12 +7747,11 @@ impl ThreadView {
.map(|log| log.read(cx).changed_buffers(cx))
.unwrap_or_default();
- let is_pending_tool_call = thread
+ let is_pending_tool_call = thread_view
.as_ref()
- .and_then(|thread| {
- self.conversation
- .read(cx)
- .pending_tool_call(thread.read(cx).session_id(), cx)
+ .and_then(|tv| {
+ let sid = tv.read(cx).thread.read(cx).session_id();
+ self.conversation.read(cx).pending_tool_call(sid, cx)
})
.is_some();
@@ -7945,12 +7977,13 @@ impl ThreadView {
)
.when_some(thread_view, |this, thread_view| {
let thread = &thread_view.read(cx).thread;
+ let tv_session_id = thread.read(cx).session_id();
let pending_tool_call = self
.conversation
.read(cx)
- .pending_tool_call(thread.read(cx).session_id(), cx);
+ .pending_tool_call(tv_session_id, cx);
- let session_id = thread.read(cx).session_id().clone();
+ let nav_session_id = tv_session_id.clone();
let fullscreen_toggle = h_flex()
.id(entry_ix)
@@ -7972,7 +8005,7 @@ impl ThreadView {
telemetry::event!("Subagent Maximized");
this.server_view
.update(cx, |this, cx| {
- this.navigate_to_session(session_id.clone(), window, cx);
+ this.navigate_to_thread(nav_session_id.clone(), window, cx);
})
.ok();
}));
@@ -8069,7 +8102,7 @@ impl ThreadView {
let scroll_handle = self
.subagent_scroll_handles
.borrow_mut()
- .entry(session_id.clone())
+ .entry(subagent_view.session_id.clone())
.or_default()
.clone();
@@ -8869,15 +8902,15 @@ impl Render for ThreadView {
.key_context("AcpThread")
.track_focus(&self.focus_handle)
.on_action(cx.listener(|this, _: &menu::Cancel, _, cx| {
- if this.parent_id.is_none() {
+ if this.parent_session_id.is_none() {
this.cancel_generation(cx);
}
}))
.on_action(cx.listener(|this, _: &workspace::GoBack, window, cx| {
- if let Some(parent_session_id) = this.parent_id.clone() {
+ if let Some(parent_session_id) = this.thread.read(cx).parent_session_id().cloned() {
this.server_view
.update(cx, |view, cx| {
- view.navigate_to_session(parent_session_id, window, cx);
+ view.navigate_to_thread(parent_session_id, window, cx);
})
.ok();
}
diff --git a/crates/agent_ui/src/test_support.rs b/crates/agent_ui/src/test_support.rs
index 94502485b1f..d9a7c7e2074 100644
--- a/crates/agent_ui/src/test_support.rs
+++ b/crates/agent_ui/src/test_support.rs
@@ -73,6 +73,9 @@ pub fn init_test(cx: &mut TestAppContext) {
cx.update(|cx| {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
+ cx.set_global(acp_thread::StubSessionCounter(
+ std::sync::atomic::AtomicUsize::new(0),
+ ));
theme_settings::init(theme::LoadThemes::JustBase, cx);
editor::init(cx);
release_channel::init("0.0.0".parse().unwrap(), cx);
@@ -128,3 +131,10 @@ pub fn active_session_id(panel: &Entity<AgentPanel>, cx: &VisualTestContext) ->
thread.read(cx).session_id().clone()
})
}
+
+pub fn active_thread_id(
+ panel: &Entity<AgentPanel>,
+ cx: &VisualTestContext,
+) -> crate::thread_metadata_store::ThreadId {
+ panel.read_with(cx, |panel, cx| panel.active_thread_id(cx).unwrap())
+}
diff --git a/crates/agent_ui/src/thread_history_view.rs b/crates/agent_ui/src/thread_history_view.rs
index a4a00455be4..8facafecd95 100644
--- a/crates/agent_ui/src/thread_history_view.rs
+++ b/crates/agent_ui/src/thread_history_view.rs
@@ -20,7 +20,7 @@ pub(crate) fn thread_title(entry: &AgentSessionInfo) -> SharedString {
entry
.title
.clone()
- .filter(|title| !title.is_empty())
+ .and_then(|title| if title.is_empty() { None } else { Some(title) })
.unwrap_or_else(|| DEFAULT_THREAD_TITLE.into())
}
diff --git a/crates/agent_ui/src/thread_import.rs b/crates/agent_ui/src/thread_import.rs
index 78de6fd34e2..4bbb1e48ed7 100644
--- a/crates/agent_ui/src/thread_import.rs
+++ b/crates/agent_ui/src/thread_import.rs
@@ -23,7 +23,7 @@ use workspace::{ModalView, MultiWorkspace, Workspace};
use crate::{
Agent, AgentPanel,
agent_connection_store::AgentConnectionStore,
- thread_metadata_store::{ThreadMetadata, ThreadMetadataStore, ThreadWorktreePaths},
+ thread_metadata_store::{ThreadId, ThreadMetadata, ThreadMetadataStore, WorktreePaths},
};
pub struct AcpThreadImportOnboarding;
@@ -206,10 +206,11 @@ impl ThreadImportModal {
.filter(|agent_id| !self.unchecked_agents.contains(agent_id))
.collect::<Vec<_>>();
- let existing_sessions = ThreadMetadataStore::global(cx)
+ let existing_sessions: HashSet<acp::SessionId> = ThreadMetadataStore::global(cx)
.read(cx)
- .entry_ids()
- .collect::<HashSet<_>>();
+ .entries()
+ .filter_map(|m| m.session_id.clone())
+ .collect();
let task = find_threads_to_import(agent_ids, existing_sessions, stores, cx);
cx.spawn(async move |this, cx| {
@@ -520,14 +521,13 @@ fn collect_importable_threads(
continue;
};
to_insert.push(ThreadMetadata {
- session_id: session.session_id,
+ thread_id: ThreadId::new(),
+ session_id: Some(session.session_id),
agent_id: agent_id.clone(),
- title: session
- .title
- .unwrap_or_else(|| crate::DEFAULT_THREAD_TITLE.into()),
+ title: session.title,
updated_at: session.updated_at.unwrap_or_else(|| Utc::now()),
created_at: session.created_at,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&folder_paths),
+ worktree_paths: WorktreePaths::from_folder_paths(&folder_paths),
remote_connection: remote_connection.clone(),
archived: true,
});
@@ -584,8 +584,8 @@ mod tests {
let result = collect_importable_threads(sessions_by_agent, existing);
assert_eq!(result.len(), 1);
- assert_eq!(result[0].session_id.0.as_ref(), "new-1");
- assert_eq!(result[0].title.as_ref(), "Brand New");
+ assert_eq!(result[0].session_id.as_ref().unwrap().0.as_ref(), "new-1");
+ assert_eq!(result[0].display_title(), "Brand New");
}
#[test]
@@ -605,7 +605,10 @@ mod tests {
let result = collect_importable_threads(sessions_by_agent, existing);
assert_eq!(result.len(), 1);
- assert_eq!(result[0].session_id.0.as_ref(), "has-dirs");
+ assert_eq!(
+ result[0].session_id.as_ref().unwrap().0.as_ref(),
+ "has-dirs"
+ );
}
#[test]
@@ -657,11 +660,11 @@ mod tests {
assert_eq!(result.len(), 2);
let s1 = result
.iter()
- .find(|t| t.session_id.0.as_ref() == "s1")
+ .find(|t| t.session_id.as_ref().map(|s| s.0.as_ref()) == Some("s1"))
.unwrap();
let s2 = result
.iter()
- .find(|t| t.session_id.0.as_ref() == "s2")
+ .find(|t| t.session_id.as_ref().map(|s| s.0.as_ref()) == Some("s2"))
.unwrap();
assert_eq!(s1.agent_id.as_ref(), "agent-a");
assert_eq!(s2.agent_id.as_ref(), "agent-b");
@@ -700,7 +703,10 @@ mod tests {
let result = collect_importable_threads(sessions_by_agent, existing);
assert_eq!(result.len(), 1);
- assert_eq!(result[0].session_id.0.as_ref(), "shared-session");
+ assert_eq!(
+ result[0].session_id.as_ref().unwrap().0.as_ref(),
+ "shared-session"
+ );
assert_eq!(
result[0].agent_id.as_ref(),
"agent-a",
diff --git a/crates/agent_ui/src/thread_metadata_store.rs b/crates/agent_ui/src/thread_metadata_store.rs
index 4ba68b400a6..cdd2f6c6da0 100644
--- a/crates/agent_ui/src/thread_metadata_store.rs
+++ b/crates/agent_ui/src/thread_metadata_store.rs
@@ -1,9 +1,5 @@
-use std::{
- path::{Path, PathBuf},
- sync::Arc,
-};
+use std::{path::PathBuf, sync::Arc};
-use acp_thread::AcpThreadEvent;
use agent::{ThreadStore, ZED_AGENT_ID};
use agent_client_protocol as acp;
use anyhow::Context as _;
@@ -12,7 +8,9 @@ use collections::{HashMap, HashSet};
use db::{
kvp::KeyValueStore,
sqlez::{
- bindable::Column, domain::Domain, statement::Statement,
+ bindable::{Bind, Column},
+ domain::Domain,
+ statement::Statement,
thread_safe_connection::ThreadSafeConnection,
},
sqlez_macros::sql,
@@ -21,6 +19,7 @@ use fs::Fs;
use futures::{FutureExt, future::Shared};
use gpui::{AppContext as _, Entity, Global, Subscription, Task};
use project::AgentId;
+pub use project::WorktreePaths;
use remote::RemoteConnectionOptions;
use ui::{App, Context, SharedString};
use util::ResultExt as _;
@@ -28,12 +27,36 @@ use workspace::{PathList, SerializedWorkspaceLocation, WorkspaceDb};
use crate::DEFAULT_THREAD_TITLE;
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)]
+pub struct ThreadId(uuid::Uuid);
+
+impl ThreadId {
+ pub fn new() -> Self {
+ Self(uuid::Uuid::new_v4())
+ }
+}
+
+impl Bind for ThreadId {
+ fn bind(&self, statement: &Statement, start_index: i32) -> anyhow::Result<i32> {
+ self.0.bind(statement, start_index)
+ }
+}
+
+impl Column for ThreadId {
+ fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> {
+ let (uuid, next) = Column::column(statement, start_index)?;
+ Ok((ThreadId(uuid), next))
+ }
+}
+
const THREAD_REMOTE_CONNECTION_MIGRATION_KEY: &str = "thread-metadata-remote-connection-backfill";
+const THREAD_ID_MIGRATION_KEY: &str = "thread-metadata-thread-id-backfill";
pub fn init(cx: &mut App) {
ThreadMetadataStore::init_global(cx);
let migration_task = migrate_thread_metadata(cx);
migrate_thread_remote_connections(cx, migration_task);
+ migrate_thread_ids(cx);
}
/// Migrate existing thread metadata from native agent thread store to the new metadata storage.
@@ -45,26 +68,36 @@ fn migrate_thread_metadata(cx: &mut App) -> Task<anyhow::Result<()>> {
let db = store.read(cx).db.clone();
cx.spawn(async move |cx| {
- let existing_entries = db.list_ids()?.into_iter().collect::<HashSet<_>>();
-
- let is_first_migration = existing_entries.is_empty();
+ let existing_list = db.list()?;
+ let is_first_migration = existing_list.is_empty();
+ let existing_session_ids: HashSet<Arc<str>> = existing_list
+ .into_iter()
+ .filter_map(|m| m.session_id.map(|s| s.0))
+ .collect();
let mut to_migrate = store.read_with(cx, |_store, cx| {
ThreadStore::global(cx)
.read(cx)
.entries()
.filter_map(|entry| {
- if existing_entries.contains(&entry.id.0) {
+ if existing_session_ids.contains(&entry.id.0) {
return None;
}
Some(ThreadMetadata {
- session_id: entry.id,
+ thread_id: ThreadId::new(),
+ session_id: Some(entry.id),
agent_id: ZED_AGENT_ID.clone(),
- title: entry.title,
+ title: if entry.title.is_empty()
+ || entry.title.as_ref() == DEFAULT_THREAD_TITLE
+ {
+ None
+ } else {
+ Some(entry.title)
+ },
updated_at: entry.updated_at,
created_at: entry.created_at,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&entry.folder_paths),
+ worktree_paths: WorktreePaths::from_folder_paths(&entry.folder_paths),
remote_connection: None,
archived: true,
})
@@ -191,153 +224,86 @@ fn migrate_thread_remote_connections(cx: &mut App, migration_task: Task<anyhow::
.detach_and_log_err(cx);
}
-struct GlobalThreadMetadataStore(Entity<ThreadMetadataStore>);
-impl Global for GlobalThreadMetadataStore {}
-
-/// Paired worktree paths for a thread. Each folder path has a corresponding
-/// main worktree path at the same position. The two lists are always the
-/// same length and are modified together via `add_path` / `remove_main_path`.
-///
-/// For non-linked worktrees, the main path and folder path are identical.
-/// For linked worktrees, the main path is the original repo and the folder
-/// path is the linked worktree location.
-///
-/// Internally stores two `PathList`s with matching insertion order so that
-/// `ordered_paths()` on both yields positionally-paired results.
-#[derive(Default, Debug, Clone)]
-pub struct ThreadWorktreePaths {
- folder_paths: PathList,
- main_worktree_paths: PathList,
-}
-
-impl PartialEq for ThreadWorktreePaths {
- fn eq(&self, other: &Self) -> bool {
- self.folder_paths == other.folder_paths
- && self.main_worktree_paths == other.main_worktree_paths
- }
-}
+fn migrate_thread_ids(cx: &mut App) {
+ let store = ThreadMetadataStore::global(cx);
+ let db = store.read(cx).db.clone();
+ let kvp = KeyValueStore::global(cx);
-impl ThreadWorktreePaths {
- /// Build from a project's current state. Each visible worktree is paired
- /// with its main repo path (resolved via git), falling back to the
- /// worktree's own path if no git repo is found.
- pub fn from_project(project: &project::Project, cx: &App) -> Self {
- let (mains, folders): (Vec<PathBuf>, Vec<PathBuf>) = project
- .visible_worktrees(cx)
- .map(|worktree| {
- let snapshot = worktree.read(cx).snapshot();
- let folder_path = snapshot.abs_path().to_path_buf();
- let main_path = snapshot
- .root_repo_common_dir()
- .and_then(|dir| Some(dir.parent()?.to_path_buf()))
- .unwrap_or_else(|| folder_path.clone());
- (main_path, folder_path)
- })
- .unzip();
- Self {
- folder_paths: PathList::new(&folders),
- main_worktree_paths: PathList::new(&mains),
+ cx.spawn(async move |cx| -> anyhow::Result<()> {
+ if kvp.read_kvp(THREAD_ID_MIGRATION_KEY)?.is_some() {
+ return Ok(());
}
- }
- /// Build from two parallel `PathList`s that already share the same
- /// insertion order. Used for deserialization from DB.
- ///
- /// Returns an error if the two lists have different lengths, which
- /// indicates corrupted data from a prior migration bug.
- pub fn from_path_lists(
- main_worktree_paths: PathList,
- folder_paths: PathList,
- ) -> anyhow::Result<Self> {
- anyhow::ensure!(
- main_worktree_paths.paths().len() == folder_paths.paths().len(),
- "main_worktree_paths has {} entries but folder_paths has {}",
- main_worktree_paths.paths().len(),
- folder_paths.paths().len(),
- );
- Ok(Self {
- folder_paths,
- main_worktree_paths,
- })
- }
-
- /// Build for non-linked worktrees where main == folder for every path.
- pub fn from_folder_paths(folder_paths: &PathList) -> Self {
- Self {
- folder_paths: folder_paths.clone(),
- main_worktree_paths: folder_paths.clone(),
+ let mut reloaded = false;
+ for metadata in db.list()? {
+ db.save(metadata).await?;
+ reloaded = true;
}
- }
-
- pub fn is_empty(&self) -> bool {
- self.folder_paths.is_empty()
- }
-
- /// The folder paths (for workspace matching / `threads_by_paths` index).
- pub fn folder_path_list(&self) -> &PathList {
- &self.folder_paths
- }
- /// The main worktree paths (for group key / `threads_by_main_paths` index).
- pub fn main_worktree_path_list(&self) -> &PathList {
- &self.main_worktree_paths
- }
-
- /// Iterate the (main_worktree_path, folder_path) pairs in insertion order.
- pub fn ordered_pairs(&self) -> impl Iterator<Item = (&PathBuf, &PathBuf)> {
- self.main_worktree_paths
- .ordered_paths()
- .zip(self.folder_paths.ordered_paths())
- }
+ let reloaded_task = reloaded
+ .then_some(store.update(cx, |store, cx| store.reload(cx)))
+ .unwrap_or(Task::ready(()).shared());
- /// Add a new path pair. If the exact (main, folder) pair already exists,
- /// this is a no-op. Rebuilds both internal `PathList`s to maintain
- /// consistent ordering.
- pub fn add_path(&mut self, main_path: &Path, folder_path: &Path) {
- let already_exists = self
- .ordered_pairs()
- .any(|(m, f)| m.as_path() == main_path && f.as_path() == folder_path);
- if already_exists {
- return;
- }
- let (mut mains, mut folders): (Vec<PathBuf>, Vec<PathBuf>) = self
- .ordered_pairs()
- .map(|(m, f)| (m.clone(), f.clone()))
- .unzip();
- mains.push(main_path.to_path_buf());
- folders.push(folder_path.to_path_buf());
- self.main_worktree_paths = PathList::new(&mains);
- self.folder_paths = PathList::new(&folders);
- }
+ kvp.write_kvp(THREAD_ID_MIGRATION_KEY.to_string(), "1".to_string())
+ .await?;
+ reloaded_task.await;
- /// Remove all pairs whose main worktree path matches the given path.
- /// This removes the corresponding entries from both lists.
- pub fn remove_main_path(&mut self, main_path: &Path) {
- let (mains, folders): (Vec<PathBuf>, Vec<PathBuf>) = self
- .ordered_pairs()
- .filter(|(m, _)| m.as_path() != main_path)
- .map(|(m, f)| (m.clone(), f.clone()))
- .unzip();
- self.main_worktree_paths = PathList::new(&mains);
- self.folder_paths = PathList::new(&folders);
- }
+ Ok(())
+ })
+ .detach_and_log_err(cx);
}
+struct GlobalThreadMetadataStore(Entity<ThreadMetadataStore>);
+impl Global for GlobalThreadMetadataStore {}
+
/// Lightweight metadata for any thread (native or ACP), enough to populate
/// the sidebar list and route to the correct load path when clicked.
#[derive(Debug, Clone, PartialEq)]
pub struct ThreadMetadata {
- pub session_id: acp::SessionId,
+ pub thread_id: ThreadId,
+ pub session_id: Option<acp::SessionId>,
pub agent_id: AgentId,
- pub title: SharedString,
+ pub title: Option<SharedString>,
pub updated_at: DateTime<Utc>,
pub created_at: Option<DateTime<Utc>>,
- pub worktree_paths: ThreadWorktreePaths,
+ pub worktree_paths: WorktreePaths,
pub remote_connection: Option<RemoteConnectionOptions>,
pub archived: bool,
}
impl ThreadMetadata {
+ pub fn new_draft(
+ thread_id: ThreadId,
+ session_id: Option<acp::SessionId>,
+ agent_id: AgentId,
+ title: Option<SharedString>,
+ worktree_paths: WorktreePaths,
+ remote_connection: Option<RemoteConnectionOptions>,
+ ) -> Self {
+ let now = Utc::now();
+ Self {
+ thread_id,
+ session_id,
+ agent_id,
+ title,
+ updated_at: now,
+ created_at: Some(now),
+ worktree_paths: worktree_paths.clone(),
+ remote_connection,
+ archived: worktree_paths.is_empty(),
+ }
+ }
+
+ pub fn is_draft(&self) -> bool {
+ self.session_id.is_none()
+ }
+
+ pub fn display_title(&self) -> SharedString {
+ self.title
+ .clone()
+ .unwrap_or_else(|| crate::DEFAULT_THREAD_TITLE.into())
+ }
+
pub fn folder_paths(&self) -> &PathList {
self.worktree_paths.folder_path_list()
}
@@ -348,10 +314,14 @@ impl ThreadMetadata {
impl From<&ThreadMetadata> for acp_thread::AgentSessionInfo {
fn from(meta: &ThreadMetadata) -> Self {
+ let session_id = meta
+ .session_id
+ .clone()
+ .unwrap_or_else(|| acp::SessionId::new(meta.thread_id.0.to_string()));
Self {
- session_id: meta.session_id.clone(),
+ session_id,
work_dirs: Some(meta.folder_paths().clone()),
- title: Some(meta.title.clone()),
+ title: meta.title.clone(),
updated_at: Some(meta.updated_at),
created_at: meta.created_at,
meta: None,
@@ -403,34 +373,57 @@ pub struct ArchivedGitWorktree {
/// The store holds all metadata needed to show threads in the sidebar/the archive.
///
-/// Automatically listens to AcpThread events and updates metadata if it has changed.
+/// Listens to ConversationView events and updates metadata when the root thread changes.
pub struct ThreadMetadataStore {
db: ThreadMetadataDb,
- threads: HashMap<acp::SessionId, ThreadMetadata>,
- threads_by_paths: HashMap<PathList, HashSet<acp::SessionId>>,
- threads_by_main_paths: HashMap<PathList, HashSet<acp::SessionId>>,
+ threads: HashMap<ThreadId, ThreadMetadata>,
+ threads_by_paths: HashMap<PathList, HashSet<ThreadId>>,
+ threads_by_main_paths: HashMap<PathList, HashSet<ThreadId>>,
+ threads_by_session: HashMap<acp::SessionId, ThreadId>,
reload_task: Option<Shared<Task<()>>>,
- session_subscriptions: HashMap<acp::SessionId, Subscription>,
+ conversation_subscriptions: HashMap<gpui::EntityId, Subscription>,
pending_thread_ops_tx: smol::channel::Sender<DbOperation>,
- in_flight_archives: HashMap<acp::SessionId, (Task<()>, smol::channel::Sender<()>)>,
+ in_flight_archives: HashMap<ThreadId, (Task<()>, smol::channel::Sender<()>)>,
_db_operations_task: Task<()>,
}
#[derive(Debug, PartialEq)]
enum DbOperation {
Upsert(ThreadMetadata),
- Delete(acp::SessionId),
+ Delete(ThreadId),
}
impl DbOperation {
- fn id(&self) -> &acp::SessionId {
+ fn id(&self) -> ThreadId {
match self {
- DbOperation::Upsert(thread) => &thread.session_id,
- DbOperation::Delete(session_id) => session_id,
+ DbOperation::Upsert(thread) => thread.thread_id,
+ DbOperation::Delete(thread_id) => *thread_id,
}
}
}
+/// Override for the test DB name used by `ThreadMetadataStore::init_global`.
+/// When set as a GPUI global, `init_global` uses this name instead of
+/// deriving one from the thread name. This prevents data from leaking
+/// across proptest cases that share a thread name.
+#[cfg(any(test, feature = "test-support"))]
+pub struct TestMetadataDbName(pub String);
+#[cfg(any(test, feature = "test-support"))]
+impl gpui::Global for TestMetadataDbName {}
+
+#[cfg(any(test, feature = "test-support"))]
+impl TestMetadataDbName {
+ pub fn global(cx: &App) -> String {
+ cx.try_global::<Self>()
+ .map(|g| g.0.clone())
+ .unwrap_or_else(|| {
+ let thread = std::thread::current();
+ let test_name = thread.name().unwrap_or("unknown_test");
+ format!("THREAD_METADATA_DB_{}", test_name)
+ })
+ }
+}
+
impl ThreadMetadataStore {
#[cfg(not(any(test, feature = "test-support")))]
pub fn init_global(cx: &mut App) {
@@ -445,9 +438,7 @@ impl ThreadMetadataStore {
#[cfg(any(test, feature = "test-support"))]
pub fn init_global(cx: &mut App) {
- let thread = std::thread::current();
- let test_name = thread.name().unwrap_or("unknown_test");
- let db_name = format!("THREAD_METADATA_DB_{}", test_name);
+ let db_name = TestMetadataDbName::global(cx);
let db = smol::block_on(db::open_test_db::<ThreadMetadataDb>(&db_name));
let thread_store = cx.new(|cx| Self::new(ThreadMetadataDb(db), cx));
cx.set_global(GlobalThreadMetadataStore(thread_store));
@@ -467,13 +458,19 @@ impl ThreadMetadataStore {
}
/// Returns all thread IDs.
- pub fn entry_ids(&self) -> impl Iterator<Item = acp::SessionId> + '_ {
- self.threads.keys().cloned()
+ pub fn entry_ids(&self) -> impl Iterator<Item = ThreadId> + '_ {
+ self.threads.keys().copied()
}
/// Returns the metadata for a specific thread, if it exists.
- pub fn entry(&self, session_id: &acp::SessionId) -> Option<&ThreadMetadata> {
- self.threads.get(session_id)
+ pub fn entry(&self, thread_id: ThreadId) -> Option<&ThreadMetadata> {
+ self.threads.get(&thread_id)
+ }
+
+ /// Returns the metadata for a thread identified by its ACP session ID.
+ pub fn entry_by_session(&self, session_id: &acp::SessionId) -> Option<&ThreadMetadata> {
+ let thread_id = self.threads_by_session.get(session_id)?;
+ self.threads.get(thread_id)
}
/// Returns all threads.
@@ -531,19 +528,23 @@ impl ThreadMetadataStore {
this.threads.clear();
this.threads_by_paths.clear();
this.threads_by_main_paths.clear();
+ this.threads_by_session.clear();
for row in rows {
+ if let Some(sid) = &row.session_id {
+ this.threads_by_session.insert(sid.clone(), row.thread_id);
+ }
this.threads_by_paths
.entry(row.folder_paths().clone())
.or_default()
- .insert(row.session_id.clone());
+ .insert(row.thread_id);
if !row.main_worktree_paths().is_empty() {
this.threads_by_main_paths
.entry(row.main_worktree_paths().clone())
.or_default()
- .insert(row.session_id.clone());
+ .insert(row.thread_id);
}
- this.threads.insert(row.session_id.clone(), row);
+ this.threads.insert(row.thread_id, row);
}
cx.notify();
@@ -573,37 +574,41 @@ impl ThreadMetadataStore {
}
fn save_internal(&mut self, metadata: ThreadMetadata) {
- if let Some(thread) = self.threads.get(&metadata.session_id) {
+ if let Some(thread) = self.threads.get(&metadata.thread_id) {
if thread.folder_paths() != metadata.folder_paths() {
- if let Some(session_ids) = self.threads_by_paths.get_mut(thread.folder_paths()) {
- session_ids.remove(&metadata.session_id);
+ if let Some(thread_ids) = self.threads_by_paths.get_mut(thread.folder_paths()) {
+ thread_ids.remove(&metadata.thread_id);
}
}
if thread.main_worktree_paths() != metadata.main_worktree_paths()
&& !thread.main_worktree_paths().is_empty()
{
- if let Some(session_ids) = self
+ if let Some(thread_ids) = self
.threads_by_main_paths
.get_mut(thread.main_worktree_paths())
{
- session_ids.remove(&metadata.session_id);
+ thread_ids.remove(&metadata.thread_id);
}
}
}
- self.threads
- .insert(metadata.session_id.clone(), metadata.clone());
+ if let Some(sid) = &metadata.session_id {
+ self.threads_by_session
+ .insert(sid.clone(), metadata.thread_id);
+ }
+
+ self.threads.insert(metadata.thread_id, metadata.clone());
self.threads_by_paths
.entry(metadata.folder_paths().clone())
.or_default()
- .insert(metadata.session_id.clone());
+ .insert(metadata.thread_id);
if !metadata.main_worktree_paths().is_empty() {
self.threads_by_main_paths
.entry(metadata.main_worktree_paths().clone())
.or_default()
- .insert(metadata.session_id.clone());
+ .insert(metadata.thread_id);
}
self.pending_thread_ops_tx
@@ -613,44 +618,69 @@ impl ThreadMetadataStore {
pub fn update_working_directories(
&mut self,
- session_id: &acp::SessionId,
+ thread_id: ThreadId,
work_dirs: PathList,
cx: &mut Context<Self>,
) {
- if let Some(thread) = self.threads.get(session_id) {
+ if let Some(thread) = self.threads.get(&thread_id) {
self.save_internal(ThreadMetadata {
- worktree_paths: ThreadWorktreePaths::from_path_lists(
+ worktree_paths: WorktreePaths::from_path_lists(
thread.main_worktree_paths().clone(),
work_dirs.clone(),
)
- .unwrap_or_else(|_| ThreadWorktreePaths::from_folder_paths(&work_dirs)),
+ .unwrap_or_else(|_| WorktreePaths::from_folder_paths(&work_dirs)),
+ ..thread.clone()
+ });
+ cx.notify();
+ }
+ }
+
+ pub fn update_worktree_paths(
+ &mut self,
+ thread_ids: &[ThreadId],
+ worktree_paths: WorktreePaths,
+ cx: &mut Context<Self>,
+ ) {
+ let mut changed = false;
+ for &thread_id in thread_ids {
+ let Some(thread) = self.threads.get(&thread_id) else {
+ continue;
+ };
+ if thread.worktree_paths == worktree_paths {
+ continue;
+ }
+ self.save_internal(ThreadMetadata {
+ worktree_paths: worktree_paths.clone(),
..thread.clone()
});
+ changed = true;
+ }
+ if changed {
cx.notify();
}
}
pub fn archive(
&mut self,
- session_id: &acp::SessionId,
+ thread_id: ThreadId,
archive_job: Option<(Task<()>, smol::channel::Sender<()>)>,
cx: &mut Context<Self>,
) {
- self.update_archived(session_id, true, cx);
+ self.update_archived(thread_id, true, cx);
if let Some(job) = archive_job {
- self.in_flight_archives.insert(session_id.clone(), job);
+ self.in_flight_archives.insert(thread_id, job);
}
}
- pub fn unarchive(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
- self.update_archived(session_id, false, cx);
+ pub fn unarchive(&mut self, thread_id: ThreadId, cx: &mut Context<Self>) {
+ self.update_archived(thread_id, false, cx);
// Dropping the Sender triggers cancellation in the background task.
- self.in_flight_archives.remove(session_id);
+ self.in_flight_archives.remove(&thread_id);
}
- pub fn cleanup_completed_archive(&mut self, session_id: &acp::SessionId) {
- self.in_flight_archives.remove(session_id);
+ pub fn cleanup_completed_archive(&mut self, thread_id: ThreadId) {
+ self.in_flight_archives.remove(&thread_id);
}
/// Updates a thread's `folder_paths` after an archived worktree has been
@@ -659,11 +689,11 @@ impl ThreadMetadataStore {
/// `path_replacements` is applied to the thread's stored folder paths.
pub fn update_restored_worktree_paths(
&mut self,
- session_id: &acp::SessionId,
+ thread_id: ThreadId,
path_replacements: &[(PathBuf, PathBuf)],
cx: &mut Context<Self>,
) {
- if let Some(thread) = self.threads.get(session_id).cloned() {
+ if let Some(thread) = self.threads.get(&thread_id).cloned() {
let mut paths: Vec<PathBuf> = thread.folder_paths().paths().to_vec();
for (old_path, new_path) in path_replacements {
if let Some(pos) = paths.iter().position(|p| p == old_path) {
@@ -672,11 +702,11 @@ impl ThreadMetadataStore {
}
let new_folder_paths = PathList::new(&paths);
self.save_internal(ThreadMetadata {
- worktree_paths: ThreadWorktreePaths::from_path_lists(
+ worktree_paths: WorktreePaths::from_path_lists(
thread.main_worktree_paths().clone(),
new_folder_paths.clone(),
)
- .unwrap_or_else(|_| ThreadWorktreePaths::from_folder_paths(&new_folder_paths)),
+ .unwrap_or_else(|_| WorktreePaths::from_folder_paths(&new_folder_paths)),
..thread
});
cx.notify();
@@ -685,11 +715,11 @@ impl ThreadMetadataStore {
pub fn complete_worktree_restore(
&mut self,
- session_id: &acp::SessionId,
+ thread_id: ThreadId,
path_replacements: &[(PathBuf, PathBuf)],
cx: &mut Context<Self>,
) {
- if let Some(thread) = self.threads.get(session_id).cloned() {
+ if let Some(thread) = self.threads.get(&thread_id).cloned() {
let mut paths: Vec<PathBuf> = thread.folder_paths().paths().to_vec();
for (old_path, new_path) in path_replacements {
for path in &mut paths {
@@ -700,11 +730,11 @@ impl ThreadMetadataStore {
}
let new_folder_paths = PathList::new(&paths);
self.save_internal(ThreadMetadata {
- worktree_paths: ThreadWorktreePaths::from_path_lists(
+ worktree_paths: WorktreePaths::from_path_lists(
thread.main_worktree_paths().clone(),
new_folder_paths.clone(),
)
- .unwrap_or_else(|_| ThreadWorktreePaths::from_folder_paths(&new_folder_paths)),
+ .unwrap_or_else(|_| WorktreePaths::from_folder_paths(&new_folder_paths)),
..thread
});
cx.notify();
@@ -712,35 +742,87 @@ impl ThreadMetadataStore {
}
/// Apply a mutation to the worktree paths of all threads whose current
- /// `main_worktree_paths` matches `current_main_paths`, then re-index.
+ /// `folder_paths` matches `current_folder_paths`, then re-index.
+ /// When `remote_connection` is provided, only threads with a matching
+ /// remote connection are affected.
pub fn change_worktree_paths(
+ &mut self,
+ current_folder_paths: &PathList,
+ remote_connection: Option<&RemoteConnectionOptions>,
+ mutate: impl Fn(&mut WorktreePaths),
+ cx: &mut Context<Self>,
+ ) {
+ let thread_ids: Vec<_> = self
+ .threads_by_paths
+ .get(current_folder_paths)
+ .into_iter()
+ .flatten()
+ .filter(|id| {
+ remote_connection.is_none()
+ || self
+ .threads
+ .get(id)
+ .and_then(|t| t.remote_connection.as_ref())
+ == remote_connection
+ })
+ .copied()
+ .collect();
+
+ self.mutate_thread_paths(&thread_ids, mutate, cx);
+ }
+
+ /// Like `change_worktree_paths`, but looks up threads by their
+ /// `main_worktree_paths` instead of `folder_paths`. Used when
+ /// migrating threads for project group key changes where the
+ /// lookup key is the group key's main paths.
+ /// When `remote_connection` is provided, only threads with a matching
+ /// remote connection are affected.
+ pub fn change_worktree_paths_by_main(
&mut self,
current_main_paths: &PathList,
- mutate: impl Fn(&mut ThreadWorktreePaths),
+ remote_connection: Option<&RemoteConnectionOptions>,
+ mutate: impl Fn(&mut WorktreePaths),
cx: &mut Context<Self>,
) {
- let session_ids: Vec<_> = self
+ let thread_ids: Vec<_> = self
.threads_by_main_paths
.get(current_main_paths)
.into_iter()
.flatten()
- .cloned()
+ .filter(|id| {
+ remote_connection.is_none()
+ || self
+ .threads
+ .get(id)
+ .and_then(|t| t.remote_connection.as_ref())
+ == remote_connection
+ })
+ .copied()
.collect();
- if session_ids.is_empty() {
+ self.mutate_thread_paths(&thread_ids, mutate, cx);
+ }
+
+ fn mutate_thread_paths(
+ &mut self,
+ thread_ids: &[ThreadId],
+ mutate: impl Fn(&mut WorktreePaths),
+ cx: &mut Context<Self>,
+ ) {
+ if thread_ids.is_empty() {
return;
}
- for session_id in &session_ids {
- if let Some(thread) = self.threads.get_mut(session_id) {
+ for thread_id in thread_ids {
+ if let Some(thread) = self.threads.get_mut(thread_id) {
if let Some(ids) = self
.threads_by_main_paths
.get_mut(thread.main_worktree_paths())
{
- ids.remove(session_id);
+ ids.remove(thread_id);
}
if let Some(ids) = self.threads_by_paths.get_mut(thread.folder_paths()) {
- ids.remove(session_id);
+ ids.remove(thread_id);
}
mutate(&mut thread.worktree_paths);
@@ -748,11 +830,11 @@ impl ThreadMetadataStore {
self.threads_by_main_paths
.entry(thread.main_worktree_paths().clone())
.or_default()
- .insert(session_id.clone());
+ .insert(*thread_id);
self.threads_by_paths
.entry(thread.folder_paths().clone())
.or_default()
- .insert(session_id.clone());
+ .insert(*thread_id);
self.pending_thread_ops_tx
.try_send(DbOperation::Upsert(thread.clone()))
@@ -789,24 +871,24 @@ impl ThreadMetadataStore {
pub fn link_thread_to_archived_worktree(
&self,
- session_id: String,
+ thread_id: ThreadId,
archived_worktree_id: i64,
cx: &App,
) -> Task<anyhow::Result<()>> {
let db = self.db.clone();
cx.background_spawn(async move {
- db.link_thread_to_archived_worktree(session_id, archived_worktree_id)
+ db.link_thread_to_archived_worktree(thread_id, archived_worktree_id)
.await
})
}
pub fn get_archived_worktrees_for_thread(
&self,
- session_id: String,
+ thread_id: ThreadId,
cx: &App,
) -> Task<anyhow::Result<Vec<ArchivedGitWorktree>>> {
let db = self.db.clone();
- cx.background_spawn(async move { db.get_archived_worktrees_for_thread(session_id).await })
+ cx.background_spawn(async move { db.get_archived_worktrees_for_thread(thread_id).await })
}
pub fn delete_archived_worktree(&self, id: i64, cx: &App) -> Task<anyhow::Result<()>> {
@@ -816,12 +898,12 @@ impl ThreadMetadataStore {
pub fn unlink_thread_from_all_archived_worktrees(
&self,
- session_id: String,
+ thread_id: ThreadId,
cx: &App,
) -> Task<anyhow::Result<()>> {
let db = self.db.clone();
cx.background_spawn(async move {
- db.unlink_thread_from_all_archived_worktrees(session_id)
+ db.unlink_thread_from_all_archived_worktrees(thread_id)
.await
})
}
@@ -838,13 +920,8 @@ impl ThreadMetadataStore {
})
}
- fn update_archived(
- &mut self,
- session_id: &acp::SessionId,
- archived: bool,
- cx: &mut Context<Self>,
- ) {
- if let Some(thread) = self.threads.get(session_id) {
+ fn update_archived(&mut self, thread_id: ThreadId, archived: bool, cx: &mut Context<Self>) {
+ if let Some(thread) = self.threads.get(&thread_id) {
self.save_internal(ThreadMetadata {
archived,
..thread.clone()
@@ -853,23 +930,26 @@ impl ThreadMetadataStore {
}
}
- pub fn delete(&mut self, session_id: acp::SessionId, cx: &mut Context<Self>) {
- if let Some(thread) = self.threads.get(&session_id) {
- if let Some(session_ids) = self.threads_by_paths.get_mut(thread.folder_paths()) {
- session_ids.remove(&session_id);
+ pub fn delete(&mut self, thread_id: ThreadId, cx: &mut Context<Self>) {
+ if let Some(thread) = self.threads.get(&thread_id) {
+ if let Some(sid) = &thread.session_id {
+ self.threads_by_session.remove(sid);
+ }
+ if let Some(thread_ids) = self.threads_by_paths.get_mut(thread.folder_paths()) {
+ thread_ids.remove(&thread_id);
}
if !thread.main_worktree_paths().is_empty() {
- if let Some(session_ids) = self
+ if let Some(thread_ids) = self
.threads_by_main_paths
.get_mut(thread.main_worktree_paths())
{
- session_ids.remove(&session_id);
+ thread_ids.remove(&thread_id);
}
}
}
- self.threads.remove(&session_id);
+ self.threads.remove(&thread_id);
self.pending_thread_ops_tx
- .try_send(DbOperation::Delete(session_id))
+ .try_send(DbOperation::Delete(thread_id))
.log_err();
cx.notify();
}
@@ -877,21 +957,16 @@ impl ThreadMetadataStore {
fn new(db: ThreadMetadataDb, cx: &mut Context<Self>) -> Self {
let weak_store = cx.weak_entity();
- cx.observe_new::<acp_thread::AcpThread>(move |thread, _window, cx| {
- // Don't track subagent threads in the sidebar.
- if thread.parent_session_id().is_some() {
- return;
- }
-
- let thread_entity = cx.entity();
+ cx.observe_new::<crate::ConversationView>(move |_view, _window, cx| {
+ let view_entity = cx.entity();
+ let entity_id = view_entity.entity_id();
cx.on_release({
let weak_store = weak_store.clone();
- move |thread, cx| {
+ move |_view, cx| {
weak_store
.update(cx, |store, _cx| {
- let session_id = thread.session_id().clone();
- store.session_subscriptions.remove(&session_id);
+ store.conversation_subscriptions.remove(&entity_id);
})
.ok();
}
@@ -900,9 +975,9 @@ impl ThreadMetadataStore {
weak_store
.update(cx, |this, cx| {
- let subscription = cx.subscribe(&thread_entity, Self::handle_thread_event);
- this.session_subscriptions
- .insert(thread.session_id().clone(), subscription);
+ let subscription = cx.subscribe(&view_entity, Self::handle_conversation_event);
+ this.conversation_subscriptions
+ .insert(entity_id, subscription);
})
.ok();
})
@@ -923,8 +998,8 @@ impl ThreadMetadataStore {
DbOperation::Upsert(metadata) => {
db.save(metadata).await.log_err();
}
- DbOperation::Delete(session_id) => {
- db.delete(session_id).await.log_err();
+ DbOperation::Delete(thread_id) => {
+ db.delete(thread_id).await.log_err();
}
}
}
@@ -937,8 +1012,9 @@ impl ThreadMetadataStore {
threads: HashMap::default(),
threads_by_paths: HashMap::default(),
threads_by_main_paths: HashMap::default(),
+ threads_by_session: HashMap::default(),
reload_task: None,
- session_subscriptions: HashMap::default(),
+ conversation_subscriptions: HashMap::default(),
pending_thread_ops_tx: tx,
in_flight_archives: HashMap::default(),
_db_operations_task,
@@ -950,91 +1026,69 @@ impl ThreadMetadataStore {
fn dedup_db_operations(operations: Vec<DbOperation>) -> Vec<DbOperation> {
let mut ops = HashMap::default();
for operation in operations.into_iter().rev() {
- if ops.contains_key(operation.id()) {
+ if ops.contains_key(&operation.id()) {
continue;
}
- ops.insert(operation.id().clone(), operation);
+ ops.insert(operation.id(), operation);
}
ops.into_values().collect()
}
- fn handle_thread_event(
+ fn handle_conversation_event(
&mut self,
- thread: Entity<acp_thread::AcpThread>,
- event: &AcpThreadEvent,
+ conversation_view: Entity<crate::ConversationView>,
+ _event: &crate::conversation_view::RootThreadUpdated,
cx: &mut Context<Self>,
) {
- // Don't track subagent threads in the sidebar.
- if thread.read(cx).parent_session_id().is_some() {
+ let view = conversation_view.read(cx);
+ let thread_id = view.thread_id;
+ let Some(thread) = view.root_acp_thread(cx) else {
+ return;
+ };
+
+ let thread_ref = thread.read(cx);
+ if thread_ref.entries().is_empty() {
return;
}
- match event {
- AcpThreadEvent::NewEntry
- | AcpThreadEvent::TitleUpdated
- | AcpThreadEvent::EntryUpdated(_)
- | AcpThreadEvent::EntriesRemoved(_)
- | AcpThreadEvent::ToolAuthorizationRequested(_)
- | AcpThreadEvent::ToolAuthorizationReceived(_)
- | AcpThreadEvent::Retry(_)
- | AcpThreadEvent::Stopped(_)
- | AcpThreadEvent::Error
- | AcpThreadEvent::LoadError(_)
- | AcpThreadEvent::Refusal
- | AcpThreadEvent::WorkingDirectoriesUpdated => {
- let thread_ref = thread.read(cx);
- if thread_ref.entries().is_empty() {
- return;
- }
+ let existing_thread = self.entry(thread_id);
+ let session_id = Some(thread_ref.session_id().clone());
+ let title = thread_ref.title();
- let existing_thread = self.threads.get(thread_ref.session_id());
- let session_id = thread_ref.session_id().clone();
- let title = thread_ref
- .title()
- .unwrap_or_else(|| DEFAULT_THREAD_TITLE.into());
-
- let updated_at = Utc::now();
-
- let created_at = existing_thread
- .and_then(|t| t.created_at)
- .unwrap_or_else(|| updated_at);
-
- let agent_id = thread_ref.connection().agent_id();
-
- let project = thread_ref.project().read(cx);
- let worktree_paths = ThreadWorktreePaths::from_project(project, cx);
-
- let project_group_key = project.project_group_key(cx);
- let remote_connection = project_group_key.host();
-
- // Threads without a folder path (e.g. started in an empty
- // window) are archived by default so they don't get lost,
- // because they won't show up in the sidebar. Users can reload
- // them from the archive.
- let archived = existing_thread
- .map(|t| t.archived)
- .unwrap_or(worktree_paths.is_empty());
-
- let metadata = ThreadMetadata {
- session_id,
- agent_id,
- title,
- created_at: Some(created_at),
- updated_at,
- worktree_paths,
- remote_connection,
- archived,
- };
+ let updated_at = Utc::now();
- self.save(metadata, cx);
- }
- AcpThreadEvent::TokenUsageUpdated
- | AcpThreadEvent::SubagentSpawned(_)
- | AcpThreadEvent::PromptCapabilitiesUpdated
- | AcpThreadEvent::AvailableCommandsUpdated(_)
- | AcpThreadEvent::ModeUpdated(_)
- | AcpThreadEvent::ConfigOptionsUpdated(_) => {}
- }
+ let created_at = existing_thread
+ .and_then(|t| t.created_at)
+ .unwrap_or_else(|| updated_at);
+
+ let agent_id = thread_ref.connection().agent_id();
+
+ let project = thread_ref.project().read(cx);
+ let worktree_paths = project.worktree_paths(cx);
+
+ let remote_connection = project.remote_connection_options(cx);
+
+ // Threads without a folder path (e.g. started in an empty
+ // window) are archived by default so they don't get lost,
+ // because they won't show up in the sidebar. Users can reload
+ // them from the archive.
+ let archived = existing_thread
+ .map(|t| t.archived)
+ .unwrap_or(worktree_paths.is_empty());
+
+ let metadata = ThreadMetadata {
+ thread_id,
+ session_id,
+ agent_id,
+ title,
+ created_at: Some(created_at),
+ updated_at,
+ worktree_paths,
+ remote_connection,
+ archived,
+ };
+
+ self.save(metadata, cx);
}
}
@@ -1078,15 +1132,56 @@ impl Domain for ThreadMetadataDb {
) STRICT;
),
sql!(ALTER TABLE sidebar_threads ADD COLUMN remote_connection TEXT),
+ sql!(ALTER TABLE sidebar_threads ADD COLUMN thread_id BLOB),
+ sql!(
+ UPDATE sidebar_threads SET thread_id = randomblob(16) WHERE thread_id IS NULL;
+
+ CREATE TABLE thread_archived_worktrees_v2(
+ thread_id BLOB NOT NULL,
+ archived_worktree_id INTEGER NOT NULL REFERENCES archived_git_worktrees(id),
+ PRIMARY KEY (thread_id, archived_worktree_id)
+ ) STRICT;
+
+ INSERT INTO thread_archived_worktrees_v2(thread_id, archived_worktree_id)
+ SELECT s.thread_id, t.archived_worktree_id
+ FROM thread_archived_worktrees t
+ JOIN sidebar_threads s ON s.session_id = t.session_id;
+
+ DROP TABLE thread_archived_worktrees;
+ ALTER TABLE thread_archived_worktrees_v2 RENAME TO thread_archived_worktrees;
+
+ CREATE TABLE sidebar_threads_v2(
+ thread_id BLOB PRIMARY KEY,
+ session_id TEXT,
+ agent_id TEXT,
+ title TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ created_at TEXT,
+ folder_paths TEXT,
+ folder_paths_order TEXT,
+ archived INTEGER DEFAULT 0,
+ main_worktree_paths TEXT,
+ main_worktree_paths_order TEXT,
+ remote_connection TEXT
+ ) STRICT;
+
+ INSERT INTO sidebar_threads_v2(thread_id, session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection)
+ SELECT thread_id, session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection
+ FROM sidebar_threads;
+
+ DROP TABLE sidebar_threads;
+ ALTER TABLE sidebar_threads_v2 RENAME TO sidebar_threads;
+ ),
];
}
db::static_connection!(ThreadMetadataDb, []);
impl ThreadMetadataDb {
- pub fn list_ids(&self) -> anyhow::Result<Vec<Arc<str>>> {
- self.select::<Arc<str>>(
- "SELECT session_id FROM sidebar_threads \
+ #[allow(dead_code)]
+ pub fn list_ids(&self) -> anyhow::Result<Vec<ThreadId>> {
+ self.select::<ThreadId>(
+ "SELECT thread_id FROM sidebar_threads \
ORDER BY updated_at DESC",
)?()
}
@@ -1094,7 +1189,7 @@ impl ThreadMetadataDb {
/// List all sidebar thread metadata, ordered by updated_at descending.
pub fn list(&self) -> anyhow::Result<Vec<ThreadMetadata>> {
self.select::<ThreadMetadata>(
- "SELECT session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection \
+ "SELECT thread_id, session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection \
FROM sidebar_threads \
ORDER BY updated_at DESC"
)?()
@@ -1102,13 +1197,17 @@ impl ThreadMetadataDb {
/// Upsert metadata for a thread.
pub async fn save(&self, row: ThreadMetadata) -> anyhow::Result<()> {
- let id = row.session_id.0.clone();
+ let session_id = row.session_id.as_ref().map(|s| s.0.clone());
let agent_id = if row.agent_id.as_ref() == ZED_AGENT_ID.as_ref() {
None
} else {
Some(row.agent_id.to_string())
};
- let title = row.title.to_string();
+ let title = row
+ .title
+ .as_ref()
+ .map(|t| t.to_string())
+ .unwrap_or_default();
let updated_at = row.updated_at.to_rfc3339();
let created_at = row.created_at.map(|dt| dt.to_rfc3339());
let serialized = row.folder_paths().serialize();
@@ -1130,12 +1229,14 @@ impl ThreadMetadataDb {
.map(serde_json::to_string)
.transpose()
.context("serialize thread metadata remote connection")?;
+ let thread_id = row.thread_id;
let archived = row.archived;
self.write(move |conn| {
- let sql = "INSERT INTO sidebar_threads(session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection) \
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11) \
- ON CONFLICT(session_id) DO UPDATE SET \
+ let sql = "INSERT INTO sidebar_threads(thread_id, session_id, agent_id, title, updated_at, created_at, folder_paths, folder_paths_order, archived, main_worktree_paths, main_worktree_paths_order, remote_connection) \
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) \
+ ON CONFLICT(thread_id) DO UPDATE SET \
+ session_id = excluded.session_id, \
agent_id = excluded.agent_id, \
title = excluded.title, \
updated_at = excluded.updated_at, \
@@ -1147,7 +1248,8 @@ impl ThreadMetadataDb {
main_worktree_paths_order = excluded.main_worktree_paths_order, \
remote_connection = excluded.remote_connection";
let mut stmt = Statement::prepare(conn, sql)?;
- let mut i = stmt.bind(&id, 1)?;
+ let mut i = stmt.bind(&thread_id, 1)?;
+ i = stmt.bind(&session_id, i)?;
i = stmt.bind(&agent_id, i)?;
i = stmt.bind(&title, i)?;
i = stmt.bind(&updated_at, i)?;
@@ -1164,12 +1266,11 @@ impl ThreadMetadataDb {
}
/// Delete metadata for a single thread.
- pub async fn delete(&self, session_id: acp::SessionId) -> anyhow::Result<()> {
- let id = session_id.0.clone();
+ pub async fn delete(&self, thread_id: ThreadId) -> anyhow::Result<()> {
self.write(move |conn| {
let mut stmt =
- Statement::prepare(conn, "DELETE FROM sidebar_threads WHERE session_id = ?")?;
- stmt.bind(&id, 1)?;
+ Statement::prepare(conn, "DELETE FROM sidebar_threads WHERE thread_id = ?")?;
+ stmt.bind(&thread_id, 1)?;
stmt.exec()
})
.await
@@ -1204,16 +1305,16 @@ impl ThreadMetadataDb {
pub async fn link_thread_to_archived_worktree(
&self,
- session_id: String,
+ thread_id: ThreadId,
archived_worktree_id: i64,
) -> anyhow::Result<()> {
self.write(move |conn| {
let mut stmt = Statement::prepare(
conn,
- "INSERT INTO thread_archived_worktrees(session_id, archived_worktree_id) \
+ "INSERT INTO thread_archived_worktrees(thread_id, archived_worktree_id) \
VALUES (?1, ?2)",
)?;
- let i = stmt.bind(&session_id, 1)?;
+ let i = stmt.bind(&thread_id, 1)?;
stmt.bind(&archived_worktree_id, i)?;
stmt.exec()
})
@@ -1222,14 +1323,14 @@ impl ThreadMetadataDb {
pub async fn get_archived_worktrees_for_thread(
&self,
- session_id: String,
+ thread_id: ThreadId,
) -> anyhow::Result<Vec<ArchivedGitWorktree>> {
- self.select_bound::<String, ArchivedGitWorktree>(
+ self.select_bound::<ThreadId, ArchivedGitWorktree>(
"SELECT a.id, a.worktree_path, a.main_repo_path, a.branch_name, a.staged_commit_hash, a.unstaged_commit_hash, a.original_commit_hash \
FROM archived_git_worktrees a \
JOIN thread_archived_worktrees t ON a.id = t.archived_worktree_id \
- WHERE t.session_id = ?1",
- )?(session_id)
+ WHERE t.thread_id = ?1",
+ )?(thread_id)
}
pub async fn delete_archived_worktree(&self, id: i64) -> anyhow::Result<()> {
@@ -1251,14 +1352,14 @@ impl ThreadMetadataDb {
pub async fn unlink_thread_from_all_archived_worktrees(
&self,
- session_id: String,
+ thread_id: ThreadId,
) -> anyhow::Result<()> {
self.write(move |conn| {
let mut stmt = Statement::prepare(
conn,
- "DELETE FROM thread_archived_worktrees WHERE session_id = ?",
+ "DELETE FROM thread_archived_worktrees WHERE thread_id = ?",
)?;
- stmt.bind(&session_id, 1)?;
+ stmt.bind(&thread_id, 1)?;
stmt.exec()
})
.await
@@ -1277,7 +1378,8 @@ impl ThreadMetadataDb {
impl Column for ThreadMetadata {
fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> {
- let (id, next): (Arc<str>, i32) = Column::column(statement, start_index)?;
+ let (thread_id_uuid, next): (uuid::Uuid, i32) = Column::column(statement, start_index)?;
+ let (id, next): (Option<Arc<str>>, i32) = Column::column(statement, next)?;
let (agent_id, next): (Option<String>, i32) = Column::column(statement, next)?;
let (title, next): (String, i32) = Column::column(statement, next)?;
let (updated_at_str, next): (String, i32) = Column::column(statement, next)?;
@@ -1328,15 +1430,21 @@ impl Column for ThreadMetadata {
.transpose()
.context("deserialize thread metadata remote connection")?;
- let worktree_paths =
- ThreadWorktreePaths::from_path_lists(main_worktree_paths, folder_paths)
- .unwrap_or_else(|_| ThreadWorktreePaths::default());
+ let worktree_paths = WorktreePaths::from_path_lists(main_worktree_paths, folder_paths)
+ .unwrap_or_else(|_| WorktreePaths::default());
+
+ let thread_id = ThreadId(thread_id_uuid);
Ok((
ThreadMetadata {
- session_id: acp::SessionId::new(id),
+ thread_id,
+ session_id: id.map(acp::SessionId::new),
agent_id,
- title: title.into(),
+ title: if title.is_empty() || title == DEFAULT_THREAD_TITLE {
+ None
+ } else {
+ Some(title.into())
+ },
updated_at,
created_at,
worktree_paths,
@@ -1376,17 +1484,18 @@ impl Column for ArchivedGitWorktree {
#[cfg(test)]
mod tests {
use super::*;
- use acp_thread::{AgentConnection, StubAgentConnection};
+ use acp_thread::StubAgentConnection;
use action_log::ActionLog;
use agent::DbThread;
use agent_client_protocol as acp;
- use gpui::TestAppContext;
+ use gpui::{TestAppContext, VisualTestContext};
use project::FakeFs;
use project::Project;
use remote::WslConnectionOptions;
use std::path::Path;
use std::rc::Rc;
+ use workspace::MultiWorkspace;
fn make_db_thread(title: &str, updated_at: DateTime<Utc>) -> DbThread {
DbThread {
@@ -1416,13 +1525,18 @@ mod tests {
folder_paths: PathList,
) -> ThreadMetadata {
ThreadMetadata {
+ thread_id: ThreadId::new(),
archived: false,
- session_id: acp::SessionId::new(session_id),
+ session_id: Some(acp::SessionId::new(session_id)),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: title.to_string().into(),
+ title: if title.is_empty() {
+ None
+ } else {
+ Some(title.to_string().into())
+ },
updated_at,
created_at: Some(updated_at),
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&folder_paths),
+ worktree_paths: WorktreePaths::from_folder_paths(&folder_paths),
remote_connection: None,
}
}
@@ -1432,13 +1546,34 @@ mod tests {
cx.update(|cx| {
let settings_store = settings::SettingsStore::test(cx);
cx.set_global(settings_store);
+ theme_settings::init(theme::LoadThemes::JustBase, cx);
+ editor::init(cx);
+ release_channel::init("0.0.0".parse().unwrap(), cx);
+ prompt_store::init(cx);
<dyn Fs>::set_global(fs, cx);
ThreadMetadataStore::init_global(cx);
ThreadStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
});
cx.run_until_parked();
}
+ fn setup_panel_with_project(
+ project: Entity<Project>,
+ cx: &mut TestAppContext,
+ ) -> (Entity<crate::AgentPanel>, VisualTestContext) {
+ let multi_workspace =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace_entity = multi_workspace
+ .read_with(cx, |mw, _cx| mw.workspace().clone())
+ .unwrap();
+ let mut vcx = VisualTestContext::from_window(multi_workspace.into(), cx);
+ let panel = workspace_entity.update_in(&mut vcx, |workspace, window, cx| {
+ cx.new(|cx| crate::AgentPanel::new(workspace, None, window, cx))
+ });
+ (panel, vcx)
+ }
+
fn clear_thread_metadata_remote_connection_backfill(cx: &mut TestAppContext) {
let kvp = cx.update(|cx| KeyValueStore::global(cx));
smol::block_on(kvp.delete_kvp("thread-metadata-remote-connection-backfill".to_string()))
@@ -1497,24 +1632,28 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let entry_ids = store
- .entry_ids()
- .map(|session_id| session_id.0.to_string())
- .collect::<Vec<_>>();
- assert_eq!(entry_ids.len(), 2);
- assert!(entry_ids.contains(&"session-1".to_string()));
- assert!(entry_ids.contains(&"session-2".to_string()));
+ assert_eq!(store.entry_ids().count(), 2);
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-1"))
+ .is_some()
+ );
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-2"))
+ .is_some()
+ );
- let first_path_entries = store
+ let first_path_entries: Vec<_> = store
.entries_for_path(&first_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(first_path_entries, vec!["session-1"]);
- let second_path_entries = store
+ let second_path_entries: Vec<_> = store
.entries_for_path(&second_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(second_path_entries, vec!["session-2"]);
});
}
@@ -1534,6 +1673,7 @@ mod tests {
initial_time,
first_paths.clone(),
);
+ let session1_thread_id = initial_metadata.thread_id;
let second_metadata = make_metadata(
"session-2",
@@ -1541,6 +1681,7 @@ mod tests {
initial_time,
second_paths.clone(),
);
+ let session2_thread_id = second_metadata.thread_id;
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
@@ -1556,25 +1697,30 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let first_path_entries = store
+ let first_path_entries: Vec<_> = store
.entries_for_path(&first_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(first_path_entries, vec!["session-1"]);
- let second_path_entries = store
+ let second_path_entries: Vec<_> = store
.entries_for_path(&second_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(second_path_entries, vec!["session-2"]);
});
- let moved_metadata = make_metadata(
- "session-1",
- "First Thread",
- updated_time,
- second_paths.clone(),
- );
+ let moved_metadata = ThreadMetadata {
+ thread_id: session1_thread_id,
+ session_id: Some(acp::SessionId::new("session-1")),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("First Thread".into()),
+ updated_at: updated_time,
+ created_at: Some(updated_time),
+ worktree_paths: WorktreePaths::from_folder_paths(&second_paths),
+ remote_connection: None,
+ archived: false,
+ };
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
@@ -1589,24 +1735,28 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let entry_ids = store
- .entry_ids()
- .map(|session_id| session_id.0.to_string())
- .collect::<Vec<_>>();
- assert_eq!(entry_ids.len(), 2);
- assert!(entry_ids.contains(&"session-1".to_string()));
- assert!(entry_ids.contains(&"session-2".to_string()));
+ assert_eq!(store.entry_ids().count(), 2);
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-1"))
+ .is_some()
+ );
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-2"))
+ .is_some()
+ );
- let first_path_entries = store
+ let first_path_entries: Vec<_> = store
.entries_for_path(&first_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert!(first_path_entries.is_empty());
- let second_path_entries = store
+ let second_path_entries: Vec<_> = store
.entries_for_path(&second_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(second_path_entries.len(), 2);
assert!(second_path_entries.contains(&"session-1".to_string()));
assert!(second_path_entries.contains(&"session-2".to_string()));
@@ -1615,7 +1765,7 @@ mod tests {
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.delete(acp::SessionId::new("session-2"), cx);
+ store.delete(session2_thread_id, cx);
});
});
@@ -1625,16 +1775,12 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let entry_ids = store
- .entry_ids()
- .map(|session_id| session_id.0.to_string())
- .collect::<Vec<_>>();
- assert_eq!(entry_ids, vec!["session-1"]);
+ assert_eq!(store.entry_ids().count(), 1);
- let second_path_entries = store
+ let second_path_entries: Vec<_> = store
.entries_for_path(&second_paths)
- .map(|entry| entry.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|entry| entry.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(second_path_entries, vec!["session-1"]);
});
}
@@ -1648,12 +1794,13 @@ mod tests {
let now = Utc::now();
let existing_metadata = ThreadMetadata {
- session_id: acp::SessionId::new("a-session-0"),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new("a-session-0")),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Existing Metadata".into(),
+ title: Some("Existing Metadata".into()),
updated_at: now - chrono::Duration::seconds(10),
created_at: Some(now - chrono::Duration::seconds(10)),
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&project_a_paths),
+ worktree_paths: WorktreePaths::from_folder_paths(&project_a_paths),
remote_connection: None,
archived: false,
};
@@ -1727,23 +1874,33 @@ mod tests {
let existing_metadata = list
.iter()
- .find(|metadata| metadata.session_id.0.as_ref() == "a-session-0")
+ .find(|metadata| {
+ metadata
+ .session_id
+ .as_ref()
+ .is_some_and(|s| s.0.as_ref() == "a-session-0")
+ })
.unwrap();
- assert_eq!(existing_metadata.title.as_ref(), "Existing Metadata");
+ assert_eq!(existing_metadata.display_title(), "Existing Metadata");
assert!(!existing_metadata.archived);
- let migrated_session_ids = list
+ let migrated_session_ids: Vec<_> = list
.iter()
- .map(|metadata| metadata.session_id.0.as_ref())
- .collect::<Vec<_>>();
- assert!(migrated_session_ids.contains(&"a-session-1"));
- assert!(migrated_session_ids.contains(&"b-session-0"));
- assert!(migrated_session_ids.contains(&"projectless"));
+ .filter_map(|metadata| metadata.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
+ assert!(migrated_session_ids.iter().any(|s| s == "a-session-1"));
+ assert!(migrated_session_ids.iter().any(|s| s == "b-session-0"));
+ assert!(migrated_session_ids.iter().any(|s| s == "projectless"));
- let migrated_entries = list
+ let migrated_entries: Vec<_> = list
.iter()
- .filter(|metadata| metadata.session_id.0.as_ref() != "a-session-0")
- .collect::<Vec<_>>();
+ .filter(|metadata| {
+ !metadata
+ .session_id
+ .as_ref()
+ .is_some_and(|s| s.0.as_ref() == "a-session-0")
+ })
+ .collect();
assert!(migrated_entries.iter().all(|metadata| metadata.archived));
}
@@ -1757,12 +1914,13 @@ mod tests {
let existing_updated_at = Utc::now();
let existing_metadata = ThreadMetadata {
- session_id: acp::SessionId::new("existing-session"),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new("existing-session")),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Existing Metadata".into(),
+ title: Some("Existing Metadata".into()),
updated_at: existing_updated_at,
created_at: Some(existing_updated_at),
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&project_paths),
+ worktree_paths: WorktreePaths::from_folder_paths(&project_paths),
remote_connection: None,
archived: false,
};
@@ -1800,7 +1958,10 @@ mod tests {
});
assert_eq!(list.len(), 1);
- assert_eq!(list[0].session_id.0.as_ref(), "existing-session");
+ assert_eq!(
+ list[0].session_id.as_ref().unwrap().0.as_ref(),
+ "existing-session"
+ );
}
#[gpui::test]
@@ -1865,7 +2026,7 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
store
.read(cx)
- .entry(&acp::SessionId::new("remote-session"))
+ .entry_by_session(&acp::SessionId::new("remote-session"))
.cloned()
.expect("expected migrated metadata row")
});
@@ -1947,15 +2108,15 @@ mod tests {
for entry in &project_a_entries[..5] {
assert!(
!entry.archived,
- "Expected {} to be unarchived (top 5 most recent)",
- entry.session_id.0
+ "Expected {:?} to be unarchived (top 5 most recent)",
+ entry.session_id
);
}
for entry in &project_a_entries[5..] {
assert!(
entry.archived,
- "Expected {} to be archived (older than top 5)",
- entry.session_id.0
+ "Expected {:?} to be archived (older than top 5)",
+ entry.session_id
);
}
@@ -1974,50 +2135,55 @@ mod tests {
let fs = FakeFs::new(cx.executor());
let project = Project::test(fs, None::<&Path>, cx).await;
- let connection = Rc::new(StubAgentConnection::new());
+ let connection = StubAgentConnection::new();
- let thread = cx
- .update(|cx| {
- connection
- .clone()
- .new_session(project.clone(), PathList::default(), cx)
- })
- .await
- .unwrap();
- let session_id = cx.read(|cx| thread.read(cx).session_id().clone());
+ let (panel, mut vcx) = setup_panel_with_project(project, cx);
+ crate::test_support::open_thread_with_connection(&panel, connection, &mut vcx);
- cx.update(|cx| {
- thread.update(cx, |thread, cx| {
- thread.set_title("Draft Thread".into(), cx).detach();
- });
+ let thread = panel.read_with(&vcx, |panel, cx| panel.active_agent_thread(cx).unwrap());
+ let session_id = thread.read_with(&vcx, |t, _| t.session_id().clone());
+ let thread_id = crate::test_support::active_thread_id(&panel, &vcx);
+
+ // Initial metadata was created by the panel with session_id: None.
+ cx.read(|cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(store.entry_ids().count(), 1);
+ assert!(
+ store.entry(thread_id).unwrap().session_id.is_none(),
+ "expected initial panel metadata to have no session_id"
+ );
});
- cx.run_until_parked();
- let metadata_ids = cx.update(|cx| {
- ThreadMetadataStore::global(cx)
- .read(cx)
- .entry_ids()
- .collect::<Vec<_>>()
+ // Setting a title on an empty thread should be ignored by the
+ // event handler (entries are empty), leaving session_id as None.
+ thread.update_in(&mut vcx, |thread, _window, cx| {
+ thread.set_title("Draft Thread".into(), cx).detach();
});
- assert!(
- metadata_ids.is_empty(),
- "expected empty draft thread title updates to be ignored"
- );
+ vcx.run_until_parked();
- cx.update(|cx| {
- thread.update(cx, |thread, cx| {
- thread.push_user_content_block(None, "Hello".into(), cx);
- });
+ cx.read(|cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert!(
+ store.entry(thread_id).unwrap().session_id.is_none(),
+ "expected title updates on empty thread to be ignored by event handler"
+ );
});
- cx.run_until_parked();
- let metadata_ids = cx.update(|cx| {
- ThreadMetadataStore::global(cx)
- .read(cx)
- .entry_ids()
- .collect::<Vec<_>>()
+ // Pushing content makes entries non-empty, so the event handler
+ // should now update metadata with the real session_id.
+ thread.update_in(&mut vcx, |thread, _window, cx| {
+ thread.push_user_content_block(None, "Hello".into(), cx);
+ });
+ vcx.run_until_parked();
+
+ cx.read(|cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(store.entry_ids().count(), 1);
+ assert_eq!(
+ store.entry(thread_id).unwrap().session_id.as_ref(),
+ Some(&session_id),
+ );
});
- assert_eq!(metadata_ids, vec![session_id]);
}
#[gpui::test]
@@ -2026,44 +2192,35 @@ mod tests {
let fs = FakeFs::new(cx.executor());
let project = Project::test(fs, None::<&Path>, cx).await;
- let connection = Rc::new(StubAgentConnection::new());
+ let connection = StubAgentConnection::new();
- let thread = cx
- .update(|cx| {
- connection
- .clone()
- .new_session(project.clone(), PathList::default(), cx)
- })
- .await
- .unwrap();
- let session_id = cx.read(|cx| thread.read(cx).session_id().clone());
+ let (panel, mut vcx) = setup_panel_with_project(project, cx);
+ crate::test_support::open_thread_with_connection(&panel, connection, &mut vcx);
- cx.update(|cx| {
- thread.update(cx, |thread, cx| {
- thread.push_user_content_block(None, "Hello".into(), cx);
- });
+ let session_id = crate::test_support::active_session_id(&panel, &vcx);
+ let thread = panel.read_with(&vcx, |panel, cx| panel.active_agent_thread(cx).unwrap());
+
+ thread.update_in(&mut vcx, |thread, _window, cx| {
+ thread.push_user_content_block(None, "Hello".into(), cx);
});
- cx.run_until_parked();
+ vcx.run_until_parked();
- let metadata_ids = cx.update(|cx| {
- ThreadMetadataStore::global(cx)
- .read(cx)
- .entry_ids()
- .collect::<Vec<_>>()
+ cx.read(|cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(store.entry_ids().count(), 1);
+ assert!(store.entry_by_session(&session_id).is_some());
});
- assert_eq!(metadata_ids, vec![session_id.clone()]);
- drop(thread);
+ // Dropping the panel releases the ConversationView and its thread.
+ drop(panel);
cx.update(|_| {});
cx.run_until_parked();
- let metadata_ids = cx.update(|cx| {
- ThreadMetadataStore::global(cx)
- .read(cx)
- .entry_ids()
- .collect::<Vec<_>>()
+ cx.read(|cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(store.entry_ids().count(), 1);
+ assert!(store.entry_by_session(&session_id).is_some());
});
- assert_eq!(metadata_ids, vec![session_id]);
}
#[gpui::test]
@@ -2075,56 +2232,47 @@ mod tests {
let fs = FakeFs::new(cx.executor());
let project_without_worktree = Project::test(fs.clone(), None::<&Path>, cx).await;
let project_with_worktree = Project::test(fs, [Path::new("/project-a")], cx).await;
- let connection = Rc::new(StubAgentConnection::new());
-
- let thread_without_worktree = cx
- .update(|cx| {
- connection.clone().new_session(
- project_without_worktree.clone(),
- PathList::default(),
- cx,
- )
- })
- .await
- .unwrap();
- let session_without_worktree =
- cx.read(|cx| thread_without_worktree.read(cx).session_id().clone());
- cx.update(|cx| {
- thread_without_worktree.update(cx, |thread, cx| {
- thread.push_user_content_block(None, "content".into(), cx);
- thread.set_title("No Project Thread".into(), cx).detach();
- });
+ // Thread in project without worktree
+ let (panel_no_wt, mut vcx_no_wt) = setup_panel_with_project(project_without_worktree, cx);
+ crate::test_support::open_thread_with_connection(
+ &panel_no_wt,
+ StubAgentConnection::new(),
+ &mut vcx_no_wt,
+ );
+ let thread_no_wt = panel_no_wt.read_with(&vcx_no_wt, |panel, cx| {
+ panel.active_agent_thread(cx).unwrap()
});
- cx.run_until_parked();
-
- let thread_with_worktree = cx
- .update(|cx| {
- connection.clone().new_session(
- project_with_worktree.clone(),
- PathList::default(),
- cx,
- )
- })
- .await
- .unwrap();
- let session_with_worktree =
- cx.read(|cx| thread_with_worktree.read(cx).session_id().clone());
-
- cx.update(|cx| {
- thread_with_worktree.update(cx, |thread, cx| {
- thread.push_user_content_block(None, "content".into(), cx);
- thread.set_title("Project Thread".into(), cx).detach();
- });
+ thread_no_wt.update_in(&mut vcx_no_wt, |thread, _window, cx| {
+ thread.push_user_content_block(None, "content".into(), cx);
+ thread.set_title("No Project Thread".into(), cx).detach();
});
- cx.run_until_parked();
+ vcx_no_wt.run_until_parked();
+ let session_without_worktree =
+ crate::test_support::active_session_id(&panel_no_wt, &vcx_no_wt);
+
+ // Thread in project with worktree
+ let (panel_wt, mut vcx_wt) = setup_panel_with_project(project_with_worktree, cx);
+ crate::test_support::open_thread_with_connection(
+ &panel_wt,
+ StubAgentConnection::new(),
+ &mut vcx_wt,
+ );
+ let thread_wt =
+ panel_wt.read_with(&vcx_wt, |panel, cx| panel.active_agent_thread(cx).unwrap());
+ thread_wt.update_in(&mut vcx_wt, |thread, _window, cx| {
+ thread.push_user_content_block(None, "content".into(), cx);
+ thread.set_title("Project Thread".into(), cx).detach();
+ });
+ vcx_wt.run_until_parked();
+ let session_with_worktree = crate::test_support::active_session_id(&panel_wt, &vcx_wt);
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
let without_worktree = store
- .entry(&session_without_worktree)
+ .entry_by_session(&session_without_worktree)
.expect("missing metadata for thread without project association");
assert!(without_worktree.folder_paths().is_empty());
assert!(
@@ -2133,7 +2281,7 @@ mod tests {
);
let with_worktree = store
- .entry(&session_with_worktree)
+ .entry_by_session(&session_with_worktree)
.expect("missing metadata for thread with project association");
assert_eq!(
*with_worktree.folder_paths(),
@@ -2154,28 +2302,24 @@ mod tests {
let project = Project::test(fs, None::<&Path>, cx).await;
let connection = Rc::new(StubAgentConnection::new());
- // Create a regular (non-subagent) AcpThread.
- let regular_thread = cx
- .update(|cx| {
- connection
- .clone()
- .new_session(project.clone(), PathList::default(), cx)
- })
- .await
- .unwrap();
+ // Create a regular (non-subagent) thread through the panel.
+ let (panel, mut vcx) = setup_panel_with_project(project.clone(), cx);
+ crate::test_support::open_thread_with_connection(&panel, (*connection).clone(), &mut vcx);
- let regular_session_id = cx.read(|cx| regular_thread.read(cx).session_id().clone());
+ let regular_thread =
+ panel.read_with(&vcx, |panel, cx| panel.active_agent_thread(cx).unwrap());
+ let regular_session_id = regular_thread.read_with(&vcx, |t, _| t.session_id().clone());
- // Set a title on the regular thread to trigger a save via handle_thread_update.
- cx.update(|cx| {
- regular_thread.update(cx, |thread, cx| {
- thread.push_user_content_block(None, "content".into(), cx);
- thread.set_title("Regular Thread".into(), cx).detach();
- });
+ regular_thread.update_in(&mut vcx, |thread, _window, cx| {
+ thread.push_user_content_block(None, "content".into(), cx);
+ thread.set_title("Regular Thread".into(), cx).detach();
});
- cx.run_until_parked();
+ vcx.run_until_parked();
- // Create a subagent AcpThread
+ // Create a standalone subagent AcpThread (not wrapped in a
+ // ConversationView). The ThreadMetadataStore only observes
+ // ConversationView events, so this thread's events should
+ // have no effect on sidebar metadata.
let subagent_session_id = acp::SessionId::new("subagent-session");
let subagent_thread = cx.update(|cx| {
let action_log = cx.new(|_| ActionLog::new(project.clone()));
@@ -2194,7 +2338,6 @@ mod tests {
})
});
- // Set a title on the subagent thread to trigger handle_thread_update.
cx.update(|cx| {
subagent_thread.update(cx, |thread, cx| {
thread
@@ -2204,14 +2347,14 @@ mod tests {
});
cx.run_until_parked();
- // List all metadata from the store cache.
+ // Only the regular thread should appear in sidebar metadata.
+ // The subagent thread is excluded because the metadata store
+ // only observes ConversationView events.
let list = cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.read(cx).entries().cloned().collect::<Vec<_>>()
});
- // The subagent thread should NOT appear in the sidebar metadata.
- // Only the regular thread should be listed.
assert_eq!(
list.len(),
1,
@@ -2219,31 +2362,22 @@ mod tests {
but found {} entries (subagent threads are leaking into the sidebar)",
list.len(),
);
- assert_eq!(list[0].session_id, regular_session_id);
- assert_eq!(list[0].title.as_ref(), "Regular Thread");
+ assert_eq!(list[0].session_id.as_ref().unwrap(), &regular_session_id);
+ assert_eq!(list[0].display_title(), "Regular Thread");
}
#[test]
fn test_dedup_db_operations_keeps_latest_operation_for_session() {
let now = Utc::now();
- let operations = vec![
- DbOperation::Upsert(make_metadata(
- "session-1",
- "First Thread",
- now,
- PathList::default(),
- )),
- DbOperation::Delete(acp::SessionId::new("session-1")),
- ];
+ let meta = make_metadata("session-1", "First Thread", now, PathList::default());
+ let thread_id = meta.thread_id;
+ let operations = vec![DbOperation::Upsert(meta), DbOperation::Delete(thread_id)];
let deduped = ThreadMetadataStore::dedup_db_operations(operations);
assert_eq!(deduped.len(), 1);
- assert_eq!(
- deduped[0],
- DbOperation::Delete(acp::SessionId::new("session-1"))
- );
+ assert_eq!(deduped[0], DbOperation::Delete(thread_id));
}
#[test]
@@ -2252,7 +2386,11 @@ mod tests {
let later = now + chrono::Duration::seconds(1);
let old_metadata = make_metadata("session-1", "Old Title", now, PathList::default());
- let new_metadata = make_metadata("session-1", "New Title", later, PathList::default());
+ let shared_thread_id = old_metadata.thread_id;
+ let new_metadata = ThreadMetadata {
+ thread_id: shared_thread_id,
+ ..make_metadata("session-1", "New Title", later, PathList::default())
+ };
let deduped = ThreadMetadataStore::dedup_db_operations(vec![
DbOperation::Upsert(old_metadata),
@@ -2286,6 +2424,7 @@ mod tests {
let paths = PathList::new(&[Path::new("/project-a")]);
let now = Utc::now();
let metadata = make_metadata("session-1", "Thread 1", now, paths.clone());
+ let thread_id = metadata.thread_id;
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
@@ -2300,23 +2439,19 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let path_entries = store
+ let path_entries: Vec<_> = store
.entries_for_path(&paths)
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(path_entries, vec!["session-1"]);
- let archived = store
- .archived_entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
- assert!(archived.is_empty());
+ assert_eq!(store.archived_entries().count(), 0);
});
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.archive(&acp::SessionId::new("session-1"), None, cx);
+ store.archive(thread_id, None, cx);
});
});
@@ -2327,22 +2462,25 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let path_entries = store
+ let path_entries: Vec<_> = store
.entries_for_path(&paths)
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert!(path_entries.is_empty());
- let archived = store.archived_entries().collect::<Vec<_>>();
+ let archived: Vec<_> = store.archived_entries().collect();
assert_eq!(archived.len(), 1);
- assert_eq!(archived[0].session_id.0.as_ref(), "session-1");
+ assert_eq!(
+ archived[0].session_id.as_ref().unwrap().0.as_ref(),
+ "session-1"
+ );
assert!(archived[0].archived);
});
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.unarchive(&acp::SessionId::new("session-1"), cx);
+ store.unarchive(thread_id, cx);
});
});
@@ -2352,17 +2490,13 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let path_entries = store
+ let path_entries: Vec<_> = store
.entries_for_path(&paths)
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(path_entries, vec!["session-1"]);
- let archived = store
- .archived_entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
- assert!(archived.is_empty());
+ assert_eq!(store.archived_entries().count(), 0);
});
}
@@ -2380,6 +2514,7 @@ mod tests {
now - chrono::Duration::seconds(1),
paths.clone(),
);
+ let session2_thread_id = metadata2.thread_id;
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
@@ -2394,7 +2529,7 @@ mod tests {
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.archive(&acp::SessionId::new("session-2"), None, cx);
+ store.archive(session2_thread_id, None, cx);
});
});
@@ -2404,24 +2539,18 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let path_entries = store
+ let path_entries: Vec<_> = store
.entries_for_path(&paths)
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(path_entries, vec!["session-1"]);
- let all_entries = store
- .entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
- assert_eq!(all_entries.len(), 2);
- assert!(all_entries.contains(&"session-1".to_string()));
- assert!(all_entries.contains(&"session-2".to_string()));
+ assert_eq!(store.entries().count(), 2);
- let archived = store
+ let archived: Vec<_> = store
.archived_entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(archived, vec!["session-2"]);
});
}
@@ -2460,17 +2589,24 @@ mod tests {
let store = ThreadMetadataStore::global(cx);
let store = store.read(cx);
- let all_entries = store
- .entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
- assert_eq!(all_entries.len(), 3);
- assert!(all_entries.contains(&"session-1".to_string()));
- assert!(all_entries.contains(&"session-2".to_string()));
- assert!(all_entries.contains(&"session-3".to_string()));
+ assert_eq!(store.entries().count(), 3);
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-1"))
+ .is_some()
+ );
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-2"))
+ .is_some()
+ );
+ assert!(
+ store
+ .entry_by_session(&acp::SessionId::new("session-3"))
+ .is_some()
+ );
- let entry_ids = store.entry_ids().collect::<Vec<_>>();
- assert_eq!(entry_ids.len(), 3);
+ assert_eq!(store.entry_ids().count(), 3);
});
}
@@ -2481,6 +2617,7 @@ mod tests {
let paths = PathList::new(&[Path::new("/project-a")]);
let now = Utc::now();
let metadata = make_metadata("session-1", "Thread 1", now, paths.clone());
+ let thread_id = metadata.thread_id;
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
@@ -2494,7 +2631,7 @@ mod tests {
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.archive(&acp::SessionId::new("session-1"), None, cx);
+ store.archive(thread_id, None, cx);
});
});
@@ -2514,21 +2651,20 @@ mod tests {
let store = store.read(cx);
let thread = store
- .entries()
- .find(|e| e.session_id.0.as_ref() == "session-1")
+ .entry_by_session(&acp::SessionId::new("session-1"))
.expect("thread should exist after reload");
assert!(thread.archived);
- let path_entries = store
+ let path_entries: Vec<_> = store
.entries_for_path(&paths)
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert!(path_entries.is_empty());
- let archived = store
+ let archived: Vec<_> = store
.archived_entries()
- .map(|e| e.session_id.0.to_string())
- .collect::<Vec<_>>();
+ .filter_map(|e| e.session_id.as_ref().map(|s| s.0.to_string()))
+ .collect();
assert_eq!(archived, vec!["session-1"]);
});
}
@@ -2542,7 +2678,7 @@ mod tests {
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
- store.archive(&acp::SessionId::new("nonexistent"), None, cx);
+ store.archive(ThreadId::new(), None, cx);
});
});
@@ -2565,13 +2701,13 @@ mod tests {
let paths = PathList::new(&[Path::new("/project-a")]);
let now = Utc::now();
let metadata = make_metadata("session-1", "Thread 1", now, paths);
- let session_id = metadata.session_id.clone();
+ let thread_id = metadata.thread_id;
cx.update(|cx| {
let store = ThreadMetadataStore::global(cx);
store.update(cx, |store, cx| {
store.save(metadata.clone(), cx);
- store.archive(&session_id, None, cx);
+ store.archive(thread_id, None, cx);
});
});
@@ -2612,16 +2748,18 @@ mod tests {
.await
.unwrap();
+ let thread_id_1 = ThreadId::new();
+
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-1".to_string(), id, cx)
+ store.link_thread_to_archived_worktree(thread_id_1, id, cx)
})
.await
.unwrap();
let worktrees = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread("session-1".to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id_1, cx)
})
.await
.unwrap();
@@ -2657,9 +2795,11 @@ mod tests {
.await
.unwrap();
+ let thread_id_1 = ThreadId::new();
+
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-1".to_string(), id, cx)
+ store.link_thread_to_archived_worktree(thread_id_1, id, cx)
})
.await
.unwrap();
@@ -2671,7 +2811,7 @@ mod tests {
let worktrees = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread("session-1".to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id_1, cx)
})
.await
.unwrap();
@@ -2698,30 +2838,33 @@ mod tests {
.await
.unwrap();
+ let thread_id_1 = ThreadId::new();
+ let thread_id_2 = ThreadId::new();
+
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-1".to_string(), id, cx)
+ store.link_thread_to_archived_worktree(thread_id_1, id, cx)
})
.await
.unwrap();
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-2".to_string(), id, cx)
+ store.link_thread_to_archived_worktree(thread_id_2, id, cx)
})
.await
.unwrap();
let wt1 = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread("session-1".to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id_1, cx)
})
.await
.unwrap();
let wt2 = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread("session-2".to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id_2, cx)
})
.await
.unwrap();
@@ -2742,6 +2885,7 @@ mod tests {
Path::new("/other/unrelated"),
]);
let meta = make_metadata("session-multi", "Multi Thread", Utc::now(), original_paths);
+ let thread_id = meta.thread_id;
store.update(cx, |store, cx| {
store.save_manually(meta, cx);
@@ -2759,16 +2903,10 @@ mod tests {
];
store.update(cx, |store, cx| {
- store.complete_worktree_restore(
- &acp::SessionId::new("session-multi"),
- &replacements,
- cx,
- );
+ store.complete_worktree_restore(thread_id, &replacements, cx);
});
- let entry = store.read_with(cx, |store, _cx| {
- store.entry(&acp::SessionId::new("session-multi")).cloned()
- });
+ let entry = store.read_with(cx, |store, _cx| store.entry(thread_id).cloned());
let entry = entry.unwrap();
let paths = entry.folder_paths().paths();
assert_eq!(paths.len(), 3);
@@ -2785,6 +2923,7 @@ mod tests {
let original_paths =
PathList::new(&[Path::new("/projects/worktree-a"), Path::new("/other/path")]);
let meta = make_metadata("session-partial", "Partial", Utc::now(), original_paths);
+ let thread_id = meta.thread_id;
store.update(cx, |store, cx| {
store.save_manually(meta, cx);
@@ -2802,18 +2941,10 @@ mod tests {
];
store.update(cx, |store, cx| {
- store.complete_worktree_restore(
- &acp::SessionId::new("session-partial"),
- &replacements,
- cx,
- );
+ store.complete_worktree_restore(thread_id, &replacements, cx);
});
- let entry = store.read_with(cx, |store, _cx| {
- store
- .entry(&acp::SessionId::new("session-partial"))
- .cloned()
- });
+ let entry = store.read_with(cx, |store, _cx| store.entry(thread_id).cloned());
let entry = entry.unwrap();
let paths = entry.folder_paths().paths();
assert_eq!(paths.len(), 2);
@@ -2833,6 +2964,7 @@ mod tests {
Path::new("/other/unrelated"),
]);
let meta = make_metadata("session-multi", "Multi Thread", Utc::now(), original_paths);
+ let thread_id = meta.thread_id;
store.update(cx, |store, cx| {
store.save_manually(meta, cx);
@@ -2850,16 +2982,10 @@ mod tests {
];
store.update(cx, |store, cx| {
- store.update_restored_worktree_paths(
- &acp::SessionId::new("session-multi"),
- &replacements,
- cx,
- );
+ store.update_restored_worktree_paths(thread_id, &replacements, cx);
});
- let entry = store.read_with(cx, |store, _cx| {
- store.entry(&acp::SessionId::new("session-multi")).cloned()
- });
+ let entry = store.read_with(cx, |store, _cx| store.entry(thread_id).cloned());
let entry = entry.unwrap();
let paths = entry.folder_paths().paths();
assert_eq!(paths.len(), 3);
@@ -2876,6 +3002,7 @@ mod tests {
let original_paths =
PathList::new(&[Path::new("/projects/worktree-a"), Path::new("/other/path")]);
let meta = make_metadata("session-partial", "Partial", Utc::now(), original_paths);
+ let thread_id = meta.thread_id;
store.update(cx, |store, cx| {
store.save_manually(meta, cx);
@@ -2893,18 +3020,10 @@ mod tests {
];
store.update(cx, |store, cx| {
- store.update_restored_worktree_paths(
- &acp::SessionId::new("session-partial"),
- &replacements,
- cx,
- );
+ store.update_restored_worktree_paths(thread_id, &replacements, cx);
});
- let entry = store.read_with(cx, |store, _cx| {
- store
- .entry(&acp::SessionId::new("session-partial"))
- .cloned()
- });
+ let entry = store.read_with(cx, |store, _cx| store.entry(thread_id).cloned());
let entry = entry.unwrap();
let paths = entry.folder_paths().paths();
assert_eq!(paths.len(), 2);
@@ -2948,23 +3067,25 @@ mod tests {
.await
.unwrap();
+ let thread_id_1 = ThreadId::new();
+
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-1".to_string(), id1, cx)
+ store.link_thread_to_archived_worktree(thread_id_1, id1, cx)
})
.await
.unwrap();
store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree("session-1".to_string(), id2, cx)
+ store.link_thread_to_archived_worktree(thread_id_1, id2, cx)
})
.await
.unwrap();
let worktrees = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread("session-1".to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id_1, cx)
})
.await
.unwrap();
@@ -2979,16 +3100,179 @@ mod tests {
assert!(paths.contains(&Path::new("/projects/worktree-b")));
}
+ // ── Migration tests ────────────────────────────────────────────────
+
+ #[test]
+ fn test_thread_id_primary_key_migration_backfills_null_thread_ids() {
+ use db::sqlez::connection::Connection;
+
+ let connection =
+ Connection::open_memory(Some("test_thread_id_pk_migration_backfills_nulls"));
+
+ // Run migrations 0-6 (the old schema, before the thread_id PK migration).
+ let old_migrations: &[&str] = &ThreadMetadataDb::MIGRATIONS[..7];
+ connection
+ .migrate(ThreadMetadataDb::NAME, old_migrations, &mut |_, _, _| false)
+ .expect("old migrations should succeed");
+
+ // Insert rows: one with a thread_id, two without.
+ connection
+ .exec(
+ "INSERT INTO sidebar_threads \
+ (session_id, title, updated_at, thread_id) \
+ VALUES ('has-tid', 'Has ThreadId', '2025-01-01T00:00:00Z', X'0102030405060708090A0B0C0D0E0F10')",
+ )
+ .unwrap()()
+ .unwrap();
+ connection
+ .exec(
+ "INSERT INTO sidebar_threads \
+ (session_id, title, updated_at) \
+ VALUES ('no-tid-1', 'No ThreadId 1', '2025-01-02T00:00:00Z')",
+ )
+ .unwrap()()
+ .unwrap();
+ connection
+ .exec(
+ "INSERT INTO sidebar_threads \
+ (session_id, title, updated_at) \
+ VALUES ('no-tid-2', 'No ThreadId 2', '2025-01-03T00:00:00Z')",
+ )
+ .unwrap()()
+ .unwrap();
+
+ // Set up archived_git_worktrees + thread_archived_worktrees rows
+ // referencing the session without a thread_id.
+ connection
+ .exec(
+ "INSERT INTO archived_git_worktrees \
+ (id, worktree_path, main_repo_path, staged_commit_hash, unstaged_commit_hash, original_commit_hash) \
+ VALUES (1, '/wt', '/main', 'abc', 'def', '000')",
+ )
+ .unwrap()()
+ .unwrap();
+ connection
+ .exec(
+ "INSERT INTO thread_archived_worktrees \
+ (session_id, archived_worktree_id) \
+ VALUES ('no-tid-1', 1)",
+ )
+ .unwrap()()
+ .unwrap();
+
+ // Run all migrations (0-7). sqlez skips 0-6 and runs only migration 7.
+ connection
+ .migrate(
+ ThreadMetadataDb::NAME,
+ ThreadMetadataDb::MIGRATIONS,
+ &mut |_, _, _| false,
+ )
+ .expect("new migration should succeed");
+
+ // All 3 rows should survive with non-NULL thread_ids.
+ let count: i64 = connection
+ .select_row_bound::<(), i64>("SELECT COUNT(*) FROM sidebar_threads")
+ .unwrap()(())
+ .unwrap()
+ .unwrap();
+ assert_eq!(count, 3, "all 3 rows should survive the migration");
+
+ let null_count: i64 = connection
+ .select_row_bound::<(), i64>(
+ "SELECT COUNT(*) FROM sidebar_threads WHERE thread_id IS NULL",
+ )
+ .unwrap()(())
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ null_count, 0,
+ "no rows should have NULL thread_id after migration"
+ );
+
+ // The row that already had a thread_id should keep its original value.
+ let original_tid: Vec<u8> = connection
+ .select_row_bound::<&str, Vec<u8>>(
+ "SELECT thread_id FROM sidebar_threads WHERE session_id = ?",
+ )
+ .unwrap()("has-tid")
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ original_tid,
+ vec![
+ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
+ 0x0F, 0x10
+ ],
+ "pre-existing thread_id should be preserved"
+ );
+
+ // The two rows that had NULL thread_ids should now have distinct non-empty blobs.
+ let generated_tid_1: Vec<u8> = connection
+ .select_row_bound::<&str, Vec<u8>>(
+ "SELECT thread_id FROM sidebar_threads WHERE session_id = ?",
+ )
+ .unwrap()("no-tid-1")
+ .unwrap()
+ .unwrap();
+ let generated_tid_2: Vec<u8> = connection
+ .select_row_bound::<&str, Vec<u8>>(
+ "SELECT thread_id FROM sidebar_threads WHERE session_id = ?",
+ )
+ .unwrap()("no-tid-2")
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ generated_tid_1.len(),
+ 16,
+ "generated thread_id should be 16 bytes"
+ );
+ assert_eq!(
+ generated_tid_2.len(),
+ 16,
+ "generated thread_id should be 16 bytes"
+ );
+ assert_ne!(
+ generated_tid_1, generated_tid_2,
+ "each generated thread_id should be unique"
+ );
+
+ // The thread_archived_worktrees join row should have migrated
+ // using the backfilled thread_id from the session without a
+ // pre-existing thread_id.
+ let archived_count: i64 = connection
+ .select_row_bound::<(), i64>("SELECT COUNT(*) FROM thread_archived_worktrees")
+ .unwrap()(())
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ archived_count, 1,
+ "thread_archived_worktrees row should survive migration"
+ );
+
+ // The thread_archived_worktrees row should reference the
+ // backfilled thread_id of the 'no-tid-1' session.
+ let archived_tid: Vec<u8> = connection
+ .select_row_bound::<(), Vec<u8>>(
+ "SELECT thread_id FROM thread_archived_worktrees LIMIT 1",
+ )
+ .unwrap()(())
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ archived_tid, generated_tid_1,
+ "thread_archived_worktrees should reference the backfilled thread_id"
+ );
+ }
+
// ── ThreadWorktreePaths tests ──────────────────────────────────────
/// Helper to build a `ThreadWorktreePaths` from (main, folder) pairs.
- fn make_worktree_paths(pairs: &[(&str, &str)]) -> ThreadWorktreePaths {
+ fn make_worktree_paths(pairs: &[(&str, &str)]) -> WorktreePaths {
let (mains, folders): (Vec<&Path>, Vec<&Path>) = pairs
.iter()
.map(|(m, f)| (Path::new(*m), Path::new(*f)))
.unzip();
- ThreadWorktreePaths::from_path_lists(PathList::new(&mains), PathList::new(&folders))
- .unwrap()
+ WorktreePaths::from_path_lists(PathList::new(&mains), PathList::new(&folders)).unwrap()
}
#[test]
@@ -3056,7 +3340,7 @@ mod tests {
]);
let main = PathList::new(&[Path::new("/projects/zed"), Path::new("/projects/cloud")]);
- let paths = ThreadWorktreePaths::from_path_lists(main, folder).unwrap();
+ let paths = WorktreePaths::from_path_lists(main, folder).unwrap();
let pairs: Vec<_> = paths
.ordered_pairs()
@@ -3107,7 +3391,7 @@ mod tests {
]);
let main = PathList::new(&[Path::new("/projects/zed")]);
- let result = ThreadWorktreePaths::from_path_lists(main, folder);
+ let result = WorktreePaths::from_path_lists(main, folder);
assert!(result.is_err());
}
}
diff --git a/crates/agent_ui/src/thread_worktree_archive.rs b/crates/agent_ui/src/thread_worktree_archive.rs
index 723e6779a44..8ac801b5de0 100644
--- a/crates/agent_ui/src/thread_worktree_archive.rs
+++ b/crates/agent_ui/src/thread_worktree_archive.rs
@@ -3,7 +3,6 @@ use std::{
sync::Arc,
};
-use agent_client_protocol as acp;
use anyhow::{Context as _, Result, anyhow};
use gpui::{App, AsyncApp, Entity, Task};
use project::{
@@ -13,7 +12,7 @@ use project::{
use util::ResultExt;
use workspace::{AppState, MultiWorkspace, Workspace};
-use crate::thread_metadata_store::{ArchivedGitWorktree, ThreadMetadataStore};
+use crate::thread_metadata_store::{ArchivedGitWorktree, ThreadId, ThreadMetadataStore};
/// The plan for archiving a single git worktree root.
///
@@ -170,14 +169,14 @@ pub fn build_root_plan(
/// references `path` in its folder paths. Used to determine whether a
/// worktree can safely be removed from disk.
pub fn path_is_referenced_by_other_unarchived_threads(
- current_session_id: &acp::SessionId,
+ current_thread_id: ThreadId,
path: &Path,
cx: &App,
) -> bool {
ThreadMetadataStore::global(cx)
.read(cx)
.entries()
- .filter(|thread| thread.session_id != *current_session_id)
+ .filter(|thread| thread.thread_id != current_thread_id)
.filter(|thread| !thread.archived)
.any(|thread| {
thread
@@ -412,7 +411,7 @@ pub async fn persist_worktree_state(root: &RootPlan, cx: &mut AsyncApp) -> Resul
};
// Link all threads on this worktree to the archived record
- let session_ids: Vec<acp::SessionId> = store.read_with(cx, |store, _cx| {
+ let thread_ids: Vec<ThreadId> = store.read_with(cx, |store, _cx| {
store
.entries()
.filter(|thread| {
@@ -422,18 +421,14 @@ pub async fn persist_worktree_state(root: &RootPlan, cx: &mut AsyncApp) -> Resul
.iter()
.any(|p| p.as_path() == root.root_path)
})
- .map(|thread| thread.session_id.clone())
+ .map(|thread| thread.thread_id)
.collect()
});
- for session_id in &session_ids {
+ for thread_id in &thread_ids {
let link_result = store
.read_with(cx, |store, cx| {
- store.link_thread_to_archived_worktree(
- session_id.0.to_string(),
- archived_worktree_id,
- cx,
- )
+ store.link_thread_to_archived_worktree(*thread_id, archived_worktree_id, cx)
})
.await;
if let Err(error) = link_result {
@@ -636,21 +631,18 @@ pub async fn cleanup_archived_worktree_record(row: &ArchivedGitWorktree, cx: &mu
/// This unlinks the thread from all its archived worktrees and, for any
/// archived worktree that is no longer referenced by any other thread,
/// deletes the git ref and DB records.
-pub async fn cleanup_thread_archived_worktrees(session_id: &acp::SessionId, cx: &mut AsyncApp) {
+pub async fn cleanup_thread_archived_worktrees(thread_id: ThreadId, cx: &mut AsyncApp) {
let store = cx.update(|cx| ThreadMetadataStore::global(cx));
let archived_worktrees = store
.read_with(cx, |store, cx| {
- store.get_archived_worktrees_for_thread(session_id.0.to_string(), cx)
+ store.get_archived_worktrees_for_thread(thread_id, cx)
})
.await;
let archived_worktrees = match archived_worktrees {
Ok(rows) => rows,
Err(error) => {
- log::error!(
- "Failed to fetch archived worktrees for thread {}: {error:#}",
- session_id.0
- );
+ log::error!("Failed to fetch archived worktrees for thread {thread_id:?}: {error:#}");
return;
}
};
@@ -661,14 +653,11 @@ pub async fn cleanup_thread_archived_worktrees(session_id: &acp::SessionId, cx:
if let Err(error) = store
.read_with(cx, |store, cx| {
- store.unlink_thread_from_all_archived_worktrees(session_id.0.to_string(), cx)
+ store.unlink_thread_from_all_archived_worktrees(thread_id, cx)
})
.await
{
- log::error!(
- "Failed to unlink thread {} from archived worktrees: {error:#}",
- session_id.0
- );
+ log::error!("Failed to unlink thread {thread_id:?} from archived worktrees: {error:#}");
return;
}
diff --git a/crates/agent_ui/src/threads_archive_view.rs b/crates/agent_ui/src/threads_archive_view.rs
index 817a9deb3ae..8e6adc4038c 100644
--- a/crates/agent_ui/src/threads_archive_view.rs
+++ b/crates/agent_ui/src/threads_archive_view.rs
@@ -3,8 +3,8 @@ use std::sync::Arc;
use crate::agent_connection_store::AgentConnectionStore;
-use crate::thread_metadata_store::{ThreadMetadata, ThreadMetadataStore};
-use crate::{Agent, RemoveSelectedThread};
+use crate::thread_metadata_store::{ThreadId, ThreadMetadata, ThreadMetadataStore};
+use crate::{Agent, DEFAULT_THREAD_TITLE, RemoveSelectedThread};
use agent::ThreadStore;
use agent_client_protocol as acp;
@@ -113,7 +113,7 @@ fn fuzzy_match_positions(query: &str, text: &str) -> Option<Vec<usize>> {
pub enum ThreadsArchiveViewEvent {
Close,
Unarchive { thread: ThreadMetadata },
- CancelRestore { session_id: acp::SessionId },
+ CancelRestore { thread_id: ThreadId },
}
impl EventEmitter<ThreadsArchiveViewEvent> for ThreadsArchiveView {}
@@ -132,7 +132,7 @@ pub struct ThreadsArchiveView {
workspace: WeakEntity<Workspace>,
agent_connection_store: WeakEntity<AgentConnectionStore>,
agent_server_store: WeakEntity<AgentServerStore>,
- restoring: HashSet<acp::SessionId>,
+ restoring: HashSet<ThreadId>,
}
impl ThreadsArchiveView {
@@ -216,13 +216,13 @@ impl ThreadsArchiveView {
self.selection = None;
}
- pub fn mark_restoring(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
- self.restoring.insert(session_id.clone());
+ pub fn mark_restoring(&mut self, thread_id: &ThreadId, cx: &mut Context<Self>) {
+ self.restoring.insert(*thread_id);
cx.notify();
}
- pub fn clear_restoring(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
- self.restoring.remove(session_id);
+ pub fn clear_restoring(&mut self, thread_id: &ThreadId, cx: &mut Context<Self>) {
+ self.restoring.remove(thread_id);
cx.notify();
}
@@ -255,7 +255,14 @@ impl ThreadsArchiveView {
for session in sessions {
let highlight_positions = if !query.is_empty() {
- match fuzzy_match_positions(&query, &session.title) {
+ match fuzzy_match_positions(
+ &query,
+ session
+ .title
+ .as_ref()
+ .map(|t| t.as_ref())
+ .unwrap_or(DEFAULT_THREAD_TITLE),
+ ) {
Some(positions) => positions,
None => continue,
}
@@ -336,7 +343,7 @@ impl ThreadsArchiveView {
window: &mut Window,
cx: &mut Context<Self>,
) {
- if self.restoring.contains(&thread.session_id) {
+ if self.restoring.contains(&thread.thread_id) {
return;
}
@@ -345,7 +352,7 @@ impl ThreadsArchiveView {
return;
}
- self.mark_restoring(&thread.session_id, cx);
+ self.mark_restoring(&thread.thread_id, cx);
self.selection = None;
self.reset_filter_editor_text(window, cx);
cx.emit(ThreadsArchiveViewEvent::Unarchive { thread });
@@ -528,9 +535,9 @@ impl ThreadsArchiveView {
IconName::Sparkle
};
- let is_restoring = self.restoring.contains(&thread.session_id);
+ let is_restoring = self.restoring.contains(&thread.thread_id);
- let base = ThreadItem::new(id, thread.title.clone())
+ let base = ThreadItem::new(id, thread.display_title())
.icon(icon)
.when_some(icon_from_external_svg, |this, svg| {
this.custom_icon_from_external_svg(svg)
@@ -557,11 +564,11 @@ impl ThreadsArchiveView {
.icon_color(Color::Muted)
.tooltip(Tooltip::text("Cancel Restore"))
.on_click({
- let session_id = thread.session_id.clone();
+ let thread_id = thread.thread_id;
cx.listener(move |this, _, _, cx| {
- this.clear_restoring(&session_id, cx);
+ this.clear_restoring(&thread_id, cx);
cx.emit(ThreadsArchiveViewEvent::CancelRestore {
- session_id: session_id.clone(),
+ thread_id,
});
cx.stop_propagation();
})
@@ -586,10 +593,16 @@ impl ThreadsArchiveView {
})
.on_click({
let agent = thread.agent_id.clone();
+ let thread_id = thread.thread_id;
let session_id = thread.session_id.clone();
cx.listener(move |this, _, _, cx| {
this.preserve_selection_on_next_update = true;
- this.delete_thread(session_id.clone(), agent.clone(), cx);
+ this.delete_thread(
+ thread_id,
+ session_id.clone(),
+ agent.clone(),
+ cx,
+ );
cx.stop_propagation();
})
}),
@@ -619,17 +632,22 @@ impl ThreadsArchiveView {
};
self.preserve_selection_on_next_update = true;
- self.delete_thread(thread.session_id.clone(), thread.agent_id.clone(), cx);
+ self.delete_thread(
+ thread.thread_id,
+ thread.session_id.clone(),
+ thread.agent_id.clone(),
+ cx,
+ );
}
fn delete_thread(
&mut self,
- session_id: acp::SessionId,
+ thread_id: ThreadId,
+ session_id: Option<acp::SessionId>,
agent: AgentId,
cx: &mut Context<Self>,
) {
- ThreadMetadataStore::global(cx)
- .update(cx, |store, cx| store.delete(session_id.clone(), cx));
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| store.delete(thread_id, cx));
let agent = Agent::from(agent);
@@ -645,13 +663,16 @@ impl ThreadsArchiveView {
.wait_for_connection()
});
cx.spawn(async move |_this, cx| {
- crate::thread_worktree_archive::cleanup_thread_archived_worktrees(&session_id, cx)
- .await;
+ crate::thread_worktree_archive::cleanup_thread_archived_worktrees(thread_id, cx).await;
let state = task.await?;
let task = cx.update(|cx| {
- if let Some(list) = state.connection.session_list(cx) {
- list.delete_session(&session_id, cx)
+ if let Some(session_id) = &session_id {
+ if let Some(list) = state.connection.session_list(cx) {
+ list.delete_session(session_id, cx)
+ } else {
+ Task::ready(Ok(()))
+ }
} else {
Task::ready(Ok(()))
}
@@ -929,9 +950,9 @@ impl ProjectPickerDelegate {
cx: &mut Context<Picker<Self>>,
) {
self.thread.worktree_paths =
- super::thread_metadata_store::ThreadWorktreePaths::from_folder_paths(&paths);
+ super::thread_metadata_store::WorktreePaths::from_folder_paths(&paths);
ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.update_working_directories(&self.thread.session_id, paths, cx);
+ store.update_working_directories(self.thread.thread_id, paths, cx);
});
self.archive_view
@@ -995,7 +1016,15 @@ impl PickerDelegate for ProjectPickerDelegate {
type ListItem = AnyElement;
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
- format!("Associate the \"{}\" thread with...", self.thread.title).into()
+ format!(
+ "Associate the \"{}\" thread with...",
+ self.thread
+ .title
+ .as_ref()
+ .map(|t| t.as_ref())
+ .unwrap_or(DEFAULT_THREAD_TITLE)
+ )
+ .into()
}
fn render_editor(
diff --git a/crates/edit_prediction/src/license_detection.rs b/crates/edit_prediction/src/license_detection.rs
index 88edfc306eb..309b35f68fb 100644
--- a/crates/edit_prediction/src/license_detection.rs
+++ b/crates/edit_prediction/src/license_detection.rs
@@ -319,7 +319,7 @@ impl LicenseDetectionWatcher {
}
worktree::Event::DeletedEntry(_)
| worktree::Event::UpdatedGitRepositories(_)
- | worktree::Event::UpdatedRootRepoCommonDir
+ | worktree::Event::UpdatedRootRepoCommonDir { .. }
| worktree::Event::Deleted => {}
});
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs
index 1479f159138..47d6a7c0d17 100644
--- a/crates/project/src/lsp_store.rs
+++ b/crates/project/src/lsp_store.rs
@@ -4416,7 +4416,7 @@ impl LspStore {
worktree::Event::UpdatedGitRepositories(_)
| worktree::Event::DeletedEntry(_)
| worktree::Event::Deleted
- | worktree::Event::UpdatedRootRepoCommonDir => {}
+ | worktree::Event::UpdatedRootRepoCommonDir { .. } => {}
})
.detach()
}
diff --git a/crates/project/src/manifest_tree.rs b/crates/project/src/manifest_tree.rs
index fb1b7e96e4a..70d9cf7c00e 100644
--- a/crates/project/src/manifest_tree.rs
+++ b/crates/project/src/manifest_tree.rs
@@ -59,7 +59,7 @@ impl WorktreeRoots {
let path = TriePath::from(entry.path.as_ref());
this.roots.remove(&path);
}
- WorktreeEvent::Deleted | WorktreeEvent::UpdatedRootRepoCommonDir => {}
+ WorktreeEvent::Deleted | WorktreeEvent::UpdatedRootRepoCommonDir { .. } => {}
}
}),
})
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs
index 39e0cc9a0a0..515a518e530 100644
--- a/crates/project/src/project.rs
+++ b/crates/project/src/project.rs
@@ -51,6 +51,7 @@ pub use git_store::{
};
pub use manifest_tree::ManifestTree;
pub use project_search::{Search, SearchResults};
+pub use worktree_store::WorktreePaths;
use anyhow::{Context as _, Result, anyhow};
use buffer_store::{BufferStore, BufferStoreEvent};
@@ -246,6 +247,7 @@ pub struct Project {
toolchain_store: Option<Entity<ToolchainStore>>,
agent_location: Option<AgentLocation>,
downloading_files: Arc<Mutex<HashMap<(WorktreeId, String), DownloadingFile>>>,
+ last_worktree_paths: WorktreePaths,
}
struct DownloadingFile {
@@ -361,6 +363,9 @@ pub enum Event {
WorktreeRemoved(WorktreeId),
WorktreeUpdatedEntries(WorktreeId, UpdatedEntriesSet),
WorktreeUpdatedRootRepoCommonDir(WorktreeId),
+ WorktreePathsChanged {
+ old_worktree_paths: WorktreePaths,
+ },
DiskBasedDiagnosticsStarted {
language_server_id: LanguageServerId,
},
@@ -1339,6 +1344,7 @@ impl Project {
agent_location: None,
downloading_files: Default::default(),
+ last_worktree_paths: WorktreePaths::default(),
}
})
}
@@ -1576,6 +1582,7 @@ impl Project {
toolchain_store: Some(toolchain_store),
agent_location: None,
downloading_files: Default::default(),
+ last_worktree_paths: WorktreePaths::default(),
};
// remote server -> local machine handlers
@@ -1857,6 +1864,7 @@ impl Project {
toolchain_store: None,
agent_location: None,
downloading_files: Default::default(),
+ last_worktree_paths: WorktreePaths::default(),
};
project.set_role(role, cx);
for worktree in worktrees {
@@ -2067,6 +2075,40 @@ impl Project {
project
}
+ #[cfg(any(test, feature = "test-support"))]
+ pub fn add_test_remote_worktree(
+ &mut self,
+ abs_path: &str,
+ cx: &mut Context<Self>,
+ ) -> Entity<Worktree> {
+ use rpc::NoopProtoClient;
+ use util::paths::PathStyle;
+
+ let root_name = std::path::Path::new(abs_path)
+ .file_name()
+ .map(|n| n.to_string_lossy().to_string())
+ .unwrap_or_default();
+
+ let client = AnyProtoClient::new(NoopProtoClient::new());
+ let worktree = Worktree::remote(
+ 0,
+ ReplicaId::new(1),
+ proto::WorktreeMetadata {
+ id: 100 + self.visible_worktrees(cx).count() as u64,
+ root_name,
+ visible: true,
+ abs_path: abs_path.to_string(),
+ root_repo_common_dir: None,
+ },
+ client,
+ PathStyle::Posix,
+ cx,
+ );
+ self.worktree_store
+ .update(cx, |store, cx| store.add(&worktree, cx));
+ worktree
+ }
+
#[inline]
pub fn dap_store(&self) -> Entity<DapStore> {
self.dap_store.clone()
@@ -2351,20 +2393,13 @@ impl Project {
.find(|tree| tree.read(cx).root_name() == root_name)
}
- pub fn project_group_key(&self, cx: &App) -> ProjectGroupKey {
- let roots = self
- .visible_worktrees(cx)
- .map(|worktree| {
- let snapshot = worktree.read(cx).snapshot();
- snapshot
- .root_repo_common_dir()
- .and_then(|dir| Some(dir.parent()?.to_path_buf()))
- .unwrap_or(snapshot.abs_path().to_path_buf())
- })
- .collect::<Vec<_>>();
- let host = self.remote_connection_options(cx);
- let path_list = PathList::new(&roots);
- ProjectGroupKey::new(host, path_list)
+ fn emit_group_key_changed_if_needed(&mut self, cx: &mut Context<Self>) {
+ let new_worktree_paths = self.worktree_paths(cx);
+ if new_worktree_paths != self.last_worktree_paths {
+ let old_worktree_paths =
+ std::mem::replace(&mut self.last_worktree_paths, new_worktree_paths);
+ cx.emit(Event::WorktreePathsChanged { old_worktree_paths });
+ }
}
#[inline]
@@ -3683,9 +3718,11 @@ impl Project {
WorktreeStoreEvent::WorktreeAdded(worktree) => {
self.on_worktree_added(worktree, cx);
cx.emit(Event::WorktreeAdded(worktree.read(cx).id()));
+ self.emit_group_key_changed_if_needed(cx);
}
WorktreeStoreEvent::WorktreeRemoved(_, id) => {
cx.emit(Event::WorktreeRemoved(*id));
+ self.emit_group_key_changed_if_needed(cx);
}
WorktreeStoreEvent::WorktreeReleased(_, id) => {
self.on_worktree_released(*id, cx);
@@ -3705,6 +3742,7 @@ impl Project {
WorktreeStoreEvent::WorktreeUpdatedGitRepositories(_, _) => {}
WorktreeStoreEvent::WorktreeUpdatedRootRepoCommonDir(worktree_id) => {
cx.emit(Event::WorktreeUpdatedRootRepoCommonDir(*worktree_id));
+ self.emit_group_key_changed_if_needed(cx);
}
}
}
@@ -6109,6 +6147,14 @@ impl Project {
worktree.read(cx).entry_for_path(rel_path).is_some()
})
}
+
+ pub fn worktree_paths(&self, cx: &App) -> WorktreePaths {
+ self.worktree_store.read(cx).paths(cx)
+ }
+
+ pub fn project_group_key(&self, cx: &App) -> ProjectGroupKey {
+ ProjectGroupKey::from_project(self, cx)
+ }
}
/// Identifies a project group by a set of paths the workspaces in this group
@@ -6116,7 +6162,7 @@ impl Project {
///
/// Paths are mapped to their main worktree path first so we can group
/// workspaces by main repos.
-#[derive(PartialEq, Eq, Hash, Clone, Debug)]
+#[derive(PartialEq, Eq, Hash, Clone, Debug, Default)]
pub struct ProjectGroupKey {
/// The paths of the main worktrees for this project group.
paths: PathList,
@@ -6131,6 +6177,25 @@ impl ProjectGroupKey {
Self { paths, host }
}
+ pub fn from_project(project: &Project, cx: &App) -> Self {
+ let paths = project.worktree_paths(cx);
+ let host = project.remote_connection_options(cx);
+ Self {
+ paths: paths.main_worktree_path_list().clone(),
+ host,
+ }
+ }
+
+ pub fn from_worktree_paths(
+ paths: &WorktreePaths,
+ host: Option<RemoteConnectionOptions>,
+ ) -> Self {
+ Self {
+ paths: paths.main_worktree_path_list().clone(),
+ host,
+ }
+ }
+
pub fn path_list(&self) -> &PathList {
&self.paths
}
@@ -6140,7 +6205,7 @@ impl ProjectGroupKey {
path_detail_map: &std::collections::HashMap<PathBuf, usize>,
) -> SharedString {
let mut names = Vec::with_capacity(self.paths.paths().len());
- for abs_path in self.paths.paths() {
+ for abs_path in self.paths.ordered_paths() {
let detail = path_detail_map.get(abs_path).copied().unwrap_or(0);
let suffix = path_suffix(abs_path, detail);
if !suffix.is_empty() {
diff --git a/crates/project/src/worktree_store.rs b/crates/project/src/worktree_store.rs
index be95a6b0ded..ab5c46752c6 100644
--- a/crates/project/src/worktree_store.rs
+++ b/crates/project/src/worktree_store.rs
@@ -24,6 +24,7 @@ use rpc::{
use text::ReplicaId;
use util::{
ResultExt,
+ path_list::PathList,
paths::{PathStyle, RemotePathBuf, SanitizedPath},
rel_path::RelPath,
};
@@ -34,6 +35,121 @@ use worktree::{
use crate::{ProjectPath, trusted_worktrees::TrustedWorktrees};
+/// The current paths for a project's worktrees. Each folder path has a corresponding
+/// main worktree path at the same position. The two lists are always the
+/// same length and are modified together via `add_path` / `remove_main_path`.
+///
+/// For non-linked worktrees, the main path and folder path are identical.
+/// For linked worktrees, the main path is the original repo and the folder
+/// path is the linked worktree location.
+#[derive(Default, Debug, Clone)]
+pub struct WorktreePaths {
+ paths: PathList,
+ main_paths: PathList,
+}
+
+impl PartialEq for WorktreePaths {
+ fn eq(&self, other: &Self) -> bool {
+ self.paths == other.paths && self.main_paths == other.main_paths
+ }
+}
+
+impl WorktreePaths {
+ /// Build from two parallel `PathList`s that already share the same
+ /// insertion order. Used for deserialization from DB.
+ ///
+ /// Returns an error if the two lists have different lengths, which
+ /// indicates corrupted data from a prior migration bug.
+ pub fn from_path_lists(
+ main_worktree_paths: PathList,
+ folder_paths: PathList,
+ ) -> anyhow::Result<Self> {
+ anyhow::ensure!(
+ main_worktree_paths.paths().len() == folder_paths.paths().len(),
+ "main_worktree_paths has {} entries but folder_paths has {}",
+ main_worktree_paths.paths().len(),
+ folder_paths.paths().len(),
+ );
+ Ok(Self {
+ paths: folder_paths,
+ main_paths: main_worktree_paths,
+ })
+ }
+
+ /// Build for non-linked worktrees where main == folder for every path.
+ pub fn from_folder_paths(folder_paths: &PathList) -> Self {
+ Self {
+ paths: folder_paths.clone(),
+ main_paths: folder_paths.clone(),
+ }
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.paths.is_empty()
+ }
+
+ /// The folder paths (for workspace matching / `threads_by_paths` index).
+ pub fn folder_path_list(&self) -> &PathList {
+ &self.paths
+ }
+
+ /// The main worktree paths (for group key / `threads_by_main_paths` index).
+ pub fn main_worktree_path_list(&self) -> &PathList {
+ &self.main_paths
+ }
+
+ /// Iterate the (main_worktree_path, folder_path) pairs in insertion order.
+ pub fn ordered_pairs(&self) -> impl Iterator<Item = (&PathBuf, &PathBuf)> {
+ self.main_paths
+ .ordered_paths()
+ .zip(self.paths.ordered_paths())
+ }
+
+ /// Add a new path pair. If the exact (main, folder) pair already exists,
+ /// this is a no-op. Rebuilds both internal `PathList`s to maintain
+ /// consistent ordering.
+ pub fn add_path(&mut self, main_path: &Path, folder_path: &Path) {
+ let already_exists = self
+ .ordered_pairs()
+ .any(|(m, f)| m.as_path() == main_path && f.as_path() == folder_path);
+ if already_exists {
+ return;
+ }
+ let (mut mains, mut folders): (Vec<PathBuf>, Vec<PathBuf>) = self
+ .ordered_pairs()
+ .map(|(m, f)| (m.clone(), f.clone()))
+ .unzip();
+ mains.push(main_path.to_path_buf());
+ folders.push(folder_path.to_path_buf());
+ self.main_paths = PathList::new(&mains);
+ self.paths = PathList::new(&folders);
+ }
+
+ /// Remove all pairs whose main worktree path matches the given path.
+ /// This removes the corresponding entries from both lists.
+ pub fn remove_main_path(&mut self, main_path: &Path) {
+ let (mains, folders): (Vec<PathBuf>, Vec<PathBuf>) = self
+ .ordered_pairs()
+ .filter(|(m, _)| m.as_path() != main_path)
+ .map(|(m, f)| (m.clone(), f.clone()))
+ .unzip();
+ self.main_paths = PathList::new(&mains);
+ self.paths = PathList::new(&folders);
+ }
+
+ /// Remove all pairs whose folder path matches the given path.
+ /// This removes the corresponding entries from both lists.
+ pub fn remove_folder_path(&mut self, folder_path: &Path) {
+ let (mains, folders): (Vec<PathBuf>, Vec<PathBuf>) = self
+ .ordered_pairs()
+ .filter(|(_, f)| f.as_path() != folder_path)
+ .map(|(m, f)| (m.clone(), f.clone()))
+ .unzip();
+ self.main_paths = PathList::new(&mains);
+ self.paths = PathList::new(&folders);
+ }
+}
+
enum WorktreeStoreState {
Local {
fs: Arc<dyn Fs>,
@@ -814,7 +930,7 @@ impl WorktreeStore {
// The worktree root itself has been deleted (for single-file worktrees)
// The worktree will be removed via the observe_release callback
}
- worktree::Event::UpdatedRootRepoCommonDir => {
+ worktree::Event::UpdatedRootRepoCommonDir { .. } => {
cx.emit(WorktreeStoreEvent::WorktreeUpdatedRootRepoCommonDir(
worktree_id,
));
@@ -1263,6 +1379,32 @@ impl WorktreeStore {
WorktreeStoreState::Remote { .. } => None,
}
}
+
+ pub fn paths(&self, cx: &App) -> WorktreePaths {
+ let (mains, folders): (Vec<PathBuf>, Vec<PathBuf>) = self
+ .visible_worktrees(cx)
+ .filter(|worktree| {
+ let worktree = worktree.read(cx);
+ // Remote worktrees that haven't received their first update
+ // don't have enough data to contribute yet.
+ !worktree.is_remote() || worktree.root_entry().is_some()
+ })
+ .map(|worktree| {
+ let snapshot = worktree.read(cx).snapshot();
+ let folder_path = snapshot.abs_path().to_path_buf();
+ let main_path = snapshot
+ .root_repo_common_dir()
+ .and_then(|dir| Some(dir.parent()?.to_path_buf()))
+ .unwrap_or_else(|| folder_path.clone());
+ (main_path, folder_path)
+ })
+ .unzip();
+
+ WorktreePaths {
+ paths: PathList::new(&folders),
+ main_paths: PathList::new(&mains),
+ }
+ }
}
#[derive(Clone, Debug)]
diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs
index c90f2f69154..f9284fc572e 100644
--- a/crates/recent_projects/src/recent_projects.rs
+++ b/crates/recent_projects/src/recent_projects.rs
@@ -32,11 +32,12 @@ use picker::{
Picker, PickerDelegate,
highlighted_match_with_paths::{HighlightedMatch, HighlightedMatchWithPaths},
};
-use project::{ProjectGroupKey, Worktree, git_store::Repository};
+use project::{Worktree, git_store::Repository};
pub use remote_connections::RemoteSettings;
pub use remote_servers::RemoteServerProjects;
use settings::{Settings, WorktreeId};
use ui_input::ErasedEditor;
+use workspace::ProjectGroupKey;
use dev_container::{DevContainerContext, find_devcontainer_configs};
use ui::{
@@ -380,7 +381,7 @@ pub fn init(cx: &mut App) {
multi_workspace
.update(cx, |multi_workspace, window, cx| {
let window_project_groups: Vec<ProjectGroupKey> =
- multi_workspace.project_group_keys().cloned().collect();
+ multi_workspace.project_group_keys();
let workspace = multi_workspace.workspace().clone();
workspace.update(cx, |workspace, cx| {
@@ -1613,11 +1614,22 @@ impl PickerDelegate for RecentProjectsDelegate {
.border_t_1()
.border_color(cx.theme().colors().border_variant)
.child({
- let open_action = workspace::Open::default();
+ let open_action = workspace::Open {
+ create_new_window: self.create_new_window,
+ };
Button::new("open_local_folder", "Open Local Project")
.key_binding(KeyBinding::for_action_in(&open_action, &focus_handle, cx))
- .on_click(move |_, window, cx| {
- window.dispatch_action(open_action.boxed_clone(), cx)
+ .on_click({
+ let workspace = self.workspace.clone();
+ let create_new_window = self.create_new_window;
+ move |_, window, cx| {
+ open_local_project(
+ workspace.clone(),
+ create_new_window,
+ window,
+ cx,
+ );
+ }
})
})
.child(
@@ -1764,6 +1776,9 @@ impl PickerDelegate for RecentProjectsDelegate {
)
.menu({
let focus_handle = focus_handle.clone();
+ let workspace_handle = self.workspace.clone();
+ let create_new_window = self.create_new_window;
+ let open_action = workspace::Open { create_new_window };
let show_add_to_workspace = match selected_entry {
Some(ProjectPickerEntry::RecentProject(hit)) => self
.workspaces
@@ -1778,6 +1793,8 @@ impl PickerDelegate for RecentProjectsDelegate {
move |window, cx| {
Some(ContextMenu::build(window, cx, {
let focus_handle = focus_handle.clone();
+ let workspace_handle = workspace_handle.clone();
+ let open_action = open_action.clone();
move |menu, _, _| {
menu.context(focus_handle)
.when(show_add_to_workspace, |menu| {
@@ -1787,9 +1804,20 @@ impl PickerDelegate for RecentProjectsDelegate {
)
.separator()
})
- .action(
+ .entry(
"Open Local Project",
- workspace::Open::default().boxed_clone(),
+ Some(open_action.boxed_clone()),
+ {
+ let workspace_handle = workspace_handle.clone();
+ move |window, cx| {
+ open_local_project(
+ workspace_handle.clone(),
+ create_new_window,
+ window,
+ cx,
+ );
+ }
+ },
)
.action(
"Open Remote Project",
@@ -1869,6 +1897,67 @@ pub(crate) fn highlights_for_path(
},
)
}
+fn open_local_project(
+ workspace: WeakEntity<Workspace>,
+ create_new_window: bool,
+ window: &mut Window,
+ cx: &mut App,
+) {
+ use gpui::PathPromptOptions;
+ use project::DirectoryLister;
+
+ let Some(workspace) = workspace.upgrade() else {
+ return;
+ };
+
+ let paths = workspace.update(cx, |workspace, cx| {
+ workspace.prompt_for_open_path(
+ PathPromptOptions {
+ files: true,
+ directories: true,
+ multiple: false,
+ prompt: None,
+ },
+ DirectoryLister::Local(
+ workspace.project().clone(),
+ workspace.app_state().fs.clone(),
+ ),
+ window,
+ cx,
+ )
+ });
+
+ let multi_workspace_handle = window.window_handle().downcast::<MultiWorkspace>();
+ window
+ .spawn(cx, async move |cx| {
+ let Some(paths) = paths.await.log_err().flatten() else {
+ return;
+ };
+ if !create_new_window {
+ if let Some(handle) = multi_workspace_handle {
+ if let Some(task) = handle
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.open_project(paths, OpenMode::Activate, window, cx)
+ })
+ .log_err()
+ {
+ task.await.log_err();
+ }
+ return;
+ }
+ }
+ if let Some(task) = workspace
+ .update_in(cx, |workspace, window, cx| {
+ workspace.open_workspace_for_paths(OpenMode::NewWindow, paths, window, cx)
+ })
+ .log_err()
+ {
+ task.await.log_err();
+ }
+ })
+ .detach();
+}
+
impl RecentProjectsDelegate {
fn add_project_to_workspace(
&mut self,
@@ -2032,9 +2121,10 @@ impl RecentProjectsDelegate {
#[cfg(test)]
mod tests {
- use gpui::{TestAppContext, VisualTestContext};
+ use gpui::{TestAppContext, UpdateGlobal, VisualTestContext};
use serde_json::json;
+ use settings::SettingsStore;
use util::path;
use workspace::{AppState, open_paths};
@@ -2227,6 +2317,159 @@ mod tests {
.unwrap();
}
+ #[gpui::test]
+ async fn test_open_local_project_reuses_multi_workspace_window(cx: &mut TestAppContext) {
+ let app_state = init_test(cx);
+
+ // Disable system path prompts so the injected mock is used.
+ cx.update(|cx| {
+ SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.workspace.use_system_path_prompts = Some(false);
+ });
+ });
+ });
+
+ app_state
+ .fs
+ .as_fake()
+ .insert_tree(
+ path!("/initial-project"),
+ json!({ "src": { "main.rs": "" } }),
+ )
+ .await;
+ app_state
+ .fs
+ .as_fake()
+ .insert_tree(path!("/new-project"), json!({ "lib": { "mod.rs": "" } }))
+ .await;
+
+ cx.update(|cx| {
+ open_paths(
+ &[PathBuf::from(path!("/initial-project"))],
+ app_state.clone(),
+ workspace::OpenOptions::default(),
+ cx,
+ )
+ })
+ .await
+ .unwrap();
+
+ let initial_window_count = cx.update(|cx| cx.windows().len());
+ assert_eq!(initial_window_count, 1);
+
+ let multi_workspace = cx.update(|cx| cx.windows()[0].downcast::<MultiWorkspace>().unwrap());
+ cx.run_until_parked();
+
+ let workspace = multi_workspace
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+
+ // Set up the prompt mock to return the new project path.
+ workspace.update(cx, |workspace, _cx| {
+ workspace.set_prompt_for_open_path(Box::new(|_, _, _, _| {
+ let (tx, rx) = futures::channel::oneshot::channel();
+ tx.send(Some(vec![PathBuf::from(path!("/new-project"))]))
+ .ok();
+ rx
+ }));
+ });
+
+ // Call open_local_project with create_new_window: false.
+ let weak_workspace = workspace.downgrade();
+ multi_workspace
+ .update(cx, |_, window, cx| {
+ open_local_project(weak_workspace, false, window, cx);
+ })
+ .unwrap();
+
+ cx.run_until_parked();
+
+ // Should NOT have opened a new window.
+ let final_window_count = cx.update(|cx| cx.windows().len());
+ assert_eq!(
+ final_window_count, initial_window_count,
+ "open_local_project with create_new_window=false should reuse the current multi-workspace window"
+ );
+ }
+
+ #[gpui::test]
+ async fn test_open_local_project_new_window_creates_new_window(cx: &mut TestAppContext) {
+ let app_state = init_test(cx);
+
+ // Disable system path prompts so the injected mock is used.
+ cx.update(|cx| {
+ SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.workspace.use_system_path_prompts = Some(false);
+ });
+ });
+ });
+
+ app_state
+ .fs
+ .as_fake()
+ .insert_tree(
+ path!("/initial-project"),
+ json!({ "src": { "main.rs": "" } }),
+ )
+ .await;
+ app_state
+ .fs
+ .as_fake()
+ .insert_tree(path!("/new-project"), json!({ "lib": { "mod.rs": "" } }))
+ .await;
+
+ cx.update(|cx| {
+ open_paths(
+ &[PathBuf::from(path!("/initial-project"))],
+ app_state.clone(),
+ workspace::OpenOptions::default(),
+ cx,
+ )
+ })
+ .await
+ .unwrap();
+
+ let initial_window_count = cx.update(|cx| cx.windows().len());
+ assert_eq!(initial_window_count, 1);
+
+ let multi_workspace = cx.update(|cx| cx.windows()[0].downcast::<MultiWorkspace>().unwrap());
+ cx.run_until_parked();
+
+ let workspace = multi_workspace
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+
+ // Set up the prompt mock to return the new project path.
+ workspace.update(cx, |workspace, _cx| {
+ workspace.set_prompt_for_open_path(Box::new(|_, _, _, _| {
+ let (tx, rx) = futures::channel::oneshot::channel();
+ tx.send(Some(vec![PathBuf::from(path!("/new-project"))]))
+ .ok();
+ rx
+ }));
+ });
+
+ // Call open_local_project with create_new_window: true.
+ let weak_workspace = workspace.downgrade();
+ multi_workspace
+ .update(cx, |_, window, cx| {
+ open_local_project(weak_workspace, true, window, cx);
+ })
+ .unwrap();
+
+ cx.run_until_parked();
+
+ // Should have opened a new window.
+ let final_window_count = cx.update(|cx| cx.windows().len());
+ assert_eq!(
+ final_window_count,
+ initial_window_count + 1,
+ "open_local_project with create_new_window=true should open a new window"
+ );
+ }
+
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
cx.update(|cx| {
let state = AppState::test(cx);
diff --git a/crates/recent_projects/src/sidebar_recent_projects.rs b/crates/recent_projects/src/sidebar_recent_projects.rs
index 2c697de73fb..7e0634e9e9c 100644
--- a/crates/recent_projects/src/sidebar_recent_projects.rs
+++ b/crates/recent_projects/src/sidebar_recent_projects.rs
@@ -10,15 +10,14 @@ use picker::{
Picker, PickerDelegate,
highlighted_match_with_paths::{HighlightedMatch, HighlightedMatchWithPaths},
};
-use project::ProjectGroupKey;
use remote::RemoteConnectionOptions;
use settings::Settings;
use ui::{KeyBinding, ListItem, ListItemSpacing, Tooltip, prelude::*};
use ui_input::ErasedEditor;
use util::{ResultExt, paths::PathExt};
use workspace::{
- MultiWorkspace, OpenMode, OpenOptions, PathList, SerializedWorkspaceLocation, Workspace,
- WorkspaceDb, WorkspaceId, notifications::DetachAndPromptErr,
+ MultiWorkspace, OpenMode, OpenOptions, PathList, ProjectGroupKey, SerializedWorkspaceLocation,
+ Workspace, WorkspaceDb, WorkspaceId, notifications::DetachAndPromptErr,
};
use zed_actions::OpenRemote;
diff --git a/crates/rpc/src/proto_client.rs b/crates/rpc/src/proto_client.rs
index 57cf7d6f67f..ba8b8782725 100644
--- a/crates/rpc/src/proto_client.rs
+++ b/crates/rpc/src/proto_client.rs
@@ -547,3 +547,43 @@ fn to_any_envelope<T: EnvelopedMessage>(
payload: response,
}) as Box<_>
}
+
+#[cfg(any(test, feature = "test-support"))]
+pub struct NoopProtoClient {
+ handler_set: parking_lot::Mutex<ProtoMessageHandlerSet>,
+}
+
+#[cfg(any(test, feature = "test-support"))]
+impl NoopProtoClient {
+ pub fn new() -> Arc<Self> {
+ Arc::new(Self {
+ handler_set: parking_lot::Mutex::new(ProtoMessageHandlerSet::default()),
+ })
+ }
+}
+
+#[cfg(any(test, feature = "test-support"))]
+impl ProtoClient for NoopProtoClient {
+ fn request(
+ &self,
+ _: proto::Envelope,
+ _: &'static str,
+ ) -> futures::future::BoxFuture<'static, Result<proto::Envelope>> {
+ unimplemented!()
+ }
+ fn send(&self, _: proto::Envelope, _: &'static str) -> Result<()> {
+ Ok(())
+ }
+ fn send_response(&self, _: proto::Envelope, _: &'static str) -> Result<()> {
+ Ok(())
+ }
+ fn message_handler_set(&self) -> &parking_lot::Mutex<ProtoMessageHandlerSet> {
+ &self.handler_set
+ }
+ fn is_via_collab(&self) -> bool {
+ false
+ }
+ fn has_wsl_interop(&self) -> bool {
+ false
+ }
+}
diff --git a/crates/sidebar/Cargo.toml b/crates/sidebar/Cargo.toml
index e9ef4dea630..dcc960141e3 100644
--- a/crates/sidebar/Cargo.toml
+++ b/crates/sidebar/Cargo.toml
@@ -65,6 +65,7 @@ git.workspace = true
gpui = { workspace = true, features = ["test-support"] }
client = { workspace = true, features = ["test-support"] }
clock = { workspace = true, features = ["test-support"] }
+db = { workspace = true, features = ["test-support"] }
http_client = { workspace = true, features = ["test-support"] }
node_runtime = { workspace = true, features = ["test-support"] }
project = { workspace = true, features = ["test-support"] }
diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs
index 8864d9e7faa..a846d2c2317 100644
--- a/crates/sidebar/src/sidebar.rs
+++ b/crates/sidebar/src/sidebar.rs
@@ -4,14 +4,14 @@ use acp_thread::ThreadStatus;
use action_log::DiffStats;
use agent_client_protocol::{self as acp};
use agent_settings::AgentSettings;
-use agent_ui::thread_metadata_store::{ThreadMetadata, ThreadMetadataStore, ThreadWorktreePaths};
+use agent_ui::thread_metadata_store::{ThreadMetadata, ThreadMetadataStore, WorktreePaths};
use agent_ui::thread_worktree_archive;
use agent_ui::threads_archive_view::{
ThreadsArchiveView, ThreadsArchiveViewEvent, format_history_entry_timestamp,
};
use agent_ui::{
- AcpThreadImportOnboarding, Agent, AgentPanel, AgentPanelEvent, DEFAULT_THREAD_TITLE, DraftId,
- NewThread, RemoveSelectedThread, ThreadImportModal,
+ AcpThreadImportOnboarding, Agent, AgentPanel, AgentPanelEvent, DEFAULT_THREAD_TITLE, NewThread,
+ RemoveSelectedThread, ThreadId, ThreadImportModal,
};
use chrono::{DateTime, Utc};
use editor::Editor;
@@ -23,9 +23,7 @@ use gpui::{
use menu::{
Cancel, Confirm, SelectChild, SelectFirst, SelectLast, SelectNext, SelectParent, SelectPrevious,
};
-use project::{
- AgentId, AgentRegistryStore, Event as ProjectEvent, ProjectGroupKey, linked_worktree_short_name,
-};
+use project::{AgentId, AgentRegistryStore, Event as ProjectEvent, linked_worktree_short_name};
use recent_projects::sidebar_recent_projects::SidebarRecentProjects;
use remote::RemoteConnectionOptions;
use ui::utils::platform_title_bar_height;
@@ -46,7 +44,7 @@ use util::ResultExt as _;
use util::path_list::PathList;
use workspace::{
AddFolderToProject, CloseWindow, FocusWorkspaceSidebar, MultiWorkspace, MultiWorkspaceEvent,
- NextProject, NextThread, Open, PreviousProject, PreviousThread, SerializedProjectGroupKey,
+ NextProject, NextThread, Open, PreviousProject, PreviousThread, ProjectGroupKey,
ShowFewerThreads, ShowMoreThreads, Sidebar as WorkspaceSidebar, SidebarSide, Toast,
ToggleWorkspaceSidebar, Workspace, notifications::NotificationId, sidebar_side_context_menu,
};
@@ -96,10 +94,6 @@ struct SerializedSidebar {
#[serde(default)]
width: Option<f32>,
#[serde(default)]
- collapsed_groups: Vec<SerializedProjectGroupKey>,
- #[serde(default)]
- expanded_groups: Vec<(SerializedProjectGroupKey, usize)>,
- #[serde(default)]
active_view: SerializedSidebarView,
}
@@ -116,45 +110,34 @@ enum ArchiveWorktreeOutcome {
}
#[derive(Clone, Debug)]
-enum ActiveEntry {
- Thread {
- session_id: acp::SessionId,
- workspace: Entity<Workspace>,
- },
- Draft {
- id: DraftId,
- workspace: Entity<Workspace>,
- },
+struct ActiveEntry {
+ thread_id: agent_ui::ThreadId,
+ /// Stable remote identifier, used for matching when thread_id
+ /// differs (e.g. after cross-window activation creates a new
+ /// local ThreadId).
+ session_id: Option<acp::SessionId>,
+ workspace: Entity<Workspace>,
}
impl ActiveEntry {
fn workspace(&self) -> &Entity<Workspace> {
- match self {
- ActiveEntry::Thread { workspace, .. } => workspace,
- ActiveEntry::Draft { workspace, .. } => workspace,
- }
- }
-
- fn is_active_thread(&self, session_id: &acp::SessionId) -> bool {
- matches!(self, ActiveEntry::Thread { session_id: id, .. } if id == session_id)
+ &self.workspace
}
- fn is_active_draft(&self, draft_id: DraftId) -> bool {
- matches!(self, ActiveEntry::Draft { id, .. } if *id == draft_id)
+ fn is_active_thread(&self, thread_id: &agent_ui::ThreadId) -> bool {
+ self.thread_id == *thread_id
}
fn matches_entry(&self, entry: &ListEntry) -> bool {
- match (self, entry) {
- (ActiveEntry::Thread { session_id, .. }, ListEntry::Thread(thread)) => {
- thread.metadata.session_id == *session_id
+ match entry {
+ ListEntry::Thread(thread) => {
+ self.thread_id == thread.metadata.thread_id
+ || self
+ .session_id
+ .as_ref()
+ .zip(thread.metadata.session_id.as_ref())
+ .is_some_and(|(a, b)| a == b)
}
- (
- ActiveEntry::Draft { id, .. },
- ListEntry::DraftThread {
- draft_id: Some(entry_id),
- ..
- },
- ) => *id == *entry_id,
_ => false,
}
}
@@ -214,6 +197,7 @@ struct ThreadEntry {
is_live: bool,
is_background: bool,
is_title_generating: bool,
+ is_draft: bool,
highlight_positions: Vec<usize>,
worktrees: Vec<WorktreeInfo>,
diff_stats: DiffStats,
@@ -226,7 +210,7 @@ impl ThreadEntry {
/// but if we have a correspond thread already loaded we want to apply the
/// live information.
fn apply_active_info(&mut self, info: &ActiveThreadInfo) {
- self.metadata.title = info.title.clone();
+ self.metadata.title = Some(info.title.clone());
self.status = info.status;
self.icon = info.icon;
self.icon_from_external_svg = info.icon_from_external_svg.clone();
@@ -253,21 +237,13 @@ enum ListEntry {
key: ProjectGroupKey,
is_fully_expanded: bool,
},
- DraftThread {
- /// `None` for placeholder entries in empty groups with no open
- /// workspace. `Some` for drafts backed by an AgentPanel.
- draft_id: Option<DraftId>,
- key: project::ProjectGroupKey,
- workspace: Option<Entity<Workspace>>,
- worktrees: Vec<WorktreeInfo>,
- },
}
#[cfg(test)]
impl ListEntry {
fn session_id(&self) -> Option<&acp::SessionId> {
match self {
- ListEntry::Thread(thread_entry) => Some(&thread_entry.metadata.session_id),
+ ListEntry::Thread(thread_entry) => thread_entry.metadata.session_id.as_ref(),
_ => None,
}
}
@@ -282,11 +258,9 @@ impl ListEntry {
ThreadEntryWorkspace::Open(ws) => vec![ws.clone()],
ThreadEntryWorkspace::Closed { .. } => Vec::new(),
},
- ListEntry::DraftThread { workspace, .. } => workspace.iter().cloned().collect(),
ListEntry::ProjectHeader { key, .. } => multi_workspace
.workspaces_for_project_group(key, cx)
- .cloned()
- .collect(),
+ .unwrap_or_default(),
ListEntry::ViewMore { .. } => Vec::new(),
}
}
@@ -301,14 +275,14 @@ impl From<ThreadEntry> for ListEntry {
#[derive(Default)]
struct SidebarContents {
entries: Vec<ListEntry>,
- notified_threads: HashSet<acp::SessionId>,
+ notified_threads: HashSet<agent_ui::ThreadId>,
project_header_indices: Vec<usize>,
has_open_projects: bool,
}
impl SidebarContents {
- fn is_thread_notified(&self, session_id: &acp::SessionId) -> bool {
- self.notified_threads.contains(session_id)
+ fn is_thread_notified(&self, thread_id: &agent_ui::ThreadId) -> bool {
+ self.notified_threads.contains(thread_id)
}
}
@@ -369,7 +343,7 @@ fn workspace_path_list(workspace: &Entity<Workspace>, cx: &App) -> PathList {
/// wouldn't identify which main project it belongs to, the main project
/// name is prefixed for disambiguation (e.g. `project:feature`).
///
-fn worktree_info_from_thread_paths(worktree_paths: &ThreadWorktreePaths) -> Vec<WorktreeInfo> {
+fn worktree_info_from_thread_paths(worktree_paths: &WorktreePaths) -> Vec<WorktreeInfo> {
let mut infos: Vec<WorktreeInfo> = Vec::new();
let mut linked_short_names: Vec<(SharedString, SharedString)> = Vec::new();
let mut unique_main_count = HashSet::new();
@@ -454,8 +428,7 @@ pub struct Sidebar {
/// Tracks which sidebar entry is currently active (highlighted).
active_entry: Option<ActiveEntry>,
hovered_thread_index: Option<usize>,
- collapsed_groups: HashSet<ProjectGroupKey>,
- expanded_groups: HashMap<ProjectGroupKey, usize>,
+
/// Updated only in response to explicit user actions (clicking a
/// thread, confirming in the thread switcher, etc.) — never from
/// background data changes. Used to sort the thread switcher popup.
@@ -463,16 +436,17 @@ pub struct Sidebar {
/// Updated when the user presses a key to send or queue a message.
/// Used for sorting threads in the sidebar and as a secondary sort
/// key in the thread switcher.
- thread_last_message_sent_or_queued: HashMap<acp::SessionId, DateTime<Utc>>,
+ thread_last_message_sent_or_queued: HashMap<agent_ui::ThreadId, DateTime<Utc>>,
thread_switcher: Option<Entity<ThreadSwitcher>>,
_thread_switcher_subscriptions: Vec<gpui::Subscription>,
- pending_remote_thread_activation: Option<acp::SessionId>,
+ pending_thread_activation: Option<agent_ui::ThreadId>,
view: SidebarView,
- restoring_tasks: HashMap<acp::SessionId, Task<()>>,
+ restoring_tasks: HashMap<agent_ui::ThreadId, Task<()>>,
recent_projects_popover_handle: PopoverMenuHandle<SidebarRecentProjects>,
project_header_menu_ix: Option<usize>,
_subscriptions: Vec<gpui::Subscription>,
- _draft_observation: Option<gpui::Subscription>,
+ _draft_observations: Vec<gpui::Subscription>,
+ reconciling: bool,
}
impl Sidebar {
@@ -497,43 +471,24 @@ impl Sidebar {
window,
|this, _multi_workspace, event: &MultiWorkspaceEvent, window, cx| match event {
MultiWorkspaceEvent::ActiveWorkspaceChanged => {
- this.observe_draft_editor(cx);
+ this.sync_active_entry_from_active_workspace(cx);
+ this.observe_draft_editors(cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
MultiWorkspaceEvent::WorkspaceAdded(workspace) => {
this.subscribe_to_workspace(workspace, window, cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
MultiWorkspaceEvent::WorkspaceRemoved(_) => {
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
- MultiWorkspaceEvent::WorktreePathAdded {
- old_main_paths,
- added_path,
- } => {
- let added_path = added_path.clone();
- ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.change_worktree_paths(
- old_main_paths,
- |paths| paths.add_path(&added_path, &added_path),
- cx,
- );
- });
- this.update_entries(cx);
- }
- MultiWorkspaceEvent::WorktreePathRemoved {
- old_main_paths,
- removed_path,
- } => {
- let removed_path = removed_path.clone();
- ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.change_worktree_paths(
- old_main_paths,
- |paths| paths.remove_main_path(&removed_path),
- cx,
- );
- });
+ MultiWorkspaceEvent::ProjectGroupKeyUpdated { old_key, new_key } => {
+ this.move_threads_for_key_change(old_key, new_key, cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
},
)
@@ -564,6 +519,7 @@ impl Sidebar {
this.subscribe_to_workspace(workspace, window, cx);
}
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
});
Self {
@@ -576,19 +532,19 @@ impl Sidebar {
selection: None,
active_entry: None,
hovered_thread_index: None,
- collapsed_groups: HashSet::new(),
- expanded_groups: HashMap::new(),
+
thread_last_accessed: HashMap::new(),
thread_last_message_sent_or_queued: HashMap::new(),
thread_switcher: None,
_thread_switcher_subscriptions: Vec::new(),
- pending_remote_thread_activation: None,
+ pending_thread_activation: None,
view: SidebarView::default(),
restoring_tasks: HashMap::new(),
recent_projects_popover_handle: PopoverMenuHandle::default(),
project_header_menu_ix: None,
_subscriptions: Vec::new(),
- _draft_observation: None,
+ _draft_observations: Vec::new(),
+ reconciling: false,
}
}
@@ -596,6 +552,55 @@ impl Sidebar {
cx.emit(workspace::SidebarEvent::SerializeNeeded);
}
+ fn is_group_collapsed(&self, key: &ProjectGroupKey, cx: &App) -> bool {
+ self.multi_workspace
+ .upgrade()
+ .and_then(|mw| {
+ mw.read(cx)
+ .group_state_by_key(key)
+ .map(|state| !state.expanded)
+ })
+ .unwrap_or(false)
+ }
+
+ fn group_extra_batches(&self, key: &ProjectGroupKey, cx: &App) -> usize {
+ self.multi_workspace
+ .upgrade()
+ .and_then(|mw| {
+ mw.read(cx)
+ .group_state_by_key(key)
+ .and_then(|state| state.visible_thread_count)
+ })
+ .unwrap_or(0)
+ }
+
+ fn set_group_expanded(&self, key: &ProjectGroupKey, expanded: bool, cx: &mut Context<Self>) {
+ if let Some(mw) = self.multi_workspace.upgrade() {
+ mw.update(cx, |mw, cx| {
+ if let Some(state) = mw.group_state_by_key_mut(key) {
+ state.expanded = expanded;
+ }
+ mw.serialize(cx);
+ });
+ }
+ }
+
+ fn set_group_visible_thread_count(
+ &self,
+ key: &ProjectGroupKey,
+ count: Option<usize>,
+ cx: &mut Context<Self>,
+ ) {
+ if let Some(mw) = self.multi_workspace.upgrade() {
+ mw.update(cx, |mw, cx| {
+ if let Some(state) = mw.group_state_by_key_mut(key) {
+ state.visible_thread_count = count;
+ }
+ mw.serialize(cx);
+ });
+ }
+ }
+
fn is_active_workspace(&self, workspace: &Entity<Workspace>, cx: &App) -> bool {
self.multi_workspace
.upgrade()
@@ -609,14 +614,23 @@ impl Sidebar {
cx: &mut Context<Self>,
) {
let project = workspace.read(cx).project().clone();
+
cx.subscribe_in(
&project,
window,
- |this, _project, event, _window, cx| match event {
+ |this, project, event, window, cx| match event {
ProjectEvent::WorktreeAdded(_)
| ProjectEvent::WorktreeRemoved(_)
| ProjectEvent::WorktreeOrderChanged => {
+ this.observe_draft_editors(cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
+ }
+ ProjectEvent::WorktreePathsChanged { old_worktree_paths } => {
+ this.move_thread_paths(project, old_worktree_paths, cx);
+ this.observe_draft_editors(cx);
+ this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
_ => {}
},
@@ -645,10 +659,12 @@ impl Sidebar {
cx.subscribe_in(
workspace,
window,
- |this, _workspace, event: &workspace::Event, _window, cx| {
+ |this, _workspace, event: &workspace::Event, window, cx| {
if let workspace::Event::PanelAdded(view) = event {
if let Ok(agent_panel) = view.clone().downcast::<AgentPanel>() {
- this.subscribe_to_agent_panel(&agent_panel, _window, cx);
+ this.subscribe_to_agent_panel(&agent_panel, window, cx);
+ this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
}
},
@@ -659,8 +675,102 @@ impl Sidebar {
if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
self.subscribe_to_agent_panel(&agent_panel, window, cx);
- self.observe_draft_editor(cx);
+ self.observe_draft_editors(cx);
+ }
+ }
+
+ fn move_threads_for_key_change(
+ &mut self,
+ old_key: &ProjectGroupKey,
+ new_key: &ProjectGroupKey,
+ cx: &mut Context<Self>,
+ ) {
+ let old_main_paths = old_key.path_list();
+ let new_main_paths = new_key.path_list();
+
+ let added_paths: Vec<PathBuf> = new_main_paths
+ .paths()
+ .iter()
+ .filter(|p| !old_main_paths.paths().contains(p))
+ .cloned()
+ .collect();
+
+ let removed_paths: Vec<PathBuf> = old_main_paths
+ .paths()
+ .iter()
+ .filter(|p| !new_main_paths.paths().contains(p))
+ .cloned()
+ .collect();
+
+ if added_paths.is_empty() && removed_paths.is_empty() {
+ return;
+ }
+
+ let remote_connection = old_key.host();
+ ThreadMetadataStore::global(cx).update(cx, |store, store_cx| {
+ store.change_worktree_paths_by_main(
+ old_main_paths,
+ remote_connection.as_ref(),
+ |paths| {
+ for path in &added_paths {
+ paths.add_path(path, path);
+ }
+ for path in &removed_paths {
+ paths.remove_main_path(path);
+ }
+ },
+ store_cx,
+ );
+ });
+ }
+
+ fn move_thread_paths(
+ &mut self,
+ project: &Entity<project::Project>,
+ old_paths: &WorktreePaths,
+ cx: &mut Context<Self>,
+ ) {
+ let new_paths = project.read(cx).worktree_paths(cx);
+ let old_folder_paths = old_paths.folder_path_list().clone();
+
+ let added_pairs: Vec<_> = new_paths
+ .ordered_pairs()
+ .filter(|(main, folder)| {
+ !old_paths
+ .ordered_pairs()
+ .any(|(old_main, old_folder)| old_main == *main && old_folder == *folder)
+ })
+ .map(|(m, f)| (m.clone(), f.clone()))
+ .collect();
+
+ let new_folder_paths = new_paths.folder_path_list();
+ let removed_folder_paths: Vec<PathBuf> = old_folder_paths
+ .paths()
+ .iter()
+ .filter(|p| !new_folder_paths.paths().contains(p))
+ .cloned()
+ .collect();
+
+ if added_pairs.is_empty() && removed_folder_paths.is_empty() {
+ return;
}
+
+ let remote_connection = project.read(cx).remote_connection_options(cx);
+ ThreadMetadataStore::global(cx).update(cx, |store, store_cx| {
+ store.change_worktree_paths(
+ &old_folder_paths,
+ remote_connection.as_ref(),
+ |paths| {
+ for (main_path, folder_path) in &added_pairs {
+ paths.add_path(main_path, folder_path);
+ }
+ for path in &removed_folder_paths {
+ paths.remove_folder_path(path);
+ }
+ },
+ store_cx,
+ );
+ });
}
fn subscribe_to_agent_panel(
@@ -672,23 +782,101 @@ impl Sidebar {
cx.subscribe_in(
agent_panel,
window,
- |this, _agent_panel, event: &AgentPanelEvent, _window, cx| match event {
+ |this, _agent_panel, event: &AgentPanelEvent, window, cx| match event {
AgentPanelEvent::ActiveViewChanged => {
- this.observe_draft_editor(cx);
+ let resolved_pending_activation =
+ this.sync_active_entry_from_panel(_agent_panel, cx);
+ if resolved_pending_activation {
+ let active_workspace = this.active_workspace(cx);
+ if let Some(active_workspace) = active_workspace {
+ this.clear_empty_group_drafts(&active_workspace, cx);
+ }
+ }
+ this.observe_draft_editors(cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
- AgentPanelEvent::ThreadFocused | AgentPanelEvent::BackgroundThreadChanged => {
+ AgentPanelEvent::ThreadFocused | AgentPanelEvent::RetainedThreadChanged => {
+ this.sync_active_entry_from_panel(_agent_panel, cx);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
- AgentPanelEvent::MessageSentOrQueued { session_id } => {
- this.record_thread_message_sent(session_id);
+ AgentPanelEvent::MessageSentOrQueued { thread_id } => {
+ this.record_thread_message_sent(thread_id);
this.update_entries(cx);
+ this.reconcile_groups(window, cx);
}
},
)
.detach();
}
+ fn sync_active_entry_from_active_workspace(&mut self, cx: &App) {
+ let panel = self
+ .active_workspace(cx)
+ .and_then(|ws| ws.read(cx).panel::<AgentPanel>(cx));
+ if let Some(panel) = panel {
+ self.sync_active_entry_from_panel(&panel, cx);
+ }
+ }
+
+ /// Syncs `active_entry` from the agent panel's current state.
+ /// Called from `ActiveViewChanged` — the panel has settled into its
+ /// new view, so we can safely read it without race conditions.
+ ///
+ /// Also resolves `pending_thread_activation` when the panel's
+ /// active thread matches the pending activation.
+ fn sync_active_entry_from_panel(&mut self, agent_panel: &Entity<AgentPanel>, cx: &App) -> bool {
+ let Some(active_workspace) = self.active_workspace(cx) else {
+ return false;
+ };
+
+ // Only sync when the event comes from the active workspace's panel.
+ let is_active_panel = active_workspace
+ .read(cx)
+ .panel::<AgentPanel>(cx)
+ .is_some_and(|p| p == *agent_panel);
+ if !is_active_panel {
+ return false;
+ }
+
+ let panel = agent_panel.read(cx);
+
+ if let Some(pending_thread_id) = self.pending_thread_activation {
+ let panel_thread_id = panel
+ .active_conversation_view()
+ .map(|cv| cv.read(cx).parent_id());
+
+ if panel_thread_id == Some(pending_thread_id) {
+ let session_id = panel
+ .active_agent_thread(cx)
+ .map(|thread| thread.read(cx).session_id().clone());
+ self.active_entry = Some(ActiveEntry {
+ thread_id: pending_thread_id,
+ session_id,
+ workspace: active_workspace,
+ });
+ self.pending_thread_activation = None;
+ return true;
+ }
+ // Pending activation not yet resolved — keep current active_entry.
+ return false;
+ }
+
+ if let Some(thread_id) = panel.active_thread_id(cx) {
+ let session_id = panel
+ .active_agent_thread(cx)
+ .map(|thread| thread.read(cx).session_id().clone());
+ self.active_entry = Some(ActiveEntry {
+ thread_id,
+ session_id,
+ workspace: active_workspace,
+ });
+ }
+
+ false
+ }
+
fn observe_docks(&mut self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
let docks: Vec<_> = workspace
.read(cx)
@@ -713,24 +901,37 @@ impl Sidebar {
}
}
- fn observe_draft_editor(&mut self, cx: &mut Context<Self>) {
- self._draft_observation = self
- .multi_workspace
- .upgrade()
- .and_then(|mw| {
- let ws = mw.read(cx).workspace();
- ws.read(cx).panel::<AgentPanel>(cx)
- })
- .and_then(|panel| {
- let cv = panel.read(cx).active_conversation_view()?;
- let tv = cv.read(cx).active_thread()?;
- Some(tv.read(cx).message_editor.clone())
- })
- .map(|editor| {
- cx.observe(&editor, |_this, _editor, cx| {
- cx.notify();
- })
- });
+ fn observe_draft_editors(&mut self, cx: &mut Context<Self>) {
+ let Some(multi_workspace) = self.multi_workspace.upgrade() else {
+ self._draft_observations.clear();
+ return;
+ };
+
+ // Collect conversation views up front to avoid holding a
+ // borrow on `cx` across `cx.observe` calls.
+ let conversation_views: Vec<_> = multi_workspace
+ .read(cx)
+ .workspaces()
+ .filter_map(|ws| ws.read(cx).panel::<AgentPanel>(cx))
+ .flat_map(|panel| panel.read(cx).conversation_views())
+ .collect();
+
+ let mut subscriptions = Vec::with_capacity(conversation_views.len());
+ for cv in conversation_views {
+ if let Some(thread_view) = cv.read(cx).active_thread() {
+ let editor = thread_view.read(cx).message_editor.clone();
+ subscriptions.push(cx.observe(&editor, |this, _editor, cx| {
+ this.update_entries(cx);
+ }));
+ } else {
+ subscriptions.push(cx.observe(&cv, |this, _cv, cx| {
+ this.observe_draft_editors(cx);
+ this.update_entries(cx);
+ }));
+ }
+ }
+
+ self._draft_observations = subscriptions;
}
fn clean_mention_links(input: &str) -> String {
@@ -858,41 +1059,6 @@ impl Sidebar {
let query = self.filter_editor.read(cx).text(cx);
- // Derive active_entry from the active workspace's agent panel.
- // A tracked draft (in `draft_threads`) is checked first via
- // `active_draft_id`. Then we check for a thread with a session_id.
- // If a thread is mid-load with no session_id yet, we fall back to
- // `pending_remote_thread_activation` or keep the previous value.
- if let Some(active_ws) = &active_workspace {
- if let Some(panel) = active_ws.read(cx).panel::<AgentPanel>(cx) {
- let panel = panel.read(cx);
- if let Some(draft_id) = panel.active_draft_id() {
- self.active_entry = Some(ActiveEntry::Draft {
- id: draft_id,
- workspace: active_ws.clone(),
- });
- } else if let Some(session_id) = panel
- .active_conversation_view()
- .and_then(|cv| cv.read(cx).parent_id(cx))
- {
- if self.pending_remote_thread_activation.as_ref() == Some(&session_id) {
- self.pending_remote_thread_activation = None;
- }
- self.active_entry = Some(ActiveEntry::Thread {
- session_id,
- workspace: active_ws.clone(),
- });
- } else if let Some(session_id) = self.pending_remote_thread_activation.clone() {
- self.active_entry = Some(ActiveEntry::Thread {
- session_id,
- workspace: active_ws.clone(),
- });
- }
- // else: conversation is mid-load or panel is
- // uninitialized — keep previous active_entry.
- }
- }
-
let previous = mem::take(&mut self.contents);
let old_statuses: HashMap<acp::SessionId, AgentThreadStatus> = previous
@@ -900,7 +1066,8 @@ impl Sidebar {
.iter()
.filter_map(|entry| match entry {
ListEntry::Thread(thread) if thread.is_live => {
- Some((thread.metadata.session_id.clone(), thread.status))
+ let sid = thread.metadata.session_id.clone()?;
+ Some((sid, thread.status))
}
_ => None,
})
@@ -909,7 +1076,9 @@ impl Sidebar {
let mut entries = Vec::new();
let mut notified_threads = previous.notified_threads;
let mut current_session_ids: HashSet<acp::SessionId> = HashSet::new();
+ let mut current_thread_ids: HashSet<agent_ui::ThreadId> = HashSet::new();
let mut project_header_indices: Vec<usize> = Vec::new();
+ let mut seen_thread_ids: HashSet<agent_ui::ThreadId> = HashSet::new();
let has_open_projects = workspaces
.iter()
@@ -927,11 +1096,11 @@ impl Sidebar {
(icon, icon_from_external_svg)
};
- let groups: Vec<_> = mw.project_groups(cx).collect();
+ let groups = mw.project_groups(cx);
let mut all_paths: Vec<PathBuf> = groups
.iter()
- .flat_map(|(key, _)| key.path_list().paths().iter().cloned())
+ .flat_map(|group| group.key.path_list().paths().iter().cloned())
.collect();
all_paths.sort();
all_paths.dedup();
@@ -942,14 +1111,16 @@ impl Sidebar {
let path_detail_map: HashMap<PathBuf, usize> =
all_paths.into_iter().zip(path_details).collect();
- for (group_key, group_workspaces) in &groups {
+ for group in &groups {
+ let group_key = &group.key;
+ let group_workspaces = &group.workspaces;
if group_key.path_list().paths().is_empty() {
continue;
}
let label = group_key.display_name(&path_detail_map);
- let is_collapsed = self.collapsed_groups.contains(&group_key);
+ let is_collapsed = self.is_group_collapsed(group_key, cx);
let should_load_threads = !is_collapsed || !query.is_empty();
let is_active = active_workspace
@@ -967,7 +1138,6 @@ impl Sidebar {
let mut waiting_thread_count: usize = 0;
if should_load_threads {
- let mut seen_session_ids: HashSet<acp::SessionId> = HashSet::new();
let thread_store = ThreadMetadataStore::global(cx);
// Build a lookup from workspace root paths to their workspace
@@ -997,6 +1167,7 @@ impl Sidebar {
|row: ThreadMetadata, workspace: ThreadEntryWorkspace| -> ThreadEntry {
let (icon, icon_from_external_svg) = resolve_agent_icon(&row.agent_id);
let worktrees = worktree_info_from_thread_paths(&row.worktree_paths);
+ let is_draft = row.is_draft();
ThreadEntry {
metadata: row,
icon,
@@ -1006,6 +1177,7 @@ impl Sidebar {
is_live: false,
is_background: false,
is_title_generating: false,
+ is_draft,
highlight_positions: Vec::new(),
worktrees,
diff_stats: DiffStats::default(),
@@ -1021,7 +1193,7 @@ impl Sidebar {
.entries_for_main_worktree_path(group_key.path_list())
.cloned()
{
- if !seen_session_ids.insert(row.session_id.clone()) {
+ if !seen_thread_ids.insert(row.thread_id) {
continue;
}
let workspace = resolve_workspace(&row);
@@ -1037,7 +1209,7 @@ impl Sidebar {
.entries_for_path(group_key.path_list())
.cloned()
{
- if !seen_session_ids.insert(row.session_id.clone()) {
+ if !seen_thread_ids.insert(row.thread_id) {
continue;
}
let workspace = resolve_workspace(&row);
@@ -1063,7 +1235,7 @@ impl Sidebar {
.entries_for_path(&worktree_path_list)
.cloned()
{
- if !seen_session_ids.insert(row.session_id.clone()) {
+ if !seen_thread_ids.insert(row.thread_id) {
continue;
}
threads.push(make_thread_entry(
@@ -1093,14 +1265,15 @@ impl Sidebar {
// Merge live info into threads and update notification state
// in a single pass.
for thread in &mut threads {
- if let Some(info) = live_info_by_session.get(&thread.metadata.session_id) {
- thread.apply_active_info(info);
+ if let Some(session_id) = &thread.metadata.session_id {
+ if let Some(info) = live_info_by_session.get(session_id) {
+ thread.apply_active_info(info);
+ }
}
let session_id = &thread.metadata.session_id;
-
let is_active_thread = self.active_entry.as_ref().is_some_and(|entry| {
- entry.is_active_thread(session_id)
+ entry.is_active_thread(&thread.metadata.thread_id)
&& active_workspace
.as_ref()
.is_some_and(|active| active == entry.workspace())
@@ -1108,26 +1281,37 @@ impl Sidebar {
if thread.status == AgentThreadStatus::Completed
&& !is_active_thread
- && old_statuses.get(session_id) == Some(&AgentThreadStatus::Running)
+ && session_id.as_ref().and_then(|sid| old_statuses.get(sid))
+ == Some(&AgentThreadStatus::Running)
{
- notified_threads.insert(session_id.clone());
+ notified_threads.insert(thread.metadata.thread_id);
}
if is_active_thread && !thread.is_background {
- notified_threads.remove(session_id);
+ notified_threads.remove(&thread.metadata.thread_id);
}
}
threads.sort_by(|a, b| {
- let a_time = self
- .thread_last_message_sent_or_queued
- .get(&a.metadata.session_id)
+ let a_time = a
+ .metadata
+ .session_id
+ .as_ref()
+ .and_then(|_sid| {
+ self.thread_last_message_sent_or_queued
+ .get(&a.metadata.thread_id)
+ })
.copied()
.or(a.metadata.created_at)
.or(Some(a.metadata.updated_at));
- let b_time = self
- .thread_last_message_sent_or_queued
- .get(&b.metadata.session_id)
+ let b_time = b
+ .metadata
+ .session_id
+ .as_ref()
+ .and_then(|_sid| {
+ self.thread_last_message_sent_or_queued
+ .get(&b.metadata.thread_id)
+ })
.copied()
.or(b.metadata.created_at)
.or(Some(b.metadata.updated_at));
@@ -1165,7 +1349,11 @@ impl Sidebar {
let mut matched_threads: Vec<ThreadEntry> = Vec::new();
for mut thread in threads {
- let title: &str = &thread.metadata.title;
+ let title: &str = thread
+ .metadata
+ .title
+ .as_ref()
+ .map_or(DEFAULT_THREAD_TITLE, |t| t.as_ref());
if let Some(positions) = fuzzy_match_positions(&query, title) {
thread.highlight_positions = positions;
}
@@ -1200,7 +1388,10 @@ impl Sidebar {
});
for thread in matched_threads {
- current_session_ids.insert(thread.metadata.session_id.clone());
+ if let Some(sid) = thread.metadata.session_id.clone() {
+ current_session_ids.insert(sid);
+ }
+ current_thread_ids.insert(thread.metadata.thread_id);
entries.push(thread.into());
}
} else {
@@ -1219,54 +1410,34 @@ impl Sidebar {
continue;
}
- // Emit DraftThread entries by reading draft IDs from
- // each workspace's AgentPanel in this group.
{
- let mut group_draft_ids: Vec<(DraftId, Entity<Workspace>)> = Vec::new();
- for ws in group_workspaces {
- if let Some(panel) = ws.read(cx).panel::<AgentPanel>(cx) {
- let ids = panel.read(cx).draft_ids();
-
- for draft_id in ids {
- group_draft_ids.push((draft_id, ws.clone()));
+ // Override titles with editor text for drafts and
+ // threads that still have the default placeholder
+ // title (panel considers them drafts even if they
+ // have a session_id).
+ for thread in &mut threads {
+ let needs_title_override =
+ thread.is_draft || thread.metadata.title.is_none();
+ if needs_title_override {
+ if let ThreadEntryWorkspace::Open(workspace) = &thread.workspace {
+ if let Some(text) =
+ self.read_draft_text(thread.metadata.thread_id, workspace, cx)
+ {
+ thread.metadata.title = Some(text);
+ }
}
}
}
-
- // For empty groups with no drafts, emit a
- // placeholder DraftThread.
- if !has_threads && group_draft_ids.is_empty() {
- entries.push(ListEntry::DraftThread {
- draft_id: None,
- key: group_key.clone(),
- workspace: group_workspaces.first().cloned(),
- worktrees: Vec::new(),
- });
- } else {
- for (draft_id, ws) in &group_draft_ids {
- let ws_worktree_paths = ThreadWorktreePaths::from_project(
- ws.read(cx).project().read(cx),
- cx,
- );
- let worktrees = worktree_info_from_thread_paths(&ws_worktree_paths);
- entries.push(ListEntry::DraftThread {
- draft_id: Some(*draft_id),
- key: group_key.clone(),
- workspace: Some(ws.clone()),
- worktrees,
- });
- }
- }
}
let total = threads.len();
- let extra_batches = self.expanded_groups.get(&group_key).copied().unwrap_or(0);
+ let extra_batches = self.group_extra_batches(&group_key, cx);
let threads_to_show =
DEFAULT_THREADS_SHOWN + (extra_batches * DEFAULT_THREADS_SHOWN);
let count = threads_to_show.min(total);
- let mut promoted_threads: HashSet<acp::SessionId> = HashSet::new();
+ let mut promoted_threads: HashSet<agent_ui::ThreadId> = HashSet::new();
// Build visible entries in a single pass. Threads within
// the cutoff are always shown. Threads beyond it are shown
@@ -1275,23 +1446,27 @@ impl Sidebar {
for (index, thread) in threads.into_iter().enumerate() {
let is_hidden = index >= count;
- let session_id = &thread.metadata.session_id;
if is_hidden {
+ let is_notified = notified_threads.contains(&thread.metadata.thread_id);
let is_promoted = thread.status == AgentThreadStatus::Running
|| thread.status == AgentThreadStatus::WaitingForConfirmation
- || notified_threads.contains(session_id)
+ || is_notified
|| self.active_entry.as_ref().is_some_and(|active| {
active.matches_entry(&ListEntry::Thread(thread.clone()))
});
if is_promoted {
- promoted_threads.insert(session_id.clone());
+ promoted_threads.insert(thread.metadata.thread_id);
}
- if !promoted_threads.contains(session_id) {
+ let is_in_promoted = promoted_threads.contains(&thread.metadata.thread_id);
+ if !is_in_promoted {
continue;
}
}
- current_session_ids.insert(session_id.clone());
+ if let Some(sid) = &thread.metadata.session_id {
+ current_session_ids.insert(sid.clone());
+ }
+ current_thread_ids.insert(thread.metadata.thread_id);
entries.push(thread.into());
}
@@ -1307,14 +1482,12 @@ impl Sidebar {
}
}
- // Prune stale notifications using the session IDs we collected during
- // the build pass (no extra scan needed).
- notified_threads.retain(|id| current_session_ids.contains(id));
+ notified_threads.retain(|id| current_thread_ids.contains(id));
self.thread_last_accessed
.retain(|id, _| current_session_ids.contains(id));
self.thread_last_message_sent_or_queued
- .retain(|id, _| current_session_ids.contains(id));
+ .retain(|id, _| current_thread_ids.contains(id));
self.contents = SidebarContents {
entries,
@@ -1324,6 +1497,79 @@ impl Sidebar {
};
}
+ /// Gives each workspace's sidebar delegate a chance to reconcile its
+ /// project group (e.g. create a draft when the group is empty).
+ ///
+ /// Called at the top of `render` so we have `Window` + `Context`
+ /// available for panel mutations.
+ fn reconcile_groups(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if self.reconciling
+ || self.pending_thread_activation.is_some()
+ || !self.restoring_tasks.is_empty()
+ {
+ return;
+ }
+ self.reconciling = true;
+
+ let Some(multi_workspace) = self.multi_workspace.upgrade() else {
+ self.reconciling = false;
+ return;
+ };
+
+ let empty_group_keys: Vec<ProjectGroupKey> = self
+ .contents
+ .entries
+ .iter()
+ .filter_map(|entry| match entry {
+ ListEntry::ProjectHeader {
+ key,
+ has_threads: false,
+ ..
+ } => Some(key.clone()),
+ _ => None,
+ })
+ .collect();
+
+ if empty_group_keys.is_empty() {
+ self.reconciling = false;
+ return;
+ }
+
+ let mut did_reconcile = false;
+
+ for key in &empty_group_keys {
+ let workspace = {
+ let mw = multi_workspace.read(cx);
+ let active = mw.workspace().clone();
+ if active.read(cx).project_group_key(cx) == *key {
+ Some(active)
+ } else {
+ mw.workspace_for_paths(key.path_list(), key.host().as_ref(), cx)
+ }
+ };
+
+ let Some(workspace) = workspace else {
+ continue;
+ };
+
+ let Some(delegate) = workspace.read(cx).sidebar_delegate() else {
+ continue;
+ };
+
+ let changed = workspace.update(cx, |workspace, cx| {
+ delegate.reconcile_group(workspace, key, window, cx)
+ });
+
+ did_reconcile |= changed;
+ }
+
+ if did_reconcile {
+ self.update_entries(cx);
+ }
+
+ self.reconciling = false;
+ }
+
/// Rebuilds the sidebar's visible entries from already-cached state.
fn update_entries(&mut self, cx: &mut Context<Self>) {
let Some(multi_workspace) = self.multi_workspace.upgrade() else {
@@ -1413,36 +1659,6 @@ impl Sidebar {
key,
is_fully_expanded,
} => self.render_view_more(ix, key, *is_fully_expanded, is_selected, cx),
- ListEntry::DraftThread {
- draft_id,
- key,
- workspace,
- worktrees,
- } => {
- let group_has_threads = self
- .contents
- .entries
- .iter()
- .any(|e| matches!(e, ListEntry::ProjectHeader { key: hk, has_threads: true, .. } if hk == key));
- // Count drafts in the AgentPanel for this group's workspaces.
- let sibling_draft_count = workspace
- .as_ref()
- .and_then(|ws| ws.read(cx).panel::<AgentPanel>(cx))
- .map(|p| p.read(cx).draft_ids().len())
- .unwrap_or(0);
- let can_dismiss = group_has_threads || sibling_draft_count > 1;
- self.render_draft_thread(
- ix,
- *draft_id,
- key,
- workspace.as_ref(),
- is_active,
- worktrees,
- is_selected,
- can_dismiss,
- cx,
- )
- }
};
if is_group_header_after_first {
@@ -1502,7 +1718,7 @@ impl Sidebar {
let disclosure_id = SharedString::from(format!("disclosure-{ix}"));
let group_name = SharedString::from(format!("{id_prefix}header-group-{ix}"));
- let is_collapsed = self.collapsed_groups.contains(key);
+ let is_collapsed = self.is_group_collapsed(key, cx);
let (disclosure_icon, disclosure_tooltip) = if is_collapsed {
(IconName::ChevronRight, "Expand Project")
} else {
@@ -1511,7 +1727,7 @@ impl Sidebar {
let key_for_toggle = key.clone();
let key_for_collapse = key.clone();
- let view_more_expanded = self.expanded_groups.contains_key(key);
+ let view_more_expanded = self.group_extra_batches(key, cx) > 0;
let label = if highlight_positions.is_empty() {
Label::new(label.clone())
@@ -1641,8 +1857,11 @@ impl Sidebar {
let key_for_collapse = key_for_collapse.clone();
move |this, _, _window, cx| {
this.selection = None;
- this.expanded_groups.remove(&key_for_collapse);
- this.serialize(cx);
+ this.set_group_visible_thread_count(
+ &key_for_collapse,
+ None,
+ cx,
+ );
this.update_entries(cx);
}
})),
@@ -1669,7 +1888,7 @@ impl Sidebar {
})
.on_click(cx.listener(
move |this, _, window, cx| {
- this.collapsed_groups.remove(&key);
+ this.set_group_expanded(&key, true, cx);
this.selection = None;
// If the active workspace belongs to this
// group, use it (preserves linked worktree
@@ -1801,7 +2020,7 @@ impl Sidebar {
multi_workspace
.update(cx, |multi_workspace, cx| {
multi_workspace.prompt_to_add_folders_to_project_group(
- &project_group_key,
+ project_group_key.clone(),
window,
cx,
);
@@ -1946,12 +2165,8 @@ impl Sidebar {
_window: &mut Window,
cx: &mut Context<Self>,
) {
- if self.collapsed_groups.contains(project_group_key) {
- self.collapsed_groups.remove(project_group_key);
- } else {
- self.collapsed_groups.insert(project_group_key.clone());
- }
- self.serialize(cx);
+ let is_collapsed = self.is_group_collapsed(project_group_key, cx);
+ self.set_group_expanded(project_group_key, is_collapsed, cx);
self.update_entries(cx);
}
@@ -2161,28 +2376,6 @@ impl Sidebar {
self.expand_thread_group(&key, cx);
}
}
- ListEntry::DraftThread {
- draft_id,
- key,
- workspace,
- ..
- } => {
- let draft_id = *draft_id;
- let key = key.clone();
- let workspace = workspace.clone();
- if let Some(draft_id) = draft_id {
- if let Some(workspace) = workspace {
- self.activate_draft(draft_id, &workspace, window, cx);
- }
- } else if let Some(workspace) = workspace {
- self.activate_workspace(&workspace, window, cx);
- workspace.update(cx, |ws, cx| {
- ws.focus_panel::<AgentPanel>(window, cx);
- });
- } else {
- self.open_workspace_for_group(&key, window, cx);
- }
- }
}
}
@@ -2226,22 +2419,95 @@ impl Sidebar {
window: &mut Window,
cx: &mut App,
) {
- workspace.update(cx, |workspace, cx| {
- workspace.reveal_panel::<AgentPanel>(window, cx);
- });
-
- if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
+ let load_thread = |agent_panel: Entity<AgentPanel>,
+ metadata: &ThreadMetadata,
+ focus: bool,
+ window: &mut Window,
+ cx: &mut App| {
+ let Some(session_id) = metadata.session_id.clone() else {
+ return;
+ };
agent_panel.update(cx, |panel, cx| {
panel.load_agent_thread(
Agent::from(metadata.agent_id.clone()),
- metadata.session_id.clone(),
+ session_id,
Some(metadata.folder_paths().clone()),
- Some(metadata.title.clone()),
+ metadata.title.clone(),
focus,
window,
cx,
);
});
+ };
+
+ let mut existing_panel = None;
+ workspace.update(cx, |workspace, cx| {
+ if let Some(panel) = workspace.panel::<AgentPanel>(cx) {
+ panel.update(cx, |panel, _cx| panel.begin_loading_thread());
+ existing_panel = Some(panel);
+ }
+ });
+
+ if let Some(agent_panel) = existing_panel {
+ load_thread(agent_panel, metadata, focus, window, cx);
+ workspace.update(cx, |workspace, cx| {
+ workspace.reveal_panel::<AgentPanel>(window, cx);
+ });
+ return;
+ }
+
+ let workspace = workspace.downgrade();
+ let metadata = metadata.clone();
+ let mut async_window_cx = window.to_async(cx);
+ cx.spawn(async move |_cx| {
+ let panel = AgentPanel::load(workspace.clone(), async_window_cx.clone()).await?;
+
+ workspace.update_in(&mut async_window_cx, |workspace, window, cx| {
+ let panel = workspace.panel::<AgentPanel>(cx).unwrap_or_else(|| {
+ workspace.add_panel(panel.clone(), window, cx);
+ panel.clone()
+ });
+ panel.update(cx, |panel, _cx| panel.begin_loading_thread());
+ load_thread(panel, &metadata, focus, window, cx);
+ workspace.reveal_panel::<AgentPanel>(window, cx);
+ })?;
+
+ anyhow::Ok(())
+ })
+ .detach_and_log_err(cx);
+ }
+
+ fn clear_empty_group_drafts(&mut self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
+ let Some(multi_workspace) = self.multi_workspace.upgrade() else {
+ return;
+ };
+
+ let group_key = workspace.read(cx).project_group_key(cx);
+ let group_workspaces: Vec<_> = multi_workspace
+ .read(cx)
+ .workspaces()
+ .filter(|candidate| candidate.read(cx).project_group_key(cx) == group_key)
+ .cloned()
+ .collect();
+
+ for group_workspace in group_workspaces {
+ group_workspace.update(cx, |workspace, cx| {
+ let Some(panel) = workspace.panel::<AgentPanel>(cx) else {
+ return;
+ };
+
+ panel.update(cx, |panel, cx| {
+ let empty_draft_ids: Vec<ThreadId> = panel
+ .draft_thread_ids(cx)
+ .into_iter()
+ .filter(|id| panel.editor_text(*id, cx).is_none())
+ .collect();
+
+ for id in empty_draft_ids {
+ panel.remove_thread(id, cx);
+ }
+ });
+ });
}
}
@@ -2260,12 +2526,17 @@ impl Sidebar {
// Set active_entry eagerly so the sidebar highlight updates
// immediately, rather than waiting for a deferred AgentPanel
// event which can race with ActiveWorkspaceChanged clearing it.
- self.active_entry = Some(ActiveEntry::Thread {
+ self.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
session_id: metadata.session_id.clone(),
workspace: workspace.clone(),
});
self.record_thread_access(&metadata.session_id);
+ if metadata.session_id.is_some() {
+ self.pending_thread_activation = Some(metadata.thread_id);
+ }
+
multi_workspace.update(cx, |multi_workspace, cx| {
multi_workspace.activate(workspace.clone(), window, cx);
if retain {
@@ -2273,7 +2544,24 @@ impl Sidebar {
}
});
- Self::load_agent_thread_in_workspace(workspace, metadata, true, window, cx);
+ // Drafts (and other retained threads without a session_id) are
+ // already in memory — activate them directly instead of loading.
+ let thread_id = metadata.thread_id;
+ if metadata.session_id.is_none() {
+ workspace.update(cx, |ws, cx| {
+ if let Some(panel) = ws.panel::<AgentPanel>(cx) {
+ panel.update(cx, |panel, cx| {
+ panel.activate_retained_thread(thread_id, true, window, cx);
+ });
+ }
+ ws.focus_panel::<AgentPanel>(window, cx);
+ });
+ self.pending_thread_activation = None;
+ self.observe_draft_editors(cx);
+ } else {
+ Self::load_agent_thread_in_workspace(workspace, metadata, true, window, cx);
+ self.clear_empty_group_drafts(workspace, cx);
+ }
self.update_entries(cx);
}
@@ -2286,6 +2574,7 @@ impl Sidebar {
cx: &mut Context<Self>,
) {
let target_session_id = metadata.session_id.clone();
+ let metadata_thread_id = metadata.thread_id;
let workspace_for_entry = workspace.clone();
let activated = target_window
@@ -2307,11 +2596,14 @@ impl Sidebar {
.and_then(|sidebar| sidebar.downcast::<Self>().ok())
{
target_sidebar.update(cx, |sidebar, cx| {
- sidebar.active_entry = Some(ActiveEntry::Thread {
+ sidebar.pending_thread_activation = Some(metadata_thread_id);
+ sidebar.active_entry = Some(ActiveEntry {
+ thread_id: metadata_thread_id,
session_id: target_session_id.clone(),
workspace: workspace_for_entry.clone(),
});
sidebar.record_thread_access(&target_session_id);
+ sidebar.clear_empty_group_drafts(&workspace_for_entry, cx);
sidebar.update_entries(cx);
});
}
@@ -2355,11 +2647,11 @@ impl Sidebar {
return;
};
- let pending_session_id = metadata.session_id.clone();
+ let pending_thread_id = metadata.thread_id;
// Mark the pending thread activation so rebuild_contents
- // preserves the Thread active_entry during loading (prevents
- // spurious draft flash).
- self.pending_remote_thread_activation = Some(pending_session_id.clone());
+ // preserves the Thread active_entry during loading and
+ // reconciliation cannot synthesize an empty fallback draft.
+ self.pending_thread_activation = Some(pending_thread_id);
let host = project_group_key.host();
let provisional_key = Some(project_group_key.clone());
@@ -2385,8 +2677,8 @@ impl Sidebar {
if result.is_err() {
this.update(cx, |this, _cx| {
- if this.pending_remote_thread_activation.as_ref() == Some(&pending_session_id) {
- this.pending_remote_thread_activation = None;
+ if this.pending_thread_activation == Some(pending_thread_id) {
+ this.pending_thread_activation = None;
}
})
.ok();
@@ -2427,15 +2719,14 @@ impl Sidebar {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let session_id = metadata.session_id.clone();
+ let thread_id = metadata.thread_id;
let weak_archive_view = match &self.view {
SidebarView::Archive(view) => Some(view.downgrade()),
_ => None,
};
if metadata.folder_paths().paths().is_empty() {
- ThreadMetadataStore::global(cx)
- .update(cx, |store, cx| store.unarchive(&session_id, cx));
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| store.unarchive(thread_id, cx));
let active_workspace = self
.multi_workspace
@@ -2462,19 +2753,18 @@ impl Sidebar {
let store = ThreadMetadataStore::global(cx);
let task = store
.read(cx)
- .get_archived_worktrees_for_thread(session_id.0.to_string(), cx);
+ .get_archived_worktrees_for_thread(thread_id, cx);
let path_list = metadata.folder_paths().clone();
- let task_session_id = session_id.clone();
let restore_task = cx.spawn_in(window, async move |this, cx| {
let result: anyhow::Result<()> = async {
let archived_worktrees = task.await?;
if archived_worktrees.is_empty() {
this.update_in(cx, |this, window, cx| {
- this.restoring_tasks.remove(&session_id);
+ this.restoring_tasks.remove(&thread_id);
ThreadMetadataStore::global(cx)
- .update(cx, |store, cx| store.unarchive(&session_id, cx));
+ .update(cx, |store, cx| store.unarchive(thread_id, cx));
if let Some(workspace) =
this.find_current_workspace_for_path_list(&path_list, cx)
@@ -2513,11 +2803,11 @@ impl Sidebar {
Err(error) => {
log::error!("Failed to restore worktree: {error:#}");
this.update_in(cx, |this, _window, cx| {
- this.restoring_tasks.remove(&session_id);
+ this.restoring_tasks.remove(&thread_id);
if let Some(weak_archive_view) = &weak_archive_view {
weak_archive_view
.update(cx, |view, cx| {
- view.clear_restoring(&session_id, cx);
+ view.clear_restoring(&thread_id, cx);
})
.ok();
}
@@ -2547,28 +2837,24 @@ impl Sidebar {
if !path_replacements.is_empty() {
cx.update(|_window, cx| {
store.update(cx, |store, cx| {
- store.update_restored_worktree_paths(
- &session_id,
- &path_replacements,
- cx,
- );
+ store.update_restored_worktree_paths(thread_id, &path_replacements, cx);
});
})?;
let updated_metadata =
- cx.update(|_window, cx| store.read(cx).entry(&session_id).cloned())?;
+ cx.update(|_window, cx| store.read(cx).entry(thread_id).cloned())?;
if let Some(updated_metadata) = updated_metadata {
let new_paths = updated_metadata.folder_paths().clone();
cx.update(|_window, cx| {
store.update(cx, |store, cx| {
- store.unarchive(&updated_metadata.session_id, cx);
+ store.unarchive(updated_metadata.thread_id, cx);
});
})?;
this.update_in(cx, |this, window, cx| {
- this.restoring_tasks.remove(&session_id);
+ this.restoring_tasks.remove(&thread_id);
let key = ProjectGroupKey::new(None, new_paths.clone());
this.open_workspace_and_activate_thread(
updated_metadata,
@@ -2589,7 +2875,7 @@ impl Sidebar {
log::error!("{error:#}");
}
});
- self.restoring_tasks.insert(task_session_id, restore_task);
+ self.restoring_tasks.insert(thread_id, restore_task);
}
fn expand_selected_entry(
@@ -2602,8 +2888,9 @@ impl Sidebar {
match self.contents.entries.get(ix) {
Some(ListEntry::ProjectHeader { key, .. }) => {
- if self.collapsed_groups.contains(key) {
- self.collapsed_groups.remove(key);
+ let key = key.clone();
+ if self.is_group_collapsed(&key, cx) {
+ self.set_group_expanded(&key, true, cx);
self.update_entries(cx);
} else if ix + 1 < self.contents.entries.len() {
self.selection = Some(ix + 1);
@@ -2625,19 +2912,19 @@ impl Sidebar {
match self.contents.entries.get(ix) {
Some(ListEntry::ProjectHeader { key, .. }) => {
- if !self.collapsed_groups.contains(key) {
- self.collapsed_groups.insert(key.clone());
+ let key = key.clone();
+ if !self.is_group_collapsed(&key, cx) {
+ self.set_group_expanded(&key, false, cx);
self.update_entries(cx);
}
}
- Some(
- ListEntry::Thread(_) | ListEntry::ViewMore { .. } | ListEntry::DraftThread { .. },
- ) => {
+ Some(ListEntry::Thread(_) | ListEntry::ViewMore { .. }) => {
for i in (0..ix).rev() {
if let Some(ListEntry::ProjectHeader { key, .. }) = self.contents.entries.get(i)
{
+ let key = key.clone();
self.selection = Some(i);
- self.collapsed_groups.insert(key.clone());
+ self.set_group_expanded(&key, false, cx);
self.update_entries(cx);
break;
}
@@ -2658,9 +2945,7 @@ impl Sidebar {
// Find the group header for the current selection.
let header_ix = match self.contents.entries.get(ix) {
Some(ListEntry::ProjectHeader { .. }) => Some(ix),
- Some(
- ListEntry::Thread(_) | ListEntry::ViewMore { .. } | ListEntry::DraftThread { .. },
- ) => (0..ix).rev().find(|&i| {
+ Some(ListEntry::Thread(_) | ListEntry::ViewMore { .. }) => (0..ix).rev().find(|&i| {
matches!(
self.contents.entries.get(i),
Some(ListEntry::ProjectHeader { .. })
@@ -2672,11 +2957,12 @@ impl Sidebar {
if let Some(header_ix) = header_ix {
if let Some(ListEntry::ProjectHeader { key, .. }) = self.contents.entries.get(header_ix)
{
- if self.collapsed_groups.contains(key) {
- self.collapsed_groups.remove(key);
+ let key = key.clone();
+ if self.is_group_collapsed(&key, cx) {
+ self.set_group_expanded(&key, true, cx);
} else {
self.selection = Some(header_ix);
- self.collapsed_groups.insert(key.clone());
+ self.set_group_expanded(&key, false, cx);
}
self.update_entries(cx);
}
@@ -2689,10 +2975,10 @@ impl Sidebar {
_window: &mut Window,
cx: &mut Context<Self>,
) {
- for entry in &self.contents.entries {
- if let ListEntry::ProjectHeader { key, .. } = entry {
- self.collapsed_groups.insert(key.clone());
- }
+ if let Some(mw) = self.multi_workspace.upgrade() {
+ mw.update(cx, |mw, _cx| {
+ mw.set_all_groups_expanded(false);
+ });
}
self.update_entries(cx);
}
@@ -2703,11 +2989,15 @@ impl Sidebar {
_window: &mut Window,
cx: &mut Context<Self>,
) {
- self.collapsed_groups.clear();
+ if let Some(mw) = self.multi_workspace.upgrade() {
+ mw.update(cx, |mw, _cx| {
+ mw.set_all_groups_expanded(true);
+ });
+ }
self.update_entries(cx);
}
- fn stop_thread(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
+ fn stop_thread(&mut self, thread_id: &agent_ui::ThreadId, cx: &mut Context<Self>) {
let Some(multi_workspace) = self.multi_workspace.upgrade() else {
return;
};
@@ -2716,7 +3006,7 @@ impl Sidebar {
for workspace in workspaces {
if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
let cancelled =
- agent_panel.update(cx, |panel, cx| panel.cancel_thread(session_id, cx));
+ agent_panel.update(cx, |panel, cx| panel.cancel_thread(thread_id, cx));
if cancelled {
return;
}
@@ -2730,11 +3020,26 @@ impl Sidebar {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let metadata = ThreadMetadataStore::global(cx)
- .read(cx)
- .entry(session_id)
- .cloned();
- let thread_folder_paths = metadata.as_ref().map(|m| m.folder_paths().clone());
+ let store = ThreadMetadataStore::global(cx);
+ let metadata = store.read(cx).entry_by_session(session_id).cloned();
+ let active_workspace = metadata.as_ref().and_then(|metadata| {
+ self.active_entry.as_ref().and_then(|entry| {
+ if entry.is_active_thread(&metadata.thread_id) {
+ Some(entry.workspace.clone())
+ } else {
+ None
+ }
+ })
+ });
+ let thread_id = metadata.as_ref().map(|metadata| metadata.thread_id);
+ let thread_folder_paths = metadata
+ .as_ref()
+ .map(|metadata| metadata.folder_paths().clone())
+ .or_else(|| {
+ active_workspace
+ .as_ref()
+ .map(|workspace| PathList::new(&workspace.read(cx).root_paths(cx)))
+ });
// Compute which linked worktree roots should be archived from disk if
// this thread is archived. This must happen before we remove any
@@ -2767,11 +3072,13 @@ impl Sidebar {
thread_worktree_archive::build_root_plan(path, &workspaces, cx)
})
.filter(|plan| {
- !thread_worktree_archive::path_is_referenced_by_other_unarchived_threads(
- session_id,
- &plan.root_path,
- cx,
- )
+ thread_id.map_or(true, |tid| {
+ !thread_worktree_archive::path_is_referenced_by_other_unarchived_threads(
+ tid,
+ &plan.root_path,
+ cx,
+ )
+ })
})
.collect::<Vec<_>>()
})
@@ -2782,15 +3089,21 @@ impl Sidebar {
// the one being archived. We capture both the neighbor's metadata
// (for activation) and its workspace paths (for the workspace
// removal fallback).
- let current_pos = self.contents.entries.iter().position(
- |entry| matches!(entry, ListEntry::Thread(t) if &t.metadata.session_id == session_id),
- );
+ let current_pos = self.contents.entries.iter().position(|entry| match entry {
+ ListEntry::Thread(thread) => thread_id.map_or_else(
+ || thread.metadata.session_id.as_ref() == Some(session_id),
+ |tid| thread.metadata.thread_id == tid,
+ ),
+ _ => false,
+ });
let neighbor = current_pos.and_then(|pos| {
self.contents.entries[pos + 1..]
.iter()
.chain(self.contents.entries[..pos].iter().rev())
.find_map(|entry| match entry {
- ListEntry::Thread(t) if t.metadata.session_id != *session_id => {
+ ListEntry::Thread(t)
+ if !t.is_draft && t.metadata.session_id.as_ref() != Some(session_id) =>
+ {
let workspace_paths = match &t.workspace {
ThreadEntryWorkspace::Open(ws) => {
PathList::new(&ws.read(cx).root_paths(cx))
@@ -2815,7 +3128,7 @@ impl Sidebar {
let remaining = ThreadMetadataStore::global(cx)
.read(cx)
.entries_for_path(folder_paths)
- .filter(|t| t.session_id != *session_id)
+ .filter(|t| t.session_id.as_ref() != Some(session_id))
.count();
if remaining > 0 {
@@ -2867,10 +3180,12 @@ impl Sidebar {
let removed = remove_task.await?;
if removed {
this.update_in(cx, |this, window, cx| {
- let in_flight =
- this.start_archive_worktree_task(&session_id, roots_to_archive, cx);
+ let in_flight = thread_id.and_then(|tid| {
+ this.start_archive_worktree_task(tid, roots_to_archive, cx)
+ });
this.archive_and_activate(
&session_id,
+ thread_id,
neighbor_metadata.as_ref(),
thread_folder_paths.as_ref(),
in_flight,
@@ -2884,9 +3199,11 @@ impl Sidebar {
.detach_and_log_err(cx);
} else {
let neighbor_metadata = neighbor.map(|(metadata, _)| metadata);
- let in_flight = self.start_archive_worktree_task(session_id, roots_to_archive, cx);
+ let in_flight = thread_id
+ .and_then(|tid| self.start_archive_worktree_task(tid, roots_to_archive, cx));
self.archive_and_activate(
session_id,
+ thread_id,
neighbor_metadata.as_ref(),
thread_folder_paths.as_ref(),
in_flight,
@@ -2914,21 +3231,28 @@ impl Sidebar {
/// initiated unarchive can cancel the task.
fn archive_and_activate(
&mut self,
- session_id: &acp::SessionId,
+ _session_id: &acp::SessionId,
+ thread_id: Option<agent_ui::ThreadId>,
neighbor: Option<&ThreadMetadata>,
thread_folder_paths: Option<&PathList>,
in_flight_archive: Option<(Task<()>, smol::channel::Sender<()>)>,
window: &mut Window,
cx: &mut Context<Self>,
) {
- ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.archive(session_id, in_flight_archive, cx);
- });
+ if let Some(thread_id) = thread_id {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.archive(thread_id, in_flight_archive, cx);
+ });
+ }
let is_active = self
.active_entry
.as_ref()
- .is_some_and(|e| e.is_active_thread(session_id));
+ .is_some_and(|entry| thread_id.is_some_and(|tid| entry.is_active_thread(&tid)));
+
+ if is_active {
+ self.active_entry = None;
+ }
if !is_active {
// The user is looking at a different thread/draft. Clear the
@@ -2944,15 +3268,17 @@ impl Sidebar {
let panel_shows_archived = panel
.read(cx)
.active_conversation_view()
- .and_then(|cv| cv.read(cx).parent_id(cx))
- .is_some_and(|id| id == *session_id);
+ .map(|cv| cv.read(cx).parent_id())
+ .is_some_and(|live_thread_id| {
+ thread_id.is_some_and(|id| id == live_thread_id)
+ });
if panel_shows_archived {
panel.update(cx, |panel, cx| {
// Replace the archived thread with a
// tracked draft so the panel isn't left
// in Uninitialized state.
- let id = panel.create_draft(window, cx);
- panel.activate_draft(id, false, window, cx);
+ let id = panel.create_thread(window, cx);
+ panel.activate_retained_thread(id, false, window, cx);
});
}
}
@@ -2971,6 +3297,11 @@ impl Sidebar {
mw.read(cx)
.workspace_for_paths(metadata.folder_paths(), None, cx)
}) {
+ self.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
+ session_id: metadata.session_id.clone(),
+ workspace: workspace.clone(),
+ });
self.activate_workspace(&workspace, window, cx);
Self::load_agent_thread_in_workspace(&workspace, metadata, true, window, cx);
return;
@@ -3003,7 +3334,7 @@ impl Sidebar {
fn start_archive_worktree_task(
&self,
- session_id: &acp::SessionId,
+ thread_id: ThreadId,
roots: Vec<thread_worktree_archive::RootPlan>,
cx: &mut Context<Self>,
) -> Option<(Task<()>, smol::channel::Sender<()>)> {
@@ -3012,13 +3343,12 @@ impl Sidebar {
}
let (cancel_tx, cancel_rx) = smol::channel::bounded::<()>(1);
- let session_id = session_id.clone();
let task = cx.spawn(async move |_this, cx| {
match Self::archive_worktree_roots(roots, cancel_rx, cx).await {
Ok(ArchiveWorktreeOutcome::Success) => {
cx.update(|cx| {
ThreadMetadataStore::global(cx).update(cx, |store, _cx| {
- store.cleanup_completed_archive(&session_id);
+ store.cleanup_completed_archive(thread_id);
});
});
}
@@ -3027,7 +3357,7 @@ impl Sidebar {
log::error!("Failed to archive worktree: {error:#}");
cx.update(|cx| {
ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.unarchive(&session_id, cx);
+ store.unarchive(thread_id, cx);
});
});
}
@@ -3113,6 +3443,13 @@ impl Sidebar {
return;
};
match self.contents.entries.get(ix) {
+ Some(ListEntry::Thread(thread)) if thread.is_draft => {
+ let draft_id = thread.metadata.thread_id;
+ if let ThreadEntryWorkspace::Open(workspace) = &thread.workspace {
+ let workspace = workspace.clone();
+ self.remove_draft(draft_id, &workspace, window, cx);
+ }
+ }
Some(ListEntry::Thread(thread)) => {
match thread.status {
AgentThreadStatus::Running | AgentThreadStatus::WaitingForConfirmation => {
@@ -3120,30 +3457,23 @@ impl Sidebar {
}
AgentThreadStatus::Completed | AgentThreadStatus::Error => {}
}
- let session_id = thread.metadata.session_id.clone();
- self.archive_thread(&session_id, window, cx);
- }
- Some(ListEntry::DraftThread {
- draft_id: Some(draft_id),
- workspace: Some(workspace),
- ..
- }) => {
- let draft_id = *draft_id;
- let workspace = workspace.clone();
- self.remove_draft(draft_id, &workspace, window, cx);
+ if let Some(session_id) = thread.metadata.session_id.clone() {
+ self.archive_thread(&session_id, window, cx);
+ }
}
_ => {}
}
}
- fn record_thread_access(&mut self, session_id: &acp::SessionId) {
- self.thread_last_accessed
- .insert(session_id.clone(), Utc::now());
+ fn record_thread_access(&mut self, session_id: &Option<acp::SessionId>) {
+ if let Some(sid) = session_id {
+ self.thread_last_accessed.insert(sid.clone(), Utc::now());
+ }
}
- fn record_thread_message_sent(&mut self, session_id: &acp::SessionId) {
+ fn record_thread_message_sent(&mut self, thread_id: &agent_ui::ThreadId) {
self.thread_last_message_sent_or_queued
- .insert(session_id.clone(), Utc::now());
+ .insert(*thread_id, Utc::now());
}
fn mru_threads_for_switcher(&self, cx: &App) -> Vec<ThreadSwitcherEntry> {
@@ -3160,6 +3490,7 @@ impl Sidebar {
None
}
ListEntry::Thread(thread) => {
+ let session_id = thread.metadata.session_id.clone()?;
let workspace = match &thread.workspace {
ThreadEntryWorkspace::Open(workspace) => Some(workspace.clone()),
ThreadEntryWorkspace::Closed { .. } => {
@@ -3174,20 +3505,18 @@ impl Sidebar {
})
}
}?;
- let notified = self
- .contents
- .is_thread_notified(&thread.metadata.session_id);
+ let notified = self.contents.is_thread_notified(&thread.metadata.thread_id);
let timestamp: SharedString = format_history_entry_timestamp(
self.thread_last_message_sent_or_queued
- .get(&thread.metadata.session_id)
+ .get(&thread.metadata.thread_id)
.copied()
.or(thread.metadata.created_at)
.unwrap_or(thread.metadata.updated_at),
)
.into();
Some(ThreadSwitcherEntry {
- session_id: thread.metadata.session_id.clone(),
- title: thread.metadata.title.clone(),
+ session_id,
+ title: thread.metadata.display_title(),
icon: thread.icon,
icon_from_external_svg: thread.icon_from_external_svg.clone(),
status: thread.status,
@@ -3223,8 +3552,12 @@ impl Sidebar {
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => {
- let a_sent = self.thread_last_message_sent_or_queued.get(&a.session_id);
- let b_sent = self.thread_last_message_sent_or_queued.get(&b.session_id);
+ let a_sent = self
+ .thread_last_message_sent_or_queued
+ .get(&a.metadata.thread_id);
+ let b_sent = self
+ .thread_last_message_sent_or_queued
+ .get(&b.metadata.thread_id);
match (a_sent, b_sent) {
(Some(a_time), Some(b_time)) => b_time.cmp(a_time),
@@ -3287,9 +3620,9 @@ impl Sidebar {
let weak_multi_workspace = self.multi_workspace.clone();
let original_metadata = match &self.active_entry {
- Some(ActiveEntry::Thread { session_id, .. }) => entries
+ Some(ActiveEntry { thread_id, .. }) => entries
.iter()
- .find(|e| &e.session_id == session_id)
+ .find(|e| *thread_id == e.metadata.thread_id)
.map(|e| e.metadata.clone()),
_ => None,
};
@@ -3314,7 +3647,8 @@ impl Sidebar {
mw.activate(workspace.clone(), window, cx);
});
}
- this.active_entry = Some(ActiveEntry::Thread {
+ this.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
session_id: metadata.session_id.clone(),
workspace: workspace.clone(),
});
@@ -3334,7 +3668,8 @@ impl Sidebar {
});
}
this.record_thread_access(&metadata.session_id);
- this.active_entry = Some(ActiveEntry::Thread {
+ this.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
session_id: metadata.session_id.clone(),
workspace: workspace.clone(),
});
@@ -3355,7 +3690,8 @@ impl Sidebar {
}
if let Some(metadata) = &original_metadata {
if let Some(original_ws) = &original_workspace {
- this.active_entry = Some(ActiveEntry::Thread {
+ this.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
session_id: metadata.session_id.clone(),
workspace: original_ws.clone(),
});
@@ -3408,7 +3744,8 @@ impl Sidebar {
mw.activate(workspace.clone(), window, cx);
});
}
- self.active_entry = Some(ActiveEntry::Thread {
+ self.active_entry = Some(ActiveEntry {
+ thread_id: metadata.thread_id,
session_id: metadata.session_id.clone(),
workspace: workspace.clone(),
});
@@ -3427,13 +3764,12 @@ impl Sidebar {
is_focused: bool,
cx: &mut Context<Self>,
) -> AnyElement {
- let has_notification = self
- .contents
- .is_thread_notified(&thread.metadata.session_id);
+ let has_notification = self.contents.is_thread_notified(&thread.metadata.thread_id);
- let title: SharedString = thread.metadata.title.clone();
+ let title: SharedString = thread.metadata.display_title();
let metadata = thread.metadata.clone();
let thread_workspace = thread.workspace.clone();
+ let is_draft = thread.is_draft;
let is_hovered = self.hovered_thread_index == Some(ix);
let is_selected = is_active;
@@ -3442,7 +3778,9 @@ impl Sidebar {
AgentThreadStatus::Running | AgentThreadStatus::WaitingForConfirmation
);
+ let thread_id_for_actions = thread.metadata.thread_id;
let session_id_for_delete = thread.metadata.session_id.clone();
+ let thread_workspace_for_dismiss = thread.workspace.clone();
let focus_handle = self.focus_handle.clone();
let id = SharedString::from(format!("thread-entry-{}", ix));
@@ -3454,7 +3792,7 @@ impl Sidebar {
let timestamp = format_history_entry_timestamp(
self.thread_last_message_sent_or_queued
- .get(&thread.metadata.session_id)
+ .get(&thread.metadata.thread_id)
.copied()
.or(thread.metadata.created_at)
.unwrap_or(thread.metadata.updated_at),
@@ -3511,14 +3849,42 @@ impl Sidebar {
.style(ButtonStyle::Tinted(TintColor::Error))
.tooltip(Tooltip::text("Stop Generation"))
.on_click({
- let session_id = session_id_for_delete.clone();
cx.listener(move |this, _, _window, cx| {
- this.stop_thread(&session_id, cx);
+ this.stop_thread(&thread_id_for_actions, cx);
})
}),
)
})
- .when(is_hovered && !is_running, |this| {
+ .when(is_hovered && !is_running && is_draft, |this| {
+ this.action_slot(
+ div()
+ .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
+ cx.stop_propagation();
+ })
+ .child(
+ IconButton::new("close-draft", IconName::Close)
+ .icon_size(IconSize::Small)
+ .icon_color(Color::Muted)
+ .tooltip(Tooltip::text("Remove Draft"))
+ .on_click({
+ let thread_workspace = thread_workspace_for_dismiss.clone();
+ cx.listener(move |this, _, window, cx| {
+ if let ThreadEntryWorkspace::Open(workspace) =
+ &thread_workspace
+ {
+ this.remove_draft(
+ thread_id_for_actions,
+ workspace,
+ window,
+ cx,
+ );
+ }
+ })
+ }),
+ ),
+ )
+ })
+ .when(is_hovered && !is_running && !is_draft, |this| {
this.action_slot(
IconButton::new("archive-thread", IconName::Archive)
.icon_size(IconSize::Small)
@@ -3537,7 +3903,9 @@ impl Sidebar {
.on_click({
let session_id = session_id_for_delete.clone();
cx.listener(move |this, _, window, cx| {
- this.archive_thread(&session_id, window, cx);
+ if let Some(ref session_id) = session_id {
+ this.archive_thread(session_id, window, cx);
+ }
})
}),
)
@@ -3594,7 +3962,7 @@ impl Sidebar {
let window_project_groups: Vec<ProjectGroupKey> = multi_workspace
.as_ref()
- .map(|mw| mw.read(cx).project_group_keys().cloned().collect())
+ .map(|mw| mw.read(cx).project_group_keys())
.unwrap_or_default();
let popover_handle = self.recent_projects_popover_handle.clone();
@@ -3706,63 +4074,41 @@ impl Sidebar {
let draft_id = workspace.update(cx, |workspace, cx| {
let panel = workspace.panel::<AgentPanel>(cx)?;
let draft_id = panel.update(cx, |panel, cx| {
- let id = panel.create_draft(window, cx);
- panel.activate_draft(id, true, window, cx);
- id
+ if let Some(id) = panel.draft_thread_ids(cx).first().copied() {
+ if panel.active_thread_id(cx) != Some(id) {
+ panel.activate_retained_thread(id, true, window, cx);
+ }
+ id
+ } else {
+ let id = panel.create_thread(window, cx);
+ panel.activate_retained_thread(id, true, window, cx);
+ id
+ }
});
workspace.focus_panel::<AgentPanel>(window, cx);
Some(draft_id)
});
if let Some(draft_id) = draft_id {
- self.active_entry = Some(ActiveEntry::Draft {
- id: draft_id,
+ self.active_entry = Some(ActiveEntry {
+ thread_id: draft_id,
+ session_id: None,
workspace: workspace.clone(),
});
}
}
- fn activate_draft(
- &mut self,
- draft_id: DraftId,
- workspace: &Entity<Workspace>,
- window: &mut Window,
- cx: &mut Context<Self>,
- ) {
- if let Some(multi_workspace) = self.multi_workspace.upgrade() {
- multi_workspace.update(cx, |mw, cx| {
- mw.activate(workspace.clone(), window, cx);
- });
- }
-
- workspace.update(cx, |ws, cx| {
- if let Some(panel) = ws.panel::<AgentPanel>(cx) {
- panel.update(cx, |panel, cx| {
- panel.activate_draft(draft_id, true, window, cx);
- });
- }
- ws.focus_panel::<AgentPanel>(window, cx);
- });
-
- self.active_entry = Some(ActiveEntry::Draft {
- id: draft_id,
- workspace: workspace.clone(),
- });
-
- self.observe_draft_editor(cx);
- }
-
fn remove_draft(
&mut self,
- draft_id: DraftId,
+ draft_id: ThreadId,
workspace: &Entity<Workspace>,
window: &mut Window,
cx: &mut Context<Self>,
) {
workspace.update(cx, |ws, cx| {
if let Some(panel) = ws.panel::<AgentPanel>(cx) {
- panel.update(cx, |panel, _cx| {
- panel.remove_draft(draft_id);
+ panel.update(cx, |panel, cx| {
+ panel.remove_thread(draft_id, cx);
});
}
});
@@ -3770,74 +4116,40 @@ impl Sidebar {
let was_active = self
.active_entry
.as_ref()
- .is_some_and(|e| e.is_active_draft(draft_id));
+ .is_some_and(|e| e.is_active_thread(&draft_id));
if was_active {
- let mut switched = false;
let group_key = workspace.read(cx).project_group_key(cx);
- // Try the next draft below in the sidebar (smaller ID
- // since the list is newest-first). Fall back to the one
- // above (larger ID) if the deleted draft was last.
- if let Some(panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
- let ids = panel.read(cx).draft_ids();
- let sibling = ids
- .iter()
- .find(|id| id.0 < draft_id.0)
- .or_else(|| ids.first());
- if let Some(&sibling_id) = sibling {
- self.activate_draft(sibling_id, workspace, window, cx);
- switched = true;
- }
- }
-
- // No sibling draft — try the first thread in the group.
- if !switched {
- let first_thread = self.contents.entries.iter().find_map(|entry| {
- if let ListEntry::Thread(thread) = entry {
+ // Find any remaining thread in the same group.
+ let next = self.contents.entries.iter().find_map(|entry| {
+ if let ListEntry::Thread(thread) = entry {
+ if thread.metadata.thread_id != draft_id {
if let ThreadEntryWorkspace::Open(ws) = &thread.workspace {
if ws.read(cx).project_group_key(cx) == group_key {
return Some((thread.metadata.clone(), ws.clone()));
}
}
}
- None
- });
- if let Some((metadata, ws)) = first_thread {
- self.activate_thread(metadata, &ws, false, window, cx);
- switched = true;
}
- }
-
- if !switched {
+ None
+ });
+ if let Some((metadata, ws)) = next {
+ self.activate_thread(metadata, &ws, false, window, cx);
+ } else {
self.active_entry = None;
}
}
self.update_entries(cx);
- }
-
- fn clear_draft(
- &mut self,
- draft_id: DraftId,
- workspace: &Entity<Workspace>,
- window: &mut Window,
- cx: &mut Context<Self>,
- ) {
- workspace.update(cx, |ws, cx| {
- if let Some(panel) = ws.panel::<AgentPanel>(cx) {
- panel.update(cx, |panel, cx| {
- panel.clear_draft_editor(draft_id, window, cx);
- });
- }
- });
- self.update_entries(cx);
+ self.reconcile_groups(window, cx);
}
/// Cleans, collapses whitespace, and truncates raw editor text
/// for display as a draft label in the sidebar.
fn truncate_draft_label(raw: &str) -> Option<SharedString> {
- let cleaned = Self::clean_mention_links(raw);
+ let first_line = raw.lines().next().unwrap_or("");
+ let cleaned = Self::clean_mention_links(first_line);
let mut text: String = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
if text.is_empty() {
return None;
@@ -3852,12 +4164,12 @@ impl Sidebar {
/// Reads a draft's prompt text from its ConversationView in the AgentPanel.
fn read_draft_text(
&self,
- draft_id: DraftId,
+ draft_id: ThreadId,
workspace: &Entity<Workspace>,
cx: &App,
) -> Option<SharedString> {
let panel = workspace.read(cx).panel::<AgentPanel>(cx)?;
- let raw = panel.read(cx).draft_editor_text(draft_id, cx)?;
+ let raw = panel.read(cx).editor_text(draft_id, cx)?;
Self::truncate_draft_label(&raw)
}
@@ -3911,7 +4223,7 @@ impl Sidebar {
let key = key.clone();
// Uncollapse the target group so that threads become visible.
- self.collapsed_groups.remove(&key);
+ self.set_group_expanded(&key, true, cx);
if let Some(workspace) = self.multi_workspace.upgrade().and_then(|mw| {
mw.read(cx)
@@ -4015,14 +4327,8 @@ impl Sidebar {
}
fn expand_thread_group(&mut self, project_group_key: &ProjectGroupKey, cx: &mut Context<Self>) {
- let current = self
- .expanded_groups
- .get(project_group_key)
- .copied()
- .unwrap_or(0);
- self.expanded_groups
- .insert(project_group_key.clone(), current + 1);
- self.serialize(cx);
+ let current = self.group_extra_batches(project_group_key, cx);
+ self.set_group_visible_thread_count(project_group_key, Some(current + 1), cx);
self.update_entries(cx);
}
@@ -4031,8 +4337,7 @@ impl Sidebar {
project_group_key: &ProjectGroupKey,
cx: &mut Context<Self>,
) {
- self.expanded_groups.remove(project_group_key);
- self.serialize(cx);
+ self.set_group_visible_thread_count(project_group_key, None, cx);
self.update_entries(cx);
}
@@ -4041,17 +4346,12 @@ impl Sidebar {
project_group_key: &ProjectGroupKey,
cx: &mut Context<Self>,
) {
- match self.expanded_groups.get(project_group_key).copied() {
- Some(batches) if batches > 1 => {
- self.expanded_groups
- .insert(project_group_key.clone(), batches - 1);
- }
- Some(_) => {
- self.expanded_groups.remove(project_group_key);
- }
- None => return,
+ let batches = self.group_extra_batches(project_group_key, cx);
+ match batches {
+ 0 => return,
+ 1 => self.set_group_visible_thread_count(project_group_key, None, cx),
+ _ => self.set_group_visible_thread_count(project_group_key, Some(batches - 1), cx),
}
- self.serialize(cx);
self.update_entries(cx);
}
@@ -4091,125 +4391,6 @@ impl Sidebar {
self.create_new_thread(&workspace, window, cx);
}
- fn render_draft_thread(
- &self,
- ix: usize,
- draft_id: Option<DraftId>,
- key: &ProjectGroupKey,
- workspace: Option<&Entity<Workspace>>,
- is_active: bool,
- worktrees: &[WorktreeInfo],
- is_selected: bool,
- can_dismiss: bool,
- cx: &mut Context<Self>,
- ) -> AnyElement {
- let label: SharedString = draft_id
- .and_then(|id| workspace.and_then(|ws| self.read_draft_text(id, ws, cx)))
- .unwrap_or_else(|| "New Agent Thread".into());
-
- let id = SharedString::from(format!("draft-thread-btn-{}", ix));
-
- let worktrees = worktrees
- .iter()
- .map(|worktree| ThreadItemWorktreeInfo {
- name: worktree.name.clone(),
- full_path: worktree.full_path.clone(),
- highlight_positions: worktree.highlight_positions.clone(),
- kind: worktree.kind,
- })
- .collect();
-
- let is_hovered = self.hovered_thread_index == Some(ix);
-
- let key = key.clone();
- let workspace_for_click = workspace.cloned();
- let workspace_for_remove = workspace.cloned();
- let workspace_for_clear = workspace.cloned();
-
- ThreadItem::new(id, label)
- .icon(IconName::Pencil)
- .icon_color(Color::Custom(cx.theme().colors().icon_muted.opacity(0.4)))
- .worktrees(worktrees)
- .selected(is_active)
- .focused(is_selected)
- .hovered(is_hovered)
- .on_hover(cx.listener(move |this, is_hovered: &bool, _window, cx| {
- if *is_hovered {
- this.hovered_thread_index = Some(ix);
- } else if this.hovered_thread_index == Some(ix) {
- this.hovered_thread_index = None;
- }
- cx.notify();
- }))
- .on_click(cx.listener(move |this, _, window, cx| {
- if let Some(draft_id) = draft_id {
- if let Some(workspace) = &workspace_for_click {
- this.activate_draft(draft_id, workspace, window, cx);
- }
- } else if let Some(workspace) = &workspace_for_click {
- // Placeholder with an open workspace — just
- // activate it. The panel remembers its last view.
- this.activate_workspace(workspace, window, cx);
- workspace.update(cx, |ws, cx| {
- ws.focus_panel::<AgentPanel>(window, cx);
- });
- } else {
- // No workspace at all — just open one. The
- // panel's load fallback will create a draft.
- this.open_workspace_for_group(&key, window, cx);
- }
- }))
- .when_some(draft_id.filter(|_| can_dismiss), |this, draft_id| {
- this.action_slot(
- div()
- .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
- cx.stop_propagation();
- })
- .child(
- IconButton::new(
- SharedString::from(format!("close-draft-{}", ix)),
- IconName::Close,
- )
- .icon_size(IconSize::Small)
- .icon_color(Color::Muted)
- .tooltip(Tooltip::text("Remove Draft"))
- .on_click(cx.listener(
- move |this, _, window, cx| {
- if let Some(workspace) = &workspace_for_remove {
- this.remove_draft(draft_id, workspace, window, cx);
- }
- },
- )),
- ),
- )
- })
- .when_some(draft_id.filter(|_| !can_dismiss), |this, draft_id| {
- this.action_slot(
- div()
- .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
- cx.stop_propagation();
- })
- .child(
- IconButton::new(
- SharedString::from(format!("clear-draft-{}", ix)),
- IconName::Close,
- )
- .icon_size(IconSize::Small)
- .icon_color(Color::Muted)
- .tooltip(Tooltip::text("Clear Draft"))
- .on_click(cx.listener(
- move |this, _, window, cx| {
- if let Some(workspace) = &workspace_for_clear {
- this.clear_draft(draft_id, workspace, window, cx);
- }
- },
- )),
- ),
- )
- })
- .into_any_element()
- }
-
fn render_no_results(&self, cx: &mut Context<Self>) -> impl IntoElement {
let has_query = self.has_filter_query(cx);
let message = if has_query {
@@ -4608,8 +4789,8 @@ impl Sidebar {
ThreadsArchiveViewEvent::Unarchive { thread } => {
this.activate_archived_thread(thread.clone(), window, cx);
}
- ThreadsArchiveViewEvent::CancelRestore { session_id } => {
- this.restoring_tasks.remove(session_id);
+ ThreadsArchiveViewEvent::CancelRestore { thread_id } => {
+ this.restoring_tasks.remove(thread_id);
}
},
);
@@ -4678,17 +4859,6 @@ impl WorkspaceSidebar for Sidebar {
fn serialized_state(&self, _cx: &App) -> Option<String> {
let serialized = SerializedSidebar {
width: Some(f32::from(self.width)),
- collapsed_groups: self
- .collapsed_groups
- .iter()
- .cloned()
- .map(SerializedProjectGroupKey::from)
- .collect(),
- expanded_groups: self
- .expanded_groups
- .iter()
- .map(|(key, count)| (SerializedProjectGroupKey::from(key.clone()), *count))
- .collect(),
active_view: match self.view {
SidebarView::ThreadList => SerializedSidebarView::ThreadList,
SidebarView::Archive(_) => SerializedSidebarView::Archive,
@@ -4707,16 +4877,6 @@ impl WorkspaceSidebar for Sidebar {
if let Some(width) = serialized.width {
self.width = px(width).clamp(MIN_WIDTH, MAX_WIDTH);
}
- self.collapsed_groups = serialized
- .collapsed_groups
- .into_iter()
- .map(ProjectGroupKey::from)
- .collect();
- self.expanded_groups = serialized
- .expanded_groups
- .into_iter()
- .map(|(s, count)| (ProjectGroupKey::from(s), count))
- .collect();
if serialized.active_view == SerializedSidebarView::Archive {
cx.defer_in(window, |this, window, cx| {
this.show_archive(window, cx);
@@ -4840,6 +5000,7 @@ fn all_thread_infos_for_workspace(
let has_pending_tool_call = conversation_view
.read(cx)
.root_thread_has_pending_tool_call(cx);
+ let conversation_thread_id = conversation_view.read(cx).parent_id();
let thread_view = conversation_view.read(cx).root_thread(cx)?;
let thread_view_ref = thread_view.read(cx);
let thread = thread_view_ref.thread.read(cx);
@@ -4852,7 +5013,7 @@ fn all_thread_infos_for_workspace(
let is_native = thread_view_ref.as_native_thread(cx).is_some();
let is_title_generating = is_native && thread.has_provisional_title();
let session_id = thread.session_id().clone();
- let is_background = agent_panel.is_background_thread(&session_id);
+ let is_background = agent_panel.is_retained_thread(&conversation_thread_id);
let status = if has_pending_tool_call {
AgentThreadStatus::WaitingForConfirmation
@@ -4905,7 +5066,7 @@ pub fn dump_workspace_info(
writeln!(output, "MultiWorkspace: {} workspace(s)", workspaces.len()).ok();
if let Some(mw) = &multi_workspace {
- let keys: Vec<_> = mw.read(cx).project_group_keys().cloned().collect();
+ let keys: Vec<_> = mw.read(cx).project_group_keys();
writeln!(output, "Project group keys ({}):", keys.len()).ok();
for key in keys {
writeln!(output, " - {key:?}").ok();
@@ -5084,7 +5245,7 @@ fn dump_single_workspace(workspace: &Workspace, output: &mut String, cx: &gpui::
writeln!(output, "Active thread: (none)").ok();
}
- let background_threads = panel.background_threads();
+ let background_threads = panel.retained_threads();
if !background_threads.is_empty() {
writeln!(
output,
@@ -5101,7 +5262,7 @@ fn dump_single_workspace(workspace: &Workspace, output: &mut String, cx: &gpui::
ThreadStatus::Generating => "generating",
};
let entry_count = thread.entries().len();
- write!(output, " - {title} (session: {session_id})").ok();
+ write!(output, " - {title} (thread: {session_id:?})").ok();
write!(output, " [{status}, {entry_count} entries").ok();
if conversation_view
.read(cx)
@@ -5111,7 +5272,7 @@ fn dump_single_workspace(workspace: &Workspace, output: &mut String, cx: &gpui::
}
writeln!(output, "]").ok();
} else {
- writeln!(output, " - (not connected) (session: {session_id})").ok();
+ writeln!(output, " - (not connected) (thread: {session_id:?})").ok();
}
}
}
diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rs
index ec9c709fab0..e5fef45f4cf 100644
--- a/crates/sidebar/src/sidebar_tests.rs
+++ b/crates/sidebar/src/sidebar_tests.rs
@@ -2,8 +2,9 @@ use super::*;
use acp_thread::{AcpThread, PermissionOptions, StubAgentConnection};
use agent::ThreadStore;
use agent_ui::{
+ ThreadId,
test_support::{active_session_id, open_thread_with_connection, send_message},
- thread_metadata_store::{ThreadMetadata, ThreadWorktreePaths},
+ thread_metadata_store::{ThreadMetadata, WorktreePaths},
};
use chrono::DateTime;
use fs::{FakeFs, Fs};
@@ -32,20 +33,49 @@ fn init_test(cx: &mut TestAppContext) {
#[track_caller]
fn assert_active_thread(sidebar: &Sidebar, session_id: &acp::SessionId, msg: &str) {
+ let active = sidebar.active_entry.as_ref();
+ let matches = active.is_some_and(|entry| {
+ // Match by session_id directly on active_entry.
+ entry.session_id.as_ref() == Some(session_id)
+ // Or match by finding the thread in sidebar entries.
+ || sidebar.contents.entries.iter().any(|list_entry| {
+ matches!(list_entry, ListEntry::Thread(t)
+ if t.metadata.session_id.as_ref() == Some(session_id)
+ && entry.matches_entry(list_entry))
+ })
+ });
assert!(
- sidebar
- .active_entry
- .as_ref()
- .is_some_and(|e| e.is_active_thread(session_id)),
- "{msg}: expected active_entry to be Thread({session_id:?}), got {:?}",
- sidebar.active_entry,
+ matches,
+ "{msg}: expected active_entry for session {session_id:?}, got {:?}",
+ active,
);
}
+#[track_caller]
+fn is_active_session(sidebar: &Sidebar, session_id: &acp::SessionId) -> bool {
+ let thread_id = sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(t) if t.metadata.session_id.as_ref() == Some(session_id) => {
+ Some(t.metadata.thread_id)
+ }
+ _ => None,
+ });
+ match thread_id {
+ Some(tid) => {
+ matches!(&sidebar.active_entry, Some(ActiveEntry { thread_id, .. }) if *thread_id == tid)
+ }
+ // Thread not in sidebar entries — can't confirm it's active.
+ None => false,
+ }
+}
+
#[track_caller]
fn assert_active_draft(sidebar: &Sidebar, workspace: &Entity<Workspace>, msg: &str) {
assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Draft { workspace: ws, .. }) if ws == workspace),
+ matches!(&sidebar.active_entry, Some(ActiveEntry { workspace: ws, .. }) if ws == workspace),
"{msg}: expected active_entry to be Draft for workspace {:?}, got {:?}",
workspace.entity_id(),
sidebar.active_entry,
@@ -57,7 +87,7 @@ fn has_thread_entry(sidebar: &Sidebar, session_id: &acp::SessionId) -> bool {
.contents
.entries
.iter()
- .any(|entry| matches!(entry, ListEntry::Thread(t) if &t.metadata.session_id == session_id))
+ .any(|entry| matches!(entry, ListEntry::Thread(t) if t.metadata.session_id.as_ref() == Some(session_id)))
}
#[track_caller]
@@ -98,16 +128,22 @@ fn assert_remote_project_integration_sidebar_state(
"expected the only sidebar project header to be `project`"
);
}
- ListEntry::Thread(thread) if &thread.metadata.session_id == main_thread_id => {
+ ListEntry::Thread(thread)
+ if thread.metadata.session_id.as_ref() == Some(main_thread_id) =>
+ {
saw_main_thread = true;
}
- ListEntry::Thread(thread) if &thread.metadata.session_id == remote_thread_id => {
+ ListEntry::Thread(thread)
+ if thread.metadata.session_id.as_ref() == Some(remote_thread_id) =>
+ {
saw_remote_thread = true;
}
+ ListEntry::Thread(thread) if thread.is_draft => {}
ListEntry::Thread(thread) => {
- let title = thread.metadata.title.as_ref();
+ let title = thread.metadata.display_title();
panic!(
- "unexpected sidebar thread while simulating remote project integration flicker: title=`{title}`"
+ "unexpected sidebar thread while simulating remote project integration flicker: title=`{}`",
+ title
);
}
ListEntry::ViewMore { .. } => {
@@ -115,7 +151,6 @@ fn assert_remote_project_integration_sidebar_state(
"unexpected `View More` entry while simulating remote project integration flicker"
);
}
- ListEntry::DraftThread { .. } => {}
}
}
@@ -175,7 +210,7 @@ async fn save_n_test_threads(
for i in 0..count {
save_thread_metadata(
acp::SessionId::new(Arc::from(format!("thread-{}", i))),
- format!("Thread {}", i + 1).into(),
+ Some(format!("Thread {}", i + 1).into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, i).unwrap(),
None,
project,
@@ -192,7 +227,7 @@ async fn save_test_thread_metadata(
) {
save_thread_metadata(
session_id.clone(),
- "Test".into(),
+ Some("Test".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
project,
@@ -208,7 +243,7 @@ async fn save_named_thread_metadata(
) {
save_thread_metadata(
acp::SessionId::new(Arc::from(session_id)),
- SharedString::from(title.to_string()),
+ Some(SharedString::from(title.to_string())),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
project,
@@ -219,16 +254,23 @@ async fn save_named_thread_metadata(
fn save_thread_metadata(
session_id: acp::SessionId,
- title: SharedString,
+ title: Option<SharedString>,
updated_at: DateTime<Utc>,
created_at: Option<DateTime<Utc>>,
project: &Entity<project::Project>,
cx: &mut TestAppContext,
) {
cx.update(|cx| {
- let worktree_paths = ThreadWorktreePaths::from_project(project.read(cx), cx);
+ let worktree_paths = project.read(cx).worktree_paths(cx);
+ let thread_id = ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .unwrap_or_else(ThreadId::new);
let metadata = ThreadMetadata {
- session_id,
+ thread_id,
+ session_id: Some(session_id),
agent_id: agent::ZED_AGENT_ID.clone(),
title,
updated_at,
@@ -247,19 +289,27 @@ fn save_thread_metadata_with_main_paths(
title: &str,
folder_paths: PathList,
main_worktree_paths: PathList,
+ updated_at: DateTime<Utc>,
cx: &mut TestAppContext,
) {
let session_id = acp::SessionId::new(Arc::from(session_id));
let title = SharedString::from(title.to_string());
- let updated_at = chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap();
+ let thread_id = cx.update(|cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .unwrap_or_else(ThreadId::new)
+ });
let metadata = ThreadMetadata {
- session_id,
+ thread_id,
+ session_id: Some(session_id),
agent_id: agent::ZED_AGENT_ID.clone(),
- title,
+ title: Some(title),
updated_at,
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_path_lists(main_worktree_paths, folder_paths)
- .unwrap(),
+ worktree_paths: WorktreePaths::from_path_lists(main_worktree_paths, folder_paths).unwrap(),
archived: false,
remote_connection: None,
};
@@ -328,7 +378,7 @@ fn visible_entries_as_strings(
sidebar: &Entity<Sidebar>,
cx: &mut gpui::VisualTestContext,
) -> Vec<String> {
- sidebar.read_with(cx, |sidebar, _cx| {
+ sidebar.read_with(cx, |sidebar, cx| {
sidebar
.contents
.entries
@@ -347,7 +397,7 @@ fn visible_entries_as_strings(
highlight_positions: _,
..
} => {
- let icon = if sidebar.collapsed_groups.contains(key) {
+ let icon = if sidebar.is_group_collapsed(key, cx) {
">"
} else {
"v"
@@ -355,24 +405,34 @@ fn visible_entries_as_strings(
format!("{} [{}]{}", icon, label, selected)
}
ListEntry::Thread(thread) => {
- let title = thread.metadata.title.as_ref();
- let live = if thread.is_live { " *" } else { "" };
- let status_str = match thread.status {
- AgentThreadStatus::Running => " (running)",
- AgentThreadStatus::Error => " (error)",
- AgentThreadStatus::WaitingForConfirmation => " (waiting)",
- _ => "",
- };
- let notified = if sidebar
- .contents
- .is_thread_notified(&thread.metadata.session_id)
- {
- " (!)"
- } else {
- ""
- };
+ let title = thread.metadata.display_title();
let worktree = format_linked_worktree_chips(&thread.worktrees);
- format!(" {title}{worktree}{live}{status_str}{notified}{selected}")
+
+ if thread.is_draft {
+ let is_active = sidebar
+ .active_entry
+ .as_ref()
+ .is_some_and(|e| e.matches_entry(entry));
+ let active_marker = if is_active { " *" } else { "" };
+ format!(" [~ Draft{worktree}]{active_marker}{selected}")
+ } else {
+ let live = if thread.is_live { " *" } else { "" };
+ let status_str = match thread.status {
+ AgentThreadStatus::Running => " (running)",
+ AgentThreadStatus::Error => " (error)",
+ AgentThreadStatus::WaitingForConfirmation => " (waiting)",
+ _ => "",
+ };
+ let notified = if sidebar
+ .contents
+ .is_thread_notified(&thread.metadata.thread_id)
+ {
+ " (!)"
+ } else {
+ ""
+ };
+ format!(" {title}{worktree}{live}{status_str}{notified}{selected}")
+ }
}
ListEntry::ViewMore {
is_fully_expanded, ..
@@ -383,15 +443,6 @@ fn visible_entries_as_strings(
format!(" + View More{}", selected)
}
}
- ListEntry::DraftThread { worktrees, .. } => {
- let worktree = format_linked_worktree_chips(worktrees);
- let is_active = sidebar
- .active_entry
- .as_ref()
- .is_some_and(|e| e.matches_entry(entry));
- let active_marker = if is_active { " *" } else { "" };
- format!(" [~ Draft{}]{}{}", worktree, active_marker, selected)
- }
}
})
.collect()
@@ -413,7 +464,6 @@ async fn test_serialization_round_trip(cx: &mut TestAppContext) {
sidebar.update_in(cx, |sidebar, window, cx| {
sidebar.set_width(Some(px(420.0)), cx);
sidebar.toggle_collapse(&project_group_key, window, cx);
- sidebar.expanded_groups.insert(project_group_key.clone(), 2);
});
cx.run_until_parked();
@@ -432,27 +482,11 @@ async fn test_serialization_round_trip(cx: &mut TestAppContext) {
cx.run_until_parked();
// Assert all serialized fields match.
- let (width1, collapsed1, expanded1) = sidebar.read_with(cx, |s, _| {
- (
- s.width,
- s.collapsed_groups.clone(),
- s.expanded_groups.clone(),
- )
- });
- let (width2, collapsed2, expanded2) = sidebar2.read_with(cx, |s, _| {
- (
- s.width,
- s.collapsed_groups.clone(),
- s.expanded_groups.clone(),
- )
- });
+ let width1 = sidebar.read_with(cx, |s, _| s.width);
+ let width2 = sidebar2.read_with(cx, |s, _| s.width);
assert_eq!(width1, width2);
- assert_eq!(collapsed1, collapsed2);
- assert_eq!(expanded1, expanded2);
assert_eq!(width1, px(420.0));
- assert!(collapsed1.contains(&project_group_key));
- assert_eq!(expanded1.get(&project_group_key), Some(&2));
}
#[gpui::test]
@@ -468,8 +502,6 @@ async fn test_restore_serialized_archive_view_does_not_panic(cx: &mut TestAppCon
let serialized = serde_json::to_string(&SerializedSidebar {
width: Some(400.0),
- collapsed_groups: Vec::new(),
- expanded_groups: Vec::new(),
active_view: SerializedSidebarView::Archive,
})
.expect("serialization should succeed");
@@ -551,13 +583,13 @@ async fn test_entities_released_on_window_close(cx: &mut TestAppContext) {
#[gpui::test]
async fn test_single_workspace_no_threads(cx: &mut TestAppContext) {
- let project = init_test_project("/my-project", cx).await;
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ let (_sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
+ visible_entries_as_strings(&_sidebar, cx),
vec!["v [my-project]", " [~ Draft]"]
);
}
@@ -571,7 +603,7 @@ async fn test_single_workspace_with_saved_threads(cx: &mut TestAppContext) {
save_thread_metadata(
acp::SessionId::new(Arc::from("thread-1")),
- "Fix crash in project panel".into(),
+ Some("Fix crash in project panel".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 3, 0, 0, 0).unwrap(),
None,
&project,
@@ -580,7 +612,7 @@ async fn test_single_workspace_with_saved_threads(cx: &mut TestAppContext) {
save_thread_metadata(
acp::SessionId::new(Arc::from("thread-2")),
- "Add inline diff view".into(),
+ Some("Add inline diff view".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&project,
@@ -612,7 +644,7 @@ async fn test_workspace_lifecycle(cx: &mut TestAppContext) {
// Single workspace with a thread
save_thread_metadata(
acp::SessionId::new(Arc::from("thread-a1")),
- "Thread A1".into(),
+ Some("Thread A1".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -710,14 +742,7 @@ async fn test_view_more_batched_expansion(cx: &mut TestAppContext) {
// Expand again by one batch
sidebar.update_in(cx, |s, _window, cx| {
- let current = s
- .expanded_groups
- .get(&project_group_key)
- .copied()
- .unwrap_or(0);
- s.expanded_groups
- .insert(project_group_key.clone(), current + 1);
- s.update_entries(cx);
+ s.expand_thread_group(&project_group_key, cx);
});
cx.run_until_parked();
@@ -728,14 +753,7 @@ async fn test_view_more_batched_expansion(cx: &mut TestAppContext) {
// Expand one more time - should show all 17 threads with Collapse button
sidebar.update_in(cx, |s, _window, cx| {
- let current = s
- .expanded_groups
- .get(&project_group_key)
- .copied()
- .unwrap_or(0);
- s.expanded_groups
- .insert(project_group_key.clone(), current + 1);
- s.update_entries(cx);
+ s.expand_thread_group(&project_group_key, cx);
});
cx.run_until_parked();
@@ -747,8 +765,7 @@ async fn test_view_more_batched_expansion(cx: &mut TestAppContext) {
// Click collapse - should go back to showing 5 threads
sidebar.update_in(cx, |s, _window, cx| {
- s.expanded_groups.remove(&project_group_key);
- s.update_entries(cx);
+ s.reset_thread_group_expansion(&project_group_key, cx);
});
cx.run_until_parked();
@@ -811,8 +828,152 @@ async fn test_collapse_and_expand_group(cx: &mut TestAppContext) {
);
}
+#[gpui::test]
+async fn test_collapse_state_survives_worktree_key_change(cx: &mut TestAppContext) {
+ // When a worktree is added to a project, the project group key changes.
+ // The sidebar's collapsed/expanded state is keyed by ProjectGroupKey, so
+ // UI state must survive the key change.
+ let (_fs, project) = init_multi_project_test(&["/project-a", "/project-b"], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ save_n_test_threads(2, &project, cx).await;
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["v [project-a]", " Thread 2", " Thread 1",]
+ );
+
+ // Collapse the group.
+ let old_key = project.read_with(cx, |project, cx| project.project_group_key(cx));
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.toggle_collapse(&old_key, window, cx);
+ });
+ cx.run_until_parked();
+
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["> [project-a]"]
+ );
+
+ // Add a second worktree — the key changes from [/project-a] to
+ // [/project-a, /project-b].
+ project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree("/project-b", true, cx)
+ })
+ .await
+ .expect("should add worktree");
+ cx.run_until_parked();
+
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ // The group should still be collapsed under the new key.
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["> [project-a, project-b]"]
+ );
+}
+
+#[gpui::test]
+async fn test_adding_folder_to_non_backed_group_migrates_threads(cx: &mut TestAppContext) {
+ use workspace::ProjectGroup;
+ // When a project group has no backing workspace (e.g. the workspace was
+ // closed but the group and its threads remain), adding a folder via
+ // `add_folders_to_project_group` should still migrate thread metadata
+ // to the new key and cause the sidebar to rerender.
+ let (_fs, project) =
+ init_multi_project_test(&["/active-project", "/orphan-a", "/orphan-b"], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ // Insert a standalone project group for [/orphan-a] with no backing
+ // workspace — simulating a group that persisted after its workspace
+ // was closed.
+ let group_key = ProjectGroupKey::new(None, PathList::new(&[PathBuf::from("/orphan-a")]));
+ multi_workspace.update(cx, |mw, _cx| {
+ mw.test_add_project_group(ProjectGroup {
+ key: group_key.clone(),
+ workspaces: Vec::new(),
+ expanded: true,
+ visible_thread_count: None,
+ });
+ });
+
+ // Verify the group has no backing workspaces.
+ multi_workspace.read_with(cx, |mw, cx| {
+ let group = mw
+ .project_groups(cx)
+ .into_iter()
+ .find(|g| g.key == group_key)
+ .expect("group should exist");
+ assert!(
+ group.workspaces.is_empty(),
+ "group should have no backing workspaces"
+ );
+ });
+
+ // Save threads directly into the metadata store under [/orphan-a].
+ save_thread_metadata_with_main_paths(
+ "t-1",
+ "Thread One",
+ PathList::new(&[PathBuf::from("/orphan-a")]),
+ PathList::new(&[PathBuf::from("/orphan-a")]),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ cx,
+ );
+ save_thread_metadata_with_main_paths(
+ "t-2",
+ "Thread Two",
+ PathList::new(&[PathBuf::from("/orphan-a")]),
+ PathList::new(&[PathBuf::from("/orphan-a")]),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 1).unwrap(),
+ cx,
+ );
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ // Verify threads show under the standalone group.
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ "v [active-project]",
+ "v [orphan-a]",
+ " Thread Two",
+ " Thread One",
+ ]
+ );
+
+ // Add /orphan-b to the non-backed group.
+ multi_workspace.update(cx, |mw, cx| {
+ mw.add_folders_to_project_group(&group_key, vec![PathBuf::from("/orphan-b")], cx);
+ });
+ cx.run_until_parked();
+
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ // Threads should now appear under the combined key.
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ "v [active-project]",
+ "v [orphan-a, orphan-b]",
+ " Thread Two",
+ " Thread One",
+ ]
+ );
+}
+
#[gpui::test]
async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
+ use workspace::ProjectGroup;
+
let project = init_test_project("/my-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
@@ -822,16 +983,23 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
let expanded_path = PathList::new(&[std::path::PathBuf::from("/expanded")]);
let collapsed_path = PathList::new(&[std::path::PathBuf::from("/collapsed")]);
+ // Set the collapsed group state through multi_workspace
+ multi_workspace.update(cx, |mw, _cx| {
+ mw.test_add_project_group(ProjectGroup {
+ key: ProjectGroupKey::new(None, collapsed_path.clone()),
+ workspaces: Vec::new(),
+ expanded: false,
+ visible_thread_count: None,
+ });
+ });
+
sidebar.update_in(cx, |s, _window, _cx| {
- s.collapsed_groups
- .insert(project::ProjectGroupKey::new(None, collapsed_path.clone()));
- s.contents
- .notified_threads
- .insert(acp::SessionId::new(Arc::from("t-5")));
+ let notified_thread_id = ThreadId::new();
+ s.contents.notified_threads.insert(notified_thread_id);
s.contents.entries = vec![
// Expanded project header
ListEntry::ProjectHeader {
- key: project::ProjectGroupKey::new(None, expanded_path.clone()),
+ key: ProjectGroupKey::new(None, expanded_path.clone()),
label: "expanded-project".into(),
highlight_positions: Vec::new(),
has_running_threads: false,
@@ -841,10 +1009,11 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
},
ListEntry::Thread(ThreadEntry {
metadata: ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("t-1")),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("t-1"))),
agent_id: AgentId::new("zed-agent"),
- worktree_paths: ThreadWorktreePaths::default(),
- title: "Completed thread".into(),
+ worktree_paths: WorktreePaths::default(),
+ title: Some("Completed thread".into()),
updated_at: Utc::now(),
created_at: Some(Utc::now()),
archived: false,
@@ -857,6 +1026,7 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
is_live: false,
is_background: false,
is_title_generating: false,
+ is_draft: false,
highlight_positions: Vec::new(),
worktrees: Vec::new(),
diff_stats: DiffStats::default(),
@@ -864,10 +1034,11 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
// Active thread with Running status
ListEntry::Thread(ThreadEntry {
metadata: ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("t-2")),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("t-2"))),
agent_id: AgentId::new("zed-agent"),
- worktree_paths: ThreadWorktreePaths::default(),
- title: "Running thread".into(),
+ worktree_paths: WorktreePaths::default(),
+ title: Some("Running thread".into()),
updated_at: Utc::now(),
created_at: Some(Utc::now()),
archived: false,
@@ -880,6 +1051,7 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
is_live: true,
is_background: false,
is_title_generating: false,
+ is_draft: false,
highlight_positions: Vec::new(),
worktrees: Vec::new(),
diff_stats: DiffStats::default(),
@@ -887,10 +1059,11 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
// Active thread with Error status
ListEntry::Thread(ThreadEntry {
metadata: ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("t-3")),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("t-3"))),
agent_id: AgentId::new("zed-agent"),
- worktree_paths: ThreadWorktreePaths::default(),
- title: "Error thread".into(),
+ worktree_paths: WorktreePaths::default(),
+ title: Some("Error thread".into()),
updated_at: Utc::now(),
created_at: Some(Utc::now()),
archived: false,
@@ -903,6 +1076,7 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
is_live: true,
is_background: false,
is_title_generating: false,
+ is_draft: false,
highlight_positions: Vec::new(),
worktrees: Vec::new(),
diff_stats: DiffStats::default(),
@@ -911,10 +1085,11 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
// remote_connection: None,
ListEntry::Thread(ThreadEntry {
metadata: ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("t-4")),
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("t-4"))),
agent_id: AgentId::new("zed-agent"),
- worktree_paths: ThreadWorktreePaths::default(),
- title: "Waiting thread".into(),
+ worktree_paths: WorktreePaths::default(),
+ title: Some("Waiting thread".into()),
updated_at: Utc::now(),
created_at: Some(Utc::now()),
archived: false,
@@ -927,6 +1102,7 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
is_live: false,
is_background: false,
is_title_generating: false,
+ is_draft: false,
highlight_positions: Vec::new(),
worktrees: Vec::new(),
diff_stats: DiffStats::default(),
@@ -935,10 +1111,11 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
// remote_connection: None,
ListEntry::Thread(ThreadEntry {
metadata: ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("t-5")),
+ thread_id: notified_thread_id,
+ session_id: Some(acp::SessionId::new(Arc::from("t-5"))),
agent_id: AgentId::new("zed-agent"),
- worktree_paths: ThreadWorktreePaths::default(),
- title: "Notified thread".into(),
+ worktree_paths: WorktreePaths::default(),
+ title: Some("Notified thread".into()),
updated_at: Utc::now(),
created_at: Some(Utc::now()),
archived: false,
@@ -951,18 +1128,19 @@ async fn test_visible_entries_as_strings(cx: &mut TestAppContext) {
is_live: true,
is_background: true,
is_title_generating: false,
+ is_draft: false,
highlight_positions: Vec::new(),
worktrees: Vec::new(),
diff_stats: DiffStats::default(),
}),
// View More entry
ListEntry::ViewMore {
- key: project::ProjectGroupKey::new(None, expanded_path.clone()),
+ key: ProjectGroupKey::new(None, expanded_path.clone()),
is_fully_expanded: false,
},
// Collapsed project header
ListEntry::ProjectHeader {
- key: project::ProjectGroupKey::new(None, collapsed_path.clone()),
+ key: ProjectGroupKey::new(None, collapsed_path.clone()),
label: "collapsed-project".into(),
highlight_positions: Vec::new(),
has_running_threads: false,
@@ -1313,10 +1491,10 @@ async fn test_keyboard_collapse_from_child_selects_parent(cx: &mut TestAppContex
#[gpui::test]
async fn test_keyboard_navigation_on_empty_list(cx: &mut TestAppContext) {
- let project = init_test_project("/empty-project", cx).await;
+ let project = init_test_project_with_agent_panel("/empty-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
// An empty project has the header and an auto-created draft.
assert_eq!(
@@ -1383,6 +1561,7 @@ async fn init_test_project_with_agent_panel(
) -> Entity<project::Project> {
agent_ui::test_support::init_test(cx);
cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
ThreadStore::init_global(cx);
ThreadMetadataStore::init_global(cx);
language_model::LanguageModelRegistry::test(cx);
@@ -1497,7 +1676,7 @@ async fn test_subagent_permission_request_marks_parent_sidebar_thread_waiting(
let subagent_thread = panel.read_with(cx, |panel, cx| {
panel
.active_conversation_view()
- .and_then(|conversation| conversation.read(cx).thread_view(&subagent_session_id))
+ .and_then(|conversation| conversation.read(cx).thread_view(&subagent_session_id, cx))
.map(|thread_view| thread_view.read(cx).thread.clone())
.expect("Expected subagent thread to be loaded into the conversation")
});
@@ -1509,7 +1688,9 @@ async fn test_subagent_permission_request_marks_parent_sidebar_thread_waiting(
.entries
.iter()
.find_map(|entry| match entry {
- ListEntry::Thread(thread) if thread.metadata.session_id == parent_session_id => {
+ ListEntry::Thread(thread)
+ if thread.metadata.session_id.as_ref() == Some(&parent_session_id) =>
+ {
Some(thread.status)
}
_ => None,
@@ -1601,7 +1782,7 @@ async fn test_search_narrows_visible_threads_to_matches(cx: &mut TestAppContext)
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project,
@@ -1652,7 +1833,7 @@ async fn test_search_matches_regardless_of_case(cx: &mut TestAppContext) {
save_thread_metadata(
acp::SessionId::new(Arc::from("thread-1")),
- "Fix Crash In Project Panel".into(),
+ Some("Fix Crash In Project Panel".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -1695,7 +1876,7 @@ async fn test_escape_clears_search_and_restores_full_list(cx: &mut TestAppContex
for (id, title, hour) in [("t-1", "Alpha thread", 2), ("t-2", "Beta thread", 1)] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project,
@@ -1755,7 +1936,7 @@ async fn test_search_only_shows_workspace_headers_with_matches(cx: &mut TestAppC
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project_a,
@@ -1779,7 +1960,7 @@ async fn test_search_only_shows_workspace_headers_with_matches(cx: &mut TestAppC
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project_b,
@@ -1843,7 +2024,7 @@ async fn test_search_matches_workspace_name(cx: &mut TestAppContext) {
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project_a,
@@ -1867,7 +2048,7 @@ async fn test_search_matches_workspace_name(cx: &mut TestAppContext) {
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project_b,
@@ -1960,7 +2141,7 @@ async fn test_search_finds_threads_hidden_behind_view_more(cx: &mut TestAppConte
};
save_thread_metadata(
acp::SessionId::new(Arc::from(format!("thread-{}", i))),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, i).unwrap(),
None,
&project,
@@ -2006,7 +2187,7 @@ async fn test_search_finds_threads_inside_collapsed_groups(cx: &mut TestAppConte
save_thread_metadata(
acp::SessionId::new(Arc::from("thread-1")),
- "Important thread".into(),
+ Some("Important thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -2057,7 +2238,7 @@ async fn test_search_then_keyboard_navigate_and_confirm(cx: &mut TestAppContext)
] {
save_thread_metadata(
acp::SessionId::new(Arc::from(id)),
- title.into(),
+ Some(title.into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
None,
&project,
@@ -2127,7 +2308,7 @@ async fn test_confirm_on_historical_thread_activates_workspace(cx: &mut TestAppC
save_thread_metadata(
acp::SessionId::new(Arc::from("hist-1")),
- "Historical Thread".into(),
+ Some("Historical Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -2173,6 +2354,148 @@ async fn test_confirm_on_historical_thread_activates_workspace(cx: &mut TestAppC
);
}
+#[gpui::test]
+async fn test_confirm_on_historical_thread_in_new_project_group_opens_real_thread(
+ cx: &mut TestAppContext,
+) {
+ use workspace::ProjectGroup;
+
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ let project_b_key = project_b.read_with(cx, |project, cx| project.project_group_key(cx));
+ multi_workspace.update(cx, |mw, _cx| {
+ mw.test_add_project_group(ProjectGroup {
+ key: project_b_key.clone(),
+ workspaces: Vec::new(),
+ expanded: true,
+ visible_thread_count: None,
+ });
+ });
+
+ let session_id = acp::SessionId::new(Arc::from("historical-new-project-group"));
+ save_thread_metadata(
+ session_id.clone(),
+ Some("Historical Thread in New Group".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap(),
+ None,
+ &project_b,
+ cx,
+ );
+ cx.run_until_parked();
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ cx.run_until_parked();
+
+ let entries_before = visible_entries_as_strings(&sidebar, cx);
+ assert_eq!(
+ entries_before,
+ vec![
+ "v [project-a]",
+ "v [project-b]",
+ " Historical Thread in New Group",
+ ],
+ "expected the closed project group to show the historical thread before first open"
+ );
+
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 1,
+ "should start without an open workspace for the new project group"
+ );
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.selection = Some(2);
+ sidebar.confirm(&Confirm, window, cx);
+ });
+ cx.run_until_parked();
+ cx.run_until_parked();
+ cx.run_until_parked();
+
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 2,
+ "confirming the historical thread should open a workspace for the new project group"
+ );
+
+ let workspace_b = multi_workspace.read_with(cx, |mw, cx| {
+ mw.workspaces()
+ .find(|workspace| {
+ PathList::new(&workspace.read(cx).root_paths(cx))
+ == project_b_key.path_list().clone()
+ })
+ .cloned()
+ .expect("expected workspace for project-b after opening the historical thread")
+ });
+
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_b,
+ "opening the historical thread should activate the new project's workspace"
+ );
+
+ let panel = workspace_b.read_with(cx, |workspace, cx| {
+ workspace
+ .panel::<AgentPanel>(cx)
+ .expect("expected first-open activation to bootstrap the agent panel")
+ });
+
+ let expected_thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("metadata should still map session id to thread id")
+ });
+
+ assert_eq!(
+ panel.read_with(cx, |panel, cx| panel.active_thread_id(cx)),
+ Some(expected_thread_id),
+ "expected the agent panel to activate the real historical thread rather than a draft"
+ );
+
+ let entries_after = visible_entries_as_strings(&sidebar, cx);
+ let matching_rows: Vec<_> = entries_after
+ .iter()
+ .filter(|entry| entry.contains("Historical Thread in New Group") || entry.contains("Draft"))
+ .cloned()
+ .collect();
+ assert_eq!(
+ matching_rows.len(),
+ 1,
+ "expected only one matching row after first open into a new project group, got entries: {entries_after:?}"
+ );
+ assert!(
+ matching_rows[0].contains("Historical Thread in New Group"),
+ "expected the surviving row to be the real historical thread, got entries: {entries_after:?}"
+ );
+ assert!(
+ !matching_rows[0].contains("Draft"),
+ "expected no draft row after first open into a new project group, got entries: {entries_after:?}"
+ );
+}
+
#[gpui::test]
async fn test_click_clears_selection_and_focus_in_restores_it(cx: &mut TestAppContext) {
let project = init_test_project("/my-project", cx).await;
@@ -2182,7 +2505,7 @@ async fn test_click_clears_selection_and_focus_in_restores_it(cx: &mut TestAppCo
save_thread_metadata(
acp::SessionId::new(Arc::from("t-1")),
- "Thread A".into(),
+ Some("Thread A".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&project,
@@ -2191,7 +2514,7 @@ async fn test_click_clears_selection_and_focus_in_restores_it(cx: &mut TestAppCo
save_thread_metadata(
acp::SessionId::new(Arc::from("t-2")),
- "Thread B".into(),
+ Some("Thread B".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -2228,7 +2551,7 @@ async fn test_click_clears_selection_and_focus_in_restores_it(cx: &mut TestAppCo
sidebar.update_in(cx, |sidebar, window, cx| {
sidebar.selection = None;
let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
- let project_group_key = project::ProjectGroupKey::new(None, path_list);
+ let project_group_key = ProjectGroupKey::new(None, path_list);
sidebar.toggle_collapse(&project_group_key, window, cx);
});
assert_eq!(sidebar.read_with(cx, |sidebar, _| sidebar.selection), None);
@@ -2336,23 +2659,15 @@ async fn test_focused_thread_tracks_user_intent(cx: &mut TestAppContext) {
);
});
+ let thread_metadata_a = cx.update(|_window, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry_by_session(&session_id_a)
+ .cloned()
+ .expect("session_id_a should exist in metadata store")
+ });
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_thread(
- ThreadMetadata {
- session_id: session_id_a.clone(),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Test".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::default(),
- archived: false,
- remote_connection: None,
- },
- &workspace_a,
- false,
- window,
- cx,
- );
+ sidebar.activate_thread(thread_metadata_a, &workspace_a, false, window, cx);
});
cx.run_until_parked();
@@ -2392,23 +2707,15 @@ async fn test_focused_thread_tracks_user_intent(cx: &mut TestAppContext) {
// Workspace A is currently active. Click a thread in workspace B,
// which also triggers a workspace switch.
+ let thread_metadata_b = cx.update(|_window, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry_by_session(&session_id_b)
+ .cloned()
+ .expect("session_id_b should exist in metadata store")
+ });
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_thread(
- ThreadMetadata {
- session_id: session_id_b.clone(),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Thread B".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::default(),
- archived: false,
- remote_connection: None,
- },
- &workspace_b,
- false,
- window,
- cx,
- );
+ sidebar.activate_thread(thread_metadata_b, &workspace_b, false, window, cx);
});
cx.run_until_parked();
@@ -2556,7 +2863,7 @@ async fn test_new_thread_button_works_after_adding_folder(cx: &mut TestAppContex
// because the panel has a thread with messages.
sidebar.read_with(cx, |sidebar, _cx| {
assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Thread { .. })),
+ matches!(&sidebar.active_entry, Some(ActiveEntry { .. })),
"Panel has a thread with messages, so active_entry should be Thread, got {:?}",
sidebar.active_entry,
);
@@ -2578,20 +2885,21 @@ async fn test_new_thread_button_works_after_adding_folder(cx: &mut TestAppContex
// thread's metadata was re-saved with the new paths by the agent panel's
// project subscription. The old [project-a] key is replaced by the new
// key since no other workspace claims it.
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- "v [project-a, project-b]", //
- " Hello *",
- ]
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ // After adding a worktree, the thread migrates to the new group key.
+ // A reconciliation draft may appear during the transition.
+ assert!(
+ entries.contains(&" Hello *".to_string()),
+ "thread should still be present after adding folder: {entries:?}"
);
+ assert_eq!(entries[0], "v [project-a, project-b]");
// The "New Thread" button must still be clickable (not stuck in
// "active/draft" state). Verify that `active_thread_is_draft` is
// false — the panel still has the old thread with messages.
sidebar.read_with(cx, |sidebar, _cx| {
assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Thread { .. })),
+ matches!(&sidebar.active_entry, Some(ActiveEntry { .. })),
"After adding a folder the panel still has a thread with messages, \
so active_entry should be Thread, got {:?}",
sidebar.active_entry,
@@ -2616,953 +2924,1110 @@ async fn test_new_thread_button_works_after_adding_folder(cx: &mut TestAppContex
);
});
}
-
#[gpui::test]
-async fn test_worktree_add_and_remove_migrates_threads(cx: &mut TestAppContext) {
- // When a worktree is added to a project, the project group key changes
- // and all historical threads should be migrated to the new key. Removing
- // the worktree should migrate them back.
- let (_fs, project) = init_multi_project_test(&["/project-a", "/project-b"], cx).await;
+async fn test_group_level_folder_add_syncs_siblings_but_individual_add_splits(
+ cx: &mut TestAppContext,
+) {
+ // Group-level operations (via the "..." menu) should keep all workspaces
+ // in the group in sync. Individual worktree additions should let a
+ // workspace diverge from its group.
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-c", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), [Path::new("/project-a")], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let _sidebar = setup_sidebar(&multi_workspace, cx);
- // Save two threads against the initial project group [/project-a].
- save_n_test_threads(2, &project, cx).await;
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ // Add a second workspace in the same group by adding it with the same
+ // project so they share a project group key.
+ let project_a2 = project::Project::test(fs.clone(), [Path::new("/project-a")], cx).await;
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_a2.clone(), window, cx);
+ });
cx.run_until_parked();
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project-a]",
- " Thread 2",
- " Thread 1",
- ]
- );
+ // Both workspaces should be in the same group with key [/project-a].
+ multi_workspace.read_with(cx, |mw, _cx| {
+ assert_eq!(mw.workspaces().count(), 2);
+ assert_eq!(mw.project_group_keys().len(), 1);
+ });
- // Verify the metadata store has threads under the old key.
- let old_key_paths = PathList::new(&[PathBuf::from("/project-a")]);
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- assert_eq!(
- store.entries_for_main_worktree_path(&old_key_paths).count(),
- 2,
- "should have 2 threads under old key before add"
- );
+ // --- Group-level add: add /project-b via the group API ---
+ let group_key = multi_workspace.read_with(cx, |mw, _cx| mw.project_group_keys()[0].clone());
+ multi_workspace.update(cx, |mw, cx| {
+ mw.add_folders_to_project_group(&group_key, vec![PathBuf::from("/project-b")], cx);
});
+ cx.run_until_parked();
- // Add a second worktree to the same project.
- project
+ // Both workspaces should now have /project-b as a worktree.
+ multi_workspace.read_with(cx, |mw, cx| {
+ for workspace in mw.workspaces() {
+ let paths = workspace.read(cx).root_paths(cx);
+ assert!(
+ paths.iter().any(|p| p.ends_with("project-b")),
+ "group-level add should propagate /project-b to all siblings, got {:?}",
+ paths,
+ );
+ }
+ });
+
+ // --- Individual add: add /project-c directly to one workspace ---
+ let first_workspace =
+ multi_workspace.read_with(cx, |mw, _cx| mw.workspaces().next().unwrap().clone());
+ let first_project = first_workspace.read_with(cx, |ws, _cx| ws.project().clone());
+ first_project
.update(cx, |project, cx| {
- project.find_or_create_worktree("/project-b", true, cx)
+ project.find_or_create_worktree("/project-c", true, cx)
})
.await
.expect("should add worktree");
cx.run_until_parked();
- // The project group key should now be [/project-a, /project-b].
- let new_key_paths = PathList::new(&[PathBuf::from("/project-a"), PathBuf::from("/project-b")]);
-
- // Verify multi-workspace state: exactly one project group key, the new one.
- multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<_> = mw.project_group_keys().cloned().collect();
- assert_eq!(
- keys.len(),
- 1,
- "should have exactly 1 project group key after add"
+ // The first workspace should now have /project-c but the second should not.
+ let second_workspace =
+ multi_workspace.read_with(cx, |mw, _cx| mw.workspaces().nth(1).unwrap().clone());
+ first_workspace.read_with(cx, |ws, cx| {
+ let paths = ws.root_paths(cx);
+ assert!(
+ paths.iter().any(|p| p.ends_with("project-c")),
+ "individual add should give /project-c to this workspace, got {:?}",
+ paths,
);
- assert_eq!(
- keys[0].path_list(),
- &new_key_paths,
- "the key should be the new combined path list"
+ });
+ second_workspace.read_with(cx, |ws, cx| {
+ let paths = ws.root_paths(cx);
+ assert!(
+ !paths.iter().any(|p| p.ends_with("project-c")),
+ "individual add should NOT propagate /project-c to sibling, got {:?}",
+ paths,
);
});
+}
- // Verify threads were migrated to the new key.
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- assert_eq!(
- store.entries_for_main_worktree_path(&old_key_paths).count(),
- 0,
- "should have 0 threads under old key after migration"
- );
- assert_eq!(
- store.entries_for_main_worktree_path(&new_key_paths).count(),
- 2,
- "should have 2 threads under new key after migration"
- );
+#[gpui::test]
+async fn test_draft_title_updates_from_editor_text(cx: &mut TestAppContext) {
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ // The reconciliation-created draft should show the default title.
+ let draft_title = sidebar.read_with(cx, |sidebar, _cx| {
+ sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => {
+ Some(thread.metadata.display_title())
+ }
+ _ => None,
+ })
+ .expect("should have a draft entry")
});
+ assert_eq!(
+ draft_title.as_ref(),
+ "New Agent Thread",
+ "draft should start with default title"
+ );
- // Sidebar should show threads under the new header.
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ // Create a new thread (activates the draft as base view and connects).
+ let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+ let panel = workspace.read_with(cx, |ws, cx| ws.panel::<AgentPanel>(cx).unwrap());
+ let connection = StubAgentConnection::new();
+ open_thread_with_connection(&panel, connection, cx);
+ cx.run_until_parked();
+
+ // Type into the draft's message editor.
+ let thread_view = panel.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
+ let message_editor = thread_view.read_with(cx, |view, _cx| view.message_editor.clone());
+ message_editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Fix the login bug", window, cx);
+ });
cx.run_until_parked();
+ // The sidebar draft title should now reflect the editor text.
+ let draft_title = sidebar.read_with(cx, |sidebar, _cx| {
+ sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => {
+ Some(thread.metadata.display_title())
+ }
+ _ => None,
+ })
+ .expect("should still have a draft entry")
+ });
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project-a, project-b]",
- " Thread 2",
- " Thread 1",
- ]
+ draft_title.as_ref(),
+ "Fix the login bug",
+ "draft title should update to match editor text"
);
+}
- // Now remove the second worktree.
- let worktree_id = project.read_with(cx, |project, cx| {
- project
- .visible_worktrees(cx)
- .find(|wt| wt.read(cx).abs_path().as_ref() == Path::new("/project-b"))
- .map(|wt| wt.read(cx).id())
- .expect("should find project-b worktree")
- });
- project.update(cx, |project, cx| {
- project.remove_worktree(worktree_id, cx);
+#[gpui::test]
+async fn test_draft_title_updates_across_two_groups(cx: &mut TestAppContext) {
+ let project_a = init_test_project_with_agent_panel("/project-a", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let (sidebar, _panel_a) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ // Add a second project group.
+ let fs = cx.update(|_, cx| <dyn fs::Fs>::global(cx));
+ fs.as_fake()
+ .insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ let project_b = project::Project::test(fs, ["/project-b".as_ref()], cx).await;
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
});
+ let panel_b = add_agent_panel(&workspace_b, cx);
cx.run_until_parked();
- // The key should revert to [/project-a].
- multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<_> = mw.project_group_keys().cloned().collect();
- assert_eq!(
- keys.len(),
- 1,
- "should have exactly 1 project group key after remove"
- );
- assert_eq!(
- keys[0].path_list(),
- &old_key_paths,
- "the key should revert to the original path list"
- );
+ // Both groups should have reconciliation drafts.
+ let draft_titles: Vec<(SharedString, bool)> = sidebar.read_with(cx, |sidebar, _cx| {
+ sidebar
+ .contents
+ .entries
+ .iter()
+ .filter_map(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => {
+ Some((thread.metadata.display_title(), false))
+ }
+ _ => None,
+ })
+ .collect()
});
+ assert_eq!(
+ draft_titles.len(),
+ 2,
+ "should have two drafts, one per group"
+ );
- // Threads should be migrated back to the old key.
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- assert_eq!(
- store.entries_for_main_worktree_path(&new_key_paths).count(),
- 0,
- "should have 0 threads under new key after revert"
- );
- assert_eq!(
- store.entries_for_main_worktree_path(&old_key_paths).count(),
- 2,
- "should have 2 threads under old key after revert"
- );
+ // Open a thread in each group's panel to get Connected state.
+ let workspace_a =
+ multi_workspace.read_with(cx, |mw, _cx| mw.workspaces().next().unwrap().clone());
+ let panel_a = workspace_a.read_with(cx, |ws, cx| ws.panel::<AgentPanel>(cx).unwrap());
+
+ let connection_a = StubAgentConnection::new();
+ open_thread_with_connection(&panel_a, connection_a, cx);
+ cx.run_until_parked();
+
+ let connection_b = StubAgentConnection::new();
+ open_thread_with_connection(&panel_b, connection_b, cx);
+ cx.run_until_parked();
+
+ // Type into group A's draft editor.
+ let thread_view_a = panel_a.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
+ let editor_a = thread_view_a.read_with(cx, |view, _cx| view.message_editor.clone());
+ editor_a.update_in(cx, |editor, window, cx| {
+ editor.set_text("Fix the login bug", window, cx);
});
+ cx.run_until_parked();
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ // Type into group B's draft editor.
+ let thread_view_b = panel_b.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
+ let editor_b = thread_view_b.read_with(cx, |view, _cx| view.message_editor.clone());
+ editor_b.update_in(cx, |editor, window, cx| {
+ editor.set_text("Refactor the database", window, cx);
+ });
cx.run_until_parked();
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project-a]",
- " Thread 2",
- " Thread 1",
- ]
+ // Both draft titles should reflect their respective editor text.
+ let draft_titles: Vec<SharedString> = sidebar.read_with(cx, |sidebar, _cx| {
+ sidebar
+ .contents
+ .entries
+ .iter()
+ .filter_map(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => {
+ Some(thread.metadata.display_title())
+ }
+ _ => None,
+ })
+ .collect()
+ });
+ assert_eq!(draft_titles.len(), 2, "should still have two drafts");
+ assert!(
+ draft_titles.contains(&SharedString::from("Fix the login bug")),
+ "group A draft should show editor text, got: {:?}",
+ draft_titles
+ );
+ assert!(
+ draft_titles.contains(&SharedString::from("Refactor the database")),
+ "group B draft should show editor text, got: {:?}",
+ draft_titles
);
}
#[gpui::test]
-async fn test_worktree_add_and_remove_preserves_thread_path_associations(cx: &mut TestAppContext) {
- // Verifies that adding/removing folders to a project correctly updates
- // each thread's worktree_paths (both folder_paths and main_worktree_paths)
- // while preserving per-path associations for linked worktrees.
+async fn test_draft_title_survives_folder_addition(cx: &mut TestAppContext) {
+ // When a folder is added to the project, the group key changes.
+ // The draft's editor observation should still work and the title
+ // should update when the user types.
init_test(cx);
let fs = FakeFs::new(cx.executor());
- fs.insert_tree(
- "/project",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: PathBuf::from("/wt-feature"),
- ref_name: Some("refs/heads/feature".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
- fs.insert_tree("/other-project", serde_json::json!({ ".git": {} }))
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "lib": {} }))
.await;
- cx.update(|cx| <dyn Fs>::set_global(fs.clone(), cx));
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- // Start with a linked worktree workspace: visible root is /wt-feature,
- // main repo is /project.
- let project =
- project::Project::test(fs.clone() as Arc<dyn Fs>, ["/wt-feature".as_ref()], cx).await;
+ let project = project::Project::test(fs.clone(), [Path::new("/project-a")], cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let _sidebar = setup_sidebar(&multi_workspace, cx);
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ // Create a thread with a connection (has a session_id, considered
+ // a draft by the panel until messages are sent).
+ let connection = StubAgentConnection::new();
+ open_thread_with_connection(&panel, connection, cx);
+ cx.run_until_parked();
- // Save a thread. It should have folder_paths=[/wt-feature], main=[/project].
- save_named_thread_metadata("thread-1", "Thread 1", &project, cx).await;
+ // Type into the editor.
+ let thread_view = panel.read_with(cx, |panel, cx| panel.active_thread_view(cx).unwrap());
+ let editor = thread_view.read_with(cx, |view, _cx| view.message_editor.clone());
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Initial text", window, cx);
+ });
+ let thread_id = panel.read_with(cx, |panel, cx| panel.active_thread_id(cx).unwrap());
+ cx.run_until_parked();
- let session_id = acp::SessionId::new(Arc::from("thread-1"));
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- let thread = store.entry(&session_id).expect("thread should exist");
- assert_eq!(
- thread.folder_paths().paths(),
- &[PathBuf::from("/wt-feature")],
- "initial folder_paths should be the linked worktree"
- );
+ // The thread without a title should show the editor text via
+ // the draft title override.
+ sidebar.read_with(cx, |sidebar, _cx| {
+ let thread = sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(t) if t.metadata.thread_id == thread_id => Some(t),
+ _ => None,
+ });
assert_eq!(
- thread.main_worktree_paths().paths(),
- &[PathBuf::from("/project")],
- "initial main_worktree_paths should be the main repo"
+ thread.and_then(|t| t.metadata.title.as_ref().map(|s| s.as_ref())),
+ Some("Initial text"),
+ "draft title should show editor text before folder add"
);
});
- // Add /other-project to the workspace.
+ // Add a second folder to the project — this changes the group key.
project
.update(cx, |project, cx| {
- project.find_or_create_worktree("/other-project", true, cx)
+ project.find_or_create_worktree("/project-b", true, cx)
})
.await
.expect("should add worktree");
cx.run_until_parked();
- // Thread should now have both paths, with correct associations.
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- let thread = store.entry(&session_id).expect("thread should exist");
- let pairs: Vec<_> = thread
- .worktree_paths
- .ordered_pairs()
- .map(|(m, f)| (m.clone(), f.clone()))
- .collect();
- assert!(
- pairs.contains(&(PathBuf::from("/project"), PathBuf::from("/wt-feature"))),
- "linked worktree association should be preserved, got: {:?}",
- pairs
- );
- assert!(
- pairs.contains(&(
- PathBuf::from("/other-project"),
- PathBuf::from("/other-project")
- )),
- "new folder should have main == folder, got: {:?}",
- pairs
- );
- });
-
- // Remove /other-project.
- let worktree_id = project.read_with(cx, |project, cx| {
- project
- .visible_worktrees(cx)
- .find(|wt| wt.read(cx).abs_path().as_ref() == Path::new("/other-project"))
- .map(|wt| wt.read(cx).id())
- .expect("should find other-project worktree")
- });
- project.update(cx, |project, cx| {
- project.remove_worktree(worktree_id, cx);
+ // Update editor text.
+ editor.update_in(cx, |editor, window, cx| {
+ editor.set_text("Updated after folder add", window, cx);
});
cx.run_until_parked();
- // Thread should be back to original state.
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- let thread = store.entry(&session_id).expect("thread should exist");
- assert_eq!(
- thread.folder_paths().paths(),
- &[PathBuf::from("/wt-feature")],
- "folder_paths should revert to just the linked worktree"
- );
- assert_eq!(
- thread.main_worktree_paths().paths(),
- &[PathBuf::from("/project")],
- "main_worktree_paths should revert to just the main repo"
- );
- let pairs: Vec<_> = thread
- .worktree_paths
- .ordered_pairs()
- .map(|(m, f)| (m.clone(), f.clone()))
- .collect();
- assert_eq!(
- pairs,
- vec![(PathBuf::from("/project"), PathBuf::from("/wt-feature"))],
- "linked worktree association should be preserved through add+remove cycle"
- );
+ // The draft title should still update. After adding a folder the
+ // group key changes, so the thread may not appear in the sidebar
+ // if its metadata was saved under the old path list. If it IS
+ // found, verify the title was overridden.
+ sidebar.read_with(cx, |sidebar, _cx| {
+ let thread = sidebar
+ .contents
+ .entries
+ .iter()
+ .find_map(|entry| match entry {
+ ListEntry::Thread(t) if t.metadata.thread_id == thread_id => Some(t),
+ _ => None,
+ });
+ if let Some(thread) = thread {
+ assert_eq!(
+ thread.metadata.title.as_ref().map(|s| s.as_ref()),
+ Some("Updated after folder add"),
+ "draft title should update even after adding a folder"
+ );
+ }
});
}
#[gpui::test]
-async fn test_worktree_add_key_collision_removes_duplicate_workspace(cx: &mut TestAppContext) {
- // When a worktree is added to workspace A and the resulting key matches
- // an existing workspace B's key (and B has the same root paths), B
- // should be removed as a true duplicate.
- let (fs, project_a) = init_multi_project_test(&["/project-a", "/project-b"], cx).await;
+async fn test_cmd_n_shows_new_thread_entry(cx: &mut TestAppContext) {
+ // When the user presses Cmd-N (NewThread action) while viewing a
+ // non-empty thread, the sidebar should show the "New Thread" entry.
+ // This exercises the same code path as the workspace action handler
+ // (which bypasses the sidebar's create_new_thread method).
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
- // Save a thread against workspace A [/project-a].
- save_named_thread_metadata("thread-a", "Thread A", &project_a, cx).await;
+ // Create a non-empty thread (has messages).
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ open_thread_with_connection(&panel, connection, cx);
+ send_message(&panel, cx);
- // Create workspace B with both worktrees [/project-a, /project-b].
- let project_b = project::Project::test(
- fs.clone() as Arc<dyn Fs>,
- ["/project-a".as_ref(), "/project-b".as_ref()],
- cx,
- )
- .await;
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx)
- });
+ let session_id = active_session_id(&panel, cx);
+ save_test_thread_metadata(&session_id, &project, cx).await;
cx.run_until_parked();
- // Switch back to workspace A so it's the active workspace when the collision happens.
- let workspace_a =
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
- multi_workspace.update_in(cx, |mw, window, cx| {
- mw.activate(workspace_a, window, cx);
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ //
+ "v [my-project]",
+ " Hello *",
+ ]
+ );
+
+ // Simulate cmd-n
+ let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
+ panel.update_in(cx, |panel, window, cx| {
+ panel.new_thread(&NewThread, window, cx);
+ });
+ workspace.update_in(cx, |workspace, window, cx| {
+ workspace.focus_panel::<AgentPanel>(window, cx);
});
cx.run_until_parked();
- // Save a thread against workspace B [/project-a, /project-b].
- save_named_thread_metadata("thread-b", "Thread B", &project_b, cx).await;
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["v [my-project]", " [~ Draft] *", " Hello *"],
+ "After Cmd-N the sidebar should show a highlighted Draft entry"
+ );
+
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert_active_draft(
+ sidebar,
+ &workspace,
+ "active_entry should be Draft after Cmd-N",
+ );
+ });
+}
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+#[gpui::test]
+async fn test_draft_with_server_session_shows_as_draft(cx: &mut TestAppContext) {
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ // Create a saved thread so the workspace has history.
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ open_thread_with_connection(&panel, connection, cx);
+ send_message(&panel, cx);
+ let saved_session_id = active_session_id(&panel, cx);
+ save_test_thread_metadata(&saved_session_id, &project, cx).await;
cx.run_until_parked();
- // Both project groups should be visible.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
- "v [project-a, project-b]",
- " Thread B",
- "v [project-a]",
- " Thread A",
+ "v [my-project]",
+ " Hello *",
]
);
- let workspace_b_id = workspace_b.entity_id();
-
- // Now add /project-b to workspace A's project, causing a key collision.
- project_a
- .update(cx, |project, cx| {
- project.find_or_create_worktree("/project-b", true, cx)
- })
- .await
- .expect("should add worktree");
+ // Create a new draft via Cmd-N. Since new_thread() now creates a
+ // tracked draft in the AgentPanel, it appears in the sidebar.
+ panel.update_in(cx, |panel, window, cx| {
+ panel.new_thread(&NewThread, window, cx);
+ });
cx.run_until_parked();
- // Workspace B should have been removed (true duplicate — same root paths).
- multi_workspace.read_with(cx, |mw, _cx| {
- let workspace_ids: Vec<_> = mw.workspaces().map(|ws| ws.entity_id()).collect();
- assert!(
- !workspace_ids.contains(&workspace_b_id),
- "workspace B should have been removed after key collision"
- );
- });
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["v [my-project]", " [~ Draft] *", " Hello *"],
+ );
- // There should be exactly one project group key now.
- let combined_paths = PathList::new(&[PathBuf::from("/project-a"), PathBuf::from("/project-b")]);
- multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<_> = mw.project_group_keys().cloned().collect();
- assert_eq!(
- keys.len(),
- 1,
- "should have exactly 1 project group key after collision"
- );
- assert_eq!(
- keys[0].path_list(),
- &combined_paths,
- "the remaining key should be the combined paths"
+ let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert_active_draft(
+ sidebar,
+ &workspace,
+ "Draft with server session should be Draft, not Thread",
);
});
+}
- // Both threads should be visible under the merged group.
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+#[gpui::test]
+async fn test_sending_message_from_draft_removes_draft(cx: &mut TestAppContext) {
+ // When the user sends a message from a draft thread, the draft
+ // should be removed from the sidebar and the active_entry should
+ // transition to a Thread pointing at the new session.
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ // Create a saved thread so the group isn't empty.
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ open_thread_with_connection(&panel, connection, cx);
+ send_message(&panel, cx);
+ let existing_session_id = active_session_id(&panel, cx);
+ save_test_thread_metadata(&existing_session_id, &project, cx).await;
+ cx.run_until_parked();
+
+ // Create a draft via Cmd-N.
+ panel.update_in(cx, |panel, window, cx| {
+ panel.new_thread(&NewThread, window, cx);
+ });
cx.run_until_parked();
+ let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project-a, project-b]",
- " Thread A",
- " Thread B",
- ]
+ vec!["v [my-project]", " [~ Draft] *", " Hello *"],
+ "draft should be visible before sending",
+ );
+ sidebar.read_with(cx, |sidebar, _| {
+ assert_active_draft(sidebar, &workspace, "should be on draft before sending");
+ });
+
+ // Simulate what happens when a draft sends its first message:
+ // the AgentPanel's MessageSentOrQueued handler removes the draft
+ // from `draft_threads`, then the sidebar rebuilds. We can't use
+ // the NativeAgentServer in tests, so replicate the key steps:
+ // remove the draft, open a real thread with a stub connection,
+ // and send.
+ let thread_id = panel.read_with(cx, |panel, cx| panel.active_thread_id(cx).unwrap());
+ panel.update_in(cx, |panel, _window, cx| {
+ panel.remove_thread(thread_id, cx);
+ });
+ let draft_connection = StubAgentConnection::new();
+ draft_connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("World".into()),
+ )]);
+ open_thread_with_connection(&panel, draft_connection, cx);
+ send_message(&panel, cx);
+ let new_session_id = active_session_id(&panel, cx);
+ save_test_thread_metadata(&new_session_id, &project, cx).await;
+ cx.run_until_parked();
+
+ // The draft should be gone and the new thread should be active.
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
+ assert_eq!(
+ draft_count, 0,
+ "draft should be removed after sending a message"
);
+
+ sidebar.read_with(cx, |sidebar, _| {
+ assert_active_thread(
+ sidebar,
+ &new_session_id,
+ "active_entry should transition to the new thread after sending",
+ );
+ });
}
#[gpui::test]
-async fn test_worktree_collision_keeps_active_workspace(cx: &mut TestAppContext) {
- // When workspace A adds a folder that makes it collide with workspace B,
- // and B is the *active* workspace, A (the incoming one) should be
- // dropped so the user stays on B. A linked worktree sibling of A
- // should migrate into B's group.
- init_test(cx);
+async fn test_cmd_n_shows_new_thread_entry_in_absorbed_worktree(cx: &mut TestAppContext) {
+ // When the active workspace is an absorbed git worktree, cmd-n
+ // should still show the "New Thread" entry under the main repo's
+ // header and highlight it as active.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
let fs = FakeFs::new(cx.executor());
- // Set up /project-a with a linked worktree.
- fs.insert_tree(
- "/project-a",
- serde_json::json!({
- ".git": {
- "worktrees": {
- "feature": {
- "commondir": "../../",
- "HEAD": "ref: refs/heads/feature",
- },
- },
- },
- "src": {},
- }),
- )
- .await;
+ // Main repo with a linked worktree.
fs.insert_tree(
- "/wt-feature",
+ "/project",
serde_json::json!({
- ".git": "gitdir: /project-a/.git/worktrees/feature",
+ ".git": {},
"src": {},
}),
)
.await;
+
+ // Worktree checkout pointing back to the main repo.
fs.add_linked_worktree_for_repo(
- Path::new("/project-a/.git"),
+ Path::new("/project/.git"),
false,
git::repository::Worktree {
- path: PathBuf::from("/wt-feature"),
- ref_name: Some("refs/heads/feature".into()),
+ path: std::path::PathBuf::from("/wt-feature-a"),
+ ref_name: Some("refs/heads/feature-a".into()),
sha: "aaa".into(),
is_main: false,
},
)
.await;
- fs.insert_tree("/project-b", serde_json::json!({ ".git": {}, "src": {} }))
- .await;
+
cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
- project_a.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
+ let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- // Linked worktree sibling of A.
- let project_wt = project::Project::test(fs.clone(), ["/wt-feature".as_ref()], cx).await;
- project_wt
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+ worktree_project
.update(cx, |p, cx| p.git_scans_complete(cx))
.await;
-
- // Workspace B has both folders already.
- let project_b = project::Project::test(
- fs.clone() as Arc<dyn Fs>,
- ["/project-a".as_ref(), "/project-b".as_ref()],
- cx,
- )
- .await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
- // Add agent panels to all workspaces.
- let workspace_a_entity = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
- add_agent_panel(&workspace_a_entity, cx);
+ let sidebar = setup_sidebar(&multi_workspace, cx);
- // Add the linked worktree workspace (sibling of A).
- let workspace_wt = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_wt.clone(), window, cx)
- });
- add_agent_panel(&workspace_wt, cx);
- cx.run_until_parked();
+ let worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(worktree_project.clone(), window, cx)
+ });
- // Add workspace B (will become active).
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx)
+ let worktree_panel = add_agent_panel(&worktree_workspace, cx);
+
+ // Switch to the worktree workspace.
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ let workspace = mw.workspaces().nth(1).unwrap().clone();
+ mw.activate(workspace, window, cx);
});
- add_agent_panel(&workspace_b, cx);
- cx.run_until_parked();
- // Save threads in each group.
- save_named_thread_metadata("thread-a", "Thread A", &project_a, cx).await;
- save_thread_metadata_with_main_paths(
- "thread-wt",
- "Worktree Thread",
- PathList::new(&[PathBuf::from("/wt-feature")]),
- PathList::new(&[PathBuf::from("/project-a")]),
- cx,
- );
- save_named_thread_metadata("thread-b", "Thread B", &project_b, cx).await;
+ // Create a non-empty thread in the worktree workspace.
+ let connection = StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ open_thread_with_connection(&worktree_panel, connection, cx);
+ send_message(&worktree_panel, cx);
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ let session_id = active_session_id(&worktree_panel, cx);
+ save_test_thread_metadata(&session_id, &worktree_project, cx).await;
cx.run_until_parked();
- // B is active, A and wt-feature are in one group, B in another.
assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().entity_id()),
- workspace_b.entity_id(),
- "workspace B should be active"
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ //
+ "v [project]",
+ " Hello {wt-feature-a} *",
+ ]
);
- multi_workspace.read_with(cx, |mw, _cx| {
- assert_eq!(mw.project_group_keys().count(), 2, "should have 2 groups");
- assert_eq!(mw.workspaces().count(), 3, "should have 3 workspaces");
+
+ // Simulate Cmd-N in the worktree workspace.
+ worktree_panel.update_in(cx, |panel, window, cx| {
+ panel.new_thread(&NewThread, window, cx);
+ });
+ worktree_workspace.update_in(cx, |workspace, window, cx| {
+ workspace.focus_panel::<AgentPanel>(window, cx);
});
+ cx.run_until_parked();
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
- "v [project-a, project-b]",
- " Thread B",
- "v [project-a]",
- " Thread A",
- " Worktree Thread {wt-feature}",
- ]
+ "v [project]",
+ " [~ Draft {wt-feature-a}] *",
+ " Hello {wt-feature-a} *"
+ ],
+ "After Cmd-N in an absorbed worktree, the sidebar should show \
+ a highlighted Draft entry under the main repo header"
);
- let workspace_a = multi_workspace.read_with(cx, |mw, _| {
- mw.workspaces()
- .find(|ws| {
- ws.entity_id() != workspace_b.entity_id()
- && ws.entity_id() != workspace_wt.entity_id()
- })
- .unwrap()
- .clone()
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert_active_draft(
+ sidebar,
+ &worktree_workspace,
+ "active_entry should be Draft after Cmd-N",
+ );
});
+}
- // Add /project-b to workspace A's project, causing a collision with B.
- project_a
- .update(cx, |project, cx| {
- project.find_or_create_worktree("/project-b", true, cx)
- })
- .await
- .expect("should add worktree");
+async fn init_test_project_with_git(
+ worktree_path: &str,
+ cx: &mut TestAppContext,
+) -> (Entity<project::Project>, Arc<dyn fs::Fs>) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ worktree_path,
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+ let project = project::Project::test(fs.clone(), [worktree_path.as_ref()], cx).await;
+ (project, fs)
+}
+
+#[gpui::test]
+async fn test_search_matches_worktree_name(cx: &mut TestAppContext) {
+ let (project, fs) = init_test_project_with_git("/project", cx).await;
+
+ fs.as_fake()
+ .add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt/rosewood"),
+ ref_name: Some("refs/heads/rosewood".into()),
+ sha: "abc".into(),
+ is_main: false,
+ },
+ )
+ .await;
+
+ project
+ .update(cx, |project, cx| project.git_scans_complete(cx))
+ .await;
+
+ let worktree_project = project::Project::test(fs.clone(), ["/wt/rosewood".as_ref()], cx).await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ save_named_thread_metadata("main-t", "Unrelated Thread", &project, cx).await;
+ save_named_thread_metadata("wt-t", "Fix Bug", &worktree_project, cx).await;
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- // Workspace A (the incoming duplicate) should have been dropped.
- multi_workspace.read_with(cx, |mw, _cx| {
- let workspace_ids: Vec<_> = mw.workspaces().map(|ws| ws.entity_id()).collect();
- assert!(
- !workspace_ids.contains(&workspace_a.entity_id()),
- "workspace A should have been dropped"
- );
- });
+ // Search for "rosewood" — should match the worktree name, not the title.
+ type_in_search(&sidebar, "rosewood", cx);
- // The active workspace should still be B.
assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().entity_id()),
- workspace_b.entity_id(),
- "workspace B should still be active"
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ //
+ "v [project]",
+ " Fix Bug {rosewood} <== selected",
+ ],
);
+}
- // The linked worktree sibling should have migrated into B's group
- // (it got the folder add and now shares the same key).
- multi_workspace.read_with(cx, |mw, _cx| {
- let workspace_ids: Vec<_> = mw.workspaces().map(|ws| ws.entity_id()).collect();
- assert!(
- workspace_ids.contains(&workspace_wt.entity_id()),
- "linked worktree workspace should still exist"
- );
- assert_eq!(
- mw.project_group_keys().count(),
- 1,
- "should have 1 group after merge"
- );
- assert_eq!(
- mw.workspaces().count(),
- 2,
- "should have 2 workspaces (B + linked worktree)"
- );
- });
+#[gpui::test]
+async fn test_git_worktree_added_live_updates_sidebar(cx: &mut TestAppContext) {
+ let (project, fs) = init_test_project_with_git("/project", cx).await;
+
+ project
+ .update(cx, |project, cx| project.git_scans_complete(cx))
+ .await;
+
+ let worktree_project = project::Project::test(fs.clone(), ["/wt/rosewood".as_ref()], cx).await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
- // The linked worktree workspace should have gotten the new folder.
- let wt_worktree_count =
- project_wt.read_with(cx, |project, cx| project.visible_worktrees(cx).count());
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ // Save a thread against a worktree path with the correct main
+ // worktree association (as if the git state had been resolved).
+ save_thread_metadata_with_main_paths(
+ "wt-thread",
+ "Worktree Thread",
+ PathList::new(&[PathBuf::from("/wt/rosewood")]),
+ PathList::new(&[PathBuf::from("/project")]),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ cx,
+ );
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ cx.run_until_parked();
+
+ // Thread is visible because its main_worktree_paths match the group.
+ // The chip name is derived from the path even before git discovery.
assert_eq!(
- wt_worktree_count, 2,
- "linked worktree project should have gotten /project-b"
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["v [project]", " Worktree Thread {rosewood}"]
);
- // After: everything merged under one group. Thread A migrated,
- // worktree thread shows its chip, B's thread and draft remain.
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ // Now add the worktree to the git state and trigger a rescan.
+ fs.as_fake()
+ .add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ true,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt/rosewood"),
+ ref_name: Some("refs/heads/rosewood".into()),
+ sha: "abc".into(),
+ is_main: false,
+ },
+ )
+ .await;
+
cx.run_until_parked();
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
- "v [project-a, project-b]",
- " Thread A",
- " Worktree Thread {project-a:wt-feature}",
- " Thread B",
+ "v [project]",
+ " Worktree Thread {rosewood}",
]
);
}
#[gpui::test]
-async fn test_worktree_add_syncs_linked_worktree_sibling(cx: &mut TestAppContext) {
- // When a worktree is added to the main workspace, a linked worktree
- // sibling (different root paths, same project group key) should also
- // get the new folder added to its project.
+async fn test_two_worktree_workspaces_absorbed_when_main_added(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
+ // Create the main repo directory (not opened as a workspace yet).
fs.insert_tree(
"/project",
serde_json::json!({
".git": {
- "worktrees": {
- "feature": {
- "commondir": "../../",
- "HEAD": "ref: refs/heads/feature",
- },
- },
},
"src": {},
}),
)
.await;
- fs.insert_tree(
- "/wt-feature",
- serde_json::json!({
- ".git": "gitdir: /project/.git/worktrees/feature",
- "src": {},
- }),
- )
- .await;
-
+ // Two worktree checkouts whose .git files point back to the main repo.
fs.add_linked_worktree_for_repo(
Path::new("/project/.git"),
false,
git::repository::Worktree {
- path: PathBuf::from("/wt-feature"),
- ref_name: Some("refs/heads/feature".into()),
+ path: std::path::PathBuf::from("/wt-feature-a"),
+ ref_name: Some("refs/heads/feature-a".into()),
sha: "aaa".into(),
is_main: false,
},
)
.await;
-
- // Create a second independent project to add as a folder later.
- fs.insert_tree(
- "/other-project",
- serde_json::json!({ ".git": {}, "src": {} }),
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-feature-b"),
+ ref_name: Some("refs/heads/feature-b".into()),
+ sha: "bbb".into(),
+ is_main: false,
+ },
)
.await;
cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- let worktree_project = project::Project::test(fs.clone(), ["/wt-feature".as_ref()], cx).await;
+ let project_a = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/wt-feature-b".as_ref()], cx).await;
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
+ project_a.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ project_b.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ // Open both worktrees as workspaces — no main repo yet.
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- // Add agent panel to the main workspace.
- let main_workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
- add_agent_panel(&main_workspace, cx);
-
- // Open the linked worktree as a separate workspace.
- let wt_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(worktree_project.clone(), window, cx)
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx);
});
- add_agent_panel(&wt_workspace, cx);
- cx.run_until_parked();
+ let sidebar = setup_sidebar(&multi_workspace, cx);
- // Both workspaces should share the same project group key [/project].
- multi_workspace.read_with(cx, |mw, _cx| {
- assert_eq!(
- mw.project_group_keys().count(),
- 1,
- "should have 1 project group key before add"
- );
- assert_eq!(mw.workspaces().count(), 2, "should have 2 workspaces");
- });
-
- // Save threads against each workspace.
- save_named_thread_metadata("main-thread", "Main Thread", &main_project, cx).await;
- save_named_thread_metadata("wt-thread", "Worktree Thread", &worktree_project, cx).await;
-
- // Verify both threads are under the old key [/project].
- let old_key_paths = PathList::new(&[PathBuf::from("/project")]);
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- assert_eq!(
- store.entries_for_main_worktree_path(&old_key_paths).count(),
- 2,
- "should have 2 threads under old key before add"
- );
- });
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-a")),
+ Some("Thread A".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ None,
+ &project_a,
+ cx,
+ );
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-b")),
+ Some("Thread B".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 1).unwrap(),
+ None,
+ &project_b,
+ cx,
+ );
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
+ // Without the main repo, each worktree has its own header.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
"v [project]",
- " Worktree Thread {wt-feature}",
- " Main Thread",
+ " Thread B {wt-feature-b}",
+ " Thread A {wt-feature-a}",
]
);
- // Add /other-project as a folder to the main workspace.
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
main_project
- .update(cx, |project, cx| {
- project.find_or_create_worktree("/other-project", true, cx)
- })
- .await
- .expect("should add worktree");
- cx.run_until_parked();
-
- // The linked worktree workspace should have gotten the new folder too.
- let wt_worktree_count =
- worktree_project.read_with(cx, |project, cx| project.visible_worktrees(cx).count());
- assert_eq!(
- wt_worktree_count, 2,
- "linked worktree project should have gotten the new folder"
- );
-
- // Both workspaces should still exist under one key.
- multi_workspace.read_with(cx, |mw, _cx| {
- assert_eq!(mw.workspaces().count(), 2, "both workspaces should survive");
- assert_eq!(
- mw.project_group_keys().count(),
- 1,
- "should still have 1 project group key"
- );
- });
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
- // Threads should have been migrated to the new key.
- let new_key_paths =
- PathList::new(&[PathBuf::from("/other-project"), PathBuf::from("/project")]);
- cx.update(|_window, cx| {
- let store = ThreadMetadataStore::global(cx).read(cx);
- assert_eq!(
- store.entries_for_main_worktree_path(&old_key_paths).count(),
- 0,
- "should have 0 threads under old key after migration"
- );
- assert_eq!(
- store.entries_for_main_worktree_path(&new_key_paths).count(),
- 2,
- "should have 2 threads under new key after migration"
- );
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(main_project.clone(), window, cx);
});
-
- // Both threads should still be visible in the sidebar.
- sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
cx.run_until_parked();
+ // Both worktree workspaces should now be absorbed under the main
+ // repo header, with worktree chips.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
- "v [other-project, project]",
- " Worktree Thread {project:wt-feature}",
- " Main Thread",
+ "v [project]",
+ " Thread B {wt-feature-b}",
+ " Thread A {wt-feature-a}",
]
);
}
#[gpui::test]
-async fn test_cmd_n_shows_new_thread_entry(cx: &mut TestAppContext) {
- // When the user presses Cmd-N (NewThread action) while viewing a
- // non-empty thread, the sidebar should show the "New Thread" entry.
- // This exercises the same code path as the workspace action handler
- // (which bypasses the sidebar's create_new_thread method).
- let project = init_test_project_with_agent_panel("/my-project", cx).await;
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+async fn test_threadless_workspace_shows_new_thread_with_worktree_chip(cx: &mut TestAppContext) {
+ // When a group has two workspaces — one with threads and one
+ // without — the threadless workspace should appear as a
+ // "New Thread" button with its worktree chip.
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
- // Create a non-empty thread (has messages).
- let connection = StubAgentConnection::new();
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("Done".into()),
- )]);
- open_thread_with_connection(&panel, connection, cx);
- send_message(&panel, cx);
+ // Main repo with two linked worktrees.
+ fs.insert_tree(
+ "/project",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-feature-a"),
+ ref_name: Some("refs/heads/feature-a".into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-feature-b"),
+ ref_name: Some("refs/heads/feature-b".into()),
+ sha: "bbb".into(),
+ is_main: false,
+ },
+ )
+ .await;
- let session_id = active_session_id(&panel, cx);
- save_test_thread_metadata(&session_id, &project, cx).await;
- cx.run_until_parked();
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [my-project]",
- " Hello *",
- ]
- );
+ // Workspace A: worktree feature-a (has threads).
+ let project_a = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
+ project_a.update(cx, |p, cx| p.git_scans_complete(cx)).await;
- // Simulate cmd-n
- let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
- panel.update_in(cx, |panel, window, cx| {
- panel.new_thread(&NewThread, window, cx);
- });
- workspace.update_in(cx, |workspace, window, cx| {
- workspace.focus_panel::<AgentPanel>(window, cx);
+ // Workspace B: worktree feature-b (no threads).
+ let project_b = project::Project::test(fs.clone(), ["/wt-feature-b".as_ref()], cx).await;
+ project_b.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx);
});
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ // Only save a thread for workspace A.
+ save_named_thread_metadata("thread-a", "Thread A", &project_a, cx).await;
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
+ // Workspace A's thread appears normally. Workspace B (threadless)
+ // appears as a "New Thread" button with its worktree chip.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec!["v [my-project]", " [~ Draft] *", " Hello *"],
- "After Cmd-N the sidebar should show a highlighted Draft entry"
+ vec!["v [project]", " Thread A {wt-feature-a}",]
);
-
- sidebar.read_with(cx, |sidebar, _cx| {
- assert_active_draft(
- sidebar,
- &workspace,
- "active_entry should be Draft after Cmd-N",
- );
- });
}
#[gpui::test]
-async fn test_draft_with_server_session_shows_as_draft(cx: &mut TestAppContext) {
- let project = init_test_project_with_agent_panel("/my-project", cx).await;
+async fn test_multi_worktree_thread_shows_multiple_chips(cx: &mut TestAppContext) {
+ // A thread created in a workspace with roots from different git
+ // worktrees should show a chip for each distinct worktree name.
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+
+ // Two main repos.
+ fs.insert_tree(
+ "/project_a",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+ fs.insert_tree(
+ "/project_b",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+
+ // Worktree checkouts.
+ for repo in &["project_a", "project_b"] {
+ let git_path = format!("/{repo}/.git");
+ for branch in &["olivetti", "selectric"] {
+ fs.add_linked_worktree_for_repo(
+ Path::new(&git_path),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from(format!("/worktrees/{repo}/{branch}/{repo}")),
+ ref_name: Some(format!("refs/heads/{branch}").into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
+ }
+ }
+
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ // Open a workspace with the worktree checkout paths as roots
+ // (this is the workspace the thread was created in).
+ let project = project::Project::test(
+ fs.clone(),
+ [
+ "/worktrees/project_a/olivetti/project_a".as_ref(),
+ "/worktrees/project_b/selectric/project_b".as_ref(),
+ ],
+ cx,
+ )
+ .await;
+ project.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+ let sidebar = setup_sidebar(&multi_workspace, cx);
- // Create a saved thread so the workspace has history.
- let connection = StubAgentConnection::new();
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("Done".into()),
- )]);
- open_thread_with_connection(&panel, connection, cx);
- send_message(&panel, cx);
- let saved_session_id = active_session_id(&panel, cx);
- save_test_thread_metadata(&saved_session_id, &project, cx).await;
+ // Save a thread under the same paths as the workspace roots.
+ save_named_thread_metadata("wt-thread", "Cross Worktree Thread", &project, cx).await;
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
+ // Should show two distinct worktree chips.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
- "v [my-project]",
- " Hello *",
+ "v [project_a, project_b]",
+ " Cross Worktree Thread {project_a:olivetti}, {project_b:selectric}",
]
);
+}
- // Create a new draft via Cmd-N. Since new_thread() now creates a
- // tracked draft in the AgentPanel, it appears in the sidebar.
- panel.update_in(cx, |panel, window, cx| {
- panel.new_thread(&NewThread, window, cx);
- });
- cx.run_until_parked();
-
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec!["v [my-project]", " [~ Draft] *", " Hello *"],
- );
+#[gpui::test]
+async fn test_same_named_worktree_chips_are_deduplicated(cx: &mut TestAppContext) {
+ // When a thread's roots span multiple repos but share the same
+ // worktree name (e.g. both in "olivetti"), only one chip should
+ // appear.
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
- let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
- sidebar.read_with(cx, |sidebar, _cx| {
- assert_active_draft(
- sidebar,
- &workspace,
- "Draft with server session should be Draft, not Thread",
- );
- });
-}
+ fs.insert_tree(
+ "/project_a",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+ fs.insert_tree(
+ "/project_b",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+
+ for repo in &["project_a", "project_b"] {
+ let git_path = format!("/{repo}/.git");
+ fs.add_linked_worktree_for_repo(
+ Path::new(&git_path),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from(format!("/worktrees/{repo}/olivetti/{repo}")),
+ ref_name: Some("refs/heads/olivetti".into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
+ }
+
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project = project::Project::test(
+ fs.clone(),
+ [
+ "/worktrees/project_a/olivetti/project_a".as_ref(),
+ "/worktrees/project_b/olivetti/project_b".as_ref(),
+ ],
+ cx,
+ )
+ .await;
+ project.update(cx, |p, cx| p.git_scans_complete(cx)).await;
-#[gpui::test]
-async fn test_sending_message_from_draft_removes_draft(cx: &mut TestAppContext) {
- // When the user sends a message from a draft thread, the draft
- // should be removed from the sidebar and the active_entry should
- // transition to a Thread pointing at the new session.
- let project = init_test_project_with_agent_panel("/my-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+ let sidebar = setup_sidebar(&multi_workspace, cx);
- // Create a saved thread so the group isn't empty.
- let connection = StubAgentConnection::new();
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("Done".into()),
- )]);
- open_thread_with_connection(&panel, connection, cx);
- send_message(&panel, cx);
- let existing_session_id = active_session_id(&panel, cx);
- save_test_thread_metadata(&existing_session_id, &project, cx).await;
- cx.run_until_parked();
+ // Thread with roots in both repos' "olivetti" worktrees.
+ save_named_thread_metadata("wt-thread", "Same Branch Thread", &project, cx).await;
- // Create a draft via Cmd-N.
- panel.update_in(cx, |panel, window, cx| {
- panel.new_thread(&NewThread, window, cx);
- });
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+ // Both worktree paths have the name "olivetti", so only one chip.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec!["v [my-project]", " [~ Draft] *", " Hello *"],
- "draft should be visible before sending",
- );
- sidebar.read_with(cx, |sidebar, _| {
- assert_active_draft(sidebar, &workspace, "should be on draft before sending");
- });
-
- // Simulate what happens when a draft sends its first message:
- // the AgentPanel's MessageSentOrQueued handler removes the draft
- // from `draft_threads`, then the sidebar rebuilds. We can't use
- // the NativeAgentServer in tests, so replicate the key steps:
- // remove the draft, open a real thread with a stub connection,
- // and send.
- let draft_id = panel.read_with(cx, |panel, _| panel.active_draft_id().unwrap());
- panel.update_in(cx, |panel, _window, _cx| {
- panel.remove_draft(draft_id);
- });
- let draft_connection = StubAgentConnection::new();
- draft_connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("World".into()),
- )]);
- open_thread_with_connection(&panel, draft_connection, cx);
- send_message(&panel, cx);
- let new_session_id = active_session_id(&panel, cx);
- save_test_thread_metadata(&new_session_id, &project, cx).await;
- cx.run_until_parked();
-
- // The draft should be gone and the new thread should be active.
- let entries = visible_entries_as_strings(&sidebar, cx);
- let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
- assert_eq!(
- draft_count, 0,
- "draft should be removed after sending a message"
+ vec![
+ //
+ "v [project_a, project_b]",
+ " Same Branch Thread {olivetti}",
+ ]
);
-
- sidebar.read_with(cx, |sidebar, _| {
- assert_active_thread(
- sidebar,
- &new_session_id,
- "active_entry should transition to the new thread after sending",
- );
- });
}
#[gpui::test]
-async fn test_cmd_n_shows_new_thread_entry_in_absorbed_worktree(cx: &mut TestAppContext) {
- // When the active workspace is an absorbed git worktree, cmd-n
- // should still show the "New Thread" entry under the main repo's
- // header and highlight it as active.
+async fn test_absorbed_worktree_running_thread_shows_live_status(cx: &mut TestAppContext) {
+ // When a worktree workspace is absorbed under the main repo, a
+ // running thread in the worktree's agent panel should still show
+ // live status (spinner + "(running)") in the sidebar.
agent_ui::test_support::init_test(cx);
cx.update(|cx| {
ThreadStore::init_global(cx);
@@ -3608,6 +4073,7 @@ async fn test_cmd_n_shows_new_thread_entry_in_absorbed_worktree(cx: &mut TestApp
.update(cx, |p, cx| p.git_scans_complete(cx))
.await;
+ // Create the MultiWorkspace with both projects.
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
@@ -3617,213 +4083,152 @@ async fn test_cmd_n_shows_new_thread_entry_in_absorbed_worktree(cx: &mut TestApp
mw.test_add_workspace(worktree_project.clone(), window, cx)
});
+ // Add an agent panel to the worktree workspace so we can run a
+ // thread inside it.
let worktree_panel = add_agent_panel(&worktree_workspace, cx);
- // Switch to the worktree workspace.
+ // Switch back to the main workspace before setting up the sidebar.
multi_workspace.update_in(cx, |mw, window, cx| {
- let workspace = mw.workspaces().nth(1).unwrap().clone();
+ let workspace = mw.workspaces().next().unwrap().clone();
mw.activate(workspace, window, cx);
});
- // Create a non-empty thread in the worktree workspace.
+ // Start a thread in the worktree workspace's panel and keep it
+ // generating (don't resolve it).
let connection = StubAgentConnection::new();
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("Done".into()),
- )]);
- open_thread_with_connection(&worktree_panel, connection, cx);
+ open_thread_with_connection(&worktree_panel, connection.clone(), cx);
send_message(&worktree_panel, cx);
let session_id = active_session_id(&worktree_panel, cx);
- save_test_thread_metadata(&session_id, &worktree_project, cx).await;
- cx.run_until_parked();
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " Hello {wt-feature-a} *",
- ]
- );
+ // Save metadata so the sidebar knows about this thread.
+ save_test_thread_metadata(&session_id, &worktree_project, cx).await;
- // Simulate Cmd-N in the worktree workspace.
- worktree_panel.update_in(cx, |panel, window, cx| {
- panel.new_thread(&NewThread, window, cx);
- });
- worktree_workspace.update_in(cx, |workspace, window, cx| {
- workspace.focus_panel::<AgentPanel>(window, cx);
+ // Keep the thread generating by sending a chunk without ending
+ // the turn.
+ cx.update(|_, cx| {
+ connection.send_update(
+ session_id.clone(),
+ acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("working...".into())),
+ cx,
+ );
});
cx.run_until_parked();
+ // The worktree thread should be absorbed under the main project
+ // and show live running status.
+ let entries = visible_entries_as_strings(&sidebar, cx);
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " [~ Draft {wt-feature-a}] *",
- " Hello {wt-feature-a} *"
- ],
- "After Cmd-N in an absorbed worktree, the sidebar should show \
- a highlighted Draft entry under the main repo header"
+ entries,
+ vec!["v [project]", " Hello {wt-feature-a} * (running)",]
);
+}
- sidebar.read_with(cx, |sidebar, _cx| {
- assert_active_draft(
- sidebar,
- &worktree_workspace,
- "active_entry should be Draft after Cmd-N",
- );
+#[gpui::test]
+async fn test_absorbed_worktree_completion_triggers_notification(cx: &mut TestAppContext) {
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
});
-}
-async fn init_test_project_with_git(
- worktree_path: &str,
- cx: &mut TestAppContext,
-) -> (Entity<project::Project>, Arc<dyn fs::Fs>) {
- init_test(cx);
let fs = FakeFs::new(cx.executor());
+
fs.insert_tree(
- worktree_path,
+ "/project",
serde_json::json!({
".git": {},
"src": {},
}),
)
.await;
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- let project = project::Project::test(fs.clone(), [worktree_path.as_ref()], cx).await;
- (project, fs)
-}
-#[gpui::test]
-async fn test_search_matches_worktree_name(cx: &mut TestAppContext) {
- let (project, fs) = init_test_project_with_git("/project", cx).await;
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-feature-a"),
+ ref_name: Some("refs/heads/feature-a".into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
- fs.as_fake()
- .add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt/rosewood"),
- ref_name: Some("refs/heads/rosewood".into()),
- sha: "abc".into(),
- is_main: false,
- },
- )
- .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- project
- .update(cx, |project, cx| project.git_scans_complete(cx))
- .await;
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
+ let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- let worktree_project = project::Project::test(fs.clone(), ["/wt/rosewood".as_ref()], cx).await;
- worktree_project
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+ worktree_project
.update(cx, |p, cx| p.git_scans_complete(cx))
.await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- save_named_thread_metadata("main-t", "Unrelated Thread", &project, cx).await;
- save_named_thread_metadata("wt-t", "Fix Bug", &worktree_project, cx).await;
-
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
- cx.run_until_parked();
-
- // Search for "rosewood" — should match the worktree name, not the title.
- type_in_search(&sidebar, "rosewood", cx);
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " Fix Bug {rosewood} <== selected",
- ],
- );
-}
+ let sidebar = setup_sidebar(&multi_workspace, cx);
-#[gpui::test]
-async fn test_git_worktree_added_live_updates_sidebar(cx: &mut TestAppContext) {
- let (project, fs) = init_test_project_with_git("/project", cx).await;
+ let worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(worktree_project.clone(), window, cx)
+ });
- project
- .update(cx, |project, cx| project.git_scans_complete(cx))
- .await;
+ let worktree_panel = add_agent_panel(&worktree_workspace, cx);
- let worktree_project = project::Project::test(fs.clone(), ["/wt/rosewood".as_ref()], cx).await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ let workspace = mw.workspaces().next().unwrap().clone();
+ mw.activate(workspace, window, cx);
+ });
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ let connection = StubAgentConnection::new();
+ open_thread_with_connection(&worktree_panel, connection.clone(), cx);
+ send_message(&worktree_panel, cx);
- // Save a thread against a worktree path with the correct main
- // worktree association (as if the git state had been resolved).
- save_thread_metadata_with_main_paths(
- "wt-thread",
- "Worktree Thread",
- PathList::new(&[PathBuf::from("/wt/rosewood")]),
- PathList::new(&[PathBuf::from("/project")]),
- cx,
- );
+ let session_id = active_session_id(&worktree_panel, cx);
+ save_test_thread_metadata(&session_id, &worktree_project, cx).await;
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ cx.update(|_, cx| {
+ connection.send_update(
+ session_id.clone(),
+ acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("working...".into())),
+ cx,
+ );
+ });
cx.run_until_parked();
- // Thread is visible because its main_worktree_paths match the group.
- // The chip name is derived from the path even before git discovery.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec!["v [project]", " Worktree Thread {rosewood}"]
+ vec!["v [project]", " Hello {wt-feature-a} * (running)",]
);
- // Now add the worktree to the git state and trigger a rescan.
- fs.as_fake()
- .add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- true,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt/rosewood"),
- ref_name: Some("refs/heads/rosewood".into()),
- sha: "abc".into(),
- is_main: false,
- },
- )
- .await;
-
+ connection.end_turn(session_id, acp::StopReason::EndTurn);
cx.run_until_parked();
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " Worktree Thread {rosewood}",
- ]
+ vec!["v [project]", " Hello {wt-feature-a} * (!)",]
);
}
#[gpui::test]
-async fn test_two_worktree_workspaces_absorbed_when_main_added(cx: &mut TestAppContext) {
+async fn test_clicking_worktree_thread_opens_workspace_when_none_exists(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
- // Create the main repo directory (not opened as a workspace yet).
fs.insert_tree(
"/project",
serde_json::json!({
- ".git": {
- },
+ ".git": {},
"src": {},
}),
)
.await;
- // Two worktree checkouts whose .git files point back to the main repo.
fs.add_linked_worktree_for_repo(
Path::new("/project/.git"),
false,
@@ -3835,83 +4240,82 @@ async fn test_two_worktree_workspaces_absorbed_when_main_added(cx: &mut TestAppC
},
)
.await;
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-b"),
- ref_name: Some("refs/heads/feature-b".into()),
- sha: "bbb".into(),
- is_main: false,
- },
- )
- .await;
cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- let project_a = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- let project_b = project::Project::test(fs.clone(), ["/wt-feature-b".as_ref()], cx).await;
+ // Only open the main repo — no workspace for the worktree.
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
- project_a.update(cx, |p, cx| p.git_scans_complete(cx)).await;
- project_b.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
- // Open both worktrees as workspaces — no main repo yet.
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
- multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx);
- });
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
let sidebar = setup_sidebar(&multi_workspace, cx);
- save_named_thread_metadata("thread-a", "Thread A", &project_a, cx).await;
- save_named_thread_metadata("thread-b", "Thread B", &project_b, cx).await;
+ // Save a thread for the worktree path (no workspace for it).
+ save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- // Without the main repo, each worktree has its own header.
+ // Thread should appear under the main repo with a worktree chip.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
vec![
//
"v [project]",
- " Thread A {wt-feature-a}",
- " Thread B {wt-feature-b}",
- ]
+ " WT Thread {wt-feature-a}",
+ ],
);
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
+ // Only 1 workspace should exist.
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 1,
+ );
- multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(main_project.clone(), window, cx);
+ // Focus the sidebar and select the worktree thread.
+ focus_sidebar(&sidebar, cx);
+ sidebar.update_in(cx, |sidebar, _window, _cx| {
+ sidebar.selection = Some(1); // index 0 is header, 1 is the thread
});
+
+ // Confirm to open the worktree thread.
+ cx.dispatch_action(Confirm);
cx.run_until_parked();
- // Both worktree workspaces should now be absorbed under the main
- // repo header, with worktree chips.
+ // A new workspace should have been created for the worktree path.
+ let new_workspace = multi_workspace.read_with(cx, |mw, _| {
+ assert_eq!(
+ mw.workspaces().count(),
+ 2,
+ "confirming a worktree thread without a workspace should open one",
+ );
+ mw.workspaces().nth(1).unwrap().clone()
+ });
+
+ let new_path_list =
+ new_workspace.read_with(cx, |_, cx| workspace_path_list(&new_workspace, cx));
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " Thread A {wt-feature-a}",
- " Thread B {wt-feature-b}",
- ]
+ new_path_list,
+ PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]),
+ "the new workspace should have been opened for the worktree path",
);
}
#[gpui::test]
-async fn test_threadless_workspace_shows_new_thread_with_worktree_chip(cx: &mut TestAppContext) {
- // When a group has two workspaces — one with threads and one
- // without — the threadless workspace should appear as a
- // "New Thread" button with its worktree chip.
+async fn test_clicking_worktree_thread_does_not_briefly_render_as_separate_project(
+ cx: &mut TestAppContext,
+) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
- // Main repo with two linked worktrees.
fs.insert_tree(
"/project",
serde_json::json!({
@@ -3920,6 +4324,7 @@ async fn test_threadless_workspace_shows_new_thread_with_worktree_chip(cx: &mut
}),
)
.await;
+
fs.add_linked_worktree_for_repo(
Path::new("/project/.git"),
false,
@@ -3931,218 +4336,128 @@ async fn test_threadless_workspace_shows_new_thread_with_worktree_chip(cx: &mut
},
)
.await;
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-b"),
- ref_name: Some("refs/heads/feature-b".into()),
- sha: "bbb".into(),
- is_main: false,
- },
- )
- .await;
cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- // Workspace A: worktree feature-a (has threads).
- let project_a = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- project_a.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
- // Workspace B: worktree feature-b (no threads).
- let project_b = project::Project::test(fs.clone(), ["/wt-feature-b".as_ref()], cx).await;
- project_b.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
- multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx);
- });
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
let sidebar = setup_sidebar(&multi_workspace, cx);
- // Only save a thread for workspace A.
- save_named_thread_metadata("thread-a", "Thread A", &project_a, cx).await;
+ save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- // Workspace A's thread appears normally. Workspace B (threadless)
- // appears as a "New Thread" button with its worktree chip.
assert_eq!(
visible_entries_as_strings(&sidebar, cx),
- vec!["v [project]", " Thread A {wt-feature-a}",]
+ vec![
+ //
+ "v [project]",
+ " WT Thread {wt-feature-a}",
+ ],
);
-}
-#[gpui::test]
-async fn test_multi_worktree_thread_shows_multiple_chips(cx: &mut TestAppContext) {
- // A thread created in a workspace with roots from different git
- // worktrees should show a chip for each distinct worktree name.
- init_test(cx);
- let fs = FakeFs::new(cx.executor());
+ focus_sidebar(&sidebar, cx);
+ sidebar.update_in(cx, |sidebar, _window, _cx| {
+ sidebar.selection = Some(1); // index 0 is header, 1 is the thread
+ });
- // Two main repos.
- fs.insert_tree(
- "/project_a",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
- fs.insert_tree(
- "/project_b",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
+ let assert_sidebar_state = |sidebar: &mut Sidebar, _cx: &mut Context<Sidebar>| {
+ let mut project_headers = sidebar.contents.entries.iter().filter_map(|entry| {
+ if let ListEntry::ProjectHeader { label, .. } = entry {
+ Some(label.as_ref())
+ } else {
+ None
+ }
+ });
- // Worktree checkouts.
- for repo in &["project_a", "project_b"] {
- let git_path = format!("/{repo}/.git");
- for branch in &["olivetti", "selectric"] {
- fs.add_linked_worktree_for_repo(
- Path::new(&git_path),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from(format!("/worktrees/{repo}/{branch}/{repo}")),
- ref_name: Some(format!("refs/heads/{branch}").into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
+ let Some(project_header) = project_headers.next() else {
+ panic!("expected exactly one sidebar project header named `project`, found none");
+ };
+ assert_eq!(
+ project_header, "project",
+ "expected the only sidebar project header to be `project`"
+ );
+ if let Some(unexpected_header) = project_headers.next() {
+ panic!(
+ "expected exactly one sidebar project header named `project`, found extra header `{unexpected_header}`"
+ );
}
- }
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+ let mut saw_expected_thread = false;
+ for entry in &sidebar.contents.entries {
+ match entry {
+ ListEntry::ProjectHeader { label, .. } => {
+ assert_eq!(
+ label.as_ref(),
+ "project",
+ "expected the only sidebar project header to be `project`"
+ );
+ }
+ ListEntry::Thread(thread)
+ if thread.metadata.title.as_ref().map(|t| t.as_ref()) == Some("WT Thread")
+ && thread.worktrees.first().map(|wt| wt.name.as_ref())
+ == Some("wt-feature-a") =>
+ {
+ saw_expected_thread = true;
+ }
+ ListEntry::Thread(thread) if thread.is_draft => {}
+ ListEntry::Thread(thread) => {
+ let title = thread.metadata.display_title();
+ let worktree_name = thread
+ .worktrees
+ .first()
+ .map(|wt| wt.name.as_ref())
+ .unwrap_or("<none>");
+ panic!(
+ "unexpected sidebar thread while opening linked worktree thread: title=`{}`, worktree=`{}`",
+ title, worktree_name
+ );
+ }
+ ListEntry::ViewMore { .. } => {
+ panic!("unexpected `View More` entry while opening linked worktree thread");
+ }
+ }
+ }
- // Open a workspace with the worktree checkout paths as roots
- // (this is the workspace the thread was created in).
- let project = project::Project::test(
- fs.clone(),
- [
- "/worktrees/project_a/olivetti/project_a".as_ref(),
- "/worktrees/project_b/selectric/project_b".as_ref(),
- ],
- cx,
- )
- .await;
- project.update(cx, |p, cx| p.git_scans_complete(cx)).await;
+ assert!(
+ saw_expected_thread,
+ "expected the sidebar to keep showing `WT Thread {{wt-feature-a}}` under `project`"
+ );
+ };
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
+ sidebar
+ .update(cx, |_, cx| cx.observe_self(assert_sidebar_state))
+ .detach();
- // Save a thread under the same paths as the workspace roots.
- save_named_thread_metadata("wt-thread", "Cross Worktree Thread", &project, cx).await;
+ let window = cx.windows()[0];
+ cx.update_window(window, |_, window, cx| {
+ window.dispatch_action(Confirm.boxed_clone(), cx);
+ })
+ .unwrap();
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- // Should show two distinct worktree chips.
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project_a, project_b]",
- " Cross Worktree Thread {project_a:olivetti}, {project_b:selectric}",
- ]
- );
+ sidebar.update(cx, assert_sidebar_state);
}
#[gpui::test]
-async fn test_same_named_worktree_chips_are_deduplicated(cx: &mut TestAppContext) {
- // When a thread's roots span multiple repos but share the same
- // worktree name (e.g. both in "olivetti"), only one chip should
- // appear.
+async fn test_clicking_absorbed_worktree_thread_activates_worktree_workspace(
+ cx: &mut TestAppContext,
+) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
- fs.insert_tree(
- "/project_a",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
- fs.insert_tree(
- "/project_b",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
-
- for repo in &["project_a", "project_b"] {
- let git_path = format!("/{repo}/.git");
- fs.add_linked_worktree_for_repo(
- Path::new(&git_path),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from(format!("/worktrees/{repo}/olivetti/{repo}")),
- ref_name: Some("refs/heads/olivetti".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
- }
-
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let project = project::Project::test(
- fs.clone(),
- [
- "/worktrees/project_a/olivetti/project_a".as_ref(),
- "/worktrees/project_b/olivetti/project_b".as_ref(),
- ],
- cx,
- )
- .await;
- project.update(cx, |p, cx| p.git_scans_complete(cx)).await;
-
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- // Thread with roots in both repos' "olivetti" worktrees.
- save_named_thread_metadata("wt-thread", "Same Branch Thread", &project, cx).await;
-
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
- cx.run_until_parked();
-
- // Both worktree paths have the name "olivetti", so only one chip.
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project_a, project_b]",
- " Same Branch Thread {olivetti}",
- ]
- );
-}
-
-#[gpui::test]
-async fn test_absorbed_worktree_running_thread_shows_live_status(cx: &mut TestAppContext) {
- // When a worktree workspace is absorbed under the main repo, a
- // running thread in the worktree's agent panel should still show
- // live status (spinner + "(running)") in the sidebar.
- agent_ui::test_support::init_test(cx);
- cx.update(|cx| {
- ThreadStore::init_global(cx);
- ThreadMetadataStore::init_global(cx);
- language_model::LanguageModelRegistry::test(cx);
- prompt_store::init(cx);
- });
-
- let fs = FakeFs::new(cx.executor());
-
- // Main repo with a linked worktree.
fs.insert_tree(
"/project",
serde_json::json!({
@@ -4152,7 +4467,6 @@ async fn test_absorbed_worktree_running_thread_shows_live_status(cx: &mut TestAp
)
.await;
- // Worktree checkout pointing back to the main repo.
fs.add_linked_worktree_for_repo(
Path::new("/project/.git"),
false,
@@ -4177,7 +4491,6 @@ async fn test_absorbed_worktree_running_thread_shows_live_status(cx: &mut TestAp
.update(cx, |p, cx| p.git_scans_complete(cx))
.await;
- // Create the MultiWorkspace with both projects.
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
@@ -4187,477 +4500,318 @@ async fn test_absorbed_worktree_running_thread_shows_live_status(cx: &mut TestAp
mw.test_add_workspace(worktree_project.clone(), window, cx)
});
- // Add an agent panel to the worktree workspace so we can run a
- // thread inside it.
- let worktree_panel = add_agent_panel(&worktree_workspace, cx);
-
- // Switch back to the main workspace before setting up the sidebar.
- multi_workspace.update_in(cx, |mw, window, cx| {
+ // Activate the main workspace before setting up the sidebar.
+ let main_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
let workspace = mw.workspaces().next().unwrap().clone();
- mw.activate(workspace, window, cx);
+ mw.activate(workspace.clone(), window, cx);
+ workspace
});
- // Start a thread in the worktree workspace's panel and keep it
- // generating (don't resolve it).
- let connection = StubAgentConnection::new();
- open_thread_with_connection(&worktree_panel, connection.clone(), cx);
- send_message(&worktree_panel, cx);
-
- let session_id = active_session_id(&worktree_panel, cx);
-
- // Save metadata so the sidebar knows about this thread.
- save_test_thread_metadata(&session_id, &worktree_project, cx).await;
+ save_named_thread_metadata("thread-main", "Main Thread", &main_project, cx).await;
+ save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
- // Keep the thread generating by sending a chunk without ending
- // the turn.
- cx.update(|_, cx| {
- connection.send_update(
- session_id.clone(),
- acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("working...".into())),
- cx,
- );
- });
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
cx.run_until_parked();
- // The worktree thread should be absorbed under the main project
- // and show live running status.
+ // The worktree workspace should be absorbed under the main repo.
let entries = visible_entries_as_strings(&sidebar, cx);
+ assert_eq!(entries.len(), 3);
+ assert_eq!(entries[0], "v [project]");
+ assert!(entries.contains(&" Main Thread".to_string()));
+ assert!(entries.contains(&" WT Thread {wt-feature-a}".to_string()));
+
+ let wt_thread_index = entries
+ .iter()
+ .position(|e| e.contains("WT Thread"))
+ .expect("should find the worktree thread entry");
+
assert_eq!(
- entries,
- vec!["v [project]", " Hello {wt-feature-a} * (running)",]
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ main_workspace,
+ "main workspace should be active initially"
);
-}
-#[gpui::test]
-async fn test_absorbed_worktree_completion_triggers_notification(cx: &mut TestAppContext) {
- agent_ui::test_support::init_test(cx);
- cx.update(|cx| {
- ThreadStore::init_global(cx);
- ThreadMetadataStore::init_global(cx);
- language_model::LanguageModelRegistry::test(cx);
- prompt_store::init(cx);
+ // Focus the sidebar and select the absorbed worktree thread.
+ focus_sidebar(&sidebar, cx);
+ sidebar.update_in(cx, |sidebar, _window, _cx| {
+ sidebar.selection = Some(wt_thread_index);
});
- let fs = FakeFs::new(cx.executor());
-
- fs.insert_tree(
- "/project",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
-
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-a"),
- ref_name: Some("refs/heads/feature-a".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
-
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+ // Confirm to activate the worktree thread.
+ cx.dispatch_action(Confirm);
+ cx.run_until_parked();
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
+ // The worktree workspace should now be active, not the main one.
+ let active_workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+ assert_eq!(
+ active_workspace, worktree_workspace,
+ "clicking an absorbed worktree thread should activate the worktree workspace"
+ );
+}
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+#[gpui::test]
+async fn test_activate_archived_thread_with_saved_paths_activates_matching_workspace(
+ cx: &mut TestAppContext,
+) {
+ // Thread has saved metadata in ThreadStore. A matching workspace is
+ // already open. Expected: activates the matching workspace.
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
.await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
.await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
let sidebar = setup_sidebar(&multi_workspace, cx);
- let worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(worktree_project.clone(), window, cx)
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
});
+ let workspace_a =
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
- let worktree_panel = add_agent_panel(&worktree_workspace, cx);
+ // Save a thread with path_list pointing to project-b.
+ let session_id = acp::SessionId::new(Arc::from("archived-1"));
+ save_test_thread_metadata(&session_id, &project_b, cx).await;
+ // Ensure workspace A is active.
multi_workspace.update_in(cx, |mw, window, cx| {
let workspace = mw.workspaces().next().unwrap().clone();
mw.activate(workspace, window, cx);
});
-
- let connection = StubAgentConnection::new();
- open_thread_with_connection(&worktree_panel, connection.clone(), cx);
- send_message(&worktree_panel, cx);
-
- let session_id = active_session_id(&worktree_panel, cx);
- save_test_thread_metadata(&session_id, &worktree_project, cx).await;
-
- cx.update(|_, cx| {
- connection.send_update(
- session_id.clone(),
- acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("working...".into())),
- cx,
- );
- });
cx.run_until_parked();
-
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec!["v [project]", " Hello {wt-feature-a} * (running)",]
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_a
);
- connection.end_turn(session_id, acp::StopReason::EndTurn);
+ // Call activate_archived_thread – should resolve saved paths and
+ // switch to the workspace for project-b.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(
+ ThreadMetadata {
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Archived Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[PathBuf::from(
+ "/project-b",
+ )])),
+ archived: false,
+ remote_connection: None,
+ },
+ window,
+ cx,
+ );
+ });
cx.run_until_parked();
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec!["v [project]", " Hello {wt-feature-a} * (!)",]
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_b,
+ "should have switched to the workspace matching the saved paths"
);
}
#[gpui::test]
-async fn test_clicking_worktree_thread_opens_workspace_when_none_exists(cx: &mut TestAppContext) {
+async fn test_activate_archived_thread_cwd_fallback_with_matching_workspace(
+ cx: &mut TestAppContext,
+) {
+ // Thread has no saved metadata but session_info has cwd. A matching
+ // workspace is open. Expected: uses cwd to find and activate it.
init_test(cx);
let fs = FakeFs::new(cx.executor());
-
- fs.insert_tree(
- "/project",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
-
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-a"),
- ref_name: Some("refs/heads/feature-a".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
-
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- // Only open the main repo — no workspace for the worktree.
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
.await;
-
- let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
.await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
+
let sidebar = setup_sidebar(&multi_workspace, cx);
- // Save a thread for the worktree path (no workspace for it).
- save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b, window, cx)
+ });
+ let workspace_a =
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ // Start with workspace A active.
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ let workspace = mw.workspaces().next().unwrap().clone();
+ mw.activate(workspace, window, cx);
+ });
cx.run_until_parked();
-
- // Thread should appear under the main repo with a worktree chip.
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " WT Thread {wt-feature-a}",
- ],
- );
-
- // Only 1 workspace should exist.
assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
- 1,
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_a
);
- // Focus the sidebar and select the worktree thread.
- focus_sidebar(&sidebar, cx);
- sidebar.update_in(cx, |sidebar, _window, _cx| {
- sidebar.selection = Some(1); // index 0 is header, 1 is the thread
- });
-
- // Confirm to open the worktree thread.
- cx.dispatch_action(Confirm);
- cx.run_until_parked();
-
- // A new workspace should have been created for the worktree path.
- let new_workspace = multi_workspace.read_with(cx, |mw, _| {
- assert_eq!(
- mw.workspaces().count(),
- 2,
- "confirming a worktree thread without a workspace should open one",
+ // No thread saved to the store – cwd is the only path hint.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(
+ ThreadMetadata {
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("unknown-session"))),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("CWD Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[
+ std::path::PathBuf::from("/project-b"),
+ ])),
+ archived: false,
+ remote_connection: None,
+ },
+ window,
+ cx,
);
- mw.workspaces().nth(1).unwrap().clone()
});
+ cx.run_until_parked();
- let new_path_list =
- new_workspace.read_with(cx, |_, cx| workspace_path_list(&new_workspace, cx));
assert_eq!(
- new_path_list,
- PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]),
- "the new workspace should have been opened for the worktree path",
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_b,
+ "should have activated the workspace matching the cwd"
);
}
#[gpui::test]
-async fn test_clicking_worktree_thread_does_not_briefly_render_as_separate_project(
+async fn test_activate_archived_thread_no_paths_no_cwd_uses_active_workspace(
cx: &mut TestAppContext,
) {
+ // Thread has no saved metadata and no cwd. Expected: falls back to
+ // the currently active workspace.
init_test(cx);
let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- fs.insert_tree(
- "/project",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
-
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-a"),
- ref_name: Some("refs/heads/feature-a".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
-
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
-
- let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
- .await;
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
+
let sidebar = setup_sidebar(&multi_workspace, cx);
- save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b, window, cx)
+ });
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ // Activate workspace B (index 1) to make it the active one.
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ let workspace = mw.workspaces().nth(1).unwrap().clone();
+ mw.activate(workspace, window, cx);
+ });
cx.run_until_parked();
-
assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec![
- //
- "v [project]",
- " WT Thread {wt-feature-a}",
- ],
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_b
);
- focus_sidebar(&sidebar, cx);
- sidebar.update_in(cx, |sidebar, _window, _cx| {
- sidebar.selection = Some(1); // index 0 is header, 1 is the thread
- });
-
- let assert_sidebar_state = |sidebar: &mut Sidebar, _cx: &mut Context<Sidebar>| {
- let mut project_headers = sidebar.contents.entries.iter().filter_map(|entry| {
- if let ListEntry::ProjectHeader { label, .. } = entry {
- Some(label.as_ref())
- } else {
- None
- }
- });
-
- let Some(project_header) = project_headers.next() else {
- panic!("expected exactly one sidebar project header named `project`, found none");
- };
- assert_eq!(
- project_header, "project",
- "expected the only sidebar project header to be `project`"
- );
- if let Some(unexpected_header) = project_headers.next() {
- panic!(
- "expected exactly one sidebar project header named `project`, found extra header `{unexpected_header}`"
- );
- }
-
- let mut saw_expected_thread = false;
- for entry in &sidebar.contents.entries {
- match entry {
- ListEntry::ProjectHeader { label, .. } => {
- assert_eq!(
- label.as_ref(),
- "project",
- "expected the only sidebar project header to be `project`"
- );
- }
- ListEntry::Thread(thread)
- if thread.metadata.title.as_ref() == "WT Thread"
- && thread.worktrees.first().map(|wt| wt.name.as_ref())
- == Some("wt-feature-a") =>
- {
- saw_expected_thread = true;
- }
- ListEntry::Thread(thread) => {
- let title = thread.metadata.title.as_ref();
- let worktree_name = thread
- .worktrees
- .first()
- .map(|wt| wt.name.as_ref())
- .unwrap_or("<none>");
- panic!(
- "unexpected sidebar thread while opening linked worktree thread: title=`{title}`, worktree=`{worktree_name}`"
- );
- }
- ListEntry::ViewMore { .. } => {
- panic!("unexpected `View More` entry while opening linked worktree thread");
- }
- ListEntry::DraftThread { .. } => {}
- }
- }
-
- assert!(
- saw_expected_thread,
- "expected the sidebar to keep showing `WT Thread {{wt-feature-a}}` under `project`"
+ // No saved thread, no cwd – should fall back to the active workspace.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(
+ ThreadMetadata {
+ thread_id: ThreadId::new(),
+ session_id: Some(acp::SessionId::new(Arc::from("no-context-session"))),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Contextless Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::default(),
+ archived: false,
+ remote_connection: None,
+ },
+ window,
+ cx,
);
- };
-
- sidebar
- .update(cx, |_, cx| cx.observe_self(assert_sidebar_state))
- .detach();
-
- let window = cx.windows()[0];
- cx.update_window(window, |_, window, cx| {
- window.dispatch_action(Confirm.boxed_clone(), cx);
- })
- .unwrap();
-
+ });
cx.run_until_parked();
- sidebar.update(cx, assert_sidebar_state);
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
+ workspace_b,
+ "should have stayed on the active workspace when no path info is available"
+ );
}
#[gpui::test]
-async fn test_clicking_absorbed_worktree_thread_activates_worktree_workspace(
- cx: &mut TestAppContext,
-) {
+async fn test_activate_archived_thread_saved_paths_opens_new_workspace(cx: &mut TestAppContext) {
+ // Thread has saved metadata pointing to a path with no open workspace.
+ // Expected: opens a new workspace for that path.
init_test(cx);
let fs = FakeFs::new(cx.executor());
-
- fs.insert_tree(
- "/project",
- serde_json::json!({
- ".git": {},
- "src": {},
- }),
- )
- .await;
-
- fs.add_linked_worktree_for_repo(
- Path::new("/project/.git"),
- false,
- git::repository::Worktree {
- path: std::path::PathBuf::from("/wt-feature-a"),
- ref_name: Some("refs/heads/feature-a".into()),
- sha: "aaa".into(),
- is_main: false,
- },
- )
- .await;
-
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
- let worktree_project = project::Project::test(fs.clone(), ["/wt-feature-a".as_ref()], cx).await;
-
- main_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
.await;
- worktree_project
- .update(cx, |p, cx| p.git_scans_complete(cx))
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
.await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
let sidebar = setup_sidebar(&multi_workspace, cx);
- let worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(worktree_project.clone(), window, cx)
- });
-
- // Activate the main workspace before setting up the sidebar.
- let main_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
- let workspace = mw.workspaces().next().unwrap().clone();
- mw.activate(workspace.clone(), window, cx);
- workspace
- });
-
- save_named_thread_metadata("thread-main", "Main Thread", &main_project, cx).await;
- save_named_thread_metadata("thread-wt", "WT Thread", &worktree_project, cx).await;
-
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
- cx.run_until_parked();
-
- // The worktree workspace should be absorbed under the main repo.
- let entries = visible_entries_as_strings(&sidebar, cx);
- assert_eq!(entries.len(), 3);
- assert_eq!(entries[0], "v [project]");
- assert!(entries.contains(&" Main Thread".to_string()));
- assert!(entries.contains(&" WT Thread {wt-feature-a}".to_string()));
-
- let wt_thread_index = entries
- .iter()
- .position(|e| e.contains("WT Thread"))
- .expect("should find the worktree thread entry");
+ // Save a thread with path_list pointing to project-b – which has no
+ // open workspace.
+ let path_list_b = PathList::new(&[std::path::PathBuf::from("/project-b")]);
+ let session_id = acp::SessionId::new(Arc::from("archived-new-ws"));
assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- main_workspace,
- "main workspace should be active initially"
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 1,
+ "should start with one workspace"
);
- // Focus the sidebar and select the absorbed worktree thread.
- focus_sidebar(&sidebar, cx);
- sidebar.update_in(cx, |sidebar, _window, _cx| {
- sidebar.selection = Some(wt_thread_index);
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(
+ ThreadMetadata {
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("New WS Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&path_list_b),
+ archived: false,
+ remote_connection: None,
+ },
+ window,
+ cx,
+ );
});
-
- // Confirm to activate the worktree thread.
- cx.dispatch_action(Confirm);
cx.run_until_parked();
- // The worktree workspace should now be active, not the main one.
- let active_workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
assert_eq!(
- active_workspace, worktree_workspace,
- "clicking an absorbed worktree thread should activate the worktree workspace"
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 2,
+ "should have opened a second workspace for the archived thread's saved paths"
);
}
#[gpui::test]
-async fn test_activate_archived_thread_with_saved_paths_activates_matching_workspace(
- cx: &mut TestAppContext,
-) {
- // Thread has saved metadata in ThreadStore. A matching workspace is
- // already open. Expected: activates the matching workspace.
+async fn test_activate_archived_thread_reuses_workspace_in_another_window(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
@@ -4669,263 +4823,10 @@ async fn test_activate_archived_thread_with_saved_paths_activates_matching_works
let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
-
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx)
- });
- let workspace_a =
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
-
- // Save a thread with path_list pointing to project-b.
- let session_id = acp::SessionId::new(Arc::from("archived-1"));
- save_test_thread_metadata(&session_id, &project_b, cx).await;
-
- // Ensure workspace A is active.
- multi_workspace.update_in(cx, |mw, window, cx| {
- let workspace = mw.workspaces().next().unwrap().clone();
- mw.activate(workspace, window, cx);
- });
- cx.run_until_parked();
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_a
- );
-
- // Call activate_archived_thread – should resolve saved paths and
- // switch to the workspace for project-b.
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_archived_thread(
- ThreadMetadata {
- session_id: session_id.clone(),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Archived Thread".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- PathBuf::from("/project-b"),
- ])),
- archived: false,
- remote_connection: None,
- },
- window,
- cx,
- );
- });
- cx.run_until_parked();
-
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_b,
- "should have switched to the workspace matching the saved paths"
- );
-}
-
-#[gpui::test]
-async fn test_activate_archived_thread_cwd_fallback_with_matching_workspace(
- cx: &mut TestAppContext,
-) {
- // Thread has no saved metadata but session_info has cwd. A matching
- // workspace is open. Expected: uses cwd to find and activate it.
- init_test(cx);
- let fs = FakeFs::new(cx.executor());
- fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
- .await;
- fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
- .await;
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
- let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
-
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
-
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b, window, cx)
- });
- let workspace_a =
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
-
- // Start with workspace A active.
- multi_workspace.update_in(cx, |mw, window, cx| {
- let workspace = mw.workspaces().next().unwrap().clone();
- mw.activate(workspace, window, cx);
- });
- cx.run_until_parked();
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_a
- );
-
- // No thread saved to the store – cwd is the only path hint.
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_archived_thread(
- ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("unknown-session")),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "CWD Thread".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- std::path::PathBuf::from("/project-b"),
- ])),
- archived: false,
- remote_connection: None,
- },
- window,
- cx,
- );
- });
- cx.run_until_parked();
-
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_b,
- "should have activated the workspace matching the cwd"
- );
-}
-
-#[gpui::test]
-async fn test_activate_archived_thread_no_paths_no_cwd_uses_active_workspace(
- cx: &mut TestAppContext,
-) {
- // Thread has no saved metadata and no cwd. Expected: falls back to
- // the currently active workspace.
- init_test(cx);
- let fs = FakeFs::new(cx.executor());
- fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
- .await;
- fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
- .await;
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
- let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
-
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
-
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b, window, cx)
- });
-
- // Activate workspace B (index 1) to make it the active one.
- multi_workspace.update_in(cx, |mw, window, cx| {
- let workspace = mw.workspaces().nth(1).unwrap().clone();
- mw.activate(workspace, window, cx);
- });
- cx.run_until_parked();
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_b
- );
-
- // No saved thread, no cwd – should fall back to the active workspace.
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_archived_thread(
- ThreadMetadata {
- session_id: acp::SessionId::new(Arc::from("no-context-session")),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Contextless Thread".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::default(),
- archived: false,
- remote_connection: None,
- },
- window,
- cx,
- );
- });
- cx.run_until_parked();
-
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()),
- workspace_b,
- "should have stayed on the active workspace when no path info is available"
- );
-}
-
-#[gpui::test]
-async fn test_activate_archived_thread_saved_paths_opens_new_workspace(cx: &mut TestAppContext) {
- // Thread has saved metadata pointing to a path with no open workspace.
- // Expected: opens a new workspace for that path.
- init_test(cx);
- let fs = FakeFs::new(cx.executor());
- fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
- .await;
- fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
- .await;
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
-
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
-
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- // Save a thread with path_list pointing to project-b – which has no
- // open workspace.
- let path_list_b = PathList::new(&[std::path::PathBuf::from("/project-b")]);
- let session_id = acp::SessionId::new(Arc::from("archived-new-ws"));
-
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
- 1,
- "should start with one workspace"
- );
-
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.activate_archived_thread(
- ThreadMetadata {
- session_id: session_id.clone(),
- agent_id: agent::ZED_AGENT_ID.clone(),
- title: "New WS Thread".into(),
- updated_at: Utc::now(),
- created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&path_list_b),
- archived: false,
- remote_connection: None,
- },
- window,
- cx,
- );
- });
- cx.run_until_parked();
-
- assert_eq!(
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
- 2,
- "should have opened a second workspace for the archived thread's saved paths"
- );
-}
-
-#[gpui::test]
-async fn test_activate_archived_thread_reuses_workspace_in_another_window(cx: &mut TestAppContext) {
- init_test(cx);
- let fs = FakeFs::new(cx.executor());
- fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
- .await;
- fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
- .await;
- cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
-
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
- let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
-
- let multi_workspace_a =
- cx.add_window(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
- let multi_workspace_b =
- cx.add_window(|window, cx| MultiWorkspace::test_new(project_b, window, cx));
+ let multi_workspace_a =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
+ let multi_workspace_b =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_b, window, cx));
let multi_workspace_a_entity = multi_workspace_a.root(cx).unwrap();
let multi_workspace_b_entity = multi_workspace_b.root(cx).unwrap();
@@ -4941,14 +4842,15 @@ async fn test_activate_archived_thread_reuses_workspace_in_another_window(cx: &m
sidebar.update_in(cx_a, |sidebar, window, cx| {
sidebar.activate_archived_thread(
ThreadMetadata {
- session_id: session_id.clone(),
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id.clone()),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Cross Window Thread".into(),
+ title: Some("Cross Window Thread".into()),
updated_at: Utc::now(),
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- PathBuf::from("/project-b"),
- ])),
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[PathBuf::from(
+ "/project-b",
+ )])),
archived: false,
remote_connection: None,
},
@@ -4978,7 +4880,7 @@ async fn test_activate_archived_thread_reuses_workspace_in_another_window(cx: &m
);
sidebar.read_with(cx_a, |sidebar, _| {
assert!(
- !matches!(&sidebar.active_entry, Some(ActiveEntry::Thread { session_id: id, .. }) if id == &session_id),
+ !is_active_session(&sidebar, &session_id),
"source window's sidebar should not eagerly claim focus for a thread opened in another window"
);
});
@@ -5020,14 +4922,15 @@ async fn test_activate_archived_thread_reuses_workspace_in_another_window_with_t
sidebar_a.update_in(cx_a, |sidebar, window, cx| {
sidebar.activate_archived_thread(
ThreadMetadata {
- session_id: session_id.clone(),
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id.clone()),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Cross Window Thread".into(),
+ title: Some("Cross Window Thread".into()),
updated_at: Utc::now(),
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- PathBuf::from("/project-b"),
- ])),
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[PathBuf::from(
+ "/project-b",
+ )])),
archived: false,
remote_connection: None,
},
@@ -5057,7 +4960,7 @@ async fn test_activate_archived_thread_reuses_workspace_in_another_window_with_t
);
sidebar_a.read_with(cx_a, |sidebar, _| {
assert!(
- !matches!(&sidebar.active_entry, Some(ActiveEntry::Thread { session_id: id, .. }) if id == &session_id),
+ !is_active_session(&sidebar, &session_id),
"source window's sidebar should not eagerly claim focus for a thread opened in another window"
);
});
@@ -5102,14 +5005,15 @@ async fn test_activate_archived_thread_prefers_current_window_for_matching_paths
sidebar_a.update_in(cx_a, |sidebar, window, cx| {
sidebar.activate_archived_thread(
ThreadMetadata {
- session_id: session_id.clone(),
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id.clone()),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Current Window Thread".into(),
+ title: Some("Current Window Thread".into()),
updated_at: Utc::now(),
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- PathBuf::from("/project-a"),
- ])),
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[PathBuf::from(
+ "/project-a",
+ )])),
archived: false,
remote_connection: None,
},
@@ -5237,7 +5141,7 @@ async fn test_archive_thread_uses_next_threads_own_workspace(cx: &mut TestAppCon
// Save thread 2's metadata with a newer timestamp so it sorts above thread 1.
save_thread_metadata(
thread2_session_id.clone(),
- "Thread 2".into(),
+ Some("Thread 2".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&main_project,
@@ -5249,7 +5153,7 @@ async fn test_archive_thread_uses_next_threads_own_workspace(cx: &mut TestAppCon
let thread1_session_id = acp::SessionId::new(Arc::from("thread1-worktree-session"));
save_thread_metadata(
thread1_session_id,
- "Thread 1".into(),
+ Some("Thread 1".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&worktree_project,
@@ -5376,7 +5280,7 @@ async fn test_archive_last_worktree_thread_removes_workspace(cx: &mut TestAppCon
// Save a thread for the main project.
save_thread_metadata(
acp::SessionId::new(Arc::from("main-thread")),
- "Main Thread".into(),
+ Some("Main Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&main_project,
@@ -5387,7 +5291,7 @@ async fn test_archive_last_worktree_thread_removes_workspace(cx: &mut TestAppCon
let wt_thread_id = acp::SessionId::new(Arc::from("worktree-thread"));
save_thread_metadata(
wt_thread_id.clone(),
- "Worktree Thread".into(),
+ Some("Worktree Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&worktree_project,
@@ -5449,7 +5353,13 @@ async fn test_linked_worktree_threads_not_duplicated_across_groups(cx: &mut Test
// repo with a single-root workspace (e.g. [/project]), linked
// worktree threads from the shared repo should only appear under
// the dedicated group [project], not under [other, project].
- init_test(cx);
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
let fs = FakeFs::new(cx.executor());
// Two independent repos, each with their own git history.
@@ -5503,17 +5413,26 @@ async fn test_linked_worktree_threads_not_duplicated_across_groups(cx: &mut Test
.update(cx, |p, cx| p.git_scans_complete(cx))
.await;
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_only.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
- multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(multi_root.clone(), window, cx);
- });
-
- // Save a thread under the linked worktree path.
- save_named_thread_metadata("wt-thread", "Worktree Thread", &worktree_project, cx).await;
+ // Save a thread under the linked worktree path BEFORE setting up
+ // the sidebar and panels, so that reconciliation sees the [project]
+ // group as non-empty and doesn't create a spurious draft there.
+ let wt_session_id = acp::SessionId::new(Arc::from("wt-thread"));
+ save_thread_metadata(
+ wt_session_id,
+ Some("Worktree Thread".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ None,
+ &worktree_project,
+ cx,
+ );
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_only.clone(), window, cx));
+ let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+ let multi_root_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(multi_root.clone(), window, cx)
+ });
+ add_agent_panel(&multi_root_workspace, cx);
cx.run_until_parked();
// The thread should appear only under [project] (the dedicated
@@ -5580,7 +5499,7 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) {
let session_id_c = active_session_id(&panel, cx);
save_thread_metadata(
session_id_c.clone(),
- "Thread C".into(),
+ Some("Thread C".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap()),
&project,
@@ -5596,7 +5515,7 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) {
let session_id_b = active_session_id(&panel, cx);
save_thread_metadata(
session_id_b.clone(),
- "Thread B".into(),
+ Some("Thread B".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap()),
&project,
@@ -5612,7 +5531,7 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) {
let session_id_a = active_session_id(&panel, cx);
save_thread_metadata(
session_id_a.clone(),
- "Thread A".into(),
+ Some("Thread A".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 3, 0, 0, 0).unwrap(),
Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 3, 0, 0, 0).unwrap()),
&project,
@@ -5670,265 +5589,1102 @@ async fn test_thread_switcher_ordering(cx: &mut TestAppContext) {
.update(cx, |s, cx| s.cycle_selection(cx));
});
cx.run_until_parked();
- assert_eq!(switcher_selected_id(&sidebar, cx), session_id_c);
-
- assert!(sidebar.update(cx, |sidebar, _cx| sidebar.thread_last_accessed.is_empty()));
+ assert_eq!(switcher_selected_id(&sidebar, cx), session_id_c);
+
+ assert!(sidebar.update(cx, |sidebar, _cx| sidebar.thread_last_accessed.is_empty()));
+
+ // Confirm on Thread C.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ let switcher = sidebar.thread_switcher.as_ref().unwrap();
+ let focus = switcher.focus_handle(cx);
+ focus.dispatch_action(&menu::Confirm, window, cx);
+ });
+ cx.run_until_parked();
+
+ // Switcher should be dismissed after confirm.
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert!(
+ sidebar.thread_switcher.is_none(),
+ "switcher should be dismissed"
+ );
+ });
+
+ sidebar.update(cx, |sidebar, _cx| {
+ let last_accessed = sidebar
+ .thread_last_accessed
+ .keys()
+ .cloned()
+ .collect::<Vec<_>>();
+ assert_eq!(last_accessed.len(), 1);
+ assert!(last_accessed.contains(&session_id_c));
+ assert!(
+ is_active_session(&sidebar, &session_id_c),
+ "active_entry should be Thread({session_id_c:?})"
+ );
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ });
+ cx.run_until_parked();
+
+ assert_eq!(
+ switcher_ids(&sidebar, cx),
+ vec![
+ session_id_c.clone(),
+ session_id_a.clone(),
+ session_id_b.clone()
+ ],
+ );
+
+ // Confirm on Thread A.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ let switcher = sidebar.thread_switcher.as_ref().unwrap();
+ let focus = switcher.focus_handle(cx);
+ focus.dispatch_action(&menu::Confirm, window, cx);
+ });
+ cx.run_until_parked();
+
+ sidebar.update(cx, |sidebar, _cx| {
+ let last_accessed = sidebar
+ .thread_last_accessed
+ .keys()
+ .cloned()
+ .collect::<Vec<_>>();
+ assert_eq!(last_accessed.len(), 2);
+ assert!(last_accessed.contains(&session_id_c));
+ assert!(last_accessed.contains(&session_id_a));
+ assert!(
+ is_active_session(&sidebar, &session_id_a),
+ "active_entry should be Thread({session_id_a:?})"
+ );
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ });
+ cx.run_until_parked();
+
+ assert_eq!(
+ switcher_ids(&sidebar, cx),
+ vec![
+ session_id_a.clone(),
+ session_id_c.clone(),
+ session_id_b.clone(),
+ ],
+ );
+
+ sidebar.update_in(cx, |sidebar, _window, cx| {
+ let switcher = sidebar.thread_switcher.as_ref().unwrap();
+ switcher.update(cx, |switcher, cx| switcher.cycle_selection(cx));
+ });
+ cx.run_until_parked();
+
+ // Confirm on Thread B.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ let switcher = sidebar.thread_switcher.as_ref().unwrap();
+ let focus = switcher.focus_handle(cx);
+ focus.dispatch_action(&menu::Confirm, window, cx);
+ });
+ cx.run_until_parked();
+
+ sidebar.update(cx, |sidebar, _cx| {
+ let last_accessed = sidebar
+ .thread_last_accessed
+ .keys()
+ .cloned()
+ .collect::<Vec<_>>();
+ assert_eq!(last_accessed.len(), 3);
+ assert!(last_accessed.contains(&session_id_c));
+ assert!(last_accessed.contains(&session_id_a));
+ assert!(last_accessed.contains(&session_id_b));
+ assert!(
+ is_active_session(&sidebar, &session_id_b),
+ "active_entry should be Thread({session_id_b:?})"
+ );
+ });
+
+ // ── 3. Add a historical thread (no last_accessed_at, no message sent) ──
+ // This thread was never opened in a panel — it only exists in metadata.
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-historical")),
+ Some("Historical Thread".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap(),
+ Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap()),
+ &project,
+ cx,
+ );
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ });
+ cx.run_until_parked();
+
+ // Historical Thread has no last_accessed_at and no last_message_sent_or_queued,
+ // so it falls to tier 3 (sorted by created_at). It should appear after all
+ // accessed threads, even though its created_at (June 2024) is much later
+ // than the others.
+ //
+ // But the live threads (A, B, C) each had send_message called which sets
+ // last_message_sent_or_queued. So for the accessed threads (tier 1) the
+ // sort key is last_accessed_at; for Historical Thread (tier 3) it's created_at.
+ let session_id_hist = acp::SessionId::new(Arc::from("thread-historical"));
+
+ let ids = switcher_ids(&sidebar, cx);
+ assert_eq!(
+ ids,
+ vec![
+ session_id_b.clone(),
+ session_id_a.clone(),
+ session_id_c.clone(),
+ session_id_hist.clone()
+ ],
+ );
+
+ sidebar.update_in(cx, |sidebar, _window, cx| {
+ sidebar.dismiss_thread_switcher(cx);
+ });
+ cx.run_until_parked();
+
+ // ── 4. Add another historical thread with older created_at ─────────
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-old-historical")),
+ Some("Old Historical Thread".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2023, 6, 1, 0, 0, 0).unwrap(),
+ Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2023, 6, 1, 0, 0, 0).unwrap()),
+ &project,
+ cx,
+ );
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ });
+ cx.run_until_parked();
+
+ // Both historical threads have no access or message times. They should
+ // appear after accessed threads, sorted by created_at (newest first).
+ let session_id_old_hist = acp::SessionId::new(Arc::from("thread-old-historical"));
+ let ids = switcher_ids(&sidebar, cx);
+ assert_eq!(
+ ids,
+ vec![
+ session_id_b,
+ session_id_a,
+ session_id_c,
+ session_id_hist,
+ session_id_old_hist,
+ ],
+ );
+
+ sidebar.update_in(cx, |sidebar, _window, cx| {
+ sidebar.dismiss_thread_switcher(cx);
+ });
+ cx.run_until_parked();
+}
+
+#[gpui::test]
+async fn test_archive_thread_keeps_metadata_but_hides_from_sidebar(cx: &mut TestAppContext) {
+ let project = init_test_project("/my-project", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-to-archive")),
+ Some("Thread To Archive".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ None,
+ &project,
+ cx,
+ );
+ cx.run_until_parked();
+
+ multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
+ cx.run_until_parked();
+
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ assert!(
+ entries.iter().any(|e| e.contains("Thread To Archive")),
+ "expected thread to be visible before archiving, got: {entries:?}"
+ );
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(
+ &acp::SessionId::new(Arc::from("thread-to-archive")),
+ window,
+ cx,
+ );
+ });
+ cx.run_until_parked();
+
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ assert!(
+ !entries.iter().any(|e| e.contains("Thread To Archive")),
+ "expected thread to be hidden after archiving, got: {entries:?}"
+ );
+
+ cx.update(|_, cx| {
+ let store = ThreadMetadataStore::global(cx);
+ let archived: Vec<_> = store.read(cx).archived_entries().collect();
+ assert_eq!(archived.len(), 1);
+ assert_eq!(
+ archived[0].session_id.as_ref().unwrap().0.as_ref(),
+ "thread-to-archive"
+ );
+ assert!(archived[0].archived);
+ });
+}
+
+#[gpui::test]
+async fn test_archive_thread_active_entry_management(cx: &mut TestAppContext) {
+ // Tests two archive scenarios:
+ // 1. Archiving a thread in a non-active workspace leaves active_entry
+ // as the current draft.
+ // 2. Archiving the thread the user is looking at falls back to a draft
+ // on the same workspace.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let (sidebar, panel_a) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
+ });
+ let panel_b = add_agent_panel(&workspace_b, cx);
+ cx.run_until_parked();
+
+ // Explicitly create a draft on workspace_b so the sidebar tracks one.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.create_new_thread(&workspace_b, window, cx);
+ });
+ cx.run_until_parked();
+
+ // --- Scenario 1: archive a thread in the non-active workspace ---
+
+ // Create a thread in project-a (non-active — project-b is active).
+ let connection = acp_thread::StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ agent_ui::test_support::open_thread_with_connection(&panel_a, connection, cx);
+ agent_ui::test_support::send_message(&panel_a, cx);
+ let thread_a = agent_ui::test_support::active_session_id(&panel_a, cx);
+ cx.run_until_parked();
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&thread_a, window, cx);
+ });
+ cx.run_until_parked();
+
+ // active_entry should still be a draft on workspace_b (the active one).
+ sidebar.read_with(cx, |sidebar, _| {
+ assert!(
+ matches!(&sidebar.active_entry, Some(ActiveEntry { workspace: ws, .. }) if ws == &workspace_b),
+ "expected Draft(workspace_b) after archiving non-active thread, got: {:?}",
+ sidebar.active_entry,
+ );
+ });
+
+ // --- Scenario 2: archive the thread the user is looking at ---
+
+ // Create a thread in project-b (the active workspace) and verify it
+ // becomes the active entry.
+ let connection = acp_thread::StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ agent_ui::test_support::open_thread_with_connection(&panel_b, connection, cx);
+ agent_ui::test_support::send_message(&panel_b, cx);
+ let thread_b = agent_ui::test_support::active_session_id(&panel_b, cx);
+ cx.run_until_parked();
+
+ sidebar.read_with(cx, |sidebar, _| {
+ assert!(
+ is_active_session(&sidebar, &thread_b),
+ "expected active_entry to be Thread({thread_b}), got: {:?}",
+ sidebar.active_entry,
+ );
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&thread_b, window, cx);
+ });
+ cx.run_until_parked();
+
+ // Should fall back to a draft on the same workspace.
+ sidebar.read_with(cx, |sidebar, _| {
+ assert!(
+ matches!(&sidebar.active_entry, Some(ActiveEntry { workspace: ws, .. }) if ws == &workspace_b),
+ "expected Draft(workspace_b) after archiving active thread, got: {:?}",
+ sidebar.active_entry,
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_unarchive_only_shows_restored_thread(cx: &mut TestAppContext) {
+ // Full flow: create a thread, archive it (removing the workspace),
+ // then unarchive. Only the restored thread should appear — no
+ // leftover drafts or previously-serialized threads.
+ let project = init_test_project_with_agent_panel("/my-project", cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+ cx.run_until_parked();
+
+ // Create a thread and send a message so it's a real thread.
+ let connection = acp_thread::StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Hello".into()),
+ )]);
+ agent_ui::test_support::open_thread_with_connection(&panel, connection, cx);
+ agent_ui::test_support::send_message(&panel, cx);
+ let session_id = agent_ui::test_support::active_session_id(&panel, cx);
+ cx.run_until_parked();
+
+ // Archive it.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&session_id, window, cx);
+ });
+ cx.run_until_parked();
+
+ // Grab metadata for unarchive.
+ let thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("thread should exist")
+ });
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(thread_id)
+ .cloned()
+ .expect("metadata should exist")
+ });
+
+ // Unarchive it — the draft should be replaced by the restored thread.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(metadata, window, cx);
+ });
+ cx.run_until_parked();
+
+ // Only the unarchived thread should be visible — no drafts, no other threads.
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let thread_count = entries
+ .iter()
+ .filter(|e| !e.starts_with("v ") && !e.starts_with("> "))
+ .count();
+ assert_eq!(
+ thread_count, 1,
+ "expected exactly 1 thread entry (the restored one), got entries: {entries:?}"
+ );
+ assert!(
+ !entries.iter().any(|e| e.contains("Draft")),
+ "expected no drafts after restoring, got entries: {entries:?}"
+ );
+}
+
+#[gpui::test]
+async fn test_unarchive_first_thread_in_group_does_not_create_spurious_draft(
+ cx: &mut TestAppContext,
+) {
+ // When a thread is unarchived into a project group that has no open
+ // workspace, the sidebar opens a new workspace and loads the thread.
+ // No spurious draft should appear alongside the unarchived thread.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+ cx.run_until_parked();
+
+ // Save an archived thread whose folder_paths point to project-b,
+ // which has no open workspace.
+ let session_id = acp::SessionId::new(Arc::from("archived-thread"));
+ let path_list_b = PathList::new(&[std::path::PathBuf::from("/project-b")]);
+ let thread_id = ThreadId::new();
+ cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.save_manually(
+ ThreadMetadata {
+ thread_id,
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Unarchived Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&path_list_b),
+ archived: true,
+ remote_connection: None,
+ },
+ cx,
+ )
+ });
+ });
+ cx.run_until_parked();
+
+ // Verify no workspace for project-b exists yet.
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 1,
+ "should start with only the project-a workspace"
+ );
+
+ // Un-archive the thread — should open project-b workspace and load it.
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(thread_id)
+ .cloned()
+ .expect("metadata should exist")
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(metadata, window, cx);
+ });
+ cx.run_until_parked();
+
+ // A second workspace should have been created for project-b.
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 2,
+ "should have opened a workspace for the unarchived thread"
+ );
+
+ // The sidebar should show the unarchived thread without a spurious draft
+ // in the project-b group.
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
+ // project-a gets a draft (it's the active workspace with no threads),
+ // but project-b should NOT have one — only the unarchived thread.
+ assert!(
+ draft_count <= 1,
+ "expected at most one draft (for project-a), got entries: {entries:?}"
+ );
+ assert!(
+ entries.iter().any(|e| e.contains("Unarchived Thread")),
+ "expected unarchived thread to appear, got entries: {entries:?}"
+ );
+}
+
+#[gpui::test]
+async fn test_unarchive_into_new_workspace_does_not_create_duplicate_real_thread(
+ cx: &mut TestAppContext,
+) {
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+ cx.run_until_parked();
+
+ let session_id = acp::SessionId::new(Arc::from("restore-into-new-workspace"));
+ let path_list_b = PathList::new(&[PathBuf::from("/project-b")]);
+ let original_thread_id = ThreadId::new();
+ cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.save_manually(
+ ThreadMetadata {
+ thread_id: original_thread_id,
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Unarchived Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&path_list_b),
+ archived: true,
+ remote_connection: None,
+ },
+ cx,
+ )
+ });
+ });
+ cx.run_until_parked();
+
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(original_thread_id)
+ .cloned()
+ .expect("metadata should exist before unarchive")
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(metadata, window, cx);
+ });
+ cx.run_until_parked();
+ cx.run_until_parked();
+ cx.run_until_parked();
+
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 2,
+ "expected unarchive to open the target workspace"
+ );
+
+ let restored_workspace = multi_workspace.read_with(cx, |mw, cx| {
+ mw.workspaces()
+ .find(|workspace| PathList::new(&workspace.read(cx).root_paths(cx)) == path_list_b)
+ .cloned()
+ .expect("expected restored workspace for unarchived thread")
+ });
+ let restored_panel = restored_workspace.read_with(cx, |workspace, cx| {
+ workspace
+ .panel::<AgentPanel>(cx)
+ .expect("expected unarchive to install an agent panel in the new workspace")
+ });
+
+ let restored_thread_id = restored_panel.read_with(cx, |panel, cx| panel.active_thread_id(cx));
+ assert_eq!(
+ restored_thread_id,
+ Some(original_thread_id),
+ "expected the new workspace's agent panel to target the restored archived thread id"
+ );
+
+ let session_entries = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .filter(|entry| entry.session_id.as_ref() == Some(&session_id))
+ .cloned()
+ .collect::<Vec<_>>()
+ });
+ assert_eq!(
+ session_entries.len(),
+ 1,
+ "expected exactly one metadata row for restored session after opening a new workspace, got: {session_entries:?}"
+ );
+ assert_eq!(
+ session_entries[0].thread_id, original_thread_id,
+ "expected restore into a new workspace to reuse the original thread id"
+ );
+ assert!(
+ !session_entries[0].archived,
+ "expected restored thread metadata to be unarchived, got: {:?}",
+ session_entries[0]
+ );
+
+ let mapped_thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ });
+ assert_eq!(
+ mapped_thread_id,
+ Some(original_thread_id),
+ "expected session mapping to remain stable after opening the new workspace"
+ );
+
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let real_thread_rows = entries
+ .iter()
+ .filter(|entry| !entry.starts_with("v ") && !entry.starts_with("> "))
+ .filter(|entry| !entry.contains("Draft"))
+ .count();
+ assert_eq!(
+ real_thread_rows, 1,
+ "expected exactly one visible real thread row after restore into a new workspace, got entries: {entries:?}"
+ );
+ assert!(
+ entries
+ .iter()
+ .any(|entry| entry.contains("Unarchived Thread")),
+ "expected restored thread row to be visible, got entries: {entries:?}"
+ );
+}
+
+#[gpui::test]
+async fn test_unarchive_into_existing_workspace_replaces_draft(cx: &mut TestAppContext) {
+ // When a workspace already exists with an empty draft (from
+ // reconcile_groups) and a thread is unarchived into it, the draft
+ // should be replaced — not kept alongside the loaded thread.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/my-project", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project = project::Project::test(fs.clone(), ["/my-project".as_ref()], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
+ cx.run_until_parked();
+
+ // Create a thread and send a message so it's no longer a draft.
+ let connection = acp_thread::StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ agent_ui::test_support::open_thread_with_connection(&panel, connection, cx);
+ agent_ui::test_support::send_message(&panel, cx);
+ let session_id = agent_ui::test_support::active_session_id(&panel, cx);
+ cx.run_until_parked();
+
+ // Archive the thread — this creates a draft to replace it.
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&session_id, window, cx);
+ });
+ cx.run_until_parked();
+
+ // Verify the draft exists before unarchive.
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ assert!(
+ entries.iter().any(|e| e.contains("Draft")),
+ "expected a draft after archiving, got: {entries:?}"
+ );
+
+ // Un-archive the thread.
+ let thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("thread should exist in store")
+ });
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(thread_id)
+ .cloned()
+ .expect("metadata should exist")
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(metadata, window, cx);
+ });
+ cx.run_until_parked();
+
+ // The draft should be gone — only the unarchived thread remains.
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
+ assert_eq!(
+ draft_count, 0,
+ "expected no drafts after unarchiving, got entries: {entries:?}"
+ );
+}
+
+#[gpui::test]
+async fn test_pending_thread_activation_suppresses_reconcile_draft_creation(
+ cx: &mut TestAppContext,
+) {
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
+ });
+ let panel_b = add_agent_panel(&workspace_b, cx);
+ cx.run_until_parked();
+
+ let preexisting_empty_draft_ids = panel_b.read_with(cx, |panel, cx| {
+ panel
+ .draft_thread_ids(cx)
+ .into_iter()
+ .filter(|id| panel.editor_text(*id, cx).is_none())
+ .collect::<Vec<_>>()
+ });
+ if !preexisting_empty_draft_ids.is_empty() {
+ panel_b.update(cx, |panel, cx| {
+ for draft_id in &preexisting_empty_draft_ids {
+ panel.remove_thread(*draft_id, cx);
+ }
+ });
+ cx.run_until_parked();
+ }
+
+ let project_b_key = project_b.read_with(cx, |project, cx| project.project_group_key(cx));
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ assert!(
+ panel_b.read(cx).draft_thread_ids(cx).is_empty(),
+ "expected target panel to start without drafts after clearing setup state"
+ );
+
+ sidebar.pending_thread_activation = Some(ThreadId::new());
+ sidebar.reconcile_groups(window, cx);
+
+ assert!(
+ panel_b.read(cx).draft_thread_ids(cx).is_empty(),
+ "expected pending_thread_activation to suppress reconcile-driven fallback draft creation"
+ );
+
+ sidebar.pending_thread_activation = None;
+ sidebar.update_entries(cx);
+ sidebar.reconcile_groups(window, cx);
+
+ let created_draft_ids = panel_b.read(cx).draft_thread_ids(cx);
+ assert_eq!(
+ created_draft_ids.len(),
+ 1,
+ "expected reconcile_groups to create a fallback draft again once the activation guard is cleared for the empty group {project_b_key:?}"
+ );
+ assert!(
+ panel_b.read(cx).editor_text(created_draft_ids[0], cx).is_none(),
+ "expected the reconciled draft to be empty"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_unarchive_into_inactive_existing_workspace_does_not_leave_active_draft(
+ cx: &mut TestAppContext,
+) {
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ let workspace_a = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
+ });
+ let _panel_b = add_agent_panel(&workspace_b, cx);
+ cx.run_until_parked();
- // Confirm on Thread C.
- sidebar.update_in(cx, |sidebar, window, cx| {
- let switcher = sidebar.thread_switcher.as_ref().unwrap();
- let focus = switcher.focus_handle(cx);
- focus.dispatch_action(&menu::Confirm, window, cx);
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.activate(workspace_a.clone(), window, cx);
});
cx.run_until_parked();
- // Switcher should be dismissed after confirm.
- sidebar.read_with(cx, |sidebar, _cx| {
- assert!(
- sidebar.thread_switcher.is_none(),
- "switcher should be dismissed"
- );
+ let session_id = acp::SessionId::new(Arc::from("unarchive-into-inactive-existing-workspace"));
+ let thread_id = ThreadId::new();
+ cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.save_manually(
+ ThreadMetadata {
+ thread_id,
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Restored In Inactive Workspace".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[
+ PathBuf::from("/project-b"),
+ ])),
+ archived: true,
+ remote_connection: None,
+ },
+ cx,
+ )
+ });
});
+ cx.run_until_parked();
- sidebar.update(cx, |sidebar, _cx| {
- let last_accessed = sidebar
- .thread_last_accessed
- .keys()
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(thread_id)
.cloned()
- .collect::<Vec<_>>();
- assert_eq!(last_accessed.len(), 1);
- assert!(last_accessed.contains(&session_id_c));
- assert!(
- sidebar
- .active_entry
- .as_ref()
- .expect("active_entry should be set")
- .is_active_thread(&session_id_c)
- );
+ .expect("archived metadata should exist before restore")
});
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ sidebar.activate_archived_thread(metadata, window, cx);
});
- cx.run_until_parked();
-
- assert_eq!(
- switcher_ids(&sidebar, cx),
- vec![
- session_id_c.clone(),
- session_id_a.clone(),
- session_id_b.clone()
- ],
- );
- // Confirm on Thread A.
- sidebar.update_in(cx, |sidebar, window, cx| {
- let switcher = sidebar.thread_switcher.as_ref().unwrap();
- let focus = switcher.focus_handle(cx);
- focus.dispatch_action(&menu::Confirm, window, cx);
+ let panel_b_before_settle = workspace_b.read_with(cx, |workspace, cx| {
+ workspace.panel::<AgentPanel>(cx).expect(
+ "target workspace should still have an agent panel immediately after activation",
+ )
});
+ let immediate_active_thread_id =
+ panel_b_before_settle.read_with(cx, |panel, cx| panel.active_thread_id(cx));
+ let immediate_draft_ids =
+ panel_b_before_settle.read_with(cx, |panel, cx| panel.draft_thread_ids(cx));
+
+ cx.run_until_parked();
+ cx.run_until_parked();
cx.run_until_parked();
- sidebar.update(cx, |sidebar, _cx| {
- let last_accessed = sidebar
- .thread_last_accessed
- .keys()
- .cloned()
- .collect::<Vec<_>>();
- assert_eq!(last_accessed.len(), 2);
- assert!(last_accessed.contains(&session_id_c));
- assert!(last_accessed.contains(&session_id_a));
- assert!(
- sidebar
- .active_entry
- .as_ref()
- .expect("active_entry should be set")
- .is_active_thread(&session_id_a)
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert_active_thread(
+ sidebar,
+ &session_id,
+ "unarchiving into an inactive existing workspace should end on the restored thread",
);
});
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ let panel_b = workspace_b.read_with(cx, |workspace, cx| {
+ workspace
+ .panel::<AgentPanel>(cx)
+ .expect("target workspace should still have an agent panel")
});
- cx.run_until_parked();
+ assert_eq!(
+ panel_b.read_with(cx, |panel, cx| panel.active_thread_id(cx)),
+ Some(thread_id),
+ "expected target panel to activate the restored thread id"
+ );
+ assert!(
+ immediate_active_thread_id.is_none() || immediate_active_thread_id == Some(thread_id),
+ "expected immediate panel state to be either still loading or already on the restored thread, got active_thread_id={immediate_active_thread_id:?}, draft_ids={immediate_draft_ids:?}"
+ );
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let target_rows: Vec<_> = entries
+ .iter()
+ .filter(|entry| entry.contains("Restored In Inactive Workspace") || entry.contains("Draft"))
+ .cloned()
+ .collect();
assert_eq!(
- switcher_ids(&sidebar, cx),
- vec![
- session_id_a.clone(),
- session_id_c.clone(),
- session_id_b.clone(),
- ],
+ target_rows.len(),
+ 1,
+ "expected only the restored row and no surviving draft in the target group, got entries: {entries:?}"
+ );
+ assert!(
+ target_rows[0].contains("Restored In Inactive Workspace"),
+ "expected the remaining row to be the restored thread, got entries: {entries:?}"
+ );
+ assert!(
+ !target_rows[0].contains("Draft"),
+ "expected no surviving draft row after unarchive into inactive existing workspace, got entries: {entries:?}"
);
+}
- sidebar.update_in(cx, |sidebar, _window, cx| {
- let switcher = sidebar.thread_switcher.as_ref().unwrap();
- switcher.update(cx, |switcher, cx| switcher.cycle_selection(cx));
+#[gpui::test]
+async fn test_unarchive_after_removing_parent_project_group_restores_real_thread(
+ cx: &mut TestAppContext,
+) {
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
+ let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b.clone(), window, cx)
});
+ let panel_b = add_agent_panel(&workspace_b, cx);
+ cx.run_until_parked();
+
+ let connection = acp_thread::StubAgentConnection::new();
+ connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
+ acp::ContentChunk::new("Done".into()),
+ )]);
+ agent_ui::test_support::open_thread_with_connection(&panel_b, connection, cx);
+ agent_ui::test_support::send_message(&panel_b, cx);
+ let session_id = agent_ui::test_support::active_session_id(&panel_b, cx);
+ save_test_thread_metadata(&session_id, &project_b, cx).await;
cx.run_until_parked();
- // Confirm on Thread B.
sidebar.update_in(cx, |sidebar, window, cx| {
- let switcher = sidebar.thread_switcher.as_ref().unwrap();
- let focus = switcher.focus_handle(cx);
- focus.dispatch_action(&menu::Confirm, window, cx);
+ sidebar.archive_thread(&session_id, window, cx);
});
cx.run_until_parked();
+ cx.run_until_parked();
+ cx.run_until_parked();
- sidebar.update(cx, |sidebar, _cx| {
- let last_accessed = sidebar
- .thread_last_accessed
- .keys()
+ let archived_metadata = cx.update(|_, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ let thread_id = store
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("archived thread should still exist in metadata store");
+ let metadata = store
+ .entry(thread_id)
.cloned()
- .collect::<Vec<_>>();
- assert_eq!(last_accessed.len(), 3);
- assert!(last_accessed.contains(&session_id_c));
- assert!(last_accessed.contains(&session_id_a));
- assert!(last_accessed.contains(&session_id_b));
+ .expect("archived metadata should still exist after archive");
assert!(
- sidebar
- .active_entry
- .as_ref()
- .expect("active_entry should be set")
- .is_active_thread(&session_id_b)
+ metadata.archived,
+ "thread should be archived before project removal"
);
+ metadata
});
- // ── 3. Add a historical thread (no last_accessed_at, no message sent) ──
- // This thread was never opened in a panel — it only exists in metadata.
- save_thread_metadata(
- acp::SessionId::new(Arc::from("thread-historical")),
- "Historical Thread".into(),
- chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap(),
- Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap()),
- &project,
- cx,
- );
-
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ let group_key_b =
+ project_b.read_with(cx, |project, cx| ProjectGroupKey::from_project(project, cx));
+ let remove_task = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.remove_project_group(&group_key_b, window, cx)
});
+ remove_task
+ .await
+ .expect("remove project group task should complete");
cx.run_until_parked();
-
- // Historical Thread has no last_accessed_at and no last_message_sent_or_queued,
- // so it falls to tier 3 (sorted by created_at). It should appear after all
- // accessed threads, even though its created_at (June 2024) is much later
- // than the others.
- //
- // But the live threads (A, B, C) each had send_message called which sets
- // last_message_sent_or_queued. So for the accessed threads (tier 1) the
- // sort key is last_accessed_at; for Historical Thread (tier 3) it's created_at.
- let session_id_hist = acp::SessionId::new(Arc::from("thread-historical"));
-
- let ids = switcher_ids(&sidebar, cx);
- assert_eq!(
- ids,
- vec![
- session_id_b.clone(),
- session_id_a.clone(),
- session_id_c.clone(),
- session_id_hist.clone()
- ],
- );
-
- sidebar.update_in(cx, |sidebar, _window, cx| {
- sidebar.dismiss_thread_switcher(cx);
- });
cx.run_until_parked();
- // ── 4. Add another historical thread with older created_at ─────────
- save_thread_metadata(
- acp::SessionId::new(Arc::from("thread-old-historical")),
- "Old Historical Thread".into(),
- chrono::TimeZone::with_ymd_and_hms(&Utc, 2023, 6, 1, 0, 0, 0).unwrap(),
- Some(chrono::TimeZone::with_ymd_and_hms(&Utc, 2023, 6, 1, 0, 0, 0).unwrap()),
- &project,
- cx,
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 1,
+ "removing the archived thread's parent project group should remove its workspace"
);
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.on_toggle_thread_switcher(&ToggleThreadSwitcher::default(), window, cx);
+ sidebar.activate_archived_thread(archived_metadata.clone(), window, cx);
});
cx.run_until_parked();
-
- // Both historical threads have no access or message times. They should
- // appear after accessed threads, sorted by created_at (newest first).
- let session_id_old_hist = acp::SessionId::new(Arc::from("thread-old-historical"));
- let ids = switcher_ids(&sidebar, cx);
- assert_eq!(
- ids,
- vec![
- session_id_b,
- session_id_a,
- session_id_c,
- session_id_hist,
- session_id_old_hist,
- ],
- );
-
- sidebar.update_in(cx, |sidebar, _window, cx| {
- sidebar.dismiss_thread_switcher(cx);
- });
cx.run_until_parked();
-}
-
-#[gpui::test]
-async fn test_archive_thread_keeps_metadata_but_hides_from_sidebar(cx: &mut TestAppContext) {
- let project = init_test_project("/my-project", cx).await;
- let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
- let sidebar = setup_sidebar(&multi_workspace, cx);
-
- save_thread_metadata(
- acp::SessionId::new(Arc::from("thread-to-archive")),
- "Thread To Archive".into(),
- chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
- None,
- &project,
- cx,
- );
cx.run_until_parked();
- multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
- cx.run_until_parked();
+ let restored_workspace = multi_workspace.read_with(cx, |mw, cx| {
+ mw.workspaces()
+ .find(|workspace| {
+ PathList::new(&workspace.read(cx).root_paths(cx))
+ == PathList::new(&[PathBuf::from("/project-b")])
+ })
+ .cloned()
+ .expect("expected unarchive to recreate the removed project workspace")
+ });
+ let restored_panel = restored_workspace.read_with(cx, |workspace, cx| {
+ workspace
+ .panel::<AgentPanel>(cx)
+ .expect("expected restored workspace to bootstrap an agent panel")
+ });
- let entries = visible_entries_as_strings(&sidebar, cx);
- assert!(
- entries.iter().any(|e| e.contains("Thread To Archive")),
- "expected thread to be visible before archiving, got: {entries:?}"
+ let restored_thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("session should still map to restored thread id")
+ });
+ assert_eq!(
+ restored_panel.read_with(cx, |panel, cx| panel.active_thread_id(cx)),
+ Some(restored_thread_id),
+ "expected unarchive after project removal to activate the restored real thread"
);
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.archive_thread(
- &acp::SessionId::new(Arc::from("thread-to-archive")),
- window,
- cx,
+ sidebar.read_with(cx, |sidebar, _cx| {
+ assert_active_thread(
+ sidebar,
+ &session_id,
+ "expected sidebar active entry to track the restored thread after project removal",
);
});
- cx.run_until_parked();
let entries = visible_entries_as_strings(&sidebar, cx);
+ let restored_title = archived_metadata.display_title().to_string();
+ let matching_rows: Vec<_> = entries
+ .iter()
+ .filter(|entry| entry.contains(&restored_title) || entry.contains("Draft"))
+ .cloned()
+ .collect();
+ assert_eq!(
+ matching_rows.len(),
+ 1,
+ "expected only one restored row and no surviving draft after unarchive following project removal, got entries: {entries:?}"
+ );
assert!(
- !entries.iter().any(|e| e.contains("Thread To Archive")),
- "expected thread to be hidden after archiving, got: {entries:?}"
+ !matching_rows[0].contains("Draft"),
+ "expected no draft row after unarchive following project removal, got entries: {entries:?}"
);
-
- cx.update(|_, cx| {
- let store = ThreadMetadataStore::global(cx);
- let archived: Vec<_> = store.read(cx).archived_entries().collect();
- assert_eq!(archived.len(), 1);
- assert_eq!(archived[0].session_id.0.as_ref(), "thread-to-archive");
- assert!(archived[0].archived);
- });
}
#[gpui::test]
-async fn test_archive_thread_active_entry_management(cx: &mut TestAppContext) {
- // Tests two archive scenarios:
- // 1. Archiving a thread in a non-active workspace leaves active_entry
- // as the current draft.
- // 2. Archiving the thread the user is looking at falls back to a draft
- // on the same workspace.
+async fn test_unarchive_does_not_create_duplicate_real_thread_metadata(cx: &mut TestAppContext) {
agent_ui::test_support::init_test(cx);
cx.update(|cx| {
ThreadStore::init_global(cx);
@@ -5938,91 +6694,90 @@ async fn test_archive_thread_active_entry_management(cx: &mut TestAppContext) {
});
let fs = FakeFs::new(cx.executor());
- fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
- .await;
- fs.insert_tree("/project-b", serde_json::json!({ "src": {} }))
+ fs.insert_tree("/my-project", serde_json::json!({ "src": {} }))
.await;
cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
- let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
- let project_b = project::Project::test(fs.clone(), ["/project-b".as_ref()], cx).await;
-
+ let project = project::Project::test(fs.clone(), ["/my-project".as_ref()], cx).await;
let (multi_workspace, cx) =
- cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
- let (sidebar, panel_a) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
-
- let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
- mw.test_add_workspace(project_b.clone(), window, cx)
- });
- let panel_b = add_agent_panel(&workspace_b, cx);
- cx.run_until_parked();
-
- // Explicitly create a draft on workspace_b so the sidebar tracks one.
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.create_new_thread(&workspace_b, window, cx);
- });
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let (sidebar, panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
cx.run_until_parked();
- // --- Scenario 1: archive a thread in the non-active workspace ---
-
- // Create a thread in project-a (non-active — project-b is active).
let connection = acp_thread::StubAgentConnection::new();
connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
acp::ContentChunk::new("Done".into()),
)]);
- agent_ui::test_support::open_thread_with_connection(&panel_a, connection, cx);
- agent_ui::test_support::send_message(&panel_a, cx);
- let thread_a = agent_ui::test_support::active_session_id(&panel_a, cx);
+ agent_ui::test_support::open_thread_with_connection(&panel, connection, cx);
+ agent_ui::test_support::send_message(&panel, cx);
+ let session_id = agent_ui::test_support::active_session_id(&panel, cx);
cx.run_until_parked();
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.archive_thread(&thread_a, window, cx);
+ let original_thread_id = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&session_id))
+ .map(|e| e.thread_id)
+ .expect("thread should exist in store before archiving")
});
- cx.run_until_parked();
- // active_entry should still be a draft on workspace_b (the active one).
- sidebar.read_with(cx, |sidebar, _| {
- assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Draft { workspace: ws, .. }) if ws == &workspace_b),
- "expected Draft(workspace_b) after archiving non-active thread, got: {:?}",
- sidebar.active_entry,
- );
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.archive_thread(&session_id, window, cx);
});
-
- // --- Scenario 2: archive the thread the user is looking at ---
-
- // Create a thread in project-b (the active workspace) and verify it
- // becomes the active entry.
- let connection = acp_thread::StubAgentConnection::new();
- connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
- acp::ContentChunk::new("Done".into()),
- )]);
- agent_ui::test_support::open_thread_with_connection(&panel_b, connection, cx);
- agent_ui::test_support::send_message(&panel_b, cx);
- let thread_b = agent_ui::test_support::active_session_id(&panel_b, cx);
cx.run_until_parked();
- sidebar.read_with(cx, |sidebar, _| {
- assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Thread { session_id, .. }) if *session_id == thread_b),
- "expected active_entry to be Thread({thread_b}), got: {:?}",
- sidebar.active_entry,
- );
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(original_thread_id)
+ .cloned()
+ .expect("metadata should exist after archiving")
});
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.archive_thread(&thread_b, window, cx);
+ sidebar.activate_archived_thread(metadata, window, cx);
});
cx.run_until_parked();
- // Should fall back to a draft on the same workspace.
- sidebar.read_with(cx, |sidebar, _| {
- assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Draft { workspace: ws, .. }) if ws == &workspace_b),
- "expected Draft(workspace_b) after archiving active thread, got: {:?}",
- sidebar.active_entry,
- );
+ let session_entries = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .filter(|entry| entry.session_id.as_ref() == Some(&session_id))
+ .cloned()
+ .collect::<Vec<_>>()
});
+
+ assert_eq!(
+ session_entries.len(),
+ 1,
+ "expected exactly one metadata row for the restored session, got: {session_entries:?}"
+ );
+ assert_eq!(
+ session_entries[0].thread_id, original_thread_id,
+ "expected unarchive to reuse the original thread id instead of creating a duplicate row"
+ );
+ assert!(
+ !session_entries[0].is_draft(),
+ "expected restored metadata to be a real thread, got: {:?}",
+ session_entries[0]
+ );
+
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ let real_thread_rows = entries
+ .iter()
+ .filter(|entry| !entry.starts_with("v ") && !entry.starts_with("> "))
+ .filter(|entry| !entry.contains("Draft"))
+ .count();
+ assert_eq!(
+ real_thread_rows, 1,
+ "expected exactly one visible real thread row after unarchive, got entries: {entries:?}"
+ );
+ assert!(
+ !entries.iter().any(|entry| entry.contains("Draft")),
+ "expected no draft rows after restoring, got entries: {entries:?}"
+ );
}
#[gpui::test]
@@ -6090,7 +6845,7 @@ async fn test_switch_to_workspace_with_archived_thread_shows_draft(cx: &mut Test
sidebar.read_with(cx, |sidebar, _| {
assert!(
- matches!(&sidebar.active_entry, Some(ActiveEntry::Draft { workspace: ws, .. }) if ws == &workspace_a),
+ matches!(&sidebar.active_entry, Some(ActiveEntry { workspace: ws, .. }) if ws == &workspace_a),
"expected Draft(workspace_a) after switching to workspace with archived thread, got: {:?}",
sidebar.active_entry,
);
@@ -6106,7 +6861,7 @@ async fn test_archived_threads_excluded_from_sidebar_entries(cx: &mut TestAppCon
save_thread_metadata(
acp::SessionId::new(Arc::from("visible-thread")),
- "Visible Thread".into(),
+ Some("Visible Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&project,
@@ -6116,7 +6871,7 @@ async fn test_archived_threads_excluded_from_sidebar_entries(cx: &mut TestAppCon
let archived_thread_session_id = acp::SessionId::new(Arc::from("archived-thread"));
save_thread_metadata(
archived_thread_session_id.clone(),
- "Archived Thread".into(),
+ Some("Archived Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -6125,7 +6880,12 @@ async fn test_archived_threads_excluded_from_sidebar_entries(cx: &mut TestAppCon
cx.update(|_, cx| {
ThreadMetadataStore::global(cx).update(cx, |store, cx| {
- store.archive(&archived_thread_session_id, None, cx)
+ let thread_id = store
+ .entries()
+ .find(|e| e.session_id.as_ref() == Some(&archived_thread_session_id))
+ .map(|e| e.thread_id)
+ .unwrap();
+ store.archive(thread_id, None, cx)
})
});
cx.run_until_parked();
@@ -6155,7 +6915,10 @@ async fn test_archived_threads_excluded_from_sidebar_entries(cx: &mut TestAppCon
let archived: Vec<_> = store.read(cx).archived_entries().collect();
assert_eq!(archived.len(), 1);
- assert_eq!(archived[0].session_id.0.as_ref(), "archived-thread");
+ assert_eq!(
+ archived[0].session_id.as_ref().unwrap().0.as_ref(),
+ "archived-thread"
+ );
});
}
@@ -6252,7 +7015,7 @@ async fn test_archive_last_thread_on_linked_worktree_does_not_create_new_thread_
// Save the worktree thread's metadata.
save_thread_metadata(
worktree_thread_id.clone(),
- "Ochre Drift Thread".into(),
+ Some("Ochre Drift Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&worktree_project,
@@ -6263,7 +7026,7 @@ async fn test_archive_last_thread_on_linked_worktree_does_not_create_new_thread_
// group that can be selected after archiving.
save_thread_metadata(
acp::SessionId::new(Arc::from("main-project-thread")),
- "Main Project Thread".into(),
+ Some("Main Project Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&main_project,
@@ -6417,7 +7180,7 @@ async fn test_archive_last_thread_on_linked_worktree_with_no_siblings_creates_dr
save_thread_metadata(
worktree_thread_id.clone(),
- "Ochre Drift Thread".into(),
+ Some("Ochre Drift Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&worktree_project,
@@ -6451,6 +7214,194 @@ async fn test_archive_last_thread_on_linked_worktree_with_no_siblings_creates_dr
});
}
+#[gpui::test]
+async fn test_unarchive_linked_worktree_thread_into_project_group_shows_only_restored_real_thread(
+ cx: &mut TestAppContext,
+) {
+ // When an archived thread belongs to a linked worktree whose main repo is
+ // already open, unarchiving should reopen the linked workspace into the
+ // same project group and show only the restored real thread row.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+
+ fs.insert_tree(
+ "/project",
+ serde_json::json!({
+ ".git": {},
+ "src": {},
+ }),
+ )
+ .await;
+
+ fs.insert_tree(
+ "/wt-ochre-drift",
+ serde_json::json!({
+ ".git": "gitdir: /project/.git/worktrees/ochre-drift",
+ "src": {},
+ }),
+ )
+ .await;
+
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-ochre-drift"),
+ ref_name: Some("refs/heads/ochre-drift".into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
+
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ let main_project = project::Project::test(fs.clone(), ["/project".as_ref()], cx).await;
+ let worktree_project =
+ project::Project::test(fs.clone(), ["/wt-ochre-drift".as_ref()], cx).await;
+
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+ let main_workspace =
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().unwrap().clone());
+ let _main_panel = add_agent_panel(&main_workspace, cx);
+ cx.run_until_parked();
+
+ let entries_before = visible_entries_as_strings(&sidebar, cx);
+ assert!(
+ entries_before.iter().any(|entry| entry.contains("Draft")),
+ "expected main workspace to start with a fallback draft, got entries: {entries_before:?}"
+ );
+
+ let session_id = acp::SessionId::new(Arc::from("linked-worktree-unarchive"));
+ let original_thread_id = ThreadId::new();
+ let main_paths = PathList::new(&[PathBuf::from("/project")]);
+ let folder_paths = PathList::new(&[PathBuf::from("/wt-ochre-drift")]);
+
+ cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx).update(cx, |store, cx| {
+ store.save_manually(
+ ThreadMetadata {
+ thread_id: original_thread_id,
+ session_id: Some(session_id.clone()),
+ agent_id: agent::ZED_AGENT_ID.clone(),
+ title: Some("Unarchived Linked Thread".into()),
+ updated_at: Utc::now(),
+ created_at: None,
+ worktree_paths: WorktreePaths::from_path_lists(
+ main_paths.clone(),
+ folder_paths.clone(),
+ )
+ .expect("main and folder paths should be well-formed"),
+ archived: true,
+ remote_connection: None,
+ },
+ cx,
+ )
+ });
+ });
+ cx.run_until_parked();
+
+ let metadata = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entry(original_thread_id)
+ .cloned()
+ .expect("archived linked-worktree metadata should exist before restore")
+ });
+
+ sidebar.update_in(cx, |sidebar, window, cx| {
+ sidebar.activate_archived_thread(metadata, window, cx);
+ });
+
+ cx.run_until_parked();
+ cx.run_until_parked();
+ cx.run_until_parked();
+
+ assert_eq!(
+ multi_workspace.read_with(cx, |mw, _| mw.workspaces().count()),
+ 2,
+ "expected unarchive to open the linked worktree workspace into the project group"
+ );
+
+ let session_entries = cx.update(|_, cx| {
+ ThreadMetadataStore::global(cx)
+ .read(cx)
+ .entries()
+ .filter(|entry| entry.session_id.as_ref() == Some(&session_id))
+ .cloned()
+ .collect::<Vec<_>>()
+ });
+ assert_eq!(
+ session_entries.len(),
+ 1,
+ "expected exactly one metadata row for restored linked worktree session, got: {session_entries:?}"
+ );
+ assert_eq!(
+ session_entries[0].thread_id, original_thread_id,
+ "expected unarchive to reuse the original linked worktree thread id"
+ );
+ assert!(
+ !session_entries[0].archived,
+ "expected restored linked worktree metadata to be unarchived, got: {:?}",
+ session_entries[0]
+ );
+
+ let assert_no_extra_rows = |entries: &[String]| {
+ let real_thread_rows = entries
+ .iter()
+ .filter(|entry| !entry.starts_with("v ") && !entry.starts_with("> "))
+ .filter(|entry| !entry.contains("Draft"))
+ .count();
+ assert_eq!(
+ real_thread_rows, 1,
+ "expected exactly one visible real thread row after linked-worktree unarchive, got entries: {entries:?}"
+ );
+ assert!(
+ !entries.iter().any(|entry| entry.contains("Draft")),
+ "expected no draft rows after linked-worktree unarchive, got entries: {entries:?}"
+ );
+ assert!(
+ !entries
+ .iter()
+ .any(|entry| entry.contains(DEFAULT_THREAD_TITLE)),
+ "expected no default-titled real placeholder row after linked-worktree unarchive, got entries: {entries:?}"
+ );
+ assert!(
+ entries
+ .iter()
+ .any(|entry| entry.contains("Unarchived Linked Thread")),
+ "expected restored linked worktree thread row to be visible, got entries: {entries:?}"
+ );
+ };
+
+ let entries_after_restore = visible_entries_as_strings(&sidebar, cx);
+ assert_no_extra_rows(&entries_after_restore);
+
+ // The reported bug may only appear after an extra scheduling turn.
+ cx.run_until_parked();
+ cx.run_until_parked();
+
+ let entries_after_extra_turns = visible_entries_as_strings(&sidebar, cx);
+ assert_no_extra_rows(&entries_after_extra_turns);
+}
+
#[gpui::test]
async fn test_archive_thread_on_linked_worktree_selects_sibling_thread(cx: &mut TestAppContext) {
// When a linked worktree thread is archived but the group has other
@@ -6536,7 +7487,7 @@ async fn test_archive_thread_on_linked_worktree_selects_sibling_thread(cx: &mut
save_thread_metadata(
worktree_thread_id.clone(),
- "Ochre Drift Thread".into(),
+ Some("Ochre Drift Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
None,
&worktree_project,
@@ -6547,7 +7498,7 @@ async fn test_archive_thread_on_linked_worktree_selects_sibling_thread(cx: &mut
let main_thread_id = acp::SessionId::new(Arc::from("main-project-thread"));
save_thread_metadata(
main_thread_id,
- "Main Project Thread".into(),
+ Some("Main Project Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&main_project,
@@ -6696,22 +7647,20 @@ async fn test_linked_worktree_workspace_reachable_and_dismissable(cx: &mut TestA
"linked worktree workspace should be reachable, but reachable are: {reachable:?}"
);
- // Find the DraftThread entry whose workspace is the linked worktree.
+ // Find the draft Thread entry whose workspace is the linked worktree.
let new_thread_ix = sidebar.read_with(cx, |sidebar, _| {
sidebar
.contents
.entries
.iter()
- .position(|entry| {
- matches!(
- entry,
- ListEntry::DraftThread {
- workspace: Some(ws),
- ..
- } if ws.entity_id() == worktree_ws_id
- )
+ .position(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => matches!(
+ &thread.workspace,
+ ThreadEntryWorkspace::Open(ws) if ws.entity_id() == worktree_ws_id
+ ),
+ _ => false,
})
- .expect("expected a DraftThread entry for the linked worktree")
+ .expect("expected a draft thread entry for the linked worktree")
});
assert_eq!(
@@ -6732,20 +7681,17 @@ async fn test_linked_worktree_workspace_reachable_and_dismissable(cx: &mut TestA
);
let has_draft_for_worktree = sidebar.read_with(cx, |sidebar, _| {
- sidebar.contents.entries.iter().any(|entry| {
- matches!(
- entry,
- ListEntry::DraftThread {
- draft_id: Some(_),
- workspace: Some(ws),
- ..
- } if ws.entity_id() == worktree_ws_id
- )
+ sidebar.contents.entries.iter().any(|entry| match entry {
+ ListEntry::Thread(thread) if thread.is_draft => matches!(
+ &thread.workspace,
+ ThreadEntryWorkspace::Open(ws) if ws.entity_id() == worktree_ws_id
+ ),
+ _ => false,
})
});
assert!(
!has_draft_for_worktree,
- "DraftThread entry for the linked worktree should be removed after dismiss"
+ "draft thread entry for the linked worktree should be removed after dismiss"
);
}
@@ -6841,6 +7787,7 @@ async fn init_multi_project_test(
) -> (Arc<FakeFs>, Entity<project::Project>) {
agent_ui::test_support::init_test(cx);
cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
ThreadStore::init_global(cx);
ThreadMetadataStore::init_global(cx);
language_model::LanguageModelRegistry::test(cx);
@@ -7057,14 +8004,15 @@ async fn test_legacy_thread_with_canonical_path_opens_main_repo_workspace(cx: &m
let legacy_session = acp::SessionId::new(Arc::from("legacy-main-thread"));
cx.update(|_, cx| {
let metadata = ThreadMetadata {
- session_id: legacy_session.clone(),
+ thread_id: ThreadId::new(),
+ session_id: Some(legacy_session.clone()),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Legacy Main Thread".into(),
+ title: Some("Legacy Main Thread".into()),
updated_at: chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_folder_paths(&PathList::new(&[
- PathBuf::from("/project"),
- ])),
+ worktree_paths: WorktreePaths::from_folder_paths(&PathList::new(&[PathBuf::from(
+ "/project",
+ )])),
archived: false,
remote_connection: None,
};
@@ -7237,18 +8185,8 @@ async fn test_linked_worktree_workspace_reachable_after_adding_unrelated_project
// Force a full sidebar rebuild with all groups expanded.
sidebar.update_in(cx, |sidebar, _window, cx| {
- sidebar.collapsed_groups.clear();
- let group_keys: Vec<project::ProjectGroupKey> = sidebar
- .contents
- .entries
- .iter()
- .filter_map(|entry| match entry {
- ListEntry::ProjectHeader { key, .. } => Some(key.clone()),
- _ => None,
- })
- .collect();
- for group_key in group_keys {
- sidebar.expanded_groups.insert(group_key, 10_000);
+ if let Some(mw) = sidebar.multi_workspace.upgrade() {
+ mw.update(cx, |mw, _cx| mw.test_expand_all_groups());
}
sidebar.update_entries(cx);
});
@@ -7292,23 +8230,14 @@ async fn test_startup_failed_restoration_shows_draft(cx: &mut TestAppContext) {
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
- // In tests, AgentPanel::test_new doesn't call `load`, so no
- // fallback draft is created. The empty group shows a placeholder.
- // Simulate the startup fallback by creating a draft explicitly.
- let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.create_new_thread(&workspace, window, cx);
- });
- cx.run_until_parked();
-
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec!["v [my-project]", " [~ Draft] *"]
- );
+ let _workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
- sidebar.read_with(cx, |sidebar, _| {
- assert_active_draft(sidebar, &workspace, "should show active draft");
- });
+ let entries = visible_entries_as_strings(&sidebar, cx);
+ assert_eq!(entries.len(), 2, "should have header + draft: {entries:?}");
+ assert!(
+ entries[1].contains("Draft"),
+ "second entry should be a draft: {entries:?}"
+ );
}
#[gpui::test]
@@ -7343,66 +8272,39 @@ async fn test_startup_successful_restoration_no_spurious_draft(cx: &mut TestAppC
#[gpui::test]
async fn test_delete_last_draft_in_empty_group_shows_placeholder(cx: &mut TestAppContext) {
- // Rule 8: Deleting the last draft in a threadless group should
+ // Deleting the last draft in a threadless group should
// leave a placeholder draft entry (not an empty group).
let project = init_test_project_with_agent_panel("/my-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
- // Create two drafts explicitly (test_new doesn't call load).
+ // Reconciliation creates a draft for the empty group.
let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.create_new_thread(&workspace, window, cx);
- });
- cx.run_until_parked();
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.create_new_thread(&workspace, window, cx);
- });
- cx.run_until_parked();
-
- assert_eq!(
- visible_entries_as_strings(&sidebar, cx),
- vec!["v [my-project]", " [~ Draft] *", " [~ Draft]"]
- );
-
- // Delete the active (first) draft. The second should become active.
- let active_draft_id = sidebar.read_with(cx, |_sidebar, cx| {
- workspace
- .read(cx)
- .panel::<AgentPanel>(cx)
- .unwrap()
- .read(cx)
- .active_draft_id()
- .unwrap()
- });
- sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.remove_draft(active_draft_id, &workspace, window, cx);
- });
- cx.run_until_parked();
-
- // Should still have 1 draft (the remaining one), now active.
let entries = visible_entries_as_strings(&sidebar, cx);
let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
- assert_eq!(draft_count, 1, "one draft should remain after deleting one");
+ assert_eq!(
+ draft_count, 1,
+ "should start with 1 draft from reconciliation"
+ );
- // Delete the last remaining draft.
- let last_draft_id = sidebar.read_with(cx, |_sidebar, cx| {
- workspace
- .read(cx)
- .panel::<AgentPanel>(cx)
- .unwrap()
+ // Find and delete the draft.
+ let draft_thread_id = sidebar.read_with(cx, |_sidebar, cx| {
+ let panel = workspace.read(cx).panel::<AgentPanel>(cx).unwrap();
+ panel
.read(cx)
- .active_draft_id()
+ .draft_thread_ids(cx)
+ .into_iter()
+ .next()
.unwrap()
});
sidebar.update_in(cx, |sidebar, window, cx| {
- sidebar.remove_draft(last_draft_id, &workspace, window, cx);
+ sidebar.remove_draft(draft_thread_id, &workspace, window, cx);
});
cx.run_until_parked();
// The group has no threads and no tracked drafts, so a
- // placeholder draft should appear.
+ // placeholder draft should appear via reconciliation.
let entries = visible_entries_as_strings(&sidebar, cx);
let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
assert_eq!(
@@ -7516,28 +8418,26 @@ async fn test_project_header_click_restores_last_viewed(cx: &mut TestAppContext)
}
#[gpui::test]
-async fn test_plus_button_always_creates_new_draft(cx: &mut TestAppContext) {
- // Rule 3: Clicking the + button on a group should always create
- // a new draft, even starting from a placeholder (no tracked drafts).
+async fn test_plus_button_reuses_empty_draft(cx: &mut TestAppContext) {
+ // Clicking the + button when an empty draft already exists should
+ // focus the existing draft rather than creating a new one.
let project = init_test_project_with_agent_panel("/my-project", cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx);
- // Start: panel has no tracked drafts, sidebar shows a placeholder.
+ // Start: panel has 1 draft from set_active.
let entries = visible_entries_as_strings(&sidebar, cx);
let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
- assert_eq!(draft_count, 1, "should start with 1 placeholder");
+ assert_eq!(draft_count, 1, "should start with 1 draft");
- // Simulate what the + button handler does: create exactly one
- // new draft per click.
let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
let simulate_plus_button =
|sidebar: &mut Sidebar, window: &mut Window, cx: &mut Context<Sidebar>| {
sidebar.create_new_thread(&workspace, window, cx);
};
- // First + click: placeholder -> 1 tracked draft.
+ // + click with empty draft: should reuse it, not create a new one.
sidebar.update_in(cx, |sidebar, window, cx| {
simulate_plus_button(sidebar, window, cx);
});
@@ -7547,30 +8447,10 @@ async fn test_plus_button_always_creates_new_draft(cx: &mut TestAppContext) {
let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
assert_eq!(
draft_count, 1,
- "first + click on placeholder should produce 1 tracked draft"
+ "+ click should reuse the existing empty draft, not create a new one"
);
- // Second + click: 1 -> 2 drafts.
- sidebar.update_in(cx, |sidebar, window, cx| {
- simulate_plus_button(sidebar, window, cx);
- });
- cx.run_until_parked();
-
- let entries = visible_entries_as_strings(&sidebar, cx);
- let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
- assert_eq!(draft_count, 2, "second + click should add 1 more draft");
-
- // Third + click: 2 -> 3 drafts.
- sidebar.update_in(cx, |sidebar, window, cx| {
- simulate_plus_button(sidebar, window, cx);
- });
- cx.run_until_parked();
-
- let entries = visible_entries_as_strings(&sidebar, cx);
- let draft_count = entries.iter().filter(|e| e.contains("Draft")).count();
- assert_eq!(draft_count, 3, "third + click should add 1 more draft");
-
- // The most recently created draft should be active (first in list).
+ // The draft should be active.
assert_eq!(entries[1], " [~ Draft] *");
}
@@ -7649,6 +8529,254 @@ async fn test_activating_workspace_with_draft_does_not_create_extras(cx: &mut Te
});
}
+#[gpui::test]
+async fn test_non_archive_thread_paths_migrate_on_worktree_add_and_remove(cx: &mut TestAppContext) {
+ // Historical threads (not open in any agent panel) should have their
+ // worktree paths updated when a folder is added to or removed from the
+ // project.
+ let (_fs, project) = init_multi_project_test(&["/project-a", "/project-b"], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let sidebar = setup_sidebar(&multi_workspace, cx);
+
+ // Save two threads directly into the metadata store (not via the agent
+ // panel), so they are purely historical — no open views hold them.
+ // Use different timestamps so sort order is deterministic.
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("hist-1")),
+ Some("Historical 1".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
+ None,
+ &project,
+ cx,
+ );
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("hist-2")),
+ Some("Historical 2".into()),
+ chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 1).unwrap(),
+ None,
+ &project,
+ cx,
+ );
+ cx.run_until_parked();
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+
+ // Sanity-check: both threads exist under the initial key [/project-a].
+ let old_key_paths = PathList::new(&[PathBuf::from("/project-a")]);
+ cx.update(|_window, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(
+ store.entries_for_main_worktree_path(&old_key_paths).count(),
+ 2,
+ "should have 2 historical threads under old key before worktree add"
+ );
+ });
+
+ // Add a second worktree to the project.
+ // TODO: Should there be different behavior for calling Project::find_or_create_worktree,
+ // or MultiWorkspace::add_folders_to_project_group?
+ project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree("/project-b", true, cx)
+ })
+ .await
+ .expect("should add worktree");
+ cx.run_until_parked();
+
+ // The historical threads should now be indexed under the new combined
+ // key [/project-a, /project-b].
+ let new_key_paths = PathList::new(&[PathBuf::from("/project-a"), PathBuf::from("/project-b")]);
+ cx.update(|_window, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(
+ store.entries_for_main_worktree_path(&old_key_paths).count(),
+ 0,
+ "should have 0 historical threads under old key after worktree add"
+ );
+ assert_eq!(
+ store.entries_for_main_worktree_path(&new_key_paths).count(),
+ 2,
+ "should have 2 historical threads under new key after worktree add"
+ );
+ });
+
+ // Sidebar should show threads under the new header.
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec![
+ "v [project-a, project-b]",
+ " Historical 2",
+ " Historical 1",
+ ]
+ );
+
+ // Now remove the second worktree.
+ let worktree_id = project.read_with(cx, |project, cx| {
+ project
+ .visible_worktrees(cx)
+ .find(|wt| wt.read(cx).abs_path().as_ref() == Path::new("/project-b"))
+ .map(|wt| wt.read(cx).id())
+ .expect("should find project-b worktree")
+ });
+ project.update(cx, |project, cx| {
+ project.remove_worktree(worktree_id, cx);
+ });
+ cx.run_until_parked();
+
+ // Historical threads should migrate back to the original key.
+ cx.update(|_window, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(
+ store.entries_for_main_worktree_path(&new_key_paths).count(),
+ 0,
+ "should have 0 historical threads under new key after worktree remove"
+ );
+ assert_eq!(
+ store.entries_for_main_worktree_path(&old_key_paths).count(),
+ 2,
+ "should have 2 historical threads under old key after worktree remove"
+ );
+ });
+
+ sidebar.update_in(cx, |sidebar, _window, cx| sidebar.update_entries(cx));
+ cx.run_until_parked();
+ assert_eq!(
+ visible_entries_as_strings(&sidebar, cx),
+ vec!["v [project-a]", " Historical 2", " Historical 1",]
+ );
+}
+
+#[gpui::test]
+async fn test_worktree_add_only_migrates_threads_for_same_folder_paths(cx: &mut TestAppContext) {
+ // When two workspaces share the same project group (same main path)
+ // but have different folder paths (main repo vs linked worktree),
+ // adding a worktree to the main workspace should only migrate threads
+ // whose folder paths match that workspace — not the linked worktree's
+ // threads.
+ agent_ui::test_support::init_test(cx);
+ cx.update(|cx| {
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ ThreadStore::init_global(cx);
+ ThreadMetadataStore::init_global(cx);
+ language_model::LanguageModelRegistry::test(cx);
+ prompt_store::init(cx);
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/project", serde_json::json!({ ".git": {}, "src": {} }))
+ .await;
+ fs.insert_tree("/project-b", serde_json::json!({ ".git": {}, "src": {} }))
+ .await;
+ fs.add_linked_worktree_for_repo(
+ Path::new("/project/.git"),
+ false,
+ git::repository::Worktree {
+ path: std::path::PathBuf::from("/wt-feature"),
+ ref_name: Some("refs/heads/feature".into()),
+ sha: "aaa".into(),
+ is_main: false,
+ },
+ )
+ .await;
+ cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
+
+ // Workspace A: main repo at /project.
+ let main_project =
+ project::Project::test(fs.clone() as Arc<dyn fs::Fs>, ["/project".as_ref()], cx).await;
+ // Workspace B: linked worktree of the same repo (same group, different folder).
+ let worktree_project =
+ project::Project::test(fs.clone() as Arc<dyn fs::Fs>, ["/wt-feature".as_ref()], cx).await;
+
+ main_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+ worktree_project
+ .update(cx, |p, cx| p.git_scans_complete(cx))
+ .await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(main_project.clone(), window, cx));
+ let _sidebar = setup_sidebar(&multi_workspace, cx);
+ multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(worktree_project.clone(), window, cx);
+ });
+ cx.run_until_parked();
+
+ // Save a thread for each workspace's folder paths.
+ let time_main = chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 1).unwrap();
+ let time_wt = chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 2).unwrap();
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-main")),
+ Some("Main Thread".into()),
+ time_main,
+ Some(time_main),
+ &main_project,
+ cx,
+ );
+ save_thread_metadata(
+ acp::SessionId::new(Arc::from("thread-wt")),
+ Some("Worktree Thread".into()),
+ time_wt,
+ Some(time_wt),
+ &worktree_project,
+ cx,
+ );
+ cx.run_until_parked();
+
+ let folder_paths_main = PathList::new(&[PathBuf::from("/project")]);
+ let folder_paths_wt = PathList::new(&[PathBuf::from("/wt-feature")]);
+
+ // Sanity-check: each thread is indexed under its own folder paths.
+ cx.update(|_window, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(
+ store.entries_for_path(&folder_paths_main).count(),
+ 1,
+ "one thread under [/project]"
+ );
+ assert_eq!(
+ store.entries_for_path(&folder_paths_wt).count(),
+ 1,
+ "one thread under [/wt-feature]"
+ );
+ });
+
+ // Add /project-b to the main project only.
+ main_project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree("/project-b", true, cx)
+ })
+ .await
+ .expect("should add worktree");
+ cx.run_until_parked();
+
+ // Main Thread (folder paths [/project]) should have migrated to
+ // [/project, /project-b]. Worktree Thread should be unchanged.
+ let folder_paths_main_b =
+ PathList::new(&[PathBuf::from("/project"), PathBuf::from("/project-b")]);
+ cx.update(|_window, cx| {
+ let store = ThreadMetadataStore::global(cx).read(cx);
+ assert_eq!(
+ store.entries_for_path(&folder_paths_main).count(),
+ 0,
+ "main thread should no longer be under old folder paths [/project]"
+ );
+ assert_eq!(
+ store.entries_for_path(&folder_paths_main_b).count(),
+ 1,
+ "main thread should now be under [/project, /project-b]"
+ );
+ assert_eq!(
+ store.entries_for_path(&folder_paths_wt).count(),
+ 1,
+ "worktree thread should remain unchanged under [/wt-feature]"
+ );
+ });
+}
+
mod property_test {
use super::*;
use gpui::proptest::prelude::*;
@@ -7796,13 +8924,13 @@ mod property_test {
.unwrap()
+ chrono::Duration::seconds(state.thread_counter as i64);
let metadata = ThreadMetadata {
- session_id,
+ thread_id: ThreadId::new(),
+ session_id: Some(session_id),
agent_id: agent::ZED_AGENT_ID.clone(),
- title,
+ title: Some(title),
updated_at,
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_path_lists(main_worktree_paths, path_list)
- .unwrap(),
+ worktree_paths: WorktreePaths::from_path_lists(main_worktree_paths, path_list).unwrap(),
archived: false,
remote_connection: None,
};
@@ -7827,12 +8955,12 @@ mod property_test {
// Find a workspace for this project group and create a real
// thread via its agent panel.
let (workspace, project) = multi_workspace.read_with(cx, |mw, cx| {
- let key = mw.project_group_keys().nth(project_group_index).unwrap();
+ let keys = mw.project_group_keys();
+ let key = &keys[project_group_index];
let ws = mw
.workspaces_for_project_group(key, cx)
- .next()
- .unwrap_or(mw.workspace())
- .clone();
+ .and_then(|ws| ws.first().cloned())
+ .unwrap_or_else(|| mw.workspace().clone());
let project = ws.read(cx).project().clone();
(ws, project)
});
@@ -7857,7 +8985,7 @@ mod property_test {
chrono::TimeZone::with_ymd_and_hms(&chrono::Utc, 2024, 1, 1, 0, 0, 0)
.unwrap()
+ chrono::Duration::seconds(state.thread_counter as i64);
- save_thread_metadata(session_id, title, updated_at, None, &project, cx);
+ save_thread_metadata(session_id, Some(title), updated_at, None, &project, cx);
}
}
Operation::SaveWorktreeThread { worktree_index } => {
@@ -7941,7 +9069,7 @@ mod property_test {
sidebar.contents.entries.iter().position(|entry| {
matches!(
entry,
- ListEntry::Thread(t) if t.metadata.session_id == session_id
+ ListEntry::Thread(t) if t.metadata.session_id.as_ref() == Some(&session_id)
)
})
});
@@ -7955,11 +9083,11 @@ mod property_test {
}
Operation::SwitchToProjectGroup { index } => {
let workspace = multi_workspace.read_with(cx, |mw, cx| {
- let key = mw.project_group_keys().nth(index).unwrap();
+ let keys = mw.project_group_keys();
+ let key = &keys[index];
mw.workspaces_for_project_group(key, cx)
- .next()
- .unwrap_or(mw.workspace())
- .clone()
+ .and_then(|ws| ws.first().cloned())
+ .unwrap_or_else(|| mw.workspace().clone())
});
multi_workspace.update_in(cx, |mw, window, cx| {
mw.activate(workspace, window, cx);
@@ -7970,7 +9098,8 @@ mod property_test {
} => {
// Get the main worktree path from the project group key.
let main_path = multi_workspace.read_with(cx, |mw, _| {
- let key = mw.project_group_keys().nth(project_group_index).unwrap();
+ let keys = mw.project_group_keys();
+ let key = &keys[project_group_index];
key.path_list()
.paths()
.first()
@@ -8022,11 +9151,11 @@ mod property_test {
// Re-scan the main workspace's project so it discovers the new worktree.
let main_workspace = multi_workspace.read_with(cx, |mw, cx| {
- let key = mw.project_group_keys().nth(project_group_index).unwrap();
+ let keys = mw.project_group_keys();
+ let key = &keys[project_group_index];
mw.workspaces_for_project_group(key, cx)
- .next()
+ .and_then(|ws| ws.first().cloned())
.unwrap()
- .clone()
});
let main_project = main_workspace.read_with(cx, |ws, _| ws.project().clone());
main_project
@@ -8042,8 +9171,10 @@ mod property_test {
project_group_index,
} => {
let workspace = multi_workspace.read_with(cx, |mw, cx| {
- let key = mw.project_group_keys().nth(project_group_index).unwrap();
- mw.workspaces_for_project_group(key, cx).next().cloned()
+ let keys = mw.project_group_keys();
+ let key = &keys[project_group_index];
+ mw.workspaces_for_project_group(key, cx)
+ .and_then(|ws| ws.first().cloned())
});
let Some(workspace) = workspace else { return };
let project = workspace.read_with(cx, |ws, _| ws.project().clone());
@@ -8068,8 +9199,10 @@ mod property_test {
project_group_index,
} => {
let workspace = multi_workspace.read_with(cx, |mw, cx| {
- let key = mw.project_group_keys().nth(project_group_index).unwrap();
- mw.workspaces_for_project_group(key, cx).next().cloned()
+ let keys = mw.project_group_keys();
+ let key = &keys[project_group_index];
+ mw.workspaces_for_project_group(key, cx)
+ .and_then(|ws| ws.first().cloned())
});
let Some(workspace) = workspace else { return };
let project = workspace.read_with(cx, |ws, _| ws.project().clone());
@@ -8094,18 +9227,8 @@ mod property_test {
fn update_sidebar(sidebar: &Entity<Sidebar>, cx: &mut gpui::VisualTestContext) {
sidebar.update_in(cx, |sidebar, _window, cx| {
- sidebar.collapsed_groups.clear();
- let group_keys: Vec<project::ProjectGroupKey> = sidebar
- .contents
- .entries
- .iter()
- .filter_map(|entry| match entry {
- ListEntry::ProjectHeader { key, .. } => Some(key.clone()),
- _ => None,
- })
- .collect();
- for group_key in group_keys {
- sidebar.expanded_groups.insert(group_key, 10_000);
+ if let Some(mw) = sidebar.multi_workspace.upgrade() {
+ mw.update(cx, |mw, _cx| mw.test_expand_all_groups());
}
sidebar.update_entries(cx);
});
@@ -8129,7 +9252,7 @@ mod property_test {
if let Some(session_id) = entry.session_id() {
if !seen.insert(session_id.clone()) {
let title = match entry {
- ListEntry::Thread(thread) => thread.metadata.title.to_string(),
+ ListEntry::Thread(thread) => thread.metadata.display_title().to_string(),
_ => "<unknown>".to_string(),
};
duplicates.push((session_id.clone(), title));
@@ -8158,12 +9281,13 @@ mod property_test {
// Every project group key in the multi-workspace that has a
// non-empty path list should appear as a ProjectHeader in the
// sidebar.
- let expected_keys: HashSet<&project::ProjectGroupKey> = mw
- .project_group_keys()
+ let all_keys = mw.project_group_keys();
+ let expected_keys: HashSet<&ProjectGroupKey> = all_keys
+ .iter()
.filter(|k| !k.path_list().paths().is_empty())
.collect();
- let sidebar_keys: HashSet<&project::ProjectGroupKey> = sidebar
+ let sidebar_keys: HashSet<&ProjectGroupKey> = sidebar
.contents
.entries
.iter()
@@ -8211,7 +9335,7 @@ mod property_test {
// Query using the same approach as the sidebar: iterate project
// group keys, then do main + legacy queries per group.
let mw = multi_workspace.read(cx);
- let mut workspaces_by_group: HashMap<project::ProjectGroupKey, Vec<Entity<Workspace>>> =
+ let mut workspaces_by_group: HashMap<ProjectGroupKey, Vec<Entity<Workspace>>> =
HashMap::default();
for workspace in &workspaces {
let key = workspace.read(cx).project_group_key(cx);
@@ -8228,19 +9352,25 @@ mod property_test {
}
let group_workspaces = workspaces_by_group
- .get(group_key)
+ .get(&group_key)
.map(|ws| ws.as_slice())
.unwrap_or_default();
// Main code path queries (run for all groups, even without workspaces).
+ // Skip drafts (session_id: None) — they are shown via the
+ // panel's draft_thread_ids, not by session_id matching.
for metadata in thread_store
.read(cx)
.entries_for_main_worktree_path(&path_list)
{
- metadata_thread_ids.insert(metadata.session_id.clone());
+ if let Some(sid) = metadata.session_id.clone() {
+ metadata_thread_ids.insert(sid);
+ }
}
for metadata in thread_store.read(cx).entries_for_path(&path_list) {
- metadata_thread_ids.insert(metadata.session_id.clone());
+ if let Some(sid) = metadata.session_id.clone() {
+ metadata_thread_ids.insert(sid);
+ }
}
// Legacy: per-workspace queries for different root paths.
@@ -8258,7 +9388,9 @@ mod property_test {
let ws_path_list = workspace_path_list(workspace, cx);
if ws_path_list != path_list {
for metadata in thread_store.read(cx).entries_for_path(&ws_path_list) {
- metadata_thread_ids.insert(metadata.session_id.clone());
+ if let Some(sid) = metadata.session_id.clone() {
+ metadata_thread_ids.insert(sid);
+ }
}
}
}
@@ -8278,7 +9410,9 @@ mod property_test {
PathList::new(std::slice::from_ref(&linked_worktree.path));
for metadata in thread_store.read(cx).entries_for_path(&worktree_path_list)
{
- metadata_thread_ids.insert(metadata.session_id.clone());
+ if let Some(sid) = metadata.session_id.clone() {
+ metadata_thread_ids.insert(sid);
+ }
}
}
}
@@ -8311,7 +9445,7 @@ mod property_test {
// when the workspace just changed and the new panel has no
// content yet.
let panel = active_workspace.read(cx).panel::<AgentPanel>(cx).unwrap();
- let panel_has_content = panel.read(cx).active_draft_id().is_some()
+ let panel_has_content = panel.read(cx).active_thread_id(cx).is_some()
|| panel.read(cx).active_conversation_view().is_some();
let Some(entry) = sidebar.active_entry.as_ref() else {
@@ -8338,21 +9472,21 @@ mod property_test {
);
// 3. The entry must match the agent panel's current state.
- if panel.read(cx).active_draft_id().is_some() {
+ if panel.read(cx).active_thread_id(cx).is_some() {
anyhow::ensure!(
- matches!(entry, ActiveEntry::Draft { .. }),
+ matches!(entry, ActiveEntry { .. }),
"panel shows a tracked draft but active_entry is {:?}",
entry,
);
- } else if let Some(session_id) = panel
+ } else if let Some(thread_id) = panel
.read(cx)
.active_conversation_view()
- .and_then(|cv| cv.read(cx).parent_id(cx))
+ .map(|cv| cv.read(cx).parent_id())
{
anyhow::ensure!(
- matches!(entry, ActiveEntry::Thread { session_id: id, .. } if id == &session_id),
- "panel has session {:?} but active_entry is {:?}",
- session_id,
+ matches!(entry, ActiveEntry { thread_id: tid, .. } if *tid == thread_id),
+ "panel has thread {:?} but active_entry is {:?}",
+ thread_id,
entry,
);
}
@@ -8365,12 +9499,40 @@ mod property_test {
.iter()
.filter(|e| entry.matches_entry(e))
.count();
- anyhow::ensure!(
- matching_count == 1,
- "expected exactly 1 sidebar entry matching active_entry {:?}, found {}",
- entry,
- matching_count,
- );
+ if matching_count != 1 {
+ let thread_entries: Vec<_> = sidebar
+ .contents
+ .entries
+ .iter()
+ .filter_map(|e| match e {
+ ListEntry::Thread(t) => Some(format!(
+ "tid={:?} sid={:?} draft={}",
+ t.metadata.thread_id, t.metadata.session_id, t.is_draft
+ )),
+ _ => None,
+ })
+ .collect();
+ let store = agent_ui::thread_metadata_store::ThreadMetadataStore::global(cx).read(cx);
+ let store_entries: Vec<_> = store
+ .entries()
+ .map(|m| {
+ format!(
+ "tid={:?} sid={:?} archived={} paths={:?}",
+ m.thread_id,
+ m.session_id,
+ m.archived,
+ m.folder_paths()
+ )
+ })
+ .collect();
+ anyhow::bail!(
+ "expected exactly 1 sidebar entry matching active_entry {:?}, found {}. sidebar threads: {:?}. store: {:?}",
+ entry,
+ matching_count,
+ thread_entries,
+ store_entries,
+ );
+ }
Ok(())
}
@@ -8419,7 +9581,6 @@ mod property_test {
.assert_project_group_key_integrity(cx)
}
- #[ignore]
#[gpui::property_test(config = ProptestConfig {
cases: 50,
..Default::default()
@@ -8429,8 +9590,20 @@ mod property_test {
raw_operations: Vec<u32>,
cx: &mut TestAppContext,
) {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+ static NEXT_PROPTEST_DB: AtomicUsize = AtomicUsize::new(0);
+
agent_ui::test_support::init_test(cx);
cx.update(|cx| {
+ cx.set_global(db::AppDatabase::test_new());
+ cx.set_global(agent_ui::MaxIdleRetainedThreads(1));
+ cx.set_global(agent_ui::thread_metadata_store::TestMetadataDbName(
+ format!(
+ "PROPTEST_THREAD_METADATA_{}",
+ NEXT_PROPTEST_DB.fetch_add(1, Ordering::SeqCst)
+ ),
+ ));
+
ThreadStore::init_global(cx);
ThreadMetadataStore::init_global(cx);
language_model::LanguageModelRegistry::test(cx);
@@ -8475,7 +9648,7 @@ mod property_test {
for &raw_op in &raw_operations {
let project_group_count =
- multi_workspace.read_with(cx, |mw, _| mw.project_group_keys().count());
+ multi_workspace.read_with(cx, |mw, _| mw.project_group_keys().len());
let operation = state.generate_operation(raw_op, project_group_count);
executed.push(format!("{:?}", operation));
perform_operation(operation, &mut state, &multi_workspace, &sidebar, cx).await;
@@ -8617,7 +9790,7 @@ async fn test_remote_project_integration_does_not_briefly_render_as_separate_pro
let main_thread_id = acp::SessionId::new(Arc::from("main-thread"));
save_thread_metadata(
main_thread_id.clone(),
- "Main Thread".into(),
+ Some("Main Thread".into()),
chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
None,
&project,
@@ -8635,12 +9808,13 @@ async fn test_remote_project_integration_does_not_briefly_render_as_separate_pro
project.read_with(cx, |p, cx| p.project_group_key(cx).path_list().clone());
cx.update(|_window, cx| {
let metadata = ThreadMetadata {
- session_id: remote_thread_id.clone(),
+ thread_id: ThreadId::new(),
+ session_id: Some(remote_thread_id.clone()),
agent_id: agent::ZED_AGENT_ID.clone(),
- title: "Worktree Thread".into(),
+ title: Some("Worktree Thread".into()),
updated_at: chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 1).unwrap(),
created_at: None,
- worktree_paths: ThreadWorktreePaths::from_path_lists(
+ worktree_paths: WorktreePaths::from_path_lists(
main_worktree_paths,
PathList::new(&[PathBuf::from("/project-wt-1")]),
)
@@ -8657,7 +9831,7 @@ async fn test_remote_project_integration_does_not_briefly_render_as_separate_pro
sidebar.selection = sidebar.contents.entries.iter().position(|entry| {
matches!(
entry,
- ListEntry::Thread(thread) if thread.metadata.session_id == remote_thread_id
+ ListEntry::Thread(thread) if thread.metadata.session_id.as_ref() == Some(&remote_thread_id)
)
});
});
@@ -8769,7 +9943,7 @@ async fn test_remote_project_integration_does_not_briefly_render_as_separate_pro
assert_eq!(
group_after_update,
- project.read_with(cx, |project, cx| project.project_group_key(cx)),
+ project.read_with(cx, |project, cx| ProjectGroupKey::from_project(project, cx)),
"expected the remote worktree workspace to be grouped under the main remote project after the real update; \
final sidebar entries: {:?}",
entries_after_update,
diff --git a/crates/title_bar/src/title_bar.rs b/crates/title_bar/src/title_bar.rs
index f47817b2450..eb30aa8f44f 100644
--- a/crates/title_bar/src/title_bar.rs
+++ b/crates/title_bar/src/title_bar.rs
@@ -715,7 +715,7 @@ impl TitleBar {
.multi_workspace
.as_ref()
.and_then(|mw| mw.upgrade())
- .map(|mw| mw.read(cx).project_group_keys().cloned().collect())
+ .map(|mw| mw.read(cx).project_group_keys())
.unwrap_or_default();
PopoverMenu::new("recent-projects-menu")
@@ -772,7 +772,7 @@ impl TitleBar {
.multi_workspace
.as_ref()
.and_then(|mw| mw.upgrade())
- .map(|mw| mw.read(cx).project_group_keys().cloned().collect())
+ .map(|mw| mw.read(cx).project_group_keys())
.unwrap_or_default();
PopoverMenu::new("sidebar-title-recent-projects-menu")
diff --git a/crates/workspace/src/multi_workspace.rs b/crates/workspace/src/multi_workspace.rs
index 9ef81194639..7e38e57d312 100644
--- a/crates/workspace/src/multi_workspace.rs
+++ b/crates/workspace/src/multi_workspace.rs
@@ -5,11 +5,11 @@ use gpui::{
ManagedView, MouseButton, Pixels, Render, Subscription, Task, Tiling, Window, WindowId,
actions, deferred, px,
};
-use project::{DirectoryLister, DisableAiSettings, Project, ProjectGroupKey};
+pub use project::ProjectGroupKey;
+use project::{DirectoryLister, DisableAiSettings, Project};
use remote::RemoteConnectionOptions;
use settings::Settings;
pub use settings::SidebarSide;
-use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::path::Path;
use std::path::PathBuf;
@@ -101,13 +101,9 @@ pub enum MultiWorkspaceEvent {
ActiveWorkspaceChanged,
WorkspaceAdded(Entity<Workspace>),
WorkspaceRemoved(EntityId),
- WorktreePathAdded {
- old_main_paths: PathList,
- added_path: PathBuf,
- },
- WorktreePathRemoved {
- old_main_paths: PathList,
- removed_path: PathBuf,
+ ProjectGroupKeyUpdated {
+ old_key: ProjectGroupKey,
+ new_key: ProjectGroupKey,
},
}
@@ -265,52 +261,32 @@ impl<T: Sidebar> SidebarHandle for Entity<T> {
}
}
-/// Tracks which workspace the user is currently looking at.
-///
-/// `Persistent` workspaces live in the `workspaces` vec and are shown in the
-/// sidebar. `Transient` workspaces exist outside the vec and are discarded
-/// when the user switches away.
-enum ActiveWorkspace {
- /// A persistent workspace, identified by index into the `workspaces` vec.
- Persistent(usize),
- /// A workspace not in the `workspaces` vec that will be discarded on
- /// switch or promoted to persistent when the sidebar is opened.
- Transient(Entity<Workspace>),
+#[derive(Clone)]
+pub struct ProjectGroup {
+ pub key: ProjectGroupKey,
+ pub workspaces: Vec<Entity<Workspace>>,
+ pub expanded: bool,
+ pub visible_thread_count: Option<usize>,
}
-impl ActiveWorkspace {
- fn transient_workspace(&self) -> Option<&Entity<Workspace>> {
- match self {
- Self::Transient(workspace) => Some(workspace),
- Self::Persistent(_) => None,
- }
- }
-
- /// Sets the active workspace to transient, returning the previous
- /// transient workspace (if any).
- fn set_transient(&mut self, workspace: Entity<Workspace>) -> Option<Entity<Workspace>> {
- match std::mem::replace(self, Self::Transient(workspace)) {
- Self::Transient(old) => Some(old),
- Self::Persistent(_) => None,
- }
- }
+pub struct SerializedProjectGroupState {
+ pub key: ProjectGroupKey,
+ pub expanded: bool,
+ pub visible_thread_count: Option<usize>,
+}
- /// Sets the active workspace to persistent at the given index,
- /// returning the previous transient workspace (if any).
- fn set_persistent(&mut self, index: usize) -> Option<Entity<Workspace>> {
- match std::mem::replace(self, Self::Persistent(index)) {
- Self::Transient(workspace) => Some(workspace),
- Self::Persistent(_) => None,
- }
- }
+#[derive(Clone)]
+pub struct ProjectGroupState {
+ pub key: ProjectGroupKey,
+ pub expanded: bool,
+ pub visible_thread_count: Option<usize>,
}
pub struct MultiWorkspace {
window_id: WindowId,
- workspaces: Vec<Entity<Workspace>>,
- active_workspace: ActiveWorkspace,
- project_group_keys: Vec<ProjectGroupKey>,
- workspace_group_keys: HashMap<EntityId, ProjectGroupKey>,
+ retained_workspaces: Vec<Entity<Workspace>>,
+ project_groups: Vec<ProjectGroupState>,
+ active_workspace: Entity<Workspace>,
sidebar: Option<Box<dyn SidebarHandle>>,
sidebar_open: bool,
sidebar_overlay: Option<AnyView>,
@@ -362,10 +338,9 @@ impl MultiWorkspace {
});
Self {
window_id: window.window_handle().window_id(),
- project_group_keys: Vec::new(),
- workspace_group_keys: HashMap::default(),
- workspaces: Vec::new(),
- active_workspace: ActiveWorkspace::Transient(workspace),
+ retained_workspaces: Vec::new(),
+ project_groups: Vec::new(),
+ active_workspace: workspace,
sidebar: None,
sidebar_open: false,
sidebar_overlay: None,
@@ -482,13 +457,9 @@ impl MultiWorkspace {
pub fn open_sidebar(&mut self, cx: &mut Context<Self>) {
self.sidebar_open = true;
- if let ActiveWorkspace::Transient(workspace) = &self.active_workspace {
- let workspace = workspace.clone();
- let index = self.promote_transient(workspace, cx);
- self.active_workspace = ActiveWorkspace::Persistent(index);
- }
+ self.retain_active_workspace(cx);
let sidebar_focus_handle = self.sidebar.as_ref().map(|s| s.focus_handle(cx));
- for workspace in self.workspaces.iter() {
+ for workspace in self.retained_workspaces.clone() {
workspace.update(cx, |workspace, _cx| {
workspace.set_sidebar_focus_handle(sidebar_focus_handle.clone());
});
@@ -499,7 +470,7 @@ impl MultiWorkspace {
pub fn close_sidebar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.sidebar_open = false;
- for workspace in self.workspaces.iter() {
+ for workspace in self.retained_workspaces.clone() {
workspace.update(cx, |workspace, _cx| {
workspace.set_sidebar_focus_handle(None);
});
@@ -567,11 +538,16 @@ impl MultiWorkspace {
cx.subscribe_in(&project, window, {
let workspace = workspace.downgrade();
move |this, _project, event, _window, cx| match event {
- project::Event::WorktreeAdded(_)
- | project::Event::WorktreeRemoved(_)
- | project::Event::WorktreeUpdatedRootRepoCommonDir(_) => {
+ project::Event::WorktreePathsChanged { old_worktree_paths } => {
if let Some(workspace) = workspace.upgrade() {
- this.handle_workspace_key_change(&workspace, cx);
+ let host = workspace
+ .read(cx)
+ .project()
+ .read(cx)
+ .remote_connection_options(cx);
+ let old_key =
+ ProjectGroupKey::from_worktree_paths(old_worktree_paths, host);
+ this.handle_project_group_key_change(&workspace, &old_key, cx);
}
}
_ => {}
@@ -587,142 +563,157 @@ impl MultiWorkspace {
.detach();
}
- fn handle_workspace_key_change(
+ fn handle_project_group_key_change(
&mut self,
workspace: &Entity<Workspace>,
+ old_key: &ProjectGroupKey,
cx: &mut Context<Self>,
) {
- let workspace_id = workspace.entity_id();
- let old_key = self.project_group_key_for_workspace(workspace, cx);
- let new_key = workspace.read(cx).project_group_key(cx);
+ if !self.is_workspace_retained(workspace) {
+ return;
+ }
- if new_key.path_list().paths().is_empty() || old_key == new_key {
+ let new_key = workspace.read(cx).project_group_key(cx);
+ if new_key.path_list().paths().is_empty() {
return;
}
- let active_workspace = self.workspace().clone();
+ // Re-key the group without emitting ProjectGroupKeyUpdated —
+ // the Project already emitted WorktreePathsChanged which the
+ // sidebar handles for thread migration.
+ self.rekey_project_group(old_key, &new_key, cx);
+ self.serialize(cx);
+ cx.notify();
+ }
- self.set_workspace_group_key(workspace, new_key.clone());
+ pub fn is_workspace_retained(&self, workspace: &Entity<Workspace>) -> bool {
+ self.retained_workspaces
+ .iter()
+ .any(|retained| retained == workspace)
+ }
- let changed_root_paths = workspace.read(cx).root_paths(cx);
- let old_paths = old_key.path_list().paths();
- let new_paths = new_key.path_list().paths();
+ pub fn active_workspace_is_retained(&self) -> bool {
+ self.is_workspace_retained(&self.active_workspace)
+ }
- // Remove workspaces that already had the new key and have the same
- // root paths (true duplicates that this workspace is replacing).
- //
- // NOTE: These are dropped without prompting for unsaved changes because
- // the user explicitly added a folder that makes this workspace
- // identical to the duplicate — they are intentionally overwriting it.
- let duplicate_workspaces: Vec<Entity<Workspace>> = self
- .workspaces
- .iter()
- .filter(|ws| {
- ws.entity_id() != workspace_id
- && self.project_group_key_for_workspace(ws, cx) == new_key
- && ws.read(cx).root_paths(cx) == changed_root_paths
- })
- .cloned()
- .collect();
+ pub fn retained_workspaces(&self) -> &[Entity<Workspace>] {
+ &self.retained_workspaces
+ }
- if duplicate_workspaces.contains(&active_workspace) {
- // The active workspace is among the duplicates — drop the
- // incoming workspace instead so the user stays where they are.
- self.detach_workspace(workspace, cx);
- self.workspaces.retain(|w| w != workspace);
- } else {
- for ws in &duplicate_workspaces {
- self.detach_workspace(ws, cx);
- self.workspaces.retain(|w| w != ws);
- }
+ /// Ensures a project group exists for `key`, creating one if needed.
+ fn ensure_project_group_state(&mut self, key: ProjectGroupKey) {
+ if key.path_list().paths().is_empty() {
+ return;
}
- // Propagate folder adds/removes to linked worktree siblings
- // (different root paths, same old key) so they stay in the group.
- let group_workspaces: Vec<Entity<Workspace>> = self
- .workspaces
- .iter()
- .filter(|ws| {
- ws.entity_id() != workspace_id
- && self.project_group_key_for_workspace(ws, cx) == old_key
- })
- .cloned()
- .collect();
+ if self.project_groups.iter().any(|group| group.key == key) {
+ return;
+ }
- for workspace in &group_workspaces {
- // Pre-set this to stop later WorktreeAdded events from triggering
- self.set_workspace_group_key(&workspace, new_key.clone());
+ self.project_groups.insert(
+ 0,
+ ProjectGroupState {
+ key,
+ expanded: true,
+ visible_thread_count: None,
+ },
+ );
+ }
- let project = workspace.read(cx).project().clone();
+ /// Transitions a project group from `old_key` to `new_key`.
+ ///
+ /// On collision (both keys have groups), the active workspace's
+ /// Re-keys a project group from `old_key` to `new_key`, handling
+ /// collisions. When two groups collide, the active workspace's
+ /// group always wins. Otherwise the old key's state is preserved
+ /// — it represents the group the user or system just acted on.
+ /// The losing group is removed, and the winner is re-keyed in
+ /// place to preserve sidebar order.
+ fn rekey_project_group(
+ &mut self,
+ old_key: &ProjectGroupKey,
+ new_key: &ProjectGroupKey,
+ cx: &App,
+ ) {
+ if old_key == new_key {
+ return;
+ }
- for added_path in new_paths.iter().filter(|p| !old_paths.contains(p)) {
- project
- .update(cx, |project, cx| {
- project.find_or_create_worktree(added_path, true, cx)
- })
- .detach_and_log_err(cx);
- }
+ if new_key.path_list().paths().is_empty() {
+ return;
+ }
- for removed_path in old_paths.iter().filter(|p| !new_paths.contains(p)) {
- project.update(cx, |project, cx| {
- project.remove_worktree_for_main_worktree_path(removed_path, cx);
- });
- }
+ let old_key_exists = self.project_groups.iter().any(|g| g.key == *old_key);
+ let new_key_exists = self.project_groups.iter().any(|g| g.key == *new_key);
+
+ if !old_key_exists {
+ self.ensure_project_group_state(new_key.clone());
+ return;
}
- // Restore the active workspace after removals may have shifted
- // the index. If the previously active workspace was removed,
- // fall back to the workspace whose key just changed.
- if let ActiveWorkspace::Persistent(_) = &self.active_workspace {
- let target = if self.workspaces.contains(&active_workspace) {
- &active_workspace
+ if new_key_exists {
+ let active_key = self.active_workspace.read(cx).project_group_key(cx);
+ if active_key == *new_key {
+ self.project_groups.retain(|g| g.key != *old_key);
} else {
- workspace
- };
- if let Some(new_index) = self.workspaces.iter().position(|ws| ws == target) {
- self.active_workspace = ActiveWorkspace::Persistent(new_index);
+ self.project_groups.retain(|g| g.key != *new_key);
+ if let Some(group) = self.project_groups.iter_mut().find(|g| g.key == *old_key) {
+ group.key = new_key.clone();
+ }
+ }
+ } else {
+ if let Some(group) = self.project_groups.iter_mut().find(|g| g.key == *old_key) {
+ group.key = new_key.clone();
}
}
+ }
- self.remove_stale_project_group_keys(cx);
+ /// Re-keys a project group and emits `ProjectGroupKeyUpdated` so
+ /// the sidebar can migrate thread metadata. Used for direct group
+ /// manipulation (add/remove folder) where no Project event fires.
+ fn update_project_group_key(
+ &mut self,
+ old_key: &ProjectGroupKey,
+ new_key: &ProjectGroupKey,
+ cx: &mut Context<Self>,
+ ) {
+ self.rekey_project_group(old_key, new_key, cx);
- let old_main_paths = old_key.path_list().clone();
- for added_path in new_paths.iter().filter(|p| !old_paths.contains(p)) {
- cx.emit(MultiWorkspaceEvent::WorktreePathAdded {
- old_main_paths: old_main_paths.clone(),
- added_path: added_path.clone(),
- });
- }
- for removed_path in old_paths.iter().filter(|p| !new_paths.contains(p)) {
- cx.emit(MultiWorkspaceEvent::WorktreePathRemoved {
- old_main_paths: old_main_paths.clone(),
- removed_path: removed_path.clone(),
+ if old_key != new_key && !new_key.path_list().paths().is_empty() {
+ cx.emit(MultiWorkspaceEvent::ProjectGroupKeyUpdated {
+ old_key: old_key.clone(),
+ new_key: new_key.clone(),
});
}
-
- self.serialize(cx);
- cx.notify();
}
- fn add_project_group_key(&mut self, project_group_key: ProjectGroupKey) {
- if project_group_key.path_list().paths().is_empty() {
- return;
- }
- if self.project_group_keys.contains(&project_group_key) {
+ pub(crate) fn retain_workspace(
+ &mut self,
+ workspace: Entity<Workspace>,
+ key: ProjectGroupKey,
+ cx: &mut Context<Self>,
+ ) {
+ self.ensure_project_group_state(key);
+ if self.is_workspace_retained(&workspace) {
return;
}
- // Store newest first so the vec is in "most recently added"
- self.project_group_keys.insert(0, project_group_key);
+
+ self.retained_workspaces.push(workspace.clone());
+ cx.emit(MultiWorkspaceEvent::WorkspaceAdded(workspace));
}
- pub(crate) fn set_workspace_group_key(
+ fn register_workspace(
&mut self,
workspace: &Entity<Workspace>,
- project_group_key: ProjectGroupKey,
+ window: &Window,
+ cx: &mut Context<Self>,
) {
- self.workspace_group_keys
- .insert(workspace.entity_id(), project_group_key.clone());
- self.add_project_group_key(project_group_key);
+ Self::subscribe_to_workspace(workspace, window, cx);
+ self.sync_sidebar_to_workspace(workspace, cx);
+ let weak_self = cx.weak_entity();
+ workspace.update(cx, |workspace, cx| {
+ workspace.set_multi_workspace(weak_self, cx);
+ });
}
pub fn project_group_key_for_workspace(
@@ -730,92 +721,139 @@ impl MultiWorkspace {
workspace: &Entity<Workspace>,
cx: &App,
) -> ProjectGroupKey {
- self.workspace_group_keys
- .get(&workspace.entity_id())
- .cloned()
- .unwrap_or_else(|| workspace.read(cx).project_group_key(cx))
- }
-
- fn remove_stale_project_group_keys(&mut self, cx: &App) {
- let workspace_keys: HashSet<ProjectGroupKey> = self
- .workspaces
- .iter()
- .map(|workspace| self.project_group_key_for_workspace(workspace, cx))
- .collect();
- self.project_group_keys
- .retain(|key| workspace_keys.contains(key));
+ workspace.read(cx).project_group_key(cx)
}
- pub fn restore_project_group_keys(&mut self, keys: Vec<ProjectGroupKey>) {
- let mut restored: Vec<ProjectGroupKey> = Vec::with_capacity(keys.len());
- for key in keys {
+ pub fn restore_project_groups(
+ &mut self,
+ groups: Vec<SerializedProjectGroupState>,
+ _cx: &mut Context<Self>,
+ ) {
+ let mut restored: Vec<ProjectGroupState> = Vec::new();
+ for SerializedProjectGroupState {
+ key,
+ expanded,
+ visible_thread_count,
+ } in groups
+ {
if key.path_list().paths().is_empty() {
continue;
}
- if !restored.contains(&key) {
- restored.push(key);
+ if restored.iter().any(|group| group.key == key) {
+ continue;
}
+ restored.push(ProjectGroupState {
+ key,
+ expanded,
+ visible_thread_count,
+ });
}
- for existing_key in &self.project_group_keys {
- if !restored.contains(existing_key) {
- restored.push(existing_key.clone());
+ for existing in std::mem::take(&mut self.project_groups) {
+ if !restored.iter().any(|group| group.key == existing.key) {
+ restored.push(existing);
}
}
- self.project_group_keys = restored;
+ self.project_groups = restored;
}
- pub fn project_group_keys(&self) -> impl Iterator<Item = &ProjectGroupKey> {
- self.project_group_keys.iter()
+ pub fn project_group_keys(&self) -> Vec<ProjectGroupKey> {
+ self.project_groups
+ .iter()
+ .map(|group| group.key.clone())
+ .collect()
}
- /// Returns the project groups, ordered by most recently added.
- pub fn project_groups(
- &self,
- cx: &App,
- ) -> impl Iterator<Item = (ProjectGroupKey, Vec<Entity<Workspace>>)> {
- let mut groups = self
- .project_group_keys
+ fn derived_project_groups(&self, cx: &App) -> Vec<ProjectGroup> {
+ self.project_groups
.iter()
- .map(|key| (key.clone(), Vec::new()))
- .collect::<Vec<_>>();
- for workspace in &self.workspaces {
- let key = self.project_group_key_for_workspace(workspace, cx);
- if let Some((_, workspaces)) = groups.iter_mut().find(|(k, _)| k == &key) {
- workspaces.push(workspace.clone());
- }
+ .map(|group| ProjectGroup {
+ key: group.key.clone(),
+ workspaces: self
+ .retained_workspaces
+ .iter()
+ .filter(|workspace| workspace.read(cx).project_group_key(cx) == group.key)
+ .cloned()
+ .collect(),
+ expanded: group.expanded,
+ visible_thread_count: group.visible_thread_count,
+ })
+ .collect()
+ }
+
+ pub fn project_groups(&self, cx: &App) -> Vec<ProjectGroup> {
+ self.derived_project_groups(cx)
+ }
+
+ pub fn group_state_by_key(&self, key: &ProjectGroupKey) -> Option<&ProjectGroupState> {
+ self.project_groups.iter().find(|group| group.key == *key)
+ }
+
+ pub fn group_state_by_key_mut(
+ &mut self,
+ key: &ProjectGroupKey,
+ ) -> Option<&mut ProjectGroupState> {
+ self.project_groups
+ .iter_mut()
+ .find(|group| group.key == *key)
+ }
+
+ pub fn set_all_groups_expanded(&mut self, expanded: bool) {
+ for group in &mut self.project_groups {
+ group.expanded = expanded;
+ }
+ }
+
+ pub fn set_all_groups_visible_thread_count(&mut self, count: Option<usize>) {
+ for group in &mut self.project_groups {
+ group.visible_thread_count = count;
}
- groups.into_iter()
}
pub fn workspaces_for_project_group(
&self,
- project_group_key: &ProjectGroupKey,
+ key: &ProjectGroupKey,
cx: &App,
- ) -> impl Iterator<Item = &Entity<Workspace>> {
- self.workspaces.iter().filter(move |workspace| {
- self.project_group_key_for_workspace(workspace, cx) == *project_group_key
+ ) -> Option<Vec<Entity<Workspace>>> {
+ let has_group = self.project_groups.iter().any(|group| group.key == *key)
+ || self
+ .retained_workspaces
+ .iter()
+ .any(|workspace| workspace.read(cx).project_group_key(cx) == *key);
+
+ has_group.then(|| {
+ self.retained_workspaces
+ .iter()
+ .filter(|workspace| workspace.read(cx).project_group_key(cx) == *key)
+ .cloned()
+ .collect()
})
}
pub fn remove_folder_from_project_group(
&mut self,
- project_group_key: &ProjectGroupKey,
+ group_key: &ProjectGroupKey,
path: &Path,
cx: &mut Context<Self>,
) {
- let new_path_list = project_group_key.path_list().without_path(path);
+ let workspaces = self
+ .workspaces_for_project_group(group_key, cx)
+ .unwrap_or_default();
+
+ let Some(group) = self
+ .project_groups
+ .iter()
+ .find(|group| group.key == *group_key)
+ else {
+ return;
+ };
+
+ let new_path_list = group.key.path_list().without_path(path);
if new_path_list.is_empty() {
return;
}
- let new_key = ProjectGroupKey::new(project_group_key.host(), new_path_list);
-
- let workspaces: Vec<_> = self
- .workspaces_for_project_group(project_group_key, cx)
- .cloned()
- .collect();
-
- self.add_project_group_key(new_key);
+ let new_key = ProjectGroupKey::new(group.key.host(), new_path_list);
+ self.update_project_group_key(group_key, &new_key, cx);
for workspace in workspaces {
let project = workspace.read(cx).project().clone();
@@ -830,7 +868,7 @@ impl MultiWorkspace {
pub fn prompt_to_add_folders_to_project_group(
&mut self,
- key: &ProjectGroupKey,
+ group_key: ProjectGroupKey,
window: &mut Window,
cx: &mut Context<Self>,
) {
@@ -848,12 +886,11 @@ impl MultiWorkspace {
)
});
- let key = key.clone();
cx.spawn_in(window, async move |this, cx| {
if let Some(new_paths) = paths.await.ok().flatten() {
if !new_paths.is_empty() {
this.update(cx, |multi_workspace, cx| {
- multi_workspace.add_folders_to_project_group(&key, new_paths, cx);
+ multi_workspace.add_folders_to_project_group(&group_key, new_paths, cx);
})?;
}
}
@@ -864,21 +901,38 @@ impl MultiWorkspace {
pub fn add_folders_to_project_group(
&mut self,
- project_group_key: &ProjectGroupKey,
+ group_key: &ProjectGroupKey,
new_paths: Vec<PathBuf>,
cx: &mut Context<Self>,
) {
- let mut all_paths: Vec<PathBuf> = project_group_key.path_list().paths().to_vec();
- all_paths.extend(new_paths.iter().cloned());
- let new_path_list = PathList::new(&all_paths);
- let new_key = ProjectGroupKey::new(project_group_key.host(), new_path_list);
+ let workspaces = self
+ .workspaces_for_project_group(group_key, cx)
+ .unwrap_or_default();
- let workspaces: Vec<_> = self
- .workspaces_for_project_group(project_group_key, cx)
- .cloned()
+ let Some(group) = self
+ .project_groups
+ .iter()
+ .find(|group| group.key == *group_key)
+ else {
+ return;
+ };
+
+ let existing_paths = group.key.path_list().paths();
+ let new_paths: Vec<PathBuf> = new_paths
+ .into_iter()
+ .filter(|p| !existing_paths.contains(p))
.collect();
- self.add_project_group_key(new_key);
+ if new_paths.is_empty() {
+ return;
+ }
+
+ let mut all_paths: Vec<PathBuf> = existing_paths.to_vec();
+ all_paths.extend(new_paths.iter().cloned());
+ let new_path_list = PathList::new(&all_paths);
+ let new_key = ProjectGroupKey::new(group.key.host(), new_path_list);
+
+ self.update_project_group_key(group_key, &new_key, cx);
for workspace in workspaces {
let project = workspace.read(cx).project().clone();
@@ -897,31 +951,28 @@ impl MultiWorkspace {
pub fn remove_project_group(
&mut self,
- key: &ProjectGroupKey,
+ group_key: &ProjectGroupKey,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<Result<bool>> {
- let workspaces: Vec<_> = self
- .workspaces_for_project_group(key, cx)
- .cloned()
- .collect();
-
- // Compute the neighbor while the key is still in the list.
- let neighbor_key = {
- let pos = self.project_group_keys.iter().position(|k| k == key);
- pos.and_then(|pos| {
- // Keys are in display order, so pos+1 is below
- // and pos-1 is above. Try below first.
- self.project_group_keys.get(pos + 1).or_else(|| {
- pos.checked_sub(1)
- .and_then(|i| self.project_group_keys.get(i))
- })
- })
- .cloned()
- };
+ let pos = self
+ .project_groups
+ .iter()
+ .position(|group| group.key == *group_key);
+ let workspaces = self
+ .workspaces_for_project_group(group_key, cx)
+ .unwrap_or_default();
+
+ // Compute the neighbor while the group is still in the list.
+ let neighbor_key = pos.and_then(|pos| {
+ self.project_groups
+ .get(pos + 1)
+ .or_else(|| pos.checked_sub(1).and_then(|i| self.project_groups.get(i)))
+ .map(|group| group.key.clone())
+ });
- // Now remove the key.
- self.project_group_keys.retain(|k| k != key);
+ // Now remove the group.
+ self.project_groups.retain(|group| group.key != *group_key);
self.remove(
workspaces,
@@ -962,14 +1013,17 @@ impl MultiWorkspace {
host: Option<&RemoteConnectionOptions>,
cx: &App,
) -> Option<Entity<Workspace>> {
- self.workspaces
- .iter()
- .find(|ws| {
- let key = ws.read(cx).project_group_key(cx);
- key.host().as_ref() == host
- && PathList::new(&ws.read(cx).root_paths(cx)) == *path_list
- })
- .cloned()
+ for workspace in self.workspaces() {
+ let root_paths = PathList::new(&workspace.read(cx).root_paths(cx));
+ let key = workspace.read(cx).project_group_key(cx);
+ let host_matches = key.host().as_ref() == host;
+ let paths_match = root_paths == *path_list;
+ if host_matches && paths_match {
+ return Some(workspace.clone());
+ }
+ }
+
+ None
}
/// Finds an existing workspace whose paths match, or creates a new one.
@@ -1068,12 +1122,6 @@ impl MultiWorkspace {
return Task::ready(Ok(workspace));
}
- if let Some(transient) = self.active_workspace.transient_workspace() {
- if transient.read(cx).project_group_key(cx).path_list() == &path_list {
- return Task::ready(Ok(transient.clone()));
- }
- }
-
let paths = path_list.paths().to_vec();
let app_state = self.workspace().read(cx).app_state().clone();
let requesting_window = window.window_handle().downcast::<MultiWorkspace>();
@@ -1097,16 +1145,14 @@ impl MultiWorkspace {
}
pub fn workspace(&self) -> &Entity<Workspace> {
- match &self.active_workspace {
- ActiveWorkspace::Persistent(index) => &self.workspaces[*index],
- ActiveWorkspace::Transient(workspace) => workspace,
- }
+ &self.active_workspace
}
pub fn workspaces(&self) -> impl Iterator<Item = &Entity<Workspace>> {
- self.workspaces
+ let active_is_retained = self.is_workspace_retained(&self.active_workspace);
+ self.retained_workspaces
.iter()
- .chain(self.active_workspace.transient_workspace())
+ .chain(std::iter::once(&self.active_workspace).filter(move |_| !active_is_retained))
}
/// Adds a workspace to this window as persistent without changing which
@@ -1114,7 +1160,17 @@ impl MultiWorkspace {
/// persistent list regardless of sidebar state — it's used for system-
/// initiated additions like deserialization and worktree discovery.
pub fn add(&mut self, workspace: Entity<Workspace>, window: &Window, cx: &mut Context<Self>) {
- self.insert_workspace(workspace, window, cx);
+ if self.is_workspace_retained(&workspace) {
+ return;
+ }
+
+ if workspace != self.active_workspace {
+ self.register_workspace(&workspace, window, cx);
+ }
+
+ let key = workspace.read(cx).project_group_key(cx);
+ self.retain_workspace(workspace, key, cx);
+ cx.notify();
}
/// Ensures the workspace is in the multiworkspace and makes it the active one.
@@ -1124,41 +1180,30 @@ impl MultiWorkspace {
window: &mut Window,
cx: &mut Context<Self>,
) {
- // Re-activating the current workspace is a no-op.
if self.workspace() == &workspace {
self.focus_active_workspace(window, cx);
return;
}
- // Resolve where we're going.
- let new_index = if let Some(index) = self.workspaces.iter().position(|w| *w == workspace) {
- Some(index)
- } else if self.sidebar_open {
- Some(self.insert_workspace(workspace.clone(), &*window, cx))
- } else {
- None
- };
+ let old_active_workspace = self.active_workspace.clone();
+ let old_active_was_retained = self.active_workspace_is_retained();
+ let workspace_was_retained = self.is_workspace_retained(&workspace);
- // Transition the active workspace.
- if let Some(index) = new_index {
- if let Some(old) = self.active_workspace.set_persistent(index) {
- if self.sidebar_open {
- self.promote_transient(old, cx);
- } else {
- self.detach_workspace(&old, cx);
- }
- }
- } else {
- Self::subscribe_to_workspace(&workspace, window, cx);
- let weak_self = cx.weak_entity();
- workspace.update(cx, |workspace, cx| {
- workspace.set_multi_workspace(weak_self, cx);
- });
- if let Some(old) = self.active_workspace.set_transient(workspace) {
- self.detach_workspace(&old, cx);
+ if !workspace_was_retained {
+ self.register_workspace(&workspace, window, cx);
+
+ if self.sidebar_open {
+ let key = workspace.read(cx).project_group_key(cx);
+ self.retain_workspace(workspace.clone(), key, cx);
}
}
+ self.active_workspace = workspace;
+
+ if !self.sidebar_open && !old_active_was_retained {
+ self.detach_workspace(&old_active_workspace, cx);
+ }
+
cx.emit(MultiWorkspaceEvent::ActiveWorkspaceChanged);
self.serialize(cx);
self.focus_active_workspace(window, cx);
@@ -1169,77 +1214,42 @@ impl MultiWorkspace {
/// transient, so it is retained across workspace switches even when
/// the sidebar is closed. No-op if the workspace is already persistent.
pub fn retain_active_workspace(&mut self, cx: &mut Context<Self>) {
- if let ActiveWorkspace::Transient(workspace) = &self.active_workspace {
- let workspace = workspace.clone();
- let index = self.promote_transient(workspace, cx);
- self.active_workspace = ActiveWorkspace::Persistent(index);
- self.serialize(cx);
- cx.notify();
+ let workspace = self.active_workspace.clone();
+ if self.is_workspace_retained(&workspace) {
+ return;
}
- }
- /// Promotes a former transient workspace into the persistent list.
- /// Returns the index of the newly inserted workspace.
- fn promote_transient(&mut self, workspace: Entity<Workspace>, cx: &mut Context<Self>) -> usize {
- let project_group_key = self.project_group_key_for_workspace(&workspace, cx);
- self.set_workspace_group_key(&workspace, project_group_key);
- self.workspaces.push(workspace.clone());
- cx.emit(MultiWorkspaceEvent::WorkspaceAdded(workspace));
- self.workspaces.len() - 1
+ let key = workspace.read(cx).project_group_key(cx);
+ self.retain_workspace(workspace, key, cx);
+ self.serialize(cx);
+ cx.notify();
}
- /// Collapses to a single transient workspace, discarding all persistent
- /// workspaces. Used when multi-workspace is disabled (e.g. disable_ai).
+ /// Collapses to a single workspace, discarding all groups.
+ /// Used when multi-workspace is disabled (e.g. disable_ai).
fn collapse_to_single_workspace(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.sidebar_open {
self.close_sidebar(window, cx);
}
- let active = self.workspace().clone();
- for workspace in std::mem::take(&mut self.workspaces) {
- if workspace != active {
+
+ let active_workspace = self.active_workspace.clone();
+ for workspace in self.retained_workspaces.clone() {
+ if workspace != active_workspace {
self.detach_workspace(&workspace, cx);
}
}
- self.project_group_keys.clear();
- self.workspace_group_keys.clear();
- self.active_workspace = ActiveWorkspace::Transient(active);
- cx.notify();
- }
- /// Inserts a workspace into the list if not already present. Returns the
- /// index of the workspace (existing or newly inserted). Does not change
- /// the active workspace index.
- fn insert_workspace(
- &mut self,
- workspace: Entity<Workspace>,
- window: &Window,
- cx: &mut Context<Self>,
- ) -> usize {
- if let Some(index) = self.workspaces.iter().position(|w| *w == workspace) {
- index
- } else {
- let project_group_key = self.project_group_key_for_workspace(&workspace, cx);
-
- Self::subscribe_to_workspace(&workspace, window, cx);
- self.sync_sidebar_to_workspace(&workspace, cx);
- let weak_self = cx.weak_entity();
- workspace.update(cx, |workspace, cx| {
- workspace.set_multi_workspace(weak_self, cx);
- });
-
- self.set_workspace_group_key(&workspace, project_group_key);
- self.workspaces.push(workspace.clone());
- cx.emit(MultiWorkspaceEvent::WorkspaceAdded(workspace));
- cx.notify();
- self.workspaces.len() - 1
- }
+ self.retained_workspaces.clear();
+ self.project_groups.clear();
+ cx.notify();
}
/// Detaches a workspace: clears session state, DB binding, cached
/// group key, and emits `WorkspaceRemoved`. The DB row is preserved
/// so the workspace still appears in the recent-projects list.
fn detach_workspace(&mut self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
- self.workspace_group_keys.remove(&workspace.entity_id());
+ self.retained_workspaces
+ .retain(|retained| retained != workspace);
cx.emit(MultiWorkspaceEvent::WorkspaceRemoved(workspace.entity_id()));
workspace.update(cx, |workspace, _cx| {
workspace.session_id.take();
@@ -1268,16 +1278,22 @@ impl MultiWorkspace {
}
}
- pub(crate) fn serialize(&mut self, cx: &mut Context<Self>) {
+ pub fn serialize(&mut self, cx: &mut Context<Self>) {
self._serialize_task = Some(cx.spawn(async move |this, cx| {
let Some((window_id, state)) = this
.read_with(cx, |this, cx| {
let state = MultiWorkspaceState {
active_workspace_id: this.workspace().read(cx).database_id(),
- project_group_keys: this
- .project_group_keys()
- .cloned()
- .map(Into::into)
+ project_groups: this
+ .project_groups
+ .iter()
+ .map(|group| {
+ crate::persistence::model::SerializedProjectGroup::from_group(
+ &group.key,
+ group.expanded,
+ group.visible_thread_count,
+ )
+ })
.collect::<Vec<_>>(),
sidebar_open: this.sidebar_open,
sidebar_state: this.sidebar.as_ref().and_then(|s| s.serialized_state(cx)),
@@ -1415,42 +1431,31 @@ impl MultiWorkspace {
}
#[cfg(any(test, feature = "test-support"))]
- pub fn assert_project_group_key_integrity(&self, cx: &App) -> anyhow::Result<()> {
- let stored_keys: HashSet<&ProjectGroupKey> = self.project_group_keys().collect();
-
- let workspace_group_keys: HashSet<&ProjectGroupKey> =
- self.workspace_group_keys.values().collect();
- let extra_keys = &workspace_group_keys - &stored_keys;
- anyhow::ensure!(
- extra_keys.is_empty(),
- "workspace_group_keys values not in project_group_keys: {:?}",
- extra_keys,
- );
+ pub fn test_expand_all_groups(&mut self) {
+ self.set_all_groups_expanded(true);
+ self.set_all_groups_visible_thread_count(Some(10_000));
+ }
- let cached_ids: HashSet<EntityId> = self.workspace_group_keys.keys().copied().collect();
- let workspace_ids: HashSet<EntityId> =
- self.workspaces.iter().map(|ws| ws.entity_id()).collect();
- anyhow::ensure!(
- cached_ids == workspace_ids,
- "workspace_group_keys entity IDs don't match workspaces.\n\
- only in cache: {:?}\n\
- only in workspaces: {:?}",
- &cached_ids - &workspace_ids,
- &workspace_ids - &cached_ids,
- );
+ #[cfg(any(test, feature = "test-support"))]
+ pub fn assert_project_group_key_integrity(&self, cx: &App) -> anyhow::Result<()> {
+ let mut retained_ids: collections::HashSet<EntityId> = Default::default();
+ for workspace in &self.retained_workspaces {
+ anyhow::ensure!(
+ retained_ids.insert(workspace.entity_id()),
+ "workspace {:?} is retained more than once",
+ workspace.entity_id(),
+ );
- for workspace in self.workspaces() {
let live_key = workspace.read(cx).project_group_key(cx);
- let cached_key = &self.workspace_group_keys[&workspace.entity_id()];
anyhow::ensure!(
- *cached_key == live_key,
- "workspace {:?} has live key {:?} but cached key {:?}",
+ self.project_groups
+ .iter()
+ .any(|group| group.key == live_key),
+ "workspace {:?} has live key {:?} but no project-group metadata",
workspace.entity_id(),
live_key,
- cached_key,
);
}
-
Ok(())
}
@@ -1479,6 +1484,15 @@ impl MultiWorkspace {
workspace
}
+ #[cfg(any(test, feature = "test-support"))]
+ pub fn test_add_project_group(&mut self, group: ProjectGroup) {
+ self.project_groups.push(ProjectGroupState {
+ key: group.key,
+ expanded: group.expanded,
+ visible_thread_count: group.visible_thread_count,
+ });
+ }
+
#[cfg(any(test, feature = "test-support"))]
pub fn create_test_workspace(
&mut self,
@@ -1524,6 +1538,45 @@ impl MultiWorkspace {
})
}
+ /// Assigns random database IDs to all retained workspaces, flushes
+ /// workspace serialization (SQLite) and multi-workspace state (KVP),
+ /// and writes session bindings so the serialized data can be read
+ /// back by `last_session_workspace_locations` +
+ /// `read_serialized_multi_workspaces`.
+ #[cfg(any(test, feature = "test-support"))]
+ pub fn flush_all_serialization(
+ &mut self,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) -> Vec<Task<()>> {
+ for workspace in self.workspaces() {
+ workspace.update(cx, |ws, _cx| {
+ if ws.database_id().is_none() {
+ ws.set_random_database_id();
+ }
+ });
+ }
+
+ let session_id = self.workspace().read(cx).session_id();
+ let window_id_u64 = window.window_handle().window_id().as_u64();
+
+ let mut tasks: Vec<Task<()>> = Vec::new();
+ for workspace in self.workspaces() {
+ tasks.push(workspace.update(cx, |ws, cx| ws.flush_serialization(window, cx)));
+ if let Some(db_id) = workspace.read(cx).database_id() {
+ let db = crate::persistence::WorkspaceDb::global(cx);
+ let session_id = session_id.clone();
+ tasks.push(cx.background_spawn(async move {
+ db.set_session_binding(db_id, session_id, Some(window_id_u64))
+ .await
+ .log_err();
+ }));
+ }
+ }
+ self.serialize(cx);
+ tasks
+ }
+
/// Removes one or more workspaces from this multi-workspace.
///
/// If the active workspace is among those being removed,
@@ -1595,37 +1648,17 @@ impl MultiWorkspace {
// Actually remove the workspaces.
this.update_in(cx, |this, _, cx| {
- // Save a handle to the active workspace so we can restore
- // its index after the removals shift the vec around.
- let active_workspace = this.workspace().clone();
-
- let mut removed_workspaces: Vec<Entity<Workspace>> = Vec::new();
-
- this.workspaces.retain(|ws| {
- if workspaces.contains(ws) {
- removed_workspaces.push(ws.clone());
- false
- } else {
- true
- }
- });
+ let mut removed_any = false;
- for workspace in &removed_workspaces {
- this.detach_workspace(workspace, cx);
+ for workspace in &workspaces {
+ let was_retained = this.is_workspace_retained(workspace);
+ if was_retained {
+ this.detach_workspace(workspace, cx);
+ removed_any = true;
+ }
}
- let removed_any = !removed_workspaces.is_empty();
-
if removed_any {
- // Restore the active workspace index after removals.
- if let Some(new_index) = this
- .workspaces
- .iter()
- .position(|ws| ws == &active_workspace)
- {
- this.active_workspace = ActiveWorkspace::Persistent(new_index);
- }
-
this.serialize(cx);
cx.notify();
}
diff --git a/crates/workspace/src/multi_workspace_tests.rs b/crates/workspace/src/multi_workspace_tests.rs
index 5f15f6422dd..bed59580305 100644
--- a/crates/workspace/src/multi_workspace_tests.rs
+++ b/crates/workspace/src/multi_workspace_tests.rs
@@ -1,9 +1,10 @@
use std::path::PathBuf;
use super::*;
+use client::proto;
use fs::FakeFs;
use gpui::TestAppContext;
-use project::{DisableAiSettings, ProjectGroupKey};
+use project::DisableAiSettings;
use serde_json::json;
use settings::SettingsStore;
use util::path;
@@ -105,9 +106,9 @@ async fn test_project_group_keys_initial(cx: &mut TestAppContext) {
});
multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<&ProjectGroupKey> = mw.project_group_keys().collect();
+ let keys: Vec<ProjectGroupKey> = mw.project_group_keys();
assert_eq!(keys.len(), 1, "should have exactly one key on creation");
- assert_eq!(*keys[0], expected_key);
+ assert_eq!(keys[0], expected_key);
});
}
@@ -135,7 +136,7 @@ async fn test_project_group_keys_add_workspace(cx: &mut TestAppContext) {
});
multi_workspace.read_with(cx, |mw, _cx| {
- assert_eq!(mw.project_group_keys().count(), 1);
+ assert_eq!(mw.project_group_keys().len(), 1);
});
// Adding a workspace with a different project root adds a new key.
@@ -144,14 +145,14 @@ async fn test_project_group_keys_add_workspace(cx: &mut TestAppContext) {
});
multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<&ProjectGroupKey> = mw.project_group_keys().collect();
+ let keys: Vec<ProjectGroupKey> = mw.project_group_keys();
assert_eq!(
keys.len(),
2,
"should have two keys after adding a second workspace"
);
- assert_eq!(*keys[0], key_b);
- assert_eq!(*keys[1], key_a);
+ assert_eq!(keys[0], key_b);
+ assert_eq!(keys[1], key_a);
});
}
@@ -225,7 +226,7 @@ async fn test_project_group_keys_duplicate_not_added(cx: &mut TestAppContext) {
});
multi_workspace.read_with(cx, |mw, _cx| {
- let keys: Vec<&ProjectGroupKey> = mw.project_group_keys().collect();
+ let keys: Vec<ProjectGroupKey> = mw.project_group_keys();
assert_eq!(
keys.len(),
1,
@@ -233,3 +234,365 @@ async fn test_project_group_keys_duplicate_not_added(cx: &mut TestAppContext) {
);
});
}
+
+#[gpui::test]
+async fn test_groups_with_same_paths_merge(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/a", json!({ "file.txt": "" })).await;
+ fs.insert_tree("/b", json!({ "file.txt": "" })).await;
+ let project_a = Project::test(fs.clone(), ["/a".as_ref()], cx).await;
+ let project_b = Project::test(fs.clone(), ["/b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
+
+ // Open the sidebar so workspaces get grouped.
+ multi_workspace.update(cx, |mw, cx| {
+ mw.open_sidebar(cx);
+ });
+ cx.run_until_parked();
+
+ // Add a second workspace, creating group_b with path [/b].
+ let group_a_key = multi_workspace.update_in(cx, |mw, window, cx| {
+ let group_a_key = mw.project_groups(cx)[0].key.clone();
+ mw.test_add_workspace(project_b, window, cx);
+ group_a_key
+ });
+ cx.run_until_parked();
+
+ // Now add /b to group_a so it has [/a, /b].
+ multi_workspace.update(cx, |mw, cx| {
+ mw.add_folders_to_project_group(&group_a_key, vec!["/b".into()], cx);
+ });
+ cx.run_until_parked();
+
+ // Verify we have two groups.
+ multi_workspace.read_with(cx, |mw, cx| {
+ assert_eq!(
+ mw.project_groups(cx).len(),
+ 2,
+ "should have two groups before the merge"
+ );
+ });
+
+ // After adding /b, group_a's key changed. Get the updated key.
+ let group_a_key_updated = multi_workspace.read_with(cx, |mw, cx| {
+ mw.project_groups(cx)
+ .iter()
+ .find(|g| g.key.path_list().paths().contains(&PathBuf::from("/a")))
+ .unwrap()
+ .key
+ .clone()
+ });
+
+ // Remove /a from group_a, making its key [/b] — same as group_b.
+ multi_workspace.update(cx, |mw, cx| {
+ mw.remove_folder_from_project_group(&group_a_key_updated, Path::new("/a"), cx);
+ });
+ cx.run_until_parked();
+
+ // The two groups now have identical keys [/b] and should have been merged.
+ multi_workspace.read_with(cx, |mw, cx| {
+ assert_eq!(
+ mw.project_groups(cx).len(),
+ 1,
+ "groups with identical paths should be merged into one"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_adding_worktree_updates_project_group_key(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
+ fs.insert_tree("/root_b", json!({ "other.txt": "" })).await;
+ let project = Project::test(fs.clone(), ["/root_a".as_ref()], cx).await;
+
+ let initial_key = project.read_with(cx, |p, cx| p.project_group_key(cx));
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+
+ // Open sidebar to retain the workspace and create the initial group.
+ multi_workspace.update(cx, |mw, cx| {
+ mw.open_sidebar(cx);
+ });
+ cx.run_until_parked();
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert_eq!(keys.len(), 1);
+ assert_eq!(keys[0], initial_key);
+ });
+
+ // Add a second worktree to the project. This triggers WorktreeAdded →
+ // handle_workspace_key_change, which should update the group key.
+ project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree("/root_b", true, cx)
+ })
+ .await
+ .expect("adding worktree should succeed");
+ cx.run_until_parked();
+
+ let updated_key = project.read_with(cx, |p, cx| p.project_group_key(cx));
+ assert_ne!(
+ initial_key, updated_key,
+ "adding a worktree should change the project group key"
+ );
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert!(
+ keys.contains(&updated_key),
+ "should contain the updated key; got {keys:?}"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_find_or_create_local_workspace_reuses_active_workspace_when_sidebar_closed(
+ cx: &mut TestAppContext,
+) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
+ let project = Project::test(fs, ["/root_a".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
+
+ let active_workspace = multi_workspace.read_with(cx, |mw, cx| {
+ assert!(
+ mw.project_groups(cx).is_empty(),
+ "sidebar-closed setup should start with no retained project groups"
+ );
+ mw.workspace().clone()
+ });
+ let active_workspace_id = active_workspace.entity_id();
+
+ let workspace = multi_workspace
+ .update_in(cx, |mw, window, cx| {
+ mw.find_or_create_local_workspace(
+ PathList::new(&[PathBuf::from("/root_a")]),
+ window,
+ cx,
+ )
+ })
+ .await
+ .expect("reopening the same local workspace should succeed");
+
+ assert_eq!(
+ workspace.entity_id(),
+ active_workspace_id,
+ "should reuse the current active workspace when the sidebar is closed"
+ );
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ assert_eq!(
+ mw.workspace().entity_id(),
+ active_workspace_id,
+ "active workspace should remain unchanged after reopening the same path"
+ );
+ assert_eq!(
+ mw.workspaces().count(),
+ 1,
+ "reusing the active workspace should not create a second open workspace"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_find_or_create_local_workspace_reuses_active_workspace_after_sidebar_open(
+ cx: &mut TestAppContext,
+) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
+ let project = Project::test(fs, ["/root_a".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
+
+ multi_workspace.update(cx, |mw, cx| {
+ mw.open_sidebar(cx);
+ });
+ cx.run_until_parked();
+
+ let active_workspace = multi_workspace.read_with(cx, |mw, cx| {
+ assert_eq!(
+ mw.project_groups(cx).len(),
+ 1,
+ "opening the sidebar should retain the active workspace in a project group"
+ );
+ mw.workspace().clone()
+ });
+ let active_workspace_id = active_workspace.entity_id();
+
+ let workspace = multi_workspace
+ .update_in(cx, |mw, window, cx| {
+ mw.find_or_create_local_workspace(
+ PathList::new(&[PathBuf::from("/root_a")]),
+ window,
+ cx,
+ )
+ })
+ .await
+ .expect("reopening the same retained local workspace should succeed");
+
+ assert_eq!(
+ workspace.entity_id(),
+ active_workspace_id,
+ "should reuse the retained active workspace after the sidebar is opened"
+ );
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ assert_eq!(
+ mw.workspaces().count(),
+ 1,
+ "reopening the same retained workspace should not create another workspace"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_switching_projects_with_sidebar_closed_detaches_old_active_workspace(
+ cx: &mut TestAppContext,
+) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/root_a", json!({ "file_a.txt": "" })).await;
+ fs.insert_tree("/root_b", json!({ "file_b.txt": "" })).await;
+ let project_a = Project::test(fs.clone(), ["/root_a".as_ref()], cx).await;
+ let project_b = Project::test(fs, ["/root_b".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
+
+ let workspace_a = multi_workspace.read_with(cx, |mw, cx| {
+ assert!(
+ mw.project_groups(cx).is_empty(),
+ "sidebar-closed setup should start with no retained project groups"
+ );
+ mw.workspace().clone()
+ });
+ assert!(
+ workspace_a.read_with(cx, |workspace, _cx| workspace.session_id().is_some()),
+ "initial active workspace should start attached to the session"
+ );
+
+ let workspace_b = multi_workspace.update_in(cx, |mw, window, cx| {
+ mw.test_add_workspace(project_b, window, cx)
+ });
+ cx.run_until_parked();
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ assert_eq!(
+ mw.workspace().entity_id(),
+ workspace_b.entity_id(),
+ "the new workspace should become active"
+ );
+ assert_eq!(
+ mw.workspaces().count(),
+ 1,
+ "only the new active workspace should remain open after switching with the sidebar closed"
+ );
+ });
+
+ assert!(
+ workspace_a.read_with(cx, |workspace, _cx| workspace.session_id().is_none()),
+ "the previous active workspace should be detached when switching away with the sidebar closed"
+ );
+}
+
+#[gpui::test]
+async fn test_remote_worktree_without_git_updates_project_group(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree("/local", json!({ "file.txt": "" })).await;
+ let project = Project::test(fs.clone(), ["/local".as_ref()], cx).await;
+
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+
+ multi_workspace.update(cx, |mw, cx| {
+ mw.open_sidebar(cx);
+ });
+ cx.run_until_parked();
+
+ let initial_key = project.read_with(cx, |p, cx| p.project_group_key(cx));
+ multi_workspace.read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert_eq!(keys.len(), 1);
+ assert_eq!(keys[0], initial_key);
+ });
+
+ // Add a remote worktree without git repo info.
+ let remote_worktree = project.update(cx, |project, cx| {
+ project.add_test_remote_worktree("/remote/project", cx)
+ });
+ cx.run_until_parked();
+
+ // The remote worktree has no entries yet, so project_group_key should
+ // still exclude it.
+ let key_after_add = project.read_with(cx, |p, cx| p.project_group_key(cx));
+ assert_eq!(
+ key_after_add, initial_key,
+ "remote worktree without entries should not affect the group key"
+ );
+
+ // Send an UpdateWorktree to the remote worktree with entries but no repo.
+ // This triggers UpdatedRootRepoCommonDir on the first update (the fix),
+ // which propagates through WorktreeStore → Project → MultiWorkspace.
+ let worktree_id = remote_worktree.read_with(cx, |wt, _| wt.id().to_proto());
+ remote_worktree.update(cx, |worktree, _cx| {
+ worktree
+ .as_remote()
+ .unwrap()
+ .update_from_remote(proto::UpdateWorktree {
+ project_id: 0,
+ worktree_id,
+ abs_path: "/remote/project".to_string(),
+ root_name: "project".to_string(),
+ updated_entries: vec![proto::Entry {
+ id: 1,
+ is_dir: true,
+ path: "".to_string(),
+ inode: 1,
+ mtime: Some(proto::Timestamp {
+ seconds: 0,
+ nanos: 0,
+ }),
+ is_ignored: false,
+ is_hidden: false,
+ is_external: false,
+ is_fifo: false,
+ size: None,
+ canonical_path: None,
+ }],
+ removed_entries: vec![],
+ scan_id: 1,
+ is_last_update: true,
+ updated_repositories: vec![],
+ removed_repositories: vec![],
+ root_repo_common_dir: None,
+ });
+ });
+ cx.run_until_parked();
+
+ let updated_key = project.read_with(cx, |p, cx| p.project_group_key(cx));
+ assert_ne!(
+ initial_key, updated_key,
+ "adding a remote worktree should change the project group key"
+ );
+
+ multi_workspace.read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert!(
+ keys.contains(&updated_key),
+ "should contain the updated key; got {keys:?}"
+ );
+ });
+}
diff --git a/crates/workspace/src/persistence.rs b/crates/workspace/src/persistence.rs
index 9ae44ef3db2..e6ee8fbd6b9 100644
--- a/crates/workspace/src/persistence.rs
+++ b/crates/workspace/src/persistence.rs
@@ -2495,6 +2495,7 @@ pub fn delete_unloaded_items(
#[cfg(test)]
mod tests {
use super::*;
+ use crate::ProjectGroupKey;
use crate::{
multi_workspace::MultiWorkspace,
persistence::{
@@ -2508,7 +2509,7 @@ mod tests {
use feature_flags::FeatureFlagAppExt;
use gpui::AppContext as _;
use pretty_assertions::assert_eq;
- use project::{Project, ProjectGroupKey};
+ use project::Project;
use remote::SshConnectionOptions;
use serde_json::json;
use std::{thread, time::Duration};
@@ -2567,10 +2568,14 @@ mod tests {
the newly activated workspace's database id"
);
- // --- Remove the first workspace (index 0, which is not the active one) ---
- multi_workspace.update_in(cx, |mw, window, cx| {
- let ws = mw.workspaces().nth(0).unwrap().clone();
- mw.remove([ws], |_, _, _| unreachable!(), window, cx)
+ // --- Remove the non-active workspace ---
+ multi_workspace.update_in(cx, |mw, _window, cx| {
+ let active = mw.workspace().clone();
+ let ws = mw
+ .workspaces()
+ .find(|ws| *ws != &active)
+ .expect("should have a non-active workspace");
+ mw.remove([ws.clone()], |_, _, _| unreachable!(), _window, cx)
.detach_and_log_err(cx);
});
@@ -4007,7 +4012,7 @@ mod tests {
window_10,
MultiWorkspaceState {
active_workspace_id: Some(WorkspaceId(2)),
- project_group_keys: vec![],
+ project_groups: vec![],
sidebar_open: true,
sidebar_state: None,
},
@@ -4019,7 +4024,7 @@ mod tests {
window_20,
MultiWorkspaceState {
active_workspace_id: Some(WorkspaceId(3)),
- project_group_keys: vec![],
+ project_groups: vec![],
sidebar_open: false,
sidebar_state: None,
},
@@ -4771,38 +4776,8 @@ mod tests {
mw.test_add_workspace(project_1_linked_worktree.clone(), window, cx)
});
- // Assign database IDs and set up session bindings so serialization
- // writes real rows.
- multi_workspace.update_in(cx, |mw, _, cx| {
- for workspace in mw.workspaces() {
- workspace.update(cx, |ws, _cx| {
- ws.set_random_database_id();
- });
- }
- });
-
- // Flush serialization for each individual workspace (writes to SQLite)
- // and for the MultiWorkspace (writes to KVP).
- let tasks = multi_workspace.update_in(cx, |mw, window, cx| {
- let session_id = mw.workspace().read(cx).session_id();
- let window_id_u64 = window.window_handle().window_id().as_u64();
-
- let mut tasks: Vec<Task<()>> = Vec::new();
- for workspace in mw.workspaces() {
- tasks.push(workspace.update(cx, |ws, cx| ws.flush_serialization(window, cx)));
- if let Some(db_id) = workspace.read(cx).database_id() {
- let db = WorkspaceDb::global(cx);
- let session_id = session_id.clone();
- tasks.push(cx.background_spawn(async move {
- db.set_session_binding(db_id, session_id, Some(window_id_u64))
- .await
- .log_err();
- }));
- }
- }
- mw.serialize(cx);
- tasks
- });
+ let tasks =
+ multi_workspace.update_in(cx, |mw, window, cx| mw.flush_all_serialization(window, cx));
cx.run_until_parked();
for task in tasks {
task.await;
@@ -4843,13 +4818,13 @@ mod tests {
serialized.active_workspace.workspace_id,
active_db_id.unwrap(),
);
- assert_eq!(serialized.state.project_group_keys.len(), 2,);
+ assert_eq!(serialized.state.project_groups.len(), 2,);
// Verify the serialized project group keys round-trip back to the
// originals.
let restored_keys: Vec<ProjectGroupKey> = serialized
.state
- .project_group_keys
+ .project_groups
.iter()
.cloned()
.map(Into::into)
@@ -4882,9 +4857,7 @@ mod tests {
// The restored window should have the same project group keys.
let restored_keys: Vec<ProjectGroupKey> = restored_handle
- .read_with(cx, |mw: &MultiWorkspace, _cx| {
- mw.project_group_keys().cloned().collect()
- })
+ .read_with(cx, |mw: &MultiWorkspace, _cx| mw.project_group_keys())
.unwrap();
assert_eq!(
restored_keys, expected_keys,
@@ -4928,7 +4901,7 @@ mod tests {
let project_c = Project::test(fs.clone(), [dir_c.as_path()], cx).await;
// Create a multi-workspace with project A, then add B and C.
- // project_group_keys stores newest first: [C, B, A].
+ // project_groups stores newest first: [C, B, A].
// Sidebar displays in the same order: C (top), B (middle), A (bottom).
let (multi_workspace, cx) = cx
.add_window_view(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));
@@ -4976,7 +4949,7 @@ mod tests {
// Activate workspace A (the bottom) so removing it tests the
// "fall back upward" path.
let workspace_a =
- multi_workspace.read_with(cx, |mw, _| mw.workspaces().next().cloned().unwrap());
+ multi_workspace.read_with(cx, |mw, _cx| mw.workspaces().next().unwrap().clone());
multi_workspace.update_in(cx, |mw, window, cx| {
mw.activate(workspace_a.clone(), window, cx);
});
diff --git a/crates/workspace/src/persistence/model.rs b/crates/workspace/src/persistence/model.rs
index b50d82fff0b..23970f52427 100644
--- a/crates/workspace/src/persistence/model.rs
+++ b/crates/workspace/src/persistence/model.rs
@@ -1,7 +1,7 @@
use super::{SerializedAxis, SerializedWindowBounds};
use crate::{
Member, Pane, PaneAxis, SerializableItemRegistry, Workspace, WorkspaceId, item::ItemHandle,
- path_list::PathList,
+ multi_workspace::SerializedProjectGroupState, path_list::PathList,
};
use anyhow::{Context, Result};
use async_recursion::async_recursion;
@@ -12,8 +12,9 @@ use db::sqlez::{
};
use gpui::{AsyncWindowContext, Entity, WeakEntity, WindowId};
+use crate::ProjectGroupKey;
use language::{Toolchain, ToolchainScope};
-use project::{Project, ProjectGroupKey, debugger::breakpoint_store::SourceBreakpoint};
+use project::{Project, debugger::breakpoint_store::SourceBreakpoint};
use remote::RemoteConnectionOptions;
use serde::{Deserialize, Serialize};
use std::{
@@ -60,31 +61,53 @@ pub struct SessionWorkspace {
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
-pub struct SerializedProjectGroupKey {
+pub struct SerializedProjectGroup {
pub path_list: SerializedPathList,
pub(crate) location: SerializedWorkspaceLocation,
+ #[serde(default = "default_expanded")]
+ pub expanded: bool,
+ #[serde(default)]
+ pub visible_thread_count: Option<usize>,
+}
+
+fn default_expanded() -> bool {
+ true
}
-impl From<ProjectGroupKey> for SerializedProjectGroupKey {
- fn from(value: ProjectGroupKey) -> Self {
- SerializedProjectGroupKey {
- path_list: value.path_list().serialize(),
- location: match value.host() {
+impl SerializedProjectGroup {
+ pub fn from_group(
+ key: &ProjectGroupKey,
+ expanded: bool,
+ visible_thread_count: Option<usize>,
+ ) -> Self {
+ Self {
+ path_list: key.path_list().serialize(),
+ location: match key.host() {
Some(host) => SerializedWorkspaceLocation::Remote(host),
None => SerializedWorkspaceLocation::Local,
},
+ expanded,
+ visible_thread_count,
}
}
-}
-impl From<SerializedProjectGroupKey> for ProjectGroupKey {
- fn from(value: SerializedProjectGroupKey) -> Self {
- let path_list = PathList::deserialize(&value.path_list);
- let host = match value.location {
+ pub fn into_restored_state(self) -> SerializedProjectGroupState {
+ let path_list = PathList::deserialize(&self.path_list);
+ let host = match self.location {
SerializedWorkspaceLocation::Local => None,
SerializedWorkspaceLocation::Remote(opts) => Some(opts),
};
- ProjectGroupKey::new(host, path_list)
+ SerializedProjectGroupState {
+ key: ProjectGroupKey::new(host, path_list),
+ expanded: self.expanded,
+ visible_thread_count: self.visible_thread_count,
+ }
+ }
+}
+
+impl From<SerializedProjectGroup> for ProjectGroupKey {
+ fn from(value: SerializedProjectGroup) -> Self {
+ value.into_restored_state().key
}
}
@@ -93,7 +116,8 @@ impl From<SerializedProjectGroupKey> for ProjectGroupKey {
pub struct MultiWorkspaceState {
pub active_workspace_id: Option<WorkspaceId>,
pub sidebar_open: bool,
- pub project_group_keys: Vec<SerializedProjectGroupKey>,
+ #[serde(alias = "project_group_keys")]
+ pub project_groups: Vec<SerializedProjectGroup>,
#[serde(default)]
pub sidebar_state: Option<String>,
}
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index b7fa93b2945..16d080fc39b 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -33,8 +33,9 @@ pub use dock::Panel;
pub use multi_workspace::{
CloseWorkspaceSidebar, DraggedSidebar, FocusWorkspaceSidebar, MultiWorkspace,
MultiWorkspaceEvent, NewThread, NextProject, NextThread, PreviousProject, PreviousThread,
- ShowFewerThreads, ShowMoreThreads, Sidebar, SidebarEvent, SidebarHandle, SidebarRenderState,
- SidebarSide, ToggleWorkspaceSidebar, sidebar_side_context_menu,
+ ProjectGroup, ProjectGroupKey, SerializedProjectGroupState, ShowFewerThreads, ShowMoreThreads,
+ Sidebar, SidebarEvent, SidebarHandle, SidebarRenderState, SidebarSide, ToggleWorkspaceSidebar,
+ sidebar_side_context_menu,
};
pub use path_list::{PathList, SerializedPathList};
pub use toast_layer::{ToastAction, ToastLayer, ToastView};
@@ -86,14 +87,14 @@ pub use persistence::{
WorkspaceDb, delete_unloaded_items,
model::{
DockStructure, ItemId, MultiWorkspaceState, SerializedMultiWorkspace,
- SerializedProjectGroupKey, SerializedWorkspaceLocation, SessionWorkspace,
+ SerializedProjectGroup, SerializedWorkspaceLocation, SessionWorkspace,
},
read_serialized_multi_workspaces, resolve_worktree_workspaces,
};
use postage::stream::Stream;
use project::{
- DirectoryLister, Project, ProjectEntryId, ProjectGroupKey, ProjectPath, ResolvedPath, Worktree,
- WorktreeId, WorktreeSettings,
+ DirectoryLister, Project, ProjectEntryId, ProjectPath, ResolvedPath, Worktree, WorktreeId,
+ WorktreeSettings,
debugger::{breakpoint_store::BreakpointStoreEvent, session::ThreadStatus},
project_settings::ProjectSettings,
toolchain_store::ToolchainStoreEvent,
@@ -215,6 +216,16 @@ pub trait DebuggerProvider {
fn active_thread_state(&self, cx: &App) -> Option<ThreadStatus>;
}
+pub trait WorkspaceSidebarDelegate: Send + Sync {
+ fn reconcile_group(
+ &self,
+ workspace: &mut Workspace,
+ group_key: &ProjectGroupKey,
+ window: &mut Window,
+ cx: &mut Context<Workspace>,
+ ) -> bool;
+}
+
/// Opens a file or directory.
#[derive(Clone, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = workspace)]
@@ -1371,6 +1382,7 @@ pub struct Workspace {
_panels_task: Option<Task<Result<()>>>,
sidebar_focus_handle: Option<FocusHandle>,
multi_workspace: Option<WeakEntity<MultiWorkspace>>,
+ sidebar_delegate: Option<Arc<dyn WorkspaceSidebarDelegate>>,
}
impl EventEmitter<Event> for Workspace {}
@@ -1799,6 +1811,7 @@ impl Workspace {
removing: false,
sidebar_focus_handle: None,
multi_workspace,
+ sidebar_delegate: None,
open_in_dev_container: false,
_dev_container_task: None,
}
@@ -2459,6 +2472,14 @@ impl Workspace {
self.multi_workspace = Some(multi_workspace);
}
+ pub fn set_sidebar_delegate(&mut self, delegate: Arc<dyn WorkspaceSidebarDelegate>) {
+ self.sidebar_delegate = Some(delegate);
+ }
+
+ pub fn sidebar_delegate(&self) -> Option<Arc<dyn WorkspaceSidebarDelegate>> {
+ self.sidebar_delegate.clone()
+ }
+
pub fn app_state(&self) -> &Arc<AppState> {
&self.app_state
}
@@ -2847,6 +2868,9 @@ impl Workspace {
window: &mut Window,
cx: &mut Context<Self>,
) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
+ // TODO: If `on_prompt_for_open_path` is set, we should always use it
+ // rather than gating on `use_system_path_prompts`. This would let tests
+ // inject a mock without also having to disable the setting.
if !lister.is_local(cx) || !WorkspaceSettings::get_global(cx).use_system_path_prompts {
let prompt = self.on_prompt_for_open_path.take().unwrap();
let rx = prompt(self, lister, window, cx);
@@ -8726,7 +8750,7 @@ pub async fn restore_multiworkspace(
log::error!("Failed to restore active workspace: {err:#}");
let mut fallback_handle = None;
- for key in &state.project_group_keys {
+ for key in &state.project_groups {
let key: ProjectGroupKey = key.clone().into();
let paths = key.path_list().paths().to_vec();
match cx
@@ -8776,20 +8800,21 @@ pub async fn apply_restored_multiworkspace_state(
) {
let MultiWorkspaceState {
sidebar_open,
- project_group_keys,
+ project_groups,
sidebar_state,
..
} = state;
- if !project_group_keys.is_empty() {
+ if !project_groups.is_empty() {
// Resolve linked worktree paths to their main repo paths so
// stale keys from previous sessions get normalized and deduped.
- let mut resolved_keys: Vec<ProjectGroupKey> = Vec::new();
- for key in project_group_keys
- .iter()
- .cloned()
- .map(ProjectGroupKey::from)
- {
+ let mut resolved_groups: Vec<SerializedProjectGroupState> = Vec::new();
+ for serialized in project_groups.iter().cloned() {
+ let SerializedProjectGroupState {
+ key,
+ expanded,
+ visible_thread_count,
+ } = serialized.into_restored_state();
if key.path_list().paths().is_empty() {
continue;
}
@@ -8806,14 +8831,18 @@ pub async fn apply_restored_multiworkspace_state(
}
}
let resolved = ProjectGroupKey::new(key.host(), PathList::new(&resolved_paths));
- if !resolved_keys.contains(&resolved) {
- resolved_keys.push(resolved);
+ if !resolved_groups.iter().any(|g| g.key == resolved) {
+ resolved_groups.push(SerializedProjectGroupState {
+ key: resolved,
+ expanded,
+ visible_thread_count,
+ });
}
}
window_handle
- .update(cx, |multi_workspace, _window, _cx| {
- multi_workspace.restore_project_group_keys(resolved_keys);
+ .update(cx, |multi_workspace, _window, cx| {
+ multi_workspace.restore_project_groups(resolved_groups, cx);
})
.ok();
}
@@ -9885,7 +9914,7 @@ async fn open_remote_project_inner(
});
if let Some(project_group_key) = provisional_project_group_key.clone() {
- multi_workspace.set_workspace_group_key(&new_workspace, project_group_key);
+ multi_workspace.retain_workspace(new_workspace.clone(), project_group_key, cx);
}
multi_workspace.activate(new_workspace.clone(), window, cx);
new_workspace
@@ -9957,14 +9986,13 @@ pub fn join_in_room_project(
.and_then(|window_handle| {
window_handle
.update(cx, |multi_workspace, _window, cx| {
- for workspace in multi_workspace.workspaces() {
- if workspace.read(cx).project().read(cx).remote_id()
- == Some(project_id)
- {
- return Some((window_handle, workspace.clone()));
- }
- }
- None
+ multi_workspace
+ .workspaces()
+ .find(|workspace| {
+ workspace.read(cx).project().read(cx).remote_id()
+ == Some(project_id)
+ })
+ .map(|workspace| (window_handle, workspace.clone()))
})
.unwrap_or(None)
})
@@ -10915,8 +10943,7 @@ mod tests {
// Activate workspace A
multi_workspace_handle
.update(cx, |mw, window, cx| {
- let workspace = mw.workspaces().next().unwrap().clone();
- mw.activate(workspace, window, cx);
+ mw.activate(workspace_a.clone(), window, cx);
})
.unwrap();
@@ -11039,7 +11066,7 @@ mod tests {
assert!(!removed, "removal should have been cancelled");
multi_workspace_handle
- .read_with(cx, |mw, _| {
+ .read_with(cx, |mw, _cx| {
assert_eq!(
mw.workspace(),
&workspace_b,
@@ -11067,7 +11094,7 @@ mod tests {
// Should be back on workspace A, and B should be gone.
multi_workspace_handle
- .read_with(cx, |mw, _| {
+ .read_with(cx, |mw, _cx| {
assert_eq!(
mw.workspace(),
&workspace_a,
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index ab8d448d793..990ed4fd54c 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -165,6 +165,7 @@ pub struct RemoteWorktree {
replica_id: ReplicaId,
visible: bool,
disconnected: bool,
+ received_initial_update: bool,
}
#[derive(Clone)]
@@ -370,7 +371,9 @@ struct UpdateObservationState {
pub enum Event {
UpdatedEntries(UpdatedEntriesSet),
UpdatedGitRepositories(UpdatedGitRepositoriesSet),
- UpdatedRootRepoCommonDir,
+ UpdatedRootRepoCommonDir {
+ old: Option<Arc<SanitizedPath>>,
+ },
DeletedEntry(ProjectEntryId),
/// The worktree root itself has been deleted (for single-file worktrees)
Deleted,
@@ -550,6 +553,7 @@ impl Worktree {
snapshot_subscriptions: Default::default(),
visible: worktree.visible,
disconnected: false,
+ received_initial_update: false,
};
// Apply updates to a separate snapshot in a background task, then
@@ -574,9 +578,16 @@ impl Worktree {
cx.spawn(async move |this, cx| {
while (snapshot_updated_rx.recv().await).is_some() {
this.update(cx, |this, cx| {
- let mut entries_changed = false;
let this = this.as_remote_mut().unwrap();
+
+ // The watch channel delivers an initial signal before
+ // any real updates arrive. Skip these spurious wakeups.
+ if this.background_snapshot.lock().1.is_empty() {
+ return;
+ }
+
let old_root_repo_common_dir = this.snapshot.root_repo_common_dir.clone();
+ let mut entries_changed = false;
{
let mut lock = this.background_snapshot.lock();
this.snapshot = lock.0.clone();
@@ -592,8 +603,14 @@ impl Worktree {
if entries_changed {
cx.emit(Event::UpdatedEntries(Arc::default()));
}
- if this.snapshot.root_repo_common_dir != old_root_repo_common_dir {
- cx.emit(Event::UpdatedRootRepoCommonDir);
+ let is_first_update = !this.received_initial_update;
+ this.received_initial_update = true;
+ if this.snapshot.root_repo_common_dir != old_root_repo_common_dir
+ || (is_first_update && this.snapshot.root_repo_common_dir.is_none())
+ {
+ cx.emit(Event::UpdatedRootRepoCommonDir {
+ old: old_root_repo_common_dir,
+ });
}
cx.notify();
while let Some((scan_id, _)) = this.snapshot_subscriptions.front() {
@@ -1221,8 +1238,9 @@ impl LocalWorktree {
.local_repo_for_work_directory_path(RelPath::empty())
.map(|repo| SanitizedPath::from_arc(repo.common_dir_abs_path.clone()));
- let root_repo_common_dir_changed =
- self.snapshot.root_repo_common_dir != new_snapshot.root_repo_common_dir;
+ let old_root_repo_common_dir = (self.snapshot.root_repo_common_dir
+ != new_snapshot.root_repo_common_dir)
+ .then(|| self.snapshot.root_repo_common_dir.clone());
self.snapshot = new_snapshot;
if let Some(share) = self.update_observer.as_mut() {
@@ -1238,8 +1256,8 @@ impl LocalWorktree {
if !repo_changes.is_empty() {
cx.emit(Event::UpdatedGitRepositories(repo_changes));
}
- if root_repo_common_dir_changed {
- cx.emit(Event::UpdatedRootRepoCommonDir);
+ if let Some(old) = old_root_repo_common_dir {
+ cx.emit(Event::UpdatedRootRepoCommonDir { old });
}
while let Some((scan_id, _)) = self.snapshot_subscriptions.front() {
diff --git a/crates/worktree/tests/integration/main.rs b/crates/worktree/tests/integration/main.rs
index 47ce5e6b0a9..f46d0877c72 100644
--- a/crates/worktree/tests/integration/main.rs
+++ b/crates/worktree/tests/integration/main.rs
@@ -9,6 +9,7 @@ use parking_lot::Mutex;
use postage::stream::Stream;
use pretty_assertions::assert_eq;
use rand::prelude::*;
+use rpc::{AnyProtoClient, NoopProtoClient, proto};
use worktree::{Entry, EntryKind, Event, PathChange, Worktree, WorktreeModelHandle};
use serde_json::json;
@@ -2807,7 +2808,7 @@ async fn test_root_repo_common_dir(executor: BackgroundExecutor, cx: &mut TestAp
let event_count = event_count.clone();
|_, cx| {
cx.subscribe(&cx.entity(), move |_, _, event, _| {
- if matches!(event, Event::UpdatedRootRepoCommonDir) {
+ if matches!(event, Event::UpdatedRootRepoCommonDir { .. }) {
event_count.set(event_count.get() + 1);
}
})
@@ -3345,3 +3346,193 @@ async fn test_single_file_worktree_deleted(cx: &mut TestAppContext) {
"Should receive Deleted event when single-file worktree root is deleted"
);
}
+
+#[gpui::test]
+async fn test_remote_worktree_without_git_emits_root_repo_event_after_first_update(
+ cx: &mut TestAppContext,
+) {
+ cx.update(|cx| {
+ let store = SettingsStore::test(cx);
+ cx.set_global(store);
+ });
+
+ let client = AnyProtoClient::new(NoopProtoClient::new());
+
+ let worktree = cx.update(|cx| {
+ Worktree::remote(
+ 1,
+ clock::ReplicaId::new(1),
+ proto::WorktreeMetadata {
+ id: 1,
+ root_name: "project".to_string(),
+ visible: true,
+ abs_path: "/home/user/project".to_string(),
+ root_repo_common_dir: None,
+ },
+ client,
+ PathStyle::Posix,
+ cx,
+ )
+ });
+
+ let events: Arc<std::sync::Mutex<Vec<&'static str>>> =
+ Arc::new(std::sync::Mutex::new(Vec::new()));
+ let events_clone = events.clone();
+ cx.update(|cx| {
+ cx.subscribe(&worktree, move |_, event, _cx| {
+ if matches!(event, Event::UpdatedRootRepoCommonDir { .. }) {
+ events_clone
+ .lock()
+ .unwrap()
+ .push("UpdatedRootRepoCommonDir");
+ }
+ if matches!(event, Event::UpdatedEntries(_)) {
+ events_clone.lock().unwrap().push("UpdatedEntries");
+ }
+ })
+ .detach();
+ });
+
+ // Send an update with entries but no repo info (plain directory).
+ worktree.update(cx, |worktree, _cx| {
+ worktree
+ .as_remote()
+ .unwrap()
+ .update_from_remote(proto::UpdateWorktree {
+ project_id: 1,
+ worktree_id: 1,
+ abs_path: "/home/user/project".to_string(),
+ root_name: "project".to_string(),
+ updated_entries: vec![proto::Entry {
+ id: 1,
+ is_dir: true,
+ path: "".to_string(),
+ inode: 1,
+ mtime: Some(proto::Timestamp {
+ seconds: 0,
+ nanos: 0,
+ }),
+ is_ignored: false,
+ is_hidden: false,
+ is_external: false,
+ is_fifo: false,
+ size: None,
+ canonical_path: None,
+ }],
+ removed_entries: vec![],
+ scan_id: 1,
+ is_last_update: true,
+ updated_repositories: vec![],
+ removed_repositories: vec![],
+ root_repo_common_dir: None,
+ });
+ });
+
+ cx.run_until_parked();
+
+ let fired = events.lock().unwrap();
+ assert!(
+ fired.contains(&"UpdatedEntries"),
+ "UpdatedEntries should fire after remote update"
+ );
+ assert!(
+ fired.contains(&"UpdatedRootRepoCommonDir"),
+ "UpdatedRootRepoCommonDir should fire after first remote update even when \
+ root_repo_common_dir is None, to signal that repo state is now known"
+ );
+}
+
+#[gpui::test]
+async fn test_remote_worktree_with_git_emits_root_repo_event_when_repo_info_arrives(
+ cx: &mut TestAppContext,
+) {
+ cx.update(|cx| {
+ let store = SettingsStore::test(cx);
+ cx.set_global(store);
+ });
+
+ let client = AnyProtoClient::new(NoopProtoClient::new());
+
+ let worktree = cx.update(|cx| {
+ Worktree::remote(
+ 1,
+ clock::ReplicaId::new(1),
+ proto::WorktreeMetadata {
+ id: 1,
+ root_name: "project".to_string(),
+ visible: true,
+ abs_path: "/home/user/project".to_string(),
+ root_repo_common_dir: None,
+ },
+ client,
+ PathStyle::Posix,
+ cx,
+ )
+ });
+
+ let events: Arc<std::sync::Mutex<Vec<&'static str>>> =
+ Arc::new(std::sync::Mutex::new(Vec::new()));
+ let events_clone = events.clone();
+ cx.update(|cx| {
+ cx.subscribe(&worktree, move |_, event, _cx| {
+ if matches!(event, Event::UpdatedRootRepoCommonDir { .. }) {
+ events_clone
+ .lock()
+ .unwrap()
+ .push("UpdatedRootRepoCommonDir");
+ }
+ })
+ .detach();
+ });
+
+ // Send an update where repo info arrives (None -> Some).
+ worktree.update(cx, |worktree, _cx| {
+ worktree
+ .as_remote()
+ .unwrap()
+ .update_from_remote(proto::UpdateWorktree {
+ project_id: 1,
+ worktree_id: 1,
+ abs_path: "/home/user/project".to_string(),
+ root_name: "project".to_string(),
+ updated_entries: vec![proto::Entry {
+ id: 1,
+ is_dir: true,
+ path: "".to_string(),
+ inode: 1,
+ mtime: Some(proto::Timestamp {
+ seconds: 0,
+ nanos: 0,
+ }),
+ is_ignored: false,
+ is_hidden: false,
+ is_external: false,
+ is_fifo: false,
+ size: None,
+ canonical_path: None,
+ }],
+ removed_entries: vec![],
+ scan_id: 1,
+ is_last_update: true,
+ updated_repositories: vec![],
+ removed_repositories: vec![],
+ root_repo_common_dir: Some("/home/user/project/.git".to_string()),
+ });
+ });
+
+ cx.run_until_parked();
+
+ let fired = events.lock().unwrap();
+ assert!(
+ fired.contains(&"UpdatedRootRepoCommonDir"),
+ "UpdatedRootRepoCommonDir should fire when repo info arrives (None -> Some)"
+ );
+ assert_eq!(
+ fired
+ .iter()
+ .filter(|e| **e == "UpdatedRootRepoCommonDir")
+ .count(),
+ 1,
+ "should fire exactly once, not duplicate"
+ );
+}
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs
index 63e86a0b7c7..60ede82b719 100644
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@ -1508,7 +1508,7 @@ fn quit(_: &Quit, cx: &mut App) {
for window in &workspace_windows {
let window = *window;
let workspaces = window
- .update(cx, |multi_workspace, _, _| {
+ .update(cx, |multi_workspace, _, _cx| {
multi_workspace.workspaces().cloned().collect::<Vec<_>>()
})
.log_err();
@@ -6130,10 +6130,9 @@ mod tests {
#[gpui::test]
async fn test_multi_workspace_session_restore(cx: &mut TestAppContext) {
use collections::HashMap;
- use project::ProjectGroupKey;
use session::Session;
use util::path_list::PathList;
- use workspace::{OpenMode, Workspace, WorkspaceId};
+ use workspace::{OpenMode, ProjectGroupKey, Workspace, WorkspaceId};
let app_state = init_test(cx);
@@ -6324,7 +6323,7 @@ mod tests {
restored_a
.read_with(cx, |mw, _| {
assert_eq!(
- mw.project_group_keys().cloned().collect::<Vec<_>>(),
+ mw.project_group_keys(),
vec![
ProjectGroupKey::new(None, PathList::new(&[dir2])),
ProjectGroupKey::new(None, PathList::new(&[dir1])),
@@ -6338,11 +6337,168 @@ mod tests {
restored_b
.read_with(cx, |mw, _| {
assert_eq!(
- mw.project_group_keys().cloned().collect::<Vec<_>>(),
+ mw.project_group_keys(),
vec![ProjectGroupKey::new(None, PathList::new(&[dir3]))]
);
assert_eq!(mw.workspaces().count(), 1);
})
.unwrap();
}
+
+ #[gpui::test]
+ async fn test_restored_project_groups_survive_workspace_key_change(cx: &mut TestAppContext) {
+ use session::Session;
+ use util::path_list::PathList;
+ use workspace::{OpenMode, ProjectGroupKey};
+
+ let app_state = init_test(cx);
+
+ let fs = app_state.fs.clone();
+ let fake_fs = fs.as_fake();
+ fake_fs
+ .insert_tree(path!("/root_a"), json!({ "file.txt": "" }))
+ .await;
+ fake_fs
+ .insert_tree(path!("/root_b"), json!({ "file.txt": "" }))
+ .await;
+ fake_fs
+ .insert_tree(path!("/root_c"), json!({ "file.txt": "" }))
+ .await;
+ fake_fs
+ .insert_tree(path!("/root_d"), json!({ "other.txt": "" }))
+ .await;
+
+ let session_id = cx.read(|cx| app_state.session.read(cx).id().to_owned());
+
+ // --- Phase 1: Build a multi-workspace with 3 project groups ---
+
+ let workspace::OpenResult { window, .. } = cx
+ .update(|cx| {
+ workspace::Workspace::new_local(
+ vec![path!("/root_a").into()],
+ app_state.clone(),
+ None,
+ None,
+ None,
+ OpenMode::Activate,
+ cx,
+ )
+ })
+ .await
+ .expect("failed to open workspace");
+
+ window.update(cx, |mw, _, cx| mw.open_sidebar(cx)).unwrap();
+
+ window
+ .update(cx, |mw, window, cx| {
+ mw.open_project(vec![path!("/root_b").into()], OpenMode::Add, window, cx)
+ })
+ .unwrap()
+ .await
+ .expect("failed to add root_b");
+
+ window
+ .update(cx, |mw, window, cx| {
+ mw.open_project(vec![path!("/root_c").into()], OpenMode::Add, window, cx)
+ })
+ .unwrap()
+ .await
+ .expect("failed to add root_c");
+ cx.run_until_parked();
+
+ let key_b = ProjectGroupKey::new(None, PathList::new(&[path!("/root_b")]));
+ let key_c = ProjectGroupKey::new(None, PathList::new(&[path!("/root_c")]));
+
+ // Make root_a the active workspace so it's the one eagerly restored.
+ window
+ .update(cx, |mw, window, cx| {
+ let workspace_a = mw
+ .workspaces()
+ .find(|ws| {
+ ws.read(cx)
+ .root_paths(cx)
+ .iter()
+ .any(|p| p.as_ref() == Path::new(path!("/root_a")))
+ })
+ .expect("workspace_a should exist")
+ .clone();
+ mw.activate(workspace_a, window, cx);
+ })
+ .unwrap();
+ cx.run_until_parked();
+
+ // --- Phase 2: Serialize, close, and restore ---
+
+ flush_workspace_serialization(&window, cx).await;
+ cx.run_until_parked();
+
+ window
+ .update(cx, |_, window, _| window.remove_window())
+ .unwrap();
+ cx.run_until_parked();
+
+ cx.update(|cx| {
+ app_state.session.update(cx, |app_session, _cx| {
+ app_session
+ .replace_session_for_test(Session::test_with_old_session(session_id.clone()));
+ });
+ });
+
+ let mut async_cx = cx.to_async();
+ crate::restore_or_create_workspace(app_state.clone(), &mut async_cx)
+ .await
+ .expect("failed to restore workspace");
+ cx.run_until_parked();
+
+ let restored_windows: Vec<WindowHandle<MultiWorkspace>> = cx.read(|cx| {
+ cx.windows()
+ .into_iter()
+ .filter_map(|w| w.downcast::<MultiWorkspace>())
+ .collect()
+ });
+ assert_eq!(restored_windows.len(), 1);
+ let restored = &restored_windows[0];
+
+ // Verify the restored window has all 3 project groups.
+ restored
+ .read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert_eq!(
+ keys.len(),
+ 3,
+ "restored window should have 3 groups; got {keys:?}"
+ );
+ assert!(keys.contains(&key_b), "should contain key_b");
+ assert!(keys.contains(&key_c), "should contain key_c");
+ })
+ .unwrap();
+
+ // --- Phase 3: Trigger a workspace key change and verify survival ---
+
+ let active_project = restored
+ .read_with(cx, |mw, cx| mw.workspace().read(cx).project().clone())
+ .unwrap();
+
+ active_project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree(path!("/root_d"), true, cx)
+ })
+ .await
+ .expect("adding worktree should succeed");
+ cx.run_until_parked();
+
+ restored
+ .read_with(cx, |mw, _cx| {
+ let keys = mw.project_group_keys();
+ assert!(
+ keys.contains(&key_b),
+ "restored group key_b should survive a workspace key change; got {keys:?}"
+ );
+ assert!(
+ keys.contains(&key_c),
+ "restored group key_c should survive a workspace key change; got {keys:?}"
+ );
+ })
+ .unwrap();
+ }
}