worktrunk 0.35.3

A CLI for Git worktree management, designed for parallel AI agent workflows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
//! Interactive branch/worktree selector.
//!
//! A skim-based TUI for selecting and switching between worktrees.

mod items;
mod log_formatter;
mod pager;
mod preview;
mod summary;

use std::cell::RefCell;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, Mutex};
use std::time::Instant;

use anyhow::Context;
// bounded/unbounded/Sender are re-exported by skim::prelude
use dashmap::DashMap;
use skim::prelude::*;
use skim::reader::CommandCollector;
use worktrunk::git::{Repository, current_or_recover};

use super::branch_deletion::delete_branch_if_safe;
use super::handle_switch::{
    approve_switch_hooks, run_pre_switch_hooks, spawn_switch_background_hooks, switch_extra_vars,
};
use super::hooks::{
    HookFailureStrategy, execute_hook, prepare_background_hooks, spawn_hook_pipeline,
};
use super::list::collect;
use super::repository_ext::{RemoveTarget, RepositoryCliExt};
use super::worktree::hooks::PostRemoveContext;
use super::worktree::{
    BranchDeletionMode, RemoveResult, SwitchBranchInfo, SwitchResult, execute_removal,
    execute_switch, offer_bare_repo_worktree_path_fix, path_mismatch, plan_switch,
};
use crate::commands::command_executor::CommandContext;
use crate::output::handle_switch_output;

use items::{HeaderSkimItem, PreviewCache, WorktreeSkimItem};
use preview::{PreviewLayout, PreviewMode, PreviewState};

/// Action selected by the user in the picker.
enum PickerAction {
    /// Switch to the selected worktree (Enter key).
    Switch,
    /// Create a new worktree from the search query (alt-c).
    Create,
}

/// Custom command collector for skim's `reload` action.
///
/// When alt-r is pressed, skim runs `execute-silent` to write the selected branch
/// name to a signal file, then `reload` invokes this collector. The collector reads
/// the signal file, removes the item from the list, and streams the remaining items
/// back to skim — all without leaving the picker.
///
/// Git operations (worktree removal, branch deletion) are deferred to a background
/// thread because skim 0.20 calls `invoke()` on the main event loop thread.
/// Blocking it freezes the TUI.
///
/// Cursor position resets to the first item after reload (skim 0.20 limitation,
/// tracked in #1695).
struct PickerCollector {
    items: Arc<Mutex<Vec<Arc<dyn SkimItem>>>>,
    signal_path: PathBuf,
    repo: Repository,
}

impl PickerCollector {
    /// Execute removal in background: pre-remove hooks + worktree + branch + post-remove hooks.
    ///
    /// Called from a background thread after the picker optimistically removes the item
    /// from the list. The entire operation runs off skim's event loop so the TUI stays
    /// responsive. If pre-remove hooks fail, the removal is aborted (but the item is
    /// already gone from the picker — a tradeoff until we can show in-progress state).
    ///
    /// `repo` is only used for `BranchOnly` deletion. `RemovedWorktree` constructs
    /// its own from `main_path` (which may differ from the picker's startup repo in
    /// bare-repo setups).
    fn do_removal(repo: &Repository, result: &RemoveResult) -> anyhow::Result<()> {
        match result {
            RemoveResult::RemovedWorktree {
                main_path,
                worktree_path,
                branch_name,
                deletion_mode,
                target_branch,
                force_worktree,
                removed_commit,
                ..
            } => {
                let repo = Repository::at(main_path)?;
                let config = repo.user_config();
                let hook_branch = branch_name.as_deref().unwrap_or("HEAD");

                // Run pre-remove hooks (synchronously in this background thread).
                // Non-zero exit aborts the removal, matching `wt remove` semantics.
                let target_ref = repo
                    .worktree_at(main_path)
                    .branch()
                    .ok()
                    .flatten()
                    .unwrap_or_default();
                let target_path_str = worktrunk::path::to_posix_path(&main_path.to_string_lossy());
                let extra_vars: Vec<(&str, &str)> = vec![
                    ("target", &target_ref),
                    ("target_worktree_path", &target_path_str),
                ];
                let pre_ctx =
                    CommandContext::new(&repo, config, Some(hook_branch), worktree_path, false);
                execute_hook(
                    &pre_ctx,
                    worktrunk::HookType::PreRemove,
                    &extra_vars,
                    HookFailureStrategy::FailFast,
                    &[],
                    None, // no display path in TUI context
                )?;

                let output = execute_removal(
                    &repo,
                    worktree_path,
                    branch_name.as_deref(),
                    *deletion_mode,
                    target_branch.as_deref(),
                    *force_worktree,
                )?;
                if let Some(staged) = output.staged_path {
                    let _ = std::fs::remove_dir_all(&staged);
                }

                // Spawn post-remove hooks in background (log to files, no terminal output).
                let post_ctx =
                    CommandContext::new(&repo, config, Some(hook_branch), main_path, false);
                let remove_vars = PostRemoveContext::new(
                    worktree_path,
                    removed_commit.as_deref(),
                    main_path,
                    &repo,
                );
                let extra_vars = remove_vars.extra_vars(hook_branch);
                for steps in prepare_background_hooks(
                    &post_ctx,
                    worktrunk::HookType::PostRemove,
                    &extra_vars,
                    None, // no display path in TUI context
                )? {
                    spawn_hook_pipeline(&post_ctx, steps)?;
                }
            }
            RemoveResult::BranchOnly {
                branch_name,
                deletion_mode,
                ..
            } => {
                if !deletion_mode.should_keep() {
                    let default_branch = repo.default_branch();
                    let target = default_branch.as_deref().unwrap_or("HEAD");
                    let _ =
                        delete_branch_if_safe(repo, branch_name, target, deletion_mode.is_force());
                }
            }
        }
        Ok(())
    }
}

impl CommandCollector for PickerCollector {
    fn invoke(
        &mut self,
        _cmd: &str,
        components_to_stop: Arc<AtomicUsize>,
    ) -> (SkimItemReceiver, Sender<i32>) {
        // Read the removal signal (item output text written by execute-silent)
        if let Ok(signal) = std::fs::read_to_string(&self.signal_path) {
            let selected_output = signal.trim().to_string();
            if !selected_output.is_empty() {
                // Validate removal before touching the list. prepare_worktree_removal
                // runs a few git commands (~15-20ms) — acceptable on skim's event loop.
                // Only remove the item and spawn background deletion if this succeeds.
                let caller_path = self.repo.current_worktree().root().ok();
                let config = self.repo.user_config();

                // Resolve removal target by path when possible (handles both
                // branched and detached worktrees). Branch-only items won't
                // match any worktree path, so they fall through to Branch.
                let worktree_path = self.repo.list_worktrees().ok().and_then(|wts| {
                    // Match by branch first, then fall back to detached (branch: None).
                    let by_branch = wts
                        .iter()
                        .find(|wt| wt.branch.as_deref() == Some(selected_output.as_str()));
                    let matched = by_branch.or_else(|| wts.iter().find(|wt| wt.branch.is_none()));
                    matched.map(|wt| wt.path.clone())
                });
                let target = match &worktree_path {
                    Some(path) => RemoveTarget::Path(path),
                    None => RemoveTarget::Branch(&selected_output),
                };

                let preparation = self.repo.prepare_worktree_removal(
                    target,
                    BranchDeletionMode::SafeDelete,
                    false,
                    config,
                    caller_path,
                    None,
                );

                match preparation {
                    Ok(result) => {
                        // Removal validated — remove item from the picker list.
                        //
                        // Note: skim's `as_any().downcast_ref::<WorktreeSkimItem>()` fails
                        // at runtime (TypeId mismatch between reader thread and main thread
                        // compilation units in skim 0.20). All item lookups use output()
                        // matching instead.
                        {
                            let mut items = self.items.lock().unwrap();
                            items.retain(|item| item.output().as_ref() != selected_output);
                        }

                        // If removing the current worktree, cd to home so skim and git
                        // commands continue to work after the directory disappears.
                        if matches!(
                            &result,
                            RemoveResult::RemovedWorktree {
                                changed_directory: true,
                                ..
                            }
                        ) && let Ok(home) = self.repo.home_path()
                        {
                            let _ = std::env::set_current_dir(&home);
                        }

                        // Defer actual git removal to a background thread so skim's
                        // event loop stays responsive.
                        let repo = self.repo.clone();
                        let _ = std::thread::Builder::new()
                            .name(format!("picker-remove-{selected_output}"))
                            .spawn(move || {
                                if let Err(e) = Self::do_removal(&repo, &result) {
                                    log::warn!(
                                        "picker: failed to remove '{selected_output}': {e:#}"
                                    );
                                }
                            });
                    }
                    Err(e) => {
                        log::info!("picker: cannot remove '{selected_output}': {e:#}");
                    }
                }

                // Clear signal for next removal
                let _ = std::fs::write(&self.signal_path, "");
            }
        }

        // Stream remaining items through a channel for skim to consume.
        // Uses unbounded channel so all items are sent immediately without blocking.
        let items = self.items.lock().unwrap();
        let (tx, rx) = unbounded();
        for item in items.iter() {
            let _ = tx.send(Arc::clone(item));
        }
        drop(tx);

        // Dummy interrupt channel — no subprocess to kill.
        // The reader's collect_item thread handles its own components_to_stop accounting;
        // we just need a valid Sender to satisfy the trait signature.
        let _ = components_to_stop;
        let (tx_interrupt, _rx_interrupt) = bounded(1);
        (rx, tx_interrupt)
    }
}

pub fn handle_picker(
    cli_branches: bool,
    cli_remotes: bool,
    change_dir_flag: Option<bool>,
) -> anyhow::Result<()> {
    // Interactive picker requires a terminal for the TUI
    if !std::io::stdin().is_terminal() {
        anyhow::bail!("Interactive picker requires an interactive terminal");
    }

    let (repo, is_recovered) = current_or_recover()?;

    // Merge CLI flags with resolved config (project-specific config is now available)
    let config = repo.config();
    let change_dir = change_dir_flag.unwrap_or_else(|| config.switch.cd());
    let show_branches = cli_branches || config.list.branches();
    let show_remotes = cli_remotes || config.list.remotes();

    // Initialize preview mode state file (auto-cleanup on drop)
    let state = PreviewState::new();

    // Gather list data using simplified collection (buffered mode)
    // Skip expensive operations not needed for picker UI
    let skip_tasks: std::collections::HashSet<collect::TaskKind> = [
        collect::TaskKind::BranchDiff,
        collect::TaskKind::CiStatus,
        collect::TaskKind::MergeTreeConflicts,
    ]
    .into_iter()
    .collect();

    // Per-task command timeout from shared [list] config.
    let command_timeout = config.list.task_timeout();

    // Wall-clock budget for the entire collect phase (default: 500ms).
    let collect_deadline = config.switch_picker.timeout().map(|d| Instant::now() + d);

    let Some(list_data) = collect::collect(
        &repo,
        collect::ShowConfig::Resolved {
            show_branches,
            show_remotes,
            skip_tasks: skip_tasks.clone(),
            command_timeout,
            collect_deadline,
        },
        false, // show_progress (no progress bars)
        false, // render_table (picker renders its own UI)
        true,  // skip_expensive_for_stale (faster for repos with many stale branches)
    )?
    else {
        return Ok(());
    };

    // Use the same layout system as `wt list` for proper column alignment
    // List width depends on preview position:
    // - Right layout: skim splits ~50% for list, ~50% for preview
    // - Down layout: list gets full width, preview is below
    let terminal_width = crate::display::terminal_width();
    let skim_list_width = match state.initial_layout {
        PreviewLayout::Right => terminal_width / 2,
        PreviewLayout::Down => terminal_width,
    };
    let layout = super::list::layout::calculate_layout_with_width(
        &list_data.items,
        &list_data.skip_tasks,
        skim_list_width,
        &list_data.main_worktree_path,
        None, // URL column not shown in picker
    );

    // Render header using layout system (need both plain and styled text for skim)
    let header_line = layout.render_header_line();
    let header_display_text = header_line.render();
    let header_plain_text = header_line.plain_text();

    // Create shared cache for all preview modes (pre-computed in background)
    let preview_cache: PreviewCache = Arc::new(DashMap::new());

    // Convert to skim items using the layout system for rendering
    // Keep Arc<ListItem> refs for background pre-computation
    let mut items_for_precompute: Vec<Arc<super::list::model::ListItem>> = Vec::new();
    let mut items: Vec<Arc<dyn SkimItem>> = list_data
        .items
        .into_iter()
        .map(|item| {
            let branch_name = item.branch_name().to_string();

            // The picker doesn't update progressively, so any column whose data
            // didn't arrive in time won't fill in later. Use the stale placeholder
            // ("·") for all items — it signals "data not available" rather than the
            // ellipsis ("⋯") which implies data is still loading.
            let rendered_line = layout.render_list_item_stale(&item);
            let display_text_with_ansi = rendered_line.render();
            let display_text = rendered_line.plain_text();

            let item = Arc::new(item);
            items_for_precompute.push(Arc::clone(&item));

            Arc::new(WorktreeSkimItem {
                display_text,
                display_text_with_ansi,
                branch_name,
                item,
                preview_cache: Arc::clone(&preview_cache),
            }) as Arc<dyn SkimItem>
        })
        .collect();

    // Insert header row at the beginning (will be non-selectable via header_lines option)
    items.insert(
        0,
        Arc::new(HeaderSkimItem {
            display_text: header_plain_text,
            display_text_with_ansi: header_display_text,
        }) as Arc<dyn SkimItem>,
    );

    // Get state path for key bindings (shell-escaped for safety)
    let state_path_display = state.path.display().to_string();
    let state_path_str = shell_escape::escape(state_path_display.into()).into_owned();

    // Calculate half-page scroll: skim uses 90% of terminal height, half of that = 45%
    let half_page = terminal_size::terminal_size()
        .map(|(_, terminal_size::Height(h))| (h as usize * 45 / 100).max(5))
        .unwrap_or(10);

    // Calculate preview window spec based on auto-detected layout
    // items.len() - 1 because we added a header row
    let num_items = items.len().saturating_sub(1);
    let preview_window_spec = state.initial_layout.to_preview_window_spec(num_items);

    // Signal file for alt-r removal communication. execute-silent writes the branch
    // name here; the PickerCollector reads it on reload. Cleaned up in PreviewState::Drop.
    let signal_path = state.path.with_extension("remove");

    // Shared items list: the PickerCollector reads and modifies this on reload.
    let shared_items: Arc<Mutex<Vec<Arc<dyn SkimItem>>>> = Arc::new(Mutex::new(items));

    // Custom collector for skim's reload action — performs removal and streams
    // updated items back, all without leaving the picker.
    let collector = PickerCollector {
        items: Arc::clone(&shared_items),
        signal_path: signal_path.clone(),
        repo: repo.clone(),
    };

    let signal_path_escaped =
        shell_escape::escape(signal_path.display().to_string().into()).into_owned();

    // Configure skim options with Rust-based preview and mode switching keybindings
    let options = SkimOptionsBuilder::default()
        .height("90%".to_string())
        // Workaround for skim-tuikit bug: partial-height mode skips smcup but
        // cleanup still sends rmcup, leaving artifacts. no_clear_start forces
        // cursor_goto + erase_down cleanup instead. See skim-rs/skim#880.
        .no_clear_start(true)
        .layout("reverse".to_string())
        .header_lines(1) // Make first line (header) non-selectable
        .multi(false)
        .no_info(true) // Hide info line (matched/total counter)
        .preview(Some("".to_string())) // Enable preview (empty string means use SkimItem::preview())
        .preview_window(preview_window_spec)
        // Color scheme using fzf's --color=light values: dark text (237) on light gray bg (251)
        //
        // Terminal color compatibility is tricky:
        // - current_bg:254 (original): too bright on dark terminals, washes out text
        // - current_bg:236 (fzf dark): too dark on light terminals, jarring contrast
        // - current_bg:251 + current:-1: light bg works on both, but unstyled text
        //   becomes unreadable on dark terminals (light-on-light)
        // - current_bg:251 + current:237: fzf's light theme, best compromise
        //
        // The light theme works universally because:
        // - On dark terminals: light gray highlight stands out clearly
        // - On light terminals: light gray is subtle but visible
        // - Dark text (237) ensures readability regardless of terminal theme
        .color(Some(
            "fg:-1,bg:-1,header:-1,matched:108,current:237,current_bg:251,current_match:108"
                .to_string(),
        ))
        .cmd_collector(Rc::new(RefCell::new(collector)) as Rc<RefCell<dyn CommandCollector>>)
        .bind(vec![
            // Mode switching (1/2/3/4/5 keys change preview content)
            format!(
                "1:execute-silent(echo 1 > {0})+refresh-preview",
                state_path_str
            ),
            format!(
                "2:execute-silent(echo 2 > {0})+refresh-preview",
                state_path_str
            ),
            format!(
                "3:execute-silent(echo 3 > {0})+refresh-preview",
                state_path_str
            ),
            format!(
                "4:execute-silent(echo 4 > {0})+refresh-preview",
                state_path_str
            ),
            format!(
                "5:execute-silent(echo 5 > {0})+refresh-preview",
                state_path_str
            ),
            // Create new worktree with query as branch name (alt-c for "create")
            "alt-c:accept(create)".to_string(),
            // Remove selected worktree: write branch name to signal file, then
            // reload triggers PickerCollector which performs the removal and
            // streams updated items back — all without leaving the picker.
            format!(
                "alt-r:execute-silent(echo {{}} > {0})+reload(remove)",
                signal_path_escaped
            ),
            // Preview toggle (alt-p shows/hides preview)
            // Note: skim doesn't support change-preview-window like fzf, only toggle
            "alt-p:toggle-preview".to_string(),
            // Preview scrolling (half-page based on terminal height)
            format!("ctrl-u:preview-up({half_page})"),
            format!("ctrl-d:preview-down({half_page})"),
        ])
        // Legend/controls moved to preview window tabs (render_preview_tabs)
        .build()
        .map_err(|e| anyhow::anyhow!("Failed to build skim options: {}", e))?;

    // Send initial items to skim via channel
    let items = shared_items.lock().unwrap();
    let (tx, rx): (SkimItemSender, SkimItemReceiver) = unbounded();
    for item in items.iter() {
        tx.send(Arc::clone(item))
            .map_err(|e| anyhow::anyhow!("Failed to send item to skim: {}", e))?;
    }
    drop(tx);
    drop(items);

    // Pre-compute all preview modes for all worktrees in parallel via rayon.
    // Each (worktree, mode) pair is a separate rayon task, allowing the thread pool
    // to overlap I/O-bound git commands. Tasks are fire-and-forget — ongoing
    // git commands are harmless read-only operations even if skim exits early.
    let (preview_width, preview_height) = state.initial_layout.preview_dimensions(num_items);

    let modes = [
        PreviewMode::WorkingTree,
        PreviewMode::Log,
        PreviewMode::BranchDiff,
        PreviewMode::UpstreamDiff,
    ];

    for item in &items_for_precompute {
        for mode in modes {
            let cache = Arc::clone(&preview_cache);
            let item = Arc::clone(item);
            rayon::spawn(move || {
                let cache_key = (item.branch_name().to_string(), mode);
                cache.entry(cache_key).or_insert_with(|| {
                    WorktreeSkimItem::compute_and_page_preview(
                        &item,
                        mode,
                        preview_width,
                        preview_height,
                    )
                });
            });
        }
    }

    // Queue summary generation after tabs 1-4 so git previews get rayon priority.
    if config.list.summary() && config.commit_generation.is_configured() {
        let llm_command = config.commit_generation.command.clone().unwrap();
        for item in &items_for_precompute {
            let item = Arc::clone(item);
            let cache = Arc::clone(&preview_cache);
            let cmd = llm_command.clone();
            let repo = repo.clone();
            rayon::spawn(move || {
                summary::generate_and_cache_summary(&item, &cmd, &cache, &repo);
            });
        }
    } else {
        // No LLM configured or summaries disabled — insert config hint so the
        // tab shows a useful message instead of a perpetual "Generating..." placeholder.
        let hint = if !config.commit_generation.is_configured() {
            "Configure [commit.generation] command to enable LLM summaries.\n\n\
             Example in ~/.config/worktrunk/config.toml:\n\n\
             [commit.generation]\n\
             command = \"llm -m haiku\"\n\n\
             [list]\n\
             summary = true\n"
        } else {
            "Enable summaries in ~/.config/worktrunk/config.toml:\n\n\
             [list]\n\
             summary = true\n"
        };
        for item in &items_for_precompute {
            let branch = item.branch_name().to_string();
            preview_cache.insert((branch, PreviewMode::Summary), hint.to_string());
        }
    }

    // Run skim (single invocation — alt-r uses reload, not re-launch)
    let output = Skim::run_with(&options, Some(rx));

    // Handle selection (signal file cleaned up by PreviewState::Drop)
    if let Some(out) = output
        && !out.is_abort
    {
        // Determine action: create (alt-c) or switch (enter)
        // Remove is handled inline via reload — it never reaches accept.
        let action = match &out.final_event {
            Event::EvActAccept(Some(label)) if label == "create" => PickerAction::Create,
            _ => PickerAction::Switch,
        };

        // --no-cd: just output the selected branch name and exit (read-only, no side effects)
        if !change_dir {
            let selected_name = out
                .selected_items
                .first()
                .map(|item| item.output().to_string());
            let query = out.query.trim().to_string();
            let identifier = resolve_identifier(&action, query, selected_name)?;
            println!("{identifier}");
            return Ok(());
        }

        let should_create = matches!(action, PickerAction::Create);

        // Get branch name: from query if creating new, from selected item if switching.
        // For detached worktrees, use the path (same as `wt switch /path` from CLI).
        let selected = out.selected_items.first();
        let selected_name = selected.map(|item| {
            if !should_create
                && let Some(data) = item
                    .as_any()
                    .downcast_ref::<WorktreeSkimItem>()
                    .and_then(|s| s.item.worktree_data())
                    .filter(|d| d.detached)
            {
                return data.path.to_string_lossy().into_owned();
            }
            item.output().to_string()
        });
        let query = out.query.trim().to_string();
        let identifier = resolve_identifier(&action, query, selected_name)?;

        // Load config — reuse recovered repo if we recovered earlier
        let repo = if is_recovered {
            repo.clone()
        } else {
            Repository::current().context("Failed to switch worktree")?
        };
        // Load config, offering bare repo worktree-path fix if needed.
        // Reload from disk so mutations are picked up by plan_switch.
        let mut config = worktrunk::config::UserConfig::load().context("Failed to load config")?;
        offer_bare_repo_worktree_path_fix(&repo, &mut config)?;

        // Run pre-switch hooks before branch resolution or worktree creation.
        // {{ branch }} receives the raw user input (before resolution).
        // Skip when recovered — the source worktree is gone, nothing to run hooks against.
        if !is_recovered {
            run_pre_switch_hooks(&repo, &config, &identifier, true)?;
        }

        // Switch to existing worktree or create new one
        let plan = plan_switch(&repo, &identifier, should_create, None, false, &config)?;
        let hooks_approved = approve_switch_hooks(&repo, &config, &plan, false, true)?;
        let (result, branch_info) = execute_switch(&repo, plan, &config, false, hooks_approved)?;

        // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).
        // Skip for detached HEAD worktrees (branch is None).
        let branch_info = match &result {
            SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {
                let expected_path = branch_info
                    .branch
                    .as_deref()
                    .and_then(|b| path_mismatch(&repo, b, path, &config));
                SwitchBranchInfo {
                    expected_path,
                    ..branch_info
                }
            }
            _ => branch_info,
        };

        // Show success message; emit cd directive if shell integration is active
        // When recovered from a deleted worktree, fall back to repo_path().
        let fallback_path = repo.repo_path()?.to_path_buf();
        let cwd = std::env::current_dir().unwrap_or(fallback_path.clone());
        let source_root = repo.current_worktree().root().unwrap_or(fallback_path);
        let hooks_display_path =
            handle_switch_output(&result, &branch_info, change_dir, Some(&source_root), &cwd)?;

        // Spawn background hooks after success message
        if hooks_approved {
            let extra_vars = switch_extra_vars(&result);
            spawn_switch_background_hooks(
                &repo,
                &config,
                &result,
                branch_info.branch.as_deref(),
                false,
                &extra_vars,
                hooks_display_path.as_deref(),
            )?;
        }
    }

    Ok(())
}

/// Resolve the branch identifier from picker output.
///
/// Extracted from the picker callback for testability. Used by both the
/// interactive path and the `--no-cd` print path.
fn resolve_identifier(
    action: &PickerAction,
    query: String,
    selected_name: Option<String>,
) -> anyhow::Result<String> {
    match action {
        PickerAction::Create => {
            if query.is_empty() {
                anyhow::bail!("Cannot create worktree: no branch name entered");
            }
            Ok(query)
        }
        PickerAction::Switch => match selected_name {
            Some(name) => Ok(name),
            None => {
                if query.is_empty() {
                    anyhow::bail!("No worktree selected");
                } else {
                    anyhow::bail!(
                        "No worktree matches '{query}' — use alt-c to create a new worktree"
                    );
                }
            }
        },
    }
}

#[cfg(test)]
pub mod tests {
    use super::preview::{PreviewLayout, PreviewMode, PreviewStateData};
    use super::{PickerAction, PickerCollector, resolve_identifier};
    use crate::commands::worktree::{BranchDeletionMode, RemoveResult};
    use std::fs;

    #[test]
    fn test_preview_state_data_roundtrip() {
        let state_path = PreviewStateData::state_path();

        // Write and read back various modes
        let _ = fs::write(&state_path, "1");
        assert_eq!(PreviewStateData::read_mode(), PreviewMode::WorkingTree);

        let _ = fs::write(&state_path, "2");
        assert_eq!(PreviewStateData::read_mode(), PreviewMode::Log);

        let _ = fs::write(&state_path, "3");
        assert_eq!(PreviewStateData::read_mode(), PreviewMode::BranchDiff);

        let _ = fs::write(&state_path, "4");
        assert_eq!(PreviewStateData::read_mode(), PreviewMode::UpstreamDiff);

        let _ = fs::write(&state_path, "5");
        assert_eq!(PreviewStateData::read_mode(), PreviewMode::Summary);

        // Cleanup
        let _ = fs::remove_file(&state_path);
    }

    #[test]
    fn test_preview_layout() {
        // Right uses absolute width derived from terminal size
        let spec = PreviewLayout::Right.to_preview_window_spec(10);
        assert!(spec.starts_with("right:"));

        // Down calculates based on item count
        let spec = PreviewLayout::Down.to_preview_window_spec(5);
        assert!(spec.starts_with("down:"));
    }

    #[test]
    fn test_resolve_identifier() {
        // Switch returns the selected name
        let result = resolve_identifier(
            &PickerAction::Switch,
            String::new(),
            Some("feature/foo".into()),
        );
        assert_eq!(result.unwrap(), "feature/foo");

        // Switch with no selection and empty query
        let result = resolve_identifier(&PickerAction::Switch, String::new(), None);
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("No worktree selected")
        );

        // Switch with no selection but a query — the panic from #1565
        let result = resolve_identifier(&PickerAction::Switch, "nonexistent".into(), None);
        let err = result.unwrap_err().to_string();
        assert!(err.contains("No worktree matches 'nonexistent'"));
        assert!(err.contains("alt-c"));

        // Create returns the query
        let result = resolve_identifier(&PickerAction::Create, "new-branch".into(), None);
        assert_eq!(result.unwrap(), "new-branch");

        // Create with empty query is an error
        let result = resolve_identifier(&PickerAction::Create, String::new(), None);
        assert!(result.unwrap_err().to_string().contains("no branch name"));
    }

    #[test]
    fn test_execute_removal_removes_worktree_and_branch() {
        let test = worktrunk::testing::TestRepo::with_initial_commit();
        let repo = worktrunk::git::Repository::at(test.path()).unwrap();
        let wt_dir = tempfile::tempdir().unwrap();
        let wt_path = wt_dir.path().join("feature");

        repo.run_command(&[
            "worktree",
            "add",
            "-b",
            "feature",
            wt_path.to_str().unwrap(),
        ])
        .unwrap();
        assert!(wt_path.exists());

        let result = RemoveResult::RemovedWorktree {
            main_path: test.path().to_path_buf(),
            worktree_path: wt_path.clone(),
            changed_directory: false,
            branch_name: Some("feature".to_string()),
            deletion_mode: BranchDeletionMode::SafeDelete,
            target_branch: Some("main".to_string()),
            integration_reason: None,
            force_worktree: false,
            expected_path: None,
            removed_commit: None,
        };

        PickerCollector::do_removal(&repo, &result).unwrap();
        assert!(!wt_path.exists(), "worktree should be removed");

        let output = repo.run_command(&["branch", "--list", "feature"]).unwrap();
        assert!(output.is_empty(), "branch should be deleted");
    }

    #[test]
    fn test_do_removal_branch_only_deletes_integrated_branch() {
        let test = worktrunk::testing::TestRepo::with_initial_commit();
        let repo = worktrunk::git::Repository::at(test.path()).unwrap();

        // Create a branch at the same commit (fully integrated into main)
        repo.run_command(&["branch", "feature"]).unwrap();

        let result = RemoveResult::BranchOnly {
            branch_name: "feature".to_string(),
            deletion_mode: BranchDeletionMode::SafeDelete,
            pruned: false,
            target_branch: None,
            integration_reason: None,
        };
        PickerCollector::do_removal(&repo, &result).unwrap();

        let output = repo.run_command(&["branch", "--list", "feature"]).unwrap();
        assert!(output.is_empty(), "integrated branch should be deleted");
    }

    #[test]
    fn test_do_removal_branch_only_retains_unmerged_branch() {
        let test = worktrunk::testing::TestRepo::with_initial_commit();
        let repo = worktrunk::git::Repository::at(test.path()).unwrap();

        // Create a branch with an unmerged commit
        repo.run_command(&["checkout", "-b", "unmerged"]).unwrap();
        fs::write(test.path().join("new.txt"), "unmerged work").unwrap();
        repo.run_command(&["add", "."]).unwrap();
        repo.run_command(&["commit", "-m", "unmerged work"])
            .unwrap();
        repo.run_command(&["checkout", "main"]).unwrap();

        let result = RemoveResult::BranchOnly {
            branch_name: "unmerged".to_string(),
            deletion_mode: BranchDeletionMode::SafeDelete,
            pruned: false,
            target_branch: None,
            integration_reason: None,
        };
        PickerCollector::do_removal(&repo, &result).unwrap();

        // Branch should be retained — SafeDelete won't delete unmerged branches
        let output = repo.run_command(&["branch", "--list", "unmerged"]).unwrap();
        assert!(
            !output.is_empty(),
            "unmerged branch should be retained with SafeDelete"
        );
    }

    #[test]
    fn test_do_removal_removes_detached_worktree() {
        let test = worktrunk::testing::TestRepo::with_initial_commit();
        let repo = worktrunk::git::Repository::at(test.path()).unwrap();
        let wt_dir = tempfile::tempdir().unwrap();
        let wt_path = wt_dir.path().join("detached");

        repo.run_command(&[
            "worktree",
            "add",
            "-b",
            "to-detach",
            wt_path.to_str().unwrap(),
        ])
        .unwrap();

        // Detach HEAD in the new worktree
        worktrunk::shell_exec::Cmd::new("git")
            .args(["checkout", "--detach", "HEAD"])
            .current_dir(&wt_path)
            .run()
            .unwrap();

        assert!(wt_path.exists());

        let result = RemoveResult::RemovedWorktree {
            main_path: test.path().to_path_buf(),
            worktree_path: wt_path.clone(),
            changed_directory: false,
            branch_name: None,
            deletion_mode: BranchDeletionMode::SafeDelete,
            target_branch: Some("main".to_string()),
            integration_reason: None,
            force_worktree: false,
            expected_path: None,
            removed_commit: None,
        };

        PickerCollector::do_removal(&repo, &result).unwrap();
        assert!(!wt_path.exists(), "detached worktree should be removed");
    }

    // Note: skim's `as_any().downcast_ref::<WorktreeSkimItem>()` fails at
    // runtime due to TypeId mismatch between skim's reader thread and the main
    // compilation unit (skim 0.20 bug). The invoke() code path uses output()
    // matching instead. Full invoke() tests require interactive skim — verified
    // via tmux-cli during development.
}