mirror of
https://github.com/openai/codex.git
synced 2026-11-26 07:26:11 +00:00
Add managed worktree creation to TUI session commands (#43120)
## What changed - Add `/worktree` to start a fresh conversation or fork the current conversation into a new managed checkout. - Offer current-checkout and new-worktree choices for `/new` and `/fork`, preserving optional conversation names. Enable these choices for local Git repositories when the `worktrees` feature is enabled. - Load destination configuration, bind the checkout to the replacement thread, and inherit conversation history only when forking. - Reject creation from explicitly untrusted sources and require an idle primary session without queued input. Refuse forks when developer instructions differ, with guidance for removing the unused checkout. ## Testing Add picker snapshots and tests for command availability, named selections, fresh versus forked history, destination configuration, worktree ownership, and feature and trust checks. GitOrigin-RevId: d6c83e00217660aacf2a6d92acad50b506607e0b
This commit is contained in:
1 parent
f5a71ff40a
commit
f6976ab036
28 files changed
+980
-30
No files matched your search
@@ -135,6 +135,7 @@ impl ChatWidget {
|
||||
status_account_display,
|
||||
runtime_model_provider_base_url,
|
||||
remote_connection: None,
|
||||
local_worktree_operations: true,
|
||||
token_info: None,
|
||||
token_usage_pending: false,
|
||||
rate_limit_snapshots_by_limit_id: BTreeMap::new(),
|
||||
@@ -287,6 +288,7 @@ impl ChatWidget {
|
||||
.set_collaboration_modes_enabled(/*enabled*/ true);
|
||||
widget.sync_service_tier_commands();
|
||||
widget.sync_personality_command_enabled();
|
||||
widget.sync_worktrees_enabled();
|
||||
widget.sync_plugins_command_enabled();
|
||||
widget.sync_goal_command_enabled();
|
||||
widget.sync_mentions_v2_enabled();
|
||||
|
||||
@@ -132,6 +132,7 @@ impl ChatWidget {
|
||||
}
|
||||
self.sync_service_tier_commands();
|
||||
self.sync_personality_command_enabled();
|
||||
self.sync_worktrees_enabled();
|
||||
self.sync_plugins_command_enabled();
|
||||
self.sync_goal_command_enabled();
|
||||
self.refresh_plugin_mentions();
|
||||
|
||||
@@ -80,6 +80,9 @@ impl ChatWidget {
|
||||
self.refresh_effective_service_tier();
|
||||
self.sync_service_tier_commands();
|
||||
}
|
||||
if feature == Feature::Worktrees {
|
||||
self.sync_worktrees_enabled();
|
||||
}
|
||||
if feature == Feature::Personality {
|
||||
self.sync_personality_command_enabled();
|
||||
}
|
||||
@@ -296,6 +299,17 @@ impl ChatWidget {
|
||||
.unwrap_or_else(|| self.current_collaboration_mode.model())
|
||||
}
|
||||
|
||||
pub(crate) fn set_local_worktree_operations(&mut self, enabled: bool) {
|
||||
self.local_worktree_operations = enabled;
|
||||
self.sync_worktrees_enabled();
|
||||
}
|
||||
|
||||
pub(super) fn sync_worktrees_enabled(&mut self) {
|
||||
self.bottom_pane.set_worktrees_enabled(
|
||||
self.config.features.enabled(Feature::Worktrees) && self.local_worktree_operations,
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn sync_personality_command_enabled(&mut self) {
|
||||
self.bottom_pane
|
||||
.set_personality_command_enabled(self.config.features.enabled(Feature::Personality));
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//! slash-command recall follows the same submitted-input rule as ordinary text.
|
||||
|
||||
use super::*;
|
||||
use crate::app_event::ManagedWorktreeMode;
|
||||
use crate::app_event::ThreadGoalSetMode;
|
||||
use crate::bottom_pane::prompt_args::parse_slash_name;
|
||||
use crate::bottom_pane::slash_commands::BuiltinCommandFlags;
|
||||
@@ -181,7 +182,7 @@ impl ChatWidget {
|
||||
self.request_redraw();
|
||||
}
|
||||
SlashCommand::New => {
|
||||
self.app_event_tx.send(AppEvent::NewSession { name: None });
|
||||
self.show_session_checkout_picker(ManagedWorktreeMode::New, /*name*/ None);
|
||||
}
|
||||
SlashCommand::Archive => {
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
@@ -247,8 +248,10 @@ impl ChatWidget {
|
||||
self.app_event_tx.send(AppEvent::OpenResumePicker);
|
||||
}
|
||||
SlashCommand::Fork => {
|
||||
self.app_event_tx
|
||||
.send(AppEvent::ForkCurrentSession { name: None });
|
||||
self.show_session_checkout_picker(ManagedWorktreeMode::Fork, /*name*/ None);
|
||||
}
|
||||
SlashCommand::Worktree => {
|
||||
self.show_managed_worktree_picker();
|
||||
}
|
||||
SlashCommand::App => {
|
||||
let Some(thread_id) = self.thread_id else {
|
||||
@@ -800,9 +803,10 @@ impl ChatWidget {
|
||||
self.app_event_tx.set_thread_name(name);
|
||||
}
|
||||
SlashCommand::New if !trimmed.is_empty() => {
|
||||
self.app_event_tx.send(AppEvent::NewSession {
|
||||
name: Some(trimmed.to_string()),
|
||||
});
|
||||
self.show_session_checkout_picker(
|
||||
ManagedWorktreeMode::New,
|
||||
Some(trimmed.to_string()),
|
||||
);
|
||||
}
|
||||
SlashCommand::Clear if !trimmed.is_empty() => {
|
||||
self.app_event_tx.send(AppEvent::ClearUi {
|
||||
@@ -810,9 +814,10 @@ impl ChatWidget {
|
||||
});
|
||||
}
|
||||
SlashCommand::Fork if !trimmed.is_empty() => {
|
||||
self.app_event_tx.send(AppEvent::ForkCurrentSession {
|
||||
name: Some(trimmed.to_string()),
|
||||
});
|
||||
self.show_session_checkout_picker(
|
||||
ManagedWorktreeMode::Fork,
|
||||
Some(trimmed.to_string()),
|
||||
);
|
||||
}
|
||||
SlashCommand::Plan if !trimmed.is_empty() => {
|
||||
let plan_available = self.apply_plan_slash_command();
|
||||
@@ -1139,6 +1144,8 @@ impl ChatWidget {
|
||||
goal_command_enabled: self.config.features.enabled(Feature::Goals),
|
||||
service_tier_commands_enabled: self.fast_mode_enabled(),
|
||||
personality_command_enabled: self.config.features.enabled(Feature::Personality),
|
||||
worktrees_enabled: self.config.features.enabled(Feature::Worktrees)
|
||||
&& self.local_worktree_operations,
|
||||
allow_elevate_sandbox,
|
||||
side_conversation_active: self.active_side_conversation,
|
||||
}
|
||||
@@ -1182,6 +1189,13 @@ impl ChatWidget {
|
||||
Some(thread_id) if self.can_change_working_directory(thread_id) => QueueDrain::Stop,
|
||||
_ => QueueDrain::Continue,
|
||||
},
|
||||
SlashCommand::Worktree => {
|
||||
if self.managed_worktree_available() {
|
||||
QueueDrain::Stop
|
||||
} else {
|
||||
QueueDrain::Continue
|
||||
}
|
||||
}
|
||||
SlashCommand::Feedback
|
||||
| SlashCommand::Export
|
||||
| SlashCommand::New
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/slash_commands.rs
|
||||
assertion_line: 3091
|
||||
expression: popup
|
||||
---
|
||||
› /work
|
||||
|
||||
/worktree start or continue a conversation in a new worktree
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/slash_commands.rs
|
||||
assertion_line: 3091
|
||||
expression: popup
|
||||
---
|
||||
› /work
|
||||
|
||||
gpt-5.6-sol default · /tmp/project
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/slash_commands.rs
|
||||
expression: popup
|
||||
---
|
||||
Create a new worktree
|
||||
|
||||
› 1. Continue current conversation Preserve this conversation in the new
|
||||
checkout
|
||||
2. Start new conversation Open a fresh conversation in the new
|
||||
checkout
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/slash_commands.rs
|
||||
expression: popup
|
||||
---
|
||||
Where should the forked conversation run?
|
||||
|
||||
› 1. Current checkout Keep using the current working directory
|
||||
2. New worktree Create an isolated managed checkout
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: tui/src/chatwidget/tests/slash_commands.rs
|
||||
expression: popup
|
||||
---
|
||||
Where should the new conversation run?
|
||||
|
||||
› 1. Current checkout Keep using the current working directory
|
||||
2. New worktree Create an isolated managed checkout
|
||||
|
||||
Press enter to confirm or esc to go back
|
||||
@@ -273,6 +273,8 @@ mod status_command_tests;
|
||||
mod status_surface_previews;
|
||||
mod terminal_title;
|
||||
mod usage;
|
||||
#[path = "tests/worktree_picker_tests.rs"]
|
||||
mod worktree_picker;
|
||||
|
||||
pub(crate) use helpers::make_chatwidget_manual_with_sender;
|
||||
pub(crate) use helpers::set_chatgpt_auth;
|
||||
|
||||
@@ -621,7 +621,7 @@ async fn queued_inline_rename_does_not_drain_again_before_turn_started() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_unknown_slash_reports_error_when_dequeued() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
chat.thread_id = Some(ThreadId::new());
|
||||
handle_turn_started(&mut chat, "turn-1");
|
||||
|
||||
@@ -642,6 +642,24 @@ async fn queued_unknown_slash_reports_error_when_dequeued() {
|
||||
"expected delayed slash error, got {rendered:?}"
|
||||
);
|
||||
assert!(chat.input_queue.queued_user_messages.is_empty());
|
||||
|
||||
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
|
||||
chat.set_local_worktree_operations(/*enabled*/ false);
|
||||
let drain = chat.submit_queued_slash_prompt(UserMessage::from("/worktree extra").into());
|
||||
assert_matches!(drain, QueueDrain::Continue);
|
||||
assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty));
|
||||
assert!(drain_insert_history(&mut rx).iter().any(|lines| {
|
||||
lines_to_single_string(lines).contains("Unrecognized command '/worktree'")
|
||||
}));
|
||||
|
||||
chat.set_local_worktree_operations(/*enabled*/ true);
|
||||
let non_git = tempfile::tempdir().unwrap();
|
||||
chat.config.cwd = non_git.path().to_path_buf().abs();
|
||||
let drain = chat.submit_queued_slash_prompt(UserMessage::from("/worktree").into());
|
||||
assert_matches!(drain, QueueDrain::Continue);
|
||||
assert!(drain_insert_history(&mut rx).iter().any(|lines| {
|
||||
lines_to_single_string(lines).contains("Managed worktrees require a local Git repository.")
|
||||
}));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
//! Worktree picker behavior and rendered choices.
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn slash_new_and_fork_offer_checkout_choices_inside_local_git_repository() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let checkout = tempdir().expect("temporary checkout");
|
||||
std::fs::create_dir(checkout.path().join(".git")).expect("git directory");
|
||||
std::fs::write(checkout.path().join(".git/HEAD"), "ref: refs/heads/main\n").expect("git HEAD");
|
||||
chat.config.cwd =
|
||||
AbsolutePathBuf::from_absolute_path(checkout.path()).expect("absolute checkout");
|
||||
|
||||
chat.dispatch_command(SlashCommand::Fork);
|
||||
assert_matches!(
|
||||
rx.try_recv(),
|
||||
Ok(AppEvent::ForkCurrentSession { name: None })
|
||||
);
|
||||
chat.dispatch_command(SlashCommand::New);
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::NewSession { name: None }));
|
||||
|
||||
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
|
||||
chat.dispatch_command(SlashCommand::Fork);
|
||||
|
||||
let popup = render_bottom_popup(&chat, /*width*/ 80);
|
||||
assert_chatwidget_snapshot!("worktrees_fork_choices", popup);
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
chat.dispatch_command(SlashCommand::New);
|
||||
let popup = render_bottom_popup(&chat, /*width*/ 80);
|
||||
assert_chatwidget_snapshot!("worktrees_new_choices", popup);
|
||||
assert!(popup.contains("Current checkout"), "popup: {popup}");
|
||||
assert!(popup.contains("New worktree"), "popup: {popup}");
|
||||
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
chat.bottom_pane
|
||||
.set_composer_text("/new named".into(), Vec::new(), Vec::new());
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE));
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::NewSession { name: Some(name) }) if name == "named");
|
||||
chat.bottom_pane
|
||||
.set_composer_text("/fork named".into(), Vec::new(), Vec::new());
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||||
chat.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::StartManagedWorktree {
|
||||
mode: crate::app_event::ManagedWorktreeMode::Fork,
|
||||
name: Some(name),
|
||||
}) if name == "named");
|
||||
chat.set_local_worktree_operations(/*enabled*/ false);
|
||||
chat.dispatch_command(SlashCommand::New);
|
||||
assert_matches!(rx.try_recv(), Ok(AppEvent::NewSession { name: None }));
|
||||
chat.dispatch_command(SlashCommand::Fork);
|
||||
assert_matches!(
|
||||
rx.try_recv(),
|
||||
Ok(AppEvent::ForkCurrentSession { name: None })
|
||||
);
|
||||
for (available, snapshot) in [
|
||||
(false, "worktrees_command_remote"),
|
||||
(true, "worktrees_command_local"),
|
||||
] {
|
||||
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
|
||||
chat.set_local_worktree_operations(available);
|
||||
chat.bottom_pane
|
||||
.set_composer_text("/work".into(), Vec::new(), Vec::new());
|
||||
let popup = normalize_snapshot_paths(render_bottom_popup(&chat, /*width*/ 80));
|
||||
assert_chatwidget_snapshot!(snapshot, popup);
|
||||
assert_eq!(popup.contains("/worktree"), available);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn slash_worktree_offers_current_or_new_conversation() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let checkout = tempdir().expect("temporary checkout");
|
||||
std::fs::create_dir(checkout.path().join(".git")).expect("git directory");
|
||||
std::fs::write(checkout.path().join(".git/HEAD"), "ref: refs/heads/main\n").expect("git HEAD");
|
||||
chat.config.cwd =
|
||||
AbsolutePathBuf::from_absolute_path(checkout.path()).expect("absolute checkout");
|
||||
|
||||
chat.set_feature_enabled(Feature::Worktrees, /*enabled*/ true);
|
||||
chat.dispatch_command(SlashCommand::Worktree);
|
||||
|
||||
let popup = render_bottom_popup(&chat, /*width*/ 80);
|
||||
assert_chatwidget_snapshot!("worktrees_conversation_choices", popup);
|
||||
assert!(
|
||||
popup.contains("Continue current conversation"),
|
||||
"popup: {popup}"
|
||||
);
|
||||
assert!(popup.contains("Start new conversation"), "popup: {popup}");
|
||||
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
//! Worktree choices for local, feature-enabled session commands.
|
||||
|
||||
use super::*;
|
||||
use crate::app_event::ManagedWorktreeMode;
|
||||
|
||||
impl ChatWidget {
|
||||
pub(super) fn managed_worktree_available(&self) -> bool {
|
||||
self.config.features.enabled(Feature::Worktrees)
|
||||
&& self.local_worktree_operations
|
||||
&& get_git_repo_root(self.config.cwd.as_path()).is_some()
|
||||
}
|
||||
|
||||
pub(super) fn show_session_checkout_picker(
|
||||
&mut self,
|
||||
mode: ManagedWorktreeMode,
|
||||
name: Option<String>,
|
||||
) {
|
||||
if !self.managed_worktree_available() {
|
||||
match mode {
|
||||
ManagedWorktreeMode::New => {
|
||||
self.app_event_tx.send(AppEvent::NewSession { name });
|
||||
}
|
||||
ManagedWorktreeMode::Fork => {
|
||||
self.app_event_tx
|
||||
.send(AppEvent::ForkCurrentSession { name });
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let title = match mode {
|
||||
ManagedWorktreeMode::New => "Where should the new conversation run?",
|
||||
ManagedWorktreeMode::Fork => "Where should the forked conversation run?",
|
||||
};
|
||||
let current_name = name.clone();
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
title: Some(title.to_string()),
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
items: vec![
|
||||
SelectionItem {
|
||||
name: "Current checkout".to_string(),
|
||||
description: Some("Keep using the current working directory".to_string()),
|
||||
actions: vec![Box::new(move |tx| match mode {
|
||||
ManagedWorktreeMode::New => {
|
||||
tx.send(AppEvent::NewSession {
|
||||
name: current_name.clone(),
|
||||
});
|
||||
}
|
||||
ManagedWorktreeMode::Fork => {
|
||||
tx.send(AppEvent::ForkCurrentSession {
|
||||
name: current_name.clone(),
|
||||
});
|
||||
}
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
SelectionItem {
|
||||
name: "New worktree".to_string(),
|
||||
description: Some("Create an isolated managed checkout".to_string()),
|
||||
actions: vec![Box::new(move |tx| {
|
||||
tx.send(AppEvent::StartManagedWorktree {
|
||||
mode,
|
||||
name: name.clone(),
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
self.request_redraw();
|
||||
}
|
||||
|
||||
pub(super) fn show_managed_worktree_picker(&mut self) {
|
||||
if !self.config.features.enabled(Feature::Worktrees) {
|
||||
self.add_error_message(
|
||||
"Enable worktrees in /experimental to create a worktree.".to_string(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if !self.managed_worktree_available() {
|
||||
self.add_error_message("Managed worktrees require a local Git repository.".to_string());
|
||||
return;
|
||||
}
|
||||
|
||||
self.bottom_pane.show_selection_view(SelectionViewParams {
|
||||
title: Some("Create a new worktree".to_string()),
|
||||
footer_hint: Some(standard_popup_hint_line()),
|
||||
items: vec![
|
||||
SelectionItem {
|
||||
name: "Continue current conversation".to_string(),
|
||||
description: Some("Preserve this conversation in the new checkout".to_string()),
|
||||
actions: vec![Box::new(|tx| {
|
||||
tx.send(AppEvent::StartManagedWorktree {
|
||||
mode: ManagedWorktreeMode::Fork,
|
||||
name: None,
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
SelectionItem {
|
||||
name: "Start new conversation".to_string(),
|
||||
description: Some("Open a fresh conversation in the new checkout".to_string()),
|
||||
actions: vec![Box::new(|tx| {
|
||||
tx.send(AppEvent::StartManagedWorktree {
|
||||
mode: ManagedWorktreeMode::New,
|
||||
name: None,
|
||||
});
|
||||
})],
|
||||
dismiss_on_select: true,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
self.request_redraw();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user