use acp_thread::ThreadStatus;
use action_log::DiffStats;
use agent_client_protocol::{self as acp};
use agent_ui::thread_metadata_store::{SidebarThreadMetadataStore, ThreadMetadata};
use agent_ui::threads_archive_view::{
    ThreadsArchiveView, ThreadsArchiveViewEvent, format_history_entry_timestamp,
};
use agent_ui::{
    Agent, AgentPanel, AgentPanelEvent, DEFAULT_THREAD_TITLE, NewThread, RemoveSelectedThread,
};
use chrono::Utc;
use editor::Editor;
use feature_flags::{AgentV2FeatureFlag, FeatureFlagViewExt as _};
use gpui::{
    Action as _, AnyElement, App, Context, Entity, FocusHandle, Focusable, ListState, Pixels,
    Render, SharedString, WeakEntity, Window, WindowHandle, list, prelude::*, px,
};
use menu::{
    Cancel, Confirm, SelectChild, SelectFirst, SelectLast, SelectNext, SelectParent, SelectPrevious,
};
use project::{Event as ProjectEvent, linked_worktree_short_name};
use recent_projects::sidebar_recent_projects::SidebarRecentProjects;
use ui::utils::platform_title_bar_height;

use settings::Settings as _;
use std::collections::{HashMap, HashSet};
use std::mem;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use theme::ActiveTheme;
use ui::{
    AgentThreadStatus, CommonAnimationExt, ContextMenu, Divider, HighlightedLabel, KeyBinding,
    PopoverMenu, PopoverMenuHandle, Tab, ThreadItem, TintColor, Tooltip, WithScrollbar, prelude::*,
};
use util::ResultExt as _;
use util::path_list::PathList;
use workspace::{
    AddFolderToProject, FocusWorkspaceSidebar, MultiWorkspace, MultiWorkspaceEvent, Open,
    Sidebar as WorkspaceSidebar, ToggleWorkspaceSidebar, Workspace, WorkspaceId,
};

use zed_actions::OpenRecent;
use zed_actions::editor::{MoveDown, MoveUp};

use zed_actions::agents_sidebar::FocusSidebarFilter;

gpui::actions!(
    agents_sidebar,
    [
        /// Creates a new thread in the currently selected or active project group.
        NewThreadInGroup,
        /// Toggles between the thread list and the archive view.
        ToggleArchive,
    ]
);

const DEFAULT_WIDTH: Pixels = px(320.0);
const MIN_WIDTH: Pixels = px(200.0);
const MAX_WIDTH: Pixels = px(800.0);
const DEFAULT_THREADS_SHOWN: usize = 5;

#[derive(Debug, Default)]
enum SidebarView {
    #[default]
    ThreadList,
    Archive(Entity<ThreadsArchiveView>),
}

#[derive(Clone, Debug)]
struct ActiveThreadInfo {
    session_id: acp::SessionId,
    title: SharedString,
    status: AgentThreadStatus,
    icon: IconName,
    icon_from_external_svg: Option<SharedString>,
    is_background: bool,
    is_title_generating: bool,
    diff_stats: DiffStats,
}

impl From<&ActiveThreadInfo> for acp_thread::AgentSessionInfo {
    fn from(info: &ActiveThreadInfo) -> Self {
        Self {
            session_id: info.session_id.clone(),
            work_dirs: None,
            title: Some(info.title.clone()),
            updated_at: Some(Utc::now()),
            created_at: Some(Utc::now()),
            meta: None,
        }
    }
}

#[derive(Clone)]
enum ThreadEntryWorkspace {
    Open(Entity<Workspace>),
    Closed(PathList),
}

#[derive(Clone)]
struct ThreadEntry {
    agent: Agent,
    session_info: acp_thread::AgentSessionInfo,
    icon: IconName,
    icon_from_external_svg: Option<SharedString>,
    status: AgentThreadStatus,
    workspace: ThreadEntryWorkspace,
    is_live: bool,
    is_background: bool,
    is_title_generating: bool,
    highlight_positions: Vec<usize>,
    worktree_name: Option<SharedString>,
    worktree_full_path: Option<SharedString>,
    worktree_highlight_positions: Vec<usize>,
    diff_stats: DiffStats,
}

#[derive(Clone)]
enum ListEntry {
    ProjectHeader {
        path_list: PathList,
        label: SharedString,
        workspace: Option<Entity<Workspace>>,
        highlight_positions: Vec<usize>,
        has_running_threads: bool,
        waiting_thread_count: usize,
    },
    Thread(ThreadEntry),
    ViewMore {
        path_list: PathList,
        is_fully_expanded: bool,
    },
    NewThread {
        path_list: PathList,
        workspace: Entity<Workspace>,
        is_active_draft: bool,
    },
}

impl From<ThreadEntry> for ListEntry {
    fn from(thread: ThreadEntry) -> Self {
        ListEntry::Thread(thread)
    }
}

#[derive(Default)]
struct SidebarContents {
    entries: Vec<ListEntry>,
    notified_threads: HashSet<acp::SessionId>,
    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 fuzzy_match_positions(query: &str, candidate: &str) -> Option<Vec<usize>> {
    let mut positions = Vec::new();
    let mut query_chars = query.chars().peekable();

    for (byte_idx, candidate_char) in candidate.char_indices() {
        if let Some(&query_char) = query_chars.peek() {
            if candidate_char.eq_ignore_ascii_case(&query_char) {
                positions.push(byte_idx);
                query_chars.next();
            }
        } else {
            break;
        }
    }

    if query_chars.peek().is_none() {
        Some(positions)
    } else {
        None
    }
}

// TODO: The mapping from workspace root paths to git repositories needs a
// unified approach across the codebase: this function, `AgentPanel::classify_worktrees`,
// thread persistence (which PathList is saved to the database), and thread
// querying (which PathList is used to read threads back). All of these need
// to agree on how repos are resolved for a given workspace, especially in
// multi-root and nested-repo configurations.
fn root_repository_snapshots(
    workspace: &Entity<Workspace>,
    cx: &App,
) -> Vec<project::git_store::RepositorySnapshot> {
    let path_list = workspace.read(cx).path_list(cx);
    let project = workspace.read(cx).project().read(cx);
    project
        .repositories(cx)
        .values()
        .filter_map(|repo| {
            let snapshot = repo.read(cx).snapshot();
            let is_root = path_list
                .paths()
                .iter()
                .any(|p| p.as_path() == snapshot.work_directory_abs_path.as_ref());
            is_root.then_some(snapshot)
        })
        .collect()
}

/// Build a mapping from git worktree checkout paths to their root repo paths.
///
/// For each open workspace's repositories, registers both the main repo path
/// (identity mapping) and every linked worktree path → root repo path.
///
/// Iterates ALL repos from all workspaces (not just root repos) so that
/// worktree checkout workspaces contribute their `work_directory → root`
/// mapping even when the main repo's linked-worktree snapshot is
/// temporarily incomplete.
fn build_worktree_root_mapping(
    workspaces: &[Entity<Workspace>],
    cx: &App,
) -> HashMap<PathBuf, Arc<Path>> {
    let mut mapping = HashMap::default();
    for workspace in workspaces {
        let project = workspace.read(cx).project().read(cx);
        for repo in project.repositories(cx).values() {
            let snapshot = repo.read(cx).snapshot();
            let root = &snapshot.original_repo_abs_path;
            mapping.insert(root.to_path_buf(), root.clone());
            mapping.insert(snapshot.work_directory_abs_path.to_path_buf(), root.clone());
            for wt in snapshot.linked_worktrees() {
                mapping.insert(wt.path.clone(), root.clone());
            }
        }
    }
    mapping
}

/// Map each path in a `PathList` to its root repo path (if known), producing
/// a canonical `PathList` suitable for grouping threads across worktrees.
fn canonicalize_path_list(
    path_list: &PathList,
    worktree_to_root: &HashMap<PathBuf, Arc<Path>>,
) -> PathList {
    let canonical_paths: Vec<PathBuf> = path_list
        .paths()
        .iter()
        .map(|p| {
            worktree_to_root
                .get(p)
                .map(|root| root.to_path_buf())
                .unwrap_or_else(|| p.clone())
        })
        .collect();
    PathList::new(&canonical_paths)
}

/// Build an index of all threads keyed by their canonicalized `folder_paths`.
///
/// Each thread's raw `folder_paths` is mapped through `worktree_to_root` so
/// that threads from different worktree checkouts of the same repos end up
/// under the same canonical key.
fn build_canonical_thread_index(
    worktree_to_root: &HashMap<PathBuf, Arc<Path>>,
    cx: &App,
) -> HashMap<PathList, Vec<ThreadMetadata>> {
    let store = SidebarThreadMetadataStore::global(cx).read(cx);
    let mut index: HashMap<PathList, Vec<ThreadMetadata>> = HashMap::default();
    for entry in store.entries() {
        let canonical = canonicalize_path_list(&entry.folder_paths, worktree_to_root);
        index.entry(canonical).or_default().push(entry);
    }
    index
}

/// Workspaces grouped by their canonical path. Workspaces that represent
/// different checkouts of the same repos (e.g. main branch and a worktree
/// checkout) share one canonical key and get a single sidebar header.
struct CanonicalWorkspaceGroups {
    /// Canonical keys in insertion order (first workspace seen wins).
    order: Vec<PathList>,
    /// Map from canonical key to the workspaces in that group.
    groups: HashMap<PathList, Vec<Entity<Workspace>>>,
}

impl CanonicalWorkspaceGroups {
    fn build(
        workspaces: &[Entity<Workspace>],
        worktree_to_root: &HashMap<PathBuf, Arc<Path>>,
        cx: &App,
    ) -> Self {
        let mut order: Vec<PathList> = Vec::new();
        let mut groups: HashMap<PathList, Vec<Entity<Workspace>>> = HashMap::new();
        for workspace in workspaces {
            let path_list = workspace.read(cx).path_list(cx);
            if path_list.paths().is_empty() {
                continue;
            }
            let canonical = canonicalize_path_list(&path_list, worktree_to_root);
            let group = groups.entry(canonical.clone()).or_default();
            if group.is_empty() {
                order.push(canonical);
            }
            group.push(workspace.clone());
        }
        Self { order, groups }
    }

    fn is_empty(&self) -> bool {
        self.groups.is_empty()
    }
}

/// Render a group of threads into `ListEntry` items, handling both query
/// (fuzzy-filter) and normal (paginated) modes.
///
/// Both open-workspace groups and historical (closed) groups use this so the
/// rendering logic stays in one place.
///
/// Returns `None` when a query is active and nothing in the group matches
/// (the caller should skip the group entirely).
fn render_thread_group(
    header: ListEntry,
    threads: Vec<ThreadEntry>,
    new_thread_entry: Option<ListEntry>,
    query: &str,
    is_collapsed: bool,
    path_list: &PathList,
    expanded_groups: &HashMap<PathList, usize>,
    focused_thread: Option<&acp::SessionId>,
    notified_threads: Option<&HashSet<acp::SessionId>>,
) -> Option<Vec<ListEntry>> {
    if !query.is_empty() {
        let workspace_matched = matches!(
            &header,
            ListEntry::ProjectHeader { highlight_positions, .. }
                if !highlight_positions.is_empty()
        );

        let matched_threads: Vec<ListEntry> = threads
            .into_iter()
            .filter_map(|mut thread| {
                let title = thread
                    .session_info
                    .title
                    .as_ref()
                    .map(|s| s.as_ref())
                    .unwrap_or("");
                if let Some(positions) = fuzzy_match_positions(query, title) {
                    thread.highlight_positions = positions;
                }
                if let Some(worktree_name) = &thread.worktree_name {
                    if let Some(positions) = fuzzy_match_positions(query, worktree_name) {
                        thread.worktree_highlight_positions = positions;
                    }
                }
                let worktree_matched = !thread.worktree_highlight_positions.is_empty();
                if workspace_matched || !thread.highlight_positions.is_empty() || worktree_matched {
                    Some(thread.into())
                } else {
                    None
                }
            })
            .collect();

        if matched_threads.is_empty() && !workspace_matched {
            return None;
        }

        return Some(std::iter::once(header).chain(matched_threads).collect());
    }

    let mut entries = vec![header];

    if is_collapsed {
        return Some(entries);
    }

    if let Some(new_thread) = new_thread_entry {
        entries.push(new_thread);
    }

    let total = threads.len();
    let extra_batches = expanded_groups.get(path_list).copied().unwrap_or(0);
    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();

    for (index, thread) in threads.into_iter().enumerate() {
        let is_hidden = index >= count;
        let session_id = &thread.session_info.session_id;
        if is_hidden {
            let is_promoted = focused_thread.is_some_and(|id| id == session_id)
                || notified_threads.is_some_and(|notified| notified.contains(session_id))
                || (notified_threads.is_some()
                    && (thread.status == AgentThreadStatus::Running
                        || thread.status == AgentThreadStatus::WaitingForConfirmation));
            if is_promoted {
                promoted_threads.insert(session_id.clone());
            }
            if !promoted_threads.contains(session_id) {
                continue;
            }
        }
        entries.push(thread.into());
    }

    let visible = count + promoted_threads.len();
    let is_fully_expanded = visible >= total;

    if total > DEFAULT_THREADS_SHOWN {
        entries.push(ListEntry::ViewMore {
            path_list: path_list.clone(),
            is_fully_expanded,
        });
    }

    Some(entries)
}

fn workspace_label_from_path_list(path_list: &PathList) -> SharedString {
    let mut names = Vec::with_capacity(path_list.paths().len());
    for abs_path in path_list.paths() {
        if let Some(name) = abs_path.file_name() {
            names.push(name.to_string_lossy().to_string());
        }
    }
    if names.is_empty() {
        // TODO: Can we do something better in this case?
        "Empty Workspace".into()
    } else {
        names.join(", ").into()
    }
}

/// The sidebar re-derives its entire entry list from scratch on every
/// change via `update_entries` → `rebuild_contents`. Avoid adding
/// incremental or inter-event coordination state — if something can
/// be computed from the current world state, compute it in the rebuild.
pub struct Sidebar {
    multi_workspace: WeakEntity<MultiWorkspace>,
    width: Pixels,
    focus_handle: FocusHandle,
    filter_editor: Entity<Editor>,
    list_state: ListState,
    contents: SidebarContents,
    /// The index of the list item that currently has the keyboard focus
    ///
    /// Note: This is NOT the same as the active item.
    selection: Option<usize>,
    /// Derived from the active panel's thread in `rebuild_contents`.
    /// Only updated when the panel returns `Some` — never cleared by
    /// derivation, since the panel may transiently return `None` while
    /// loading. User actions may write directly for immediate feedback.
    focused_thread: Option<acp::SessionId>,
    agent_panel_visible: bool,
    active_thread_is_draft: bool,
    hovered_thread_index: Option<usize>,
    collapsed_groups: HashSet<PathList>,
    expanded_groups: HashMap<PathList, usize>,
    view: SidebarView,
    recent_projects_popover_handle: PopoverMenuHandle<SidebarRecentProjects>,
    project_header_menu_ix: Option<usize>,
    _subscriptions: Vec<gpui::Subscription>,
    _draft_observation: Option<gpui::Subscription>,
}

impl Sidebar {
    pub fn new(
        multi_workspace: Entity<MultiWorkspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let focus_handle = cx.focus_handle();
        cx.on_focus_in(&focus_handle, window, Self::focus_in)
            .detach();

        let filter_editor = cx.new(|cx| {
            let mut editor = Editor::single_line(window, cx);
            editor.set_use_modal_editing(true);
            editor.set_placeholder_text("Search…", window, cx);
            editor
        });

        cx.subscribe_in(
            &multi_workspace,
            window,
            |this, _multi_workspace, event: &MultiWorkspaceEvent, window, cx| match event {
                MultiWorkspaceEvent::ActiveWorkspaceChanged => {
                    this.observe_draft_editor(cx);
                    this.update_entries(cx);
                }
                MultiWorkspaceEvent::WorkspaceAdded(workspace) => {
                    this.subscribe_to_workspace(workspace, window, cx);
                    this.update_entries(cx);
                }
                MultiWorkspaceEvent::WorkspaceRemoved(_) => {
                    this.update_entries(cx);
                }
            },
        )
        .detach();

        cx.subscribe(&filter_editor, |this: &mut Self, _, event, cx| {
            if let editor::EditorEvent::BufferEdited = event {
                let query = this.filter_editor.read(cx).text(cx);
                if !query.is_empty() {
                    this.selection.take();
                }
                this.update_entries(cx);
                if !query.is_empty() {
                    this.select_first_entry();
                }
            }
        })
        .detach();

        cx.observe(
            &SidebarThreadMetadataStore::global(cx),
            |this, _store, cx| {
                this.update_entries(cx);
            },
        )
        .detach();

        cx.observe_flag::<AgentV2FeatureFlag, _>(window, |_is_enabled, this, _window, cx| {
            this.update_entries(cx);
        })
        .detach();

        let workspaces = multi_workspace.read(cx).workspaces().to_vec();
        cx.defer_in(window, move |this, window, cx| {
            for workspace in &workspaces {
                this.subscribe_to_workspace(workspace, window, cx);
            }
            this.update_entries(cx);
        });

        Self {
            multi_workspace: multi_workspace.downgrade(),
            width: DEFAULT_WIDTH,
            focus_handle,
            filter_editor,
            list_state: ListState::new(0, gpui::ListAlignment::Top, px(1000.)),
            contents: SidebarContents::default(),
            selection: None,
            focused_thread: None,
            agent_panel_visible: false,
            active_thread_is_draft: false,
            hovered_thread_index: None,
            collapsed_groups: HashSet::new(),
            expanded_groups: HashMap::new(),
            view: SidebarView::default(),
            recent_projects_popover_handle: PopoverMenuHandle::default(),
            project_header_menu_ix: None,
            _subscriptions: Vec::new(),
            _draft_observation: None,
        }
    }

    fn is_active_workspace(&self, workspace: &Entity<Workspace>, cx: &App) -> bool {
        self.multi_workspace
            .upgrade()
            .map_or(false, |mw| mw.read(cx).workspace() == workspace)
    }

    fn subscribe_to_workspace(
        &mut self,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let project = workspace.read(cx).project().clone();
        cx.subscribe_in(
            &project,
            window,
            |this, _project, event, _window, cx| match event {
                ProjectEvent::WorktreeAdded(_)
                | ProjectEvent::WorktreeRemoved(_)
                | ProjectEvent::WorktreeOrderChanged => {
                    this.update_entries(cx);
                }
                _ => {}
            },
        )
        .detach();

        let git_store = workspace.read(cx).project().read(cx).git_store().clone();
        cx.subscribe_in(
            &git_store,
            window,
            |this, _, event: &project::git_store::GitStoreEvent, window, cx| {
                if matches!(
                    event,
                    project::git_store::GitStoreEvent::RepositoryUpdated(
                        _,
                        project::git_store::RepositoryEvent::GitWorktreeListChanged,
                        _,
                    )
                ) {
                    this.prune_stale_worktree_workspaces(window, cx);
                    this.update_entries(cx);
                }
            },
        )
        .detach();

        cx.subscribe_in(
            workspace,
            window,
            |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);
                    }
                }
            },
        )
        .detach();

        self.observe_docks(workspace, cx);

        if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
            self.subscribe_to_agent_panel(&agent_panel, window, cx);
            if self.is_active_workspace(workspace, cx) {
                self.agent_panel_visible = AgentPanel::is_visible(workspace, cx);
            }
            self.observe_draft_editor(cx);
        }
    }

    fn subscribe_to_agent_panel(
        &mut self,
        agent_panel: &Entity<AgentPanel>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.subscribe_in(
            agent_panel,
            window,
            |this, agent_panel, event: &AgentPanelEvent, _window, cx| match event {
                AgentPanelEvent::ActiveViewChanged => {
                    let is_new_draft = agent_panel
                        .read(cx)
                        .active_conversation_view()
                        .is_some_and(|cv| cv.read(cx).parent_id(cx).is_none());
                    if is_new_draft {
                        this.focused_thread = None;
                    }
                    this.observe_draft_editor(cx);
                    this.update_entries(cx);
                }
                AgentPanelEvent::ThreadFocused | AgentPanelEvent::BackgroundThreadChanged => {
                    this.update_entries(cx);
                }
            },
        )
        .detach();
    }

    fn observe_docks(&mut self, workspace: &Entity<Workspace>, cx: &mut Context<Self>) {
        let docks: Vec<_> = workspace
            .read(cx)
            .all_docks()
            .into_iter()
            .cloned()
            .collect();
        let workspace = workspace.downgrade();
        for dock in docks {
            let workspace = workspace.clone();
            cx.observe(&dock, move |this, _dock, cx| {
                let Some(workspace) = workspace.upgrade() else {
                    return;
                };
                if !this.is_active_workspace(&workspace, cx) {
                    return;
                }

                let is_visible = AgentPanel::is_visible(&workspace, cx);

                if this.agent_panel_visible != is_visible {
                    this.agent_panel_visible = is_visible;
                    cx.notify();
                }
            })
            .detach();
        }
    }

    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 active_draft_text(&self, cx: &App) -> Option<SharedString> {
        let mw = self.multi_workspace.upgrade()?;
        let workspace = mw.read(cx).workspace();
        let panel = workspace.read(cx).panel::<AgentPanel>(cx)?;
        let conversation_view = panel.read(cx).active_conversation_view()?;
        let thread_view = conversation_view.read(cx).active_thread()?;
        let raw = thread_view.read(cx).message_editor.read(cx).text(cx);
        let cleaned = Self::clean_mention_links(&raw);
        let mut text: String = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
        if text.is_empty() {
            None
        } else {
            const MAX_CHARS: usize = 250;
            if let Some((truncate_at, _)) = text.char_indices().nth(MAX_CHARS) {
                text.truncate(truncate_at);
            }
            Some(text.into())
        }
    }

    fn clean_mention_links(input: &str) -> String {
        let mut result = String::with_capacity(input.len());
        let mut remaining = input;

        while let Some(start) = remaining.find("[@") {
            result.push_str(&remaining[..start]);
            let after_bracket = &remaining[start + 1..]; // skip '['
            if let Some(close_bracket) = after_bracket.find("](") {
                let mention = &after_bracket[..close_bracket]; // "@something"
                let after_link_start = &after_bracket[close_bracket + 2..]; // after "]("
                if let Some(close_paren) = after_link_start.find(')') {
                    result.push_str(mention);
                    remaining = &after_link_start[close_paren + 1..];
                    continue;
                }
            }
            // Couldn't parse full link syntax — emit the literal "[@" and move on.
            result.push_str("[@");
            remaining = &remaining[start + 2..];
        }
        result.push_str(remaining);
        result
    }

    fn all_thread_infos_for_workspace(
        workspace: &Entity<Workspace>,
        cx: &App,
    ) -> Vec<ActiveThreadInfo> {
        let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) else {
            return Vec::new();
        };
        let agent_panel_ref = agent_panel.read(cx);

        agent_panel_ref
            .parent_threads(cx)
            .into_iter()
            .map(|thread_view| {
                let thread_view_ref = thread_view.read(cx);
                let thread = thread_view_ref.thread.read(cx);

                let icon = thread_view_ref.agent_icon;
                let icon_from_external_svg = thread_view_ref.agent_icon_from_external_svg.clone();
                let title = thread
                    .title()
                    .unwrap_or_else(|| DEFAULT_THREAD_TITLE.into());
                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_ref.is_background_thread(&session_id);

                let status = if thread.is_waiting_for_confirmation() {
                    AgentThreadStatus::WaitingForConfirmation
                } else if thread.had_error() {
                    AgentThreadStatus::Error
                } else {
                    match thread.status() {
                        ThreadStatus::Generating => AgentThreadStatus::Running,
                        ThreadStatus::Idle => AgentThreadStatus::Completed,
                    }
                };

                let diff_stats = thread.action_log().read(cx).diff_stats(cx);

                ActiveThreadInfo {
                    session_id,
                    title,
                    status,
                    icon,
                    icon_from_external_svg,
                    is_background,
                    is_title_generating,
                    diff_stats,
                }
            })
            .collect()
    }

    fn thread_entry_from_metadata(
        row: &ThreadMetadata,
        workspace: ThreadEntryWorkspace,
        worktree_name: Option<SharedString>,
        agent_server_store: Option<&Entity<project::AgentServerStore>>,
        cx: &App,
    ) -> ThreadEntry {
        let (agent, icon, icon_from_external_svg) = match &row.agent_id {
            None => (Agent::NativeAgent, IconName::ZedAgent, None),
            Some(id) => {
                let custom_icon =
                    agent_server_store.and_then(|store| store.read(cx).agent_icon(id));
                (
                    Agent::Custom { id: id.clone() },
                    IconName::Terminal,
                    custom_icon,
                )
            }
        };
        ThreadEntry {
            agent,
            session_info: acp_thread::AgentSessionInfo {
                session_id: row.session_id.clone(),
                work_dirs: None,
                title: Some(row.title.clone()),
                updated_at: Some(row.updated_at),
                created_at: row.created_at,
                meta: None,
            },
            icon,
            icon_from_external_svg,
            status: AgentThreadStatus::default(),
            workspace,
            is_live: false,
            is_background: false,
            is_title_generating: false,
            highlight_positions: Vec::new(),
            worktree_name,
            worktree_full_path: None,
            worktree_highlight_positions: Vec::new(),
            diff_stats: DiffStats::default(),
        }
    }

    fn rebuild_contents(&mut self, cx: &App) {
        // When modifying this function, aim for a single forward pass over
        // workspaces and threads plus an O(T log T) sort, where T is the number of
        // threads. Avoid adding extra scans over the data.

        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };
        let mw = multi_workspace.read(cx);
        let workspaces = mw.workspaces().to_vec();
        let active_workspace = mw.workspaces().get(mw.active_workspace_index()).cloned();

        // Build a lookup for agent icons from the first workspace's AgentServerStore.
        let agent_server_store = workspaces
            .first()
            .map(|ws| ws.read(cx).project().read(cx).agent_server_store().clone());

        let query = self.filter_editor.read(cx).text(cx);

        // Re-derive agent_panel_visible from the active workspace so it stays
        // correct after workspace switches.
        self.agent_panel_visible = active_workspace
            .as_ref()
            .map_or(false, |ws| AgentPanel::is_visible(ws, cx));

        // Derive active_thread_is_draft BEFORE focused_thread so we can
        // use it as a guard below.
        self.active_thread_is_draft = active_workspace
            .as_ref()
            .and_then(|ws| ws.read(cx).panel::<AgentPanel>(cx))
            .map_or(false, |panel| panel.read(cx).active_thread_is_draft(cx));

        // Derive focused_thread from the active workspace's agent panel.
        // Only update when the panel gives us a positive signal — if the
        // panel returns None (e.g. still loading after a thread activation),
        // keep the previous value so eager writes from user actions survive.
        let panel_focused = active_workspace
            .as_ref()
            .and_then(|ws| ws.read(cx).panel::<AgentPanel>(cx))
            .and_then(|panel| {
                panel
                    .read(cx)
                    .active_conversation_view()
                    .and_then(|cv| cv.read(cx).parent_id(cx))
            });
        if panel_focused.is_some() && !self.active_thread_is_draft {
            self.focused_thread = panel_focused;
        }

        let previous = mem::take(&mut self.contents);

        let old_statuses: HashMap<acp::SessionId, AgentThreadStatus> = previous
            .entries
            .iter()
            .filter_map(|entry| match entry {
                ListEntry::Thread(thread) if thread.is_live => {
                    Some((thread.session_info.session_id.clone(), thread.status))
                }
                _ => None,
            })
            .collect();

        let mut entries = Vec::new();
        let mut notified_threads = previous.notified_threads;
        let mut current_session_ids: HashSet<acp::SessionId> = HashSet::new();
        let mut project_header_indices: Vec<usize> = Vec::new();

        // Build a mapping from worktree checkout paths → root repo paths so
        // that threads saved against a worktree checkout can be grouped under
        // the root repo's sidebar header.
        let worktree_to_root = build_worktree_root_mapping(&workspaces, cx);

        let thread_store = SidebarThreadMetadataStore::global(cx);
        let canonical_threads = build_canonical_thread_index(&worktree_to_root, cx);

        let workspace_groups = CanonicalWorkspaceGroups::build(&workspaces, &worktree_to_root, cx);

        let has_open_projects = !workspace_groups.is_empty();

        // Track session IDs claimed by open workspace groups so we can
        // identify truly unmatched threads for historical groups.
        let mut claimed_session_ids: HashSet<acp::SessionId> = HashSet::new();

        for canonical_key in &workspace_groups.order {
            let group_workspaces = &workspace_groups.groups[canonical_key];
            let label = workspace_label_from_path_list(canonical_key);

            let is_collapsed = self.collapsed_groups.contains(canonical_key);
            let should_load_threads = !is_collapsed || !query.is_empty();

            // Merge live thread info from ALL workspaces in the group.
            let mut live_infos: Vec<ActiveThreadInfo> = Vec::new();
            for ws in group_workspaces {
                live_infos.extend(Self::all_thread_infos_for_workspace(ws, cx));
            }

            let mut threads: Vec<ThreadEntry> = Vec::new();
            let mut has_running_threads = false;
            let mut waiting_thread_count: usize = 0;

            // Claim all threads for this group (even if collapsed) so
            // they don't appear as historical groups.
            if let Some(rows) = canonical_threads.get(canonical_key) {
                for row in rows {
                    claimed_session_ids.insert(row.session_id.clone());
                }
            }

            if should_load_threads {
                let all_rows = canonical_threads
                    .get(canonical_key)
                    .cloned()
                    .unwrap_or_default();
                for row in all_rows {
                    // Prefer the workspace whose raw paths match this
                    // thread's saved paths. Fall back to the first
                    // workspace in the group.
                    let target_workspace = group_workspaces
                        .iter()
                        .find(|ws| ws.read(cx).path_list(cx) == row.folder_paths)
                        .or(group_workspaces.first())
                        .map(|ws| ThreadEntryWorkspace::Open(ws.clone()))
                        .unwrap_or_else(|| ThreadEntryWorkspace::Closed(row.folder_paths.clone()));
                    // Show a worktree chip for single-root threads
                    // from a different checkout than the canonical key
                    // (e.g. "wt-feature-a" for a thread saved in that
                    // worktree checkout).
                    let worktree_name = if row.folder_paths.paths().len() == 1
                        && canonical_key.paths().len() == 1
                        && row.folder_paths != *canonical_key
                    {
                        linked_worktree_short_name(
                            &canonical_key.paths()[0],
                            &row.folder_paths.paths()[0],
                        )
                    } else {
                        None
                    };
                    threads.push(Self::thread_entry_from_metadata(
                        &row,
                        target_workspace,
                        worktree_name,
                        agent_server_store.as_ref(),
                        cx,
                    ));
                }

                // Build a lookup from live_infos and compute running/waiting
                // counts in a single pass.
                let mut live_info_by_session: HashMap<&acp::SessionId, &ActiveThreadInfo> =
                    HashMap::new();
                for info in &live_infos {
                    live_info_by_session.insert(&info.session_id, info);
                    if info.status == AgentThreadStatus::Running {
                        has_running_threads = true;
                    }
                    if info.status == AgentThreadStatus::WaitingForConfirmation {
                        waiting_thread_count += 1;
                    }
                }

                // Merge live info into threads and update notification state
                // in a single pass.
                for thread in &mut threads {
                    let session_id = &thread.session_info.session_id;

                    if let Some(info) = live_info_by_session.get(session_id) {
                        thread.session_info.title = Some(info.title.clone());
                        thread.status = info.status;
                        thread.icon = info.icon;
                        thread.icon_from_external_svg = info.icon_from_external_svg.clone();
                        thread.is_live = true;
                        thread.is_background = info.is_background;
                        thread.is_title_generating = info.is_title_generating;
                        thread.diff_stats = info.diff_stats;
                    }

                    let is_thread_workspace_active = match &thread.workspace {
                        ThreadEntryWorkspace::Open(thread_workspace) => active_workspace
                            .as_ref()
                            .is_some_and(|active| active == thread_workspace),
                        ThreadEntryWorkspace::Closed(_) => false,
                    };

                    if thread.status == AgentThreadStatus::Completed
                        && !is_thread_workspace_active
                        && old_statuses.get(session_id) == Some(&AgentThreadStatus::Running)
                    {
                        notified_threads.insert(session_id.clone());
                    }

                    if is_thread_workspace_active && !thread.is_background {
                        notified_threads.remove(session_id);
                    }
                }

                threads.sort_by(|a, b| {
                    let a_time = a.session_info.created_at.or(a.session_info.updated_at);
                    let b_time = b.session_info.created_at.or(b.session_info.updated_at);
                    b_time.cmp(&a_time)
                });
            } else {
                for info in &live_infos {
                    if info.status == AgentThreadStatus::Running {
                        has_running_threads = true;
                    }
                    if info.status == AgentThreadStatus::WaitingForConfirmation {
                        waiting_thread_count += 1;
                    }
                }
            }

            let highlight_positions = if !query.is_empty() {
                fuzzy_match_positions(&query, &label).unwrap_or_default()
            } else {
                Vec::new()
            };

            let header = ListEntry::ProjectHeader {
                path_list: canonical_key.clone(),
                label,
                workspace: group_workspaces.first().cloned(),
                highlight_positions,
                has_running_threads,
                waiting_thread_count,
            };

            let new_thread_entry = if query.is_empty() {
                let active_workspace_in_group = active_workspace
                    .as_ref()
                    .and_then(|active| group_workspaces.iter().find(|ws| *ws == active));
                let is_draft_for_workspace = self.agent_panel_visible
                    && self.active_thread_is_draft
                    && self.focused_thread.is_none()
                    && active_workspace_in_group.is_some();

                if threads.is_empty() || is_draft_for_workspace {
                    Some(ListEntry::NewThread {
                        path_list: canonical_key.clone(),
                        workspace: active_workspace_in_group
                            .cloned()
                            .unwrap_or_else(|| group_workspaces[0].clone()),
                        is_active_draft: is_draft_for_workspace,
                    })
                } else {
                    None
                }
            } else {
                None
            };

            let Some(group_entries) = render_thread_group(
                header,
                threads,
                new_thread_entry,
                &query,
                is_collapsed,
                canonical_key,
                &self.expanded_groups,
                self.focused_thread.as_ref(),
                Some(&notified_threads),
            ) else {
                continue;
            };

            for entry in group_entries {
                match &entry {
                    ListEntry::ProjectHeader { .. } => {
                        project_header_indices.push(entries.len());
                    }
                    ListEntry::Thread(thread) => {
                        current_session_ids.insert(thread.session_info.session_id.clone());
                    }
                    _ => {}
                }
                entries.push(entry);
            }
        }

        for entry in self.gather_historical_threads(
            &query,
            thread_store.read(cx),
            &claimed_session_ids,
            agent_server_store.as_ref(),
            cx,
        ) {
            match &entry {
                ListEntry::ProjectHeader { .. } => {
                    project_header_indices.push(entries.len());
                }
                ListEntry::Thread(thread) => {
                    current_session_ids.insert(thread.session_info.session_id.clone());
                }
                _ => {}
            }
            entries.push(entry);
        }

        // 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));

        self.contents = SidebarContents {
            entries,
            notified_threads,
            project_header_indices,
            has_open_projects,
        };
    }

    /// Create historical (closed) project groups for threads that weren't
    /// claimed by any open workspace. Group by raw folder_paths so each
    /// distinct workspace context gets its own section.
    fn gather_historical_threads(
        &self,
        query: &str,
        thread_store: &SidebarThreadMetadataStore,
        claimed_session_ids: &HashSet<agent_client_protocol::SessionId>,
        agent_server_store: Option<&Entity<project::AgentServerStore>>,
        cx: &App,
    ) -> Vec<ListEntry> {
        let mut entries = Vec::new();
        let mut historical_groups: HashMap<&PathList, Vec<&ThreadMetadata>> = HashMap::new();
        for entry in thread_store.all_entries() {
            if !claimed_session_ids.contains(&entry.session_id)
                && !entry.folder_paths.paths().is_empty()
            {
                historical_groups
                    .entry(&entry.folder_paths)
                    .or_default()
                    .push(entry);
            }
        }

        let mut sorted_keys: Vec<&&PathList> = historical_groups.keys().collect();
        sorted_keys.sort_by(|a, b| {
            workspace_label_from_path_list(a).cmp(&workspace_label_from_path_list(b))
        });

        for &path_list_key in &sorted_keys {
            let group_threads = &historical_groups[path_list_key];
            let label = workspace_label_from_path_list(path_list_key);
            let is_collapsed = self.collapsed_groups.contains(*path_list_key);

            let highlight_positions = if !query.is_empty() {
                fuzzy_match_positions(query, &label).unwrap_or_default()
            } else {
                Vec::new()
            };

            let header = ListEntry::ProjectHeader {
                path_list: (*path_list_key).clone(),
                label,
                workspace: None,
                highlight_positions,
                has_running_threads: false,
                waiting_thread_count: 0,
            };

            let mut threads: Vec<ThreadEntry> = group_threads
                .iter()
                .map(|row| {
                    Self::thread_entry_from_metadata(
                        row,
                        ThreadEntryWorkspace::Closed((*path_list_key).clone()),
                        None,
                        agent_server_store,
                        cx,
                    )
                })
                .collect();

            threads.sort_by(|a, b| {
                let a_time = a.session_info.created_at.or(a.session_info.updated_at);
                let b_time = b.session_info.created_at.or(b.session_info.updated_at);
                b_time.cmp(&a_time)
            });

            if let Some(group_entries) = render_thread_group(
                header,
                threads,
                None,
                query,
                is_collapsed,
                path_list_key,
                &self.expanded_groups,
                self.focused_thread.as_ref(),
                None,
            ) {
                entries.extend(group_entries);
            }
        }
        entries
    }

    /// 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 {
            return;
        };
        if !multi_workspace.read(cx).multi_workspace_enabled(cx) {
            return;
        }

        let had_notifications = self.has_notifications(cx);
        let scroll_position = self.list_state.logical_scroll_top();

        self.rebuild_contents(cx);

        self.list_state.reset(self.contents.entries.len());
        self.list_state.scroll_to(scroll_position);

        if had_notifications != self.has_notifications(cx) {
            multi_workspace.update(cx, |_, cx| {
                cx.notify();
            });
        }

        cx.notify();
    }

    fn select_first_entry(&mut self) {
        self.selection = self
            .contents
            .entries
            .iter()
            .position(|entry| matches!(entry, ListEntry::Thread(_)))
            .or_else(|| {
                if self.contents.entries.is_empty() {
                    None
                } else {
                    Some(0)
                }
            });
    }

    fn render_list_entry(
        &mut self,
        ix: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let Some(entry) = self.contents.entries.get(ix) else {
            return div().into_any_element();
        };
        let is_focused = self.focus_handle.is_focused(window);
        // is_selected means the keyboard selector is here.
        let is_selected = is_focused && self.selection == Some(ix);

        let is_group_header_after_first =
            ix > 0 && matches!(entry, ListEntry::ProjectHeader { .. });

        let rendered = match entry {
            ListEntry::ProjectHeader {
                path_list,
                label,
                workspace,
                highlight_positions,
                has_running_threads,
                waiting_thread_count,
            } => self.render_project_header(
                ix,
                false,
                path_list,
                label,
                workspace.as_ref(),
                highlight_positions,
                *has_running_threads,
                *waiting_thread_count,
                is_selected,
                cx,
            ),
            ListEntry::Thread(thread) => self.render_thread(ix, thread, is_selected, cx),
            ListEntry::ViewMore {
                path_list,
                is_fully_expanded,
            } => self.render_view_more(ix, path_list, *is_fully_expanded, is_selected, cx),
            ListEntry::NewThread {
                path_list,
                workspace,
                is_active_draft,
            } => {
                self.render_new_thread(ix, path_list, workspace, *is_active_draft, is_selected, cx)
            }
        };

        if is_group_header_after_first {
            v_flex()
                .w_full()
                .border_t_1()
                .border_color(cx.theme().colors().border.opacity(0.5))
                .child(rendered)
                .into_any_element()
        } else {
            rendered
        }
    }

    fn render_project_header(
        &self,
        ix: usize,
        is_sticky: bool,
        path_list: &PathList,
        label: &SharedString,
        workspace: Option<&Entity<Workspace>>,
        highlight_positions: &[usize],
        has_running_threads: bool,
        waiting_thread_count: usize,
        is_selected: bool,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let id_prefix = if is_sticky { "sticky-" } else { "" };
        let id = SharedString::from(format!("{id_prefix}project-header-{ix}"));
        let group_name = SharedString::from(format!("{id_prefix}header-group-{ix}"));

        let is_collapsed = self.collapsed_groups.contains(path_list);
        let disclosure_icon = if is_collapsed {
            IconName::ChevronRight
        } else {
            IconName::ChevronDown
        };

        let has_new_thread_entry = self
            .contents
            .entries
            .get(ix + 1)
            .is_some_and(|entry| matches!(entry, ListEntry::NewThread { .. }));
        let show_new_thread_button = !has_new_thread_entry && !self.has_filter_query(cx);

        let workspace_for_remove = workspace.cloned();
        let workspace_for_menu = workspace.cloned();

        let path_list_for_toggle = path_list.clone();
        let path_list_for_collapse = path_list.clone();
        let view_more_expanded = self.expanded_groups.contains_key(path_list);

        let multi_workspace = self.multi_workspace.upgrade();
        let workspace_count = multi_workspace
            .as_ref()
            .map_or(0, |mw| mw.read(cx).workspaces().len());

        let label = if highlight_positions.is_empty() {
            Label::new(label.clone())
                .color(Color::Muted)
                .into_any_element()
        } else {
            HighlightedLabel::new(label.clone(), highlight_positions.to_vec())
                .color(Color::Muted)
                .into_any_element()
        };

        let color = cx.theme().colors();
        let hover_color = color
            .element_active
            .blend(color.element_background.opacity(0.2));

        h_flex()
            .id(id)
            .group(&group_name)
            .h(Tab::content_height(cx))
            .w_full()
            .px_1p5()
            .border_1()
            .map(|this| {
                if is_selected {
                    this.border_color(color.border_focused)
                } else {
                    this.border_color(gpui::transparent_black())
                }
            })
            .justify_between()
            .hover(|s| s.bg(hover_color))
            .child(
                h_flex()
                    .relative()
                    .min_w_0()
                    .w_full()
                    .gap_1p5()
                    .child(
                        h_flex().size_4().flex_none().justify_center().child(
                            Icon::new(disclosure_icon)
                                .size(IconSize::Small)
                                .color(Color::Custom(cx.theme().colors().icon_muted.opacity(0.5))),
                        ),
                    )
                    .child(label)
                    .when(is_collapsed && has_running_threads, |this| {
                        this.child(
                            Icon::new(IconName::LoadCircle)
                                .size(IconSize::XSmall)
                                .color(Color::Muted)
                                .with_rotate_animation(2),
                        )
                    })
                    .when(is_collapsed && waiting_thread_count > 0, |this| {
                        let tooltip_text = if waiting_thread_count == 1 {
                            "1 thread is waiting for confirmation".to_string()
                        } else {
                            format!("{waiting_thread_count} threads are waiting for confirmation",)
                        };
                        this.child(
                            div()
                                .id(format!("{id_prefix}waiting-indicator-{ix}"))
                                .child(
                                    Icon::new(IconName::Warning)
                                        .size(IconSize::XSmall)
                                        .color(Color::Warning),
                                )
                                .tooltip(Tooltip::text(tooltip_text)),
                        )
                    }),
            )
            .child({
                let workspace_for_new_thread = workspace.cloned();
                let path_list_for_new_thread = path_list.clone();

                h_flex()
                    .when(self.project_header_menu_ix != Some(ix), |this| {
                        this.visible_on_hover(group_name)
                    })
                    .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
                        cx.stop_propagation();
                    })
                    .when_some(
                        workspace_for_menu
                            .as_ref()
                            .zip(workspace_for_remove.as_ref()),
                        |this, (menu_ws, remove_ws)| {
                            this.child(
                                self.render_project_header_menu(
                                    ix, id_prefix, menu_ws, remove_ws, cx,
                                ),
                            )
                        },
                    )
                    .when(view_more_expanded && !is_collapsed, |this| {
                        this.child(
                            IconButton::new(
                                SharedString::from(format!(
                                    "{id_prefix}project-header-collapse-{ix}",
                                )),
                                IconName::ListCollapse,
                            )
                            .icon_size(IconSize::Small)
                            .icon_color(Color::Muted)
                            .tooltip(Tooltip::text("Collapse Displayed Threads"))
                            .on_click(cx.listener({
                                let path_list_for_collapse = path_list_for_collapse.clone();
                                move |this, _, _window, cx| {
                                    this.selection = None;
                                    this.expanded_groups.remove(&path_list_for_collapse);
                                    this.update_entries(cx);
                                }
                            })),
                        )
                    })
                    .when_some(
                        workspace_for_remove.clone().filter(|_| workspace_count > 1),
                        |this, workspace_for_remove_btn| {
                            this.child(
                                IconButton::new(
                                    SharedString::from(format!(
                                        "{id_prefix}project-header-remove-{ix}",
                                    )),
                                    IconName::Close,
                                )
                                .icon_size(IconSize::Small)
                                .icon_color(Color::Muted)
                                .tooltip(Tooltip::text("Remove Project"))
                                .on_click(cx.listener(
                                    move |this, _, window, cx| {
                                        this.remove_workspace(
                                            &workspace_for_remove_btn,
                                            window,
                                            cx,
                                        );
                                    },
                                )),
                            )
                        },
                    )
                    .when_some(
                        workspace_for_new_thread.filter(|_| show_new_thread_button),
                        |this, workspace_for_new_thread| {
                            let path_list_for_new_thread = path_list_for_new_thread.clone();
                            this.child(
                                IconButton::new(
                                    SharedString::from(format!(
                                        "{id_prefix}project-header-new-thread-{ix}",
                                    )),
                                    IconName::Plus,
                                )
                                .icon_size(IconSize::Small)
                                .icon_color(Color::Muted)
                                .tooltip(Tooltip::text("New Thread"))
                                .on_click(cx.listener({
                                    let workspace_for_new_thread = workspace_for_new_thread.clone();
                                    let path_list_for_new_thread = path_list_for_new_thread.clone();
                                    move |this, _, window, cx| {
                                        this.collapsed_groups.remove(&path_list_for_new_thread);
                                        this.selection = None;
                                        this.create_new_thread(
                                            &workspace_for_new_thread,
                                            window,
                                            cx,
                                        );
                                    }
                                })),
                            )
                        },
                    )
            })
            .on_click(cx.listener(move |this, _, window, cx| {
                this.selection = None;
                this.toggle_collapse(&path_list_for_toggle, window, cx);
            }))
            // TODO: Decide if we really want the header to be activating different workspaces
            // .on_click(cx.listener(move |this, _, window, cx| {
            //     this.selection = None;
            //     this.activate_workspace(&workspace_for_activate, window, cx);
            // }))
            .into_any_element()
    }

    fn render_project_header_menu(
        &self,
        ix: usize,
        id_prefix: &str,
        workspace: &Entity<Workspace>,
        workspace_for_remove: &Entity<Workspace>,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let workspace_for_menu = workspace.clone();
        let workspace_for_remove = workspace_for_remove.clone();
        let multi_workspace = self.multi_workspace.clone();
        let this = cx.weak_entity();

        PopoverMenu::new(format!("{id_prefix}project-header-menu-{ix}"))
            .on_open(Rc::new({
                let this = this.clone();
                move |_window, cx| {
                    this.update(cx, |sidebar, cx| {
                        sidebar.project_header_menu_ix = Some(ix);
                        cx.notify();
                    })
                    .ok();
                }
            }))
            .menu(move |window, cx| {
                let workspace = workspace_for_menu.clone();
                let workspace_for_remove = workspace_for_remove.clone();
                let multi_workspace = multi_workspace.clone();

                let menu = ContextMenu::build_persistent(window, cx, move |menu, _window, cx| {
                    let worktrees: Vec<_> = workspace
                        .read(cx)
                        .visible_worktrees(cx)
                        .map(|worktree| {
                            let worktree_read = worktree.read(cx);
                            let id = worktree_read.id();
                            let name: SharedString =
                                worktree_read.root_name().as_unix_str().to_string().into();
                            (id, name)
                        })
                        .collect();

                    let worktree_count = worktrees.len();

                    let mut menu = menu
                        .header("Project Folders")
                        .end_slot_action(Box::new(menu::EndSlot));

                    for (worktree_id, name) in &worktrees {
                        let worktree_id = *worktree_id;
                        let workspace_for_worktree = workspace.clone();
                        let workspace_for_remove_worktree = workspace_for_remove.clone();
                        let multi_workspace_for_worktree = multi_workspace.clone();

                        let remove_handler = move |window: &mut Window, cx: &mut App| {
                            if worktree_count <= 1 {
                                if let Some(mw) = multi_workspace_for_worktree.upgrade() {
                                    let ws = workspace_for_remove_worktree.clone();
                                    mw.update(cx, |multi_workspace, cx| {
                                        if let Some(index) = multi_workspace
                                            .workspaces()
                                            .iter()
                                            .position(|w| *w == ws)
                                        {
                                            multi_workspace.remove_workspace(index, window, cx);
                                        }
                                    });
                                }
                            } else {
                                workspace_for_worktree.update(cx, |workspace, cx| {
                                    workspace.project().update(cx, |project, cx| {
                                        project.remove_worktree(worktree_id, cx);
                                    });
                                });
                            }
                        };

                        menu = menu.entry_with_end_slot_on_hover(
                            name.clone(),
                            None,
                            |_, _| {},
                            IconName::Close,
                            "Remove Folder".into(),
                            remove_handler,
                        );
                    }

                    let workspace_for_add = workspace.clone();
                    let multi_workspace_for_add = multi_workspace.clone();
                    let menu = menu.separator().entry(
                        "Add Folder to Project",
                        Some(Box::new(AddFolderToProject)),
                        move |window, cx| {
                            if let Some(mw) = multi_workspace_for_add.upgrade() {
                                mw.update(cx, |mw, cx| {
                                    mw.activate(workspace_for_add.clone(), cx);
                                });
                            }
                            workspace_for_add.update(cx, |workspace, cx| {
                                workspace.add_folder_to_project(&AddFolderToProject, window, cx);
                            });
                        },
                    );

                    let workspace_count = multi_workspace
                        .upgrade()
                        .map_or(0, |mw| mw.read(cx).workspaces().len());
                    if workspace_count > 1 {
                        let workspace_for_move = workspace.clone();
                        let multi_workspace_for_move = multi_workspace.clone();
                        menu.entry(
                            "Move to New Window",
                            Some(Box::new(
                                zed_actions::agents_sidebar::MoveWorkspaceToNewWindow,
                            )),
                            move |window, cx| {
                                if let Some(mw) = multi_workspace_for_move.upgrade() {
                                    mw.update(cx, |multi_workspace, cx| {
                                        if let Some(index) = multi_workspace
                                            .workspaces()
                                            .iter()
                                            .position(|w| *w == workspace_for_move)
                                        {
                                            multi_workspace
                                                .move_workspace_to_new_window(index, window, cx);
                                        }
                                    });
                                }
                            },
                        )
                    } else {
                        menu
                    }
                });

                let this = this.clone();
                window
                    .subscribe(&menu, cx, move |_, _: &gpui::DismissEvent, _window, cx| {
                        this.update(cx, |sidebar, cx| {
                            sidebar.project_header_menu_ix = None;
                            cx.notify();
                        })
                        .ok();
                    })
                    .detach();

                Some(menu)
            })
            .trigger(
                IconButton::new(
                    SharedString::from(format!("{id_prefix}-ellipsis-menu-{ix}")),
                    IconName::Ellipsis,
                )
                .selected_style(ButtonStyle::Tinted(TintColor::Accent))
                .icon_size(IconSize::Small)
                .icon_color(Color::Muted),
            )
            .anchor(gpui::Corner::TopRight)
            .offset(gpui::Point {
                x: px(0.),
                y: px(1.),
            })
    }

    fn render_sticky_header(
        &self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<AnyElement> {
        let scroll_top = self.list_state.logical_scroll_top();

        let &header_idx = self
            .contents
            .project_header_indices
            .iter()
            .rev()
            .find(|&&idx| idx <= scroll_top.item_ix)?;

        let needs_sticky = header_idx < scroll_top.item_ix
            || (header_idx == scroll_top.item_ix && scroll_top.offset_in_item > px(0.));

        if !needs_sticky {
            return None;
        }

        let ListEntry::ProjectHeader {
            path_list,
            label,
            workspace,
            highlight_positions,
            has_running_threads,
            waiting_thread_count,
        } = self.contents.entries.get(header_idx)?
        else {
            return None;
        };

        let is_focused = self.focus_handle.is_focused(window);
        let is_selected = is_focused && self.selection == Some(header_idx);

        let header_element = self.render_project_header(
            header_idx,
            true,
            &path_list,
            &label,
            workspace.as_ref(),
            &highlight_positions,
            *has_running_threads,
            *waiting_thread_count,
            is_selected,
            cx,
        );

        let top_offset = self
            .contents
            .project_header_indices
            .iter()
            .find(|&&idx| idx > header_idx)
            .and_then(|&next_idx| {
                let bounds = self.list_state.bounds_for_item(next_idx)?;
                let viewport = self.list_state.viewport_bounds();
                let y_in_viewport = bounds.origin.y - viewport.origin.y;
                let header_height = bounds.size.height;
                (y_in_viewport < header_height).then_some(y_in_viewport - header_height)
            })
            .unwrap_or(px(0.));

        let color = cx.theme().colors();
        let background = color
            .title_bar_background
            .blend(color.panel_background.opacity(0.2));

        let element = v_flex()
            .absolute()
            .top(top_offset)
            .left_0()
            .w_full()
            .bg(background)
            .border_b_1()
            .border_color(color.border.opacity(0.5))
            .child(header_element)
            .shadow_xs()
            .into_any_element();

        Some(element)
    }

    fn prune_stale_worktree_workspaces(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };
        let workspaces = multi_workspace.read(cx).workspaces().to_vec();

        // Collect all worktree paths that are currently listed by any main
        // repo open in any workspace.
        let mut known_worktree_paths: HashSet<std::path::PathBuf> = HashSet::new();
        for workspace in &workspaces {
            for snapshot in root_repository_snapshots(workspace, cx) {
                if snapshot.work_directory_abs_path != snapshot.original_repo_abs_path {
                    continue;
                }
                for git_worktree in snapshot.linked_worktrees() {
                    known_worktree_paths.insert(git_worktree.path.to_path_buf());
                }
            }
        }

        // Collect ALL workspace root paths so we can check if the main
        // repo is open without depending on git scan completion.
        let all_workspace_roots: HashSet<PathBuf> = workspaces
            .iter()
            .flat_map(|ws| ws.read(cx).root_paths(cx))
            .map(|p| p.to_path_buf())
            .collect();

        // Find workspaces that consist of exactly one root folder which is a
        // stale worktree checkout. Multi-root workspaces are never pruned —
        // losing one worktree shouldn't destroy a workspace that also
        // contains other folders.
        let mut to_remove: Vec<Entity<Workspace>> = Vec::new();
        for workspace in &workspaces {
            let path_list = workspace.read(cx).path_list(cx);
            if path_list.paths().len() != 1 {
                continue;
            }
            let should_prune = root_repository_snapshots(workspace, cx)
                .iter()
                .any(|snapshot| {
                    snapshot.work_directory_abs_path != snapshot.original_repo_abs_path
                        && !known_worktree_paths.contains(snapshot.work_directory_abs_path.as_ref())
                        // Don't prune if another workspace has the main
                        // repo's path as its root — the linked-worktree
                        // list may not be loaded yet.
                        && !all_workspace_roots
                            .contains(snapshot.original_repo_abs_path.as_ref())
                });
            if should_prune {
                to_remove.push(workspace.clone());
            }
        }

        for workspace in &to_remove {
            self.remove_workspace(workspace, window, cx);
        }
    }

    fn remove_workspace(
        &mut self,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };

        multi_workspace.update(cx, |multi_workspace, cx| {
            let Some(index) = multi_workspace
                .workspaces()
                .iter()
                .position(|w| w == workspace)
            else {
                return;
            };
            multi_workspace.remove_workspace(index, window, cx);
        });
    }

    fn toggle_collapse(
        &mut self,
        path_list: &PathList,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.collapsed_groups.contains(path_list) {
            self.collapsed_groups.remove(path_list);
        } else {
            self.collapsed_groups.insert(path_list.clone());
        }
        self.update_entries(cx);
    }

    fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.focus_handle.is_focused(window) {
            return;
        }

        if let SidebarView::Archive(archive) = &self.view {
            let has_selection = archive.read(cx).has_selection();
            if !has_selection {
                archive.update(cx, |view, cx| view.focus_filter_editor(window, cx));
            }
        } else if self.selection.is_none() {
            self.filter_editor.focus_handle(cx).focus(window, cx);
        }
    }

    fn cancel(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context<Self>) {
        if self.reset_filter_editor_text(window, cx) {
            self.update_entries(cx);
        } else {
            self.selection = None;
            self.filter_editor.focus_handle(cx).focus(window, cx);
            cx.notify();
        }
    }

    fn focus_sidebar_filter(
        &mut self,
        _: &FocusSidebarFilter,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.selection = None;
        if let SidebarView::Archive(archive) = &self.view {
            archive.update(cx, |view, cx| {
                view.clear_selection();
                view.focus_filter_editor(window, cx);
            });
        } else {
            self.filter_editor.focus_handle(cx).focus(window, cx);
        }

        // When vim mode is active, the editor defaults to normal mode which
        // blocks text input. Switch to insert mode so the user can type
        // immediately.
        if vim_mode_setting::VimModeSetting::get_global(cx).0 {
            if let Ok(action) = cx.build_action("vim::SwitchToInsertMode", None) {
                window.dispatch_action(action, cx);
            }
        }

        cx.notify();
    }

    fn reset_filter_editor_text(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
        self.filter_editor.update(cx, |editor, cx| {
            if editor.buffer().read(cx).len(cx).0 > 0 {
                editor.set_text("", window, cx);
                true
            } else {
                false
            }
        })
    }

    fn has_filter_query(&self, cx: &App) -> bool {
        !self.filter_editor.read(cx).text(cx).is_empty()
    }

    fn editor_move_down(&mut self, _: &MoveDown, window: &mut Window, cx: &mut Context<Self>) {
        self.select_next(&SelectNext, window, cx);
        if self.selection.is_some() {
            self.focus_handle.focus(window, cx);
        }
    }

    fn editor_move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
        self.select_previous(&SelectPrevious, window, cx);
        if self.selection.is_some() {
            self.focus_handle.focus(window, cx);
        }
    }

    fn editor_confirm(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if self.selection.is_none() {
            self.select_next(&SelectNext, window, cx);
        }
        if self.selection.is_some() {
            self.focus_handle.focus(window, cx);
        }
    }

    fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
        let next = match self.selection {
            Some(ix) if ix + 1 < self.contents.entries.len() => ix + 1,
            Some(_) if !self.contents.entries.is_empty() => 0,
            None if !self.contents.entries.is_empty() => 0,
            _ => return,
        };
        self.selection = Some(next);
        self.list_state.scroll_to_reveal_item(next);
        cx.notify();
    }

    fn select_previous(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
        match self.selection {
            Some(0) => {
                self.selection = None;
                self.filter_editor.focus_handle(cx).focus(window, cx);
                cx.notify();
            }
            Some(ix) => {
                self.selection = Some(ix - 1);
                self.list_state.scroll_to_reveal_item(ix - 1);
                cx.notify();
            }
            None if !self.contents.entries.is_empty() => {
                let last = self.contents.entries.len() - 1;
                self.selection = Some(last);
                self.list_state.scroll_to_reveal_item(last);
                cx.notify();
            }
            None => {}
        }
    }

    fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context<Self>) {
        if !self.contents.entries.is_empty() {
            self.selection = Some(0);
            self.list_state.scroll_to_reveal_item(0);
            cx.notify();
        }
    }

    fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
        if let Some(last) = self.contents.entries.len().checked_sub(1) {
            self.selection = Some(last);
            self.list_state.scroll_to_reveal_item(last);
            cx.notify();
        }
    }

    fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
        let Some(ix) = self.selection else { return };
        let Some(entry) = self.contents.entries.get(ix) else {
            return;
        };

        match entry {
            ListEntry::ProjectHeader { path_list, .. } => {
                let path_list = path_list.clone();
                self.toggle_collapse(&path_list, window, cx);
            }
            ListEntry::Thread(thread) => {
                let session_info = thread.session_info.clone();
                match &thread.workspace {
                    ThreadEntryWorkspace::Open(workspace) => {
                        let workspace = workspace.clone();
                        self.activate_thread(
                            thread.agent.clone(),
                            session_info,
                            &workspace,
                            window,
                            cx,
                        );
                    }
                    ThreadEntryWorkspace::Closed(path_list) => {
                        self.open_workspace_and_activate_thread(
                            thread.agent.clone(),
                            session_info,
                            path_list.clone(),
                            window,
                            cx,
                        );
                    }
                }
            }
            ListEntry::ViewMore {
                path_list,
                is_fully_expanded,
                ..
            } => {
                let path_list = path_list.clone();
                if *is_fully_expanded {
                    self.expanded_groups.remove(&path_list);
                } else {
                    let current = self.expanded_groups.get(&path_list).copied().unwrap_or(0);
                    self.expanded_groups.insert(path_list, current + 1);
                }
                self.update_entries(cx);
            }
            ListEntry::NewThread { workspace, .. } => {
                let workspace = workspace.clone();
                self.create_new_thread(&workspace, window, cx);
            }
        }
    }

    fn find_workspace_across_windows(
        &self,
        cx: &App,
        predicate: impl Fn(&Entity<Workspace>, &App) -> bool,
    ) -> Option<(WindowHandle<MultiWorkspace>, Entity<Workspace>)> {
        cx.windows()
            .into_iter()
            .filter_map(|window| window.downcast::<MultiWorkspace>())
            .find_map(|window| {
                let workspace = window.read(cx).ok().and_then(|multi_workspace| {
                    multi_workspace
                        .workspaces()
                        .iter()
                        .find(|workspace| predicate(workspace, cx))
                        .cloned()
                })?;
                Some((window, workspace))
            })
    }

    fn find_workspace_in_current_window(
        &self,
        cx: &App,
        predicate: impl Fn(&Entity<Workspace>, &App) -> bool,
    ) -> Option<Entity<Workspace>> {
        self.multi_workspace.upgrade().and_then(|multi_workspace| {
            multi_workspace
                .read(cx)
                .workspaces()
                .iter()
                .find(|workspace| predicate(workspace, cx))
                .cloned()
        })
    }

    fn load_agent_thread_in_workspace(
        workspace: &Entity<Workspace>,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        window: &mut Window,
        cx: &mut App,
    ) {
        workspace.update(cx, |workspace, cx| {
            workspace.open_panel::<AgentPanel>(window, cx);
        });

        if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
            agent_panel.update(cx, |panel, cx| {
                panel.load_agent_thread(
                    agent,
                    session_info.session_id,
                    session_info.work_dirs,
                    session_info.title,
                    true,
                    window,
                    cx,
                );
            });
        }
    }

    fn activate_thread_locally(
        &mut self,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };

        // Set focused_thread eagerly so the sidebar highlight updates
        // immediately, rather than waiting for a deferred AgentPanel
        // event which can race with ActiveWorkspaceChanged clearing it.
        self.focused_thread = Some(session_info.session_id.clone());

        multi_workspace.update(cx, |multi_workspace, cx| {
            multi_workspace.activate(workspace.clone(), cx);
        });

        Self::load_agent_thread_in_workspace(workspace, agent, session_info, window, cx);

        self.update_entries(cx);
    }

    fn activate_thread_in_other_window(
        &self,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        workspace: Entity<Workspace>,
        target_window: WindowHandle<MultiWorkspace>,
        cx: &mut Context<Self>,
    ) {
        let target_session_id = session_info.session_id.clone();

        let activated = target_window
            .update(cx, |multi_workspace, window, cx| {
                window.activate_window();
                multi_workspace.activate(workspace.clone(), cx);
                Self::load_agent_thread_in_workspace(&workspace, agent, session_info, window, cx);
            })
            .log_err()
            .is_some();

        if activated {
            if let Some(target_sidebar) = target_window
                .read(cx)
                .ok()
                .and_then(|multi_workspace| {
                    multi_workspace.sidebar().map(|sidebar| sidebar.to_any())
                })
                .and_then(|sidebar| sidebar.downcast::<Self>().ok())
            {
                target_sidebar.update(cx, |sidebar, cx| {
                    sidebar.focused_thread = Some(target_session_id);
                    sidebar.update_entries(cx);
                });
            }
        }
    }

    fn activate_thread(
        &mut self,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self
            .find_workspace_in_current_window(cx, |candidate, _| candidate == workspace)
            .is_some()
        {
            self.activate_thread_locally(agent, session_info, &workspace, window, cx);
            return;
        }

        let Some((target_window, workspace)) =
            self.find_workspace_across_windows(cx, |candidate, _| candidate == workspace)
        else {
            return;
        };

        self.activate_thread_in_other_window(agent, session_info, workspace, target_window, cx);
    }

    fn open_workspace_and_activate_thread(
        &mut self,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        path_list: PathList,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };

        let paths: Vec<std::path::PathBuf> =
            path_list.paths().iter().map(|p| p.to_path_buf()).collect();

        let open_task = multi_workspace.update(cx, |mw, cx| mw.open_project(paths, window, cx));

        cx.spawn_in(window, async move |this, cx| {
            let workspace = open_task.await?;
            this.update_in(cx, |this, window, cx| {
                this.activate_thread(agent, session_info, &workspace, window, cx);
            })?;
            anyhow::Ok(())
        })
        .detach_and_log_err(cx);
    }

    fn find_current_workspace_for_path_list(
        &self,
        path_list: &PathList,
        cx: &App,
    ) -> Option<Entity<Workspace>> {
        self.find_workspace_in_current_window(cx, |workspace, cx| {
            let worktree_to_root = build_worktree_root_mapping(&[workspace.clone()], cx);
            let canonical_thread = canonicalize_path_list(path_list, &worktree_to_root);
            let canonical_workspace =
                canonicalize_path_list(&workspace.read(cx).path_list(cx), &worktree_to_root);
            canonical_workspace == canonical_thread
        })
    }

    fn find_open_workspace_for_path_list(
        &self,
        path_list: &PathList,
        cx: &App,
    ) -> Option<(WindowHandle<MultiWorkspace>, Entity<Workspace>)> {
        self.find_workspace_across_windows(cx, |workspace, cx| {
            let worktree_to_root = build_worktree_root_mapping(&[workspace.clone()], cx);
            let canonical_thread = canonicalize_path_list(path_list, &worktree_to_root);
            let canonical_workspace =
                canonicalize_path_list(&workspace.read(cx).path_list(cx), &worktree_to_root);
            canonical_workspace == canonical_thread
        })
    }

    fn activate_archived_thread(
        &mut self,
        agent: Agent,
        session_info: acp_thread::AgentSessionInfo,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Eagerly save thread metadata so that the sidebar is updated immediately
        SidebarThreadMetadataStore::global(cx)
            .update(cx, |store, cx| {
                store.save(
                    ThreadMetadata::from_session_info(agent.id(), &session_info),
                    cx,
                )
            })
            .detach_and_log_err(cx);

        if let Some(path_list) = &session_info.work_dirs {
            if let Some(workspace) = self.find_current_workspace_for_path_list(path_list, cx) {
                self.activate_thread_locally(agent, session_info, &workspace, window, cx);
            } else if let Some((target_window, workspace)) =
                self.find_open_workspace_for_path_list(path_list, cx)
            {
                self.activate_thread_in_other_window(
                    agent,
                    session_info,
                    workspace,
                    target_window,
                    cx,
                );
            } else {
                // No matching workspace is open. The thread metadata was
                // already saved above, so `rebuild_contents` will create a
                // closed historical group for it. Just highlight it.
                self.focused_thread = Some(session_info.session_id.clone());
                self.update_entries(cx);
            }
            return;
        }

        let active_workspace = self.multi_workspace.upgrade().and_then(|w| {
            w.read(cx)
                .workspaces()
                .get(w.read(cx).active_workspace_index())
                .cloned()
        });

        if let Some(workspace) = active_workspace {
            self.activate_thread_locally(agent, session_info, &workspace, window, cx);
        }
    }

    fn expand_selected_entry(
        &mut self,
        _: &SelectChild,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(ix) = self.selection else { return };

        match self.contents.entries.get(ix) {
            Some(ListEntry::ProjectHeader { path_list, .. }) => {
                if self.collapsed_groups.contains(path_list) {
                    let path_list = path_list.clone();
                    self.collapsed_groups.remove(&path_list);
                    self.update_entries(cx);
                } else if ix + 1 < self.contents.entries.len() {
                    self.selection = Some(ix + 1);
                    self.list_state.scroll_to_reveal_item(ix + 1);
                    cx.notify();
                }
            }
            _ => {}
        }
    }

    fn collapse_selected_entry(
        &mut self,
        _: &SelectParent,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(ix) = self.selection else { return };

        match self.contents.entries.get(ix) {
            Some(ListEntry::ProjectHeader { path_list, .. }) => {
                if !self.collapsed_groups.contains(path_list) {
                    let path_list = path_list.clone();
                    self.collapsed_groups.insert(path_list);
                    self.update_entries(cx);
                }
            }
            Some(
                ListEntry::Thread(_) | ListEntry::ViewMore { .. } | ListEntry::NewThread { .. },
            ) => {
                for i in (0..ix).rev() {
                    if let Some(ListEntry::ProjectHeader { path_list, .. }) =
                        self.contents.entries.get(i)
                    {
                        let path_list = path_list.clone();
                        self.selection = Some(i);
                        self.collapsed_groups.insert(path_list);
                        self.update_entries(cx);
                        break;
                    }
                }
            }
            None => {}
        }
    }

    fn toggle_selected_fold(
        &mut self,
        _: &editor::actions::ToggleFold,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(ix) = self.selection else { return };

        // 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::NewThread { .. },
            ) => (0..ix).rev().find(|&i| {
                matches!(
                    self.contents.entries.get(i),
                    Some(ListEntry::ProjectHeader { .. })
                )
            }),
            None => None,
        };

        if let Some(header_ix) = header_ix {
            if let Some(ListEntry::ProjectHeader { path_list, .. }) =
                self.contents.entries.get(header_ix)
            {
                let path_list = path_list.clone();
                if self.collapsed_groups.contains(&path_list) {
                    self.collapsed_groups.remove(&path_list);
                } else {
                    self.selection = Some(header_ix);
                    self.collapsed_groups.insert(path_list);
                }
                self.update_entries(cx);
            }
        }
    }

    fn fold_all(
        &mut self,
        _: &editor::actions::FoldAll,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        for entry in &self.contents.entries {
            if let ListEntry::ProjectHeader { path_list, .. } = entry {
                self.collapsed_groups.insert(path_list.clone());
            }
        }
        self.update_entries(cx);
    }

    fn unfold_all(
        &mut self,
        _: &editor::actions::UnfoldAll,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.collapsed_groups.clear();
        self.update_entries(cx);
    }

    fn stop_thread(&mut self, session_id: &acp::SessionId, cx: &mut Context<Self>) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };

        let workspaces = multi_workspace.read(cx).workspaces().to_vec();
        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));
                if cancelled {
                    return;
                }
            }
        }
    }

    fn archive_thread(
        &mut self,
        session_id: &acp::SessionId,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // If we're archiving the currently focused thread, move focus to the
        // nearest thread within the same project group. We never cross group
        // boundaries — if the group has no other threads, clear focus and open
        // a blank new thread in the panel instead.
        if self.focused_thread.as_ref() == Some(session_id) {
            let current_pos = self.contents.entries.iter().position(|entry| {
                matches!(entry, ListEntry::Thread(t) if &t.session_info.session_id == session_id)
            });

            // Find the workspace that owns this thread's project group by
            // walking backwards to the nearest ProjectHeader. We must use
            // *this* workspace (not the active workspace) because the user
            // might be archiving a thread in a non-active group.
            let group_workspace = current_pos.and_then(|pos| {
                self.contents.entries[..pos]
                    .iter()
                    .rev()
                    .find_map(|e| match e {
                        ListEntry::ProjectHeader { workspace, .. } => workspace.clone(),
                        _ => None,
                    })
            });

            let next_thread = current_pos.and_then(|pos| {
                let group_start = self.contents.entries[..pos]
                    .iter()
                    .rposition(|e| matches!(e, ListEntry::ProjectHeader { .. }))
                    .map_or(0, |i| i + 1);
                let group_end = self.contents.entries[pos + 1..]
                    .iter()
                    .position(|e| matches!(e, ListEntry::ProjectHeader { .. }))
                    .map_or(self.contents.entries.len(), |i| pos + 1 + i);

                let above = self.contents.entries[group_start..pos]
                    .iter()
                    .rev()
                    .find_map(|entry| {
                        if let ListEntry::Thread(t) = entry {
                            Some(t)
                        } else {
                            None
                        }
                    });

                above.or_else(|| {
                    self.contents.entries[pos + 1..group_end]
                        .iter()
                        .find_map(|entry| {
                            if let ListEntry::Thread(t) = entry {
                                Some(t)
                            } else {
                                None
                            }
                        })
                })
            });

            if let Some(next) = next_thread {
                self.focused_thread = Some(next.session_info.session_id.clone());

                if let Some(workspace) = &group_workspace {
                    if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
                        agent_panel.update(cx, |panel, cx| {
                            panel.load_agent_thread(
                                next.agent.clone(),
                                next.session_info.session_id.clone(),
                                next.session_info.work_dirs.clone(),
                                next.session_info.title.clone(),
                                true,
                                window,
                                cx,
                            );
                        });
                    }
                }
            } else {
                self.focused_thread = None;
                if let Some(workspace) = &group_workspace {
                    if let Some(agent_panel) = workspace.read(cx).panel::<AgentPanel>(cx) {
                        agent_panel.update(cx, |panel, cx| {
                            panel.new_thread(&NewThread, window, cx);
                        });
                    }
                }
            }
        }

        SidebarThreadMetadataStore::global(cx)
            .update(cx, |store, cx| store.delete(session_id.clone(), cx))
            .detach_and_log_err(cx);
    }

    fn remove_selected_thread(
        &mut self,
        _: &RemoveSelectedThread,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(ix) = self.selection else {
            return;
        };
        let Some(ListEntry::Thread(thread)) = self.contents.entries.get(ix) else {
            return;
        };
        if thread.agent != Agent::NativeAgent {
            return;
        }
        let session_id = thread.session_info.session_id.clone();
        self.archive_thread(&session_id, window, cx);
    }

    fn render_thread(
        &self,
        ix: usize,
        thread: &ThreadEntry,
        is_focused: bool,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let has_notification = self
            .contents
            .is_thread_notified(&thread.session_info.session_id);

        let title: SharedString = thread
            .session_info
            .title
            .clone()
            .unwrap_or_else(|| "Untitled".into());
        let session_info = thread.session_info.clone();
        let thread_workspace = thread.workspace.clone();

        let is_hovered = self.hovered_thread_index == Some(ix);
        let is_selected = self.agent_panel_visible
            && self.focused_thread.as_ref() == Some(&session_info.session_id);
        let is_running = matches!(
            thread.status,
            AgentThreadStatus::Running | AgentThreadStatus::WaitingForConfirmation
        );

        let session_id_for_delete = thread.session_info.session_id.clone();
        let focus_handle = self.focus_handle.clone();

        let id = SharedString::from(format!("thread-entry-{}", ix));

        let timestamp = thread
            .session_info
            .created_at
            .or(thread.session_info.updated_at)
            .map(format_history_entry_timestamp);

        ThreadItem::new(id, title)
            .icon(thread.icon)
            .status(thread.status)
            .when_some(thread.icon_from_external_svg.clone(), |this, svg| {
                this.custom_icon_from_external_svg(svg)
            })
            .when_some(thread.worktree_name.clone(), |this, name| {
                let this = this.worktree(name);
                match thread.worktree_full_path.clone() {
                    Some(path) => this.worktree_full_path(path),
                    None => this,
                }
            })
            .worktree_highlight_positions(thread.worktree_highlight_positions.clone())
            .when_some(timestamp, |this, ts| this.timestamp(ts))
            .highlight_positions(thread.highlight_positions.to_vec())
            .title_generating(thread.is_title_generating)
            .notified(has_notification)
            .when(thread.diff_stats.lines_added > 0, |this| {
                this.added(thread.diff_stats.lines_added as usize)
            })
            .when(thread.diff_stats.lines_removed > 0, |this| {
                this.removed(thread.diff_stats.lines_removed as usize)
            })
            .selected(is_selected)
            .focused(is_focused)
            .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();
            }))
            .when(is_hovered && is_running, |this| {
                this.action_slot(
                    IconButton::new("stop-thread", IconName::Stop)
                        .icon_size(IconSize::Small)
                        .icon_color(Color::Error)
                        .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);
                            })
                        }),
                )
            })
            .when(is_hovered && !is_running, |this| {
                this.action_slot(
                    IconButton::new("archive-thread", IconName::Archive)
                        .icon_size(IconSize::Small)
                        .icon_color(Color::Muted)
                        .tooltip({
                            let focus_handle = focus_handle.clone();
                            move |_window, cx| {
                                Tooltip::for_action_in(
                                    "Archive Thread",
                                    &RemoveSelectedThread,
                                    &focus_handle,
                                    cx,
                                )
                            }
                        })
                        .on_click({
                            let session_id = session_id_for_delete.clone();
                            cx.listener(move |this, _, window, cx| {
                                this.archive_thread(&session_id, window, cx);
                            })
                        }),
                )
            })
            .on_click({
                let agent = thread.agent.clone();
                cx.listener(move |this, _, window, cx| {
                    this.selection = None;
                    match &thread_workspace {
                        ThreadEntryWorkspace::Open(workspace) => {
                            this.activate_thread(
                                agent.clone(),
                                session_info.clone(),
                                workspace,
                                window,
                                cx,
                            );
                        }
                        ThreadEntryWorkspace::Closed(path_list) => {
                            this.open_workspace_and_activate_thread(
                                agent.clone(),
                                session_info.clone(),
                                path_list.clone(),
                                window,
                                cx,
                            );
                        }
                    }
                })
            })
            .into_any_element()
    }

    fn render_filter_input(&self, cx: &mut Context<Self>) -> impl IntoElement {
        div()
            .min_w_0()
            .flex_1()
            .capture_action(
                cx.listener(|this, _: &editor::actions::Newline, window, cx| {
                    this.editor_confirm(window, cx);
                }),
            )
            .child(self.filter_editor.clone())
    }

    fn render_recent_projects_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let multi_workspace = self.multi_workspace.upgrade();

        let workspace = multi_workspace
            .as_ref()
            .map(|mw| mw.read(cx).workspace().downgrade());

        let focus_handle = workspace
            .as_ref()
            .and_then(|ws| ws.upgrade())
            .map(|w| w.read(cx).focus_handle(cx))
            .unwrap_or_else(|| cx.focus_handle());

        let sibling_workspace_ids: HashSet<WorkspaceId> = multi_workspace
            .as_ref()
            .map(|mw| {
                mw.read(cx)
                    .workspaces()
                    .iter()
                    .filter_map(|ws| ws.read(cx).database_id())
                    .collect()
            })
            .unwrap_or_default();

        let popover_handle = self.recent_projects_popover_handle.clone();

        PopoverMenu::new("sidebar-recent-projects-menu")
            .with_handle(popover_handle)
            .menu(move |window, cx| {
                workspace.as_ref().map(|ws| {
                    SidebarRecentProjects::popover(
                        ws.clone(),
                        sibling_workspace_ids.clone(),
                        focus_handle.clone(),
                        window,
                        cx,
                    )
                })
            })
            .trigger_with_tooltip(
                IconButton::new("open-project", IconName::OpenFolder)
                    .icon_size(IconSize::Small)
                    .selected_style(ButtonStyle::Tinted(TintColor::Accent)),
                |_window, cx| {
                    Tooltip::for_action(
                        "Add Project",
                        &OpenRecent {
                            create_new_window: false,
                        },
                        cx,
                    )
                },
            )
            .offset(gpui::Point {
                x: px(-2.0),
                y: px(-2.0),
            })
            .anchor(gpui::Corner::BottomRight)
    }

    fn render_view_more(
        &self,
        ix: usize,
        path_list: &PathList,
        is_fully_expanded: bool,
        is_selected: bool,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let path_list = path_list.clone();
        let id = SharedString::from(format!("view-more-{}", ix));

        let label: SharedString = if is_fully_expanded {
            "Collapse".into()
        } else {
            "View More".into()
        };

        ThreadItem::new(id, label)
            .focused(is_selected)
            .icon_visible(false)
            .title_label_color(Color::Muted)
            .on_click(cx.listener(move |this, _, _window, cx| {
                this.selection = None;
                if is_fully_expanded {
                    this.expanded_groups.remove(&path_list);
                } else {
                    let current = this.expanded_groups.get(&path_list).copied().unwrap_or(0);
                    this.expanded_groups.insert(path_list.clone(), current + 1);
                }
                this.update_entries(cx);
            }))
            .into_any_element()
    }

    fn new_thread_in_group(
        &mut self,
        _: &NewThreadInGroup,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // If there is a keyboard selection, walk backwards through
        // `project_header_indices` to find the header that owns the selected
        // row. Otherwise fall back to the active workspace.
        let workspace = if let Some(selected_ix) = self.selection {
            self.contents
                .project_header_indices
                .iter()
                .rev()
                .find(|&&header_ix| header_ix <= selected_ix)
                .and_then(|&header_ix| match &self.contents.entries[header_ix] {
                    ListEntry::ProjectHeader { workspace, .. } => workspace.clone(),
                    _ => None,
                })
        } else {
            // Use the currently active workspace.
            self.multi_workspace
                .upgrade()
                .map(|mw| mw.read(cx).workspace().clone())
        };

        let Some(workspace) = workspace else {
            return;
        };

        self.create_new_thread(&workspace, window, cx);
    }

    fn create_new_thread(
        &mut self,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(multi_workspace) = self.multi_workspace.upgrade() else {
            return;
        };

        // Clear focused_thread immediately so no existing thread stays
        // highlighted while the new blank thread is being shown. Without this,
        // if the target workspace is already active (so ActiveWorkspaceChanged
        // never fires), the previous thread's highlight would linger.
        self.focused_thread = None;

        multi_workspace.update(cx, |multi_workspace, cx| {
            multi_workspace.activate(workspace.clone(), cx);
        });

        workspace.update(cx, |workspace, cx| {
            if let Some(agent_panel) = workspace.panel::<AgentPanel>(cx) {
                agent_panel.update(cx, |panel, cx| {
                    panel.new_thread(&NewThread, window, cx);
                });
            }
            workspace.focus_panel::<AgentPanel>(window, cx);
        });
    }

    fn render_new_thread(
        &self,
        ix: usize,
        _path_list: &PathList,
        workspace: &Entity<Workspace>,
        is_active_draft: bool,
        is_selected: bool,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let is_active = is_active_draft && self.agent_panel_visible && self.active_thread_is_draft;

        let label: SharedString = if is_active {
            self.active_draft_text(cx)
                .unwrap_or_else(|| DEFAULT_THREAD_TITLE.into())
        } else {
            DEFAULT_THREAD_TITLE.into()
        };

        let workspace = workspace.clone();
        let id = SharedString::from(format!("new-thread-btn-{}", ix));

        let thread_item = ThreadItem::new(id, label)
            .icon(IconName::Plus)
            .icon_color(Color::Custom(cx.theme().colors().icon_muted.opacity(0.8)))
            .selected(is_active)
            .focused(is_selected)
            .when(!is_active, |this| {
                this.on_click(cx.listener(move |this, _, window, cx| {
                    this.selection = None;
                    this.create_new_thread(&workspace, window, cx);
                }))
            });

        if is_active {
            div()
                .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
                    cx.stop_propagation();
                })
                .child(thread_item)
                .into_any_element()
        } else {
            thread_item.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 {
            "No threads match your search."
        } else {
            "No threads yet"
        };

        v_flex()
            .id("sidebar-no-results")
            .p_4()
            .size_full()
            .items_center()
            .justify_center()
            .child(
                Label::new(message)
                    .size(LabelSize::Small)
                    .color(Color::Muted),
            )
    }

    fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
        v_flex()
            .id("sidebar-empty-state")
            .p_4()
            .size_full()
            .items_center()
            .justify_center()
            .gap_1()
            .track_focus(&self.focus_handle(cx))
            .child(
                Button::new("open_project", "Open Project")
                    .full_width()
                    .key_binding(KeyBinding::for_action(&workspace::Open::default(), cx))
                    .on_click(|_, window, cx| {
                        window.dispatch_action(
                            Open {
                                create_new_window: false,
                            }
                            .boxed_clone(),
                            cx,
                        );
                    }),
            )
            .child(
                h_flex()
                    .w_1_2()
                    .gap_2()
                    .child(Divider::horizontal())
                    .child(Label::new("or").size(LabelSize::XSmall).color(Color::Muted))
                    .child(Divider::horizontal()),
            )
            .child(
                Button::new("clone_repo", "Clone Repository")
                    .full_width()
                    .on_click(|_, window, cx| {
                        window.dispatch_action(git::Clone.boxed_clone(), cx);
                    }),
            )
    }

    fn render_sidebar_header(
        &self,
        no_open_projects: bool,
        window: &Window,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let has_query = self.has_filter_query(cx);
        let traffic_lights = cfg!(target_os = "macos") && !window.is_fullscreen();
        let header_height = platform_title_bar_height(window);

        h_flex()
            .h(header_height)
            .mt_px()
            .pb_px()
            .when(traffic_lights, |this| {
                this.pl(px(ui::utils::TRAFFIC_LIGHT_PADDING))
            })
            .pr_1p5()
            .gap_1()
            .when(!no_open_projects, |this| {
                this.border_b_1()
                    .border_color(cx.theme().colors().border)
                    .child(Divider::vertical().color(ui::DividerColor::Border))
                    .child(
                        div().ml_1().child(
                            Icon::new(IconName::MagnifyingGlass)
                                .size(IconSize::Small)
                                .color(Color::Muted),
                        ),
                    )
                    .child(self.render_filter_input(cx))
                    .child(
                        h_flex()
                            .gap_1()
                            .when(
                                self.selection.is_some()
                                    && !self.filter_editor.focus_handle(cx).is_focused(window),
                                |this| this.child(KeyBinding::for_action(&FocusSidebarFilter, cx)),
                            )
                            .when(has_query, |this| {
                                this.child(
                                    IconButton::new("clear_filter", IconName::Close)
                                        .icon_size(IconSize::Small)
                                        .tooltip(Tooltip::text("Clear Search"))
                                        .on_click(cx.listener(|this, _, window, cx| {
                                            this.reset_filter_editor_text(window, cx);
                                            this.update_entries(cx);
                                        })),
                                )
                            }),
                    )
            })
    }

    fn render_sidebar_toggle_button(&self, _cx: &mut Context<Self>) -> impl IntoElement {
        IconButton::new("sidebar-close-toggle", IconName::ThreadsSidebarLeftOpen)
            .icon_size(IconSize::Small)
            .tooltip(Tooltip::element(move |_window, cx| {
                v_flex()
                    .gap_1()
                    .child(
                        h_flex()
                            .gap_2()
                            .justify_between()
                            .child(Label::new("Toggle Sidebar"))
                            .child(KeyBinding::for_action(&ToggleWorkspaceSidebar, cx)),
                    )
                    .child(
                        h_flex()
                            .pt_1()
                            .gap_2()
                            .border_t_1()
                            .border_color(cx.theme().colors().border_variant)
                            .justify_between()
                            .child(Label::new("Focus Sidebar"))
                            .child(KeyBinding::for_action(&FocusWorkspaceSidebar, cx)),
                    )
                    .into_any_element()
            }))
            .on_click(|_, window, cx| {
                window.dispatch_action(ToggleWorkspaceSidebar.boxed_clone(), cx);
            })
    }
}

impl Sidebar {
    fn toggle_archive(&mut self, _: &ToggleArchive, window: &mut Window, cx: &mut Context<Self>) {
        match &self.view {
            SidebarView::ThreadList => self.show_archive(window, cx),
            SidebarView::Archive(_) => self.show_thread_list(window, cx),
        }
    }

    fn show_archive(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let Some(active_workspace) = self.multi_workspace.upgrade().and_then(|w| {
            w.read(cx)
                .workspaces()
                .get(w.read(cx).active_workspace_index())
                .cloned()
        }) else {
            return;
        };

        let Some(agent_panel) = active_workspace.read(cx).panel::<AgentPanel>(cx) else {
            return;
        };

        let thread_store = agent_panel.read(cx).thread_store().clone();
        let fs = active_workspace.read(cx).project().read(cx).fs().clone();
        let agent_connection_store = agent_panel.read(cx).connection_store().clone();
        let agent_server_store = active_workspace
            .read(cx)
            .project()
            .read(cx)
            .agent_server_store()
            .clone();

        let archive_view = cx.new(|cx| {
            ThreadsArchiveView::new(
                agent_connection_store,
                agent_server_store,
                thread_store,
                fs,
                window,
                cx,
            )
        });
        let subscription = cx.subscribe_in(
            &archive_view,
            window,
            |this, _, event: &ThreadsArchiveViewEvent, window, cx| match event {
                ThreadsArchiveViewEvent::Close => {
                    this.show_thread_list(window, cx);
                }
                ThreadsArchiveViewEvent::Unarchive {
                    agent,
                    session_info,
                } => {
                    this.show_thread_list(window, cx);
                    this.activate_archived_thread(agent.clone(), session_info.clone(), window, cx);
                }
            },
        );

        self._subscriptions.push(subscription);
        self.view = SidebarView::Archive(archive_view.clone());
        archive_view.update(cx, |view, cx| view.focus_filter_editor(window, cx));
        cx.notify();
    }

    fn show_thread_list(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.view = SidebarView::ThreadList;
        self._subscriptions.clear();
        let handle = self.filter_editor.read(cx).focus_handle(cx);
        handle.focus(window, cx);
        cx.notify();
    }
}

impl WorkspaceSidebar for Sidebar {
    fn width(&self, _cx: &App) -> Pixels {
        self.width
    }

    fn set_width(&mut self, width: Option<Pixels>, cx: &mut Context<Self>) {
        self.width = width.unwrap_or(DEFAULT_WIDTH).clamp(MIN_WIDTH, MAX_WIDTH);
        cx.notify();
    }

    fn has_notifications(&self, _cx: &App) -> bool {
        !self.contents.notified_threads.is_empty()
    }

    fn is_threads_list_view_active(&self) -> bool {
        matches!(self.view, SidebarView::ThreadList)
    }

    fn prepare_for_focus(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
        self.selection = None;
        cx.notify();
    }
}

impl Focusable for Sidebar {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for Sidebar {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let _titlebar_height = ui::utils::platform_title_bar_height(window);
        let ui_font = theme::setup_ui_font(window, cx);
        let sticky_header = self.render_sticky_header(window, cx);

        let color = cx.theme().colors();
        let bg = color
            .title_bar_background
            .blend(color.panel_background.opacity(0.32));

        let no_open_projects = !self.contents.has_open_projects;
        let no_search_results = self.contents.entries.is_empty();

        v_flex()
            .id("workspace-sidebar")
            .key_context("ThreadsSidebar")
            .track_focus(&self.focus_handle)
            .on_action(cx.listener(Self::select_next))
            .on_action(cx.listener(Self::select_previous))
            .on_action(cx.listener(Self::editor_move_down))
            .on_action(cx.listener(Self::editor_move_up))
            .on_action(cx.listener(Self::select_first))
            .on_action(cx.listener(Self::select_last))
            .on_action(cx.listener(Self::confirm))
            .on_action(cx.listener(Self::expand_selected_entry))
            .on_action(cx.listener(Self::collapse_selected_entry))
            .on_action(cx.listener(Self::toggle_selected_fold))
            .on_action(cx.listener(Self::fold_all))
            .on_action(cx.listener(Self::unfold_all))
            .on_action(cx.listener(Self::cancel))
            .on_action(cx.listener(Self::remove_selected_thread))
            .on_action(cx.listener(Self::new_thread_in_group))
            .on_action(cx.listener(Self::toggle_archive))
            .on_action(cx.listener(Self::focus_sidebar_filter))
            .on_action(cx.listener(|this, _: &OpenRecent, window, cx| {
                this.recent_projects_popover_handle.toggle(window, cx);
            }))
            .font(ui_font)
            .h_full()
            .w(self.width)
            .bg(bg)
            .border_r_1()
            .border_color(color.border)
            .map(|this| match &self.view {
                SidebarView::ThreadList => this
                    .child(self.render_sidebar_header(no_open_projects, window, cx))
                    .map(|this| {
                        if no_open_projects {
                            this.child(self.render_empty_state(cx))
                        } else {
                            this.child(
                                v_flex()
                                    .relative()
                                    .flex_1()
                                    .overflow_hidden()
                                    .child(
                                        list(
                                            self.list_state.clone(),
                                            cx.processor(Self::render_list_entry),
                                        )
                                        .flex_1()
                                        .size_full(),
                                    )
                                    .when(no_search_results, |this| {
                                        this.child(self.render_no_results(cx))
                                    })
                                    .when_some(sticky_header, |this, header| this.child(header))
                                    .vertical_scrollbar_for(&self.list_state, window, cx),
                            )
                        }
                    }),
                SidebarView::Archive(archive_view) => this.child(archive_view.clone()),
            })
            .child(
                h_flex()
                    .p_1()
                    .gap_1()
                    .justify_between()
                    .border_t_1()
                    .border_color(cx.theme().colors().border)
                    .child(self.render_sidebar_toggle_button(cx))
                    .child(
                        h_flex()
                            .gap_1()
                            .child(self.render_recent_projects_button(cx))
                            .child(
                                IconButton::new("archive", IconName::Archive)
                                    .icon_size(IconSize::Small)
                                    .toggle_state(matches!(self.view, SidebarView::Archive(..)))
                                    .tooltip(move |_, cx| {
                                        Tooltip::for_action(
                                            "Toggle Archived Threads",
                                            &ToggleArchive,
                                            cx,
                                        )
                                    })
                                    .on_click(cx.listener(|this, _, window, cx| {
                                        this.toggle_archive(&ToggleArchive, window, cx);
                                    })),
                            ),
                    ),
            )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use acp_thread::StubAgentConnection;
    use agent::ThreadStore;
    use agent_ui::test_support::{active_session_id, open_thread_with_connection, send_message};
    use assistant_text_thread::TextThreadStore;
    use chrono::DateTime;
    use feature_flags::FeatureFlagAppExt as _;
    use fs::FakeFs;
    use gpui::TestAppContext;
    use pretty_assertions::assert_eq;
    use settings::SettingsStore;
    use std::{path::PathBuf, sync::Arc};
    use util::path_list::PathList;

    fn init_test(cx: &mut TestAppContext) {
        cx.update(|cx| {
            let settings_store = SettingsStore::test(cx);
            cx.set_global(settings_store);
            theme::init(theme::LoadThemes::JustBase, cx);
            editor::init(cx);
            cx.update_flags(false, vec!["agent-v2".into()]);
            ThreadStore::init_global(cx);
            SidebarThreadMetadataStore::init_global(cx);
            language_model::LanguageModelRegistry::test(cx);
            prompt_store::init(cx);
        });
    }

    fn has_thread_entry(sidebar: &Sidebar, session_id: &acp::SessionId) -> bool {
        sidebar.contents.entries.iter().any(|entry| {
            matches!(entry, ListEntry::Thread(t) if &t.session_info.session_id == session_id)
        })
    }

    async fn init_test_project(
        worktree_path: &str,
        cx: &mut TestAppContext,
    ) -> Entity<project::Project> {
        init_test(cx);
        let fs = FakeFs::new(cx.executor());
        fs.insert_tree(worktree_path, serde_json::json!({ "src": {} }))
            .await;
        cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
        project::Project::test(fs, [worktree_path.as_ref()], cx).await
    }

    fn setup_sidebar(
        multi_workspace: &Entity<MultiWorkspace>,
        cx: &mut gpui::VisualTestContext,
    ) -> Entity<Sidebar> {
        let multi_workspace = multi_workspace.clone();
        let sidebar =
            cx.update(|window, cx| cx.new(|cx| Sidebar::new(multi_workspace.clone(), window, cx)));
        multi_workspace.update(cx, |mw, cx| {
            mw.register_sidebar(sidebar.clone(), cx);
        });
        cx.run_until_parked();
        sidebar
    }

    async fn save_n_test_threads(
        count: u32,
        path_list: &PathList,
        cx: &mut gpui::VisualTestContext,
    ) {
        for i in 0..count {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(format!("thread-{}", i))),
                format!("Thread {}", i + 1).into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, i).unwrap(),
                path_list.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();
    }

    async fn save_test_thread_metadata(
        session_id: &acp::SessionId,
        path_list: PathList,
        cx: &mut TestAppContext,
    ) {
        save_thread_metadata(
            session_id.clone(),
            "Test".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list,
            cx,
        )
        .await;
    }

    async fn save_named_thread_metadata(
        session_id: &str,
        title: &str,
        path_list: &PathList,
        cx: &mut gpui::VisualTestContext,
    ) {
        save_thread_metadata(
            acp::SessionId::new(Arc::from(session_id)),
            SharedString::from(title.to_string()),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();
    }

    async fn save_thread_metadata(
        session_id: acp::SessionId,
        title: SharedString,
        updated_at: DateTime<Utc>,
        path_list: PathList,
        cx: &mut TestAppContext,
    ) {
        let metadata = ThreadMetadata {
            session_id,
            agent_id: None,
            title,
            updated_at,
            created_at: None,
            folder_paths: path_list,
        };
        let task = cx.update(|cx| {
            SidebarThreadMetadataStore::global(cx).update(cx, |store, cx| store.save(metadata, cx))
        });
        task.await.unwrap();
    }

    fn open_and_focus_sidebar(sidebar: &Entity<Sidebar>, cx: &mut gpui::VisualTestContext) {
        let multi_workspace = sidebar.read_with(cx, |s, _| s.multi_workspace.upgrade());
        if let Some(multi_workspace) = multi_workspace {
            multi_workspace.update_in(cx, |mw, window, cx| {
                if !mw.sidebar_open() {
                    mw.toggle_sidebar(window, cx);
                }
            });
        }
        cx.run_until_parked();
        sidebar.update_in(cx, |_, window, cx| {
            cx.focus_self(window);
        });
        cx.run_until_parked();
    }

    fn visible_entries_as_strings(
        sidebar: &Entity<Sidebar>,
        cx: &mut gpui::VisualTestContext,
    ) -> Vec<String> {
        sidebar.read_with(cx, |sidebar, _cx| {
            sidebar
                .contents
                .entries
                .iter()
                .enumerate()
                .map(|(ix, entry)| {
                    let selected = if sidebar.selection == Some(ix) {
                        "  <== selected"
                    } else {
                        ""
                    };
                    match entry {
                        ListEntry::ProjectHeader {
                            label,
                            path_list,
                            highlight_positions: _,
                            ..
                        } => {
                            let icon = if sidebar.collapsed_groups.contains(path_list) {
                                ">"
                            } else {
                                "v"
                            };
                            format!("{} [{}]{}", icon, label, selected)
                        }
                        ListEntry::Thread(thread) => {
                            let title = thread
                                .session_info
                                .title
                                .as_ref()
                                .map(|s| s.as_ref())
                                .unwrap_or("Untitled");
                            let active = 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.session_info.session_id)
                            {
                                " (!)"
                            } else {
                                ""
                            };
                            let worktree = thread
                                .worktree_name
                                .as_ref()
                                .map(|name| format!(" {{{}}}", name))
                                .unwrap_or_default();
                            format!(
                                "  {}{}{}{}{}{}",
                                title, worktree, active, status_str, notified, selected
                            )
                        }
                        ListEntry::ViewMore {
                            is_fully_expanded, ..
                        } => {
                            if *is_fully_expanded {
                                format!("  - Collapse{}", selected)
                            } else {
                                format!("  + View More{}", selected)
                            }
                        }
                        ListEntry::NewThread { .. } => {
                            format!("  [+ New Thread]{}", selected)
                        }
                    }
                })
                .collect()
        })
    }

    #[test]
    fn test_clean_mention_links() {
        // Simple mention link
        assert_eq!(
            Sidebar::clean_mention_links("check [@Button.tsx](file:///path/to/Button.tsx)"),
            "check @Button.tsx"
        );

        // Multiple mention links
        assert_eq!(
            Sidebar::clean_mention_links(
                "look at [@foo.rs](file:///foo.rs) and [@bar.rs](file:///bar.rs)"
            ),
            "look at @foo.rs and @bar.rs"
        );

        // No mention links — passthrough
        assert_eq!(
            Sidebar::clean_mention_links("plain text with no mentions"),
            "plain text with no mentions"
        );

        // Incomplete link syntax — preserved as-is
        assert_eq!(
            Sidebar::clean_mention_links("broken [@mention without closing"),
            "broken [@mention without closing"
        );

        // Regular markdown link (no @) — not touched
        assert_eq!(
            Sidebar::clean_mention_links("see [docs](https://example.com)"),
            "see [docs](https://example.com)"
        );

        // Empty input
        assert_eq!(Sidebar::clean_mention_links(""), "");
    }

    #[gpui::test]
    async fn test_entities_released_on_window_close(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let weak_workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().downgrade());
        let weak_sidebar = sidebar.downgrade();
        let weak_multi_workspace = multi_workspace.downgrade();

        drop(sidebar);
        drop(multi_workspace);
        cx.update(|window, _cx| window.remove_window());
        cx.run_until_parked();

        weak_multi_workspace.assert_released();
        weak_sidebar.assert_released();
        weak_workspace.assert_released();
    }

    #[gpui::test]
    async fn test_single_workspace_no_threads(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  [+ New Thread]"]
        );
    }

    #[gpui::test]
    async fn test_single_workspace_with_saved_threads(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("thread-1")),
            "Fix crash in project panel".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 3, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;

        save_thread_metadata(
            acp::SessionId::new(Arc::from("thread-2")),
            "Add inline diff view".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix crash in project panel",
                "  Add inline diff view",
            ]
        );
    }

    #[gpui::test]
    async fn test_workspace_lifecycle(cx: &mut TestAppContext) {
        let project = init_test_project("/project-a", 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);

        // Single workspace with a thread
        let path_list = PathList::new(&[std::path::PathBuf::from("/project-a")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("thread-a1")),
            "Thread A1".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Thread A1"]
        );

        // Add a second workspace
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.create_test_workspace(window, cx).detach();
        });
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Thread A1",]
        );

        // Remove the second workspace
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.remove_workspace(1, window, cx);
        });
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Thread A1"]
        );
    }

    #[gpui::test]
    async fn test_view_more_pagination(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(12, &path_list, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Thread 12",
                "  Thread 11",
                "  Thread 10",
                "  Thread 9",
                "  Thread 8",
                "  + View More",
            ]
        );
    }

    #[gpui::test]
    async fn test_view_more_batched_expansion(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        // Create 17 threads: initially shows 5, then 10, then 15, then all 17 with Collapse
        save_n_test_threads(17, &path_list, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Initially shows 5 threads + View More
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 7); // header + 5 threads + View More
        assert!(entries.iter().any(|e| e.contains("View More")));

        // Focus and navigate to View More, then confirm to expand by one batch
        open_and_focus_sidebar(&sidebar, cx);
        for _ in 0..7 {
            cx.dispatch_action(SelectNext);
        }
        cx.dispatch_action(Confirm);
        cx.run_until_parked();

        // Now shows 10 threads + View More
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 12); // header + 10 threads + View More
        assert!(entries.iter().any(|e| e.contains("View More")));

        // Expand again by one batch
        sidebar.update_in(cx, |s, _window, cx| {
            let current = s.expanded_groups.get(&path_list).copied().unwrap_or(0);
            s.expanded_groups.insert(path_list.clone(), current + 1);
            s.update_entries(cx);
        });
        cx.run_until_parked();

        // Now shows 15 threads + View More
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 17); // header + 15 threads + View More
        assert!(entries.iter().any(|e| e.contains("View More")));

        // 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(&path_list).copied().unwrap_or(0);
            s.expanded_groups.insert(path_list.clone(), current + 1);
            s.update_entries(cx);
        });
        cx.run_until_parked();

        // All 17 threads shown with Collapse button
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 19); // header + 17 threads + Collapse
        assert!(!entries.iter().any(|e| e.contains("View More")));
        assert!(entries.iter().any(|e| e.contains("Collapse")));

        // Click collapse - should go back to showing 5 threads
        sidebar.update_in(cx, |s, _window, cx| {
            s.expanded_groups.remove(&path_list);
            s.update_entries(cx);
        });
        cx.run_until_parked();

        // Back to initial state: 5 threads + View More
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 7); // header + 5 threads + View More
        assert!(entries.iter().any(|e| e.contains("View More")));
    }

    #[gpui::test]
    async fn test_collapse_and_expand_group(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(1, &path_list, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread 1"]
        );

        // Collapse
        sidebar.update_in(cx, |s, window, cx| {
            s.toggle_collapse(&path_list, window, cx);
        });
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]"]
        );

        // Expand
        sidebar.update_in(cx, |s, window, cx| {
            s.toggle_collapse(&path_list, window, cx);
        });
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread 1"]
        );
    }

    #[gpui::test]
    async fn test_visible_entries_as_strings(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
        let expanded_path = PathList::new(&[std::path::PathBuf::from("/expanded")]);
        let collapsed_path = PathList::new(&[std::path::PathBuf::from("/collapsed")]);

        sidebar.update_in(cx, |s, _window, _cx| {
            s.collapsed_groups.insert(collapsed_path.clone());
            s.contents
                .notified_threads
                .insert(acp::SessionId::new(Arc::from("t-5")));
            s.contents.entries = vec![
                // Expanded project header
                ListEntry::ProjectHeader {
                    path_list: expanded_path.clone(),
                    label: "expanded-project".into(),
                    workspace: Some(workspace.clone()),
                    highlight_positions: Vec::new(),
                    has_running_threads: false,
                    waiting_thread_count: 0,
                },
                ListEntry::Thread(ThreadEntry {
                    agent: Agent::NativeAgent,
                    session_info: acp_thread::AgentSessionInfo {
                        session_id: acp::SessionId::new(Arc::from("t-1")),
                        work_dirs: None,
                        title: Some("Completed thread".into()),
                        updated_at: Some(Utc::now()),
                        created_at: Some(Utc::now()),
                        meta: None,
                    },
                    icon: IconName::ZedAgent,
                    icon_from_external_svg: None,
                    status: AgentThreadStatus::Completed,
                    workspace: ThreadEntryWorkspace::Open(workspace.clone()),
                    is_live: false,
                    is_background: false,
                    is_title_generating: false,
                    highlight_positions: Vec::new(),
                    worktree_name: None,
                    worktree_full_path: None,
                    worktree_highlight_positions: Vec::new(),
                    diff_stats: DiffStats::default(),
                }),
                // Active thread with Running status
                ListEntry::Thread(ThreadEntry {
                    agent: Agent::NativeAgent,
                    session_info: acp_thread::AgentSessionInfo {
                        session_id: acp::SessionId::new(Arc::from("t-2")),
                        work_dirs: None,
                        title: Some("Running thread".into()),
                        updated_at: Some(Utc::now()),
                        created_at: Some(Utc::now()),
                        meta: None,
                    },
                    icon: IconName::ZedAgent,
                    icon_from_external_svg: None,
                    status: AgentThreadStatus::Running,
                    workspace: ThreadEntryWorkspace::Open(workspace.clone()),
                    is_live: true,
                    is_background: false,
                    is_title_generating: false,
                    highlight_positions: Vec::new(),
                    worktree_name: None,
                    worktree_full_path: None,
                    worktree_highlight_positions: Vec::new(),
                    diff_stats: DiffStats::default(),
                }),
                // Active thread with Error status
                ListEntry::Thread(ThreadEntry {
                    agent: Agent::NativeAgent,
                    session_info: acp_thread::AgentSessionInfo {
                        session_id: acp::SessionId::new(Arc::from("t-3")),
                        work_dirs: None,
                        title: Some("Error thread".into()),
                        updated_at: Some(Utc::now()),
                        created_at: Some(Utc::now()),
                        meta: None,
                    },
                    icon: IconName::ZedAgent,
                    icon_from_external_svg: None,
                    status: AgentThreadStatus::Error,
                    workspace: ThreadEntryWorkspace::Open(workspace.clone()),
                    is_live: true,
                    is_background: false,
                    is_title_generating: false,
                    highlight_positions: Vec::new(),
                    worktree_name: None,
                    worktree_full_path: None,
                    worktree_highlight_positions: Vec::new(),
                    diff_stats: DiffStats::default(),
                }),
                // Thread with WaitingForConfirmation status, not active
                ListEntry::Thread(ThreadEntry {
                    agent: Agent::NativeAgent,
                    session_info: acp_thread::AgentSessionInfo {
                        session_id: acp::SessionId::new(Arc::from("t-4")),
                        work_dirs: None,
                        title: Some("Waiting thread".into()),
                        updated_at: Some(Utc::now()),
                        created_at: Some(Utc::now()),
                        meta: None,
                    },
                    icon: IconName::ZedAgent,
                    icon_from_external_svg: None,
                    status: AgentThreadStatus::WaitingForConfirmation,
                    workspace: ThreadEntryWorkspace::Open(workspace.clone()),
                    is_live: false,
                    is_background: false,
                    is_title_generating: false,
                    highlight_positions: Vec::new(),
                    worktree_name: None,
                    worktree_full_path: None,
                    worktree_highlight_positions: Vec::new(),
                    diff_stats: DiffStats::default(),
                }),
                // Background thread that completed (should show notification)
                ListEntry::Thread(ThreadEntry {
                    agent: Agent::NativeAgent,
                    session_info: acp_thread::AgentSessionInfo {
                        session_id: acp::SessionId::new(Arc::from("t-5")),
                        work_dirs: None,
                        title: Some("Notified thread".into()),
                        updated_at: Some(Utc::now()),
                        created_at: Some(Utc::now()),
                        meta: None,
                    },
                    icon: IconName::ZedAgent,
                    icon_from_external_svg: None,
                    status: AgentThreadStatus::Completed,
                    workspace: ThreadEntryWorkspace::Open(workspace.clone()),
                    is_live: true,
                    is_background: true,
                    is_title_generating: false,
                    highlight_positions: Vec::new(),
                    worktree_name: None,
                    worktree_full_path: None,
                    worktree_highlight_positions: Vec::new(),
                    diff_stats: DiffStats::default(),
                }),
                // View More entry
                ListEntry::ViewMore {
                    path_list: expanded_path.clone(),
                    is_fully_expanded: false,
                },
                // Collapsed project header
                ListEntry::ProjectHeader {
                    path_list: collapsed_path.clone(),
                    label: "collapsed-project".into(),
                    workspace: Some(workspace.clone()),
                    highlight_positions: Vec::new(),
                    has_running_threads: false,
                    waiting_thread_count: 0,
                },
            ];

            // Select the Running thread (index 2)
            s.selection = Some(2);
        });

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [expanded-project]",
                "  Completed thread",
                "  Running thread * (running)  <== selected",
                "  Error thread * (error)",
                "  Waiting thread (waiting)",
                "  Notified thread * (!)",
                "  + View More",
                "> [collapsed-project]",
            ]
        );

        // Move selection to the collapsed header
        sidebar.update_in(cx, |s, _window, _cx| {
            s.selection = Some(7);
        });

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx).last().cloned(),
            Some("> [collapsed-project]  <== selected".to_string()),
        );

        // Clear selection
        sidebar.update_in(cx, |s, _window, _cx| {
            s.selection = None;
        });

        // No entry should have the selected marker
        let entries = visible_entries_as_strings(&sidebar, cx);
        for entry in &entries {
            assert!(
                !entry.contains("<== selected"),
                "unexpected selection marker in: {}",
                entry
            );
        }
    }

    #[gpui::test]
    async fn test_keyboard_select_next_and_previous(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(3, &path_list, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Entries: [header, thread3, thread2, thread1]
        // Focusing the sidebar does not set a selection; select_next/select_previous
        // handle None gracefully by starting from the first or last entry.
        open_and_focus_sidebar(&sidebar, cx);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);

        // First SelectNext from None starts at index 0
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));

        // Move down through remaining entries
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));

        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(2));

        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(3));

        // At the end, wraps back to first entry
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));

        // Navigate back to the end
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(2));
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(3));

        // Move back up
        cx.dispatch_action(SelectPrevious);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(2));

        cx.dispatch_action(SelectPrevious);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));

        cx.dispatch_action(SelectPrevious);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));

        // At the top, selection clears (focus returns to editor)
        cx.dispatch_action(SelectPrevious);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);
    }

    #[gpui::test]
    async fn test_keyboard_select_first_and_last(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(3, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        open_and_focus_sidebar(&sidebar, cx);

        // SelectLast jumps to the end
        cx.dispatch_action(SelectLast);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(3));

        // SelectFirst jumps to the beginning
        cx.dispatch_action(SelectFirst);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));
    }

    #[gpui::test]
    async fn test_keyboard_focus_in_does_not_set_selection(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        // Initially no selection
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);

        // Open the sidebar so it's rendered, then focus it to trigger focus_in.
        // focus_in no longer sets a default selection.
        open_and_focus_sidebar(&sidebar, cx);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);

        // Manually set a selection, blur, then refocus — selection should be preserved
        sidebar.update_in(cx, |sidebar, _window, _cx| {
            sidebar.selection = Some(0);
        });

        cx.update(|window, _cx| {
            window.blur();
        });
        cx.run_until_parked();

        sidebar.update_in(cx, |_, window, cx| {
            cx.focus_self(window);
        });
        cx.run_until_parked();
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));
    }

    #[gpui::test]
    async fn test_keyboard_confirm_on_project_header_toggles_collapse(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(1, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread 1"]
        );

        // Focus the sidebar and select the header (index 0)
        open_and_focus_sidebar(&sidebar, cx);
        sidebar.update_in(cx, |sidebar, _window, _cx| {
            sidebar.selection = Some(0);
        });

        // Confirm on project header collapses the group
        cx.dispatch_action(Confirm);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]  <== selected"]
        );

        // Confirm again expands the group
        cx.dispatch_action(Confirm);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]  <== selected", "  Thread 1",]
        );
    }

    #[gpui::test]
    async fn test_keyboard_confirm_on_view_more_expands(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(8, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Should show header + 5 threads + "View More"
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 7);
        assert!(entries.iter().any(|e| e.contains("View More")));

        // Focus sidebar (selection starts at None), then navigate down to the "View More" entry (index 6)
        open_and_focus_sidebar(&sidebar, cx);
        for _ in 0..7 {
            cx.dispatch_action(SelectNext);
        }
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(6));

        // Confirm on "View More" to expand
        cx.dispatch_action(Confirm);
        cx.run_until_parked();

        // All 8 threads should now be visible with a "Collapse" button
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(entries.len(), 10); // header + 8 threads + Collapse button
        assert!(!entries.iter().any(|e| e.contains("View More")));
        assert!(entries.iter().any(|e| e.contains("Collapse")));
    }

    #[gpui::test]
    async fn test_keyboard_expand_and_collapse_selected_entry(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(1, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread 1"]
        );

        // Focus sidebar and manually select the header (index 0). Press left to collapse.
        open_and_focus_sidebar(&sidebar, cx);
        sidebar.update_in(cx, |sidebar, _window, _cx| {
            sidebar.selection = Some(0);
        });

        cx.dispatch_action(SelectParent);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]  <== selected"]
        );

        // Press right to expand
        cx.dispatch_action(SelectChild);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]  <== selected", "  Thread 1",]
        );

        // Press right again on already-expanded header moves selection down
        cx.dispatch_action(SelectChild);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));
    }

    #[gpui::test]
    async fn test_keyboard_collapse_from_child_selects_parent(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(1, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Focus sidebar (selection starts at None), then navigate down to the thread (child)
        open_and_focus_sidebar(&sidebar, cx);
        cx.dispatch_action(SelectNext);
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread 1  <== selected",]
        );

        // Pressing left on a child collapses the parent group and selects it
        cx.dispatch_action(SelectParent);
        cx.run_until_parked();

        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]  <== selected"]
        );
    }

    #[gpui::test]
    async fn test_keyboard_navigation_on_empty_list(cx: &mut TestAppContext) {
        let project = init_test_project("/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);

        // An empty project has the header and a new thread button.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [empty-project]", "  [+ New Thread]"]
        );

        // Focus sidebar — focus_in does not set a selection
        open_and_focus_sidebar(&sidebar, cx);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);

        // First SelectNext from None starts at index 0 (header)
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));

        // SelectNext moves to the new thread button
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));

        // At the end, wraps back to first entry
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(0));

        // SelectPrevious from first entry clears selection (returns to editor)
        cx.dispatch_action(SelectPrevious);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), None);
    }

    #[gpui::test]
    async fn test_selection_clamps_after_entry_removal(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
        save_n_test_threads(1, &path_list, cx).await;
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Focus sidebar (selection starts at None), navigate down to the thread (index 1)
        open_and_focus_sidebar(&sidebar, cx);
        cx.dispatch_action(SelectNext);
        cx.dispatch_action(SelectNext);
        assert_eq!(sidebar.read_with(cx, |s, _| s.selection), Some(1));

        // Collapse the group, which removes the thread from the list
        cx.dispatch_action(SelectParent);
        cx.run_until_parked();

        // Selection should be clamped to the last valid index (0 = header)
        let selection = sidebar.read_with(cx, |s, _| s.selection);
        let entry_count = sidebar.read_with(cx, |s, _| s.contents.entries.len());
        assert!(
            selection.unwrap_or(0) < entry_count,
            "selection {} should be within bounds (entries: {})",
            selection.unwrap_or(0),
            entry_count,
        );
    }

    async fn init_test_project_with_agent_panel(
        worktree_path: &str,
        cx: &mut TestAppContext,
    ) -> Entity<project::Project> {
        agent_ui::test_support::init_test(cx);
        cx.update(|cx| {
            cx.update_flags(false, vec!["agent-v2".into()]);
            ThreadStore::init_global(cx);
            SidebarThreadMetadataStore::init_global(cx);
            language_model::LanguageModelRegistry::test(cx);
            prompt_store::init(cx);
        });

        let fs = FakeFs::new(cx.executor());
        fs.insert_tree(worktree_path, serde_json::json!({ "src": {} }))
            .await;
        cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));
        project::Project::test(fs, [worktree_path.as_ref()], cx).await
    }

    fn add_agent_panel(
        workspace: &Entity<Workspace>,
        project: &Entity<project::Project>,
        cx: &mut gpui::VisualTestContext,
    ) -> Entity<AgentPanel> {
        workspace.update_in(cx, |workspace, window, cx| {
            let text_thread_store = cx.new(|cx| TextThreadStore::fake(project.clone(), cx));
            let panel = cx.new(|cx| AgentPanel::test_new(workspace, text_thread_store, window, cx));
            workspace.add_panel(panel.clone(), window, cx);
            panel
        })
    }

    fn setup_sidebar_with_agent_panel(
        multi_workspace: &Entity<MultiWorkspace>,
        project: &Entity<project::Project>,
        cx: &mut gpui::VisualTestContext,
    ) -> (Entity<Sidebar>, Entity<AgentPanel>) {
        let sidebar = setup_sidebar(multi_workspace, cx);
        let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
        let panel = add_agent_panel(&workspace, project, cx);
        (sidebar, panel)
    }

    #[gpui::test]
    async fn test_parallel_threads_shown_with_live_status(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, &project, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        // Open thread A and keep it generating.
        let connection = StubAgentConnection::new();
        open_thread_with_connection(&panel, connection.clone(), cx);
        send_message(&panel, cx);

        let session_id_a = active_session_id(&panel, cx);
        save_test_thread_metadata(&session_id_a, path_list.clone(), cx).await;

        cx.update(|_, cx| {
            connection.send_update(
                session_id_a.clone(),
                acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("working...".into())),
                cx,
            );
        });
        cx.run_until_parked();

        // Open thread B (idle, default response) — thread A goes to background.
        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 session_id_b = active_session_id(&panel, cx);
        save_test_thread_metadata(&session_id_b, path_list.clone(), cx).await;

        cx.run_until_parked();

        let mut entries = visible_entries_as_strings(&sidebar, cx);
        entries[1..].sort();
        assert_eq!(
            entries,
            vec!["v [my-project]", "  Hello *", "  Hello * (running)",]
        );
    }

    #[gpui::test]
    async fn test_background_thread_completion_triggers_notification(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, &project_a, cx);

        let path_list_a = PathList::new(&[std::path::PathBuf::from("/project-a")]);

        // Open thread on workspace A and keep it generating.
        let connection_a = StubAgentConnection::new();
        open_thread_with_connection(&panel_a, connection_a.clone(), cx);
        send_message(&panel_a, cx);

        let session_id_a = active_session_id(&panel_a, cx);
        save_test_thread_metadata(&session_id_a, path_list_a.clone(), cx).await;

        cx.update(|_, cx| {
            connection_a.send_update(
                session_id_a.clone(),
                acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new("chunk".into())),
                cx,
            );
        });
        cx.run_until_parked();

        // Add a second workspace and activate it (making workspace A the background).
        let fs = cx.update(|_, cx| <dyn fs::Fs>::global(cx));
        let project_b = project::Project::test(fs, [], cx).await;
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(project_b, window, cx);
        });
        cx.run_until_parked();

        // Thread A is still running; no notification yet.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Hello * (running)",]
        );

        // Complete thread A's turn (transition Running → Completed).
        connection_a.end_turn(session_id_a.clone(), acp::StopReason::EndTurn);
        cx.run_until_parked();

        // The completed background thread shows a notification indicator.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Hello * (!)",]
        );
    }

    fn type_in_search(sidebar: &Entity<Sidebar>, query: &str, cx: &mut gpui::VisualTestContext) {
        sidebar.update_in(cx, |sidebar, window, cx| {
            window.focus(&sidebar.filter_editor.focus_handle(cx), cx);
            sidebar.filter_editor.update(cx, |editor, cx| {
                editor.set_text(query, window, cx);
            });
        });
        cx.run_until_parked();
    }

    #[gpui::test]
    async fn test_search_narrows_visible_threads_to_matches(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        for (id, title, hour) in [
            ("t-1", "Fix crash in project panel", 3),
            ("t-2", "Add inline diff view", 2),
            ("t-3", "Refactor settings module", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix crash in project panel",
                "  Add inline diff view",
                "  Refactor settings module",
            ]
        );

        // User types "diff" in the search box — only the matching thread remains,
        // with its workspace header preserved for context.
        type_in_search(&sidebar, "diff", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Add inline diff view  <== selected",]
        );

        // User changes query to something with no matches — list is empty.
        type_in_search(&sidebar, "nonexistent", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            Vec::<String>::new()
        );
    }

    #[gpui::test]
    async fn test_search_matches_regardless_of_case(cx: &mut TestAppContext) {
        // Scenario: A user remembers a thread title but not the exact casing.
        // Search should match case-insensitively so they can still find it.
        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));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("thread-1")),
            "Fix Crash In Project Panel".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();

        // Lowercase query matches mixed-case title.
        type_in_search(&sidebar, "fix crash", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix Crash In Project Panel  <== selected",
            ]
        );

        // Uppercase query also matches the same title.
        type_in_search(&sidebar, "FIX CRASH", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix Crash In Project Panel  <== selected",
            ]
        );
    }

    #[gpui::test]
    async fn test_escape_clears_search_and_restores_full_list(cx: &mut TestAppContext) {
        // Scenario: A user searches, finds what they need, then presses Escape
        // to dismiss the filter and see the full list again.
        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));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        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(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        // Confirm the full list is showing.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Alpha thread", "  Beta thread",]
        );

        // User types a search query to filter down.
        open_and_focus_sidebar(&sidebar, cx);
        type_in_search(&sidebar, "alpha", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Alpha thread  <== selected",]
        );

        // User presses Escape — filter clears, full list is restored.
        // The selection index (1) now points at the first thread entry.
        cx.dispatch_action(Cancel);
        cx.run_until_parked();
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Alpha thread  <== selected",
                "  Beta thread",
            ]
        );
    }

    #[gpui::test]
    async fn test_search_only_shows_workspace_headers_with_matches(cx: &mut TestAppContext) {
        let project_a = init_test_project("/project-a", 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 path_list_a = PathList::new(&[std::path::PathBuf::from("/project-a")]);

        for (id, title, hour) in [
            ("a1", "Fix bug in sidebar", 2),
            ("a2", "Add tests for editor", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list_a.clone(),
                cx,
            )
            .await;
        }

        // Add a second workspace.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.create_test_workspace(window, cx).detach();
        });
        cx.run_until_parked();

        let path_list_b = PathList::new::<std::path::PathBuf>(&[]);

        for (id, title, hour) in [
            ("b1", "Refactor sidebar layout", 3),
            ("b2", "Fix typo in README", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list_b.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project-a]",
                "  Fix bug in sidebar",
                "  Add tests for editor",
            ]
        );

        // "sidebar" matches a thread in each workspace — both headers stay visible.
        type_in_search(&sidebar, "sidebar", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Fix bug in sidebar  <== selected",]
        );

        // "typo" only matches in the second workspace — the first header disappears.
        type_in_search(&sidebar, "typo", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            Vec::<String>::new()
        );

        // "project-a" matches the first workspace name — the header appears
        // with all child threads included.
        type_in_search(&sidebar, "project-a", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project-a]",
                "  Fix bug in sidebar  <== selected",
                "  Add tests for editor",
            ]
        );
    }

    #[gpui::test]
    async fn test_search_matches_workspace_name(cx: &mut TestAppContext) {
        let project_a = init_test_project("/alpha-project", 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 path_list_a = PathList::new(&[std::path::PathBuf::from("/alpha-project")]);

        for (id, title, hour) in [
            ("a1", "Fix bug in sidebar", 2),
            ("a2", "Add tests for editor", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list_a.clone(),
                cx,
            )
            .await;
        }

        // Add a second workspace.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.create_test_workspace(window, cx).detach();
        });
        cx.run_until_parked();

        let path_list_b = PathList::new::<std::path::PathBuf>(&[]);

        for (id, title, hour) in [
            ("b1", "Refactor sidebar layout", 3),
            ("b2", "Fix typo in README", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list_b.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        // "alpha" matches the workspace name "alpha-project" but no thread titles.
        // The workspace header should appear with all child threads included.
        type_in_search(&sidebar, "alpha", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [alpha-project]",
                "  Fix bug in sidebar  <== selected",
                "  Add tests for editor",
            ]
        );

        // "sidebar" matches thread titles in both workspaces but not workspace names.
        // Both headers appear with their matching threads.
        type_in_search(&sidebar, "sidebar", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [alpha-project]", "  Fix bug in sidebar  <== selected",]
        );

        // "alpha sidebar" matches the workspace name "alpha-project" (fuzzy: a-l-p-h-a-s-i-d-e-b-a-r
        // doesn't match) — but does not match either workspace name or any thread.
        // Actually let's test something simpler: a query that matches both a workspace
        // name AND some threads in that workspace. Matching threads should still appear.
        type_in_search(&sidebar, "fix", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [alpha-project]", "  Fix bug in sidebar  <== selected",]
        );

        // A query that matches a workspace name AND a thread in that same workspace.
        // Both the header (highlighted) and all child threads should appear.
        type_in_search(&sidebar, "alpha", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [alpha-project]",
                "  Fix bug in sidebar  <== selected",
                "  Add tests for editor",
            ]
        );

        // Now search for something that matches only a workspace name when there
        // are also threads with matching titles — the non-matching workspace's
        // threads should still appear if their titles match.
        type_in_search(&sidebar, "alp", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [alpha-project]",
                "  Fix bug in sidebar  <== selected",
                "  Add tests for editor",
            ]
        );
    }

    #[gpui::test]
    async fn test_search_finds_threads_hidden_behind_view_more(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        // Create 8 threads. The oldest one has a unique name and will be
        // behind View More (only 5 shown by default).
        for i in 0..8u32 {
            let title = if i == 0 {
                "Hidden gem thread".to_string()
            } else {
                format!("Thread {}", i + 1)
            };
            save_thread_metadata(
                acp::SessionId::new(Arc::from(format!("thread-{}", i))),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, i).unwrap(),
                path_list.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        // Confirm the thread is not visible and View More is shown.
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert!(
            entries.iter().any(|e| e.contains("View More")),
            "should have View More button"
        );
        assert!(
            !entries.iter().any(|e| e.contains("Hidden gem")),
            "Hidden gem should be behind View More"
        );

        // User searches for the hidden thread — it appears, and View More is gone.
        type_in_search(&sidebar, "hidden gem", cx);
        let filtered = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(
            filtered,
            vec!["v [my-project]", "  Hidden gem thread  <== selected",]
        );
        assert!(
            !filtered.iter().any(|e| e.contains("View More")),
            "View More should not appear when filtering"
        );
    }

    #[gpui::test]
    async fn test_search_finds_threads_inside_collapsed_groups(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("thread-1")),
            "Important thread".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();

        // User focuses the sidebar and collapses the group using keyboard:
        // manually select the header, then press SelectParent to collapse.
        open_and_focus_sidebar(&sidebar, cx);
        sidebar.update_in(cx, |sidebar, _window, _cx| {
            sidebar.selection = Some(0);
        });
        cx.dispatch_action(SelectParent);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]  <== selected"]
        );

        // User types a search — the thread appears even though its group is collapsed.
        type_in_search(&sidebar, "important", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["> [my-project]", "  Important thread  <== selected",]
        );
    }

    #[gpui::test]
    async fn test_search_then_keyboard_navigate_and_confirm(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        for (id, title, hour) in [
            ("t-1", "Fix crash in panel", 3),
            ("t-2", "Fix lint warnings", 2),
            ("t-3", "Add new feature", 1),
        ] {
            save_thread_metadata(
                acp::SessionId::new(Arc::from(id)),
                title.into(),
                chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, hour, 0, 0).unwrap(),
                path_list.clone(),
                cx,
            )
            .await;
        }
        cx.run_until_parked();

        open_and_focus_sidebar(&sidebar, cx);

        // User types "fix" — two threads match.
        type_in_search(&sidebar, "fix", cx);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix crash in panel  <== selected",
                "  Fix lint warnings",
            ]
        );

        // Selection starts on the first matching thread. User presses
        // SelectNext to move to the second match.
        cx.dispatch_action(SelectNext);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix crash in panel",
                "  Fix lint warnings  <== selected",
            ]
        );

        // User can also jump back with SelectPrevious.
        cx.dispatch_action(SelectPrevious);
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [my-project]",
                "  Fix crash in panel  <== selected",
                "  Fix lint warnings",
            ]
        );
    }

    #[gpui::test]
    async fn test_confirm_on_historical_thread_activates_workspace(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, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.create_test_workspace(window, cx).detach();
        });
        cx.run_until_parked();

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("hist-1")),
            "Historical Thread".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 6, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;
        cx.run_until_parked();
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Historical Thread",]
        );

        // Switch to workspace 1 so we can verify the confirm switches back.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(1, window, cx);
        });
        cx.run_until_parked();
        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            1
        );

        // Confirm on the historical (non-live) thread at index 1.
        // Before a previous fix, the workspace field was Option<usize> and
        // historical threads had None, so activate_thread early-returned
        // without switching the workspace.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.selection = Some(1);
            sidebar.confirm(&Confirm, window, cx);
        });
        cx.run_until_parked();

        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            0
        );
    }

    #[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;
        let (multi_workspace, cx) =
            cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
        let sidebar = setup_sidebar(&multi_workspace, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        save_thread_metadata(
            acp::SessionId::new(Arc::from("t-1")),
            "Thread A".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 2, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;

        save_thread_metadata(
            acp::SessionId::new(Arc::from("t-2")),
            "Thread B".into(),
            chrono::TimeZone::with_ymd_and_hms(&Utc, 2024, 1, 1, 0, 0, 0).unwrap(),
            path_list.clone(),
            cx,
        )
        .await;

        cx.run_until_parked();
        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Thread A", "  Thread B",]
        );

        // Keyboard confirm preserves selection.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.selection = Some(1);
            sidebar.confirm(&Confirm, window, cx);
        });
        assert_eq!(
            sidebar.read_with(cx, |sidebar, _| sidebar.selection),
            Some(1)
        );

        // Click handlers clear selection to None so no highlight lingers
        // after a click regardless of focus state. The hover style provides
        // visual feedback during mouse interaction instead.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.selection = None;
            let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);
            sidebar.toggle_collapse(&path_list, window, cx);
        });
        assert_eq!(sidebar.read_with(cx, |sidebar, _| sidebar.selection), None);

        // When the user tabs back into the sidebar, focus_in no longer
        // restores selection — it stays None.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.focus_in(window, cx);
        });
        assert_eq!(sidebar.read_with(cx, |sidebar, _| sidebar.selection), None);
    }

    #[gpui::test]
    async fn test_thread_title_update_propagates_to_sidebar(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, &project, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        let connection = StubAgentConnection::new();
        connection.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
            acp::ContentChunk::new("Hi there!".into()),
        )]);
        open_thread_with_connection(&panel, connection, cx);
        send_message(&panel, cx);

        let session_id = active_session_id(&panel, cx);
        save_test_thread_metadata(&session_id, path_list.clone(), cx).await;
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Hello *"]
        );

        // Simulate the agent generating a title. The notification chain is:
        // AcpThread::set_title emits TitleUpdated →
        // ConnectionView::handle_thread_event calls cx.notify() →
        // AgentPanel observer fires and emits AgentPanelEvent →
        // Sidebar subscription calls update_entries / rebuild_contents.
        //
        // Before the fix, handle_thread_event did NOT call cx.notify() for
        // TitleUpdated, so the AgentPanel observer never fired and the
        // sidebar kept showing the old title.
        let thread = panel.read_with(cx, |panel, cx| panel.active_agent_thread(cx).unwrap());
        thread.update(cx, |thread, cx| {
            thread
                .set_title("Friendly Greeting with AI".into(), cx)
                .detach();
        });
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  Friendly Greeting with AI *"]
        );
    }

    #[gpui::test]
    async fn test_focused_thread_tracks_user_intent(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, &project_a, cx);

        let path_list_a = PathList::new(&[std::path::PathBuf::from("/project-a")]);

        // Save a thread so it appears in the list.
        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_a, connection_a, cx);
        send_message(&panel_a, cx);
        let session_id_a = active_session_id(&panel_a, cx);
        save_test_thread_metadata(&session_id_a, path_list_a.clone(), cx).await;

        // Add a second workspace with its own agent panel.
        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, &project_b, cx);
        cx.run_until_parked();

        let workspace_a = multi_workspace.read_with(cx, |mw, _cx| mw.workspaces()[0].clone());

        // ── 1. Initial state: focused thread derived from active panel ─────
        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_a),
                "The active panel's thread should be focused on startup"
            );
        });

        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.activate_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id_a.clone(),
                    work_dirs: None,
                    title: Some("Test".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                &workspace_a,
                window,
                cx,
            );
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_a),
                "After clicking a thread, it should be the focused thread"
            );
            assert!(
                has_thread_entry(sidebar, &session_id_a),
                "The clicked thread should be present in the entries"
            );
        });

        workspace_a.read_with(cx, |workspace, cx| {
            assert!(
                workspace.panel::<AgentPanel>(cx).is_some(),
                "Agent panel should exist"
            );
            let dock = workspace.right_dock().read(cx);
            assert!(
                dock.is_open(),
                "Clicking a thread should open the agent panel dock"
            );
        });

        let connection_b = StubAgentConnection::new();
        connection_b.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
            acp::ContentChunk::new("Thread B".into()),
        )]);
        open_thread_with_connection(&panel_b, connection_b, cx);
        send_message(&panel_b, cx);
        let session_id_b = active_session_id(&panel_b, cx);
        let path_list_b = PathList::new(&[std::path::PathBuf::from("/project-b")]);
        save_test_thread_metadata(&session_id_b, path_list_b.clone(), cx).await;
        cx.run_until_parked();

        // Workspace A is currently active. Click a thread in workspace B,
        // which also triggers a workspace switch.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.activate_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id_b.clone(),
                    work_dirs: None,
                    title: Some("Thread B".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                &workspace_b,
                window,
                cx,
            );
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_b),
                "Clicking a thread in another workspace should focus that thread"
            );
            assert!(
                has_thread_entry(sidebar, &session_id_b),
                "The cross-workspace thread should be present in the entries"
            );
        });

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, window, cx);
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_a),
                "Switching workspace should seed focused_thread from the new active panel"
            );
            assert!(
                has_thread_entry(sidebar, &session_id_a),
                "The seeded thread should be present in the entries"
            );
        });

        let connection_b2 = StubAgentConnection::new();
        connection_b2.set_next_prompt_updates(vec![acp::SessionUpdate::AgentMessageChunk(
            acp::ContentChunk::new(DEFAULT_THREAD_TITLE.into()),
        )]);
        open_thread_with_connection(&panel_b, connection_b2, cx);
        send_message(&panel_b, cx);
        let session_id_b2 = active_session_id(&panel_b, cx);
        save_test_thread_metadata(&session_id_b2, path_list_b.clone(), cx).await;
        cx.run_until_parked();

        // Panel B is not the active workspace's panel (workspace A is
        // active), so opening a thread there should not change focused_thread.
        // This prevents running threads in background workspaces from causing
        // the selection highlight to jump around.
        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_a),
                "Opening a thread in a non-active panel should not change focused_thread"
            );
        });

        workspace_b.update_in(cx, |workspace, window, cx| {
            workspace.focus_handle(cx).focus(window, cx);
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_a),
                "Defocusing the sidebar should not change focused_thread"
            );
        });

        // Switching workspaces via the multi_workspace (simulates clicking
        // a workspace header) should clear focused_thread.
        multi_workspace.update_in(cx, |mw, window, cx| {
            if let Some(index) = mw.workspaces().iter().position(|w| w == &workspace_b) {
                mw.activate_index(index, window, cx);
            }
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_b2),
                "Switching workspace should seed focused_thread from the new active panel"
            );
            assert!(
                has_thread_entry(sidebar, &session_id_b2),
                "The seeded thread should be present in the entries"
            );
        });

        // ── 8. Focusing the agent panel thread keeps focused_thread ────
        // Workspace B still has session_id_b2 loaded in the agent panel.
        // Clicking into the thread (simulated by focusing its view) should
        // keep focused_thread since it was already seeded on workspace switch.
        panel_b.update_in(cx, |panel, window, cx| {
            if let Some(thread_view) = panel.active_conversation_view() {
                thread_view.read(cx).focus_handle(cx).focus(window, cx);
            }
        });
        cx.run_until_parked();

        sidebar.read_with(cx, |sidebar, _cx| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id_b2),
                "Focusing the agent panel thread should set focused_thread"
            );
            assert!(
                has_thread_entry(sidebar, &session_id_b2),
                "The focused thread should be present in the entries"
            );
        });
    }

    #[gpui::test]
    async fn test_new_thread_button_works_after_adding_folder(cx: &mut TestAppContext) {
        let project = init_test_project_with_agent_panel("/project-a", cx).await;
        let fs = cx.update(|cx| <dyn fs::Fs>::global(cx));
        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, &project, cx);

        let path_list_a = PathList::new(&[std::path::PathBuf::from("/project-a")]);

        // Start a thread and send a message so it 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 session_id = active_session_id(&panel, cx);
        save_test_thread_metadata(&session_id, path_list_a.clone(), cx).await;
        cx.run_until_parked();

        // Verify the thread appears in the sidebar.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project-a]", "  Hello *",]
        );

        // The "New Thread" button should NOT be in "active/draft" state
        // because the panel has a thread with messages.
        sidebar.read_with(cx, |sidebar, _cx| {
            assert!(
                !sidebar.active_thread_is_draft,
                "Panel has a thread with messages, so it should not be a draft"
            );
        });

        // Now add a second folder to the workspace, changing the path_list.
        fs.as_fake()
            .insert_tree("/project-b", serde_json::json!({ "src": {} }))
            .await;
        project
            .update(cx, |project, cx| {
                project.find_or_create_worktree("/project-b", true, cx)
            })
            .await
            .expect("should add worktree");
        cx.run_until_parked();

        // The workspace path_list is now [project-a, project-b]. The old
        // thread was stored under [project-a], so it no longer appears in
        // the sidebar list for this workspace.
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert!(
            !entries.iter().any(|e| e.contains("Hello")),
            "Thread stored under the old path_list should not appear: {:?}",
            entries
        );

        // 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!(
                !sidebar.active_thread_is_draft,
                "After adding a folder the panel still has a thread with messages, \
                 so active_thread_is_draft should be false"
            );
        });

        // Actually click "New Thread" by calling create_new_thread and
        // verify a new draft is created.
        let workspace = multi_workspace.read_with(cx, |mw, _cx| mw.workspace().clone());
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.create_new_thread(&workspace, window, cx);
        });
        cx.run_until_parked();

        // After creating a new thread, the panel should now be in draft
        // state (no messages on the new thread).
        sidebar.read_with(cx, |sidebar, _cx| {
            assert!(
                sidebar.active_thread_is_draft,
                "After creating a new thread the panel should be in draft state"
            );
        });
    }

    #[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, &project, cx);

        let path_list = PathList::new(&[std::path::PathBuf::from("/my-project")]);

        // 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);

        let session_id = active_session_id(&panel, cx);
        save_test_thread_metadata(&session_id, path_list.clone(), cx).await;
        cx.run_until_parked();

        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();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [my-project]", "  [+ New Thread]", "  Hello *"],
            "After Cmd-N the sidebar should show a highlighted New Thread entry"
        );

        sidebar.read_with(cx, |sidebar, _cx| {
            assert!(
                sidebar.focused_thread.is_none(),
                "focused_thread should be cleared after Cmd-N"
            );
            assert!(
                sidebar.active_thread_is_draft,
                "the new blank thread should be a draft"
            );
        });
    }

    #[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.
        agent_ui::test_support::init_test(cx);
        cx.update(|cx| {
            cx.update_flags(false, vec!["agent-v2".into()]);
            ThreadStore::init_global(cx);
            SidebarThreadMetadataStore::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!({
                ".git": {
                    "worktrees": {
                        "feature-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        // Worktree checkout pointing back to the main repo.
        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;

        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
        })
        .unwrap();

        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))
            .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 worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(worktree_project.clone(), window, cx)
        });

        let worktree_panel = add_agent_panel(&worktree_workspace, &worktree_project, cx);

        // Switch to the worktree workspace.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(1, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        // 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);

        let session_id = active_session_id(&worktree_panel, cx);
        let wt_path_list = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        save_test_thread_metadata(&session_id, wt_path_list, cx).await;
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project]", "  Hello {wt-feature-a} *"]
        );

        // 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]",
                "  [+ New Thread]",
                "  Hello {wt-feature-a} *"
            ],
            "After Cmd-N in an absorbed worktree, the sidebar should show \
             a highlighted New Thread entry under the main repo header"
        );

        sidebar.read_with(cx, |sidebar, _cx| {
            assert!(
                sidebar.focused_thread.is_none(),
                "focused_thread should be cleared after Cmd-N"
            );
            assert!(
                sidebar.active_thread_is_draft,
                "the new blank thread should be a draft"
            );
        });
    }

    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()
            .with_git_state(std::path::Path::new("/project/.git"), false, |state| {
                state.worktrees.push(git::repository::Worktree {
                    path: std::path::PathBuf::from("/wt/rosewood"),
                    ref_name: Some("refs/heads/rosewood".into()),
                    sha: "abc".into(),
                });
            })
            .unwrap();

        project
            .update(cx, |project, cx| project.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);

        let main_paths = PathList::new(&[std::path::PathBuf::from("/project")]);
        let wt_paths = PathList::new(&[std::path::PathBuf::from("/wt/rosewood")]);
        save_named_thread_metadata("main-t", "Unrelated Thread", &main_paths, cx).await;
        save_named_thread_metadata("wt-t", "Fix Bug", &wt_paths, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Search for "Fix Bug" — should match the thread title.
        type_in_search(&sidebar, "Fix Bug", cx);

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project]", "  Fix Bug {rosewood}  <== selected"],
        );
    }

    #[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 (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 that doesn't exist yet.
        let wt_paths = PathList::new(&[std::path::PathBuf::from("/wt/rosewood")]);
        save_named_thread_metadata("wt-thread", "Worktree Thread", &wt_paths, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Thread appears as a historical (closed) group since no workspace matches its path.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project]",
                "  [+ New Thread]",
                "v [rosewood]",
                "  Worktree Thread",
            ]
        );

        // Now add the worktree to the git state and trigger a rescan.
        fs.as_fake()
            .with_git_state(std::path::Path::new("/project/.git"), true, |state| {
                state.worktrees.push(git::repository::Worktree {
                    path: std::path::PathBuf::from("/wt/rosewood"),
                    ref_name: Some("refs/heads/rosewood".into()),
                    sha: "abc".into(),
                });
            })
            .unwrap();

        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project]", "  Worktree Thread {rosewood}",]
        );
    }

    #[gpui::test]
    async fn test_worktree_workspaces_group_canonically_and_unlink(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-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                        "feature-b": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-b",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        // Two worktree checkouts whose .git files point back to the main repo.
        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;
        fs.insert_tree(
            "/wt-feature-b",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-b",
                "src": {},
            }),
        )
        .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;

        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(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);

        let paths_a = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        let paths_b = PathList::new(&[std::path::PathBuf::from("/wt-feature-b")]);
        save_named_thread_metadata("thread-a", "Thread A", &paths_a, cx).await;
        save_named_thread_metadata("thread-b", "Thread B", &paths_b, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // Both worktree workspaces canonicalize to [/project], so they
        // share a single header even before the main repo workspace exists.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project]",
                "  Thread A {wt-feature-a}",
                "  Thread B {wt-feature-b}",
            ]
        );

        // Configure the main repo to list both worktrees before opening
        // it so the initial git scan picks them up.
        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-b"),
                ref_name: Some("refs/heads/feature-b".into()),
                sha: "bbb".into(),
            });
        })
        .unwrap();

        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;

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(main_project.clone(), window, cx);
        });
        cx.run_until_parked();

        // Adding the main repo workspace doesn't change the grouping —
        // threads are still under the single [project] header, with chips.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project]",
                "  Thread A {wt-feature-a}",
                "  Thread B {wt-feature-b}",
            ]
        );

        // Remove feature-b from the main repo's linked worktrees.
        fs.with_git_state(std::path::Path::new("/project/.git"), true, |state| {
            state
                .worktrees
                .retain(|wt| wt.path != std::path::Path::new("/wt-feature-b"));
        })
        .unwrap();

        cx.run_until_parked();

        // Once feature-b is no longer a linked worktree, its workspace
        // should be pruned and Thread B should appear as its own
        // top-level group — it's no longer part of the [project] family.
        //
        // TODO: This currently fails because prune_stale_worktree_workspaces
        // skips pruning when the main repo workspace is open (the
        // all_workspace_roots guard). The guard needs to be refined so it
        // only protects against incomplete git scans, not completed scans
        // that explicitly omit the worktree.
        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec![
                "v [project]",
                "  Thread A {wt-feature-a}",
                "v [wt-feature-b]  (Closed)",
                "  Thread B",
            ]
        );
    }

    #[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| {
            cx.update_flags(false, vec!["agent-v2".into()]);
            ThreadStore::init_global(cx);
            SidebarThreadMetadataStore::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!({
                ".git": {
                    "worktrees": {
                        "feature-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        // Worktree checkout pointing back to the main repo.
        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;

        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
        })
        .unwrap();

        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))
            .await;
        worktree_project
            .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)
        });

        let worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
            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, &worktree_project, cx);

        // Switch back to the main workspace before setting up the sidebar.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        // 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.
        let wt_paths = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        save_test_thread_metadata(&session_id, wt_paths, 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,
            );
        });
        cx.run_until_parked();

        // The worktree thread should be grouped under the main project
        // and show live running status.
        let entries = visible_entries_as_strings(&sidebar, cx);
        assert_eq!(
            entries,
            vec!["v [project]", "  Hello {wt-feature-a} * (running)",]
        );
    }

    #[gpui::test]
    async fn test_absorbed_worktree_completion_triggers_notification(cx: &mut TestAppContext) {
        agent_ui::test_support::init_test(cx);
        cx.update(|cx| {
            cx.update_flags(false, vec!["agent-v2".into()]);
            ThreadStore::init_global(cx);
            SidebarThreadMetadataStore::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": {
                    "worktrees": {
                        "feature-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;

        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
        })
        .unwrap();

        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))
            .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 worktree_workspace = multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(worktree_project.clone(), window, cx)
        });

        let worktree_panel = add_agent_panel(&worktree_workspace, &worktree_project, cx);

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, 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);

        let session_id = active_session_id(&worktree_panel, cx);
        let wt_paths = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        save_test_thread_metadata(&session_id, wt_paths, 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)",]
        );

        connection.end_turn(session_id, acp::StopReason::EndTurn);
        cx.run_until_parked();

        assert_eq!(
            visible_entries_as_strings(&sidebar, cx),
            vec!["v [project]", "  Hello {wt-feature-a} * (!)",]
        );
    }

    #[gpui::test]
    async fn test_clicking_worktree_thread_opens_workspace_when_none_exists(
        cx: &mut TestAppContext,
    ) {
        init_test(cx);
        let fs = FakeFs::new(cx.executor());

        fs.insert_tree(
            "/project",
            serde_json::json!({
                ".git": {
                    "worktrees": {
                        "feature-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;

        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
        })
        .unwrap();

        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))
            .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);

        // Save a thread for the worktree path (no workspace for it).
        let paths_wt = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        save_named_thread_metadata("thread-wt", "WT Thread", &paths_wt, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        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().len()),
            1,
        );

        // Focus the sidebar and select the worktree thread.
        open_and_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 activate the thread — the main workspace is in the
        // same canonical group, so no new workspace should be opened.
        cx.dispatch_action(Confirm);
        cx.run_until_parked();

        // Workspace count should remain 1 — the existing main workspace
        // covers this canonical group.
        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.workspaces().len()),
            1,
            "no new workspace should be opened when the canonical group already has one",
        );
    }

    #[gpui::test]
    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",
            serde_json::json!({
                ".git": {
                    "worktrees": {
                        "feature-a": {
                            "commondir": "../../",
                            "HEAD": "ref: refs/heads/feature-a",
                        },
                    },
                },
                "src": {},
            }),
        )
        .await;

        fs.insert_tree(
            "/wt-feature-a",
            serde_json::json!({
                ".git": "gitdir: /project/.git/worktrees/feature-a",
                "src": {},
            }),
        )
        .await;

        fs.with_git_state(std::path::Path::new("/project/.git"), false, |state| {
            state.worktrees.push(git::repository::Worktree {
                path: std::path::PathBuf::from("/wt-feature-a"),
                ref_name: Some("refs/heads/feature-a".into()),
                sha: "aaa".into(),
            });
        })
        .unwrap();

        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))
            .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 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.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        let paths_main = PathList::new(&[std::path::PathBuf::from("/project")]);
        let paths_wt = PathList::new(&[std::path::PathBuf::from("/wt-feature-a")]);
        save_named_thread_metadata("thread-main", "Main Thread", &paths_main, cx).await;
        save_named_thread_metadata("thread-wt", "WT Thread", &paths_wt, cx).await;

        multi_workspace.update_in(cx, |_, _window, cx| cx.notify());
        cx.run_until_parked();

        // The worktree thread is grouped under the main repo header.
        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!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            0,
            "main workspace should be active initially"
        );

        // Focus the sidebar and select the absorbed worktree thread.
        open_and_focus_sidebar(&sidebar, cx);
        sidebar.update_in(cx, |sidebar, _window, _cx| {
            sidebar.selection = Some(wt_thread_index);
        });

        // 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.workspaces()[mw.active_workspace_index()].clone()
        });
        assert_eq!(
            active_workspace, worktree_workspace,
            "clicking an absorbed worktree thread should activate the worktree workspace"
        );
    }

    #[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;
        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));

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(project_b, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        // Save a thread with path_list pointing to project-b.
        let path_list_b = PathList::new(&[std::path::PathBuf::from("/project-b")]);
        let session_id = acp::SessionId::new(Arc::from("archived-1"));
        save_test_thread_metadata(&session_id, path_list_b.clone(), cx).await;

        // Ensure workspace A is active.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, window, cx);
        });
        cx.run_until_parked();
        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            0
        );

        // 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(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id.clone(),
                    work_dirs: Some(PathList::new(&[PathBuf::from("/project-b")])),
                    title: Some("Archived Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx.run_until_parked();

        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            1,
            "should have activated the workspace matching the saved path_list"
        );
    }

    #[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));

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(project_b, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        // Start with workspace A active.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(0, window, cx);
        });
        cx.run_until_parked();
        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            0
        );

        // No thread saved to the store – cwd is the only path hint.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: acp::SessionId::new(Arc::from("unknown-session")),
                    work_dirs: Some(PathList::new(&[std::path::PathBuf::from("/project-b")])),
                    title: Some("CWD Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx.run_until_parked();

        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            1,
            "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));

        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.test_add_workspace(project_b, window, cx);
        });

        let sidebar = setup_sidebar(&multi_workspace, cx);

        // Activate workspace B (index 1) to make it the active one.
        multi_workspace.update_in(cx, |mw, window, cx| {
            mw.activate_index(1, window, cx);
        });
        cx.run_until_parked();
        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            1
        );

        // No saved thread, no cwd – should fall back to the active workspace.
        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: acp::SessionId::new(Arc::from("no-context-session")),
                    work_dirs: None,
                    title: Some("Contextless Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx.run_until_parked();

        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.active_workspace_index()),
            1,
            "should have stayed on the active workspace when no path info is available"
        );
    }

    #[gpui::test]
    async fn test_activate_archived_thread_saved_paths_shows_historical_group(
        cx: &mut TestAppContext,
    ) {
        // Thread has saved metadata pointing to a path with no open workspace.
        // Expected: saves metadata and sets focused_thread without opening a new 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 (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().len()),
            1,
            "should start with one workspace"
        );

        sidebar.update_in(cx, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id.clone(),
                    work_dirs: Some(path_list_b),
                    title: Some("New WS Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx.run_until_parked();

        assert_eq!(
            multi_workspace.read_with(cx, |mw, _| mw.workspaces().len()),
            1,
            "should NOT open a second workspace; thread is shown as a closed historical group"
        );

        sidebar.read_with(cx, |sidebar, _| {
            assert_eq!(
                sidebar.focused_thread.as_ref().map(|id| id.to_string()),
                Some(session_id.to_string()),
                "focused_thread should be set to the archived session"
            );
        });
    }

    #[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_entity = multi_workspace_a.root(cx).unwrap();

        let cx_a = &mut gpui::VisualTestContext::from_window(multi_workspace_a.into(), cx);
        let sidebar = setup_sidebar(&multi_workspace_a_entity, cx_a);

        let session_id = acp::SessionId::new(Arc::from("archived-cross-window"));

        sidebar.update_in(cx_a, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id.clone(),
                    work_dirs: Some(PathList::new(&[PathBuf::from("/project-b")])),
                    title: Some("Cross Window Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx_a.run_until_parked();

        assert_eq!(
            multi_workspace_a
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "should not add the other window's workspace into the current window"
        );
        assert_eq!(
            multi_workspace_b
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "should reuse the existing workspace in the other window"
        );
        assert!(
            cx_a.read(|cx| cx.active_window().unwrap()) == *multi_workspace_b,
            "should activate the window that already owns the matching workspace"
        );
        sidebar.read_with(cx_a, |sidebar, _| {
            assert_eq!(
                sidebar.focused_thread, None,
                "source window's sidebar should not eagerly claim focus for a thread opened in another window"
            );
        });
    }

    #[gpui::test]
    async fn test_activate_archived_thread_reuses_workspace_in_another_window_with_target_sidebar(
        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.clone(), window, cx));

        let multi_workspace_a_entity = multi_workspace_a.root(cx).unwrap();
        let multi_workspace_b_entity = multi_workspace_b.root(cx).unwrap();

        let cx_a = &mut gpui::VisualTestContext::from_window(multi_workspace_a.into(), cx);
        let sidebar_a = setup_sidebar(&multi_workspace_a_entity, cx_a);

        let cx_b = &mut gpui::VisualTestContext::from_window(multi_workspace_b.into(), cx);
        let sidebar_b = setup_sidebar(&multi_workspace_b_entity, cx_b);
        let workspace_b = multi_workspace_b_entity.read_with(cx_b, |mw, _| mw.workspace().clone());
        let _panel_b = add_agent_panel(&workspace_b, &project_b, cx_b);

        let session_id = acp::SessionId::new(Arc::from("archived-cross-window-with-sidebar"));

        sidebar_a.update_in(cx_a, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id.clone(),
                    work_dirs: Some(PathList::new(&[PathBuf::from("/project-b")])),
                    title: Some("Cross Window Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx_a.run_until_parked();

        assert_eq!(
            multi_workspace_a
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "should not add the other window's workspace into the current window"
        );
        assert_eq!(
            multi_workspace_b
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "should reuse the existing workspace in the other window"
        );
        assert!(
            cx_a.read(|cx| cx.active_window().unwrap()) == *multi_workspace_b,
            "should activate the window that already owns the matching workspace"
        );
        sidebar_a.read_with(cx_a, |sidebar, _| {
            assert_eq!(
                sidebar.focused_thread, None,
                "source window's sidebar should not eagerly claim focus for a thread opened in another window"
            );
        });
        sidebar_b.read_with(cx_b, |sidebar, _| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id),
                "target window's sidebar should eagerly focus the activated archived thread"
            );
        });
    }

    #[gpui::test]
    async fn test_activate_archived_thread_prefers_current_window_for_matching_paths(
        cx: &mut TestAppContext,
    ) {
        init_test(cx);
        let fs = FakeFs::new(cx.executor());
        fs.insert_tree("/project-a", serde_json::json!({ "src": {} }))
            .await;
        cx.update(|cx| <dyn fs::Fs>::set_global(fs.clone(), cx));

        let project_b = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;
        let project_a = project::Project::test(fs.clone(), ["/project-a".as_ref()], cx).await;

        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_a_entity = multi_workspace_a.root(cx).unwrap();

        let cx_a = &mut gpui::VisualTestContext::from_window(multi_workspace_a.into(), cx);
        let sidebar_a = setup_sidebar(&multi_workspace_a_entity, cx_a);

        let session_id = acp::SessionId::new(Arc::from("archived-current-window"));

        sidebar_a.update_in(cx_a, |sidebar, window, cx| {
            sidebar.activate_archived_thread(
                Agent::NativeAgent,
                acp_thread::AgentSessionInfo {
                    session_id: session_id.clone(),
                    work_dirs: Some(PathList::new(&[PathBuf::from("/project-a")])),
                    title: Some("Current Window Thread".into()),
                    updated_at: None,
                    created_at: None,
                    meta: None,
                },
                window,
                cx,
            );
        });
        cx_a.run_until_parked();

        assert!(
            cx_a.read(|cx| cx.active_window().unwrap()) == *multi_workspace_a,
            "should keep activation in the current window when it already has a matching workspace"
        );
        sidebar_a.read_with(cx_a, |sidebar, _| {
            assert_eq!(
                sidebar.focused_thread.as_ref(),
                Some(&session_id),
                "current window's sidebar should eagerly focus the activated archived thread"
            );
        });
        assert_eq!(
            multi_workspace_a
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "current window should continue reusing its existing workspace"
        );
        assert_eq!(
            multi_workspace_b
                .read_with(cx_a, |mw, _| mw.workspaces().len())
                .unwrap(),
            1,
            "other windows should not be activated just because they also match the saved paths"
        );
    }
}
