mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Include linked worktrees in TUI session discovery (#43279)
## Why Directory-scoped session lookup misses conversations in linked worktrees of the same repository. Managed worktree creation also runs synchronous Git work on the TUI event loop. ## What changed - With `worktrees` enabled for local sessions, include corresponding directories across linked checkouts in resume and fork pickers and `--last` lookup. Group them together in the agents overview and show checkout paths for picker entries from another worktree. - Validate shared Git metadata and backlinks, preserve relative working directories and logical path aliases, and reject directory symlink escapes. Keep the expanded directory filter stable across pagination until the listing restarts. - Fall back to the originally requested directory when an older daemon rejects an array-valued `cwd` filter. - Create managed worktrees on a blocking worker, check transition blockers before allocation, and revalidate the source session before switching. Report retained checkout paths and cleanup instructions if the transition cannot complete. - Start the in-session picker server in a separate task to avoid exhausting the TUI event future's stack. ## Testing Add coverage for linked-checkout discovery, feature gating, picker selection and rendering, project grouping, pagination stability, and legacy daemon fallback. Extend worktree lifecycle tests to cover running-agent blockers and recovery after the source session changes during creation. GitOrigin-RevId: 4a8a36c0dba3d32d72600f2edf8258a29fb4cb07
This commit is contained in:
1 parent
ad931a45b2
commit
d30f9cc72a
27 files changed
+1511
-144
No files matched your search
@@ -41,6 +41,23 @@ use tokio_tungstenite::tungstenite::Message;
|
||||
pub(super) type RecordedRequests = Arc<Mutex<Vec<JSONRPCRequest>>>;
|
||||
pub(super) type RecordingAppServer = (AppServerSession, RecordedRequests, JoinHandle<Result<()>>);
|
||||
|
||||
async fn complete_managed_worktree_creation(
|
||||
app: &mut App,
|
||||
tui: &mut crate::tui::Tui,
|
||||
server: &mut AppServerSession,
|
||||
events: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
|
||||
) -> Result<()> {
|
||||
loop {
|
||||
let event = tokio::time::timeout(Duration::from_secs(15), events.recv())
|
||||
.await?
|
||||
.ok_or_else(|| color_eyre::eyre::eyre!("worktree creation event channel closed"))?;
|
||||
if matches!(event, AppEvent::ManagedWorktreeCreated(_)) {
|
||||
app.handle_event(tui, server, event).await?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn same_thread_retry_keeps_subscription_and_restores_draft() -> Result<()> {
|
||||
let (mut app, codex_home) = make_history_test_app().await?;
|
||||
@@ -2947,6 +2964,7 @@ model_reasoning_effort = "low"
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
complete_managed_worktree_creation(&mut app, &mut tui, &mut server, &mut events).await?;
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(original));
|
||||
assert!(recorded_params(&requests, "thread/fork").is_empty());
|
||||
let unused = manager
|
||||
@@ -2976,6 +2994,26 @@ model_reasoning_effort = "low"
|
||||
.split_once("; remove it with")
|
||||
.expect("cleanup marker");
|
||||
insta::assert_snapshot!(error.replace(path, "<CHECKOUT>"), @"■ Cannot fork into this worktree because developer instructions differ. Start a new conversation instead. An unused checkout was created at <CHECKOUT>; remove it with `git worktree remove <checkout-path>` from the source repository.");
|
||||
let retained = std::iter::from_fn(|| events.try_recv().ok())
|
||||
.filter_map(|event| match event {
|
||||
AppEvent::InsertHistoryCell(cell) => {
|
||||
Some(lines_to_single_string(&cell.display_lines(/*width*/ 500)))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.find(|message| message.contains("A checkout was retained at"))
|
||||
.expect("post-allocation recovery message");
|
||||
let (_, with_path) = retained
|
||||
.split_once("A checkout was retained at ")
|
||||
.expect("retained checkout marker");
|
||||
let (path, _) = with_path
|
||||
.split_once("; remove it with")
|
||||
.expect("retained checkout cleanup marker");
|
||||
assert_eq!(
|
||||
dunce::canonicalize(path)?,
|
||||
dunce::canonicalize(&unused.root)?
|
||||
);
|
||||
assert!(retained.contains("git worktree remove <checkout-path>"));
|
||||
fs::write(
|
||||
source.join(".codex/config.toml"),
|
||||
r#"developer_instructions = "committed policy"
|
||||
@@ -2994,8 +3032,63 @@ terminal_visualization_instructions = true
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
complete_managed_worktree_creation(&mut app, &mut tui, &mut server, &mut events).await?;
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(original));
|
||||
assert!(recorded_params(&requests, "thread/fork").is_empty());
|
||||
while events.try_recv().is_ok() {}
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
AppEvent::StartManagedWorktree {
|
||||
mode: ManagedWorktreeMode::New,
|
||||
name: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(original));
|
||||
assert!(app.pending_managed_worktree_creation);
|
||||
let created = loop {
|
||||
let event = tokio::time::timeout(Duration::from_secs(15), events.recv())
|
||||
.await?
|
||||
.ok_or_else(|| color_eyre::eyre::eyre!("worktree completion channel closed"))?;
|
||||
if let AppEvent::ManagedWorktreeCreated(created) = event {
|
||||
break created;
|
||||
}
|
||||
};
|
||||
let stale_checkout = created
|
||||
.result
|
||||
.as_ref()
|
||||
.expect("created checkout")
|
||||
.1
|
||||
.root
|
||||
.clone();
|
||||
app.primary_thread_id = Some(ThreadId::new());
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
AppEvent::ManagedWorktreeCreated(created),
|
||||
)
|
||||
.await?;
|
||||
app.primary_thread_id = Some(original);
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(original));
|
||||
assert!(!app.pending_managed_worktree_creation);
|
||||
let stale_error = std::iter::from_fn(|| events.try_recv().ok())
|
||||
.filter_map(|event| match event {
|
||||
AppEvent::InsertHistoryCell(cell) => {
|
||||
Some(lines_to_single_string(&cell.display_lines(/*width*/ 500)))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.find(|message| message.contains("A checkout was retained at"))
|
||||
.expect("stale creation recovery message");
|
||||
assert!(stale_error.contains(&stale_checkout.display().to_string()));
|
||||
assert!(stale_error.contains("git worktree remove <checkout-path>"));
|
||||
assert_eq!(
|
||||
manager
|
||||
.owner(&stale_checkout)
|
||||
.map_err(|error| color_eyre::eyre::eyre!(error.to_string()))?,
|
||||
None
|
||||
);
|
||||
for mode in [ManagedWorktreeMode::New, ManagedWorktreeMode::Fork] {
|
||||
requests.lock().expect("request recorder lock").clear();
|
||||
let previous = app.chat_widget.thread_id();
|
||||
@@ -3009,6 +3102,7 @@ terminal_visualization_instructions = true
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
complete_managed_worktree_creation(&mut app, &mut tui, &mut server, &mut events).await?;
|
||||
let replacement = app.chat_widget.thread_id().expect("replacement thread");
|
||||
assert_ne!(Some(replacement), previous);
|
||||
assert_eq!(
|
||||
@@ -3105,6 +3199,10 @@ terminal_visualization_instructions = true
|
||||
)
|
||||
.await?;
|
||||
let is_new = mode == ManagedWorktreeMode::New;
|
||||
if is_new {
|
||||
complete_managed_worktree_creation(&mut app, &mut tui, &mut server, &mut events)
|
||||
.await?;
|
||||
}
|
||||
assert_eq!(app.chat_widget.thread_id() != Some(unsaved), is_new);
|
||||
assert_eq!(recorded_params(&requests, "thread/fork").len(), 0);
|
||||
}
|
||||
|
||||
@@ -89,6 +89,51 @@ async fn worktree_creation_rejects_untrusted_source_before_allocation() -> Resul
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worktree_creation_rejects_running_agent_before_allocation() -> Result<()> {
|
||||
let (mut app, mut events, _op_rx) = make_test_app_with_channels().await;
|
||||
let home = tempfile::tempdir()?;
|
||||
app.config.codex_home = home.path().to_path_buf().abs();
|
||||
app.config.features.enable(Feature::Worktrees)?;
|
||||
app.config.active_project.trust_level = Some(codex_protocol::config_types::TrustLevel::Trusted);
|
||||
let primary = ThreadId::new();
|
||||
app.primary_thread_id = Some(primary);
|
||||
app.chat_widget
|
||||
.handle_thread_session_quiet(test_thread_session(primary, app.config.cwd.to_path_buf()));
|
||||
let child = ThreadId::new();
|
||||
app.agent_navigation.upsert(
|
||||
child, /*agent_nickname*/ None, /*agent_role*/ None, /*is_closed*/ false,
|
||||
);
|
||||
app.agent_navigation.set_running(child, /*is_running*/ true);
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
|
||||
while events.try_recv().is_ok() {}
|
||||
|
||||
app.handle_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
AppEvent::StartManagedWorktree {
|
||||
mode: crate::app_event::ManagedWorktreeMode::New,
|
||||
name: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert!(!app.pending_managed_worktree_creation);
|
||||
assert!(!home.path().join("worktrees").exists());
|
||||
let message = std::iter::from_fn(|| events.try_recv().ok())
|
||||
.find_map(|event| match event {
|
||||
AppEvent::InsertHistoryCell(cell) => {
|
||||
Some(lines_to_single_string(&cell.display_lines(/*width*/ 100)))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.expect("running agent message");
|
||||
insta::assert_snapshot!(message, @"■ Cannot change: another agent is running.");
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn turn_start_failure_is_shown_without_exiting() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
|
||||
Reference in new issue
Block a user