Add a managed worktree browser to the TUI (#43286)

## What changed

Add a searchable “Browse worktrees” option to `/worktree` for local repositories with worktrees enabled. List checkouts from the repository's configured pool, show owner metadata, and let users resume an owner thread or copy a worktree's working directory.

Load worktrees asynchronously and discard results after the loading popup is dismissed or the request context changes. Ignore actions from stale requests and offer copying when owner metadata is missing or invalid.

## Testing

Add popup snapshots and tests for search, resume and copy actions, stale results, and disabled local worktree operations. Extend lifecycle coverage to verify discovery from repository subdirectories and handling of missing or invalid owner metadata.

GitOrigin-RevId: 2b7e694a133278da640b6bb275fd5c1202b271c7
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-06 21:36:02 +00:00
1 parent 8d7cc24a87
commit b053ef9e5a
13 files changed
+457 -3

No files matched your search

@@ -127,6 +127,7 @@ impl ChatWidget {
model_catalog,
model_popup_request_id: None,
permission_popup_request_id: None,
worktree_popup_request_id: None,
permission_profiles_menu_opened: false,
model_popup_model_ids: Vec::new(),
session_telemetry,
@@ -0,0 +1,12 @@
---
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
assertion_line: 114
expression: "render_bottom_popup(&chat, 80)"
---
Worktree
/repo/worktree
› 1. Resume owner thread
2. Copy working directory
Press enter to confirm or esc to go back
@@ -0,0 +1,12 @@
---
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
assertion_line: 100
expression: "render_bottom_popup(&chat, 80)"
---
Managed worktrees
Select a worktree to resume its owner or copy its working directory
› /repo/worktree Owner: 00000000-0000-0000-0000-000000000001
Press enter to confirm or esc to go back
@@ -0,0 +1,10 @@
---
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
assertion_line: 90
expression: "render_bottom_popup(&chat, 80)"
---
Managed worktrees
› Loading worktrees…
Press enter to confirm or esc to go back
@@ -1,12 +1,15 @@
---
source: tui/src/chatwidget/tests/slash_commands.rs
source: tui/src/chatwidget/tests/worktree_picker_tests.rs
assertion_line: 71
expression: popup
---
Create a new worktree
Worktrees
› 1. Continue current conversation Preserve this conversation in the new
checkout
2. Start new conversation Open a fresh conversation in the new
checkout
3. Browse worktrees Resume an owner thread or copy a working
directory
Press enter to confirm or esc to go back
@@ -89,3 +89,85 @@ async fn slash_worktree_offers_current_or_new_conversation() {
assert!(popup.contains("Start new conversation"), "popup: {popup}");
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
}
#[tokio::test]
async fn worktree_browser_actions_and_stale_results() {
use crate::worktree_browser::Entry;
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let checkout = tempdir().unwrap();
std::fs::create_dir(checkout.path().join(".git")).unwrap();
std::fs::write(checkout.path().join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
chat.config.cwd = AbsolutePathBuf::from_absolute_path(checkout.path()).unwrap();
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
let request = chat.request_managed_worktrees().unwrap();
assert_chatwidget_snapshot!(
"worktree_browser_loading",
render_bottom_popup(&chat, /*width*/ 80)
);
let owner = ThreadId::from_string("00000000-0000-0000-0000-000000000001").unwrap();
let entry = Entry {
cwd: PathBuf::from("/repo/worktree"),
owner: Some(owner),
};
chat.on_managed_worktrees_loaded(request, Ok(vec![entry.clone()]));
assert_chatwidget_snapshot!(
"worktree_browser_list",
render_bottom_popup(&chat, /*width*/ 80)
);
for character in "worktree".chars() {
chat.handle_key_event(KeyEvent::from(KeyCode::Char(character)));
}
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
let AppEvent::ShowManagedWorktreeActions {
request,
entry: selected,
} = rx.try_recv().unwrap()
else {
panic!("worktree action");
};
assert_eq!(selected, entry);
chat.show_managed_worktree_actions(request.clone(), selected);
assert_chatwidget_snapshot!(
"worktree_browser_actions",
render_bottom_popup(&chat, /*width*/ 80)
);
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
let AppEvent::ManagedWorktreeAction {
request: selected_request,
action,
} = rx.try_recv().unwrap()
else {
panic!("resume action");
};
assert_matches!(chat.managed_worktree_action(&selected_request, action.clone()), Some(AppEvent::ResumeSessionByIdOrName(id)) if id == owner.to_string());
chat.set_local_worktree_operations(/*enabled*/ false);
assert!(
chat.managed_worktree_action(&selected_request, action)
.is_none()
);
chat.set_local_worktree_operations(/*enabled*/ true);
chat.show_managed_worktree_actions(
request,
Entry {
owner: None,
..entry
},
);
assert!(!render_bottom_popup(&chat, /*width*/ 80).contains("Resume owner"));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
let AppEvent::ManagedWorktreeAction { request, action } = rx.try_recv().unwrap() else {
panic!("copy action");
};
assert_matches!(chat.managed_worktree_action(&request, action), Some(AppEvent::CopySelection { text, .. }) if &*text == "/repo/worktree");
let first = chat.request_managed_worktrees().unwrap();
chat.handle_key_event(KeyEvent::from(KeyCode::Esc));
chat.on_managed_worktrees_loaded(first, Ok(Vec::new()));
assert!(!chat.bottom_pane.has_active_view());
let stale = chat.request_managed_worktrees().unwrap();
let current = chat.request_managed_worktrees().unwrap();
chat.on_managed_worktrees_loaded(stale, Err("stale result".to_string()));
assert_eq!(chat.worktree_popup_request_id, Some(current.id));
chat.config.cwd = AbsolutePathBuf::from_absolute_path(checkout.path().join("other")).unwrap();
chat.on_managed_worktrees_loaded(current, Ok(Vec::new()));
assert!(!chat.bottom_pane.has_active_view());
}
+172 -1
View File
@@ -2,6 +2,11 @@
use super::*;
use crate::app_event::ManagedWorktreeMode;
use crate::worktree_browser::Action;
use crate::worktree_browser::Entry;
use crate::worktree_browser::Request;
const BROWSER_VIEW_ID: &str = "managed-worktrees";
impl ChatWidget {
pub(super) fn managed_worktree_available(&self) -> bool {
@@ -86,7 +91,7 @@ impl ChatWidget {
}
self.bottom_pane.show_selection_view(SelectionViewParams {
title: Some("Create a new worktree".to_string()),
title: Some("Worktrees".to_string()),
footer_hint: Some(standard_popup_hint_line()),
items: vec![
SelectionItem {
@@ -113,9 +118,175 @@ impl ChatWidget {
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "Browse worktrees".to_string(),
description: Some(
"Resume an owner thread or copy a working directory".to_string(),
),
actions: vec![Box::new(|tx| tx.send(AppEvent::BrowseManagedWorktrees))],
dismiss_on_select: true,
..Default::default()
},
],
..Default::default()
});
self.request_redraw();
}
pub(crate) fn request_managed_worktrees(&mut self) -> Option<Request> {
if !self.managed_worktree_available() {
return None;
}
let request = Request {
id: uuid::Uuid::new_v4(),
cwd: self.config.cwd.to_path_buf(),
thread_id: self.thread_id,
};
self.bottom_pane.dismiss_view_by_id(BROWSER_VIEW_ID);
self.worktree_popup_request_id = Some(request.id);
self.bottom_pane.show_selection_view(SelectionViewParams {
view_id: Some(BROWSER_VIEW_ID),
title: Some("Managed worktrees".to_string()),
items: vec![SelectionItem {
name: "Loading worktrees…".to_string(),
is_disabled: true,
..Default::default()
}],
footer_hint: Some(standard_popup_hint_line()),
..Default::default()
});
Some(request)
}
fn worktree_request_is_current(&self, request: &Request) -> bool {
self.worktree_popup_request_id == Some(request.id)
&& request.cwd == self.config.cwd.as_path()
&& request.thread_id == self.thread_id
&& self.managed_worktree_available()
}
pub(crate) fn on_managed_worktrees_loaded(
&mut self,
request: Request,
result: Result<Vec<Entry>, String>,
) {
if self.worktree_popup_request_id != Some(request.id) {
return;
}
if !self.worktree_request_is_current(&request)
|| !self.bottom_pane.dismiss_active_view_if_id(BROWSER_VIEW_ID)
{
self.worktree_popup_request_id = None;
self.bottom_pane.dismiss_view_by_id(BROWSER_VIEW_ID);
return;
}
let entries = match result {
Ok(entries) => entries,
Err(error) => {
self.worktree_popup_request_id = None;
self.add_error_message(format!("Cannot list managed worktrees: {error}"));
return;
}
};
self.bottom_pane.show_selection_view(SelectionViewParams {
title: Some("Managed worktrees".to_string()),
subtitle: Some(
if entries.is_empty() {
"No worktrees in this repository's configured pool"
} else {
"Select a worktree to resume its owner or copy its working directory"
}
.to_string(),
),
is_searchable: true,
items: entries
.into_iter()
.map(|entry| {
let request = request.clone();
SelectionItem {
name: entry.cwd.display().to_string(),
search_value: Some(entry.cwd.display().to_string()),
description: Some(entry.owner.map_or_else(
|| "No owner metadata".to_string(),
|owner| format!("Owner: {owner}"),
)),
actions: vec![Box::new(move |tx| {
tx.send(AppEvent::ShowManagedWorktreeActions {
request: request.clone(),
entry: entry.clone(),
})
})],
dismiss_on_select: true,
..Default::default()
}
})
.collect(),
footer_hint: Some(standard_popup_hint_line()),
..Default::default()
});
}
pub(crate) fn managed_worktree_action(
&self,
request: &Request,
action: Action,
) -> Option<AppEvent> {
if !self.worktree_request_is_current(request) {
return None;
}
Some(match action {
Action::Resume(owner) => AppEvent::ResumeSessionByIdOrName(owner.to_string()),
Action::Copy(cwd) => AppEvent::CopySelection {
text: cwd.to_str()?.into(),
label: "Worktree working directory".to_string(),
format: crate::clipboard_copy::CopyFormat::PlainText,
},
})
}
pub(crate) fn show_managed_worktree_actions(&mut self, request: Request, entry: Entry) {
if !self.worktree_request_is_current(&request) {
return;
}
let mut items = Vec::new();
if let Some(owner) = entry.owner {
let request = request.clone();
items.push(SelectionItem {
name: "Resume owner thread".to_string(),
actions: vec![Box::new(move |tx| {
tx.send(AppEvent::ManagedWorktreeAction {
request: request.clone(),
action: Action::Resume(owner),
})
})],
dismiss_on_select: true,
..Default::default()
});
}
let cwd = entry.cwd.clone();
items.push(SelectionItem {
name: "Copy working directory".to_string(),
is_disabled: entry.cwd.to_str().is_none(),
disabled_reason: entry
.cwd
.to_str()
.is_none()
.then(|| "Path is not valid UTF-8".to_string()),
actions: vec![Box::new(move |tx| {
tx.send(AppEvent::ManagedWorktreeAction {
request: request.clone(),
action: Action::Copy(cwd.clone()),
})
})],
dismiss_on_select: true,
..Default::default()
});
self.bottom_pane.show_selection_view(SelectionViewParams {
title: Some("Worktree".to_string()),
subtitle: Some(entry.cwd.display().to_string()),
items,
footer_hint: Some(standard_popup_hint_line()),
..Default::default()
});
}
}