Defer managed worktree transitions to fresh TUI loop iterations (#43298)

## Why

Managed worktree transitions run through the event handler into the large synchronous `ChatWidget` constructor. Split this work so each stage runs on a fresh event-loop stack.

## What changed

- Queue worktree setup, checkout completion and configuration loading, session transition, and widget attachment as separate stages with boxed futures.
- Recheck the source session before transitioning and report retained checkout cleanup instructions when the transition cannot proceed.
- Keep the old configuration paired with the old widget until attachment, and protect input during the handoff.

## Testing

Update worktree lifecycle and submission tests to drain the deferred stages explicitly and assert that checkout creation starts before completion.

GitOrigin-RevId: 0435f56d08bf3a6392d35b5c1cfdcc0f1fe02753
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-06 22:34:30 +00:00
1 parent 3cd6004dc4
commit 52e12e0cb5
10 files changed
+235 -36

No files matched your search

+8
View File
@@ -635,7 +635,15 @@ pub(crate) struct App {
tokio::sync::broadcast::Sender<codex_app_server_protocol::ThreadStatusChangedNotification>,
dynamic_tool_tasks: HashMap<codex_app_server_protocol::RequestId, (String, JoinHandle<()>)>,
pending_startup_thread_start: bool,
/// Starts worktree setup after the event handler returns, with a fresh stack.
pending_start_managed_worktree: Option<(crate::app_event::ManagedWorktreeMode, Option<String>)>,
pending_managed_worktree_creation: bool,
/// Defers checkout completion and config loading until the event handler returns.
pending_managed_worktree_created: Option<Box<crate::app_event::ManagedWorktreeCreated>>,
/// Defers the saved-history fork until the event handler has returned.
pending_managed_worktree_transition: Option<Box<crate::app_event::ManagedWorktreeTransition>>,
/// Holds notifications until the new widget is attached on a fresh loop iteration.
pending_managed_worktree_attach: Option<Box<working_directory::ManagedWorktreeAttach>>,
/// Keeps protected screens quarantined until initialized chat receives genuine user input.
startup_protected_input_boundary: bool,
/// Keeps that boundary armed while a startup approval waits for the typing-idle timer.